on dev help sign in
RenderSQL in manipuations
1609239Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Manipulation.hs+65−78
@@ -43,9 +43,6 @@ module Squeal.PostgreSQL.Manipulation
43 -- * Insert
43 -- * Insert
44 , insertInto
44 , insertInto
45 , insertInto_
45 , insertInto_
46− , renderConflictClause
47− , renderConflictTarget
48− , renderConflictAction
49 -- * Update
46 -- * Update
50 , update
47 , update
51 , update_
48 , update_
@@ -206,14 +203,14 @@ instance RenderSQL (Manipulation db params columns) where
206instance With Manipulation where
203instance With Manipulation where
207 with Done manip = manip
204 with Done manip = manip
208 with (cte :>> ctes) manip = UnsafeManipulation $
205 with (cte :>> ctes) manip = UnsafeManipulation $
209− "WITH" <+> renderCommonTableExpressions renderManipulation cte ctes
206+ "WITH" <+> renderCommonTableExpressions renderSQL cte ctes
210− <+> renderManipulation manip
207+ <+> renderSQL manip
211
208
212-- | Convert a `Query` into a `Manipulation`.
209-- | Convert a `Query` into a `Manipulation`.
213queryStatement
210queryStatement
214 :: Query db params columns
211 :: Query db params columns
215 -> Manipulation db params columns
212 -> Manipulation db params columns
216−queryStatement q = UnsafeManipulation $ renderQuery q
213+queryStatement q = UnsafeManipulation $ renderSQL q
217
214
218{-----------------------------------------
215{-----------------------------------------
219INSERT statements
216INSERT statements
@@ -241,8 +238,8 @@ insertInto
241 -> Manipulation db params row1
238 -> Manipulation db params row1
242insertInto tab qry conflict ret = UnsafeManipulation $
239insertInto tab qry conflict ret = UnsafeManipulation $
243 "INSERT" <+> "INTO" <+> renderSQL tab
240 "INSERT" <+> "INTO" <+> renderSQL tab
244− <+> renderQueryClause qry
241+ <+> renderSQL qry
245− <> renderConflictClause conflict
242+ <> renderSQL conflict
246 <> renderSQL ret
243 <> renderSQL ret
247
244
248insertInto_
245insertInto_
@@ -274,39 +271,37 @@ data QueryClause db params columns where
274 => Query db params row
271 => Query db params row
275 -> QueryClause db params columns
272 -> QueryClause db params columns
276
273
277−renderQueryClause
278− :: QueryClause db params columns
274+instance RenderSQL (QueryClause db params columns) where
279− -> ByteString
275+ renderSQL = \case
280−renderQueryClause = \case
281− Values row0 rows ->
276+ Values row0 rows ->
282− parenthesized (renderCommaSeparated renderSQLPart row0)
277+ parenthesized (renderCommaSeparated renderSQLPart row0)
283− <+> "VALUES"
278+ <+> "VALUES"
284− <+> commaSeparated
279+ <+> commaSeparated
285− ( parenthesized
280+ ( parenthesized
286− . renderCommaSeparated renderValuePart <$> row0 : rows )
281+ . renderCommaSeparated renderValuePart <$> row0 : rows )
287− Select row0 tab ->
282+ Select row0 tab ->
288− parenthesized (renderCommaSeparatedMaybe renderSQLPartMaybe row0)
283+ parenthesized (renderCommaSeparatedMaybe renderSQLPartMaybe row0)
289− <+> "SELECT"
284+ <+> "SELECT"
290− <+> renderCommaSeparatedMaybe renderValuePartMaybe row0
285+ <+> renderCommaSeparatedMaybe renderValuePartMaybe row0
291− <+> renderTableExpression tab
286+ <+> renderSQL tab
292− Subquery qry -> renderQuery qry
287+ Subquery qry -> renderQuery qry
293− where
288+ where
294− renderSQLPartMaybe, renderValuePartMaybe
289+ renderSQLPartMaybe, renderValuePartMaybe
295− :: ColumnExpression db params grp from column -> Maybe ByteString
290+ :: ColumnExpression db params grp from column -> Maybe ByteString
296− renderSQLPartMaybe = \case
291+ renderSQLPartMaybe = \case
297− DefaultAs _ -> Nothing
292+ DefaultAs _ -> Nothing
298− Specific (_ `As` name) -> Just $ renderSQL name
293+ Specific (_ `As` name) -> Just $ renderSQL name
299− renderValuePartMaybe = \case
294+ renderValuePartMaybe = \case
300− DefaultAs _ -> Nothing
295+ DefaultAs _ -> Nothing
301− Specific (value `As` _) -> Just $ renderExpression value
296+ Specific (value `As` _) -> Just $ renderExpression value
302− renderSQLPart, renderValuePart
297+ renderSQLPart, renderValuePart
303− :: ColumnExpression db params grp from column -> ByteString
298+ :: ColumnExpression db params grp from column -> ByteString
304− renderSQLPart = \case
299+ renderSQLPart = \case
305− DefaultAs name -> renderSQL name
300+ DefaultAs name -> renderSQL name
306− Specific (_ `As` name) -> renderSQL name
301+ Specific (_ `As` name) -> renderSQL name
307− renderValuePart = \case
302+ renderValuePart = \case
308− DefaultAs _ -> "DEFAULT"
303+ DefaultAs _ -> "DEFAULT"
309− Specific (value `As` _) -> renderExpression value
304+ Specific (value `As` _) -> renderExpression value
310
305
311pattern Values_
306pattern Values_
312 :: SOP.SListI columns
307 :: SOP.SListI columns
@@ -389,14 +384,12 @@ instance
389 (NP (ColumnExpression db params ('Grouped bys) from) columns) where
384 (NP (ColumnExpression db params ('Grouped bys) from) columns) where
390 tab ! col = tab ! col :* Nil
385 tab ! col = tab ! col :* Nil
391
386
392−renderColumnExpression
393− :: ColumnExpression db params grp from column
387+instance RenderSQL (ColumnExpression db params grp from column) where
394− -> ByteString
388+ renderSQL = \case
395−renderColumnExpression = \case
396− DefaultAs col ->
389+ DefaultAs col ->
397− renderSQL col <+> "=" <+> "DEFAULT"
390+ renderSQL col <+> "=" <+> "DEFAULT"
398− Specific (expression `As` col) ->
391+ Specific (expression `As` col) ->
399− renderSQL col <+> "=" <+> renderExpression expression
392+ renderSQL col <+> "=" <+> renderSQL expression
400
393
401-- | A `ReturningClause` computes and return value(s) based
394-- | A `ReturningClause` computes and return value(s) based
402-- on each row actually inserted, updated or deleted. This is primarily
395-- on each row actually inserted, updated or deleted. This is primarily
@@ -435,14 +428,12 @@ data ConflictClause tab db params table where
435 -> ConflictClause tab db params (constraints :=> columns)
428 -> ConflictClause tab db params (constraints :=> columns)
436
429
437-- | Render a `ConflictClause`.
430-- | Render a `ConflictClause`.
438−renderConflictClause
439− :: SOP.SListI (TableToColumns table)
431+instance SOP.SListI (TableToColumns table)
440− => ConflictClause tab db params table
432+ => RenderSQL (ConflictClause tab db params table) where
441− -> ByteString
433+ renderSQL = \case
442−renderConflictClause = \case
443− OnConflictDoRaise -> ""
434+ OnConflictDoRaise -> ""
444− OnConflict target action -> " ON CONFLICT"
435+ OnConflict target action -> " ON CONFLICT"
445− <+> renderConflictTarget target <+> renderConflictAction action
436+ <+> renderSQL target <+> renderSQL action
446
437
447data ConflictAction tab db params columns where
438data ConflictAction tab db params columns where
448 DoNothing :: ConflictAction tab db params columns
439 DoNothing :: ConflictAction tab db params columns
@@ -456,17 +447,15 @@ data ConflictAction tab db params columns where
456 -> [Condition db params 'Ungrouped '[tab ::: row, "excluded" ::: row]]
447 -> [Condition db params 'Ungrouped '[tab ::: row, "excluded" ::: row]]
457 -> ConflictAction tab db params columns
448 -> ConflictAction tab db params columns
458
449
459−renderConflictAction
460− :: ConflictAction tab db params columns
450+instance RenderSQL (ConflictAction tab db params columns) where
461− -> ByteString
451+ renderSQL = \case
462−renderConflictAction = \case
463− DoNothing -> "DO NOTHING"
452+ DoNothing -> "DO NOTHING"
464− DoUpdate updates whs'
453+ DoUpdate updates whs'
465− -> "DO UPDATE SET"
454+ -> "DO UPDATE SET"
466− <+> renderCommaSeparated renderColumnExpression updates
455+ <+> renderCommaSeparated renderSQL updates
467− <> case whs' of
456+ <> case whs' of
468− [] -> ""
457+ [] -> ""
469− wh:whs -> " WHERE" <+> renderExpression (foldr (.&&) wh whs)
458+ wh:whs -> " WHERE" <+> renderSQL (foldr (.&&) wh whs)
470
459
471-- | A `ConflictTarget` specifies the constraint violation that triggers a
460-- | A `ConflictTarget` specifies the constraint violation that triggers a
472-- `ConflictAction`.
461-- `ConflictAction`.
@@ -477,11 +466,9 @@ data ConflictTarget constraints where
477 -> ConflictTarget constraints
466 -> ConflictTarget constraints
478
467
479-- | Render a `ConflictTarget`
468-- | Render a `ConflictTarget`
480−renderConflictTarget
481− :: ConflictTarget constraints
469+instance RenderSQL (ConflictTarget constraints) where
482− -> ByteString
483−renderConflictTarget (OnConstraint con) =
470+ renderSQL (OnConstraint con) =
484− "ON" <+> "CONSTRAINT" <+> renderSQL con
471+ "ON" <+> "CONSTRAINT" <+> renderSQL con
485
472
486{-----------------------------------------
473{-----------------------------------------
487UPDATE statements
474UPDATE statements
@@ -510,8 +497,8 @@ update tab columns wh returning = UnsafeManipulation $
510 "UPDATE"
497 "UPDATE"
511 <+> renderSQL tab
498 <+> renderSQL tab
512 <+> "SET"
499 <+> "SET"
513− <+> renderCommaSeparated renderColumnExpression columns
500+ <+> renderCommaSeparated renderSQL columns
514− <+> "WHERE" <+> renderExpression wh
501+ <+> "WHERE" <+> renderSQL wh
515 <> renderSQL returning
502 <> renderSQL returning
516
503
517-- | Update a row returning `Nil`.
504-- | Update a row returning `Nil`.
@@ -561,8 +548,8 @@ deleteFrom tab using wh returning = UnsafeManipulation $
561 <+> renderSQL tab
548 <+> renderSQL tab
562 <> case using of
549 <> case using of
563 NoUsing -> ""
550 NoUsing -> ""
564− Using tables -> " USING" <+> renderFromClause tables
551+ Using from -> " USING" <+> renderSQL from
565− <+> "WHERE" <+> renderExpression wh
552+ <+> "WHERE" <+> renderSQL wh
566 <> renderSQL returning
553 <> renderSQL returning
567
554
568-- | Delete rows returning `Nil`.
555-- | Delete rows returning `Nil`.
@@ -591,4 +578,4 @@ also
591 -- ^ left
578 -- ^ left
592 -> FromClause schema params (Join left right)
579 -> FromClause schema params (Join left right)
593also right left = UnsafeFromClause $
580also right left = UnsafeFromClause $
594− renderFromClause left <> "," <+> renderFromClause right
581+ renderSQL left <> "," <+> renderSQL right
squeal-postgresql/src/Squeal/PostgreSQL/Query.hs+59−63
@@ -69,7 +69,6 @@ module Squeal.PostgreSQL.Query
69 , jsonbToRecordSet
69 , jsonbToRecordSet
70 -- * Table Expressions
70 -- * Table Expressions
71 , TableExpression (..)
71 , TableExpression (..)
72− , renderTableExpression
73 , from
72 , from
74 , where_
73 , where_
75 , groupBy
74 , groupBy
@@ -90,7 +89,6 @@ module Squeal.PostgreSQL.Query
90 , fullOuterJoin
89 , fullOuterJoin
91 -- * Grouping
90 -- * Grouping
92 , By (..)
91 , By (..)
93− , renderBy
94 , GroupByClause (..)
92 , GroupByClause (..)
95 , HavingClause (..)
93 , HavingClause (..)
96 -- * Sorting
94 -- * Sorting
@@ -329,9 +327,9 @@ union
329 -> Query db params columns
327 -> Query db params columns
330 -> Query db params columns
328 -> Query db params columns
331q1 `union` q2 = UnsafeQuery $
329q1 `union` q2 = UnsafeQuery $
332− parenthesized (renderQuery q1)
330+ parenthesized (renderSQL q1)
333 <+> "UNION"
331 <+> "UNION"
334− <+> parenthesized (renderQuery q2)
332+ <+> parenthesized (renderSQL q2)
335
333
336-- | The results of two queries can be combined using the set operation
334-- | The results of two queries can be combined using the set operation
337-- `unionAll`, the disjoint union. Duplicate rows are retained.
335-- `unionAll`, the disjoint union. Duplicate rows are retained.
@@ -340,9 +338,9 @@ unionAll
340 -> Query db params columns
338 -> Query db params columns
341 -> Query db params columns
339 -> Query db params columns
342q1 `unionAll` q2 = UnsafeQuery $
340q1 `unionAll` q2 = UnsafeQuery $
343− parenthesized (renderQuery q1)
341+ parenthesized (renderSQL q1)
344 <+> "UNION" <+> "ALL"
342 <+> "UNION" <+> "ALL"
345− <+> parenthesized (renderQuery q2)
343+ <+> parenthesized (renderSQL q2)
346
344
347-- | The results of two queries can be combined using the set operation
345-- | The results of two queries can be combined using the set operation
348-- `intersect`, the intersection. Duplicate rows are eliminated.
346-- `intersect`, the intersection. Duplicate rows are eliminated.
@@ -351,9 +349,9 @@ intersect
351 -> Query db params columns
349 -> Query db params columns
352 -> Query db params columns
350 -> Query db params columns
353q1 `intersect` q2 = UnsafeQuery $
351q1 `intersect` q2 = UnsafeQuery $
354− parenthesized (renderQuery q1)
352+ parenthesized (renderSQL q1)
355 <+> "INTERSECT"
353 <+> "INTERSECT"
356− <+> parenthesized (renderQuery q2)
354+ <+> parenthesized (renderSQL q2)
357
355
358-- | The results of two queries can be combined using the set operation
356-- | The results of two queries can be combined using the set operation
359-- `intersectAll`, the intersection. Duplicate rows are retained.
357-- `intersectAll`, the intersection. Duplicate rows are retained.
@@ -362,9 +360,9 @@ intersectAll
362 -> Query db params columns
360 -> Query db params columns
363 -> Query db params columns
361 -> Query db params columns
364q1 `intersectAll` q2 = UnsafeQuery $
362q1 `intersectAll` q2 = UnsafeQuery $
365− parenthesized (renderQuery q1)
363+ parenthesized (renderSQL q1)
366 <+> "INTERSECT" <+> "ALL"
364 <+> "INTERSECT" <+> "ALL"
367− <+> parenthesized (renderQuery q2)
365+ <+> parenthesized (renderSQL q2)
368
366
369-- | The results of two queries can be combined using the set operation
367-- | The results of two queries can be combined using the set operation
370-- `except`, the set difference. Duplicate rows are eliminated.
368-- `except`, the set difference. Duplicate rows are eliminated.
@@ -373,9 +371,9 @@ except
373 -> Query db params columns
371 -> Query db params columns
374 -> Query db params columns
372 -> Query db params columns
375q1 `except` q2 = UnsafeQuery $
373q1 `except` q2 = UnsafeQuery $
376− parenthesized (renderQuery q1)
374+ parenthesized (renderSQL q1)
377 <+> "EXCEPT"
375 <+> "EXCEPT"
378− <+> parenthesized (renderQuery q2)
376+ <+> parenthesized (renderSQL q2)
379
377
380-- | The results of two queries can be combined using the set operation
378-- | The results of two queries can be combined using the set operation
381-- `exceptAll`, the set difference. Duplicate rows are retained.
379-- `exceptAll`, the set difference. Duplicate rows are retained.
@@ -384,9 +382,9 @@ exceptAll
384 -> Query db params columns
382 -> Query db params columns
385 -> Query db params columns
383 -> Query db params columns
386q1 `exceptAll` q2 = UnsafeQuery $
384q1 `exceptAll` q2 = UnsafeQuery $
387− parenthesized (renderQuery q1)
385+ parenthesized (renderSQL q1)
388 <+> "EXCEPT" <+> "ALL"
386 <+> "EXCEPT" <+> "ALL"
389− <+> parenthesized (renderQuery q2)
387+ <+> parenthesized (renderSQL q2)
390
388
391{-----------------------------------------
389{-----------------------------------------
392SELECT queries
390SELECT queries
@@ -456,7 +454,7 @@ select
456select selection tabexpr = UnsafeQuery $
454select selection tabexpr = UnsafeQuery $
457 "SELECT"
455 "SELECT"
458 <+> renderSQL selection
456 <+> renderSQL selection
459− <+> renderTableExpression tabexpr
457+ <+> renderSQL tabexpr
460
458
461select_
459select_
462 :: (SListI row, row ~ (x ': xs))
460 :: (SListI row, row ~ (x ': xs))
@@ -477,7 +475,7 @@ selectDistinct
477selectDistinct selection tabexpr = UnsafeQuery $
475selectDistinct selection tabexpr = UnsafeQuery $
478 "SELECT DISTINCT"
476 "SELECT DISTINCT"
479 <+> renderSQL selection
477 <+> renderSQL selection
480− <+> renderTableExpression tabexpr
478+ <+> renderSQL tabexpr
481
479
482selectDistinct_
480selectDistinct_
483 :: (SListI columns, columns ~ (col ': cols))
481 :: (SListI columns, columns ~ (col ': cols))
@@ -511,11 +509,11 @@ values rw rws = UnsafeQuery $ "SELECT * FROM"
511 ( parenthesized
509 ( parenthesized
512 . renderCommaSeparated renderValuePart <$> rw:rws )
510 . renderCommaSeparated renderValuePart <$> rw:rws )
513 ) <+> "AS t"
511 ) <+> "AS t"
514− <+> parenthesized (renderCommaSeparated renderSQLPart rw)
512+ <+> parenthesized (renderCommaSeparated renderAliasPart rw)
515 where
513 where
516− renderSQLPart, renderValuePart
514+ renderAliasPart, renderValuePart
517 :: Aliased (Expression db params 'Ungrouped '[] ) ty -> ByteString
515 :: Aliased (Expression db params 'Ungrouped '[] ) ty -> ByteString
518− renderSQLPart (_ `As` name) = renderSQL name
516+ renderAliasPart (_ `As` name) = renderSQL name
519 renderValuePart (value `As` _) = renderSQL value
517 renderValuePart (value `As` _) = renderSQL value
520
518
521-- | `values_` computes a row value or set of row values
519-- | `values_` computes a row value or set of row values
@@ -585,34 +583,32 @@ data TableExpression
585 }
583 }
586
584
587-- | Render a `TableExpression`
585-- | Render a `TableExpression`
588−renderTableExpression
589− :: TableExpression db params grp from
586+instance RenderSQL (TableExpression db params grp from) where
590− -> ByteString
587+ renderSQL
591−renderTableExpression
592− (TableExpression frm' whs' grps' hvs' srts' lims' offs') = mconcat
588+ (TableExpression frm' whs' grps' hvs' srts' lims' offs') = mconcat
593− [ "FROM ", renderFromClause frm'
589+ [ "FROM ", renderSQL frm'
594− , renderWheres whs'
590+ , renderWheres whs'
595− , renderSQL grps'
591+ , renderSQL grps'
596− , renderSQL hvs'
592+ , renderSQL hvs'
597− , renderOrderByClause srts'
593+ , renderOrderByClause srts'
598− , renderLimits lims'
594+ , renderLimits lims'
599− , renderOffsets offs'
595+ , renderOffsets offs'
600− ]
596+ ]
601− where
597+ where
602− renderWheres = \case
598+ renderWheres = \case
603− [] -> ""
599+ [] -> ""
604− wh:[] -> " WHERE" <+> renderSQL wh
600+ wh:[] -> " WHERE" <+> renderSQL wh
605− wh:whs -> " WHERE" <+> renderSQL (foldr (.&&) wh whs)
601+ wh:whs -> " WHERE" <+> renderSQL (foldr (.&&) wh whs)
606− renderOrderByClause = \case
602+ renderOrderByClause = \case
607− [] -> ""
603+ [] -> ""
608− srts -> " ORDER BY"
604+ srts -> " ORDER BY"
609− <+> commaSeparated (renderSQL <$> srts)
605+ <+> commaSeparated (renderSQL <$> srts)
610− renderLimits = \case
606+ renderLimits = \case
611− [] -> ""
607+ [] -> ""
612− lims -> " LIMIT" <+> fromString (show (minimum lims))
608+ lims -> " LIMIT" <+> fromString (show (minimum lims))
613− renderOffsets = \case
609+ renderOffsets = \case
614− [] -> ""
610+ [] -> ""
615− offs -> " OFFSET" <+> fromString (show (sum offs))
611+ offs -> " OFFSET" <+> fromString (show (sum offs))
616
612
617-- | A `from` generates a `TableExpression` from a table reference that can be
613-- | A `from` generates a `TableExpression` from a table reference that can be
618-- a table name, or a derived table such as a subquery, a JOIN construct,
614-- a table name, or a derived table such as a subquery, a JOIN construct,
@@ -835,6 +831,8 @@ as a subquery, a @JOIN@ construct, or complex combinations of these.
835newtype FromClause db params from
831newtype FromClause db params from
836 = UnsafeFromClause { renderFromClause :: ByteString }
832 = UnsafeFromClause { renderFromClause :: ByteString }
837 deriving (GHC.Generic,Show,Eq,Ord,NFData)
833 deriving (GHC.Generic,Show,Eq,Ord,NFData)
834+instance RenderSQL (FromClause db params from) where
835+ renderSQL = renderFromClause
838
836
839-- | A real `table` is a table from the database.
837-- | A real `table` is a table from the database.
840table
838table
@@ -878,7 +876,7 @@ crossJoin
878 -- ^ left
876 -- ^ left
879 -> FromClause db params (Join left right)
877 -> FromClause db params (Join left right)
880crossJoin right left = UnsafeFromClause $
878crossJoin right left = UnsafeFromClause $
881− renderFromClause left <+> "CROSS JOIN" <+> renderFromClause right
879+ renderSQL left <+> "CROSS JOIN" <+> renderSQL right
882
880
883{- | @left & innerJoin right on@. The joined table is filtered by
881{- | @left & innerJoin right on@. The joined table is filtered by
884the @on@ condition.
882the @on@ condition.
@@ -892,7 +890,7 @@ innerJoin
892 -- ^ left
890 -- ^ left
893 -> FromClause db params (Join left right)
891 -> FromClause db params (Join left right)
894innerJoin right on left = UnsafeFromClause $
892innerJoin right on left = UnsafeFromClause $
895− renderFromClause left <+> "INNER JOIN" <+> renderFromClause right
893+ renderSQL left <+> "INNER JOIN" <+> renderSQL right
896 <+> "ON" <+> renderSQL on
894 <+> "ON" <+> renderSQL on
897
895
898{- | @left & leftOuterJoin right on@. First, an inner join is performed.
896{- | @left & leftOuterJoin right on@. First, an inner join is performed.
@@ -909,7 +907,7 @@ leftOuterJoin
909 -- ^ left
907 -- ^ left
910 -> FromClause db params (Join left (NullifyFrom right))
908 -> FromClause db params (Join left (NullifyFrom right))
911leftOuterJoin right on left = UnsafeFromClause $
909leftOuterJoin right on left = UnsafeFromClause $
912− renderFromClause left <+> "LEFT OUTER JOIN" <+> renderFromClause right
910+ renderSQL left <+> "LEFT OUTER JOIN" <+> renderSQL right
913 <+> "ON" <+> renderSQL on
911 <+> "ON" <+> renderSQL on
914
912
915{- | @left & rightOuterJoin right on@. First, an inner join is performed.
913{- | @left & rightOuterJoin right on@. First, an inner join is performed.
@@ -927,7 +925,7 @@ rightOuterJoin
927 -- ^ left
925 -- ^ left
928 -> FromClause db params (Join (NullifyFrom left) right)
926 -> FromClause db params (Join (NullifyFrom left) right)
929rightOuterJoin right on left = UnsafeFromClause $
927rightOuterJoin right on left = UnsafeFromClause $
930− renderFromClause left <+> "RIGHT OUTER JOIN" <+> renderFromClause right
928+ renderSQL left <+> "RIGHT OUTER JOIN" <+> renderSQL right
931 <+> "ON" <+> renderSQL on
929 <+> "ON" <+> renderSQL on
932
930
933{- | @left & fullOuterJoin right on@. First, an inner join is performed.
931{- | @left & fullOuterJoin right on@. First, an inner join is performed.
@@ -947,7 +945,7 @@ fullOuterJoin
947 -> FromClause db params
945 -> FromClause db params
948 (Join (NullifyFrom left) (NullifyFrom right))
946 (Join (NullifyFrom left) (NullifyFrom right))
949fullOuterJoin right on left = UnsafeFromClause $
947fullOuterJoin right on left = UnsafeFromClause $
950− renderFromClause left <+> "FULL OUTER JOIN" <+> renderFromClause right
948+ renderSQL left <+> "FULL OUTER JOIN" <+> renderSQL right
951 <+> "ON" <+> renderSQL on
949 <+> "ON" <+> renderSQL on
952
950
953{-----------------------------------------
951{-----------------------------------------
@@ -974,6 +972,10 @@ data By
974deriving instance Show (By from by)
972deriving instance Show (By from by)
975deriving instance Eq (By from by)
973deriving instance Eq (By from by)
976deriving instance Ord (By from by)
974deriving instance Ord (By from by)
975+instance RenderSQL (By from by) where
976+ renderSQL = \case
977+ By1 column -> renderSQL column
978+ By2 rel column -> renderSQL rel <> "." <> renderSQL column
977
979
978instance (HasUnique rel rels cols, Has col cols ty, by ~ '(rel, col))
980instance (HasUnique rel rels cols, Has col cols ty, by ~ '(rel, col))
979 => IsLabel col (By rels by) where fromLabel = By1 fromLabel
981 => IsLabel col (By rels by) where fromLabel = By1 fromLabel
@@ -985,12 +987,6 @@ instance (Has rel rels cols, Has col cols ty, bys ~ '[ '(rel, col)])
985 => IsQualified rel col (NP (By rels) bys) where
987 => IsQualified rel col (NP (By rels) bys) where
986 rel ! col = By2 rel col :* Nil
988 rel ! col = By2 rel col :* Nil
987
989
988−-- | Renders a `By`.
989−renderBy :: By from by -> ByteString
990−renderBy = \case
991− By1 column -> renderSQL column
992− By2 rel column -> renderSQL rel <> "." <> renderSQL column
993−
994-- | A `GroupByClause` indicates the `Grouping` of a `TableExpression`.
990-- | A `GroupByClause` indicates the `Grouping` of a `TableExpression`.
995-- A `NoGroups` indicates `Ungrouped` while a `Group` indicates `Grouped`.
991-- A `NoGroups` indicates `Ungrouped` while a `Group` indicates `Grouped`.
996-- @NoGroups@ is distinguised from @Group Nil@ since no aggregation can be
992-- @NoGroups@ is distinguised from @Group Nil@ since no aggregation can be
@@ -1009,7 +1005,7 @@ instance RenderSQL (GroupByClause grp from) where
1009 renderSQL = \case
1005 renderSQL = \case
1010 NoGroups -> ""
1006 NoGroups -> ""
1011 Group Nil -> ""
1007 Group Nil -> ""
1012− Group bys -> " GROUP BY" <+> renderCommaSeparated renderBy bys
1008+ Group bys -> " GROUP BY" <+> renderCommaSeparated renderSQL bys
1013
1009
1014-- | A `HavingClause` is used to eliminate groups that are not of interest.
1010-- | A `HavingClause` is used to eliminate groups that are not of interest.
1015-- An `Ungrouped` `TableExpression` may only use `NoHaving` while a `Grouped`
1011-- An `Ungrouped` `TableExpression` may only use `NoHaving` while a `Grouped`
@@ -1083,7 +1079,7 @@ unsafeSubqueryExpression
1083 -> Query db params '[alias ::: ty]
1079 -> Query db params '[alias ::: ty]
1084 -> Expression db params grp from (nullity 'PGbool)
1080 -> Expression db params grp from (nullity 'PGbool)
1085unsafeSubqueryExpression op x q = UnsafeExpression $
1081unsafeSubqueryExpression op x q = UnsafeExpression $
1086− renderSQL x <+> op <+> parenthesized (renderQuery q)
1082+ renderSQL x <+> op <+> parenthesized (renderSQL q)
1087
1083
1088unsafeRowSubqueryExpression
1084unsafeRowSubqueryExpression
1089 :: SListI row
1085 :: SListI row
@@ -1092,7 +1088,7 @@ unsafeRowSubqueryExpression
1092 -> Query db params row
1088 -> Query db params row
1093 -> Expression db params grp from (nullity 'PGbool)
1089 -> Expression db params grp from (nullity 'PGbool)
1094unsafeRowSubqueryExpression op xs q = UnsafeExpression $
1090unsafeRowSubqueryExpression op xs q = UnsafeExpression $
1095− renderSQL (row xs) <+> op <+> parenthesized (renderQuery q)
1091+ renderSQL (row xs) <+> op <+> parenthesized (renderSQL q)
1096
1092
1097-- | The right-hand side is a sub`Query`, which must return exactly one column.
1093-- | The right-hand side is a sub`Query`, which must return exactly one column.
1098-- The left-hand expression is evaluated and compared to each row of the
1094-- The left-hand expression is evaluated and compared to each row of the
@@ -1404,5 +1400,5 @@ class With statement where
1404instance With Query where
1400instance With Query where
1405 with Done query = query
1401 with Done query = query
1406 with (cte :>> ctes) query = UnsafeQuery $
1402 with (cte :>> ctes) query = UnsafeQuery $
1407− "WITH" <+> renderCommonTableExpressions renderQuery cte ctes
1403+ "WITH" <+> renderCommonTableExpressions renderSQL cte ctes
1408− <+> renderQuery query
1404+ <+> renderSQL query