@@ -1,1313 +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 ty
385− -> SortExpression params tables grouping
387− :: Expression params tables grouping ty
388− -> SortExpression params tables grouping
390−renderSortExpression :: SortExpression params tables grouping -> ByteString
391−renderSortExpression = \case
392− Asc expression -> renderExpression expression <+> "ASC"
393− Desc expression -> renderExpression expression <+> "DESC"
395−{-----------------------------------------
397−-----------------------------------------}
400− (tables :: [(Symbol,[(Symbol,ColumnType)])])
401− (tabcol :: (Symbol,Symbol)) where
403− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
405− -> By tables '(table, column)
407− :: (HasTable table tables columns, HasColumn column columns ty)
408− => (Alias table, Alias column)
409− -> By tables '(table, column)
411−renderBy :: By tables tabcolty -> ByteString
413− By column -> renderAlias column
414− By2 (table, column) -> renderAlias table <> "." <> renderAlias column
416−data GroupByClause tables grouping where
417− NoGroups :: GroupByClause tables 'Ungrouped
420− => NP (By tables) bys
421− -> GroupByClause tables ('Grouped bys)
423−renderGroupByClause :: GroupByClause tables grouping -> ByteString
424−renderGroupByClause = \case
427− Group bys -> " GROUP BY" <+> renderCommaSeparated renderBy bys
429−data HavingClause params tables grouping where
430− NoHaving :: HavingClause params tables 'Ungrouped
432− :: [Condition params tables ('Grouped bys)]
433− -> HavingClause params tables ('Grouped bys)
435−renderHavingClause :: HavingClause params tables grouping -> ByteString
436−renderHavingClause = \case
439− Having conditions ->
440− " HAVING" <+> commaSeparated (renderExpression <$> conditions)
442−class (KnownSymbol table, KnownSymbol column)
443− => GroupedBy table column bys where
445− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
447− -> Expression params tables ('Grouped bys) ty
448− getGroup1 column = UnsafeExpression $ renderAlias column
450− :: (HasTable table tables columns, HasColumn column columns ty)
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 #-}
460− , KnownSymbol column
461− , GroupedBy table column bys
462− ) => GroupedBy table column (tabcol ': bys)
465− ( tables ~ '[table ::: columns]
466− , HasColumn column columns ty
467− , GroupedBy table column bys
469− (Expression params tables ('Grouped bys) ty) where
470− fromLabel = getGroup1 (Alias @column)
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
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, ")"]
488− => Expression params tables 'Ungrouped ('Required (nullity ty))
489− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
490−sum_ = unsafeAggregate "sum"
494− => Expression params tables 'Ungrouped ('Required (nullity int))
495− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGnumeric))
496−avgInt = unsafeAggregate "avg"
500− => Expression params tables 'Ungrouped ('Required (nullity float))
501− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGfloat8))
502−avgFloat = unsafeAggregate "avg"
506− => Expression params tables 'Ungrouped ('Required (nullity int))
507− -> Expression params tables ('Grouped bys) ('Required (nullity int))
508−bitAnd = unsafeAggregate "bit_and"
512− => Expression params tables 'Ungrouped ('Required (nullity int))
513− -> Expression params tables ('Grouped bys) ('Required (nullity int))
514−bitOr = unsafeAggregate "bit_or"
517− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
518− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
519−boolAnd = unsafeAggregate "bool_and"
522− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
523− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
524−boolOr = unsafeAggregate "bool_or"
527− :: Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
528−countStar = UnsafeExpression "count(*)"
531− :: Expression params tables 'Ungrouped ('Required ty)
532− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGint8))
533−count = unsafeAggregate "count"
536− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
537− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
538−every = unsafeAggregate "every"
541− :: Expression params tables 'Ungrouped ('Required ty)
542− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
543−jsonAgg = unsafeAggregate "json_agg"
546− :: Expression params tables 'Ungrouped ('Required ty)
547− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
548−jsonbAgg = unsafeAggregate "jsonb_agg"
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 <> ")"
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 <> ")"
567− :: Expression params tables 'Ungrouped ('Required (nullity ty))
568− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
569−max_ = unsafeAggregate "max"
572− :: Expression params tables 'Ungrouped ('Required (nullity ty))
573− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
574−min_ = unsafeAggregate "min"
576−{-----------------------------------------
578−-----------------------------------------}
581− (schema :: [(Symbol,[(Symbol,ColumnType)])])
582− (columns :: [(Symbol,ColumnType)])
583− = UnsafeTable { renderTable :: ByteString }
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
596−instance HasTable table schema columns
597− => IsLabel table (Table schema columns) where
598− fromLabel = getTable (Alias @table)
600−data TableReference params schema tables where
602− :: Aliased (Table schema) table
603− -> TableReference params schema '[table]
605− :: Aliased (Query schema params) table
606− -> TableReference params schema '[table]
608− :: TableReference params schema right
609− -> TableReference params schema left
610− -> TableReference params schema (Join left right)
612− :: TableReference params schema right
613− -> Condition params (Join left right) 'Ungrouped
614− -> TableReference params schema left
615− -> TableReference params schema (Join left right)
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))
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)
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))
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
644− renderJoin op right on left =
645− renderTableReference left <+> op <+> renderTableReference right
646− <+> "ON" <+> renderExpression on
649− (params :: [ColumnType])
650− (schema :: [(Symbol,[(Symbol,ColumnType)])])
651− (tables :: [(Symbol,[(Symbol,ColumnType)])])
652− (grouping :: Grouping)
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]
663−renderTableExpression
664− :: TableExpression params schema tables grouping
666−renderTableExpression
667− (TableExpression tables whs' grps' hvs' srts' lims' offs') = mconcat
668− [ "FROM ", renderTableReference tables
670− , renderGroupByClause grps'
671− , renderHavingClause hvs'
673− , renderLimits lims'
674− , renderOffsets offs'
677− renderWheres = \case
679− wh:[] -> " WHERE" <+> renderExpression wh
680− wh:whs -> " WHERE" <+> renderExpression (foldr (&&*) wh whs)
684− <+> commaSeparated (renderSortExpression <$> srts)
685− renderLimits = \case
687− lims -> " LIMIT" <+> fromString (show (minimum lims))
688− renderOffsets = \case
690− offs -> " OFFSET" <+> fromString (show (sum offs))
693− :: TableReference params schema tables
694− -> TableExpression params schema tables 'Ungrouped
695−from tables = TableExpression tables [] NoGroups NoHaving [] [] []
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}
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
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) }
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}
733− -> TableExpression params schema tables grouping
734− -> TableExpression params schema tables grouping
735−limit lim tables = tables {limitClause = lim : limitClause tables}
739− -> TableExpression params schema tables grouping
740− -> TableExpression params schema tables grouping
741−offset off tables = tables {offsetClause = off : offsetClause tables}
743−{-----------------------------------------
745−-----------------------------------------}
747−newtype TypeExpression (ty :: ColumnType)
748− = UnsafeTypeExpression { renderTypeExpression :: ByteString }
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"
794− -> TypeExpression ('Required ('Null ('PGchar n)))
795−char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
799− -> TypeExpression ('Required ('Null ('PGchar n)))
800−character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
804− -> TypeExpression ('Required ('Null ('PGvarchar n)))
805−varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
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"
834− :: TypeExpression (optionality ('Null ty))
835− -> TypeExpression (optionality ('NotNull ty))
836−notNull ty = UnsafeTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
838− :: Expression '[] '[] 'Ungrouped ('Required ty)
839− -> TypeExpression ('Required ty)
840− -> TypeExpression ('Optional ty)
841−default_ x ty = UnsafeTypeExpression $
842− renderTypeExpression ty <+> "DEFAULT" <+> renderExpression x
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
867−{-----------------------------------------
869−-----------------------------------------}
872− (schema0 :: [(Symbol,[(Symbol,ColumnType)])])
873− (schema1 :: [(Symbol,[(Symbol,ColumnType)])])
874− = UnsafeDefinition { renderDefinition :: ByteString }
876−instance Category Definition where
877− id = UnsafeDefinition ";"
878− ddl1 . ddl0 = UnsafeDefinition $
879− renderDefinition ddl0 <+> renderDefinition ddl1
882− (schema :: [(Symbol,[(Symbol,ColumnType)])])
883− (params :: [ColumnType])
884− (columns :: [(Symbol,ColumnType)])
885− = UnsafeManipulation { renderManipulation :: ByteString }
888− (schema :: [(Symbol,[(Symbol,ColumnType)])])
889− (params :: [ColumnType])
890− (columns :: [(Symbol,ColumnType)])
891− = UnsafeQuery { renderQuery :: ByteString }
894− :: Query schema params columns
895− -> Manipulation schema params columns
896−query q = UnsafeManipulation $ renderQuery q <> ";"
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)
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)
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)
921− <+> parenthesized (renderQuery q2)
922−q1 `exceptAll` q2 = UnsafeQuery $
923− parenthesized (renderQuery q1)
924− <+> "EXCEPT" <+> "ALL"
925− <+> parenthesized (renderQuery q2)
927−{-----------------------------------------
929−-----------------------------------------}
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 $
938− <+> renderCommaSeparated (renderAliased renderExpression) list
939− <+> renderTableExpression tabs
942− :: (tables ~ '[table ::: columns])
943− => TableExpression params schema tables 'Ungrouped
944− -> Query schema params columns
945−selectStar tabs = UnsafeQuery $ "SELECT" <+> "*" <+> renderTableExpression tabs
948− :: HasTable table tables columns
950− -> TableExpression params schema tables 'Ungrouped
951− -> Query schema params columns
952−selectDotStar table tables = UnsafeQuery $
953− "SELECT" <+> renderAlias table <> ".*" <+> renderTableExpression tables
957− => NP (Aliased (Expression params '[] 'Ungrouped)) columns
958− -> Query schema params columns
959−selectRow row = UnsafeQuery $
960− "SELECT" <+> renderCommaSeparated (renderAliased renderExpression) row
962−{-----------------------------------------
964−-----------------------------------------}
967− :: (KnownSymbol table, SListI columns)
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
975− ( renderCommaSeparated renderColumnDef columns
976− <> renderConstraints constraints )
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
985− _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
988− (schema :: [(Symbol,[(Symbol,ColumnType)])])
989− (columns :: [(Symbol,ColumnType)])
990− = UnsafeTableConstraint { renderTableConstraint :: ByteString }
993− :: Condition '[] '[table ::: columns] 'Ungrouped
994− -> TableConstraint schema columns
995−check condition = UnsafeTableConstraint $
996− "CHECK" <+> parenthesized (renderExpression condition)
999− (columns :: [(Symbol,ColumnType)])
1000− (columnty :: (Symbol,ColumnType))
1003− :: HasColumn column columns ty
1005− -> Column columns (column ::: ty)
1007−renderColumn :: Column columns columnty -> ByteString
1008−renderColumn (Column column) = renderAlias column
1011− :: SListI subcolumns
1012− => NP (Column columns) subcolumns
1013− -> TableConstraint schema columns
1014−unique columns = UnsafeTableConstraint $
1015− "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
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)
1024−type family SameTypes columns0 columns1 :: Constraint where
1025− SameTypes '[] '[] = ()
1026− SameTypes ((column0 ::: ty0) ': columns0) ((column1 ::: ty1) ': columns1)
1027− = (ty0 ~ ty1, SameTypes columns0 columns1)
1029−type family NotNullTypes columns :: Constraint where
1030− NotNullTypes '[] = ()
1031− NotNullTypes ((column ::: (optionality ('NotNull ty))) ': columns)
1032− = NotNullTypes columns
1035− :: ( HasTable reftable schema refcolumns
1036− , SameTypes subcolumns refsubcolumns
1037− , NotNullTypes subcolumns
1038− , SListI subcolumns
1039− , SListI refsubcolumns)
1040− => NP (Column columns) subcolumns
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)
1049−{-----------------------------------------
1051−-----------------------------------------}
1054− :: KnownSymbol table
1056− -> Definition schema (Drop table schema)
1057−dropTable table = UnsafeDefinition $ "DROP TABLE" <+> renderAlias table <> ";"
1059−{-----------------------------------------
1061−-----------------------------------------}
1064− :: HasTable table schema columns0
1066− -> AlterTable columns0 columns1
1067− -> Definition schema (Alter table schema columns1)
1068−alterTable table alteration = UnsafeDefinition $
1070− <+> renderAlias table
1071− <+> renderAlterTable alteration
1075− :: (KnownSymbol table0, KnownSymbol table1)
1078− -> Definition schema (Rename table0 table1 schema)
1079−alterTableRename table0 table1 = UnsafeDefinition $
1080− "ALTER TABLE" <+> renderAlias table0
1081− <+> "RENAME TO" <+> renderAlias table1 <> ";"
1084− (columns0 :: [(Symbol, ColumnType)])
1085− (columns1 :: [(Symbol, ColumnType)]) =
1086− UnsafeAlterTable {renderAlterTable :: ByteString}
1089− :: KnownSymbol 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
1097− :: KnownSymbol 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
1105− :: KnownSymbol column
1107− -> AlterTable columns (Drop column columns)
1108−dropColumn column = UnsafeAlterTable $
1109− "DROP COLUMN" <+> renderAlias column
1112− :: (KnownSymbol column0, KnownSymbol column1)
1115− -> AlterTable columns (Rename column0 column1 columns)
1116−renameColumn column0 column1 = UnsafeAlterTable $
1117− "RENAME COLUMN" <+> renderAlias column0 <+> "TO" <+> renderAlias column1
1120− :: (KnownSymbol column, HasColumn column columns ty0)
1122− -> AlterColumn ty0 ty1
1123− -> AlterTable columns (Alter column columns ty1)
1124−alterColumn column alteration = UnsafeAlterTable $
1125− "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
1127−data AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
1128− UnsafeAlterColumn {renderAlterColumn :: ByteString}
1131− :: Expression '[] '[] 'Ungrouped ('Required ty)
1132− -> AlterColumn ('Required ty) ('Optional ty)
1133−setDefault expression = UnsafeAlterColumn $
1134− "SET DEFAULT" <+> renderExpression expression
1136−dropDefault :: AlterColumn ('Optional ty) ('Required ty)
1137−dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
1140− :: AlterColumn (optionality ('Null ty)) (optionality ('NotNull ty))
1141−setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
1144− :: AlterColumn (optionality ('NotNull ty)) (optionality ('Null ty))
1145−dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
1148− :: TypeExpression (optionality (nullity ty1))
1149− -> AlterColumn (optionality (nullity ty0)) (optionality (nullity ty1))
1150−alterType ty = UnsafeAlterColumn $ "TYPE " <> renderTypeExpression ty
1152−{-----------------------------------------
1154−-----------------------------------------}
1157− :: (SListI columns, SListI results, HasTable table schema columns)
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
1170− (schema :: [(Symbol,[(Symbol,ColumnType)])])
1171− (params :: [ColumnType])
1172− (columns :: [(Symbol,ColumnType)])
1174− (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1175− [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
1176− | ValuesQuery (Query schema params columns)
1180− => ValuesClause schema params columns
1182−renderValuesClause = \case
1184− parenthesized (renderCommaSeparated renderAliasPart row)
1187− (parenthesized . renderCommaSeparated renderValuePart <$> row:rows)
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
1195−data ReturningClause
1196− (params :: [ColumnType])
1197− (columns :: [(Symbol,ColumnType)])
1198− (results :: [(Symbol,ColumnType)])
1200− ReturningStar :: ReturningClause params columns columns
1202− :: NP (Aliased (Expression params '[table ::: columns] 'Ungrouped))
1204− -> ReturningClause params columns results
1206−renderReturningClause
1208− => ReturningClause params columns results
1210−renderReturningClause = \case
1211− ReturningStar -> " RETURNING *;"
1212− Returning Nil -> ";"
1213− Returning results -> " RETURNING"
1214− <+> renderCommaSeparated (renderAliased renderExpression) results <> ";"
1216−data ConflictClause params columns where
1217− Conflict :: ConflictClause params columns
1218− OnConflictDoNothing :: ConflictClause params columns
1220− :: NP (Aliased (UpdateExpression params '[table ::: columns])) columns
1221− -> Condition params '[table ::: columns] 'Ungrouped
1222− -> ConflictClause params columns
1224−renderConflictClause
1226− => ConflictClause params columns
1228−renderConflictClause = \case
1230− OnConflictDoNothing -> " ON CONFLICT DO NOTHING"
1231− OnConflictDoUpdate updates wh
1232− -> " ON CONFLICT DO UPDATE SET"
1233− <+> renderCommaSeparatedMaybe renderUpdateExpression updates
1234− <+> "WHERE" <+> renderExpression wh
1236−{-----------------------------------------
1238−-----------------------------------------}
1240−data UpdateExpression params table ty
1242− | Set (Expression params table 'Ungrouped ty)
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
1253− :: (HasTable table schema columns, SListI columns, SListI results)
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 $
1261− <+> renderAlias table
1263− <+> renderCommaSeparatedMaybe renderUpdateExpression columns
1264− <+> "WHERE" <+> renderExpression wh
1265− <> renderReturningClause returning
1267−{-----------------------------------------
1269−-----------------------------------------}
1272− :: HasTable table schema columns
1274− -> Condition params '[table ::: columns] 'Ungrouped
1275− -> Manipulation schema params '[]
1276−deleteFrom table wh = UnsafeManipulation $
1277− "DELETE FROM" <+> renderAlias table
1278− <+> "WHERE" <+> renderExpression wh <> ";"
1280−{-----------------------------------------
1281−render helper functions
1282−-----------------------------------------}
1284−parenthesized :: ByteString -> ByteString
1285−parenthesized str = "(" <> str <> ")"
1287−(<+>) :: ByteString -> ByteString -> ByteString
1288−str1 <+> str2 = str1 <> " " <> str2
1290−commaSeparated :: [ByteString] -> ByteString
1291−commaSeparated = ByteString.intercalate ", "
1293−renderCommaSeparated
1295− => (forall x. expression x -> ByteString)
1296− -> NP expression xs -> ByteString
1297−renderCommaSeparated render
1300− . hmap (K . render)
1302−renderCommaSeparatedMaybe
1304− => (forall x. expression x -> Maybe ByteString)
1305− -> NP expression xs -> ByteString
1306−renderCommaSeparatedMaybe render
1310− . hmap (K . render)
1312−renderNat :: KnownNat n => proxy n -> ByteString
1313−renderNat (_ :: proxy n) = fromString (show (natVal' (proxy# :: Proxy# n)))