1
Fork 0

add table constraint definitions to create table

This commit is contained in:
Jake Wheat 2015-08-02 20:36:05 +03:00
parent e6e8264b3d
commit fa1df4c7a2
4 changed files with 230 additions and 77 deletions
tools/Language/SQL/SimpleSQL

View file

@ -25,6 +25,10 @@ This module covers the tests for parsing schema and DDL statements.
todo: schema name can have .
schema name can be quoted iden or unicode quoted iden
add schema element support:
create a list of schema elements
then do pairwise combinations in schema element list to test
<schema character set or path> ::=
<schema character set specification>
@ -310,25 +314,25 @@ todo: constraint characteristics
> "create table t (a int not null);"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing NotNullConstraint]])
> [ColConstraintDef Nothing ColNotNullConstraint]])
> ,(TestStatement SQL2011
> "create table t (a int constraint a_not_null not null);"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef (Just [Name "a_not_null"]) NotNullConstraint]])
> [ColConstraintDef (Just [Name "a_not_null"]) ColNotNullConstraint]])
> ,(TestStatement SQL2011
> "create table t (a int unique);"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing UniqueConstraint]])
> [ColConstraintDef Nothing ColUniqueConstraint]])
> ,(TestStatement SQL2011
> "create table t (a int primary key);"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing PrimaryKeyConstraint]])
> [ColConstraintDef Nothing ColPrimaryKeyConstraint]])
references t(a,b)
[ Full |partial| simepl]
@ -339,7 +343,7 @@ references t(a,b)
> "create table t (a int references u);"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] Nothing DefaultReferenceMatch
> DefaultReferentialAction DefaultReferentialAction]])
@ -347,7 +351,7 @@ references t(a,b)
> "create table t (a int references u(a));"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] (Just $ Name "a") DefaultReferenceMatch
> DefaultReferentialAction DefaultReferentialAction]])
@ -355,7 +359,7 @@ references t(a,b)
> "create table t (a int references u match full);"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] Nothing MatchFull
> DefaultReferentialAction DefaultReferentialAction]])
@ -363,7 +367,7 @@ references t(a,b)
> "create table t (a int references u match partial);"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] Nothing MatchPartial
> DefaultReferentialAction DefaultReferentialAction]])
@ -371,7 +375,7 @@ references t(a,b)
> "create table t (a int references u match simple);"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] Nothing MatchSimple
> DefaultReferentialAction DefaultReferentialAction]])
@ -379,7 +383,7 @@ references t(a,b)
> "create table t (a int references u on update cascade );"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] Nothing DefaultReferenceMatch
> RefCascade DefaultReferentialAction]])
@ -387,7 +391,7 @@ references t(a,b)
> "create table t (a int references u on update set null );"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] Nothing DefaultReferenceMatch
> RefSetNull DefaultReferentialAction]])
@ -395,7 +399,7 @@ references t(a,b)
> "create table t (a int references u on update set default );"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] Nothing DefaultReferenceMatch
> RefSetDefault DefaultReferentialAction]])
@ -403,7 +407,7 @@ references t(a,b)
> "create table t (a int references u on update no action );"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] Nothing DefaultReferenceMatch
> RefNoAction DefaultReferentialAction]])
@ -411,7 +415,7 @@ references t(a,b)
> "create table t (a int references u on delete cascade );"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] Nothing DefaultReferenceMatch
> DefaultReferentialAction RefCascade]])
@ -420,7 +424,7 @@ references t(a,b)
> "create table t (a int references u on update cascade on delete restrict );"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] Nothing DefaultReferenceMatch
> RefCascade RefRestrict]])
@ -428,19 +432,22 @@ references t(a,b)
> "create table t (a int references u on delete restrict on update cascade );"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing $ ReferencesConstraint
> [ColConstraintDef Nothing $ ColReferencesConstraint
> [Name "u"] Nothing DefaultReferenceMatch
> RefCascade RefRestrict]])
TODO: try combinations and permutations of column constraints and
options
> ,(TestStatement SQL2011
> "create table t (a int check (a>5));"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing
> [ConstraintDef Nothing
> (CheckConstraint $ BinOp (Iden [Name "a"]) [Name ">"] (NumLit "5"))]])
> [ColConstraintDef Nothing
> (ColCheckConstraint $ BinOp (Iden [Name "a"]) [Name ">"] (NumLit "5"))]])
check (valueexpr)
@ -465,7 +472,7 @@ check (valueexpr)
> ,(TestStatement SQL2011
> "create table t (a int generated as identity\
> "create table t (a int generated as identity\n\
> \ ( start with 5 increment by 5 maxvalue 500 minvalue 5 cycle ));"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"])
@ -477,7 +484,7 @@ check (valueexpr)
> ,SGOCycle]) []])
> ,(TestStatement SQL2011
> "create table t (a int generated as identity\
> "create table t (a int generated as identity\n\
> \ ( start with -4 no maxvalue no minvalue no cycle ));"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"])
@ -505,7 +512,7 @@ generated always (valueexpr)
<left paren> <value expression> <right paren>
> ,(TestStatement SQL2011
> "create table t (a int, \
> "create table t (a int, \n\
> \ a2 int generated always as (a * 2));"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing []
@ -566,6 +573,42 @@ generated always (valueexpr)
<unique column list> ::=
<column name list>
> ,(TestStatement SQL2011
> "create table t (a int, unique (a));"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing []
> ,TableConstraintDef Nothing $ TableUniqueConstraint [Name "a"]
> ])
> ,(TestStatement SQL2011
> "create table t (a int, constraint a_unique unique (a));"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing []
> ,TableConstraintDef (Just [Name "a_unique"]) $
> TableUniqueConstraint [Name "a"]
> ])
todo: test permutations of column defs and table constraints
> ,(TestStatement SQL2011
> "create table t (a int, b int, unique (a,b));"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing []
> ,ColumnDef (Name "b") (TypeName [Name "int"]) Nothing []
> ,TableConstraintDef Nothing $
> TableUniqueConstraint [Name "a", Name "b"]
> ])
> ,(TestStatement SQL2011
> "create table t (a int, b int, primary key (a,b));"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing []
> ,ColumnDef (Name "b") (TypeName [Name "int"]) Nothing []
> ,TableConstraintDef Nothing $
> TablePrimaryKeyConstraint [Name "a", Name "b"]
> ])
<without overlap specification> ::=
<application time period name> WITHOUT OVERLAPS
@ -579,6 +622,36 @@ defintely skip
[ <comma> <referencing period specification> ] <right paren>
<references specification>
> ,(TestStatement SQL2011
> "create table t (a int, b int,\n\
> \ foreign key (a,b) references u(c,d) match full on update cascade on delete restrict );"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing []
> ,ColumnDef (Name "b") (TypeName [Name "int"]) Nothing []
> ,TableConstraintDef Nothing $
> TableReferencesConstraint
> [Name "a", Name "b"]
> [Name "u"]
> (Just [Name "c", Name "d"])
> MatchFull RefCascade RefRestrict
> ])
> ,(TestStatement SQL2011
> "create table t (a int,\n\
> \ constraint tfku1 foreign key (a) references u);"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing []
> ,TableConstraintDef (Just [Name "tfku1"]) $
> TableReferencesConstraint
> [Name "a"]
> [Name "u"]
> Nothing DefaultReferenceMatch
> DefaultReferentialAction DefaultReferentialAction
> ])
<references specification> ::=
REFERENCES <referenced table and columns>
[ MATCH <match type> ] [ <referential triggered action> ]
@ -625,10 +698,40 @@ defintely skip
| RESTRICT
| NO ACTION
11.9 <check constraint definition>
<check constraint definition> ::=
CHECK <left paren> <search condition> <right paren>
> ,(TestStatement SQL2011
> "create table t (a int, b int, \n\
> \ check (a > b));"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing []
> ,ColumnDef (Name "b") (TypeName [Name "int"]) Nothing []
> ,TableConstraintDef Nothing $
> TableCheckConstraint
> (BinOp (Iden [Name "a"]) [Name ">"] (Iden [Name "b"]))
> ])
> ,(TestStatement SQL2011
> "create table t (a int, b int, \n\
> \ constraint agtb check (a > b));"
> $ CreateTable [Name "t"]
> [ColumnDef (Name "a") (TypeName [Name "int"]) Nothing []
> ,ColumnDef (Name "b") (TypeName [Name "int"]) Nothing []
> ,TableConstraintDef (Just [Name "agtb"]) $
> TableCheckConstraint
> (BinOp (Iden [Name "a"]) [Name ">"] (Iden [Name "b"]))
> ])
TODO: lots more combos of table elements
and types and the other bits in a column def
11.10 <alter table statement>
<alter table statement> ::=