⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vdbe.html

📁 这是sqlite3.56的文档。拿来给大家阅读使用
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<blockquote><tt>5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rewind&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;12</tt></blockquote><p>As in the SELECT example, the <a href="opcode.html#Rewind">Rewind</a> instruction rewinds the cursor to the beginning of the table, readying it for use in the loop body.</p><blockquote><tt>6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Column&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1<br>7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;50<br>8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ge&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;11</tt></blockquote><p>The WHERE clause is implemented by instructions 6 through 8.The job of the where clause is to skip the ListWrite if the WHEREcondition is false.  To this end, it jumps ahead to the Next instructionif the "two" column (extracted by the Column instruction) isgreater than or equal to 50.</p><p>As before, the Column instruction uses cursor P1 and pushes the data record in column P2 (1, column "two") onto the stack.  The Integer instruction pushes the value 50 onto the top of the stack.  After these two instructions the stack looks like:</p><blockquote><table border=2><tr><td align=left>(integer) 50</td></tr><tr><td align=left>(record) current record for column "two" </td></tr></table></blockquote><p>The <a href="opcode.html#Ge">Ge</a> operator compares the top two elements on the stack, pops them, and then branches based on the result of the comparison.  If the second element is >= the top element, then jump to address P2 (the Next instruction at the end of the loop).  Because P1 is true, if either operand is NULL (and thus the result is NULL) then take the jump.  If we don't jump, just advance to the next instruction.</p><blockquote><tt>9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Recno&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0<br>10&nbsp;&nbsp;&nbsp;&nbsp;ListWrite&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</tt></blockquote><p>The <a href="opcode.html#Recno">Recno</a> instruction pushes onto the stack an integer which is the first 4 bytes of the the key to the current entry in a sequential scan of the table pointed to by cursor P1.The <a href="opcode.html#ListWrite">ListWrite</a> instruction writes the integer on the top of the stack into a temporary storage list and pops the top element.  This is the important work of this loop, to store the keys of the records to be deleted so we can delete them in the second loop.  After this ListWrite instruction the stack is empty again.</p><blockquote><tt>11&nbsp;&nbsp;&nbsp;&nbsp;Next&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6<br>12&nbsp;&nbsp;&nbsp;&nbsp;Close&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</tt></blockquote><p> The Next instruction increments the cursor to point to the next element in the table pointed to by cursor P0, and if it was successful branches to P2 (6, the beginning of the loop body).  The Close instruction closes cursor P1.  It doesn't affect the temporary storage list because it isn't associated with cursor P1; it is instead a global working list (which can be saved with ListPush).</p><blockquote><tt>13&nbsp;&nbsp;&nbsp;&nbsp;ListRewind&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</tt></blockquote><p> The <a href="opcode.html#ListRewind">ListRewind</a> instruction rewinds the temporary storage list to the beginning.  This prepares it for use in the second loop.</p><blockquote><tt>14&nbsp;&nbsp;&nbsp;&nbsp;Integer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0<br>15&nbsp;&nbsp;&nbsp;&nbsp;OpenWrite&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3</tt></blockquote><p> As in the INSERT example, we push the database number P1 (0, the main database) onto the stack and use OpenWrite to open the cursor P1 on table P2 (base page 3, "examp") for modification.</p><blockquote><tt>16&nbsp;&nbsp;&nbsp;&nbsp;ListRead&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;20<br>17&nbsp;&nbsp;&nbsp;&nbsp;NotExists&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;19<br>18&nbsp;&nbsp;&nbsp;&nbsp;Delete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1<br>19&nbsp;&nbsp;&nbsp;&nbsp;Goto&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;16</tt></blockquote><p>This loop does the actual deleting.  It is organized differently from the one in the UPDATE example.  The ListRead instruction plays the role that the Next did in the INSERT loop, but because it jumps to P2 on failure, and Next jumps on success, we put it at the start of the loop instead of the end.  This means that we have to put a Goto at the end of the loop to jump back to the the loop test at the beginning.  So this loop has the form of a C while(){...} loop, while the loop in the INSERT example had the form of a do{...}while() loop.  The Delete instruction fills the role that the callback function did in the preceding examples.</p><p>The <a href="opcode.html#ListRead">ListRead</a> instruction reads an element from the temporary storage list and pushes it onto the stack.  If this was successful, it continues to the next instruction.  If this fails because the list is empty, it branches to P2, which is the instruction just after the loop.  Afterwards the stack looks like:</p><blockquote><table border=2><tr><td align=left>(integer) key for current record</td></tr></table></blockquote><p>Notice the similarity between the ListRead and Next instructions.  Both operations work according to this rule:</p><blockquote>Push the next "thing" onto the stack and fall through OR jump to P2, depending on whether or not there is a next "thing" to push.</blockquote><p>One difference between Next and ListRead is their idea of a "thing".  The "things" for the Next instruction are records in a database file.  "Things" for ListRead are integer keys in a list.  Another difference is whether to jump or fall through if there is no next "thing".  In this case, Next falls through, and ListRead jumps. Later on, we will see other looping instructions (NextIdx and SortNext) that operate using the same principle.</p><p>The <a href="opcode.html#NotExists">NotExists</a> instruction pops the top stack element and uses it as an integer key.  If a record with that key does not exist in table P1, then jump to P2.  If a record does exist, then fall thru to the next instruction.  In this case P2 takes us to the Goto at the end of the loop, which jumps back to the ListRead at the beginning.  This could have been coded to have P2 be 16, the ListRead at the start of the loop, but the SQLite parser which generated this code didn't make that optimization.</p><p>The <a href="opcode.html#Delete">Delete</a> does the work of this loop; it pops an integer key off the stack (placed there by the preceding ListRead) and deletes the record of cursor P1 that has that key.  Because P2 is true, the row change counter is incremented.</p><p>The <a href="opcode.html#Goto">Goto</a> jumps back to the beginning of the loop.  This is the end of the loop.</p><blockquote><tt>20&nbsp;&nbsp;&nbsp;&nbsp;ListReset&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0<br>21&nbsp;&nbsp;&nbsp;&nbsp;Close&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0<br>22&nbsp;&nbsp;&nbsp;&nbsp;Commit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0<br>23&nbsp;&nbsp;&nbsp;&nbsp;Halt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</tt></blockquote><p>This block of instruction cleans up the VDBE program. Three of these instructions aren't really required, but are generated by the SQLite parser from its code templates, which are designed to handle more complicated cases.</p><p>The <a href="opcode.html#ListReset">ListReset</a> instruction empties the temporary storage list.  This list is emptied automatically when the VDBE program terminates, so it isn't necessary in this case.  The Close instruction closes the cursor P1.  Again, this is done by the VDBE engine when it is finished running this program.  The Commit ends the current transaction successfully, and causes all changes that occurred in this transaction to be saved to the database.  The final Halt is also unneccessary, since it is added to every VDBE program when it is prepared to run.</p><p>UPDATE statements work very much like DELETE statements exceptthat instead of deleting the record they replace it with a new one.Consider this example:</p><blockquote><pre>UPDATE examp SET one= '(' || one || ')' WHERE two < 50;</pre></blockquote><p>Instead of deleting records where the "two" column is less than50, this statement just puts the "one" column in parenthesesThe VDBE program to implement this statement follows:</p><blockquote><tt>addr&nbsp;&nbsp;opcode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>----&nbsp;&nbsp;------------&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