@@ -6,8 +6,6 @@
6 , LambdaCase
6 , LambdaCase
7 , MagicHash
7 , MagicHash
8 , OverloadedStrings
8 , OverloadedStrings
10− , ScopedTypeVariables
11 , TypeFamilies
9 , TypeFamilies
12 , TypeOperators
10 , TypeOperators
13 , UndecidableInstances
11 , UndecidableInstances
@@ -62,8 +60,8 @@ instance {-# OVERLAPPING #-} KnownSymbol column
62instance {-# OVERLAPPABLE #-} (KnownSymbol column, HasColumn column table ty)
60instance {-# OVERLAPPABLE #-} (KnownSymbol column, HasColumn column table ty)
63 => HasColumn column (ty' ': table) ty
61 => HasColumn column (ty' ': table) ty
65−instance HasColumn column columns ty
63+instance (HasColumn column columns ty, tables ~ '[table ::: columns])
66− => IsLabel column (Expression params '[table ::: columns] ty) where
64+ => IsLabel column (Expression params tables ty) where
67 fromLabel = getColumn
65 fromLabel = getColumn
@@ -167,18 +165,18 @@ crossJoin
167 :: TableRef params schema '[table]
165 :: TableRef params schema '[table]
168 -> TableRef params schema tables
166 -> TableRef params schema tables
169 -> TableRef params schema (table ': tables)
167 -> TableRef params schema (table ': tables)
170−crossJoin table tables = UnsafeTableRef $
168+crossJoin tab tabs = UnsafeTableRef $
171− renderTableRef tables <> " CROSS JOIN " <> renderTableRef table
169+ renderTableRef tabs <> " CROSS JOIN " <> renderTableRef tab
174 :: TableRef params schema '[table]
172 :: TableRef params schema '[table]
175 -> Expression params (table ': tables) 'PGBool
173 -> Expression params (table ': tables) 'PGBool
176 -> TableRef params schema tables
174 -> TableRef params schema tables
177 -> TableRef params schema (table ': tables)
175 -> TableRef params schema (table ': tables)
178−innerJoin table on tables = UnsafeTableRef $
176+innerJoin tab on tabs = UnsafeTableRef $
179− renderTableRef tables
177+ renderTableRef tabs
180 <> " INNER JOIN " <>
178 <> " INNER JOIN " <>
181− renderTableRef table
179+ renderTableRef tab
182 <> " ON " <>
180 <> " ON " <>
183 renderExpression on
181 renderExpression on
@@ -206,81 +204,110 @@ project
206 . recordToList
204 . recordToList
207 . rmap (Const . renderAliased renderExpression)
205 . rmap (Const . renderAliased renderExpression)
209−data Tabulation
207+data Clauses params tables = Clauses
208+ { whereClause :: Maybe (Expression params tables 'PGBool)
209+ , limitClause :: Maybe (Expression params '[] 'PGInt8)
210+ , offsetClause :: Maybe (Expression params '[] 'PGInt8)
213+instance Monoid (Clauses params tables) where
214+ mempty = Clauses Nothing Nothing Nothing
215+ Clauses wh1 lim1 off1 `mappend` Clauses wh2 lim2 off2 = Clauses
216+ { whereClause = case (wh1,wh2) of
217+ (Nothing,Nothing) -> Nothing
218+ (Just w1,Nothing) -> Just w1
219+ (Nothing,Just w2) -> Just w2
220+ (Just w1,Just w2) -> Just (w1 &&* w2)
221+ , limitClause = case (lim1,lim2) of
222+ (Nothing,Nothing) -> Nothing
223+ (Just l1,Nothing) -> Just l1
224+ (Nothing,Just l2) -> Just l2
225+ (Just l1,Just l2) -> Just (l1 `minB` l2)
226+ , offsetClause = case (off1,off2) of
227+ (Nothing,Nothing) -> Nothing
228+ (Just o1,Nothing) -> Just o1
229+ (Nothing,Just o2) -> Just o2
230+ (Just o1,Just o2) -> Just (o1 + o2)
210 (params :: [PGType])
234 (params :: [PGType])
211 (schema :: [(Symbol,[(Symbol,PGType)])])
235 (schema :: [(Symbol,[(Symbol,PGType)])])
212 (tables :: [(Symbol,[(Symbol,PGType)])])
236 (tables :: [(Symbol,[(Symbol,PGType)])])
213− = Tabulation
237+ = TableExpression
214− { fromClause :: TableRef params schema tables
238+ { tableRef :: TableRef params schema tables
215− , whereClause :: Maybe (Expression params tables 'PGBool)
239+ , clauses :: Clauses params tables
216− , limitClause :: Maybe (Expression params '[] 'PGInt8)
217− , offsetClause :: Maybe (Expression params '[] 'PGInt8)
220−tabulation :: TableRef params schema tables -> Tabulation params schema tables
243+ :: TableRef params schema tables
221−tabulation tab = Tabulation tab Nothing Nothing Nothing
244+ -> TableExpression params schema tables
245+tables tref = TableExpression tref mempty
223−renderTabulation :: Tabulation params schema tables -> ByteString
247+renderTabulation :: TableExpression params schema tables -> ByteString
224−renderTabulation (Tabulation fr wh lim off)= mconcat
248+renderTabulation (TableExpression tref (Clauses wh lim off))= mconcat
225− [ renderTableRef fr
249+ [ renderTableRef tref
226 , maybe "" ((" WHERE " <>) . renderExpression) wh
250 , maybe "" ((" WHERE " <>) . renderExpression) wh
227 , maybe "" ((" LIMIT " <>) . renderExpression) lim
251 , maybe "" ((" LIMIT " <>) . renderExpression) lim
228 , maybe "" ((" OFFSET " <>) . renderExpression) off
252 , maybe "" ((" OFFSET " <>) . renderExpression) off
231−instance HasTable table schema columns
255+instance (HasTable table schema columns, table ~ table')
232− => IsLabel table (TableRef params schema '[table ::: columns]) where
256+ => IsLabel table (TableRef params schema '[table' ::: columns]) where
233 fromLabel = getTable
257 fromLabel = getTable
235−instance HasTable table schema columns
259+instance (HasTable table schema columns, table ~ table')
236− => IsLabel table (Tabulation params schema '[table ::: columns]) where
260+ => IsLabel table (TableExpression params schema '[table' ::: columns]) where
237− fromLabel p = Tabulation (getTable p) Nothing Nothing Nothing
261+ fromLabel p = TableExpression (getTable p) mempty
240 :: Expression params tables 'PGBool
264 :: Expression params tables 'PGBool
241− -> Tabulation params schema tables
265+ -> TableExpression params schema tables
242− -> Tabulation params schema tables
266+ -> TableExpression params schema tables
243−where_ condition tables = tables
267+where_ wh (TableExpression tabs clauses1) = TableExpression tabs
244− { whereClause = case whereClause tables of
268+ (clauses1 <> Clauses (Just wh) Nothing Nothing)
245− Nothing -> Just condition
246− Just conditions -> Just (conditions &&* condition)
250 :: Expression params '[] 'PGInt8
271 :: Expression params '[] 'PGInt8
251− -> Tabulation params schema tables
272+ -> TableExpression params schema tables
252− -> Tabulation params schema tables
273+ -> TableExpression params schema tables
253−limit n tables = tables
274+limit lim (TableExpression tabs clauses1) = TableExpression tabs
254− { limitClause = case limitClause tables of
275+ (clauses1 <> Clauses Nothing (Just lim) Nothing)
256− Just n' -> Just (n' `minB` n)
260 :: Expression params '[] 'PGInt8
278 :: Expression params '[] 'PGInt8
261− -> Tabulation params schema tables
279+ -> TableExpression params schema tables
262− -> Tabulation params schema tables
280+ -> TableExpression params schema tables
263−offset n tables = tables
281+offset off (TableExpression tabs clauses1) = TableExpression tabs
264− { offsetClause = case offsetClause tables of
282+ (clauses1 <> Clauses Nothing Nothing (Just off))
266− Just n' -> Just (n' + n)
269−newtype Selection params schema columns = UnsafeSelection
284+newtype Selection
285+ (params :: [PGType])
286+ (schema :: [(Symbol,[(Symbol,PGType)])])
287+ (columns :: [(Symbol,PGType)])
270− { renderSelection :: ByteString }
289+ { renderSelection :: ByteString }
292+ :: TableExpression params schema tables
273− :: Projection params tables columns
293+ -> Projection params tables columns
274− -> Tabulation params schema tables
275 -> Selection params schema columns
294 -> Selection params schema columns
276−from projection tables = UnsafeSelection $
295+from tabs projection = UnsafeSelection $
277− renderProjection projection <> " FROM " <> renderTabulation tables
296+ renderProjection projection <> " FROM " <> renderTabulation tabs
279−newtype Query params schema0 schema1 columns = UnsafeQuery
298+newtype Query
299+ (params :: [PGType])
300+ (schema0 :: [(Symbol,[(Symbol,PGType)])])
301+ (schema1 :: [(Symbol,[(Symbol,PGType)])])
302+ (columns :: [(Symbol,PGType)])
280− { renderQuery :: ByteString }
303+ = UnsafeQuery { renderQuery :: ByteString }
282−newtype PreparedQuery params schema0 schema1 columns = UnsafePreparedQuery
305+newtype PreparedQuery
306+ (params :: [PGType])
307+ (schema0 :: [(Symbol,[(Symbol,PGType)])])
308+ (schema1 :: [(Symbol,[(Symbol,PGType)])])
309+ (columns :: [(Symbol,PGType)])
283− { renderPreparedQuery :: ByteString }
310+ = UnsafePreparedQuery { renderPreparedQuery :: ByteString }
286 :: Selection params schema columns
313 :: Selection params schema columns
@@ -289,18 +316,15 @@ select = UnsafeQuery . ("SELECT " <>) . (<> ";") . renderSelection
291 :: Aliased (Selection params schema) table
318 :: Aliased (Selection params schema) table
292− -> Tabulation params schema '[table]
319+ -> TableExpression params schema '[table]
293−subselect selection = Tabulation
320+subselect selection = TableExpression
294− { fromClause = UnsafeTableRef . ("SELECT " <>) $
321+ { tableRef = UnsafeTableRef . ("SELECT " <>) $
295 renderAliased renderSelection selection
322 renderAliased renderSelection selection
296− , whereClause = Nothing
323+ , clauses = mempty
297− , limitClause = Nothing
298− , offsetClause = Nothing
301insertInto
326insertInto
302− :: forall schema columns table params
303− . HasTable table schema columns
327+ :: HasTable table schema columns
304 => Alias table
328 => Alias table
305 -> Rec (Aliased (Expression params '[])) columns
329 -> Rec (Aliased (Expression params '[])) columns
306 -> Query params schema schema '[]
330 -> Query params schema schema '[]