Copyright | (c) Eitan Chatav 2017 |
---|---|
Maintainer | eitan@morphism.tech |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
Squeal.PostgreSQL.Definition
Description
Squeal data definition language.
Synopsis
- newtype Definition (schemas0 :: SchemasType) (schemas1 :: SchemasType) = UnsafeDefinition {}
- (>>>) :: Category cat => cat a b -> cat b c -> cat a c
- manipDefinition :: Manipulation '[] schemas '[] '[] -> Definition schemas schemas
- createSchema :: KnownSymbol sch => Alias sch -> Definition schemas (Create sch '[] schemas)
- createSchemaIfNotExists :: (KnownSymbol sch, Has sch schemas schema) => Alias sch -> Definition schemas schemas
- createTable :: (KnownSymbol sch, KnownSymbol tab, columns ~ (col ': cols), SListI columns, SListI constraints, Has sch schemas0 schema0, schemas1 ~ Alter sch (Create tab (Table (constraints :=> columns)) schema0) schemas0) => QualifiedAlias sch tab -> NP (Aliased (ColumnTypeExpression schemas0)) columns -> NP (Aliased (TableConstraintExpression sch tab schemas1)) constraints -> Definition schemas0 schemas1
- createTableIfNotExists :: (Has sch schemas schema, Has tab schema (Table (constraints :=> columns)), SListI columns, SListI constraints) => QualifiedAlias sch tab -> NP (Aliased (ColumnTypeExpression schemas)) columns -> NP (Aliased (TableConstraintExpression sch tab schemas)) constraints -> Definition schemas schemas
- createView :: (KnownSymbol sch, KnownSymbol vw, Has sch schemas schema) => QualifiedAlias sch vw -> Query '[] '[] schemas '[] view -> Definition schemas (Alter sch (Create vw (View view) schema) schemas)
- createTypeEnum :: (KnownSymbol enum, Has sch schemas schema, All KnownSymbol labels) => QualifiedAlias sch enum -> NP PGlabel labels -> Definition schemas (Alter sch (Create enum (Typedef (PGenum labels)) schema) schemas)
- createTypeEnumFrom :: forall hask sch enum schemas schema. (Generic hask, All KnownSymbol (LabelsPG hask), KnownSymbol enum, Has sch schemas schema) => QualifiedAlias sch enum -> Definition schemas (Alter sch (Create enum (Typedef (PG (Enumerated hask))) schema) schemas)
- createTypeComposite :: (KnownSymbol ty, Has sch schemas schema, SListI fields) => QualifiedAlias sch ty -> NP (Aliased (TypeExpression schemas)) fields -> Definition schemas (Alter sch (Create ty (Typedef (PGcomposite fields)) schema) schemas)
- createTypeCompositeFrom :: forall hask sch ty schemas schema. (All (FieldTyped schemas) (RowPG hask), KnownSymbol ty, Has sch schemas schema) => QualifiedAlias sch ty -> Definition schemas (Alter sch (Create ty (Typedef (PG (Composite hask))) schema) schemas)
- class FieldTyped schemas ty where
- fieldtype :: Aliased (TypeExpression schemas) ty
- createDomain :: (Has sch schemas schema, KnownSymbol dom) => QualifiedAlias sch dom -> (forall nullity. TypeExpression schemas (nullity ty)) -> (forall tab. Condition '[] '[] Ungrouped schemas '[] '[tab ::: '["value" ::: Null ty]]) -> Definition schemas (Alter sch (Create alias (Typedef ty) schema) schemas)
- newtype TableConstraintExpression (sch :: Symbol) (tab :: Symbol) (schemas :: SchemasType) (constraint :: TableConstraint) = UnsafeTableConstraintExpression {}
- check :: (Has sch schemas schema, Has tab schema (Table table), HasAll aliases (TableToRow table) subcolumns) => NP Alias aliases -> (forall t. Condition '[] '[] Ungrouped schemas '[] '[t ::: subcolumns]) -> TableConstraintExpression sch tab schemas (Check aliases)
- unique :: (Has sch schemas schema, Has tab schema (Table table), HasAll aliases (TableToRow table) subcolumns) => NP Alias aliases -> TableConstraintExpression sch tab schemas (Unique aliases)
- primaryKey :: (Has sch schemas schema, Has tab schema (Table table), HasAll aliases (TableToColumns table) subcolumns, AllNotNull subcolumns) => NP Alias aliases -> TableConstraintExpression sch tab schemas (PrimaryKey aliases)
- foreignKey :: ForeignKeyed schemas sch schema child parent table reftable columns refcolumns constraints cols reftys tys => NP Alias columns -> Alias parent -> NP Alias refcolumns -> OnDeleteClause -> OnUpdateClause -> TableConstraintExpression sch child schemas (ForeignKey columns parent refcolumns)
- type ForeignKeyed schemas sch schema child parent table reftable columns refcolumns constraints cols reftys tys = (Has sch schemas schema, Has child schema (Table table), Has parent schema (Table reftable), HasAll columns (TableToColumns table) tys, reftable ~ (constraints :=> cols), HasAll refcolumns cols reftys, AllZip SamePGType tys reftys, Uniquely refcolumns constraints)
- data OnDeleteClause
- data OnUpdateClause
- dropSchema :: Has sch schemas schema => Alias sch -> Definition schemas (Drop sch schemas)
- dropTable :: (Has sch schemas schema, Has tab schema (Table table)) => QualifiedAlias sch tab -> Definition schemas (Alter sch (Drop tab schema) schemas)
- dropView :: (Has sch schemas schema, Has vw schema (View view)) => QualifiedAlias sch vw -> Definition schemas (Alter sch (Drop vw schema) schemas)
- dropType :: (Has sch schemas schema, Has td schema (Typedef ty)) => QualifiedAlias sch td -> Definition schemas (Alter sch (Drop td schema) schemas)
- alterTable :: (Has sch schemas schema, Has tab schema (Table table0)) => QualifiedAlias sch tab -> AlterTable sch tab schemas table1 -> Definition schemas (Alter sch (Alter tab (Table table1) schema) schemas)
- alterTableRename :: (KnownSymbol table0, KnownSymbol table1) => Alias table0 -> Alias table1 -> Definition schema (Rename table0 table1 schema)
- newtype AlterTable (sch :: Symbol) (tab :: Symbol) (schemas :: SchemasType) (table :: TableType) = UnsafeAlterTable {}
- addConstraint :: (KnownSymbol alias, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns), table1 ~ (Create alias constraint constraints :=> columns)) => Alias alias -> TableConstraintExpression sch tab schemas constraint -> AlterTable sch tab schemas table1
- dropConstraint :: (KnownSymbol constraint, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns), table1 ~ (Drop constraint constraints :=> columns)) => Alias constraint -> AlterTable sch tab schemas table1
- class AddColumn ty where
- addColumn :: (KnownSymbol column, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns)) => Alias column -> ColumnTypeExpression schemas ty -> AlterTable sch tab schemas (constraints :=> Create column ty columns)
- dropColumn :: (KnownSymbol column, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns), table1 ~ (constraints :=> Drop column columns)) => Alias column -> AlterTable sch tab schemas table1
- renameColumn :: (KnownSymbol column0, KnownSymbol column1, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns), table1 ~ (constraints :=> Rename column0 column1 columns)) => Alias column0 -> Alias column1 -> AlterTable sch tab schemas table1
- alterColumn :: (KnownSymbol column, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns), Has column columns ty0, table1 ~ (constraints :=> Alter column ty1 columns)) => Alias column -> AlterColumn schemas ty0 ty1 -> AlterTable sch tab schemas table1
- newtype AlterColumn (schemas :: SchemasType) (ty0 :: ColumnType) (ty1 :: ColumnType) = UnsafeAlterColumn {}
- setDefault :: Expression '[] '[] Ungrouped schemas '[] '[] ty -> AlterColumn schemas (constraint :=> ty) (Def :=> ty)
- dropDefault :: AlterColumn schemas (Def :=> ty) (NoDef :=> ty)
- setNotNull :: AlterColumn schemas (constraint :=> Null ty) (constraint :=> NotNull ty)
- dropNotNull :: AlterColumn schemas (constraint :=> NotNull ty) (constraint :=> Null ty)
- alterType :: ColumnTypeExpression schemas ty -> AlterColumn schemas ty0 ty
- newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType) = UnsafeColumnTypeExpression {}
- nullable :: TypeExpression schemas (nullity ty) -> ColumnTypeExpression schemas (NoDef :=> Null ty)
- notNullable :: TypeExpression schemas (nullity ty) -> ColumnTypeExpression schemas (NoDef :=> NotNull ty)
- default_ :: Expression '[] '[] Ungrouped schemas '[] '[] ty -> ColumnTypeExpression schemas (NoDef :=> ty) -> ColumnTypeExpression schemas (Def :=> ty)
- serial2 :: ColumnTypeExpression schemas (Def :=> NotNull PGint2)
- smallserial :: ColumnTypeExpression schemas (Def :=> NotNull PGint2)
- serial4 :: ColumnTypeExpression schemas (Def :=> NotNull PGint4)
- serial :: ColumnTypeExpression schemas (Def :=> NotNull PGint4)
- serial8 :: ColumnTypeExpression schemas (Def :=> NotNull PGint8)
- bigserial :: ColumnTypeExpression schemas (Def :=> NotNull PGint8)
Definition
newtype Definition (schemas0 :: SchemasType) (schemas1 :: SchemasType) Source #
A Definition
is a statement that changes the schemas of the
database, like a createTable
, dropTable
, or alterTable
command.
Definition
s may be composed using the >>>
operator.
Constructors
UnsafeDefinition | |
Fields |
Instances
Migratory Definition Source # | |
Defined in Squeal.PostgreSQL.Migration Methods migrateUp :: AlignedList (Migration Definition) schemas0 schemas1 -> PQ schemas0 schemas1 IO () Source # migrateDown :: AlignedList (Migration Definition) schemas0 schemas1 -> PQ schemas1 schemas0 IO () Source # | |
Category Definition Source # | |
Defined in Squeal.PostgreSQL.Definition | |
Eq (Definition schemas0 schemas1) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods (==) :: Definition schemas0 schemas1 -> Definition schemas0 schemas1 -> Bool # (/=) :: Definition schemas0 schemas1 -> Definition schemas0 schemas1 -> Bool # | |
Ord (Definition schemas0 schemas1) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods compare :: Definition schemas0 schemas1 -> Definition schemas0 schemas1 -> Ordering # (<) :: Definition schemas0 schemas1 -> Definition schemas0 schemas1 -> Bool # (<=) :: Definition schemas0 schemas1 -> Definition schemas0 schemas1 -> Bool # (>) :: Definition schemas0 schemas1 -> Definition schemas0 schemas1 -> Bool # (>=) :: Definition schemas0 schemas1 -> Definition schemas0 schemas1 -> Bool # max :: Definition schemas0 schemas1 -> Definition schemas0 schemas1 -> Definition schemas0 schemas1 # min :: Definition schemas0 schemas1 -> Definition schemas0 schemas1 -> Definition schemas0 schemas1 # | |
Show (Definition schemas0 schemas1) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods showsPrec :: Int -> Definition schemas0 schemas1 -> ShowS # show :: Definition schemas0 schemas1 -> String # showList :: [Definition schemas0 schemas1] -> ShowS # | |
Generic (Definition schemas0 schemas1) Source # | |
Defined in Squeal.PostgreSQL.Definition Associated Types type Rep (Definition schemas0 schemas1) :: Type -> Type # Methods from :: Definition schemas0 schemas1 -> Rep (Definition schemas0 schemas1) x # to :: Rep (Definition schemas0 schemas1) x -> Definition schemas0 schemas1 # | |
NFData (Definition schemas0 schemas1) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods rnf :: Definition schemas0 schemas1 -> () # | |
RenderSQL (Definition schemas0 schemas1) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods renderSQL :: Definition schemas0 schemas1 -> ByteString Source # | |
type Rep (Definition schemas0 schemas1) Source # | |
Defined in Squeal.PostgreSQL.Definition type Rep (Definition schemas0 schemas1) = D1 (MetaData "Definition" "Squeal.PostgreSQL.Definition" "squeal-postgresql-0.5.2.0-4fAomBtpMjd6mRwLthA4w2" True) (C1 (MetaCons "UnsafeDefinition" PrefixI True) (S1 (MetaSel (Just "renderDefinition") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 ByteString))) |
Arguments
:: Manipulation '[] schemas '[] '[] | no input or output |
-> Definition schemas schemas |
A Manipulation
without input or output can be run as a statement
along with other Definition
s, by embedding it using manipDefinition
.
Tables
Create
createSchema :: KnownSymbol sch => Alias sch -> Definition schemas (Create sch '[] schemas) Source #
createSchema
enters a new schema into the current database.
The schema name must be distinct from the name of any existing schema
in the current database.
A schema is essentially a namespace: it contains named objects
(tables, data types, functions, and operators) whose names
can duplicate those of other objects existing in other schemas.
Named objects are accessed by QualifiedAlias
es with the schema
name as a prefix.
createSchemaIfNotExists :: (KnownSymbol sch, Has sch schemas schema) => Alias sch -> Definition schemas schemas Source #
Idempotent version of createSchema
.
Arguments
:: (KnownSymbol sch, KnownSymbol tab, columns ~ (col ': cols), SListI columns, SListI constraints, Has sch schemas0 schema0, schemas1 ~ Alter sch (Create tab (Table (constraints :=> columns)) schema0) schemas0) | |
=> QualifiedAlias sch tab | the name of the table to add |
-> NP (Aliased (ColumnTypeExpression schemas0)) columns | the names and datatype of each column |
-> NP (Aliased (TableConstraintExpression sch tab schemas1)) constraints | constraints that must hold for the table |
-> Definition schemas0 schemas1 |
createTable
adds a table to the schema.
>>>
:set -XOverloadedLabels
>>>
:{
type Table = '[] :=> '[ "a" ::: 'NoDef :=> 'Null 'PGint4 , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ] :}
>>>
:{
let setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table]) setup = createTable #tab (nullable int `as` #a :* nullable real `as` #b) Nil in printSQL setup :} CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
createTableIfNotExists Source #
Arguments
:: (Has sch schemas schema, Has tab schema (Table (constraints :=> columns)), SListI columns, SListI constraints) | |
=> QualifiedAlias sch tab | the name of the table to add |
-> NP (Aliased (ColumnTypeExpression schemas)) columns | the names and datatype of each column |
-> NP (Aliased (TableConstraintExpression sch tab schemas)) constraints | constraints that must hold for the table |
-> Definition schemas schemas |
createTableIfNotExists
creates a table if it doesn't exist, but does not add it to the schema.
Instead, the schema already has the table so if the table did not yet exist, the schema was wrong.
createTableIfNotExists
fixes this. Interestingly, this property makes it an idempotent in
the Category
of Definition
s.
>>>
:set -XOverloadedLabels -XTypeApplications
>>>
:{
type Table = '[] :=> '[ "a" ::: 'NoDef :=> 'Null 'PGint4 , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ] :}
>>>
type Schemas = Public '["tab" ::: 'Table Table]
>>>
:{
let setup :: Definition Schemas Schemas setup = createTableIfNotExists #tab (nullable int `as` #a :* nullable real `as` #b) Nil in printSQL setup :} CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
Arguments
:: (KnownSymbol sch, KnownSymbol vw, Has sch schemas schema) | |
=> QualifiedAlias sch vw | the name of the view to add |
-> Query '[] '[] schemas '[] view | query |
-> Definition schemas (Alter sch (Create vw (View view) schema) schemas) |
Create a view. >>> type ABC = '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4] >>> type BC = '["b" ::: 'Null 'PGint4, "c" ::: 'Null 'PGint4] >>> :{ let definition :: Definition '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC)]] '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC), "bc" ::: 'View BC]] definition = createView b :* abc))) in printSQL definition :} CREATE VIEW "bc" AS SELECT "b" AS "b", "c" AS "c" FROM "abc" AS "abc";
Arguments
:: (KnownSymbol enum, Has sch schemas schema, All KnownSymbol labels) | |
=> QualifiedAlias sch enum | name of the user defined enumerated type |
-> NP PGlabel labels | labels of the enumerated type |
-> Definition schemas (Alter sch (Create enum (Typedef (PGenum labels)) schema) schemas) |
Enumerated types are created using the createTypeEnum
command, for example
>>>
printSQL $ (createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy") :: Definition (Public '[]) '["public" ::: '["mood" ::: 'Typedef ('PGenum '["sad","ok","happy"])]])
CREATE TYPE "mood" AS ENUM ('sad', 'ok', 'happy');
Arguments
:: (Generic hask, All KnownSymbol (LabelsPG hask), KnownSymbol enum, Has sch schemas schema) | |
=> QualifiedAlias sch enum | name of the user defined enumerated type |
-> Definition schemas (Alter sch (Create enum (Typedef (PG (Enumerated hask))) schema) schemas) |
Enumerated types can also be generated from a Haskell type, for example
>>>
data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
>>>
instance SOP.Generic Schwarma
>>>
instance SOP.HasDatatypeInfo Schwarma
>>>
:{
let createSchwarma :: Definition (Public '[]) '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]] createSchwarma = createTypeEnumFrom @Schwarma #schwarma in printSQL createSchwarma :} CREATE TYPE "schwarma" AS ENUM ('Beef', 'Lamb', 'Chicken');
Arguments
:: (KnownSymbol ty, Has sch schemas schema, SListI fields) | |
=> QualifiedAlias sch ty | name of the user defined composite type |
-> NP (Aliased (TypeExpression schemas)) fields | list of attribute names and data types |
-> Definition schemas (Alter sch (Create ty (Typedef (PGcomposite fields)) schema) schemas) |
createTypeComposite
creates a composite type. The composite type is
specified by a list of attribute names and data types.
>>>
:{
type PGcomplex = 'PGcomposite '[ "real" ::: 'NotNull 'PGfloat8 , "imaginary" ::: 'NotNull 'PGfloat8 ] :}
>>>
:{
let setup :: Definition (Public '[]) '["public" ::: '["complex" ::: 'Typedef PGcomplex]] setup = createTypeComposite #complex (float8 `as` #real :* float8 `as` #imaginary) in printSQL setup :} CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
createTypeCompositeFrom Source #
Arguments
:: (All (FieldTyped schemas) (RowPG hask), KnownSymbol ty, Has sch schemas schema) | |
=> QualifiedAlias sch ty | name of the user defined composite type |
-> Definition schemas (Alter sch (Create ty (Typedef (PG (Composite hask))) schema) schemas) |
Composite types can also be generated from a Haskell type, for example
>>>
data Complex = Complex {real :: Double, imaginary :: Double} deriving GHC.Generic
>>>
instance SOP.Generic Complex
>>>
instance SOP.HasDatatypeInfo Complex
>>>
type Schema = '["complex" ::: 'Typedef (PG (Composite Complex))]
>>>
:{
let createComplex :: Definition (Public '[]) (Public Schema) createComplex = createTypeCompositeFrom @Complex #complex in printSQL createComplex :} CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
class FieldTyped schemas ty where Source #
Lift PGTyped
to a field
Methods
fieldtype :: Aliased (TypeExpression schemas) ty Source #
Instances
(KnownSymbol alias, PGTyped schemas ty) => FieldTyped schemas (alias ::: ty) Source # | |
Defined in Squeal.PostgreSQL.Definition |
createDomain :: (Has sch schemas schema, KnownSymbol dom) => QualifiedAlias sch dom -> (forall nullity. TypeExpression schemas (nullity ty)) -> (forall tab. Condition '[] '[] Ungrouped schemas '[] '[tab ::: '["value" ::: Null ty]]) -> Definition schemas (Alter sch (Create alias (Typedef ty) schema) schemas) Source #
createDomain
creates a new domain. A domain is essentially a data type
with constraints (restrictions on the allowed set of values).
Domains are useful for abstracting common constraints on fields
into a single location for maintenance. For example, several tables might
contain email address columns, all requiring the same check
constraint
to verify the address syntax. Define a domain rather than setting up
each table's constraint individually.
>>>
:{
let createPositive :: Definition (Public '[]) (Public '["positive" ::: 'Typedef 'PGfloat4]) createPositive = createDomain #positive real (#value .> 0 .&& (#value & isNotNull)) in printSQL createPositive :} CREATE DOMAIN "positive" AS real CHECK ((("value" > 0) AND "value" IS NOT NULL));
newtype TableConstraintExpression (sch :: Symbol) (tab :: Symbol) (schemas :: SchemasType) (constraint :: TableConstraint) Source #
Data types are a way to limit the kind of data that can be stored in a
table. For many applications, however, the constraint they provide is
too coarse. For example, a column containing a product price should
probably only accept positive values. But there is no standard data type
that accepts only positive numbers. Another issue is that you might want
to constrain column data with respect to other columns or rows.
For example, in a table containing product information,
there should be only one row for each product number.
TableConstraint
s give you as much control over the data in your tables
as you wish. If a user attempts to store data in a column that would
violate a constraint, an error is raised. This applies
even if the value came from the default value definition.
Constructors
UnsafeTableConstraintExpression | |
Instances
Eq (TableConstraintExpression sch tab schemas constraint) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods (==) :: TableConstraintExpression sch tab schemas constraint -> TableConstraintExpression sch tab schemas constraint -> Bool # (/=) :: TableConstraintExpression sch tab schemas constraint -> TableConstraintExpression sch tab schemas constraint -> Bool # | |
Ord (TableConstraintExpression sch tab schemas constraint) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods compare :: TableConstraintExpression sch tab schemas constraint -> TableConstraintExpression sch tab schemas constraint -> Ordering # (<) :: TableConstraintExpression sch tab schemas constraint -> TableConstraintExpression sch tab schemas constraint -> Bool # (<=) :: TableConstraintExpression sch tab schemas constraint -> TableConstraintExpression sch tab schemas constraint -> Bool # (>) :: TableConstraintExpression sch tab schemas constraint -> TableConstraintExpression sch tab schemas constraint -> Bool # (>=) :: TableConstraintExpression sch tab schemas constraint -> TableConstraintExpression sch tab schemas constraint -> Bool # max :: TableConstraintExpression sch tab schemas constraint -> TableConstraintExpression sch tab schemas constraint -> TableConstraintExpression sch tab schemas constraint # min :: TableConstraintExpression sch tab schemas constraint -> TableConstraintExpression sch tab schemas constraint -> TableConstraintExpression sch tab schemas constraint # | |
Show (TableConstraintExpression sch tab schemas constraint) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods showsPrec :: Int -> TableConstraintExpression sch tab schemas constraint -> ShowS # show :: TableConstraintExpression sch tab schemas constraint -> String # showList :: [TableConstraintExpression sch tab schemas constraint] -> ShowS # | |
Generic (TableConstraintExpression sch tab schemas constraint) Source # | |
Defined in Squeal.PostgreSQL.Definition Associated Types type Rep (TableConstraintExpression sch tab schemas constraint) :: Type -> Type # Methods from :: TableConstraintExpression sch tab schemas constraint -> Rep (TableConstraintExpression sch tab schemas constraint) x # to :: Rep (TableConstraintExpression sch tab schemas constraint) x -> TableConstraintExpression sch tab schemas constraint # | |
NFData (TableConstraintExpression sch tab schemas constraint) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods rnf :: TableConstraintExpression sch tab schemas constraint -> () # | |
RenderSQL (TableConstraintExpression sch tab schemas constraint) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods renderSQL :: TableConstraintExpression sch tab schemas constraint -> ByteString Source # | |
type Rep (TableConstraintExpression sch tab schemas constraint) Source # | |
Defined in Squeal.PostgreSQL.Definition type Rep (TableConstraintExpression sch tab schemas constraint) = D1 (MetaData "TableConstraintExpression" "Squeal.PostgreSQL.Definition" "squeal-postgresql-0.5.2.0-4fAomBtpMjd6mRwLthA4w2" True) (C1 (MetaCons "UnsafeTableConstraintExpression" PrefixI True) (S1 (MetaSel (Just "renderTableConstraintExpression") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 ByteString))) |
Arguments
:: (Has sch schemas schema, Has tab schema (Table table), HasAll aliases (TableToRow table) subcolumns) | |
=> NP Alias aliases | specify the subcolumns which are getting checked |
-> (forall t. Condition '[] '[] Ungrouped schemas '[] '[t ::: subcolumns]) | a closed |
-> TableConstraintExpression sch tab schemas (Check aliases) |
A check
constraint is the most generic TableConstraint
type.
It allows you to specify that the value in a certain column must satisfy
a Boolean (truth-value) expression.
>>>
:{
type Schema = '[ "tab" ::: 'Table ('[ "inequality" ::: 'Check '["a","b"]] :=> '[ "a" ::: 'NoDef :=> 'NotNull 'PGint4, "b" ::: 'NoDef :=> 'NotNull 'PGint4 ])] :}
>>>
:{
let definition :: Definition (Public '[]) (Public Schema) definition = createTable #tab ( (int & notNullable) `as` #a :* (int & notNullable) `as` #b ) ( check (#a :* #b) (#a .> #b) `as` #inequality ) :}
>>>
printSQL definition
CREATE TABLE "tab" ("a" int NOT NULL, "b" int NOT NULL, CONSTRAINT "inequality" CHECK (("a" > "b")));
Arguments
:: (Has sch schemas schema, Has tab schema (Table table), HasAll aliases (TableToRow table) subcolumns) | |
=> NP Alias aliases | specify subcolumns which together are unique for each row |
-> TableConstraintExpression sch tab schemas (Unique aliases) |
A unique
constraint ensure that the data contained in a column,
or a group of columns, is unique among all the rows in the table.
>>>
:{
type Schema = '[ "tab" ::: 'Table( '[ "uq_a_b" ::: 'Unique '["a","b"]] :=> '[ "a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4 ])] :}
>>>
:{
let definition :: Definition (Public '[]) (Public Schema) definition = createTable #tab ( (int & nullable) `as` #a :* (int & nullable) `as` #b ) ( unique (#a :* #b) `as` #uq_a_b ) :}
>>>
printSQL definition
CREATE TABLE "tab" ("a" int NULL, "b" int NULL, CONSTRAINT "uq_a_b" UNIQUE ("a", "b"));
Arguments
:: (Has sch schemas schema, Has tab schema (Table table), HasAll aliases (TableToColumns table) subcolumns, AllNotNull subcolumns) | |
=> NP Alias aliases | specify the subcolumns which together form a primary key. |
-> TableConstraintExpression sch tab schemas (PrimaryKey aliases) |
A primaryKey
constraint indicates that a column, or group of columns,
can be used as a unique identifier for rows in the table.
This requires that the values be both unique and not null.
>>>
:{
type Schema = '[ "tab" ::: 'Table ('[ "pk_id" ::: 'PrimaryKey '["id"]] :=> '[ "id" ::: 'Def :=> 'NotNull 'PGint4, "name" ::: 'NoDef :=> 'NotNull 'PGtext ])] :}
>>>
:{
let definition :: Definition (Public '[]) (Public Schema) definition = createTable #tab ( serial `as` #id :* (text & notNullable) `as` #name ) ( primaryKey #id `as` #pk_id ) :}
>>>
printSQL definition
CREATE TABLE "tab" ("id" serial, "name" text NOT NULL, CONSTRAINT "pk_id" PRIMARY KEY ("id"));
Arguments
:: ForeignKeyed schemas sch schema child parent table reftable columns refcolumns constraints cols reftys tys | |
=> NP Alias columns | column or columns in the table |
-> Alias parent | reference table |
-> NP Alias refcolumns | reference column or columns in the reference table |
-> OnDeleteClause | what to do when reference is deleted |
-> OnUpdateClause | what to do when reference is updated |
-> TableConstraintExpression sch child schemas (ForeignKey columns parent refcolumns) |
A foreignKey
specifies that the values in a column
(or a group of columns) must match the values appearing in some row of
another table. We say this maintains the referential integrity
between two related tables.
>>>
:{
type Schema = '[ "users" ::: 'Table ( '[ "pk_users" ::: 'PrimaryKey '["id"] ] :=> '[ "id" ::: 'Def :=> 'NotNull 'PGint4 , "name" ::: 'NoDef :=> 'NotNull 'PGtext ]) , "emails" ::: 'Table ( '[ "pk_emails" ::: 'PrimaryKey '["id"] , "fk_user_id" ::: 'ForeignKey '["user_id"] "users" '["id"] ] :=> '[ "id" ::: 'Def :=> 'NotNull 'PGint4 , "user_id" ::: 'NoDef :=> 'NotNull 'PGint4 , "email" ::: 'NoDef :=> 'Null 'PGtext ]) ] :}
>>>
:{
let setup :: Definition (Public '[]) (Public Schema) setup = createTable #users ( serial `as` #id :* (text & notNullable) `as` #name ) ( primaryKey #id `as` #pk_users ) >>> createTable #emails ( serial `as` #id :* (int & notNullable) `as` #user_id :* (text & nullable) `as` #email ) ( primaryKey #id `as` #pk_emails :* foreignKey #user_id #users #id OnDeleteCascade OnUpdateCascade `as` #fk_user_id ) in printSQL setup :} CREATE TABLE "users" ("id" serial, "name" text NOT NULL, CONSTRAINT "pk_users" PRIMARY KEY ("id")); CREATE TABLE "emails" ("id" serial, "user_id" int NOT NULL, "email" text NULL, CONSTRAINT "pk_emails" PRIMARY KEY ("id"), CONSTRAINT "fk_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE);
A foreignKey
can even be a table self-reference.
>>>
:{
type Schema = '[ "employees" ::: 'Table ( '[ "employees_pk" ::: 'PrimaryKey '["id"] , "employees_employer_fk" ::: 'ForeignKey '["employer_id"] "employees" '["id"] ] :=> '[ "id" ::: 'Def :=> 'NotNull 'PGint4 , "name" ::: 'NoDef :=> 'NotNull 'PGtext , "employer_id" ::: 'NoDef :=> 'Null 'PGint4 ]) ] :}
>>>
:{
let setup :: Definition (Public '[]) (Public Schema) setup = createTable #employees ( serial `as` #id :* (text & notNullable) `as` #name :* (integer & nullable) `as` #employer_id ) ( primaryKey #id `as` #employees_pk :* foreignKey #employer_id #employees #id OnDeleteCascade OnUpdateCascade `as` #employees_employer_fk ) in printSQL setup :} CREATE TABLE "employees" ("id" serial, "name" text NOT NULL, "employer_id" integer NULL, CONSTRAINT "employees_pk" PRIMARY KEY ("id"), CONSTRAINT "employees_employer_fk" FOREIGN KEY ("employer_id") REFERENCES "employees" ("id") ON DELETE CASCADE ON UPDATE CASCADE);
type ForeignKeyed schemas sch schema child parent table reftable columns refcolumns constraints cols reftys tys = (Has sch schemas schema, Has child schema (Table table), Has parent schema (Table reftable), HasAll columns (TableToColumns table) tys, reftable ~ (constraints :=> cols), HasAll refcolumns cols reftys, AllZip SamePGType tys reftys, Uniquely refcolumns constraints) Source #
A constraint synonym between types involved in a foreign key constraint.
data OnDeleteClause Source #
OnDeleteClause
indicates what to do with rows that reference a deleted row.
Constructors
OnDeleteNoAction | if any referencing rows still exist when the constraint is checked, an error is raised |
OnDeleteRestrict | prevents deletion of a referenced row |
OnDeleteCascade | specifies that when a referenced row is deleted, row(s) referencing it should be automatically deleted as well |
Instances
data OnUpdateClause Source #
Analagous to OnDeleteClause
there is also OnUpdateClause
which is invoked
when a referenced column is changed (updated).
Constructors
OnUpdateNoAction | if any referencing rows has not changed when the constraint is checked, an error is raised |
OnUpdateRestrict | prevents update of a referenced row |
OnUpdateCascade | the updated values of the referenced column(s) should be copied into the referencing row(s) |
Instances
Drop
dropSchema :: Has sch schemas schema => Alias sch -> Definition schemas (Drop sch schemas) Source #
>>>
:{
let definition :: Definition '["muh_schema" ::: schema, "public" ::: public] '["public" ::: public] definition = dropSchema #muh_schema :}
>>>
printSQL definition
DROP SCHEMA "muh_schema";
Arguments
:: (Has sch schemas schema, Has tab schema (Table table)) | |
=> QualifiedAlias sch tab | table to remove |
-> Definition schemas (Alter sch (Drop tab schema) schemas) |
dropTable
removes a table from the schema.
>>>
:{
let definition :: Definition '["public" ::: '["muh_table" ::: 'Table t]] (Public '[]) definition = dropTable #muh_table :}
>>>
printSQL definition
DROP TABLE "muh_table";
Arguments
:: (Has sch schemas schema, Has vw schema (View view)) | |
=> QualifiedAlias sch vw | view to remove |
-> Definition schemas (Alter sch (Drop vw schema) schemas) |
Drop a view.
>>>
:{
let definition :: Definition '[ "public" ::: '["abc" ::: 'Table ('[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4]) , "bc" ::: 'View ('["b" ::: 'Null 'PGint4, "c" ::: 'Null 'PGint4])]] '[ "public" ::: '["abc" ::: 'Table ('[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4])]] definition = dropView #bc in printSQL definition :} DROP VIEW "bc";
Arguments
:: (Has sch schemas schema, Has td schema (Typedef ty)) | |
=> QualifiedAlias sch td | name of the user defined type |
-> Definition schemas (Alter sch (Drop td schema) schemas) |
Drop a type.
>>>
data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
>>>
instance SOP.Generic Schwarma
>>>
instance SOP.HasDatatypeInfo Schwarma
>>>
printSQL (dropType #schwarma :: Definition '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]] (Public '[]))
DROP TYPE "schwarma";
Alter
Arguments
:: (Has sch schemas schema, Has tab schema (Table table0)) | |
=> QualifiedAlias sch tab | table to alter |
-> AlterTable sch tab schemas table1 | alteration to perform |
-> Definition schemas (Alter sch (Alter tab (Table table1) schema) schemas) |
alterTable
changes the definition of a table from the schema.
Arguments
:: (KnownSymbol table0, KnownSymbol table1) | |
=> Alias table0 | table to rename |
-> Alias table1 | what to rename it |
-> Definition schema (Rename table0 table1 schema) |
alterTableRename
changes the name of a table from the schema.
>>>
printSQL $ alterTableRename #foo #bar
ALTER TABLE "foo" RENAME TO "bar";
newtype AlterTable (sch :: Symbol) (tab :: Symbol) (schemas :: SchemasType) (table :: TableType) Source #
An AlterTable
describes the alteration to perform on the columns
of a table.
Constructors
UnsafeAlterTable | |
Fields |
Instances
Eq (AlterTable sch tab schemas table) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods (==) :: AlterTable sch tab schemas table -> AlterTable sch tab schemas table -> Bool # (/=) :: AlterTable sch tab schemas table -> AlterTable sch tab schemas table -> Bool # | |
Ord (AlterTable sch tab schemas table) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods compare :: AlterTable sch tab schemas table -> AlterTable sch tab schemas table -> Ordering # (<) :: AlterTable sch tab schemas table -> AlterTable sch tab schemas table -> Bool # (<=) :: AlterTable sch tab schemas table -> AlterTable sch tab schemas table -> Bool # (>) :: AlterTable sch tab schemas table -> AlterTable sch tab schemas table -> Bool # (>=) :: AlterTable sch tab schemas table -> AlterTable sch tab schemas table -> Bool # max :: AlterTable sch tab schemas table -> AlterTable sch tab schemas table -> AlterTable sch tab schemas table # min :: AlterTable sch tab schemas table -> AlterTable sch tab schemas table -> AlterTable sch tab schemas table # | |
Show (AlterTable sch tab schemas table) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods showsPrec :: Int -> AlterTable sch tab schemas table -> ShowS # show :: AlterTable sch tab schemas table -> String # showList :: [AlterTable sch tab schemas table] -> ShowS # | |
Generic (AlterTable sch tab schemas table) Source # | |
Defined in Squeal.PostgreSQL.Definition Associated Types type Rep (AlterTable sch tab schemas table) :: Type -> Type # Methods from :: AlterTable sch tab schemas table -> Rep (AlterTable sch tab schemas table) x # to :: Rep (AlterTable sch tab schemas table) x -> AlterTable sch tab schemas table # | |
NFData (AlterTable sch tab schemas table) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods rnf :: AlterTable sch tab schemas table -> () # | |
type Rep (AlterTable sch tab schemas table) Source # | |
Defined in Squeal.PostgreSQL.Definition type Rep (AlterTable sch tab schemas table) = D1 (MetaData "AlterTable" "Squeal.PostgreSQL.Definition" "squeal-postgresql-0.5.2.0-4fAomBtpMjd6mRwLthA4w2" True) (C1 (MetaCons "UnsafeAlterTable" PrefixI True) (S1 (MetaSel (Just "renderAlterTable") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 ByteString))) |
Arguments
:: (KnownSymbol alias, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns), table1 ~ (Create alias constraint constraints :=> columns)) | |
=> Alias alias | |
-> TableConstraintExpression sch tab schemas constraint | constraint to add |
-> AlterTable sch tab schemas table1 |
An addConstraint
adds a table constraint.
>>>
:{
let definition :: Definition '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]] '["public" ::: '["tab" ::: 'Table ('["positive" ::: 'Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]] definition = alterTable #tab (addConstraint #positive (check #col (#col .> 0))) in printSQL definition :} ALTER TABLE "tab" ADD CONSTRAINT "positive" CHECK (("col" > 0));
Arguments
:: (KnownSymbol constraint, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns), table1 ~ (Drop constraint constraints :=> columns)) | |
=> Alias constraint | constraint to drop |
-> AlterTable sch tab schemas table1 |
A dropConstraint
drops a table constraint.
>>>
:{
let definition :: Definition '["public" ::: '["tab" ::: 'Table ('["positive" ::: Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]] '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]] definition = alterTable #tab (dropConstraint #positive) in printSQL definition :} ALTER TABLE "tab" DROP CONSTRAINT "positive";
class AddColumn ty where Source #
An AddColumn
is either NULL
or has DEFAULT
.
Minimal complete definition
Nothing
Methods
Arguments
:: (KnownSymbol column, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns)) | |
=> Alias column | column to add |
-> ColumnTypeExpression schemas ty | type of the new column |
-> AlterTable sch tab schemas (constraints :=> Create column ty columns) |
addColumn
adds a new column, initially filled with whatever
default value is given or with NULL
.
>>>
:{
let definition :: Definition '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]] '["public" ::: '["tab" ::: 'Table ('[] :=> '[ "col1" ::: 'NoDef :=> 'Null 'PGint4 , "col2" ::: 'Def :=> 'Null 'PGtext ])]] definition = alterTable #tab (addColumn #col2 (text & nullable & default_ "foo")) in printSQL definition :} ALTER TABLE "tab" ADD COLUMN "col2" text NULL DEFAULT E'foo';
>>>
:{
let definition :: Definition '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]] '["public" ::: '["tab" ::: 'Table ('[] :=> '[ "col1" ::: 'NoDef :=> 'Null 'PGint4 , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]] definition = alterTable #tab (addColumn #col2 (text & nullable)) in printSQL definition :} ALTER TABLE "tab" ADD COLUMN "col2" text NULL;
Arguments
:: (KnownSymbol column, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns), table1 ~ (constraints :=> Drop column columns)) | |
=> Alias column | column to remove |
-> AlterTable sch tab schemas table1 |
A dropColumn
removes a column. Whatever data was in the column
disappears. Table constraints involving the column are dropped, too.
However, if the column is referenced by a foreign key constraint of
another table, PostgreSQL will not silently drop that constraint.
>>>
:{
let definition :: Definition '["public" ::: '["tab" ::: 'Table ('[] :=> '[ "col1" ::: 'NoDef :=> 'Null 'PGint4 , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]] '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]] definition = alterTable #tab (dropColumn #col2) in printSQL definition :} ALTER TABLE "tab" DROP COLUMN "col2";
Arguments
:: (KnownSymbol column0, KnownSymbol column1, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns), table1 ~ (constraints :=> Rename column0 column1 columns)) | |
=> Alias column0 | column to rename |
-> Alias column1 | what to rename the column |
-> AlterTable sch tab schemas table1 |
A renameColumn
renames a column.
>>>
:{
let definition :: Definition '["public" ::: '["tab" ::: 'Table ('[] :=> '["foo" ::: 'NoDef :=> 'Null 'PGint4])]] '["public" ::: '["tab" ::: 'Table ('[] :=> '["bar" ::: 'NoDef :=> 'Null 'PGint4])]] definition = alterTable #tab (renameColumn #foo #bar) in printSQL definition :} ALTER TABLE "tab" RENAME COLUMN "foo" TO "bar";
Arguments
:: (KnownSymbol column, Has sch schemas schema, Has tab schema (Table table0), table0 ~ (constraints :=> columns), Has column columns ty0, table1 ~ (constraints :=> Alter column ty1 columns)) | |
=> Alias column | column to alter |
-> AlterColumn schemas ty0 ty1 | alteration to perform |
-> AlterTable sch tab schemas table1 |
An alterColumn
alters a single column.
newtype AlterColumn (schemas :: SchemasType) (ty0 :: ColumnType) (ty1 :: ColumnType) Source #
An AlterColumn
describes the alteration to perform on a single column.
Constructors
UnsafeAlterColumn | |
Fields |
Instances
Eq (AlterColumn schemas ty0 ty1) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods (==) :: AlterColumn schemas ty0 ty1 -> AlterColumn schemas ty0 ty1 -> Bool # (/=) :: AlterColumn schemas ty0 ty1 -> AlterColumn schemas ty0 ty1 -> Bool # | |
Ord (AlterColumn schemas ty0 ty1) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods compare :: AlterColumn schemas ty0 ty1 -> AlterColumn schemas ty0 ty1 -> Ordering # (<) :: AlterColumn schemas ty0 ty1 -> AlterColumn schemas ty0 ty1 -> Bool # (<=) :: AlterColumn schemas ty0 ty1 -> AlterColumn schemas ty0 ty1 -> Bool # (>) :: AlterColumn schemas ty0 ty1 -> AlterColumn schemas ty0 ty1 -> Bool # (>=) :: AlterColumn schemas ty0 ty1 -> AlterColumn schemas ty0 ty1 -> Bool # max :: AlterColumn schemas ty0 ty1 -> AlterColumn schemas ty0 ty1 -> AlterColumn schemas ty0 ty1 # min :: AlterColumn schemas ty0 ty1 -> AlterColumn schemas ty0 ty1 -> AlterColumn schemas ty0 ty1 # | |
Show (AlterColumn schemas ty0 ty1) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods showsPrec :: Int -> AlterColumn schemas ty0 ty1 -> ShowS # show :: AlterColumn schemas ty0 ty1 -> String # showList :: [AlterColumn schemas ty0 ty1] -> ShowS # | |
Generic (AlterColumn schemas ty0 ty1) Source # | |
Defined in Squeal.PostgreSQL.Definition Associated Types type Rep (AlterColumn schemas ty0 ty1) :: Type -> Type # Methods from :: AlterColumn schemas ty0 ty1 -> Rep (AlterColumn schemas ty0 ty1) x # to :: Rep (AlterColumn schemas ty0 ty1) x -> AlterColumn schemas ty0 ty1 # | |
NFData (AlterColumn schemas ty0 ty1) Source # | |
Defined in Squeal.PostgreSQL.Definition Methods rnf :: AlterColumn schemas ty0 ty1 -> () # | |
type Rep (AlterColumn schemas ty0 ty1) Source # | |
Defined in Squeal.PostgreSQL.Definition type Rep (AlterColumn schemas ty0 ty1) = D1 (MetaData "AlterColumn" "Squeal.PostgreSQL.Definition" "squeal-postgresql-0.5.2.0-4fAomBtpMjd6mRwLthA4w2" True) (C1 (MetaCons "UnsafeAlterColumn" PrefixI True) (S1 (MetaSel (Just "renderAlterColumn") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 ByteString))) |
Arguments
:: Expression '[] '[] Ungrouped schemas '[] '[] ty | default value to set |
-> AlterColumn schemas (constraint :=> ty) (Def :=> ty) |
A setDefault
sets a new default for a column. Note that this doesn't
affect any existing rows in the table, it just changes the default for
future insert and update commands.
>>>
:{
let definition :: Definition '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]] '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'Def :=> 'Null 'PGint4])]] definition = alterTable #tab (alterColumn #col (setDefault 5)) in printSQL definition :} ALTER TABLE "tab" ALTER COLUMN "col" SET DEFAULT 5;
dropDefault :: AlterColumn schemas (Def :=> ty) (NoDef :=> ty) Source #
A dropDefault
removes any default value for a column.
>>>
:{
let definition :: Definition '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'Def :=> 'Null 'PGint4])]] '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]] definition = alterTable #tab (alterColumn #col dropDefault) in printSQL definition :} ALTER TABLE "tab" ALTER COLUMN "col" DROP DEFAULT;
setNotNull :: AlterColumn schemas (constraint :=> Null ty) (constraint :=> NotNull ty) Source #
A setNotNull
adds a NOT NULL
constraint to a column.
The constraint will be checked immediately, so the table data must satisfy
the constraint before it can be added.
>>>
:{
let definition :: Definition '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]] '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]] definition = alterTable #tab (alterColumn #col setNotNull) in printSQL definition :} ALTER TABLE "tab" ALTER COLUMN "col" SET NOT NULL;
dropNotNull :: AlterColumn schemas (constraint :=> NotNull ty) (constraint :=> Null ty) Source #
A dropNotNull
drops a NOT NULL
constraint from a column.
>>>
:{
let definition :: Definition '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]] '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]] definition = alterTable #tab (alterColumn #col dropNotNull) in printSQL definition :} ALTER TABLE "tab" ALTER COLUMN "col" DROP NOT NULL;
alterType :: ColumnTypeExpression schemas ty -> AlterColumn schemas ty0 ty Source #
An alterType
converts a column to a different data type.
This will succeed only if each existing entry in the column can be
converted to the new type by an implicit cast.
>>>
:{
let definition :: Definition '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]] '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGnumeric])]] definition = alterTable #tab (alterColumn #col (alterType (numeric & notNullable))) in printSQL definition :} ALTER TABLE "tab" ALTER COLUMN "col" TYPE numeric NOT NULL;
Columns
newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType) Source #
ColumnTypeExpression
s are used in createTable
commands.
Constructors
UnsafeColumnTypeExpression | |
Fields |
Instances
nullable :: TypeExpression schemas (nullity ty) -> ColumnTypeExpression schemas (NoDef :=> Null ty) Source #
used in createTable
commands as a column constraint to note that
NULL
may be present in a column
notNullable :: TypeExpression schemas (nullity ty) -> ColumnTypeExpression schemas (NoDef :=> NotNull ty) Source #
used in createTable
commands as a column constraint to ensure
NULL
is not present in a column
default_ :: Expression '[] '[] Ungrouped schemas '[] '[] ty -> ColumnTypeExpression schemas (NoDef :=> ty) -> ColumnTypeExpression schemas (Def :=> ty) Source #
used in createTable
commands as a column constraint to give a default
serial2 :: ColumnTypeExpression schemas (Def :=> NotNull PGint2) Source #
not a true type, but merely a notational convenience for creating
unique identifier columns with type PGint2
smallserial :: ColumnTypeExpression schemas (Def :=> NotNull PGint2) Source #
not a true type, but merely a notational convenience for creating
unique identifier columns with type PGint2
serial4 :: ColumnTypeExpression schemas (Def :=> NotNull PGint4) Source #
not a true type, but merely a notational convenience for creating
unique identifier columns with type PGint4
serial :: ColumnTypeExpression schemas (Def :=> NotNull PGint4) Source #
not a true type, but merely a notational convenience for creating
unique identifier columns with type PGint4