annotate src/mysql.sml @ 878:a8952047e1d3

Sequence code compiles in MySQL
author Adam Chlipala <adamc@hcoop.net>
date Thu, 16 Jul 2009 16:29:13 -0400
parents dae141d911d9
children b2a175a0f2ef
rev   line source
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 MySQL :> MYSQL = struct
adamc@866 29
adamc@866 30 open Settings
adamc@866 31 open Print.PD
adamc@866 32 open Print
adamc@866 33
adamc@873 34 fun p_sql_type t =
adamc@873 35 case t of
adamc@873 36 Int => "bigint"
adamc@873 37 | Float => "double"
adamc@873 38 | String => "longtext"
adamc@873 39 | Bool => "bool"
adamc@873 40 | Time => "timestamp"
adamc@873 41 | Blob => "longblob"
adamc@873 42 | Channel => "bigint"
adamc@873 43 | Client => "int"
adamc@873 44 | Nullable t => p_sql_type t
adamc@873 45
adamc@873 46 fun p_buffer_type t =
adamc@873 47 case t of
adamc@873 48 Int => "MYSQL_TYPE_LONGLONG"
adamc@873 49 | Float => "MYSQL_TYPE_DOUBLE"
adamc@873 50 | String => "MYSQL_TYPE_STRING"
adamc@873 51 | Bool => "MYSQL_TYPE_LONG"
adamc@873 52 | Time => "MYSQL_TYPE_TIME"
adamc@873 53 | Blob => "MYSQL_TYPE_BLOB"
adamc@873 54 | Channel => "MYSQL_TYPE_LONGLONG"
adamc@873 55 | Client => "MYSQL_TYPE_LONG"
adamc@873 56 | Nullable t => p_buffer_type t
adamc@873 57
adamc@874 58 fun p_sql_type_base t =
adamc@874 59 case t of
adamc@874 60 Int => "bigint"
adamc@874 61 | Float => "double"
adamc@874 62 | String => "longtext"
adamc@874 63 | Bool => "tinyint"
adamc@874 64 | Time => "timestamp"
adamc@874 65 | Blob => "longblob"
adamc@874 66 | Channel => "bigint"
adamc@874 67 | Client => "int"
adamc@874 68 | Nullable t => p_sql_type_base t
adamc@874 69
adamc@874 70 val ident = String.translate (fn #"'" => "PRIME"
adamc@874 71 | ch => str ch)
adamc@874 72
adamc@874 73 fun checkRel (table, checkNullable) (s, xts) =
adamc@874 74 let
adamc@874 75 val sl = CharVector.map Char.toLower s
adamc@874 76
adamc@874 77 val q = "SELECT COUNT(*) FROM information_schema." ^ table ^ " WHERE table_name = '"
adamc@874 78 ^ sl ^ "'"
adamc@874 79
adamc@874 80 val q' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '",
adamc@874 81 sl,
adamc@874 82 "' AND (",
adamc@874 83 String.concatWith " OR "
adamc@874 84 (map (fn (x, t) =>
adamc@874 85 String.concat ["(column_name = 'uw_",
adamc@874 86 CharVector.map
adamc@874 87 Char.toLower (ident x),
adamc@874 88 "' AND data_type = '",
adamc@874 89 p_sql_type_base t,
adamc@874 90 "'",
adamc@874 91 if checkNullable then
adamc@874 92 (" AND is_nullable = '"
adamc@874 93 ^ (if isNotNull t then
adamc@874 94 "NO"
adamc@874 95 else
adamc@874 96 "YES")
adamc@874 97 ^ "'")
adamc@874 98 else
adamc@874 99 "",
adamc@874 100 ")"]) xts),
adamc@874 101 ")"]
adamc@874 102
adamc@874 103 val q'' = String.concat ["SELECT COUNT(*) FROM information_schema.columns WHERE table_name = '",
adamc@874 104 sl,
adamc@874 105 "' AND column_name LIKE 'uw_%'"]
adamc@874 106 in
adamc@874 107 box [string "if (mysql_query(conn->conn, \"",
adamc@874 108 string q,
adamc@874 109 string "\")) {",
adamc@874 110 newline,
adamc@874 111 box [string "mysql_close(conn->conn);",
adamc@874 112 newline,
adamc@874 113 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@874 114 string q,
adamc@874 115 string "\");",
adamc@874 116 newline],
adamc@874 117 string "}",
adamc@874 118 newline,
adamc@874 119 newline,
adamc@874 120
adamc@874 121 string "if ((res = mysql_store_result(conn->conn)) == NULL) {",
adamc@874 122 newline,
adamc@874 123 box [string "mysql_free_result(res);",
adamc@874 124 newline,
adamc@874 125 string "mysql_close(conn->conn);",
adamc@874 126 newline,
adamc@874 127 string "uw_error(ctx, FATAL, \"Result store failed:\\n",
adamc@874 128 string q,
adamc@874 129 string "\");",
adamc@874 130 newline],
adamc@874 131 string "}",
adamc@874 132 newline,
adamc@874 133 newline,
adamc@874 134
adamc@874 135 string "if (mysql_num_fields(res) != 1) {",
adamc@874 136 newline,
adamc@874 137 box [string "mysql_free_result(res);",
adamc@874 138 newline,
adamc@874 139 string "mysql_close(conn->conn);",
adamc@874 140 newline,
adamc@874 141 string "uw_error(ctx, FATAL, \"Bad column count:\\n",
adamc@874 142 string q,
adamc@874 143 string "\");",
adamc@874 144 newline],
adamc@874 145 string "}",
adamc@874 146 newline,
adamc@874 147 newline,
adamc@874 148
adamc@874 149 string "if ((row = mysql_fetch_row(res)) == NULL) {",
adamc@874 150 newline,
adamc@874 151 box [string "mysql_free_result(res);",
adamc@874 152 newline,
adamc@874 153 string "mysql_close(conn->conn);",
adamc@874 154 newline,
adamc@874 155 string "uw_error(ctx, FATAL, \"Row fetch failed:\\n",
adamc@874 156 string q,
adamc@874 157 string "\");",
adamc@874 158 newline],
adamc@874 159 string "}",
adamc@874 160 newline,
adamc@874 161 newline,
adamc@874 162
adamc@874 163 string "if (strcmp(row[0], \"1\")) {",
adamc@874 164 newline,
adamc@874 165 box [string "mysql_free_result(res);",
adamc@874 166 newline,
adamc@874 167 string "mysql_close(conn->conn);",
adamc@874 168 newline,
adamc@874 169 string "uw_error(ctx, FATAL, \"Table '",
adamc@874 170 string s,
adamc@874 171 string "' does not exist.\");",
adamc@874 172 newline],
adamc@874 173 string "}",
adamc@874 174 newline,
adamc@874 175 newline,
adamc@874 176 string "mysql_free_result(res);",
adamc@874 177 newline,
adamc@874 178 newline,
adamc@874 179
adamc@874 180 string "if (mysql_query(conn->conn, \"",
adamc@874 181 string q',
adamc@874 182 string "\")) {",
adamc@874 183 newline,
adamc@874 184 box [string "mysql_close(conn->conn);",
adamc@874 185 newline,
adamc@874 186 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@874 187 string q',
adamc@874 188 string "\");",
adamc@874 189 newline],
adamc@874 190 string "}",
adamc@874 191 newline,
adamc@874 192 newline,
adamc@874 193
adamc@874 194 string "if ((res = mysql_store_result(conn->conn)) == NULL) {",
adamc@874 195 newline,
adamc@874 196 box [string "mysql_free_result(res);",
adamc@874 197 newline,
adamc@874 198 string "mysql_close(conn->conn);",
adamc@874 199 newline,
adamc@874 200 string "uw_error(ctx, FATAL, \"Result store failed:\\n",
adamc@874 201 string q',
adamc@874 202 string "\");",
adamc@874 203 newline],
adamc@874 204 string "}",
adamc@874 205 newline,
adamc@874 206 newline,
adamc@874 207
adamc@874 208 string "if (mysql_num_fields(res) != 1) {",
adamc@874 209 newline,
adamc@874 210 box [string "mysql_free_result(res);",
adamc@874 211 newline,
adamc@874 212 string "mysql_close(conn->conn);",
adamc@874 213 newline,
adamc@874 214 string "uw_error(ctx, FATAL, \"Bad column count:\\n",
adamc@874 215 string q',
adamc@874 216 string "\");",
adamc@874 217 newline],
adamc@874 218 string "}",
adamc@874 219 newline,
adamc@874 220 newline,
adamc@874 221
adamc@874 222 string "if ((row = mysql_fetch_row(res)) == NULL) {",
adamc@874 223 newline,
adamc@874 224 box [string "mysql_free_result(res);",
adamc@874 225 newline,
adamc@874 226 string "mysql_close(conn->conn);",
adamc@874 227 newline,
adamc@874 228 string "uw_error(ctx, FATAL, \"Row fetch failed:\\n",
adamc@874 229 string q',
adamc@874 230 string "\");",
adamc@874 231 newline],
adamc@874 232 string "}",
adamc@874 233 newline,
adamc@874 234 newline,
adamc@874 235
adamc@874 236 string "if (strcmp(row[0], \"",
adamc@874 237 string (Int.toString (length xts)),
adamc@874 238 string "\")) {",
adamc@874 239 newline,
adamc@874 240 box [string "mysql_free_result(res);",
adamc@874 241 newline,
adamc@874 242 string "mysql_close(conn->conn);",
adamc@874 243 newline,
adamc@874 244 string "uw_error(ctx, FATAL, \"Table '",
adamc@874 245 string s,
adamc@874 246 string "' has the wrong column types.\");",
adamc@874 247 newline],
adamc@874 248 string "}",
adamc@874 249 newline,
adamc@874 250 newline,
adamc@874 251 string "mysql_free_result(res);",
adamc@874 252 newline,
adamc@874 253 newline,
adamc@874 254
adamc@874 255 string "if (mysql_query(conn->conn, \"",
adamc@874 256 string q'',
adamc@874 257 string "\")) {",
adamc@874 258 newline,
adamc@874 259 box [string "mysql_close(conn->conn);",
adamc@874 260 newline,
adamc@874 261 string "uw_error(ctx, FATAL, \"Query failed:\\n",
adamc@874 262 string q'',
adamc@874 263 string "\");",
adamc@874 264 newline],
adamc@874 265 string "}",
adamc@874 266 newline,
adamc@874 267 newline,
adamc@874 268
adamc@874 269 string "if ((res = mysql_store_result(conn->conn)) == NULL) {",
adamc@874 270 newline,
adamc@874 271 box [string "mysql_free_result(res);",
adamc@874 272 newline,
adamc@874 273 string "mysql_close(conn->conn);",
adamc@874 274 newline,
adamc@874 275 string "uw_error(ctx, FATAL, \"Result store failed:\\n",
adamc@874 276 string q'',
adamc@874 277 string "\");",
adamc@874 278 newline],
adamc@874 279 string "}",
adamc@874 280 newline,
adamc@874 281 newline,
adamc@874 282
adamc@874 283 string "if (mysql_num_fields(res) != 1) {",
adamc@874 284 newline,
adamc@874 285 box [string "mysql_free_result(res);",
adamc@874 286 newline,
adamc@874 287 string "mysql_close(conn->conn);",
adamc@874 288 newline,
adamc@874 289 string "uw_error(ctx, FATAL, \"Bad column count:\\n",
adamc@874 290 string q'',
adamc@874 291 string "\");",
adamc@874 292 newline],
adamc@874 293 string "}",
adamc@874 294 newline,
adamc@874 295 newline,
adamc@874 296
adamc@874 297 string "if ((row = mysql_fetch_row(res)) == NULL) {",
adamc@874 298 newline,
adamc@874 299 box [string "mysql_free_result(res);",
adamc@874 300 newline,
adamc@874 301 string "mysql_close(conn->conn);",
adamc@874 302 newline,
adamc@874 303 string "uw_error(ctx, FATAL, \"Row fetch failed:\\n",
adamc@874 304 string q'',
adamc@874 305 string "\");",
adamc@874 306 newline],
adamc@874 307 string "}",
adamc@874 308 newline,
adamc@874 309 newline,
adamc@874 310
adamc@874 311 string "if (strcmp(row[0], \"",
adamc@874 312 string (Int.toString (length xts)),
adamc@874 313 string "\")) {",
adamc@874 314 newline,
adamc@874 315 box [string "mysql_free_result(res);",
adamc@874 316 newline,
adamc@874 317 string "mysql_close(conn->conn);",
adamc@874 318 newline,
adamc@874 319 string "uw_error(ctx, FATAL, \"Table '",
adamc@874 320 string s,
adamc@874 321 string "' has extra columns.\");",
adamc@874 322 newline],
adamc@874 323 string "}",
adamc@874 324 newline,
adamc@874 325 newline,
adamc@874 326 string "mysql_free_result(res);",
adamc@874 327 newline]
adamc@874 328 end
adamc@874 329
adamc@872 330 fun init {dbstring, prepared = ss, tables, views, sequences} =
adamc@866 331 let
adamc@866 332 val host = ref NONE
adamc@866 333 val user = ref NONE
adamc@866 334 val passwd = ref NONE
adamc@866 335 val db = ref NONE
adamc@866 336 val port = ref NONE
adamc@866 337 val unix_socket = ref NONE
adamc@866 338
adamc@866 339 fun stringOf r = case !r of
adamc@866 340 NONE => string "NULL"
adamc@866 341 | SOME s => box [string "\"",
adamc@866 342 string (String.toString s),
adamc@866 343 string "\""]
adamc@866 344 in
adamc@866 345 app (fn s =>
adamc@866 346 case String.fields (fn ch => ch = #"=") s of
adamc@866 347 [name, value] =>
adamc@866 348 (case name of
adamc@866 349 "host" =>
adamc@866 350 if size value > 0 andalso String.sub (value, 0) = #"/" then
adamc@866 351 unix_socket := SOME value
adamc@866 352 else
adamc@866 353 host := SOME value
adamc@866 354 | "hostaddr" => host := SOME value
adamc@866 355 | "port" => port := Int.fromString value
adamc@866 356 | "dbname" => db := SOME value
adamc@866 357 | "user" => user := SOME value
adamc@866 358 | "password" => passwd := SOME value
adamc@866 359 | _ => ())
adamc@866 360 | _ => ()) (String.tokens Char.isSpace dbstring);
adamc@866 361
adamc@866 362 box [string "typedef struct {",
adamc@866 363 newline,
adamc@866 364 box [string "MYSQL *conn;",
adamc@866 365 newline,
adamc@866 366 p_list_sepi (box [])
adamc@866 367 (fn i => fn _ =>
adamc@866 368 box [string "MYSQL_STMT *p",
adamc@866 369 string (Int.toString i),
adamc@866 370 string ";",
adamc@866 371 newline])
adamc@866 372 ss],
adamc@866 373 string "} uw_conn;",
adamc@866 374 newline,
adamc@866 375 newline,
adamc@866 376
adamc@874 377 string "void uw_client_init(void) {",
adamc@874 378 newline,
adamc@874 379 box [string "if (mysql_library_init(0, NULL, NULL)) {",
adamc@874 380 newline,
adamc@874 381 box [string "fprintf(stderr, \"Could not initialize MySQL library\\n\");",
adamc@874 382 newline,
adamc@874 383 string "exit(1);",
adamc@874 384 newline],
adamc@874 385 string "}",
adamc@874 386 newline],
adamc@874 387 string "}",
adamc@874 388 newline,
adamc@874 389 newline,
adamc@874 390
adamc@866 391 if #persistent (currentProtocol ()) then
adamc@874 392 box [string "static void uw_db_validate(uw_context ctx) {",
adamc@874 393 newline,
adamc@874 394 string "uw_conn *conn = uw_get_db(ctx);",
adamc@874 395 newline,
adamc@874 396 string "MYSQL_RES *res;",
adamc@874 397 newline,
adamc@874 398 string "MYSQL_ROW row;",
adamc@874 399 newline,
adamc@874 400 newline,
adamc@874 401 p_list_sep newline (checkRel ("tables", true)) tables,
adamc@874 402 p_list_sep newline (checkRel ("views", false)) views,
adamc@874 403 string "}",
adamc@874 404 newline,
adamc@874 405 newline,
adamc@874 406
adamc@874 407 string "static void uw_db_prepare(uw_context ctx) {",
adamc@866 408 newline,
adamc@866 409 string "uw_conn *conn = uw_get_db(ctx);",
adamc@866 410 newline,
adamc@866 411 string "MYSQL_STMT *stmt;",
adamc@866 412 newline,
adamc@866 413 newline,
adamc@866 414
adamc@866 415 p_list_sepi newline (fn i => fn (s, n) =>
adamc@866 416 let
adamc@866 417 fun uhoh this s args =
adamc@866 418 box [p_list_sepi (box [])
adamc@866 419 (fn j => fn () =>
adamc@866 420 box [string
adamc@866 421 "mysql_stmt_close(conn->p",
adamc@866 422 string (Int.toString j),
adamc@866 423 string ");",
adamc@866 424 newline])
adamc@866 425 (List.tabulate (i, fn _ => ())),
adamc@866 426 box (if this then
adamc@866 427 [string
adamc@866 428 "mysql_stmt_close(conn->p",
adamc@866 429 string (Int.toString i),
adamc@866 430 string ");",
adamc@866 431 newline]
adamc@866 432 else
adamc@866 433 []),
adamc@866 434 string "mysql_close(conn->conn);",
adamc@866 435 newline,
adamc@866 436 string "uw_error(ctx, FATAL, \"",
adamc@866 437 string s,
adamc@866 438 string "\"",
adamc@866 439 p_list_sep (box []) (fn s => box [string ", ",
adamc@866 440 string s]) args,
adamc@866 441 string ");",
adamc@866 442 newline]
adamc@866 443 in
adamc@866 444 box [string "stmt = mysql_stmt_init(conn->conn);",
adamc@866 445 newline,
adamc@866 446 string "if (stmt == NULL) {",
adamc@866 447 newline,
adamc@866 448 uhoh false "Out of memory allocating prepared statement" [],
adamc@866 449 string "}",
adamc@866 450 newline,
adamc@874 451 string "conn->p",
adamc@874 452 string (Int.toString i),
adamc@874 453 string " = stmt;",
adamc@874 454 newline,
adamc@866 455
adamc@866 456 string "if (mysql_stmt_prepare(stmt, \"",
adamc@866 457 string (String.toString s),
adamc@866 458 string "\", ",
adamc@866 459 string (Int.toString (size s)),
adamc@866 460 string ")) {",
adamc@866 461 newline,
adamc@866 462 box [string "char msg[1024];",
adamc@866 463 newline,
adamc@866 464 string "strncpy(msg, mysql_stmt_error(stmt), 1024);",
adamc@866 465 newline,
adamc@866 466 string "msg[1023] = 0;",
adamc@866 467 newline,
adamc@866 468 uhoh true "Error preparing statement: %s" ["msg"]],
adamc@866 469 string "}",
adamc@866 470 newline]
adamc@866 471 end)
adamc@866 472 ss,
adamc@866 473
adamc@866 474 string "}"]
adamc@866 475 else
adamc@866 476 string "static void uw_db_prepare(uw_context ctx) { }",
adamc@866 477 newline,
adamc@866 478 newline,
adamc@866 479
adamc@866 480 string "void uw_db_init(uw_context ctx) {",
adamc@866 481 newline,
adamc@866 482 string "MYSQL *mysql = mysql_init(NULL);",
adamc@866 483 newline,
adamc@866 484 string "uw_conn *conn;",
adamc@866 485 newline,
adamc@866 486 string "if (mysql == NULL) uw_error(ctx, FATAL, ",
adamc@866 487 string "\"libmysqlclient can't allocate a connection.\");",
adamc@866 488 newline,
adamc@866 489 string "if (mysql_real_connect(mysql, ",
adamc@866 490 stringOf host,
adamc@866 491 string ", ",
adamc@866 492 stringOf user,
adamc@866 493 string ", ",
adamc@866 494 stringOf passwd,
adamc@866 495 string ", ",
adamc@866 496 stringOf db,
adamc@866 497 string ", ",
adamc@866 498 case !port of
adamc@866 499 NONE => string "0"
adamc@866 500 | SOME n => string (Int.toString n),
adamc@866 501 string ", ",
adamc@866 502 stringOf unix_socket,
adamc@874 503 string ", 0) == NULL) {",
adamc@866 504 newline,
adamc@866 505 box [string "char msg[1024];",
adamc@866 506 newline,
adamc@866 507 string "strncpy(msg, mysql_error(mysql), 1024);",
adamc@866 508 newline,
adamc@866 509 string "msg[1023] = 0;",
adamc@866 510 newline,
adamc@866 511 string "mysql_close(mysql);",
adamc@866 512 newline,
adamc@866 513 string "uw_error(ctx, BOUNDED_RETRY, ",
adamc@866 514 string "\"Connection to MySQL server failed: %s\", msg);"],
adamc@866 515 newline,
adamc@866 516 string "}",
adamc@866 517 newline,
adamc@874 518 string "conn = calloc(1, sizeof(uw_conn));",
adamc@866 519 newline,
adamc@866 520 string "conn->conn = mysql;",
adamc@866 521 newline,
adamc@866 522 string "uw_set_db(ctx, conn);",
adamc@866 523 newline,
adamc@866 524 string "uw_db_validate(ctx);",
adamc@866 525 newline,
adamc@866 526 string "uw_db_prepare(ctx);",
adamc@866 527 newline,
adamc@866 528 string "}",
adamc@866 529 newline,
adamc@866 530 newline,
adamc@866 531
adamc@866 532 string "void uw_db_close(uw_context ctx) {",
adamc@866 533 newline,
adamc@866 534 string "uw_conn *conn = uw_get_db(ctx);",
adamc@866 535 newline,
adamc@866 536 p_list_sepi (box [])
adamc@866 537 (fn i => fn _ =>
adamc@866 538 box [string "if (conn->p",
adamc@866 539 string (Int.toString i),
adamc@866 540 string ") mysql_stmt_close(conn->p",
adamc@866 541 string (Int.toString i),
adamc@866 542 string ");",
adamc@866 543 newline])
adamc@866 544 ss,
adamc@866 545 string "mysql_close(conn->conn);",
adamc@866 546 newline,
adamc@866 547 string "}",
adamc@866 548 newline,
adamc@866 549 newline,
adamc@866 550
adamc@866 551 string "int uw_db_begin(uw_context ctx) {",
adamc@866 552 newline,
adamc@866 553 string "uw_conn *conn = uw_get_db(ctx);",
adamc@866 554 newline,
adamc@866 555 newline,
adamc@866 556 string "return mysql_query(conn->conn, \"SET TRANSACTION ISOLATION LEVEL SERIALIZABLE\")",
adamc@866 557 newline,
adamc@866 558 string " || mysql_query(conn->conn, \"BEGIN\");",
adamc@866 559 newline,
adamc@866 560 string "}",
adamc@866 561 newline,
adamc@866 562 newline,
adamc@866 563
adamc@866 564 string "int uw_db_commit(uw_context ctx) {",
adamc@866 565 newline,
adamc@866 566 string "uw_conn *conn = uw_get_db(ctx);",
adamc@866 567 newline,
adamc@866 568 string "return mysql_commit(conn->conn);",
adamc@866 569 newline,
adamc@866 570 string "}",
adamc@866 571 newline,
adamc@866 572 newline,
adamc@866 573
adamc@866 574 string "int uw_db_rollback(uw_context ctx) {",
adamc@866 575 newline,
adamc@866 576 string "uw_conn *conn = uw_get_db(ctx);",
adamc@866 577 newline,
adamc@866 578 string "return mysql_rollback(conn->conn);",
adamc@866 579 newline,
adamc@866 580 string "}",
adamc@866 581 newline,
adamc@866 582 newline]
adamc@866 583 end
adamc@866 584
adamc@873 585 fun p_getcol {wontLeakStrings = _, col = i, typ = t} =
adamc@873 586 let
adamc@873 587 fun getter t =
adamc@873 588 case t of
adamc@873 589 String => box [string "({",
adamc@873 590 newline,
adamc@873 591 string "uw_Basis_string s = uw_malloc(ctx, length",
adamc@873 592 string (Int.toString i),
adamc@873 593 string " + 1);",
adamc@873 594 newline,
adamc@873 595 string "out[",
adamc@873 596 string (Int.toString i),
adamc@873 597 string "].buffer = s;",
adamc@873 598 newline,
adamc@873 599 string "out[",
adamc@873 600 string (Int.toString i),
adamc@873 601 string "].buffer_length = length",
adamc@873 602 string (Int.toString i),
adamc@873 603 string " + 1;",
adamc@873 604 newline,
adamc@873 605 string "mysql_stmt_fetch_column(stmt, &out[",
adamc@873 606 string (Int.toString i),
adamc@873 607 string "], ",
adamc@873 608 string (Int.toString i),
adamc@873 609 string ", 0);",
adamc@873 610 newline,
adamc@873 611 string "s[length",
adamc@873 612 string (Int.toString i),
adamc@873 613 string "] = 0;",
adamc@873 614 newline,
adamc@873 615 string "s;",
adamc@873 616 newline,
adamc@873 617 string "})"]
adamc@873 618 | Blob => box [string "({",
adamc@873 619 newline,
adamc@873 620 string "uw_Basis_blob b = {length",
adamc@873 621 string (Int.toString i),
adamc@873 622 string ", uw_malloc(ctx, length",
adamc@873 623 string (Int.toString i),
adamc@873 624 string ")};",
adamc@873 625 newline,
adamc@873 626 string "out[",
adamc@873 627 string (Int.toString i),
adamc@873 628 string "].buffer = b.data;",
adamc@873 629 newline,
adamc@873 630 string "out[",
adamc@873 631 string (Int.toString i),
adamc@873 632 string "].buffer_length = length",
adamc@873 633 string (Int.toString i),
adamc@873 634 string ";",
adamc@873 635 newline,
adamc@873 636 string "mysql_stmt_fetch_column(stmt, &out[",
adamc@873 637 string (Int.toString i),
adamc@873 638 string "], ",
adamc@873 639 string (Int.toString i),
adamc@873 640 string ", 0);",
adamc@873 641 newline,
adamc@873 642 string "b;",
adamc@873 643 newline,
adamc@873 644 string "})"]
adamc@873 645 | Time => box [string "({",
adamc@876 646 string "MYSQL_TIME *mt = &buffer",
adamc@873 647 string (Int.toString i),
adamc@873 648 string ";",
adamc@873 649 newline,
adamc@873 650 newline,
adamc@873 651 string "struct tm t = {mt->second, mt->minute, mt->hour, mt->day, mt->month, mt->year, 0, 0, -1};",
adamc@873 652 newline,
adamc@876 653 string "mktime(&t);",
adamc@873 654 newline,
adamc@873 655 string "})"]
adamc@873 656 | _ => box [string "buffer",
adamc@873 657 string (Int.toString i)]
adamc@873 658 in
adamc@873 659 case t of
adamc@873 660 Nullable t => box [string "(is_null",
adamc@873 661 string (Int.toString i),
adamc@873 662 string " ? NULL : ",
adamc@873 663 case t of
adamc@873 664 String => getter t
adamc@873 665 | _ => box [string "({",
adamc@873 666 newline,
adamc@873 667 string (p_sql_ctype t),
adamc@873 668 space,
adamc@873 669 string "*tmp = uw_malloc(ctx, sizeof(",
adamc@873 670 string (p_sql_ctype t),
adamc@873 671 string "));",
adamc@873 672 newline,
adamc@873 673 string "*tmp = ",
adamc@873 674 getter t,
adamc@873 675 string ";",
adamc@873 676 newline,
adamc@873 677 string "tmp;",
adamc@873 678 newline,
adamc@873 679 string "})"],
adamc@873 680 string ")"]
adamc@873 681 | _ => box [string "(is_null",
adamc@873 682 string (Int.toString i),
adamc@873 683 string " ? ",
adamc@873 684 box [string "({",
adamc@873 685 string (p_sql_ctype t),
adamc@873 686 space,
adamc@873 687 string "tmp;",
adamc@873 688 newline,
adamc@873 689 string "uw_error(ctx, FATAL, \"Unexpectedly NULL field #",
adamc@873 690 string (Int.toString i),
adamc@873 691 string "\");",
adamc@873 692 newline,
adamc@873 693 string "tmp;",
adamc@873 694 newline,
adamc@873 695 string "})"],
adamc@873 696 string " : ",
adamc@873 697 getter t,
adamc@873 698 string ")"]
adamc@873 699 end
adamc@873 700
adamc@873 701 fun queryCommon {loc, query, cols, doCols} =
adamc@873 702 box [string "int n, r;",
adamc@873 703 newline,
adamc@873 704 string "MYSQL_BIND out[",
adamc@873 705 string (Int.toString (length cols)),
adamc@873 706 string "];",
adamc@873 707 newline,
adamc@873 708 p_list_sepi (box []) (fn i => fn t =>
adamc@873 709 let
adamc@873 710 fun buffers t =
adamc@873 711 case t of
adamc@873 712 String => box [string "unsigned long length",
adamc@873 713 string (Int.toString i),
adamc@873 714 string ";",
adamc@873 715 newline]
adamc@873 716 | Blob => box [string "unsigned long length",
adamc@873 717 string (Int.toString i),
adamc@873 718 string ";",
adamc@873 719 newline]
adamc@876 720 | Time => box [string "MYSQL_TIME buffer",
adamc@876 721 string (Int.toString i),
adamc@876 722 string ";",
adamc@876 723 newline]
adamc@873 724 | _ => box [string (p_sql_ctype t),
adamc@873 725 space,
adamc@873 726 string "buffer",
adamc@873 727 string (Int.toString i),
adamc@873 728 string ";",
adamc@873 729 newline]
adamc@873 730 in
adamc@873 731 box [string "my_bool is_null",
adamc@873 732 string (Int.toString i),
adamc@873 733 string ";",
adamc@873 734 newline,
adamc@873 735 case t of
adamc@873 736 Nullable t => buffers t
adamc@873 737 | _ => buffers t,
adamc@873 738 newline]
adamc@873 739 end) cols,
adamc@873 740 newline,
adamc@873 741
adamc@873 742 string "memset(out, 0, sizeof out);",
adamc@873 743 newline,
adamc@873 744 p_list_sepi (box []) (fn i => fn t =>
adamc@873 745 let
adamc@873 746 fun buffers t =
adamc@873 747 case t of
adamc@875 748 String => box [string "out[",
adamc@875 749 string (Int.toString i),
adamc@875 750 string "].length = &length",
adamc@875 751 string (Int.toString i),
adamc@875 752 string ";",
adamc@875 753 newline]
adamc@875 754 | Blob => box [string "out[",
adamc@875 755 string (Int.toString i),
adamc@875 756 string "].length = &length",
adamc@875 757 string (Int.toString i),
adamc@875 758 string ";",
adamc@875 759 newline]
adamc@873 760 | _ => box [string "out[",
adamc@873 761 string (Int.toString i),
adamc@873 762 string "].buffer = &buffer",
adamc@873 763 string (Int.toString i),
adamc@873 764 string ";",
adamc@873 765 newline]
adamc@873 766 in
adamc@873 767 box [string "out[",
adamc@873 768 string (Int.toString i),
adamc@873 769 string "].buffer_type = ",
adamc@873 770 string (p_buffer_type t),
adamc@873 771 string ";",
adamc@873 772 newline,
adamc@873 773 string "out[",
adamc@873 774 string (Int.toString i),
adamc@873 775 string "].is_null = &is_null",
adamc@873 776 string (Int.toString i),
adamc@873 777 string ";",
adamc@873 778 newline,
adamc@873 779
adamc@873 780 case t of
adamc@873 781 Nullable t => buffers t
adamc@873 782 | _ => buffers t,
adamc@873 783 newline]
adamc@873 784 end) cols,
adamc@873 785 newline,
adamc@873 786
adamc@875 787 string "if (mysql_stmt_reset(stmt)) uw_error(ctx, FATAL, \"",
adamc@875 788 string (ErrorMsg.spanToString loc),
adamc@875 789 string ": Error reseting statement: %s\\n%s\", ",
adamc@875 790 query,
adamc@875 791 string ", mysql_error(conn->conn));",
adamc@875 792 newline,
adamc@875 793 newline,
adamc@875 794
adamc@873 795 string "if (mysql_stmt_execute(stmt)) uw_error(ctx, FATAL, \"",
adamc@873 796 string (ErrorMsg.spanToString loc),
adamc@875 797 string ": Error executing query: %s\\n%s\", ",
adamc@875 798 query,
adamc@875 799 string ", mysql_error(conn->conn));",
adamc@875 800 newline,
adamc@875 801 newline,
adamc@875 802
adamc@875 803 string "if (mysql_stmt_bind_result(stmt, out)) uw_error(ctx, FATAL, \"",
adamc@875 804 string (ErrorMsg.spanToString loc),
adamc@875 805 string ": Error binding query result: %s\\n%s\", ",
adamc@875 806 query,
adamc@875 807 string ", mysql_error(conn->conn));",
adamc@873 808 newline,
adamc@873 809 newline,
adamc@873 810
adamc@873 811 string "if (mysql_stmt_store_result(stmt)) uw_error(ctx, FATAL, \"",
adamc@873 812 string (ErrorMsg.spanToString loc),
adamc@875 813 string ": Error storing query result: %s\\n%s\", ",
adamc@875 814 query,
adamc@875 815 string ", mysql_error(conn->conn));",
adamc@873 816 newline,
adamc@873 817 newline,
adamc@873 818
adamc@873 819 string "uw_end_region(ctx);",
adamc@873 820 newline,
adamc@875 821 string "while (1) {",
adamc@875 822 newline,
adamc@875 823 string "r = mysql_stmt_fetch(stmt);",
adamc@875 824 newline,
adamc@875 825 string "if (r != 0 && r != MYSQL_DATA_TRUNCATED) break;",
adamc@873 826 newline,
adamc@873 827 doCols p_getcol,
adamc@873 828 string "}",
adamc@873 829 newline,
adamc@873 830 newline,
adamc@873 831
adamc@874 832 string "if (r == 1) uw_error(ctx, FATAL, \"",
adamc@873 833 string (ErrorMsg.spanToString loc),
adamc@875 834 string ": query result fetching failed: %s\\n%s\", ",
adamc@875 835 query,
adamc@875 836 string ", mysql_error(conn->conn));",
adamc@875 837 newline,
adamc@875 838 newline,
adamc@875 839
adamc@875 840 string "if (mysql_stmt_reset(stmt)) uw_error(ctx, FATAL, \"",
adamc@875 841 string (ErrorMsg.spanToString loc),
adamc@875 842 string ": Error reseting statement: %s\\n%s\", ",
adamc@875 843 query,
adamc@875 844 string ", mysql_error(conn->conn));",
adamc@875 845 newline,
adamc@875 846 newline]
adamc@873 847
adamc@873 848 fun query {loc, cols, doCols} =
adamc@873 849 box [string "uw_conn *conn = uw_get_db(ctx);",
adamc@873 850 newline,
adamc@876 851 string "MYSQL_STMT *stmt = mysql_stmt_init(conn->conn);",
adamc@873 852 newline,
adamc@875 853 string "if (stmt == NULL) uw_error(ctx, FATAL, \"",
adamc@873 854 string (ErrorMsg.spanToString loc),
adamc@873 855 string ": can't allocate temporary prepared statement\");",
adamc@873 856 newline,
adamc@873 857 string "uw_push_cleanup(ctx, (void (*)(void *))mysql_stmt_close, stmt);",
adamc@873 858 newline,
adamc@873 859 string "if (mysql_stmt_prepare(stmt, query, strlen(query))) uw_error(ctx, FATAL, \"",
adamc@873 860 string (ErrorMsg.spanToString loc),
adamc@875 861 string ": error preparing statement: %s\\n%s\", query, mysql_error(conn->conn));",
adamc@873 862 newline,
adamc@873 863 newline,
adamc@873 864
adamc@873 865 queryCommon {loc = loc, cols = cols, doCols = doCols, query = string "query"},
adamc@873 866
adamc@873 867 string "uw_pop_cleanup(ctx);",
adamc@873 868 newline]
adamc@873 869
adamc@873 870 fun queryPrepared {loc, id, query, inputs, cols, doCols} =
adamc@873 871 box [string "uw_conn *conn = uw_get_db(ctx);",
adamc@873 872 newline,
adamc@873 873 string "MYSQL_BIND in[",
adamc@873 874 string (Int.toString (length inputs)),
adamc@873 875 string "];",
adamc@873 876 newline,
adamc@873 877 p_list_sepi (box []) (fn i => fn t =>
adamc@873 878 let
adamc@873 879 fun buffers t =
adamc@873 880 case t of
adamc@873 881 String => box [string "unsigned long in_length",
adamc@873 882 string (Int.toString i),
adamc@873 883 string ";",
adamc@873 884 newline]
adamc@873 885 | Blob => box [string "unsigned long in_length",
adamc@873 886 string (Int.toString i),
adamc@873 887 string ";",
adamc@873 888 newline]
adamc@876 889 | Time => box [string "MYSQL_TIME in_buffer",
adamc@873 890 string (Int.toString i),
adamc@873 891 string ";",
adamc@873 892 newline]
adamc@873 893 | _ => box []
adamc@873 894 in
adamc@873 895 box [case t of
adamc@873 896 Nullable t => box [string "my_bool in_is_null",
adamc@873 897 string (Int.toString i),
adamc@873 898 string ";",
adamc@873 899 newline,
adamc@873 900 buffers t]
adamc@873 901 | _ => buffers t,
adamc@873 902 newline]
adamc@873 903 end) inputs,
adamc@873 904 string "MYSQL_STMT *stmt = conn->p",
adamc@873 905 string (Int.toString id),
adamc@873 906 string ";",
adamc@873 907 newline,
adamc@873 908 newline,
adamc@873 909
adamc@878 910 string "if (stmt == NULL) {",
adamc@878 911 newline,
adamc@878 912 box [string "stmt = mysql_stmt_init(conn->conn);",
adamc@878 913 newline,
adamc@878 914 string "if (stmt == NULL) uw_error(ctx, FATAL, \"Out of memory allocating prepared statement\");",
adamc@878 915 newline,
adamc@878 916 string "if (mysql_stmt_prepare(stmt, \"",
adamc@878 917 string (String.toString query),
adamc@878 918 string "\", ",
adamc@878 919 string (Int.toString (size query)),
adamc@878 920 string ")) {",
adamc@878 921 newline,
adamc@878 922 box [string "char msg[1024];",
adamc@878 923 newline,
adamc@878 924 string "strncpy(msg, mysql_stmt_error(stmt), 1024);",
adamc@878 925 newline,
adamc@878 926 string "msg[1023] = 0;",
adamc@878 927 newline,
adamc@878 928 string "uw_error(ctx, FATAL, \"Error preparing statement: %s\", msg);",
adamc@878 929 newline],
adamc@878 930 string "}",
adamc@878 931 newline,
adamc@878 932 string "conn->p",
adamc@878 933 string (Int.toString id),
adamc@878 934 string " = stmt;",
adamc@878 935 newline],
adamc@878 936 string "}",
adamc@878 937 newline,
adamc@878 938 newline,
adamc@878 939
adamc@873 940 string "memset(in, 0, sizeof in);",
adamc@873 941 newline,
adamc@873 942 p_list_sepi (box []) (fn i => fn t =>
adamc@873 943 let
adamc@873 944 fun buffers t =
adamc@873 945 case t of
adamc@873 946 String => box [string "in[",
adamc@873 947 string (Int.toString i),
adamc@873 948 string "].buffer = arg",
adamc@873 949 string (Int.toString (i + 1)),
adamc@873 950 string ";",
adamc@873 951 newline,
adamc@873 952 string "in_length",
adamc@873 953 string (Int.toString i),
adamc@873 954 string "= in[",
adamc@873 955 string (Int.toString i),
adamc@873 956 string "].buffer_length = strlen(arg",
adamc@873 957 string (Int.toString (i + 1)),
adamc@873 958 string ");",
adamc@873 959 newline,
adamc@873 960 string "in[",
adamc@873 961 string (Int.toString i),
adamc@873 962 string "].length = &in_length",
adamc@873 963 string (Int.toString i),
adamc@873 964 string ";",
adamc@873 965 newline]
adamc@873 966 | Blob => box [string "in[",
adamc@873 967 string (Int.toString i),
adamc@873 968 string "].buffer = arg",
adamc@873 969 string (Int.toString (i + 1)),
adamc@873 970 string ".data;",
adamc@873 971 newline,
adamc@873 972 string "in_length",
adamc@873 973 string (Int.toString i),
adamc@873 974 string "= in[",
adamc@873 975 string (Int.toString i),
adamc@873 976 string "].buffer_length = arg",
adamc@873 977 string (Int.toString (i + 1)),
adamc@873 978 string ".size;",
adamc@873 979 newline,
adamc@873 980 string "in[",
adamc@873 981 string (Int.toString i),
adamc@873 982 string "].length = &in_length",
adamc@873 983 string (Int.toString i),
adamc@873 984 string ";",
adamc@873 985 newline]
adamc@873 986 | Time =>
adamc@873 987 let
adamc@873 988 fun oneField dst src =
adamc@873 989 box [string "in_buffer",
adamc@873 990 string (Int.toString i),
adamc@873 991 string ".",
adamc@873 992 string dst,
adamc@873 993 string " = tms.tm_",
adamc@873 994 string src,
adamc@873 995 string ";",
adamc@873 996 newline]
adamc@873 997 in
adamc@873 998 box [string "({",
adamc@873 999 newline,
adamc@873 1000 string "struct tm tms;",
adamc@873 1001 newline,
adamc@873 1002 string "if (localtime_r(&arg",
adamc@873 1003 string (Int.toString (i + 1)),
adamc@873 1004 string ", &tm) == NULL) uw_error(\"",
adamc@873 1005 string (ErrorMsg.spanToString loc),
adamc@873 1006 string ": error converting to MySQL time\");",
adamc@873 1007 newline,
adamc@873 1008 oneField "year" "year",
adamc@873 1009 oneField "month" "mon",
adamc@873 1010 oneField "day" "mday",
adamc@873 1011 oneField "hour" "hour",
adamc@873 1012 oneField "minute" "min",
adamc@873 1013 oneField "second" "sec",
adamc@873 1014 newline,
adamc@873 1015 string "in[",
adamc@873 1016 string (Int.toString i),
adamc@873 1017 string "].buffer = &in_buffer",
adamc@873 1018 string (Int.toString i),
adamc@873 1019 string ";",
adamc@873 1020 newline]
adamc@873 1021 end
adamc@873 1022
adamc@873 1023 | _ => box [string "in[",
adamc@873 1024 string (Int.toString i),
adamc@873 1025 string "].buffer = &arg",
adamc@873 1026 string (Int.toString (i + 1)),
adamc@873 1027 string ";",
adamc@873 1028 newline]
adamc@873 1029 in
adamc@873 1030 box [string "in[",
adamc@873 1031 string (Int.toString i),
adamc@873 1032 string "].buffer_type = ",
adamc@873 1033 string (p_buffer_type t),
adamc@873 1034 string ";",
adamc@873 1035 newline,
adamc@873 1036
adamc@873 1037 case t of
adamc@873 1038 Nullable t => box [string "in[",
adamc@873 1039 string (Int.toString i),
adamc@873 1040 string "].is_null = &in_is_null",
adamc@873 1041 string (Int.toString i),
adamc@873 1042 string ";",
adamc@873 1043 newline,
adamc@873 1044 string "if (arg",
adamc@873 1045 string (Int.toString (i + 1)),
adamc@873 1046 string " == NULL) {",
adamc@873 1047 newline,
adamc@873 1048 box [string "in_is_null",
adamc@873 1049 string (Int.toString i),
adamc@873 1050 string " = 1;",
adamc@873 1051 newline],
adamc@873 1052 string "} else {",
adamc@873 1053 box [case t of
adamc@873 1054 String => box []
adamc@873 1055 | _ =>
adamc@873 1056 box [string (p_sql_ctype t),
adamc@873 1057 space,
adamc@876 1058 string "tmp = *arg",
adamc@876 1059 string (Int.toString (i + 1)),
adamc@876 1060 string ";",
adamc@876 1061 newline,
adamc@876 1062 string (p_sql_ctype t),
adamc@876 1063 space,
adamc@873 1064 string "arg",
adamc@873 1065 string (Int.toString (i + 1)),
adamc@876 1066 string " = tmp;",
adamc@873 1067 newline],
adamc@873 1068 string "in_is_null",
adamc@873 1069 string (Int.toString i),
adamc@873 1070 string " = 0;",
adamc@873 1071 newline,
adamc@873 1072 buffers t,
adamc@876 1073 newline],
adamc@876 1074 string "}",
adamc@876 1075 newline]
adamc@873 1076
adamc@873 1077 | _ => buffers t,
adamc@873 1078 newline]
adamc@873 1079 end) inputs,
adamc@873 1080 newline,
adamc@873 1081
adamc@875 1082 string "if (mysql_stmt_bind_param(stmt, in)) uw_error(ctx, FATAL, \"",
adamc@875 1083 string (ErrorMsg.spanToString loc),
adamc@875 1084 string ": error binding parameters\");",
adamc@875 1085 newline,
adamc@875 1086
adamc@873 1087 queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"",
adamc@873 1088 string (String.toString query),
adamc@873 1089 string "\""]}]
adamc@873 1090
adamc@875 1091 fun dmlCommon {loc, dml} =
adamc@875 1092 box [string "if (mysql_stmt_execute(stmt)) uw_error(ctx, FATAL, \"",
adamc@875 1093 string (ErrorMsg.spanToString loc),
adamc@875 1094 string ": Error executing DML: %s\\n%s\", ",
adamc@875 1095 dml,
adamc@875 1096 string ", mysql_error(conn->conn));",
adamc@875 1097 newline,
adamc@875 1098 newline]
adamc@875 1099
adamc@875 1100 fun dml loc =
adamc@875 1101 box [string "uw_conn *conn = uw_get_db(ctx);",
adamc@875 1102 newline,
adamc@875 1103 string "MYSQL_stmt *stmt = mysql_stmt_init(conn->conn);",
adamc@875 1104 newline,
adamc@875 1105 string "if (stmt == NULL) uw_error(ctx, \"",
adamc@875 1106 string (ErrorMsg.spanToString loc),
adamc@875 1107 string ": can't allocate temporary prepared statement\");",
adamc@875 1108 newline,
adamc@875 1109 string "uw_push_cleanup(ctx, (void (*)(void *))mysql_stmt_close, stmt);",
adamc@875 1110 newline,
adamc@875 1111 string "if (mysql_stmt_prepare(stmt, dml, strlen(dml))) uw_error(ctx, FATAL, \"",
adamc@875 1112 string (ErrorMsg.spanToString loc),
adamc@875 1113 string ": error preparing statement: %s\\n%s\", dml, mysql_error(conn->conn));",
adamc@875 1114 newline,
adamc@875 1115 newline,
adamc@875 1116
adamc@875 1117 dmlCommon {loc = loc, dml = string "dml"},
adamc@875 1118
adamc@875 1119 string "uw_pop_cleanup(ctx);",
adamc@875 1120 newline]
adamc@875 1121
adamc@875 1122 fun dmlPrepared {loc, id, dml, inputs} =
adamc@875 1123 box [string "uw_conn *conn = uw_get_db(ctx);",
adamc@875 1124 newline,
adamc@875 1125 string "MYSQL_BIND in[",
adamc@875 1126 string (Int.toString (length inputs)),
adamc@875 1127 string "];",
adamc@875 1128 newline,
adamc@875 1129 p_list_sepi (box []) (fn i => fn t =>
adamc@875 1130 let
adamc@875 1131 fun buffers t =
adamc@875 1132 case t of
adamc@875 1133 String => box [string "unsigned long in_length",
adamc@875 1134 string (Int.toString i),
adamc@875 1135 string ";",
adamc@875 1136 newline]
adamc@875 1137 | Blob => box [string "unsigned long in_length",
adamc@875 1138 string (Int.toString i),
adamc@875 1139 string ";",
adamc@875 1140 newline]
adamc@876 1141 | Time => box [string "MYSQL_TIME in_buffer",
adamc@875 1142 string (Int.toString i),
adamc@875 1143 string ";",
adamc@875 1144 newline]
adamc@875 1145 | _ => box []
adamc@875 1146 in
adamc@875 1147 box [case t of
adamc@875 1148 Nullable t => box [string "my_bool in_is_null",
adamc@875 1149 string (Int.toString i),
adamc@875 1150 string ";",
adamc@875 1151 newline,
adamc@875 1152 buffers t]
adamc@875 1153 | _ => buffers t,
adamc@875 1154 newline]
adamc@875 1155 end) inputs,
adamc@875 1156 string "MYSQL_STMT *stmt = conn->p",
adamc@875 1157 string (Int.toString id),
adamc@875 1158 string ";",
adamc@875 1159 newline,
adamc@875 1160 newline,
adamc@875 1161
adamc@878 1162 string "if (stmt == NULL) {",
adamc@878 1163 newline,
adamc@878 1164 box [string "stmt = mysql_stmt_init(conn->conn);",
adamc@878 1165 newline,
adamc@878 1166 string "if (stmt == NULL) uw_error(ctx, FATAL, \"Out of memory allocating prepared statement\");",
adamc@878 1167 newline,
adamc@878 1168 string "if (mysql_stmt_prepare(stmt, \"",
adamc@878 1169 string (String.toString dml),
adamc@878 1170 string "\", ",
adamc@878 1171 string (Int.toString (size dml)),
adamc@878 1172 string ")) {",
adamc@878 1173 newline,
adamc@878 1174 box [string "char msg[1024];",
adamc@878 1175 newline,
adamc@878 1176 string "strncpy(msg, mysql_stmt_error(stmt), 1024);",
adamc@878 1177 newline,
adamc@878 1178 string "msg[1023] = 0;",
adamc@878 1179 newline,
adamc@878 1180 string "uw_error(ctx, FATAL, \"Error preparing statement: %s\", msg);",
adamc@878 1181 newline],
adamc@878 1182 string "}",
adamc@878 1183 newline,
adamc@878 1184 string "conn->p",
adamc@878 1185 string (Int.toString id),
adamc@878 1186 string " = stmt;",
adamc@878 1187 newline],
adamc@878 1188 string "}",
adamc@878 1189 newline,
adamc@878 1190 newline,
adamc@878 1191
adamc@875 1192 string "memset(in, 0, sizeof in);",
adamc@875 1193 newline,
adamc@875 1194 p_list_sepi (box []) (fn i => fn t =>
adamc@875 1195 let
adamc@875 1196 fun buffers t =
adamc@875 1197 case t of
adamc@875 1198 String => box [string "in[",
adamc@875 1199 string (Int.toString i),
adamc@875 1200 string "].buffer = arg",
adamc@875 1201 string (Int.toString (i + 1)),
adamc@875 1202 string ";",
adamc@875 1203 newline,
adamc@875 1204 string "in_length",
adamc@875 1205 string (Int.toString i),
adamc@875 1206 string "= in[",
adamc@875 1207 string (Int.toString i),
adamc@875 1208 string "].buffer_length = strlen(arg",
adamc@875 1209 string (Int.toString (i + 1)),
adamc@875 1210 string ");",
adamc@875 1211 newline,
adamc@875 1212 string "in[",
adamc@875 1213 string (Int.toString i),
adamc@875 1214 string "].length = &in_length",
adamc@875 1215 string (Int.toString i),
adamc@875 1216 string ";",
adamc@875 1217 newline]
adamc@875 1218 | Blob => box [string "in[",
adamc@875 1219 string (Int.toString i),
adamc@875 1220 string "].buffer = arg",
adamc@875 1221 string (Int.toString (i + 1)),
adamc@875 1222 string ".data;",
adamc@875 1223 newline,
adamc@875 1224 string "in_length",
adamc@875 1225 string (Int.toString i),
adamc@875 1226 string "= in[",
adamc@875 1227 string (Int.toString i),
adamc@875 1228 string "].buffer_length = arg",
adamc@875 1229 string (Int.toString (i + 1)),
adamc@875 1230 string ".size;",
adamc@875 1231 newline,
adamc@875 1232 string "in[",
adamc@875 1233 string (Int.toString i),
adamc@875 1234 string "].length = &in_length",
adamc@875 1235 string (Int.toString i),
adamc@875 1236 string ";",
adamc@875 1237 newline]
adamc@875 1238 | Time =>
adamc@875 1239 let
adamc@875 1240 fun oneField dst src =
adamc@875 1241 box [string "in_buffer",
adamc@875 1242 string (Int.toString i),
adamc@875 1243 string ".",
adamc@875 1244 string dst,
adamc@875 1245 string " = tms.tm_",
adamc@875 1246 string src,
adamc@875 1247 string ";",
adamc@875 1248 newline]
adamc@875 1249 in
adamc@875 1250 box [string "({",
adamc@875 1251 newline,
adamc@875 1252 string "struct tm tms;",
adamc@875 1253 newline,
adamc@875 1254 string "if (localtime_r(&arg",
adamc@875 1255 string (Int.toString (i + 1)),
adamc@875 1256 string ", &tm) == NULL) uw_error(\"",
adamc@875 1257 string (ErrorMsg.spanToString loc),
adamc@875 1258 string ": error converting to MySQL time\");",
adamc@875 1259 newline,
adamc@875 1260 oneField "year" "year",
adamc@875 1261 oneField "month" "mon",
adamc@875 1262 oneField "day" "mday",
adamc@875 1263 oneField "hour" "hour",
adamc@875 1264 oneField "minute" "min",
adamc@875 1265 oneField "second" "sec",
adamc@875 1266 newline,
adamc@875 1267 string "in[",
adamc@875 1268 string (Int.toString i),
adamc@875 1269 string "].buffer = &in_buffer",
adamc@875 1270 string (Int.toString i),
adamc@875 1271 string ";",
adamc@875 1272 newline]
adamc@875 1273 end
adamc@875 1274
adamc@875 1275 | _ => box [string "in[",
adamc@875 1276 string (Int.toString i),
adamc@875 1277 string "].buffer = &arg",
adamc@875 1278 string (Int.toString (i + 1)),
adamc@875 1279 string ";",
adamc@875 1280 newline]
adamc@875 1281 in
adamc@875 1282 box [string "in[",
adamc@875 1283 string (Int.toString i),
adamc@875 1284 string "].buffer_type = ",
adamc@875 1285 string (p_buffer_type t),
adamc@875 1286 string ";",
adamc@875 1287 newline,
adamc@875 1288
adamc@875 1289 case t of
adamc@875 1290 Nullable t => box [string "in[",
adamc@875 1291 string (Int.toString i),
adamc@875 1292 string "].is_null = &in_is_null",
adamc@875 1293 string (Int.toString i),
adamc@875 1294 string ";",
adamc@875 1295 newline,
adamc@875 1296 string "if (arg",
adamc@875 1297 string (Int.toString (i + 1)),
adamc@875 1298 string " == NULL) {",
adamc@875 1299 newline,
adamc@875 1300 box [string "in_is_null",
adamc@875 1301 string (Int.toString i),
adamc@875 1302 string " = 1;",
adamc@875 1303 newline],
adamc@875 1304 string "} else {",
adamc@875 1305 box [case t of
adamc@875 1306 String => box []
adamc@875 1307 | _ =>
adamc@875 1308 box [string (p_sql_ctype t),
adamc@875 1309 space,
adamc@876 1310 string "tmp = *arg",
adamc@876 1311 string (Int.toString (i + 1)),
adamc@876 1312 string ";",
adamc@876 1313 newline,
adamc@876 1314 string (p_sql_ctype t),
adamc@876 1315 space,
adamc@875 1316 string "arg",
adamc@875 1317 string (Int.toString (i + 1)),
adamc@876 1318 string " = tmp;",
adamc@875 1319 newline],
adamc@875 1320 string "in_is_null",
adamc@875 1321 string (Int.toString i),
adamc@875 1322 string " = 0;",
adamc@875 1323 newline,
adamc@875 1324 buffers t,
adamc@876 1325 newline],
adamc@876 1326 string "}",
adamc@876 1327 newline]
adamc@875 1328
adamc@875 1329 | _ => buffers t,
adamc@875 1330 newline]
adamc@875 1331 end) inputs,
adamc@875 1332 newline,
adamc@875 1333
adamc@875 1334 string "if (mysql_stmt_bind_param(stmt, in)) uw_error(ctx, FATAL, \"",
adamc@875 1335 string (ErrorMsg.spanToString loc),
adamc@875 1336 string ": error binding parameters\");",
adamc@875 1337 newline,
adamc@875 1338
adamc@875 1339 dmlCommon {loc = loc, dml = box [string "\"",
adamc@875 1340 string (String.toString dml),
adamc@875 1341 string "\""]}]
adamc@875 1342
adamc@878 1343 fun nextval {loc, seqE, seqName} =
adamc@878 1344 box [string "uw_conn *conn = uw_get_db(ctx);",
adamc@878 1345 newline,
adamc@878 1346 string "char *insert = ",
adamc@878 1347 case seqName of
adamc@878 1348 SOME s => string ("\"INSERT INTO " ^ s ^ " VALUES ()\"")
adamc@878 1349 | NONE => box [string "uw_Basis_strcat(ctx, \"INSERT INTO \", uw_Basis_strcat(ctx, ",
adamc@878 1350 seqE,
adamc@878 1351 string ", \" VALUES ()\"))"],
adamc@878 1352 string ";",
adamc@878 1353 newline,
adamc@878 1354 string "char *delete = ",
adamc@878 1355 case seqName of
adamc@878 1356 SOME s => string ("\"DELETE FROM " ^ s ^ "\"")
adamc@878 1357 | NONE => box [string "uw_Basis_strcat(ctx, \"DELETE FROM \", ",
adamc@878 1358 seqE,
adamc@878 1359 string ")"],
adamc@878 1360 string ";",
adamc@878 1361 newline,
adamc@878 1362 newline,
adamc@878 1363
adamc@878 1364 string "if (mysql_query(conn->conn, insert)) uw_error(ctx, FATAL, \"'nextval' INSERT failed\");",
adamc@878 1365 newline,
adamc@878 1366 string "n = mysql_insert_id(conn->conn);",
adamc@878 1367 newline,
adamc@878 1368 string "if (mysql_query(conn->conn, delete)) uw_error(ctx, FATAL, \"'nextval' DELETE failed\");",
adamc@878 1369 newline]
adamc@878 1370
adamc@878 1371 fun nextvalPrepared _ = raise Fail "MySQL.nextvalPrepared called"
adamc@867 1372
adamc@877 1373 fun sqlifyString s = "'" ^ String.translate (fn #"'" => "\\'"
adamc@877 1374 | #"\\" => "\\\\"
adamc@877 1375 | ch =>
adamc@877 1376 if Char.isPrint ch then
adamc@877 1377 str ch
adamc@877 1378 else
adamc@877 1379 (ErrorMsg.error
adamc@877 1380 "Non-printing character found in SQL string literal";
adamc@877 1381 ""))
adamc@877 1382 (String.toString s) ^ "'"
adamc@874 1383
adamc@877 1384 fun p_cast (s, _) = s
adamc@874 1385
adamc@874 1386 fun p_blank _ = "?"
adamc@874 1387
adamc@866 1388 val () = addDbms {name = "mysql",
adamc@866 1389 header = "mysql/mysql.h",
adamc@866 1390 link = "-lmysqlclient",
adamc@867 1391 init = init,
adamc@873 1392 p_sql_type = p_sql_type,
adamc@867 1393 query = query,
adamc@868 1394 queryPrepared = queryPrepared,
adamc@868 1395 dml = dml,
adamc@869 1396 dmlPrepared = dmlPrepared,
adamc@869 1397 nextval = nextval,
adamc@874 1398 nextvalPrepared = nextvalPrepared,
adamc@874 1399 sqlifyString = sqlifyString,
adamc@874 1400 p_cast = p_cast,
adamc@874 1401 p_blank = p_blank,
adamc@877 1402 supportsDeleteAs = false,
adamc@877 1403 createSequence = fn s => "CREATE TABLE " ^ s ^ " (id INTEGER PRIMARY KEY AUTO_INCREMENT)",
adamc@878 1404 textKeysNeedLengths = true,
adamc@878 1405 supportsNextval = false}
adamc@866 1406
adamc@866 1407 end