comparison src/sqlite.sml @ 1014:ea9f03ac2710

Chars working with SQLite
author Adam Chlipala <adamc@hcoop.net>
date Thu, 22 Oct 2009 17:36:30 -0400
parents 16f7cb0891b6
children b2311dfb3158
comparison
equal deleted inserted replaced
1013:d9483301ff0e 1014:ea9f03ac2710
34 fun p_sql_type t = 34 fun p_sql_type t =
35 case t of 35 case t of
36 Int => "integer" 36 Int => "integer"
37 | Float => "real" 37 | Float => "real"
38 | String => "text" 38 | String => "text"
39 | Char => "integer" 39 | Char => "text"
40 | Bool => "integer" 40 | Bool => "integer"
41 | Time => "text" 41 | Time => "text"
42 | Blob => "blob" 42 | Blob => "blob"
43 | Channel => "integer" 43 | Channel => "integer"
44 | Client => "integer" 44 | Client => "integer"
368 case t of 368 case t of
369 Int => box [string "sqlite3_column_int64(stmt, ", string (Int.toString i), string ")"] 369 Int => box [string "sqlite3_column_int64(stmt, ", string (Int.toString i), string ")"]
370 | Float => box [string "sqlite3_column_double(stmt, ", string (Int.toString i), string ")"] 370 | Float => box [string "sqlite3_column_double(stmt, ", string (Int.toString i), string ")"]
371 | String => 371 | String =>
372 if wontLeakStrings then 372 if wontLeakStrings then
373 box [string "sqlite3_column_text(stmt, ", string (Int.toString i), string ")"] 373 box [string "(uw_Basis_string)sqlite3_column_text(stmt, ", string (Int.toString i), string ")"]
374 else 374 else
375 box [string "uw_strdup(ctx, sqlite3_column_text(stmt, ", string (Int.toString i), string "))"] 375 box [string "uw_strdup(ctx, (uw_Basis_string)sqlite3_column_text(stmt, ", string (Int.toString i), string "))"]
376 | Char => box [string "sqlite3_column_int(stmt, ", string (Int.toString i), string ")"] 376 | Char => box [string "sqlite3_column_text(stmt, ", string (Int.toString i), string ")[0]"]
377 | Bool => box [string "(uw_Basis_bool)sqlite3_column_int(stmt, ", string (Int.toString i), string ")"] 377 | Bool => box [string "(uw_Basis_bool)sqlite3_column_int(stmt, ", string (Int.toString i), string ")"]
378 | Time => box [string "uw_Basis_stringToTime_error(ctx, sqlite3_column_text(stmt, ", string (Int.toString i), string "))"] 378 | Time => box [string "uw_Basis_stringToTime_error(ctx, (uw_Basis_string)sqlite3_column_text(stmt, ", string (Int.toString i), string "))"]
379 | Blob => box [string "({", 379 | Blob => box [string "({",
380 newline, 380 newline,
381 string "char *data = (char *)sqlite3_column_blob(stmt, ", 381 string "char *data = (char *)sqlite3_column_blob(stmt, ",
382 string (Int.toString i), 382 string (Int.toString i),
383 string ");", 383 string ");",
504 queryCommon {loc = loc, cols = cols, doCols = doCols, query = string "query"}, 504 queryCommon {loc = loc, cols = cols, doCols = doCols, query = string "query"},
505 505
506 string "uw_pop_cleanup(ctx);", 506 string "uw_pop_cleanup(ctx);",
507 newline] 507 newline]
508 508
509 val p_pre_inputs =
510 p_list_sepi (box [])
511 (fn i => fn t =>
512 case t of
513 Char => box [string "char arg",
514 string (Int.toString (i + 1)),
515 string "s = {arg",
516 string (Int.toString (i + 1)),
517 string ", 0};",
518 newline]
519 | _ => box [])
520
509 fun p_inputs loc = 521 fun p_inputs loc =
510 p_list_sepi (box []) 522 p_list_sepi (box [])
511 (fn i => fn t => 523 (fn i => fn t =>
512 let 524 let
513 fun bind (t, arg) = 525 fun bind (t, arg) =
519 string ")"] 531 string ")"]
520 | Float => box [string "sqlite3_bind_double(stmt, ", 532 | Float => box [string "sqlite3_bind_double(stmt, ",
521 string (Int.toString (i + 1)), 533 string (Int.toString (i + 1)),
522 string ", ", 534 string ", ",
523 arg, 535 arg,
524 string ")"] 536 string ")"]
525 | String => box [string "sqlite3_bind_text(stmt, ", 537 | String => box [string "sqlite3_bind_text(stmt, ",
526 string (Int.toString (i + 1)), 538 string (Int.toString (i + 1)),
527 string ", ", 539 string ", ",
528 arg, 540 arg,
529 string ", -1, SQLITE_TRANSIENT)"] 541 string ", -1, SQLITE_TRANSIENT)"]
530 | Char => box [string "sqlite3_bind_int(stmt, ", 542 | Char => box [string "sqlite3_bind_text(stmt, ",
531 string (Int.toString (i + 1)), 543 string (Int.toString (i + 1)),
532 string ", ", 544 string ", ",
533 arg, 545 arg,
534 string ")"] 546 string "s, -1, SQLITE_TRANSIENT)"]
535 | Bool => box [string "sqlite3_bind_int(stmt, ", 547 | Bool => box [string "sqlite3_bind_int(stmt, ",
536 string (Int.toString (i + 1)), 548 string (Int.toString (i + 1)),
537 string ", ", 549 string ", ",
538 arg, 550 arg,
539 string ")"] 551 string ")"]
582 end) 594 end)
583 595
584 fun queryPrepared {loc, id, query, inputs, cols, doCols, nested} = 596 fun queryPrepared {loc, id, query, inputs, cols, doCols, nested} =
585 box [string "uw_conn *conn = uw_get_db(ctx);", 597 box [string "uw_conn *conn = uw_get_db(ctx);",
586 newline, 598 newline,
599 p_pre_inputs inputs,
587 if nested then 600 if nested then
588 box [string "sqlite3_stmt *stmt;", 601 box [string "sqlite3_stmt *stmt;",
589 newline] 602 newline]
590 else 603 else
591 box [string "sqlite3_stmt *stmt = conn->p", 604 box [string "sqlite3_stmt *stmt = conn->p",
674 newline] 687 newline]
675 688
676 fun dmlPrepared {loc, id, dml, inputs} = 689 fun dmlPrepared {loc, id, dml, inputs} =
677 box [string "uw_conn *conn = uw_get_db(ctx);", 690 box [string "uw_conn *conn = uw_get_db(ctx);",
678 newline, 691 newline,
692 p_pre_inputs inputs,
679 string "sqlite3_stmt *stmt = conn->p", 693 string "sqlite3_stmt *stmt = conn->p",
680 string (Int.toString id), 694 string (Int.toString id),
681 string ";", 695 string ";",
682 newline, 696 newline,
683 newline, 697 newline,
777 createSequence = fn s => "CREATE TABLE " ^ s ^ " (id INTEGER PRIMARY KEY AUTOINCREMENT)", 791 createSequence = fn s => "CREATE TABLE " ^ s ^ " (id INTEGER PRIMARY KEY AUTOINCREMENT)",
778 textKeysNeedLengths = false, 792 textKeysNeedLengths = false,
779 supportsNextval = false, 793 supportsNextval = false,
780 supportsNestedPrepared = false, 794 supportsNestedPrepared = false,
781 sqlPrefix = "", 795 sqlPrefix = "",
782 supportsOctetLength = false} 796 supportsOctetLength = false,
797 trueString = "1",
798 falseString = "0"}
783 799
784 end 800 end