/*
 ** This function is used to allocate and populate UnpackedRecord
 ** structures intended to be compared against sample index keys stored
 ** in the sqlite_stat4 table.
 **
 ** A single call to this function attempts to populates field iVal (leftmost
 ** is 0 etc.) of the unpacked record with a value extracted from expression
 ** pExpr. Extraction of values is possible if:
 **
 **  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
 **
 **  * The expression is a bound variable, and this is a reprepare, or
 **
 **  * The sqlite3ValueFromExpr() function is able to extract a value
 **    from the expression (i.e. the expression is a literal value).
 **
 ** If a value can be extracted, the affinity passed as the 5th argument
 ** is applied to it before it is copied into the UnpackedRecord. Output
 ** parameter *pbOk is set to true if a value is extracted, or false 
 ** otherwise.
 **
 ** When this function is called, *ppRec must either point to an object
 ** allocated by an earlier call to this function, or must be NULL. If it
 ** is NULL and a value can be successfully extracted, a new UnpackedRecord
 ** is allocated (and *ppRec set to point to it) before returning.
 **
 ** Unless an error is encountered, SQLITE_OK is returned. It is not an
 ** error if a value cannot be extracted from pExpr. If an error does
 ** occur, an SQLite error code is returned.
 */
SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
                                             Parse *pParse,                  /* Parse context */
                                             Index *pIdx,                    /* Index being probed */
                                             UnpackedRecord **ppRec,         /* IN/OUT: Probe record */
                                             Expr *pExpr,                    /* The expression to extract a value from */
                                             u8 affinity,                    /* Affinity to use */
                                             int iVal,                       /* Array element to populate */
                                             int *pbOk                       /* OUT: True if value was extracted */
){
    int rc = SQLITE_OK;
    sqlite3_value *pVal = 0;
    sqlite3 *db = pParse->db;
    
    
    struct ValueNewStat4Ctx alloc;
    alloc.pParse = pParse;
    alloc.pIdx = pIdx;
    alloc.ppRec = ppRec;
    alloc.iVal = iVal;
    
    /* Skip over any TK_COLLATE nodes */
    pExpr = sqlite3ExprSkipCollate(pExpr);
    
    if( !pExpr ){
        pVal = valueNew(db, &alloc);
        if( pVal ){
            sqlite3VdbeMemSetNull((Mem*)pVal);
            *pbOk = 1;
        }
    }else if( pExpr->op==TK_VARIABLE
             || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
             ){
        Vdbe *v;
        int iBindVar = pExpr->iColumn;
        sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
        if( (v = pParse->pReprepare)!=0 ){
            pVal = valueNew(db, &alloc);
            if( pVal ){
                rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
                if( rc==SQLITE_OK ){
                    sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
                }
                pVal->db = pParse->db;
                *pbOk = 1;
                sqlite3VdbeMemStoreType((Mem*)pVal);
            }
        }else{
            *pbOk = 0;
        }
    }else{
        rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
        *pbOk = (pVal!=0);
    }
    
    assert( pVal==0 || pVal->db==db );
    return rc;
}
Beispiel #2
0
/*
** Check to see if the given expression is a LIKE or GLOB operator that
** can be optimized using inequality constraints.  Return TRUE if it is
** so and false if not.
**
** In order for the operator to be optimizible, the RHS must be a string
** literal that does not begin with a wildcard.  The LHS must be a column
** that may only be NULL, a string, or a BLOB, never a number. (This means
** that virtual tables cannot participate in the LIKE optimization.)  The
** collating sequence for the column on the LHS must be appropriate for
** the operator.
*/
static int isLikeOrGlob(
  Parse *pParse,    /* Parsing and code generating context */
  Expr *pExpr,      /* Test this expression */
  Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
  int *pisComplete, /* True if the only wildcard is % in the last character */
  int *pnoCase      /* True if uppercase is equivalent to lowercase */
){
  const char *z = 0;         /* String on RHS of LIKE operator */
  Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
  ExprList *pList;           /* List of operands to the LIKE operator */
  int c;                     /* One character in z[] */
  int cnt;                   /* Number of non-wildcard prefix characters */
  char wc[3];                /* Wildcard characters */
  sqlite3 *db = pParse->db;  /* Database connection */
  sqlite3_value *pVal = 0;
  int op;                    /* Opcode of pRight */

  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
    return 0;
  }
#ifdef SQLITE_EBCDIC
  if( *pnoCase ) return 0;
#endif
  pList = pExpr->x.pList;
  pLeft = pList->a[1].pExpr;
  if( pLeft->op!=TK_COLUMN 
   || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
   || IsVirtual(pLeft->pTab)  /* Value might be numeric */
  ){
    /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
    ** be the name of an indexed column with TEXT affinity. */
    return 0;
  }
  assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */

  pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
  op = pRight->op;
  if( op==TK_VARIABLE ){
    Vdbe *pReprepare = pParse->pReprepare;
    int iCol = pRight->iColumn;
    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
    if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
      z = (char *)sqlite3_value_text(pVal);
    }
    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
  }else if( op==TK_STRING ){
    z = pRight->u.zToken;
  }
  if( z ){
    cnt = 0;
    while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
      cnt++;
    }
    if( cnt!=0 && 255!=(u8)z[cnt-1] ){
      Expr *pPrefix;
      *pisComplete = c==wc[0] && z[cnt+1]==0;
      pPrefix = sqlite3Expr(db, TK_STRING, z);
      if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
      *ppPrefix = pPrefix;
      if( op==TK_VARIABLE ){
        Vdbe *v = pParse->pVdbe;
        sqlite3VdbeSetVarmask(v, pRight->iColumn);
        if( *pisComplete && pRight->u.zToken[1] ){
          /* If the rhs of the LIKE expression is a variable, and the current
          ** value of the variable means there is no need to invoke the LIKE
          ** function, then no OP_Variable will be added to the program.
          ** This causes problems for the sqlite3_bind_parameter_name()
          ** API. To work around them, add a dummy OP_Variable here.
          */ 
          int r1 = sqlite3GetTempReg(pParse);
          sqlite3ExprCodeTarget(pParse, pRight, r1);
          sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
          sqlite3ReleaseTempReg(pParse, r1);
        }
      }
    }else{
      z = 0;
    }
  }

  sqlite3ValueFree(pVal);
  return (z!=0);
}