@@ -50,6 +50,8 @@ module Squeal.PostgreSQL.Definition
50 , createDomain
50 , createDomain
51 , createTypeRange
51 , createTypeRange
52 , createIndex
52 , createIndex
54+ , btree, hash, gist, spgist, gin, brin
53 , createFunction
55 , createFunction
54 , createOrReplaceFunction
56 , createOrReplaceFunction
55 , createSetFunction
57 , createSetFunction
@@ -70,10 +72,15 @@ module Squeal.PostgreSQL.Definition
70 , languageSqlQuery
72 , languageSqlQuery
71 -- ** Drop
73 -- ** Drop
72 , dropSchema
74 , dropSchema
73 , dropTable
76 , dropTable
74 , dropView
78 , dropView
75 , dropType
80 , dropType
76 , dropIndex
82 , dropIndex
77 , dropFunction
84 , dropFunction
78 , dropOperator
85 , dropOperator
79 -- ** Alter
86 -- ** Alter
@@ -591,12 +598,19 @@ DROP statements
591-- >>> printSQL definition
598-- >>> printSQL definition
592-- DROP SCHEMA "muh_schema";
599-- DROP SCHEMA "muh_schema";
593dropSchema
600dropSchema
594− :: Has sch schemas schema
601+ :: KnownSymbol sch
595 => Alias sch
602 => Alias sch
596 -- ^ user defined schema
603 -- ^ user defined schema
597 -> Definition schemas (Drop sch schemas)
604 -> Definition schemas (Drop sch schemas)
598dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
605dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
610+ -- ^ user defined schema
611+ -> Definition schemas (DropIfExists sch schemas)
612+dropSchemaIfExists sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
600-- | `dropTable` removes a table from the schema.
614-- | `dropTable` removes a table from the schema.
@@ -609,11 +623,18 @@ dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
609-- DROP TABLE "muh_table";
623-- DROP TABLE "muh_table";
611 :: ( Has sch schemas schema
625 :: ( Has sch schemas schema
612− , Has tab schema ('Table table))
626+ , KnownSymbol tab )
613 => QualifiedAlias sch tab -- ^ table to remove
627 => QualifiedAlias sch tab -- ^ table to remove
614− -> Definition schemas (Alter sch (Drop tab schema) schemas)
628+ -> Definition schemas (Alter sch (DropSchemum tab 'Table schema) schemas)
615dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderSQL tab <> ";"
629dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderSQL tab <> ";"
632+ :: ( Has sch schemas schema
633+ , Has tab schema ('Table table))
634+ => QualifiedAlias sch tab -- ^ table to remove
635+ -> Definition schemas (Alter sch (DropIfExists tab schema) schemas)
636+dropTableIfExists tab = UnsafeDefinition $ "DROP TABLE IF EXISTS" <+> renderSQL tab <> ";"
617{-----------------------------------------
638{-----------------------------------------
618ALTER statements
639ALTER statements
619-----------------------------------------}
640-----------------------------------------}
@@ -940,11 +961,17 @@ createOrReplaceView alias query = UnsafeDefinition $
941-- DROP VIEW "bc";
962-- DROP VIEW "bc";
943− :: (Has sch schemas schema, Has vw schema ('View view))
964+ :: (Has sch schemas schema, KnownSymbol vw)
944 => QualifiedAlias sch vw -- ^ view to remove
965 => QualifiedAlias sch vw -- ^ view to remove
945− -> Definition schemas (Alter sch (Drop vw schema) schemas)
966+ -> Definition schemas (Alter sch (DropSchemum vw 'View schema) schemas)
946dropView vw = UnsafeDefinition $ "DROP VIEW" <+> renderSQL vw <> ";"
967dropView vw = UnsafeDefinition $ "DROP VIEW" <+> renderSQL vw <> ";"
970+ :: (Has sch schemas schema, KnownSymbol vw)
971+ => QualifiedAlias sch vw -- ^ view to remove
972+ -> Definition schemas (Alter sch (DropIfExists vw schema) schemas)
973+dropViewIfExists vw = UnsafeDefinition $ "DROP VIEW IF EXISTS" <+> renderSQL vw <> ";"
948-- | Enumerated types are created using the `createTypeEnum` command, for example
975-- | Enumerated types are created using the `createTypeEnum` command, for example
950-- >>> printSQL $ (createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy") :: Definition (Public '[]) '["public" ::: '["mood" ::: 'Typedef ('PGenum '["sad","ok","happy"])]])
977-- >>> printSQL $ (createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy") :: Definition (Public '[]) '["public" ::: '["mood" ::: 'Typedef ('PGenum '["sad","ok","happy"])]])
@@ -1100,10 +1127,10 @@ type Table = '[] :=>
1103− setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table, "ix" ::: 'Index])
1130+ setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table, "ix" ::: 'Index 'Btree])
1105 createTable #tab (nullable int `as` #a :* nullable real `as` #b) Nil >>>
1132 createTable #tab (nullable int `as` #a :* nullable real `as` #b) Nil >>>
1106− createIndex #ix #tab Btree [#a & AscNullsFirst, #b & AscNullsLast]
1133+ createIndex #ix #tab btree [#a & AscNullsFirst, #b & AscNullsLast]
1107in printSQL setup
1134in printSQL setup
1109CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
1136CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
@@ -1113,9 +1140,9 @@ createIndex
1113 :: (Has sch schemas schema, Has tab schema ('Table table), KnownSymbol ix)
1140 :: (Has sch schemas schema, Has tab schema ('Table table), KnownSymbol ix)
1114 => Alias ix
1141 => Alias ix
1115 -> QualifiedAlias sch tab
1142 -> QualifiedAlias sch tab
1116− -> IndexMethod
1143+ -> IndexMethod method
1117 -> [SortExpression '[] '[] 'Ungrouped schemas '[] '[tab ::: TableToRow table]]
1144 -> [SortExpression '[] '[] 'Ungrouped schemas '[] '[tab ::: TableToRow table]]
1118− -> Definition schemas (Alter sch (Create ix 'Index schema) schemas)
1145+ -> Definition schemas (Alter sch (Create ix ('Index method) schema) schemas)
1119createIndex ix tab method cols = UnsafeDefinition $
1146createIndex ix tab method cols = UnsafeDefinition $
1120 "CREATE" <+> "INDEX" <+> renderSQL ix <+> "ON" <+> renderSQL tab
1147 "CREATE" <+> "INDEX" <+> renderSQL ix <+> "ON" <+> renderSQL tab
1121 <+> "USING" <+> renderSQL method
1148 <+> "USING" <+> renderSQL method
@@ -1134,23 +1161,21 @@ createIndex ix tab method cols = UnsafeDefinition $
1134 DescNullsLast expression -> parenthesized (renderSQL expression)
1161 DescNullsLast expression -> parenthesized (renderSQL expression)
1135 <+> "DESC NULLS LAST"
1162 <+> "DESC NULLS LAST"
1137−data IndexMethod
1164+newtype IndexMethod ty = UnsafeIndexMethod {renderIndexMethod :: ByteString}
1144 deriving stock (Eq, Ord, Show, GHC.Generic)
1165 deriving stock (Eq, Ord, Show, GHC.Generic)
1145− deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
1146−instance RenderSQL IndexMethod where
1166+instance RenderSQL (IndexMethod ty) where renderSQL = renderIndexMethod
1147− renderSQL = \case
1167+btree :: IndexMethod 'Btree
1148− Btree -> "btree"
1168+btree = UnsafeIndexMethod "btree"
1149− Hash -> "hash"
1169+hash :: IndexMethod 'Hash
1150− Gist -> "gist"
1170+hash = UnsafeIndexMethod "hash"
1151− Spgist -> "spgist"
1171+gist :: IndexMethod 'Gist
1152− Gin -> "gin"
1172+gist = UnsafeIndexMethod "gist"
1153− Brin -> "brin"
1173+spgist :: IndexMethod 'Spgist
1174+spgist = UnsafeIndexMethod "spgist"
1175+gin :: IndexMethod 'Gin
1176+gin = UnsafeIndexMethod "gin"
1177+brin :: IndexMethod 'Brin
1178+brin = UnsafeIndexMethod "brin"
1155createFunction
1180createFunction
1156 :: ( Has sch schemas schema
1181 :: ( Has sch schemas schema
@@ -1309,15 +1334,22 @@ instance RenderSQL (FunctionDefinition schemas args ret) where
1309 renderSQL = renderFunctionDefinition
1334 renderSQL = renderFunctionDefinition
1312−-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index]) (Public '[]))
1337+-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index 'Btree]) (Public '[]))
1313-- DROP INDEX "ix";
1338-- DROP INDEX "ix";
1314dropIndex
1339dropIndex
1315− :: (Has sch schemas schema, Has ix schema 'Index)
1340+ :: (Has sch schemas schema, KnownSymbol ix)
1316 => QualifiedAlias sch ix
1341 => QualifiedAlias sch ix
1317 -- ^ name of the user defined index
1342 -- ^ name of the user defined index
1318− -> Definition schemas (Alter sch (Drop ix schema) schemas)
1343+ -> Definition schemas (Alter sch (DropSchemum ix 'Index schema) schemas)
1319dropIndex ix = UnsafeDefinition $ "DROP" <+> "INDEX" <+> renderSQL ix <> ";"
1344dropIndex ix = UnsafeDefinition $ "DROP" <+> "INDEX" <+> renderSQL ix <> ";"
1347+ :: (Has sch schemas schema, KnownSymbol ix)
1348+ => QualifiedAlias sch ix
1349+ -- ^ name of the user defined index
1350+ -> Definition schemas (Alter sch (DropSchemumIfExists ix 'Index schema) schemas)
1351+dropIndexIfExists ix = UnsafeDefinition $ "DROP INDEX IF EXISTS" <+> renderSQL ix <> ";"
1321-- | Lift `PGTyped` to a field
1353-- | Lift `PGTyped` to a field
1322class FieldTyped schemas ty where
1354class FieldTyped schemas ty where
1323 fieldtype :: Aliased (TypeExpression schemas) ty
1355 fieldtype :: Aliased (TypeExpression schemas) ty
@@ -1333,12 +1365,19 @@ instance (KnownSymbol alias, PGTyped schemas ty)
1333-- >>> printSQL (dropType #schwarma :: Definition '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]] (Public '[]))
1365-- >>> printSQL (dropType #schwarma :: Definition '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]] (Public '[]))
1334-- DROP TYPE "schwarma";
1366-- DROP TYPE "schwarma";
1336− :: (Has sch schemas schema, Has td schema ('Typedef ty))
1368+ :: (Has sch schemas schema, KnownSymbol td)
1337 => QualifiedAlias sch td
1369 => QualifiedAlias sch td
1338 -- ^ name of the user defined type
1370 -- ^ name of the user defined type
1339− -> Definition schemas (Alter sch (Drop td schema) schemas)
1371+ -> Definition schemas (Alter sch (DropSchemum td 'Typedef schema) schemas)
1340dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderSQL tydef <> ";"
1372dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderSQL tydef <> ";"
1375+ :: (Has sch schemas schema, KnownSymbol td)
1376+ => QualifiedAlias sch td
1377+ -- ^ name of the user defined type
1378+ -> Definition schemas (Alter sch (DropSchemumIfExists td 'Typedef schema) schemas)
1379+dropTypeIfExists tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderSQL tydef <> ";"
1342-- | `ColumnTypeExpression`s are used in `createTable` commands.
1381-- | `ColumnTypeExpression`s are used in `createTable` commands.
1343newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
1382newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
1344 = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }
1383 = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }