@@ -23,13 +23,14 @@
23 , UndecidableInstances
23 , UndecidableInstances
26−module Squeal.PostgreSQL.Statement
26+module Squeal.PostgreSQL.Expression
27 ( -- * Column Expressions
27 ( -- * Column Expressions
28 Expression (UnsafeExpression, renderExpression)
28 Expression (UnsafeExpression, renderExpression)
29 , HasParameter (param)
29 , HasParameter (param)
30 , HasColumn (getColumn)
30 , HasColumn (getColumn)
31 , Column (Column)
31 , Column (Column)
32 , renderColumn
32 , renderColumn
33+ , GroupedBy (getGroup1, getGroup2)
33 -- ** Default
34 -- ** Default
@@ -99,28 +100,6 @@ module Squeal.PostgreSQL.Statement
99 -- * Tables
100 -- * Tables
100 , Table (UnsafeTable, renderTable)
101 , Table (UnsafeTable, renderTable)
101 , HasTable (getTable)
102 , 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
124 -- * TypeExpression
103 -- * TypeExpression
125 , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
104 , TypeExpression (UnsafeTypeExpression, renderTypeExpression)
126 , PGTyped (pgtype)
105 , PGTyped (pgtype)
@@ -160,62 +139,6 @@ module Squeal.PostgreSQL.Statement
161 , notNull
140 , notNull
162 , default_
141 , default_
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
219 -- * Re-export
142 -- * Re-export
221 , NP ((:*), Nil)
144 , NP ((:*), Nil)
@@ -225,22 +148,18 @@ import Control.Category
225import Control.DeepSeq
148import Control.DeepSeq
226import Data.ByteString (ByteString)
149import Data.ByteString (ByteString)
227import Data.Data (Data)
150import Data.Data (Data)
229import Data.Function ((&))
151import Data.Function ((&))
231import Data.Monoid hiding (All)
152import Data.Monoid hiding (All)
232import Data.Ratio
153import Data.Ratio
233import Data.String
154import Data.String
235import Generics.SOP hiding (from)
155import Generics.SOP hiding (from)
237import GHC.OverloadedLabels
156import GHC.OverloadedLabels
238import GHC.TypeLits
157import GHC.TypeLits
239import Prelude hiding (id, (.))
158import Prelude hiding (id, (.))
241import qualified GHC.Generics as GHC
160import qualified GHC.Generics as GHC
242−import qualified Data.ByteString as ByteString
162+import Squeal.PostgreSQL.Prettyprint
244import Squeal.PostgreSQL.Schema
163import Squeal.PostgreSQL.Schema
246{-----------------------------------------
165{-----------------------------------------
@@ -322,6 +241,46 @@ deriving instance Show (Column columns columnty)
322deriving instance Eq (Column columns columnty)
241deriving instance Eq (Column columns columnty)
323deriving instance Ord (Column columns columnty)
242deriving instance Ord (Column columns columnty)
244+renderColumn :: Column columns columnty -> ByteString
245+renderColumn (Column column) = renderAlias column
247+class (KnownSymbol table, KnownSymbol column)
248+ => GroupedBy table column bys where
250+ :: (HasUnique table tables columns, HasColumn column columns ty)
252+ -> Expression params tables ('Grouped bys) ty
253+ getGroup1 column = UnsafeExpression $ renderAlias column
255+ :: (HasTable table tables columns, HasColumn column columns ty)
258+ -> Expression params tables ('Grouped bys) ty
259+ getGroup2 table column = UnsafeExpression $
260+ renderAlias table <> "." <> renderAlias column
261+instance {-# OVERLAPPING #-} (KnownSymbol table, KnownSymbol column)
262+ => GroupedBy table column ('(table,column) ': bys)
263+instance {-# OVERLAPPABLE #-}
265+ , KnownSymbol column
266+ , GroupedBy table column bys
267+ ) => GroupedBy table column (tabcol ': bys)
270+ ( HasUnique table tables columns
271+ , HasColumn column columns ty
272+ , GroupedBy table column bys
274+ (Expression params tables ('Grouped bys) ty) where
275+ fromLabel = getGroup1 (Alias @column)
278+ ( HasTable table tables columns
279+ , HasColumn column columns ty
280+ , GroupedBy table column bys
281+ ) => IsTableColumn table column
282+ (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
325-- | >>> renderExpression def
284-- | >>> renderExpression def
326-- "DEFAULT"
285-- "DEFAULT"
327def :: Expression params '[] 'Ungrouped ('Optional (nullity ty))
286def :: Expression params '[] 'Ungrouped ('Optional (nullity ty))
@@ -667,138 +626,10 @@ localTimestamp
667 :: Expression params tables grouping ('Required (nullity 'PGtimestamp))
626 :: Expression params tables grouping ('Required (nullity 'PGtimestamp))
668localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
627localTimestamp = UnsafeExpression "LOCALTIMESTAMP"
670−data SortExpression params tables grouping where
672− :: Expression params tables grouping ('Required ('NotNull ty))
673− -> SortExpression params tables grouping
675− :: Expression params tables grouping ('Required ('NotNull 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
684− :: Expression params tables grouping ('Required ('Null ty))
685− -> SortExpression params tables grouping
687− :: Expression params tables grouping ('Required ('Null ty))
688− -> SortExpression params tables grouping
689−deriving instance Show (SortExpression params tables grouping)
691−renderSortExpression :: SortExpression params tables grouping -> ByteString
692−renderSortExpression = \case
693− Asc expression -> renderExpression expression <+> "ASC"
694− Desc expression -> renderExpression expression <+> "DESC"
695− AscNullsFirst expression -> renderExpression expression
696− <+> "ASC NULLS FIRST"
697− DescNullsFirst expression -> renderExpression expression
698− <+> "DESC NULLS FIRST"
699− AscNullsLast expression -> renderExpression expression <+> "ASC NULLS LAST"
700− DescNullsLast expression -> renderExpression expression <+> "DESC NULLS LAST"
703{-----------------------------------------
629{-----------------------------------------
704aggregation
630aggregation
705-----------------------------------------}
631-----------------------------------------}
708− (tables :: TablesType)
709− (by :: (Symbol,Symbol)) where
711− :: (HasUnique table tables columns, HasColumn column columns ty)
713− -> By tables '(table, column)
715− :: (HasTable table tables columns, HasColumn column columns ty)
716− => (Alias table, Alias column)
717− -> By tables '(table, column)
718−deriving instance Show (By tables by)
719−deriving instance Eq (By tables by)
720−deriving instance Ord (By tables by)
722−renderBy :: By tables tabcolty -> ByteString
724− By column -> renderAlias column
725− By2 (table, column) -> renderAlias table <> "." <> renderAlias column
727−data GroupByClause tables grouping where
728− NoGroups :: GroupByClause tables 'Ungrouped
731− => NP (By tables) bys
732− -> GroupByClause tables ('Grouped bys)
734−renderGroupByClause :: GroupByClause tables grouping -> ByteString
735−renderGroupByClause = \case
738− Group bys -> " GROUP BY" <+> renderCommaSeparated renderBy bys
740−data HavingClause params tables grouping where
741− NoHaving :: HavingClause params tables 'Ungrouped
743− :: [Condition params tables ('Grouped bys)]
744− -> HavingClause params tables ('Grouped bys)
745−deriving instance Show (HavingClause params tables grouping)
746−deriving instance Eq (HavingClause params tables grouping)
747−deriving instance Ord (HavingClause params tables grouping)
749−renderHavingClause :: HavingClause params tables grouping -> ByteString
750−renderHavingClause = \case
753− Having conditions ->
754− " HAVING" <+> commaSeparated (renderExpression <$> conditions)
756−class (KnownSymbol table, KnownSymbol column)
757− => GroupedBy table column bys where
759− :: (HasUnique table tables columns, HasColumn column columns ty)
761− -> Expression params tables ('Grouped bys) ty
762− getGroup1 column = UnsafeExpression $ renderAlias column
764− :: (HasTable table tables columns, HasColumn column columns ty)
767− -> Expression params tables ('Grouped bys) ty
768− getGroup2 table column = UnsafeExpression $
769− renderAlias table <> "." <> renderAlias column
770−instance {-# OVERLAPPING #-} (KnownSymbol table, KnownSymbol column)
771− => GroupedBy table column ('(table,column) ': bys)
772−instance {-# OVERLAPPABLE #-}
774− , KnownSymbol column
775− , GroupedBy table column bys
776− ) => GroupedBy table column (tabcol ': bys)
779− ( HasUnique table tables columns
780− , HasColumn column columns ty
781− , GroupedBy table column bys
783− (Expression params tables ('Grouped bys) ty) where
784− fromLabel = getGroup1 (Alias @column)
787− ( HasTable table tables columns
788− , HasColumn column columns ty
789− , GroupedBy table column bys
790− ) => IsTableColumn table column
791− (Expression params tables ('Grouped bys) ty) where (!) = getGroup2
793−-- data AllOrDistinct = All | Distinct
794−-- deriving (GHC.Generic,Show,Eq,Ord,Data)
795−-- instance NFData AllOrDistinct
797−-- renderAllOrDistinct :: AllOrDistinct -> ByteString
798−-- renderAllOrDistinct = \case
800−-- Distinct -> "DISTINCT"
802unsafeAggregate, unsafeAggregateDistinct
633unsafeAggregate, unsafeAggregateDistinct
803 :: ByteString
634 :: ByteString
804 -> Expression params tables 'Ungrouped ('Required xty)
635 -> Expression params tables 'Ungrouped ('Required xty)
@@ -913,149 +744,6 @@ instance HasTable table schema columns
913 => IsLabel table (Table schema columns) where
744 => IsLabel table (Table schema columns) where
914 fromLabel = getTable (Alias @table)
745 fromLabel = getTable (Alias @table)
916−data FromClause params schema tables where
918− :: Aliased (Table schema) table
919− -> FromClause params schema '[table]
921− :: Aliased (Query schema params) table
922− -> FromClause params schema '[table]
924− :: FromClause params schema right
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 right)
933− :: FromClause params schema right
934− -> Condition params (Join left right) 'Ungrouped
935− -> FromClause params schema left
936− -> FromClause params schema (Join left (NullifyTables right))
938− :: FromClause params schema right
939− -> Condition params (Join left right) 'Ungrouped
940− -> FromClause params schema left
941− -> FromClause params schema (Join (NullifyTables left) right)
943− :: FromClause params schema right
944− -> Condition params (Join left right) 'Ungrouped
945− -> FromClause params schema left
946− -> FromClause params schema
947− (Join (NullifyTables left) (NullifyTables right))
949−renderFromClause :: FromClause params schema tables -> ByteString
950−renderFromClause = \case
951− Table table -> renderAliased renderTable table
952− Subquery selection -> renderAliased (parenthesized . renderQuery) selection
953− CrossJoin right left ->
954− renderFromClause left <+> "CROSS JOIN" <+> renderFromClause right
955− InnerJoin right on left -> renderJoin "INNER JOIN" right on left
956− LeftOuterJoin right on left -> renderJoin "LEFT OUTER JOIN" right on left
957− RightOuterJoin right on left -> renderJoin "RIGHT OUTER JOIN" right on left
958− FullOuterJoin right on left -> renderJoin "FULL OUTER JOIN" right on left
960− renderJoin op right on left =
961− renderFromClause left <+> op <+> renderFromClause right
962− <+> "ON" <+> renderExpression on
965− (params :: [ColumnType])
966− (schema :: TablesType)
967− (tables :: TablesType)
968− (grouping :: Grouping)
970− { fromClause :: FromClause params schema tables
971− , whereClause :: [Condition params tables 'Ungrouped]
972− , groupByClause :: GroupByClause tables grouping
973− , havingClause :: HavingClause params tables grouping
974− , orderByClause :: [SortExpression params tables grouping]
975− , limitClause :: [Word64]
976− , offsetClause :: [Word64]
979−renderTableExpression
980− :: TableExpression params schema tables grouping
982−renderTableExpression
983− (TableExpression tables whs' grps' hvs' srts' lims' offs') = mconcat
984− [ "FROM ", renderFromClause tables
986− , renderGroupByClause grps'
987− , renderHavingClause hvs'
989− , renderLimits lims'
990− , renderOffsets offs'
993− renderWheres = \case
995− wh:[] -> " WHERE" <+> renderExpression wh
996− wh:whs -> " WHERE" <+> renderExpression (foldr (&&*) wh whs)
1000− <+> commaSeparated (renderSortExpression <$> srts)
1001− renderLimits = \case
1003− lims -> " LIMIT" <+> fromString (show (minimum lims))
1004− renderOffsets = \case
1006− offs -> " OFFSET" <+> fromString (show (sum offs))
1009− :: FromClause params schema tables
1010− -> TableExpression params schema tables 'Ungrouped
1011−from tables = TableExpression tables [] NoGroups NoHaving [] [] []
1014− :: Condition params tables 'Ungrouped
1015− -> TableExpression params schema tables grouping
1016− -> TableExpression params schema tables grouping
1017−where_ wh tables = tables {whereClause = wh : whereClause tables}
1021− => NP (By tables) bys
1022− -> TableExpression params schema tables 'Ungrouped
1023− -> TableExpression params schema tables ('Grouped bys)
1024−group bys tables = TableExpression
1025− { fromClause = fromClause tables
1026− , whereClause = whereClause tables
1027− , groupByClause = Group bys
1028− , havingClause = Having []
1029− , orderByClause = []
1030− , limitClause = limitClause tables
1031− , offsetClause = offsetClause tables
1035− :: Condition params tables ('Grouped bys)
1036− -> TableExpression params schema tables ('Grouped bys)
1037− -> TableExpression params schema tables ('Grouped bys)
1038−having hv tables = tables
1039− { havingClause = case havingClause tables of Having hvs -> Having (hv:hvs) }
1042− :: [SortExpression params tables grouping]
1043− -> TableExpression params schema tables grouping
1044− -> TableExpression params schema tables grouping
1045−orderBy srts tables = tables {orderByClause = orderByClause tables ++ srts}
1049− -> TableExpression params schema tables grouping
1050− -> TableExpression params schema tables grouping
1051−limit lim tables = tables {limitClause = lim : limitClause tables}
1055− -> TableExpression params schema tables grouping
1056− -> TableExpression params schema tables grouping
1057−offset off tables = tables {offsetClause = off : offsetClause tables}
1059{-----------------------------------------
747{-----------------------------------------
1060type expressions
748type expressions
1061-----------------------------------------}
749-----------------------------------------}
@@ -1181,494 +869,3 @@ instance PGTyped 'PGinterval where pgtype = interval
1181instance PGTyped 'PGuuid where pgtype = uuid
869instance PGTyped 'PGuuid where pgtype = uuid
1182instance PGTyped 'PGjson where pgtype = json
870instance PGTyped 'PGjson where pgtype = json
1183instance PGTyped 'PGjsonb where pgtype = jsonb
871instance PGTyped 'PGjsonb where pgtype = jsonb
1185−{-----------------------------------------
1187−-----------------------------------------}
1190− (schema0 :: TablesType)
1191− (schema1 :: TablesType)
1192− = UnsafeDefinition { renderDefinition :: ByteString }
1193− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1195−instance Category Definition where
1196− id = UnsafeDefinition ";"
1197− ddl1 . ddl0 = UnsafeDefinition $
1198− renderDefinition ddl0 <+> renderDefinition ddl1
1200−newtype Manipulation
1201− (schema :: TablesType)
1202− (params :: [ColumnType])
1203− (columns :: ColumnsType)
1204− = UnsafeManipulation { renderManipulation :: ByteString }
1205− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1208− (schema :: TablesType)
1209− (params :: [ColumnType])
1210− (columns :: ColumnsType)
1211− = UnsafeQuery { renderQuery :: ByteString }
1212− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1215− :: Query schema params columns
1216− -> Manipulation schema params columns
1217−queryStatement q = UnsafeManipulation $ renderQuery q <> ";"
1219−union, unionAll, intersect, intersectAll, except, exceptAll
1220− :: Query schema params columns
1221− -> Query schema params columns
1222− -> Query schema params columns
1223−q1 `union` q2 = UnsafeQuery $
1224− parenthesized (renderQuery q1)
1226− <+> parenthesized (renderQuery q2)
1227−q1 `unionAll` q2 = UnsafeQuery $
1228− parenthesized (renderQuery q1)
1229− <+> "UNION" <+> "ALL"
1230− <+> parenthesized (renderQuery q2)
1231−q1 `intersect` q2 = UnsafeQuery $
1232− parenthesized (renderQuery q1)
1234− <+> parenthesized (renderQuery q2)
1235−q1 `intersectAll` q2 = UnsafeQuery $
1236− parenthesized (renderQuery q1)
1237− <+> "INTERSECT" <+> "ALL"
1238− <+> parenthesized (renderQuery q2)
1239−q1 `except` q2 = UnsafeQuery $
1240− parenthesized (renderQuery q1)
1242− <+> parenthesized (renderQuery q2)
1243−q1 `exceptAll` q2 = UnsafeQuery $
1244− parenthesized (renderQuery q1)
1245− <+> "EXCEPT" <+> "ALL"
1246− <+> parenthesized (renderQuery q2)
1248−{-----------------------------------------
1250−-----------------------------------------}
1254− => NP (Aliased (Expression params tables grouping)) (column ': columns)
1255− -> TableExpression params schema tables grouping
1256− -> Query schema params (column ': columns)
1257−select list tabs = UnsafeQuery $
1259− <+> renderCommaSeparated (renderAliased renderExpression) list
1260− <+> renderTableExpression tabs
1264− => NP (Aliased (Expression params tables grouping)) (column ': columns)
1265− -> TableExpression params schema tables grouping
1266− -> Query schema params (column ': columns)
1267−selectDistinct list tabs = UnsafeQuery $
1269− <+> renderCommaSeparated (renderAliased renderExpression) list
1270− <+> renderTableExpression tabs
1273− :: HasUnique table tables columns
1274− => TableExpression params schema tables 'Ungrouped
1275− -> Query schema params columns
1276−selectStar tabs = UnsafeQuery $ "SELECT" <+> "*" <+> renderTableExpression tabs
1279− :: HasUnique table tables columns
1280− => TableExpression params schema tables 'Ungrouped
1281− -> Query schema params columns
1282−selectDistinctStar tabs = UnsafeQuery $
1283− "SELECT DISTINCT" <+> "*" <+> renderTableExpression tabs
1286− :: HasTable table tables columns
1288− -> TableExpression params schema tables 'Ungrouped
1289− -> Query schema params columns
1290−selectDotStar table tables = UnsafeQuery $
1291− "SELECT" <+> renderAlias table <> ".*" <+> renderTableExpression tables
1293−selectDistinctDotStar
1294− :: HasTable table tables columns
1296− -> TableExpression params schema tables 'Ungrouped
1297− -> Query schema params columns
1298−selectDistinctDotStar table tables = UnsafeQuery $
1299− "SELECT DISTINCT" <+> renderAlias table <> ".*"
1300− <+> renderTableExpression tables
1302−{-----------------------------------------
1304−-----------------------------------------}
1307− :: (KnownSymbol table, SListI columns)
1309− -> NP (Aliased TypeExpression) (column ': columns)
1310− -> [TableConstraint schema (column ': columns)]
1311− -> Definition schema (Create table (column ': columns) schema)
1312−createTable table columns constraints = UnsafeDefinition $
1313− "CREATE TABLE" <+> renderAlias table
1315− ( renderCommaSeparated renderColumnDef columns
1316− <> renderConstraints constraints )
1319− renderColumnDef :: Aliased TypeExpression x -> ByteString
1320− renderColumnDef (ty `As` column) =
1321− renderAlias column <+> renderTypeExpression ty
1322− renderConstraints :: [TableConstraint schema columns] -> ByteString
1323− renderConstraints = \case
1325− _ -> ", " <> commaSeparated (renderTableConstraint <$> constraints)
1327−newtype TableConstraint
1328− (schema :: TablesType)
1329− (columns :: ColumnsType)
1330− = UnsafeTableConstraint { renderTableConstraint :: ByteString }
1331− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1334− :: Condition '[] '[table ::: columns] 'Ungrouped
1335− -> TableConstraint schema columns
1336−check condition = UnsafeTableConstraint $
1337− "CHECK" <+> parenthesized (renderExpression condition)
1339−renderColumn :: Column columns columnty -> ByteString
1340−renderColumn (Column column) = renderAlias column
1343− :: SListI subcolumns
1344− => NP (Column columns) subcolumns
1345− -> TableConstraint schema columns
1346−unique columns = UnsafeTableConstraint $
1347− "UNIQUE" <+> parenthesized (renderCommaSeparated renderColumn columns)
1350− :: (SListI subcolumns, AllNotNull subcolumns)
1351− => NP (Column columns) subcolumns
1352− -> TableConstraint schema columns
1353−primaryKey columns = UnsafeTableConstraint $
1354− "PRIMARY KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1357− :: ( HasTable reftable schema refcolumns
1358− , SameTypes subcolumns refsubcolumns
1359− , NotAllNull subcolumns
1360− , SListI subcolumns
1361− , SListI refsubcolumns)
1362− => NP (Column columns) subcolumns
1364− -> NP (Column refcolumns) refsubcolumns
1367− -> TableConstraint schema columns
1368−foreignKey columns reftable refcolumns onDelete onUpdate =
1369− UnsafeTableConstraint $
1370− "FOREIGN KEY" <+> parenthesized (renderCommaSeparated renderColumn columns)
1371− <+> "REFERENCES" <+> renderAlias reftable
1372− <+> parenthesized (renderCommaSeparated renderColumn refcolumns)
1373− <+> renderOnDelete onDelete
1374− <+> renderOnUpdate onUpdate
1380− deriving (GHC.Generic,Show,Eq,Ord,Data)
1381−instance NFData OnDelete
1383−renderOnDelete :: OnDelete -> ByteString
1384−renderOnDelete = \case
1385− OnDeleteNoAction -> "ON DELETE NO ACTION"
1386− OnDeleteRestrict -> "ON DELETE RESTRICT"
1387− OnDeleteCascade -> "ON DELETE CASCADE"
1393− deriving (GHC.Generic,Show,Eq,Ord,Data)
1394−instance NFData OnUpdate
1396−renderOnUpdate :: OnUpdate -> ByteString
1397−renderOnUpdate = \case
1398− OnUpdateNoAction -> "ON UPDATE NO ACTION"
1399− OnUpdateRestrict -> "ON UPDATE RESTRICT"
1400− OnUpdateCascade -> "ON UPDATE CASCADE"
1402−{-----------------------------------------
1404−-----------------------------------------}
1407− :: KnownSymbol table
1409− -> Definition schema (Drop table schema)
1410−dropTable table = UnsafeDefinition $ "DROP TABLE" <+> renderAlias table <> ";"
1412−{-----------------------------------------
1414−-----------------------------------------}
1417− :: HasTable table schema columns0
1419− -> AlterTable columns0 columns1
1420− -> Definition schema (Alter table schema columns1)
1421−alterTable table alteration = UnsafeDefinition $
1423− <+> renderAlias table
1424− <+> renderAlterTable alteration
1428− :: (KnownSymbol table0, KnownSymbol table1)
1431− -> Definition schema (Rename table0 table1 schema)
1432−alterTableRename table0 table1 = UnsafeDefinition $
1433− "ALTER TABLE" <+> renderAlias table0
1434− <+> "RENAME TO" <+> renderAlias table1 <> ";"
1437− (columns0 :: ColumnsType)
1438− (columns1 :: ColumnsType) =
1439− UnsafeAlterTable {renderAlterTable :: ByteString}
1440− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1443− :: KnownSymbol column
1445− -> TypeExpression ('Optional ty)
1446− -> AlterTable columns (Create column ('Optional ty) columns)
1447−addColumnDefault column ty = UnsafeAlterTable $
1448− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1451− :: KnownSymbol column
1453− -> TypeExpression ('Required ('Null ty))
1454− -> AlterTable columns (Create column ('Required ('Null ty)) columns)
1455−addColumnNull column ty = UnsafeAlterTable $
1456− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
1459− :: KnownSymbol column
1461− -> AlterTable columns (Drop column columns)
1462−dropColumn column = UnsafeAlterTable $
1463− "DROP COLUMN" <+> renderAlias column
1466− :: (KnownSymbol column0, KnownSymbol column1)
1469− -> AlterTable columns (Rename column0 column1 columns)
1470−renameColumn column0 column1 = UnsafeAlterTable $
1471− "RENAME COLUMN" <+> renderAlias column0 <+> "TO" <+> renderAlias column1
1474− :: (KnownSymbol column, HasColumn column columns ty0)
1476− -> AlterColumn ty0 ty1
1477− -> AlterTable columns (Alter column columns ty1)
1478−alterColumn column alteration = UnsafeAlterTable $
1479− "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
1481−newtype AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
1482− UnsafeAlterColumn {renderAlterColumn :: ByteString}
1483− deriving (GHC.Generic,Show,Eq,Ord,Data,NFData)
1486− :: Expression '[] '[] 'Ungrouped ('Required ty)
1487− -> AlterColumn ('Required ty) ('Optional ty)
1488−setDefault expression = UnsafeAlterColumn $
1489− "SET DEFAULT" <+> renderExpression expression
1491−dropDefault :: AlterColumn ('Optional ty) ('Required ty)
1492−dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
1495− :: AlterColumn (optionality ('Null ty)) (optionality ('NotNull ty))
1496−setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
1499− :: AlterColumn (optionality ('NotNull ty)) (optionality ('Null ty))
1500−dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
1503− :: TypeExpression (optionality (nullity ty1))
1504− -> AlterColumn (optionality (nullity ty0)) (optionality (nullity ty1))
1505−alterType ty = UnsafeAlterColumn $ "TYPE " <> renderTypeExpression ty
1507−{-----------------------------------------
1509−-----------------------------------------}
1512− :: (SListI columns, SListI results, HasTable table schema columns)
1514− -> ValuesClause schema params columns
1515− -> ConflictClause params columns
1516− -> ReturningClause params columns results
1517− -> Manipulation schema params results
1518−insertInto table insert conflict returning = UnsafeManipulation $
1519− "INSERT" <+> "INTO" <+> renderAlias table
1520− <+> renderValuesClause insert
1521− <> renderConflictClause conflict
1522− <> renderReturningClause returning
1525− (schema :: TablesType)
1526− (params :: [ColumnType])
1527− (columns :: ColumnsType)
1529− (NP (Aliased (Expression params '[] 'Ungrouped)) columns)
1530− [NP (Aliased (Expression params '[] 'Ungrouped)) columns]
1531− | ValuesQuery (Query schema params columns)
1535− => ValuesClause schema params columns
1537−renderValuesClause = \case
1539− parenthesized (renderCommaSeparated renderAliasPart row)
1542− (parenthesized . renderCommaSeparated renderValuePart <$> row:rows)
1544− renderAliasPart, renderValuePart
1545− :: Aliased (Expression params '[] 'Ungrouped) ty -> ByteString
1546− renderAliasPart (_ `As` name) = renderAlias name
1547− renderValuePart (value `As` _) = renderExpression value
1548− ValuesQuery q -> renderQuery q
1550−data ReturningClause
1551− (params :: [ColumnType])
1552− (columns :: ColumnsType)
1553− (results :: ColumnsType)
1555− ReturningStar :: ReturningClause params columns columns
1558− (Aliased (Expression params '[table ::: columns] 'Ungrouped))
1560− -> ReturningClause params columns results
1562−renderReturningClause
1564− => ReturningClause params columns results
1566−renderReturningClause = \case
1567− ReturningStar -> " RETURNING *;"
1568− Returning Nil -> ";"
1569− Returning results -> " RETURNING"
1570− <+> renderCommaSeparated (renderAliased renderExpression) results <> ";"
1572−data ConflictClause params columns where
1573− Conflict :: ConflictClause params columns
1574− OnConflictDoNothing :: ConflictClause params columns
1576− :: NP (Aliased (UpdateExpression params columns)) columns
1577− -> Maybe (Condition params '[table ::: columns] 'Ungrouped)
1578− -> ConflictClause params columns
1580−renderConflictClause
1582− => ConflictClause params columns
1584−renderConflictClause = \case
1586− OnConflictDoNothing -> " ON CONFLICT DO NOTHING"
1587− OnConflictDoUpdate updates whMaybe
1588− -> " ON CONFLICT DO UPDATE SET"
1589− <+> renderCommaSeparatedMaybe renderUpdateExpression updates
1592− Just wh -> " WHERE" <+> renderExpression wh
1594−{-----------------------------------------
1596−-----------------------------------------}
1598−data UpdateExpression params columns ty
1600− | Set (forall table. Expression params '[table ::: columns] 'Ungrouped ty)
1601−deriving instance Show (UpdateExpression params columns ty)
1602−deriving instance Eq (UpdateExpression params columns ty)
1603−deriving instance Ord (UpdateExpression params columns ty)
1605−renderUpdateExpression
1606− :: Aliased (UpdateExpression params columns) column
1607− -> Maybe ByteString
1608−renderUpdateExpression = \case
1609− Same `As` _ -> Nothing
1610− Set expression `As` column -> Just $
1611− renderAlias column <+> "=" <+> renderExpression expression
1614− :: (HasTable table schema columns, SListI columns, SListI results)
1616− -> NP (Aliased (UpdateExpression params columns)) columns
1617− -> Condition params '[tab ::: columns] 'Ungrouped
1618− -> ReturningClause params columns results
1619− -> Manipulation schema params results
1620−update table columns wh returning = UnsafeManipulation $
1622− <+> renderAlias table
1624− <+> renderCommaSeparatedMaybe renderUpdateExpression columns
1625− <+> "WHERE" <+> renderExpression wh
1626− <> renderReturningClause returning
1628−{-----------------------------------------
1630−-----------------------------------------}
1633− :: HasTable table schema columns
1635− -> Condition params '[table ::: columns] 'Ungrouped
1636− -> Manipulation schema params '[]
1637−deleteFrom table wh = UnsafeManipulation $
1638− "DELETE FROM" <+> renderAlias table
1639− <+> "WHERE" <+> renderExpression wh <> ";"
1641−{-----------------------------------------
1642−render helper functions
1643−-----------------------------------------}
1645−parenthesized :: ByteString -> ByteString
1646−parenthesized str = "(" <> str <> ")"
1648−(<+>) :: ByteString -> ByteString -> ByteString
1649−str1 <+> str2 = str1 <> " " <> str2
1651−commaSeparated :: [ByteString] -> ByteString
1652−commaSeparated = ByteString.intercalate ", "
1654−renderCommaSeparated
1656− => (forall x. expression x -> ByteString)
1657− -> NP expression xs -> ByteString
1658−renderCommaSeparated render
1661− . hmap (K . render)
1663−renderCommaSeparatedMaybe
1665− => (forall x. expression x -> Maybe ByteString)
1666− -> NP expression xs -> ByteString
1667−renderCommaSeparatedMaybe render
1671− . hmap (K . render)
1673−renderNat :: KnownNat n => proxy n -> ByteString
1674−renderNat (_ :: proxy n) = fromString (show (natVal' (proxy# :: Proxy# n)))