insert.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  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. #include "sqliteInt.h"
  16. /*
  17. ** Generate code that will open a table for reading.
  18. */
  19. void sqlite3OpenTable(
  20. Parse *p, /* Generate code into this VDBE */
  21. int iCur, /* The cursor number of the table */
  22. int iDb, /* The database index in sqlite3.aDb[] */
  23. Table *pTab, /* The table to be opened */
  24. int opcode /* OP_OpenRead or OP_OpenWrite */
  25. ){
  26. Vdbe *v;
  27. assert( !IsVirtual(pTab) );
  28. v = sqlite3GetVdbe(p);
  29. assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
  30. sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName);
  31. sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb);
  32. sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32);
  33. VdbeComment((v, "%s", pTab->zName));
  34. }
  35. /*
  36. ** Return a pointer to the column affinity string associated with index
  37. ** pIdx. A column affinity string has one character for each column in
  38. ** the table, according to the affinity of the column:
  39. **
  40. ** Character Column affinity
  41. ** ------------------------------
  42. ** 'a' TEXT
  43. ** 'b' NONE
  44. ** 'c' NUMERIC
  45. ** 'd' INTEGER
  46. ** 'e' REAL
  47. **
  48. ** An extra 'd' is appended to the end of the string to cover the
  49. ** rowid that appears as the last column in every index.
  50. **
  51. ** Memory for the buffer containing the column index affinity string
  52. ** is managed along with the rest of the Index structure. It will be
  53. ** released when sqlite3DeleteIndex() is called.
  54. */
  55. const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
  56. if( !pIdx->zColAff ){
  57. /* The first time a column affinity string for a particular index is
  58. ** required, it is allocated and populated here. It is then stored as
  59. ** a member of the Index structure for subsequent use.
  60. **
  61. ** The column affinity string will eventually be deleted by
  62. ** sqliteDeleteIndex() when the Index structure itself is cleaned
  63. ** up.
  64. */
  65. int n;
  66. Table *pTab = pIdx->pTable;
  67. sqlite3 *db = sqlite3VdbeDb(v);
  68. pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2);
  69. if( !pIdx->zColAff ){
  70. db->mallocFailed = 1;
  71. return 0;
  72. }
  73. for(n=0; n<pIdx->nColumn; n++){
  74. pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
  75. }
  76. pIdx->zColAff[n++] = SQLITE_AFF_INTEGER;
  77. pIdx->zColAff[n] = 0;
  78. }
  79. return pIdx->zColAff;
  80. }
  81. /*
  82. ** Set P4 of the most recently inserted opcode to a column affinity
  83. ** string for table pTab. A column affinity string has one character
  84. ** for each column indexed by the index, according to the affinity of the
  85. ** column:
  86. **
  87. ** Character Column affinity
  88. ** ------------------------------
  89. ** 'a' TEXT
  90. ** 'b' NONE
  91. ** 'c' NUMERIC
  92. ** 'd' INTEGER
  93. ** 'e' REAL
  94. */
  95. void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
  96. /* The first time a column affinity string for a particular table
  97. ** is required, it is allocated and populated here. It is then
  98. ** stored as a member of the Table structure for subsequent use.
  99. **
  100. ** The column affinity string will eventually be deleted by
  101. ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
  102. */
  103. if( !pTab->zColAff ){
  104. char *zColAff;
  105. int i;
  106. sqlite3 *db = sqlite3VdbeDb(v);
  107. zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
  108. if( !zColAff ){
  109. db->mallocFailed = 1;
  110. return;
  111. }
  112. for(i=0; i<pTab->nCol; i++){
  113. zColAff[i] = pTab->aCol[i].affinity;
  114. }
  115. zColAff[pTab->nCol] = '\0';
  116. pTab->zColAff = zColAff;
  117. }
  118. sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
  119. }
  120. /*
  121. ** Return non-zero if the table pTab in database iDb or any of its indices
  122. ** have been opened at any point in the VDBE program beginning at location
  123. ** iStartAddr throught the end of the program. This is used to see if
  124. ** a statement of the form "INSERT INTO <iDb, pTab> SELECT ..." can
  125. ** run without using temporary table for the results of the SELECT.
  126. */
  127. static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
  128. Vdbe *v = sqlite3GetVdbe(p);
  129. int i;
  130. int iEnd = sqlite3VdbeCurrentAddr(v);
  131. #ifndef SQLITE_OMIT_VIRTUALTABLE
  132. VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
  133. #endif
  134. for(i=iStartAddr; i<iEnd; i++){
  135. VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
  136. assert( pOp!=0 );
  137. if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
  138. Index *pIndex;
  139. int tnum = pOp->p2;
  140. if( tnum==pTab->tnum ){
  141. return 1;
  142. }
  143. for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
  144. if( tnum==pIndex->tnum ){
  145. return 1;
  146. }
  147. }
  148. }
  149. #ifndef SQLITE_OMIT_VIRTUALTABLE
  150. if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
  151. assert( pOp->p4.pVtab!=0 );
  152. assert( pOp->p4type==P4_VTAB );
  153. return 1;
  154. }
  155. #endif
  156. }
  157. return 0;
  158. }
  159. #ifndef SQLITE_OMIT_AUTOINCREMENT
  160. /*
  161. ** Locate or create an AutoincInfo structure associated with table pTab
  162. ** which is in database iDb. Return the register number for the register
  163. ** that holds the maximum rowid.
  164. **
  165. ** There is at most one AutoincInfo structure per table even if the
  166. ** same table is autoincremented multiple times due to inserts within
  167. ** triggers. A new AutoincInfo structure is created if this is the
  168. ** first use of table pTab. On 2nd and subsequent uses, the original
  169. ** AutoincInfo structure is used.
  170. **
  171. ** Three memory locations are allocated:
  172. **
  173. ** (1) Register to hold the name of the pTab table.
  174. ** (2) Register to hold the maximum ROWID of pTab.
  175. ** (3) Register to hold the rowid in sqlite_sequence of pTab
  176. **
  177. ** The 2nd register is the one that is returned. That is all the
  178. ** insert routine needs to know about.
  179. */
  180. static int autoIncBegin(
  181. Parse *pParse, /* Parsing context */
  182. int iDb, /* Index of the database holding pTab */
  183. Table *pTab /* The table we are writing to */
  184. ){
  185. int memId = 0; /* Register holding maximum rowid */
  186. if( pTab->tabFlags & TF_Autoincrement ){
  187. Parse *pToplevel = sqlite3ParseToplevel(pParse);
  188. AutoincInfo *pInfo;
  189. pInfo = pToplevel->pAinc;
  190. while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
  191. if( pInfo==0 ){
  192. pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
  193. if( pInfo==0 ) return 0;
  194. pInfo->pNext = pToplevel->pAinc;
  195. pToplevel->pAinc = pInfo;
  196. pInfo->pTab = pTab;
  197. pInfo->iDb = iDb;
  198. pToplevel->nMem++; /* Register to hold name of table */
  199. pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */
  200. pToplevel->nMem++; /* Rowid in sqlite_sequence */
  201. }
  202. memId = pInfo->regCtr;
  203. }
  204. return memId;
  205. }
  206. /*
  207. ** This routine generates code that will initialize all of the
  208. ** register used by the autoincrement tracker.
  209. */
  210. void sqlite3AutoincrementBegin(Parse *pParse){
  211. AutoincInfo *p; /* Information about an AUTOINCREMENT */
  212. sqlite3 *db = pParse->db; /* The database connection */
  213. Db *pDb; /* Database only autoinc table */
  214. int memId; /* Register holding max rowid */
  215. int addr; /* A VDBE address */
  216. Vdbe *v = pParse->pVdbe; /* VDBE under construction */
  217. /* This routine is never called during trigger-generation. It is
  218. ** only called from the top-level */
  219. assert( pParse->pTriggerTab==0 );
  220. assert( pParse==sqlite3ParseToplevel(pParse) );
  221. assert( v ); /* We failed long ago if this is not so */
  222. for(p = pParse->pAinc; p; p = p->pNext){
  223. pDb = &db->aDb[p->iDb];
  224. memId = p->regCtr;
  225. assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
  226. sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
  227. sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
  228. addr = sqlite3VdbeCurrentAddr(v);
  229. sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
  230. sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
  231. sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
  232. sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
  233. sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
  234. sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
  235. sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
  236. sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
  237. sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
  238. sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
  239. sqlite3VdbeAddOp0(v, OP_Close);
  240. }
  241. }
  242. /*
  243. ** Update the maximum rowid for an autoincrement calculation.
  244. **
  245. ** This routine should be called when the top of the stack holds a
  246. ** new rowid that is about to be inserted. If that new rowid is
  247. ** larger than the maximum rowid in the memId memory cell, then the
  248. ** memory cell is updated. The stack is unchanged.
  249. */
  250. static void autoIncStep(Parse *pParse, int memId, int regRowid){
  251. if( memId>0 ){
  252. sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
  253. }
  254. }
  255. /*
  256. ** This routine generates the code needed to write autoincrement
  257. ** maximum rowid values back into the sqlite_sequence register.
  258. ** Every statement that might do an INSERT into an autoincrement
  259. ** table (either directly or through triggers) needs to call this
  260. ** routine just before the "exit" code.
  261. */
  262. void sqlite3AutoincrementEnd(Parse *pParse){
  263. AutoincInfo *p;
  264. Vdbe *v = pParse->pVdbe;
  265. sqlite3 *db = pParse->db;
  266. assert( v );
  267. for(p = pParse->pAinc; p; p = p->pNext){
  268. Db *pDb = &db->aDb[p->iDb];
  269. int j1, j2, j3, j4, j5;
  270. int iRec;
  271. int memId = p->regCtr;
  272. iRec = sqlite3GetTempReg(pParse);
  273. assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
  274. sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
  275. j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
  276. j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
  277. j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
  278. j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
  279. sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
  280. sqlite3VdbeJumpHere(v, j2);
  281. sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
  282. j5 = sqlite3VdbeAddOp0(v, OP_Goto);
  283. sqlite3VdbeJumpHere(v, j4);
  284. sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
  285. sqlite3VdbeJumpHere(v, j1);
  286. sqlite3VdbeJumpHere(v, j5);
  287. sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
  288. sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
  289. sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  290. sqlite3VdbeAddOp0(v, OP_Close);
  291. sqlite3ReleaseTempReg(pParse, iRec);
  292. }
  293. }
  294. #else
  295. /*
  296. ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
  297. ** above are all no-ops
  298. */
  299. # define autoIncBegin(A,B,C) (0)
  300. # define autoIncStep(A,B,C)
  301. #endif /* SQLITE_OMIT_AUTOINCREMENT */
  302. /*
  303. ** Generate code for a co-routine that will evaluate a subquery one
  304. ** row at a time.
  305. **
  306. ** The pSelect parameter is the subquery that the co-routine will evaluation.
  307. ** Information about the location of co-routine and the registers it will use
  308. ** is returned by filling in the pDest object.
  309. **
  310. ** Registers are allocated as follows:
  311. **
  312. ** pDest->iSDParm The register holding the next entry-point of the
  313. ** co-routine. Run the co-routine to its next breakpoint
  314. ** by calling "OP_Yield $X" where $X is pDest->iSDParm.
  315. **
  316. ** pDest->iSDParm+1 The register holding the "completed" flag for the
  317. ** co-routine. This register is 0 if the previous Yield
  318. ** generated a new result row, or 1 if the subquery
  319. ** has completed. If the Yield is called again
  320. ** after this register becomes 1, then the VDBE will
  321. ** halt with an SQLITE_INTERNAL error.
  322. **
  323. ** pDest->iSdst First result register.
  324. **
  325. ** pDest->nSdst Number of result registers.
  326. **
  327. ** This routine handles all of the register allocation and fills in the
  328. ** pDest structure appropriately.
  329. **
  330. ** Here is a schematic of the generated code assuming that X is the
  331. ** co-routine entry-point register reg[pDest->iSDParm], that EOF is the
  332. ** completed flag reg[pDest->iSDParm+1], and R and S are the range of
  333. ** registers that hold the result set, reg[pDest->iSdst] through
  334. ** reg[pDest->iSdst+pDest->nSdst-1]:
  335. **
  336. ** X <- A
  337. ** EOF <- 0
  338. ** goto B
  339. ** A: setup for the SELECT
  340. ** loop rows in the SELECT
  341. ** load results into registers R..S
  342. ** yield X
  343. ** end loop
  344. ** cleanup after the SELECT
  345. ** EOF <- 1
  346. ** yield X
  347. ** halt-error
  348. ** B:
  349. **
  350. ** To use this subroutine, the caller generates code as follows:
  351. **
  352. ** [ Co-routine generated by this subroutine, shown above ]
  353. ** S: yield X
  354. ** if EOF goto E
  355. ** if skip this row, goto C
  356. ** if terminate loop, goto E
  357. ** deal with this row
  358. ** C: goto S
  359. ** E:
  360. */
  361. int sqlite3CodeCoroutine(Parse *pParse, Select *pSelect, SelectDest *pDest){
  362. int regYield; /* Register holding co-routine entry-point */
  363. int regEof; /* Register holding co-routine completion flag */
  364. int addrTop; /* Top of the co-routine */
  365. int j1; /* Jump instruction */
  366. int rc; /* Result code */
  367. Vdbe *v; /* VDBE under construction */
  368. regYield = ++pParse->nMem;
  369. regEof = ++pParse->nMem;
  370. v = sqlite3GetVdbe(pParse);
  371. addrTop = sqlite3VdbeCurrentAddr(v);
  372. sqlite3VdbeAddOp2(v, OP_Integer, addrTop+2, regYield); /* X <- A */
  373. VdbeComment((v, "Co-routine entry point"));
  374. sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof); /* EOF <- 0 */
  375. VdbeComment((v, "Co-routine completion flag"));
  376. sqlite3SelectDestInit(pDest, SRT_Coroutine, regYield);
  377. j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
  378. rc = sqlite3Select(pParse, pSelect, pDest);
  379. assert( pParse->nErr==0 || rc );
  380. if( pParse->db->mallocFailed && rc==SQLITE_OK ) rc = SQLITE_NOMEM;
  381. if( rc ) return rc;
  382. sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof); /* EOF <- 1 */
  383. sqlite3VdbeAddOp1(v, OP_Yield, regYield); /* yield X */
  384. sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
  385. VdbeComment((v, "End of coroutine"));
  386. sqlite3VdbeJumpHere(v, j1); /* label B: */
  387. return rc;
  388. }
  389. /* Forward declaration */
  390. static int xferOptimization(
  391. Parse *pParse, /* Parser context */
  392. Table *pDest, /* The table we are inserting into */
  393. Select *pSelect, /* A SELECT statement to use as the data source */
  394. int onError, /* How to handle constraint errors */
  395. int iDbDest /* The database of pDest */
  396. );
  397. /*
  398. ** This routine is call to handle SQL of the following forms:
  399. **
  400. ** insert into TABLE (IDLIST) values(EXPRLIST)
  401. ** insert into TABLE (IDLIST) select
  402. **
  403. ** The IDLIST following the table name is always optional. If omitted,
  404. ** then a list of all columns for the table is substituted. The IDLIST
  405. ** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted.
  406. **
  407. ** The pList parameter holds EXPRLIST in the first form of the INSERT
  408. ** statement above, and pSelect is NULL. For the second form, pList is
  409. ** NULL and pSelect is a pointer to the select statement used to generate
  410. ** data for the insert.
  411. **
  412. ** The code generated follows one of four templates. For a simple
  413. ** select with data coming from a VALUES clause, the code executes
  414. ** once straight down through. Pseudo-code follows (we call this
  415. ** the "1st template"):
  416. **
  417. ** open write cursor to <table> and its indices
  418. ** puts VALUES clause expressions onto the stack
  419. ** write the resulting record into <table>
  420. ** cleanup
  421. **
  422. ** The three remaining templates assume the statement is of the form
  423. **
  424. ** INSERT INTO <table> SELECT ...
  425. **
  426. ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
  427. ** in other words if the SELECT pulls all columns from a single table
  428. ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
  429. ** if <table2> and <table1> are distinct tables but have identical
  430. ** schemas, including all the same indices, then a special optimization
  431. ** is invoked that copies raw records from <table2> over to <table1>.
  432. ** See the xferOptimization() function for the implementation of this
  433. ** template. This is the 2nd template.
  434. **
  435. ** open a write cursor to <table>
  436. ** open read cursor on <table2>
  437. ** transfer all records in <table2> over to <table>
  438. ** close cursors
  439. ** foreach index on <table>
  440. ** open a write cursor on the <table> index
  441. ** open a read cursor on the corresponding <table2> index
  442. ** transfer all records from the read to the write cursors
  443. ** close cursors
  444. ** end foreach
  445. **
  446. ** The 3rd template is for when the second template does not apply
  447. ** and the SELECT clause does not read from <table> at any time.
  448. ** The generated code follows this template:
  449. **
  450. ** EOF <- 0
  451. ** X <- A
  452. ** goto B
  453. ** A: setup for the SELECT
  454. ** loop over the rows in the SELECT
  455. ** load values into registers R..R+n
  456. ** yield X
  457. ** end loop
  458. ** cleanup after the SELECT
  459. ** EOF <- 1
  460. ** yield X
  461. ** goto A
  462. ** B: open write cursor to <table> and its indices
  463. ** C: yield X
  464. ** if EOF goto D
  465. ** insert the select result into <table> from R..R+n
  466. ** goto C
  467. ** D: cleanup
  468. **
  469. ** The 4th template is used if the insert statement takes its
  470. ** values from a SELECT but the data is being inserted into a table
  471. ** that is also read as part of the SELECT. In the third form,
  472. ** we have to use a intermediate table to store the results of
  473. ** the select. The template is like this:
  474. **
  475. ** EOF <- 0
  476. ** X <- A
  477. ** goto B
  478. ** A: setup for the SELECT
  479. ** loop over the tables in the SELECT
  480. ** load value into register R..R+n
  481. ** yield X
  482. ** end loop
  483. ** cleanup after the SELECT
  484. ** EOF <- 1
  485. ** yield X
  486. ** halt-error
  487. ** B: open temp table
  488. ** L: yield X
  489. ** if EOF goto M
  490. ** insert row from R..R+n into temp table
  491. ** goto L
  492. ** M: open write cursor to <table> and its indices
  493. ** rewind temp table
  494. ** C: loop over rows of intermediate table
  495. ** transfer values form intermediate table into <table>
  496. ** end loop
  497. ** D: cleanup
  498. */
  499. void sqlite3Insert(
  500. Parse *pParse, /* Parser context */
  501. SrcList *pTabList, /* Name of table into which we are inserting */
  502. ExprList *pList, /* List of values to be inserted */
  503. Select *pSelect, /* A SELECT statement to use as the data source */
  504. IdList *pColumn, /* Column names corresponding to IDLIST. */
  505. int onError /* How to handle constraint errors */
  506. ){
  507. sqlite3 *db; /* The main database structure */
  508. Table *pTab; /* The table to insert into. aka TABLE */
  509. char *zTab; /* Name of the table into which we are inserting */
  510. const char *zDb; /* Name of the database holding this table */
  511. int i, j, idx; /* Loop counters */
  512. Vdbe *v; /* Generate code into this virtual machine */
  513. Index *pIdx; /* For looping over indices of the table */
  514. int nColumn; /* Number of columns in the data */
  515. int nHidden = 0; /* Number of hidden columns if TABLE is virtual */
  516. int baseCur = 0; /* VDBE Cursor number for pTab */
  517. int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
  518. int endOfLoop; /* Label for the end of the insertion loop */
  519. int useTempTable = 0; /* Store SELECT results in intermediate table */
  520. int srcTab = 0; /* Data comes from this temporary cursor if >=0 */
  521. int addrInsTop = 0; /* Jump to label "D" */
  522. int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */
  523. int addrSelect = 0; /* Address of coroutine that implements the SELECT */
  524. SelectDest dest; /* Destination for SELECT on rhs of INSERT */
  525. int iDb; /* Index of database holding TABLE */
  526. Db *pDb; /* The database containing table being inserted into */
  527. int appendFlag = 0; /* True if the insert is likely to be an append */
  528. /* Register allocations */
  529. int regFromSelect = 0;/* Base register for data coming from SELECT */
  530. int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */
  531. int regRowCount = 0; /* Memory cell used for the row counter */
  532. int regIns; /* Block of regs holding rowid+data being inserted */
  533. int regRowid; /* registers holding insert rowid */
  534. int regData; /* register holding first column to insert */
  535. int regEof = 0; /* Register recording end of SELECT data */
  536. int *aRegIdx = 0; /* One register allocated to each index */
  537. #ifndef SQLITE_OMIT_TRIGGER
  538. int isView; /* True if attempting to insert into a view */
  539. Trigger *pTrigger; /* List of triggers on pTab, if required */
  540. int tmask; /* Mask of trigger times */
  541. #endif
  542. db = pParse->db;
  543. memset(&dest, 0, sizeof(dest));
  544. if( pParse->nErr || db->mallocFailed ){
  545. goto insert_cleanup;
  546. }
  547. /* Locate the table into which we will be inserting new information.
  548. */
  549. assert( pTabList->nSrc==1 );
  550. zTab = pTabList->a[0].zName;
  551. if( NEVER(zTab==0) ) goto insert_cleanup;
  552. pTab = sqlite3SrcListLookup(pParse, pTabList);
  553. if( pTab==0 ){
  554. goto insert_cleanup;
  555. }
  556. iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  557. assert( iDb<db->nDb );
  558. pDb = &db->aDb[iDb];
  559. zDb = pDb->zName;
  560. if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
  561. goto insert_cleanup;
  562. }
  563. /* Figure out if we have any triggers and if the table being
  564. ** inserted into is a view
  565. */
  566. #ifndef SQLITE_OMIT_TRIGGER
  567. pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
  568. isView = pTab->pSelect!=0;
  569. #else
  570. # define pTrigger 0
  571. # define tmask 0
  572. # define isView 0
  573. #endif
  574. #ifdef SQLITE_OMIT_VIEW
  575. # undef isView
  576. # define isView 0
  577. #endif
  578. assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
  579. /* If pTab is really a view, make sure it has been initialized.
  580. ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual
  581. ** module table).
  582. */
  583. if( sqlite3ViewGetColumnNames(pParse, pTab) ){
  584. goto insert_cleanup;
  585. }
  586. /* Ensure that:
  587. * (a) the table is not read-only,
  588. * (b) that if it is a view then ON INSERT triggers exist
  589. */
  590. if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
  591. goto insert_cleanup;
  592. }
  593. /* Allocate a VDBE
  594. */
  595. v = sqlite3GetVdbe(pParse);
  596. if( v==0 ) goto insert_cleanup;
  597. if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
  598. sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
  599. #ifndef SQLITE_OMIT_XFER_OPT
  600. /* If the statement is of the form
  601. **
  602. ** INSERT INTO <table1> SELECT * FROM <table2>;
  603. **
  604. ** Then special optimizations can be applied that make the transfer
  605. ** very fast and which reduce fragmentation of indices.
  606. **
  607. ** This is the 2nd template.
  608. */
  609. if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
  610. assert( !pTrigger );
  611. assert( pList==0 );
  612. goto insert_end;
  613. }
  614. #endif /* SQLITE_OMIT_XFER_OPT */
  615. /* If this is an AUTOINCREMENT table, look up the sequence number in the
  616. ** sqlite_sequence table and store it in memory cell regAutoinc.
  617. */
  618. regAutoinc = autoIncBegin(pParse, iDb, pTab);
  619. /* Figure out how many columns of data are supplied. If the data
  620. ** is coming from a SELECT statement, then generate a co-routine that
  621. ** produces a single row of the SELECT on each invocation. The
  622. ** co-routine is the common header to the 3rd and 4th templates.
  623. */
  624. if( pSelect ){
  625. /* Data is coming from a SELECT. Generate a co-routine to run that
  626. ** SELECT. */
  627. int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
  628. if( rc ) goto insert_cleanup;
  629. regEof = dest.iSDParm + 1;
  630. regFromSelect = dest.iSdst;
  631. assert( pSelect->pEList );
  632. nColumn = pSelect->pEList->nExpr;
  633. assert( dest.nSdst==nColumn );
  634. /* Set useTempTable to TRUE if the result of the SELECT statement
  635. ** should be written into a temporary table (template 4). Set to
  636. ** FALSE if each* row of the SELECT can be written directly into
  637. ** the destination table (template 3).
  638. **
  639. ** A temp table must be used if the table being updated is also one
  640. ** of the tables being read by the SELECT statement. Also use a
  641. ** temp table in the case of row triggers.
  642. */
  643. if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
  644. useTempTable = 1;
  645. }
  646. if( useTempTable ){
  647. /* Invoke the coroutine to extract information from the SELECT
  648. ** and add it to a transient table srcTab. The code generated
  649. ** here is from the 4th template:
  650. **
  651. ** B: open temp table
  652. ** L: yield X
  653. ** if EOF goto M
  654. ** insert row from R..R+n into temp table
  655. ** goto L
  656. ** M: ...
  657. */
  658. int regRec; /* Register to hold packed record */
  659. int regTempRowid; /* Register to hold temp table ROWID */
  660. int addrTop; /* Label "L" */
  661. int addrIf; /* Address of jump to M */
  662. srcTab = pParse->nTab++;
  663. regRec = sqlite3GetTempReg(pParse);
  664. regTempRowid = sqlite3GetTempReg(pParse);
  665. sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
  666. addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
  667. addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
  668. sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
  669. sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
  670. sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
  671. sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
  672. sqlite3VdbeJumpHere(v, addrIf);
  673. sqlite3ReleaseTempReg(pParse, regRec);
  674. sqlite3ReleaseTempReg(pParse, regTempRowid);
  675. }
  676. }else{
  677. /* This is the case if the data for the INSERT is coming from a VALUES
  678. ** clause
  679. */
  680. NameContext sNC;
  681. memset(&sNC, 0, sizeof(sNC));
  682. sNC.pParse = pParse;
  683. srcTab = -1;
  684. assert( useTempTable==0 );
  685. nColumn = pList ? pList->nExpr : 0;
  686. for(i=0; i<nColumn; i++){
  687. if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
  688. goto insert_cleanup;
  689. }
  690. }
  691. }
  692. /* Make sure the number of columns in the source data matches the number
  693. ** of columns to be inserted into the table.
  694. */
  695. if( IsVirtual(pTab) ){
  696. for(i=0; i<pTab->nCol; i++){
  697. nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
  698. }
  699. }
  700. if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
  701. sqlite3ErrorMsg(pParse,
  702. "table %S has %d columns but %d values were supplied",
  703. pTabList, 0, pTab->nCol-nHidden, nColumn);
  704. goto insert_cleanup;
  705. }
  706. if( pColumn!=0 && nColumn!=pColumn->nId ){
  707. sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
  708. goto insert_cleanup;
  709. }
  710. /* If the INSERT statement included an IDLIST term, then make sure
  711. ** all elements of the IDLIST really are columns of the table and
  712. ** remember the column indices.
  713. **
  714. ** If the table has an INTEGER PRIMARY KEY column and that column
  715. ** is named in the IDLIST, then record in the keyColumn variable
  716. ** the index into IDLIST of the primary key column. keyColumn is
  717. ** the index of the primary key as it appears in IDLIST, not as
  718. ** is appears in the original table. (The index of the primary
  719. ** key in the original table is pTab->iPKey.)
  720. */
  721. if( pColumn ){
  722. for(i=0; i<pColumn->nId; i++){
  723. pColumn->a[i].idx = -1;
  724. }
  725. for(i=0; i<pColumn->nId; i++){
  726. for(j=0; j<pTab->nCol; j++){
  727. if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
  728. pColumn->a[i].idx = j;
  729. if( j==pTab->iPKey ){
  730. keyColumn = i;
  731. }
  732. break;
  733. }
  734. }
  735. if( j>=pTab->nCol ){
  736. if( sqlite3IsRowid(pColumn->a[i].zName) ){
  737. keyColumn = i;
  738. }else{
  739. sqlite3ErrorMsg(pParse, "table %S has no column named %s",
  740. pTabList, 0, pColumn->a[i].zName);
  741. pParse->checkSchema = 1;
  742. goto insert_cleanup;
  743. }
  744. }
  745. }
  746. }
  747. /* If there is no IDLIST term but the table has an integer primary
  748. ** key, the set the keyColumn variable to the primary key column index
  749. ** in the original table definition.
  750. */
  751. if( pColumn==0 && nColumn>0 ){
  752. keyColumn = pTab->iPKey;
  753. }
  754. /* Initialize the count of rows to be inserted
  755. */
  756. if( db->flags & SQLITE_CountRows ){
  757. regRowCount = ++pParse->nMem;
  758. sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
  759. }
  760. /* If this is not a view, open the table and and all indices */
  761. if( !isView ){
  762. int nIdx;
  763. baseCur = pParse->nTab;
  764. nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite);
  765. aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
  766. if( aRegIdx==0 ){
  767. goto insert_cleanup;
  768. }
  769. for(i=0; i<nIdx; i++){
  770. aRegIdx[i] = ++pParse->nMem;
  771. }
  772. }
  773. /* This is the top of the main insertion loop */
  774. if( useTempTable ){
  775. /* This block codes the top of loop only. The complete loop is the
  776. ** following pseudocode (template 4):
  777. **
  778. ** rewind temp table
  779. ** C: loop over rows of intermediate table
  780. ** transfer values form intermediate table into <table>
  781. ** end loop
  782. ** D: ...
  783. */
  784. addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
  785. addrCont = sqlite3VdbeCurrentAddr(v);
  786. }else if( pSelect ){
  787. /* This block codes the top of loop only. The complete loop is the
  788. ** following pseudocode (template 3):
  789. **
  790. ** C: yield X
  791. ** if EOF goto D
  792. ** insert the select result into <table> from R..R+n
  793. ** goto C
  794. ** D: ...
  795. */
  796. addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
  797. addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
  798. }
  799. /* Allocate registers for holding the rowid of the new row,
  800. ** the content of the new row, and the assemblied row record.
  801. */
  802. regRowid = regIns = pParse->nMem+1;
  803. pParse->nMem += pTab->nCol + 1;
  804. if( IsVirtual(pTab) ){
  805. regRowid++;
  806. pParse->nMem++;
  807. }
  808. regData = regRowid+1;
  809. /* Run the BEFORE and INSTEAD OF triggers, if there are any
  810. */
  811. endOfLoop = sqlite3VdbeMakeLabel(v);
  812. if( tmask & TRIGGER_BEFORE ){
  813. int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
  814. /* build the NEW.* reference row. Note that if there is an INTEGER
  815. ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
  816. ** translated into a unique ID for the row. But on a BEFORE trigger,
  817. ** we do not know what the unique ID will be (because the insert has
  818. ** not happened yet) so we substitute a rowid of -1
  819. */
  820. if( keyColumn<0 ){
  821. sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
  822. }else{
  823. int j1;
  824. if( useTempTable ){
  825. sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols);
  826. }else{
  827. assert( pSelect==0 ); /* Otherwise useTempTable is true */
  828. sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols);
  829. }
  830. j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
  831. sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
  832. sqlite3VdbeJumpHere(v, j1);
  833. sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
  834. }
  835. /* Cannot have triggers on a virtual table. If it were possible,
  836. ** this block would have to account for hidden column.
  837. */
  838. assert( !IsVirtual(pTab) );
  839. /* Create the new column data
  840. */
  841. for(i=0; i<pTab->nCol; i++){
  842. if( pColumn==0 ){
  843. j = i;
  844. }else{
  845. for(j=0; j<pColumn->nId; j++){
  846. if( pColumn->a[j].idx==i ) break;
  847. }
  848. }
  849. if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
  850. sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
  851. }else if( useTempTable ){
  852. sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
  853. }else{
  854. assert( pSelect==0 ); /* Otherwise useTempTable is true */
  855. sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
  856. }
  857. }
  858. /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
  859. ** do not attempt any conversions before assembling the record.
  860. ** If this is a real table, attempt conversions as required by the
  861. ** table column affinities.
  862. */
  863. if( !isView ){
  864. sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
  865. sqlite3TableAffinityStr(v, pTab);
  866. }
  867. /* Fire BEFORE or INSTEAD OF triggers */
  868. sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
  869. pTab, regCols-pTab->nCol-1, onError, endOfLoop);
  870. sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
  871. }
  872. /* Push the record number for the new entry onto the stack. The
  873. ** record number is a randomly generate integer created by NewRowid
  874. ** except when the table has an INTEGER PRIMARY KEY column, in which
  875. ** case the record number is the same as that column.
  876. */
  877. if( !isView ){
  878. if( IsVirtual(pTab) ){
  879. /* The row that the VUpdate opcode will delete: none */
  880. sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
  881. }
  882. if( keyColumn>=0 ){
  883. if( useTempTable ){
  884. sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid);
  885. }else if( pSelect ){
  886. sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid);
  887. }else{
  888. VdbeOp *pOp;
  889. sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid);
  890. pOp = sqlite3VdbeGetOp(v, -1);
  891. if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
  892. appendFlag = 1;
  893. pOp->opcode = OP_NewRowid;
  894. pOp->p1 = baseCur;
  895. pOp->p2 = regRowid;
  896. pOp->p3 = regAutoinc;
  897. }
  898. }
  899. /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
  900. ** to generate a unique primary key value.
  901. */
  902. if( !appendFlag ){
  903. int j1;
  904. if( !IsVirtual(pTab) ){
  905. j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
  906. sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
  907. sqlite3VdbeJumpHere(v, j1);
  908. }else{
  909. j1 = sqlite3VdbeCurrentAddr(v);
  910. sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
  911. }
  912. sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
  913. }
  914. }else if( IsVirtual(pTab) ){
  915. sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
  916. }else{
  917. sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc);
  918. appendFlag = 1;
  919. }
  920. autoIncStep(pParse, regAutoinc, regRowid);
  921. /* Push onto the stack, data for all columns of the new entry, beginning
  922. ** with the first column.
  923. */
  924. nHidden = 0;
  925. for(i=0; i<pTab->nCol; i++){
  926. int iRegStore = regRowid+1+i;
  927. if( i==pTab->iPKey ){
  928. /* The value of the INTEGER PRIMARY KEY column is always a NULL.
  929. ** Whenever this column is read, the record number will be substituted
  930. ** in its place. So will fill this column with a NULL to avoid
  931. ** taking up data space with information that will never be used. */
  932. sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
  933. continue;
  934. }
  935. if( pColumn==0 ){
  936. if( IsHiddenColumn(&pTab->aCol[i]) ){
  937. assert( IsVirtual(pTab) );
  938. j = -1;
  939. nHidden++;
  940. }else{
  941. j = i - nHidden;
  942. }
  943. }else{
  944. for(j=0; j<pColumn->nId; j++){
  945. if( pColumn->a[j].idx==i ) break;
  946. }
  947. }
  948. if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
  949. sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
  950. }else if( useTempTable ){
  951. sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
  952. }else if( pSelect ){
  953. sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
  954. }else{
  955. sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
  956. }
  957. }
  958. /* Generate code to check constraints and generate index keys and
  959. ** do the insertion.
  960. */
  961. #ifndef SQLITE_OMIT_VIRTUALTABLE
  962. if( IsVirtual(pTab) ){
  963. const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
  964. sqlite3VtabMakeWritable(pParse, pTab);
  965. sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
  966. sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
  967. sqlite3MayAbort(pParse);
  968. }else
  969. #endif
  970. {
  971. int isReplace; /* Set to true if constraints may cause a replace */
  972. sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx,
  973. keyColumn>=0, 0, onError, endOfLoop, &isReplace
  974. );
  975. sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
  976. sqlite3CompleteInsertion(
  977. pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0
  978. );
  979. }
  980. }
  981. /* Update the count of rows that are inserted
  982. */
  983. if( (db->flags & SQLITE_CountRows)!=0 ){
  984. sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
  985. }
  986. if( pTrigger ){
  987. /* Code AFTER triggers */
  988. sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
  989. pTab, regData-2-pTab->nCol, onError, endOfLoop);
  990. }
  991. /* The bottom of the main insertion loop, if the data source
  992. ** is a SELECT statement.
  993. */
  994. sqlite3VdbeResolveLabel(v, endOfLoop);
  995. if( useTempTable ){
  996. sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
  997. sqlite3VdbeJumpHere(v, addrInsTop);
  998. sqlite3VdbeAddOp1(v, OP_Close, srcTab);
  999. }else if( pSelect ){
  1000. sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
  1001. sqlite3VdbeJumpHere(v, addrInsTop);
  1002. }
  1003. if( !IsVirtual(pTab) && !isView ){
  1004. /* Close all tables opened */
  1005. sqlite3VdbeAddOp1(v, OP_Close, baseCur);
  1006. for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
  1007. sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur);
  1008. }
  1009. }
  1010. insert_end:
  1011. /* Update the sqlite_sequence table by storing the content of the
  1012. ** maximum rowid counter values recorded while inserting into
  1013. ** autoincrement tables.
  1014. */
  1015. if( pParse->nested==0 && pParse->pTriggerTab==0 ){
  1016. sqlite3AutoincrementEnd(pParse);
  1017. }
  1018. /*
  1019. ** Return the number of rows inserted. If this routine is
  1020. ** generating code because of a call to sqlite3NestedParse(), do not
  1021. ** invoke the callback function.
  1022. */
  1023. if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
  1024. sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
  1025. sqlite3VdbeSetNumCols(v, 1);
  1026. sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
  1027. }
  1028. insert_cleanup:
  1029. sqlite3SrcListDelete(db, pTabList);
  1030. sqlite3ExprListDelete(db, pList);
  1031. sqlite3SelectDelete(db, pSelect);
  1032. sqlite3IdListDelete(db, pColumn);
  1033. sqlite3DbFree(db, aRegIdx);
  1034. }
  1035. /* Make sure "isView" and other macros defined above are undefined. Otherwise
  1036. ** thely may interfere with compilation of other functions in this file
  1037. ** (or in another file, if this file becomes part of the amalgamation). */
  1038. #ifdef isView
  1039. #undef isView
  1040. #endif
  1041. #ifdef pTrigger
  1042. #undef pTrigger
  1043. #endif
  1044. #ifdef tmask
  1045. #undef tmask
  1046. #endif
  1047. /*
  1048. ** Generate code to do constraint checks prior to an INSERT or an UPDATE.
  1049. **
  1050. ** The input is a range of consecutive registers as follows:
  1051. **
  1052. ** 1. The rowid of the row after the update.
  1053. **
  1054. ** 2. The data in the first column of the entry after the update.
  1055. **
  1056. ** i. Data from middle columns...
  1057. **
  1058. ** N. The data in the last column of the entry after the update.
  1059. **
  1060. ** The regRowid parameter is the index of the register containing (1).
  1061. **
  1062. ** If isUpdate is true and rowidChng is non-zero, then rowidChng contains
  1063. ** the address of a register containing the rowid before the update takes
  1064. ** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate
  1065. ** is false, indicating an INSERT statement, then a non-zero rowidChng
  1066. ** indicates that the rowid was explicitly specified as part of the
  1067. ** INSERT statement. If rowidChng is false, it means that the rowid is
  1068. ** computed automatically in an insert or that the rowid value is not
  1069. ** modified by an update.
  1070. **
  1071. ** The code generated by this routine store new index entries into
  1072. ** registers identified by aRegIdx[]. No index entry is created for
  1073. ** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is
  1074. ** the same as the order of indices on the linked list of indices
  1075. ** attached to the table.
  1076. **
  1077. ** This routine also generates code to check constraints. NOT NULL,
  1078. ** CHECK, and UNIQUE constraints are all checked. If a constraint fails,
  1079. ** then the appropriate action is performed. There are five possible
  1080. ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
  1081. **
  1082. ** Constraint type Action What Happens
  1083. ** --------------- ---------- ----------------------------------------
  1084. ** any ROLLBACK The current transaction is rolled back and
  1085. ** sqlite3_exec() returns immediately with a
  1086. ** return code of SQLITE_CONSTRAINT.
  1087. **
  1088. ** any ABORT Back out changes from the current command
  1089. ** only (do not do a complete rollback) then
  1090. ** cause sqlite3_exec() to return immediately
  1091. ** with SQLITE_CONSTRAINT.
  1092. **
  1093. ** any FAIL Sqlite3_exec() returns immediately with a
  1094. ** return code of SQLITE_CONSTRAINT. The
  1095. ** transaction is not rolled back and any
  1096. ** prior changes are retained.
  1097. **
  1098. ** any IGNORE The record number and data is popped from
  1099. ** the stack and there is an immediate jump
  1100. ** to label ignoreDest.
  1101. **
  1102. ** NOT NULL REPLACE The NULL value is replace by the default
  1103. ** value for that column. If the default value
  1104. ** is NULL, the action is the same as ABORT.
  1105. **
  1106. ** UNIQUE REPLACE The other row that conflicts with the row
  1107. ** being inserted is removed.
  1108. **
  1109. ** CHECK REPLACE Illegal. The results in an exception.
  1110. **
  1111. ** Which action to take is determined by the overrideError parameter.
  1112. ** Or if overrideError==OE_Default, then the pParse->onError parameter
  1113. ** is used. Or if pParse->onError==OE_Default then the onError value
  1114. ** for the constraint is used.
  1115. **
  1116. ** The calling routine must open a read/write cursor for pTab with
  1117. ** cursor number "baseCur". All indices of pTab must also have open
  1118. ** read/write cursors with cursor number baseCur+i for the i-th cursor.
  1119. ** Except, if there is no possibility of a REPLACE action then
  1120. ** cursors do not need to be open for indices where aRegIdx[i]==0.
  1121. */
  1122. void sqlite3GenerateConstraintChecks(
  1123. Parse *pParse, /* The parser context */
  1124. Table *pTab, /* the table into which we are inserting */
  1125. int baseCur, /* Index of a read/write cursor pointing at pTab */
  1126. int regRowid, /* Index of the range of input registers */
  1127. int *aRegIdx, /* Register used by each index. 0 for unused indices */
  1128. int rowidChng, /* True if the rowid might collide with existing entry */
  1129. int isUpdate, /* True for UPDATE, False for INSERT */
  1130. int overrideError, /* Override onError to this if not OE_Default */
  1131. int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
  1132. int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */
  1133. ){
  1134. int i; /* loop counter */
  1135. Vdbe *v; /* VDBE under constrution */
  1136. int nCol; /* Number of columns */
  1137. int onError; /* Conflict resolution strategy */
  1138. int j1; /* Addresss of jump instruction */
  1139. int j2 = 0, j3; /* Addresses of jump instructions */
  1140. int regData; /* Register containing first data column */
  1141. int iCur; /* Table cursor number */
  1142. Index *pIdx; /* Pointer to one of the indices */
  1143. sqlite3 *db; /* Database connection */
  1144. int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
  1145. int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid;
  1146. db = pParse->db;
  1147. v = sqlite3GetVdbe(pParse);
  1148. assert( v!=0 );
  1149. assert( pTab->pSelect==0 ); /* This table is not a VIEW */
  1150. nCol = pTab->nCol;
  1151. regData = regRowid + 1;
  1152. /* Test all NOT NULL constraints.
  1153. */
  1154. for(i=0; i<nCol; i++){
  1155. if( i==pTab->iPKey ){
  1156. continue;
  1157. }
  1158. onError = pTab->aCol[i].notNull;
  1159. if( onError==OE_None ) continue;
  1160. if( overrideError!=OE_Default ){
  1161. onError = overrideError;
  1162. }else if( onError==OE_Default ){
  1163. onError = OE_Abort;
  1164. }
  1165. if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
  1166. onError = OE_Abort;
  1167. }
  1168. assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
  1169. || onError==OE_Ignore || onError==OE_Replace );
  1170. switch( onError ){
  1171. case OE_Abort:
  1172. sqlite3MayAbort(pParse);
  1173. case OE_Rollback:
  1174. case OE_Fail: {
  1175. char *zMsg;
  1176. sqlite3VdbeAddOp3(v, OP_HaltIfNull,
  1177. SQLITE_CONSTRAINT_NOTNULL, onError, regData+i);
  1178. zMsg = sqlite3MPrintf(db, "%s.%s may not be NULL",
  1179. pTab->zName, pTab->aCol[i].zName);
  1180. sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC);
  1181. break;
  1182. }
  1183. case OE_Ignore: {
  1184. sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest);
  1185. break;
  1186. }
  1187. default: {
  1188. assert( onError==OE_Replace );
  1189. j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i);
  1190. sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i);
  1191. sqlite3VdbeJumpHere(v, j1);
  1192. break;
  1193. }
  1194. }
  1195. }
  1196. /* Test all CHECK constraints
  1197. */
  1198. #ifndef SQLITE_OMIT_CHECK
  1199. if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
  1200. ExprList *pCheck = pTab->pCheck;
  1201. pParse->ckBase = regData;
  1202. onError = overrideError!=OE_Default ? overrideError : OE_Abort;
  1203. for(i=0; i<pCheck->nExpr; i++){
  1204. int allOk = sqlite3VdbeMakeLabel(v);
  1205. sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
  1206. if( onError==OE_Ignore ){
  1207. sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
  1208. }else{
  1209. char *zConsName = pCheck->a[i].zName;
  1210. if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
  1211. if( zConsName ){
  1212. zConsName = sqlite3MPrintf(db, "constraint %s failed", zConsName);
  1213. }else{
  1214. zConsName = 0;
  1215. }
  1216. sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
  1217. onError, zConsName, P4_DYNAMIC);
  1218. }
  1219. sqlite3VdbeResolveLabel(v, allOk);
  1220. }
  1221. }
  1222. #endif /* !defined(SQLITE_OMIT_CHECK) */
  1223. /* If we have an INTEGER PRIMARY KEY, make sure the primary key
  1224. ** of the new record does not previously exist. Except, if this
  1225. ** is an UPDATE and the primary key is not changing, that is OK.
  1226. */
  1227. if( rowidChng ){
  1228. onError = pTab->keyConf;
  1229. if( overrideError!=OE_Default ){
  1230. onError = overrideError;
  1231. }else if( onError==OE_Default ){
  1232. onError = OE_Abort;
  1233. }
  1234. if( isUpdate ){
  1235. j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng);
  1236. }
  1237. j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid);
  1238. switch( onError ){
  1239. default: {
  1240. onError = OE_Abort;
  1241. /* Fall thru into the next case */
  1242. }
  1243. case OE_Rollback:
  1244. case OE_Abort:
  1245. case OE_Fail: {
  1246. sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_PRIMARYKEY,
  1247. onError, "PRIMARY KEY must be unique", P4_STATIC);
  1248. break;
  1249. }
  1250. case OE_Replace: {
  1251. /* If there are DELETE triggers on this table and the
  1252. ** recursive-triggers flag is set, call GenerateRowDelete() to
  1253. ** remove the conflicting row from the table. This will fire
  1254. ** the triggers and remove both the table and index b-tree entries.
  1255. **
  1256. ** Otherwise, if there are no triggers or the recursive-triggers
  1257. ** flag is not set, but the table has one or more indexes, call
  1258. ** GenerateRowIndexDelete(). This removes the index b-tree entries
  1259. ** only. The table b-tree entry will be replaced by the new entry
  1260. ** when it is inserted.
  1261. **
  1262. ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
  1263. ** also invoke MultiWrite() to indicate that this VDBE may require
  1264. ** statement rollback (if the statement is aborted after the delete
  1265. ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
  1266. ** but being more selective here allows statements like:
  1267. **
  1268. ** REPLACE INTO t(rowid) VALUES($newrowid)
  1269. **
  1270. ** to run without a statement journal if there are no indexes on the
  1271. ** table.
  1272. */
  1273. Trigger *pTrigger = 0;
  1274. if( db->flags&SQLITE_RecTriggers ){
  1275. pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
  1276. }
  1277. if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
  1278. sqlite3MultiWrite(pParse);
  1279. sqlite3GenerateRowDelete(
  1280. pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace
  1281. );
  1282. }else if( pTab->pIndex ){
  1283. sqlite3MultiWrite(pParse);
  1284. sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0);
  1285. }
  1286. seenReplace = 1;
  1287. break;
  1288. }
  1289. case OE_Ignore: {
  1290. assert( seenReplace==0 );
  1291. sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
  1292. break;
  1293. }
  1294. }
  1295. sqlite3VdbeJumpHere(v, j3);
  1296. if( isUpdate ){
  1297. sqlite3VdbeJumpHere(v, j2);
  1298. }
  1299. }
  1300. /* Test all UNIQUE constraints by creating entries for each UNIQUE
  1301. ** index and making sure that duplicate entries do not already exist.
  1302. ** Add the new records to the indices as we go.
  1303. */
  1304. for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
  1305. int regIdx;
  1306. int regR;
  1307. int addrSkipRow = 0;
  1308. if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */
  1309. if( pIdx->pPartIdxWhere ){
  1310. sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[iCur]);
  1311. addrSkipRow = sqlite3VdbeMakeLabel(v);
  1312. pParse->ckBase = regData;
  1313. sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrSkipRow,
  1314. SQLITE_JUMPIFNULL);
  1315. pParse->ckBase = 0;
  1316. }
  1317. /* Create a key for accessing the index entry */
  1318. regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
  1319. for(i=0; i<pIdx->nColumn; i++){
  1320. int idx = pIdx->aiColumn[i];
  1321. if( idx==pTab->iPKey ){
  1322. sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
  1323. }else{
  1324. sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i);
  1325. }
  1326. }
  1327. sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
  1328. sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]);
  1329. sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
  1330. sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
  1331. /* Find out what action to take in case there is an indexing conflict */
  1332. onError = pIdx->onError;
  1333. if( onError==OE_None ){
  1334. sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
  1335. sqlite3VdbeResolveLabel(v, addrSkipRow);
  1336. continue; /* pIdx is not a UNIQUE index */
  1337. }
  1338. if( overrideError!=OE_Default ){
  1339. onError = overrideError;
  1340. }else if( onError==OE_Default ){
  1341. onError = OE_Abort;
  1342. }
  1343. if( seenReplace ){
  1344. if( onError==OE_Ignore ) onError = OE_Replace;
  1345. else if( onError==OE_Fail ) onError = OE_Abort;
  1346. }
  1347. /* Check to see if the new index entry will be unique */
  1348. regR = sqlite3GetTempReg(pParse);
  1349. sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR);
  1350. j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0,
  1351. regR, SQLITE_INT_TO_PTR(regIdx),
  1352. P4_INT32);
  1353. sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
  1354. /* Generate code that executes if the new index entry is not unique */
  1355. assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
  1356. || onError==OE_Ignore || onError==OE_Replace );
  1357. switch( onError ){
  1358. case OE_Rollback:
  1359. case OE_Abort:
  1360. case OE_Fail: {
  1361. int j;
  1362. StrAccum errMsg;
  1363. const char *zSep;
  1364. char *zErr;
  1365. sqlite3StrAccumInit(&errMsg, 0, 0, 200);
  1366. errMsg.db = db;
  1367. zSep = pIdx->nColumn>1 ? "columns " : "column ";
  1368. for(j=0; j<pIdx->nColumn; j++){
  1369. char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
  1370. sqlite3StrAccumAppend(&errMsg, zSep, -1);
  1371. zSep = ", ";
  1372. sqlite3StrAccumAppend(&errMsg, zCol, -1);
  1373. }
  1374. sqlite3StrAccumAppend(&errMsg,
  1375. pIdx->nColumn>1 ? " are not unique" : " is not unique", -1);
  1376. zErr = sqlite3StrAccumFinish(&errMsg);
  1377. sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_UNIQUE,
  1378. onError, zErr, 0);
  1379. sqlite3DbFree(errMsg.db, zErr);
  1380. break;
  1381. }
  1382. case OE_Ignore: {
  1383. assert( seenReplace==0 );
  1384. sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
  1385. break;
  1386. }
  1387. default: {
  1388. Trigger *pTrigger = 0;
  1389. assert( onError==OE_Replace );
  1390. sqlite3MultiWrite(pParse);
  1391. if( db->flags&SQLITE_RecTriggers ){
  1392. pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
  1393. }
  1394. sqlite3GenerateRowDelete(
  1395. pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
  1396. );
  1397. seenReplace = 1;
  1398. break;
  1399. }
  1400. }
  1401. sqlite3VdbeJumpHere(v, j3);
  1402. sqlite3VdbeResolveLabel(v, addrSkipRow);
  1403. sqlite3ReleaseTempReg(pParse, regR);
  1404. }
  1405. if( pbMayReplace ){
  1406. *pbMayReplace = seenReplace;
  1407. }
  1408. }
  1409. /*
  1410. ** This routine generates code to finish the INSERT or UPDATE operation
  1411. ** that was started by a prior call to sqlite3GenerateConstraintChecks.
  1412. ** A consecutive range of registers starting at regRowid contains the
  1413. ** rowid and the content to be inserted.
  1414. **
  1415. ** The arguments to this routine should be the same as the first six
  1416. ** arguments to sqlite3GenerateConstraintChecks.
  1417. */
  1418. void sqlite3CompleteInsertion(
  1419. Parse *pParse, /* The parser context */
  1420. Table *pTab, /* the table into which we are inserting */
  1421. int baseCur, /* Index of a read/write cursor pointing at pTab */
  1422. int regRowid, /* Range of content */
  1423. int *aRegIdx, /* Register used by each index. 0 for unused indices */
  1424. int isUpdate, /* True for UPDATE, False for INSERT */
  1425. int appendBias, /* True if this is likely to be an append */
  1426. int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
  1427. ){
  1428. int i;
  1429. Vdbe *v;
  1430. Index *pIdx;
  1431. u8 pik_flags;
  1432. int regData;
  1433. int regRec;
  1434. v = sqlite3GetVdbe(pParse);
  1435. assert( v!=0 );
  1436. assert( pTab->pSelect==0 ); /* This table is not a VIEW */
  1437. for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  1438. if( aRegIdx[i]==0 ) continue;
  1439. if( pIdx->pPartIdxWhere ){
  1440. sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
  1441. }
  1442. sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
  1443. if( useSeekResult ){
  1444. sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  1445. }
  1446. }
  1447. regData = regRowid + 1;
  1448. regRec = sqlite3GetTempReg(pParse);
  1449. sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
  1450. sqlite3TableAffinityStr(v, pTab);
  1451. sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
  1452. if( pParse->nested ){
  1453. pik_flags = 0;
  1454. }else{
  1455. pik_flags = OPFLAG_NCHANGE;
  1456. pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
  1457. }
  1458. if( appendBias ){
  1459. pik_flags |= OPFLAG_APPEND;
  1460. }
  1461. if( useSeekResult ){
  1462. pik_flags |= OPFLAG_USESEEKRESULT;
  1463. }
  1464. sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid);
  1465. if( !pParse->nested ){
  1466. sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
  1467. }
  1468. sqlite3VdbeChangeP5(v, pik_flags);
  1469. }
  1470. /*
  1471. ** Generate code that will open cursors for a table and for all
  1472. ** indices of that table. The "baseCur" parameter is the cursor number used
  1473. ** for the table. Indices are opened on subsequent cursors.
  1474. **
  1475. ** Return the number of indices on the table.
  1476. */
  1477. int sqlite3OpenTableAndIndices(
  1478. Parse *pParse, /* Parsing context */
  1479. Table *pTab, /* Table to be opened */
  1480. int baseCur, /* Cursor number assigned to the table */
  1481. int op /* OP_OpenRead or OP_OpenWrite */
  1482. ){
  1483. int i;
  1484. int iDb;
  1485. Index *pIdx;
  1486. Vdbe *v;
  1487. if( IsVirtual(pTab) ) return 0;
  1488. iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  1489. v = sqlite3GetVdbe(pParse);
  1490. assert( v!=0 );
  1491. sqlite3OpenTable(pParse, baseCur, iDb, pTab, op);
  1492. for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  1493. KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
  1494. assert( pIdx->pSchema==pTab->pSchema );
  1495. sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb,
  1496. (char*)pKey, P4_KEYINFO_HANDOFF);
  1497. VdbeComment((v, "%s", pIdx->zName));
  1498. }
  1499. if( pParse->nTab<baseCur+i ){
  1500. pParse->nTab = baseCur+i;
  1501. }
  1502. return i-1;
  1503. }
  1504. #ifdef SQLITE_TEST
  1505. /*
  1506. ** The following global variable is incremented whenever the
  1507. ** transfer optimization is used. This is used for testing
  1508. ** purposes only - to make sure the transfer optimization really
  1509. ** is happening when it is suppose to.
  1510. */
  1511. int sqlite3_xferopt_count;
  1512. #endif /* SQLITE_TEST */
  1513. #ifndef SQLITE_OMIT_XFER_OPT
  1514. /*
  1515. ** Check to collation names to see if they are compatible.
  1516. */
  1517. static int xferCompatibleCollation(const char *z1, const char *z2){
  1518. if( z1==0 ){
  1519. return z2==0;
  1520. }
  1521. if( z2==0 ){
  1522. return 0;
  1523. }
  1524. return sqlite3StrICmp(z1, z2)==0;
  1525. }
  1526. /*
  1527. ** Check to see if index pSrc is compatible as a source of data
  1528. ** for index pDest in an insert transfer optimization. The rules
  1529. ** for a compatible index:
  1530. **
  1531. ** * The index is over the same set of columns
  1532. ** * The same DESC and ASC markings occurs on all columns
  1533. ** * The same onError processing (OE_Abort, OE_Ignore, etc)
  1534. ** * The same collating sequence on each column
  1535. ** * The index has the exact same WHERE clause
  1536. */
  1537. static int xferCompatibleIndex(Index *pDest, Index *pSrc){
  1538. int i;
  1539. assert( pDest && pSrc );
  1540. assert( pDest->pTable!=pSrc->pTable );
  1541. if( pDest->nColumn!=pSrc->nColumn ){
  1542. return 0; /* Different number of columns */
  1543. }
  1544. if( pDest->onError!=pSrc->onError ){
  1545. return 0; /* Different conflict resolution strategies */
  1546. }
  1547. for(i=0; i<pSrc->nColumn; i++){
  1548. if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
  1549. return 0; /* Different columns indexed */
  1550. }
  1551. if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
  1552. return 0; /* Different sort orders */
  1553. }
  1554. if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
  1555. return 0; /* Different collating sequences */
  1556. }
  1557. }
  1558. if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
  1559. return 0; /* Different WHERE clauses */
  1560. }
  1561. /* If no test above fails then the indices must be compatible */
  1562. return 1;
  1563. }
  1564. /*
  1565. ** Attempt the transfer optimization on INSERTs of the form
  1566. **
  1567. ** INSERT INTO tab1 SELECT * FROM tab2;
  1568. **
  1569. ** The xfer optimization transfers raw records from tab2 over to tab1.
  1570. ** Columns are not decoded and reassemblied, which greatly improves
  1571. ** performance. Raw index records are transferred in the same way.
  1572. **
  1573. ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
  1574. ** There are lots of rules for determining compatibility - see comments
  1575. ** embedded in the code for details.
  1576. **
  1577. ** This routine returns TRUE if the optimization is guaranteed to be used.
  1578. ** Sometimes the xfer optimization will only work if the destination table
  1579. ** is empty - a factor that can only be determined at run-time. In that
  1580. ** case, this routine generates code for the xfer optimization but also
  1581. ** does a test to see if the destination table is empty and jumps over the
  1582. ** xfer optimization code if the test fails. In that case, this routine
  1583. ** returns FALSE so that the caller will know to go ahead and generate
  1584. ** an unoptimized transfer. This routine also returns FALSE if there
  1585. ** is no chance that the xfer optimization can be applied.
  1586. **
  1587. ** This optimization is particularly useful at making VACUUM run faster.
  1588. */
  1589. static int xferOptimization(
  1590. Parse *pParse, /* Parser context */
  1591. Table *pDest, /* The table we are inserting into */
  1592. Select *pSelect, /* A SELECT statement to use as the data source */
  1593. int onError, /* How to handle constraint errors */
  1594. int iDbDest /* The database of pDest */
  1595. ){
  1596. ExprList *pEList; /* The result set of the SELECT */
  1597. Table *pSrc; /* The table in the FROM clause of SELECT */
  1598. Index *pSrcIdx, *pDestIdx; /* Source and destination indices */
  1599. struct SrcList_item *pItem; /* An element of pSelect->pSrc */
  1600. int i; /* Loop counter */
  1601. int iDbSrc; /* The database of pSrc */
  1602. int iSrc, iDest; /* Cursors from source and destination */
  1603. int addr1, addr2; /* Loop addresses */
  1604. int emptyDestTest; /* Address of test for empty pDest */
  1605. int emptySrcTest; /* Address of test for empty pSrc */
  1606. Vdbe *v; /* The VDBE we are building */
  1607. KeyInfo *pKey; /* Key information for an index */
  1608. int regAutoinc; /* Memory register used by AUTOINC */
  1609. int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */
  1610. int regData, regRowid; /* Registers holding data and rowid */
  1611. if( pSelect==0 ){
  1612. return 0; /* Must be of the form INSERT INTO ... SELECT ... */
  1613. }
  1614. if( sqlite3TriggerList(pParse, pDest) ){
  1615. return 0; /* tab1 must not have triggers */
  1616. }
  1617. #ifndef SQLITE_OMIT_VIRTUALTABLE
  1618. if( pDest->tabFlags & TF_Virtual ){
  1619. return 0; /* tab1 must not be a virtual table */
  1620. }
  1621. #endif
  1622. if( onError==OE_Default ){
  1623. if( pDest->iPKey>=0 ) onError = pDest->keyConf;
  1624. if( onError==OE_Default ) onError = OE_Abort;
  1625. }
  1626. assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
  1627. if( pSelect->pSrc->nSrc!=1 ){
  1628. return 0; /* FROM clause must have exactly one term */
  1629. }
  1630. if( pSelect->pSrc->a[0].pSelect ){
  1631. return 0; /* FROM clause cannot contain a subquery */
  1632. }
  1633. if( pSelect->pWhere ){
  1634. return 0; /* SELECT may not have a WHERE clause */
  1635. }
  1636. if( pSelect->pOrderBy ){
  1637. return 0; /* SELECT may not have an ORDER BY clause */
  1638. }
  1639. /* Do not need to test for a HAVING clause. If HAVING is present but
  1640. ** there is no ORDER BY, we will get an error. */
  1641. if( pSelect->pGroupBy ){
  1642. return 0; /* SELECT may not have a GROUP BY clause */
  1643. }
  1644. if( pSelect->pLimit ){
  1645. return 0; /* SELECT may not have a LIMIT clause */
  1646. }
  1647. assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */
  1648. if( pSelect->pPrior ){
  1649. return 0; /* SELECT may not be a compound query */
  1650. }
  1651. if( pSelect->selFlags & SF_Distinct ){
  1652. return 0; /* SELECT may not be DISTINCT */
  1653. }
  1654. pEList = pSelect->pEList;
  1655. assert( pEList!=0 );
  1656. if( pEList->nExpr!=1 ){
  1657. return 0; /* The result set must have exactly one column */
  1658. }
  1659. assert( pEList->a[0].pExpr );
  1660. if( pEList->a[0].pExpr->op!=TK_ALL ){
  1661. return 0; /* The result set must be the special operator "*" */
  1662. }
  1663. /* At this point we have established that the statement is of the
  1664. ** correct syntactic form to participate in this optimization. Now
  1665. ** we have to check the semantics.
  1666. */
  1667. pItem = pSelect->pSrc->a;
  1668. pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
  1669. if( pSrc==0 ){
  1670. return 0; /* FROM clause does not contain a real table */
  1671. }
  1672. if( pSrc==pDest ){
  1673. return 0; /* tab1 and tab2 may not be the same table */
  1674. }
  1675. #ifndef SQLITE_OMIT_VIRTUALTABLE
  1676. if( pSrc->tabFlags & TF_Virtual ){
  1677. return 0; /* tab2 must not be a virtual table */
  1678. }
  1679. #endif
  1680. if( pSrc->pSelect ){
  1681. return 0; /* tab2 may not be a view */
  1682. }
  1683. if( pDest->nCol!=pSrc->nCol ){
  1684. return 0; /* Number of columns must be the same in tab1 and tab2 */
  1685. }
  1686. if( pDest->iPKey!=pSrc->iPKey ){
  1687. return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
  1688. }
  1689. for(i=0; i<pDest->nCol; i++){
  1690. if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
  1691. return 0; /* Affinity must be the same on all columns */
  1692. }
  1693. if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
  1694. return 0; /* Collating sequence must be the same on all columns */
  1695. }
  1696. if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
  1697. return 0; /* tab2 must be NOT NULL if tab1 is */
  1698. }
  1699. }
  1700. for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
  1701. if( pDestIdx->onError!=OE_None ){
  1702. destHasUniqueIdx = 1;
  1703. }
  1704. for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
  1705. if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
  1706. }
  1707. if( pSrcIdx==0 ){
  1708. return 0; /* pDestIdx has no corresponding index in pSrc */
  1709. }
  1710. }
  1711. #ifndef SQLITE_OMIT_CHECK
  1712. if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
  1713. return 0; /* Tables have different CHECK constraints. Ticket #2252 */
  1714. }
  1715. #endif
  1716. #ifndef SQLITE_OMIT_FOREIGN_KEY
  1717. /* Disallow the transfer optimization if the destination table constains
  1718. ** any foreign key constraints. This is more restrictive than necessary.
  1719. ** But the main beneficiary of the transfer optimization is the VACUUM
  1720. ** command, and the VACUUM command disables foreign key constraints. So
  1721. ** the extra complication to make this rule less restrictive is probably
  1722. ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
  1723. */
  1724. if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
  1725. return 0;
  1726. }
  1727. #endif
  1728. if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
  1729. return 0; /* xfer opt does not play well with PRAGMA count_changes */
  1730. }
  1731. /* If we get this far, it means that the xfer optimization is at
  1732. ** least a possibility, though it might only work if the destination
  1733. ** table (tab1) is initially empty.
  1734. */
  1735. #ifdef SQLITE_TEST
  1736. sqlite3_xferopt_count++;
  1737. #endif
  1738. iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
  1739. v = sqlite3GetVdbe(pParse);
  1740. sqlite3CodeVerifySchema(pParse, iDbSrc);
  1741. iSrc = pParse->nTab++;
  1742. iDest = pParse->nTab++;
  1743. regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
  1744. sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
  1745. if( (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
  1746. || destHasUniqueIdx /* (2) */
  1747. || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
  1748. ){
  1749. /* In some circumstances, we are able to run the xfer optimization
  1750. ** only if the destination table is initially empty. This code makes
  1751. ** that determination. Conditions under which the destination must
  1752. ** be empty:
  1753. **
  1754. ** (1) There is no INTEGER PRIMARY KEY but there are indices.
  1755. ** (If the destination is not initially empty, the rowid fields
  1756. ** of index entries might need to change.)
  1757. **
  1758. ** (2) The destination has a unique index. (The xfer optimization
  1759. ** is unable to test uniqueness.)
  1760. **
  1761. ** (3) onError is something other than OE_Abort and OE_Rollback.
  1762. */
  1763. addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
  1764. emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
  1765. sqlite3VdbeJumpHere(v, addr1);
  1766. }else{
  1767. emptyDestTest = 0;
  1768. }
  1769. sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
  1770. emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
  1771. regData = sqlite3GetTempReg(pParse);
  1772. regRowid = sqlite3GetTempReg(pParse);
  1773. if( pDest->iPKey>=0 ){
  1774. addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
  1775. addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
  1776. sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_PRIMARYKEY,
  1777. onError, "PRIMARY KEY must be unique", P4_STATIC);
  1778. sqlite3VdbeJumpHere(v, addr2);
  1779. autoIncStep(pParse, regAutoinc, regRowid);
  1780. }else if( pDest->pIndex==0 ){
  1781. addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
  1782. }else{
  1783. addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
  1784. assert( (pDest->tabFlags & TF_Autoincrement)==0 );
  1785. }
  1786. sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
  1787. sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
  1788. sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
  1789. sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
  1790. sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
  1791. for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
  1792. for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
  1793. if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
  1794. }
  1795. assert( pSrcIdx );
  1796. sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
  1797. sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  1798. pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx);
  1799. sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc,
  1800. (char*)pKey, P4_KEYINFO_HANDOFF);
  1801. VdbeComment((v, "%s", pSrcIdx->zName));
  1802. pKey = sqlite3IndexKeyinfo(pParse, pDestIdx);
  1803. sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest,
  1804. (char*)pKey, P4_KEYINFO_HANDOFF);
  1805. sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
  1806. VdbeComment((v, "%s", pDestIdx->zName));
  1807. addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
  1808. sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
  1809. sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
  1810. sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
  1811. sqlite3VdbeJumpHere(v, addr1);
  1812. }
  1813. sqlite3VdbeJumpHere(v, emptySrcTest);
  1814. sqlite3ReleaseTempReg(pParse, regRowid);
  1815. sqlite3ReleaseTempReg(pParse, regData);
  1816. sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
  1817. sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  1818. if( emptyDestTest ){
  1819. sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
  1820. sqlite3VdbeJumpHere(v, emptyDestTest);
  1821. sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  1822. return 0;
  1823. }else{
  1824. return 1;
  1825. }
  1826. }
  1827. #endif /* SQLITE_OMIT_XFER_OPT */