comparison src/sqlite.sml @ 886:5805fa825fe8

Most of demo working with SQLite
author Adam Chlipala <adamc@hcoop.net>
date Fri, 17 Jul 2009 17:03:37 -0400
parents e6070333d8a8
children 9eb479691d1c
comparison
equal deleted inserted replaced
885:e6070333d8a8 886:5805fa825fe8
230 string (String.toString s), 230 string (String.toString s),
231 string "\", -1, &conn->p", 231 string "\", -1, &conn->p",
232 string (Int.toString i), 232 string (Int.toString i),
233 string ", NULL) != SQLITE_OK) {", 233 string ", NULL) != SQLITE_OK) {",
234 newline, 234 newline,
235 uhoh false ("Error preparing statement: " 235 box [string "char msg[1024];",
236 ^ String.toString s) [], 236 newline,
237 string "strncpy(msg, sqlite3_errmsg(conn->conn), 1024);",
238 newline,
239 string "msg[1023] = 0;",
240 newline,
241 uhoh false ("Error preparing statement: "
242 ^ String.toString s ^ "\\n%s") ["msg"]],
237 string "}", 243 string "}",
238 newline] 244 newline]
239 end) 245 end)
240 ss, 246 ss,
241 247
377 string "), data};", 383 string "), data};",
378 newline, 384 newline,
379 string "b;", 385 string "b;",
380 newline, 386 newline,
381 string "})"] 387 string "})"]
382 | Channel => box [string "sqlite3_column_int64(stmt, ", string (Int.toString i), string ")"] 388 | Channel => box [string "({",
389 newline,
390 string "sqlite3_int64 n = sqlite3_column_int64(stmt, ",
391 string (Int.toString i),
392 string ");",
393 newline,
394 string "uw_Basis_channel ch = {n >> 32, n & 0xFFFFFFFF};",
395 newline,
396 string "ch;",
397 newline,
398 string "})"]
383 | Client => box [string "sqlite3_column_int(stmt, ", string (Int.toString i), string ")"] 399 | Client => box [string "sqlite3_column_int(stmt, ", string (Int.toString i), string ")"]
384 400
385 | Nullable _ => raise Fail "Postgres: Recursive Nullable" 401 | Nullable _ => raise Fail "Postgres: Recursive Nullable"
386 402
387 fun getter t = 403 fun getter t =
467 newline] 483 newline]
468 484
469 fun query {loc, cols, doCols} = 485 fun query {loc, cols, doCols} =
470 box [string "uw_conn *conn = uw_get_db(ctx);", 486 box [string "uw_conn *conn = uw_get_db(ctx);",
471 newline, 487 newline,
472 string "sqlite3 *stmt;", 488 string "sqlite3_stmt *stmt;",
473 newline, 489 newline,
474 newline, 490 newline,
475 string "if (sqlite3_prepare_v2(conn->conn, query, -1, &stmt, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"Error preparing statement: %s\\n%s\", sqlite3_errmsg(conn->conn));", 491 string "if (sqlite3_prepare_v2(conn->conn, query, -1, &stmt, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"Error preparing statement: %s\\n%s\", sqlite3_errmsg(conn->conn));",
476 newline, 492 newline,
477 newline, 493 newline,
520 string ", ", 536 string ", ",
521 arg, 537 arg,
522 string ".data, ", 538 string ".data, ",
523 arg, 539 arg,
524 string ".size, SQLITE_TRANSIENT"] 540 string ".size, SQLITE_TRANSIENT"]
525 | Channel => box [string "sqlite_bind_int64(stmt, ", 541 | Channel => box [string "sqlite3_bind_int64(stmt, ",
526 string (Int.toString (i + 1)), 542 string (Int.toString (i + 1)),
527 string ", ", 543 string ", ((sqlite3_int64)",
528 arg, 544 arg,
529 string ")"] 545 string ".cli << 32) | ",
546 arg,
547 string ".chn)"]
530 | Client => box [string "sqlite3_bind_int(stmt, ", 548 | Client => box [string "sqlite3_bind_int(stmt, ",
531 string (Int.toString (i + 1)), 549 string (Int.toString (i + 1)),
532 string ", ", 550 string ", ",
533 arg, 551 arg,
534 string ")"] 552 string ")"]
627 newline] 645 newline]
628 646
629 fun dml loc = 647 fun dml loc =
630 box [string "uw_conn *conn = uw_get_db(ctx);", 648 box [string "uw_conn *conn = uw_get_db(ctx);",
631 newline, 649 newline,
632 string "sqlite3 *stmt;", 650 string "sqlite3_stmt *stmt;",
633 newline, 651 newline,
634 newline, 652 newline,
635 string "if (sqlite3_prepare_v2(conn->conn, dml, -1, &stmt, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"Error preparing statement: %s\\n%s\", dml, sqlite3_errmsg(conn->conn));", 653 string "if (sqlite3_prepare_v2(conn->conn, dml, -1, &stmt, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"Error preparing statement: %s\\n%s\", dml, sqlite3_errmsg(conn->conn));",
636 newline, 654 newline,
637 newline, 655 newline,
688 fun nextval {loc, seqE, seqName} = 706 fun nextval {loc, seqE, seqName} =
689 box [string "uw_conn *conn = uw_get_db(ctx);", 707 box [string "uw_conn *conn = uw_get_db(ctx);",
690 newline, 708 newline,
691 string "char *insert = ", 709 string "char *insert = ",
692 case seqName of 710 case seqName of
693 SOME s => string ("\"INSERT INTO " ^ s ^ " VALUES ()\"") 711 SOME s => string ("\"INSERT INTO " ^ s ^ " VALUES (NULL)\"")
694 | NONE => box [string "uw_Basis_strcat(ctx, \"INSERT INTO \", uw_Basis_strcat(ctx, ", 712 | NONE => box [string "uw_Basis_strcat(ctx, \"INSERT INTO \", uw_Basis_strcat(ctx, ",
695 seqE, 713 seqE,
696 string ", \" VALUES ()\"))"], 714 string ", \" VALUES ()\"))"],
697 string ";", 715 string ";",
698 newline, 716 newline,
704 string ")"], 722 string ")"],
705 string ";", 723 string ";",
706 newline, 724 newline,
707 newline, 725 newline,
708 726
709 string "if (sqlite3_exec(conn->conn, insert, NULL, NULL, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"'nextval' INSERT failed\");", 727 string "if (sqlite3_exec(conn->conn, insert, NULL, NULL, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"'nextval' INSERT failed: %s\", sqlite3_errmsg(conn->conn));",
710 newline, 728 newline,
711 string "n = sqlite3_last_insert_rowid(conn->conn);", 729 string "n = sqlite3_last_insert_rowid(conn->conn);",
712 newline, 730 newline,
713 string "if (sqlite3_exec(conn->conn, delete, NULL, NULL, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"'nextval' DELETE failed\");", 731 string "if (sqlite3_exec(conn->conn, delete, NULL, NULL, NULL) != SQLITE_OK) uw_error(ctx, FATAL, \"'nextval' DELETE failed: %s\", sqlite3_errmsg(conn->conn));",
714 newline] 732 newline]
715 733
716 fun nextvalPrepared _ = raise Fail "SQLite.nextvalPrepared called" 734 fun nextvalPrepared _ = raise Fail "SQLite.nextvalPrepared called"
717 735
718 fun sqlifyString s = "'" ^ String.translate (fn #"'" => "''" 736 fun sqlifyString s = "'" ^ String.translate (fn #"'" => "''"
742 nextvalPrepared = nextvalPrepared, 760 nextvalPrepared = nextvalPrepared,
743 sqlifyString = sqlifyString, 761 sqlifyString = sqlifyString,
744 p_cast = p_cast, 762 p_cast = p_cast,
745 p_blank = p_blank, 763 p_blank = p_blank,
746 supportsDeleteAs = false, 764 supportsDeleteAs = false,
747 createSequence = fn s => "CREATE TABLE " ^ s ^ " (id INTEGER PRIMARY KEY AUTO INCREMENT)", 765 supportsUpdateAs = false,
766 createSequence = fn s => "CREATE TABLE " ^ s ^ " (id INTEGER PRIMARY KEY AUTOINCREMENT)",
748 textKeysNeedLengths = false, 767 textKeysNeedLengths = false,
749 supportsNextval = false, 768 supportsNextval = false,
750 supportsNestedPrepared = false, 769 supportsNestedPrepared = false,
751 sqlPrefix = ""} 770 sqlPrefix = ""}
752 771