What would it be like if you could add the keyword "STRICT" after a CREATE

TABLE statement to cause the table to (1) allow only a few well-defined
datatypes, (2) rigidly enforce those types, (3) require NOT NULL on PK
columns, (4) always enforce foreign key constraint, and so forth?  This
branch seeks to explore that question.

FossilOrigin-Name: 78732b9f98936693ae29c85a692c35a84c7d065aec79903af34b08d18f10a5e6
This commit is contained in:
drh
2021-08-18 13:13:58 +00:00
parent b70f2eabb4
commit 44183f83d0
6 changed files with 87 additions and 15 deletions
+16 -1
View File
@@ -2554,7 +2554,7 @@ void sqlite3EndTable(
Parse *pParse, /* Parse context */
Token *pCons, /* The ',' token after the last column defn. */
Token *pEnd, /* The ')' before options in the CREATE TABLE */
u8 tabOpts, /* Extra table options. Usually 0. */
u32 tabOpts, /* Extra table options. Usually 0. */
Select *pSelect /* Select from a "CREATE ... AS SELECT" */
){
Table *p; /* The new table */
@@ -2590,6 +2590,21 @@ void sqlite3EndTable(
if( p->tnum==1 ) p->tabFlags |= TF_Readonly;
}
/* Do not allow COLTYPE_CUSTOM in STRICT mode */
if( tabOpts & TF_Strict ){
int ii;
p->tabFlags |= TF_Strict;
for(ii=0; ii<p->nCol; ii++){
if( p->aCol[ii].eCType==COLTYPE_CUSTOM ){
sqlite3ErrorMsg(pParse,
"unknown datatype for %s.%s: \"%s\"",
p->zName, p->aCol[ii].zCnName, sqlite3ColumnType(p->aCol+ii, "")
);
return;
}
}
}
assert( (p->tabFlags & TF_HasPrimaryKey)==0
|| p->iPKey>=0 || sqlite3PrimaryKeyIndex(p)!=0 );
assert( (p->tabFlags & TF_HasPrimaryKey)!=0
+15 -4
View File
@@ -196,16 +196,19 @@ ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
temp(A) ::= TEMP. {A = pParse->db->init.busy==0;}
%endif SQLITE_OMIT_TEMPDB
temp(A) ::= . {A = 0;}
create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_options(F). {
create_table_args ::= LP columnlist conslist_opt(X) RP(E) table_option_set(F). {
sqlite3EndTable(pParse,&X,&E,F,0);
}
create_table_args ::= AS select(S). {
sqlite3EndTable(pParse,0,0,0,S);
sqlite3SelectDelete(pParse->db, S);
}
%type table_options {int}
table_options(A) ::= . {A = 0;}
table_options(A) ::= WITHOUT nm(X). {
%type table_option_set {u32}
%type table_option {u32}
table_option_set(A) ::= . {A = 0;}
table_option_set(A) ::= table_option(A).
table_option_set(A) ::= table_option_set(X) COMMA table_option(Y). {A = X|Y;}
table_option(A) ::= WITHOUT nm(X). {
if( X.n==5 && sqlite3_strnicmp(X.z,"rowid",5)==0 ){
A = TF_WithoutRowid | TF_NoVisibleRowid;
}else{
@@ -213,6 +216,14 @@ table_options(A) ::= WITHOUT nm(X). {
sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
}
}
table_option(A) ::= nm(X). {
if( X.n==6 && sqlite3_strnicmp(X.z,"strict",6)==0 ){
A = TF_Strict;
}else{
A = 0;
sqlite3ErrorMsg(pParse, "unknown table option: %.*s", X.n, X.z);
}
}
columnlist ::= columnlist COMMA columnname carglist.
columnlist ::= columnname carglist.
columnname(A) ::= nm(A) typetoken(Y). {sqlite3AddColumn(pParse,A,Y);}
+2 -1
View File
@@ -2284,6 +2284,7 @@ struct Table {
#define TF_HasStat4 0x00002000 /* STAT4 info available for this table */
#define TF_Ephemeral 0x00004000 /* An ephemeral table */
#define TF_Eponymous 0x00008000 /* An eponymous virtual table */
#define TF_Strict 0x00010000 /* STRICT mode */
/*
** Allowed values for Table.eTabType
@@ -4435,7 +4436,7 @@ void sqlite3AddCheckConstraint(Parse*, Expr*, const char*, const char*);
void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
void sqlite3AddCollateType(Parse*, Token*);
void sqlite3AddGenerated(Parse*,Expr*,Token*);
void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
void sqlite3EndTable(Parse*,Token*,Token*,u32,Select*);
void sqlite3AddReturning(Parse*,ExprList*);
int sqlite3ParseUri(const char*,const char*,unsigned int*,
sqlite3_vfs**,char**,char **);