PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - pdo_sqlite/sqlite/src - insert.c
Test: PHP Code Coverage
Date: 2009-11-19 Instrumented lines: 587
Code covered: 43.1 % Executed lines: 253
Legend: not executed executed

       1                 : /*
       2                 : ** 2001 September 15
       3                 : **
       4                 : ** The author disclaims copyright to this source code.  In place of
       5                 : ** a legal notice, here is a blessing:
       6                 : **
       7                 : **    May you do good and not evil.
       8                 : **    May you find forgiveness for yourself and forgive others.
       9                 : **    May you share freely, never taking more than you give.
      10                 : **
      11                 : *************************************************************************
      12                 : ** This file contains C code routines that are called by the parser
      13                 : ** to handle INSERT statements in SQLite.
      14                 : **
      15                 : ** $Id$
      16                 : */
      17                 : #include "sqliteInt.h"
      18                 : 
      19                 : /*
      20                 : ** Set P3 of the most recently inserted opcode to a column affinity
      21                 : ** string for index pIdx. A column affinity string has one character
      22                 : ** for each column in the table, according to the affinity of the column:
      23                 : **
      24                 : **  Character      Column affinity
      25                 : **  ------------------------------
      26                 : **  'a'            TEXT
      27                 : **  'b'            NONE
      28                 : **  'c'            NUMERIC
      29                 : **  'd'            INTEGER
      30                 : **  'e'            REAL
      31                 : */
      32             104 : void sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
      33             104 :   if( !pIdx->zColAff ){
      34                 :     /* The first time a column affinity string for a particular index is
      35                 :     ** required, it is allocated and populated here. It is then stored as
      36                 :     ** a member of the Index structure for subsequent use.
      37                 :     **
      38                 :     ** The column affinity string will eventually be deleted by
      39                 :     ** sqliteDeleteIndex() when the Index structure itself is cleaned
      40                 :     ** up.
      41                 :     */
      42                 :     int n;
      43              39 :     Table *pTab = pIdx->pTable;
      44              39 :     pIdx->zColAff = (char *)sqliteMalloc(pIdx->nColumn+1);
      45              39 :     if( !pIdx->zColAff ){
      46               0 :       return;
      47                 :     }
      48              78 :     for(n=0; n<pIdx->nColumn; n++){
      49              39 :       pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
      50                 :     }
      51              39 :     pIdx->zColAff[pIdx->nColumn] = '\0';
      52                 :   }
      53                 :  
      54             104 :   sqlite3VdbeChangeP3(v, -1, pIdx->zColAff, 0);
      55                 : }
      56                 : 
      57                 : /*
      58                 : ** Set P3 of the most recently inserted opcode to a column affinity
      59                 : ** string for table pTab. A column affinity string has one character
      60                 : ** for each column indexed by the index, according to the affinity of the
      61                 : ** column:
      62                 : **
      63                 : **  Character      Column affinity
      64                 : **  ------------------------------
      65                 : **  'a'            TEXT
      66                 : **  'b'            NONE
      67                 : **  'c'            NUMERIC
      68                 : **  'd'            INTEGER
      69                 : **  'e'            REAL
      70                 : */
      71             222 : void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
      72                 :   /* The first time a column affinity string for a particular table
      73                 :   ** is required, it is allocated and populated here. It is then 
      74                 :   ** stored as a member of the Table structure for subsequent use.
      75                 :   **
      76                 :   ** The column affinity string will eventually be deleted by
      77                 :   ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
      78                 :   */
      79             222 :   if( !pTab->zColAff ){
      80                 :     char *zColAff;
      81                 :     int i;
      82                 : 
      83             111 :     zColAff = (char *)sqliteMalloc(pTab->nCol+1);
      84             111 :     if( !zColAff ){
      85               0 :       return;
      86                 :     }
      87                 : 
      88             513 :     for(i=0; i<pTab->nCol; i++){
      89             402 :       zColAff[i] = pTab->aCol[i].affinity;
      90                 :     }
      91             111 :     zColAff[pTab->nCol] = '\0';
      92                 : 
      93             111 :     pTab->zColAff = zColAff;
      94                 :   }
      95                 : 
      96             222 :   sqlite3VdbeChangeP3(v, -1, pTab->zColAff, 0);
      97                 : }
      98                 : 
      99                 : /*
     100                 : ** Return non-zero if SELECT statement p opens the table with rootpage
     101                 : ** iTab in database iDb.  This is used to see if a statement of the form 
     102                 : ** "INSERT INTO <iDb, iTab> SELECT ..." can run without using temporary
     103                 : ** table for the results of the SELECT. 
     104                 : **
     105                 : ** No checking is done for sub-selects that are part of expressions.
     106                 : */
     107               0 : static int selectReadsTable(Select *p, Schema *pSchema, int iTab){
     108                 :   int i;
     109                 :   struct SrcList_item *pItem;
     110               0 :   if( p->pSrc==0 ) return 0;
     111               0 :   for(i=0, pItem=p->pSrc->a; i<p->pSrc->nSrc; i++, pItem++){
     112               0 :     if( pItem->pSelect ){
     113               0 :       if( selectReadsTable(pItem->pSelect, pSchema, iTab) ) return 1;
     114                 :     }else{
     115               0 :       if( pItem->pTab->pSchema==pSchema && pItem->pTab->tnum==iTab ) return 1;
     116                 :     }
     117                 :   }
     118               0 :   return 0;
     119                 : }
     120                 : 
     121                 : #ifndef SQLITE_OMIT_AUTOINCREMENT
     122                 : /*
     123                 : ** Write out code to initialize the autoincrement logic.  This code
     124                 : ** looks up the current autoincrement value in the sqlite_sequence
     125                 : ** table and stores that value in a memory cell.  Code generated by
     126                 : ** autoIncStep() will keep that memory cell holding the largest
     127                 : ** rowid value.  Code generated by autoIncEnd() will write the new
     128                 : ** largest value of the counter back into the sqlite_sequence table.
     129                 : **
     130                 : ** This routine returns the index of the mem[] cell that contains
     131                 : ** the maximum rowid counter.
     132                 : **
     133                 : ** Two memory cells are allocated.  The next memory cell after the
     134                 : ** one returned holds the rowid in sqlite_sequence where we will
     135                 : ** write back the revised maximum rowid.
     136                 : */
     137                 : static int autoIncBegin(
     138                 :   Parse *pParse,      /* Parsing context */
     139                 :   int iDb,            /* Index of the database holding pTab */
     140                 :   Table *pTab         /* The table we are writing to */
     141             159 : ){
     142             159 :   int memId = 0;
     143             159 :   if( pTab->autoInc ){
     144               0 :     Vdbe *v = pParse->pVdbe;
     145               0 :     Db *pDb = &pParse->db->aDb[iDb];
     146               0 :     int iCur = pParse->nTab;
     147                 :     int addr;
     148                 :     assert( v );
     149               0 :     addr = sqlite3VdbeCurrentAddr(v);
     150               0 :     memId = pParse->nMem+1;
     151               0 :     pParse->nMem += 2;
     152               0 :     sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
     153               0 :     sqlite3VdbeAddOp(v, OP_Rewind, iCur, addr+13);
     154               0 :     sqlite3VdbeAddOp(v, OP_Column, iCur, 0);
     155               0 :     sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
     156               0 :     sqlite3VdbeAddOp(v, OP_Ne, 0x100, addr+12);
     157               0 :     sqlite3VdbeAddOp(v, OP_Rowid, iCur, 0);
     158               0 :     sqlite3VdbeAddOp(v, OP_MemStore, memId-1, 1);
     159               0 :     sqlite3VdbeAddOp(v, OP_Column, iCur, 1);
     160               0 :     sqlite3VdbeAddOp(v, OP_MemStore, memId, 1);
     161               0 :     sqlite3VdbeAddOp(v, OP_Goto, 0, addr+13);
     162               0 :     sqlite3VdbeAddOp(v, OP_Next, iCur, addr+4);
     163               0 :     sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
     164                 :   }
     165             159 :   return memId;
     166                 : }
     167                 : 
     168                 : /*
     169                 : ** Update the maximum rowid for an autoincrement calculation.
     170                 : **
     171                 : ** This routine should be called when the top of the stack holds a
     172                 : ** new rowid that is about to be inserted.  If that new rowid is
     173                 : ** larger than the maximum rowid in the memId memory cell, then the
     174                 : ** memory cell is updated.  The stack is unchanged.
     175                 : */
     176             159 : static void autoIncStep(Parse *pParse, int memId){
     177             159 :   if( memId>0 ){
     178               0 :     sqlite3VdbeAddOp(pParse->pVdbe, OP_MemMax, memId, 0);
     179                 :   }
     180             159 : }
     181                 : 
     182                 : /*
     183                 : ** After doing one or more inserts, the maximum rowid is stored
     184                 : ** in mem[memId].  Generate code to write this value back into the
     185                 : ** the sqlite_sequence table.
     186                 : */
     187                 : static void autoIncEnd(
     188                 :   Parse *pParse,     /* The parsing context */
     189                 :   int iDb,           /* Index of the database holding pTab */
     190                 :   Table *pTab,       /* Table we are inserting into */
     191                 :   int memId          /* Memory cell holding the maximum rowid */
     192             159 : ){
     193             159 :   if( pTab->autoInc ){
     194               0 :     int iCur = pParse->nTab;
     195               0 :     Vdbe *v = pParse->pVdbe;
     196               0 :     Db *pDb = &pParse->db->aDb[iDb];
     197                 :     int addr;
     198                 :     assert( v );
     199               0 :     addr = sqlite3VdbeCurrentAddr(v);
     200               0 :     sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
     201               0 :     sqlite3VdbeAddOp(v, OP_MemLoad, memId-1, 0);
     202               0 :     sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+7);
     203               0 :     sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
     204               0 :     sqlite3VdbeAddOp(v, OP_NewRowid, iCur, 0);
     205               0 :     sqlite3VdbeOp3(v, OP_String8, 0, 0, pTab->zName, 0);
     206               0 :     sqlite3VdbeAddOp(v, OP_MemLoad, memId, 0);
     207               0 :     sqlite3VdbeAddOp(v, OP_MakeRecord, 2, 0);
     208               0 :     sqlite3VdbeAddOp(v, OP_Insert, iCur, OPFLAG_APPEND);
     209               0 :     sqlite3VdbeAddOp(v, OP_Close, iCur, 0);
     210                 :   }
     211             159 : }
     212                 : #else
     213                 : /*
     214                 : ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
     215                 : ** above are all no-ops
     216                 : */
     217                 : # define autoIncBegin(A,B,C) (0)
     218                 : # define autoIncStep(A,B)
     219                 : # define autoIncEnd(A,B,C,D)
     220                 : #endif /* SQLITE_OMIT_AUTOINCREMENT */
     221                 : 
     222                 : 
     223                 : /* Forward declaration */
     224                 : static int xferOptimization(
     225                 :   Parse *pParse,        /* Parser context */
     226                 :   Table *pDest,         /* The table we are inserting into */
     227                 :   Select *pSelect,      /* A SELECT statement to use as the data source */
     228                 :   int onError,          /* How to handle constraint errors */
     229                 :   int iDbDest           /* The database of pDest */
     230                 : );
     231                 : 
     232                 : /*
     233                 : ** This routine is call to handle SQL of the following forms:
     234                 : **
     235                 : **    insert into TABLE (IDLIST) values(EXPRLIST)
     236                 : **    insert into TABLE (IDLIST) select
     237                 : **
     238                 : ** The IDLIST following the table name is always optional.  If omitted,
     239                 : ** then a list of all columns for the table is substituted.  The IDLIST
     240                 : ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
     241                 : **
     242                 : ** The pList parameter holds EXPRLIST in the first form of the INSERT
     243                 : ** statement above, and pSelect is NULL.  For the second form, pList is
     244                 : ** NULL and pSelect is a pointer to the select statement used to generate
     245                 : ** data for the insert.
     246                 : **
     247                 : ** The code generated follows one of four templates.  For a simple
     248                 : ** select with data coming from a VALUES clause, the code executes
     249                 : ** once straight down through.  The template looks like this:
     250                 : **
     251                 : **         open write cursor to <table> and its indices
     252                 : **         puts VALUES clause expressions onto the stack
     253                 : **         write the resulting record into <table>
     254                 : **         cleanup
     255                 : **
     256                 : ** The three remaining templates assume the statement is of the form
     257                 : **
     258                 : **   INSERT INTO <table> SELECT ...
     259                 : **
     260                 : ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
     261                 : ** in other words if the SELECT pulls all columns from a single table
     262                 : ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
     263                 : ** if <table2> and <table1> are distinct tables but have identical
     264                 : ** schemas, including all the same indices, then a special optimization
     265                 : ** is invoked that copies raw records from <table2> over to <table1>.
     266                 : ** See the xferOptimization() function for the implementation of this
     267                 : ** template.  This is the second template.
     268                 : **
     269                 : **         open a write cursor to <table>
     270                 : **         open read cursor on <table2>
     271                 : **         transfer all records in <table2> over to <table>
     272                 : **         close cursors
     273                 : **         foreach index on <table>
     274                 : **           open a write cursor on the <table> index
     275                 : **           open a read cursor on the corresponding <table2> index
     276                 : **           transfer all records from the read to the write cursors
     277                 : **           close cursors
     278                 : **         end foreach
     279                 : **
     280                 : ** The third template is for when the second template does not apply
     281                 : ** and the SELECT clause does not read from <table> at any time.
     282                 : ** The generated code follows this template:
     283                 : **
     284                 : **         goto B
     285                 : **      A: setup for the SELECT
     286                 : **         loop over the rows in the SELECT
     287                 : **           gosub C
     288                 : **         end loop
     289                 : **         cleanup after the SELECT
     290                 : **         goto D
     291                 : **      B: open write cursor to <table> and its indices
     292                 : **         goto A
     293                 : **      C: insert the select result into <table>
     294                 : **         return
     295                 : **      D: cleanup
     296                 : **
     297                 : ** The fourth template is used if the insert statement takes its
     298                 : ** values from a SELECT but the data is being inserted into a table
     299                 : ** that is also read as part of the SELECT.  In the third form,
     300                 : ** we have to use a intermediate table to store the results of
     301                 : ** the select.  The template is like this:
     302                 : **
     303                 : **         goto B
     304                 : **      A: setup for the SELECT
     305                 : **         loop over the tables in the SELECT
     306                 : **           gosub C
     307                 : **         end loop
     308                 : **         cleanup after the SELECT
     309                 : **         goto D
     310                 : **      C: insert the select result into the intermediate table
     311                 : **         return
     312                 : **      B: open a cursor to an intermediate table
     313                 : **         goto A
     314                 : **      D: open write cursor to <table> and its indices
     315                 : **         loop over the intermediate table
     316                 : **           transfer values form intermediate table into <table>
     317                 : **         end the loop
     318                 : **         cleanup
     319                 : */
     320                 : void sqlite3Insert(
     321                 :   Parse *pParse,        /* Parser context */
     322                 :   SrcList *pTabList,    /* Name of table into which we are inserting */
     323                 :   ExprList *pList,      /* List of values to be inserted */
     324                 :   Select *pSelect,      /* A SELECT statement to use as the data source */
     325                 :   IdList *pColumn,      /* Column names corresponding to IDLIST. */
     326                 :   int onError           /* How to handle constraint errors */
     327             159 : ){
     328                 :   Table *pTab;          /* The table to insert into */
     329                 :   char *zTab;           /* Name of the table into which we are inserting */
     330                 :   const char *zDb;      /* Name of the database holding this table */
     331                 :   int i, j, idx;        /* Loop counters */
     332                 :   Vdbe *v;              /* Generate code into this virtual machine */
     333                 :   Index *pIdx;          /* For looping over indices of the table */
     334                 :   int nColumn;          /* Number of columns in the data */
     335             159 :   int base = 0;         /* VDBE Cursor number for pTab */
     336             159 :   int iCont=0,iBreak=0; /* Beginning and end of the loop over srcTab */
     337                 :   sqlite3 *db;          /* The main database structure */
     338             159 :   int keyColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
     339                 :   int endOfLoop;        /* Label for the end of the insertion loop */
     340             159 :   int useTempTable = 0; /* Store SELECT results in intermediate table */
     341             159 :   int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
     342             159 :   int iSelectLoop = 0;  /* Address of code that implements the SELECT */
     343             159 :   int iCleanup = 0;     /* Address of the cleanup code */
     344             159 :   int iInsertBlock = 0; /* Address of the subroutine used to insert data */
     345             159 :   int iCntMem = 0;      /* Memory cell used for the row counter */
     346             159 :   int newIdx = -1;      /* Cursor for the NEW table */
     347                 :   Db *pDb;              /* The database containing table being inserted into */
     348             159 :   int counterMem = 0;   /* Memory cell holding AUTOINCREMENT counter */
     349             159 :   int appendFlag = 0;   /* True if the insert is likely to be an append */
     350                 :   int iDb;
     351                 : 
     352                 : #ifndef SQLITE_OMIT_TRIGGER
     353                 :   int isView;                 /* True if attempting to insert into a view */
     354             159 :   int triggers_exist = 0;     /* True if there are FOR EACH ROW triggers */
     355                 : #endif
     356                 : 
     357             159 :   if( pParse->nErr || sqlite3MallocFailed() ){
     358                 :     goto insert_cleanup;
     359                 :   }
     360             159 :   db = pParse->db;
     361                 : 
     362                 :   /* Locate the table into which we will be inserting new information.
     363                 :   */
     364                 :   assert( pTabList->nSrc==1 );
     365             159 :   zTab = pTabList->a[0].zName;
     366             159 :   if( zTab==0 ) goto insert_cleanup;
     367             159 :   pTab = sqlite3SrcListLookup(pParse, pTabList);
     368             159 :   if( pTab==0 ){
     369               0 :     goto insert_cleanup;
     370                 :   }
     371             159 :   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
     372                 :   assert( iDb<db->nDb );
     373             159 :   pDb = &db->aDb[iDb];
     374             159 :   zDb = pDb->zName;
     375             159 :   if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
     376               0 :     goto insert_cleanup;
     377                 :   }
     378                 : 
     379                 :   /* Figure out if we have any triggers and if the table being
     380                 :   ** inserted into is a view
     381                 :   */
     382                 : #ifndef SQLITE_OMIT_TRIGGER
     383             159 :   triggers_exist = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0);
     384             159 :   isView = pTab->pSelect!=0;
     385                 : #else
     386                 : # define triggers_exist 0
     387                 : # define isView 0
     388                 : #endif
     389                 : #ifdef SQLITE_OMIT_VIEW
     390                 : # undef isView
     391                 : # define isView 0
     392                 : #endif
     393                 : 
     394                 :   /* Ensure that:
     395                 :   *  (a) the table is not read-only, 
     396                 :   *  (b) that if it is a view then ON INSERT triggers exist
     397                 :   */
     398             159 :   if( sqlite3IsReadOnly(pParse, pTab, triggers_exist) ){
     399               0 :     goto insert_cleanup;
     400                 :   }
     401                 :   assert( pTab!=0 );
     402                 : 
     403                 :   /* If pTab is really a view, make sure it has been initialized.
     404                 :   ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual 
     405                 :   ** module table).
     406                 :   */
     407             159 :   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
     408               0 :     goto insert_cleanup;
     409                 :   }
     410                 : 
     411                 :   /* Allocate a VDBE
     412                 :   */
     413             159 :   v = sqlite3GetVdbe(pParse);
     414             159 :   if( v==0 ) goto insert_cleanup;
     415             159 :   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
     416             159 :   sqlite3BeginWriteOperation(pParse, pSelect || triggers_exist, iDb);
     417                 : 
     418                 :   /* if there are row triggers, allocate a temp table for new.* references. */
     419             159 :   if( triggers_exist ){
     420               0 :     newIdx = pParse->nTab++;
     421                 :   }
     422                 : 
     423                 : #ifndef SQLITE_OMIT_XFER_OPT
     424                 :   /* If the statement is of the form
     425                 :   **
     426                 :   **       INSERT INTO <table1> SELECT * FROM <table2>;
     427                 :   **
     428                 :   ** Then special optimizations can be applied that make the transfer
     429                 :   ** very fast and which reduce fragmentation of indices.
     430                 :   */
     431             159 :   if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
     432                 :     assert( !triggers_exist );
     433                 :     assert( pList==0 );
     434               0 :     goto insert_cleanup;
     435                 :   }
     436                 : #endif /* SQLITE_OMIT_XFER_OPT */
     437                 : 
     438                 :   /* If this is an AUTOINCREMENT table, look up the sequence number in the
     439                 :   ** sqlite_sequence table and store it in memory cell counterMem.  Also
     440                 :   ** remember the rowid of the sqlite_sequence table entry in memory cell
     441                 :   ** counterRowid.
     442                 :   */
     443             159 :   counterMem = autoIncBegin(pParse, iDb, pTab);
     444                 : 
     445                 :   /* Figure out how many columns of data are supplied.  If the data
     446                 :   ** is coming from a SELECT statement, then this step also generates
     447                 :   ** all the code to implement the SELECT statement and invoke a subroutine
     448                 :   ** to process each row of the result. (Template 2.) If the SELECT
     449                 :   ** statement uses the the table that is being inserted into, then the
     450                 :   ** subroutine is also coded here.  That subroutine stores the SELECT
     451                 :   ** results in a temporary table. (Template 3.)
     452                 :   */
     453             159 :   if( pSelect ){
     454                 :     /* Data is coming from a SELECT.  Generate code to implement that SELECT
     455                 :     */
     456                 :     int rc, iInitCode;
     457               0 :     iInitCode = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
     458               0 :     iSelectLoop = sqlite3VdbeCurrentAddr(v);
     459               0 :     iInsertBlock = sqlite3VdbeMakeLabel(v);
     460                 : 
     461                 :     /* Resolve the expressions in the SELECT statement and execute it. */
     462               0 :     rc = sqlite3Select(pParse, pSelect, SRT_Subroutine, iInsertBlock,0,0,0,0);
     463               0 :     if( rc || pParse->nErr || sqlite3MallocFailed() ){
     464                 :       goto insert_cleanup;
     465                 :     }
     466                 : 
     467               0 :     iCleanup = sqlite3VdbeMakeLabel(v);
     468               0 :     sqlite3VdbeAddOp(v, OP_Goto, 0, iCleanup);
     469                 :     assert( pSelect->pEList );
     470               0 :     nColumn = pSelect->pEList->nExpr;
     471                 : 
     472                 :     /* Set useTempTable to TRUE if the result of the SELECT statement
     473                 :     ** should be written into a temporary table.  Set to FALSE if each
     474                 :     ** row of the SELECT can be written directly into the result table.
     475                 :     **
     476                 :     ** A temp table must be used if the table being updated is also one
     477                 :     ** of the tables being read by the SELECT statement.  Also use a 
     478                 :     ** temp table in the case of row triggers.
     479                 :     */
     480               0 :     if( triggers_exist || selectReadsTable(pSelect,pTab->pSchema,pTab->tnum) ){
     481               0 :       useTempTable = 1;
     482                 :     }
     483                 : 
     484               0 :     if( useTempTable ){
     485                 :       /* Generate the subroutine that SELECT calls to process each row of
     486                 :       ** the result.  Store the result in a temporary table
     487                 :       */
     488               0 :       srcTab = pParse->nTab++;
     489               0 :       sqlite3VdbeResolveLabel(v, iInsertBlock);
     490               0 :       sqlite3VdbeAddOp(v, OP_MakeRecord, nColumn, 0);
     491               0 :       sqlite3VdbeAddOp(v, OP_NewRowid, srcTab, 0);
     492               0 :       sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
     493               0 :       sqlite3VdbeAddOp(v, OP_Insert, srcTab, OPFLAG_APPEND);
     494               0 :       sqlite3VdbeAddOp(v, OP_Return, 0, 0);
     495                 : 
     496                 :       /* The following code runs first because the GOTO at the very top
     497                 :       ** of the program jumps to it.  Create the temporary table, then jump
     498                 :       ** back up and execute the SELECT code above.
     499                 :       */
     500               0 :       sqlite3VdbeJumpHere(v, iInitCode);
     501               0 :       sqlite3VdbeAddOp(v, OP_OpenEphemeral, srcTab, 0);
     502               0 :       sqlite3VdbeAddOp(v, OP_SetNumColumns, srcTab, nColumn);
     503               0 :       sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
     504               0 :       sqlite3VdbeResolveLabel(v, iCleanup);
     505                 :     }else{
     506               0 :       sqlite3VdbeJumpHere(v, iInitCode);
     507                 :     }
     508                 :   }else{
     509                 :     /* This is the case if the data for the INSERT is coming from a VALUES
     510                 :     ** clause
     511                 :     */
     512                 :     NameContext sNC;
     513             159 :     memset(&sNC, 0, sizeof(sNC));
     514             159 :     sNC.pParse = pParse;
     515             159 :     srcTab = -1;
     516             159 :     useTempTable = 0;
     517             159 :     nColumn = pList ? pList->nExpr : 0;
     518             640 :     for(i=0; i<nColumn; i++){
     519             481 :       if( sqlite3ExprResolveNames(&sNC, pList->a[i].pExpr) ){
     520               0 :         goto insert_cleanup;
     521                 :       }
     522                 :     }
     523                 :   }
     524                 : 
     525                 :   /* Make sure the number of columns in the source data matches the number
     526                 :   ** of columns to be inserted into the table.
     527                 :   */
     528             159 :   if( pColumn==0 && nColumn && nColumn!=pTab->nCol ){
     529               0 :     sqlite3ErrorMsg(pParse, 
     530                 :        "table %S has %d columns but %d values were supplied",
     531                 :        pTabList, 0, pTab->nCol, nColumn);
     532               0 :     goto insert_cleanup;
     533                 :   }
     534             159 :   if( pColumn!=0 && nColumn!=pColumn->nId ){
     535               0 :     sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
     536               0 :     goto insert_cleanup;
     537                 :   }
     538                 : 
     539                 :   /* If the INSERT statement included an IDLIST term, then make sure
     540                 :   ** all elements of the IDLIST really are columns of the table and 
     541                 :   ** remember the column indices.
     542                 :   **
     543                 :   ** If the table has an INTEGER PRIMARY KEY column and that column
     544                 :   ** is named in the IDLIST, then record in the keyColumn variable
     545                 :   ** the index into IDLIST of the primary key column.  keyColumn is
     546                 :   ** the index of the primary key as it appears in IDLIST, not as
     547                 :   ** is appears in the original table.  (The index of the primary
     548                 :   ** key in the original table is pTab->iPKey.)
     549                 :   */
     550             159 :   if( pColumn ){
     551              81 :     for(i=0; i<pColumn->nId; i++){
     552              56 :       pColumn->a[i].idx = -1;
     553                 :     }
     554              81 :     for(i=0; i<pColumn->nId; i++){
     555             100 :       for(j=0; j<pTab->nCol; j++){
     556             100 :         if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
     557              56 :           pColumn->a[i].idx = j;
     558              56 :           if( j==pTab->iPKey ){
     559               3 :             keyColumn = i;
     560                 :           }
     561              56 :           break;
     562                 :         }
     563                 :       }
     564              56 :       if( j>=pTab->nCol ){
     565               0 :         if( sqlite3IsRowid(pColumn->a[i].zName) ){
     566               0 :           keyColumn = i;
     567                 :         }else{
     568               0 :           sqlite3ErrorMsg(pParse, "table %S has no column named %s",
     569                 :               pTabList, 0, pColumn->a[i].zName);
     570               0 :           pParse->nErr++;
     571               0 :           goto insert_cleanup;
     572                 :         }
     573                 :       }
     574                 :     }
     575                 :   }
     576                 : 
     577                 :   /* If there is no IDLIST term but the table has an integer primary
     578                 :   ** key, the set the keyColumn variable to the primary key column index
     579                 :   ** in the original table definition.
     580                 :   */
     581             159 :   if( pColumn==0 && nColumn>0 ){
     582             134 :     keyColumn = pTab->iPKey;
     583                 :   }
     584                 : 
     585                 :   /* Open the temp table for FOR EACH ROW triggers
     586                 :   */
     587             159 :   if( triggers_exist ){
     588               0 :     sqlite3VdbeAddOp(v, OP_OpenPseudo, newIdx, 0);
     589               0 :     sqlite3VdbeAddOp(v, OP_SetNumColumns, newIdx, pTab->nCol);
     590                 :   }
     591                 :     
     592                 :   /* Initialize the count of rows to be inserted
     593                 :   */
     594             159 :   if( db->flags & SQLITE_CountRows ){
     595               0 :     iCntMem = pParse->nMem++;
     596               0 :     sqlite3VdbeAddOp(v, OP_MemInt, 0, iCntMem);
     597                 :   }
     598                 : 
     599                 :   /* Open tables and indices if there are no row triggers */
     600             159 :   if( !triggers_exist ){
     601             159 :     base = pParse->nTab;
     602             159 :     sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
     603                 :   }
     604                 : 
     605                 :   /* If the data source is a temporary table, then we have to create
     606                 :   ** a loop because there might be multiple rows of data.  If the data
     607                 :   ** source is a subroutine call from the SELECT statement, then we need
     608                 :   ** to launch the SELECT statement processing.
     609                 :   */
     610             159 :   if( useTempTable ){
     611               0 :     iBreak = sqlite3VdbeMakeLabel(v);
     612               0 :     sqlite3VdbeAddOp(v, OP_Rewind, srcTab, iBreak);
     613               0 :     iCont = sqlite3VdbeCurrentAddr(v);
     614             159 :   }else if( pSelect ){
     615               0 :     sqlite3VdbeAddOp(v, OP_Goto, 0, iSelectLoop);
     616               0 :     sqlite3VdbeResolveLabel(v, iInsertBlock);
     617                 :   }
     618                 : 
     619                 :   /* Run the BEFORE and INSTEAD OF triggers, if there are any
     620                 :   */
     621             159 :   endOfLoop = sqlite3VdbeMakeLabel(v);
     622             159 :   if( triggers_exist & TRIGGER_BEFORE ){
     623                 : 
     624                 :     /* build the NEW.* reference row.  Note that if there is an INTEGER
     625                 :     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
     626                 :     ** translated into a unique ID for the row.  But on a BEFORE trigger,
     627                 :     ** we do not know what the unique ID will be (because the insert has
     628                 :     ** not happened yet) so we substitute a rowid of -1
     629                 :     */
     630               0 :     if( keyColumn<0 ){
     631               0 :       sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
     632               0 :     }else if( useTempTable ){
     633               0 :       sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
     634                 :     }else{
     635                 :       assert( pSelect==0 );  /* Otherwise useTempTable is true */
     636               0 :       sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
     637               0 :       sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
     638               0 :       sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
     639               0 :       sqlite3VdbeAddOp(v, OP_Integer, -1, 0);
     640               0 :       sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
     641                 :     }
     642                 : 
     643                 :     /* Create the new column data
     644                 :     */
     645               0 :     for(i=0; i<pTab->nCol; i++){
     646               0 :       if( pColumn==0 ){
     647               0 :         j = i;
     648                 :       }else{
     649               0 :         for(j=0; j<pColumn->nId; j++){
     650               0 :           if( pColumn->a[j].idx==i ) break;
     651                 :         }
     652                 :       }
     653               0 :       if( pColumn && j>=pColumn->nId ){
     654               0 :         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
     655               0 :       }else if( useTempTable ){
     656               0 :         sqlite3VdbeAddOp(v, OP_Column, srcTab, j); 
     657                 :       }else{
     658                 :         assert( pSelect==0 ); /* Otherwise useTempTable is true */
     659               0 :         sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr);
     660                 :       }
     661                 :     }
     662               0 :     sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
     663                 : 
     664                 :     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
     665                 :     ** do not attempt any conversions before assembling the record.
     666                 :     ** If this is a real table, attempt conversions as required by the
     667                 :     ** table column affinities.
     668                 :     */
     669               0 :     if( !isView ){
     670               0 :       sqlite3TableAffinityStr(v, pTab);
     671                 :     }
     672               0 :     sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
     673                 : 
     674                 :     /* Fire BEFORE or INSTEAD OF triggers */
     675               0 :     if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_BEFORE, pTab, 
     676                 :         newIdx, -1, onError, endOfLoop) ){
     677               0 :       goto insert_cleanup;
     678                 :     }
     679                 :   }
     680                 : 
     681                 :   /* If any triggers exists, the opening of tables and indices is deferred
     682                 :   ** until now.
     683                 :   */
     684             159 :   if( triggers_exist && !isView ){
     685               0 :     base = pParse->nTab;
     686               0 :     sqlite3OpenTableAndIndices(pParse, pTab, base, OP_OpenWrite);
     687                 :   }
     688                 : 
     689                 :   /* Push the record number for the new entry onto the stack.  The
     690                 :   ** record number is a randomly generate integer created by NewRowid
     691                 :   ** except when the table has an INTEGER PRIMARY KEY column, in which
     692                 :   ** case the record number is the same as that column. 
     693                 :   */
     694             159 :   if( !isView ){
     695             159 :     if( IsVirtual(pTab) ){
     696                 :       /* The row that the VUpdate opcode will delete:  none */
     697               0 :       sqlite3VdbeAddOp(v, OP_Null, 0, 0);
     698                 :     }
     699             159 :     if( keyColumn>=0 ){
     700               3 :       if( useTempTable ){
     701               0 :         sqlite3VdbeAddOp(v, OP_Column, srcTab, keyColumn);
     702               3 :       }else if( pSelect ){
     703               0 :         sqlite3VdbeAddOp(v, OP_Dup, nColumn - keyColumn - 1, 1);
     704                 :       }else{
     705                 :         VdbeOp *pOp;
     706               3 :         sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr);
     707               3 :         pOp = sqlite3VdbeGetOp(v, sqlite3VdbeCurrentAddr(v) - 1);
     708               3 :         if( pOp && pOp->opcode==OP_Null ){
     709               0 :           appendFlag = 1;
     710               0 :           pOp->opcode = OP_NewRowid;
     711               0 :           pOp->p1 = base;
     712               0 :           pOp->p2 = counterMem;
     713                 :         }
     714                 :       }
     715                 :       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
     716                 :       ** to generate a unique primary key value.
     717                 :       */
     718               3 :       if( !appendFlag ){
     719               3 :         sqlite3VdbeAddOp(v, OP_NotNull, -1, sqlite3VdbeCurrentAddr(v)+3);
     720               3 :         sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
     721               3 :         sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
     722               3 :         sqlite3VdbeAddOp(v, OP_MustBeInt, 0, 0);
     723                 :       }
     724             156 :     }else if( IsVirtual(pTab) ){
     725               0 :       sqlite3VdbeAddOp(v, OP_Null, 0, 0);
     726                 :     }else{
     727             156 :       sqlite3VdbeAddOp(v, OP_NewRowid, base, counterMem);
     728             156 :       appendFlag = 1;
     729                 :     }
     730             159 :     autoIncStep(pParse, counterMem);
     731                 : 
     732                 :     /* Push onto the stack, data for all columns of the new entry, beginning
     733                 :     ** with the first column.
     734                 :     */
     735             641 :     for(i=0; i<pTab->nCol; i++){
     736             482 :       if( i==pTab->iPKey ){
     737                 :         /* The value of the INTEGER PRIMARY KEY column is always a NULL.
     738                 :         ** Whenever this column is read, the record number will be substituted
     739                 :         ** in its place.  So will fill this column with a NULL to avoid
     740                 :         ** taking up data space with information that will never be used. */
     741               4 :         sqlite3VdbeAddOp(v, OP_Null, 0, 0);
     742               4 :         continue;
     743                 :       }
     744             478 :       if( pColumn==0 ){
     745             425 :         j = i;
     746                 :       }else{
     747              96 :         for(j=0; j<pColumn->nId; j++){
     748              96 :           if( pColumn->a[j].idx==i ) break;
     749                 :         }
     750                 :       }
     751             478 :       if( nColumn==0 || (pColumn && j>=pColumn->nId) ){
     752               0 :         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
     753             478 :       }else if( useTempTable ){
     754               0 :         sqlite3VdbeAddOp(v, OP_Column, srcTab, j); 
     755             478 :       }else if( pSelect ){
     756               0 :         sqlite3VdbeAddOp(v, OP_Dup, i+nColumn-j+IsVirtual(pTab), 1);
     757                 :       }else{
     758             478 :         sqlite3ExprCode(pParse, pList->a[j].pExpr);
     759                 :       }
     760                 :     }
     761                 : 
     762                 :     /* Generate code to check constraints and generate index keys and
     763                 :     ** do the insertion.
     764                 :     */
     765                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
     766             159 :     if( IsVirtual(pTab) ){
     767               0 :       pParse->pVirtualLock = pTab;
     768               0 :       sqlite3VdbeOp3(v, OP_VUpdate, 1, pTab->nCol+2,
     769                 :                      (const char*)pTab->pVtab, P3_VTAB);
     770                 :     }else
     771                 : #endif
     772                 :     {
     773             159 :       sqlite3GenerateConstraintChecks(pParse, pTab, base, 0, keyColumn>=0,
     774                 :                                      0, onError, endOfLoop);
     775             159 :       sqlite3CompleteInsertion(pParse, pTab, base, 0,0,0,
     776                 :                             (triggers_exist & TRIGGER_AFTER)!=0 ? newIdx : -1,
     777                 :                             appendFlag);
     778                 :     }
     779                 :   }
     780                 : 
     781                 :   /* Update the count of rows that are inserted
     782                 :   */
     783             159 :   if( (db->flags & SQLITE_CountRows)!=0 ){
     784               0 :     sqlite3VdbeAddOp(v, OP_MemIncr, 1, iCntMem);
     785                 :   }
     786                 : 
     787             159 :   if( triggers_exist ){
     788                 :     /* Close all tables opened */
     789               0 :     if( !isView ){
     790               0 :       sqlite3VdbeAddOp(v, OP_Close, base, 0);
     791               0 :       for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
     792               0 :         sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
     793                 :       }
     794                 :     }
     795                 : 
     796                 :     /* Code AFTER triggers */
     797               0 :     if( sqlite3CodeRowTrigger(pParse, TK_INSERT, 0, TRIGGER_AFTER, pTab,
     798                 :           newIdx, -1, onError, endOfLoop) ){
     799               0 :       goto insert_cleanup;
     800                 :     }
     801                 :   }
     802                 : 
     803                 :   /* The bottom of the loop, if the data source is a SELECT statement
     804                 :   */
     805             159 :   sqlite3VdbeResolveLabel(v, endOfLoop);
     806             159 :   if( useTempTable ){
     807               0 :     sqlite3VdbeAddOp(v, OP_Next, srcTab, iCont);
     808               0 :     sqlite3VdbeResolveLabel(v, iBreak);
     809               0 :     sqlite3VdbeAddOp(v, OP_Close, srcTab, 0);
     810             159 :   }else if( pSelect ){
     811               0 :     sqlite3VdbeAddOp(v, OP_Pop, nColumn, 0);
     812               0 :     sqlite3VdbeAddOp(v, OP_Return, 0, 0);
     813               0 :     sqlite3VdbeResolveLabel(v, iCleanup);
     814                 :   }
     815                 : 
     816             159 :   if( !triggers_exist && !IsVirtual(pTab) ){
     817                 :     /* Close all tables opened */
     818             159 :     sqlite3VdbeAddOp(v, OP_Close, base, 0);
     819             254 :     for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
     820              95 :       sqlite3VdbeAddOp(v, OP_Close, idx+base, 0);
     821                 :     }
     822                 :   }
     823                 : 
     824                 :   /* Update the sqlite_sequence table by storing the content of the
     825                 :   ** counter value in memory counterMem back into the sqlite_sequence
     826                 :   ** table.
     827                 :   */
     828             159 :   autoIncEnd(pParse, iDb, pTab, counterMem);
     829                 : 
     830                 :   /*
     831                 :   ** Return the number of rows inserted. If this routine is 
     832                 :   ** generating code because of a call to sqlite3NestedParse(), do not
     833                 :   ** invoke the callback function.
     834                 :   */
     835             159 :   if( db->flags & SQLITE_CountRows && pParse->nested==0 && !pParse->trigStack ){
     836               0 :     sqlite3VdbeAddOp(v, OP_MemLoad, iCntMem, 0);
     837               0 :     sqlite3VdbeAddOp(v, OP_Callback, 1, 0);
     838               0 :     sqlite3VdbeSetNumCols(v, 1);
     839               0 :     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", P3_STATIC);
     840                 :   }
     841                 : 
     842             159 : insert_cleanup:
     843             159 :   sqlite3SrcListDelete(pTabList);
     844             159 :   sqlite3ExprListDelete(pList);
     845             159 :   sqlite3SelectDelete(pSelect);
     846             159 :   sqlite3IdListDelete(pColumn);
     847             159 : }
     848                 : 
     849                 : /*
     850                 : ** Generate code to do a constraint check prior to an INSERT or an UPDATE.
     851                 : **
     852                 : ** When this routine is called, the stack contains (from bottom to top)
     853                 : ** the following values:
     854                 : **
     855                 : **    1.  The rowid of the row to be updated before the update.  This
     856                 : **        value is omitted unless we are doing an UPDATE that involves a
     857                 : **        change to the record number.
     858                 : **
     859                 : **    2.  The rowid of the row after the update.
     860                 : **
     861                 : **    3.  The data in the first column of the entry after the update.
     862                 : **
     863                 : **    i.  Data from middle columns...
     864                 : **
     865                 : **    N.  The data in the last column of the entry after the update.
     866                 : **
     867                 : ** The old rowid shown as entry (1) above is omitted unless both isUpdate
     868                 : ** and rowidChng are 1.  isUpdate is true for UPDATEs and false for
     869                 : ** INSERTs and rowidChng is true if the record number is being changed.
     870                 : **
     871                 : ** The code generated by this routine pushes additional entries onto
     872                 : ** the stack which are the keys for new index entries for the new record.
     873                 : ** The order of index keys is the same as the order of the indices on
     874                 : ** the pTable->pIndex list.  A key is only created for index i if 
     875                 : ** aIdxUsed!=0 and aIdxUsed[i]!=0.
     876                 : **
     877                 : ** This routine also generates code to check constraints.  NOT NULL,
     878                 : ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
     879                 : ** then the appropriate action is performed.  There are five possible
     880                 : ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
     881                 : **
     882                 : **  Constraint type  Action       What Happens
     883                 : **  ---------------  ----------   ----------------------------------------
     884                 : **  any              ROLLBACK     The current transaction is rolled back and
     885                 : **                                sqlite3_exec() returns immediately with a
     886                 : **                                return code of SQLITE_CONSTRAINT.
     887                 : **
     888                 : **  any              ABORT        Back out changes from the current command
     889                 : **                                only (do not do a complete rollback) then
     890                 : **                                cause sqlite3_exec() to return immediately
     891                 : **                                with SQLITE_CONSTRAINT.
     892                 : **
     893                 : **  any              FAIL         Sqlite_exec() returns immediately with a
     894                 : **                                return code of SQLITE_CONSTRAINT.  The
     895                 : **                                transaction is not rolled back and any
     896                 : **                                prior changes are retained.
     897                 : **
     898                 : **  any              IGNORE       The record number and data is popped from
     899                 : **                                the stack and there is an immediate jump
     900                 : **                                to label ignoreDest.
     901                 : **
     902                 : **  NOT NULL         REPLACE      The NULL value is replace by the default
     903                 : **                                value for that column.  If the default value
     904                 : **                                is NULL, the action is the same as ABORT.
     905                 : **
     906                 : **  UNIQUE           REPLACE      The other row that conflicts with the row
     907                 : **                                being inserted is removed.
     908                 : **
     909                 : **  CHECK            REPLACE      Illegal.  The results in an exception.
     910                 : **
     911                 : ** Which action to take is determined by the overrideError parameter.
     912                 : ** Or if overrideError==OE_Default, then the pParse->onError parameter
     913                 : ** is used.  Or if pParse->onError==OE_Default then the onError value
     914                 : ** for the constraint is used.
     915                 : **
     916                 : ** The calling routine must open a read/write cursor for pTab with
     917                 : ** cursor number "base".  All indices of pTab must also have open
     918                 : ** read/write cursors with cursor number base+i for the i-th cursor.
     919                 : ** Except, if there is no possibility of a REPLACE action then
     920                 : ** cursors do not need to be open for indices where aIdxUsed[i]==0.
     921                 : **
     922                 : ** If the isUpdate flag is true, it means that the "base" cursor is
     923                 : ** initially pointing to an entry that is being updated.  The isUpdate
     924                 : ** flag causes extra code to be generated so that the "base" cursor
     925                 : ** is still pointing at the same entry after the routine returns.
     926                 : ** Without the isUpdate flag, the "base" cursor might be moved.
     927                 : */
     928                 : void sqlite3GenerateConstraintChecks(
     929                 :   Parse *pParse,      /* The parser context */
     930                 :   Table *pTab,        /* the table into which we are inserting */
     931                 :   int base,           /* Index of a read/write cursor pointing at pTab */
     932                 :   char *aIdxUsed,     /* Which indices are used.  NULL means all are used */
     933                 :   int rowidChng,      /* True if the record number will change */
     934                 :   int isUpdate,       /* True for UPDATE, False for INSERT */
     935                 :   int overrideError,  /* Override onError to this if not OE_Default */
     936                 :   int ignoreDest      /* Jump to this label on an OE_Ignore resolution */
     937             222 : ){
     938                 :   int i;
     939                 :   Vdbe *v;
     940                 :   int nCol;
     941                 :   int onError;
     942                 :   int addr;
     943                 :   int extra;
     944                 :   int iCur;
     945                 :   Index *pIdx;
     946             222 :   int seenReplace = 0;
     947             222 :   int jumpInst1=0, jumpInst2;
     948             222 :   int hasTwoRowids = (isUpdate && rowidChng);
     949                 : 
     950             222 :   v = sqlite3GetVdbe(pParse);
     951                 :   assert( v!=0 );
     952                 :   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
     953             222 :   nCol = pTab->nCol;
     954                 : 
     955                 :   /* Test all NOT NULL constraints.
     956                 :   */
     957            1015 :   for(i=0; i<nCol; i++){
     958             793 :     if( i==pTab->iPKey ){
     959               4 :       continue;
     960                 :     }
     961             789 :     onError = pTab->aCol[i].notNull;
     962             789 :     if( onError==OE_None ) continue;
     963              96 :     if( overrideError!=OE_Default ){
     964               0 :       onError = overrideError;
     965              96 :     }else if( onError==OE_Default ){
     966              96 :       onError = OE_Abort;
     967                 :     }
     968              96 :     if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
     969               0 :       onError = OE_Abort;
     970                 :     }
     971              96 :     sqlite3VdbeAddOp(v, OP_Dup, nCol-1-i, 1);
     972              96 :     addr = sqlite3VdbeAddOp(v, OP_NotNull, 1, 0);
     973                 :     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
     974                 :         || onError==OE_Ignore || onError==OE_Replace );
     975              96 :     switch( onError ){
     976                 :       case OE_Rollback:
     977                 :       case OE_Abort:
     978                 :       case OE_Fail: {
     979              96 :         char *zMsg = 0;
     980              96 :         sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
     981              96 :         sqlite3SetString(&zMsg, pTab->zName, ".", pTab->aCol[i].zName,
     982                 :                         " may not be NULL", (char*)0);
     983              96 :         sqlite3VdbeChangeP3(v, -1, zMsg, P3_DYNAMIC);
     984              96 :         break;
     985                 :       }
     986                 :       case OE_Ignore: {
     987               0 :         sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
     988               0 :         sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
     989               0 :         break;
     990                 :       }
     991                 :       case OE_Replace: {
     992               0 :         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt);
     993               0 :         sqlite3VdbeAddOp(v, OP_Push, nCol-i, 0);
     994                 :         break;
     995                 :       }
     996                 :     }
     997              96 :     sqlite3VdbeJumpHere(v, addr);
     998                 :   }
     999                 : 
    1000                 :   /* Test all CHECK constraints
    1001                 :   */
    1002                 : #ifndef SQLITE_OMIT_CHECK
    1003             222 :   if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){
    1004               0 :     int allOk = sqlite3VdbeMakeLabel(v);
    1005                 :     assert( pParse->ckOffset==0 );
    1006               0 :     pParse->ckOffset = nCol;
    1007               0 :     sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, 1);
    1008                 :     assert( pParse->ckOffset==nCol );
    1009               0 :     pParse->ckOffset = 0;
    1010               0 :     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
    1011               0 :     if( onError==OE_Ignore || onError==OE_Replace ){
    1012               0 :       sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
    1013               0 :       sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
    1014                 :     }else{
    1015               0 :       sqlite3VdbeAddOp(v, OP_Halt, SQLITE_CONSTRAINT, onError);
    1016                 :     }
    1017               0 :     sqlite3VdbeResolveLabel(v, allOk);
    1018                 :   }
    1019                 : #endif /* !defined(SQLITE_OMIT_CHECK) */
    1020                 : 
    1021                 :   /* If we have an INTEGER PRIMARY KEY, make sure the primary key
    1022                 :   ** of the new record does not previously exist.  Except, if this
    1023                 :   ** is an UPDATE and the primary key is not changing, that is OK.
    1024                 :   */
    1025             222 :   if( rowidChng ){
    1026               3 :     onError = pTab->keyConf;
    1027               3 :     if( overrideError!=OE_Default ){
    1028               0 :       onError = overrideError;
    1029               3 :     }else if( onError==OE_Default ){
    1030               3 :       onError = OE_Abort;
    1031                 :     }
    1032                 :     
    1033               3 :     if( isUpdate ){
    1034               0 :       sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
    1035               0 :       sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
    1036               0 :       jumpInst1 = sqlite3VdbeAddOp(v, OP_Eq, 0, 0);
    1037                 :     }
    1038               3 :     sqlite3VdbeAddOp(v, OP_Dup, nCol, 1);
    1039               3 :     jumpInst2 = sqlite3VdbeAddOp(v, OP_NotExists, base, 0);
    1040               3 :     switch( onError ){
    1041                 :       default: {
    1042               0 :         onError = OE_Abort;
    1043                 :         /* Fall thru into the next case */
    1044                 :       }
    1045                 :       case OE_Rollback:
    1046                 :       case OE_Abort:
    1047                 :       case OE_Fail: {
    1048               3 :         sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError,
    1049                 :                          "PRIMARY KEY must be unique", P3_STATIC);
    1050               3 :         break;
    1051                 :       }
    1052                 :       case OE_Replace: {
    1053               0 :         sqlite3GenerateRowIndexDelete(v, pTab, base, 0);
    1054               0 :         if( isUpdate ){
    1055               0 :           sqlite3VdbeAddOp(v, OP_Dup, nCol+hasTwoRowids, 1);
    1056               0 :           sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
    1057                 :         }
    1058               0 :         seenReplace = 1;
    1059               0 :         break;
    1060                 :       }
    1061                 :       case OE_Ignore: {
    1062                 :         assert( seenReplace==0 );
    1063               0 :         sqlite3VdbeAddOp(v, OP_Pop, nCol+1+hasTwoRowids, 0);
    1064               0 :         sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
    1065                 :         break;
    1066                 :       }
    1067                 :     }
    1068               3 :     sqlite3VdbeJumpHere(v, jumpInst2);
    1069               3 :     if( isUpdate ){
    1070               0 :       sqlite3VdbeJumpHere(v, jumpInst1);
    1071               0 :       sqlite3VdbeAddOp(v, OP_Dup, nCol+1, 1);
    1072               0 :       sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
    1073                 :     }
    1074                 :   }
    1075                 : 
    1076                 :   /* Test all UNIQUE constraints by creating entries for each UNIQUE
    1077                 :   ** index and making sure that duplicate entries do not already exist.
    1078                 :   ** Add the new records to the indices as we go.
    1079                 :   */
    1080             222 :   extra = -1;
    1081             317 :   for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
    1082              95 :     if( aIdxUsed && aIdxUsed[iCur]==0 ) continue;  /* Skip unused indices */
    1083              95 :     extra++;
    1084                 : 
    1085                 :     /* Create a key for accessing the index entry */
    1086              95 :     sqlite3VdbeAddOp(v, OP_Dup, nCol+extra, 1);
    1087             190 :     for(i=0; i<pIdx->nColumn; i++){
    1088              95 :       int idx = pIdx->aiColumn[i];
    1089              95 :       if( idx==pTab->iPKey ){
    1090               0 :         sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol+1, 1);
    1091                 :       }else{
    1092              95 :         sqlite3VdbeAddOp(v, OP_Dup, i+extra+nCol-idx, 1);
    1093                 :       }
    1094                 :     }
    1095              95 :     jumpInst1 = sqlite3VdbeAddOp(v, OP_MakeIdxRec, pIdx->nColumn, 0);
    1096              95 :     sqlite3IndexAffinityStr(v, pIdx);
    1097                 : 
    1098                 :     /* Find out what action to take in case there is an indexing conflict */
    1099              95 :     onError = pIdx->onError;
    1100              95 :     if( onError==OE_None ) continue;  /* pIdx is not a UNIQUE index */
    1101              95 :     if( overrideError!=OE_Default ){
    1102               0 :       onError = overrideError;
    1103              95 :     }else if( onError==OE_Default ){
    1104              95 :       onError = OE_Abort;
    1105                 :     }
    1106              95 :     if( seenReplace ){
    1107               0 :       if( onError==OE_Ignore ) onError = OE_Replace;
    1108               0 :       else if( onError==OE_Fail ) onError = OE_Abort;
    1109                 :     }
    1110                 :     
    1111                 : 
    1112                 :     /* Check to see if the new index entry will be unique */
    1113              95 :     sqlite3VdbeAddOp(v, OP_Dup, extra+nCol+1+hasTwoRowids, 1);
    1114              95 :     jumpInst2 = sqlite3VdbeAddOp(v, OP_IsUnique, base+iCur+1, 0);
    1115                 : 
    1116                 :     /* Generate code that executes if the new index entry is not unique */
    1117                 :     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
    1118                 :         || onError==OE_Ignore || onError==OE_Replace );
    1119              95 :     switch( onError ){
    1120                 :       case OE_Rollback:
    1121                 :       case OE_Abort:
    1122                 :       case OE_Fail: {
    1123                 :         int j, n1, n2;
    1124                 :         char zErrMsg[200];
    1125              95 :         strcpy(zErrMsg, pIdx->nColumn>1 ? "columns " : "column ");
    1126              95 :         n1 = strlen(zErrMsg);
    1127             190 :         for(j=0; j<pIdx->nColumn && n1<sizeof(zErrMsg)-30; j++){
    1128              95 :           char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
    1129              95 :           n2 = strlen(zCol);
    1130              95 :           if( j>0 ){
    1131               0 :             strcpy(&zErrMsg[n1], ", ");
    1132               0 :             n1 += 2;
    1133                 :           }
    1134              95 :           if( n1+n2>sizeof(zErrMsg)-30 ){
    1135               0 :             strcpy(&zErrMsg[n1], "...");
    1136               0 :             n1 += 3;
    1137               0 :             break;
    1138                 :           }else{
    1139              95 :             strcpy(&zErrMsg[n1], zCol);
    1140              95 :             n1 += n2;
    1141                 :           }
    1142                 :         }
    1143              95 :         strcpy(&zErrMsg[n1], 
    1144                 :             pIdx->nColumn>1 ? " are not unique" : " is not unique");
    1145              95 :         sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, zErrMsg, 0);
    1146              95 :         break;
    1147                 :       }
    1148                 :       case OE_Ignore: {
    1149                 :         assert( seenReplace==0 );
    1150               0 :         sqlite3VdbeAddOp(v, OP_Pop, nCol+extra+3+hasTwoRowids, 0);
    1151               0 :         sqlite3VdbeAddOp(v, OP_Goto, 0, ignoreDest);
    1152               0 :         break;
    1153                 :       }
    1154                 :       case OE_Replace: {
    1155               0 :         sqlite3GenerateRowDelete(pParse->db, v, pTab, base, 0);
    1156               0 :         if( isUpdate ){
    1157               0 :           sqlite3VdbeAddOp(v, OP_Dup, nCol+extra+1+hasTwoRowids, 1);
    1158               0 :           sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
    1159                 :         }
    1160               0 :         seenReplace = 1;
    1161                 :         break;
    1162                 :       }
    1163                 :     }
    1164                 : #if NULL_DISTINCT_FOR_UNIQUE
    1165              95 :     sqlite3VdbeJumpHere(v, jumpInst1);
    1166                 : #endif
    1167              95 :     sqlite3VdbeJumpHere(v, jumpInst2);
    1168                 :   }
    1169             222 : }
    1170                 : 
    1171                 : /*
    1172                 : ** This routine generates code to finish the INSERT or UPDATE operation
    1173                 : ** that was started by a prior call to sqlite3GenerateConstraintChecks.
    1174                 : ** The stack must contain keys for all active indices followed by data
    1175                 : ** and the rowid for the new entry.  This routine creates the new
    1176                 : ** entries in all indices and in the main table.
    1177                 : **
    1178                 : ** The arguments to this routine should be the same as the first six
    1179                 : ** arguments to sqlite3GenerateConstraintChecks.
    1180                 : */
    1181                 : void sqlite3CompleteInsertion(
    1182                 :   Parse *pParse,      /* The parser context */
    1183                 :   Table *pTab,        /* the table into which we are inserting */
    1184                 :   int base,           /* Index of a read/write cursor pointing at pTab */
    1185                 :   char *aIdxUsed,     /* Which indices are used.  NULL means all are used */
    1186                 :   int rowidChng,      /* True if the record number will change */
    1187                 :   int isUpdate,       /* True for UPDATE, False for INSERT */
    1188                 :   int newIdx,         /* Index of NEW table for triggers.  -1 if none */
    1189                 :   int appendBias      /* True if this is likely to be an append */
    1190             222 : ){
    1191                 :   int i;
    1192                 :   Vdbe *v;
    1193                 :   int nIdx;
    1194                 :   Index *pIdx;
    1195                 :   int pik_flags;
    1196                 : 
    1197             222 :   v = sqlite3GetVdbe(pParse);
    1198                 :   assert( v!=0 );
    1199                 :   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
    1200             222 :   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
    1201             317 :   for(i=nIdx-1; i>=0; i--){
    1202              95 :     if( aIdxUsed && aIdxUsed[i]==0 ) continue;
    1203              95 :     sqlite3VdbeAddOp(v, OP_IdxInsert, base+i+1, 0);
    1204                 :   }
    1205             222 :   sqlite3VdbeAddOp(v, OP_MakeRecord, pTab->nCol, 0);
    1206             222 :   sqlite3TableAffinityStr(v, pTab);
    1207                 : #ifndef SQLITE_OMIT_TRIGGER
    1208             222 :   if( newIdx>=0 ){
    1209               0 :     sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
    1210               0 :     sqlite3VdbeAddOp(v, OP_Dup, 1, 0);
    1211               0 :     sqlite3VdbeAddOp(v, OP_Insert, newIdx, 0);
    1212                 :   }
    1213                 : #endif
    1214             222 :   if( pParse->nested ){
    1215             101 :     pik_flags = 0;
    1216                 :   }else{
    1217             121 :     pik_flags = OPFLAG_NCHANGE;
    1218             121 :     pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
    1219                 :   }
    1220             222 :   if( appendBias ){
    1221             156 :     pik_flags |= OPFLAG_APPEND;
    1222                 :   }
    1223             222 :   sqlite3VdbeAddOp(v, OP_Insert, base, pik_flags);
    1224             222 :   if( !pParse->nested ){
    1225             121 :     sqlite3VdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
    1226                 :   }
    1227                 :   
    1228             222 :   if( isUpdate && rowidChng ){
    1229               0 :     sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
    1230                 :   }
    1231             222 : }
    1232                 : 
    1233                 : /*
    1234                 : ** Generate code that will open cursors for a table and for all
    1235                 : ** indices of that table.  The "base" parameter is the cursor number used
    1236                 : ** for the table.  Indices are opened on subsequent cursors.
    1237                 : */
    1238                 : void sqlite3OpenTableAndIndices(
    1239                 :   Parse *pParse,   /* Parsing context */
    1240                 :   Table *pTab,     /* Table to be opened */
    1241                 :   int base,        /* Cursor number assigned to the table */
    1242                 :   int op           /* OP_OpenRead or OP_OpenWrite */
    1243             164 : ){
    1244                 :   int i;
    1245                 :   int iDb;
    1246                 :   Index *pIdx;
    1247                 :   Vdbe *v;
    1248                 : 
    1249             164 :   if( IsVirtual(pTab) ) return;
    1250             164 :   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
    1251             164 :   v = sqlite3GetVdbe(pParse);
    1252                 :   assert( v!=0 );
    1253             164 :   sqlite3OpenTable(pParse, base, iDb, pTab, op);
    1254             259 :   for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
    1255              95 :     KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
    1256                 :     assert( pIdx->pSchema==pTab->pSchema );
    1257              95 :     sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
    1258                 :     VdbeComment((v, "# %s", pIdx->zName));
    1259              95 :     sqlite3VdbeOp3(v, op, i+base, pIdx->tnum, (char*)pKey, P3_KEYINFO_HANDOFF);
    1260                 :   }
    1261             164 :   if( pParse->nTab<=base+i ){
    1262             164 :     pParse->nTab = base+i;
    1263                 :   }
    1264                 : }
    1265                 : 
    1266                 : 
    1267                 : #ifdef SQLITE_TEST
    1268                 : /*
    1269                 : ** The following global variable is incremented whenever the
    1270                 : ** transfer optimization is used.  This is used for testing
    1271                 : ** purposes only - to make sure the transfer optimization really
    1272                 : ** is happening when it is suppose to.
    1273                 : */
    1274                 : int sqlite3_xferopt_count;
    1275                 : #endif /* SQLITE_TEST */
    1276                 : 
    1277                 : 
    1278                 : #ifndef SQLITE_OMIT_XFER_OPT
    1279                 : /*
    1280                 : ** Check to collation names to see if they are compatible.
    1281                 : */
    1282               0 : static int xferCompatibleCollation(const char *z1, const char *z2){
    1283               0 :   if( z1==0 ){
    1284               0 :     return z2==0;
    1285                 :   }
    1286               0 :   if( z2==0 ){
    1287               0 :     return 0;
    1288                 :   }
    1289               0 :   return sqlite3StrICmp(z1, z2)==0;
    1290                 : }
    1291                 : 
    1292                 : 
    1293                 : /*
    1294                 : ** Check to see if index pSrc is compatible as a source of data
    1295                 : ** for index pDest in an insert transfer optimization.  The rules
    1296                 : ** for a compatible index:
    1297                 : **
    1298                 : **    *   The index is over the same set of columns
    1299                 : **    *   The same DESC and ASC markings occurs on all columns
    1300                 : **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
    1301                 : **    *   The same collating sequence on each column
    1302                 : */
    1303               0 : static int xferCompatibleIndex(Index *pDest, Index *pSrc){
    1304                 :   int i;
    1305                 :   assert( pDest && pSrc );
    1306                 :   assert( pDest->pTable!=pSrc->pTable );
    1307               0 :   if( pDest->nColumn!=pSrc->nColumn ){
    1308               0 :     return 0;   /* Different number of columns */
    1309                 :   }
    1310               0 :   if( pDest->onError!=pSrc->onError ){
    1311               0 :     return 0;   /* Different conflict resolution strategies */
    1312                 :   }
    1313               0 :   for(i=0; i<pSrc->nColumn; i++){
    1314               0 :     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
    1315               0 :       return 0;   /* Different columns indexed */
    1316                 :     }
    1317               0 :     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
    1318               0 :       return 0;   /* Different sort orders */
    1319                 :     }
    1320               0 :     if( pSrc->azColl[i]!=pDest->azColl[i] ){
    1321               0 :       return 0;   /* Different sort orders */
    1322                 :     }
    1323                 :   }
    1324                 : 
    1325                 :   /* If no test above fails then the indices must be compatible */
    1326               0 :   return 1;
    1327                 : }
    1328                 : 
    1329                 : /*
    1330                 : ** Attempt the transfer optimization on INSERTs of the form
    1331                 : **
    1332                 : **     INSERT INTO tab1 SELECT * FROM tab2;
    1333                 : **
    1334                 : ** This optimization is only attempted if
    1335                 : **
    1336                 : **    (1)  tab1 and tab2 have identical schemas including all the
    1337                 : **         same indices and constraints
    1338                 : **
    1339                 : **    (2)  tab1 and tab2 are different tables
    1340                 : **
    1341                 : **    (3)  There must be no triggers on tab1
    1342                 : **
    1343                 : **    (4)  The result set of the SELECT statement is "*"
    1344                 : **
    1345                 : **    (5)  The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
    1346                 : **         or LIMIT clause.
    1347                 : **
    1348                 : **    (6)  The SELECT statement is a simple (not a compound) select that
    1349                 : **         contains only tab2 in its FROM clause
    1350                 : **
    1351                 : ** This method for implementing the INSERT transfers raw records from
    1352                 : ** tab2 over to tab1.  The columns are not decoded.  Raw records from
    1353                 : ** the indices of tab2 are transfered to tab1 as well.  In so doing,
    1354                 : ** the resulting tab1 has much less fragmentation.
    1355                 : **
    1356                 : ** This routine returns TRUE if the optimization is attempted.  If any
    1357                 : ** of the conditions above fail so that the optimization should not
    1358                 : ** be attempted, then this routine returns FALSE.
    1359                 : */
    1360                 : static int xferOptimization(
    1361                 :   Parse *pParse,        /* Parser context */
    1362                 :   Table *pDest,         /* The table we are inserting into */
    1363                 :   Select *pSelect,      /* A SELECT statement to use as the data source */
    1364                 :   int onError,          /* How to handle constraint errors */
    1365                 :   int iDbDest           /* The database of pDest */
    1366             134 : ){
    1367                 :   ExprList *pEList;                /* The result set of the SELECT */
    1368                 :   Table *pSrc;                     /* The table in the FROM clause of SELECT */
    1369                 :   Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
    1370                 :   struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
    1371                 :   int i;                           /* Loop counter */
    1372                 :   int iDbSrc;                      /* The database of pSrc */
    1373                 :   int iSrc, iDest;                 /* Cursors from source and destination */
    1374                 :   int addr1, addr2;                /* Loop addresses */
    1375                 :   int emptyDestTest;               /* Address of test for empty pDest */
    1376                 :   int emptySrcTest;                /* Address of test for empty pSrc */
    1377                 :   Vdbe *v;                         /* The VDBE we are building */
    1378                 :   KeyInfo *pKey;                   /* Key information for an index */
    1379                 :   int counterMem;                  /* Memory register used by AUTOINC */
    1380             134 :   int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
    1381                 : 
    1382             134 :   if( pSelect==0 ){
    1383             134 :     return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
    1384                 :   }
    1385               0 :   if( pDest->pTrigger ){
    1386               0 :     return 0;   /* tab1 must not have triggers */
    1387                 :   }
    1388                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
    1389               0 :   if( pDest->isVirtual ){
    1390               0 :     return 0;   /* tab1 must not be a virtual table */
    1391                 :   }
    1392                 : #endif
    1393               0 :   if( onError==OE_Default ){
    1394               0 :     onError = OE_Abort;
    1395                 :   }
    1396               0 :   if( onError!=OE_Abort && onError!=OE_Rollback ){
    1397               0 :     return 0;   /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
    1398                 :   }
    1399               0 :   if( pSelect->pSrc==0 ){
    1400               0 :     return 0;   /* SELECT must have a FROM clause */
    1401                 :   }
    1402               0 :   if( pSelect->pSrc->nSrc!=1 ){
    1403               0 :     return 0;   /* FROM clause must have exactly one term */
    1404                 :   }
    1405               0 :   if( pSelect->pSrc->a[0].pSelect ){
    1406               0 :     return 0;   /* FROM clause cannot contain a subquery */
    1407                 :   }
    1408               0 :   if( pSelect->pWhere ){
    1409               0 :     return 0;   /* SELECT may not have a WHERE clause */
    1410                 :   }
    1411               0 :   if( pSelect->pOrderBy ){
    1412               0 :     return 0;   /* SELECT may not have an ORDER BY clause */
    1413                 :   }
    1414                 :   /* Do not need to test for a HAVING clause.  If HAVING is present but
    1415                 :   ** there is no ORDER BY, we will get an error. */
    1416               0 :   if( pSelect->pGroupBy ){
    1417               0 :     return 0;   /* SELECT may not have a GROUP BY clause */
    1418                 :   }
    1419               0 :   if( pSelect->pLimit ){
    1420               0 :     return 0;   /* SELECT may not have a LIMIT clause */
    1421                 :   }
    1422                 :   assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
    1423               0 :   if( pSelect->pPrior ){
    1424               0 :     return 0;   /* SELECT may not be a compound query */
    1425                 :   }
    1426               0 :   if( pSelect->isDistinct ){
    1427               0 :     return 0;   /* SELECT may not be DISTINCT */
    1428                 :   }
    1429               0 :   pEList = pSelect->pEList;
    1430                 :   assert( pEList!=0 );
    1431               0 :   if( pEList->nExpr!=1 ){
    1432               0 :     return 0;   /* The result set must have exactly one column */
    1433                 :   }
    1434                 :   assert( pEList->a[0].pExpr );
    1435               0 :   if( pEList->a[0].pExpr->op!=TK_ALL ){
    1436               0 :     return 0;   /* The result set must be the special operator "*" */
    1437                 :   }
    1438                 : 
    1439                 :   /* At this point we have established that the statement is of the
    1440                 :   ** correct syntactic form to participate in this optimization.  Now
    1441                 :   ** we have to check the semantics.
    1442                 :   */
    1443               0 :   pItem = pSelect->pSrc->a;
    1444               0 :   pSrc = sqlite3LocateTable(pParse, pItem->zName, pItem->zDatabase);
    1445               0 :   if( pSrc==0 ){
    1446               0 :     return 0;   /* FROM clause does not contain a real table */
    1447                 :   }
    1448               0 :   if( pSrc==pDest ){
    1449               0 :     return 0;   /* tab1 and tab2 may not be the same table */
    1450                 :   }
    1451                 : #ifndef SQLITE_OMIT_VIRTUALTABLE
    1452               0 :   if( pSrc->isVirtual ){
    1453               0 :     return 0;   /* tab2 must not be a virtual table */
    1454                 :   }
    1455                 : #endif
    1456               0 :   if( pSrc->pSelect ){
    1457               0 :     return 0;   /* tab2 may not be a view */
    1458                 :   }
    1459               0 :   if( pDest->nCol!=pSrc->nCol ){
    1460               0 :     return 0;   /* Number of columns must be the same in tab1 and tab2 */
    1461                 :   }
    1462               0 :   if( pDest->iPKey!=pSrc->iPKey ){
    1463               0 :     return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
    1464                 :   }
    1465               0 :   for(i=0; i<pDest->nCol; i++){
    1466               0 :     if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
    1467               0 :       return 0;    /* Affinity must be the same on all columns */
    1468                 :     }
    1469               0 :     if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
    1470               0 :       return 0;    /* Collating sequence must be the same on all columns */
    1471                 :     }
    1472               0 :     if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
    1473               0 :       return 0;    /* tab2 must be NOT NULL if tab1 is */
    1474                 :     }
    1475                 :   }
    1476               0 :   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
    1477               0 :     if( pDestIdx->onError!=OE_None ){
    1478               0 :       destHasUniqueIdx = 1;
    1479                 :     }
    1480               0 :     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
    1481               0 :       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
    1482                 :     }
    1483               0 :     if( pSrcIdx==0 ){
    1484               0 :       return 0;    /* pDestIdx has no corresponding index in pSrc */
    1485                 :     }
    1486                 :   }
    1487                 : #ifndef SQLITE_OMIT_CHECK
    1488               0 :   if( pDest->pCheck && !sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){
    1489               0 :     return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
    1490                 :   }
    1491                 : #endif
    1492                 : 
    1493                 :   /* If we get this far, it means either:
    1494                 :   **
    1495                 :   **    *   We can always do the transfer if the table contains an
    1496                 :   **        an integer primary key
    1497                 :   **
    1498                 :   **    *   We can conditionally do the transfer if the destination
    1499                 :   **        table is empty.
    1500                 :   */
    1501                 : #ifdef SQLITE_TEST
    1502                 :   sqlite3_xferopt_count++;
    1503                 : #endif
    1504               0 :   iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
    1505               0 :   v = sqlite3GetVdbe(pParse);
    1506               0 :   iSrc = pParse->nTab++;
    1507               0 :   iDest = pParse->nTab++;
    1508               0 :   counterMem = autoIncBegin(pParse, iDbDest, pDest);
    1509               0 :   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
    1510               0 :   if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
    1511                 :     /* If tables do not have an INTEGER PRIMARY KEY and there
    1512                 :     ** are indices to be copied and the destination is not empty,
    1513                 :     ** we have to disallow the transfer optimization because the
    1514                 :     ** the rowids might change which will mess up indexing.
    1515                 :     **
    1516                 :     ** Or if the destination has a UNIQUE index and is not empty,
    1517                 :     ** we also disallow the transfer optimization because we cannot
    1518                 :     ** insure that all entries in the union of DEST and SRC will be
    1519                 :     ** unique.
    1520                 :     */
    1521               0 :     addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iDest, 0);
    1522               0 :     emptyDestTest = sqlite3VdbeAddOp(v, OP_Goto, 0, 0);
    1523               0 :     sqlite3VdbeJumpHere(v, addr1);
    1524                 :   }else{
    1525               0 :     emptyDestTest = 0;
    1526                 :   }
    1527               0 :   sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
    1528               0 :   emptySrcTest = sqlite3VdbeAddOp(v, OP_Rewind, iSrc, 0);
    1529               0 :   if( pDest->iPKey>=0 ){
    1530               0 :     addr1 = sqlite3VdbeAddOp(v, OP_Rowid, iSrc, 0);
    1531               0 :     sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
    1532               0 :     addr2 = sqlite3VdbeAddOp(v, OP_NotExists, iDest, 0);
    1533               0 :     sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, onError, 
    1534                 :                       "PRIMARY KEY must be unique", P3_STATIC);
    1535               0 :     sqlite3VdbeJumpHere(v, addr2);
    1536               0 :     autoIncStep(pParse, counterMem);
    1537               0 :   }else if( pDest->pIndex==0 ){
    1538               0 :     addr1 = sqlite3VdbeAddOp(v, OP_NewRowid, iDest, 0);
    1539                 :   }else{
    1540               0 :     addr1 = sqlite3VdbeAddOp(v, OP_Rowid, iSrc, 0);
    1541                 :     assert( pDest->autoInc==0 );
    1542                 :   }
    1543               0 :   sqlite3VdbeAddOp(v, OP_RowData, iSrc, 0);
    1544               0 :   sqlite3VdbeOp3(v, OP_Insert, iDest,
    1545                 :                     OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND,
    1546                 :                     pDest->zName, 0);
    1547               0 :   sqlite3VdbeAddOp(v, OP_Next, iSrc, addr1);
    1548               0 :   autoIncEnd(pParse, iDbDest, pDest, counterMem);
    1549               0 :   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
    1550               0 :     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
    1551               0 :       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
    1552                 :     }
    1553                 :     assert( pSrcIdx );
    1554               0 :     sqlite3VdbeAddOp(v, OP_Close, iSrc, 0);
    1555               0 :     sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
    1556               0 :     sqlite3VdbeAddOp(v, OP_Integer, iDbSrc, 0);
    1557               0 :     pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
    1558                 :     VdbeComment((v, "# %s", pSrcIdx->zName));
    1559               0 :     sqlite3VdbeOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, 
    1560                 :                    (char*)pKey, P3_KEYINFO_HANDOFF);
    1561               0 :     sqlite3VdbeAddOp(v, OP_Integer, iDbDest, 0);
    1562               0 :     pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
    1563                 :     VdbeComment((v, "# %s", pDestIdx->zName));
    1564               0 :     sqlite3VdbeOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, 
    1565                 :                    (char*)pKey, P3_KEYINFO_HANDOFF);
    1566               0 :     addr1 = sqlite3VdbeAddOp(v, OP_Rewind, iSrc, 0);
    1567               0 :     sqlite3VdbeAddOp(v, OP_RowKey, iSrc, 0);
    1568               0 :     sqlite3VdbeAddOp(v, OP_IdxInsert, iDest, 1);
    1569               0 :     sqlite3VdbeAddOp(v, OP_Next, iSrc, addr1+1);
    1570               0 :     sqlite3VdbeJumpHere(v, addr1);
    1571                 :   }
    1572               0 :   sqlite3VdbeJumpHere(v, emptySrcTest);
    1573               0 :   sqlite3VdbeAddOp(v, OP_Close, iSrc, 0);
    1574               0 :   sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
    1575               0 :   if( emptyDestTest ){
    1576               0 :     sqlite3VdbeAddOp(v, OP_Halt, SQLITE_OK, 0);
    1577               0 :     sqlite3VdbeJumpHere(v, emptyDestTest);
    1578               0 :     sqlite3VdbeAddOp(v, OP_Close, iDest, 0);
    1579               0 :     return 0;
    1580                 :   }else{
    1581               0 :     return 1;
    1582                 :   }
    1583                 : }
    1584                 : #endif /* SQLITE_OMIT_XFER_OPT */

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:16 +0000 (5 days ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.