📄 select.c
字号:
if( p==0 || p->pSrc==0 || db->mallocFailed ){ return 1; } pTabList = p->pSrc; pEList = p->pEList; /* Make sure cursor numbers have been assigned to all entries in ** the FROM clause of the SELECT statement. */ sqlite3SrcListAssignCursors(pParse, p->pSrc); /* Look up every table named in the FROM clause of the select. If ** an entry of the FROM clause is a subquery instead of a table or view, ** then create a transient table structure to describe the subquery. */ for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ Table *pTab; if( pFrom->pTab!=0 ){ /* This statement has already been prepared. There is no need ** to go further. */ assert( i==0 ); return 0; } if( pFrom->zName==0 ){#ifndef SQLITE_OMIT_SUBQUERY /* A sub-query in the FROM clause of a SELECT */ assert( pFrom->pSelect!=0 ); if( pFrom->zAlias==0 ){ pFrom->zAlias = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pFrom->pSelect); } assert( pFrom->pTab==0 ); pFrom->pTab = pTab = sqlite3ResultSetOfSelect(pParse, pFrom->zAlias, pFrom->pSelect); if( pTab==0 ){ return 1; } /* The isEphem 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->isEphem = 1;#endif }else{ /* An ordinary table or view name in the FROM clause */ assert( pFrom->pTab==0 ); pFrom->pTab = pTab = sqlite3LocateTable(pParse,pFrom->zName,pFrom->zDatabase); if( pTab==0 ){ return 1; } pTab->nRef++;#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE) if( pTab->pSelect || IsVirtual(pTab) ){ /* We reach here if the named table is a really a view */ if( sqlite3ViewGetColumnNames(pParse, pTab) ){ return 1; } /* If pFrom->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( pFrom->pSelect==0 ){ pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect); } }#endif } } /* 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; int flags = pParse->db->flags; int longNames = (flags & SQLITE_FullColNames)!=0 && (flags & SQLITE_ShortColNames)==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 = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr, 0); if( pNew ){ pNew->a[pNew->nExpr-1].zName = a[k].zName; }else{ rc = 1; } 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 = sqlite3NameFromToken(db, &pE->pLeft->token); }else{ zTName = 0; } for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ Table *pTab = pFrom->pTab; char *zTabName = pFrom->zAlias; if( zTabName==0 || zTabName[0]==0 ){ zTabName = pTab->zName; } if( zTName && (zTabName==0 || zTabName[0]==0 || sqlite3StrICmp(zTName, zTabName)!=0) ){ continue; } tableSeen = 1; for(j=0; j<pTab->nCol; j++){ Expr *pExpr, *pRight; char *zName = pTab->aCol[j].zName; /* If a column is marked as 'hidden' (currently only possible ** for virtual tables), do not include it in the expanded ** result-set list. */ if( IsHiddenColumn(&pTab->aCol[j]) ){ assert(IsVirtual(pTab)); continue; } if( i>0 ){ struct SrcList_item *pLeft = &pTabList->a[i-1]; if( (pLeft[1].jointype & JT_NATURAL)!=0 && columnIndex(pLeft->pTab, zName)>=0 ){ /* In a NATURAL join, omit the join columns from the ** table on the right */ continue; } if( sqlite3IdListIndex(pLeft[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 = sqlite3PExpr(pParse, TK_ID, 0, 0, 0); if( pRight==0 ) break; setQuotedToken(pParse, &pRight->token, zName); if( zTabName && (longNames || pTabList->nSrc>1) ){ Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, 0); pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); if( pExpr==0 ) break; setQuotedToken(pParse, &pLeft->token, zTabName); setToken(&pExpr->span, sqlite3MPrintf(db, "%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; pExpr->span.dyn = 0; } if( longNames ){ pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pExpr->span); }else{ pNew = sqlite3ExprListAppend(pParse, pNew, pExpr, &pRight->token); } } } if( !tableSeen ){ if( zTName ){ sqlite3ErrorMsg(pParse, "no such table: %s", zTName); }else{ sqlite3ErrorMsg(pParse, "no tables specified"); } rc = 1; } sqlite3_free(zTName); } } sqlite3ExprListDelete(pEList); p->pEList = pNew; } if( p->pEList && p->pEList->nExpr>SQLITE_MAX_COLUMN ){ sqlite3ErrorMsg(pParse, "too many columns in result set"); rc = SQLITE_ERROR; } if( db->mallocFailed ){ rc = SQLITE_NOMEM; } return rc;}/*** pE is a pointer to an expression which is a single term in** ORDER BY or GROUP BY clause.**** If pE evaluates to an integer constant i, then return i.** This is an indication to the caller that it should sort** by the i-th column of the result set.**** If pE is a well-formed expression and the SELECT statement** is not compound, then return 0. This indicates to the** caller that it should sort by the value of the ORDER BY** expression.**** If the SELECT is compound, then attempt to match pE against** result set columns in the left-most SELECT statement. Return** the index i of the matching column, as an indication to the ** caller that it should sort by the i-th column. If there is** no match, return -1 and leave an error message in pParse.*/static int matchOrderByTermToExprList( Parse *pParse, /* Parsing context for error messages */ Select *pSelect, /* The SELECT statement with the ORDER BY clause */ Expr *pE, /* The specific ORDER BY term */ int idx, /* When ORDER BY term is this */ int isCompound, /* True if this is a compound SELECT */ u8 *pHasAgg /* True if expression contains aggregate functions */){ int i; /* Loop counter */ ExprList *pEList; /* The columns of the result set */ NameContext nc; /* Name context for resolving pE */ /* If the term is an integer constant, return the value of that ** constant */ pEList = pSelect->pEList; if( sqlite3ExprIsInteger(pE, &i) ){ if( i<=0 ){ /* If i is too small, make it too big. That way the calling ** function still sees a value that is out of range, but does ** not confuse the column number with 0 or -1 result code. */ i = pEList->nExpr+1; } return i; } /* If the term is a simple identifier that try to match that identifier ** against a column name in the result set. */ if( pE->op==TK_ID || (pE->op==TK_STRING && pE->token.z[0]!='\'') ){ sqlite3 *db = pParse->db; char *zCol = sqlite3NameFromToken(db, &pE->token); if( zCol==0 ){ return -1; } for(i=0; i<pEList->nExpr; i++){ char *zAs = pEList->a[i].zName; if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){ sqlite3_free(zCol); return i+1; } } sqlite3_free(zCol); } /* Resolve all names in the ORDER BY term expression */ memset(&nc, 0, sizeof(nc)); nc.pParse = pParse; nc.pSrcList = pSelect->pSrc; nc.pEList = pEList; nc.allowAgg = 1; nc.nErr = 0; if( sqlite3ExprResolveNames(&nc, pE) ){ if( isCompound ){ sqlite3ErrorClear(pParse); return 0; }else{ return -1; } } if( nc.hasAgg && pHasAgg ){ *pHasAgg = 1; } /* For a compound SELECT, we need to try to match the ORDER BY ** expression against an expression in the result set */ if( isCompound ){ for(i=0; i<pEList->nExpr; i++){ if( sqlite3ExprCompare(pEList->a[i].pExpr, pE) ){ return i+1; } } } return 0;}/*** Analyze and ORDER BY or GROUP BY clause in a simple SELECT statement.** Return the number of errors seen.**** Every term of the ORDER BY or GROUP BY clause needs to be an** expression. If any expression is an integer constant, then** that expression is replaced by the corresponding ** expression from the result set.*/static int processOrderGroupBy( Parse *pParse, /* Parsing context. Leave error messages here */ Select *pSelect, /* The SELECT statement containing the clause */ ExprList *pOrderBy, /* The ORDER BY or GROUP BY clause to be processed */ int isOrder, /* 1 for ORDER BY. 0 for GROUP BY */ u8 *pHasAgg /* Set to TRUE if any term contains an aggregate */){ int i; sqlite3 *db = pParse->db; ExprList *pEList; if( pOrderBy==0 ) return 0; if( pOrderBy->nExpr>SQLITE_MAX_COLUMN ){ const char *zType = isOrder ? "ORDER" : "GROUP"; sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType); return 1; } pEList = pSelect->pEList; if( pEList==0 ){ return 0; } for(i=0; i<pOrderBy->nExpr; i++){ int iCol; Expr *pE = pOrderBy->a[i].pExpr; iCol = matchOrderByTermToExprList(pParse, pSelect, pE, i+1, 0, pHasAgg); if( iCol<0 ){ return 1; } if( iCol>pEList->nExpr ){ const char *zType = isOrder ? "ORDER" : "GROUP"; sqlite3ErrorMsg(pParse, "%r %s BY term out of range - should be " "between 1 and %d", i+1, zType, pEList->nExpr); return 1; } if( iCol>0 ){ CollSeq *pColl = pE->pColl; int flags = pE->flags & EP_ExpCollate; sqlite3ExprDelete(pE); pE = sqlite3ExprDup(db, pEList->a[iCol-1].pExpr); pOrderBy->a[i].pExpr = pE; if( pColl && flags ){ pE->pColl = pColl; pE->flags |= flags; } } } return 0;}/*** Analyze and ORDER BY or GROUP BY clause in a SELECT statement. Return** the number of errors seen.**** The processing depends on whether the SELECT is simple or compound.** For a simple SELECT statement, evry term of the ORDER BY or GROUP BY** clause needs to be an expression. If any expression is an integer** constant, then that expression is replaced by the corresponding ** expression from the result set.**** For compound SELECT statements, every expression needs to be of** type TK_COLUMN with a iTable value as given in the 4th parameter.** If any expression is an integer, that becomes the column number.** Otherwise, match the expression against result set columns from** the left-most SELECT.*/static int processCompoundOrderBy( Parse *pParse, /* Parsing context. Leave error messages here */ Select *pSelect, /* The SELECT statement containing the ORDER BY */ int iTable /* Output table for compound SELECT statements */){ int i; ExprList *pOrderBy; ExprList *pEList;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -