@@ -1,1669 +0,0 @@
1−{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
9− , FunctionalDependencies
11− , GeneralizedNewtypeDeriving
17− , ScopedTypeVariables
23− , UndecidableInstances
26−module Squeal.PostgreSQL.Statement
27− ( -- * Column Expressions
28− Expression (UnsafeExpression, renderExpression)
29− , HasParameter (param)
30− , HasColumn (getColumn)
81− , unsafeAggregateDistinct
84− , PGAvg (avg, avgDistinct)
100− , Table (UnsafeTable, renderTable)
101− , HasTable (getTable)
102− -- ** Froms and Joins
105− -- ** Table Expressions
106− , TableExpression (..)
107− , renderTableExpression
117− , GroupByClause (NoGroups, Group)
118− , renderGroupByClause
119− , HavingClause (NoHaving, Having)
120− , renderHavingClause
121− , GroupedBy (getGroup1, getGroup2)
122− , SortExpression (..)
123− , renderSortExpression
125− , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
153− , timestampWithTimeZone
165− , Query (UnsafeQuery, renderQuery)
169− , selectDistinctStar
171− , selectDistinctDotStar
178− -- ** Data Definition Language
179− , Definition (UnsafeDefinition, renderDefinition)
181− , TableConstraint (UnsafeTableConstraint, renderTableConstraint)
186− , OnDelete (OnDeleteNoAction, OnDeleteRestrict, OnDeleteCascade)
188− , OnUpdate (OnUpdateNoAction, OnUpdateRestrict, OnUpdateCascade)
193− , AlterTable (UnsafeAlterTable, renderAlterTable)
199− , AlterColumn (UnsafeAlterColumn, renderAlterColumn)
205− -- ** Data Manipulation Language
206− , Manipulation (UnsafeManipulation, renderManipulation)
209− , ValuesClause (Values, ValuesQuery)
210− , renderValuesClause
211− , ReturningClause (ReturningStar, Returning)
212− , renderReturningClause
213− , ConflictClause (Conflict, OnConflictDoNothing, OnConflictDoUpdate)
214− , renderConflictClause
215− , UpdateExpression (Same, Set)
216− , renderUpdateExpression
221−import Control.Category
222−import Control.DeepSeq
223−import Data.ByteString (ByteString)
224−import Data.Data (Data)
227−import Data.Monoid hiding (All)
231−import Generics.SOP hiding (from)
233−import GHC.OverloadedLabels
235−import Prelude hiding (id, (.))
237−import qualified GHC.Generics as GHC
238−import qualified Data.ByteString as ByteString
240−import Squeal.PostgreSQL.Schema
242−{-----------------------------------------
244−-----------------------------------------}
246−{- | `Expression`s are used in a variety of contexts,
247−such as in the target list of the `select` command,
248−as new column values in `insertInto` or `update`,
249−or in search `Condition`s in a number of commands.
251−The expression syntax allows the calculation of
252−values from primitive expression using arithmetic, logical,
253−and other operations.
256− (params :: [ColumnType])
257− (tables :: TablesType)
258− (grouping :: Grouping)
260− = UnsafeExpression { renderExpression :: ByteString }
261− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
263−{- | A `HasParameter` constraint is used to indicate a value that is
264−supplied externally to a SQL statement.
265−`Squeal.PostgreSQL.PQ.manipulateParams`,
266−`Squeal.PostgreSQL.PQ.queryParams` and
267−`Squeal.PostgreSQL.PQ.traversePrepared` support specifying data values
268−separately from the SQL command string, in which case `param`s are used to
269−refer to the out-of-line data values.
271−class (PGTyped (BaseType ty), KnownNat n)
272− => HasParameter (n :: Nat) params ty | n params -> ty where
273− param :: Expression params tables grouping ty
274− param = UnsafeExpression $ parenthesized $
275− "$" <> renderNat (Proxy @n) <+> "::"
276− <+> renderTypeExpression (pgtype @(BaseType ty))
277−instance {-# OVERLAPPING #-} PGTyped (BaseType ty1)
278− => HasParameter 1 (ty1:tys) ty1
279−instance {-# OVERLAPPABLE #-} (KnownNat n, HasParameter (n-1) params ty)
280− => HasParameter n (ty' : params) ty
282−{- | A `HasColumn` constraint indicates an unqualified column reference.
283−`getColumn` can only be unambiguous when the `TableExpression` the column
284−references is unique, in which case the column may be referenced using
285−@-XOverloadedLabels@. Otherwise, combined with a `HasTable` constraint, the
286−qualified column reference operator `!` may be used.
288−class KnownSymbol column => HasColumn column columns ty
289− | column columns -> ty where
291− :: HasUnique table tables columns
293− -> Expression params tables 'Ungrouped ty
294− getColumn column = UnsafeExpression $ renderAlias column
295−instance {-# OVERLAPPING #-} KnownSymbol column
296− => HasColumn column ((column ::: optionality ty) ': tys) ('Required ty)
297−instance {-# OVERLAPPABLE #-} (KnownSymbol column, HasColumn column table ty)
298− => HasColumn column (ty' ': table) ty
300−instance (HasColumn column columns ty, HasUnique table tables columns)
301− => IsLabel column (Expression params tables 'Ungrouped ty) where
302− fromLabel = getColumn (Alias @column)
304−instance (HasTable table tables columns, HasColumn column columns ty)
305− => IsTableColumn table column (Expression params tables 'Ungrouped ty) where
306− table ! column = UnsafeExpression $
307− renderAlias table <> "." <> renderAlias column
310− (columns :: ColumnsType)
311− (columnty :: (Symbol,ColumnType))
314− :: HasColumn column columns ty
316− -> Column columns (column ::: ty)
317−deriving instance Show (Column columns columnty)
318−deriving instance Eq (Column columns columnty)
319−deriving instance Ord (Column columns columnty)
321−-- | >>> renderExpression def
323−def :: Expression params '[] 'Ungrouped ('Optional (nullity ty))
324−def = UnsafeExpression "DEFAULT"
326−-- | >>> renderExpression $ unDef false
329− :: Expression params '[] 'Ungrouped ('Required (nullity ty))
330− -> Expression params '[] 'Ungrouped ('Optional (nullity ty))
331−unDef = UnsafeExpression . renderExpression
333−-- | analagous to `Nothing`
335−-- >>> renderExpression $ null_
337−null_ :: Expression params tables grouping (optionality ('Null ty))
338−null_ = UnsafeExpression "NULL"
340−-- | analagous to `Just`
342−-- >>> renderExpression $ unNull true
345− :: Expression params tables grouping (optionality ('NotNull ty))
346− -> Expression params tables grouping (optionality ('Null ty))
347−unNull = UnsafeExpression . renderExpression
349−-- | return the leftmost value which is not NULL
351−-- >>> renderExpression $ coalesce [null_, unNull true] false
352−-- "COALESCE(NULL, TRUE, FALSE)"
354− :: [Expression params tables grouping ('Required ('Null ty))]
355− -> Expression params tables grouping ('Required ('NotNull ty))
356− -> Expression params tables grouping ('Required ('NotNull ty))
357−coalesce nullxs notNullx = UnsafeExpression $
358− "COALESCE" <> parenthesized (commaSeparated
359− ((renderExpression <$> nullxs) <> [renderExpression notNullx]))
361−-- | analagous to `fromMaybe` using @COALESCE@
363−-- >>> renderExpression $ fromNull true null_
364−-- "COALESCE(NULL, TRUE)"
366− :: Expression params tables grouping ('Required ('NotNull ty))
367− -> Expression params tables grouping ('Required ('Null ty))
368− -> Expression params tables grouping ('Required ('NotNull ty))
369−fromNull notNullx nullx = coalesce [nullx] notNullx
371−-- | >>> renderExpression $ null_ & isNull
374− :: Expression params tables grouping ('Required ('Null ty))
375− -> Condition params tables grouping
376−isNull x = UnsafeExpression $ renderExpression x <+> "IS NULL"
378−-- | >>> renderExpression $ null_ & isn'tNull
379−-- "NULL IS NOT NULL"
381− :: Expression params tables grouping ('Required ('Null ty))
382− -> Condition params tables grouping
383−isn'tNull x = UnsafeExpression $ renderExpression x <+> "IS NOT NULL"
385−-- | analagous to `maybe` using @IS NULL@
387−-- >>> renderExpression $ matchNull true not null_
388−-- "CASE WHEN NULL IS NULL THEN TRUE ELSE (NOT TRUE) END"
390− :: Expression params tables grouping ('Required nullty)
391− -> ( Expression params tables grouping ('Required ('NotNull ty))
392− -> Expression params tables grouping ('Required nullty) )
393− -> Expression params tables grouping ('Required ('Null ty))
394− -> Expression params tables grouping ('Required nullty)
395−matchNull y f x = ifThenElse (isNull x) y
396− (f (UnsafeExpression (renderExpression y)))
398−-- | left inverse to `fromNull`
400−-- >>> renderExpression $ nullIf false (fromNull false null_)
401−-- "NULL IF (FALSE, COALESCE(NULL, FALSE))"
403− :: Expression params tables grouping ('Required ('NotNull ty))
404− -> Expression params tables grouping ('Required ('NotNull ty))
405− -> Expression params tables grouping ('Required ('Null ty))
406−nullIf x y = UnsafeExpression $ "NULL IF" <+> parenthesized
407− (renderExpression x <> ", " <> renderExpression y)
409−-- | >>> renderExpression $ greatest @'[_] currentTimestamp [param (Proxy @1)]
410−-- "GREATEST(CURRENT_TIMESTAMP, ($1 :: timestamp with time zone))"
412− :: Expression params tables grouping ('Required nullty)
413− -> [Expression params tables grouping ('Required nullty)]
414− -> Expression params table grouping ('Required nullty)
415−greatest x xs = UnsafeExpression $ "GREATEST("
416− <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
418−-- | >>> renderExpression $ least currentTimestamp [null_]
419−-- "LEAST(CURRENT_TIMESTAMP, NULL)"
421− :: Expression params tables grouping ('Required nullty)
422− -> [Expression params tables grouping ('Required nullty)]
423− -> Expression params table grouping ('Required nullty)
424−least x xs = UnsafeExpression $ "LEAST("
425− <> commaSeparated (renderExpression <$> (x:xs)) <> ")"
429− -> Expression params tables grouping ('Required ty0)
430− -> Expression params tables grouping ('Required ty1)
431− -> Expression params tables grouping ('Required ty2)
432−unsafeBinaryOp op x y = UnsafeExpression $ parenthesized $
433− renderExpression x <+> op <+> renderExpression y
437− -> Expression params tables grouping ('Required ty0)
438− -> Expression params tables grouping ('Required ty1)
439−unsafeUnaryOp op x = UnsafeExpression $ parenthesized $
440− op <+> renderExpression x
444− -> Expression params tables grouping ('Required xty)
445− -> Expression params tables grouping ('Required yty)
446−unsafeFunction fun x = UnsafeExpression $
447− fun <> parenthesized (renderExpression x)
450− => Num (Expression params tables grouping ('Required (nullity ty))) where
451− (+) = unsafeBinaryOp "+"
452− (-) = unsafeBinaryOp "-"
453− (*) = unsafeBinaryOp "*"
454− abs = unsafeFunction "abs"
455− signum = unsafeFunction "sign"
461−instance (PGNum ty, PGFloating ty) => Fractional
462− (Expression params tables grouping ('Required (nullity ty))) where
463− (/) = unsafeBinaryOp "/"
464− fromRational x = fromInteger (numerator x) / fromInteger (denominator x)
466−instance (PGNum ty, PGFloating ty) => Floating
467− (Expression params tables grouping ('Required (nullity ty))) where
468− pi = UnsafeExpression "pi()"
469− exp = unsafeFunction "exp"
470− log = unsafeFunction "ln"
471− sqrt = unsafeFunction "sqrt"
472− b ** x = UnsafeExpression $
473− "power(" <> renderExpression b <> ", " <> renderExpression x <> ")"
474− logBase b y = log y / log b
475− sin = unsafeFunction "sin"
476− cos = unsafeFunction "cos"
477− tan = unsafeFunction "tan"
478− asin = unsafeFunction "asin"
479− acos = unsafeFunction "acos"
480− atan = unsafeFunction "atan"
481− sinh x = (exp x - exp (-x)) / 2
482− cosh x = (exp x + exp (-x)) / 2
483− tanh x = sinh x / cosh x
484− asinh x = log (x + sqrt (x*x + 1))
485− acosh x = log (x + sqrt (x*x - 1))
486− atanh x = log ((1 + x) / (1 - x)) / 2
490− => Expression params tables grouping ('Required (nullity float))
491− -> Expression params tables grouping ('Required (nullity float))
492− -> Expression params tables grouping ('Required (nullity float))
493−atan2_ y x = UnsafeExpression $
494− "atan2(" <> renderExpression y <> ", " <> renderExpression x <> ")"
497− :: TypeExpression ('Required ('Null ty1))
498− -> Expression params tables grouping ('Required (nullity ty0))
499− -> Expression params tables grouping ('Required (nullity ty1))
500−cast ty x = UnsafeExpression $ parenthesized $
501− renderExpression x <+> "::" <+> renderTypeExpression ty
505− => Expression params tables grouping ('Required (nullity int))
506− -> Expression params tables grouping ('Required (nullity int))
507− -> Expression params tables grouping ('Required (nullity int))
508−div_ = unsafeBinaryOp "/"
512− => Expression params tables grouping ('Required (nullity int))
513− -> Expression params tables grouping ('Required (nullity int))
514− -> Expression params tables grouping ('Required (nullity int))
515−mod_ = unsafeBinaryOp "%"
519− => Expression params tables grouping ('Required (nullity frac))
520− -> Expression params tables grouping ('Required (nullity frac))
521−trunc = unsafeFunction "trunc"
525− => Expression params tables grouping ('Required (nullity frac))
526− -> Expression params tables grouping ('Required (nullity frac))
527−round_ = unsafeFunction "round"
531− => Expression params tables grouping ('Required (nullity frac))
532− -> Expression params tables grouping ('Required (nullity frac))
533−ceiling_ = unsafeFunction "ceiling"
536− (Expression params tables grouping ('Required (nullity 'PGtext))) where
537− fromString str = UnsafeExpression $
538− "E\'" <> fromString (escape =<< str) <> "\'"
551−type Condition params tables grouping =
552− Expression params tables grouping ('Required ('NotNull 'PGbool))
554−true :: Condition params tables grouping
555−true = UnsafeExpression "TRUE"
557−false :: Condition params tables grouping
558−false = UnsafeExpression "FALSE"
561− :: Condition params tables grouping
562− -> Condition params tables grouping
563−not_ = unsafeUnaryOp "NOT"
566− :: Condition params tables grouping
567− -> Condition params tables grouping
568− -> Condition params tables grouping
569−(&&*) = unsafeBinaryOp "AND"
572− :: Condition params tables grouping
573− -> Condition params tables grouping
574− -> Condition params tables grouping
575−(||*) = unsafeBinaryOp "OR"
578− :: [ ( Condition params tables grouping
579− , Expression params tables grouping ('Required ty)
581− -> Expression params tables grouping ('Required ty)
582− -> Expression params tables grouping ('Required ty)
583−caseWhenThenElse whenThens else_ = UnsafeExpression $ mconcat
587− [ " WHEN ", renderExpression when_
588− , " THEN ", renderExpression then_
590− | (when_,then_) <- whenThens
592− , " ELSE ", renderExpression else_
597− :: Condition params tables grouping
598− -> Expression params tables grouping ('Required ty)
599− -> Expression params tables grouping ('Required ty)
600− -> Expression params tables grouping ('Required ty)
601−ifThenElse if_ then_ else_ = caseWhenThenElse [(if_,then_)] else_
604− :: Expression params tables grouping ('Required (nullity ty))
605− -> Expression params tables grouping ('Required (nullity ty))
606− -> Expression params tables grouping ('Required (nullity 'PGbool))
607−(==*) = unsafeBinaryOp "="
611− :: Expression params tables grouping ('Required (nullity ty))
612− -> Expression params tables grouping ('Required (nullity ty))
613− -> Expression params tables grouping ('Required (nullity 'PGbool))
614−(/=*) = unsafeBinaryOp "<>"
618− :: Expression params tables grouping ('Required (nullity ty))
619− -> Expression params tables grouping ('Required (nullity ty))
620− -> Expression params tables grouping ('Required (nullity 'PGbool))
621−(>=*) = unsafeBinaryOp ">="
625− :: Expression params tables grouping ('Required (nullity ty))
626− -> Expression params tables grouping ('Required (nullity ty))
627− -> Expression params tables grouping ('Required (nullity 'PGbool))
628−(*<) = unsafeBinaryOp "<"
632− :: Expression params tables grouping ('Required (nullity ty))
633− -> Expression params tables grouping ('Required (nullity ty))
634− -> Expression params tables grouping ('Required (nullity 'PGbool))
635−(*<=) = unsafeBinaryOp "<="
639− :: Expression params tables grouping ('Required (nullity ty))
640− -> Expression params tables grouping ('Required (nullity ty))
641− -> Expression params tables grouping ('Required (nullity 'PGbool))
642−(>*) = unsafeBinaryOp ">"
646− :: Expression params tables grouping ('Required (nullity 'PGdate))
647−currentDate = UnsafeExpression "CURRENT_DATE"
650− :: Expression params tables grouping ('Required (nullity 'PGtimetz))
651−currentTime = UnsafeExpression "CURRENT_TIME"
654− :: Expression params tables grouping ('Required (nullity 'PGtimestamptz))
655−currentTimestamp = UnsafeExpression "CURRENT_TIMESTAMP"
658− :: Expression params tables grouping ('Required (nullity 'PGtime))
659−localTime = UnsafeExpression "LOCALTIME"
662− :: Expression params tables grouping ('Required (nullity 'PGtimestamp))
663−localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
665−data SortExpression params tables grouping where
667− :: Expression params tables grouping ('Required ('NotNull ty))
668− -> SortExpression params tables grouping
670− :: Expression params tables grouping ('Required ('NotNull ty))
671− -> SortExpression params tables grouping
673− :: Expression params tables grouping ('Required ('Null ty))
674− -> SortExpression params tables grouping
676− :: Expression params tables grouping ('Required ('Null ty))
677− -> SortExpression params tables grouping
679− :: Expression params tables grouping ('Required ('Null ty))
680− -> SortExpression params tables grouping
682− :: Expression params tables grouping ('Required ('Null ty))
683− -> SortExpression params tables grouping
684−deriving instance Show (SortExpression params tables grouping)
686−renderSortExpression :: SortExpression params tables grouping -> ByteString
687−renderSortExpression = \case
688− Asc expression -> renderExpression expression <+> "ASC"
689− Desc expression -> renderExpression expression <+> "DESC"
690− AscNullsFirst expression -> renderExpression expression
691− <+> "ASC NULLS FIRST"
692− DescNullsFirst expression -> renderExpression expression
693− <+> "DESC NULLS FIRST"
694− AscNullsLast expression -> renderExpression expression <+> "ASC NULLS LAST"
695− DescNullsLast expression -> renderExpression expression <+> "DESC NULLS LAST"
698−{-----------------------------------------
700−-----------------------------------------}
703− (tables :: TablesType)
704− (by :: (Symbol,Symbol)) where
706− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
708− -> By tables '(table, column)
710− :: (HasTable table tables columns, HasColumn column columns ty)
711− => (Alias table, Alias column)
712− -> By tables '(table, column)
713−deriving instance Show (By tables by)
714−deriving instance Eq (By tables by)
715−deriving instance Ord (By tables by)
717−renderBy :: By tables tabcolty -> ByteString
719− By column -> renderAlias column
720− By2 (table, column) -> renderAlias table <> "." <> renderAlias column
722−data GroupByClause tables grouping where
723− NoGroups :: GroupByClause tables 'Ungrouped
726− => NP (By tables) bys
727− -> GroupByClause tables ('Grouped bys)
729−renderGroupByClause :: GroupByClause tables grouping -> ByteString
730−renderGroupByClause = \case
733− Group bys -> " GROUP BY" <+> renderCommaSeparated renderBy bys
735−data HavingClause params tables grouping where
736− NoHaving :: HavingClause params tables 'Ungrouped
738− :: [Condition params tables ('Grouped bys)]
739− -> HavingClause params tables ('Grouped bys)
740−deriving instance Show (HavingClause params tables grouping)
741−deriving instance Eq (HavingClause params tables grouping)
742−deriving instance Ord (HavingClause params tables grouping)
744−renderHavingClause :: HavingClause params tables grouping -> ByteString
745−renderHavingClause = \case
748− Having conditions ->
749− " HAVING" <+> commaSeparated (renderExpression <$> conditions)
751−class (KnownSymbol table, KnownSymbol column)
752− => GroupedBy table column bys where
754− :: (tables ~ '[table ::: columns], HasColumn column columns ty)
756− -> Expression params tables ('Grouped bys) ty
757− getGroup1 column = UnsafeExpression $ renderAlias column
759− :: (HasTable table tables columns, HasColumn column columns ty)
762− -> Expression params tables ('Grouped bys) ty
763− getGroup2 table column = UnsafeExpression $
764− renderAlias table <> "." <> renderAlias column
765−instance {-# OVERLAPPING #-} (KnownSymbol table, KnownSymbol column)
766− => GroupedBy table column ('(table,column) ': bys)
767−instance {-# OVERLAPPABLE #-}
769− , KnownSymbol column
770− , GroupedBy table column bys
771− ) => GroupedBy table column (tabcol ': bys)
774− ( HasUnique table tables columns
775− , HasColumn column columns ty
776− , GroupedBy table column bys
778− (Expression params tables ('Grouped bys) ty) where
779− fromLabel = getGroup1 (Alias @column)
782− ( HasTable table tables columns
783− , HasColumn column columns ty
784− , GroupedBy table column bys
785− ) => IsTableColumn table column
786− (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
788−-- data AllOrDistinct = All | Distinct
789−-- deriving (GHC.Generic,Show,Eq,Ord,Data)
790−-- instance NFData AllOrDistinct
792−-- renderAllOrDistinct :: AllOrDistinct -> ByteString
793−-- renderAllOrDistinct = \case
795−-- Distinct -> "DISTINCT"
797−unsafeAggregate, unsafeAggregateDistinct
799− -> Expression params tables 'Ungrouped ('Required xty)
800− -> Expression params tables ('Grouped bys) ('Required yty)
801−unsafeAggregate fun x = UnsafeExpression $ mconcat
802− [fun, "(", renderExpression x, ")"]
803−unsafeAggregateDistinct fun x = UnsafeExpression $ mconcat
804− [fun, "(DISTINCT ", renderExpression x, ")"]
808− => Expression params tables 'Ungrouped ('Required (nullity ty))
809− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
810−sum_ = unsafeAggregate "sum"
811−sumDistinct = unsafeAggregateDistinct "sum"
813−class PGAvg ty avg | ty -> avg where
815− :: Expression params tables 'Ungrouped ('Required (nullity ty))
816− -> Expression params tables ('Grouped bys) ('Required (nullity avg))
817− avg = unsafeAggregate "avg"
818− avgDistinct = unsafeAggregateDistinct "avg"
819−instance PGAvg 'PGint2 'PGnumeric
820−instance PGAvg 'PGint4 'PGnumeric
821−instance PGAvg 'PGint8 'PGnumeric
822−instance PGAvg 'PGnumeric 'PGnumeric
823−instance PGAvg 'PGfloat4 'PGfloat8
824−instance PGAvg 'PGfloat8 'PGfloat8
825−instance PGAvg 'PGinterval 'PGinterval
829− => Expression params tables 'Ungrouped ('Required (nullity int))
830− -> Expression params tables ('Grouped bys) ('Required (nullity int))
831−bitAnd = unsafeAggregate "bit_and"
832−bitOr = unsafeAggregate "bit_or"
835− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
836− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
837−boolAnd = unsafeAggregate "bool_and"
838−boolOr = unsafeAggregate "bool_or"
840−countStar, countDistinctStar
841− :: Expression params table ('Grouped bys) ('Required ('NotNull 'PGint8))
842−countStar = UnsafeExpression $ "count(*)"
843−countDistinctStar = UnsafeExpression $ "count(DISTINCT *)"
846− :: Expression params tables 'Ungrouped ('Required ty)
847− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGint8))
848−count = unsafeAggregate "count"
851− :: Expression params tables 'Ungrouped ('Required (nullity 'PGbool))
852− -> Expression params tables ('Grouped bys) ('Required (nullity 'PGbool))
853−every = unsafeAggregate "every"
856− :: Expression params tables 'Ungrouped ('Required ty)
857− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
858−jsonAgg = unsafeAggregate "json_agg"
861− :: Expression params tables 'Ungrouped ('Required ty)
862− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
863−jsonbAgg = unsafeAggregate "jsonb_agg"
866− :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
867− -> Expression params tables 'Ungrouped ('Required valty)
868− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjson))
869−jsonObjectAgg k v = UnsafeExpression $
870− "json_object_agg(" <> renderExpression k
871− <> ", " <> renderExpression v <> ")"
874− :: Expression params tables 'Ungrouped ('Required ('NotNull keyty))
875− -> Expression params tables 'Ungrouped ('Required valty)
876− -> Expression params tables ('Grouped bys) ('Required ('NotNull 'PGjsonb))
877−jsonbObjectAgg k v = UnsafeExpression $
879− <> renderExpression k <> ", " <> renderExpression v <> ")"
882− :: Expression params tables 'Ungrouped ('Required (nullity ty))
883− -> Expression params tables ('Grouped bys) ('Required (nullity ty))
884−max_ = unsafeAggregate "max"
885−min_ = unsafeAggregate "min"
887−{-----------------------------------------
889−-----------------------------------------}
892− (schema :: TablesType)
893− (columns :: ColumnsType)
894− = UnsafeTable { renderTable :: ByteString }
895− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
897−class KnownSymbol table => HasTable table tables columns
898− | table tables -> columns where
899− getTable :: Alias table -> Table tables columns
900− getTable table = UnsafeTable $ renderAlias table
901−instance {-# OVERLAPPING #-} KnownSymbol table
902− => HasTable table ((table ::: columns) ': tables) columns
903−instance {-# OVERLAPPABLE #-}
904− (KnownSymbol table, HasTable table schema columns)
905− => HasTable table (table' ': schema) columns
907−instance HasTable table schema columns
908− => IsLabel table (Table schema columns) where
909− fromLabel = getTable (Alias @table)
911−data FromClause params schema tables where
913− :: Aliased (Table schema) table
914− -> FromClause params schema '[table]
916− :: Aliased (Query schema params) table
917− -> FromClause params schema '[table]
919− :: FromClause params schema right
920− -> FromClause params schema left
921− -> FromClause params schema (Join left right)
923− :: FromClause params schema right
924− -> Condition params (Join left right) 'Ungrouped
925− -> FromClause params schema left
926− -> FromClause params schema (Join left right)
928− :: FromClause params schema right
929− -> Condition params (Join left right) 'Ungrouped
930− -> FromClause params schema left
931− -> FromClause params schema (Join left (NullifyTables right))
933− :: FromClause params schema right
934− -> Condition params (Join left right) 'Ungrouped
935− -> FromClause params schema left
936− -> FromClause params schema (Join (NullifyTables left) right)
938− :: FromClause params schema right
939− -> Condition params (Join left right) 'Ungrouped
940− -> FromClause params schema left
941− -> FromClause params schema
942− (Join (NullifyTables left) (NullifyTables right))
944−renderFromClause :: FromClause params schema tables -> ByteString
945−renderFromClause = \case
946− Table table -> renderAliased renderTable table
947− Subquery selection -> renderAliased (parenthesized . renderQuery) selection
948− CrossJoin right left ->
949− renderFromClause left <+> "CROSS JOIN" <+> renderFromClause right
950− InnerJoin right on left -> renderJoin "INNER JOIN" right on left
951− LeftOuterJoin right on left -> renderJoin "LEFT OUTER JOIN" right on left
952− RightOuterJoin right on left -> renderJoin "RIGHT OUTER JOIN" right on left
953− FullOuterJoin right on left -> renderJoin "FULL OUTER JOIN" right on left
955− renderJoin op right on left =
956− renderFromClause left <+> op <+> renderFromClause right
957− <+> "ON" <+> renderExpression on
960− (params :: [ColumnType])
961− (schema :: TablesType)
962− (tables :: TablesType)
963− (grouping :: Grouping)
965− { fromClause :: FromClause params schema tables
966− , whereClause :: [Condition params tables 'Ungrouped]
967− , groupByClause :: GroupByClause tables grouping
968− , havingClause :: HavingClause params tables grouping
969− , orderByClause :: [SortExpression params tables grouping]
970− , limitClause :: [Word64]
971− , offsetClause :: [Word64]
974−renderTableExpression
975− :: TableExpression params schema tables grouping
977−renderTableExpression
978− (TableExpression tables whs' grps' hvs' srts' lims' offs') = mconcat
979− [ "FROM ", renderFromClause tables
981− , renderGroupByClause grps'
982− , renderHavingClause hvs'
984− , renderLimits lims'
985− , renderOffsets offs'
988− renderWheres = \case
990− wh:[] -> " WHERE" <+> renderExpression wh
991− wh:whs -> " WHERE" <+> renderExpression (foldr (&&*) wh whs)
995− <+> commaSeparated (renderSortExpression <$> srts)
996− renderLimits = \case
998− lims -> " LIMIT" <+> fromString (show (minimum lims))
999− renderOffsets = \case
1001− offs -> " OFFSET" <+> fromString (show (sum offs))
1004− :: FromClause params schema tables
1005− -> TableExpression params schema tables 'Ungrouped
1006−from tables = TableExpression tables [] NoGroups NoHaving [] [] []
1009− :: Condition params tables 'Ungrouped
1010− -> TableExpression params schema tables grouping
1011− -> TableExpression params schema tables grouping
1012−where_ wh tables = tables {whereClause = wh : whereClause tables}
1016− => NP (By tables) bys
1017− -> TableExpression params schema tables 'Ungrouped
1018− -> TableExpression params schema tables ('Grouped bys)
1019−group bys tables = TableExpression
1020− { fromClause = fromClause tables
1021− , whereClause = whereClause tables
1022− , groupByClause = Group bys
1023− , havingClause = Having []
1024− , orderByClause = []
1025− , limitClause = limitClause tables
1026− , offsetClause = offsetClause tables
1030− :: Condition params tables ('Grouped bys)
1031− -> TableExpression params schema tables ('Grouped bys)
1032− -> TableExpression params schema tables ('Grouped bys)
1033−having hv tables = tables
1034− { havingClause = case havingClause tables of Having hvs -> Having (hv:hvs) }
1037− :: [SortExpression params tables grouping]
1038− -> TableExpression params schema tables grouping
1039− -> TableExpression params schema tables grouping
1040−orderBy srts tables = tables {orderByClause = orderByClause tables ++ srts}
1044− -> TableExpression params schema tables grouping
1045− -> TableExpression params schema tables grouping
1046−limit lim tables = tables {limitClause = lim : limitClause tables}
1050− -> TableExpression params schema tables grouping
1051− -> TableExpression params schema tables grouping
1052−offset off tables = tables {offsetClause = off : offsetClause tables}
1054−{-----------------------------------------
1056−-----------------------------------------}
1058−newtype TypeExpression (ty :: ColumnType)
1059− = UnsafeTypeExpression { renderTypeExpression :: ByteString }
1060− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1062−bool :: TypeExpression ('Required ('Null 'PGbool))
1063−bool = UnsafeTypeExpression "bool"
1064−int2 :: TypeExpression ('Required ('Null 'PGint2))
1065−int2 = UnsafeTypeExpression "int2"
1066−smallint :: TypeExpression ('Required ('Null 'PGint2))
1067−smallint = UnsafeTypeExpression "smallint"
1068−int4 :: TypeExpression ('Required ('Null 'PGint4))
1069−int4 = UnsafeTypeExpression "int4"
1070−int :: TypeExpression ('Required ('Null 'PGint4))
1071−int = UnsafeTypeExpression "int"
1072−integer :: TypeExpression ('Required ('Null 'PGint4))
1073−integer = UnsafeTypeExpression "integer"
1074−int8 :: TypeExpression ('Required ('Null 'PGint8))
1075−int8 = UnsafeTypeExpression "int8"
1076−bigint :: TypeExpression ('Required ('Null 'PGint8))
1077−bigint = UnsafeTypeExpression "bigint"
1078−numeric :: TypeExpression ('Required ('Null 'PGnumeric))
1079−numeric = UnsafeTypeExpression "numeric"
1080−float4 :: TypeExpression ('Required ('Null 'PGfloat4))
1081−float4 = UnsafeTypeExpression "float4"
1082−real :: TypeExpression ('Required ('Null 'PGfloat4))
1083−real = UnsafeTypeExpression "real"
1084−float8 :: TypeExpression ('Required ('Null 'PGfloat8))
1085−float8 = UnsafeTypeExpression "float8"
1086−doublePrecision :: TypeExpression ('Required ('Null 'PGfloat8))
1087−doublePrecision = UnsafeTypeExpression "double precision"
1088−serial2 :: TypeExpression ('Optional ('NotNull 'PGint2))
1089−serial2 = UnsafeTypeExpression "serial2"
1090−smallserial :: TypeExpression ('Optional ('NotNull 'PGint2))
1091−smallserial = UnsafeTypeExpression "smallserial"
1092−serial4 :: TypeExpression ('Optional ('NotNull 'PGint4))
1093−serial4 = UnsafeTypeExpression "serial4"
1094−serial :: TypeExpression ('Optional ('NotNull 'PGint4))
1095−serial = UnsafeTypeExpression "serial"
1096−serial8 :: TypeExpression ('Optional ('NotNull 'PGint8))
1097−serial8 = UnsafeTypeExpression "serial8"
1098−bigserial :: TypeExpression ('Optional ('NotNull 'PGint8))
1099−bigserial = UnsafeTypeExpression "bigserial"
1100−text :: TypeExpression ('Required ('Null 'PGtext))
1101−text = UnsafeTypeExpression "text"
1103− :: (KnownNat n, 1 <= n)
1105− -> TypeExpression ('Required ('Null ('PGchar n)))
1106−char p = UnsafeTypeExpression $ "char(" <> renderNat p <> ")"
1108− :: (KnownNat n, 1 <= n)
1110− -> TypeExpression ('Required ('Null ('PGchar n)))
1111−character p = UnsafeTypeExpression $ "character(" <> renderNat p <> ")"
1113− :: (KnownNat n, 1 <= n)
1115− -> TypeExpression ('Required ('Null ('PGvarchar n)))
1116−varchar p = UnsafeTypeExpression $ "varchar(" <> renderNat p <> ")"
1118− :: (KnownNat n, 1 <= n)
1120− -> TypeExpression ('Required ('Null ('PGvarchar n)))
1121−characterVarying p = UnsafeTypeExpression $
1122− "character varying(" <> renderNat p <> ")"
1123−bytea :: TypeExpression ('Required ('Null ('PGbytea)))
1124−bytea = UnsafeTypeExpression "bytea"
1125−timestamp :: TypeExpression ('Required ('Null ('PGtimestamp)))
1126−timestamp = UnsafeTypeExpression "timestamp"
1127−timestampWithTimeZone :: TypeExpression ('Required ('Null ('PGtimestamptz)))
1128−timestampWithTimeZone = UnsafeTypeExpression "timestamp with time zone"
1129−date :: TypeExpression ('Required ('Null ('PGdate)))
1130−date = UnsafeTypeExpression "date"
1131−time :: TypeExpression ('Required ('Null ('PGtime)))
1132−time = UnsafeTypeExpression "time"
1133−timeWithTimeZone :: TypeExpression ('Required ('Null ('PGtimetz)))
1134−timeWithTimeZone = UnsafeTypeExpression "time with time zone"
1135−interval :: TypeExpression ('Required ('Null ('PGinterval)))
1136−interval = UnsafeTypeExpression "interval"
1137−uuid :: TypeExpression ('Required ('Null ('PGuuid)))
1138−uuid = UnsafeTypeExpression "uuid"
1139−json :: TypeExpression ('Required ('Null ('PGjson)))
1140−json = UnsafeTypeExpression "json"
1141−jsonb :: TypeExpression ('Required ('Null ('PGjsonb)))
1142−jsonb = UnsafeTypeExpression "jsonb"
1145− :: TypeExpression (optionality ('Null ty))
1146− -> TypeExpression (optionality ('NotNull ty))
1147−notNull ty = UnsafeTypeExpression $ renderTypeExpression ty <+> "NOT NULL"
1149− :: Expression '[] '[] 'Ungrouped ('Required ty)
1150− -> TypeExpression ('Required ty)
1151− -> TypeExpression ('Optional ty)
1152−default_ x ty = UnsafeTypeExpression $
1153− renderTypeExpression ty <+> "DEFAULT" <+> renderExpression x
1155−class PGTyped (ty :: PGType) where
1156− pgtype :: TypeExpression ('Required ('Null ty))
1157−instance PGTyped 'PGbool where pgtype = bool
1158−instance PGTyped 'PGint2 where pgtype = int2
1159−instance PGTyped 'PGint4 where pgtype = int4
1160−instance PGTyped 'PGint8 where pgtype = int8
1161−instance PGTyped 'PGnumeric where pgtype = numeric
1162−instance PGTyped 'PGfloat4 where pgtype = float4
1163−instance PGTyped 'PGfloat8 where pgtype = float8
1164−instance PGTyped 'PGtext where pgtype = text
1165−instance (KnownNat n, 1 <= n)
1166− => PGTyped ('PGchar n) where pgtype = char (Proxy @n)
1167−instance (KnownNat n, 1 <= n)
1168− => PGTyped ('PGvarchar n) where pgtype = varchar (Proxy @n)
1169−instance PGTyped 'PGbytea where pgtype = bytea
1170−instance PGTyped 'PGtimestamp where pgtype = timestamp
1171−instance PGTyped 'PGtimestamptz where pgtype = timestampWithTimeZone
1172−instance PGTyped 'PGdate where pgtype = date
1173−instance PGTyped 'PGtime where pgtype = time
1174−instance PGTyped 'PGtimetz where pgtype = timeWithTimeZone
1175−instance PGTyped 'PGinterval where pgtype = interval
1176−instance PGTyped 'PGuuid where pgtype = uuid
1177−instance PGTyped 'PGjson where pgtype = json
1178−instance PGTyped 'PGjsonb where pgtype = jsonb
1180−{-----------------------------------------
1182−-----------------------------------------}
1185− (schema0 :: TablesType)
1186− (schema1 :: TablesType)
1187− = UnsafeDefinition { renderDefinition :: ByteString }
1188− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1190−instance Category Definition where
1191− id = UnsafeDefinition ";"
1192− ddl1 . ddl0 = UnsafeDefinition $
1193− renderDefinition ddl0 <+> renderDefinition ddl1
1195−newtype Manipulation
1196− (schema :: TablesType)
1197− (params :: [ColumnType])
1198− (columns :: ColumnsType)
1199− = UnsafeManipulation { renderManipulation :: ByteString }
1200− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1203− (schema :: TablesType)
1204− (params :: [ColumnType])
1205− (columns :: ColumnsType)
1206− = UnsafeQuery { renderQuery :: ByteString }
1207− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1210− :: Query schema params columns
1211− -> Manipulation schema params columns
1212−queryStatement q = UnsafeManipulation $ renderQuery q <> ";"
1214−union, unionAll, intersect, intersectAll, except, exceptAll
1215− :: Query schema params columns
1216− -> Query schema params columns
1217− -> Query schema params columns
1218−q1 `union` q2 = UnsafeQuery $
1219− parenthesized (renderQuery q1)
1221− <+> parenthesized (renderQuery q2)
1222−q1 `unionAll` q2 = UnsafeQuery $
1223− parenthesized (renderQuery q1)
1224− <+> "UNION" <+> "ALL"
1225− <+> parenthesized (renderQuery q2)
1226−q1 `intersect` q2 = UnsafeQuery $
1227− parenthesized (renderQuery q1)
1229− <+> parenthesized (renderQuery q2)
1230−q1 `intersectAll` q2 = UnsafeQuery $
1231− parenthesized (renderQuery q1)
1232− <+> "INTERSECT" <+> "ALL"
1233− <+> parenthesized (renderQuery q2)
1234−q1 `except` q2 = UnsafeQuery $
1235− parenthesized (renderQuery q1)
1237− <+> parenthesized (renderQuery q2)
1238−q1 `exceptAll` q2 = UnsafeQuery $
1239− parenthesized (renderQuery q1)
1240− <+> "EXCEPT" <+> "ALL"
1241− <+> parenthesized (renderQuery q2)
1243−{-----------------------------------------
1245−-----------------------------------------}
1249− => NP (Aliased (Expression params tables grouping)) (column ': columns)
1250− -> TableExpression params schema tables grouping
1251− -> Query schema params (column ': columns)
1252−select list tabs = UnsafeQuery $
1254− <+> renderCommaSeparated (renderAliased renderExpression) list
1255− <+> renderTableExpression tabs
1259− => NP (Aliased (Expression params tables grouping)) (column ': columns)
1260− -> TableExpression params schema tables grouping
1261− -> Query schema params (column ': columns)
1262−selectDistinct list tabs = UnsafeQuery $
1264− <+> renderCommaSeparated (renderAliased renderExpression) list
1265− <+> renderTableExpression tabs
1268− :: tables ~ '[table ::: columns]
1269− => TableExpression params schema tables 'Ungrouped
1270− -> Query schema params columns
1271−selectStar tabs = UnsafeQuery $ "SELECT" <+> "*" <+> renderTableExpression tabs
1274− :: tables ~ '[table ::: columns]
1275− => TableExpression params schema tables 'Ungrouped
1276− -> Query schema params columns
1277−selectDistinctStar tabs = UnsafeQuery $
1278− "SELECT DISTINCT" <+> "*" <+> renderTableExpression tabs
1281− :: HasTable table tables columns
1283− -> TableExpression params schema tables 'Ungrouped
1284− -> Query schema params columns
1285−selectDotStar table tables = UnsafeQuery $
1286− "SELECT" <+> renderAlias table <> ".*" <+> renderTableExpression tables
1288−selectDistinctDotStar
1289− :: HasTable table tables columns
1291− -> TableExpression params schema tables 'Ungrouped
1292− -> Query schema params columns
1293−selectDistinctDotStar table tables = UnsafeQuery $
1294− "SELECT DISTINCT" <+> renderAlias table <> ".*"
1295− <+> renderTableExpression tables
1297−{-----------------------------------------
1299−-----------------------------------------}
1302− :: (KnownSymbol table, SListI columns)
1304− -> NP (Aliased TypeExpression) (column ': columns)
1305− -> [TableConstraint schema (column ': columns)]
1306− -> Definition schema (Create table (column ': columns) schema)
1307−createTable table columns constraints = UnsafeDefinition $
1308− "CREATE TABLE" <+> renderAlias table
1310− ( renderCommaSeparated renderColumnDef columns
1311− <> renderConstraints constraints )
1314− renderColumnDef :: Aliased TypeExpression x -> ByteString
1315− renderColumnDef (ty `As` column) =
1316− renderAlias column <+> renderTypeExpression ty
1317− renderConstraints :: [TableConstraint schema columns] -> ByteString
1318− renderConstraints = \case
1320− _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
1322−newtype TableConstraint
1323− (schema :: TablesType)
1324− (columns :: ColumnsType)
1325− = UnsafeTableConstraint { renderTableConstraint :: ByteString }
1326− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1329− :: Condition '[] '[table ::: columns] 'Ungrouped
1330− -> TableConstraint schema columns
1331−check condition = UnsafeTableConstraint $
1332− "CHECK" <+> parenthesized (renderExpression condition)
1334−renderColumn :: Column columns columnty -> ByteString
1335−renderColumn (Column column) = renderAlias column
1338− :: SListI subcolumns
1339− => NP (Column columns) subcolumns
1340− -> TableConstraint schema columns
1341−unique columns = UnsafeTableConstraint $
1342− "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
1345− :: (SListI subcolumns, AllNotNull subcolumns)
1346− => NP (Column columns) subcolumns
1347− -> TableConstraint schema columns
1348−primaryKey columns = UnsafeTableConstraint $
1349− "PRIMARY KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1352− :: ( HasTable reftable schema refcolumns
1353− , SameTypes subcolumns refsubcolumns
1354− , NotAllNull subcolumns
1355− , SListI subcolumns
1356− , SListI refsubcolumns)
1357− => NP (Column columns) subcolumns
1359− -> NP (Column refcolumns) refsubcolumns
1362− -> TableConstraint schema columns
1363−foreignKey columns reftable refcolumns onDelete onUpdate =
1364− UnsafeTableConstraint $
1365− "FOREIGN KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1366− <+> "REFERENCES" <+> renderAlias reftable
1367− <+> parenthesized (renderCommaSeparated renderColumn refcolumns)
1368− <+> renderOnDelete onDelete
1369− <+> renderOnUpdate onUpdate
1375− deriving (GHC.Generic,Show,Eq,Ord,Data)
1376−instance NFData OnDelete
1378−renderOnDelete :: OnDelete -> ByteString
1379−renderOnDelete = \case
1380− OnDeleteNoAction -> "ON DELETE NO ACTION"
1381− OnDeleteRestrict -> "ON DELETE RESTRICT"
1382− OnDeleteCascade -> "ON DELETE CASCADE"
1388− deriving (GHC.Generic,Show,Eq,Ord,Data)
1389−instance NFData OnUpdate
1391−renderOnUpdate :: OnUpdate -> ByteString
1392−renderOnUpdate = \case
1393− OnUpdateNoAction -> "ON UPDATE NO ACTION"
1394− OnUpdateRestrict -> "ON UPDATE RESTRICT"
1395− OnUpdateCascade -> "ON UPDATE CASCADE"
1397−{-----------------------------------------
1399−-----------------------------------------}
1402− :: KnownSymbol table
1404− -> Definition schema (Drop table schema)
1405−dropTable table = UnsafeDefinition $ "DROP TABLE" <+> renderAlias table <> ";"
1407−{-----------------------------------------
1409−-----------------------------------------}
1412− :: HasTable table schema columns0
1414− -> AlterTable columns0 columns1
1415− -> Definition schema (Alter table schema columns1)
1416−alterTable table alteration = UnsafeDefinition $
1418− <+> renderAlias table
1419− <+> renderAlterTable alteration
1423− :: (KnownSymbol table0, KnownSymbol table1)
1426− -> Definition schema (Rename table0 table1 schema)
1427−alterTableRename table0 table1 = UnsafeDefinition $
1428− "ALTER TABLE" <+> renderAlias table0
1429− <+> "RENAME TO" <+> renderAlias table1 <> ";"
1432− (columns0 :: [(Symbol, ColumnType)])
1433− (columns1 :: [(Symbol, ColumnType)]) =
1434− UnsafeAlterTable {renderAlterTable :: ByteString}
1435− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1438− :: KnownSymbol column
1440− -> TypeExpression ('Optional ty)
1441− -> AlterTable columns (Create column ('Optional ty) columns)
1442−addColumnDefault column ty = UnsafeAlterTable $
1443− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1446− :: KnownSymbol column
1448− -> TypeExpression ('Required ('Null ty))
1449− -> AlterTable columns (Create column ('Required ('Null ty)) columns)
1450−addColumnNull column ty = UnsafeAlterTable $
1451− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1454− :: KnownSymbol column
1456− -> AlterTable columns (Drop column columns)
1457−dropColumn column = UnsafeAlterTable $
1458− "DROP COLUMN" <+> renderAlias column
1461− :: (KnownSymbol column0, KnownSymbol column1)
1464− -> AlterTable columns (Rename column0 column1 columns)
1465−renameColumn column0 column1 = UnsafeAlterTable $
1466− "RENAME COLUMN" <+> renderAlias column0 <+> "TO" <+> renderAlias column1
1469− :: (KnownSymbol column, HasColumn column columns ty0)
1471− -> AlterColumn ty0 ty1
1472− -> AlterTable columns (Alter column columns ty1)
1473−alterColumn column alteration = UnsafeAlterTable $
1474− "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
1476−newtype AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
1477− UnsafeAlterColumn {renderAlterColumn :: ByteString}
1478− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1481− :: Expression '[] '[] 'Ungrouped ('Required ty)
1482− -> AlterColumn ('Required ty) ('Optional ty)
1483−setDefault expression = UnsafeAlterColumn $
1484− "SET DEFAULT" <+> renderExpression expression
1486−dropDefault :: AlterColumn ('Optional ty) ('Required ty)
1487−dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
1490− :: AlterColumn (optionality ('Null ty)) (optionality ('NotNull ty))
1491−setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
1494− :: AlterColumn (optionality ('NotNull ty)) (optionality ('Null ty))
1495−dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
1498− :: TypeExpression (optionality (nullity ty1))
1499− -> AlterColumn (optionality (nullity ty0)) (optionality (nullity ty1))
1500−alterType ty = UnsafeAlterColumn $ "TYPE " <> renderTypeExpression ty
1502−{-----------------------------------------
1504−-----------------------------------------}
1507− :: (SListI columns, SListI results, HasTable table schema columns)
1509− -> ValuesClause schema params columns
1510− -> ConflictClause params columns
1511− -> ReturningClause params columns results
1512− -> Manipulation schema params results
1513−insertInto table insert conflict returning = UnsafeManipulation $
1514− "INSERT" <+> "INTO" <+> renderAlias table
1515− <+> renderValuesClause insert
1516− <> renderConflictClause conflict
1517− <> renderReturningClause returning
1520− (schema :: TablesType)
1521− (params :: [ColumnType])
1522− (columns :: ColumnsType)
1524− (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1525− [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
1526− | ValuesQuery (Query schema params columns)
1530− => ValuesClause schema params columns
1532−renderValuesClause = \case
1534− parenthesized (renderCommaSeparated renderAliasPart row)
1537− (parenthesized . renderCommaSeparated renderValuePart <$> row:rows)
1539− renderAliasPart, renderValuePart
1540− :: Aliased (Expression params '[] 'Ungrouped) ty -> ByteString
1541− renderAliasPart (_ `As` name) = renderAlias name
1542− renderValuePart (value `As` _) = renderExpression value
1543− ValuesQuery q -> renderQuery q
1545−data ReturningClause
1546− (params :: [ColumnType])
1547− (columns :: ColumnsType)
1548− (results :: ColumnsType)
1550− ReturningStar :: ReturningClause params columns columns
1553− (Aliased (Expression params '[table ::: columns] 'Ungrouped))
1555− -> ReturningClause params columns results
1557−renderReturningClause
1559− => ReturningClause params columns results
1561−renderReturningClause = \case
1562− ReturningStar -> " RETURNING *;"
1563− Returning Nil -> ";"
1564− Returning results -> " RETURNING"
1565− <+> renderCommaSeparated (renderAliased renderExpression) results <> ";"
1567−data ConflictClause params columns where
1568− Conflict :: ConflictClause params columns
1569− OnConflictDoNothing :: ConflictClause params columns
1571− :: NP (Aliased (UpdateExpression params columns)) columns
1572− -> Maybe (Condition params '[table ::: columns] 'Ungrouped)
1573− -> ConflictClause params columns
1575−renderConflictClause
1577− => ConflictClause params columns
1579−renderConflictClause = \case
1581− OnConflictDoNothing -> " ON CONFLICT DO NOTHING"
1582− OnConflictDoUpdate updates whMaybe
1583− -> " ON CONFLICT DO UPDATE SET"
1584− <+> renderCommaSeparatedMaybe renderUpdateExpression updates
1587− Just wh -> " WHERE" <+> renderExpression wh
1589−{-----------------------------------------
1591−-----------------------------------------}
1593−data UpdateExpression params columns ty
1595− | Set (forall table. Expression params '[table ::: columns] 'Ungrouped ty)
1596−deriving instance Show (UpdateExpression params columns ty)
1597−deriving instance Eq (UpdateExpression params columns ty)
1598−deriving instance Ord (UpdateExpression params columns ty)
1600−renderUpdateExpression
1601− :: Aliased (UpdateExpression params columns) column
1602− -> Maybe ByteString
1603−renderUpdateExpression = \case
1604− Same `As` _ -> Nothing
1605− Set expression `As` column -> Just $
1606− renderAlias column <+> "=" <+> renderExpression expression
1609− :: (HasTable table schema columns, SListI columns, SListI results)
1611− -> NP (Aliased (UpdateExpression params columns)) columns
1612− -> Condition params '[tab ::: columns] 'Ungrouped
1613− -> ReturningClause params columns results
1614− -> Manipulation schema params results
1615−update table columns wh returning = UnsafeManipulation $
1617− <+> renderAlias table
1619− <+> renderCommaSeparatedMaybe renderUpdateExpression columns
1620− <+> "WHERE" <+> renderExpression wh
1621− <> renderReturningClause returning
1623−{-----------------------------------------
1625−-----------------------------------------}
1628− :: HasTable table schema columns
1630− -> Condition params '[table ::: columns] 'Ungrouped
1631− -> Manipulation schema params '[]
1632−deleteFrom table wh = UnsafeManipulation $
1633− "DELETE FROM" <+> renderAlias table
1634− <+> "WHERE" <+> renderExpression wh <> ";"
1636−{-----------------------------------------
1637−render helper functions
1638−-----------------------------------------}
1640−parenthesized :: ByteString -> ByteString
1641−parenthesized str = "(" <> str <> ")"
1643−(<+>) :: ByteString -> ByteString -> ByteString
1644−str1 <+> str2 = str1 <> " " <> str2
1646−commaSeparated :: [ByteString] -> ByteString
1647−commaSeparated = ByteString.intercalate ", "
1649−renderCommaSeparated
1651− => (forall x. expression x -> ByteString)
1652− -> NP expression xs -> ByteString
1653−renderCommaSeparated render
1656− . hmap (K . render)
1658−renderCommaSeparatedMaybe
1660− => (forall x. expression x -> Maybe ByteString)
1661− -> NP expression xs -> ByteString
1662−renderCommaSeparatedMaybe render
1666− . hmap (K . render)
1668−renderNat :: KnownNat n => proxy n -> ByteString
1669−renderNat (_ :: proxy n) = fromString (show (natVal' (proxy# :: Proxy# n)))