diff src/postgres.sml @ 873:41971801b62d

MySQL query gets up to C linking
author Adam Chlipala <adamc@hcoop.net>
date Sun, 12 Jul 2009 13:16:05 -0400
parents 9654bce27cff
children 3c7b48040dcf
line wrap: on
line diff
--- a/src/postgres.sml	Tue Jun 30 16:17:32 2009 -0400
+++ b/src/postgres.sml	Sun Jul 12 13:16:05 2009 -0400
@@ -34,6 +34,18 @@
 val ident = String.translate (fn #"'" => "PRIME"
                                | ch => str ch)
 
+fun p_sql_type t =
+    case t of
+        Int => "int8"
+      | Float => "float8"
+      | String => "text"
+      | Bool => "bool"
+      | Time => "timestamp"
+      | Blob => "bytea"
+      | Channel => "int8"
+      | Client => "int4"
+      | Nullable t => p_sql_type t
+
 fun p_sql_type_base t =
     case t of
         Int => "bigint"
@@ -540,7 +552,7 @@
         getter t
     end
 
-fun queryCommon {loc, query, numCols, doCols} =
+fun queryCommon {loc, query, cols, doCols} =
     box [string "int n, i;",
          newline,
          newline,
@@ -564,7 +576,7 @@
          newline,
 
          string "if (PQnfields(res) != ",
-         string (Int.toString numCols),
+         string (Int.toString (length cols)),
          string ") {",
          newline,
          box [string "int nf = PQnfields(res);",
@@ -574,7 +586,7 @@
               string "uw_error(ctx, FATAL, \"",
               string (ErrorMsg.spanToString loc),
               string ": Query returned %d columns instead of ",
-              string (Int.toString numCols),
+              string (Int.toString (length cols)),
               string ":\\n%s\\n%s\", nf, ",
               query,
               string ", PQerrorMessage(conn));",
@@ -598,13 +610,13 @@
          string "uw_pop_cleanup(ctx);",
          newline]    
 
-fun query {loc, numCols, doCols} =
+fun query {loc, cols, doCols} =
     box [string "PGconn *conn = uw_get_db(ctx);",
          newline,
          string "PGresult *res = PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);",
          newline,
          newline,
-         queryCommon {loc = loc, numCols = numCols, doCols = doCols, query = string "query"}]
+         queryCommon {loc = loc, cols = cols, doCols = doCols, query = string "query"}]
 
 fun p_ensql t e =
     case t of
@@ -623,7 +635,7 @@
                            p_ensql t (box [string "(*", e, string ")"]),
                            string ")"]
 
-fun queryPrepared {loc, id, query, inputs, numCols, doCols} =
+fun queryPrepared {loc, id, query, inputs, cols, doCols} =
     box [string "PGconn *conn = uw_get_db(ctx);",
          newline,
          string "const int paramFormats[] = { ",
@@ -662,9 +674,9 @@
                   string ", NULL, paramValues, paramLengths, paramFormats, 0);"],
          newline,
          newline,
-         queryCommon {loc = loc, numCols = numCols, doCols = doCols, query = box [string "\"",
-                                                                                  string (String.toString query),
-                                                                                  string "\""]}]
+         queryCommon {loc = loc, cols = cols, doCols = doCols, query = box [string "\"",
+                                                                            string (String.toString query),
+                                                                            string "\""]}]
 
 fun dmlCommon {loc, dml} =
     box [string "if (res == NULL) uw_error(ctx, FATAL, \"Out of memory allocating DML result.\");",
@@ -821,6 +833,7 @@
                   link = "-lpq",
                   global_init = box [string "void uw_client_init() { }",
                                      newline],
+                  p_sql_type = p_sql_type,
                   init = init,
                   query = query,
                   queryPrepared = queryPrepared,