aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adam@chlipala.net>2013-09-13 10:24:10 -0400
committerGravatar Adam Chlipala <adam@chlipala.net>2013-09-13 10:24:10 -0400
commited7e4c443e611490ce83c8ee6bedea14c636011c (patch)
tree0fb1418d845cdeac2a2910d51d39adf337d33cbe
parent99b557f9f4e8e71809323f752bd4e84747e9ec8b (diff)
Tweak Sergey's patch to work with Postgres
-rw-r--r--src/c/urweb.c70
-rw-r--r--tests/dbupload.ur25
-rw-r--r--tests/dbupload.urp6
3 files changed, 54 insertions, 47 deletions
diff --git a/src/c/urweb.c b/src/c/urweb.c
index 30e4d7a6..88361a87 100644
--- a/src/c/urweb.c
+++ b/src/c/urweb.c
@@ -2509,46 +2509,45 @@ uw_Basis_string uw_Basis_sqlifyChar(uw_context ctx, uw_Basis_char c) {
char *uw_sqlsuffixBlob = "::bytea";
-uw_Basis_string uw_Basis_sqlifyBlob_old(uw_context ctx, uw_Basis_blob b) {
+uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
char *r, *s2;
size_t i;
- uw_check_heap(ctx, b.size * 5 + 3 + uw_Estrings + strlen(uw_sqlsuffixBlob));
+ uw_check_heap(ctx, b.size * 5 + 4 + strlen(uw_sqlsuffixBlob));
r = s2 = ctx->heap.front;
if (uw_Estrings)
*s2++ = 'E';
+ else
+ *s2++ = 'X';
*s2++ = '\'';
for (i = 0; i < b.size; ++i) {
- char c = b.data[i];
+ unsigned char c = b.data[i];
- switch (c) {
- case '\'':
- if (uw_Estrings)
+ if (uw_Estrings) {
+ switch (c) {
+ case '\'':
strcpy(s2, "\\'");
- else
- strcpy(s2, "''");
- s2 += 2;
- break;
- case '\\':
- if (uw_Estrings) {
+ s2 += 2;
+ break;
+ case '\\':
strcpy(s2, "\\\\\\\\");
s2 += 4;
- } else
- *s2++ = '\\';
- break;
- default:
- if (isprint((int)c))
- *s2++ = c;
- else if (uw_Estrings) {
- sprintf(s2, "\\\\%03o", c);
- s2 += 5;
+ break;
+ default:
+ if (isprint((int)c))
+ *s2++ = c;
+ else {
+ sprintf(s2, "\\\\%03o", c);
+ s2 += 5;
+ }
}
- else
- uw_error(ctx, FATAL, "Non-printable character %u in blob to SQLify", c);
+ } else {
+ sprintf(s2, "%02X", c);
+ s2 += 2;
}
- }
+ }
*s2++ = '\'';
strcpy(s2, uw_sqlsuffixBlob);
@@ -2556,29 +2555,6 @@ uw_Basis_string uw_Basis_sqlifyBlob_old(uw_context ctx, uw_Basis_blob b) {
return r;
}
-int uw_Xstrings = 1;
-
-uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
- char *r, *s2;
- size_t i;
-
- uw_check_heap(ctx, b.size * 2 + 3 + uw_Xstrings);
-
- r = s2 = ctx->heap.front;
- *s2++ = 'X';
- *s2++ = '\'';
-
- for (i = 0; i < b.size; ++i) {
- char c = b.data[i];
- sprintf(s2, "%02X", c);
- s2 += 2;
- }
-
- *s2++ = '\'';
- ctx->heap.front = s2 + 1;
- return r;
-}
-
char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
int len;
char *r;
diff --git a/tests/dbupload.ur b/tests/dbupload.ur
new file mode 100644
index 00000000..f088d63b
--- /dev/null
+++ b/tests/dbupload.ur
@@ -0,0 +1,25 @@
+table t : { Id : int, Blob : blob, MimeType : string }
+sequence s
+
+fun getImage id : transaction page =
+ r <- oneRow1 (SELECT t.Blob, t.MimeType
+ FROM t
+ WHERE t.Id = {[id]});
+ returnBlob r.Blob (blessMime r.MimeType)
+
+fun main () : transaction page =
+ let
+ fun handle r =
+ id <- nextval s;
+ dml (INSERT INTO t (Id, Blob, MimeType)
+ VALUES ({[id]}, {[fileData r.File]}, {[fileMimeType r.File]}));
+ main ()
+ in
+ x <- queryX1 (SELECT t.Id FROM t)
+ (fn r => <xml><img src={url (getImage r.Id)}/><br/></xml>);
+ return <xml><body>
+ <form><upload{#File}/> <submit action={handle}/></form>
+ <hr/>
+ {x}
+ </body></xml>
+ end
diff --git a/tests/dbupload.urp b/tests/dbupload.urp
new file mode 100644
index 00000000..dd8417d1
--- /dev/null
+++ b/tests/dbupload.urp
@@ -0,0 +1,6 @@
+database dbname=dbupload
+sql dbupload.sql
+allow mime *
+rewrite all Dbupload/*
+
+dbupload