@@ -1,1372 +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 '[] 'Ungrouped ('Optional (nullity ty))
84−def = UnsafeExpression "DEFAULT"
87− :: Expression params '[] 'Ungrouped ('Required (nullity ty))
88− -> Expression params '[] 'Ungrouped ('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 ">"
383− :: Expression params tables grouping ('Required (nullity 'PGdate))
384−currentDate = UnsafeExpression "CURRENT_DATE"
387− :: Expression params tables grouping ('Required (nullity 'PGtimetz))
388−currentTime = UnsafeExpression "CURRENT_TIME"
391− :: Expression params tables grouping ('Required (nullity 'PGtimestamptz))
392−currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
395− :: Expression params tables grouping ('Required (nullity 'PGtime))
396−localTime = UnsafeExpression "LOCALTIME"
399− :: Expression params tables grouping ('Required (nullity 'PGtimestamp))
400−localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
402−data SortExpression params tables grouping where
404− :: Expression params tables grouping ('Required ('NotNull ty))
405− -> SortExpression params tables grouping
407− :: Expression params tables grouping ('Required ('NotNull ty))
408− -> SortExpression params tables grouping
410− :: Expression params tables grouping ('Required ('Null ty))
411− -> SortExpression params tables grouping
413− :: Expression params tables grouping ('Required ('Null ty))
414− -> SortExpression params tables grouping
416− :: Expression params tables grouping ('Required ('Null ty))
417− -> SortExpression params tables grouping
419− :: Expression params tables grouping ('Required ('Null ty))
420− -> SortExpression params tables grouping
422−renderSortExpression :: SortExpression params tables grouping -> ByteString
423−renderSortExpression = \case
424− Asc expression -> renderExpression expression <+> "ASC"
425− Desc expression -> renderExpression expression <+> "DESC"
426− AscNullsFirst expression -> renderExpression expression
427− <+> "ASC NULLS FIRST"
428− DescNullsFirst expression -> renderExpression expression
429− <+> "DESC NULLS FIRST"
430− AscNullsLast expression -> renderExpression expression <+> "ASC NULLS LAST"
431− DescNullsLast expression -> renderExpression expression <+> "DESC NULLS LAST"
434−{-----------------------------------------
436−-----------------------------------------}
439− (tables :: [(Symbol,[(Symbol,ColumnType)])])
440− (tabcol :: (Symbol,Symbol)) where
442− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
444− -> By tables '(table, column)
446− :: (HasTable table tables columns, HasColumn column columns ty)
447− => (Alias table, Alias column)
448− -> By tables '(table, column)
450−renderBy :: By tables tabcolty -> ByteString
452− By column -> renderAlias column
453− By2 (table, column) -> renderAlias table <> "." <> renderAlias column
455−data GroupByClause tables grouping where
456− NoGroups :: GroupByClause tables 'Ungrouped
459− => NP (By tables) bys
460− -> GroupByClause tables ('Grouped bys)
462−renderGroupByClause :: GroupByClause tables grouping -> ByteString
463−renderGroupByClause = \case
466− Group bys -> " GROUP BY" <+> renderCommaSeparated renderBy bys
468−data HavingClause params tables grouping where
469− NoHaving :: HavingClause params tables 'Ungrouped
471− :: [Condition params tables ('Grouped bys)]
472− -> HavingClause params tables ('Grouped bys)
474−renderHavingClause :: HavingClause params tables grouping -> ByteString
475−renderHavingClause = \case
478− Having conditions ->
479− " HAVING" <+> commaSeparated (renderExpression <$> conditions)
481−class (KnownSymbol table, KnownSymbol column)
482− => GroupedBy table column bys where
484− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
486− -> Expression params tables ('Grouped bys) ty
487− getGroup1 column = UnsafeExpression $ renderAlias column
489− :: (HasTable table tables columns, HasColumn column columns ty)
492− -> Expression params tables ('Grouped bys) ty
493− getGroup2 table column = UnsafeExpression $
494− renderAlias table <> "." <> renderAlias column
495−instance {-# OVERLAPPING #-} (KnownSymbol table, KnownSymbol column)
496− => GroupedBy table column ('(table,column) ': bys)
497−instance {-# OVERLAPPABLE #-}
499− , KnownSymbol column
500− , GroupedBy table column bys
501− ) => GroupedBy table column (tabcol ': bys)
504− ( tables ~ '[table ::: columns]
505− , HasColumn column columns ty
506− , GroupedBy table column bys
508− (Expression params tables ('Grouped bys) ty) where
509− fromLabel = getGroup1 (Alias @column)
512− ( HasTable table tables columns
513− , HasColumn column columns ty
514− , GroupedBy table column bys
515− ) => IsTableColumn table column
516− (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
520− -> Expression params tables 'Ungrouped ('Required xty)
521− -> Expression params tables ('Grouped bys) ('Required yty)
522−unsafeAggregate fun x = UnsafeExpression $ mconcat
523− [fun, "(", renderExpression x, ")"]
527− => Expression params tables 'Ungrouped ('Required (nullity ty))
528− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
529−sum_ = unsafeAggregate "sum"
533− => Expression params tables 'Ungrouped ('Required (nullity int))
534− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGnumeric))
535−avgInt = unsafeAggregate "avg"
539− => Expression params tables 'Ungrouped ('Required (nullity float))
540− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGfloat8))
541−avgFloat = unsafeAggregate "avg"
545− => Expression params tables 'Ungrouped ('Required (nullity int))
546− -> Expression params tables ('Grouped bys) ('Required (nullity int))
547−bitAnd = unsafeAggregate "bit_and"
551− => Expression params tables 'Ungrouped ('Required (nullity int))
552− -> Expression params tables ('Grouped bys) ('Required (nullity int))
553−bitOr = unsafeAggregate "bit_or"
556− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
557− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
558−boolAnd = unsafeAggregate "bool_and"
561− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
562− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
563−boolOr = unsafeAggregate "bool_or"
566− :: Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
567−countStar = UnsafeExpression "count(*)"
570− :: Expression params tables 'Ungrouped ('Required ty)
571− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGint8))
572−count = unsafeAggregate "count"
575− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
576− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
577−every = unsafeAggregate "every"
580− :: Expression params tables 'Ungrouped ('Required ty)
581− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
582−jsonAgg = unsafeAggregate "json_agg"
585− :: Expression params tables 'Ungrouped ('Required ty)
586− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
587−jsonbAgg = unsafeAggregate "jsonb_agg"
590− :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
591− -> Expression params tables 'Ungrouped ('Required valty)
592− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
593−jsonObjectAgg k v = UnsafeExpression $
594− "json_object_agg(" <> renderExpression k
595− <> ", " <> renderExpression v <> ")"
598− :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
599− -> Expression params tables 'Ungrouped ('Required valty)
600− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
601−jsonbObjectAgg k v = UnsafeExpression $
602− "jsonb_object_agg(" <> renderExpression k
603− <> ", " <> renderExpression v <> ")"
606− :: Expression params tables 'Ungrouped ('Required (nullity ty))
607− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
608−max_ = unsafeAggregate "max"
611− :: Expression params tables 'Ungrouped ('Required (nullity ty))
612− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
613−min_ = unsafeAggregate "min"
615−{-----------------------------------------
617−-----------------------------------------}
620− (schema :: [(Symbol,[(Symbol,ColumnType)])])
621− (columns :: [(Symbol,ColumnType)])
622− = UnsafeTable { renderTable :: ByteString }
625−class KnownSymbol table => HasTable table tables columns
626− | table tables -> columns where
627− getTable :: Alias table -> Table tables columns
628− getTable table = UnsafeTable $ renderAlias table
629−instance {-# OVERLAPPING #-} KnownSymbol table
630− => HasTable table ((table ::: columns) ': tables) columns
631−instance {-# OVERLAPPABLE #-}
632− (KnownSymbol table, HasTable table schema columns)
633− => HasTable table (table' ': schema) columns
635−instance HasTable table schema columns
636− => IsLabel table (Table schema columns) where
637− fromLabel = getTable (Alias @table)
639−data FromClause params schema tables where
641− :: Aliased (Table schema) table
642− -> FromClause params schema '[table]
644− :: Aliased (Query schema params) table
645− -> FromClause params schema '[table]
647− :: FromClause params schema right
648− -> FromClause params schema left
649− -> FromClause params schema (Join left right)
651− :: FromClause params schema right
652− -> Condition params (Join left right) 'Ungrouped
653− -> FromClause params schema left
654− -> FromClause params schema (Join left right)
656− :: FromClause params schema right
657− -> Condition params (Join left right) 'Ungrouped
658− -> FromClause params schema left
659− -> FromClause params schema (Join left (NullifyTables right))
661− :: FromClause params schema right
662− -> Condition params (Join left right) 'Ungrouped
663− -> FromClause params schema left
664− -> FromClause params schema (Join (NullifyTables left) right)
666− :: FromClause params schema right
667− -> Condition params (Join left right) 'Ungrouped
668− -> FromClause params schema left
669− -> FromClause params schema
670− (Join (NullifyTables left) (NullifyTables right))
672−renderFromClause :: FromClause params schema tables -> ByteString
673−renderFromClause = \case
674− Table table -> renderAliased renderTable table
675− Subquery selection -> renderAliased renderQuery selection
676− CrossJoin right left ->
677− renderFromClause left <+> "CROSS JOIN" <+> renderFromClause right
678− InnerJoin right on left -> renderJoin "INNER JOIN" right on left
679− LeftOuterJoin right on left -> renderJoin "LEFT OUTER JOIN" right on left
680− RightOuterJoin right on left -> renderJoin "RIGHT OUTER JOIN" right on left
681− FullOuterJoin right on left -> renderJoin "FULL OUTER JOIN" right on left
683− renderJoin op right on left =
684− renderFromClause left <+> op <+> renderFromClause right
685− <+> "ON" <+> renderExpression on
688− (params :: [ColumnType])
689− (schema :: [(Symbol,[(Symbol,ColumnType)])])
690− (tables :: [(Symbol,[(Symbol,ColumnType)])])
691− (grouping :: Grouping)
693− { fromClause :: FromClause params schema tables
694− , whereClause :: [Condition params tables 'Ungrouped]
695− , groupByClause :: GroupByClause tables grouping
696− , havingClause :: HavingClause params tables grouping
697− , orderByClause :: [SortExpression params tables grouping]
698− , limitClause :: [Word64]
699− , offsetClause :: [Word64]
702−renderTableExpression
703− :: TableExpression params schema tables grouping
705−renderTableExpression
706− (TableExpression tables whs' grps' hvs' srts' lims' offs') = mconcat
707− [ "FROM ", renderFromClause tables
709− , renderGroupByClause grps'
710− , renderHavingClause hvs'
712− , renderLimits lims'
713− , renderOffsets offs'
716− renderWheres = \case
718− wh:[] -> " WHERE" <+> renderExpression wh
719− wh:whs -> " WHERE" <+> renderExpression (foldr (&&*) wh whs)
723− <+> commaSeparated (renderSortExpression <$> srts)
724− renderLimits = \case
726− lims -> " LIMIT" <+> fromString (show (minimum lims))
727− renderOffsets = \case
729− offs -> " OFFSET" <+> fromString (show (sum offs))
732− :: FromClause params schema tables
733− -> TableExpression params schema tables 'Ungrouped
734−from tables = TableExpression tables [] NoGroups NoHaving [] [] []
737− :: Condition params tables 'Ungrouped
738− -> TableExpression params schema tables grouping
739− -> TableExpression params schema tables grouping
740−where_ wh tables = tables {whereClause = wh : whereClause tables}
744− => NP (By tables) bys
745− -> TableExpression params schema tables 'Ungrouped
746− -> TableExpression params schema tables ('Grouped bys)
747−group bys tables = TableExpression
748− { fromClause = fromClause tables
749− , whereClause = whereClause tables
750− , groupByClause = Group bys
751− , havingClause = Having []
752− , orderByClause = []
753− , limitClause = limitClause tables
754− , offsetClause = offsetClause tables
758− :: Condition params tables ('Grouped bys)
759− -> TableExpression params schema tables ('Grouped bys)
760− -> TableExpression params schema tables ('Grouped bys)
761−having hv tables = tables
762− { havingClause = case havingClause tables of Having hvs -> Having (hv:hvs) }
765− :: [SortExpression params tables grouping]
766− -> TableExpression params schema tables grouping
767− -> TableExpression params schema tables grouping
768−orderBy srts tables = tables {orderByClause = orderByClause tables ++ srts}
772− -> TableExpression params schema tables grouping
773− -> TableExpression params schema tables grouping
774−limit lim tables = tables {limitClause = lim : limitClause tables}
778− -> TableExpression params schema tables grouping
779− -> TableExpression params schema tables grouping
780−offset off tables = tables {offsetClause = off : offsetClause tables}
782−{-----------------------------------------
784−-----------------------------------------}
786−newtype TypeExpression (ty :: ColumnType)
787− = UnsafeTypeExpression { renderTypeExpression :: ByteString }
790−bool :: TypeExpression ('Required ('Null 'PGbool))
791−bool = UnsafeTypeExpression "bool"
792−int2 :: TypeExpression ('Required ('Null 'PGint2))
793−int2 = UnsafeTypeExpression "int2"
794−smallint :: TypeExpression ('Required ('Null 'PGint2))
795−smallint = UnsafeTypeExpression "smallint"
796−int4 :: TypeExpression ('Required ('Null 'PGint4))
797−int4 = UnsafeTypeExpression "int4"
798−int :: TypeExpression ('Required ('Null 'PGint4))
799−int = UnsafeTypeExpression "int"
800−integer :: TypeExpression ('Required ('Null 'PGint4))
801−integer = UnsafeTypeExpression "integer"
802−int8 :: TypeExpression ('Required ('Null 'PGint8))
803−int8 = UnsafeTypeExpression "int8"
804−bigint :: TypeExpression ('Required ('Null 'PGint8))
805−bigint = UnsafeTypeExpression "bigint"
806−numeric :: TypeExpression ('Required ('Null 'PGnumeric))
807−numeric = UnsafeTypeExpression "numeric"
808−float4 :: TypeExpression ('Required ('Null 'PGfloat4))
809−float4 = UnsafeTypeExpression "float4"
810−real :: TypeExpression ('Required ('Null 'PGfloat4))
811−real = UnsafeTypeExpression "real"
812−float8 :: TypeExpression ('Required ('Null 'PGfloat8))
813−float8 = UnsafeTypeExpression "float8"
814−doublePrecision :: TypeExpression ('Required ('Null 'PGfloat8))
815−doublePrecision = UnsafeTypeExpression "double precision"
816−serial2 :: TypeExpression ('Optional ('NotNull 'PGint2))
817−serial2 = UnsafeTypeExpression "serial2"
818−smallserial :: TypeExpression ('Optional ('NotNull 'PGint2))
819−smallserial = UnsafeTypeExpression "smallserial"
820−serial4 :: TypeExpression ('Optional ('NotNull 'PGint4))
821−serial4 = UnsafeTypeExpression "serial4"
822−serial :: TypeExpression ('Optional ('NotNull 'PGint4))
823−serial = UnsafeTypeExpression "serial"
824−serial8 :: TypeExpression ('Optional ('NotNull 'PGint8))
825−serial8 = UnsafeTypeExpression "serial8"
826−bigserial :: TypeExpression ('Optional ('NotNull 'PGint8))
827−bigserial = UnsafeTypeExpression "bigserial"
828−text :: TypeExpression ('Required ('Null 'PGtext))
829−text = UnsafeTypeExpression "text"
833− -> TypeExpression ('Required ('Null ('PGchar n)))
834−char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
838− -> TypeExpression ('Required ('Null ('PGchar n)))
839−character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
843− -> TypeExpression ('Required ('Null ('PGvarchar n)))
844−varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
848− -> TypeExpression ('Required ('Null ('PGvarchar n)))
849−characterVarying p = UnsafeTypeExpression $
850− "character varying(" <> renderNat p <> ")"
851−bytea :: TypeExpression ('Required ('Null ('PGbytea)))
852−bytea = UnsafeTypeExpression "bytea"
853−timestamp :: TypeExpression ('Required ('Null ('PGtimestamp)))
854−timestamp = UnsafeTypeExpression "timestamp"
855−timestampWithTimeZone :: TypeExpression ('Required ('Null ('PGtimestamptz)))
856−timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
857−date :: TypeExpression ('Required ('Null ('PGdate)))
858−date = UnsafeTypeExpression "date"
859−time :: TypeExpression ('Required ('Null ('PGtime)))
860−time = UnsafeTypeExpression "time"
861−timeWithTimeZone :: TypeExpression ('Required ('Null ('PGtimetz)))
862−timeWithTimeZone = UnsafeTypeExpression "time with time zone"
863−interval :: TypeExpression ('Required ('Null ('PGinterval)))
864−interval = UnsafeTypeExpression "interval"
865−uuid :: TypeExpression ('Required ('Null ('PGuuid)))
866−uuid = UnsafeTypeExpression "uuid"
867−json :: TypeExpression ('Required ('Null ('PGjson)))
868−json = UnsafeTypeExpression "json"
869−jsonb :: TypeExpression ('Required ('Null ('PGjsonb)))
870−jsonb = UnsafeTypeExpression "jsonb"
873− :: TypeExpression (optionality ('Null ty))
874− -> TypeExpression (optionality ('NotNull ty))
875−notNull ty = UnsafeTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
877− :: Expression '[] '[] 'Ungrouped ('Required ty)
878− -> TypeExpression ('Required ty)
879− -> TypeExpression ('Optional ty)
880−default_ x ty = UnsafeTypeExpression $
881− renderTypeExpression ty <+> "DEFAULT" <+> renderExpression x
883−class PGTyped (ty :: PGType) where
884− pgtype :: TypeExpression ('Required ('Null ty))
885−instance PGTyped 'PGbool where pgtype = bool
886−instance PGTyped 'PGint2 where pgtype = int2
887−instance PGTyped 'PGint4 where pgtype = int4
888−instance PGTyped 'PGint8 where pgtype = int8
889−instance PGTyped 'PGnumeric where pgtype = numeric
890−instance PGTyped 'PGfloat4 where pgtype = float4
891−instance PGTyped 'PGfloat8 where pgtype = float8
892−instance PGTyped 'PGtext where pgtype = text
893−instance KnownNat n => PGTyped ('PGchar n) where pgtype = char (Proxy @n)
894−instance KnownNat n => PGTyped ('PGvarchar n) where pgtype = varchar (Proxy @n)
895−instance PGTyped 'PGbytea where pgtype = bytea
896−instance PGTyped 'PGtimestamp where pgtype = timestamp
897−instance PGTyped 'PGtimestamptz where pgtype = timestampWithTimeZone
898−instance PGTyped 'PGdate where pgtype = date
899−instance PGTyped 'PGtime where pgtype = time
900−instance PGTyped 'PGtimetz where pgtype = timeWithTimeZone
901−instance PGTyped 'PGinterval where pgtype = interval
902−instance PGTyped 'PGuuid where pgtype = uuid
903−instance PGTyped 'PGjson where pgtype = json
904−instance PGTyped 'PGjsonb where pgtype = jsonb
906−{-----------------------------------------
908−-----------------------------------------}
911− (schema0 :: [(Symbol,[(Symbol,ColumnType)])])
912− (schema1 :: [(Symbol,[(Symbol,ColumnType)])])
913− = UnsafeDefinition { renderDefinition :: ByteString }
915−instance Category Definition where
916− id = UnsafeDefinition ";"
917− ddl1 . ddl0 = UnsafeDefinition $
918− renderDefinition ddl0 <+> renderDefinition ddl1
921− (schema :: [(Symbol,[(Symbol,ColumnType)])])
922− (params :: [ColumnType])
923− (columns :: [(Symbol,ColumnType)])
924− = UnsafeManipulation { renderManipulation :: ByteString }
927− (schema :: [(Symbol,[(Symbol,ColumnType)])])
928− (params :: [ColumnType])
929− (columns :: [(Symbol,ColumnType)])
930− = UnsafeQuery { renderQuery :: ByteString }
933− :: Query schema params columns
934− -> Manipulation schema params columns
935−queryStatement q = UnsafeManipulation $ renderQuery q <> ";"
937−union, unionAll, intersect, intersectAll, except, exceptAll
938− :: Query schema params columns
939− -> Query schema params columns
940− -> Query schema params columns
941−q1 `union` q2 = UnsafeQuery $
942− parenthesized (renderQuery q1)
944− <+> parenthesized (renderQuery q2)
945−q1 `unionAll` q2 = UnsafeQuery $
946− parenthesized (renderQuery q1)
947− <+> "UNION" <+> "ALL"
948− <+> parenthesized (renderQuery q2)
949−q1 `intersect` q2 = UnsafeQuery $
950− parenthesized (renderQuery q1)
952− <+> parenthesized (renderQuery q2)
953−q1 `intersectAll` q2 = UnsafeQuery $
954− parenthesized (renderQuery q1)
955− <+> "INTERSECT" <+> "ALL"
956− <+> parenthesized (renderQuery q2)
957−q1 `except` q2 = UnsafeQuery $
958− parenthesized (renderQuery q1)
960− <+> parenthesized (renderQuery q2)
961−q1 `exceptAll` q2 = UnsafeQuery $
962− parenthesized (renderQuery q1)
963− <+> "EXCEPT" <+> "ALL"
964− <+> parenthesized (renderQuery q2)
966−{-----------------------------------------
968−-----------------------------------------}
972− => NP (Aliased (Expression params tables grouping)) (column ': columns)
973− -> TableExpression params schema tables grouping
974− -> Query schema params (column ': columns)
975−select list tabs = UnsafeQuery $
977− <+> renderCommaSeparated (renderAliased renderExpression) list
978− <+> renderTableExpression tabs
981− :: tables ~ '[table ::: columns]
982− => TableExpression params schema tables 'Ungrouped
983− -> Query schema params columns
984−selectStar tabs = UnsafeQuery $ "SELECT" <+> "*" <+> renderTableExpression tabs
987− :: HasTable table tables columns
989− -> TableExpression params schema tables 'Ungrouped
990− -> Query schema params columns
991−selectDotStar table tables = UnsafeQuery $
992− "SELECT" <+> renderAlias table <> ".*" <+> renderTableExpression tables
996− => NP (Aliased (Expression params '[] 'Ungrouped)) columns
997− -> Query schema params columns
998−selectRow row = UnsafeQuery $
999− "SELECT" <+> renderCommaSeparated (renderAliased renderExpression) row
1001−{-----------------------------------------
1003−-----------------------------------------}
1006− :: (KnownSymbol table, SListI columns)
1008− -> NP (Aliased TypeExpression) (column ': columns)
1009− -> [TableConstraint schema (column ': columns)]
1010− -> Definition schema (Create table (column ': columns) schema)
1011−createTable table columns constraints = UnsafeDefinition $
1012− "CREATE TABLE" <+> renderAlias table
1014− ( renderCommaSeparated renderColumnDef columns
1015− <> renderConstraints constraints )
1018− renderColumnDef :: Aliased TypeExpression x -> ByteString
1019− renderColumnDef (ty `As` column) =
1020− renderAlias column <+> renderTypeExpression ty
1021− renderConstraints :: [TableConstraint schema columns] -> ByteString
1022− renderConstraints = \case
1024− _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
1026−data TableConstraint
1027− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1028− (columns :: [(Symbol,ColumnType)])
1029− = UnsafeTableConstraint { renderTableConstraint :: ByteString }
1032− :: Condition '[] '[table ::: columns] 'Ungrouped
1033− -> TableConstraint schema columns
1034−check condition = UnsafeTableConstraint $
1035− "CHECK" <+> parenthesized (renderExpression condition)
1038− (columns :: [(Symbol,ColumnType)])
1039− (columnty :: (Symbol,ColumnType))
1042− :: HasColumn column columns ty
1044− -> Column columns (column ::: ty)
1046−renderColumn :: Column columns columnty -> ByteString
1047−renderColumn (Column column) = renderAlias column
1050− :: SListI subcolumns
1051− => NP (Column columns) subcolumns
1052− -> TableConstraint schema columns
1053−unique columns = UnsafeTableConstraint $
1054− "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
1057− :: (SListI subcolumns, AllNotNull subcolumns)
1058− => NP (Column columns) subcolumns
1059− -> TableConstraint schema columns
1060−primaryKey columns = UnsafeTableConstraint $
1061− "PRIMARY KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1064− :: ( HasTable reftable schema refcolumns
1065− , SameTypes subcolumns refsubcolumns
1066− , NotAllNull subcolumns
1067− , SListI subcolumns
1068− , SListI refsubcolumns)
1069− => NP (Column columns) subcolumns
1071− -> NP (Column refcolumns) refsubcolumns
1074− -> TableConstraint schema columns
1075−foreignKey columns reftable refcolumns onDelete onUpdate =
1076− UnsafeTableConstraint $
1077− "FOREIGN KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1078− <+> "REFERENCES" <+> renderAlias reftable
1079− <+> parenthesized (renderCommaSeparated renderColumn refcolumns)
1080− <+> renderOnDelete onDelete
1081− <+> renderOnUpdate onUpdate
1088−renderOnDelete :: OnDelete -> ByteString
1089−renderOnDelete = \case
1090− OnDeleteNoAction -> "ON DELETE NO ACTION"
1091− OnDeleteRestrict -> "ON DELETE RESTRICT"
1092− OnDeleteCascade -> "ON DELETE CASCADE"
1099−renderOnUpdate :: OnUpdate -> ByteString
1100−renderOnUpdate = \case
1101− OnUpdateNoAction -> "ON UPDATE NO ACTION"
1102− OnUpdateRestrict -> "ON UPDATE RESTRICT"
1103− OnUpdateCascade -> "ON UPDATE CASCADE"
1105−{-----------------------------------------
1107−-----------------------------------------}
1110− :: KnownSymbol table
1112− -> Definition schema (Drop table schema)
1113−dropTable table = UnsafeDefinition $ "DROP TABLE" <+> renderAlias table <> ";"
1115−{-----------------------------------------
1117−-----------------------------------------}
1120− :: HasTable table schema columns0
1122− -> AlterTable columns0 columns1
1123− -> Definition schema (Alter table schema columns1)
1124−alterTable table alteration = UnsafeDefinition $
1126− <+> renderAlias table
1127− <+> renderAlterTable alteration
1131− :: (KnownSymbol table0, KnownSymbol table1)
1134− -> Definition schema (Rename table0 table1 schema)
1135−alterTableRename table0 table1 = UnsafeDefinition $
1136− "ALTER TABLE" <+> renderAlias table0
1137− <+> "RENAME TO" <+> renderAlias table1 <> ";"
1140− (columns0 :: [(Symbol, ColumnType)])
1141− (columns1 :: [(Symbol, ColumnType)]) =
1142− UnsafeAlterTable {renderAlterTable :: ByteString}
1145− :: KnownSymbol column
1147− -> TypeExpression ('Optional ty)
1148− -> AlterTable columns (Create column ('Optional ty) columns)
1149−addColumnDefault column ty = UnsafeAlterTable $
1150− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1153− :: KnownSymbol column
1155− -> TypeExpression ('Required ('Null ty))
1156− -> AlterTable columns ((column ::: 'Required ('Null ty)) ': columns)
1157−addColumnNull column ty = UnsafeAlterTable $
1158− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1161− :: KnownSymbol column
1163− -> AlterTable columns (Drop column columns)
1164−dropColumn column = UnsafeAlterTable $
1165− "DROP COLUMN" <+> renderAlias column
1168− :: (KnownSymbol column0, KnownSymbol column1)
1171− -> AlterTable columns (Rename column0 column1 columns)
1172−renameColumn column0 column1 = UnsafeAlterTable $
1173− "RENAME COLUMN" <+> renderAlias column0 <+> "TO" <+> renderAlias column1
1176− :: (KnownSymbol column, HasColumn column columns ty0)
1178− -> AlterColumn ty0 ty1
1179− -> AlterTable columns (Alter column columns ty1)
1180−alterColumn column alteration = UnsafeAlterTable $
1181− "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
1183−data AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
1184− UnsafeAlterColumn {renderAlterColumn :: ByteString}
1187− :: Expression '[] '[] 'Ungrouped ('Required ty)
1188− -> AlterColumn ('Required ty) ('Optional ty)
1189−setDefault expression = UnsafeAlterColumn $
1190− "SET DEFAULT" <+> renderExpression expression
1192−dropDefault :: AlterColumn ('Optional ty) ('Required ty)
1193−dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
1196− :: AlterColumn (optionality ('Null ty)) (optionality ('NotNull ty))
1197−setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
1200− :: AlterColumn (optionality ('NotNull ty)) (optionality ('Null ty))
1201−dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
1204− :: TypeExpression (optionality (nullity ty1))
1205− -> AlterColumn (optionality (nullity ty0)) (optionality (nullity ty1))
1206−alterType ty = UnsafeAlterColumn $ "TYPE " <> renderTypeExpression ty
1208−{-----------------------------------------
1210−-----------------------------------------}
1213− :: (SListI columns, SListI results, HasTable table schema columns)
1215− -> ValuesClause schema params columns
1216− -> ConflictClause params columns
1217− -> ReturningClause params columns results
1218− -> Manipulation schema params results
1219−insertInto table insert conflict returning = UnsafeManipulation $
1220− "INSERT" <+> "INTO" <+> renderAlias table
1221− <+> renderValuesClause insert
1222− <> renderConflictClause conflict
1223− <> renderReturningClause returning
1226− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1227− (params :: [ColumnType])
1228− (columns :: [(Symbol,ColumnType)])
1230− (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1231− [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
1232− | ValuesQuery (Query schema params columns)
1236− => ValuesClause schema params columns
1238−renderValuesClause = \case
1240− parenthesized (renderCommaSeparated renderAliasPart row)
1243− (parenthesized . renderCommaSeparated renderValuePart <$> row:rows)
1245− renderAliasPart, renderValuePart
1246− :: Aliased (Expression params '[] 'Ungrouped) ty -> ByteString
1247− renderAliasPart (_ `As` name) = renderAlias name
1248− renderValuePart (value `As` _) = renderExpression value
1249− ValuesQuery q -> renderQuery q
1251−data ReturningClause
1252− (params :: [ColumnType])
1253− (columns :: [(Symbol,ColumnType)])
1254− (results :: [(Symbol,ColumnType)])
1256− ReturningStar :: ReturningClause params columns columns
1259− (Aliased (Expression params '[table ::: columns] 'Ungrouped))
1261− -> ReturningClause params columns results
1263−renderReturningClause
1265− => ReturningClause params columns results
1267−renderReturningClause = \case
1268− ReturningStar -> " RETURNING *;"
1269− Returning Nil -> ";"
1270− Returning results -> " RETURNING"
1271− <+> renderCommaSeparated (renderAliased renderExpression) results <> ";"
1273−data ConflictClause params columns where
1274− Conflict :: ConflictClause params columns
1275− OnConflictDoNothing :: ConflictClause params columns
1277− :: NP (Aliased (UpdateExpression params columns)) columns
1278− -> Maybe (Condition params '[table ::: columns] 'Ungrouped)
1279− -> ConflictClause params columns
1281−renderConflictClause
1283− => ConflictClause params columns
1285−renderConflictClause = \case
1287− OnConflictDoNothing -> " ON CONFLICT DO NOTHING"
1288− OnConflictDoUpdate updates whMaybe
1289− -> " ON CONFLICT DO UPDATE SET"
1290− <+> renderCommaSeparatedMaybe renderUpdateExpression updates
1293− Just wh -> " WHERE" <+> renderExpression wh
1295−{-----------------------------------------
1297−-----------------------------------------}
1299−data UpdateExpression params columns ty
1301− | Set (forall table. Expression params '[table ::: columns] 'Ungrouped ty)
1303−renderUpdateExpression
1304− :: Aliased (UpdateExpression params columns) column
1305− -> Maybe ByteString
1306−renderUpdateExpression = \case
1307− Same `As` _ -> Nothing
1308− Set expression `As` column -> Just $
1309− renderAlias column <+> "=" <+> renderExpression expression
1312− :: (HasTable table schema columns, SListI columns, SListI results)
1314− -> NP (Aliased (UpdateExpression params columns)) columns
1315− -> Condition params '[tab ::: columns] 'Ungrouped
1316− -> ReturningClause params columns results
1317− -> Manipulation schema params results
1318−update table columns wh returning = UnsafeManipulation $
1320− <+> renderAlias table
1322− <+> renderCommaSeparatedMaybe renderUpdateExpression columns
1323− <+> "WHERE" <+> renderExpression wh
1324− <> renderReturningClause returning
1326−{-----------------------------------------
1328−-----------------------------------------}
1331− :: HasTable table schema columns
1333− -> Condition params '[table ::: columns] 'Ungrouped
1334− -> Manipulation schema params '[]
1335−deleteFrom table wh = UnsafeManipulation $
1336− "DELETE FROM" <+> renderAlias table
1337− <+> "WHERE" <+> renderExpression wh <> ";"
1339−{-----------------------------------------
1340−render helper functions
1341−-----------------------------------------}
1343−parenthesized :: ByteString -> ByteString
1344−parenthesized str = "(" <> str <> ")"
1346−(<+>) :: ByteString -> ByteString -> ByteString
1347−str1 <+> str2 = str1 <> " " <> str2
1349−commaSeparated :: [ByteString] -> ByteString
1350−commaSeparated = ByteString.intercalate ", "
1352−renderCommaSeparated
1354− => (forall x. expression x -> ByteString)
1355− -> NP expression xs -> ByteString
1356−renderCommaSeparated render
1359− . hmap (K . render)
1361−renderCommaSeparatedMaybe
1363− => (forall x. expression x -> Maybe ByteString)
1364− -> NP expression xs -> ByteString
1365−renderCommaSeparatedMaybe render
1369− . hmap (K . render)
1371−renderNat :: KnownNat n => proxy n -> ByteString
1372−renderNat (_ :: proxy n) = fromString (show (natVal' (proxy# :: Proxy# n)))