⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expr.c

📁 sqlite 嵌入式数据库的源码
💻 C
📖 第 1 页 / 共 5 页
字号:
  }  switch( pExpr->op ){    case TK_IN: {      char affinity;      KeyInfo keyInfo;      int addr;        /* Address of OP_OpenTemp instruction */      affinity = sqlite3ExprAffinity(pExpr->pLeft);      /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'      ** expression it is handled the same way. A temporary table is       ** filled with single-field index keys representing the results      ** from the SELECT or the <exprlist>.      **      ** If the 'x' expression is a column value, or the SELECT...      ** statement returns a column value, then the affinity of that      ** column is used to build the index keys. If both 'x' and the      ** SELECT... statement are columns, then numeric affinity is used      ** if either column has NUMERIC or INTEGER affinity. If neither      ** 'x' nor the SELECT... statement are columns, then numeric affinity      ** is used.      */      pExpr->iTable = pParse->nTab++;      addr = sqlite3VdbeAddOp(v, OP_OpenTemp, pExpr->iTable, 0);      memset(&keyInfo, 0, sizeof(keyInfo));      keyInfo.nField = 1;      sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);      if( pExpr->pSelect ){        /* Case 1:     expr IN (SELECT ...)        **        ** Generate code to write the results of the select into the temporary        ** table allocated and opened above.        */        int iParm = pExpr->iTable +  (((int)affinity)<<16);        ExprList *pEList;        assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );        sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);        pEList = pExpr->pSelect->pEList;        if( pEList && pEList->nExpr>0 ){           keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,              pEList->a[0].pExpr);        }      }else if( pExpr->pList ){        /* Case 2:     expr IN (exprlist)        **	** For each expression, build an index key from the evaluation and        ** store it in the temporary table. If <expr> is a column, then use        ** that columns affinity when building index keys. If <expr> is not        ** a column, use numeric affinity.        */        int i;        if( !affinity ){          affinity = SQLITE_AFF_NUMERIC;        }        keyInfo.aColl[0] = pExpr->pLeft->pColl;        /* Loop through each expression in <exprlist>. */        for(i=0; i<pExpr->pList->nExpr; i++){          Expr *pE2 = pExpr->pList->a[i].pExpr;          /* Check that the expression is constant and valid. */          if( !sqlite3ExprIsConstant(pE2) ){            sqlite3ErrorMsg(pParse,              "right-hand side of IN operator must be constant");            return;          }          /* Evaluate the expression and insert it into the temp table */          sqlite3ExprCode(pParse, pE2);          sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);          sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);        }      }      sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);      break;    }    case TK_EXISTS:    case TK_SELECT: {      /* This has to be a scalar SELECT.  Generate code to put the      ** value of this select in a memory cell and record the number      ** of the memory cell in iColumn.      */      int sop;      Select *pSel;      pExpr->iColumn = pParse->nMem++;      pSel = pExpr->pSelect;      if( pExpr->op==TK_SELECT ){        sop = SRT_Mem;      }else{        static const Token one = { "1", 0, 1 };        sop = SRT_Exists;        sqlite3ExprListDelete(pSel->pEList);        pSel->pEList = sqlite3ExprListAppend(0,                           sqlite3Expr(TK_INTEGER, 0, 0, &one), 0);      }      sqlite3Select(pParse, pSel, sop, pExpr->iColumn, 0, 0, 0, 0);      break;    }  }  if( pExpr->pSelect ){    sqlite3VdbeAddOp(v, OP_AggContextPop, 0, 0);  }  if( label<0 ){    sqlite3VdbeResolveLabel(v, label);  }  return;}#endif /* SQLITE_OMIT_SUBQUERY *//*** Generate an instruction that will put the integer describe by** text z[0..n-1] on the stack.*/static void codeInteger(Vdbe *v, const char *z, int n){  int i;  if( sqlite3GetInt32(z, &i) ){    sqlite3VdbeAddOp(v, OP_Integer, i, 0);  }else if( sqlite3FitsIn64Bits(z) ){    sqlite3VdbeOp3(v, OP_Integer, 0, 0, z, n);  }else{    sqlite3VdbeOp3(v, OP_Real, 0, 0, z, n);  }}/*** Generate code into the current Vdbe to evaluate the given** expression and leave the result on the top of stack.**** This code depends on the fact that certain token values (ex: TK_EQ)** are the same as opcode values (ex: OP_Eq) that implement the corresponding** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in** the make process cause these values to align.  Assert()s in the code** below verify that the numbers are aligned correctly.*/void sqlite3ExprCode(Parse *pParse, Expr *pExpr){  Vdbe *v = pParse->pVdbe;  int op;  if( v==0 ) return;  if( pExpr==0 ){    sqlite3VdbeAddOp(v, OP_Null, 0, 0);    return;  }  op = pExpr->op;  switch( op ){    case TK_COLUMN: {      if( !pParse->fillAgg && pExpr->iAgg>=0 ){        sqlite3VdbeAddOp(v, OP_AggGet, pExpr->iAggCtx, pExpr->iAgg);      }else if( pExpr->iColumn>=0 ){        sqlite3VdbeAddOp(v, OP_Column, pExpr->iTable, pExpr->iColumn);        sqlite3ColumnDefault(v, pExpr->pTab, pExpr->iColumn);      }else{        sqlite3VdbeAddOp(v, OP_Rowid, pExpr->iTable, 0);      }      break;    }    case TK_INTEGER: {      codeInteger(v, pExpr->token.z, pExpr->token.n);      break;    }    case TK_FLOAT:    case TK_STRING: {      assert( TK_FLOAT==OP_Real );      assert( TK_STRING==OP_String8 );      sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z, pExpr->token.n);      sqlite3VdbeDequoteP3(v, -1);      break;    }    case TK_NULL: {      sqlite3VdbeAddOp(v, OP_Null, 0, 0);      break;    }#ifndef SQLITE_OMIT_BLOB_LITERAL    case TK_BLOB: {      assert( TK_BLOB==OP_HexBlob );      sqlite3VdbeOp3(v, op, 0, 0, pExpr->token.z+1, pExpr->token.n-1);      sqlite3VdbeDequoteP3(v, -1);      break;    }#endif    case TK_VARIABLE: {      sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);      if( pExpr->token.n>1 ){        sqlite3VdbeChangeP3(v, -1, pExpr->token.z, pExpr->token.n);      }      break;    }    case TK_REGISTER: {      sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);      break;    }    case TK_LT:    case TK_LE:    case TK_GT:    case TK_GE:    case TK_NE:    case TK_EQ: {      assert( TK_LT==OP_Lt );      assert( TK_LE==OP_Le );      assert( TK_GT==OP_Gt );      assert( TK_GE==OP_Ge );      assert( TK_EQ==OP_Eq );      assert( TK_NE==OP_Ne );      sqlite3ExprCode(pParse, pExpr->pLeft);      sqlite3ExprCode(pParse, pExpr->pRight);      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);      break;    }    case TK_AND:    case TK_OR:    case TK_PLUS:    case TK_STAR:    case TK_MINUS:    case TK_REM:    case TK_BITAND:    case TK_BITOR:    case TK_SLASH:    case TK_LSHIFT:    case TK_RSHIFT:     case TK_CONCAT: {      assert( TK_AND==OP_And );      assert( TK_OR==OP_Or );      assert( TK_PLUS==OP_Add );      assert( TK_MINUS==OP_Subtract );      assert( TK_REM==OP_Remainder );      assert( TK_BITAND==OP_BitAnd );      assert( TK_BITOR==OP_BitOr );      assert( TK_SLASH==OP_Divide );      assert( TK_LSHIFT==OP_ShiftLeft );      assert( TK_RSHIFT==OP_ShiftRight );      assert( TK_CONCAT==OP_Concat );      sqlite3ExprCode(pParse, pExpr->pLeft);      sqlite3ExprCode(pParse, pExpr->pRight);      sqlite3VdbeAddOp(v, op, 0, 0);      break;    }    case TK_UMINUS: {      Expr *pLeft = pExpr->pLeft;      assert( pLeft );      if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){        Token *p = &pLeft->token;        char *z = sqliteMalloc( p->n + 2 );        sprintf(z, "-%.*s", p->n, p->z);        if( pLeft->op==TK_FLOAT ){          sqlite3VdbeOp3(v, OP_Real, 0, 0, z, p->n+1);        }else{          codeInteger(v, z, p->n+1);        }        sqliteFree(z);        break;      }      /* Fall through into TK_NOT */    }    case TK_BITNOT:    case TK_NOT: {      assert( TK_BITNOT==OP_BitNot );      assert( TK_NOT==OP_Not );      sqlite3ExprCode(pParse, pExpr->pLeft);      sqlite3VdbeAddOp(v, op, 0, 0);      break;    }    case TK_ISNULL:    case TK_NOTNULL: {      int dest;      assert( TK_ISNULL==OP_IsNull );      assert( TK_NOTNULL==OP_NotNull );      sqlite3VdbeAddOp(v, OP_Integer, 1, 0);      sqlite3ExprCode(pParse, pExpr->pLeft);      dest = sqlite3VdbeCurrentAddr(v) + 2;      sqlite3VdbeAddOp(v, op, 1, dest);      sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);      break;    }    case TK_AGG_FUNCTION: {      sqlite3VdbeAddOp(v, OP_AggGet, 0, pExpr->iAgg);      break;    }    case TK_CONST_FUNC:    case TK_FUNCTION: {      ExprList *pList = pExpr->pList;      int nExpr = pList ? pList->nExpr : 0;      FuncDef *pDef;      int nId;      const char *zId;      int p2 = 0;      int i;      u8 enc = pParse->db->enc;      CollSeq *pColl = 0;      zId = pExpr->token.z;      nId = pExpr->token.n;      pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);      assert( pDef!=0 );      nExpr = sqlite3ExprCodeExprList(pParse, pList);      for(i=0; i<nExpr && i<32; i++){        if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){          p2 |= (1<<i);        }        if( pDef->needCollSeq && !pColl ){          pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);        }      }      if( pDef->needCollSeq ){        if( !pColl ) pColl = pParse->db->pDfltColl;         sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);      }      sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);      break;    }#ifndef SQLITE_OMIT_SUBQUERY    case TK_EXISTS:    case TK_SELECT: {      sqlite3CodeSubselect(pParse, pExpr);      sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);      VdbeComment((v, "# load subquery result"));      break;    }    case TK_IN: {      int addr;      char affinity;      sqlite3CodeSubselect(pParse, pExpr);      /* Figure out the affinity to use to create a key from the results      ** of the expression. affinityStr stores a static string suitable for      ** P3 of OP_MakeRecord.      */      affinity = comparisonAffinity(pExpr);      sqlite3VdbeAddOp(v, OP_Integer, 1, 0);      /* Code the <expr> from "<expr> IN (...)". The temporary table      ** pExpr->iTable contains the values that make up the (...) set.      */      sqlite3ExprCode(pParse, pExpr->pLeft);      addr = sqlite3VdbeCurrentAddr(v);      sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4);            /* addr + 0 */      sqlite3VdbeAddOp(v, OP_Pop, 2, 0);      sqlite3VdbeAddOp(v, OP_Null, 0, 0);      sqlite3VdbeAddOp(v, OP_Goto, 0, addr+7);      sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);   /* addr + 4 */      sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, addr+7);      sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);                  /* addr + 6 */      break;    }#endif    case TK_BETWEEN: {      Expr *pLeft = pExpr->pLeft;      struct ExprList_item *pLItem = pExpr->pList->a;      Expr *pRight = pLItem->pExpr;      sqlite3ExprCode(pParse, pLeft);      sqlite3VdbeAddOp(v, OP_Dup, 0, 0);      sqlite3ExprCode(pParse, pRight);      codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);      sqlite3VdbeAddOp(v, OP_Pull, 1, 0);      pLItem++;      pRight = pLItem->pExpr;      sqlite3ExprCode(pParse, pRight);      codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);      sqlite3VdbeAddOp(v, OP_And, 0, 0);      break;    }    case TK_UPLUS:    case TK_AS: {      sqlite3ExprCode(pParse, pExpr->pLeft);      break;    }    case TK_CASE: {      int expr_end_label;      int jumpInst;      int addr;      int nExpr;      int i;      ExprList *pEList;      struct ExprList_item *aListelem;      assert(pExpr->pList);      assert((pExpr->pList->nExpr % 2) == 0);      assert(pExpr->pList->nExpr > 0);      pEList = pExpr->pList;      aListelem = pEList->a;      nExpr = pEList->nExpr;      expr_end_label = sqlite3VdbeMakeLabel(v);      if( pExpr->pLeft ){        sqlite3ExprCode(pParse, pExpr->pLeft);      }      for(i=0; i<nExpr; i=i+2){        sqlite3ExprCode(pParse, aListelem[i].pExpr);        if( pExpr->pLeft ){          sqlite3VdbeAddOp(v, OP_Dup, 1, 1);          jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,                                 OP_Ne, 0, 1);          sqlite3VdbeAddOp(v, OP_Pop, 1, 0);        }else{          jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);        }        sqlite3ExprCode(pParse, aListelem[i+1].pExpr);        sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);        addr = sqlite3VdbeCurrentAddr(v);        sqlite3VdbeChangeP2(v, jumpInst, addr);      }      if( pExpr->pLeft ){        sqlite3VdbeAddOp(v, OP_Pop, 1, 0);      }      if( pExpr->pRight ){        sqlite3ExprCode(pParse, pExpr->pRight);      }else{        sqlite3VdbeAddOp(v, OP_Null, 0, 0);      }      sqlite3VdbeResolveLabel(v, expr_end_label);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -