0000: 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
0010: 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
0020: 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
0030: 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
0040: 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
0050: 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
0060: 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
0070: 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
0080: 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
0090: 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
00a0: 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
00b0: 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
00c0: 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
00d0: 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
00e0: 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
00f0: 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
0100: 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
0110: 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
0120: 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 ******.** This f
0180: 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 ile contains C c
0190: 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 ode routines tha
01a0: 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 t are called by
01b0: 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f the parser.** to
01c0: 20 68 61 6e 64 6c 65 20 55 50 44 41 54 45 20 73 handle UPDATE s
01d0: 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 69 tatements..*/.#i
01e0: 6e 63 6c 75 64 65 20 22 73 71 6c 69 74 65 49 6e nclude "sqliteIn
01f0: 74 2e 68 22 0a 0a 23 69 66 6e 64 65 66 20 53 51 t.h"..#ifndef SQ
0200: 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
0210: 4c 54 41 42 4c 45 0a 2f 2a 20 46 6f 72 77 61 72 LTABLE./* Forwar
0220: 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f d declaration */
0230: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 .static void upd
0240: 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28 ateVirtualTable(
0250: 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
0260: 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 , /* The p
0270: 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a arsing context *
0280: 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 /. SrcList *pSr
0290: 63 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 c, /* The
02a0: 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f virtual table to
02b0: 20 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a be modified */.
02c0: 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 Table *pTab,
02d0: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 /* The vi
02e0: 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 rtual table */.
02f0: 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e ExprList *pChan
0300: 67 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c ges, /* The col
0310: 75 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69 umns to change i
0320: 6e 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 n the UPDATE sta
0330: 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 tement */. Expr
0340: 20 2a 70 52 6f 77 69 64 45 78 70 72 2c 20 20 20 *pRowidExpr,
0350: 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75 /* Expression u
0360: 73 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 sed to recompute
0370: 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 the rowid */.
0380: 69 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20 int *aXRef,
0390: 20 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 /* Mapping
03a0: 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 from columns of
03b0: 70 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20 pTab to entries
03c0: 69 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20 in pChanges */.
03d0: 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 Expr *pWhere,
03e0: 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63 /* WHERE c
03f0: 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44 lause of the UPD
0400: 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f ATE statement */
0410: 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20 . int onError
0420: 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20 43 4f /* ON CO
0430: 4e 46 4c 49 43 54 20 73 74 72 61 74 65 67 79 20 NFLICT strategy
0440: 2a 2f 0a 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 */.);.#endif /*
0450: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 SQLITE_OMIT_VIRT
0460: 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a UALTABLE */../*.
0470: 2a 2a 20 54 68 65 20 6d 6f 73 74 20 72 65 63 65 ** The most rece
0480: 6e 74 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 ntly coded instr
0490: 75 63 74 69 6f 6e 20 77 61 73 20 61 6e 20 4f 50 uction was an OP
04a0: 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 _Column to retri
04b0: 65 76 65 20 74 68 65 0a 2a 2a 20 69 2d 74 68 20 eve the.** i-th
04c0: 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 column of table
04d0: 70 54 61 62 2e 20 54 68 69 73 20 72 6f 75 74 69 pTab. This routi
04e0: 6e 65 20 73 65 74 73 20 74 68 65 20 50 34 20 70 ne sets the P4 p
04f0: 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 arameter of the
0500: 0a 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f .** OP_Column to
0510: 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c the default val
0520: 75 65 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a ue, if any..**.*
0530: 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 * The default va
0540: 6c 75 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 lue of a column
0550: 69 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 is specified by
0560: 61 20 44 45 46 41 55 4c 54 20 63 6c 61 75 73 65 a DEFAULT clause
0570: 20 69 6e 20 74 68 65 20 0a 2a 2a 20 63 6f 6c 75 in the .** colu
0580: 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 54 mn definition. T
0590: 68 69 73 20 77 61 73 20 65 69 74 68 65 72 20 73 his was either s
05a0: 75 70 70 6c 69 65 64 20 62 79 20 74 68 65 20 75 upplied by the u
05b0: 73 65 72 20 77 68 65 6e 20 74 68 65 20 74 61 62 ser when the tab
05c0: 6c 65 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 65 le.** was create
05d0: 64 2c 20 6f 72 20 61 64 64 65 64 20 6c 61 74 65 d, or added late
05e0: 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 64 r to the table d
05f0: 65 66 69 6e 69 74 69 6f 6e 20 62 79 20 61 6e 20 efinition by an
0600: 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2a 20 63 ALTER TABLE.** c
0610: 6f 6d 6d 61 6e 64 2e 20 49 66 20 74 68 65 20 6c ommand. If the l
0620: 61 74 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 atter, then the
0630: 72 6f 77 2d 72 65 63 6f 72 64 73 20 69 6e 20 74 row-records in t
0640: 68 65 20 74 61 62 6c 65 20 62 74 72 65 65 20 6f he table btree o
0650: 6e 20 64 69 73 6b 0a 2a 2a 20 6d 61 79 20 6e 6f n disk.** may no
0660: 74 20 63 6f 6e 74 61 69 6e 20 61 20 76 61 6c 75 t contain a valu
0670: 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e e for the column
0680: 20 61 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74 and the default
0690: 20 76 61 6c 75 65 2c 20 74 61 6b 65 6e 0a 2a 2a value, taken.**
06a0: 20 66 72 6f 6d 20 74 68 65 20 50 34 20 70 61 72 from the P4 par
06b0: 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 4f 50 ameter of the OP
06c0: 5f 43 6f 6c 75 6d 6e 20 69 6e 73 74 72 75 63 74 _Column instruct
06d0: 69 6f 6e 2c 20 69 73 20 72 65 74 75 72 6e 65 64 ion, is returned
06e0: 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 49 66 20 instead..** If
06f0: 74 68 65 20 66 6f 72 6d 65 72 2c 20 74 68 65 6e the former, then
0700: 20 61 6c 6c 20 72 6f 77 2d 72 65 63 6f 72 64 73 all row-records
0710: 20 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 are guaranteed
0720: 74 6f 20 69 6e 63 6c 75 64 65 20 61 20 76 61 6c to include a val
0730: 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f ue.** for the co
0740: 6c 75 6d 6e 20 61 6e 64 20 74 68 65 20 50 34 20 lumn and the P4
0750: 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 72 65 71 value is not req
0760: 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6c uired..**.** Col
0770: 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 umn definitions
0780: 63 72 65 61 74 65 64 20 62 79 20 61 6e 20 41 4c created by an AL
0790: 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e TER TABLE comman
07a0: 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 d may only have
07b0: 0a 2a 2a 20 6c 69 74 65 72 61 6c 20 64 65 66 61 .** literal defa
07c0: 75 6c 74 20 76 61 6c 75 65 73 20 73 70 65 63 69 ult values speci
07d0: 66 69 65 64 3a 20 61 20 6e 75 6d 62 65 72 2c 20 fied: a number,
07e0: 6e 75 6c 6c 20 6f 72 20 61 20 73 74 72 69 6e 67 null or a string
07f0: 2e 20 28 49 66 20 61 20 6d 6f 72 65 0a 2a 2a 20 . (If a more.**
0800: 63 6f 6d 70 6c 69 63 61 74 65 64 20 64 65 66 61 complicated defa
0810: 75 6c 74 20 65 78 70 72 65 73 73 69 6f 6e 20 76 ult expression v
0820: 61 6c 75 65 20 77 61 73 20 70 72 6f 76 69 64 65 alue was provide
0830: 64 2c 20 69 74 20 69 73 20 65 76 61 6c 75 61 74 d, it is evaluat
0840: 65 64 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 ed .** when the
0850: 41 4c 54 45 52 20 54 41 42 4c 45 20 69 73 20 65 ALTER TABLE is e
0860: 78 65 63 75 74 65 64 20 61 6e 64 20 6f 6e 65 20 xecuted and one
0870: 6f 66 20 74 68 65 20 6c 69 74 65 72 61 6c 20 76 of the literal v
0880: 61 6c 75 65 73 20 77 72 69 74 74 65 6e 0a 2a 2a alues written.**
0890: 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 into the sqlite
08a0: 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 29 0a _master table.).
08b0: 2a 2a 0a 2a 2a 20 54 68 65 72 65 66 6f 72 65 2c **.** Therefore,
08c0: 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 the P4 paramete
08d0: 72 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 r is only requir
08e0: 65 64 20 69 66 20 74 68 65 20 64 65 66 61 75 6c ed if the defaul
08f0: 74 20 76 61 6c 75 65 20 66 6f 72 0a 2a 2a 20 74 t value for.** t
0900: 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20 6c he column is a l
0910: 69 74 65 72 61 6c 20 6e 75 6d 62 65 72 2c 20 73 iteral number, s
0920: 74 72 69 6e 67 20 6f 72 20 6e 75 6c 6c 2e 20 54 tring or null. T
0930: 68 65 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 he sqlite3ValueF
0940: 72 6f 6d 45 78 70 72 28 29 0a 2a 2a 20 66 75 6e romExpr().** fun
0950: 63 74 69 6f 6e 20 69 73 20 63 61 70 61 62 6c 65 ction is capable
0960: 20 6f 66 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 of transforming
0970: 20 74 68 65 73 65 20 74 79 70 65 73 20 6f 66 20 these types of
0980: 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 74 6f expressions into
0990: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 .** sqlite3_valu
09a0: 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a e objects..**.**
09b0: 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 69 52 If parameter iR
09c0: 65 67 20 69 73 20 6e 6f 74 20 6e 65 67 61 74 69 eg is not negati
09d0: 76 65 2c 20 63 6f 64 65 20 61 6e 20 4f 50 5f 52 ve, code an OP_R
09e0: 65 61 6c 41 66 66 69 6e 69 74 79 20 69 6e 73 74 ealAffinity inst
09f0: 72 75 63 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 72 65 ruction.** on re
0a00: 67 69 73 74 65 72 20 69 52 65 67 2e 20 54 68 69 gister iReg. Thi
0a10: 73 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 61 s is used when a
0a20: 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 n equivalent int
0a30: 65 67 65 72 20 76 61 6c 75 65 20 69 73 20 0a 2a eger value is .*
0a40: 2a 20 73 74 6f 72 65 64 20 69 6e 20 70 6c 61 63 * stored in plac
0a50: 65 20 6f 66 20 61 6e 20 38 2d 62 79 74 65 20 66 e of an 8-byte f
0a60: 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 loating point va
0a70: 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 lue in order to
0a80: 73 61 76 65 20 0a 2a 2a 20 73 70 61 63 65 2e 0a save .** space..
0a90: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 43 */.void sqlite3C
0aa0: 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 62 olumnDefault(Vdb
0ab0: 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a 70 54 61 e *v, Table *pTa
0ac0: 62 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 69 52 b, int i, int iR
0ad0: 65 67 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 eg){. assert( p
0ae0: 54 61 62 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 Tab!=0 );. if(
0af0: 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 !pTab->pSelect )
0b00: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 {. sqlite3_va
0b10: 6c 75 65 20 2a 70 56 61 6c 75 65 20 3d 20 30 3b lue *pValue = 0;
0b20: 0a 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e . u8 enc = EN
0b30: 43 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 C(sqlite3VdbeDb(
0b40: 76 29 29 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 v));. Column
0b50: 2a 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 *pCol = &pTab->a
0b60: 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20 56 64 62 65 Col[i];. Vdbe
0b70: 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 2e Comment((v, "%s.
0b80: 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 %s", pTab->zName
0b90: 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 29 3b , pCol->zName));
0ba0: 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 70 . assert( i<p
0bb0: 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 Tab->nCol );.
0bc0: 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f sqlite3ValueFro
0bd0: 6d 45 78 70 72 28 73 71 6c 69 74 65 33 56 64 62 mExpr(sqlite3Vdb
0be0: 65 44 62 28 76 29 2c 20 70 43 6f 6c 2d 3e 70 44 eDb(v), pCol->pD
0bf0: 66 6c 74 2c 20 65 6e 63 2c 20 0a 20 20 20 20 20 flt, enc, .
0c00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0c10: 20 20 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 pCol->affini
0c20: 74 79 2c 20 26 70 56 61 6c 75 65 29 3b 0a 20 20 ty, &pValue);.
0c30: 20 20 69 66 28 20 70 56 61 6c 75 65 20 29 7b 0a if( pValue ){.
0c40: 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
0c50: 65 41 70 70 65 6e 64 50 34 28 76 2c 20 70 56 61 eAppendP4(v, pVa
0c60: 6c 75 65 2c 20 50 34 5f 4d 45 4d 29 3b 0a 20 20 lue, P4_MEM);.
0c70: 20 20 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 }. }.#ifndef
0c80: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 SQLITE_OMIT_FLOA
0c90: 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 69 66 28 TING_POINT. if(
0ca0: 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 pTab->aCol[i].a
0cb0: 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f ffinity==SQLITE_
0cc0: 41 46 46 5f 52 45 41 4c 20 29 7b 0a 20 20 20 20 AFF_REAL ){.
0cd0: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
0ce0: 31 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 1(v, OP_RealAffi
0cf0: 6e 69 74 79 2c 20 69 52 65 67 29 3b 0a 20 20 7d nity, iReg);. }
0d00: 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a .#endif.}../*.**
0d10: 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 Check to see if
0d20: 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20 6f 66 20 column iCol of
0d30: 69 6e 64 65 78 20 70 49 64 78 20 72 65 66 65 72 index pIdx refer
0d40: 65 6e 63 65 73 20 61 6e 79 20 6f 66 20 74 68 65 ences any of the
0d50: 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 20 64 65 66 69 .** columns defi
0d60: 6e 65 64 20 62 79 20 61 58 52 65 66 20 61 6e 64 ned by aXRef and
0d70: 20 63 68 6e 67 52 6f 77 69 64 2e 20 20 52 65 74 chngRowid. Ret
0d80: 75 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 64 urn true if it d
0d90: 6f 65 73 0a 2a 2a 20 61 6e 64 20 66 61 6c 73 65 oes.** and false
0da0: 20 69 66 20 6e 6f 74 2e 20 20 54 68 69 73 20 69 if not. This i
0db0: 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f s an optimizatio
0dc0: 6e 2e 20 20 46 61 6c 73 65 2d 70 6f 73 69 74 69 n. False-positi
0dd0: 76 65 73 20 61 72 65 20 61 0a 2a 2a 20 70 65 72 ves are a.** per
0de0: 66 6f 72 6d 61 6e 63 65 20 64 65 67 72 61 64 61 formance degrada
0df0: 74 69 6f 6e 2c 20 62 75 74 20 66 61 6c 73 65 2d tion, but false-
0e00: 6e 65 67 61 74 69 76 65 73 20 63 61 6e 20 72 65 negatives can re
0e10: 73 75 6c 74 20 69 6e 20 61 20 63 6f 72 72 75 70 sult in a corrup
0e20: 74 0a 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 69 t.** index and i
0e30: 6e 63 6f 72 72 65 63 74 20 61 6e 73 77 65 72 73 ncorrect answers
0e40: 2e 0a 2a 2a 0a 2a 2a 20 61 58 52 65 66 5b 6a 5d ..**.** aXRef[j]
0e50: 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 6e 65 67 will be non-neg
0e60: 61 74 69 76 65 20 69 66 20 63 6f 6c 75 6d 6e 20 ative if column
0e70: 6a 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 j of the origina
0e80: 6c 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 62 65 l table is.** be
0e90: 69 6e 67 20 75 70 64 61 74 65 64 2e 20 20 63 68 ing updated. ch
0ea0: 6e 67 52 6f 77 69 64 20 77 69 6c 6c 20 62 65 20 ngRowid will be
0eb0: 74 72 75 65 20 69 66 20 74 68 65 20 72 6f 77 69 true if the rowi
0ec0: 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 d of the table i
0ed0: 73 0a 2a 2a 20 62 65 69 6e 67 20 75 70 64 61 74 s.** being updat
0ee0: 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
0ef0: 74 20 69 6e 64 65 78 43 6f 6c 75 6d 6e 49 73 42 t indexColumnIsB
0f00: 65 69 6e 67 55 70 64 61 74 65 64 28 0a 20 20 49 eingUpdated(. I
0f10: 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20 ndex *pIdx,
0f20: 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 74 6f /* The index to
0f30: 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 check */. int
0f40: 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a iCol, /*
0f50: 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 Which column of
0f60: 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 63 68 the index to ch
0f70: 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 eck */. int *aX
0f80: 52 65 66 2c 20 20 20 20 20 20 20 2f 2a 20 61 58 Ref, /* aX
0f90: 52 65 66 5b 6a 5d 3e 3d 30 20 69 66 20 63 6f 6c Ref[j]>=0 if col
0fa0: 75 6d 6e 20 6a 20 69 73 20 62 65 69 6e 67 20 75 umn j is being u
0fb0: 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 pdated */. int
0fc0: 63 68 6e 67 52 6f 77 69 64 20 20 20 20 20 2f 2a chngRowid /*
0fd0: 20 74 72 75 65 20 69 66 20 74 68 65 20 72 6f 77 true if the row
0fe0: 69 64 20 69 73 20 62 65 69 6e 67 20 75 70 64 61 id is being upda
0ff0: 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 31 36 20 ted */.){. i16
1000: 69 49 64 78 43 6f 6c 20 3d 20 70 49 64 78 2d 3e iIdxCol = pIdx->
1010: 61 69 43 6f 6c 75 6d 6e 5b 69 43 6f 6c 5d 3b 0a aiColumn[iCol];.
1020: 20 20 61 73 73 65 72 74 28 20 69 49 64 78 43 6f assert( iIdxCo
1030: 6c 21 3d 58 4e 5f 52 4f 57 49 44 20 29 3b 20 2f l!=XN_ROWID ); /
1040: 2a 20 43 61 6e 6e 6f 74 20 69 6e 64 65 78 20 72 * Cannot index r
1050: 6f 77 69 64 20 2a 2f 0a 20 20 69 66 28 20 69 49 owid */. if( iI
1060: 64 78 43 6f 6c 3e 3d 30 20 29 7b 0a 20 20 20 20 dxCol>=0 ){.
1070: 72 65 74 75 72 6e 20 61 58 52 65 66 5b 69 49 64 return aXRef[iId
1080: 78 43 6f 6c 5d 3e 3d 30 3b 0a 20 20 7d 0a 20 20 xCol]>=0;. }.
1090: 61 73 73 65 72 74 28 20 69 49 64 78 43 6f 6c 3d assert( iIdxCol=
10a0: 3d 58 4e 5f 45 58 50 52 20 29 3b 0a 20 20 61 73 =XN_EXPR );. as
10b0: 73 65 72 74 28 20 70 49 64 78 2d 3e 61 43 6f 6c sert( pIdx->aCol
10c0: 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 Expr!=0 );. ass
10d0: 65 72 74 28 20 70 49 64 78 2d 3e 61 43 6f 6c 45 ert( pIdx->aColE
10e0: 78 70 72 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 xpr->a[iCol].pEx
10f0: 70 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 pr!=0 );. retur
1100: 6e 20 73 71 6c 69 74 65 33 45 78 70 72 52 65 66 n sqlite3ExprRef
1110: 65 72 65 6e 63 65 73 55 70 64 61 74 65 64 43 6f erencesUpdatedCo
1120: 6c 75 6d 6e 28 70 49 64 78 2d 3e 61 43 6f 6c 45 lumn(pIdx->aColE
1130: 78 70 72 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 xpr->a[iCol].pEx
1140: 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 pr,.
1150: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1160: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1170: 61 58 52 65 66 2c 63 68 6e 67 52 6f 77 69 64 29 aXRef,chngRowid)
1180: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b ;.}../*.** Check
1190: 20 74 6f 20 73 65 65 20 69 66 20 69 6e 64 65 78 to see if index
11a0: 20 70 49 64 78 20 69 73 20 61 20 70 61 72 74 69 pIdx is a parti
11b0: 61 6c 20 69 6e 64 65 78 20 77 68 6f 73 65 20 63 al index whose c
11c0: 6f 6e 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 65 78 onditional.** ex
11d0: 70 72 65 73 73 69 6f 6e 20 6d 69 67 68 74 20 63 pression might c
11e0: 68 61 6e 67 65 20 76 61 6c 75 65 73 20 64 75 65 hange values due
11f0: 20 74 6f 20 61 6e 20 55 50 44 41 54 45 2e 20 20 to an UPDATE.
1200: 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 0a 2a Return true if.*
1210: 2a 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 73 * the index is s
1220: 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 ubject to change
1230: 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 74 68 and false if th
1240: 65 20 69 6e 64 65 78 20 69 73 20 67 75 61 72 61 e index is guara
1250: 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 nteed.** to be u
1260: 6e 63 68 61 6e 67 65 64 2e 20 20 54 68 69 73 20 nchanged. This
1270: 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 is an optimizati
1280: 6f 6e 2e 20 20 46 61 6c 73 65 2d 70 6f 73 69 74 on. False-posit
1290: 69 76 65 73 20 61 72 65 20 61 0a 2a 2a 20 70 65 ives are a.** pe
12a0: 72 66 6f 72 6d 61 6e 63 65 20 64 65 67 72 61 64 rformance degrad
12b0: 61 74 69 6f 6e 2c 20 62 75 74 20 66 61 6c 73 65 ation, but false
12c0: 2d 6e 65 67 61 74 69 76 65 73 20 63 61 6e 20 72 -negatives can r
12d0: 65 73 75 6c 74 20 69 6e 20 61 20 63 6f 72 72 75 esult in a corru
12e0: 70 74 0a 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 pt.** index and
12f0: 69 6e 63 6f 72 72 65 63 74 20 61 6e 73 77 65 72 incorrect answer
1300: 73 2e 0a 2a 2a 0a 2a 2a 20 61 58 52 65 66 5b 6a s..**.** aXRef[j
1310: 5d 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 6e 65 ] will be non-ne
1320: 67 61 74 69 76 65 20 69 66 20 63 6f 6c 75 6d 6e gative if column
1330: 20 6a 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e j of the origin
1340: 61 6c 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 62 al table is.** b
1350: 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20 20 63 eing updated. c
1360: 68 6e 67 52 6f 77 69 64 20 77 69 6c 6c 20 62 65 hngRowid will be
1370: 20 74 72 75 65 20 69 66 20 74 68 65 20 72 6f 77 true if the row
1380: 69 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 id of the table
1390: 69 73 0a 2a 2a 20 62 65 69 6e 67 20 75 70 64 61 is.** being upda
13a0: 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ted..*/.static i
13b0: 6e 74 20 69 6e 64 65 78 57 68 65 72 65 43 6c 61 nt indexWhereCla
13c0: 75 73 65 4d 69 67 68 74 43 68 61 6e 67 65 28 0a useMightChange(.
13d0: 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 Index *pIdx,
13e0: 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 /* The index
13f0: 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 to check */. i
1400: 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20 20 nt *aXRef,
1410: 20 2f 2a 20 61 58 52 65 66 5b 6a 5d 3e 3d 30 20 /* aXRef[j]>=0
1420: 69 66 20 63 6f 6c 75 6d 6e 20 6a 20 69 73 20 62 if column j is b
1430: 65 69 6e 67 20 75 70 64 61 74 65 64 20 2a 2f 0a eing updated */.
1440: 20 20 69 6e 74 20 63 68 6e 67 52 6f 77 69 64 20 int chngRowid
1450: 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66 20 74 /* true if t
1460: 68 65 20 72 6f 77 69 64 20 69 73 20 62 65 69 6e he rowid is bein
1470: 67 20 75 70 64 61 74 65 64 20 2a 2f 0a 29 7b 0a g updated */.){.
1480: 20 20 69 66 28 20 70 49 64 78 2d 3e 70 50 61 72 if( pIdx->pPar
1490: 74 49 64 78 57 68 65 72 65 3d 3d 30 20 29 20 72 tIdxWhere==0 ) r
14a0: 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 eturn 0;. retur
14b0: 6e 20 73 71 6c 69 74 65 33 45 78 70 72 52 65 66 n sqlite3ExprRef
14c0: 65 72 65 6e 63 65 73 55 70 64 61 74 65 64 43 6f erencesUpdatedCo
14d0: 6c 75 6d 6e 28 70 49 64 78 2d 3e 70 50 61 72 74 lumn(pIdx->pPart
14e0: 49 64 78 57 68 65 72 65 2c 0a 20 20 20 20 20 20 IdxWhere,.
14f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1500: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1510: 20 20 20 20 20 20 61 58 52 65 66 2c 20 63 68 6e aXRef, chn
1520: 67 52 6f 77 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a gRowid);.}../*.*
1530: 2a 20 50 72 6f 63 65 73 73 20 61 6e 20 55 50 44 * Process an UPD
1540: 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a ATE statement..*
1550: 2a 0a 2a 2a 20 20 20 55 50 44 41 54 45 20 4f 52 *.** UPDATE OR
1560: 20 49 47 4e 4f 52 45 20 74 61 62 6c 65 5f 77 78 IGNORE table_wx
1570: 79 7a 20 53 45 54 20 61 3d 62 2c 20 63 3d 64 20 yz SET a=b, c=d
1580: 57 48 45 52 45 20 65 3c 35 20 41 4e 44 20 66 20 WHERE e<5 AND f
1590: 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 NOT NULL;.**
15a0: 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 2f 20 \_______/
15b0: 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c \________/ \
15c0: 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f ______/ \_
15d0: 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f _______________/
15e0: 0a 2a 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e .* on
15f0: 45 72 72 6f 72 20 20 20 70 54 61 62 4c 69 73 74 Error pTabList
1600: 20 20 20 20 20 20 70 43 68 61 6e 67 65 73 20 20 pChanges
1610: 20 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72 pWher
1620: 65 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 e.*/.void sqlite
1630: 33 55 70 64 61 74 65 28 0a 20 20 50 61 72 73 65 3Update(. Parse
1640: 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
1650: 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 /* The parser
1660: 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 context */. Src
1670: 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 List *pTabList,
1680: 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 /* The table
1690: 20 69 6e 20 77 68 69 63 68 20 77 65 20 73 68 6f in which we sho
16a0: 75 6c 64 20 63 68 61 6e 67 65 20 74 68 69 6e 67 uld change thing
16b0: 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 s */. ExprList
16c0: 2a 70 43 68 61 6e 67 65 73 2c 20 20 20 20 2f 2a *pChanges, /*
16d0: 20 54 68 69 6e 67 73 20 74 6f 20 62 65 20 63 68 Things to be ch
16e0: 61 6e 67 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 anged */. Expr
16f0: 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 *pWhere,
1700: 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 /* The WHERE c
1710: 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e lause. May be n
1720: 75 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 ull */. int onE
1730: 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 20 rror,
1740: 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 /* How to handle
1750: 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f constraint erro
1760: 72 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 rs */. ExprList
1770: 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 2f *pOrderBy, /
1780: 2a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 * ORDER BY claus
1790: 65 2e 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a e. May be null *
17a0: 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 /. Expr *pLimit
17b0: 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 , /* LI
17c0: 4d 49 54 20 63 6c 61 75 73 65 2e 20 4d 61 79 20 MIT clause. May
17d0: 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 55 70 73 be null */. Ups
17e0: 65 72 74 20 2a 70 55 70 73 65 72 74 20 20 20 20 ert *pUpsert
17f0: 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 /* ON CONFLI
1800: 43 54 20 63 6c 61 75 73 65 2c 20 6f 72 20 6e 75 CT clause, or nu
1810: 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 ll */.){. int i
1820: 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 , j;
1830: 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 /* Loop counte
1840: 72 73 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 rs */. Table *p
1850: 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f Tab; /
1860: 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62 * The table to b
1870: 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 e updated */. i
1880: 6e 74 20 61 64 64 72 54 6f 70 20 3d 20 30 3b 20 nt addrTop = 0;
1890: 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 69 6e /* VDBE in
18a0: 73 74 72 75 63 74 69 6f 6e 20 61 64 64 72 65 73 struction addres
18b0: 73 20 6f 66 20 74 68 65 20 73 74 61 72 74 20 6f s of the start o
18c0: 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 f the loop */.
18d0: 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 WhereInfo *pWInf
18e0: 6f 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d o; /* Inform
18f0: 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 ation about the
1900: 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a WHERE clause */.
1910: 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 Vdbe *v;
1920: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
1930: 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 virtual database
1940: 20 65 6e 67 69 6e 65 20 2a 2f 0a 20 20 49 6e 64 engine */. Ind
1950: 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 ex *pIdx;
1960: 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 /* For loopi
1970: 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20 ng over indices
1980: 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 50 6b 3b */. Index *pPk;
1990: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
19a0: 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 he PRIMARY KEY i
19b0: 6e 64 65 78 20 66 6f 72 20 57 49 54 48 4f 55 54 ndex for WITHOUT
19c0: 20 52 4f 57 49 44 20 74 61 62 6c 65 73 20 2a 2f ROWID tables */
19d0: 0a 20 20 69 6e 74 20 6e 49 64 78 3b 20 20 20 20 . int nIdx;
19e0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
19f0: 62 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20 74 ber of indices t
1a00: 68 61 74 20 6e 65 65 64 20 75 70 64 61 74 69 6e hat need updatin
1a10: 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 49 g */. int nAllI
1a20: 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a dx; /*
1a30: 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 Total number of
1a40: 20 69 6e 64 65 78 65 73 20 2a 2f 0a 20 20 69 6e indexes */. in
1a50: 74 20 69 42 61 73 65 43 75 72 3b 20 20 20 20 20 t iBaseCur;
1a60: 20 20 20 20 20 2f 2a 20 42 61 73 65 20 63 75 72 /* Base cur
1a70: 73 6f 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 sor number */.
1a80: 69 6e 74 20 69 44 61 74 61 43 75 72 3b 20 20 20 int iDataCur;
1a90: 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 /* Cursor
1aa0: 20 66 6f 72 20 74 68 65 20 63 61 6e 6f 6e 69 63 for the canonic
1ab0: 61 6c 20 64 61 74 61 20 62 74 72 65 65 20 2a 2f al data btree */
1ac0: 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20 . int iIdxCur;
1ad0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 /* Cur
1ae0: 73 6f 72 20 66 6f 72 20 74 68 65 20 66 69 72 73 sor for the firs
1af0: 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 73 71 6c t index */. sql
1b00: 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
1b10: 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 /* The datab
1b20: 61 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f ase structure */
1b30: 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20 . int *aRegIdx
1b40: 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 52 65 67 = 0; /* Reg
1b50: 69 73 74 65 72 73 20 66 6f 72 20 74 6f 20 65 61 isters for to ea
1b60: 63 68 20 69 6e 64 65 78 20 61 6e 64 20 74 68 65 ch index and the
1b70: 20 6d 61 69 6e 20 74 61 62 6c 65 20 2a 2f 0a 20 main table */.
1b80: 20 69 6e 74 20 2a 61 58 52 65 66 20 3d 20 30 3b int *aXRef = 0;
1b90: 20 20 20 20 20 20 20 20 2f 2a 20 61 58 52 65 66 /* aXRef
1ba0: 5b 69 5d 20 69 73 20 74 68 65 20 69 6e 64 65 78 [i] is the index
1bb0: 20 69 6e 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b in pChanges->a[
1bc0: 5d 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 ] of the.
1bd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1be0: 20 20 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69 ** an expressi
1bf0: 6f 6e 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 on for the i-th
1c00: 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 column of the ta
1c10: 62 6c 65 2e 0a 20 20 20 20 20 20 20 20 20 20 20 ble..
1c20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a **
1c30: 20 61 58 52 65 66 5b 69 5d 3d 3d 2d 31 20 69 66 aXRef[i]==-1 if
1c40: 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e the i-th column
1c50: 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2e is not changed.
1c60: 20 2a 2f 0a 20 20 75 38 20 2a 61 54 6f 4f 70 65 */. u8 *aToOpe
1c70: 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 n; /*
1c80: 31 20 66 6f 72 20 74 61 62 6c 65 73 20 61 6e 64 1 for tables and
1c90: 20 69 6e 64 69 63 65 73 20 74 6f 20 62 65 20 6f indices to be o
1ca0: 70 65 6e 65 64 20 2a 2f 0a 20 20 75 38 20 63 68 pened */. u8 ch
1cb0: 6e 67 50 6b 3b 20 20 20 20 20 20 20 20 20 20 20 ngPk;
1cc0: 20 20 2f 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 /* PRIMARY KEY
1cd0: 20 63 68 61 6e 67 65 64 20 69 6e 20 61 20 57 49 changed in a WI
1ce0: 54 48 4f 55 54 20 52 4f 57 49 44 20 74 61 62 6c THOUT ROWID tabl
1cf0: 65 20 2a 2f 0a 20 20 75 38 20 63 68 6e 67 52 6f e */. u8 chngRo
1d00: 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a wid; /*
1d10: 20 52 6f 77 69 64 20 63 68 61 6e 67 65 64 20 69 Rowid changed i
1d20: 6e 20 61 20 6e 6f 72 6d 61 6c 20 74 61 62 6c 65 n a normal table
1d30: 20 2a 2f 0a 20 20 75 38 20 63 68 6e 67 4b 65 79 */. u8 chngKey
1d40: 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
1d50: 45 69 74 68 65 72 20 63 68 6e 67 50 6b 20 6f 72 Either chngPk or
1d60: 20 63 68 6e 67 52 6f 77 69 64 20 2a 2f 0a 20 20 chngRowid */.
1d70: 45 78 70 72 20 2a 70 52 6f 77 69 64 45 78 70 72 Expr *pRowidExpr
1d80: 20 3d 20 30 3b 20 20 2f 2a 20 45 78 70 72 65 73 = 0; /* Expres
1d90: 73 69 6f 6e 20 64 65 66 69 6e 69 6e 67 20 74 68 sion defining th
1da0: 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d e new record num
1db0: 62 65 72 20 2a 2f 0a 20 20 41 75 74 68 43 6f 6e ber */. AuthCon
1dc0: 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 20 20 text sContext;
1dd0: 2f 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 61 /* The authoriza
1de0: 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a tion context */.
1df0: 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e NameContext sN
1e00: 43 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 C; /* The
1e10: 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74 6f 20 name-context to
1e20: 72 65 73 6f 6c 76 65 20 65 78 70 72 65 73 73 69 resolve expressi
1e30: 6f 6e 73 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 ons in */. int
1e40: 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 iDb;
1e50: 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 /* Database c
1e60: 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 ontaining the ta
1e70: 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74 65 ble being update
1e80: 64 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 6e 65 50 d */. int eOneP
1e90: 61 73 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a ass; /*
1ea0: 20 4f 4e 45 50 41 53 53 5f 58 58 58 20 76 61 6c ONEPASS_XXX val
1eb0: 75 65 20 66 72 6f 6d 20 77 68 65 72 65 2e 63 20 ue from where.c
1ec0: 2a 2f 0a 20 20 69 6e 74 20 68 61 73 46 4b 3b 20 */. int hasFK;
1ed0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1ee0: 72 75 65 20 69 66 20 66 6f 72 65 69 67 6e 20 6b rue if foreign k
1ef0: 65 79 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 ey processing is
1f00: 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 required */. i
1f10: 6e 74 20 6c 61 62 65 6c 42 72 65 61 6b 3b 20 20 nt labelBreak;
1f20: 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 /* Jump he
1f30: 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 re to break out
1f40: 6f 66 20 55 50 44 41 54 45 20 6c 6f 6f 70 20 2a of UPDATE loop *
1f50: 2f 0a 20 20 69 6e 74 20 6c 61 62 65 6c 43 6f 6e /. int labelCon
1f60: 74 69 6e 75 65 3b 20 20 20 20 20 2f 2a 20 4a 75 tinue; /* Ju
1f70: 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 mp here to conti
1f80: 6e 75 65 20 6e 65 78 74 20 73 74 65 70 20 6f 66 nue next step of
1f90: 20 55 50 44 41 54 45 20 6c 6f 6f 70 20 2a 2f 0a UPDATE loop */.
1fa0: 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 int flags;
1fb0: 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 /* Flag
1fc0: 73 20 66 6f 72 20 73 71 6c 69 74 65 33 57 68 65 s for sqlite3Whe
1fd0: 72 65 42 65 67 69 6e 28 29 20 2a 2f 0a 0a 23 69 reBegin() */..#i
1fe0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
1ff0: 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20 T_TRIGGER. int
2000: 69 73 56 69 65 77 3b 20 20 20 20 20 20 20 20 20 isView;
2010: 20 20 20 2f 2a 20 54 72 75 65 20 77 68 65 6e 20 /* True when
2020: 75 70 64 61 74 69 6e 67 20 61 20 76 69 65 77 20 updating a view
2030: 28 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 (INSTEAD OF trig
2040: 67 65 72 29 20 2a 2f 0a 20 20 54 72 69 67 67 65 ger) */. Trigge
2050: 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 r *pTrigger;
2060: 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 /* List of trig
2070: 67 65 72 73 20 6f 6e 20 70 54 61 62 2c 20 69 66 gers on pTab, if
2080: 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 required */. i
2090: 6e 74 20 74 6d 61 73 6b 3b 20 20 20 20 20 20 20 nt tmask;
20a0: 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 /* Mask of
20b0: 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 7c TRIGGER_BEFORE|
20c0: 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a 2f TRIGGER_AFTER */
20d0: 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 6e 65 .#endif. int ne
20e0: 77 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 wmask;
20f0: 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 4e 45 57 2e /* Mask of NEW.
2100: 2a 20 63 6f 6c 75 6d 6e 73 20 61 63 63 65 73 73 * columns access
2110: 65 64 20 62 79 20 42 45 46 4f 52 45 20 74 72 69 ed by BEFORE tri
2120: 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 ggers */. int i
2130: 45 70 68 20 3d 20 30 3b 20 20 20 20 20 20 20 20 Eph = 0;
2140: 20 20 2f 2a 20 45 70 68 65 6d 65 72 61 6c 20 74 /* Ephemeral t
2150: 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20 61 6c 6c able holding all
2160: 20 70 72 69 6d 61 72 79 20 6b 65 79 20 76 61 6c primary key val
2170: 75 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 ues */. int nKe
2180: 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 y = 0;
2190: 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 /* Number of ele
21a0: 6d 65 6e 74 73 20 69 6e 20 72 65 67 4b 65 79 20 ments in regKey
21b0: 66 6f 72 20 57 49 54 48 4f 55 54 20 52 4f 57 49 for WITHOUT ROWI
21c0: 44 20 2a 2f 0a 20 20 69 6e 74 20 61 69 43 75 72 D */. int aiCur
21d0: 4f 6e 65 50 61 73 73 5b 32 5d 3b 20 20 20 2f 2a OnePass[2]; /*
21e0: 20 54 68 65 20 77 72 69 74 65 20 63 75 72 73 6f The write curso
21f0: 72 73 20 6f 70 65 6e 65 64 20 62 79 20 57 48 45 rs opened by WHE
2200: 52 45 5f 4f 4e 45 50 41 53 53 20 2a 2f 0a 20 20 RE_ONEPASS */.
2210: 69 6e 74 20 61 64 64 72 4f 70 65 6e 20 3d 20 30 int addrOpen = 0
2220: 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 ; /* Addres
2230: 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 s of OP_OpenEphe
2240: 6d 65 72 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 meral */. int i
2250: 50 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 Pk = 0;
2260: 20 20 2f 2a 20 46 69 72 73 74 20 6f 66 20 6e 50 /* First of nP
2270: 6b 20 63 65 6c 6c 73 20 68 6f 6c 64 69 6e 67 20 k cells holding
2280: 50 52 49 4d 41 52 59 20 4b 45 59 20 76 61 6c 75 PRIMARY KEY valu
2290: 65 20 2a 2f 0a 20 20 69 31 36 20 6e 50 6b 20 3d e */. i16 nPk =
22a0: 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 0; /*
22b0: 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6d 70 6f Number of compo
22c0: 6e 65 6e 74 73 20 6f 66 20 74 68 65 20 50 52 49 nents of the PRI
22d0: 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 69 6e MARY KEY */. in
22e0: 74 20 62 52 65 70 6c 61 63 65 20 3d 20 30 3b 20 t bReplace = 0;
22f0: 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
2300: 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63 74 REPLACE conflict
2310: 20 72 65 73 6f 6c 75 74 69 6f 6e 20 6d 69 67 68 resolution migh
2320: 74 20 68 61 70 70 65 6e 20 2a 2f 0a 0a 20 20 2f t happen */.. /
2330: 2a 20 52 65 67 69 73 74 65 72 20 41 6c 6c 6f 63 * Register Alloc
2340: 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 ations */. int
2350: 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 30 3b regRowCount = 0;
2360: 20 20 20 2f 2a 20 41 20 63 6f 75 6e 74 20 6f 66 /* A count of
2370: 20 72 6f 77 73 20 63 68 61 6e 67 65 64 20 2a 2f rows changed */
2380: 0a 20 20 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77 . int regOldRow
2390: 69 64 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 id = 0; /* The
23a0: 20 6f 6c 64 20 72 6f 77 69 64 20 2a 2f 0a 20 20 old rowid */.
23b0: 69 6e 74 20 72 65 67 4e 65 77 52 6f 77 69 64 20 int regNewRowid
23c0: 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 6e 65 = 0; /* The ne
23d0: 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 w rowid */. int
23e0: 20 72 65 67 4e 65 77 20 3d 20 30 3b 20 20 20 20 regNew = 0;
23f0: 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f /* Content o
2400: 66 20 74 68 65 20 4e 45 57 2e 2a 20 74 61 62 6c f the NEW.* tabl
2410: 65 20 69 6e 20 74 72 69 67 67 65 72 73 20 2a 2f e in triggers */
2420: 0a 20 20 69 6e 74 20 72 65 67 4f 6c 64 20 3d 20 . int regOld =
2430: 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 0; /* Con
2440: 74 65 6e 74 20 6f 66 20 4f 4c 44 2e 2a 20 74 61 tent of OLD.* ta
2450: 62 6c 65 20 69 6e 20 74 72 69 67 67 65 72 73 20 ble in triggers
2460: 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 53 */. int regRowS
2470: 65 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 52 et = 0; /* R
2480: 6f 77 73 65 74 20 6f 66 20 72 6f 77 73 20 74 6f owset of rows to
2490: 20 62 65 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 be updated */.
24a0: 20 69 6e 74 20 72 65 67 4b 65 79 20 3d 20 30 3b int regKey = 0;
24b0: 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6d 70 6f /* compo
24c0: 73 69 74 65 20 50 52 49 4d 41 52 59 20 4b 45 59 site PRIMARY KEY
24d0: 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 6d 65 6d value */.. mem
24e0: 73 65 74 28 26 73 43 6f 6e 74 65 78 74 2c 20 30 set(&sContext, 0
24f0: 2c 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74 65 78 , sizeof(sContex
2500: 74 29 29 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 t));. db = pPar
2510: 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 se->db;. if( pP
2520: 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 arse->nErr || db
2530: 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
2540: 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 {. goto updat
2550: 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 e_cleanup;. }.
2560: 20 61 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 assert( pTabLis
2570: 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 0a 20 t->nSrc==1 );..
2580: 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 /* Locate the t
2590: 61 62 6c 65 20 77 68 69 63 68 20 77 65 20 77 61 able which we wa
25a0: 6e 74 20 74 6f 20 75 70 64 61 74 65 2e 20 0a 20 nt to update. .
25b0: 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c */. pTab = sql
25c0: 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 ite3SrcListLooku
25d0: 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 p(pParse, pTabLi
25e0: 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d st);. if( pTab=
25f0: 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 =0 ) goto update
2600: 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 44 62 20 _cleanup;. iDb
2610: 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 = sqlite3SchemaT
2620: 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 oIndex(pParse->d
2630: 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 b, pTab->pSchema
2640: 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 );.. /* Figure
2650: 6f 75 74 20 69 66 20 77 65 20 68 61 76 65 20 61 out if we have a
2660: 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 ny triggers and
2670: 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 if the table bei
2680: 6e 67 0a 20 20 2a 2a 20 75 70 64 61 74 65 64 20 ng. ** updated
2690: 69 73 20 61 20 76 69 65 77 2e 0a 20 20 2a 2f 0a is a view.. */.
26a0: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
26b0: 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 70 54 MIT_TRIGGER. pT
26c0: 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 rigger = sqlite3
26d0: 54 72 69 67 67 65 72 73 45 78 69 73 74 28 70 50 TriggersExist(pP
26e0: 61 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f 55 arse, pTab, TK_U
26f0: 50 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73 2c PDATE, pChanges,
2700: 20 26 74 6d 61 73 6b 29 3b 0a 20 20 69 73 56 69 &tmask);. isVi
2710: 65 77 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 ew = pTab->pSele
2720: 63 74 21 3d 30 3b 0a 20 20 61 73 73 65 72 74 28 ct!=0;. assert(
2730: 20 70 54 72 69 67 67 65 72 20 7c 7c 20 74 6d 61 pTrigger || tma
2740: 73 6b 3d 3d 30 20 29 3b 0a 23 65 6c 73 65 0a 23 sk==0 );.#else.#
2750: 20 64 65 66 69 6e 65 20 70 54 72 69 67 67 65 72 define pTrigger
2760: 20 30 0a 23 20 64 65 66 69 6e 65 20 69 73 56 69 0.# define isVi
2770: 65 77 20 30 0a 23 20 64 65 66 69 6e 65 20 74 6d ew 0.# define tm
2780: 61 73 6b 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 ask 0.#endif.#if
2790: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
27a0: 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69 73 56 VIEW.# undef isV
27b0: 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69 73 56 iew.# define isV
27c0: 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 iew 0.#endif..#i
27d0: 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 fdef SQLITE_ENAB
27e0: 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 LE_UPDATE_DELETE
27f0: 5f 4c 49 4d 49 54 0a 20 20 69 66 28 20 21 69 73 _LIMIT. if( !is
2800: 56 69 65 77 20 29 7b 0a 20 20 20 20 70 57 68 65 View ){. pWhe
2810: 72 65 20 3d 20 73 71 6c 69 74 65 33 4c 69 6d 69 re = sqlite3Limi
2820: 74 57 68 65 72 65 28 0a 20 20 20 20 20 20 20 20 tWhere(.
2830: 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 pParse, pTabList
2840: 2c 20 70 57 68 65 72 65 2c 20 70 4f 72 64 65 72 , pWhere, pOrder
2850: 42 79 2c 20 70 4c 69 6d 69 74 2c 20 22 55 50 44 By, pLimit, "UPD
2860: 41 54 45 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 ATE". );.
2870: 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 pOrderBy = 0;.
2880: 20 20 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 pLimit = 0;.
2890: 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 }.#endif.. if(
28a0: 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f sqlite3ViewGetCo
28b0: 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 lumnNames(pParse
28c0: 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 , pTab) ){. g
28d0: 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e oto update_clean
28e0: 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 up;. }. if( sq
28f0: 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 lite3IsReadOnly(
2900: 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 74 6d pParse, pTab, tm
2910: 61 73 6b 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f ask) ){. goto
2920: 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b update_cleanup;
2930: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 . }.. /* Alloc
2940: 61 74 65 20 61 20 63 75 72 73 6f 72 73 20 66 6f ate a cursors fo
2950: 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 r the main datab
2960: 61 73 65 20 74 61 62 6c 65 20 61 6e 64 20 66 6f ase table and fo
2970: 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 2e 0a 20 r all indices..
2980: 20 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 63 75 ** The index cu
2990: 72 73 6f 72 73 20 6d 69 67 68 74 20 6e 6f 74 20 rsors might not
29a0: 62 65 20 75 73 65 64 2c 20 62 75 74 20 69 66 20 be used, but if
29b0: 74 68 65 79 20 61 72 65 20 75 73 65 64 20 74 68 they are used th
29c0: 65 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 ey. ** need to
29d0: 6f 63 63 75 72 20 72 69 67 68 74 20 61 66 74 65 occur right afte
29e0: 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 r the database c
29f0: 75 72 73 6f 72 2e 20 20 53 6f 20 67 6f 20 61 68 ursor. So go ah
2a00: 65 61 64 20 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c ead and. ** all
2a10: 6f 63 61 74 65 20 65 6e 6f 75 67 68 20 73 70 61 ocate enough spa
2a20: 63 65 2c 20 6a 75 73 74 20 69 6e 20 63 61 73 65 ce, just in case
2a30: 2e 0a 20 20 2a 2f 0a 20 20 69 42 61 73 65 43 75 .. */. iBaseCu
2a40: 72 20 3d 20 69 44 61 74 61 43 75 72 20 3d 20 70 r = iDataCur = p
2a50: 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 Parse->nTab++;.
2a60: 20 69 49 64 78 43 75 72 20 3d 20 69 44 61 74 61 iIdxCur = iData
2a70: 43 75 72 2b 31 3b 0a 20 20 70 50 6b 20 3d 20 48 Cur+1;. pPk = H
2a80: 61 73 52 6f 77 69 64 28 70 54 61 62 29 20 3f 20 asRowid(pTab) ?
2a90: 30 20 3a 20 73 71 6c 69 74 65 33 50 72 69 6d 61 0 : sqlite3Prima
2aa0: 72 79 4b 65 79 49 6e 64 65 78 28 70 54 61 62 29 ryKeyIndex(pTab)
2ab0: 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 50 ;. testcase( pP
2ac0: 6b 21 3d 30 20 26 26 20 70 50 6b 21 3d 70 54 61 k!=0 && pPk!=pTa
2ad0: 62 2d 3e 70 49 6e 64 65 78 20 29 3b 0a 20 20 66 b->pIndex );. f
2ae0: 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49 64 78 3d or(nIdx=0, pIdx=
2af0: 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 pTab->pIndex; pI
2b00: 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 dx; pIdx=pIdx->p
2b10: 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b 0a 20 Next, nIdx++){.
2b20: 20 20 20 69 66 28 20 70 50 6b 3d 3d 70 49 64 78 if( pPk==pIdx
2b30: 20 29 7b 0a 20 20 20 20 20 20 69 44 61 74 61 43 ){. iDataC
2b40: 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 ur = pParse->nTa
2b50: 62 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 b;. }. pPa
2b60: 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d rse->nTab++;. }
2b70: 0a 20 20 69 66 28 20 70 55 70 73 65 72 74 20 29 . if( pUpsert )
2b80: 7b 0a 20 20 20 20 2f 2a 20 4f 6e 20 61 6e 20 55 {. /* On an U
2b90: 50 53 45 52 54 2c 20 72 65 75 73 65 20 74 68 65 PSERT, reuse the
2ba0: 20 73 61 6d 65 20 63 75 72 73 6f 72 73 20 61 6c same cursors al
2bb0: 72 65 61 64 79 20 6f 70 65 6e 65 64 20 62 79 20 ready opened by
2bc0: 49 4e 53 45 52 54 20 2a 2f 0a 20 20 20 20 69 44 INSERT */. iD
2bd0: 61 74 61 43 75 72 20 3d 20 70 55 70 73 65 72 74 ataCur = pUpsert
2be0: 2d 3e 69 44 61 74 61 43 75 72 3b 0a 20 20 20 20 ->iDataCur;.
2bf0: 69 49 64 78 43 75 72 20 3d 20 70 55 70 73 65 72 iIdxCur = pUpser
2c00: 74 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 t->iIdxCur;.
2c10: 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 69 pParse->nTab = i
2c20: 42 61 73 65 43 75 72 3b 0a 20 20 7d 0a 20 20 70 BaseCur;. }. p
2c30: 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 TabList->a[0].iC
2c40: 75 72 73 6f 72 20 3d 20 69 44 61 74 61 43 75 72 ursor = iDataCur
2c50: 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 ;.. /* Allocate
2c60: 20 73 70 61 63 65 20 66 6f 72 20 61 58 52 65 66 space for aXRef
2c70: 5b 5d 2c 20 61 52 65 67 49 64 78 5b 5d 2c 20 61 [], aRegIdx[], a
2c80: 6e 64 20 61 54 6f 4f 70 65 6e 5b 5d 2e 20 20 0a nd aToOpen[]. .
2c90: 20 20 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 ** Initialize
2ca0: 61 58 52 65 66 5b 5d 20 61 6e 64 20 61 54 6f 4f aXRef[] and aToO
2cb0: 70 65 6e 5b 5d 20 74 6f 20 74 68 65 69 72 20 64 pen[] to their d
2cc0: 65 66 61 75 6c 74 20 76 61 6c 75 65 73 2e 0a 20 efault values..
2cd0: 20 2a 2f 0a 20 20 61 58 52 65 66 20 3d 20 73 71 */. aXRef = sq
2ce0: 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 lite3DbMallocRaw
2cf0: 4e 4e 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e NN(db, sizeof(in
2d00: 74 29 20 2a 20 28 70 54 61 62 2d 3e 6e 43 6f 6c t) * (pTab->nCol
2d10: 2b 6e 49 64 78 2b 31 29 20 2b 20 6e 49 64 78 2b +nIdx+1) + nIdx+
2d20: 32 20 29 3b 0a 20 20 69 66 28 20 61 58 52 65 66 2 );. if( aXRef
2d30: 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 ==0 ) goto updat
2d40: 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 61 52 65 e_cleanup;. aRe
2d50: 67 49 64 78 20 3d 20 61 58 52 65 66 2b 70 54 61 gIdx = aXRef+pTa
2d60: 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 54 6f 4f 70 b->nCol;. aToOp
2d70: 65 6e 20 3d 20 28 75 38 2a 29 28 61 52 65 67 49 en = (u8*)(aRegI
2d80: 64 78 2b 6e 49 64 78 2b 31 29 3b 0a 20 20 6d 65 dx+nIdx+1);. me
2d90: 6d 73 65 74 28 61 54 6f 4f 70 65 6e 2c 20 31 2c mset(aToOpen, 1,
2da0: 20 6e 49 64 78 2b 31 29 3b 0a 20 20 61 54 6f 4f nIdx+1);. aToO
2db0: 70 65 6e 5b 6e 49 64 78 2b 31 5d 20 3d 20 30 3b pen[nIdx+1] = 0;
2dc0: 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 . for(i=0; i<pT
2dd0: 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 20 61 ab->nCol; i++) a
2de0: 58 52 65 66 5b 69 5d 20 3d 20 2d 31 3b 0a 0a 20 XRef[i] = -1;..
2df0: 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 /* Initialize t
2e00: 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 he name-context
2e10: 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 */. memset(&sNC
2e20: 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 , 0, sizeof(sNC)
2e30: 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65 20 );. sNC.pParse
2e40: 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 4e 43 2e = pParse;. sNC.
2e50: 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c pSrcList = pTabL
2e60: 69 73 74 3b 0a 20 20 73 4e 43 2e 75 4e 43 2e 70 ist;. sNC.uNC.p
2e70: 55 70 73 65 72 74 20 3d 20 70 55 70 73 65 72 74 Upsert = pUpsert
2e80: 3b 0a 20 20 73 4e 43 2e 6e 63 46 6c 61 67 73 20 ;. sNC.ncFlags
2e90: 3d 20 4e 43 5f 55 55 70 73 65 72 74 3b 0a 0a 20 = NC_UUpsert;..
2ea0: 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 /* Resolve the
2eb0: 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 column names in
2ec0: 61 6c 6c 20 74 68 65 20 65 78 70 72 65 73 73 69 all the expressi
2ed0: 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 ons of the. **
2ee0: 6f 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 of the UPDATE st
2ef0: 61 74 65 6d 65 6e 74 2e 20 20 41 6c 73 6f 20 66 atement. Also f
2f00: 69 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 ind the column i
2f10: 6e 64 65 78 0a 20 20 2a 2a 20 66 6f 72 20 65 61 ndex. ** for ea
2f20: 63 68 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 ch column to be
2f30: 75 70 64 61 74 65 64 20 69 6e 20 74 68 65 20 70 updated in the p
2f40: 43 68 61 6e 67 65 73 20 61 72 72 61 79 2e 20 20 Changes array.
2f50: 46 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20 63 6f For each. ** co
2f60: 6c 75 6d 6e 20 74 6f 20 62 65 20 75 70 64 61 74 lumn to be updat
2f70: 65 64 2c 20 6d 61 6b 65 20 73 75 72 65 20 77 65 ed, make sure we
2f80: 20 68 61 76 65 20 61 75 74 68 6f 72 69 7a 61 74 have authorizat
2f90: 69 6f 6e 20 74 6f 20 63 68 61 6e 67 65 0a 20 20 ion to change.
2fa0: 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a ** that column..
2fb0: 20 20 2a 2f 0a 20 20 63 68 6e 67 52 6f 77 69 64 */. chngRowid
2fc0: 20 3d 20 63 68 6e 67 50 6b 20 3d 20 30 3b 0a 20 = chngPk = 0;.
2fd0: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 61 for(i=0; i<pCha
2fe0: 6e 67 65 73 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b nges->nExpr; i++
2ff0: 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
3000: 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d e3ResolveExprNam
3010: 65 73 28 26 73 4e 43 2c 20 70 43 68 61 6e 67 65 es(&sNC, pChange
3020: 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 s->a[i].pExpr) )
3030: 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64 {. goto upd
3040: 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 ate_cleanup;.
3050: 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 }. for(j=0;
3060: 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b j<pTab->nCol; j+
3070: 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 +){. if( sq
3080: 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 lite3StrICmp(pTa
3090: 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 b->aCol[j].zName
30a0: 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d , pChanges->a[i]
30b0: 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 .zName)==0 ){.
30c0: 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 54 61 if( j==pTa
30d0: 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 b->iPKey ){.
30e0: 20 20 20 20 20 20 63 68 6e 67 52 6f 77 69 64 20 chngRowid
30f0: 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 = 1;. p
3100: 52 6f 77 69 64 45 78 70 72 20 3d 20 70 43 68 61 RowidExpr = pCha
3110: 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 nges->a[i].pExpr
3120: 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 ;. }else
3130: 69 66 28 20 70 50 6b 20 26 26 20 28 70 54 61 62 if( pPk && (pTab
3140: 2d 3e 61 43 6f 6c 5b 6a 5d 2e 63 6f 6c 46 6c 61 ->aCol[j].colFla
3150: 67 73 20 26 20 43 4f 4c 46 4c 41 47 5f 50 52 49 gs & COLFLAG_PRI
3160: 4d 4b 45 59 29 21 3d 30 20 29 7b 0a 20 20 20 20 MKEY)!=0 ){.
3170: 20 20 20 20 20 20 63 68 6e 67 50 6b 20 3d 20 31 chngPk = 1
3180: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
3190: 20 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20 69 aXRef[j] = i
31a0: 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b ;. break;
31b0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
31c0: 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e if( j>=pTab->
31d0: 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 nCol ){. if
31e0: 28 20 70 50 6b 3d 3d 30 20 26 26 20 73 71 6c 69 ( pPk==0 && sqli
31f0: 74 65 33 49 73 52 6f 77 69 64 28 70 43 68 61 6e te3IsRowid(pChan
3200: 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 ges->a[i].zName)
3210: 20 29 7b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 ){. j =
3220: 2d 31 3b 0a 20 20 20 20 20 20 20 20 63 68 6e 67 -1;. chng
3230: 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 Rowid = 1;.
3240: 20 20 20 70 52 6f 77 69 64 45 78 70 72 20 3d 20 pRowidExpr =
3250: 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 pChanges->a[i].p
3260: 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73 Expr;. }els
3270: 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 e{. sqlit
3280: 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
3290: 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 e, "no such colu
32a0: 6d 6e 3a 20 25 73 22 2c 20 70 43 68 61 6e 67 65 mn: %s", pChange
32b0: 73 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a s->a[i].zName);.
32c0: 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
32d0: 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b checkSchema = 1;
32e0: 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 75 70 . goto up
32f0: 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 date_cleanup;.
3300: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e }. }.#ifn
3310: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
3320: 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 AUTHORIZATION.
3330: 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 {. int rc
3340: 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c ;. rc = sql
3350: 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 ite3AuthCheck(pP
3360: 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 55 50 44 arse, SQLITE_UPD
3370: 41 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 ATE, pTab->zName
3380: 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
3390: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6a 3c j<
33a0: 30 20 3f 20 22 52 4f 57 49 44 22 20 3a 20 70 54 0 ? "ROWID" : pT
33b0: 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d ab->aCol[j].zNam
33c0: 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
33d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 d
33e0: 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 44 62 53 b->aDb[iDb].zDbS
33f0: 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 Name);. if(
3400: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 rc==SQLITE_DENY
3410: 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f ){. goto
3420: 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b update_cleanup;
3430: 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
3440: 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f rc==SQLITE_IGNO
3450: 52 45 20 29 7b 0a 20 20 20 20 20 20 20 20 61 58 RE ){. aX
3460: 52 65 66 5b 6a 5d 20 3d 20 2d 31 3b 0a 20 20 20 Ref[j] = -1;.
3470: 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 }. }.#endi
3480: 66 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 f. }. assert(
3490: 28 63 68 6e 67 52 6f 77 69 64 20 26 20 63 68 6e (chngRowid & chn
34a0: 67 50 6b 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 gPk)==0 );. ass
34b0: 65 72 74 28 20 63 68 6e 67 52 6f 77 69 64 3d 3d ert( chngRowid==
34c0: 30 20 7c 7c 20 63 68 6e 67 52 6f 77 69 64 3d 3d 0 || chngRowid==
34d0: 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 1 );. assert( c
34e0: 68 6e 67 50 6b 3d 3d 30 20 7c 7c 20 63 68 6e 67 hngPk==0 || chng
34f0: 50 6b 3d 3d 31 20 29 3b 0a 20 20 63 68 6e 67 4b Pk==1 );. chngK
3500: 65 79 20 3d 20 63 68 6e 67 52 6f 77 69 64 20 2b ey = chngRowid +
3510: 20 63 68 6e 67 50 6b 3b 0a 0a 20 20 2f 2a 20 54 chngPk;.. /* T
3520: 68 65 20 53 45 54 20 65 78 70 72 65 73 73 69 6f he SET expressio
3530: 6e 73 20 61 72 65 20 6e 6f 74 20 61 63 74 75 61 ns are not actua
3540: 6c 6c 79 20 75 73 65 64 20 69 6e 73 69 64 65 20 lly used inside
3550: 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e 20 the WHERE loop.
3560: 20 0a 20 20 2a 2a 20 53 6f 20 72 65 73 65 74 20 . ** So reset
3570: 74 68 65 20 63 6f 6c 55 73 65 64 20 6d 61 73 6b the colUsed mask
3580: 2e 20 55 6e 6c 65 73 73 20 74 68 69 73 20 69 73 . Unless this is
3590: 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 a virtual table
35a0: 2e 20 49 6e 20 74 68 61 74 0a 20 20 2a 2a 20 63 . In that. ** c
35b0: 61 73 65 2c 20 73 65 74 20 61 6c 6c 20 62 69 74 ase, set all bit
35c0: 73 20 6f 66 20 74 68 65 20 63 6f 6c 55 73 65 64 s of the colUsed
35d0: 20 6d 61 73 6b 20 28 74 6f 20 65 6e 73 75 72 65 mask (to ensure
35e0: 20 74 68 61 74 20 74 68 65 20 76 69 72 74 75 61 that the virtua
35f0: 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 20 69 6d 70 l. ** table imp
3600: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 61 6b 65 lementation make
3610: 73 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 61 76 s all columns av
3620: 61 69 6c 61 62 6c 65 29 2e 0a 20 20 2a 2f 0a 20 ailable).. */.
3630: 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e pTabList->a[0].
3640: 63 6f 6c 55 73 65 64 20 3d 20 49 73 56 69 72 74 colUsed = IsVirt
3650: 75 61 6c 28 70 54 61 62 29 20 3f 20 41 4c 4c 42 ual(pTab) ? ALLB
3660: 49 54 53 20 3a 20 30 3b 0a 0a 20 20 68 61 73 46 ITS : 0;.. hasF
3670: 4b 20 3d 20 73 71 6c 69 74 65 33 46 6b 52 65 71 K = sqlite3FkReq
3680: 75 69 72 65 64 28 70 50 61 72 73 65 2c 20 70 54 uired(pParse, pT
3690: 61 62 2c 20 61 58 52 65 66 2c 20 63 68 6e 67 4b ab, aXRef, chngK
36a0: 65 79 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65 ey);.. /* There
36b0: 20 69 73 20 6f 6e 65 20 65 6e 74 72 79 20 69 6e is one entry in
36c0: 20 74 68 65 20 61 52 65 67 49 64 78 5b 5d 20 61 the aRegIdx[] a
36d0: 72 72 61 79 20 66 6f 72 20 65 61 63 68 20 69 6e rray for each in
36e0: 64 65 78 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 dex on the table
36f0: 0a 20 20 2a 2a 20 62 65 69 6e 67 20 75 70 64 61 . ** being upda
3700: 74 65 64 2e 20 20 46 69 6c 6c 20 69 6e 20 61 52 ted. Fill in aR
3710: 65 67 49 64 78 5b 5d 20 77 69 74 68 20 61 20 72 egIdx[] with a r
3720: 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 74 egister number t
3730: 68 61 74 20 77 69 6c 6c 20 68 6f 6c 64 0a 20 20 hat will hold.
3740: 2a 2a 20 74 68 65 20 6b 65 79 20 66 6f 72 20 61 ** the key for a
3750: 63 63 65 73 73 69 6e 67 20 65 61 63 68 20 69 6e ccessing each in
3760: 64 65 78 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 dex.. */. if(
3770: 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c onError==OE_Repl
3780: 61 63 65 20 29 20 62 52 65 70 6c 61 63 65 20 3d ace ) bReplace =
3790: 20 31 3b 0a 20 20 66 6f 72 28 6e 41 6c 6c 49 64 1;. for(nAllId
37a0: 78 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e x=0, pIdx=pTab->
37b0: 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 pIndex; pIdx; pI
37c0: 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 dx=pIdx->pNext,
37d0: 6e 41 6c 6c 49 64 78 2b 2b 29 7b 0a 20 20 20 20 nAllIdx++){.
37e0: 69 6e 74 20 72 65 67 3b 0a 20 20 20 20 69 66 28 int reg;. if(
37f0: 20 63 68 6e 67 4b 65 79 20 7c 7c 20 68 61 73 46 chngKey || hasF
3800: 4b 3e 31 20 7c 7c 20 70 49 64 78 3d 3d 70 50 6b K>1 || pIdx==pPk
3810: 0a 20 20 20 20 20 7c 7c 20 69 6e 64 65 78 57 68 . || indexWh
3820: 65 72 65 43 6c 61 75 73 65 4d 69 67 68 74 43 68 ereClauseMightCh
3830: 61 6e 67 65 28 70 49 64 78 2c 61 58 52 65 66 2c ange(pIdx,aXRef,
3840: 63 68 6e 67 52 6f 77 69 64 29 0a 20 20 20 20 29 chngRowid). )
3850: 7b 0a 20 20 20 20 20 20 72 65 67 20 3d 20 2b 2b {. reg = ++
3860: 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pParse->nMem;.
3870: 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d pParse->nMem
3880: 20 2b 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d += pIdx->nColum
3890: 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 n;. }else{.
38a0: 20 20 20 20 72 65 67 20 3d 20 30 3b 0a 20 20 20 reg = 0;.
38b0: 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 for(i=0; i<pI
38c0: 64 78 2d 3e 6e 4b 65 79 43 6f 6c 3b 20 69 2b 2b dx->nKeyCol; i++
38d0: 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 ){. if( i
38e0: 6e 64 65 78 43 6f 6c 75 6d 6e 49 73 42 65 69 6e ndexColumnIsBein
38f0: 67 55 70 64 61 74 65 64 28 70 49 64 78 2c 20 69 gUpdated(pIdx, i
3900: 2c 20 61 58 52 65 66 2c 20 63 68 6e 67 52 6f 77 , aXRef, chngRow
3910: 69 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 id) ){.
3920: 20 72 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d reg = ++pParse-
3930: 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 >nMem;.
3940: 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d pParse->nMem +=
3950: 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a pIdx->nColumn;.
3960: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6f 6e if( on
3970: 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c Error==OE_Defaul
3980: 74 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72 72 t && pIdx->onErr
3990: 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 or==OE_Replace )
39a0: 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 52 {. bR
39b0: 65 70 6c 61 63 65 20 3d 20 31 3b 0a 20 20 20 20 eplace = 1;.
39c0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
39d0: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 break;.
39e0: 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
39f0: 0a 20 20 20 20 69 66 28 20 72 65 67 3d 3d 30 20 . if( reg==0
3a00: 29 20 61 54 6f 4f 70 65 6e 5b 6e 41 6c 6c 49 64 ) aToOpen[nAllId
3a10: 78 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 61 52 x+1] = 0;. aR
3a20: 65 67 49 64 78 5b 6e 41 6c 6c 49 64 78 5d 20 3d egIdx[nAllIdx] =
3a30: 20 72 65 67 3b 0a 20 20 7d 0a 20 20 61 52 65 67 reg;. }. aReg
3a40: 49 64 78 5b 6e 41 6c 6c 49 64 78 5d 20 3d 20 2b Idx[nAllIdx] = +
3a50: 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 +pParse->nMem;
3a60: 2f 2a 20 52 65 67 69 73 74 65 72 20 73 74 6f 72 /* Register stor
3a70: 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 72 65 ing the table re
3a80: 63 6f 72 64 20 2a 2f 0a 20 20 69 66 28 20 62 52 cord */. if( bR
3a90: 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 2f 2a eplace ){. /*
3aa0: 20 49 66 20 52 45 50 4c 41 43 45 20 63 6f 6e 66 If REPLACE conf
3ab0: 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 lict resolution
3ac0: 6d 69 67 68 74 20 62 65 20 69 6e 76 6f 6b 65 64 might be invoked
3ad0: 2c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 20 6f , open cursors o
3ae0: 6e 20 61 6c 6c 20 0a 20 20 20 20 2a 2a 20 69 6e n all . ** in
3af0: 64 65 78 65 73 20 69 6e 20 63 61 73 65 20 74 68 dexes in case th
3b00: 65 79 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f ey are needed to
3b10: 20 64 65 6c 65 74 65 20 72 65 63 6f 72 64 73 2e delete records.
3b20: 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 */. memset(
3b30: 61 54 6f 4f 70 65 6e 2c 20 31 2c 20 6e 49 64 78 aToOpen, 1, nIdx
3b40: 2b 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 +1);. }.. /* B
3b50: 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 egin generating
3b60: 63 6f 64 65 2e 20 2a 2f 0a 20 20 76 20 3d 20 73 code. */. v = s
3b70: 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 qlite3GetVdbe(pP
3b80: 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d arse);. if( v==
3b90: 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 0 ) goto update_
3ba0: 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 70 cleanup;. if( p
3bb0: 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 Parse->nested==0
3bc0: 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f ) sqlite3VdbeCo
3bd0: 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20 untChanges(v);.
3be0: 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 sqlite3BeginWri
3bf0: 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 teOperation(pPar
3c00: 73 65 2c 20 70 54 72 69 67 67 65 72 20 7c 7c 20 se, pTrigger ||
3c10: 68 61 73 46 4b 2c 20 69 44 62 29 3b 0a 0a 20 20 hasFK, iDb);..
3c20: 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 72 65 71 75 /* Allocate requ
3c30: 69 72 65 64 20 72 65 67 69 73 74 65 72 73 2e 20 ired registers.
3c40: 2a 2f 0a 20 20 69 66 28 20 21 49 73 56 69 72 74 */. if( !IsVirt
3c50: 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 ual(pTab) ){.
3c60: 20 2f 2a 20 46 6f 72 20 6e 6f 77 2c 20 72 65 67 /* For now, reg
3c70: 52 6f 77 53 65 74 20 61 6e 64 20 61 52 65 67 49 RowSet and aRegI
3c80: 64 78 5b 6e 41 6c 6c 49 64 78 5d 20 73 68 61 72 dx[nAllIdx] shar
3c90: 65 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 e the same regis
3ca0: 74 65 72 2e 0a 20 20 20 20 2a 2a 20 49 66 20 72 ter.. ** If r
3cb0: 65 67 52 6f 77 53 65 74 20 74 75 72 6e 73 20 6f egRowSet turns o
3cc0: 75 74 20 74 6f 20 62 65 20 6e 65 65 64 65 64 2c ut to be needed,
3cd0: 20 74 68 65 6e 20 61 52 65 67 49 64 78 5b 6e 41 then aRegIdx[nA
3ce0: 6c 6c 49 64 78 5d 20 77 69 6c 6c 20 62 65 0a 20 llIdx] will be.
3cf0: 20 20 20 2a 2a 20 72 65 61 6c 6c 6f 63 61 74 65 ** reallocate
3d00: 64 2e 20 20 61 52 65 67 49 64 78 5b 6e 41 6c 6c d. aRegIdx[nAll
3d10: 49 64 78 5d 20 69 73 20 74 68 65 20 72 65 67 69 Idx] is the regi
3d20: 73 74 65 72 20 69 6e 20 77 68 69 63 68 20 74 68 ster in which th
3d30: 65 20 6d 61 69 6e 0a 20 20 20 20 2a 2a 20 74 61 e main. ** ta
3d40: 62 6c 65 20 72 65 63 6f 72 64 20 69 73 20 77 72 ble record is wr
3d50: 69 74 74 65 6e 2e 20 20 72 65 67 52 6f 77 53 65 itten. regRowSe
3d60: 74 20 68 6f 6c 64 73 20 74 68 65 20 52 6f 77 53 t holds the RowS
3d70: 65 74 20 66 6f 72 20 74 68 65 0a 20 20 20 20 2a et for the. *
3d80: 2a 20 74 77 6f 2d 70 61 73 73 20 75 70 64 61 74 * two-pass updat
3d90: 65 20 61 6c 67 6f 72 69 74 68 6d 2e 20 2a 2f 0a e algorithm. */.
3da0: 20 20 20 20 61 73 73 65 72 74 28 20 61 52 65 67 assert( aReg
3db0: 49 64 78 5b 6e 41 6c 6c 49 64 78 5d 3d 3d 70 50 Idx[nAllIdx]==pP
3dc0: 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 arse->nMem );.
3dd0: 20 20 72 65 67 52 6f 77 53 65 74 20 3d 20 61 52 regRowSet = aR
3de0: 65 67 49 64 78 5b 6e 41 6c 6c 49 64 78 5d 3b 0a egIdx[nAllIdx];.
3df0: 20 20 20 20 72 65 67 4f 6c 64 52 6f 77 69 64 20 regOldRowid
3e00: 3d 20 72 65 67 4e 65 77 52 6f 77 69 64 20 3d 20 = regNewRowid =
3e10: 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a ++pParse->nMem;.
3e20: 20 20 20 20 69 66 28 20 63 68 6e 67 50 6b 20 7c if( chngPk |
3e30: 7c 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68 61 | pTrigger || ha
3e40: 73 46 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 67 sFK ){. reg
3e50: 4f 6c 64 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d Old = pParse->nM
3e60: 65 6d 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 50 em + 1;. pP
3e70: 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 arse->nMem += pT
3e80: 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 7d 0a ab->nCol;. }.
3e90: 20 20 20 20 69 66 28 20 63 68 6e 67 4b 65 79 20 if( chngKey
3ea0: 7c 7c 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68 || pTrigger || h
3eb0: 61 73 46 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 asFK ){. re
3ec0: 67 4e 65 77 52 6f 77 69 64 20 3d 20 2b 2b 70 50 gNewRowid = ++pP
3ed0: 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 arse->nMem;.
3ee0: 7d 0a 20 20 20 20 72 65 67 4e 65 77 20 3d 20 70 }. regNew = p
3ef0: 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b Parse->nMem + 1;
3f00: 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 . pParse->nMe
3f10: 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b m += pTab->nCol;
3f20: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 61 72 74 . }.. /* Start
3f30: 20 74 68 65 20 76 69 65 77 20 63 6f 6e 74 65 78 the view contex
3f40: 74 2e 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 t. */. if( isVi
3f50: 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ew ){. sqlite
3f60: 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 3AuthContextPush
3f70: 28 70 50 61 72 73 65 2c 20 26 73 43 6f 6e 74 65 (pParse, &sConte
3f80: 78 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 xt, pTab->zName)
3f90: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 ;. }.. /* If w
3fa0: 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 e are trying to
3fb0: 75 70 64 61 74 65 20 61 20 76 69 65 77 2c 20 72 update a view, r
3fc0: 65 61 6c 69 7a 65 20 74 68 61 74 20 76 69 65 77 ealize that view
3fd0: 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 6e 20 65 70 into. ** an ep
3fe0: 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 hemeral table..
3ff0: 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 */.#if !defined
4000: 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 (SQLITE_OMIT_VIE
4010: 57 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 W) && !defined(S
4020: 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 QLITE_OMIT_TRIGG
4030: 45 52 29 0a 20 20 69 66 28 20 69 73 56 69 65 77 ER). if( isView
4040: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d ){. sqlite3M
4050: 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28 70 aterializeView(p
4060: 50 61 72 73 65 2c 20 70 54 61 62 2c 20 0a 20 20 Parse, pTab, .
4070: 20 20 20 20 20 20 70 57 68 65 72 65 2c 20 70 4f pWhere, pO
4080: 72 64 65 72 42 79 2c 20 70 4c 69 6d 69 74 2c 20 rderBy, pLimit,
4090: 69 44 61 74 61 43 75 72 0a 20 20 20 20 29 3b 0a iDataCur. );.
40a0: 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 30 pOrderBy = 0
40b0: 3b 0a 20 20 20 20 70 4c 69 6d 69 74 20 3d 20 30 ;. pLimit = 0
40c0: 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 ;. }.#endif..
40d0: 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63 /* Resolve the c
40e0: 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61 olumn names in a
40f0: 6c 6c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f ll the expressio
4100: 6e 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 57 ns in the. ** W
4110: 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a HERE clause.. *
4120: 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 /. if( sqlite3R
4130: 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 esolveExprNames(
4140: 26 73 4e 43 2c 20 70 57 68 65 72 65 29 20 29 7b &sNC, pWhere) ){
4150: 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 . goto update
4160: 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 23 _cleanup;. }..#
4170: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d ifndef SQLITE_OM
4180: 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a IT_VIRTUALTABLE.
4190: 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 /* Virtual tab
41a0: 6c 65 73 20 6d 75 73 74 20 62 65 20 68 61 6e 64 les must be hand
41b0: 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 20 2a led separately *
41c0: 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 /. if( IsVirtua
41d0: 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 75 l(pTab) ){. u
41e0: 70 64 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c pdateVirtualTabl
41f0: 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 e(pParse, pTabLi
4200: 73 74 2c 20 70 54 61 62 2c 20 70 43 68 61 6e 67 st, pTab, pChang
4210: 65 73 2c 20 70 52 6f 77 69 64 45 78 70 72 2c 20 es, pRowidExpr,
4220: 61 58 52 65 66 2c 0a 20 20 20 20 20 20 20 20 20 aXRef,.
4230: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57 pW
4240: 68 65 72 65 2c 20 6f 6e 45 72 72 6f 72 29 3b 0a here, onError);.
4250: 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f goto update_
4260: 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 23 65 6e cleanup;. }.#en
4270: 64 69 66 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 dif.. /* Jump t
4280: 6f 20 6c 61 62 65 6c 42 72 65 61 6b 20 74 6f 20 o labelBreak to
4290: 61 62 61 6e 64 6f 6e 20 66 75 72 74 68 65 72 20 abandon further
42a0: 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 74 68 processing of th
42b0: 69 73 20 55 50 44 41 54 45 20 2a 2f 0a 20 20 6c is UPDATE */. l
42c0: 61 62 65 6c 43 6f 6e 74 69 6e 75 65 20 3d 20 6c abelContinue = l
42d0: 61 62 65 6c 42 72 65 61 6b 20 3d 20 73 71 6c 69 abelBreak = sqli
42e0: 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c te3VdbeMakeLabel
42f0: 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 2f 2a 20 (pParse);.. /*
4300: 4e 6f 74 20 61 6e 20 55 50 53 45 52 54 2e 20 20 Not an UPSERT.
4310: 4e 6f 72 6d 61 6c 20 70 72 6f 63 65 73 73 69 6e Normal processin
4320: 67 2e 20 20 42 65 67 69 6e 20 62 79 0a 20 20 2a g. Begin by. *
4330: 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 * initialize the
4340: 20 63 6f 75 6e 74 20 6f 66 20 75 70 64 61 74 65 count of update
4350: 64 20 72 6f 77 73 20 2a 2f 0a 20 20 69 66 28 20 d rows */. if(
4360: 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 (db->flags&SQLIT
4370: 45 5f 43 6f 75 6e 74 52 6f 77 73 29 21 3d 30 0a E_CountRows)!=0.
4380: 20 20 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 && !pParse->p
4390: 54 72 69 67 67 65 72 54 61 62 0a 20 20 20 26 26 TriggerTab. &&
43a0: 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 !pParse->nested
43b0: 0a 20 20 20 26 26 20 70 55 70 73 65 72 74 3d 3d . && pUpsert==
43c0: 30 0a 20 20 29 7b 0a 20 20 20 20 72 65 67 52 6f 0. ){. regRo
43d0: 77 43 6f 75 6e 74 20 3d 20 2b 2b 70 50 61 72 73 wCount = ++pPars
43e0: 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c e->nMem;. sql
43f0: 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
4400: 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c , OP_Integer, 0,
4410: 20 72 65 67 52 6f 77 43 6f 75 6e 74 29 3b 0a 20 regRowCount);.
4420: 20 7d 0a 0a 20 20 69 66 28 20 48 61 73 52 6f 77 }.. if( HasRow
4430: 69 64 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 id(pTab) ){.
4440: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
4450: 33 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 3(v, OP_Null, 0,
4460: 20 72 65 67 52 6f 77 53 65 74 2c 20 72 65 67 4f regRowSet, regO
4470: 6c 64 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 ldRowid);. }els
4480: 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 e{. assert( p
4490: 50 6b 21 3d 30 20 29 3b 0a 20 20 20 20 6e 50 6b Pk!=0 );. nPk
44a0: 20 3d 20 70 50 6b 2d 3e 6e 4b 65 79 43 6f 6c 3b = pPk->nKeyCol;
44b0: 0a 20 20 20 20 69 50 6b 20 3d 20 70 50 61 72 73 . iPk = pPars
44c0: 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 e->nMem+1;. p
44d0: 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e Parse->nMem += n
44e0: 50 6b 3b 0a 20 20 20 20 72 65 67 4b 65 79 20 3d Pk;. regKey =
44f0: 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
4500: 0a 20 20 20 20 69 66 28 20 70 55 70 73 65 72 74 . if( pUpsert
4510: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 45 70 ==0 ){. iEp
4520: 68 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 h = pParse->nTab
4530: 2b 2b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ++;. sqli
4540: 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
4550: 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 50 6b OP_Null, 0, iPk
4560: 2c 20 69 50 6b 2b 6e 50 6b 2d 31 29 3b 0a 20 20 , iPk+nPk-1);.
4570: 20 20 20 20 61 64 64 72 4f 70 65 6e 20 3d 20 73 addrOpen = s
4580: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
4590: 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d (v, OP_OpenEphem
45a0: 65 72 61 6c 2c 20 69 45 70 68 2c 20 6e 50 6b 29 eral, iEph, nPk)
45b0: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
45c0: 64 62 65 53 65 74 50 34 4b 65 79 49 6e 66 6f 28 dbeSetP4KeyInfo(
45d0: 70 50 61 72 73 65 2c 20 70 50 6b 29 3b 0a 20 20 pParse, pPk);.
45e0: 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 }. }. . if(
45f0: 20 70 55 70 73 65 72 74 20 29 7b 0a 20 20 20 20 pUpsert ){.
4600: 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e /* If this is an
4610: 20 55 50 53 45 52 54 2c 20 74 68 65 6e 20 61 6c UPSERT, then al
4620: 6c 20 63 75 72 73 6f 72 73 20 68 61 76 65 20 61 l cursors have a
4630: 6c 72 65 61 64 79 20 62 65 65 6e 20 6f 70 65 6e lready been open
4640: 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 74 68 65 ed by. ** the
4650: 20 6f 75 74 65 72 20 49 4e 53 45 52 54 20 61 6e outer INSERT an
4660: 64 20 74 68 65 20 64 61 74 61 20 63 75 72 73 6f d the data curso
4670: 72 20 73 68 6f 75 6c 64 20 62 65 20 70 6f 69 6e r should be poin
4680: 74 69 6e 67 20 61 74 20 74 68 65 20 72 6f 77 0a ting at the row.
4690: 20 20 20 20 2a 2a 20 74 68 61 74 20 69 73 20 74 ** that is t
46a0: 6f 20 62 65 20 75 70 64 61 74 65 64 2e 20 20 53 o be updated. S
46b0: 6f 20 62 79 70 61 73 73 20 74 68 65 20 63 6f 64 o bypass the cod
46c0: 65 20 74 68 61 74 20 73 65 61 72 63 68 65 73 20 e that searches
46d0: 66 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 for the. ** r
46e0: 6f 77 28 73 29 20 74 6f 20 62 65 20 75 70 64 61 ow(s) to be upda
46f0: 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ted.. */.
4700: 70 57 49 6e 66 6f 20 3d 20 30 3b 0a 20 20 20 20 pWInfo = 0;.
4710: 65 4f 6e 65 50 61 73 73 20 3d 20 4f 4e 45 50 41 eOnePass = ONEPA
4720: 53 53 5f 53 49 4e 47 4c 45 3b 0a 20 20 20 20 73 SS_SINGLE;. s
4730: 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 qlite3ExprIfFals
4740: 65 28 70 50 61 72 73 65 2c 20 70 57 68 65 72 65 e(pParse, pWhere
4750: 2c 20 6c 61 62 65 6c 42 72 65 61 6b 2c 20 53 51 , labelBreak, SQ
4760: 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 LITE_JUMPIFNULL)
4770: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f ;. }else{. /
4780: 2a 20 42 65 67 69 6e 20 74 68 65 20 64 61 74 61 * Begin the data
4790: 62 61 73 65 20 73 63 61 6e 2e 20 0a 20 20 20 20 base scan. .
47a0: 2a 2a 0a 20 20 20 20 2a 2a 20 44 6f 20 6e 6f 74 **. ** Do not
47b0: 20 63 6f 6e 73 69 64 65 72 20 61 20 73 69 6e 67 consider a sing
47c0: 6c 65 2d 70 61 73 73 20 73 74 72 61 74 65 67 79 le-pass strategy
47d0: 20 66 6f 72 20 61 20 6d 75 6c 74 69 2d 72 6f 77 for a multi-row
47e0: 20 75 70 64 61 74 65 20 69 66 0a 20 20 20 20 2a update if. *
47f0: 2a 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 * there are any
4800: 74 72 69 67 67 65 72 73 20 6f 72 20 66 6f 72 65 triggers or fore
4810: 69 67 6e 20 6b 65 79 73 20 74 6f 20 70 72 6f 63 ign keys to proc
4820: 65 73 73 2c 20 6f 72 20 72 6f 77 73 20 6d 61 79 ess, or rows may
4830: 0a 20 20 20 20 2a 2a 20 62 65 20 64 65 6c 65 74 . ** be delet
4840: 65 64 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f ed as a result o
4850: 66 20 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c 69 f REPLACE confli
4860: 63 74 20 68 61 6e 64 6c 69 6e 67 2e 20 41 6e 79 ct handling. Any
4870: 20 6f 66 20 74 68 65 73 65 0a 20 20 20 20 2a 2a of these. **
4880: 20 74 68 69 6e 67 73 20 6d 69 67 68 74 20 64 69 things might di
4890: 73 74 75 72 62 20 61 20 63 75 72 73 6f 72 20 62 sturb a cursor b
48a0: 65 69 6e 67 20 75 73 65 64 20 74 6f 20 73 63 61 eing used to sca
48b0: 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61 n through the ta
48c0: 62 6c 65 0a 20 20 20 20 2a 2a 20 6f 72 20 69 6e ble. ** or in
48d0: 64 65 78 2c 20 63 61 75 73 69 6e 67 20 61 20 73 dex, causing a s
48e0: 69 6e 67 6c 65 2d 70 61 73 73 20 61 70 70 72 6f ingle-pass appro
48f0: 61 63 68 20 74 6f 20 6d 61 6c 66 75 6e 63 74 69 ach to malfuncti
4900: 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 66 6c 61 67 on. */. flag
4910: 73 20 3d 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 s = WHERE_ONEPAS
4920: 53 5f 44 45 53 49 52 45 44 7c 57 48 45 52 45 5f S_DESIRED|WHERE_
4930: 53 45 45 4b 5f 55 4e 49 51 5f 54 41 42 4c 45 3b SEEK_UNIQ_TABLE;
4940: 0a 20 20 20 20 69 66 28 20 21 70 50 61 72 73 65 . if( !pParse
4950: 2d 3e 6e 65 73 74 65 64 20 26 26 20 21 70 54 72 ->nested && !pTr
4960: 69 67 67 65 72 20 26 26 20 21 68 61 73 46 4b 20 igger && !hasFK
4970: 26 26 20 21 63 68 6e 67 4b 65 79 20 26 26 20 21 && !chngKey && !
4980: 62 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 bReplace ){.
4990: 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 flags |= WHERE
49a0: 5f 4f 4e 45 50 41 53 53 5f 4d 55 4c 54 49 52 4f _ONEPASS_MULTIRO
49b0: 57 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 57 49 W;. }. pWI
49c0: 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 nfo = sqlite3Whe
49d0: 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 reBegin(pParse,
49e0: 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 pTabList, pWhere
49f0: 2c 20 30 2c 20 30 2c 20 66 6c 61 67 73 2c 20 69 , 0, 0, flags, i
4a00: 49 64 78 43 75 72 29 3b 0a 20 20 20 20 69 66 28 IdxCur);. if(
4a10: 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 pWInfo==0 ) got
4a20: 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 o update_cleanup
4a30: 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 41 20 6f 6e ;. . /* A on
4a40: 65 2d 70 61 73 73 20 73 74 72 61 74 65 67 79 20 e-pass strategy
4a50: 74 68 61 74 20 6d 69 67 68 74 20 75 70 64 61 74 that might updat
4a60: 65 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 e more than one
4a70: 72 6f 77 20 6d 61 79 20 6e 6f 74 0a 20 20 20 20 row may not.
4a80: 2a 2a 20 62 65 20 75 73 65 64 20 69 66 20 61 6e ** be used if an
4a90: 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 y column of the
4aa0: 69 6e 64 65 78 20 75 73 65 64 20 66 6f 72 20 74 index used for t
4ab0: 68 65 20 73 63 61 6e 20 69 73 20 62 65 69 6e 67 he scan is being
4ac0: 0a 20 20 20 20 2a 2a 20 75 70 64 61 74 65 64 2e . ** updated.
4ad0: 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 Otherwise, if t
4ae0: 68 65 72 65 20 69 73 20 61 6e 20 69 6e 64 65 78 here is an index
4af0: 20 6f 6e 20 22 62 22 2c 20 73 74 61 74 65 6d 65 on "b", stateme
4b00: 6e 74 73 20 6c 69 6b 65 0a 20 20 20 20 2a 2a 20 nts like. **
4b10: 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f the following co
4b20: 75 6c 64 20 63 72 65 61 74 65 20 61 6e 20 69 6e uld create an in
4b30: 66 69 6e 69 74 65 20 6c 6f 6f 70 3a 0a 20 20 20 finite loop:.
4b40: 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 55 50 44 **. ** UPD
4b50: 41 54 45 20 74 31 20 53 45 54 20 62 3d 62 2b 31 ATE t1 SET b=b+1
4b60: 20 57 48 45 52 45 20 62 3e 3f 0a 20 20 20 20 2a WHERE b>?. *
4b70: 2a 0a 20 20 20 20 2a 2a 20 46 61 6c 6c 20 62 61 *. ** Fall ba
4b80: 63 6b 20 74 6f 20 4f 4e 45 50 41 53 53 5f 4f 46 ck to ONEPASS_OF
4b90: 46 20 69 66 20 77 68 65 72 65 2e 63 20 68 61 73 F if where.c has
4ba0: 20 73 65 6c 65 63 74 65 64 20 61 20 4f 4e 45 50 selected a ONEP
4bb0: 41 53 53 5f 4d 55 4c 54 49 0a 20 20 20 20 2a 2a ASS_MULTI. **
4bc0: 20 73 74 72 61 74 65 67 79 20 74 68 61 74 20 75 strategy that u
4bd0: 73 65 73 20 61 6e 20 69 6e 64 65 78 20 66 6f 72 ses an index for
4be0: 20 77 68 69 63 68 20 6f 6e 65 20 6f 72 20 6d 6f which one or mo
4bf0: 72 65 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 62 re columns are b
4c00: 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 70 64 61 eing. ** upda
4c10: 74 65 64 2e 20 20 2a 2f 0a 20 20 20 20 65 4f 6e ted. */. eOn
4c20: 65 50 61 73 73 20 3d 20 73 71 6c 69 74 65 33 57 ePass = sqlite3W
4c30: 68 65 72 65 4f 6b 4f 6e 65 50 61 73 73 28 70 57 hereOkOnePass(pW
4c40: 49 6e 66 6f 2c 20 61 69 43 75 72 4f 6e 65 50 61 Info, aiCurOnePa
4c50: 73 73 29 3b 0a 20 20 20 20 69 66 28 20 65 4f 6e ss);. if( eOn
4c60: 65 50 61 73 73 21 3d 4f 4e 45 50 41 53 53 5f 53 ePass!=ONEPASS_S
4c70: 49 4e 47 4c 45 20 29 7b 0a 20 20 20 20 20 20 73 INGLE ){. s
4c80: 71 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74 65 qlite3MultiWrite
4c90: 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 (pParse);.
4ca0: 69 66 28 20 65 4f 6e 65 50 61 73 73 3d 3d 4f 4e if( eOnePass==ON
4cb0: 45 50 41 53 53 5f 4d 55 4c 54 49 20 29 7b 0a 20 EPASS_MULTI ){.
4cc0: 20 20 20 20 20 20 20 69 6e 74 20 69 43 75 72 20 int iCur
4cd0: 3d 20 61 69 43 75 72 4f 6e 65 50 61 73 73 5b 31 = aiCurOnePass[1
4ce0: 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 ];. if( i
4cf0: 43 75 72 3e 3d 30 20 26 26 20 69 43 75 72 21 3d Cur>=0 && iCur!=
4d00: 69 44 61 74 61 43 75 72 20 26 26 20 61 54 6f 4f iDataCur && aToO
4d10: 70 65 6e 5b 69 43 75 72 2d 69 42 61 73 65 43 75 pen[iCur-iBaseCu
4d20: 72 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 r] ){.
4d30: 65 4f 6e 65 50 61 73 73 20 3d 20 4f 4e 45 50 41 eOnePass = ONEPA
4d40: 53 53 5f 4f 46 46 3b 0a 20 20 20 20 20 20 20 20 SS_OFF;.
4d50: 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 }. assert
4d60: 28 20 69 43 75 72 21 3d 69 44 61 74 61 43 75 72 ( iCur!=iDataCur
4d70: 20 7c 7c 20 21 48 61 73 52 6f 77 69 64 28 70 54 || !HasRowid(pT
4d80: 61 62 29 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ab) );. }.
4d90: 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 }. }.. if(
4da0: 48 61 73 52 6f 77 69 64 28 70 54 61 62 29 20 29 HasRowid(pTab) )
4db0: 7b 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 {. /* Read th
4dc0: 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 63 e rowid of the c
4dd0: 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 urrent row of th
4de0: 65 20 57 48 45 52 45 20 73 63 61 6e 2e 20 49 6e e WHERE scan. In
4df0: 20 4f 4e 45 50 41 53 53 5f 4f 46 46 0a 20 20 20 ONEPASS_OFF.
4e00: 20 2a 2a 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 ** mode, write
4e10: 74 68 65 20 72 6f 77 69 64 20 69 6e 74 6f 20 74 the rowid into t
4e20: 68 65 20 46 49 46 4f 2e 20 49 6e 20 65 69 74 68 he FIFO. In eith
4e30: 65 72 20 6f 66 20 74 68 65 20 6f 6e 65 2d 70 61 er of the one-pa
4e40: 73 73 20 6d 6f 64 65 73 2c 0a 20 20 20 20 2a 2a ss modes,. **
4e50: 20 6c 65 61 76 65 20 69 74 20 69 6e 20 72 65 67 leave it in reg
4e60: 69 73 74 65 72 20 72 65 67 4f 6c 64 52 6f 77 69 ister regOldRowi
4e70: 64 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 d. */. sqlit
4e80: 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
4e90: 4f 50 5f 52 6f 77 69 64 2c 20 69 44 61 74 61 43 OP_Rowid, iDataC
4ea0: 75 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 ur, regOldRowid)
4eb0: 3b 0a 20 20 20 20 69 66 28 20 65 4f 6e 65 50 61 ;. if( eOnePa
4ec0: 73 73 3d 3d 4f 4e 45 50 41 53 53 5f 4f 46 46 20 ss==ONEPASS_OFF
4ed0: 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 6e ){. /* We n
4ee0: 65 65 64 20 74 6f 20 75 73 65 20 72 65 67 52 6f eed to use regRo
4ef0: 77 53 65 74 2c 20 73 6f 20 72 65 61 6c 6c 6f 63 wSet, so realloc
4f00: 61 74 65 20 61 52 65 67 49 64 78 5b 6e 41 6c 6c ate aRegIdx[nAll
4f10: 49 64 78 5d 20 2a 2f 0a 20 20 20 20 20 20 61 52 Idx] */. aR
4f20: 65 67 49 64 78 5b 6e 41 6c 6c 49 64 78 5d 20 3d egIdx[nAllIdx] =
4f30: 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
4f40: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
4f50: 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 beAddOp2(v, OP_R
4f60: 6f 77 53 65 74 41 64 64 2c 20 72 65 67 52 6f 77 owSetAdd, regRow
4f70: 53 65 74 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 Set, regOldRowid
4f80: 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 );. }. }else
4f90: 7b 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 {. /* Read th
4fa0: 65 20 50 4b 20 6f 66 20 74 68 65 20 63 75 72 72 e PK of the curr
4fb0: 65 6e 74 20 72 6f 77 20 69 6e 74 6f 20 61 6e 20 ent row into an
4fc0: 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74 65 array of registe
4fd0: 72 73 2e 20 49 6e 0a 20 20 20 20 2a 2a 20 4f 4e rs. In. ** ON
4fe0: 45 50 41 53 53 5f 4f 46 46 20 6d 6f 64 65 2c 20 EPASS_OFF mode,
4ff0: 73 65 72 69 61 6c 69 7a 65 20 74 68 65 20 61 72 serialize the ar
5000: 72 61 79 20 69 6e 74 6f 20 61 20 72 65 63 6f 72 ray into a recor
5010: 64 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 d and store it i
5020: 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 70 68 n. ** the eph
5030: 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 20 4f 72 emeral table. Or
5040: 2c 20 69 6e 20 4f 4e 45 50 41 53 53 5f 53 49 4e , in ONEPASS_SIN
5050: 47 4c 45 20 6f 72 20 4d 55 4c 54 49 20 6d 6f 64 GLE or MULTI mod
5060: 65 2c 20 63 68 61 6e 67 65 0a 20 20 20 20 2a 2a e, change. **
5070: 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 the OP_OpenEphe
5080: 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f meral instructio
5090: 6e 20 74 6f 20 61 20 4e 6f 6f 70 20 28 74 68 65 n to a Noop (the
50a0: 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 ephemeral table
50b0: 20 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 . ** is not
50c0: 72 65 71 75 69 72 65 64 29 20 61 6e 64 20 6c 65 required) and le
50d0: 61 76 65 20 74 68 65 20 50 4b 20 66 69 65 6c 64 ave the PK field
50e0: 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 6f s in the array o
50f0: 66 20 72 65 67 69 73 74 65 72 73 2e 20 20 2a 2f f registers. */
5100: 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
5110: 6e 50 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 nPk; i++){.
5120: 20 61 73 73 65 72 74 28 20 70 50 6b 2d 3e 61 69 assert( pPk->ai
5130: 43 6f 6c 75 6d 6e 5b 69 5d 3e 3d 30 20 29 3b 0a Column[i]>=0 );.
5140: 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
5150: 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 4f 66 rCodeGetColumnOf
5160: 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 Table(pParse, pT
5170: 61 62 2c 20 69 44 61 74 61 43 75 72 2c 0a 20 20 ab, iDataCur,.
5180: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5190: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
51a0: 20 20 20 20 70 50 6b 2d 3e 61 69 43 6f 6c 75 6d pPk->aiColum
51b0: 6e 5b 69 5d 2c 20 69 50 6b 2b 69 29 3b 0a 20 20 n[i], iPk+i);.
51c0: 20 20 7d 0a 20 20 20 20 69 66 28 20 65 4f 6e 65 }. if( eOne
51d0: 50 61 73 73 20 29 7b 0a 20 20 20 20 20 20 69 66 Pass ){. if
51e0: 28 20 61 64 64 72 4f 70 65 6e 20 29 20 73 71 6c ( addrOpen ) sql
51f0: 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f ite3VdbeChangeTo
5200: 4e 6f 6f 70 28 76 2c 20 61 64 64 72 4f 70 65 6e Noop(v, addrOpen
5210: 29 3b 0a 20 20 20 20 20 20 6e 4b 65 79 20 3d 20 );. nKey =
5220: 6e 50 6b 3b 0a 20 20 20 20 20 20 72 65 67 4b 65 nPk;. regKe
5230: 79 20 3d 20 69 50 6b 3b 0a 20 20 20 20 7d 65 6c y = iPk;. }el
5240: 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 se{. sqlite
5250: 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
5260: 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 69 50 P_MakeRecord, iP
5270: 6b 2c 20 6e 50 6b 2c 20 72 65 67 4b 65 79 2c 0a k, nPk, regKey,.
5280: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5290: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 49 sqlite3I
52a0: 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 ndexAffinityStr(
52b0: 64 62 2c 20 70 50 6b 29 2c 20 6e 50 6b 29 3b 0a db, pPk), nPk);.
52c0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
52d0: 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 eAddOp4Int(v, OP
52e0: 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 45 70 68 _IdxInsert, iEph
52f0: 2c 20 72 65 67 4b 65 79 2c 20 69 50 6b 2c 20 6e , regKey, iPk, n
5300: 50 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a Pk);. }. }..
5310: 20 20 69 66 28 20 70 55 70 73 65 72 74 3d 3d 30 if( pUpsert==0
5320: 20 29 7b 0a 20 20 20 20 69 66 28 20 65 4f 6e 65 ){. if( eOne
5330: 50 61 73 73 21 3d 4f 4e 45 50 41 53 53 5f 4d 55 Pass!=ONEPASS_MU
5340: 4c 54 49 20 29 7b 0a 20 20 20 20 20 20 73 71 6c LTI ){. sql
5350: 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 ite3WhereEnd(pWI
5360: 6e 66 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 nfo);. }. .
5370: 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 if( !isView )
5380: 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 {. int addr
5390: 4f 6e 63 65 20 3d 20 30 3b 0a 20 20 0a 20 20 20 Once = 0;. .
53a0: 20 20 20 2f 2a 20 4f 70 65 6e 20 65 76 65 72 79 /* Open every
53b0: 20 69 6e 64 65 78 20 74 68 61 74 20 6e 65 65 64 index that need
53c0: 73 20 75 70 64 61 74 69 6e 67 2e 20 2a 2f 0a 20 s updating. */.
53d0: 20 20 20 20 20 69 66 28 20 65 4f 6e 65 50 61 73 if( eOnePas
53e0: 73 21 3d 4f 4e 45 50 41 53 53 5f 4f 46 46 20 29 s!=ONEPASS_OFF )
53f0: 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 69 {. if( ai
5400: 43 75 72 4f 6e 65 50 61 73 73 5b 30 5d 3e 3d 30 CurOnePass[0]>=0
5410: 20 29 20 61 54 6f 4f 70 65 6e 5b 61 69 43 75 72 ) aToOpen[aiCur
5420: 4f 6e 65 50 61 73 73 5b 30 5d 2d 69 42 61 73 65 OnePass[0]-iBase
5430: 43 75 72 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 Cur] = 0;.
5440: 20 20 69 66 28 20 61 69 43 75 72 4f 6e 65 50 61 if( aiCurOnePa
5450: 73 73 5b 31 5d 3e 3d 30 20 29 20 61 54 6f 4f 70 ss[1]>=0 ) aToOp
5460: 65 6e 5b 61 69 43 75 72 4f 6e 65 50 61 73 73 5b en[aiCurOnePass[
5470: 31 5d 2d 69 42 61 73 65 43 75 72 5d 20 3d 20 30 1]-iBaseCur] = 0
5480: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 ;. }. .
5490: 20 20 20 69 66 28 20 65 4f 6e 65 50 61 73 73 3d if( eOnePass=
54a0: 3d 4f 4e 45 50 41 53 53 5f 4d 55 4c 54 49 20 26 =ONEPASS_MULTI &
54b0: 26 20 28 6e 49 64 78 2d 28 61 69 43 75 72 4f 6e & (nIdx-(aiCurOn
54c0: 65 50 61 73 73 5b 31 5d 3e 3d 30 29 29 3e 30 20 ePass[1]>=0))>0
54d0: 29 7b 0a 20 20 20 20 20 20 20 20 61 64 64 72 4f ){. addrO
54e0: 6e 63 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 nce = sqlite3Vdb
54f0: 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 4f 6e eAddOp0(v, OP_On
5500: 63 65 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 ce); VdbeCoverag
5510: 65 28 76 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 e(v);. }.
5520: 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 sqlite3OpenT
5530: 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70 ableAndIndices(p
5540: 50 61 72 73 65 2c 20 70 54 61 62 2c 20 4f 50 5f Parse, pTab, OP_
5550: 4f 70 65 6e 57 72 69 74 65 2c 20 30 2c 20 69 42 OpenWrite, 0, iB
5560: 61 73 65 43 75 72 2c 0a 20 20 20 20 20 20 20 20 aseCur,.
5570: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5580: 20 20 20 20 20 20 20 20 20 61 54 6f 4f 70 65 6e aToOpen
5590: 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 , 0, 0);. i
55a0: 66 28 20 61 64 64 72 4f 6e 63 65 20 29 20 73 71 f( addrOnce ) sq
55b0: 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
55c0: 65 28 76 2c 20 61 64 64 72 4f 6e 63 65 29 3b 0a e(v, addrOnce);.
55d0: 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 }. . /*
55e0: 54 6f 70 20 6f 66 20 74 68 65 20 75 70 64 61 74 Top of the updat
55f0: 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 69 66 e loop */. if
5600: 28 20 65 4f 6e 65 50 61 73 73 21 3d 4f 4e 45 50 ( eOnePass!=ONEP
5610: 41 53 53 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20 ASS_OFF ){.
5620: 20 69 66 28 20 21 69 73 56 69 65 77 20 26 26 20 if( !isView &&
5630: 61 69 43 75 72 4f 6e 65 50 61 73 73 5b 30 5d 21 aiCurOnePass[0]!
5640: 3d 69 44 61 74 61 43 75 72 20 26 26 20 61 69 43 =iDataCur && aiC
5650: 75 72 4f 6e 65 50 61 73 73 5b 31 5d 21 3d 69 44 urOnePass[1]!=iD
5660: 61 74 61 43 75 72 20 29 7b 0a 20 20 20 20 20 20 ataCur ){.
5670: 20 20 61 73 73 65 72 74 28 20 70 50 6b 20 29 3b assert( pPk );
5680: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
5690: 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c VdbeAddOp4Int(v,
56a0: 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 69 44 OP_NotFound, iD
56b0: 61 74 61 43 75 72 2c 20 6c 61 62 65 6c 42 72 65 ataCur, labelBre
56c0: 61 6b 2c 20 72 65 67 4b 65 79 2c 6e 4b 65 79 29 ak, regKey,nKey)
56d0: 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f ;. VdbeCo
56e0: 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 20 verage(v);.
56f0: 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65 4f 6e }. if( eOn
5700: 65 50 61 73 73 21 3d 4f 4e 45 50 41 53 53 5f 53 ePass!=ONEPASS_S
5710: 49 4e 47 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 INGLE ){.
5720: 20 6c 61 62 65 6c 43 6f 6e 74 69 6e 75 65 20 3d labelContinue =
5730: 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 sqlite3VdbeMake
5740: 4c 61 62 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 Label(pParse);.
5750: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
5760: 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
5770: 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 70 50 6b , OP_IsNull, pPk
5780: 20 3f 20 72 65 67 4b 65 79 20 3a 20 72 65 67 4f ? regKey : regO
5790: 6c 64 52 6f 77 69 64 2c 20 6c 61 62 65 6c 42 72 ldRowid, labelBr
57a0: 65 61 6b 29 3b 0a 20 20 20 20 20 20 56 64 62 65 eak);. Vdbe
57b0: 43 6f 76 65 72 61 67 65 49 66 28 76 2c 20 70 50 CoverageIf(v, pP
57c0: 6b 3d 3d 30 29 3b 0a 20 20 20 20 20 20 56 64 62 k==0);. Vdb
57d0: 65 43 6f 76 65 72 61 67 65 49 66 28 76 2c 20 70 eCoverageIf(v, p
57e0: 50 6b 21 3d 30 29 3b 0a 20 20 20 20 7d 65 6c 73 Pk!=0);. }els
57f0: 65 20 69 66 28 20 70 50 6b 20 29 7b 0a 20 20 20 e if( pPk ){.
5800: 20 20 20 6c 61 62 65 6c 43 6f 6e 74 69 6e 75 65 labelContinue
5810: 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 = sqlite3VdbeMa
5820: 6b 65 4c 61 62 65 6c 28 70 50 61 72 73 65 29 3b keLabel(pParse);
5830: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
5840: 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 beAddOp2(v, OP_R
5850: 65 77 69 6e 64 2c 20 69 45 70 68 2c 20 6c 61 62 ewind, iEph, lab
5860: 65 6c 42 72 65 61 6b 29 3b 20 56 64 62 65 43 6f elBreak); VdbeCo
5870: 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 20 verage(v);.
5880: 20 61 64 64 72 54 6f 70 20 3d 20 73 71 6c 69 74 addrTop = sqlit
5890: 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
58a0: 4f 50 5f 52 6f 77 44 61 74 61 2c 20 69 45 70 68 OP_RowData, iEph
58b0: 2c 20 72 65 67 4b 65 79 29 3b 0a 20 20 20 20 20 , regKey);.
58c0: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
58d0: 70 34 49 6e 74 28 76 2c 20 4f 50 5f 4e 6f 74 46 p4Int(v, OP_NotF
58e0: 6f 75 6e 64 2c 20 69 44 61 74 61 43 75 72 2c 20 ound, iDataCur,
58f0: 6c 61 62 65 6c 43 6f 6e 74 69 6e 75 65 2c 20 72 labelContinue, r
5900: 65 67 4b 65 79 2c 20 30 29 3b 0a 20 20 20 20 20 egKey, 0);.
5910: 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 VdbeCoverage(v)
5920: 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
5930: 20 20 20 6c 61 62 65 6c 43 6f 6e 74 69 6e 75 65 labelContinue
5940: 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
5950: 64 4f 70 33 28 76 2c 20 4f 50 5f 52 6f 77 53 65 dOp3(v, OP_RowSe
5960: 74 52 65 61 64 2c 20 72 65 67 52 6f 77 53 65 74 tRead, regRowSet
5970: 2c 6c 61 62 65 6c 42 72 65 61 6b 2c 0a 20 20 20 ,labelBreak,.
5980: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
5990: 20 20 20 20 20 20 20 20 20 20 20 20 72 65 67 4f regO
59a0: 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 ldRowid);.
59b0: 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b VdbeCoverage(v);
59c0: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
59d0: 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e beAddOp3(v, OP_N
59e0: 6f 74 45 78 69 73 74 73 2c 20 69 44 61 74 61 43 otExists, iDataC
59f0: 75 72 2c 20 6c 61 62 65 6c 43 6f 6e 74 69 6e 75 ur, labelContinu
5a00: 65 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b e, regOldRowid);
5a10: 0a 20 20 20 20 20 20 56 64 62 65 43 6f 76 65 72 . VdbeCover
5a20: 61 67 65 28 76 29 3b 0a 20 20 20 20 7d 0a 20 20 age(v);. }.
5a30: 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 72 }.. /* If the r
5a40: 6f 77 69 64 20 76 61 6c 75 65 20 77 69 6c 6c 20 owid value will
5a50: 63 68 61 6e 67 65 2c 20 73 65 74 20 72 65 67 69 change, set regi
5a60: 73 74 65 72 20 72 65 67 4e 65 77 52 6f 77 69 64 ster regNewRowid
5a70: 20 74 6f 0a 20 20 2a 2a 20 63 6f 6e 74 61 69 6e to. ** contain
5a80: 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e 20 the new value.
5a90: 49 66 20 74 68 65 20 72 6f 77 69 64 20 69 73 20 If the rowid is
5aa0: 6e 6f 74 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 not being modifi
5ab0: 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 ed,. ** then re
5ac0: 67 4e 65 77 52 6f 77 69 64 20 69 73 20 74 68 65 gNewRowid is the
5ad0: 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 20 61 same register a
5ae0: 73 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 77 s regOldRowid, w
5af0: 68 69 63 68 20 69 73 0a 20 20 2a 2a 20 61 6c 72 hich is. ** alr
5b00: 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 2e 20 eady populated.
5b10: 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 63 68 */. assert( ch
5b20: 6e 67 4b 65 79 20 7c 7c 20 70 54 72 69 67 67 65 ngKey || pTrigge
5b30: 72 20 7c 7c 20 68 61 73 46 4b 20 7c 7c 20 72 65 r || hasFK || re
5b40: 67 4f 6c 64 52 6f 77 69 64 3d 3d 72 65 67 4e 65 gOldRowid==regNe
5b50: 77 52 6f 77 69 64 20 29 3b 0a 20 20 69 66 28 20 wRowid );. if(
5b60: 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 chngRowid ){.
5b70: 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
5b80: 28 70 50 61 72 73 65 2c 20 70 52 6f 77 69 64 45 (pParse, pRowidE
5b90: 78 70 72 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 xpr, regNewRowid
5ba0: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
5bb0: 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d beAddOp1(v, OP_M
5bc0: 75 73 74 42 65 49 6e 74 2c 20 72 65 67 4e 65 77 ustBeInt, regNew
5bd0: 52 6f 77 69 64 29 3b 20 56 64 62 65 43 6f 76 65 Rowid); VdbeCove
5be0: 72 61 67 65 28 76 29 3b 0a 20 20 7d 0a 0a 20 20 rage(v);. }..
5bf0: 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6f /* Compute the o
5c00: 6c 64 20 70 72 65 2d 55 50 44 41 54 45 20 63 6f ld pre-UPDATE co
5c10: 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 72 6f 77 ntent of the row
5c20: 20 62 65 69 6e 67 20 63 68 61 6e 67 65 64 2c 20 being changed,
5c30: 69 66 20 74 68 61 74 0a 20 20 2a 2a 20 69 6e 66 if that. ** inf
5c40: 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6e 65 65 64 ormation is need
5c50: 65 64 20 2a 2f 0a 20 20 69 66 28 20 63 68 6e 67 ed */. if( chng
5c60: 50 6b 20 7c 7c 20 68 61 73 46 4b 20 7c 7c 20 70 Pk || hasFK || p
5c70: 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 75 Trigger ){. u
5c80: 33 32 20 6f 6c 64 6d 61 73 6b 20 3d 20 28 68 61 32 oldmask = (ha
5c90: 73 46 4b 20 3f 20 73 71 6c 69 74 65 33 46 6b 4f sFK ? sqlite3FkO
5ca0: 6c 64 6d 61 73 6b 28 70 50 61 72 73 65 2c 20 70 ldmask(pParse, p
5cb0: 54 61 62 29 20 3a 20 30 29 3b 0a 20 20 20 20 6f Tab) : 0);. o
5cc0: 6c 64 6d 61 73 6b 20 7c 3d 20 73 71 6c 69 74 65 ldmask |= sqlite
5cd0: 33 54 72 69 67 67 65 72 43 6f 6c 6d 61 73 6b 28 3TriggerColmask(
5ce0: 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 pParse, .
5cf0: 20 70 54 72 69 67 67 65 72 2c 20 70 43 68 61 6e pTrigger, pChan
5d00: 67 65 73 2c 20 30 2c 20 54 52 49 47 47 45 52 5f ges, 0, TRIGGER_
5d10: 42 45 46 4f 52 45 7c 54 52 49 47 47 45 52 5f 41 BEFORE|TRIGGER_A
5d20: 46 54 45 52 2c 20 70 54 61 62 2c 20 6f 6e 45 72 FTER, pTab, onEr
5d30: 72 6f 72 0a 20 20 20 20 29 3b 0a 20 20 20 20 66 ror. );. f
5d40: 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e or(i=0; i<pTab->
5d50: 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 nCol; i++){.
5d60: 20 20 69 66 28 20 6f 6c 64 6d 61 73 6b 3d 3d 30 if( oldmask==0
5d70: 78 66 66 66 66 66 66 66 66 0a 20 20 20 20 20 20 xffffffff.
5d80: 20 7c 7c 20 28 69 3c 33 32 20 26 26 20 28 6f 6c || (i<32 && (ol
5d90: 64 6d 61 73 6b 20 26 20 4d 41 53 4b 42 49 54 33 dmask & MASKBIT3
5da0: 32 28 69 29 29 21 3d 30 29 0a 20 20 20 20 20 20 2(i))!=0).
5db0: 20 7c 7c 20 28 70 54 61 62 2d 3e 61 43 6f 6c 5b || (pTab->aCol[
5dc0: 69 5d 2e 63 6f 6c 46 6c 61 67 73 20 26 20 43 4f i].colFlags & CO
5dd0: 4c 46 4c 41 47 5f 50 52 49 4d 4b 45 59 29 21 3d LFLAG_PRIMKEY)!=
5de0: 30 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 0. ){.
5df0: 20 20 20 74 65 73 74 63 61 73 65 28 20 20 6f 6c testcase( ol
5e00: 64 6d 61 73 6b 21 3d 30 78 66 66 66 66 66 66 66 dmask!=0xfffffff
5e10: 66 20 26 26 20 69 3d 3d 33 31 20 29 3b 0a 20 20 f && i==31 );.
5e20: 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
5e30: 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 4f 66 rCodeGetColumnOf
5e40: 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 Table(pParse, pT
5e50: 61 62 2c 20 69 44 61 74 61 43 75 72 2c 20 69 2c ab, iDataCur, i,
5e60: 20 72 65 67 4f 6c 64 2b 69 29 3b 0a 20 20 20 20 regOld+i);.
5e70: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
5e80: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
5e90: 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 p2(v, OP_Null, 0
5ea0: 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0a 20 20 20 , regOld+i);.
5eb0: 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 }. }. i
5ec0: 66 28 20 63 68 6e 67 52 6f 77 69 64 3d 3d 30 20 f( chngRowid==0
5ed0: 26 26 20 70 50 6b 3d 3d 30 20 29 7b 0a 20 20 20 && pPk==0 ){.
5ee0: 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
5ef0: 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c dOp2(v, OP_Copy,
5f00: 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 72 65 regOldRowid, re
5f10: 67 4e 65 77 52 6f 77 69 64 29 3b 0a 20 20 20 20 gNewRowid);.
5f20: 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 6f 70 75 }. }.. /* Popu
5f30: 6c 61 74 65 20 74 68 65 20 61 72 72 61 79 20 6f late the array o
5f40: 66 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 f registers begi
5f50: 6e 6e 69 6e 67 20 61 74 20 72 65 67 4e 65 77 20 nning at regNew
5f60: 77 69 74 68 20 74 68 65 20 6e 65 77 0a 20 20 2a with the new. *
5f70: 2a 20 72 6f 77 20 64 61 74 61 2e 20 54 68 69 73 * row data. This
5f80: 20 61 72 72 61 79 20 69 73 20 75 73 65 64 20 74 array is used t
5f90: 6f 20 63 68 65 63 6b 20 63 6f 6e 73 74 61 6e 74 o check constant
5fa0: 73 2c 20 63 72 65 61 74 65 20 74 68 65 20 6e 65 s, create the ne
5fb0: 77 0a 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 w. ** table and
5fc0: 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2c 20 index records,
5fd0: 61 6e 64 20 61 73 20 74 68 65 20 76 61 6c 75 65 and as the value
5fe0: 73 20 66 6f 72 20 61 6e 79 20 6e 65 77 2e 2a 20 s for any new.*
5ff0: 72 65 66 65 72 65 6e 63 65 73 0a 20 20 2a 2a 20 references. **
6000: 6d 61 64 65 20 62 79 20 74 72 69 67 67 65 72 73 made by triggers
6010: 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 .. **. ** If t
6020: 68 65 72 65 20 61 72 65 20 6f 6e 65 20 6f 72 20 here are one or
6030: 6d 6f 72 65 20 42 45 46 4f 52 45 20 74 72 69 67 more BEFORE trig
6040: 67 65 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f gers, then do no
6050: 74 20 70 6f 70 75 6c 61 74 65 20 74 68 65 0a 20 t populate the.
6060: 20 2a 2a 20 72 65 67 69 73 74 65 72 73 20 61 73 ** registers as
6070: 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 6f sociated with co
6080: 6c 75 6d 6e 73 20 74 68 61 74 20 61 72 65 20 28 lumns that are (
6090: 61 29 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20 a) not modified
60a0: 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 55 50 44 by. ** this UPD
60b0: 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 61 6e ATE statement an
60c0: 64 20 28 62 29 20 6e 6f 74 20 61 63 63 65 73 73 d (b) not access
60d0: 65 64 20 62 79 20 6e 65 77 2e 2a 20 72 65 66 65 ed by new.* refe
60e0: 72 65 6e 63 65 73 2e 20 54 68 65 0a 20 20 2a 2a rences. The. **
60f0: 20 76 61 6c 75 65 73 20 66 6f 72 20 72 65 67 69 values for regi
6100: 73 74 65 72 73 20 6e 6f 74 20 6d 6f 64 69 66 69 sters not modifi
6110: 65 64 20 62 79 20 74 68 65 20 55 50 44 41 54 45 ed by the UPDATE
6120: 20 6d 75 73 74 20 62 65 20 72 65 6c 6f 61 64 65 must be reloade
6130: 64 20 66 72 6f 6d 20 0a 20 20 2a 2a 20 74 68 65 d from . ** the
6140: 20 64 61 74 61 62 61 73 65 20 61 66 74 65 72 20 database after
6150: 74 68 65 20 42 45 46 4f 52 45 20 74 72 69 67 67 the BEFORE trigg
6160: 65 72 73 20 61 72 65 20 66 69 72 65 64 20 61 6e ers are fired an
6170: 79 77 61 79 20 28 61 73 20 74 68 65 20 74 72 69 yway (as the tri
6180: 67 67 65 72 20 0a 20 20 2a 2a 20 6d 61 79 20 68 gger . ** may h
6190: 61 76 65 20 6d 6f 64 69 66 69 65 64 20 74 68 65 ave modified the
61a0: 6d 29 2e 20 53 6f 20 6e 6f 74 20 6c 6f 61 64 69 m). So not loadi
61b0: 6e 67 20 74 68 6f 73 65 20 74 68 61 74 20 61 72 ng those that ar
61c0: 65 20 6e 6f 74 20 67 6f 69 6e 67 20 74 6f 0a 20 e not going to.
61d0: 20 2a 2a 20 62 65 20 75 73 65 64 20 65 6c 69 6d ** be used elim
61e0: 69 6e 61 74 65 73 20 73 6f 6d 65 20 72 65 64 75 inates some redu
61f0: 6e 64 61 6e 74 20 6f 70 63 6f 64 65 73 2e 0a 20 ndant opcodes..
6200: 20 2a 2f 0a 20 20 6e 65 77 6d 61 73 6b 20 3d 20 */. newmask =
6210: 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 43 6f sqlite3TriggerCo
6220: 6c 6d 61 73 6b 28 0a 20 20 20 20 20 20 70 50 61 lmask(. pPa
6230: 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 70 rse, pTrigger, p
6240: 43 68 61 6e 67 65 73 2c 20 31 2c 20 54 52 49 47 Changes, 1, TRIG
6250: 47 45 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 GER_BEFORE, pTab
6260: 2c 20 6f 6e 45 72 72 6f 72 0a 20 20 29 3b 0a 20 , onError. );.
6270: 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 for(i=0; i<pTab
6280: 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 ->nCol; i++){.
6290: 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 if( i==pTab->i
62a0: 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 73 71 PKey ){. sq
62b0: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
62c0: 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 v, OP_Null, 0, r
62d0: 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 7d 65 egNew+i);. }e
62e0: 6c 73 65 7b 0a 20 20 20 20 20 20 6a 20 3d 20 61 lse{. j = a
62f0: 58 52 65 66 5b 69 5d 3b 0a 20 20 20 20 20 20 69 XRef[i];. i
6300: 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20 20 f( j>=0 ){.
6310: 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f sqlite3ExprCo
6320: 64 65 28 70 50 61 72 73 65 2c 20 70 43 68 61 6e de(pParse, pChan
6330: 67 65 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c ges->a[j].pExpr,
6340: 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 regNew+i);.
6350: 20 20 7d 65 6c 73 65 20 69 66 28 20 30 3d 3d 28 }else if( 0==(
6360: 74 6d 61 73 6b 26 54 52 49 47 47 45 52 5f 42 45 tmask&TRIGGER_BE
6370: 46 4f 52 45 29 20 7c 7c 20 69 3e 33 31 20 7c 7c FORE) || i>31 ||
6380: 20 28 6e 65 77 6d 61 73 6b 20 26 20 4d 41 53 4b (newmask & MASK
6390: 42 49 54 33 32 28 69 29 29 20 29 7b 0a 20 20 20 BIT32(i)) ){.
63a0: 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 /* This bra
63b0: 6e 63 68 20 6c 6f 61 64 73 20 74 68 65 20 76 61 nch loads the va
63c0: 6c 75 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 lue of a column
63d0: 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 that will not be
63e0: 20 63 68 61 6e 67 65 64 20 0a 20 20 20 20 20 20 changed .
63f0: 20 20 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67 69 ** into a regi
6400: 73 74 65 72 2e 20 54 68 69 73 20 69 73 20 64 6f ster. This is do
6410: 6e 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 ne if there are
6420: 6e 6f 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 no BEFORE trigge
6430: 72 73 2c 20 6f 72 0a 20 20 20 20 20 20 20 20 2a rs, or. *
6440: 2a 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6f * if there are o
6450: 6e 65 20 6f 72 20 6d 6f 72 65 20 42 45 46 4f 52 ne or more BEFOR
6460: 45 20 74 72 69 67 67 65 72 73 20 74 68 61 74 20 E triggers that
6470: 75 73 65 20 74 68 69 73 20 76 61 6c 75 65 20 76 use this value v
6480: 69 61 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 20 ia. ** a
6490: 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 20 new.* reference
64a0: 69 6e 20 61 20 74 72 69 67 67 65 72 20 70 72 6f in a trigger pro
64b0: 67 72 61 6d 2e 0a 20 20 20 20 20 20 20 20 2a 2f gram.. */
64c0: 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 . testcas
64d0: 65 28 20 69 3d 3d 33 31 20 29 3b 0a 20 20 20 20 e( i==31 );.
64e0: 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d testcase( i=
64f0: 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20 20 73 =32 );. s
6500: 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 qlite3ExprCodeGe
6510: 74 43 6f 6c 75 6d 6e 4f 66 54 61 62 6c 65 28 70 tColumnOfTable(p
6520: 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 44 61 Parse, pTab, iDa
6530: 74 61 43 75 72 2c 20 69 2c 20 72 65 67 4e 65 77 taCur, i, regNew
6540: 2b 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 +i);. }else
6550: 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
6560: 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
6570: 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 4e 65 P_Null, 0, regNe
6580: 77 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 w+i);. }.
6590: 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 }. }.. /* Fi
65a0: 72 65 20 61 6e 79 20 42 45 46 4f 52 45 20 55 50 re any BEFORE UP
65b0: 44 41 54 45 20 74 72 69 67 67 65 72 73 2e 20 54 DATE triggers. T
65c0: 68 69 73 20 68 61 70 70 65 6e 73 20 62 65 66 6f his happens befo
65d0: 72 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 re constraints a
65e0: 72 65 0a 20 20 2a 2a 20 76 65 72 69 66 69 65 64 re. ** verified
65f0: 2e 20 4f 6e 65 20 63 6f 75 6c 64 20 61 72 67 75 . One could argu
6600: 65 20 74 68 61 74 20 74 68 69 73 20 69 73 20 77 e that this is w
6610: 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 rong.. */. if(
6620: 20 74 6d 61 73 6b 26 54 52 49 47 47 45 52 5f 42 tmask&TRIGGER_B
6630: 45 46 4f 52 45 20 29 7b 0a 20 20 20 20 73 71 6c EFORE ){. sql
6640: 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 ite3TableAffinit
6650: 79 28 76 2c 20 70 54 61 62 2c 20 72 65 67 4e 65 y(v, pTab, regNe
6660: 77 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 w);. sqlite3C
6670: 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 odeRowTrigger(pP
6680: 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 arse, pTrigger,
6690: 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68 61 6e TK_UPDATE, pChan
66a0: 67 65 73 2c 20 0a 20 20 20 20 20 20 20 20 54 52 ges, . TR
66b0: 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 70 54 IGGER_BEFORE, pT
66c0: 61 62 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c ab, regOldRowid,
66d0: 20 6f 6e 45 72 72 6f 72 2c 20 6c 61 62 65 6c 43 onError, labelC
66e0: 6f 6e 74 69 6e 75 65 29 3b 0a 0a 20 20 20 20 2f ontinue);.. /
66f0: 2a 20 54 68 65 20 72 6f 77 2d 74 72 69 67 67 65 * The row-trigge
6700: 72 20 6d 61 79 20 68 61 76 65 20 64 65 6c 65 74 r may have delet
6710: 65 64 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 ed the row being
6720: 20 75 70 64 61 74 65 64 2e 20 49 6e 20 74 68 69 updated. In thi
6730: 73 0a 20 20 20 20 2a 2a 20 63 61 73 65 2c 20 6a s. ** case, j
6740: 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 ump to the next
6750: 72 6f 77 2e 20 4e 6f 20 75 70 64 61 74 65 73 20 row. No updates
6760: 6f 72 20 41 46 54 45 52 20 74 72 69 67 67 65 72 or AFTER trigger
6770: 73 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 72 65 s are . ** re
6780: 71 75 69 72 65 64 2e 20 54 68 69 73 20 62 65 68 quired. This beh
6790: 61 76 69 6f 72 20 2d 20 77 68 61 74 20 68 61 70 avior - what hap
67a0: 70 65 6e 73 20 77 68 65 6e 20 74 68 65 20 72 6f pens when the ro
67b0: 77 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 0a w being updated.
67c0: 20 20 20 20 2a 2a 20 69 73 20 64 65 6c 65 74 65 ** is delete
67d0: 64 20 6f 72 20 72 65 6e 61 6d 65 64 20 62 79 20 d or renamed by
67e0: 61 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72 a BEFORE trigger
67f0: 20 2d 20 69 73 20 6c 65 66 74 20 75 6e 64 65 66 - is left undef
6800: 69 6e 65 64 20 69 6e 20 74 68 65 0a 20 20 20 20 ined in the.
6810: 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e ** documentation
6820: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
6830: 20 70 50 6b 20 29 7b 0a 20 20 20 20 20 20 73 71 pPk ){. sq
6840: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 lite3VdbeAddOp4I
6850: 6e 74 28 76 2c 20 4f 50 5f 4e 6f 74 46 6f 75 6e nt(v, OP_NotFoun
6860: 64 2c 20 69 44 61 74 61 43 75 72 2c 20 6c 61 62 d, iDataCur, lab
6870: 65 6c 43 6f 6e 74 69 6e 75 65 2c 72 65 67 4b 65 elContinue,regKe
6880: 79 2c 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 56 y,nKey);. V
6890: 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b 0a dbeCoverage(v);.
68a0: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
68b0: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
68c0: 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 p3(v, OP_NotExis
68d0: 74 73 2c 20 69 44 61 74 61 43 75 72 2c 20 6c 61 ts, iDataCur, la
68e0: 62 65 6c 43 6f 6e 74 69 6e 75 65 2c 20 72 65 67 belContinue, reg
68f0: 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 OldRowid);.
6900: 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 VdbeCoverage(v)
6910: 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 ;. }.. /*
6920: 41 66 74 65 72 2d 42 45 46 4f 52 45 2d 74 72 69 After-BEFORE-tri
6930: 67 67 65 72 2d 72 65 6c 6f 61 64 2d 6c 6f 6f 70 gger-reload-loop
6940: 3a 0a 20 20 20 20 2a 2a 20 49 66 20 69 74 20 64 :. ** If it d
6950: 69 64 20 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 id not delete it
6960: 2c 20 74 68 65 20 42 45 46 4f 52 45 20 74 72 69 , the BEFORE tri
6970: 67 67 65 72 20 6d 61 79 20 73 74 69 6c 6c 20 68 gger may still h
6980: 61 76 65 20 6d 6f 64 69 66 69 65 64 20 0a 20 20 ave modified .
6990: 20 20 2a 2a 20 73 6f 6d 65 20 6f 66 20 74 68 65 ** some of the
69a0: 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 columns of the
69b0: 72 6f 77 20 62 65 69 6e 67 20 75 70 64 61 74 65 row being update
69c0: 64 2e 20 4c 6f 61 64 20 74 68 65 20 76 61 6c 75 d. Load the valu
69d0: 65 73 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 61 es for . ** a
69e0: 6c 6c 20 63 6f 6c 75 6d 6e 73 20 6e 6f 74 20 6d ll columns not m
69f0: 6f 64 69 66 69 65 64 20 62 79 20 74 68 65 20 75 odified by the u
6a00: 70 64 61 74 65 20 73 74 61 74 65 6d 65 6e 74 20 pdate statement
6a10: 69 6e 74 6f 20 74 68 65 69 72 20 72 65 67 69 73 into their regis
6a20: 74 65 72 73 0a 20 20 20 20 2a 2a 20 69 6e 20 63 ters. ** in c
6a30: 61 73 65 20 74 68 69 73 20 68 61 73 20 68 61 70 ase this has hap
6a40: 70 65 6e 65 64 2e 20 4f 6e 6c 79 20 75 6e 6d 6f pened. Only unmo
6a50: 64 69 66 69 65 64 20 63 6f 6c 75 6d 6e 73 20 61 dified columns a
6a60: 72 65 20 72 65 6c 6f 61 64 65 64 2e 0a 20 20 20 re reloaded..
6a70: 20 2a 2a 20 54 68 65 20 76 61 6c 75 65 73 20 63 ** The values c
6a80: 6f 6d 70 75 74 65 64 20 66 6f 72 20 6d 6f 64 69 omputed for modi
6a90: 66 69 65 64 20 63 6f 6c 75 6d 6e 73 20 75 73 65 fied columns use
6aa0: 20 74 68 65 20 76 61 6c 75 65 73 20 62 65 66 6f the values befo
6ab0: 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 42 45 re the. ** BE
6ac0: 46 4f 52 45 20 74 72 69 67 67 65 72 20 72 75 6e FORE trigger run
6ad0: 73 2e 20 20 53 65 65 20 74 65 73 74 20 63 61 73 s. See test cas
6ae0: 65 20 74 72 69 67 67 65 72 31 2d 31 38 2e 30 20 e trigger1-18.0
6af0: 28 61 64 64 65 64 20 32 30 31 38 2d 30 34 2d 32 (added 2018-04-2
6b00: 36 29 0a 20 20 20 20 2a 2a 20 66 6f 72 20 61 6e 6). ** for an
6b10: 20 65 78 61 6d 70 6c 65 2e 0a 20 20 20 20 2a 2f example.. */
6b20: 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
6b30: 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 pTab->nCol; i++)
6b40: 7b 0a 20 20 20 20 20 20 69 66 28 20 61 58 52 65 {. if( aXRe
6b50: 66 5b 69 5d 3c 30 20 26 26 20 69 21 3d 70 54 61 f[i]<0 && i!=pTa
6b60: 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 b->iPKey ){.
6b70: 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
6b80: 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 4f 66 54 61 odeGetColumnOfTa
6b90: 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 ble(pParse, pTab
6ba0: 2c 20 69 44 61 74 61 43 75 72 2c 20 69 2c 20 72 , iDataCur, i, r
6bb0: 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20 20 egNew+i);.
6bc0: 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 }. }. }.. i
6bd0: 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 f( !isView ){.
6be0: 20 20 2f 2a 20 44 6f 20 63 6f 6e 73 74 72 61 69 /* Do constrai
6bf0: 6e 74 20 63 68 65 63 6b 73 2e 20 2a 2f 0a 20 20 nt checks. */.
6c00: 20 20 61 73 73 65 72 74 28 20 72 65 67 4f 6c 64 assert( regOld
6c10: 52 6f 77 69 64 3e 30 20 29 3b 0a 20 20 20 20 73 Rowid>0 );. s
6c20: 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f qlite3GenerateCo
6c30: 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28 70 nstraintChecks(p
6c40: 50 61 72 73 65 2c 20 70 54 61 62 2c 20 61 52 65 Parse, pTab, aRe
6c50: 67 49 64 78 2c 20 69 44 61 74 61 43 75 72 2c 20 gIdx, iDataCur,
6c60: 69 49 64 78 43 75 72 2c 0a 20 20 20 20 20 20 20 iIdxCur,.
6c70: 20 72 65 67 4e 65 77 52 6f 77 69 64 2c 20 72 65 regNewRowid, re
6c80: 67 4f 6c 64 52 6f 77 69 64 2c 20 63 68 6e 67 4b gOldRowid, chngK
6c90: 65 79 2c 20 6f 6e 45 72 72 6f 72 2c 20 6c 61 62 ey, onError, lab
6ca0: 65 6c 43 6f 6e 74 69 6e 75 65 2c 20 26 62 52 65 elContinue, &bRe
6cb0: 70 6c 61 63 65 2c 0a 20 20 20 20 20 20 20 20 61 place,. a
6cc0: 58 52 65 66 2c 20 30 29 3b 0a 0a 20 20 20 20 2f XRef, 0);.. /
6cd0: 2a 20 49 66 20 52 45 50 4c 41 43 45 20 63 6f 6e * If REPLACE con
6ce0: 66 6c 69 63 74 20 68 61 6e 64 6c 69 6e 67 20 6d flict handling m
6cf0: 61 79 20 68 61 76 65 20 62 65 65 6e 20 75 73 65 ay have been use
6d00: 64 2c 20 6f 72 20 69 66 20 74 68 65 20 50 4b 20 d, or if the PK
6d10: 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 6f of the. ** ro
6d20: 77 20 69 73 20 63 68 61 6e 67 69 6e 67 2c 20 74 w is changing, t
6d30: 68 65 6e 20 74 68 65 20 47 65 6e 65 72 61 74 65 hen the Generate
6d40: 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 ConstraintChecks
6d50: 28 29 20 61 62 6f 76 65 20 6d 61 79 20 68 61 76 () above may hav
6d60: 65 0a 20 20 20 20 2a 2a 20 6d 6f 76 65 64 20 63 e. ** moved c
6d70: 75 72 73 6f 72 20 69 44 61 74 61 43 75 72 2e 20 ursor iDataCur.
6d80: 52 65 73 65 65 6b 20 69 74 2e 20 2a 2f 0a 20 20 Reseek it. */.
6d90: 20 20 69 66 28 20 62 52 65 70 6c 61 63 65 20 7c if( bReplace |
6da0: 7c 20 63 68 6e 67 4b 65 79 20 29 7b 0a 20 20 20 | chngKey ){.
6db0: 20 20 20 69 66 28 20 70 50 6b 20 29 7b 0a 20 20 if( pPk ){.
6dc0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
6dd0: 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 eAddOp4Int(v, OP
6de0: 5f 4e 6f 74 46 6f 75 6e 64 2c 69 44 61 74 61 43 _NotFound,iDataC
6df0: 75 72 2c 6c 61 62 65 6c 43 6f 6e 74 69 6e 75 65 ur,labelContinue
6e00: 2c 72 65 67 4b 65 79 2c 6e 4b 65 79 29 3b 0a 20 ,regKey,nKey);.
6e10: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
6e20: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
6e30: 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 ddOp3(v, OP_NotE
6e40: 78 69 73 74 73 2c 20 69 44 61 74 61 43 75 72 2c xists, iDataCur,
6e50: 20 6c 61 62 65 6c 43 6f 6e 74 69 6e 75 65 2c 72 labelContinue,r
6e60: 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 egOldRowid);.
6e70: 20 20 20 7d 0a 20 20 20 20 20 20 56 64 62 65 43 }. VdbeC
6e80: 6f 76 65 72 61 67 65 4e 65 76 65 72 54 61 6b 65 overageNeverTake
6e90: 6e 28 76 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 n(v);. }..
6ea0: 20 2f 2a 20 44 6f 20 46 4b 20 63 6f 6e 73 74 72 /* Do FK constr
6eb0: 61 69 6e 74 20 63 68 65 63 6b 73 2e 20 2a 2f 0a aint checks. */.
6ec0: 20 20 20 20 69 66 28 20 68 61 73 46 4b 20 29 7b if( hasFK ){
6ed0: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 6b . sqlite3Fk
6ee0: 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 70 54 Check(pParse, pT
6ef0: 61 62 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c ab, regOldRowid,
6f00: 20 30 2c 20 61 58 52 65 66 2c 20 63 68 6e 67 4b 0, aXRef, chngK
6f10: 65 79 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ey);. }..
6f20: 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 69 6e /* Delete the in
6f30: 64 65 78 20 65 6e 74 72 69 65 73 20 61 73 73 6f dex entries asso
6f40: 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 ciated with the
6f50: 63 75 72 72 65 6e 74 20 72 65 63 6f 72 64 2e 20 current record.
6f60: 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 47 */. sqlite3G
6f70: 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 enerateRowIndexD
6f80: 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70 54 elete(pParse, pT
6f90: 61 62 2c 20 69 44 61 74 61 43 75 72 2c 20 69 49 ab, iDataCur, iI
6fa0: 64 78 43 75 72 2c 20 61 52 65 67 49 64 78 2c 20 dxCur, aRegIdx,
6fb0: 2d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 -1);.. /* If
6fc0: 63 68 61 6e 67 69 6e 67 20 74 68 65 20 72 6f 77 changing the row
6fd0: 69 64 20 76 61 6c 75 65 2c 20 6f 72 20 69 66 20 id value, or if
6fe0: 74 68 65 72 65 20 61 72 65 20 66 6f 72 65 69 67 there are foreig
6ff0: 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 n key constraint
7000: 73 0a 20 20 20 20 2a 2a 20 74 6f 20 70 72 6f 63 s. ** to proc
7010: 65 73 73 2c 20 64 65 6c 65 74 65 20 74 68 65 20 ess, delete the
7020: 6f 6c 64 20 72 65 63 6f 72 64 2e 20 4f 74 68 65 old record. Othe
7030: 72 77 69 73 65 2c 20 61 64 64 20 61 20 6e 6f 6f rwise, add a noo
7040: 70 20 4f 50 5f 44 65 6c 65 74 65 0a 20 20 20 20 p OP_Delete.
7050: 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65 ** to invoke the
7060: 20 70 72 65 2d 75 70 64 61 74 65 20 68 6f 6f 6b pre-update hook
7070: 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 .. **. **
7080: 54 68 61 74 20 28 72 65 67 4e 65 77 3d 3d 72 65 That (regNew==re
7090: 67 6e 65 77 52 6f 77 69 64 2b 31 29 20 69 73 20 gnewRowid+1) is
70a0: 74 72 75 65 20 69 73 20 61 6c 73 6f 20 69 6d 70 true is also imp
70b0: 6f 72 74 61 6e 74 20 66 6f 72 20 74 68 65 20 0a ortant for the .
70c0: 20 20 20 20 2a 2a 20 70 72 65 2d 75 70 64 61 74 ** pre-updat
70d0: 65 20 68 6f 6f 6b 2e 20 49 66 20 74 68 65 20 63 e hook. If the c
70e0: 61 6c 6c 65 72 20 69 6e 76 6f 6b 65 73 20 70 72 aller invokes pr
70f0: 65 75 70 64 61 74 65 5f 6e 65 77 28 29 2c 20 74 eupdate_new(), t
7100: 68 65 20 72 65 74 75 72 6e 65 64 0a 20 20 20 20 he returned.
7110: 2a 2a 20 76 61 6c 75 65 20 69 73 20 63 6f 70 69 ** value is copi
7120: 65 64 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 63 ed from memory c
7130: 65 6c 6c 20 28 72 65 67 4e 65 77 52 6f 77 69 64 ell (regNewRowid
7140: 2b 31 2b 69 43 6f 6c 29 2c 20 77 68 65 72 65 20 +1+iCol), where
7150: 69 43 6f 6c 0a 20 20 20 20 2a 2a 20 69 73 20 74 iCol. ** is t
7160: 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 he column index
7170: 73 75 70 70 6c 69 65 64 20 62 79 20 74 68 65 20 supplied by the
7180: 75 73 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 user.. */.
7190: 20 61 73 73 65 72 74 28 20 72 65 67 4e 65 77 3d assert( regNew=
71a0: 3d 72 65 67 4e 65 77 52 6f 77 69 64 2b 31 20 29 =regNewRowid+1 )
71b0: 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f ;.#ifdef SQLITE_
71c0: 45 4e 41 42 4c 45 5f 50 52 45 55 50 44 41 54 45 ENABLE_PREUPDATE
71d0: 5f 48 4f 4f 4b 0a 20 20 20 20 73 71 6c 69 74 65 _HOOK. sqlite
71e0: 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
71f0: 50 5f 44 65 6c 65 74 65 2c 20 69 44 61 74 61 43 P_Delete, iDataC
7200: 75 72 2c 0a 20 20 20 20 20 20 20 20 4f 50 46 4c ur,. OPFL
7210: 41 47 5f 49 53 55 50 44 41 54 45 20 7c 20 28 28 AG_ISUPDATE | ((
7220: 68 61 73 46 4b 3e 31 20 7c 7c 20 63 68 6e 67 4b hasFK>1 || chngK
7230: 65 79 29 20 3f 20 30 20 3a 20 4f 50 46 4c 41 47 ey) ? 0 : OPFLAG
7240: 5f 49 53 4e 4f 4f 50 29 2c 0a 20 20 20 20 20 20 _ISNOOP),.
7250: 20 20 72 65 67 4e 65 77 52 6f 77 69 64 0a 20 20 regNewRowid.
7260: 20 20 29 3b 0a 20 20 20 20 69 66 28 20 65 4f 6e );. if( eOn
7270: 65 50 61 73 73 3d 3d 4f 4e 45 50 41 53 53 5f 4d ePass==ONEPASS_M
7280: 55 4c 54 49 20 29 7b 0a 20 20 20 20 20 20 61 73 ULTI ){. as
7290: 73 65 72 74 28 20 68 61 73 46 4b 3d 3d 30 20 26 sert( hasFK==0 &
72a0: 26 20 63 68 6e 67 4b 65 79 3d 3d 30 20 29 3b 0a & chngKey==0 );.
72b0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
72c0: 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 eChangeP5(v, OPF
72d0: 4c 41 47 5f 53 41 56 45 50 4f 53 49 54 49 4f 4e LAG_SAVEPOSITION
72e0: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
72f0: 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 !pParse->nested
7300: 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
7310: 33 56 64 62 65 41 70 70 65 6e 64 50 34 28 76 2c 3VdbeAppendP4(v,
7320: 20 70 54 61 62 2c 20 50 34 5f 54 41 42 4c 45 29 pTab, P4_TABLE)
7330: 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 ;. }.#else.
7340: 20 20 69 66 28 20 68 61 73 46 4b 3e 31 20 7c 7c if( hasFK>1 ||
7350: 20 63 68 6e 67 4b 65 79 20 29 7b 0a 20 20 20 20 chngKey ){.
7360: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
7370: 4f 70 32 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 Op2(v, OP_Delete
7380: 2c 20 69 44 61 74 61 43 75 72 2c 20 30 29 3b 0a , iDataCur, 0);.
7390: 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 }.#endif..
73a0: 20 20 69 66 28 20 68 61 73 46 4b 20 29 7b 0a 20 if( hasFK ){.
73b0: 20 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68 sqlite3FkCh
73c0: 65 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62 eck(pParse, pTab
73d0: 2c 20 30 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 , 0, regNewRowid
73e0: 2c 20 61 58 52 65 66 2c 20 63 68 6e 67 4b 65 79 , aXRef, chngKey
73f0: 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 );. }. .
7400: 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20 6e 65 /* Insert the ne
7410: 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 w index entries
7420: 61 6e 64 20 74 68 65 20 6e 65 77 20 72 65 63 6f and the new reco
7430: 72 64 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 rd. */. sqlit
7440: 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 e3CompleteInsert
7450: 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 70 50 61 ion(. pPa
7460: 72 73 65 2c 20 70 54 61 62 2c 20 69 44 61 74 61 rse, pTab, iData
7470: 43 75 72 2c 20 69 49 64 78 43 75 72 2c 20 72 65 Cur, iIdxCur, re
7480: 67 4e 65 77 52 6f 77 69 64 2c 20 61 52 65 67 49 gNewRowid, aRegI
7490: 64 78 2c 20 0a 20 20 20 20 20 20 20 20 4f 50 46 dx, . OPF
74a0: 4c 41 47 5f 49 53 55 50 44 41 54 45 20 7c 20 28 LAG_ISUPDATE | (
74b0: 65 4f 6e 65 50 61 73 73 3d 3d 4f 4e 45 50 41 53 eOnePass==ONEPAS
74c0: 53 5f 4d 55 4c 54 49 20 3f 20 4f 50 46 4c 41 47 S_MULTI ? OPFLAG
74d0: 5f 53 41 56 45 50 4f 53 49 54 49 4f 4e 20 3a 20 _SAVEPOSITION :
74e0: 30 29 2c 20 0a 20 20 20 20 20 20 20 20 30 2c 20 0), . 0,
74f0: 30 0a 20 20 20 20 29 3b 0a 0a 20 20 20 20 2f 2a 0. );.. /*
7500: 20 44 6f 20 61 6e 79 20 4f 4e 20 43 41 53 43 41 Do any ON CASCA
7510: 44 45 2c 20 53 45 54 20 4e 55 4c 4c 20 6f 72 20 DE, SET NULL or
7520: 53 45 54 20 44 45 46 41 55 4c 54 20 6f 70 65 72 SET DEFAULT oper
7530: 61 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 ations required
7540: 74 6f 0a 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 to. ** handle
7550: 20 72 6f 77 73 20 28 70 6f 73 73 69 62 6c 79 20 rows (possibly
7560: 69 6e 20 6f 74 68 65 72 20 74 61 62 6c 65 73 29 in other tables)
7570: 20 74 68 61 74 20 72 65 66 65 72 20 76 69 61 20 that refer via
7580: 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 20 20 a foreign key.
7590: 20 20 2a 2a 20 74 6f 20 74 68 65 20 72 6f 77 20 ** to the row
75a0: 6a 75 73 74 20 75 70 64 61 74 65 64 2e 20 2a 2f just updated. */
75b0: 20 0a 20 20 20 20 69 66 28 20 68 61 73 46 4b 20 . if( hasFK
75c0: 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
75d0: 46 6b 41 63 74 69 6f 6e 73 28 70 50 61 72 73 65 FkActions(pParse
75e0: 2c 20 70 54 61 62 2c 20 70 43 68 61 6e 67 65 73 , pTab, pChanges
75f0: 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 61 , regOldRowid, a
7600: 58 52 65 66 2c 20 63 68 6e 67 4b 65 79 29 3b 0a XRef, chngKey);.
7610: 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
7620: 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f Increment the ro
7630: 77 20 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2f 0a w counter . */.
7640: 20 20 69 66 28 20 72 65 67 52 6f 77 43 6f 75 6e if( regRowCoun
7650: 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
7660: 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
7670: 5f 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43 _AddImm, regRowC
7680: 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 ount, 1);. }..
7690: 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 sqlite3CodeRowT
76a0: 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 rigger(pParse, p
76b0: 54 72 69 67 67 65 72 2c 20 54 4b 5f 55 50 44 41 Trigger, TK_UPDA
76c0: 54 45 2c 20 70 43 68 61 6e 67 65 73 2c 20 0a 20 TE, pChanges, .
76d0: 20 20 20 20 20 54 52 49 47 47 45 52 5f 41 46 54 TRIGGER_AFT
76e0: 45 52 2c 20 70 54 61 62 2c 20 72 65 67 4f 6c 64 ER, pTab, regOld
76f0: 52 6f 77 69 64 2c 20 6f 6e 45 72 72 6f 72 2c 20 Rowid, onError,
7700: 6c 61 62 65 6c 43 6f 6e 74 69 6e 75 65 29 3b 0a labelContinue);.
7710: 0a 20 20 2f 2a 20 52 65 70 65 61 74 20 74 68 65 . /* Repeat the
7720: 20 61 62 6f 76 65 20 77 69 74 68 20 74 68 65 20 above with the
7730: 6e 65 78 74 20 72 65 63 6f 72 64 20 74 6f 20 62 next record to b
7740: 65 20 75 70 64 61 74 65 64 2c 20 75 6e 74 69 6c e updated, until
7750: 0a 20 20 2a 2a 20 61 6c 6c 20 72 65 63 6f 72 64 . ** all record
7760: 20 73 65 6c 65 63 74 65 64 20 62 79 20 74 68 65 selected by the
7770: 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 68 61 WHERE clause ha
7780: 76 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 2e ve been updated.
7790: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 65 4f 6e 65 . */. if( eOne
77a0: 50 61 73 73 3d 3d 4f 4e 45 50 41 53 53 5f 53 49 Pass==ONEPASS_SI
77b0: 4e 47 4c 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4e NGLE ){. /* N
77c0: 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 61 74 20 othing to do at
77d0: 65 6e 64 2d 6f 66 2d 6c 6f 6f 70 20 66 6f 72 20 end-of-loop for
77e0: 61 20 73 69 6e 67 6c 65 2d 70 61 73 73 20 2a 2f a single-pass */
77f0: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 65 4f 6e . }else if( eOn
7800: 65 50 61 73 73 3d 3d 4f 4e 45 50 41 53 53 5f 4d ePass==ONEPASS_M
7810: 55 4c 54 49 20 29 7b 0a 20 20 20 20 73 71 6c 69 ULTI ){. sqli
7820: 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
7830: 62 65 6c 28 76 2c 20 6c 61 62 65 6c 43 6f 6e 74 bel(v, labelCont
7840: 69 6e 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 inue);. sqlit
7850: 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 e3WhereEnd(pWInf
7860: 6f 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 o);. }else if(
7870: 70 50 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 pPk ){. sqlit
7880: 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 e3VdbeResolveLab
7890: 65 6c 28 76 2c 20 6c 61 62 65 6c 43 6f 6e 74 69 el(v, labelConti
78a0: 6e 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 nue);. sqlite
78b0: 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
78c0: 50 5f 4e 65 78 74 2c 20 69 45 70 68 2c 20 61 64 P_Next, iEph, ad
78d0: 64 72 54 6f 70 29 3b 20 56 64 62 65 43 6f 76 65 drTop); VdbeCove
78e0: 72 61 67 65 28 76 29 3b 0a 20 20 7d 65 6c 73 65 rage(v);. }else
78f0: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
7900: 65 47 6f 74 6f 28 76 2c 20 6c 61 62 65 6c 43 6f eGoto(v, labelCo
7910: 6e 74 69 6e 75 65 29 3b 0a 20 20 7d 0a 20 20 73 ntinue);. }. s
7920: 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 qlite3VdbeResolv
7930: 65 4c 61 62 65 6c 28 76 2c 20 6c 61 62 65 6c 42 eLabel(v, labelB
7940: 72 65 61 6b 29 3b 0a 0a 20 20 2f 2a 20 55 70 64 reak);.. /* Upd
7950: 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 ate the sqlite_s
7960: 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 62 79 equence table by
7970: 20 73 74 6f 72 69 6e 67 20 74 68 65 20 63 6f 6e storing the con
7980: 74 65 6e 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a tent of the. **
7990: 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 63 maximum rowid c
79a0: 6f 75 6e 74 65 72 20 76 61 6c 75 65 73 20 72 65 ounter values re
79b0: 63 6f 72 64 65 64 20 77 68 69 6c 65 20 69 6e 73 corded while ins
79c0: 65 72 74 69 6e 67 20 69 6e 74 6f 0a 20 20 2a 2a erting into. **
79d0: 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74 autoincrement t
79e0: 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 ables.. */. if
79f0: 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 ( pParse->nested
7a00: 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 70 ==0 && pParse->p
7a10: 54 72 69 67 67 65 72 54 61 62 3d 3d 30 20 26 26 TriggerTab==0 &&
7a20: 20 70 55 70 73 65 72 74 3d 3d 30 20 29 7b 0a 20 pUpsert==0 ){.
7a30: 20 20 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e sqlite3Autoin
7a40: 63 72 65 6d 65 6e 74 45 6e 64 28 70 50 61 72 73 crementEnd(pPars
7a50: 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 e);. }.. /*.
7a60: 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 ** Return the nu
7a70: 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 mber of rows tha
7a80: 74 20 77 65 72 65 20 63 68 61 6e 67 65 64 2c 20 t were changed,
7a90: 69 66 20 77 65 20 61 72 65 20 74 72 61 63 6b 69 if we are tracki
7aa0: 6e 67 0a 20 20 2a 2a 20 74 68 61 74 20 69 6e 66 ng. ** that inf
7ab0: 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 ormation.. */.
7ac0: 20 69 66 28 20 72 65 67 52 6f 77 43 6f 75 6e 74 if( regRowCount
7ad0: 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 ){. sqlite3V
7ae0: 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
7af0: 52 65 73 75 6c 74 52 6f 77 2c 20 72 65 67 52 6f ResultRow, regRo
7b00: 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 20 20 wCount, 1);.
7b10: 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 sqlite3VdbeSetNu
7b20: 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 mCols(v, 1);.
7b30: 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 sqlite3VdbeSetC
7b40: 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c olName(v, 0, COL
7b50: 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 NAME_NAME, "rows
7b60: 20 75 70 64 61 74 65 64 22 2c 20 53 51 4c 49 54 updated", SQLIT
7b70: 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a E_STATIC);. }..
7b80: 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3a 0a update_cleanup:.
7b90: 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e sqlite3AuthCon
7ba0: 74 65 78 74 50 6f 70 28 26 73 43 6f 6e 74 65 78 textPop(&sContex
7bb0: 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 t);. sqlite3DbF
7bc0: 72 65 65 28 64 62 2c 20 61 58 52 65 66 29 3b 20 ree(db, aXRef);
7bd0: 2f 2a 20 41 6c 73 6f 20 66 72 65 65 73 20 61 52 /* Also frees aR
7be0: 65 67 49 64 78 5b 5d 20 61 6e 64 20 61 54 6f 4f egIdx[] and aToO
7bf0: 70 65 6e 5b 5d 20 2a 2f 0a 20 20 73 71 6c 69 74 pen[] */. sqlit
7c00: 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 e3SrcListDelete(
7c10: 64 62 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 db, pTabList);.
7c20: 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
7c30: 44 65 6c 65 74 65 28 64 62 2c 20 70 43 68 61 6e Delete(db, pChan
7c40: 67 65 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 ges);. sqlite3E
7c50: 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57 xprDelete(db, pW
7c60: 68 65 72 65 29 3b 0a 23 69 66 20 64 65 66 69 6e here);.#if defin
7c70: 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ed(SQLITE_ENABLE
7c80: 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c _UPDATE_DELETE_L
7c90: 49 4d 49 54 29 20 0a 20 20 73 71 6c 69 74 65 33 IMIT) . sqlite3
7ca0: 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 ExprListDelete(d
7cb0: 62 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 b, pOrderBy);.
7cc0: 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
7cd0: 65 28 64 62 2c 20 70 4c 69 6d 69 74 29 3b 0a 23 e(db, pLimit);.#
7ce0: 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 3b 0a endif. return;.
7cf0: 7d 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22 }./* Make sure "
7d00: 69 73 56 69 65 77 22 20 61 6e 64 20 6f 74 68 65 isView" and othe
7d10: 72 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 r macros defined
7d20: 20 61 62 6f 76 65 20 61 72 65 20 75 6e 64 65 66 above are undef
7d30: 69 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 0a ined. Otherwise.
7d40: 2a 2a 20 74 68 65 79 20 6d 61 79 20 69 6e 74 65 ** they may inte
7d50: 72 66 65 72 65 20 77 69 74 68 20 63 6f 6d 70 69 rfere with compi
7d60: 6c 61 74 69 6f 6e 20 6f 66 20 6f 74 68 65 72 20 lation of other
7d70: 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 69 functions in thi
7d80: 73 20 66 69 6c 65 0a 2a 2a 20 28 6f 72 20 69 6e s file.** (or in
7d90: 20 61 6e 6f 74 68 65 72 20 66 69 6c 65 2c 20 69 another file, i
7da0: 66 20 74 68 69 73 20 66 69 6c 65 20 62 65 63 6f f this file beco
7db0: 6d 65 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 mes part of the
7dc0: 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 29 2e 20 20 amalgamation).
7dd0: 2a 2f 0a 23 69 66 64 65 66 20 69 73 56 69 65 77 */.#ifdef isView
7de0: 0a 20 23 75 6e 64 65 66 20 69 73 56 69 65 77 0a . #undef isView.
7df0: 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 70 54 #endif.#ifdef pT
7e00: 72 69 67 67 65 72 0a 20 23 75 6e 64 65 66 20 70 rigger. #undef p
7e10: 54 72 69 67 67 65 72 0a 23 65 6e 64 69 66 0a 0a Trigger.#endif..
7e20: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
7e30: 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
7e40: 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 ./*.** Generate
7e50: 63 6f 64 65 20 66 6f 72 20 61 6e 20 55 50 44 41 code for an UPDA
7e60: 54 45 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 TE of a virtual
7e70: 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 table..**.** The
7e80: 72 65 20 61 72 65 20 74 77 6f 20 70 6f 73 73 69 re are two possi
7e90: 62 6c 65 20 73 74 72 61 74 65 67 69 65 73 20 2d ble strategies -
7ea0: 20 74 68 65 20 64 65 66 61 75 6c 74 20 61 6e 64 the default and
7eb0: 20 74 68 65 20 73 70 65 63 69 61 6c 20 0a 2a 2a the special .**
7ec0: 20 22 6f 6e 65 70 61 73 73 22 20 73 74 72 61 74 "onepass" strat
7ed0: 65 67 79 2e 20 4f 6e 65 70 61 73 73 20 69 73 20 egy. Onepass is
7ee0: 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 only used if the
7ef0: 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a virtual table .
7f00: 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f ** implementatio
7f10: 6e 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 n indicates that
7f20: 20 70 57 68 65 72 65 20 6d 61 79 20 6d 61 74 63 pWhere may matc
7f30: 68 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 72 6f h at most one ro
7f40: 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 w..**.** The def
7f50: 61 75 6c 74 20 73 74 72 61 74 65 67 79 20 69 73 ault strategy is
7f60: 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 65 70 to create an ep
7f70: 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 74 68 hemeral table th
7f80: 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 66 at contains.** f
7f90: 6f 72 20 65 61 63 68 20 72 6f 77 20 74 6f 20 62 or each row to b
7fa0: 65 20 63 68 61 6e 67 65 64 3a 0a 2a 2a 0a 2a 2a e changed:.**.**
7fb0: 20 20 20 28 41 29 20 20 54 68 65 20 6f 72 69 67 (A) The orig
7fc0: 69 6e 61 6c 20 72 6f 77 69 64 20 6f 66 20 74 68 inal rowid of th
7fd0: 61 74 20 72 6f 77 2e 0a 2a 2a 20 20 20 28 42 29 at row..** (B)
7fe0: 20 20 54 68 65 20 72 65 76 69 73 65 64 20 72 6f The revised ro
7ff0: 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77 2e wid for the row.
8000: 0a 2a 2a 20 20 20 28 43 29 20 20 54 68 65 20 63 .** (C) The c
8010: 6f 6e 74 65 6e 74 20 6f 66 20 65 76 65 72 79 20 ontent of every
8020: 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 6f column in the ro
8030: 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 6c 6f w..**.** Then lo
8040: 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 63 op through the c
8050: 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 69 73 20 ontents of this
8060: 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 ephemeral table
8070: 65 78 65 63 75 74 69 6e 67 20 61 0a 2a 2a 20 56 executing a.** V
8080: 55 70 64 61 74 65 20 66 6f 72 20 65 61 63 68 20 Update for each
8090: 72 6f 77 2e 20 57 68 65 6e 20 66 69 6e 69 73 68 row. When finish
80a0: 65 64 2c 20 64 72 6f 70 20 74 68 65 20 65 70 68 ed, drop the eph
80b0: 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a emeral table..**
80c0: 0a 2a 2a 20 54 68 65 20 22 6f 6e 65 70 61 73 73 .** The "onepass
80d0: 22 20 73 74 72 61 74 65 67 79 20 64 6f 65 73 20 " strategy does
80e0: 6e 6f 74 20 75 73 65 20 61 6e 20 65 70 68 65 6d not use an ephem
80f0: 65 72 61 6c 20 74 61 62 6c 65 2e 20 49 6e 73 74 eral table. Inst
8100: 65 61 64 2c 20 69 74 0a 2a 2a 20 73 74 6f 72 65 ead, it.** store
8110: 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 s the same value
8120: 73 20 28 41 2c 20 42 20 61 6e 64 20 43 20 61 62 s (A, B and C ab
8130: 6f 76 65 29 20 69 6e 20 61 20 72 65 67 69 73 74 ove) in a regist
8140: 65 72 20 61 72 72 61 79 20 61 6e 64 0a 2a 2a 20 er array and.**
8150: 6d 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 69 makes a single i
8160: 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 56 55 70 nvocation of VUp
8170: 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 date..*/.static
8180: 76 6f 69 64 20 75 70 64 61 74 65 56 69 72 74 75 void updateVirtu
8190: 61 6c 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 alTable(. Parse
81a0: 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
81b0: 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 /* The parsing c
81c0: 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c ontext */. SrcL
81d0: 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 ist *pSrc,
81e0: 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 /* The virtual
81f0: 74 61 62 6c 65 20 74 6f 20 62 65 20 6d 6f 64 69 table to be modi
8200: 66 69 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20 fied */. Table
8210: 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f *pTab, /
8220: 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 74 61 * The virtual ta
8230: 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ble */. ExprLis
8240: 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a t *pChanges, /*
8250: 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 The columns to
8260: 63 68 61 6e 67 65 20 69 6e 20 74 68 65 20 55 50 change in the UP
8270: 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a DATE statement *
8280: 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64 /. Expr *pRowid
8290: 2c 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 , /* Expr
82a0: 65 73 73 69 6f 6e 20 75 73 65 64 20 74 6f 20 72 ession used to r
82b0: 65 63 6f 6d 70 75 74 65 20 74 68 65 20 72 6f 77 ecompute the row
82c0: 69 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52 id */. int *aXR
82d0: 65 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ef, /*
82e0: 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 6f 6c Mapping from col
82f0: 75 6d 6e 73 20 6f 66 20 70 54 61 62 20 74 6f 20 umns of pTab to
8300: 65 6e 74 72 69 65 73 20 69 6e 20 70 43 68 61 6e entries in pChan
8310: 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 ges */. Expr *p
8320: 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a Where, /*
8330: 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 WHERE clause of
8340: 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74 the UPDATE stat
8350: 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6f ement */. int o
8360: 6e 45 72 72 6f 72 20 20 20 20 20 20 20 20 20 20 nError
8370: 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 73 /* ON CONFLICT s
8380: 74 72 61 74 65 67 79 20 2a 2f 0a 29 7b 0a 20 20 trategy */.){.
8390: 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 Vdbe *v = pParse
83a0: 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 56 69 72 ->pVdbe; /* Vir
83b0: 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 tual machine und
83c0: 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 er construction
83d0: 2a 2f 0a 20 20 69 6e 74 20 65 70 68 65 6d 54 61 */. int ephemTa
83e0: 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f b; /
83f0: 2a 20 54 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20 * Table holding
8400: 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 the result of th
8410: 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e e SELECT */. in
8420: 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 t i;
8430: 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
8440: 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c counter */. sql
8450: 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
8460: 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 e->db; /* Databa
8470: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
8480: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 . const char *p
8490: 56 54 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 VTab = (const ch
84a0: 61 72 2a 29 73 71 6c 69 74 65 33 47 65 74 56 54 ar*)sqlite3GetVT
84b0: 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 3b 0a able(db, pTab);.
84c0: 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 WhereInfo *pWI
84d0: 6e 66 6f 3b 0a 20 20 69 6e 74 20 6e 41 72 67 20 nfo;. int nArg
84e0: 3d 20 32 20 2b 20 70 54 61 62 2d 3e 6e 43 6f 6c = 2 + pTab->nCol
84f0: 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 ; /* Number
8500: 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f of arguments to
8510: 20 56 55 70 64 61 74 65 20 2a 2f 0a 20 20 69 6e VUpdate */. in
8520: 74 20 72 65 67 41 72 67 3b 20 20 20 20 20 20 20 t regArg;
8530: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
8540: 20 46 69 72 73 74 20 72 65 67 69 73 74 65 72 20 First register
8550: 69 6e 20 56 55 70 64 61 74 65 20 61 72 67 20 61 in VUpdate arg a
8560: 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 72 65 rray */. int re
8570: 67 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 gRec;
8580: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 /* Reg
8590: 69 73 74 65 72 20 69 6e 20 77 68 69 63 68 20 74 ister in which t
85a0: 6f 20 61 73 73 65 6d 62 6c 65 20 72 65 63 6f 72 o assemble recor
85b0: 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f d */. int regRo
85c0: 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 wid;
85d0: 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 /* Regist
85e0: 65 72 20 66 6f 72 20 65 70 68 65 6d 20 74 61 62 er for ephem tab
85f0: 6c 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e le rowid */. in
8600: 74 20 69 43 73 72 20 3d 20 70 53 72 63 2d 3e 61 t iCsr = pSrc->a
8610: 5b 30 5d 2e 69 43 75 72 73 6f 72 3b 20 20 2f 2a [0].iCursor; /*
8620: 20 43 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72 Cursor used for
8630: 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 virtual table s
8640: 63 61 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 44 75 can */. int aDu
8650: 6d 6d 79 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 mmy[2];
8660: 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 75 73 /* Unus
8670: 65 64 20 61 72 67 20 66 6f 72 20 73 71 6c 69 74 ed arg for sqlit
8680: 65 33 57 68 65 72 65 4f 6b 4f 6e 65 50 61 73 73 e3WhereOkOnePass
8690: 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65 4f 6e 65 () */. int eOne
86a0: 50 61 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 Pass;
86b0: 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 /* True
86c0: 74 6f 20 75 73 65 20 6f 6e 65 70 61 73 73 20 73 to use onepass s
86d0: 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 69 6e 74 trategy */. int
86e0: 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 20 20 addr;
86f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
8700: 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 Address of OP_Op
8710: 65 6e 45 70 68 65 6d 65 72 61 6c 20 2a 2f 0a 0a enEphemeral */..
8720: 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 41 /* Allocate nA
8730: 72 67 20 72 65 67 69 73 74 65 72 73 20 69 6e 20 rg registers in
8740: 77 68 69 63 68 20 74 6f 20 67 61 74 68 65 72 20 which to gather
8750: 74 68 65 20 61 72 67 75 6d 65 6e 74 73 20 66 6f the arguments fo
8760: 72 20 56 55 70 64 61 74 65 2e 20 54 68 65 6e 0a r VUpdate. Then.
8770: 20 20 2a 2a 20 63 72 65 61 74 65 20 61 6e 64 20 ** create and
8780: 6f 70 65 6e 20 74 68 65 20 65 70 68 65 6d 65 72 open the ephemer
8790: 61 6c 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 al table in whic
87a0: 68 20 74 68 65 20 72 65 63 6f 72 64 73 20 63 72 h the records cr
87b0: 65 61 74 65 64 20 66 72 6f 6d 0a 20 20 2a 2a 20 eated from. **
87c0: 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 20 these arguments
87d0: 77 69 6c 6c 20 62 65 20 74 65 6d 70 6f 72 61 72 will be temporar
87e0: 69 6c 79 20 73 74 6f 72 65 64 2e 20 2a 2f 0a 20 ily stored. */.
87f0: 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20 assert( v );.
8800: 65 70 68 65 6d 54 61 62 20 3d 20 70 50 61 72 73 ephemTab = pPars
8810: 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 61 64 64 e->nTab++;. add
8820: 72 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 r= sqlite3VdbeAd
8830: 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 dOp2(v, OP_OpenE
8840: 70 68 65 6d 65 72 61 6c 2c 20 65 70 68 65 6d 54 phemeral, ephemT
8850: 61 62 2c 20 6e 41 72 67 29 3b 0a 20 20 72 65 67 ab, nArg);. reg
8860: 41 72 67 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d Arg = pParse->nM
8870: 65 6d 20 2b 20 31 3b 0a 20 20 70 50 61 72 73 65 em + 1;. pParse
8880: 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 41 72 67 3b 0a ->nMem += nArg;.
8890: 20 20 72 65 67 52 65 63 20 3d 20 2b 2b 70 50 61 regRec = ++pPa
88a0: 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 rse->nMem;. reg
88b0: 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 Rowid = ++pParse
88c0: 2d 3e 6e 4d 65 6d 3b 0a 0a 20 20 2f 2a 20 53 74 ->nMem;.. /* St
88d0: 61 72 74 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 art scanning the
88e0: 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a virtual table *
88f0: 2f 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c /. pWInfo = sql
8900: 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 ite3WhereBegin(p
8910: 50 61 72 73 65 2c 20 70 53 72 63 2c 20 70 57 68 Parse, pSrc, pWh
8920: 65 72 65 2c 20 30 2c 30 2c 57 48 45 52 45 5f 4f ere, 0,0,WHERE_O
8930: 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 2c 30 NEPASS_DESIRED,0
8940: 29 3b 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 3d );. if( pWInfo=
8950: 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 =0 ) return;..
8960: 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 /* Populate the
8970: 61 72 67 75 6d 65 6e 74 20 72 65 67 69 73 74 65 argument registe
8980: 72 73 2e 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 rs. */. for(i=0
8990: 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 ; i<pTab->nCol;
89a0: 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 58 i++){. if( aX
89b0: 52 65 66 5b 69 5d 3e 3d 30 20 29 7b 0a 20 20 20 Ref[i]>=0 ){.
89c0: 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f sqlite3ExprCo
89d0: 64 65 28 70 50 61 72 73 65 2c 20 70 43 68 61 6e de(pParse, pChan
89e0: 67 65 73 2d 3e 61 5b 61 58 52 65 66 5b 69 5d 5d ges->a[aXRef[i]]
89f0: 2e 70 45 78 70 72 2c 20 72 65 67 41 72 67 2b 32 .pExpr, regArg+2
8a00: 2b 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a +i);. }else{.
8a10: 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
8a20: 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 56 43 eAddOp3(v, OP_VC
8a30: 6f 6c 75 6d 6e 2c 20 69 43 73 72 2c 20 69 2c 20 olumn, iCsr, i,
8a40: 72 65 67 41 72 67 2b 32 2b 69 29 3b 0a 20 20 20 regArg+2+i);.
8a50: 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
8a60: 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 angeP5(v, OPFLAG
8a70: 5f 4e 4f 43 48 4e 47 29 3b 2f 2a 20 45 6e 61 62 _NOCHNG);/* Enab
8a80: 6c 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f le sqlite3_vtab_
8a90: 6e 6f 63 68 61 6e 67 65 28 29 20 2a 2f 0a 20 20 nochange() */.
8aa0: 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 48 61 }. }. if( Ha
8ab0: 73 52 6f 77 69 64 28 70 54 61 62 29 20 29 7b 0a sRowid(pTab) ){.
8ac0: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
8ad0: 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 ddOp2(v, OP_Rowi
8ae0: 64 2c 20 69 43 73 72 2c 20 72 65 67 41 72 67 29 d, iCsr, regArg)
8af0: 3b 0a 20 20 20 20 69 66 28 20 70 52 6f 77 69 64 ;. if( pRowid
8b00: 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
8b10: 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 3ExprCode(pParse
8b20: 2c 20 70 52 6f 77 69 64 2c 20 72 65 67 41 72 67 , pRowid, regArg
8b30: 2b 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a +1);. }else{.
8b40: 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
8b50: 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f eAddOp2(v, OP_Ro
8b60: 77 69 64 2c 20 69 43 73 72 2c 20 72 65 67 41 72 wid, iCsr, regAr
8b70: 67 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 g+1);. }. }e
8b80: 6c 73 65 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a lse{. Index *
8b90: 70 50 6b 3b 20 20 20 2f 2a 20 50 52 49 4d 41 52 pPk; /* PRIMAR
8ba0: 59 20 4b 45 59 20 69 6e 64 65 78 20 2a 2f 0a 20 Y KEY index */.
8bb0: 20 20 20 69 31 36 20 69 50 6b 3b 20 20 20 20 20 i16 iPk;
8bc0: 20 2f 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 /* PRIMARY KEY
8bd0: 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 70 50 column */. pP
8be0: 6b 20 3d 20 73 71 6c 69 74 65 33 50 72 69 6d 61 k = sqlite3Prima
8bf0: 72 79 4b 65 79 49 6e 64 65 78 28 70 54 61 62 29 ryKeyIndex(pTab)
8c00: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 ;. assert( pP
8c10: 6b 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 k!=0 );. asse
8c20: 72 74 28 20 70 50 6b 2d 3e 6e 4b 65 79 43 6f 6c rt( pPk->nKeyCol
8c30: 3d 3d 31 20 29 3b 0a 20 20 20 20 69 50 6b 20 3d ==1 );. iPk =
8c40: 20 70 50 6b 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 pPk->aiColumn[0
8c50: 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ];. sqlite3Vd
8c60: 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 56 beAddOp3(v, OP_V
8c70: 43 6f 6c 75 6d 6e 2c 20 69 43 73 72 2c 20 69 50 Column, iCsr, iP
8c80: 6b 2c 20 72 65 67 41 72 67 29 3b 0a 20 20 20 20 k, regArg);.
8c90: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
8ca0: 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 2(v, OP_SCopy, r
8cb0: 65 67 41 72 67 2b 32 2b 69 50 6b 2c 20 72 65 67 egArg+2+iPk, reg
8cc0: 41 72 67 2b 31 29 3b 0a 20 20 7d 0a 0a 20 20 65 Arg+1);. }.. e
8cd0: 4f 6e 65 50 61 73 73 20 3d 20 73 71 6c 69 74 65 OnePass = sqlite
8ce0: 33 57 68 65 72 65 4f 6b 4f 6e 65 50 61 73 73 28 3WhereOkOnePass(
8cf0: 70 57 49 6e 66 6f 2c 20 61 44 75 6d 6d 79 29 3b pWInfo, aDummy);
8d00: 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 .. /* There is
8d10: 6e 6f 20 4f 4e 45 50 41 53 53 5f 4d 55 4c 54 49 no ONEPASS_MULTI
8d20: 20 6f 6e 20 76 69 72 74 75 61 6c 20 74 61 62 6c on virtual tabl
8d30: 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 es */. assert(
8d40: 65 4f 6e 65 50 61 73 73 3d 3d 4f 4e 45 50 41 53 eOnePass==ONEPAS
8d50: 53 5f 4f 46 46 20 7c 7c 20 65 4f 6e 65 50 61 73 S_OFF || eOnePas
8d60: 73 3d 3d 4f 4e 45 50 41 53 53 5f 53 49 4e 47 4c s==ONEPASS_SINGL
8d70: 45 20 29 3b 0a 0a 20 20 69 66 28 20 65 4f 6e 65 E );.. if( eOne
8d80: 50 61 73 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 Pass ){. /* I
8d90: 66 20 75 73 69 6e 67 20 74 68 65 20 6f 6e 65 70 f using the onep
8da0: 61 73 73 20 73 74 72 61 74 65 67 79 2c 20 6e 6f ass strategy, no
8db0: 2d 6f 70 20 6f 75 74 20 74 68 65 20 4f 50 5f 4f -op out the OP_O
8dc0: 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 63 6f 64 penEphemeral cod
8dd0: 65 64 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 2e ed. ** above.
8de0: 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 */. sqlite3V
8df0: 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 dbeChangeToNoop(
8e00: 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 73 71 v, addr);. sq
8e10: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
8e20: 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 73 v, OP_Close, iCs
8e30: 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 r);. }else{.
8e40: 20 2f 2a 20 43 72 65 61 74 65 20 61 20 72 65 63 /* Create a rec
8e50: 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 61 72 67 ord from the arg
8e60: 75 6d 65 6e 74 20 72 65 67 69 73 74 65 72 20 63 ument register c
8e70: 6f 6e 74 65 6e 74 73 20 61 6e 64 20 69 6e 73 65 ontents and inse
8e80: 72 74 20 69 74 20 69 6e 74 6f 0a 20 20 20 20 2a rt it into. *
8e90: 2a 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 * the ephemeral
8ea0: 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 73 71 table. */. sq
8eb0: 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28 lite3MultiWrite(
8ec0: 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71 6c pParse);. sql
8ed0: 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
8ee0: 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c , OP_MakeRecord,
8ef0: 20 72 65 67 41 72 67 2c 20 6e 41 72 67 2c 20 72 regArg, nArg, r
8f00: 65 67 52 65 63 29 3b 0a 23 69 66 64 65 66 20 53 egRec);.#ifdef S
8f10: 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 QLITE_DEBUG.
8f20: 2f 2a 20 53 69 67 6e 61 6c 20 61 6e 20 61 73 73 /* Signal an ass
8f30: 65 72 74 28 29 20 77 69 74 68 69 6e 20 4f 50 5f ert() within OP_
8f40: 4d 61 6b 65 52 65 63 6f 72 64 20 74 68 61 74 20 MakeRecord that
8f50: 69 74 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f it is allowed to
8f60: 0a 20 20 20 20 2a 2a 20 61 63 63 65 70 74 20 6e . ** accept n
8f70: 6f 2d 63 68 61 6e 67 65 20 72 65 63 6f 72 64 73 o-change records
8f80: 20 77 69 74 68 20 73 65 72 69 61 6c 5f 74 79 70 with serial_typ
8f90: 65 20 31 30 20 2a 2f 0a 20 20 20 20 73 71 6c 69 e 10 */. sqli
8fa0: 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 te3VdbeChangeP5(
8fb0: 76 2c 20 4f 50 46 4c 41 47 5f 4e 4f 43 48 4e 47 v, OPFLAG_NOCHNG
8fc0: 5f 4d 41 47 49 43 29 3b 0a 23 65 6e 64 69 66 0a _MAGIC);.#endif.
8fd0: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
8fe0: 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 ddOp2(v, OP_NewR
8ff0: 6f 77 69 64 2c 20 65 70 68 65 6d 54 61 62 2c 20 owid, ephemTab,
9000: 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 regRowid);. s
9010: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
9020: 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 65 (v, OP_Insert, e
9030: 70 68 65 6d 54 61 62 2c 20 72 65 67 52 65 63 2c phemTab, regRec,
9040: 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 0a regRowid);. }.
9050: 0a 0a 20 20 69 66 28 20 65 4f 6e 65 50 61 73 73 .. if( eOnePass
9060: 3d 3d 4f 4e 45 50 41 53 53 5f 4f 46 46 20 29 7b ==ONEPASS_OFF ){
9070: 0a 20 20 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 . /* End the
9080: 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 63 virtual table sc
9090: 61 6e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 an */. sqlite
90a0: 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 3WhereEnd(pWInfo
90b0: 29 3b 0a 0a 20 20 20 20 2f 2a 20 42 65 67 69 6e );.. /* Begin
90c0: 20 73 63 61 6e 6e 6e 69 6e 67 20 74 68 72 6f 75 scannning throu
90d0: 67 68 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c gh the ephemeral
90e0: 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 61 table. */. a
90f0: 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 ddr = sqlite3Vdb
9100: 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 eAddOp1(v, OP_Re
9110: 77 69 6e 64 2c 20 65 70 68 65 6d 54 61 62 29 3b wind, ephemTab);
9120: 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 VdbeCoverage(v)
9130: 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63 ;.. /* Extrac
9140: 74 20 61 72 67 75 6d 65 6e 74 73 20 66 72 6f 6d t arguments from
9150: 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 the current row
9160: 20 6f 66 20 74 68 65 20 65 70 68 65 6d 65 72 61 of the ephemera
9170: 6c 20 74 61 62 6c 65 20 61 6e 64 20 0a 20 20 20 l table and .
9180: 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 56 ** invoke the V
9190: 55 70 64 61 74 65 20 6d 65 74 68 6f 64 2e 20 20 Update method.
91a0: 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 */. for(i=0;
91b0: 69 3c 6e 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 i<nArg; i++){.
91c0: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
91d0: 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 ddOp3(v, OP_Colu
91e0: 6d 6e 2c 20 65 70 68 65 6d 54 61 62 2c 20 69 2c mn, ephemTab, i,
91f0: 20 72 65 67 41 72 67 2b 69 29 3b 0a 20 20 20 20 regArg+i);.
9200: 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 }. }. sqlite3V
9210: 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 tabMakeWritable(
9220: 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 pParse, pTab);.
9230: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
9240: 70 34 28 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 p4(v, OP_VUpdate
9250: 2c 20 30 2c 20 6e 41 72 67 2c 20 72 65 67 41 72 , 0, nArg, regAr
9260: 67 2c 20 70 56 54 61 62 2c 20 50 34 5f 56 54 41 g, pVTab, P4_VTA
9270: 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 B);. sqlite3Vdb
9280: 65 43 68 61 6e 67 65 50 35 28 76 2c 20 6f 6e 45 eChangeP5(v, onE
9290: 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 rror==OE_Default
92a0: 20 3f 20 4f 45 5f 41 62 6f 72 74 20 3a 20 6f 6e ? OE_Abort : on
92b0: 45 72 72 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 Error);. sqlite
92c0: 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 3MayAbort(pParse
92d0: 29 3b 0a 0a 20 20 2f 2a 20 45 6e 64 20 6f 66 20 );.. /* End of
92e0: 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 the ephemeral ta
92f0: 62 6c 65 20 73 63 61 6e 2e 20 4f 72 2c 20 69 66 ble scan. Or, if
9300: 20 75 73 69 6e 67 20 74 68 65 20 6f 6e 65 70 61 using the onepa
9310: 73 73 20 73 74 72 61 74 65 67 79 2c 0a 20 20 2a ss strategy,. *
9320: 2a 20 6a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 * jump to here i
9330: 66 20 74 68 65 20 73 63 61 6e 20 76 69 73 69 74 f the scan visit
9340: 65 64 20 7a 65 72 6f 20 72 6f 77 73 2e 20 2a 2f ed zero rows. */
9350: 0a 20 20 69 66 28 20 65 4f 6e 65 50 61 73 73 3d . if( eOnePass=
9360: 3d 4f 4e 45 50 41 53 53 5f 4f 46 46 20 29 7b 0a =ONEPASS_OFF ){.
9370: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
9380: 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 ddOp2(v, OP_Next
9390: 2c 20 65 70 68 65 6d 54 61 62 2c 20 61 64 64 72 , ephemTab, addr
93a0: 2b 31 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 +1); VdbeCoverag
93b0: 65 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 e(v);. sqlite
93c0: 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
93d0: 20 61 64 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 addr);. sqli
93e0: 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
93f0: 20 4f 50 5f 43 6c 6f 73 65 2c 20 65 70 68 65 6d OP_Close, ephem
9400: 54 61 62 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 Tab, 0);. }else
9410: 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 {. sqlite3Whe
9420: 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 reEnd(pWInfo);.
9430: 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 }.}.#endif /* S
9440: 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 QLITE_OMIT_VIRTU
9450: 41 4c 54 41 42 4c 45 20 2a 2f 0a ALTABLE */.