diff options
-rw-r--r-- | doc/manual.tex | 2 | ||||
-rw-r--r-- | src/urweb.grm | 8 | ||||
-rw-r--r-- | tests/tags.ur | 23 | ||||
-rw-r--r-- | tests/tags.urp | 6 |
4 files changed, 38 insertions, 1 deletions
diff --git a/doc/manual.tex b/doc/manual.tex index 4a11430b..97e5d2c0 100644 --- a/doc/manual.tex +++ b/doc/manual.tex @@ -2280,7 +2280,7 @@ $$\begin{array}{rrcll} &&& \{\{e\}\} \; \mt{AS} \; \{c\} & \textrm{computed table expression, with computed local name} \\ \textrm{$\mt{FROM}$ items} & F &::=& T \mid \{\{e\}\} \mid F \; J \; \mt{JOIN} \; F \; \mt{ON} \; E \\ &&& \mid F \; \mt{CROSS} \; \mt{JOIN} \ F \\ - &&& \mid (Q) \; \mt{AS} \; t \\ + &&& \mid (Q) \; \mt{AS} \; t \mid (\{\{e\}\}) \; \mt{AS} \; t \\ \textrm{Joins} & J &::=& [\mt{INNER}] \\ &&& \mid [\mt{LEFT} \mid \mt{RIGHT} \mid \mt{FULL}] \; [\mt{OUTER}] \\ \textrm{SQL expressions} & E &::=& t.f & \textrm{column references} \\ diff --git a/src/urweb.grm b/src/urweb.grm index 4671569d..1f4540ba 100644 --- a/src/urweb.grm +++ b/src/urweb.grm @@ -1981,6 +1981,14 @@ fitem : table' ([#1 table'], #2 table') in ([tname], (EApp (e, query), loc)) end) + | LPAREN LBRACE LBRACE eexp RBRACE RBRACE RPAREN AS tname (let + val loc = s (LPARENleft, RPARENright) + + val e = (EVar (["Basis"], "sql_from_query", Infer), loc) + val e = (ECApp (e, tname), loc) + in + ([tname], (EApp (e, eexp), loc)) + end) | LPAREN fitem RPAREN (fitem) tname : CSYMBOL (CName CSYMBOL, s (CSYMBOLleft, CSYMBOLright)) diff --git a/tests/tags.ur b/tests/tags.ur new file mode 100644 index 00000000..1213d824 --- /dev/null +++ b/tests/tags.ur @@ -0,0 +1,23 @@ +table images : { Id : int, Content : blob } +table tags : { Id : int, Tag : string } + +datatype mode = Present | Absent +type condition = { Tag : string, Mode : mode } + +type tag_query = sql_query [] [] [] [Id = int] + +fun addCondition (c : condition) (q : tag_query) : tag_query = + case c.Mode of + Present => (SELECT I.Id AS Id + FROM ({{q}}) AS I + JOIN tags ON tags.Id = I.Id AND tags.Tag = {[c.Tag]}) + | Absent => q + +fun withConditions (cs : list condition) : tag_query = + List.foldl addCondition (SELECT images.Id AS Id FROM images) cs + +fun main (cs : list condition) : transaction page = + x <- queryX (withConditions cs) (fn r => <xml><li>{[r.Id]}</li></xml>); + return <xml><body> + {x} + </body></xml> diff --git a/tests/tags.urp b/tests/tags.urp new file mode 100644 index 00000000..b2f21c5a --- /dev/null +++ b/tests/tags.urp @@ -0,0 +1,6 @@ +database dbname=test +sql tags.sql +rewrite url Tags/* + +$/list +tags |