on dev help sign in
reorder type variables in Statement
85723d0Eitan Chatav
squeel-postgresql/exe/Example.hs+1−1
@@ -33,7 +33,7 @@ main = do
33 &>>
33 &>>
34 insertInto #table1 ( 3 `As` #col1 :& 4 `As` #col2 :& RNil )
34 insertInto #table1 ( 3 `As` #col1 :& 4 `As` #col2 :& RNil )
35 Just selectTable1Result <- flip pqExecParams RNil $
35 Just selectTable1Result <- flip pqExecParams RNil $
36− (select $ starFrom #table1 :: Statement '[] Tables Tables Columns)
36+ (select $ starFrom #table1 :: Statement '[] Columns Tables Tables)
37 Just (Right value00) <- getvalue selectTable1Result (RowNumber 0) colNum0
37 Just (Right value00) <- getvalue selectTable1Result (RowNumber 0) colNum0
38 Just (Right value01) <- getvalue selectTable1Result (RowNumber 0) colNum1
38 Just (Right value01) <- getvalue selectTable1Result (RowNumber 0) colNum1
39 Just (Right value10) <- getvalue selectTable1Result (RowNumber 1) colNum0
39 Just (Right value10) <- getvalue selectTable1Result (RowNumber 1) colNum0
squeel-postgresql/src/Squeel/PostgreSQL/PQ.hs+8−8
@@ -66,24 +66,24 @@ class MonadPQ pq where
66
66
67 pqExec
67 pqExec
68 :: MonadBase IO io
68 :: MonadBase IO io
69− => Statement '[] db0 db1 '[]
69+ => Statement '[] '[] db0 db1
70 -> pq db0 db1 io (Maybe (Result '[]))
70 -> pq db0 db1 io (Maybe (Result '[]))
71
71
72 pqExecParams
72 pqExecParams
73 :: (MonadBase IO io, ToOids ps, ToValues xs ps)
73 :: (MonadBase IO io, ToOids ps, ToValues xs ps)
74− => Statement ps db0 db1 ys
74+ => Statement ps ys db0 db1
75 -> Rec Identity xs
75 -> Rec Identity xs
76 -> pq db0 db1 io (Maybe (Result ys))
76 -> pq db0 db1 io (Maybe (Result ys))
77
77
78 pqPrepare
78 pqPrepare
79 :: (MonadBase IO io, ToOids ps)
79 :: (MonadBase IO io, ToOids ps)
80 => ByteString
80 => ByteString
81− -> Statement ps db0 db1 xs
81+ -> Statement ps xs db0 db1
82− -> pq db0 db1 io (Maybe (Result []), PreparedStatement ps db0 db1 xs)
82+ -> pq db0 db1 io (Maybe (Result []), PreparedStatement ps xs db0 db1)
83
83
84 pqExecPrepared
84 pqExecPrepared
85 :: (MonadBase IO io, ToValues xs ps)
85 :: (MonadBase IO io, ToValues xs ps)
86− => PreparedStatement ps db0 db1 ys
86+ => PreparedStatement ps ys db0 db1
87 -> Rec Identity xs
87 -> Rec Identity xs
88 -> pq db0 db1 io (Maybe (Result ys))
88 -> pq db0 db1 io (Maybe (Result ys))
89
89
@@ -102,7 +102,7 @@ instance MonadPQ PQ where
102 result <- liftBase $ LibPQ.exec conn q
102 result <- liftBase $ LibPQ.exec conn q
103 return (Result <$> result, Connection conn)
103 return (Result <$> result, Connection conn)
104
104
105− pqExecParams (UnsafeStatement q :: Statement ps db0 db1 ys) params =
105+ pqExecParams (UnsafeStatement q :: Statement ps ys db0 db1) params =
106 PQ $ \ (Connection conn) -> do
106 PQ $ \ (Connection conn) -> do
107 let
107 let
108 params' =
108 params' =
@@ -115,7 +115,7 @@ instance MonadPQ PQ where
115 result <- liftBase $ LibPQ.execParams conn q params' LibPQ.Binary
115 result <- liftBase $ LibPQ.execParams conn q params' LibPQ.Binary
116 return (Result <$> result, Connection conn)
116 return (Result <$> result, Connection conn)
117
117
118− pqPrepare statementName (UnsafeStatement q :: Statement ps db0 db1 ys) =
118+ pqPrepare statementName (UnsafeStatement q :: Statement ps ys db0 db1) =
119 PQ $ \ (Connection conn) -> do
119 PQ $ \ (Connection conn) -> do
120 result <- liftBase $
120 result <- liftBase $
121 LibPQ.prepare conn statementName q (Just (toOids (proxy# :: Proxy# ps)))
121 LibPQ.prepare conn statementName q (Just (toOids (proxy# :: Proxy# ps)))
@@ -124,7 +124,7 @@ instance MonadPQ PQ where
124 , UnsafePreparedStatement statementName
124 , UnsafePreparedStatement statementName
125 ) , Connection conn )
125 ) , Connection conn )
126
126
127− pqExecPrepared (q :: PreparedStatement ps db0 db1 ys) params =
127+ pqExecPrepared (q :: PreparedStatement ps ys db0 db1) params =
128 PQ $ \ (Connection conn) -> do
128 PQ $ \ (Connection conn) -> do
129 let
129 let
130 params' =
130 params' =
squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+10−10
@@ -372,28 +372,28 @@ list `from` tabs = UnsafeSelection $
372
372
373newtype Statement
373newtype Statement
374 (params :: [NullityType])
374 (params :: [NullityType])
375+ (columns :: [(Symbol,NullityType)])
375 (schema0 :: [(Symbol,[(Symbol,NullityType)])])
376 (schema0 :: [(Symbol,[(Symbol,NullityType)])])
376 (schema1 :: [(Symbol,[(Symbol,NullityType)])])
377 (schema1 :: [(Symbol,[(Symbol,NullityType)])])
377− (columns :: [(Symbol,NullityType)])
378 = UnsafeStatement { renderStatement :: ByteString }
378 = UnsafeStatement { renderStatement :: ByteString }
379
379
380(&>>)
380(&>>)
381− :: Statement '[] schema0 schema1 '[]
381+ :: Statement '[] '[] schema0 schema1
382− -> Statement '[] schema1 schema2 results
382+ -> Statement '[] '[] schema1 schema2
383− -> Statement '[] schema0 schema2 results
383+ -> Statement '[] '[] schema0 schema2
384statement1 &>> statement2 = UnsafeStatement $
384statement1 &>> statement2 = UnsafeStatement $
385 renderStatement statement1 <> " " <> renderStatement statement2
385 renderStatement statement1 <> " " <> renderStatement statement2
386
386
387newtype PreparedStatement
387newtype PreparedStatement
388 (params :: [NullityType])
388 (params :: [NullityType])
389+ (columns :: [(Symbol,NullityType)])
389 (schema0 :: [(Symbol,[(Symbol,NullityType)])])
390 (schema0 :: [(Symbol,[(Symbol,NullityType)])])
390 (schema1 :: [(Symbol,[(Symbol,NullityType)])])
391 (schema1 :: [(Symbol,[(Symbol,NullityType)])])
391− (columns :: [(Symbol,NullityType)])
392 = UnsafePreparedStatement { renderPreparedStatement :: ByteString }
392 = UnsafePreparedStatement { renderPreparedStatement :: ByteString }
393
393
394select
394select
395 :: Selection params schema columns
395 :: Selection params schema columns
396− -> Statement params schema schema columns
396+ -> Statement params columns schema schema
397select = UnsafeStatement . ("SELECT " <>) . (<> ";") . renderSelection
397select = UnsafeStatement . ("SELECT " <>) . (<> ";") . renderSelection
398
398
399subselect
399subselect
@@ -408,7 +408,7 @@ insertInto
408 :: HasTable table schema columns
408 :: HasTable table schema columns
409 => Alias table
409 => Alias table
410 -> Rec (Aliased (Expression params '[])) columns
410 -> Rec (Aliased (Expression params '[])) columns
411− -> Statement params schema schema '[]
411+ -> Statement params '[] schema schema
412insertInto (Alias table) expressions = UnsafeStatement $ "INSERT INTO "
412insertInto (Alias table) expressions = UnsafeStatement $ "INSERT INTO "
413 <> fromString (symbolVal' table)
413 <> fromString (symbolVal' table)
414 <> " (" <> ByteString.intercalate ", " aliases
414 <> " (" <> ByteString.intercalate ", " aliases
@@ -427,7 +427,7 @@ createTable
427 :: (KnownSymbol table, KnownColumns columns)
427 :: (KnownSymbol table, KnownColumns columns)
428 => Alias table
428 => Alias table
429 -> Proxy# columns
429 -> Proxy# columns
430− -> Statement '[] schema ((table ::: columns) ': schema) '[]
430+ -> Statement '[] '[] schema ((table ::: columns) ': schema)
431createTable (Alias table) columns = UnsafeStatement $ mconcat
431createTable (Alias table) columns = UnsafeStatement $ mconcat
432 [ "CREATE TABLE "
432 [ "CREATE TABLE "
433 , fromString $ symbolVal' table
433 , fromString $ symbolVal' table
@@ -438,7 +438,7 @@ createTable (Alias table) columns = UnsafeStatement $ mconcat
438
438
439class KnownSymbol table => DropTable table schema0 schema1
439class KnownSymbol table => DropTable table schema0 schema1
440 | table schema0 -> schema1 where
440 | table schema0 -> schema1 where
441− dropTable :: Alias table -> Statement '[] schema0 schema1 '[]
441+ dropTable :: Alias table -> Statement '[] '[] schema0 schema1
442 dropTable (Alias table) = UnsafeStatement $
442 dropTable (Alias table) = UnsafeStatement $
443 "DROP TABLE " <> fromString (symbolVal' table) <> ";"
443 "DROP TABLE " <> fromString (symbolVal' table) <> ";"
444instance {-# OVERLAPPING #-}
444instance {-# OVERLAPPING #-}
@@ -459,7 +459,7 @@ update
459 => Alias table
459 => Alias table
460 -> Rec (Aliased (Maybe `Compose` Expression params '[table ::: columns])) columns
460 -> Rec (Aliased (Maybe `Compose` Expression params '[table ::: columns])) columns
461 -> Expression params '[table ::: columns] ('NotNull 'PGBool)
461 -> Expression params '[table ::: columns] ('NotNull 'PGBool)
462− -> Statement params schema schema '[]
462+ -> Statement params '[] schema schema
463update (Alias table) columns where' = UnsafeStatement $ mconcat
463update (Alias table) columns where' = UnsafeStatement $ mconcat
464 [ "UPDATE "
464 [ "UPDATE "
465 , fromString $ symbolVal' table
465 , fromString $ symbolVal' table
squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+15−15
@@ -23,7 +23,7 @@ spec = do
23 query `shouldRenderAs` str = renderStatement query `shouldBe` str
23 query `shouldRenderAs` str = renderStatement query `shouldBe` str
24 it "correctly renders a simple SELECT query" $ do
24 it "correctly renders a simple SELECT query" $ do
25 let
25 let
26− statement :: Statement '[] Tables Tables SumAndCol1
26+ statement :: Statement '[] SumAndCol1 Tables Tables
27 statement = select $
27 statement = select $
28 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
28 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
29 `from` (#table1 & where_ true)
29 `from` (#table1 & where_ true)
@@ -31,7 +31,7 @@ spec = do
31 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 AS table1 WHERE TRUE;"
31 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 AS table1 WHERE TRUE;"
32 it "combines WHEREs using AND" $ do
32 it "combines WHEREs using AND" $ do
33 let
33 let
34− statement :: Statement '[] Tables Tables SumAndCol1
34+ statement :: Statement '[] SumAndCol1 Tables Tables
35 statement = select $
35 statement = select $
36 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
36 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
37 `from` (#table1 & where_ true & where_ false)
37 `from` (#table1 & where_ true & where_ false)
@@ -40,45 +40,45 @@ spec = do
40 it "performs sub SELECTs" $ do
40 it "performs sub SELECTs" $ do
41 let
41 let
42 selection = ((#col1 + #col2) `As` #sum :& #col1 :& RNil) `from` #table1
42 selection = ((#col1 + #col2) `As` #sum :& #col1 :& RNil) `from` #table1
43− statement :: Statement '[] Tables Tables SumAndCol1
43+ statement :: Statement '[] SumAndCol1 Tables Tables
44 statement = select $ starFrom (subselect (selection `As` #sub))
44 statement = select $ starFrom (subselect (selection `As` #sub))
45 statement `shouldRenderAs`
45 statement `shouldRenderAs`
46 "SELECT * FROM SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 AS table1 AS sub;"
46 "SELECT * FROM SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 AS table1 AS sub;"
47 it "does LIMIT clauses" $ do
47 it "does LIMIT clauses" $ do
48 let
48 let
49− statement :: Statement '[] Tables Tables Columns
49+ statement :: Statement '[] Columns Tables Tables
50 statement = select $ starFrom (#table1 & limit 1)
50 statement = select $ starFrom (#table1 & limit 1)
51 statement `shouldRenderAs` "SELECT * FROM table1 AS table1 LIMIT 1;"
51 statement `shouldRenderAs` "SELECT * FROM table1 AS table1 LIMIT 1;"
52 it "should use the minimum of given LIMITs" $ do
52 it "should use the minimum of given LIMITs" $ do
53 let
53 let
54− statement :: Statement '[] Tables Tables Columns
54+ statement :: Statement '[] Columns Tables Tables
55 statement = select $ starFrom (#table1 & limit 1 & limit 2)
55 statement = select $ starFrom (#table1 & limit 1 & limit 2)
56 statement `shouldRenderAs`
56 statement `shouldRenderAs`
57 "SELECT * FROM table1 AS table1 LIMIT CASE WHEN (1 <= 2) THEN 1 ELSE 2 END;"
57 "SELECT * FROM table1 AS table1 LIMIT CASE WHEN (1 <= 2) THEN 1 ELSE 2 END;"
58 it "should render parameters using $ signs" $ do
58 it "should render parameters using $ signs" $ do
59 let
59 let
60− statement :: Statement '[ 'NotNull 'PGInt8] Tables Tables Columns
60+ statement :: Statement '[ 'NotNull 'PGInt8] Columns Tables Tables
61 statement = select $ starFrom (#table1 & limit param1)
61 statement = select $ starFrom (#table1 & limit param1)
62 statement `shouldRenderAs` "SELECT * FROM table1 AS table1 LIMIT $1;"
62 statement `shouldRenderAs` "SELECT * FROM table1 AS table1 LIMIT $1;"
63 it "does OFFSET clauses" $ do
63 it "does OFFSET clauses" $ do
64 let
64 let
65− statement :: Statement '[] Tables Tables Columns
65+ statement :: Statement '[] Columns Tables Tables
66 statement = select $ starFrom (#table1 & offset 1)
66 statement = select $ starFrom (#table1 & offset 1)
67 statement `shouldRenderAs` "SELECT * FROM table1 AS table1 OFFSET 1;"
67 statement `shouldRenderAs` "SELECT * FROM table1 AS table1 OFFSET 1;"
68 it "should use the sum of given OFFSETs" $ do
68 it "should use the sum of given OFFSETs" $ do
69 let
69 let
70− statement :: Statement '[] Tables Tables Columns
70+ statement :: Statement '[] Columns Tables Tables
71 statement = select $ starFrom (#table1 & offset 1 & offset 2)
71 statement = select $ starFrom (#table1 & offset 1 & offset 2)
72 statement `shouldRenderAs` "SELECT * FROM table1 AS table1 OFFSET (1 + 2);"
72 statement `shouldRenderAs` "SELECT * FROM table1 AS table1 OFFSET (1 + 2);"
73 it "correctly render simple INSERTs" $ do
73 it "correctly render simple INSERTs" $ do
74 let
74 let
75− statement :: Statement '[] Tables Tables '[]
75+ statement :: Statement '[] '[] Tables Tables
76 statement = insertInto #table1 $ 2 `As` #col1 :& 4 `As` #col2 :& RNil
76 statement = insertInto #table1 $ 2 `As` #col1 :& 4 `As` #col2 :& RNil
77 statement `shouldRenderAs`
77 statement `shouldRenderAs`
78 "INSERT INTO table1 (col1, col2) VALUES (2, 4);"
78 "INSERT INTO table1 (col1, col2) VALUES (2, 4);"
79 it "should be safe against SQL injection in literal text" $ do
79 it "should be safe against SQL injection in literal text" $ do
80 let
80 let
81− statement :: Statement '[] StudentsTable StudentsTable '[]
81+ statement :: Statement '[] '[] StudentsTable StudentsTable
82 statement = insertInto #students $
82 statement = insertInto #students $
83 "Robert'); DROP TABLE students;" `As` #name :& RNil
83 "Robert'); DROP TABLE students;" `As` #name :& RNil
84 statement `shouldRenderAs`
84 statement `shouldRenderAs`
@@ -91,7 +91,7 @@ spec = do
91 :& #shippers &. #shipperVal `As` #shipperVal :& RNil
91 :& #shippers &. #shipperVal `As` #shipperVal :& RNil
92 it "should render CROSS JOINs" $ do
92 it "should render CROSS JOINs" $ do
93 let
93 let
94− statement :: Statement '[] JoinTables JoinTables ValueColumns
94+ statement :: Statement '[] ValueColumns JoinTables JoinTables
95 statement = select $ vals `from`
95 statement = select $ vals `from`
96 (join (#orders & Cross #customers & Cross #shippers))
96 (join (#orders & Cross #customers & Cross #shippers))
97 statement `shouldRenderAs`
97 statement `shouldRenderAs`
@@ -111,7 +111,7 @@ spec = do
111 (#orders &. #customerID ==* #customers &. #customerID)
111 (#orders &. #customerID ==* #customers &. #customerID)
112 & Inner #shippers
112 & Inner #shippers
113 (#orders &. #shipperID ==* #shippers &. #shipperID)
113 (#orders &. #shipperID ==* #shippers &. #shipperID)
114− selection :: Statement '[] JoinTables JoinTables ValueColumns
114+ selection :: Statement '[] ValueColumns JoinTables JoinTables
115 selection = select $ vals `from` innerJoins
115 selection = select $ vals `from` innerJoins
116 selection `shouldRenderAs`
116 selection `shouldRenderAs`
117 "SELECT\
117 "SELECT\
@@ -125,7 +125,7 @@ spec = do
125 \ ON (orders.shipperID = shippers.shipperID);"
125 \ ON (orders.shipperID = shippers.shipperID);"
126 it "should render self JOINs" $ do
126 it "should render self JOINs" $ do
127 let
127 let
128− statement :: Statement '[] JoinTables JoinTables OrderColumns
128+ statement :: Statement '[] OrderColumns JoinTables JoinTables
129 statement = select $ #orders1 `dotStarFrom`
129 statement = select $ #orders1 `dotStarFrom`
130 (join (Table (#orders `As` #orders1)
130 (join (Table (#orders `As` #orders1)
131 & Cross (#orders `As` #orders2)))
131 & Cross (#orders `As` #orders2)))
@@ -135,13 +135,13 @@ spec = do
135 \ CROSS JOIN orders AS orders2;"
135 \ CROSS JOIN orders AS orders2;"
136 it "should render simple CREATE TABLE statements" $ do
136 it "should render simple CREATE TABLE statements" $ do
137 let
137 let
138− statement :: Statement '[] '[] Tables '[]
138+ statement :: Statement '[] '[] '[] Tables
139 statement = createTable #table1 (proxy# :: Proxy# Columns)
139 statement = createTable #table1 (proxy# :: Proxy# Columns)
140 statement `shouldRenderAs`
140 statement `shouldRenderAs`
141 "CREATE TABLE table1 (col1 int4 NOT NULL, col2 int4 NOT NULL);"
141 "CREATE TABLE table1 (col1 int4 NOT NULL, col2 int4 NOT NULL);"
142 it "should render DROP TABLE statements" $ do
142 it "should render DROP TABLE statements" $ do
143 let
143 let
144− statement :: Statement '[] Tables '[] '[]
144+ statement :: Statement '[] '[] Tables '[]
145 statement = dropTable #table1
145 statement = dropTable #table1
146 statement `shouldRenderAs` "DROP TABLE table1;"
146 statement `shouldRenderAs` "DROP TABLE table1;"
147
147