Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Bug fixes in the VIEW implementation. (CVS 396) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
668ef6380eba256ef82477b63aef8502 |
User & Date: | drh 2002-02-27 01:47:12.000 |
Context
2002-02-27
| ||
01:53 | The user-defined function routines failed to detect a malloc() failure. (CVS 397) (check-in: 085b0d671a user: drh tags: trunk) | |
01:47 | Bug fixes in the VIEW implementation. (CVS 396) (check-in: 668ef6380e user: drh tags: trunk) | |
2002-02-26
| ||
23:55 | Comment changes only. (CVS 395) (check-in: 63b87f2ea6 user: drh tags: trunk) | |
Changes
Changes to src/build.c.
︙ | ︙ | |||
21 22 23 24 25 26 27 | ** COPY ** VACUUM ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** PRAGMA ** | | | 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | ** COPY ** VACUUM ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** PRAGMA ** ** $Id: build.c,v 1.80 2002/02/27 01:47:12 drh Exp $ */ #include "sqliteInt.h" #include <ctype.h> /* ** This routine is called after a single SQL statement has been ** parsed and we want to execute the VDBE code to implement |
︙ | ︙ | |||
820 821 822 823 824 825 826 827 828 829 830 831 832 833 | if( pSelTab==0 ) goto create_view_failed; assert( p->aCol==0 ); p->nCol = pSelTab->nCol; p->aCol = pSelTab->aCol; pSelTab->nCol = 0; pSelTab->aCol = 0; sqliteDeleteTable(0, pSelTab); sEnd = pParse->sLastToken; if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){ sEnd.z += sEnd.n; } sEnd.n = 0; n = ((int)sEnd.z) - (int)pBegin->z; z = p->pSelect->zSelect = sqliteStrNDup(pBegin->z, n+1); | > | 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 | if( pSelTab==0 ) goto create_view_failed; assert( p->aCol==0 ); p->nCol = pSelTab->nCol; p->aCol = pSelTab->aCol; pSelTab->nCol = 0; pSelTab->aCol = 0; sqliteDeleteTable(0, pSelTab); sqliteSelectUnbind(pSelect); sEnd = pParse->sLastToken; if( sEnd.z[0]!=0 && sEnd.z[0]!=';' ){ sEnd.z += sEnd.n; } sEnd.n = 0; n = ((int)sEnd.z) - (int)pBegin->z; z = p->pSelect->zSelect = sqliteStrNDup(pBegin->z, n+1); |
︙ | ︙ | |||
1337 1338 1339 1340 1341 1342 1343 | */ void sqliteIdListDelete(IdList *pList){ int i; if( pList==0 ) return; for(i=0; i<pList->nId; i++){ sqliteFree(pList->a[i].zName); sqliteFree(pList->a[i].zAlias); | < < < < < | < < > < | 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 | */ void sqliteIdListDelete(IdList *pList){ int i; if( pList==0 ) return; for(i=0; i<pList->nId; i++){ sqliteFree(pList->a[i].zName); sqliteFree(pList->a[i].zAlias); if( pList->a[i].pTab && pList->a[i].pTab->isTransient ){ sqliteDeleteTable(0, pList->a[i].pTab); } sqliteSelectDelete(pList->a[i].pSelect); } sqliteFree(pList->a); sqliteFree(pList); } /* ** The COPY command is for compatibility with PostgreSQL and specificially ** for the ability to read the output of pg_dump. The format is as ** follows: ** ** COPY table FROM file [USING DELIMITERS string] |
︙ | ︙ |
Changes to src/expr.c.
︙ | ︙ | |||
8 9 10 11 12 13 14 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains routines used for analyzing expressions and ** for generating VDBE code that evaluates expressions in SQLite. ** | | | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains routines used for analyzing expressions and ** for generating VDBE code that evaluates expressions in SQLite. ** ** $Id: expr.c,v 1.46 2002/02/27 01:47:12 drh Exp $ */ #include "sqliteInt.h" /* ** Construct a new expression node and return a pointer to it. Memory ** for this node is obtained from sqliteMalloc(). The calling function |
︙ | ︙ | |||
142 143 144 145 146 147 148 149 150 151 152 153 154 155 | sqliteExprListMoveStrings(pSelect->pEList, offset); sqliteExprMoveStrings(pSelect->pWhere, offset); sqliteExprListMoveStrings(pSelect->pGroupBy, offset); sqliteExprMoveStrings(pSelect->pHaving, offset); sqliteExprListMoveStrings(pSelect->pOrderBy, offset); sqliteSelectMoveStrings(pSelect->pPrior, offset); } /* ** Add a new element to the end of an expression list. If pList is ** initially NULL, then create a new expression list. */ ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ int i; | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | sqliteExprListMoveStrings(pSelect->pEList, offset); sqliteExprMoveStrings(pSelect->pWhere, offset); sqliteExprListMoveStrings(pSelect->pGroupBy, offset); sqliteExprMoveStrings(pSelect->pHaving, offset); sqliteExprListMoveStrings(pSelect->pOrderBy, offset); sqliteSelectMoveStrings(pSelect->pPrior, offset); } /* ** The following group of routines make deep copies of expressions, ** expression lists, ID lists, and select statements. The copies can ** be deleted (by being passed to their respective ...Delete() routines) ** without effecting the originals. ** ** Note, however, that the Expr.token.z and Expr.span.z fields point to ** string space that is allocated separately from the expression tree ** itself. These routines do NOT duplicate that string space. ** ** The expression list and ID list return by sqliteExprListDup() and ** sqliteIdListDup() can not be further expanded by subsequent calls ** to sqliteExprListAppend() or sqliteIdListAppend(). ** ** Any tables that the ID list might point to are not duplicated. */ Expr *sqliteExprDup(Expr *p){ Expr *pNew; if( p==0 ) return 0; pNew = sqliteMalloc( sizeof(*p) ); if( pNew==0 ) return 0; pNew->op = p->op; pNew->pLeft = sqliteExprDup(p->pLeft); pNew->pRight = sqliteExprDup(p->pRight); pNew->pList = sqliteExprListDup(p->pList); pNew->token = p->token; pNew->span = p->span; pNew->pSelect = sqliteSelectDup(p->pSelect); return pNew; } ExprList *sqliteExprListDup(ExprList *p){ ExprList *pNew; int i; if( p==0 ) return 0; pNew = sqliteMalloc( sizeof(*pNew) ); if( pNew==0 ) return 0; pNew->nExpr = p->nExpr; pNew->a = sqliteMalloc( p->nExpr*sizeof(p->a[0]) ); for(i=0; i<p->nExpr; i++){ pNew->a[i].pExpr = sqliteExprDup(p->a[i].pExpr); pNew->a[i].zName = sqliteStrDup(p->a[i].zName); pNew->a[i].sortOrder = p->a[i].sortOrder; pNew->a[i].isAgg = p->a[i].isAgg; pNew->a[i].done = 0; } return pNew; } IdList *sqliteIdListDup(IdList *p){ IdList *pNew; int i; if( p==0 ) return 0; pNew = sqliteMalloc( sizeof(*pNew) ); if( pNew==0 ) return 0; pNew->nId = p->nId; pNew->a = sqliteMalloc( p->nId*sizeof(p->a[0]) ); for(i=0; i<p->nId; i++){ pNew->a[i].zName = sqliteStrDup(p->a[i].zName); pNew->a[i].zAlias = sqliteStrDup(p->a[i].zAlias); pNew->a[i].idx = p->a[i].idx; pNew->a[i].pTab = 0; pNew->a[i].pSelect = sqliteSelectDup(p->a[i].pSelect); } return pNew; } Select *sqliteSelectDup(Select *p){ Select *pNew; if( p==0 ) return 0; pNew = sqliteMalloc( sizeof(*p) ); if( pNew==0 ) return 0; pNew->isDistinct = p->isDistinct; pNew->pEList = sqliteExprListDup(p->pEList); pNew->pSrc = sqliteIdListDup(p->pSrc); pNew->pWhere = sqliteExprDup(p->pWhere); pNew->pGroupBy = sqliteExprListDup(p->pGroupBy); pNew->pHaving = sqliteExprDup(p->pHaving); pNew->pOrderBy = sqliteExprListDup(p->pOrderBy); pNew->op = p->op; pNew->pPrior = sqliteSelectDup(p->pPrior); pNew->nLimit = p->nLimit; pNew->nOffset = p->nOffset; pNew->zSelect = 0; return pNew; } /* ** Add a new element to the end of an expression list. If pList is ** initially NULL, then create a new expression list. */ ExprList *sqliteExprListAppend(ExprList *pList, Expr *pExpr, Token *pName){ int i; |
︙ | ︙ |
Changes to src/select.c.
︙ | ︙ | |||
8 9 10 11 12 13 14 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle SELECT statements in SQLite. ** | | | 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** This file contains C code routines that are called by the parser ** to handle SELECT statements in SQLite. ** ** $Id: select.c,v 1.67 2002/02/27 01:47:12 drh Exp $ */ #include "sqliteInt.h" /* ** Allocate a new Select structure and return a pointer to that ** structure. */ |
︙ | ︙ | |||
393 394 395 396 397 398 399 | if( pTab==0 ){ sqliteSetString(&pParse->zErrMsg, "no such table: ", pTabList->a[i].zName, 0); pParse->nErr++; return 1; } if( pTab->pSelect ){ | | | 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | if( pTab==0 ){ sqliteSetString(&pParse->zErrMsg, "no such table: ", pTabList->a[i].zName, 0); pParse->nErr++; return 1; } if( pTab->pSelect ){ pTabList->a[i].pSelect = sqliteSelectDup(pTab->pSelect); } } } /* For every "*" that occurs in the column list, insert the names of ** all columns in all tables. The parser inserted a special expression ** with the TK_ALL operator for each "*" that it found in the column list. |
︙ | ︙ | |||
451 452 453 454 455 456 457 458 459 460 461 462 463 464 | } } sqliteExprListDelete(pEList); p->pEList = pNew; } return 0; } /* ** 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. | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | } } sqliteExprListDelete(pEList); p->pEList = pNew; } return 0; } /* ** This routine recursively unlinks the Select.pSrc.a[].pTab pointers ** in a select structure. It just sets the pointers to NULL. This ** routine is recursive in the sense that if the Select.pSrc.a[].pSelect ** pointer is not NULL, this routine is called recursively on that pointer. ** ** This routine is called on the Select structure that defines a ** VIEW in order to undo any bindings to tables. This is necessary ** because those tables might be DROPed by a subsequent SQL command. */ void sqliteSelectUnbind(Select *p){ int i; IdList *pSrc = p->pSrc; Table *pTab; if( p==0 ) return; for(i=0; i<pSrc->nId; i++){ if( (pTab = pSrc->a[i].pTab)!=0 ){ if( pTab->isTransient ){ sqliteDeleteTable(0, pTab); sqliteSelectDelete(pSrc->a[i].pSelect); pSrc->a[i].pSelect = 0; } pSrc->a[i].pTab = 0; if( pSrc->a[i].pSelect ){ sqliteSelectUnbind(pSrc->a[i].pSelect); } } } } /* ** 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. |
︙ | ︙ |
Changes to src/sqliteInt.h.
1 2 3 4 5 6 7 8 9 10 11 12 13 | /* ** 2001 September 15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** Internal interface definitions for SQLite. ** | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /* ** 2001 September 15 ** ** The author disclaims copyright to this source code. In place of ** a legal notice, here is a blessing: ** ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* ** Internal interface definitions for SQLite. ** ** @(#) $Id: sqliteInt.h,v 1.94 2002/02/27 01:47:12 drh Exp $ */ #include "sqlite.h" #include "hash.h" #include "vdbe.h" #include "parse.h" #include "btree.h" #include <stdio.h> |
︙ | ︙ | |||
601 602 603 604 605 606 607 608 609 610 611 612 613 614 | void sqliteIdListDelete(IdList*); void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, int, Token*, Token*); void sqliteDropIndex(Parse*, Token*); int sqliteSelect(Parse*, Select*, int, int); Select *sqliteSelectNew(ExprList*,IdList*,Expr*,ExprList*,Expr*,ExprList*, int,int,int); void sqliteSelectDelete(Select*); Table *sqliteTableNameToTable(Parse*, const char*); IdList *sqliteTableTokenToIdList(Parse*, Token*); void sqliteDeleteFrom(Parse*, Token*, Expr*); void sqliteUpdate(Parse*, Token*, ExprList*, Expr*, int); WhereInfo *sqliteWhereBegin(Parse*, IdList*, Expr*, int); void sqliteWhereEnd(WhereInfo*); void sqliteExprCode(Parse*, Expr*); | > | 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | void sqliteIdListDelete(IdList*); void sqliteCreateIndex(Parse*, Token*, Token*, IdList*, int, Token*, Token*); void sqliteDropIndex(Parse*, Token*); int sqliteSelect(Parse*, Select*, int, int); Select *sqliteSelectNew(ExprList*,IdList*,Expr*,ExprList*,Expr*,ExprList*, int,int,int); void sqliteSelectDelete(Select*); void sqliteSelectUnbind(Select*); Table *sqliteTableNameToTable(Parse*, const char*); IdList *sqliteTableTokenToIdList(Parse*, Token*); void sqliteDeleteFrom(Parse*, Token*, Expr*); void sqliteUpdate(Parse*, Token*, ExprList*, Expr*, int); WhereInfo *sqliteWhereBegin(Parse*, IdList*, Expr*, int); void sqliteWhereEnd(WhereInfo*); void sqliteExprCode(Parse*, Expr*); |
︙ | ︙ | |||
642 643 644 645 646 647 648 649 650 | void sqliteCompleteInsertion(Parse*, Table*, int, char*, int, int); void sqliteBeginWriteOperation(Parse*); void sqliteBeginMultiWriteOperation(Parse*); void sqliteEndWriteOperation(Parse*); void sqliteExprMoveStrings(Expr*, int); void sqliteExprListMoveStrings(ExprList*, int); void sqliteSelectMoveStrings(Select*, int); UserFunc *sqliteFindUserFunction(sqlite*,const char*,int,int,int); void sqliteRegisterBuildinFunctions(sqlite*); | > > > > | 643 644 645 646 647 648 649 650 651 652 653 654 655 | void sqliteCompleteInsertion(Parse*, Table*, int, char*, int, int); void sqliteBeginWriteOperation(Parse*); void sqliteBeginMultiWriteOperation(Parse*); void sqliteEndWriteOperation(Parse*); void sqliteExprMoveStrings(Expr*, int); void sqliteExprListMoveStrings(ExprList*, int); void sqliteSelectMoveStrings(Select*, int); Expr *sqliteExprDup(Expr*); ExprList *sqliteExprListDup(ExprList*); IdList *sqliteIdListDup(IdList*); Select *sqliteSelectDup(Select*); UserFunc *sqliteFindUserFunction(sqlite*,const char*,int,int,int); void sqliteRegisterBuildinFunctions(sqlite*); |
Changes to src/util.c.
︙ | ︙ | |||
10 11 12 13 14 15 16 | ** ************************************************************************* ** Utility functions used throughout sqlite. ** ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** | | | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ** ************************************************************************* ** Utility functions used throughout sqlite. ** ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** ** $Id: util.c,v 1.38 2002/02/27 01:47:12 drh Exp $ */ #include "sqliteInt.h" #include <stdarg.h> #include <ctype.h> /* ** If malloc() ever fails, this global variable gets set to 1. |
︙ | ︙ | |||
179 180 181 182 183 184 185 | *pz = zNew; } /* ** Make a copy of a string in memory obtained from sqliteMalloc() */ char *sqliteStrDup_(const char *z, char *zFile, int line){ | > > | > > | | 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | *pz = zNew; } /* ** Make a copy of a string in memory obtained from sqliteMalloc() */ char *sqliteStrDup_(const char *z, char *zFile, int line){ char *zNew; if( z==0 ) return 0; zNew = sqliteMalloc_(strlen(z)+1, zFile, line); if( zNew ) strcpy(zNew, z); return zNew; } char *sqliteStrNDup_(const char *z, int n, char *zFile, int line){ char *zNew; if( z==0 ) return 0; zNew = sqliteMalloc_(n+1, zFile, line); if( zNew ){ memcpy(zNew, z, n); zNew[n] = 0; } return zNew; } #endif /* MEMORY_DEBUG */ |
︙ | ︙ |
Added test/view.test.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | # 2002 February 26 # # The author disclaims copyright to this source code. In place of # a legal notice, here is a blessing: # # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing VIEW statements. # # $Id: view.test,v 1.1 2002/02/27 01:47:12 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl do_test view-1.0 { execsql { CREATE TABLE t1(a,b,c); INSERT INTO t1 VALUES(1,2,3); INSERT INTO t1 VALUES(4,5,6); INSERT INTO t1 VALUES(7,8,9); SELECT * FROM t1; } } {1 2 3 4 5 6 7 8 9} do_test view-1.1 { execsql { BEGIN; CREATE VIEW v1 AS SELECT a,b FROM t1; SELECT * FROM v1 ORDER BY a; } } {1 2 4 5 7 8} do_test view-1.2 { catchsql { ROLLBACK; SELECT * FROM v1 ORDER BY a; } } {1 {no such table: v1}} do_test view-1.3 { execsql { CREATE VIEW v1 AS SELECT a,b FROM t1; SELECT * FROM v1 ORDER BY a; } } {1 2 4 5 7 8} do_test view-1.4 { catchsql { DROP VIEW v1; SELECT * FROM v1 ORDER BY a; } } {1 {no such table: v1}} do_test view-1.5 { execsql { CREATE VIEW v1 AS SELECT a,b FROM t1; SELECT * FROM v1 ORDER BY a; } } {1 2 4 5 7 8} do_test view-1.6 { catchsql { DROP TABLE t1; SELECT * FROM v1 ORDER BY a; } } {1 {no such table: t1}} do_test view-1.7 { execsql { CREATE TABLE t1(x,a,b,c); INSERT INTO t1 VALUES(1,2,3,4); INSERT INTO t1 VALUES(4,5,6,7); INSERT INTO t1 VALUES(7,8,9,10); SELECT * FROM v1 ORDER BY a; } } {2 3 5 6 8 9} finish_test |