on dev help sign in
remove `db` param from `Relation`
4d6fd6cEitan Chatav
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+31−18
@@ -22,14 +22,15 @@ import Data.ByteString (ByteString)
22import Data.Monoid
22import Data.Monoid
23import Data.Proxy
23import Data.Proxy
24import Data.String
24import Data.String
25+import Data.Vinyl
25−-- import Data.Vinyl
26+import Data.Vinyl.Functor
26import GHC.TypeLits
27import GHC.TypeLits
27
28
28import qualified Data.ByteString.Char8 as Char8
29import qualified Data.ByteString.Char8 as Char8
29
30
30import Squeel.PostgreSQL.Type
31import Squeel.PostgreSQL.Type
31
32
32−data Expression (ps :: [PGType]) (cols :: [Column]) (x :: PGType) =
33+data Expression ps cols x =
33 Expression { unExpression :: ByteString } deriving Show
34 Expression { unExpression :: ByteString } deriving Show
34
35
35param1 :: Expression (p1:ps) cols p1
36param1 :: Expression (p1:ps) cols p1
@@ -135,12 +136,24 @@ instance Num (Expression ps cols 'PGInt4) where
135 abs (Expression x) = Expression $ "@" <> x
136 abs (Expression x) = Expression $ "@" <> x
136 signum (Expression x) = Expression $ "sign(" <> x <> ")"
137 signum (Expression x) = Expression $ "sign(" <> x <> ")"
137
138
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)))
144
157
145-- star :: Relation db params xs xs
158-- star :: Relation db params xs xs
146-- star = Relation "SELECT * FROM "
159-- star = Relation "SELECT * FROM "
@@ -153,14 +166,14 @@ data Relation
153-- table (_ :: Proxy tableName) = Relation $
166-- table (_ :: Proxy tableName) = Relation $
154-- "SELECT * FROM " <> Char8.pack (symbolVal (Proxy @tableName))
167-- "SELECT * FROM " <> Char8.pack (symbolVal (Proxy @tableName))
155
168
156−starFrom
169+-- starFrom
157− :: forall tableName tableCols db params columns
170+-- :: forall tableName tableCols db params columns
158− . Alias (Relation params columns db) (tableName ':= tableCols)
171+-- . Alias (Relation params columns db) (tableName ':= tableCols)
159− -> Relation params columns db tableCols
172+-- -> Relation params columns db tableCols
160−starFrom = \case
173+-- starFrom = \case
161− As x -> x
174+-- As x -> x
162− Elem -> Relation $
175+-- Elem -> Relation $
163− "SELECT * FROM " <> Char8.pack (symbolVal (Proxy @tableName))
176+-- "SELECT * FROM " <> Char8.pack (symbolVal (Proxy @tableName))
164
177
165-- from
178-- from
166-- :: Relation db params ys zs
179-- :: Relation db params ys zs
@@ -190,5 +203,5 @@ newtype Query
190newtype PreparedQuery db0 db1 ps xs
203newtype PreparedQuery db0 db1 ps xs
191 = PreparedQuery { unPreparedQuery :: ByteString }
204 = PreparedQuery { unPreparedQuery :: ByteString }
192
205
193−select :: Relation ps ('[] :: [Column]) db xs -> Query db db ps xs
206+-- select :: Relation ps ('[] :: [Column]) db xs -> Query db db ps xs
194−select (Relation x) = Query x
207+-- select (Relation x) = Query x