@@ -5,6 +5,7 @@
5 , MultiParamTypeClasses
5 , MultiParamTypeClasses
6 , OverloadedStrings
6 , OverloadedStrings
7 , PolyKinds
7 , PolyKinds
8 , ScopedTypeVariables
9 , ScopedTypeVariables
9 , TypeApplications
10 , TypeApplications
10 , TypeFamilies
11 , TypeFamilies
@@ -14,12 +15,15 @@
14module Squeel.PostgreSQL.Query where
15module Squeel.PostgreSQL.Query where
16import Data.Boolean
17import Data.Boolean
17−import Data.ByteString
18+import Data.ByteString (ByteString)
18import Data.Monoid
19import Data.Monoid
19import Data.Proxy
20import Data.Proxy
20import Data.String
21import Data.String
23+-- import Data.Vinyl.Functor
21import GHC.TypeLits
24import GHC.TypeLits
26+-- import qualified Data.ByteString as ByteString
23import qualified Data.ByteString.Char8 as Char8
27import qualified Data.ByteString.Char8 as Char8
25import Squeel.PostgreSQL.Type
29import Squeel.PostgreSQL.Type
@@ -60,6 +64,10 @@ column_ (_ :: Proxy ident) = UnsafeExpression $
60data Identified ident x = (:=) ident x
64data Identified ident x = (:=) ident x
61data Aliased obj named where AS :: obj x -> Aliased obj (name ':= x)
65data Aliased obj named where AS :: obj x -> Aliased obj (name ':= x)
67+-- class KnownAlias named obj where
69+-- renderAliased :: (obj x -> ByteString) -> Aliased obj named
63unalias :: Aliased obj (name ':= x) -> obj x
71unalias :: Aliased obj (name ':= x) -> obj x
64unalias (AS obj) = obj
72unalias (AS obj) = obj
@@ -69,6 +77,29 @@ column
69 => Aliased (Expression ps xs) (ident ':= x)
77 => Aliased (Expression ps xs) (ident ':= x)
70column = AS $ column_ (Proxy @ident)
78column = AS $ column_ (Proxy @ident)
82+ => (obj x -> ByteString)
83+ -> Aliased obj (name ':= x)
85+renderAliased render (AS obj :: Aliased obj (name ':= x)) =
86+ render obj <> " AS " <> Char8.pack (symbolVal (Proxy @name))
88+class AllAliased xs where
90+ :: (forall x. obj x -> ByteString)
91+ -> Rec (Aliased obj) xs
93+instance KnownSymbol name => AllAliased '[name ':= x] where
94+ renderAllAliased render
95+ (aliased :& RNil :: Rec (Aliased obj) '[name ':= x]) =
96+ renderAliased render aliased
97+instance (KnownSymbol name, AllAliased xs)
98+ => AllAliased ((name ':= x) ': xs) where
99+ renderAllAliased render
100+ (x :& xs :: Rec (Aliased obj) ((name ':= x) ': xs)) =
101+ renderAliased render x <> ", " <> renderAllAliased render xs
72instance IsString (Expression ps xs 'PGText) where
103instance IsString (Expression ps xs 'PGText) where
73 fromString str = UnsafeExpression $ "\'" <> fromString str <> "\'"
104 fromString str = UnsafeExpression $ "\'" <> fromString str <> "\'"
@@ -110,7 +141,20 @@ instance OrdB (Expression ps xs y) where
110 x <* y = UnsafeExpression $ renderExpression x <> "<" <> renderExpression y
141 x <* y = UnsafeExpression $ renderExpression x <> "<" <> renderExpression y
111 x <=* y = UnsafeExpression $ renderExpression x <> "<=" <> renderExpression y
142 x <=* y = UnsafeExpression $ renderExpression x <> "<=" <> renderExpression y
144+newtype Projection ps xs ys = UnsafeProjection
145+ { renderProjection :: ByteString }
147+star :: Projection ps xs xs
148+star = UnsafeProjection "*"
152+ => Rec (Aliased (Expression ps xs)) ys
153+ -> Projection ps xs ys
154+project = UnsafeProjection . renderAllAliased renderExpression
156+newtype Relation ps xss ys = UnsafeRelation
113−data Relation ps xs ys = UnsafeRelation { renderRelation :: ByteString }
157+ { renderRelation :: ByteString }
116 :: forall ident xss xs ps
160 :: forall ident xss xs ps