on dev help sign in
tabulate
bc895f9Eitan Chatav
squeel-postgresql/src/Squeel/PostgreSQL/Query.hs+28−20
@@ -147,38 +147,46 @@ project
147 -> Projection ps xs ys
147 -> Projection ps xs ys
148project = UnsafeProjection . renderAllAliased renderExpression
148project = UnsafeProjection . renderAllAliased renderExpression
149
149
150−newtype Relation ps xss ys = UnsafeRelation
150+data Tabulation ps xss xs = UnsafeTabulation
151− { renderRelation :: ByteString }
151+ { renderTabulation :: ByteString }
152
152
153−instance HasField label xss xs
153+data Relation ps xss xs = Relation
154− => IsLabel label (Relation ps xss xs) where
154+ { tabulation :: Tabulation ps xss xs
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
161 , restriction :: Maybe (Expression ps xs 'PGBool)
155 , restriction :: Maybe (Expression ps xs 'PGBool)
162 }
156 }
163
157
164−renderSelection :: Selection ps xss xs ys -> ByteString
158+renderRelation :: Relation ps xss xs -> ByteString
165−renderSelection (Selection xs ys wh) =
159+renderRelation (Relation tab wh) =
166− renderProjection xs <> " FROM " <> renderRelation ys
167− <> maybe "" ((" WHERE " <>) . renderExpression) wh
160+ renderTabulation tab <> 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
171
161
172where_
162where_
173− :: Selection ps xss xs ys
163+ :: Relation ps xss xs
174 -> Expression ps xs 'PGBool
164 -> Expression ps xs 'PGBool
175− -> Selection ps xss xs ys
165+ -> Relation ps xss xs
176ys `where_` condition = ys
166ys `where_` condition = ys
177 { restriction = case restriction ys of
167 { restriction = case restriction ys of
178 Nothing -> Just condition
168 Nothing -> Just condition
179− Just conditions -> Just (condition &&* conditions)
169+ Just conditions -> Just (conditions &&* condition)
180 }
170 }
181
171
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+
182newtype Query ps xss yss zs = UnsafeQuery { renderQuery :: ByteString }
187newtype Query ps xss yss zs = UnsafeQuery { renderQuery :: ByteString }
183newtype PreparedQuery ps xss yss zs =
188newtype PreparedQuery ps xss yss zs =
184 UnsafePreparedQuery { renderPreparedQuery :: ByteString }
189 UnsafePreparedQuery { renderPreparedQuery :: ByteString }
190+
191+select :: Selection ps xss ys -> Query ps xss xss ys
192+select selection = UnsafeQuery $ "SELECT " <> renderSelection selection