Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: third_party/sqlite/src/src/insert.c

Issue 6990047: Import SQLite 3.7.6.3. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/sqlite/src/src/hwtime.h ('k') | third_party/sqlite/src/src/journal.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 ** 2001 September 15 2 ** 2001 September 15
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
11 ************************************************************************* 11 *************************************************************************
12 ** This file contains C code routines that are called by the parser 12 ** This file contains C code routines that are called by the parser
13 ** to handle INSERT statements in SQLite. 13 ** to handle INSERT statements in SQLite.
14 **
15 ** $Id: insert.c,v 1.270 2009/07/24 17:58:53 danielk1977 Exp $
16 */ 14 */
17 #include "sqliteInt.h" 15 #include "sqliteInt.h"
18 16
19 /* 17 /*
20 ** Generate code that will open a table for reading. 18 ** Generate code that will open a table for reading.
21 */ 19 */
22 void sqlite3OpenTable( 20 void sqlite3OpenTable(
23 Parse *p, /* Generate code into this VDBE */ 21 Parse *p, /* Generate code into this VDBE */
24 int iCur, /* The cursor number of the table */ 22 int iCur, /* The cursor number of the table */
25 int iDb, /* The database index in sqlite3.aDb[] */ 23 int iDb, /* The database index in sqlite3.aDb[] */
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 ** required, it is allocated and populated here. It is then stored as 60 ** required, it is allocated and populated here. It is then stored as
63 ** a member of the Index structure for subsequent use. 61 ** a member of the Index structure for subsequent use.
64 ** 62 **
65 ** The column affinity string will eventually be deleted by 63 ** The column affinity string will eventually be deleted by
66 ** sqliteDeleteIndex() when the Index structure itself is cleaned 64 ** sqliteDeleteIndex() when the Index structure itself is cleaned
67 ** up. 65 ** up.
68 */ 66 */
69 int n; 67 int n;
70 Table *pTab = pIdx->pTable; 68 Table *pTab = pIdx->pTable;
71 sqlite3 *db = sqlite3VdbeDb(v); 69 sqlite3 *db = sqlite3VdbeDb(v);
72 pIdx->zColAff = (char *)sqlite3Malloc(pIdx->nColumn+2); 70 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2);
73 if( !pIdx->zColAff ){ 71 if( !pIdx->zColAff ){
74 db->mallocFailed = 1; 72 db->mallocFailed = 1;
75 return 0; 73 return 0;
76 } 74 }
77 for(n=0; n<pIdx->nColumn; n++){ 75 for(n=0; n<pIdx->nColumn; n++){
78 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; 76 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
79 } 77 }
80 pIdx->zColAff[n++] = SQLITE_AFF_NONE; 78 pIdx->zColAff[n++] = SQLITE_AFF_NONE;
81 pIdx->zColAff[n] = 0; 79 pIdx->zColAff[n] = 0;
82 } 80 }
(...skipping 21 matching lines...) Expand all
104 ** stored as a member of the Table structure for subsequent use. 102 ** stored as a member of the Table structure for subsequent use.
105 ** 103 **
106 ** The column affinity string will eventually be deleted by 104 ** The column affinity string will eventually be deleted by
107 ** sqlite3DeleteTable() when the Table structure itself is cleaned up. 105 ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
108 */ 106 */
109 if( !pTab->zColAff ){ 107 if( !pTab->zColAff ){
110 char *zColAff; 108 char *zColAff;
111 int i; 109 int i;
112 sqlite3 *db = sqlite3VdbeDb(v); 110 sqlite3 *db = sqlite3VdbeDb(v);
113 111
114 zColAff = (char *)sqlite3Malloc(pTab->nCol+1); 112 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
115 if( !zColAff ){ 113 if( !zColAff ){
116 db->mallocFailed = 1; 114 db->mallocFailed = 1;
117 return; 115 return;
118 } 116 }
119 117
120 for(i=0; i<pTab->nCol; i++){ 118 for(i=0; i<pTab->nCol; i++){
121 zColAff[i] = pTab->aCol[i].affinity; 119 zColAff[i] = pTab->aCol[i].affinity;
122 } 120 }
123 zColAff[pTab->nCol] = '\0'; 121 zColAff[pTab->nCol] = '\0';
124 122
125 pTab->zColAff = zColAff; 123 pTab->zColAff = zColAff;
126 } 124 }
127 125
128 sqlite3VdbeChangeP4(v, -1, pTab->zColAff, 0); 126 sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
129 } 127 }
130 128
131 /* 129 /*
132 ** Return non-zero if the table pTab in database iDb or any of its indices 130 ** Return non-zero if the table pTab in database iDb or any of its indices
133 ** have been opened at any point in the VDBE program beginning at location 131 ** have been opened at any point in the VDBE program beginning at location
134 ** iStartAddr throught the end of the program. This is used to see if 132 ** iStartAddr throught the end of the program. This is used to see if
135 ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can 133 ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
136 ** run without using temporary table for the results of the SELECT. 134 ** run without using temporary table for the results of the SELECT.
137 */ 135 */
138 static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){ 136 static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 230
233 /* This routine is never called during trigger-generation. It is 231 /* This routine is never called during trigger-generation. It is
234 ** only called from the top-level */ 232 ** only called from the top-level */
235 assert( pParse->pTriggerTab==0 ); 233 assert( pParse->pTriggerTab==0 );
236 assert( pParse==sqlite3ParseToplevel(pParse) ); 234 assert( pParse==sqlite3ParseToplevel(pParse) );
237 235
238 assert( v ); /* We failed long ago if this is not so */ 236 assert( v ); /* We failed long ago if this is not so */
239 for(p = pParse->pAinc; p; p = p->pNext){ 237 for(p = pParse->pAinc; p; p = p->pNext){
240 pDb = &db->aDb[p->iDb]; 238 pDb = &db->aDb[p->iDb];
241 memId = p->regCtr; 239 memId = p->regCtr;
240 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
242 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead); 241 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
243 addr = sqlite3VdbeCurrentAddr(v); 242 addr = sqlite3VdbeCurrentAddr(v);
244 sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0); 243 sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
245 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); 244 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
246 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId); 245 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
247 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); 246 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
248 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); 247 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
249 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1); 248 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
250 sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId); 249 sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
251 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9); 250 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
(...skipping 30 matching lines...) Expand all
282 sqlite3 *db = pParse->db; 281 sqlite3 *db = pParse->db;
283 282
284 assert( v ); 283 assert( v );
285 for(p = pParse->pAinc; p; p = p->pNext){ 284 for(p = pParse->pAinc; p; p = p->pNext){
286 Db *pDb = &db->aDb[p->iDb]; 285 Db *pDb = &db->aDb[p->iDb];
287 int j1, j2, j3, j4, j5; 286 int j1, j2, j3, j4, j5;
288 int iRec; 287 int iRec;
289 int memId = p->regCtr; 288 int memId = p->regCtr;
290 289
291 iRec = sqlite3GetTempReg(pParse); 290 iRec = sqlite3GetTempReg(pParse);
291 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
292 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); 292 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
293 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); 293 j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
294 j2 = sqlite3VdbeAddOp0(v, OP_Rewind); 294 j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
295 j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec); 295 j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
296 j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec); 296 j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
297 sqlite3VdbeAddOp2(v, OP_Next, 0, j3); 297 sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
298 sqlite3VdbeJumpHere(v, j2); 298 sqlite3VdbeJumpHere(v, j2);
299 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1); 299 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
300 j5 = sqlite3VdbeAddOp0(v, OP_Goto); 300 j5 = sqlite3VdbeAddOp0(v, OP_Goto);
301 sqlite3VdbeJumpHere(v, j4); 301 sqlite3VdbeJumpHere(v, j4);
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 Db *pDb; /* The database containing table being inserted into */ 460 Db *pDb; /* The database containing table being inserted into */
461 int appendFlag = 0; /* True if the insert is likely to be an append */ 461 int appendFlag = 0; /* True if the insert is likely to be an append */
462 462
463 /* Register allocations */ 463 /* Register allocations */
464 int regFromSelect = 0;/* Base register for data coming from SELECT */ 464 int regFromSelect = 0;/* Base register for data coming from SELECT */
465 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ 465 int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
466 int regRowCount = 0; /* Memory cell used for the row counter */ 466 int regRowCount = 0; /* Memory cell used for the row counter */
467 int regIns; /* Block of regs holding rowid+data being inserted */ 467 int regIns; /* Block of regs holding rowid+data being inserted */
468 int regRowid; /* registers holding insert rowid */ 468 int regRowid; /* registers holding insert rowid */
469 int regData; /* register holding first column to insert */ 469 int regData; /* register holding first column to insert */
470 int regRecord; /* Holds the assemblied row record */
471 int regEof = 0; /* Register recording end of SELECT data */ 470 int regEof = 0; /* Register recording end of SELECT data */
472 int *aRegIdx = 0; /* One register allocated to each index */ 471 int *aRegIdx = 0; /* One register allocated to each index */
473 472
474 #ifndef SQLITE_OMIT_TRIGGER 473 #ifndef SQLITE_OMIT_TRIGGER
475 int isView; /* True if attempting to insert into a view */ 474 int isView; /* True if attempting to insert into a view */
476 Trigger *pTrigger; /* List of triggers on pTab, if required */ 475 Trigger *pTrigger; /* List of triggers on pTab, if required */
477 int tmask; /* Mask of trigger times */ 476 int tmask; /* Mask of trigger times */
478 #endif 477 #endif
479 478
480 db = pParse->db; 479 db = pParse->db;
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 } 721 }
723 break; 722 break;
724 } 723 }
725 } 724 }
726 if( j>=pTab->nCol ){ 725 if( j>=pTab->nCol ){
727 if( sqlite3IsRowid(pColumn->a[i].zName) ){ 726 if( sqlite3IsRowid(pColumn->a[i].zName) ){
728 keyColumn = i; 727 keyColumn = i;
729 }else{ 728 }else{
730 sqlite3ErrorMsg(pParse, "table %S has no column named %s", 729 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
731 pTabList, 0, pColumn->a[i].zName); 730 pTabList, 0, pColumn->a[i].zName);
732 pParse->nErr++; 731 pParse->checkSchema = 1;
733 goto insert_cleanup; 732 goto insert_cleanup;
734 } 733 }
735 } 734 }
736 } 735 }
737 } 736 }
738 737
739 /* If there is no IDLIST term but the table has an integer primary 738 /* If there is no IDLIST term but the table has an integer primary
740 ** key, the set the keyColumn variable to the primary key column index 739 ** key, the set the keyColumn variable to the primary key column index
741 ** in the original table definition. 740 ** in the original table definition.
742 */ 741 */
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 ** goto C 788 ** goto C
790 ** D: ... 789 ** D: ...
791 */ 790 */
792 addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); 791 addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm);
793 addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof); 792 addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
794 } 793 }
795 794
796 /* Allocate registers for holding the rowid of the new row, 795 /* Allocate registers for holding the rowid of the new row,
797 ** the content of the new row, and the assemblied row record. 796 ** the content of the new row, and the assemblied row record.
798 */ 797 */
799 regRecord = ++pParse->nMem;
800 regRowid = regIns = pParse->nMem+1; 798 regRowid = regIns = pParse->nMem+1;
801 pParse->nMem += pTab->nCol + 1; 799 pParse->nMem += pTab->nCol + 1;
802 if( IsVirtual(pTab) ){ 800 if( IsVirtual(pTab) ){
803 regRowid++; 801 regRowid++;
804 pParse->nMem++; 802 pParse->nMem++;
805 } 803 }
806 regData = regRowid+1; 804 regData = regRowid+1;
807 805
808 /* Run the BEFORE and INSTEAD OF triggers, if there are any 806 /* Run the BEFORE and INSTEAD OF triggers, if there are any
809 */ 807 */
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 /* Create the new column data 839 /* Create the new column data
842 */ 840 */
843 for(i=0; i<pTab->nCol; i++){ 841 for(i=0; i<pTab->nCol; i++){
844 if( pColumn==0 ){ 842 if( pColumn==0 ){
845 j = i; 843 j = i;
846 }else{ 844 }else{
847 for(j=0; j<pColumn->nId; j++){ 845 for(j=0; j<pColumn->nId; j++){
848 if( pColumn->a[j].idx==i ) break; 846 if( pColumn->a[j].idx==i ) break;
849 } 847 }
850 } 848 }
851 if( pColumn && j>=pColumn->nId ){ 849 if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
852 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); 850 sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
853 }else if( useTempTable ){ 851 }else if( useTempTable ){
854 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); 852 sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
855 }else{ 853 }else{
856 assert( pSelect==0 ); /* Otherwise useTempTable is true */ 854 assert( pSelect==0 ); /* Otherwise useTempTable is true */
857 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); 855 sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
858 } 856 }
859 } 857 }
860 858
861 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, 859 /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
862 ** do not attempt any conversions before assembling the record. 860 ** do not attempt any conversions before assembling the record.
863 ** If this is a real table, attempt conversions as required by the 861 ** If this is a real table, attempt conversions as required by the
864 ** table column affinities. 862 ** table column affinities.
865 */ 863 */
866 if( !isView ){ 864 if( !isView ){
867 sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol); 865 sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
868 sqlite3TableAffinityStr(v, pTab); 866 sqlite3TableAffinityStr(v, pTab);
869 } 867 }
870 868
871 /* Fire BEFORE or INSTEAD OF triggers */ 869 /* Fire BEFORE or INSTEAD OF triggers */
872 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 870 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
873 pTab, -1, regCols-pTab->nCol-1, onError, endOfLoop); 871 pTab, regCols-pTab->nCol-1, onError, endOfLoop);
874 872
875 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1); 873 sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
876 } 874 }
877 875
878 /* Push the record number for the new entry onto the stack. The 876 /* Push the record number for the new entry onto the stack. The
879 ** record number is a randomly generate integer created by NewRowid 877 ** record number is a randomly generate integer created by NewRowid
880 ** except when the table has an INTEGER PRIMARY KEY column, in which 878 ** except when the table has an INTEGER PRIMARY KEY column, in which
881 ** case the record number is the same as that column. 879 ** case the record number is the same as that column.
882 */ 880 */
883 if( !isView ){ 881 if( !isView ){
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
972 sqlite3VtabMakeWritable(pParse, pTab); 970 sqlite3VtabMakeWritable(pParse, pTab);
973 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB); 971 sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
974 sqlite3MayAbort(pParse); 972 sqlite3MayAbort(pParse);
975 }else 973 }else
976 #endif 974 #endif
977 { 975 {
978 int isReplace; /* Set to true if constraints may cause a replace */ 976 int isReplace; /* Set to true if constraints may cause a replace */
979 sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx, 977 sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
980 keyColumn>=0, 0, onError, endOfLoop, &isReplace 978 keyColumn>=0, 0, onError, endOfLoop, &isReplace
981 ); 979 );
980 sqlite3FkCheck(pParse, pTab, 0, regIns);
982 sqlite3CompleteInsertion( 981 sqlite3CompleteInsertion(
983 pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0 982 pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
984 ); 983 );
985 } 984 }
986 } 985 }
987 986
988 /* Update the count of rows that are inserted 987 /* Update the count of rows that are inserted
989 */ 988 */
990 if( (db->flags & SQLITE_CountRows)!=0 ){ 989 if( (db->flags & SQLITE_CountRows)!=0 ){
991 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); 990 sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
992 } 991 }
993 992
994 if( pTrigger ){ 993 if( pTrigger ){
995 /* Code AFTER triggers */ 994 /* Code AFTER triggers */
996 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, 995 sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
997 pTab, -1, regData-2-pTab->nCol, onError, endOfLoop); 996 pTab, regData-2-pTab->nCol, onError, endOfLoop);
998 } 997 }
999 998
1000 /* The bottom of the main insertion loop, if the data source 999 /* The bottom of the main insertion loop, if the data source
1001 ** is a SELECT statement. 1000 ** is a SELECT statement.
1002 */ 1001 */
1003 sqlite3VdbeResolveLabel(v, endOfLoop); 1002 sqlite3VdbeResolveLabel(v, endOfLoop);
1004 if( useTempTable ){ 1003 if( useTempTable ){
1005 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); 1004 sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
1006 sqlite3VdbeJumpHere(v, addrInsTop); 1005 sqlite3VdbeJumpHere(v, addrInsTop);
1007 sqlite3VdbeAddOp1(v, OP_Close, srcTab); 1006 sqlite3VdbeAddOp1(v, OP_Close, srcTab);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1039 } 1038 }
1040 1039
1041 insert_cleanup: 1040 insert_cleanup:
1042 sqlite3SrcListDelete(db, pTabList); 1041 sqlite3SrcListDelete(db, pTabList);
1043 sqlite3ExprListDelete(db, pList); 1042 sqlite3ExprListDelete(db, pList);
1044 sqlite3SelectDelete(db, pSelect); 1043 sqlite3SelectDelete(db, pSelect);
1045 sqlite3IdListDelete(db, pColumn); 1044 sqlite3IdListDelete(db, pColumn);
1046 sqlite3DbFree(db, aRegIdx); 1045 sqlite3DbFree(db, aRegIdx);
1047 } 1046 }
1048 1047
1048 /* Make sure "isView" and other macros defined above are undefined. Otherwise
1049 ** thely may interfere with compilation of other functions in this file
1050 ** (or in another file, if this file becomes part of the amalgamation). */
1051 #ifdef isView
1052 #undef isView
1053 #endif
1054 #ifdef pTrigger
1055 #undef pTrigger
1056 #endif
1057 #ifdef tmask
1058 #undef tmask
1059 #endif
1060
1061
1049 /* 1062 /*
1050 ** Generate code to do constraint checks prior to an INSERT or an UPDATE. 1063 ** Generate code to do constraint checks prior to an INSERT or an UPDATE.
1051 ** 1064 **
1052 ** The input is a range of consecutive registers as follows: 1065 ** The input is a range of consecutive registers as follows:
1053 ** 1066 **
1054 ** 1. The rowid of the row after the update. 1067 ** 1. The rowid of the row after the update.
1055 ** 1068 **
1056 ** 2. The data in the first column of the entry after the update. 1069 ** 2. The data in the first column of the entry after the update.
1057 ** 1070 **
1058 ** i. Data from middle columns... 1071 ** i. Data from middle columns...
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
1168 onError = OE_Abort; 1181 onError = OE_Abort;
1169 } 1182 }
1170 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail 1183 assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
1171 || onError==OE_Ignore || onError==OE_Replace ); 1184 || onError==OE_Ignore || onError==OE_Replace );
1172 switch( onError ){ 1185 switch( onError ){
1173 case OE_Abort: 1186 case OE_Abort:
1174 sqlite3MayAbort(pParse); 1187 sqlite3MayAbort(pParse);
1175 case OE_Rollback: 1188 case OE_Rollback:
1176 case OE_Fail: { 1189 case OE_Fail: {
1177 char *zMsg; 1190 char *zMsg;
1178 j1 = sqlite3VdbeAddOp3(v, OP_HaltIfNull, 1191 sqlite3VdbeAddOp3(v, OP_HaltIfNull,
1179 SQLITE_CONSTRAINT, onError, regData+i); 1192 SQLITE_CONSTRAINT, onError, regData+i);
1180 zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL", 1193 zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL",
1181 pTab->zName, pTab->aCol[i].zName); 1194 pTab->zName, pTab->aCol[i].zName);
1182 sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC); 1195 sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
1183 break; 1196 break;
1184 } 1197 }
1185 case OE_Ignore: { 1198 case OE_Ignore: {
1186 sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest); 1199 sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
1187 break; 1200 break;
1188 } 1201 }
(...skipping 11 matching lines...) Expand all
1200 */ 1213 */
1201 #ifndef SQLITE_OMIT_CHECK 1214 #ifndef SQLITE_OMIT_CHECK
1202 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){ 1215 if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
1203 int allOk = sqlite3VdbeMakeLabel(v); 1216 int allOk = sqlite3VdbeMakeLabel(v);
1204 pParse->ckBase = regData; 1217 pParse->ckBase = regData;
1205 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL); 1218 sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL);
1206 onError = overrideError!=OE_Default ? overrideError : OE_Abort; 1219 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
1207 if( onError==OE_Ignore ){ 1220 if( onError==OE_Ignore ){
1208 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 1221 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
1209 }else{ 1222 }else{
1223 if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
1210 sqlite3HaltConstraint(pParse, onError, 0, 0); 1224 sqlite3HaltConstraint(pParse, onError, 0, 0);
1211 } 1225 }
1212 sqlite3VdbeResolveLabel(v, allOk); 1226 sqlite3VdbeResolveLabel(v, allOk);
1213 } 1227 }
1214 #endif /* !defined(SQLITE_OMIT_CHECK) */ 1228 #endif /* !defined(SQLITE_OMIT_CHECK) */
1215 1229
1216 /* If we have an INTEGER PRIMARY KEY, make sure the primary key 1230 /* If we have an INTEGER PRIMARY KEY, make sure the primary key
1217 ** of the new record does not previously exist. Except, if this 1231 ** of the new record does not previously exist. Except, if this
1218 ** is an UPDATE and the primary key is not changing, that is OK. 1232 ** is an UPDATE and the primary key is not changing, that is OK.
1219 */ 1233 */
1220 if( rowidChng ){ 1234 if( rowidChng ){
1221 onError = pTab->keyConf; 1235 onError = pTab->keyConf;
1222 if( overrideError!=OE_Default ){ 1236 if( overrideError!=OE_Default ){
1223 onError = overrideError; 1237 onError = overrideError;
1224 }else if( onError==OE_Default ){ 1238 }else if( onError==OE_Default ){
1225 onError = OE_Abort; 1239 onError = OE_Abort;
1226 } 1240 }
1227 1241
1228 if( onError!=OE_Replace || pTab->pIndex ){ 1242 if( isUpdate ){
1229 if( isUpdate ){ 1243 j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
1230 j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng); 1244 }
1245 j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
1246 switch( onError ){
1247 default: {
1248 onError = OE_Abort;
1249 /* Fall thru into the next case */
1231 } 1250 }
1232 j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid); 1251 case OE_Rollback:
1233 switch( onError ){ 1252 case OE_Abort:
1234 default: { 1253 case OE_Fail: {
1235 onError = OE_Abort; 1254 sqlite3HaltConstraint(
1236 /* Fall thru into the next case */ 1255 pParse, onError, "PRIMARY KEY must be unique", P4_STATIC);
1256 break;
1257 }
1258 case OE_Replace: {
1259 /* If there are DELETE triggers on this table and the
1260 ** recursive-triggers flag is set, call GenerateRowDelete() to
1261 ** remove the conflicting row from the the table. This will fire
1262 ** the triggers and remove both the table and index b-tree entries.
1263 **
1264 ** Otherwise, if there are no triggers or the recursive-triggers
1265 ** flag is not set, but the table has one or more indexes, call
1266 ** GenerateRowIndexDelete(). This removes the index b-tree entries
1267 ** only. The table b-tree entry will be replaced by the new entry
1268 ** when it is inserted.
1269 **
1270 ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
1271 ** also invoke MultiWrite() to indicate that this VDBE may require
1272 ** statement rollback (if the statement is aborted after the delete
1273 ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
1274 ** but being more selective here allows statements like:
1275 **
1276 ** REPLACE INTO t(rowid) VALUES($newrowid)
1277 **
1278 ** to run without a statement journal if there are no indexes on the
1279 ** table.
1280 */
1281 Trigger *pTrigger = 0;
1282 if( pParse->db->flags&SQLITE_RecTriggers ){
1283 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
1237 } 1284 }
1238 case OE_Rollback: 1285 if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
1239 case OE_Abort: 1286 sqlite3MultiWrite(pParse);
1240 case OE_Fail: { 1287 sqlite3GenerateRowDelete(
1241 sqlite3HaltConstraint( 1288 pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
1242 pParse, onError, "PRIMARY KEY must be unique", P4_STATIC); 1289 );
1243 break; 1290 }else if( pTab->pIndex ){
1291 sqlite3MultiWrite(pParse);
1292 sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
1244 } 1293 }
1245 case OE_Replace: { 1294 seenReplace = 1;
1246 /* If there are DELETE triggers on this table and the 1295 break;
1247 ** recursive-triggers flag is set, call GenerateRowDelete() to
1248 ** remove the conflicting row from the the table. This will fire
1249 ** the triggers and remove both the table and index b-tree entries.
1250 **
1251 ** Otherwise, if there are no triggers or the recursive-triggers
1252 ** flag is not set, call GenerateRowIndexDelete(). This removes
1253 ** the index b-tree entries only. The table b-tree entry will be
1254 ** replaced by the new entry when it is inserted. */
1255 Trigger *pTrigger = 0;
1256 if( pParse->db->flags&SQLITE_RecTriggers ){
1257 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
1258 }
1259 if( pTrigger ){
1260 sqlite3GenerateRowDelete(
1261 pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
1262 );
1263 }else{
1264 sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
1265 }
1266 seenReplace = 1;
1267 break;
1268 }
1269 case OE_Ignore: {
1270 assert( seenReplace==0 );
1271 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
1272 break;
1273 }
1274 } 1296 }
1275 sqlite3VdbeJumpHere(v, j3); 1297 case OE_Ignore: {
1276 if( isUpdate ){ 1298 assert( seenReplace==0 );
1277 sqlite3VdbeJumpHere(v, j2); 1299 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
1300 break;
1278 } 1301 }
1279 } 1302 }
1303 sqlite3VdbeJumpHere(v, j3);
1304 if( isUpdate ){
1305 sqlite3VdbeJumpHere(v, j2);
1306 }
1280 } 1307 }
1281 1308
1282 /* Test all UNIQUE constraints by creating entries for each UNIQUE 1309 /* Test all UNIQUE constraints by creating entries for each UNIQUE
1283 ** index and making sure that duplicate entries do not already exist. 1310 ** index and making sure that duplicate entries do not already exist.
1284 ** Add the new records to the indices as we go. 1311 ** Add the new records to the indices as we go.
1285 */ 1312 */
1286 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ 1313 for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
1287 int regIdx; 1314 int regIdx;
1288 int regR; 1315 int regR;
1289 1316
1290 if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */ 1317 if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */
1291 1318
1292 /* Create a key for accessing the index entry */ 1319 /* Create a key for accessing the index entry */
1293 regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1); 1320 regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
1294 for(i=0; i<pIdx->nColumn; i++){ 1321 for(i=0; i<pIdx->nColumn; i++){
1295 int idx = pIdx->aiColumn[i]; 1322 int idx = pIdx->aiColumn[i];
1296 if( idx==pTab->iPKey ){ 1323 if( idx==pTab->iPKey ){
1297 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); 1324 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
1298 }else{ 1325 }else{
1299 sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i); 1326 sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
1300 } 1327 }
1301 } 1328 }
1302 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); 1329 sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
1303 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]); 1330 sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
1304 sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0); 1331 sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
1305 sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1); 1332 sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
1306 1333
1307 /* Find out what action to take in case there is an indexing conflict */ 1334 /* Find out what action to take in case there is an indexing conflict */
1308 onError = pIdx->onError; 1335 onError = pIdx->onError;
1309 if( onError==OE_None ){ 1336 if( onError==OE_None ){
1310 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1); 1337 sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
1311 continue; /* pIdx is not a UNIQUE index */ 1338 continue; /* pIdx is not a UNIQUE index */
1312 } 1339 }
1313 if( overrideError!=OE_Default ){ 1340 if( overrideError!=OE_Default ){
1314 onError = overrideError; 1341 onError = overrideError;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1357 break; 1384 break;
1358 } 1385 }
1359 case OE_Ignore: { 1386 case OE_Ignore: {
1360 assert( seenReplace==0 ); 1387 assert( seenReplace==0 );
1361 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); 1388 sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
1362 break; 1389 break;
1363 } 1390 }
1364 default: { 1391 default: {
1365 Trigger *pTrigger = 0; 1392 Trigger *pTrigger = 0;
1366 assert( onError==OE_Replace ); 1393 assert( onError==OE_Replace );
1394 sqlite3MultiWrite(pParse);
1367 if( pParse->db->flags&SQLITE_RecTriggers ){ 1395 if( pParse->db->flags&SQLITE_RecTriggers ){
1368 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); 1396 pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
1369 } 1397 }
1370 sqlite3GenerateRowDelete( 1398 sqlite3GenerateRowDelete(
1371 pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace 1399 pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
1372 ); 1400 );
1373 seenReplace = 1; 1401 seenReplace = 1;
1374 break; 1402 break;
1375 } 1403 }
1376 } 1404 }
1377 sqlite3VdbeJumpHere(v, j3); 1405 sqlite3VdbeJumpHere(v, j3);
1378 sqlite3ReleaseTempReg(pParse, regR); 1406 sqlite3ReleaseTempReg(pParse, regR);
1379 } 1407 }
1380 1408
1381 if( pbMayReplace ){ 1409 if( pbMayReplace ){
1382 *pbMayReplace = seenReplace; 1410 *pbMayReplace = seenReplace;
1383 } 1411 }
1384 } 1412 }
1385 1413
1386 /* 1414 /*
1387 ** This routine generates code to finish the INSERT or UPDATE operation 1415 ** This routine generates code to finish the INSERT or UPDATE operation
1388 ** that was started by a prior call to sqlite3GenerateConstraintChecks. 1416 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
1389 ** A consecutive range of registers starting at regRowid contains the 1417 ** A consecutive range of registers starting at regRowid contains the
1390 ** rowid and the content to be inserted. 1418 ** rowid and the content to be inserted.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1433 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); 1461 pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
1434 } 1462 }
1435 if( appendBias ){ 1463 if( appendBias ){
1436 pik_flags |= OPFLAG_APPEND; 1464 pik_flags |= OPFLAG_APPEND;
1437 } 1465 }
1438 if( useSeekResult ){ 1466 if( useSeekResult ){
1439 pik_flags |= OPFLAG_USESEEKRESULT; 1467 pik_flags |= OPFLAG_USESEEKRESULT;
1440 } 1468 }
1441 sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid); 1469 sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
1442 if( !pParse->nested ){ 1470 if( !pParse->nested ){
1443 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); 1471 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
1444 } 1472 }
1445 sqlite3VdbeChangeP5(v, pik_flags); 1473 sqlite3VdbeChangeP5(v, pik_flags);
1446 } 1474 }
1447 1475
1448 /* 1476 /*
1449 ** Generate code that will open cursors for a table and for all 1477 ** Generate code that will open cursors for a table and for all
1450 ** indices of that table. The "baseCur" parameter is the cursor number used 1478 ** indices of that table. The "baseCur" parameter is the cursor number used
1451 ** for the table. Indices are opened on subsequent cursors. 1479 ** for the table. Indices are opened on subsequent cursors.
1452 ** 1480 **
1453 ** Return the number of indices on the table. 1481 ** Return the number of indices on the table.
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
1695 destHasUniqueIdx = 1; 1723 destHasUniqueIdx = 1;
1696 } 1724 }
1697 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ 1725 for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
1698 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; 1726 if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
1699 } 1727 }
1700 if( pSrcIdx==0 ){ 1728 if( pSrcIdx==0 ){
1701 return 0; /* pDestIdx has no corresponding index in pSrc */ 1729 return 0; /* pDestIdx has no corresponding index in pSrc */
1702 } 1730 }
1703 } 1731 }
1704 #ifndef SQLITE_OMIT_CHECK 1732 #ifndef SQLITE_OMIT_CHECK
1705 if( pDest->pCheck && !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){ 1733 if( pDest->pCheck && sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
1706 return 0; /* Tables have different CHECK constraints. Ticket #2252 */ 1734 return 0; /* Tables have different CHECK constraints. Ticket #2252 */
1707 } 1735 }
1708 #endif 1736 #endif
1709 1737
1710 /* If we get this far, it means either: 1738 /* If we get this far, it means either:
1711 ** 1739 **
1712 ** * We can always do the transfer if the table contains an 1740 ** * We can always do the transfer if the table contains an
1713 ** an integer primary key 1741 ** an integer primary key
1714 ** 1742 **
1715 ** * We can conditionally do the transfer if the destination 1743 ** * We can conditionally do the transfer if the destination
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1793 if( emptyDestTest ){ 1821 if( emptyDestTest ){
1794 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); 1822 sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
1795 sqlite3VdbeJumpHere(v, emptyDestTest); 1823 sqlite3VdbeJumpHere(v, emptyDestTest);
1796 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); 1824 sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
1797 return 0; 1825 return 0;
1798 }else{ 1826 }else{
1799 return 1; 1827 return 1;
1800 } 1828 }
1801 } 1829 }
1802 #endif /* SQLITE_OMIT_XFER_OPT */ 1830 #endif /* SQLITE_OMIT_XFER_OPT */
1803
1804 /* Make sure "isView" gets undefined in case this file becomes part of
1805 ** the amalgamation - so that subsequent files do not see isView as a
1806 ** macro. */
1807 #undef isView
OLDNEW
« no previous file with comments | « third_party/sqlite/src/src/hwtime.h ('k') | third_party/sqlite/src/src/journal.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698