2013-12-17 10:40:31 +01:00
|
|
|
|
2013-12-17 10:48:40 +01:00
|
|
|
These are the tests for the query expression components apart from the
|
|
|
|
table refs which are in a separate file.
|
2013-12-17 10:40:31 +01:00
|
|
|
|
2013-12-17 10:48:40 +01:00
|
|
|
|
|
|
|
These are a few misc tests which don't fit anywhere else.
|
|
|
|
|
|
|
|
> module Language.SQL.SimpleSQL.QueryExprComponents (queryExprComponentTests) where
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> import Language.SQL.SimpleSQL.TestTypes
|
|
|
|
> import Language.SQL.SimpleSQL.Syntax
|
|
|
|
|
|
|
|
|
2013-12-17 10:48:40 +01:00
|
|
|
> queryExprComponentTests :: TestItem
|
|
|
|
> queryExprComponentTests = Group "queryExprComponentTests"
|
2013-12-17 10:40:31 +01:00
|
|
|
> [duplicates
|
|
|
|
> ,selectLists
|
|
|
|
> ,whereClause
|
|
|
|
> ,having
|
|
|
|
> ,orderBy
|
2013-12-17 19:04:49 +01:00
|
|
|
> ,offsetFetch
|
2013-12-17 10:40:31 +01:00
|
|
|
> ,combos
|
|
|
|
> ,withQueries
|
2013-12-17 12:27:16 +01:00
|
|
|
> ,values
|
2013-12-17 12:58:44 +01:00
|
|
|
> ,tables
|
2013-12-17 10:40:31 +01:00
|
|
|
> ]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
> duplicates :: TestItem
|
2014-06-27 11:19:15 +02:00
|
|
|
> duplicates = Group "duplicates" $ map (uncurry (TestQueryExpr SQL2011))
|
2014-04-18 10:18:21 +02:00
|
|
|
> [("select a from t" ,ms SQDefault)
|
2013-12-17 10:40:31 +01:00
|
|
|
> ,("select all a from t" ,ms All)
|
|
|
|
> ,("select distinct a from t", ms Distinct)
|
|
|
|
> ]
|
|
|
|
> where
|
|
|
|
> ms d = makeSelect
|
2013-12-19 09:34:32 +01:00
|
|
|
> {qeSetQuantifier = d
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,qeSelectList = [(Iden [Name "a"],Nothing)]
|
|
|
|
> ,qeFrom = [TRSimple [Name "t"]]}
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> selectLists :: TestItem
|
2014-06-27 11:19:15 +02:00
|
|
|
> selectLists = Group "selectLists" $ map (uncurry (TestQueryExpr SQL2011))
|
2013-12-17 10:40:31 +01:00
|
|
|
> [("select 1",
|
2013-12-31 10:31:00 +01:00
|
|
|
> makeSelect {qeSelectList = [(NumLit "1",Nothing)]})
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> ,("select a"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,makeSelect {qeSelectList = [(Iden [Name "a"],Nothing)]})
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> ,("select a,b"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,makeSelect {qeSelectList = [(Iden [Name "a"],Nothing)
|
|
|
|
> ,(Iden [Name "b"],Nothing)]})
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> ,("select 1+2,3+4"
|
|
|
|
> ,makeSelect {qeSelectList =
|
2014-04-19 10:45:45 +02:00
|
|
|
> [(BinOp (NumLit "1") [Name "+"] (NumLit "2"),Nothing)
|
|
|
|
> ,(BinOp (NumLit "3") [Name "+"] (NumLit "4"),Nothing)]})
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> ,("select a as a, /*comment*/ b as b"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,makeSelect {qeSelectList = [(Iden [Name "a"], Just $ Name "a")
|
|
|
|
> ,(Iden [Name "b"], Just $ Name "b")]})
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> ,("select a a, b b"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,makeSelect {qeSelectList = [(Iden [Name "a"], Just $ Name "a")
|
|
|
|
> ,(Iden [Name "b"], Just $ Name "b")]})
|
2013-12-19 09:34:32 +01:00
|
|
|
|
|
|
|
> ,("select a + b * c"
|
|
|
|
> ,makeSelect {qeSelectList =
|
2014-04-19 10:45:45 +02:00
|
|
|
> [(BinOp (Iden [Name "a"]) [Name "+"]
|
|
|
|
> (BinOp (Iden [Name "b"]) [Name "*"] (Iden [Name "c"]))
|
2013-12-31 10:31:00 +01:00
|
|
|
> ,Nothing)]})
|
2013-12-19 09:34:32 +01:00
|
|
|
|
2013-12-17 10:40:31 +01:00
|
|
|
> ]
|
|
|
|
|
|
|
|
> whereClause :: TestItem
|
2014-06-27 11:19:15 +02:00
|
|
|
> whereClause = Group "whereClause" $ map (uncurry (TestQueryExpr SQL2011))
|
2013-12-17 10:40:31 +01:00
|
|
|
> [("select a from t where a = 5"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,makeSelect {qeSelectList = [(Iden [Name "a"],Nothing)]
|
|
|
|
> ,qeFrom = [TRSimple [Name "t"]]
|
|
|
|
> ,qeWhere = Just $ BinOp (Iden [Name "a"]) [Name "="] (NumLit "5")})
|
2013-12-17 10:40:31 +01:00
|
|
|
> ]
|
|
|
|
|
|
|
|
> having :: TestItem
|
2014-06-27 11:19:15 +02:00
|
|
|
> having = Group "having" $ map (uncurry (TestQueryExpr SQL2011))
|
2013-12-17 10:40:31 +01:00
|
|
|
> [("select a,sum(b) from t group by a having sum(b) > 5"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,makeSelect {qeSelectList = [(Iden [Name "a"],Nothing)
|
|
|
|
> ,(App [Name "sum"] [Iden [Name "b"]],Nothing)]
|
|
|
|
> ,qeFrom = [TRSimple [Name "t"]]
|
|
|
|
> ,qeGroupBy = [SimpleGroup $ Iden [Name "a"]]
|
|
|
|
> ,qeHaving = Just $ BinOp (App [Name "sum"] [Iden [Name "b"]])
|
|
|
|
> [Name ">"] (NumLit "5")
|
2013-12-17 10:40:31 +01:00
|
|
|
> })
|
|
|
|
> ]
|
|
|
|
|
|
|
|
> orderBy :: TestItem
|
2014-06-27 11:19:15 +02:00
|
|
|
> orderBy = Group "orderBy" $ map (uncurry (TestQueryExpr SQL2011))
|
2013-12-17 10:40:31 +01:00
|
|
|
> [("select a from t order by a"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,ms [SortSpec (Iden [Name "a"]) DirDefault NullsOrderDefault])
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> ,("select a from t order by a, b"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,ms [SortSpec (Iden [Name "a"]) DirDefault NullsOrderDefault
|
|
|
|
> ,SortSpec (Iden [Name "b"]) DirDefault NullsOrderDefault])
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> ,("select a from t order by a asc"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,ms [SortSpec (Iden [Name "a"]) Asc NullsOrderDefault])
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> ,("select a from t order by a desc, b desc"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,ms [SortSpec (Iden [Name "a"]) Desc NullsOrderDefault
|
|
|
|
> ,SortSpec (Iden [Name "b"]) Desc NullsOrderDefault])
|
2013-12-17 17:28:31 +01:00
|
|
|
|
|
|
|
> ,("select a from t order by a desc nulls first, b desc nulls last"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,ms [SortSpec (Iden [Name "a"]) Desc NullsFirst
|
|
|
|
> ,SortSpec (Iden [Name "b"]) Desc NullsLast])
|
2013-12-17 17:28:31 +01:00
|
|
|
|
2013-12-17 10:40:31 +01:00
|
|
|
> ]
|
|
|
|
> where
|
2014-04-19 10:45:45 +02:00
|
|
|
> ms o = makeSelect {qeSelectList = [(Iden [Name "a"],Nothing)]
|
|
|
|
> ,qeFrom = [TRSimple [Name "t"]]
|
2013-12-17 10:40:31 +01:00
|
|
|
> ,qeOrderBy = o}
|
|
|
|
|
2013-12-17 19:04:49 +01:00
|
|
|
> offsetFetch :: TestItem
|
2014-06-27 11:19:15 +02:00
|
|
|
> offsetFetch = Group "offsetFetch" $ map (uncurry (TestQueryExpr SQL2011))
|
2013-12-17 15:00:17 +01:00
|
|
|
> [-- ansi standard
|
|
|
|
> ("select a from t offset 5 rows fetch next 10 rows only"
|
|
|
|
> ,ms (Just $ NumLit "5") (Just $ NumLit "10"))
|
|
|
|
> ,("select a from t offset 5 rows;"
|
|
|
|
> ,ms (Just $ NumLit "5") Nothing)
|
|
|
|
> ,("select a from t fetch next 10 row only;"
|
|
|
|
> ,ms Nothing (Just $ NumLit "10"))
|
|
|
|
> ,("select a from t offset 5 row fetch first 10 row only"
|
|
|
|
> ,ms (Just $ NumLit "5") (Just $ NumLit "10"))
|
2014-04-20 22:14:55 +02:00
|
|
|
> -- postgres: disabled, will add back when postgres
|
|
|
|
> -- dialect is added
|
|
|
|
> --,("select a from t limit 10 offset 5"
|
|
|
|
> -- ,ms (Just $ NumLit "5") (Just $ NumLit "10"))
|
2013-12-17 10:40:31 +01:00
|
|
|
> ]
|
|
|
|
> where
|
2013-12-17 15:00:17 +01:00
|
|
|
> ms o l = makeSelect
|
2014-04-19 10:45:45 +02:00
|
|
|
> {qeSelectList = [(Iden [Name "a"],Nothing)]
|
|
|
|
> ,qeFrom = [TRSimple [Name "t"]]
|
2013-12-17 15:00:17 +01:00
|
|
|
> ,qeOffset = o
|
2013-12-19 16:50:25 +01:00
|
|
|
> ,qeFetchFirst = l}
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> combos :: TestItem
|
2014-06-27 11:19:15 +02:00
|
|
|
> combos = Group "combos" $ map (uncurry (TestQueryExpr SQL2011))
|
2013-12-17 10:40:31 +01:00
|
|
|
> [("select a from t union select b from u"
|
2014-04-18 10:18:21 +02:00
|
|
|
> ,CombineQueryExpr ms1 Union SQDefault Respectively ms2)
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> ,("select a from t intersect select b from u"
|
2014-04-18 10:18:21 +02:00
|
|
|
> ,CombineQueryExpr ms1 Intersect SQDefault Respectively ms2)
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> ,("select a from t except all select b from u"
|
|
|
|
> ,CombineQueryExpr ms1 Except All Respectively ms2)
|
|
|
|
|
|
|
|
> ,("select a from t union distinct corresponding \
|
|
|
|
> \select b from u"
|
|
|
|
> ,CombineQueryExpr ms1 Union Distinct Corresponding ms2)
|
|
|
|
|
|
|
|
> ,("select a from t union select a from t union select a from t"
|
|
|
|
> -- TODO: union should be left associative. I think the others also
|
|
|
|
> -- so this needs to be fixed (new optionSuffix variation which
|
|
|
|
> -- handles this)
|
2014-04-18 10:18:21 +02:00
|
|
|
> ,CombineQueryExpr ms1 Union SQDefault Respectively
|
|
|
|
> (CombineQueryExpr ms1 Union SQDefault Respectively ms1))
|
2013-12-17 10:40:31 +01:00
|
|
|
> ]
|
|
|
|
> where
|
|
|
|
> ms1 = makeSelect
|
2014-04-19 10:45:45 +02:00
|
|
|
> {qeSelectList = [(Iden [Name "a"],Nothing)]
|
|
|
|
> ,qeFrom = [TRSimple [Name "t"]]}
|
2013-12-17 10:40:31 +01:00
|
|
|
> ms2 = makeSelect
|
2014-04-19 10:45:45 +02:00
|
|
|
> {qeSelectList = [(Iden [Name "b"],Nothing)]
|
|
|
|
> ,qeFrom = [TRSimple [Name "u"]]}
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
> withQueries :: TestItem
|
2014-06-27 11:19:15 +02:00
|
|
|
> withQueries = Group "with queries" $ map (uncurry (TestQueryExpr SQL2011))
|
2013-12-17 10:40:31 +01:00
|
|
|
> [("with u as (select a from t) select a from u"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,With False [(Alias (Name "u") Nothing, ms1)] ms2)
|
2013-12-17 12:41:06 +01:00
|
|
|
|
|
|
|
> ,("with u(b) as (select a from t) select a from u"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,With False [(Alias (Name "u") (Just [Name "b"]), ms1)] ms2)
|
2013-12-17 10:40:31 +01:00
|
|
|
|
|
|
|
> ,("with x as (select a from t),\n\
|
|
|
|
> \ u as (select a from x)\n\
|
|
|
|
> \select a from u"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,With False [(Alias (Name "x") Nothing, ms1), (Alias (Name "u") Nothing,ms3)] ms2)
|
2013-12-17 12:41:06 +01:00
|
|
|
|
|
|
|
> ,("with recursive u as (select a from t) select a from u"
|
2014-04-19 10:45:45 +02:00
|
|
|
> ,With True [(Alias (Name "u") Nothing, ms1)] ms2)
|
2013-12-17 10:40:31 +01:00
|
|
|
> ]
|
|
|
|
> where
|
|
|
|
> ms c t = makeSelect
|
2014-04-19 10:45:45 +02:00
|
|
|
> {qeSelectList = [(Iden [Name c],Nothing)]
|
|
|
|
> ,qeFrom = [TRSimple [Name t]]}
|
2013-12-17 10:40:31 +01:00
|
|
|
> ms1 = ms "a" "t"
|
|
|
|
> ms2 = ms "a" "u"
|
|
|
|
> ms3 = ms "a" "x"
|
2013-12-17 12:27:16 +01:00
|
|
|
|
|
|
|
> values :: TestItem
|
2014-06-27 11:19:15 +02:00
|
|
|
> values = Group "values" $ map (uncurry (TestQueryExpr SQL2011))
|
2013-12-17 12:27:16 +01:00
|
|
|
> [("values (1,2),(3,4)"
|
|
|
|
> ,Values [[NumLit "1", NumLit "2"]
|
|
|
|
> ,[NumLit "3", NumLit "4"]])
|
|
|
|
> ]
|
2013-12-17 12:58:44 +01:00
|
|
|
|
|
|
|
> tables :: TestItem
|
2014-06-27 11:19:15 +02:00
|
|
|
> tables = Group "tables" $ map (uncurry (TestQueryExpr SQL2011))
|
2014-04-19 10:45:45 +02:00
|
|
|
> [("table tbl", Table [Name "tbl"])
|
2013-12-17 12:58:44 +01:00
|
|
|
> ]
|