/ Check-in [6445519e]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Clean up comments and variable names prior to merge.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | is-true-operator
Files: files | file ages | folders
SHA3-256: 6445519e91c4f98b4a9a45d5091d733c31497ebf0eb23a76edce3091f626035d
User & Date: drh 2018-02-27 14:49:25
Context
2018-02-27
15:40
Add support for TRUE and FALSE keywords and for operators IS TRUE, IS FALSE, IS NOT TRUE, and IS NOT FALSE. If there is are columns named TRUE or FALSE, then the keywords resolve to the column names, for compatibility. The behavior of the "DEFAULT true" phrase is changed to mean what it says, rather than being an alias for "DEFAULT 'true'". check-in: 9a7f02c5 user: drh tags: trunk
14:49
Clean up comments and variable names prior to merge. Closed-Leaf check-in: 6445519e user: drh tags: is-true-operator
00:58
Remove an unnecessary decision. check-in: adcb4665 user: drh tags: is-true-operator
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/expr.c.

  1730   1730     UNUSED_PARAMETER(NotUsed);
  1731   1731     pWalker->eCode = 0;
  1732   1732     return WRC_Abort;
  1733   1733   }
  1734   1734   
  1735   1735   /*
  1736   1736   ** If the input expression is an ID with the name "true" or "false"
  1737         -** then convert it into an appropriate TK_TRUEFALSE term.  Return true
  1738         -** if a conversion occurred, and false if the expression is unaltered.
         1737  +** then convert it into an TK_TRUEFALSE term.  Return non-zero if
         1738  +** the conversion happened, and zero if the expression is unaltered.
  1739   1739   */
  1740   1740   int sqlite3ExprIdToTrueFalse(Expr *pExpr){
  1741   1741     assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
  1742   1742     if( sqlite3StrICmp(pExpr->u.zToken, "true")==0
  1743   1743      || sqlite3StrICmp(pExpr->u.zToken, "false")==0
  1744   1744     ){
  1745   1745       pExpr->op = TK_TRUEFALSE;
  1746   1746       return 1;
  1747   1747     }
  1748   1748     return 0;
  1749   1749   }
  1750   1750   
  1751   1751   /*
  1752         -** The argument is one of a TK_TRUEFALSE term.  Return 1 if it is TRUE
         1752  +** The argument must be a TK_TRUEFALSE Expr node.  Return 1 if it is TRUE
  1753   1753   ** and 0 if it is FALSE.
  1754   1754   */
  1755         -int sqlite3ExprTruthOperand(const Expr *pExpr){
         1755  +int sqlite3ExprTruthValue(const Expr *pExpr){
  1756   1756     assert( pExpr->op==TK_TRUEFALSE );
  1757   1757     assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
  1758   1758          || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
  1759   1759     return pExpr->u.zToken[4]==0;
  1760   1760   }
  1761   1761   
  1762   1762   
................................................................................
  3574   3574                                  pExpr->op2);
  3575   3575       }
  3576   3576       case TK_INTEGER: {
  3577   3577         codeInteger(pParse, pExpr, 0, target);
  3578   3578         return target;
  3579   3579       }
  3580   3580       case TK_TRUEFALSE: {
  3581         -      sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthOperand(pExpr), target);
         3581  +      sqlite3VdbeAddOp2(v, OP_Integer, sqlite3ExprTruthValue(pExpr), target);
  3582   3582         return target;
  3583   3583       }
  3584   3584   #ifndef SQLITE_OMIT_FLOATING_POINT
  3585   3585       case TK_FLOAT: {
  3586   3586         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  3587   3587         codeReal(v, pExpr->u.zToken, 0, target);
  3588   3588         return target;
................................................................................
  3733   3733         assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
  3734   3734         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  3735   3735         testcase( regFree1==0 );
  3736   3736         sqlite3VdbeAddOp2(v, op, r1, inReg);
  3737   3737         break;
  3738   3738       }
  3739   3739       case TK_TRUTH: {
  3740         -      int isTrue;
         3740  +      int isTrue;    /* IS TRUE or IS NOT TRUE */
         3741  +      int bNormal;   /* IS TRUE or IS FALSE */
  3741   3742         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  3742   3743         testcase( regFree1==0 );
  3743         -      isTrue = sqlite3ExprTruthOperand(pExpr->pRight);
  3744         -      sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue,
  3745         -                           isTrue ^ (pExpr->op2==TK_IS));
         3744  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
         3745  +      bNormal = pExpr->op2==TK_IS;
         3746  +      testcase( isTrue && bNormal);
         3747  +      testcase( !isTrue && bNormal);
         3748  +      sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
  3746   3749         break;
  3747   3750       }
  3748   3751       case TK_ISNULL:
  3749   3752       case TK_NOTNULL: {
  3750   3753         int addr;
  3751   3754         assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
  3752   3755         assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
................................................................................
  4517   4520       }
  4518   4521       case TK_NOT: {
  4519   4522         testcase( jumpIfNull==0 );
  4520   4523         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  4521   4524         break;
  4522   4525       }
  4523   4526       case TK_TRUTH: {
  4524         -      int isNot;
  4525         -      int isTrue;
         4527  +      int isNot;      /* IS NOT TRUE or IS NOT FALSE */
         4528  +      int isTrue;     /* IS TRUE or IS NOT TRUE */
  4526   4529         testcase( jumpIfNull==0 );
  4527   4530         isNot = pExpr->op2==TK_ISNOT;
  4528         -      isTrue = sqlite3ExprTruthOperand(pExpr->pRight);
         4531  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
  4529   4532         testcase( isTrue && isNot );
  4530         -      testcase( isTrue && !isNot );
         4533  +      testcase( !isTrue && isNot );
  4531   4534         if( isTrue ^ isNot ){
  4532   4535           sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
  4533   4536                             isNot ? SQLITE_JUMPIFNULL : 0);
  4534   4537         }else{
  4535   4538           sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
  4536   4539                              isNot ? SQLITE_JUMPIFNULL : 0);
  4537   4540         }
................................................................................
  4688   4691       }
  4689   4692       case TK_NOT: {
  4690   4693         testcase( jumpIfNull==0 );
  4691   4694         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  4692   4695         break;
  4693   4696       }
  4694   4697       case TK_TRUTH: {
  4695         -      int isNot;
  4696         -      int isTrue;
         4698  +      int isNot;   /* IS NOT TRUE or IS NOT FALSE */
         4699  +      int isTrue;  /* IS TRUE or IS NOT TRUE */
  4697   4700         testcase( jumpIfNull==0 );
  4698         -      assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
  4699   4701         isNot = pExpr->op2==TK_ISNOT;
  4700         -      isTrue = sqlite3ExprTruthOperand(pExpr->pRight);
         4702  +      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
  4701   4703         testcase( isTrue && isNot );
  4702         -      testcase( isTrue && !isNot );
         4704  +      testcase( !isTrue && isNot );
  4703   4705         if( isTrue ^ isNot ){
  4704   4706           /* IS TRUE and IS NOT FALSE */
  4705   4707           sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
  4706   4708                              isNot ? 0 : SQLITE_JUMPIFNULL);
  4707   4709   
  4708   4710         }else{
  4709   4711           /* IS FALSE and IS NOT TRUE */
  4710   4712           sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
  4711         -                          isNot ? 0: SQLITE_JUMPIFNULL);
         4713  +                          isNot ? 0 : SQLITE_JUMPIFNULL);
  4712   4714         }
  4713   4715         break;
  4714   4716       }
  4715   4717       case TK_IS:
  4716   4718       case TK_ISNOT:
  4717   4719         testcase( pExpr->op==TK_IS );
  4718   4720         testcase( pExpr->op==TK_ISNOT );

Changes to src/parse.y.

   310    310   ccons ::= DEFAULT MINUS(A) term(X) scanpt(Z).      {
   311    311     Expr *p = sqlite3PExpr(pParse, TK_UMINUS, X, 0);
   312    312     sqlite3AddDefaultValue(pParse,p,A.z,Z);
   313    313   }
   314    314   ccons ::= DEFAULT scanpt id(X).       {
   315    315     Expr *p = tokenExpr(pParse, TK_STRING, X);
   316    316     sqlite3ExprIdToTrueFalse(p);
   317         -  testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthOperand(p) );
          317  +  testcase( p->op==TK_TRUEFALSE && sqlite3ExprTruthValue(p) );
   318    318     sqlite3AddDefaultValue(pParse,p,X.z,X.z+X.n);
   319    319   }
   320    320   
   321    321   // In addition to the type name, we also care about the primary key and
   322    322   // UNIQUE constraints.
   323    323   //
   324    324   ccons ::= NULL onconf.

Changes to src/sqliteInt.h.

  3836   3836   void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
  3837   3837   void sqlite3BeginTransaction(Parse*, int);
  3838   3838   void sqlite3EndTransaction(Parse*,int);
  3839   3839   void sqlite3Savepoint(Parse*, int, Token*);
  3840   3840   void sqlite3CloseSavepoints(sqlite3 *);
  3841   3841   void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
  3842   3842   int sqlite3ExprIdToTrueFalse(Expr*);
  3843         -int sqlite3ExprTruthOperand(const Expr*);
         3843  +int sqlite3ExprTruthValue(const Expr*);
  3844   3844   int sqlite3ExprIsConstant(Expr*);
  3845   3845   int sqlite3ExprIsConstantNotJoin(Expr*);
  3846   3846   int sqlite3ExprIsConstantOrFunction(Expr*, u8);
  3847   3847   int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
  3848   3848   int sqlite3ExprIsTableConstant(Expr*,int);
  3849   3849   #ifdef SQLITE_ENABLE_CURSOR_HINTS
  3850   3850   int sqlite3ExprContainsSubquery(Expr*);

Changes to src/treeview.c.

   290    290       }
   291    291       case TK_NULL: {
   292    292         sqlite3TreeViewLine(pView,"NULL");
   293    293         break;
   294    294       }
   295    295       case TK_TRUEFALSE: {
   296    296         sqlite3TreeViewLine(pView,
   297         -         sqlite3ExprTruthOperand(pExpr) ? "TRUE" : "FALSE");
          297  +         sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
   298    298         break;
   299    299       }
   300    300   #ifndef SQLITE_OMIT_BLOB_LITERAL
   301    301       case TK_BLOB: {
   302    302         sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
   303    303         break;
   304    304       }
................................................................................
   357    357         int x;
   358    358         const char *azOp[] = {
   359    359            "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
   360    360         };
   361    361         assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
   362    362         assert( pExpr->pRight );
   363    363         assert( pExpr->pRight->op==TK_TRUEFALSE );
   364         -      x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthOperand(pExpr->pRight);
          364  +      x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
   365    365         zUniOp = azOp[x];
   366    366         break;
   367    367       }
   368    368   
   369    369       case TK_SPAN: {
   370    370         sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
   371    371         sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);

Changes to src/vdbe.c.

  2193   2193   
  2194   2194   /* Opcode: IsTrue P1 P2 P3 P4 *
  2195   2195   ** Synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4
  2196   2196   **
  2197   2197   ** This opcode implements the IS TRUE, IS FALSE, IS NOT TRUE, and
  2198   2198   ** IS NOT FALSE operators.
  2199   2199   **
  2200         -** Interpret the value in register P1 as a boolean value.  Store the that
         2200  +** Interpret the value in register P1 as a boolean value.  Store that
  2201   2201   ** boolean (a 0 or 1) in register P2.  Or if the value in register P1 is 
  2202   2202   ** NULL, then the P3 is stored in register P2.  Invert the answer if P4
  2203   2203   ** is 1.
  2204   2204   **
  2205   2205   ** The logic is summarized like this:
  2206   2206   **
  2207   2207   ** <ul> 
  2208         -** <li> P3==0, P4==0  &rarr;  r[P2] = r[P1] IS TRUE
  2209         -** <li> P3==1, P4==1  &rarr;  r[P2] = r[P1] IS FALSE
  2210         -** <li> P3==0, P4==1  &rarr;  r[P2] = r[P1] IS NOT TRUE
  2211         -** <li> P3==1, P4==0  &rarr;  r[P2] = r[P1] IS NOT FALSE
         2208  +** <li> If P3==0 and P4==0  then  r[P2] := r[P1] IS TRUE
         2209  +** <li> If P3==1 and P4==1  then  r[P2] := r[P1] IS FALSE
         2210  +** <li> If P3==0 and P4==1  then  r[P2] := r[P1] IS NOT TRUE
         2211  +** <li> If P3==1 and P4==0  then  r[P2] := r[P1] IS NOT FALSE
  2212   2212   ** </ul>
  2213   2213   */
  2214   2214   case OP_IsTrue: {               /* in1, out2 */
  2215   2215     assert( pOp->p4type==P4_INT32 );
  2216   2216     assert( pOp->p4.i==0 || pOp->p4.i==1 );
         2217  +  assert( pOp->p3==0 || pOp->p3==1 );
  2217   2218     sqlite3VdbeMemSetInt64(&aMem[pOp->p2],
  2218   2219         sqlite3VdbeBooleanValue(&aMem[pOp->p1], pOp->p3) ^ pOp->p4.i);
  2219   2220     break;
  2220   2221   }
  2221   2222   
  2222   2223   /* Opcode: Not P1 P2 * * *
  2223   2224   ** Synopsis: r[P2]= !r[P1]