@@ -22,14 +22,15 @@ import Data.ByteString (ByteString)
2222import Data.Monoid
2323import Data.Proxy
2424import Data.String
25−-- import Data.Vinyl
25+import Data.Vinyl
26+import Data.Vinyl.Functor
2627import GHC.TypeLits
2728
2829import qualified Data.ByteString.Char8 as Char8
2930
3031import Squeel.PostgreSQL.Type
3132
32−data Expression (ps :: [PGType]) (cols :: [Column]) (x :: PGType) =
33+data Expression ps cols x =
3334 Expression { unExpression :: ByteString } deriving Show
3435
3536param1 :: Expression (p1:ps) cols p1
@@ -135,12 +136,24 @@ instance Num (Expression ps cols 'PGInt4) where
135136 abs (Expression x) = Expression $ "@" <> x
136137 signum (Expression x) = Expression $ "sign(" <> x <> ")"
137138
138−data Relation
139− (ps :: [PGType])
140− (xs :: [Column])
141− (db :: [Table])
142− (ys :: [Column])
143− = Relation { unRelation :: ByteString }
139+data Relation ps xs ys = Relation { unRelation :: ByteString } deriving Show
140+
141+star :: Relation ps xs xs
142+star = Relation "*"
143+
144+project :: Rec (Alias (Expression ps xs)) ys -> Relation ps xs ys
145+project
146+ = Relation
147+ . Char8.intercalate ", "
148+ . recordToList
149+ . rmap (Const . expressionString)
150+ where
151+ expressionString :: Alias (Expression ps xs) y -> ByteString
152+ expressionString = \case
153+ (As (Expression expr) :: Alias (Expression ps xs) y)
154+ -> expr <> " AS " <> Char8.pack (symbolVal (Proxy @(Name y)))
155+ (Elem :: Alias (Expression ps xs) y)
156+ -> Char8.pack (symbolVal (Proxy @(Name y)))
144157
145158-- star :: Relation db params xs xs
146159-- star = Relation "SELECT * FROM "
@@ -153,14 +166,14 @@ data Relation
153166-- table (_ :: Proxy tableName) = Relation $
154167-- "SELECT * FROM " <> Char8.pack (symbolVal (Proxy @tableName))
155168
156−starFrom
157− :: forall tableName tableCols db params columns
158− . Alias (Relation params columns db) (tableName ':= tableCols)
159− -> Relation params columns db tableCols
160−starFrom = \case
161− As x -> x
162− Elem -> Relation $
163− "SELECT * FROM " <> Char8.pack (symbolVal (Proxy @tableName))
169+-- starFrom
170+-- :: forall tableName tableCols db params columns
171+-- . Alias (Relation params columns db) (tableName ':= tableCols)
172+-- -> Relation params columns db tableCols
173+-- starFrom = \case
174+-- As x -> x
175+-- Elem -> Relation $
176+-- "SELECT * FROM " <> Char8.pack (symbolVal (Proxy @tableName))
164177
165178-- from
166179-- :: Relation db params ys zs
@@ -190,5 +203,5 @@ newtype Query
190203newtype PreparedQuery db0 db1 ps xs
191204 = PreparedQuery { unPreparedQuery :: ByteString }
192205
193−select :: Relation ps ('[] :: [Column]) db xs -> Query db db ps xs
194−select (Relation x) = Query x
206+-- select :: Relation ps ('[] :: [Column]) db xs -> Query db db ps xs
207+-- select (Relation x) = Query x