on dev help sign in
Merge branch 'relation'
5763a2aEitan Chatav
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+46−2
@@ -5,6 +5,7 @@
5 , MultiParamTypeClasses
5 , MultiParamTypeClasses
6 , OverloadedStrings
6 , OverloadedStrings
7 , PolyKinds
7 , PolyKinds
8+ , RankNTypes
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
15
16
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
22+import Data.Vinyl
23+-- import Data.Vinyl.Functor
21import GHC.TypeLits
24import GHC.TypeLits
22
25
26+-- import qualified Data.ByteString as ByteString
23import qualified Data.ByteString.Char8 as Char8
27import qualified Data.ByteString.Char8 as Char8
24
28
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)
62
66
67+-- class KnownAlias named obj where
68+-- type Alias named
69+-- renderAliased :: (obj x -> ByteString) -> Aliased obj named
70+
63unalias :: Aliased obj (name ':= x) -> obj x
71unalias :: Aliased obj (name ':= x) -> obj x
64unalias (AS obj) = obj
72unalias (AS obj) = obj
65
73
@@ -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)
71
79
80+renderAliased
81+ :: KnownSymbol name
82+ => (obj x -> ByteString)
83+ -> Aliased obj (name ':= x)
84+ -> ByteString
85+renderAliased render (AS obj :: Aliased obj (name ':= x)) =
86+ render obj <> " AS " <> Char8.pack (symbolVal (Proxy @name))
87+
88+class AllAliased xs where
89+ renderAllAliased
90+ :: (forall x. obj x -> ByteString)
91+ -> Rec (Aliased obj) xs
92+ -> ByteString
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
102+
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 <> "\'"
74
105
@@ -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
112
143
144+newtype Projection ps xs ys = UnsafeProjection
145+ { renderProjection :: ByteString }
146+
147+star :: Projection ps xs xs
148+star = UnsafeProjection "*"
149+
150+project
151+ :: AllAliased ys
152+ => Rec (Aliased (Expression ps xs)) ys
153+ -> Projection ps xs ys
154+project = UnsafeProjection . renderAllAliased renderExpression
155+
156+newtype Relation ps xss ys = UnsafeRelation
113−data Relation ps xs ys = UnsafeRelation { renderRelation :: ByteString }
157+ { renderRelation :: ByteString }
114
158
115table_
159table_
116 :: forall ident xss xs ps
160 :: forall ident xss xs ps