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

📄 select.c

📁 sqlite-3.4.1,嵌入式数据库.是一个功能强大的开源数据库,给学习和研发以及小型公司的发展带来了全所未有的好处.
💻 C
📖 第 1 页 / 共 5 页
字号:
*/static void createSortingIndex(Parse *pParse, Select *p, ExprList *pOrderBy){  if( pOrderBy ){    int addr;    assert( pOrderBy->iECursor==0 );    pOrderBy->iECursor = pParse->nTab++;    addr = sqlite3VdbeAddOp(pParse->pVdbe, OP_OpenEphemeral,                            pOrderBy->iECursor, pOrderBy->nExpr+1);    assert( p->addrOpenEphm[2] == -1 );    p->addrOpenEphm[2] = addr;  }}#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 */  int nCol;             /* Number of columns in the result set */  ExprList *pOrderBy;   /* The ORDER BY clause on p */  int aSetP2[2];        /* Set P2 value of these op to number of columns */  int nSetP2 = 0;       /* Number of slots in aSetP2[] used */  /* 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;  assert( pPrior->pRightmost!=pPrior );  assert( pPrior->pRightmost==p->pRightmost );  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;  }  /* Create the destination temporary table if necessary  */  if( eDest==SRT_EphemTab ){    assert( p->pEList );    assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) );    aSetP2[nSetP2++] = sqlite3VdbeAddOp(v, OP_OpenEphemeral, iParm, 0);    eDest = SRT_Table;  }  /* Generate code for the left and right SELECT statements.  */  pOrderBy = p->pOrderBy;  switch( p->op ){    case TK_ALL: {      if( pOrderBy==0 ){        int addr = 0;        assert( !pPrior->pLimit );        pPrior->pLimit = p->pLimit;        pPrior->pOffset = p->pOffset;        rc = sqlite3Select(pParse, pPrior, eDest, iParm, 0, 0, 0, aff);        p->pLimit = 0;        p->pOffset = 0;        if( rc ){          goto multi_select_end;        }        p->pPrior = 0;        p->iLimit = pPrior->iLimit;        p->iOffset = pPrior->iOffset;        if( p->iLimit>=0 ){          addr = sqlite3VdbeAddOp(v, OP_IfMemZero, p->iLimit, 0);          VdbeComment((v, "# Jump ahead if LIMIT reached"));        }        rc = sqlite3Select(pParse, p, eDest, iParm, 0, 0, 0, aff);        p->pPrior = pPrior;        if( rc ){          goto multi_select_end;        }        if( addr ){          sqlite3VdbeJumpHere(v, addr);        }        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 */      int addr;      priorOp = p->op==TK_ALL ? SRT_Table : SRT_Union;      if( eDest==priorOp && 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( pOrderBy && matchOrderbyToColumn(pParse, p, pOrderBy, unionTab,1) ){          rc = 1;          goto multi_select_end;        }        addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, unionTab, 0);        if( priorOp==SRT_Table ){          assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) );          aSetP2[nSetP2++] = addr;        }else{          assert( p->addrOpenEphm[0] == -1 );          p->addrOpenEphm[0] = addr;          p->pRightmost->usesEphm = 1;        }        createSortingIndex(pParse, p, pOrderBy);        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;      p->pOrderBy = 0;      p->disallowOrderBy = pOrderBy!=0;      pLimit = p->pLimit;      p->pLimit = 0;      pOffset = p->pOffset;      p->pOffset = 0;      rc = sqlite3Select(pParse, p, op, unionTab, 0, 0, 0, aff);      /* Query flattening in sqlite3Select() might refill p->pOrderBy.      ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */      sqlite3ExprListDelete(p->pOrderBy);      p->pPrior = pPrior;      p->pOrderBy = pOrderBy;      sqlite3ExprDelete(p->pLimit);      p->pLimit = pLimit;      p->pOffset = pOffset;      p->iLimit = -1;      p->iOffset = -1;      if( rc ){        goto multi_select_end;      }      /* Convert the data in the temporary table into whatever form      ** it is that we currently need.      */            if( eDest!=priorOp || unionTab!=iParm ){        int iCont, iBreak, iStart;        assert( p->pEList );        if( eDest==SRT_Callback ){          Select *pFirst = p;          while( pFirst->pPrior ) pFirst = pFirst->pPrior;          generateColumnNames(pParse, 0, pFirst->pEList);        }        iBreak = sqlite3VdbeMakeLabel(v);        iCont = sqlite3VdbeMakeLabel(v);        computeLimitRegisters(pParse, p, iBreak);        sqlite3VdbeAddOp(v, OP_Rewind, unionTab, iBreak);        iStart = sqlite3VdbeCurrentAddr(v);        rc = selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,                             pOrderBy, -1, eDest, iParm,                              iCont, iBreak, 0);        if( rc ){          rc = 1;          goto multi_select_end;        }        sqlite3VdbeResolveLabel(v, iCont);        sqlite3VdbeAddOp(v, OP_Next, unionTab, iStart);        sqlite3VdbeResolveLabel(v, iBreak);        sqlite3VdbeAddOp(v, OP_Close, unionTab, 0);      }      break;    }    case TK_INTERSECT: {      int tab1, tab2;      int iCont, iBreak, iStart;      Expr *pLimit, *pOffset;      int addr;      /* INTERSECT is different from the others since it requires      ** two temporary tables.  Hence it has its own case.  Begin      ** by allocating the tables we will need.      */      tab1 = pParse->nTab++;      tab2 = pParse->nTab++;      if( pOrderBy && matchOrderbyToColumn(pParse,p,pOrderBy,tab1,1) ){        rc = 1;        goto multi_select_end;      }      createSortingIndex(pParse, p, pOrderBy);      addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab1, 0);      assert( p->addrOpenEphm[0] == -1 );      p->addrOpenEphm[0] = addr;      p->pRightmost->usesEphm = 1;      assert( p->pEList );      /* Code the SELECTs to our left into temporary table "tab1".      */      rc = sqlite3Select(pParse, pPrior, SRT_Union, tab1, 0, 0, 0, aff);      if( rc ){        goto multi_select_end;      }      /* Code the current SELECT into temporary table "tab2"      */      addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, tab2, 0);      assert( p->addrOpenEphm[1] == -1 );      p->addrOpenEphm[1] = addr;      p->pPrior = 0;      pLimit = p->pLimit;      p->pLimit = 0;      pOffset = p->pOffset;      p->pOffset = 0;      rc = sqlite3Select(pParse, p, SRT_Union, tab2, 0, 0, 0, aff);      p->pPrior = pPrior;      sqlite3ExprDelete(p->pLimit);      p->pLimit = pLimit;      p->pOffset = pOffset;      if( rc ){        goto multi_select_end;      }      /* Generate code to take the intersection of the two temporary      ** tables.      */      assert( p->pEList );      if( eDest==SRT_Callback ){        Select *pFirst = p;        while( pFirst->pPrior ) pFirst = pFirst->pPrior;        generateColumnNames(pParse, 0, pFirst->pEList);      }      iBreak = sqlite3VdbeMakeLabel(v);      iCont = sqlite3VdbeMakeLabel(v);      computeLimitRegisters(pParse, p, iBreak);      sqlite3VdbeAddOp(v, OP_Rewind, tab1, iBreak);      iStart = sqlite3VdbeAddOp(v, OP_RowKey, tab1, 0);      sqlite3VdbeAddOp(v, OP_NotFound, tab2, iCont);      rc = selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,                             pOrderBy, -1, eDest, iParm,                              iCont, iBreak, 0);      if( rc ){        rc = 1;        goto multi_select_end;      }      sqlite3VdbeResolveLabel(v, iCont);      sqlite3VdbeAddOp(v, OP_Next, tab1, iStart);      sqlite3VdbeResolveLabel(v, iBreak);      sqlite3VdbeAddOp(v, OP_Close, tab2, 0);      sqlite3VdbeAddOp(v, OP_Close, tab1, 0);      break;    }  }  /* Make sure all SELECTs in the statement have the same number of elements  ** in their result sets.  */  assert( p->pEList && pPrior->pEList );  if( p->pEList->nExpr!=pPrior->pEList->nExpr ){    sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"      " do not have the same number of result columns", selectOpName(p->op));    rc = 1;    goto multi_select_end;  }  /* Set the number of columns in temporary tables  */  nCol = p->pEList->nExpr;  while( nSetP2 ){    sqlite3VdbeChangeP2(v, aSetP2[--nSetP2], nCol);  }  /* Compute collating sequences used by either the ORDER BY clause or  ** by any temporary tables needed to implement the compound select.  ** Attach the KeyInfo structure to all temporary tables.  Invoke the  ** ORDER BY processing if there is an ORDER BY clause.  **  ** This section is run by the right-most SELECT statement only.  ** SELECT statements to the left always skip this part.  The right-most  ** SELECT might also skip this part if it has no ORDER BY clause and  ** no temp tables are required.  */  if( pOrderBy || p->usesEphm ){    int i;                        /* Loop counter */    KeyInfo *pKeyInfo;            /* Collating sequence for the result set */    Select *pLoop;                /* For looping through SELECT statements */    int nKeyCol;                  /* Number of entries in pKeyInfo->aCol[] */    CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */    CollSeq **aCopy;              /* A copy of pKeyInfo->aColl[] */    assert( p->pRightmost==p );    nKeyCol = nCol + (pOrderBy ? pOrderBy->nExpr : 0);    pKeyInfo = sqliteMalloc(sizeof(*pKeyInfo)+nKeyCol*(sizeof(CollSeq*) + 1));    if( !pKeyInfo ){      rc = SQLITE_NOMEM;      goto multi_select_end;    }    pKeyInfo->enc = ENC(pParse->db);    pKeyInfo->nField = nCol;    for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){      *apColl = multiSelectCollSeq(pParse, p, i);      if( 0==*

⌨️ 快捷键说明

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