SQLite
Check-in [972443b4eb]
Not logged in
Overview
SHA1 Hash:972443b4eb282d45507da06c75e2cd46dd72326b
Date: 2012-12-07 18:38:16
User: drh
Comment:Some errors in veryquick resolved. Many more to go.
Tags And Properties
Changes
hide diffs unified diffs patch

Changes to src/expr.c

55 return pExpr->affinity; 55 return pExpr->affinity; 56 } 56 } 57 57 58 /* 58 /* 59 ** Set the collating sequence for expression pExpr to be the collating 59 ** Set the collating sequence for expression pExpr to be the collating 60 ** sequence named by pToken. Return a pointer to a new Expr node that 60 ** sequence named by pToken. Return a pointer to a new Expr node that 61 ** implements the COLLATE operator. 61 ** implements the COLLATE operator. > 62 ** > 63 ** If a memory allocation error occurs, that fact is recorded in pParse->db > 64 ** and the pExpr parameter is returned unchanged. 62 */ 65 */ 63 Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr *pExpr, Token *pCollName){ | 66 Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr *pExpr, Token *pCollName){ > 67 if( pCollName->n>0 ){ 64 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1); | 68 Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1); 65 if( pNew ){ | 69 if( pNew ){ 66 pNew->pLeft = pExpr; | 70 pNew->pLeft = pExpr; 67 pNew->flags |= EP_Collate; | 71 pNew->flags |= EP_Collate; > 72 pExpr = pNew; 68 } | 73 } > 74 } 69 return pNew; | 75 return pExpr; > 76 } > 77 Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ > 78 if( zC ){ > 79 Token s; > 80 s.z = zC; > 81 s.n = sqlite3Strlen30(s.z); > 82 pExpr = sqlite3ExprAddCollateToken(pParse, pExpr, &s); > 83 } > 84 return pExpr; > 85 } > 86 > 87 /* > 88 ** Skip over any TK_COLLATE operator in an expression. > 89 */ > 90 Expr *sqlite3ExprSkipCollate(Expr *pExpr){ > 91 if( pExpr && pExpr->op==TK_COLLATE ) pExpr = pExpr->pLeft; > 92 return pExpr; 70 } 93 } 71 94 72 /* 95 /* 73 ** Return the collation sequence for the expression pExpr. If 96 ** Return the collation sequence for the expression pExpr. If 74 ** there is no defined collating sequence, return NULL. 97 ** there is no defined collating sequence, return NULL. 75 ** 98 ** 76 ** The collating sequence might be determined by a COLLATE operator 99 ** The collating sequence might be determined by a COLLATE operator

Changes to src/fkey.c

507 507 508 pLeft = sqlite3Expr(db, TK_REGISTER, 0); 508 pLeft = sqlite3Expr(db, TK_REGISTER, 0); 509 if( pLeft ){ 509 if( pLeft ){ 510 /* Set the collation sequence and affinity of the LHS of each TK_EQ 510 /* Set the collation sequence and affinity of the LHS of each TK_EQ 511 ** expression to the parent key column defaults. */ 511 ** expression to the parent key column defaults. */ 512 if( pIdx ){ 512 if( pIdx ){ 513 Column *pCol; 513 Column *pCol; 514 Expr *pNew; < 515 Token s; < 516 iCol = pIdx->aiColumn[i]; 514 iCol = pIdx->aiColumn[i]; 517 pCol = &pTab->aCol[iCol]; 515 pCol = &pTab->aCol[iCol]; 518 if( pTab->iPKey==iCol ) iCol = -1; 516 if( pTab->iPKey==iCol ) iCol = -1; 519 pLeft->iTable = regData+iCol+1; 517 pLeft->iTable = regData+iCol+1; 520 pLeft->affinity = pCol->affinity; 518 pLeft->affinity = pCol->affinity; 521 s.z = pCol->zColl; | 519 pLeft = sqlite3ExprAddCollateString(pParse, pLeft, pCol->zColl); 522 s.n = sqlite3Strlen30(s.z); < 523 pNew = sqlite3ExprSetCollByToken(pParse, pLeft, &s); < 524 if( pNew ) pLeft = pNew; < 525 }else{ 520 }else{ 526 pLeft->iTable = regData; 521 pLeft->iTable = regData; 527 pLeft->affinity = SQLITE_AFF_INTEGER; 522 pLeft->affinity = SQLITE_AFF_INTEGER; 528 } 523 } 529 } 524 } 530 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; 525 iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; 531 assert( iCol>=0 ); 526 assert( iCol>=0 );

Changes to src/parse.y

811 } 811 } 812 expr(A) ::= VARIABLE(X). { 812 expr(A) ::= VARIABLE(X). { 813 spanExpr(&A, pParse, TK_VARIABLE, &X); 813 spanExpr(&A, pParse, TK_VARIABLE, &X); 814 sqlite3ExprAssignVarNumber(pParse, A.pExpr); 814 sqlite3ExprAssignVarNumber(pParse, A.pExpr); 815 spanSet(&A, &X, &X); 815 spanSet(&A, &X, &X); 816 } 816 } 817 expr(A) ::= expr(E) COLLATE ids(C). { 817 expr(A) ::= expr(E) COLLATE ids(C). { 818 A.pExpr = sqlite3ExprSetCollByToken(pParse, E.pExpr, &C); | 818 A.pExpr = sqlite3ExprAddCollateToken(pParse, E.pExpr, &C); 819 A.zStart = E.zStart; 819 A.zStart = E.zStart; 820 A.zEnd = &C.z[C.n]; 820 A.zEnd = &C.z[C.n]; 821 } 821 } 822 %ifndef SQLITE_OMIT_CAST 822 %ifndef SQLITE_OMIT_CAST 823 expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). { 823 expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). { 824 A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T); 824 A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T); 825 spanSet(&A,&X,&Y); 825 spanSet(&A,&X,&Y); ................................................................................................................................................................................ 1136 %destructor idxlist {sqlite3ExprListDelete(pParse->db, $$);} 1136 %destructor idxlist {sqlite3ExprListDelete(pParse->db, $$);} 1137 %type idxlist_opt {ExprList*} 1137 %type idxlist_opt {ExprList*} 1138 %destructor idxlist_opt {sqlite3ExprListDelete(pParse->db, $$);} 1138 %destructor idxlist_opt {sqlite3ExprListDelete(pParse->db, $$);} 1139 1139 1140 idxlist_opt(A) ::= . {A = 0;} 1140 idxlist_opt(A) ::= . {A = 0;} 1141 idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;} 1141 idxlist_opt(A) ::= LP idxlist(X) RP. {A = X;} 1142 idxlist(A) ::= idxlist(X) COMMA nm(Y) collate(C) sortorder(Z). { 1142 idxlist(A) ::= idxlist(X) COMMA nm(Y) collate(C) sortorder(Z). { 1143 Expr *p = 0; | 1143 Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C); 1144 if( C.n>0 ){ < 1145 p = sqlite3Expr(pParse->db, TK_COLUMN, 0); < 1146 sqlite3ExprSetCollByToken(pParse, p, &C); < 1147 } < 1148 A = sqlite3ExprListAppend(pParse,X, p); 1144 A = sqlite3ExprListAppend(pParse,X, p); 1149 sqlite3ExprListSetName(pParse,A,&Y,1); 1145 sqlite3ExprListSetName(pParse,A,&Y,1); 1150 sqlite3ExprListCheckLength(pParse, A, "index"); 1146 sqlite3ExprListCheckLength(pParse, A, "index"); 1151 if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z; 1147 if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z; 1152 } 1148 } 1153 idxlist(A) ::= nm(Y) collate(C) sortorder(Z). { 1149 idxlist(A) ::= nm(Y) collate(C) sortorder(Z). { 1154 Expr *p = 0; | 1150 Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &C); 1155 if( C.n>0 ){ < 1156 p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0); < 1157 sqlite3ExprSetCollByToken(pParse, p, &C); < 1158 } < 1159 A = sqlite3ExprListAppend(pParse,0, p); 1151 A = sqlite3ExprListAppend(pParse,0, p); 1160 sqlite3ExprListSetName(pParse, A, &Y, 1); 1152 sqlite3ExprListSetName(pParse, A, &Y, 1); 1161 sqlite3ExprListCheckLength(pParse, A, "index"); 1153 sqlite3ExprListCheckLength(pParse, A, "index"); 1162 if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z; 1154 if( A ) A->a[A->nExpr-1].sortOrder = (u8)Z; 1163 } 1155 } 1164 1156 1165 %type collate {Token} 1157 %type collate {Token}

Changes to src/resolve.c

63 ** Is equivalent to: 63 ** Is equivalent to: 64 ** 64 ** 65 ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5 65 ** SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5 66 ** 66 ** 67 ** The result of random()%5 in the GROUP BY clause is probably different 67 ** The result of random()%5 in the GROUP BY clause is probably different 68 ** from the result in the result-set. We might fix this someday. Or 68 ** from the result in the result-set. We might fix this someday. Or 69 ** then again, we might not... 69 ** then again, we might not... > 70 ** > 71 ** If the reference is followed by a COLLATE operator, then make sure > 72 ** the COLLATE operator is preserved. For example: > 73 ** > 74 ** SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase; > 75 ** > 76 ** Should be transformed into: > 77 ** > 78 ** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase; 70 ** 79 ** 71 ** The nSubquery parameter specifies how many levels of subquery the 80 ** The nSubquery parameter specifies how many levels of subquery the 72 ** alias is removed from the original expression. The usually value is 81 ** alias is removed from the original expression. The usually value is 73 ** zero but it might be more if the alias is contained within a subquery 82 ** zero but it might be more if the alias is contained within a subquery 74 ** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION 83 ** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION 75 ** structures must be increased by the nSubquery amount. 84 ** structures must be increased by the nSubquery amount. 76 */ 85 */ ................................................................................................................................................................................ 87 sqlite3 *db; /* The database connection */ 96 sqlite3 *db; /* The database connection */ 88 97 89 assert( iCol>=0 && iCol<pEList->nExpr ); 98 assert( iCol>=0 && iCol<pEList->nExpr ); 90 pOrig = pEList->a[iCol].pExpr; 99 pOrig = pEList->a[iCol].pExpr; 91 assert( pOrig!=0 ); 100 assert( pOrig!=0 ); 92 assert( pOrig->flags & EP_Resolved ); 101 assert( pOrig->flags & EP_Resolved ); 93 db = pParse->db; 102 db = pParse->db; 94 if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){ < 95 pDup = sqlite3ExprDup(db, pOrig, 0); | 103 pDup = sqlite3ExprDup(db, pOrig, 0); > 104 if( pDup==0 ) return; > 105 if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){ 96 incrAggFunctionDepth(pDup, nSubquery); 106 incrAggFunctionDepth(pDup, nSubquery); 97 pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0); 107 pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0); 98 if( pDup==0 ) return; 108 if( pDup==0 ) return; 99 if( pEList->a[iCol].iAlias==0 ){ 109 if( pEList->a[iCol].iAlias==0 ){ 100 pEList->a[iCol].iAlias = (u16)(++pParse->nAlias); 110 pEList->a[iCol].iAlias = (u16)(++pParse->nAlias); 101 } 111 } 102 pDup->iTable = pEList->a[iCol].iAlias; 112 pDup->iTable = pEList->a[iCol].iAlias; 103 }else if( ExprHasProperty(pOrig, EP_IntValue) || pOrig->u.zToken==0 ){ | 113 } 104 pDup = sqlite3ExprDup(db, pOrig, 0); | 114 #if 1 /* FIXME */ 105 if( pDup==0 ) return; | 115 if( pExpr->flags & EP_Collate ){ 106 }else{ | 116 CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr); 107 char *zToken = pOrig->u.zToken; | 117 if( pColl ){ 108 assert( zToken!=0 ); | 118 pDup = sqlite3ExprAddCollateString(pParse, pDup, pColl->zName); 109 pOrig->u.zToken = 0; | 119 } 110 pDup = sqlite3ExprDup(db, pOrig, 0); | 120 pDup->flags |= EP_Collate; 111 pOrig->u.zToken = zToken; | 121 } 112 if( pDup==0 ) return; | 122 #else 113 assert( (pDup->flags & (EP_Reduced|EP_TokenOnly))==0 ); | 123 /* Should be this: */ 114 pDup->flags2 |= EP2_MallocedToken; | 124 if( pExpr->op==TK_COLLATE ){ 115 pDup->u.zToken = sqlite3DbStrDup(db, zToken); | 125 pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken); 116 } | 126 } 117 pDup->flags |= EP_Collate & pExpr->flags; | 127 #endif 118 128 119 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 129 /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 120 ** prevents ExprDelete() from deleting the Expr structure itself, 130 ** prevents ExprDelete() from deleting the Expr structure itself, 121 ** allowing it to be repopulated by the memcpy() on the following line. 131 ** allowing it to be repopulated by the memcpy() on the following line. 122 */ 132 */ 123 ExprSetProperty(pExpr, EP_Static); 133 ExprSetProperty(pExpr, EP_Static); 124 sqlite3ExprDelete(db, pExpr); 134 sqlite3ExprDelete(db, pExpr); 125 memcpy(pExpr, pDup, sizeof(*pExpr)); 135 memcpy(pExpr, pDup, sizeof(*pExpr)); > 136 if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){ > 137 assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 ); > 138 pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken); > 139 pExpr->flags2 |= EP2_MallocedToken; > 140 } 126 sqlite3DbFree(db, pDup); 141 sqlite3DbFree(db, pDup); 127 } 142 } 128 143 129 144 130 /* 145 /* 131 ** Return TRUE if the name zCol occurs anywhere in the USING clause. 146 ** Return TRUE if the name zCol occurs anywhere in the USING clause. 132 ** 147 ** ................................................................................................................................................................................ 932 /* If an AS-name match is found, mark this ORDER BY column as being 947 /* If an AS-name match is found, mark this ORDER BY column as being 933 ** a copy of the iCol-th result-set column. The subsequent call to 948 ** a copy of the iCol-th result-set column. The subsequent call to 934 ** sqlite3ResolveOrderGroupBy() will convert the expression to a 949 ** sqlite3ResolveOrderGroupBy() will convert the expression to a 935 ** copy of the iCol-th result-set expression. */ 950 ** copy of the iCol-th result-set expression. */ 936 pItem->iOrderByCol = (u16)iCol; 951 pItem->iOrderByCol = (u16)iCol; 937 continue; 952 continue; 938 } 953 } 939 if( sqlite3ExprIsInteger(pE, &iCol) ){ | 954 if( sqlite3ExprIsInteger(sqlite3ExprSkipCollate(pE), &iCol) ){ 940 /* The ORDER BY term is an integer constant. Again, set the column 955 /* The ORDER BY term is an integer constant. Again, set the column 941 ** number so that sqlite3ResolveOrderGroupBy() will convert the 956 ** number so that sqlite3ResolveOrderGroupBy() will convert the 942 ** order-by term to a copy of the result-set expression */ 957 ** order-by term to a copy of the result-set expression */ 943 if( iCol<1 ){ | 958 if( (iCol & ~0xffff)!=0 ){ 944 resolveOutOfRangeError(pParse, zType, i+1, nResult); 959 resolveOutOfRangeError(pParse, zType, i+1, nResult); 945 return 1; 960 return 1; 946 } 961 } 947 pItem->iOrderByCol = (u16)iCol; 962 pItem->iOrderByCol = (u16)iCol; 948 continue; 963 continue; 949 } 964 } 950 965

Changes to src/sqliteInt.h

3017 u8 sqlite3HexToInt(int h); 3017 u8 sqlite3HexToInt(int h); 3018 int sqlite3TwoPartName(Parse *, Token *, Token *, Token **); 3018 int sqlite3TwoPartName(Parse *, Token *, Token *, Token **); 3019 const char *sqlite3ErrStr(int); 3019 const char *sqlite3ErrStr(int); 3020 int sqlite3ReadSchema(Parse *pParse); 3020 int sqlite3ReadSchema(Parse *pParse); 3021 CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int); 3021 CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int); 3022 CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName); 3022 CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName); 3023 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr); 3023 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr); 3024 Expr *sqlite3ExprSetCollByToken(Parse *pParse, Expr*, Token*); | 3024 Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*); > 3025 Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*); > 3026 Expr *sqlite3ExprSkipCollate(Expr*); 3025 int sqlite3CheckCollSeq(Parse *, CollSeq *); 3027 int sqlite3CheckCollSeq(Parse *, CollSeq *); 3026 int sqlite3CheckObjectName(Parse *, const char *); 3028 int sqlite3CheckObjectName(Parse *, const char *); 3027 void sqlite3VdbeSetChanges(sqlite3 *, int); 3029 void sqlite3VdbeSetChanges(sqlite3 *, int); 3028 int sqlite3AddInt64(i64*,i64); 3030 int sqlite3AddInt64(i64*,i64); 3029 int sqlite3SubInt64(i64*,i64); 3031 int sqlite3SubInt64(i64*,i64); 3030 int sqlite3MulInt64(i64*,i64); 3032 int sqlite3MulInt64(i64*,i64); 3031 int sqlite3AbsInt32(int); 3033 int sqlite3AbsInt32(int);

Changes to src/where.c

1345 } 1345 } 1346 *pC = c + 1; 1346 *pC = c + 1; 1347 } 1347 } 1348 sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; 1348 sCollSeqName.z = noCase ? "NOCASE" : "BINARY"; 1349 sCollSeqName.n = 6; 1349 sCollSeqName.n = 6; 1350 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); 1350 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0); 1351 pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 1351 pNewExpr1 = sqlite3PExpr(pParse, TK_GE, 1352 sqlite3ExprSetCollByToken(pParse,pNewExpr1,&sCollSeqName), | 1352 sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName), 1353 pStr1, 0); 1353 pStr1, 0); 1354 idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); 1354 idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); 1355 testcase( idxNew1==0 ); 1355 testcase( idxNew1==0 ); 1356 exprAnalyze(pSrc, pWC, idxNew1); 1356 exprAnalyze(pSrc, pWC, idxNew1); 1357 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); 1357 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0); 1358 pNewExpr2 = sqlite3PExpr(pParse, TK_LT, 1358 pNewExpr2 = sqlite3PExpr(pParse, TK_LT, 1359 sqlite3ExprSetCollByToken(pParse,pNewExpr2,&sCollSeqName), | 1359 sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName), 1360 pStr2, 0); 1360 pStr2, 0); 1361 idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); 1361 idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); 1362 testcase( idxNew2==0 ); 1362 testcase( idxNew2==0 ); 1363 exprAnalyze(pSrc, pWC, idxNew2); 1363 exprAnalyze(pSrc, pWC, idxNew2); 1364 pTerm = &pWC->a[idxTerm]; 1364 pTerm = &pWC->a[idxTerm]; 1365 if( isComplete ){ 1365 if( isComplete ){ 1366 pWC->a[idxNew1].iParent = idxTerm; 1366 pWC->a[idxNew1].iParent = idxTerm;

Changes to test/collate1.test

71 } {} 71 } {} 72 do_test collate1-1.1 { 72 do_test collate1-1.1 { 73 execsql { 73 execsql { 74 SELECT c2 FROM collate1t1 ORDER BY 1; 74 SELECT c2 FROM collate1t1 ORDER BY 1; 75 } 75 } 76 } {{} 0x119 0x2D} 76 } {{} 0x119 0x2D} 77 do_test collate1-1.2 { 77 do_test collate1-1.2 { > 78 breakpoint 78 execsql { 79 execsql { 79 SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex; 80 SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex; 80 } 81 } 81 } {{} 0x2D 0x119} 82 } {{} 0x2D 0x119} 82 do_test collate1-1.3 { 83 do_test collate1-1.3 { 83 execsql { 84 execsql { 84 SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex DESC; 85 SELECT c2 FROM collate1t1 ORDER BY 1 COLLATE hex DESC;