summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2008-09-07 12:56:46 -0400
committerGravatar Adam Chlipala <adamc@hcoop.net>2008-09-07 12:56:46 -0400
commit09b9ebf63c7a41dc40a702832b9d103e85dcf297 (patch)
treef15a5b9e7cd38634d3a0ba5f2ffb474848a61f57
parent20a746521647810909d3cd3806fa50c238c736ba (diff)
Reading ints and floats from SQL
-rw-r--r--include/types.h4
-rw-r--r--src/c/urweb.c3
-rw-r--r--src/cjr_print.sml33
-rw-r--r--tests/pquery.ur10
4 files changed, 18 insertions, 32 deletions
diff --git a/include/types.h b/include/types.h
index 73a3e575..c2c8f687 100644
--- a/include/types.h
+++ b/include/types.h
@@ -17,3 +17,7 @@ typedef lw_Basis_string lw_Basis_page;
typedef enum { SUCCESS, FATAL, BOUNDED_RETRY, UNLIMITED_RETRY } failure_kind;
+
+
+#define INTS_MAX 50
+#define FLOATS_MAX 100
diff --git a/src/c/urweb.c b/src/c/urweb.c
index 47b45367..632dbf1e 100644
--- a/src/c/urweb.c
+++ b/src/c/urweb.c
@@ -225,9 +225,6 @@ void lw_write(lw_context ctx, const char* s) {
}
-#define INTS_MAX 50
-#define FLOATS_MAX 100
-
char *lw_Basis_attrifyInt(lw_context ctx, lw_Basis_int n) {
char *result;
int len;
diff --git a/src/cjr_print.sml b/src/cjr_print.sml
index e9bc54de..24dedb6c 100644
--- a/src/cjr_print.sml
+++ b/src/cjr_print.sml
@@ -390,12 +390,10 @@ fun patConInfo env pc =
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 getPargs (e, _) =
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 @@ fun p_exp' par env (e, loc) =
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 @@ fun p_exp' par env (e, loc) =
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,
diff --git a/tests/pquery.ur b/tests/pquery.ur
index 0ccbc9f2..1086aaee 100644
--- a/tests/pquery.ur
+++ b/tests/pquery.ur
@@ -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>