on dev help sign in
Query ~> Statement
commitb865f94fa042bd23d295e47ad3826d30d881992e authorEitan Chatav <eitan.chatav@gmail.com> dateSat 10 Jun 2017, 09:00 (9 years ago) parent8d840c2
4 files changed+44−44
squeel-postgresql/squeel-postgresql.cabal+2−2 squeel-postgresql/src/Squeel/PostgreSQL/PQ.hs+12−12 squeel-postgresql/src/Squeel/PostgreSQL/Query.hs → squeel-postgresql/src/Squeel/PostgreSQL/Statement.hs+13−13 squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs → squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hs+17−17
squeel-postgresql/squeel-postgresql.cabal+2−2
@@ -14,7 +14,7 @@ library
1414 hs-source-dirs: src
1515 exposed-modules:
1616 Squeel.PostgreSQL.PQ
17− Squeel.PostgreSQL.Query
17+ Squeel.PostgreSQL.Statement
1818 Squeel.PostgreSQL.Schema
1919 Squeel.PostgreSQL.Value
2020 default-language: Haskell2010
@@ -43,7 +43,7 @@ test-suite squeel-postgresql-test
4343 ghc-options: -Wall -Werror
4444 main-is: Spec.hs
4545 other-modules:
46− Squeel.PostgreSQL.QuerySpec
46+ Squeel.PostgreSQL.StatementSpec
4747 Squeel.PostgreSQL.ValueSpec
4848 build-depends:
4949 base
squeel-postgresql/src/Squeel/PostgreSQL/PQ.hs+12−12
@@ -28,7 +28,7 @@ import GHC.TypeLits
2828
2929import qualified Database.PostgreSQL.LibPQ as LibPQ
3030
31−import Squeel.PostgreSQL.Query
31+import Squeel.PostgreSQL.Statement
3232import Squeel.PostgreSQL.Value
3333import Squeel.PostgreSQL.Schema
3434
@@ -64,24 +64,24 @@ class MonadPQ pq where
6464
6565 pqExec
6666 :: MonadBase IO io
67− => Query '[] db0 db1 xs
67+ => Statement '[] db0 db1 xs
6868 -> pq db0 db1 io (Maybe (Result xs))
6969
7070 pqExecParams
7171 :: (MonadBase IO io, ToOids ps, ToValues xs ps)
72− => Query ps db0 db1 ys
72+ => Statement ps db0 db1 ys
7373 -> Rec Identity xs
7474 -> pq db0 db1 io (Maybe (Result ys))
7575
7676 pqPrepare
7777 :: (MonadBase IO io, ToOids ps)
7878 => ByteString
79− -> Query ps db0 db1 xs
80− -> pq db0 db1 io (Maybe (Result []), PreparedQuery ps db0 db1 xs)
79+ -> Statement ps db0 db1 xs
80+ -> pq db0 db1 io (Maybe (Result []), PreparedStatement ps db0 db1 xs)
8181
8282 pqExecPrepared
8383 :: (MonadBase IO io, ToValues xs ps)
84− => PreparedQuery ps db0 db1 ys
84+ => PreparedStatement ps db0 db1 ys
8585 -> Rec Identity xs
8686 -> pq db0 db1 io (Maybe (Result ys))
8787
@@ -96,11 +96,11 @@ instance MonadPQ PQ where
9696 (x', conn') <- x conn
9797 runPQ (f x') conn'
9898
99− pqExec (UnsafeQuery q) = PQ $ \ (Connection conn) -> do
99+ pqExec (UnsafeStatement q) = PQ $ \ (Connection conn) -> do
100100 result <- liftBase $ LibPQ.exec conn q
101101 return (Result <$> result, Connection conn)
102102
103− pqExecParams (UnsafeQuery q :: Query ps db0 db1 ys) params =
103+ pqExecParams (UnsafeStatement q :: Statement ps db0 db1 ys) params =
104104 PQ $ \ (Connection conn) -> do
105105 let
106106 params' =
@@ -113,16 +113,16 @@ instance MonadPQ PQ where
113113 result <- liftBase $ LibPQ.execParams conn q params' LibPQ.Binary
114114 return (Result <$> result, Connection conn)
115115
116− pqPrepare statementName (UnsafeQuery q :: Query ps db0 db1 ys) =
116+ pqPrepare statementName (UnsafeStatement q :: Statement ps db0 db1 ys) =
117117 PQ $ \ (Connection conn) -> do
118118 result <- liftBase $
119119 LibPQ.prepare conn statementName q (Just (toOids (proxy# :: Proxy# ps)))
120120 return
121121 ( ( Result <$> result
122− , UnsafePreparedQuery statementName
122+ , UnsafePreparedStatement statementName
123123 ) , Connection conn )
124124
125− pqExecPrepared (q :: PreparedQuery ps db0 db1 ys) params =
125+ pqExecPrepared (q :: PreparedStatement ps db0 db1 ys) params =
126126 PQ $ \ (Connection conn) -> do
127127 let
128128 params' =
@@ -130,7 +130,7 @@ instance MonadPQ PQ where
130130 | param' <- toValues (proxy# :: Proxy# ps) params
131131 ]
132132 result <- liftBase $
133− LibPQ.execPrepared conn (renderPreparedQuery q) params' LibPQ.Binary
133+ LibPQ.execPrepared conn (renderPreparedStatement q) params' LibPQ.Binary
134134 return (Result <$> result, Connection conn)
135135
136136instance Monad m => Applicative (PQ db db m) where
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs → squeel-postgresql/src/Squeel/PostgreSQL/Statement.hsrenamed+13−13
@@ -11,7 +11,7 @@
1111 , UndecidableInstances
1212#-}
1313
14−module Squeel.PostgreSQL.Query where
14+module Squeel.PostgreSQL.Statement where
1515
1616import Data.Boolean
1717import Data.ByteString (ByteString)
@@ -302,24 +302,24 @@ list `from` tabs = UnsafeSelection $
302302 . recordToList
303303 . rmap (Const . renderAliased renderExpression)
304304
305−newtype Query
305+newtype Statement
306306 (params :: [PGType])
307307 (schema0 :: [(Symbol,[(Symbol,PGType)])])
308308 (schema1 :: [(Symbol,[(Symbol,PGType)])])
309309 (columns :: [(Symbol,PGType)])
310− = UnsafeQuery { renderQuery :: ByteString }
310+ = UnsafeStatement { renderStatement :: ByteString }
311311
312−newtype PreparedQuery
312+newtype PreparedStatement
313313 (params :: [PGType])
314314 (schema0 :: [(Symbol,[(Symbol,PGType)])])
315315 (schema1 :: [(Symbol,[(Symbol,PGType)])])
316316 (columns :: [(Symbol,PGType)])
317− = UnsafePreparedQuery { renderPreparedQuery :: ByteString }
317+ = UnsafePreparedStatement { renderPreparedStatement :: ByteString }
318318
319319select
320320 :: Selection params schema columns
321− -> Query params schema schema columns
322−select = UnsafeQuery . ("SELECT " <>) . (<> ";") . renderSelection
321+ -> Statement params schema schema columns
322+select = UnsafeStatement . ("SELECT " <>) . (<> ";") . renderSelection
323323
324324subselect
325325 :: Aliased (Selection params schema) table
@@ -334,8 +334,8 @@ insertInto
334334 :: HasTable table schema columns
335335 => Alias table
336336 -> Rec (Aliased (Expression params '[])) columns
337− -> Query params schema schema '[]
338−insertInto (Alias table) expressions = UnsafeQuery $ "INSERT INTO "
337+ -> Statement params schema schema '[]
338+insertInto (Alias table) expressions = UnsafeStatement $ "INSERT INTO "
339339 <> fromString (symbolVal' table)
340340 <> " (" <> ByteString.intercalate ", " aliases
341341 <> ") VALUES ("
@@ -353,8 +353,8 @@ createTable
353353 :: (KnownSymbol table, KnownColumns columns)
354354 => Alias table
355355 -> Proxy# columns
356− -> Query '[] schema ((table ::: columns) ': schema) '[]
357−createTable (Alias table) columns = UnsafeQuery $ mconcat
356+ -> Statement '[] schema ((table ::: columns) ': schema) '[]
357+createTable (Alias table) columns = UnsafeStatement $ mconcat
358358 [ "CREATE TABLE "
359359 , fromString $ symbolVal' table
360360 , " ("
@@ -364,8 +364,8 @@ createTable (Alias table) columns = UnsafeQuery $ mconcat
364364
365365class KnownSymbol table => DropTable table schema0 schema1
366366 | table schema0 -> schema1 where
367− dropTable :: Alias table -> Query '[] schema0 schema1 '[]
368− dropTable (Alias table) = UnsafeQuery $
367+ dropTable :: Alias table -> Statement '[] schema0 schema1 '[]
368+ dropTable (Alias table) = UnsafeStatement $
369369 "DROP TABLE " <> fromString (symbolVal' table) <> ";"
370370instance {-# OVERLAPPING #-}
371371 (KnownSymbol table, table ~ table')
squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs → squeel-postgresql/test/Squeel/PostgreSQL/StatementSpec.hsrenamed+17−17
@@ -6,7 +6,7 @@
66 , TypeOperators
77#-}
88
9−module Squeel.PostgreSQL.QuerySpec where
9+module Squeel.PostgreSQL.StatementSpec where
1010
1111import Data.Boolean
1212import Data.Function
@@ -14,16 +14,16 @@ import Data.Vinyl
1414import GHC.Exts
1515import Test.Hspec
1616
17−import Squeel.PostgreSQL.Query
17+import Squeel.PostgreSQL.Statement
1818import Squeel.PostgreSQL.Schema
1919
2020spec :: Spec
2121spec = do
2222 let
23− query `shouldRenderAs` str = renderQuery query `shouldBe` str
23+ query `shouldRenderAs` str = renderStatement query `shouldBe` str
2424 it "correctly renders a simple SELECT query" $ do
2525 let
26− statement :: Query '[] Tables Tables SumAndCol1
26+ statement :: Statement '[] Tables Tables SumAndCol1
2727 statement = select $
2828 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
2929 `from` (#table1 & where_ true)
@@ -31,7 +31,7 @@ spec = do
3131 "SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 WHERE TRUE;"
3232 it "combines WHEREs using AND" $ do
3333 let
34− statement :: Query '[] Tables Tables SumAndCol1
34+ statement :: Statement '[] Tables Tables SumAndCol1
3535 statement = select $
3636 ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
3737 `from` (#table1 & where_ true & where_ false)
@@ -40,45 +40,45 @@ spec = do
4040 it "performs sub SELECTs" $ do
4141 let
4242 selection = ((#col1 + #col2) `As` #sum :& #col1 :& RNil) `from` #table1
43− statement :: Query '[] Tables Tables SumAndCol1
43+ statement :: Statement '[] Tables Tables SumAndCol1
4444 statement = select $ starFrom (subselect (selection `As` #sub))
4545 statement `shouldRenderAs`
4646 "SELECT * FROM SELECT (col1 + col2) AS sum, col1 AS col1 FROM table1 AS sub;"
4747 it "does LIMIT clauses" $ do
4848 let
49− statement :: Query '[] Tables Tables Columns
49+ statement :: Statement '[] Tables Tables Columns
5050 statement = select $ starFrom (#table1 & limit 1)
5151 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT 1;"
5252 it "should use the minimum of given LIMITs" $ do
5353 let
54− statement :: Query '[] Tables Tables Columns
54+ statement :: Statement '[] Tables Tables Columns
5555 statement = select $ starFrom (#table1 & limit 1 & limit 2)
5656 statement `shouldRenderAs`
5757 "SELECT * FROM table1 LIMIT CASE WHEN (1 <= 2) THEN 1 ELSE 2 END;"
5858 it "should render parameters using $ signs" $ do
5959 let
60− statement :: Query '[ 'PGInt8] Tables Tables Columns
60+ statement :: Statement '[ 'PGInt8] Tables Tables Columns
6161 statement = select $ starFrom (#table1 & limit param1)
6262 statement `shouldRenderAs` "SELECT * FROM table1 LIMIT $1;"
6363 it "does OFFSET clauses" $ do
6464 let
65− statement :: Query '[] Tables Tables Columns
65+ statement :: Statement '[] Tables Tables Columns
6666 statement = select $ starFrom (#table1 & offset 1)
6767 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET 1;"
6868 it "should use the sum of given OFFSETs" $ do
6969 let
70− statement :: Query '[] Tables Tables Columns
70+ statement :: Statement '[] Tables Tables Columns
7171 statement = select $ starFrom (#table1 & offset 1 & offset 2)
7272 statement `shouldRenderAs` "SELECT * FROM table1 OFFSET (1 + 2);"
7373 it "correctly render simple INSERTs" $ do
7474 let
75− statement :: Query '[] Tables Tables '[]
75+ statement :: Statement '[] Tables Tables '[]
7676 statement = insertInto #table1 $ 2 `As` #col1 :& 4 `As` #col2 :& RNil
7777 statement `shouldRenderAs`
7878 "INSERT INTO table1 (col1, col2) VALUES (2, 4);"
7979 it "should be safe against SQL injection in literal text" $ do
8080 let
81− statement :: Query '[] StudentsTable StudentsTable '[]
81+ statement :: Statement '[] StudentsTable StudentsTable '[]
8282 statement = insertInto #students $
8383 "Robert'); DROP TABLE students;" `As` #name :& RNil
8484 statement `shouldRenderAs`
@@ -91,7 +91,7 @@ spec = do
9191 :& #shippers .&. #shipperVal `As` #shipperVal :& RNil
9292 it "should render CROSS JOINs" $ do
9393 let
94− statement :: Query '[] JoinTables JoinTables ValueColumns
94+ statement :: Statement '[] JoinTables JoinTables ValueColumns
9595 statement = select $ vals `from`
9696 (tables (#orders & crossJoin #customers & crossJoin #shippers))
9797 statement `shouldRenderAs`
@@ -111,7 +111,7 @@ spec = do
111111 (#orders .&. #customerID ==* #customers .&. #customerID)
112112 & innerJoin #shippers
113113 (#orders .&. #shipperID ==* #shippers .&. #shipperID)
114− selection :: Query '[] JoinTables JoinTables ValueColumns
114+ selection :: Statement '[] JoinTables JoinTables ValueColumns
115115 selection = select $ vals `from` innerJoins
116116 selection `shouldRenderAs`
117117 "SELECT\
@@ -125,13 +125,13 @@ spec = do
125125 \ ON (orders.shipperID = shippers.shipperID);"
126126 it "should render simple CREATE TABLE statements" $ do
127127 let
128− statement :: Query '[] '[] Tables '[]
128+ statement :: Statement '[] '[] Tables '[]
129129 statement = createTable #table1 (proxy# :: Proxy# Columns)
130130 statement `shouldRenderAs`
131131 "CREATE TABLE table1 (col1 int4, col2 int4);"
132132 it "should render DROP TABLE statements" $ do
133133 let
134− statement :: Query '[] Tables '[] '[]
134+ statement :: Statement '[] Tables '[] '[]
135135 statement = dropTable #table1
136136 statement `shouldRenderAs`
137137 "DROP TABLE table1;"