changeset 1014:ea9f03ac2710

Chars working with SQLite
author Adam Chlipala <adamc@hcoop.net>
date Thu, 22 Oct 2009 17:36:30 -0400
parents d9483301ff0e
children e47303e5d73d
files src/mono_opt.sml src/monoize.sml src/mysql.sml src/postgres.sml src/settings.sig src/settings.sml src/sqlite.sml
diffstat 7 files changed, 47 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/mono_opt.sml	Thu Oct 22 17:04:37 2009 -0400
+++ b/src/mono_opt.sml	Thu Oct 22 17:36:30 2009 -0400
@@ -323,9 +323,9 @@
       | EFfiApp ("Basis", "sqlifyBool", [b as (_, loc)]) =>
         optExp (ECase (b,
                        [((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "True", arg = NONE}, NONE), loc),
-                         (EPrim (Prim.String "TRUE"), loc)),
+                         (EPrim (Prim.String (#trueString (Settings.currentDbms ()))), loc)),
                         ((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "False", arg = NONE}, NONE), loc),
-                         (EPrim (Prim.String "FALSE"), loc))],
+                         (EPrim (Prim.String (#falseString (Settings.currentDbms ()))), loc))],
                        {disc = (TFfi ("Basis", "bool"), loc),
                         result = (TFfi ("Basis", "string"), loc)}), loc)
       | EFfiApp ("Basis", "sqlifyString", [(EPrim (Prim.String n), _)]) =>
--- a/src/monoize.sml	Thu Oct 22 17:04:37 2009 -0400
+++ b/src/monoize.sml	Thu Oct 22 17:36:30 2009 -0400
@@ -1876,7 +1876,8 @@
                                                ],
 
                                            (L'.ECase (gf "Having",
-                                                      [((L'.PPrim (Prim.String "TRUE"), loc),
+                                                      [((L'.PPrim (Prim.String
+                                                                       (#trueString (Settings.currentDbms ()))), loc),
                                                         sc ""),
                                                        ((L'.PWild, loc),
                                                         strcat [sc " HAVING ", gf "Having"])],
--- a/src/mysql.sml	Thu Oct 22 17:04:37 2009 -0400
+++ b/src/mysql.sml	Thu Oct 22 17:36:30 2009 -0400
@@ -1539,6 +1539,8 @@
                   supportsNextval = false,
                   supportsNestedPrepared = false,
                   sqlPrefix = "SET storage_engine=InnoDB;\n\n",
-                  supportsOctetLength = true}
+                  supportsOctetLength = true,
+                  trueString = "TRUE",
+                  falseString = "FALSE"}
 
 end
--- a/src/postgres.sml	Thu Oct 22 17:04:37 2009 -0400
+++ b/src/postgres.sml	Thu Oct 22 17:36:30 2009 -0400
@@ -902,7 +902,9 @@
                   supportsNextval = true,
                   supportsNestedPrepared = true,
                   sqlPrefix = "",
-                  supportsOctetLength = true}
+                  supportsOctetLength = true,
+                  trueString = "TRUE",
+                  falseString = "FALSE"}
 
 val () = setDbms "postgres"
 
--- a/src/settings.sig	Thu Oct 22 17:04:37 2009 -0400
+++ b/src/settings.sig	Thu Oct 22 17:36:30 2009 -0400
@@ -157,7 +157,9 @@
          supportsNextval : bool,
          supportsNestedPrepared : bool,
          sqlPrefix : string,
-         supportsOctetLength : bool
+         supportsOctetLength : bool,
+         trueString : string,
+         falseString : string
     }
 
     val addDbms : dbms -> unit
--- a/src/settings.sml	Thu Oct 22 17:04:37 2009 -0400
+++ b/src/settings.sml	Thu Oct 22 17:36:30 2009 -0400
@@ -352,7 +352,9 @@
      supportsNextval : bool,
      supportsNestedPrepared : bool,
      sqlPrefix : string,
-     supportsOctetLength : bool
+     supportsOctetLength : bool,
+     trueString : string,
+     falseString : string
 }
 
 val dbmses = ref ([] : dbms list)
@@ -377,7 +379,9 @@
                   supportsNextval = false,
                   supportsNestedPrepared = false,
                   sqlPrefix = "",
-                  supportsOctetLength = false} : dbms)
+                  supportsOctetLength = false,
+                  trueString = "",
+                  falseString = ""} : dbms)
 
 fun addDbms v = dbmses := v :: !dbmses
 fun setDbms s =
--- a/src/sqlite.sml	Thu Oct 22 17:04:37 2009 -0400
+++ b/src/sqlite.sml	Thu Oct 22 17:36:30 2009 -0400
@@ -36,7 +36,7 @@
         Int => "integer"
       | Float => "real"
       | String => "text"
-      | Char => "integer"
+      | Char => "text"
       | Bool => "integer"
       | Time => "text"
       | Blob => "blob"
@@ -370,12 +370,12 @@
               | Float => box [string "sqlite3_column_double(stmt, ", string (Int.toString i), string ")"]
               | String =>
                 if wontLeakStrings then
-                    box [string "sqlite3_column_text(stmt, ", string (Int.toString i), string ")"]
+                    box [string "(uw_Basis_string)sqlite3_column_text(stmt, ", string (Int.toString i), string ")"]
                 else
-                    box [string "uw_strdup(ctx, sqlite3_column_text(stmt, ", string (Int.toString i), string "))"]
-              | Char => box [string "sqlite3_column_int(stmt, ", string (Int.toString i), string ")"]
+                    box [string "uw_strdup(ctx, (uw_Basis_string)sqlite3_column_text(stmt, ", string (Int.toString i), string "))"]
+              | Char => box [string "sqlite3_column_text(stmt, ", string (Int.toString i), string ")[0]"]
               | Bool => box [string "(uw_Basis_bool)sqlite3_column_int(stmt, ", string (Int.toString i), string ")"]
-              | Time => box [string "uw_Basis_stringToTime_error(ctx, sqlite3_column_text(stmt, ", string (Int.toString i), string "))"]
+              | Time => box [string "uw_Basis_stringToTime_error(ctx, (uw_Basis_string)sqlite3_column_text(stmt, ", string (Int.toString i), string "))"]
               | Blob => box [string "({",
                              newline,
                              string "char *data = (char *)sqlite3_column_blob(stmt, ",
@@ -506,6 +506,18 @@
          string "uw_pop_cleanup(ctx);",
          newline]
 
+val p_pre_inputs =
+    p_list_sepi (box [])
+                (fn i => fn t =>
+                            case t of
+                                Char => box [string "char arg",
+                                             string (Int.toString (i + 1)),
+                                             string "s = {arg",
+                                             string (Int.toString (i + 1)),
+                                             string ", 0};",
+                                             newline]
+                              | _ => box [])
+
 fun p_inputs loc =
     p_list_sepi (box [])
                 (fn i => fn t =>
@@ -521,17 +533,17 @@
                                                       string (Int.toString (i + 1)),
                                                       string ", ",
                                                       arg,
-                                                    string ")"]
+                                                      string ")"]
                                       | String => box [string "sqlite3_bind_text(stmt, ",
                                                        string (Int.toString (i + 1)),
                                                        string ", ",
                                                        arg,
                                                        string ", -1, SQLITE_TRANSIENT)"]
-                                      | Char => box [string "sqlite3_bind_int(stmt, ",
-                                                       string (Int.toString (i + 1)),
-                                                       string ", ",
-                                                       arg,
-                                                       string ")"]
+                                      | Char => box [string "sqlite3_bind_text(stmt, ",
+                                                     string (Int.toString (i + 1)),
+                                                     string ", ",
+                                                     arg,
+                                                     string "s, -1, SQLITE_TRANSIENT)"]
                                       | Bool => box [string "sqlite3_bind_int(stmt, ",
                                                      string (Int.toString (i + 1)),
                                                      string ", ",
@@ -584,6 +596,7 @@
 fun queryPrepared {loc, id, query, inputs, cols, doCols, nested} =
     box [string "uw_conn *conn = uw_get_db(ctx);",
          newline,
+         p_pre_inputs inputs,
          if nested then
              box [string "sqlite3_stmt *stmt;",
                   newline]
@@ -676,6 +689,7 @@
 fun dmlPrepared {loc, id, dml, inputs} =
     box [string "uw_conn *conn = uw_get_db(ctx);",
          newline,
+         p_pre_inputs inputs,
          string "sqlite3_stmt *stmt = conn->p",
          string (Int.toString id),
          string ";",
@@ -779,6 +793,8 @@
                   supportsNextval = false,
                   supportsNestedPrepared = false,
                   sqlPrefix = "",
-                  supportsOctetLength = false}
+                  supportsOctetLength = false,
+                  trueString = "1",
+                  falseString = "0"}
 
 end