Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Tables created with the CREATE TABLE <tbl> AS SELECT ... syntax now inherit column declaration types from the SELECT statement. (CVS 1538) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
31c1668dbc2b84438a5b78b0270f58b3 |
User & Date: | danielk1977 2004-06-07 10:00:31.000 |
Context
2004-06-07
| ||
16:27 | More work on windows locking. Fix some problems with unix locking. There is still an assertion failure on windows locking in attach2.test. (CVS 1539) (check-in: 0c2d169cf3 user: drh tags: trunk) | |
10:00 | Tables created with the CREATE TABLE <tbl> AS SELECT ... syntax now inherit column declaration types from the SELECT statement. (CVS 1538) (check-in: 31c1668dbc user: danielk1977 tags: trunk) | |
07:52 | Add pragma to set/get text encoding. Also fix an obscure problem where a temp trigger could be accidently dropped. (CVS 1537) (check-in: 983221b038 user: danielk1977 tags: trunk) | |
Changes
Changes to src/build.c.
︙ | ︙ | |||
19 20 21 22 23 24 25 | ** DROP INDEX ** creating ID lists ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** PRAGMA ** | | | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | ** DROP INDEX ** creating ID lists ** BEGIN TRANSACTION ** COMMIT ** ROLLBACK ** PRAGMA ** ** $Id: build.c,v 1.208 2004/06/07 10:00:31 danielk1977 Exp $ */ #include "sqliteInt.h" #include <ctype.h> /* ** This routine is called when a new SQL statement is beginning to ** be parsed. Check to see if the schema for the database needs |
︙ | ︙ | |||
710 711 712 713 714 715 716 | if( z==0 ) return; for(i=j=0; z[i]; i++){ int c = z[i]; if( isspace(c) ) continue; z[j++] = c; } z[j] = 0; | < | 710 711 712 713 714 715 716 717 718 719 720 721 722 723 | if( z==0 ) return; for(i=j=0; z[i]; i++){ int c = z[i]; if( isspace(c) ) continue; z[j++] = c; } z[j] = 0; pCol->affinity = sqlite3AffinityType(z, n); } /* ** The given token is the default value for the last column added to ** the table currently under construction. If "minusFlag" is true, it ** means the value token was preceded by a minus sign. |
︙ | ︙ | |||
981 982 983 984 985 986 987 988 989 990 991 992 993 994 | static char *createTableStmt(Table *p){ int i, k, n; char *zStmt; char *zSep, *zSep2, *zEnd; n = 0; for(i=0; i<p->nCol; i++){ n += identLength(p->aCol[i].zName); } n += identLength(p->zName); if( n<40 ){ zSep = ""; zSep2 = ","; zEnd = ")"; }else{ | > > > | 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 | static char *createTableStmt(Table *p){ int i, k, n; char *zStmt; char *zSep, *zSep2, *zEnd; n = 0; for(i=0; i<p->nCol; i++){ n += identLength(p->aCol[i].zName); if( p->aCol[i].zType ){ n += (strlen(p->aCol[i].zType) + 1); } } n += identLength(p->zName); if( n<40 ){ zSep = ""; zSep2 = ","; zEnd = ")"; }else{ |
︙ | ︙ | |||
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 | identPut(zStmt, &k, p->zName); zStmt[k++] = '('; for(i=0; i<p->nCol; i++){ strcpy(&zStmt[k], zSep); k += strlen(&zStmt[k]); zSep = zSep2; identPut(zStmt, &k, p->aCol[i].zName); } strcpy(&zStmt[k], zEnd); return zStmt; } /* ** This routine is called to report the final ")" that terminates | > > > > > | 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 | identPut(zStmt, &k, p->zName); zStmt[k++] = '('; for(i=0; i<p->nCol; i++){ strcpy(&zStmt[k], zSep); k += strlen(&zStmt[k]); zSep = zSep2; identPut(zStmt, &k, p->aCol[i].zName); if( p->aCol[i].zType ){ zStmt[k++] = ' '; strcpy(&zStmt[k], p->aCol[i].zType); k += strlen(p->aCol[i].zType); } } strcpy(&zStmt[k], zEnd); return zStmt; } /* ** This routine is called to report the final ")" that terminates |
︙ | ︙ | |||
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 | void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){ Table *p; sqlite *db = pParse->db; if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return; p = pParse->pNewTable; if( p==0 ) return; /* If the table is generated from a SELECT, then construct the ** list of columns and the text of the table. */ if( pSelect ){ | > > < < < < < < < < | 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 | void sqlite3EndTable(Parse *pParse, Token *pEnd, Select *pSelect){ Table *p; sqlite *db = pParse->db; if( (pEnd==0 && pSelect==0) || pParse->nErr || sqlite3_malloc_failed ) return; p = pParse->pNewTable; if( p==0 ) return; assert( !db->init.busy || !pSelect ); /* If the table is generated from a SELECT, then construct the ** list of columns and the text of the table. */ if( pSelect ){ } /* If the db->init.busy is 1 it means we are reading the SQL off the ** "sqlite_master" or "sqlite_temp_master" table on the disk. ** So do not write to the disk again. Extract the root page number ** for the table from the db->init.newTnum field. (The page number ** should have been put there by the sqliteOpenCb routine.) |
︙ | ︙ | |||
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 | */ if( !db->init.busy ){ int n; Vdbe *v; v = sqlite3GetVdbe(pParse); if( v==0 ) return; if( p->pSelect==0 ){ /* A regular table */ sqlite3VdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER); }else{ /* A view */ sqlite3VdbeAddOp(v, OP_Integer, 0, 0); } p->tnum = 0; | > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | > | 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 | */ if( !db->init.busy ){ int n; Vdbe *v; v = sqlite3GetVdbe(pParse); if( v==0 ) return; if( p->pSelect==0 ){ /* A regular table */ sqlite3VdbeOp3(v, OP_CreateTable, 0, p->iDb, (char*)&p->tnum, P3_POINTER); }else{ /* A view */ sqlite3VdbeAddOp(v, OP_Integer, 0, 0); } p->tnum = 0; sqlite3VdbeAddOp(v, OP_Close, 0, 0); /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT ** statement to populate the new table. The root-page number for the ** new table is on the top of the vdbe stack. ** ** Once the SELECT has been coded by sqlite3Select(), it is in a ** suitable state to query for the column names and types to be used ** by the new table. */ if( pSelect ){ Table *pSelTab; sqlite3VdbeAddOp(v, OP_Dup, 0, 0); sqlite3VdbeAddOp(v, OP_Integer, p->iDb, 0); sqlite3VdbeAddOp(v, OP_OpenWrite, 1, 0); pParse->nTab = 2; sqlite3Select(pParse, pSelect, SRT_Table, 1, 0, 0, 0, 0); sqlite3VdbeAddOp(v, OP_Close, 1, 0); if( pParse->nErr==0 ){ pSelTab = sqlite3ResultSetOfSelect(pParse, 0, pSelect); if( pSelTab==0 ) return; assert( p->aCol==0 ); p->nCol = pSelTab->nCol; p->aCol = pSelTab->aCol; pSelTab->nCol = 0; pSelTab->aCol = 0; sqlite3DeleteTable(0, pSelTab); } } sqlite3OpenMasterTable(v, p->iDb); sqlite3VdbeOp3(v, OP_String8, 0, 0, p->pSelect==0?"table":"view",P3_STATIC); sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0); sqlite3VdbeOp3(v, OP_String8, 0, 0, p->zName, 0); sqlite3VdbeAddOp(v, OP_Pull, 3, 0); if( pSelect ){ char *z = createTableStmt(p); n = z ? strlen(z) : 0; sqlite3VdbeAddOp(v, OP_String8, 0, 0); sqlite3VdbeChangeP3(v, -1, z, n); sqliteFree(z); }else{ |
︙ | ︙ | |||
1111 1112 1113 1114 1115 1116 1117 | } sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC); sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0); if( p->iDb!=1 ){ sqlite3ChangeCookie(db, v, p->iDb); } sqlite3VdbeAddOp(v, OP_Close, 0, 0); | < < < < < | | 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 | } sqlite3VdbeOp3(v, OP_MakeRecord, 5, 0, "tttit", P3_STATIC); sqlite3VdbeAddOp(v, OP_PutIntKey, 0, 0); if( p->iDb!=1 ){ sqlite3ChangeCookie(db, v, p->iDb); } sqlite3VdbeAddOp(v, OP_Close, 0, 0); sqlite3EndWriteOperation(pParse); } /* Add the table to the in-memory representation of the database. */ if( pParse->explain==0 && pParse->nErr==0 ){ Table *pOld; |
︙ | ︙ |
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.184 2004/06/07 10:00:31 danielk1977 Exp $ */ #include "sqliteInt.h" /* ** Allocate a new Select structure and return a pointer to that ** structure. |
︙ | ︙ | |||
608 609 610 611 612 613 614 | sqlite3VdbeResolveLabel(v, end2); sqlite3VdbeAddOp(v, OP_Pop, 1, 0); sqlite3VdbeResolveLabel(v, end1); sqlite3VdbeAddOp(v, OP_SortReset, 0, 0); } /* | | | | < < < | | < < | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | < < < < < < < < < < < < < < < < < < < < | 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 | sqlite3VdbeResolveLabel(v, end2); sqlite3VdbeAddOp(v, OP_Pop, 1, 0); sqlite3VdbeResolveLabel(v, end1); sqlite3VdbeAddOp(v, OP_SortReset, 0, 0); } /* ** Return a pointer to a string containing the 'declaration type' of the ** expression pExpr. The string may be treated as static by the caller. ** ** If the declaration type is the exact datatype definition extracted from ** the original CREATE TABLE statement if the expression is a column. ** ** The declaration type for an expression is either TEXT, NUMERIC or ANY. ** The declaration type for a ROWID field is INTEGER. */ static const char *columnType(Parse *pParse, SrcList *pTabList, Expr *pExpr){ char const *zType = 0; int j; if( pExpr==0 ) return 0; if( pExpr->op==TK_COLUMN && pTabList ){ Table *pTab; int iCol = pExpr->iColumn; for(j=0; j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable; j++){} assert( j<pTabList->nSrc ); pTab = pTabList->a[j].pTab; if( iCol<0 ) iCol = pTab->iPKey; assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) ); if( iCol<0 ){ zType = "INTEGER"; }else{ zType = pTab->aCol[iCol].zType; } }else{ switch( sqlite3ExprType(pExpr) ){ case SQLITE_AFF_TEXT: zType = "TEXT"; break; case SQLITE_AFF_NUMERIC: zType = "NUMERIC"; break; default: zType = "ANY"; break; } } return zType; } /* ** Generate code that will tell the VDBE the declaration types of columns ** in the result set. */ static void generateColumnTypes( Parse *pParse, /* Parser context */ SrcList *pTabList, /* List of tables */ ExprList *pEList /* Expressions defining the result set */ ){ Vdbe *v = pParse->pVdbe; int i; for(i=0; i<pEList->nExpr; i++){ Expr *p = pEList->a[i].pExpr; const char *zType = columnType(pParse, pTabList, p); if( p==0 ) continue; sqlite3VdbeSetColName(v, i+pEList->nExpr, zType, P3_STATIC); } } /* ** Generate code that will tell the VDBE the names of columns ** in the result set. This information is used to provide the |
︙ | ︙ | |||
775 776 777 778 779 780 781 | } pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; pEList = pSelect->pEList; pTab->nCol = pEList->nExpr; assert( pTab->nCol>0 ); pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); for(i=0; i<pTab->nCol; i++){ | | > > | | > | | > > > | 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 | } pTab->zName = zTabName ? sqliteStrDup(zTabName) : 0; pEList = pSelect->pEList; pTab->nCol = pEList->nExpr; assert( pTab->nCol>0 ); pTab->aCol = aCol = sqliteMalloc( sizeof(pTab->aCol[0])*pTab->nCol ); for(i=0; i<pTab->nCol; i++){ Expr *pR; char *zType; Expr *p = pEList->a[i].pExpr; if( pEList->a[i].zName ){ aCol[i].zName = sqliteStrDup(pEList->a[i].zName); }else if( p->op==TK_DOT && (pR=p->pRight)!=0 && pR->token.z && pR->token.z[0] ){ int cnt; sqlite3SetNString(&aCol[i].zName, pR->token.z, pR->token.n, 0); for(j=cnt=0; j<i; j++){ if( sqlite3StrICmp(aCol[j].zName, aCol[i].zName)==0 ){ int n; char zBuf[30]; sprintf(zBuf,"_%d",++cnt); n = strlen(zBuf); sqlite3SetNString(&aCol[i].zName, pR->token.z, pR->token.n, zBuf,n,0); j = -1; } } }else if( p->span.z && p->span.z[0] ){ sqlite3SetNString(&pTab->aCol[i].zName, p->span.z, p->span.n, 0); }else{ char zBuf[30]; sprintf(zBuf, "column%d", i+1); pTab->aCol[i].zName = sqliteStrDup(zBuf); } zType = sqliteStrDup(columnType(pParse, pSelect->pSrc ,p)); pTab->aCol[i].zType = zType; pTab->aCol[i].affinity = SQLITE_AFF_NUMERIC; if( zType ){ pTab->aCol[i].affinity = sqlite3AffinityType(zType, strlen(zType)); } } pTab->iPKey = -1; return pTab; } /* ** For the given SELECT statement, do three things. |
︙ | ︙ |
Changes to test/table.test.
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. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing the CREATE TABLE statement. # | | | 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. # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing the CREATE TABLE statement. # # $Id: table.test,v 1.25 2004/06/07 10:00:31 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Create a basic table and verify it is added to sqlite_master # do_test table-1.1 { |
︙ | ︙ | |||
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | SELECT * FROM weird; } } {desc a asc b explain 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all} # Try out the CREATE TABLE AS syntax # do_test table-8.1 { execsql2 { CREATE TABLE t2 AS SELECT * FROM weird; SELECT * FROM t2; } } {desc a asc b explain 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all} do_test table-8.1.1 { execsql { SELECT sql FROM sqlite_master WHERE name='t2'; } } {{CREATE TABLE t2( | > | | | | | | | | | 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 | SELECT * FROM weird; } } {desc a asc b explain 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all} # Try out the CREATE TABLE AS syntax # do_test table-8.1 { breakpoint execsql2 { CREATE TABLE t2 AS SELECT * FROM weird; SELECT * FROM t2; } } {desc a asc b explain 9 14_vac 0 fuzzy_dog_12 xyz begin hi end y'all} do_test table-8.1.1 { execsql { SELECT sql FROM sqlite_master WHERE name='t2'; } } {{CREATE TABLE t2( 'desc' text, 'asc' text, 'explain' int, '14_vac' boolean, fuzzy_dog_12 varchar(10), 'begin' blob, 'end' clob )}} do_test table-8.2 { execsql { CREATE TABLE 't3''xyz'(a,b,c); INSERT INTO [t3'xyz] VALUES(1,2,3); SELECT * FROM [t3'xyz]; } } {1 2 3} do_test table-8.3 { execsql2 { CREATE TABLE [t4'abc] AS SELECT count(*) as cnt, max(b+c) FROM [t3'xyz]; SELECT * FROM [t4'abc]; } } {cnt 1 max(b+c) 5} do_test table-8.3.1 { execsql { SELECT sql FROM sqlite_master WHERE name='t4''abc' } } {{CREATE TABLE 't4''abc'(cnt NUMERIC,'max(b+c)' NUMERIC)}} do_test table-8.4 { execsql2 { CREATE TEMPORARY TABLE t5 AS SELECT count(*) AS [y'all] FROM [t3'xyz]; SELECT * FROM t5; } } {y'all 1} do_test table-8.5 { |
︙ | ︙ |