on dev help sign in
Selection
commit0b5668bc1c585f28b330355c7ed1bf349a0ee10c authorEitan Chatav <eitan.chatav@gmail.com> dateThu 20 Apr 2017, 02:23 (10 years ago) parent5763a2a
1 file changed+24−4
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+24−4
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+24−4
@@ -64,10 +64,6 @@ column_ (_ :: Proxy ident) = UnsafeExpression $
6464data Identified ident x = (:=) ident x
6565data Aliased obj named where AS :: obj x -> Aliased obj (name ':= x)
6666
67−-- class KnownAlias named obj where
68−-- type Alias named
69−-- renderAliased :: (obj x -> ByteString) -> Aliased obj named
70−
7167unalias :: Aliased obj (name ':= x) -> obj x
7268unalias (AS obj) = obj
7369
@@ -170,6 +166,30 @@ table
170166 => Aliased (Relation ps xss) (ident ':= xs)
171167table = AS $ table_ (Proxy @ident)
172168
169+data Selection ps xss xs ys = Selection
170+ { projection :: Projection ps xs ys
171+ , relation :: Relation ps xss xs
172+ , restriction :: Maybe (Expression ps xs 'PGBool)
173+ }
174+
175+renderSelection :: Selection ps xss xs ys -> ByteString
176+renderSelection (Selection xs ys wh) =
177+ renderProjection xs <> " FROM " <> renderRelation ys
178+ <> maybe "" ((" WHERE " <>) . renderExpression) wh
179+
180+from :: Projection ps xs ys -> Relation ps xss xs -> Selection ps xss xs ys
181+ys `from` xs = Selection ys xs Nothing
182+
183+where_
184+ :: Selection ps xss xs ys
185+ -> Expression ps xs 'PGBool
186+ -> Selection ps xss xs ys
187+ys `where_` condition = ys
188+ { restriction = case restriction ys of
189+ Nothing -> Just condition
190+ Just conditions -> Just (condition &&* conditions)
191+ }
192+
173193newtype Query ps xss yss zs = UnsafeQuery { renderQuery :: ByteString }
174194newtype PreparedQuery ps xss yss zs =
175195 UnsafePreparedQuery { renderPreparedQuery :: ByteString }