adamc@866
|
1 (* Copyright (c) 2008-2009, Adam Chlipala
|
adamc@866
|
2 * All rights reserved.
|
adamc@866
|
3 *
|
adamc@866
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@866
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@866
|
6 *
|
adamc@866
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@866
|
8 * this list of conditions and the following disclaimer.
|
adamc@866
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@866
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@866
|
11 * and/or other materials provided with the distribution.
|
adamc@866
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@866
|
13 * derived from this software without specific prior written permission.
|
adamc@866
|
14 *
|
adamc@866
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@866
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@866
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@866
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@866
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@866
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@866
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@866
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@866
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@866
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@866
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@866
|
26 *)
|
adamc@866
|
27
|
adamc@866
|
28 structure Postgres :> POSTGRES = struct
|
adamc@866
|
29
|
adamc@866
|
30 open Settings
|
adamc@866
|
31 open Print.PD
|
adamc@866
|
32 open Print
|
adamc@866
|
33
|
adamc@870
|
34 val ident = String.translate (fn #"'" => "PRIME"
|
adamc@870
|
35 | ch => str ch)
|
adamc@870
|
36
|
adamc@873
|
37 fun p_sql_type t =
|
adamc@873
|
38 case t of
|
adamc@873
|
39 Int => "int8"
|
adamc@873
|
40 | Float => "float8"
|
adamc@873
|
41 | String => "text"
|
adamc@873
|
42 | Bool => "bool"
|
adamc@873
|
43 | Time => "timestamp"
|
adamc@873
|
44 | Blob => "bytea"
|
adamc@873
|
45 | Channel => "int8"
|
adamc@873
|
46 | Client => "int4"
|
adamc@873
|
47 | Nullable t => p_sql_type t
|
adamc@873
|
48
|
adamc@870
|
49 fun p_sql_type_base t =
|
adamc@870
|
50 case t of
|
adamc@871
|
51 Int => "bigint"
|
adamc@871
|
52 | Float => "double precision"
|
adamc@870
|
53 | String => "text"
|
adamc@871
|
54 | Bool => "boolean"
|
adamc@871
|
55 | Time => "timestamp without time zone"
|
adamc@870
|
56 | Blob => "bytea"
|
adamc@871
|
57 | Channel => "bigint"
|
adamc@871
|
58 | Client => "integer"
|
adamc@870
|
59 | Nullable t => p_sql_type_base t
|
adamc@870
|
60
|
adamc@872
|
61 fun checkRel (table, checkNullable) (s, xts) =
|
adamc@871
|
62 let
|
adamc@871
|
63 val sl = CharVector.map Char.toLower s
|
adamc@871
|
64
|
adamc@872
|
65 val q = "SELECT COUNT(*) FROM information_schema." ^ table ^ " WHERE table_name = '"
|
adamc@871
|
66 ^ sl ^ "'"
|
adamc@871
|
67
|
adamc@871
|
68 val q' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '",
|
adamc@871
|
69 sl,
|
adamc@871
|
70 "' AND (",
|
adamc@871
|
71 String.concatWith " OR "
|
adamc@871
|
72 (map (fn (x, t) =>
|
adamc@871
|
73 String.concat ["(column_name = 'uw_",
|
adamc@871
|
74 CharVector.map
|
adamc@871
|
75 Char.toLower (ident x),
|
adamc@871
|
76 "' AND data_type = '",
|
adamc@871
|
77 p_sql_type_base t,
|
adamc@872
|
78 "'",
|
adamc@872
|
79 if checkNullable then
|
adamc@872
|
80 (" AND is_nullable = '"
|
adamc@872
|
81 ^ (if isNotNull t then
|
adamc@872
|
82 "NO"
|
adamc@872
|
83 else
|
adamc@872
|
84 "YES")
|
adamc@872
|
85 ^ "'")
|
adamc@871
|
86 else
|
adamc@872
|
87 "",
|
adamc@872
|
88 ")"]) xts),
|
adamc@871
|
89 ")"]
|
adamc@871
|
90
|
adamc@871
|
91 val q'' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '",
|
adamc@871
|
92 sl,
|
adamc@871
|
93 "' AND column_name LIKE 'uw_%'"]
|
adamc@871
|
94 in
|
adamc@871
|
95 box [string "res = PQexec(conn, \"",
|
adamc@871
|
96 string q,
|
adamc@871
|
97 string "\");",
|
adamc@871
|
98 newline,
|
adamc@871
|
99 newline,
|
adamc@871
|
100 string "if (res == NULL) {",
|
adamc@871
|
101 newline,
|
adamc@871
|
102 box [string "PQfinish(conn);",
|
adamc@871
|
103 newline,
|
adamc@871
|
104 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@871
|
105 newline],
|
adamc@871
|
106 string "}",
|
adamc@871
|
107 newline,
|
adamc@871
|
108 newline,
|
adamc@871
|
109 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@871
|
110 newline,
|
adamc@871
|
111 box [string "char msg[1024];",
|
adamc@871
|
112 newline,
|
adamc@871
|
113 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@871
|
114 newline,
|
adamc@871
|
115 string "msg[1023] = 0;",
|
adamc@871
|
116 newline,
|
adamc@871
|
117 string "PQclear(res);",
|
adamc@871
|
118 newline,
|
adamc@871
|
119 string "PQfinish(conn);",
|
adamc@871
|
120 newline,
|
adamc@871
|
121 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@871
|
122 string q,
|
adamc@871
|
123 string "\\n%s\", msg);",
|
adamc@871
|
124 newline],
|
adamc@871
|
125 string "}",
|
adamc@871
|
126 newline,
|
adamc@871
|
127 newline,
|
adamc@871
|
128 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
|
adamc@871
|
129 newline,
|
adamc@871
|
130 box [string "PQclear(res);",
|
adamc@871
|
131 newline,
|
adamc@871
|
132 string "PQfinish(conn);",
|
adamc@871
|
133 newline,
|
adamc@871
|
134 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@871
|
135 string s,
|
adamc@871
|
136 string "' does not exist.\");",
|
adamc@871
|
137 newline],
|
adamc@871
|
138 string "}",
|
adamc@871
|
139 newline,
|
adamc@871
|
140 newline,
|
adamc@871
|
141 string "PQclear(res);",
|
adamc@871
|
142 newline,
|
adamc@871
|
143
|
adamc@871
|
144 string "res = PQexec(conn, \"",
|
adamc@871
|
145 string q',
|
adamc@871
|
146 string "\");",
|
adamc@871
|
147 newline,
|
adamc@871
|
148 newline,
|
adamc@871
|
149 string "if (res == NULL) {",
|
adamc@871
|
150 newline,
|
adamc@871
|
151 box [string "PQfinish(conn);",
|
adamc@871
|
152 newline,
|
adamc@871
|
153 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@871
|
154 newline],
|
adamc@871
|
155 string "}",
|
adamc@871
|
156 newline,
|
adamc@871
|
157 newline,
|
adamc@871
|
158 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@871
|
159 newline,
|
adamc@871
|
160 box [string "char msg[1024];",
|
adamc@871
|
161 newline,
|
adamc@871
|
162 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@871
|
163 newline,
|
adamc@871
|
164 string "msg[1023] = 0;",
|
adamc@871
|
165 newline,
|
adamc@871
|
166 string "PQclear(res);",
|
adamc@871
|
167 newline,
|
adamc@871
|
168 string "PQfinish(conn);",
|
adamc@871
|
169 newline,
|
adamc@871
|
170 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@871
|
171 string q',
|
adamc@871
|
172 string "\\n%s\", msg);",
|
adamc@871
|
173 newline],
|
adamc@871
|
174 string "}",
|
adamc@871
|
175 newline,
|
adamc@871
|
176 newline,
|
adamc@871
|
177 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
|
adamc@871
|
178 string (Int.toString (length xts)),
|
adamc@871
|
179 string "\")) {",
|
adamc@871
|
180 newline,
|
adamc@871
|
181 box [string "PQclear(res);",
|
adamc@871
|
182 newline,
|
adamc@871
|
183 string "PQfinish(conn);",
|
adamc@871
|
184 newline,
|
adamc@871
|
185 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@871
|
186 string s,
|
adamc@871
|
187 string "' has the wrong column types.\");",
|
adamc@871
|
188 newline],
|
adamc@871
|
189 string "}",
|
adamc@871
|
190 newline,
|
adamc@871
|
191 newline,
|
adamc@871
|
192 string "PQclear(res);",
|
adamc@871
|
193 newline,
|
adamc@871
|
194 newline,
|
adamc@871
|
195
|
adamc@871
|
196 string "res = PQexec(conn, \"",
|
adamc@871
|
197 string q'',
|
adamc@871
|
198 string "\");",
|
adamc@871
|
199 newline,
|
adamc@871
|
200 newline,
|
adamc@871
|
201 string "if (res == NULL) {",
|
adamc@871
|
202 newline,
|
adamc@871
|
203 box [string "PQfinish(conn);",
|
adamc@871
|
204 newline,
|
adamc@871
|
205 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@871
|
206 newline],
|
adamc@871
|
207 string "}",
|
adamc@871
|
208 newline,
|
adamc@871
|
209 newline,
|
adamc@871
|
210 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@871
|
211 newline,
|
adamc@871
|
212 box [string "char msg[1024];",
|
adamc@871
|
213 newline,
|
adamc@871
|
214 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@871
|
215 newline,
|
adamc@871
|
216 string "msg[1023] = 0;",
|
adamc@871
|
217 newline,
|
adamc@871
|
218 string "PQclear(res);",
|
adamc@871
|
219 newline,
|
adamc@871
|
220 string "PQfinish(conn);",
|
adamc@871
|
221 newline,
|
adamc@871
|
222 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@871
|
223 string q'',
|
adamc@871
|
224 string "\\n%s\", msg);",
|
adamc@871
|
225 newline],
|
adamc@871
|
226 string "}",
|
adamc@871
|
227 newline,
|
adamc@871
|
228 newline,
|
adamc@871
|
229 string "if (strcmp(PQgetvalue(res, 0, 0), \"",
|
adamc@871
|
230 string (Int.toString (length xts)),
|
adamc@871
|
231 string "\")) {",
|
adamc@871
|
232 newline,
|
adamc@871
|
233 box [string "PQclear(res);",
|
adamc@871
|
234 newline,
|
adamc@871
|
235 string "PQfinish(conn);",
|
adamc@871
|
236 newline,
|
adamc@871
|
237 string "uw_error(ctx, FATAL, \"Table '",
|
adamc@871
|
238 string s,
|
adamc@871
|
239 string "' has extra columns.\");",
|
adamc@871
|
240 newline],
|
adamc@871
|
241 string "}",
|
adamc@871
|
242 newline,
|
adamc@871
|
243 newline,
|
adamc@871
|
244 string "PQclear(res);",
|
adamc@871
|
245 newline]
|
adamc@871
|
246 end
|
adamc@871
|
247
|
adamc@872
|
248 fun init {dbstring, prepared = ss, tables, views, sequences} =
|
adamc@866
|
249 box [if #persistent (currentProtocol ()) then
|
adamc@876
|
250 box [string "void uw_client_init(void) { }",
|
adamc@874
|
251 newline,
|
adamc@874
|
252 newline,
|
adamc@874
|
253
|
adamc@874
|
254 string "static void uw_db_validate(uw_context ctx) {",
|
adamc@870
|
255 newline,
|
adamc@870
|
256 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@870
|
257 newline,
|
adamc@870
|
258 string "PGresult *res;",
|
adamc@870
|
259 newline,
|
adamc@870
|
260 newline,
|
adamc@872
|
261 p_list_sep newline (checkRel ("tables", true)) tables,
|
adamc@872
|
262 p_list_sep newline (checkRel ("views", false)) views,
|
adamc@870
|
263
|
adamc@870
|
264 p_list_sep newline
|
adamc@870
|
265 (fn s =>
|
adamc@870
|
266 let
|
adamc@870
|
267 val sl = CharVector.map Char.toLower s
|
adamc@870
|
268
|
adamc@870
|
269 val q = "SELECT COUNT(*) FROM pg_class WHERE relname = '"
|
adamc@870
|
270 ^ sl ^ "' AND relkind = 'S'"
|
adamc@870
|
271 in
|
adamc@870
|
272 box [string "res = PQexec(conn, \"",
|
adamc@870
|
273 string q,
|
adamc@870
|
274 string "\");",
|
adamc@870
|
275 newline,
|
adamc@870
|
276 newline,
|
adamc@870
|
277 string "if (res == NULL) {",
|
adamc@870
|
278 newline,
|
adamc@870
|
279 box [string "PQfinish(conn);",
|
adamc@870
|
280 newline,
|
adamc@870
|
281 string "uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@870
|
282 newline],
|
adamc@870
|
283 string "}",
|
adamc@870
|
284 newline,
|
adamc@870
|
285 newline,
|
adamc@870
|
286 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@870
|
287 newline,
|
adamc@870
|
288 box [string "char msg[1024];",
|
adamc@870
|
289 newline,
|
adamc@870
|
290 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@870
|
291 newline,
|
adamc@870
|
292 string "msg[1023] = 0;",
|
adamc@870
|
293 newline,
|
adamc@870
|
294 string "PQclear(res);",
|
adamc@870
|
295 newline,
|
adamc@870
|
296 string "PQfinish(conn);",
|
adamc@870
|
297 newline,
|
adamc@870
|
298 string "uw_error(ctx, FATAL, \"Query failed:\\n",
|
adamc@870
|
299 string q,
|
adamc@870
|
300 string "\\n%s\", msg);",
|
adamc@870
|
301 newline],
|
adamc@870
|
302 string "}",
|
adamc@870
|
303 newline,
|
adamc@870
|
304 newline,
|
adamc@870
|
305 string "if (strcmp(PQgetvalue(res, 0, 0), \"1\")) {",
|
adamc@870
|
306 newline,
|
adamc@870
|
307 box [string "PQclear(res);",
|
adamc@870
|
308 newline,
|
adamc@870
|
309 string "PQfinish(conn);",
|
adamc@870
|
310 newline,
|
adamc@870
|
311 string "uw_error(ctx, FATAL, \"Sequence '",
|
adamc@870
|
312 string s,
|
adamc@870
|
313 string "' does not exist.\");",
|
adamc@870
|
314 newline],
|
adamc@870
|
315 string "}",
|
adamc@870
|
316 newline,
|
adamc@870
|
317 newline,
|
adamc@870
|
318 string "PQclear(res);",
|
adamc@870
|
319 newline]
|
adamc@870
|
320 end) sequences,
|
adamc@870
|
321
|
adamc@870
|
322 string "}",
|
adamc@870
|
323
|
adamc@870
|
324 string "static void uw_db_prepare(uw_context ctx) {",
|
adamc@866
|
325 newline,
|
adamc@866
|
326 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@866
|
327 newline,
|
adamc@866
|
328 string "PGresult *res;",
|
adamc@866
|
329 newline,
|
adamc@866
|
330 newline,
|
adamc@866
|
331
|
adamc@866
|
332 p_list_sepi newline (fn i => fn (s, n) =>
|
adamc@866
|
333 box [string "res = PQprepare(conn, \"uw",
|
adamc@866
|
334 string (Int.toString i),
|
adamc@866
|
335 string "\", \"",
|
adamc@866
|
336 string (String.toString s),
|
adamc@866
|
337 string "\", ",
|
adamc@866
|
338 string (Int.toString n),
|
adamc@866
|
339 string ", NULL);",
|
adamc@866
|
340 newline,
|
adamc@866
|
341 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@866
|
342 newline,
|
adamc@866
|
343 box [string "char msg[1024];",
|
adamc@866
|
344 newline,
|
adamc@866
|
345 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@866
|
346 newline,
|
adamc@866
|
347 string "msg[1023] = 0;",
|
adamc@866
|
348 newline,
|
adamc@866
|
349 string "PQclear(res);",
|
adamc@866
|
350 newline,
|
adamc@866
|
351 string "PQfinish(conn);",
|
adamc@866
|
352 newline,
|
adamc@866
|
353 string "uw_error(ctx, FATAL, \"Unable to create prepared statement:\\n",
|
adamc@866
|
354 string (String.toString s),
|
adamc@866
|
355 string "\\n%s\", msg);",
|
adamc@866
|
356 newline],
|
adamc@866
|
357 string "}",
|
adamc@866
|
358 newline,
|
adamc@866
|
359 string "PQclear(res);",
|
adamc@866
|
360 newline])
|
adamc@866
|
361 ss,
|
adamc@866
|
362
|
adamc@866
|
363 string "}",
|
adamc@866
|
364 newline,
|
adamc@866
|
365 newline,
|
adamc@866
|
366
|
adamc@866
|
367 string "void uw_db_close(uw_context ctx) {",
|
adamc@866
|
368 newline,
|
adamc@866
|
369 string "PQfinish(uw_get_db(ctx));",
|
adamc@866
|
370 newline,
|
adamc@866
|
371 string "}",
|
adamc@866
|
372 newline,
|
adamc@866
|
373 newline,
|
adamc@866
|
374
|
adamc@866
|
375 string "int uw_db_begin(uw_context ctx) {",
|
adamc@866
|
376 newline,
|
adamc@866
|
377 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@866
|
378 newline,
|
adamc@866
|
379 string "PGresult *res = PQexec(conn, \"BEGIN ISOLATION LEVEL SERIALIZABLE\");",
|
adamc@866
|
380 newline,
|
adamc@866
|
381 newline,
|
adamc@866
|
382 string "if (res == NULL) return 1;",
|
adamc@866
|
383 newline,
|
adamc@866
|
384 newline,
|
adamc@866
|
385 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@866
|
386 box [string "PQclear(res);",
|
adamc@866
|
387 newline,
|
adamc@866
|
388 string "return 1;",
|
adamc@866
|
389 newline],
|
adamc@866
|
390 string "}",
|
adamc@866
|
391 newline,
|
adamc@866
|
392 string "return 0;",
|
adamc@866
|
393 newline,
|
adamc@866
|
394 string "}",
|
adamc@866
|
395 newline,
|
adamc@866
|
396 newline,
|
adamc@866
|
397
|
adamc@866
|
398 string "int uw_db_commit(uw_context ctx) {",
|
adamc@866
|
399 newline,
|
adamc@866
|
400 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@866
|
401 newline,
|
adamc@866
|
402 string "PGresult *res = PQexec(conn, \"COMMIT\");",
|
adamc@866
|
403 newline,
|
adamc@866
|
404 newline,
|
adamc@866
|
405 string "if (res == NULL) return 1;",
|
adamc@866
|
406 newline,
|
adamc@866
|
407 newline,
|
adamc@866
|
408 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@866
|
409 box [string "PQclear(res);",
|
adamc@866
|
410 newline,
|
adamc@866
|
411 string "return 1;",
|
adamc@866
|
412 newline],
|
adamc@866
|
413 string "}",
|
adamc@866
|
414 newline,
|
adamc@866
|
415 string "return 0;",
|
adamc@866
|
416 newline,
|
adamc@866
|
417 string "}",
|
adamc@866
|
418 newline,
|
adamc@866
|
419 newline,
|
adamc@866
|
420
|
adamc@866
|
421 string "int uw_db_rollback(uw_context ctx) {",
|
adamc@866
|
422 newline,
|
adamc@866
|
423 string "PGconn *conn = uw_get_db(ctx);",
|
adamc@866
|
424 newline,
|
adamc@866
|
425 string "PGresult *res = PQexec(conn, \"ROLLBACK\");",
|
adamc@866
|
426 newline,
|
adamc@866
|
427 newline,
|
adamc@866
|
428 string "if (res == NULL) return 1;",
|
adamc@866
|
429 newline,
|
adamc@866
|
430 newline,
|
adamc@866
|
431 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@866
|
432 box [string "PQclear(res);",
|
adamc@866
|
433 newline,
|
adamc@866
|
434 string "return 1;",
|
adamc@866
|
435 newline],
|
adamc@866
|
436 string "}",
|
adamc@866
|
437 newline,
|
adamc@866
|
438 string "return 0;",
|
adamc@866
|
439 newline,
|
adamc@866
|
440 string "}",
|
adamc@866
|
441 newline,
|
adamc@866
|
442 newline]
|
adamc@866
|
443 else
|
adamc@870
|
444 box [string "static void uw_db_validate(uw_context ctx) { }",
|
adamc@870
|
445 newline,
|
adamc@870
|
446 string "static void uw_db_prepare(uw_context ctx) { }"],
|
adamc@870
|
447
|
adamc@866
|
448 newline,
|
adamc@866
|
449 newline,
|
adamc@866
|
450
|
adamc@866
|
451 string "void uw_db_init(uw_context ctx) {",
|
adamc@866
|
452 newline,
|
adamc@866
|
453 string "PGconn *conn = PQconnectdb(\"",
|
adamc@866
|
454 string (String.toString dbstring),
|
adamc@866
|
455 string "\");",
|
adamc@866
|
456 newline,
|
adamc@866
|
457 string "if (conn == NULL) uw_error(ctx, FATAL, ",
|
adamc@866
|
458 string "\"libpq can't allocate a connection.\");",
|
adamc@866
|
459 newline,
|
adamc@866
|
460 string "if (PQstatus(conn) != CONNECTION_OK) {",
|
adamc@866
|
461 newline,
|
adamc@866
|
462 box [string "char msg[1024];",
|
adamc@866
|
463 newline,
|
adamc@866
|
464 string "strncpy(msg, PQerrorMessage(conn), 1024);",
|
adamc@866
|
465 newline,
|
adamc@866
|
466 string "msg[1023] = 0;",
|
adamc@866
|
467 newline,
|
adamc@866
|
468 string "PQfinish(conn);",
|
adamc@866
|
469 newline,
|
adamc@866
|
470 string "uw_error(ctx, BOUNDED_RETRY, ",
|
adamc@866
|
471 string "\"Connection to Postgres server failed: %s\", msg);"],
|
adamc@866
|
472 newline,
|
adamc@866
|
473 string "}",
|
adamc@866
|
474 newline,
|
adamc@866
|
475 string "uw_set_db(ctx, conn);",
|
adamc@866
|
476 newline,
|
adamc@866
|
477 string "uw_db_validate(ctx);",
|
adamc@866
|
478 newline,
|
adamc@866
|
479 string "uw_db_prepare(ctx);",
|
adamc@866
|
480 newline,
|
adamc@866
|
481 string "}"]
|
adamc@866
|
482
|
adamc@867
|
483 fun p_getcol {wontLeakStrings, col = i, typ = t} =
|
adamc@867
|
484 let
|
adamc@867
|
485 fun p_unsql t e eLen =
|
adamc@867
|
486 case t of
|
adamc@867
|
487 Int => box [string "uw_Basis_stringToInt_error(ctx, ", e, string ")"]
|
adamc@867
|
488 | Float => box [string "uw_Basis_stringToFloat_error(ctx, ", e, string ")"]
|
adamc@867
|
489 | String =>
|
adamc@867
|
490 if wontLeakStrings then
|
adamc@867
|
491 e
|
adamc@867
|
492 else
|
adamc@867
|
493 box [string "uw_strdup(ctx, ", e, string ")"]
|
adamc@867
|
494 | Bool => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"]
|
adamc@867
|
495 | Time => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"]
|
adamc@867
|
496 | Blob => box [string "uw_Basis_stringToBlob_error(ctx, ",
|
adamc@867
|
497 e,
|
adamc@867
|
498 string ", ",
|
adamc@867
|
499 eLen,
|
adamc@867
|
500 string ")"]
|
adamc@867
|
501 | Channel => box [string "uw_Basis_stringToChannel_error(ctx, ", e, string ")"]
|
adamc@867
|
502 | Client => box [string "uw_Basis_stringToClient_error(ctx, ", e, string ")"]
|
adamc@867
|
503
|
adamc@867
|
504 | Nullable _ => raise Fail "Postgres: Recursive Nullable"
|
adamc@867
|
505
|
adamc@867
|
506 fun getter t =
|
adamc@867
|
507 case t of
|
adamc@867
|
508 Nullable t =>
|
adamc@867
|
509 box [string "(PQgetisnull(res, i, ",
|
adamc@867
|
510 string (Int.toString i),
|
adamc@867
|
511 string ") ? NULL : ",
|
adamc@867
|
512 case t of
|
adamc@867
|
513 String => getter t
|
adamc@867
|
514 | _ => box [string "({",
|
adamc@867
|
515 newline,
|
adamc@874
|
516 string (p_sql_ctype t),
|
adamc@867
|
517 space,
|
adamc@867
|
518 string "*tmp = uw_malloc(ctx, sizeof(",
|
adamc@874
|
519 string (p_sql_ctype t),
|
adamc@867
|
520 string "));",
|
adamc@867
|
521 newline,
|
adamc@867
|
522 string "*tmp = ",
|
adamc@867
|
523 getter t,
|
adamc@867
|
524 string ";",
|
adamc@867
|
525 newline,
|
adamc@867
|
526 string "tmp;",
|
adamc@867
|
527 newline,
|
adamc@867
|
528 string "})"],
|
adamc@867
|
529 string ")"]
|
adamc@867
|
530 | _ =>
|
adamc@867
|
531 box [string "(PQgetisnull(res, i, ",
|
adamc@867
|
532 string (Int.toString i),
|
adamc@867
|
533 string ") ? ",
|
adamc@867
|
534 box [string "({",
|
adamc@874
|
535 string (p_sql_ctype t),
|
adamc@867
|
536 space,
|
adamc@867
|
537 string "tmp;",
|
adamc@867
|
538 newline,
|
adamc@867
|
539 string "uw_error(ctx, FATAL, \"Unexpectedly NULL field #",
|
adamc@867
|
540 string (Int.toString i),
|
adamc@867
|
541 string "\");",
|
adamc@867
|
542 newline,
|
adamc@867
|
543 string "tmp;",
|
adamc@867
|
544 newline,
|
adamc@867
|
545 string "})"],
|
adamc@867
|
546 string " : ",
|
adamc@867
|
547 p_unsql t
|
adamc@867
|
548 (box [string "PQgetvalue(res, i, ",
|
adamc@867
|
549 string (Int.toString i),
|
adamc@867
|
550 string ")"])
|
adamc@867
|
551 (box [string "PQgetlength(res, i, ",
|
adamc@867
|
552 string (Int.toString i),
|
adamc@867
|
553 string ")"]),
|
adamc@867
|
554 string ")"]
|
adamc@867
|
555 in
|
adamc@867
|
556 getter t
|
adamc@867
|
557 end
|
adamc@867
|
558
|
adamc@873
|
559 fun queryCommon {loc, query, cols, doCols} =
|
adamc@867
|
560 box [string "int n, i;",
|
adamc@867
|
561 newline,
|
adamc@867
|
562 newline,
|
adamc@867
|
563
|
adamc@867
|
564 string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating query result.\");",
|
adamc@867
|
565 newline,
|
adamc@867
|
566 newline,
|
adamc@867
|
567
|
adamc@867
|
568 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@867
|
569 newline,
|
adamc@867
|
570 box [string "PQclear(res);",
|
adamc@867
|
571 newline,
|
adamc@867
|
572 string "uw_error(ctx, FATAL, \"",
|
adamc@867
|
573 string (ErrorMsg.spanToString loc),
|
adamc@867
|
574 string ": Query failed:\\n%s\\n%s\", ",
|
adamc@867
|
575 query,
|
adamc@867
|
576 string ", PQerrorMessage(conn));",
|
adamc@867
|
577 newline],
|
adamc@867
|
578 string "}",
|
adamc@867
|
579 newline,
|
adamc@867
|
580 newline,
|
adamc@867
|
581
|
adamc@867
|
582 string "if (PQnfields(res) != ",
|
adamc@873
|
583 string (Int.toString (length cols)),
|
adamc@867
|
584 string ") {",
|
adamc@867
|
585 newline,
|
adamc@867
|
586 box [string "int nf = PQnfields(res);",
|
adamc@867
|
587 newline,
|
adamc@867
|
588 string "PQclear(res);",
|
adamc@867
|
589 newline,
|
adamc@867
|
590 string "uw_error(ctx, FATAL, \"",
|
adamc@867
|
591 string (ErrorMsg.spanToString loc),
|
adamc@867
|
592 string ": Query returned %d columns instead of ",
|
adamc@873
|
593 string (Int.toString (length cols)),
|
adamc@867
|
594 string ":\\n%s\\n%s\", nf, ",
|
adamc@867
|
595 query,
|
adamc@867
|
596 string ", PQerrorMessage(conn));",
|
adamc@867
|
597 newline],
|
adamc@867
|
598 string "}",
|
adamc@867
|
599 newline,
|
adamc@867
|
600 newline,
|
adamc@867
|
601
|
adamc@867
|
602 string "uw_end_region(ctx);",
|
adamc@867
|
603 newline,
|
adamc@867
|
604 string "uw_push_cleanup(ctx, (void (*)(void *))PQclear, res);",
|
adamc@867
|
605 newline,
|
adamc@867
|
606 string "n = PQntuples(res);",
|
adamc@867
|
607 newline,
|
adamc@867
|
608 string "for (i = 0; i < n; ++i) {",
|
adamc@867
|
609 newline,
|
adamc@867
|
610 doCols p_getcol,
|
adamc@867
|
611 string "}",
|
adamc@867
|
612 newline,
|
adamc@867
|
613 newline,
|
adamc@867
|
614 string "uw_pop_cleanup(ctx);",
|
adamc@867
|
615 newline]
|
adamc@867
|
616
|
adamc@873
|
617 fun query {loc, cols, doCols} =
|
adamc@867
|
618 box [string "PGconn *conn = uw_get_db(ctx);",
|
adamc@867
|
619 newline,
|
adamc@867
|
620 string "PGresult *res = PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);",
|
adamc@867
|
621 newline,
|
adamc@867
|
622 newline,
|
adamc@873
|
623 queryCommon {loc = loc, cols = cols, doCols = doCols, query = string "query"}]
|
adamc@867
|
624
|
adamc@867
|
625 fun p_ensql t e =
|
adamc@867
|
626 case t of
|
adamc@867
|
627 Int => box [string "uw_Basis_attrifyInt(ctx, ", e, string ")"]
|
adamc@867
|
628 | Float => box [string "uw_Basis_attrifyFloat(ctx, ", e, string ")"]
|
adamc@867
|
629 | String => e
|
adamc@867
|
630 | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"]
|
adamc@867
|
631 | Time => box [string "uw_Basis_attrifyTime(ctx, ", e, string ")"]
|
adamc@867
|
632 | Blob => box [e, string ".data"]
|
adamc@867
|
633 | Channel => box [string "uw_Basis_attrifyChannel(ctx, ", e, string ")"]
|
adamc@867
|
634 | Client => box [string "uw_Basis_attrifyClient(ctx, ", e, string ")"]
|
adamc@867
|
635 | Nullable String => e
|
adamc@867
|
636 | Nullable t => box [string "(",
|
adamc@867
|
637 e,
|
adamc@867
|
638 string " == NULL ? NULL : ",
|
adamc@867
|
639 p_ensql t (box [string "(*", e, string ")"]),
|
adamc@867
|
640 string ")"]
|
adamc@867
|
641
|
adamc@873
|
642 fun queryPrepared {loc, id, query, inputs, cols, doCols} =
|
adamc@867
|
643 box [string "PGconn *conn = uw_get_db(ctx);",
|
adamc@867
|
644 newline,
|
adamc@867
|
645 string "const int paramFormats[] = { ",
|
adamc@867
|
646 p_list_sep (box [string ",", space])
|
adamc@867
|
647 (fn t => if isBlob t then string "1" else string "0") inputs,
|
adamc@867
|
648 string " };",
|
adamc@867
|
649 newline,
|
adamc@867
|
650 string "const int paramLengths[] = { ",
|
adamc@867
|
651 p_list_sepi (box [string ",", space])
|
adamc@867
|
652 (fn i => fn Blob => string ("arg" ^ Int.toString (i + 1) ^ ".size")
|
adamc@867
|
653 | Nullable Blob => string ("arg" ^ Int.toString (i + 1)
|
adamc@867
|
654 ^ "?arg" ^ Int.toString (i + 1) ^ "->size:0")
|
adamc@867
|
655 | _ => string "0") inputs,
|
adamc@867
|
656 string " };",
|
adamc@867
|
657 newline,
|
adamc@867
|
658 string "const char *paramValues[] = { ",
|
adamc@867
|
659 p_list_sepi (box [string ",", space])
|
adamc@867
|
660 (fn i => fn t => p_ensql t (box [string "arg",
|
adamc@867
|
661 string (Int.toString (i + 1))]))
|
adamc@867
|
662 inputs,
|
adamc@867
|
663 string " };",
|
adamc@867
|
664 newline,
|
adamc@867
|
665 newline,
|
adamc@867
|
666 string "PGresult *res = ",
|
adamc@867
|
667 if #persistent (Settings.currentProtocol ()) then
|
adamc@867
|
668 box [string "PQexecPrepared(conn, \"uw",
|
adamc@867
|
669 string (Int.toString id),
|
adamc@867
|
670 string "\", ",
|
adamc@867
|
671 string (Int.toString (length inputs)),
|
adamc@867
|
672 string ", paramValues, paramLengths, paramFormats, 0);"]
|
adamc@867
|
673 else
|
adamc@867
|
674 box [string "PQexecParams(conn, \"",
|
adamc@867
|
675 string (String.toString query),
|
adamc@867
|
676 string "\", ",
|
adamc@867
|
677 string (Int.toString (length inputs)),
|
adamc@867
|
678 string ", NULL, paramValues, paramLengths, paramFormats, 0);"],
|
adamc@867
|
679 newline,
|
adamc@867
|
680 newline,
|
adamc@873
|
681 queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"",
|
adamc@873
|
682 string (String.toString query),
|
adamc@873
|
683 string "\""]}]
|
adamc@867
|
684
|
adamc@868
|
685 fun dmlCommon {loc, dml} =
|
adamc@868
|
686 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
|
adamc@868
|
687 newline,
|
adamc@868
|
688 newline,
|
adamc@868
|
689
|
adamc@868
|
690 string "if (PQresultStatus(res) != PGRES_COMMAND_OK) {",
|
adamc@868
|
691 newline,
|
adamc@868
|
692 box [string "if (!strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), \"40001\")) {",
|
adamc@868
|
693 box [newline,
|
adamc@868
|
694 string "PQclear(res);",
|
adamc@868
|
695 newline,
|
adamc@868
|
696 string "uw_error(ctx, UNLIMITED_RETRY, \"Serialization failure\");",
|
adamc@868
|
697 newline],
|
adamc@868
|
698 string "}",
|
adamc@868
|
699 newline,
|
adamc@868
|
700 string "PQclear(res);",
|
adamc@868
|
701 newline,
|
adamc@868
|
702 string "uw_error(ctx, FATAL, \"",
|
adamc@868
|
703 string (ErrorMsg.spanToString loc),
|
adamc@868
|
704 string ": DML failed:\\n%s\\n%s\", ",
|
adamc@868
|
705 dml,
|
adamc@868
|
706 string ", PQerrorMessage(conn));",
|
adamc@868
|
707 newline],
|
adamc@868
|
708 string "}",
|
adamc@868
|
709 newline,
|
adamc@868
|
710 newline,
|
adamc@868
|
711
|
adamc@868
|
712 string "PQclear(res);",
|
adamc@868
|
713 newline]
|
adamc@868
|
714
|
adamc@868
|
715 fun dml loc =
|
adamc@868
|
716 box [string "PGconn *conn = uw_get_db(ctx);",
|
adamc@868
|
717 newline,
|
adamc@868
|
718 string "PGresult *res = PQexecParams(conn, dml, 0, NULL, NULL, NULL, NULL, 0);",
|
adamc@868
|
719 newline,
|
adamc@868
|
720 newline,
|
adamc@868
|
721 dmlCommon {loc = loc, dml = string "dml"}]
|
adamc@868
|
722
|
adamc@868
|
723 fun dmlPrepared {loc, id, dml, inputs} =
|
adamc@868
|
724 box [string "PGconn *conn = uw_get_db(ctx);",
|
adamc@868
|
725 newline,
|
adamc@868
|
726 string "const int paramFormats[] = { ",
|
adamc@868
|
727 p_list_sep (box [string ",", space])
|
adamc@868
|
728 (fn t => if isBlob t then string "1" else string "0") inputs,
|
adamc@868
|
729 string " };",
|
adamc@868
|
730 newline,
|
adamc@868
|
731 string "const int paramLengths[] = { ",
|
adamc@868
|
732 p_list_sepi (box [string ",", space])
|
adamc@868
|
733 (fn i => fn Blob => string ("arg" ^ Int.toString (i + 1) ^ ".size")
|
adamc@868
|
734 | Nullable Blob => string ("arg" ^ Int.toString (i + 1)
|
adamc@868
|
735 ^ "?arg" ^ Int.toString (i + 1) ^ "->size:0")
|
adamc@868
|
736 | _ => string "0") inputs,
|
adamc@868
|
737 string " };",
|
adamc@868
|
738 newline,
|
adamc@868
|
739 string "const char *paramValues[] = { ",
|
adamc@868
|
740 p_list_sepi (box [string ",", space])
|
adamc@868
|
741 (fn i => fn t => p_ensql t (box [string "arg",
|
adamc@868
|
742 string (Int.toString (i + 1))]))
|
adamc@868
|
743 inputs,
|
adamc@868
|
744 string " };",
|
adamc@868
|
745 newline,
|
adamc@868
|
746 newline,
|
adamc@868
|
747 string "PGresult *res = ",
|
adamc@868
|
748 if #persistent (Settings.currentProtocol ()) then
|
adamc@868
|
749 box [string "PQexecPrepared(conn, \"uw",
|
adamc@868
|
750 string (Int.toString id),
|
adamc@868
|
751 string "\", ",
|
adamc@868
|
752 string (Int.toString (length inputs)),
|
adamc@868
|
753 string ", paramValues, paramLengths, paramFormats, 0);"]
|
adamc@868
|
754 else
|
adamc@868
|
755 box [string "PQexecParams(conn, \"",
|
adamc@868
|
756 string (String.toString dml),
|
adamc@868
|
757 string "\", ",
|
adamc@868
|
758 string (Int.toString (length inputs)),
|
adamc@868
|
759 string ", NULL, paramValues, paramLengths, paramFormats, 0);"],
|
adamc@868
|
760 newline,
|
adamc@868
|
761 newline,
|
adamc@868
|
762 dmlCommon {loc = loc, dml = box [string "\"",
|
adamc@868
|
763 string (String.toString dml),
|
adamc@868
|
764 string "\""]}]
|
adamc@868
|
765
|
adamc@869
|
766 fun nextvalCommon {loc, query} =
|
adamc@869
|
767 box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating nextval result.\");",
|
adamc@869
|
768 newline,
|
adamc@869
|
769 newline,
|
adamc@869
|
770
|
adamc@869
|
771 string "if (PQresultStatus(res) != PGRES_TUPLES_OK) {",
|
adamc@869
|
772 newline,
|
adamc@869
|
773 box [string "PQclear(res);",
|
adamc@869
|
774 newline,
|
adamc@869
|
775 string "uw_error(ctx, FATAL, \"",
|
adamc@869
|
776 string (ErrorMsg.spanToString loc),
|
adamc@869
|
777 string ": Query failed:\\n%s\\n%s\", ",
|
adamc@869
|
778 query,
|
adamc@869
|
779 string ", PQerrorMessage(conn));",
|
adamc@869
|
780 newline],
|
adamc@869
|
781 string "}",
|
adamc@869
|
782 newline,
|
adamc@869
|
783 newline,
|
adamc@869
|
784
|
adamc@869
|
785 string "uw_end_region(ctx);",
|
adamc@869
|
786 newline,
|
adamc@869
|
787 string "n = PQntuples(res);",
|
adamc@869
|
788 newline,
|
adamc@869
|
789 string "if (n != 1) {",
|
adamc@869
|
790 newline,
|
adamc@869
|
791 box [string "PQclear(res);",
|
adamc@869
|
792 newline,
|
adamc@869
|
793 string "uw_error(ctx, FATAL, \"",
|
adamc@869
|
794 string (ErrorMsg.spanToString loc),
|
adamc@869
|
795 string ": Wrong number of result rows:\\n%s\\n%s\", ",
|
adamc@869
|
796 query,
|
adamc@869
|
797 string ", PQerrorMessage(conn));",
|
adamc@869
|
798 newline],
|
adamc@869
|
799 string "}",
|
adamc@869
|
800 newline,
|
adamc@869
|
801 newline,
|
adamc@869
|
802
|
adamc@869
|
803 string "n = uw_Basis_stringToInt_error(ctx, PQgetvalue(res, 0, 0));",
|
adamc@869
|
804 newline,
|
adamc@869
|
805 string "PQclear(res);",
|
adamc@869
|
806 newline]
|
adamc@869
|
807
|
adamc@869
|
808 fun nextval loc =
|
adamc@869
|
809 box [string "PGconn *conn = uw_get_db(ctx);",
|
adamc@869
|
810 newline,
|
adamc@869
|
811 string "PGresult *res = PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);",
|
adamc@869
|
812 newline,
|
adamc@869
|
813 newline,
|
adamc@869
|
814 nextvalCommon {loc = loc, query = string "query"}]
|
adamc@869
|
815
|
adamc@869
|
816 fun nextvalPrepared {loc, id, query} =
|
adamc@869
|
817 box [string "PGconn *conn = uw_get_db(ctx);",
|
adamc@869
|
818 newline,
|
adamc@869
|
819 newline,
|
adamc@869
|
820 string "PGresult *res = ",
|
adamc@869
|
821 if #persistent (Settings.currentProtocol ()) then
|
adamc@869
|
822 box [string "PQexecPrepared(conn, \"uw",
|
adamc@869
|
823 string (Int.toString id),
|
adamc@869
|
824 string "\", 0, NULL, NULL, NULL, 0);"]
|
adamc@869
|
825 else
|
adamc@869
|
826 box [string "PQexecParams(conn, \"",
|
adamc@869
|
827 string (String.toString query),
|
adamc@869
|
828 string "\", 0, NULL, NULL, NULL, NULL, 0);"],
|
adamc@869
|
829 newline,
|
adamc@869
|
830 newline,
|
adamc@869
|
831 nextvalCommon {loc = loc, query = box [string "\"",
|
adamc@869
|
832 string (String.toString query),
|
adamc@869
|
833 string "\""]}]
|
adamc@869
|
834
|
adamc@874
|
835 fun sqlifyString s = "E'" ^ String.translate (fn #"'" => "\\'"
|
adamc@874
|
836 | #"\\" => "\\\\"
|
adamc@874
|
837 | ch =>
|
adamc@874
|
838 if Char.isPrint ch then
|
adamc@874
|
839 str ch
|
adamc@874
|
840 else
|
adamc@874
|
841 "\\" ^ StringCvt.padLeft #"0" 3
|
adamc@874
|
842 (Int.fmt StringCvt.OCT (ord ch)))
|
adamc@874
|
843 (String.toString s) ^ "'::text"
|
adamc@874
|
844
|
adamc@874
|
845 fun p_cast (s, t) = s ^ "::" ^ p_sql_type t
|
adamc@874
|
846
|
adamc@874
|
847 fun p_blank (n, t) = p_cast ("$" ^ Int.toString n, t)
|
adamc@874
|
848
|
adamc@866
|
849 val () = addDbms {name = "postgres",
|
adamc@866
|
850 header = "postgresql/libpq-fe.h",
|
adamc@866
|
851 link = "-lpq",
|
adamc@873
|
852 p_sql_type = p_sql_type,
|
adamc@867
|
853 init = init,
|
adamc@867
|
854 query = query,
|
adamc@868
|
855 queryPrepared = queryPrepared,
|
adamc@868
|
856 dml = dml,
|
adamc@869
|
857 dmlPrepared = dmlPrepared,
|
adamc@869
|
858 nextval = nextval,
|
adamc@874
|
859 nextvalPrepared = nextvalPrepared,
|
adamc@874
|
860 sqlifyString = sqlifyString,
|
adamc@874
|
861 p_cast = p_cast,
|
adamc@874
|
862 p_blank = p_blank,
|
adamc@877
|
863 supportsDeleteAs = true,
|
adamc@877
|
864 createSequence = fn s => "CREATE SEQUENCE " ^ s,
|
adamc@877
|
865 textKeysNeedLengths = false}
|
adamc@874
|
866
|
adamc@866
|
867 val () = setDbms "postgres"
|
adamc@866
|
868
|
adamc@866
|
869 end
|