1 with Ada
.Text_IO
; use Ada
.Text_IO
;
2 with Backlit
.Postgres
; use Backlit
.Postgres
;
4 procedure Simple_Demo
is
10 procedure Create_Film_Table
15 Put_Line
("Create_Film_Table");
19 -- the command can be split in more than one line, in fact
20 -- the command will be executed as a single command.
21 -- check the documentation for more info.
22 D
.Append_Command
("DROP TABLE films;");
25 when Backlit
.Query_Error
=>
26 Put_Line
(Error_Message
(R
));
28 D
.Append_Command
("CREATE TABLE films (");
29 D
.Append_Command
(" code char(5) CONSTRAINT firstkey PRIMARY KEY,");
30 D
.Append_Command
(" title varchar(40) NOT NULL,");
31 D
.Append_Command
(" did integer NOT NULL,");
32 D
.Append_Command
(" date_prod date,");
33 D
.Append_Command
(" kind varchar(10),");
34 D
.Append_Command
(" len interval hour to minute);");
36 Put
("Create_Film_Table :");
37 if R
.Status
= COMMAND_OK
then
43 when Backlit
.Query_Error
=>
44 Put_Line
(Error_Message
(R
));
45 end Create_Film_Table
;
47 procedure Insert_Value_1
(D
: in out DBD_Type
)
51 D
.Append_Command
("INSERT INTO films");
52 D
.Append_Command
("(code, title, did,date_prod, kind, len)");
53 D
.Append_Command
("VALUES (1, 'test1', 1, '12-24-2008', 'test', '1h35m');");
56 when Backlit
.Query_Error
=>
57 Put_Line
(Error_Message
(R
));
60 procedure Prepare_1
(D
: in out DBD_Type
)
63 Put_Line
("prepare_procedure :");
64 D
.Append_Command
("INSERT INTO films");
65 D
.Append_Command
("(code, title, did,date_prod, kind, len)");
66 D
.Append_Command
("VALUES ($1, $2, $3, $4, $5, $6);");
67 D
.Prepare
("films_insert", 6);
70 procedure Insert_Prepared_1
78 Put_Line
("=> code :" & Count
'Img);
79 D
.Bind_Parameter
(6, "2h45m");
80 D
.Bind_Parameter
(4, "01-30-2007");
81 D
.Bind_Parameter
(2, "test" & Count
'Img);
82 D
.Bind_Parameter
(3, Count
'img);
83 D
.Bind_Parameter
(5, "test" & Count
'Img);
84 D
.Bind_Parameter
(1, Count
'Img);
85 -- The bind parameter can be put in any order
86 -- thanks to Ada.Containers.Indefinite_Ordered_Maps.
87 D
.Exec
(R
,"films_insert");
88 -- we insert an error here :
90 Put_Line
("=> We insert an error here : duplicate key");
95 when Backlit
.Query_Error
=>
96 Put_Line
(Error_Message
(R
));
97 end Insert_Prepared_1
;
99 procedure Insert_Value_2
(D
: in out DBD_Type
)
102 Code
: Natural := 20;
104 Put_Line
("=> We insert directly some value with code 20 named test 20");
105 D
.Append_Command
("INSERT INTO films");
106 D
.Append_Command
("(code, title, did,date_prod, kind, len)");
107 D
.Append_Command
("VALUES ($1, $2, $3, $4, $5, $6);");
108 D
.Bind_Parameter
(6, "2h45m");
109 D
.Bind_Parameter
(4, "01-30-2007");
110 D
.Bind_Parameter
(2, "test 20");
111 D
.Bind_Parameter
(3, "3");
112 D
.Bind_Parameter
(5, "test 20");
113 D
.Bind_Parameter
(1, Code
'Img);
114 -- The bind parameter can be put in any order
115 -- thanks to Ada.Containers.Indefinite_Ordered_Maps.
118 when Backlit
.Query_Error
=>
119 Put_Line
(Error_Message
(R
));
122 procedure select_1
(D
: in out DBD_Type
)
128 D
.Append_Command
("SELECT code, title FROM films");
129 D
.Append_Command
("WHERE did=$1 OR date_prod > $2;");
130 D
.Bind_Parameter
(1, "1");
131 D
.Bind_Parameter
(2, "01-30-2000");
133 if R
.Status
= TUPLES_OK
then
135 Put_Line
("number of tuples :" & NT
'Img);
137 Put_Line
("number of fields :"& NF
'Img);
139 for I
in First_Tuples
(R
) .. Last_Tuples
(R
) loop
141 for J
in First_Fields
(R
) .. Last_Fields
(R
) loop
142 Put
(R
.Get_Value
(I
, J
));
151 when Backlit
.Query_Error
=>
152 Put_Line
(Error_Message
(R
));
157 ---------------------------
158 -- Database Connection --
159 ---------------------------
160 ---- there is two way to set the connection
161 ---- but you can't use both on the same connection.
163 -- D.Set_Host_Name ("localhost");
164 -- D.Set_Host_Address ("127.0.0.1");
165 -- D.Set_User_Name ("postgres");
166 -- D.Set_db_Name ("template1");
167 -- D.Set_User_Password ("secret");
168 -- D.Set_Port ("5432");
169 ---- and then make a server connection
172 -- The other way is to pass the connection parameter
173 -- as a string at the connection time
174 Put_Line
("stating connection...");
175 D
.Connect
("host=localhost user=postgres dbname=template1");
177 -------------------------
178 -- command execution --
179 -------------------------
180 if Is_Connected
(D
) then
183 Put
("connection failure!");
186 -- you may create your own connection procedure
187 -- giving the user different try for the password
189 -- check the Needs_Password and Used_Password functions.
191 Create_Film_Table
(D
);
192 -- Insert_Value_1 (D);
196 Insert_Prepared_1
(D1
);
204 when Backlit
.Error
=>
205 Put_Line
(Error_Message
(D
));