changeset 295:1afa94582275

Reading ints and floats from SQL
author Adam Chlipala <adamc@hcoop.net>
date Sun, 07 Sep 2008 12:56:46 -0400
parents df02b09ff1ed
children 5dc11235129d
files include/types.h src/c/urweb.c src/cjr_print.sml tests/pquery.ur
diffstat 4 files changed, 18 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/include/types.h	Sun Sep 07 12:19:36 2008 -0400
+++ b/include/types.h	Sun Sep 07 12:56:46 2008 -0400
@@ -17,3 +17,7 @@
 
 
 typedef enum { SUCCESS, FATAL, BOUNDED_RETRY, UNLIMITED_RETRY } failure_kind;
+
+
+#define INTS_MAX 50
+#define FLOATS_MAX 100
--- a/src/c/urweb.c	Sun Sep 07 12:19:36 2008 -0400
+++ b/src/c/urweb.c	Sun Sep 07 12:56:46 2008 -0400
@@ -225,9 +225,6 @@
 }
 
 
-#define INTS_MAX 50
-#define FLOATS_MAX 100
-
 char *lw_Basis_attrifyInt(lw_context ctx, lw_Basis_int n) {
   char *result;
   int len;
--- a/src/cjr_print.sml	Sun Sep 07 12:19:36 2008 -0400
+++ b/src/cjr_print.sml	Sun Sep 07 12:56:46 2008 -0400
@@ -390,12 +390,10 @@
 
 fun p_unsql env (tAll as (t, loc)) e =
     case t of
-        TFfi ("Basis", "int") => box [string "*(lw_Basis_int *)", e]
-      | TFfi ("Basis", "float") => box [string "*(lw_Basis_float *)", e]
+        TFfi ("Basis", "int") => box [string "lw_Basis_stringToInt_error(ctx, ", e, string ")"]
+      | TFfi ("Basis", "float") => box [string "lw_Basis_stringToFloat_error(ctx, ", e, string ")"]
       | TFfi ("Basis", "string") => box [string "lw_Basis_strdup(ctx, ", e, string ")"]
-      | TFfi ("Basis", "bool") => box [string "(*(int *)",
-                                       e,
-                                       string " ? lw_Basis_True : lw_Basis_False)"]
+      | TFfi ("Basis", "bool") => box [string "lw_Basis_stringToBool_error(ctx, ", e, string ")"]
       | _ => (ErrorMsg.errorAt loc "Don't know how to unmarshal type from SQL";
               Print.eprefaces' [("Type", p_typ env tAll)];
               string "ERROR")
@@ -427,10 +425,10 @@
 
 fun p_ensql t e =
     case t of
-        Int => box [string "(char *)&", e]
-      | Float => box [string "(char *)&", e]
+        Int => box [string "lw_Basis_attrifyInt(ctx, ", e, string ")"]
+      | Float => box [string "lw_Basis_attrifyFloat(ctx, ", e, string ")"]
       | String => e
-      | Bool => box [string "lw_Basis_ensqlBool(", e, string ")"]
+      | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"]
 
 fun p_ensql_len t e =
     case t of
@@ -751,21 +749,6 @@
                               ets,
                               string " };",
                               newline,
-                              newline,
-
-                              string "const int paramLengths[] = { ",
-                              p_list_sepi (box [string ",", space])
-                              (fn i => fn (_, t) => p_ensql_len t (box [string "arg",
-                                                                        string (Int.toString (i + 1))]))
-                              ets,
-                              string " };",
-                              newline,
-                              newline,
-                              
-                              string "const static int paramFormats[] = { ",
-                              p_list_sep (box [string ",", space]) (fn _ => string "1") ets,
-                              string " };",
-                              newline,
                               newline]
                      end,
                  string "int n, i;",
@@ -781,12 +764,12 @@
                  newline,
                  string "PGresult *res = ",
                  case prepared of
-                     NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 1);"
+                     NONE => string "PQexecParams(conn, query, 0, NULL, NULL, NULL, NULL, 0);"
                    | SOME n => box [string "PQexecPrepared(conn, \"lw",
                                     string (Int.toString n),
                                     string "\", ",
                                     string (Int.toString (length (getPargs query))),
-                                    string ", paramValues, paramLengths, paramFormats, 1);"],
+                                    string ", paramValues, NULL, NULL, 0);"],
                  newline,
                  newline,
 
--- a/tests/pquery.ur	Sun Sep 07 12:19:36 2008 -0400
+++ b/tests/pquery.ur	Sun Sep 07 12:56:46 2008 -0400
@@ -1,11 +1,13 @@
 table t1 : {A : int, B : string, C : float}
 
 fun lookup (inp : {B : string}) =
-        s <- query (SELECT t1.B FROM t1 WHERE t1.B = {inp.B})
-                (fn fs _ => return fs.T1.B)
-                "Couldn't find it!";
+        s <- query (SELECT * FROM t1 WHERE t1.B = {inp.B})
+                (fn fs _ => return fs.T1)
+                {A = 0, B = "Couldn't find it!", C = 0.0};
         return <html><body>
-                Result: {cdata s}
+                A: {cdata (show _ s.A)}<br/>
+                B: {cdata (show _ s.B)}<br/>
+                C: {cdata (show _ s.C)}<br/>
         </body></html>
 
 fun main () : transaction page = return <html><body>