| | @@ -673,11 +673,11 @@ |
| 673 | 673 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 674 | 674 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 675 | 675 | */ |
| 676 | 676 | #define SQLITE_VERSION "3.7.16" |
| 677 | 677 | #define SQLITE_VERSION_NUMBER 3007016 |
| 678 | | -#define SQLITE_SOURCE_ID "2012-12-21 16:15:35 ff6857b6ed6a46671006b75157d8cf853a816ef9" |
| 678 | +#define SQLITE_SOURCE_ID "2013-01-07 13:26:23 0a1207c895d9f77586a3a2a4965175909be90503" |
| 679 | 679 | |
| 680 | 680 | /* |
| 681 | 681 | ** CAPI3REF: Run-Time Library Version Numbers |
| 682 | 682 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 683 | 683 | ** |
| | @@ -10553,24 +10553,24 @@ |
| 10553 | 10553 | ** and the value of Index.onError indicate the which conflict resolution |
| 10554 | 10554 | ** algorithm to employ whenever an attempt is made to insert a non-unique |
| 10555 | 10555 | ** element. |
| 10556 | 10556 | */ |
| 10557 | 10557 | struct Index { |
| 10558 | | - char *zName; /* Name of this index */ |
| 10559 | | - int *aiColumn; /* Which columns are used by this index. 1st is 0 */ |
| 10560 | | - tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */ |
| 10561 | | - Table *pTable; /* The SQL table being indexed */ |
| 10562 | | - char *zColAff; /* String defining the affinity of each column */ |
| 10563 | | - Index *pNext; /* The next index associated with the same table */ |
| 10564 | | - Schema *pSchema; /* Schema containing this index */ |
| 10565 | | - u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */ |
| 10566 | | - char **azColl; /* Array of collation sequence names for index */ |
| 10567 | | - int nColumn; /* Number of columns in the table used by this index */ |
| 10568 | | - int tnum; /* Page containing root of this index in database file */ |
| 10569 | | - u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 10570 | | - u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */ |
| 10571 | | - u8 bUnordered; /* Use this index for == or IN queries only */ |
| 10558 | + char *zName; /* Name of this index */ |
| 10559 | + int *aiColumn; /* Which columns are used by this index. 1st is 0 */ |
| 10560 | + tRowcnt *aiRowEst; /* From ANALYZE: Est. rows selected by each column */ |
| 10561 | + Table *pTable; /* The SQL table being indexed */ |
| 10562 | + char *zColAff; /* String defining the affinity of each column */ |
| 10563 | + Index *pNext; /* The next index associated with the same table */ |
| 10564 | + Schema *pSchema; /* Schema containing this index */ |
| 10565 | + u8 *aSortOrder; /* for each column: True==DESC, False==ASC */ |
| 10566 | + char **azColl; /* Array of collation sequence names for index */ |
| 10567 | + int tnum; /* DB Page containing root of this index */ |
| 10568 | + u16 nColumn; /* Number of columns in table used by this index */ |
| 10569 | + u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ |
| 10570 | + unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */ |
| 10571 | + unsigned bUnordered:1; /* Use this index for == or IN queries only */ |
| 10572 | 10572 | #ifdef SQLITE_ENABLE_STAT3 |
| 10573 | 10573 | int nSample; /* Number of elements in aSample[] */ |
| 10574 | 10574 | tRowcnt avgEq; /* Average nEq value for key values not in aSample */ |
| 10575 | 10575 | IndexSample *aSample; /* Samples of the left-most key */ |
| 10576 | 10576 | #endif |
| | @@ -10840,22 +10840,31 @@ |
| 10840 | 10840 | ** name. An expr/name combination can be used in several ways, such |
| 10841 | 10841 | ** as the list of "expr AS ID" fields following a "SELECT" or in the |
| 10842 | 10842 | ** list of "ID = expr" items in an UPDATE. A list of expressions can |
| 10843 | 10843 | ** also be used as the argument to a function, in which case the a.zName |
| 10844 | 10844 | ** field is not used. |
| 10845 | +** |
| 10846 | +** By default the Expr.zSpan field holds a human-readable description of |
| 10847 | +** the expression that is used in the generation of error messages and |
| 10848 | +** column labels. In this case, Expr.zSpan is typically the text of a |
| 10849 | +** column expression as it exists in a SELECT statement. However, if |
| 10850 | +** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name |
| 10851 | +** of the result column in the form: DATABASE.TABLE.COLUMN. This later |
| 10852 | +** form is used for name resolution with nested FROM clauses. |
| 10845 | 10853 | */ |
| 10846 | 10854 | struct ExprList { |
| 10847 | 10855 | int nExpr; /* Number of expressions on the list */ |
| 10848 | 10856 | int iECursor; /* VDBE Cursor associated with this ExprList */ |
| 10849 | 10857 | struct ExprList_item { /* For each expression in the list */ |
| 10850 | | - Expr *pExpr; /* The list of expressions */ |
| 10851 | | - char *zName; /* Token associated with this expression */ |
| 10852 | | - char *zSpan; /* Original text of the expression */ |
| 10853 | | - u8 sortOrder; /* 1 for DESC or 0 for ASC */ |
| 10854 | | - u8 done; /* A flag to indicate when processing is finished */ |
| 10855 | | - u16 iOrderByCol; /* For ORDER BY, column number in result set */ |
| 10856 | | - u16 iAlias; /* Index into Parse.aAlias[] for zName */ |
| 10858 | + Expr *pExpr; /* The list of expressions */ |
| 10859 | + char *zName; /* Token associated with this expression */ |
| 10860 | + char *zSpan; /* Original text of the expression */ |
| 10861 | + u8 sortOrder; /* 1 for DESC or 0 for ASC */ |
| 10862 | + unsigned done :1; /* A flag to indicate when processing is finished */ |
| 10863 | + unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */ |
| 10864 | + u16 iOrderByCol; /* For ORDER BY, column number in result set */ |
| 10865 | + u16 iAlias; /* Index into Parse.aAlias[] for zName */ |
| 10857 | 10866 | } *a; /* Alloc a power of two greater or equal to nExpr */ |
| 10858 | 10867 | }; |
| 10859 | 10868 | |
| 10860 | 10869 | /* |
| 10861 | 10870 | ** An instance of this structure is used by the parser to record both |
| | @@ -12141,10 +12150,11 @@ |
| 12141 | 12150 | SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *); |
| 12142 | 12151 | SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...); |
| 12143 | 12152 | SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*); |
| 12144 | 12153 | SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int); |
| 12145 | 12154 | SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*); |
| 12155 | +SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*); |
| 12146 | 12156 | SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*); |
| 12147 | 12157 | SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*); |
| 12148 | 12158 | SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*); |
| 12149 | 12159 | SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int); |
| 12150 | 12160 | SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *); |
| | @@ -23289,15 +23299,11 @@ |
| 23289 | 23299 | { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
| 23290 | 23300 | #endif |
| 23291 | 23301 | #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 23292 | 23302 | aSyscall[13].pCurrent) |
| 23293 | 23303 | |
| 23294 | | -#if SQLITE_ENABLE_LOCKING_STYLE |
| 23295 | 23304 | { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
| 23296 | | -#else |
| 23297 | | - { "fchmod", (sqlite3_syscall_ptr)0, 0 }, |
| 23298 | | -#endif |
| 23299 | 23305 | #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
| 23300 | 23306 | |
| 23301 | 23307 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
| 23302 | 23308 | { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
| 23303 | 23309 | #else |
| | @@ -23318,13 +23324,10 @@ |
| 23318 | 23324 | #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 23319 | 23325 | |
| 23320 | 23326 | { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 }, |
| 23321 | 23327 | #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) |
| 23322 | 23328 | |
| 23323 | | - { "umask", (sqlite3_syscall_ptr)umask, 0 }, |
| 23324 | | -#define osUmask ((mode_t(*)(mode_t))aSyscall[21].pCurrent) |
| 23325 | | - |
| 23326 | 23329 | }; /* End of the overrideable system calls */ |
| 23327 | 23330 | |
| 23328 | 23331 | /* |
| 23329 | 23332 | ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
| 23330 | 23333 | ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| | @@ -23425,31 +23428,29 @@ |
| 23425 | 23428 | ** process that is able to write to the database will also be able to |
| 23426 | 23429 | ** recover the hot journals. |
| 23427 | 23430 | */ |
| 23428 | 23431 | static int robust_open(const char *z, int f, mode_t m){ |
| 23429 | 23432 | int fd; |
| 23430 | | - mode_t m2; |
| 23431 | | - mode_t origM = 0; |
| 23432 | | - if( m==0 ){ |
| 23433 | | - m2 = SQLITE_DEFAULT_FILE_PERMISSIONS; |
| 23434 | | - }else{ |
| 23435 | | - m2 = m; |
| 23436 | | - origM = osUmask(0); |
| 23437 | | - } |
| 23433 | + mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS; |
| 23438 | 23434 | do{ |
| 23439 | 23435 | #if defined(O_CLOEXEC) |
| 23440 | 23436 | fd = osOpen(z,f|O_CLOEXEC,m2); |
| 23441 | 23437 | #else |
| 23442 | 23438 | fd = osOpen(z,f,m2); |
| 23443 | 23439 | #endif |
| 23444 | 23440 | }while( fd<0 && errno==EINTR ); |
| 23445 | | - if( m ){ |
| 23446 | | - osUmask(origM); |
| 23447 | | - } |
| 23441 | + if( fd>=0 ){ |
| 23442 | + if( m!=0 ){ |
| 23443 | + struct stat statbuf; |
| 23444 | + if( osFstat(fd, &statbuf)==0 && (statbuf.st_mode&0777)!=m ){ |
| 23445 | + osFchmod(fd, m); |
| 23446 | + } |
| 23447 | + } |
| 23448 | 23448 | #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) |
| 23449 | | - if( fd>=0 ) osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 23449 | + osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 23450 | 23450 | #endif |
| 23451 | + } |
| 23451 | 23452 | return fd; |
| 23452 | 23453 | } |
| 23453 | 23454 | |
| 23454 | 23455 | /* |
| 23455 | 23456 | ** Helper functions to obtain and relinquish the global mutex. The |
| | @@ -29871,11 +29872,11 @@ |
| 29871 | 29872 | }; |
| 29872 | 29873 | unsigned int i; /* Loop counter */ |
| 29873 | 29874 | |
| 29874 | 29875 | /* Double-check that the aSyscall[] array has been constructed |
| 29875 | 29876 | ** correctly. See ticket [bb3a86e890c8e96ab] */ |
| 29876 | | - assert( ArraySize(aSyscall)==22 ); |
| 29877 | + assert( ArraySize(aSyscall)==21 ); |
| 29877 | 29878 | |
| 29878 | 29879 | /* Register all VFSes defined in the aVfs[] array */ |
| 29879 | 29880 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
| 29880 | 29881 | sqlite3_vfs_register(&aVfs[i], i==0); |
| 29881 | 29882 | } |
| | @@ -72655,10 +72656,39 @@ |
| 72655 | 72656 | } |
| 72656 | 72657 | } |
| 72657 | 72658 | return 0; |
| 72658 | 72659 | } |
| 72659 | 72660 | |
| 72661 | +/* |
| 72662 | +** Subqueries stores the original database, table and column names for their |
| 72663 | +** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN". |
| 72664 | +** Check to see if the zSpan given to this routine matches the zDb, zTab, |
| 72665 | +** and zCol. If any of zDb, zTab, and zCol are NULL then those fields will |
| 72666 | +** match anything. |
| 72667 | +*/ |
| 72668 | +SQLITE_PRIVATE int sqlite3MatchSpanName( |
| 72669 | + const char *zSpan, |
| 72670 | + const char *zCol, |
| 72671 | + const char *zTab, |
| 72672 | + const char *zDb |
| 72673 | +){ |
| 72674 | + int n; |
| 72675 | + for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} |
| 72676 | + if( zDb && sqlite3StrNICmp(zSpan, zDb, n)!=0 ){ |
| 72677 | + return 0; |
| 72678 | + } |
| 72679 | + zSpan += n+1; |
| 72680 | + for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){} |
| 72681 | + if( zTab && sqlite3StrNICmp(zSpan, zTab, n)!=0 ){ |
| 72682 | + return 0; |
| 72683 | + } |
| 72684 | + zSpan += n+1; |
| 72685 | + if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){ |
| 72686 | + return 0; |
| 72687 | + } |
| 72688 | + return 1; |
| 72689 | +} |
| 72660 | 72690 | |
| 72661 | 72691 | /* |
| 72662 | 72692 | ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up |
| 72663 | 72693 | ** that name in the set of source tables in pSrcList and make the pExpr |
| 72664 | 72694 | ** expression node refer back to that source column. The following changes |
| | @@ -72710,44 +72740,63 @@ |
| 72710 | 72740 | |
| 72711 | 72741 | /* Initialize the node to no-match */ |
| 72712 | 72742 | pExpr->iTable = -1; |
| 72713 | 72743 | pExpr->pTab = 0; |
| 72714 | 72744 | ExprSetIrreducible(pExpr); |
| 72745 | + |
| 72746 | + /* Translate the schema name in zDb into a pointer to the corresponding |
| 72747 | + ** schema. If not found, pSchema will remain NULL and nothing will match |
| 72748 | + ** resulting in an appropriate error message toward the end of this routine |
| 72749 | + */ |
| 72750 | + if( zDb ){ |
| 72751 | + for(i=0; i<db->nDb; i++){ |
| 72752 | + assert( db->aDb[i].zName ); |
| 72753 | + if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){ |
| 72754 | + pSchema = db->aDb[i].pSchema; |
| 72755 | + break; |
| 72756 | + } |
| 72757 | + } |
| 72758 | + } |
| 72715 | 72759 | |
| 72716 | 72760 | /* Start at the inner-most context and move outward until a match is found */ |
| 72717 | 72761 | while( pNC && cnt==0 ){ |
| 72718 | 72762 | ExprList *pEList; |
| 72719 | 72763 | SrcList *pSrcList = pNC->pSrcList; |
| 72720 | 72764 | |
| 72721 | 72765 | if( pSrcList ){ |
| 72722 | 72766 | for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){ |
| 72723 | 72767 | Table *pTab; |
| 72724 | | - int iDb; |
| 72725 | 72768 | Column *pCol; |
| 72726 | 72769 | |
| 72727 | 72770 | pTab = pItem->pTab; |
| 72728 | 72771 | assert( pTab!=0 && pTab->zName!=0 ); |
| 72729 | | - iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 72730 | 72772 | assert( pTab->nCol>0 ); |
| 72773 | + if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){ |
| 72774 | + ExprList *pEList = pItem->pSelect->pEList; |
| 72775 | + int hit = 0; |
| 72776 | + for(j=0; j<pEList->nExpr; j++){ |
| 72777 | + if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){ |
| 72778 | + cnt++; |
| 72779 | + cntTab = 2; |
| 72780 | + pMatch = pItem; |
| 72781 | + pExpr->iColumn = j; |
| 72782 | + hit = 1; |
| 72783 | + } |
| 72784 | + } |
| 72785 | + if( hit || zTab==0 ) continue; |
| 72786 | + } |
| 72787 | + if( zDb && pTab->pSchema!=pSchema ){ |
| 72788 | + continue; |
| 72789 | + } |
| 72731 | 72790 | if( zTab ){ |
| 72732 | | - if( pItem->zAlias ){ |
| 72733 | | - char *zTabName = pItem->zAlias; |
| 72734 | | - if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue; |
| 72735 | | - }else{ |
| 72736 | | - char *zTabName = pTab->zName; |
| 72737 | | - if( NEVER(zTabName==0) || sqlite3StrICmp(zTabName, zTab)!=0 ){ |
| 72738 | | - continue; |
| 72739 | | - } |
| 72740 | | - if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){ |
| 72741 | | - continue; |
| 72742 | | - } |
| 72791 | + const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName; |
| 72792 | + assert( zTabName!=0 ); |
| 72793 | + if( sqlite3StrICmp(zTabName, zTab)!=0 ){ |
| 72794 | + continue; |
| 72743 | 72795 | } |
| 72744 | 72796 | } |
| 72745 | 72797 | if( 0==(cntTab++) ){ |
| 72746 | | - pExpr->iTable = pItem->iCursor; |
| 72747 | | - pExpr->pTab = pTab; |
| 72748 | | - pSchema = pTab->pSchema; |
| 72749 | 72798 | pMatch = pItem; |
| 72750 | 72799 | } |
| 72751 | 72800 | for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){ |
| 72752 | 72801 | if( sqlite3StrICmp(pCol->zName, zCol)==0 ){ |
| 72753 | 72802 | /* If there has been exactly one prior match and this match |
| | @@ -72757,21 +72806,23 @@ |
| 72757 | 72806 | if( cnt==1 ){ |
| 72758 | 72807 | if( pItem->jointype & JT_NATURAL ) continue; |
| 72759 | 72808 | if( nameInUsingClause(pItem->pUsing, zCol) ) continue; |
| 72760 | 72809 | } |
| 72761 | 72810 | cnt++; |
| 72762 | | - pExpr->iTable = pItem->iCursor; |
| 72763 | | - pExpr->pTab = pTab; |
| 72764 | 72811 | pMatch = pItem; |
| 72765 | | - pSchema = pTab->pSchema; |
| 72766 | 72812 | /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */ |
| 72767 | 72813 | pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j; |
| 72768 | 72814 | break; |
| 72769 | 72815 | } |
| 72770 | 72816 | } |
| 72771 | 72817 | } |
| 72772 | | - } |
| 72818 | + if( pMatch ){ |
| 72819 | + pExpr->iTable = pMatch->iCursor; |
| 72820 | + pExpr->pTab = pMatch->pTab; |
| 72821 | + pSchema = pExpr->pTab->pSchema; |
| 72822 | + } |
| 72823 | + } /* if( pSrcList ) */ |
| 72773 | 72824 | |
| 72774 | 72825 | #ifndef SQLITE_OMIT_TRIGGER |
| 72775 | 72826 | /* If we have not already resolved the name, then maybe |
| 72776 | 72827 | ** it is a new.* or old.* trigger argument reference |
| 72777 | 72828 | */ |
| | @@ -73066,42 +73117,43 @@ |
| 73066 | 73117 | int wrong_num_args = 0; /* True if wrong number of arguments */ |
| 73067 | 73118 | int is_agg = 0; /* True if is an aggregate function */ |
| 73068 | 73119 | int auth; /* Authorization to use the function */ |
| 73069 | 73120 | int nId; /* Number of characters in function name */ |
| 73070 | 73121 | const char *zId; /* The function name. */ |
| 73071 | | - FuncDef *pDef; /* Information about the function */ |
| 73072 | 73122 | u8 enc = ENC(pParse->db); /* The database encoding */ |
| 73073 | 73123 | |
| 73074 | 73124 | testcase( pExpr->op==TK_CONST_FUNC ); |
| 73075 | 73125 | assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); |
| 73076 | 73126 | zId = pExpr->u.zToken; |
| 73077 | 73127 | nId = sqlite3Strlen30(zId); |
| 73078 | | - pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); |
| 73079 | | - if( pDef==0 ){ |
| 73080 | | - pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0); |
| 73081 | | - if( pDef==0 ){ |
| 73082 | | - no_such_func = 1; |
| 73083 | | - }else{ |
| 73084 | | - wrong_num_args = 1; |
| 73085 | | - } |
| 73086 | | - }else{ |
| 73087 | | - is_agg = pDef->xFunc==0; |
| 73088 | | - } |
| 73089 | | -#ifndef SQLITE_OMIT_AUTHORIZATION |
| 73090 | | - if( pDef ){ |
| 73091 | | - auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); |
| 73092 | | - if( auth!=SQLITE_OK ){ |
| 73093 | | - if( auth==SQLITE_DENY ){ |
| 73094 | | - sqlite3ErrorMsg(pParse, "not authorized to use function: %s", |
| 73095 | | - pDef->zName); |
| 73096 | | - pNC->nErr++; |
| 73097 | | - } |
| 73098 | | - pExpr->op = TK_NULL; |
| 73099 | | - return WRC_Prune; |
| 73100 | | - } |
| 73101 | | - } |
| 73102 | | -#endif |
| 73128 | + if( pParse->db->init.busy==0 ){ |
| 73129 | + FuncDef *pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0); |
| 73130 | + if( pDef==0 ){ |
| 73131 | + pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0); |
| 73132 | + if( pDef==0 ){ |
| 73133 | + no_such_func = 1; |
| 73134 | + }else{ |
| 73135 | + wrong_num_args = 1; |
| 73136 | + } |
| 73137 | + }else{ |
| 73138 | + is_agg = pDef->xFunc==0; |
| 73139 | + } |
| 73140 | +#ifndef SQLITE_OMIT_AUTHORIZATION |
| 73141 | + if( pDef ){ |
| 73142 | + auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0); |
| 73143 | + if( auth!=SQLITE_OK ){ |
| 73144 | + if( auth==SQLITE_DENY ){ |
| 73145 | + sqlite3ErrorMsg(pParse, "not authorized to use function: %s", |
| 73146 | + pDef->zName); |
| 73147 | + pNC->nErr++; |
| 73148 | + } |
| 73149 | + pExpr->op = TK_NULL; |
| 73150 | + return WRC_Prune; |
| 73151 | + } |
| 73152 | + } |
| 73153 | +#endif |
| 73154 | + } |
| 73103 | 73155 | if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){ |
| 73104 | 73156 | sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId); |
| 73105 | 73157 | pNC->nErr++; |
| 73106 | 73158 | is_agg = 0; |
| 73107 | 73159 | }else if( no_such_func ){ |
| | @@ -77065,10 +77117,16 @@ |
| 77065 | 77117 | for(i=0; i<pList->nExpr; i++){ |
| 77066 | 77118 | sqlite3ExplainPrintf(pOut, "item[%d] = ", i); |
| 77067 | 77119 | sqlite3ExplainPush(pOut); |
| 77068 | 77120 | sqlite3ExplainExpr(pOut, pList->a[i].pExpr); |
| 77069 | 77121 | sqlite3ExplainPop(pOut); |
| 77122 | + if( pList->a[i].zName ){ |
| 77123 | + sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName); |
| 77124 | + } |
| 77125 | + if( pList->a[i].bSpanIsTab ){ |
| 77126 | + sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan); |
| 77127 | + } |
| 77070 | 77128 | if( i<pList->nExpr-1 ){ |
| 77071 | 77129 | sqlite3ExplainNL(pOut); |
| 77072 | 77130 | } |
| 77073 | 77131 | } |
| 77074 | 77132 | sqlite3ExplainPop(pOut); |
| | @@ -92739,13 +92797,15 @@ |
| 92739 | 92797 | if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){ |
| 92740 | 92798 | Table *pTab; |
| 92741 | 92799 | if( sqlite3ReadSchema(pParse) ) goto pragma_out; |
| 92742 | 92800 | pTab = sqlite3FindTable(db, zRight, zDb); |
| 92743 | 92801 | if( pTab ){ |
| 92744 | | - int i; |
| 92802 | + int i, k; |
| 92745 | 92803 | int nHidden = 0; |
| 92746 | 92804 | Column *pCol; |
| 92805 | + Index *pPk; |
| 92806 | + for(pPk=pTab->pIndex; pPk && pPk->autoIndex!=2; pPk=pPk->pNext){} |
| 92747 | 92807 | sqlite3VdbeSetNumCols(v, 6); |
| 92748 | 92808 | pParse->nMem = 6; |
| 92749 | 92809 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC); |
| 92750 | 92810 | sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); |
| 92751 | 92811 | sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC); |
| | @@ -92766,12 +92826,18 @@ |
| 92766 | 92826 | if( pCol->zDflt ){ |
| 92767 | 92827 | sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0); |
| 92768 | 92828 | }else{ |
| 92769 | 92829 | sqlite3VdbeAddOp2(v, OP_Null, 0, 5); |
| 92770 | 92830 | } |
| 92771 | | - sqlite3VdbeAddOp2(v, OP_Integer, |
| 92772 | | - (pCol->colFlags&COLFLAG_PRIMKEY)!=0, 6); |
| 92831 | + if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){ |
| 92832 | + k = 0; |
| 92833 | + }else if( pPk==0 ){ |
| 92834 | + k = 1; |
| 92835 | + }else{ |
| 92836 | + for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){} |
| 92837 | + } |
| 92838 | + sqlite3VdbeAddOp2(v, OP_Integer, k, 6); |
| 92773 | 92839 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6); |
| 92774 | 92840 | } |
| 92775 | 92841 | } |
| 92776 | 92842 | }else |
| 92777 | 92843 | |
| | @@ -95756,12 +95822,10 @@ |
| 95756 | 95822 | |
| 95757 | 95823 | for(i=0, pCol=aCol; i<nCol; i++, pCol++){ |
| 95758 | 95824 | /* Get an appropriate name for the column |
| 95759 | 95825 | */ |
| 95760 | 95826 | p = sqlite3ExprSkipCollate(pEList->a[i].pExpr); |
| 95761 | | - assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue) |
| 95762 | | - || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 ); |
| 95763 | 95827 | if( (zName = pEList->a[i].zName)!=0 ){ |
| 95764 | 95828 | /* If the column contains an "AS <name>" phrase, use <name> as the name */ |
| 95765 | 95829 | zName = sqlite3DbStrDup(db, zName); |
| 95766 | 95830 | }else{ |
| 95767 | 95831 | Expr *pColExpr = p; /* The expression that is the result column name */ |
| | @@ -95795,10 +95859,13 @@ |
| 95795 | 95859 | */ |
| 95796 | 95860 | nName = sqlite3Strlen30(zName); |
| 95797 | 95861 | for(j=cnt=0; j<i; j++){ |
| 95798 | 95862 | if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){ |
| 95799 | 95863 | char *zNewName; |
| 95864 | + int k; |
| 95865 | + for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){} |
| 95866 | + if( zName[k]==':' ) nName = k; |
| 95800 | 95867 | zName[nName] = 0; |
| 95801 | 95868 | zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt); |
| 95802 | 95869 | sqlite3DbFree(db, zName); |
| 95803 | 95870 | zName = zNewName; |
| 95804 | 95871 | j = -1; |
| | @@ -97711,10 +97778,11 @@ |
| 97711 | 97778 | int i, j, k; |
| 97712 | 97779 | SrcList *pTabList; |
| 97713 | 97780 | ExprList *pEList; |
| 97714 | 97781 | struct SrcList_item *pFrom; |
| 97715 | 97782 | sqlite3 *db = pParse->db; |
| 97783 | + Expr *pE, *pRight, *pExpr; |
| 97716 | 97784 | |
| 97717 | 97785 | if( db->mallocFailed ){ |
| 97718 | 97786 | return WRC_Abort; |
| 97719 | 97787 | } |
| 97720 | 97788 | if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){ |
| | @@ -97796,11 +97864,11 @@ |
| 97796 | 97864 | ** |
| 97797 | 97865 | ** The first loop just checks to see if there are any "*" operators |
| 97798 | 97866 | ** that need expanding. |
| 97799 | 97867 | */ |
| 97800 | 97868 | for(k=0; k<pEList->nExpr; k++){ |
| 97801 | | - Expr *pE = pEList->a[k].pExpr; |
| 97869 | + pE = pEList->a[k].pExpr; |
| 97802 | 97870 | if( pE->op==TK_ALL ) break; |
| 97803 | 97871 | assert( pE->op!=TK_DOT || pE->pRight!=0 ); |
| 97804 | 97872 | assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) ); |
| 97805 | 97873 | if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break; |
| 97806 | 97874 | } |
| | @@ -97814,14 +97882,22 @@ |
| 97814 | 97882 | ExprList *pNew = 0; |
| 97815 | 97883 | int flags = pParse->db->flags; |
| 97816 | 97884 | int longNames = (flags & SQLITE_FullColNames)!=0 |
| 97817 | 97885 | && (flags & SQLITE_ShortColNames)==0; |
| 97818 | 97886 | |
| 97887 | + /* When processing FROM-clause subqueries, it is always the case |
| 97888 | + ** that full_column_names=OFF and short_column_names=ON. The |
| 97889 | + ** sqlite3ResultSetOfSelect() routine makes it so. */ |
| 97890 | + assert( (p->selFlags & SF_NestedFrom)==0 |
| 97891 | + || ((flags & SQLITE_FullColNames)==0 && |
| 97892 | + (flags & SQLITE_ShortColNames)!=0) ); |
| 97893 | + |
| 97819 | 97894 | for(k=0; k<pEList->nExpr; k++){ |
| 97820 | | - Expr *pE = a[k].pExpr; |
| 97821 | | - assert( pE->op!=TK_DOT || pE->pRight!=0 ); |
| 97822 | | - if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){ |
| 97895 | + pE = a[k].pExpr; |
| 97896 | + pRight = pE->pRight; |
| 97897 | + assert( pE->op!=TK_DOT || pRight!=0 ); |
| 97898 | + if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){ |
| 97823 | 97899 | /* This particular expression does not need to be expanded. |
| 97824 | 97900 | */ |
| 97825 | 97901 | pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr); |
| 97826 | 97902 | if( pNew ){ |
| 97827 | 97903 | pNew->a[pNew->nExpr-1].zName = a[k].zName; |
| | @@ -97832,44 +97908,56 @@ |
| 97832 | 97908 | a[k].pExpr = 0; |
| 97833 | 97909 | }else{ |
| 97834 | 97910 | /* This expression is a "*" or a "TABLE.*" and needs to be |
| 97835 | 97911 | ** expanded. */ |
| 97836 | 97912 | int tableSeen = 0; /* Set to 1 when TABLE matches */ |
| 97837 | | - char *zTName; /* text of name of TABLE */ |
| 97913 | + char *zTName = 0; /* text of name of TABLE */ |
| 97838 | 97914 | if( pE->op==TK_DOT ){ |
| 97839 | 97915 | assert( pE->pLeft!=0 ); |
| 97840 | 97916 | assert( !ExprHasProperty(pE->pLeft, EP_IntValue) ); |
| 97841 | 97917 | zTName = pE->pLeft->u.zToken; |
| 97842 | | - }else{ |
| 97843 | | - zTName = 0; |
| 97844 | 97918 | } |
| 97845 | 97919 | for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){ |
| 97846 | 97920 | Table *pTab = pFrom->pTab; |
| 97921 | + Select *pSub = pFrom->pSelect; |
| 97847 | 97922 | char *zTabName = pFrom->zAlias; |
| 97923 | + const char *zSchemaName = 0; |
| 97924 | + int iDb; |
| 97848 | 97925 | if( zTabName==0 ){ |
| 97849 | 97926 | zTabName = pTab->zName; |
| 97850 | 97927 | } |
| 97851 | 97928 | if( db->mallocFailed ) break; |
| 97852 | | - if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){ |
| 97853 | | - continue; |
| 97929 | + if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){ |
| 97930 | + pSub = 0; |
| 97931 | + if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){ |
| 97932 | + continue; |
| 97933 | + } |
| 97934 | + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); |
| 97935 | + zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*"; |
| 97854 | 97936 | } |
| 97855 | | - tableSeen = 1; |
| 97856 | 97937 | for(j=0; j<pTab->nCol; j++){ |
| 97857 | | - Expr *pExpr, *pRight; |
| 97858 | 97938 | char *zName = pTab->aCol[j].zName; |
| 97859 | 97939 | char *zColname; /* The computed column name */ |
| 97860 | 97940 | char *zToFree; /* Malloced string that needs to be freed */ |
| 97861 | 97941 | Token sColname; /* Computed column name as a token */ |
| 97942 | + |
| 97943 | + assert( zName ); |
| 97944 | + if( zTName && pSub |
| 97945 | + && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0 |
| 97946 | + ){ |
| 97947 | + continue; |
| 97948 | + } |
| 97862 | 97949 | |
| 97863 | 97950 | /* If a column is marked as 'hidden' (currently only possible |
| 97864 | 97951 | ** for virtual tables), do not include it in the expanded |
| 97865 | 97952 | ** result-set list. |
| 97866 | 97953 | */ |
| 97867 | 97954 | if( IsHiddenColumn(&pTab->aCol[j]) ){ |
| 97868 | 97955 | assert(IsVirtual(pTab)); |
| 97869 | 97956 | continue; |
| 97870 | 97957 | } |
| 97958 | + tableSeen = 1; |
| 97871 | 97959 | |
| 97872 | 97960 | if( i>0 && zTName==0 ){ |
| 97873 | 97961 | if( (pFrom->jointype & JT_NATURAL)!=0 |
| 97874 | 97962 | && tableAndColumnIndex(pTabList, i, zName, 0, 0) |
| 97875 | 97963 | ){ |
| | @@ -97888,10 +97976,14 @@ |
| 97888 | 97976 | zToFree = 0; |
| 97889 | 97977 | if( longNames || pTabList->nSrc>1 ){ |
| 97890 | 97978 | Expr *pLeft; |
| 97891 | 97979 | pLeft = sqlite3Expr(db, TK_ID, zTabName); |
| 97892 | 97980 | pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); |
| 97981 | + if( zSchemaName ){ |
| 97982 | + pLeft = sqlite3Expr(db, TK_ID, zSchemaName); |
| 97983 | + pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0); |
| 97984 | + } |
| 97893 | 97985 | if( longNames ){ |
| 97894 | 97986 | zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName); |
| 97895 | 97987 | zToFree = zColname; |
| 97896 | 97988 | } |
| 97897 | 97989 | }else{ |
| | @@ -97899,10 +97991,22 @@ |
| 97899 | 97991 | } |
| 97900 | 97992 | pNew = sqlite3ExprListAppend(pParse, pNew, pExpr); |
| 97901 | 97993 | sColname.z = zColname; |
| 97902 | 97994 | sColname.n = sqlite3Strlen30(zColname); |
| 97903 | 97995 | sqlite3ExprListSetName(pParse, pNew, &sColname, 0); |
| 97996 | + if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){ |
| 97997 | + struct ExprList_item *pX = &pNew->a[pNew->nExpr-1]; |
| 97998 | + if( pSub ){ |
| 97999 | + pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan); |
| 98000 | + testcase( pX->zSpan==0 ); |
| 98001 | + }else{ |
| 98002 | + pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s", |
| 98003 | + zSchemaName, zTabName, zColname); |
| 98004 | + testcase( pX->zSpan==0 ); |
| 98005 | + } |
| 98006 | + pX->bSpanIsTab = 1; |
| 98007 | + } |
| 97904 | 98008 | sqlite3DbFree(db, zToFree); |
| 97905 | 98009 | } |
| 97906 | 98010 | } |
| 97907 | 98011 | if( !tableSeen ){ |
| 97908 | 98012 | if( zTName ){ |
| 97909 | 98013 | |