diff options
-rw-r--r-- | lib/basis.urs | 4 | ||||
-rw-r--r-- | src/mono_opt.sml | 21 | ||||
-rw-r--r-- | src/mono_reduce.sml | 4 | ||||
-rw-r--r-- | src/monoize.sml | 74 | ||||
-rw-r--r-- | tests/query.ur | 8 |
5 files changed, 103 insertions, 8 deletions
diff --git a/lib/basis.urs b/lib/basis.urs index 7435e716..f53ee1c1 100644 --- a/lib/basis.urs +++ b/lib/basis.urs @@ -119,9 +119,9 @@ val sql_lt : sql_comparison val sql_le : sql_comparison val sql_gt : sql_comparison val sql_ge : sql_comparison -val sql_comparison : sql_comparison - -> tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} +val sql_comparison : tables ::: {{Type}} -> agg ::: {{Type}} -> exps ::: {Type} -> t ::: Type + -> sql_comparison -> sql_exp tables agg exps t -> sql_exp tables agg exps t -> sql_exp tables agg exps bool diff --git a/src/mono_opt.sml b/src/mono_opt.sml index 4b417217..1430dfe2 100644 --- a/src/mono_opt.sml +++ b/src/mono_opt.sml @@ -79,6 +79,12 @@ val urlifyString = String.translate (fn #" " => "+" str ch else "%" ^ hexIt ch) + + +val sqlifyInt = attrifyInt +val sqlifyFloat = attrifyFloat + +fun sqlifyString s = "E'" ^ String.toString s ^ "'" fun exp e = case e of @@ -203,6 +209,21 @@ fun exp e = | EWrite (EFfiApp ("Basis", "urlifyBool", [e]), _) => EFfiApp ("Basis", "urlifyBool_w", [e]) + | EFfiApp ("Basis", "sqlifyInt", [(EPrim (Prim.Int n), _)]) => + EPrim (Prim.String (sqlifyInt n)) + | EFfiApp ("Basis", "sqlifyFloat", [(EPrim (Prim.Float n), _)]) => + EPrim (Prim.String (sqlifyFloat n)) + | EFfiApp ("Basis", "sqlifyBool", [b as (_, loc)]) => + optExp (ECase (b, + [((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "True", arg = NONE}, NONE), loc), + (EPrim (Prim.String "TRUE"), loc)), + ((PCon (Enum, PConFfi {mod = "Basis", datatyp = "bool", con = "False", arg = NONE}, NONE), loc), + (EPrim (Prim.String "FALSE"), loc))], + {disc = (TFfi ("Basis", "bool"), loc), + result = (TFfi ("Basis", "string"), loc)}), loc) + | EFfiApp ("Basis", "sqlifyString", [(EPrim (Prim.String n), _)]) => + EPrim (Prim.String (sqlifyString n)) + | EWrite (ECase (discE, pes, {disc, ...}), loc) => optExp (ECase (discE, map (fn (p, e) => (p, (EWrite e, loc))) pes, diff --git a/src/mono_reduce.sml b/src/mono_reduce.sml index 1941f0cc..06cc8bbf 100644 --- a/src/mono_reduce.sml +++ b/src/mono_reduce.sml @@ -181,8 +181,8 @@ fun exp env e = (ELet (x1, t1, b1, liftExpInExp 1 b2), loc)), loc) in - Print.prefaces "ELet commute" [("e", MonoPrint.p_exp env (e, loc)), - ("e'", MonoPrint.p_exp env e')]; + (*Print.prefaces "ELet commute" [("e", MonoPrint.p_exp env (e, loc)), + ("e'", MonoPrint.p_exp env e')];*) #1 (reduceExp env e') end | EApp ((ELet (x, t, e, b), loc), e') => diff --git a/src/monoize.sml b/src/monoize.sml index abbd4f40..e908b162 100644 --- a/src/monoize.sml +++ b/src/monoize.sml @@ -612,7 +612,9 @@ fun monoExp (env, st, fm) (all as (e, loc)) = xts)) stables), sc " FROM ", strcatComma loc (map (fn (x, _) => strcat loc [(L'.EField (gf "From", x), loc), - sc (" AS " ^ x)]) tables) + sc (" AS " ^ x)]) tables), + sc " WHERE ", + gf "Where" ]), loc), fm) | _ => poly () @@ -635,6 +637,23 @@ fun monoExp (env, st, fm) (all as (e, loc)) = (L'.ERel 0, loc)), loc), fm) end + | L.EFfi ("Basis", "sql_int") => + ((L'.EAbs ("x", (L'.TFfi ("Basis", "int"), loc), (L'.TFfi ("Basis", "string"), loc), + (L'.EFfiApp ("Basis", "sqlifyInt", [(L'.ERel 0, loc)]), loc)), loc), + fm) + | L.EFfi ("Basis", "sql_float") => + ((L'.EAbs ("x", (L'.TFfi ("Basis", "float"), loc), (L'.TFfi ("Basis", "string"), loc), + (L'.EFfiApp ("Basis", "sqlifyFloat", [(L'.ERel 0, loc)]), loc)), loc), + fm) + | L.EFfi ("Basis", "sql_bool") => + ((L'.EAbs ("x", (L'.TFfi ("Basis", "bool"), loc), (L'.TFfi ("Basis", "string"), loc), + (L'.EFfiApp ("Basis", "sqlifyBool", [(L'.ERel 0, loc)]), loc)), loc), + fm) + | L.EFfi ("Basis", "sql_string") => + ((L'.EAbs ("x", (L'.TFfi ("Basis", "string"), loc), (L'.TFfi ("Basis", "string"), loc), + (L'.EFfiApp ("Basis", "sqlifyString", [(L'.ERel 0, loc)]), loc)), loc), + fm) + | L.ECApp ((L.EFfi ("Basis", "sql_subset"), _), _) => ((L'.ERecord [], loc), fm) | L.ECApp ((L.EFfi ("Basis", "sql_subset_all"), _), _) => @@ -647,6 +666,59 @@ fun monoExp (env, st, fm) (all as (e, loc)) = ((L'.EPrim (Prim.String ""), loc), fm) | L.EFfi ("Basis", "sql_no_offset") => ((L'.EPrim (Prim.String ""), loc), fm) + + | L.EFfi ("Basis", "sql_eq") => + ((L'.EPrim (Prim.String "="), loc), fm) + | L.EFfi ("Basis", "sql_ne") => + ((L'.EPrim (Prim.String "<>"), loc), fm) + | L.EFfi ("Basis", "sql_lt") => + ((L'.EPrim (Prim.String "<"), loc), fm) + | L.EFfi ("Basis", "sql_le") => + ((L'.EPrim (Prim.String "<="), loc), fm) + | L.EFfi ("Basis", "sql_gt") => + ((L'.EPrim (Prim.String ">"), loc), fm) + | L.EFfi ("Basis", "sql_ge") => + ((L'.EPrim (Prim.String ">="), loc), fm) + + | L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.EFfi ("Basis", "sql_comparison"), _), + _), _), + _), _), + _), _), + _) => + let + val s = (L'.TFfi ("Basis", "string"), loc) + fun sc s = (L'.EPrim (Prim.String s), loc) + in + ((L'.EAbs ("c", s, (L'.TFun (s, (L'.TFun (s, s), loc)), loc), + (L'.EAbs ("e1", s, (L'.TFun (s, s), loc), + (L'.EAbs ("e2", s, s, + strcat loc [(L'.ERel 1, loc), + sc " ", + (L'.ERel 2, loc), + sc " ", + (L'.ERel 0, loc)]), loc)), loc)), loc), + fm) + end + + | L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.ECApp ( + (L.EFfi ("Basis", "sql_field"), _), + _), _), + _), _), + _), _), + _), _), + _), _), + (L.CName tab, _)), _), + (L.CName field, _)) => ((L'.EPrim (Prim.String (tab ^ "." ^ field)), loc), fm) | L.EApp ( (L.ECApp ( diff --git a/tests/query.ur b/tests/query.ur index 91c0cee3..afe5fab8 100644 --- a/tests/query.ur +++ b/tests/query.ur @@ -1,10 +1,12 @@ -table t1 : {A : int, B : string, C : float} +table t1 : {A : int, B : string, C : float, D : bool} table t2 : {A : float, D : int} datatype list a = Nil | Cons of a * list a -val q1 = (SELECT * FROM t1) -val r1 : transaction (list {A : int, B : string, C : float}) = +(*val q1 = (SELECT * FROM t1)*) +val q1 = (SELECT * FROM t1 WHERE t1.A = 0) + +val r1 : transaction (list {A : int, B : string, C : float, D : bool}) = query q1 (fn fs acc => return (Cons (fs.T1, acc))) Nil |