From 2f09ba1edb5aeb22cf61b57a8f145c5277d88a3c Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Sun, 10 Aug 2003 02:21:03 +0000 Subject: [PATCH] Random improvements --- libpq/pg.sml | 26 +++++++++++++++++++++----- libpq/sources.cm | 1 - sql_driver.sig | 1 + 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/libpq/pg.sml b/libpq/pg.sml index 928bf98..723df54 100644 --- a/libpq/pg.sml +++ b/libpq/pg.sml @@ -192,7 +192,12 @@ struct fun pad' (s, 0) = s | pad' (s, n) = pad' ("0" ^ s, n-1) - fun pad (n, i) = pad' (Int.toString n, i) + fun pad (n, i) = + let + val base = Int.toString n + in + pad' (base, Int.max (i - size base, 0)) + end fun offsetStr NONE = "+00" | offsetStr (SOME n) = @@ -205,14 +210,15 @@ struct "+" ^ pad (n, 2) end - fun timestampToSql t = + fun timestampToSqlUnquoted t = let val d = Date.fromTimeLocal t in - "'" ^ pad (Date.year d, 4) ^ "-" ^ pad (fromMonth (Date.month d), 2) ^ "-" ^ pad (Date.day d, 2) ^ + pad (Date.year d, 4) ^ "-" ^ pad (fromMonth (Date.month d), 2) ^ "-" ^ pad (Date.day d, 2) ^ " " ^ pad (Date.hour d, 2) ^ ":" ^ pad (Date.minute d, 2) ^ ":" ^ pad (Date.second d, 2) ^ - ".000000+" ^ offsetStr (Date.offset d) ^ "'" + ".000000" ^ offsetStr (Date.offset d) end + fun timestampToSql t = "'" ^ timestampToSqlUnquoted t ^ "'" fun timestampFromSql s = let val tokens = String.tokens (fn ch => ch = #"-" orelse ch = #" " orelse ch = #":" @@ -224,7 +230,17 @@ struct month = toMonth (intFromSql mon), offset = SOME (Time.fromSeconds (Int32.fromInt (intFromSql offset * 3600))), second = intFromSql second div 1000, year = intFromSql year}) - | _ => raise Format "Invalid timestamp" + | [year, mon, day, hour, minute, second, _] => + Date.toTime (Date.date {day = intFromSql day, hour = intFromSql mon, minute = intFromSql minute, + month = toMonth (intFromSql mon), + offset = NONE, + second = intFromSql second div 1000, year = intFromSql year}) + | [year, mon, day, hour, minute, second] => + Date.toTime (Date.date {day = intFromSql day, hour = intFromSql mon, minute = intFromSql minute, + month = toMonth (intFromSql mon), + offset = NONE, + second = intFromSql second div 1000, year = intFromSql year}) + | _ => raise Format ("Invalid timestamp " ^ s) end diff --git a/libpq/sources.cm b/libpq/sources.cm index dd36ba9..d4485a0 100644 --- a/libpq/sources.cm +++ b/libpq/sources.cm @@ -28,4 +28,3 @@ is FFI/libpq.h.cm (*: make ()*) pg.sml - pg.sig diff --git a/sql_driver.sig b/sql_driver.sig index 2b1016e..c96fbdb 100644 --- a/sql_driver.sig +++ b/sql_driver.sig @@ -45,6 +45,7 @@ sig val stringToSql : string -> string val stringFromSql : string -> string val timestampToSql : timestamp -> string + val timestampToSqlUnquoted : timestamp -> string val timestampFromSql : string -> timestamp val realToSql : real -> string val realFromSql : string -> real -- 2.20.1