1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-08 03:22:21 +03:00

Disallow subqueries in CHECK constraints. (CVS 2756)

FossilOrigin-Name: db27afc4cdc5b51c1fa0e83dbd6d4a4a69c5b642
This commit is contained in:
drh
2005-11-03 02:03:13 +00:00
parent 6275b88b9e
commit 06f6541e2f
7 changed files with 131 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
C CHECK\sconstraints\sthat\sevaluate\sto\sNULL\spass.\s(CVS\s2755) C Disallow\ssubqueries\sin\sCHECK\sconstraints.\s(CVS\s2756)
D 2005-11-03T01:22:31 D 2005-11-03T02:03:13
F Makefile.in 12784cdce5ffc8dfb707300c34e4f1eb3b8a14f1 F Makefile.in 12784cdce5ffc8dfb707300c34e4f1eb3b8a14f1
F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092 F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -35,13 +35,13 @@ F src/attach.c 4b21689700a72ae281fa85dbaff06b2a62bd49ee
F src/auth.c 31e2304bef67f44d635655f44234387ea7d21454 F src/auth.c 31e2304bef67f44d635655f44234387ea7d21454
F src/btree.c 1ccc3b3931774a68ee0d6a8e2c8ea83f27b853fb F src/btree.c 1ccc3b3931774a68ee0d6a8e2c8ea83f27b853fb
F src/btree.h 1ed561263ca0e335bc3e81d761c9d5ff8c22f61e F src/btree.h 1ed561263ca0e335bc3e81d761c9d5ff8c22f61e
F src/build.c 5441ae700097557051c40a1d7a67d170f19d94f3 F src/build.c 03ccfb2d5addaa6ba7d1155cf4f27938e54b8270
F src/callback.c 90ab4f235a2603c4cb8e6a2497091a71fb732bfa F src/callback.c 90ab4f235a2603c4cb8e6a2497091a71fb732bfa
F src/complete.c 4de937dfdd4c79a501772ab2035b26082f337a79 F src/complete.c 4de937dfdd4c79a501772ab2035b26082f337a79
F src/date.c 7444b0900a28da77e57e3337a636873cff0ae940 F src/date.c 7444b0900a28da77e57e3337a636873cff0ae940
F src/delete.c 29dac493f4d83b05f91233b116827c133bcdab72 F src/delete.c 29dac493f4d83b05f91233b116827c133bcdab72
F src/experimental.c 50c1e3b34f752f4ac10c36f287db095c2b61766d F src/experimental.c 50c1e3b34f752f4ac10c36f287db095c2b61766d
F src/expr.c e74e9c265c3572f71ab9d8f795cb656469209bb6 F src/expr.c 14466b227096263213d7748174c2bd8cb722cc3c
F src/func.c 7d81dccd9c440c6c4e761056333e629192814af0 F src/func.c 7d81dccd9c440c6c4e761056333e629192814af0
F src/hash.c 8747cf51d12de46512880dfcf1b68b4e24072863 F src/hash.c 8747cf51d12de46512880dfcf1b68b4e24072863
F src/hash.h 1b0c445e1c89ff2aaad9b4605ba61375af001e84 F src/hash.h 1b0c445e1c89ff2aaad9b4605ba61375af001e84
@@ -59,7 +59,7 @@ F src/os_win.c fbccc85e7011174068c27d54256746321a1f0059
F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b
F src/pager.c adbb27f13ac75cd5bc29a3d84803e0cab1edfa88 F src/pager.c adbb27f13ac75cd5bc29a3d84803e0cab1edfa88
F src/pager.h e7b41ce8e7b5f629d456708b7ad9a8c8ede37140 F src/pager.h e7b41ce8e7b5f629d456708b7ad9a8c8ede37140
F src/parse.y 416bc5ed6239356173d7283881750f6b7ed2b899 F src/parse.y 1d5afb972ed5ef3ec8712a2b5953b48f086091da
F src/pragma.c 9ec219dc4ee2d4e78f4ec5c9d1422089758af13f F src/pragma.c 9ec219dc4ee2d4e78f4ec5c9d1422089758af13f
F src/prepare.c fc098db25d2a121affb08686cf04833fd50452d4 F src/prepare.c fc098db25d2a121affb08686cf04833fd50452d4
F src/printf.c 3ea3a17d25d7ac498efc18007c70371a42c968f8 F src/printf.c 3ea3a17d25d7ac498efc18007c70371a42c968f8
@@ -67,7 +67,7 @@ F src/random.c 90adff4e73a3b249eb4f1fc2a6ff9cf78c7233a4
F src/select.c be02f123e8651bee22beb07d89dcfa75bcc2e291 F src/select.c be02f123e8651bee22beb07d89dcfa75bcc2e291
F src/shell.c 3596c1e559b82663057940d19ba533ad421c7dd3 F src/shell.c 3596c1e559b82663057940d19ba533ad421c7dd3
F src/sqlite.h.in 8e648e1f386e4509f2f96c09ded7c07b0df0c9a2 F src/sqlite.h.in 8e648e1f386e4509f2f96c09ded7c07b0df0c9a2
F src/sqliteInt.h d4f226034a9f1b2414aed408908984008db5c44f F src/sqliteInt.h c6685ddb841f9b5585758642b6ae32a7428c7936
F src/table.c e03b60eaabaeb54a00d7e931566d77302dfc19b0 F src/table.c e03b60eaabaeb54a00d7e931566d77302dfc19b0
F src/tclsqlite.c 4f274fae3d4a1863451a553dd8e5015747a5d91d F src/tclsqlite.c 4f274fae3d4a1863451a553dd8e5015747a5d91d
F src/test1.c 77506b6b88125c26f00a11bf3ff5c8dc824e837e F src/test1.c 77506b6b88125c26f00a11bf3ff5c8dc824e837e
@@ -124,7 +124,7 @@ F test/capi2.test f897209386fb21cfdc9267595e0c667ebaca9164
F test/capi3.test fc8e573467049add3bfaf81f53827e8ff153cf8f F test/capi3.test fc8e573467049add3bfaf81f53827e8ff153cf8f
F test/capi3b.test 5b6a66f9f295f79f443b5d3f33187fa5ef6cf336 F test/capi3b.test 5b6a66f9f295f79f443b5d3f33187fa5ef6cf336
F test/cast.test 2543165ced4249c89ce5f0352222df503a98b9e5 F test/cast.test 2543165ced4249c89ce5f0352222df503a98b9e5
F test/check.test d7f0850650b7c0adda71cbc99eba4407475a9424 F test/check.test c591a20fcb082ae7362299c00b4fb85536f62f1b
F test/collate1.test add9454cef160677bb8b34148b8f277ce7f9f1c4 F test/collate1.test add9454cef160677bb8b34148b8f277ce7f9f1c4
F test/collate2.test 224a632ba04907c049804b08162efd234aa7871f F test/collate2.test 224a632ba04907c049804b08162efd234aa7871f
F test/collate3.test 947a77f5b8227e037a7094d0e338a5504f155cc4 F test/collate3.test 947a77f5b8227e037a7094d0e338a5504f155cc4
@@ -316,7 +316,7 @@ F www/tclsqlite.tcl ddcf912ea48695603c8ed7efb29f0812ef8d1b49
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b
F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
P 2313d912baeca0fd516d524f16708953de483729 P 55b314a22c69fbe129b024e953f3230b67eaaa87
R d64542a6a946813b7fd22cace2c7e954 R 8803f664e97429fe76d644ff77ce2e9b
U drh U drh
Z 1f3c50f7b443762a810db649999a6322 Z a81fd0388a861af771476f6fd322db14

View File

@@ -1 +1 @@
55b314a22c69fbe129b024e953f3230b67eaaa87 db27afc4cdc5b51c1fa0e83dbd6d4a4a69c5b642

View File

@@ -22,7 +22,7 @@
** COMMIT ** COMMIT
** ROLLBACK ** ROLLBACK
** **
** $Id: build.c,v 1.353 2005/11/03 00:41:17 drh Exp $ ** $Id: build.c,v 1.354 2005/11/03 02:03:13 drh Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#include <ctype.h> #include <ctype.h>
@@ -1307,6 +1307,7 @@ void sqlite3EndTable(
sSrc.a[0].iCursor = -1; sSrc.a[0].iCursor = -1;
sNC.pParse = pParse; sNC.pParse = pParse;
sNC.pSrcList = &sSrc; sNC.pSrcList = &sSrc;
sNC.isCheck = 1;
if( sqlite3ExprResolveNames(&sNC, p->pCheck) ){ if( sqlite3ExprResolveNames(&sNC, p->pCheck) ){
return; return;
} }

View File

@@ -12,7 +12,7 @@
** This file contains routines used for analyzing expressions and ** This file contains routines used for analyzing expressions and
** for generating VDBE code that evaluates expressions in SQLite. ** for generating VDBE code that evaluates expressions in SQLite.
** **
** $Id: expr.c,v 1.233 2005/11/03 00:41:17 drh Exp $ ** $Id: expr.c,v 1.234 2005/11/03 02:03:13 drh Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
#include <ctype.h> #include <ctype.h>
@@ -1188,6 +1188,11 @@ static int nameResolverStep(void *pArg, Expr *pExpr){
#endif #endif
case TK_IN: { case TK_IN: {
if( pExpr->pSelect ){ if( pExpr->pSelect ){
#ifndef SQLITE_OMIT_CHECK
if( pNC->isCheck ){
sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
}
#endif
int nRef = pNC->nRef; int nRef = pNC->nRef;
sqlite3SelectResolve(pParse, pExpr->pSelect, pNC); sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
assert( pNC->nRef>=nRef ); assert( pNC->nRef>=nRef );

View File

@@ -14,7 +14,7 @@
** the parser. Lemon will also generate a header file containing ** the parser. Lemon will also generate a header file containing
** numeric codes for all of the tokens. ** numeric codes for all of the tokens.
** **
** @(#) $Id: parse.y,v 1.181 2005/11/03 00:41:17 drh Exp $ ** @(#) $Id: parse.y,v 1.182 2005/11/03 02:03:13 drh Exp $
*/ */
// All token codes are small integers with #defines that begin with "TK_" // All token codes are small integers with #defines that begin with "TK_"
@@ -318,7 +318,7 @@ tcons ::= PRIMARY KEY LP idxlist(X) autoinc(I) RP onconf(R).
{sqlite3AddPrimaryKey(pParse,X,R,I);} {sqlite3AddPrimaryKey(pParse,X,R,I);}
tcons ::= UNIQUE LP idxlist(X) RP onconf(R). tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
{sqlite3CreateIndex(pParse,0,0,0,X,R,0,0);} {sqlite3CreateIndex(pParse,0,0,0,X,R,0,0);}
tcons ::= CHECK expr onconf. tcons ::= CHECK LP expr(E) RP onconf. {sqlite3AddCheckConstraint(pParse,E);}
tcons ::= FOREIGN KEY LP idxlist(FA) RP tcons ::= FOREIGN KEY LP idxlist(FA) RP
REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). { REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
sqlite3CreateForeignKey(pParse, FA, &T, TA, R); sqlite3CreateForeignKey(pParse, FA, &T, TA, R);

View File

@@ -11,7 +11,7 @@
************************************************************************* *************************************************************************
** Internal interface definitions for SQLite. ** Internal interface definitions for SQLite.
** **
** @(#) $Id: sqliteInt.h,v 1.425 2005/11/03 00:41:17 drh Exp $ ** @(#) $Id: sqliteInt.h,v 1.426 2005/11/03 02:03:13 drh Exp $
*/ */
#ifndef _SQLITEINT_H_ #ifndef _SQLITEINT_H_
#define _SQLITEINT_H_ #define _SQLITEINT_H_
@@ -1088,6 +1088,7 @@ struct NameContext {
int nErr; /* Number of errors encountered while resolving names */ int nErr; /* Number of errors encountered while resolving names */
u8 allowAgg; /* Aggregate functions allowed here */ u8 allowAgg; /* Aggregate functions allowed here */
u8 hasAgg; /* True if aggregates are seen */ u8 hasAgg; /* True if aggregates are seen */
u8 isCheck; /* True if resolving names in a CHECK constraint */
int nDepth; /* Depth of subquery recursion. 1 for no recursion */ int nDepth; /* Depth of subquery recursion. 1 for no recursion */
AggInfo *pAggInfo; /* Information about aggregates at this level */ AggInfo *pAggInfo; /* Information about aggregates at this level */
NameContext *pNext; /* Next outer name context. NULL for outermost */ NameContext *pNext; /* Next outer name context. NULL for outermost */

View File

@@ -11,7 +11,7 @@
# This file implements regression tests for SQLite library. The # This file implements regression tests for SQLite library. The
# focus of this file is testing CHECK constraints # focus of this file is testing CHECK constraints
# #
# $Id: check.test,v 1.2 2005/11/03 01:22:31 drh Exp $ # $Id: check.test,v 1.3 2005/11/03 02:03:13 drh Exp $
set testdir [file dirname $argv0] set testdir [file dirname $argv0]
source $testdir/tester.tcl source $testdir/tester.tcl
@@ -151,4 +151,110 @@ do_test check-2.6 {
} }
} {1 {constraint failed}} } {1 {constraint failed}}
do_test check-3.1 {
catchsql {
CREATE TABLE t3(
x, y, z,
CHECK( x<(SELECT min(x) FROM t1) )
);
}
} {1 {subqueries prohibited in CHECK constraints}}
do_test check-3.2 {
execsql {
SELECT name FROM sqlite_master ORDER BY name
}
} {t1 t2}
do_test check-3.3 {
catchsql {
CREATE TABLE t3(
x, y, z,
CHECK( q<x )
);
}
} {1 {no such column: q}}
do_test check-3.4 {
execsql {
SELECT name FROM sqlite_master ORDER BY name
}
} {t1 t2}
do_test check-3.5 {
catchsql {
CREATE TABLE t3(
x, y, z,
CHECK( t2.x<x )
);
}
} {1 {no such column: t2.x}}
do_test check-3.6 {
execsql {
SELECT name FROM sqlite_master ORDER BY name
}
} {t1 t2}
do_test check-3.7 {
catchsql {
CREATE TABLE t3(
x, y, z,
CHECK( t3.x<25 )
);
}
} {0 {}}
do_test check-3.8 {
execsql {
INSERT INTO t3 VALUES(1,2,3);
SELECT * FROM t3;
}
} {1 2 3}
do_test check-3.9 {
catchsql {
INSERT INTO t3 VALUES(111,222,333);
}
} {1 {constraint failed}}
do_test check-4.1 {
execsql {
CREATE TABLE t4(x, y,
CHECK (
x+y==11
OR x*y==12
OR x/y BETWEEN 5 AND 8
OR -x==y+10
)
);
}
} {}
do_test check-4.2 {
execsql {
INSERT INTO t4 VALUES(1,10);
SELECT * FROM t4
}
} {1 10}
do_test check-4.3 {
execsql {
UPDATE t4 SET x=4, y=3;
SELECT * FROM t4
}
} {4 3}
do_test check-4.3 {
execsql {
UPDATE t4 SET x=12, y=2;
SELECT * FROM t4
}
} {12 2}
do_test check-4.4 {
execsql {
UPDATE t4 SET x=12, y=-22;
SELECT * FROM t4
}
} {12 -22}
do_test check-4.5 {
catchsql {
UPDATE t4 SET x=0, y=1;
}
} {1 {constraint failed}}
do_test check-4.6 {
execsql {
SELECT * FROM t4;
}
} {12 -22}
finish_test finish_test