@@ -1,1313 +0,0 @@
1−{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
2−{-# LANGUAGE
3− DataKinds
4− , FlexibleContexts
5− , FlexibleInstances
6− , FunctionalDependencies
7− , GADTs
8− , LambdaCase
9− , MagicHash
10− , OverloadedStrings
11− , PolyKinds
12− , RankNTypes
13− , ScopedTypeVariables
14− , TypeApplications
15− , TypeFamilies
16− , TypeOperators
17− , UndecidableInstances
18−#-}
19−
20−module Squeal.PostgreSQL.Statement where
21−
22−import Control.Category
23−import Data.ByteString (ByteString)
24−import Data.Foldable
25−import Data.Maybe
26−import Data.Monoid
27−import Data.Ratio
28−import Data.String
29−import Data.Word
30−import Generics.SOP
31−import GHC.Exts
32−import GHC.OverloadedLabels
33−import GHC.TypeLits
34−import Prelude hiding (RealFrac(..), id, (.))
35−
36−import qualified Data.ByteString as ByteString
37−
38−import Squeal.PostgreSQL.Schema
39−
40−{-----------------------------------------
41−column expressions
42−-----------------------------------------}
43−
44−newtype Expression
45− (params :: [ColumnType])
46− (tables :: [(Symbol,[(Symbol,ColumnType)])])
47− (grouping :: Grouping)
48− (ty :: ColumnType)
49− = UnsafeExpression { renderExpression :: ByteString }
50− deriving (Show,Eq)
51−
52−class (PGTyped (BaseType ty), KnownNat n)
53− => HasParameter (n :: Nat) params ty | n params -> ty where
54− param :: proxy n -> Expression params tables grouping ty
55− param _ = UnsafeExpression $ parenthesized $
56− "$" <> renderNat (Proxy @n) <+> "::"
57− <+> renderTypeExpression (pgtype @(BaseType ty))
58−instance {-# OVERLAPPING #-} PGTyped (BaseType ty1)
59− => HasParameter 1 (ty1:tys) ty1
60−instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
61− => HasParameter n (ty' : params) ty
62−
63−class KnownSymbol column => HasColumn column columns ty
64− | column columns -> ty where
65− getColumn
66− :: Alias column
67− -> Expression params '[table ::: columns] 'Ungrouped ty
68− getColumn column = UnsafeExpression $ renderAlias column
69−instance {-# OVERLAPPING #-} KnownSymbol column
70− => HasColumn column ((column ::: optionality ty) ': tys) ('Required ty)
71−instance {-# OVERLAPPABLE #-} (KnownSymbol column, HasColumn column table ty)
72− => HasColumn column (ty' ': table) ty
73−
74−instance (HasColumn column columns ty, tables ~ '[table ::: columns])
75− => IsLabel column (Expression params tables 'Ungrouped ty) where
76− fromLabel = getColumn (Alias @column)
77−
78−instance (HasTable table tables columns, HasColumn column columns ty)
79− => IsTableColumn table column (Expression params tables 'Ungrouped ty) where
80− table ! column = UnsafeExpression $
81− renderAlias table <> "." <> renderAlias column
82−
83−def :: Expression params '[] grouping ('Optional (nullity ty))
84−def = UnsafeExpression "DEFAULT"
85−
86−unDef
87− :: Expression params '[] grouping ('Required (nullity ty))
88− -> Expression params '[] grouping ('Optional (nullity ty))
89−unDef = UnsafeExpression . renderExpression
90−
91−null :: Expression params tables grouping (optionality ('Null ty))
92−null = UnsafeExpression "NULL"
93−
94−unNull
95− :: Expression params tables grouping (optionality ('NotNull ty))
96− -> Expression params tables grouping (optionality ('Null ty))
97−unNull = UnsafeExpression . renderExpression
98−
99−coalesce
100− :: [Expression params tables grouping ('Required ('Null ty))]
101− -> Expression params tables grouping ('Required ('NotNull ty))
102− -> Expression params tables grouping ('Required ('NotNull ty))
103−coalesce nullxs notNullx = UnsafeExpression $
104− "COALESCE" <> parenthesized (commaSeparated
105− ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
106−
107−fromNull
108− :: Expression params tables grouping ('Required ('NotNull ty))
109− -> Expression params tables grouping ('Required ('Null ty))
110− -> Expression params tables grouping ('Required ('NotNull ty))
111−fromNull notNullx nullx = coalesce [nullx] notNullx
112−
113−isNull
114− :: Expression params tables grouping ('Required ('Null ty))
115− -> Condition params tables grouping
116−isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
117−
118−isn'tNull
119− :: Expression params tables grouping ('Required ('Null ty))
120− -> Condition params tables grouping
121−isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
122−
123−matchNull
124− :: Expression params tables grouping ('Required nullty)
125− -> ( Expression params tables grouping ('Required ('NotNull ty))
126− -> Expression params tables grouping ('Required nullty) )
127− -> Expression params tables grouping ('Required ('Null ty))
128− -> Expression params tables grouping ('Required nullty)
129−matchNull y f x = ifThenElse (isNull x) y
130− (f (UnsafeExpression (renderExpression y)))
131−
132−nullIf
133− :: Expression params tables grouping ('Required ('NotNull ty))
134− -> Expression params tables grouping ('Required ('NotNull ty))
135− -> Expression params tables grouping ('Required ('Null ty))
136−nullIf x y = UnsafeExpression $ parenthesized $
137− renderExpression x <> ", " <> renderExpression y
138−
139−greatest
140− :: [Expression params tables grouping ('Required nullty)]
141− -> Expression params table grouping ('Required nullty)
142−greatest xs = UnsafeExpression $ "GREATEST("
143− <> commaSeparated (renderExpression <$> xs) <> ")"
144−
145−least
146− :: [Expression params tables grouping ('Required nullty)]
147− -> Expression params table grouping ('Required nullty)
148−least xs = UnsafeExpression $ "LEAST("
149− <> commaSeparated (renderExpression <$> xs) <> ")"
150−
151−unsafeBinaryOp
152− :: ByteString
153− -> Expression params tables grouping ('Required ty0)
154− -> Expression params tables grouping ('Required ty1)
155− -> Expression params tables grouping ('Required ty2)
156−unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
157− renderExpression x <+> op <+> renderExpression y
158−
159−unsafeUnaryOp
160− :: ByteString
161− -> Expression params tables grouping ('Required ty0)
162− -> Expression params tables grouping ('Required ty1)
163−unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
164− op <+> renderExpression x
165−
166−unsafeFunction
167− :: ByteString
168− -> Expression params tables grouping ('Required xty)
169− -> Expression params tables grouping ('Required yty)
170−unsafeFunction fun x = UnsafeExpression $
171− fun <> parenthesized (renderExpression x)
172−
173−instance PGNum ty
174− => Num (Expression params tables grouping ('Required (nullity ty))) where
175− (+) = unsafeBinaryOp "+"
176− (-) = unsafeBinaryOp "-"
177− (*) = unsafeBinaryOp "*"
178− abs = unsafeFunction "abs"
179− signum = unsafeFunction "sign"
180− fromInteger
181− = UnsafeExpression
182− . (<> if decimal (Proxy :: Proxy ty) then "." else "")
183− . fromString
184− . show
185−
186−instance PGFloating ty => Fractional
187− (Expression params tables grouping ('Required (nullity ty))) where
188− (/) = unsafeBinaryOp "/"
189− fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
190−
191−instance (PGFloating ty, PGTyped ty) => Floating
192− (Expression params tables grouping ('Required (nullity ty))) where
193− pi = UnsafeExpression "pi()"
194− exp = unsafeFunction "exp"
195− log = unsafeFunction "ln"
196− sqrt = unsafeFunction "sqrt"
197− b ** x = UnsafeExpression $
198− "power(" <> renderExpression b <> ", " <> renderExpression x <> ")"
199− logBase b y = cast pgtype $ logBaseNumeric b y
200− where
201− logBaseNumeric
202− :: Expression params tables grouping ('Required (nullity ty))
203− -> Expression params tables grouping ('Required (nullity ty))
204− -> Expression params tables grouping ('Required (nullity 'PGnumeric))
205− logBaseNumeric b' y' = UnsafeExpression $ mconcat
206− [ "log("
207− , renderExpression (cast numeric b')
208− , ", "
209− , renderExpression (cast numeric y')
210− , ")"
211− ]
212− sin = unsafeFunction "sin"
213− cos = unsafeFunction "cos"
214− tan = unsafeFunction "tan"
215− asin = unsafeFunction "asin"
216− acos = unsafeFunction "acos"
217− atan = unsafeFunction "atan"
218− sinh x = (exp x - exp (-x)) / 2
219− cosh x = (exp x + exp (-x)) / 2
220− tanh x = sinh x / cosh x
221− asinh x = log (x + sqrt (x*x + 1))
222− acosh x = log (x + sqrt (x*x - 1))
223− atanh x = log ((1 + x) / (1 - x)) / 2
224−
225−atan2
226− :: PGFloating float
227− => Expression params tables grouping ('Required (nullity float))
228− -> Expression params tables grouping ('Required (nullity float))
229− -> Expression params tables grouping ('Required (nullity float))
230−atan2 y x = UnsafeExpression $
231− "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
232−
233−cast
234− :: TypeExpression ('Required ('Null ty1))
235− -> Expression params tables grouping ('Required (nullity ty0))
236− -> Expression params tables grouping ('Required (nullity ty1))
237−cast ty x = UnsafeExpression $ parenthesized $
238− renderExpression x <+> "::" <+> renderTypeExpression ty
239−
240−div
241− :: PGIntegral int
242− => Expression params tables grouping ('Required (nullity int))
243− -> Expression params tables grouping ('Required (nullity int))
244− -> Expression params tables grouping ('Required (nullity int))
245−div = unsafeBinaryOp "/"
246−
247−mod
248− :: PGIntegral int
249− => Expression params tables grouping ('Required (nullity int))
250− -> Expression params tables grouping ('Required (nullity int))
251− -> Expression params tables grouping ('Required (nullity int))
252−mod = unsafeBinaryOp "%"
253−
254−trunc
255− :: PGFloating frac
256− => Expression params tables grouping ('Required (nullity frac))
257− -> Expression params tables grouping ('Required (nullity frac))
258−trunc = unsafeFunction "trunc"
259−
260−round_
261− :: PGFloating frac
262− => Expression params tables grouping ('Required (nullity frac))
263− -> Expression params tables grouping ('Required (nullity frac))
264−round_ = unsafeFunction "round"
265−
266−ceiling_
267− :: PGFloating frac
268− => Expression params tables grouping ('Required (nullity frac))
269− -> Expression params tables grouping ('Required (nullity frac))
270−ceiling_ = unsafeFunction "ceiling"
271−
272−instance IsString
273− (Expression params tables grouping ('Required (nullity 'PGtext))) where
274− fromString str = UnsafeExpression $
275− "E\'" <> fromString (escape =<< str) <> "\'"
276− where
277− escape = \case
278− '\NUL' -> "\\0"
279− '\'' -> "''"
280− '"' -> "\\\""
281− '\b' -> "\\b"
282− '\n' -> "\\n"
283− '\r' -> "\\r"
284− '\t' -> "\\t"
285− '\\' -> "\\\\"
286− c -> [c]
287−
288−type Condition params tables grouping =
289− Expression params tables grouping ('Required ('NotNull 'PGbool))
290−
291−true :: Condition params tables grouping
292−true = UnsafeExpression "TRUE"
293−
294−false :: Condition params tables grouping
295−false = UnsafeExpression "FALSE"
296−
297−not_
298− :: Condition params tables grouping
299− -> Condition params tables grouping
300−not_ = unsafeUnaryOp "NOT"
301−
302−(&&*)
303− :: Condition params tables grouping
304− -> Condition params tables grouping
305− -> Condition params tables grouping
306−(&&*) = unsafeBinaryOp "AND"
307−
308−(||*)
309− :: Condition params tables grouping
310− -> Condition params tables grouping
311− -> Condition params tables grouping
312−(||*) = unsafeBinaryOp "OR"
313−
314−caseWhenThenElse
315− :: [ ( Condition params tables grouping
316− , Expression params tables grouping ('Required ty)
317− ) ]
318− -> Expression params tables grouping ('Required ty)
319− -> Expression params tables grouping ('Required ty)
320−caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
321− [ "CASE"
322− , mconcat
323− [ mconcat
324− [ " WHEN ", renderExpression when_
325− , " THEN ", renderExpression then_
326− ]
327− | (when_,then_) <- whenThens
328− ]
329− , " ELSE ", renderExpression else_
330− , " END"
331− ]
332−
333−ifThenElse
334− :: Condition params tables grouping
335− -> Expression params tables grouping ('Required ty)
336− -> Expression params tables grouping ('Required ty)
337− -> Expression params tables grouping ('Required ty)
338−ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
339−
340−(==*)
341− :: Expression params tables grouping ('Required (nullity ty))
342− -> Expression params tables grouping ('Required (nullity ty))
343− -> Expression params tables grouping ('Required (nullity 'PGbool))
344−(==*) = unsafeBinaryOp "="
345−infix 4 ==*
346−
347−(/=*)
348− :: Expression params tables grouping ('Required (nullity ty))
349− -> Expression params tables grouping ('Required (nullity ty))
350− -> Expression params tables grouping ('Required (nullity 'PGbool))
351−(/=*) = unsafeBinaryOp "<>"
352−infix 4 /=*
353−
354−(>=*)
355− :: Expression params tables grouping ('Required (nullity ty))
356− -> Expression params tables grouping ('Required (nullity ty))
357− -> Expression params tables grouping ('Required (nullity 'PGbool))
358−(>=*) = unsafeBinaryOp ">="
359−infix 4 >=*
360−
361−(<*)
362− :: Expression params tables grouping ('Required (nullity ty))
363− -> Expression params tables grouping ('Required (nullity ty))
364− -> Expression params tables grouping ('Required (nullity 'PGbool))
365−(<*) = unsafeBinaryOp "<"
366−infix 4 <*
367−
368−(<=*)
369− :: Expression params tables grouping ('Required (nullity ty))
370− -> Expression params tables grouping ('Required (nullity ty))
371− -> Expression params tables grouping ('Required (nullity 'PGbool))
372−(<=*) = unsafeBinaryOp "<="
373−infix 4 <=*
374−
375−(>*)
376− :: Expression params tables grouping ('Required (nullity ty))
377− -> Expression params tables grouping ('Required (nullity ty))
378− -> Expression params tables grouping ('Required (nullity 'PGbool))
379−(>*) = unsafeBinaryOp ">"
380−infix 4 >*
381−
382−data SortExpression params tables grouping where
383− Asc
384− :: Expression params tables grouping ty
385− -> SortExpression params tables grouping
386− Desc
387− :: Expression params tables grouping ty
388− -> SortExpression params tables grouping
389−
390−renderSortExpression :: SortExpression params tables grouping -> ByteString
391−renderSortExpression = \case
392− Asc expression -> renderExpression expression <+> "ASC"
393− Desc expression -> renderExpression expression <+> "DESC"
394−
395−{-----------------------------------------
396−aggregation
397−-----------------------------------------}
398−
399−data By
400− (tables :: [(Symbol,[(Symbol,ColumnType)])])
401− (tabcol :: (Symbol,Symbol)) where
402− By
403− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
404− => Alias column
405− -> By tables '(table, column)
406− By2
407− :: (HasTable table tables columns, HasColumn column columns ty)
408− => (Alias table, Alias column)
409− -> By tables '(table, column)
410−
411−renderBy :: By tables tabcolty -> ByteString
412−renderBy = \case
413− By column -> renderAlias column
414− By2 (table, column) -> renderAlias table <> "." <> renderAlias column
415−
416−data GroupByClause tables grouping where
417− NoGroups :: GroupByClause tables 'Ungrouped
418− Group
419− :: SListI bys
420− => NP (By tables) bys
421− -> GroupByClause tables ('Grouped bys)
422−
423−renderGroupByClause :: GroupByClause tables grouping -> ByteString
424−renderGroupByClause = \case
425− NoGroups -> ""
426− Group Nil -> ""
427− Group bys -> " GROUP BY" <+> renderCommaSeparated renderBy bys
428−
429−data HavingClause params tables grouping where
430− NoHaving :: HavingClause params tables 'Ungrouped
431− Having
432− :: [Condition params tables ('Grouped bys)]
433− -> HavingClause params tables ('Grouped bys)
434−
435−renderHavingClause :: HavingClause params tables grouping -> ByteString
436−renderHavingClause = \case
437− NoHaving -> ""
438− Having [] -> ""
439− Having conditions ->
440− " HAVING" <+> commaSeparated (renderExpression <$> conditions)
441−
442−class (KnownSymbol table, KnownSymbol column)
443− => GroupedBy table column bys where
444− getGroup1
445− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
446− => Alias column
447− -> Expression params tables ('Grouped bys) ty
448− getGroup1 column = UnsafeExpression $ renderAlias column
449− getGroup2
450− :: (HasTable table tables columns, HasColumn column columns ty)
451− => Alias table
452− -> Alias column
453− -> Expression params tables ('Grouped bys) ty
454− getGroup2 table column = UnsafeExpression $
455− renderAlias table <> "." <> renderAlias column
456−instance {-# OVERLAPPING #-} (KnownSymbol table, KnownSymbol column)
457− => GroupedBy table column ('(table,column) ': bys)
458−instance {-# OVERLAPPABLE #-}
459− ( KnownSymbol table
460− , KnownSymbol column
461− , GroupedBy table column bys
462− ) => GroupedBy table column (tabcol ': bys)
463−
464−instance
465− ( tables ~ '[table ::: columns]
466− , HasColumn column columns ty
467− , GroupedBy table column bys
468− ) => IsLabel column
469− (Expression params tables ('Grouped bys) ty) where
470− fromLabel = getGroup1 (Alias @column)
471−
472−instance
473− ( HasTable table tables columns
474− , HasColumn column columns ty
475− , GroupedBy table column bys
476− ) => IsTableColumn table column
477− (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
478−
479−unsafeAggregate
480− :: ByteString
481− -> Expression params tables 'Ungrouped ('Required xty)
482− -> Expression params tables ('Grouped bys) ('Required yty)
483−unsafeAggregate fun x = UnsafeExpression $ mconcat
484− [fun, "(", renderExpression x, ")"]
485−
486−sum_
487− :: PGNum ty
488− => Expression params tables 'Ungrouped ('Required (nullity ty))
489− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
490−sum_ = unsafeAggregate "sum"
491−
492−avgInt
493− :: PGIntegral int
494− => Expression params tables 'Ungrouped ('Required (nullity int))
495− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGnumeric))
496−avgInt = unsafeAggregate "avg"
497−
498−avgFloat
499− :: PGFloating float
500− => Expression params tables 'Ungrouped ('Required (nullity float))
501− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGfloat8))
502−avgFloat = unsafeAggregate "avg"
503−
504−bitAnd
505− :: PGIntegral int
506− => Expression params tables 'Ungrouped ('Required (nullity int))
507− -> Expression params tables ('Grouped bys) ('Required (nullity int))
508−bitAnd = unsafeAggregate "bit_and"
509−
510−bitOr
511− :: PGIntegral int
512− => Expression params tables 'Ungrouped ('Required (nullity int))
513− -> Expression params tables ('Grouped bys) ('Required (nullity int))
514−bitOr = unsafeAggregate "bit_or"
515−
516−boolAnd
517− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
518− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
519−boolAnd = unsafeAggregate "bool_and"
520−
521−boolOr
522− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
523− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
524−boolOr = unsafeAggregate "bool_or"
525−
526−countStar
527− :: Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
528−countStar = UnsafeExpression "count(*)"
529−
530−count
531− :: Expression params tables 'Ungrouped ('Required ty)
532− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGint8))
533−count = unsafeAggregate "count"
534−
535−every
536− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
537− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
538−every = unsafeAggregate "every"
539−
540−jsonAgg
541− :: Expression params tables 'Ungrouped ('Required ty)
542− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
543−jsonAgg = unsafeAggregate "json_agg"
544−
545−jsonbAgg
546− :: Expression params tables 'Ungrouped ('Required ty)
547− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
548−jsonbAgg = unsafeAggregate "jsonb_agg"
549−
550−jsonObjectAgg
551− :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
552− -> Expression params tables 'Ungrouped ('Required valty)
553− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
554−jsonObjectAgg k v = UnsafeExpression $
555− "json_object_agg(" <> renderExpression k
556− <> ", " <> renderExpression v <> ")"
557−
558−jsonbObjectAgg
559− :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
560− -> Expression params tables 'Ungrouped ('Required valty)
561− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
562−jsonbObjectAgg k v = UnsafeExpression $
563− "jsonb_object_agg(" <> renderExpression k
564− <> ", " <> renderExpression v <> ")"
565−
566−max_
567− :: Expression params tables 'Ungrouped ('Required (nullity ty))
568− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
569−max_ = unsafeAggregate "max"
570−
571−min_
572− :: Expression params tables 'Ungrouped ('Required (nullity ty))
573− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
574−min_ = unsafeAggregate "min"
575−
576−{-----------------------------------------
577−tables
578−-----------------------------------------}
579−
580−newtype Table
581− (schema :: [(Symbol,[(Symbol,ColumnType)])])
582− (columns :: [(Symbol,ColumnType)])
583− = UnsafeTable { renderTable :: ByteString }
584− deriving (Show,Eq)
585−
586−class KnownSymbol table => HasTable table tables columns
587− | table tables -> columns where
588− getTable :: Alias table -> Table tables columns
589− getTable table = UnsafeTable $ renderAlias table
590−instance {-# OVERLAPPING #-} KnownSymbol table
591− => HasTable table ((table ::: columns) ': tables) columns
592−instance {-# OVERLAPPABLE #-}
593− (KnownSymbol table, HasTable table schema columns)
594− => HasTable table (table' ': schema) columns
595−
596−instance HasTable table schema columns
597− => IsLabel table (Table schema columns) where
598− fromLabel = getTable (Alias @table)
599−
600−data TableReference params schema tables where
601− Table
602− :: Aliased (Table schema) table
603− -> TableReference params schema '[table]
604− Subquery
605− :: Aliased (Query schema params) table
606− -> TableReference params schema '[table]
607− CrossJoin
608− :: TableReference params schema right
609− -> TableReference params schema left
610− -> TableReference params schema (Join left right)
611− InnerJoin
612− :: TableReference params schema right
613− -> Condition params (Join left right) 'Ungrouped
614− -> TableReference params schema left
615− -> TableReference params schema (Join left right)
616− LeftOuterJoin
617− :: TableReference params schema right
618− -> Condition params (Join left right) 'Ungrouped
619− -> TableReference params schema left
620− -> TableReference params schema (Join left (NullifyTables right))
621− RightOuterJoin
622− :: TableReference params schema right
623− -> Condition params (Join left right) 'Ungrouped
624− -> TableReference params schema left
625− -> TableReference params schema (Join (NullifyTables left) right)
626− FullOuterJoin
627− :: TableReference params schema right
628− -> Condition params (Join left right) 'Ungrouped
629− -> TableReference params schema left
630− -> TableReference params schema
631− (Join (NullifyTables left) (NullifyTables right))
632−
633−renderTableReference :: TableReference params schema tables -> ByteString
634−renderTableReference = \case
635− Table table -> renderAliased renderTable table
636− Subquery selection -> renderAliased renderQuery selection
637− CrossJoin right left ->
638− renderTableReference left <+> "CROSS JOIN" <+> renderTableReference right
639− InnerJoin right on left -> renderJoin "INNER JOIN" right on left
640− LeftOuterJoin right on left -> renderJoin "LEFT OUTER JOIN" right on left
641− RightOuterJoin right on left -> renderJoin "RIGHT OUTER JOIN" right on left
642− FullOuterJoin right on left -> renderJoin "FULL OUTER JOIN" right on left
643− where
644− renderJoin op right on left =
645− renderTableReference left <+> op <+> renderTableReference right
646− <+> "ON" <+> renderExpression on
647−
648−data TableExpression
649− (params :: [ColumnType])
650− (schema :: [(Symbol,[(Symbol,ColumnType)])])
651− (tables :: [(Symbol,[(Symbol,ColumnType)])])
652− (grouping :: Grouping)
653− = TableExpression
654− { fromClause :: TableReference params schema tables
655− , whereClause :: [Condition params tables 'Ungrouped]
656− , groupByClause :: GroupByClause tables grouping
657− , havingClause :: HavingClause params tables grouping
658− , orderByClause :: [SortExpression params tables grouping]
659− , limitClause :: [Word64]
660− , offsetClause :: [Word64]
661− }
662−
663−renderTableExpression
664− :: TableExpression params schema tables grouping
665− -> ByteString
666−renderTableExpression
667− (TableExpression tables whs' grps' hvs' srts' lims' offs') = mconcat
668− [ "FROM ", renderTableReference tables
669− , renderWheres whs'
670− , renderGroupByClause grps'
671− , renderHavingClause hvs'
672− , renderSorts srts'
673− , renderLimits lims'
674− , renderOffsets offs'
675− ]
676− where
677− renderWheres = \case
678− [] -> ""
679− wh:[] -> " WHERE" <+> renderExpression wh
680− wh:whs -> " WHERE" <+> renderExpression (foldr (&&*) wh whs)
681− renderSorts = \case
682− [] -> ""
683− srts -> " SORT BY"
684− <+> commaSeparated (renderSortExpression <$> srts)
685− renderLimits = \case
686− [] -> ""
687− lims -> " LIMIT" <+> fromString (show (minimum lims))
688− renderOffsets = \case
689− [] -> ""
690− offs -> " OFFSET" <+> fromString (show (sum offs))
691−
692−from
693− :: TableReference params schema tables
694− -> TableExpression params schema tables 'Ungrouped
695−from tables = TableExpression tables [] NoGroups NoHaving [] [] []
696−
697−where_
698− :: Condition params tables 'Ungrouped
699− -> TableExpression params schema tables grouping
700− -> TableExpression params schema tables grouping
701−where_ wh tables = tables {whereClause = wh : whereClause tables}
702−
703−group
704− :: SListI bys
705− => NP (By tables) bys
706− -> TableExpression params schema tables 'Ungrouped
707− -> TableExpression params schema tables ('Grouped bys)
708−group bys tables = TableExpression
709− { fromClause = fromClause tables
710− , whereClause = whereClause tables
711− , groupByClause = Group bys
712− , havingClause = Having []
713− , orderByClause = []
714− , limitClause = limitClause tables
715− , offsetClause = offsetClause tables
716− }
717−
718−having
719− :: Condition params tables ('Grouped bys)
720− -> TableExpression params schema tables ('Grouped bys)
721− -> TableExpression params schema tables ('Grouped bys)
722−having hv tables = tables
723− { havingClause = case havingClause tables of Having hvs -> Having (hv:hvs) }
724−
725−orderBy
726− :: [SortExpression params tables grouping]
727− -> TableExpression params schema tables grouping
728− -> TableExpression params schema tables grouping
729−orderBy srts tables = tables {orderByClause = orderByClause tables ++ srts}
730−
731−limit
732− :: Word64
733− -> TableExpression params schema tables grouping
734− -> TableExpression params schema tables grouping
735−limit lim tables = tables {limitClause = lim : limitClause tables}
736−
737−offset
738− :: Word64
739− -> TableExpression params schema tables grouping
740− -> TableExpression params schema tables grouping
741−offset off tables = tables {offsetClause = off : offsetClause tables}
742−
743−{-----------------------------------------
744−type expressions
745−-----------------------------------------}
746−
747−newtype TypeExpression (ty :: ColumnType)
748− = UnsafeTypeExpression { renderTypeExpression :: ByteString }
749− deriving (Show,Eq)
750−
751−bool :: TypeExpression ('Required ('Null 'PGbool))
752−bool = UnsafeTypeExpression "bool"
753−int2 :: TypeExpression ('Required ('Null 'PGint2))
754−int2 = UnsafeTypeExpression "int2"
755−smallint :: TypeExpression ('Required ('Null 'PGint2))
756−smallint = UnsafeTypeExpression "smallint"
757−int4 :: TypeExpression ('Required ('Null 'PGint4))
758−int4 = UnsafeTypeExpression "int4"
759−int :: TypeExpression ('Required ('Null 'PGint4))
760−int = UnsafeTypeExpression "int"
761−integer :: TypeExpression ('Required ('Null 'PGint4))
762−integer = UnsafeTypeExpression "integer"
763−int8 :: TypeExpression ('Required ('Null 'PGint8))
764−int8 = UnsafeTypeExpression "int8"
765−bigint :: TypeExpression ('Required ('Null 'PGint8))
766−bigint = UnsafeTypeExpression "bigint"
767−numeric :: TypeExpression ('Required ('Null 'PGnumeric))
768−numeric = UnsafeTypeExpression "numeric"
769−float4 :: TypeExpression ('Required ('Null 'PGfloat4))
770−float4 = UnsafeTypeExpression "float4"
771−real :: TypeExpression ('Required ('Null 'PGfloat4))
772−real = UnsafeTypeExpression "real"
773−float8 :: TypeExpression ('Required ('Null 'PGfloat8))
774−float8 = UnsafeTypeExpression "float8"
775−doublePrecision :: TypeExpression ('Required ('Null 'PGfloat8))
776−doublePrecision = UnsafeTypeExpression "double precision"
777−serial2 :: TypeExpression ('Optional ('NotNull 'PGint2))
778−serial2 = UnsafeTypeExpression "serial2"
779−smallserial :: TypeExpression ('Optional ('NotNull 'PGint2))
780−smallserial = UnsafeTypeExpression "smallserial"
781−serial4 :: TypeExpression ('Optional ('NotNull 'PGint4))
782−serial4 = UnsafeTypeExpression "serial4"
783−serial :: TypeExpression ('Optional ('NotNull 'PGint4))
784−serial = UnsafeTypeExpression "serial"
785−serial8 :: TypeExpression ('Optional ('NotNull 'PGint8))
786−serial8 = UnsafeTypeExpression "serial8"
787−bigserial :: TypeExpression ('Optional ('NotNull 'PGint8))
788−bigserial = UnsafeTypeExpression "bigserial"
789−text :: TypeExpression ('Required ('Null 'PGtext))
790−text = UnsafeTypeExpression "text"
791−char
792− :: KnownNat n
793− => proxy n
794− -> TypeExpression ('Required ('Null ('PGchar n)))
795−char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
796−character
797− :: KnownNat n
798− => proxy n
799− -> TypeExpression ('Required ('Null ('PGchar n)))
800−character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
801−varchar
802− :: KnownNat n
803− => proxy n
804− -> TypeExpression ('Required ('Null ('PGvarchar n)))
805−varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
806−characterVarying
807− :: KnownNat n
808− => proxy n
809− -> TypeExpression ('Required ('Null ('PGvarchar n)))
810−characterVarying p = UnsafeTypeExpression $
811− "character varying(" <> renderNat p <> ")"
812−bytea :: TypeExpression ('Required ('Null ('PGbytea)))
813−bytea = UnsafeTypeExpression "bytea"
814−timestamp :: TypeExpression ('Required ('Null ('PGtimestamp)))
815−timestamp = UnsafeTypeExpression "timestamp"
816−timestampWithTimeZone :: TypeExpression ('Required ('Null ('PGtimestamptz)))
817−timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
818−date :: TypeExpression ('Required ('Null ('PGdate)))
819−date = UnsafeTypeExpression "date"
820−time :: TypeExpression ('Required ('Null ('PGtime)))
821−time = UnsafeTypeExpression "time"
822−timeWithTimeZone :: TypeExpression ('Required ('Null ('PGtimetz)))
823−timeWithTimeZone = UnsafeTypeExpression "time with time zone"
824−interval :: TypeExpression ('Required ('Null ('PGinterval)))
825−interval = UnsafeTypeExpression "interval"
826−uuid :: TypeExpression ('Required ('Null ('PGuuid)))
827−uuid = UnsafeTypeExpression "uuid"
828−json :: TypeExpression ('Required ('Null ('PGjson)))
829−json = UnsafeTypeExpression "json"
830−jsonb :: TypeExpression ('Required ('Null ('PGjsonb)))
831−jsonb = UnsafeTypeExpression "jsonb"
832−
833−notNull
834− :: TypeExpression (optionality ('Null ty))
835− -> TypeExpression (optionality ('NotNull ty))
836−notNull ty = UnsafeTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
837−default_
838− :: Expression '[] '[] 'Ungrouped ('Required ty)
839− -> TypeExpression ('Required ty)
840− -> TypeExpression ('Optional ty)
841−default_ x ty = UnsafeTypeExpression $
842− renderTypeExpression ty <+> "DEFAULT" <+> renderExpression x
843−
844−class PGTyped (ty :: PGType) where
845− pgtype :: TypeExpression ('Required ('Null ty))
846−instance PGTyped 'PGbool where pgtype = bool
847−instance PGTyped 'PGint2 where pgtype = int2
848−instance PGTyped 'PGint4 where pgtype = int4
849−instance PGTyped 'PGint8 where pgtype = int8
850−instance PGTyped 'PGnumeric where pgtype = numeric
851−instance PGTyped 'PGfloat4 where pgtype = float4
852−instance PGTyped 'PGfloat8 where pgtype = float8
853−instance PGTyped 'PGtext where pgtype = text
854−instance KnownNat n => PGTyped ('PGchar n) where pgtype = char (Proxy @n)
855−instance KnownNat n => PGTyped ('PGvarchar n) where pgtype = varchar (Proxy @n)
856−instance PGTyped 'PGbytea where pgtype = bytea
857−instance PGTyped 'PGtimestamp where pgtype = timestamp
858−instance PGTyped 'PGtimestamptz where pgtype = timestampWithTimeZone
859−instance PGTyped 'PGdate where pgtype = date
860−instance PGTyped 'PGtime where pgtype = time
861−instance PGTyped 'PGtimetz where pgtype = timeWithTimeZone
862−instance PGTyped 'PGinterval where pgtype = interval
863−instance PGTyped 'PGuuid where pgtype = uuid
864−instance PGTyped 'PGjson where pgtype = json
865−instance PGTyped 'PGjsonb where pgtype = jsonb
866−
867−{-----------------------------------------
868−statements
869−-----------------------------------------}
870−
871−newtype Definition
872− (schema0 :: [(Symbol,[(Symbol,ColumnType)])])
873− (schema1 :: [(Symbol,[(Symbol,ColumnType)])])
874− = UnsafeDefinition { renderDefinition :: ByteString }
875−
876−instance Category Definition where
877− id = UnsafeDefinition ";"
878− ddl1 . ddl0 = UnsafeDefinition $
879− renderDefinition ddl0 <+> renderDefinition ddl1
880−
881−newtype Manipulation
882− (schema :: [(Symbol,[(Symbol,ColumnType)])])
883− (params :: [ColumnType])
884− (columns :: [(Symbol,ColumnType)])
885− = UnsafeManipulation { renderManipulation :: ByteString }
886−
887−newtype Query
888− (schema :: [(Symbol,[(Symbol,ColumnType)])])
889− (params :: [ColumnType])
890− (columns :: [(Symbol,ColumnType)])
891− = UnsafeQuery { renderQuery :: ByteString }
892−
893−query
894− :: Query schema params columns
895− -> Manipulation schema params columns
896−query q = UnsafeManipulation $ renderQuery q <> ";"
897−
898−union, unionAll, intersect, intersectAll, except, exceptAll
899− :: Query schema params columns
900− -> Query schema params columns
901− -> Query schema params columns
902−q1 `union` q2 = UnsafeQuery $
903− parenthesized (renderQuery q1)
904− <+> "UNION"
905− <+> parenthesized (renderQuery q2)
906−q1 `unionAll` q2 = UnsafeQuery $
907− parenthesized (renderQuery q1)
908− <+> "UNION" <+> "ALL"
909− <+> parenthesized (renderQuery q2)
910−q1 `intersect` q2 = UnsafeQuery $
911− parenthesized (renderQuery q1)
912− <+> "INTERSECT"
913− <+> parenthesized (renderQuery q2)
914−q1 `intersectAll` q2 = UnsafeQuery $
915− parenthesized (renderQuery q1)
916− <+> "INTERSECT" <+> "ALL"
917− <+> parenthesized (renderQuery q2)
918−q1 `except` q2 = UnsafeQuery $
919− parenthesized (renderQuery q1)
920− <+> "EXCEPT"
921− <+> parenthesized (renderQuery q2)
922−q1 `exceptAll` q2 = UnsafeQuery $
923− parenthesized (renderQuery q1)
924− <+> "EXCEPT" <+> "ALL"
925− <+> parenthesized (renderQuery q2)
926−
927−{-----------------------------------------
928−SELECT statements
929−-----------------------------------------}
930−
931−select
932− :: SListI columns
933− => NP (Aliased (Expression params tables grouping)) (column ': columns)
934− -> TableExpression params schema tables grouping
935− -> Query schema params (column ': columns)
936−select list tabs = UnsafeQuery $
937− "SELECT"
938− <+> renderCommaSeparated (renderAliased renderExpression) list
939− <+> renderTableExpression tabs
940−
941−selectStar
942− :: (tables ~ '[table ::: columns])
943− => TableExpression params schema tables 'Ungrouped
944− -> Query schema params columns
945−selectStar tabs = UnsafeQuery $ "SELECT" <+> "*" <+> renderTableExpression tabs
946−
947−selectDotStar
948− :: HasTable table tables columns
949− => Alias table
950− -> TableExpression params schema tables 'Ungrouped
951− -> Query schema params columns
952−selectDotStar table tables = UnsafeQuery $
953− "SELECT" <+> renderAlias table <> ".*" <+> renderTableExpression tables
954−
955−selectRow
956− :: SListI columns
957− => NP (Aliased (Expression params '[] 'Ungrouped)) columns
958− -> Query schema params columns
959−selectRow row = UnsafeQuery $
960− "SELECT" <+> renderCommaSeparated (renderAliased renderExpression) row
961−
962−{-----------------------------------------
963−CREATE statements
964−-----------------------------------------}
965−
966−createTable
967− :: (KnownSymbol table, SListI columns)
968− => Alias table
969− -> NP (Aliased TypeExpression) (column ': columns)
970− -> [TableConstraint schema (column ': columns)]
971− -> Definition schema (Create table (column ': columns) schema)
972−createTable table columns constraints = UnsafeDefinition $
973− "CREATE TABLE" <+> renderAlias table
974− <+> parenthesized
975− ( renderCommaSeparated renderColumnDef columns
976− <> renderConstraints constraints )
977− <> ";"
978− where
979− renderColumnDef :: Aliased TypeExpression x -> ByteString
980− renderColumnDef (ty `As` column) =
981− renderAlias column <+> renderTypeExpression ty
982− renderConstraints :: [TableConstraint schema columns] -> ByteString
983− renderConstraints = \case
984− [] -> ""
985− _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
986−
987−data TableConstraint
988− (schema :: [(Symbol,[(Symbol,ColumnType)])])
989− (columns :: [(Symbol,ColumnType)])
990− = UnsafeTableConstraint { renderTableConstraint :: ByteString }
991−
992−check
993− :: Condition '[] '[table ::: columns] 'Ungrouped
994− -> TableConstraint schema columns
995−check condition = UnsafeTableConstraint $
996− "CHECK" <+> parenthesized (renderExpression condition)
997−
998−data Column
999− (columns :: [(Symbol,ColumnType)])
1000− (columnty :: (Symbol,ColumnType))
1001− where
1002− Column
1003− :: HasColumn column columns ty
1004− => Alias column
1005− -> Column columns (column ::: ty)
1006−
1007−renderColumn :: Column columns columnty -> ByteString
1008−renderColumn (Column column) = renderAlias column
1009−
1010−unique
1011− :: SListI subcolumns
1012− => NP (Column columns) subcolumns
1013− -> TableConstraint schema columns
1014−unique columns = UnsafeTableConstraint $
1015− "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
1016−
1017−primaryKey
1018− :: (SListI subcolumns, NotNullTypes subcolumns)
1019− => NP (Column columns) subcolumns
1020− -> TableConstraint schema columns
1021−primaryKey columns = UnsafeTableConstraint $
1022− "PRIMARY KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1023−
1024−type family SameTypes columns0 columns1 :: Constraint where
1025− SameTypes '[] '[] = ()
1026− SameTypes ((column0 ::: ty0) ': columns0) ((column1 ::: ty1) ': columns1)
1027− = (ty0 ~ ty1, SameTypes columns0 columns1)
1028−
1029−type family NotNullTypes columns :: Constraint where
1030− NotNullTypes '[] = ()
1031− NotNullTypes ((column ::: (optionality ('NotNull ty))) ': columns)
1032− = NotNullTypes columns
1033−
1034−foreignKey
1035− :: ( HasTable reftable schema refcolumns
1036− , SameTypes subcolumns refsubcolumns
1037− , NotNullTypes subcolumns
1038− , SListI subcolumns
1039− , SListI refsubcolumns)
1040− => NP (Column columns) subcolumns
1041− -> Alias reftable
1042− -> NP (Column refcolumns) refsubcolumns
1043− -> TableConstraint schema columns
1044−foreignKey columns reftable refcolumns = UnsafeTableConstraint $
1045− "FOREIGN KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1046− <+> "REFERENCES" <+> renderAlias reftable
1047− <+> parenthesized (renderCommaSeparated renderColumn refcolumns)
1048−
1049−{-----------------------------------------
1050−DROP statements
1051−-----------------------------------------}
1052−
1053−dropTable
1054− :: KnownSymbol table
1055− => Alias table
1056− -> Definition schema (Drop table schema)
1057−dropTable table = UnsafeDefinition $ "DROP TABLE" <+> renderAlias table <> ";"
1058−
1059−{-----------------------------------------
1060−ALTER statements
1061−-----------------------------------------}
1062−
1063−alterTable
1064− :: HasTable table schema columns0
1065− => Alias table
1066− -> AlterTable columns0 columns1
1067− -> Definition schema (Alter table schema columns1)
1068−alterTable table alteration = UnsafeDefinition $
1069− "ALTER TABLE"
1070− <+> renderAlias table
1071− <+> renderAlterTable alteration
1072− <> ";"
1073−
1074−alterTableRename
1075− :: (KnownSymbol table0, KnownSymbol table1)
1076− => Alias table0
1077− -> Alias table1
1078− -> Definition schema (Rename table0 table1 schema)
1079−alterTableRename table0 table1 = UnsafeDefinition $
1080− "ALTER TABLE" <+> renderAlias table0
1081− <+> "RENAME TO" <+> renderAlias table1 <> ";"
1082−
1083−data AlterTable
1084− (columns0 :: [(Symbol, ColumnType)])
1085− (columns1 :: [(Symbol, ColumnType)]) =
1086− UnsafeAlterTable {renderAlterTable :: ByteString}
1087−
1088−addColumnDefault
1089− :: KnownSymbol column
1090− => Alias column
1091− -> TypeExpression ('Optional ty)
1092− -> AlterTable columns (Create column ('Optional ty) columns)
1093−addColumnDefault column ty = UnsafeAlterTable $
1094− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1095−
1096−addColumnNull
1097− :: KnownSymbol column
1098− => Alias column
1099− -> TypeExpression ('Required ('Null ty))
1100− -> AlterTable columns ((column ::: 'Required ('Null ty)) ': columns)
1101−addColumnNull column ty = UnsafeAlterTable $
1102− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1103−
1104−dropColumn
1105− :: KnownSymbol column
1106− => Alias column
1107− -> AlterTable columns (Drop column columns)
1108−dropColumn column = UnsafeAlterTable $
1109− "DROP COLUMN" <+> renderAlias column
1110−
1111−renameColumn
1112− :: (KnownSymbol column0, KnownSymbol column1)
1113− => Alias column0
1114− -> Alias column1
1115− -> AlterTable columns (Rename column0 column1 columns)
1116−renameColumn column0 column1 = UnsafeAlterTable $
1117− "RENAME COLUMN" <+> renderAlias column0 <+> "TO" <+> renderAlias column1
1118−
1119−alterColumn
1120− :: (KnownSymbol column, HasColumn column columns ty0)
1121− => Alias column
1122− -> AlterColumn ty0 ty1
1123− -> AlterTable columns (Alter column columns ty1)
1124−alterColumn column alteration = UnsafeAlterTable $
1125− "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
1126−
1127−data AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
1128− UnsafeAlterColumn {renderAlterColumn :: ByteString}
1129−
1130−setDefault
1131− :: Expression '[] '[] 'Ungrouped ('Required ty)
1132− -> AlterColumn ('Required ty) ('Optional ty)
1133−setDefault expression = UnsafeAlterColumn $
1134− "SET DEFAULT" <+> renderExpression expression
1135−
1136−dropDefault :: AlterColumn ('Optional ty) ('Required ty)
1137−dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
1138−
1139−setNotNull
1140− :: AlterColumn (optionality ('Null ty)) (optionality ('NotNull ty))
1141−setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
1142−
1143−dropNotNull
1144− :: AlterColumn (optionality ('NotNull ty)) (optionality ('Null ty))
1145−dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
1146−
1147−alterType
1148− :: TypeExpression (optionality (nullity ty1))
1149− -> AlterColumn (optionality (nullity ty0)) (optionality (nullity ty1))
1150−alterType ty = UnsafeAlterColumn $ "TYPE " <> renderTypeExpression ty
1151−
1152−{-----------------------------------------
1153−INSERT statements
1154−-----------------------------------------}
1155−
1156−insertInto
1157− :: (SListI columns, SListI results, HasTable table schema columns)
1158− => Alias table
1159− -> ValuesClause schema params columns
1160− -> ConflictClause params columns
1161− -> ReturningClause params columns results
1162− -> Manipulation schema params results
1163−insertInto table insert conflict returning = UnsafeManipulation $
1164− "INSERT" <+> "INTO" <+> renderAlias table
1165− <+> renderValuesClause insert
1166− <> renderConflictClause conflict
1167− <> renderReturningClause returning
1168−
1169−data ValuesClause
1170− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1171− (params :: [ColumnType])
1172− (columns :: [(Symbol,ColumnType)])
1173− = Values
1174− (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1175− [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
1176− | ValuesQuery (Query schema params columns)
1177−
1178−renderValuesClause
1179− :: SListI columns
1180− => ValuesClause schema params columns
1181− -> ByteString
1182−renderValuesClause = \case
1183− Values row rows ->
1184− parenthesized (renderCommaSeparated renderAliasPart row)
1185− <+> "VALUES"
1186− <+> commaSeparated
1187− (parenthesized . renderCommaSeparated renderValuePart <$> row:rows)
1188− where
1189− renderAliasPart, renderValuePart
1190− :: Aliased (Expression params '[] 'Ungrouped) ty -> ByteString
1191− renderAliasPart (_ `As` name) = renderAlias name
1192− renderValuePart (value `As` _) = renderExpression value
1193− ValuesQuery q -> renderQuery q
1194−
1195−data ReturningClause
1196− (params :: [ColumnType])
1197− (columns :: [(Symbol,ColumnType)])
1198− (results :: [(Symbol,ColumnType)])
1199− where
1200− ReturningStar :: ReturningClause params columns columns
1201− Returning
1202− :: NP (Aliased (Expression params '[table ::: columns] 'Ungrouped))
1203− results
1204− -> ReturningClause params columns results
1205−
1206−renderReturningClause
1207− :: SListI results
1208− => ReturningClause params columns results
1209− -> ByteString
1210−renderReturningClause = \case
1211− ReturningStar -> " RETURNING *;"
1212− Returning Nil -> ";"
1213− Returning results -> " RETURNING"
1214− <+> renderCommaSeparated (renderAliased renderExpression) results <> ";"
1215−
1216−data ConflictClause params columns where
1217− Conflict :: ConflictClause params columns
1218− OnConflictDoNothing :: ConflictClause params columns
1219− OnConflictDoUpdate
1220− :: NP (Aliased (UpdateExpression params '[table ::: columns])) columns
1221− -> Condition params '[table ::: columns] 'Ungrouped
1222− -> ConflictClause params columns
1223−
1224−renderConflictClause
1225− :: SListI columns
1226− => ConflictClause params columns
1227− -> ByteString
1228−renderConflictClause = \case
1229− Conflict -> ""
1230− OnConflictDoNothing -> " ON CONFLICT DO NOTHING"
1231− OnConflictDoUpdate updates wh
1232− -> " ON CONFLICT DO UPDATE SET"
1233− <+> renderCommaSeparatedMaybe renderUpdateExpression updates
1234− <+> "WHERE" <+> renderExpression wh
1235−
1236−{-----------------------------------------
1237−UPDATE statements
1238−-----------------------------------------}
1239−
1240−data UpdateExpression params table ty
1241− = Same
1242− | Set (Expression params table 'Ungrouped ty)
1243−
1244−renderUpdateExpression
1245− :: Aliased (UpdateExpression params '[table ::: columns]) column
1246− -> Maybe ByteString
1247−renderUpdateExpression = \case
1248− Same `As` _ -> Nothing
1249− Set expression `As` column -> Just $
1250− renderAlias column <+> "=" <+> renderExpression expression
1251−
1252−update
1253− :: (HasTable table schema columns, SListI columns, SListI results)
1254− => Alias table
1255− -> NP (Aliased (UpdateExpression params '[table ::: columns])) columns
1256− -> Condition params '[table ::: columns] 'Ungrouped
1257− -> ReturningClause params columns results
1258− -> Manipulation schema params results
1259−update table columns wh returning = UnsafeManipulation $
1260− "UPDATE"
1261− <+> renderAlias table
1262− <+> "SET"
1263− <+> renderCommaSeparatedMaybe renderUpdateExpression columns
1264− <+> "WHERE" <+> renderExpression wh
1265− <> renderReturningClause returning
1266−
1267−{-----------------------------------------
1268−DELETE statements
1269−-----------------------------------------}
1270−
1271−deleteFrom
1272− :: HasTable table schema columns
1273− => Alias table
1274− -> Condition params '[table ::: columns] 'Ungrouped
1275− -> Manipulation schema params '[]
1276−deleteFrom table wh = UnsafeManipulation $
1277− "DELETE FROM" <+> renderAlias table
1278− <+> "WHERE" <+> renderExpression wh <> ";"
1279−
1280−{-----------------------------------------
1281−render helper functions
1282−-----------------------------------------}
1283−
1284−parenthesized :: ByteString -> ByteString
1285−parenthesized str = "(" <> str <> ")"
1286−
1287−(<+>) :: ByteString -> ByteString -> ByteString
1288−str1 <+> str2 = str1 <> " " <> str2
1289−
1290−commaSeparated :: [ByteString] -> ByteString
1291−commaSeparated = ByteString.intercalate ", "
1292−
1293−renderCommaSeparated
1294− :: SListI xs
1295− => (forall x. expression x -> ByteString)
1296− -> NP expression xs -> ByteString
1297−renderCommaSeparated render
1298− = commaSeparated
1299− . hcollapse
1300− . hmap (K . render)
1301−
1302−renderCommaSeparatedMaybe
1303− :: SListI xs
1304− => (forall x. expression x -> Maybe ByteString)
1305− -> NP expression xs -> ByteString
1306−renderCommaSeparatedMaybe render
1307− = commaSeparated
1308− . catMaybes
1309− . hcollapse
1310− . hmap (K . render)
1311−
1312−renderNat :: KnownNat n => proxy n -> ByteString
1313−renderNat (_ :: proxy n) = fromString (show (natVal' (proxy# :: Proxy# n)))