From 89be6e97ad713323aaf23f866a7fe08303e7180f Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Fri, 30 Nov 2012 09:50:00 -0500 Subject: Fix generation of timestamp literals for MySQL and SQLite --- include/urweb/urweb.h | 2 +- src/c/urweb.c | 17 +++++++++++------ src/mysql.sml | 2 ++ src/postgres.sml | 2 ++ src/sqlite.sml | 2 ++ tests/timestamp.ur | 11 +++++++++++ tests/timestamp.urp | 5 +++++ 7 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 tests/timestamp.ur create mode 100644 tests/timestamp.urp diff --git a/include/urweb/urweb.h b/include/urweb/urweb.h index 38efa20c..2db8b095 100644 --- a/include/urweb/urweb.h +++ b/include/urweb/urweb.h @@ -273,7 +273,7 @@ uw_Basis_string uw_Basis_unAs(uw_context, uw_Basis_string); extern char *uw_sqlfmtInt; extern char *uw_sqlfmtFloat; -extern int uw_Estrings; +extern int uw_Estrings, uw_sql_type_annotations; extern char *uw_sqlsuffixString; extern char *uw_sqlsuffixChar; extern char *uw_sqlsuffixBlob; diff --git a/src/c/urweb.c b/src/c/urweb.c index 3a0af564..61fee786 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -2415,7 +2415,7 @@ char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) { return uw_Basis_sqlifyFloat(ctx, *n); } -int uw_Estrings = 1; +int uw_Estrings = 1, uw_sql_type_annotations = 1; char *uw_sqlsuffixString = "::text"; char *uw_sqlsuffixChar = "::char"; @@ -2634,12 +2634,17 @@ char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) { if (localtime_r(&t.seconds, &stm)) { s = uw_malloc(ctx, TIMES_MAX); len = strftime(s, TIMES_MAX, TIME_FMT_PG, &stm); - if (t.microseconds) { - r = uw_malloc(ctx, len + 21); - sprintf(r, "'%s.%06u'::timestamp", s, t.microseconds); + if (uw_sql_type_annotations) { + if (t.microseconds) { + r = uw_malloc(ctx, len + 21); + sprintf(r, "'%s.%06u'::timestamp", s, t.microseconds); + } else { + r = uw_malloc(ctx, len + 14); + sprintf(r, "'%s'::timestamp", s); + } } else { - r = uw_malloc(ctx, len + 14); - sprintf(r, "'%s'::timestamp", s); + r = uw_malloc(ctx, len + 3); + sprintf(r, "'%s'", s); } return r; } else diff --git a/src/mysql.sml b/src/mysql.sml index 1a641d57..999a9ea3 100644 --- a/src/mysql.sml +++ b/src/mysql.sml @@ -389,6 +389,8 @@ fun init {dbstring, prepared = ss, tables, views, sequences} = newline, string "uw_Estrings = 0;", newline, + string "uw_sql_type_annotations = 0;", + newline, string "uw_sqlsuffixString = \"\";", newline, string "uw_sqlsuffixChar = \"\";", diff --git a/src/postgres.sml b/src/postgres.sml index e555c565..ce7a78dd 100644 --- a/src/postgres.sml +++ b/src/postgres.sml @@ -380,6 +380,8 @@ fun init {dbstring, prepared = ss, tables, views, sequences} = newline, string "uw_Estrings = 1;", newline, + string "uw_sql_type_annotations = 1;", + newline, string "uw_sqlsuffixString = \"::text\";", newline, string "uw_sqlsuffixChar = \"::char\";", diff --git a/src/sqlite.sml b/src/sqlite.sml index 0f83c967..4ec64788 100644 --- a/src/sqlite.sml +++ b/src/sqlite.sml @@ -164,6 +164,8 @@ fun init {dbstring, prepared = ss, tables, views, sequences} = newline, string "uw_Estrings = 0;", newline, + string "uw_sql_type_annotations = 0;", + newline, string "uw_sqlsuffixString = \"\";", newline, string "uw_sqlsuffixChar = \"\";", diff --git a/tests/timestamp.ur b/tests/timestamp.ur new file mode 100644 index 00000000..56f030ed --- /dev/null +++ b/tests/timestamp.ur @@ -0,0 +1,11 @@ +table t : { A : time } + +fun many ls = + case ls of + [] => (WHERE TRUE) + | tm :: ls' => (WHERE t.A = {[tm]} AND {many ls'}) + +task initialize = fn () => + tm <- now; + dml (DELETE FROM t WHERE {many (tm :: [])}) + diff --git a/tests/timestamp.urp b/tests/timestamp.urp new file mode 100644 index 00000000..d07aa304 --- /dev/null +++ b/tests/timestamp.urp @@ -0,0 +1,5 @@ +database dbname=test +dbms mysql +sql timestamp.sql + +timestamp -- cgit v1.2.3