@@ -108,12 +108,12 @@ statements
108108-- database, like a `createTable`, `dropTable`, or `alterTable` command.
109109-- `Definition`s may be composed using the `>>>` operator.
110110newtype Definition
111− (schema0 :: SchemaType)
112− (schema1 :: SchemaType)
111+ (db0 :: DBType)
112+ (db1 :: DBType)
113113 = UnsafeDefinition { renderDefinition :: ByteString }
114114 deriving (GHC.Generic,Show,Eq,Ord,NFData)
115115
116−instance RenderSQL (Definition schema0 schema1) where
116+instance RenderSQL (Definition db0 db1) where
117117 renderSQL = renderDefinition
118118
119119instance Category Definition where
@@ -144,17 +144,20 @@ in printSQL setup
144144CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
145145-}
146146createTable
147− :: ( KnownSymbol table
147+ :: ( KnownSymbol sch
148+ , KnownSymbol tab
148149 , columns ~ (col ': cols)
149150 , SOP.SListI columns
150151 , SOP.SListI constraints
151− , schema1 ~ Create table ('Table (constraints :=> columns)) schema0 )
152− => Alias table -- ^ the name of the table to add
152+ , Has sch db0 schema0
153+ , schema1 ~ Create table ('Table (constraints :=> columns)) schema0
154+ , db1 ~ Alter sch schema1 db0 )
155+ => QualifiedAlias sch tab -- ^ the name of the table to add
153156 -> NP (Aliased (ColumnTypeExpression schema0)) columns
154157 -- ^ the names and datatype of each column
155158 -> NP (Aliased (TableConstraintExpression schema1 table)) constraints
156159 -- ^ constraints that must hold for the table
157− -> Definition schema0 schema1
160+ -> Definition db0 db1
158161createTable tab columns constraints = UnsafeDefinition $
159162 "CREATE TABLE" <+> renderCreation tab columns constraints
160163
@@ -182,31 +185,32 @@ in printSQL setup
182185CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
183186-}
184187createTableIfNotExists
185− :: ( Has table schema ('Table (constraints :=> columns))
188+ :: ( HasQualified sch tab db schema ('Table (constraints :=> columns))
186189 , SOP.SListI columns
187190 , SOP.SListI constraints )
188− => Alias table -- ^ the name of the table to add
191+ => QualifiedAlias sch tab -- ^ the name of the table to add
189192 -> NP (Aliased (ColumnTypeExpression schema)) columns
190193 -- ^ the names and datatype of each column
191194 -> NP (Aliased (TableConstraintExpression schema table)) constraints
192195 -- ^ constraints that must hold for the table
193− -> Definition schema schema
196+ -> Definition db db
194197createTableIfNotExists tab columns constraints = UnsafeDefinition $
195198 "CREATE TABLE IF NOT EXISTS"
196199 <+> renderCreation tab columns constraints
197200
198201-- helper function for `createTable` and `createTableIfNotExists`
199202renderCreation
200− :: ( KnownSymbol table
203+ :: ( KnownSymbol sch
204+ , KnownSymbol tab
201205 , SOP.SListI columns
202206 , SOP.SListI constraints )
203− => Alias table -- ^ the name of the table to add
207+ => QualifiedAlias sch tab -- ^ the name of the table to add
204208 -> NP (Aliased (ColumnTypeExpression schema0)) columns
205209 -- ^ the names and datatype of each column
206210 -> NP (Aliased (TableConstraintExpression schema1 table)) constraints
207211 -- ^ constraints that must hold for the table
208212 -> ByteString
209−renderCreation tab columns constraints = renderAlias tab
213+renderCreation tab columns constraints = renderQualifiedAlias tab
210214 <+> parenthesized
211215 ( renderCommaSeparated renderColumnDef columns
212216 <> ( case constraints of
@@ -512,10 +516,10 @@ DROP statements
512516-- >>> printSQL definition
513517-- DROP TABLE "muh_table";
514518dropTable
515− :: Has table schema ('Table t)
516− => Alias table -- ^ table to remove
517− -> Definition schema (Drop table schema)
518−dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderAlias tab <> ";"
519+ :: HasQualified sch tab db schema ('Table table)
520+ => QualifiedAlias sch tab -- ^ table to remove
521+ -> Definition db (Alter sch (Drop tab schema) db)
522+dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderQualifiedAlias tab <> ";"
519523
520524{-----------------------------------------
521525ALTER statements
@@ -523,13 +527,13 @@ ALTER statements
523527
524528-- | `alterTable` changes the definition of a table from the schema.
525529alterTable
526− :: KnownSymbol alias
527− => Alias alias -- ^ table to alter
528− -> AlterTable alias table schema -- ^ alteration to perform
529− -> Definition schema (Alter alias ('Table table) schema)
530+ :: HasQualified sch tab db schema ('Table table0)
531+ => QualifiedAlias sch tab -- ^ table to alter
532+ -> AlterTable tab table1 schema -- ^ alteration to perform
533+ -> Definition db (Alter sch (Alter tab ('Table table1) schema) db)
530534alterTable tab alteration = UnsafeDefinition $
531535 "ALTER TABLE"
532− <+> renderAlias tab
536+ <+> renderQualifiedAlias tab
533537 <+> renderAlterTable alteration
534538 <> ";"
535539
@@ -805,13 +809,13 @@ alterType ty = UnsafeAlterColumn $ "TYPE" <+> renderColumnTypeExpression ty
805809-- :}
806810-- CREATE VIEW "bc" AS SELECT "b" AS "b", "c" AS "c" FROM "abc" AS "abc";
807811createView
808− :: KnownSymbol view
809− => Alias view -- ^ the name of the view to add
810− -> Query schema '[] row
812+ :: (KnownSymbol sch, KnownSymbol vw)
813+ => QualifiedAlias sch vw -- ^ the name of the view to add
814+ -> Query schema '[] view
811815 -- ^ query
812− -> Definition schema (Create view ('View row) schema)
816+ -> Definition db (Alter sch (Create vw ('View view) schema) db)
813817createView alias query = UnsafeDefinition $
814− "CREATE" <+> "VIEW" <+> renderAlias alias <+> "AS"
818+ "CREATE" <+> "VIEW" <+> renderQualifiedAlias alias <+> "AS"
815819 <+> renderQuery query <> ";"
816820
817821-- | Drop a view.
@@ -827,24 +831,24 @@ createView alias query = UnsafeDefinition $
827831-- :}
828832-- DROP VIEW "bc";
829833dropView
830− :: Has view schema ('View v)
831− => Alias view -- ^ view to remove
832− -> Definition schema (Drop view schema)
833−dropView v = UnsafeDefinition $ "DROP VIEW" <+> renderAlias v <> ";"
834+ :: HasQualified sch vw db schema ('View view)
835+ => QualifiedAlias sch vw -- ^ view to remove
836+ -> Definition db (Alter sch (Drop vw schema) db)
837+dropView vw = UnsafeDefinition $ "DROP VIEW" <+> renderQualifiedAlias vw <> ";"
834838
835839-- | Enumerated types are created using the `createTypeEnum` command, for example
836840--
837841-- >>> printSQL $ createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy")
838842-- CREATE TYPE "mood" AS ENUM ('sad', 'ok', 'happy');
839843createTypeEnum
840− :: (KnownSymbol enum, SOP.All KnownSymbol labels)
841− => Alias enum
844+ :: (KnownSymbol enum, Has sch db schema, SOP.All KnownSymbol labels)
845+ => QualifiedAlias sch enum
842846 -- ^ name of the user defined enumerated type
843847 -> NP PGlabel labels
844848 -- ^ labels of the enumerated type
845− -> Definition schema (Create enum ('Typedef ('PGenum labels)) schema)
849+ -> Definition db (Alter sch (Create enum ('Typedef ('PGenum labels)) schema) db)
846850createTypeEnum enum labels = UnsafeDefinition $
847− "CREATE" <+> "TYPE" <+> renderAlias enum <+> "AS" <+> "ENUM" <+>
851+ "CREATE" <+> "TYPE" <+> renderQualifiedAlias enum <+> "AS" <+> "ENUM" <+>
848852 parenthesized (commaSeparated (renderLabels labels)) <> ";"
849853
850854-- | Enumerated types can also be generated from a Haskell type, for example
@@ -855,14 +859,14 @@ createTypeEnum enum labels = UnsafeDefinition $
855859-- >>> printSQL $ createTypeEnumFrom @Schwarma #schwarma
856860-- CREATE TYPE "schwarma" AS ENUM ('Beef', 'Lamb', 'Chicken');
857861createTypeEnumFrom
858− :: forall hask enum schema.
862+ :: forall hask sch enum db schema.
859863 ( SOP.Generic hask
860864 , SOP.All KnownSymbol (LabelsPG hask)
861865 , KnownSymbol enum
862− )
863− => Alias enum
866+ , Has sch db schema )
867+ => QualifiedAlias sch enum
864868 -- ^ name of the user defined enumerated type
865− -> Definition schema (Create enum ('Typedef (PG (Enumerated hask))) schema)
869+ -> Definition db (Alter sch (Create enum ('Typedef (PG (Enumerated hask))) schema) db)
866870createTypeEnumFrom enum = createTypeEnum enum
867871 (SOP.hpure label :: NP PGlabel (LabelsPG hask))
868872
@@ -885,14 +889,14 @@ in printSQL setup
885889CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
886890-}
887891createTypeComposite
888− :: (KnownSymbol ty, SOP.SListI fields)
889− => Alias ty
892+ :: (KnownSymbol ty, Has sch db schema, SOP.SListI fields)
893+ => QualifiedAlias sch ty
890894 -- ^ name of the user defined composite type
891895 -> NP (Aliased (TypeExpression schema)) fields
892896 -- ^ list of attribute names and data types
893− -> Definition schema (Create ty ('Typedef ('PGcomposite fields)) schema)
897+ -> Definition db (Alter sch (Create ty ('Typedef ('PGcomposite fields)) schema) db)
894898createTypeComposite ty fields = UnsafeDefinition $
895− "CREATE" <+> "TYPE" <+> renderAlias ty <+> "AS" <+> parenthesized
899+ "CREATE" <+> "TYPE" <+> renderQualifiedAlias ty <+> "AS" <+> parenthesized
896900 (renderCommaSeparated renderField fields) <> ";"
897901 where
898902 renderField :: Aliased (TypeExpression schema) x -> ByteString
@@ -907,12 +911,13 @@ createTypeComposite ty fields = UnsafeDefinition $
907911-- >>> printSQL $ createTypeCompositeFrom @Complex #complex
908912-- CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
909913createTypeCompositeFrom
910− :: forall hask ty schema.
914+ :: forall hask sch ty db schema.
911915 ( SOP.All (FieldTyped schema) (RowPG hask)
912− , KnownSymbol ty )
913− => Alias ty
916+ , KnownSymbol ty
917+ , Has sch db schema )
918+ => QualifiedAlias sch ty
914919 -- ^ name of the user defined composite type
915− -> Definition schema (Create ty ( 'Typedef (PG (Composite hask))) schema)
920+ -> Definition db (Alter sch (Create ty ( 'Typedef (PG (Composite hask))) schema) db)
916921createTypeCompositeFrom ty = createTypeComposite ty
917922 (SOP.hcpure (SOP.Proxy :: SOP.Proxy (FieldTyped schema)) fieldtype
918923 :: NP (Aliased (TypeExpression schema)) (RowPG hask))
@@ -931,11 +936,11 @@ instance (KnownSymbol alias, PGTyped schema ty)
931936-- >>> printSQL (dropType #schwarma :: Definition '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))] '[])
932937-- DROP TYPE "schwarma";
933938dropType
934− :: Has tydef schema ('Typedef ty)
935− => Alias tydef
939+ :: HasQualified sch td db schema ('Typedef ty)
940+ => QualifiedAlias sch td
936941 -- ^ name of the user defined type
937− -> Definition schema (Drop tydef schema)
938−dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderAlias tydef <> ";"
942+ -> Definition db (Alter sch (Drop td schema) db)
943+dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderQualifiedAlias tydef <> ";"
939944
940945-- | `ColumnTypeExpression`s are used in `createTable` commands.
941946newtype ColumnTypeExpression (schema :: SchemaType) (ty :: ColumnType)