changeset 281:7d5860add50f

Change sqlify[int|float|string] to annotate with SQL types
author Adam Chlipala <adamc@hcoop.net>
date Thu, 04 Sep 2008 10:27:21 -0400
parents fdd7a698be01
children 0236d9412ad2
files include/urweb.h src/c/urweb.c src/mono_opt.sml
diffstat 3 files changed, 39 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Tue Sep 02 17:31:45 2008 -0400
+++ b/include/urweb.h	Thu Sep 04 10:27:21 2008 -0400
@@ -60,4 +60,7 @@
 lw_Basis_string lw_Basis_strcat(lw_context, lw_Basis_string, lw_Basis_string);
 lw_Basis_string lw_Basis_strdup(lw_context, lw_Basis_string);
 
+lw_Basis_int lw_Basis_sqlifyInt(lw_context, lw_Basis_int);
+lw_Basis_float lw_Basis_sqlifyFloat(lw_context, lw_Basis_float);
 lw_Basis_string lw_Basis_sqlifyString(lw_context, lw_Basis_string);
+lw_Basis_bool lw_Basis_sqlifyBool(lw_context, lw_Basis_bool);
--- a/src/c/urweb.c	Tue Sep 02 17:31:45 2008 -0400
+++ b/src/c/urweb.c	Thu Sep 04 10:27:21 2008 -0400
@@ -591,10 +591,33 @@
 }
 
 
+char *lw_Basis_sqlifyInt(lw_context ctx, lw_Basis_int n) {
+  int len;
+  char *r;
+
+  lw_check_heap(ctx, INTS_MAX + 6);
+  r = ctx->heap_front;
+  sprintf(r, "%lld::int8%n", n, &len);
+  ctx->heap_front += len+1;
+  return r;
+}
+
+char *lw_Basis_sqlifyFloat(lw_context ctx, lw_Basis_float n) {
+  int len;
+  char *r;
+
+  lw_check_heap(ctx, FLOATS_MAX + 8);
+  r = ctx->heap_front;
+  sprintf(r, "%g::float8%n", n, &len);
+  ctx->heap_front += len+1;
+  return r;
+}
+
+
 lw_Basis_string lw_Basis_sqlifyString(lw_context ctx, lw_Basis_string s) {
   char *r, *s2;
 
-  lw_check_heap(ctx, strlen(s) * 2 + 4);
+  lw_check_heap(ctx, strlen(s) * 2 + 10);
 
   r = s2 = ctx->heap_front;
   *s2++ = 'E';
@@ -622,8 +645,14 @@
     }
   }
 
-  *s2++ = '\'';
-  *s2++ = 0;
-  ctx->heap_front = s2;
+  strcpy(s2, "'::text");
+  ctx->heap_front = s2 + 8;
   return r;
 }
+
+char *lw_Basis_sqlifyBool(lw_context ctx, lw_Basis_bool b) {
+  if (b == lw_Basis_False)
+    return "FALSE";
+  else
+    return "TRUE";
+}
--- a/src/mono_opt.sml	Tue Sep 02 17:31:45 2008 -0400
+++ b/src/mono_opt.sml	Thu Sep 04 10:27:21 2008 -0400
@@ -81,12 +81,12 @@
                                                   "%" ^ hexIt ch)
 
 
-val sqlifyInt = attrifyInt
-val sqlifyFloat = attrifyFloat
+fun sqlifyInt n = attrifyInt n ^ "::int8"
+fun sqlifyFloat n = attrifyFloat n ^ "::float8"
 
 fun sqlifyString s = "E'" ^ String.translate (fn #"'" => "\\'"
                                                | ch => str ch)
-                                             (String.toString s) ^ "'"
+                                             (String.toString s) ^ "'::text"
         
 fun exp e =
     case e of