1
Fork 0

add support for 'table t' syntax

This commit is contained in:
Jake Wheat 2013-12-17 13:58:44 +02:00
parent 4308acb982
commit 72b67166d9
6 changed files with 63 additions and 64 deletions

View file

@ -602,7 +602,8 @@ and union, etc..
> queryExpr :: P QueryExpr > queryExpr :: P QueryExpr
> queryExpr = > queryExpr =
> choice [with > choice [with
> ,choice [values,select] >>= optionSuffix queryExprSuffix] > ,choice [values,table, select]
> >>= optionSuffix queryExprSuffix]
> where > where
> select = try (keyword_ "select") >> > select = try (keyword_ "select") >>
> Select > Select
@ -617,6 +618,7 @@ and union, etc..
> <*> optionMaybe offset > <*> optionMaybe offset
> values = try (keyword_ "values") > values = try (keyword_ "values")
> >> Values <$> commaSep (parens (commaSep scalarExpr)) > >> Values <$> commaSep (parens (commaSep scalarExpr))
> table = try (keyword_ "table") >> Table <$> name
> queryExprSuffix :: QueryExpr -> P QueryExpr > queryExprSuffix :: QueryExpr -> P QueryExpr
> queryExprSuffix qe = > queryExprSuffix qe =

View file

@ -180,6 +180,8 @@
> queryExpr (Values vs) = > queryExpr (Values vs) =
> text "values" > text "values"
> <+> nest 7 (commaSep (map (parens . commaSep . map scalarExpr) vs)) > <+> nest 7 (commaSep (map (parens . commaSep . map scalarExpr) vs))
> queryExpr (Table t) = text "table" <+> name t
> alias :: Alias -> Doc > alias :: Alias -> Doc
> alias (Alias nm cols) = > alias (Alias nm cols) =

View file

@ -164,25 +164,13 @@
> ,qeViews :: [(Alias,QueryExpr)] > ,qeViews :: [(Alias,QueryExpr)]
> ,qeQueryExpression :: QueryExpr} > ,qeQueryExpression :: QueryExpr}
> | Values [[ScalarExpr]] > | Values [[ScalarExpr]]
> | Table Name
> deriving (Eq,Show,Read) > deriving (Eq,Show,Read)
TODO: add queryexpr parens to deal with e.g. TODO: add queryexpr parens to deal with e.g.
(select 1 union select 2) union select 3 (select 1 union select 2) union select 3
I'm not sure if this is valid syntax or not. I'm not sure if this is valid syntax or not.
> -- | represents the Distinct or All keywords, which can be used
> -- before a select list, in an aggregate/window function
> -- application, or in a query expression set operator
> data Duplicates = Distinct | All deriving (Eq,Show,Read)
> -- | The direction for a column in order by.
> data Direction = Asc | Desc deriving (Eq,Show,Read)
> -- | Query expression set operators
> data CombineOp = Union | Except | Intersect deriving (Eq,Show,Read)
> -- | Corresponding, an option for the set operators
> data Corresponding = Corresponding | Respectively deriving (Eq,Show,Read)
> -- | helper/'default' value for query exprs to make creating query > -- | helper/'default' value for query exprs to make creating query
> -- expr values a little easier > -- expr values a little easier
> makeSelect :: QueryExpr > makeSelect :: QueryExpr
@ -196,6 +184,19 @@ I'm not sure if this is valid syntax or not.
> ,qeLimit = Nothing > ,qeLimit = Nothing
> ,qeOffset = Nothing} > ,qeOffset = Nothing}
> -- | represents the Distinct or All keywords, which can be used
> -- before a select list, in an aggregate/window function
> -- application, or in a query expression set operator
> data Duplicates = Distinct | All deriving (Eq,Show,Read)
> -- | The direction for a column in order by.
> data Direction = Asc | Desc deriving (Eq,Show,Read)
> -- | Query expression set operators
> data CombineOp = Union | Except | Intersect deriving (Eq,Show,Read)
> -- | Corresponding, an option for the set operators
> data Corresponding = Corresponding | Respectively deriving (Eq,Show,Read)
> -- | Represents a entry in the csv of tables in the from clause. > -- | Represents a entry in the csv of tables in the from clause.
> data TableRef = -- | from t > data TableRef = -- | from t
> TRSimple Name > TRSimple Name
@ -213,6 +214,9 @@ I'm not sure if this is valid syntax or not.
> | TRLateral TableRef > | TRLateral TableRef
> deriving (Eq,Show,Read) > deriving (Eq,Show,Read)
> -- | Represents an alias for a table valued expression, used in with
> -- queries and in from alias, e.g. select a from t u, select a from t u(b),
> -- with a(c) as select 1, select * from a;
> data Alias = Alias Name (Maybe [Name]) > data Alias = Alias Name (Maybe [Name])
> deriving (Eq,Show,Read) > deriving (Eq,Show,Read)

71
TODO
View file

@ -1,39 +1,8 @@
next release: next release:
lateral since it is easy. I think it is effectively just a prefix
operator on tablerefs (can appear like this from lateral a, from
lateral a,b, from a, lateral b, from a natural join lateral b
(although all of these are sematically garbage, this is the
valid syntax)
more dots: implement as dot operator
row ctor: row(a,b) is fine, but also when there is 2 or more elements, escapes in string literals
the word row can be omitted: (a,b)
more symbolic operators, array access a[5]? don't think this is
standard sql, if not, leave for now. There is something about
arrays in sql:2008
window frames and named windows
review tests to copy from hssqlppp
order by nulls first/last
extend case
group by extensions. Question: some of the syntax can be represented
by app and row ctor, should this be reused or new syntax created
(the standard has special syntax for cube and rollup).
table, values
collate? -> postfix operator which binds very tightly:
a < 'foo' collate 'C'
->
Op "<" [Iden "a", SpecialOp "collate" [StringLit 'foo', StringLit
'C']]
also postfix in order by:
select a from t order by a collate 'C': add to order by syntax, one
collation per column
function table reference
much more table reference tests, for joins and aliases etc.
ansi standard versions of limit and offset ansi standard versions of limit and offset
OFFSET start { ROW | ROWS } OFFSET start { ROW | ROWS }
@ -45,14 +14,44 @@ in the postgresql docs, the start and count must be in parens unless
+ sql server top syntax + sql server top syntax
quoted identifiers and proper character sets for identifiers more dots: implement as dot operator
more symbolic operators, array access a[5]? don't think this is
standard sql, if not, leave for now. There is something about
arrays in sql:2008
fix lateral binding issue
row ctor: row(a,b) is fine, but also when there is 2 or more elements,
the word row can be omitted: (a,b)
window frames and named windows
review tests to copy from hssqlppp
order by nulls first/last
extend case
group by extensions. Question: some of the syntax can be represented
by app and row ctor, should this be reused or new syntax created
(the standard has special syntax for cube and rollup).
collate? -> postfix operator which binds very tightly:
a < 'foo' collate 'C'
->
Op "<" [Iden "a", SpecialOp "collate" [StringLit 'foo', StringLit
'C']]
also postfix in order by:
select a from t order by a collate 'C': add to order by syntax, one
collation per column
much more table reference tests, for joins and aliases etc.
proper character sets for identifiers
run through postgres docs and add example sql as tests
review internal sql collection for more syntax/tests review internal sql collection for more syntax/tests
all ansi sql operators all ansi sql operators
escapes in string literals
review syntax to replace maybe and bool with better ctors review syntax to replace maybe and bool with better ctors
---- ----

View file

@ -263,17 +263,3 @@ select page reference
> ,"SELECT distributors.* WHERE distributors.name = 'Westward';" > ,"SELECT distributors.* WHERE distributors.name = 'Westward';"
> ] > ]
> {-f = mapM_ (putStrLn . either peFormattedError show . parseQueryExpr "" Nothing)
> ["SELECT * FROM t1 CROSS JOIN t2;"
> ,"SELECT * FROM t1 INNER JOIN t2 ON t1.num = t2.num;"
> ,"SELECT * FROM t1 INNER JOIN t2 USING (num);"
> ,"SELECT * FROM t1 NATURAL INNER JOIN t2;"
> ,"SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num;"
> ,"SELECT * FROM t1 LEFT JOIN t2 USING (num);"
> ,"SELECT * FROM t1 RIGHT JOIN t2 ON t1.num = t2.num;"
> ,"SELECT * FROM t1 FULL JOIN t2 ON t1.num = t2.num;"
> ,"SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num AND t2.value = 'xxx';"
> - ,"SELECT * FROM t1 LEFT JOIN t2 ON t1.num = t2.num WHERE t2.value = 'xxx';"]-}

View file

@ -24,6 +24,7 @@ These are a few misc tests which don't fit anywhere else.
> ,combos > ,combos
> ,withQueries > ,withQueries
> ,values > ,values
> ,tables
> ] > ]
@ -199,3 +200,8 @@ These are a few misc tests which don't fit anywhere else.
> ,Values [[NumLit "1", NumLit "2"] > ,Values [[NumLit "1", NumLit "2"]
> ,[NumLit "3", NumLit "4"]]) > ,[NumLit "3", NumLit "4"]])
> ] > ]
> tables :: TestItem
> tables = Group "tables" $ map (uncurry TestQueryExpr)
> [("table tbl", Table "tbl")
> ]