@@ -5,6 +5,7 @@
55 , MultiParamTypeClasses
66 , OverloadedStrings
77 , PolyKinds
8+ , RankNTypes
89 , ScopedTypeVariables
910 , TypeApplications
1011 , TypeFamilies
@@ -14,12 +15,15 @@
1415module Squeel.PostgreSQL.Query where
1516
1617import Data.Boolean
17−import Data.ByteString
18+import Data.ByteString (ByteString)
1819import Data.Monoid
1920import Data.Proxy
2021import Data.String
22+import Data.Vinyl
23+-- import Data.Vinyl.Functor
2124import GHC.TypeLits
2225
26+-- import qualified Data.ByteString as ByteString
2327import qualified Data.ByteString.Char8 as Char8
2428
2529import Squeel.PostgreSQL.Type
@@ -60,6 +64,10 @@ column_ (_ :: Proxy ident) = UnsafeExpression $
6064data Identified ident x = (:=) ident x
6165data Aliased obj named where AS :: obj x -> Aliased obj (name ':= x)
6266
67+-- class KnownAlias named obj where
68+-- type Alias named
69+-- renderAliased :: (obj x -> ByteString) -> Aliased obj named
70+
6371unalias :: Aliased obj (name ':= x) -> obj x
6472unalias (AS obj) = obj
6573
@@ -69,6 +77,29 @@ column
6977 => Aliased (Expression ps xs) (ident ':= x)
7078column = AS $ column_ (Proxy @ident)
7179
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+
72103instance IsString (Expression ps xs 'PGText) where
73104 fromString str = UnsafeExpression $ "\'" <> fromString str <> "\'"
74105
@@ -110,7 +141,20 @@ instance OrdB (Expression ps xs y) where
110141 x <* y = UnsafeExpression $ renderExpression x <> "<" <> renderExpression y
111142 x <=* y = UnsafeExpression $ renderExpression x <> "<=" <> renderExpression y
112143
113−data Relation ps xs ys = UnsafeRelation { renderRelation :: ByteString }
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
157+ { renderRelation :: ByteString }
114158
115159table_
116160 :: forall ident xss xs ps