@@ -1,1666 +0,0 @@
1−{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
8− , FunctionalDependencies
10− , GeneralizedNewtypeDeriving
16− , ScopedTypeVariables
22− , UndecidableInstances
25−module Squeal.PostgreSQL.Statement
26− ( -- * Column Expressions
27− Expression (UnsafeExpression, renderExpression)
28− , HasParameter (param)
29− , HasColumn (getColumn)
80− , unsafeAggregateDistinct
83− , PGAvg (avg, avgDistinct)
99− , Table (UnsafeTable, renderTable)
100− , HasTable (getTable)
101− -- ** Froms and Joins
104− -- ** Table Expressions
105− , TableExpression (..)
106− , renderTableExpression
116− , GroupByClause (NoGroups, Group)
117− , renderGroupByClause
118− , HavingClause (NoHaving, Having)
119− , renderHavingClause
120− , GroupedBy (getGroup1, getGroup2)
121− , SortExpression (..)
122− , renderSortExpression
124− , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
152− , timestampWithTimeZone
164− , Query (UnsafeQuery, renderQuery)
168− , selectDistinctStar
170− , selectDistinctDotStar
177− -- ** Data Definition Language
178− , Definition (UnsafeDefinition, renderDefinition)
180− , TableConstraint (UnsafeTableConstraint, renderTableConstraint)
185− , OnDelete (OnDeleteNoAction, OnDeleteRestrict, OnDeleteCascade)
187− , OnUpdate (OnUpdateNoAction, OnUpdateRestrict, OnUpdateCascade)
192− , AlterTable (UnsafeAlterTable, renderAlterTable)
198− , AlterColumn (UnsafeAlterColumn, renderAlterColumn)
204− -- ** Data Manipulation Language
205− , Manipulation (UnsafeManipulation, renderManipulation)
208− , ValuesClause (Values, ValuesQuery)
209− , renderValuesClause
210− , ReturningClause (ReturningStar, Returning)
211− , renderReturningClause
212− , ConflictClause (Conflict, OnConflictDoNothing, OnConflictDoUpdate)
213− , renderConflictClause
214− , UpdateExpression (Same, Set)
215− , renderUpdateExpression
220−import Control.Category
221−import Control.DeepSeq
222−import Data.ByteString (ByteString)
223−import Data.Data (Data)
226−import Data.Monoid hiding (All)
230−import Generics.SOP hiding (from)
232−import GHC.OverloadedLabels
234−import Prelude hiding (id, (.))
236−import qualified GHC.Generics as GHC
237−import qualified Data.ByteString as ByteString
239−import Squeal.PostgreSQL.Schema
241−{-----------------------------------------
243−-----------------------------------------}
245−{- | `Expression`s are used in a variety of contexts,
246−such as in the target list of the `select` command,
247−as new column values in `insertInto` or `update`,
248−or in search `Condition`s in a number of commands.
250−The expression syntax allows the calculation of
251−values from primitive expression using arithmetic, logical,
252−and other operations.
255− (params :: [ColumnType])
256− (tables :: TablesType)
257− (grouping :: Grouping)
259− = UnsafeExpression { renderExpression :: ByteString }
260− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
262−{- | A `HasParameter` constraint is used to indicate a value that is
263−supplied externally to a SQL statement.
264−`Squeal.PostgreSQL.PQ.manipulateParams`,
265−`Squeal.PostgreSQL.PQ.queryParams` and
266−`Squeal.PostgreSQL.PQ.traversePrepared` support specifying data values
267−separately from the SQL command string, in which case `param`s are used to
268−refer to the out-of-line data values.
270−class (PGTyped (BaseType ty), KnownNat n)
271− => HasParameter (n :: Nat) params ty | n params -> ty where
272− param :: proxy n -> Expression params tables grouping ty
273− param _ = UnsafeExpression $ parenthesized $
274− "$" <> renderNat (Proxy @n) <+> "::"
275− <+> renderTypeExpression (pgtype @(BaseType ty))
276−instance {-# OVERLAPPING #-} PGTyped (BaseType ty1)
277− => HasParameter 1 (ty1:tys) ty1
278−instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
279− => HasParameter n (ty' : params) ty
281−{- | A `HasColumn` constraint indicates an unqualified column reference.
282−`getColumn` can only be unambiguous when the `TableExpression` the column
283−references is unique, in which case the column may be referenced using
284−@-XOverloadedLabels@. Otherwise, combined with a `HasTable` constraint, the
285−qualified column reference operator `!` may be used.
287−class KnownSymbol column => HasColumn column columns ty
288− | column columns -> ty where
290− :: HasUnique table tables columns
292− -> Expression params tables 'Ungrouped ty
293− getColumn column = UnsafeExpression $ renderAlias column
294−instance {-# OVERLAPPING #-} KnownSymbol column
295− => HasColumn column ((column ::: optionality ty) ': tys) ('Required ty)
296−instance {-# OVERLAPPABLE #-} (KnownSymbol column, HasColumn column table ty)
297− => HasColumn column (ty' ': table) ty
299−instance (HasColumn column columns ty, HasUnique table tables columns)
300− => IsLabel column (Expression params tables 'Ungrouped ty) where
301− fromLabel = getColumn (Alias @column)
303−instance (HasTable table tables columns, HasColumn column columns ty)
304− => IsTableColumn table column (Expression params tables 'Ungrouped ty) where
305− table ! column = UnsafeExpression $
306− renderAlias table <> "." <> renderAlias column
309− (columns :: ColumnsType)
310− (columnty :: (Symbol,ColumnType))
313− :: HasColumn column columns ty
315− -> Column columns (column ::: ty)
316−deriving instance Show (Column columns columnty)
317−deriving instance Eq (Column columns columnty)
318−deriving instance Ord (Column columns columnty)
320−-- | >>> renderExpression def
322−def :: Expression params '[] 'Ungrouped ('Optional (nullity ty))
323−def = UnsafeExpression "DEFAULT"
325−-- | >>> renderExpression $ unDef false
328− :: Expression params '[] 'Ungrouped ('Required (nullity ty))
329− -> Expression params '[] 'Ungrouped ('Optional (nullity ty))
330−unDef = UnsafeExpression . renderExpression
332−-- | analagous to `Nothing`
334−-- >>> renderExpression $ null_
336−null_ :: Expression params tables grouping (optionality ('Null ty))
337−null_ = UnsafeExpression "NULL"
339−-- | analagous to `Just`
341−-- >>> renderExpression $ unNull true
344− :: Expression params tables grouping (optionality ('NotNull ty))
345− -> Expression params tables grouping (optionality ('Null ty))
346−unNull = UnsafeExpression . renderExpression
348−-- | return the leftmost value which is not NULL
350−-- >>> renderExpression $ coalesce [null_, unNull true] false
351−-- "COALESCE(NULL, TRUE, FALSE)"
353− :: [Expression params tables grouping ('Required ('Null ty))]
354− -> Expression params tables grouping ('Required ('NotNull ty))
355− -> Expression params tables grouping ('Required ('NotNull ty))
356−coalesce nullxs notNullx = UnsafeExpression $
357− "COALESCE" <> parenthesized (commaSeparated
358− ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
360−-- | analagous to `fromMaybe` using @COALESCE@
362−-- >>> renderExpression $ fromNull true null_
363−-- "COALESCE(NULL, TRUE)"
365− :: Expression params tables grouping ('Required ('NotNull ty))
366− -> Expression params tables grouping ('Required ('Null ty))
367− -> Expression params tables grouping ('Required ('NotNull ty))
368−fromNull notNullx nullx = coalesce [nullx] notNullx
370−-- | >>> renderExpression $ null_ & isNull
373− :: Expression params tables grouping ('Required ('Null ty))
374− -> Condition params tables grouping
375−isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
377−-- | >>> renderExpression $ null_ & isn'tNull
378−-- "NULL IS NOT NULL"
380− :: Expression params tables grouping ('Required ('Null ty))
381− -> Condition params tables grouping
382−isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
384−-- | analagous to `maybe` using @IS NULL@
386−-- >>> renderExpression $ matchNull true not null_
387−-- "CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT TRUE) END"
389− :: Expression params tables grouping ('Required nullty)
390− -> ( Expression params tables grouping ('Required ('NotNull ty))
391− -> Expression params tables grouping ('Required nullty) )
392− -> Expression params tables grouping ('Required ('Null ty))
393− -> Expression params tables grouping ('Required nullty)
394−matchNull y f x = ifThenElse (isNull x) y
395− (f (UnsafeExpression (renderExpression y)))
397−-- | left inverse to `fromNull`
399−-- >>> renderExpression $ nullIf false (fromNull false null_)
400−-- "NULL IF (FALSE, COALESCE(NULL, FALSE))"
402− :: Expression params tables grouping ('Required ('NotNull ty))
403− -> Expression params tables grouping ('Required ('NotNull ty))
404− -> Expression params tables grouping ('Required ('Null ty))
405−nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
406− (renderExpression x <> ", " <> renderExpression y)
408−-- | >>> renderExpression $ greatest @'[_] currentTimestamp [param (Proxy @1)]
409−-- "GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))"
411− :: Expression params tables grouping ('Required nullty)
412− -> [Expression params tables grouping ('Required nullty)]
413− -> Expression params table grouping ('Required nullty)
414−greatest x xs = UnsafeExpression $ "GREATEST("
415− <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
417−-- | >>> renderExpression $ least currentTimestamp [null_]
418−-- "LEAST(CURRENT_TIMESTAMP, NULL)"
420− :: Expression params tables grouping ('Required nullty)
421− -> [Expression params tables grouping ('Required nullty)]
422− -> Expression params table grouping ('Required nullty)
423−least x xs = UnsafeExpression $ "LEAST("
424− <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
428− -> Expression params tables grouping ('Required ty0)
429− -> Expression params tables grouping ('Required ty1)
430− -> Expression params tables grouping ('Required ty2)
431−unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
432− renderExpression x <+> op <+> renderExpression y
436− -> Expression params tables grouping ('Required ty0)
437− -> Expression params tables grouping ('Required ty1)
438−unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
439− op <+> renderExpression x
443− -> Expression params tables grouping ('Required xty)
444− -> Expression params tables grouping ('Required yty)
445−unsafeFunction fun x = UnsafeExpression $
446− fun <> parenthesized (renderExpression x)
449− => Num (Expression params tables grouping ('Required (nullity ty))) where
450− (+) = unsafeBinaryOp "+"
451− (-) = unsafeBinaryOp "-"
452− (*) = unsafeBinaryOp "*"
453− abs = unsafeFunction "abs"
454− signum = unsafeFunction "sign"
460−instance (PGNum ty, PGFloating ty) => Fractional
461− (Expression params tables grouping ('Required (nullity ty))) where
462− (/) = unsafeBinaryOp "/"
463− fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
465−instance (PGNum ty, PGFloating ty) => Floating
466− (Expression params tables grouping ('Required (nullity ty))) where
467− pi = UnsafeExpression "pi()"
468− exp = unsafeFunction "exp"
469− log = unsafeFunction "ln"
470− sqrt = unsafeFunction "sqrt"
471− b ** x = UnsafeExpression $
472− "power(" <> renderExpression b <> ", " <> renderExpression x <> ")"
473− logBase b y = log y / log b
474− sin = unsafeFunction "sin"
475− cos = unsafeFunction "cos"
476− tan = unsafeFunction "tan"
477− asin = unsafeFunction "asin"
478− acos = unsafeFunction "acos"
479− atan = unsafeFunction "atan"
480− sinh x = (exp x - exp (-x)) / 2
481− cosh x = (exp x + exp (-x)) / 2
482− tanh x = sinh x / cosh x
483− asinh x = log (x + sqrt (x*x + 1))
484− acosh x = log (x + sqrt (x*x - 1))
485− atanh x = log ((1 + x) / (1 - x)) / 2
489− => Expression params tables grouping ('Required (nullity float))
490− -> Expression params tables grouping ('Required (nullity float))
491− -> Expression params tables grouping ('Required (nullity float))
492−atan2_ y x = UnsafeExpression $
493− "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
496− :: TypeExpression ('Required ('Null ty1))
497− -> Expression params tables grouping ('Required (nullity ty0))
498− -> Expression params tables grouping ('Required (nullity ty1))
499−cast ty x = UnsafeExpression $ parenthesized $
500− renderExpression x <+> "::" <+> renderTypeExpression ty
504− => Expression params tables grouping ('Required (nullity int))
505− -> Expression params tables grouping ('Required (nullity int))
506− -> Expression params tables grouping ('Required (nullity int))
507−div_ = unsafeBinaryOp "/"
511− => Expression params tables grouping ('Required (nullity int))
512− -> Expression params tables grouping ('Required (nullity int))
513− -> Expression params tables grouping ('Required (nullity int))
514−mod_ = unsafeBinaryOp "%"
518− => Expression params tables grouping ('Required (nullity frac))
519− -> Expression params tables grouping ('Required (nullity frac))
520−trunc = unsafeFunction "trunc"
524− => Expression params tables grouping ('Required (nullity frac))
525− -> Expression params tables grouping ('Required (nullity frac))
526−round_ = unsafeFunction "round"
530− => Expression params tables grouping ('Required (nullity frac))
531− -> Expression params tables grouping ('Required (nullity frac))
532−ceiling_ = unsafeFunction "ceiling"
535− (Expression params tables grouping ('Required (nullity 'PGtext))) where
536− fromString str = UnsafeExpression $
537− "E\'" <> fromString (escape =<< str) <> "\'"
550−type Condition params tables grouping =
551− Expression params tables grouping ('Required ('NotNull 'PGbool))
553−true :: Condition params tables grouping
554−true = UnsafeExpression "TRUE"
556−false :: Condition params tables grouping
557−false = UnsafeExpression "FALSE"
560− :: Condition params tables grouping
561− -> Condition params tables grouping
562−not_ = unsafeUnaryOp "NOT"
565− :: Condition params tables grouping
566− -> Condition params tables grouping
567− -> Condition params tables grouping
568−(&&*) = unsafeBinaryOp "AND"
571− :: Condition params tables grouping
572− -> Condition params tables grouping
573− -> Condition params tables grouping
574−(||*) = unsafeBinaryOp "OR"
577− :: [ ( Condition params tables grouping
578− , Expression params tables grouping ('Required ty)
580− -> Expression params tables grouping ('Required ty)
581− -> Expression params tables grouping ('Required ty)
582−caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
586− [ " WHEN ", renderExpression when_
587− , " THEN ", renderExpression then_
589− | (when_,then_) <- whenThens
591− , " ELSE ", renderExpression else_
596− :: Condition params tables grouping
597− -> Expression params tables grouping ('Required ty)
598− -> Expression params tables grouping ('Required ty)
599− -> Expression params tables grouping ('Required ty)
600−ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
603− :: Expression params tables grouping ('Required (nullity ty))
604− -> Expression params tables grouping ('Required (nullity ty))
605− -> Expression params tables grouping ('Required (nullity 'PGbool))
606−(==*) = unsafeBinaryOp "="
610− :: Expression params tables grouping ('Required (nullity ty))
611− -> Expression params tables grouping ('Required (nullity ty))
612− -> Expression params tables grouping ('Required (nullity 'PGbool))
613−(/=*) = unsafeBinaryOp "<>"
617− :: Expression params tables grouping ('Required (nullity ty))
618− -> Expression params tables grouping ('Required (nullity ty))
619− -> Expression params tables grouping ('Required (nullity 'PGbool))
620−(>=*) = unsafeBinaryOp ">="
624− :: Expression params tables grouping ('Required (nullity ty))
625− -> Expression params tables grouping ('Required (nullity ty))
626− -> Expression params tables grouping ('Required (nullity 'PGbool))
627−(*<) = unsafeBinaryOp "<"
631− :: Expression params tables grouping ('Required (nullity ty))
632− -> Expression params tables grouping ('Required (nullity ty))
633− -> Expression params tables grouping ('Required (nullity 'PGbool))
634−(*<=) = unsafeBinaryOp "<="
638− :: Expression params tables grouping ('Required (nullity ty))
639− -> Expression params tables grouping ('Required (nullity ty))
640− -> Expression params tables grouping ('Required (nullity 'PGbool))
641−(>*) = unsafeBinaryOp ">"
645− :: Expression params tables grouping ('Required (nullity 'PGdate))
646−currentDate = UnsafeExpression "CURRENT_DATE"
649− :: Expression params tables grouping ('Required (nullity 'PGtimetz))
650−currentTime = UnsafeExpression "CURRENT_TIME"
653− :: Expression params tables grouping ('Required (nullity 'PGtimestamptz))
654−currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
657− :: Expression params tables grouping ('Required (nullity 'PGtime))
658−localTime = UnsafeExpression "LOCALTIME"
661− :: Expression params tables grouping ('Required (nullity 'PGtimestamp))
662−localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
664−data SortExpression params tables grouping where
666− :: Expression params tables grouping ('Required ('NotNull ty))
667− -> SortExpression params tables grouping
669− :: Expression params tables grouping ('Required ('NotNull ty))
670− -> SortExpression params tables grouping
672− :: Expression params tables grouping ('Required ('Null ty))
673− -> SortExpression params tables grouping
675− :: Expression params tables grouping ('Required ('Null ty))
676− -> SortExpression params tables grouping
678− :: Expression params tables grouping ('Required ('Null ty))
679− -> SortExpression params tables grouping
681− :: Expression params tables grouping ('Required ('Null ty))
682− -> SortExpression params tables grouping
683−deriving instance Show (SortExpression params tables grouping)
685−renderSortExpression :: SortExpression params tables grouping -> ByteString
686−renderSortExpression = \case
687− Asc expression -> renderExpression expression <+> "ASC"
688− Desc expression -> renderExpression expression <+> "DESC"
689− AscNullsFirst expression -> renderExpression expression
690− <+> "ASC NULLS FIRST"
691− DescNullsFirst expression -> renderExpression expression
692− <+> "DESC NULLS FIRST"
693− AscNullsLast expression -> renderExpression expression <+> "ASC NULLS LAST"
694− DescNullsLast expression -> renderExpression expression <+> "DESC NULLS LAST"
697−{-----------------------------------------
699−-----------------------------------------}
702− (tables :: TablesType)
703− (by :: (Symbol,Symbol)) where
705− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
707− -> By tables '(table, column)
709− :: (HasTable table tables columns, HasColumn column columns ty)
710− => (Alias table, Alias column)
711− -> By tables '(table, column)
712−deriving instance Show (By tables by)
713−deriving instance Eq (By tables by)
714−deriving instance Ord (By tables by)
716−renderBy :: By tables tabcolty -> ByteString
718− By column -> renderAlias column
719− By2 (table, column) -> renderAlias table <> "." <> renderAlias column
721−data GroupByClause tables grouping where
722− NoGroups :: GroupByClause tables 'Ungrouped
725− => NP (By tables) bys
726− -> GroupByClause tables ('Grouped bys)
728−renderGroupByClause :: GroupByClause tables grouping -> ByteString
729−renderGroupByClause = \case
732− Group bys -> " GROUP BY" <+> renderCommaSeparated renderBy bys
734−data HavingClause params tables grouping where
735− NoHaving :: HavingClause params tables 'Ungrouped
737− :: [Condition params tables ('Grouped bys)]
738− -> HavingClause params tables ('Grouped bys)
739−deriving instance Show (HavingClause params tables grouping)
740−deriving instance Eq (HavingClause params tables grouping)
741−deriving instance Ord (HavingClause params tables grouping)
743−renderHavingClause :: HavingClause params tables grouping -> ByteString
744−renderHavingClause = \case
747− Having conditions ->
748− " HAVING" <+> commaSeparated (renderExpression <$> conditions)
750−class (KnownSymbol table, KnownSymbol column)
751− => GroupedBy table column bys where
753− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
755− -> Expression params tables ('Grouped bys) ty
756− getGroup1 column = UnsafeExpression $ renderAlias column
758− :: (HasTable table tables columns, HasColumn column columns ty)
761− -> Expression params tables ('Grouped bys) ty
762− getGroup2 table column = UnsafeExpression $
763− renderAlias table <> "." <> renderAlias column
764−instance {-# OVERLAPPING #-} (KnownSymbol table, KnownSymbol column)
765− => GroupedBy table column ('(table,column) ': bys)
766−instance {-# OVERLAPPABLE #-}
768− , KnownSymbol column
769− , GroupedBy table column bys
770− ) => GroupedBy table column (tabcol ': bys)
773− ( HasUnique table tables columns
774− , HasColumn column columns ty
775− , GroupedBy table column bys
777− (Expression params tables ('Grouped bys) ty) where
778− fromLabel = getGroup1 (Alias @column)
781− ( HasTable table tables columns
782− , HasColumn column columns ty
783− , GroupedBy table column bys
784− ) => IsTableColumn table column
785− (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
787−-- data AllOrDistinct = All | Distinct
788−-- deriving (GHC.Generic,Show,Eq,Ord,Data)
789−-- instance NFData AllOrDistinct
791−-- renderAllOrDistinct :: AllOrDistinct -> ByteString
792−-- renderAllOrDistinct = \case
794−-- Distinct -> "DISTINCT"
796−unsafeAggregate, unsafeAggregateDistinct
798− -> Expression params tables 'Ungrouped ('Required xty)
799− -> Expression params tables ('Grouped bys) ('Required yty)
800−unsafeAggregate fun x = UnsafeExpression $ mconcat
801− [fun, "(", renderExpression x, ")"]
802−unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
803− [fun, "(DISTINCT ", renderExpression x, ")"]
807− => Expression params tables 'Ungrouped ('Required (nullity ty))
808− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
809−sum_ = unsafeAggregate "sum"
810−sumDistinct = unsafeAggregateDistinct "sum"
812−class PGAvg ty avg | ty -> avg where
814− :: Expression params tables 'Ungrouped ('Required (nullity ty))
815− -> Expression params tables ('Grouped bys) ('Required (nullity avg))
816− avg = unsafeAggregate "avg"
817− avgDistinct = unsafeAggregateDistinct "avg"
818−instance PGAvg 'PGint2 'PGnumeric
819−instance PGAvg 'PGint4 'PGnumeric
820−instance PGAvg 'PGint8 'PGnumeric
821−instance PGAvg 'PGnumeric 'PGnumeric
822−instance PGAvg 'PGfloat4 'PGfloat8
823−instance PGAvg 'PGfloat8 'PGfloat8
824−instance PGAvg 'PGinterval 'PGinterval
828− => Expression params tables 'Ungrouped ('Required (nullity int))
829− -> Expression params tables ('Grouped bys) ('Required (nullity int))
830−bitAnd = unsafeAggregate "bit_and"
831−bitOr = unsafeAggregate "bit_or"
834− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
835− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
836−boolAnd = unsafeAggregate "bool_and"
837−boolOr = unsafeAggregate "bool_or"
839−countStar, countDistinctStar
840− :: Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
841−countStar = UnsafeExpression $ "count(*)"
842−countDistinctStar = UnsafeExpression $ "count(DISTINCT *)"
845− :: Expression params tables 'Ungrouped ('Required ty)
846− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGint8))
847−count = unsafeAggregate "count"
850− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
851− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
852−every = unsafeAggregate "every"
855− :: Expression params tables 'Ungrouped ('Required ty)
856− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
857−jsonAgg = unsafeAggregate "json_agg"
860− :: Expression params tables 'Ungrouped ('Required ty)
861− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
862−jsonbAgg = unsafeAggregate "jsonb_agg"
865− :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
866− -> Expression params tables 'Ungrouped ('Required valty)
867− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
868−jsonObjectAgg k v = UnsafeExpression $
869− "json_object_agg(" <> renderExpression k
870− <> ", " <> renderExpression v <> ")"
873− :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
874− -> Expression params tables 'Ungrouped ('Required valty)
875− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
876−jsonbObjectAgg k v = UnsafeExpression $
878− <> renderExpression k <> ", " <> renderExpression v <> ")"
881− :: Expression params tables 'Ungrouped ('Required (nullity ty))
882− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
883−max_ = unsafeAggregate "max"
884−min_ = unsafeAggregate "min"
886−{-----------------------------------------
888−-----------------------------------------}
891− (schema :: TablesType)
892− (columns :: ColumnsType)
893− = UnsafeTable { renderTable :: ByteString }
894− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
896−class KnownSymbol table => HasTable table tables columns
897− | table tables -> columns where
898− getTable :: Alias table -> Table tables columns
899− getTable table = UnsafeTable $ renderAlias table
900−instance {-# OVERLAPPING #-} KnownSymbol table
901− => HasTable table ((table ::: columns) ': tables) columns
902−instance {-# OVERLAPPABLE #-}
903− (KnownSymbol table, HasTable table schema columns)
904− => HasTable table (table' ': schema) columns
906−instance HasTable table schema columns
907− => IsLabel table (Table schema columns) where
908− fromLabel = getTable (Alias @table)
910−data FromClause params schema tables where
912− :: Aliased (Table schema) table
913− -> FromClause params schema '[table]
915− :: Aliased (Query schema params) table
916− -> FromClause params schema '[table]
918− :: FromClause params schema right
919− -> FromClause params schema left
920− -> FromClause params schema (Join left right)
922− :: FromClause params schema right
923− -> Condition params (Join left right) 'Ungrouped
924− -> FromClause params schema left
925− -> FromClause params schema (Join left right)
927− :: FromClause params schema right
928− -> Condition params (Join left right) 'Ungrouped
929− -> FromClause params schema left
930− -> FromClause params schema (Join left (NullifyTables right))
932− :: FromClause params schema right
933− -> Condition params (Join left right) 'Ungrouped
934− -> FromClause params schema left
935− -> FromClause params schema (Join (NullifyTables left) right)
937− :: FromClause params schema right
938− -> Condition params (Join left right) 'Ungrouped
939− -> FromClause params schema left
940− -> FromClause params schema
941− (Join (NullifyTables left) (NullifyTables right))
943−renderFromClause :: FromClause params schema tables -> ByteString
944−renderFromClause = \case
945− Table table -> renderAliased renderTable table
946− Subquery selection -> renderAliased (parenthesized . renderQuery) selection
947− CrossJoin right left ->
948− renderFromClause left <+> "CROSS JOIN" <+> renderFromClause right
949− InnerJoin right on left -> renderJoin "INNER JOIN" right on left
950− LeftOuterJoin right on left -> renderJoin "LEFT OUTER JOIN" right on left
951− RightOuterJoin right on left -> renderJoin "RIGHT OUTER JOIN" right on left
952− FullOuterJoin right on left -> renderJoin "FULL OUTER JOIN" right on left
954− renderJoin op right on left =
955− renderFromClause left <+> op <+> renderFromClause right
956− <+> "ON" <+> renderExpression on
959− (params :: [ColumnType])
960− (schema :: TablesType)
961− (tables :: TablesType)
962− (grouping :: Grouping)
964− { fromClause :: FromClause params schema tables
965− , whereClause :: [Condition params tables 'Ungrouped]
966− , groupByClause :: GroupByClause tables grouping
967− , havingClause :: HavingClause params tables grouping
968− , orderByClause :: [SortExpression params tables grouping]
969− , limitClause :: [Word64]
970− , offsetClause :: [Word64]
973−renderTableExpression
974− :: TableExpression params schema tables grouping
976−renderTableExpression
977− (TableExpression tables whs' grps' hvs' srts' lims' offs') = mconcat
978− [ "FROM ", renderFromClause tables
980− , renderGroupByClause grps'
981− , renderHavingClause hvs'
983− , renderLimits lims'
984− , renderOffsets offs'
987− renderWheres = \case
989− wh:[] -> " WHERE" <+> renderExpression wh
990− wh:whs -> " WHERE" <+> renderExpression (foldr (&&*) wh whs)
994− <+> commaSeparated (renderSortExpression <$> srts)
995− renderLimits = \case
997− lims -> " LIMIT" <+> fromString (show (minimum lims))
998− renderOffsets = \case
1000− offs -> " OFFSET" <+> fromString (show (sum offs))
1003− :: FromClause params schema tables
1004− -> TableExpression params schema tables 'Ungrouped
1005−from tables = TableExpression tables [] NoGroups NoHaving [] [] []
1008− :: Condition params tables 'Ungrouped
1009− -> TableExpression params schema tables grouping
1010− -> TableExpression params schema tables grouping
1011−where_ wh tables = tables {whereClause = wh : whereClause tables}
1015− => NP (By tables) bys
1016− -> TableExpression params schema tables 'Ungrouped
1017− -> TableExpression params schema tables ('Grouped bys)
1018−group bys tables = TableExpression
1019− { fromClause = fromClause tables
1020− , whereClause = whereClause tables
1021− , groupByClause = Group bys
1022− , havingClause = Having []
1023− , orderByClause = []
1024− , limitClause = limitClause tables
1025− , offsetClause = offsetClause tables
1029− :: Condition params tables ('Grouped bys)
1030− -> TableExpression params schema tables ('Grouped bys)
1031− -> TableExpression params schema tables ('Grouped bys)
1032−having hv tables = tables
1033− { havingClause = case havingClause tables of Having hvs -> Having (hv:hvs) }
1036− :: [SortExpression params tables grouping]
1037− -> TableExpression params schema tables grouping
1038− -> TableExpression params schema tables grouping
1039−orderBy srts tables = tables {orderByClause = orderByClause tables ++ srts}
1043− -> TableExpression params schema tables grouping
1044− -> TableExpression params schema tables grouping
1045−limit lim tables = tables {limitClause = lim : limitClause tables}
1049− -> TableExpression params schema tables grouping
1050− -> TableExpression params schema tables grouping
1051−offset off tables = tables {offsetClause = off : offsetClause tables}
1053−{-----------------------------------------
1055−-----------------------------------------}
1057−newtype TypeExpression (ty :: ColumnType)
1058− = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1059− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1061−bool :: TypeExpression ('Required ('Null 'PGbool))
1062−bool = UnsafeTypeExpression "bool"
1063−int2 :: TypeExpression ('Required ('Null 'PGint2))
1064−int2 = UnsafeTypeExpression "int2"
1065−smallint :: TypeExpression ('Required ('Null 'PGint2))
1066−smallint = UnsafeTypeExpression "smallint"
1067−int4 :: TypeExpression ('Required ('Null 'PGint4))
1068−int4 = UnsafeTypeExpression "int4"
1069−int :: TypeExpression ('Required ('Null 'PGint4))
1070−int = UnsafeTypeExpression "int"
1071−integer :: TypeExpression ('Required ('Null 'PGint4))
1072−integer = UnsafeTypeExpression "integer"
1073−int8 :: TypeExpression ('Required ('Null 'PGint8))
1074−int8 = UnsafeTypeExpression "int8"
1075−bigint :: TypeExpression ('Required ('Null 'PGint8))
1076−bigint = UnsafeTypeExpression "bigint"
1077−numeric :: TypeExpression ('Required ('Null 'PGnumeric))
1078−numeric = UnsafeTypeExpression "numeric"
1079−float4 :: TypeExpression ('Required ('Null 'PGfloat4))
1080−float4 = UnsafeTypeExpression "float4"
1081−real :: TypeExpression ('Required ('Null 'PGfloat4))
1082−real = UnsafeTypeExpression "real"
1083−float8 :: TypeExpression ('Required ('Null 'PGfloat8))
1084−float8 = UnsafeTypeExpression "float8"
1085−doublePrecision :: TypeExpression ('Required ('Null 'PGfloat8))
1086−doublePrecision = UnsafeTypeExpression "double precision"
1087−serial2 :: TypeExpression ('Optional ('NotNull 'PGint2))
1088−serial2 = UnsafeTypeExpression "serial2"
1089−smallserial :: TypeExpression ('Optional ('NotNull 'PGint2))
1090−smallserial = UnsafeTypeExpression "smallserial"
1091−serial4 :: TypeExpression ('Optional ('NotNull 'PGint4))
1092−serial4 = UnsafeTypeExpression "serial4"
1093−serial :: TypeExpression ('Optional ('NotNull 'PGint4))
1094−serial = UnsafeTypeExpression "serial"
1095−serial8 :: TypeExpression ('Optional ('NotNull 'PGint8))
1096−serial8 = UnsafeTypeExpression "serial8"
1097−bigserial :: TypeExpression ('Optional ('NotNull 'PGint8))
1098−bigserial = UnsafeTypeExpression "bigserial"
1099−text :: TypeExpression ('Required ('Null 'PGtext))
1100−text = UnsafeTypeExpression "text"
1104− -> TypeExpression ('Required ('Null ('PGchar n)))
1105−char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
1109− -> TypeExpression ('Required ('Null ('PGchar n)))
1110−character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
1114− -> TypeExpression ('Required ('Null ('PGvarchar n)))
1115−varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
1119− -> TypeExpression ('Required ('Null ('PGvarchar n)))
1120−characterVarying p = UnsafeTypeExpression $
1121− "character varying(" <> renderNat p <> ")"
1122−bytea :: TypeExpression ('Required ('Null ('PGbytea)))
1123−bytea = UnsafeTypeExpression "bytea"
1124−timestamp :: TypeExpression ('Required ('Null ('PGtimestamp)))
1125−timestamp = UnsafeTypeExpression "timestamp"
1126−timestampWithTimeZone :: TypeExpression ('Required ('Null ('PGtimestamptz)))
1127−timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
1128−date :: TypeExpression ('Required ('Null ('PGdate)))
1129−date = UnsafeTypeExpression "date"
1130−time :: TypeExpression ('Required ('Null ('PGtime)))
1131−time = UnsafeTypeExpression "time"
1132−timeWithTimeZone :: TypeExpression ('Required ('Null ('PGtimetz)))
1133−timeWithTimeZone = UnsafeTypeExpression "time with time zone"
1134−interval :: TypeExpression ('Required ('Null ('PGinterval)))
1135−interval = UnsafeTypeExpression "interval"
1136−uuid :: TypeExpression ('Required ('Null ('PGuuid)))
1137−uuid = UnsafeTypeExpression "uuid"
1138−json :: TypeExpression ('Required ('Null ('PGjson)))
1139−json = UnsafeTypeExpression "json"
1140−jsonb :: TypeExpression ('Required ('Null ('PGjsonb)))
1141−jsonb = UnsafeTypeExpression "jsonb"
1144− :: TypeExpression (optionality ('Null ty))
1145− -> TypeExpression (optionality ('NotNull ty))
1146−notNull ty = UnsafeTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
1148− :: Expression '[] '[] 'Ungrouped ('Required ty)
1149− -> TypeExpression ('Required ty)
1150− -> TypeExpression ('Optional ty)
1151−default_ x ty = UnsafeTypeExpression $
1152− renderTypeExpression ty <+> "DEFAULT" <+> renderExpression x
1154−class PGTyped (ty :: PGType) where
1155− pgtype :: TypeExpression ('Required ('Null ty))
1156−instance PGTyped 'PGbool where pgtype = bool
1157−instance PGTyped 'PGint2 where pgtype = int2
1158−instance PGTyped 'PGint4 where pgtype = int4
1159−instance PGTyped 'PGint8 where pgtype = int8
1160−instance PGTyped 'PGnumeric where pgtype = numeric
1161−instance PGTyped 'PGfloat4 where pgtype = float4
1162−instance PGTyped 'PGfloat8 where pgtype = float8
1163−instance PGTyped 'PGtext where pgtype = text
1164−instance KnownNat n => PGTyped ('PGchar n) where pgtype = char (Proxy @n)
1165−instance KnownNat n => PGTyped ('PGvarchar n) where pgtype = varchar (Proxy @n)
1166−instance PGTyped 'PGbytea where pgtype = bytea
1167−instance PGTyped 'PGtimestamp where pgtype = timestamp
1168−instance PGTyped 'PGtimestamptz where pgtype = timestampWithTimeZone
1169−instance PGTyped 'PGdate where pgtype = date
1170−instance PGTyped 'PGtime where pgtype = time
1171−instance PGTyped 'PGtimetz where pgtype = timeWithTimeZone
1172−instance PGTyped 'PGinterval where pgtype = interval
1173−instance PGTyped 'PGuuid where pgtype = uuid
1174−instance PGTyped 'PGjson where pgtype = json
1175−instance PGTyped 'PGjsonb where pgtype = jsonb
1177−{-----------------------------------------
1179−-----------------------------------------}
1182− (schema0 :: TablesType)
1183− (schema1 :: TablesType)
1184− = UnsafeDefinition { renderDefinition :: ByteString }
1185− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1187−instance Category Definition where
1188− id = UnsafeDefinition ";"
1189− ddl1 . ddl0 = UnsafeDefinition $
1190− renderDefinition ddl0 <+> renderDefinition ddl1
1192−newtype Manipulation
1193− (schema :: TablesType)
1194− (params :: [ColumnType])
1195− (columns :: ColumnsType)
1196− = UnsafeManipulation { renderManipulation :: ByteString }
1197− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1200− (schema :: TablesType)
1201− (params :: [ColumnType])
1202− (columns :: ColumnsType)
1203− = UnsafeQuery { renderQuery :: ByteString }
1204− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1207− :: Query schema params columns
1208− -> Manipulation schema params columns
1209−queryStatement q = UnsafeManipulation $ renderQuery q <> ";"
1211−union, unionAll, intersect, intersectAll, except, exceptAll
1212− :: Query schema params columns
1213− -> Query schema params columns
1214− -> Query schema params columns
1215−q1 `union` q2 = UnsafeQuery $
1216− parenthesized (renderQuery q1)
1218− <+> parenthesized (renderQuery q2)
1219−q1 `unionAll` q2 = UnsafeQuery $
1220− parenthesized (renderQuery q1)
1221− <+> "UNION" <+> "ALL"
1222− <+> parenthesized (renderQuery q2)
1223−q1 `intersect` q2 = UnsafeQuery $
1224− parenthesized (renderQuery q1)
1226− <+> parenthesized (renderQuery q2)
1227−q1 `intersectAll` q2 = UnsafeQuery $
1228− parenthesized (renderQuery q1)
1229− <+> "INTERSECT" <+> "ALL"
1230− <+> parenthesized (renderQuery q2)
1231−q1 `except` q2 = UnsafeQuery $
1232− parenthesized (renderQuery q1)
1234− <+> parenthesized (renderQuery q2)
1235−q1 `exceptAll` q2 = UnsafeQuery $
1236− parenthesized (renderQuery q1)
1237− <+> "EXCEPT" <+> "ALL"
1238− <+> parenthesized (renderQuery q2)
1240−{-----------------------------------------
1242−-----------------------------------------}
1246− => NP (Aliased (Expression params tables grouping)) (column ': columns)
1247− -> TableExpression params schema tables grouping
1248− -> Query schema params (column ': columns)
1249−select list tabs = UnsafeQuery $
1251− <+> renderCommaSeparated (renderAliased renderExpression) list
1252− <+> renderTableExpression tabs
1256− => NP (Aliased (Expression params tables grouping)) (column ': columns)
1257− -> TableExpression params schema tables grouping
1258− -> Query schema params (column ': columns)
1259−selectDistinct list tabs = UnsafeQuery $
1261− <+> renderCommaSeparated (renderAliased renderExpression) list
1262− <+> renderTableExpression tabs
1265− :: tables ~ '[table ::: columns]
1266− => TableExpression params schema tables 'Ungrouped
1267− -> Query schema params columns
1268−selectStar tabs = UnsafeQuery $ "SELECT" <+> "*" <+> renderTableExpression tabs
1271− :: tables ~ '[table ::: columns]
1272− => TableExpression params schema tables 'Ungrouped
1273− -> Query schema params columns
1274−selectDistinctStar tabs = UnsafeQuery $
1275− "SELECT DISTINCT" <+> "*" <+> renderTableExpression tabs
1278− :: HasTable table tables columns
1280− -> TableExpression params schema tables 'Ungrouped
1281− -> Query schema params columns
1282−selectDotStar table tables = UnsafeQuery $
1283− "SELECT" <+> renderAlias table <> ".*" <+> renderTableExpression tables
1285−selectDistinctDotStar
1286− :: HasTable table tables columns
1288− -> TableExpression params schema tables 'Ungrouped
1289− -> Query schema params columns
1290−selectDistinctDotStar table tables = UnsafeQuery $
1291− "SELECT DISTINCT" <+> renderAlias table <> ".*"
1292− <+> renderTableExpression tables
1294−{-----------------------------------------
1296−-----------------------------------------}
1299− :: (KnownSymbol table, SListI columns)
1301− -> NP (Aliased TypeExpression) (column ': columns)
1302− -> [TableConstraint schema (column ': columns)]
1303− -> Definition schema (Create table (column ': columns) schema)
1304−createTable table columns constraints = UnsafeDefinition $
1305− "CREATE TABLE" <+> renderAlias table
1307− ( renderCommaSeparated renderColumnDef columns
1308− <> renderConstraints constraints )
1311− renderColumnDef :: Aliased TypeExpression x -> ByteString
1312− renderColumnDef (ty `As` column) =
1313− renderAlias column <+> renderTypeExpression ty
1314− renderConstraints :: [TableConstraint schema columns] -> ByteString
1315− renderConstraints = \case
1317− _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
1319−newtype TableConstraint
1320− (schema :: TablesType)
1321− (columns :: ColumnsType)
1322− = UnsafeTableConstraint { renderTableConstraint :: ByteString }
1323− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1326− :: Condition '[] '[table ::: columns] 'Ungrouped
1327− -> TableConstraint schema columns
1328−check condition = UnsafeTableConstraint $
1329− "CHECK" <+> parenthesized (renderExpression condition)
1331−renderColumn :: Column columns columnty -> ByteString
1332−renderColumn (Column column) = renderAlias column
1335− :: SListI subcolumns
1336− => NP (Column columns) subcolumns
1337− -> TableConstraint schema columns
1338−unique columns = UnsafeTableConstraint $
1339− "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
1342− :: (SListI subcolumns, AllNotNull subcolumns)
1343− => NP (Column columns) subcolumns
1344− -> TableConstraint schema columns
1345−primaryKey columns = UnsafeTableConstraint $
1346− "PRIMARY KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1349− :: ( HasTable reftable schema refcolumns
1350− , SameTypes subcolumns refsubcolumns
1351− , NotAllNull subcolumns
1352− , SListI subcolumns
1353− , SListI refsubcolumns)
1354− => NP (Column columns) subcolumns
1356− -> NP (Column refcolumns) refsubcolumns
1359− -> TableConstraint schema columns
1360−foreignKey columns reftable refcolumns onDelete onUpdate =
1361− UnsafeTableConstraint $
1362− "FOREIGN KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1363− <+> "REFERENCES" <+> renderAlias reftable
1364− <+> parenthesized (renderCommaSeparated renderColumn refcolumns)
1365− <+> renderOnDelete onDelete
1366− <+> renderOnUpdate onUpdate
1372− deriving (GHC.Generic,Show,Eq,Ord,Data)
1373−instance NFData OnDelete
1375−renderOnDelete :: OnDelete -> ByteString
1376−renderOnDelete = \case
1377− OnDeleteNoAction -> "ON DELETE NO ACTION"
1378− OnDeleteRestrict -> "ON DELETE RESTRICT"
1379− OnDeleteCascade -> "ON DELETE CASCADE"
1385− deriving (GHC.Generic,Show,Eq,Ord,Data)
1386−instance NFData OnUpdate
1388−renderOnUpdate :: OnUpdate -> ByteString
1389−renderOnUpdate = \case
1390− OnUpdateNoAction -> "ON UPDATE NO ACTION"
1391− OnUpdateRestrict -> "ON UPDATE RESTRICT"
1392− OnUpdateCascade -> "ON UPDATE CASCADE"
1394−{-----------------------------------------
1396−-----------------------------------------}
1399− :: KnownSymbol table
1401− -> Definition schema (Drop table schema)
1402−dropTable table = UnsafeDefinition $ "DROP TABLE" <+> renderAlias table <> ";"
1404−{-----------------------------------------
1406−-----------------------------------------}
1409− :: HasTable table schema columns0
1411− -> AlterTable columns0 columns1
1412− -> Definition schema (Alter table schema columns1)
1413−alterTable table alteration = UnsafeDefinition $
1415− <+> renderAlias table
1416− <+> renderAlterTable alteration
1420− :: (KnownSymbol table0, KnownSymbol table1)
1423− -> Definition schema (Rename table0 table1 schema)
1424−alterTableRename table0 table1 = UnsafeDefinition $
1425− "ALTER TABLE" <+> renderAlias table0
1426− <+> "RENAME TO" <+> renderAlias table1 <> ";"
1429− (columns0 :: [(Symbol, ColumnType)])
1430− (columns1 :: [(Symbol, ColumnType)]) =
1431− UnsafeAlterTable {renderAlterTable :: ByteString}
1432− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1435− :: KnownSymbol column
1437− -> TypeExpression ('Optional ty)
1438− -> AlterTable columns (Create column ('Optional ty) columns)
1439−addColumnDefault column ty = UnsafeAlterTable $
1440− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1443− :: KnownSymbol column
1445− -> TypeExpression ('Required ('Null ty))
1446− -> AlterTable columns (Create column ('Required ('Null ty)) columns)
1447−addColumnNull column ty = UnsafeAlterTable $
1448− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1451− :: KnownSymbol column
1453− -> AlterTable columns (Drop column columns)
1454−dropColumn column = UnsafeAlterTable $
1455− "DROP COLUMN" <+> renderAlias column
1458− :: (KnownSymbol column0, KnownSymbol column1)
1461− -> AlterTable columns (Rename column0 column1 columns)
1462−renameColumn column0 column1 = UnsafeAlterTable $
1463− "RENAME COLUMN" <+> renderAlias column0 <+> "TO" <+> renderAlias column1
1466− :: (KnownSymbol column, HasColumn column columns ty0)
1468− -> AlterColumn ty0 ty1
1469− -> AlterTable columns (Alter column columns ty1)
1470−alterColumn column alteration = UnsafeAlterTable $
1471− "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
1473−newtype AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
1474− UnsafeAlterColumn {renderAlterColumn :: ByteString}
1475− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1478− :: Expression '[] '[] 'Ungrouped ('Required ty)
1479− -> AlterColumn ('Required ty) ('Optional ty)
1480−setDefault expression = UnsafeAlterColumn $
1481− "SET DEFAULT" <+> renderExpression expression
1483−dropDefault :: AlterColumn ('Optional ty) ('Required ty)
1484−dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
1487− :: AlterColumn (optionality ('Null ty)) (optionality ('NotNull ty))
1488−setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
1491− :: AlterColumn (optionality ('NotNull ty)) (optionality ('Null ty))
1492−dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
1495− :: TypeExpression (optionality (nullity ty1))
1496− -> AlterColumn (optionality (nullity ty0)) (optionality (nullity ty1))
1497−alterType ty = UnsafeAlterColumn $ "TYPE " <> renderTypeExpression ty
1499−{-----------------------------------------
1501−-----------------------------------------}
1504− :: (SListI columns, SListI results, HasTable table schema columns)
1506− -> ValuesClause schema params columns
1507− -> ConflictClause params columns
1508− -> ReturningClause params columns results
1509− -> Manipulation schema params results
1510−insertInto table insert conflict returning = UnsafeManipulation $
1511− "INSERT" <+> "INTO" <+> renderAlias table
1512− <+> renderValuesClause insert
1513− <> renderConflictClause conflict
1514− <> renderReturningClause returning
1517− (schema :: TablesType)
1518− (params :: [ColumnType])
1519− (columns :: ColumnsType)
1521− (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1522− [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
1523− | ValuesQuery (Query schema params columns)
1527− => ValuesClause schema params columns
1529−renderValuesClause = \case
1531− parenthesized (renderCommaSeparated renderAliasPart row)
1534− (parenthesized . renderCommaSeparated renderValuePart <$> row:rows)
1536− renderAliasPart, renderValuePart
1537− :: Aliased (Expression params '[] 'Ungrouped) ty -> ByteString
1538− renderAliasPart (_ `As` name) = renderAlias name
1539− renderValuePart (value `As` _) = renderExpression value
1540− ValuesQuery q -> renderQuery q
1542−data ReturningClause
1543− (params :: [ColumnType])
1544− (columns :: ColumnsType)
1545− (results :: ColumnsType)
1547− ReturningStar :: ReturningClause params columns columns
1550− (Aliased (Expression params '[table ::: columns] 'Ungrouped))
1552− -> ReturningClause params columns results
1554−renderReturningClause
1556− => ReturningClause params columns results
1558−renderReturningClause = \case
1559− ReturningStar -> " RETURNING *;"
1560− Returning Nil -> ";"
1561− Returning results -> " RETURNING"
1562− <+> renderCommaSeparated (renderAliased renderExpression) results <> ";"
1564−data ConflictClause params columns where
1565− Conflict :: ConflictClause params columns
1566− OnConflictDoNothing :: ConflictClause params columns
1568− :: NP (Aliased (UpdateExpression params columns)) columns
1569− -> Maybe (Condition params '[table ::: columns] 'Ungrouped)
1570− -> ConflictClause params columns
1572−renderConflictClause
1574− => ConflictClause params columns
1576−renderConflictClause = \case
1578− OnConflictDoNothing -> " ON CONFLICT DO NOTHING"
1579− OnConflictDoUpdate updates whMaybe
1580− -> " ON CONFLICT DO UPDATE SET"
1581− <+> renderCommaSeparatedMaybe renderUpdateExpression updates
1584− Just wh -> " WHERE" <+> renderExpression wh
1586−{-----------------------------------------
1588−-----------------------------------------}
1590−data UpdateExpression params columns ty
1592− | Set (forall table. Expression params '[table ::: columns] 'Ungrouped ty)
1593−deriving instance Show (UpdateExpression params columns ty)
1594−deriving instance Eq (UpdateExpression params columns ty)
1595−deriving instance Ord (UpdateExpression params columns ty)
1597−renderUpdateExpression
1598− :: Aliased (UpdateExpression params columns) column
1599− -> Maybe ByteString
1600−renderUpdateExpression = \case
1601− Same `As` _ -> Nothing
1602− Set expression `As` column -> Just $
1603− renderAlias column <+> "=" <+> renderExpression expression
1606− :: (HasTable table schema columns, SListI columns, SListI results)
1608− -> NP (Aliased (UpdateExpression params columns)) columns
1609− -> Condition params '[tab ::: columns] 'Ungrouped
1610− -> ReturningClause params columns results
1611− -> Manipulation schema params results
1612−update table columns wh returning = UnsafeManipulation $
1614− <+> renderAlias table
1616− <+> renderCommaSeparatedMaybe renderUpdateExpression columns
1617− <+> "WHERE" <+> renderExpression wh
1618− <> renderReturningClause returning
1620−{-----------------------------------------
1622−-----------------------------------------}
1625− :: HasTable table schema columns
1627− -> Condition params '[table ::: columns] 'Ungrouped
1628− -> Manipulation schema params '[]
1629−deleteFrom table wh = UnsafeManipulation $
1630− "DELETE FROM" <+> renderAlias table
1631− <+> "WHERE" <+> renderExpression wh <> ";"
1633−{-----------------------------------------
1634−render helper functions
1635−-----------------------------------------}
1637−parenthesized :: ByteString -> ByteString
1638−parenthesized str = "(" <> str <> ")"
1640−(<+>) :: ByteString -> ByteString -> ByteString
1641−str1 <+> str2 = str1 <> " " <> str2
1643−commaSeparated :: [ByteString] -> ByteString
1644−commaSeparated = ByteString.intercalate ", "
1646−renderCommaSeparated
1648− => (forall x. expression x -> ByteString)
1649− -> NP expression xs -> ByteString
1650−renderCommaSeparated render
1653− . hmap (K . render)
1655−renderCommaSeparatedMaybe
1657− => (forall x. expression x -> Maybe ByteString)
1658− -> NP expression xs -> ByteString
1659−renderCommaSeparatedMaybe render
1663− . hmap (K . render)
1665−renderNat :: KnownNat n => proxy n -> ByteString
1666−renderNat (_ :: proxy n) = fromString (show (natVal' (proxy# :: Proxy# n)))