@@ -50,6 +50,8 @@ module Squeal.PostgreSQL.Definition
5050 , createDomain
5151 , createTypeRange
5252 , createIndex
53+ , IndexMethod (..)
54+ , btree, hash, gist, spgist, gin, brin
5355 , createFunction
5456 , createOrReplaceFunction
5557 , createSetFunction
@@ -70,10 +72,15 @@ module Squeal.PostgreSQL.Definition
7072 , languageSqlQuery
7173 -- ** Drop
7274 , dropSchema
75+ , dropSchemaIfExists
7376 , dropTable
77+ , dropTableIfExists
7478 , dropView
79+ , dropViewIfExists
7580 , dropType
81+ , dropTypeIfExists
7682 , dropIndex
83+ , dropIndexIfExists
7784 , dropFunction
7885 , dropOperator
7986 -- ** Alter
@@ -591,12 +598,19 @@ DROP statements
591598-- >>> printSQL definition
592599-- DROP SCHEMA "muh_schema";
593600dropSchema
594− :: Has sch schemas schema
601+ :: KnownSymbol sch
595602 => Alias sch
596603 -- ^ user defined schema
597604 -> Definition schemas (Drop sch schemas)
598605dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
599606
607+dropSchemaIfExists
608+ :: KnownSymbol sch
609+ => Alias sch
610+ -- ^ user defined schema
611+ -> Definition schemas (DropIfExists sch schemas)
612+dropSchemaIfExists sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
613+
600614-- | `dropTable` removes a table from the schema.
601615--
602616-- >>> :{
@@ -609,11 +623,18 @@ dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
609623-- DROP TABLE "muh_table";
610624dropTable
611625 :: ( Has sch schemas schema
612− , Has tab schema ('Table table))
626+ , KnownSymbol tab )
613627 => 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)
615629dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderSQL tab <> ";"
616630
631+dropTableIfExists
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 <> ";"
637+
617638{-----------------------------------------
618639ALTER statements
619640-----------------------------------------}
@@ -940,11 +961,17 @@ createOrReplaceView alias query = UnsafeDefinition $
940961-- :}
941962-- DROP VIEW "bc";
942963dropView
943− :: (Has sch schemas schema, Has vw schema ('View view))
964+ :: (Has sch schemas schema, KnownSymbol vw)
944965 => 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)
946967dropView vw = UnsafeDefinition $ "DROP VIEW" <+> renderSQL vw <> ";"
947968
969+dropViewIfExists
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 <> ";"
974+
948975-- | Enumerated types are created using the `createTypeEnum` command, for example
949976--
950977-- >>> printSQL $ (createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy") :: Definition (Public '[]) '["public" ::: '["mood" ::: 'Typedef ('PGenum '["sad","ok","happy"])]])
@@ -1100,10 +1127,10 @@ type Table = '[] :=>
11001127
11011128>>> :{
11021129let
1103− setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table, "ix" ::: 'Index])
1130+ setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table, "ix" ::: 'Index 'Btree])
11041131 setup =
11051132 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]
11071134in printSQL setup
11081135:}
11091136CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
@@ -1113,9 +1140,9 @@ createIndex
11131140 :: (Has sch schemas schema, Has tab schema ('Table table), KnownSymbol ix)
11141141 => Alias ix
11151142 -> QualifiedAlias sch tab
1116− -> IndexMethod
1143+ -> IndexMethod method
11171144 -> [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)
11191146createIndex ix tab method cols = UnsafeDefinition $
11201147 "CREATE" <+> "INDEX" <+> renderSQL ix <+> "ON" <+> renderSQL tab
11211148 <+> "USING" <+> renderSQL method
@@ -1134,23 +1161,21 @@ createIndex ix tab method cols = UnsafeDefinition $
11341161 DescNullsLast expression -> parenthesized (renderSQL expression)
11351162 <+> "DESC NULLS LAST"
11361163
1137−data IndexMethod
1138− = Btree
1139− | Hash
1140− | Gist
1141− | Spgist
1142− | Gin
1143− | Brin
1164+newtype IndexMethod ty = UnsafeIndexMethod {renderIndexMethod :: ByteString}
11441165 deriving stock (Eq, Ord, Show, GHC.Generic)
1145− deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
1146−instance RenderSQL IndexMethod where
1147− renderSQL = \case
1148− Btree -> "btree"
1149− Hash -> "hash"
1150− Gist -> "gist"
1151− Spgist -> "spgist"
1152− Gin -> "gin"
1153− Brin -> "brin"
1166+instance RenderSQL (IndexMethod ty) where renderSQL = renderIndexMethod
1167+btree :: IndexMethod 'Btree
1168+btree = UnsafeIndexMethod "btree"
1169+hash :: IndexMethod 'Hash
1170+hash = UnsafeIndexMethod "hash"
1171+gist :: IndexMethod 'Gist
1172+gist = UnsafeIndexMethod "gist"
1173+spgist :: IndexMethod 'Spgist
1174+spgist = UnsafeIndexMethod "spgist"
1175+gin :: IndexMethod 'Gin
1176+gin = UnsafeIndexMethod "gin"
1177+brin :: IndexMethod 'Brin
1178+brin = UnsafeIndexMethod "brin"
11541179
11551180createFunction
11561181 :: ( Has sch schemas schema
@@ -1309,15 +1334,22 @@ instance RenderSQL (FunctionDefinition schemas args ret) where
13091334 renderSQL = renderFunctionDefinition
13101335
13111336-- |
1312−-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index]) (Public '[]))
1337+-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index 'Btree]) (Public '[]))
13131338-- DROP INDEX "ix";
13141339dropIndex
1315− :: (Has sch schemas schema, Has ix schema 'Index)
1340+ :: (Has sch schemas schema, KnownSymbol ix)
13161341 => QualifiedAlias sch ix
13171342 -- ^ 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)
13191344dropIndex ix = UnsafeDefinition $ "DROP" <+> "INDEX" <+> renderSQL ix <> ";"
13201345
1346+dropIndexIfExists
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 <> ";"
1352+
13211353-- | Lift `PGTyped` to a field
13221354class FieldTyped schemas ty where
13231355 fieldtype :: Aliased (TypeExpression schemas) ty
@@ -1333,12 +1365,19 @@ instance (KnownSymbol alias, PGTyped schemas ty)
13331365-- >>> printSQL (dropType #schwarma :: Definition '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]] (Public '[]))
13341366-- DROP TYPE "schwarma";
13351367dropType
1336− :: (Has sch schemas schema, Has td schema ('Typedef ty))
1368+ :: (Has sch schemas schema, KnownSymbol td)
13371369 => QualifiedAlias sch td
13381370 -- ^ 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)
13401372dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderSQL tydef <> ";"
13411373
1374+dropTypeIfExists
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 <> ";"
1380+
13421381-- | `ColumnTypeExpression`s are used in `createTable` commands.
13431382newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
13441383 = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }