Disallow subqueries in CHECK constraints. (CVS 2756)

FossilOrigin-Name: db27afc4cdc5b51c1fa0e83dbd6d4a4a69c5b642
diff --git a/src/build.c b/src/build.c
index 11b7a03..b6ecf0f 100644
--- a/src/build.c
+++ b/src/build.c
@@ -22,7 +22,7 @@
 **     COMMIT
 **     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 <ctype.h>
@@ -1307,6 +1307,7 @@
     sSrc.a[0].iCursor = -1;
     sNC.pParse = pParse;
     sNC.pSrcList = &sSrc;
+    sNC.isCheck = 1;
     if( sqlite3ExprResolveNames(&sNC, p->pCheck) ){
       return;
     }
diff --git a/src/expr.c b/src/expr.c
index 88c48cf..7ac8393 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -12,7 +12,7 @@
 ** This file contains routines used for analyzing expressions and
 ** 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 <ctype.h>
@@ -1188,6 +1188,11 @@
 #endif
     case TK_IN: {
       if( pExpr->pSelect ){
+#ifndef SQLITE_OMIT_CHECK
+        if( pNC->isCheck ){
+          sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
+        }
+#endif
         int nRef = pNC->nRef;
         sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
         assert( pNC->nRef>=nRef );
diff --git a/src/parse.y b/src/parse.y
index 490ceb2..b7a7e45 100644
--- a/src/parse.y
+++ b/src/parse.y
@@ -14,7 +14,7 @@
 ** the parser.  Lemon will also generate a header file containing
 ** 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_"
@@ -268,7 +268,7 @@
 ccons ::= PRIMARY KEY sortorder onconf(R) autoinc(I).
                                      {sqlite3AddPrimaryKey(pParse,0,R,I);}
 ccons ::= UNIQUE onconf(R).          {sqlite3CreateIndex(pParse,0,0,0,0,R,0,0);}
-ccons ::= CHECK LP expr(X) RP.       {sqlite3AddCheckConstraint(pParse, X);}
+ccons ::= CHECK LP expr(X) RP.       {sqlite3AddCheckConstraint(pParse,X);}
 ccons ::= REFERENCES nm(T) idxlist_opt(TA) refargs(R).
                                 {sqlite3CreateForeignKey(pParse,0,&T,TA,R);}
 ccons ::= defer_subclause(D).   {sqlite3DeferForeignKey(pParse,D);}
@@ -318,7 +318,7 @@
                                          {sqlite3AddPrimaryKey(pParse,X,R,I);}
 tcons ::= UNIQUE LP idxlist(X) RP onconf(R).
                                        {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
           REFERENCES nm(T) idxlist_opt(TA) refargs(R) defer_subclause_opt(D). {
     sqlite3CreateForeignKey(pParse, FA, &T, TA, R);
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index 048d070..3cb1aef 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -11,7 +11,7 @@
 *************************************************************************
 ** 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_
 #define _SQLITEINT_H_
@@ -1088,6 +1088,7 @@
   int nErr;            /* Number of errors encountered while resolving names */
   u8 allowAgg;         /* Aggregate functions allowed here */
   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 */
   AggInfo *pAggInfo;   /* Information about aggregates at this level */
   NameContext *pNext;  /* Next outer name context.  NULL for outermost */