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

📄 select.c

📁 sqlite 嵌入式数据库的源码
💻 C
📖 第 1 页 / 共 5 页
字号:
  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( prepSelectStmt(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( sqlite3ExprIsInteger(pE, &iCol) ){      if( iCol<=0 || iCol>pEList->nExpr ){        sqlite3ErrorMsg(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;        zLabel = sqlite3NameFromToken(&pE->token);        assert( zLabel!=0 );        if( sqlite3StrICmp(zName, zLabel)==0 ){           iCol = j;        }        sqliteFree(zLabel);      }      if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){        iCol = j;      }    }    if( iCol>=0 ){      pE->op = TK_COLUMN;      pE->iColumn = iCol;      pE->iTable = iTable;      pE->iAgg = -1;      pOrderBy->a[i].done = 1;    }    if( iCol<0 && mustComplete ){      sqlite3ErrorMsg(pParse,        "ORDER BY term number %d does not match any result column", i+1);      nErr++;      break;    }  }  return nErr;  }#endif /* #ifndef SQLITE_OMIT_COMPOUND_SELECT *//*** 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 *sqlite3GetVdbe(Parse *pParse){  Vdbe *v = pParse->pVdbe;  if( v==0 ){    v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db);  }  return v;}/*** Compute the iLimit and iOffset fields of the SELECT based on the** pLimit and pOffset expressions.  nLimit and nOffset hold the expressions** that appear in the original SQL statement after the LIMIT and OFFSET** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset ** are the integer memory register numbers for counters used to compute ** the limit and offset.  If there is no limit and/or offset, then ** iLimit and iOffset are negative.**** This routine changes the values if iLimit and iOffset only if** a limit or offset is defined by nLimit and nOffset.  iLimit and** iOffset should have been preset to appropriate default values** (usually but not always -1) prior to calling this routine.** Only if nLimit>=0 or nOffset>0 do the limit registers get** redefined.  The UNION ALL operator uses this property to force** the reuse of the same limit and offset registers across multiple** SELECT statements.*/static void computeLimitRegisters(Parse *pParse, Select *p){  /*   ** "LIMIT -1" always shows all rows.  There is some  ** contraversy about what the correct behavior should be.  ** The current implementation interprets "LIMIT 0" to mean  ** no rows.  */  if( p->pLimit ){    int iMem = pParse->nMem++;    Vdbe *v = sqlite3GetVdbe(pParse);    if( v==0 ) return;    sqlite3ExprCode(pParse, p->pLimit);    sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);    sqlite3VdbeAddOp(v, OP_Negative, 0, 0);    sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1);    VdbeComment((v, "# LIMIT counter"));    p->iLimit = iMem;  }  if( p->pOffset ){    int iMem = pParse->nMem++;    Vdbe *v = sqlite3GetVdbe(pParse);    if( v==0 ) return;    sqlite3ExprCode(pParse, p->pOffset);    sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);    sqlite3VdbeAddOp(v, OP_Negative, 0, 0);    sqlite3VdbeAddOp(v, OP_MemStore, iMem, 1);    VdbeComment((v, "# OFFSET counter"));    p->iOffset = iMem;  }}/*** Generate VDBE instructions that will open a transient table that** will be used for an index or to store keyed results for a compound** select.  In other words, open a transient table that needs a** KeyInfo structure.  The number of columns in the KeyInfo is determined** by the result set of the SELECT statement in the second argument.**** Specifically, this routine is called to open an index table for** DISTINCT, UNION, INTERSECT and EXCEPT select statements (but not ** UNION ALL).**** The value returned is the address of the OP_OpenTemp instruction.*/static int openTempIndex(Parse *pParse, Select *p, int iTab){  KeyInfo *pKeyInfo;  int nColumn;  sqlite3 *db = pParse->db;  int i;  Vdbe *v = pParse->pVdbe;  int addr;  if( prepSelectStmt(pParse, p) ){    return 0;  }  nColumn = p->pEList->nExpr;  pKeyInfo = sqliteMalloc( sizeof(*pKeyInfo)+nColumn*sizeof(CollSeq*) );  if( pKeyInfo==0 ) return 0;  pKeyInfo->enc = db->enc;  pKeyInfo->nField = nColumn;  for(i=0; i<nColumn; i++){    pKeyInfo->aColl[i] = sqlite3ExprCollSeq(pParse, p->pEList->a[i].pExpr);    if( !pKeyInfo->aColl[i] ){      pKeyInfo->aColl[i] = db->pDfltColl;    }  }  addr = sqlite3VdbeOp3(v, OP_OpenTemp, iTab, 0,       (char*)pKeyInfo, P3_KEYINFO_HANDOFF);  return addr;}#ifndef SQLITE_OMIT_COMPOUND_SELECT/*** Add the address "addr" to the set of all OpenTemp opcode addresses** that are being accumulated in p->ppOpenTemp.*/static int multiSelectOpenTempAddr(Select *p, int addr){  IdList *pList = *p->ppOpenTemp = sqlite3IdListAppend(*p->ppOpenTemp, 0);  if( pList==0 ){    return SQLITE_NOMEM;  }  pList->a[pList->nId-1].idx = addr;  return SQLITE_OK;}#endif /* SQLITE_OMIT_COMPOUND_SELECT */#ifndef SQLITE_OMIT_COMPOUND_SELECT/*** Return the appropriate collating sequence for the iCol-th column of** the result set for the compound-select statement "p".  Return NULL if** the column has no default collating sequence.**** The collating sequence for the compound select is taken from the** left-most term of the select that has a collating sequence.*/static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){  CollSeq *pRet;  if( p->pPrior ){    pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);  }else{    pRet = 0;  }  if( pRet==0 ){    pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);  }  return pRet;}#endif /* SQLITE_OMIT_COMPOUND_SELECT */#ifndef SQLITE_OMIT_COMPOUND_SELECT/*** This routine is called to process a query that is really the union** or intersection of two or more separate queries.**** "p" points to the right-most of the two queries.  the query on the** left is p->pPrior.  The left query could also be a compound query** in which case this routine will be called recursively. **** The results of the total query are to be written into a destination** of type eDest with parameter iParm.**** Example 1:  Consider a three-way compound SQL statement.****     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3**** This statement is parsed up as follows:****     SELECT c FROM t3**      |**      `----->  SELECT b FROM t2**                |**                `------>  SELECT a FROM t1**** The arrows in the diagram above represent the Select.pPrior pointer.** So if this routine is called with p equal to the t3 query, then** pPrior will be the t2 query.  p->op will be TK_UNION in this case.**** Notice that because of the way SQLite parses compound SELECTs, the** individual selects always group from left to right.*/static int multiSelect(  Parse *pParse,        /* Parsing context */  Select *p,            /* The right-most of SELECTs to be coded */  int eDest,            /* \___  Store query results as specified */  int iParm,            /* /     by these two parameters.         */  char *aff             /* If eDest is SRT_Union, the affinity string */){  int rc = SQLITE_OK;   /* Success code from a subroutine */  Select *pPrior;       /* Another SELECT immediately to our left */  Vdbe *v;              /* Generate code to this VDBE */  IdList *pOpenTemp = 0;/* OP_OpenTemp opcodes that need a KeyInfo */  int aAddr[5];         /* Addresses of SetNumColumns operators */  int nAddr = 0;        /* Number used */  int nCol;             /* Number of columns in the result set */  /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only  ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.  */  if( p==0 || p->pPrior==0 ){    rc = 1;    goto multi_select_end;  }  pPrior = p->pPrior;  if( pPrior->pOrderBy ){    sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",      selectOpName(p->op));    rc = 1;    goto multi_select_end;  }  if( pPrior->pLimit ){    sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",      selectOpName(p->op));    rc = 1;    goto multi_select_end;  }  /* Make sure we have a valid query engine.  If not, create a new one.  */  v = sqlite3GetVdbe(pParse);  if( v==0 ){    rc = 1;    goto multi_select_end;  }  /* If *p this is the right-most select statement, then initialize  ** p->ppOpenTemp to point to pOpenTemp.  If *p is not the right most  ** statement then p->ppOpenTemp will have already been initialized  ** by a prior call to this same procedure.  Pass along the pOpenTemp  ** pointer to pPrior, the next statement to our left.  */  if( p->ppOpenTemp==0 ){    p->ppOpenTemp = &pOpenTemp;  }  pPrior->ppOpenTemp = p->ppOpenTemp;  /* Create the destination temporary table if necessary  */  if( eDest==SRT_TempTable ){    assert( p->pEList );    sqlite3VdbeAddOp(v, OP_OpenTemp, iParm, 0);    assert( nAddr==0 );    aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, iParm, 0);    eDest = SRT_Table;  }  /* Generate code for the left and right SELECT statements.  */  switch( p->op ){    case TK_ALL: {      if( p->pOrderBy==0 ){        assert( !pPrior->pLimit );        pPrior->pLimit = p->pLimit;        pPrior->pOffset = p->pOffset;        rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff);        if( rc ){          goto multi_select_end;        }        p->pPrior = 0;        p->iLimit = pPrior->iLimit;        p->iOffset = pPrior->iOffset;        p->pLimit = 0;        p->pOffset = 0;        rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff);        p->pPrior = pPrior;        if( rc ){          goto multi_select_end;        }        break;      }      /* For UNION ALL ... ORDER BY fall through to the next case */    }    case TK_EXCEPT:    case TK_UNION: {      int unionTab;    /* Cursor number of the temporary table holding result */      int op = 0;      /* One of the SRT_ operations to apply to self */      int priorOp;     /* The SRT_ operation to apply to prior selects */      Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */      ExprList *pOrderBy;     /* The ORDER BY clause for the right SELECT */      int addr;      priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;      if( eDest==priorOp && p->pOrderBy==0 && !p->pLimit && !p->pOffset ){        /* We can reuse a temporary table generated by a SELECT to our        ** right.        */        unionTab = iParm;      }else{        /* We will need to create our own temporary table to hold the        ** intermediate results.        */        unionTab = pParse->nTab++;        if( p->pOrderBy         && matchOrderbyToColumn(pParse, p, p->pOrderBy, unionTab, 1) ){          rc = 1;          goto multi_select_end;        }        addr = sqlite3VdbeAddOp(v, OP_OpenTemp, unionTab, 0);        if( p->op!=TK_ALL ){          rc = multiSelectOpenTempAddr(p, addr);          if( rc!=SQLITE_OK ){            goto multi_select_end;          }        }	assert( nAddr<sizeof(aAddr)/sizeof(aAddr[0]) );        aAddr[nAddr++] = sqlite3VdbeAddOp(v, OP_SetNumColumns, unionTab, 0);        assert( p->pEList );      }      /* Code the SELECT statements to our left      */      assert( !pPrior->pOrderBy );      rc = sqlite3Select(pParse, pPrior, priorOp, unionTab, 0, 0, 0, aff);      if( rc ){        goto multi_select_end;      }      /* Code the current SELECT statement      */      switch( p->op ){         case TK_EXCEPT:  op = SRT_Except;   break;         case TK_UNION:   op = SRT_Union;    break;         case TK_ALL:     op = SRT_Table;    break;      }      p->pPrior = 0;      pOrderBy = p->pOrderBy;      p->pOrderBy = 0;      pLimit = p->pLimit;      p->pLimit = 0;      pOffset = p->pOffset;      p->pOffset = 0;      rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff);      p->pPrior = pPrior;      p->pOrderBy = pOrderBy;      sqlite3ExprDelete(p->pLimit);      p->pLimit = pLimit;      p->pOffset = pOffset;      p->iLimit = -1;      p->iOffset = -1;

⌨️ 快捷键说明

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