@@ -1,1332 +0,0 @@
1−{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
6− , FunctionalDependencies
13− , ScopedTypeVariables
17− , UndecidableInstances
20−module Squeal.PostgreSQL.Statement where
22−import Control.Category
23−import Data.ByteString (ByteString)
32−import GHC.OverloadedLabels
34−import Prelude hiding (RealFrac(..), id, (.))
36−import qualified Data.ByteString as ByteString
38−import Squeal.PostgreSQL.Schema
40−{-----------------------------------------
42−-----------------------------------------}
45− (params :: [ColumnType])
46− (tables :: [(Symbol,[(Symbol,ColumnType)])])
47− (grouping :: Grouping)
49− = UnsafeExpression { renderExpression :: ByteString }
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
63−class KnownSymbol column => HasColumn column columns ty
64− | column columns -> ty where
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
74−instance (HasColumn column columns ty, tables ~ '[table ::: columns])
75− => IsLabel column (Expression params tables 'Ungrouped ty) where
76− fromLabel = getColumn (Alias @column)
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
83−def :: Expression params '[] grouping ('Optional (nullity ty))
84−def = UnsafeExpression "DEFAULT"
87− :: Expression params '[] grouping ('Required (nullity ty))
88− -> Expression params '[] grouping ('Optional (nullity ty))
89−unDef = UnsafeExpression . renderExpression
91−null :: Expression params tables grouping (optionality ('Null ty))
92−null = UnsafeExpression "NULL"
95− :: Expression params tables grouping (optionality ('NotNull ty))
96− -> Expression params tables grouping (optionality ('Null ty))
97−unNull = UnsafeExpression . renderExpression
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]))
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
114− :: Expression params tables grouping ('Required ('Null ty))
115− -> Condition params tables grouping
116−isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
119− :: Expression params tables grouping ('Required ('Null ty))
120− -> Condition params tables grouping
121−isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
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)))
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
140− :: [Expression params tables grouping ('Required nullty)]
141− -> Expression params table grouping ('Required nullty)
142−greatest xs = UnsafeExpression $ "GREATEST("
143− <> commaSeparated (renderExpression <$> xs) <> ")"
146− :: [Expression params tables grouping ('Required nullty)]
147− -> Expression params table grouping ('Required nullty)
148−least xs = UnsafeExpression $ "LEAST("
149− <> commaSeparated (renderExpression <$> xs) <> ")"
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
161− -> Expression params tables grouping ('Required ty0)
162− -> Expression params tables grouping ('Required ty1)
163−unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
164− op <+> renderExpression x
168− -> Expression params tables grouping ('Required xty)
169− -> Expression params tables grouping ('Required yty)
170−unsafeFunction fun x = UnsafeExpression $
171− fun <> parenthesized (renderExpression x)
174− => Num (Expression params tables grouping ('Required (nullity ty))) where
175− (+) = unsafeBinaryOp "+"
176− (-) = unsafeBinaryOp "-"
177− (*) = unsafeBinaryOp "*"
178− abs = unsafeFunction "abs"
179− signum = unsafeFunction "sign"
182− . (<> if decimal (Proxy :: Proxy ty) then "." else "")
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)
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
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
207− , renderExpression (cast numeric b')
209− , renderExpression (cast numeric y')
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
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 <> ")"
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
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 "/"
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 "%"
256− => Expression params tables grouping ('Required (nullity frac))
257− -> Expression params tables grouping ('Required (nullity frac))
258−trunc = unsafeFunction "trunc"
262− => Expression params tables grouping ('Required (nullity frac))
263− -> Expression params tables grouping ('Required (nullity frac))
264−round_ = unsafeFunction "round"
268− => Expression params tables grouping ('Required (nullity frac))
269− -> Expression params tables grouping ('Required (nullity frac))
270−ceiling_ = unsafeFunction "ceiling"
273− (Expression params tables grouping ('Required (nullity 'PGtext))) where
274− fromString str = UnsafeExpression $
275− "E\'" <> fromString (escape =<< str) <> "\'"
288−type Condition params tables grouping =
289− Expression params tables grouping ('Required ('NotNull 'PGbool))
291−true :: Condition params tables grouping
292−true = UnsafeExpression "TRUE"
294−false :: Condition params tables grouping
295−false = UnsafeExpression "FALSE"
298− :: Condition params tables grouping
299− -> Condition params tables grouping
300−not_ = unsafeUnaryOp "NOT"
303− :: Condition params tables grouping
304− -> Condition params tables grouping
305− -> Condition params tables grouping
306−(&&*) = unsafeBinaryOp "AND"
309− :: Condition params tables grouping
310− -> Condition params tables grouping
311− -> Condition params tables grouping
312−(||*) = unsafeBinaryOp "OR"
315− :: [ ( Condition params tables grouping
316− , Expression params tables grouping ('Required ty)
318− -> Expression params tables grouping ('Required ty)
319− -> Expression params tables grouping ('Required ty)
320−caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
324− [ " WHEN ", renderExpression when_
325− , " THEN ", renderExpression then_
327− | (when_,then_) <- whenThens
329− , " ELSE ", renderExpression else_
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_
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 "="
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 "<>"
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 ">="
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 "<"
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 "<="
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 ">"
382−data SortExpression params tables grouping where
384− :: Expression params tables grouping ('Required ('NotNull ty))
385− -> SortExpression params tables grouping
387− :: Expression params tables grouping ('Required ('NotNull ty))
388− -> SortExpression params tables grouping
390− :: Expression params tables grouping ('Required ('Null ty))
391− -> SortExpression params tables grouping
393− :: Expression params tables grouping ('Required ('Null ty))
394− -> SortExpression params tables grouping
396− :: Expression params tables grouping ('Required ('Null ty))
397− -> SortExpression params tables grouping
399− :: Expression params tables grouping ('Required ('Null ty))
400− -> SortExpression params tables grouping
402−renderSortExpression :: SortExpression params tables grouping -> ByteString
403−renderSortExpression = \case
404− Asc expression -> renderExpression expression <+> "ASC"
405− Desc expression -> renderExpression expression <+> "DESC"
406− AscNullsFirst expression -> renderExpression expression
407− <+> "ASC NULLS FIRST"
408− DescNullsFirst expression -> renderExpression expression
409− <+> "DESC NULLS FIRST"
410− AscNullsLast expression -> renderExpression expression <+> "ASC NULLS LAST"
411− DescNullsLast expression -> renderExpression expression <+> "DESC NULLS LAST"
414−{-----------------------------------------
416−-----------------------------------------}
419− (tables :: [(Symbol,[(Symbol,ColumnType)])])
420− (tabcol :: (Symbol,Symbol)) where
422− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
424− -> By tables '(table, column)
426− :: (HasTable table tables columns, HasColumn column columns ty)
427− => (Alias table, Alias column)
428− -> By tables '(table, column)
430−renderBy :: By tables tabcolty -> ByteString
432− By column -> renderAlias column
433− By2 (table, column) -> renderAlias table <> "." <> renderAlias column
435−data GroupByClause tables grouping where
436− NoGroups :: GroupByClause tables 'Ungrouped
439− => NP (By tables) bys
440− -> GroupByClause tables ('Grouped bys)
442−renderGroupByClause :: GroupByClause tables grouping -> ByteString
443−renderGroupByClause = \case
446− Group bys -> " GROUP BY" <+> renderCommaSeparated renderBy bys
448−data HavingClause params tables grouping where
449− NoHaving :: HavingClause params tables 'Ungrouped
451− :: [Condition params tables ('Grouped bys)]
452− -> HavingClause params tables ('Grouped bys)
454−renderHavingClause :: HavingClause params tables grouping -> ByteString
455−renderHavingClause = \case
458− Having conditions ->
459− " HAVING" <+> commaSeparated (renderExpression <$> conditions)
461−class (KnownSymbol table, KnownSymbol column)
462− => GroupedBy table column bys where
464− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
466− -> Expression params tables ('Grouped bys) ty
467− getGroup1 column = UnsafeExpression $ renderAlias column
469− :: (HasTable table tables columns, HasColumn column columns ty)
472− -> Expression params tables ('Grouped bys) ty
473− getGroup2 table column = UnsafeExpression $
474− renderAlias table <> "." <> renderAlias column
475−instance {-# OVERLAPPING #-} (KnownSymbol table, KnownSymbol column)
476− => GroupedBy table column ('(table,column) ': bys)
477−instance {-# OVERLAPPABLE #-}
479− , KnownSymbol column
480− , GroupedBy table column bys
481− ) => GroupedBy table column (tabcol ': bys)
484− ( tables ~ '[table ::: columns]
485− , HasColumn column columns ty
486− , GroupedBy table column bys
488− (Expression params tables ('Grouped bys) ty) where
489− fromLabel = getGroup1 (Alias @column)
492− ( HasTable table tables columns
493− , HasColumn column columns ty
494− , GroupedBy table column bys
495− ) => IsTableColumn table column
496− (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
500− -> Expression params tables 'Ungrouped ('Required xty)
501− -> Expression params tables ('Grouped bys) ('Required yty)
502−unsafeAggregate fun x = UnsafeExpression $ mconcat
503− [fun, "(", renderExpression x, ")"]
507− => Expression params tables 'Ungrouped ('Required (nullity ty))
508− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
509−sum_ = unsafeAggregate "sum"
513− => Expression params tables 'Ungrouped ('Required (nullity int))
514− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGnumeric))
515−avgInt = unsafeAggregate "avg"
519− => Expression params tables 'Ungrouped ('Required (nullity float))
520− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGfloat8))
521−avgFloat = unsafeAggregate "avg"
525− => Expression params tables 'Ungrouped ('Required (nullity int))
526− -> Expression params tables ('Grouped bys) ('Required (nullity int))
527−bitAnd = unsafeAggregate "bit_and"
531− => Expression params tables 'Ungrouped ('Required (nullity int))
532− -> Expression params tables ('Grouped bys) ('Required (nullity int))
533−bitOr = unsafeAggregate "bit_or"
536− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
537− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
538−boolAnd = unsafeAggregate "bool_and"
541− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
542− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
543−boolOr = unsafeAggregate "bool_or"
546− :: Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
547−countStar = UnsafeExpression "count(*)"
550− :: Expression params tables 'Ungrouped ('Required ty)
551− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGint8))
552−count = unsafeAggregate "count"
555− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
556− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
557−every = unsafeAggregate "every"
560− :: Expression params tables 'Ungrouped ('Required ty)
561− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
562−jsonAgg = unsafeAggregate "json_agg"
565− :: Expression params tables 'Ungrouped ('Required ty)
566− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
567−jsonbAgg = unsafeAggregate "jsonb_agg"
570− :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
571− -> Expression params tables 'Ungrouped ('Required valty)
572− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
573−jsonObjectAgg k v = UnsafeExpression $
574− "json_object_agg(" <> renderExpression k
575− <> ", " <> renderExpression v <> ")"
578− :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
579− -> Expression params tables 'Ungrouped ('Required valty)
580− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
581−jsonbObjectAgg k v = UnsafeExpression $
582− "jsonb_object_agg(" <> renderExpression k
583− <> ", " <> renderExpression v <> ")"
586− :: Expression params tables 'Ungrouped ('Required (nullity ty))
587− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
588−max_ = unsafeAggregate "max"
591− :: Expression params tables 'Ungrouped ('Required (nullity ty))
592− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
593−min_ = unsafeAggregate "min"
595−{-----------------------------------------
597−-----------------------------------------}
600− (schema :: [(Symbol,[(Symbol,ColumnType)])])
601− (columns :: [(Symbol,ColumnType)])
602− = UnsafeTable { renderTable :: ByteString }
605−class KnownSymbol table => HasTable table tables columns
606− | table tables -> columns where
607− getTable :: Alias table -> Table tables columns
608− getTable table = UnsafeTable $ renderAlias table
609−instance {-# OVERLAPPING #-} KnownSymbol table
610− => HasTable table ((table ::: columns) ': tables) columns
611−instance {-# OVERLAPPABLE #-}
612− (KnownSymbol table, HasTable table schema columns)
613− => HasTable table (table' ': schema) columns
615−instance HasTable table schema columns
616− => IsLabel table (Table schema columns) where
617− fromLabel = getTable (Alias @table)
619−data FromClause params schema tables where
621− :: Aliased (Table schema) table
622− -> FromClause params schema '[table]
624− :: Aliased (Query schema params) table
625− -> FromClause params schema '[table]
627− :: FromClause params schema right
628− -> FromClause params schema left
629− -> FromClause params schema (Join left right)
631− :: FromClause params schema right
632− -> Condition params (Join left right) 'Ungrouped
633− -> FromClause params schema left
634− -> FromClause params schema (Join left right)
636− :: FromClause params schema right
637− -> Condition params (Join left right) 'Ungrouped
638− -> FromClause params schema left
639− -> FromClause params schema (Join left (NullifyTables right))
641− :: FromClause params schema right
642− -> Condition params (Join left right) 'Ungrouped
643− -> FromClause params schema left
644− -> FromClause params schema (Join (NullifyTables left) right)
646− :: FromClause params schema right
647− -> Condition params (Join left right) 'Ungrouped
648− -> FromClause params schema left
649− -> FromClause params schema
650− (Join (NullifyTables left) (NullifyTables right))
652−renderFromClause :: FromClause params schema tables -> ByteString
653−renderFromClause = \case
654− Table table -> renderAliased renderTable table
655− Subquery selection -> renderAliased renderQuery selection
656− CrossJoin right left ->
657− renderFromClause left <+> "CROSS JOIN" <+> renderFromClause right
658− InnerJoin right on left -> renderJoin "INNER JOIN" right on left
659− LeftOuterJoin right on left -> renderJoin "LEFT OUTER JOIN" right on left
660− RightOuterJoin right on left -> renderJoin "RIGHT OUTER JOIN" right on left
661− FullOuterJoin right on left -> renderJoin "FULL OUTER JOIN" right on left
663− renderJoin op right on left =
664− renderFromClause left <+> op <+> renderFromClause right
665− <+> "ON" <+> renderExpression on
668− (params :: [ColumnType])
669− (schema :: [(Symbol,[(Symbol,ColumnType)])])
670− (tables :: [(Symbol,[(Symbol,ColumnType)])])
671− (grouping :: Grouping)
673− { fromClause :: FromClause params schema tables
674− , whereClause :: [Condition params tables 'Ungrouped]
675− , groupByClause :: GroupByClause tables grouping
676− , havingClause :: HavingClause params tables grouping
677− , orderByClause :: [SortExpression params tables grouping]
678− , limitClause :: [Word64]
679− , offsetClause :: [Word64]
682−renderTableExpression
683− :: TableExpression params schema tables grouping
685−renderTableExpression
686− (TableExpression tables whs' grps' hvs' srts' lims' offs') = mconcat
687− [ "FROM ", renderFromClause tables
689− , renderGroupByClause grps'
690− , renderHavingClause hvs'
692− , renderLimits lims'
693− , renderOffsets offs'
696− renderWheres = \case
698− wh:[] -> " WHERE" <+> renderExpression wh
699− wh:whs -> " WHERE" <+> renderExpression (foldr (&&*) wh whs)
703− <+> commaSeparated (renderSortExpression <$> srts)
704− renderLimits = \case
706− lims -> " LIMIT" <+> fromString (show (minimum lims))
707− renderOffsets = \case
709− offs -> " OFFSET" <+> fromString (show (sum offs))
712− :: FromClause params schema tables
713− -> TableExpression params schema tables 'Ungrouped
714−from tables = TableExpression tables [] NoGroups NoHaving [] [] []
717− :: Condition params tables 'Ungrouped
718− -> TableExpression params schema tables grouping
719− -> TableExpression params schema tables grouping
720−where_ wh tables = tables {whereClause = wh : whereClause tables}
724− => NP (By tables) bys
725− -> TableExpression params schema tables 'Ungrouped
726− -> TableExpression params schema tables ('Grouped bys)
727−group bys tables = TableExpression
728− { fromClause = fromClause tables
729− , whereClause = whereClause tables
730− , groupByClause = Group bys
731− , havingClause = Having []
732− , orderByClause = []
733− , limitClause = limitClause tables
734− , offsetClause = offsetClause tables
738− :: Condition params tables ('Grouped bys)
739− -> TableExpression params schema tables ('Grouped bys)
740− -> TableExpression params schema tables ('Grouped bys)
741−having hv tables = tables
742− { havingClause = case havingClause tables of Having hvs -> Having (hv:hvs) }
745− :: [SortExpression params tables grouping]
746− -> TableExpression params schema tables grouping
747− -> TableExpression params schema tables grouping
748−orderBy srts tables = tables {orderByClause = orderByClause tables ++ srts}
752− -> TableExpression params schema tables grouping
753− -> TableExpression params schema tables grouping
754−limit lim tables = tables {limitClause = lim : limitClause tables}
758− -> TableExpression params schema tables grouping
759− -> TableExpression params schema tables grouping
760−offset off tables = tables {offsetClause = off : offsetClause tables}
762−{-----------------------------------------
764−-----------------------------------------}
766−newtype TypeExpression (ty :: ColumnType)
767− = UnsafeTypeExpression { renderTypeExpression :: ByteString }
770−bool :: TypeExpression ('Required ('Null 'PGbool))
771−bool = UnsafeTypeExpression "bool"
772−int2 :: TypeExpression ('Required ('Null 'PGint2))
773−int2 = UnsafeTypeExpression "int2"
774−smallint :: TypeExpression ('Required ('Null 'PGint2))
775−smallint = UnsafeTypeExpression "smallint"
776−int4 :: TypeExpression ('Required ('Null 'PGint4))
777−int4 = UnsafeTypeExpression "int4"
778−int :: TypeExpression ('Required ('Null 'PGint4))
779−int = UnsafeTypeExpression "int"
780−integer :: TypeExpression ('Required ('Null 'PGint4))
781−integer = UnsafeTypeExpression "integer"
782−int8 :: TypeExpression ('Required ('Null 'PGint8))
783−int8 = UnsafeTypeExpression "int8"
784−bigint :: TypeExpression ('Required ('Null 'PGint8))
785−bigint = UnsafeTypeExpression "bigint"
786−numeric :: TypeExpression ('Required ('Null 'PGnumeric))
787−numeric = UnsafeTypeExpression "numeric"
788−float4 :: TypeExpression ('Required ('Null 'PGfloat4))
789−float4 = UnsafeTypeExpression "float4"
790−real :: TypeExpression ('Required ('Null 'PGfloat4))
791−real = UnsafeTypeExpression "real"
792−float8 :: TypeExpression ('Required ('Null 'PGfloat8))
793−float8 = UnsafeTypeExpression "float8"
794−doublePrecision :: TypeExpression ('Required ('Null 'PGfloat8))
795−doublePrecision = UnsafeTypeExpression "double precision"
796−serial2 :: TypeExpression ('Optional ('NotNull 'PGint2))
797−serial2 = UnsafeTypeExpression "serial2"
798−smallserial :: TypeExpression ('Optional ('NotNull 'PGint2))
799−smallserial = UnsafeTypeExpression "smallserial"
800−serial4 :: TypeExpression ('Optional ('NotNull 'PGint4))
801−serial4 = UnsafeTypeExpression "serial4"
802−serial :: TypeExpression ('Optional ('NotNull 'PGint4))
803−serial = UnsafeTypeExpression "serial"
804−serial8 :: TypeExpression ('Optional ('NotNull 'PGint8))
805−serial8 = UnsafeTypeExpression "serial8"
806−bigserial :: TypeExpression ('Optional ('NotNull 'PGint8))
807−bigserial = UnsafeTypeExpression "bigserial"
808−text :: TypeExpression ('Required ('Null 'PGtext))
809−text = UnsafeTypeExpression "text"
813− -> TypeExpression ('Required ('Null ('PGchar n)))
814−char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
818− -> TypeExpression ('Required ('Null ('PGchar n)))
819−character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
823− -> TypeExpression ('Required ('Null ('PGvarchar n)))
824−varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
828− -> TypeExpression ('Required ('Null ('PGvarchar n)))
829−characterVarying p = UnsafeTypeExpression $
830− "character varying(" <> renderNat p <> ")"
831−bytea :: TypeExpression ('Required ('Null ('PGbytea)))
832−bytea = UnsafeTypeExpression "bytea"
833−timestamp :: TypeExpression ('Required ('Null ('PGtimestamp)))
834−timestamp = UnsafeTypeExpression "timestamp"
835−timestampWithTimeZone :: TypeExpression ('Required ('Null ('PGtimestamptz)))
836−timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
837−date :: TypeExpression ('Required ('Null ('PGdate)))
838−date = UnsafeTypeExpression "date"
839−time :: TypeExpression ('Required ('Null ('PGtime)))
840−time = UnsafeTypeExpression "time"
841−timeWithTimeZone :: TypeExpression ('Required ('Null ('PGtimetz)))
842−timeWithTimeZone = UnsafeTypeExpression "time with time zone"
843−interval :: TypeExpression ('Required ('Null ('PGinterval)))
844−interval = UnsafeTypeExpression "interval"
845−uuid :: TypeExpression ('Required ('Null ('PGuuid)))
846−uuid = UnsafeTypeExpression "uuid"
847−json :: TypeExpression ('Required ('Null ('PGjson)))
848−json = UnsafeTypeExpression "json"
849−jsonb :: TypeExpression ('Required ('Null ('PGjsonb)))
850−jsonb = UnsafeTypeExpression "jsonb"
853− :: TypeExpression (optionality ('Null ty))
854− -> TypeExpression (optionality ('NotNull ty))
855−notNull ty = UnsafeTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
857− :: Expression '[] '[] 'Ungrouped ('Required ty)
858− -> TypeExpression ('Required ty)
859− -> TypeExpression ('Optional ty)
860−default_ x ty = UnsafeTypeExpression $
861− renderTypeExpression ty <+> "DEFAULT" <+> renderExpression x
863−class PGTyped (ty :: PGType) where
864− pgtype :: TypeExpression ('Required ('Null ty))
865−instance PGTyped 'PGbool where pgtype = bool
866−instance PGTyped 'PGint2 where pgtype = int2
867−instance PGTyped 'PGint4 where pgtype = int4
868−instance PGTyped 'PGint8 where pgtype = int8
869−instance PGTyped 'PGnumeric where pgtype = numeric
870−instance PGTyped 'PGfloat4 where pgtype = float4
871−instance PGTyped 'PGfloat8 where pgtype = float8
872−instance PGTyped 'PGtext where pgtype = text
873−instance KnownNat n => PGTyped ('PGchar n) where pgtype = char (Proxy @n)
874−instance KnownNat n => PGTyped ('PGvarchar n) where pgtype = varchar (Proxy @n)
875−instance PGTyped 'PGbytea where pgtype = bytea
876−instance PGTyped 'PGtimestamp where pgtype = timestamp
877−instance PGTyped 'PGtimestamptz where pgtype = timestampWithTimeZone
878−instance PGTyped 'PGdate where pgtype = date
879−instance PGTyped 'PGtime where pgtype = time
880−instance PGTyped 'PGtimetz where pgtype = timeWithTimeZone
881−instance PGTyped 'PGinterval where pgtype = interval
882−instance PGTyped 'PGuuid where pgtype = uuid
883−instance PGTyped 'PGjson where pgtype = json
884−instance PGTyped 'PGjsonb where pgtype = jsonb
886−{-----------------------------------------
888−-----------------------------------------}
891− (schema0 :: [(Symbol,[(Symbol,ColumnType)])])
892− (schema1 :: [(Symbol,[(Symbol,ColumnType)])])
893− = UnsafeDefinition { renderDefinition :: ByteString }
895−instance Category Definition where
896− id = UnsafeDefinition ";"
897− ddl1 . ddl0 = UnsafeDefinition $
898− renderDefinition ddl0 <+> renderDefinition ddl1
901− (schema :: [(Symbol,[(Symbol,ColumnType)])])
902− (params :: [ColumnType])
903− (columns :: [(Symbol,ColumnType)])
904− = UnsafeManipulation { renderManipulation :: ByteString }
907− (schema :: [(Symbol,[(Symbol,ColumnType)])])
908− (params :: [ColumnType])
909− (columns :: [(Symbol,ColumnType)])
910− = UnsafeQuery { renderQuery :: ByteString }
913− :: Query schema params columns
914− -> Manipulation schema params columns
915−query q = UnsafeManipulation $ renderQuery q <> ";"
917−union, unionAll, intersect, intersectAll, except, exceptAll
918− :: Query schema params columns
919− -> Query schema params columns
920− -> Query schema params columns
921−q1 `union` q2 = UnsafeQuery $
922− parenthesized (renderQuery q1)
924− <+> parenthesized (renderQuery q2)
925−q1 `unionAll` q2 = UnsafeQuery $
926− parenthesized (renderQuery q1)
927− <+> "UNION" <+> "ALL"
928− <+> parenthesized (renderQuery q2)
929−q1 `intersect` q2 = UnsafeQuery $
930− parenthesized (renderQuery q1)
932− <+> parenthesized (renderQuery q2)
933−q1 `intersectAll` q2 = UnsafeQuery $
934− parenthesized (renderQuery q1)
935− <+> "INTERSECT" <+> "ALL"
936− <+> parenthesized (renderQuery q2)
937−q1 `except` q2 = UnsafeQuery $
938− parenthesized (renderQuery q1)
940− <+> parenthesized (renderQuery q2)
941−q1 `exceptAll` q2 = UnsafeQuery $
942− parenthesized (renderQuery q1)
943− <+> "EXCEPT" <+> "ALL"
944− <+> parenthesized (renderQuery q2)
946−{-----------------------------------------
948−-----------------------------------------}
952− => NP (Aliased (Expression params tables grouping)) (column ': columns)
953− -> TableExpression params schema tables grouping
954− -> Query schema params (column ': columns)
955−select list tabs = UnsafeQuery $
957− <+> renderCommaSeparated (renderAliased renderExpression) list
958− <+> renderTableExpression tabs
961− :: (tables ~ '[table ::: columns])
962− => TableExpression params schema tables 'Ungrouped
963− -> Query schema params columns
964−selectStar tabs = UnsafeQuery $ "SELECT" <+> "*" <+> renderTableExpression tabs
967− :: HasTable table tables columns
969− -> TableExpression params schema tables 'Ungrouped
970− -> Query schema params columns
971−selectDotStar table tables = UnsafeQuery $
972− "SELECT" <+> renderAlias table <> ".*" <+> renderTableExpression tables
976− => NP (Aliased (Expression params '[] 'Ungrouped)) columns
977− -> Query schema params columns
978−selectRow row = UnsafeQuery $
979− "SELECT" <+> renderCommaSeparated (renderAliased renderExpression) row
981−{-----------------------------------------
983−-----------------------------------------}
986− :: (KnownSymbol table, SListI columns)
988− -> NP (Aliased TypeExpression) (column ': columns)
989− -> [TableConstraint schema (column ': columns)]
990− -> Definition schema (Create table (column ': columns) schema)
991−createTable table columns constraints = UnsafeDefinition $
992− "CREATE TABLE" <+> renderAlias table
994− ( renderCommaSeparated renderColumnDef columns
995− <> renderConstraints constraints )
998− renderColumnDef :: Aliased TypeExpression x -> ByteString
999− renderColumnDef (ty `As` column) =
1000− renderAlias column <+> renderTypeExpression ty
1001− renderConstraints :: [TableConstraint schema columns] -> ByteString
1002− renderConstraints = \case
1004− _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
1006−data TableConstraint
1007− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1008− (columns :: [(Symbol,ColumnType)])
1009− = UnsafeTableConstraint { renderTableConstraint :: ByteString }
1012− :: (forall table. Condition '[] '[table ::: columns] 'Ungrouped)
1013− -> TableConstraint schema columns
1014−check condition = UnsafeTableConstraint $
1015− "CHECK" <+> parenthesized (renderExpression condition)
1018− (columns :: [(Symbol,ColumnType)])
1019− (columnty :: (Symbol,ColumnType))
1022− :: HasColumn column columns ty
1024− -> Column columns (column ::: ty)
1026−renderColumn :: Column columns columnty -> ByteString
1027−renderColumn (Column column) = renderAlias column
1030− :: SListI subcolumns
1031− => NP (Column columns) subcolumns
1032− -> TableConstraint schema columns
1033−unique columns = UnsafeTableConstraint $
1034− "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
1037− :: (SListI subcolumns, NotNullTypes subcolumns)
1038− => NP (Column columns) subcolumns
1039− -> TableConstraint schema columns
1040−primaryKey columns = UnsafeTableConstraint $
1041− "PRIMARY KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1043−type family SameTypes columns0 columns1 :: Constraint where
1044− SameTypes '[] '[] = ()
1045− SameTypes ((column0 ::: ty0) ': columns0) ((column1 ::: ty1) ': columns1)
1046− = (ty0 ~ ty1, SameTypes columns0 columns1)
1048−type family NotNullTypes columns :: Constraint where
1049− NotNullTypes '[] = ()
1050− NotNullTypes ((column ::: (optionality ('NotNull ty))) ': columns)
1051− = NotNullTypes columns
1054− :: ( HasTable reftable schema refcolumns
1055− , SameTypes subcolumns refsubcolumns
1056− , NotNullTypes subcolumns
1057− , SListI subcolumns
1058− , SListI refsubcolumns)
1059− => NP (Column columns) subcolumns
1061− -> NP (Column refcolumns) refsubcolumns
1062− -> TableConstraint schema columns
1063−foreignKey columns reftable refcolumns = UnsafeTableConstraint $
1064− "FOREIGN KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1065− <+> "REFERENCES" <+> renderAlias reftable
1066− <+> parenthesized (renderCommaSeparated renderColumn refcolumns)
1068−{-----------------------------------------
1070−-----------------------------------------}
1073− :: KnownSymbol table
1075− -> Definition schema (Drop table schema)
1076−dropTable table = UnsafeDefinition $ "DROP TABLE" <+> renderAlias table <> ";"
1078−{-----------------------------------------
1080−-----------------------------------------}
1083− :: HasTable table schema columns0
1085− -> AlterTable columns0 columns1
1086− -> Definition schema (Alter table schema columns1)
1087−alterTable table alteration = UnsafeDefinition $
1089− <+> renderAlias table
1090− <+> renderAlterTable alteration
1094− :: (KnownSymbol table0, KnownSymbol table1)
1097− -> Definition schema (Rename table0 table1 schema)
1098−alterTableRename table0 table1 = UnsafeDefinition $
1099− "ALTER TABLE" <+> renderAlias table0
1100− <+> "RENAME TO" <+> renderAlias table1 <> ";"
1103− (columns0 :: [(Symbol, ColumnType)])
1104− (columns1 :: [(Symbol, ColumnType)]) =
1105− UnsafeAlterTable {renderAlterTable :: ByteString}
1108− :: KnownSymbol column
1110− -> TypeExpression ('Optional ty)
1111− -> AlterTable columns (Create column ('Optional ty) columns)
1112−addColumnDefault column ty = UnsafeAlterTable $
1113− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1116− :: KnownSymbol column
1118− -> TypeExpression ('Required ('Null ty))
1119− -> AlterTable columns ((column ::: 'Required ('Null ty)) ': columns)
1120−addColumnNull column ty = UnsafeAlterTable $
1121− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1124− :: KnownSymbol column
1126− -> AlterTable columns (Drop column columns)
1127−dropColumn column = UnsafeAlterTable $
1128− "DROP COLUMN" <+> renderAlias column
1131− :: (KnownSymbol column0, KnownSymbol column1)
1134− -> AlterTable columns (Rename column0 column1 columns)
1135−renameColumn column0 column1 = UnsafeAlterTable $
1136− "RENAME COLUMN" <+> renderAlias column0 <+> "TO" <+> renderAlias column1
1139− :: (KnownSymbol column, HasColumn column columns ty0)
1141− -> AlterColumn ty0 ty1
1142− -> AlterTable columns (Alter column columns ty1)
1143−alterColumn column alteration = UnsafeAlterTable $
1144− "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
1146−data AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
1147− UnsafeAlterColumn {renderAlterColumn :: ByteString}
1150− :: Expression '[] '[] 'Ungrouped ('Required ty)
1151− -> AlterColumn ('Required ty) ('Optional ty)
1152−setDefault expression = UnsafeAlterColumn $
1153− "SET DEFAULT" <+> renderExpression expression
1155−dropDefault :: AlterColumn ('Optional ty) ('Required ty)
1156−dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
1159− :: AlterColumn (optionality ('Null ty)) (optionality ('NotNull ty))
1160−setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
1163− :: AlterColumn (optionality ('NotNull ty)) (optionality ('Null ty))
1164−dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
1167− :: TypeExpression (optionality (nullity ty1))
1168− -> AlterColumn (optionality (nullity ty0)) (optionality (nullity ty1))
1169−alterType ty = UnsafeAlterColumn $ "TYPE " <> renderTypeExpression ty
1171−{-----------------------------------------
1173−-----------------------------------------}
1176− :: (SListI columns, SListI results, HasTable table schema columns)
1178− -> ValuesClause schema params columns
1179− -> ConflictClause params columns
1180− -> ReturningClause params columns results
1181− -> Manipulation schema params results
1182−insertInto table insert conflict returning = UnsafeManipulation $
1183− "INSERT" <+> "INTO" <+> renderAlias table
1184− <+> renderValuesClause insert
1185− <> renderConflictClause conflict
1186− <> renderReturningClause returning
1189− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1190− (params :: [ColumnType])
1191− (columns :: [(Symbol,ColumnType)])
1193− (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1194− [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
1195− | ValuesQuery (Query schema params columns)
1199− => ValuesClause schema params columns
1201−renderValuesClause = \case
1203− parenthesized (renderCommaSeparated renderAliasPart row)
1206− (parenthesized . renderCommaSeparated renderValuePart <$> row:rows)
1208− renderAliasPart, renderValuePart
1209− :: Aliased (Expression params '[] 'Ungrouped) ty -> ByteString
1210− renderAliasPart (_ `As` name) = renderAlias name
1211− renderValuePart (value `As` _) = renderExpression value
1212− ValuesQuery q -> renderQuery q
1214−data ReturningClause
1215− (params :: [ColumnType])
1216− (columns :: [(Symbol,ColumnType)])
1217− (results :: [(Symbol,ColumnType)])
1219− ReturningStar :: ReturningClause params columns columns
1221− :: NP (Aliased (Expression params '[table ::: columns] 'Ungrouped))
1223− -> ReturningClause params columns results
1225−renderReturningClause
1227− => ReturningClause params columns results
1229−renderReturningClause = \case
1230− ReturningStar -> " RETURNING *;"
1231− Returning Nil -> ";"
1232− Returning results -> " RETURNING"
1233− <+> renderCommaSeparated (renderAliased renderExpression) results <> ";"
1235−data ConflictClause params columns where
1236− Conflict :: ConflictClause params columns
1237− OnConflictDoNothing :: ConflictClause params columns
1239− :: NP (Aliased (UpdateExpression params '[table ::: columns])) columns
1240− -> Condition params '[table ::: columns] 'Ungrouped
1241− -> ConflictClause params columns
1243−renderConflictClause
1245− => ConflictClause params columns
1247−renderConflictClause = \case
1249− OnConflictDoNothing -> " ON CONFLICT DO NOTHING"
1250− OnConflictDoUpdate updates wh
1251− -> " ON CONFLICT DO UPDATE SET"
1252− <+> renderCommaSeparatedMaybe renderUpdateExpression updates
1253− <+> "WHERE" <+> renderExpression wh
1255−{-----------------------------------------
1257−-----------------------------------------}
1259−data UpdateExpression params table ty
1261− | Set (Expression params table 'Ungrouped ty)
1263−renderUpdateExpression
1264− :: Aliased (UpdateExpression params '[table ::: columns]) column
1265− -> Maybe ByteString
1266−renderUpdateExpression = \case
1267− Same `As` _ -> Nothing
1268− Set expression `As` column -> Just $
1269− renderAlias column <+> "=" <+> renderExpression expression
1272− :: (HasTable table schema columns, SListI columns, SListI results)
1274− -> NP (Aliased (UpdateExpression params '[table ::: columns])) columns
1275− -> Condition params '[table ::: columns] 'Ungrouped
1276− -> ReturningClause params columns results
1277− -> Manipulation schema params results
1278−update table columns wh returning = UnsafeManipulation $
1280− <+> renderAlias table
1282− <+> renderCommaSeparatedMaybe renderUpdateExpression columns
1283− <+> "WHERE" <+> renderExpression wh
1284− <> renderReturningClause returning
1286−{-----------------------------------------
1288−-----------------------------------------}
1291− :: HasTable table schema columns
1293− -> Condition params '[table ::: columns] 'Ungrouped
1294− -> Manipulation schema params '[]
1295−deleteFrom table wh = UnsafeManipulation $
1296− "DELETE FROM" <+> renderAlias table
1297− <+> "WHERE" <+> renderExpression wh <> ";"
1299−{-----------------------------------------
1300−render helper functions
1301−-----------------------------------------}
1303−parenthesized :: ByteString -> ByteString
1304−parenthesized str = "(" <> str <> ")"
1306−(<+>) :: ByteString -> ByteString -> ByteString
1307−str1 <+> str2 = str1 <> " " <> str2
1309−commaSeparated :: [ByteString] -> ByteString
1310−commaSeparated = ByteString.intercalate ", "
1312−renderCommaSeparated
1314− => (forall x. expression x -> ByteString)
1315− -> NP expression xs -> ByteString
1316−renderCommaSeparated render
1319− . hmap (K . render)
1321−renderCommaSeparatedMaybe
1323− => (forall x. expression x -> Maybe ByteString)
1324− -> NP expression xs -> ByteString
1325−renderCommaSeparatedMaybe render
1329− . hmap (K . render)
1331−renderNat :: KnownNat n => proxy n -> ByteString
1332−renderNat (_ :: proxy n) = fromString (show (natVal' (proxy# :: Proxy# n)))