@@ -21,9 +21,11 @@ import Data.Monoid
21import Data.Proxy
21import Data.Proxy
22import Data.String
22import Data.String
23import Data.Vinyl
23import Data.Vinyl
24+import Data.Vinyl.Functor
24import GHC.OverloadedLabels
25import GHC.OverloadedLabels
25import GHC.TypeLits
26import GHC.TypeLits
28+import qualified Data.ByteString as ByteString
27import qualified Data.ByteString.Char8 as Char8
29import qualified Data.ByteString.Char8 as Char8
29import Squeel.PostgreSQL.Type
31import Squeel.PostgreSQL.Type
@@ -33,7 +35,7 @@ class HasField label xs x where
33instance {-# OVERLAPPING #-} KnownSymbol label
35instance {-# OVERLAPPING #-} KnownSymbol label
34 => HasField label ('(label, x) ': xs) x where
36 => HasField label ('(label, x) ': xs) x where
35 fieldName _ _ _ = Char8.pack (symbolVal (Proxy @label))
37 fieldName _ _ _ = Char8.pack (symbolVal (Proxy @label))
36−instance HasField label xs y
38+instance {-# OVERLAPPABLE #-} HasField label xs y
37 => HasField label (x ': xs) y where
39 => HasField label (x ': xs) y where
38 fieldName _ _ _ = fieldName (Proxy @label) (Proxy @xs) (Proxy @y)
40 fieldName _ _ _ = fieldName (Proxy @label) (Proxy @xs) (Proxy @y)
@@ -45,6 +47,9 @@ instance IsLabel label (obj x)
45 => IsLabel label (Aliased obj '(label, x)) where
47 => IsLabel label (Aliased obj '(label, x)) where
46 fromLabel p = fromLabel p `As` fromLabel p
48 fromLabel p = fromLabel p `As` fromLabel p
50+aliasName :: forall label. KnownSymbol label => Alias label -> ByteString
51+aliasName _ = Char8.pack (symbolVal (Proxy @label))
48alias :: Aliased obj '(label, x) -> Alias label
53alias :: Aliased obj '(label, x) -> Alias label
49alias _ = Alias
54alias _ = Alias
@@ -59,6 +64,23 @@ renderAliased
59renderAliased render (obj `As` label) =
64renderAliased render (obj `As` label) =
60 render obj <> " AS " <> Char8.pack (symbolVal label)
65 render obj <> " AS " <> Char8.pack (symbolVal label)
67+class UnzipAliased (xs :: [(Symbol,k)]) where
68+ type Unaliased xs :: [k]
70+ :: Rec (Aliased obj) xs
71+ -> ([ByteString], Rec obj (Unaliased xs))
72+instance UnzipAliased '[] where
73+ type Unaliased '[] = '[]
74+ unzipAliased _ = ([], RNil)
75+instance (KnownSymbol label, UnzipAliased xs)
76+ => UnzipAliased ( '(label, x) ': xs) where
77+ type Unaliased ( '(label, x) ': xs) = x ': Unaliased xs
78+ unzipAliased (x :& xs) =
80+ (labels,values) = unzipAliased xs
82+ (aliasName (alias x) : labels, unalias x :& values)
62class AllAliased xs where
84class AllAliased xs where
63 renderAllAliased
85 renderAllAliased
64 :: (forall x. obj x -> ByteString)
86 :: (forall x. obj x -> ByteString)
@@ -232,9 +254,33 @@ subselect selection = Relation
232 , offsetting = Nothing
254 , offsetting = Nothing
257+newtype Insertion ps xss xs = UnsafeInsertion
258+ {renderInsertion :: ByteString }
261+ :: forall label xss xs ps
262+ . (HasField label xss xs, UnzipAliased xs)
264+ -> Rec (Aliased (Expression ps '[])) xs
265+ -> Insertion ps xss xs
266+into table values = UnsafeInsertion $ "INTO "
267+ <> fieldName table (Proxy @xss) (Proxy @xs)
268+ <> " (" <> ByteString.intercalate ", " names
270+ <> ByteString.intercalate ", "
271+ (recordToList (rmap (Const . renderExpression) exprs))
274+ (names,exprs) = unzipAliased values
235newtype Query ps xss yss zs = UnsafeQuery { renderQuery :: ByteString }
276newtype Query ps xss yss zs = UnsafeQuery { renderQuery :: ByteString }
236newtype PreparedQuery ps xss yss zs =
277newtype PreparedQuery ps xss yss zs =
237 UnsafePreparedQuery { renderPreparedQuery :: ByteString }
278 UnsafePreparedQuery { renderPreparedQuery :: ByteString }
239select :: Selection ps xss ys -> Query ps xss xss ys
280select :: Selection ps xss ys -> Query ps xss xss ys
281+select selection = UnsafeQuery $
240−select selection = UnsafeQuery $ "SELECT " <> renderSelection selection <> ";"
282+ "SELECT " <> renderSelection selection <> ";"
284+insert :: Insertion ps xss xs -> Query ps xss xss '[]
285+insert insertion = UnsafeQuery $
286+ "INSERT " <> renderInsertion insertion <> ";"