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

📄 expr.c

📁 1.编译色情sqlite源代码为dll;2.运用sqlite3数据库存储二进制数据到数据库
💻 C
📖 第 1 页 / 共 5 页
字号:
        if( (pIdx->aiColumn[0]==iCol)         && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))         && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))        ){          int iDb;          int iMem = ++pParse->nMem;          int iAddr;          char *pKey;            pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);          iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);          sqlite3VdbeUsesBtree(v, iDb);          iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem);          sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem);            sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,                               pKey,P4_KEYINFO_HANDOFF);          VdbeComment((v, "%s", pIdx->zName));          eType = IN_INDEX_INDEX;          sqlite3VdbeAddOp2(v, OP_SetNumColumns, iTab, pIdx->nColumn);          sqlite3VdbeJumpHere(v, iAddr);        }      }    }  }  if( eType==0 ){    sqlite3CodeSubselect(pParse, pX);    eType = IN_INDEX_EPH;  }else{    pX->iTable = iTab;  }  return eType;}#endif/*** Generate code for scalar subqueries used as an expression** and IN operators.  Examples:****     (SELECT a FROM b)          -- subquery**     EXISTS (SELECT a FROM b)   -- EXISTS subquery**     x IN (4,5,11)              -- IN operator with list on right-hand side**     x IN (SELECT a FROM b)     -- IN operator with subquery on the right**** The pExpr parameter describes the expression that contains the IN** operator or subquery.*/#ifndef SQLITE_OMIT_SUBQUERYvoid sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){  int testAddr = 0;                       /* One-time test address */  Vdbe *v = sqlite3GetVdbe(pParse);  if( v==0 ) return;  /* This code must be run in its entirety every time it is encountered  ** if any of the following is true:  **  **    *  The right-hand side is a correlated subquery  **    *  The right-hand side is an expression list containing variables  **    *  We are inside a trigger  **  ** If all of the above are false, then we can run this code just once  ** save the results, and reuse the same result on subsequent invocations.  */  if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){    int mem = ++pParse->nMem;    sqlite3VdbeAddOp1(v, OP_If, mem);    testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem);    assert( testAddr>0 || pParse->db->mallocFailed );  }  switch( pExpr->op ){    case TK_IN: {      char affinity;      KeyInfo keyInfo;      int addr;        /* Address of OP_OpenEphemeral 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 virtual 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 = sqlite3VdbeAddOp1(v, OP_OpenEphemeral, pExpr->iTable);      memset(&keyInfo, 0, sizeof(keyInfo));      keyInfo.nField = 1;      sqlite3VdbeAddOp2(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.        */        SelectDest dest;        ExprList *pEList;        sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);        dest.affinity = (int)affinity;        assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );        if( sqlite3Select(pParse, pExpr->pSelect, &dest, 0, 0, 0, 0) ){          return;        }        pEList = pExpr->pSelect->pEList;        if( pEList && pEList->nExpr>0 ){           keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(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;        ExprList *pList = pExpr->pList;        struct ExprList_item *pItem;        int r1, r2;        if( !affinity ){          affinity = SQLITE_AFF_NONE;        }        keyInfo.aColl[0] = pExpr->pLeft->pColl;        /* Loop through each expression in <exprlist>. */        r1 = sqlite3GetTempReg(pParse);        r2 = sqlite3GetTempReg(pParse);        for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){          Expr *pE2 = pItem->pExpr;          /* If the expression is not constant then we will need to          ** disable the test that was generated above that makes sure          ** this code only executes once.  Because for a non-constant          ** expression we need to rerun this code each time.          */          if( testAddr && !sqlite3ExprIsConstant(pE2) ){            sqlite3VdbeChangeToNoop(v, testAddr-1, 2);            testAddr = 0;          }          /* Evaluate the expression and insert it into the temp table */          sqlite3ExprCode(pParse, pE2, r1);          sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);          sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);        }        sqlite3ReleaseTempReg(pParse, r1);        sqlite3ReleaseTempReg(pParse, r2);      }      sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_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.      */      static const Token one = { (u8*)"1", 0, 1 };      Select *pSel;      SelectDest dest;      pSel = pExpr->pSelect;      sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);      if( pExpr->op==TK_SELECT ){        dest.eDest = SRT_Mem;        sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm);        VdbeComment((v, "Init subquery result"));      }else{        dest.eDest = SRT_Exists;        sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm);        VdbeComment((v, "Init EXISTS result"));      }      sqlite3ExprDelete(pSel->pLimit);      pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);      if( sqlite3Select(pParse, pSel, &dest, 0, 0, 0, 0) ){        return;      }      pExpr->iColumn = dest.iParm;      break;    }  }  if( testAddr ){    sqlite3VdbeJumpHere(v, testAddr-1);  }  return;}#endif /* SQLITE_OMIT_SUBQUERY *//*** Duplicate an 8-byte value*/static char *dup8bytes(Vdbe *v, const char *in){  char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);  if( out ){    memcpy(out, in, 8);  }  return out;}/*** Generate an instruction that will put the floating point** value described by z[0..n-1] into register iMem.**** The z[] string will probably not be zero-terminated.  But the ** z[n] character is guaranteed to be something that does not look** like the continuation of the number.*/static void codeReal(Vdbe *v, const char *z, int n, int negateFlag, int iMem){  assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );  if( z ){    double value;    char *zV;    assert( !isdigit(z[n]) );    sqlite3AtoF(z, &value);    if( negateFlag ) value = -value;    zV = dup8bytes(v, (char*)&value);    sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);  }}/*** Generate an instruction that will put the integer describe by** text z[0..n-1] into register iMem.**** The z[] string will probably not be zero-terminated.  But the ** z[n] character is guaranteed to be something that does not look** like the continuation of the number.*/static void codeInteger(Vdbe *v, const char *z, int n, int negFlag, int iMem){  assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );  if( z ){    int i;    assert( !isdigit(z[n]) );    if( sqlite3GetInt32(z, &i) ){      if( negFlag ) i = -i;      sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);    }else if( sqlite3FitsIn64Bits(z, negFlag) ){      i64 value;      char *zV;      sqlite3Atoi64(z, &value);      if( negFlag ) value = -value;      zV = dup8bytes(v, (char*)&value);      sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);    }else{      codeReal(v, z, n, negFlag, iMem);    }  }}/*** Generate code that will extract the iColumn-th column from** table pTab and store the column value in register iReg.** There is an open cursor to pTab in ** iTable.  If iColumn<0 then code is generated that extracts the rowid.*/void sqlite3ExprCodeGetColumn(  Vdbe *v,         /* The VM being created */  Table *pTab,     /* Description of the table we are reading from */  int iColumn,     /* Index of the table column */  int iTable,      /* The cursor pointing to the table */  int iReg         /* Store results here */){  if( iColumn<0 ){    int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;    sqlite3VdbeAddOp2(v, op, iTable, iReg);  }else if( pTab==0 ){    sqlite3VdbeAddOp3(v, OP_Column, iTable, iColumn, iReg);  }else{    int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;    sqlite3VdbeAddOp3(v, op, iTable, iColumn, iReg);    sqlite3ColumnDefault(v, pTab, iColumn);#ifndef SQLITE_OMIT_FLOATING_POINT    if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){      sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);    }#endif  }}/*** Generate code into the current Vdbe to evaluate the given** expression.  Attempt to store the results in register "target".** Return the register where results are stored.**** With this routine, there is no guaranteed that results will** be stored in target.  The result might be stored in some other** register if it is convenient to do so.  The calling function** must check the return code and move the results to the desired** register.*/static int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){  Vdbe *v = pParse->pVdbe;  /* The VM under construction */  int op;                   /* The opcode being coded */  int inReg = target;       /* Results stored in register inReg */  int regFree1 = 0;         /* If non-zero free this temporary register */  int regFree2 = 0;         /* If non-zero free this temporary register */  int r1, r2, r3;           /* Various register numbers */  assert( v!=0 || pParse->db->mallocFailed );  assert( target>0 && target<=pParse->nMem );  if( v==0 ) return 0;  if( pExpr==0 ){    op = TK_NULL;  }else{    op = pExpr->op;  }  switch( op ){    case TK_AGG_COLUMN: {      AggInfo *pAggInfo = pExpr->pAggInfo;      struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];      if( !pAggInfo->directMode ){        assert( pCol->iMem>0 );        inReg = pCol->iMem;        break;      }else if( pAggInfo->useSortingIdx ){        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,                              pCol->iSorterColumn, target);        break;      }      /* Otherwise, fall thru into the TK_COLUMN case */    }    case TK_COLUMN: {      if( pExpr->iTable<0 ){        /* This only happens when coding check constraints */        assert( pParse->ckBase>0 );        inReg = pExpr->iColumn + pParse->ckBase;      }else{        sqlite3ExprCodeGetColumn(v, pExpr->pTab,                                 pExpr->iColumn, pExpr->iTable, target);      }      break;    }    case TK_INTEGER: {      codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);      break;    }    case TK_FLOAT: {      codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0, target);      break;    }    case TK_STRING: {      sqlite3DequoteExpr(pParse->db, pExpr);      sqlite3VdbeAddOp4(v,OP_String8, 0, target, 0,                        (char*)pExpr->token.z, pExpr->token.n);      break;    }    case TK_NULL: {      sqlite3VdbeAddOp2(v, OP_Null, 0, target);      break;    }#ifndef SQLITE_OMIT_BLOB_LITERAL    case TK_BLOB: {      int n;      const char *z;      char *zBlob;      assert( pExpr->token.n>=3 );      assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );      assert( pExpr->token.z[1]=='\'' );      assert( pExpr->token.z[pExpr->token.n-1]=='\'' );      n = pExpr->token.n - 3;      z = (char*)pExpr->token.z + 2;      zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);      sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);      break;    }#endif    case TK_VARIABLE: {      sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iTable, target);      if( pExpr->token.n>1 ){        sqlite3VdbeChangeP4(v, -1, (char*)pExpr->token.z, pExpr->token.n);      }      break;    }    case TK_REGISTER: {      inReg = pExpr->iTable;      break;    }#ifndef SQLITE_OMIT_CAST    case TK_CAST: {      /* Expressions of the form:   CAST(pLeft AS token) */      int aff,

⌨️ 快捷键说明

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