@@ -147,38 +147,46 @@ project
147147 -> Projection ps xs ys
148148project = UnsafeProjection . renderAllAliased renderExpression
149149
150−newtype Relation ps xss ys = UnsafeRelation
151− { renderRelation :: ByteString }
150+data Tabulation ps xss xs = UnsafeTabulation
151+ { renderTabulation :: ByteString }
152152
153−instance HasField label xss xs
154− => IsLabel label (Relation ps xss xs) where
155− fromLabel _ = UnsafeRelation $
156− fieldName (Proxy @label) (Proxy @xss) (Proxy @xs)
157−
158−data Selection ps xss xs ys = Selection
159− { projection :: Projection ps xs ys
160− , relation :: Relation ps xss xs
153+data Relation ps xss xs = Relation
154+ { tabulation :: Tabulation ps xss xs
161155 , restriction :: Maybe (Expression ps xs 'PGBool)
162156 }
163157
164−renderSelection :: Selection ps xss xs ys -> ByteString
165−renderSelection (Selection xs ys wh) =
166− renderProjection xs <> " FROM " <> renderRelation ys
167− <> maybe "" ((" WHERE " <>) . renderExpression) wh
168−
169−from :: Projection ps xs ys -> Relation ps xss xs -> Selection ps xss xs ys
170−ys `from` xs = Selection ys xs Nothing
158+renderRelation :: Relation ps xss xs -> ByteString
159+renderRelation (Relation tab wh) =
160+ renderTabulation tab <> maybe "" ((" WHERE " <>) . renderExpression) wh
171161
172162where_
173− :: Selection ps xss xs ys
163+ :: Relation ps xss xs
174164 -> Expression ps xs 'PGBool
175− -> Selection ps xss xs ys
165+ -> Relation ps xss xs
176166ys `where_` condition = ys
177167 { restriction = case restriction ys of
178168 Nothing -> Just condition
179− Just conditions -> Just (condition &&* conditions)
169+ Just conditions -> Just (conditions &&* condition)
180170 }
181171
172+instance HasField label xss xs
173+ => IsLabel label (Relation ps xss xs) where
174+ fromLabel _ = Relation
175+ { tabulation = UnsafeTabulation $
176+ fieldName (Proxy @label) (Proxy @xss) (Proxy @xs)
177+ , restriction = Nothing
178+ }
179+
180+newtype Selection ps xss ys = UnsafeSelection
181+ { renderSelection :: ByteString }
182+
183+from :: Projection ps xs ys -> Relation ps xss xs -> Selection ps xss ys
184+ys `from` xs = UnsafeSelection $
185+ renderProjection ys <> " FROM " <> renderRelation xs
186+
182187newtype Query ps xss yss zs = UnsafeQuery { renderQuery :: ByteString }
183188newtype PreparedQuery ps xss yss zs =
184189 UnsafePreparedQuery { renderPreparedQuery :: ByteString }
190+
191+select :: Selection ps xss ys -> Query ps xss xss ys
192+select selection = UnsafeQuery $ "SELECT " <> renderSelection selection