on dev help sign in
start doing inserts
commit98ab760cc5a6f5dc9915ab0995bb6934acea3185 authorEitan Chatav <eitan.chatav@gmail.com> dateSat 22 Apr 2017, 16:31 (10 years ago) parent9c9395f
2 files changed+39−4
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+23−2 squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs+16−2
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+23−2
@@ -21,9 +21,11 @@ import Data.Monoid
2121import Data.Proxy
2222import Data.String
2323import Data.Vinyl
24+import Data.Vinyl.Functor
2425import GHC.OverloadedLabels
2526import GHC.TypeLits
2627
28+import qualified Data.ByteString as ByteString
2729import qualified Data.ByteString.Char8 as Char8
2830
2931import Squeel.PostgreSQL.Type
@@ -33,7 +35,7 @@ class HasField label xs x where
3335instance {-# OVERLAPPING #-} KnownSymbol label
3436 => HasField label ('(label, x) ': xs) x where
3537 fieldName _ _ _ = Char8.pack (symbolVal (Proxy @label))
36−instance HasField label xs y
38+instance {-# OVERLAPPABLE #-} HasField label xs y
3739 => HasField label (x ': xs) y where
3840 fieldName _ _ _ = fieldName (Proxy @label) (Proxy @xs) (Proxy @y)
3941
@@ -232,9 +234,28 @@ subselect selection = Relation
232234 , offsetting = Nothing
233235 }
234236
237+newtype Insertion ps xss = UnsafeInsertion {renderInsertion :: ByteString }
238+
239+into
240+ :: forall label xss xs ps
241+ . HasField label xss xs
242+ => Proxy label
243+ -> Rec (Aliased (Expression ps '[])) xs
244+ -> Insertion ps xss
245+into table values = UnsafeInsertion $ "INTO "
246+ <> fieldName table (Proxy @xss) (Proxy @xs)
247+ <> " VALUES "
248+ <> ByteString.intercalate ", "
249+ (recordToList (rmap (Const . renderExpression . unalias) values))
250+
235251newtype Query ps xss yss zs = UnsafeQuery { renderQuery :: ByteString }
236252newtype PreparedQuery ps xss yss zs =
237253 UnsafePreparedQuery { renderPreparedQuery :: ByteString }
238254
239255select :: Selection ps xss ys -> Query ps xss xss ys
240−select selection = UnsafeQuery $ "SELECT " <> renderSelection selection <> ";"
256+select selection = UnsafeQuery $
257+ "SELECT " <> renderSelection selection <> ";"
258+
259+insert :: Insertion ps xss -> Query ps xss xss '[]
260+insert insertion = UnsafeQuery $
261+ "INSERT " <> renderInsertion insertion <> ";"
squeel-postgresql/test/Squeel/PostgreSQL/QuerySpec.hs+16−2
@@ -2,11 +2,13 @@
22 DataKinds
33 , OverloadedLabels
44 , OverloadedStrings
5+ , TypeApplications
56#-}
67
78module Squeel.PostgreSQL.QuerySpec where
89
910import Data.Boolean
11+import Data.Proxy
1012import Data.Vinyl
1113import Test.Hspec
1214
@@ -47,6 +49,10 @@ spec = do
4749 renderQuery (select (star `from` (table1 `offset` 1 `offset` 2)))
4850 `shouldBe`
4951 "SELECT * FROM table1 OFFSET 1+2;"
52+ it "correctly render simple INSERTs" $ do
53+ renderQuery (insert insertion)
54+ `shouldBe`
55+ "INSERT INTO "
5056
5157
5258type Columns =
@@ -54,14 +60,22 @@ type Columns =
5460 , '("col2", 'PGInt4)
5561 ]
5662
63+type Tables = '[ '("table1", Columns)]
64+
5765sumAndCol1 :: Projection '[] Columns
5866 '[ '("sum", 'PGInt4)
5967 , '("col1", 'PGInt4)
6068 ]
6169sumAndCol1 = project ((#col1 + #col2) `As` #sum :& #col1 :& RNil)
6270
63−table1 :: Relation '[] '[ '("table1", Columns)] Columns
71+table1 :: Relation '[] Tables Columns
6472table1 = #table1
6573
66−parameterizedTable1 :: Relation '[ 'PGInt8] '[ '("table1", Columns)] Columns
74+parameterizedTable1 :: Relation '[ 'PGInt8] Tables Columns
6775parameterizedTable1 = #table1
76+
77+insertion :: Insertion '[] Tables
78+insertion = into (Proxy @"table1") values
79+
80+values :: Rec (Expression '[] '[]) '[ 'PGInt4, 'PGInt4]
81+values = (2 `AS` #col1 :& 4 `AS` #col2 :& RNil)