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

📄 select.c

📁 sqlite数据库管理系统开放源码
💻 C
📖 第 1 页 / 共 5 页
字号:
Table *sqliteResultSetOfSelect(Parse *pParse, char *zTabName, Select *pSelect){  Table *pTab;  int i, j;  ExprList *pEList;  Column *aCol;  if( fillInColumnList(pParse, pSelect) ){    return 0;  }  pTab = sqliteMalloc( sizeof(Table) );  if( pTab==0 ){    return 0;  }  pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0;  pEList = pSelect->pEList;  pTab->nCol = pEList->nExpr;  assert( pTab->nCol>0 );  pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol );  for(i=0; i<pTab->nCol; i++){    Expr *p, *pR;    if( pEList->a[i].zName ){      aCol[i].zName = sqliteStrDup(pEList->a[i].zName);    }else if( (p=pEList->a[i].pExpr)->op==TK_DOT                && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){      int cnt;      sqliteSetNString(&aCol[i].zName, pR->token.z, pR->token.n, 0);      for(j=cnt=0; j<i; j++){        if( sqliteStrICmp(aCol[j].zName, aCol[i].zName)==0 ){          int n;          char zBuf[30];          sprintf(zBuf,"_%d",++cnt);          n = strlen(zBuf);          sqliteSetNString(&aCol[i].zName, pR->token.z, pR->token.n, zBuf, n,0);          j = -1;        }      }    }else if( p->span.z && p->span.z[0] ){      sqliteSetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0);    }else{      char zBuf[30];      sprintf(zBuf, "column%d", i+1);      aCol[i].zName = sqliteStrDup(zBuf);    }    sqliteDequote(aCol[i].zName);  }  pTab->iPKey = -1;  return pTab;}/*** For the given SELECT statement, do three things.****    (1)  Fill in the pTabList->a[].pTab fields in the SrcList that **         defines the set of tables that should be scanned.  For views,**         fill pTabList->a[].pSelect with a copy of the SELECT statement**         that implements the view.  A copy is made of the view's SELECT**         statement so that we can freely modify or delete that statement**         without worrying about messing up the presistent representation**         of the view.****    (2)  Add terms to the WHERE clause to accomodate the NATURAL keyword**         on joins and the ON and USING clause of joins.****    (3)  Scan the list of columns in the result set (pEList) looking**         for instances of the "*" operator or the TABLE.* operator.**         If found, expand each "*" to be every column in every table**         and TABLE.* to be every column in TABLE.**** Return 0 on success.  If there are problems, leave an error message** in pParse and return non-zero.*/static int fillInColumnList(Parse *pParse, Select *p){  int i, j, k, rc;  SrcList *pTabList;  ExprList *pEList;  Table *pTab;  if( p==0 || p->pSrc==0 ) return 1;  pTabList = p->pSrc;  pEList = p->pEList;  /* Look up every table in the table list.  */  for(i=0; i<pTabList->nSrc; i++){    if( pTabList->a[i].pTab ){      /* This routine has run before!  No need to continue */      return 0;    }    if( pTabList->a[i].zName==0 ){      /* A sub-query in the FROM clause of a SELECT */      assert( pTabList->a[i].pSelect!=0 );      if( pTabList->a[i].zAlias==0 ){        char zFakeName[60];        sprintf(zFakeName, "sqlite_subquery_%p_",           (void*)pTabList->a[i].pSelect);        sqliteSetString(&pTabList->a[i].zAlias, zFakeName, 0);      }      pTabList->a[i].pTab = pTab =         sqliteResultSetOfSelect(pParse, pTabList->a[i].zAlias,                                        pTabList->a[i].pSelect);      if( pTab==0 ){        return 1;      }      /* The isTransient flag indicates that the Table structure has been      ** dynamically allocated and may be freed at any time.  In other words,      ** pTab is not pointing to a persistent table structure that defines      ** part of the schema. */      pTab->isTransient = 1;    }else{      /* An ordinary table or view name in the FROM clause */      pTabList->a[i].pTab = pTab =         sqliteLocateTable(pParse,pTabList->a[i].zName,pTabList->a[i].zDatabase);      if( pTab==0 ){        return 1;      }      if( pTab->pSelect ){        /* We reach here if the named table is a really a view */        if( sqliteViewGetColumnNames(pParse, pTab) ){          return 1;        }        /* If pTabList->a[i].pSelect!=0 it means we are dealing with a        ** view within a view.  The SELECT structure has already been        ** copied by the outer view so we can skip the copy step here        ** in the inner view.        */        if( pTabList->a[i].pSelect==0 ){          pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect);        }      }    }  }  /* Process NATURAL keywords, and ON and USING clauses of joins.  */  if( sqliteProcessJoin(pParse, p) ) return 1;  /* For every "*" that occurs in the column list, insert the names of  ** all columns in all tables.  And for every TABLE.* insert the names  ** of all columns in TABLE.  The parser inserted a special expression  ** with the TK_ALL operator for each "*" that it found in the column list.  ** The following code just has to locate the TK_ALL expressions and expand  ** each one to the list of all columns in all tables.  **  ** The first loop just checks to see if there are any "*" operators  ** that need expanding.  */  for(k=0; k<pEList->nExpr; k++){    Expr *pE = pEList->a[k].pExpr;    if( pE->op==TK_ALL ) break;    if( pE->op==TK_DOT && pE->pRight && pE->pRight->op==TK_ALL         && pE->pLeft && pE->pLeft->op==TK_ID ) break;  }  rc = 0;  if( k<pEList->nExpr ){    /*    ** If we get here it means the result set contains one or more "*"    ** operators that need to be expanded.  Loop through each expression    ** in the result set and expand them one by one.    */    struct ExprList_item *a = pEList->a;    ExprList *pNew = 0;    for(k=0; k<pEList->nExpr; k++){      Expr *pE = a[k].pExpr;      if( pE->op!=TK_ALL &&           (pE->op!=TK_DOT || pE->pRight==0 || pE->pRight->op!=TK_ALL) ){        /* This particular expression does not need to be expanded.        */        pNew = sqliteExprListAppend(pNew, a[k].pExpr, 0);        pNew->a[pNew->nExpr-1].zName = a[k].zName;        a[k].pExpr = 0;        a[k].zName = 0;      }else{        /* This expression is a "*" or a "TABLE.*" and needs to be        ** expanded. */        int tableSeen = 0;      /* Set to 1 when TABLE matches */        char *zTName;           /* text of name of TABLE */        if( pE->op==TK_DOT && pE->pLeft ){          zTName = sqliteTableNameFromToken(&pE->pLeft->token);        }else{          zTName = 0;        }        for(i=0; i<pTabList->nSrc; i++){          Table *pTab = pTabList->a[i].pTab;          char *zTabName = pTabList->a[i].zAlias;          if( zTabName==0 || zTabName[0]==0 ){             zTabName = pTab->zName;          }          if( zTName && (zTabName==0 || zTabName[0]==0 ||                  sqliteStrICmp(zTName, zTabName)!=0) ){            continue;          }          tableSeen = 1;          for(j=0; j<pTab->nCol; j++){            Expr *pExpr, *pLeft, *pRight;            char *zName = pTab->aCol[j].zName;            if( i>0 && (pTabList->a[i-1].jointype & JT_NATURAL)!=0 &&                columnIndex(pTabList->a[i-1].pTab, zName)>=0 ){              /* In a NATURAL join, omit the join columns from the               ** table on the right */              continue;            }            if( i>0 && sqliteIdListIndex(pTabList->a[i-1].pUsing, zName)>=0 ){              /* In a join with a USING clause, omit columns in the              ** using clause from the table on the right. */              continue;            }            pRight = sqliteExpr(TK_ID, 0, 0, 0);            if( pRight==0 ) break;            pRight->token.z = zName;            pRight->token.n = strlen(zName);            pRight->token.dyn = 0;            if( zTabName && pTabList->nSrc>1 ){              pLeft = sqliteExpr(TK_ID, 0, 0, 0);              pExpr = sqliteExpr(TK_DOT, pLeft, pRight, 0);              if( pExpr==0 ) break;              pLeft->token.z = zTabName;              pLeft->token.n = strlen(zTabName);              pLeft->token.dyn = 0;              sqliteSetString((char**)&pExpr->span.z, zTabName, ".", zName, 0);              pExpr->span.n = strlen(pExpr->span.z);              pExpr->span.dyn = 1;              pExpr->token.z = 0;              pExpr->token.n = 0;              pExpr->token.dyn = 0;            }else{              pExpr = pRight;              pExpr->span = pExpr->token;            }            pNew = sqliteExprListAppend(pNew, pExpr, 0);          }        }        if( !tableSeen ){          if( zTName ){            sqliteErrorMsg(pParse, "no such table: %s", zTName);          }else{            sqliteErrorMsg(pParse, "no tables specified");          }          rc = 1;        }        sqliteFree(zTName);      }    }    sqliteExprListDelete(pEList);    p->pEList = pNew;  }  return rc;}/*** This routine recursively unlinks the Select.pSrc.a[].pTab pointers** in a select structure.  It just sets the pointers to NULL.  This** routine is recursive in the sense that if the Select.pSrc.a[].pSelect** pointer is not NULL, this routine is called recursively on that pointer.**** This routine is called on the Select structure that defines a** VIEW in order to undo any bindings to tables.  This is necessary** because those tables might be DROPed by a subsequent SQL command.** If the bindings are not removed, then the Select.pSrc->a[].pTab field** will be left pointing to a deallocated Table structure after the** DROP and a coredump will occur the next time the VIEW is used.*/void sqliteSelectUnbind(Select *p){  int i;  SrcList *pSrc = p->pSrc;  Table *pTab;  if( p==0 ) return;  for(i=0; i<pSrc->nSrc; i++){    if( (pTab = pSrc->a[i].pTab)!=0 ){      if( pTab->isTransient ){        sqliteDeleteTable(0, pTab);      }      pSrc->a[i].pTab = 0;      if( pSrc->a[i].pSelect ){        sqliteSelectUnbind(pSrc->a[i].pSelect);      }    }  }}/*** This routine associates entries in an ORDER BY expression list with** columns in a result.  For each ORDER BY expression, the opcode of** the top-level node is changed to TK_COLUMN and the iColumn value of** the top-level node is filled in with column number and the iTable** value of the top-level node is filled with iTable parameter.**** If there are prior SELECT clauses, they are processed first.  A match** in an earlier SELECT takes precedence over a later SELECT.**** Any entry that does not match is flagged as an error.  The number** of errors is returned.**** This routine does NOT correctly initialize the Expr.dataType  field** of the ORDER BY expressions.  The multiSelectSortOrder() routine** must be called to do that after the individual select statements** have all been analyzed.  This routine is unable to compute Expr.dataType** because it must be called before the individual select statements** have been analyzed.*/static int matchOrderbyToColumn(  Parse *pParse,          /* A place to leave error messages */  Select *pSelect,        /* Match to result columns of this SELECT */  ExprList *pOrderBy,     /* The ORDER BY values to match against columns */  int iTable,             /* Insert this value in iTable */  int mustComplete        /* If TRUE all ORDER BYs must match */){  int nErr = 0;  int i, j;  ExprList *pEList;  if( pSelect==0 || pOrderBy==0 ) return 1;  if( mustComplete ){    for(i=0; i<pOrderBy->nExpr; i++){ pOrderBy->a[i].done = 0; }  }  if( fillInColumnList(pParse, pSelect) ){    return 1;  }  if( pSelect->pPrior ){    if( matchOrderbyToColumn(pParse, pSelect->pPrior, pOrderBy, iTable, 0) ){      return 1;    }  }  pEList = pSelect->pEList;  for(i=0; i<pOrderBy->nExpr; i++){    Expr *pE = pOrderBy->a[i].pExpr;    int iCol = -1;    if( pOrderBy->a[i].done ) continue;    if( sqliteExprIsInteger(pE, &iCol) ){      if( iCol<=0 || iCol>pEList->nExpr ){        sqliteErrorMsg(pParse,          "ORDER BY position %d should be between 1 and %d",          iCol, pEList->nExpr);        nErr++;        break;      }      if( !mustComplete ) continue;      iCol--;    }    for(j=0; iCol<0 && j<pEList->nExpr; j++){      if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){        char *zName, *zLabel;        zName = pEList->a[j].zName;        assert( pE->token.z );        zLabel = sqliteStrNDup(pE->token.z, pE->token.n);        sqliteDequote(zLabel);        if( sqliteStrICmp(zName, zLabel)==0 ){           iCol = j;        }        sqliteFree(zLabel);      }      if( iCol<0 && sqliteExprCompare(pE, pEList->a[j].pExpr) ){        iCol = j;      }    }    if( iCol>=0 ){      pE->op = TK_COLUMN;      pE->iColumn = iCol;      pE->iTable = iTable;      pOrderBy->a[i].done = 1;    }    if( iCol<0 && mustComplete ){      sqliteErrorMsg(pParse,        "ORDER BY term number %d does not match any result column", i+1);      nErr++;      break;    }  }  return nErr;  }/*** Get a VDBE for the given parser context.  Create a new one if necessary.** If an error occurs, return NULL and leave a message in pParse.*/Vdbe *sqliteGetVdbe(Parse *pParse){  Vdbe *v = pParse->pVdbe;  if( v==0 ){    v = pParse->pVdbe = sqliteVdbeCreate(pParse->db);  }  return v;}/*** This routine sets the Expr.dataType field on all elements of** the pOrderBy expression list.  The pOrderBy list will have been** set up by matchOrderbyToColumn().  Hence each expression has** a TK_COLUMN as its root node.  The Expr.iColumn refers to a ** column in the result set.   The datatype is set to SQLITE_SO_TEXT** if the corresponding column in p and every SELECT to the left of** p has a datatype of SQLITE_SO_TEXT.  If the cooressponding column** in p or any of the left SELECTs is SQLITE_SO_NUM, then the datatype** of the order-by expression is set to SQLITE_SO_NUM.

⌨️ 快捷键说明

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