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

Side by Side Diff: third_party/sqlite/amalgamation/sqlite3.08.c

Issue 2755803002: NCI: trybot test for sqlite 3.17 import. (Closed)
Patch Set: also clang on Linux i386 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /************** Begin file sqlite3rbu.c **************************************/
2 /*
3 ** 2014 August 30
4 **
5 ** The author disclaims copyright to this source code. In place of
6 ** a legal notice, here is a blessing:
7 **
8 ** May you do good and not evil.
9 ** May you find forgiveness for yourself and forgive others.
10 ** May you share freely, never taking more than you give.
11 **
12 *************************************************************************
13 **
14 **
15 ** OVERVIEW
16 **
17 ** The RBU extension requires that the RBU update be packaged as an
18 ** SQLite database. The tables it expects to find are described in
19 ** sqlite3rbu.h. Essentially, for each table xyz in the target database
20 ** that the user wishes to write to, a corresponding data_xyz table is
21 ** created in the RBU database and populated with one row for each row to
22 ** update, insert or delete from the target table.
23 **
24 ** The update proceeds in three stages:
25 **
26 ** 1) The database is updated. The modified database pages are written
27 ** to a *-oal file. A *-oal file is just like a *-wal file, except
28 ** that it is named "<database>-oal" instead of "<database>-wal".
29 ** Because regular SQLite clients do not look for file named
30 ** "<database>-oal", they go on using the original database in
31 ** rollback mode while the *-oal file is being generated.
32 **
33 ** During this stage RBU does not update the database by writing
34 ** directly to the target tables. Instead it creates "imposter"
35 ** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
36 ** to update each b-tree individually. All updates required by each
37 ** b-tree are completed before moving on to the next, and all
38 ** updates are done in sorted key order.
39 **
40 ** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
41 ** location using a call to rename(2). Before doing this the RBU
42 ** module takes an EXCLUSIVE lock on the database file, ensuring
43 ** that there are no other active readers.
44 **
45 ** Once the EXCLUSIVE lock is released, any other database readers
46 ** detect the new *-wal file and read the database in wal mode. At
47 ** this point they see the new version of the database - including
48 ** the updates made as part of the RBU update.
49 **
50 ** 3) The new *-wal file is checkpointed. This proceeds in the same way
51 ** as a regular database checkpoint, except that a single frame is
52 ** checkpointed each time sqlite3rbu_step() is called. If the RBU
53 ** handle is closed before the entire *-wal file is checkpointed,
54 ** the checkpoint progress is saved in the RBU database and the
55 ** checkpoint can be resumed by another RBU client at some point in
56 ** the future.
57 **
58 ** POTENTIAL PROBLEMS
59 **
60 ** The rename() call might not be portable. And RBU is not currently
61 ** syncing the directory after renaming the file.
62 **
63 ** When state is saved, any commit to the *-oal file and the commit to
64 ** the RBU update database are not atomic. So if the power fails at the
65 ** wrong moment they might get out of sync. As the main database will be
66 ** committed before the RBU update database this will likely either just
67 ** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
68 ** constraint violations).
69 **
70 ** If some client does modify the target database mid RBU update, or some
71 ** other error occurs, the RBU extension will keep throwing errors. It's
72 ** not really clear how to get out of this state. The system could just
73 ** by delete the RBU update database and *-oal file and have the device
74 ** download the update again and start over.
75 **
76 ** At present, for an UPDATE, both the new.* and old.* records are
77 ** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
78 ** fields are collected. This means we're probably writing a lot more
79 ** data to disk when saving the state of an ongoing update to the RBU
80 ** update database than is strictly necessary.
81 **
82 */
83
84 /* #include <assert.h> */
85 /* #include <string.h> */
86 /* #include <stdio.h> */
87
88 /* #include "sqlite3.h" */
89
90 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
91 /************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
92 /************** Begin file sqlite3rbu.h **************************************/
93 /*
94 ** 2014 August 30
95 **
96 ** The author disclaims copyright to this source code. In place of
97 ** a legal notice, here is a blessing:
98 **
99 ** May you do good and not evil.
100 ** May you find forgiveness for yourself and forgive others.
101 ** May you share freely, never taking more than you give.
102 **
103 *************************************************************************
104 **
105 ** This file contains the public interface for the RBU extension.
106 */
107
108 /*
109 ** SUMMARY
110 **
111 ** Writing a transaction containing a large number of operations on
112 ** b-tree indexes that are collectively larger than the available cache
113 ** memory can be very inefficient.
114 **
115 ** The problem is that in order to update a b-tree, the leaf page (at least)
116 ** containing the entry being inserted or deleted must be modified. If the
117 ** working set of leaves is larger than the available cache memory, then a
118 ** single leaf that is modified more than once as part of the transaction
119 ** may be loaded from or written to the persistent media multiple times.
120 ** Additionally, because the index updates are likely to be applied in
121 ** random order, access to pages within the database is also likely to be in
122 ** random order, which is itself quite inefficient.
123 **
124 ** One way to improve the situation is to sort the operations on each index
125 ** by index key before applying them to the b-tree. This leads to an IO
126 ** pattern that resembles a single linear scan through the index b-tree,
127 ** and all but guarantees each modified leaf page is loaded and stored
128 ** exactly once. SQLite uses this trick to improve the performance of
129 ** CREATE INDEX commands. This extension allows it to be used to improve
130 ** the performance of large transactions on existing databases.
131 **
132 ** Additionally, this extension allows the work involved in writing the
133 ** large transaction to be broken down into sub-transactions performed
134 ** sequentially by separate processes. This is useful if the system cannot
135 ** guarantee that a single update process will run for long enough to apply
136 ** the entire update, for example because the update is being applied on a
137 ** mobile device that is frequently rebooted. Even after the writer process
138 ** has committed one or more sub-transactions, other database clients continue
139 ** to read from the original database snapshot. In other words, partially
140 ** applied transactions are not visible to other clients.
141 **
142 ** "RBU" stands for "Resumable Bulk Update". As in a large database update
143 ** transmitted via a wireless network to a mobile device. A transaction
144 ** applied using this extension is hence refered to as an "RBU update".
145 **
146 **
147 ** LIMITATIONS
148 **
149 ** An "RBU update" transaction is subject to the following limitations:
150 **
151 ** * The transaction must consist of INSERT, UPDATE and DELETE operations
152 ** only.
153 **
154 ** * INSERT statements may not use any default values.
155 **
156 ** * UPDATE and DELETE statements must identify their target rows by
157 ** non-NULL PRIMARY KEY values. Rows with NULL values stored in PRIMARY
158 ** KEY fields may not be updated or deleted. If the table being written
159 ** has no PRIMARY KEY, affected rows must be identified by rowid.
160 **
161 ** * UPDATE statements may not modify PRIMARY KEY columns.
162 **
163 ** * No triggers will be fired.
164 **
165 ** * No foreign key violations are detected or reported.
166 **
167 ** * CHECK constraints are not enforced.
168 **
169 ** * No constraint handling mode except for "OR ROLLBACK" is supported.
170 **
171 **
172 ** PREPARATION
173 **
174 ** An "RBU update" is stored as a separate SQLite database. A database
175 ** containing an RBU update is an "RBU database". For each table in the
176 ** target database to be updated, the RBU database should contain a table
177 ** named "data_<target name>" containing the same set of columns as the
178 ** target table, and one more - "rbu_control". The data_% table should
179 ** have no PRIMARY KEY or UNIQUE constraints, but each column should have
180 ** the same type as the corresponding column in the target database.
181 ** The "rbu_control" column should have no type at all. For example, if
182 ** the target database contains:
183 **
184 ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
185 **
186 ** Then the RBU database should contain:
187 **
188 ** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
189 **
190 ** The order of the columns in the data_% table does not matter.
191 **
192 ** Instead of a regular table, the RBU database may also contain virtual
193 ** tables or view named using the data_<target> naming scheme.
194 **
195 ** Instead of the plain data_<target> naming scheme, RBU database tables
196 ** may also be named data<integer>_<target>, where <integer> is any sequence
197 ** of zero or more numeric characters (0-9). This can be significant because
198 ** tables within the RBU database are always processed in order sorted by
199 ** name. By judicious selection of the <integer> portion of the names
200 ** of the RBU tables the user can therefore control the order in which they
201 ** are processed. This can be useful, for example, to ensure that "external
202 ** content" FTS4 tables are updated before their underlying content tables.
203 **
204 ** If the target database table is a virtual table or a table that has no
205 ** PRIMARY KEY declaration, the data_% table must also contain a column
206 ** named "rbu_rowid". This column is mapped to the tables implicit primary
207 ** key column - "rowid". Virtual tables for which the "rowid" column does
208 ** not function like a primary key value cannot be updated using RBU. For
209 ** example, if the target db contains either of the following:
210 **
211 ** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
212 ** CREATE TABLE x1(a, b)
213 **
214 ** then the RBU database should contain:
215 **
216 ** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
217 **
218 ** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
219 ** target table must be present in the input table. For virtual tables,
220 ** hidden columns are optional - they are updated by RBU if present in
221 ** the input table, or not otherwise. For example, to write to an fts4
222 ** table with a hidden languageid column such as:
223 **
224 ** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
225 **
226 ** Either of the following input table schemas may be used:
227 **
228 ** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
229 ** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
230 **
231 ** For each row to INSERT into the target database as part of the RBU
232 ** update, the corresponding data_% table should contain a single record
233 ** with the "rbu_control" column set to contain integer value 0. The
234 ** other columns should be set to the values that make up the new record
235 ** to insert.
236 **
237 ** If the target database table has an INTEGER PRIMARY KEY, it is not
238 ** possible to insert a NULL value into the IPK column. Attempting to
239 ** do so results in an SQLITE_MISMATCH error.
240 **
241 ** For each row to DELETE from the target database as part of the RBU
242 ** update, the corresponding data_% table should contain a single record
243 ** with the "rbu_control" column set to contain integer value 1. The
244 ** real primary key values of the row to delete should be stored in the
245 ** corresponding columns of the data_% table. The values stored in the
246 ** other columns are not used.
247 **
248 ** For each row to UPDATE from the target database as part of the RBU
249 ** update, the corresponding data_% table should contain a single record
250 ** with the "rbu_control" column set to contain a value of type text.
251 ** The real primary key values identifying the row to update should be
252 ** stored in the corresponding columns of the data_% table row, as should
253 ** the new values of all columns being update. The text value in the
254 ** "rbu_control" column must contain the same number of characters as
255 ** there are columns in the target database table, and must consist entirely
256 ** of 'x' and '.' characters (or in some special cases 'd' - see below). For
257 ** each column that is being updated, the corresponding character is set to
258 ** 'x'. For those that remain as they are, the corresponding character of the
259 ** rbu_control value should be set to '.'. For example, given the tables
260 ** above, the update statement:
261 **
262 ** UPDATE t1 SET c = 'usa' WHERE a = 4;
263 **
264 ** is represented by the data_t1 row created by:
265 **
266 ** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
267 **
268 ** Instead of an 'x' character, characters of the rbu_control value specified
269 ** for UPDATEs may also be set to 'd'. In this case, instead of updating the
270 ** target table with the value stored in the corresponding data_% column, the
271 ** user-defined SQL function "rbu_delta()" is invoked and the result stored in
272 ** the target table column. rbu_delta() is invoked with two arguments - the
273 ** original value currently stored in the target table column and the
274 ** value specified in the data_xxx table.
275 **
276 ** For example, this row:
277 **
278 ** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
279 **
280 ** is similar to an UPDATE statement such as:
281 **
282 ** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
283 **
284 ** Finally, if an 'f' character appears in place of a 'd' or 's' in an
285 ** ota_control string, the contents of the data_xxx table column is assumed
286 ** to be a "fossil delta" - a patch to be applied to a blob value in the
287 ** format used by the fossil source-code management system. In this case
288 ** the existing value within the target database table must be of type BLOB.
289 ** It is replaced by the result of applying the specified fossil delta to
290 ** itself.
291 **
292 ** If the target database table is a virtual table or a table with no PRIMARY
293 ** KEY, the rbu_control value should not include a character corresponding
294 ** to the rbu_rowid value. For example, this:
295 **
296 ** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
297 ** VALUES(NULL, 'usa', 12, '.x');
298 **
299 ** causes a result similar to:
300 **
301 ** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
302 **
303 ** The data_xxx tables themselves should have no PRIMARY KEY declarations.
304 ** However, RBU is more efficient if reading the rows in from each data_xxx
305 ** table in "rowid" order is roughly the same as reading them sorted by
306 ** the PRIMARY KEY of the corresponding target database table. In other
307 ** words, rows should be sorted using the destination table PRIMARY KEY
308 ** fields before they are inserted into the data_xxx tables.
309 **
310 ** USAGE
311 **
312 ** The API declared below allows an application to apply an RBU update
313 ** stored on disk to an existing target database. Essentially, the
314 ** application:
315 **
316 ** 1) Opens an RBU handle using the sqlite3rbu_open() function.
317 **
318 ** 2) Registers any required virtual table modules with the database
319 ** handle returned by sqlite3rbu_db(). Also, if required, register
320 ** the rbu_delta() implementation.
321 **
322 ** 3) Calls the sqlite3rbu_step() function one or more times on
323 ** the new handle. Each call to sqlite3rbu_step() performs a single
324 ** b-tree operation, so thousands of calls may be required to apply
325 ** a complete update.
326 **
327 ** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
328 ** sqlite3rbu_step() has been called enough times to completely
329 ** apply the update to the target database, then the RBU database
330 ** is marked as fully applied. Otherwise, the state of the RBU
331 ** update application is saved in the RBU database for later
332 ** resumption.
333 **
334 ** See comments below for more detail on APIs.
335 **
336 ** If an update is only partially applied to the target database by the
337 ** time sqlite3rbu_close() is called, various state information is saved
338 ** within the RBU database. This allows subsequent processes to automatically
339 ** resume the RBU update from where it left off.
340 **
341 ** To remove all RBU extension state information, returning an RBU database
342 ** to its original contents, it is sufficient to drop all tables that begin
343 ** with the prefix "rbu_"
344 **
345 ** DATABASE LOCKING
346 **
347 ** An RBU update may not be applied to a database in WAL mode. Attempting
348 ** to do so is an error (SQLITE_ERROR).
349 **
350 ** While an RBU handle is open, a SHARED lock may be held on the target
351 ** database file. This means it is possible for other clients to read the
352 ** database, but not to write it.
353 **
354 ** If an RBU update is started and then suspended before it is completed,
355 ** then an external client writes to the database, then attempting to resume
356 ** the suspended RBU update is also an error (SQLITE_BUSY).
357 */
358
359 #ifndef _SQLITE3RBU_H
360 #define _SQLITE3RBU_H
361
362 /* #include "sqlite3.h" ** Required for error code definitions ** * /
363
364 #if 0
365 extern "C" {
366 #endif
367
368 typedef struct sqlite3rbu sqlite3rbu;
369
370 /*
371 ** Open an RBU handle.
372 **
373 ** Argument zTarget is the path to the target database. Argument zRbu is
374 ** the path to the RBU database. Each call to this function must be matched
375 ** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
376 ** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
377 ** or zRbu begin with "file:", it will be interpreted as an SQLite
378 ** database URI, not a regular file name.
379 **
380 ** If the zState argument is passed a NULL value, the RBU extension stores
381 ** the current state of the update (how many rows have been updated, which
382 ** indexes are yet to be updated etc.) within the RBU database itself. This
383 ** can be convenient, as it means that the RBU application does not need to
384 ** organize removing a separate state file after the update is concluded.
385 ** Or, if zState is non-NULL, it must be a path to a database file in which
386 ** the RBU extension can store the state of the update.
387 **
388 ** When resuming an RBU update, the zState argument must be passed the same
389 ** value as when the RBU update was started.
390 **
391 ** Once the RBU update is finished, the RBU extension does not
392 ** automatically remove any zState database file, even if it created it.
393 **
394 ** By default, RBU uses the default VFS to access the files on disk. To
395 ** use a VFS other than the default, an SQLite "file:" URI containing a
396 ** "vfs=..." option may be passed as the zTarget option.
397 **
398 ** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
399 ** SQLite's built-in VFSs, including the multiplexor VFS. However it does
400 ** not work out of the box with zipvfs. Refer to the comment describing
401 ** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
402 */
403 SQLITE_API sqlite3rbu *sqlite3rbu_open(
404 const char *zTarget,
405 const char *zRbu,
406 const char *zState
407 );
408
409 /*
410 ** Open an RBU handle to perform an RBU vacuum on database file zTarget.
411 ** An RBU vacuum is similar to SQLite's built-in VACUUM command, except
412 ** that it can be suspended and resumed like an RBU update.
413 **
414 ** The second argument to this function identifies a database in which
415 ** to store the state of the RBU vacuum operation if it is suspended. The
416 ** first time sqlite3rbu_vacuum() is called, to start an RBU vacuum
417 ** operation, the state database should either not exist or be empty
418 ** (contain no tables). If an RBU vacuum is suspended by calling
419 ** sqlite3rbu_close() on the RBU handle before sqlite3rbu_step() has
420 ** returned SQLITE_DONE, the vacuum state is stored in the state database.
421 ** The vacuum can be resumed by calling this function to open a new RBU
422 ** handle specifying the same target and state databases.
423 **
424 ** If the second argument passed to this function is NULL, then the
425 ** name of the state database is "<database>-vacuum", where <database>
426 ** is the name of the target database file. In this case, on UNIX, if the
427 ** state database is not already present in the file-system, it is created
428 ** with the same permissions as the target db is made.
429 **
430 ** This function does not delete the state database after an RBU vacuum
431 ** is completed, even if it created it. However, if the call to
432 ** sqlite3rbu_close() returns any value other than SQLITE_OK, the contents
433 ** of the state tables within the state database are zeroed. This way,
434 ** the next call to sqlite3rbu_vacuum() opens a handle that starts a
435 ** new RBU vacuum operation.
436 **
437 ** As with sqlite3rbu_open(), Zipvfs users should rever to the comment
438 ** describing the sqlite3rbu_create_vfs() API function below for
439 ** a description of the complications associated with using RBU with
440 ** zipvfs databases.
441 */
442 SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
443 const char *zTarget,
444 const char *zState
445 );
446
447 /*
448 ** Internally, each RBU connection uses a separate SQLite database
449 ** connection to access the target and rbu update databases. This
450 ** API allows the application direct access to these database handles.
451 **
452 ** The first argument passed to this function must be a valid, open, RBU
453 ** handle. The second argument should be passed zero to access the target
454 ** database handle, or non-zero to access the rbu update database handle.
455 ** Accessing the underlying database handles may be useful in the
456 ** following scenarios:
457 **
458 ** * If any target tables are virtual tables, it may be necessary to
459 ** call sqlite3_create_module() on the target database handle to
460 ** register the required virtual table implementations.
461 **
462 ** * If the data_xxx tables in the RBU source database are virtual
463 ** tables, the application may need to call sqlite3_create_module() on
464 ** the rbu update db handle to any required virtual table
465 ** implementations.
466 **
467 ** * If the application uses the "rbu_delta()" feature described above,
468 ** it must use sqlite3_create_function() or similar to register the
469 ** rbu_delta() implementation with the target database handle.
470 **
471 ** If an error has occurred, either while opening or stepping the RBU object,
472 ** this function may return NULL. The error code and message may be collected
473 ** when sqlite3rbu_close() is called.
474 **
475 ** Database handles returned by this function remain valid until the next
476 ** call to any sqlite3rbu_xxx() function other than sqlite3rbu_db().
477 */
478 SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu*, int bRbu);
479
480 /*
481 ** Do some work towards applying the RBU update to the target db.
482 **
483 ** Return SQLITE_DONE if the update has been completely applied, or
484 ** SQLITE_OK if no error occurs but there remains work to do to apply
485 ** the RBU update. If an error does occur, some other error code is
486 ** returned.
487 **
488 ** Once a call to sqlite3rbu_step() has returned a value other than
489 ** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
490 ** that immediately return the same value.
491 */
492 SQLITE_API int sqlite3rbu_step(sqlite3rbu *pRbu);
493
494 /*
495 ** Force RBU to save its state to disk.
496 **
497 ** If a power failure or application crash occurs during an update, following
498 ** system recovery RBU may resume the update from the point at which the state
499 ** was last saved. In other words, from the most recent successful call to
500 ** sqlite3rbu_close() or this function.
501 **
502 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
503 */
504 SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *pRbu);
505
506 /*
507 ** Close an RBU handle.
508 **
509 ** If the RBU update has been completely applied, mark the RBU database
510 ** as fully applied. Otherwise, assuming no error has occurred, save the
511 ** current state of the RBU update appliation to the RBU database.
512 **
513 ** If an error has already occurred as part of an sqlite3rbu_step()
514 ** or sqlite3rbu_open() call, or if one occurs within this function, an
515 ** SQLite error code is returned. Additionally, *pzErrmsg may be set to
516 ** point to a buffer containing a utf-8 formatted English language error
517 ** message. It is the responsibility of the caller to eventually free any
518 ** such buffer using sqlite3_free().
519 **
520 ** Otherwise, if no error occurs, this function returns SQLITE_OK if the
521 ** update has been partially applied, or SQLITE_DONE if it has been
522 ** completely applied.
523 */
524 SQLITE_API int sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
525
526 /*
527 ** Return the total number of key-value operations (inserts, deletes or
528 ** updates) that have been performed on the target database since the
529 ** current RBU update was started.
530 */
531 SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu);
532
533 /*
534 ** Obtain permyriadage (permyriadage is to 10000 as percentage is to 100)
535 ** progress indications for the two stages of an RBU update. This API may
536 ** be useful for driving GUI progress indicators and similar.
537 **
538 ** An RBU update is divided into two stages:
539 **
540 ** * Stage 1, in which changes are accumulated in an oal/wal file, and
541 ** * Stage 2, in which the contents of the wal file are copied into the
542 ** main database.
543 **
544 ** The update is visible to non-RBU clients during stage 2. During stage 1
545 ** non-RBU reader clients may see the original database.
546 **
547 ** If this API is called during stage 2 of the update, output variable
548 ** (*pnOne) is set to 10000 to indicate that stage 1 has finished and (*pnTwo)
549 ** to a value between 0 and 10000 to indicate the permyriadage progress of
550 ** stage 2. A value of 5000 indicates that stage 2 is half finished,
551 ** 9000 indicates that it is 90% finished, and so on.
552 **
553 ** If this API is called during stage 1 of the update, output variable
554 ** (*pnTwo) is set to 0 to indicate that stage 2 has not yet started. The
555 ** value to which (*pnOne) is set depends on whether or not the RBU
556 ** database contains an "rbu_count" table. The rbu_count table, if it
557 ** exists, must contain the same columns as the following:
558 **
559 ** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
560 **
561 ** There must be one row in the table for each source (data_xxx) table within
562 ** the RBU database. The 'tbl' column should contain the name of the source
563 ** table. The 'cnt' column should contain the number of rows within the
564 ** source table.
565 **
566 ** If the rbu_count table is present and populated correctly and this
567 ** API is called during stage 1, the *pnOne output variable is set to the
568 ** permyriadage progress of the same stage. If the rbu_count table does
569 ** not exist, then (*pnOne) is set to -1 during stage 1. If the rbu_count
570 ** table exists but is not correctly populated, the value of the *pnOne
571 ** output variable during stage 1 is undefined.
572 */
573 SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *pRbu, int *pnOne, int *pnTwo) ;
574
575 /*
576 ** Obtain an indication as to the current stage of an RBU update or vacuum.
577 ** This function always returns one of the SQLITE_RBU_STATE_XXX constants
578 ** defined in this file. Return values should be interpreted as follows:
579 **
580 ** SQLITE_RBU_STATE_OAL:
581 ** RBU is currently building a *-oal file. The next call to sqlite3rbu_step()
582 ** may either add further data to the *-oal file, or compute data that will
583 ** be added by a subsequent call.
584 **
585 ** SQLITE_RBU_STATE_MOVE:
586 ** RBU has finished building the *-oal file. The next call to sqlite3rbu_step( )
587 ** will move the *-oal file to the equivalent *-wal path. If the current
588 ** operation is an RBU update, then the updated version of the database
589 ** file will become visible to ordinary SQLite clients following the next
590 ** call to sqlite3rbu_step().
591 **
592 ** SQLITE_RBU_STATE_CHECKPOINT:
593 ** RBU is currently performing an incremental checkpoint. The next call to
594 ** sqlite3rbu_step() will copy a page of data from the *-wal file into
595 ** the target database file.
596 **
597 ** SQLITE_RBU_STATE_DONE:
598 ** The RBU operation has finished. Any subsequent calls to sqlite3rbu_step()
599 ** will immediately return SQLITE_DONE.
600 **
601 ** SQLITE_RBU_STATE_ERROR:
602 ** An error has occurred. Any subsequent calls to sqlite3rbu_step() will
603 ** immediately return the SQLite error code associated with the error.
604 */
605 #define SQLITE_RBU_STATE_OAL 1
606 #define SQLITE_RBU_STATE_MOVE 2
607 #define SQLITE_RBU_STATE_CHECKPOINT 3
608 #define SQLITE_RBU_STATE_DONE 4
609 #define SQLITE_RBU_STATE_ERROR 5
610
611 SQLITE_API int sqlite3rbu_state(sqlite3rbu *pRbu);
612
613 /*
614 ** Create an RBU VFS named zName that accesses the underlying file-system
615 ** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
616 ** then the new RBU VFS uses the default system VFS to access the file-system.
617 ** The new object is registered as a non-default VFS with SQLite before
618 ** returning.
619 **
620 ** Part of the RBU implementation uses a custom VFS object. Usually, this
621 ** object is created and deleted automatically by RBU.
622 **
623 ** The exception is for applications that also use zipvfs. In this case,
624 ** the custom VFS must be explicitly created by the user before the RBU
625 ** handle is opened. The RBU VFS should be installed so that the zipvfs
626 ** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
627 ** (for example multiplexor) to access the file-system. For example,
628 ** to assemble an RBU enabled VFS stack that uses both zipvfs and
629 ** multiplexor (error checking omitted):
630 **
631 ** // Create a VFS named "multiplex" (not the default).
632 ** sqlite3_multiplex_initialize(0, 0);
633 **
634 ** // Create an rbu VFS named "rbu" that uses multiplexor. If the
635 ** // second argument were replaced with NULL, the "rbu" VFS would
636 ** // access the file-system via the system default VFS, bypassing the
637 ** // multiplexor.
638 ** sqlite3rbu_create_vfs("rbu", "multiplex");
639 **
640 ** // Create a zipvfs VFS named "zipvfs" that uses rbu.
641 ** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
642 **
643 ** // Make zipvfs the default VFS.
644 ** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
645 **
646 ** Because the default VFS created above includes a RBU functionality, it
647 ** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
648 ** that does not include the RBU layer results in an error.
649 **
650 ** The overhead of adding the "rbu" VFS to the system is negligible for
651 ** non-RBU users. There is no harm in an application accessing the
652 ** file-system via "rbu" all the time, even if it only uses RBU functionality
653 ** occasionally.
654 */
655 SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent);
656
657 /*
658 ** Deregister and destroy an RBU vfs created by an earlier call to
659 ** sqlite3rbu_create_vfs().
660 **
661 ** VFS objects are not reference counted. If a VFS object is destroyed
662 ** before all database handles that use it have been closed, the results
663 ** are undefined.
664 */
665 SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName);
666
667 #if 0
668 } /* end of the 'extern "C"' block */
669 #endif
670
671 #endif /* _SQLITE3RBU_H */
672
673 /************** End of sqlite3rbu.h ******************************************/
674 /************** Continuing where we left off in sqlite3rbu.c *****************/
675
676 #if defined(_WIN32_WCE)
677 /* #include "windows.h" */
678 #endif
679
680 /* Maximum number of prepared UPDATE statements held by this module */
681 #define SQLITE_RBU_UPDATE_CACHESIZE 16
682
683 /*
684 ** Swap two objects of type TYPE.
685 */
686 #if !defined(SQLITE_AMALGAMATION)
687 # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
688 #endif
689
690 /*
691 ** The rbu_state table is used to save the state of a partially applied
692 ** update so that it can be resumed later. The table consists of integer
693 ** keys mapped to values as follows:
694 **
695 ** RBU_STATE_STAGE:
696 ** May be set to integer values 1, 2, 4 or 5. As follows:
697 ** 1: the *-rbu file is currently under construction.
698 ** 2: the *-rbu file has been constructed, but not yet moved
699 ** to the *-wal path.
700 ** 4: the checkpoint is underway.
701 ** 5: the rbu update has been checkpointed.
702 **
703 ** RBU_STATE_TBL:
704 ** Only valid if STAGE==1. The target database name of the table
705 ** currently being written.
706 **
707 ** RBU_STATE_IDX:
708 ** Only valid if STAGE==1. The target database name of the index
709 ** currently being written, or NULL if the main table is currently being
710 ** updated.
711 **
712 ** RBU_STATE_ROW:
713 ** Only valid if STAGE==1. Number of rows already processed for the current
714 ** table/index.
715 **
716 ** RBU_STATE_PROGRESS:
717 ** Trbul number of sqlite3rbu_step() calls made so far as part of this
718 ** rbu update.
719 **
720 ** RBU_STATE_CKPT:
721 ** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
722 ** header created by recovering the *-wal file. This is used to detect
723 ** cases when another client appends frames to the *-wal file in the
724 ** middle of an incremental checkpoint (an incremental checkpoint cannot
725 ** be continued if this happens).
726 **
727 ** RBU_STATE_COOKIE:
728 ** Valid if STAGE==1. The current change-counter cookie value in the
729 ** target db file.
730 **
731 ** RBU_STATE_OALSZ:
732 ** Valid if STAGE==1. The size in bytes of the *-oal file.
733 */
734 #define RBU_STATE_STAGE 1
735 #define RBU_STATE_TBL 2
736 #define RBU_STATE_IDX 3
737 #define RBU_STATE_ROW 4
738 #define RBU_STATE_PROGRESS 5
739 #define RBU_STATE_CKPT 6
740 #define RBU_STATE_COOKIE 7
741 #define RBU_STATE_OALSZ 8
742 #define RBU_STATE_PHASEONESTEP 9
743
744 #define RBU_STAGE_OAL 1
745 #define RBU_STAGE_MOVE 2
746 #define RBU_STAGE_CAPTURE 3
747 #define RBU_STAGE_CKPT 4
748 #define RBU_STAGE_DONE 5
749
750
751 #define RBU_CREATE_STATE \
752 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
753
754 typedef struct RbuFrame RbuFrame;
755 typedef struct RbuObjIter RbuObjIter;
756 typedef struct RbuState RbuState;
757 typedef struct rbu_vfs rbu_vfs;
758 typedef struct rbu_file rbu_file;
759 typedef struct RbuUpdateStmt RbuUpdateStmt;
760
761 #if !defined(SQLITE_AMALGAMATION)
762 typedef unsigned int u32;
763 typedef unsigned short u16;
764 typedef unsigned char u8;
765 typedef sqlite3_int64 i64;
766 #endif
767
768 /*
769 ** These values must match the values defined in wal.c for the equivalent
770 ** locks. These are not magic numbers as they are part of the SQLite file
771 ** format.
772 */
773 #define WAL_LOCK_WRITE 0
774 #define WAL_LOCK_CKPT 1
775 #define WAL_LOCK_READ0 3
776
777 #define SQLITE_FCNTL_RBUCNT 5149216
778
779 /*
780 ** A structure to store values read from the rbu_state table in memory.
781 */
782 struct RbuState {
783 int eStage;
784 char *zTbl;
785 char *zIdx;
786 i64 iWalCksum;
787 int nRow;
788 i64 nProgress;
789 u32 iCookie;
790 i64 iOalSz;
791 i64 nPhaseOneStep;
792 };
793
794 struct RbuUpdateStmt {
795 char *zMask; /* Copy of update mask used with pUpdate */
796 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
797 RbuUpdateStmt *pNext;
798 };
799
800 /*
801 ** An iterator of this type is used to iterate through all objects in
802 ** the target database that require updating. For each such table, the
803 ** iterator visits, in order:
804 **
805 ** * the table itself,
806 ** * each index of the table (zero or more points to visit), and
807 ** * a special "cleanup table" state.
808 **
809 ** abIndexed:
810 ** If the table has no indexes on it, abIndexed is set to NULL. Otherwise,
811 ** it points to an array of flags nTblCol elements in size. The flag is
812 ** set for each column that is either a part of the PK or a part of an
813 ** index. Or clear otherwise.
814 **
815 */
816 struct RbuObjIter {
817 sqlite3_stmt *pTblIter; /* Iterate through tables */
818 sqlite3_stmt *pIdxIter; /* Index iterator */
819 int nTblCol; /* Size of azTblCol[] array */
820 char **azTblCol; /* Array of unquoted target column names */
821 char **azTblType; /* Array of target column types */
822 int *aiSrcOrder; /* src table col -> target table col */
823 u8 *abTblPk; /* Array of flags, set on target PK columns */
824 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
825 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
826 int eType; /* Table type - an RBU_PK_XXX value */
827
828 /* Output variables. zTbl==0 implies EOF. */
829 int bCleanup; /* True in "cleanup" state */
830 const char *zTbl; /* Name of target db table */
831 const char *zDataTbl; /* Name of rbu db table (or null) */
832 const char *zIdx; /* Name of target db index (or null) */
833 int iTnum; /* Root page of current object */
834 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
835 int bUnique; /* Current index is unique */
836 int nIndex; /* Number of aux. indexes on table zTbl */
837
838 /* Statements created by rbuObjIterPrepareAll() */
839 int nCol; /* Number of columns in current object */
840 sqlite3_stmt *pSelect; /* Source data */
841 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
842 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
843 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zDataTbl */
844
845 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
846 RbuUpdateStmt *pRbuUpdate;
847 };
848
849 /*
850 ** Values for RbuObjIter.eType
851 **
852 ** 0: Table does not exist (error)
853 ** 1: Table has an implicit rowid.
854 ** 2: Table has an explicit IPK column.
855 ** 3: Table has an external PK index.
856 ** 4: Table is WITHOUT ROWID.
857 ** 5: Table is a virtual table.
858 */
859 #define RBU_PK_NOTABLE 0
860 #define RBU_PK_NONE 1
861 #define RBU_PK_IPK 2
862 #define RBU_PK_EXTERNAL 3
863 #define RBU_PK_WITHOUT_ROWID 4
864 #define RBU_PK_VTAB 5
865
866
867 /*
868 ** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
869 ** one of the following operations.
870 */
871 #define RBU_INSERT 1 /* Insert on a main table b-tree */
872 #define RBU_DELETE 2 /* Delete a row from a main table b-tree */
873 #define RBU_REPLACE 3 /* Delete and then insert a row */
874 #define RBU_IDX_DELETE 4 /* Delete a row from an aux. index b-tree */
875 #define RBU_IDX_INSERT 5 /* Insert on an aux. index b-tree */
876
877 #define RBU_UPDATE 6 /* Update a row in a main table b-tree */
878
879 /*
880 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
881 ** file should be copied to page iDbPage of the database file.
882 */
883 struct RbuFrame {
884 u32 iDbPage;
885 u32 iWalFrame;
886 };
887
888 /*
889 ** RBU handle.
890 **
891 ** nPhaseOneStep:
892 ** If the RBU database contains an rbu_count table, this value is set to
893 ** a running estimate of the number of b-tree operations required to
894 ** finish populating the *-oal file. This allows the sqlite3_bp_progress()
895 ** API to calculate the permyriadage progress of populating the *-oal file
896 ** using the formula:
897 **
898 ** permyriadage = (10000 * nProgress) / nPhaseOneStep
899 **
900 ** nPhaseOneStep is initialized to the sum of:
901 **
902 ** nRow * (nIndex + 1)
903 **
904 ** for all source tables in the RBU database, where nRow is the number
905 ** of rows in the source table and nIndex the number of indexes on the
906 ** corresponding target database table.
907 **
908 ** This estimate is accurate if the RBU update consists entirely of
909 ** INSERT operations. However, it is inaccurate if:
910 **
911 ** * the RBU update contains any UPDATE operations. If the PK specified
912 ** for an UPDATE operation does not exist in the target table, then
913 ** no b-tree operations are required on index b-trees. Or if the
914 ** specified PK does exist, then (nIndex*2) such operations are
915 ** required (one delete and one insert on each index b-tree).
916 **
917 ** * the RBU update contains any DELETE operations for which the specified
918 ** PK does not exist. In this case no operations are required on index
919 ** b-trees.
920 **
921 ** * the RBU update contains REPLACE operations. These are similar to
922 ** UPDATE operations.
923 **
924 ** nPhaseOneStep is updated to account for the conditions above during the
925 ** first pass of each source table. The updated nPhaseOneStep value is
926 ** stored in the rbu_state table if the RBU update is suspended.
927 */
928 struct sqlite3rbu {
929 int eStage; /* Value of RBU_STATE_STAGE field */
930 sqlite3 *dbMain; /* target database handle */
931 sqlite3 *dbRbu; /* rbu database handle */
932 char *zTarget; /* Path to target db */
933 char *zRbu; /* Path to rbu db */
934 char *zState; /* Path to state db (or NULL if zRbu) */
935 char zStateDb[5]; /* Db name for state ("stat" or "main") */
936 int rc; /* Value returned by last rbu_step() call */
937 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
938 int nStep; /* Rows processed for current object */
939 int nProgress; /* Rows processed for all objects */
940 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
941 const char *zVfsName; /* Name of automatically created rbu vfs */
942 rbu_file *pTargetFd; /* File handle open on target db */
943 i64 iOalSz;
944 i64 nPhaseOneStep;
945
946 /* The following state variables are used as part of the incremental
947 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
948 ** function rbuSetupCheckpoint() for details. */
949 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
950 u32 mLock;
951 int nFrame; /* Entries in aFrame[] array */
952 int nFrameAlloc; /* Allocated size of aFrame[] array */
953 RbuFrame *aFrame;
954 int pgsz;
955 u8 *aBuf;
956 i64 iWalCksum;
957
958 /* Used in RBU vacuum mode only */
959 int nRbu; /* Number of RBU VFS in the stack */
960 rbu_file *pRbuFd; /* Fd for main db of dbRbu */
961 };
962
963 /*
964 ** An rbu VFS is implemented using an instance of this structure.
965 */
966 struct rbu_vfs {
967 sqlite3_vfs base; /* rbu VFS shim methods */
968 sqlite3_vfs *pRealVfs; /* Underlying VFS */
969 sqlite3_mutex *mutex; /* Mutex to protect pMain */
970 rbu_file *pMain; /* Linked list of main db files */
971 };
972
973 /*
974 ** Each file opened by an rbu VFS is represented by an instance of
975 ** the following structure.
976 */
977 struct rbu_file {
978 sqlite3_file base; /* sqlite3_file methods */
979 sqlite3_file *pReal; /* Underlying file handle */
980 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
981 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
982
983 int openFlags; /* Flags this file was opened with */
984 u32 iCookie; /* Cookie value for main db files */
985 u8 iWriteVer; /* "write-version" value for main db files */
986 u8 bNolock; /* True to fail EXCLUSIVE locks */
987
988 int nShm; /* Number of entries in apShm[] array */
989 char **apShm; /* Array of mmap'd *-shm regions */
990 char *zDel; /* Delete this when closing file */
991
992 const char *zWal; /* Wal filename for this main db file */
993 rbu_file *pWalFd; /* Wal file descriptor for this main db */
994 rbu_file *pMainNext; /* Next MAIN_DB file */
995 };
996
997 /*
998 ** True for an RBU vacuum handle, or false otherwise.
999 */
1000 #define rbuIsVacuum(p) ((p)->zTarget==0)
1001
1002
1003 /*************************************************************************
1004 ** The following three functions, found below:
1005 **
1006 ** rbuDeltaGetInt()
1007 ** rbuDeltaChecksum()
1008 ** rbuDeltaApply()
1009 **
1010 ** are lifted from the fossil source code (http://fossil-scm.org). They
1011 ** are used to implement the scalar SQL function rbu_fossil_delta().
1012 */
1013
1014 /*
1015 ** Read bytes from *pz and convert them into a positive integer. When
1016 ** finished, leave *pz pointing to the first character past the end of
1017 ** the integer. The *pLen parameter holds the length of the string
1018 ** in *pz and is decremented once for each character in the integer.
1019 */
1020 static unsigned int rbuDeltaGetInt(const char **pz, int *pLen){
1021 static const signed char zValue[] = {
1022 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1023 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1024 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
1025 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
1026 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1027 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, 36,
1028 -1, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
1029 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, 63, -1,
1030 };
1031 unsigned int v = 0;
1032 int c;
1033 unsigned char *z = (unsigned char*)*pz;
1034 unsigned char *zStart = z;
1035 while( (c = zValue[0x7f&*(z++)])>=0 ){
1036 v = (v<<6) + c;
1037 }
1038 z--;
1039 *pLen -= z - zStart;
1040 *pz = (char*)z;
1041 return v;
1042 }
1043
1044 /*
1045 ** Compute a 32-bit checksum on the N-byte buffer. Return the result.
1046 */
1047 static unsigned int rbuDeltaChecksum(const char *zIn, size_t N){
1048 const unsigned char *z = (const unsigned char *)zIn;
1049 unsigned sum0 = 0;
1050 unsigned sum1 = 0;
1051 unsigned sum2 = 0;
1052 unsigned sum3 = 0;
1053 while(N >= 16){
1054 sum0 += ((unsigned)z[0] + z[4] + z[8] + z[12]);
1055 sum1 += ((unsigned)z[1] + z[5] + z[9] + z[13]);
1056 sum2 += ((unsigned)z[2] + z[6] + z[10]+ z[14]);
1057 sum3 += ((unsigned)z[3] + z[7] + z[11]+ z[15]);
1058 z += 16;
1059 N -= 16;
1060 }
1061 while(N >= 4){
1062 sum0 += z[0];
1063 sum1 += z[1];
1064 sum2 += z[2];
1065 sum3 += z[3];
1066 z += 4;
1067 N -= 4;
1068 }
1069 sum3 += (sum2 << 8) + (sum1 << 16) + (sum0 << 24);
1070 switch(N){
1071 case 3: sum3 += (z[2] << 8);
1072 case 2: sum3 += (z[1] << 16);
1073 case 1: sum3 += (z[0] << 24);
1074 default: ;
1075 }
1076 return sum3;
1077 }
1078
1079 /*
1080 ** Apply a delta.
1081 **
1082 ** The output buffer should be big enough to hold the whole output
1083 ** file and a NUL terminator at the end. The delta_output_size()
1084 ** routine will determine this size for you.
1085 **
1086 ** The delta string should be null-terminated. But the delta string
1087 ** may contain embedded NUL characters (if the input and output are
1088 ** binary files) so we also have to pass in the length of the delta in
1089 ** the lenDelta parameter.
1090 **
1091 ** This function returns the size of the output file in bytes (excluding
1092 ** the final NUL terminator character). Except, if the delta string is
1093 ** malformed or intended for use with a source file other than zSrc,
1094 ** then this routine returns -1.
1095 **
1096 ** Refer to the delta_create() documentation above for a description
1097 ** of the delta file format.
1098 */
1099 static int rbuDeltaApply(
1100 const char *zSrc, /* The source or pattern file */
1101 int lenSrc, /* Length of the source file */
1102 const char *zDelta, /* Delta to apply to the pattern */
1103 int lenDelta, /* Length of the delta */
1104 char *zOut /* Write the output into this preallocated buffer */
1105 ){
1106 unsigned int limit;
1107 unsigned int total = 0;
1108 #ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST
1109 char *zOrigOut = zOut;
1110 #endif
1111
1112 limit = rbuDeltaGetInt(&zDelta, &lenDelta);
1113 if( *zDelta!='\n' ){
1114 /* ERROR: size integer not terminated by "\n" */
1115 return -1;
1116 }
1117 zDelta++; lenDelta--;
1118 while( *zDelta && lenDelta>0 ){
1119 unsigned int cnt, ofst;
1120 cnt = rbuDeltaGetInt(&zDelta, &lenDelta);
1121 switch( zDelta[0] ){
1122 case '@': {
1123 zDelta++; lenDelta--;
1124 ofst = rbuDeltaGetInt(&zDelta, &lenDelta);
1125 if( lenDelta>0 && zDelta[0]!=',' ){
1126 /* ERROR: copy command not terminated by ',' */
1127 return -1;
1128 }
1129 zDelta++; lenDelta--;
1130 total += cnt;
1131 if( total>limit ){
1132 /* ERROR: copy exceeds output file size */
1133 return -1;
1134 }
1135 if( (int)(ofst+cnt) > lenSrc ){
1136 /* ERROR: copy extends past end of input */
1137 return -1;
1138 }
1139 memcpy(zOut, &zSrc[ofst], cnt);
1140 zOut += cnt;
1141 break;
1142 }
1143 case ':': {
1144 zDelta++; lenDelta--;
1145 total += cnt;
1146 if( total>limit ){
1147 /* ERROR: insert command gives an output larger than predicted */
1148 return -1;
1149 }
1150 if( (int)cnt>lenDelta ){
1151 /* ERROR: insert count exceeds size of delta */
1152 return -1;
1153 }
1154 memcpy(zOut, zDelta, cnt);
1155 zOut += cnt;
1156 zDelta += cnt;
1157 lenDelta -= cnt;
1158 break;
1159 }
1160 case ';': {
1161 zDelta++; lenDelta--;
1162 zOut[0] = 0;
1163 #ifndef FOSSIL_OMIT_DELTA_CKSUM_TEST
1164 if( cnt!=rbuDeltaChecksum(zOrigOut, total) ){
1165 /* ERROR: bad checksum */
1166 return -1;
1167 }
1168 #endif
1169 if( total!=limit ){
1170 /* ERROR: generated size does not match predicted size */
1171 return -1;
1172 }
1173 return total;
1174 }
1175 default: {
1176 /* ERROR: unknown delta operator */
1177 return -1;
1178 }
1179 }
1180 }
1181 /* ERROR: unterminated delta */
1182 return -1;
1183 }
1184
1185 static int rbuDeltaOutputSize(const char *zDelta, int lenDelta){
1186 int size;
1187 size = rbuDeltaGetInt(&zDelta, &lenDelta);
1188 if( *zDelta!='\n' ){
1189 /* ERROR: size integer not terminated by "\n" */
1190 return -1;
1191 }
1192 return size;
1193 }
1194
1195 /*
1196 ** End of code taken from fossil.
1197 *************************************************************************/
1198
1199 /*
1200 ** Implementation of SQL scalar function rbu_fossil_delta().
1201 **
1202 ** This function applies a fossil delta patch to a blob. Exactly two
1203 ** arguments must be passed to this function. The first is the blob to
1204 ** patch and the second the patch to apply. If no error occurs, this
1205 ** function returns the patched blob.
1206 */
1207 static void rbuFossilDeltaFunc(
1208 sqlite3_context *context,
1209 int argc,
1210 sqlite3_value **argv
1211 ){
1212 const char *aDelta;
1213 int nDelta;
1214 const char *aOrig;
1215 int nOrig;
1216
1217 int nOut;
1218 int nOut2;
1219 char *aOut;
1220
1221 assert( argc==2 );
1222
1223 nOrig = sqlite3_value_bytes(argv[0]);
1224 aOrig = (const char*)sqlite3_value_blob(argv[0]);
1225 nDelta = sqlite3_value_bytes(argv[1]);
1226 aDelta = (const char*)sqlite3_value_blob(argv[1]);
1227
1228 /* Figure out the size of the output */
1229 nOut = rbuDeltaOutputSize(aDelta, nDelta);
1230 if( nOut<0 ){
1231 sqlite3_result_error(context, "corrupt fossil delta", -1);
1232 return;
1233 }
1234
1235 aOut = sqlite3_malloc(nOut+1);
1236 if( aOut==0 ){
1237 sqlite3_result_error_nomem(context);
1238 }else{
1239 nOut2 = rbuDeltaApply(aOrig, nOrig, aDelta, nDelta, aOut);
1240 if( nOut2!=nOut ){
1241 sqlite3_result_error(context, "corrupt fossil delta", -1);
1242 }else{
1243 sqlite3_result_blob(context, aOut, nOut, sqlite3_free);
1244 }
1245 }
1246 }
1247
1248
1249 /*
1250 ** Prepare the SQL statement in buffer zSql against database handle db.
1251 ** If successful, set *ppStmt to point to the new statement and return
1252 ** SQLITE_OK.
1253 **
1254 ** Otherwise, if an error does occur, set *ppStmt to NULL and return
1255 ** an SQLite error code. Additionally, set output variable *pzErrmsg to
1256 ** point to a buffer containing an error message. It is the responsibility
1257 ** of the caller to (eventually) free this buffer using sqlite3_free().
1258 */
1259 static int prepareAndCollectError(
1260 sqlite3 *db,
1261 sqlite3_stmt **ppStmt,
1262 char **pzErrmsg,
1263 const char *zSql
1264 ){
1265 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
1266 if( rc!=SQLITE_OK ){
1267 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
1268 *ppStmt = 0;
1269 }
1270 return rc;
1271 }
1272
1273 /*
1274 ** Reset the SQL statement passed as the first argument. Return a copy
1275 ** of the value returned by sqlite3_reset().
1276 **
1277 ** If an error has occurred, then set *pzErrmsg to point to a buffer
1278 ** containing an error message. It is the responsibility of the caller
1279 ** to eventually free this buffer using sqlite3_free().
1280 */
1281 static int resetAndCollectError(sqlite3_stmt *pStmt, char **pzErrmsg){
1282 int rc = sqlite3_reset(pStmt);
1283 if( rc!=SQLITE_OK ){
1284 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(sqlite3_db_handle(pStmt)));
1285 }
1286 return rc;
1287 }
1288
1289 /*
1290 ** Unless it is NULL, argument zSql points to a buffer allocated using
1291 ** sqlite3_malloc containing an SQL statement. This function prepares the SQL
1292 ** statement against database db and frees the buffer. If statement
1293 ** compilation is successful, *ppStmt is set to point to the new statement
1294 ** handle and SQLITE_OK is returned.
1295 **
1296 ** Otherwise, if an error occurs, *ppStmt is set to NULL and an error code
1297 ** returned. In this case, *pzErrmsg may also be set to point to an error
1298 ** message. It is the responsibility of the caller to free this error message
1299 ** buffer using sqlite3_free().
1300 **
1301 ** If argument zSql is NULL, this function assumes that an OOM has occurred.
1302 ** In this case SQLITE_NOMEM is returned and *ppStmt set to NULL.
1303 */
1304 static int prepareFreeAndCollectError(
1305 sqlite3 *db,
1306 sqlite3_stmt **ppStmt,
1307 char **pzErrmsg,
1308 char *zSql
1309 ){
1310 int rc;
1311 assert( *pzErrmsg==0 );
1312 if( zSql==0 ){
1313 rc = SQLITE_NOMEM;
1314 *ppStmt = 0;
1315 }else{
1316 rc = prepareAndCollectError(db, ppStmt, pzErrmsg, zSql);
1317 sqlite3_free(zSql);
1318 }
1319 return rc;
1320 }
1321
1322 /*
1323 ** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
1324 ** by an earlier call to rbuObjIterCacheTableInfo().
1325 */
1326 static void rbuObjIterFreeCols(RbuObjIter *pIter){
1327 int i;
1328 for(i=0; i<pIter->nTblCol; i++){
1329 sqlite3_free(pIter->azTblCol[i]);
1330 sqlite3_free(pIter->azTblType[i]);
1331 }
1332 sqlite3_free(pIter->azTblCol);
1333 pIter->azTblCol = 0;
1334 pIter->azTblType = 0;
1335 pIter->aiSrcOrder = 0;
1336 pIter->abTblPk = 0;
1337 pIter->abNotNull = 0;
1338 pIter->nTblCol = 0;
1339 pIter->eType = 0; /* Invalid value */
1340 }
1341
1342 /*
1343 ** Finalize all statements and free all allocations that are specific to
1344 ** the current object (table/index pair).
1345 */
1346 static void rbuObjIterClearStatements(RbuObjIter *pIter){
1347 RbuUpdateStmt *pUp;
1348
1349 sqlite3_finalize(pIter->pSelect);
1350 sqlite3_finalize(pIter->pInsert);
1351 sqlite3_finalize(pIter->pDelete);
1352 sqlite3_finalize(pIter->pTmpInsert);
1353 pUp = pIter->pRbuUpdate;
1354 while( pUp ){
1355 RbuUpdateStmt *pTmp = pUp->pNext;
1356 sqlite3_finalize(pUp->pUpdate);
1357 sqlite3_free(pUp);
1358 pUp = pTmp;
1359 }
1360
1361 pIter->pSelect = 0;
1362 pIter->pInsert = 0;
1363 pIter->pDelete = 0;
1364 pIter->pRbuUpdate = 0;
1365 pIter->pTmpInsert = 0;
1366 pIter->nCol = 0;
1367 }
1368
1369 /*
1370 ** Clean up any resources allocated as part of the iterator object passed
1371 ** as the only argument.
1372 */
1373 static void rbuObjIterFinalize(RbuObjIter *pIter){
1374 rbuObjIterClearStatements(pIter);
1375 sqlite3_finalize(pIter->pTblIter);
1376 sqlite3_finalize(pIter->pIdxIter);
1377 rbuObjIterFreeCols(pIter);
1378 memset(pIter, 0, sizeof(RbuObjIter));
1379 }
1380
1381 /*
1382 ** Advance the iterator to the next position.
1383 **
1384 ** If no error occurs, SQLITE_OK is returned and the iterator is left
1385 ** pointing to the next entry. Otherwise, an error code and message is
1386 ** left in the RBU handle passed as the first argument. A copy of the
1387 ** error code is returned.
1388 */
1389 static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
1390 int rc = p->rc;
1391 if( rc==SQLITE_OK ){
1392
1393 /* Free any SQLite statements used while processing the previous object */
1394 rbuObjIterClearStatements(pIter);
1395 if( pIter->zIdx==0 ){
1396 rc = sqlite3_exec(p->dbMain,
1397 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
1398 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
1399 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
1400 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
1401 , 0, 0, &p->zErrmsg
1402 );
1403 }
1404
1405 if( rc==SQLITE_OK ){
1406 if( pIter->bCleanup ){
1407 rbuObjIterFreeCols(pIter);
1408 pIter->bCleanup = 0;
1409 rc = sqlite3_step(pIter->pTblIter);
1410 if( rc!=SQLITE_ROW ){
1411 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
1412 pIter->zTbl = 0;
1413 }else{
1414 pIter->zTbl = (const char*)sqlite3_column_text(pIter->pTblIter, 0);
1415 pIter->zDataTbl = (const char*)sqlite3_column_text(pIter->pTblIter,1);
1416 rc = (pIter->zDataTbl && pIter->zTbl) ? SQLITE_OK : SQLITE_NOMEM;
1417 }
1418 }else{
1419 if( pIter->zIdx==0 ){
1420 sqlite3_stmt *pIdx = pIter->pIdxIter;
1421 rc = sqlite3_bind_text(pIdx, 1, pIter->zTbl, -1, SQLITE_STATIC);
1422 }
1423 if( rc==SQLITE_OK ){
1424 rc = sqlite3_step(pIter->pIdxIter);
1425 if( rc!=SQLITE_ROW ){
1426 rc = resetAndCollectError(pIter->pIdxIter, &p->zErrmsg);
1427 pIter->bCleanup = 1;
1428 pIter->zIdx = 0;
1429 }else{
1430 pIter->zIdx = (const char*)sqlite3_column_text(pIter->pIdxIter, 0);
1431 pIter->iTnum = sqlite3_column_int(pIter->pIdxIter, 1);
1432 pIter->bUnique = sqlite3_column_int(pIter->pIdxIter, 2);
1433 rc = pIter->zIdx ? SQLITE_OK : SQLITE_NOMEM;
1434 }
1435 }
1436 }
1437 }
1438 }
1439
1440 if( rc!=SQLITE_OK ){
1441 rbuObjIterFinalize(pIter);
1442 p->rc = rc;
1443 }
1444 return rc;
1445 }
1446
1447
1448 /*
1449 ** The implementation of the rbu_target_name() SQL function. This function
1450 ** accepts one or two arguments. The first argument is the name of a table -
1451 ** the name of a table in the RBU database. The second, if it is present, is 1
1452 ** for a view or 0 for a table.
1453 **
1454 ** For a non-vacuum RBU handle, if the table name matches the pattern:
1455 **
1456 ** data[0-9]_<name>
1457 **
1458 ** where <name> is any sequence of 1 or more characters, <name> is returned.
1459 ** Otherwise, if the only argument does not match the above pattern, an SQL
1460 ** NULL is returned.
1461 **
1462 ** "data_t1" -> "t1"
1463 ** "data0123_t2" -> "t2"
1464 ** "dataAB_t3" -> NULL
1465 **
1466 ** For an rbu vacuum handle, a copy of the first argument is returned if
1467 ** the second argument is either missing or 0 (not a view).
1468 */
1469 static void rbuTargetNameFunc(
1470 sqlite3_context *pCtx,
1471 int argc,
1472 sqlite3_value **argv
1473 ){
1474 sqlite3rbu *p = sqlite3_user_data(pCtx);
1475 const char *zIn;
1476 assert( argc==1 || argc==2 );
1477
1478 zIn = (const char*)sqlite3_value_text(argv[0]);
1479 if( zIn ){
1480 if( rbuIsVacuum(p) ){
1481 if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
1482 sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
1483 }
1484 }else{
1485 if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
1486 int i;
1487 for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
1488 if( zIn[i]=='_' && zIn[i+1] ){
1489 sqlite3_result_text(pCtx, &zIn[i+1], -1, SQLITE_STATIC);
1490 }
1491 }
1492 }
1493 }
1494 }
1495
1496 /*
1497 ** Initialize the iterator structure passed as the second argument.
1498 **
1499 ** If no error occurs, SQLITE_OK is returned and the iterator is left
1500 ** pointing to the first entry. Otherwise, an error code and message is
1501 ** left in the RBU handle passed as the first argument. A copy of the
1502 ** error code is returned.
1503 */
1504 static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
1505 int rc;
1506 memset(pIter, 0, sizeof(RbuObjIter));
1507
1508 rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
1509 sqlite3_mprintf(
1510 "SELECT rbu_target_name(name, type='view') AS target, name "
1511 "FROM sqlite_master "
1512 "WHERE type IN ('table', 'view') AND target IS NOT NULL "
1513 " %s "
1514 "ORDER BY name"
1515 , rbuIsVacuum(p) ? "AND rootpage!=0 AND rootpage IS NOT NULL" : ""));
1516
1517 if( rc==SQLITE_OK ){
1518 rc = prepareAndCollectError(p->dbMain, &pIter->pIdxIter, &p->zErrmsg,
1519 "SELECT name, rootpage, sql IS NULL OR substr(8, 6)=='UNIQUE' "
1520 " FROM main.sqlite_master "
1521 " WHERE type='index' AND tbl_name = ?"
1522 );
1523 }
1524
1525 pIter->bCleanup = 1;
1526 p->rc = rc;
1527 return rbuObjIterNext(p, pIter);
1528 }
1529
1530 /*
1531 ** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
1532 ** an error code is stored in the RBU handle passed as the first argument.
1533 **
1534 ** If an error has already occurred (p->rc is already set to something other
1535 ** than SQLITE_OK), then this function returns NULL without modifying the
1536 ** stored error code. In this case it still calls sqlite3_free() on any
1537 ** printf() parameters associated with %z conversions.
1538 */
1539 static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
1540 char *zSql = 0;
1541 va_list ap;
1542 va_start(ap, zFmt);
1543 zSql = sqlite3_vmprintf(zFmt, ap);
1544 if( p->rc==SQLITE_OK ){
1545 if( zSql==0 ) p->rc = SQLITE_NOMEM;
1546 }else{
1547 sqlite3_free(zSql);
1548 zSql = 0;
1549 }
1550 va_end(ap);
1551 return zSql;
1552 }
1553
1554 /*
1555 ** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
1556 ** arguments are the usual subsitution values. This function performs
1557 ** the printf() style substitutions and executes the result as an SQL
1558 ** statement on the RBU handles database.
1559 **
1560 ** If an error occurs, an error code and error message is stored in the
1561 ** RBU handle. If an error has already occurred when this function is
1562 ** called, it is a no-op.
1563 */
1564 static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
1565 va_list ap;
1566 char *zSql;
1567 va_start(ap, zFmt);
1568 zSql = sqlite3_vmprintf(zFmt, ap);
1569 if( p->rc==SQLITE_OK ){
1570 if( zSql==0 ){
1571 p->rc = SQLITE_NOMEM;
1572 }else{
1573 p->rc = sqlite3_exec(db, zSql, 0, 0, &p->zErrmsg);
1574 }
1575 }
1576 sqlite3_free(zSql);
1577 va_end(ap);
1578 return p->rc;
1579 }
1580
1581 /*
1582 ** Attempt to allocate and return a pointer to a zeroed block of nByte
1583 ** bytes.
1584 **
1585 ** If an error (i.e. an OOM condition) occurs, return NULL and leave an
1586 ** error code in the rbu handle passed as the first argument. Or, if an
1587 ** error has already occurred when this function is called, return NULL
1588 ** immediately without attempting the allocation or modifying the stored
1589 ** error code.
1590 */
1591 static void *rbuMalloc(sqlite3rbu *p, int nByte){
1592 void *pRet = 0;
1593 if( p->rc==SQLITE_OK ){
1594 assert( nByte>0 );
1595 pRet = sqlite3_malloc64(nByte);
1596 if( pRet==0 ){
1597 p->rc = SQLITE_NOMEM;
1598 }else{
1599 memset(pRet, 0, nByte);
1600 }
1601 }
1602 return pRet;
1603 }
1604
1605
1606 /*
1607 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
1608 ** there is room for at least nCol elements. If an OOM occurs, store an
1609 ** error code in the RBU handle passed as the first argument.
1610 */
1611 static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
1612 int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
1613 char **azNew;
1614
1615 azNew = (char**)rbuMalloc(p, nByte);
1616 if( azNew ){
1617 pIter->azTblCol = azNew;
1618 pIter->azTblType = &azNew[nCol];
1619 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
1620 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
1621 pIter->abNotNull = (u8*)&pIter->abTblPk[nCol];
1622 pIter->abIndexed = (u8*)&pIter->abNotNull[nCol];
1623 }
1624 }
1625
1626 /*
1627 ** The first argument must be a nul-terminated string. This function
1628 ** returns a copy of the string in memory obtained from sqlite3_malloc().
1629 ** It is the responsibility of the caller to eventually free this memory
1630 ** using sqlite3_free().
1631 **
1632 ** If an OOM condition is encountered when attempting to allocate memory,
1633 ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
1634 ** if the allocation succeeds, (*pRc) is left unchanged.
1635 */
1636 static char *rbuStrndup(const char *zStr, int *pRc){
1637 char *zRet = 0;
1638
1639 assert( *pRc==SQLITE_OK );
1640 if( zStr ){
1641 size_t nCopy = strlen(zStr) + 1;
1642 zRet = (char*)sqlite3_malloc64(nCopy);
1643 if( zRet ){
1644 memcpy(zRet, zStr, nCopy);
1645 }else{
1646 *pRc = SQLITE_NOMEM;
1647 }
1648 }
1649
1650 return zRet;
1651 }
1652
1653 /*
1654 ** Finalize the statement passed as the second argument.
1655 **
1656 ** If the sqlite3_finalize() call indicates that an error occurs, and the
1657 ** rbu handle error code is not already set, set the error code and error
1658 ** message accordingly.
1659 */
1660 static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
1661 sqlite3 *db = sqlite3_db_handle(pStmt);
1662 int rc = sqlite3_finalize(pStmt);
1663 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
1664 p->rc = rc;
1665 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
1666 }
1667 }
1668
1669 /* Determine the type of a table.
1670 **
1671 ** peType is of type (int*), a pointer to an output parameter of type
1672 ** (int). This call sets the output parameter as follows, depending
1673 ** on the type of the table specified by parameters dbName and zTbl.
1674 **
1675 ** RBU_PK_NOTABLE: No such table.
1676 ** RBU_PK_NONE: Table has an implicit rowid.
1677 ** RBU_PK_IPK: Table has an explicit IPK column.
1678 ** RBU_PK_EXTERNAL: Table has an external PK index.
1679 ** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
1680 ** RBU_PK_VTAB: Table is a virtual table.
1681 **
1682 ** Argument *piPk is also of type (int*), and also points to an output
1683 ** parameter. Unless the table has an external primary key index
1684 ** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
1685 ** if the table does have an external primary key index, then *piPk
1686 ** is set to the root page number of the primary key index before
1687 ** returning.
1688 **
1689 ** ALGORITHM:
1690 **
1691 ** if( no entry exists in sqlite_master ){
1692 ** return RBU_PK_NOTABLE
1693 ** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
1694 ** return RBU_PK_VTAB
1695 ** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
1696 ** if( the index that is the pk exists in sqlite_master ){
1697 ** *piPK = rootpage of that index.
1698 ** return RBU_PK_EXTERNAL
1699 ** }else{
1700 ** return RBU_PK_WITHOUT_ROWID
1701 ** }
1702 ** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
1703 ** return RBU_PK_IPK
1704 ** }else{
1705 ** return RBU_PK_NONE
1706 ** }
1707 */
1708 static void rbuTableType(
1709 sqlite3rbu *p,
1710 const char *zTab,
1711 int *peType,
1712 int *piTnum,
1713 int *piPk
1714 ){
1715 /*
1716 ** 0) SELECT count(*) FROM sqlite_master where name=%Q AND IsVirtual(%Q)
1717 ** 1) PRAGMA index_list = ?
1718 ** 2) SELECT count(*) FROM sqlite_master where name=%Q
1719 ** 3) PRAGMA table_info = ?
1720 */
1721 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
1722
1723 *peType = RBU_PK_NOTABLE;
1724 *piPk = 0;
1725
1726 assert( p->rc==SQLITE_OK );
1727 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
1728 sqlite3_mprintf(
1729 "SELECT (sql LIKE 'create virtual%%'), rootpage"
1730 " FROM sqlite_master"
1731 " WHERE name=%Q", zTab
1732 ));
1733 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
1734 /* Either an error, or no such table. */
1735 goto rbuTableType_end;
1736 }
1737 if( sqlite3_column_int(aStmt[0], 0) ){
1738 *peType = RBU_PK_VTAB; /* virtual table */
1739 goto rbuTableType_end;
1740 }
1741 *piTnum = sqlite3_column_int(aStmt[0], 1);
1742
1743 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
1744 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
1745 );
1746 if( p->rc ) goto rbuTableType_end;
1747 while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
1748 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
1749 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
1750 if( zOrig && zIdx && zOrig[0]=='p' ){
1751 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
1752 sqlite3_mprintf(
1753 "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
1754 ));
1755 if( p->rc==SQLITE_OK ){
1756 if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
1757 *piPk = sqlite3_column_int(aStmt[2], 0);
1758 *peType = RBU_PK_EXTERNAL;
1759 }else{
1760 *peType = RBU_PK_WITHOUT_ROWID;
1761 }
1762 }
1763 goto rbuTableType_end;
1764 }
1765 }
1766
1767 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
1768 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
1769 );
1770 if( p->rc==SQLITE_OK ){
1771 while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
1772 if( sqlite3_column_int(aStmt[3],5)>0 ){
1773 *peType = RBU_PK_IPK; /* explicit IPK column */
1774 goto rbuTableType_end;
1775 }
1776 }
1777 *peType = RBU_PK_NONE;
1778 }
1779
1780 rbuTableType_end: {
1781 unsigned int i;
1782 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
1783 rbuFinalize(p, aStmt[i]);
1784 }
1785 }
1786 }
1787
1788 /*
1789 ** This is a helper function for rbuObjIterCacheTableInfo(). It populates
1790 ** the pIter->abIndexed[] array.
1791 */
1792 static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
1793 sqlite3_stmt *pList = 0;
1794 int bIndex = 0;
1795
1796 if( p->rc==SQLITE_OK ){
1797 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
1798 p->rc = prepareFreeAndCollectError(p->dbMain, &pList, &p->zErrmsg,
1799 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
1800 );
1801 }
1802
1803 pIter->nIndex = 0;
1804 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pList) ){
1805 const char *zIdx = (const char*)sqlite3_column_text(pList, 1);
1806 sqlite3_stmt *pXInfo = 0;
1807 if( zIdx==0 ) break;
1808 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
1809 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
1810 );
1811 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
1812 int iCid = sqlite3_column_int(pXInfo, 1);
1813 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
1814 }
1815 rbuFinalize(p, pXInfo);
1816 bIndex = 1;
1817 pIter->nIndex++;
1818 }
1819
1820 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
1821 /* "PRAGMA index_list" includes the main PK b-tree */
1822 pIter->nIndex--;
1823 }
1824
1825 rbuFinalize(p, pList);
1826 if( bIndex==0 ) pIter->abIndexed = 0;
1827 }
1828
1829
1830 /*
1831 ** If they are not already populated, populate the pIter->azTblCol[],
1832 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
1833 ** the table (not index) that the iterator currently points to.
1834 **
1835 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
1836 ** an error does occur, an error code and error message are also left in
1837 ** the RBU handle.
1838 */
1839 static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
1840 if( pIter->azTblCol==0 ){
1841 sqlite3_stmt *pStmt = 0;
1842 int nCol = 0;
1843 int i; /* for() loop iterator variable */
1844 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
1845 int iOrder = 0;
1846 int iTnum = 0;
1847
1848 /* Figure out the type of table this step will deal with. */
1849 assert( pIter->eType==0 );
1850 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
1851 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
1852 p->rc = SQLITE_ERROR;
1853 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
1854 }
1855 if( p->rc ) return p->rc;
1856 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
1857
1858 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
1859 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
1860 || pIter->eType==RBU_PK_VTAB
1861 );
1862
1863 /* Populate the azTblCol[] and nTblCol variables based on the columns
1864 ** of the input table. Ignore any input table columns that begin with
1865 ** "rbu_". */
1866 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
1867 sqlite3_mprintf("SELECT * FROM '%q'", pIter->zDataTbl)
1868 );
1869 if( p->rc==SQLITE_OK ){
1870 nCol = sqlite3_column_count(pStmt);
1871 rbuAllocateIterArrays(p, pIter, nCol);
1872 }
1873 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
1874 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
1875 if( sqlite3_strnicmp("rbu_", zName, 4) ){
1876 char *zCopy = rbuStrndup(zName, &p->rc);
1877 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
1878 pIter->azTblCol[pIter->nTblCol++] = zCopy;
1879 }
1880 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
1881 bRbuRowid = 1;
1882 }
1883 }
1884 sqlite3_finalize(pStmt);
1885 pStmt = 0;
1886
1887 if( p->rc==SQLITE_OK
1888 && rbuIsVacuum(p)==0
1889 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
1890 ){
1891 p->rc = SQLITE_ERROR;
1892 p->zErrmsg = sqlite3_mprintf(
1893 "table %q %s rbu_rowid column", pIter->zDataTbl,
1894 (bRbuRowid ? "may not have" : "requires")
1895 );
1896 }
1897
1898 /* Check that all non-HIDDEN columns in the destination table are also
1899 ** present in the input table. Populate the abTblPk[], azTblType[] and
1900 ** aiTblOrder[] arrays at the same time. */
1901 if( p->rc==SQLITE_OK ){
1902 p->rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
1903 sqlite3_mprintf("PRAGMA table_info(%Q)", pIter->zTbl)
1904 );
1905 }
1906 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
1907 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
1908 if( zName==0 ) break; /* An OOM - finalize() below returns S_NOMEM */
1909 for(i=iOrder; i<pIter->nTblCol; i++){
1910 if( 0==strcmp(zName, pIter->azTblCol[i]) ) break;
1911 }
1912 if( i==pIter->nTblCol ){
1913 p->rc = SQLITE_ERROR;
1914 p->zErrmsg = sqlite3_mprintf("column missing from %q: %s",
1915 pIter->zDataTbl, zName
1916 );
1917 }else{
1918 int iPk = sqlite3_column_int(pStmt, 5);
1919 int bNotNull = sqlite3_column_int(pStmt, 3);
1920 const char *zType = (const char*)sqlite3_column_text(pStmt, 2);
1921
1922 if( i!=iOrder ){
1923 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
1924 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
1925 }
1926
1927 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
1928 pIter->abTblPk[iOrder] = (iPk!=0);
1929 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
1930 iOrder++;
1931 }
1932 }
1933
1934 rbuFinalize(p, pStmt);
1935 rbuObjIterCacheIndexedCols(p, pIter);
1936 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
1937 assert( pIter->eType!=RBU_PK_VTAB || pIter->nIndex==0 );
1938 }
1939
1940 return p->rc;
1941 }
1942
1943 /*
1944 ** This function constructs and returns a pointer to a nul-terminated
1945 ** string containing some SQL clause or list based on one or more of the
1946 ** column names currently stored in the pIter->azTblCol[] array.
1947 */
1948 static char *rbuObjIterGetCollist(
1949 sqlite3rbu *p, /* RBU object */
1950 RbuObjIter *pIter /* Object iterator for column names */
1951 ){
1952 char *zList = 0;
1953 const char *zSep = "";
1954 int i;
1955 for(i=0; i<pIter->nTblCol; i++){
1956 const char *z = pIter->azTblCol[i];
1957 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
1958 zSep = ", ";
1959 }
1960 return zList;
1961 }
1962
1963 /*
1964 ** This function is used to create a SELECT list (the list of SQL
1965 ** expressions that follows a SELECT keyword) for a SELECT statement
1966 ** used to read from an data_xxx or rbu_tmp_xxx table while updating the
1967 ** index object currently indicated by the iterator object passed as the
1968 ** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
1969 ** to obtain the required information.
1970 **
1971 ** If the index is of the following form:
1972 **
1973 ** CREATE INDEX i1 ON t1(c, b COLLATE nocase);
1974 **
1975 ** and "t1" is a table with an explicit INTEGER PRIMARY KEY column
1976 ** "ipk", the returned string is:
1977 **
1978 ** "`c` COLLATE 'BINARY', `b` COLLATE 'NOCASE', `ipk` COLLATE 'BINARY'"
1979 **
1980 ** As well as the returned string, three other malloc'd strings are
1981 ** returned via output parameters. As follows:
1982 **
1983 ** pzImposterCols: ...
1984 ** pzImposterPk: ...
1985 ** pzWhere: ...
1986 */
1987 static char *rbuObjIterGetIndexCols(
1988 sqlite3rbu *p, /* RBU object */
1989 RbuObjIter *pIter, /* Object iterator for column names */
1990 char **pzImposterCols, /* OUT: Columns for imposter table */
1991 char **pzImposterPk, /* OUT: Imposter PK clause */
1992 char **pzWhere, /* OUT: WHERE clause */
1993 int *pnBind /* OUT: Trbul number of columns */
1994 ){
1995 int rc = p->rc; /* Error code */
1996 int rc2; /* sqlite3_finalize() return code */
1997 char *zRet = 0; /* String to return */
1998 char *zImpCols = 0; /* String to return via *pzImposterCols */
1999 char *zImpPK = 0; /* String to return via *pzImposterPK */
2000 char *zWhere = 0; /* String to return via *pzWhere */
2001 int nBind = 0; /* Value to return via *pnBind */
2002 const char *zCom = ""; /* Set to ", " later on */
2003 const char *zAnd = ""; /* Set to " AND " later on */
2004 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = ? */
2005
2006 if( rc==SQLITE_OK ){
2007 assert( p->zErrmsg==0 );
2008 rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
2009 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
2010 );
2011 }
2012
2013 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
2014 int iCid = sqlite3_column_int(pXInfo, 1);
2015 int bDesc = sqlite3_column_int(pXInfo, 3);
2016 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
2017 const char *zCol;
2018 const char *zType;
2019
2020 if( iCid<0 ){
2021 /* An integer primary key. If the table has an explicit IPK, use
2022 ** its name. Otherwise, use "rbu_rowid". */
2023 if( pIter->eType==RBU_PK_IPK ){
2024 int i;
2025 for(i=0; pIter->abTblPk[i]==0; i++);
2026 assert( i<pIter->nTblCol );
2027 zCol = pIter->azTblCol[i];
2028 }else if( rbuIsVacuum(p) ){
2029 zCol = "_rowid_";
2030 }else{
2031 zCol = "rbu_rowid";
2032 }
2033 zType = "INTEGER";
2034 }else{
2035 zCol = pIter->azTblCol[iCid];
2036 zType = pIter->azTblType[iCid];
2037 }
2038
2039 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
2040 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
2041 const char *zOrder = (bDesc ? " DESC" : "");
2042 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
2043 zImpPK, zCom, nBind, zCol, zOrder
2044 );
2045 }
2046 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
2047 zImpCols, zCom, nBind, zCol, zType, zCollate
2048 );
2049 zWhere = sqlite3_mprintf(
2050 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
2051 );
2052 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
2053 zCom = ", ";
2054 zAnd = " AND ";
2055 nBind++;
2056 }
2057
2058 rc2 = sqlite3_finalize(pXInfo);
2059 if( rc==SQLITE_OK ) rc = rc2;
2060
2061 if( rc!=SQLITE_OK ){
2062 sqlite3_free(zRet);
2063 sqlite3_free(zImpCols);
2064 sqlite3_free(zImpPK);
2065 sqlite3_free(zWhere);
2066 zRet = 0;
2067 zImpCols = 0;
2068 zImpPK = 0;
2069 zWhere = 0;
2070 p->rc = rc;
2071 }
2072
2073 *pzImposterCols = zImpCols;
2074 *pzImposterPk = zImpPK;
2075 *pzWhere = zWhere;
2076 *pnBind = nBind;
2077 return zRet;
2078 }
2079
2080 /*
2081 ** Assuming the current table columns are "a", "b" and "c", and the zObj
2082 ** paramter is passed "old", return a string of the form:
2083 **
2084 ** "old.a, old.b, old.b"
2085 **
2086 ** With the column names escaped.
2087 **
2088 ** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
2089 ** the text ", old._rowid_" to the returned value.
2090 */
2091 static char *rbuObjIterGetOldlist(
2092 sqlite3rbu *p,
2093 RbuObjIter *pIter,
2094 const char *zObj
2095 ){
2096 char *zList = 0;
2097 if( p->rc==SQLITE_OK && pIter->abIndexed ){
2098 const char *zS = "";
2099 int i;
2100 for(i=0; i<pIter->nTblCol; i++){
2101 if( pIter->abIndexed[i] ){
2102 const char *zCol = pIter->azTblCol[i];
2103 zList = sqlite3_mprintf("%z%s%s.\"%w\"", zList, zS, zObj, zCol);
2104 }else{
2105 zList = sqlite3_mprintf("%z%sNULL", zList, zS);
2106 }
2107 zS = ", ";
2108 if( zList==0 ){
2109 p->rc = SQLITE_NOMEM;
2110 break;
2111 }
2112 }
2113
2114 /* For a table with implicit rowids, append "old._rowid_" to the list. */
2115 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
2116 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
2117 }
2118 }
2119 return zList;
2120 }
2121
2122 /*
2123 ** Return an expression that can be used in a WHERE clause to match the
2124 ** primary key of the current table. For example, if the table is:
2125 **
2126 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c));
2127 **
2128 ** Return the string:
2129 **
2130 ** "b = ?1 AND c = ?2"
2131 */
2132 static char *rbuObjIterGetWhere(
2133 sqlite3rbu *p,
2134 RbuObjIter *pIter
2135 ){
2136 char *zList = 0;
2137 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
2138 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
2139 }else if( pIter->eType==RBU_PK_EXTERNAL ){
2140 const char *zSep = "";
2141 int i;
2142 for(i=0; i<pIter->nTblCol; i++){
2143 if( pIter->abTblPk[i] ){
2144 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
2145 zSep = " AND ";
2146 }
2147 }
2148 zList = rbuMPrintf(p,
2149 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
2150 );
2151
2152 }else{
2153 const char *zSep = "";
2154 int i;
2155 for(i=0; i<pIter->nTblCol; i++){
2156 if( pIter->abTblPk[i] ){
2157 const char *zCol = pIter->azTblCol[i];
2158 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
2159 zSep = " AND ";
2160 }
2161 }
2162 }
2163 return zList;
2164 }
2165
2166 /*
2167 ** The SELECT statement iterating through the keys for the current object
2168 ** (p->objiter.pSelect) currently points to a valid row. However, there
2169 ** is something wrong with the rbu_control value in the rbu_control value
2170 ** stored in the (p->nCol+1)'th column. Set the error code and error message
2171 ** of the RBU handle to something reflecting this.
2172 */
2173 static void rbuBadControlError(sqlite3rbu *p){
2174 p->rc = SQLITE_ERROR;
2175 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
2176 }
2177
2178
2179 /*
2180 ** Return a nul-terminated string containing the comma separated list of
2181 ** assignments that should be included following the "SET" keyword of
2182 ** an UPDATE statement used to update the table object that the iterator
2183 ** passed as the second argument currently points to if the rbu_control
2184 ** column of the data_xxx table entry is set to zMask.
2185 **
2186 ** The memory for the returned string is obtained from sqlite3_malloc().
2187 ** It is the responsibility of the caller to eventually free it using
2188 ** sqlite3_free().
2189 **
2190 ** If an OOM error is encountered when allocating space for the new
2191 ** string, an error code is left in the rbu handle passed as the first
2192 ** argument and NULL is returned. Or, if an error has already occurred
2193 ** when this function is called, NULL is returned immediately, without
2194 ** attempting the allocation or modifying the stored error code.
2195 */
2196 static char *rbuObjIterGetSetlist(
2197 sqlite3rbu *p,
2198 RbuObjIter *pIter,
2199 const char *zMask
2200 ){
2201 char *zList = 0;
2202 if( p->rc==SQLITE_OK ){
2203 int i;
2204
2205 if( (int)strlen(zMask)!=pIter->nTblCol ){
2206 rbuBadControlError(p);
2207 }else{
2208 const char *zSep = "";
2209 for(i=0; i<pIter->nTblCol; i++){
2210 char c = zMask[pIter->aiSrcOrder[i]];
2211 if( c=='x' ){
2212 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
2213 zList, zSep, pIter->azTblCol[i], i+1
2214 );
2215 zSep = ", ";
2216 }
2217 else if( c=='d' ){
2218 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
2219 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
2220 );
2221 zSep = ", ";
2222 }
2223 else if( c=='f' ){
2224 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_fossil_delta(\"%w\", ?%d)",
2225 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
2226 );
2227 zSep = ", ";
2228 }
2229 }
2230 }
2231 }
2232 return zList;
2233 }
2234
2235 /*
2236 ** Return a nul-terminated string consisting of nByte comma separated
2237 ** "?" expressions. For example, if nByte is 3, return a pointer to
2238 ** a buffer containing the string "?,?,?".
2239 **
2240 ** The memory for the returned string is obtained from sqlite3_malloc().
2241 ** It is the responsibility of the caller to eventually free it using
2242 ** sqlite3_free().
2243 **
2244 ** If an OOM error is encountered when allocating space for the new
2245 ** string, an error code is left in the rbu handle passed as the first
2246 ** argument and NULL is returned. Or, if an error has already occurred
2247 ** when this function is called, NULL is returned immediately, without
2248 ** attempting the allocation or modifying the stored error code.
2249 */
2250 static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
2251 char *zRet = 0;
2252 int nByte = nBind*2 + 1;
2253
2254 zRet = (char*)rbuMalloc(p, nByte);
2255 if( zRet ){
2256 int i;
2257 for(i=0; i<nBind; i++){
2258 zRet[i*2] = '?';
2259 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
2260 }
2261 }
2262 return zRet;
2263 }
2264
2265 /*
2266 ** The iterator currently points to a table (not index) of type
2267 ** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
2268 ** declaration for the corresponding imposter table. For example,
2269 ** if the iterator points to a table created as:
2270 **
2271 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
2272 **
2273 ** this function returns:
2274 **
2275 ** PRIMARY KEY("b", "a" DESC)
2276 */
2277 static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
2278 char *z = 0;
2279 assert( pIter->zIdx==0 );
2280 if( p->rc==SQLITE_OK ){
2281 const char *zSep = "PRIMARY KEY(";
2282 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
2283 sqlite3_stmt *pXInfo = 0; /* PRAGMA index_xinfo = <pk-index> */
2284
2285 p->rc = prepareFreeAndCollectError(p->dbMain, &pXList, &p->zErrmsg,
2286 sqlite3_mprintf("PRAGMA main.index_list = %Q", pIter->zTbl)
2287 );
2288 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXList) ){
2289 const char *zOrig = (const char*)sqlite3_column_text(pXList,3);
2290 if( zOrig && strcmp(zOrig, "pk")==0 ){
2291 const char *zIdx = (const char*)sqlite3_column_text(pXList,1);
2292 if( zIdx ){
2293 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
2294 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
2295 );
2296 }
2297 break;
2298 }
2299 }
2300 rbuFinalize(p, pXList);
2301
2302 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
2303 if( sqlite3_column_int(pXInfo, 5) ){
2304 /* int iCid = sqlite3_column_int(pXInfo, 0); */
2305 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
2306 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
2307 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
2308 zSep = ", ";
2309 }
2310 }
2311 z = rbuMPrintf(p, "%z)", z);
2312 rbuFinalize(p, pXInfo);
2313 }
2314 return z;
2315 }
2316
2317 /*
2318 ** This function creates the second imposter table used when writing to
2319 ** a table b-tree where the table has an external primary key. If the
2320 ** iterator passed as the second argument does not currently point to
2321 ** a table (not index) with an external primary key, this function is a
2322 ** no-op.
2323 **
2324 ** Assuming the iterator does point to a table with an external PK, this
2325 ** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
2326 ** used to access that PK index. For example, if the target table is
2327 ** declared as follows:
2328 **
2329 ** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
2330 **
2331 ** then the imposter table schema is:
2332 **
2333 ** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
2334 **
2335 */
2336 static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
2337 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
2338 int tnum = pIter->iPkTnum; /* Root page of PK index */
2339 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
2340 const char *zIdx = 0; /* Name of PK index */
2341 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
2342 const char *zComma = "";
2343 char *zCols = 0; /* Used to build up list of table cols */
2344 char *zPk = 0; /* Used to build up table PK declaration */
2345
2346 /* Figure out the name of the primary key index for the current table.
2347 ** This is needed for the argument to "PRAGMA index_xinfo". Set
2348 ** zIdx to point to a nul-terminated string containing this name. */
2349 p->rc = prepareAndCollectError(p->dbMain, &pQuery, &p->zErrmsg,
2350 "SELECT name FROM sqlite_master WHERE rootpage = ?"
2351 );
2352 if( p->rc==SQLITE_OK ){
2353 sqlite3_bind_int(pQuery, 1, tnum);
2354 if( SQLITE_ROW==sqlite3_step(pQuery) ){
2355 zIdx = (const char*)sqlite3_column_text(pQuery, 0);
2356 }
2357 }
2358 if( zIdx ){
2359 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
2360 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
2361 );
2362 }
2363 rbuFinalize(p, pQuery);
2364
2365 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
2366 int bKey = sqlite3_column_int(pXInfo, 5);
2367 if( bKey ){
2368 int iCid = sqlite3_column_int(pXInfo, 1);
2369 int bDesc = sqlite3_column_int(pXInfo, 3);
2370 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
2371 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
2372 iCid, pIter->azTblType[iCid], zCollate
2373 );
2374 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
2375 zComma = ", ";
2376 }
2377 }
2378 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
2379 rbuFinalize(p, pXInfo);
2380
2381 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
2382 rbuMPrintfExec(p, p->dbMain,
2383 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
2384 zCols, zPk
2385 );
2386 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
2387 }
2388 }
2389
2390 /*
2391 ** If an error has already occurred when this function is called, it
2392 ** immediately returns zero (without doing any work). Or, if an error
2393 ** occurs during the execution of this function, it sets the error code
2394 ** in the sqlite3rbu object indicated by the first argument and returns
2395 ** zero.
2396 **
2397 ** The iterator passed as the second argument is guaranteed to point to
2398 ** a table (not an index) when this function is called. This function
2399 ** attempts to create any imposter table required to write to the main
2400 ** table b-tree of the table before returning. Non-zero is returned if
2401 ** an imposter table are created, or zero otherwise.
2402 **
2403 ** An imposter table is required in all cases except RBU_PK_VTAB. Only
2404 ** virtual tables are written to directly. The imposter table has the
2405 ** same schema as the actual target table (less any UNIQUE constraints).
2406 ** More precisely, the "same schema" means the same columns, types,
2407 ** collation sequences. For tables that do not have an external PRIMARY
2408 ** KEY, it also means the same PRIMARY KEY declaration.
2409 */
2410 static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
2411 if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
2412 int tnum = pIter->iTnum;
2413 const char *zComma = "";
2414 char *zSql = 0;
2415 int iCol;
2416 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
2417
2418 for(iCol=0; p->rc==SQLITE_OK && iCol<pIter->nTblCol; iCol++){
2419 const char *zPk = "";
2420 const char *zCol = pIter->azTblCol[iCol];
2421 const char *zColl = 0;
2422
2423 p->rc = sqlite3_table_column_metadata(
2424 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
2425 );
2426
2427 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
2428 /* If the target table column is an "INTEGER PRIMARY KEY", add
2429 ** "PRIMARY KEY" to the imposter table column declaration. */
2430 zPk = "PRIMARY KEY ";
2431 }
2432 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
2433 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
2434 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
2435 );
2436 zComma = ", ";
2437 }
2438
2439 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
2440 char *zPk = rbuWithoutRowidPK(p, pIter);
2441 if( zPk ){
2442 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
2443 }
2444 }
2445
2446 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
2447 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
2448 pIter->zTbl, zSql,
2449 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
2450 );
2451 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
2452 }
2453 }
2454
2455 /*
2456 ** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
2457 ** Specifically a statement of the form:
2458 **
2459 ** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
2460 **
2461 ** The number of bound variables is equal to the number of columns in
2462 ** the target table, plus one (for the rbu_control column), plus one more
2463 ** (for the rbu_rowid column) if the target table is an implicit IPK or
2464 ** virtual table.
2465 */
2466 static void rbuObjIterPrepareTmpInsert(
2467 sqlite3rbu *p,
2468 RbuObjIter *pIter,
2469 const char *zCollist,
2470 const char *zRbuRowid
2471 ){
2472 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
2473 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
2474 if( zBind ){
2475 assert( pIter->pTmpInsert==0 );
2476 p->rc = prepareFreeAndCollectError(
2477 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
2478 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
2479 p->zStateDb, pIter->zDataTbl, zCollist, zRbuRowid, zBind
2480 ));
2481 }
2482 }
2483
2484 static void rbuTmpInsertFunc(
2485 sqlite3_context *pCtx,
2486 int nVal,
2487 sqlite3_value **apVal
2488 ){
2489 sqlite3rbu *p = sqlite3_user_data(pCtx);
2490 int rc = SQLITE_OK;
2491 int i;
2492
2493 assert( sqlite3_value_int(apVal[0])!=0
2494 || p->objiter.eType==RBU_PK_EXTERNAL
2495 || p->objiter.eType==RBU_PK_NONE
2496 );
2497 if( sqlite3_value_int(apVal[0])!=0 ){
2498 p->nPhaseOneStep += p->objiter.nIndex;
2499 }
2500
2501 for(i=0; rc==SQLITE_OK && i<nVal; i++){
2502 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
2503 }
2504 if( rc==SQLITE_OK ){
2505 sqlite3_step(p->objiter.pTmpInsert);
2506 rc = sqlite3_reset(p->objiter.pTmpInsert);
2507 }
2508
2509 if( rc!=SQLITE_OK ){
2510 sqlite3_result_error_code(pCtx, rc);
2511 }
2512 }
2513
2514 /*
2515 ** Ensure that the SQLite statement handles required to update the
2516 ** target database object currently indicated by the iterator passed
2517 ** as the second argument are available.
2518 */
2519 static int rbuObjIterPrepareAll(
2520 sqlite3rbu *p,
2521 RbuObjIter *pIter,
2522 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
2523 ){
2524 assert( pIter->bCleanup==0 );
2525 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
2526 const int tnum = pIter->iTnum;
2527 char *zCollist = 0; /* List of indexed columns */
2528 char **pz = &p->zErrmsg;
2529 const char *zIdx = pIter->zIdx;
2530 char *zLimit = 0;
2531
2532 if( nOffset ){
2533 zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
2534 if( !zLimit ) p->rc = SQLITE_NOMEM;
2535 }
2536
2537 if( zIdx ){
2538 const char *zTbl = pIter->zTbl;
2539 char *zImposterCols = 0; /* Columns for imposter table */
2540 char *zImposterPK = 0; /* Primary key declaration for imposter */
2541 char *zWhere = 0; /* WHERE clause on PK columns */
2542 char *zBind = 0;
2543 int nBind = 0;
2544
2545 assert( pIter->eType!=RBU_PK_VTAB );
2546 zCollist = rbuObjIterGetIndexCols(
2547 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
2548 );
2549 zBind = rbuObjIterGetBindlist(p, nBind);
2550
2551 /* Create the imposter table used to write to this index. */
2552 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
2553 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
2554 rbuMPrintfExec(p, p->dbMain,
2555 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
2556 zTbl, zImposterCols, zImposterPK
2557 );
2558 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
2559
2560 /* Create the statement to insert index entries */
2561 pIter->nCol = nBind;
2562 if( p->rc==SQLITE_OK ){
2563 p->rc = prepareFreeAndCollectError(
2564 p->dbMain, &pIter->pInsert, &p->zErrmsg,
2565 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
2566 );
2567 }
2568
2569 /* And to delete index entries */
2570 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
2571 p->rc = prepareFreeAndCollectError(
2572 p->dbMain, &pIter->pDelete, &p->zErrmsg,
2573 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
2574 );
2575 }
2576
2577 /* Create the SELECT statement to read keys in sorted order */
2578 if( p->rc==SQLITE_OK ){
2579 char *zSql;
2580 if( rbuIsVacuum(p) ){
2581 zSql = sqlite3_mprintf(
2582 "SELECT %s, 0 AS rbu_control FROM '%q' ORDER BY %s%s",
2583 zCollist,
2584 pIter->zDataTbl,
2585 zCollist, zLimit
2586 );
2587 }else
2588
2589 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
2590 zSql = sqlite3_mprintf(
2591 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
2592 zCollist, p->zStateDb, pIter->zDataTbl,
2593 zCollist, zLimit
2594 );
2595 }else{
2596 zSql = sqlite3_mprintf(
2597 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
2598 "UNION ALL "
2599 "SELECT %s, rbu_control FROM '%q' "
2600 "WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
2601 "ORDER BY %s%s",
2602 zCollist, p->zStateDb, pIter->zDataTbl,
2603 zCollist, pIter->zDataTbl,
2604 zCollist, zLimit
2605 );
2606 }
2607 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
2608 }
2609
2610 sqlite3_free(zImposterCols);
2611 sqlite3_free(zImposterPK);
2612 sqlite3_free(zWhere);
2613 sqlite3_free(zBind);
2614 }else{
2615 int bRbuRowid = (pIter->eType==RBU_PK_VTAB)
2616 ||(pIter->eType==RBU_PK_NONE)
2617 ||(pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p));
2618 const char *zTbl = pIter->zTbl; /* Table this step applies to */
2619 const char *zWrite; /* Imposter table name */
2620
2621 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
2622 char *zWhere = rbuObjIterGetWhere(p, pIter);
2623 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
2624 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
2625
2626 zCollist = rbuObjIterGetCollist(p, pIter);
2627 pIter->nCol = pIter->nTblCol;
2628
2629 /* Create the imposter table or tables (if required). */
2630 rbuCreateImposterTable(p, pIter);
2631 rbuCreateImposterTable2(p, pIter);
2632 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
2633
2634 /* Create the INSERT statement to write to the target PK b-tree */
2635 if( p->rc==SQLITE_OK ){
2636 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
2637 sqlite3_mprintf(
2638 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
2639 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
2640 )
2641 );
2642 }
2643
2644 /* Create the DELETE statement to write to the target PK b-tree.
2645 ** Because it only performs INSERT operations, this is not required for
2646 ** an rbu vacuum handle. */
2647 if( rbuIsVacuum(p)==0 && p->rc==SQLITE_OK ){
2648 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pDelete, pz,
2649 sqlite3_mprintf(
2650 "DELETE FROM \"%s%w\" WHERE %s", zWrite, zTbl, zWhere
2651 )
2652 );
2653 }
2654
2655 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
2656 const char *zRbuRowid = "";
2657 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
2658 zRbuRowid = ", rbu_rowid";
2659 }
2660
2661 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
2662 rbuMPrintfExec(p, p->dbRbu,
2663 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
2664 "SELECT *%s FROM '%q' WHERE 0;"
2665 , p->zStateDb, pIter->zDataTbl
2666 , (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
2667 , pIter->zDataTbl
2668 );
2669
2670 rbuMPrintfExec(p, p->dbMain,
2671 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
2672 "BEGIN "
2673 " SELECT rbu_tmp_insert(3, %s);"
2674 "END;"
2675
2676 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
2677 "BEGIN "
2678 " SELECT rbu_tmp_insert(3, %s);"
2679 "END;"
2680
2681 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
2682 "BEGIN "
2683 " SELECT rbu_tmp_insert(4, %s);"
2684 "END;",
2685 zWrite, zTbl, zOldlist,
2686 zWrite, zTbl, zOldlist,
2687 zWrite, zTbl, zNewlist
2688 );
2689
2690 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
2691 rbuMPrintfExec(p, p->dbMain,
2692 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
2693 "BEGIN "
2694 " SELECT rbu_tmp_insert(0, %s);"
2695 "END;",
2696 zWrite, zTbl, zNewlist
2697 );
2698 }
2699
2700 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
2701 }
2702
2703 /* Create the SELECT statement to read keys from data_xxx */
2704 if( p->rc==SQLITE_OK ){
2705 const char *zRbuRowid = "";
2706 if( bRbuRowid ){
2707 zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
2708 }
2709 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
2710 sqlite3_mprintf(
2711 "SELECT %s,%s rbu_control%s FROM '%q'%s",
2712 zCollist,
2713 (rbuIsVacuum(p) ? "0 AS " : ""),
2714 zRbuRowid,
2715 pIter->zDataTbl, zLimit
2716 )
2717 );
2718 }
2719
2720 sqlite3_free(zWhere);
2721 sqlite3_free(zOldlist);
2722 sqlite3_free(zNewlist);
2723 sqlite3_free(zBindings);
2724 }
2725 sqlite3_free(zCollist);
2726 sqlite3_free(zLimit);
2727 }
2728
2729 return p->rc;
2730 }
2731
2732 /*
2733 ** Set output variable *ppStmt to point to an UPDATE statement that may
2734 ** be used to update the imposter table for the main table b-tree of the
2735 ** table object that pIter currently points to, assuming that the
2736 ** rbu_control column of the data_xyz table contains zMask.
2737 **
2738 ** If the zMask string does not specify any columns to update, then this
2739 ** is not an error. Output variable *ppStmt is set to NULL in this case.
2740 */
2741 static int rbuGetUpdateStmt(
2742 sqlite3rbu *p, /* RBU handle */
2743 RbuObjIter *pIter, /* Object iterator */
2744 const char *zMask, /* rbu_control value ('x.x.') */
2745 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
2746 ){
2747 RbuUpdateStmt **pp;
2748 RbuUpdateStmt *pUp = 0;
2749 int nUp = 0;
2750
2751 /* In case an error occurs */
2752 *ppStmt = 0;
2753
2754 /* Search for an existing statement. If one is found, shift it to the front
2755 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
2756 ** to the number of statements currently in the cache and pUp to the
2757 ** last object in the list. */
2758 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
2759 pUp = *pp;
2760 if( strcmp(pUp->zMask, zMask)==0 ){
2761 *pp = pUp->pNext;
2762 pUp->pNext = pIter->pRbuUpdate;
2763 pIter->pRbuUpdate = pUp;
2764 *ppStmt = pUp->pUpdate;
2765 return SQLITE_OK;
2766 }
2767 nUp++;
2768 }
2769 assert( pUp==0 || pUp->pNext==0 );
2770
2771 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
2772 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
2773 *pp = 0;
2774 sqlite3_finalize(pUp->pUpdate);
2775 pUp->pUpdate = 0;
2776 }else{
2777 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
2778 }
2779
2780 if( pUp ){
2781 char *zWhere = rbuObjIterGetWhere(p, pIter);
2782 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
2783 char *zUpdate = 0;
2784
2785 pUp->zMask = (char*)&pUp[1];
2786 memcpy(pUp->zMask, zMask, pIter->nTblCol);
2787 pUp->pNext = pIter->pRbuUpdate;
2788 pIter->pRbuUpdate = pUp;
2789
2790 if( zSet ){
2791 const char *zPrefix = "";
2792
2793 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
2794 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
2795 zPrefix, pIter->zTbl, zSet, zWhere
2796 );
2797 p->rc = prepareFreeAndCollectError(
2798 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
2799 );
2800 *ppStmt = pUp->pUpdate;
2801 }
2802 sqlite3_free(zWhere);
2803 sqlite3_free(zSet);
2804 }
2805
2806 return p->rc;
2807 }
2808
2809 static sqlite3 *rbuOpenDbhandle(
2810 sqlite3rbu *p,
2811 const char *zName,
2812 int bUseVfs
2813 ){
2814 sqlite3 *db = 0;
2815 if( p->rc==SQLITE_OK ){
2816 const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
2817 p->rc = sqlite3_open_v2(zName, &db, flags, bUseVfs ? p->zVfsName : 0);
2818 if( p->rc ){
2819 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
2820 sqlite3_close(db);
2821 db = 0;
2822 }
2823 }
2824 return db;
2825 }
2826
2827 /*
2828 ** Free an RbuState object allocated by rbuLoadState().
2829 */
2830 static void rbuFreeState(RbuState *p){
2831 if( p ){
2832 sqlite3_free(p->zTbl);
2833 sqlite3_free(p->zIdx);
2834 sqlite3_free(p);
2835 }
2836 }
2837
2838 /*
2839 ** Allocate an RbuState object and load the contents of the rbu_state
2840 ** table into it. Return a pointer to the new object. It is the
2841 ** responsibility of the caller to eventually free the object using
2842 ** sqlite3_free().
2843 **
2844 ** If an error occurs, leave an error code and message in the rbu handle
2845 ** and return NULL.
2846 */
2847 static RbuState *rbuLoadState(sqlite3rbu *p){
2848 RbuState *pRet = 0;
2849 sqlite3_stmt *pStmt = 0;
2850 int rc;
2851 int rc2;
2852
2853 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
2854 if( pRet==0 ) return 0;
2855
2856 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
2857 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
2858 );
2859 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
2860 switch( sqlite3_column_int(pStmt, 0) ){
2861 case RBU_STATE_STAGE:
2862 pRet->eStage = sqlite3_column_int(pStmt, 1);
2863 if( pRet->eStage!=RBU_STAGE_OAL
2864 && pRet->eStage!=RBU_STAGE_MOVE
2865 && pRet->eStage!=RBU_STAGE_CKPT
2866 ){
2867 p->rc = SQLITE_CORRUPT;
2868 }
2869 break;
2870
2871 case RBU_STATE_TBL:
2872 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
2873 break;
2874
2875 case RBU_STATE_IDX:
2876 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
2877 break;
2878
2879 case RBU_STATE_ROW:
2880 pRet->nRow = sqlite3_column_int(pStmt, 1);
2881 break;
2882
2883 case RBU_STATE_PROGRESS:
2884 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
2885 break;
2886
2887 case RBU_STATE_CKPT:
2888 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
2889 break;
2890
2891 case RBU_STATE_COOKIE:
2892 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
2893 break;
2894
2895 case RBU_STATE_OALSZ:
2896 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
2897 break;
2898
2899 case RBU_STATE_PHASEONESTEP:
2900 pRet->nPhaseOneStep = sqlite3_column_int64(pStmt, 1);
2901 break;
2902
2903 default:
2904 rc = SQLITE_CORRUPT;
2905 break;
2906 }
2907 }
2908 rc2 = sqlite3_finalize(pStmt);
2909 if( rc==SQLITE_OK ) rc = rc2;
2910
2911 p->rc = rc;
2912 return pRet;
2913 }
2914
2915
2916 /*
2917 ** Open the database handle and attach the RBU database as "rbu". If an
2918 ** error occurs, leave an error code and message in the RBU handle.
2919 */
2920 static void rbuOpenDatabase(sqlite3rbu *p, int *pbRetry){
2921 assert( p->rc || (p->dbMain==0 && p->dbRbu==0) );
2922 assert( p->rc || rbuIsVacuum(p) || p->zTarget!=0 );
2923
2924 /* Open the RBU database */
2925 p->dbRbu = rbuOpenDbhandle(p, p->zRbu, 1);
2926
2927 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
2928 sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
2929 if( p->zState==0 ){
2930 const char *zFile = sqlite3_db_filename(p->dbRbu, "main");
2931 p->zState = rbuMPrintf(p, "file://%s-vacuum?modeof=%s", zFile, zFile);
2932 }
2933 }
2934
2935 /* If using separate RBU and state databases, attach the state database to
2936 ** the RBU db handle now. */
2937 if( p->zState ){
2938 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
2939 memcpy(p->zStateDb, "stat", 4);
2940 }else{
2941 memcpy(p->zStateDb, "main", 4);
2942 }
2943
2944 #if 0
2945 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
2946 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, 0);
2947 }
2948 #endif
2949
2950 /* If it has not already been created, create the rbu_state table */
2951 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
2952
2953 #if 0
2954 if( rbuIsVacuum(p) ){
2955 if( p->rc==SQLITE_OK ){
2956 int rc2;
2957 int bOk = 0;
2958 sqlite3_stmt *pCnt = 0;
2959 p->rc = prepareAndCollectError(p->dbRbu, &pCnt, &p->zErrmsg,
2960 "SELECT count(*) FROM stat.sqlite_master"
2961 );
2962 if( p->rc==SQLITE_OK
2963 && sqlite3_step(pCnt)==SQLITE_ROW
2964 && 1==sqlite3_column_int(pCnt, 0)
2965 ){
2966 bOk = 1;
2967 }
2968 rc2 = sqlite3_finalize(pCnt);
2969 if( p->rc==SQLITE_OK ) p->rc = rc2;
2970
2971 if( p->rc==SQLITE_OK && bOk==0 ){
2972 p->rc = SQLITE_ERROR;
2973 p->zErrmsg = sqlite3_mprintf("invalid state database");
2974 }
2975
2976 if( p->rc==SQLITE_OK ){
2977 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
2978 }
2979 }
2980 }
2981 #endif
2982
2983 if( p->rc==SQLITE_OK && rbuIsVacuum(p) ){
2984 int bOpen = 0;
2985 int rc;
2986 p->nRbu = 0;
2987 p->pRbuFd = 0;
2988 rc = sqlite3_file_control(p->dbRbu, "main", SQLITE_FCNTL_RBUCNT, (void*)p);
2989 if( rc!=SQLITE_NOTFOUND ) p->rc = rc;
2990 if( p->eStage>=RBU_STAGE_MOVE ){
2991 bOpen = 1;
2992 }else{
2993 RbuState *pState = rbuLoadState(p);
2994 if( pState ){
2995 bOpen = (pState->eStage>=RBU_STAGE_MOVE);
2996 rbuFreeState(pState);
2997 }
2998 }
2999 if( bOpen ) p->dbMain = rbuOpenDbhandle(p, p->zRbu, p->nRbu<=1);
3000 }
3001
3002 p->eStage = 0;
3003 if( p->rc==SQLITE_OK && p->dbMain==0 ){
3004 if( !rbuIsVacuum(p) ){
3005 p->dbMain = rbuOpenDbhandle(p, p->zTarget, 1);
3006 }else if( p->pRbuFd->pWalFd ){
3007 if( pbRetry ){
3008 p->pRbuFd->bNolock = 0;
3009 sqlite3_close(p->dbRbu);
3010 sqlite3_close(p->dbMain);
3011 p->dbMain = 0;
3012 p->dbRbu = 0;
3013 *pbRetry = 1;
3014 return;
3015 }
3016 p->rc = SQLITE_ERROR;
3017 p->zErrmsg = sqlite3_mprintf("cannot vacuum wal mode database");
3018 }else{
3019 char *zTarget;
3020 char *zExtra = 0;
3021 if( strlen(p->zRbu)>=5 && 0==memcmp("file:", p->zRbu, 5) ){
3022 zExtra = &p->zRbu[5];
3023 while( *zExtra ){
3024 if( *zExtra++=='?' ) break;
3025 }
3026 if( *zExtra=='\0' ) zExtra = 0;
3027 }
3028
3029 zTarget = sqlite3_mprintf("file:%s-vacuum?rbu_memory=1%s%s",
3030 sqlite3_db_filename(p->dbRbu, "main"),
3031 (zExtra==0 ? "" : "&"), (zExtra==0 ? "" : zExtra)
3032 );
3033
3034 if( zTarget==0 ){
3035 p->rc = SQLITE_NOMEM;
3036 return;
3037 }
3038 p->dbMain = rbuOpenDbhandle(p, zTarget, p->nRbu<=1);
3039 sqlite3_free(zTarget);
3040 }
3041 }
3042
3043 if( p->rc==SQLITE_OK ){
3044 p->rc = sqlite3_create_function(p->dbMain,
3045 "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
3046 );
3047 }
3048
3049 if( p->rc==SQLITE_OK ){
3050 p->rc = sqlite3_create_function(p->dbMain,
3051 "rbu_fossil_delta", 2, SQLITE_UTF8, 0, rbuFossilDeltaFunc, 0, 0
3052 );
3053 }
3054
3055 if( p->rc==SQLITE_OK ){
3056 p->rc = sqlite3_create_function(p->dbRbu,
3057 "rbu_target_name", -1, SQLITE_UTF8, (void*)p, rbuTargetNameFunc, 0, 0
3058 );
3059 }
3060
3061 if( p->rc==SQLITE_OK ){
3062 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
3063 }
3064 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
3065
3066 /* Mark the database file just opened as an RBU target database. If
3067 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
3068 ** This is an error. */
3069 if( p->rc==SQLITE_OK ){
3070 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
3071 }
3072
3073 if( p->rc==SQLITE_NOTFOUND ){
3074 p->rc = SQLITE_ERROR;
3075 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
3076 }
3077 }
3078
3079 /*
3080 ** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
3081 ** It is a no-op unless SQLITE_ENABLE_8_3_NAMES is defined.
3082 **
3083 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
3084 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
3085 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
3086 ** three characters, then shorten the suffix on z[] to be the last three
3087 ** characters of the original suffix.
3088 **
3089 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
3090 ** do the suffix shortening regardless of URI parameter.
3091 **
3092 ** Examples:
3093 **
3094 ** test.db-journal => test.nal
3095 ** test.db-wal => test.wal
3096 ** test.db-shm => test.shm
3097 ** test.db-mj7f3319fa => test.9fa
3098 */
3099 static void rbuFileSuffix3(const char *zBase, char *z){
3100 #ifdef SQLITE_ENABLE_8_3_NAMES
3101 #if SQLITE_ENABLE_8_3_NAMES<2
3102 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
3103 #endif
3104 {
3105 int i, sz;
3106 sz = (int)strlen(z)&0xffffff;
3107 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
3108 if( z[i]=='.' && sz>i+4 ) memmove(&z[i+1], &z[sz-3], 4);
3109 }
3110 #endif
3111 }
3112
3113 /*
3114 ** Return the current wal-index header checksum for the target database
3115 ** as a 64-bit integer.
3116 **
3117 ** The checksum is store in the first page of xShmMap memory as an 8-byte
3118 ** blob starting at byte offset 40.
3119 */
3120 static i64 rbuShmChecksum(sqlite3rbu *p){
3121 i64 iRet = 0;
3122 if( p->rc==SQLITE_OK ){
3123 sqlite3_file *pDb = p->pTargetFd->pReal;
3124 u32 volatile *ptr;
3125 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
3126 if( p->rc==SQLITE_OK ){
3127 iRet = ((i64)ptr[10] << 32) + ptr[11];
3128 }
3129 }
3130 return iRet;
3131 }
3132
3133 /*
3134 ** This function is called as part of initializing or reinitializing an
3135 ** incremental checkpoint.
3136 **
3137 ** It populates the sqlite3rbu.aFrame[] array with the set of
3138 ** (wal frame -> db page) copy operations required to checkpoint the
3139 ** current wal file, and obtains the set of shm locks required to safely
3140 ** perform the copy operations directly on the file-system.
3141 **
3142 ** If argument pState is not NULL, then the incremental checkpoint is
3143 ** being resumed. In this case, if the checksum of the wal-index-header
3144 ** following recovery is not the same as the checksum saved in the RbuState
3145 ** object, then the rbu handle is set to DONE state. This occurs if some
3146 ** other client appends a transaction to the wal file in the middle of
3147 ** an incremental checkpoint.
3148 */
3149 static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
3150
3151 /* If pState is NULL, then the wal file may not have been opened and
3152 ** recovered. Running a read-statement here to ensure that doing so
3153 ** does not interfere with the "capture" process below. */
3154 if( pState==0 ){
3155 p->eStage = 0;
3156 if( p->rc==SQLITE_OK ){
3157 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
3158 }
3159 }
3160
3161 /* Assuming no error has occurred, run a "restart" checkpoint with the
3162 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
3163 ** special behaviour in the rbu VFS:
3164 **
3165 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
3166 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
3167 ** proceed with running a passive checkpoint instead of failing).
3168 **
3169 ** * Attempts to read from the *-wal file or write to the database file
3170 ** do not perform any IO. Instead, the frame/page combinations that
3171 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
3172 ** array.
3173 **
3174 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
3175 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
3176 ** no-ops. These locks will not be released until the connection
3177 ** is closed.
3178 **
3179 ** * Attempting to xSync() the database file causes an SQLITE_INTERNAL
3180 ** error.
3181 **
3182 ** As a result, unless an error (i.e. OOM or SQLITE_BUSY) occurs, the
3183 ** checkpoint below fails with SQLITE_INTERNAL, and leaves the aFrame[]
3184 ** array populated with a set of (frame -> page) mappings. Because the
3185 ** WRITER, CHECKPOINT and READ0 locks are still held, it is safe to copy
3186 ** data from the wal file into the database file according to the
3187 ** contents of aFrame[].
3188 */
3189 if( p->rc==SQLITE_OK ){
3190 int rc2;
3191 p->eStage = RBU_STAGE_CAPTURE;
3192 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
3193 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
3194 }
3195
3196 if( p->rc==SQLITE_OK && p->nFrame>0 ){
3197 p->eStage = RBU_STAGE_CKPT;
3198 p->nStep = (pState ? pState->nRow : 0);
3199 p->aBuf = rbuMalloc(p, p->pgsz);
3200 p->iWalCksum = rbuShmChecksum(p);
3201 }
3202
3203 if( p->rc==SQLITE_OK ){
3204 if( p->nFrame==0 || (pState && pState->iWalCksum!=p->iWalCksum) ){
3205 p->rc = SQLITE_DONE;
3206 p->eStage = RBU_STAGE_DONE;
3207 }
3208 }
3209 }
3210
3211 /*
3212 ** Called when iAmt bytes are read from offset iOff of the wal file while
3213 ** the rbu object is in capture mode. Record the frame number of the frame
3214 ** being read in the aFrame[] array.
3215 */
3216 static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
3217 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
3218 u32 iFrame;
3219
3220 if( pRbu->mLock!=mReq ){
3221 pRbu->rc = SQLITE_BUSY;
3222 return SQLITE_INTERNAL;
3223 }
3224
3225 pRbu->pgsz = iAmt;
3226 if( pRbu->nFrame==pRbu->nFrameAlloc ){
3227 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
3228 RbuFrame *aNew;
3229 aNew = (RbuFrame*)sqlite3_realloc64(pRbu->aFrame, nNew * sizeof(RbuFrame));
3230 if( aNew==0 ) return SQLITE_NOMEM;
3231 pRbu->aFrame = aNew;
3232 pRbu->nFrameAlloc = nNew;
3233 }
3234
3235 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
3236 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
3237 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
3238 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
3239 pRbu->nFrame++;
3240 return SQLITE_OK;
3241 }
3242
3243 /*
3244 ** Called when a page of data is written to offset iOff of the database
3245 ** file while the rbu handle is in capture mode. Record the page number
3246 ** of the page being written in the aFrame[] array.
3247 */
3248 static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
3249 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
3250 return SQLITE_OK;
3251 }
3252
3253 /*
3254 ** This is called as part of an incremental checkpoint operation. Copy
3255 ** a single frame of data from the wal file into the database file, as
3256 ** indicated by the RbuFrame object.
3257 */
3258 static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
3259 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
3260 sqlite3_file *pDb = p->pTargetFd->pReal;
3261 i64 iOff;
3262
3263 assert( p->rc==SQLITE_OK );
3264 iOff = (i64)(pFrame->iWalFrame-1) * (p->pgsz + 24) + 32 + 24;
3265 p->rc = pWal->pMethods->xRead(pWal, p->aBuf, p->pgsz, iOff);
3266 if( p->rc ) return;
3267
3268 iOff = (i64)(pFrame->iDbPage-1) * p->pgsz;
3269 p->rc = pDb->pMethods->xWrite(pDb, p->aBuf, p->pgsz, iOff);
3270 }
3271
3272
3273 /*
3274 ** Take an EXCLUSIVE lock on the database file.
3275 */
3276 static void rbuLockDatabase(sqlite3rbu *p){
3277 sqlite3_file *pReal = p->pTargetFd->pReal;
3278 assert( p->rc==SQLITE_OK );
3279 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
3280 if( p->rc==SQLITE_OK ){
3281 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
3282 }
3283 }
3284
3285 #if defined(_WIN32_WCE)
3286 static LPWSTR rbuWinUtf8ToUnicode(const char *zFilename){
3287 int nChar;
3288 LPWSTR zWideFilename;
3289
3290 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
3291 if( nChar==0 ){
3292 return 0;
3293 }
3294 zWideFilename = sqlite3_malloc64( nChar*sizeof(zWideFilename[0]) );
3295 if( zWideFilename==0 ){
3296 return 0;
3297 }
3298 memset(zWideFilename, 0, nChar*sizeof(zWideFilename[0]));
3299 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
3300 nChar);
3301 if( nChar==0 ){
3302 sqlite3_free(zWideFilename);
3303 zWideFilename = 0;
3304 }
3305 return zWideFilename;
3306 }
3307 #endif
3308
3309 /*
3310 ** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
3311 ** on the database file. This proc moves the *-oal file to the *-wal path,
3312 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
3313 ** If an error occurs, leave an error code and error message in the rbu
3314 ** handle.
3315 */
3316 static void rbuMoveOalFile(sqlite3rbu *p){
3317 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
3318 const char *zMove = zBase;
3319 char *zOal;
3320 char *zWal;
3321
3322 if( rbuIsVacuum(p) ){
3323 zMove = sqlite3_db_filename(p->dbRbu, "main");
3324 }
3325 zOal = sqlite3_mprintf("%s-oal", zMove);
3326 zWal = sqlite3_mprintf("%s-wal", zMove);
3327
3328 assert( p->eStage==RBU_STAGE_MOVE );
3329 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
3330 if( zWal==0 || zOal==0 ){
3331 p->rc = SQLITE_NOMEM;
3332 }else{
3333 /* Move the *-oal file to *-wal. At this point connection p->db is
3334 ** holding a SHARED lock on the target database file (because it is
3335 ** in WAL mode). So no other connection may be writing the db.
3336 **
3337 ** In order to ensure that there are no database readers, an EXCLUSIVE
3338 ** lock is obtained here before the *-oal is moved to *-wal.
3339 */
3340 rbuLockDatabase(p);
3341 if( p->rc==SQLITE_OK ){
3342 rbuFileSuffix3(zBase, zWal);
3343 rbuFileSuffix3(zBase, zOal);
3344
3345 /* Re-open the databases. */
3346 rbuObjIterFinalize(&p->objiter);
3347 sqlite3_close(p->dbRbu);
3348 sqlite3_close(p->dbMain);
3349 p->dbMain = 0;
3350 p->dbRbu = 0;
3351
3352 #if defined(_WIN32_WCE)
3353 {
3354 LPWSTR zWideOal;
3355 LPWSTR zWideWal;
3356
3357 zWideOal = rbuWinUtf8ToUnicode(zOal);
3358 if( zWideOal ){
3359 zWideWal = rbuWinUtf8ToUnicode(zWal);
3360 if( zWideWal ){
3361 if( MoveFileW(zWideOal, zWideWal) ){
3362 p->rc = SQLITE_OK;
3363 }else{
3364 p->rc = SQLITE_IOERR;
3365 }
3366 sqlite3_free(zWideWal);
3367 }else{
3368 p->rc = SQLITE_IOERR_NOMEM;
3369 }
3370 sqlite3_free(zWideOal);
3371 }else{
3372 p->rc = SQLITE_IOERR_NOMEM;
3373 }
3374 }
3375 #else
3376 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
3377 #endif
3378
3379 if( p->rc==SQLITE_OK ){
3380 rbuOpenDatabase(p, 0);
3381 rbuSetupCheckpoint(p, 0);
3382 }
3383 }
3384 }
3385
3386 sqlite3_free(zWal);
3387 sqlite3_free(zOal);
3388 }
3389
3390 /*
3391 ** The SELECT statement iterating through the keys for the current object
3392 ** (p->objiter.pSelect) currently points to a valid row. This function
3393 ** determines the type of operation requested by this row and returns
3394 ** one of the following values to indicate the result:
3395 **
3396 ** * RBU_INSERT
3397 ** * RBU_DELETE
3398 ** * RBU_IDX_DELETE
3399 ** * RBU_UPDATE
3400 **
3401 ** If RBU_UPDATE is returned, then output variable *pzMask is set to
3402 ** point to the text value indicating the columns to update.
3403 **
3404 ** If the rbu_control field contains an invalid value, an error code and
3405 ** message are left in the RBU handle and zero returned.
3406 */
3407 static int rbuStepType(sqlite3rbu *p, const char **pzMask){
3408 int iCol = p->objiter.nCol; /* Index of rbu_control column */
3409 int res = 0; /* Return value */
3410
3411 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
3412 case SQLITE_INTEGER: {
3413 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
3414 switch( iVal ){
3415 case 0: res = RBU_INSERT; break;
3416 case 1: res = RBU_DELETE; break;
3417 case 2: res = RBU_REPLACE; break;
3418 case 3: res = RBU_IDX_DELETE; break;
3419 case 4: res = RBU_IDX_INSERT; break;
3420 }
3421 break;
3422 }
3423
3424 case SQLITE_TEXT: {
3425 const unsigned char *z = sqlite3_column_text(p->objiter.pSelect, iCol);
3426 if( z==0 ){
3427 p->rc = SQLITE_NOMEM;
3428 }else{
3429 *pzMask = (const char*)z;
3430 }
3431 res = RBU_UPDATE;
3432
3433 break;
3434 }
3435
3436 default:
3437 break;
3438 }
3439
3440 if( res==0 ){
3441 rbuBadControlError(p);
3442 }
3443 return res;
3444 }
3445
3446 #ifdef SQLITE_DEBUG
3447 /*
3448 ** Assert that column iCol of statement pStmt is named zName.
3449 */
3450 static void assertColumnName(sqlite3_stmt *pStmt, int iCol, const char *zName){
3451 const char *zCol = sqlite3_column_name(pStmt, iCol);
3452 assert( 0==sqlite3_stricmp(zName, zCol) );
3453 }
3454 #else
3455 # define assertColumnName(x,y,z)
3456 #endif
3457
3458 /*
3459 ** Argument eType must be one of RBU_INSERT, RBU_DELETE, RBU_IDX_INSERT or
3460 ** RBU_IDX_DELETE. This function performs the work of a single
3461 ** sqlite3rbu_step() call for the type of operation specified by eType.
3462 */
3463 static void rbuStepOneOp(sqlite3rbu *p, int eType){
3464 RbuObjIter *pIter = &p->objiter;
3465 sqlite3_value *pVal;
3466 sqlite3_stmt *pWriter;
3467 int i;
3468
3469 assert( p->rc==SQLITE_OK );
3470 assert( eType!=RBU_DELETE || pIter->zIdx==0 );
3471 assert( eType==RBU_DELETE || eType==RBU_IDX_DELETE
3472 || eType==RBU_INSERT || eType==RBU_IDX_INSERT
3473 );
3474
3475 /* If this is a delete, decrement nPhaseOneStep by nIndex. If the DELETE
3476 ** statement below does actually delete a row, nPhaseOneStep will be
3477 ** incremented by the same amount when SQL function rbu_tmp_insert()
3478 ** is invoked by the trigger. */
3479 if( eType==RBU_DELETE ){
3480 p->nPhaseOneStep -= p->objiter.nIndex;
3481 }
3482
3483 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
3484 pWriter = pIter->pDelete;
3485 }else{
3486 pWriter = pIter->pInsert;
3487 }
3488
3489 for(i=0; i<pIter->nCol; i++){
3490 /* If this is an INSERT into a table b-tree and the table has an
3491 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
3492 ** to write a NULL into the IPK column. That is not permitted. */
3493 if( eType==RBU_INSERT
3494 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
3495 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
3496 ){
3497 p->rc = SQLITE_MISMATCH;
3498 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
3499 return;
3500 }
3501
3502 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
3503 continue;
3504 }
3505
3506 pVal = sqlite3_column_value(pIter->pSelect, i);
3507 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
3508 if( p->rc ) return;
3509 }
3510 if( pIter->zIdx==0 ){
3511 if( pIter->eType==RBU_PK_VTAB
3512 || pIter->eType==RBU_PK_NONE
3513 || (pIter->eType==RBU_PK_EXTERNAL && rbuIsVacuum(p))
3514 ){
3515 /* For a virtual table, or a table with no primary key, the
3516 ** SELECT statement is:
3517 **
3518 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
3519 **
3520 ** Hence column_value(pIter->nCol+1).
3521 */
3522 assertColumnName(pIter->pSelect, pIter->nCol+1,
3523 rbuIsVacuum(p) ? "rowid" : "rbu_rowid"
3524 );
3525 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
3526 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
3527 }
3528 }
3529 if( p->rc==SQLITE_OK ){
3530 sqlite3_step(pWriter);
3531 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
3532 }
3533 }
3534
3535 /*
3536 ** This function does the work for an sqlite3rbu_step() call.
3537 **
3538 ** The object-iterator (p->objiter) currently points to a valid object,
3539 ** and the input cursor (p->objiter.pSelect) currently points to a valid
3540 ** input row. Perform whatever processing is required and return.
3541 **
3542 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
3543 ** and message is left in the RBU handle and a copy of the error code
3544 ** returned.
3545 */
3546 static int rbuStep(sqlite3rbu *p){
3547 RbuObjIter *pIter = &p->objiter;
3548 const char *zMask = 0;
3549 int eType = rbuStepType(p, &zMask);
3550
3551 if( eType ){
3552 assert( eType==RBU_INSERT || eType==RBU_DELETE
3553 || eType==RBU_REPLACE || eType==RBU_IDX_DELETE
3554 || eType==RBU_IDX_INSERT || eType==RBU_UPDATE
3555 );
3556 assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
3557
3558 if( pIter->zIdx==0 && (eType==RBU_IDX_DELETE || eType==RBU_IDX_INSERT) ){
3559 rbuBadControlError(p);
3560 }
3561 else if( eType==RBU_REPLACE ){
3562 if( pIter->zIdx==0 ){
3563 p->nPhaseOneStep += p->objiter.nIndex;
3564 rbuStepOneOp(p, RBU_DELETE);
3565 }
3566 if( p->rc==SQLITE_OK ) rbuStepOneOp(p, RBU_INSERT);
3567 }
3568 else if( eType!=RBU_UPDATE ){
3569 rbuStepOneOp(p, eType);
3570 }
3571 else{
3572 sqlite3_value *pVal;
3573 sqlite3_stmt *pUpdate = 0;
3574 assert( eType==RBU_UPDATE );
3575 p->nPhaseOneStep -= p->objiter.nIndex;
3576 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
3577 if( pUpdate ){
3578 int i;
3579 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
3580 char c = zMask[pIter->aiSrcOrder[i]];
3581 pVal = sqlite3_column_value(pIter->pSelect, i);
3582 if( pIter->abTblPk[i] || c!='.' ){
3583 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
3584 }
3585 }
3586 if( p->rc==SQLITE_OK
3587 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
3588 ){
3589 /* Bind the rbu_rowid value to column _rowid_ */
3590 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
3591 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
3592 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
3593 }
3594 if( p->rc==SQLITE_OK ){
3595 sqlite3_step(pUpdate);
3596 p->rc = resetAndCollectError(pUpdate, &p->zErrmsg);
3597 }
3598 }
3599 }
3600 }
3601 return p->rc;
3602 }
3603
3604 /*
3605 ** Increment the schema cookie of the main database opened by p->dbMain.
3606 **
3607 ** Or, if this is an RBU vacuum, set the schema cookie of the main db
3608 ** opened by p->dbMain to one more than the schema cookie of the main
3609 ** db opened by p->dbRbu.
3610 */
3611 static void rbuIncrSchemaCookie(sqlite3rbu *p){
3612 if( p->rc==SQLITE_OK ){
3613 sqlite3 *dbread = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
3614 int iCookie = 1000000;
3615 sqlite3_stmt *pStmt;
3616
3617 p->rc = prepareAndCollectError(dbread, &pStmt, &p->zErrmsg,
3618 "PRAGMA schema_version"
3619 );
3620 if( p->rc==SQLITE_OK ){
3621 /* Coverage: it may be that this sqlite3_step() cannot fail. There
3622 ** is already a transaction open, so the prepared statement cannot
3623 ** throw an SQLITE_SCHEMA exception. The only database page the
3624 ** statement reads is page 1, which is guaranteed to be in the cache.
3625 ** And no memory allocations are required. */
3626 if( SQLITE_ROW==sqlite3_step(pStmt) ){
3627 iCookie = sqlite3_column_int(pStmt, 0);
3628 }
3629 rbuFinalize(p, pStmt);
3630 }
3631 if( p->rc==SQLITE_OK ){
3632 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
3633 }
3634 }
3635 }
3636
3637 /*
3638 ** Update the contents of the rbu_state table within the rbu database. The
3639 ** value stored in the RBU_STATE_STAGE column is eStage. All other values
3640 ** are determined by inspecting the rbu handle passed as the first argument.
3641 */
3642 static void rbuSaveState(sqlite3rbu *p, int eStage){
3643 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
3644 sqlite3_stmt *pInsert = 0;
3645 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
3646 int rc;
3647
3648 assert( p->zErrmsg==0 );
3649 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
3650 sqlite3_mprintf(
3651 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
3652 "(%d, %d), "
3653 "(%d, %Q), "
3654 "(%d, %Q), "
3655 "(%d, %d), "
3656 "(%d, %d), "
3657 "(%d, %lld), "
3658 "(%d, %lld), "
3659 "(%d, %lld), "
3660 "(%d, %lld) ",
3661 p->zStateDb,
3662 RBU_STATE_STAGE, eStage,
3663 RBU_STATE_TBL, p->objiter.zTbl,
3664 RBU_STATE_IDX, p->objiter.zIdx,
3665 RBU_STATE_ROW, p->nStep,
3666 RBU_STATE_PROGRESS, p->nProgress,
3667 RBU_STATE_CKPT, p->iWalCksum,
3668 RBU_STATE_COOKIE, (i64)pFd->iCookie,
3669 RBU_STATE_OALSZ, p->iOalSz,
3670 RBU_STATE_PHASEONESTEP, p->nPhaseOneStep
3671 )
3672 );
3673 assert( pInsert==0 || rc==SQLITE_OK );
3674
3675 if( rc==SQLITE_OK ){
3676 sqlite3_step(pInsert);
3677 rc = sqlite3_finalize(pInsert);
3678 }
3679 if( rc!=SQLITE_OK ) p->rc = rc;
3680 }
3681 }
3682
3683
3684 /*
3685 ** The second argument passed to this function is the name of a PRAGMA
3686 ** setting - "page_size", "auto_vacuum", "user_version" or "application_id".
3687 ** This function executes the following on sqlite3rbu.dbRbu:
3688 **
3689 ** "PRAGMA main.$zPragma"
3690 **
3691 ** where $zPragma is the string passed as the second argument, then
3692 ** on sqlite3rbu.dbMain:
3693 **
3694 ** "PRAGMA main.$zPragma = $val"
3695 **
3696 ** where $val is the value returned by the first PRAGMA invocation.
3697 **
3698 ** In short, it copies the value of the specified PRAGMA setting from
3699 ** dbRbu to dbMain.
3700 */
3701 static void rbuCopyPragma(sqlite3rbu *p, const char *zPragma){
3702 if( p->rc==SQLITE_OK ){
3703 sqlite3_stmt *pPragma = 0;
3704 p->rc = prepareFreeAndCollectError(p->dbRbu, &pPragma, &p->zErrmsg,
3705 sqlite3_mprintf("PRAGMA main.%s", zPragma)
3706 );
3707 if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPragma) ){
3708 p->rc = rbuMPrintfExec(p, p->dbMain, "PRAGMA main.%s = %d",
3709 zPragma, sqlite3_column_int(pPragma, 0)
3710 );
3711 }
3712 rbuFinalize(p, pPragma);
3713 }
3714 }
3715
3716 /*
3717 ** The RBU handle passed as the only argument has just been opened and
3718 ** the state database is empty. If this RBU handle was opened for an
3719 ** RBU vacuum operation, create the schema in the target db.
3720 */
3721 static void rbuCreateTargetSchema(sqlite3rbu *p){
3722 sqlite3_stmt *pSql = 0;
3723 sqlite3_stmt *pInsert = 0;
3724
3725 assert( rbuIsVacuum(p) );
3726 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=1", 0,0, &p->zErrmsg);
3727 if( p->rc==SQLITE_OK ){
3728 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
3729 "SELECT sql FROM sqlite_master WHERE sql!='' AND rootpage!=0"
3730 " AND name!='sqlite_sequence' "
3731 " ORDER BY type DESC"
3732 );
3733 }
3734
3735 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
3736 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
3737 p->rc = sqlite3_exec(p->dbMain, zSql, 0, 0, &p->zErrmsg);
3738 }
3739 rbuFinalize(p, pSql);
3740 if( p->rc!=SQLITE_OK ) return;
3741
3742 if( p->rc==SQLITE_OK ){
3743 p->rc = prepareAndCollectError(p->dbRbu, &pSql, &p->zErrmsg,
3744 "SELECT * FROM sqlite_master WHERE rootpage=0 OR rootpage IS NULL"
3745 );
3746 }
3747
3748 if( p->rc==SQLITE_OK ){
3749 p->rc = prepareAndCollectError(p->dbMain, &pInsert, &p->zErrmsg,
3750 "INSERT INTO sqlite_master VALUES(?,?,?,?,?)"
3751 );
3752 }
3753
3754 while( p->rc==SQLITE_OK && sqlite3_step(pSql)==SQLITE_ROW ){
3755 int i;
3756 for(i=0; i<5; i++){
3757 sqlite3_bind_value(pInsert, i+1, sqlite3_column_value(pSql, i));
3758 }
3759 sqlite3_step(pInsert);
3760 p->rc = sqlite3_reset(pInsert);
3761 }
3762 if( p->rc==SQLITE_OK ){
3763 p->rc = sqlite3_exec(p->dbMain, "PRAGMA writable_schema=0",0,0,&p->zErrmsg);
3764 }
3765
3766 rbuFinalize(p, pSql);
3767 rbuFinalize(p, pInsert);
3768 }
3769
3770 /*
3771 ** Step the RBU object.
3772 */
3773 SQLITE_API int sqlite3rbu_step(sqlite3rbu *p){
3774 if( p ){
3775 switch( p->eStage ){
3776 case RBU_STAGE_OAL: {
3777 RbuObjIter *pIter = &p->objiter;
3778
3779 /* If this is an RBU vacuum operation and the state table was empty
3780 ** when this handle was opened, create the target database schema. */
3781 if( rbuIsVacuum(p) && p->nProgress==0 && p->rc==SQLITE_OK ){
3782 rbuCreateTargetSchema(p);
3783 rbuCopyPragma(p, "user_version");
3784 rbuCopyPragma(p, "application_id");
3785 }
3786
3787 while( p->rc==SQLITE_OK && pIter->zTbl ){
3788
3789 if( pIter->bCleanup ){
3790 /* Clean up the rbu_tmp_xxx table for the previous table. It
3791 ** cannot be dropped as there are currently active SQL statements.
3792 ** But the contents can be deleted. */
3793 if( rbuIsVacuum(p)==0 && pIter->abIndexed ){
3794 rbuMPrintfExec(p, p->dbRbu,
3795 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zDataTbl
3796 );
3797 }
3798 }else{
3799 rbuObjIterPrepareAll(p, pIter, 0);
3800
3801 /* Advance to the next row to process. */
3802 if( p->rc==SQLITE_OK ){
3803 int rc = sqlite3_step(pIter->pSelect);
3804 if( rc==SQLITE_ROW ){
3805 p->nProgress++;
3806 p->nStep++;
3807 return rbuStep(p);
3808 }
3809 p->rc = sqlite3_reset(pIter->pSelect);
3810 p->nStep = 0;
3811 }
3812 }
3813
3814 rbuObjIterNext(p, pIter);
3815 }
3816
3817 if( p->rc==SQLITE_OK ){
3818 assert( pIter->zTbl==0 );
3819 rbuSaveState(p, RBU_STAGE_MOVE);
3820 rbuIncrSchemaCookie(p);
3821 if( p->rc==SQLITE_OK ){
3822 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
3823 }
3824 if( p->rc==SQLITE_OK ){
3825 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
3826 }
3827 p->eStage = RBU_STAGE_MOVE;
3828 }
3829 break;
3830 }
3831
3832 case RBU_STAGE_MOVE: {
3833 if( p->rc==SQLITE_OK ){
3834 rbuMoveOalFile(p);
3835 p->nProgress++;
3836 }
3837 break;
3838 }
3839
3840 case RBU_STAGE_CKPT: {
3841 if( p->rc==SQLITE_OK ){
3842 if( p->nStep>=p->nFrame ){
3843 sqlite3_file *pDb = p->pTargetFd->pReal;
3844
3845 /* Sync the db file */
3846 p->rc = pDb->pMethods->xSync(pDb, SQLITE_SYNC_NORMAL);
3847
3848 /* Update nBackfill */
3849 if( p->rc==SQLITE_OK ){
3850 void volatile *ptr;
3851 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, &ptr);
3852 if( p->rc==SQLITE_OK ){
3853 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
3854 }
3855 }
3856
3857 if( p->rc==SQLITE_OK ){
3858 p->eStage = RBU_STAGE_DONE;
3859 p->rc = SQLITE_DONE;
3860 }
3861 }else{
3862 RbuFrame *pFrame = &p->aFrame[p->nStep];
3863 rbuCheckpointFrame(p, pFrame);
3864 p->nStep++;
3865 }
3866 p->nProgress++;
3867 }
3868 break;
3869 }
3870
3871 default:
3872 break;
3873 }
3874 return p->rc;
3875 }else{
3876 return SQLITE_NOMEM;
3877 }
3878 }
3879
3880 /*
3881 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
3882 ** otherwise. Either or both argument may be NULL. Two NULL values are
3883 ** considered equal, and NULL is considered distinct from all other values.
3884 */
3885 static int rbuStrCompare(const char *z1, const char *z2){
3886 if( z1==0 && z2==0 ) return 0;
3887 if( z1==0 || z2==0 ) return 1;
3888 return (sqlite3_stricmp(z1, z2)!=0);
3889 }
3890
3891 /*
3892 ** This function is called as part of sqlite3rbu_open() when initializing
3893 ** an rbu handle in OAL stage. If the rbu update has not started (i.e.
3894 ** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
3895 ** things so that the next call to sqlite3rbu_step() continues on from
3896 ** where the previous rbu handle left off.
3897 **
3898 ** If an error occurs, an error code and error message are left in the
3899 ** rbu handle passed as the first argument.
3900 */
3901 static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
3902 assert( p->rc==SQLITE_OK );
3903 if( pState->zTbl ){
3904 RbuObjIter *pIter = &p->objiter;
3905 int rc = SQLITE_OK;
3906
3907 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
3908 || rbuStrCompare(pIter->zIdx, pState->zIdx)
3909 || rbuStrCompare(pIter->zTbl, pState->zTbl)
3910 )){
3911 rc = rbuObjIterNext(p, pIter);
3912 }
3913
3914 if( rc==SQLITE_OK && !pIter->zTbl ){
3915 rc = SQLITE_ERROR;
3916 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
3917 }
3918
3919 if( rc==SQLITE_OK ){
3920 p->nStep = pState->nRow;
3921 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
3922 }
3923
3924 p->rc = rc;
3925 }
3926 }
3927
3928 /*
3929 ** If there is a "*-oal" file in the file-system corresponding to the
3930 ** target database in the file-system, delete it. If an error occurs,
3931 ** leave an error code and error message in the rbu handle.
3932 */
3933 static void rbuDeleteOalFile(sqlite3rbu *p){
3934 char *zOal = rbuMPrintf(p, "%s-oal", p->zTarget);
3935 if( zOal ){
3936 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
3937 assert( pVfs && p->rc==SQLITE_OK && p->zErrmsg==0 );
3938 pVfs->xDelete(pVfs, zOal, 0);
3939 sqlite3_free(zOal);
3940 }
3941 }
3942
3943 /*
3944 ** Allocate a private rbu VFS for the rbu handle passed as the only
3945 ** argument. This VFS will be used unless the call to sqlite3rbu_open()
3946 ** specified a URI with a vfs=? option in place of a target database
3947 ** file name.
3948 */
3949 static void rbuCreateVfs(sqlite3rbu *p){
3950 int rnd;
3951 char zRnd[64];
3952
3953 assert( p->rc==SQLITE_OK );
3954 sqlite3_randomness(sizeof(int), (void*)&rnd);
3955 sqlite3_snprintf(sizeof(zRnd), zRnd, "rbu_vfs_%d", rnd);
3956 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
3957 if( p->rc==SQLITE_OK ){
3958 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
3959 assert( pVfs );
3960 p->zVfsName = pVfs->zName;
3961 }
3962 }
3963
3964 /*
3965 ** Destroy the private VFS created for the rbu handle passed as the only
3966 ** argument by an earlier call to rbuCreateVfs().
3967 */
3968 static void rbuDeleteVfs(sqlite3rbu *p){
3969 if( p->zVfsName ){
3970 sqlite3rbu_destroy_vfs(p->zVfsName);
3971 p->zVfsName = 0;
3972 }
3973 }
3974
3975 /*
3976 ** This user-defined SQL function is invoked with a single argument - the
3977 ** name of a table expected to appear in the target database. It returns
3978 ** the number of auxilliary indexes on the table.
3979 */
3980 static void rbuIndexCntFunc(
3981 sqlite3_context *pCtx,
3982 int nVal,
3983 sqlite3_value **apVal
3984 ){
3985 sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
3986 sqlite3_stmt *pStmt = 0;
3987 char *zErrmsg = 0;
3988 int rc;
3989
3990 assert( nVal==1 );
3991
3992 rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &zErrmsg,
3993 sqlite3_mprintf("SELECT count(*) FROM sqlite_master "
3994 "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
3995 );
3996 if( rc!=SQLITE_OK ){
3997 sqlite3_result_error(pCtx, zErrmsg, -1);
3998 }else{
3999 int nIndex = 0;
4000 if( SQLITE_ROW==sqlite3_step(pStmt) ){
4001 nIndex = sqlite3_column_int(pStmt, 0);
4002 }
4003 rc = sqlite3_finalize(pStmt);
4004 if( rc==SQLITE_OK ){
4005 sqlite3_result_int(pCtx, nIndex);
4006 }else{
4007 sqlite3_result_error(pCtx, sqlite3_errmsg(p->dbMain), -1);
4008 }
4009 }
4010
4011 sqlite3_free(zErrmsg);
4012 }
4013
4014 /*
4015 ** If the RBU database contains the rbu_count table, use it to initialize
4016 ** the sqlite3rbu.nPhaseOneStep variable. The schema of the rbu_count table
4017 ** is assumed to contain the same columns as:
4018 **
4019 ** CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
4020 **
4021 ** There should be one row in the table for each data_xxx table in the
4022 ** database. The 'tbl' column should contain the name of a data_xxx table,
4023 ** and the cnt column the number of rows it contains.
4024 **
4025 ** sqlite3rbu.nPhaseOneStep is initialized to the sum of (1 + nIndex) * cnt
4026 ** for all rows in the rbu_count table, where nIndex is the number of
4027 ** indexes on the corresponding target database table.
4028 */
4029 static void rbuInitPhaseOneSteps(sqlite3rbu *p){
4030 if( p->rc==SQLITE_OK ){
4031 sqlite3_stmt *pStmt = 0;
4032 int bExists = 0; /* True if rbu_count exists */
4033
4034 p->nPhaseOneStep = -1;
4035
4036 p->rc = sqlite3_create_function(p->dbRbu,
4037 "rbu_index_cnt", 1, SQLITE_UTF8, (void*)p, rbuIndexCntFunc, 0, 0
4038 );
4039
4040 /* Check for the rbu_count table. If it does not exist, or if an error
4041 ** occurs, nPhaseOneStep will be left set to -1. */
4042 if( p->rc==SQLITE_OK ){
4043 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
4044 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'rbu_count'"
4045 );
4046 }
4047 if( p->rc==SQLITE_OK ){
4048 if( SQLITE_ROW==sqlite3_step(pStmt) ){
4049 bExists = 1;
4050 }
4051 p->rc = sqlite3_finalize(pStmt);
4052 }
4053
4054 if( p->rc==SQLITE_OK && bExists ){
4055 p->rc = prepareAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
4056 "SELECT sum(cnt * (1 + rbu_index_cnt(rbu_target_name(tbl))))"
4057 "FROM rbu_count"
4058 );
4059 if( p->rc==SQLITE_OK ){
4060 if( SQLITE_ROW==sqlite3_step(pStmt) ){
4061 p->nPhaseOneStep = sqlite3_column_int64(pStmt, 0);
4062 }
4063 p->rc = sqlite3_finalize(pStmt);
4064 }
4065 }
4066 }
4067 }
4068
4069
4070 static sqlite3rbu *openRbuHandle(
4071 const char *zTarget,
4072 const char *zRbu,
4073 const char *zState
4074 ){
4075 sqlite3rbu *p;
4076 size_t nTarget = zTarget ? strlen(zTarget) : 0;
4077 size_t nRbu = strlen(zRbu);
4078 size_t nByte = sizeof(sqlite3rbu) + nTarget+1 + nRbu+1;
4079
4080 p = (sqlite3rbu*)sqlite3_malloc64(nByte);
4081 if( p ){
4082 RbuState *pState = 0;
4083
4084 /* Create the custom VFS. */
4085 memset(p, 0, sizeof(sqlite3rbu));
4086 rbuCreateVfs(p);
4087
4088 /* Open the target, RBU and state databases */
4089 if( p->rc==SQLITE_OK ){
4090 char *pCsr = (char*)&p[1];
4091 int bRetry = 0;
4092 if( zTarget ){
4093 p->zTarget = pCsr;
4094 memcpy(p->zTarget, zTarget, nTarget+1);
4095 pCsr += nTarget+1;
4096 }
4097 p->zRbu = pCsr;
4098 memcpy(p->zRbu, zRbu, nRbu+1);
4099 pCsr += nRbu+1;
4100 if( zState ){
4101 p->zState = rbuMPrintf(p, "%s", zState);
4102 }
4103
4104 /* If the first attempt to open the database file fails and the bRetry
4105 ** flag it set, this means that the db was not opened because it seemed
4106 ** to be a wal-mode db. But, this may have happened due to an earlier
4107 ** RBU vacuum operation leaving an old wal file in the directory.
4108 ** If this is the case, it will have been checkpointed and deleted
4109 ** when the handle was closed and a second attempt to open the
4110 ** database may succeed. */
4111 rbuOpenDatabase(p, &bRetry);
4112 if( bRetry ){
4113 rbuOpenDatabase(p, 0);
4114 }
4115 }
4116
4117 if( p->rc==SQLITE_OK ){
4118 pState = rbuLoadState(p);
4119 assert( pState || p->rc!=SQLITE_OK );
4120 if( p->rc==SQLITE_OK ){
4121
4122 if( pState->eStage==0 ){
4123 rbuDeleteOalFile(p);
4124 rbuInitPhaseOneSteps(p);
4125 p->eStage = RBU_STAGE_OAL;
4126 }else{
4127 p->eStage = pState->eStage;
4128 p->nPhaseOneStep = pState->nPhaseOneStep;
4129 }
4130 p->nProgress = pState->nProgress;
4131 p->iOalSz = pState->iOalSz;
4132 }
4133 }
4134 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
4135
4136 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
4137 if( p->eStage==RBU_STAGE_OAL ){
4138 p->rc = SQLITE_ERROR;
4139 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
4140 }else if( p->eStage==RBU_STAGE_MOVE ){
4141 p->eStage = RBU_STAGE_CKPT;
4142 p->nStep = 0;
4143 }
4144 }
4145
4146 if( p->rc==SQLITE_OK
4147 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
4148 && pState->eStage!=0
4149 ){
4150 rbu_file *pFd = (rbuIsVacuum(p) ? p->pRbuFd : p->pTargetFd);
4151 if( pFd->iCookie!=pState->iCookie ){
4152 /* At this point (pTargetFd->iCookie) contains the value of the
4153 ** change-counter cookie (the thing that gets incremented when a
4154 ** transaction is committed in rollback mode) currently stored on
4155 ** page 1 of the database file. */
4156 p->rc = SQLITE_BUSY;
4157 p->zErrmsg = sqlite3_mprintf("database modified during rbu %s",
4158 (rbuIsVacuum(p) ? "vacuum" : "update")
4159 );
4160 }
4161 }
4162
4163 if( p->rc==SQLITE_OK ){
4164 if( p->eStage==RBU_STAGE_OAL ){
4165 sqlite3 *db = p->dbMain;
4166 p->rc = sqlite3_exec(p->dbRbu, "BEGIN", 0, 0, &p->zErrmsg);
4167
4168 /* Point the object iterator at the first object */
4169 if( p->rc==SQLITE_OK ){
4170 p->rc = rbuObjIterFirst(p, &p->objiter);
4171 }
4172
4173 /* If the RBU database contains no data_xxx tables, declare the RBU
4174 ** update finished. */
4175 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
4176 p->rc = SQLITE_DONE;
4177 p->eStage = RBU_STAGE_DONE;
4178 }else{
4179 if( p->rc==SQLITE_OK && pState->eStage==0 && rbuIsVacuum(p) ){
4180 rbuCopyPragma(p, "page_size");
4181 rbuCopyPragma(p, "auto_vacuum");
4182 }
4183
4184 /* Open transactions both databases. The *-oal file is opened or
4185 ** created at this point. */
4186 if( p->rc==SQLITE_OK ){
4187 p->rc = sqlite3_exec(db, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
4188 }
4189
4190 /* Check if the main database is a zipvfs db. If it is, set the upper
4191 ** level pager to use "journal_mode=off". This prevents it from
4192 ** generating a large journal using a temp file. */
4193 if( p->rc==SQLITE_OK ){
4194 int frc = sqlite3_file_control(db, "main", SQLITE_FCNTL_ZIPVFS, 0);
4195 if( frc==SQLITE_OK ){
4196 p->rc = sqlite3_exec(
4197 db, "PRAGMA journal_mode=off",0,0,&p->zErrmsg);
4198 }
4199 }
4200
4201 if( p->rc==SQLITE_OK ){
4202 rbuSetupOal(p, pState);
4203 }
4204 }
4205 }else if( p->eStage==RBU_STAGE_MOVE ){
4206 /* no-op */
4207 }else if( p->eStage==RBU_STAGE_CKPT ){
4208 rbuSetupCheckpoint(p, pState);
4209 }else if( p->eStage==RBU_STAGE_DONE ){
4210 p->rc = SQLITE_DONE;
4211 }else{
4212 p->rc = SQLITE_CORRUPT;
4213 }
4214 }
4215
4216 rbuFreeState(pState);
4217 }
4218
4219 return p;
4220 }
4221
4222 /*
4223 ** Allocate and return an RBU handle with all fields zeroed except for the
4224 ** error code, which is set to SQLITE_MISUSE.
4225 */
4226 static sqlite3rbu *rbuMisuseError(void){
4227 sqlite3rbu *pRet;
4228 pRet = sqlite3_malloc64(sizeof(sqlite3rbu));
4229 if( pRet ){
4230 memset(pRet, 0, sizeof(sqlite3rbu));
4231 pRet->rc = SQLITE_MISUSE;
4232 }
4233 return pRet;
4234 }
4235
4236 /*
4237 ** Open and return a new RBU handle.
4238 */
4239 SQLITE_API sqlite3rbu *sqlite3rbu_open(
4240 const char *zTarget,
4241 const char *zRbu,
4242 const char *zState
4243 ){
4244 if( zTarget==0 || zRbu==0 ){ return rbuMisuseError(); }
4245 /* TODO: Check that zTarget and zRbu are non-NULL */
4246 return openRbuHandle(zTarget, zRbu, zState);
4247 }
4248
4249 /*
4250 ** Open a handle to begin or resume an RBU VACUUM operation.
4251 */
4252 SQLITE_API sqlite3rbu *sqlite3rbu_vacuum(
4253 const char *zTarget,
4254 const char *zState
4255 ){
4256 if( zTarget==0 ){ return rbuMisuseError(); }
4257 /* TODO: Check that both arguments are non-NULL */
4258 return openRbuHandle(0, zTarget, zState);
4259 }
4260
4261 /*
4262 ** Return the database handle used by pRbu.
4263 */
4264 SQLITE_API sqlite3 *sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
4265 sqlite3 *db = 0;
4266 if( pRbu ){
4267 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
4268 }
4269 return db;
4270 }
4271
4272
4273 /*
4274 ** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
4275 ** then edit any error message string so as to remove all occurrences of
4276 ** the pattern "rbu_imp_[0-9]*".
4277 */
4278 static void rbuEditErrmsg(sqlite3rbu *p){
4279 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
4280 unsigned int i;
4281 size_t nErrmsg = strlen(p->zErrmsg);
4282 for(i=0; i<(nErrmsg-8); i++){
4283 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
4284 int nDel = 8;
4285 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
4286 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
4287 nErrmsg -= nDel;
4288 }
4289 }
4290 }
4291 }
4292
4293 /*
4294 ** Close the RBU handle.
4295 */
4296 SQLITE_API int sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
4297 int rc;
4298 if( p ){
4299
4300 /* Commit the transaction to the *-oal file. */
4301 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
4302 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
4303 }
4304
4305 rbuSaveState(p, p->eStage);
4306
4307 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
4308 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
4309 }
4310
4311 /* Close any open statement handles. */
4312 rbuObjIterFinalize(&p->objiter);
4313
4314 /* If this is an RBU vacuum handle and the vacuum has either finished
4315 ** successfully or encountered an error, delete the contents of the
4316 ** state table. This causes the next call to sqlite3rbu_vacuum()
4317 ** specifying the current target and state databases to start a new
4318 ** vacuum from scratch. */
4319 if( rbuIsVacuum(p) && p->rc!=SQLITE_OK && p->dbRbu ){
4320 int rc2 = sqlite3_exec(p->dbRbu, "DELETE FROM stat.rbu_state", 0, 0, 0);
4321 if( p->rc==SQLITE_DONE && rc2!=SQLITE_OK ) p->rc = rc2;
4322 }
4323
4324 /* Close the open database handle and VFS object. */
4325 sqlite3_close(p->dbRbu);
4326 sqlite3_close(p->dbMain);
4327 rbuDeleteVfs(p);
4328 sqlite3_free(p->aBuf);
4329 sqlite3_free(p->aFrame);
4330
4331 rbuEditErrmsg(p);
4332 rc = p->rc;
4333 *pzErrmsg = p->zErrmsg;
4334 sqlite3_free(p->zState);
4335 sqlite3_free(p);
4336 }else{
4337 rc = SQLITE_NOMEM;
4338 *pzErrmsg = 0;
4339 }
4340 return rc;
4341 }
4342
4343 /*
4344 ** Return the total number of key-value operations (inserts, deletes or
4345 ** updates) that have been performed on the target database since the
4346 ** current RBU update was started.
4347 */
4348 SQLITE_API sqlite3_int64 sqlite3rbu_progress(sqlite3rbu *pRbu){
4349 return pRbu->nProgress;
4350 }
4351
4352 /*
4353 ** Return permyriadage progress indications for the two main stages of
4354 ** an RBU update.
4355 */
4356 SQLITE_API void sqlite3rbu_bp_progress(sqlite3rbu *p, int *pnOne, int *pnTwo){
4357 const int MAX_PROGRESS = 10000;
4358 switch( p->eStage ){
4359 case RBU_STAGE_OAL:
4360 if( p->nPhaseOneStep>0 ){
4361 *pnOne = (int)(MAX_PROGRESS * (i64)p->nProgress/(i64)p->nPhaseOneStep);
4362 }else{
4363 *pnOne = -1;
4364 }
4365 *pnTwo = 0;
4366 break;
4367
4368 case RBU_STAGE_MOVE:
4369 *pnOne = MAX_PROGRESS;
4370 *pnTwo = 0;
4371 break;
4372
4373 case RBU_STAGE_CKPT:
4374 *pnOne = MAX_PROGRESS;
4375 *pnTwo = (int)(MAX_PROGRESS * (i64)p->nStep / (i64)p->nFrame);
4376 break;
4377
4378 case RBU_STAGE_DONE:
4379 *pnOne = MAX_PROGRESS;
4380 *pnTwo = MAX_PROGRESS;
4381 break;
4382
4383 default:
4384 assert( 0 );
4385 }
4386 }
4387
4388 /*
4389 ** Return the current state of the RBU vacuum or update operation.
4390 */
4391 SQLITE_API int sqlite3rbu_state(sqlite3rbu *p){
4392 int aRes[] = {
4393 0, SQLITE_RBU_STATE_OAL, SQLITE_RBU_STATE_MOVE,
4394 0, SQLITE_RBU_STATE_CHECKPOINT, SQLITE_RBU_STATE_DONE
4395 };
4396
4397 assert( RBU_STAGE_OAL==1 );
4398 assert( RBU_STAGE_MOVE==2 );
4399 assert( RBU_STAGE_CKPT==4 );
4400 assert( RBU_STAGE_DONE==5 );
4401 assert( aRes[RBU_STAGE_OAL]==SQLITE_RBU_STATE_OAL );
4402 assert( aRes[RBU_STAGE_MOVE]==SQLITE_RBU_STATE_MOVE );
4403 assert( aRes[RBU_STAGE_CKPT]==SQLITE_RBU_STATE_CHECKPOINT );
4404 assert( aRes[RBU_STAGE_DONE]==SQLITE_RBU_STATE_DONE );
4405
4406 if( p->rc!=SQLITE_OK && p->rc!=SQLITE_DONE ){
4407 return SQLITE_RBU_STATE_ERROR;
4408 }else{
4409 assert( p->rc!=SQLITE_DONE || p->eStage==RBU_STAGE_DONE );
4410 assert( p->eStage==RBU_STAGE_OAL
4411 || p->eStage==RBU_STAGE_MOVE
4412 || p->eStage==RBU_STAGE_CKPT
4413 || p->eStage==RBU_STAGE_DONE
4414 );
4415 return aRes[p->eStage];
4416 }
4417 }
4418
4419 SQLITE_API int sqlite3rbu_savestate(sqlite3rbu *p){
4420 int rc = p->rc;
4421 if( rc==SQLITE_DONE ) return SQLITE_OK;
4422
4423 assert( p->eStage>=RBU_STAGE_OAL && p->eStage<=RBU_STAGE_DONE );
4424 if( p->eStage==RBU_STAGE_OAL ){
4425 assert( rc!=SQLITE_DONE );
4426 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, 0);
4427 }
4428
4429 p->rc = rc;
4430 rbuSaveState(p, p->eStage);
4431 rc = p->rc;
4432
4433 if( p->eStage==RBU_STAGE_OAL ){
4434 assert( rc!=SQLITE_DONE );
4435 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, 0);
4436 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, 0);
4437 if( rc==SQLITE_OK ) rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0,0);
4438 }
4439
4440 p->rc = rc;
4441 return rc;
4442 }
4443
4444 /**************************************************************************
4445 ** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
4446 ** of a standard VFS in the following ways:
4447 **
4448 ** 1. Whenever the first page of a main database file is read or
4449 ** written, the value of the change-counter cookie is stored in
4450 ** rbu_file.iCookie. Similarly, the value of the "write-version"
4451 ** database header field is stored in rbu_file.iWriteVer. This ensures
4452 ** that the values are always trustworthy within an open transaction.
4453 **
4454 ** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
4455 ** member variable of the associated database file descriptor is set
4456 ** to point to the new file. A mutex protected linked list of all main
4457 ** db fds opened using a particular RBU VFS is maintained at
4458 ** rbu_vfs.pMain to facilitate this.
4459 **
4460 ** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
4461 ** object can be marked as the target database of an RBU update. This
4462 ** turns on the following extra special behaviour:
4463 **
4464 ** 3a. If xAccess() is called to check if there exists a *-wal file
4465 ** associated with an RBU target database currently in RBU_STAGE_OAL
4466 ** stage (preparing the *-oal file), the following special handling
4467 ** applies:
4468 **
4469 ** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
4470 ** target database may not be in wal mode already.
4471 **
4472 ** * if the *-wal file does not exist, set the output parameter to
4473 ** non-zero (to tell SQLite that it does exist) anyway.
4474 **
4475 ** Then, when xOpen() is called to open the *-wal file associated with
4476 ** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
4477 ** file, the rbu vfs opens the corresponding *-oal file instead.
4478 **
4479 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
4480 ** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
4481 ** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
4482 ** are no-ops on target database files in RBU_STAGE_OAL mode. This is
4483 ** because assert() statements in some VFS implementations fail if
4484 ** xShmLock() is called before xShmMap().
4485 **
4486 ** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
4487 ** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
4488 ** fails with an SQLITE_BUSY error. This is to stop RBU connections
4489 ** from automatically checkpointing a *-wal (or *-oal) file from within
4490 ** sqlite3_close().
4491 **
4492 ** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
4493 ** all xWrite() calls on the target database file perform no IO.
4494 ** Instead the frame and page numbers that would be read and written
4495 ** are recorded. Additionally, successful attempts to obtain exclusive
4496 ** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
4497 ** database file are recorded. xShmLock() calls to unlock the same
4498 ** locks are no-ops (so that once obtained, these locks are never
4499 ** relinquished). Finally, calls to xSync() on the target database
4500 ** file fail with SQLITE_INTERNAL errors.
4501 */
4502
4503 static void rbuUnlockShm(rbu_file *p){
4504 if( p->pRbu ){
4505 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
4506 int i;
4507 for(i=0; i<SQLITE_SHM_NLOCK;i++){
4508 if( (1<<i) & p->pRbu->mLock ){
4509 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
4510 }
4511 }
4512 p->pRbu->mLock = 0;
4513 }
4514 }
4515
4516 /*
4517 ** Close an rbu file.
4518 */
4519 static int rbuVfsClose(sqlite3_file *pFile){
4520 rbu_file *p = (rbu_file*)pFile;
4521 int rc;
4522 int i;
4523
4524 /* Free the contents of the apShm[] array. And the array itself. */
4525 for(i=0; i<p->nShm; i++){
4526 sqlite3_free(p->apShm[i]);
4527 }
4528 sqlite3_free(p->apShm);
4529 p->apShm = 0;
4530 sqlite3_free(p->zDel);
4531
4532 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
4533 rbu_file **pp;
4534 sqlite3_mutex_enter(p->pRbuVfs->mutex);
4535 for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
4536 *pp = p->pMainNext;
4537 sqlite3_mutex_leave(p->pRbuVfs->mutex);
4538 rbuUnlockShm(p);
4539 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
4540 }
4541
4542 /* Close the underlying file handle */
4543 rc = p->pReal->pMethods->xClose(p->pReal);
4544 return rc;
4545 }
4546
4547
4548 /*
4549 ** Read and return an unsigned 32-bit big-endian integer from the buffer
4550 ** passed as the only argument.
4551 */
4552 static u32 rbuGetU32(u8 *aBuf){
4553 return ((u32)aBuf[0] << 24)
4554 + ((u32)aBuf[1] << 16)
4555 + ((u32)aBuf[2] << 8)
4556 + ((u32)aBuf[3]);
4557 }
4558
4559 /*
4560 ** Write an unsigned 32-bit value in big-endian format to the supplied
4561 ** buffer.
4562 */
4563 static void rbuPutU32(u8 *aBuf, u32 iVal){
4564 aBuf[0] = (iVal >> 24) & 0xFF;
4565 aBuf[1] = (iVal >> 16) & 0xFF;
4566 aBuf[2] = (iVal >> 8) & 0xFF;
4567 aBuf[3] = (iVal >> 0) & 0xFF;
4568 }
4569
4570 static void rbuPutU16(u8 *aBuf, u16 iVal){
4571 aBuf[0] = (iVal >> 8) & 0xFF;
4572 aBuf[1] = (iVal >> 0) & 0xFF;
4573 }
4574
4575 /*
4576 ** Read data from an rbuVfs-file.
4577 */
4578 static int rbuVfsRead(
4579 sqlite3_file *pFile,
4580 void *zBuf,
4581 int iAmt,
4582 sqlite_int64 iOfst
4583 ){
4584 rbu_file *p = (rbu_file*)pFile;
4585 sqlite3rbu *pRbu = p->pRbu;
4586 int rc;
4587
4588 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
4589 assert( p->openFlags & SQLITE_OPEN_WAL );
4590 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
4591 }else{
4592 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
4593 && (p->openFlags & SQLITE_OPEN_WAL)
4594 && iOfst>=pRbu->iOalSz
4595 ){
4596 rc = SQLITE_OK;
4597 memset(zBuf, 0, iAmt);
4598 }else{
4599 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
4600 #if 1
4601 /* If this is being called to read the first page of the target
4602 ** database as part of an rbu vacuum operation, synthesize the
4603 ** contents of the first page if it does not yet exist. Otherwise,
4604 ** SQLite will not check for a *-wal file. */
4605 if( pRbu && rbuIsVacuum(pRbu)
4606 && rc==SQLITE_IOERR_SHORT_READ && iOfst==0
4607 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
4608 && pRbu->rc==SQLITE_OK
4609 ){
4610 sqlite3_file *pFd = (sqlite3_file*)pRbu->pRbuFd;
4611 rc = pFd->pMethods->xRead(pFd, zBuf, iAmt, iOfst);
4612 if( rc==SQLITE_OK ){
4613 u8 *aBuf = (u8*)zBuf;
4614 u32 iRoot = rbuGetU32(&aBuf[52]) ? 1 : 0;
4615 rbuPutU32(&aBuf[52], iRoot); /* largest root page number */
4616 rbuPutU32(&aBuf[36], 0); /* number of free pages */
4617 rbuPutU32(&aBuf[32], 0); /* first page on free list trunk */
4618 rbuPutU32(&aBuf[28], 1); /* size of db file in pages */
4619 rbuPutU32(&aBuf[24], pRbu->pRbuFd->iCookie+1); /* Change counter */
4620
4621 if( iAmt>100 ){
4622 memset(&aBuf[100], 0, iAmt-100);
4623 rbuPutU16(&aBuf[105], iAmt & 0xFFFF);
4624 aBuf[100] = 0x0D;
4625 }
4626 }
4627 }
4628 #endif
4629 }
4630 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
4631 /* These look like magic numbers. But they are stable, as they are part
4632 ** of the definition of the SQLite file format, which may not change. */
4633 u8 *pBuf = (u8*)zBuf;
4634 p->iCookie = rbuGetU32(&pBuf[24]);
4635 p->iWriteVer = pBuf[19];
4636 }
4637 }
4638 return rc;
4639 }
4640
4641 /*
4642 ** Write data to an rbuVfs-file.
4643 */
4644 static int rbuVfsWrite(
4645 sqlite3_file *pFile,
4646 const void *zBuf,
4647 int iAmt,
4648 sqlite_int64 iOfst
4649 ){
4650 rbu_file *p = (rbu_file*)pFile;
4651 sqlite3rbu *pRbu = p->pRbu;
4652 int rc;
4653
4654 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
4655 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
4656 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
4657 }else{
4658 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
4659 && (p->openFlags & SQLITE_OPEN_WAL)
4660 && iOfst>=pRbu->iOalSz
4661 ){
4662 pRbu->iOalSz = iAmt + iOfst;
4663 }
4664 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
4665 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
4666 /* These look like magic numbers. But they are stable, as they are part
4667 ** of the definition of the SQLite file format, which may not change. */
4668 u8 *pBuf = (u8*)zBuf;
4669 p->iCookie = rbuGetU32(&pBuf[24]);
4670 p->iWriteVer = pBuf[19];
4671 }
4672 }
4673 return rc;
4674 }
4675
4676 /*
4677 ** Truncate an rbuVfs-file.
4678 */
4679 static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
4680 rbu_file *p = (rbu_file*)pFile;
4681 return p->pReal->pMethods->xTruncate(p->pReal, size);
4682 }
4683
4684 /*
4685 ** Sync an rbuVfs-file.
4686 */
4687 static int rbuVfsSync(sqlite3_file *pFile, int flags){
4688 rbu_file *p = (rbu_file *)pFile;
4689 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
4690 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
4691 return SQLITE_INTERNAL;
4692 }
4693 return SQLITE_OK;
4694 }
4695 return p->pReal->pMethods->xSync(p->pReal, flags);
4696 }
4697
4698 /*
4699 ** Return the current file-size of an rbuVfs-file.
4700 */
4701 static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
4702 rbu_file *p = (rbu_file *)pFile;
4703 int rc;
4704 rc = p->pReal->pMethods->xFileSize(p->pReal, pSize);
4705
4706 /* If this is an RBU vacuum operation and this is the target database,
4707 ** pretend that it has at least one page. Otherwise, SQLite will not
4708 ** check for the existance of a *-wal file. rbuVfsRead() contains
4709 ** similar logic. */
4710 if( rc==SQLITE_OK && *pSize==0
4711 && p->pRbu && rbuIsVacuum(p->pRbu)
4712 && (p->openFlags & SQLITE_OPEN_MAIN_DB)
4713 ){
4714 *pSize = 1024;
4715 }
4716 return rc;
4717 }
4718
4719 /*
4720 ** Lock an rbuVfs-file.
4721 */
4722 static int rbuVfsLock(sqlite3_file *pFile, int eLock){
4723 rbu_file *p = (rbu_file*)pFile;
4724 sqlite3rbu *pRbu = p->pRbu;
4725 int rc = SQLITE_OK;
4726
4727 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
4728 if( eLock==SQLITE_LOCK_EXCLUSIVE
4729 && (p->bNolock || (pRbu && pRbu->eStage!=RBU_STAGE_DONE))
4730 ){
4731 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
4732 ** prevents it from checkpointing the database from sqlite3_close(). */
4733 rc = SQLITE_BUSY;
4734 }else{
4735 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
4736 }
4737
4738 return rc;
4739 }
4740
4741 /*
4742 ** Unlock an rbuVfs-file.
4743 */
4744 static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
4745 rbu_file *p = (rbu_file *)pFile;
4746 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
4747 }
4748
4749 /*
4750 ** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
4751 */
4752 static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
4753 rbu_file *p = (rbu_file *)pFile;
4754 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
4755 }
4756
4757 /*
4758 ** File control method. For custom operations on an rbuVfs-file.
4759 */
4760 static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
4761 rbu_file *p = (rbu_file *)pFile;
4762 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
4763 int rc;
4764
4765 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
4766 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
4767 );
4768 if( op==SQLITE_FCNTL_RBU ){
4769 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
4770
4771 /* First try to find another RBU vfs lower down in the vfs stack. If
4772 ** one is found, this vfs will operate in pass-through mode. The lower
4773 ** level vfs will do the special RBU handling. */
4774 rc = xControl(p->pReal, op, pArg);
4775
4776 if( rc==SQLITE_NOTFOUND ){
4777 /* Now search for a zipvfs instance lower down in the VFS stack. If
4778 ** one is found, this is an error. */
4779 void *dummy = 0;
4780 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
4781 if( rc==SQLITE_OK ){
4782 rc = SQLITE_ERROR;
4783 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
4784 }else if( rc==SQLITE_NOTFOUND ){
4785 pRbu->pTargetFd = p;
4786 p->pRbu = pRbu;
4787 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
4788 rc = SQLITE_OK;
4789 }
4790 }
4791 return rc;
4792 }
4793 else if( op==SQLITE_FCNTL_RBUCNT ){
4794 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
4795 pRbu->nRbu++;
4796 pRbu->pRbuFd = p;
4797 p->bNolock = 1;
4798 }
4799
4800 rc = xControl(p->pReal, op, pArg);
4801 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
4802 rbu_vfs *pRbuVfs = p->pRbuVfs;
4803 char *zIn = *(char**)pArg;
4804 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
4805 *(char**)pArg = zOut;
4806 if( zOut==0 ) rc = SQLITE_NOMEM;
4807 }
4808
4809 return rc;
4810 }
4811
4812 /*
4813 ** Return the sector-size in bytes for an rbuVfs-file.
4814 */
4815 static int rbuVfsSectorSize(sqlite3_file *pFile){
4816 rbu_file *p = (rbu_file *)pFile;
4817 return p->pReal->pMethods->xSectorSize(p->pReal);
4818 }
4819
4820 /*
4821 ** Return the device characteristic flags supported by an rbuVfs-file.
4822 */
4823 static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
4824 rbu_file *p = (rbu_file *)pFile;
4825 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
4826 }
4827
4828 /*
4829 ** Take or release a shared-memory lock.
4830 */
4831 static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
4832 rbu_file *p = (rbu_file*)pFile;
4833 sqlite3rbu *pRbu = p->pRbu;
4834 int rc = SQLITE_OK;
4835
4836 #ifdef SQLITE_AMALGAMATION
4837 assert( WAL_CKPT_LOCK==1 );
4838 #endif
4839
4840 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
4841 if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
4842 /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
4843 ** taking this lock also prevents any checkpoints from occurring.
4844 ** todo: really, it's not clear why this might occur, as
4845 ** wal_autocheckpoint ought to be turned off. */
4846 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
4847 }else{
4848 int bCapture = 0;
4849 if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
4850 && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
4851 && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
4852 ){
4853 bCapture = 1;
4854 }
4855
4856 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
4857 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
4858 if( bCapture && rc==SQLITE_OK ){
4859 pRbu->mLock |= (1 << ofst);
4860 }
4861 }
4862 }
4863
4864 return rc;
4865 }
4866
4867 /*
4868 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
4869 */
4870 static int rbuVfsShmMap(
4871 sqlite3_file *pFile,
4872 int iRegion,
4873 int szRegion,
4874 int isWrite,
4875 void volatile **pp
4876 ){
4877 rbu_file *p = (rbu_file*)pFile;
4878 int rc = SQLITE_OK;
4879 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
4880
4881 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
4882 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
4883 ** instead of a file on disk. */
4884 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
4885 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
4886 if( iRegion<=p->nShm ){
4887 int nByte = (iRegion+1) * sizeof(char*);
4888 char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
4889 if( apNew==0 ){
4890 rc = SQLITE_NOMEM;
4891 }else{
4892 memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
4893 p->apShm = apNew;
4894 p->nShm = iRegion+1;
4895 }
4896 }
4897
4898 if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
4899 char *pNew = (char*)sqlite3_malloc64(szRegion);
4900 if( pNew==0 ){
4901 rc = SQLITE_NOMEM;
4902 }else{
4903 memset(pNew, 0, szRegion);
4904 p->apShm[iRegion] = pNew;
4905 }
4906 }
4907
4908 if( rc==SQLITE_OK ){
4909 *pp = p->apShm[iRegion];
4910 }else{
4911 *pp = 0;
4912 }
4913 }else{
4914 assert( p->apShm==0 );
4915 rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp);
4916 }
4917
4918 return rc;
4919 }
4920
4921 /*
4922 ** Memory barrier.
4923 */
4924 static void rbuVfsShmBarrier(sqlite3_file *pFile){
4925 rbu_file *p = (rbu_file *)pFile;
4926 p->pReal->pMethods->xShmBarrier(p->pReal);
4927 }
4928
4929 /*
4930 ** The xShmUnmap method.
4931 */
4932 static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
4933 rbu_file *p = (rbu_file*)pFile;
4934 int rc = SQLITE_OK;
4935 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
4936
4937 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
4938 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
4939 /* no-op */
4940 }else{
4941 /* Release the checkpointer and writer locks */
4942 rbuUnlockShm(p);
4943 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
4944 }
4945 return rc;
4946 }
4947
4948 /*
4949 ** Given that zWal points to a buffer containing a wal file name passed to
4950 ** either the xOpen() or xAccess() VFS method, return a pointer to the
4951 ** file-handle opened by the same database connection on the corresponding
4952 ** database file.
4953 */
4954 static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
4955 rbu_file *pDb;
4956 sqlite3_mutex_enter(pRbuVfs->mutex);
4957 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext){}
4958 sqlite3_mutex_leave(pRbuVfs->mutex);
4959 return pDb;
4960 }
4961
4962 /*
4963 ** A main database named zName has just been opened. The following
4964 ** function returns a pointer to a buffer owned by SQLite that contains
4965 ** the name of the *-wal file this db connection will use. SQLite
4966 ** happens to pass a pointer to this buffer when using xAccess()
4967 ** or xOpen() to operate on the *-wal file.
4968 */
4969 static const char *rbuMainToWal(const char *zName, int flags){
4970 int n = (int)strlen(zName);
4971 const char *z = &zName[n];
4972 if( flags & SQLITE_OPEN_URI ){
4973 int odd = 0;
4974 while( 1 ){
4975 if( z[0]==0 ){
4976 odd = 1 - odd;
4977 if( odd && z[1]==0 ) break;
4978 }
4979 z++;
4980 }
4981 z += 2;
4982 }else{
4983 while( *z==0 ) z++;
4984 }
4985 z += (n + 8 + 1);
4986 return z;
4987 }
4988
4989 /*
4990 ** Open an rbu file handle.
4991 */
4992 static int rbuVfsOpen(
4993 sqlite3_vfs *pVfs,
4994 const char *zName,
4995 sqlite3_file *pFile,
4996 int flags,
4997 int *pOutFlags
4998 ){
4999 static sqlite3_io_methods rbuvfs_io_methods = {
5000 2, /* iVersion */
5001 rbuVfsClose, /* xClose */
5002 rbuVfsRead, /* xRead */
5003 rbuVfsWrite, /* xWrite */
5004 rbuVfsTruncate, /* xTruncate */
5005 rbuVfsSync, /* xSync */
5006 rbuVfsFileSize, /* xFileSize */
5007 rbuVfsLock, /* xLock */
5008 rbuVfsUnlock, /* xUnlock */
5009 rbuVfsCheckReservedLock, /* xCheckReservedLock */
5010 rbuVfsFileControl, /* xFileControl */
5011 rbuVfsSectorSize, /* xSectorSize */
5012 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
5013 rbuVfsShmMap, /* xShmMap */
5014 rbuVfsShmLock, /* xShmLock */
5015 rbuVfsShmBarrier, /* xShmBarrier */
5016 rbuVfsShmUnmap, /* xShmUnmap */
5017 0, 0 /* xFetch, xUnfetch */
5018 };
5019 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
5020 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
5021 rbu_file *pFd = (rbu_file *)pFile;
5022 int rc = SQLITE_OK;
5023 const char *zOpen = zName;
5024 int oflags = flags;
5025
5026 memset(pFd, 0, sizeof(rbu_file));
5027 pFd->pReal = (sqlite3_file*)&pFd[1];
5028 pFd->pRbuVfs = pRbuVfs;
5029 pFd->openFlags = flags;
5030 if( zName ){
5031 if( flags & SQLITE_OPEN_MAIN_DB ){
5032 /* A main database has just been opened. The following block sets
5033 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
5034 ** the name of the *-wal file this db connection will use. SQLite
5035 ** happens to pass a pointer to this buffer when using xAccess()
5036 ** or xOpen() to operate on the *-wal file. */
5037 pFd->zWal = rbuMainToWal(zName, flags);
5038 }
5039 else if( flags & SQLITE_OPEN_WAL ){
5040 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
5041 if( pDb ){
5042 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
5043 /* This call is to open a *-wal file. Intead, open the *-oal. This
5044 ** code ensures that the string passed to xOpen() is terminated by a
5045 ** pair of '\0' bytes in case the VFS attempts to extract a URI
5046 ** parameter from it. */
5047 const char *zBase = zName;
5048 size_t nCopy;
5049 char *zCopy;
5050 if( rbuIsVacuum(pDb->pRbu) ){
5051 zBase = sqlite3_db_filename(pDb->pRbu->dbRbu, "main");
5052 zBase = rbuMainToWal(zBase, SQLITE_OPEN_URI);
5053 }
5054 nCopy = strlen(zBase);
5055 zCopy = sqlite3_malloc64(nCopy+2);
5056 if( zCopy ){
5057 memcpy(zCopy, zBase, nCopy);
5058 zCopy[nCopy-3] = 'o';
5059 zCopy[nCopy] = '\0';
5060 zCopy[nCopy+1] = '\0';
5061 zOpen = (const char*)(pFd->zDel = zCopy);
5062 }else{
5063 rc = SQLITE_NOMEM;
5064 }
5065 pFd->pRbu = pDb->pRbu;
5066 }
5067 pDb->pWalFd = pFd;
5068 }
5069 }
5070 }
5071
5072 if( oflags & SQLITE_OPEN_MAIN_DB
5073 && sqlite3_uri_boolean(zName, "rbu_memory", 0)
5074 ){
5075 assert( oflags & SQLITE_OPEN_MAIN_DB );
5076 oflags = SQLITE_OPEN_TEMP_DB | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
5077 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
5078 zOpen = 0;
5079 }
5080
5081 if( rc==SQLITE_OK ){
5082 rc = pRealVfs->xOpen(pRealVfs, zOpen, pFd->pReal, oflags, pOutFlags);
5083 }
5084 if( pFd->pReal->pMethods ){
5085 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
5086 ** pointer and, if the file is a main database file, link it into the
5087 ** mutex protected linked list of all such files. */
5088 pFile->pMethods = &rbuvfs_io_methods;
5089 if( flags & SQLITE_OPEN_MAIN_DB ){
5090 sqlite3_mutex_enter(pRbuVfs->mutex);
5091 pFd->pMainNext = pRbuVfs->pMain;
5092 pRbuVfs->pMain = pFd;
5093 sqlite3_mutex_leave(pRbuVfs->mutex);
5094 }
5095 }else{
5096 sqlite3_free(pFd->zDel);
5097 }
5098
5099 return rc;
5100 }
5101
5102 /*
5103 ** Delete the file located at zPath.
5104 */
5105 static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
5106 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5107 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
5108 }
5109
5110 /*
5111 ** Test for access permissions. Return true if the requested permission
5112 ** is available, or false otherwise.
5113 */
5114 static int rbuVfsAccess(
5115 sqlite3_vfs *pVfs,
5116 const char *zPath,
5117 int flags,
5118 int *pResOut
5119 ){
5120 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
5121 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
5122 int rc;
5123
5124 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
5125
5126 /* If this call is to check if a *-wal file associated with an RBU target
5127 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
5128 ** the following special handling is activated:
5129 **
5130 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
5131 ** ensures that the RBU extension never tries to update a database
5132 ** in wal mode, even if the first page of the database file has
5133 ** been damaged.
5134 **
5135 ** b) if the *-wal file does not exist, claim that it does anyway,
5136 ** causing SQLite to call xOpen() to open it. This call will also
5137 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
5138 ** file opened instead.
5139 */
5140 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
5141 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
5142 if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
5143 if( *pResOut ){
5144 rc = SQLITE_CANTOPEN;
5145 }else{
5146 *pResOut = 1;
5147 }
5148 }
5149 }
5150
5151 return rc;
5152 }
5153
5154 /*
5155 ** Populate buffer zOut with the full canonical pathname corresponding
5156 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
5157 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
5158 */
5159 static int rbuVfsFullPathname(
5160 sqlite3_vfs *pVfs,
5161 const char *zPath,
5162 int nOut,
5163 char *zOut
5164 ){
5165 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5166 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
5167 }
5168
5169 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5170 /*
5171 ** Open the dynamic library located at zPath and return a handle.
5172 */
5173 static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
5174 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5175 return pRealVfs->xDlOpen(pRealVfs, zPath);
5176 }
5177
5178 /*
5179 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
5180 ** utf-8 string describing the most recent error encountered associated
5181 ** with dynamic libraries.
5182 */
5183 static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
5184 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5185 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
5186 }
5187
5188 /*
5189 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
5190 */
5191 static void (*rbuVfsDlSym(
5192 sqlite3_vfs *pVfs,
5193 void *pArg,
5194 const char *zSym
5195 ))(void){
5196 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5197 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
5198 }
5199
5200 /*
5201 ** Close the dynamic library handle pHandle.
5202 */
5203 static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
5204 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5205 pRealVfs->xDlClose(pRealVfs, pHandle);
5206 }
5207 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
5208
5209 /*
5210 ** Populate the buffer pointed to by zBufOut with nByte bytes of
5211 ** random data.
5212 */
5213 static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
5214 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5215 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
5216 }
5217
5218 /*
5219 ** Sleep for nMicro microseconds. Return the number of microseconds
5220 ** actually slept.
5221 */
5222 static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
5223 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5224 return pRealVfs->xSleep(pRealVfs, nMicro);
5225 }
5226
5227 /*
5228 ** Return the current time as a Julian Day number in *pTimeOut.
5229 */
5230 static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
5231 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
5232 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
5233 }
5234
5235 /*
5236 ** No-op.
5237 */
5238 static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
5239 return 0;
5240 }
5241
5242 /*
5243 ** Deregister and destroy an RBU vfs created by an earlier call to
5244 ** sqlite3rbu_create_vfs().
5245 */
5246 SQLITE_API void sqlite3rbu_destroy_vfs(const char *zName){
5247 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
5248 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
5249 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
5250 sqlite3_vfs_unregister(pVfs);
5251 sqlite3_free(pVfs);
5252 }
5253 }
5254
5255 /*
5256 ** Create an RBU VFS named zName that accesses the underlying file-system
5257 ** via existing VFS zParent. The new object is registered as a non-default
5258 ** VFS with SQLite before returning.
5259 */
5260 SQLITE_API int sqlite3rbu_create_vfs(const char *zName, const char *zParent){
5261
5262 /* Template for VFS */
5263 static sqlite3_vfs vfs_template = {
5264 1, /* iVersion */
5265 0, /* szOsFile */
5266 0, /* mxPathname */
5267 0, /* pNext */
5268 0, /* zName */
5269 0, /* pAppData */
5270 rbuVfsOpen, /* xOpen */
5271 rbuVfsDelete, /* xDelete */
5272 rbuVfsAccess, /* xAccess */
5273 rbuVfsFullPathname, /* xFullPathname */
5274
5275 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5276 rbuVfsDlOpen, /* xDlOpen */
5277 rbuVfsDlError, /* xDlError */
5278 rbuVfsDlSym, /* xDlSym */
5279 rbuVfsDlClose, /* xDlClose */
5280 #else
5281 0, 0, 0, 0,
5282 #endif
5283
5284 rbuVfsRandomness, /* xRandomness */
5285 rbuVfsSleep, /* xSleep */
5286 rbuVfsCurrentTime, /* xCurrentTime */
5287 rbuVfsGetLastError, /* xGetLastError */
5288 0, /* xCurrentTimeInt64 (version 2) */
5289 0, 0, 0 /* Unimplemented version 3 methods */
5290 };
5291
5292 rbu_vfs *pNew = 0; /* Newly allocated VFS */
5293 int rc = SQLITE_OK;
5294 size_t nName;
5295 size_t nByte;
5296
5297 nName = strlen(zName);
5298 nByte = sizeof(rbu_vfs) + nName + 1;
5299 pNew = (rbu_vfs*)sqlite3_malloc64(nByte);
5300 if( pNew==0 ){
5301 rc = SQLITE_NOMEM;
5302 }else{
5303 sqlite3_vfs *pParent; /* Parent VFS */
5304 memset(pNew, 0, nByte);
5305 pParent = sqlite3_vfs_find(zParent);
5306 if( pParent==0 ){
5307 rc = SQLITE_NOTFOUND;
5308 }else{
5309 char *zSpace;
5310 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
5311 pNew->base.mxPathname = pParent->mxPathname;
5312 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
5313 pNew->pRealVfs = pParent;
5314 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
5315 memcpy(zSpace, zName, nName);
5316
5317 /* Allocate the mutex and register the new VFS (not as the default) */
5318 pNew->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
5319 if( pNew->mutex==0 ){
5320 rc = SQLITE_NOMEM;
5321 }else{
5322 rc = sqlite3_vfs_register(&pNew->base, 0);
5323 }
5324 }
5325
5326 if( rc!=SQLITE_OK ){
5327 sqlite3_mutex_free(pNew->mutex);
5328 sqlite3_free(pNew);
5329 }
5330 }
5331
5332 return rc;
5333 }
5334
5335
5336 /**************************************************************************/
5337
5338 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
5339
5340 /************** End of sqlite3rbu.c ******************************************/
5341 /************** Begin file dbstat.c ******************************************/
5342 /*
5343 ** 2010 July 12
5344 **
5345 ** The author disclaims copyright to this source code. In place of
5346 ** a legal notice, here is a blessing:
5347 **
5348 ** May you do good and not evil.
5349 ** May you find forgiveness for yourself and forgive others.
5350 ** May you share freely, never taking more than you give.
5351 **
5352 ******************************************************************************
5353 **
5354 ** This file contains an implementation of the "dbstat" virtual table.
5355 **
5356 ** The dbstat virtual table is used to extract low-level formatting
5357 ** information from an SQLite database in order to implement the
5358 ** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
5359 ** for an example implementation.
5360 **
5361 ** Additional information is available on the "dbstat.html" page of the
5362 ** official SQLite documentation.
5363 */
5364
5365 /* #include "sqliteInt.h" ** Requires access to internal data structures ** */
5366 #if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
5367 && !defined(SQLITE_OMIT_VIRTUALTABLE)
5368
5369 /*
5370 ** Page paths:
5371 **
5372 ** The value of the 'path' column describes the path taken from the
5373 ** root-node of the b-tree structure to each page. The value of the
5374 ** root-node path is '/'.
5375 **
5376 ** The value of the path for the left-most child page of the root of
5377 ** a b-tree is '/000/'. (Btrees store content ordered from left to right
5378 ** so the pages to the left have smaller keys than the pages to the right.)
5379 ** The next to left-most child of the root page is
5380 ** '/001', and so on, each sibling page identified by a 3-digit hex
5381 ** value. The children of the 451st left-most sibling have paths such
5382 ** as '/1c2/000/, '/1c2/001/' etc.
5383 **
5384 ** Overflow pages are specified by appending a '+' character and a
5385 ** six-digit hexadecimal value to the path to the cell they are linked
5386 ** from. For example, the three overflow pages in a chain linked from
5387 ** the left-most cell of the 450th child of the root page are identified
5388 ** by the paths:
5389 **
5390 ** '/1c2/000+000000' // First page in overflow chain
5391 ** '/1c2/000+000001' // Second page in overflow chain
5392 ** '/1c2/000+000002' // Third page in overflow chain
5393 **
5394 ** If the paths are sorted using the BINARY collation sequence, then
5395 ** the overflow pages associated with a cell will appear earlier in the
5396 ** sort-order than its child page:
5397 **
5398 ** '/1c2/000/' // Left-most child of 451st child of root
5399 */
5400 #define VTAB_SCHEMA \
5401 "CREATE TABLE xx( " \
5402 " name TEXT, /* Name of table or index */" \
5403 " path TEXT, /* Path to page from root */" \
5404 " pageno INTEGER, /* Page number */" \
5405 " pagetype TEXT, /* 'internal', 'leaf' or 'overflow' */" \
5406 " ncell INTEGER, /* Cells on page (0 for overflow) */" \
5407 " payload INTEGER, /* Bytes of payload on this page */" \
5408 " unused INTEGER, /* Bytes of unused space on this page */" \
5409 " mx_payload INTEGER, /* Largest payload size of all cells */" \
5410 " pgoffset INTEGER, /* Offset of page in file */" \
5411 " pgsize INTEGER, /* Size of the page */" \
5412 " schema TEXT HIDDEN /* Database schema being analyzed */" \
5413 ");"
5414
5415
5416 typedef struct StatTable StatTable;
5417 typedef struct StatCursor StatCursor;
5418 typedef struct StatPage StatPage;
5419 typedef struct StatCell StatCell;
5420
5421 struct StatCell {
5422 int nLocal; /* Bytes of local payload */
5423 u32 iChildPg; /* Child node (or 0 if this is a leaf) */
5424 int nOvfl; /* Entries in aOvfl[] */
5425 u32 *aOvfl; /* Array of overflow page numbers */
5426 int nLastOvfl; /* Bytes of payload on final overflow page */
5427 int iOvfl; /* Iterates through aOvfl[] */
5428 };
5429
5430 struct StatPage {
5431 u32 iPgno;
5432 DbPage *pPg;
5433 int iCell;
5434
5435 char *zPath; /* Path to this page */
5436
5437 /* Variables populated by statDecodePage(): */
5438 u8 flags; /* Copy of flags byte */
5439 int nCell; /* Number of cells on page */
5440 int nUnused; /* Number of unused bytes on page */
5441 StatCell *aCell; /* Array of parsed cells */
5442 u32 iRightChildPg; /* Right-child page number (or 0) */
5443 int nMxPayload; /* Largest payload of any cell on this page */
5444 };
5445
5446 struct StatCursor {
5447 sqlite3_vtab_cursor base;
5448 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
5449 int isEof; /* After pStmt has returned SQLITE_DONE */
5450 int iDb; /* Schema used for this query */
5451
5452 StatPage aPage[32];
5453 int iPage; /* Current entry in aPage[] */
5454
5455 /* Values to return. */
5456 char *zName; /* Value of 'name' column */
5457 char *zPath; /* Value of 'path' column */
5458 u32 iPageno; /* Value of 'pageno' column */
5459 char *zPagetype; /* Value of 'pagetype' column */
5460 int nCell; /* Value of 'ncell' column */
5461 int nPayload; /* Value of 'payload' column */
5462 int nUnused; /* Value of 'unused' column */
5463 int nMxPayload; /* Value of 'mx_payload' column */
5464 i64 iOffset; /* Value of 'pgOffset' column */
5465 int szPage; /* Value of 'pgSize' column */
5466 };
5467
5468 struct StatTable {
5469 sqlite3_vtab base;
5470 sqlite3 *db;
5471 int iDb; /* Index of database to analyze */
5472 };
5473
5474 #ifndef get2byte
5475 # define get2byte(x) ((x)[0]<<8 | (x)[1])
5476 #endif
5477
5478 /*
5479 ** Connect to or create a statvfs virtual table.
5480 */
5481 static int statConnect(
5482 sqlite3 *db,
5483 void *pAux,
5484 int argc, const char *const*argv,
5485 sqlite3_vtab **ppVtab,
5486 char **pzErr
5487 ){
5488 StatTable *pTab = 0;
5489 int rc = SQLITE_OK;
5490 int iDb;
5491
5492 if( argc>=4 ){
5493 Token nm;
5494 sqlite3TokenInit(&nm, (char*)argv[3]);
5495 iDb = sqlite3FindDb(db, &nm);
5496 if( iDb<0 ){
5497 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
5498 return SQLITE_ERROR;
5499 }
5500 }else{
5501 iDb = 0;
5502 }
5503 rc = sqlite3_declare_vtab(db, VTAB_SCHEMA);
5504 if( rc==SQLITE_OK ){
5505 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
5506 if( pTab==0 ) rc = SQLITE_NOMEM_BKPT;
5507 }
5508
5509 assert( rc==SQLITE_OK || pTab==0 );
5510 if( rc==SQLITE_OK ){
5511 memset(pTab, 0, sizeof(StatTable));
5512 pTab->db = db;
5513 pTab->iDb = iDb;
5514 }
5515
5516 *ppVtab = (sqlite3_vtab*)pTab;
5517 return rc;
5518 }
5519
5520 /*
5521 ** Disconnect from or destroy a statvfs virtual table.
5522 */
5523 static int statDisconnect(sqlite3_vtab *pVtab){
5524 sqlite3_free(pVtab);
5525 return SQLITE_OK;
5526 }
5527
5528 /*
5529 ** There is no "best-index". This virtual table always does a linear
5530 ** scan. However, a schema=? constraint should cause this table to
5531 ** operate on a different database schema, so check for it.
5532 **
5533 ** idxNum is normally 0, but will be 1 if a schema=? constraint exists.
5534 */
5535 static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
5536 int i;
5537
5538 pIdxInfo->estimatedCost = 1.0e6; /* Initial cost estimate */
5539
5540 /* Look for a valid schema=? constraint. If found, change the idxNum to
5541 ** 1 and request the value of that constraint be sent to xFilter. And
5542 ** lower the cost estimate to encourage the constrained version to be
5543 ** used.
5544 */
5545 for(i=0; i<pIdxInfo->nConstraint; i++){
5546 if( pIdxInfo->aConstraint[i].usable==0 ) continue;
5547 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
5548 if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue;
5549 pIdxInfo->idxNum = 1;
5550 pIdxInfo->estimatedCost = 1.0;
5551 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
5552 pIdxInfo->aConstraintUsage[i].omit = 1;
5553 break;
5554 }
5555
5556
5557 /* Records are always returned in ascending order of (name, path).
5558 ** If this will satisfy the client, set the orderByConsumed flag so that
5559 ** SQLite does not do an external sort.
5560 */
5561 if( ( pIdxInfo->nOrderBy==1
5562 && pIdxInfo->aOrderBy[0].iColumn==0
5563 && pIdxInfo->aOrderBy[0].desc==0
5564 ) ||
5565 ( pIdxInfo->nOrderBy==2
5566 && pIdxInfo->aOrderBy[0].iColumn==0
5567 && pIdxInfo->aOrderBy[0].desc==0
5568 && pIdxInfo->aOrderBy[1].iColumn==1
5569 && pIdxInfo->aOrderBy[1].desc==0
5570 )
5571 ){
5572 pIdxInfo->orderByConsumed = 1;
5573 }
5574
5575 return SQLITE_OK;
5576 }
5577
5578 /*
5579 ** Open a new statvfs cursor.
5580 */
5581 static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
5582 StatTable *pTab = (StatTable *)pVTab;
5583 StatCursor *pCsr;
5584
5585 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
5586 if( pCsr==0 ){
5587 return SQLITE_NOMEM_BKPT;
5588 }else{
5589 memset(pCsr, 0, sizeof(StatCursor));
5590 pCsr->base.pVtab = pVTab;
5591 pCsr->iDb = pTab->iDb;
5592 }
5593
5594 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
5595 return SQLITE_OK;
5596 }
5597
5598 static void statClearPage(StatPage *p){
5599 int i;
5600 if( p->aCell ){
5601 for(i=0; i<p->nCell; i++){
5602 sqlite3_free(p->aCell[i].aOvfl);
5603 }
5604 sqlite3_free(p->aCell);
5605 }
5606 sqlite3PagerUnref(p->pPg);
5607 sqlite3_free(p->zPath);
5608 memset(p, 0, sizeof(StatPage));
5609 }
5610
5611 static void statResetCsr(StatCursor *pCsr){
5612 int i;
5613 sqlite3_reset(pCsr->pStmt);
5614 for(i=0; i<ArraySize(pCsr->aPage); i++){
5615 statClearPage(&pCsr->aPage[i]);
5616 }
5617 pCsr->iPage = 0;
5618 sqlite3_free(pCsr->zPath);
5619 pCsr->zPath = 0;
5620 pCsr->isEof = 0;
5621 }
5622
5623 /*
5624 ** Close a statvfs cursor.
5625 */
5626 static int statClose(sqlite3_vtab_cursor *pCursor){
5627 StatCursor *pCsr = (StatCursor *)pCursor;
5628 statResetCsr(pCsr);
5629 sqlite3_finalize(pCsr->pStmt);
5630 sqlite3_free(pCsr);
5631 return SQLITE_OK;
5632 }
5633
5634 static void getLocalPayload(
5635 int nUsable, /* Usable bytes per page */
5636 u8 flags, /* Page flags */
5637 int nTotal, /* Total record (payload) size */
5638 int *pnLocal /* OUT: Bytes stored locally */
5639 ){
5640 int nLocal;
5641 int nMinLocal;
5642 int nMaxLocal;
5643
5644 if( flags==0x0D ){ /* Table leaf node */
5645 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
5646 nMaxLocal = nUsable - 35;
5647 }else{ /* Index interior and leaf nodes */
5648 nMinLocal = (nUsable - 12) * 32 / 255 - 23;
5649 nMaxLocal = (nUsable - 12) * 64 / 255 - 23;
5650 }
5651
5652 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
5653 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
5654 *pnLocal = nLocal;
5655 }
5656
5657 static int statDecodePage(Btree *pBt, StatPage *p){
5658 int nUnused;
5659 int iOff;
5660 int nHdr;
5661 int isLeaf;
5662 int szPage;
5663
5664 u8 *aData = sqlite3PagerGetData(p->pPg);
5665 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
5666
5667 p->flags = aHdr[0];
5668 p->nCell = get2byte(&aHdr[3]);
5669 p->nMxPayload = 0;
5670
5671 isLeaf = (p->flags==0x0A || p->flags==0x0D);
5672 nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
5673
5674 nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell;
5675 nUnused += (int)aHdr[7];
5676 iOff = get2byte(&aHdr[1]);
5677 while( iOff ){
5678 nUnused += get2byte(&aData[iOff+2]);
5679 iOff = get2byte(&aData[iOff]);
5680 }
5681 p->nUnused = nUnused;
5682 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
5683 szPage = sqlite3BtreeGetPageSize(pBt);
5684
5685 if( p->nCell ){
5686 int i; /* Used to iterate through cells */
5687 int nUsable; /* Usable bytes per page */
5688
5689 sqlite3BtreeEnter(pBt);
5690 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
5691 sqlite3BtreeLeave(pBt);
5692 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
5693 if( p->aCell==0 ) return SQLITE_NOMEM_BKPT;
5694 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
5695
5696 for(i=0; i<p->nCell; i++){
5697 StatCell *pCell = &p->aCell[i];
5698
5699 iOff = get2byte(&aData[nHdr+i*2]);
5700 if( !isLeaf ){
5701 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
5702 iOff += 4;
5703 }
5704 if( p->flags==0x05 ){
5705 /* A table interior node. nPayload==0. */
5706 }else{
5707 u32 nPayload; /* Bytes of payload total (local+overflow) */
5708 int nLocal; /* Bytes of payload stored locally */
5709 iOff += getVarint32(&aData[iOff], nPayload);
5710 if( p->flags==0x0D ){
5711 u64 dummy;
5712 iOff += sqlite3GetVarint(&aData[iOff], &dummy);
5713 }
5714 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
5715 getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
5716 pCell->nLocal = nLocal;
5717 assert( nLocal>=0 );
5718 assert( nPayload>=(u32)nLocal );
5719 assert( nLocal<=(nUsable-35) );
5720 if( nPayload>(u32)nLocal ){
5721 int j;
5722 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
5723 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
5724 pCell->nOvfl = nOvfl;
5725 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
5726 if( pCell->aOvfl==0 ) return SQLITE_NOMEM_BKPT;
5727 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
5728 for(j=1; j<nOvfl; j++){
5729 int rc;
5730 u32 iPrev = pCell->aOvfl[j-1];
5731 DbPage *pPg = 0;
5732 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
5733 if( rc!=SQLITE_OK ){
5734 assert( pPg==0 );
5735 return rc;
5736 }
5737 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
5738 sqlite3PagerUnref(pPg);
5739 }
5740 }
5741 }
5742 }
5743 }
5744
5745 return SQLITE_OK;
5746 }
5747
5748 /*
5749 ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
5750 ** the current value of pCsr->iPageno.
5751 */
5752 static void statSizeAndOffset(StatCursor *pCsr){
5753 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
5754 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
5755 Pager *pPager = sqlite3BtreePager(pBt);
5756 sqlite3_file *fd;
5757 sqlite3_int64 x[2];
5758
5759 /* The default page size and offset */
5760 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
5761 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
5762
5763 /* If connected to a ZIPVFS backend, override the page size and
5764 ** offset with actual values obtained from ZIPVFS.
5765 */
5766 fd = sqlite3PagerFile(pPager);
5767 x[0] = pCsr->iPageno;
5768 if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
5769 pCsr->iOffset = x[0];
5770 pCsr->szPage = (int)x[1];
5771 }
5772 }
5773
5774 /*
5775 ** Move a statvfs cursor to the next entry in the file.
5776 */
5777 static int statNext(sqlite3_vtab_cursor *pCursor){
5778 int rc;
5779 int nPayload;
5780 char *z;
5781 StatCursor *pCsr = (StatCursor *)pCursor;
5782 StatTable *pTab = (StatTable *)pCursor->pVtab;
5783 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
5784 Pager *pPager = sqlite3BtreePager(pBt);
5785
5786 sqlite3_free(pCsr->zPath);
5787 pCsr->zPath = 0;
5788
5789 statNextRestart:
5790 if( pCsr->aPage[0].pPg==0 ){
5791 rc = sqlite3_step(pCsr->pStmt);
5792 if( rc==SQLITE_ROW ){
5793 int nPage;
5794 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
5795 sqlite3PagerPagecount(pPager, &nPage);
5796 if( nPage==0 ){
5797 pCsr->isEof = 1;
5798 return sqlite3_reset(pCsr->pStmt);
5799 }
5800 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
5801 pCsr->aPage[0].iPgno = iRoot;
5802 pCsr->aPage[0].iCell = 0;
5803 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
5804 pCsr->iPage = 0;
5805 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
5806 }else{
5807 pCsr->isEof = 1;
5808 return sqlite3_reset(pCsr->pStmt);
5809 }
5810 }else{
5811
5812 /* Page p itself has already been visited. */
5813 StatPage *p = &pCsr->aPage[pCsr->iPage];
5814
5815 while( p->iCell<p->nCell ){
5816 StatCell *pCell = &p->aCell[p->iCell];
5817 if( pCell->iOvfl<pCell->nOvfl ){
5818 int nUsable;
5819 sqlite3BtreeEnter(pBt);
5820 nUsable = sqlite3BtreeGetPageSize(pBt) -
5821 sqlite3BtreeGetReserveNoMutex(pBt);
5822 sqlite3BtreeLeave(pBt);
5823 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
5824 pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
5825 pCsr->zPagetype = "overflow";
5826 pCsr->nCell = 0;
5827 pCsr->nMxPayload = 0;
5828 pCsr->zPath = z = sqlite3_mprintf(
5829 "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
5830 );
5831 if( pCell->iOvfl<pCell->nOvfl-1 ){
5832 pCsr->nUnused = 0;
5833 pCsr->nPayload = nUsable - 4;
5834 }else{
5835 pCsr->nPayload = pCell->nLastOvfl;
5836 pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
5837 }
5838 pCell->iOvfl++;
5839 statSizeAndOffset(pCsr);
5840 return z==0 ? SQLITE_NOMEM_BKPT : SQLITE_OK;
5841 }
5842 if( p->iRightChildPg ) break;
5843 p->iCell++;
5844 }
5845
5846 if( !p->iRightChildPg || p->iCell>p->nCell ){
5847 statClearPage(p);
5848 if( pCsr->iPage==0 ) return statNext(pCursor);
5849 pCsr->iPage--;
5850 goto statNextRestart; /* Tail recursion */
5851 }
5852 pCsr->iPage++;
5853 assert( p==&pCsr->aPage[pCsr->iPage-1] );
5854
5855 if( p->iCell==p->nCell ){
5856 p[1].iPgno = p->iRightChildPg;
5857 }else{
5858 p[1].iPgno = p->aCell[p->iCell].iChildPg;
5859 }
5860 rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0);
5861 p[1].iCell = 0;
5862 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
5863 p->iCell++;
5864 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
5865 }
5866
5867
5868 /* Populate the StatCursor fields with the values to be returned
5869 ** by the xColumn() and xRowid() methods.
5870 */
5871 if( rc==SQLITE_OK ){
5872 int i;
5873 StatPage *p = &pCsr->aPage[pCsr->iPage];
5874 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
5875 pCsr->iPageno = p->iPgno;
5876
5877 rc = statDecodePage(pBt, p);
5878 if( rc==SQLITE_OK ){
5879 statSizeAndOffset(pCsr);
5880
5881 switch( p->flags ){
5882 case 0x05: /* table internal */
5883 case 0x02: /* index internal */
5884 pCsr->zPagetype = "internal";
5885 break;
5886 case 0x0D: /* table leaf */
5887 case 0x0A: /* index leaf */
5888 pCsr->zPagetype = "leaf";
5889 break;
5890 default:
5891 pCsr->zPagetype = "corrupted";
5892 break;
5893 }
5894 pCsr->nCell = p->nCell;
5895 pCsr->nUnused = p->nUnused;
5896 pCsr->nMxPayload = p->nMxPayload;
5897 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
5898 if( z==0 ) rc = SQLITE_NOMEM_BKPT;
5899 nPayload = 0;
5900 for(i=0; i<p->nCell; i++){
5901 nPayload += p->aCell[i].nLocal;
5902 }
5903 pCsr->nPayload = nPayload;
5904 }
5905 }
5906
5907 return rc;
5908 }
5909
5910 static int statEof(sqlite3_vtab_cursor *pCursor){
5911 StatCursor *pCsr = (StatCursor *)pCursor;
5912 return pCsr->isEof;
5913 }
5914
5915 static int statFilter(
5916 sqlite3_vtab_cursor *pCursor,
5917 int idxNum, const char *idxStr,
5918 int argc, sqlite3_value **argv
5919 ){
5920 StatCursor *pCsr = (StatCursor *)pCursor;
5921 StatTable *pTab = (StatTable*)(pCursor->pVtab);
5922 char *zSql;
5923 int rc = SQLITE_OK;
5924 char *zMaster;
5925
5926 if( idxNum==1 ){
5927 const char *zDbase = (const char*)sqlite3_value_text(argv[0]);
5928 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
5929 if( pCsr->iDb<0 ){
5930 sqlite3_free(pCursor->pVtab->zErrMsg);
5931 pCursor->pVtab->zErrMsg = sqlite3_mprintf("no such schema: %s", zDbase);
5932 return pCursor->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM_BKPT;
5933 }
5934 }else{
5935 pCsr->iDb = pTab->iDb;
5936 }
5937 statResetCsr(pCsr);
5938 sqlite3_finalize(pCsr->pStmt);
5939 pCsr->pStmt = 0;
5940 zMaster = pCsr->iDb==1 ? "sqlite_temp_master" : "sqlite_master";
5941 zSql = sqlite3_mprintf(
5942 "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
5943 " UNION ALL "
5944 "SELECT name, rootpage, type"
5945 " FROM \"%w\".%s WHERE rootpage!=0"
5946 " ORDER BY name", pTab->db->aDb[pCsr->iDb].zDbSName, zMaster);
5947 if( zSql==0 ){
5948 return SQLITE_NOMEM_BKPT;
5949 }else{
5950 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
5951 sqlite3_free(zSql);
5952 }
5953
5954 if( rc==SQLITE_OK ){
5955 rc = statNext(pCursor);
5956 }
5957 return rc;
5958 }
5959
5960 static int statColumn(
5961 sqlite3_vtab_cursor *pCursor,
5962 sqlite3_context *ctx,
5963 int i
5964 ){
5965 StatCursor *pCsr = (StatCursor *)pCursor;
5966 switch( i ){
5967 case 0: /* name */
5968 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
5969 break;
5970 case 1: /* path */
5971 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
5972 break;
5973 case 2: /* pageno */
5974 sqlite3_result_int64(ctx, pCsr->iPageno);
5975 break;
5976 case 3: /* pagetype */
5977 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
5978 break;
5979 case 4: /* ncell */
5980 sqlite3_result_int(ctx, pCsr->nCell);
5981 break;
5982 case 5: /* payload */
5983 sqlite3_result_int(ctx, pCsr->nPayload);
5984 break;
5985 case 6: /* unused */
5986 sqlite3_result_int(ctx, pCsr->nUnused);
5987 break;
5988 case 7: /* mx_payload */
5989 sqlite3_result_int(ctx, pCsr->nMxPayload);
5990 break;
5991 case 8: /* pgoffset */
5992 sqlite3_result_int64(ctx, pCsr->iOffset);
5993 break;
5994 case 9: /* pgsize */
5995 sqlite3_result_int(ctx, pCsr->szPage);
5996 break;
5997 default: { /* schema */
5998 sqlite3 *db = sqlite3_context_db_handle(ctx);
5999 int iDb = pCsr->iDb;
6000 sqlite3_result_text(ctx, db->aDb[iDb].zDbSName, -1, SQLITE_STATIC);
6001 break;
6002 }
6003 }
6004 return SQLITE_OK;
6005 }
6006
6007 static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
6008 StatCursor *pCsr = (StatCursor *)pCursor;
6009 *pRowid = pCsr->iPageno;
6010 return SQLITE_OK;
6011 }
6012
6013 /*
6014 ** Invoke this routine to register the "dbstat" virtual table module
6015 */
6016 SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){
6017 static sqlite3_module dbstat_module = {
6018 0, /* iVersion */
6019 statConnect, /* xCreate */
6020 statConnect, /* xConnect */
6021 statBestIndex, /* xBestIndex */
6022 statDisconnect, /* xDisconnect */
6023 statDisconnect, /* xDestroy */
6024 statOpen, /* xOpen - open a cursor */
6025 statClose, /* xClose - close a cursor */
6026 statFilter, /* xFilter - configure scan constraints */
6027 statNext, /* xNext - advance a cursor */
6028 statEof, /* xEof - check for end of scan */
6029 statColumn, /* xColumn - read data */
6030 statRowid, /* xRowid - read data */
6031 0, /* xUpdate */
6032 0, /* xBegin */
6033 0, /* xSync */
6034 0, /* xCommit */
6035 0, /* xRollback */
6036 0, /* xFindMethod */
6037 0, /* xRename */
6038 };
6039 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
6040 }
6041 #elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
6042 SQLITE_PRIVATE int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
6043 #endif /* SQLITE_ENABLE_DBSTAT_VTAB */
6044
6045 /************** End of dbstat.c **********************************************/
6046 /************** Begin file sqlite3session.c **********************************/
6047
6048 #if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
6049 /* #include "sqlite3session.h" */
6050 /* #include <assert.h> */
6051 /* #include <string.h> */
6052
6053 #ifndef SQLITE_AMALGAMATION
6054 /* # include "sqliteInt.h" */
6055 /* # include "vdbeInt.h" */
6056 #endif
6057
6058 typedef struct SessionTable SessionTable;
6059 typedef struct SessionChange SessionChange;
6060 typedef struct SessionBuffer SessionBuffer;
6061 typedef struct SessionInput SessionInput;
6062
6063 /*
6064 ** Minimum chunk size used by streaming versions of functions.
6065 */
6066 #ifndef SESSIONS_STRM_CHUNK_SIZE
6067 # ifdef SQLITE_TEST
6068 # define SESSIONS_STRM_CHUNK_SIZE 64
6069 # else
6070 # define SESSIONS_STRM_CHUNK_SIZE 1024
6071 # endif
6072 #endif
6073
6074 typedef struct SessionHook SessionHook;
6075 struct SessionHook {
6076 void *pCtx;
6077 int (*xOld)(void*,int,sqlite3_value**);
6078 int (*xNew)(void*,int,sqlite3_value**);
6079 int (*xCount)(void*);
6080 int (*xDepth)(void*);
6081 };
6082
6083 /*
6084 ** Session handle structure.
6085 */
6086 struct sqlite3_session {
6087 sqlite3 *db; /* Database handle session is attached to */
6088 char *zDb; /* Name of database session is attached to */
6089 int bEnable; /* True if currently recording */
6090 int bIndirect; /* True if all changes are indirect */
6091 int bAutoAttach; /* True to auto-attach tables */
6092 int rc; /* Non-zero if an error has occurred */
6093 void *pFilterCtx; /* First argument to pass to xTableFilter */
6094 int (*xTableFilter)(void *pCtx, const char *zTab);
6095 sqlite3_session *pNext; /* Next session object on same db. */
6096 SessionTable *pTable; /* List of attached tables */
6097 SessionHook hook; /* APIs to grab new and old data with */
6098 };
6099
6100 /*
6101 ** Instances of this structure are used to build strings or binary records.
6102 */
6103 struct SessionBuffer {
6104 u8 *aBuf; /* Pointer to changeset buffer */
6105 int nBuf; /* Size of buffer aBuf */
6106 int nAlloc; /* Size of allocation containing aBuf */
6107 };
6108
6109 /*
6110 ** An object of this type is used internally as an abstraction for
6111 ** input data. Input data may be supplied either as a single large buffer
6112 ** (e.g. sqlite3changeset_start()) or using a stream function (e.g.
6113 ** sqlite3changeset_start_strm()).
6114 */
6115 struct SessionInput {
6116 int bNoDiscard; /* If true, discard no data */
6117 int iCurrent; /* Offset in aData[] of current change */
6118 int iNext; /* Offset in aData[] of next change */
6119 u8 *aData; /* Pointer to buffer containing changeset */
6120 int nData; /* Number of bytes in aData */
6121
6122 SessionBuffer buf; /* Current read buffer */
6123 int (*xInput)(void*, void*, int*); /* Input stream call (or NULL) */
6124 void *pIn; /* First argument to xInput */
6125 int bEof; /* Set to true after xInput finished */
6126 };
6127
6128 /*
6129 ** Structure for changeset iterators.
6130 */
6131 struct sqlite3_changeset_iter {
6132 SessionInput in; /* Input buffer or stream */
6133 SessionBuffer tblhdr; /* Buffer to hold apValue/zTab/abPK/ */
6134 int bPatchset; /* True if this is a patchset */
6135 int rc; /* Iterator error code */
6136 sqlite3_stmt *pConflict; /* Points to conflicting row, if any */
6137 char *zTab; /* Current table */
6138 int nCol; /* Number of columns in zTab */
6139 int op; /* Current operation */
6140 int bIndirect; /* True if current change was indirect */
6141 u8 *abPK; /* Primary key array */
6142 sqlite3_value **apValue; /* old.* and new.* values */
6143 };
6144
6145 /*
6146 ** Each session object maintains a set of the following structures, one
6147 ** for each table the session object is monitoring. The structures are
6148 ** stored in a linked list starting at sqlite3_session.pTable.
6149 **
6150 ** The keys of the SessionTable.aChange[] hash table are all rows that have
6151 ** been modified in any way since the session object was attached to the
6152 ** table.
6153 **
6154 ** The data associated with each hash-table entry is a structure containing
6155 ** a subset of the initial values that the modified row contained at the
6156 ** start of the session. Or no initial values if the row was inserted.
6157 */
6158 struct SessionTable {
6159 SessionTable *pNext;
6160 char *zName; /* Local name of table */
6161 int nCol; /* Number of columns in table zName */
6162 const char **azCol; /* Column names */
6163 u8 *abPK; /* Array of primary key flags */
6164 int nEntry; /* Total number of entries in hash table */
6165 int nChange; /* Size of apChange[] array */
6166 SessionChange **apChange; /* Hash table buckets */
6167 };
6168
6169 /*
6170 ** RECORD FORMAT:
6171 **
6172 ** The following record format is similar to (but not compatible with) that
6173 ** used in SQLite database files. This format is used as part of the
6174 ** change-set binary format, and so must be architecture independent.
6175 **
6176 ** Unlike the SQLite database record format, each field is self-contained -
6177 ** there is no separation of header and data. Each field begins with a
6178 ** single byte describing its type, as follows:
6179 **
6180 ** 0x00: Undefined value.
6181 ** 0x01: Integer value.
6182 ** 0x02: Real value.
6183 ** 0x03: Text value.
6184 ** 0x04: Blob value.
6185 ** 0x05: SQL NULL value.
6186 **
6187 ** Note that the above match the definitions of SQLITE_INTEGER, SQLITE_TEXT
6188 ** and so on in sqlite3.h. For undefined and NULL values, the field consists
6189 ** only of the single type byte. For other types of values, the type byte
6190 ** is followed by:
6191 **
6192 ** Text values:
6193 ** A varint containing the number of bytes in the value (encoded using
6194 ** UTF-8). Followed by a buffer containing the UTF-8 representation
6195 ** of the text value. There is no nul terminator.
6196 **
6197 ** Blob values:
6198 ** A varint containing the number of bytes in the value, followed by
6199 ** a buffer containing the value itself.
6200 **
6201 ** Integer values:
6202 ** An 8-byte big-endian integer value.
6203 **
6204 ** Real values:
6205 ** An 8-byte big-endian IEEE 754-2008 real value.
6206 **
6207 ** Varint values are encoded in the same way as varints in the SQLite
6208 ** record format.
6209 **
6210 ** CHANGESET FORMAT:
6211 **
6212 ** A changeset is a collection of DELETE, UPDATE and INSERT operations on
6213 ** one or more tables. Operations on a single table are grouped together,
6214 ** but may occur in any order (i.e. deletes, updates and inserts are all
6215 ** mixed together).
6216 **
6217 ** Each group of changes begins with a table header:
6218 **
6219 ** 1 byte: Constant 0x54 (capital 'T')
6220 ** Varint: Number of columns in the table.
6221 ** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
6222 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
6223 **
6224 ** Followed by one or more changes to the table.
6225 **
6226 ** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
6227 ** 1 byte: The "indirect-change" flag.
6228 ** old.* record: (delete and update only)
6229 ** new.* record: (insert and update only)
6230 **
6231 ** The "old.*" and "new.*" records, if present, are N field records in the
6232 ** format described above under "RECORD FORMAT", where N is the number of
6233 ** columns in the table. The i'th field of each record is associated with
6234 ** the i'th column of the table, counting from left to right in the order
6235 ** in which columns were declared in the CREATE TABLE statement.
6236 **
6237 ** The new.* record that is part of each INSERT change contains the values
6238 ** that make up the new row. Similarly, the old.* record that is part of each
6239 ** DELETE change contains the values that made up the row that was deleted
6240 ** from the database. In the changeset format, the records that are part
6241 ** of INSERT or DELETE changes never contain any undefined (type byte 0x00)
6242 ** fields.
6243 **
6244 ** Within the old.* record associated with an UPDATE change, all fields
6245 ** associated with table columns that are not PRIMARY KEY columns and are
6246 ** not modified by the UPDATE change are set to "undefined". Other fields
6247 ** are set to the values that made up the row before the UPDATE that the
6248 ** change records took place. Within the new.* record, fields associated
6249 ** with table columns modified by the UPDATE change contain the new
6250 ** values. Fields associated with table columns that are not modified
6251 ** are set to "undefined".
6252 **
6253 ** PATCHSET FORMAT:
6254 **
6255 ** A patchset is also a collection of changes. It is similar to a changeset,
6256 ** but leaves undefined those fields that are not useful if no conflict
6257 ** resolution is required when applying the changeset.
6258 **
6259 ** Each group of changes begins with a table header:
6260 **
6261 ** 1 byte: Constant 0x50 (capital 'P')
6262 ** Varint: Number of columns in the table.
6263 ** nCol bytes: 0x01 for PK columns, 0x00 otherwise.
6264 ** N bytes: Unqualified table name (encoded using UTF-8). Nul-terminated.
6265 **
6266 ** Followed by one or more changes to the table.
6267 **
6268 ** 1 byte: Either SQLITE_INSERT (0x12), UPDATE (0x17) or DELETE (0x09).
6269 ** 1 byte: The "indirect-change" flag.
6270 ** single record: (PK fields for DELETE, PK and modified fields for UPDATE,
6271 ** full record for INSERT).
6272 **
6273 ** As in the changeset format, each field of the single record that is part
6274 ** of a patchset change is associated with the correspondingly positioned
6275 ** table column, counting from left to right within the CREATE TABLE
6276 ** statement.
6277 **
6278 ** For a DELETE change, all fields within the record except those associated
6279 ** with PRIMARY KEY columns are set to "undefined". The PRIMARY KEY fields
6280 ** contain the values identifying the row to delete.
6281 **
6282 ** For an UPDATE change, all fields except those associated with PRIMARY KEY
6283 ** columns and columns that are modified by the UPDATE are set to "undefined".
6284 ** PRIMARY KEY fields contain the values identifying the table row to update,
6285 ** and fields associated with modified columns contain the new column values.
6286 **
6287 ** The records associated with INSERT changes are in the same format as for
6288 ** changesets. It is not possible for a record associated with an INSERT
6289 ** change to contain a field set to "undefined".
6290 */
6291
6292 /*
6293 ** For each row modified during a session, there exists a single instance of
6294 ** this structure stored in a SessionTable.aChange[] hash table.
6295 */
6296 struct SessionChange {
6297 int op; /* One of UPDATE, DELETE, INSERT */
6298 int bIndirect; /* True if this change is "indirect" */
6299 int nRecord; /* Number of bytes in buffer aRecord[] */
6300 u8 *aRecord; /* Buffer containing old.* record */
6301 SessionChange *pNext; /* For hash-table collisions */
6302 };
6303
6304 /*
6305 ** Write a varint with value iVal into the buffer at aBuf. Return the
6306 ** number of bytes written.
6307 */
6308 static int sessionVarintPut(u8 *aBuf, int iVal){
6309 return putVarint32(aBuf, iVal);
6310 }
6311
6312 /*
6313 ** Return the number of bytes required to store value iVal as a varint.
6314 */
6315 static int sessionVarintLen(int iVal){
6316 return sqlite3VarintLen(iVal);
6317 }
6318
6319 /*
6320 ** Read a varint value from aBuf[] into *piVal. Return the number of
6321 ** bytes read.
6322 */
6323 static int sessionVarintGet(u8 *aBuf, int *piVal){
6324 return getVarint32(aBuf, *piVal);
6325 }
6326
6327 /* Load an unaligned and unsigned 32-bit integer */
6328 #define SESSION_UINT32(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
6329
6330 /*
6331 ** Read a 64-bit big-endian integer value from buffer aRec[]. Return
6332 ** the value read.
6333 */
6334 static sqlite3_int64 sessionGetI64(u8 *aRec){
6335 u64 x = SESSION_UINT32(aRec);
6336 u32 y = SESSION_UINT32(aRec+4);
6337 x = (x<<32) + y;
6338 return (sqlite3_int64)x;
6339 }
6340
6341 /*
6342 ** Write a 64-bit big-endian integer value to the buffer aBuf[].
6343 */
6344 static void sessionPutI64(u8 *aBuf, sqlite3_int64 i){
6345 aBuf[0] = (i>>56) & 0xFF;
6346 aBuf[1] = (i>>48) & 0xFF;
6347 aBuf[2] = (i>>40) & 0xFF;
6348 aBuf[3] = (i>>32) & 0xFF;
6349 aBuf[4] = (i>>24) & 0xFF;
6350 aBuf[5] = (i>>16) & 0xFF;
6351 aBuf[6] = (i>> 8) & 0xFF;
6352 aBuf[7] = (i>> 0) & 0xFF;
6353 }
6354
6355 /*
6356 ** This function is used to serialize the contents of value pValue (see
6357 ** comment titled "RECORD FORMAT" above).
6358 **
6359 ** If it is non-NULL, the serialized form of the value is written to
6360 ** buffer aBuf. *pnWrite is set to the number of bytes written before
6361 ** returning. Or, if aBuf is NULL, the only thing this function does is
6362 ** set *pnWrite.
6363 **
6364 ** If no error occurs, SQLITE_OK is returned. Or, if an OOM error occurs
6365 ** within a call to sqlite3_value_text() (may fail if the db is utf-16))
6366 ** SQLITE_NOMEM is returned.
6367 */
6368 static int sessionSerializeValue(
6369 u8 *aBuf, /* If non-NULL, write serialized value here */
6370 sqlite3_value *pValue, /* Value to serialize */
6371 int *pnWrite /* IN/OUT: Increment by bytes written */
6372 ){
6373 int nByte; /* Size of serialized value in bytes */
6374
6375 if( pValue ){
6376 int eType; /* Value type (SQLITE_NULL, TEXT etc.) */
6377
6378 eType = sqlite3_value_type(pValue);
6379 if( aBuf ) aBuf[0] = eType;
6380
6381 switch( eType ){
6382 case SQLITE_NULL:
6383 nByte = 1;
6384 break;
6385
6386 case SQLITE_INTEGER:
6387 case SQLITE_FLOAT:
6388 if( aBuf ){
6389 /* TODO: SQLite does something special to deal with mixed-endian
6390 ** floating point values (e.g. ARM7). This code probably should
6391 ** too. */
6392 u64 i;
6393 if( eType==SQLITE_INTEGER ){
6394 i = (u64)sqlite3_value_int64(pValue);
6395 }else{
6396 double r;
6397 assert( sizeof(double)==8 && sizeof(u64)==8 );
6398 r = sqlite3_value_double(pValue);
6399 memcpy(&i, &r, 8);
6400 }
6401 sessionPutI64(&aBuf[1], i);
6402 }
6403 nByte = 9;
6404 break;
6405
6406 default: {
6407 u8 *z;
6408 int n;
6409 int nVarint;
6410
6411 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
6412 if( eType==SQLITE_TEXT ){
6413 z = (u8 *)sqlite3_value_text(pValue);
6414 }else{
6415 z = (u8 *)sqlite3_value_blob(pValue);
6416 }
6417 n = sqlite3_value_bytes(pValue);
6418 if( z==0 && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
6419 nVarint = sessionVarintLen(n);
6420
6421 if( aBuf ){
6422 sessionVarintPut(&aBuf[1], n);
6423 if( n ) memcpy(&aBuf[nVarint + 1], z, n);
6424 }
6425
6426 nByte = 1 + nVarint + n;
6427 break;
6428 }
6429 }
6430 }else{
6431 nByte = 1;
6432 if( aBuf ) aBuf[0] = '\0';
6433 }
6434
6435 if( pnWrite ) *pnWrite += nByte;
6436 return SQLITE_OK;
6437 }
6438
6439
6440 /*
6441 ** This macro is used to calculate hash key values for data structures. In
6442 ** order to use this macro, the entire data structure must be represented
6443 ** as a series of unsigned integers. In order to calculate a hash-key value
6444 ** for a data structure represented as three such integers, the macro may
6445 ** then be used as follows:
6446 **
6447 ** int hash_key_value;
6448 ** hash_key_value = HASH_APPEND(0, <value 1>);
6449 ** hash_key_value = HASH_APPEND(hash_key_value, <value 2>);
6450 ** hash_key_value = HASH_APPEND(hash_key_value, <value 3>);
6451 **
6452 ** In practice, the data structures this macro is used for are the primary
6453 ** key values of modified rows.
6454 */
6455 #define HASH_APPEND(hash, add) ((hash) << 3) ^ (hash) ^ (unsigned int)(add)
6456
6457 /*
6458 ** Append the hash of the 64-bit integer passed as the second argument to the
6459 ** hash-key value passed as the first. Return the new hash-key value.
6460 */
6461 static unsigned int sessionHashAppendI64(unsigned int h, i64 i){
6462 h = HASH_APPEND(h, i & 0xFFFFFFFF);
6463 return HASH_APPEND(h, (i>>32)&0xFFFFFFFF);
6464 }
6465
6466 /*
6467 ** Append the hash of the blob passed via the second and third arguments to
6468 ** the hash-key value passed as the first. Return the new hash-key value.
6469 */
6470 static unsigned int sessionHashAppendBlob(unsigned int h, int n, const u8 *z){
6471 int i;
6472 for(i=0; i<n; i++) h = HASH_APPEND(h, z[i]);
6473 return h;
6474 }
6475
6476 /*
6477 ** Append the hash of the data type passed as the second argument to the
6478 ** hash-key value passed as the first. Return the new hash-key value.
6479 */
6480 static unsigned int sessionHashAppendType(unsigned int h, int eType){
6481 return HASH_APPEND(h, eType);
6482 }
6483
6484 /*
6485 ** This function may only be called from within a pre-update callback.
6486 ** It calculates a hash based on the primary key values of the old.* or
6487 ** new.* row currently available and, assuming no error occurs, writes it to
6488 ** *piHash before returning. If the primary key contains one or more NULL
6489 ** values, *pbNullPK is set to true before returning.
6490 **
6491 ** If an error occurs, an SQLite error code is returned and the final values
6492 ** of *piHash asn *pbNullPK are undefined. Otherwise, SQLITE_OK is returned
6493 ** and the output variables are set as described above.
6494 */
6495 static int sessionPreupdateHash(
6496 sqlite3_session *pSession, /* Session object that owns pTab */
6497 SessionTable *pTab, /* Session table handle */
6498 int bNew, /* True to hash the new.* PK */
6499 int *piHash, /* OUT: Hash value */
6500 int *pbNullPK /* OUT: True if there are NULL values in PK */
6501 ){
6502 unsigned int h = 0; /* Hash value to return */
6503 int i; /* Used to iterate through columns */
6504
6505 assert( *pbNullPK==0 );
6506 assert( pTab->nCol==pSession->hook.xCount(pSession->hook.pCtx) );
6507 for(i=0; i<pTab->nCol; i++){
6508 if( pTab->abPK[i] ){
6509 int rc;
6510 int eType;
6511 sqlite3_value *pVal;
6512
6513 if( bNew ){
6514 rc = pSession->hook.xNew(pSession->hook.pCtx, i, &pVal);
6515 }else{
6516 rc = pSession->hook.xOld(pSession->hook.pCtx, i, &pVal);
6517 }
6518 if( rc!=SQLITE_OK ) return rc;
6519
6520 eType = sqlite3_value_type(pVal);
6521 h = sessionHashAppendType(h, eType);
6522 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
6523 i64 iVal;
6524 if( eType==SQLITE_INTEGER ){
6525 iVal = sqlite3_value_int64(pVal);
6526 }else{
6527 double rVal = sqlite3_value_double(pVal);
6528 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
6529 memcpy(&iVal, &rVal, 8);
6530 }
6531 h = sessionHashAppendI64(h, iVal);
6532 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
6533 const u8 *z;
6534 int n;
6535 if( eType==SQLITE_TEXT ){
6536 z = (const u8 *)sqlite3_value_text(pVal);
6537 }else{
6538 z = (const u8 *)sqlite3_value_blob(pVal);
6539 }
6540 n = sqlite3_value_bytes(pVal);
6541 if( !z && (eType!=SQLITE_BLOB || n>0) ) return SQLITE_NOMEM;
6542 h = sessionHashAppendBlob(h, n, z);
6543 }else{
6544 assert( eType==SQLITE_NULL );
6545 *pbNullPK = 1;
6546 }
6547 }
6548 }
6549
6550 *piHash = (h % pTab->nChange);
6551 return SQLITE_OK;
6552 }
6553
6554 /*
6555 ** The buffer that the argument points to contains a serialized SQL value.
6556 ** Return the number of bytes of space occupied by the value (including
6557 ** the type byte).
6558 */
6559 static int sessionSerialLen(u8 *a){
6560 int e = *a;
6561 int n;
6562 if( e==0 ) return 1;
6563 if( e==SQLITE_NULL ) return 1;
6564 if( e==SQLITE_INTEGER || e==SQLITE_FLOAT ) return 9;
6565 return sessionVarintGet(&a[1], &n) + 1 + n;
6566 }
6567
6568 /*
6569 ** Based on the primary key values stored in change aRecord, calculate a
6570 ** hash key. Assume the has table has nBucket buckets. The hash keys
6571 ** calculated by this function are compatible with those calculated by
6572 ** sessionPreupdateHash().
6573 **
6574 ** The bPkOnly argument is non-zero if the record at aRecord[] is from
6575 ** a patchset DELETE. In this case the non-PK fields are omitted entirely.
6576 */
6577 static unsigned int sessionChangeHash(
6578 SessionTable *pTab, /* Table handle */
6579 int bPkOnly, /* Record consists of PK fields only */
6580 u8 *aRecord, /* Change record */
6581 int nBucket /* Assume this many buckets in hash table */
6582 ){
6583 unsigned int h = 0; /* Value to return */
6584 int i; /* Used to iterate through columns */
6585 u8 *a = aRecord; /* Used to iterate through change record */
6586
6587 for(i=0; i<pTab->nCol; i++){
6588 int eType = *a;
6589 int isPK = pTab->abPK[i];
6590 if( bPkOnly && isPK==0 ) continue;
6591
6592 /* It is not possible for eType to be SQLITE_NULL here. The session
6593 ** module does not record changes for rows with NULL values stored in
6594 ** primary key columns. */
6595 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
6596 || eType==SQLITE_TEXT || eType==SQLITE_BLOB
6597 || eType==SQLITE_NULL || eType==0
6598 );
6599 assert( !isPK || (eType!=0 && eType!=SQLITE_NULL) );
6600
6601 if( isPK ){
6602 a++;
6603 h = sessionHashAppendType(h, eType);
6604 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
6605 h = sessionHashAppendI64(h, sessionGetI64(a));
6606 a += 8;
6607 }else{
6608 int n;
6609 a += sessionVarintGet(a, &n);
6610 h = sessionHashAppendBlob(h, n, a);
6611 a += n;
6612 }
6613 }else{
6614 a += sessionSerialLen(a);
6615 }
6616 }
6617 return (h % nBucket);
6618 }
6619
6620 /*
6621 ** Arguments aLeft and aRight are pointers to change records for table pTab.
6622 ** This function returns true if the two records apply to the same row (i.e.
6623 ** have the same values stored in the primary key columns), or false
6624 ** otherwise.
6625 */
6626 static int sessionChangeEqual(
6627 SessionTable *pTab, /* Table used for PK definition */
6628 int bLeftPkOnly, /* True if aLeft[] contains PK fields only */
6629 u8 *aLeft, /* Change record */
6630 int bRightPkOnly, /* True if aRight[] contains PK fields only */
6631 u8 *aRight /* Change record */
6632 ){
6633 u8 *a1 = aLeft; /* Cursor to iterate through aLeft */
6634 u8 *a2 = aRight; /* Cursor to iterate through aRight */
6635 int iCol; /* Used to iterate through table columns */
6636
6637 for(iCol=0; iCol<pTab->nCol; iCol++){
6638 if( pTab->abPK[iCol] ){
6639 int n1 = sessionSerialLen(a1);
6640 int n2 = sessionSerialLen(a2);
6641
6642 if( pTab->abPK[iCol] && (n1!=n2 || memcmp(a1, a2, n1)) ){
6643 return 0;
6644 }
6645 a1 += n1;
6646 a2 += n2;
6647 }else{
6648 if( bLeftPkOnly==0 ) a1 += sessionSerialLen(a1);
6649 if( bRightPkOnly==0 ) a2 += sessionSerialLen(a2);
6650 }
6651 }
6652
6653 return 1;
6654 }
6655
6656 /*
6657 ** Arguments aLeft and aRight both point to buffers containing change
6658 ** records with nCol columns. This function "merges" the two records into
6659 ** a single records which is written to the buffer at *paOut. *paOut is
6660 ** then set to point to one byte after the last byte written before
6661 ** returning.
6662 **
6663 ** The merging of records is done as follows: For each column, if the
6664 ** aRight record contains a value for the column, copy the value from
6665 ** their. Otherwise, if aLeft contains a value, copy it. If neither
6666 ** record contains a value for a given column, then neither does the
6667 ** output record.
6668 */
6669 static void sessionMergeRecord(
6670 u8 **paOut,
6671 int nCol,
6672 u8 *aLeft,
6673 u8 *aRight
6674 ){
6675 u8 *a1 = aLeft; /* Cursor used to iterate through aLeft */
6676 u8 *a2 = aRight; /* Cursor used to iterate through aRight */
6677 u8 *aOut = *paOut; /* Output cursor */
6678 int iCol; /* Used to iterate from 0 to nCol */
6679
6680 for(iCol=0; iCol<nCol; iCol++){
6681 int n1 = sessionSerialLen(a1);
6682 int n2 = sessionSerialLen(a2);
6683 if( *a2 ){
6684 memcpy(aOut, a2, n2);
6685 aOut += n2;
6686 }else{
6687 memcpy(aOut, a1, n1);
6688 aOut += n1;
6689 }
6690 a1 += n1;
6691 a2 += n2;
6692 }
6693
6694 *paOut = aOut;
6695 }
6696
6697 /*
6698 ** This is a helper function used by sessionMergeUpdate().
6699 **
6700 ** When this function is called, both *paOne and *paTwo point to a value
6701 ** within a change record. Before it returns, both have been advanced so
6702 ** as to point to the next value in the record.
6703 **
6704 ** If, when this function is called, *paTwo points to a valid value (i.e.
6705 ** *paTwo[0] is not 0x00 - the "no value" placeholder), a copy of the *paTwo
6706 ** pointer is returned and *pnVal is set to the number of bytes in the
6707 ** serialized value. Otherwise, a copy of *paOne is returned and *pnVal
6708 ** set to the number of bytes in the value at *paOne. If *paOne points
6709 ** to the "no value" placeholder, *pnVal is set to 1. In other words:
6710 **
6711 ** if( *paTwo is valid ) return *paTwo;
6712 ** return *paOne;
6713 **
6714 */
6715 static u8 *sessionMergeValue(
6716 u8 **paOne, /* IN/OUT: Left-hand buffer pointer */
6717 u8 **paTwo, /* IN/OUT: Right-hand buffer pointer */
6718 int *pnVal /* OUT: Bytes in returned value */
6719 ){
6720 u8 *a1 = *paOne;
6721 u8 *a2 = *paTwo;
6722 u8 *pRet = 0;
6723 int n1;
6724
6725 assert( a1 );
6726 if( a2 ){
6727 int n2 = sessionSerialLen(a2);
6728 if( *a2 ){
6729 *pnVal = n2;
6730 pRet = a2;
6731 }
6732 *paTwo = &a2[n2];
6733 }
6734
6735 n1 = sessionSerialLen(a1);
6736 if( pRet==0 ){
6737 *pnVal = n1;
6738 pRet = a1;
6739 }
6740 *paOne = &a1[n1];
6741
6742 return pRet;
6743 }
6744
6745 /*
6746 ** This function is used by changeset_concat() to merge two UPDATE changes
6747 ** on the same row.
6748 */
6749 static int sessionMergeUpdate(
6750 u8 **paOut, /* IN/OUT: Pointer to output buffer */
6751 SessionTable *pTab, /* Table change pertains to */
6752 int bPatchset, /* True if records are patchset records */
6753 u8 *aOldRecord1, /* old.* record for first change */
6754 u8 *aOldRecord2, /* old.* record for second change */
6755 u8 *aNewRecord1, /* new.* record for first change */
6756 u8 *aNewRecord2 /* new.* record for second change */
6757 ){
6758 u8 *aOld1 = aOldRecord1;
6759 u8 *aOld2 = aOldRecord2;
6760 u8 *aNew1 = aNewRecord1;
6761 u8 *aNew2 = aNewRecord2;
6762
6763 u8 *aOut = *paOut;
6764 int i;
6765
6766 if( bPatchset==0 ){
6767 int bRequired = 0;
6768
6769 assert( aOldRecord1 && aNewRecord1 );
6770
6771 /* Write the old.* vector first. */
6772 for(i=0; i<pTab->nCol; i++){
6773 int nOld;
6774 u8 *aOld;
6775 int nNew;
6776 u8 *aNew;
6777
6778 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
6779 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
6780 if( pTab->abPK[i] || nOld!=nNew || memcmp(aOld, aNew, nNew) ){
6781 if( pTab->abPK[i]==0 ) bRequired = 1;
6782 memcpy(aOut, aOld, nOld);
6783 aOut += nOld;
6784 }else{
6785 *(aOut++) = '\0';
6786 }
6787 }
6788
6789 if( !bRequired ) return 0;
6790 }
6791
6792 /* Write the new.* vector */
6793 aOld1 = aOldRecord1;
6794 aOld2 = aOldRecord2;
6795 aNew1 = aNewRecord1;
6796 aNew2 = aNewRecord2;
6797 for(i=0; i<pTab->nCol; i++){
6798 int nOld;
6799 u8 *aOld;
6800 int nNew;
6801 u8 *aNew;
6802
6803 aOld = sessionMergeValue(&aOld1, &aOld2, &nOld);
6804 aNew = sessionMergeValue(&aNew1, &aNew2, &nNew);
6805 if( bPatchset==0
6806 && (pTab->abPK[i] || (nOld==nNew && 0==memcmp(aOld, aNew, nNew)))
6807 ){
6808 *(aOut++) = '\0';
6809 }else{
6810 memcpy(aOut, aNew, nNew);
6811 aOut += nNew;
6812 }
6813 }
6814
6815 *paOut = aOut;
6816 return 1;
6817 }
6818
6819 /*
6820 ** This function is only called from within a pre-update-hook callback.
6821 ** It determines if the current pre-update-hook change affects the same row
6822 ** as the change stored in argument pChange. If so, it returns true. Otherwise
6823 ** if the pre-update-hook does not affect the same row as pChange, it returns
6824 ** false.
6825 */
6826 static int sessionPreupdateEqual(
6827 sqlite3_session *pSession, /* Session object that owns SessionTable */
6828 SessionTable *pTab, /* Table associated with change */
6829 SessionChange *pChange, /* Change to compare to */
6830 int op /* Current pre-update operation */
6831 ){
6832 int iCol; /* Used to iterate through columns */
6833 u8 *a = pChange->aRecord; /* Cursor used to scan change record */
6834
6835 assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );
6836 for(iCol=0; iCol<pTab->nCol; iCol++){
6837 if( !pTab->abPK[iCol] ){
6838 a += sessionSerialLen(a);
6839 }else{
6840 sqlite3_value *pVal; /* Value returned by preupdate_new/old */
6841 int rc; /* Error code from preupdate_new/old */
6842 int eType = *a++; /* Type of value from change record */
6843
6844 /* The following calls to preupdate_new() and preupdate_old() can not
6845 ** fail. This is because they cache their return values, and by the
6846 ** time control flows to here they have already been called once from
6847 ** within sessionPreupdateHash(). The first two asserts below verify
6848 ** this (that the method has already been called). */
6849 if( op==SQLITE_INSERT ){
6850 /* assert( db->pPreUpdate->pNewUnpacked || db->pPreUpdate->aNew ); */
6851 rc = pSession->hook.xNew(pSession->hook.pCtx, iCol, &pVal);
6852 }else{
6853 /* assert( db->pPreUpdate->pUnpacked ); */
6854 rc = pSession->hook.xOld(pSession->hook.pCtx, iCol, &pVal);
6855 }
6856 assert( rc==SQLITE_OK );
6857 if( sqlite3_value_type(pVal)!=eType ) return 0;
6858
6859 /* A SessionChange object never has a NULL value in a PK column */
6860 assert( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT
6861 || eType==SQLITE_BLOB || eType==SQLITE_TEXT
6862 );
6863
6864 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
6865 i64 iVal = sessionGetI64(a);
6866 a += 8;
6867 if( eType==SQLITE_INTEGER ){
6868 if( sqlite3_value_int64(pVal)!=iVal ) return 0;
6869 }else{
6870 double rVal;
6871 assert( sizeof(iVal)==8 && sizeof(rVal)==8 );
6872 memcpy(&rVal, &iVal, 8);
6873 if( sqlite3_value_double(pVal)!=rVal ) return 0;
6874 }
6875 }else{
6876 int n;
6877 const u8 *z;
6878 a += sessionVarintGet(a, &n);
6879 if( sqlite3_value_bytes(pVal)!=n ) return 0;
6880 if( eType==SQLITE_TEXT ){
6881 z = sqlite3_value_text(pVal);
6882 }else{
6883 z = sqlite3_value_blob(pVal);
6884 }
6885 if( memcmp(a, z, n) ) return 0;
6886 a += n;
6887 break;
6888 }
6889 }
6890 }
6891
6892 return 1;
6893 }
6894
6895 /*
6896 ** If required, grow the hash table used to store changes on table pTab
6897 ** (part of the session pSession). If a fatal OOM error occurs, set the
6898 ** session object to failed and return SQLITE_ERROR. Otherwise, return
6899 ** SQLITE_OK.
6900 **
6901 ** It is possible that a non-fatal OOM error occurs in this function. In
6902 ** that case the hash-table does not grow, but SQLITE_OK is returned anyway.
6903 ** Growing the hash table in this case is a performance optimization only,
6904 ** it is not required for correct operation.
6905 */
6906 static int sessionGrowHash(int bPatchset, SessionTable *pTab){
6907 if( pTab->nChange==0 || pTab->nEntry>=(pTab->nChange/2) ){
6908 int i;
6909 SessionChange **apNew;
6910 int nNew = (pTab->nChange ? pTab->nChange : 128) * 2;
6911
6912 apNew = (SessionChange **)sqlite3_malloc(sizeof(SessionChange *) * nNew);
6913 if( apNew==0 ){
6914 if( pTab->nChange==0 ){
6915 return SQLITE_ERROR;
6916 }
6917 return SQLITE_OK;
6918 }
6919 memset(apNew, 0, sizeof(SessionChange *) * nNew);
6920
6921 for(i=0; i<pTab->nChange; i++){
6922 SessionChange *p;
6923 SessionChange *pNext;
6924 for(p=pTab->apChange[i]; p; p=pNext){
6925 int bPkOnly = (p->op==SQLITE_DELETE && bPatchset);
6926 int iHash = sessionChangeHash(pTab, bPkOnly, p->aRecord, nNew);
6927 pNext = p->pNext;
6928 p->pNext = apNew[iHash];
6929 apNew[iHash] = p;
6930 }
6931 }
6932
6933 sqlite3_free(pTab->apChange);
6934 pTab->nChange = nNew;
6935 pTab->apChange = apNew;
6936 }
6937
6938 return SQLITE_OK;
6939 }
6940
6941 /*
6942 ** This function queries the database for the names of the columns of table
6943 ** zThis, in schema zDb. It is expected that the table has nCol columns. If
6944 ** not, SQLITE_SCHEMA is returned and none of the output variables are
6945 ** populated.
6946 **
6947 ** Otherwise, if they are not NULL, variable *pnCol is set to the number
6948 ** of columns in the database table and variable *pzTab is set to point to a
6949 ** nul-terminated copy of the table name. *pazCol (if not NULL) is set to
6950 ** point to an array of pointers to column names. And *pabPK (again, if not
6951 ** NULL) is set to point to an array of booleans - true if the corresponding
6952 ** column is part of the primary key.
6953 **
6954 ** For example, if the table is declared as:
6955 **
6956 ** CREATE TABLE tbl1(w, x, y, z, PRIMARY KEY(w, z));
6957 **
6958 ** Then the four output variables are populated as follows:
6959 **
6960 ** *pnCol = 4
6961 ** *pzTab = "tbl1"
6962 ** *pazCol = {"w", "x", "y", "z"}
6963 ** *pabPK = {1, 0, 0, 1}
6964 **
6965 ** All returned buffers are part of the same single allocation, which must
6966 ** be freed using sqlite3_free() by the caller. If pazCol was not NULL, then
6967 ** pointer *pazCol should be freed to release all memory. Otherwise, pointer
6968 ** *pabPK. It is illegal for both pazCol and pabPK to be NULL.
6969 */
6970 static int sessionTableInfo(
6971 sqlite3 *db, /* Database connection */
6972 const char *zDb, /* Name of attached database (e.g. "main") */
6973 const char *zThis, /* Table name */
6974 int *pnCol, /* OUT: number of columns */
6975 const char **pzTab, /* OUT: Copy of zThis */
6976 const char ***pazCol, /* OUT: Array of column names for table */
6977 u8 **pabPK /* OUT: Array of booleans - true for PK col */
6978 ){
6979 char *zPragma;
6980 sqlite3_stmt *pStmt;
6981 int rc;
6982 int nByte;
6983 int nDbCol = 0;
6984 int nThis;
6985 int i;
6986 u8 *pAlloc = 0;
6987 char **azCol = 0;
6988 u8 *abPK = 0;
6989
6990 assert( pazCol && pabPK );
6991
6992 nThis = sqlite3Strlen30(zThis);
6993 zPragma = sqlite3_mprintf("PRAGMA '%q'.table_info('%q')", zDb, zThis);
6994 if( !zPragma ) return SQLITE_NOMEM;
6995
6996 rc = sqlite3_prepare_v2(db, zPragma, -1, &pStmt, 0);
6997 sqlite3_free(zPragma);
6998 if( rc!=SQLITE_OK ) return rc;
6999
7000 nByte = nThis + 1;
7001 while( SQLITE_ROW==sqlite3_step(pStmt) ){
7002 nByte += sqlite3_column_bytes(pStmt, 1);
7003 nDbCol++;
7004 }
7005 rc = sqlite3_reset(pStmt);
7006
7007 if( rc==SQLITE_OK ){
7008 nByte += nDbCol * (sizeof(const char *) + sizeof(u8) + 1);
7009 pAlloc = sqlite3_malloc(nByte);
7010 if( pAlloc==0 ){
7011 rc = SQLITE_NOMEM;
7012 }
7013 }
7014 if( rc==SQLITE_OK ){
7015 azCol = (char **)pAlloc;
7016 pAlloc = (u8 *)&azCol[nDbCol];
7017 abPK = (u8 *)pAlloc;
7018 pAlloc = &abPK[nDbCol];
7019 if( pzTab ){
7020 memcpy(pAlloc, zThis, nThis+1);
7021 *pzTab = (char *)pAlloc;
7022 pAlloc += nThis+1;
7023 }
7024
7025 i = 0;
7026 while( SQLITE_ROW==sqlite3_step(pStmt) ){
7027 int nName = sqlite3_column_bytes(pStmt, 1);
7028 const unsigned char *zName = sqlite3_column_text(pStmt, 1);
7029 if( zName==0 ) break;
7030 memcpy(pAlloc, zName, nName+1);
7031 azCol[i] = (char *)pAlloc;
7032 pAlloc += nName+1;
7033 abPK[i] = sqlite3_column_int(pStmt, 5);
7034 i++;
7035 }
7036 rc = sqlite3_reset(pStmt);
7037
7038 }
7039
7040 /* If successful, populate the output variables. Otherwise, zero them and
7041 ** free any allocation made. An error code will be returned in this case.
7042 */
7043 if( rc==SQLITE_OK ){
7044 *pazCol = (const char **)azCol;
7045 *pabPK = abPK;
7046 *pnCol = nDbCol;
7047 }else{
7048 *pazCol = 0;
7049 *pabPK = 0;
7050 *pnCol = 0;
7051 if( pzTab ) *pzTab = 0;
7052 sqlite3_free(azCol);
7053 }
7054 sqlite3_finalize(pStmt);
7055 return rc;
7056 }
7057
7058 /*
7059 ** This function is only called from within a pre-update handler for a
7060 ** write to table pTab, part of session pSession. If this is the first
7061 ** write to this table, initalize the SessionTable.nCol, azCol[] and
7062 ** abPK[] arrays accordingly.
7063 **
7064 ** If an error occurs, an error code is stored in sqlite3_session.rc and
7065 ** non-zero returned. Or, if no error occurs but the table has no primary
7066 ** key, sqlite3_session.rc is left set to SQLITE_OK and non-zero returned to
7067 ** indicate that updates on this table should be ignored. SessionTable.abPK
7068 ** is set to NULL in this case.
7069 */
7070 static int sessionInitTable(sqlite3_session *pSession, SessionTable *pTab){
7071 if( pTab->nCol==0 ){
7072 u8 *abPK;
7073 assert( pTab->azCol==0 || pTab->abPK==0 );
7074 pSession->rc = sessionTableInfo(pSession->db, pSession->zDb,
7075 pTab->zName, &pTab->nCol, 0, &pTab->azCol, &abPK
7076 );
7077 if( pSession->rc==SQLITE_OK ){
7078 int i;
7079 for(i=0; i<pTab->nCol; i++){
7080 if( abPK[i] ){
7081 pTab->abPK = abPK;
7082 break;
7083 }
7084 }
7085 }
7086 }
7087 return (pSession->rc || pTab->abPK==0);
7088 }
7089
7090 /*
7091 ** This function is only called from with a pre-update-hook reporting a
7092 ** change on table pTab (attached to session pSession). The type of change
7093 ** (UPDATE, INSERT, DELETE) is specified by the first argument.
7094 **
7095 ** Unless one is already present or an error occurs, an entry is added
7096 ** to the changed-rows hash table associated with table pTab.
7097 */
7098 static void sessionPreupdateOneChange(
7099 int op, /* One of SQLITE_UPDATE, INSERT, DELETE */
7100 sqlite3_session *pSession, /* Session object pTab is attached to */
7101 SessionTable *pTab /* Table that change applies to */
7102 ){
7103 int iHash;
7104 int bNull = 0;
7105 int rc = SQLITE_OK;
7106
7107 if( pSession->rc ) return;
7108
7109 /* Load table details if required */
7110 if( sessionInitTable(pSession, pTab) ) return;
7111
7112 /* Check the number of columns in this xPreUpdate call matches the
7113 ** number of columns in the table. */
7114 if( pTab->nCol!=pSession->hook.xCount(pSession->hook.pCtx) ){
7115 pSession->rc = SQLITE_SCHEMA;
7116 return;
7117 }
7118
7119 /* Grow the hash table if required */
7120 if( sessionGrowHash(0, pTab) ){
7121 pSession->rc = SQLITE_NOMEM;
7122 return;
7123 }
7124
7125 /* Calculate the hash-key for this change. If the primary key of the row
7126 ** includes a NULL value, exit early. Such changes are ignored by the
7127 ** session module. */
7128 rc = sessionPreupdateHash(pSession, pTab, op==SQLITE_INSERT, &iHash, &bNull);
7129 if( rc!=SQLITE_OK ) goto error_out;
7130
7131 if( bNull==0 ){
7132 /* Search the hash table for an existing record for this row. */
7133 SessionChange *pC;
7134 for(pC=pTab->apChange[iHash]; pC; pC=pC->pNext){
7135 if( sessionPreupdateEqual(pSession, pTab, pC, op) ) break;
7136 }
7137
7138 if( pC==0 ){
7139 /* Create a new change object containing all the old values (if
7140 ** this is an SQLITE_UPDATE or SQLITE_DELETE), or just the PK
7141 ** values (if this is an INSERT). */
7142 SessionChange *pChange; /* New change object */
7143 int nByte; /* Number of bytes to allocate */
7144 int i; /* Used to iterate through columns */
7145
7146 assert( rc==SQLITE_OK );
7147 pTab->nEntry++;
7148
7149 /* Figure out how large an allocation is required */
7150 nByte = sizeof(SessionChange);
7151 for(i=0; i<pTab->nCol; i++){
7152 sqlite3_value *p = 0;
7153 if( op!=SQLITE_INSERT ){
7154 TESTONLY(int trc = ) pSession->hook.xOld(pSession->hook.pCtx, i, &p);
7155 assert( trc==SQLITE_OK );
7156 }else if( pTab->abPK[i] ){
7157 TESTONLY(int trc = ) pSession->hook.xNew(pSession->hook.pCtx, i, &p);
7158 assert( trc==SQLITE_OK );
7159 }
7160
7161 /* This may fail if SQLite value p contains a utf-16 string that must
7162 ** be converted to utf-8 and an OOM error occurs while doing so. */
7163 rc = sessionSerializeValue(0, p, &nByte);
7164 if( rc!=SQLITE_OK ) goto error_out;
7165 }
7166
7167 /* Allocate the change object */
7168 pChange = (SessionChange *)sqlite3_malloc(nByte);
7169 if( !pChange ){
7170 rc = SQLITE_NOMEM;
7171 goto error_out;
7172 }else{
7173 memset(pChange, 0, sizeof(SessionChange));
7174 pChange->aRecord = (u8 *)&pChange[1];
7175 }
7176
7177 /* Populate the change object. None of the preupdate_old(),
7178 ** preupdate_new() or SerializeValue() calls below may fail as all
7179 ** required values and encodings have already been cached in memory.
7180 ** It is not possible for an OOM to occur in this block. */
7181 nByte = 0;
7182 for(i=0; i<pTab->nCol; i++){
7183 sqlite3_value *p = 0;
7184 if( op!=SQLITE_INSERT ){
7185 pSession->hook.xOld(pSession->hook.pCtx, i, &p);
7186 }else if( pTab->abPK[i] ){
7187 pSession->hook.xNew(pSession->hook.pCtx, i, &p);
7188 }
7189 sessionSerializeValue(&pChange->aRecord[nByte], p, &nByte);
7190 }
7191
7192 /* Add the change to the hash-table */
7193 if( pSession->bIndirect || pSession->hook.xDepth(pSession->hook.pCtx) ){
7194 pChange->bIndirect = 1;
7195 }
7196 pChange->nRecord = nByte;
7197 pChange->op = op;
7198 pChange->pNext = pTab->apChange[iHash];
7199 pTab->apChange[iHash] = pChange;
7200
7201 }else if( pC->bIndirect ){
7202 /* If the existing change is considered "indirect", but this current
7203 ** change is "direct", mark the change object as direct. */
7204 if( pSession->hook.xDepth(pSession->hook.pCtx)==0
7205 && pSession->bIndirect==0
7206 ){
7207 pC->bIndirect = 0;
7208 }
7209 }
7210 }
7211
7212 /* If an error has occurred, mark the session object as failed. */
7213 error_out:
7214 if( rc!=SQLITE_OK ){
7215 pSession->rc = rc;
7216 }
7217 }
7218
7219 static int sessionFindTable(
7220 sqlite3_session *pSession,
7221 const char *zName,
7222 SessionTable **ppTab
7223 ){
7224 int rc = SQLITE_OK;
7225 int nName = sqlite3Strlen30(zName);
7226 SessionTable *pRet;
7227
7228 /* Search for an existing table */
7229 for(pRet=pSession->pTable; pRet; pRet=pRet->pNext){
7230 if( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) ) break;
7231 }
7232
7233 if( pRet==0 && pSession->bAutoAttach ){
7234 /* If there is a table-filter configured, invoke it. If it returns 0,
7235 ** do not automatically add the new table. */
7236 if( pSession->xTableFilter==0
7237 || pSession->xTableFilter(pSession->pFilterCtx, zName)
7238 ){
7239 rc = sqlite3session_attach(pSession, zName);
7240 if( rc==SQLITE_OK ){
7241 for(pRet=pSession->pTable; pRet->pNext; pRet=pRet->pNext);
7242 assert( 0==sqlite3_strnicmp(pRet->zName, zName, nName+1) );
7243 }
7244 }
7245 }
7246
7247 assert( rc==SQLITE_OK || pRet==0 );
7248 *ppTab = pRet;
7249 return rc;
7250 }
7251
7252 /*
7253 ** The 'pre-update' hook registered by this module with SQLite databases.
7254 */
7255 static void xPreUpdate(
7256 void *pCtx, /* Copy of third arg to preupdate_hook() */
7257 sqlite3 *db, /* Database handle */
7258 int op, /* SQLITE_UPDATE, DELETE or INSERT */
7259 char const *zDb, /* Database name */
7260 char const *zName, /* Table name */
7261 sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */
7262 sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */
7263 ){
7264 sqlite3_session *pSession;
7265 int nDb = sqlite3Strlen30(zDb);
7266
7267 assert( sqlite3_mutex_held(db->mutex) );
7268
7269 for(pSession=(sqlite3_session *)pCtx; pSession; pSession=pSession->pNext){
7270 SessionTable *pTab;
7271
7272 /* If this session is attached to a different database ("main", "temp"
7273 ** etc.), or if it is not currently enabled, there is nothing to do. Skip
7274 ** to the next session object attached to this database. */
7275 if( pSession->bEnable==0 ) continue;
7276 if( pSession->rc ) continue;
7277 if( sqlite3_strnicmp(zDb, pSession->zDb, nDb+1) ) continue;
7278
7279 pSession->rc = sessionFindTable(pSession, zName, &pTab);
7280 if( pTab ){
7281 assert( pSession->rc==SQLITE_OK );
7282 sessionPreupdateOneChange(op, pSession, pTab);
7283 if( op==SQLITE_UPDATE ){
7284 sessionPreupdateOneChange(SQLITE_INSERT, pSession, pTab);
7285 }
7286 }
7287 }
7288 }
7289
7290 /*
7291 ** The pre-update hook implementations.
7292 */
7293 static int sessionPreupdateOld(void *pCtx, int iVal, sqlite3_value **ppVal){
7294 return sqlite3_preupdate_old((sqlite3*)pCtx, iVal, ppVal);
7295 }
7296 static int sessionPreupdateNew(void *pCtx, int iVal, sqlite3_value **ppVal){
7297 return sqlite3_preupdate_new((sqlite3*)pCtx, iVal, ppVal);
7298 }
7299 static int sessionPreupdateCount(void *pCtx){
7300 return sqlite3_preupdate_count((sqlite3*)pCtx);
7301 }
7302 static int sessionPreupdateDepth(void *pCtx){
7303 return sqlite3_preupdate_depth((sqlite3*)pCtx);
7304 }
7305
7306 /*
7307 ** Install the pre-update hooks on the session object passed as the only
7308 ** argument.
7309 */
7310 static void sessionPreupdateHooks(
7311 sqlite3_session *pSession
7312 ){
7313 pSession->hook.pCtx = (void*)pSession->db;
7314 pSession->hook.xOld = sessionPreupdateOld;
7315 pSession->hook.xNew = sessionPreupdateNew;
7316 pSession->hook.xCount = sessionPreupdateCount;
7317 pSession->hook.xDepth = sessionPreupdateDepth;
7318 }
7319
7320 typedef struct SessionDiffCtx SessionDiffCtx;
7321 struct SessionDiffCtx {
7322 sqlite3_stmt *pStmt;
7323 int nOldOff;
7324 };
7325
7326 /*
7327 ** The diff hook implementations.
7328 */
7329 static int sessionDiffOld(void *pCtx, int iVal, sqlite3_value **ppVal){
7330 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
7331 *ppVal = sqlite3_column_value(p->pStmt, iVal+p->nOldOff);
7332 return SQLITE_OK;
7333 }
7334 static int sessionDiffNew(void *pCtx, int iVal, sqlite3_value **ppVal){
7335 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
7336 *ppVal = sqlite3_column_value(p->pStmt, iVal);
7337 return SQLITE_OK;
7338 }
7339 static int sessionDiffCount(void *pCtx){
7340 SessionDiffCtx *p = (SessionDiffCtx*)pCtx;
7341 return p->nOldOff ? p->nOldOff : sqlite3_column_count(p->pStmt);
7342 }
7343 static int sessionDiffDepth(void *pCtx){
7344 return 0;
7345 }
7346
7347 /*
7348 ** Install the diff hooks on the session object passed as the only
7349 ** argument.
7350 */
7351 static void sessionDiffHooks(
7352 sqlite3_session *pSession,
7353 SessionDiffCtx *pDiffCtx
7354 ){
7355 pSession->hook.pCtx = (void*)pDiffCtx;
7356 pSession->hook.xOld = sessionDiffOld;
7357 pSession->hook.xNew = sessionDiffNew;
7358 pSession->hook.xCount = sessionDiffCount;
7359 pSession->hook.xDepth = sessionDiffDepth;
7360 }
7361
7362 static char *sessionExprComparePK(
7363 int nCol,
7364 const char *zDb1, const char *zDb2,
7365 const char *zTab,
7366 const char **azCol, u8 *abPK
7367 ){
7368 int i;
7369 const char *zSep = "";
7370 char *zRet = 0;
7371
7372 for(i=0; i<nCol; i++){
7373 if( abPK[i] ){
7374 zRet = sqlite3_mprintf("%z%s\"%w\".\"%w\".\"%w\"=\"%w\".\"%w\".\"%w\"",
7375 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
7376 );
7377 zSep = " AND ";
7378 if( zRet==0 ) break;
7379 }
7380 }
7381
7382 return zRet;
7383 }
7384
7385 static char *sessionExprCompareOther(
7386 int nCol,
7387 const char *zDb1, const char *zDb2,
7388 const char *zTab,
7389 const char **azCol, u8 *abPK
7390 ){
7391 int i;
7392 const char *zSep = "";
7393 char *zRet = 0;
7394 int bHave = 0;
7395
7396 for(i=0; i<nCol; i++){
7397 if( abPK[i]==0 ){
7398 bHave = 1;
7399 zRet = sqlite3_mprintf(
7400 "%z%s\"%w\".\"%w\".\"%w\" IS NOT \"%w\".\"%w\".\"%w\"",
7401 zRet, zSep, zDb1, zTab, azCol[i], zDb2, zTab, azCol[i]
7402 );
7403 zSep = " OR ";
7404 if( zRet==0 ) break;
7405 }
7406 }
7407
7408 if( bHave==0 ){
7409 assert( zRet==0 );
7410 zRet = sqlite3_mprintf("0");
7411 }
7412
7413 return zRet;
7414 }
7415
7416 static char *sessionSelectFindNew(
7417 int nCol,
7418 const char *zDb1, /* Pick rows in this db only */
7419 const char *zDb2, /* But not in this one */
7420 const char *zTbl, /* Table name */
7421 const char *zExpr
7422 ){
7423 char *zRet = sqlite3_mprintf(
7424 "SELECT * FROM \"%w\".\"%w\" WHERE NOT EXISTS ("
7425 " SELECT 1 FROM \"%w\".\"%w\" WHERE %s"
7426 ")",
7427 zDb1, zTbl, zDb2, zTbl, zExpr
7428 );
7429 return zRet;
7430 }
7431
7432 static int sessionDiffFindNew(
7433 int op,
7434 sqlite3_session *pSession,
7435 SessionTable *pTab,
7436 const char *zDb1,
7437 const char *zDb2,
7438 char *zExpr
7439 ){
7440 int rc = SQLITE_OK;
7441 char *zStmt = sessionSelectFindNew(pTab->nCol, zDb1, zDb2, pTab->zName,zExpr);
7442
7443 if( zStmt==0 ){
7444 rc = SQLITE_NOMEM;
7445 }else{
7446 sqlite3_stmt *pStmt;
7447 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
7448 if( rc==SQLITE_OK ){
7449 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
7450 pDiffCtx->pStmt = pStmt;
7451 pDiffCtx->nOldOff = 0;
7452 while( SQLITE_ROW==sqlite3_step(pStmt) ){
7453 sessionPreupdateOneChange(op, pSession, pTab);
7454 }
7455 rc = sqlite3_finalize(pStmt);
7456 }
7457 sqlite3_free(zStmt);
7458 }
7459
7460 return rc;
7461 }
7462
7463 static int sessionDiffFindModified(
7464 sqlite3_session *pSession,
7465 SessionTable *pTab,
7466 const char *zFrom,
7467 const char *zExpr
7468 ){
7469 int rc = SQLITE_OK;
7470
7471 char *zExpr2 = sessionExprCompareOther(pTab->nCol,
7472 pSession->zDb, zFrom, pTab->zName, pTab->azCol, pTab->abPK
7473 );
7474 if( zExpr2==0 ){
7475 rc = SQLITE_NOMEM;
7476 }else{
7477 char *zStmt = sqlite3_mprintf(
7478 "SELECT * FROM \"%w\".\"%w\", \"%w\".\"%w\" WHERE %s AND (%z)",
7479 pSession->zDb, pTab->zName, zFrom, pTab->zName, zExpr, zExpr2
7480 );
7481 if( zStmt==0 ){
7482 rc = SQLITE_NOMEM;
7483 }else{
7484 sqlite3_stmt *pStmt;
7485 rc = sqlite3_prepare(pSession->db, zStmt, -1, &pStmt, 0);
7486
7487 if( rc==SQLITE_OK ){
7488 SessionDiffCtx *pDiffCtx = (SessionDiffCtx*)pSession->hook.pCtx;
7489 pDiffCtx->pStmt = pStmt;
7490 pDiffCtx->nOldOff = pTab->nCol;
7491 while( SQLITE_ROW==sqlite3_step(pStmt) ){
7492 sessionPreupdateOneChange(SQLITE_UPDATE, pSession, pTab);
7493 }
7494 rc = sqlite3_finalize(pStmt);
7495 }
7496 sqlite3_free(zStmt);
7497 }
7498 }
7499
7500 return rc;
7501 }
7502
7503 SQLITE_API int sqlite3session_diff(
7504 sqlite3_session *pSession,
7505 const char *zFrom,
7506 const char *zTbl,
7507 char **pzErrMsg
7508 ){
7509 const char *zDb = pSession->zDb;
7510 int rc = pSession->rc;
7511 SessionDiffCtx d;
7512
7513 memset(&d, 0, sizeof(d));
7514 sessionDiffHooks(pSession, &d);
7515
7516 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
7517 if( pzErrMsg ) *pzErrMsg = 0;
7518 if( rc==SQLITE_OK ){
7519 char *zExpr = 0;
7520 sqlite3 *db = pSession->db;
7521 SessionTable *pTo; /* Table zTbl */
7522
7523 /* Locate and if necessary initialize the target table object */
7524 rc = sessionFindTable(pSession, zTbl, &pTo);
7525 if( pTo==0 ) goto diff_out;
7526 if( sessionInitTable(pSession, pTo) ){
7527 rc = pSession->rc;
7528 goto diff_out;
7529 }
7530
7531 /* Check the table schemas match */
7532 if( rc==SQLITE_OK ){
7533 int bHasPk = 0;
7534 int bMismatch = 0;
7535 int nCol; /* Columns in zFrom.zTbl */
7536 u8 *abPK;
7537 const char **azCol = 0;
7538 rc = sessionTableInfo(db, zFrom, zTbl, &nCol, 0, &azCol, &abPK);
7539 if( rc==SQLITE_OK ){
7540 if( pTo->nCol!=nCol ){
7541 bMismatch = 1;
7542 }else{
7543 int i;
7544 for(i=0; i<nCol; i++){
7545 if( pTo->abPK[i]!=abPK[i] ) bMismatch = 1;
7546 if( sqlite3_stricmp(azCol[i], pTo->azCol[i]) ) bMismatch = 1;
7547 if( abPK[i] ) bHasPk = 1;
7548 }
7549 }
7550
7551 }
7552 sqlite3_free((char*)azCol);
7553 if( bMismatch ){
7554 *pzErrMsg = sqlite3_mprintf("table schemas do not match");
7555 rc = SQLITE_SCHEMA;
7556 }
7557 if( bHasPk==0 ){
7558 /* Ignore tables with no primary keys */
7559 goto diff_out;
7560 }
7561 }
7562
7563 if( rc==SQLITE_OK ){
7564 zExpr = sessionExprComparePK(pTo->nCol,
7565 zDb, zFrom, pTo->zName, pTo->azCol, pTo->abPK
7566 );
7567 }
7568
7569 /* Find new rows */
7570 if( rc==SQLITE_OK ){
7571 rc = sessionDiffFindNew(SQLITE_INSERT, pSession, pTo, zDb, zFrom, zExpr);
7572 }
7573
7574 /* Find old rows */
7575 if( rc==SQLITE_OK ){
7576 rc = sessionDiffFindNew(SQLITE_DELETE, pSession, pTo, zFrom, zDb, zExpr);
7577 }
7578
7579 /* Find modified rows */
7580 if( rc==SQLITE_OK ){
7581 rc = sessionDiffFindModified(pSession, pTo, zFrom, zExpr);
7582 }
7583
7584 sqlite3_free(zExpr);
7585 }
7586
7587 diff_out:
7588 sessionPreupdateHooks(pSession);
7589 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
7590 return rc;
7591 }
7592
7593 /*
7594 ** Create a session object. This session object will record changes to
7595 ** database zDb attached to connection db.
7596 */
7597 SQLITE_API int sqlite3session_create(
7598 sqlite3 *db, /* Database handle */
7599 const char *zDb, /* Name of db (e.g. "main") */
7600 sqlite3_session **ppSession /* OUT: New session object */
7601 ){
7602 sqlite3_session *pNew; /* Newly allocated session object */
7603 sqlite3_session *pOld; /* Session object already attached to db */
7604 int nDb = sqlite3Strlen30(zDb); /* Length of zDb in bytes */
7605
7606 /* Zero the output value in case an error occurs. */
7607 *ppSession = 0;
7608
7609 /* Allocate and populate the new session object. */
7610 pNew = (sqlite3_session *)sqlite3_malloc(sizeof(sqlite3_session) + nDb + 1);
7611 if( !pNew ) return SQLITE_NOMEM;
7612 memset(pNew, 0, sizeof(sqlite3_session));
7613 pNew->db = db;
7614 pNew->zDb = (char *)&pNew[1];
7615 pNew->bEnable = 1;
7616 memcpy(pNew->zDb, zDb, nDb+1);
7617 sessionPreupdateHooks(pNew);
7618
7619 /* Add the new session object to the linked list of session objects
7620 ** attached to database handle $db. Do this under the cover of the db
7621 ** handle mutex. */
7622 sqlite3_mutex_enter(sqlite3_db_mutex(db));
7623 pOld = (sqlite3_session*)sqlite3_preupdate_hook(db, xPreUpdate, (void*)pNew);
7624 pNew->pNext = pOld;
7625 sqlite3_mutex_leave(sqlite3_db_mutex(db));
7626
7627 *ppSession = pNew;
7628 return SQLITE_OK;
7629 }
7630
7631 /*
7632 ** Free the list of table objects passed as the first argument. The contents
7633 ** of the changed-rows hash tables are also deleted.
7634 */
7635 static void sessionDeleteTable(SessionTable *pList){
7636 SessionTable *pNext;
7637 SessionTable *pTab;
7638
7639 for(pTab=pList; pTab; pTab=pNext){
7640 int i;
7641 pNext = pTab->pNext;
7642 for(i=0; i<pTab->nChange; i++){
7643 SessionChange *p;
7644 SessionChange *pNextChange;
7645 for(p=pTab->apChange[i]; p; p=pNextChange){
7646 pNextChange = p->pNext;
7647 sqlite3_free(p);
7648 }
7649 }
7650 sqlite3_free((char*)pTab->azCol); /* cast works around VC++ bug */
7651 sqlite3_free(pTab->apChange);
7652 sqlite3_free(pTab);
7653 }
7654 }
7655
7656 /*
7657 ** Delete a session object previously allocated using sqlite3session_create().
7658 */
7659 SQLITE_API void sqlite3session_delete(sqlite3_session *pSession){
7660 sqlite3 *db = pSession->db;
7661 sqlite3_session *pHead;
7662 sqlite3_session **pp;
7663
7664 /* Unlink the session from the linked list of sessions attached to the
7665 ** database handle. Hold the db mutex while doing so. */
7666 sqlite3_mutex_enter(sqlite3_db_mutex(db));
7667 pHead = (sqlite3_session*)sqlite3_preupdate_hook(db, 0, 0);
7668 for(pp=&pHead; ALWAYS((*pp)!=0); pp=&((*pp)->pNext)){
7669 if( (*pp)==pSession ){
7670 *pp = (*pp)->pNext;
7671 if( pHead ) sqlite3_preupdate_hook(db, xPreUpdate, (void*)pHead);
7672 break;
7673 }
7674 }
7675 sqlite3_mutex_leave(sqlite3_db_mutex(db));
7676
7677 /* Delete all attached table objects. And the contents of their
7678 ** associated hash-tables. */
7679 sessionDeleteTable(pSession->pTable);
7680
7681 /* Free the session object itself. */
7682 sqlite3_free(pSession);
7683 }
7684
7685 /*
7686 ** Set a table filter on a Session Object.
7687 */
7688 SQLITE_API void sqlite3session_table_filter(
7689 sqlite3_session *pSession,
7690 int(*xFilter)(void*, const char*),
7691 void *pCtx /* First argument passed to xFilter */
7692 ){
7693 pSession->bAutoAttach = 1;
7694 pSession->pFilterCtx = pCtx;
7695 pSession->xTableFilter = xFilter;
7696 }
7697
7698 /*
7699 ** Attach a table to a session. All subsequent changes made to the table
7700 ** while the session object is enabled will be recorded.
7701 **
7702 ** Only tables that have a PRIMARY KEY defined may be attached. It does
7703 ** not matter if the PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias)
7704 ** or not.
7705 */
7706 SQLITE_API int sqlite3session_attach(
7707 sqlite3_session *pSession, /* Session object */
7708 const char *zName /* Table name */
7709 ){
7710 int rc = SQLITE_OK;
7711 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
7712
7713 if( !zName ){
7714 pSession->bAutoAttach = 1;
7715 }else{
7716 SessionTable *pTab; /* New table object (if required) */
7717 int nName; /* Number of bytes in string zName */
7718
7719 /* First search for an existing entry. If one is found, this call is
7720 ** a no-op. Return early. */
7721 nName = sqlite3Strlen30(zName);
7722 for(pTab=pSession->pTable; pTab; pTab=pTab->pNext){
7723 if( 0==sqlite3_strnicmp(pTab->zName, zName, nName+1) ) break;
7724 }
7725
7726 if( !pTab ){
7727 /* Allocate new SessionTable object. */
7728 pTab = (SessionTable *)sqlite3_malloc(sizeof(SessionTable) + nName + 1);
7729 if( !pTab ){
7730 rc = SQLITE_NOMEM;
7731 }else{
7732 /* Populate the new SessionTable object and link it into the list.
7733 ** The new object must be linked onto the end of the list, not
7734 ** simply added to the start of it in order to ensure that tables
7735 ** appear in the correct order when a changeset or patchset is
7736 ** eventually generated. */
7737 SessionTable **ppTab;
7738 memset(pTab, 0, sizeof(SessionTable));
7739 pTab->zName = (char *)&pTab[1];
7740 memcpy(pTab->zName, zName, nName+1);
7741 for(ppTab=&pSession->pTable; *ppTab; ppTab=&(*ppTab)->pNext);
7742 *ppTab = pTab;
7743 }
7744 }
7745 }
7746
7747 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
7748 return rc;
7749 }
7750
7751 /*
7752 ** Ensure that there is room in the buffer to append nByte bytes of data.
7753 ** If not, use sqlite3_realloc() to grow the buffer so that there is.
7754 **
7755 ** If successful, return zero. Otherwise, if an OOM condition is encountered,
7756 ** set *pRc to SQLITE_NOMEM and return non-zero.
7757 */
7758 static int sessionBufferGrow(SessionBuffer *p, int nByte, int *pRc){
7759 if( *pRc==SQLITE_OK && p->nAlloc-p->nBuf<nByte ){
7760 u8 *aNew;
7761 int nNew = p->nAlloc ? p->nAlloc : 128;
7762 do {
7763 nNew = nNew*2;
7764 }while( nNew<(p->nBuf+nByte) );
7765
7766 aNew = (u8 *)sqlite3_realloc(p->aBuf, nNew);
7767 if( 0==aNew ){
7768 *pRc = SQLITE_NOMEM;
7769 }else{
7770 p->aBuf = aNew;
7771 p->nAlloc = nNew;
7772 }
7773 }
7774 return (*pRc!=SQLITE_OK);
7775 }
7776
7777 /*
7778 ** Append the value passed as the second argument to the buffer passed
7779 ** as the first.
7780 **
7781 ** This function is a no-op if *pRc is non-zero when it is called.
7782 ** Otherwise, if an error occurs, *pRc is set to an SQLite error code
7783 ** before returning.
7784 */
7785 static void sessionAppendValue(SessionBuffer *p, sqlite3_value *pVal, int *pRc){
7786 int rc = *pRc;
7787 if( rc==SQLITE_OK ){
7788 int nByte = 0;
7789 rc = sessionSerializeValue(0, pVal, &nByte);
7790 sessionBufferGrow(p, nByte, &rc);
7791 if( rc==SQLITE_OK ){
7792 rc = sessionSerializeValue(&p->aBuf[p->nBuf], pVal, 0);
7793 p->nBuf += nByte;
7794 }else{
7795 *pRc = rc;
7796 }
7797 }
7798 }
7799
7800 /*
7801 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
7802 ** called. Otherwise, append a single byte to the buffer.
7803 **
7804 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
7805 ** returning.
7806 */
7807 static void sessionAppendByte(SessionBuffer *p, u8 v, int *pRc){
7808 if( 0==sessionBufferGrow(p, 1, pRc) ){
7809 p->aBuf[p->nBuf++] = v;
7810 }
7811 }
7812
7813 /*
7814 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
7815 ** called. Otherwise, append a single varint to the buffer.
7816 **
7817 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
7818 ** returning.
7819 */
7820 static void sessionAppendVarint(SessionBuffer *p, int v, int *pRc){
7821 if( 0==sessionBufferGrow(p, 9, pRc) ){
7822 p->nBuf += sessionVarintPut(&p->aBuf[p->nBuf], v);
7823 }
7824 }
7825
7826 /*
7827 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
7828 ** called. Otherwise, append a blob of data to the buffer.
7829 **
7830 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
7831 ** returning.
7832 */
7833 static void sessionAppendBlob(
7834 SessionBuffer *p,
7835 const u8 *aBlob,
7836 int nBlob,
7837 int *pRc
7838 ){
7839 if( nBlob>0 && 0==sessionBufferGrow(p, nBlob, pRc) ){
7840 memcpy(&p->aBuf[p->nBuf], aBlob, nBlob);
7841 p->nBuf += nBlob;
7842 }
7843 }
7844
7845 /*
7846 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
7847 ** called. Otherwise, append a string to the buffer. All bytes in the string
7848 ** up to (but not including) the nul-terminator are written to the buffer.
7849 **
7850 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
7851 ** returning.
7852 */
7853 static void sessionAppendStr(
7854 SessionBuffer *p,
7855 const char *zStr,
7856 int *pRc
7857 ){
7858 int nStr = sqlite3Strlen30(zStr);
7859 if( 0==sessionBufferGrow(p, nStr, pRc) ){
7860 memcpy(&p->aBuf[p->nBuf], zStr, nStr);
7861 p->nBuf += nStr;
7862 }
7863 }
7864
7865 /*
7866 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
7867 ** called. Otherwise, append the string representation of integer iVal
7868 ** to the buffer. No nul-terminator is written.
7869 **
7870 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
7871 ** returning.
7872 */
7873 static void sessionAppendInteger(
7874 SessionBuffer *p, /* Buffer to append to */
7875 int iVal, /* Value to write the string rep. of */
7876 int *pRc /* IN/OUT: Error code */
7877 ){
7878 char aBuf[24];
7879 sqlite3_snprintf(sizeof(aBuf)-1, aBuf, "%d", iVal);
7880 sessionAppendStr(p, aBuf, pRc);
7881 }
7882
7883 /*
7884 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
7885 ** called. Otherwise, append the string zStr enclosed in quotes (") and
7886 ** with any embedded quote characters escaped to the buffer. No
7887 ** nul-terminator byte is written.
7888 **
7889 ** If an OOM condition is encountered, set *pRc to SQLITE_NOMEM before
7890 ** returning.
7891 */
7892 static void sessionAppendIdent(
7893 SessionBuffer *p, /* Buffer to a append to */
7894 const char *zStr, /* String to quote, escape and append */
7895 int *pRc /* IN/OUT: Error code */
7896 ){
7897 int nStr = sqlite3Strlen30(zStr)*2 + 2 + 1;
7898 if( 0==sessionBufferGrow(p, nStr, pRc) ){
7899 char *zOut = (char *)&p->aBuf[p->nBuf];
7900 const char *zIn = zStr;
7901 *zOut++ = '"';
7902 while( *zIn ){
7903 if( *zIn=='"' ) *zOut++ = '"';
7904 *zOut++ = *(zIn++);
7905 }
7906 *zOut++ = '"';
7907 p->nBuf = (int)((u8 *)zOut - p->aBuf);
7908 }
7909 }
7910
7911 /*
7912 ** This function is a no-op if *pRc is other than SQLITE_OK when it is
7913 ** called. Otherwse, it appends the serialized version of the value stored
7914 ** in column iCol of the row that SQL statement pStmt currently points
7915 ** to to the buffer.
7916 */
7917 static void sessionAppendCol(
7918 SessionBuffer *p, /* Buffer to append to */
7919 sqlite3_stmt *pStmt, /* Handle pointing to row containing value */
7920 int iCol, /* Column to read value from */
7921 int *pRc /* IN/OUT: Error code */
7922 ){
7923 if( *pRc==SQLITE_OK ){
7924 int eType = sqlite3_column_type(pStmt, iCol);
7925 sessionAppendByte(p, (u8)eType, pRc);
7926 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
7927 sqlite3_int64 i;
7928 u8 aBuf[8];
7929 if( eType==SQLITE_INTEGER ){
7930 i = sqlite3_column_int64(pStmt, iCol);
7931 }else{
7932 double r = sqlite3_column_double(pStmt, iCol);
7933 memcpy(&i, &r, 8);
7934 }
7935 sessionPutI64(aBuf, i);
7936 sessionAppendBlob(p, aBuf, 8, pRc);
7937 }
7938 if( eType==SQLITE_BLOB || eType==SQLITE_TEXT ){
7939 u8 *z;
7940 int nByte;
7941 if( eType==SQLITE_BLOB ){
7942 z = (u8 *)sqlite3_column_blob(pStmt, iCol);
7943 }else{
7944 z = (u8 *)sqlite3_column_text(pStmt, iCol);
7945 }
7946 nByte = sqlite3_column_bytes(pStmt, iCol);
7947 if( z || (eType==SQLITE_BLOB && nByte==0) ){
7948 sessionAppendVarint(p, nByte, pRc);
7949 sessionAppendBlob(p, z, nByte, pRc);
7950 }else{
7951 *pRc = SQLITE_NOMEM;
7952 }
7953 }
7954 }
7955 }
7956
7957 /*
7958 **
7959 ** This function appends an update change to the buffer (see the comments
7960 ** under "CHANGESET FORMAT" at the top of the file). An update change
7961 ** consists of:
7962 **
7963 ** 1 byte: SQLITE_UPDATE (0x17)
7964 ** n bytes: old.* record (see RECORD FORMAT)
7965 ** m bytes: new.* record (see RECORD FORMAT)
7966 **
7967 ** The SessionChange object passed as the third argument contains the
7968 ** values that were stored in the row when the session began (the old.*
7969 ** values). The statement handle passed as the second argument points
7970 ** at the current version of the row (the new.* values).
7971 **
7972 ** If all of the old.* values are equal to their corresponding new.* value
7973 ** (i.e. nothing has changed), then no data at all is appended to the buffer.
7974 **
7975 ** Otherwise, the old.* record contains all primary key values and the
7976 ** original values of any fields that have been modified. The new.* record
7977 ** contains the new values of only those fields that have been modified.
7978 */
7979 static int sessionAppendUpdate(
7980 SessionBuffer *pBuf, /* Buffer to append to */
7981 int bPatchset, /* True for "patchset", 0 for "changeset" */
7982 sqlite3_stmt *pStmt, /* Statement handle pointing at new row */
7983 SessionChange *p, /* Object containing old values */
7984 u8 *abPK /* Boolean array - true for PK columns */
7985 ){
7986 int rc = SQLITE_OK;
7987 SessionBuffer buf2 = {0,0,0}; /* Buffer to accumulate new.* record in */
7988 int bNoop = 1; /* Set to zero if any values are modified */
7989 int nRewind = pBuf->nBuf; /* Set to zero if any values are modified */
7990 int i; /* Used to iterate through columns */
7991 u8 *pCsr = p->aRecord; /* Used to iterate through old.* values */
7992
7993 sessionAppendByte(pBuf, SQLITE_UPDATE, &rc);
7994 sessionAppendByte(pBuf, p->bIndirect, &rc);
7995 for(i=0; i<sqlite3_column_count(pStmt); i++){
7996 int bChanged = 0;
7997 int nAdvance;
7998 int eType = *pCsr;
7999 switch( eType ){
8000 case SQLITE_NULL:
8001 nAdvance = 1;
8002 if( sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
8003 bChanged = 1;
8004 }
8005 break;
8006
8007 case SQLITE_FLOAT:
8008 case SQLITE_INTEGER: {
8009 nAdvance = 9;
8010 if( eType==sqlite3_column_type(pStmt, i) ){
8011 sqlite3_int64 iVal = sessionGetI64(&pCsr[1]);
8012 if( eType==SQLITE_INTEGER ){
8013 if( iVal==sqlite3_column_int64(pStmt, i) ) break;
8014 }else{
8015 double dVal;
8016 memcpy(&dVal, &iVal, 8);
8017 if( dVal==sqlite3_column_double(pStmt, i) ) break;
8018 }
8019 }
8020 bChanged = 1;
8021 break;
8022 }
8023
8024 default: {
8025 int n;
8026 int nHdr = 1 + sessionVarintGet(&pCsr[1], &n);
8027 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
8028 nAdvance = nHdr + n;
8029 if( eType==sqlite3_column_type(pStmt, i)
8030 && n==sqlite3_column_bytes(pStmt, i)
8031 && (n==0 || 0==memcmp(&pCsr[nHdr], sqlite3_column_blob(pStmt, i), n))
8032 ){
8033 break;
8034 }
8035 bChanged = 1;
8036 }
8037 }
8038
8039 /* If at least one field has been modified, this is not a no-op. */
8040 if( bChanged ) bNoop = 0;
8041
8042 /* Add a field to the old.* record. This is omitted if this modules is
8043 ** currently generating a patchset. */
8044 if( bPatchset==0 ){
8045 if( bChanged || abPK[i] ){
8046 sessionAppendBlob(pBuf, pCsr, nAdvance, &rc);
8047 }else{
8048 sessionAppendByte(pBuf, 0, &rc);
8049 }
8050 }
8051
8052 /* Add a field to the new.* record. Or the only record if currently
8053 ** generating a patchset. */
8054 if( bChanged || (bPatchset && abPK[i]) ){
8055 sessionAppendCol(&buf2, pStmt, i, &rc);
8056 }else{
8057 sessionAppendByte(&buf2, 0, &rc);
8058 }
8059
8060 pCsr += nAdvance;
8061 }
8062
8063 if( bNoop ){
8064 pBuf->nBuf = nRewind;
8065 }else{
8066 sessionAppendBlob(pBuf, buf2.aBuf, buf2.nBuf, &rc);
8067 }
8068 sqlite3_free(buf2.aBuf);
8069
8070 return rc;
8071 }
8072
8073 /*
8074 ** Append a DELETE change to the buffer passed as the first argument. Use
8075 ** the changeset format if argument bPatchset is zero, or the patchset
8076 ** format otherwise.
8077 */
8078 static int sessionAppendDelete(
8079 SessionBuffer *pBuf, /* Buffer to append to */
8080 int bPatchset, /* True for "patchset", 0 for "changeset" */
8081 SessionChange *p, /* Object containing old values */
8082 int nCol, /* Number of columns in table */
8083 u8 *abPK /* Boolean array - true for PK columns */
8084 ){
8085 int rc = SQLITE_OK;
8086
8087 sessionAppendByte(pBuf, SQLITE_DELETE, &rc);
8088 sessionAppendByte(pBuf, p->bIndirect, &rc);
8089
8090 if( bPatchset==0 ){
8091 sessionAppendBlob(pBuf, p->aRecord, p->nRecord, &rc);
8092 }else{
8093 int i;
8094 u8 *a = p->aRecord;
8095 for(i=0; i<nCol; i++){
8096 u8 *pStart = a;
8097 int eType = *a++;
8098
8099 switch( eType ){
8100 case 0:
8101 case SQLITE_NULL:
8102 assert( abPK[i]==0 );
8103 break;
8104
8105 case SQLITE_FLOAT:
8106 case SQLITE_INTEGER:
8107 a += 8;
8108 break;
8109
8110 default: {
8111 int n;
8112 a += sessionVarintGet(a, &n);
8113 a += n;
8114 break;
8115 }
8116 }
8117 if( abPK[i] ){
8118 sessionAppendBlob(pBuf, pStart, (int)(a-pStart), &rc);
8119 }
8120 }
8121 assert( (a - p->aRecord)==p->nRecord );
8122 }
8123
8124 return rc;
8125 }
8126
8127 /*
8128 ** Formulate and prepare a SELECT statement to retrieve a row from table
8129 ** zTab in database zDb based on its primary key. i.e.
8130 **
8131 ** SELECT * FROM zDb.zTab WHERE pk1 = ? AND pk2 = ? AND ...
8132 */
8133 static int sessionSelectStmt(
8134 sqlite3 *db, /* Database handle */
8135 const char *zDb, /* Database name */
8136 const char *zTab, /* Table name */
8137 int nCol, /* Number of columns in table */
8138 const char **azCol, /* Names of table columns */
8139 u8 *abPK, /* PRIMARY KEY array */
8140 sqlite3_stmt **ppStmt /* OUT: Prepared SELECT statement */
8141 ){
8142 int rc = SQLITE_OK;
8143 int i;
8144 const char *zSep = "";
8145 SessionBuffer buf = {0, 0, 0};
8146
8147 sessionAppendStr(&buf, "SELECT * FROM ", &rc);
8148 sessionAppendIdent(&buf, zDb, &rc);
8149 sessionAppendStr(&buf, ".", &rc);
8150 sessionAppendIdent(&buf, zTab, &rc);
8151 sessionAppendStr(&buf, " WHERE ", &rc);
8152 for(i=0; i<nCol; i++){
8153 if( abPK[i] ){
8154 sessionAppendStr(&buf, zSep, &rc);
8155 sessionAppendIdent(&buf, azCol[i], &rc);
8156 sessionAppendStr(&buf, " = ?", &rc);
8157 sessionAppendInteger(&buf, i+1, &rc);
8158 zSep = " AND ";
8159 }
8160 }
8161 if( rc==SQLITE_OK ){
8162 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, ppStmt, 0);
8163 }
8164 sqlite3_free(buf.aBuf);
8165 return rc;
8166 }
8167
8168 /*
8169 ** Bind the PRIMARY KEY values from the change passed in argument pChange
8170 ** to the SELECT statement passed as the first argument. The SELECT statement
8171 ** is as prepared by function sessionSelectStmt().
8172 **
8173 ** Return SQLITE_OK if all PK values are successfully bound, or an SQLite
8174 ** error code (e.g. SQLITE_NOMEM) otherwise.
8175 */
8176 static int sessionSelectBind(
8177 sqlite3_stmt *pSelect, /* SELECT from sessionSelectStmt() */
8178 int nCol, /* Number of columns in table */
8179 u8 *abPK, /* PRIMARY KEY array */
8180 SessionChange *pChange /* Change structure */
8181 ){
8182 int i;
8183 int rc = SQLITE_OK;
8184 u8 *a = pChange->aRecord;
8185
8186 for(i=0; i<nCol && rc==SQLITE_OK; i++){
8187 int eType = *a++;
8188
8189 switch( eType ){
8190 case 0:
8191 case SQLITE_NULL:
8192 assert( abPK[i]==0 );
8193 break;
8194
8195 case SQLITE_INTEGER: {
8196 if( abPK[i] ){
8197 i64 iVal = sessionGetI64(a);
8198 rc = sqlite3_bind_int64(pSelect, i+1, iVal);
8199 }
8200 a += 8;
8201 break;
8202 }
8203
8204 case SQLITE_FLOAT: {
8205 if( abPK[i] ){
8206 double rVal;
8207 i64 iVal = sessionGetI64(a);
8208 memcpy(&rVal, &iVal, 8);
8209 rc = sqlite3_bind_double(pSelect, i+1, rVal);
8210 }
8211 a += 8;
8212 break;
8213 }
8214
8215 case SQLITE_TEXT: {
8216 int n;
8217 a += sessionVarintGet(a, &n);
8218 if( abPK[i] ){
8219 rc = sqlite3_bind_text(pSelect, i+1, (char *)a, n, SQLITE_TRANSIENT);
8220 }
8221 a += n;
8222 break;
8223 }
8224
8225 default: {
8226 int n;
8227 assert( eType==SQLITE_BLOB );
8228 a += sessionVarintGet(a, &n);
8229 if( abPK[i] ){
8230 rc = sqlite3_bind_blob(pSelect, i+1, a, n, SQLITE_TRANSIENT);
8231 }
8232 a += n;
8233 break;
8234 }
8235 }
8236 }
8237
8238 return rc;
8239 }
8240
8241 /*
8242 ** This function is a no-op if *pRc is set to other than SQLITE_OK when it
8243 ** is called. Otherwise, append a serialized table header (part of the binary
8244 ** changeset format) to buffer *pBuf. If an error occurs, set *pRc to an
8245 ** SQLite error code before returning.
8246 */
8247 static void sessionAppendTableHdr(
8248 SessionBuffer *pBuf, /* Append header to this buffer */
8249 int bPatchset, /* Use the patchset format if true */
8250 SessionTable *pTab, /* Table object to append header for */
8251 int *pRc /* IN/OUT: Error code */
8252 ){
8253 /* Write a table header */
8254 sessionAppendByte(pBuf, (bPatchset ? 'P' : 'T'), pRc);
8255 sessionAppendVarint(pBuf, pTab->nCol, pRc);
8256 sessionAppendBlob(pBuf, pTab->abPK, pTab->nCol, pRc);
8257 sessionAppendBlob(pBuf, (u8 *)pTab->zName, (int)strlen(pTab->zName)+1, pRc);
8258 }
8259
8260 /*
8261 ** Generate either a changeset (if argument bPatchset is zero) or a patchset
8262 ** (if it is non-zero) based on the current contents of the session object
8263 ** passed as the first argument.
8264 **
8265 ** If no error occurs, SQLITE_OK is returned and the new changeset/patchset
8266 ** stored in output variables *pnChangeset and *ppChangeset. Or, if an error
8267 ** occurs, an SQLite error code is returned and both output variables set
8268 ** to 0.
8269 */
8270 static int sessionGenerateChangeset(
8271 sqlite3_session *pSession, /* Session object */
8272 int bPatchset, /* True for patchset, false for changeset */
8273 int (*xOutput)(void *pOut, const void *pData, int nData),
8274 void *pOut, /* First argument for xOutput */
8275 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
8276 void **ppChangeset /* OUT: Buffer containing changeset */
8277 ){
8278 sqlite3 *db = pSession->db; /* Source database handle */
8279 SessionTable *pTab; /* Used to iterate through attached tables */
8280 SessionBuffer buf = {0,0,0}; /* Buffer in which to accumlate changeset */
8281 int rc; /* Return code */
8282
8283 assert( xOutput==0 || (pnChangeset==0 && ppChangeset==0 ) );
8284
8285 /* Zero the output variables in case an error occurs. If this session
8286 ** object is already in the error state (sqlite3_session.rc != SQLITE_OK),
8287 ** this call will be a no-op. */
8288 if( xOutput==0 ){
8289 *pnChangeset = 0;
8290 *ppChangeset = 0;
8291 }
8292
8293 if( pSession->rc ) return pSession->rc;
8294 rc = sqlite3_exec(pSession->db, "SAVEPOINT changeset", 0, 0, 0);
8295 if( rc!=SQLITE_OK ) return rc;
8296
8297 sqlite3_mutex_enter(sqlite3_db_mutex(db));
8298
8299 for(pTab=pSession->pTable; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
8300 if( pTab->nEntry ){
8301 const char *zName = pTab->zName;
8302 int nCol; /* Number of columns in table */
8303 u8 *abPK; /* Primary key array */
8304 const char **azCol = 0; /* Table columns */
8305 int i; /* Used to iterate through hash buckets */
8306 sqlite3_stmt *pSel = 0; /* SELECT statement to query table pTab */
8307 int nRewind = buf.nBuf; /* Initial size of write buffer */
8308 int nNoop; /* Size of buffer after writing tbl header */
8309
8310 /* Check the table schema is still Ok. */
8311 rc = sessionTableInfo(db, pSession->zDb, zName, &nCol, 0, &azCol, &abPK);
8312 if( !rc && (pTab->nCol!=nCol || memcmp(abPK, pTab->abPK, nCol)) ){
8313 rc = SQLITE_SCHEMA;
8314 }
8315
8316 /* Write a table header */
8317 sessionAppendTableHdr(&buf, bPatchset, pTab, &rc);
8318
8319 /* Build and compile a statement to execute: */
8320 if( rc==SQLITE_OK ){
8321 rc = sessionSelectStmt(
8322 db, pSession->zDb, zName, nCol, azCol, abPK, &pSel);
8323 }
8324
8325 nNoop = buf.nBuf;
8326 for(i=0; i<pTab->nChange && rc==SQLITE_OK; i++){
8327 SessionChange *p; /* Used to iterate through changes */
8328
8329 for(p=pTab->apChange[i]; rc==SQLITE_OK && p; p=p->pNext){
8330 rc = sessionSelectBind(pSel, nCol, abPK, p);
8331 if( rc!=SQLITE_OK ) continue;
8332 if( sqlite3_step(pSel)==SQLITE_ROW ){
8333 if( p->op==SQLITE_INSERT ){
8334 int iCol;
8335 sessionAppendByte(&buf, SQLITE_INSERT, &rc);
8336 sessionAppendByte(&buf, p->bIndirect, &rc);
8337 for(iCol=0; iCol<nCol; iCol++){
8338 sessionAppendCol(&buf, pSel, iCol, &rc);
8339 }
8340 }else{
8341 rc = sessionAppendUpdate(&buf, bPatchset, pSel, p, abPK);
8342 }
8343 }else if( p->op!=SQLITE_INSERT ){
8344 rc = sessionAppendDelete(&buf, bPatchset, p, nCol, abPK);
8345 }
8346 if( rc==SQLITE_OK ){
8347 rc = sqlite3_reset(pSel);
8348 }
8349
8350 /* If the buffer is now larger than SESSIONS_STRM_CHUNK_SIZE, pass
8351 ** its contents to the xOutput() callback. */
8352 if( xOutput
8353 && rc==SQLITE_OK
8354 && buf.nBuf>nNoop
8355 && buf.nBuf>SESSIONS_STRM_CHUNK_SIZE
8356 ){
8357 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
8358 nNoop = -1;
8359 buf.nBuf = 0;
8360 }
8361
8362 }
8363 }
8364
8365 sqlite3_finalize(pSel);
8366 if( buf.nBuf==nNoop ){
8367 buf.nBuf = nRewind;
8368 }
8369 sqlite3_free((char*)azCol); /* cast works around VC++ bug */
8370 }
8371 }
8372
8373 if( rc==SQLITE_OK ){
8374 if( xOutput==0 ){
8375 *pnChangeset = buf.nBuf;
8376 *ppChangeset = buf.aBuf;
8377 buf.aBuf = 0;
8378 }else if( buf.nBuf>0 ){
8379 rc = xOutput(pOut, (void*)buf.aBuf, buf.nBuf);
8380 }
8381 }
8382
8383 sqlite3_free(buf.aBuf);
8384 sqlite3_exec(db, "RELEASE changeset", 0, 0, 0);
8385 sqlite3_mutex_leave(sqlite3_db_mutex(db));
8386 return rc;
8387 }
8388
8389 /*
8390 ** Obtain a changeset object containing all changes recorded by the
8391 ** session object passed as the first argument.
8392 **
8393 ** It is the responsibility of the caller to eventually free the buffer
8394 ** using sqlite3_free().
8395 */
8396 SQLITE_API int sqlite3session_changeset(
8397 sqlite3_session *pSession, /* Session object */
8398 int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */
8399 void **ppChangeset /* OUT: Buffer containing changeset */
8400 ){
8401 return sessionGenerateChangeset(pSession, 0, 0, 0, pnChangeset, ppChangeset);
8402 }
8403
8404 /*
8405 ** Streaming version of sqlite3session_changeset().
8406 */
8407 SQLITE_API int sqlite3session_changeset_strm(
8408 sqlite3_session *pSession,
8409 int (*xOutput)(void *pOut, const void *pData, int nData),
8410 void *pOut
8411 ){
8412 return sessionGenerateChangeset(pSession, 0, xOutput, pOut, 0, 0);
8413 }
8414
8415 /*
8416 ** Streaming version of sqlite3session_patchset().
8417 */
8418 SQLITE_API int sqlite3session_patchset_strm(
8419 sqlite3_session *pSession,
8420 int (*xOutput)(void *pOut, const void *pData, int nData),
8421 void *pOut
8422 ){
8423 return sessionGenerateChangeset(pSession, 1, xOutput, pOut, 0, 0);
8424 }
8425
8426 /*
8427 ** Obtain a patchset object containing all changes recorded by the
8428 ** session object passed as the first argument.
8429 **
8430 ** It is the responsibility of the caller to eventually free the buffer
8431 ** using sqlite3_free().
8432 */
8433 SQLITE_API int sqlite3session_patchset(
8434 sqlite3_session *pSession, /* Session object */
8435 int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */
8436 void **ppPatchset /* OUT: Buffer containing changeset */
8437 ){
8438 return sessionGenerateChangeset(pSession, 1, 0, 0, pnPatchset, ppPatchset);
8439 }
8440
8441 /*
8442 ** Enable or disable the session object passed as the first argument.
8443 */
8444 SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable){
8445 int ret;
8446 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
8447 if( bEnable>=0 ){
8448 pSession->bEnable = bEnable;
8449 }
8450 ret = pSession->bEnable;
8451 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
8452 return ret;
8453 }
8454
8455 /*
8456 ** Enable or disable the session object passed as the first argument.
8457 */
8458 SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect) {
8459 int ret;
8460 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
8461 if( bIndirect>=0 ){
8462 pSession->bIndirect = bIndirect;
8463 }
8464 ret = pSession->bIndirect;
8465 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
8466 return ret;
8467 }
8468
8469 /*
8470 ** Return true if there have been no changes to monitored tables recorded
8471 ** by the session object passed as the only argument.
8472 */
8473 SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession){
8474 int ret = 0;
8475 SessionTable *pTab;
8476
8477 sqlite3_mutex_enter(sqlite3_db_mutex(pSession->db));
8478 for(pTab=pSession->pTable; pTab && ret==0; pTab=pTab->pNext){
8479 ret = (pTab->nEntry>0);
8480 }
8481 sqlite3_mutex_leave(sqlite3_db_mutex(pSession->db));
8482
8483 return (ret==0);
8484 }
8485
8486 /*
8487 ** Do the work for either sqlite3changeset_start() or start_strm().
8488 */
8489 static int sessionChangesetStart(
8490 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
8491 int (*xInput)(void *pIn, void *pData, int *pnData),
8492 void *pIn,
8493 int nChangeset, /* Size of buffer pChangeset in bytes */
8494 void *pChangeset /* Pointer to buffer containing changeset */
8495 ){
8496 sqlite3_changeset_iter *pRet; /* Iterator to return */
8497 int nByte; /* Number of bytes to allocate for iterator */
8498
8499 assert( xInput==0 || (pChangeset==0 && nChangeset==0) );
8500
8501 /* Zero the output variable in case an error occurs. */
8502 *pp = 0;
8503
8504 /* Allocate and initialize the iterator structure. */
8505 nByte = sizeof(sqlite3_changeset_iter);
8506 pRet = (sqlite3_changeset_iter *)sqlite3_malloc(nByte);
8507 if( !pRet ) return SQLITE_NOMEM;
8508 memset(pRet, 0, sizeof(sqlite3_changeset_iter));
8509 pRet->in.aData = (u8 *)pChangeset;
8510 pRet->in.nData = nChangeset;
8511 pRet->in.xInput = xInput;
8512 pRet->in.pIn = pIn;
8513 pRet->in.bEof = (xInput ? 0 : 1);
8514
8515 /* Populate the output variable and return success. */
8516 *pp = pRet;
8517 return SQLITE_OK;
8518 }
8519
8520 /*
8521 ** Create an iterator used to iterate through the contents of a changeset.
8522 */
8523 SQLITE_API int sqlite3changeset_start(
8524 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
8525 int nChangeset, /* Size of buffer pChangeset in bytes */
8526 void *pChangeset /* Pointer to buffer containing changeset */
8527 ){
8528 return sessionChangesetStart(pp, 0, 0, nChangeset, pChangeset);
8529 }
8530
8531 /*
8532 ** Streaming version of sqlite3changeset_start().
8533 */
8534 SQLITE_API int sqlite3changeset_start_strm(
8535 sqlite3_changeset_iter **pp, /* OUT: Changeset iterator handle */
8536 int (*xInput)(void *pIn, void *pData, int *pnData),
8537 void *pIn
8538 ){
8539 return sessionChangesetStart(pp, xInput, pIn, 0, 0);
8540 }
8541
8542 /*
8543 ** If the SessionInput object passed as the only argument is a streaming
8544 ** object and the buffer is full, discard some data to free up space.
8545 */
8546 static void sessionDiscardData(SessionInput *pIn){
8547 if( pIn->bEof && pIn->xInput && pIn->iNext>=SESSIONS_STRM_CHUNK_SIZE ){
8548 int nMove = pIn->buf.nBuf - pIn->iNext;
8549 assert( nMove>=0 );
8550 if( nMove>0 ){
8551 memmove(pIn->buf.aBuf, &pIn->buf.aBuf[pIn->iNext], nMove);
8552 }
8553 pIn->buf.nBuf -= pIn->iNext;
8554 pIn->iNext = 0;
8555 pIn->nData = pIn->buf.nBuf;
8556 }
8557 }
8558
8559 /*
8560 ** Ensure that there are at least nByte bytes available in the buffer. Or,
8561 ** if there are not nByte bytes remaining in the input, that all available
8562 ** data is in the buffer.
8563 **
8564 ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
8565 */
8566 static int sessionInputBuffer(SessionInput *pIn, int nByte){
8567 int rc = SQLITE_OK;
8568 if( pIn->xInput ){
8569 while( !pIn->bEof && (pIn->iNext+nByte)>=pIn->nData && rc==SQLITE_OK ){
8570 int nNew = SESSIONS_STRM_CHUNK_SIZE;
8571
8572 if( pIn->bNoDiscard==0 ) sessionDiscardData(pIn);
8573 if( SQLITE_OK==sessionBufferGrow(&pIn->buf, nNew, &rc) ){
8574 rc = pIn->xInput(pIn->pIn, &pIn->buf.aBuf[pIn->buf.nBuf], &nNew);
8575 if( nNew==0 ){
8576 pIn->bEof = 1;
8577 }else{
8578 pIn->buf.nBuf += nNew;
8579 }
8580 }
8581
8582 pIn->aData = pIn->buf.aBuf;
8583 pIn->nData = pIn->buf.nBuf;
8584 }
8585 }
8586 return rc;
8587 }
8588
8589 /*
8590 ** When this function is called, *ppRec points to the start of a record
8591 ** that contains nCol values. This function advances the pointer *ppRec
8592 ** until it points to the byte immediately following that record.
8593 */
8594 static void sessionSkipRecord(
8595 u8 **ppRec, /* IN/OUT: Record pointer */
8596 int nCol /* Number of values in record */
8597 ){
8598 u8 *aRec = *ppRec;
8599 int i;
8600 for(i=0; i<nCol; i++){
8601 int eType = *aRec++;
8602 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
8603 int nByte;
8604 aRec += sessionVarintGet((u8*)aRec, &nByte);
8605 aRec += nByte;
8606 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
8607 aRec += 8;
8608 }
8609 }
8610
8611 *ppRec = aRec;
8612 }
8613
8614 /*
8615 ** This function sets the value of the sqlite3_value object passed as the
8616 ** first argument to a copy of the string or blob held in the aData[]
8617 ** buffer. SQLITE_OK is returned if successful, or SQLITE_NOMEM if an OOM
8618 ** error occurs.
8619 */
8620 static int sessionValueSetStr(
8621 sqlite3_value *pVal, /* Set the value of this object */
8622 u8 *aData, /* Buffer containing string or blob data */
8623 int nData, /* Size of buffer aData[] in bytes */
8624 u8 enc /* String encoding (0 for blobs) */
8625 ){
8626 /* In theory this code could just pass SQLITE_TRANSIENT as the final
8627 ** argument to sqlite3ValueSetStr() and have the copy created
8628 ** automatically. But doing so makes it difficult to detect any OOM
8629 ** error. Hence the code to create the copy externally. */
8630 u8 *aCopy = sqlite3_malloc(nData+1);
8631 if( aCopy==0 ) return SQLITE_NOMEM;
8632 memcpy(aCopy, aData, nData);
8633 sqlite3ValueSetStr(pVal, nData, (char*)aCopy, enc, sqlite3_free);
8634 return SQLITE_OK;
8635 }
8636
8637 /*
8638 ** Deserialize a single record from a buffer in memory. See "RECORD FORMAT"
8639 ** for details.
8640 **
8641 ** When this function is called, *paChange points to the start of the record
8642 ** to deserialize. Assuming no error occurs, *paChange is set to point to
8643 ** one byte after the end of the same record before this function returns.
8644 ** If the argument abPK is NULL, then the record contains nCol values. Or,
8645 ** if abPK is other than NULL, then the record contains only the PK fields
8646 ** (in other words, it is a patchset DELETE record).
8647 **
8648 ** If successful, each element of the apOut[] array (allocated by the caller)
8649 ** is set to point to an sqlite3_value object containing the value read
8650 ** from the corresponding position in the record. If that value is not
8651 ** included in the record (i.e. because the record is part of an UPDATE change
8652 ** and the field was not modified), the corresponding element of apOut[] is
8653 ** set to NULL.
8654 **
8655 ** It is the responsibility of the caller to free all sqlite_value structures
8656 ** using sqlite3_free().
8657 **
8658 ** If an error occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
8659 ** The apOut[] array may have been partially populated in this case.
8660 */
8661 static int sessionReadRecord(
8662 SessionInput *pIn, /* Input data */
8663 int nCol, /* Number of values in record */
8664 u8 *abPK, /* Array of primary key flags, or NULL */
8665 sqlite3_value **apOut /* Write values to this array */
8666 ){
8667 int i; /* Used to iterate through columns */
8668 int rc = SQLITE_OK;
8669
8670 for(i=0; i<nCol && rc==SQLITE_OK; i++){
8671 int eType = 0; /* Type of value (SQLITE_NULL, TEXT etc.) */
8672 if( abPK && abPK[i]==0 ) continue;
8673 rc = sessionInputBuffer(pIn, 9);
8674 if( rc==SQLITE_OK ){
8675 eType = pIn->aData[pIn->iNext++];
8676 }
8677
8678 assert( apOut[i]==0 );
8679 if( eType ){
8680 apOut[i] = sqlite3ValueNew(0);
8681 if( !apOut[i] ) rc = SQLITE_NOMEM;
8682 }
8683
8684 if( rc==SQLITE_OK ){
8685 u8 *aVal = &pIn->aData[pIn->iNext];
8686 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
8687 int nByte;
8688 pIn->iNext += sessionVarintGet(aVal, &nByte);
8689 rc = sessionInputBuffer(pIn, nByte);
8690 if( rc==SQLITE_OK ){
8691 u8 enc = (eType==SQLITE_TEXT ? SQLITE_UTF8 : 0);
8692 rc = sessionValueSetStr(apOut[i],&pIn->aData[pIn->iNext],nByte,enc);
8693 }
8694 pIn->iNext += nByte;
8695 }
8696 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
8697 sqlite3_int64 v = sessionGetI64(aVal);
8698 if( eType==SQLITE_INTEGER ){
8699 sqlite3VdbeMemSetInt64(apOut[i], v);
8700 }else{
8701 double d;
8702 memcpy(&d, &v, 8);
8703 sqlite3VdbeMemSetDouble(apOut[i], d);
8704 }
8705 pIn->iNext += 8;
8706 }
8707 }
8708 }
8709
8710 return rc;
8711 }
8712
8713 /*
8714 ** The input pointer currently points to the second byte of a table-header.
8715 ** Specifically, to the following:
8716 **
8717 ** + number of columns in table (varint)
8718 ** + array of PK flags (1 byte per column),
8719 ** + table name (nul terminated).
8720 **
8721 ** This function ensures that all of the above is present in the input
8722 ** buffer (i.e. that it can be accessed without any calls to xInput()).
8723 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
8724 ** The input pointer is not moved.
8725 */
8726 static int sessionChangesetBufferTblhdr(SessionInput *pIn, int *pnByte){
8727 int rc = SQLITE_OK;
8728 int nCol = 0;
8729 int nRead = 0;
8730
8731 rc = sessionInputBuffer(pIn, 9);
8732 if( rc==SQLITE_OK ){
8733 nRead += sessionVarintGet(&pIn->aData[pIn->iNext + nRead], &nCol);
8734 rc = sessionInputBuffer(pIn, nRead+nCol+100);
8735 nRead += nCol;
8736 }
8737
8738 while( rc==SQLITE_OK ){
8739 while( (pIn->iNext + nRead)<pIn->nData && pIn->aData[pIn->iNext + nRead] ){
8740 nRead++;
8741 }
8742 if( (pIn->iNext + nRead)<pIn->nData ) break;
8743 rc = sessionInputBuffer(pIn, nRead + 100);
8744 }
8745 *pnByte = nRead+1;
8746 return rc;
8747 }
8748
8749 /*
8750 ** The input pointer currently points to the first byte of the first field
8751 ** of a record consisting of nCol columns. This function ensures the entire
8752 ** record is buffered. It does not move the input pointer.
8753 **
8754 ** If successful, SQLITE_OK is returned and *pnByte is set to the size of
8755 ** the record in bytes. Otherwise, an SQLite error code is returned. The
8756 ** final value of *pnByte is undefined in this case.
8757 */
8758 static int sessionChangesetBufferRecord(
8759 SessionInput *pIn, /* Input data */
8760 int nCol, /* Number of columns in record */
8761 int *pnByte /* OUT: Size of record in bytes */
8762 ){
8763 int rc = SQLITE_OK;
8764 int nByte = 0;
8765 int i;
8766 for(i=0; rc==SQLITE_OK && i<nCol; i++){
8767 int eType;
8768 rc = sessionInputBuffer(pIn, nByte + 10);
8769 if( rc==SQLITE_OK ){
8770 eType = pIn->aData[pIn->iNext + nByte++];
8771 if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
8772 int n;
8773 nByte += sessionVarintGet(&pIn->aData[pIn->iNext+nByte], &n);
8774 nByte += n;
8775 rc = sessionInputBuffer(pIn, nByte);
8776 }else if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
8777 nByte += 8;
8778 }
8779 }
8780 }
8781 *pnByte = nByte;
8782 return rc;
8783 }
8784
8785 /*
8786 ** The input pointer currently points to the second byte of a table-header.
8787 ** Specifically, to the following:
8788 **
8789 ** + number of columns in table (varint)
8790 ** + array of PK flags (1 byte per column),
8791 ** + table name (nul terminated).
8792 **
8793 ** This function decodes the table-header and populates the p->nCol,
8794 ** p->zTab and p->abPK[] variables accordingly. The p->apValue[] array is
8795 ** also allocated or resized according to the new value of p->nCol. The
8796 ** input pointer is left pointing to the byte following the table header.
8797 **
8798 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code
8799 ** is returned and the final values of the various fields enumerated above
8800 ** are undefined.
8801 */
8802 static int sessionChangesetReadTblhdr(sqlite3_changeset_iter *p){
8803 int rc;
8804 int nCopy;
8805 assert( p->rc==SQLITE_OK );
8806
8807 rc = sessionChangesetBufferTblhdr(&p->in, &nCopy);
8808 if( rc==SQLITE_OK ){
8809 int nByte;
8810 int nVarint;
8811 nVarint = sessionVarintGet(&p->in.aData[p->in.iNext], &p->nCol);
8812 nCopy -= nVarint;
8813 p->in.iNext += nVarint;
8814 nByte = p->nCol * sizeof(sqlite3_value*) * 2 + nCopy;
8815 p->tblhdr.nBuf = 0;
8816 sessionBufferGrow(&p->tblhdr, nByte, &rc);
8817 }
8818
8819 if( rc==SQLITE_OK ){
8820 int iPK = sizeof(sqlite3_value*)*p->nCol*2;
8821 memset(p->tblhdr.aBuf, 0, iPK);
8822 memcpy(&p->tblhdr.aBuf[iPK], &p->in.aData[p->in.iNext], nCopy);
8823 p->in.iNext += nCopy;
8824 }
8825
8826 p->apValue = (sqlite3_value**)p->tblhdr.aBuf;
8827 p->abPK = (u8*)&p->apValue[p->nCol*2];
8828 p->zTab = (char*)&p->abPK[p->nCol];
8829 return (p->rc = rc);
8830 }
8831
8832 /*
8833 ** Advance the changeset iterator to the next change.
8834 **
8835 ** If both paRec and pnRec are NULL, then this function works like the public
8836 ** API sqlite3changeset_next(). If SQLITE_ROW is returned, then the
8837 ** sqlite3changeset_new() and old() APIs may be used to query for values.
8838 **
8839 ** Otherwise, if paRec and pnRec are not NULL, then a pointer to the change
8840 ** record is written to *paRec before returning and the number of bytes in
8841 ** the record to *pnRec.
8842 **
8843 ** Either way, this function returns SQLITE_ROW if the iterator is
8844 ** successfully advanced to the next change in the changeset, an SQLite
8845 ** error code if an error occurs, or SQLITE_DONE if there are no further
8846 ** changes in the changeset.
8847 */
8848 static int sessionChangesetNext(
8849 sqlite3_changeset_iter *p, /* Changeset iterator */
8850 u8 **paRec, /* If non-NULL, store record pointer here */
8851 int *pnRec /* If non-NULL, store size of record here */
8852 ){
8853 int i;
8854 u8 op;
8855
8856 assert( (paRec==0 && pnRec==0) || (paRec && pnRec) );
8857
8858 /* If the iterator is in the error-state, return immediately. */
8859 if( p->rc!=SQLITE_OK ) return p->rc;
8860
8861 /* Free the current contents of p->apValue[], if any. */
8862 if( p->apValue ){
8863 for(i=0; i<p->nCol*2; i++){
8864 sqlite3ValueFree(p->apValue[i]);
8865 }
8866 memset(p->apValue, 0, sizeof(sqlite3_value*)*p->nCol*2);
8867 }
8868
8869 /* Make sure the buffer contains at least 10 bytes of input data, or all
8870 ** remaining data if there are less than 10 bytes available. This is
8871 ** sufficient either for the 'T' or 'P' byte and the varint that follows
8872 ** it, or for the two single byte values otherwise. */
8873 p->rc = sessionInputBuffer(&p->in, 2);
8874 if( p->rc!=SQLITE_OK ) return p->rc;
8875
8876 /* If the iterator is already at the end of the changeset, return DONE. */
8877 if( p->in.iNext>=p->in.nData ){
8878 return SQLITE_DONE;
8879 }
8880
8881 sessionDiscardData(&p->in);
8882 p->in.iCurrent = p->in.iNext;
8883
8884 op = p->in.aData[p->in.iNext++];
8885 if( op=='T' || op=='P' ){
8886 p->bPatchset = (op=='P');
8887 if( sessionChangesetReadTblhdr(p) ) return p->rc;
8888 if( (p->rc = sessionInputBuffer(&p->in, 2)) ) return p->rc;
8889 p->in.iCurrent = p->in.iNext;
8890 op = p->in.aData[p->in.iNext++];
8891 }
8892
8893 p->op = op;
8894 p->bIndirect = p->in.aData[p->in.iNext++];
8895 if( p->op!=SQLITE_UPDATE && p->op!=SQLITE_DELETE && p->op!=SQLITE_INSERT ){
8896 return (p->rc = SQLITE_CORRUPT_BKPT);
8897 }
8898
8899 if( paRec ){
8900 int nVal; /* Number of values to buffer */
8901 if( p->bPatchset==0 && op==SQLITE_UPDATE ){
8902 nVal = p->nCol * 2;
8903 }else if( p->bPatchset && op==SQLITE_DELETE ){
8904 nVal = 0;
8905 for(i=0; i<p->nCol; i++) if( p->abPK[i] ) nVal++;
8906 }else{
8907 nVal = p->nCol;
8908 }
8909 p->rc = sessionChangesetBufferRecord(&p->in, nVal, pnRec);
8910 if( p->rc!=SQLITE_OK ) return p->rc;
8911 *paRec = &p->in.aData[p->in.iNext];
8912 p->in.iNext += *pnRec;
8913 }else{
8914
8915 /* If this is an UPDATE or DELETE, read the old.* record. */
8916 if( p->op!=SQLITE_INSERT && (p->bPatchset==0 || p->op==SQLITE_DELETE) ){
8917 u8 *abPK = p->bPatchset ? p->abPK : 0;
8918 p->rc = sessionReadRecord(&p->in, p->nCol, abPK, p->apValue);
8919 if( p->rc!=SQLITE_OK ) return p->rc;
8920 }
8921
8922 /* If this is an INSERT or UPDATE, read the new.* record. */
8923 if( p->op!=SQLITE_DELETE ){
8924 p->rc = sessionReadRecord(&p->in, p->nCol, 0, &p->apValue[p->nCol]);
8925 if( p->rc!=SQLITE_OK ) return p->rc;
8926 }
8927
8928 if( p->bPatchset && p->op==SQLITE_UPDATE ){
8929 /* If this is an UPDATE that is part of a patchset, then all PK and
8930 ** modified fields are present in the new.* record. The old.* record
8931 ** is currently completely empty. This block shifts the PK fields from
8932 ** new.* to old.*, to accommodate the code that reads these arrays. */
8933 for(i=0; i<p->nCol; i++){
8934 assert( p->apValue[i]==0 );
8935 assert( p->abPK[i]==0 || p->apValue[i+p->nCol] );
8936 if( p->abPK[i] ){
8937 p->apValue[i] = p->apValue[i+p->nCol];
8938 p->apValue[i+p->nCol] = 0;
8939 }
8940 }
8941 }
8942 }
8943
8944 return SQLITE_ROW;
8945 }
8946
8947 /*
8948 ** Advance an iterator created by sqlite3changeset_start() to the next
8949 ** change in the changeset. This function may return SQLITE_ROW, SQLITE_DONE
8950 ** or SQLITE_CORRUPT.
8951 **
8952 ** This function may not be called on iterators passed to a conflict handler
8953 ** callback by changeset_apply().
8954 */
8955 SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *p){
8956 return sessionChangesetNext(p, 0, 0);
8957 }
8958
8959 /*
8960 ** The following function extracts information on the current change
8961 ** from a changeset iterator. It may only be called after changeset_next()
8962 ** has returned SQLITE_ROW.
8963 */
8964 SQLITE_API int sqlite3changeset_op(
8965 sqlite3_changeset_iter *pIter, /* Iterator handle */
8966 const char **pzTab, /* OUT: Pointer to table name */
8967 int *pnCol, /* OUT: Number of columns in table */
8968 int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */
8969 int *pbIndirect /* OUT: True if change is indirect */
8970 ){
8971 *pOp = pIter->op;
8972 *pnCol = pIter->nCol;
8973 *pzTab = pIter->zTab;
8974 if( pbIndirect ) *pbIndirect = pIter->bIndirect;
8975 return SQLITE_OK;
8976 }
8977
8978 /*
8979 ** Return information regarding the PRIMARY KEY and number of columns in
8980 ** the database table affected by the change that pIter currently points
8981 ** to. This function may only be called after changeset_next() returns
8982 ** SQLITE_ROW.
8983 */
8984 SQLITE_API int sqlite3changeset_pk(
8985 sqlite3_changeset_iter *pIter, /* Iterator object */
8986 unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */
8987 int *pnCol /* OUT: Number of entries in output array */
8988 ){
8989 *pabPK = pIter->abPK;
8990 if( pnCol ) *pnCol = pIter->nCol;
8991 return SQLITE_OK;
8992 }
8993
8994 /*
8995 ** This function may only be called while the iterator is pointing to an
8996 ** SQLITE_UPDATE or SQLITE_DELETE change (see sqlite3changeset_op()).
8997 ** Otherwise, SQLITE_MISUSE is returned.
8998 **
8999 ** It sets *ppValue to point to an sqlite3_value structure containing the
9000 ** iVal'th value in the old.* record. Or, if that particular value is not
9001 ** included in the record (because the change is an UPDATE and the field
9002 ** was not modified and is not a PK column), set *ppValue to NULL.
9003 **
9004 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
9005 ** not modified. Otherwise, SQLITE_OK.
9006 */
9007 SQLITE_API int sqlite3changeset_old(
9008 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9009 int iVal, /* Index of old.* value to retrieve */
9010 sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */
9011 ){
9012 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_DELETE ){
9013 return SQLITE_MISUSE;
9014 }
9015 if( iVal<0 || iVal>=pIter->nCol ){
9016 return SQLITE_RANGE;
9017 }
9018 *ppValue = pIter->apValue[iVal];
9019 return SQLITE_OK;
9020 }
9021
9022 /*
9023 ** This function may only be called while the iterator is pointing to an
9024 ** SQLITE_UPDATE or SQLITE_INSERT change (see sqlite3changeset_op()).
9025 ** Otherwise, SQLITE_MISUSE is returned.
9026 **
9027 ** It sets *ppValue to point to an sqlite3_value structure containing the
9028 ** iVal'th value in the new.* record. Or, if that particular value is not
9029 ** included in the record (because the change is an UPDATE and the field
9030 ** was not modified), set *ppValue to NULL.
9031 **
9032 ** If value iVal is out-of-range, SQLITE_RANGE is returned and *ppValue is
9033 ** not modified. Otherwise, SQLITE_OK.
9034 */
9035 SQLITE_API int sqlite3changeset_new(
9036 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9037 int iVal, /* Index of new.* value to retrieve */
9038 sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */
9039 ){
9040 if( pIter->op!=SQLITE_UPDATE && pIter->op!=SQLITE_INSERT ){
9041 return SQLITE_MISUSE;
9042 }
9043 if( iVal<0 || iVal>=pIter->nCol ){
9044 return SQLITE_RANGE;
9045 }
9046 *ppValue = pIter->apValue[pIter->nCol+iVal];
9047 return SQLITE_OK;
9048 }
9049
9050 /*
9051 ** The following two macros are used internally. They are similar to the
9052 ** sqlite3changeset_new() and sqlite3changeset_old() functions, except that
9053 ** they omit all error checking and return a pointer to the requested value.
9054 */
9055 #define sessionChangesetNew(pIter, iVal) (pIter)->apValue[(pIter)->nCol+(iVal)]
9056 #define sessionChangesetOld(pIter, iVal) (pIter)->apValue[(iVal)]
9057
9058 /*
9059 ** This function may only be called with a changeset iterator that has been
9060 ** passed to an SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT
9061 ** conflict-handler function. Otherwise, SQLITE_MISUSE is returned.
9062 **
9063 ** If successful, *ppValue is set to point to an sqlite3_value structure
9064 ** containing the iVal'th value of the conflicting record.
9065 **
9066 ** If value iVal is out-of-range or some other error occurs, an SQLite error
9067 ** code is returned. Otherwise, SQLITE_OK.
9068 */
9069 SQLITE_API int sqlite3changeset_conflict(
9070 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9071 int iVal, /* Index of conflict record value to fetch */
9072 sqlite3_value **ppValue /* OUT: Value from conflicting row */
9073 ){
9074 if( !pIter->pConflict ){
9075 return SQLITE_MISUSE;
9076 }
9077 if( iVal<0 || iVal>=pIter->nCol ){
9078 return SQLITE_RANGE;
9079 }
9080 *ppValue = sqlite3_column_value(pIter->pConflict, iVal);
9081 return SQLITE_OK;
9082 }
9083
9084 /*
9085 ** This function may only be called with an iterator passed to an
9086 ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
9087 ** it sets the output variable to the total number of known foreign key
9088 ** violations in the destination database and returns SQLITE_OK.
9089 **
9090 ** In all other cases this function returns SQLITE_MISUSE.
9091 */
9092 SQLITE_API int sqlite3changeset_fk_conflicts(
9093 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9094 int *pnOut /* OUT: Number of FK violations */
9095 ){
9096 if( pIter->pConflict || pIter->apValue ){
9097 return SQLITE_MISUSE;
9098 }
9099 *pnOut = pIter->nCol;
9100 return SQLITE_OK;
9101 }
9102
9103
9104 /*
9105 ** Finalize an iterator allocated with sqlite3changeset_start().
9106 **
9107 ** This function may not be called on iterators passed to a conflict handler
9108 ** callback by changeset_apply().
9109 */
9110 SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *p){
9111 int rc = SQLITE_OK;
9112 if( p ){
9113 int i; /* Used to iterate through p->apValue[] */
9114 rc = p->rc;
9115 if( p->apValue ){
9116 for(i=0; i<p->nCol*2; i++) sqlite3ValueFree(p->apValue[i]);
9117 }
9118 sqlite3_free(p->tblhdr.aBuf);
9119 sqlite3_free(p->in.buf.aBuf);
9120 sqlite3_free(p);
9121 }
9122 return rc;
9123 }
9124
9125 static int sessionChangesetInvert(
9126 SessionInput *pInput, /* Input changeset */
9127 int (*xOutput)(void *pOut, const void *pData, int nData),
9128 void *pOut,
9129 int *pnInverted, /* OUT: Number of bytes in output changeset */
9130 void **ppInverted /* OUT: Inverse of pChangeset */
9131 ){
9132 int rc = SQLITE_OK; /* Return value */
9133 SessionBuffer sOut; /* Output buffer */
9134 int nCol = 0; /* Number of cols in current table */
9135 u8 *abPK = 0; /* PK array for current table */
9136 sqlite3_value **apVal = 0; /* Space for values for UPDATE inversion */
9137 SessionBuffer sPK = {0, 0, 0}; /* PK array for current table */
9138
9139 /* Initialize the output buffer */
9140 memset(&sOut, 0, sizeof(SessionBuffer));
9141
9142 /* Zero the output variables in case an error occurs. */
9143 if( ppInverted ){
9144 *ppInverted = 0;
9145 *pnInverted = 0;
9146 }
9147
9148 while( 1 ){
9149 u8 eType;
9150
9151 /* Test for EOF. */
9152 if( (rc = sessionInputBuffer(pInput, 2)) ) goto finished_invert;
9153 if( pInput->iNext>=pInput->nData ) break;
9154 eType = pInput->aData[pInput->iNext];
9155
9156 switch( eType ){
9157 case 'T': {
9158 /* A 'table' record consists of:
9159 **
9160 ** * A constant 'T' character,
9161 ** * Number of columns in said table (a varint),
9162 ** * An array of nCol bytes (sPK),
9163 ** * A nul-terminated table name.
9164 */
9165 int nByte;
9166 int nVar;
9167 pInput->iNext++;
9168 if( (rc = sessionChangesetBufferTblhdr(pInput, &nByte)) ){
9169 goto finished_invert;
9170 }
9171 nVar = sessionVarintGet(&pInput->aData[pInput->iNext], &nCol);
9172 sPK.nBuf = 0;
9173 sessionAppendBlob(&sPK, &pInput->aData[pInput->iNext+nVar], nCol, &rc);
9174 sessionAppendByte(&sOut, eType, &rc);
9175 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
9176 if( rc ) goto finished_invert;
9177
9178 pInput->iNext += nByte;
9179 sqlite3_free(apVal);
9180 apVal = 0;
9181 abPK = sPK.aBuf;
9182 break;
9183 }
9184
9185 case SQLITE_INSERT:
9186 case SQLITE_DELETE: {
9187 int nByte;
9188 int bIndirect = pInput->aData[pInput->iNext+1];
9189 int eType2 = (eType==SQLITE_DELETE ? SQLITE_INSERT : SQLITE_DELETE);
9190 pInput->iNext += 2;
9191 assert( rc==SQLITE_OK );
9192 rc = sessionChangesetBufferRecord(pInput, nCol, &nByte);
9193 sessionAppendByte(&sOut, eType2, &rc);
9194 sessionAppendByte(&sOut, bIndirect, &rc);
9195 sessionAppendBlob(&sOut, &pInput->aData[pInput->iNext], nByte, &rc);
9196 pInput->iNext += nByte;
9197 if( rc ) goto finished_invert;
9198 break;
9199 }
9200
9201 case SQLITE_UPDATE: {
9202 int iCol;
9203
9204 if( 0==apVal ){
9205 apVal = (sqlite3_value **)sqlite3_malloc(sizeof(apVal[0])*nCol*2);
9206 if( 0==apVal ){
9207 rc = SQLITE_NOMEM;
9208 goto finished_invert;
9209 }
9210 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
9211 }
9212
9213 /* Write the header for the new UPDATE change. Same as the original. */
9214 sessionAppendByte(&sOut, eType, &rc);
9215 sessionAppendByte(&sOut, pInput->aData[pInput->iNext+1], &rc);
9216
9217 /* Read the old.* and new.* records for the update change. */
9218 pInput->iNext += 2;
9219 rc = sessionReadRecord(pInput, nCol, 0, &apVal[0]);
9220 if( rc==SQLITE_OK ){
9221 rc = sessionReadRecord(pInput, nCol, 0, &apVal[nCol]);
9222 }
9223
9224 /* Write the new old.* record. Consists of the PK columns from the
9225 ** original old.* record, and the other values from the original
9226 ** new.* record. */
9227 for(iCol=0; iCol<nCol; iCol++){
9228 sqlite3_value *pVal = apVal[iCol + (abPK[iCol] ? 0 : nCol)];
9229 sessionAppendValue(&sOut, pVal, &rc);
9230 }
9231
9232 /* Write the new new.* record. Consists of a copy of all values
9233 ** from the original old.* record, except for the PK columns, which
9234 ** are set to "undefined". */
9235 for(iCol=0; iCol<nCol; iCol++){
9236 sqlite3_value *pVal = (abPK[iCol] ? 0 : apVal[iCol]);
9237 sessionAppendValue(&sOut, pVal, &rc);
9238 }
9239
9240 for(iCol=0; iCol<nCol*2; iCol++){
9241 sqlite3ValueFree(apVal[iCol]);
9242 }
9243 memset(apVal, 0, sizeof(apVal[0])*nCol*2);
9244 if( rc!=SQLITE_OK ){
9245 goto finished_invert;
9246 }
9247
9248 break;
9249 }
9250
9251 default:
9252 rc = SQLITE_CORRUPT_BKPT;
9253 goto finished_invert;
9254 }
9255
9256 assert( rc==SQLITE_OK );
9257 if( xOutput && sOut.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
9258 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
9259 sOut.nBuf = 0;
9260 if( rc!=SQLITE_OK ) goto finished_invert;
9261 }
9262 }
9263
9264 assert( rc==SQLITE_OK );
9265 if( pnInverted ){
9266 *pnInverted = sOut.nBuf;
9267 *ppInverted = sOut.aBuf;
9268 sOut.aBuf = 0;
9269 }else if( sOut.nBuf>0 ){
9270 rc = xOutput(pOut, sOut.aBuf, sOut.nBuf);
9271 }
9272
9273 finished_invert:
9274 sqlite3_free(sOut.aBuf);
9275 sqlite3_free(apVal);
9276 sqlite3_free(sPK.aBuf);
9277 return rc;
9278 }
9279
9280
9281 /*
9282 ** Invert a changeset object.
9283 */
9284 SQLITE_API int sqlite3changeset_invert(
9285 int nChangeset, /* Number of bytes in input */
9286 const void *pChangeset, /* Input changeset */
9287 int *pnInverted, /* OUT: Number of bytes in output changeset */
9288 void **ppInverted /* OUT: Inverse of pChangeset */
9289 ){
9290 SessionInput sInput;
9291
9292 /* Set up the input stream */
9293 memset(&sInput, 0, sizeof(SessionInput));
9294 sInput.nData = nChangeset;
9295 sInput.aData = (u8*)pChangeset;
9296
9297 return sessionChangesetInvert(&sInput, 0, 0, pnInverted, ppInverted);
9298 }
9299
9300 /*
9301 ** Streaming version of sqlite3changeset_invert().
9302 */
9303 SQLITE_API int sqlite3changeset_invert_strm(
9304 int (*xInput)(void *pIn, void *pData, int *pnData),
9305 void *pIn,
9306 int (*xOutput)(void *pOut, const void *pData, int nData),
9307 void *pOut
9308 ){
9309 SessionInput sInput;
9310 int rc;
9311
9312 /* Set up the input stream */
9313 memset(&sInput, 0, sizeof(SessionInput));
9314 sInput.xInput = xInput;
9315 sInput.pIn = pIn;
9316
9317 rc = sessionChangesetInvert(&sInput, xOutput, pOut, 0, 0);
9318 sqlite3_free(sInput.buf.aBuf);
9319 return rc;
9320 }
9321
9322 typedef struct SessionApplyCtx SessionApplyCtx;
9323 struct SessionApplyCtx {
9324 sqlite3 *db;
9325 sqlite3_stmt *pDelete; /* DELETE statement */
9326 sqlite3_stmt *pUpdate; /* UPDATE statement */
9327 sqlite3_stmt *pInsert; /* INSERT statement */
9328 sqlite3_stmt *pSelect; /* SELECT statement */
9329 int nCol; /* Size of azCol[] and abPK[] arrays */
9330 const char **azCol; /* Array of column names */
9331 u8 *abPK; /* Boolean array - true if column is in PK */
9332
9333 int bDeferConstraints; /* True to defer constraints */
9334 SessionBuffer constraints; /* Deferred constraints are stored here */
9335 };
9336
9337 /*
9338 ** Formulate a statement to DELETE a row from database db. Assuming a table
9339 ** structure like this:
9340 **
9341 ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
9342 **
9343 ** The DELETE statement looks like this:
9344 **
9345 ** DELETE FROM x WHERE a = :1 AND c = :3 AND (:5 OR b IS :2 AND d IS :4)
9346 **
9347 ** Variable :5 (nCol+1) is a boolean. It should be set to 0 if we require
9348 ** matching b and d values, or 1 otherwise. The second case comes up if the
9349 ** conflict handler is invoked with NOTFOUND and returns CHANGESET_REPLACE.
9350 **
9351 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pDelete is left
9352 ** pointing to the prepared version of the SQL statement.
9353 */
9354 static int sessionDeleteRow(
9355 sqlite3 *db, /* Database handle */
9356 const char *zTab, /* Table name */
9357 SessionApplyCtx *p /* Session changeset-apply context */
9358 ){
9359 int i;
9360 const char *zSep = "";
9361 int rc = SQLITE_OK;
9362 SessionBuffer buf = {0, 0, 0};
9363 int nPk = 0;
9364
9365 sessionAppendStr(&buf, "DELETE FROM ", &rc);
9366 sessionAppendIdent(&buf, zTab, &rc);
9367 sessionAppendStr(&buf, " WHERE ", &rc);
9368
9369 for(i=0; i<p->nCol; i++){
9370 if( p->abPK[i] ){
9371 nPk++;
9372 sessionAppendStr(&buf, zSep, &rc);
9373 sessionAppendIdent(&buf, p->azCol[i], &rc);
9374 sessionAppendStr(&buf, " = ?", &rc);
9375 sessionAppendInteger(&buf, i+1, &rc);
9376 zSep = " AND ";
9377 }
9378 }
9379
9380 if( nPk<p->nCol ){
9381 sessionAppendStr(&buf, " AND (?", &rc);
9382 sessionAppendInteger(&buf, p->nCol+1, &rc);
9383 sessionAppendStr(&buf, " OR ", &rc);
9384
9385 zSep = "";
9386 for(i=0; i<p->nCol; i++){
9387 if( !p->abPK[i] ){
9388 sessionAppendStr(&buf, zSep, &rc);
9389 sessionAppendIdent(&buf, p->azCol[i], &rc);
9390 sessionAppendStr(&buf, " IS ?", &rc);
9391 sessionAppendInteger(&buf, i+1, &rc);
9392 zSep = "AND ";
9393 }
9394 }
9395 sessionAppendStr(&buf, ")", &rc);
9396 }
9397
9398 if( rc==SQLITE_OK ){
9399 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pDelete, 0);
9400 }
9401 sqlite3_free(buf.aBuf);
9402
9403 return rc;
9404 }
9405
9406 /*
9407 ** Formulate and prepare a statement to UPDATE a row from database db.
9408 ** Assuming a table structure like this:
9409 **
9410 ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
9411 **
9412 ** The UPDATE statement looks like this:
9413 **
9414 ** UPDATE x SET
9415 ** a = CASE WHEN ?2 THEN ?3 ELSE a END,
9416 ** b = CASE WHEN ?5 THEN ?6 ELSE b END,
9417 ** c = CASE WHEN ?8 THEN ?9 ELSE c END,
9418 ** d = CASE WHEN ?11 THEN ?12 ELSE d END
9419 ** WHERE a = ?1 AND c = ?7 AND (?13 OR
9420 ** (?5==0 OR b IS ?4) AND (?11==0 OR d IS ?10) AND
9421 ** )
9422 **
9423 ** For each column in the table, there are three variables to bind:
9424 **
9425 ** ?(i*3+1) The old.* value of the column, if any.
9426 ** ?(i*3+2) A boolean flag indicating that the value is being modified.
9427 ** ?(i*3+3) The new.* value of the column, if any.
9428 **
9429 ** Also, a boolean flag that, if set to true, causes the statement to update
9430 ** a row even if the non-PK values do not match. This is required if the
9431 ** conflict-handler is invoked with CHANGESET_DATA and returns
9432 ** CHANGESET_REPLACE. This is variable "?(nCol*3+1)".
9433 **
9434 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pUpdate is left
9435 ** pointing to the prepared version of the SQL statement.
9436 */
9437 static int sessionUpdateRow(
9438 sqlite3 *db, /* Database handle */
9439 const char *zTab, /* Table name */
9440 SessionApplyCtx *p /* Session changeset-apply context */
9441 ){
9442 int rc = SQLITE_OK;
9443 int i;
9444 const char *zSep = "";
9445 SessionBuffer buf = {0, 0, 0};
9446
9447 /* Append "UPDATE tbl SET " */
9448 sessionAppendStr(&buf, "UPDATE ", &rc);
9449 sessionAppendIdent(&buf, zTab, &rc);
9450 sessionAppendStr(&buf, " SET ", &rc);
9451
9452 /* Append the assignments */
9453 for(i=0; i<p->nCol; i++){
9454 sessionAppendStr(&buf, zSep, &rc);
9455 sessionAppendIdent(&buf, p->azCol[i], &rc);
9456 sessionAppendStr(&buf, " = CASE WHEN ?", &rc);
9457 sessionAppendInteger(&buf, i*3+2, &rc);
9458 sessionAppendStr(&buf, " THEN ?", &rc);
9459 sessionAppendInteger(&buf, i*3+3, &rc);
9460 sessionAppendStr(&buf, " ELSE ", &rc);
9461 sessionAppendIdent(&buf, p->azCol[i], &rc);
9462 sessionAppendStr(&buf, " END", &rc);
9463 zSep = ", ";
9464 }
9465
9466 /* Append the PK part of the WHERE clause */
9467 sessionAppendStr(&buf, " WHERE ", &rc);
9468 for(i=0; i<p->nCol; i++){
9469 if( p->abPK[i] ){
9470 sessionAppendIdent(&buf, p->azCol[i], &rc);
9471 sessionAppendStr(&buf, " = ?", &rc);
9472 sessionAppendInteger(&buf, i*3+1, &rc);
9473 sessionAppendStr(&buf, " AND ", &rc);
9474 }
9475 }
9476
9477 /* Append the non-PK part of the WHERE clause */
9478 sessionAppendStr(&buf, " (?", &rc);
9479 sessionAppendInteger(&buf, p->nCol*3+1, &rc);
9480 sessionAppendStr(&buf, " OR 1", &rc);
9481 for(i=0; i<p->nCol; i++){
9482 if( !p->abPK[i] ){
9483 sessionAppendStr(&buf, " AND (?", &rc);
9484 sessionAppendInteger(&buf, i*3+2, &rc);
9485 sessionAppendStr(&buf, "=0 OR ", &rc);
9486 sessionAppendIdent(&buf, p->azCol[i], &rc);
9487 sessionAppendStr(&buf, " IS ?", &rc);
9488 sessionAppendInteger(&buf, i*3+1, &rc);
9489 sessionAppendStr(&buf, ")", &rc);
9490 }
9491 }
9492 sessionAppendStr(&buf, ")", &rc);
9493
9494 if( rc==SQLITE_OK ){
9495 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pUpdate, 0);
9496 }
9497 sqlite3_free(buf.aBuf);
9498
9499 return rc;
9500 }
9501
9502 /*
9503 ** Formulate and prepare an SQL statement to query table zTab by primary
9504 ** key. Assuming the following table structure:
9505 **
9506 ** CREATE TABLE x(a, b, c, d, PRIMARY KEY(a, c));
9507 **
9508 ** The SELECT statement looks like this:
9509 **
9510 ** SELECT * FROM x WHERE a = ?1 AND c = ?3
9511 **
9512 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pSelect is left
9513 ** pointing to the prepared version of the SQL statement.
9514 */
9515 static int sessionSelectRow(
9516 sqlite3 *db, /* Database handle */
9517 const char *zTab, /* Table name */
9518 SessionApplyCtx *p /* Session changeset-apply context */
9519 ){
9520 return sessionSelectStmt(
9521 db, "main", zTab, p->nCol, p->azCol, p->abPK, &p->pSelect);
9522 }
9523
9524 /*
9525 ** Formulate and prepare an INSERT statement to add a record to table zTab.
9526 ** For example:
9527 **
9528 ** INSERT INTO main."zTab" VALUES(?1, ?2, ?3 ...);
9529 **
9530 ** If successful, SQLITE_OK is returned and SessionApplyCtx.pInsert is left
9531 ** pointing to the prepared version of the SQL statement.
9532 */
9533 static int sessionInsertRow(
9534 sqlite3 *db, /* Database handle */
9535 const char *zTab, /* Table name */
9536 SessionApplyCtx *p /* Session changeset-apply context */
9537 ){
9538 int rc = SQLITE_OK;
9539 int i;
9540 SessionBuffer buf = {0, 0, 0};
9541
9542 sessionAppendStr(&buf, "INSERT INTO main.", &rc);
9543 sessionAppendIdent(&buf, zTab, &rc);
9544 sessionAppendStr(&buf, "(", &rc);
9545 for(i=0; i<p->nCol; i++){
9546 if( i!=0 ) sessionAppendStr(&buf, ", ", &rc);
9547 sessionAppendIdent(&buf, p->azCol[i], &rc);
9548 }
9549
9550 sessionAppendStr(&buf, ") VALUES(?", &rc);
9551 for(i=1; i<p->nCol; i++){
9552 sessionAppendStr(&buf, ", ?", &rc);
9553 }
9554 sessionAppendStr(&buf, ")", &rc);
9555
9556 if( rc==SQLITE_OK ){
9557 rc = sqlite3_prepare_v2(db, (char *)buf.aBuf, buf.nBuf, &p->pInsert, 0);
9558 }
9559 sqlite3_free(buf.aBuf);
9560 return rc;
9561 }
9562
9563 /*
9564 ** A wrapper around sqlite3_bind_value() that detects an extra problem.
9565 ** See comments in the body of this function for details.
9566 */
9567 static int sessionBindValue(
9568 sqlite3_stmt *pStmt, /* Statement to bind value to */
9569 int i, /* Parameter number to bind to */
9570 sqlite3_value *pVal /* Value to bind */
9571 ){
9572 int eType = sqlite3_value_type(pVal);
9573 /* COVERAGE: The (pVal->z==0) branch is never true using current versions
9574 ** of SQLite. If a malloc fails in an sqlite3_value_xxx() function, either
9575 ** the (pVal->z) variable remains as it was or the type of the value is
9576 ** set to SQLITE_NULL. */
9577 if( (eType==SQLITE_TEXT || eType==SQLITE_BLOB) && pVal->z==0 ){
9578 /* This condition occurs when an earlier OOM in a call to
9579 ** sqlite3_value_text() or sqlite3_value_blob() (perhaps from within
9580 ** a conflict-handler) has zeroed the pVal->z pointer. Return NOMEM. */
9581 return SQLITE_NOMEM;
9582 }
9583 return sqlite3_bind_value(pStmt, i, pVal);
9584 }
9585
9586 /*
9587 ** Iterator pIter must point to an SQLITE_INSERT entry. This function
9588 ** transfers new.* values from the current iterator entry to statement
9589 ** pStmt. The table being inserted into has nCol columns.
9590 **
9591 ** New.* value $i from the iterator is bound to variable ($i+1) of
9592 ** statement pStmt. If parameter abPK is NULL, all values from 0 to (nCol-1)
9593 ** are transfered to the statement. Otherwise, if abPK is not NULL, it points
9594 ** to an array nCol elements in size. In this case only those values for
9595 ** which abPK[$i] is true are read from the iterator and bound to the
9596 ** statement.
9597 **
9598 ** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
9599 */
9600 static int sessionBindRow(
9601 sqlite3_changeset_iter *pIter, /* Iterator to read values from */
9602 int(*xValue)(sqlite3_changeset_iter *, int, sqlite3_value **),
9603 int nCol, /* Number of columns */
9604 u8 *abPK, /* If not NULL, bind only if true */
9605 sqlite3_stmt *pStmt /* Bind values to this statement */
9606 ){
9607 int i;
9608 int rc = SQLITE_OK;
9609
9610 /* Neither sqlite3changeset_old or sqlite3changeset_new can fail if the
9611 ** argument iterator points to a suitable entry. Make sure that xValue
9612 ** is one of these to guarantee that it is safe to ignore the return
9613 ** in the code below. */
9614 assert( xValue==sqlite3changeset_old || xValue==sqlite3changeset_new );
9615
9616 for(i=0; rc==SQLITE_OK && i<nCol; i++){
9617 if( !abPK || abPK[i] ){
9618 sqlite3_value *pVal;
9619 (void)xValue(pIter, i, &pVal);
9620 rc = sessionBindValue(pStmt, i+1, pVal);
9621 }
9622 }
9623 return rc;
9624 }
9625
9626 /*
9627 ** SQL statement pSelect is as generated by the sessionSelectRow() function.
9628 ** This function binds the primary key values from the change that changeset
9629 ** iterator pIter points to to the SELECT and attempts to seek to the table
9630 ** entry. If a row is found, the SELECT statement left pointing at the row
9631 ** and SQLITE_ROW is returned. Otherwise, if no row is found and no error
9632 ** has occured, the statement is reset and SQLITE_OK is returned. If an
9633 ** error occurs, the statement is reset and an SQLite error code is returned.
9634 **
9635 ** If this function returns SQLITE_ROW, the caller must eventually reset()
9636 ** statement pSelect. If any other value is returned, the statement does
9637 ** not require a reset().
9638 **
9639 ** If the iterator currently points to an INSERT record, bind values from the
9640 ** new.* record to the SELECT statement. Or, if it points to a DELETE or
9641 ** UPDATE, bind values from the old.* record.
9642 */
9643 static int sessionSeekToRow(
9644 sqlite3 *db, /* Database handle */
9645 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9646 u8 *abPK, /* Primary key flags array */
9647 sqlite3_stmt *pSelect /* SELECT statement from sessionSelectRow() */
9648 ){
9649 int rc; /* Return code */
9650 int nCol; /* Number of columns in table */
9651 int op; /* Changset operation (SQLITE_UPDATE etc.) */
9652 const char *zDummy; /* Unused */
9653
9654 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
9655 rc = sessionBindRow(pIter,
9656 op==SQLITE_INSERT ? sqlite3changeset_new : sqlite3changeset_old,
9657 nCol, abPK, pSelect
9658 );
9659
9660 if( rc==SQLITE_OK ){
9661 rc = sqlite3_step(pSelect);
9662 if( rc!=SQLITE_ROW ) rc = sqlite3_reset(pSelect);
9663 }
9664
9665 return rc;
9666 }
9667
9668 /*
9669 ** Invoke the conflict handler for the change that the changeset iterator
9670 ** currently points to.
9671 **
9672 ** Argument eType must be either CHANGESET_DATA or CHANGESET_CONFLICT.
9673 ** If argument pbReplace is NULL, then the type of conflict handler invoked
9674 ** depends solely on eType, as follows:
9675 **
9676 ** eType value Value passed to xConflict
9677 ** -------------------------------------------------
9678 ** CHANGESET_DATA CHANGESET_NOTFOUND
9679 ** CHANGESET_CONFLICT CHANGESET_CONSTRAINT
9680 **
9681 ** Or, if pbReplace is not NULL, then an attempt is made to find an existing
9682 ** record with the same primary key as the record about to be deleted, updated
9683 ** or inserted. If such a record can be found, it is available to the conflict
9684 ** handler as the "conflicting" record. In this case the type of conflict
9685 ** handler invoked is as follows:
9686 **
9687 ** eType value PK Record found? Value passed to xConflict
9688 ** ----------------------------------------------------------------
9689 ** CHANGESET_DATA Yes CHANGESET_DATA
9690 ** CHANGESET_DATA No CHANGESET_NOTFOUND
9691 ** CHANGESET_CONFLICT Yes CHANGESET_CONFLICT
9692 ** CHANGESET_CONFLICT No CHANGESET_CONSTRAINT
9693 **
9694 ** If pbReplace is not NULL, and a record with a matching PK is found, and
9695 ** the conflict handler function returns SQLITE_CHANGESET_REPLACE, *pbReplace
9696 ** is set to non-zero before returning SQLITE_OK.
9697 **
9698 ** If the conflict handler returns SQLITE_CHANGESET_ABORT, SQLITE_ABORT is
9699 ** returned. Or, if the conflict handler returns an invalid value,
9700 ** SQLITE_MISUSE. If the conflict handler returns SQLITE_CHANGESET_OMIT,
9701 ** this function returns SQLITE_OK.
9702 */
9703 static int sessionConflictHandler(
9704 int eType, /* Either CHANGESET_DATA or CONFLICT */
9705 SessionApplyCtx *p, /* changeset_apply() context */
9706 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9707 int(*xConflict)(void *, int, sqlite3_changeset_iter*),
9708 void *pCtx, /* First argument for conflict handler */
9709 int *pbReplace /* OUT: Set to true if PK row is found */
9710 ){
9711 int res = 0; /* Value returned by conflict handler */
9712 int rc;
9713 int nCol;
9714 int op;
9715 const char *zDummy;
9716
9717 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
9718
9719 assert( eType==SQLITE_CHANGESET_CONFLICT || eType==SQLITE_CHANGESET_DATA );
9720 assert( SQLITE_CHANGESET_CONFLICT+1==SQLITE_CHANGESET_CONSTRAINT );
9721 assert( SQLITE_CHANGESET_DATA+1==SQLITE_CHANGESET_NOTFOUND );
9722
9723 /* Bind the new.* PRIMARY KEY values to the SELECT statement. */
9724 if( pbReplace ){
9725 rc = sessionSeekToRow(p->db, pIter, p->abPK, p->pSelect);
9726 }else{
9727 rc = SQLITE_OK;
9728 }
9729
9730 if( rc==SQLITE_ROW ){
9731 /* There exists another row with the new.* primary key. */
9732 pIter->pConflict = p->pSelect;
9733 res = xConflict(pCtx, eType, pIter);
9734 pIter->pConflict = 0;
9735 rc = sqlite3_reset(p->pSelect);
9736 }else if( rc==SQLITE_OK ){
9737 if( p->bDeferConstraints && eType==SQLITE_CHANGESET_CONFLICT ){
9738 /* Instead of invoking the conflict handler, append the change blob
9739 ** to the SessionApplyCtx.constraints buffer. */
9740 u8 *aBlob = &pIter->in.aData[pIter->in.iCurrent];
9741 int nBlob = pIter->in.iNext - pIter->in.iCurrent;
9742 sessionAppendBlob(&p->constraints, aBlob, nBlob, &rc);
9743 res = SQLITE_CHANGESET_OMIT;
9744 }else{
9745 /* No other row with the new.* primary key. */
9746 res = xConflict(pCtx, eType+1, pIter);
9747 if( res==SQLITE_CHANGESET_REPLACE ) rc = SQLITE_MISUSE;
9748 }
9749 }
9750
9751 if( rc==SQLITE_OK ){
9752 switch( res ){
9753 case SQLITE_CHANGESET_REPLACE:
9754 assert( pbReplace );
9755 *pbReplace = 1;
9756 break;
9757
9758 case SQLITE_CHANGESET_OMIT:
9759 break;
9760
9761 case SQLITE_CHANGESET_ABORT:
9762 rc = SQLITE_ABORT;
9763 break;
9764
9765 default:
9766 rc = SQLITE_MISUSE;
9767 break;
9768 }
9769 }
9770
9771 return rc;
9772 }
9773
9774 /*
9775 ** Attempt to apply the change that the iterator passed as the first argument
9776 ** currently points to to the database. If a conflict is encountered, invoke
9777 ** the conflict handler callback.
9778 **
9779 ** If argument pbRetry is NULL, then ignore any CHANGESET_DATA conflict. If
9780 ** one is encountered, update or delete the row with the matching primary key
9781 ** instead. Or, if pbRetry is not NULL and a CHANGESET_DATA conflict occurs,
9782 ** invoke the conflict handler. If it returns CHANGESET_REPLACE, set *pbRetry
9783 ** to true before returning. In this case the caller will invoke this function
9784 ** again, this time with pbRetry set to NULL.
9785 **
9786 ** If argument pbReplace is NULL and a CHANGESET_CONFLICT conflict is
9787 ** encountered invoke the conflict handler with CHANGESET_CONSTRAINT instead.
9788 ** Or, if pbReplace is not NULL, invoke it with CHANGESET_CONFLICT. If such
9789 ** an invocation returns SQLITE_CHANGESET_REPLACE, set *pbReplace to true
9790 ** before retrying. In this case the caller attempts to remove the conflicting
9791 ** row before invoking this function again, this time with pbReplace set
9792 ** to NULL.
9793 **
9794 ** If any conflict handler returns SQLITE_CHANGESET_ABORT, this function
9795 ** returns SQLITE_ABORT. Otherwise, if no error occurs, SQLITE_OK is
9796 ** returned.
9797 */
9798 static int sessionApplyOneOp(
9799 sqlite3_changeset_iter *pIter, /* Changeset iterator */
9800 SessionApplyCtx *p, /* changeset_apply() context */
9801 int(*xConflict)(void *, int, sqlite3_changeset_iter *),
9802 void *pCtx, /* First argument for the conflict handler */
9803 int *pbReplace, /* OUT: True to remove PK row and retry */
9804 int *pbRetry /* OUT: True to retry. */
9805 ){
9806 const char *zDummy;
9807 int op;
9808 int nCol;
9809 int rc = SQLITE_OK;
9810
9811 assert( p->pDelete && p->pUpdate && p->pInsert && p->pSelect );
9812 assert( p->azCol && p->abPK );
9813 assert( !pbReplace || *pbReplace==0 );
9814
9815 sqlite3changeset_op(pIter, &zDummy, &nCol, &op, 0);
9816
9817 if( op==SQLITE_DELETE ){
9818
9819 /* Bind values to the DELETE statement. If conflict handling is required,
9820 ** bind values for all columns and set bound variable (nCol+1) to true.
9821 ** Or, if conflict handling is not required, bind just the PK column
9822 ** values and, if it exists, set (nCol+1) to false. Conflict handling
9823 ** is not required if:
9824 **
9825 ** * this is a patchset, or
9826 ** * (pbRetry==0), or
9827 ** * all columns of the table are PK columns (in this case there is
9828 ** no (nCol+1) variable to bind to).
9829 */
9830 u8 *abPK = (pIter->bPatchset ? p->abPK : 0);
9831 rc = sessionBindRow(pIter, sqlite3changeset_old, nCol, abPK, p->pDelete);
9832 if( rc==SQLITE_OK && sqlite3_bind_parameter_count(p->pDelete)>nCol ){
9833 rc = sqlite3_bind_int(p->pDelete, nCol+1, (pbRetry==0 || abPK));
9834 }
9835 if( rc!=SQLITE_OK ) return rc;
9836
9837 sqlite3_step(p->pDelete);
9838 rc = sqlite3_reset(p->pDelete);
9839 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
9840 rc = sessionConflictHandler(
9841 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
9842 );
9843 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
9844 rc = sessionConflictHandler(
9845 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
9846 );
9847 }
9848
9849 }else if( op==SQLITE_UPDATE ){
9850 int i;
9851
9852 /* Bind values to the UPDATE statement. */
9853 for(i=0; rc==SQLITE_OK && i<nCol; i++){
9854 sqlite3_value *pOld = sessionChangesetOld(pIter, i);
9855 sqlite3_value *pNew = sessionChangesetNew(pIter, i);
9856
9857 sqlite3_bind_int(p->pUpdate, i*3+2, !!pNew);
9858 if( pOld ){
9859 rc = sessionBindValue(p->pUpdate, i*3+1, pOld);
9860 }
9861 if( rc==SQLITE_OK && pNew ){
9862 rc = sessionBindValue(p->pUpdate, i*3+3, pNew);
9863 }
9864 }
9865 if( rc==SQLITE_OK ){
9866 sqlite3_bind_int(p->pUpdate, nCol*3+1, pbRetry==0 || pIter->bPatchset);
9867 }
9868 if( rc!=SQLITE_OK ) return rc;
9869
9870 /* Attempt the UPDATE. In the case of a NOTFOUND or DATA conflict,
9871 ** the result will be SQLITE_OK with 0 rows modified. */
9872 sqlite3_step(p->pUpdate);
9873 rc = sqlite3_reset(p->pUpdate);
9874
9875 if( rc==SQLITE_OK && sqlite3_changes(p->db)==0 ){
9876 /* A NOTFOUND or DATA error. Search the table to see if it contains
9877 ** a row with a matching primary key. If so, this is a DATA conflict.
9878 ** Otherwise, if there is no primary key match, it is a NOTFOUND. */
9879
9880 rc = sessionConflictHandler(
9881 SQLITE_CHANGESET_DATA, p, pIter, xConflict, pCtx, pbRetry
9882 );
9883
9884 }else if( (rc&0xff)==SQLITE_CONSTRAINT ){
9885 /* This is always a CONSTRAINT conflict. */
9886 rc = sessionConflictHandler(
9887 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, 0
9888 );
9889 }
9890
9891 }else{
9892 assert( op==SQLITE_INSERT );
9893 rc = sessionBindRow(pIter, sqlite3changeset_new, nCol, 0, p->pInsert);
9894 if( rc!=SQLITE_OK ) return rc;
9895
9896 sqlite3_step(p->pInsert);
9897 rc = sqlite3_reset(p->pInsert);
9898 if( (rc&0xff)==SQLITE_CONSTRAINT ){
9899 rc = sessionConflictHandler(
9900 SQLITE_CHANGESET_CONFLICT, p, pIter, xConflict, pCtx, pbReplace
9901 );
9902 }
9903 }
9904
9905 return rc;
9906 }
9907
9908 /*
9909 ** Attempt to apply the change that the iterator passed as the first argument
9910 ** currently points to to the database. If a conflict is encountered, invoke
9911 ** the conflict handler callback.
9912 **
9913 ** The difference between this function and sessionApplyOne() is that this
9914 ** function handles the case where the conflict-handler is invoked and
9915 ** returns SQLITE_CHANGESET_REPLACE - indicating that the change should be
9916 ** retried in some manner.
9917 */
9918 static int sessionApplyOneWithRetry(
9919 sqlite3 *db, /* Apply change to "main" db of this handle */
9920 sqlite3_changeset_iter *pIter, /* Changeset iterator to read change from */
9921 SessionApplyCtx *pApply, /* Apply context */
9922 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
9923 void *pCtx /* First argument passed to xConflict */
9924 ){
9925 int bReplace = 0;
9926 int bRetry = 0;
9927 int rc;
9928
9929 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, &bReplace, &bRetry);
9930 assert( rc==SQLITE_OK || (bRetry==0 && bReplace==0) );
9931
9932 /* If the bRetry flag is set, the change has not been applied due to an
9933 ** SQLITE_CHANGESET_DATA problem (i.e. this is an UPDATE or DELETE and
9934 ** a row with the correct PK is present in the db, but one or more other
9935 ** fields do not contain the expected values) and the conflict handler
9936 ** returned SQLITE_CHANGESET_REPLACE. In this case retry the operation,
9937 ** but pass NULL as the final argument so that sessionApplyOneOp() ignores
9938 ** the SQLITE_CHANGESET_DATA problem. */
9939 if( bRetry ){
9940 assert( pIter->op==SQLITE_UPDATE || pIter->op==SQLITE_DELETE );
9941 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
9942 }
9943
9944 /* If the bReplace flag is set, the change is an INSERT that has not
9945 ** been performed because the database already contains a row with the
9946 ** specified primary key and the conflict handler returned
9947 ** SQLITE_CHANGESET_REPLACE. In this case remove the conflicting row
9948 ** before reattempting the INSERT. */
9949 else if( bReplace ){
9950 assert( pIter->op==SQLITE_INSERT );
9951 rc = sqlite3_exec(db, "SAVEPOINT replace_op", 0, 0, 0);
9952 if( rc==SQLITE_OK ){
9953 rc = sessionBindRow(pIter,
9954 sqlite3changeset_new, pApply->nCol, pApply->abPK, pApply->pDelete);
9955 sqlite3_bind_int(pApply->pDelete, pApply->nCol+1, 1);
9956 }
9957 if( rc==SQLITE_OK ){
9958 sqlite3_step(pApply->pDelete);
9959 rc = sqlite3_reset(pApply->pDelete);
9960 }
9961 if( rc==SQLITE_OK ){
9962 rc = sessionApplyOneOp(pIter, pApply, xConflict, pCtx, 0, 0);
9963 }
9964 if( rc==SQLITE_OK ){
9965 rc = sqlite3_exec(db, "RELEASE replace_op", 0, 0, 0);
9966 }
9967 }
9968
9969 return rc;
9970 }
9971
9972 /*
9973 ** Retry the changes accumulated in the pApply->constraints buffer.
9974 */
9975 static int sessionRetryConstraints(
9976 sqlite3 *db,
9977 int bPatchset,
9978 const char *zTab,
9979 SessionApplyCtx *pApply,
9980 int(*xConflict)(void*, int, sqlite3_changeset_iter*),
9981 void *pCtx /* First argument passed to xConflict */
9982 ){
9983 int rc = SQLITE_OK;
9984
9985 while( pApply->constraints.nBuf ){
9986 sqlite3_changeset_iter *pIter2 = 0;
9987 SessionBuffer cons = pApply->constraints;
9988 memset(&pApply->constraints, 0, sizeof(SessionBuffer));
9989
9990 rc = sessionChangesetStart(&pIter2, 0, 0, cons.nBuf, cons.aBuf);
9991 if( rc==SQLITE_OK ){
9992 int nByte = 2*pApply->nCol*sizeof(sqlite3_value*);
9993 int rc2;
9994 pIter2->bPatchset = bPatchset;
9995 pIter2->zTab = (char*)zTab;
9996 pIter2->nCol = pApply->nCol;
9997 pIter2->abPK = pApply->abPK;
9998 sessionBufferGrow(&pIter2->tblhdr, nByte, &rc);
9999 pIter2->apValue = (sqlite3_value**)pIter2->tblhdr.aBuf;
10000 if( rc==SQLITE_OK ) memset(pIter2->apValue, 0, nByte);
10001
10002 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter2) ){
10003 rc = sessionApplyOneWithRetry(db, pIter2, pApply, xConflict, pCtx);
10004 }
10005
10006 rc2 = sqlite3changeset_finalize(pIter2);
10007 if( rc==SQLITE_OK ) rc = rc2;
10008 }
10009 assert( pApply->bDeferConstraints || pApply->constraints.nBuf==0 );
10010
10011 sqlite3_free(cons.aBuf);
10012 if( rc!=SQLITE_OK ) break;
10013 if( pApply->constraints.nBuf>=cons.nBuf ){
10014 /* No progress was made on the last round. */
10015 pApply->bDeferConstraints = 0;
10016 }
10017 }
10018
10019 return rc;
10020 }
10021
10022 /*
10023 ** Argument pIter is a changeset iterator that has been initialized, but
10024 ** not yet passed to sqlite3changeset_next(). This function applies the
10025 ** changeset to the main database attached to handle "db". The supplied
10026 ** conflict handler callback is invoked to resolve any conflicts encountered
10027 ** while applying the change.
10028 */
10029 static int sessionChangesetApply(
10030 sqlite3 *db, /* Apply change to "main" db of this handle */
10031 sqlite3_changeset_iter *pIter, /* Changeset to apply */
10032 int(*xFilter)(
10033 void *pCtx, /* Copy of sixth arg to _apply() */
10034 const char *zTab /* Table name */
10035 ),
10036 int(*xConflict)(
10037 void *pCtx, /* Copy of fifth arg to _apply() */
10038 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10039 sqlite3_changeset_iter *p /* Handle describing change and conflict */
10040 ),
10041 void *pCtx /* First argument passed to xConflict */
10042 ){
10043 int schemaMismatch = 0;
10044 int rc; /* Return code */
10045 const char *zTab = 0; /* Name of current table */
10046 int nTab = 0; /* Result of sqlite3Strlen30(zTab) */
10047 SessionApplyCtx sApply; /* changeset_apply() context object */
10048 int bPatchset;
10049
10050 assert( xConflict!=0 );
10051
10052 pIter->in.bNoDiscard = 1;
10053 memset(&sApply, 0, sizeof(sApply));
10054 sqlite3_mutex_enter(sqlite3_db_mutex(db));
10055 rc = sqlite3_exec(db, "SAVEPOINT changeset_apply", 0, 0, 0);
10056 if( rc==SQLITE_OK ){
10057 rc = sqlite3_exec(db, "PRAGMA defer_foreign_keys = 1", 0, 0, 0);
10058 }
10059 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3changeset_next(pIter) ){
10060 int nCol;
10061 int op;
10062 const char *zNew;
10063
10064 sqlite3changeset_op(pIter, &zNew, &nCol, &op, 0);
10065
10066 if( zTab==0 || sqlite3_strnicmp(zNew, zTab, nTab+1) ){
10067 u8 *abPK;
10068
10069 rc = sessionRetryConstraints(
10070 db, pIter->bPatchset, zTab, &sApply, xConflict, pCtx
10071 );
10072 if( rc!=SQLITE_OK ) break;
10073
10074 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
10075 sqlite3_finalize(sApply.pDelete);
10076 sqlite3_finalize(sApply.pUpdate);
10077 sqlite3_finalize(sApply.pInsert);
10078 sqlite3_finalize(sApply.pSelect);
10079 memset(&sApply, 0, sizeof(sApply));
10080 sApply.db = db;
10081 sApply.bDeferConstraints = 1;
10082
10083 /* If an xFilter() callback was specified, invoke it now. If the
10084 ** xFilter callback returns zero, skip this table. If it returns
10085 ** non-zero, proceed. */
10086 schemaMismatch = (xFilter && (0==xFilter(pCtx, zNew)));
10087 if( schemaMismatch ){
10088 zTab = sqlite3_mprintf("%s", zNew);
10089 if( zTab==0 ){
10090 rc = SQLITE_NOMEM;
10091 break;
10092 }
10093 nTab = (int)strlen(zTab);
10094 sApply.azCol = (const char **)zTab;
10095 }else{
10096 int nMinCol = 0;
10097 int i;
10098
10099 sqlite3changeset_pk(pIter, &abPK, 0);
10100 rc = sessionTableInfo(
10101 db, "main", zNew, &sApply.nCol, &zTab, &sApply.azCol, &sApply.abPK
10102 );
10103 if( rc!=SQLITE_OK ) break;
10104 for(i=0; i<sApply.nCol; i++){
10105 if( sApply.abPK[i] ) nMinCol = i+1;
10106 }
10107
10108 if( sApply.nCol==0 ){
10109 schemaMismatch = 1;
10110 sqlite3_log(SQLITE_SCHEMA,
10111 "sqlite3changeset_apply(): no such table: %s", zTab
10112 );
10113 }
10114 else if( sApply.nCol<nCol ){
10115 schemaMismatch = 1;
10116 sqlite3_log(SQLITE_SCHEMA,
10117 "sqlite3changeset_apply(): table %s has %d columns, "
10118 "expected %d or more",
10119 zTab, sApply.nCol, nCol
10120 );
10121 }
10122 else if( nCol<nMinCol || memcmp(sApply.abPK, abPK, nCol)!=0 ){
10123 schemaMismatch = 1;
10124 sqlite3_log(SQLITE_SCHEMA, "sqlite3changeset_apply(): "
10125 "primary key mismatch for table %s", zTab
10126 );
10127 }
10128 else{
10129 sApply.nCol = nCol;
10130 if((rc = sessionSelectRow(db, zTab, &sApply))
10131 || (rc = sessionUpdateRow(db, zTab, &sApply))
10132 || (rc = sessionDeleteRow(db, zTab, &sApply))
10133 || (rc = sessionInsertRow(db, zTab, &sApply))
10134 ){
10135 break;
10136 }
10137 }
10138 nTab = sqlite3Strlen30(zTab);
10139 }
10140 }
10141
10142 /* If there is a schema mismatch on the current table, proceed to the
10143 ** next change. A log message has already been issued. */
10144 if( schemaMismatch ) continue;
10145
10146 rc = sessionApplyOneWithRetry(db, pIter, &sApply, xConflict, pCtx);
10147 }
10148
10149 bPatchset = pIter->bPatchset;
10150 if( rc==SQLITE_OK ){
10151 rc = sqlite3changeset_finalize(pIter);
10152 }else{
10153 sqlite3changeset_finalize(pIter);
10154 }
10155
10156 if( rc==SQLITE_OK ){
10157 rc = sessionRetryConstraints(db, bPatchset, zTab, &sApply, xConflict, pCtx);
10158 }
10159
10160 if( rc==SQLITE_OK ){
10161 int nFk, notUsed;
10162 sqlite3_db_status(db, SQLITE_DBSTATUS_DEFERRED_FKS, &nFk, &notUsed, 0);
10163 if( nFk!=0 ){
10164 int res = SQLITE_CHANGESET_ABORT;
10165 sqlite3_changeset_iter sIter;
10166 memset(&sIter, 0, sizeof(sIter));
10167 sIter.nCol = nFk;
10168 res = xConflict(pCtx, SQLITE_CHANGESET_FOREIGN_KEY, &sIter);
10169 if( res!=SQLITE_CHANGESET_OMIT ){
10170 rc = SQLITE_CONSTRAINT;
10171 }
10172 }
10173 }
10174 sqlite3_exec(db, "PRAGMA defer_foreign_keys = 0", 0, 0, 0);
10175
10176 if( rc==SQLITE_OK ){
10177 rc = sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
10178 }else{
10179 sqlite3_exec(db, "ROLLBACK TO changeset_apply", 0, 0, 0);
10180 sqlite3_exec(db, "RELEASE changeset_apply", 0, 0, 0);
10181 }
10182
10183 sqlite3_finalize(sApply.pInsert);
10184 sqlite3_finalize(sApply.pDelete);
10185 sqlite3_finalize(sApply.pUpdate);
10186 sqlite3_finalize(sApply.pSelect);
10187 sqlite3_free((char*)sApply.azCol); /* cast works around VC++ bug */
10188 sqlite3_free((char*)sApply.constraints.aBuf);
10189 sqlite3_mutex_leave(sqlite3_db_mutex(db));
10190 return rc;
10191 }
10192
10193 /*
10194 ** Apply the changeset passed via pChangeset/nChangeset to the main database
10195 ** attached to handle "db". Invoke the supplied conflict handler callback
10196 ** to resolve any conflicts encountered while applying the change.
10197 */
10198 SQLITE_API int sqlite3changeset_apply(
10199 sqlite3 *db, /* Apply change to "main" db of this handle */
10200 int nChangeset, /* Size of changeset in bytes */
10201 void *pChangeset, /* Changeset blob */
10202 int(*xFilter)(
10203 void *pCtx, /* Copy of sixth arg to _apply() */
10204 const char *zTab /* Table name */
10205 ),
10206 int(*xConflict)(
10207 void *pCtx, /* Copy of fifth arg to _apply() */
10208 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10209 sqlite3_changeset_iter *p /* Handle describing change and conflict */
10210 ),
10211 void *pCtx /* First argument passed to xConflict */
10212 ){
10213 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
10214 int rc = sqlite3changeset_start(&pIter, nChangeset, pChangeset);
10215 if( rc==SQLITE_OK ){
10216 rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
10217 }
10218 return rc;
10219 }
10220
10221 /*
10222 ** Apply the changeset passed via xInput/pIn to the main database
10223 ** attached to handle "db". Invoke the supplied conflict handler callback
10224 ** to resolve any conflicts encountered while applying the change.
10225 */
10226 SQLITE_API int sqlite3changeset_apply_strm(
10227 sqlite3 *db, /* Apply change to "main" db of this handle */
10228 int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
10229 void *pIn, /* First arg for xInput */
10230 int(*xFilter)(
10231 void *pCtx, /* Copy of sixth arg to _apply() */
10232 const char *zTab /* Table name */
10233 ),
10234 int(*xConflict)(
10235 void *pCtx, /* Copy of sixth arg to _apply() */
10236 int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */
10237 sqlite3_changeset_iter *p /* Handle describing change and conflict */
10238 ),
10239 void *pCtx /* First argument passed to xConflict */
10240 ){
10241 sqlite3_changeset_iter *pIter; /* Iterator to skip through changeset */
10242 int rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
10243 if( rc==SQLITE_OK ){
10244 rc = sessionChangesetApply(db, pIter, xFilter, xConflict, pCtx);
10245 }
10246 return rc;
10247 }
10248
10249 /*
10250 ** sqlite3_changegroup handle.
10251 */
10252 struct sqlite3_changegroup {
10253 int rc; /* Error code */
10254 int bPatch; /* True to accumulate patchsets */
10255 SessionTable *pList; /* List of tables in current patch */
10256 };
10257
10258 /*
10259 ** This function is called to merge two changes to the same row together as
10260 ** part of an sqlite3changeset_concat() operation. A new change object is
10261 ** allocated and a pointer to it stored in *ppNew.
10262 */
10263 static int sessionChangeMerge(
10264 SessionTable *pTab, /* Table structure */
10265 int bPatchset, /* True for patchsets */
10266 SessionChange *pExist, /* Existing change */
10267 int op2, /* Second change operation */
10268 int bIndirect, /* True if second change is indirect */
10269 u8 *aRec, /* Second change record */
10270 int nRec, /* Number of bytes in aRec */
10271 SessionChange **ppNew /* OUT: Merged change */
10272 ){
10273 SessionChange *pNew = 0;
10274
10275 if( !pExist ){
10276 pNew = (SessionChange *)sqlite3_malloc(sizeof(SessionChange) + nRec);
10277 if( !pNew ){
10278 return SQLITE_NOMEM;
10279 }
10280 memset(pNew, 0, sizeof(SessionChange));
10281 pNew->op = op2;
10282 pNew->bIndirect = bIndirect;
10283 pNew->nRecord = nRec;
10284 pNew->aRecord = (u8*)&pNew[1];
10285 memcpy(pNew->aRecord, aRec, nRec);
10286 }else{
10287 int op1 = pExist->op;
10288
10289 /*
10290 ** op1=INSERT, op2=INSERT -> Unsupported. Discard op2.
10291 ** op1=INSERT, op2=UPDATE -> INSERT.
10292 ** op1=INSERT, op2=DELETE -> (none)
10293 **
10294 ** op1=UPDATE, op2=INSERT -> Unsupported. Discard op2.
10295 ** op1=UPDATE, op2=UPDATE -> UPDATE.
10296 ** op1=UPDATE, op2=DELETE -> DELETE.
10297 **
10298 ** op1=DELETE, op2=INSERT -> UPDATE.
10299 ** op1=DELETE, op2=UPDATE -> Unsupported. Discard op2.
10300 ** op1=DELETE, op2=DELETE -> Unsupported. Discard op2.
10301 */
10302 if( (op1==SQLITE_INSERT && op2==SQLITE_INSERT)
10303 || (op1==SQLITE_UPDATE && op2==SQLITE_INSERT)
10304 || (op1==SQLITE_DELETE && op2==SQLITE_UPDATE)
10305 || (op1==SQLITE_DELETE && op2==SQLITE_DELETE)
10306 ){
10307 pNew = pExist;
10308 }else if( op1==SQLITE_INSERT && op2==SQLITE_DELETE ){
10309 sqlite3_free(pExist);
10310 assert( pNew==0 );
10311 }else{
10312 u8 *aExist = pExist->aRecord;
10313 int nByte;
10314 u8 *aCsr;
10315
10316 /* Allocate a new SessionChange object. Ensure that the aRecord[]
10317 ** buffer of the new object is large enough to hold any record that
10318 ** may be generated by combining the input records. */
10319 nByte = sizeof(SessionChange) + pExist->nRecord + nRec;
10320 pNew = (SessionChange *)sqlite3_malloc(nByte);
10321 if( !pNew ){
10322 sqlite3_free(pExist);
10323 return SQLITE_NOMEM;
10324 }
10325 memset(pNew, 0, sizeof(SessionChange));
10326 pNew->bIndirect = (bIndirect && pExist->bIndirect);
10327 aCsr = pNew->aRecord = (u8 *)&pNew[1];
10328
10329 if( op1==SQLITE_INSERT ){ /* INSERT + UPDATE */
10330 u8 *a1 = aRec;
10331 assert( op2==SQLITE_UPDATE );
10332 pNew->op = SQLITE_INSERT;
10333 if( bPatchset==0 ) sessionSkipRecord(&a1, pTab->nCol);
10334 sessionMergeRecord(&aCsr, pTab->nCol, aExist, a1);
10335 }else if( op1==SQLITE_DELETE ){ /* DELETE + INSERT */
10336 assert( op2==SQLITE_INSERT );
10337 pNew->op = SQLITE_UPDATE;
10338 if( bPatchset ){
10339 memcpy(aCsr, aRec, nRec);
10340 aCsr += nRec;
10341 }else{
10342 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aExist, 0,aRec,0) ){
10343 sqlite3_free(pNew);
10344 pNew = 0;
10345 }
10346 }
10347 }else if( op2==SQLITE_UPDATE ){ /* UPDATE + UPDATE */
10348 u8 *a1 = aExist;
10349 u8 *a2 = aRec;
10350 assert( op1==SQLITE_UPDATE );
10351 if( bPatchset==0 ){
10352 sessionSkipRecord(&a1, pTab->nCol);
10353 sessionSkipRecord(&a2, pTab->nCol);
10354 }
10355 pNew->op = SQLITE_UPDATE;
10356 if( 0==sessionMergeUpdate(&aCsr, pTab, bPatchset, aRec, aExist,a1,a2) ){
10357 sqlite3_free(pNew);
10358 pNew = 0;
10359 }
10360 }else{ /* UPDATE + DELETE */
10361 assert( op1==SQLITE_UPDATE && op2==SQLITE_DELETE );
10362 pNew->op = SQLITE_DELETE;
10363 if( bPatchset ){
10364 memcpy(aCsr, aRec, nRec);
10365 aCsr += nRec;
10366 }else{
10367 sessionMergeRecord(&aCsr, pTab->nCol, aRec, aExist);
10368 }
10369 }
10370
10371 if( pNew ){
10372 pNew->nRecord = (int)(aCsr - pNew->aRecord);
10373 }
10374 sqlite3_free(pExist);
10375 }
10376 }
10377
10378 *ppNew = pNew;
10379 return SQLITE_OK;
10380 }
10381
10382 /*
10383 ** Add all changes in the changeset traversed by the iterator passed as
10384 ** the first argument to the changegroup hash tables.
10385 */
10386 static int sessionChangesetToHash(
10387 sqlite3_changeset_iter *pIter, /* Iterator to read from */
10388 sqlite3_changegroup *pGrp /* Changegroup object to add changeset to */
10389 ){
10390 u8 *aRec;
10391 int nRec;
10392 int rc = SQLITE_OK;
10393 SessionTable *pTab = 0;
10394
10395
10396 while( SQLITE_ROW==sessionChangesetNext(pIter, &aRec, &nRec) ){
10397 const char *zNew;
10398 int nCol;
10399 int op;
10400 int iHash;
10401 int bIndirect;
10402 SessionChange *pChange;
10403 SessionChange *pExist = 0;
10404 SessionChange **pp;
10405
10406 if( pGrp->pList==0 ){
10407 pGrp->bPatch = pIter->bPatchset;
10408 }else if( pIter->bPatchset!=pGrp->bPatch ){
10409 rc = SQLITE_ERROR;
10410 break;
10411 }
10412
10413 sqlite3changeset_op(pIter, &zNew, &nCol, &op, &bIndirect);
10414 if( !pTab || sqlite3_stricmp(zNew, pTab->zName) ){
10415 /* Search the list for a matching table */
10416 int nNew = (int)strlen(zNew);
10417 u8 *abPK;
10418
10419 sqlite3changeset_pk(pIter, &abPK, 0);
10420 for(pTab = pGrp->pList; pTab; pTab=pTab->pNext){
10421 if( 0==sqlite3_strnicmp(pTab->zName, zNew, nNew+1) ) break;
10422 }
10423 if( !pTab ){
10424 SessionTable **ppTab;
10425
10426 pTab = sqlite3_malloc(sizeof(SessionTable) + nCol + nNew+1);
10427 if( !pTab ){
10428 rc = SQLITE_NOMEM;
10429 break;
10430 }
10431 memset(pTab, 0, sizeof(SessionTable));
10432 pTab->nCol = nCol;
10433 pTab->abPK = (u8*)&pTab[1];
10434 memcpy(pTab->abPK, abPK, nCol);
10435 pTab->zName = (char*)&pTab->abPK[nCol];
10436 memcpy(pTab->zName, zNew, nNew+1);
10437
10438 /* The new object must be linked on to the end of the list, not
10439 ** simply added to the start of it. This is to ensure that the
10440 ** tables within the output of sqlite3changegroup_output() are in
10441 ** the right order. */
10442 for(ppTab=&pGrp->pList; *ppTab; ppTab=&(*ppTab)->pNext);
10443 *ppTab = pTab;
10444 }else if( pTab->nCol!=nCol || memcmp(pTab->abPK, abPK, nCol) ){
10445 rc = SQLITE_SCHEMA;
10446 break;
10447 }
10448 }
10449
10450 if( sessionGrowHash(pIter->bPatchset, pTab) ){
10451 rc = SQLITE_NOMEM;
10452 break;
10453 }
10454 iHash = sessionChangeHash(
10455 pTab, (pIter->bPatchset && op==SQLITE_DELETE), aRec, pTab->nChange
10456 );
10457
10458 /* Search for existing entry. If found, remove it from the hash table.
10459 ** Code below may link it back in.
10460 */
10461 for(pp=&pTab->apChange[iHash]; *pp; pp=&(*pp)->pNext){
10462 int bPkOnly1 = 0;
10463 int bPkOnly2 = 0;
10464 if( pIter->bPatchset ){
10465 bPkOnly1 = (*pp)->op==SQLITE_DELETE;
10466 bPkOnly2 = op==SQLITE_DELETE;
10467 }
10468 if( sessionChangeEqual(pTab, bPkOnly1, (*pp)->aRecord, bPkOnly2, aRec) ){
10469 pExist = *pp;
10470 *pp = (*pp)->pNext;
10471 pTab->nEntry--;
10472 break;
10473 }
10474 }
10475
10476 rc = sessionChangeMerge(pTab,
10477 pIter->bPatchset, pExist, op, bIndirect, aRec, nRec, &pChange
10478 );
10479 if( rc ) break;
10480 if( pChange ){
10481 pChange->pNext = pTab->apChange[iHash];
10482 pTab->apChange[iHash] = pChange;
10483 pTab->nEntry++;
10484 }
10485 }
10486
10487 if( rc==SQLITE_OK ) rc = pIter->rc;
10488 return rc;
10489 }
10490
10491 /*
10492 ** Serialize a changeset (or patchset) based on all changesets (or patchsets)
10493 ** added to the changegroup object passed as the first argument.
10494 **
10495 ** If xOutput is not NULL, then the changeset/patchset is returned to the
10496 ** user via one or more calls to xOutput, as with the other streaming
10497 ** interfaces.
10498 **
10499 ** Or, if xOutput is NULL, then (*ppOut) is populated with a pointer to a
10500 ** buffer containing the output changeset before this function returns. In
10501 ** this case (*pnOut) is set to the size of the output buffer in bytes. It
10502 ** is the responsibility of the caller to free the output buffer using
10503 ** sqlite3_free() when it is no longer required.
10504 **
10505 ** If successful, SQLITE_OK is returned. Or, if an error occurs, an SQLite
10506 ** error code. If an error occurs and xOutput is NULL, (*ppOut) and (*pnOut)
10507 ** are both set to 0 before returning.
10508 */
10509 static int sessionChangegroupOutput(
10510 sqlite3_changegroup *pGrp,
10511 int (*xOutput)(void *pOut, const void *pData, int nData),
10512 void *pOut,
10513 int *pnOut,
10514 void **ppOut
10515 ){
10516 int rc = SQLITE_OK;
10517 SessionBuffer buf = {0, 0, 0};
10518 SessionTable *pTab;
10519 assert( xOutput==0 || (ppOut==0 && pnOut==0) );
10520
10521 /* Create the serialized output changeset based on the contents of the
10522 ** hash tables attached to the SessionTable objects in list p->pList.
10523 */
10524 for(pTab=pGrp->pList; rc==SQLITE_OK && pTab; pTab=pTab->pNext){
10525 int i;
10526 if( pTab->nEntry==0 ) continue;
10527
10528 sessionAppendTableHdr(&buf, pGrp->bPatch, pTab, &rc);
10529 for(i=0; i<pTab->nChange; i++){
10530 SessionChange *p;
10531 for(p=pTab->apChange[i]; p; p=p->pNext){
10532 sessionAppendByte(&buf, p->op, &rc);
10533 sessionAppendByte(&buf, p->bIndirect, &rc);
10534 sessionAppendBlob(&buf, p->aRecord, p->nRecord, &rc);
10535 }
10536 }
10537
10538 if( rc==SQLITE_OK && xOutput && buf.nBuf>=SESSIONS_STRM_CHUNK_SIZE ){
10539 rc = xOutput(pOut, buf.aBuf, buf.nBuf);
10540 buf.nBuf = 0;
10541 }
10542 }
10543
10544 if( rc==SQLITE_OK ){
10545 if( xOutput ){
10546 if( buf.nBuf>0 ) rc = xOutput(pOut, buf.aBuf, buf.nBuf);
10547 }else{
10548 *ppOut = buf.aBuf;
10549 *pnOut = buf.nBuf;
10550 buf.aBuf = 0;
10551 }
10552 }
10553 sqlite3_free(buf.aBuf);
10554
10555 return rc;
10556 }
10557
10558 /*
10559 ** Allocate a new, empty, sqlite3_changegroup.
10560 */
10561 SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp){
10562 int rc = SQLITE_OK; /* Return code */
10563 sqlite3_changegroup *p; /* New object */
10564 p = (sqlite3_changegroup*)sqlite3_malloc(sizeof(sqlite3_changegroup));
10565 if( p==0 ){
10566 rc = SQLITE_NOMEM;
10567 }else{
10568 memset(p, 0, sizeof(sqlite3_changegroup));
10569 }
10570 *pp = p;
10571 return rc;
10572 }
10573
10574 /*
10575 ** Add the changeset currently stored in buffer pData, size nData bytes,
10576 ** to changeset-group p.
10577 */
10578 SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup *pGrp, int nData, void *pData){
10579 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
10580 int rc; /* Return code */
10581
10582 rc = sqlite3changeset_start(&pIter, nData, pData);
10583 if( rc==SQLITE_OK ){
10584 rc = sessionChangesetToHash(pIter, pGrp);
10585 }
10586 sqlite3changeset_finalize(pIter);
10587 return rc;
10588 }
10589
10590 /*
10591 ** Obtain a buffer containing a changeset representing the concatenation
10592 ** of all changesets added to the group so far.
10593 */
10594 SQLITE_API int sqlite3changegroup_output(
10595 sqlite3_changegroup *pGrp,
10596 int *pnData,
10597 void **ppData
10598 ){
10599 return sessionChangegroupOutput(pGrp, 0, 0, pnData, ppData);
10600 }
10601
10602 /*
10603 ** Streaming versions of changegroup_add().
10604 */
10605 SQLITE_API int sqlite3changegroup_add_strm(
10606 sqlite3_changegroup *pGrp,
10607 int (*xInput)(void *pIn, void *pData, int *pnData),
10608 void *pIn
10609 ){
10610 sqlite3_changeset_iter *pIter; /* Iterator opened on pData/nData */
10611 int rc; /* Return code */
10612
10613 rc = sqlite3changeset_start_strm(&pIter, xInput, pIn);
10614 if( rc==SQLITE_OK ){
10615 rc = sessionChangesetToHash(pIter, pGrp);
10616 }
10617 sqlite3changeset_finalize(pIter);
10618 return rc;
10619 }
10620
10621 /*
10622 ** Streaming versions of changegroup_output().
10623 */
10624 SQLITE_API int sqlite3changegroup_output_strm(
10625 sqlite3_changegroup *pGrp,
10626 int (*xOutput)(void *pOut, const void *pData, int nData),
10627 void *pOut
10628 ){
10629 return sessionChangegroupOutput(pGrp, xOutput, pOut, 0, 0);
10630 }
10631
10632 /*
10633 ** Delete a changegroup object.
10634 */
10635 SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup *pGrp){
10636 if( pGrp ){
10637 sessionDeleteTable(pGrp->pList);
10638 sqlite3_free(pGrp);
10639 }
10640 }
10641
10642 /*
10643 ** Combine two changesets together.
10644 */
10645 SQLITE_API int sqlite3changeset_concat(
10646 int nLeft, /* Number of bytes in lhs input */
10647 void *pLeft, /* Lhs input changeset */
10648 int nRight /* Number of bytes in rhs input */,
10649 void *pRight, /* Rhs input changeset */
10650 int *pnOut, /* OUT: Number of bytes in output changeset */
10651 void **ppOut /* OUT: changeset (left <concat> right) */
10652 ){
10653 sqlite3_changegroup *pGrp;
10654 int rc;
10655
10656 rc = sqlite3changegroup_new(&pGrp);
10657 if( rc==SQLITE_OK ){
10658 rc = sqlite3changegroup_add(pGrp, nLeft, pLeft);
10659 }
10660 if( rc==SQLITE_OK ){
10661 rc = sqlite3changegroup_add(pGrp, nRight, pRight);
10662 }
10663 if( rc==SQLITE_OK ){
10664 rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
10665 }
10666 sqlite3changegroup_delete(pGrp);
10667
10668 return rc;
10669 }
10670
10671 /*
10672 ** Streaming version of sqlite3changeset_concat().
10673 */
10674 SQLITE_API int sqlite3changeset_concat_strm(
10675 int (*xInputA)(void *pIn, void *pData, int *pnData),
10676 void *pInA,
10677 int (*xInputB)(void *pIn, void *pData, int *pnData),
10678 void *pInB,
10679 int (*xOutput)(void *pOut, const void *pData, int nData),
10680 void *pOut
10681 ){
10682 sqlite3_changegroup *pGrp;
10683 int rc;
10684
10685 rc = sqlite3changegroup_new(&pGrp);
10686 if( rc==SQLITE_OK ){
10687 rc = sqlite3changegroup_add_strm(pGrp, xInputA, pInA);
10688 }
10689 if( rc==SQLITE_OK ){
10690 rc = sqlite3changegroup_add_strm(pGrp, xInputB, pInB);
10691 }
10692 if( rc==SQLITE_OK ){
10693 rc = sqlite3changegroup_output_strm(pGrp, xOutput, pOut);
10694 }
10695 sqlite3changegroup_delete(pGrp);
10696
10697 return rc;
10698 }
10699
10700 #endif /* SQLITE_ENABLE_SESSION && SQLITE_ENABLE_PREUPDATE_HOOK */
10701
10702 /************** End of sqlite3session.c **************************************/
10703 /************** Begin file json1.c *******************************************/
10704 /*
10705 ** 2015-08-12
10706 **
10707 ** The author disclaims copyright to this source code. In place of
10708 ** a legal notice, here is a blessing:
10709 **
10710 ** May you do good and not evil.
10711 ** May you find forgiveness for yourself and forgive others.
10712 ** May you share freely, never taking more than you give.
10713 **
10714 ******************************************************************************
10715 **
10716 ** This SQLite extension implements JSON functions. The interface is
10717 ** modeled after MySQL JSON functions:
10718 **
10719 ** https://dev.mysql.com/doc/refman/5.7/en/json.html
10720 **
10721 ** For the time being, all JSON is stored as pure text. (We might add
10722 ** a JSONB type in the future which stores a binary encoding of JSON in
10723 ** a BLOB, but there is no support for JSONB in the current implementation.
10724 ** This implementation parses JSON text at 250 MB/s, so it is hard to see
10725 ** how JSONB might improve on that.)
10726 */
10727 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
10728 #if !defined(SQLITEINT_H)
10729 /* #include "sqlite3ext.h" */
10730 #endif
10731 SQLITE_EXTENSION_INIT1
10732 /* #include <assert.h> */
10733 /* #include <string.h> */
10734 /* #include <stdlib.h> */
10735 /* #include <stdarg.h> */
10736
10737 /* Mark a function parameter as unused, to suppress nuisance compiler
10738 ** warnings. */
10739 #ifndef UNUSED_PARAM
10740 # define UNUSED_PARAM(X) (void)(X)
10741 #endif
10742
10743 #ifndef LARGEST_INT64
10744 # define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
10745 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
10746 #endif
10747
10748 /*
10749 ** Versions of isspace(), isalnum() and isdigit() to which it is safe
10750 ** to pass signed char values.
10751 */
10752 #ifdef sqlite3Isdigit
10753 /* Use the SQLite core versions if this routine is part of the
10754 ** SQLite amalgamation */
10755 # define safe_isdigit(x) sqlite3Isdigit(x)
10756 # define safe_isalnum(x) sqlite3Isalnum(x)
10757 # define safe_isxdigit(x) sqlite3Isxdigit(x)
10758 #else
10759 /* Use the standard library for separate compilation */
10760 #include <ctype.h> /* amalgamator: keep */
10761 # define safe_isdigit(x) isdigit((unsigned char)(x))
10762 # define safe_isalnum(x) isalnum((unsigned char)(x))
10763 # define safe_isxdigit(x) isxdigit((unsigned char)(x))
10764 #endif
10765
10766 /*
10767 ** Growing our own isspace() routine this way is twice as fast as
10768 ** the library isspace() function, resulting in a 7% overall performance
10769 ** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
10770 */
10771 static const char jsonIsSpace[] = {
10772 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
10773 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10774 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10775 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10776 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10777 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10778 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10779 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10780 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10781 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10782 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10783 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10784 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10785 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10786 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10787 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10788 };
10789 #define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
10790
10791 #ifndef SQLITE_AMALGAMATION
10792 /* Unsigned integer types. These are already defined in the sqliteInt.h,
10793 ** but the definitions need to be repeated for separate compilation. */
10794 typedef sqlite3_uint64 u64;
10795 typedef unsigned int u32;
10796 typedef unsigned char u8;
10797 #endif
10798
10799 /* Objects */
10800 typedef struct JsonString JsonString;
10801 typedef struct JsonNode JsonNode;
10802 typedef struct JsonParse JsonParse;
10803
10804 /* An instance of this object represents a JSON string
10805 ** under construction. Really, this is a generic string accumulator
10806 ** that can be and is used to create strings other than JSON.
10807 */
10808 struct JsonString {
10809 sqlite3_context *pCtx; /* Function context - put error messages here */
10810 char *zBuf; /* Append JSON content here */
10811 u64 nAlloc; /* Bytes of storage available in zBuf[] */
10812 u64 nUsed; /* Bytes of zBuf[] currently used */
10813 u8 bStatic; /* True if zBuf is static space */
10814 u8 bErr; /* True if an error has been encountered */
10815 char zSpace[100]; /* Initial static space */
10816 };
10817
10818 /* JSON type values
10819 */
10820 #define JSON_NULL 0
10821 #define JSON_TRUE 1
10822 #define JSON_FALSE 2
10823 #define JSON_INT 3
10824 #define JSON_REAL 4
10825 #define JSON_STRING 5
10826 #define JSON_ARRAY 6
10827 #define JSON_OBJECT 7
10828
10829 /* The "subtype" set for JSON values */
10830 #define JSON_SUBTYPE 74 /* Ascii for "J" */
10831
10832 /*
10833 ** Names of the various JSON types:
10834 */
10835 static const char * const jsonType[] = {
10836 "null", "true", "false", "integer", "real", "text", "array", "object"
10837 };
10838
10839 /* Bit values for the JsonNode.jnFlag field
10840 */
10841 #define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
10842 #define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
10843 #define JNODE_REMOVE 0x04 /* Do not output */
10844 #define JNODE_REPLACE 0x08 /* Replace with JsonNode.iVal */
10845 #define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */
10846 #define JNODE_LABEL 0x20 /* Is a label of an object */
10847
10848
10849 /* A single node of parsed JSON
10850 */
10851 struct JsonNode {
10852 u8 eType; /* One of the JSON_ type values */
10853 u8 jnFlags; /* JNODE flags */
10854 u8 iVal; /* Replacement value when JNODE_REPLACE */
10855 u32 n; /* Bytes of content, or number of sub-nodes */
10856 union {
10857 const char *zJContent; /* Content for INT, REAL, and STRING */
10858 u32 iAppend; /* More terms for ARRAY and OBJECT */
10859 u32 iKey; /* Key for ARRAY objects in json_tree() */
10860 } u;
10861 };
10862
10863 /* A completely parsed JSON string
10864 */
10865 struct JsonParse {
10866 u32 nNode; /* Number of slots of aNode[] used */
10867 u32 nAlloc; /* Number of slots of aNode[] allocated */
10868 JsonNode *aNode; /* Array of nodes containing the parse */
10869 const char *zJson; /* Original JSON string */
10870 u32 *aUp; /* Index of parent of each node */
10871 u8 oom; /* Set to true if out of memory */
10872 u8 nErr; /* Number of errors seen */
10873 };
10874
10875 /**************************************************************************
10876 ** Utility routines for dealing with JsonString objects
10877 **************************************************************************/
10878
10879 /* Set the JsonString object to an empty string
10880 */
10881 static void jsonZero(JsonString *p){
10882 p->zBuf = p->zSpace;
10883 p->nAlloc = sizeof(p->zSpace);
10884 p->nUsed = 0;
10885 p->bStatic = 1;
10886 }
10887
10888 /* Initialize the JsonString object
10889 */
10890 static void jsonInit(JsonString *p, sqlite3_context *pCtx){
10891 p->pCtx = pCtx;
10892 p->bErr = 0;
10893 jsonZero(p);
10894 }
10895
10896
10897 /* Free all allocated memory and reset the JsonString object back to its
10898 ** initial state.
10899 */
10900 static void jsonReset(JsonString *p){
10901 if( !p->bStatic ) sqlite3_free(p->zBuf);
10902 jsonZero(p);
10903 }
10904
10905
10906 /* Report an out-of-memory (OOM) condition
10907 */
10908 static void jsonOom(JsonString *p){
10909 p->bErr = 1;
10910 sqlite3_result_error_nomem(p->pCtx);
10911 jsonReset(p);
10912 }
10913
10914 /* Enlarge pJson->zBuf so that it can hold at least N more bytes.
10915 ** Return zero on success. Return non-zero on an OOM error
10916 */
10917 static int jsonGrow(JsonString *p, u32 N){
10918 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
10919 char *zNew;
10920 if( p->bStatic ){
10921 if( p->bErr ) return 1;
10922 zNew = sqlite3_malloc64(nTotal);
10923 if( zNew==0 ){
10924 jsonOom(p);
10925 return SQLITE_NOMEM;
10926 }
10927 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
10928 p->zBuf = zNew;
10929 p->bStatic = 0;
10930 }else{
10931 zNew = sqlite3_realloc64(p->zBuf, nTotal);
10932 if( zNew==0 ){
10933 jsonOom(p);
10934 return SQLITE_NOMEM;
10935 }
10936 p->zBuf = zNew;
10937 }
10938 p->nAlloc = nTotal;
10939 return SQLITE_OK;
10940 }
10941
10942 /* Append N bytes from zIn onto the end of the JsonString string.
10943 */
10944 static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
10945 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
10946 memcpy(p->zBuf+p->nUsed, zIn, N);
10947 p->nUsed += N;
10948 }
10949
10950 /* Append formatted text (not to exceed N bytes) to the JsonString.
10951 */
10952 static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
10953 va_list ap;
10954 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
10955 va_start(ap, zFormat);
10956 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
10957 va_end(ap);
10958 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
10959 }
10960
10961 /* Append a single character
10962 */
10963 static void jsonAppendChar(JsonString *p, char c){
10964 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
10965 p->zBuf[p->nUsed++] = c;
10966 }
10967
10968 /* Append a comma separator to the output buffer, if the previous
10969 ** character is not '[' or '{'.
10970 */
10971 static void jsonAppendSeparator(JsonString *p){
10972 char c;
10973 if( p->nUsed==0 ) return;
10974 c = p->zBuf[p->nUsed-1];
10975 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
10976 }
10977
10978 /* Append the N-byte string in zIn to the end of the JsonString string
10979 ** under construction. Enclose the string in "..." and escape
10980 ** any double-quotes or backslash characters contained within the
10981 ** string.
10982 */
10983 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
10984 u32 i;
10985 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
10986 p->zBuf[p->nUsed++] = '"';
10987 for(i=0; i<N; i++){
10988 unsigned char c = ((unsigned const char*)zIn)[i];
10989 if( c=='"' || c=='\\' ){
10990 json_simple_escape:
10991 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
10992 p->zBuf[p->nUsed++] = '\\';
10993 }else if( c<=0x1f ){
10994 static const char aSpecial[] = {
10995 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
10996 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
10997 };
10998 assert( sizeof(aSpecial)==32 );
10999 assert( aSpecial['\b']=='b' );
11000 assert( aSpecial['\f']=='f' );
11001 assert( aSpecial['\n']=='n' );
11002 assert( aSpecial['\r']=='r' );
11003 assert( aSpecial['\t']=='t' );
11004 if( aSpecial[c] ){
11005 c = aSpecial[c];
11006 goto json_simple_escape;
11007 }
11008 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
11009 p->zBuf[p->nUsed++] = '\\';
11010 p->zBuf[p->nUsed++] = 'u';
11011 p->zBuf[p->nUsed++] = '0';
11012 p->zBuf[p->nUsed++] = '0';
11013 p->zBuf[p->nUsed++] = '0' + (c>>4);
11014 c = "0123456789abcdef"[c&0xf];
11015 }
11016 p->zBuf[p->nUsed++] = c;
11017 }
11018 p->zBuf[p->nUsed++] = '"';
11019 assert( p->nUsed<p->nAlloc );
11020 }
11021
11022 /*
11023 ** Append a function parameter value to the JSON string under
11024 ** construction.
11025 */
11026 static void jsonAppendValue(
11027 JsonString *p, /* Append to this JSON string */
11028 sqlite3_value *pValue /* Value to append */
11029 ){
11030 switch( sqlite3_value_type(pValue) ){
11031 case SQLITE_NULL: {
11032 jsonAppendRaw(p, "null", 4);
11033 break;
11034 }
11035 case SQLITE_INTEGER:
11036 case SQLITE_FLOAT: {
11037 const char *z = (const char*)sqlite3_value_text(pValue);
11038 u32 n = (u32)sqlite3_value_bytes(pValue);
11039 jsonAppendRaw(p, z, n);
11040 break;
11041 }
11042 case SQLITE_TEXT: {
11043 const char *z = (const char*)sqlite3_value_text(pValue);
11044 u32 n = (u32)sqlite3_value_bytes(pValue);
11045 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
11046 jsonAppendRaw(p, z, n);
11047 }else{
11048 jsonAppendString(p, z, n);
11049 }
11050 break;
11051 }
11052 default: {
11053 if( p->bErr==0 ){
11054 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
11055 p->bErr = 2;
11056 jsonReset(p);
11057 }
11058 break;
11059 }
11060 }
11061 }
11062
11063
11064 /* Make the JSON in p the result of the SQL function.
11065 */
11066 static void jsonResult(JsonString *p){
11067 if( p->bErr==0 ){
11068 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
11069 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
11070 SQLITE_UTF8);
11071 jsonZero(p);
11072 }
11073 assert( p->bStatic );
11074 }
11075
11076 /**************************************************************************
11077 ** Utility routines for dealing with JsonNode and JsonParse objects
11078 **************************************************************************/
11079
11080 /*
11081 ** Return the number of consecutive JsonNode slots need to represent
11082 ** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
11083 ** OBJECT types, the number might be larger.
11084 **
11085 ** Appended elements are not counted. The value returned is the number
11086 ** by which the JsonNode counter should increment in order to go to the
11087 ** next peer value.
11088 */
11089 static u32 jsonNodeSize(JsonNode *pNode){
11090 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
11091 }
11092
11093 /*
11094 ** Reclaim all memory allocated by a JsonParse object. But do not
11095 ** delete the JsonParse object itself.
11096 */
11097 static void jsonParseReset(JsonParse *pParse){
11098 sqlite3_free(pParse->aNode);
11099 pParse->aNode = 0;
11100 pParse->nNode = 0;
11101 pParse->nAlloc = 0;
11102 sqlite3_free(pParse->aUp);
11103 pParse->aUp = 0;
11104 }
11105
11106 /*
11107 ** Convert the JsonNode pNode into a pure JSON string and
11108 ** append to pOut. Subsubstructure is also included. Return
11109 ** the number of JsonNode objects that are encoded.
11110 */
11111 static void jsonRenderNode(
11112 JsonNode *pNode, /* The node to render */
11113 JsonString *pOut, /* Write JSON here */
11114 sqlite3_value **aReplace /* Replacement values */
11115 ){
11116 switch( pNode->eType ){
11117 default: {
11118 assert( pNode->eType==JSON_NULL );
11119 jsonAppendRaw(pOut, "null", 4);
11120 break;
11121 }
11122 case JSON_TRUE: {
11123 jsonAppendRaw(pOut, "true", 4);
11124 break;
11125 }
11126 case JSON_FALSE: {
11127 jsonAppendRaw(pOut, "false", 5);
11128 break;
11129 }
11130 case JSON_STRING: {
11131 if( pNode->jnFlags & JNODE_RAW ){
11132 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
11133 break;
11134 }
11135 /* Fall through into the next case */
11136 }
11137 case JSON_REAL:
11138 case JSON_INT: {
11139 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
11140 break;
11141 }
11142 case JSON_ARRAY: {
11143 u32 j = 1;
11144 jsonAppendChar(pOut, '[');
11145 for(;;){
11146 while( j<=pNode->n ){
11147 if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
11148 if( pNode[j].jnFlags & JNODE_REPLACE ){
11149 jsonAppendSeparator(pOut);
11150 jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
11151 }
11152 }else{
11153 jsonAppendSeparator(pOut);
11154 jsonRenderNode(&pNode[j], pOut, aReplace);
11155 }
11156 j += jsonNodeSize(&pNode[j]);
11157 }
11158 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
11159 pNode = &pNode[pNode->u.iAppend];
11160 j = 1;
11161 }
11162 jsonAppendChar(pOut, ']');
11163 break;
11164 }
11165 case JSON_OBJECT: {
11166 u32 j = 1;
11167 jsonAppendChar(pOut, '{');
11168 for(;;){
11169 while( j<=pNode->n ){
11170 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
11171 jsonAppendSeparator(pOut);
11172 jsonRenderNode(&pNode[j], pOut, aReplace);
11173 jsonAppendChar(pOut, ':');
11174 if( pNode[j+1].jnFlags & JNODE_REPLACE ){
11175 jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
11176 }else{
11177 jsonRenderNode(&pNode[j+1], pOut, aReplace);
11178 }
11179 }
11180 j += 1 + jsonNodeSize(&pNode[j+1]);
11181 }
11182 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
11183 pNode = &pNode[pNode->u.iAppend];
11184 j = 1;
11185 }
11186 jsonAppendChar(pOut, '}');
11187 break;
11188 }
11189 }
11190 }
11191
11192 /*
11193 ** Return a JsonNode and all its descendents as a JSON string.
11194 */
11195 static void jsonReturnJson(
11196 JsonNode *pNode, /* Node to return */
11197 sqlite3_context *pCtx, /* Return value for this function */
11198 sqlite3_value **aReplace /* Array of replacement values */
11199 ){
11200 JsonString s;
11201 jsonInit(&s, pCtx);
11202 jsonRenderNode(pNode, &s, aReplace);
11203 jsonResult(&s);
11204 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
11205 }
11206
11207 /*
11208 ** Make the JsonNode the return value of the function.
11209 */
11210 static void jsonReturn(
11211 JsonNode *pNode, /* Node to return */
11212 sqlite3_context *pCtx, /* Return value for this function */
11213 sqlite3_value **aReplace /* Array of replacement values */
11214 ){
11215 switch( pNode->eType ){
11216 default: {
11217 assert( pNode->eType==JSON_NULL );
11218 sqlite3_result_null(pCtx);
11219 break;
11220 }
11221 case JSON_TRUE: {
11222 sqlite3_result_int(pCtx, 1);
11223 break;
11224 }
11225 case JSON_FALSE: {
11226 sqlite3_result_int(pCtx, 0);
11227 break;
11228 }
11229 case JSON_INT: {
11230 sqlite3_int64 i = 0;
11231 const char *z = pNode->u.zJContent;
11232 if( z[0]=='-' ){ z++; }
11233 while( z[0]>='0' && z[0]<='9' ){
11234 unsigned v = *(z++) - '0';
11235 if( i>=LARGEST_INT64/10 ){
11236 if( i>LARGEST_INT64/10 ) goto int_as_real;
11237 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
11238 if( v==9 ) goto int_as_real;
11239 if( v==8 ){
11240 if( pNode->u.zJContent[0]=='-' ){
11241 sqlite3_result_int64(pCtx, SMALLEST_INT64);
11242 goto int_done;
11243 }else{
11244 goto int_as_real;
11245 }
11246 }
11247 }
11248 i = i*10 + v;
11249 }
11250 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
11251 sqlite3_result_int64(pCtx, i);
11252 int_done:
11253 break;
11254 int_as_real: /* fall through to real */;
11255 }
11256 case JSON_REAL: {
11257 double r;
11258 #ifdef SQLITE_AMALGAMATION
11259 const char *z = pNode->u.zJContent;
11260 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
11261 #else
11262 r = strtod(pNode->u.zJContent, 0);
11263 #endif
11264 sqlite3_result_double(pCtx, r);
11265 break;
11266 }
11267 case JSON_STRING: {
11268 #if 0 /* Never happens because JNODE_RAW is only set by json_set(),
11269 ** json_insert() and json_replace() and those routines do not
11270 ** call jsonReturn() */
11271 if( pNode->jnFlags & JNODE_RAW ){
11272 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
11273 SQLITE_TRANSIENT);
11274 }else
11275 #endif
11276 assert( (pNode->jnFlags & JNODE_RAW)==0 );
11277 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
11278 /* JSON formatted without any backslash-escapes */
11279 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
11280 SQLITE_TRANSIENT);
11281 }else{
11282 /* Translate JSON formatted string into raw text */
11283 u32 i;
11284 u32 n = pNode->n;
11285 const char *z = pNode->u.zJContent;
11286 char *zOut;
11287 u32 j;
11288 zOut = sqlite3_malloc( n+1 );
11289 if( zOut==0 ){
11290 sqlite3_result_error_nomem(pCtx);
11291 break;
11292 }
11293 for(i=1, j=0; i<n-1; i++){
11294 char c = z[i];
11295 if( c!='\\' ){
11296 zOut[j++] = c;
11297 }else{
11298 c = z[++i];
11299 if( c=='u' ){
11300 u32 v = 0, k;
11301 for(k=0; k<4; i++, k++){
11302 assert( i<n-2 );
11303 c = z[i+1];
11304 assert( safe_isxdigit(c) );
11305 if( c<='9' ) v = v*16 + c - '0';
11306 else if( c<='F' ) v = v*16 + c - 'A' + 10;
11307 else v = v*16 + c - 'a' + 10;
11308 }
11309 if( v==0 ) break;
11310 if( v<=0x7f ){
11311 zOut[j++] = (char)v;
11312 }else if( v<=0x7ff ){
11313 zOut[j++] = (char)(0xc0 | (v>>6));
11314 zOut[j++] = 0x80 | (v&0x3f);
11315 }else{
11316 zOut[j++] = (char)(0xe0 | (v>>12));
11317 zOut[j++] = 0x80 | ((v>>6)&0x3f);
11318 zOut[j++] = 0x80 | (v&0x3f);
11319 }
11320 }else{
11321 if( c=='b' ){
11322 c = '\b';
11323 }else if( c=='f' ){
11324 c = '\f';
11325 }else if( c=='n' ){
11326 c = '\n';
11327 }else if( c=='r' ){
11328 c = '\r';
11329 }else if( c=='t' ){
11330 c = '\t';
11331 }
11332 zOut[j++] = c;
11333 }
11334 }
11335 }
11336 zOut[j] = 0;
11337 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
11338 }
11339 break;
11340 }
11341 case JSON_ARRAY:
11342 case JSON_OBJECT: {
11343 jsonReturnJson(pNode, pCtx, aReplace);
11344 break;
11345 }
11346 }
11347 }
11348
11349 /* Forward reference */
11350 static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
11351
11352 /*
11353 ** A macro to hint to the compiler that a function should not be
11354 ** inlined.
11355 */
11356 #if defined(__GNUC__)
11357 # define JSON_NOINLINE __attribute__((noinline))
11358 #elif defined(_MSC_VER) && _MSC_VER>=1310
11359 # define JSON_NOINLINE __declspec(noinline)
11360 #else
11361 # define JSON_NOINLINE
11362 #endif
11363
11364
11365 static JSON_NOINLINE int jsonParseAddNodeExpand(
11366 JsonParse *pParse, /* Append the node to this object */
11367 u32 eType, /* Node type */
11368 u32 n, /* Content size or sub-node count */
11369 const char *zContent /* Content */
11370 ){
11371 u32 nNew;
11372 JsonNode *pNew;
11373 assert( pParse->nNode>=pParse->nAlloc );
11374 if( pParse->oom ) return -1;
11375 nNew = pParse->nAlloc*2 + 10;
11376 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
11377 if( pNew==0 ){
11378 pParse->oom = 1;
11379 return -1;
11380 }
11381 pParse->nAlloc = nNew;
11382 pParse->aNode = pNew;
11383 assert( pParse->nNode<pParse->nAlloc );
11384 return jsonParseAddNode(pParse, eType, n, zContent);
11385 }
11386
11387 /*
11388 ** Create a new JsonNode instance based on the arguments and append that
11389 ** instance to the JsonParse. Return the index in pParse->aNode[] of the
11390 ** new node, or -1 if a memory allocation fails.
11391 */
11392 static int jsonParseAddNode(
11393 JsonParse *pParse, /* Append the node to this object */
11394 u32 eType, /* Node type */
11395 u32 n, /* Content size or sub-node count */
11396 const char *zContent /* Content */
11397 ){
11398 JsonNode *p;
11399 if( pParse->nNode>=pParse->nAlloc ){
11400 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
11401 }
11402 p = &pParse->aNode[pParse->nNode];
11403 p->eType = (u8)eType;
11404 p->jnFlags = 0;
11405 p->iVal = 0;
11406 p->n = n;
11407 p->u.zJContent = zContent;
11408 return pParse->nNode++;
11409 }
11410
11411 /*
11412 ** Return true if z[] begins with 4 (or more) hexadecimal digits
11413 */
11414 static int jsonIs4Hex(const char *z){
11415 int i;
11416 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
11417 return 1;
11418 }
11419
11420 /*
11421 ** Parse a single JSON value which begins at pParse->zJson[i]. Return the
11422 ** index of the first character past the end of the value parsed.
11423 **
11424 ** Return negative for a syntax error. Special cases: return -2 if the
11425 ** first non-whitespace character is '}' and return -3 if the first
11426 ** non-whitespace character is ']'.
11427 */
11428 static int jsonParseValue(JsonParse *pParse, u32 i){
11429 char c;
11430 u32 j;
11431 int iThis;
11432 int x;
11433 JsonNode *pNode;
11434 while( safe_isspace(pParse->zJson[i]) ){ i++; }
11435 if( (c = pParse->zJson[i])=='{' ){
11436 /* Parse object */
11437 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
11438 if( iThis<0 ) return -1;
11439 for(j=i+1;;j++){
11440 while( safe_isspace(pParse->zJson[j]) ){ j++; }
11441 x = jsonParseValue(pParse, j);
11442 if( x<0 ){
11443 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
11444 return -1;
11445 }
11446 if( pParse->oom ) return -1;
11447 pNode = &pParse->aNode[pParse->nNode-1];
11448 if( pNode->eType!=JSON_STRING ) return -1;
11449 pNode->jnFlags |= JNODE_LABEL;
11450 j = x;
11451 while( safe_isspace(pParse->zJson[j]) ){ j++; }
11452 if( pParse->zJson[j]!=':' ) return -1;
11453 j++;
11454 x = jsonParseValue(pParse, j);
11455 if( x<0 ) return -1;
11456 j = x;
11457 while( safe_isspace(pParse->zJson[j]) ){ j++; }
11458 c = pParse->zJson[j];
11459 if( c==',' ) continue;
11460 if( c!='}' ) return -1;
11461 break;
11462 }
11463 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
11464 return j+1;
11465 }else if( c=='[' ){
11466 /* Parse array */
11467 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
11468 if( iThis<0 ) return -1;
11469 for(j=i+1;;j++){
11470 while( safe_isspace(pParse->zJson[j]) ){ j++; }
11471 x = jsonParseValue(pParse, j);
11472 if( x<0 ){
11473 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
11474 return -1;
11475 }
11476 j = x;
11477 while( safe_isspace(pParse->zJson[j]) ){ j++; }
11478 c = pParse->zJson[j];
11479 if( c==',' ) continue;
11480 if( c!=']' ) return -1;
11481 break;
11482 }
11483 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
11484 return j+1;
11485 }else if( c=='"' ){
11486 /* Parse string */
11487 u8 jnFlags = 0;
11488 j = i+1;
11489 for(;;){
11490 c = pParse->zJson[j];
11491 if( c==0 ) return -1;
11492 if( c=='\\' ){
11493 c = pParse->zJson[++j];
11494 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
11495 || c=='n' || c=='r' || c=='t'
11496 || (c=='u' && jsonIs4Hex(pParse->zJson+j+1)) ){
11497 jnFlags = JNODE_ESCAPE;
11498 }else{
11499 return -1;
11500 }
11501 }else if( c=='"' ){
11502 break;
11503 }
11504 j++;
11505 }
11506 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
11507 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
11508 return j+1;
11509 }else if( c=='n'
11510 && strncmp(pParse->zJson+i,"null",4)==0
11511 && !safe_isalnum(pParse->zJson[i+4]) ){
11512 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
11513 return i+4;
11514 }else if( c=='t'
11515 && strncmp(pParse->zJson+i,"true",4)==0
11516 && !safe_isalnum(pParse->zJson[i+4]) ){
11517 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
11518 return i+4;
11519 }else if( c=='f'
11520 && strncmp(pParse->zJson+i,"false",5)==0
11521 && !safe_isalnum(pParse->zJson[i+5]) ){
11522 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
11523 return i+5;
11524 }else if( c=='-' || (c>='0' && c<='9') ){
11525 /* Parse number */
11526 u8 seenDP = 0;
11527 u8 seenE = 0;
11528 j = i+1;
11529 for(;; j++){
11530 c = pParse->zJson[j];
11531 if( c>='0' && c<='9' ) continue;
11532 if( c=='.' ){
11533 if( pParse->zJson[j-1]=='-' ) return -1;
11534 if( seenDP ) return -1;
11535 seenDP = 1;
11536 continue;
11537 }
11538 if( c=='e' || c=='E' ){
11539 if( pParse->zJson[j-1]<'0' ) return -1;
11540 if( seenE ) return -1;
11541 seenDP = seenE = 1;
11542 c = pParse->zJson[j+1];
11543 if( c=='+' || c=='-' ){
11544 j++;
11545 c = pParse->zJson[j+1];
11546 }
11547 if( c<'0' || c>'9' ) return -1;
11548 continue;
11549 }
11550 break;
11551 }
11552 if( pParse->zJson[j-1]<'0' ) return -1;
11553 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
11554 j - i, &pParse->zJson[i]);
11555 return j;
11556 }else if( c=='}' ){
11557 return -2; /* End of {...} */
11558 }else if( c==']' ){
11559 return -3; /* End of [...] */
11560 }else if( c==0 ){
11561 return 0; /* End of file */
11562 }else{
11563 return -1; /* Syntax error */
11564 }
11565 }
11566
11567 /*
11568 ** Parse a complete JSON string. Return 0 on success or non-zero if there
11569 ** are any errors. If an error occurs, free all memory associated with
11570 ** pParse.
11571 **
11572 ** pParse is uninitialized when this routine is called.
11573 */
11574 static int jsonParse(
11575 JsonParse *pParse, /* Initialize and fill this JsonParse object */
11576 sqlite3_context *pCtx, /* Report errors here */
11577 const char *zJson /* Input JSON text to be parsed */
11578 ){
11579 int i;
11580 memset(pParse, 0, sizeof(*pParse));
11581 if( zJson==0 ) return 1;
11582 pParse->zJson = zJson;
11583 i = jsonParseValue(pParse, 0);
11584 if( pParse->oom ) i = -1;
11585 if( i>0 ){
11586 while( safe_isspace(zJson[i]) ) i++;
11587 if( zJson[i] ) i = -1;
11588 }
11589 if( i<=0 ){
11590 if( pCtx!=0 ){
11591 if( pParse->oom ){
11592 sqlite3_result_error_nomem(pCtx);
11593 }else{
11594 sqlite3_result_error(pCtx, "malformed JSON", -1);
11595 }
11596 }
11597 jsonParseReset(pParse);
11598 return 1;
11599 }
11600 return 0;
11601 }
11602
11603 /* Mark node i of pParse as being a child of iParent. Call recursively
11604 ** to fill in all the descendants of node i.
11605 */
11606 static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
11607 JsonNode *pNode = &pParse->aNode[i];
11608 u32 j;
11609 pParse->aUp[i] = iParent;
11610 switch( pNode->eType ){
11611 case JSON_ARRAY: {
11612 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
11613 jsonParseFillInParentage(pParse, i+j, i);
11614 }
11615 break;
11616 }
11617 case JSON_OBJECT: {
11618 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
11619 pParse->aUp[i+j] = i;
11620 jsonParseFillInParentage(pParse, i+j+1, i);
11621 }
11622 break;
11623 }
11624 default: {
11625 break;
11626 }
11627 }
11628 }
11629
11630 /*
11631 ** Compute the parentage of all nodes in a completed parse.
11632 */
11633 static int jsonParseFindParents(JsonParse *pParse){
11634 u32 *aUp;
11635 assert( pParse->aUp==0 );
11636 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
11637 if( aUp==0 ){
11638 pParse->oom = 1;
11639 return SQLITE_NOMEM;
11640 }
11641 jsonParseFillInParentage(pParse, 0, 0);
11642 return SQLITE_OK;
11643 }
11644
11645 /*
11646 ** Compare the OBJECT label at pNode against zKey,nKey. Return true on
11647 ** a match.
11648 */
11649 static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
11650 if( pNode->jnFlags & JNODE_RAW ){
11651 if( pNode->n!=nKey ) return 0;
11652 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
11653 }else{
11654 if( pNode->n!=nKey+2 ) return 0;
11655 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
11656 }
11657 }
11658
11659 /* forward declaration */
11660 static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
11661
11662 /*
11663 ** Search along zPath to find the node specified. Return a pointer
11664 ** to that node, or NULL if zPath is malformed or if there is no such
11665 ** node.
11666 **
11667 ** If pApnd!=0, then try to append new nodes to complete zPath if it is
11668 ** possible to do so and if no existing node corresponds to zPath. If
11669 ** new nodes are appended *pApnd is set to 1.
11670 */
11671 static JsonNode *jsonLookupStep(
11672 JsonParse *pParse, /* The JSON to search */
11673 u32 iRoot, /* Begin the search at this node */
11674 const char *zPath, /* The path to search */
11675 int *pApnd, /* Append nodes to complete path if not NULL */
11676 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
11677 ){
11678 u32 i, j, nKey;
11679 const char *zKey;
11680 JsonNode *pRoot = &pParse->aNode[iRoot];
11681 if( zPath[0]==0 ) return pRoot;
11682 if( zPath[0]=='.' ){
11683 if( pRoot->eType!=JSON_OBJECT ) return 0;
11684 zPath++;
11685 if( zPath[0]=='"' ){
11686 zKey = zPath + 1;
11687 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
11688 nKey = i-1;
11689 if( zPath[i] ){
11690 i++;
11691 }else{
11692 *pzErr = zPath;
11693 return 0;
11694 }
11695 }else{
11696 zKey = zPath;
11697 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
11698 nKey = i;
11699 }
11700 if( nKey==0 ){
11701 *pzErr = zPath;
11702 return 0;
11703 }
11704 j = 1;
11705 for(;;){
11706 while( j<=pRoot->n ){
11707 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
11708 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
11709 }
11710 j++;
11711 j += jsonNodeSize(&pRoot[j]);
11712 }
11713 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
11714 iRoot += pRoot->u.iAppend;
11715 pRoot = &pParse->aNode[iRoot];
11716 j = 1;
11717 }
11718 if( pApnd ){
11719 u32 iStart, iLabel;
11720 JsonNode *pNode;
11721 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
11722 iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
11723 zPath += i;
11724 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
11725 if( pParse->oom ) return 0;
11726 if( pNode ){
11727 pRoot = &pParse->aNode[iRoot];
11728 pRoot->u.iAppend = iStart - iRoot;
11729 pRoot->jnFlags |= JNODE_APPEND;
11730 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
11731 }
11732 return pNode;
11733 }
11734 }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
11735 if( pRoot->eType!=JSON_ARRAY ) return 0;
11736 i = 0;
11737 j = 1;
11738 while( safe_isdigit(zPath[j]) ){
11739 i = i*10 + zPath[j] - '0';
11740 j++;
11741 }
11742 if( zPath[j]!=']' ){
11743 *pzErr = zPath;
11744 return 0;
11745 }
11746 zPath += j + 1;
11747 j = 1;
11748 for(;;){
11749 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
11750 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
11751 j += jsonNodeSize(&pRoot[j]);
11752 }
11753 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
11754 iRoot += pRoot->u.iAppend;
11755 pRoot = &pParse->aNode[iRoot];
11756 j = 1;
11757 }
11758 if( j<=pRoot->n ){
11759 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
11760 }
11761 if( i==0 && pApnd ){
11762 u32 iStart;
11763 JsonNode *pNode;
11764 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
11765 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
11766 if( pParse->oom ) return 0;
11767 if( pNode ){
11768 pRoot = &pParse->aNode[iRoot];
11769 pRoot->u.iAppend = iStart - iRoot;
11770 pRoot->jnFlags |= JNODE_APPEND;
11771 }
11772 return pNode;
11773 }
11774 }else{
11775 *pzErr = zPath;
11776 }
11777 return 0;
11778 }
11779
11780 /*
11781 ** Append content to pParse that will complete zPath. Return a pointer
11782 ** to the inserted node, or return NULL if the append fails.
11783 */
11784 static JsonNode *jsonLookupAppend(
11785 JsonParse *pParse, /* Append content to the JSON parse */
11786 const char *zPath, /* Description of content to append */
11787 int *pApnd, /* Set this flag to 1 */
11788 const char **pzErr /* Make this point to any syntax error */
11789 ){
11790 *pApnd = 1;
11791 if( zPath[0]==0 ){
11792 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
11793 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
11794 }
11795 if( zPath[0]=='.' ){
11796 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
11797 }else if( strncmp(zPath,"[0]",3)==0 ){
11798 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
11799 }else{
11800 return 0;
11801 }
11802 if( pParse->oom ) return 0;
11803 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
11804 }
11805
11806 /*
11807 ** Return the text of a syntax error message on a JSON path. Space is
11808 ** obtained from sqlite3_malloc().
11809 */
11810 static char *jsonPathSyntaxError(const char *zErr){
11811 return sqlite3_mprintf("JSON path error near '%q'", zErr);
11812 }
11813
11814 /*
11815 ** Do a node lookup using zPath. Return a pointer to the node on success.
11816 ** Return NULL if not found or if there is an error.
11817 **
11818 ** On an error, write an error message into pCtx and increment the
11819 ** pParse->nErr counter.
11820 **
11821 ** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
11822 ** nodes are appended.
11823 */
11824 static JsonNode *jsonLookup(
11825 JsonParse *pParse, /* The JSON to search */
11826 const char *zPath, /* The path to search */
11827 int *pApnd, /* Append nodes to complete path if not NULL */
11828 sqlite3_context *pCtx /* Report errors here, if not NULL */
11829 ){
11830 const char *zErr = 0;
11831 JsonNode *pNode = 0;
11832 char *zMsg;
11833
11834 if( zPath==0 ) return 0;
11835 if( zPath[0]!='$' ){
11836 zErr = zPath;
11837 goto lookup_err;
11838 }
11839 zPath++;
11840 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
11841 if( zErr==0 ) return pNode;
11842
11843 lookup_err:
11844 pParse->nErr++;
11845 assert( zErr!=0 && pCtx!=0 );
11846 zMsg = jsonPathSyntaxError(zErr);
11847 if( zMsg ){
11848 sqlite3_result_error(pCtx, zMsg, -1);
11849 sqlite3_free(zMsg);
11850 }else{
11851 sqlite3_result_error_nomem(pCtx);
11852 }
11853 return 0;
11854 }
11855
11856
11857 /*
11858 ** Report the wrong number of arguments for json_insert(), json_replace()
11859 ** or json_set().
11860 */
11861 static void jsonWrongNumArgs(
11862 sqlite3_context *pCtx,
11863 const char *zFuncName
11864 ){
11865 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
11866 zFuncName);
11867 sqlite3_result_error(pCtx, zMsg, -1);
11868 sqlite3_free(zMsg);
11869 }
11870
11871
11872 /****************************************************************************
11873 ** SQL functions used for testing and debugging
11874 ****************************************************************************/
11875
11876 #ifdef SQLITE_DEBUG
11877 /*
11878 ** The json_parse(JSON) function returns a string which describes
11879 ** a parse of the JSON provided. Or it returns NULL if JSON is not
11880 ** well-formed.
11881 */
11882 static void jsonParseFunc(
11883 sqlite3_context *ctx,
11884 int argc,
11885 sqlite3_value **argv
11886 ){
11887 JsonString s; /* Output string - not real JSON */
11888 JsonParse x; /* The parse */
11889 u32 i;
11890
11891 assert( argc==1 );
11892 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
11893 jsonParseFindParents(&x);
11894 jsonInit(&s, ctx);
11895 for(i=0; i<x.nNode; i++){
11896 const char *zType;
11897 if( x.aNode[i].jnFlags & JNODE_LABEL ){
11898 assert( x.aNode[i].eType==JSON_STRING );
11899 zType = "label";
11900 }else{
11901 zType = jsonType[x.aNode[i].eType];
11902 }
11903 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
11904 i, zType, x.aNode[i].n, x.aUp[i]);
11905 if( x.aNode[i].u.zJContent!=0 ){
11906 jsonAppendRaw(&s, " ", 1);
11907 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
11908 }
11909 jsonAppendRaw(&s, "\n", 1);
11910 }
11911 jsonParseReset(&x);
11912 jsonResult(&s);
11913 }
11914
11915 /*
11916 ** The json_test1(JSON) function return true (1) if the input is JSON
11917 ** text generated by another json function. It returns (0) if the input
11918 ** is not known to be JSON.
11919 */
11920 static void jsonTest1Func(
11921 sqlite3_context *ctx,
11922 int argc,
11923 sqlite3_value **argv
11924 ){
11925 UNUSED_PARAM(argc);
11926 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
11927 }
11928 #endif /* SQLITE_DEBUG */
11929
11930 /****************************************************************************
11931 ** Scalar SQL function implementations
11932 ****************************************************************************/
11933
11934 /*
11935 ** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
11936 ** corresponding to the SQL value input. Mostly this means putting
11937 ** double-quotes around strings and returning the unquoted string "null"
11938 ** when given a NULL input.
11939 */
11940 static void jsonQuoteFunc(
11941 sqlite3_context *ctx,
11942 int argc,
11943 sqlite3_value **argv
11944 ){
11945 JsonString jx;
11946 UNUSED_PARAM(argc);
11947
11948 jsonInit(&jx, ctx);
11949 jsonAppendValue(&jx, argv[0]);
11950 jsonResult(&jx);
11951 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
11952 }
11953
11954 /*
11955 ** Implementation of the json_array(VALUE,...) function. Return a JSON
11956 ** array that contains all values given in arguments. Or if any argument
11957 ** is a BLOB, throw an error.
11958 */
11959 static void jsonArrayFunc(
11960 sqlite3_context *ctx,
11961 int argc,
11962 sqlite3_value **argv
11963 ){
11964 int i;
11965 JsonString jx;
11966
11967 jsonInit(&jx, ctx);
11968 jsonAppendChar(&jx, '[');
11969 for(i=0; i<argc; i++){
11970 jsonAppendSeparator(&jx);
11971 jsonAppendValue(&jx, argv[i]);
11972 }
11973 jsonAppendChar(&jx, ']');
11974 jsonResult(&jx);
11975 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
11976 }
11977
11978
11979 /*
11980 ** json_array_length(JSON)
11981 ** json_array_length(JSON, PATH)
11982 **
11983 ** Return the number of elements in the top-level JSON array.
11984 ** Return 0 if the input is not a well-formed JSON array.
11985 */
11986 static void jsonArrayLengthFunc(
11987 sqlite3_context *ctx,
11988 int argc,
11989 sqlite3_value **argv
11990 ){
11991 JsonParse x; /* The parse */
11992 sqlite3_int64 n = 0;
11993 u32 i;
11994 JsonNode *pNode;
11995
11996 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
11997 assert( x.nNode );
11998 if( argc==2 ){
11999 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
12000 pNode = jsonLookup(&x, zPath, 0, ctx);
12001 }else{
12002 pNode = x.aNode;
12003 }
12004 if( pNode==0 ){
12005 x.nErr = 1;
12006 }else if( pNode->eType==JSON_ARRAY ){
12007 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
12008 for(i=1; i<=pNode->n; n++){
12009 i += jsonNodeSize(&pNode[i]);
12010 }
12011 }
12012 if( x.nErr==0 ) sqlite3_result_int64(ctx, n);
12013 jsonParseReset(&x);
12014 }
12015
12016 /*
12017 ** json_extract(JSON, PATH, ...)
12018 **
12019 ** Return the element described by PATH. Return NULL if there is no
12020 ** PATH element. If there are multiple PATHs, then return a JSON array
12021 ** with the result from each path. Throw an error if the JSON or any PATH
12022 ** is malformed.
12023 */
12024 static void jsonExtractFunc(
12025 sqlite3_context *ctx,
12026 int argc,
12027 sqlite3_value **argv
12028 ){
12029 JsonParse x; /* The parse */
12030 JsonNode *pNode;
12031 const char *zPath;
12032 JsonString jx;
12033 int i;
12034
12035 if( argc<2 ) return;
12036 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
12037 jsonInit(&jx, ctx);
12038 jsonAppendChar(&jx, '[');
12039 for(i=1; i<argc; i++){
12040 zPath = (const char*)sqlite3_value_text(argv[i]);
12041 pNode = jsonLookup(&x, zPath, 0, ctx);
12042 if( x.nErr ) break;
12043 if( argc>2 ){
12044 jsonAppendSeparator(&jx);
12045 if( pNode ){
12046 jsonRenderNode(pNode, &jx, 0);
12047 }else{
12048 jsonAppendRaw(&jx, "null", 4);
12049 }
12050 }else if( pNode ){
12051 jsonReturn(pNode, ctx, 0);
12052 }
12053 }
12054 if( argc>2 && i==argc ){
12055 jsonAppendChar(&jx, ']');
12056 jsonResult(&jx);
12057 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
12058 }
12059 jsonReset(&jx);
12060 jsonParseReset(&x);
12061 }
12062
12063 /*
12064 ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
12065 ** object that contains all name/value given in arguments. Or if any name
12066 ** is not a string or if any value is a BLOB, throw an error.
12067 */
12068 static void jsonObjectFunc(
12069 sqlite3_context *ctx,
12070 int argc,
12071 sqlite3_value **argv
12072 ){
12073 int i;
12074 JsonString jx;
12075 const char *z;
12076 u32 n;
12077
12078 if( argc&1 ){
12079 sqlite3_result_error(ctx, "json_object() requires an even number "
12080 "of arguments", -1);
12081 return;
12082 }
12083 jsonInit(&jx, ctx);
12084 jsonAppendChar(&jx, '{');
12085 for(i=0; i<argc; i+=2){
12086 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
12087 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
12088 jsonReset(&jx);
12089 return;
12090 }
12091 jsonAppendSeparator(&jx);
12092 z = (const char*)sqlite3_value_text(argv[i]);
12093 n = (u32)sqlite3_value_bytes(argv[i]);
12094 jsonAppendString(&jx, z, n);
12095 jsonAppendChar(&jx, ':');
12096 jsonAppendValue(&jx, argv[i+1]);
12097 }
12098 jsonAppendChar(&jx, '}');
12099 jsonResult(&jx);
12100 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
12101 }
12102
12103
12104 /*
12105 ** json_remove(JSON, PATH, ...)
12106 **
12107 ** Remove the named elements from JSON and return the result. malformed
12108 ** JSON or PATH arguments result in an error.
12109 */
12110 static void jsonRemoveFunc(
12111 sqlite3_context *ctx,
12112 int argc,
12113 sqlite3_value **argv
12114 ){
12115 JsonParse x; /* The parse */
12116 JsonNode *pNode;
12117 const char *zPath;
12118 u32 i;
12119
12120 if( argc<1 ) return;
12121 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
12122 assert( x.nNode );
12123 for(i=1; i<(u32)argc; i++){
12124 zPath = (const char*)sqlite3_value_text(argv[i]);
12125 if( zPath==0 ) goto remove_done;
12126 pNode = jsonLookup(&x, zPath, 0, ctx);
12127 if( x.nErr ) goto remove_done;
12128 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
12129 }
12130 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
12131 jsonReturnJson(x.aNode, ctx, 0);
12132 }
12133 remove_done:
12134 jsonParseReset(&x);
12135 }
12136
12137 /*
12138 ** json_replace(JSON, PATH, VALUE, ...)
12139 **
12140 ** Replace the value at PATH with VALUE. If PATH does not already exist,
12141 ** this routine is a no-op. If JSON or PATH is malformed, throw an error.
12142 */
12143 static void jsonReplaceFunc(
12144 sqlite3_context *ctx,
12145 int argc,
12146 sqlite3_value **argv
12147 ){
12148 JsonParse x; /* The parse */
12149 JsonNode *pNode;
12150 const char *zPath;
12151 u32 i;
12152
12153 if( argc<1 ) return;
12154 if( (argc&1)==0 ) {
12155 jsonWrongNumArgs(ctx, "replace");
12156 return;
12157 }
12158 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
12159 assert( x.nNode );
12160 for(i=1; i<(u32)argc; i+=2){
12161 zPath = (const char*)sqlite3_value_text(argv[i]);
12162 pNode = jsonLookup(&x, zPath, 0, ctx);
12163 if( x.nErr ) goto replace_err;
12164 if( pNode ){
12165 pNode->jnFlags |= (u8)JNODE_REPLACE;
12166 pNode->iVal = (u8)(i+1);
12167 }
12168 }
12169 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
12170 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
12171 }else{
12172 jsonReturnJson(x.aNode, ctx, argv);
12173 }
12174 replace_err:
12175 jsonParseReset(&x);
12176 }
12177
12178 /*
12179 ** json_set(JSON, PATH, VALUE, ...)
12180 **
12181 ** Set the value at PATH to VALUE. Create the PATH if it does not already
12182 ** exist. Overwrite existing values that do exist.
12183 ** If JSON or PATH is malformed, throw an error.
12184 **
12185 ** json_insert(JSON, PATH, VALUE, ...)
12186 **
12187 ** Create PATH and initialize it to VALUE. If PATH already exists, this
12188 ** routine is a no-op. If JSON or PATH is malformed, throw an error.
12189 */
12190 static void jsonSetFunc(
12191 sqlite3_context *ctx,
12192 int argc,
12193 sqlite3_value **argv
12194 ){
12195 JsonParse x; /* The parse */
12196 JsonNode *pNode;
12197 const char *zPath;
12198 u32 i;
12199 int bApnd;
12200 int bIsSet = *(int*)sqlite3_user_data(ctx);
12201
12202 if( argc<1 ) return;
12203 if( (argc&1)==0 ) {
12204 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
12205 return;
12206 }
12207 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
12208 assert( x.nNode );
12209 for(i=1; i<(u32)argc; i+=2){
12210 zPath = (const char*)sqlite3_value_text(argv[i]);
12211 bApnd = 0;
12212 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
12213 if( x.oom ){
12214 sqlite3_result_error_nomem(ctx);
12215 goto jsonSetDone;
12216 }else if( x.nErr ){
12217 goto jsonSetDone;
12218 }else if( pNode && (bApnd || bIsSet) ){
12219 pNode->jnFlags |= (u8)JNODE_REPLACE;
12220 pNode->iVal = (u8)(i+1);
12221 }
12222 }
12223 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
12224 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
12225 }else{
12226 jsonReturnJson(x.aNode, ctx, argv);
12227 }
12228 jsonSetDone:
12229 jsonParseReset(&x);
12230 }
12231
12232 /*
12233 ** json_type(JSON)
12234 ** json_type(JSON, PATH)
12235 **
12236 ** Return the top-level "type" of a JSON string. Throw an error if
12237 ** either the JSON or PATH inputs are not well-formed.
12238 */
12239 static void jsonTypeFunc(
12240 sqlite3_context *ctx,
12241 int argc,
12242 sqlite3_value **argv
12243 ){
12244 JsonParse x; /* The parse */
12245 const char *zPath;
12246 JsonNode *pNode;
12247
12248 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
12249 assert( x.nNode );
12250 if( argc==2 ){
12251 zPath = (const char*)sqlite3_value_text(argv[1]);
12252 pNode = jsonLookup(&x, zPath, 0, ctx);
12253 }else{
12254 pNode = x.aNode;
12255 }
12256 if( pNode ){
12257 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
12258 }
12259 jsonParseReset(&x);
12260 }
12261
12262 /*
12263 ** json_valid(JSON)
12264 **
12265 ** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
12266 ** Return 0 otherwise.
12267 */
12268 static void jsonValidFunc(
12269 sqlite3_context *ctx,
12270 int argc,
12271 sqlite3_value **argv
12272 ){
12273 JsonParse x; /* The parse */
12274 int rc = 0;
12275
12276 UNUSED_PARAM(argc);
12277 if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 ){
12278 rc = 1;
12279 }
12280 jsonParseReset(&x);
12281 sqlite3_result_int(ctx, rc);
12282 }
12283
12284
12285 /****************************************************************************
12286 ** Aggregate SQL function implementations
12287 ****************************************************************************/
12288 /*
12289 ** json_group_array(VALUE)
12290 **
12291 ** Return a JSON array composed of all values in the aggregate.
12292 */
12293 static void jsonArrayStep(
12294 sqlite3_context *ctx,
12295 int argc,
12296 sqlite3_value **argv
12297 ){
12298 JsonString *pStr;
12299 UNUSED_PARAM(argc);
12300 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
12301 if( pStr ){
12302 if( pStr->zBuf==0 ){
12303 jsonInit(pStr, ctx);
12304 jsonAppendChar(pStr, '[');
12305 }else{
12306 jsonAppendChar(pStr, ',');
12307 pStr->pCtx = ctx;
12308 }
12309 jsonAppendValue(pStr, argv[0]);
12310 }
12311 }
12312 static void jsonArrayFinal(sqlite3_context *ctx){
12313 JsonString *pStr;
12314 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
12315 if( pStr ){
12316 pStr->pCtx = ctx;
12317 jsonAppendChar(pStr, ']');
12318 if( pStr->bErr ){
12319 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
12320 assert( pStr->bStatic );
12321 }else{
12322 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
12323 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
12324 pStr->bStatic = 1;
12325 }
12326 }else{
12327 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
12328 }
12329 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
12330 }
12331
12332 /*
12333 ** json_group_obj(NAME,VALUE)
12334 **
12335 ** Return a JSON object composed of all names and values in the aggregate.
12336 */
12337 static void jsonObjectStep(
12338 sqlite3_context *ctx,
12339 int argc,
12340 sqlite3_value **argv
12341 ){
12342 JsonString *pStr;
12343 const char *z;
12344 u32 n;
12345 UNUSED_PARAM(argc);
12346 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
12347 if( pStr ){
12348 if( pStr->zBuf==0 ){
12349 jsonInit(pStr, ctx);
12350 jsonAppendChar(pStr, '{');
12351 }else{
12352 jsonAppendChar(pStr, ',');
12353 pStr->pCtx = ctx;
12354 }
12355 z = (const char*)sqlite3_value_text(argv[0]);
12356 n = (u32)sqlite3_value_bytes(argv[0]);
12357 jsonAppendString(pStr, z, n);
12358 jsonAppendChar(pStr, ':');
12359 jsonAppendValue(pStr, argv[1]);
12360 }
12361 }
12362 static void jsonObjectFinal(sqlite3_context *ctx){
12363 JsonString *pStr;
12364 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
12365 if( pStr ){
12366 jsonAppendChar(pStr, '}');
12367 if( pStr->bErr ){
12368 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
12369 assert( pStr->bStatic );
12370 }else{
12371 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
12372 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
12373 pStr->bStatic = 1;
12374 }
12375 }else{
12376 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
12377 }
12378 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
12379 }
12380
12381
12382 #ifndef SQLITE_OMIT_VIRTUALTABLE
12383 /****************************************************************************
12384 ** The json_each virtual table
12385 ****************************************************************************/
12386 typedef struct JsonEachCursor JsonEachCursor;
12387 struct JsonEachCursor {
12388 sqlite3_vtab_cursor base; /* Base class - must be first */
12389 u32 iRowid; /* The rowid */
12390 u32 iBegin; /* The first node of the scan */
12391 u32 i; /* Index in sParse.aNode[] of current row */
12392 u32 iEnd; /* EOF when i equals or exceeds this value */
12393 u8 eType; /* Type of top-level element */
12394 u8 bRecursive; /* True for json_tree(). False for json_each() */
12395 char *zJson; /* Input JSON */
12396 char *zRoot; /* Path by which to filter zJson */
12397 JsonParse sParse; /* Parse of the input JSON */
12398 };
12399
12400 /* Constructor for the json_each virtual table */
12401 static int jsonEachConnect(
12402 sqlite3 *db,
12403 void *pAux,
12404 int argc, const char *const*argv,
12405 sqlite3_vtab **ppVtab,
12406 char **pzErr
12407 ){
12408 sqlite3_vtab *pNew;
12409 int rc;
12410
12411 /* Column numbers */
12412 #define JEACH_KEY 0
12413 #define JEACH_VALUE 1
12414 #define JEACH_TYPE 2
12415 #define JEACH_ATOM 3
12416 #define JEACH_ID 4
12417 #define JEACH_PARENT 5
12418 #define JEACH_FULLKEY 6
12419 #define JEACH_PATH 7
12420 #define JEACH_JSON 8
12421 #define JEACH_ROOT 9
12422
12423 UNUSED_PARAM(pzErr);
12424 UNUSED_PARAM(argv);
12425 UNUSED_PARAM(argc);
12426 UNUSED_PARAM(pAux);
12427 rc = sqlite3_declare_vtab(db,
12428 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
12429 "json HIDDEN,root HIDDEN)");
12430 if( rc==SQLITE_OK ){
12431 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
12432 if( pNew==0 ) return SQLITE_NOMEM;
12433 memset(pNew, 0, sizeof(*pNew));
12434 }
12435 return rc;
12436 }
12437
12438 /* destructor for json_each virtual table */
12439 static int jsonEachDisconnect(sqlite3_vtab *pVtab){
12440 sqlite3_free(pVtab);
12441 return SQLITE_OK;
12442 }
12443
12444 /* constructor for a JsonEachCursor object for json_each(). */
12445 static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
12446 JsonEachCursor *pCur;
12447
12448 UNUSED_PARAM(p);
12449 pCur = sqlite3_malloc( sizeof(*pCur) );
12450 if( pCur==0 ) return SQLITE_NOMEM;
12451 memset(pCur, 0, sizeof(*pCur));
12452 *ppCursor = &pCur->base;
12453 return SQLITE_OK;
12454 }
12455
12456 /* constructor for a JsonEachCursor object for json_tree(). */
12457 static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
12458 int rc = jsonEachOpenEach(p, ppCursor);
12459 if( rc==SQLITE_OK ){
12460 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
12461 pCur->bRecursive = 1;
12462 }
12463 return rc;
12464 }
12465
12466 /* Reset a JsonEachCursor back to its original state. Free any memory
12467 ** held. */
12468 static void jsonEachCursorReset(JsonEachCursor *p){
12469 sqlite3_free(p->zJson);
12470 sqlite3_free(p->zRoot);
12471 jsonParseReset(&p->sParse);
12472 p->iRowid = 0;
12473 p->i = 0;
12474 p->iEnd = 0;
12475 p->eType = 0;
12476 p->zJson = 0;
12477 p->zRoot = 0;
12478 }
12479
12480 /* Destructor for a jsonEachCursor object */
12481 static int jsonEachClose(sqlite3_vtab_cursor *cur){
12482 JsonEachCursor *p = (JsonEachCursor*)cur;
12483 jsonEachCursorReset(p);
12484 sqlite3_free(cur);
12485 return SQLITE_OK;
12486 }
12487
12488 /* Return TRUE if the jsonEachCursor object has been advanced off the end
12489 ** of the JSON object */
12490 static int jsonEachEof(sqlite3_vtab_cursor *cur){
12491 JsonEachCursor *p = (JsonEachCursor*)cur;
12492 return p->i >= p->iEnd;
12493 }
12494
12495 /* Advance the cursor to the next element for json_tree() */
12496 static int jsonEachNext(sqlite3_vtab_cursor *cur){
12497 JsonEachCursor *p = (JsonEachCursor*)cur;
12498 if( p->bRecursive ){
12499 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
12500 p->i++;
12501 p->iRowid++;
12502 if( p->i<p->iEnd ){
12503 u32 iUp = p->sParse.aUp[p->i];
12504 JsonNode *pUp = &p->sParse.aNode[iUp];
12505 p->eType = pUp->eType;
12506 if( pUp->eType==JSON_ARRAY ){
12507 if( iUp==p->i-1 ){
12508 pUp->u.iKey = 0;
12509 }else{
12510 pUp->u.iKey++;
12511 }
12512 }
12513 }
12514 }else{
12515 switch( p->eType ){
12516 case JSON_ARRAY: {
12517 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
12518 p->iRowid++;
12519 break;
12520 }
12521 case JSON_OBJECT: {
12522 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
12523 p->iRowid++;
12524 break;
12525 }
12526 default: {
12527 p->i = p->iEnd;
12528 break;
12529 }
12530 }
12531 }
12532 return SQLITE_OK;
12533 }
12534
12535 /* Append the name of the path for element i to pStr
12536 */
12537 static void jsonEachComputePath(
12538 JsonEachCursor *p, /* The cursor */
12539 JsonString *pStr, /* Write the path here */
12540 u32 i /* Path to this element */
12541 ){
12542 JsonNode *pNode, *pUp;
12543 u32 iUp;
12544 if( i==0 ){
12545 jsonAppendChar(pStr, '$');
12546 return;
12547 }
12548 iUp = p->sParse.aUp[i];
12549 jsonEachComputePath(p, pStr, iUp);
12550 pNode = &p->sParse.aNode[i];
12551 pUp = &p->sParse.aNode[iUp];
12552 if( pUp->eType==JSON_ARRAY ){
12553 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
12554 }else{
12555 assert( pUp->eType==JSON_OBJECT );
12556 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
12557 assert( pNode->eType==JSON_STRING );
12558 assert( pNode->jnFlags & JNODE_LABEL );
12559 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
12560 }
12561 }
12562
12563 /* Return the value of a column */
12564 static int jsonEachColumn(
12565 sqlite3_vtab_cursor *cur, /* The cursor */
12566 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
12567 int i /* Which column to return */
12568 ){
12569 JsonEachCursor *p = (JsonEachCursor*)cur;
12570 JsonNode *pThis = &p->sParse.aNode[p->i];
12571 switch( i ){
12572 case JEACH_KEY: {
12573 if( p->i==0 ) break;
12574 if( p->eType==JSON_OBJECT ){
12575 jsonReturn(pThis, ctx, 0);
12576 }else if( p->eType==JSON_ARRAY ){
12577 u32 iKey;
12578 if( p->bRecursive ){
12579 if( p->iRowid==0 ) break;
12580 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
12581 }else{
12582 iKey = p->iRowid;
12583 }
12584 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
12585 }
12586 break;
12587 }
12588 case JEACH_VALUE: {
12589 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
12590 jsonReturn(pThis, ctx, 0);
12591 break;
12592 }
12593 case JEACH_TYPE: {
12594 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
12595 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
12596 break;
12597 }
12598 case JEACH_ATOM: {
12599 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
12600 if( pThis->eType>=JSON_ARRAY ) break;
12601 jsonReturn(pThis, ctx, 0);
12602 break;
12603 }
12604 case JEACH_ID: {
12605 sqlite3_result_int64(ctx,
12606 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
12607 break;
12608 }
12609 case JEACH_PARENT: {
12610 if( p->i>p->iBegin && p->bRecursive ){
12611 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
12612 }
12613 break;
12614 }
12615 case JEACH_FULLKEY: {
12616 JsonString x;
12617 jsonInit(&x, ctx);
12618 if( p->bRecursive ){
12619 jsonEachComputePath(p, &x, p->i);
12620 }else{
12621 if( p->zRoot ){
12622 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
12623 }else{
12624 jsonAppendChar(&x, '$');
12625 }
12626 if( p->eType==JSON_ARRAY ){
12627 jsonPrintf(30, &x, "[%d]", p->iRowid);
12628 }else{
12629 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
12630 }
12631 }
12632 jsonResult(&x);
12633 break;
12634 }
12635 case JEACH_PATH: {
12636 if( p->bRecursive ){
12637 JsonString x;
12638 jsonInit(&x, ctx);
12639 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
12640 jsonResult(&x);
12641 break;
12642 }
12643 /* For json_each() path and root are the same so fall through
12644 ** into the root case */
12645 }
12646 default: {
12647 const char *zRoot = p->zRoot;
12648 if( zRoot==0 ) zRoot = "$";
12649 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
12650 break;
12651 }
12652 case JEACH_JSON: {
12653 assert( i==JEACH_JSON );
12654 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
12655 break;
12656 }
12657 }
12658 return SQLITE_OK;
12659 }
12660
12661 /* Return the current rowid value */
12662 static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
12663 JsonEachCursor *p = (JsonEachCursor*)cur;
12664 *pRowid = p->iRowid;
12665 return SQLITE_OK;
12666 }
12667
12668 /* The query strategy is to look for an equality constraint on the json
12669 ** column. Without such a constraint, the table cannot operate. idxNum is
12670 ** 1 if the constraint is found, 3 if the constraint and zRoot are found,
12671 ** and 0 otherwise.
12672 */
12673 static int jsonEachBestIndex(
12674 sqlite3_vtab *tab,
12675 sqlite3_index_info *pIdxInfo
12676 ){
12677 int i;
12678 int jsonIdx = -1;
12679 int rootIdx = -1;
12680 const struct sqlite3_index_constraint *pConstraint;
12681
12682 UNUSED_PARAM(tab);
12683 pConstraint = pIdxInfo->aConstraint;
12684 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
12685 if( pConstraint->usable==0 ) continue;
12686 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
12687 switch( pConstraint->iColumn ){
12688 case JEACH_JSON: jsonIdx = i; break;
12689 case JEACH_ROOT: rootIdx = i; break;
12690 default: /* no-op */ break;
12691 }
12692 }
12693 if( jsonIdx<0 ){
12694 pIdxInfo->idxNum = 0;
12695 pIdxInfo->estimatedCost = 1e99;
12696 }else{
12697 pIdxInfo->estimatedCost = 1.0;
12698 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
12699 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
12700 if( rootIdx<0 ){
12701 pIdxInfo->idxNum = 1;
12702 }else{
12703 pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
12704 pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
12705 pIdxInfo->idxNum = 3;
12706 }
12707 }
12708 return SQLITE_OK;
12709 }
12710
12711 /* Start a search on a new JSON string */
12712 static int jsonEachFilter(
12713 sqlite3_vtab_cursor *cur,
12714 int idxNum, const char *idxStr,
12715 int argc, sqlite3_value **argv
12716 ){
12717 JsonEachCursor *p = (JsonEachCursor*)cur;
12718 const char *z;
12719 const char *zRoot = 0;
12720 sqlite3_int64 n;
12721
12722 UNUSED_PARAM(idxStr);
12723 UNUSED_PARAM(argc);
12724 jsonEachCursorReset(p);
12725 if( idxNum==0 ) return SQLITE_OK;
12726 z = (const char*)sqlite3_value_text(argv[0]);
12727 if( z==0 ) return SQLITE_OK;
12728 n = sqlite3_value_bytes(argv[0]);
12729 p->zJson = sqlite3_malloc64( n+1 );
12730 if( p->zJson==0 ) return SQLITE_NOMEM;
12731 memcpy(p->zJson, z, (size_t)n+1);
12732 if( jsonParse(&p->sParse, 0, p->zJson) ){
12733 int rc = SQLITE_NOMEM;
12734 if( p->sParse.oom==0 ){
12735 sqlite3_free(cur->pVtab->zErrMsg);
12736 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
12737 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
12738 }
12739 jsonEachCursorReset(p);
12740 return rc;
12741 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
12742 jsonEachCursorReset(p);
12743 return SQLITE_NOMEM;
12744 }else{
12745 JsonNode *pNode = 0;
12746 if( idxNum==3 ){
12747 const char *zErr = 0;
12748 zRoot = (const char*)sqlite3_value_text(argv[1]);
12749 if( zRoot==0 ) return SQLITE_OK;
12750 n = sqlite3_value_bytes(argv[1]);
12751 p->zRoot = sqlite3_malloc64( n+1 );
12752 if( p->zRoot==0 ) return SQLITE_NOMEM;
12753 memcpy(p->zRoot, zRoot, (size_t)n+1);
12754 if( zRoot[0]!='$' ){
12755 zErr = zRoot;
12756 }else{
12757 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
12758 }
12759 if( zErr ){
12760 sqlite3_free(cur->pVtab->zErrMsg);
12761 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
12762 jsonEachCursorReset(p);
12763 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
12764 }else if( pNode==0 ){
12765 return SQLITE_OK;
12766 }
12767 }else{
12768 pNode = p->sParse.aNode;
12769 }
12770 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
12771 p->eType = pNode->eType;
12772 if( p->eType>=JSON_ARRAY ){
12773 pNode->u.iKey = 0;
12774 p->iEnd = p->i + pNode->n + 1;
12775 if( p->bRecursive ){
12776 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
12777 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
12778 p->i--;
12779 }
12780 }else{
12781 p->i++;
12782 }
12783 }else{
12784 p->iEnd = p->i+1;
12785 }
12786 }
12787 return SQLITE_OK;
12788 }
12789
12790 /* The methods of the json_each virtual table */
12791 static sqlite3_module jsonEachModule = {
12792 0, /* iVersion */
12793 0, /* xCreate */
12794 jsonEachConnect, /* xConnect */
12795 jsonEachBestIndex, /* xBestIndex */
12796 jsonEachDisconnect, /* xDisconnect */
12797 0, /* xDestroy */
12798 jsonEachOpenEach, /* xOpen - open a cursor */
12799 jsonEachClose, /* xClose - close a cursor */
12800 jsonEachFilter, /* xFilter - configure scan constraints */
12801 jsonEachNext, /* xNext - advance a cursor */
12802 jsonEachEof, /* xEof - check for end of scan */
12803 jsonEachColumn, /* xColumn - read data */
12804 jsonEachRowid, /* xRowid - read data */
12805 0, /* xUpdate */
12806 0, /* xBegin */
12807 0, /* xSync */
12808 0, /* xCommit */
12809 0, /* xRollback */
12810 0, /* xFindMethod */
12811 0, /* xRename */
12812 0, /* xSavepoint */
12813 0, /* xRelease */
12814 0 /* xRollbackTo */
12815 };
12816
12817 /* The methods of the json_tree virtual table. */
12818 static sqlite3_module jsonTreeModule = {
12819 0, /* iVersion */
12820 0, /* xCreate */
12821 jsonEachConnect, /* xConnect */
12822 jsonEachBestIndex, /* xBestIndex */
12823 jsonEachDisconnect, /* xDisconnect */
12824 0, /* xDestroy */
12825 jsonEachOpenTree, /* xOpen - open a cursor */
12826 jsonEachClose, /* xClose - close a cursor */
12827 jsonEachFilter, /* xFilter - configure scan constraints */
12828 jsonEachNext, /* xNext - advance a cursor */
12829 jsonEachEof, /* xEof - check for end of scan */
12830 jsonEachColumn, /* xColumn - read data */
12831 jsonEachRowid, /* xRowid - read data */
12832 0, /* xUpdate */
12833 0, /* xBegin */
12834 0, /* xSync */
12835 0, /* xCommit */
12836 0, /* xRollback */
12837 0, /* xFindMethod */
12838 0, /* xRename */
12839 0, /* xSavepoint */
12840 0, /* xRelease */
12841 0 /* xRollbackTo */
12842 };
12843 #endif /* SQLITE_OMIT_VIRTUALTABLE */
12844
12845 /****************************************************************************
12846 ** The following routines are the only publically visible identifiers in this
12847 ** file. Call the following routines in order to register the various SQL
12848 ** functions and the virtual table implemented by this file.
12849 ****************************************************************************/
12850
12851 SQLITE_PRIVATE int sqlite3Json1Init(sqlite3 *db){
12852 int rc = SQLITE_OK;
12853 unsigned int i;
12854 static const struct {
12855 const char *zName;
12856 int nArg;
12857 int flag;
12858 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
12859 } aFunc[] = {
12860 { "json", 1, 0, jsonRemoveFunc },
12861 { "json_array", -1, 0, jsonArrayFunc },
12862 { "json_array_length", 1, 0, jsonArrayLengthFunc },
12863 { "json_array_length", 2, 0, jsonArrayLengthFunc },
12864 { "json_extract", -1, 0, jsonExtractFunc },
12865 { "json_insert", -1, 0, jsonSetFunc },
12866 { "json_object", -1, 0, jsonObjectFunc },
12867 { "json_quote", 1, 0, jsonQuoteFunc },
12868 { "json_remove", -1, 0, jsonRemoveFunc },
12869 { "json_replace", -1, 0, jsonReplaceFunc },
12870 { "json_set", -1, 1, jsonSetFunc },
12871 { "json_type", 1, 0, jsonTypeFunc },
12872 { "json_type", 2, 0, jsonTypeFunc },
12873 { "json_valid", 1, 0, jsonValidFunc },
12874
12875 #if SQLITE_DEBUG
12876 /* DEBUG and TESTING functions */
12877 { "json_parse", 1, 0, jsonParseFunc },
12878 { "json_test1", 1, 0, jsonTest1Func },
12879 #endif
12880 };
12881 static const struct {
12882 const char *zName;
12883 int nArg;
12884 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
12885 void (*xFinal)(sqlite3_context*);
12886 } aAgg[] = {
12887 { "json_group_array", 1, jsonArrayStep, jsonArrayFinal },
12888 { "json_group_object", 2, jsonObjectStep, jsonObjectFinal },
12889 };
12890 #ifndef SQLITE_OMIT_VIRTUALTABLE
12891 static const struct {
12892 const char *zName;
12893 sqlite3_module *pModule;
12894 } aMod[] = {
12895 { "json_each", &jsonEachModule },
12896 { "json_tree", &jsonTreeModule },
12897 };
12898 #endif
12899 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
12900 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
12901 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
12902 (void*)&aFunc[i].flag,
12903 aFunc[i].xFunc, 0, 0);
12904 }
12905 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
12906 rc = sqlite3_create_function(db, aAgg[i].zName, aAgg[i].nArg,
12907 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
12908 0, aAgg[i].xStep, aAgg[i].xFinal);
12909 }
12910 #ifndef SQLITE_OMIT_VIRTUALTABLE
12911 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
12912 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
12913 }
12914 #endif
12915 return rc;
12916 }
12917
12918
12919 #ifndef SQLITE_CORE
12920 #ifdef _WIN32
12921 __declspec(dllexport)
12922 #endif
12923 SQLITE_API int sqlite3_json_init(
12924 sqlite3 *db,
12925 char **pzErrMsg,
12926 const sqlite3_api_routines *pApi
12927 ){
12928 SQLITE_EXTENSION_INIT2(pApi);
12929 (void)pzErrMsg; /* Unused parameter */
12930 return sqlite3Json1Init(db);
12931 }
12932 #endif
12933 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */
12934
12935 /************** End of json1.c ***********************************************/
12936
12937 /* Chain include. */
12938 #include "sqlite3.09.c"
OLDNEW
« no previous file with comments | « third_party/sqlite/amalgamation/sqlite3.07.c ('k') | third_party/sqlite/amalgamation/sqlite3.09.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698