Ticket #142: db.adb

File db.adb, 3.2 KB (added by andry.ogorodnik, 9 years ago)
Line 
1
2with Ada.Unchecked_Deallocation;
3
4with SQL.Databases;
5with SQL.Queries;
6with Matreshka.Internals.SQL_Drivers.SQLite3.Factory;
7
8with League.Strings;
9with League.Holders.Integers;
10with League.Text_Codecs;
11
12package body DB is
13
14 type Database_Access is access all SQL.Databases.SQL_Database;
15
16 procedure Free is new
17 Ada.Unchecked_Deallocation (SQL.Databases.SQL_Database, Database_Access);
18
19 Connect : Database_Access;
20
21 ASCII_Codec : constant League.Text_Codecs.Text_Codec :=
22 League.Text_Codecs.Codec
23 (League.Strings.To_Universal_String ("ISO-8859-1"));
24
25 function "+"
26 (Item : Wide_Wide_String) return League.Strings.Universal_String
27 renames League.Strings.To_Universal_String;
28
29 ------------------
30 -- Add_Currency --
31 ------------------
32
33 procedure Add_Currency
34 (Id : Integer;
35 Ratio : Integer;
36 Version : Integer)
37 is
38 Query : aliased SQL.Queries.SQL_Query := Connect.Query
39 (+("INSERT INTO CURRENCIES (ID, VERSION, RATIO) "
40 & "VALUES (:ID, :VERSION, :RATIO)"));
41
42 use League.Holders;
43 begin
44 Query.Bind_Value (+":ID", Integers.To_Holder (Id));
45
46 Query.Bind_Value
47 (+":VERSION", Integers.To_Holder (Version));
48
49 Query.Bind_Value
50 (+":RATIO", Integers.To_Holder (Ratio));
51
52 Query.Execute;
53 Connect.Commit;
54 end Add_Currency;
55
56 -----------
57 -- Close --
58 -----------
59
60 procedure Close is
61 begin
62 if Connect /= null then
63 Connect.Close;
64 Free (Connect);
65 end if;
66 end Close;
67
68 ---------------------
69 -- Delete_Currency --
70 ---------------------
71
72 procedure Delete_Currency (Id : Integer)
73 is
74 Query : aliased SQL.Queries.SQL_Query := Connect.Query
75 (+"DELETE FROM CURRENCIES WHERE ID=:ID");
76
77 use League.Holders.Integers;
78 begin
79 Query.Bind_Value (+":ID", To_Holder (Id));
80
81 Query.Execute;
82 Connect.Commit;
83 end Delete_Currency;
84
85 ----------
86 -- Open --
87 ----------
88
89 procedure Open is
90 DB_Driver : constant League.Strings.Universal_String :=
91 League.Strings.To_Universal_String ("SQLITE3");
92 DB_Options : constant League.Strings.Universal_String :=
93 League.Strings.To_Universal_String ("local.db");
94 begin
95 Connect := new SQL.Databases.SQL_Database'
96 (SQL.Databases.Create (DB_Driver, DB_Options));
97
98 Connect.Open;
99
100 declare
101 Query : SQL.Queries.SQL_Query := Connect.Query
102 (+("CREATE TABLE IF NOT EXISTS CURRENCIES "
103 & "(ID NOT NULL, VERSION NOT NULL, RATIO NOT NULL)"));
104 begin
105 Query.Execute;
106 end;
107
108 Connect.Commit;
109 end Open;
110
111 ---------------------
112 -- Update_Currency --
113 ---------------------
114
115 procedure Update_Currency
116 (Id : Integer;
117 Ratio : Integer;
118 Version : Integer)
119 is
120 Query : aliased SQL.Queries.SQL_Query := Connect.Query
121 (+("UPDATE CURRENCIES SET VERSION=:VERSION, "
122 & "RATIO=:RATIO WHERE ID=:ID"));
123
124 use League.Holders.Integers;
125 begin
126 Query.Bind_Value (+":ID", To_Holder (Id));
127 Query.Bind_Value (+":VERSION", To_Holder (Version));
128 Query.Bind_Value (+":RATIO", To_Holder (Ratio));
129 Query.Execute;
130
131 Connect.Commit;
132 end Update_Currency;
133
134end DB;