📄 select.c
字号:
/* In a NATURAL join, omit the join columns from the ** table on the right */ continue; } if( sqlite3IdListIndex(pLeft->pUsing, zName)>=0 ){ /* In a join with a USING clause, omit columns in the ** using clause from the table on the right. */ continue; } } pRight = sqlite3Expr(TK_ID, 0, 0, 0); if( pRight==0 ) break; setToken(&pRight->token, zName); if( zTabName && (longNames || pTabList->nSrc>1) ){ pLeft = sqlite3Expr(TK_ID, 0, 0, 0); pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0); if( pExpr==0 ) break; setToken(&pLeft->token, zTabName); setToken(&pExpr->span, sqlite3MPrintf("%s.%s", zTabName, zName)); pExpr->span.dyn = 1; pExpr->token.z = 0; pExpr->token.n = 0; pExpr->token.dyn = 0; }else{ pExpr = pRight; pExpr->span = pExpr->token; } if( longNames ){ pNew = sqlite3ExprListAppend(pNew, pExpr, &pExpr->span); }else{ pNew = sqlite3ExprListAppend(pNew, pExpr, &pRight->token); } } } if( !tableSeen ){ if( zTName ){ sqlite3ErrorMsg(pParse, "no such table: %s", zTName); }else{ sqlite3ErrorMsg(pParse, "no tables specified"); } rc = 1; } sqliteFree(zTName); } } sqlite3ExprListDelete(pEList); p->pEList = pNew; } return rc;}#ifndef SQLITE_OMIT_COMPOUND_SELECT/*** 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.*/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( 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; }}/*** Allocate a virtual index to use for sorting.*/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_OpenVirtual, pOrderBy->iECursor, pOrderBy->nExpr+1); assert( p->addrOpenVirt[2] == -1 ); p->addrOpenVirt[2] = addr; }}/*** The opcode at addr is an OP_OpenVirtual that created a sorting** index tha we ended up not needing. This routine changes that** opcode to OP_Noop.*/static void uncreateSortingIndex(Parse *pParse, int addr){ Vdbe *v = pParse->pVdbe; VdbeOp *pOp = sqlite3VdbeGetOp(v, addr); sqlite3VdbeChangeP3(v, addr, 0, 0); pOp->opcode = OP_Noop; pOp->p1 = 0; pOp->p2 = 0;}#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_VirtualTab ){ assert( p->pEList ); assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) ); aSetP2[nSetP2++] = sqlite3VdbeAddOp(v, OP_OpenVirtual, 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 ){ 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 */ 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_OpenVirtual, unionTab, 0); if( priorOp==SRT_Table ){ assert( nSetP2<sizeof(aSetP2)/sizeof(aSetP2[0]) ); aSetP2[nSetP2++] = addr; }else{ assert( p->addrOpenVirt[0] == -1 ); p->addrOpenVirt[0] = addr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -