Ticket #138: main2.adb

File main2.adb, 2.2 KB (added by Maxim Reznik, 9 years ago)
Line 
1with Ada.Wide_Wide_Text_IO;
2
3with League.Strings;
4with League.Holders.Floats;
5with League.Holders.Integers;
6--with Matreshka.Internals.SQL_Drivers.PostgreSQL.Factory;
7with Matreshka.Internals.SQL_Drivers.SQLite3.Factory;
8with SQL.Databases;
9with SQL.Queries;
10
11procedure Main is
12
13 function "+"
14 (Item : Wide_Wide_String) return League.Strings.Universal_String
15 renames League.Strings.To_Universal_String;
16
17 -- PostgreSQL
18
19-- DB_Driver : constant League.Strings.Universal_String := +"POSTGRESQL";
20-- DB_Options : constant League.Strings.Universal_String := +"";
21 -- Example: +"user='me' password='my' dbname='db'"
22
23 -- SQLite3
24
25 DB_Driver : constant League.Strings.Universal_String := +"SQLITE3";
26 DB_Options : constant League.Strings.Universal_String := +"test.db";
27
28 D : aliased SQL.Databases.SQL_Database
29 := SQL.Databases.Create (DB_Driver, DB_Options);
30
31begin
32 D.Open;
33
34 declare
35 Q : SQL.Queries.SQL_Query := D.Query;
36
37 begin
38 Q.Prepare
39 (+"CREATE TABLE point (x INTEGER, y CHARACTER VARYING, z FLOAT)");
40 Q.Execute;
41 end;
42
43 declare
44 Q : SQL.Queries.SQL_Query := D.Query;
45
46 begin
47 Q.Prepare (+"INSERT INTO point (x, y, z) VALUES (:x, :y, :z)");
48 Q.Bind_Value (+":z", League.Holders.Floats.To_Holder (4.5));
49 Q.Bind_Value (+":y", League.Holders.To_Holder (+"xyz"));
50 Q.Bind_Value (+":x", League.Holders.Integers.To_Holder (5));
51 Q.Execute;
52 Q.Bind_Value (+":z", League.Holders.Floats.To_Holder (6.6));
53 Q.Bind_Value (+":y", League.Holders.To_Holder (+"6.6"));
54 Q.Bind_Value (+":x", League.Holders.Integers.To_Holder (6));
55 Q.Execute;
56 end;
57
58 declare
59 Q : aliased SQL.Queries.SQL_Query := D.Query;
60
61 begin
62 Q.Prepare (+"SELECT y FROM point where x=:x");
63 Q.Bind_Value (+":x", League.Holders.Integers.To_Holder (5));
64 Q.Execute;
65
66 if Q.Next then
67 Ada.Wide_Wide_Text_IO.Put_Line
68 (League.Holders.Element (Q.Value (1)).To_Wide_Wide_String);
69 end if;
70
71 Q.Bind_Value (+":x", League.Holders.Integers.To_Holder (6));
72 Q.Execute;
73
74 if Q.Next then
75 Ada.Wide_Wide_Text_IO.Put_Line
76 (League.Holders.Element (Q.Value (1)).To_Wide_Wide_String);
77 end if;
78 end;
79
80 D.Close;
81end Main;