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

📄 expr.c

📁 一个小型嵌入式数据库SQLite的源码,C语言
💻 C
📖 第 1 页 / 共 5 页
字号:
** means that the form of the name is Z and that columns from any table** can be used.**** If the name cannot be resolved unambiguously, leave an error message** in pParse and return non-zero.  Return zero on success.*/static int lookupName(  Parse *pParse,       /* The parsing context */  Token *pDbToken,     /* Name of the database containing table, or NULL */  Token *pTableToken,  /* Name of table containing column, or NULL */  Token *pColumnToken, /* Name of the column. */  NameContext *pNC,    /* The name context used to resolve the name */  Expr *pExpr          /* Make this EXPR node point to the selected column */){  char *zDb = 0;       /* Name of the database.  The "X" in X.Y.Z */  char *zTab = 0;      /* Name of the table.  The "Y" in X.Y.Z or Y.Z */  char *zCol = 0;      /* Name of the column.  The "Z" */  int i, j;            /* Loop counters */  int cnt = 0;         /* Number of matching column names */  int cntTab = 0;      /* Number of matching table names */  sqlite3 *db = pParse->db;  /* The database */  struct SrcList_item *pItem;       /* Use for looping over pSrcList items */  struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */  NameContext *pTopNC = pNC;        /* First namecontext in the list */  assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */  zDb = sqlite3NameFromToken(pDbToken);  zTab = sqlite3NameFromToken(pTableToken);  zCol = sqlite3NameFromToken(pColumnToken);  if( sqlite3MallocFailed() ){    goto lookupname_end;  }  pExpr->iTable = -1;  while( pNC && cnt==0 ){    ExprList *pEList;    SrcList *pSrcList = pNC->pSrcList;    if( pSrcList ){      for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){        Table *pTab;        int iDb;        Column *pCol;          pTab = pItem->pTab;        assert( pTab!=0 );        iDb = sqlite3SchemaToIndex(db, pTab->pSchema);        assert( pTab->nCol>0 );        if( zTab ){          if( pItem->zAlias ){            char *zTabName = pItem->zAlias;            if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;          }else{            char *zTabName = pTab->zName;            if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;            if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){              continue;            }          }        }        if( 0==(cntTab++) ){          pExpr->iTable = pItem->iCursor;          pExpr->pSchema = pTab->pSchema;          pMatch = pItem;        }        for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){          if( sqlite3StrICmp(pCol->zName, zCol)==0 ){            const char *zColl = pTab->aCol[j].zColl;            IdList *pUsing;            cnt++;            pExpr->iTable = pItem->iCursor;            pMatch = pItem;            pExpr->pSchema = pTab->pSchema;            /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */            pExpr->iColumn = j==pTab->iPKey ? -1 : j;            pExpr->affinity = pTab->aCol[j].affinity;            pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);            if( pItem->jointype & JT_NATURAL ){              /* If this match occurred in the left table of a natural join,              ** then skip the right table to avoid a duplicate match */              pItem++;              i++;            }            if( (pUsing = pItem->pUsing)!=0 ){              /* If this match occurs on a column that is in the USING clause              ** of a join, skip the search of the right table of the join              ** to avoid a duplicate match there. */              int k;              for(k=0; k<pUsing->nId; k++){                if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){                  pItem++;                  i++;                  break;                }              }            }            break;          }        }      }    }#ifndef SQLITE_OMIT_TRIGGER    /* If we have not already resolved the name, then maybe     ** it is a new.* or old.* trigger argument reference    */    if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){      TriggerStack *pTriggerStack = pParse->trigStack;      Table *pTab = 0;      if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){        pExpr->iTable = pTriggerStack->newIdx;        assert( pTriggerStack->pTab );        pTab = pTriggerStack->pTab;      }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){        pExpr->iTable = pTriggerStack->oldIdx;        assert( pTriggerStack->pTab );        pTab = pTriggerStack->pTab;      }      if( pTab ){         int iCol;        Column *pCol = pTab->aCol;        pExpr->pSchema = pTab->pSchema;        cntTab++;        for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {          if( sqlite3StrICmp(pCol->zName, zCol)==0 ){            const char *zColl = pTab->aCol[iCol].zColl;            cnt++;            pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;            pExpr->affinity = pTab->aCol[iCol].affinity;            pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);            pExpr->pTab = pTab;            break;          }        }      }    }#endif /* !defined(SQLITE_OMIT_TRIGGER) */    /*    ** Perhaps the name is a reference to the ROWID    */    if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){      cnt = 1;      pExpr->iColumn = -1;      pExpr->affinity = SQLITE_AFF_INTEGER;    }    /*    ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z    ** might refer to an result-set alias.  This happens, for example, when    ** we are resolving names in the WHERE clause of the following command:    **    **     SELECT a+b AS x FROM table WHERE x<10;    **    ** In cases like this, replace pExpr with a copy of the expression that    ** forms the result set entry ("a+b" in the example) and return immediately.    ** Note that the expression in the result set should have already been    ** resolved by the time the WHERE clause is resolved.    */    if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){      for(j=0; j<pEList->nExpr; j++){        char *zAs = pEList->a[j].zName;        if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){          assert( pExpr->pLeft==0 && pExpr->pRight==0 );          pExpr->op = TK_AS;          pExpr->iColumn = j;          pExpr->pLeft = sqlite3ExprDup(pEList->a[j].pExpr);          cnt = 1;          assert( zTab==0 && zDb==0 );          goto lookupname_end_2;        }      }     }    /* Advance to the next name context.  The loop will exit when either    ** we have a match (cnt>0) or when we run out of name contexts.    */    if( cnt==0 ){      pNC = pNC->pNext;    }  }  /*  ** If X and Y are NULL (in other words if only the column name Z is  ** supplied) and the value of Z is enclosed in double-quotes, then  ** Z is a string literal if it doesn't match any column names.  In that  ** case, we need to return right away and not make any changes to  ** pExpr.  **  ** Because no reference was made to outer contexts, the pNC->nRef  ** fields are not changed in any context.  */  if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){    sqliteFree(zCol);    return 0;  }  /*  ** cnt==0 means there was not match.  cnt>1 means there were two or  ** more matches.  Either way, we have an error.  */  if( cnt!=1 ){    char *z = 0;    char *zErr;    zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";    if( zDb ){      sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);    }else if( zTab ){      sqlite3SetString(&z, zTab, ".", zCol, (char*)0);    }else{      z = sqliteStrDup(zCol);    }    sqlite3ErrorMsg(pParse, zErr, z);    sqliteFree(z);    pTopNC->nErr++;  }  /* If a column from a table in pSrcList is referenced, then record  ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes  ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the  ** column number is greater than the number of bits in the bitmask  ** then set the high-order bit of the bitmask.  */  if( pExpr->iColumn>=0 && pMatch!=0 ){    int n = pExpr->iColumn;    if( n>=sizeof(Bitmask)*8 ){      n = sizeof(Bitmask)*8-1;    }    assert( pMatch->iCursor==pExpr->iTable );    pMatch->colUsed |= 1<<n;  }lookupname_end:  /* Clean up and return  */  sqliteFree(zDb);  sqliteFree(zTab);  sqlite3ExprDelete(pExpr->pLeft);  pExpr->pLeft = 0;  sqlite3ExprDelete(pExpr->pRight);  pExpr->pRight = 0;  pExpr->op = TK_COLUMN;lookupname_end_2:  sqliteFree(zCol);  if( cnt==1 ){    assert( pNC!=0 );    sqlite3AuthRead(pParse, pExpr, pNC->pSrcList);    if( pMatch && !pMatch->pSelect ){      pExpr->pTab = pMatch->pTab;    }    /* Increment the nRef value on all name contexts from TopNC up to    ** the point where the name matched. */    for(;;){      assert( pTopNC!=0 );      pTopNC->nRef++;      if( pTopNC==pNC ) break;      pTopNC = pTopNC->pNext;    }    return 0;  } else {    return 1;  }}/*** This routine is designed as an xFunc for walkExprTree().**** Resolve symbolic names into TK_COLUMN operators for the current** node in the expression tree.  Return 0 to continue the search down** the tree or 2 to abort the tree walk.**** This routine also does error checking and name resolution for** function names.  The operator for aggregate functions is changed** to TK_AGG_FUNCTION.*/static int nameResolverStep(void *pArg, Expr *pExpr){  NameContext *pNC = (NameContext*)pArg;  Parse *pParse;  if( pExpr==0 ) return 1;  assert( pNC!=0 );  pParse = pNC->pParse;  if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;  ExprSetProperty(pExpr, EP_Resolved);#ifndef NDEBUG  if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){    SrcList *pSrcList = pNC->pSrcList;    int i;    for(i=0; i<pNC->pSrcList->nSrc; i++){      assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);    }  }#endif  switch( pExpr->op ){    /* Double-quoted strings (ex: "abc") are used as identifiers if    ** possible.  Otherwise they remain as strings.  Single-quoted    ** strings (ex: 'abc') are always string literals.    */    case TK_STRING: {      if( pExpr->token.z[0]=='\'' ) break;      /* Fall thru into the TK_ID case if this is a double-quoted string */    }    /* A lone identifier is the name of a column.    */    case TK_ID: {      lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);      return 1;    }      /* A table name and column name:     ID.ID    ** Or a database, table and column:  ID.ID.ID    */    case TK_DOT: {      Token *pColumn;      Token *pTable;      Token *pDb;      Expr *pRight;      /* if( pSrcList==0 ) break; */      pRight = pExpr->pRight;      if( pRight->op==TK_ID ){        pDb = 0;        pTable = &pExpr->pLeft->token;        pColumn = &pRight->token;      }else{        assert( pRight->op==TK_DOT );        pDb = &pExpr->pLeft->token;        pTable = &pRight->pLeft->token;        pColumn = &pRight->pRight->token;      }      lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);      return 1;    }    /* Resolve function names    */    case TK_CONST_FUNC:    case TK_FUNCTION: {      ExprList *pList = pExpr->pList;    /* The argument list */      int n = pList ? pList->nExpr : 0;  /* Number of arguments */      int no_such_func = 0;       /* True if no such function exists */      int wrong_num_args = 0;     /* True if wrong number of arguments */      int is_agg = 0;             /* True if is an aggregate function */      int i;      int auth;                   /* Authorization to use the function */      int nId;                    /* Number of characters in function name */      const char *zId;            /* The function name. */      FuncDef *pDef;              /* Information about the function */      int enc = ENC(pParse->db);  /* The database encoding */      zId = (char*)pExpr->token.z;      nId = pExpr->token.n;      pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);      if( pDef==0 ){        pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);        if( pDef==0 ){          no_such_func = 1;        }else{          wrong_num_args = 1;        }      }else{        is_agg = pDef->xFunc==0;      }#ifndef SQLITE_OMIT_AUTHORIZER      if( pDef ){        auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);        if( auth!=SQLITE_OK ){          if( auth==SQLITE_DENY ){            sqlite3ErrorMsg(pParse, "not authorized to use function: %s",                                    pDef->zName);            pNC->nErr++;          }          pExpr->op = TK_NULL;          return 1;        }      }#endif      if( is_agg && !pNC->allowAgg ){        sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);        pNC->nErr++;        is_agg = 0;      }else if( no_such_func ){        sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);        pNC->nErr++;      }else if( wrong_num_args ){        sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",             nId, zId);        pNC->nErr++;      }      if( is_agg ){        pExpr->op = TK_AGG_FUNCTION;        pNC->hasAgg = 1;      }      if( is_agg ) pNC->allowAgg = 0;      for(i=0; pNC->nErr==0 && i<n; i++){        walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);      }      if( is_agg ) pNC->allowAgg = 1;      /* FIX ME:  Compute pExpr->affinity based on the expected return      ** type of the function       */      return is_agg;    }#ifndef SQLITE_OMIT_SUBQUERY    case TK_SELECT:

⌨️ 快捷键说明

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