extensions/sqlite/sqlite-source/where.c
changeset 1645 27307df4d711
parent 1203 633c25ce6405
     1.1 --- a/extensions/sqlite/sqlite-source/where.c	Sun Oct 28 05:06:12 2007 +0000
     1.2 +++ b/extensions/sqlite/sqlite-source/where.c	Sun Oct 28 05:48:06 2007 +0000
     1.3 @@ -207,7 +207,7 @@
     1.4      }
     1.5    }
     1.6    if( pWC->a!=pWC->aStatic ){
     1.7 -    sqliteFree(pWC->a);
     1.8 +    sqlite3_free(pWC->a);
     1.9    }
    1.10  }
    1.11  
    1.12 @@ -228,8 +228,9 @@
    1.13    int idx;
    1.14    if( pWC->nTerm>=pWC->nSlot ){
    1.15      WhereTerm *pOld = pWC->a;
    1.16 -    pWC->a = sqliteMalloc( sizeof(pWC->a[0])*pWC->nSlot*2 );
    1.17 +    pWC->a = sqlite3_malloc( sizeof(pWC->a[0])*pWC->nSlot*2 );
    1.18      if( pWC->a==0 ){
    1.19 +      pWC->pParse->db->mallocFailed = 1;
    1.20        if( flags & TERM_DYNAMIC ){
    1.21          sqlite3ExprDelete(p);
    1.22        }
    1.23 @@ -237,7 +238,7 @@
    1.24      }
    1.25      memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
    1.26      if( pOld!=pWC->aStatic ){
    1.27 -      sqliteFree(pOld);
    1.28 +      sqlite3_free(pOld);
    1.29      }
    1.30      pWC->nSlot *= 2;
    1.31    }
    1.32 @@ -349,15 +350,14 @@
    1.33    return mask;
    1.34  }
    1.35  static Bitmask exprSelectTableUsage(ExprMaskSet *pMaskSet, Select *pS){
    1.36 -  Bitmask mask;
    1.37 -  if( pS==0 ){
    1.38 -    mask = 0;
    1.39 -  }else{
    1.40 -    mask = exprListTableUsage(pMaskSet, pS->pEList);
    1.41 +  Bitmask mask = 0;
    1.42 +  while( pS ){
    1.43 +    mask |= exprListTableUsage(pMaskSet, pS->pEList);
    1.44      mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
    1.45      mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
    1.46      mask |= exprTableUsage(pMaskSet, pS->pWhere);
    1.47      mask |= exprTableUsage(pMaskSet, pS->pHaving);
    1.48 +    pS = pS->pPrior;
    1.49    }
    1.50    return mask;
    1.51  }
    1.52 @@ -383,10 +383,22 @@
    1.53  /*
    1.54  ** Commute a comparision operator.  Expressions of the form "X op Y"
    1.55  ** are converted into "Y op X".
    1.56 +**
    1.57 +** If a collation sequence is associated with either the left or right
    1.58 +** side of the comparison, it remains associated with the same side after
    1.59 +** the commutation. So "Y collate NOCASE op X" becomes 
    1.60 +** "X collate NOCASE op Y". This is because any collation sequence on
    1.61 +** the left hand side of a comparison overrides any collation sequence 
    1.62 +** attached to the right. For the same reason the EP_ExpCollate flag
    1.63 +** is not commuted.
    1.64  */
    1.65  static void exprCommute(Expr *pExpr){
    1.66 +  u16 expRight = (pExpr->pRight->flags & EP_ExpCollate);
    1.67 +  u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate);
    1.68    assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
    1.69    SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
    1.70 +  pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft;
    1.71 +  pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight;
    1.72    SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
    1.73    if( pExpr->op>=TK_GT ){
    1.74      assert( TK_LT==TK_GT+2 );
    1.75 @@ -538,7 +550,7 @@
    1.76        (pColl->type!=SQLITE_COLL_NOCASE || !noCase) ){
    1.77      return 0;
    1.78    }
    1.79 -  sqlite3DequoteExpr(pRight);
    1.80 +  sqlite3DequoteExpr(db, pRight);
    1.81    z = (char *)pRight->token.z;
    1.82    for(cnt=0; (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2]; cnt++){}
    1.83    if( cnt==0 || 255==(u8)z[cnt] ){
    1.84 @@ -703,8 +715,10 @@
    1.85    int nPattern;
    1.86    int isComplete;
    1.87    int op;
    1.88 +  Parse *pParse = pWC->pParse;
    1.89 +  sqlite3 *db = pParse->db;
    1.90  
    1.91 -  if( sqlite3MallocFailed() ) return;
    1.92 +  if( db->mallocFailed ) return;
    1.93    prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
    1.94    op = pExpr->op;
    1.95    if( op==TK_IN ){
    1.96 @@ -737,8 +751,8 @@
    1.97        Expr *pDup;
    1.98        if( pTerm->leftCursor>=0 ){
    1.99          int idxNew;
   1.100 -        pDup = sqlite3ExprDup(pExpr);
   1.101 -        if( sqlite3MallocFailed() ){
   1.102 +        pDup = sqlite3ExprDup(db, pExpr);
   1.103 +        if( db->mallocFailed ){
   1.104            sqlite3ExprDelete(pDup);
   1.105            return;
   1.106          }
   1.107 @@ -776,8 +790,8 @@
   1.108      for(i=0; i<2; i++){
   1.109        Expr *pNewExpr;
   1.110        int idxNew;
   1.111 -      pNewExpr = sqlite3Expr(ops[i], sqlite3ExprDup(pExpr->pLeft),
   1.112 -                             sqlite3ExprDup(pList->a[i].pExpr), 0);
   1.113 +      pNewExpr = sqlite3Expr(db, ops[i], sqlite3ExprDup(db, pExpr->pLeft),
   1.114 +                             sqlite3ExprDup(db, pList->a[i].pExpr), 0);
   1.115        idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
   1.116        exprAnalyze(pSrc, pWC, idxNew);
   1.117        pTerm = &pWC->a[idxTerm];
   1.118 @@ -837,13 +851,13 @@
   1.119        Expr *pLeft = 0;
   1.120        for(i=sOr.nTerm-1, pOrTerm=sOr.a; i>=0 && ok; i--, pOrTerm++){
   1.121          if( (pOrTerm->flags & TERM_OR_OK)==0 ) continue;
   1.122 -        pDup = sqlite3ExprDup(pOrTerm->pExpr->pRight);
   1.123 -        pList = sqlite3ExprListAppend(pList, pDup, 0);
   1.124 +        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight);
   1.125 +        pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup, 0);
   1.126          pLeft = pOrTerm->pExpr->pLeft;
   1.127        }
   1.128        assert( pLeft!=0 );
   1.129 -      pDup = sqlite3ExprDup(pLeft);
   1.130 -      pNew = sqlite3Expr(TK_IN, pDup, 0, 0);
   1.131 +      pDup = sqlite3ExprDup(db, pLeft);
   1.132 +      pNew = sqlite3Expr(db, TK_IN, pDup, 0, 0);
   1.133        if( pNew ){
   1.134          int idxNew;
   1.135          transferJoinMarkings(pNew, pExpr);
   1.136 @@ -866,7 +880,7 @@
   1.137    /* Add constraints to reduce the search space on a LIKE or GLOB
   1.138    ** operator.
   1.139    */
   1.140 -  if( isLikeOrGlob(pWC->pParse->db, pExpr, &nPattern, &isComplete) ){
   1.141 +  if( isLikeOrGlob(db, pExpr, &nPattern, &isComplete) ){
   1.142      Expr *pLeft, *pRight;
   1.143      Expr *pStr1, *pStr2;
   1.144      Expr *pNewExpr1, *pNewExpr2;
   1.145 @@ -874,21 +888,21 @@
   1.146  
   1.147      pLeft = pExpr->pList->a[1].pExpr;
   1.148      pRight = pExpr->pList->a[0].pExpr;
   1.149 -    pStr1 = sqlite3Expr(TK_STRING, 0, 0, 0);
   1.150 +    pStr1 = sqlite3PExpr(pParse, TK_STRING, 0, 0, 0);
   1.151      if( pStr1 ){
   1.152 -      sqlite3TokenCopy(&pStr1->token, &pRight->token);
   1.153 +      sqlite3TokenCopy(db, &pStr1->token, &pRight->token);
   1.154        pStr1->token.n = nPattern;
   1.155        pStr1->flags = EP_Dequoted;
   1.156      }
   1.157 -    pStr2 = sqlite3ExprDup(pStr1);
   1.158 +    pStr2 = sqlite3ExprDup(db, pStr1);
   1.159      if( pStr2 ){
   1.160        assert( pStr2->token.dyn );
   1.161        ++*(u8*)&pStr2->token.z[nPattern-1];
   1.162      }
   1.163 -    pNewExpr1 = sqlite3Expr(TK_GE, sqlite3ExprDup(pLeft), pStr1, 0);
   1.164 +    pNewExpr1 = sqlite3PExpr(pParse, TK_GE, sqlite3ExprDup(db,pLeft), pStr1, 0);
   1.165      idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
   1.166      exprAnalyze(pSrc, pWC, idxNew1);
   1.167 -    pNewExpr2 = sqlite3Expr(TK_LT, sqlite3ExprDup(pLeft), pStr2, 0);
   1.168 +    pNewExpr2 = sqlite3PExpr(pParse, TK_LT, sqlite3ExprDup(db,pLeft), pStr2, 0);
   1.169      idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
   1.170      exprAnalyze(pSrc, pWC, idxNew2);
   1.171      pTerm = &pWC->a[idxTerm];
   1.172 @@ -919,7 +933,7 @@
   1.173      prereqColumn = exprTableUsage(pMaskSet, pLeft);
   1.174      if( (prereqExpr & prereqColumn)==0 ){
   1.175        Expr *pNewExpr;
   1.176 -      pNewExpr = sqlite3Expr(TK_MATCH, 0, sqlite3ExprDup(pRight), 0);
   1.177 +      pNewExpr = sqlite3Expr(db, TK_MATCH, 0, sqlite3ExprDup(db, pRight), 0);
   1.178        idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
   1.179        pNewTerm = &pWC->a[idxNew];
   1.180        pNewTerm->prereqRight = prereqExpr;
   1.181 @@ -1245,7 +1259,7 @@
   1.182  
   1.183      /* Allocate the sqlite3_index_info structure
   1.184      */
   1.185 -    pIdxInfo = sqliteMalloc( sizeof(*pIdxInfo)
   1.186 +    pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
   1.187                               + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
   1.188                               + sizeof(*pIdxOrderBy)*nOrderBy );
   1.189      if( pIdxInfo==0 ){
   1.190 @@ -1274,7 +1288,7 @@
   1.191        if( pTerm->eOperator==WO_IN ) continue;
   1.192        pIdxCons[j].iColumn = pTerm->leftColumn;
   1.193        pIdxCons[j].iTermOffset = i;
   1.194 -      pIdxCons[j].op = (unsigned char)pTerm->eOperator;
   1.195 +      pIdxCons[j].op = pTerm->eOperator;
   1.196        /* The direct assignment in the previous line is possible only because
   1.197        ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
   1.198        ** following asserts verify this fact. */
   1.199 @@ -1363,7 +1377,7 @@
   1.200    TRACE_IDX_OUTPUTS(pIdxInfo);
   1.201    if( rc!=SQLITE_OK ){
   1.202      if( rc==SQLITE_NOMEM ){
   1.203 -      sqlite3FailedMalloc();
   1.204 +      pParse->db->mallocFailed = 1;
   1.205      }else {
   1.206        sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
   1.207      }
   1.208 @@ -1551,7 +1565,7 @@
   1.209           && nEq==pProbe->nColumn ){
   1.210        flags |= WHERE_UNIQUE;
   1.211      }
   1.212 -    WHERETRACE(("...... nEq=%d inMult=%.9g cost=%.9g\n", nEq, inMultiplier, cost));
   1.213 +    WHERETRACE(("...... nEq=%d inMult=%.9g cost=%.9g\n",nEq,inMultiplier,cost));
   1.214  
   1.215      /* Look for range constraints
   1.216      */
   1.217 @@ -1612,10 +1626,9 @@
   1.218  
   1.219      /* If this index has achieved the lowest cost so far, then use it.
   1.220      */
   1.221 -    if( cost < lowestCost ){
   1.222 +    if( flags && cost < lowestCost ){
   1.223        bestIdx = pProbe;
   1.224        lowestCost = cost;
   1.225 -      assert( flags!=0 );
   1.226        bestFlags = flags;
   1.227        bestNEq = nEq;
   1.228      }
   1.229 @@ -1724,7 +1737,7 @@
   1.230        pLevel->nxt = sqlite3VdbeMakeLabel(v);
   1.231      }
   1.232      pLevel->nIn++;
   1.233 -    pLevel->aInLoop = sqliteReallocOrFree(pLevel->aInLoop,
   1.234 +    pLevel->aInLoop = sqlite3DbReallocOrFree(pParse->db, pLevel->aInLoop,
   1.235                                      sizeof(pLevel->aInLoop[0])*pLevel->nIn);
   1.236      pIn = pLevel->aInLoop;
   1.237      if( pIn ){
   1.238 @@ -1843,10 +1856,10 @@
   1.239            */
   1.240            sqlite3_free(pInfo->idxStr);
   1.241          }
   1.242 -        sqliteFree(pInfo);
   1.243 +        sqlite3_free(pInfo);
   1.244        }
   1.245      }
   1.246 -    sqliteFree(pWInfo);
   1.247 +    sqlite3_free(pWInfo);
   1.248    }
   1.249  }
   1.250  
   1.251 @@ -1957,6 +1970,7 @@
   1.252    WhereLevel *pLevel;             /* A single level in the pWInfo list */
   1.253    int iFrom;                      /* First unused FROM clause element */
   1.254    int andFlags;              /* AND-ed combination of all wc.a[].flags */
   1.255 +  sqlite3 *db;               /* Database connection */
   1.256  
   1.257    /* The number of tables in the FROM clause is limited by the number of
   1.258    ** bits in a Bitmask 
   1.259 @@ -1976,8 +1990,10 @@
   1.260    /* Allocate and initialize the WhereInfo structure that will become the
   1.261    ** return value.
   1.262    */
   1.263 -  pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
   1.264 -  if( sqlite3MallocFailed() ){
   1.265 +  db = pParse->db;
   1.266 +  pWInfo = sqlite3DbMallocZero(db,  
   1.267 +                      sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
   1.268 +  if( db->mallocFailed ){
   1.269      goto whereBeginNoMem;
   1.270    }
   1.271    pWInfo->nLevel = pTabList->nSrc;
   1.272 @@ -2002,7 +2018,7 @@
   1.273      createMask(&maskSet, pTabList->a[i].iCursor);
   1.274    }
   1.275    exprAnalyzeAll(pTabList, &wc);
   1.276 -  if( sqlite3MallocFailed() ){
   1.277 +  if( db->mallocFailed ){
   1.278      goto whereBeginNoMem;
   1.279    }
   1.280  
   1.281 @@ -2134,24 +2150,24 @@
   1.282      if( pParse->explain==2 ){
   1.283        char *zMsg;
   1.284        struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
   1.285 -      zMsg = sqlite3MPrintf("TABLE %s", pItem->zName);
   1.286 +      zMsg = sqlite3MPrintf(db, "TABLE %s", pItem->zName);
   1.287        if( pItem->zAlias ){
   1.288 -        zMsg = sqlite3MPrintf("%z AS %s", zMsg, pItem->zAlias);
   1.289 +        zMsg = sqlite3MPrintf(db, "%z AS %s", zMsg, pItem->zAlias);
   1.290        }
   1.291        if( (pIx = pLevel->pIdx)!=0 ){
   1.292 -        zMsg = sqlite3MPrintf("%z WITH INDEX %s", zMsg, pIx->zName);
   1.293 +        zMsg = sqlite3MPrintf(db, "%z WITH INDEX %s", zMsg, pIx->zName);
   1.294        }else if( pLevel->flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
   1.295 -        zMsg = sqlite3MPrintf("%z USING PRIMARY KEY", zMsg);
   1.296 +        zMsg = sqlite3MPrintf(db, "%z USING PRIMARY KEY", zMsg);
   1.297        }
   1.298  #ifndef SQLITE_OMIT_VIRTUALTABLE
   1.299        else if( pLevel->pBestIdx ){
   1.300          sqlite3_index_info *pBestIdx = pLevel->pBestIdx;
   1.301 -        zMsg = sqlite3MPrintf("%z VIRTUAL TABLE INDEX %d:%s", zMsg,
   1.302 +        zMsg = sqlite3MPrintf(db, "%z VIRTUAL TABLE INDEX %d:%s", zMsg,
   1.303                      pBestIdx->idxNum, pBestIdx->idxStr);
   1.304        }
   1.305  #endif
   1.306        if( pLevel->flags & WHERE_ORDERBY ){
   1.307 -        zMsg = sqlite3MPrintf("%z ORDER BY", zMsg);
   1.308 +        zMsg = sqlite3MPrintf(db, "%z ORDER BY", zMsg);
   1.309        }
   1.310        sqlite3VdbeOp3(v, OP_Explain, i, pLevel->iFrom, zMsg, P3_DYNAMIC);
   1.311      }
   1.312 @@ -2679,7 +2695,7 @@
   1.313          sqlite3VdbeAddOp(v, OP_Next, pIn->iCur, pIn->topAddr);
   1.314          sqlite3VdbeJumpHere(v, pIn->topAddr-1);
   1.315        }
   1.316 -      sqliteFree(pLevel->aInLoop);
   1.317 +      sqlite3_free(pLevel->aInLoop);
   1.318      }
   1.319      sqlite3VdbeResolveLabel(v, pLevel->brk);
   1.320      if( pLevel->iLeftJoin ){