Fossil SCM

Merge in latest changes from trunk.

andybradford 2015-07-25 17:02 pending-review merge
Commit ee463a39be9ed5037ebc74229689443cfbe93043
+4 -3
--- src/db.c
+++ src/db.c
@@ -1974,12 +1974,13 @@
19741974
** the user about the conflict */
19751975
fossil_warning(
19761976
"setting %s has both versioned and non-versioned values: using "
19771977
"versioned value from file .fossil-settings/%s (to silence this "
19781978
"warning, either create an empty file named "
1979
- ".fossil-settings/%s.no-warn or delete the non-versioned setting "
1980
- " with \"fossil unset %s\")", zName, zName, zName, zName
1979
+ ".fossil-settings/%s.no-warn in the check-out root, "
1980
+ "or delete the non-versioned setting "
1981
+ "with \"fossil unset %s\")", zName, zName, zName, zName
19811982
);
19821983
}
19831984
/* Prefer the versioned setting */
19841985
return ( zVersionedSetting!=0 ) ? zVersionedSetting : zNonVersionedSetting;
19851986
}
@@ -2462,11 +2463,11 @@
24622463
** The "settings" command with no arguments lists all properties and their
24632464
** values. With just a property name it shows the value of that property.
24642465
** With a value argument it changes the property for the current repository.
24652466
**
24662467
** Settings marked as versionable are overridden by the contents of the
2467
-** file named .fossil-settings/PROPERTY in the checked out files, if that
2468
+** file named .fossil-settings/PROPERTY in the check-out root, if that
24682469
** file exists.
24692470
**
24702471
** The "unset" command clears a property setting.
24712472
**
24722473
**
24732474
--- src/db.c
+++ src/db.c
@@ -1974,12 +1974,13 @@
1974 ** the user about the conflict */
1975 fossil_warning(
1976 "setting %s has both versioned and non-versioned values: using "
1977 "versioned value from file .fossil-settings/%s (to silence this "
1978 "warning, either create an empty file named "
1979 ".fossil-settings/%s.no-warn or delete the non-versioned setting "
1980 " with \"fossil unset %s\")", zName, zName, zName, zName
 
1981 );
1982 }
1983 /* Prefer the versioned setting */
1984 return ( zVersionedSetting!=0 ) ? zVersionedSetting : zNonVersionedSetting;
1985 }
@@ -2462,11 +2463,11 @@
2462 ** The "settings" command with no arguments lists all properties and their
2463 ** values. With just a property name it shows the value of that property.
2464 ** With a value argument it changes the property for the current repository.
2465 **
2466 ** Settings marked as versionable are overridden by the contents of the
2467 ** file named .fossil-settings/PROPERTY in the checked out files, if that
2468 ** file exists.
2469 **
2470 ** The "unset" command clears a property setting.
2471 **
2472 **
2473
--- src/db.c
+++ src/db.c
@@ -1974,12 +1974,13 @@
1974 ** the user about the conflict */
1975 fossil_warning(
1976 "setting %s has both versioned and non-versioned values: using "
1977 "versioned value from file .fossil-settings/%s (to silence this "
1978 "warning, either create an empty file named "
1979 ".fossil-settings/%s.no-warn in the check-out root, "
1980 "or delete the non-versioned setting "
1981 "with \"fossil unset %s\")", zName, zName, zName, zName
1982 );
1983 }
1984 /* Prefer the versioned setting */
1985 return ( zVersionedSetting!=0 ) ? zVersionedSetting : zNonVersionedSetting;
1986 }
@@ -2462,11 +2463,11 @@
2463 ** The "settings" command with no arguments lists all properties and their
2464 ** values. With just a property name it shows the value of that property.
2465 ** With a value argument it changes the property for the current repository.
2466 **
2467 ** Settings marked as versionable are overridden by the contents of the
2468 ** file named .fossil-settings/PROPERTY in the check-out root, if that
2469 ** file exists.
2470 **
2471 ** The "unset" command clears a property setting.
2472 **
2473 **
2474
+7 -7
--- src/linenoise.c
+++ src/linenoise.c
@@ -176,24 +176,24 @@
176176
static void refreshLine(struct linenoiseState *l);
177177
178178
/* Debugging macro. */
179179
#if 0
180180
FILE *lndebug_fp = NULL;
181
-#define lndebug(...) \
181
+#define lndebug(fmt, arg1) \
182182
do { \
183183
if (lndebug_fp == NULL) { \
184184
lndebug_fp = fopen("/tmp/lndebug.txt","a"); \
185185
fprintf(lndebug_fp, \
186186
"[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \
187187
(int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \
188188
(int)l->maxrows,old_rows); \
189189
} \
190
- fprintf(lndebug_fp, ", " __VA_ARGS__); \
190
+ fprintf(lndebug_fp, ", " fmt, arg1); \
191191
fflush(lndebug_fp); \
192192
} while (0)
193193
#else
194
-#define lndebug(fmt, ...)
194
+#define lndebug(fmt, arg1)
195195
#endif
196196
197197
/* ======================= Low level terminal handling ====================== */
198198
199199
/* Set if to use or not the multi line mode. */
@@ -526,17 +526,17 @@
526526
abAppend(&ab,seq,strlen(seq));
527527
}
528528
529529
/* Now for every row clear it, go up. */
530530
for (j = 0; j < old_rows-1; j++) {
531
- lndebug("clear+up");
531
+ lndebug("clear+up", 0);
532532
snprintf(seq,64,"\r\x1b[0K\x1b[1A");
533533
abAppend(&ab,seq,strlen(seq));
534534
}
535535
536536
/* Clean the top line. */
537
- lndebug("clear");
537
+ lndebug("clear", 0);
538538
snprintf(seq,64,"\r\x1b[0K");
539539
abAppend(&ab,seq,strlen(seq));
540540
541541
/* Write the prompt and the current buffer content */
542542
abAppend(&ab,l->prompt,strlen(l->prompt));
@@ -546,11 +546,11 @@
546546
* emit a newline and move the prompt to the first column. */
547547
if (l->pos &&
548548
l->pos == l->len &&
549549
(l->pos+plen) % l->cols == 0)
550550
{
551
- lndebug("<newline>");
551
+ lndebug("<newline>", 0);
552552
abAppend(&ab,"\n",1);
553553
snprintf(seq,64,"\r");
554554
abAppend(&ab,seq,strlen(seq));
555555
rows++;
556556
if (rows > (int)l->maxrows) l->maxrows = rows;
@@ -574,11 +574,11 @@
574574
snprintf(seq,64,"\r\x1b[%dC", col);
575575
else
576576
snprintf(seq,64,"\r");
577577
abAppend(&ab,seq,strlen(seq));
578578
579
- lndebug("\n");
579
+ lndebug("\n", 0);
580580
l->oldpos = l->pos;
581581
582582
if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
583583
abFree(&ab);
584584
}
585585
--- src/linenoise.c
+++ src/linenoise.c
@@ -176,24 +176,24 @@
176 static void refreshLine(struct linenoiseState *l);
177
178 /* Debugging macro. */
179 #if 0
180 FILE *lndebug_fp = NULL;
181 #define lndebug(...) \
182 do { \
183 if (lndebug_fp == NULL) { \
184 lndebug_fp = fopen("/tmp/lndebug.txt","a"); \
185 fprintf(lndebug_fp, \
186 "[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \
187 (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \
188 (int)l->maxrows,old_rows); \
189 } \
190 fprintf(lndebug_fp, ", " __VA_ARGS__); \
191 fflush(lndebug_fp); \
192 } while (0)
193 #else
194 #define lndebug(fmt, ...)
195 #endif
196
197 /* ======================= Low level terminal handling ====================== */
198
199 /* Set if to use or not the multi line mode. */
@@ -526,17 +526,17 @@
526 abAppend(&ab,seq,strlen(seq));
527 }
528
529 /* Now for every row clear it, go up. */
530 for (j = 0; j < old_rows-1; j++) {
531 lndebug("clear+up");
532 snprintf(seq,64,"\r\x1b[0K\x1b[1A");
533 abAppend(&ab,seq,strlen(seq));
534 }
535
536 /* Clean the top line. */
537 lndebug("clear");
538 snprintf(seq,64,"\r\x1b[0K");
539 abAppend(&ab,seq,strlen(seq));
540
541 /* Write the prompt and the current buffer content */
542 abAppend(&ab,l->prompt,strlen(l->prompt));
@@ -546,11 +546,11 @@
546 * emit a newline and move the prompt to the first column. */
547 if (l->pos &&
548 l->pos == l->len &&
549 (l->pos+plen) % l->cols == 0)
550 {
551 lndebug("<newline>");
552 abAppend(&ab,"\n",1);
553 snprintf(seq,64,"\r");
554 abAppend(&ab,seq,strlen(seq));
555 rows++;
556 if (rows > (int)l->maxrows) l->maxrows = rows;
@@ -574,11 +574,11 @@
574 snprintf(seq,64,"\r\x1b[%dC", col);
575 else
576 snprintf(seq,64,"\r");
577 abAppend(&ab,seq,strlen(seq));
578
579 lndebug("\n");
580 l->oldpos = l->pos;
581
582 if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
583 abFree(&ab);
584 }
585
--- src/linenoise.c
+++ src/linenoise.c
@@ -176,24 +176,24 @@
176 static void refreshLine(struct linenoiseState *l);
177
178 /* Debugging macro. */
179 #if 0
180 FILE *lndebug_fp = NULL;
181 #define lndebug(fmt, arg1) \
182 do { \
183 if (lndebug_fp == NULL) { \
184 lndebug_fp = fopen("/tmp/lndebug.txt","a"); \
185 fprintf(lndebug_fp, \
186 "[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \
187 (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \
188 (int)l->maxrows,old_rows); \
189 } \
190 fprintf(lndebug_fp, ", " fmt, arg1); \
191 fflush(lndebug_fp); \
192 } while (0)
193 #else
194 #define lndebug(fmt, arg1)
195 #endif
196
197 /* ======================= Low level terminal handling ====================== */
198
199 /* Set if to use or not the multi line mode. */
@@ -526,17 +526,17 @@
526 abAppend(&ab,seq,strlen(seq));
527 }
528
529 /* Now for every row clear it, go up. */
530 for (j = 0; j < old_rows-1; j++) {
531 lndebug("clear+up", 0);
532 snprintf(seq,64,"\r\x1b[0K\x1b[1A");
533 abAppend(&ab,seq,strlen(seq));
534 }
535
536 /* Clean the top line. */
537 lndebug("clear", 0);
538 snprintf(seq,64,"\r\x1b[0K");
539 abAppend(&ab,seq,strlen(seq));
540
541 /* Write the prompt and the current buffer content */
542 abAppend(&ab,l->prompt,strlen(l->prompt));
@@ -546,11 +546,11 @@
546 * emit a newline and move the prompt to the first column. */
547 if (l->pos &&
548 l->pos == l->len &&
549 (l->pos+plen) % l->cols == 0)
550 {
551 lndebug("<newline>", 0);
552 abAppend(&ab,"\n",1);
553 snprintf(seq,64,"\r");
554 abAppend(&ab,seq,strlen(seq));
555 rows++;
556 if (rows > (int)l->maxrows) l->maxrows = rows;
@@ -574,11 +574,11 @@
574 snprintf(seq,64,"\r\x1b[%dC", col);
575 else
576 snprintf(seq,64,"\r");
577 abAppend(&ab,seq,strlen(seq));
578
579 lndebug("\n", 0);
580 l->oldpos = l->pos;
581
582 if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
583 abFree(&ab);
584 }
585
+2 -1
--- src/setup.c
+++ src/setup.c
@@ -1472,11 +1472,12 @@
14721472
}
14731473
}
14741474
@ </td></tr></table>
14751475
@ </div></form>
14761476
@ <p>Settings marked with (v) are 'versionable' and will be overridden
1477
- @ by the contents of files named <tt>.fossil-settings/PROPERTY</tt>.
1477
+ @ by the contents of files named <tt>.fossil-settings/PROPERTY</tt>
1478
+ @ in the check-out root.
14781479
@ If such a file is present, the corresponding field above is not
14791480
@ editable.</p><hr /><p>
14801481
@ These settings work in the same way, as the <kbd>set</kbd>
14811482
@ commandline:<br />
14821483
@ </p><pre>%s(zHelp_setting_cmd)</pre>
14831484
--- src/setup.c
+++ src/setup.c
@@ -1472,11 +1472,12 @@
1472 }
1473 }
1474 @ </td></tr></table>
1475 @ </div></form>
1476 @ <p>Settings marked with (v) are 'versionable' and will be overridden
1477 @ by the contents of files named <tt>.fossil-settings/PROPERTY</tt>.
 
1478 @ If such a file is present, the corresponding field above is not
1479 @ editable.</p><hr /><p>
1480 @ These settings work in the same way, as the <kbd>set</kbd>
1481 @ commandline:<br />
1482 @ </p><pre>%s(zHelp_setting_cmd)</pre>
1483
--- src/setup.c
+++ src/setup.c
@@ -1472,11 +1472,12 @@
1472 }
1473 }
1474 @ </td></tr></table>
1475 @ </div></form>
1476 @ <p>Settings marked with (v) are 'versionable' and will be overridden
1477 @ by the contents of files named <tt>.fossil-settings/PROPERTY</tt>
1478 @ in the check-out root.
1479 @ If such a file is present, the corresponding field above is not
1480 @ editable.</p><hr /><p>
1481 @ These settings work in the same way, as the <kbd>set</kbd>
1482 @ commandline:<br />
1483 @ </p><pre>%s(zHelp_setting_cmd)</pre>
1484
+1240 -1037
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -325,11 +325,11 @@
325325
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
326326
** [sqlite_version()] and [sqlite_source_id()].
327327
*/
328328
#define SQLITE_VERSION "3.8.11"
329329
#define SQLITE_VERSION_NUMBER 3008011
330
-#define SQLITE_SOURCE_ID "2015-07-08 16:22:42 5348ffc3fda5168c1e9e14aa88b0c6aedbda7c94"
330
+#define SQLITE_SOURCE_ID "2015-07-23 20:44:49 017c5019e1ce042025d4f327e50ec50af49f9fa4"
331331
332332
/*
333333
** CAPI3REF: Run-Time Library Version Numbers
334334
** KEYWORDS: sqlite3_version, sqlite3_sourceid
335335
**
@@ -1179,13 +1179,13 @@
11791179
**
11801180
** <li>[[SQLITE_FCNTL_ZIPVFS]]
11811181
** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
11821182
** VFS should return SQLITE_NOTFOUND for this opcode.
11831183
**
1184
-** <li>[[SQLITE_FCNTL_OTA]]
1185
-** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
1186
-** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
1184
+** <li>[[SQLITE_FCNTL_RBU]]
1185
+** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
1186
+** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
11871187
** this opcode.
11881188
** </ul>
11891189
*/
11901190
#define SQLITE_FCNTL_LOCKSTATE 1
11911191
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -1209,11 +1209,11 @@
12091209
#define SQLITE_FCNTL_SYNC 21
12101210
#define SQLITE_FCNTL_COMMIT_PHASETWO 22
12111211
#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
12121212
#define SQLITE_FCNTL_WAL_BLOCK 24
12131213
#define SQLITE_FCNTL_ZIPVFS 25
1214
-#define SQLITE_FCNTL_OTA 26
1214
+#define SQLITE_FCNTL_RBU 26
12151215
12161216
/* deprecated names */
12171217
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
12181218
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
12191219
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -8361,11 +8361,11 @@
83618361
83628362
/*
83638363
** Make sure that the compiler intrinsics we desire are enabled when
83648364
** compiling with an appropriate version of MSVC.
83658365
*/
8366
-#if defined(_MSC_VER) && _MSC_VER>=1300
8366
+#if defined(_MSC_VER) && _MSC_VER>=1300 && !defined(_WIN32_WCE)
83678367
# include <intrin.h>
83688368
# pragma intrinsic(_byteswap_ushort)
83698369
# pragma intrinsic(_byteswap_ulong)
83708370
#endif
83718371
@@ -9189,11 +9189,13 @@
91899189
#endif
91909190
#ifndef SQLITE_MAX_MMAP_SIZE
91919191
# if defined(__linux__) \
91929192
|| defined(_WIN32) \
91939193
|| (defined(__APPLE__) && defined(__MACH__)) \
9194
- || defined(__sun)
9194
+ || defined(__sun) \
9195
+ || defined(__FreeBSD__) \
9196
+ || defined(__DragonFly__)
91959197
# define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
91969198
# else
91979199
# define SQLITE_MAX_MMAP_SIZE 0
91989200
# endif
91999201
# define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
@@ -13883,10 +13885,11 @@
1388313885
**
1388413886
*************************************************************************
1388513887
**
1388613888
** This file contains definitions of global variables and constants.
1388713889
*/
13890
+/* #include "sqliteInt.h" */
1388813891
1388913892
/* An array to map all upper-case characters into their corresponding
1389013893
** lower-case character.
1389113894
**
1389213895
** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
@@ -14122,10 +14125,11 @@
1412214125
*/
1412314126
#ifndef SQLITE_OMIT_WSD
1412414127
SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
1412514128
#endif
1412614129
14130
+/* #include "opcodes.h" */
1412714131
/*
1412814132
** Properties of opcodes. The OPFLG_INITIALIZER macro is
1412914133
** created by mkopcodeh.awk during compilation. Data is obtained
1413014134
** from the comments following the "case OP_xxxx:" statements in
1413114135
** the vdbe.c file.
@@ -14150,10 +14154,11 @@
1415014154
** SQLite was built with.
1415114155
*/
1415214156
1415314157
#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
1415414158
14159
+/* #include "sqliteInt.h" */
1415514160
1415614161
/*
1415714162
** An array of names of all compile-time options. This array should
1415814163
** be sorted A-Z.
1415914164
**
@@ -14579,10 +14584,11 @@
1457914584
*************************************************************************
1458014585
**
1458114586
** This module implements the sqlite3_status() interface and related
1458214587
** functionality.
1458314588
*/
14589
+/* #include "sqliteInt.h" */
1458414590
/************** Include vdbeInt.h in the middle of status.c ******************/
1458514591
/************** Begin file vdbeInt.h *****************************************/
1458614592
/*
1458714593
** 2003 September 6
1458814594
**
@@ -15462,10 +15468,11 @@
1546215468
** Astronomical Algorithms, 2nd Edition, 1998
1546315469
** ISBM 0-943396-61-1
1546415470
** Willmann-Bell, Inc
1546515471
** Richmond, Virginia (USA)
1546615472
*/
15473
+/* #include "sqliteInt.h" */
1546715474
/* #include <stdlib.h> */
1546815475
/* #include <assert.h> */
1546915476
#include <time.h>
1547015477
1547115478
#ifndef SQLITE_OMIT_DATETIME_FUNCS
@@ -15773,11 +15780,11 @@
1577315780
Z = (int)((p->iJD + 43200000)/86400000);
1577415781
A = (int)((Z - 1867216.25)/36524.25);
1577515782
A = Z + 1 + A - (A/4);
1577615783
B = A + 1524;
1577715784
C = (int)((B - 122.1)/365.25);
15778
- D = (36525*C)/100;
15785
+ D = (36525*(C&32767))/100;
1577915786
E = (int)((B-D)/30.6001);
1578015787
X1 = (int)(30.6001*E);
1578115788
p->D = B - D - X1;
1578215789
p->M = E<14 ? E-1 : E-13;
1578315790
p->Y = p->M>2 ? C - 4716 : C - 4715;
@@ -16574,10 +16581,11 @@
1657416581
**
1657516582
** This file contains OS interface code that is common to all
1657616583
** architectures.
1657716584
*/
1657816585
#define _SQLITE_OS_C_ 1
16586
+/* #include "sqliteInt.h" */
1657916587
#undef _SQLITE_OS_C_
1658016588
1658116589
/*
1658216590
** The default SQLite sqlite3_vfs implementations do not allocate
1658316591
** memory (actually, os_unix.c allocates a small amount of memory
@@ -16980,10 +16988,11 @@
1698016988
** is completely recoverable simply by not carrying out the resize. The
1698116989
** hash table will continue to function normally. So a malloc failure
1698216990
** during a hash table resize is a benign fault.
1698316991
*/
1698416992
16993
+/* #include "sqliteInt.h" */
1698516994
1698616995
#ifndef SQLITE_OMIT_BUILTIN_TEST
1698716996
1698816997
/*
1698916998
** Global variables.
@@ -17061,10 +17070,11 @@
1706117070
** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
1706217071
** here always fail. SQLite will not operate with these drivers. These
1706317072
** are merely placeholders. Real drivers must be substituted using
1706417073
** sqlite3_config() before SQLite will operate.
1706517074
*/
17075
+/* #include "sqliteInt.h" */
1706617076
1706717077
/*
1706817078
** This version of the memory allocator is the default. It is
1706917079
** used when no other memory allocator is specified using compile-time
1707017080
** macros.
@@ -17147,10 +17157,11 @@
1714717157
** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
1714817158
** _msize() on windows systems. This might
1714917159
** be necessary when compiling for Delphi,
1715017160
** for example.
1715117161
*/
17162
+/* #include "sqliteInt.h" */
1715217163
1715317164
/*
1715417165
** This version of the memory allocator is the default. It is
1715517166
** used when no other memory allocator is specified using compile-time
1715617167
** macros.
@@ -17422,10 +17433,11 @@
1742217433
** leaks and memory usage errors.
1742317434
**
1742417435
** This file contains implementations of the low-level memory allocation
1742517436
** routines specified in the sqlite3_mem_methods object.
1742617437
*/
17438
+/* #include "sqliteInt.h" */
1742717439
1742817440
/*
1742917441
** This version of the memory allocator is used only if the
1743017442
** SQLITE_MEMDEBUG macro is defined
1743117443
*/
@@ -17956,10 +17968,11 @@
1795617968
** be changed.
1795717969
**
1795817970
** This version of the memory allocation subsystem is included
1795917971
** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
1796017972
*/
17973
+/* #include "sqliteInt.h" */
1796117974
1796217975
/*
1796317976
** This version of the memory allocator is only built into the library
1796417977
** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
1796517978
** mean that the library will use a memory-pool by default, just that
@@ -18670,10 +18683,11 @@
1867018683
** N >= M*(1 + log2(n)/2) - n + 1
1867118684
**
1867218685
** The sqlite3_status() logic tracks the maximum values of n and M so
1867318686
** that an application can, at any time, verify this constraint.
1867418687
*/
18688
+/* #include "sqliteInt.h" */
1867518689
1867618690
/*
1867718691
** This version of the memory allocator is used only when
1867818692
** SQLITE_ENABLE_MEMSYS5 is defined.
1867918693
*/
@@ -19213,10 +19227,11 @@
1921319227
*************************************************************************
1921419228
** This file contains the C functions that implement mutexes.
1921519229
**
1921619230
** This file contains code that is common across all mutex implementations.
1921719231
*/
19232
+/* #include "sqliteInt.h" */
1921819233
1921919234
#if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
1922019235
/*
1922119236
** For debugging purposes, record when the mutex subsystem is initialized
1922219237
** and uninitialized so that we can assert() if there is an attempt to
@@ -19386,10 +19401,11 @@
1938619401
**
1938719402
** If compiled with SQLITE_DEBUG, then additional logic is inserted
1938819403
** that does error checking on mutexes to make sure they are being
1938919404
** called correctly.
1939019405
*/
19406
+/* #include "sqliteInt.h" */
1939119407
1939219408
#ifndef SQLITE_MUTEX_OMIT
1939319409
1939419410
#ifndef SQLITE_DEBUG
1939519411
/*
@@ -19589,10 +19605,11 @@
1958919605
** May you share freely, never taking more than you give.
1959019606
**
1959119607
*************************************************************************
1959219608
** This file contains the C functions that implement mutexes for pthreads
1959319609
*/
19610
+/* #include "sqliteInt.h" */
1959419611
1959519612
/*
1959619613
** The code in this file is only used if we are compiling threadsafe
1959719614
** under unix with pthreads.
1959819615
**
@@ -19963,10 +19980,11 @@
1996319980
** May you share freely, never taking more than you give.
1996419981
**
1996519982
*************************************************************************
1996619983
** This file contains the C functions that implement mutexes for Win32.
1996719984
*/
19985
+/* #include "sqliteInt.h" */
1996819986
1996919987
#if SQLITE_OS_WIN
1997019988
/*
1997119989
** Include code that is common to all os_*.c files
1997219990
*/
@@ -20638,10 +20656,11 @@
2063820656
**
2063920657
*************************************************************************
2064020658
**
2064120659
** Memory allocation functions used throughout sqlite.
2064220660
*/
20661
+/* #include "sqliteInt.h" */
2064320662
/* #include <stdarg.h> */
2064420663
2064520664
/*
2064620665
** Attempt to release up to n bytes of non-essential memory currently
2064720666
** held by SQLite. An example of non-essential memory is memory used to
@@ -21449,10 +21468,11 @@
2144921468
** This file contains code for a set of "printf"-like routines. These
2145021469
** routines format strings much like the printf() from the standard C
2145121470
** library, though the implementation here has enhancements to support
2145221471
** SQLite.
2145321472
*/
21473
+/* #include "sqliteInt.h" */
2145421474
2145521475
/*
2145621476
** Conversion types fall into various categories as defined by the
2145721477
** following enumeration.
2145821478
*/
@@ -22451,10 +22471,15 @@
2245122471
** stack space on small-stack systems when logging is disabled.
2245222472
**
2245322473
** sqlite3_log() must render into a static buffer. It cannot dynamically
2245422474
** allocate memory because it might be called while the memory allocator
2245522475
** mutex is held.
22476
+**
22477
+** sqlite3VXPrintf() might ask for *temporary* memory allocations for
22478
+** certain format characters (%q) or for very large precisions or widths.
22479
+** Care must be taken that any sqlite3_log() calls that occur while the
22480
+** memory mutex is held do not use these mechanisms.
2245622481
*/
2245722482
static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
2245822483
StrAccum acc; /* String accumulator */
2245922484
char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
2246022485
@@ -22526,10 +22551,11 @@
2252622551
** analysis.
2252722552
**
2252822553
** The interfaces in this file is only available when compiling
2252922554
** with SQLITE_DEBUG.
2253022555
*/
22556
+/* #include "sqliteInt.h" */
2253122557
#ifdef SQLITE_DEBUG
2253222558
2253322559
/*
2253422560
** Add a new subitem to the tree. The moreToFollow flag indicates that this
2253522561
** is not the last item in the tree.
@@ -22957,10 +22983,11 @@
2295722983
** generator (PRNG) for SQLite.
2295822984
**
2295922985
** Random numbers are used by some of the database backends in order
2296022986
** to generate random integer keys for tables or random filenames.
2296122987
*/
22988
+/* #include "sqliteInt.h" */
2296222989
2296322990
2296422991
/* All threads share a single random number generator.
2296522992
** This structure is the current state of the generator.
2296622993
*/
@@ -23103,11 +23130,13 @@
2310323130
** single threaded systems. Nothing in SQLite requires multiple threads.
2310423131
** This interface exists so that applications that want to take advantage
2310523132
** of multiple cores can do so, while also allowing applications to stay
2310623133
** single-threaded if desired.
2310723134
*/
23135
+/* #include "sqliteInt.h" */
2310823136
#if SQLITE_OS_WIN
23137
+/* # include "os_win.h" */
2310923138
#endif
2311023139
2311123140
#if SQLITE_MAX_WORKER_THREADS>0
2311223141
2311323142
/********************************* Unix Pthreads ****************************/
@@ -23377,11 +23406,13 @@
2337723406
** BOM or Byte Order Mark:
2337823407
** 0xff 0xfe little-endian utf-16 follows
2337923408
** 0xfe 0xff big-endian utf-16 follows
2338023409
**
2338123410
*/
23411
+/* #include "sqliteInt.h" */
2338223412
/* #include <assert.h> */
23413
+/* #include "vdbeInt.h" */
2338323414
2338423415
#ifndef SQLITE_AMALGAMATION
2338523416
/*
2338623417
** The following constant value is used by the SQLITE_BIGENDIAN and
2338723418
** SQLITE_LITTLEENDIAN macros.
@@ -23890,10 +23921,11 @@
2389023921
**
2389123922
** This file contains functions for allocating memory, comparing
2389223923
** strings, and stuff like that.
2389323924
**
2389423925
*/
23926
+/* #include "sqliteInt.h" */
2389523927
/* #include <stdarg.h> */
2389623928
#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
2389723929
# include <math.h>
2389823930
#endif
2389923931
@@ -25286,10 +25318,11 @@
2528625318
**
2528725319
*************************************************************************
2528825320
** This is the implementation of generic hash-tables
2528925321
** used in SQLite.
2529025322
*/
25323
+/* #include "sqliteInt.h" */
2529125324
/* #include <assert.h> */
2529225325
2529325326
/* Turn bulk memory into a hash table object by initializing the
2529425327
** fields of the Hash structure.
2529525328
**
@@ -25763,10 +25796,11 @@
2576325796
** * sqlite3_vfs method implementations.
2576425797
** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
2576525798
** * Definitions of sqlite3_vfs objects for all locking methods
2576625799
** plus implementations of sqlite3_os_init() and sqlite3_os_end().
2576725800
*/
25801
+/* #include "sqliteInt.h" */
2576825802
#if SQLITE_OS_UNIX /* This file is used on unix only */
2576925803
2577025804
/*
2577125805
** There are various methods for file locking used for concurrency
2577225806
** control:
@@ -33497,10 +33531,11 @@
3349733531
**
3349833532
******************************************************************************
3349933533
**
3350033534
** This file contains code that is specific to Windows.
3350133535
*/
33536
+/* #include "sqliteInt.h" */
3350233537
#if SQLITE_OS_WIN /* This file is used for Windows only */
3350333538
3350433539
/*
3350533540
** Include code that is common to all os_*.c files
3350633541
*/
@@ -33705,10 +33740,11 @@
3370533740
/************** Continuing where we left off in os_win.c *********************/
3370633741
3370733742
/*
3370833743
** Include the header file for the Windows VFS.
3370933744
*/
33745
+/* #include "os_win.h" */
3371033746
3371133747
/*
3371233748
** Compiling and using WAL mode requires several APIs that are only
3371333749
** available in Windows platforms based on the NT kernel.
3371433750
*/
@@ -39358,10 +39394,11 @@
3935839394
** sometimes grow into tens of thousands or larger. The size of the
3935939395
** Bitvec object is the number of pages in the database file at the
3936039396
** start of a transaction, and is thus usually less than a few thousand,
3936139397
** but can be as large as 2 billion for a really big database.
3936239398
*/
39399
+/* #include "sqliteInt.h" */
3936339400
3936439401
/* Size of the Bitvec structure in bytes. */
3936539402
#define BITVEC_SZ 512
3936639403
3936739404
/* Round the union size down to the nearest pointer boundary, since that's how
@@ -39747,10 +39784,11 @@
3974739784
** May you share freely, never taking more than you give.
3974839785
**
3974939786
*************************************************************************
3975039787
** This file implements that page cache.
3975139788
*/
39789
+/* #include "sqliteInt.h" */
3975239790
3975339791
/*
3975439792
** A complete page cache is an instance of this structure.
3975539793
*/
3975639794
struct PCache {
@@ -40488,10 +40526,11 @@
4048840526
**
4048940527
** Earlier versions of SQLite used only methods (1) and (2). But experiments
4049040528
** show that method (3) with N==100 provides about a 5% performance boost for
4049140529
** common workloads.
4049240530
*/
40531
+/* #include "sqliteInt.h" */
4049340532
4049440533
typedef struct PCache1 PCache1;
4049540534
typedef struct PgHdr1 PgHdr1;
4049640535
typedef struct PgFreeslot PgFreeslot;
4049740536
typedef struct PGroup PGroup;
@@ -40598,10 +40637,11 @@
4059840637
** fixed at sqlite3_initialize() time and do not require mutex protection.
4059940638
** The nFreeSlot and pFree values do require mutex protection.
4060040639
*/
4060140640
int isInit; /* True if initialized */
4060240641
int separateCache; /* Use a new PGroup for each PCache */
40642
+ int nInitPage; /* Initial bulk allocation size */
4060340643
int szSlot; /* Size of each free slot */
4060440644
int nSlot; /* The number of pcache slots */
4060540645
int nReserve; /* Try to keep nFreeSlot above this */
4060640646
void *pStart, *pEnd; /* Bounds of global page cache memory */
4060740647
/* Above requires no mutex. Use mutex below for variable that follow. */
@@ -40665,10 +40705,47 @@
4066540705
pBuf = (void*)&((char*)pBuf)[sz];
4066640706
}
4066740707
pcache1.pEnd = pBuf;
4066840708
}
4066940709
}
40710
+
40711
+/*
40712
+** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
40713
+** true if pCache->pFree ends up containing one or more free pages.
40714
+*/
40715
+static int pcache1InitBulk(PCache1 *pCache){
40716
+ i64 szBulk;
40717
+ char *zBulk;
40718
+ if( pcache1.nInitPage==0 ) return 0;
40719
+ /* Do not bother with a bulk allocation if the cache size very small */
40720
+ if( pCache->nMax<3 ) return 0;
40721
+ sqlite3BeginBenignMalloc();
40722
+ if( pcache1.nInitPage>0 ){
40723
+ szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
40724
+ }else{
40725
+ szBulk = -1024 * (i64)pcache1.nInitPage;
40726
+ }
40727
+ if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
40728
+ szBulk = pCache->szAlloc*pCache->nMax;
40729
+ }
40730
+ zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
40731
+ sqlite3EndBenignMalloc();
40732
+ if( zBulk ){
40733
+ int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
40734
+ int i;
40735
+ for(i=0; i<nBulk; i++){
40736
+ PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
40737
+ pX->page.pBuf = zBulk;
40738
+ pX->page.pExtra = &pX[1];
40739
+ pX->isBulkLocal = 1;
40740
+ pX->pNext = pCache->pFree;
40741
+ pCache->pFree = pX;
40742
+ zBulk += pCache->szAlloc;
40743
+ }
40744
+ }
40745
+ return pCache->pFree!=0;
40746
+}
4067040747
4067140748
/*
4067240749
** Malloc function used within this file to allocate space from the buffer
4067340750
** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
4067440751
** such buffer exists or there is no space left in it, this function falls
@@ -40766,11 +40843,11 @@
4076640843
static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
4076740844
PgHdr1 *p = 0;
4076840845
void *pPg;
4076940846
4077040847
assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
40771
- if( pCache->pFree ){
40848
+ if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
4077240849
p = pCache->pFree;
4077340850
pCache->pFree = p->pNext;
4077440851
p->pNext = 0;
4077540852
}else{
4077640853
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
@@ -40970,18 +41047,23 @@
4097041047
4097141048
/*
4097241049
** If there are currently more than nMaxPage pages allocated, try
4097341050
** to recycle pages to reduce the number allocated to nMaxPage.
4097441051
*/
40975
-static void pcache1EnforceMaxPage(PGroup *pGroup){
41052
+static void pcache1EnforceMaxPage(PCache1 *pCache){
41053
+ PGroup *pGroup = pCache->pGroup;
4097641054
assert( sqlite3_mutex_held(pGroup->mutex) );
4097741055
while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
4097841056
PgHdr1 *p = pGroup->pLruTail;
4097941057
assert( p->pCache->pGroup==pGroup );
4098041058
assert( p->isPinned==0 );
4098141059
pcache1PinPage(p);
4098241060
pcache1RemoveFromHash(p, 1);
41061
+ }
41062
+ if( pCache->nPage==0 && pCache->pBulk ){
41063
+ sqlite3_free(pCache->pBulk);
41064
+ pCache->pBulk = pCache->pFree = 0;
4098341065
}
4098441066
}
4098541067
4098641068
/*
4098741069
** Discard all pages from cache pCache with a page number (key value)
@@ -41054,10 +41136,18 @@
4105441136
if( sqlite3GlobalConfig.bCoreMutex ){
4105541137
pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
4105641138
pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
4105741139
}
4105841140
#endif
41141
+ if( pcache1.separateCache
41142
+ && sqlite3GlobalConfig.nPage!=0
41143
+ && sqlite3GlobalConfig.pPage==0
41144
+ ){
41145
+ pcache1.nInitPage = sqlite3GlobalConfig.nPage;
41146
+ }else{
41147
+ pcache1.nInitPage = 0;
41148
+ }
4105941149
pcache1.grp.mxPinned = 10;
4106041150
pcache1.isInit = 1;
4106141151
return SQLITE_OK;
4106241152
}
4106341153
@@ -41108,40 +41198,10 @@
4110841198
pCache->nMin = 10;
4110941199
pGroup->nMinPage += pCache->nMin;
4111041200
pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
4111141201
}
4111241202
pcache1LeaveMutex(pGroup);
41113
- /* Try to initialize the local bulk pagecache line allocation if using
41114
- ** separate caches and if nPage!=0 */
41115
- if( pcache1.separateCache
41116
- && sqlite3GlobalConfig.nPage!=0
41117
- && sqlite3GlobalConfig.pPage==0
41118
- ){
41119
- int szBulk;
41120
- char *zBulk;
41121
- sqlite3BeginBenignMalloc();
41122
- if( sqlite3GlobalConfig.nPage>0 ){
41123
- szBulk = pCache->szAlloc * sqlite3GlobalConfig.nPage;
41124
- }else{
41125
- szBulk = -1024*sqlite3GlobalConfig.nPage;
41126
- }
41127
- zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
41128
- sqlite3EndBenignMalloc();
41129
- if( zBulk ){
41130
- int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
41131
- int i;
41132
- for(i=0; i<nBulk; i++){
41133
- PgHdr1 *pX = (PgHdr1*)&zBulk[szPage];
41134
- pX->page.pBuf = zBulk;
41135
- pX->page.pExtra = &pX[1];
41136
- pX->isBulkLocal = 1;
41137
- pX->pNext = pCache->pFree;
41138
- pCache->pFree = pX;
41139
- zBulk += pCache->szAlloc;
41140
- }
41141
- }
41142
- }
4114341203
if( pCache->nHash==0 ){
4114441204
pcache1Destroy((sqlite3_pcache*)pCache);
4114541205
pCache = 0;
4114641206
}
4114741207
}
@@ -41160,11 +41220,11 @@
4116041220
pcache1EnterMutex(pGroup);
4116141221
pGroup->nMaxPage += (nMax - pCache->nMax);
4116241222
pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
4116341223
pCache->nMax = nMax;
4116441224
pCache->n90pct = pCache->nMax*9/10;
41165
- pcache1EnforceMaxPage(pGroup);
41225
+ pcache1EnforceMaxPage(pCache);
4116641226
pcache1LeaveMutex(pGroup);
4116741227
}
4116841228
}
4116941229
4117041230
/*
@@ -41178,11 +41238,11 @@
4117841238
PGroup *pGroup = pCache->pGroup;
4117941239
int savedMaxPage;
4118041240
pcache1EnterMutex(pGroup);
4118141241
savedMaxPage = pGroup->nMaxPage;
4118241242
pGroup->nMaxPage = 0;
41183
- pcache1EnforceMaxPage(pGroup);
41243
+ pcache1EnforceMaxPage(pCache);
4118441244
pGroup->nMaxPage = savedMaxPage;
4118541245
pcache1LeaveMutex(pGroup);
4118641246
}
4118741247
}
4118841248
@@ -41515,11 +41575,11 @@
4151541575
assert( pGroup->nMaxPage >= pCache->nMax );
4151641576
pGroup->nMaxPage -= pCache->nMax;
4151741577
assert( pGroup->nMinPage >= pCache->nMin );
4151841578
pGroup->nMinPage -= pCache->nMin;
4151941579
pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
41520
- pcache1EnforceMaxPage(pGroup);
41580
+ pcache1EnforceMaxPage(pCache);
4152141581
pcache1LeaveMutex(pGroup);
4152241582
sqlite3_free(pCache->pBulk);
4152341583
sqlite3_free(pCache->apHash);
4152441584
sqlite3_free(pCache);
4152541585
}
@@ -41679,10 +41739,11 @@
4167941739
** primitives are constant time. The cost of DESTROY is O(N).
4168041740
**
4168141741
** There is an added cost of O(N) when switching between TEST and
4168241742
** SMALLEST primitives.
4168341743
*/
41744
+/* #include "sqliteInt.h" */
4168441745
4168541746
4168641747
/*
4168741748
** Target size for allocation chunks.
4168841749
*/
@@ -42148,10 +42209,11 @@
4214842209
** locking to prevent two processes from writing the same database
4214942210
** file simultaneously, or one process from reading the database while
4215042211
** another is writing.
4215142212
*/
4215242213
#ifndef SQLITE_OMIT_DISKIO
42214
+/* #include "sqliteInt.h" */
4215342215
/************** Include wal.h in the middle of pager.c ***********************/
4215442216
/************** Begin file wal.h *********************************************/
4215542217
/*
4215642218
** 2010 February 1
4215742219
**
@@ -42169,10 +42231,11 @@
4216942231
*/
4217042232
4217142233
#ifndef _WAL_H_
4217242234
#define _WAL_H_
4217342235
42236
+/* #include "sqliteInt.h" */
4217442237
4217542238
/* Additional values that can be added to the sync_flags argument of
4217642239
** sqlite3WalFrames():
4217742240
*/
4217842241
#define WAL_SYNC_TRANSACTIONS 0x20 /* Sync at the end of each transaction */
@@ -49783,10 +49846,11 @@
4978349846
** that correspond to frames greater than the new K value are removed
4978449847
** from the hash table at this point.
4978549848
*/
4978649849
#ifndef SQLITE_OMIT_WAL
4978749850
49851
+/* #include "wal.h" */
4978849852
4978949853
/*
4979049854
** Trace output macros
4979149855
*/
4979249856
#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
@@ -50188,13 +50252,13 @@
5018850252
5018950253
assert( pWal->writeLock );
5019050254
pWal->hdr.isInit = 1;
5019150255
pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
5019250256
walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
50193
- memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
50257
+ memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
5019450258
walShmBarrier(pWal);
50195
- memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
50259
+ memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
5019650260
}
5019750261
5019850262
/*
5019950263
** This function encodes a single frame header and writes it to a buffer
5020050264
** supplied by the caller. A frame-header is made up of a series of
@@ -50492,17 +50556,17 @@
5049250556
#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
5049350557
/* Verify that the every entry in the mapping region is still reachable
5049450558
** via the hash table even after the cleanup.
5049550559
*/
5049650560
if( iLimit ){
50497
- int i; /* Loop counter */
50561
+ int j; /* Loop counter */
5049850562
int iKey; /* Hash key */
50499
- for(i=1; i<=iLimit; i++){
50500
- for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
50501
- if( aHash[iKey]==i ) break;
50563
+ for(j=1; j<=iLimit; j++){
50564
+ for(iKey=walHash(aPgno[j]); aHash[iKey]; iKey=walNextHash(iKey)){
50565
+ if( aHash[iKey]==j ) break;
5050250566
}
50503
- assert( aHash[iKey]==i );
50567
+ assert( aHash[iKey]==j );
5050450568
}
5050550569
}
5050650570
#endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
5050750571
}
5050850572
@@ -51000,11 +51064,11 @@
5100051064
5100151065
const int nList = *pnList; /* Size of input list */
5100251066
int nMerge = 0; /* Number of elements in list aMerge */
5100351067
ht_slot *aMerge = 0; /* List to be merged */
5100451068
int iList; /* Index into input list */
51005
- int iSub = 0; /* Index into aSub array */
51069
+ u32 iSub = 0; /* Index into aSub array */
5100651070
struct Sublist aSub[13]; /* Array of sub-lists */
5100751071
5100851072
memset(aSub, 0, sizeof(aSub));
5100951073
assert( nList<=HASHTABLE_NPAGE && nList>0 );
5101051074
assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
@@ -51011,11 +51075,13 @@
5101151075
5101251076
for(iList=0; iList<nList; iList++){
5101351077
nMerge = 1;
5101451078
aMerge = &aList[iList];
5101551079
for(iSub=0; iList & (1<<iSub); iSub++){
51016
- struct Sublist *p = &aSub[iSub];
51080
+ struct Sublist *p;
51081
+ assert( iSub<ArraySize(aSub) );
51082
+ p = &aSub[iSub];
5101751083
assert( p->aList && p->nList<=(1<<iSub) );
5101851084
assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
5101951085
walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
5102051086
}
5102151087
aSub[iSub].aList = aMerge;
@@ -51022,11 +51088,13 @@
5102251088
aSub[iSub].nList = nMerge;
5102351089
}
5102451090
5102551091
for(iSub++; iSub<ArraySize(aSub); iSub++){
5102651092
if( nList & (1<<iSub) ){
51027
- struct Sublist *p = &aSub[iSub];
51093
+ struct Sublist *p;
51094
+ assert( iSub<ArraySize(aSub) );
51095
+ p = &aSub[iSub];
5102851096
assert( p->nList<=(1<<iSub) );
5102951097
assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
5103051098
walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
5103151099
}
5103251100
}
@@ -52933,10 +53001,11 @@
5293353001
** SIZE DESCRIPTION
5293453002
** 4 Page number of next trunk page
5293553003
** 4 Number of leaf pointers on this page
5293653004
** * zero or more pages numbers of leaves
5293753005
*/
53006
+/* #include "sqliteInt.h" */
5293853007
5293953008
5294053009
/* The following value is the maximum cell size assuming a maximum page
5294153010
** size give above.
5294253011
*/
@@ -53730,10 +53799,11 @@
5373053799
*************************************************************************
5373153800
** This file implements an external (disk-based) database using BTrees.
5373253801
** See the header comment on "btreeInt.h" for additional information.
5373353802
** Including a description of file format and an overview of operation.
5373453803
*/
53804
+/* #include "btreeInt.h" */
5373553805
5373653806
/*
5373753807
** The header string that appears at the beginning of every
5373853808
** SQLite database.
5373953809
*/
@@ -62675,11 +62745,11 @@
6267562745
u32 *heap = 0; /* Min-heap used for checking cell coverage */
6267662746
u32 x, prev = 0; /* Next and previous entry on the min-heap */
6267762747
const char *saved_zPfx = pCheck->zPfx;
6267862748
int saved_v1 = pCheck->v1;
6267962749
int saved_v2 = pCheck->v2;
62680
- u8 savedIsInit;
62750
+ u8 savedIsInit = 0;
6268162751
6268262752
/* Check that the page exists
6268362753
*/
6268462754
pBt = pCheck->pBt;
6268562755
usableSize = pBt->usableSize;
@@ -63311,10 +63381,12 @@
6331163381
**
6331263382
*************************************************************************
6331363383
** This file contains the implementation of the sqlite3_backup_XXX()
6331463384
** API functions and the related features.
6331563385
*/
63386
+/* #include "sqliteInt.h" */
63387
+/* #include "btreeInt.h" */
6331663388
6331763389
/*
6331863390
** Structure allocated for each backup operation.
6331963391
*/
6332063392
struct sqlite3_backup {
@@ -64109,10 +64181,12 @@
6410964181
** This file contains code use to manipulate "Mem" structure. A "Mem"
6411064182
** stores a single value in the VDBE. Mem is an opaque structure visible
6411164183
** only within the VDBE. Interface routines refer to a Mem using the
6411264184
** name sqlite_value
6411364185
*/
64186
+/* #include "sqliteInt.h" */
64187
+/* #include "vdbeInt.h" */
6411464188
6411564189
#ifdef SQLITE_DEBUG
6411664190
/*
6411764191
** Check invariants on a Mem object.
6411864192
**
@@ -65826,10 +65900,12 @@
6582665900
**
6582765901
*************************************************************************
6582865902
** This file contains code used for creating, destroying, and populating
6582965903
** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
6583065904
*/
65905
+/* #include "sqliteInt.h" */
65906
+/* #include "vdbeInt.h" */
6583165907
6583265908
/*
6583365909
** Create a new virtual database engine.
6583465910
*/
6583565911
SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
@@ -70074,10 +70150,12 @@
7007470150
*************************************************************************
7007570151
**
7007670152
** This file contains code use to implement APIs that are part of the
7007770153
** VDBE.
7007870154
*/
70155
+/* #include "sqliteInt.h" */
70156
+/* #include "vdbeInt.h" */
7007970157
7008070158
#ifndef SQLITE_OMIT_DEPRECATED
7008170159
/*
7008270160
** Return TRUE (non-zero) of the statement supplied as an argument needs
7008370161
** to be recompiled. A statement needs to be recompiled whenever the
@@ -70112,10 +70190,35 @@
7011270190
}else{
7011370191
return vdbeSafety(p);
7011470192
}
7011570193
}
7011670194
70195
+#ifndef SQLITE_OMIT_TRACE
70196
+/*
70197
+** Invoke the profile callback. This routine is only called if we already
70198
+** know that the profile callback is defined and needs to be invoked.
70199
+*/
70200
+static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
70201
+ sqlite3_int64 iNow;
70202
+ assert( p->startTime>0 );
70203
+ assert( db->xProfile!=0 );
70204
+ assert( db->init.busy==0 );
70205
+ assert( p->zSql!=0 );
70206
+ sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
70207
+ db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
70208
+ p->startTime = 0;
70209
+}
70210
+/*
70211
+** The checkProfileCallback(DB,P) macro checks to see if a profile callback
70212
+** is needed, and it invokes the callback if it is needed.
70213
+*/
70214
+# define checkProfileCallback(DB,P) \
70215
+ if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
70216
+#else
70217
+# define checkProfileCallback(DB,P) /*no-op*/
70218
+#endif
70219
+
7011770220
/*
7011870221
** The following routine destroys a virtual machine that is created by
7011970222
** the sqlite3_compile() routine. The integer returned is an SQLITE_
7012070223
** success/failure code that describes the result of executing the virtual
7012170224
** machine.
@@ -70132,10 +70235,11 @@
7013270235
}else{
7013370236
Vdbe *v = (Vdbe*)pStmt;
7013470237
sqlite3 *db = v->db;
7013570238
if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
7013670239
sqlite3_mutex_enter(db->mutex);
70240
+ checkProfileCallback(db, v);
7013770241
rc = sqlite3VdbeFinalize(v);
7013870242
rc = sqlite3ApiExit(db, rc);
7013970243
sqlite3LeaveMutexAndCloseZombie(db);
7014070244
}
7014170245
return rc;
@@ -70153,16 +70257,18 @@
7015370257
int rc;
7015470258
if( pStmt==0 ){
7015570259
rc = SQLITE_OK;
7015670260
}else{
7015770261
Vdbe *v = (Vdbe*)pStmt;
70158
- sqlite3_mutex_enter(v->db->mutex);
70262
+ sqlite3 *db = v->db;
70263
+ sqlite3_mutex_enter(db->mutex);
70264
+ checkProfileCallback(db, v);
7015970265
rc = sqlite3VdbeReset(v);
7016070266
sqlite3VdbeRewind(v);
70161
- assert( (rc & (v->db->errMask))==rc );
70162
- rc = sqlite3ApiExit(v->db, rc);
70163
- sqlite3_mutex_leave(v->db->mutex);
70267
+ assert( (rc & (db->errMask))==rc );
70268
+ rc = sqlite3ApiExit(db, rc);
70269
+ sqlite3_mutex_leave(db->mutex);
7016470270
}
7016570271
return rc;
7016670272
}
7016770273
7016870274
/*
@@ -70508,10 +70614,11 @@
7050870614
}
7050970615
}
7051070616
#endif
7051170617
return rc;
7051270618
}
70619
+
7051370620
7051470621
/*
7051570622
** Execute the statement pStmt, either until a row of data is ready, the
7051670623
** statement is completely executed or an error occurs.
7051770624
**
@@ -70577,12 +70684,14 @@
7057770684
assert( db->nVdbeWrite>0 || db->autoCommit==0
7057870685
|| (db->nDeferredCons==0 && db->nDeferredImmCons==0)
7057970686
);
7058070687
7058170688
#ifndef SQLITE_OMIT_TRACE
70582
- if( db->xProfile && !db->init.busy ){
70689
+ if( db->xProfile && !db->init.busy && p->zSql ){
7058370690
sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
70691
+ }else{
70692
+ assert( p->startTime==0 );
7058470693
}
7058570694
#endif
7058670695
7058770696
db->nVdbeActive++;
7058870697
if( p->readOnly==0 ) db->nVdbeWrite++;
@@ -70602,17 +70711,12 @@
7060270711
rc = sqlite3VdbeExec(p);
7060370712
db->nVdbeExec--;
7060470713
}
7060570714
7060670715
#ifndef SQLITE_OMIT_TRACE
70607
- /* Invoke the profile callback if there is one
70608
- */
70609
- if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
70610
- sqlite3_int64 iNow;
70611
- sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
70612
- db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
70613
- }
70716
+ /* If the statement completed successfully, invoke the profile callback */
70717
+ if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
7061470718
#endif
7061570719
7061670720
if( rc==SQLITE_DONE ){
7061770721
assert( p->rc==SQLITE_OK );
7061870722
p->rc = doWalCallbacks(db);
@@ -71685,10 +71789,12 @@
7168571789
** This file contains code used to insert the values of host parameters
7168671790
** (aka "wildcards") into the SQL text output by sqlite3_trace().
7168771791
**
7168871792
** The Vdbe parse-tree explainer is also found here.
7168971793
*/
71794
+/* #include "sqliteInt.h" */
71795
+/* #include "vdbeInt.h" */
7169071796
7169171797
#ifndef SQLITE_OMIT_TRACE
7169271798
7169371799
/*
7169471800
** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
@@ -71877,10 +71983,12 @@
7187771983
** documentation, headers files, or other derived files. The formatting
7187871984
** of the code in this file is, therefore, important. See other comments
7187971985
** in this file for details. If in doubt, do not deviate from existing
7188071986
** commenting and indentation practices when changing or adding code.
7188171987
*/
71988
+/* #include "sqliteInt.h" */
71989
+/* #include "vdbeInt.h" */
7188271990
7188371991
/*
7188471992
** Invoke this macro on memory cells just prior to changing the
7188571993
** value of the cell. This macro verifies that shallow copies are
7188671994
** not misused. A shallow copy of a string or blob just copies a
@@ -78635,10 +78743,12 @@
7863578743
*************************************************************************
7863678744
**
7863778745
** This file contains code used to implement incremental BLOB I/O.
7863878746
*/
7863978747
78748
+/* #include "sqliteInt.h" */
78749
+/* #include "vdbeInt.h" */
7864078750
7864178751
#ifndef SQLITE_OMIT_INCRBLOB
7864278752
7864378753
/*
7864478754
** Valid sqlite3_blob* handles point to Incrblob structures.
@@ -79233,10 +79343,12 @@
7923379343
** than one background thread may be created. Specifically, there may be
7923479344
** one background thread for each temporary file on disk, and one background
7923579345
** thread to merge the output of each of the others to a single PMA for
7923679346
** the main thread to read from.
7923779347
*/
79348
+/* #include "sqliteInt.h" */
79349
+/* #include "vdbeInt.h" */
7923879350
7923979351
/*
7924079352
** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
7924179353
** messages to stderr that may be helpful in understanding the performance
7924279354
** characteristics of the sorter in multi-threaded mode.
@@ -81849,10 +81961,11 @@
8184981961
** 1) The in-memory representation grows too large for the allocated
8185081962
** buffer, or
8185181963
** 2) The sqlite3JournalCreate() function is called.
8185281964
*/
8185381965
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
81966
+/* #include "sqliteInt.h" */
8185481967
8185581968
8185681969
/*
8185781970
** A JournalFile object is a subclass of sqlite3_file used by
8185881971
** as an open file handle for journal files.
@@ -82096,10 +82209,11 @@
8209682209
**
8209782210
** This file contains code use to implement an in-memory rollback journal.
8209882211
** The in-memory rollback journal is used to journal transactions for
8209982212
** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
8210082213
*/
82214
+/* #include "sqliteInt.h" */
8210182215
8210282216
/* Forward references to internal structures */
8210382217
typedef struct MemJournal MemJournal;
8210482218
typedef struct FilePoint FilePoint;
8210582219
typedef struct FileChunk FileChunk;
@@ -82351,10 +82465,11 @@
8235182465
**
8235282466
*************************************************************************
8235382467
** This file contains routines used for walking the parser tree for
8235482468
** an SQL statement.
8235582469
*/
82470
+/* #include "sqliteInt.h" */
8235682471
/* #include <stdlib.h> */
8235782472
/* #include <string.h> */
8235882473
8235982474
8236082475
/*
@@ -82509,10 +82624,11 @@
8250982624
**
8251082625
** This file contains routines used for walking the parser tree and
8251182626
** resolve all identifiers by associating them with a particular
8251282627
** table and column.
8251382628
*/
82629
+/* #include "sqliteInt.h" */
8251482630
/* #include <stdlib.h> */
8251582631
/* #include <string.h> */
8251682632
8251782633
/*
8251882634
** Walk the expression tree pExpr and increase the aggregate function
@@ -84021,10 +84137,11 @@
8402184137
**
8402284138
*************************************************************************
8402384139
** This file contains routines used for analyzing expressions and
8402484140
** for generating VDBE code that evaluates expressions in SQLite.
8402584141
*/
84142
+/* #include "sqliteInt.h" */
8402684143
8402784144
/*
8402884145
** Return the 'affinity' of the expression pExpr if any.
8402984146
**
8403084147
** If pExpr is a column, a reference to a column via an 'AS' alias,
@@ -88183,10 +88300,11 @@
8818388300
**
8818488301
*************************************************************************
8818588302
** This file contains C code routines that used to generate VDBE code
8818688303
** that implements the ALTER TABLE command.
8818788304
*/
88305
+/* #include "sqliteInt.h" */
8818888306
8818988307
/*
8819088308
** The code in this file only exists if we are not omitting the
8819188309
** ALTER TABLE logic from the build.
8819288310
*/
@@ -89145,10 +89263,11 @@
8914589263
** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
8914689264
** all contain just a single integer which is the same as the first
8914789265
** integer in the equivalent columns in sqlite_stat4.
8914889266
*/
8914989267
#ifndef SQLITE_OMIT_ANALYZE
89268
+/* #include "sqliteInt.h" */
8915089269
8915189270
#if defined(SQLITE_ENABLE_STAT4)
8915289271
# define IsStat4 1
8915389272
# define IsStat3 0
8915489273
#elif defined(SQLITE_ENABLE_STAT3)
@@ -90910,10 +91029,11 @@
9091091029
** May you share freely, never taking more than you give.
9091191030
**
9091291031
*************************************************************************
9091391032
** This file contains code used to implement the ATTACH and DETACH commands.
9091491033
*/
91034
+/* #include "sqliteInt.h" */
9091591035
9091691036
#ifndef SQLITE_OMIT_ATTACH
9091791037
/*
9091891038
** Resolve an expression that was part of an ATTACH or DETACH statement. This
9091991039
** is slightly different from resolving a normal SQL expression, because simple
@@ -91499,10 +91619,11 @@
9149991619
** This file contains code used to implement the sqlite3_set_authorizer()
9150091620
** API. This facility is an optional feature of the library. Embedded
9150191621
** systems that do not need this facility may omit it by recompiling
9150291622
** the library with -DSQLITE_OMIT_AUTHORIZATION=1
9150391623
*/
91624
+/* #include "sqliteInt.h" */
9150491625
9150591626
/*
9150691627
** All of the code in this file may be omitted by defining a single
9150791628
** macro.
9150891629
*/
@@ -91769,10 +91890,11 @@
9176991890
** creating ID lists
9177091891
** BEGIN TRANSACTION
9177191892
** COMMIT
9177291893
** ROLLBACK
9177391894
*/
91895
+/* #include "sqliteInt.h" */
9177491896
9177591897
/*
9177691898
** This routine is called when a new SQL statement is beginning to
9177791899
** be parsed. Initialize the pParse structure as needed.
9177891900
*/
@@ -96089,10 +96211,11 @@
9608996211
**
9609096212
** This file contains functions used to access the internal hash tables
9609196213
** of user defined functions and collation sequences.
9609296214
*/
9609396215
96216
+/* #include "sqliteInt.h" */
9609496217
9609596218
/*
9609696219
** Invoke the 'collation needed' callback to request a collation sequence
9609796220
** in the encoding enc of name zName, length nName.
9609896221
*/
@@ -96566,10 +96689,11 @@
9656696689
**
9656796690
*************************************************************************
9656896691
** This file contains C code routines that are called by the parser
9656996692
** in order to generate code for DELETE FROM statements.
9657096693
*/
96694
+/* #include "sqliteInt.h" */
9657196695
9657296696
/*
9657396697
** While a SrcList can in general represent multiple tables and subqueries
9657496698
** (as in the FROM clause of a SELECT statement) in this case it contains
9657596699
** the name of a single table, as one might find in an INSERT, DELETE,
@@ -97408,12 +97532,14 @@
9740897532
*************************************************************************
9740997533
** This file contains the C-language implementations for many of the SQL
9741097534
** functions of SQLite. (Some function, and in particular the date and
9741197535
** time functions, are implemented separately.)
9741297536
*/
97537
+/* #include "sqliteInt.h" */
9741397538
/* #include <stdlib.h> */
9741497539
/* #include <assert.h> */
97540
+/* #include "vdbeInt.h" */
9741597541
9741697542
/*
9741797543
** Return the collating function associated with a function.
9741897544
*/
9741997545
static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
@@ -99205,10 +99331,11 @@
9920599331
**
9920699332
*************************************************************************
9920799333
** This file contains code used by the compiler to add foreign key
9920899334
** support to compiled SQL statements.
9920999335
*/
99336
+/* #include "sqliteInt.h" */
9921099337
9921199338
#ifndef SQLITE_OMIT_FOREIGN_KEY
9921299339
#ifndef SQLITE_OMIT_TRIGGER
9921399340
9921499341
/*
@@ -100609,10 +100736,11 @@
100609100736
**
100610100737
*************************************************************************
100611100738
** This file contains C code routines that are called by the parser
100612100739
** to handle INSERT statements in SQLite.
100613100740
*/
100741
+/* #include "sqliteInt.h" */
100614100742
100615100743
/*
100616100744
** Generate code that will
100617100745
**
100618100746
** (1) acquire a lock for table pTab then
@@ -102671,10 +102799,11 @@
102671102799
** implement the programmer interface to the library. Routines in
102672102800
** other files are for internal use by SQLite and should not be
102673102801
** accessed by users of the library.
102674102802
*/
102675102803
102804
+/* #include "sqliteInt.h" */
102676102805
102677102806
/*
102678102807
** Execute SQL code. Return one of the SQLITE_ success/failure
102679102808
** codes. Also write an error message into memory obtained from
102680102809
** malloc() and make *pzErrMsg point to that message.
@@ -102839,10 +102968,11 @@
102839102968
** as extensions by SQLite should #include this file instead of
102840102969
** sqlite3.h.
102841102970
*/
102842102971
#ifndef _SQLITE3EXT_H_
102843102972
#define _SQLITE3EXT_H_
102973
+/* #include "sqlite3.h" */
102844102974
102845102975
typedef struct sqlite3_api_routines sqlite3_api_routines;
102846102976
102847102977
/*
102848102978
** The following structure holds pointers to all of the SQLite API
@@ -103345,10 +103475,11 @@
103345103475
103346103476
#endif /* _SQLITE3EXT_H_ */
103347103477
103348103478
/************** End of sqlite3ext.h ******************************************/
103349103479
/************** Continuing where we left off in loadext.c ********************/
103480
+/* #include "sqliteInt.h" */
103350103481
/* #include <string.h> */
103351103482
103352103483
#ifndef SQLITE_OMIT_LOAD_EXTENSION
103353103484
103354103485
/*
@@ -104114,10 +104245,11 @@
104114104245
** May you share freely, never taking more than you give.
104115104246
**
104116104247
*************************************************************************
104117104248
** This file contains code used to implement the PRAGMA command.
104118104249
*/
104250
+/* #include "sqliteInt.h" */
104119104251
104120104252
#if !defined(SQLITE_ENABLE_LOCKING_STYLE)
104121104253
# if defined(__APPLE__)
104122104254
# define SQLITE_ENABLE_LOCKING_STYLE 1
104123104255
# else
@@ -104220,11 +104352,11 @@
104220104352
/* ePragFlag: */ 0,
104221104353
/* iArg: */ 0 },
104222104354
#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
104223104355
{ /* zName: */ "cache_size",
104224104356
/* ePragTyp: */ PragTyp_CACHE_SIZE,
104225
- /* ePragFlag: */ PragFlag_NeedSchema,
104357
+ /* ePragFlag: */ 0,
104226104358
/* iArg: */ 0 },
104227104359
#endif
104228104360
#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104229104361
{ /* zName: */ "cache_spill",
104230104362
/* ePragTyp: */ PragTyp_FLAG,
@@ -105290,10 +105422,11 @@
105290105422
** of memory.
105291105423
*/
105292105424
case PragTyp_CACHE_SIZE: {
105293105425
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
105294105426
if( !zRight ){
105427
+ if( sqlite3ReadSchema(pParse) ) goto pragma_out;
105295105428
returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
105296105429
}else{
105297105430
int size = sqlite3Atoi(zRight);
105298105431
pDb->pSchema->cache_size = size;
105299105432
sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
@@ -106565,10 +106698,11 @@
106565106698
*************************************************************************
106566106699
** This file contains the implementation of the sqlite3_prepare()
106567106700
** interface, and routines that contribute to loading the database schema
106568106701
** from disk.
106569106702
*/
106703
+/* #include "sqliteInt.h" */
106570106704
106571106705
/*
106572106706
** Fill the InitData structure with an error message that indicates
106573106707
** that the database is corrupt.
106574106708
*/
@@ -107459,10 +107593,11 @@
107459107593
**
107460107594
*************************************************************************
107461107595
** This file contains C code routines that are called by the parser
107462107596
** to handle SELECT statements in SQLite.
107463107597
*/
107598
+/* #include "sqliteInt.h" */
107464107599
107465107600
/*
107466107601
** Trace output macros
107467107602
*/
107468107603
#if SELECTTRACE_ENABLED
@@ -108528,11 +108663,10 @@
108528108663
}
108529108664
}
108530108665
return pInfo;
108531108666
}
108532108667
108533
-#ifndef SQLITE_OMIT_COMPOUND_SELECT
108534108668
/*
108535108669
** Name of the connection operator, used for error messages.
108536108670
*/
108537108671
static const char *selectOpName(int id){
108538108672
char *z;
@@ -108542,11 +108676,10 @@
108542108676
case TK_EXCEPT: z = "EXCEPT"; break;
108543108677
default: z = "UNION"; break;
108544108678
}
108545108679
return z;
108546108680
}
108547
-#endif /* SQLITE_OMIT_COMPOUND_SELECT */
108548108681
108549108682
#ifndef SQLITE_OMIT_EXPLAIN
108550108683
/*
108551108684
** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
108552108685
** is a no-op. Otherwise, it adds a single row of output to the EQP result,
@@ -109545,23 +109678,10 @@
109545109678
Parse *pParse, /* Parsing context */
109546109679
Select *p, /* The right-most of SELECTs to be coded */
109547109680
SelectDest *pDest /* What to do with query results */
109548109681
);
109549109682
109550
-/*
109551
-** Error message for when two or more terms of a compound select have different
109552
-** size result sets.
109553
-*/
109554
-SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
109555
- if( p->selFlags & SF_Values ){
109556
- sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
109557
- }else{
109558
- sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
109559
- " do not have the same number of result columns", selectOpName(p->op));
109560
- }
109561
-}
109562
-
109563109683
/*
109564109684
** Handle the special case of a compound-select that originates from a
109565109685
** VALUES clause. By handling this as a special case, we avoid deep
109566109686
** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
109567109687
** on a VALUES clause.
@@ -109983,10 +110103,23 @@
109983110103
pDest->nSdst = dest.nSdst;
109984110104
sqlite3SelectDelete(db, pDelete);
109985110105
return rc;
109986110106
}
109987110107
#endif /* SQLITE_OMIT_COMPOUND_SELECT */
110108
+
110109
+/*
110110
+** Error message for when two or more terms of a compound select have different
110111
+** size result sets.
110112
+*/
110113
+SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
110114
+ if( p->selFlags & SF_Values ){
110115
+ sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
110116
+ }else{
110117
+ sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
110118
+ " do not have the same number of result columns", selectOpName(p->op));
110119
+ }
110120
+}
109988110121
109989110122
/*
109990110123
** Code an output subroutine for a coroutine implementation of a
109991110124
** SELECT statment.
109992110125
**
@@ -113077,10 +113210,11 @@
113077113210
** interface routine of sqlite3_exec().
113078113211
**
113079113212
** These routines are in a separate files so that they will not be linked
113080113213
** if they are not used.
113081113214
*/
113215
+/* #include "sqliteInt.h" */
113082113216
/* #include <stdlib.h> */
113083113217
/* #include <string.h> */
113084113218
113085113219
#ifndef SQLITE_OMIT_GET_TABLE
113086113220
@@ -113273,10 +113407,11 @@
113273113407
** May you share freely, never taking more than you give.
113274113408
**
113275113409
*************************************************************************
113276113410
** This file contains the implementation for TRIGGERs
113277113411
*/
113412
+/* #include "sqliteInt.h" */
113278113413
113279113414
#ifndef SQLITE_OMIT_TRIGGER
113280113415
/*
113281113416
** Delete a linked list of TriggerStep structures.
113282113417
*/
@@ -114396,10 +114531,11 @@
114396114531
**
114397114532
*************************************************************************
114398114533
** This file contains C code routines that are called by the parser
114399114534
** to handle UPDATE statements.
114400114535
*/
114536
+/* #include "sqliteInt.h" */
114401114537
114402114538
#ifndef SQLITE_OMIT_VIRTUALTABLE
114403114539
/* Forward declaration */
114404114540
static void updateVirtualTable(
114405114541
Parse *pParse, /* The parsing context */
@@ -115172,10 +115308,12 @@
115172115308
** This file contains code used to implement the VACUUM command.
115173115309
**
115174115310
** Most of the code in this file may be omitted by defining the
115175115311
** SQLITE_OMIT_VACUUM macro.
115176115312
*/
115313
+/* #include "sqliteInt.h" */
115314
+/* #include "vdbeInt.h" */
115177115315
115178115316
#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
115179115317
/*
115180115318
** Finalize a prepared statement. If there was an error, store the
115181115319
** text of the error message in *pzErrMsg. Return the result code.
@@ -115544,10 +115682,11 @@
115544115682
**
115545115683
*************************************************************************
115546115684
** This file contains code used to help implement virtual tables.
115547115685
*/
115548115686
#ifndef SQLITE_OMIT_VIRTUALTABLE
115687
+/* #include "sqliteInt.h" */
115549115688
115550115689
/*
115551115690
** Before a virtual table xCreate() or xConnect() method is invoked, the
115552115691
** sqlite3.pVtabCtx member variable is set to point to an instance of
115553115692
** this struct allocated on the stack. It is used by the implementation of
@@ -116702,10 +116841,11 @@
116702116841
** This file was split off from where.c on 2015-06-06 in order to reduce the
116703116842
** size of where.c and make it easier to edit. This file contains the routines
116704116843
** that actually generate the bulk of the WHERE loop code. The original where.c
116705116844
** file retains the code that does query planning and analysis.
116706116845
*/
116846
+/* #include "sqliteInt.h" */
116707116847
/************** Include whereInt.h in the middle of wherecode.c **************/
116708116848
/************** Begin file whereInt.h ****************************************/
116709116849
/*
116710116850
** 2013-11-12
116711116851
**
@@ -118513,11 +118653,15 @@
118513118653
118514118654
/* Read the PK into an array of temp registers. */
118515118655
r = sqlite3GetTempRange(pParse, nPk);
118516118656
for(iPk=0; iPk<nPk; iPk++){
118517118657
int iCol = pPk->aiColumn[iPk];
118518
- sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
118658
+ int rx;
118659
+ rx = sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur,r+iPk,0);
118660
+ if( rx!=r+iPk ){
118661
+ sqlite3VdbeAddOp2(v, OP_SCopy, rx, r+iPk);
118662
+ }
118519118663
}
118520118664
118521118665
/* Check if the temp table already contains this key. If so,
118522118666
** the row has already been included in the result set and
118523118667
** can be ignored (by jumping past the Gosub below). Otherwise,
@@ -118737,10 +118881,12 @@
118737118881
**
118738118882
** This file was originally part of where.c but was split out to improve
118739118883
** readability and editabiliity. This file contains utility routines for
118740118884
** analyzing Expr objects in the WHERE clause.
118741118885
*/
118886
+/* #include "sqliteInt.h" */
118887
+/* #include "whereInt.h" */
118742118888
118743118889
/* Forward declarations */
118744118890
static void exprAnalyze(SrcList*, WhereClause*, int);
118745118891
118746118892
/*
@@ -119987,10 +120133,12 @@
119987120133
** generating the code that loops through a table looking for applicable
119988120134
** rows. Indices are selected and used to speed the search when doing
119989120135
** so is applicable. Because this module is responsible for selecting
119990120136
** indices, you might also think of this module as the "query optimizer".
119991120137
*/
120138
+/* #include "sqliteInt.h" */
120139
+/* #include "whereInt.h" */
119992120140
119993120141
/* Forward declaration of methods */
119994120142
static int whereLoopResize(sqlite3*, WhereLoop*, int);
119995120143
119996120144
/* Test variable that can be set to enable WHERE tracing */
@@ -124486,10 +124634,11 @@
124486124634
*/
124487124635
/* First off, code is included that follows the "include" declaration
124488124636
** in the input grammar file. */
124489124637
/* #include <stdio.h> */
124490124638
124639
+/* #include "sqliteInt.h" */
124491124640
124492124641
/*
124493124642
** Disable all error recovery processing in the parser push-down
124494124643
** automaton.
124495124644
*/
@@ -128071,10 +128220,11 @@
128071128220
**
128072128221
** This file contains C code that splits an SQL input string up into
128073128222
** individual tokens and sends those tokens one-by-one over to the
128074128223
** parser for analysis.
128075128224
*/
128225
+/* #include "sqliteInt.h" */
128076128226
/* #include <stdlib.h> */
128077128227
128078128228
/*
128079128229
** The charMap() macro maps alphabetic characters into their
128080128230
** lower-case ASCII equivalent. On ASCII machines, this is just
@@ -128880,10 +129030,11 @@
128880129030
** This file contains C code that implements the sqlite3_complete() API.
128881129031
** This code used to be part of the tokenizer.c source file. But by
128882129032
** separating it out, the code will be automatically omitted from
128883129033
** static links that do not use it.
128884129034
*/
129035
+/* #include "sqliteInt.h" */
128885129036
#ifndef SQLITE_OMIT_COMPLETE
128886129037
128887129038
/*
128888129039
** This is defined in tokenize.c. We just have to import the definition.
128889129040
*/
@@ -129170,10 +129321,11 @@
129170129321
** Main file for the SQLite library. The routines in this file
129171129322
** implement the programmer interface to the library. Routines in
129172129323
** other files are for internal use by SQLite and should not be
129173129324
** accessed by users of the library.
129174129325
*/
129326
+/* #include "sqliteInt.h" */
129175129327
129176129328
#ifdef SQLITE_ENABLE_FTS3
129177129329
/************** Include fts3.h in the middle of main.c ***********************/
129178129330
/************** Begin file fts3.h ********************************************/
129179129331
/*
@@ -129189,10 +129341,11 @@
129189129341
******************************************************************************
129190129342
**
129191129343
** This header file is used by programs that want to link against the
129192129344
** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
129193129345
*/
129346
+/* #include "sqlite3.h" */
129194129347
129195129348
#if 0
129196129349
extern "C" {
129197129350
#endif /* __cplusplus */
129198129351
@@ -129221,10 +129374,11 @@
129221129374
******************************************************************************
129222129375
**
129223129376
** This header file is used by programs that want to link against the
129224129377
** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
129225129378
*/
129379
+/* #include "sqlite3.h" */
129226129380
129227129381
#if 0
129228129382
extern "C" {
129229129383
#endif /* __cplusplus */
129230129384
@@ -129253,10 +129407,11 @@
129253129407
******************************************************************************
129254129408
**
129255129409
** This header file is used by programs that want to link against the
129256129410
** ICU extension. All it does is declare the sqlite3IcuInit() interface.
129257129411
*/
129412
+/* #include "sqlite3.h" */
129258129413
129259129414
#if 0
129260129415
extern "C" {
129261129416
#endif /* __cplusplus */
129262129417
@@ -129885,10 +130040,11 @@
129885130040
** space for the lookaside memory is obtained from sqlite3_malloc().
129886130041
** If pStart is not NULL then it is sz*cnt bytes of memory to use for
129887130042
** the lookaside memory.
129888130043
*/
129889130044
static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
130045
+#ifndef SQLITE_OMIT_LOOKASIDE
129890130046
void *pStart;
129891130047
if( db->lookaside.nOut ){
129892130048
return SQLITE_BUSY;
129893130049
}
129894130050
/* Free any existing lookaside buffer for this handle before
@@ -129935,10 +130091,11 @@
129935130091
db->lookaside.pStart = db;
129936130092
db->lookaside.pEnd = db;
129937130093
db->lookaside.bEnabled = 0;
129938130094
db->lookaside.bMalloced = 0;
129939130095
}
130096
+#endif /* SQLITE_OMIT_LOOKASIDE */
129940130097
return SQLITE_OK;
129941130098
}
129942130099
129943130100
/*
129944130101
** Return the mutex associated with a database connection.
@@ -133045,10 +133202,12 @@
133045133202
*************************************************************************
133046133203
**
133047133204
** This file contains the implementation of the sqlite3_unlock_notify()
133048133205
** API method and its associated functionality.
133049133206
*/
133207
+/* #include "sqliteInt.h" */
133208
+/* #include "btreeInt.h" */
133050133209
133051133210
/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
133052133211
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
133053133212
133054133213
/*
@@ -133688,13 +133847,15 @@
133688133847
133689133848
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
133690133849
133691133850
/* If not building as part of the core, include sqlite3ext.h. */
133692133851
#ifndef SQLITE_CORE
133852
+/* # include "sqlite3ext.h" */
133693133853
SQLITE_EXTENSION_INIT3
133694133854
#endif
133695133855
133856
+/* #include "sqlite3.h" */
133696133857
/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
133697133858
/************** Begin file fts3_tokenizer.h **********************************/
133698133859
/*
133699133860
** 2006 July 10
133700133861
**
@@ -133719,10 +133880,11 @@
133719133880
133720133881
/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
133721133882
** If tokenizers are to be allowed to call sqlite3_*() functions, then
133722133883
** we will need a way to register the API consistently.
133723133884
*/
133885
+/* #include "sqlite3.h" */
133724133886
133725133887
/*
133726133888
** Structures used by the tokenizer interface. When a new tokenizer
133727133889
** implementation is registered, the caller provides a pointer to
133728133890
** an sqlite3_tokenizer_module containing pointers to the callback
@@ -134559,11 +134721,13 @@
134559134721
/* #include <stddef.h> */
134560134722
/* #include <stdio.h> */
134561134723
/* #include <string.h> */
134562134724
/* #include <stdarg.h> */
134563134725
134726
+/* #include "fts3.h" */
134564134727
#ifndef SQLITE_CORE
134728
+/* # include "sqlite3ext.h" */
134565134729
SQLITE_EXTENSION_INIT1
134566134730
#endif
134567134731
134568134732
static int fts3EvalNext(Fts3Cursor *pCsr);
134569134733
static int fts3EvalStart(Fts3Cursor *pCsr);
@@ -138606,10 +138770,11 @@
138606138770
if( p==0 ){
138607138771
p = aDoclist;
138608138772
p += sqlite3Fts3GetVarint(p, piDocid);
138609138773
}else{
138610138774
fts3PoslistCopy(0, &p);
138775
+ while( p<&aDoclist[nDoclist] && *p==0 ) p++;
138611138776
if( p>=&aDoclist[nDoclist] ){
138612138777
*pbEof = 1;
138613138778
}else{
138614138779
sqlite3_int64 iVar;
138615138780
p += sqlite3Fts3GetVarint(p, &iVar);
@@ -140012,14 +140177,14 @@
140012140177
pIter = pPhrase->doclist.pList;
140013140178
if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
140014140179
int rc = SQLITE_OK;
140015140180
int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
140016140181
int bOr = 0;
140017
- u8 bEof = 0;
140018140182
u8 bTreeEof = 0;
140019140183
Fts3Expr *p; /* Used to iterate from pExpr to root */
140020140184
Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
140185
+ int bMatch;
140021140186
140022140187
/* Check if this phrase descends from an OR expression node. If not,
140023140188
** return NULL. Otherwise, the entry that corresponds to docid
140024140189
** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
140025140190
** tree that the node is part of has been marked as EOF, but the node
@@ -140049,35 +140214,51 @@
140049140214
fts3EvalNextRow(pCsr, pNear, &rc);
140050140215
}
140051140216
}
140052140217
if( rc!=SQLITE_OK ) return rc;
140053140218
140054
- pIter = pPhrase->pOrPoslist;
140055
- iDocid = pPhrase->iOrDocid;
140056
- if( pCsr->bDesc==bDescDoclist ){
140057
- bEof = !pPhrase->doclist.nAll ||
140058
- (pIter >= (pPhrase->doclist.aAll + pPhrase->doclist.nAll));
140059
- while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
140060
- sqlite3Fts3DoclistNext(
140061
- bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll,
140062
- &pIter, &iDocid, &bEof
140063
- );
140064
- }
140065
- }else{
140066
- bEof = !pPhrase->doclist.nAll || (pIter && pIter<=pPhrase->doclist.aAll);
140067
- while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
140068
- int dummy;
140069
- sqlite3Fts3DoclistPrev(
140070
- bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll,
140071
- &pIter, &iDocid, &dummy, &bEof
140072
- );
140073
- }
140074
- }
140075
- pPhrase->pOrPoslist = pIter;
140076
- pPhrase->iOrDocid = iDocid;
140077
-
140078
- if( bEof || iDocid!=pCsr->iPrevId ) pIter = 0;
140219
+ bMatch = 1;
140220
+ for(p=pNear; p; p=p->pLeft){
140221
+ u8 bEof = 0;
140222
+ Fts3Expr *pTest = p;
140223
+ Fts3Phrase *pPh;
140224
+ assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
140225
+ if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
140226
+ assert( pTest->eType==FTSQUERY_PHRASE );
140227
+ pPh = pTest->pPhrase;
140228
+
140229
+ pIter = pPh->pOrPoslist;
140230
+ iDocid = pPh->iOrDocid;
140231
+ if( pCsr->bDesc==bDescDoclist ){
140232
+ bEof = !pPh->doclist.nAll ||
140233
+ (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
140234
+ while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
140235
+ sqlite3Fts3DoclistNext(
140236
+ bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
140237
+ &pIter, &iDocid, &bEof
140238
+ );
140239
+ }
140240
+ }else{
140241
+ bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
140242
+ while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
140243
+ int dummy;
140244
+ sqlite3Fts3DoclistPrev(
140245
+ bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
140246
+ &pIter, &iDocid, &dummy, &bEof
140247
+ );
140248
+ }
140249
+ }
140250
+ pPh->pOrPoslist = pIter;
140251
+ pPh->iOrDocid = iDocid;
140252
+ if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
140253
+ }
140254
+
140255
+ if( bMatch ){
140256
+ pIter = pPhrase->pOrPoslist;
140257
+ }else{
140258
+ pIter = 0;
140259
+ }
140079140260
}
140080140261
if( pIter==0 ) return SQLITE_OK;
140081140262
140082140263
if( *pIter==0x01 ){
140083140264
pIter++;
@@ -140161,10 +140342,11 @@
140161140342
** May you share freely, never taking more than you give.
140162140343
**
140163140344
******************************************************************************
140164140345
**
140165140346
*/
140347
+/* #include "fts3Int.h" */
140166140348
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
140167140349
140168140350
/* #include <string.h> */
140169140351
/* #include <assert.h> */
140170140352
@@ -140717,10 +140899,11 @@
140717140899
** This module contains code that implements a parser for fts3 query strings
140718140900
** (the right-hand argument to the MATCH operator). Because the supported
140719140901
** syntax is relatively simple, the whole tokenizer/parser system is
140720140902
** hand-coded.
140721140903
*/
140904
+/* #include "fts3Int.h" */
140722140905
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
140723140906
140724140907
/*
140725140908
** By default, this module parses the legacy syntax that has been
140726140909
** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
@@ -142010,16 +142193,18 @@
142010142193
** (in which case SQLITE_CORE is not defined), or
142011142194
**
142012142195
** * The FTS3 module is being built into the core of
142013142196
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
142014142197
*/
142198
+/* #include "fts3Int.h" */
142015142199
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
142016142200
142017142201
/* #include <assert.h> */
142018142202
/* #include <stdlib.h> */
142019142203
/* #include <string.h> */
142020142204
142205
+/* #include "fts3_hash.h" */
142021142206
142022142207
/*
142023142208
** Malloc and Free functions
142024142209
*/
142025142210
static void *fts3HashMalloc(int n){
@@ -142393,17 +142578,19 @@
142393142578
** (in which case SQLITE_CORE is not defined), or
142394142579
**
142395142580
** * The FTS3 module is being built into the core of
142396142581
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
142397142582
*/
142583
+/* #include "fts3Int.h" */
142398142584
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
142399142585
142400142586
/* #include <assert.h> */
142401142587
/* #include <stdlib.h> */
142402142588
/* #include <stdio.h> */
142403142589
/* #include <string.h> */
142404142590
142591
+/* #include "fts3_tokenizer.h" */
142405142592
142406142593
/*
142407142594
** Class derived from sqlite3_tokenizer
142408142595
*/
142409142596
typedef struct porter_tokenizer {
@@ -143057,10 +143244,11 @@
143057143244
** (in which case SQLITE_CORE is not defined), or
143058143245
**
143059143246
** * The FTS3 module is being built into the core of
143060143247
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
143061143248
*/
143249
+/* #include "fts3Int.h" */
143062143250
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
143063143251
143064143252
/* #include <assert.h> */
143065143253
/* #include <string.h> */
143066143254
@@ -143552,17 +143740,19 @@
143552143740
** (in which case SQLITE_CORE is not defined), or
143553143741
**
143554143742
** * The FTS3 module is being built into the core of
143555143743
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
143556143744
*/
143745
+/* #include "fts3Int.h" */
143557143746
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
143558143747
143559143748
/* #include <assert.h> */
143560143749
/* #include <stdlib.h> */
143561143750
/* #include <stdio.h> */
143562143751
/* #include <string.h> */
143563143752
143753
+/* #include "fts3_tokenizer.h" */
143564143754
143565143755
typedef struct simple_tokenizer {
143566143756
sqlite3_tokenizer base;
143567143757
char delim[128]; /* flag ASCII delimiters */
143568143758
} simple_tokenizer;
@@ -143803,10 +143993,11 @@
143803143993
** end: Byte offset of the byte immediately following the end of the
143804143994
** token within the input string.
143805143995
** pos: Token offset of token within input.
143806143996
**
143807143997
*/
143998
+/* #include "fts3Int.h" */
143808143999
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
143809144000
143810144001
/* #include <string.h> */
143811144002
/* #include <assert.h> */
143812144003
@@ -144238,10 +144429,11 @@
144238144429
** tables. It also contains code to merge FTS3 b-tree segments. Some
144239144430
** of the sub-routines used to merge segments are also used by the query
144240144431
** code in fts3.c.
144241144432
*/
144242144433
144434
+/* #include "fts3Int.h" */
144243144435
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
144244144436
144245144437
/* #include <string.h> */
144246144438
/* #include <assert.h> */
144247144439
/* #include <stdlib.h> */
@@ -149898,10 +150090,11 @@
149898150090
** May you share freely, never taking more than you give.
149899150091
**
149900150092
******************************************************************************
149901150093
*/
149902150094
150095
+/* #include "fts3Int.h" */
149903150096
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
149904150097
149905150098
/* #include <string.h> */
149906150099
/* #include <assert.h> */
149907150100
@@ -151610,17 +151803,19 @@
151610151803
** Implementation of the "unicode" full-text-search tokenizer.
151611151804
*/
151612151805
151613151806
#ifndef SQLITE_DISABLE_FTS3_UNICODE
151614151807
151808
+/* #include "fts3Int.h" */
151615151809
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
151616151810
151617151811
/* #include <assert.h> */
151618151812
/* #include <stdlib.h> */
151619151813
/* #include <stdio.h> */
151620151814
/* #include <string.h> */
151621151815
151816
+/* #include "fts3_tokenizer.h" */
151622151817
151623151818
/*
151624151819
** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
151625151820
** from the sqlite3 source file utf.c. If this file is compiled as part
151626151821
** of the amalgamation, they are not required.
@@ -152412,12 +152607,14 @@
152412152607
*/
152413152608
152414152609
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
152415152610
152416152611
#ifndef SQLITE_CORE
152612
+/* #include "sqlite3ext.h" */
152417152613
SQLITE_EXTENSION_INIT1
152418152614
#else
152615
+/* #include "sqlite3.h" */
152419152616
#endif
152420152617
152421152618
/* #include <string.h> */
152422152619
/* #include <assert.h> */
152423152620
/* #include <stdio.h> */
@@ -155909,12 +156106,14 @@
155909156106
#include <unicode/ucol.h>
155910156107
155911156108
/* #include <assert.h> */
155912156109
155913156110
#ifndef SQLITE_CORE
156111
+/* #include "sqlite3ext.h" */
155914156112
SQLITE_EXTENSION_INIT1
155915156113
#else
156114
+/* #include "sqlite3.h" */
155916156115
#endif
155917156116
155918156117
/*
155919156118
** Maximum length (in bytes) of the pattern in a LIKE or GLOB
155920156119
** operator.
@@ -156386,15 +156585,17 @@
156386156585
** May you share freely, never taking more than you give.
156387156586
**
156388156587
*************************************************************************
156389156588
** This file implements a tokenizer for fts3 based on the ICU library.
156390156589
*/
156590
+/* #include "fts3Int.h" */
156391156591
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
156392156592
#ifdef SQLITE_ENABLE_ICU
156393156593
156394156594
/* #include <assert.h> */
156395156595
/* #include <string.h> */
156596
+/* #include "fts3_tokenizer.h" */
156396156597
156397156598
#include <unicode/ubrk.h>
156398156599
/* #include <unicode/ucol.h> */
156399156600
/* #include <unicode/ustring.h> */
156400156601
#include <unicode/utf16.h>
@@ -156635,11 +156836,11 @@
156635156836
156636156837
#endif /* defined(SQLITE_ENABLE_ICU) */
156637156838
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
156638156839
156639156840
/************** End of fts3_icu.c ********************************************/
156640
-/************** Begin file sqlite3ota.c **************************************/
156841
+/************** Begin file sqlite3rbu.c **************************************/
156641156842
/*
156642156843
** 2014 August 30
156643156844
**
156644156845
** The author disclaims copyright to this source code. In place of
156645156846
** a legal notice, here is a blessing:
@@ -156651,15 +156852,15 @@
156651156852
*************************************************************************
156652156853
**
156653156854
**
156654156855
** OVERVIEW
156655156856
**
156656
-** The OTA extension requires that the OTA update be packaged as an
156857
+** The RBU extension requires that the RBU update be packaged as an
156657156858
** SQLite database. The tables it expects to find are described in
156658
-** sqlite3ota.h. Essentially, for each table xyz in the target database
156859
+** sqlite3rbu.h. Essentially, for each table xyz in the target database
156659156860
** that the user wishes to write to, a corresponding data_xyz table is
156660
-** created in the OTA database and populated with one row for each row to
156861
+** created in the RBU database and populated with one row for each row to
156661156862
** update, insert or delete from the target table.
156662156863
**
156663156864
** The update proceeds in three stages:
156664156865
**
156665156866
** 1) The database is updated. The modified database pages are written
@@ -156667,70 +156868,71 @@
156667156868
** that it is named "<database>-oal" instead of "<database>-wal".
156668156869
** Because regular SQLite clients do not look for file named
156669156870
** "<database>-oal", they go on using the original database in
156670156871
** rollback mode while the *-oal file is being generated.
156671156872
**
156672
-** During this stage OTA does not update the database by writing
156873
+** During this stage RBU does not update the database by writing
156673156874
** directly to the target tables. Instead it creates "imposter"
156674156875
** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
156675156876
** to update each b-tree individually. All updates required by each
156676156877
** b-tree are completed before moving on to the next, and all
156677156878
** updates are done in sorted key order.
156678156879
**
156679156880
** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
156680
-** location using a call to rename(2). Before doing this the OTA
156881
+** location using a call to rename(2). Before doing this the RBU
156681156882
** module takes an EXCLUSIVE lock on the database file, ensuring
156682156883
** that there are no other active readers.
156683156884
**
156684156885
** Once the EXCLUSIVE lock is released, any other database readers
156685156886
** detect the new *-wal file and read the database in wal mode. At
156686156887
** this point they see the new version of the database - including
156687
-** the updates made as part of the OTA update.
156888
+** the updates made as part of the RBU update.
156688156889
**
156689156890
** 3) The new *-wal file is checkpointed. This proceeds in the same way
156690156891
** as a regular database checkpoint, except that a single frame is
156691
-** checkpointed each time sqlite3ota_step() is called. If the OTA
156892
+** checkpointed each time sqlite3rbu_step() is called. If the RBU
156692156893
** handle is closed before the entire *-wal file is checkpointed,
156693
-** the checkpoint progress is saved in the OTA database and the
156694
-** checkpoint can be resumed by another OTA client at some point in
156894
+** the checkpoint progress is saved in the RBU database and the
156895
+** checkpoint can be resumed by another RBU client at some point in
156695156896
** the future.
156696156897
**
156697156898
** POTENTIAL PROBLEMS
156698156899
**
156699
-** The rename() call might not be portable. And OTA is not currently
156900
+** The rename() call might not be portable. And RBU is not currently
156700156901
** syncing the directory after renaming the file.
156701156902
**
156702156903
** When state is saved, any commit to the *-oal file and the commit to
156703
-** the OTA update database are not atomic. So if the power fails at the
156904
+** the RBU update database are not atomic. So if the power fails at the
156704156905
** wrong moment they might get out of sync. As the main database will be
156705
-** committed before the OTA update database this will likely either just
156906
+** committed before the RBU update database this will likely either just
156706156907
** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
156707156908
** constraint violations).
156708156909
**
156709
-** If some client does modify the target database mid OTA update, or some
156710
-** other error occurs, the OTA extension will keep throwing errors. It's
156910
+** If some client does modify the target database mid RBU update, or some
156911
+** other error occurs, the RBU extension will keep throwing errors. It's
156711156912
** not really clear how to get out of this state. The system could just
156712
-** by delete the OTA update database and *-oal file and have the device
156913
+** by delete the RBU update database and *-oal file and have the device
156713156914
** download the update again and start over.
156714156915
**
156715156916
** At present, for an UPDATE, both the new.* and old.* records are
156716
-** collected in the ota_xyz table. And for both UPDATEs and DELETEs all
156917
+** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
156717156918
** fields are collected. This means we're probably writing a lot more
156718
-** data to disk when saving the state of an ongoing update to the OTA
156919
+** data to disk when saving the state of an ongoing update to the RBU
156719156920
** update database than is strictly necessary.
156720156921
**
156721156922
*/
156722156923
156723156924
/* #include <assert.h> */
156724156925
/* #include <string.h> */
156725156926
/* #include <stdio.h> */
156726156927
/* #include <unistd.h> */
156727156928
156929
+/* #include "sqlite3.h" */
156728156930
156729
-#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA)
156730
-/************** Include sqlite3ota.h in the middle of sqlite3ota.c ***********/
156731
-/************** Begin file sqlite3ota.h **************************************/
156931
+#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
156932
+/************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
156933
+/************** Begin file sqlite3rbu.h **************************************/
156732156934
/*
156733156935
** 2014 August 30
156734156936
**
156735156937
** The author disclaims copyright to this source code. In place of
156736156938
** a legal notice, here is a blessing:
@@ -156739,11 +156941,11 @@
156739156941
** May you find forgiveness for yourself and forgive others.
156740156942
** May you share freely, never taking more than you give.
156741156943
**
156742156944
*************************************************************************
156743156945
**
156744
-** This file contains the public interface for the OTA extension.
156946
+** This file contains the public interface for the RBU extension.
156745156947
*/
156746156948
156747156949
/*
156748156950
** SUMMARY
156749156951
**
@@ -156776,18 +156978,18 @@
156776156978
** mobile device that is frequently rebooted. Even after the writer process
156777156979
** has committed one or more sub-transactions, other database clients continue
156778156980
** to read from the original database snapshot. In other words, partially
156779156981
** applied transactions are not visible to other clients.
156780156982
**
156781
-** "OTA" stands for "Over The Air" update. As in a large database update
156983
+** "RBU" stands for "Over The Air" update. As in a large database update
156782156984
** transmitted via a wireless network to a mobile device. A transaction
156783
-** applied using this extension is hence refered to as an "OTA update".
156985
+** applied using this extension is hence refered to as an "RBU update".
156784156986
**
156785156987
**
156786156988
** LIMITATIONS
156787156989
**
156788
-** An "OTA update" transaction is subject to the following limitations:
156990
+** An "RBU update" transaction is subject to the following limitations:
156789156991
**
156790156992
** * The transaction must consist of INSERT, UPDATE and DELETE operations
156791156993
** only.
156792156994
**
156793156995
** * INSERT statements may not use any default values.
@@ -156808,430 +157010,430 @@
156808157010
** * No constraint handling mode except for "OR ROLLBACK" is supported.
156809157011
**
156810157012
**
156811157013
** PREPARATION
156812157014
**
156813
-** An "OTA update" is stored as a separate SQLite database. A database
156814
-** containing an OTA update is an "OTA database". For each table in the
156815
-** target database to be updated, the OTA database should contain a table
157015
+** An "RBU update" is stored as a separate SQLite database. A database
157016
+** containing an RBU update is an "RBU database". For each table in the
157017
+** target database to be updated, the RBU database should contain a table
156816157018
** named "data_<target name>" containing the same set of columns as the
156817
-** target table, and one more - "ota_control". The data_% table should
157019
+** target table, and one more - "rbu_control". The data_% table should
156818157020
** have no PRIMARY KEY or UNIQUE constraints, but each column should have
156819157021
** the same type as the corresponding column in the target database.
156820
-** The "ota_control" column should have no type at all. For example, if
157022
+** The "rbu_control" column should have no type at all. For example, if
156821157023
** the target database contains:
156822157024
**
156823157025
** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
156824157026
**
156825
-** Then the OTA database should contain:
157027
+** Then the RBU database should contain:
156826157028
**
156827
-** CREATE TABLE data_t1(a INTEGER, b TEXT, c, ota_control);
157029
+** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
156828157030
**
156829157031
** The order of the columns in the data_% table does not matter.
156830157032
**
156831157033
** If the target database table is a virtual table or a table that has no
156832157034
** PRIMARY KEY declaration, the data_% table must also contain a column
156833
-** named "ota_rowid". This column is mapped to the tables implicit primary
157035
+** named "rbu_rowid". This column is mapped to the tables implicit primary
156834157036
** key column - "rowid". Virtual tables for which the "rowid" column does
156835
-** not function like a primary key value cannot be updated using OTA. For
157037
+** not function like a primary key value cannot be updated using RBU. For
156836157038
** example, if the target db contains either of the following:
156837157039
**
156838157040
** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
156839157041
** CREATE TABLE x1(a, b)
156840157042
**
156841
-** then the OTA database should contain:
157043
+** then the RBU database should contain:
156842157044
**
156843
-** CREATE TABLE data_x1(a, b, ota_rowid, ota_control);
157045
+** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
156844157046
**
156845157047
** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
156846157048
** target table must be present in the input table. For virtual tables,
156847
-** hidden columns are optional - they are updated by OTA if present in
157049
+** hidden columns are optional - they are updated by RBU if present in
156848157050
** the input table, or not otherwise. For example, to write to an fts4
156849157051
** table with a hidden languageid column such as:
156850157052
**
156851157053
** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
156852157054
**
156853157055
** Either of the following input table schemas may be used:
156854157056
**
156855
-** CREATE TABLE data_ft1(a, b, langid, ota_rowid, ota_control);
156856
-** CREATE TABLE data_ft1(a, b, ota_rowid, ota_control);
157057
+** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
157058
+** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
156857157059
**
156858
-** For each row to INSERT into the target database as part of the OTA
157060
+** For each row to INSERT into the target database as part of the RBU
156859157061
** update, the corresponding data_% table should contain a single record
156860
-** with the "ota_control" column set to contain integer value 0. The
157062
+** with the "rbu_control" column set to contain integer value 0. The
156861157063
** other columns should be set to the values that make up the new record
156862157064
** to insert.
156863157065
**
156864157066
** If the target database table has an INTEGER PRIMARY KEY, it is not
156865157067
** possible to insert a NULL value into the IPK column. Attempting to
156866157068
** do so results in an SQLITE_MISMATCH error.
156867157069
**
156868
-** For each row to DELETE from the target database as part of the OTA
157070
+** For each row to DELETE from the target database as part of the RBU
156869157071
** update, the corresponding data_% table should contain a single record
156870
-** with the "ota_control" column set to contain integer value 1. The
157072
+** with the "rbu_control" column set to contain integer value 1. The
156871157073
** real primary key values of the row to delete should be stored in the
156872157074
** corresponding columns of the data_% table. The values stored in the
156873157075
** other columns are not used.
156874157076
**
156875
-** For each row to UPDATE from the target database as part of the OTA
157077
+** For each row to UPDATE from the target database as part of the RBU
156876157078
** update, the corresponding data_% table should contain a single record
156877
-** with the "ota_control" column set to contain a value of type text.
157079
+** with the "rbu_control" column set to contain a value of type text.
156878157080
** The real primary key values identifying the row to update should be
156879157081
** stored in the corresponding columns of the data_% table row, as should
156880157082
** the new values of all columns being update. The text value in the
156881
-** "ota_control" column must contain the same number of characters as
157083
+** "rbu_control" column must contain the same number of characters as
156882157084
** there are columns in the target database table, and must consist entirely
156883157085
** of 'x' and '.' characters (or in some special cases 'd' - see below). For
156884157086
** each column that is being updated, the corresponding character is set to
156885157087
** 'x'. For those that remain as they are, the corresponding character of the
156886
-** ota_control value should be set to '.'. For example, given the tables
157088
+** rbu_control value should be set to '.'. For example, given the tables
156887157089
** above, the update statement:
156888157090
**
156889157091
** UPDATE t1 SET c = 'usa' WHERE a = 4;
156890157092
**
156891157093
** is represented by the data_t1 row created by:
156892157094
**
156893
-** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..x');
157095
+** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
156894157096
**
156895
-** Instead of an 'x' character, characters of the ota_control value specified
157097
+** Instead of an 'x' character, characters of the rbu_control value specified
156896157098
** for UPDATEs may also be set to 'd'. In this case, instead of updating the
156897157099
** target table with the value stored in the corresponding data_% column, the
156898
-** user-defined SQL function "ota_delta()" is invoked and the result stored in
156899
-** the target table column. ota_delta() is invoked with two arguments - the
157100
+** user-defined SQL function "rbu_delta()" is invoked and the result stored in
157101
+** the target table column. rbu_delta() is invoked with two arguments - the
156900157102
** original value currently stored in the target table column and the
156901157103
** value specified in the data_xxx table.
156902157104
**
156903157105
** For example, this row:
156904157106
**
156905
-** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..d');
157107
+** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
156906157108
**
156907157109
** is similar to an UPDATE statement such as:
156908157110
**
156909
-** UPDATE t1 SET c = ota_delta(c, 'usa') WHERE a = 4;
157111
+** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
156910157112
**
156911157113
** If the target database table is a virtual table or a table with no PRIMARY
156912
-** KEY, the ota_control value should not include a character corresponding
156913
-** to the ota_rowid value. For example, this:
157114
+** KEY, the rbu_control value should not include a character corresponding
157115
+** to the rbu_rowid value. For example, this:
156914157116
**
156915
-** INSERT INTO data_ft1(a, b, ota_rowid, ota_control)
157117
+** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
156916157118
** VALUES(NULL, 'usa', 12, '.x');
156917157119
**
156918157120
** causes a result similar to:
156919157121
**
156920157122
** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
156921157123
**
156922157124
** The data_xxx tables themselves should have no PRIMARY KEY declarations.
156923
-** However, OTA is more efficient if reading the rows in from each data_xxx
157125
+** However, RBU is more efficient if reading the rows in from each data_xxx
156924157126
** table in "rowid" order is roughly the same as reading them sorted by
156925157127
** the PRIMARY KEY of the corresponding target database table. In other
156926157128
** words, rows should be sorted using the destination table PRIMARY KEY
156927157129
** fields before they are inserted into the data_xxx tables.
156928157130
**
156929157131
** USAGE
156930157132
**
156931
-** The API declared below allows an application to apply an OTA update
157133
+** The API declared below allows an application to apply an RBU update
156932157134
** stored on disk to an existing target database. Essentially, the
156933157135
** application:
156934157136
**
156935
-** 1) Opens an OTA handle using the sqlite3ota_open() function.
157137
+** 1) Opens an RBU handle using the sqlite3rbu_open() function.
156936157138
**
156937157139
** 2) Registers any required virtual table modules with the database
156938
-** handle returned by sqlite3ota_db(). Also, if required, register
156939
-** the ota_delta() implementation.
157140
+** handle returned by sqlite3rbu_db(). Also, if required, register
157141
+** the rbu_delta() implementation.
156940157142
**
156941
-** 3) Calls the sqlite3ota_step() function one or more times on
156942
-** the new handle. Each call to sqlite3ota_step() performs a single
157143
+** 3) Calls the sqlite3rbu_step() function one or more times on
157144
+** the new handle. Each call to sqlite3rbu_step() performs a single
156943157145
** b-tree operation, so thousands of calls may be required to apply
156944157146
** a complete update.
156945157147
**
156946
-** 4) Calls sqlite3ota_close() to close the OTA update handle. If
156947
-** sqlite3ota_step() has been called enough times to completely
156948
-** apply the update to the target database, then the OTA database
156949
-** is marked as fully applied. Otherwise, the state of the OTA
156950
-** update application is saved in the OTA database for later
157148
+** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
157149
+** sqlite3rbu_step() has been called enough times to completely
157150
+** apply the update to the target database, then the RBU database
157151
+** is marked as fully applied. Otherwise, the state of the RBU
157152
+** update application is saved in the RBU database for later
156951157153
** resumption.
156952157154
**
156953157155
** See comments below for more detail on APIs.
156954157156
**
156955157157
** If an update is only partially applied to the target database by the
156956
-** time sqlite3ota_close() is called, various state information is saved
156957
-** within the OTA database. This allows subsequent processes to automatically
156958
-** resume the OTA update from where it left off.
157158
+** time sqlite3rbu_close() is called, various state information is saved
157159
+** within the RBU database. This allows subsequent processes to automatically
157160
+** resume the RBU update from where it left off.
156959157161
**
156960
-** To remove all OTA extension state information, returning an OTA database
157162
+** To remove all RBU extension state information, returning an RBU database
156961157163
** to its original contents, it is sufficient to drop all tables that begin
156962
-** with the prefix "ota_"
157164
+** with the prefix "rbu_"
156963157165
**
156964157166
** DATABASE LOCKING
156965157167
**
156966
-** An OTA update may not be applied to a database in WAL mode. Attempting
157168
+** An RBU update may not be applied to a database in WAL mode. Attempting
156967157169
** to do so is an error (SQLITE_ERROR).
156968157170
**
156969
-** While an OTA handle is open, a SHARED lock may be held on the target
157171
+** While an RBU handle is open, a SHARED lock may be held on the target
156970157172
** database file. This means it is possible for other clients to read the
156971157173
** database, but not to write it.
156972157174
**
156973
-** If an OTA update is started and then suspended before it is completed,
157175
+** If an RBU update is started and then suspended before it is completed,
156974157176
** then an external client writes to the database, then attempting to resume
156975
-** the suspended OTA update is also an error (SQLITE_BUSY).
157177
+** the suspended RBU update is also an error (SQLITE_BUSY).
156976157178
*/
156977157179
156978
-#ifndef _SQLITE3OTA_H
156979
-#define _SQLITE3OTA_H
157180
+#ifndef _SQLITE3RBU_H
157181
+#define _SQLITE3RBU_H
156980157182
157183
+/* #include "sqlite3.h" ** Required for error code definitions ** */
156981157184
156982
-typedef struct sqlite3ota sqlite3ota;
157185
+typedef struct sqlite3rbu sqlite3rbu;
156983157186
156984157187
/*
156985
-** Open an OTA handle.
157188
+** Open an RBU handle.
156986157189
**
156987
-** Argument zTarget is the path to the target database. Argument zOta is
156988
-** the path to the OTA database. Each call to this function must be matched
156989
-** by a call to sqlite3ota_close(). When opening the databases, OTA passes
157190
+** Argument zTarget is the path to the target database. Argument zRbu is
157191
+** the path to the RBU database. Each call to this function must be matched
157192
+** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
156990157193
** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
156991
-** or zOta begin with "file:", it will be interpreted as an SQLite
157194
+** or zRbu begin with "file:", it will be interpreted as an SQLite
156992157195
** database URI, not a regular file name.
156993157196
**
156994
-** If the zState argument is passed a NULL value, the OTA extension stores
157197
+** If the zState argument is passed a NULL value, the RBU extension stores
156995157198
** the current state of the update (how many rows have been updated, which
156996
-** indexes are yet to be updated etc.) within the OTA database itself. This
156997
-** can be convenient, as it means that the OTA application does not need to
157199
+** indexes are yet to be updated etc.) within the RBU database itself. This
157200
+** can be convenient, as it means that the RBU application does not need to
156998157201
** organize removing a separate state file after the update is concluded.
156999157202
** Or, if zState is non-NULL, it must be a path to a database file in which
157000
-** the OTA extension can store the state of the update.
157203
+** the RBU extension can store the state of the update.
157001157204
**
157002
-** When resuming an OTA update, the zState argument must be passed the same
157003
-** value as when the OTA update was started.
157205
+** When resuming an RBU update, the zState argument must be passed the same
157206
+** value as when the RBU update was started.
157004157207
**
157005
-** Once the OTA update is finished, the OTA extension does not
157208
+** Once the RBU update is finished, the RBU extension does not
157006157209
** automatically remove any zState database file, even if it created it.
157007157210
**
157008
-** By default, OTA uses the default VFS to access the files on disk. To
157211
+** By default, RBU uses the default VFS to access the files on disk. To
157009157212
** use a VFS other than the default, an SQLite "file:" URI containing a
157010157213
** "vfs=..." option may be passed as the zTarget option.
157011157214
**
157012
-** IMPORTANT NOTE FOR ZIPVFS USERS: The OTA extension works with all of
157215
+** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
157013157216
** SQLite's built-in VFSs, including the multiplexor VFS. However it does
157014157217
** not work out of the box with zipvfs. Refer to the comment describing
157015
-** the zipvfs_create_vfs() API below for details on using OTA with zipvfs.
157218
+** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
157016157219
*/
157017
-SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
157220
+SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_open(
157018157221
const char *zTarget,
157019
- const char *zOta,
157222
+ const char *zRbu,
157020157223
const char *zState
157021157224
);
157022157225
157023157226
/*
157024
-** Internally, each OTA connection uses a separate SQLite database
157025
-** connection to access the target and ota update databases. This
157227
+** Internally, each RBU connection uses a separate SQLite database
157228
+** connection to access the target and rbu update databases. This
157026157229
** API allows the application direct access to these database handles.
157027157230
**
157028
-** The first argument passed to this function must be a valid, open, OTA
157231
+** The first argument passed to this function must be a valid, open, RBU
157029157232
** handle. The second argument should be passed zero to access the target
157030
-** database handle, or non-zero to access the ota update database handle.
157233
+** database handle, or non-zero to access the rbu update database handle.
157031157234
** Accessing the underlying database handles may be useful in the
157032157235
** following scenarios:
157033157236
**
157034157237
** * If any target tables are virtual tables, it may be necessary to
157035157238
** call sqlite3_create_module() on the target database handle to
157036157239
** register the required virtual table implementations.
157037157240
**
157038
-** * If the data_xxx tables in the OTA source database are virtual
157241
+** * If the data_xxx tables in the RBU source database are virtual
157039157242
** tables, the application may need to call sqlite3_create_module() on
157040
-** the ota update db handle to any required virtual table
157243
+** the rbu update db handle to any required virtual table
157041157244
** implementations.
157042157245
**
157043
-** * If the application uses the "ota_delta()" feature described above,
157246
+** * If the application uses the "rbu_delta()" feature described above,
157044157247
** it must use sqlite3_create_function() or similar to register the
157045
-** ota_delta() implementation with the target database handle.
157248
+** rbu_delta() implementation with the target database handle.
157046157249
**
157047
-** If an error has occurred, either while opening or stepping the OTA object,
157250
+** If an error has occurred, either while opening or stepping the RBU object,
157048157251
** this function may return NULL. The error code and message may be collected
157049
-** when sqlite3ota_close() is called.
157252
+** when sqlite3rbu_close() is called.
157050157253
*/
157051
-SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota*, int bOta);
157254
+SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3rbu_db(sqlite3rbu*, int bRbu);
157052157255
157053157256
/*
157054
-** Do some work towards applying the OTA update to the target db.
157257
+** Do some work towards applying the RBU update to the target db.
157055157258
**
157056157259
** Return SQLITE_DONE if the update has been completely applied, or
157057157260
** SQLITE_OK if no error occurs but there remains work to do to apply
157058
-** the OTA update. If an error does occur, some other error code is
157261
+** the RBU update. If an error does occur, some other error code is
157059157262
** returned.
157060157263
**
157061
-** Once a call to sqlite3ota_step() has returned a value other than
157062
-** SQLITE_OK, all subsequent calls on the same OTA handle are no-ops
157264
+** Once a call to sqlite3rbu_step() has returned a value other than
157265
+** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
157063157266
** that immediately return the same value.
157064157267
*/
157065
-SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *pOta);
157268
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_step(sqlite3rbu *pRbu);
157066157269
157067157270
/*
157068
-** Close an OTA handle.
157271
+** Close an RBU handle.
157069157272
**
157070
-** If the OTA update has been completely applied, mark the OTA database
157273
+** If the RBU update has been completely applied, mark the RBU database
157071157274
** as fully applied. Otherwise, assuming no error has occurred, save the
157072
-** current state of the OTA update appliation to the OTA database.
157275
+** current state of the RBU update appliation to the RBU database.
157073157276
**
157074
-** If an error has already occurred as part of an sqlite3ota_step()
157075
-** or sqlite3ota_open() call, or if one occurs within this function, an
157277
+** If an error has already occurred as part of an sqlite3rbu_step()
157278
+** or sqlite3rbu_open() call, or if one occurs within this function, an
157076157279
** SQLite error code is returned. Additionally, *pzErrmsg may be set to
157077157280
** point to a buffer containing a utf-8 formatted English language error
157078157281
** message. It is the responsibility of the caller to eventually free any
157079157282
** such buffer using sqlite3_free().
157080157283
**
157081157284
** Otherwise, if no error occurs, this function returns SQLITE_OK if the
157082157285
** update has been partially applied, or SQLITE_DONE if it has been
157083157286
** completely applied.
157084157287
*/
157085
-SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *pOta, char **pzErrmsg);
157288
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
157086157289
157087157290
/*
157088157291
** Return the total number of key-value operations (inserts, deletes or
157089157292
** updates) that have been performed on the target database since the
157090
-** current OTA update was started.
157293
+** current RBU update was started.
157091157294
*/
157092
-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta);
157295
+SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3rbu_progress(sqlite3rbu *pRbu);
157093157296
157094157297
/*
157095
-** Create an OTA VFS named zName that accesses the underlying file-system
157298
+** Create an RBU VFS named zName that accesses the underlying file-system
157096157299
** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
157097
-** then the new OTA VFS uses the default system VFS to access the file-system.
157300
+** then the new RBU VFS uses the default system VFS to access the file-system.
157098157301
** The new object is registered as a non-default VFS with SQLite before
157099157302
** returning.
157100157303
**
157101
-** Part of the OTA implementation uses a custom VFS object. Usually, this
157102
-** object is created and deleted automatically by OTA.
157304
+** Part of the RBU implementation uses a custom VFS object. Usually, this
157305
+** object is created and deleted automatically by RBU.
157103157306
**
157104157307
** The exception is for applications that also use zipvfs. In this case,
157105
-** the custom VFS must be explicitly created by the user before the OTA
157106
-** handle is opened. The OTA VFS should be installed so that the zipvfs
157107
-** VFS uses the OTA VFS, which in turn uses any other VFS layers in use
157308
+** the custom VFS must be explicitly created by the user before the RBU
157309
+** handle is opened. The RBU VFS should be installed so that the zipvfs
157310
+** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
157108157311
** (for example multiplexor) to access the file-system. For example,
157109
-** to assemble an OTA enabled VFS stack that uses both zipvfs and
157312
+** to assemble an RBU enabled VFS stack that uses both zipvfs and
157110157313
** multiplexor (error checking omitted):
157111157314
**
157112157315
** // Create a VFS named "multiplex" (not the default).
157113157316
** sqlite3_multiplex_initialize(0, 0);
157114157317
**
157115
-** // Create an ota VFS named "ota" that uses multiplexor. If the
157116
-** // second argument were replaced with NULL, the "ota" VFS would
157318
+** // Create an rbu VFS named "rbu" that uses multiplexor. If the
157319
+** // second argument were replaced with NULL, the "rbu" VFS would
157117157320
** // access the file-system via the system default VFS, bypassing the
157118157321
** // multiplexor.
157119
-** sqlite3ota_create_vfs("ota", "multiplex");
157322
+** sqlite3rbu_create_vfs("rbu", "multiplex");
157120157323
**
157121
-** // Create a zipvfs VFS named "zipvfs" that uses ota.
157122
-** zipvfs_create_vfs_v3("zipvfs", "ota", 0, xCompressorAlgorithmDetector);
157324
+** // Create a zipvfs VFS named "zipvfs" that uses rbu.
157325
+** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
157123157326
**
157124157327
** // Make zipvfs the default VFS.
157125157328
** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
157126157329
**
157127
-** Because the default VFS created above includes a OTA functionality, it
157128
-** may be used by OTA clients. Attempting to use OTA with a zipvfs VFS stack
157129
-** that does not include the OTA layer results in an error.
157330
+** Because the default VFS created above includes a RBU functionality, it
157331
+** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
157332
+** that does not include the RBU layer results in an error.
157130157333
**
157131
-** The overhead of adding the "ota" VFS to the system is negligible for
157132
-** non-OTA users. There is no harm in an application accessing the
157133
-** file-system via "ota" all the time, even if it only uses OTA functionality
157334
+** The overhead of adding the "rbu" VFS to the system is negligible for
157335
+** non-RBU users. There is no harm in an application accessing the
157336
+** file-system via "rbu" all the time, even if it only uses RBU functionality
157134157337
** occasionally.
157135157338
*/
157136
-SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent);
157339
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_create_vfs(const char *zName, const char *zParent);
157137157340
157138157341
/*
157139
-** Deregister and destroy an OTA vfs created by an earlier call to
157140
-** sqlite3ota_create_vfs().
157342
+** Deregister and destroy an RBU vfs created by an earlier call to
157343
+** sqlite3rbu_create_vfs().
157141157344
**
157142157345
** VFS objects are not reference counted. If a VFS object is destroyed
157143157346
** before all database handles that use it have been closed, the results
157144157347
** are undefined.
157145157348
*/
157146
-SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName);
157349
+SQLITE_API void SQLITE_STDCALL sqlite3rbu_destroy_vfs(const char *zName);
157147157350
157148
-#endif /* _SQLITE3OTA_H */
157351
+#endif /* _SQLITE3RBU_H */
157149157352
157150
-
157151
-/************** End of sqlite3ota.h ******************************************/
157152
-/************** Continuing where we left off in sqlite3ota.c *****************/
157353
+/************** End of sqlite3rbu.h ******************************************/
157354
+/************** Continuing where we left off in sqlite3rbu.c *****************/
157153157355
157154157356
/* Maximum number of prepared UPDATE statements held by this module */
157155
-#define SQLITE_OTA_UPDATE_CACHESIZE 16
157357
+#define SQLITE_RBU_UPDATE_CACHESIZE 16
157156157358
157157157359
/*
157158157360
** Swap two objects of type TYPE.
157159157361
*/
157160157362
#if !defined(SQLITE_AMALGAMATION)
157161157363
# define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
157162157364
#endif
157163157365
157164157366
/*
157165
-** The ota_state table is used to save the state of a partially applied
157367
+** The rbu_state table is used to save the state of a partially applied
157166157368
** update so that it can be resumed later. The table consists of integer
157167157369
** keys mapped to values as follows:
157168157370
**
157169
-** OTA_STATE_STAGE:
157371
+** RBU_STATE_STAGE:
157170157372
** May be set to integer values 1, 2, 4 or 5. As follows:
157171
-** 1: the *-ota file is currently under construction.
157172
-** 2: the *-ota file has been constructed, but not yet moved
157373
+** 1: the *-rbu file is currently under construction.
157374
+** 2: the *-rbu file has been constructed, but not yet moved
157173157375
** to the *-wal path.
157174157376
** 4: the checkpoint is underway.
157175
-** 5: the ota update has been checkpointed.
157377
+** 5: the rbu update has been checkpointed.
157176157378
**
157177
-** OTA_STATE_TBL:
157379
+** RBU_STATE_TBL:
157178157380
** Only valid if STAGE==1. The target database name of the table
157179157381
** currently being written.
157180157382
**
157181
-** OTA_STATE_IDX:
157383
+** RBU_STATE_IDX:
157182157384
** Only valid if STAGE==1. The target database name of the index
157183157385
** currently being written, or NULL if the main table is currently being
157184157386
** updated.
157185157387
**
157186
-** OTA_STATE_ROW:
157388
+** RBU_STATE_ROW:
157187157389
** Only valid if STAGE==1. Number of rows already processed for the current
157188157390
** table/index.
157189157391
**
157190
-** OTA_STATE_PROGRESS:
157191
-** Total number of sqlite3ota_step() calls made so far as part of this
157192
-** ota update.
157392
+** RBU_STATE_PROGRESS:
157393
+** Trbul number of sqlite3rbu_step() calls made so far as part of this
157394
+** rbu update.
157193157395
**
157194
-** OTA_STATE_CKPT:
157396
+** RBU_STATE_CKPT:
157195157397
** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
157196157398
** header created by recovering the *-wal file. This is used to detect
157197157399
** cases when another client appends frames to the *-wal file in the
157198157400
** middle of an incremental checkpoint (an incremental checkpoint cannot
157199157401
** be continued if this happens).
157200157402
**
157201
-** OTA_STATE_COOKIE:
157403
+** RBU_STATE_COOKIE:
157202157404
** Valid if STAGE==1. The current change-counter cookie value in the
157203157405
** target db file.
157204157406
**
157205
-** OTA_STATE_OALSZ:
157407
+** RBU_STATE_OALSZ:
157206157408
** Valid if STAGE==1. The size in bytes of the *-oal file.
157207157409
*/
157208
-#define OTA_STATE_STAGE 1
157209
-#define OTA_STATE_TBL 2
157210
-#define OTA_STATE_IDX 3
157211
-#define OTA_STATE_ROW 4
157212
-#define OTA_STATE_PROGRESS 5
157213
-#define OTA_STATE_CKPT 6
157214
-#define OTA_STATE_COOKIE 7
157215
-#define OTA_STATE_OALSZ 8
157216
-
157217
-#define OTA_STAGE_OAL 1
157218
-#define OTA_STAGE_MOVE 2
157219
-#define OTA_STAGE_CAPTURE 3
157220
-#define OTA_STAGE_CKPT 4
157221
-#define OTA_STAGE_DONE 5
157222
-
157223
-
157224
-#define OTA_CREATE_STATE \
157225
- "CREATE TABLE IF NOT EXISTS %s.ota_state(k INTEGER PRIMARY KEY, v)"
157226
-
157227
-typedef struct OtaFrame OtaFrame;
157228
-typedef struct OtaObjIter OtaObjIter;
157229
-typedef struct OtaState OtaState;
157230
-typedef struct ota_vfs ota_vfs;
157231
-typedef struct ota_file ota_file;
157232
-typedef struct OtaUpdateStmt OtaUpdateStmt;
157410
+#define RBU_STATE_STAGE 1
157411
+#define RBU_STATE_TBL 2
157412
+#define RBU_STATE_IDX 3
157413
+#define RBU_STATE_ROW 4
157414
+#define RBU_STATE_PROGRESS 5
157415
+#define RBU_STATE_CKPT 6
157416
+#define RBU_STATE_COOKIE 7
157417
+#define RBU_STATE_OALSZ 8
157418
+
157419
+#define RBU_STAGE_OAL 1
157420
+#define RBU_STAGE_MOVE 2
157421
+#define RBU_STAGE_CAPTURE 3
157422
+#define RBU_STAGE_CKPT 4
157423
+#define RBU_STAGE_DONE 5
157424
+
157425
+
157426
+#define RBU_CREATE_STATE \
157427
+ "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
157428
+
157429
+typedef struct RbuFrame RbuFrame;
157430
+typedef struct RbuObjIter RbuObjIter;
157431
+typedef struct RbuState RbuState;
157432
+typedef struct rbu_vfs rbu_vfs;
157433
+typedef struct rbu_file rbu_file;
157434
+typedef struct RbuUpdateStmt RbuUpdateStmt;
157233157435
157234157436
#if !defined(SQLITE_AMALGAMATION)
157235157437
typedef unsigned int u32;
157236157438
typedef unsigned char u8;
157237157439
typedef sqlite3_int64 i64;
@@ -157245,13 +157447,13 @@
157245157447
#define WAL_LOCK_WRITE 0
157246157448
#define WAL_LOCK_CKPT 1
157247157449
#define WAL_LOCK_READ0 3
157248157450
157249157451
/*
157250
-** A structure to store values read from the ota_state table in memory.
157452
+** A structure to store values read from the rbu_state table in memory.
157251157453
*/
157252
-struct OtaState {
157454
+struct RbuState {
157253157455
int eStage;
157254157456
char *zTbl;
157255157457
char *zIdx;
157256157458
i64 iWalCksum;
157257157459
int nRow;
@@ -157258,14 +157460,14 @@
157258157460
i64 nProgress;
157259157461
u32 iCookie;
157260157462
i64 iOalSz;
157261157463
};
157262157464
157263
-struct OtaUpdateStmt {
157465
+struct RbuUpdateStmt {
157264157466
char *zMask; /* Copy of update mask used with pUpdate */
157265157467
sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
157266
- OtaUpdateStmt *pNext;
157468
+ RbuUpdateStmt *pNext;
157267157469
};
157268157470
157269157471
/*
157270157472
** An iterator of this type is used to iterate through all objects in
157271157473
** the target database that require updating. For each such table, the
@@ -157280,131 +157482,131 @@
157280157482
** it points to an array of flags nTblCol elements in size. The flag is
157281157483
** set for each column that is either a part of the PK or a part of an
157282157484
** index. Or clear otherwise.
157283157485
**
157284157486
*/
157285
-struct OtaObjIter {
157487
+struct RbuObjIter {
157286157488
sqlite3_stmt *pTblIter; /* Iterate through tables */
157287157489
sqlite3_stmt *pIdxIter; /* Index iterator */
157288157490
int nTblCol; /* Size of azTblCol[] array */
157289157491
char **azTblCol; /* Array of unquoted target column names */
157290157492
char **azTblType; /* Array of target column types */
157291157493
int *aiSrcOrder; /* src table col -> target table col */
157292157494
u8 *abTblPk; /* Array of flags, set on target PK columns */
157293157495
u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
157294157496
u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
157295
- int eType; /* Table type - an OTA_PK_XXX value */
157497
+ int eType; /* Table type - an RBU_PK_XXX value */
157296157498
157297157499
/* Output variables. zTbl==0 implies EOF. */
157298157500
int bCleanup; /* True in "cleanup" state */
157299157501
const char *zTbl; /* Name of target db table */
157300157502
const char *zIdx; /* Name of target db index (or null) */
157301157503
int iTnum; /* Root page of current object */
157302157504
int iPkTnum; /* If eType==EXTERNAL, root of PK index */
157303157505
int bUnique; /* Current index is unique */
157304157506
157305
- /* Statements created by otaObjIterPrepareAll() */
157507
+ /* Statements created by rbuObjIterPrepareAll() */
157306157508
int nCol; /* Number of columns in current object */
157307157509
sqlite3_stmt *pSelect; /* Source data */
157308157510
sqlite3_stmt *pInsert; /* Statement for INSERT operations */
157309157511
sqlite3_stmt *pDelete; /* Statement for DELETE ops */
157310
- sqlite3_stmt *pTmpInsert; /* Insert into ota_tmp_$zTbl */
157512
+ sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zTbl */
157311157513
157312157514
/* Last UPDATE used (for PK b-tree updates only), or NULL. */
157313
- OtaUpdateStmt *pOtaUpdate;
157515
+ RbuUpdateStmt *pRbuUpdate;
157314157516
};
157315157517
157316157518
/*
157317
-** Values for OtaObjIter.eType
157519
+** Values for RbuObjIter.eType
157318157520
**
157319157521
** 0: Table does not exist (error)
157320157522
** 1: Table has an implicit rowid.
157321157523
** 2: Table has an explicit IPK column.
157322157524
** 3: Table has an external PK index.
157323157525
** 4: Table is WITHOUT ROWID.
157324157526
** 5: Table is a virtual table.
157325157527
*/
157326
-#define OTA_PK_NOTABLE 0
157327
-#define OTA_PK_NONE 1
157328
-#define OTA_PK_IPK 2
157329
-#define OTA_PK_EXTERNAL 3
157330
-#define OTA_PK_WITHOUT_ROWID 4
157331
-#define OTA_PK_VTAB 5
157528
+#define RBU_PK_NOTABLE 0
157529
+#define RBU_PK_NONE 1
157530
+#define RBU_PK_IPK 2
157531
+#define RBU_PK_EXTERNAL 3
157532
+#define RBU_PK_WITHOUT_ROWID 4
157533
+#define RBU_PK_VTAB 5
157332157534
157333157535
157334157536
/*
157335
-** Within the OTA_STAGE_OAL stage, each call to sqlite3ota_step() performs
157537
+** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
157336157538
** one of the following operations.
157337157539
*/
157338
-#define OTA_INSERT 1 /* Insert on a main table b-tree */
157339
-#define OTA_DELETE 2 /* Delete a row from a main table b-tree */
157340
-#define OTA_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
157341
-#define OTA_IDX_INSERT 4 /* Insert on an aux. index b-tree */
157342
-#define OTA_UPDATE 5 /* Update a row in a main table b-tree */
157540
+#define RBU_INSERT 1 /* Insert on a main table b-tree */
157541
+#define RBU_DELETE 2 /* Delete a row from a main table b-tree */
157542
+#define RBU_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
157543
+#define RBU_IDX_INSERT 4 /* Insert on an aux. index b-tree */
157544
+#define RBU_UPDATE 5 /* Update a row in a main table b-tree */
157343157545
157344157546
157345157547
/*
157346157548
** A single step of an incremental checkpoint - frame iWalFrame of the wal
157347157549
** file should be copied to page iDbPage of the database file.
157348157550
*/
157349
-struct OtaFrame {
157551
+struct RbuFrame {
157350157552
u32 iDbPage;
157351157553
u32 iWalFrame;
157352157554
};
157353157555
157354157556
/*
157355
-** OTA handle.
157557
+** RBU handle.
157356157558
*/
157357
-struct sqlite3ota {
157358
- int eStage; /* Value of OTA_STATE_STAGE field */
157559
+struct sqlite3rbu {
157560
+ int eStage; /* Value of RBU_STATE_STAGE field */
157359157561
sqlite3 *dbMain; /* target database handle */
157360
- sqlite3 *dbOta; /* ota database handle */
157562
+ sqlite3 *dbRbu; /* rbu database handle */
157361157563
char *zTarget; /* Path to target db */
157362
- char *zOta; /* Path to ota db */
157363
- char *zState; /* Path to state db (or NULL if zOta) */
157564
+ char *zRbu; /* Path to rbu db */
157565
+ char *zState; /* Path to state db (or NULL if zRbu) */
157364157566
char zStateDb[5]; /* Db name for state ("stat" or "main") */
157365
- int rc; /* Value returned by last ota_step() call */
157567
+ int rc; /* Value returned by last rbu_step() call */
157366157568
char *zErrmsg; /* Error message if rc!=SQLITE_OK */
157367157569
int nStep; /* Rows processed for current object */
157368157570
int nProgress; /* Rows processed for all objects */
157369
- OtaObjIter objiter; /* Iterator for skipping through tbl/idx */
157370
- const char *zVfsName; /* Name of automatically created ota vfs */
157371
- ota_file *pTargetFd; /* File handle open on target db */
157571
+ RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
157572
+ const char *zVfsName; /* Name of automatically created rbu vfs */
157573
+ rbu_file *pTargetFd; /* File handle open on target db */
157372157574
i64 iOalSz;
157373157575
157374157576
/* The following state variables are used as part of the incremental
157375
- ** checkpoint stage (eStage==OTA_STAGE_CKPT). See comments surrounding
157376
- ** function otaSetupCheckpoint() for details. */
157577
+ ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
157578
+ ** function rbuSetupCheckpoint() for details. */
157377157579
u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
157378157580
u32 mLock;
157379157581
int nFrame; /* Entries in aFrame[] array */
157380157582
int nFrameAlloc; /* Allocated size of aFrame[] array */
157381
- OtaFrame *aFrame;
157583
+ RbuFrame *aFrame;
157382157584
int pgsz;
157383157585
u8 *aBuf;
157384157586
i64 iWalCksum;
157385157587
};
157386157588
157387157589
/*
157388
-** An ota VFS is implemented using an instance of this structure.
157590
+** An rbu VFS is implemented using an instance of this structure.
157389157591
*/
157390
-struct ota_vfs {
157391
- sqlite3_vfs base; /* ota VFS shim methods */
157592
+struct rbu_vfs {
157593
+ sqlite3_vfs base; /* rbu VFS shim methods */
157392157594
sqlite3_vfs *pRealVfs; /* Underlying VFS */
157393157595
sqlite3_mutex *mutex; /* Mutex to protect pMain */
157394
- ota_file *pMain; /* Linked list of main db files */
157596
+ rbu_file *pMain; /* Linked list of main db files */
157395157597
};
157396157598
157397157599
/*
157398
-** Each file opened by an ota VFS is represented by an instance of
157600
+** Each file opened by an rbu VFS is represented by an instance of
157399157601
** the following structure.
157400157602
*/
157401
-struct ota_file {
157603
+struct rbu_file {
157402157604
sqlite3_file base; /* sqlite3_file methods */
157403157605
sqlite3_file *pReal; /* Underlying file handle */
157404
- ota_vfs *pOtaVfs; /* Pointer to the ota_vfs object */
157405
- sqlite3ota *pOta; /* Pointer to ota object (ota target only) */
157606
+ rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
157607
+ sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
157406157608
157407157609
int openFlags; /* Flags this file was opened with */
157408157610
u32 iCookie; /* Cookie value for main db files */
157409157611
u8 iWriteVer; /* "write-version" value for main db files */
157410157612
@@ -157411,12 +157613,12 @@
157411157613
int nShm; /* Number of entries in apShm[] array */
157412157614
char **apShm; /* Array of mmap'd *-shm regions */
157413157615
char *zDel; /* Delete this when closing file */
157414157616
157415157617
const char *zWal; /* Wal filename for this main db file */
157416
- ota_file *pWalFd; /* Wal file descriptor for this main db */
157417
- ota_file *pMainNext; /* Next MAIN_DB file */
157618
+ rbu_file *pWalFd; /* Wal file descriptor for this main db */
157619
+ rbu_file *pMainNext; /* Next MAIN_DB file */
157418157620
};
157419157621
157420157622
157421157623
/*
157422157624
** Prepare the SQL statement in buffer zSql against database handle db.
@@ -157490,14 +157692,14 @@
157490157692
}
157491157693
return rc;
157492157694
}
157493157695
157494157696
/*
157495
-** Free the OtaObjIter.azTblCol[] and OtaObjIter.abTblPk[] arrays allocated
157496
-** by an earlier call to otaObjIterCacheTableInfo().
157697
+** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
157698
+** by an earlier call to rbuObjIterCacheTableInfo().
157497157699
*/
157498
-static void otaObjIterFreeCols(OtaObjIter *pIter){
157700
+static void rbuObjIterFreeCols(RbuObjIter *pIter){
157499157701
int i;
157500157702
for(i=0; i<pIter->nTblCol; i++){
157501157703
sqlite3_free(pIter->azTblCol[i]);
157502157704
sqlite3_free(pIter->azTblType[i]);
157503157705
}
@@ -157513,72 +157715,72 @@
157513157715
157514157716
/*
157515157717
** Finalize all statements and free all allocations that are specific to
157516157718
** the current object (table/index pair).
157517157719
*/
157518
-static void otaObjIterClearStatements(OtaObjIter *pIter){
157519
- OtaUpdateStmt *pUp;
157720
+static void rbuObjIterClearStatements(RbuObjIter *pIter){
157721
+ RbuUpdateStmt *pUp;
157520157722
157521157723
sqlite3_finalize(pIter->pSelect);
157522157724
sqlite3_finalize(pIter->pInsert);
157523157725
sqlite3_finalize(pIter->pDelete);
157524157726
sqlite3_finalize(pIter->pTmpInsert);
157525
- pUp = pIter->pOtaUpdate;
157727
+ pUp = pIter->pRbuUpdate;
157526157728
while( pUp ){
157527
- OtaUpdateStmt *pTmp = pUp->pNext;
157729
+ RbuUpdateStmt *pTmp = pUp->pNext;
157528157730
sqlite3_finalize(pUp->pUpdate);
157529157731
sqlite3_free(pUp);
157530157732
pUp = pTmp;
157531157733
}
157532157734
157533157735
pIter->pSelect = 0;
157534157736
pIter->pInsert = 0;
157535157737
pIter->pDelete = 0;
157536
- pIter->pOtaUpdate = 0;
157738
+ pIter->pRbuUpdate = 0;
157537157739
pIter->pTmpInsert = 0;
157538157740
pIter->nCol = 0;
157539157741
}
157540157742
157541157743
/*
157542157744
** Clean up any resources allocated as part of the iterator object passed
157543157745
** as the only argument.
157544157746
*/
157545
-static void otaObjIterFinalize(OtaObjIter *pIter){
157546
- otaObjIterClearStatements(pIter);
157747
+static void rbuObjIterFinalize(RbuObjIter *pIter){
157748
+ rbuObjIterClearStatements(pIter);
157547157749
sqlite3_finalize(pIter->pTblIter);
157548157750
sqlite3_finalize(pIter->pIdxIter);
157549
- otaObjIterFreeCols(pIter);
157550
- memset(pIter, 0, sizeof(OtaObjIter));
157751
+ rbuObjIterFreeCols(pIter);
157752
+ memset(pIter, 0, sizeof(RbuObjIter));
157551157753
}
157552157754
157553157755
/*
157554157756
** Advance the iterator to the next position.
157555157757
**
157556157758
** If no error occurs, SQLITE_OK is returned and the iterator is left
157557157759
** pointing to the next entry. Otherwise, an error code and message is
157558
-** left in the OTA handle passed as the first argument. A copy of the
157760
+** left in the RBU handle passed as the first argument. A copy of the
157559157761
** error code is returned.
157560157762
*/
157561
-static int otaObjIterNext(sqlite3ota *p, OtaObjIter *pIter){
157763
+static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
157562157764
int rc = p->rc;
157563157765
if( rc==SQLITE_OK ){
157564157766
157565157767
/* Free any SQLite statements used while processing the previous object */
157566
- otaObjIterClearStatements(pIter);
157768
+ rbuObjIterClearStatements(pIter);
157567157769
if( pIter->zIdx==0 ){
157568157770
rc = sqlite3_exec(p->dbMain,
157569
- "DROP TRIGGER IF EXISTS temp.ota_insert_tr;"
157570
- "DROP TRIGGER IF EXISTS temp.ota_update1_tr;"
157571
- "DROP TRIGGER IF EXISTS temp.ota_update2_tr;"
157572
- "DROP TRIGGER IF EXISTS temp.ota_delete_tr;"
157771
+ "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
157772
+ "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
157773
+ "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
157774
+ "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
157573157775
, 0, 0, &p->zErrmsg
157574157776
);
157575157777
}
157576157778
157577157779
if( rc==SQLITE_OK ){
157578157780
if( pIter->bCleanup ){
157579
- otaObjIterFreeCols(pIter);
157781
+ rbuObjIterFreeCols(pIter);
157580157782
pIter->bCleanup = 0;
157581157783
rc = sqlite3_step(pIter->pTblIter);
157582157784
if( rc!=SQLITE_ROW ){
157583157785
rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
157584157786
pIter->zTbl = 0;
@@ -157607,11 +157809,11 @@
157607157809
}
157608157810
}
157609157811
}
157610157812
157611157813
if( rc!=SQLITE_OK ){
157612
- otaObjIterFinalize(pIter);
157814
+ rbuObjIterFinalize(pIter);
157613157815
p->rc = rc;
157614157816
}
157615157817
return rc;
157616157818
}
157617157819
@@ -157618,18 +157820,18 @@
157618157820
/*
157619157821
** Initialize the iterator structure passed as the second argument.
157620157822
**
157621157823
** If no error occurs, SQLITE_OK is returned and the iterator is left
157622157824
** pointing to the first entry. Otherwise, an error code and message is
157623
-** left in the OTA handle passed as the first argument. A copy of the
157825
+** left in the RBU handle passed as the first argument. A copy of the
157624157826
** error code is returned.
157625157827
*/
157626
-static int otaObjIterFirst(sqlite3ota *p, OtaObjIter *pIter){
157828
+static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
157627157829
int rc;
157628
- memset(pIter, 0, sizeof(OtaObjIter));
157830
+ memset(pIter, 0, sizeof(RbuObjIter));
157629157831
157630
- rc = prepareAndCollectError(p->dbOta, &pIter->pTblIter, &p->zErrmsg,
157832
+ rc = prepareAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
157631157833
"SELECT substr(name, 6) FROM sqlite_master "
157632157834
"WHERE type='table' AND name LIKE 'data_%'"
157633157835
);
157634157836
157635157837
if( rc==SQLITE_OK ){
@@ -157640,23 +157842,23 @@
157640157842
);
157641157843
}
157642157844
157643157845
pIter->bCleanup = 1;
157644157846
p->rc = rc;
157645
- return otaObjIterNext(p, pIter);
157847
+ return rbuObjIterNext(p, pIter);
157646157848
}
157647157849
157648157850
/*
157649157851
** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
157650
-** an error code is stored in the OTA handle passed as the first argument.
157852
+** an error code is stored in the RBU handle passed as the first argument.
157651157853
**
157652157854
** If an error has already occurred (p->rc is already set to something other
157653157855
** than SQLITE_OK), then this function returns NULL without modifying the
157654157856
** stored error code. In this case it still calls sqlite3_free() on any
157655157857
** printf() parameters associated with %z conversions.
157656157858
*/
157657
-static char *otaMPrintf(sqlite3ota *p, const char *zFmt, ...){
157859
+static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
157658157860
char *zSql = 0;
157659157861
va_list ap;
157660157862
va_start(ap, zFmt);
157661157863
zSql = sqlite3_vmprintf(zFmt, ap);
157662157864
if( p->rc==SQLITE_OK ){
@@ -157671,17 +157873,17 @@
157671157873
157672157874
/*
157673157875
** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
157674157876
** arguments are the usual subsitution values. This function performs
157675157877
** the printf() style substitutions and executes the result as an SQL
157676
-** statement on the OTA handles database.
157878
+** statement on the RBU handles database.
157677157879
**
157678157880
** If an error occurs, an error code and error message is stored in the
157679
-** OTA handle. If an error has already occurred when this function is
157881
+** RBU handle. If an error has already occurred when this function is
157680157882
** called, it is a no-op.
157681157883
*/
157682
-static int otaMPrintfExec(sqlite3ota *p, sqlite3 *db, const char *zFmt, ...){
157884
+static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
157683157885
va_list ap;
157684157886
va_start(ap, zFmt);
157685157887
char *zSql = sqlite3_vmprintf(zFmt, ap);
157686157888
if( p->rc==SQLITE_OK ){
157687157889
if( zSql==0 ){
@@ -157698,16 +157900,16 @@
157698157900
/*
157699157901
** Attempt to allocate and return a pointer to a zeroed block of nByte
157700157902
** bytes.
157701157903
**
157702157904
** If an error (i.e. an OOM condition) occurs, return NULL and leave an
157703
-** error code in the ota handle passed as the first argument. Or, if an
157905
+** error code in the rbu handle passed as the first argument. Or, if an
157704157906
** error has already occurred when this function is called, return NULL
157705157907
** immediately without attempting the allocation or modifying the stored
157706157908
** error code.
157707157909
*/
157708
-static void *otaMalloc(sqlite3ota *p, int nByte){
157910
+static void *rbuMalloc(sqlite3rbu *p, int nByte){
157709157911
void *pRet = 0;
157710157912
if( p->rc==SQLITE_OK ){
157711157913
assert( nByte>0 );
157712157914
pRet = sqlite3_malloc(nByte);
157713157915
if( pRet==0 ){
@@ -157721,17 +157923,17 @@
157721157923
157722157924
157723157925
/*
157724157926
** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
157725157927
** there is room for at least nCol elements. If an OOM occurs, store an
157726
-** error code in the OTA handle passed as the first argument.
157928
+** error code in the RBU handle passed as the first argument.
157727157929
*/
157728
-static void otaAllocateIterArrays(sqlite3ota *p, OtaObjIter *pIter, int nCol){
157930
+static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
157729157931
int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
157730157932
char **azNew;
157731157933
157732
- azNew = (char**)otaMalloc(p, nByte);
157934
+ azNew = (char**)rbuMalloc(p, nByte);
157733157935
if( azNew ){
157734157936
pIter->azTblCol = azNew;
157735157937
pIter->azTblType = &azNew[nCol];
157736157938
pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
157737157939
pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
@@ -157748,11 +157950,11 @@
157748157950
**
157749157951
** If an OOM condition is encountered when attempting to allocate memory,
157750157952
** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
157751157953
** if the allocation succeeds, (*pRc) is left unchanged.
157752157954
*/
157753
-static char *otaStrndup(const char *zStr, int *pRc){
157955
+static char *rbuStrndup(const char *zStr, int *pRc){
157754157956
char *zRet = 0;
157755157957
157756157958
assert( *pRc==SQLITE_OK );
157757157959
if( zStr ){
157758157960
int nCopy = strlen(zStr) + 1;
@@ -157769,14 +157971,14 @@
157769157971
157770157972
/*
157771157973
** Finalize the statement passed as the second argument.
157772157974
**
157773157975
** If the sqlite3_finalize() call indicates that an error occurs, and the
157774
-** ota handle error code is not already set, set the error code and error
157976
+** rbu handle error code is not already set, set the error code and error
157775157977
** message accordingly.
157776157978
*/
157777
-static void otaFinalize(sqlite3ota *p, sqlite3_stmt *pStmt){
157979
+static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
157778157980
sqlite3 *db = sqlite3_db_handle(pStmt);
157779157981
int rc = sqlite3_finalize(pStmt);
157780157982
if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
157781157983
p->rc = rc;
157782157984
p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
@@ -157787,16 +157989,16 @@
157787157989
**
157788157990
** peType is of type (int*), a pointer to an output parameter of type
157789157991
** (int). This call sets the output parameter as follows, depending
157790157992
** on the type of the table specified by parameters dbName and zTbl.
157791157993
**
157792
-** OTA_PK_NOTABLE: No such table.
157793
-** OTA_PK_NONE: Table has an implicit rowid.
157794
-** OTA_PK_IPK: Table has an explicit IPK column.
157795
-** OTA_PK_EXTERNAL: Table has an external PK index.
157796
-** OTA_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
157797
-** OTA_PK_VTAB: Table is a virtual table.
157994
+** RBU_PK_NOTABLE: No such table.
157995
+** RBU_PK_NONE: Table has an implicit rowid.
157996
+** RBU_PK_IPK: Table has an explicit IPK column.
157997
+** RBU_PK_EXTERNAL: Table has an external PK index.
157998
+** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
157999
+** RBU_PK_VTAB: Table is a virtual table.
157798158000
**
157799158001
** Argument *piPk is also of type (int*), and also points to an output
157800158002
** parameter. Unless the table has an external primary key index
157801158003
** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
157802158004
** if the table does have an external primary key index, then *piPk
@@ -157804,28 +158006,28 @@
157804158006
** returning.
157805158007
**
157806158008
** ALGORITHM:
157807158009
**
157808158010
** if( no entry exists in sqlite_master ){
157809
-** return OTA_PK_NOTABLE
158011
+** return RBU_PK_NOTABLE
157810158012
** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
157811
-** return OTA_PK_VTAB
158013
+** return RBU_PK_VTAB
157812158014
** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
157813158015
** if( the index that is the pk exists in sqlite_master ){
157814158016
** *piPK = rootpage of that index.
157815
-** return OTA_PK_EXTERNAL
158017
+** return RBU_PK_EXTERNAL
157816158018
** }else{
157817
-** return OTA_PK_WITHOUT_ROWID
158019
+** return RBU_PK_WITHOUT_ROWID
157818158020
** }
157819158021
** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
157820
-** return OTA_PK_IPK
158022
+** return RBU_PK_IPK
157821158023
** }else{
157822
-** return OTA_PK_NONE
158024
+** return RBU_PK_NONE
157823158025
** }
157824158026
*/
157825
-static void otaTableType(
157826
- sqlite3ota *p,
158027
+static void rbuTableType(
158028
+ sqlite3rbu *p,
157827158029
const char *zTab,
157828158030
int *peType,
157829158031
int *piTnum,
157830158032
int *piPk
157831158033
){
@@ -157835,11 +158037,11 @@
157835158037
** 2) SELECT count(*) FROM sqlite_master where name=%Q
157836158038
** 3) PRAGMA table_info = ?
157837158039
*/
157838158040
sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
157839158041
157840
- *peType = OTA_PK_NOTABLE;
158042
+ *peType = RBU_PK_NOTABLE;
157841158043
*piPk = 0;
157842158044
157843158045
assert( p->rc==SQLITE_OK );
157844158046
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
157845158047
sqlite3_mprintf(
@@ -157847,22 +158049,22 @@
157847158049
" FROM sqlite_master"
157848158050
" WHERE name=%Q", zTab
157849158051
));
157850158052
if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
157851158053
/* Either an error, or no such table. */
157852
- goto otaTableType_end;
158054
+ goto rbuTableType_end;
157853158055
}
157854158056
if( sqlite3_column_int(aStmt[0], 0) ){
157855
- *peType = OTA_PK_VTAB; /* virtual table */
157856
- goto otaTableType_end;
158057
+ *peType = RBU_PK_VTAB; /* virtual table */
158058
+ goto rbuTableType_end;
157857158059
}
157858158060
*piTnum = sqlite3_column_int(aStmt[0], 1);
157859158061
157860158062
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
157861158063
sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
157862158064
);
157863
- if( p->rc ) goto otaTableType_end;
158065
+ if( p->rc ) goto rbuTableType_end;
157864158066
while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
157865158067
const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
157866158068
const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
157867158069
if( zOrig && zIdx && zOrig[0]=='p' ){
157868158070
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
@@ -157870,45 +158072,45 @@
157870158072
"SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
157871158073
));
157872158074
if( p->rc==SQLITE_OK ){
157873158075
if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
157874158076
*piPk = sqlite3_column_int(aStmt[2], 0);
157875
- *peType = OTA_PK_EXTERNAL;
158077
+ *peType = RBU_PK_EXTERNAL;
157876158078
}else{
157877
- *peType = OTA_PK_WITHOUT_ROWID;
158079
+ *peType = RBU_PK_WITHOUT_ROWID;
157878158080
}
157879158081
}
157880
- goto otaTableType_end;
158082
+ goto rbuTableType_end;
157881158083
}
157882158084
}
157883158085
157884158086
p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
157885158087
sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
157886158088
);
157887158089
if( p->rc==SQLITE_OK ){
157888158090
while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
157889158091
if( sqlite3_column_int(aStmt[3],5)>0 ){
157890
- *peType = OTA_PK_IPK; /* explicit IPK column */
157891
- goto otaTableType_end;
158092
+ *peType = RBU_PK_IPK; /* explicit IPK column */
158093
+ goto rbuTableType_end;
157892158094
}
157893158095
}
157894
- *peType = OTA_PK_NONE;
158096
+ *peType = RBU_PK_NONE;
157895158097
}
157896158098
157897
-otaTableType_end: {
158099
+rbuTableType_end: {
157898158100
int i;
157899158101
for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
157900
- otaFinalize(p, aStmt[i]);
158102
+ rbuFinalize(p, aStmt[i]);
157901158103
}
157902158104
}
157903158105
}
157904158106
157905158107
/*
157906
-** This is a helper function for otaObjIterCacheTableInfo(). It populates
158108
+** This is a helper function for rbuObjIterCacheTableInfo(). It populates
157907158109
** the pIter->abIndexed[] array.
157908158110
*/
157909
-static void otaObjIterCacheIndexedCols(sqlite3ota *p, OtaObjIter *pIter){
158111
+static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
157910158112
sqlite3_stmt *pList = 0;
157911158113
int bIndex = 0;
157912158114
157913158115
if( p->rc==SQLITE_OK ){
157914158116
memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
@@ -157926,15 +158128,15 @@
157926158128
);
157927158129
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
157928158130
int iCid = sqlite3_column_int(pXInfo, 1);
157929158131
if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
157930158132
}
157931
- otaFinalize(p, pXInfo);
158133
+ rbuFinalize(p, pXInfo);
157932158134
bIndex = 1;
157933158135
}
157934158136
157935
- otaFinalize(p, pList);
158137
+ rbuFinalize(p, pList);
157936158138
if( bIndex==0 ) pIter->abIndexed = 0;
157937158139
}
157938158140
157939158141
157940158142
/*
@@ -157942,67 +158144,67 @@
157942158144
** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
157943158145
** the table (not index) that the iterator currently points to.
157944158146
**
157945158147
** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
157946158148
** an error does occur, an error code and error message are also left in
157947
-** the OTA handle.
158149
+** the RBU handle.
157948158150
*/
157949
-static int otaObjIterCacheTableInfo(sqlite3ota *p, OtaObjIter *pIter){
158151
+static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
157950158152
if( pIter->azTblCol==0 ){
157951158153
sqlite3_stmt *pStmt = 0;
157952158154
int nCol = 0;
157953158155
int i; /* for() loop iterator variable */
157954
- int bOtaRowid = 0; /* If input table has column "ota_rowid" */
158156
+ int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
157955158157
int iOrder = 0;
157956158158
int iTnum = 0;
157957158159
157958158160
/* Figure out the type of table this step will deal with. */
157959158161
assert( pIter->eType==0 );
157960
- otaTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
157961
- if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_NOTABLE ){
158162
+ rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
158163
+ if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
157962158164
p->rc = SQLITE_ERROR;
157963158165
p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
157964158166
}
157965158167
if( p->rc ) return p->rc;
157966158168
if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
157967158169
157968
- assert( pIter->eType==OTA_PK_NONE || pIter->eType==OTA_PK_IPK
157969
- || pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_WITHOUT_ROWID
157970
- || pIter->eType==OTA_PK_VTAB
158170
+ assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
158171
+ || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
158172
+ || pIter->eType==RBU_PK_VTAB
157971158173
);
157972158174
157973158175
/* Populate the azTblCol[] and nTblCol variables based on the columns
157974158176
** of the input table. Ignore any input table columns that begin with
157975
- ** "ota_". */
157976
- p->rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
158177
+ ** "rbu_". */
158178
+ p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
157977158179
sqlite3_mprintf("SELECT * FROM 'data_%q'", pIter->zTbl)
157978158180
);
157979158181
if( p->rc==SQLITE_OK ){
157980158182
nCol = sqlite3_column_count(pStmt);
157981
- otaAllocateIterArrays(p, pIter, nCol);
158183
+ rbuAllocateIterArrays(p, pIter, nCol);
157982158184
}
157983158185
for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
157984158186
const char *zName = (const char*)sqlite3_column_name(pStmt, i);
157985
- if( sqlite3_strnicmp("ota_", zName, 4) ){
157986
- char *zCopy = otaStrndup(zName, &p->rc);
158187
+ if( sqlite3_strnicmp("rbu_", zName, 4) ){
158188
+ char *zCopy = rbuStrndup(zName, &p->rc);
157987158189
pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
157988158190
pIter->azTblCol[pIter->nTblCol++] = zCopy;
157989158191
}
157990
- else if( 0==sqlite3_stricmp("ota_rowid", zName) ){
157991
- bOtaRowid = 1;
158192
+ else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
158193
+ bRbuRowid = 1;
157992158194
}
157993158195
}
157994158196
sqlite3_finalize(pStmt);
157995158197
pStmt = 0;
157996158198
157997158199
if( p->rc==SQLITE_OK
157998
- && bOtaRowid!=(pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
158200
+ && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
157999158201
){
158000158202
p->rc = SQLITE_ERROR;
158001158203
p->zErrmsg = sqlite3_mprintf(
158002
- "table data_%q %s ota_rowid column", pIter->zTbl,
158003
- (bOtaRowid ? "may not have" : "requires")
158204
+ "table data_%q %s rbu_rowid column", pIter->zTbl,
158205
+ (bRbuRowid ? "may not have" : "requires")
158004158206
);
158005158207
}
158006158208
158007158209
/* Check that all non-HIDDEN columns in the destination table are also
158008158210
** present in the input table. Populate the abTblPk[], azTblType[] and
@@ -158031,20 +158233,20 @@
158031158233
if( i!=iOrder ){
158032158234
SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
158033158235
SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
158034158236
}
158035158237
158036
- pIter->azTblType[iOrder] = otaStrndup(zType, &p->rc);
158238
+ pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
158037158239
pIter->abTblPk[iOrder] = (iPk!=0);
158038158240
pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
158039158241
iOrder++;
158040158242
}
158041158243
}
158042158244
158043
- otaFinalize(p, pStmt);
158044
- otaObjIterCacheIndexedCols(p, pIter);
158045
- assert( pIter->eType!=OTA_PK_VTAB || pIter->abIndexed==0 );
158245
+ rbuFinalize(p, pStmt);
158246
+ rbuObjIterCacheIndexedCols(p, pIter);
158247
+ assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
158046158248
}
158047158249
158048158250
return p->rc;
158049158251
}
158050158252
@@ -158051,29 +158253,29 @@
158051158253
/*
158052158254
** This function constructs and returns a pointer to a nul-terminated
158053158255
** string containing some SQL clause or list based on one or more of the
158054158256
** column names currently stored in the pIter->azTblCol[] array.
158055158257
*/
158056
-static char *otaObjIterGetCollist(
158057
- sqlite3ota *p, /* OTA object */
158058
- OtaObjIter *pIter /* Object iterator for column names */
158258
+static char *rbuObjIterGetCollist(
158259
+ sqlite3rbu *p, /* RBU object */
158260
+ RbuObjIter *pIter /* Object iterator for column names */
158059158261
){
158060158262
char *zList = 0;
158061158263
const char *zSep = "";
158062158264
int i;
158063158265
for(i=0; i<pIter->nTblCol; i++){
158064158266
const char *z = pIter->azTblCol[i];
158065
- zList = otaMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
158267
+ zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
158066158268
zSep = ", ";
158067158269
}
158068158270
return zList;
158069158271
}
158070158272
158071158273
/*
158072158274
** This function is used to create a SELECT list (the list of SQL
158073158275
** expressions that follows a SELECT keyword) for a SELECT statement
158074
-** used to read from an data_xxx or ota_tmp_xxx table while updating the
158276
+** used to read from an data_xxx or rbu_tmp_xxx table while updating the
158075158277
** index object currently indicated by the iterator object passed as the
158076158278
** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
158077158279
** to obtain the required information.
158078158280
**
158079158281
** If the index is of the following form:
@@ -158090,17 +158292,17 @@
158090158292
**
158091158293
** pzImposterCols: ...
158092158294
** pzImposterPk: ...
158093158295
** pzWhere: ...
158094158296
*/
158095
-static char *otaObjIterGetIndexCols(
158096
- sqlite3ota *p, /* OTA object */
158097
- OtaObjIter *pIter, /* Object iterator for column names */
158297
+static char *rbuObjIterGetIndexCols(
158298
+ sqlite3rbu *p, /* RBU object */
158299
+ RbuObjIter *pIter, /* Object iterator for column names */
158098158300
char **pzImposterCols, /* OUT: Columns for imposter table */
158099158301
char **pzImposterPk, /* OUT: Imposter PK clause */
158100158302
char **pzWhere, /* OUT: WHERE clause */
158101
- int *pnBind /* OUT: Total number of columns */
158303
+ int *pnBind /* OUT: Trbul number of columns */
158102158304
){
158103158305
int rc = p->rc; /* Error code */
158104158306
int rc2; /* sqlite3_finalize() return code */
158105158307
char *zRet = 0; /* String to return */
158106158308
char *zImpCols = 0; /* String to return via *pzImposterCols */
@@ -158125,18 +158327,18 @@
158125158327
const char *zCol;
158126158328
const char *zType;
158127158329
158128158330
if( iCid<0 ){
158129158331
/* An integer primary key. If the table has an explicit IPK, use
158130
- ** its name. Otherwise, use "ota_rowid". */
158131
- if( pIter->eType==OTA_PK_IPK ){
158332
+ ** its name. Otherwise, use "rbu_rowid". */
158333
+ if( pIter->eType==RBU_PK_IPK ){
158132158334
int i;
158133158335
for(i=0; pIter->abTblPk[i]==0; i++);
158134158336
assert( i<pIter->nTblCol );
158135158337
zCol = pIter->azTblCol[i];
158136158338
}else{
158137
- zCol = "ota_rowid";
158339
+ zCol = "rbu_rowid";
158138158340
}
158139158341
zType = "INTEGER";
158140158342
}else{
158141158343
zCol = pIter->azTblCol[iCid];
158142158344
zType = pIter->azTblType[iCid];
@@ -158143,19 +158345,19 @@
158143158345
}
158144158346
158145158347
zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
158146158348
if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
158147158349
const char *zOrder = (bDesc ? " DESC" : "");
158148
- zImpPK = sqlite3_mprintf("%z%s\"ota_imp_%d%w\"%s",
158350
+ zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
158149158351
zImpPK, zCom, nBind, zCol, zOrder
158150158352
);
158151158353
}
158152
- zImpCols = sqlite3_mprintf("%z%s\"ota_imp_%d%w\" %s COLLATE %Q",
158354
+ zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
158153158355
zImpCols, zCom, nBind, zCol, zType, zCollate
158154158356
);
158155158357
zWhere = sqlite3_mprintf(
158156
- "%z%s\"ota_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
158358
+ "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
158157158359
);
158158158360
if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
158159158361
zCom = ", ";
158160158362
zAnd = " AND ";
158161158363
nBind++;
@@ -158189,16 +158391,16 @@
158189158391
**
158190158392
** "old.a, old.b, old.b"
158191158393
**
158192158394
** With the column names escaped.
158193158395
**
158194
-** For tables with implicit rowids - OTA_PK_EXTERNAL and OTA_PK_NONE, append
158396
+** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
158195158397
** the text ", old._rowid_" to the returned value.
158196158398
*/
158197
-static char *otaObjIterGetOldlist(
158198
- sqlite3ota *p,
158199
- OtaObjIter *pIter,
158399
+static char *rbuObjIterGetOldlist(
158400
+ sqlite3rbu *p,
158401
+ RbuObjIter *pIter,
158200158402
const char *zObj
158201158403
){
158202158404
char *zList = 0;
158203158405
if( p->rc==SQLITE_OK && pIter->abIndexed ){
158204158406
const char *zS = "";
@@ -158216,12 +158418,12 @@
158216158418
break;
158217158419
}
158218158420
}
158219158421
158220158422
/* For a table with implicit rowids, append "old._rowid_" to the list. */
158221
- if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158222
- zList = otaMPrintf(p, "%z, %s._rowid_", zList, zObj);
158423
+ if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158424
+ zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
158223158425
}
158224158426
}
158225158427
return zList;
158226158428
}
158227158429
@@ -158233,37 +158435,37 @@
158233158435
**
158234158436
** Return the string:
158235158437
**
158236158438
** "b = ?1 AND c = ?2"
158237158439
*/
158238
-static char *otaObjIterGetWhere(
158239
- sqlite3ota *p,
158240
- OtaObjIter *pIter
158440
+static char *rbuObjIterGetWhere(
158441
+ sqlite3rbu *p,
158442
+ RbuObjIter *pIter
158241158443
){
158242158444
char *zList = 0;
158243
- if( pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE ){
158244
- zList = otaMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
158245
- }else if( pIter->eType==OTA_PK_EXTERNAL ){
158445
+ if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
158446
+ zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
158447
+ }else if( pIter->eType==RBU_PK_EXTERNAL ){
158246158448
const char *zSep = "";
158247158449
int i;
158248158450
for(i=0; i<pIter->nTblCol; i++){
158249158451
if( pIter->abTblPk[i] ){
158250
- zList = otaMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
158452
+ zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
158251158453
zSep = " AND ";
158252158454
}
158253158455
}
158254
- zList = otaMPrintf(p,
158255
- "_rowid_ = (SELECT id FROM ota_imposter2 WHERE %z)", zList
158456
+ zList = rbuMPrintf(p,
158457
+ "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
158256158458
);
158257158459
158258158460
}else{
158259158461
const char *zSep = "";
158260158462
int i;
158261158463
for(i=0; i<pIter->nTblCol; i++){
158262158464
if( pIter->abTblPk[i] ){
158263158465
const char *zCol = pIter->azTblCol[i];
158264
- zList = otaMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
158466
+ zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
158265158467
zSep = " AND ";
158266158468
}
158267158469
}
158268158470
}
158269158471
return zList;
@@ -158270,60 +158472,60 @@
158270158472
}
158271158473
158272158474
/*
158273158475
** The SELECT statement iterating through the keys for the current object
158274158476
** (p->objiter.pSelect) currently points to a valid row. However, there
158275
-** is something wrong with the ota_control value in the ota_control value
158477
+** is something wrong with the rbu_control value in the rbu_control value
158276158478
** stored in the (p->nCol+1)'th column. Set the error code and error message
158277
-** of the OTA handle to something reflecting this.
158479
+** of the RBU handle to something reflecting this.
158278158480
*/
158279
-static void otaBadControlError(sqlite3ota *p){
158481
+static void rbuBadControlError(sqlite3rbu *p){
158280158482
p->rc = SQLITE_ERROR;
158281
- p->zErrmsg = sqlite3_mprintf("invalid ota_control value");
158483
+ p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
158282158484
}
158283158485
158284158486
158285158487
/*
158286158488
** Return a nul-terminated string containing the comma separated list of
158287158489
** assignments that should be included following the "SET" keyword of
158288158490
** an UPDATE statement used to update the table object that the iterator
158289
-** passed as the second argument currently points to if the ota_control
158491
+** passed as the second argument currently points to if the rbu_control
158290158492
** column of the data_xxx table entry is set to zMask.
158291158493
**
158292158494
** The memory for the returned string is obtained from sqlite3_malloc().
158293158495
** It is the responsibility of the caller to eventually free it using
158294158496
** sqlite3_free().
158295158497
**
158296158498
** If an OOM error is encountered when allocating space for the new
158297
-** string, an error code is left in the ota handle passed as the first
158499
+** string, an error code is left in the rbu handle passed as the first
158298158500
** argument and NULL is returned. Or, if an error has already occurred
158299158501
** when this function is called, NULL is returned immediately, without
158300158502
** attempting the allocation or modifying the stored error code.
158301158503
*/
158302
-static char *otaObjIterGetSetlist(
158303
- sqlite3ota *p,
158304
- OtaObjIter *pIter,
158504
+static char *rbuObjIterGetSetlist(
158505
+ sqlite3rbu *p,
158506
+ RbuObjIter *pIter,
158305158507
const char *zMask
158306158508
){
158307158509
char *zList = 0;
158308158510
if( p->rc==SQLITE_OK ){
158309158511
int i;
158310158512
158311158513
if( strlen(zMask)!=pIter->nTblCol ){
158312
- otaBadControlError(p);
158514
+ rbuBadControlError(p);
158313158515
}else{
158314158516
const char *zSep = "";
158315158517
for(i=0; i<pIter->nTblCol; i++){
158316158518
char c = zMask[pIter->aiSrcOrder[i]];
158317158519
if( c=='x' ){
158318
- zList = otaMPrintf(p, "%z%s\"%w\"=?%d",
158520
+ zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
158319158521
zList, zSep, pIter->azTblCol[i], i+1
158320158522
);
158321158523
zSep = ", ";
158322158524
}
158323158525
if( c=='d' ){
158324
- zList = otaMPrintf(p, "%z%s\"%w\"=ota_delta(\"%w\", ?%d)",
158526
+ zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
158325158527
zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
158326158528
);
158327158529
zSep = ", ";
158328158530
}
158329158531
}
@@ -158340,20 +158542,20 @@
158340158542
** The memory for the returned string is obtained from sqlite3_malloc().
158341158543
** It is the responsibility of the caller to eventually free it using
158342158544
** sqlite3_free().
158343158545
**
158344158546
** If an OOM error is encountered when allocating space for the new
158345
-** string, an error code is left in the ota handle passed as the first
158547
+** string, an error code is left in the rbu handle passed as the first
158346158548
** argument and NULL is returned. Or, if an error has already occurred
158347158549
** when this function is called, NULL is returned immediately, without
158348158550
** attempting the allocation or modifying the stored error code.
158349158551
*/
158350
-static char *otaObjIterGetBindlist(sqlite3ota *p, int nBind){
158552
+static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
158351158553
char *zRet = 0;
158352158554
int nByte = nBind*2 + 1;
158353158555
158354
- zRet = (char*)otaMalloc(p, nByte);
158556
+ zRet = (char*)rbuMalloc(p, nByte);
158355158557
if( zRet ){
158356158558
int i;
158357158559
for(i=0; i<nBind; i++){
158358158560
zRet[i*2] = '?';
158359158561
zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
@@ -158362,21 +158564,21 @@
158362158564
return zRet;
158363158565
}
158364158566
158365158567
/*
158366158568
** The iterator currently points to a table (not index) of type
158367
-** OTA_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
158569
+** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
158368158570
** declaration for the corresponding imposter table. For example,
158369158571
** if the iterator points to a table created as:
158370158572
**
158371158573
** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
158372158574
**
158373158575
** this function returns:
158374158576
**
158375158577
** PRIMARY KEY("b", "a" DESC)
158376158578
*/
158377
-static char *otaWithoutRowidPK(sqlite3ota *p, OtaObjIter *pIter){
158579
+static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
158378158580
char *z = 0;
158379158581
assert( pIter->zIdx==0 );
158380158582
if( p->rc==SQLITE_OK ){
158381158583
const char *zSep = "PRIMARY KEY(";
158382158584
sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
@@ -158395,23 +158597,23 @@
158395158597
);
158396158598
}
158397158599
break;
158398158600
}
158399158601
}
158400
- otaFinalize(p, pXList);
158602
+ rbuFinalize(p, pXList);
158401158603
158402158604
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158403158605
if( sqlite3_column_int(pXInfo, 5) ){
158404158606
/* int iCid = sqlite3_column_int(pXInfo, 0); */
158405158607
const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
158406158608
const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
158407
- z = otaMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
158609
+ z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
158408158610
zSep = ", ";
158409158611
}
158410158612
}
158411
- z = otaMPrintf(p, "%z)", z);
158412
- otaFinalize(p, pXInfo);
158613
+ z = rbuMPrintf(p, "%z)", z);
158614
+ rbuFinalize(p, pXInfo);
158413158615
}
158414158616
return z;
158415158617
}
158416158618
158417158619
/*
@@ -158420,23 +158622,23 @@
158420158622
** iterator passed as the second argument does not currently point to
158421158623
** a table (not index) with an external primary key, this function is a
158422158624
** no-op.
158423158625
**
158424158626
** Assuming the iterator does point to a table with an external PK, this
158425
-** function creates a WITHOUT ROWID imposter table named "ota_imposter2"
158627
+** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
158426158628
** used to access that PK index. For example, if the target table is
158427158629
** declared as follows:
158428158630
**
158429158631
** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
158430158632
**
158431158633
** then the imposter table schema is:
158432158634
**
158433
-** CREATE TABLE ota_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
158635
+** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
158434158636
**
158435158637
*/
158436
-static void otaCreateImposterTable2(sqlite3ota *p, OtaObjIter *pIter){
158437
- if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_EXTERNAL ){
158638
+static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
158639
+ if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
158438158640
int tnum = pIter->iPkTnum; /* Root page of PK index */
158439158641
sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
158440158642
const char *zIdx = 0; /* Name of PK index */
158441158643
sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
158442158644
const char *zComma = "";
@@ -158458,31 +158660,31 @@
158458158660
if( zIdx ){
158459158661
p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
158460158662
sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
158461158663
);
158462158664
}
158463
- otaFinalize(p, pQuery);
158665
+ rbuFinalize(p, pQuery);
158464158666
158465158667
while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158466158668
int bKey = sqlite3_column_int(pXInfo, 5);
158467158669
if( bKey ){
158468158670
int iCid = sqlite3_column_int(pXInfo, 1);
158469158671
int bDesc = sqlite3_column_int(pXInfo, 3);
158470158672
const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
158471
- zCols = otaMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
158673
+ zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
158472158674
iCid, pIter->azTblType[iCid], zCollate
158473158675
);
158474
- zPk = otaMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
158676
+ zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
158475158677
zComma = ", ";
158476158678
}
158477158679
}
158478
- zCols = otaMPrintf(p, "%z, id INTEGER", zCols);
158479
- otaFinalize(p, pXInfo);
158680
+ zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
158681
+ rbuFinalize(p, pXInfo);
158480158682
158481158683
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158482
- otaMPrintfExec(p, p->dbMain,
158483
- "CREATE TABLE ota_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
158684
+ rbuMPrintfExec(p, p->dbMain,
158685
+ "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
158484158686
zCols, zPk
158485158687
);
158486158688
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158487158689
}
158488158690
}
@@ -158489,28 +158691,28 @@
158489158691
158490158692
/*
158491158693
** If an error has already occurred when this function is called, it
158492158694
** immediately returns zero (without doing any work). Or, if an error
158493158695
** occurs during the execution of this function, it sets the error code
158494
-** in the sqlite3ota object indicated by the first argument and returns
158696
+** in the sqlite3rbu object indicated by the first argument and returns
158495158697
** zero.
158496158698
**
158497158699
** The iterator passed as the second argument is guaranteed to point to
158498158700
** a table (not an index) when this function is called. This function
158499158701
** attempts to create any imposter table required to write to the main
158500158702
** table b-tree of the table before returning. Non-zero is returned if
158501158703
** an imposter table are created, or zero otherwise.
158502158704
**
158503
-** An imposter table is required in all cases except OTA_PK_VTAB. Only
158705
+** An imposter table is required in all cases except RBU_PK_VTAB. Only
158504158706
** virtual tables are written to directly. The imposter table has the
158505158707
** same schema as the actual target table (less any UNIQUE constraints).
158506158708
** More precisely, the "same schema" means the same columns, types,
158507158709
** collation sequences. For tables that do not have an external PRIMARY
158508158710
** KEY, it also means the same PRIMARY KEY declaration.
158509158711
*/
158510
-static void otaCreateImposterTable(sqlite3ota *p, OtaObjIter *pIter){
158511
- if( p->rc==SQLITE_OK && pIter->eType!=OTA_PK_VTAB ){
158712
+static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
158713
+ if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
158512158714
int tnum = pIter->iTnum;
158513158715
const char *zComma = "";
158514158716
char *zSql = 0;
158515158717
int iCol;
158516158718
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
@@ -158522,73 +158724,73 @@
158522158724
158523158725
p->rc = sqlite3_table_column_metadata(
158524158726
p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
158525158727
);
158526158728
158527
- if( pIter->eType==OTA_PK_IPK && pIter->abTblPk[iCol] ){
158729
+ if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
158528158730
/* If the target table column is an "INTEGER PRIMARY KEY", add
158529158731
** "PRIMARY KEY" to the imposter table column declaration. */
158530158732
zPk = "PRIMARY KEY ";
158531158733
}
158532
- zSql = otaMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
158734
+ zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
158533158735
zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
158534158736
(pIter->abNotNull[iCol] ? " NOT NULL" : "")
158535158737
);
158536158738
zComma = ", ";
158537158739
}
158538158740
158539
- if( pIter->eType==OTA_PK_WITHOUT_ROWID ){
158540
- char *zPk = otaWithoutRowidPK(p, pIter);
158741
+ if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
158742
+ char *zPk = rbuWithoutRowidPK(p, pIter);
158541158743
if( zPk ){
158542
- zSql = otaMPrintf(p, "%z, %z", zSql, zPk);
158744
+ zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
158543158745
}
158544158746
}
158545158747
158546158748
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158547
- otaMPrintfExec(p, p->dbMain, "CREATE TABLE \"ota_imp_%w\"(%z)%s",
158749
+ rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
158548158750
pIter->zTbl, zSql,
158549
- (pIter->eType==OTA_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
158751
+ (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
158550158752
);
158551158753
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158552158754
}
158553158755
}
158554158756
158555158757
/*
158556
-** Prepare a statement used to insert rows into the "ota_tmp_xxx" table.
158758
+** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
158557158759
** Specifically a statement of the form:
158558158760
**
158559
-** INSERT INTO ota_tmp_xxx VALUES(?, ?, ? ...);
158761
+** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
158560158762
**
158561158763
** The number of bound variables is equal to the number of columns in
158562
-** the target table, plus one (for the ota_control column), plus one more
158563
-** (for the ota_rowid column) if the target table is an implicit IPK or
158764
+** the target table, plus one (for the rbu_control column), plus one more
158765
+** (for the rbu_rowid column) if the target table is an implicit IPK or
158564158766
** virtual table.
158565158767
*/
158566
-static void otaObjIterPrepareTmpInsert(
158567
- sqlite3ota *p,
158568
- OtaObjIter *pIter,
158768
+static void rbuObjIterPrepareTmpInsert(
158769
+ sqlite3rbu *p,
158770
+ RbuObjIter *pIter,
158569158771
const char *zCollist,
158570
- const char *zOtaRowid
158772
+ const char *zRbuRowid
158571158773
){
158572
- int bOtaRowid = (pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE);
158573
- char *zBind = otaObjIterGetBindlist(p, pIter->nTblCol + 1 + bOtaRowid);
158774
+ int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
158775
+ char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
158574158776
if( zBind ){
158575158777
assert( pIter->pTmpInsert==0 );
158576158778
p->rc = prepareFreeAndCollectError(
158577
- p->dbOta, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
158578
- "INSERT INTO %s.'ota_tmp_%q'(ota_control,%s%s) VALUES(%z)",
158579
- p->zStateDb, pIter->zTbl, zCollist, zOtaRowid, zBind
158779
+ p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
158780
+ "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
158781
+ p->zStateDb, pIter->zTbl, zCollist, zRbuRowid, zBind
158580158782
));
158581158783
}
158582158784
}
158583158785
158584
-static void otaTmpInsertFunc(
158786
+static void rbuTmpInsertFunc(
158585158787
sqlite3_context *pCtx,
158586158788
int nVal,
158587158789
sqlite3_value **apVal
158588158790
){
158589
- sqlite3ota *p = sqlite3_user_data(pCtx);
158791
+ sqlite3rbu *p = sqlite3_user_data(pCtx);
158590158792
int rc = SQLITE_OK;
158591158793
int i;
158592158794
158593158795
for(i=0; rc==SQLITE_OK && i<nVal; i++){
158594158796
rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
@@ -158606,17 +158808,17 @@
158606158808
/*
158607158809
** Ensure that the SQLite statement handles required to update the
158608158810
** target database object currently indicated by the iterator passed
158609158811
** as the second argument are available.
158610158812
*/
158611
-static int otaObjIterPrepareAll(
158612
- sqlite3ota *p,
158613
- OtaObjIter *pIter,
158813
+static int rbuObjIterPrepareAll(
158814
+ sqlite3rbu *p,
158815
+ RbuObjIter *pIter,
158614158816
int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
158615158817
){
158616158818
assert( pIter->bCleanup==0 );
158617
- if( pIter->pSelect==0 && otaObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
158819
+ if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
158618158820
const int tnum = pIter->iTnum;
158619158821
char *zCollist = 0; /* List of indexed columns */
158620158822
char **pz = &p->zErrmsg;
158621158823
const char *zIdx = pIter->zIdx;
158622158824
char *zLimit = 0;
@@ -158632,104 +158834,104 @@
158632158834
char *zImposterPK = 0; /* Primary key declaration for imposter */
158633158835
char *zWhere = 0; /* WHERE clause on PK columns */
158634158836
char *zBind = 0;
158635158837
int nBind = 0;
158636158838
158637
- assert( pIter->eType!=OTA_PK_VTAB );
158638
- zCollist = otaObjIterGetIndexCols(
158839
+ assert( pIter->eType!=RBU_PK_VTAB );
158840
+ zCollist = rbuObjIterGetIndexCols(
158639158841
p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
158640158842
);
158641
- zBind = otaObjIterGetBindlist(p, nBind);
158843
+ zBind = rbuObjIterGetBindlist(p, nBind);
158642158844
158643158845
/* Create the imposter table used to write to this index. */
158644158846
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
158645158847
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
158646
- otaMPrintfExec(p, p->dbMain,
158647
- "CREATE TABLE \"ota_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
158848
+ rbuMPrintfExec(p, p->dbMain,
158849
+ "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
158648158850
zTbl, zImposterCols, zImposterPK
158649158851
);
158650158852
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158651158853
158652158854
/* Create the statement to insert index entries */
158653158855
pIter->nCol = nBind;
158654158856
if( p->rc==SQLITE_OK ){
158655158857
p->rc = prepareFreeAndCollectError(
158656158858
p->dbMain, &pIter->pInsert, &p->zErrmsg,
158657
- sqlite3_mprintf("INSERT INTO \"ota_imp_%w\" VALUES(%s)", zTbl, zBind)
158859
+ sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
158658158860
);
158659158861
}
158660158862
158661158863
/* And to delete index entries */
158662158864
if( p->rc==SQLITE_OK ){
158663158865
p->rc = prepareFreeAndCollectError(
158664158866
p->dbMain, &pIter->pDelete, &p->zErrmsg,
158665
- sqlite3_mprintf("DELETE FROM \"ota_imp_%w\" WHERE %s", zTbl, zWhere)
158867
+ sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
158666158868
);
158667158869
}
158668158870
158669158871
/* Create the SELECT statement to read keys in sorted order */
158670158872
if( p->rc==SQLITE_OK ){
158671158873
char *zSql;
158672
- if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158874
+ if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158673158875
zSql = sqlite3_mprintf(
158674
- "SELECT %s, ota_control FROM %s.'ota_tmp_%q' ORDER BY %s%s",
158876
+ "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
158675158877
zCollist, p->zStateDb, pIter->zTbl,
158676158878
zCollist, zLimit
158677158879
);
158678158880
}else{
158679158881
zSql = sqlite3_mprintf(
158680
- "SELECT %s, ota_control FROM 'data_%q' "
158681
- "WHERE typeof(ota_control)='integer' AND ota_control!=1 "
158882
+ "SELECT %s, rbu_control FROM 'data_%q' "
158883
+ "WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
158682158884
"UNION ALL "
158683
- "SELECT %s, ota_control FROM %s.'ota_tmp_%q' "
158885
+ "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
158684158886
"ORDER BY %s%s",
158685158887
zCollist, pIter->zTbl,
158686158888
zCollist, p->zStateDb, pIter->zTbl,
158687158889
zCollist, zLimit
158688158890
);
158689158891
}
158690
- p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz, zSql);
158892
+ p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
158691158893
}
158692158894
158693158895
sqlite3_free(zImposterCols);
158694158896
sqlite3_free(zImposterPK);
158695158897
sqlite3_free(zWhere);
158696158898
sqlite3_free(zBind);
158697158899
}else{
158698
- int bOtaRowid = (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE);
158900
+ int bRbuRowid = (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE);
158699158901
const char *zTbl = pIter->zTbl; /* Table this step applies to */
158700158902
const char *zWrite; /* Imposter table name */
158701158903
158702
- char *zBindings = otaObjIterGetBindlist(p, pIter->nTblCol + bOtaRowid);
158703
- char *zWhere = otaObjIterGetWhere(p, pIter);
158704
- char *zOldlist = otaObjIterGetOldlist(p, pIter, "old");
158705
- char *zNewlist = otaObjIterGetOldlist(p, pIter, "new");
158904
+ char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
158905
+ char *zWhere = rbuObjIterGetWhere(p, pIter);
158906
+ char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
158907
+ char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
158706158908
158707
- zCollist = otaObjIterGetCollist(p, pIter);
158909
+ zCollist = rbuObjIterGetCollist(p, pIter);
158708158910
pIter->nCol = pIter->nTblCol;
158709158911
158710158912
/* Create the SELECT statement to read keys from data_xxx */
158711158913
if( p->rc==SQLITE_OK ){
158712
- p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz,
158914
+ p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
158713158915
sqlite3_mprintf(
158714
- "SELECT %s, ota_control%s FROM 'data_%q'%s",
158715
- zCollist, (bOtaRowid ? ", ota_rowid" : ""), zTbl, zLimit
158916
+ "SELECT %s, rbu_control%s FROM 'data_%q'%s",
158917
+ zCollist, (bRbuRowid ? ", rbu_rowid" : ""), zTbl, zLimit
158716158918
)
158717158919
);
158718158920
}
158719158921
158720158922
/* Create the imposter table or tables (if required). */
158721
- otaCreateImposterTable(p, pIter);
158722
- otaCreateImposterTable2(p, pIter);
158723
- zWrite = (pIter->eType==OTA_PK_VTAB ? "" : "ota_imp_");
158923
+ rbuCreateImposterTable(p, pIter);
158924
+ rbuCreateImposterTable2(p, pIter);
158925
+ zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
158724158926
158725158927
/* Create the INSERT statement to write to the target PK b-tree */
158726158928
if( p->rc==SQLITE_OK ){
158727158929
p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
158728158930
sqlite3_mprintf(
158729158931
"INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
158730
- zWrite, zTbl, zCollist, (bOtaRowid ? ", _rowid_" : ""), zBindings
158932
+ zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
158731158933
)
158732158934
);
158733158935
}
158734158936
158735158937
/* Create the DELETE statement to write to the target PK b-tree */
@@ -158740,55 +158942,55 @@
158740158942
)
158741158943
);
158742158944
}
158743158945
158744158946
if( pIter->abIndexed ){
158745
- const char *zOtaRowid = "";
158746
- if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158747
- zOtaRowid = ", ota_rowid";
158947
+ const char *zRbuRowid = "";
158948
+ if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158949
+ zRbuRowid = ", rbu_rowid";
158748158950
}
158749158951
158750
- /* Create the ota_tmp_xxx table and the triggers to populate it. */
158751
- otaMPrintfExec(p, p->dbOta,
158752
- "CREATE TABLE IF NOT EXISTS %s.'ota_tmp_%q' AS "
158952
+ /* Create the rbu_tmp_xxx table and the triggers to populate it. */
158953
+ rbuMPrintfExec(p, p->dbRbu,
158954
+ "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
158753158955
"SELECT *%s FROM 'data_%q' WHERE 0;"
158754158956
, p->zStateDb
158755
- , zTbl, (pIter->eType==OTA_PK_EXTERNAL ? ", 0 AS ota_rowid" : "")
158957
+ , zTbl, (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
158756158958
, zTbl
158757158959
);
158758158960
158759
- otaMPrintfExec(p, p->dbMain,
158760
- "CREATE TEMP TRIGGER ota_delete_tr BEFORE DELETE ON \"%s%w\" "
158961
+ rbuMPrintfExec(p, p->dbMain,
158962
+ "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
158761158963
"BEGIN "
158762
- " SELECT ota_tmp_insert(2, %s);"
158964
+ " SELECT rbu_tmp_insert(2, %s);"
158763158965
"END;"
158764158966
158765
- "CREATE TEMP TRIGGER ota_update1_tr BEFORE UPDATE ON \"%s%w\" "
158967
+ "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
158766158968
"BEGIN "
158767
- " SELECT ota_tmp_insert(2, %s);"
158969
+ " SELECT rbu_tmp_insert(2, %s);"
158768158970
"END;"
158769158971
158770
- "CREATE TEMP TRIGGER ota_update2_tr AFTER UPDATE ON \"%s%w\" "
158972
+ "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
158771158973
"BEGIN "
158772
- " SELECT ota_tmp_insert(3, %s);"
158974
+ " SELECT rbu_tmp_insert(3, %s);"
158773158975
"END;",
158774158976
zWrite, zTbl, zOldlist,
158775158977
zWrite, zTbl, zOldlist,
158776158978
zWrite, zTbl, zNewlist
158777158979
);
158778158980
158779
- if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158780
- otaMPrintfExec(p, p->dbMain,
158781
- "CREATE TEMP TRIGGER ota_insert_tr AFTER INSERT ON \"%s%w\" "
158981
+ if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158982
+ rbuMPrintfExec(p, p->dbMain,
158983
+ "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
158782158984
"BEGIN "
158783
- " SELECT ota_tmp_insert(0, %s);"
158985
+ " SELECT rbu_tmp_insert(0, %s);"
158784158986
"END;",
158785158987
zWrite, zTbl, zNewlist
158786158988
);
158787158989
}
158788158990
158789
- otaObjIterPrepareTmpInsert(p, pIter, zCollist, zOtaRowid);
158991
+ rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
158790158992
}
158791158993
158792158994
sqlite3_free(zWhere);
158793158995
sqlite3_free(zOldlist);
158794158996
sqlite3_free(zNewlist);
@@ -158803,68 +159005,68 @@
158803159005
158804159006
/*
158805159007
** Set output variable *ppStmt to point to an UPDATE statement that may
158806159008
** be used to update the imposter table for the main table b-tree of the
158807159009
** table object that pIter currently points to, assuming that the
158808
-** ota_control column of the data_xyz table contains zMask.
159010
+** rbu_control column of the data_xyz table contains zMask.
158809159011
**
158810159012
** If the zMask string does not specify any columns to update, then this
158811159013
** is not an error. Output variable *ppStmt is set to NULL in this case.
158812159014
*/
158813
-static int otaGetUpdateStmt(
158814
- sqlite3ota *p, /* OTA handle */
158815
- OtaObjIter *pIter, /* Object iterator */
158816
- const char *zMask, /* ota_control value ('x.x.') */
159015
+static int rbuGetUpdateStmt(
159016
+ sqlite3rbu *p, /* RBU handle */
159017
+ RbuObjIter *pIter, /* Object iterator */
159018
+ const char *zMask, /* rbu_control value ('x.x.') */
158817159019
sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
158818159020
){
158819
- OtaUpdateStmt **pp;
158820
- OtaUpdateStmt *pUp = 0;
159021
+ RbuUpdateStmt **pp;
159022
+ RbuUpdateStmt *pUp = 0;
158821159023
int nUp = 0;
158822159024
158823159025
/* In case an error occurs */
158824159026
*ppStmt = 0;
158825159027
158826159028
/* Search for an existing statement. If one is found, shift it to the front
158827159029
** of the LRU queue and return immediately. Otherwise, leave nUp pointing
158828159030
** to the number of statements currently in the cache and pUp to the
158829159031
** last object in the list. */
158830
- for(pp=&pIter->pOtaUpdate; *pp; pp=&((*pp)->pNext)){
159032
+ for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
158831159033
pUp = *pp;
158832159034
if( strcmp(pUp->zMask, zMask)==0 ){
158833159035
*pp = pUp->pNext;
158834
- pUp->pNext = pIter->pOtaUpdate;
158835
- pIter->pOtaUpdate = pUp;
159036
+ pUp->pNext = pIter->pRbuUpdate;
159037
+ pIter->pRbuUpdate = pUp;
158836159038
*ppStmt = pUp->pUpdate;
158837159039
return SQLITE_OK;
158838159040
}
158839159041
nUp++;
158840159042
}
158841159043
assert( pUp==0 || pUp->pNext==0 );
158842159044
158843
- if( nUp>=SQLITE_OTA_UPDATE_CACHESIZE ){
158844
- for(pp=&pIter->pOtaUpdate; *pp!=pUp; pp=&((*pp)->pNext));
159045
+ if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
159046
+ for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
158845159047
*pp = 0;
158846159048
sqlite3_finalize(pUp->pUpdate);
158847159049
pUp->pUpdate = 0;
158848159050
}else{
158849
- pUp = (OtaUpdateStmt*)otaMalloc(p, sizeof(OtaUpdateStmt)+pIter->nTblCol+1);
159051
+ pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
158850159052
}
158851159053
158852159054
if( pUp ){
158853
- char *zWhere = otaObjIterGetWhere(p, pIter);
158854
- char *zSet = otaObjIterGetSetlist(p, pIter, zMask);
159055
+ char *zWhere = rbuObjIterGetWhere(p, pIter);
159056
+ char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
158855159057
char *zUpdate = 0;
158856159058
158857159059
pUp->zMask = (char*)&pUp[1];
158858159060
memcpy(pUp->zMask, zMask, pIter->nTblCol);
158859
- pUp->pNext = pIter->pOtaUpdate;
158860
- pIter->pOtaUpdate = pUp;
159061
+ pUp->pNext = pIter->pRbuUpdate;
159062
+ pIter->pRbuUpdate = pUp;
158861159063
158862159064
if( zSet ){
158863159065
const char *zPrefix = "";
158864159066
158865
- if( pIter->eType!=OTA_PK_VTAB ) zPrefix = "ota_imp_";
159067
+ if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
158866159068
zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
158867159069
zPrefix, pIter->zTbl, zSet, zWhere
158868159070
);
158869159071
p->rc = prepareFreeAndCollectError(
158870159072
p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
@@ -158876,11 +159078,11 @@
158876159078
}
158877159079
158878159080
return p->rc;
158879159081
}
158880159082
158881
-static sqlite3 *otaOpenDbhandle(sqlite3ota *p, const char *zName){
159083
+static sqlite3 *rbuOpenDbhandle(sqlite3rbu *p, const char *zName){
158882159084
sqlite3 *db = 0;
158883159085
if( p->rc==SQLITE_OK ){
158884159086
const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
158885159087
p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
158886159088
if( p->rc ){
@@ -158891,51 +159093,51 @@
158891159093
}
158892159094
return db;
158893159095
}
158894159096
158895159097
/*
158896
-** Open the database handle and attach the OTA database as "ota". If an
158897
-** error occurs, leave an error code and message in the OTA handle.
159098
+** Open the database handle and attach the RBU database as "rbu". If an
159099
+** error occurs, leave an error code and message in the RBU handle.
158898159100
*/
158899
-static void otaOpenDatabase(sqlite3ota *p){
159101
+static void rbuOpenDatabase(sqlite3rbu *p){
158900159102
assert( p->rc==SQLITE_OK );
158901
- assert( p->dbMain==0 && p->dbOta==0 );
159103
+ assert( p->dbMain==0 && p->dbRbu==0 );
158902159104
158903159105
p->eStage = 0;
158904
- p->dbMain = otaOpenDbhandle(p, p->zTarget);
158905
- p->dbOta = otaOpenDbhandle(p, p->zOta);
159106
+ p->dbMain = rbuOpenDbhandle(p, p->zTarget);
159107
+ p->dbRbu = rbuOpenDbhandle(p, p->zRbu);
158906159108
158907
- /* If using separate OTA and state databases, attach the state database to
158908
- ** the OTA db handle now. */
159109
+ /* If using separate RBU and state databases, attach the state database to
159110
+ ** the RBU db handle now. */
158909159111
if( p->zState ){
158910
- otaMPrintfExec(p, p->dbOta, "ATTACH %Q AS stat", p->zState);
159112
+ rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
158911159113
memcpy(p->zStateDb, "stat", 4);
158912159114
}else{
158913159115
memcpy(p->zStateDb, "main", 4);
158914159116
}
158915159117
158916159118
if( p->rc==SQLITE_OK ){
158917159119
p->rc = sqlite3_create_function(p->dbMain,
158918
- "ota_tmp_insert", -1, SQLITE_UTF8, (void*)p, otaTmpInsertFunc, 0, 0
159120
+ "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
158919159121
);
158920159122
}
158921159123
158922159124
if( p->rc==SQLITE_OK ){
158923
- p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
159125
+ p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
158924159126
}
158925
- otaMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
159127
+ rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
158926159128
158927
- /* Mark the database file just opened as an OTA target database. If
158928
- ** this call returns SQLITE_NOTFOUND, then the OTA vfs is not in use.
159129
+ /* Mark the database file just opened as an RBU target database. If
159130
+ ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
158929159131
** This is an error. */
158930159132
if( p->rc==SQLITE_OK ){
158931
- p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
159133
+ p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
158932159134
}
158933159135
158934159136
if( p->rc==SQLITE_NOTFOUND ){
158935159137
p->rc = SQLITE_ERROR;
158936
- p->zErrmsg = sqlite3_mprintf("ota vfs not found");
159138
+ p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
158937159139
}
158938159140
}
158939159141
158940159142
/*
158941159143
** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
@@ -158955,11 +159157,11 @@
158955159157
** test.db-journal => test.nal
158956159158
** test.db-wal => test.wal
158957159159
** test.db-shm => test.shm
158958159160
** test.db-mj7f3319fa => test.9fa
158959159161
*/
158960
-static void otaFileSuffix3(const char *zBase, char *z){
159162
+static void rbuFileSuffix3(const char *zBase, char *z){
158961159163
#ifdef SQLITE_ENABLE_8_3_NAMES
158962159164
#if SQLITE_ENABLE_8_3_NAMES<2
158963159165
if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
158964159166
#endif
158965159167
{
@@ -158976,11 +159178,11 @@
158976159178
** as a 64-bit integer.
158977159179
**
158978159180
** The checksum is store in the first page of xShmMap memory as an 8-byte
158979159181
** blob starting at byte offset 40.
158980159182
*/
158981
-static i64 otaShmChecksum(sqlite3ota *p){
159183
+static i64 rbuShmChecksum(sqlite3rbu *p){
158982159184
i64 iRet = 0;
158983159185
if( p->rc==SQLITE_OK ){
158984159186
sqlite3_file *pDb = p->pTargetFd->pReal;
158985159187
u32 volatile *ptr;
158986159188
p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
@@ -158993,23 +159195,23 @@
158993159195
158994159196
/*
158995159197
** This function is called as part of initializing or reinitializing an
158996159198
** incremental checkpoint.
158997159199
**
158998
-** It populates the sqlite3ota.aFrame[] array with the set of
159200
+** It populates the sqlite3rbu.aFrame[] array with the set of
158999159201
** (wal frame -> db page) copy operations required to checkpoint the
159000159202
** current wal file, and obtains the set of shm locks required to safely
159001159203
** perform the copy operations directly on the file-system.
159002159204
**
159003159205
** If argument pState is not NULL, then the incremental checkpoint is
159004159206
** being resumed. In this case, if the checksum of the wal-index-header
159005
-** following recovery is not the same as the checksum saved in the OtaState
159006
-** object, then the ota handle is set to DONE state. This occurs if some
159207
+** following recovery is not the same as the checksum saved in the RbuState
159208
+** object, then the rbu handle is set to DONE state. This occurs if some
159007159209
** other client appends a transaction to the wal file in the middle of
159008159210
** an incremental checkpoint.
159009159211
*/
159010
-static void otaSetupCheckpoint(sqlite3ota *p, OtaState *pState){
159212
+static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
159011159213
159012159214
/* If pState is NULL, then the wal file may not have been opened and
159013159215
** recovered. Running a read-statement here to ensure that doing so
159014159216
** does not interfere with the "capture" process below. */
159015159217
if( pState==0 ){
@@ -159018,20 +159220,20 @@
159018159220
p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
159019159221
}
159020159222
}
159021159223
159022159224
/* Assuming no error has occurred, run a "restart" checkpoint with the
159023
- ** sqlite3ota.eStage variable set to CAPTURE. This turns on the following
159024
- ** special behaviour in the ota VFS:
159225
+ ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
159226
+ ** special behaviour in the rbu VFS:
159025159227
**
159026159228
** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
159027159229
** the checkpoint fails with SQLITE_BUSY (normally SQLite would
159028159230
** proceed with running a passive checkpoint instead of failing).
159029159231
**
159030159232
** * Attempts to read from the *-wal file or write to the database file
159031159233
** do not perform any IO. Instead, the frame/page combinations that
159032
- ** would be read/written are recorded in the sqlite3ota.aFrame[]
159234
+ ** would be read/written are recorded in the sqlite3rbu.aFrame[]
159033159235
** array.
159034159236
**
159035159237
** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
159036159238
** READ0 and CHECKPOINT locks taken as part of the checkpoint are
159037159239
** no-ops. These locks will not be released until the connection
@@ -159047,76 +159249,76 @@
159047159249
** data from the wal file into the database file according to the
159048159250
** contents of aFrame[].
159049159251
*/
159050159252
if( p->rc==SQLITE_OK ){
159051159253
int rc2;
159052
- p->eStage = OTA_STAGE_CAPTURE;
159254
+ p->eStage = RBU_STAGE_CAPTURE;
159053159255
rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
159054159256
if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
159055159257
}
159056159258
159057159259
if( p->rc==SQLITE_OK ){
159058
- p->eStage = OTA_STAGE_CKPT;
159260
+ p->eStage = RBU_STAGE_CKPT;
159059159261
p->nStep = (pState ? pState->nRow : 0);
159060
- p->aBuf = otaMalloc(p, p->pgsz);
159061
- p->iWalCksum = otaShmChecksum(p);
159262
+ p->aBuf = rbuMalloc(p, p->pgsz);
159263
+ p->iWalCksum = rbuShmChecksum(p);
159062159264
}
159063159265
159064159266
if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
159065159267
p->rc = SQLITE_DONE;
159066
- p->eStage = OTA_STAGE_DONE;
159268
+ p->eStage = RBU_STAGE_DONE;
159067159269
}
159068159270
}
159069159271
159070159272
/*
159071159273
** Called when iAmt bytes are read from offset iOff of the wal file while
159072
-** the ota object is in capture mode. Record the frame number of the frame
159274
+** the rbu object is in capture mode. Record the frame number of the frame
159073159275
** being read in the aFrame[] array.
159074159276
*/
159075
-static int otaCaptureWalRead(sqlite3ota *pOta, i64 iOff, int iAmt){
159277
+static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
159076159278
const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
159077159279
u32 iFrame;
159078159280
159079
- if( pOta->mLock!=mReq ){
159080
- pOta->rc = SQLITE_BUSY;
159281
+ if( pRbu->mLock!=mReq ){
159282
+ pRbu->rc = SQLITE_BUSY;
159081159283
return SQLITE_INTERNAL;
159082159284
}
159083159285
159084
- pOta->pgsz = iAmt;
159085
- if( pOta->nFrame==pOta->nFrameAlloc ){
159086
- int nNew = (pOta->nFrameAlloc ? pOta->nFrameAlloc : 64) * 2;
159087
- OtaFrame *aNew;
159088
- aNew = (OtaFrame*)sqlite3_realloc(pOta->aFrame, nNew * sizeof(OtaFrame));
159286
+ pRbu->pgsz = iAmt;
159287
+ if( pRbu->nFrame==pRbu->nFrameAlloc ){
159288
+ int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
159289
+ RbuFrame *aNew;
159290
+ aNew = (RbuFrame*)sqlite3_realloc(pRbu->aFrame, nNew * sizeof(RbuFrame));
159089159291
if( aNew==0 ) return SQLITE_NOMEM;
159090
- pOta->aFrame = aNew;
159091
- pOta->nFrameAlloc = nNew;
159292
+ pRbu->aFrame = aNew;
159293
+ pRbu->nFrameAlloc = nNew;
159092159294
}
159093159295
159094159296
iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
159095
- if( pOta->iMaxFrame<iFrame ) pOta->iMaxFrame = iFrame;
159096
- pOta->aFrame[pOta->nFrame].iWalFrame = iFrame;
159097
- pOta->aFrame[pOta->nFrame].iDbPage = 0;
159098
- pOta->nFrame++;
159297
+ if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
159298
+ pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
159299
+ pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
159300
+ pRbu->nFrame++;
159099159301
return SQLITE_OK;
159100159302
}
159101159303
159102159304
/*
159103159305
** Called when a page of data is written to offset iOff of the database
159104
-** file while the ota handle is in capture mode. Record the page number
159306
+** file while the rbu handle is in capture mode. Record the page number
159105159307
** of the page being written in the aFrame[] array.
159106159308
*/
159107
-static int otaCaptureDbWrite(sqlite3ota *pOta, i64 iOff){
159108
- pOta->aFrame[pOta->nFrame-1].iDbPage = (u32)(iOff / pOta->pgsz) + 1;
159309
+static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
159310
+ pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
159109159311
return SQLITE_OK;
159110159312
}
159111159313
159112159314
/*
159113159315
** This is called as part of an incremental checkpoint operation. Copy
159114159316
** a single frame of data from the wal file into the database file, as
159115
-** indicated by the OtaFrame object.
159317
+** indicated by the RbuFrame object.
159116159318
*/
159117
-static void otaCheckpointFrame(sqlite3ota *p, OtaFrame *pFrame){
159319
+static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
159118159320
sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
159119159321
sqlite3_file *pDb = p->pTargetFd->pReal;
159120159322
i64 iOff;
159121159323
159122159324
assert( p->rc==SQLITE_OK );
@@ -159130,33 +159332,33 @@
159130159332
159131159333
159132159334
/*
159133159335
** Take an EXCLUSIVE lock on the database file.
159134159336
*/
159135
-static void otaLockDatabase(sqlite3ota *p){
159337
+static void rbuLockDatabase(sqlite3rbu *p){
159136159338
sqlite3_file *pReal = p->pTargetFd->pReal;
159137159339
assert( p->rc==SQLITE_OK );
159138159340
p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
159139159341
if( p->rc==SQLITE_OK ){
159140159342
p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
159141159343
}
159142159344
}
159143159345
159144159346
/*
159145
-** The OTA handle is currently in OTA_STAGE_OAL state, with a SHARED lock
159347
+** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
159146159348
** on the database file. This proc moves the *-oal file to the *-wal path,
159147159349
** then reopens the database file (this time in vanilla, non-oal, WAL mode).
159148
-** If an error occurs, leave an error code and error message in the ota
159350
+** If an error occurs, leave an error code and error message in the rbu
159149159351
** handle.
159150159352
*/
159151
-static void otaMoveOalFile(sqlite3ota *p){
159353
+static void rbuMoveOalFile(sqlite3rbu *p){
159152159354
const char *zBase = sqlite3_db_filename(p->dbMain, "main");
159153159355
159154159356
char *zWal = sqlite3_mprintf("%s-wal", zBase);
159155159357
char *zOal = sqlite3_mprintf("%s-oal", zBase);
159156159358
159157
- assert( p->eStage==OTA_STAGE_MOVE );
159359
+ assert( p->eStage==RBU_STAGE_MOVE );
159158159360
assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159159159361
if( zWal==0 || zOal==0 ){
159160159362
p->rc = SQLITE_NOMEM;
159161159363
}else{
159162159364
/* Move the *-oal file to *-wal. At this point connection p->db is
@@ -159164,25 +159366,25 @@
159164159366
** in WAL mode). So no other connection may be writing the db.
159165159367
**
159166159368
** In order to ensure that there are no database readers, an EXCLUSIVE
159167159369
** lock is obtained here before the *-oal is moved to *-wal.
159168159370
*/
159169
- otaLockDatabase(p);
159371
+ rbuLockDatabase(p);
159170159372
if( p->rc==SQLITE_OK ){
159171
- otaFileSuffix3(zBase, zWal);
159172
- otaFileSuffix3(zBase, zOal);
159373
+ rbuFileSuffix3(zBase, zWal);
159374
+ rbuFileSuffix3(zBase, zOal);
159173159375
159174159376
/* Re-open the databases. */
159175
- otaObjIterFinalize(&p->objiter);
159377
+ rbuObjIterFinalize(&p->objiter);
159176159378
sqlite3_close(p->dbMain);
159177
- sqlite3_close(p->dbOta);
159379
+ sqlite3_close(p->dbRbu);
159178159380
p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
159179159381
if( p->rc==SQLITE_OK ){
159180159382
p->dbMain = 0;
159181
- p->dbOta = 0;
159182
- otaOpenDatabase(p);
159183
- otaSetupCheckpoint(p, 0);
159383
+ p->dbRbu = 0;
159384
+ rbuOpenDatabase(p);
159385
+ rbuSetupCheckpoint(p, 0);
159184159386
}
159185159387
}
159186159388
}
159187159389
159188159390
sqlite3_free(zWal);
@@ -159193,36 +159395,36 @@
159193159395
** The SELECT statement iterating through the keys for the current object
159194159396
** (p->objiter.pSelect) currently points to a valid row. This function
159195159397
** determines the type of operation requested by this row and returns
159196159398
** one of the following values to indicate the result:
159197159399
**
159198
-** * OTA_INSERT
159199
-** * OTA_DELETE
159200
-** * OTA_IDX_DELETE
159201
-** * OTA_UPDATE
159400
+** * RBU_INSERT
159401
+** * RBU_DELETE
159402
+** * RBU_IDX_DELETE
159403
+** * RBU_UPDATE
159202159404
**
159203
-** If OTA_UPDATE is returned, then output variable *pzMask is set to
159405
+** If RBU_UPDATE is returned, then output variable *pzMask is set to
159204159406
** point to the text value indicating the columns to update.
159205159407
**
159206
-** If the ota_control field contains an invalid value, an error code and
159207
-** message are left in the OTA handle and zero returned.
159408
+** If the rbu_control field contains an invalid value, an error code and
159409
+** message are left in the RBU handle and zero returned.
159208159410
*/
159209
-static int otaStepType(sqlite3ota *p, const char **pzMask){
159210
- int iCol = p->objiter.nCol; /* Index of ota_control column */
159411
+static int rbuStepType(sqlite3rbu *p, const char **pzMask){
159412
+ int iCol = p->objiter.nCol; /* Index of rbu_control column */
159211159413
int res = 0; /* Return value */
159212159414
159213159415
switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
159214159416
case SQLITE_INTEGER: {
159215159417
int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
159216159418
if( iVal==0 ){
159217
- res = OTA_INSERT;
159419
+ res = RBU_INSERT;
159218159420
}else if( iVal==1 ){
159219
- res = OTA_DELETE;
159421
+ res = RBU_DELETE;
159220159422
}else if( iVal==2 ){
159221
- res = OTA_IDX_DELETE;
159423
+ res = RBU_IDX_DELETE;
159222159424
}else if( iVal==3 ){
159223
- res = OTA_IDX_INSERT;
159425
+ res = RBU_IDX_INSERT;
159224159426
}
159225159427
break;
159226159428
}
159227159429
159228159430
case SQLITE_TEXT: {
@@ -159230,21 +159432,21 @@
159230159432
if( z==0 ){
159231159433
p->rc = SQLITE_NOMEM;
159232159434
}else{
159233159435
*pzMask = (const char*)z;
159234159436
}
159235
- res = OTA_UPDATE;
159437
+ res = RBU_UPDATE;
159236159438
159237159439
break;
159238159440
}
159239159441
159240159442
default:
159241159443
break;
159242159444
}
159243159445
159244159446
if( res==0 ){
159245
- otaBadControlError(p);
159447
+ rbuBadControlError(p);
159246159448
}
159247159449
return res;
159248159450
}
159249159451
159250159452
#ifdef SQLITE_DEBUG
@@ -159258,82 +159460,82 @@
159258159460
#else
159259159461
# define assertColumnName(x,y,z)
159260159462
#endif
159261159463
159262159464
/*
159263
-** This function does the work for an sqlite3ota_step() call.
159465
+** This function does the work for an sqlite3rbu_step() call.
159264159466
**
159265159467
** The object-iterator (p->objiter) currently points to a valid object,
159266159468
** and the input cursor (p->objiter.pSelect) currently points to a valid
159267159469
** input row. Perform whatever processing is required and return.
159268159470
**
159269159471
** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
159270
-** and message is left in the OTA handle and a copy of the error code
159472
+** and message is left in the RBU handle and a copy of the error code
159271159473
** returned.
159272159474
*/
159273
-static int otaStep(sqlite3ota *p){
159274
- OtaObjIter *pIter = &p->objiter;
159475
+static int rbuStep(sqlite3rbu *p){
159476
+ RbuObjIter *pIter = &p->objiter;
159275159477
const char *zMask = 0;
159276159478
int i;
159277
- int eType = otaStepType(p, &zMask);
159479
+ int eType = rbuStepType(p, &zMask);
159278159480
159279159481
if( eType ){
159280
- assert( eType!=OTA_UPDATE || pIter->zIdx==0 );
159482
+ assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
159281159483
159282
- if( pIter->zIdx==0 && eType==OTA_IDX_DELETE ){
159283
- otaBadControlError(p);
159484
+ if( pIter->zIdx==0 && eType==RBU_IDX_DELETE ){
159485
+ rbuBadControlError(p);
159284159486
}
159285159487
else if(
159286
- eType==OTA_INSERT
159287
- || eType==OTA_DELETE
159288
- || eType==OTA_IDX_DELETE
159289
- || eType==OTA_IDX_INSERT
159488
+ eType==RBU_INSERT
159489
+ || eType==RBU_DELETE
159490
+ || eType==RBU_IDX_DELETE
159491
+ || eType==RBU_IDX_INSERT
159290159492
){
159291159493
sqlite3_value *pVal;
159292159494
sqlite3_stmt *pWriter;
159293159495
159294
- assert( eType!=OTA_UPDATE );
159295
- assert( eType!=OTA_DELETE || pIter->zIdx==0 );
159496
+ assert( eType!=RBU_UPDATE );
159497
+ assert( eType!=RBU_DELETE || pIter->zIdx==0 );
159296159498
159297
- if( eType==OTA_IDX_DELETE || eType==OTA_DELETE ){
159499
+ if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
159298159500
pWriter = pIter->pDelete;
159299159501
}else{
159300159502
pWriter = pIter->pInsert;
159301159503
}
159302159504
159303159505
for(i=0; i<pIter->nCol; i++){
159304159506
/* If this is an INSERT into a table b-tree and the table has an
159305159507
** explicit INTEGER PRIMARY KEY, check that this is not an attempt
159306159508
** to write a NULL into the IPK column. That is not permitted. */
159307
- if( eType==OTA_INSERT
159308
- && pIter->zIdx==0 && pIter->eType==OTA_PK_IPK && pIter->abTblPk[i]
159509
+ if( eType==RBU_INSERT
159510
+ && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
159309159511
&& sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
159310159512
){
159311159513
p->rc = SQLITE_MISMATCH;
159312159514
p->zErrmsg = sqlite3_mprintf("datatype mismatch");
159313159515
goto step_out;
159314159516
}
159315159517
159316
- if( eType==OTA_DELETE && pIter->abTblPk[i]==0 ){
159518
+ if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
159317159519
continue;
159318159520
}
159319159521
159320159522
pVal = sqlite3_column_value(pIter->pSelect, i);
159321159523
p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
159322159524
if( p->rc ) goto step_out;
159323159525
}
159324159526
if( pIter->zIdx==0
159325
- && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
159527
+ && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
159326159528
){
159327159529
/* For a virtual table, or a table with no primary key, the
159328159530
** SELECT statement is:
159329159531
**
159330
- ** SELECT <cols>, ota_control, ota_rowid FROM ....
159532
+ ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
159331159533
**
159332159534
** Hence column_value(pIter->nCol+1).
159333159535
*/
159334
- assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
159536
+ assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
159335159537
pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159336159538
p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
159337159539
}
159338159540
if( p->rc==SQLITE_OK ){
159339159541
sqlite3_step(pWriter);
@@ -159340,25 +159542,25 @@
159340159542
p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
159341159543
}
159342159544
}else{
159343159545
sqlite3_value *pVal;
159344159546
sqlite3_stmt *pUpdate = 0;
159345
- assert( eType==OTA_UPDATE );
159346
- otaGetUpdateStmt(p, pIter, zMask, &pUpdate);
159547
+ assert( eType==RBU_UPDATE );
159548
+ rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
159347159549
if( pUpdate ){
159348159550
for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
159349159551
char c = zMask[pIter->aiSrcOrder[i]];
159350159552
pVal = sqlite3_column_value(pIter->pSelect, i);
159351159553
if( pIter->abTblPk[i] || c=='x' || c=='d' ){
159352159554
p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
159353159555
}
159354159556
}
159355159557
if( p->rc==SQLITE_OK
159356
- && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
159558
+ && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
159357159559
){
159358
- /* Bind the ota_rowid value to column _rowid_ */
159359
- assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
159560
+ /* Bind the rbu_rowid value to column _rowid_ */
159561
+ assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
159360159562
pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159361159563
p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
159362159564
}
159363159565
if( p->rc==SQLITE_OK ){
159364159566
sqlite3_step(pUpdate);
@@ -159373,11 +159575,11 @@
159373159575
}
159374159576
159375159577
/*
159376159578
** Increment the schema cookie of the main database opened by p->dbMain.
159377159579
*/
159378
-static void otaIncrSchemaCookie(sqlite3ota *p){
159580
+static void rbuIncrSchemaCookie(sqlite3rbu *p){
159379159581
if( p->rc==SQLITE_OK ){
159380159582
int iCookie = 1000000;
159381159583
sqlite3_stmt *pStmt;
159382159584
159383159585
p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
@@ -159390,49 +159592,49 @@
159390159592
** statement reads is page 1, which is guaranteed to be in the cache.
159391159593
** And no memory allocations are required. */
159392159594
if( SQLITE_ROW==sqlite3_step(pStmt) ){
159393159595
iCookie = sqlite3_column_int(pStmt, 0);
159394159596
}
159395
- otaFinalize(p, pStmt);
159597
+ rbuFinalize(p, pStmt);
159396159598
}
159397159599
if( p->rc==SQLITE_OK ){
159398
- otaMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
159600
+ rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
159399159601
}
159400159602
}
159401159603
}
159402159604
159403159605
/*
159404
-** Update the contents of the ota_state table within the ota database. The
159405
-** value stored in the OTA_STATE_STAGE column is eStage. All other values
159406
-** are determined by inspecting the ota handle passed as the first argument.
159606
+** Update the contents of the rbu_state table within the rbu database. The
159607
+** value stored in the RBU_STATE_STAGE column is eStage. All other values
159608
+** are determined by inspecting the rbu handle passed as the first argument.
159407159609
*/
159408
-static void otaSaveState(sqlite3ota *p, int eStage){
159610
+static void rbuSaveState(sqlite3rbu *p, int eStage){
159409159611
if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
159410159612
sqlite3_stmt *pInsert = 0;
159411159613
int rc;
159412159614
159413159615
assert( p->zErrmsg==0 );
159414
- rc = prepareFreeAndCollectError(p->dbOta, &pInsert, &p->zErrmsg,
159616
+ rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
159415159617
sqlite3_mprintf(
159416
- "INSERT OR REPLACE INTO %s.ota_state(k, v) VALUES "
159618
+ "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
159417159619
"(%d, %d), "
159418159620
"(%d, %Q), "
159419159621
"(%d, %Q), "
159420159622
"(%d, %d), "
159421159623
"(%d, %d), "
159422159624
"(%d, %lld), "
159423159625
"(%d, %lld), "
159424159626
"(%d, %lld) ",
159425159627
p->zStateDb,
159426
- OTA_STATE_STAGE, eStage,
159427
- OTA_STATE_TBL, p->objiter.zTbl,
159428
- OTA_STATE_IDX, p->objiter.zIdx,
159429
- OTA_STATE_ROW, p->nStep,
159430
- OTA_STATE_PROGRESS, p->nProgress,
159431
- OTA_STATE_CKPT, p->iWalCksum,
159432
- OTA_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
159433
- OTA_STATE_OALSZ, p->iOalSz
159628
+ RBU_STATE_STAGE, eStage,
159629
+ RBU_STATE_TBL, p->objiter.zTbl,
159630
+ RBU_STATE_IDX, p->objiter.zIdx,
159631
+ RBU_STATE_ROW, p->nStep,
159632
+ RBU_STATE_PROGRESS, p->nProgress,
159633
+ RBU_STATE_CKPT, p->iWalCksum,
159634
+ RBU_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
159635
+ RBU_STATE_OALSZ, p->iOalSz
159434159636
)
159435159637
);
159436159638
assert( pInsert==0 || rc==SQLITE_OK );
159437159639
159438159640
if( rc==SQLITE_OK ){
@@ -159443,71 +159645,71 @@
159443159645
}
159444159646
}
159445159647
159446159648
159447159649
/*
159448
-** Step the OTA object.
159650
+** Step the RBU object.
159449159651
*/
159450
-SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *p){
159652
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_step(sqlite3rbu *p){
159451159653
if( p ){
159452159654
switch( p->eStage ){
159453
- case OTA_STAGE_OAL: {
159454
- OtaObjIter *pIter = &p->objiter;
159655
+ case RBU_STAGE_OAL: {
159656
+ RbuObjIter *pIter = &p->objiter;
159455159657
while( p->rc==SQLITE_OK && pIter->zTbl ){
159456159658
159457159659
if( pIter->bCleanup ){
159458
- /* Clean up the ota_tmp_xxx table for the previous table. It
159660
+ /* Clean up the rbu_tmp_xxx table for the previous table. It
159459159661
** cannot be dropped as there are currently active SQL statements.
159460159662
** But the contents can be deleted. */
159461159663
if( pIter->abIndexed ){
159462
- otaMPrintfExec(p, p->dbOta,
159463
- "DELETE FROM %s.'ota_tmp_%q'", p->zStateDb, pIter->zTbl
159664
+ rbuMPrintfExec(p, p->dbRbu,
159665
+ "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zTbl
159464159666
);
159465159667
}
159466159668
}else{
159467
- otaObjIterPrepareAll(p, pIter, 0);
159669
+ rbuObjIterPrepareAll(p, pIter, 0);
159468159670
159469159671
/* Advance to the next row to process. */
159470159672
if( p->rc==SQLITE_OK ){
159471159673
int rc = sqlite3_step(pIter->pSelect);
159472159674
if( rc==SQLITE_ROW ){
159473159675
p->nProgress++;
159474159676
p->nStep++;
159475
- return otaStep(p);
159677
+ return rbuStep(p);
159476159678
}
159477159679
p->rc = sqlite3_reset(pIter->pSelect);
159478159680
p->nStep = 0;
159479159681
}
159480159682
}
159481159683
159482
- otaObjIterNext(p, pIter);
159684
+ rbuObjIterNext(p, pIter);
159483159685
}
159484159686
159485159687
if( p->rc==SQLITE_OK ){
159486159688
assert( pIter->zTbl==0 );
159487
- otaSaveState(p, OTA_STAGE_MOVE);
159488
- otaIncrSchemaCookie(p);
159689
+ rbuSaveState(p, RBU_STAGE_MOVE);
159690
+ rbuIncrSchemaCookie(p);
159489159691
if( p->rc==SQLITE_OK ){
159490159692
p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
159491159693
}
159492159694
if( p->rc==SQLITE_OK ){
159493
- p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
159494
- }
159495
- p->eStage = OTA_STAGE_MOVE;
159496
- }
159497
- break;
159498
- }
159499
-
159500
- case OTA_STAGE_MOVE: {
159695
+ p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
159696
+ }
159697
+ p->eStage = RBU_STAGE_MOVE;
159698
+ }
159699
+ break;
159700
+ }
159701
+
159702
+ case RBU_STAGE_MOVE: {
159501159703
if( p->rc==SQLITE_OK ){
159502
- otaMoveOalFile(p);
159704
+ rbuMoveOalFile(p);
159503159705
p->nProgress++;
159504159706
}
159505159707
break;
159506159708
}
159507159709
159508
- case OTA_STAGE_CKPT: {
159710
+ case RBU_STAGE_CKPT: {
159509159711
if( p->rc==SQLITE_OK ){
159510159712
if( p->nStep>=p->nFrame ){
159511159713
sqlite3_file *pDb = p->pTargetFd->pReal;
159512159714
159513159715
/* Sync the db file */
@@ -159521,16 +159723,16 @@
159521159723
((u32 volatile*)ptr)[24] = p->iMaxFrame;
159522159724
}
159523159725
}
159524159726
159525159727
if( p->rc==SQLITE_OK ){
159526
- p->eStage = OTA_STAGE_DONE;
159728
+ p->eStage = RBU_STAGE_DONE;
159527159729
p->rc = SQLITE_DONE;
159528159730
}
159529159731
}else{
159530
- OtaFrame *pFrame = &p->aFrame[p->nStep];
159531
- otaCheckpointFrame(p, pFrame);
159732
+ RbuFrame *pFrame = &p->aFrame[p->nStep];
159733
+ rbuCheckpointFrame(p, pFrame);
159532159734
p->nStep++;
159533159735
}
159534159736
p->nProgress++;
159535159737
}
159536159738
break;
@@ -159544,78 +159746,78 @@
159544159746
return SQLITE_NOMEM;
159545159747
}
159546159748
}
159547159749
159548159750
/*
159549
-** Free an OtaState object allocated by otaLoadState().
159751
+** Free an RbuState object allocated by rbuLoadState().
159550159752
*/
159551
-static void otaFreeState(OtaState *p){
159753
+static void rbuFreeState(RbuState *p){
159552159754
if( p ){
159553159755
sqlite3_free(p->zTbl);
159554159756
sqlite3_free(p->zIdx);
159555159757
sqlite3_free(p);
159556159758
}
159557159759
}
159558159760
159559159761
/*
159560
-** Allocate an OtaState object and load the contents of the ota_state
159762
+** Allocate an RbuState object and load the contents of the rbu_state
159561159763
** table into it. Return a pointer to the new object. It is the
159562159764
** responsibility of the caller to eventually free the object using
159563159765
** sqlite3_free().
159564159766
**
159565
-** If an error occurs, leave an error code and message in the ota handle
159767
+** If an error occurs, leave an error code and message in the rbu handle
159566159768
** and return NULL.
159567159769
*/
159568
-static OtaState *otaLoadState(sqlite3ota *p){
159569
- OtaState *pRet = 0;
159770
+static RbuState *rbuLoadState(sqlite3rbu *p){
159771
+ RbuState *pRet = 0;
159570159772
sqlite3_stmt *pStmt = 0;
159571159773
int rc;
159572159774
int rc2;
159573159775
159574
- pRet = (OtaState*)otaMalloc(p, sizeof(OtaState));
159776
+ pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
159575159777
if( pRet==0 ) return 0;
159576159778
159577
- rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
159578
- sqlite3_mprintf("SELECT k, v FROM %s.ota_state", p->zStateDb)
159779
+ rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
159780
+ sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
159579159781
);
159580159782
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
159581159783
switch( sqlite3_column_int(pStmt, 0) ){
159582
- case OTA_STATE_STAGE:
159784
+ case RBU_STATE_STAGE:
159583159785
pRet->eStage = sqlite3_column_int(pStmt, 1);
159584
- if( pRet->eStage!=OTA_STAGE_OAL
159585
- && pRet->eStage!=OTA_STAGE_MOVE
159586
- && pRet->eStage!=OTA_STAGE_CKPT
159786
+ if( pRet->eStage!=RBU_STAGE_OAL
159787
+ && pRet->eStage!=RBU_STAGE_MOVE
159788
+ && pRet->eStage!=RBU_STAGE_CKPT
159587159789
){
159588159790
p->rc = SQLITE_CORRUPT;
159589159791
}
159590159792
break;
159591159793
159592
- case OTA_STATE_TBL:
159593
- pRet->zTbl = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159794
+ case RBU_STATE_TBL:
159795
+ pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159594159796
break;
159595159797
159596
- case OTA_STATE_IDX:
159597
- pRet->zIdx = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159798
+ case RBU_STATE_IDX:
159799
+ pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159598159800
break;
159599159801
159600
- case OTA_STATE_ROW:
159802
+ case RBU_STATE_ROW:
159601159803
pRet->nRow = sqlite3_column_int(pStmt, 1);
159602159804
break;
159603159805
159604
- case OTA_STATE_PROGRESS:
159806
+ case RBU_STATE_PROGRESS:
159605159807
pRet->nProgress = sqlite3_column_int64(pStmt, 1);
159606159808
break;
159607159809
159608
- case OTA_STATE_CKPT:
159810
+ case RBU_STATE_CKPT:
159609159811
pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
159610159812
break;
159611159813
159612
- case OTA_STATE_COOKIE:
159814
+ case RBU_STATE_COOKIE:
159613159815
pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
159614159816
break;
159615159817
159616
- case OTA_STATE_OALSZ:
159818
+ case RBU_STATE_OALSZ:
159617159819
pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
159618159820
break;
159619159821
159620159822
default:
159621159823
rc = SQLITE_CORRUPT;
@@ -159632,142 +159834,142 @@
159632159834
/*
159633159835
** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
159634159836
** otherwise. Either or both argument may be NULL. Two NULL values are
159635159837
** considered equal, and NULL is considered distinct from all other values.
159636159838
*/
159637
-static int otaStrCompare(const char *z1, const char *z2){
159839
+static int rbuStrCompare(const char *z1, const char *z2){
159638159840
if( z1==0 && z2==0 ) return 0;
159639159841
if( z1==0 || z2==0 ) return 1;
159640159842
return (sqlite3_stricmp(z1, z2)!=0);
159641159843
}
159642159844
159643159845
/*
159644
-** This function is called as part of sqlite3ota_open() when initializing
159645
-** an ota handle in OAL stage. If the ota update has not started (i.e.
159646
-** the ota_state table was empty) it is a no-op. Otherwise, it arranges
159647
-** things so that the next call to sqlite3ota_step() continues on from
159648
-** where the previous ota handle left off.
159846
+** This function is called as part of sqlite3rbu_open() when initializing
159847
+** an rbu handle in OAL stage. If the rbu update has not started (i.e.
159848
+** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
159849
+** things so that the next call to sqlite3rbu_step() continues on from
159850
+** where the previous rbu handle left off.
159649159851
**
159650159852
** If an error occurs, an error code and error message are left in the
159651
-** ota handle passed as the first argument.
159853
+** rbu handle passed as the first argument.
159652159854
*/
159653
-static void otaSetupOal(sqlite3ota *p, OtaState *pState){
159855
+static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
159654159856
assert( p->rc==SQLITE_OK );
159655159857
if( pState->zTbl ){
159656
- OtaObjIter *pIter = &p->objiter;
159858
+ RbuObjIter *pIter = &p->objiter;
159657159859
int rc = SQLITE_OK;
159658159860
159659159861
while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
159660
- || otaStrCompare(pIter->zIdx, pState->zIdx)
159661
- || otaStrCompare(pIter->zTbl, pState->zTbl)
159862
+ || rbuStrCompare(pIter->zIdx, pState->zIdx)
159863
+ || rbuStrCompare(pIter->zTbl, pState->zTbl)
159662159864
)){
159663
- rc = otaObjIterNext(p, pIter);
159865
+ rc = rbuObjIterNext(p, pIter);
159664159866
}
159665159867
159666159868
if( rc==SQLITE_OK && !pIter->zTbl ){
159667159869
rc = SQLITE_ERROR;
159668
- p->zErrmsg = sqlite3_mprintf("ota_state mismatch error");
159870
+ p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
159669159871
}
159670159872
159671159873
if( rc==SQLITE_OK ){
159672159874
p->nStep = pState->nRow;
159673
- rc = otaObjIterPrepareAll(p, &p->objiter, p->nStep);
159875
+ rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
159674159876
}
159675159877
159676159878
p->rc = rc;
159677159879
}
159678159880
}
159679159881
159680159882
/*
159681159883
** If there is a "*-oal" file in the file-system corresponding to the
159682159884
** target database in the file-system, delete it. If an error occurs,
159683
-** leave an error code and error message in the ota handle.
159885
+** leave an error code and error message in the rbu handle.
159684159886
*/
159685
-static void otaDeleteOalFile(sqlite3ota *p){
159887
+static void rbuDeleteOalFile(sqlite3rbu *p){
159686159888
char *zOal = sqlite3_mprintf("%s-oal", p->zTarget);
159687159889
assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159688159890
unlink(zOal);
159689159891
sqlite3_free(zOal);
159690159892
}
159691159893
159692159894
/*
159693
-** Allocate a private ota VFS for the ota handle passed as the only
159694
-** argument. This VFS will be used unless the call to sqlite3ota_open()
159895
+** Allocate a private rbu VFS for the rbu handle passed as the only
159896
+** argument. This VFS will be used unless the call to sqlite3rbu_open()
159695159897
** specified a URI with a vfs=? option in place of a target database
159696159898
** file name.
159697159899
*/
159698
-static void otaCreateVfs(sqlite3ota *p){
159900
+static void rbuCreateVfs(sqlite3rbu *p){
159699159901
int rnd;
159700159902
char zRnd[64];
159701159903
159702159904
assert( p->rc==SQLITE_OK );
159703159905
sqlite3_randomness(sizeof(int), (void*)&rnd);
159704
- sprintf(zRnd, "ota_vfs_%d", rnd);
159705
- p->rc = sqlite3ota_create_vfs(zRnd, 0);
159906
+ sprintf(zRnd, "rbu_vfs_%d", rnd);
159907
+ p->rc = sqlite3rbu_create_vfs(zRnd, 0);
159706159908
if( p->rc==SQLITE_OK ){
159707159909
sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
159708159910
assert( pVfs );
159709159911
p->zVfsName = pVfs->zName;
159710159912
}
159711159913
}
159712159914
159713159915
/*
159714
-** Destroy the private VFS created for the ota handle passed as the only
159715
-** argument by an earlier call to otaCreateVfs().
159916
+** Destroy the private VFS created for the rbu handle passed as the only
159917
+** argument by an earlier call to rbuCreateVfs().
159716159918
*/
159717
-static void otaDeleteVfs(sqlite3ota *p){
159919
+static void rbuDeleteVfs(sqlite3rbu *p){
159718159920
if( p->zVfsName ){
159719
- sqlite3ota_destroy_vfs(p->zVfsName);
159921
+ sqlite3rbu_destroy_vfs(p->zVfsName);
159720159922
p->zVfsName = 0;
159721159923
}
159722159924
}
159723159925
159724159926
/*
159725
-** Open and return a new OTA handle.
159927
+** Open and return a new RBU handle.
159726159928
*/
159727
-SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
159929
+SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_open(
159728159930
const char *zTarget,
159729
- const char *zOta,
159931
+ const char *zRbu,
159730159932
const char *zState
159731159933
){
159732
- sqlite3ota *p;
159934
+ sqlite3rbu *p;
159733159935
int nTarget = strlen(zTarget);
159734
- int nOta = strlen(zOta);
159936
+ int nRbu = strlen(zRbu);
159735159937
int nState = zState ? strlen(zState) : 0;
159736159938
159737
- p = (sqlite3ota*)sqlite3_malloc(sizeof(sqlite3ota)+nTarget+1+nOta+1+nState+1);
159939
+ p = (sqlite3rbu*)sqlite3_malloc(sizeof(sqlite3rbu)+nTarget+1+nRbu+1+nState+1);
159738159940
if( p ){
159739
- OtaState *pState = 0;
159941
+ RbuState *pState = 0;
159740159942
159741159943
/* Create the custom VFS. */
159742
- memset(p, 0, sizeof(sqlite3ota));
159743
- otaCreateVfs(p);
159944
+ memset(p, 0, sizeof(sqlite3rbu));
159945
+ rbuCreateVfs(p);
159744159946
159745159947
/* Open the target database */
159746159948
if( p->rc==SQLITE_OK ){
159747159949
p->zTarget = (char*)&p[1];
159748159950
memcpy(p->zTarget, zTarget, nTarget+1);
159749
- p->zOta = &p->zTarget[nTarget+1];
159750
- memcpy(p->zOta, zOta, nOta+1);
159951
+ p->zRbu = &p->zTarget[nTarget+1];
159952
+ memcpy(p->zRbu, zRbu, nRbu+1);
159751159953
if( zState ){
159752
- p->zState = &p->zOta[nOta+1];
159954
+ p->zState = &p->zRbu[nRbu+1];
159753159955
memcpy(p->zState, zState, nState+1);
159754159956
}
159755
- otaOpenDatabase(p);
159957
+ rbuOpenDatabase(p);
159756159958
}
159757159959
159758
- /* If it has not already been created, create the ota_state table */
159759
- otaMPrintfExec(p, p->dbOta, OTA_CREATE_STATE, p->zStateDb);
159960
+ /* If it has not already been created, create the rbu_state table */
159961
+ rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
159760159962
159761159963
if( p->rc==SQLITE_OK ){
159762
- pState = otaLoadState(p);
159964
+ pState = rbuLoadState(p);
159763159965
assert( pState || p->rc!=SQLITE_OK );
159764159966
if( p->rc==SQLITE_OK ){
159765159967
159766159968
if( pState->eStage==0 ){
159767
- otaDeleteOalFile(p);
159768
- p->eStage = OTA_STAGE_OAL;
159969
+ rbuDeleteOalFile(p);
159970
+ p->eStage = RBU_STAGE_OAL;
159769159971
}else{
159770159972
p->eStage = pState->eStage;
159771159973
}
159772159974
p->nProgress = pState->nProgress;
159773159975
p->iOalSz = pState->iOalSz;
@@ -159774,97 +159976,97 @@
159774159976
}
159775159977
}
159776159978
assert( p->rc!=SQLITE_OK || p->eStage!=0 );
159777159979
159778159980
if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
159779
- if( p->eStage==OTA_STAGE_OAL ){
159981
+ if( p->eStage==RBU_STAGE_OAL ){
159780159982
p->rc = SQLITE_ERROR;
159781159983
p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
159782
- }else if( p->eStage==OTA_STAGE_MOVE ){
159783
- p->eStage = OTA_STAGE_CKPT;
159984
+ }else if( p->eStage==RBU_STAGE_MOVE ){
159985
+ p->eStage = RBU_STAGE_CKPT;
159784159986
p->nStep = 0;
159785159987
}
159786159988
}
159787159989
159788159990
if( p->rc==SQLITE_OK
159789
- && (p->eStage==OTA_STAGE_OAL || p->eStage==OTA_STAGE_MOVE)
159991
+ && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
159790159992
&& pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
159791159993
){
159792159994
/* At this point (pTargetFd->iCookie) contains the value of the
159793159995
** change-counter cookie (the thing that gets incremented when a
159794159996
** transaction is committed in rollback mode) currently stored on
159795159997
** page 1 of the database file. */
159796159998
p->rc = SQLITE_BUSY;
159797
- p->zErrmsg = sqlite3_mprintf("database modified during ota update");
159999
+ p->zErrmsg = sqlite3_mprintf("database modified during rbu update");
159798160000
}
159799160001
159800160002
if( p->rc==SQLITE_OK ){
159801
- if( p->eStage==OTA_STAGE_OAL ){
160003
+ if( p->eStage==RBU_STAGE_OAL ){
159802160004
159803160005
/* Open transactions both databases. The *-oal file is opened or
159804160006
** created at this point. */
159805160007
p->rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
159806160008
if( p->rc==SQLITE_OK ){
159807
- p->rc = sqlite3_exec(p->dbOta, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160009
+ p->rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
159808160010
}
159809160011
159810160012
/* Point the object iterator at the first object */
159811160013
if( p->rc==SQLITE_OK ){
159812
- p->rc = otaObjIterFirst(p, &p->objiter);
160014
+ p->rc = rbuObjIterFirst(p, &p->objiter);
159813160015
}
159814160016
159815
- /* If the OTA database contains no data_xxx tables, declare the OTA
160017
+ /* If the RBU database contains no data_xxx tables, declare the RBU
159816160018
** update finished. */
159817160019
if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
159818160020
p->rc = SQLITE_DONE;
159819160021
}
159820160022
159821160023
if( p->rc==SQLITE_OK ){
159822
- otaSetupOal(p, pState);
160024
+ rbuSetupOal(p, pState);
159823160025
}
159824160026
159825
- }else if( p->eStage==OTA_STAGE_MOVE ){
160027
+ }else if( p->eStage==RBU_STAGE_MOVE ){
159826160028
/* no-op */
159827
- }else if( p->eStage==OTA_STAGE_CKPT ){
159828
- otaSetupCheckpoint(p, pState);
159829
- }else if( p->eStage==OTA_STAGE_DONE ){
160029
+ }else if( p->eStage==RBU_STAGE_CKPT ){
160030
+ rbuSetupCheckpoint(p, pState);
160031
+ }else if( p->eStage==RBU_STAGE_DONE ){
159830160032
p->rc = SQLITE_DONE;
159831160033
}else{
159832160034
p->rc = SQLITE_CORRUPT;
159833160035
}
159834160036
}
159835160037
159836
- otaFreeState(pState);
160038
+ rbuFreeState(pState);
159837160039
}
159838160040
159839160041
return p;
159840160042
}
159841160043
159842160044
159843160045
/*
159844
-** Return the database handle used by pOta.
160046
+** Return the database handle used by pRbu.
159845160047
*/
159846
-SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota *pOta, int bOta){
160048
+SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
159847160049
sqlite3 *db = 0;
159848
- if( pOta ){
159849
- db = (bOta ? pOta->dbOta : pOta->dbMain);
160050
+ if( pRbu ){
160051
+ db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
159850160052
}
159851160053
return db;
159852160054
}
159853160055
159854160056
159855160057
/*
159856
-** If the error code currently stored in the OTA handle is SQLITE_CONSTRAINT,
160058
+** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
159857160059
** then edit any error message string so as to remove all occurrences of
159858
-** the pattern "ota_imp_[0-9]*".
160060
+** the pattern "rbu_imp_[0-9]*".
159859160061
*/
159860
-static void otaEditErrmsg(sqlite3ota *p){
160062
+static void rbuEditErrmsg(sqlite3rbu *p){
159861160063
if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
159862160064
int i;
159863160065
int nErrmsg = strlen(p->zErrmsg);
159864160066
for(i=0; i<(nErrmsg-8); i++){
159865
- if( memcmp(&p->zErrmsg[i], "ota_imp_", 8)==0 ){
160067
+ if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
159866160068
int nDel = 8;
159867160069
while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
159868160070
memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
159869160071
nErrmsg -= nDel;
159870160072
}
@@ -159871,38 +160073,38 @@
159871160073
}
159872160074
}
159873160075
}
159874160076
159875160077
/*
159876
-** Close the OTA handle.
160078
+** Close the RBU handle.
159877160079
*/
159878
-SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *p, char **pzErrmsg){
160080
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
159879160081
int rc;
159880160082
if( p ){
159881160083
159882160084
/* Commit the transaction to the *-oal file. */
159883
- if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
160085
+ if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
159884160086
p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
159885160087
}
159886160088
159887
- otaSaveState(p, p->eStage);
160089
+ rbuSaveState(p, p->eStage);
159888160090
159889
- if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
159890
- p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
160091
+ if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
160092
+ p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
159891160093
}
159892160094
159893160095
/* Close any open statement handles. */
159894
- otaObjIterFinalize(&p->objiter);
160096
+ rbuObjIterFinalize(&p->objiter);
159895160097
159896160098
/* Close the open database handle and VFS object. */
159897160099
sqlite3_close(p->dbMain);
159898
- sqlite3_close(p->dbOta);
159899
- otaDeleteVfs(p);
160100
+ sqlite3_close(p->dbRbu);
160101
+ rbuDeleteVfs(p);
159900160102
sqlite3_free(p->aBuf);
159901160103
sqlite3_free(p->aFrame);
159902160104
159903
- otaEditErrmsg(p);
160105
+ rbuEditErrmsg(p);
159904160106
rc = p->rc;
159905160107
*pzErrmsg = p->zErrmsg;
159906160108
sqlite3_free(p);
159907160109
}else{
159908160110
rc = SQLITE_NOMEM;
@@ -159912,65 +160114,65 @@
159912160114
}
159913160115
159914160116
/*
159915160117
** Return the total number of key-value operations (inserts, deletes or
159916160118
** updates) that have been performed on the target database since the
159917
-** current OTA update was started.
160119
+** current RBU update was started.
159918160120
*/
159919
-SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta){
159920
- return pOta->nProgress;
160121
+SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3rbu_progress(sqlite3rbu *pRbu){
160122
+ return pRbu->nProgress;
159921160123
}
159922160124
159923160125
/**************************************************************************
159924
-** Beginning of OTA VFS shim methods. The VFS shim modifies the behaviour
160126
+** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
159925160127
** of a standard VFS in the following ways:
159926160128
**
159927160129
** 1. Whenever the first page of a main database file is read or
159928160130
** written, the value of the change-counter cookie is stored in
159929
-** ota_file.iCookie. Similarly, the value of the "write-version"
159930
-** database header field is stored in ota_file.iWriteVer. This ensures
160131
+** rbu_file.iCookie. Similarly, the value of the "write-version"
160132
+** database header field is stored in rbu_file.iWriteVer. This ensures
159931160133
** that the values are always trustworthy within an open transaction.
159932160134
**
159933
-** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (ota_file.pWalFd)
160135
+** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
159934160136
** member variable of the associated database file descriptor is set
159935160137
** to point to the new file. A mutex protected linked list of all main
159936
-** db fds opened using a particular OTA VFS is maintained at
159937
-** ota_vfs.pMain to facilitate this.
160138
+** db fds opened using a particular RBU VFS is maintained at
160139
+** rbu_vfs.pMain to facilitate this.
159938160140
**
159939
-** 3. Using a new file-control "SQLITE_FCNTL_OTA", a main db ota_file
159940
-** object can be marked as the target database of an OTA update. This
160141
+** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
160142
+** object can be marked as the target database of an RBU update. This
159941160143
** turns on the following extra special behaviour:
159942160144
**
159943160145
** 3a. If xAccess() is called to check if there exists a *-wal file
159944
-** associated with an OTA target database currently in OTA_STAGE_OAL
160146
+** associated with an RBU target database currently in RBU_STAGE_OAL
159945160147
** stage (preparing the *-oal file), the following special handling
159946160148
** applies:
159947160149
**
159948
-** * if the *-wal file does exist, return SQLITE_CANTOPEN. An OTA
160150
+** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
159949160151
** target database may not be in wal mode already.
159950160152
**
159951160153
** * if the *-wal file does not exist, set the output parameter to
159952160154
** non-zero (to tell SQLite that it does exist) anyway.
159953160155
**
159954160156
** Then, when xOpen() is called to open the *-wal file associated with
159955
-** the OTA target in OTA_STAGE_OAL stage, instead of opening the *-wal
159956
-** file, the ota vfs opens the corresponding *-oal file instead.
160157
+** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
160158
+** file, the rbu vfs opens the corresponding *-oal file instead.
159957160159
**
159958160160
** 3b. The *-shm pages returned by xShmMap() for a target db file in
159959
-** OTA_STAGE_OAL mode are actually stored in heap memory. This is to
160161
+** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
159960160162
** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
159961
-** are no-ops on target database files in OTA_STAGE_OAL mode. This is
160163
+** are no-ops on target database files in RBU_STAGE_OAL mode. This is
159962160164
** because assert() statements in some VFS implementations fail if
159963160165
** xShmLock() is called before xShmMap().
159964160166
**
159965160167
** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
159966
-** mode except OTA_STAGE_DONE (all work completed and checkpointed), it
159967
-** fails with an SQLITE_BUSY error. This is to stop OTA connections
160168
+** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
160169
+** fails with an SQLITE_BUSY error. This is to stop RBU connections
159968160170
** from automatically checkpointing a *-wal (or *-oal) file from within
159969160171
** sqlite3_close().
159970160172
**
159971
-** 3d. In OTA_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
160173
+** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
159972160174
** all xWrite() calls on the target database file perform no IO.
159973160175
** Instead the frame and page numbers that would be read and written
159974160176
** are recorded. Additionally, successful attempts to obtain exclusive
159975160177
** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
159976160178
** database file are recorded. xShmLock() calls to unlock the same
@@ -159977,28 +160179,28 @@
159977160179
** locks are no-ops (so that once obtained, these locks are never
159978160180
** relinquished). Finally, calls to xSync() on the target database
159979160181
** file fail with SQLITE_INTERNAL errors.
159980160182
*/
159981160183
159982
-static void otaUnlockShm(ota_file *p){
159983
- if( p->pOta ){
160184
+static void rbuUnlockShm(rbu_file *p){
160185
+ if( p->pRbu ){
159984160186
int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
159985160187
int i;
159986160188
for(i=0; i<SQLITE_SHM_NLOCK;i++){
159987
- if( (1<<i) & p->pOta->mLock ){
160189
+ if( (1<<i) & p->pRbu->mLock ){
159988160190
xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
159989160191
}
159990160192
}
159991
- p->pOta->mLock = 0;
160193
+ p->pRbu->mLock = 0;
159992160194
}
159993160195
}
159994160196
159995160197
/*
159996
-** Close an ota file.
160198
+** Close an rbu file.
159997160199
*/
159998
-static int otaVfsClose(sqlite3_file *pFile){
159999
- ota_file *p = (ota_file*)pFile;
160200
+static int rbuVfsClose(sqlite3_file *pFile){
160201
+ rbu_file *p = (rbu_file*)pFile;
160000160202
int rc;
160001160203
int i;
160002160204
160003160205
/* Free the contents of the apShm[] array. And the array itself. */
160004160206
for(i=0; i<p->nShm; i++){
@@ -160007,16 +160209,16 @@
160007160209
sqlite3_free(p->apShm);
160008160210
p->apShm = 0;
160009160211
sqlite3_free(p->zDel);
160010160212
160011160213
if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160012
- ota_file **pp;
160013
- sqlite3_mutex_enter(p->pOtaVfs->mutex);
160014
- for(pp=&p->pOtaVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
160214
+ rbu_file **pp;
160215
+ sqlite3_mutex_enter(p->pRbuVfs->mutex);
160216
+ for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
160015160217
*pp = p->pMainNext;
160016
- sqlite3_mutex_leave(p->pOtaVfs->mutex);
160017
- otaUnlockShm(p);
160218
+ sqlite3_mutex_leave(p->pRbuVfs->mutex);
160219
+ rbuUnlockShm(p);
160018160220
p->pReal->pMethods->xShmUnmap(p->pReal, 0);
160019160221
}
160020160222
160021160223
/* Close the underlying file handle */
160022160224
rc = p->pReal->pMethods->xClose(p->pReal);
@@ -160026,37 +160228,37 @@
160026160228
160027160229
/*
160028160230
** Read and return an unsigned 32-bit big-endian integer from the buffer
160029160231
** passed as the only argument.
160030160232
*/
160031
-static u32 otaGetU32(u8 *aBuf){
160233
+static u32 rbuGetU32(u8 *aBuf){
160032160234
return ((u32)aBuf[0] << 24)
160033160235
+ ((u32)aBuf[1] << 16)
160034160236
+ ((u32)aBuf[2] << 8)
160035160237
+ ((u32)aBuf[3]);
160036160238
}
160037160239
160038160240
/*
160039
-** Read data from an otaVfs-file.
160241
+** Read data from an rbuVfs-file.
160040160242
*/
160041
-static int otaVfsRead(
160243
+static int rbuVfsRead(
160042160244
sqlite3_file *pFile,
160043160245
void *zBuf,
160044160246
int iAmt,
160045160247
sqlite_int64 iOfst
160046160248
){
160047
- ota_file *p = (ota_file*)pFile;
160048
- sqlite3ota *pOta = p->pOta;
160249
+ rbu_file *p = (rbu_file*)pFile;
160250
+ sqlite3rbu *pRbu = p->pRbu;
160049160251
int rc;
160050160252
160051
- if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
160253
+ if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
160052160254
assert( p->openFlags & SQLITE_OPEN_WAL );
160053
- rc = otaCaptureWalRead(p->pOta, iOfst, iAmt);
160255
+ rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
160054160256
}else{
160055
- if( pOta && pOta->eStage==OTA_STAGE_OAL
160257
+ if( pRbu && pRbu->eStage==RBU_STAGE_OAL
160056160258
&& (p->openFlags & SQLITE_OPEN_WAL)
160057
- && iOfst>=pOta->iOalSz
160259
+ && iOfst>=pRbu->iOalSz
160058160260
){
160059160261
rc = SQLITE_OK;
160060160262
memset(zBuf, 0, iAmt);
160061160263
}else{
160062160264
rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
@@ -160063,92 +160265,92 @@
160063160265
}
160064160266
if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160065160267
/* These look like magic numbers. But they are stable, as they are part
160066160268
** of the definition of the SQLite file format, which may not change. */
160067160269
u8 *pBuf = (u8*)zBuf;
160068
- p->iCookie = otaGetU32(&pBuf[24]);
160270
+ p->iCookie = rbuGetU32(&pBuf[24]);
160069160271
p->iWriteVer = pBuf[19];
160070160272
}
160071160273
}
160072160274
return rc;
160073160275
}
160074160276
160075160277
/*
160076
-** Write data to an otaVfs-file.
160278
+** Write data to an rbuVfs-file.
160077160279
*/
160078
-static int otaVfsWrite(
160280
+static int rbuVfsWrite(
160079160281
sqlite3_file *pFile,
160080160282
const void *zBuf,
160081160283
int iAmt,
160082160284
sqlite_int64 iOfst
160083160285
){
160084
- ota_file *p = (ota_file*)pFile;
160085
- sqlite3ota *pOta = p->pOta;
160286
+ rbu_file *p = (rbu_file*)pFile;
160287
+ sqlite3rbu *pRbu = p->pRbu;
160086160288
int rc;
160087160289
160088
- if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
160290
+ if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
160089160291
assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
160090
- rc = otaCaptureDbWrite(p->pOta, iOfst);
160292
+ rc = rbuCaptureDbWrite(p->pRbu, iOfst);
160091160293
}else{
160092
- if( pOta && pOta->eStage==OTA_STAGE_OAL
160294
+ if( pRbu && pRbu->eStage==RBU_STAGE_OAL
160093160295
&& (p->openFlags & SQLITE_OPEN_WAL)
160094
- && iOfst>=pOta->iOalSz
160296
+ && iOfst>=pRbu->iOalSz
160095160297
){
160096
- pOta->iOalSz = iAmt + iOfst;
160298
+ pRbu->iOalSz = iAmt + iOfst;
160097160299
}
160098160300
rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
160099160301
if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160100160302
/* These look like magic numbers. But they are stable, as they are part
160101160303
** of the definition of the SQLite file format, which may not change. */
160102160304
u8 *pBuf = (u8*)zBuf;
160103
- p->iCookie = otaGetU32(&pBuf[24]);
160305
+ p->iCookie = rbuGetU32(&pBuf[24]);
160104160306
p->iWriteVer = pBuf[19];
160105160307
}
160106160308
}
160107160309
return rc;
160108160310
}
160109160311
160110160312
/*
160111
-** Truncate an otaVfs-file.
160313
+** Truncate an rbuVfs-file.
160112160314
*/
160113
-static int otaVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
160114
- ota_file *p = (ota_file*)pFile;
160315
+static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
160316
+ rbu_file *p = (rbu_file*)pFile;
160115160317
return p->pReal->pMethods->xTruncate(p->pReal, size);
160116160318
}
160117160319
160118160320
/*
160119
-** Sync an otaVfs-file.
160321
+** Sync an rbuVfs-file.
160120160322
*/
160121
-static int otaVfsSync(sqlite3_file *pFile, int flags){
160122
- ota_file *p = (ota_file *)pFile;
160123
- if( p->pOta && p->pOta->eStage==OTA_STAGE_CAPTURE ){
160323
+static int rbuVfsSync(sqlite3_file *pFile, int flags){
160324
+ rbu_file *p = (rbu_file *)pFile;
160325
+ if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
160124160326
if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160125160327
return SQLITE_INTERNAL;
160126160328
}
160127160329
return SQLITE_OK;
160128160330
}
160129160331
return p->pReal->pMethods->xSync(p->pReal, flags);
160130160332
}
160131160333
160132160334
/*
160133
-** Return the current file-size of an otaVfs-file.
160335
+** Return the current file-size of an rbuVfs-file.
160134160336
*/
160135
-static int otaVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
160136
- ota_file *p = (ota_file *)pFile;
160337
+static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
160338
+ rbu_file *p = (rbu_file *)pFile;
160137160339
return p->pReal->pMethods->xFileSize(p->pReal, pSize);
160138160340
}
160139160341
160140160342
/*
160141
-** Lock an otaVfs-file.
160343
+** Lock an rbuVfs-file.
160142160344
*/
160143
-static int otaVfsLock(sqlite3_file *pFile, int eLock){
160144
- ota_file *p = (ota_file*)pFile;
160145
- sqlite3ota *pOta = p->pOta;
160345
+static int rbuVfsLock(sqlite3_file *pFile, int eLock){
160346
+ rbu_file *p = (rbu_file*)pFile;
160347
+ sqlite3rbu *pRbu = p->pRbu;
160146160348
int rc = SQLITE_OK;
160147160349
160148160350
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160149
- if( pOta && eLock==SQLITE_LOCK_EXCLUSIVE && pOta->eStage!=OTA_STAGE_DONE ){
160351
+ if( pRbu && eLock==SQLITE_LOCK_EXCLUSIVE && pRbu->eStage!=RBU_STAGE_DONE ){
160150160352
/* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
160151160353
** prevents it from checkpointing the database from sqlite3_close(). */
160152160354
rc = SQLITE_BUSY;
160153160355
}else{
160154160356
rc = p->pReal->pMethods->xLock(p->pReal, eLock);
@@ -160156,122 +160358,122 @@
160156160358
160157160359
return rc;
160158160360
}
160159160361
160160160362
/*
160161
-** Unlock an otaVfs-file.
160363
+** Unlock an rbuVfs-file.
160162160364
*/
160163
-static int otaVfsUnlock(sqlite3_file *pFile, int eLock){
160164
- ota_file *p = (ota_file *)pFile;
160365
+static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
160366
+ rbu_file *p = (rbu_file *)pFile;
160165160367
return p->pReal->pMethods->xUnlock(p->pReal, eLock);
160166160368
}
160167160369
160168160370
/*
160169
-** Check if another file-handle holds a RESERVED lock on an otaVfs-file.
160371
+** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
160170160372
*/
160171
-static int otaVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
160172
- ota_file *p = (ota_file *)pFile;
160373
+static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
160374
+ rbu_file *p = (rbu_file *)pFile;
160173160375
return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
160174160376
}
160175160377
160176160378
/*
160177
-** File control method. For custom operations on an otaVfs-file.
160379
+** File control method. For custom operations on an rbuVfs-file.
160178160380
*/
160179
-static int otaVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
160180
- ota_file *p = (ota_file *)pFile;
160381
+static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
160382
+ rbu_file *p = (rbu_file *)pFile;
160181160383
int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
160182160384
int rc;
160183160385
160184160386
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
160185160387
|| p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
160186160388
);
160187
- if( op==SQLITE_FCNTL_OTA ){
160188
- sqlite3ota *pOta = (sqlite3ota*)pArg;
160389
+ if( op==SQLITE_FCNTL_RBU ){
160390
+ sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
160189160391
160190
- /* First try to find another OTA vfs lower down in the vfs stack. If
160392
+ /* First try to find another RBU vfs lower down in the vfs stack. If
160191160393
** one is found, this vfs will operate in pass-through mode. The lower
160192
- ** level vfs will do the special OTA handling. */
160394
+ ** level vfs will do the special RBU handling. */
160193160395
rc = xControl(p->pReal, op, pArg);
160194160396
160195160397
if( rc==SQLITE_NOTFOUND ){
160196160398
/* Now search for a zipvfs instance lower down in the VFS stack. If
160197160399
** one is found, this is an error. */
160198160400
void *dummy = 0;
160199160401
rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
160200160402
if( rc==SQLITE_OK ){
160201160403
rc = SQLITE_ERROR;
160202
- pOta->zErrmsg = sqlite3_mprintf("ota/zipvfs setup error");
160404
+ pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
160203160405
}else if( rc==SQLITE_NOTFOUND ){
160204
- pOta->pTargetFd = p;
160205
- p->pOta = pOta;
160206
- if( p->pWalFd ) p->pWalFd->pOta = pOta;
160406
+ pRbu->pTargetFd = p;
160407
+ p->pRbu = pRbu;
160408
+ if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
160207160409
rc = SQLITE_OK;
160208160410
}
160209160411
}
160210160412
return rc;
160211160413
}
160212160414
160213160415
rc = xControl(p->pReal, op, pArg);
160214160416
if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
160215
- ota_vfs *pOtaVfs = p->pOtaVfs;
160417
+ rbu_vfs *pRbuVfs = p->pRbuVfs;
160216160418
char *zIn = *(char**)pArg;
160217
- char *zOut = sqlite3_mprintf("ota(%s)/%z", pOtaVfs->base.zName, zIn);
160419
+ char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
160218160420
*(char**)pArg = zOut;
160219160421
if( zOut==0 ) rc = SQLITE_NOMEM;
160220160422
}
160221160423
160222160424
return rc;
160223160425
}
160224160426
160225160427
/*
160226
-** Return the sector-size in bytes for an otaVfs-file.
160428
+** Return the sector-size in bytes for an rbuVfs-file.
160227160429
*/
160228
-static int otaVfsSectorSize(sqlite3_file *pFile){
160229
- ota_file *p = (ota_file *)pFile;
160430
+static int rbuVfsSectorSize(sqlite3_file *pFile){
160431
+ rbu_file *p = (rbu_file *)pFile;
160230160432
return p->pReal->pMethods->xSectorSize(p->pReal);
160231160433
}
160232160434
160233160435
/*
160234
-** Return the device characteristic flags supported by an otaVfs-file.
160436
+** Return the device characteristic flags supported by an rbuVfs-file.
160235160437
*/
160236
-static int otaVfsDeviceCharacteristics(sqlite3_file *pFile){
160237
- ota_file *p = (ota_file *)pFile;
160438
+static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
160439
+ rbu_file *p = (rbu_file *)pFile;
160238160440
return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
160239160441
}
160240160442
160241160443
/*
160242160444
** Take or release a shared-memory lock.
160243160445
*/
160244
-static int otaVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
160245
- ota_file *p = (ota_file*)pFile;
160246
- sqlite3ota *pOta = p->pOta;
160446
+static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
160447
+ rbu_file *p = (rbu_file*)pFile;
160448
+ sqlite3rbu *pRbu = p->pRbu;
160247160449
int rc = SQLITE_OK;
160248160450
160249160451
#ifdef SQLITE_AMALGAMATION
160250160452
assert( WAL_CKPT_LOCK==1 );
160251160453
#endif
160252160454
160253160455
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160254
- if( pOta && (pOta->eStage==OTA_STAGE_OAL || pOta->eStage==OTA_STAGE_MOVE) ){
160456
+ if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
160255160457
/* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
160256160458
** taking this lock also prevents any checkpoints from occurring.
160257160459
** todo: really, it's not clear why this might occur, as
160258160460
** wal_autocheckpoint ought to be turned off. */
160259160461
if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
160260160462
}else{
160261160463
int bCapture = 0;
160262160464
if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
160263
- && pOta && pOta->eStage==OTA_STAGE_CAPTURE
160465
+ && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
160264160466
&& (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
160265160467
){
160266160468
bCapture = 1;
160267160469
}
160268160470
160269160471
if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
160270160472
rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
160271160473
if( bCapture && rc==SQLITE_OK ){
160272
- pOta->mLock |= (1 << ofst);
160474
+ pRbu->mLock |= (1 << ofst);
160273160475
}
160274160476
}
160275160477
}
160276160478
160277160479
return rc;
@@ -160278,26 +160480,26 @@
160278160480
}
160279160481
160280160482
/*
160281160483
** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
160282160484
*/
160283
-static int otaVfsShmMap(
160485
+static int rbuVfsShmMap(
160284160486
sqlite3_file *pFile,
160285160487
int iRegion,
160286160488
int szRegion,
160287160489
int isWrite,
160288160490
void volatile **pp
160289160491
){
160290
- ota_file *p = (ota_file*)pFile;
160492
+ rbu_file *p = (rbu_file*)pFile;
160291160493
int rc = SQLITE_OK;
160292
- int eStage = (p->pOta ? p->pOta->eStage : 0);
160494
+ int eStage = (p->pRbu ? p->pRbu->eStage : 0);
160293160495
160294
- /* If not in OTA_STAGE_OAL, allow this call to pass through. Or, if this
160295
- ** ota is in the OTA_STAGE_OAL state, use heap memory for *-shm space
160496
+ /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
160497
+ ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
160296160498
** instead of a file on disk. */
160297160499
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160298
- if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
160500
+ if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
160299160501
if( iRegion<=p->nShm ){
160300160502
int nByte = (iRegion+1) * sizeof(char*);
160301160503
char **apNew = (char**)sqlite3_realloc(p->apShm, nByte);
160302160504
if( apNew==0 ){
160303160505
rc = SQLITE_NOMEM;
@@ -160332,29 +160534,29 @@
160332160534
}
160333160535
160334160536
/*
160335160537
** Memory barrier.
160336160538
*/
160337
-static void otaVfsShmBarrier(sqlite3_file *pFile){
160338
- ota_file *p = (ota_file *)pFile;
160539
+static void rbuVfsShmBarrier(sqlite3_file *pFile){
160540
+ rbu_file *p = (rbu_file *)pFile;
160339160541
p->pReal->pMethods->xShmBarrier(p->pReal);
160340160542
}
160341160543
160342160544
/*
160343160545
** The xShmUnmap method.
160344160546
*/
160345
-static int otaVfsShmUnmap(sqlite3_file *pFile, int delFlag){
160346
- ota_file *p = (ota_file*)pFile;
160547
+static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
160548
+ rbu_file *p = (rbu_file*)pFile;
160347160549
int rc = SQLITE_OK;
160348
- int eStage = (p->pOta ? p->pOta->eStage : 0);
160550
+ int eStage = (p->pRbu ? p->pRbu->eStage : 0);
160349160551
160350160552
assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160351
- if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
160553
+ if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
160352160554
/* no-op */
160353160555
}else{
160354160556
/* Release the checkpointer and writer locks */
160355
- otaUnlockShm(p);
160557
+ rbuUnlockShm(p);
160356160558
rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
160357160559
}
160358160560
return rc;
160359160561
}
160360160562
@@ -160362,56 +160564,56 @@
160362160564
** Given that zWal points to a buffer containing a wal file name passed to
160363160565
** either the xOpen() or xAccess() VFS method, return a pointer to the
160364160566
** file-handle opened by the same database connection on the corresponding
160365160567
** database file.
160366160568
*/
160367
-static ota_file *otaFindMaindb(ota_vfs *pOtaVfs, const char *zWal){
160368
- ota_file *pDb;
160369
- sqlite3_mutex_enter(pOtaVfs->mutex);
160370
- for(pDb=pOtaVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
160371
- sqlite3_mutex_leave(pOtaVfs->mutex);
160569
+static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
160570
+ rbu_file *pDb;
160571
+ sqlite3_mutex_enter(pRbuVfs->mutex);
160572
+ for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
160573
+ sqlite3_mutex_leave(pRbuVfs->mutex);
160372160574
return pDb;
160373160575
}
160374160576
160375160577
/*
160376
-** Open an ota file handle.
160578
+** Open an rbu file handle.
160377160579
*/
160378
-static int otaVfsOpen(
160580
+static int rbuVfsOpen(
160379160581
sqlite3_vfs *pVfs,
160380160582
const char *zName,
160381160583
sqlite3_file *pFile,
160382160584
int flags,
160383160585
int *pOutFlags
160384160586
){
160385
- static sqlite3_io_methods otavfs_io_methods = {
160587
+ static sqlite3_io_methods rbuvfs_io_methods = {
160386160588
2, /* iVersion */
160387
- otaVfsClose, /* xClose */
160388
- otaVfsRead, /* xRead */
160389
- otaVfsWrite, /* xWrite */
160390
- otaVfsTruncate, /* xTruncate */
160391
- otaVfsSync, /* xSync */
160392
- otaVfsFileSize, /* xFileSize */
160393
- otaVfsLock, /* xLock */
160394
- otaVfsUnlock, /* xUnlock */
160395
- otaVfsCheckReservedLock, /* xCheckReservedLock */
160396
- otaVfsFileControl, /* xFileControl */
160397
- otaVfsSectorSize, /* xSectorSize */
160398
- otaVfsDeviceCharacteristics, /* xDeviceCharacteristics */
160399
- otaVfsShmMap, /* xShmMap */
160400
- otaVfsShmLock, /* xShmLock */
160401
- otaVfsShmBarrier, /* xShmBarrier */
160402
- otaVfsShmUnmap /* xShmUnmap */
160589
+ rbuVfsClose, /* xClose */
160590
+ rbuVfsRead, /* xRead */
160591
+ rbuVfsWrite, /* xWrite */
160592
+ rbuVfsTruncate, /* xTruncate */
160593
+ rbuVfsSync, /* xSync */
160594
+ rbuVfsFileSize, /* xFileSize */
160595
+ rbuVfsLock, /* xLock */
160596
+ rbuVfsUnlock, /* xUnlock */
160597
+ rbuVfsCheckReservedLock, /* xCheckReservedLock */
160598
+ rbuVfsFileControl, /* xFileControl */
160599
+ rbuVfsSectorSize, /* xSectorSize */
160600
+ rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
160601
+ rbuVfsShmMap, /* xShmMap */
160602
+ rbuVfsShmLock, /* xShmLock */
160603
+ rbuVfsShmBarrier, /* xShmBarrier */
160604
+ rbuVfsShmUnmap /* xShmUnmap */
160403160605
};
160404
- ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
160405
- sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
160406
- ota_file *pFd = (ota_file *)pFile;
160606
+ rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
160607
+ sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
160608
+ rbu_file *pFd = (rbu_file *)pFile;
160407160609
int rc = SQLITE_OK;
160408160610
const char *zOpen = zName;
160409160611
160410
- memset(pFd, 0, sizeof(ota_file));
160612
+ memset(pFd, 0, sizeof(rbu_file));
160411160613
pFd->pReal = (sqlite3_file*)&pFd[1];
160412
- pFd->pOtaVfs = pOtaVfs;
160614
+ pFd->pRbuVfs = pRbuVfs;
160413160615
pFd->openFlags = flags;
160414160616
if( zName ){
160415160617
if( flags & SQLITE_OPEN_MAIN_DB ){
160416160618
/* A main database has just been opened. The following block sets
160417160619
** (pFd->zWal) to point to a buffer owned by SQLite that contains
@@ -160435,13 +160637,13 @@
160435160637
}
160436160638
z += (n + 8 + 1);
160437160639
pFd->zWal = z;
160438160640
}
160439160641
else if( flags & SQLITE_OPEN_WAL ){
160440
- ota_file *pDb = otaFindMaindb(pOtaVfs, zName);
160642
+ rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
160441160643
if( pDb ){
160442
- if( pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
160644
+ if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
160443160645
/* This call is to open a *-wal file. Intead, open the *-oal. This
160444160646
** code ensures that the string passed to xOpen() is terminated by a
160445160647
** pair of '\0' bytes in case the VFS attempts to extract a URI
160446160648
** parameter from it. */
160447160649
int nCopy = strlen(zName);
@@ -160453,11 +160655,11 @@
160453160655
zCopy[nCopy+1] = '\0';
160454160656
zOpen = (const char*)(pFd->zDel = zCopy);
160455160657
}else{
160456160658
rc = SQLITE_NOMEM;
160457160659
}
160458
- pFd->pOta = pDb->pOta;
160660
+ pFd->pRbu = pDb->pRbu;
160459160661
}
160460160662
pDb->pWalFd = pFd;
160461160663
}
160462160664
}
160463160665
}
@@ -160467,16 +160669,16 @@
160467160669
}
160468160670
if( pFd->pReal->pMethods ){
160469160671
/* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
160470160672
** pointer and, if the file is a main database file, link it into the
160471160673
** mutex protected linked list of all such files. */
160472
- pFile->pMethods = &otavfs_io_methods;
160674
+ pFile->pMethods = &rbuvfs_io_methods;
160473160675
if( flags & SQLITE_OPEN_MAIN_DB ){
160474
- sqlite3_mutex_enter(pOtaVfs->mutex);
160475
- pFd->pMainNext = pOtaVfs->pMain;
160476
- pOtaVfs->pMain = pFd;
160477
- sqlite3_mutex_leave(pOtaVfs->mutex);
160676
+ sqlite3_mutex_enter(pRbuVfs->mutex);
160677
+ pFd->pMainNext = pRbuVfs->pMain;
160678
+ pRbuVfs->pMain = pFd;
160679
+ sqlite3_mutex_leave(pRbuVfs->mutex);
160478160680
}
160479160681
}else{
160480160682
sqlite3_free(pFd->zDel);
160481160683
}
160482160684
@@ -160484,48 +160686,48 @@
160484160686
}
160485160687
160486160688
/*
160487160689
** Delete the file located at zPath.
160488160690
*/
160489
-static int otaVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
160490
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160691
+static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
160692
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160491160693
return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
160492160694
}
160493160695
160494160696
/*
160495160697
** Test for access permissions. Return true if the requested permission
160496160698
** is available, or false otherwise.
160497160699
*/
160498
-static int otaVfsAccess(
160700
+static int rbuVfsAccess(
160499160701
sqlite3_vfs *pVfs,
160500160702
const char *zPath,
160501160703
int flags,
160502160704
int *pResOut
160503160705
){
160504
- ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
160505
- sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
160706
+ rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
160707
+ sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
160506160708
int rc;
160507160709
160508160710
rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
160509160711
160510
- /* If this call is to check if a *-wal file associated with an OTA target
160511
- ** database connection exists, and the OTA update is in OTA_STAGE_OAL,
160712
+ /* If this call is to check if a *-wal file associated with an RBU target
160713
+ ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
160512160714
** the following special handling is activated:
160513160715
**
160514160716
** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
160515
- ** ensures that the OTA extension never tries to update a database
160717
+ ** ensures that the RBU extension never tries to update a database
160516160718
** in wal mode, even if the first page of the database file has
160517160719
** been damaged.
160518160720
**
160519160721
** b) if the *-wal file does not exist, claim that it does anyway,
160520160722
** causing SQLite to call xOpen() to open it. This call will also
160521
- ** be intercepted (see the otaVfsOpen() function) and the *-oal
160723
+ ** be intercepted (see the rbuVfsOpen() function) and the *-oal
160522160724
** file opened instead.
160523160725
*/
160524160726
if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
160525
- ota_file *pDb = otaFindMaindb(pOtaVfs, zPath);
160526
- if( pDb && pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
160727
+ rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
160728
+ if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
160527160729
if( *pResOut ){
160528160730
rc = SQLITE_CANTOPEN;
160529160731
}else{
160530160732
*pResOut = 1;
160531160733
}
@@ -160538,151 +160740,151 @@
160538160740
/*
160539160741
** Populate buffer zOut with the full canonical pathname corresponding
160540160742
** to the pathname in zPath. zOut is guaranteed to point to a buffer
160541160743
** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
160542160744
*/
160543
-static int otaVfsFullPathname(
160745
+static int rbuVfsFullPathname(
160544160746
sqlite3_vfs *pVfs,
160545160747
const char *zPath,
160546160748
int nOut,
160547160749
char *zOut
160548160750
){
160549
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160751
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160550160752
return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
160551160753
}
160552160754
160553160755
#ifndef SQLITE_OMIT_LOAD_EXTENSION
160554160756
/*
160555160757
** Open the dynamic library located at zPath and return a handle.
160556160758
*/
160557
-static void *otaVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
160558
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160759
+static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
160760
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160559160761
return pRealVfs->xDlOpen(pRealVfs, zPath);
160560160762
}
160561160763
160562160764
/*
160563160765
** Populate the buffer zErrMsg (size nByte bytes) with a human readable
160564160766
** utf-8 string describing the most recent error encountered associated
160565160767
** with dynamic libraries.
160566160768
*/
160567
-static void otaVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
160568
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160769
+static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
160770
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160569160771
pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
160570160772
}
160571160773
160572160774
/*
160573160775
** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
160574160776
*/
160575
-static void (*otaVfsDlSym(
160777
+static void (*rbuVfsDlSym(
160576160778
sqlite3_vfs *pVfs,
160577160779
void *pArg,
160578160780
const char *zSym
160579160781
))(void){
160580
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160782
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160581160783
return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
160582160784
}
160583160785
160584160786
/*
160585160787
** Close the dynamic library handle pHandle.
160586160788
*/
160587
-static void otaVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
160588
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160789
+static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
160790
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160589160791
return pRealVfs->xDlClose(pRealVfs, pHandle);
160590160792
}
160591160793
#endif /* SQLITE_OMIT_LOAD_EXTENSION */
160592160794
160593160795
/*
160594160796
** Populate the buffer pointed to by zBufOut with nByte bytes of
160595160797
** random data.
160596160798
*/
160597
-static int otaVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
160598
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160799
+static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
160800
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160599160801
return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
160600160802
}
160601160803
160602160804
/*
160603160805
** Sleep for nMicro microseconds. Return the number of microseconds
160604160806
** actually slept.
160605160807
*/
160606
-static int otaVfsSleep(sqlite3_vfs *pVfs, int nMicro){
160607
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160808
+static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
160809
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160608160810
return pRealVfs->xSleep(pRealVfs, nMicro);
160609160811
}
160610160812
160611160813
/*
160612160814
** Return the current time as a Julian Day number in *pTimeOut.
160613160815
*/
160614
-static int otaVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
160615
- sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160816
+static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
160817
+ sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160616160818
return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
160617160819
}
160618160820
160619160821
/*
160620160822
** No-op.
160621160823
*/
160622
-static int otaVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
160824
+static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
160623160825
return 0;
160624160826
}
160625160827
160626160828
/*
160627
-** Deregister and destroy an OTA vfs created by an earlier call to
160628
-** sqlite3ota_create_vfs().
160829
+** Deregister and destroy an RBU vfs created by an earlier call to
160830
+** sqlite3rbu_create_vfs().
160629160831
*/
160630
-SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName){
160832
+SQLITE_API void SQLITE_STDCALL sqlite3rbu_destroy_vfs(const char *zName){
160631160833
sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
160632
- if( pVfs && pVfs->xOpen==otaVfsOpen ){
160633
- sqlite3_mutex_free(((ota_vfs*)pVfs)->mutex);
160834
+ if( pVfs && pVfs->xOpen==rbuVfsOpen ){
160835
+ sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
160634160836
sqlite3_vfs_unregister(pVfs);
160635160837
sqlite3_free(pVfs);
160636160838
}
160637160839
}
160638160840
160639160841
/*
160640
-** Create an OTA VFS named zName that accesses the underlying file-system
160842
+** Create an RBU VFS named zName that accesses the underlying file-system
160641160843
** via existing VFS zParent. The new object is registered as a non-default
160642160844
** VFS with SQLite before returning.
160643160845
*/
160644
-SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent){
160846
+SQLITE_API int SQLITE_STDCALL sqlite3rbu_create_vfs(const char *zName, const char *zParent){
160645160847
160646160848
/* Template for VFS */
160647160849
static sqlite3_vfs vfs_template = {
160648160850
1, /* iVersion */
160649160851
0, /* szOsFile */
160650160852
0, /* mxPathname */
160651160853
0, /* pNext */
160652160854
0, /* zName */
160653160855
0, /* pAppData */
160654
- otaVfsOpen, /* xOpen */
160655
- otaVfsDelete, /* xDelete */
160656
- otaVfsAccess, /* xAccess */
160657
- otaVfsFullPathname, /* xFullPathname */
160856
+ rbuVfsOpen, /* xOpen */
160857
+ rbuVfsDelete, /* xDelete */
160858
+ rbuVfsAccess, /* xAccess */
160859
+ rbuVfsFullPathname, /* xFullPathname */
160658160860
160659160861
#ifndef SQLITE_OMIT_LOAD_EXTENSION
160660
- otaVfsDlOpen, /* xDlOpen */
160661
- otaVfsDlError, /* xDlError */
160662
- otaVfsDlSym, /* xDlSym */
160663
- otaVfsDlClose, /* xDlClose */
160862
+ rbuVfsDlOpen, /* xDlOpen */
160863
+ rbuVfsDlError, /* xDlError */
160864
+ rbuVfsDlSym, /* xDlSym */
160865
+ rbuVfsDlClose, /* xDlClose */
160664160866
#else
160665160867
0, 0, 0, 0,
160666160868
#endif
160667160869
160668
- otaVfsRandomness, /* xRandomness */
160669
- otaVfsSleep, /* xSleep */
160670
- otaVfsCurrentTime, /* xCurrentTime */
160671
- otaVfsGetLastError, /* xGetLastError */
160870
+ rbuVfsRandomness, /* xRandomness */
160871
+ rbuVfsSleep, /* xSleep */
160872
+ rbuVfsCurrentTime, /* xCurrentTime */
160873
+ rbuVfsGetLastError, /* xGetLastError */
160672160874
0, /* xCurrentTimeInt64 (version 2) */
160673160875
0, 0, 0 /* Unimplemented version 3 methods */
160674160876
};
160675160877
160676
- ota_vfs *pNew = 0; /* Newly allocated VFS */
160878
+ rbu_vfs *pNew = 0; /* Newly allocated VFS */
160677160879
int nName;
160678160880
int rc = SQLITE_OK;
160679160881
160680160882
int nByte;
160681160883
nName = strlen(zName);
160682
- nByte = sizeof(ota_vfs) + nName + 1;
160683
- pNew = (ota_vfs*)sqlite3_malloc(nByte);
160884
+ nByte = sizeof(rbu_vfs) + nName + 1;
160885
+ pNew = (rbu_vfs*)sqlite3_malloc(nByte);
160684160886
if( pNew==0 ){
160685160887
rc = SQLITE_NOMEM;
160686160888
}else{
160687160889
sqlite3_vfs *pParent; /* Parent VFS */
160688160890
memset(pNew, 0, nByte);
@@ -160691,11 +160893,11 @@
160691160893
rc = SQLITE_NOTFOUND;
160692160894
}else{
160693160895
char *zSpace;
160694160896
memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
160695160897
pNew->base.mxPathname = pParent->mxPathname;
160696
- pNew->base.szOsFile = sizeof(ota_file) + pParent->szOsFile;
160898
+ pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
160697160899
pNew->pRealVfs = pParent;
160698160900
pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
160699160901
memcpy(zSpace, zName, nName);
160700160902
160701160903
/* Allocate the mutex and register the new VFS (not as the default) */
@@ -160717,13 +160919,13 @@
160717160919
}
160718160920
160719160921
160720160922
/**************************************************************************/
160721160923
160722
-#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA) */
160924
+#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
160723160925
160724
-/************** End of sqlite3ota.c ******************************************/
160926
+/************** End of sqlite3rbu.c ******************************************/
160725160927
/************** Begin file dbstat.c ******************************************/
160726160928
/*
160727160929
** 2010 July 12
160728160930
**
160729160931
** The author disclaims copyright to this source code. In place of
@@ -160741,10 +160943,11 @@
160741160943
** information from an SQLite database in order to implement the
160742160944
** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
160743160945
** for an example implementation.
160744160946
*/
160745160947
160948
+/* #include "sqliteInt.h" ** Requires access to internal data structures ** */
160746160949
#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
160747160950
&& !defined(SQLITE_OMIT_VIRTUALTABLE)
160748160951
160749160952
/*
160750160953
** Page paths:
160751160954
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -325,11 +325,11 @@
325 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
326 ** [sqlite_version()] and [sqlite_source_id()].
327 */
328 #define SQLITE_VERSION "3.8.11"
329 #define SQLITE_VERSION_NUMBER 3008011
330 #define SQLITE_SOURCE_ID "2015-07-08 16:22:42 5348ffc3fda5168c1e9e14aa88b0c6aedbda7c94"
331
332 /*
333 ** CAPI3REF: Run-Time Library Version Numbers
334 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
335 **
@@ -1179,13 +1179,13 @@
1179 **
1180 ** <li>[[SQLITE_FCNTL_ZIPVFS]]
1181 ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1182 ** VFS should return SQLITE_NOTFOUND for this opcode.
1183 **
1184 ** <li>[[SQLITE_FCNTL_OTA]]
1185 ** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
1186 ** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
1187 ** this opcode.
1188 ** </ul>
1189 */
1190 #define SQLITE_FCNTL_LOCKSTATE 1
1191 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -1209,11 +1209,11 @@
1209 #define SQLITE_FCNTL_SYNC 21
1210 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
1211 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
1212 #define SQLITE_FCNTL_WAL_BLOCK 24
1213 #define SQLITE_FCNTL_ZIPVFS 25
1214 #define SQLITE_FCNTL_OTA 26
1215
1216 /* deprecated names */
1217 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1218 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1219 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -8361,11 +8361,11 @@
8361
8362 /*
8363 ** Make sure that the compiler intrinsics we desire are enabled when
8364 ** compiling with an appropriate version of MSVC.
8365 */
8366 #if defined(_MSC_VER) && _MSC_VER>=1300
8367 # include <intrin.h>
8368 # pragma intrinsic(_byteswap_ushort)
8369 # pragma intrinsic(_byteswap_ulong)
8370 #endif
8371
@@ -9189,11 +9189,13 @@
9189 #endif
9190 #ifndef SQLITE_MAX_MMAP_SIZE
9191 # if defined(__linux__) \
9192 || defined(_WIN32) \
9193 || (defined(__APPLE__) && defined(__MACH__)) \
9194 || defined(__sun)
 
 
9195 # define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
9196 # else
9197 # define SQLITE_MAX_MMAP_SIZE 0
9198 # endif
9199 # define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
@@ -13883,10 +13885,11 @@
13883 **
13884 *************************************************************************
13885 **
13886 ** This file contains definitions of global variables and constants.
13887 */
 
13888
13889 /* An array to map all upper-case characters into their corresponding
13890 ** lower-case character.
13891 **
13892 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
@@ -14122,10 +14125,11 @@
14122 */
14123 #ifndef SQLITE_OMIT_WSD
14124 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
14125 #endif
14126
 
14127 /*
14128 ** Properties of opcodes. The OPFLG_INITIALIZER macro is
14129 ** created by mkopcodeh.awk during compilation. Data is obtained
14130 ** from the comments following the "case OP_xxxx:" statements in
14131 ** the vdbe.c file.
@@ -14150,10 +14154,11 @@
14150 ** SQLite was built with.
14151 */
14152
14153 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
14154
 
14155
14156 /*
14157 ** An array of names of all compile-time options. This array should
14158 ** be sorted A-Z.
14159 **
@@ -14579,10 +14584,11 @@
14579 *************************************************************************
14580 **
14581 ** This module implements the sqlite3_status() interface and related
14582 ** functionality.
14583 */
 
14584 /************** Include vdbeInt.h in the middle of status.c ******************/
14585 /************** Begin file vdbeInt.h *****************************************/
14586 /*
14587 ** 2003 September 6
14588 **
@@ -15462,10 +15468,11 @@
15462 ** Astronomical Algorithms, 2nd Edition, 1998
15463 ** ISBM 0-943396-61-1
15464 ** Willmann-Bell, Inc
15465 ** Richmond, Virginia (USA)
15466 */
 
15467 /* #include <stdlib.h> */
15468 /* #include <assert.h> */
15469 #include <time.h>
15470
15471 #ifndef SQLITE_OMIT_DATETIME_FUNCS
@@ -15773,11 +15780,11 @@
15773 Z = (int)((p->iJD + 43200000)/86400000);
15774 A = (int)((Z - 1867216.25)/36524.25);
15775 A = Z + 1 + A - (A/4);
15776 B = A + 1524;
15777 C = (int)((B - 122.1)/365.25);
15778 D = (36525*C)/100;
15779 E = (int)((B-D)/30.6001);
15780 X1 = (int)(30.6001*E);
15781 p->D = B - D - X1;
15782 p->M = E<14 ? E-1 : E-13;
15783 p->Y = p->M>2 ? C - 4716 : C - 4715;
@@ -16574,10 +16581,11 @@
16574 **
16575 ** This file contains OS interface code that is common to all
16576 ** architectures.
16577 */
16578 #define _SQLITE_OS_C_ 1
 
16579 #undef _SQLITE_OS_C_
16580
16581 /*
16582 ** The default SQLite sqlite3_vfs implementations do not allocate
16583 ** memory (actually, os_unix.c allocates a small amount of memory
@@ -16980,10 +16988,11 @@
16980 ** is completely recoverable simply by not carrying out the resize. The
16981 ** hash table will continue to function normally. So a malloc failure
16982 ** during a hash table resize is a benign fault.
16983 */
16984
 
16985
16986 #ifndef SQLITE_OMIT_BUILTIN_TEST
16987
16988 /*
16989 ** Global variables.
@@ -17061,10 +17070,11 @@
17061 ** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
17062 ** here always fail. SQLite will not operate with these drivers. These
17063 ** are merely placeholders. Real drivers must be substituted using
17064 ** sqlite3_config() before SQLite will operate.
17065 */
 
17066
17067 /*
17068 ** This version of the memory allocator is the default. It is
17069 ** used when no other memory allocator is specified using compile-time
17070 ** macros.
@@ -17147,10 +17157,11 @@
17147 ** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
17148 ** _msize() on windows systems. This might
17149 ** be necessary when compiling for Delphi,
17150 ** for example.
17151 */
 
17152
17153 /*
17154 ** This version of the memory allocator is the default. It is
17155 ** used when no other memory allocator is specified using compile-time
17156 ** macros.
@@ -17422,10 +17433,11 @@
17422 ** leaks and memory usage errors.
17423 **
17424 ** This file contains implementations of the low-level memory allocation
17425 ** routines specified in the sqlite3_mem_methods object.
17426 */
 
17427
17428 /*
17429 ** This version of the memory allocator is used only if the
17430 ** SQLITE_MEMDEBUG macro is defined
17431 */
@@ -17956,10 +17968,11 @@
17956 ** be changed.
17957 **
17958 ** This version of the memory allocation subsystem is included
17959 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
17960 */
 
17961
17962 /*
17963 ** This version of the memory allocator is only built into the library
17964 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
17965 ** mean that the library will use a memory-pool by default, just that
@@ -18670,10 +18683,11 @@
18670 ** N >= M*(1 + log2(n)/2) - n + 1
18671 **
18672 ** The sqlite3_status() logic tracks the maximum values of n and M so
18673 ** that an application can, at any time, verify this constraint.
18674 */
 
18675
18676 /*
18677 ** This version of the memory allocator is used only when
18678 ** SQLITE_ENABLE_MEMSYS5 is defined.
18679 */
@@ -19213,10 +19227,11 @@
19213 *************************************************************************
19214 ** This file contains the C functions that implement mutexes.
19215 **
19216 ** This file contains code that is common across all mutex implementations.
19217 */
 
19218
19219 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
19220 /*
19221 ** For debugging purposes, record when the mutex subsystem is initialized
19222 ** and uninitialized so that we can assert() if there is an attempt to
@@ -19386,10 +19401,11 @@
19386 **
19387 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
19388 ** that does error checking on mutexes to make sure they are being
19389 ** called correctly.
19390 */
 
19391
19392 #ifndef SQLITE_MUTEX_OMIT
19393
19394 #ifndef SQLITE_DEBUG
19395 /*
@@ -19589,10 +19605,11 @@
19589 ** May you share freely, never taking more than you give.
19590 **
19591 *************************************************************************
19592 ** This file contains the C functions that implement mutexes for pthreads
19593 */
 
19594
19595 /*
19596 ** The code in this file is only used if we are compiling threadsafe
19597 ** under unix with pthreads.
19598 **
@@ -19963,10 +19980,11 @@
19963 ** May you share freely, never taking more than you give.
19964 **
19965 *************************************************************************
19966 ** This file contains the C functions that implement mutexes for Win32.
19967 */
 
19968
19969 #if SQLITE_OS_WIN
19970 /*
19971 ** Include code that is common to all os_*.c files
19972 */
@@ -20638,10 +20656,11 @@
20638 **
20639 *************************************************************************
20640 **
20641 ** Memory allocation functions used throughout sqlite.
20642 */
 
20643 /* #include <stdarg.h> */
20644
20645 /*
20646 ** Attempt to release up to n bytes of non-essential memory currently
20647 ** held by SQLite. An example of non-essential memory is memory used to
@@ -21449,10 +21468,11 @@
21449 ** This file contains code for a set of "printf"-like routines. These
21450 ** routines format strings much like the printf() from the standard C
21451 ** library, though the implementation here has enhancements to support
21452 ** SQLite.
21453 */
 
21454
21455 /*
21456 ** Conversion types fall into various categories as defined by the
21457 ** following enumeration.
21458 */
@@ -22451,10 +22471,15 @@
22451 ** stack space on small-stack systems when logging is disabled.
22452 **
22453 ** sqlite3_log() must render into a static buffer. It cannot dynamically
22454 ** allocate memory because it might be called while the memory allocator
22455 ** mutex is held.
 
 
 
 
 
22456 */
22457 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
22458 StrAccum acc; /* String accumulator */
22459 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
22460
@@ -22526,10 +22551,11 @@
22526 ** analysis.
22527 **
22528 ** The interfaces in this file is only available when compiling
22529 ** with SQLITE_DEBUG.
22530 */
 
22531 #ifdef SQLITE_DEBUG
22532
22533 /*
22534 ** Add a new subitem to the tree. The moreToFollow flag indicates that this
22535 ** is not the last item in the tree.
@@ -22957,10 +22983,11 @@
22957 ** generator (PRNG) for SQLite.
22958 **
22959 ** Random numbers are used by some of the database backends in order
22960 ** to generate random integer keys for tables or random filenames.
22961 */
 
22962
22963
22964 /* All threads share a single random number generator.
22965 ** This structure is the current state of the generator.
22966 */
@@ -23103,11 +23130,13 @@
23103 ** single threaded systems. Nothing in SQLite requires multiple threads.
23104 ** This interface exists so that applications that want to take advantage
23105 ** of multiple cores can do so, while also allowing applications to stay
23106 ** single-threaded if desired.
23107 */
 
23108 #if SQLITE_OS_WIN
 
23109 #endif
23110
23111 #if SQLITE_MAX_WORKER_THREADS>0
23112
23113 /********************************* Unix Pthreads ****************************/
@@ -23377,11 +23406,13 @@
23377 ** BOM or Byte Order Mark:
23378 ** 0xff 0xfe little-endian utf-16 follows
23379 ** 0xfe 0xff big-endian utf-16 follows
23380 **
23381 */
 
23382 /* #include <assert.h> */
 
23383
23384 #ifndef SQLITE_AMALGAMATION
23385 /*
23386 ** The following constant value is used by the SQLITE_BIGENDIAN and
23387 ** SQLITE_LITTLEENDIAN macros.
@@ -23890,10 +23921,11 @@
23890 **
23891 ** This file contains functions for allocating memory, comparing
23892 ** strings, and stuff like that.
23893 **
23894 */
 
23895 /* #include <stdarg.h> */
23896 #if HAVE_ISNAN || SQLITE_HAVE_ISNAN
23897 # include <math.h>
23898 #endif
23899
@@ -25286,10 +25318,11 @@
25286 **
25287 *************************************************************************
25288 ** This is the implementation of generic hash-tables
25289 ** used in SQLite.
25290 */
 
25291 /* #include <assert.h> */
25292
25293 /* Turn bulk memory into a hash table object by initializing the
25294 ** fields of the Hash structure.
25295 **
@@ -25763,10 +25796,11 @@
25763 ** * sqlite3_vfs method implementations.
25764 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
25765 ** * Definitions of sqlite3_vfs objects for all locking methods
25766 ** plus implementations of sqlite3_os_init() and sqlite3_os_end().
25767 */
 
25768 #if SQLITE_OS_UNIX /* This file is used on unix only */
25769
25770 /*
25771 ** There are various methods for file locking used for concurrency
25772 ** control:
@@ -33497,10 +33531,11 @@
33497 **
33498 ******************************************************************************
33499 **
33500 ** This file contains code that is specific to Windows.
33501 */
 
33502 #if SQLITE_OS_WIN /* This file is used for Windows only */
33503
33504 /*
33505 ** Include code that is common to all os_*.c files
33506 */
@@ -33705,10 +33740,11 @@
33705 /************** Continuing where we left off in os_win.c *********************/
33706
33707 /*
33708 ** Include the header file for the Windows VFS.
33709 */
 
33710
33711 /*
33712 ** Compiling and using WAL mode requires several APIs that are only
33713 ** available in Windows platforms based on the NT kernel.
33714 */
@@ -39358,10 +39394,11 @@
39358 ** sometimes grow into tens of thousands or larger. The size of the
39359 ** Bitvec object is the number of pages in the database file at the
39360 ** start of a transaction, and is thus usually less than a few thousand,
39361 ** but can be as large as 2 billion for a really big database.
39362 */
 
39363
39364 /* Size of the Bitvec structure in bytes. */
39365 #define BITVEC_SZ 512
39366
39367 /* Round the union size down to the nearest pointer boundary, since that's how
@@ -39747,10 +39784,11 @@
39747 ** May you share freely, never taking more than you give.
39748 **
39749 *************************************************************************
39750 ** This file implements that page cache.
39751 */
 
39752
39753 /*
39754 ** A complete page cache is an instance of this structure.
39755 */
39756 struct PCache {
@@ -40488,10 +40526,11 @@
40488 **
40489 ** Earlier versions of SQLite used only methods (1) and (2). But experiments
40490 ** show that method (3) with N==100 provides about a 5% performance boost for
40491 ** common workloads.
40492 */
 
40493
40494 typedef struct PCache1 PCache1;
40495 typedef struct PgHdr1 PgHdr1;
40496 typedef struct PgFreeslot PgFreeslot;
40497 typedef struct PGroup PGroup;
@@ -40598,10 +40637,11 @@
40598 ** fixed at sqlite3_initialize() time and do not require mutex protection.
40599 ** The nFreeSlot and pFree values do require mutex protection.
40600 */
40601 int isInit; /* True if initialized */
40602 int separateCache; /* Use a new PGroup for each PCache */
 
40603 int szSlot; /* Size of each free slot */
40604 int nSlot; /* The number of pcache slots */
40605 int nReserve; /* Try to keep nFreeSlot above this */
40606 void *pStart, *pEnd; /* Bounds of global page cache memory */
40607 /* Above requires no mutex. Use mutex below for variable that follow. */
@@ -40665,10 +40705,47 @@
40665 pBuf = (void*)&((char*)pBuf)[sz];
40666 }
40667 pcache1.pEnd = pBuf;
40668 }
40669 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40670
40671 /*
40672 ** Malloc function used within this file to allocate space from the buffer
40673 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
40674 ** such buffer exists or there is no space left in it, this function falls
@@ -40766,11 +40843,11 @@
40766 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
40767 PgHdr1 *p = 0;
40768 void *pPg;
40769
40770 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
40771 if( pCache->pFree ){
40772 p = pCache->pFree;
40773 pCache->pFree = p->pNext;
40774 p->pNext = 0;
40775 }else{
40776 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
@@ -40970,18 +41047,23 @@
40970
40971 /*
40972 ** If there are currently more than nMaxPage pages allocated, try
40973 ** to recycle pages to reduce the number allocated to nMaxPage.
40974 */
40975 static void pcache1EnforceMaxPage(PGroup *pGroup){
 
40976 assert( sqlite3_mutex_held(pGroup->mutex) );
40977 while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
40978 PgHdr1 *p = pGroup->pLruTail;
40979 assert( p->pCache->pGroup==pGroup );
40980 assert( p->isPinned==0 );
40981 pcache1PinPage(p);
40982 pcache1RemoveFromHash(p, 1);
 
 
 
 
40983 }
40984 }
40985
40986 /*
40987 ** Discard all pages from cache pCache with a page number (key value)
@@ -41054,10 +41136,18 @@
41054 if( sqlite3GlobalConfig.bCoreMutex ){
41055 pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
41056 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
41057 }
41058 #endif
 
 
 
 
 
 
 
 
41059 pcache1.grp.mxPinned = 10;
41060 pcache1.isInit = 1;
41061 return SQLITE_OK;
41062 }
41063
@@ -41108,40 +41198,10 @@
41108 pCache->nMin = 10;
41109 pGroup->nMinPage += pCache->nMin;
41110 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
41111 }
41112 pcache1LeaveMutex(pGroup);
41113 /* Try to initialize the local bulk pagecache line allocation if using
41114 ** separate caches and if nPage!=0 */
41115 if( pcache1.separateCache
41116 && sqlite3GlobalConfig.nPage!=0
41117 && sqlite3GlobalConfig.pPage==0
41118 ){
41119 int szBulk;
41120 char *zBulk;
41121 sqlite3BeginBenignMalloc();
41122 if( sqlite3GlobalConfig.nPage>0 ){
41123 szBulk = pCache->szAlloc * sqlite3GlobalConfig.nPage;
41124 }else{
41125 szBulk = -1024*sqlite3GlobalConfig.nPage;
41126 }
41127 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
41128 sqlite3EndBenignMalloc();
41129 if( zBulk ){
41130 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
41131 int i;
41132 for(i=0; i<nBulk; i++){
41133 PgHdr1 *pX = (PgHdr1*)&zBulk[szPage];
41134 pX->page.pBuf = zBulk;
41135 pX->page.pExtra = &pX[1];
41136 pX->isBulkLocal = 1;
41137 pX->pNext = pCache->pFree;
41138 pCache->pFree = pX;
41139 zBulk += pCache->szAlloc;
41140 }
41141 }
41142 }
41143 if( pCache->nHash==0 ){
41144 pcache1Destroy((sqlite3_pcache*)pCache);
41145 pCache = 0;
41146 }
41147 }
@@ -41160,11 +41220,11 @@
41160 pcache1EnterMutex(pGroup);
41161 pGroup->nMaxPage += (nMax - pCache->nMax);
41162 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
41163 pCache->nMax = nMax;
41164 pCache->n90pct = pCache->nMax*9/10;
41165 pcache1EnforceMaxPage(pGroup);
41166 pcache1LeaveMutex(pGroup);
41167 }
41168 }
41169
41170 /*
@@ -41178,11 +41238,11 @@
41178 PGroup *pGroup = pCache->pGroup;
41179 int savedMaxPage;
41180 pcache1EnterMutex(pGroup);
41181 savedMaxPage = pGroup->nMaxPage;
41182 pGroup->nMaxPage = 0;
41183 pcache1EnforceMaxPage(pGroup);
41184 pGroup->nMaxPage = savedMaxPage;
41185 pcache1LeaveMutex(pGroup);
41186 }
41187 }
41188
@@ -41515,11 +41575,11 @@
41515 assert( pGroup->nMaxPage >= pCache->nMax );
41516 pGroup->nMaxPage -= pCache->nMax;
41517 assert( pGroup->nMinPage >= pCache->nMin );
41518 pGroup->nMinPage -= pCache->nMin;
41519 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
41520 pcache1EnforceMaxPage(pGroup);
41521 pcache1LeaveMutex(pGroup);
41522 sqlite3_free(pCache->pBulk);
41523 sqlite3_free(pCache->apHash);
41524 sqlite3_free(pCache);
41525 }
@@ -41679,10 +41739,11 @@
41679 ** primitives are constant time. The cost of DESTROY is O(N).
41680 **
41681 ** There is an added cost of O(N) when switching between TEST and
41682 ** SMALLEST primitives.
41683 */
 
41684
41685
41686 /*
41687 ** Target size for allocation chunks.
41688 */
@@ -42148,10 +42209,11 @@
42148 ** locking to prevent two processes from writing the same database
42149 ** file simultaneously, or one process from reading the database while
42150 ** another is writing.
42151 */
42152 #ifndef SQLITE_OMIT_DISKIO
 
42153 /************** Include wal.h in the middle of pager.c ***********************/
42154 /************** Begin file wal.h *********************************************/
42155 /*
42156 ** 2010 February 1
42157 **
@@ -42169,10 +42231,11 @@
42169 */
42170
42171 #ifndef _WAL_H_
42172 #define _WAL_H_
42173
 
42174
42175 /* Additional values that can be added to the sync_flags argument of
42176 ** sqlite3WalFrames():
42177 */
42178 #define WAL_SYNC_TRANSACTIONS 0x20 /* Sync at the end of each transaction */
@@ -49783,10 +49846,11 @@
49783 ** that correspond to frames greater than the new K value are removed
49784 ** from the hash table at this point.
49785 */
49786 #ifndef SQLITE_OMIT_WAL
49787
 
49788
49789 /*
49790 ** Trace output macros
49791 */
49792 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
@@ -50188,13 +50252,13 @@
50188
50189 assert( pWal->writeLock );
50190 pWal->hdr.isInit = 1;
50191 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
50192 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
50193 memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
50194 walShmBarrier(pWal);
50195 memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
50196 }
50197
50198 /*
50199 ** This function encodes a single frame header and writes it to a buffer
50200 ** supplied by the caller. A frame-header is made up of a series of
@@ -50492,17 +50556,17 @@
50492 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
50493 /* Verify that the every entry in the mapping region is still reachable
50494 ** via the hash table even after the cleanup.
50495 */
50496 if( iLimit ){
50497 int i; /* Loop counter */
50498 int iKey; /* Hash key */
50499 for(i=1; i<=iLimit; i++){
50500 for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
50501 if( aHash[iKey]==i ) break;
50502 }
50503 assert( aHash[iKey]==i );
50504 }
50505 }
50506 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
50507 }
50508
@@ -51000,11 +51064,11 @@
51000
51001 const int nList = *pnList; /* Size of input list */
51002 int nMerge = 0; /* Number of elements in list aMerge */
51003 ht_slot *aMerge = 0; /* List to be merged */
51004 int iList; /* Index into input list */
51005 int iSub = 0; /* Index into aSub array */
51006 struct Sublist aSub[13]; /* Array of sub-lists */
51007
51008 memset(aSub, 0, sizeof(aSub));
51009 assert( nList<=HASHTABLE_NPAGE && nList>0 );
51010 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
@@ -51011,11 +51075,13 @@
51011
51012 for(iList=0; iList<nList; iList++){
51013 nMerge = 1;
51014 aMerge = &aList[iList];
51015 for(iSub=0; iList & (1<<iSub); iSub++){
51016 struct Sublist *p = &aSub[iSub];
 
 
51017 assert( p->aList && p->nList<=(1<<iSub) );
51018 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
51019 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
51020 }
51021 aSub[iSub].aList = aMerge;
@@ -51022,11 +51088,13 @@
51022 aSub[iSub].nList = nMerge;
51023 }
51024
51025 for(iSub++; iSub<ArraySize(aSub); iSub++){
51026 if( nList & (1<<iSub) ){
51027 struct Sublist *p = &aSub[iSub];
 
 
51028 assert( p->nList<=(1<<iSub) );
51029 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
51030 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
51031 }
51032 }
@@ -52933,10 +53001,11 @@
52933 ** SIZE DESCRIPTION
52934 ** 4 Page number of next trunk page
52935 ** 4 Number of leaf pointers on this page
52936 ** * zero or more pages numbers of leaves
52937 */
 
52938
52939
52940 /* The following value is the maximum cell size assuming a maximum page
52941 ** size give above.
52942 */
@@ -53730,10 +53799,11 @@
53730 *************************************************************************
53731 ** This file implements an external (disk-based) database using BTrees.
53732 ** See the header comment on "btreeInt.h" for additional information.
53733 ** Including a description of file format and an overview of operation.
53734 */
 
53735
53736 /*
53737 ** The header string that appears at the beginning of every
53738 ** SQLite database.
53739 */
@@ -62675,11 +62745,11 @@
62675 u32 *heap = 0; /* Min-heap used for checking cell coverage */
62676 u32 x, prev = 0; /* Next and previous entry on the min-heap */
62677 const char *saved_zPfx = pCheck->zPfx;
62678 int saved_v1 = pCheck->v1;
62679 int saved_v2 = pCheck->v2;
62680 u8 savedIsInit;
62681
62682 /* Check that the page exists
62683 */
62684 pBt = pCheck->pBt;
62685 usableSize = pBt->usableSize;
@@ -63311,10 +63381,12 @@
63311 **
63312 *************************************************************************
63313 ** This file contains the implementation of the sqlite3_backup_XXX()
63314 ** API functions and the related features.
63315 */
 
 
63316
63317 /*
63318 ** Structure allocated for each backup operation.
63319 */
63320 struct sqlite3_backup {
@@ -64109,10 +64181,12 @@
64109 ** This file contains code use to manipulate "Mem" structure. A "Mem"
64110 ** stores a single value in the VDBE. Mem is an opaque structure visible
64111 ** only within the VDBE. Interface routines refer to a Mem using the
64112 ** name sqlite_value
64113 */
 
 
64114
64115 #ifdef SQLITE_DEBUG
64116 /*
64117 ** Check invariants on a Mem object.
64118 **
@@ -65826,10 +65900,12 @@
65826 **
65827 *************************************************************************
65828 ** This file contains code used for creating, destroying, and populating
65829 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
65830 */
 
 
65831
65832 /*
65833 ** Create a new virtual database engine.
65834 */
65835 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
@@ -70074,10 +70150,12 @@
70074 *************************************************************************
70075 **
70076 ** This file contains code use to implement APIs that are part of the
70077 ** VDBE.
70078 */
 
 
70079
70080 #ifndef SQLITE_OMIT_DEPRECATED
70081 /*
70082 ** Return TRUE (non-zero) of the statement supplied as an argument needs
70083 ** to be recompiled. A statement needs to be recompiled whenever the
@@ -70112,10 +70190,35 @@
70112 }else{
70113 return vdbeSafety(p);
70114 }
70115 }
70116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70117 /*
70118 ** The following routine destroys a virtual machine that is created by
70119 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
70120 ** success/failure code that describes the result of executing the virtual
70121 ** machine.
@@ -70132,10 +70235,11 @@
70132 }else{
70133 Vdbe *v = (Vdbe*)pStmt;
70134 sqlite3 *db = v->db;
70135 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
70136 sqlite3_mutex_enter(db->mutex);
 
70137 rc = sqlite3VdbeFinalize(v);
70138 rc = sqlite3ApiExit(db, rc);
70139 sqlite3LeaveMutexAndCloseZombie(db);
70140 }
70141 return rc;
@@ -70153,16 +70257,18 @@
70153 int rc;
70154 if( pStmt==0 ){
70155 rc = SQLITE_OK;
70156 }else{
70157 Vdbe *v = (Vdbe*)pStmt;
70158 sqlite3_mutex_enter(v->db->mutex);
 
 
70159 rc = sqlite3VdbeReset(v);
70160 sqlite3VdbeRewind(v);
70161 assert( (rc & (v->db->errMask))==rc );
70162 rc = sqlite3ApiExit(v->db, rc);
70163 sqlite3_mutex_leave(v->db->mutex);
70164 }
70165 return rc;
70166 }
70167
70168 /*
@@ -70508,10 +70614,11 @@
70508 }
70509 }
70510 #endif
70511 return rc;
70512 }
 
70513
70514 /*
70515 ** Execute the statement pStmt, either until a row of data is ready, the
70516 ** statement is completely executed or an error occurs.
70517 **
@@ -70577,12 +70684,14 @@
70577 assert( db->nVdbeWrite>0 || db->autoCommit==0
70578 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
70579 );
70580
70581 #ifndef SQLITE_OMIT_TRACE
70582 if( db->xProfile && !db->init.busy ){
70583 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
 
 
70584 }
70585 #endif
70586
70587 db->nVdbeActive++;
70588 if( p->readOnly==0 ) db->nVdbeWrite++;
@@ -70602,17 +70711,12 @@
70602 rc = sqlite3VdbeExec(p);
70603 db->nVdbeExec--;
70604 }
70605
70606 #ifndef SQLITE_OMIT_TRACE
70607 /* Invoke the profile callback if there is one
70608 */
70609 if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
70610 sqlite3_int64 iNow;
70611 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
70612 db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
70613 }
70614 #endif
70615
70616 if( rc==SQLITE_DONE ){
70617 assert( p->rc==SQLITE_OK );
70618 p->rc = doWalCallbacks(db);
@@ -71685,10 +71789,12 @@
71685 ** This file contains code used to insert the values of host parameters
71686 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
71687 **
71688 ** The Vdbe parse-tree explainer is also found here.
71689 */
 
 
71690
71691 #ifndef SQLITE_OMIT_TRACE
71692
71693 /*
71694 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
@@ -71877,10 +71983,12 @@
71877 ** documentation, headers files, or other derived files. The formatting
71878 ** of the code in this file is, therefore, important. See other comments
71879 ** in this file for details. If in doubt, do not deviate from existing
71880 ** commenting and indentation practices when changing or adding code.
71881 */
 
 
71882
71883 /*
71884 ** Invoke this macro on memory cells just prior to changing the
71885 ** value of the cell. This macro verifies that shallow copies are
71886 ** not misused. A shallow copy of a string or blob just copies a
@@ -78635,10 +78743,12 @@
78635 *************************************************************************
78636 **
78637 ** This file contains code used to implement incremental BLOB I/O.
78638 */
78639
 
 
78640
78641 #ifndef SQLITE_OMIT_INCRBLOB
78642
78643 /*
78644 ** Valid sqlite3_blob* handles point to Incrblob structures.
@@ -79233,10 +79343,12 @@
79233 ** than one background thread may be created. Specifically, there may be
79234 ** one background thread for each temporary file on disk, and one background
79235 ** thread to merge the output of each of the others to a single PMA for
79236 ** the main thread to read from.
79237 */
 
 
79238
79239 /*
79240 ** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
79241 ** messages to stderr that may be helpful in understanding the performance
79242 ** characteristics of the sorter in multi-threaded mode.
@@ -81849,10 +81961,11 @@
81849 ** 1) The in-memory representation grows too large for the allocated
81850 ** buffer, or
81851 ** 2) The sqlite3JournalCreate() function is called.
81852 */
81853 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 
81854
81855
81856 /*
81857 ** A JournalFile object is a subclass of sqlite3_file used by
81858 ** as an open file handle for journal files.
@@ -82096,10 +82209,11 @@
82096 **
82097 ** This file contains code use to implement an in-memory rollback journal.
82098 ** The in-memory rollback journal is used to journal transactions for
82099 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
82100 */
 
82101
82102 /* Forward references to internal structures */
82103 typedef struct MemJournal MemJournal;
82104 typedef struct FilePoint FilePoint;
82105 typedef struct FileChunk FileChunk;
@@ -82351,10 +82465,11 @@
82351 **
82352 *************************************************************************
82353 ** This file contains routines used for walking the parser tree for
82354 ** an SQL statement.
82355 */
 
82356 /* #include <stdlib.h> */
82357 /* #include <string.h> */
82358
82359
82360 /*
@@ -82509,10 +82624,11 @@
82509 **
82510 ** This file contains routines used for walking the parser tree and
82511 ** resolve all identifiers by associating them with a particular
82512 ** table and column.
82513 */
 
82514 /* #include <stdlib.h> */
82515 /* #include <string.h> */
82516
82517 /*
82518 ** Walk the expression tree pExpr and increase the aggregate function
@@ -84021,10 +84137,11 @@
84021 **
84022 *************************************************************************
84023 ** This file contains routines used for analyzing expressions and
84024 ** for generating VDBE code that evaluates expressions in SQLite.
84025 */
 
84026
84027 /*
84028 ** Return the 'affinity' of the expression pExpr if any.
84029 **
84030 ** If pExpr is a column, a reference to a column via an 'AS' alias,
@@ -88183,10 +88300,11 @@
88183 **
88184 *************************************************************************
88185 ** This file contains C code routines that used to generate VDBE code
88186 ** that implements the ALTER TABLE command.
88187 */
 
88188
88189 /*
88190 ** The code in this file only exists if we are not omitting the
88191 ** ALTER TABLE logic from the build.
88192 */
@@ -89145,10 +89263,11 @@
89145 ** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
89146 ** all contain just a single integer which is the same as the first
89147 ** integer in the equivalent columns in sqlite_stat4.
89148 */
89149 #ifndef SQLITE_OMIT_ANALYZE
 
89150
89151 #if defined(SQLITE_ENABLE_STAT4)
89152 # define IsStat4 1
89153 # define IsStat3 0
89154 #elif defined(SQLITE_ENABLE_STAT3)
@@ -90910,10 +91029,11 @@
90910 ** May you share freely, never taking more than you give.
90911 **
90912 *************************************************************************
90913 ** This file contains code used to implement the ATTACH and DETACH commands.
90914 */
 
90915
90916 #ifndef SQLITE_OMIT_ATTACH
90917 /*
90918 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
90919 ** is slightly different from resolving a normal SQL expression, because simple
@@ -91499,10 +91619,11 @@
91499 ** This file contains code used to implement the sqlite3_set_authorizer()
91500 ** API. This facility is an optional feature of the library. Embedded
91501 ** systems that do not need this facility may omit it by recompiling
91502 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
91503 */
 
91504
91505 /*
91506 ** All of the code in this file may be omitted by defining a single
91507 ** macro.
91508 */
@@ -91769,10 +91890,11 @@
91769 ** creating ID lists
91770 ** BEGIN TRANSACTION
91771 ** COMMIT
91772 ** ROLLBACK
91773 */
 
91774
91775 /*
91776 ** This routine is called when a new SQL statement is beginning to
91777 ** be parsed. Initialize the pParse structure as needed.
91778 */
@@ -96089,10 +96211,11 @@
96089 **
96090 ** This file contains functions used to access the internal hash tables
96091 ** of user defined functions and collation sequences.
96092 */
96093
 
96094
96095 /*
96096 ** Invoke the 'collation needed' callback to request a collation sequence
96097 ** in the encoding enc of name zName, length nName.
96098 */
@@ -96566,10 +96689,11 @@
96566 **
96567 *************************************************************************
96568 ** This file contains C code routines that are called by the parser
96569 ** in order to generate code for DELETE FROM statements.
96570 */
 
96571
96572 /*
96573 ** While a SrcList can in general represent multiple tables and subqueries
96574 ** (as in the FROM clause of a SELECT statement) in this case it contains
96575 ** the name of a single table, as one might find in an INSERT, DELETE,
@@ -97408,12 +97532,14 @@
97408 *************************************************************************
97409 ** This file contains the C-language implementations for many of the SQL
97410 ** functions of SQLite. (Some function, and in particular the date and
97411 ** time functions, are implemented separately.)
97412 */
 
97413 /* #include <stdlib.h> */
97414 /* #include <assert.h> */
 
97415
97416 /*
97417 ** Return the collating function associated with a function.
97418 */
97419 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
@@ -99205,10 +99331,11 @@
99205 **
99206 *************************************************************************
99207 ** This file contains code used by the compiler to add foreign key
99208 ** support to compiled SQL statements.
99209 */
 
99210
99211 #ifndef SQLITE_OMIT_FOREIGN_KEY
99212 #ifndef SQLITE_OMIT_TRIGGER
99213
99214 /*
@@ -100609,10 +100736,11 @@
100609 **
100610 *************************************************************************
100611 ** This file contains C code routines that are called by the parser
100612 ** to handle INSERT statements in SQLite.
100613 */
 
100614
100615 /*
100616 ** Generate code that will
100617 **
100618 ** (1) acquire a lock for table pTab then
@@ -102671,10 +102799,11 @@
102671 ** implement the programmer interface to the library. Routines in
102672 ** other files are for internal use by SQLite and should not be
102673 ** accessed by users of the library.
102674 */
102675
 
102676
102677 /*
102678 ** Execute SQL code. Return one of the SQLITE_ success/failure
102679 ** codes. Also write an error message into memory obtained from
102680 ** malloc() and make *pzErrMsg point to that message.
@@ -102839,10 +102968,11 @@
102839 ** as extensions by SQLite should #include this file instead of
102840 ** sqlite3.h.
102841 */
102842 #ifndef _SQLITE3EXT_H_
102843 #define _SQLITE3EXT_H_
 
102844
102845 typedef struct sqlite3_api_routines sqlite3_api_routines;
102846
102847 /*
102848 ** The following structure holds pointers to all of the SQLite API
@@ -103345,10 +103475,11 @@
103345
103346 #endif /* _SQLITE3EXT_H_ */
103347
103348 /************** End of sqlite3ext.h ******************************************/
103349 /************** Continuing where we left off in loadext.c ********************/
 
103350 /* #include <string.h> */
103351
103352 #ifndef SQLITE_OMIT_LOAD_EXTENSION
103353
103354 /*
@@ -104114,10 +104245,11 @@
104114 ** May you share freely, never taking more than you give.
104115 **
104116 *************************************************************************
104117 ** This file contains code used to implement the PRAGMA command.
104118 */
 
104119
104120 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
104121 # if defined(__APPLE__)
104122 # define SQLITE_ENABLE_LOCKING_STYLE 1
104123 # else
@@ -104220,11 +104352,11 @@
104220 /* ePragFlag: */ 0,
104221 /* iArg: */ 0 },
104222 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
104223 { /* zName: */ "cache_size",
104224 /* ePragTyp: */ PragTyp_CACHE_SIZE,
104225 /* ePragFlag: */ PragFlag_NeedSchema,
104226 /* iArg: */ 0 },
104227 #endif
104228 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104229 { /* zName: */ "cache_spill",
104230 /* ePragTyp: */ PragTyp_FLAG,
@@ -105290,10 +105422,11 @@
105290 ** of memory.
105291 */
105292 case PragTyp_CACHE_SIZE: {
105293 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
105294 if( !zRight ){
 
105295 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
105296 }else{
105297 int size = sqlite3Atoi(zRight);
105298 pDb->pSchema->cache_size = size;
105299 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
@@ -106565,10 +106698,11 @@
106565 *************************************************************************
106566 ** This file contains the implementation of the sqlite3_prepare()
106567 ** interface, and routines that contribute to loading the database schema
106568 ** from disk.
106569 */
 
106570
106571 /*
106572 ** Fill the InitData structure with an error message that indicates
106573 ** that the database is corrupt.
106574 */
@@ -107459,10 +107593,11 @@
107459 **
107460 *************************************************************************
107461 ** This file contains C code routines that are called by the parser
107462 ** to handle SELECT statements in SQLite.
107463 */
 
107464
107465 /*
107466 ** Trace output macros
107467 */
107468 #if SELECTTRACE_ENABLED
@@ -108528,11 +108663,10 @@
108528 }
108529 }
108530 return pInfo;
108531 }
108532
108533 #ifndef SQLITE_OMIT_COMPOUND_SELECT
108534 /*
108535 ** Name of the connection operator, used for error messages.
108536 */
108537 static const char *selectOpName(int id){
108538 char *z;
@@ -108542,11 +108676,10 @@
108542 case TK_EXCEPT: z = "EXCEPT"; break;
108543 default: z = "UNION"; break;
108544 }
108545 return z;
108546 }
108547 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
108548
108549 #ifndef SQLITE_OMIT_EXPLAIN
108550 /*
108551 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
108552 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
@@ -109545,23 +109678,10 @@
109545 Parse *pParse, /* Parsing context */
109546 Select *p, /* The right-most of SELECTs to be coded */
109547 SelectDest *pDest /* What to do with query results */
109548 );
109549
109550 /*
109551 ** Error message for when two or more terms of a compound select have different
109552 ** size result sets.
109553 */
109554 SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
109555 if( p->selFlags & SF_Values ){
109556 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
109557 }else{
109558 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
109559 " do not have the same number of result columns", selectOpName(p->op));
109560 }
109561 }
109562
109563 /*
109564 ** Handle the special case of a compound-select that originates from a
109565 ** VALUES clause. By handling this as a special case, we avoid deep
109566 ** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
109567 ** on a VALUES clause.
@@ -109983,10 +110103,23 @@
109983 pDest->nSdst = dest.nSdst;
109984 sqlite3SelectDelete(db, pDelete);
109985 return rc;
109986 }
109987 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
 
 
 
 
 
 
 
 
 
 
 
 
 
109988
109989 /*
109990 ** Code an output subroutine for a coroutine implementation of a
109991 ** SELECT statment.
109992 **
@@ -113077,10 +113210,11 @@
113077 ** interface routine of sqlite3_exec().
113078 **
113079 ** These routines are in a separate files so that they will not be linked
113080 ** if they are not used.
113081 */
 
113082 /* #include <stdlib.h> */
113083 /* #include <string.h> */
113084
113085 #ifndef SQLITE_OMIT_GET_TABLE
113086
@@ -113273,10 +113407,11 @@
113273 ** May you share freely, never taking more than you give.
113274 **
113275 *************************************************************************
113276 ** This file contains the implementation for TRIGGERs
113277 */
 
113278
113279 #ifndef SQLITE_OMIT_TRIGGER
113280 /*
113281 ** Delete a linked list of TriggerStep structures.
113282 */
@@ -114396,10 +114531,11 @@
114396 **
114397 *************************************************************************
114398 ** This file contains C code routines that are called by the parser
114399 ** to handle UPDATE statements.
114400 */
 
114401
114402 #ifndef SQLITE_OMIT_VIRTUALTABLE
114403 /* Forward declaration */
114404 static void updateVirtualTable(
114405 Parse *pParse, /* The parsing context */
@@ -115172,10 +115308,12 @@
115172 ** This file contains code used to implement the VACUUM command.
115173 **
115174 ** Most of the code in this file may be omitted by defining the
115175 ** SQLITE_OMIT_VACUUM macro.
115176 */
 
 
115177
115178 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
115179 /*
115180 ** Finalize a prepared statement. If there was an error, store the
115181 ** text of the error message in *pzErrMsg. Return the result code.
@@ -115544,10 +115682,11 @@
115544 **
115545 *************************************************************************
115546 ** This file contains code used to help implement virtual tables.
115547 */
115548 #ifndef SQLITE_OMIT_VIRTUALTABLE
 
115549
115550 /*
115551 ** Before a virtual table xCreate() or xConnect() method is invoked, the
115552 ** sqlite3.pVtabCtx member variable is set to point to an instance of
115553 ** this struct allocated on the stack. It is used by the implementation of
@@ -116702,10 +116841,11 @@
116702 ** This file was split off from where.c on 2015-06-06 in order to reduce the
116703 ** size of where.c and make it easier to edit. This file contains the routines
116704 ** that actually generate the bulk of the WHERE loop code. The original where.c
116705 ** file retains the code that does query planning and analysis.
116706 */
 
116707 /************** Include whereInt.h in the middle of wherecode.c **************/
116708 /************** Begin file whereInt.h ****************************************/
116709 /*
116710 ** 2013-11-12
116711 **
@@ -118513,11 +118653,15 @@
118513
118514 /* Read the PK into an array of temp registers. */
118515 r = sqlite3GetTempRange(pParse, nPk);
118516 for(iPk=0; iPk<nPk; iPk++){
118517 int iCol = pPk->aiColumn[iPk];
118518 sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
 
 
 
 
118519 }
118520
118521 /* Check if the temp table already contains this key. If so,
118522 ** the row has already been included in the result set and
118523 ** can be ignored (by jumping past the Gosub below). Otherwise,
@@ -118737,10 +118881,12 @@
118737 **
118738 ** This file was originally part of where.c but was split out to improve
118739 ** readability and editabiliity. This file contains utility routines for
118740 ** analyzing Expr objects in the WHERE clause.
118741 */
 
 
118742
118743 /* Forward declarations */
118744 static void exprAnalyze(SrcList*, WhereClause*, int);
118745
118746 /*
@@ -119987,10 +120133,12 @@
119987 ** generating the code that loops through a table looking for applicable
119988 ** rows. Indices are selected and used to speed the search when doing
119989 ** so is applicable. Because this module is responsible for selecting
119990 ** indices, you might also think of this module as the "query optimizer".
119991 */
 
 
119992
119993 /* Forward declaration of methods */
119994 static int whereLoopResize(sqlite3*, WhereLoop*, int);
119995
119996 /* Test variable that can be set to enable WHERE tracing */
@@ -124486,10 +124634,11 @@
124486 */
124487 /* First off, code is included that follows the "include" declaration
124488 ** in the input grammar file. */
124489 /* #include <stdio.h> */
124490
 
124491
124492 /*
124493 ** Disable all error recovery processing in the parser push-down
124494 ** automaton.
124495 */
@@ -128071,10 +128220,11 @@
128071 **
128072 ** This file contains C code that splits an SQL input string up into
128073 ** individual tokens and sends those tokens one-by-one over to the
128074 ** parser for analysis.
128075 */
 
128076 /* #include <stdlib.h> */
128077
128078 /*
128079 ** The charMap() macro maps alphabetic characters into their
128080 ** lower-case ASCII equivalent. On ASCII machines, this is just
@@ -128880,10 +129030,11 @@
128880 ** This file contains C code that implements the sqlite3_complete() API.
128881 ** This code used to be part of the tokenizer.c source file. But by
128882 ** separating it out, the code will be automatically omitted from
128883 ** static links that do not use it.
128884 */
 
128885 #ifndef SQLITE_OMIT_COMPLETE
128886
128887 /*
128888 ** This is defined in tokenize.c. We just have to import the definition.
128889 */
@@ -129170,10 +129321,11 @@
129170 ** Main file for the SQLite library. The routines in this file
129171 ** implement the programmer interface to the library. Routines in
129172 ** other files are for internal use by SQLite and should not be
129173 ** accessed by users of the library.
129174 */
 
129175
129176 #ifdef SQLITE_ENABLE_FTS3
129177 /************** Include fts3.h in the middle of main.c ***********************/
129178 /************** Begin file fts3.h ********************************************/
129179 /*
@@ -129189,10 +129341,11 @@
129189 ******************************************************************************
129190 **
129191 ** This header file is used by programs that want to link against the
129192 ** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
129193 */
 
129194
129195 #if 0
129196 extern "C" {
129197 #endif /* __cplusplus */
129198
@@ -129221,10 +129374,11 @@
129221 ******************************************************************************
129222 **
129223 ** This header file is used by programs that want to link against the
129224 ** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
129225 */
 
129226
129227 #if 0
129228 extern "C" {
129229 #endif /* __cplusplus */
129230
@@ -129253,10 +129407,11 @@
129253 ******************************************************************************
129254 **
129255 ** This header file is used by programs that want to link against the
129256 ** ICU extension. All it does is declare the sqlite3IcuInit() interface.
129257 */
 
129258
129259 #if 0
129260 extern "C" {
129261 #endif /* __cplusplus */
129262
@@ -129885,10 +130040,11 @@
129885 ** space for the lookaside memory is obtained from sqlite3_malloc().
129886 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
129887 ** the lookaside memory.
129888 */
129889 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
 
129890 void *pStart;
129891 if( db->lookaside.nOut ){
129892 return SQLITE_BUSY;
129893 }
129894 /* Free any existing lookaside buffer for this handle before
@@ -129935,10 +130091,11 @@
129935 db->lookaside.pStart = db;
129936 db->lookaside.pEnd = db;
129937 db->lookaside.bEnabled = 0;
129938 db->lookaside.bMalloced = 0;
129939 }
 
129940 return SQLITE_OK;
129941 }
129942
129943 /*
129944 ** Return the mutex associated with a database connection.
@@ -133045,10 +133202,12 @@
133045 *************************************************************************
133046 **
133047 ** This file contains the implementation of the sqlite3_unlock_notify()
133048 ** API method and its associated functionality.
133049 */
 
 
133050
133051 /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
133052 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
133053
133054 /*
@@ -133688,13 +133847,15 @@
133688
133689 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
133690
133691 /* If not building as part of the core, include sqlite3ext.h. */
133692 #ifndef SQLITE_CORE
 
133693 SQLITE_EXTENSION_INIT3
133694 #endif
133695
 
133696 /************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
133697 /************** Begin file fts3_tokenizer.h **********************************/
133698 /*
133699 ** 2006 July 10
133700 **
@@ -133719,10 +133880,11 @@
133719
133720 /* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
133721 ** If tokenizers are to be allowed to call sqlite3_*() functions, then
133722 ** we will need a way to register the API consistently.
133723 */
 
133724
133725 /*
133726 ** Structures used by the tokenizer interface. When a new tokenizer
133727 ** implementation is registered, the caller provides a pointer to
133728 ** an sqlite3_tokenizer_module containing pointers to the callback
@@ -134559,11 +134721,13 @@
134559 /* #include <stddef.h> */
134560 /* #include <stdio.h> */
134561 /* #include <string.h> */
134562 /* #include <stdarg.h> */
134563
 
134564 #ifndef SQLITE_CORE
 
134565 SQLITE_EXTENSION_INIT1
134566 #endif
134567
134568 static int fts3EvalNext(Fts3Cursor *pCsr);
134569 static int fts3EvalStart(Fts3Cursor *pCsr);
@@ -138606,10 +138770,11 @@
138606 if( p==0 ){
138607 p = aDoclist;
138608 p += sqlite3Fts3GetVarint(p, piDocid);
138609 }else{
138610 fts3PoslistCopy(0, &p);
 
138611 if( p>=&aDoclist[nDoclist] ){
138612 *pbEof = 1;
138613 }else{
138614 sqlite3_int64 iVar;
138615 p += sqlite3Fts3GetVarint(p, &iVar);
@@ -140012,14 +140177,14 @@
140012 pIter = pPhrase->doclist.pList;
140013 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
140014 int rc = SQLITE_OK;
140015 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
140016 int bOr = 0;
140017 u8 bEof = 0;
140018 u8 bTreeEof = 0;
140019 Fts3Expr *p; /* Used to iterate from pExpr to root */
140020 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
 
140021
140022 /* Check if this phrase descends from an OR expression node. If not,
140023 ** return NULL. Otherwise, the entry that corresponds to docid
140024 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
140025 ** tree that the node is part of has been marked as EOF, but the node
@@ -140049,35 +140214,51 @@
140049 fts3EvalNextRow(pCsr, pNear, &rc);
140050 }
140051 }
140052 if( rc!=SQLITE_OK ) return rc;
140053
140054 pIter = pPhrase->pOrPoslist;
140055 iDocid = pPhrase->iOrDocid;
140056 if( pCsr->bDesc==bDescDoclist ){
140057 bEof = !pPhrase->doclist.nAll ||
140058 (pIter >= (pPhrase->doclist.aAll + pPhrase->doclist.nAll));
140059 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
140060 sqlite3Fts3DoclistNext(
140061 bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll,
140062 &pIter, &iDocid, &bEof
140063 );
140064 }
140065 }else{
140066 bEof = !pPhrase->doclist.nAll || (pIter && pIter<=pPhrase->doclist.aAll);
140067 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
140068 int dummy;
140069 sqlite3Fts3DoclistPrev(
140070 bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll,
140071 &pIter, &iDocid, &dummy, &bEof
140072 );
140073 }
140074 }
140075 pPhrase->pOrPoslist = pIter;
140076 pPhrase->iOrDocid = iDocid;
140077
140078 if( bEof || iDocid!=pCsr->iPrevId ) pIter = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140079 }
140080 if( pIter==0 ) return SQLITE_OK;
140081
140082 if( *pIter==0x01 ){
140083 pIter++;
@@ -140161,10 +140342,11 @@
140161 ** May you share freely, never taking more than you give.
140162 **
140163 ******************************************************************************
140164 **
140165 */
 
140166 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
140167
140168 /* #include <string.h> */
140169 /* #include <assert.h> */
140170
@@ -140717,10 +140899,11 @@
140717 ** This module contains code that implements a parser for fts3 query strings
140718 ** (the right-hand argument to the MATCH operator). Because the supported
140719 ** syntax is relatively simple, the whole tokenizer/parser system is
140720 ** hand-coded.
140721 */
 
140722 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
140723
140724 /*
140725 ** By default, this module parses the legacy syntax that has been
140726 ** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
@@ -142010,16 +142193,18 @@
142010 ** (in which case SQLITE_CORE is not defined), or
142011 **
142012 ** * The FTS3 module is being built into the core of
142013 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
142014 */
 
142015 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
142016
142017 /* #include <assert.h> */
142018 /* #include <stdlib.h> */
142019 /* #include <string.h> */
142020
 
142021
142022 /*
142023 ** Malloc and Free functions
142024 */
142025 static void *fts3HashMalloc(int n){
@@ -142393,17 +142578,19 @@
142393 ** (in which case SQLITE_CORE is not defined), or
142394 **
142395 ** * The FTS3 module is being built into the core of
142396 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
142397 */
 
142398 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
142399
142400 /* #include <assert.h> */
142401 /* #include <stdlib.h> */
142402 /* #include <stdio.h> */
142403 /* #include <string.h> */
142404
 
142405
142406 /*
142407 ** Class derived from sqlite3_tokenizer
142408 */
142409 typedef struct porter_tokenizer {
@@ -143057,10 +143244,11 @@
143057 ** (in which case SQLITE_CORE is not defined), or
143058 **
143059 ** * The FTS3 module is being built into the core of
143060 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
143061 */
 
143062 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
143063
143064 /* #include <assert.h> */
143065 /* #include <string.h> */
143066
@@ -143552,17 +143740,19 @@
143552 ** (in which case SQLITE_CORE is not defined), or
143553 **
143554 ** * The FTS3 module is being built into the core of
143555 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
143556 */
 
143557 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
143558
143559 /* #include <assert.h> */
143560 /* #include <stdlib.h> */
143561 /* #include <stdio.h> */
143562 /* #include <string.h> */
143563
 
143564
143565 typedef struct simple_tokenizer {
143566 sqlite3_tokenizer base;
143567 char delim[128]; /* flag ASCII delimiters */
143568 } simple_tokenizer;
@@ -143803,10 +143993,11 @@
143803 ** end: Byte offset of the byte immediately following the end of the
143804 ** token within the input string.
143805 ** pos: Token offset of token within input.
143806 **
143807 */
 
143808 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
143809
143810 /* #include <string.h> */
143811 /* #include <assert.h> */
143812
@@ -144238,10 +144429,11 @@
144238 ** tables. It also contains code to merge FTS3 b-tree segments. Some
144239 ** of the sub-routines used to merge segments are also used by the query
144240 ** code in fts3.c.
144241 */
144242
 
144243 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
144244
144245 /* #include <string.h> */
144246 /* #include <assert.h> */
144247 /* #include <stdlib.h> */
@@ -149898,10 +150090,11 @@
149898 ** May you share freely, never taking more than you give.
149899 **
149900 ******************************************************************************
149901 */
149902
 
149903 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
149904
149905 /* #include <string.h> */
149906 /* #include <assert.h> */
149907
@@ -151610,17 +151803,19 @@
151610 ** Implementation of the "unicode" full-text-search tokenizer.
151611 */
151612
151613 #ifndef SQLITE_DISABLE_FTS3_UNICODE
151614
 
151615 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
151616
151617 /* #include <assert.h> */
151618 /* #include <stdlib.h> */
151619 /* #include <stdio.h> */
151620 /* #include <string.h> */
151621
 
151622
151623 /*
151624 ** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
151625 ** from the sqlite3 source file utf.c. If this file is compiled as part
151626 ** of the amalgamation, they are not required.
@@ -152412,12 +152607,14 @@
152412 */
152413
152414 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
152415
152416 #ifndef SQLITE_CORE
 
152417 SQLITE_EXTENSION_INIT1
152418 #else
 
152419 #endif
152420
152421 /* #include <string.h> */
152422 /* #include <assert.h> */
152423 /* #include <stdio.h> */
@@ -155909,12 +156106,14 @@
155909 #include <unicode/ucol.h>
155910
155911 /* #include <assert.h> */
155912
155913 #ifndef SQLITE_CORE
 
155914 SQLITE_EXTENSION_INIT1
155915 #else
 
155916 #endif
155917
155918 /*
155919 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
155920 ** operator.
@@ -156386,15 +156585,17 @@
156386 ** May you share freely, never taking more than you give.
156387 **
156388 *************************************************************************
156389 ** This file implements a tokenizer for fts3 based on the ICU library.
156390 */
 
156391 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
156392 #ifdef SQLITE_ENABLE_ICU
156393
156394 /* #include <assert.h> */
156395 /* #include <string.h> */
 
156396
156397 #include <unicode/ubrk.h>
156398 /* #include <unicode/ucol.h> */
156399 /* #include <unicode/ustring.h> */
156400 #include <unicode/utf16.h>
@@ -156635,11 +156836,11 @@
156635
156636 #endif /* defined(SQLITE_ENABLE_ICU) */
156637 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
156638
156639 /************** End of fts3_icu.c ********************************************/
156640 /************** Begin file sqlite3ota.c **************************************/
156641 /*
156642 ** 2014 August 30
156643 **
156644 ** The author disclaims copyright to this source code. In place of
156645 ** a legal notice, here is a blessing:
@@ -156651,15 +156852,15 @@
156651 *************************************************************************
156652 **
156653 **
156654 ** OVERVIEW
156655 **
156656 ** The OTA extension requires that the OTA update be packaged as an
156657 ** SQLite database. The tables it expects to find are described in
156658 ** sqlite3ota.h. Essentially, for each table xyz in the target database
156659 ** that the user wishes to write to, a corresponding data_xyz table is
156660 ** created in the OTA database and populated with one row for each row to
156661 ** update, insert or delete from the target table.
156662 **
156663 ** The update proceeds in three stages:
156664 **
156665 ** 1) The database is updated. The modified database pages are written
@@ -156667,70 +156868,71 @@
156667 ** that it is named "<database>-oal" instead of "<database>-wal".
156668 ** Because regular SQLite clients do not look for file named
156669 ** "<database>-oal", they go on using the original database in
156670 ** rollback mode while the *-oal file is being generated.
156671 **
156672 ** During this stage OTA does not update the database by writing
156673 ** directly to the target tables. Instead it creates "imposter"
156674 ** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
156675 ** to update each b-tree individually. All updates required by each
156676 ** b-tree are completed before moving on to the next, and all
156677 ** updates are done in sorted key order.
156678 **
156679 ** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
156680 ** location using a call to rename(2). Before doing this the OTA
156681 ** module takes an EXCLUSIVE lock on the database file, ensuring
156682 ** that there are no other active readers.
156683 **
156684 ** Once the EXCLUSIVE lock is released, any other database readers
156685 ** detect the new *-wal file and read the database in wal mode. At
156686 ** this point they see the new version of the database - including
156687 ** the updates made as part of the OTA update.
156688 **
156689 ** 3) The new *-wal file is checkpointed. This proceeds in the same way
156690 ** as a regular database checkpoint, except that a single frame is
156691 ** checkpointed each time sqlite3ota_step() is called. If the OTA
156692 ** handle is closed before the entire *-wal file is checkpointed,
156693 ** the checkpoint progress is saved in the OTA database and the
156694 ** checkpoint can be resumed by another OTA client at some point in
156695 ** the future.
156696 **
156697 ** POTENTIAL PROBLEMS
156698 **
156699 ** The rename() call might not be portable. And OTA is not currently
156700 ** syncing the directory after renaming the file.
156701 **
156702 ** When state is saved, any commit to the *-oal file and the commit to
156703 ** the OTA update database are not atomic. So if the power fails at the
156704 ** wrong moment they might get out of sync. As the main database will be
156705 ** committed before the OTA update database this will likely either just
156706 ** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
156707 ** constraint violations).
156708 **
156709 ** If some client does modify the target database mid OTA update, or some
156710 ** other error occurs, the OTA extension will keep throwing errors. It's
156711 ** not really clear how to get out of this state. The system could just
156712 ** by delete the OTA update database and *-oal file and have the device
156713 ** download the update again and start over.
156714 **
156715 ** At present, for an UPDATE, both the new.* and old.* records are
156716 ** collected in the ota_xyz table. And for both UPDATEs and DELETEs all
156717 ** fields are collected. This means we're probably writing a lot more
156718 ** data to disk when saving the state of an ongoing update to the OTA
156719 ** update database than is strictly necessary.
156720 **
156721 */
156722
156723 /* #include <assert.h> */
156724 /* #include <string.h> */
156725 /* #include <stdio.h> */
156726 /* #include <unistd.h> */
156727
 
156728
156729 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA)
156730 /************** Include sqlite3ota.h in the middle of sqlite3ota.c ***********/
156731 /************** Begin file sqlite3ota.h **************************************/
156732 /*
156733 ** 2014 August 30
156734 **
156735 ** The author disclaims copyright to this source code. In place of
156736 ** a legal notice, here is a blessing:
@@ -156739,11 +156941,11 @@
156739 ** May you find forgiveness for yourself and forgive others.
156740 ** May you share freely, never taking more than you give.
156741 **
156742 *************************************************************************
156743 **
156744 ** This file contains the public interface for the OTA extension.
156745 */
156746
156747 /*
156748 ** SUMMARY
156749 **
@@ -156776,18 +156978,18 @@
156776 ** mobile device that is frequently rebooted. Even after the writer process
156777 ** has committed one or more sub-transactions, other database clients continue
156778 ** to read from the original database snapshot. In other words, partially
156779 ** applied transactions are not visible to other clients.
156780 **
156781 ** "OTA" stands for "Over The Air" update. As in a large database update
156782 ** transmitted via a wireless network to a mobile device. A transaction
156783 ** applied using this extension is hence refered to as an "OTA update".
156784 **
156785 **
156786 ** LIMITATIONS
156787 **
156788 ** An "OTA update" transaction is subject to the following limitations:
156789 **
156790 ** * The transaction must consist of INSERT, UPDATE and DELETE operations
156791 ** only.
156792 **
156793 ** * INSERT statements may not use any default values.
@@ -156808,430 +157010,430 @@
156808 ** * No constraint handling mode except for "OR ROLLBACK" is supported.
156809 **
156810 **
156811 ** PREPARATION
156812 **
156813 ** An "OTA update" is stored as a separate SQLite database. A database
156814 ** containing an OTA update is an "OTA database". For each table in the
156815 ** target database to be updated, the OTA database should contain a table
156816 ** named "data_<target name>" containing the same set of columns as the
156817 ** target table, and one more - "ota_control". The data_% table should
156818 ** have no PRIMARY KEY or UNIQUE constraints, but each column should have
156819 ** the same type as the corresponding column in the target database.
156820 ** The "ota_control" column should have no type at all. For example, if
156821 ** the target database contains:
156822 **
156823 ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
156824 **
156825 ** Then the OTA database should contain:
156826 **
156827 ** CREATE TABLE data_t1(a INTEGER, b TEXT, c, ota_control);
156828 **
156829 ** The order of the columns in the data_% table does not matter.
156830 **
156831 ** If the target database table is a virtual table or a table that has no
156832 ** PRIMARY KEY declaration, the data_% table must also contain a column
156833 ** named "ota_rowid". This column is mapped to the tables implicit primary
156834 ** key column - "rowid". Virtual tables for which the "rowid" column does
156835 ** not function like a primary key value cannot be updated using OTA. For
156836 ** example, if the target db contains either of the following:
156837 **
156838 ** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
156839 ** CREATE TABLE x1(a, b)
156840 **
156841 ** then the OTA database should contain:
156842 **
156843 ** CREATE TABLE data_x1(a, b, ota_rowid, ota_control);
156844 **
156845 ** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
156846 ** target table must be present in the input table. For virtual tables,
156847 ** hidden columns are optional - they are updated by OTA if present in
156848 ** the input table, or not otherwise. For example, to write to an fts4
156849 ** table with a hidden languageid column such as:
156850 **
156851 ** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
156852 **
156853 ** Either of the following input table schemas may be used:
156854 **
156855 ** CREATE TABLE data_ft1(a, b, langid, ota_rowid, ota_control);
156856 ** CREATE TABLE data_ft1(a, b, ota_rowid, ota_control);
156857 **
156858 ** For each row to INSERT into the target database as part of the OTA
156859 ** update, the corresponding data_% table should contain a single record
156860 ** with the "ota_control" column set to contain integer value 0. The
156861 ** other columns should be set to the values that make up the new record
156862 ** to insert.
156863 **
156864 ** If the target database table has an INTEGER PRIMARY KEY, it is not
156865 ** possible to insert a NULL value into the IPK column. Attempting to
156866 ** do so results in an SQLITE_MISMATCH error.
156867 **
156868 ** For each row to DELETE from the target database as part of the OTA
156869 ** update, the corresponding data_% table should contain a single record
156870 ** with the "ota_control" column set to contain integer value 1. The
156871 ** real primary key values of the row to delete should be stored in the
156872 ** corresponding columns of the data_% table. The values stored in the
156873 ** other columns are not used.
156874 **
156875 ** For each row to UPDATE from the target database as part of the OTA
156876 ** update, the corresponding data_% table should contain a single record
156877 ** with the "ota_control" column set to contain a value of type text.
156878 ** The real primary key values identifying the row to update should be
156879 ** stored in the corresponding columns of the data_% table row, as should
156880 ** the new values of all columns being update. The text value in the
156881 ** "ota_control" column must contain the same number of characters as
156882 ** there are columns in the target database table, and must consist entirely
156883 ** of 'x' and '.' characters (or in some special cases 'd' - see below). For
156884 ** each column that is being updated, the corresponding character is set to
156885 ** 'x'. For those that remain as they are, the corresponding character of the
156886 ** ota_control value should be set to '.'. For example, given the tables
156887 ** above, the update statement:
156888 **
156889 ** UPDATE t1 SET c = 'usa' WHERE a = 4;
156890 **
156891 ** is represented by the data_t1 row created by:
156892 **
156893 ** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..x');
156894 **
156895 ** Instead of an 'x' character, characters of the ota_control value specified
156896 ** for UPDATEs may also be set to 'd'. In this case, instead of updating the
156897 ** target table with the value stored in the corresponding data_% column, the
156898 ** user-defined SQL function "ota_delta()" is invoked and the result stored in
156899 ** the target table column. ota_delta() is invoked with two arguments - the
156900 ** original value currently stored in the target table column and the
156901 ** value specified in the data_xxx table.
156902 **
156903 ** For example, this row:
156904 **
156905 ** INSERT INTO data_t1(a, b, c, ota_control) VALUES(4, NULL, 'usa', '..d');
156906 **
156907 ** is similar to an UPDATE statement such as:
156908 **
156909 ** UPDATE t1 SET c = ota_delta(c, 'usa') WHERE a = 4;
156910 **
156911 ** If the target database table is a virtual table or a table with no PRIMARY
156912 ** KEY, the ota_control value should not include a character corresponding
156913 ** to the ota_rowid value. For example, this:
156914 **
156915 ** INSERT INTO data_ft1(a, b, ota_rowid, ota_control)
156916 ** VALUES(NULL, 'usa', 12, '.x');
156917 **
156918 ** causes a result similar to:
156919 **
156920 ** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
156921 **
156922 ** The data_xxx tables themselves should have no PRIMARY KEY declarations.
156923 ** However, OTA is more efficient if reading the rows in from each data_xxx
156924 ** table in "rowid" order is roughly the same as reading them sorted by
156925 ** the PRIMARY KEY of the corresponding target database table. In other
156926 ** words, rows should be sorted using the destination table PRIMARY KEY
156927 ** fields before they are inserted into the data_xxx tables.
156928 **
156929 ** USAGE
156930 **
156931 ** The API declared below allows an application to apply an OTA update
156932 ** stored on disk to an existing target database. Essentially, the
156933 ** application:
156934 **
156935 ** 1) Opens an OTA handle using the sqlite3ota_open() function.
156936 **
156937 ** 2) Registers any required virtual table modules with the database
156938 ** handle returned by sqlite3ota_db(). Also, if required, register
156939 ** the ota_delta() implementation.
156940 **
156941 ** 3) Calls the sqlite3ota_step() function one or more times on
156942 ** the new handle. Each call to sqlite3ota_step() performs a single
156943 ** b-tree operation, so thousands of calls may be required to apply
156944 ** a complete update.
156945 **
156946 ** 4) Calls sqlite3ota_close() to close the OTA update handle. If
156947 ** sqlite3ota_step() has been called enough times to completely
156948 ** apply the update to the target database, then the OTA database
156949 ** is marked as fully applied. Otherwise, the state of the OTA
156950 ** update application is saved in the OTA database for later
156951 ** resumption.
156952 **
156953 ** See comments below for more detail on APIs.
156954 **
156955 ** If an update is only partially applied to the target database by the
156956 ** time sqlite3ota_close() is called, various state information is saved
156957 ** within the OTA database. This allows subsequent processes to automatically
156958 ** resume the OTA update from where it left off.
156959 **
156960 ** To remove all OTA extension state information, returning an OTA database
156961 ** to its original contents, it is sufficient to drop all tables that begin
156962 ** with the prefix "ota_"
156963 **
156964 ** DATABASE LOCKING
156965 **
156966 ** An OTA update may not be applied to a database in WAL mode. Attempting
156967 ** to do so is an error (SQLITE_ERROR).
156968 **
156969 ** While an OTA handle is open, a SHARED lock may be held on the target
156970 ** database file. This means it is possible for other clients to read the
156971 ** database, but not to write it.
156972 **
156973 ** If an OTA update is started and then suspended before it is completed,
156974 ** then an external client writes to the database, then attempting to resume
156975 ** the suspended OTA update is also an error (SQLITE_BUSY).
156976 */
156977
156978 #ifndef _SQLITE3OTA_H
156979 #define _SQLITE3OTA_H
156980
 
156981
156982 typedef struct sqlite3ota sqlite3ota;
156983
156984 /*
156985 ** Open an OTA handle.
156986 **
156987 ** Argument zTarget is the path to the target database. Argument zOta is
156988 ** the path to the OTA database. Each call to this function must be matched
156989 ** by a call to sqlite3ota_close(). When opening the databases, OTA passes
156990 ** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
156991 ** or zOta begin with "file:", it will be interpreted as an SQLite
156992 ** database URI, not a regular file name.
156993 **
156994 ** If the zState argument is passed a NULL value, the OTA extension stores
156995 ** the current state of the update (how many rows have been updated, which
156996 ** indexes are yet to be updated etc.) within the OTA database itself. This
156997 ** can be convenient, as it means that the OTA application does not need to
156998 ** organize removing a separate state file after the update is concluded.
156999 ** Or, if zState is non-NULL, it must be a path to a database file in which
157000 ** the OTA extension can store the state of the update.
157001 **
157002 ** When resuming an OTA update, the zState argument must be passed the same
157003 ** value as when the OTA update was started.
157004 **
157005 ** Once the OTA update is finished, the OTA extension does not
157006 ** automatically remove any zState database file, even if it created it.
157007 **
157008 ** By default, OTA uses the default VFS to access the files on disk. To
157009 ** use a VFS other than the default, an SQLite "file:" URI containing a
157010 ** "vfs=..." option may be passed as the zTarget option.
157011 **
157012 ** IMPORTANT NOTE FOR ZIPVFS USERS: The OTA extension works with all of
157013 ** SQLite's built-in VFSs, including the multiplexor VFS. However it does
157014 ** not work out of the box with zipvfs. Refer to the comment describing
157015 ** the zipvfs_create_vfs() API below for details on using OTA with zipvfs.
157016 */
157017 SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
157018 const char *zTarget,
157019 const char *zOta,
157020 const char *zState
157021 );
157022
157023 /*
157024 ** Internally, each OTA connection uses a separate SQLite database
157025 ** connection to access the target and ota update databases. This
157026 ** API allows the application direct access to these database handles.
157027 **
157028 ** The first argument passed to this function must be a valid, open, OTA
157029 ** handle. The second argument should be passed zero to access the target
157030 ** database handle, or non-zero to access the ota update database handle.
157031 ** Accessing the underlying database handles may be useful in the
157032 ** following scenarios:
157033 **
157034 ** * If any target tables are virtual tables, it may be necessary to
157035 ** call sqlite3_create_module() on the target database handle to
157036 ** register the required virtual table implementations.
157037 **
157038 ** * If the data_xxx tables in the OTA source database are virtual
157039 ** tables, the application may need to call sqlite3_create_module() on
157040 ** the ota update db handle to any required virtual table
157041 ** implementations.
157042 **
157043 ** * If the application uses the "ota_delta()" feature described above,
157044 ** it must use sqlite3_create_function() or similar to register the
157045 ** ota_delta() implementation with the target database handle.
157046 **
157047 ** If an error has occurred, either while opening or stepping the OTA object,
157048 ** this function may return NULL. The error code and message may be collected
157049 ** when sqlite3ota_close() is called.
157050 */
157051 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota*, int bOta);
157052
157053 /*
157054 ** Do some work towards applying the OTA update to the target db.
157055 **
157056 ** Return SQLITE_DONE if the update has been completely applied, or
157057 ** SQLITE_OK if no error occurs but there remains work to do to apply
157058 ** the OTA update. If an error does occur, some other error code is
157059 ** returned.
157060 **
157061 ** Once a call to sqlite3ota_step() has returned a value other than
157062 ** SQLITE_OK, all subsequent calls on the same OTA handle are no-ops
157063 ** that immediately return the same value.
157064 */
157065 SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *pOta);
157066
157067 /*
157068 ** Close an OTA handle.
157069 **
157070 ** If the OTA update has been completely applied, mark the OTA database
157071 ** as fully applied. Otherwise, assuming no error has occurred, save the
157072 ** current state of the OTA update appliation to the OTA database.
157073 **
157074 ** If an error has already occurred as part of an sqlite3ota_step()
157075 ** or sqlite3ota_open() call, or if one occurs within this function, an
157076 ** SQLite error code is returned. Additionally, *pzErrmsg may be set to
157077 ** point to a buffer containing a utf-8 formatted English language error
157078 ** message. It is the responsibility of the caller to eventually free any
157079 ** such buffer using sqlite3_free().
157080 **
157081 ** Otherwise, if no error occurs, this function returns SQLITE_OK if the
157082 ** update has been partially applied, or SQLITE_DONE if it has been
157083 ** completely applied.
157084 */
157085 SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *pOta, char **pzErrmsg);
157086
157087 /*
157088 ** Return the total number of key-value operations (inserts, deletes or
157089 ** updates) that have been performed on the target database since the
157090 ** current OTA update was started.
157091 */
157092 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta);
157093
157094 /*
157095 ** Create an OTA VFS named zName that accesses the underlying file-system
157096 ** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
157097 ** then the new OTA VFS uses the default system VFS to access the file-system.
157098 ** The new object is registered as a non-default VFS with SQLite before
157099 ** returning.
157100 **
157101 ** Part of the OTA implementation uses a custom VFS object. Usually, this
157102 ** object is created and deleted automatically by OTA.
157103 **
157104 ** The exception is for applications that also use zipvfs. In this case,
157105 ** the custom VFS must be explicitly created by the user before the OTA
157106 ** handle is opened. The OTA VFS should be installed so that the zipvfs
157107 ** VFS uses the OTA VFS, which in turn uses any other VFS layers in use
157108 ** (for example multiplexor) to access the file-system. For example,
157109 ** to assemble an OTA enabled VFS stack that uses both zipvfs and
157110 ** multiplexor (error checking omitted):
157111 **
157112 ** // Create a VFS named "multiplex" (not the default).
157113 ** sqlite3_multiplex_initialize(0, 0);
157114 **
157115 ** // Create an ota VFS named "ota" that uses multiplexor. If the
157116 ** // second argument were replaced with NULL, the "ota" VFS would
157117 ** // access the file-system via the system default VFS, bypassing the
157118 ** // multiplexor.
157119 ** sqlite3ota_create_vfs("ota", "multiplex");
157120 **
157121 ** // Create a zipvfs VFS named "zipvfs" that uses ota.
157122 ** zipvfs_create_vfs_v3("zipvfs", "ota", 0, xCompressorAlgorithmDetector);
157123 **
157124 ** // Make zipvfs the default VFS.
157125 ** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
157126 **
157127 ** Because the default VFS created above includes a OTA functionality, it
157128 ** may be used by OTA clients. Attempting to use OTA with a zipvfs VFS stack
157129 ** that does not include the OTA layer results in an error.
157130 **
157131 ** The overhead of adding the "ota" VFS to the system is negligible for
157132 ** non-OTA users. There is no harm in an application accessing the
157133 ** file-system via "ota" all the time, even if it only uses OTA functionality
157134 ** occasionally.
157135 */
157136 SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent);
157137
157138 /*
157139 ** Deregister and destroy an OTA vfs created by an earlier call to
157140 ** sqlite3ota_create_vfs().
157141 **
157142 ** VFS objects are not reference counted. If a VFS object is destroyed
157143 ** before all database handles that use it have been closed, the results
157144 ** are undefined.
157145 */
157146 SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName);
157147
157148 #endif /* _SQLITE3OTA_H */
157149
157150
157151 /************** End of sqlite3ota.h ******************************************/
157152 /************** Continuing where we left off in sqlite3ota.c *****************/
157153
157154 /* Maximum number of prepared UPDATE statements held by this module */
157155 #define SQLITE_OTA_UPDATE_CACHESIZE 16
157156
157157 /*
157158 ** Swap two objects of type TYPE.
157159 */
157160 #if !defined(SQLITE_AMALGAMATION)
157161 # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
157162 #endif
157163
157164 /*
157165 ** The ota_state table is used to save the state of a partially applied
157166 ** update so that it can be resumed later. The table consists of integer
157167 ** keys mapped to values as follows:
157168 **
157169 ** OTA_STATE_STAGE:
157170 ** May be set to integer values 1, 2, 4 or 5. As follows:
157171 ** 1: the *-ota file is currently under construction.
157172 ** 2: the *-ota file has been constructed, but not yet moved
157173 ** to the *-wal path.
157174 ** 4: the checkpoint is underway.
157175 ** 5: the ota update has been checkpointed.
157176 **
157177 ** OTA_STATE_TBL:
157178 ** Only valid if STAGE==1. The target database name of the table
157179 ** currently being written.
157180 **
157181 ** OTA_STATE_IDX:
157182 ** Only valid if STAGE==1. The target database name of the index
157183 ** currently being written, or NULL if the main table is currently being
157184 ** updated.
157185 **
157186 ** OTA_STATE_ROW:
157187 ** Only valid if STAGE==1. Number of rows already processed for the current
157188 ** table/index.
157189 **
157190 ** OTA_STATE_PROGRESS:
157191 ** Total number of sqlite3ota_step() calls made so far as part of this
157192 ** ota update.
157193 **
157194 ** OTA_STATE_CKPT:
157195 ** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
157196 ** header created by recovering the *-wal file. This is used to detect
157197 ** cases when another client appends frames to the *-wal file in the
157198 ** middle of an incremental checkpoint (an incremental checkpoint cannot
157199 ** be continued if this happens).
157200 **
157201 ** OTA_STATE_COOKIE:
157202 ** Valid if STAGE==1. The current change-counter cookie value in the
157203 ** target db file.
157204 **
157205 ** OTA_STATE_OALSZ:
157206 ** Valid if STAGE==1. The size in bytes of the *-oal file.
157207 */
157208 #define OTA_STATE_STAGE 1
157209 #define OTA_STATE_TBL 2
157210 #define OTA_STATE_IDX 3
157211 #define OTA_STATE_ROW 4
157212 #define OTA_STATE_PROGRESS 5
157213 #define OTA_STATE_CKPT 6
157214 #define OTA_STATE_COOKIE 7
157215 #define OTA_STATE_OALSZ 8
157216
157217 #define OTA_STAGE_OAL 1
157218 #define OTA_STAGE_MOVE 2
157219 #define OTA_STAGE_CAPTURE 3
157220 #define OTA_STAGE_CKPT 4
157221 #define OTA_STAGE_DONE 5
157222
157223
157224 #define OTA_CREATE_STATE \
157225 "CREATE TABLE IF NOT EXISTS %s.ota_state(k INTEGER PRIMARY KEY, v)"
157226
157227 typedef struct OtaFrame OtaFrame;
157228 typedef struct OtaObjIter OtaObjIter;
157229 typedef struct OtaState OtaState;
157230 typedef struct ota_vfs ota_vfs;
157231 typedef struct ota_file ota_file;
157232 typedef struct OtaUpdateStmt OtaUpdateStmt;
157233
157234 #if !defined(SQLITE_AMALGAMATION)
157235 typedef unsigned int u32;
157236 typedef unsigned char u8;
157237 typedef sqlite3_int64 i64;
@@ -157245,13 +157447,13 @@
157245 #define WAL_LOCK_WRITE 0
157246 #define WAL_LOCK_CKPT 1
157247 #define WAL_LOCK_READ0 3
157248
157249 /*
157250 ** A structure to store values read from the ota_state table in memory.
157251 */
157252 struct OtaState {
157253 int eStage;
157254 char *zTbl;
157255 char *zIdx;
157256 i64 iWalCksum;
157257 int nRow;
@@ -157258,14 +157460,14 @@
157258 i64 nProgress;
157259 u32 iCookie;
157260 i64 iOalSz;
157261 };
157262
157263 struct OtaUpdateStmt {
157264 char *zMask; /* Copy of update mask used with pUpdate */
157265 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
157266 OtaUpdateStmt *pNext;
157267 };
157268
157269 /*
157270 ** An iterator of this type is used to iterate through all objects in
157271 ** the target database that require updating. For each such table, the
@@ -157280,131 +157482,131 @@
157280 ** it points to an array of flags nTblCol elements in size. The flag is
157281 ** set for each column that is either a part of the PK or a part of an
157282 ** index. Or clear otherwise.
157283 **
157284 */
157285 struct OtaObjIter {
157286 sqlite3_stmt *pTblIter; /* Iterate through tables */
157287 sqlite3_stmt *pIdxIter; /* Index iterator */
157288 int nTblCol; /* Size of azTblCol[] array */
157289 char **azTblCol; /* Array of unquoted target column names */
157290 char **azTblType; /* Array of target column types */
157291 int *aiSrcOrder; /* src table col -> target table col */
157292 u8 *abTblPk; /* Array of flags, set on target PK columns */
157293 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
157294 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
157295 int eType; /* Table type - an OTA_PK_XXX value */
157296
157297 /* Output variables. zTbl==0 implies EOF. */
157298 int bCleanup; /* True in "cleanup" state */
157299 const char *zTbl; /* Name of target db table */
157300 const char *zIdx; /* Name of target db index (or null) */
157301 int iTnum; /* Root page of current object */
157302 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
157303 int bUnique; /* Current index is unique */
157304
157305 /* Statements created by otaObjIterPrepareAll() */
157306 int nCol; /* Number of columns in current object */
157307 sqlite3_stmt *pSelect; /* Source data */
157308 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
157309 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
157310 sqlite3_stmt *pTmpInsert; /* Insert into ota_tmp_$zTbl */
157311
157312 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
157313 OtaUpdateStmt *pOtaUpdate;
157314 };
157315
157316 /*
157317 ** Values for OtaObjIter.eType
157318 **
157319 ** 0: Table does not exist (error)
157320 ** 1: Table has an implicit rowid.
157321 ** 2: Table has an explicit IPK column.
157322 ** 3: Table has an external PK index.
157323 ** 4: Table is WITHOUT ROWID.
157324 ** 5: Table is a virtual table.
157325 */
157326 #define OTA_PK_NOTABLE 0
157327 #define OTA_PK_NONE 1
157328 #define OTA_PK_IPK 2
157329 #define OTA_PK_EXTERNAL 3
157330 #define OTA_PK_WITHOUT_ROWID 4
157331 #define OTA_PK_VTAB 5
157332
157333
157334 /*
157335 ** Within the OTA_STAGE_OAL stage, each call to sqlite3ota_step() performs
157336 ** one of the following operations.
157337 */
157338 #define OTA_INSERT 1 /* Insert on a main table b-tree */
157339 #define OTA_DELETE 2 /* Delete a row from a main table b-tree */
157340 #define OTA_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
157341 #define OTA_IDX_INSERT 4 /* Insert on an aux. index b-tree */
157342 #define OTA_UPDATE 5 /* Update a row in a main table b-tree */
157343
157344
157345 /*
157346 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
157347 ** file should be copied to page iDbPage of the database file.
157348 */
157349 struct OtaFrame {
157350 u32 iDbPage;
157351 u32 iWalFrame;
157352 };
157353
157354 /*
157355 ** OTA handle.
157356 */
157357 struct sqlite3ota {
157358 int eStage; /* Value of OTA_STATE_STAGE field */
157359 sqlite3 *dbMain; /* target database handle */
157360 sqlite3 *dbOta; /* ota database handle */
157361 char *zTarget; /* Path to target db */
157362 char *zOta; /* Path to ota db */
157363 char *zState; /* Path to state db (or NULL if zOta) */
157364 char zStateDb[5]; /* Db name for state ("stat" or "main") */
157365 int rc; /* Value returned by last ota_step() call */
157366 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
157367 int nStep; /* Rows processed for current object */
157368 int nProgress; /* Rows processed for all objects */
157369 OtaObjIter objiter; /* Iterator for skipping through tbl/idx */
157370 const char *zVfsName; /* Name of automatically created ota vfs */
157371 ota_file *pTargetFd; /* File handle open on target db */
157372 i64 iOalSz;
157373
157374 /* The following state variables are used as part of the incremental
157375 ** checkpoint stage (eStage==OTA_STAGE_CKPT). See comments surrounding
157376 ** function otaSetupCheckpoint() for details. */
157377 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
157378 u32 mLock;
157379 int nFrame; /* Entries in aFrame[] array */
157380 int nFrameAlloc; /* Allocated size of aFrame[] array */
157381 OtaFrame *aFrame;
157382 int pgsz;
157383 u8 *aBuf;
157384 i64 iWalCksum;
157385 };
157386
157387 /*
157388 ** An ota VFS is implemented using an instance of this structure.
157389 */
157390 struct ota_vfs {
157391 sqlite3_vfs base; /* ota VFS shim methods */
157392 sqlite3_vfs *pRealVfs; /* Underlying VFS */
157393 sqlite3_mutex *mutex; /* Mutex to protect pMain */
157394 ota_file *pMain; /* Linked list of main db files */
157395 };
157396
157397 /*
157398 ** Each file opened by an ota VFS is represented by an instance of
157399 ** the following structure.
157400 */
157401 struct ota_file {
157402 sqlite3_file base; /* sqlite3_file methods */
157403 sqlite3_file *pReal; /* Underlying file handle */
157404 ota_vfs *pOtaVfs; /* Pointer to the ota_vfs object */
157405 sqlite3ota *pOta; /* Pointer to ota object (ota target only) */
157406
157407 int openFlags; /* Flags this file was opened with */
157408 u32 iCookie; /* Cookie value for main db files */
157409 u8 iWriteVer; /* "write-version" value for main db files */
157410
@@ -157411,12 +157613,12 @@
157411 int nShm; /* Number of entries in apShm[] array */
157412 char **apShm; /* Array of mmap'd *-shm regions */
157413 char *zDel; /* Delete this when closing file */
157414
157415 const char *zWal; /* Wal filename for this main db file */
157416 ota_file *pWalFd; /* Wal file descriptor for this main db */
157417 ota_file *pMainNext; /* Next MAIN_DB file */
157418 };
157419
157420
157421 /*
157422 ** Prepare the SQL statement in buffer zSql against database handle db.
@@ -157490,14 +157692,14 @@
157490 }
157491 return rc;
157492 }
157493
157494 /*
157495 ** Free the OtaObjIter.azTblCol[] and OtaObjIter.abTblPk[] arrays allocated
157496 ** by an earlier call to otaObjIterCacheTableInfo().
157497 */
157498 static void otaObjIterFreeCols(OtaObjIter *pIter){
157499 int i;
157500 for(i=0; i<pIter->nTblCol; i++){
157501 sqlite3_free(pIter->azTblCol[i]);
157502 sqlite3_free(pIter->azTblType[i]);
157503 }
@@ -157513,72 +157715,72 @@
157513
157514 /*
157515 ** Finalize all statements and free all allocations that are specific to
157516 ** the current object (table/index pair).
157517 */
157518 static void otaObjIterClearStatements(OtaObjIter *pIter){
157519 OtaUpdateStmt *pUp;
157520
157521 sqlite3_finalize(pIter->pSelect);
157522 sqlite3_finalize(pIter->pInsert);
157523 sqlite3_finalize(pIter->pDelete);
157524 sqlite3_finalize(pIter->pTmpInsert);
157525 pUp = pIter->pOtaUpdate;
157526 while( pUp ){
157527 OtaUpdateStmt *pTmp = pUp->pNext;
157528 sqlite3_finalize(pUp->pUpdate);
157529 sqlite3_free(pUp);
157530 pUp = pTmp;
157531 }
157532
157533 pIter->pSelect = 0;
157534 pIter->pInsert = 0;
157535 pIter->pDelete = 0;
157536 pIter->pOtaUpdate = 0;
157537 pIter->pTmpInsert = 0;
157538 pIter->nCol = 0;
157539 }
157540
157541 /*
157542 ** Clean up any resources allocated as part of the iterator object passed
157543 ** as the only argument.
157544 */
157545 static void otaObjIterFinalize(OtaObjIter *pIter){
157546 otaObjIterClearStatements(pIter);
157547 sqlite3_finalize(pIter->pTblIter);
157548 sqlite3_finalize(pIter->pIdxIter);
157549 otaObjIterFreeCols(pIter);
157550 memset(pIter, 0, sizeof(OtaObjIter));
157551 }
157552
157553 /*
157554 ** Advance the iterator to the next position.
157555 **
157556 ** If no error occurs, SQLITE_OK is returned and the iterator is left
157557 ** pointing to the next entry. Otherwise, an error code and message is
157558 ** left in the OTA handle passed as the first argument. A copy of the
157559 ** error code is returned.
157560 */
157561 static int otaObjIterNext(sqlite3ota *p, OtaObjIter *pIter){
157562 int rc = p->rc;
157563 if( rc==SQLITE_OK ){
157564
157565 /* Free any SQLite statements used while processing the previous object */
157566 otaObjIterClearStatements(pIter);
157567 if( pIter->zIdx==0 ){
157568 rc = sqlite3_exec(p->dbMain,
157569 "DROP TRIGGER IF EXISTS temp.ota_insert_tr;"
157570 "DROP TRIGGER IF EXISTS temp.ota_update1_tr;"
157571 "DROP TRIGGER IF EXISTS temp.ota_update2_tr;"
157572 "DROP TRIGGER IF EXISTS temp.ota_delete_tr;"
157573 , 0, 0, &p->zErrmsg
157574 );
157575 }
157576
157577 if( rc==SQLITE_OK ){
157578 if( pIter->bCleanup ){
157579 otaObjIterFreeCols(pIter);
157580 pIter->bCleanup = 0;
157581 rc = sqlite3_step(pIter->pTblIter);
157582 if( rc!=SQLITE_ROW ){
157583 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
157584 pIter->zTbl = 0;
@@ -157607,11 +157809,11 @@
157607 }
157608 }
157609 }
157610
157611 if( rc!=SQLITE_OK ){
157612 otaObjIterFinalize(pIter);
157613 p->rc = rc;
157614 }
157615 return rc;
157616 }
157617
@@ -157618,18 +157820,18 @@
157618 /*
157619 ** Initialize the iterator structure passed as the second argument.
157620 **
157621 ** If no error occurs, SQLITE_OK is returned and the iterator is left
157622 ** pointing to the first entry. Otherwise, an error code and message is
157623 ** left in the OTA handle passed as the first argument. A copy of the
157624 ** error code is returned.
157625 */
157626 static int otaObjIterFirst(sqlite3ota *p, OtaObjIter *pIter){
157627 int rc;
157628 memset(pIter, 0, sizeof(OtaObjIter));
157629
157630 rc = prepareAndCollectError(p->dbOta, &pIter->pTblIter, &p->zErrmsg,
157631 "SELECT substr(name, 6) FROM sqlite_master "
157632 "WHERE type='table' AND name LIKE 'data_%'"
157633 );
157634
157635 if( rc==SQLITE_OK ){
@@ -157640,23 +157842,23 @@
157640 );
157641 }
157642
157643 pIter->bCleanup = 1;
157644 p->rc = rc;
157645 return otaObjIterNext(p, pIter);
157646 }
157647
157648 /*
157649 ** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
157650 ** an error code is stored in the OTA handle passed as the first argument.
157651 **
157652 ** If an error has already occurred (p->rc is already set to something other
157653 ** than SQLITE_OK), then this function returns NULL without modifying the
157654 ** stored error code. In this case it still calls sqlite3_free() on any
157655 ** printf() parameters associated with %z conversions.
157656 */
157657 static char *otaMPrintf(sqlite3ota *p, const char *zFmt, ...){
157658 char *zSql = 0;
157659 va_list ap;
157660 va_start(ap, zFmt);
157661 zSql = sqlite3_vmprintf(zFmt, ap);
157662 if( p->rc==SQLITE_OK ){
@@ -157671,17 +157873,17 @@
157671
157672 /*
157673 ** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
157674 ** arguments are the usual subsitution values. This function performs
157675 ** the printf() style substitutions and executes the result as an SQL
157676 ** statement on the OTA handles database.
157677 **
157678 ** If an error occurs, an error code and error message is stored in the
157679 ** OTA handle. If an error has already occurred when this function is
157680 ** called, it is a no-op.
157681 */
157682 static int otaMPrintfExec(sqlite3ota *p, sqlite3 *db, const char *zFmt, ...){
157683 va_list ap;
157684 va_start(ap, zFmt);
157685 char *zSql = sqlite3_vmprintf(zFmt, ap);
157686 if( p->rc==SQLITE_OK ){
157687 if( zSql==0 ){
@@ -157698,16 +157900,16 @@
157698 /*
157699 ** Attempt to allocate and return a pointer to a zeroed block of nByte
157700 ** bytes.
157701 **
157702 ** If an error (i.e. an OOM condition) occurs, return NULL and leave an
157703 ** error code in the ota handle passed as the first argument. Or, if an
157704 ** error has already occurred when this function is called, return NULL
157705 ** immediately without attempting the allocation or modifying the stored
157706 ** error code.
157707 */
157708 static void *otaMalloc(sqlite3ota *p, int nByte){
157709 void *pRet = 0;
157710 if( p->rc==SQLITE_OK ){
157711 assert( nByte>0 );
157712 pRet = sqlite3_malloc(nByte);
157713 if( pRet==0 ){
@@ -157721,17 +157923,17 @@
157721
157722
157723 /*
157724 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
157725 ** there is room for at least nCol elements. If an OOM occurs, store an
157726 ** error code in the OTA handle passed as the first argument.
157727 */
157728 static void otaAllocateIterArrays(sqlite3ota *p, OtaObjIter *pIter, int nCol){
157729 int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
157730 char **azNew;
157731
157732 azNew = (char**)otaMalloc(p, nByte);
157733 if( azNew ){
157734 pIter->azTblCol = azNew;
157735 pIter->azTblType = &azNew[nCol];
157736 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
157737 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
@@ -157748,11 +157950,11 @@
157748 **
157749 ** If an OOM condition is encountered when attempting to allocate memory,
157750 ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
157751 ** if the allocation succeeds, (*pRc) is left unchanged.
157752 */
157753 static char *otaStrndup(const char *zStr, int *pRc){
157754 char *zRet = 0;
157755
157756 assert( *pRc==SQLITE_OK );
157757 if( zStr ){
157758 int nCopy = strlen(zStr) + 1;
@@ -157769,14 +157971,14 @@
157769
157770 /*
157771 ** Finalize the statement passed as the second argument.
157772 **
157773 ** If the sqlite3_finalize() call indicates that an error occurs, and the
157774 ** ota handle error code is not already set, set the error code and error
157775 ** message accordingly.
157776 */
157777 static void otaFinalize(sqlite3ota *p, sqlite3_stmt *pStmt){
157778 sqlite3 *db = sqlite3_db_handle(pStmt);
157779 int rc = sqlite3_finalize(pStmt);
157780 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
157781 p->rc = rc;
157782 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
@@ -157787,16 +157989,16 @@
157787 **
157788 ** peType is of type (int*), a pointer to an output parameter of type
157789 ** (int). This call sets the output parameter as follows, depending
157790 ** on the type of the table specified by parameters dbName and zTbl.
157791 **
157792 ** OTA_PK_NOTABLE: No such table.
157793 ** OTA_PK_NONE: Table has an implicit rowid.
157794 ** OTA_PK_IPK: Table has an explicit IPK column.
157795 ** OTA_PK_EXTERNAL: Table has an external PK index.
157796 ** OTA_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
157797 ** OTA_PK_VTAB: Table is a virtual table.
157798 **
157799 ** Argument *piPk is also of type (int*), and also points to an output
157800 ** parameter. Unless the table has an external primary key index
157801 ** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
157802 ** if the table does have an external primary key index, then *piPk
@@ -157804,28 +158006,28 @@
157804 ** returning.
157805 **
157806 ** ALGORITHM:
157807 **
157808 ** if( no entry exists in sqlite_master ){
157809 ** return OTA_PK_NOTABLE
157810 ** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
157811 ** return OTA_PK_VTAB
157812 ** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
157813 ** if( the index that is the pk exists in sqlite_master ){
157814 ** *piPK = rootpage of that index.
157815 ** return OTA_PK_EXTERNAL
157816 ** }else{
157817 ** return OTA_PK_WITHOUT_ROWID
157818 ** }
157819 ** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
157820 ** return OTA_PK_IPK
157821 ** }else{
157822 ** return OTA_PK_NONE
157823 ** }
157824 */
157825 static void otaTableType(
157826 sqlite3ota *p,
157827 const char *zTab,
157828 int *peType,
157829 int *piTnum,
157830 int *piPk
157831 ){
@@ -157835,11 +158037,11 @@
157835 ** 2) SELECT count(*) FROM sqlite_master where name=%Q
157836 ** 3) PRAGMA table_info = ?
157837 */
157838 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
157839
157840 *peType = OTA_PK_NOTABLE;
157841 *piPk = 0;
157842
157843 assert( p->rc==SQLITE_OK );
157844 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
157845 sqlite3_mprintf(
@@ -157847,22 +158049,22 @@
157847 " FROM sqlite_master"
157848 " WHERE name=%Q", zTab
157849 ));
157850 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
157851 /* Either an error, or no such table. */
157852 goto otaTableType_end;
157853 }
157854 if( sqlite3_column_int(aStmt[0], 0) ){
157855 *peType = OTA_PK_VTAB; /* virtual table */
157856 goto otaTableType_end;
157857 }
157858 *piTnum = sqlite3_column_int(aStmt[0], 1);
157859
157860 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
157861 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
157862 );
157863 if( p->rc ) goto otaTableType_end;
157864 while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
157865 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
157866 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
157867 if( zOrig && zIdx && zOrig[0]=='p' ){
157868 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
@@ -157870,45 +158072,45 @@
157870 "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
157871 ));
157872 if( p->rc==SQLITE_OK ){
157873 if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
157874 *piPk = sqlite3_column_int(aStmt[2], 0);
157875 *peType = OTA_PK_EXTERNAL;
157876 }else{
157877 *peType = OTA_PK_WITHOUT_ROWID;
157878 }
157879 }
157880 goto otaTableType_end;
157881 }
157882 }
157883
157884 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
157885 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
157886 );
157887 if( p->rc==SQLITE_OK ){
157888 while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
157889 if( sqlite3_column_int(aStmt[3],5)>0 ){
157890 *peType = OTA_PK_IPK; /* explicit IPK column */
157891 goto otaTableType_end;
157892 }
157893 }
157894 *peType = OTA_PK_NONE;
157895 }
157896
157897 otaTableType_end: {
157898 int i;
157899 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
157900 otaFinalize(p, aStmt[i]);
157901 }
157902 }
157903 }
157904
157905 /*
157906 ** This is a helper function for otaObjIterCacheTableInfo(). It populates
157907 ** the pIter->abIndexed[] array.
157908 */
157909 static void otaObjIterCacheIndexedCols(sqlite3ota *p, OtaObjIter *pIter){
157910 sqlite3_stmt *pList = 0;
157911 int bIndex = 0;
157912
157913 if( p->rc==SQLITE_OK ){
157914 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
@@ -157926,15 +158128,15 @@
157926 );
157927 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
157928 int iCid = sqlite3_column_int(pXInfo, 1);
157929 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
157930 }
157931 otaFinalize(p, pXInfo);
157932 bIndex = 1;
157933 }
157934
157935 otaFinalize(p, pList);
157936 if( bIndex==0 ) pIter->abIndexed = 0;
157937 }
157938
157939
157940 /*
@@ -157942,67 +158144,67 @@
157942 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
157943 ** the table (not index) that the iterator currently points to.
157944 **
157945 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
157946 ** an error does occur, an error code and error message are also left in
157947 ** the OTA handle.
157948 */
157949 static int otaObjIterCacheTableInfo(sqlite3ota *p, OtaObjIter *pIter){
157950 if( pIter->azTblCol==0 ){
157951 sqlite3_stmt *pStmt = 0;
157952 int nCol = 0;
157953 int i; /* for() loop iterator variable */
157954 int bOtaRowid = 0; /* If input table has column "ota_rowid" */
157955 int iOrder = 0;
157956 int iTnum = 0;
157957
157958 /* Figure out the type of table this step will deal with. */
157959 assert( pIter->eType==0 );
157960 otaTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
157961 if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_NOTABLE ){
157962 p->rc = SQLITE_ERROR;
157963 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
157964 }
157965 if( p->rc ) return p->rc;
157966 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
157967
157968 assert( pIter->eType==OTA_PK_NONE || pIter->eType==OTA_PK_IPK
157969 || pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_WITHOUT_ROWID
157970 || pIter->eType==OTA_PK_VTAB
157971 );
157972
157973 /* Populate the azTblCol[] and nTblCol variables based on the columns
157974 ** of the input table. Ignore any input table columns that begin with
157975 ** "ota_". */
157976 p->rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
157977 sqlite3_mprintf("SELECT * FROM 'data_%q'", pIter->zTbl)
157978 );
157979 if( p->rc==SQLITE_OK ){
157980 nCol = sqlite3_column_count(pStmt);
157981 otaAllocateIterArrays(p, pIter, nCol);
157982 }
157983 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
157984 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
157985 if( sqlite3_strnicmp("ota_", zName, 4) ){
157986 char *zCopy = otaStrndup(zName, &p->rc);
157987 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
157988 pIter->azTblCol[pIter->nTblCol++] = zCopy;
157989 }
157990 else if( 0==sqlite3_stricmp("ota_rowid", zName) ){
157991 bOtaRowid = 1;
157992 }
157993 }
157994 sqlite3_finalize(pStmt);
157995 pStmt = 0;
157996
157997 if( p->rc==SQLITE_OK
157998 && bOtaRowid!=(pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
157999 ){
158000 p->rc = SQLITE_ERROR;
158001 p->zErrmsg = sqlite3_mprintf(
158002 "table data_%q %s ota_rowid column", pIter->zTbl,
158003 (bOtaRowid ? "may not have" : "requires")
158004 );
158005 }
158006
158007 /* Check that all non-HIDDEN columns in the destination table are also
158008 ** present in the input table. Populate the abTblPk[], azTblType[] and
@@ -158031,20 +158233,20 @@
158031 if( i!=iOrder ){
158032 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
158033 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
158034 }
158035
158036 pIter->azTblType[iOrder] = otaStrndup(zType, &p->rc);
158037 pIter->abTblPk[iOrder] = (iPk!=0);
158038 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
158039 iOrder++;
158040 }
158041 }
158042
158043 otaFinalize(p, pStmt);
158044 otaObjIterCacheIndexedCols(p, pIter);
158045 assert( pIter->eType!=OTA_PK_VTAB || pIter->abIndexed==0 );
158046 }
158047
158048 return p->rc;
158049 }
158050
@@ -158051,29 +158253,29 @@
158051 /*
158052 ** This function constructs and returns a pointer to a nul-terminated
158053 ** string containing some SQL clause or list based on one or more of the
158054 ** column names currently stored in the pIter->azTblCol[] array.
158055 */
158056 static char *otaObjIterGetCollist(
158057 sqlite3ota *p, /* OTA object */
158058 OtaObjIter *pIter /* Object iterator for column names */
158059 ){
158060 char *zList = 0;
158061 const char *zSep = "";
158062 int i;
158063 for(i=0; i<pIter->nTblCol; i++){
158064 const char *z = pIter->azTblCol[i];
158065 zList = otaMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
158066 zSep = ", ";
158067 }
158068 return zList;
158069 }
158070
158071 /*
158072 ** This function is used to create a SELECT list (the list of SQL
158073 ** expressions that follows a SELECT keyword) for a SELECT statement
158074 ** used to read from an data_xxx or ota_tmp_xxx table while updating the
158075 ** index object currently indicated by the iterator object passed as the
158076 ** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
158077 ** to obtain the required information.
158078 **
158079 ** If the index is of the following form:
@@ -158090,17 +158292,17 @@
158090 **
158091 ** pzImposterCols: ...
158092 ** pzImposterPk: ...
158093 ** pzWhere: ...
158094 */
158095 static char *otaObjIterGetIndexCols(
158096 sqlite3ota *p, /* OTA object */
158097 OtaObjIter *pIter, /* Object iterator for column names */
158098 char **pzImposterCols, /* OUT: Columns for imposter table */
158099 char **pzImposterPk, /* OUT: Imposter PK clause */
158100 char **pzWhere, /* OUT: WHERE clause */
158101 int *pnBind /* OUT: Total number of columns */
158102 ){
158103 int rc = p->rc; /* Error code */
158104 int rc2; /* sqlite3_finalize() return code */
158105 char *zRet = 0; /* String to return */
158106 char *zImpCols = 0; /* String to return via *pzImposterCols */
@@ -158125,18 +158327,18 @@
158125 const char *zCol;
158126 const char *zType;
158127
158128 if( iCid<0 ){
158129 /* An integer primary key. If the table has an explicit IPK, use
158130 ** its name. Otherwise, use "ota_rowid". */
158131 if( pIter->eType==OTA_PK_IPK ){
158132 int i;
158133 for(i=0; pIter->abTblPk[i]==0; i++);
158134 assert( i<pIter->nTblCol );
158135 zCol = pIter->azTblCol[i];
158136 }else{
158137 zCol = "ota_rowid";
158138 }
158139 zType = "INTEGER";
158140 }else{
158141 zCol = pIter->azTblCol[iCid];
158142 zType = pIter->azTblType[iCid];
@@ -158143,19 +158345,19 @@
158143 }
158144
158145 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
158146 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
158147 const char *zOrder = (bDesc ? " DESC" : "");
158148 zImpPK = sqlite3_mprintf("%z%s\"ota_imp_%d%w\"%s",
158149 zImpPK, zCom, nBind, zCol, zOrder
158150 );
158151 }
158152 zImpCols = sqlite3_mprintf("%z%s\"ota_imp_%d%w\" %s COLLATE %Q",
158153 zImpCols, zCom, nBind, zCol, zType, zCollate
158154 );
158155 zWhere = sqlite3_mprintf(
158156 "%z%s\"ota_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
158157 );
158158 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
158159 zCom = ", ";
158160 zAnd = " AND ";
158161 nBind++;
@@ -158189,16 +158391,16 @@
158189 **
158190 ** "old.a, old.b, old.b"
158191 **
158192 ** With the column names escaped.
158193 **
158194 ** For tables with implicit rowids - OTA_PK_EXTERNAL and OTA_PK_NONE, append
158195 ** the text ", old._rowid_" to the returned value.
158196 */
158197 static char *otaObjIterGetOldlist(
158198 sqlite3ota *p,
158199 OtaObjIter *pIter,
158200 const char *zObj
158201 ){
158202 char *zList = 0;
158203 if( p->rc==SQLITE_OK && pIter->abIndexed ){
158204 const char *zS = "";
@@ -158216,12 +158418,12 @@
158216 break;
158217 }
158218 }
158219
158220 /* For a table with implicit rowids, append "old._rowid_" to the list. */
158221 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158222 zList = otaMPrintf(p, "%z, %s._rowid_", zList, zObj);
158223 }
158224 }
158225 return zList;
158226 }
158227
@@ -158233,37 +158435,37 @@
158233 **
158234 ** Return the string:
158235 **
158236 ** "b = ?1 AND c = ?2"
158237 */
158238 static char *otaObjIterGetWhere(
158239 sqlite3ota *p,
158240 OtaObjIter *pIter
158241 ){
158242 char *zList = 0;
158243 if( pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE ){
158244 zList = otaMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
158245 }else if( pIter->eType==OTA_PK_EXTERNAL ){
158246 const char *zSep = "";
158247 int i;
158248 for(i=0; i<pIter->nTblCol; i++){
158249 if( pIter->abTblPk[i] ){
158250 zList = otaMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
158251 zSep = " AND ";
158252 }
158253 }
158254 zList = otaMPrintf(p,
158255 "_rowid_ = (SELECT id FROM ota_imposter2 WHERE %z)", zList
158256 );
158257
158258 }else{
158259 const char *zSep = "";
158260 int i;
158261 for(i=0; i<pIter->nTblCol; i++){
158262 if( pIter->abTblPk[i] ){
158263 const char *zCol = pIter->azTblCol[i];
158264 zList = otaMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
158265 zSep = " AND ";
158266 }
158267 }
158268 }
158269 return zList;
@@ -158270,60 +158472,60 @@
158270 }
158271
158272 /*
158273 ** The SELECT statement iterating through the keys for the current object
158274 ** (p->objiter.pSelect) currently points to a valid row. However, there
158275 ** is something wrong with the ota_control value in the ota_control value
158276 ** stored in the (p->nCol+1)'th column. Set the error code and error message
158277 ** of the OTA handle to something reflecting this.
158278 */
158279 static void otaBadControlError(sqlite3ota *p){
158280 p->rc = SQLITE_ERROR;
158281 p->zErrmsg = sqlite3_mprintf("invalid ota_control value");
158282 }
158283
158284
158285 /*
158286 ** Return a nul-terminated string containing the comma separated list of
158287 ** assignments that should be included following the "SET" keyword of
158288 ** an UPDATE statement used to update the table object that the iterator
158289 ** passed as the second argument currently points to if the ota_control
158290 ** column of the data_xxx table entry is set to zMask.
158291 **
158292 ** The memory for the returned string is obtained from sqlite3_malloc().
158293 ** It is the responsibility of the caller to eventually free it using
158294 ** sqlite3_free().
158295 **
158296 ** If an OOM error is encountered when allocating space for the new
158297 ** string, an error code is left in the ota handle passed as the first
158298 ** argument and NULL is returned. Or, if an error has already occurred
158299 ** when this function is called, NULL is returned immediately, without
158300 ** attempting the allocation or modifying the stored error code.
158301 */
158302 static char *otaObjIterGetSetlist(
158303 sqlite3ota *p,
158304 OtaObjIter *pIter,
158305 const char *zMask
158306 ){
158307 char *zList = 0;
158308 if( p->rc==SQLITE_OK ){
158309 int i;
158310
158311 if( strlen(zMask)!=pIter->nTblCol ){
158312 otaBadControlError(p);
158313 }else{
158314 const char *zSep = "";
158315 for(i=0; i<pIter->nTblCol; i++){
158316 char c = zMask[pIter->aiSrcOrder[i]];
158317 if( c=='x' ){
158318 zList = otaMPrintf(p, "%z%s\"%w\"=?%d",
158319 zList, zSep, pIter->azTblCol[i], i+1
158320 );
158321 zSep = ", ";
158322 }
158323 if( c=='d' ){
158324 zList = otaMPrintf(p, "%z%s\"%w\"=ota_delta(\"%w\", ?%d)",
158325 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
158326 );
158327 zSep = ", ";
158328 }
158329 }
@@ -158340,20 +158542,20 @@
158340 ** The memory for the returned string is obtained from sqlite3_malloc().
158341 ** It is the responsibility of the caller to eventually free it using
158342 ** sqlite3_free().
158343 **
158344 ** If an OOM error is encountered when allocating space for the new
158345 ** string, an error code is left in the ota handle passed as the first
158346 ** argument and NULL is returned. Or, if an error has already occurred
158347 ** when this function is called, NULL is returned immediately, without
158348 ** attempting the allocation or modifying the stored error code.
158349 */
158350 static char *otaObjIterGetBindlist(sqlite3ota *p, int nBind){
158351 char *zRet = 0;
158352 int nByte = nBind*2 + 1;
158353
158354 zRet = (char*)otaMalloc(p, nByte);
158355 if( zRet ){
158356 int i;
158357 for(i=0; i<nBind; i++){
158358 zRet[i*2] = '?';
158359 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
@@ -158362,21 +158564,21 @@
158362 return zRet;
158363 }
158364
158365 /*
158366 ** The iterator currently points to a table (not index) of type
158367 ** OTA_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
158368 ** declaration for the corresponding imposter table. For example,
158369 ** if the iterator points to a table created as:
158370 **
158371 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
158372 **
158373 ** this function returns:
158374 **
158375 ** PRIMARY KEY("b", "a" DESC)
158376 */
158377 static char *otaWithoutRowidPK(sqlite3ota *p, OtaObjIter *pIter){
158378 char *z = 0;
158379 assert( pIter->zIdx==0 );
158380 if( p->rc==SQLITE_OK ){
158381 const char *zSep = "PRIMARY KEY(";
158382 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
@@ -158395,23 +158597,23 @@
158395 );
158396 }
158397 break;
158398 }
158399 }
158400 otaFinalize(p, pXList);
158401
158402 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158403 if( sqlite3_column_int(pXInfo, 5) ){
158404 /* int iCid = sqlite3_column_int(pXInfo, 0); */
158405 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
158406 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
158407 z = otaMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
158408 zSep = ", ";
158409 }
158410 }
158411 z = otaMPrintf(p, "%z)", z);
158412 otaFinalize(p, pXInfo);
158413 }
158414 return z;
158415 }
158416
158417 /*
@@ -158420,23 +158622,23 @@
158420 ** iterator passed as the second argument does not currently point to
158421 ** a table (not index) with an external primary key, this function is a
158422 ** no-op.
158423 **
158424 ** Assuming the iterator does point to a table with an external PK, this
158425 ** function creates a WITHOUT ROWID imposter table named "ota_imposter2"
158426 ** used to access that PK index. For example, if the target table is
158427 ** declared as follows:
158428 **
158429 ** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
158430 **
158431 ** then the imposter table schema is:
158432 **
158433 ** CREATE TABLE ota_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
158434 **
158435 */
158436 static void otaCreateImposterTable2(sqlite3ota *p, OtaObjIter *pIter){
158437 if( p->rc==SQLITE_OK && pIter->eType==OTA_PK_EXTERNAL ){
158438 int tnum = pIter->iPkTnum; /* Root page of PK index */
158439 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
158440 const char *zIdx = 0; /* Name of PK index */
158441 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
158442 const char *zComma = "";
@@ -158458,31 +158660,31 @@
158458 if( zIdx ){
158459 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
158460 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
158461 );
158462 }
158463 otaFinalize(p, pQuery);
158464
158465 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158466 int bKey = sqlite3_column_int(pXInfo, 5);
158467 if( bKey ){
158468 int iCid = sqlite3_column_int(pXInfo, 1);
158469 int bDesc = sqlite3_column_int(pXInfo, 3);
158470 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
158471 zCols = otaMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
158472 iCid, pIter->azTblType[iCid], zCollate
158473 );
158474 zPk = otaMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
158475 zComma = ", ";
158476 }
158477 }
158478 zCols = otaMPrintf(p, "%z, id INTEGER", zCols);
158479 otaFinalize(p, pXInfo);
158480
158481 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158482 otaMPrintfExec(p, p->dbMain,
158483 "CREATE TABLE ota_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
158484 zCols, zPk
158485 );
158486 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158487 }
158488 }
@@ -158489,28 +158691,28 @@
158489
158490 /*
158491 ** If an error has already occurred when this function is called, it
158492 ** immediately returns zero (without doing any work). Or, if an error
158493 ** occurs during the execution of this function, it sets the error code
158494 ** in the sqlite3ota object indicated by the first argument and returns
158495 ** zero.
158496 **
158497 ** The iterator passed as the second argument is guaranteed to point to
158498 ** a table (not an index) when this function is called. This function
158499 ** attempts to create any imposter table required to write to the main
158500 ** table b-tree of the table before returning. Non-zero is returned if
158501 ** an imposter table are created, or zero otherwise.
158502 **
158503 ** An imposter table is required in all cases except OTA_PK_VTAB. Only
158504 ** virtual tables are written to directly. The imposter table has the
158505 ** same schema as the actual target table (less any UNIQUE constraints).
158506 ** More precisely, the "same schema" means the same columns, types,
158507 ** collation sequences. For tables that do not have an external PRIMARY
158508 ** KEY, it also means the same PRIMARY KEY declaration.
158509 */
158510 static void otaCreateImposterTable(sqlite3ota *p, OtaObjIter *pIter){
158511 if( p->rc==SQLITE_OK && pIter->eType!=OTA_PK_VTAB ){
158512 int tnum = pIter->iTnum;
158513 const char *zComma = "";
158514 char *zSql = 0;
158515 int iCol;
158516 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
@@ -158522,73 +158724,73 @@
158522
158523 p->rc = sqlite3_table_column_metadata(
158524 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
158525 );
158526
158527 if( pIter->eType==OTA_PK_IPK && pIter->abTblPk[iCol] ){
158528 /* If the target table column is an "INTEGER PRIMARY KEY", add
158529 ** "PRIMARY KEY" to the imposter table column declaration. */
158530 zPk = "PRIMARY KEY ";
158531 }
158532 zSql = otaMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
158533 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
158534 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
158535 );
158536 zComma = ", ";
158537 }
158538
158539 if( pIter->eType==OTA_PK_WITHOUT_ROWID ){
158540 char *zPk = otaWithoutRowidPK(p, pIter);
158541 if( zPk ){
158542 zSql = otaMPrintf(p, "%z, %z", zSql, zPk);
158543 }
158544 }
158545
158546 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158547 otaMPrintfExec(p, p->dbMain, "CREATE TABLE \"ota_imp_%w\"(%z)%s",
158548 pIter->zTbl, zSql,
158549 (pIter->eType==OTA_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
158550 );
158551 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158552 }
158553 }
158554
158555 /*
158556 ** Prepare a statement used to insert rows into the "ota_tmp_xxx" table.
158557 ** Specifically a statement of the form:
158558 **
158559 ** INSERT INTO ota_tmp_xxx VALUES(?, ?, ? ...);
158560 **
158561 ** The number of bound variables is equal to the number of columns in
158562 ** the target table, plus one (for the ota_control column), plus one more
158563 ** (for the ota_rowid column) if the target table is an implicit IPK or
158564 ** virtual table.
158565 */
158566 static void otaObjIterPrepareTmpInsert(
158567 sqlite3ota *p,
158568 OtaObjIter *pIter,
158569 const char *zCollist,
158570 const char *zOtaRowid
158571 ){
158572 int bOtaRowid = (pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE);
158573 char *zBind = otaObjIterGetBindlist(p, pIter->nTblCol + 1 + bOtaRowid);
158574 if( zBind ){
158575 assert( pIter->pTmpInsert==0 );
158576 p->rc = prepareFreeAndCollectError(
158577 p->dbOta, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
158578 "INSERT INTO %s.'ota_tmp_%q'(ota_control,%s%s) VALUES(%z)",
158579 p->zStateDb, pIter->zTbl, zCollist, zOtaRowid, zBind
158580 ));
158581 }
158582 }
158583
158584 static void otaTmpInsertFunc(
158585 sqlite3_context *pCtx,
158586 int nVal,
158587 sqlite3_value **apVal
158588 ){
158589 sqlite3ota *p = sqlite3_user_data(pCtx);
158590 int rc = SQLITE_OK;
158591 int i;
158592
158593 for(i=0; rc==SQLITE_OK && i<nVal; i++){
158594 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
@@ -158606,17 +158808,17 @@
158606 /*
158607 ** Ensure that the SQLite statement handles required to update the
158608 ** target database object currently indicated by the iterator passed
158609 ** as the second argument are available.
158610 */
158611 static int otaObjIterPrepareAll(
158612 sqlite3ota *p,
158613 OtaObjIter *pIter,
158614 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
158615 ){
158616 assert( pIter->bCleanup==0 );
158617 if( pIter->pSelect==0 && otaObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
158618 const int tnum = pIter->iTnum;
158619 char *zCollist = 0; /* List of indexed columns */
158620 char **pz = &p->zErrmsg;
158621 const char *zIdx = pIter->zIdx;
158622 char *zLimit = 0;
@@ -158632,104 +158834,104 @@
158632 char *zImposterPK = 0; /* Primary key declaration for imposter */
158633 char *zWhere = 0; /* WHERE clause on PK columns */
158634 char *zBind = 0;
158635 int nBind = 0;
158636
158637 assert( pIter->eType!=OTA_PK_VTAB );
158638 zCollist = otaObjIterGetIndexCols(
158639 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
158640 );
158641 zBind = otaObjIterGetBindlist(p, nBind);
158642
158643 /* Create the imposter table used to write to this index. */
158644 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
158645 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
158646 otaMPrintfExec(p, p->dbMain,
158647 "CREATE TABLE \"ota_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
158648 zTbl, zImposterCols, zImposterPK
158649 );
158650 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158651
158652 /* Create the statement to insert index entries */
158653 pIter->nCol = nBind;
158654 if( p->rc==SQLITE_OK ){
158655 p->rc = prepareFreeAndCollectError(
158656 p->dbMain, &pIter->pInsert, &p->zErrmsg,
158657 sqlite3_mprintf("INSERT INTO \"ota_imp_%w\" VALUES(%s)", zTbl, zBind)
158658 );
158659 }
158660
158661 /* And to delete index entries */
158662 if( p->rc==SQLITE_OK ){
158663 p->rc = prepareFreeAndCollectError(
158664 p->dbMain, &pIter->pDelete, &p->zErrmsg,
158665 sqlite3_mprintf("DELETE FROM \"ota_imp_%w\" WHERE %s", zTbl, zWhere)
158666 );
158667 }
158668
158669 /* Create the SELECT statement to read keys in sorted order */
158670 if( p->rc==SQLITE_OK ){
158671 char *zSql;
158672 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158673 zSql = sqlite3_mprintf(
158674 "SELECT %s, ota_control FROM %s.'ota_tmp_%q' ORDER BY %s%s",
158675 zCollist, p->zStateDb, pIter->zTbl,
158676 zCollist, zLimit
158677 );
158678 }else{
158679 zSql = sqlite3_mprintf(
158680 "SELECT %s, ota_control FROM 'data_%q' "
158681 "WHERE typeof(ota_control)='integer' AND ota_control!=1 "
158682 "UNION ALL "
158683 "SELECT %s, ota_control FROM %s.'ota_tmp_%q' "
158684 "ORDER BY %s%s",
158685 zCollist, pIter->zTbl,
158686 zCollist, p->zStateDb, pIter->zTbl,
158687 zCollist, zLimit
158688 );
158689 }
158690 p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz, zSql);
158691 }
158692
158693 sqlite3_free(zImposterCols);
158694 sqlite3_free(zImposterPK);
158695 sqlite3_free(zWhere);
158696 sqlite3_free(zBind);
158697 }else{
158698 int bOtaRowid = (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE);
158699 const char *zTbl = pIter->zTbl; /* Table this step applies to */
158700 const char *zWrite; /* Imposter table name */
158701
158702 char *zBindings = otaObjIterGetBindlist(p, pIter->nTblCol + bOtaRowid);
158703 char *zWhere = otaObjIterGetWhere(p, pIter);
158704 char *zOldlist = otaObjIterGetOldlist(p, pIter, "old");
158705 char *zNewlist = otaObjIterGetOldlist(p, pIter, "new");
158706
158707 zCollist = otaObjIterGetCollist(p, pIter);
158708 pIter->nCol = pIter->nTblCol;
158709
158710 /* Create the SELECT statement to read keys from data_xxx */
158711 if( p->rc==SQLITE_OK ){
158712 p->rc = prepareFreeAndCollectError(p->dbOta, &pIter->pSelect, pz,
158713 sqlite3_mprintf(
158714 "SELECT %s, ota_control%s FROM 'data_%q'%s",
158715 zCollist, (bOtaRowid ? ", ota_rowid" : ""), zTbl, zLimit
158716 )
158717 );
158718 }
158719
158720 /* Create the imposter table or tables (if required). */
158721 otaCreateImposterTable(p, pIter);
158722 otaCreateImposterTable2(p, pIter);
158723 zWrite = (pIter->eType==OTA_PK_VTAB ? "" : "ota_imp_");
158724
158725 /* Create the INSERT statement to write to the target PK b-tree */
158726 if( p->rc==SQLITE_OK ){
158727 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
158728 sqlite3_mprintf(
158729 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
158730 zWrite, zTbl, zCollist, (bOtaRowid ? ", _rowid_" : ""), zBindings
158731 )
158732 );
158733 }
158734
158735 /* Create the DELETE statement to write to the target PK b-tree */
@@ -158740,55 +158942,55 @@
158740 )
158741 );
158742 }
158743
158744 if( pIter->abIndexed ){
158745 const char *zOtaRowid = "";
158746 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158747 zOtaRowid = ", ota_rowid";
158748 }
158749
158750 /* Create the ota_tmp_xxx table and the triggers to populate it. */
158751 otaMPrintfExec(p, p->dbOta,
158752 "CREATE TABLE IF NOT EXISTS %s.'ota_tmp_%q' AS "
158753 "SELECT *%s FROM 'data_%q' WHERE 0;"
158754 , p->zStateDb
158755 , zTbl, (pIter->eType==OTA_PK_EXTERNAL ? ", 0 AS ota_rowid" : "")
158756 , zTbl
158757 );
158758
158759 otaMPrintfExec(p, p->dbMain,
158760 "CREATE TEMP TRIGGER ota_delete_tr BEFORE DELETE ON \"%s%w\" "
158761 "BEGIN "
158762 " SELECT ota_tmp_insert(2, %s);"
158763 "END;"
158764
158765 "CREATE TEMP TRIGGER ota_update1_tr BEFORE UPDATE ON \"%s%w\" "
158766 "BEGIN "
158767 " SELECT ota_tmp_insert(2, %s);"
158768 "END;"
158769
158770 "CREATE TEMP TRIGGER ota_update2_tr AFTER UPDATE ON \"%s%w\" "
158771 "BEGIN "
158772 " SELECT ota_tmp_insert(3, %s);"
158773 "END;",
158774 zWrite, zTbl, zOldlist,
158775 zWrite, zTbl, zOldlist,
158776 zWrite, zTbl, zNewlist
158777 );
158778
158779 if( pIter->eType==OTA_PK_EXTERNAL || pIter->eType==OTA_PK_NONE ){
158780 otaMPrintfExec(p, p->dbMain,
158781 "CREATE TEMP TRIGGER ota_insert_tr AFTER INSERT ON \"%s%w\" "
158782 "BEGIN "
158783 " SELECT ota_tmp_insert(0, %s);"
158784 "END;",
158785 zWrite, zTbl, zNewlist
158786 );
158787 }
158788
158789 otaObjIterPrepareTmpInsert(p, pIter, zCollist, zOtaRowid);
158790 }
158791
158792 sqlite3_free(zWhere);
158793 sqlite3_free(zOldlist);
158794 sqlite3_free(zNewlist);
@@ -158803,68 +159005,68 @@
158803
158804 /*
158805 ** Set output variable *ppStmt to point to an UPDATE statement that may
158806 ** be used to update the imposter table for the main table b-tree of the
158807 ** table object that pIter currently points to, assuming that the
158808 ** ota_control column of the data_xyz table contains zMask.
158809 **
158810 ** If the zMask string does not specify any columns to update, then this
158811 ** is not an error. Output variable *ppStmt is set to NULL in this case.
158812 */
158813 static int otaGetUpdateStmt(
158814 sqlite3ota *p, /* OTA handle */
158815 OtaObjIter *pIter, /* Object iterator */
158816 const char *zMask, /* ota_control value ('x.x.') */
158817 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
158818 ){
158819 OtaUpdateStmt **pp;
158820 OtaUpdateStmt *pUp = 0;
158821 int nUp = 0;
158822
158823 /* In case an error occurs */
158824 *ppStmt = 0;
158825
158826 /* Search for an existing statement. If one is found, shift it to the front
158827 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
158828 ** to the number of statements currently in the cache and pUp to the
158829 ** last object in the list. */
158830 for(pp=&pIter->pOtaUpdate; *pp; pp=&((*pp)->pNext)){
158831 pUp = *pp;
158832 if( strcmp(pUp->zMask, zMask)==0 ){
158833 *pp = pUp->pNext;
158834 pUp->pNext = pIter->pOtaUpdate;
158835 pIter->pOtaUpdate = pUp;
158836 *ppStmt = pUp->pUpdate;
158837 return SQLITE_OK;
158838 }
158839 nUp++;
158840 }
158841 assert( pUp==0 || pUp->pNext==0 );
158842
158843 if( nUp>=SQLITE_OTA_UPDATE_CACHESIZE ){
158844 for(pp=&pIter->pOtaUpdate; *pp!=pUp; pp=&((*pp)->pNext));
158845 *pp = 0;
158846 sqlite3_finalize(pUp->pUpdate);
158847 pUp->pUpdate = 0;
158848 }else{
158849 pUp = (OtaUpdateStmt*)otaMalloc(p, sizeof(OtaUpdateStmt)+pIter->nTblCol+1);
158850 }
158851
158852 if( pUp ){
158853 char *zWhere = otaObjIterGetWhere(p, pIter);
158854 char *zSet = otaObjIterGetSetlist(p, pIter, zMask);
158855 char *zUpdate = 0;
158856
158857 pUp->zMask = (char*)&pUp[1];
158858 memcpy(pUp->zMask, zMask, pIter->nTblCol);
158859 pUp->pNext = pIter->pOtaUpdate;
158860 pIter->pOtaUpdate = pUp;
158861
158862 if( zSet ){
158863 const char *zPrefix = "";
158864
158865 if( pIter->eType!=OTA_PK_VTAB ) zPrefix = "ota_imp_";
158866 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
158867 zPrefix, pIter->zTbl, zSet, zWhere
158868 );
158869 p->rc = prepareFreeAndCollectError(
158870 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
@@ -158876,11 +159078,11 @@
158876 }
158877
158878 return p->rc;
158879 }
158880
158881 static sqlite3 *otaOpenDbhandle(sqlite3ota *p, const char *zName){
158882 sqlite3 *db = 0;
158883 if( p->rc==SQLITE_OK ){
158884 const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
158885 p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
158886 if( p->rc ){
@@ -158891,51 +159093,51 @@
158891 }
158892 return db;
158893 }
158894
158895 /*
158896 ** Open the database handle and attach the OTA database as "ota". If an
158897 ** error occurs, leave an error code and message in the OTA handle.
158898 */
158899 static void otaOpenDatabase(sqlite3ota *p){
158900 assert( p->rc==SQLITE_OK );
158901 assert( p->dbMain==0 && p->dbOta==0 );
158902
158903 p->eStage = 0;
158904 p->dbMain = otaOpenDbhandle(p, p->zTarget);
158905 p->dbOta = otaOpenDbhandle(p, p->zOta);
158906
158907 /* If using separate OTA and state databases, attach the state database to
158908 ** the OTA db handle now. */
158909 if( p->zState ){
158910 otaMPrintfExec(p, p->dbOta, "ATTACH %Q AS stat", p->zState);
158911 memcpy(p->zStateDb, "stat", 4);
158912 }else{
158913 memcpy(p->zStateDb, "main", 4);
158914 }
158915
158916 if( p->rc==SQLITE_OK ){
158917 p->rc = sqlite3_create_function(p->dbMain,
158918 "ota_tmp_insert", -1, SQLITE_UTF8, (void*)p, otaTmpInsertFunc, 0, 0
158919 );
158920 }
158921
158922 if( p->rc==SQLITE_OK ){
158923 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
158924 }
158925 otaMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
158926
158927 /* Mark the database file just opened as an OTA target database. If
158928 ** this call returns SQLITE_NOTFOUND, then the OTA vfs is not in use.
158929 ** This is an error. */
158930 if( p->rc==SQLITE_OK ){
158931 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_OTA, (void*)p);
158932 }
158933
158934 if( p->rc==SQLITE_NOTFOUND ){
158935 p->rc = SQLITE_ERROR;
158936 p->zErrmsg = sqlite3_mprintf("ota vfs not found");
158937 }
158938 }
158939
158940 /*
158941 ** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
@@ -158955,11 +159157,11 @@
158955 ** test.db-journal => test.nal
158956 ** test.db-wal => test.wal
158957 ** test.db-shm => test.shm
158958 ** test.db-mj7f3319fa => test.9fa
158959 */
158960 static void otaFileSuffix3(const char *zBase, char *z){
158961 #ifdef SQLITE_ENABLE_8_3_NAMES
158962 #if SQLITE_ENABLE_8_3_NAMES<2
158963 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
158964 #endif
158965 {
@@ -158976,11 +159178,11 @@
158976 ** as a 64-bit integer.
158977 **
158978 ** The checksum is store in the first page of xShmMap memory as an 8-byte
158979 ** blob starting at byte offset 40.
158980 */
158981 static i64 otaShmChecksum(sqlite3ota *p){
158982 i64 iRet = 0;
158983 if( p->rc==SQLITE_OK ){
158984 sqlite3_file *pDb = p->pTargetFd->pReal;
158985 u32 volatile *ptr;
158986 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
@@ -158993,23 +159195,23 @@
158993
158994 /*
158995 ** This function is called as part of initializing or reinitializing an
158996 ** incremental checkpoint.
158997 **
158998 ** It populates the sqlite3ota.aFrame[] array with the set of
158999 ** (wal frame -> db page) copy operations required to checkpoint the
159000 ** current wal file, and obtains the set of shm locks required to safely
159001 ** perform the copy operations directly on the file-system.
159002 **
159003 ** If argument pState is not NULL, then the incremental checkpoint is
159004 ** being resumed. In this case, if the checksum of the wal-index-header
159005 ** following recovery is not the same as the checksum saved in the OtaState
159006 ** object, then the ota handle is set to DONE state. This occurs if some
159007 ** other client appends a transaction to the wal file in the middle of
159008 ** an incremental checkpoint.
159009 */
159010 static void otaSetupCheckpoint(sqlite3ota *p, OtaState *pState){
159011
159012 /* If pState is NULL, then the wal file may not have been opened and
159013 ** recovered. Running a read-statement here to ensure that doing so
159014 ** does not interfere with the "capture" process below. */
159015 if( pState==0 ){
@@ -159018,20 +159220,20 @@
159018 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
159019 }
159020 }
159021
159022 /* Assuming no error has occurred, run a "restart" checkpoint with the
159023 ** sqlite3ota.eStage variable set to CAPTURE. This turns on the following
159024 ** special behaviour in the ota VFS:
159025 **
159026 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
159027 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
159028 ** proceed with running a passive checkpoint instead of failing).
159029 **
159030 ** * Attempts to read from the *-wal file or write to the database file
159031 ** do not perform any IO. Instead, the frame/page combinations that
159032 ** would be read/written are recorded in the sqlite3ota.aFrame[]
159033 ** array.
159034 **
159035 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
159036 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
159037 ** no-ops. These locks will not be released until the connection
@@ -159047,76 +159249,76 @@
159047 ** data from the wal file into the database file according to the
159048 ** contents of aFrame[].
159049 */
159050 if( p->rc==SQLITE_OK ){
159051 int rc2;
159052 p->eStage = OTA_STAGE_CAPTURE;
159053 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
159054 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
159055 }
159056
159057 if( p->rc==SQLITE_OK ){
159058 p->eStage = OTA_STAGE_CKPT;
159059 p->nStep = (pState ? pState->nRow : 0);
159060 p->aBuf = otaMalloc(p, p->pgsz);
159061 p->iWalCksum = otaShmChecksum(p);
159062 }
159063
159064 if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
159065 p->rc = SQLITE_DONE;
159066 p->eStage = OTA_STAGE_DONE;
159067 }
159068 }
159069
159070 /*
159071 ** Called when iAmt bytes are read from offset iOff of the wal file while
159072 ** the ota object is in capture mode. Record the frame number of the frame
159073 ** being read in the aFrame[] array.
159074 */
159075 static int otaCaptureWalRead(sqlite3ota *pOta, i64 iOff, int iAmt){
159076 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
159077 u32 iFrame;
159078
159079 if( pOta->mLock!=mReq ){
159080 pOta->rc = SQLITE_BUSY;
159081 return SQLITE_INTERNAL;
159082 }
159083
159084 pOta->pgsz = iAmt;
159085 if( pOta->nFrame==pOta->nFrameAlloc ){
159086 int nNew = (pOta->nFrameAlloc ? pOta->nFrameAlloc : 64) * 2;
159087 OtaFrame *aNew;
159088 aNew = (OtaFrame*)sqlite3_realloc(pOta->aFrame, nNew * sizeof(OtaFrame));
159089 if( aNew==0 ) return SQLITE_NOMEM;
159090 pOta->aFrame = aNew;
159091 pOta->nFrameAlloc = nNew;
159092 }
159093
159094 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
159095 if( pOta->iMaxFrame<iFrame ) pOta->iMaxFrame = iFrame;
159096 pOta->aFrame[pOta->nFrame].iWalFrame = iFrame;
159097 pOta->aFrame[pOta->nFrame].iDbPage = 0;
159098 pOta->nFrame++;
159099 return SQLITE_OK;
159100 }
159101
159102 /*
159103 ** Called when a page of data is written to offset iOff of the database
159104 ** file while the ota handle is in capture mode. Record the page number
159105 ** of the page being written in the aFrame[] array.
159106 */
159107 static int otaCaptureDbWrite(sqlite3ota *pOta, i64 iOff){
159108 pOta->aFrame[pOta->nFrame-1].iDbPage = (u32)(iOff / pOta->pgsz) + 1;
159109 return SQLITE_OK;
159110 }
159111
159112 /*
159113 ** This is called as part of an incremental checkpoint operation. Copy
159114 ** a single frame of data from the wal file into the database file, as
159115 ** indicated by the OtaFrame object.
159116 */
159117 static void otaCheckpointFrame(sqlite3ota *p, OtaFrame *pFrame){
159118 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
159119 sqlite3_file *pDb = p->pTargetFd->pReal;
159120 i64 iOff;
159121
159122 assert( p->rc==SQLITE_OK );
@@ -159130,33 +159332,33 @@
159130
159131
159132 /*
159133 ** Take an EXCLUSIVE lock on the database file.
159134 */
159135 static void otaLockDatabase(sqlite3ota *p){
159136 sqlite3_file *pReal = p->pTargetFd->pReal;
159137 assert( p->rc==SQLITE_OK );
159138 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
159139 if( p->rc==SQLITE_OK ){
159140 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
159141 }
159142 }
159143
159144 /*
159145 ** The OTA handle is currently in OTA_STAGE_OAL state, with a SHARED lock
159146 ** on the database file. This proc moves the *-oal file to the *-wal path,
159147 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
159148 ** If an error occurs, leave an error code and error message in the ota
159149 ** handle.
159150 */
159151 static void otaMoveOalFile(sqlite3ota *p){
159152 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
159153
159154 char *zWal = sqlite3_mprintf("%s-wal", zBase);
159155 char *zOal = sqlite3_mprintf("%s-oal", zBase);
159156
159157 assert( p->eStage==OTA_STAGE_MOVE );
159158 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159159 if( zWal==0 || zOal==0 ){
159160 p->rc = SQLITE_NOMEM;
159161 }else{
159162 /* Move the *-oal file to *-wal. At this point connection p->db is
@@ -159164,25 +159366,25 @@
159164 ** in WAL mode). So no other connection may be writing the db.
159165 **
159166 ** In order to ensure that there are no database readers, an EXCLUSIVE
159167 ** lock is obtained here before the *-oal is moved to *-wal.
159168 */
159169 otaLockDatabase(p);
159170 if( p->rc==SQLITE_OK ){
159171 otaFileSuffix3(zBase, zWal);
159172 otaFileSuffix3(zBase, zOal);
159173
159174 /* Re-open the databases. */
159175 otaObjIterFinalize(&p->objiter);
159176 sqlite3_close(p->dbMain);
159177 sqlite3_close(p->dbOta);
159178 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
159179 if( p->rc==SQLITE_OK ){
159180 p->dbMain = 0;
159181 p->dbOta = 0;
159182 otaOpenDatabase(p);
159183 otaSetupCheckpoint(p, 0);
159184 }
159185 }
159186 }
159187
159188 sqlite3_free(zWal);
@@ -159193,36 +159395,36 @@
159193 ** The SELECT statement iterating through the keys for the current object
159194 ** (p->objiter.pSelect) currently points to a valid row. This function
159195 ** determines the type of operation requested by this row and returns
159196 ** one of the following values to indicate the result:
159197 **
159198 ** * OTA_INSERT
159199 ** * OTA_DELETE
159200 ** * OTA_IDX_DELETE
159201 ** * OTA_UPDATE
159202 **
159203 ** If OTA_UPDATE is returned, then output variable *pzMask is set to
159204 ** point to the text value indicating the columns to update.
159205 **
159206 ** If the ota_control field contains an invalid value, an error code and
159207 ** message are left in the OTA handle and zero returned.
159208 */
159209 static int otaStepType(sqlite3ota *p, const char **pzMask){
159210 int iCol = p->objiter.nCol; /* Index of ota_control column */
159211 int res = 0; /* Return value */
159212
159213 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
159214 case SQLITE_INTEGER: {
159215 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
159216 if( iVal==0 ){
159217 res = OTA_INSERT;
159218 }else if( iVal==1 ){
159219 res = OTA_DELETE;
159220 }else if( iVal==2 ){
159221 res = OTA_IDX_DELETE;
159222 }else if( iVal==3 ){
159223 res = OTA_IDX_INSERT;
159224 }
159225 break;
159226 }
159227
159228 case SQLITE_TEXT: {
@@ -159230,21 +159432,21 @@
159230 if( z==0 ){
159231 p->rc = SQLITE_NOMEM;
159232 }else{
159233 *pzMask = (const char*)z;
159234 }
159235 res = OTA_UPDATE;
159236
159237 break;
159238 }
159239
159240 default:
159241 break;
159242 }
159243
159244 if( res==0 ){
159245 otaBadControlError(p);
159246 }
159247 return res;
159248 }
159249
159250 #ifdef SQLITE_DEBUG
@@ -159258,82 +159460,82 @@
159258 #else
159259 # define assertColumnName(x,y,z)
159260 #endif
159261
159262 /*
159263 ** This function does the work for an sqlite3ota_step() call.
159264 **
159265 ** The object-iterator (p->objiter) currently points to a valid object,
159266 ** and the input cursor (p->objiter.pSelect) currently points to a valid
159267 ** input row. Perform whatever processing is required and return.
159268 **
159269 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
159270 ** and message is left in the OTA handle and a copy of the error code
159271 ** returned.
159272 */
159273 static int otaStep(sqlite3ota *p){
159274 OtaObjIter *pIter = &p->objiter;
159275 const char *zMask = 0;
159276 int i;
159277 int eType = otaStepType(p, &zMask);
159278
159279 if( eType ){
159280 assert( eType!=OTA_UPDATE || pIter->zIdx==0 );
159281
159282 if( pIter->zIdx==0 && eType==OTA_IDX_DELETE ){
159283 otaBadControlError(p);
159284 }
159285 else if(
159286 eType==OTA_INSERT
159287 || eType==OTA_DELETE
159288 || eType==OTA_IDX_DELETE
159289 || eType==OTA_IDX_INSERT
159290 ){
159291 sqlite3_value *pVal;
159292 sqlite3_stmt *pWriter;
159293
159294 assert( eType!=OTA_UPDATE );
159295 assert( eType!=OTA_DELETE || pIter->zIdx==0 );
159296
159297 if( eType==OTA_IDX_DELETE || eType==OTA_DELETE ){
159298 pWriter = pIter->pDelete;
159299 }else{
159300 pWriter = pIter->pInsert;
159301 }
159302
159303 for(i=0; i<pIter->nCol; i++){
159304 /* If this is an INSERT into a table b-tree and the table has an
159305 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
159306 ** to write a NULL into the IPK column. That is not permitted. */
159307 if( eType==OTA_INSERT
159308 && pIter->zIdx==0 && pIter->eType==OTA_PK_IPK && pIter->abTblPk[i]
159309 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
159310 ){
159311 p->rc = SQLITE_MISMATCH;
159312 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
159313 goto step_out;
159314 }
159315
159316 if( eType==OTA_DELETE && pIter->abTblPk[i]==0 ){
159317 continue;
159318 }
159319
159320 pVal = sqlite3_column_value(pIter->pSelect, i);
159321 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
159322 if( p->rc ) goto step_out;
159323 }
159324 if( pIter->zIdx==0
159325 && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
159326 ){
159327 /* For a virtual table, or a table with no primary key, the
159328 ** SELECT statement is:
159329 **
159330 ** SELECT <cols>, ota_control, ota_rowid FROM ....
159331 **
159332 ** Hence column_value(pIter->nCol+1).
159333 */
159334 assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
159335 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159336 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
159337 }
159338 if( p->rc==SQLITE_OK ){
159339 sqlite3_step(pWriter);
@@ -159340,25 +159542,25 @@
159340 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
159341 }
159342 }else{
159343 sqlite3_value *pVal;
159344 sqlite3_stmt *pUpdate = 0;
159345 assert( eType==OTA_UPDATE );
159346 otaGetUpdateStmt(p, pIter, zMask, &pUpdate);
159347 if( pUpdate ){
159348 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
159349 char c = zMask[pIter->aiSrcOrder[i]];
159350 pVal = sqlite3_column_value(pIter->pSelect, i);
159351 if( pIter->abTblPk[i] || c=='x' || c=='d' ){
159352 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
159353 }
159354 }
159355 if( p->rc==SQLITE_OK
159356 && (pIter->eType==OTA_PK_VTAB || pIter->eType==OTA_PK_NONE)
159357 ){
159358 /* Bind the ota_rowid value to column _rowid_ */
159359 assertColumnName(pIter->pSelect, pIter->nCol+1, "ota_rowid");
159360 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159361 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
159362 }
159363 if( p->rc==SQLITE_OK ){
159364 sqlite3_step(pUpdate);
@@ -159373,11 +159575,11 @@
159373 }
159374
159375 /*
159376 ** Increment the schema cookie of the main database opened by p->dbMain.
159377 */
159378 static void otaIncrSchemaCookie(sqlite3ota *p){
159379 if( p->rc==SQLITE_OK ){
159380 int iCookie = 1000000;
159381 sqlite3_stmt *pStmt;
159382
159383 p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
@@ -159390,49 +159592,49 @@
159390 ** statement reads is page 1, which is guaranteed to be in the cache.
159391 ** And no memory allocations are required. */
159392 if( SQLITE_ROW==sqlite3_step(pStmt) ){
159393 iCookie = sqlite3_column_int(pStmt, 0);
159394 }
159395 otaFinalize(p, pStmt);
159396 }
159397 if( p->rc==SQLITE_OK ){
159398 otaMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
159399 }
159400 }
159401 }
159402
159403 /*
159404 ** Update the contents of the ota_state table within the ota database. The
159405 ** value stored in the OTA_STATE_STAGE column is eStage. All other values
159406 ** are determined by inspecting the ota handle passed as the first argument.
159407 */
159408 static void otaSaveState(sqlite3ota *p, int eStage){
159409 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
159410 sqlite3_stmt *pInsert = 0;
159411 int rc;
159412
159413 assert( p->zErrmsg==0 );
159414 rc = prepareFreeAndCollectError(p->dbOta, &pInsert, &p->zErrmsg,
159415 sqlite3_mprintf(
159416 "INSERT OR REPLACE INTO %s.ota_state(k, v) VALUES "
159417 "(%d, %d), "
159418 "(%d, %Q), "
159419 "(%d, %Q), "
159420 "(%d, %d), "
159421 "(%d, %d), "
159422 "(%d, %lld), "
159423 "(%d, %lld), "
159424 "(%d, %lld) ",
159425 p->zStateDb,
159426 OTA_STATE_STAGE, eStage,
159427 OTA_STATE_TBL, p->objiter.zTbl,
159428 OTA_STATE_IDX, p->objiter.zIdx,
159429 OTA_STATE_ROW, p->nStep,
159430 OTA_STATE_PROGRESS, p->nProgress,
159431 OTA_STATE_CKPT, p->iWalCksum,
159432 OTA_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
159433 OTA_STATE_OALSZ, p->iOalSz
159434 )
159435 );
159436 assert( pInsert==0 || rc==SQLITE_OK );
159437
159438 if( rc==SQLITE_OK ){
@@ -159443,71 +159645,71 @@
159443 }
159444 }
159445
159446
159447 /*
159448 ** Step the OTA object.
159449 */
159450 SQLITE_API int SQLITE_STDCALL sqlite3ota_step(sqlite3ota *p){
159451 if( p ){
159452 switch( p->eStage ){
159453 case OTA_STAGE_OAL: {
159454 OtaObjIter *pIter = &p->objiter;
159455 while( p->rc==SQLITE_OK && pIter->zTbl ){
159456
159457 if( pIter->bCleanup ){
159458 /* Clean up the ota_tmp_xxx table for the previous table. It
159459 ** cannot be dropped as there are currently active SQL statements.
159460 ** But the contents can be deleted. */
159461 if( pIter->abIndexed ){
159462 otaMPrintfExec(p, p->dbOta,
159463 "DELETE FROM %s.'ota_tmp_%q'", p->zStateDb, pIter->zTbl
159464 );
159465 }
159466 }else{
159467 otaObjIterPrepareAll(p, pIter, 0);
159468
159469 /* Advance to the next row to process. */
159470 if( p->rc==SQLITE_OK ){
159471 int rc = sqlite3_step(pIter->pSelect);
159472 if( rc==SQLITE_ROW ){
159473 p->nProgress++;
159474 p->nStep++;
159475 return otaStep(p);
159476 }
159477 p->rc = sqlite3_reset(pIter->pSelect);
159478 p->nStep = 0;
159479 }
159480 }
159481
159482 otaObjIterNext(p, pIter);
159483 }
159484
159485 if( p->rc==SQLITE_OK ){
159486 assert( pIter->zTbl==0 );
159487 otaSaveState(p, OTA_STAGE_MOVE);
159488 otaIncrSchemaCookie(p);
159489 if( p->rc==SQLITE_OK ){
159490 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
159491 }
159492 if( p->rc==SQLITE_OK ){
159493 p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
159494 }
159495 p->eStage = OTA_STAGE_MOVE;
159496 }
159497 break;
159498 }
159499
159500 case OTA_STAGE_MOVE: {
159501 if( p->rc==SQLITE_OK ){
159502 otaMoveOalFile(p);
159503 p->nProgress++;
159504 }
159505 break;
159506 }
159507
159508 case OTA_STAGE_CKPT: {
159509 if( p->rc==SQLITE_OK ){
159510 if( p->nStep>=p->nFrame ){
159511 sqlite3_file *pDb = p->pTargetFd->pReal;
159512
159513 /* Sync the db file */
@@ -159521,16 +159723,16 @@
159521 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
159522 }
159523 }
159524
159525 if( p->rc==SQLITE_OK ){
159526 p->eStage = OTA_STAGE_DONE;
159527 p->rc = SQLITE_DONE;
159528 }
159529 }else{
159530 OtaFrame *pFrame = &p->aFrame[p->nStep];
159531 otaCheckpointFrame(p, pFrame);
159532 p->nStep++;
159533 }
159534 p->nProgress++;
159535 }
159536 break;
@@ -159544,78 +159746,78 @@
159544 return SQLITE_NOMEM;
159545 }
159546 }
159547
159548 /*
159549 ** Free an OtaState object allocated by otaLoadState().
159550 */
159551 static void otaFreeState(OtaState *p){
159552 if( p ){
159553 sqlite3_free(p->zTbl);
159554 sqlite3_free(p->zIdx);
159555 sqlite3_free(p);
159556 }
159557 }
159558
159559 /*
159560 ** Allocate an OtaState object and load the contents of the ota_state
159561 ** table into it. Return a pointer to the new object. It is the
159562 ** responsibility of the caller to eventually free the object using
159563 ** sqlite3_free().
159564 **
159565 ** If an error occurs, leave an error code and message in the ota handle
159566 ** and return NULL.
159567 */
159568 static OtaState *otaLoadState(sqlite3ota *p){
159569 OtaState *pRet = 0;
159570 sqlite3_stmt *pStmt = 0;
159571 int rc;
159572 int rc2;
159573
159574 pRet = (OtaState*)otaMalloc(p, sizeof(OtaState));
159575 if( pRet==0 ) return 0;
159576
159577 rc = prepareFreeAndCollectError(p->dbOta, &pStmt, &p->zErrmsg,
159578 sqlite3_mprintf("SELECT k, v FROM %s.ota_state", p->zStateDb)
159579 );
159580 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
159581 switch( sqlite3_column_int(pStmt, 0) ){
159582 case OTA_STATE_STAGE:
159583 pRet->eStage = sqlite3_column_int(pStmt, 1);
159584 if( pRet->eStage!=OTA_STAGE_OAL
159585 && pRet->eStage!=OTA_STAGE_MOVE
159586 && pRet->eStage!=OTA_STAGE_CKPT
159587 ){
159588 p->rc = SQLITE_CORRUPT;
159589 }
159590 break;
159591
159592 case OTA_STATE_TBL:
159593 pRet->zTbl = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159594 break;
159595
159596 case OTA_STATE_IDX:
159597 pRet->zIdx = otaStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159598 break;
159599
159600 case OTA_STATE_ROW:
159601 pRet->nRow = sqlite3_column_int(pStmt, 1);
159602 break;
159603
159604 case OTA_STATE_PROGRESS:
159605 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
159606 break;
159607
159608 case OTA_STATE_CKPT:
159609 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
159610 break;
159611
159612 case OTA_STATE_COOKIE:
159613 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
159614 break;
159615
159616 case OTA_STATE_OALSZ:
159617 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
159618 break;
159619
159620 default:
159621 rc = SQLITE_CORRUPT;
@@ -159632,142 +159834,142 @@
159632 /*
159633 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
159634 ** otherwise. Either or both argument may be NULL. Two NULL values are
159635 ** considered equal, and NULL is considered distinct from all other values.
159636 */
159637 static int otaStrCompare(const char *z1, const char *z2){
159638 if( z1==0 && z2==0 ) return 0;
159639 if( z1==0 || z2==0 ) return 1;
159640 return (sqlite3_stricmp(z1, z2)!=0);
159641 }
159642
159643 /*
159644 ** This function is called as part of sqlite3ota_open() when initializing
159645 ** an ota handle in OAL stage. If the ota update has not started (i.e.
159646 ** the ota_state table was empty) it is a no-op. Otherwise, it arranges
159647 ** things so that the next call to sqlite3ota_step() continues on from
159648 ** where the previous ota handle left off.
159649 **
159650 ** If an error occurs, an error code and error message are left in the
159651 ** ota handle passed as the first argument.
159652 */
159653 static void otaSetupOal(sqlite3ota *p, OtaState *pState){
159654 assert( p->rc==SQLITE_OK );
159655 if( pState->zTbl ){
159656 OtaObjIter *pIter = &p->objiter;
159657 int rc = SQLITE_OK;
159658
159659 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
159660 || otaStrCompare(pIter->zIdx, pState->zIdx)
159661 || otaStrCompare(pIter->zTbl, pState->zTbl)
159662 )){
159663 rc = otaObjIterNext(p, pIter);
159664 }
159665
159666 if( rc==SQLITE_OK && !pIter->zTbl ){
159667 rc = SQLITE_ERROR;
159668 p->zErrmsg = sqlite3_mprintf("ota_state mismatch error");
159669 }
159670
159671 if( rc==SQLITE_OK ){
159672 p->nStep = pState->nRow;
159673 rc = otaObjIterPrepareAll(p, &p->objiter, p->nStep);
159674 }
159675
159676 p->rc = rc;
159677 }
159678 }
159679
159680 /*
159681 ** If there is a "*-oal" file in the file-system corresponding to the
159682 ** target database in the file-system, delete it. If an error occurs,
159683 ** leave an error code and error message in the ota handle.
159684 */
159685 static void otaDeleteOalFile(sqlite3ota *p){
159686 char *zOal = sqlite3_mprintf("%s-oal", p->zTarget);
159687 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159688 unlink(zOal);
159689 sqlite3_free(zOal);
159690 }
159691
159692 /*
159693 ** Allocate a private ota VFS for the ota handle passed as the only
159694 ** argument. This VFS will be used unless the call to sqlite3ota_open()
159695 ** specified a URI with a vfs=? option in place of a target database
159696 ** file name.
159697 */
159698 static void otaCreateVfs(sqlite3ota *p){
159699 int rnd;
159700 char zRnd[64];
159701
159702 assert( p->rc==SQLITE_OK );
159703 sqlite3_randomness(sizeof(int), (void*)&rnd);
159704 sprintf(zRnd, "ota_vfs_%d", rnd);
159705 p->rc = sqlite3ota_create_vfs(zRnd, 0);
159706 if( p->rc==SQLITE_OK ){
159707 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
159708 assert( pVfs );
159709 p->zVfsName = pVfs->zName;
159710 }
159711 }
159712
159713 /*
159714 ** Destroy the private VFS created for the ota handle passed as the only
159715 ** argument by an earlier call to otaCreateVfs().
159716 */
159717 static void otaDeleteVfs(sqlite3ota *p){
159718 if( p->zVfsName ){
159719 sqlite3ota_destroy_vfs(p->zVfsName);
159720 p->zVfsName = 0;
159721 }
159722 }
159723
159724 /*
159725 ** Open and return a new OTA handle.
159726 */
159727 SQLITE_API sqlite3ota *SQLITE_STDCALL sqlite3ota_open(
159728 const char *zTarget,
159729 const char *zOta,
159730 const char *zState
159731 ){
159732 sqlite3ota *p;
159733 int nTarget = strlen(zTarget);
159734 int nOta = strlen(zOta);
159735 int nState = zState ? strlen(zState) : 0;
159736
159737 p = (sqlite3ota*)sqlite3_malloc(sizeof(sqlite3ota)+nTarget+1+nOta+1+nState+1);
159738 if( p ){
159739 OtaState *pState = 0;
159740
159741 /* Create the custom VFS. */
159742 memset(p, 0, sizeof(sqlite3ota));
159743 otaCreateVfs(p);
159744
159745 /* Open the target database */
159746 if( p->rc==SQLITE_OK ){
159747 p->zTarget = (char*)&p[1];
159748 memcpy(p->zTarget, zTarget, nTarget+1);
159749 p->zOta = &p->zTarget[nTarget+1];
159750 memcpy(p->zOta, zOta, nOta+1);
159751 if( zState ){
159752 p->zState = &p->zOta[nOta+1];
159753 memcpy(p->zState, zState, nState+1);
159754 }
159755 otaOpenDatabase(p);
159756 }
159757
159758 /* If it has not already been created, create the ota_state table */
159759 otaMPrintfExec(p, p->dbOta, OTA_CREATE_STATE, p->zStateDb);
159760
159761 if( p->rc==SQLITE_OK ){
159762 pState = otaLoadState(p);
159763 assert( pState || p->rc!=SQLITE_OK );
159764 if( p->rc==SQLITE_OK ){
159765
159766 if( pState->eStage==0 ){
159767 otaDeleteOalFile(p);
159768 p->eStage = OTA_STAGE_OAL;
159769 }else{
159770 p->eStage = pState->eStage;
159771 }
159772 p->nProgress = pState->nProgress;
159773 p->iOalSz = pState->iOalSz;
@@ -159774,97 +159976,97 @@
159774 }
159775 }
159776 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
159777
159778 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
159779 if( p->eStage==OTA_STAGE_OAL ){
159780 p->rc = SQLITE_ERROR;
159781 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
159782 }else if( p->eStage==OTA_STAGE_MOVE ){
159783 p->eStage = OTA_STAGE_CKPT;
159784 p->nStep = 0;
159785 }
159786 }
159787
159788 if( p->rc==SQLITE_OK
159789 && (p->eStage==OTA_STAGE_OAL || p->eStage==OTA_STAGE_MOVE)
159790 && pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
159791 ){
159792 /* At this point (pTargetFd->iCookie) contains the value of the
159793 ** change-counter cookie (the thing that gets incremented when a
159794 ** transaction is committed in rollback mode) currently stored on
159795 ** page 1 of the database file. */
159796 p->rc = SQLITE_BUSY;
159797 p->zErrmsg = sqlite3_mprintf("database modified during ota update");
159798 }
159799
159800 if( p->rc==SQLITE_OK ){
159801 if( p->eStage==OTA_STAGE_OAL ){
159802
159803 /* Open transactions both databases. The *-oal file is opened or
159804 ** created at this point. */
159805 p->rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
159806 if( p->rc==SQLITE_OK ){
159807 p->rc = sqlite3_exec(p->dbOta, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
159808 }
159809
159810 /* Point the object iterator at the first object */
159811 if( p->rc==SQLITE_OK ){
159812 p->rc = otaObjIterFirst(p, &p->objiter);
159813 }
159814
159815 /* If the OTA database contains no data_xxx tables, declare the OTA
159816 ** update finished. */
159817 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
159818 p->rc = SQLITE_DONE;
159819 }
159820
159821 if( p->rc==SQLITE_OK ){
159822 otaSetupOal(p, pState);
159823 }
159824
159825 }else if( p->eStage==OTA_STAGE_MOVE ){
159826 /* no-op */
159827 }else if( p->eStage==OTA_STAGE_CKPT ){
159828 otaSetupCheckpoint(p, pState);
159829 }else if( p->eStage==OTA_STAGE_DONE ){
159830 p->rc = SQLITE_DONE;
159831 }else{
159832 p->rc = SQLITE_CORRUPT;
159833 }
159834 }
159835
159836 otaFreeState(pState);
159837 }
159838
159839 return p;
159840 }
159841
159842
159843 /*
159844 ** Return the database handle used by pOta.
159845 */
159846 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3ota_db(sqlite3ota *pOta, int bOta){
159847 sqlite3 *db = 0;
159848 if( pOta ){
159849 db = (bOta ? pOta->dbOta : pOta->dbMain);
159850 }
159851 return db;
159852 }
159853
159854
159855 /*
159856 ** If the error code currently stored in the OTA handle is SQLITE_CONSTRAINT,
159857 ** then edit any error message string so as to remove all occurrences of
159858 ** the pattern "ota_imp_[0-9]*".
159859 */
159860 static void otaEditErrmsg(sqlite3ota *p){
159861 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
159862 int i;
159863 int nErrmsg = strlen(p->zErrmsg);
159864 for(i=0; i<(nErrmsg-8); i++){
159865 if( memcmp(&p->zErrmsg[i], "ota_imp_", 8)==0 ){
159866 int nDel = 8;
159867 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
159868 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
159869 nErrmsg -= nDel;
159870 }
@@ -159871,38 +160073,38 @@
159871 }
159872 }
159873 }
159874
159875 /*
159876 ** Close the OTA handle.
159877 */
159878 SQLITE_API int SQLITE_STDCALL sqlite3ota_close(sqlite3ota *p, char **pzErrmsg){
159879 int rc;
159880 if( p ){
159881
159882 /* Commit the transaction to the *-oal file. */
159883 if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
159884 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
159885 }
159886
159887 otaSaveState(p, p->eStage);
159888
159889 if( p->rc==SQLITE_OK && p->eStage==OTA_STAGE_OAL ){
159890 p->rc = sqlite3_exec(p->dbOta, "COMMIT", 0, 0, &p->zErrmsg);
159891 }
159892
159893 /* Close any open statement handles. */
159894 otaObjIterFinalize(&p->objiter);
159895
159896 /* Close the open database handle and VFS object. */
159897 sqlite3_close(p->dbMain);
159898 sqlite3_close(p->dbOta);
159899 otaDeleteVfs(p);
159900 sqlite3_free(p->aBuf);
159901 sqlite3_free(p->aFrame);
159902
159903 otaEditErrmsg(p);
159904 rc = p->rc;
159905 *pzErrmsg = p->zErrmsg;
159906 sqlite3_free(p);
159907 }else{
159908 rc = SQLITE_NOMEM;
@@ -159912,65 +160114,65 @@
159912 }
159913
159914 /*
159915 ** Return the total number of key-value operations (inserts, deletes or
159916 ** updates) that have been performed on the target database since the
159917 ** current OTA update was started.
159918 */
159919 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3ota_progress(sqlite3ota *pOta){
159920 return pOta->nProgress;
159921 }
159922
159923 /**************************************************************************
159924 ** Beginning of OTA VFS shim methods. The VFS shim modifies the behaviour
159925 ** of a standard VFS in the following ways:
159926 **
159927 ** 1. Whenever the first page of a main database file is read or
159928 ** written, the value of the change-counter cookie is stored in
159929 ** ota_file.iCookie. Similarly, the value of the "write-version"
159930 ** database header field is stored in ota_file.iWriteVer. This ensures
159931 ** that the values are always trustworthy within an open transaction.
159932 **
159933 ** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (ota_file.pWalFd)
159934 ** member variable of the associated database file descriptor is set
159935 ** to point to the new file. A mutex protected linked list of all main
159936 ** db fds opened using a particular OTA VFS is maintained at
159937 ** ota_vfs.pMain to facilitate this.
159938 **
159939 ** 3. Using a new file-control "SQLITE_FCNTL_OTA", a main db ota_file
159940 ** object can be marked as the target database of an OTA update. This
159941 ** turns on the following extra special behaviour:
159942 **
159943 ** 3a. If xAccess() is called to check if there exists a *-wal file
159944 ** associated with an OTA target database currently in OTA_STAGE_OAL
159945 ** stage (preparing the *-oal file), the following special handling
159946 ** applies:
159947 **
159948 ** * if the *-wal file does exist, return SQLITE_CANTOPEN. An OTA
159949 ** target database may not be in wal mode already.
159950 **
159951 ** * if the *-wal file does not exist, set the output parameter to
159952 ** non-zero (to tell SQLite that it does exist) anyway.
159953 **
159954 ** Then, when xOpen() is called to open the *-wal file associated with
159955 ** the OTA target in OTA_STAGE_OAL stage, instead of opening the *-wal
159956 ** file, the ota vfs opens the corresponding *-oal file instead.
159957 **
159958 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
159959 ** OTA_STAGE_OAL mode are actually stored in heap memory. This is to
159960 ** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
159961 ** are no-ops on target database files in OTA_STAGE_OAL mode. This is
159962 ** because assert() statements in some VFS implementations fail if
159963 ** xShmLock() is called before xShmMap().
159964 **
159965 ** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
159966 ** mode except OTA_STAGE_DONE (all work completed and checkpointed), it
159967 ** fails with an SQLITE_BUSY error. This is to stop OTA connections
159968 ** from automatically checkpointing a *-wal (or *-oal) file from within
159969 ** sqlite3_close().
159970 **
159971 ** 3d. In OTA_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
159972 ** all xWrite() calls on the target database file perform no IO.
159973 ** Instead the frame and page numbers that would be read and written
159974 ** are recorded. Additionally, successful attempts to obtain exclusive
159975 ** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
159976 ** database file are recorded. xShmLock() calls to unlock the same
@@ -159977,28 +160179,28 @@
159977 ** locks are no-ops (so that once obtained, these locks are never
159978 ** relinquished). Finally, calls to xSync() on the target database
159979 ** file fail with SQLITE_INTERNAL errors.
159980 */
159981
159982 static void otaUnlockShm(ota_file *p){
159983 if( p->pOta ){
159984 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
159985 int i;
159986 for(i=0; i<SQLITE_SHM_NLOCK;i++){
159987 if( (1<<i) & p->pOta->mLock ){
159988 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
159989 }
159990 }
159991 p->pOta->mLock = 0;
159992 }
159993 }
159994
159995 /*
159996 ** Close an ota file.
159997 */
159998 static int otaVfsClose(sqlite3_file *pFile){
159999 ota_file *p = (ota_file*)pFile;
160000 int rc;
160001 int i;
160002
160003 /* Free the contents of the apShm[] array. And the array itself. */
160004 for(i=0; i<p->nShm; i++){
@@ -160007,16 +160209,16 @@
160007 sqlite3_free(p->apShm);
160008 p->apShm = 0;
160009 sqlite3_free(p->zDel);
160010
160011 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160012 ota_file **pp;
160013 sqlite3_mutex_enter(p->pOtaVfs->mutex);
160014 for(pp=&p->pOtaVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
160015 *pp = p->pMainNext;
160016 sqlite3_mutex_leave(p->pOtaVfs->mutex);
160017 otaUnlockShm(p);
160018 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
160019 }
160020
160021 /* Close the underlying file handle */
160022 rc = p->pReal->pMethods->xClose(p->pReal);
@@ -160026,37 +160228,37 @@
160026
160027 /*
160028 ** Read and return an unsigned 32-bit big-endian integer from the buffer
160029 ** passed as the only argument.
160030 */
160031 static u32 otaGetU32(u8 *aBuf){
160032 return ((u32)aBuf[0] << 24)
160033 + ((u32)aBuf[1] << 16)
160034 + ((u32)aBuf[2] << 8)
160035 + ((u32)aBuf[3]);
160036 }
160037
160038 /*
160039 ** Read data from an otaVfs-file.
160040 */
160041 static int otaVfsRead(
160042 sqlite3_file *pFile,
160043 void *zBuf,
160044 int iAmt,
160045 sqlite_int64 iOfst
160046 ){
160047 ota_file *p = (ota_file*)pFile;
160048 sqlite3ota *pOta = p->pOta;
160049 int rc;
160050
160051 if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
160052 assert( p->openFlags & SQLITE_OPEN_WAL );
160053 rc = otaCaptureWalRead(p->pOta, iOfst, iAmt);
160054 }else{
160055 if( pOta && pOta->eStage==OTA_STAGE_OAL
160056 && (p->openFlags & SQLITE_OPEN_WAL)
160057 && iOfst>=pOta->iOalSz
160058 ){
160059 rc = SQLITE_OK;
160060 memset(zBuf, 0, iAmt);
160061 }else{
160062 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
@@ -160063,92 +160265,92 @@
160063 }
160064 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160065 /* These look like magic numbers. But they are stable, as they are part
160066 ** of the definition of the SQLite file format, which may not change. */
160067 u8 *pBuf = (u8*)zBuf;
160068 p->iCookie = otaGetU32(&pBuf[24]);
160069 p->iWriteVer = pBuf[19];
160070 }
160071 }
160072 return rc;
160073 }
160074
160075 /*
160076 ** Write data to an otaVfs-file.
160077 */
160078 static int otaVfsWrite(
160079 sqlite3_file *pFile,
160080 const void *zBuf,
160081 int iAmt,
160082 sqlite_int64 iOfst
160083 ){
160084 ota_file *p = (ota_file*)pFile;
160085 sqlite3ota *pOta = p->pOta;
160086 int rc;
160087
160088 if( pOta && pOta->eStage==OTA_STAGE_CAPTURE ){
160089 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
160090 rc = otaCaptureDbWrite(p->pOta, iOfst);
160091 }else{
160092 if( pOta && pOta->eStage==OTA_STAGE_OAL
160093 && (p->openFlags & SQLITE_OPEN_WAL)
160094 && iOfst>=pOta->iOalSz
160095 ){
160096 pOta->iOalSz = iAmt + iOfst;
160097 }
160098 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
160099 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160100 /* These look like magic numbers. But they are stable, as they are part
160101 ** of the definition of the SQLite file format, which may not change. */
160102 u8 *pBuf = (u8*)zBuf;
160103 p->iCookie = otaGetU32(&pBuf[24]);
160104 p->iWriteVer = pBuf[19];
160105 }
160106 }
160107 return rc;
160108 }
160109
160110 /*
160111 ** Truncate an otaVfs-file.
160112 */
160113 static int otaVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
160114 ota_file *p = (ota_file*)pFile;
160115 return p->pReal->pMethods->xTruncate(p->pReal, size);
160116 }
160117
160118 /*
160119 ** Sync an otaVfs-file.
160120 */
160121 static int otaVfsSync(sqlite3_file *pFile, int flags){
160122 ota_file *p = (ota_file *)pFile;
160123 if( p->pOta && p->pOta->eStage==OTA_STAGE_CAPTURE ){
160124 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160125 return SQLITE_INTERNAL;
160126 }
160127 return SQLITE_OK;
160128 }
160129 return p->pReal->pMethods->xSync(p->pReal, flags);
160130 }
160131
160132 /*
160133 ** Return the current file-size of an otaVfs-file.
160134 */
160135 static int otaVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
160136 ota_file *p = (ota_file *)pFile;
160137 return p->pReal->pMethods->xFileSize(p->pReal, pSize);
160138 }
160139
160140 /*
160141 ** Lock an otaVfs-file.
160142 */
160143 static int otaVfsLock(sqlite3_file *pFile, int eLock){
160144 ota_file *p = (ota_file*)pFile;
160145 sqlite3ota *pOta = p->pOta;
160146 int rc = SQLITE_OK;
160147
160148 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160149 if( pOta && eLock==SQLITE_LOCK_EXCLUSIVE && pOta->eStage!=OTA_STAGE_DONE ){
160150 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
160151 ** prevents it from checkpointing the database from sqlite3_close(). */
160152 rc = SQLITE_BUSY;
160153 }else{
160154 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
@@ -160156,122 +160358,122 @@
160156
160157 return rc;
160158 }
160159
160160 /*
160161 ** Unlock an otaVfs-file.
160162 */
160163 static int otaVfsUnlock(sqlite3_file *pFile, int eLock){
160164 ota_file *p = (ota_file *)pFile;
160165 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
160166 }
160167
160168 /*
160169 ** Check if another file-handle holds a RESERVED lock on an otaVfs-file.
160170 */
160171 static int otaVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
160172 ota_file *p = (ota_file *)pFile;
160173 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
160174 }
160175
160176 /*
160177 ** File control method. For custom operations on an otaVfs-file.
160178 */
160179 static int otaVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
160180 ota_file *p = (ota_file *)pFile;
160181 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
160182 int rc;
160183
160184 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
160185 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
160186 );
160187 if( op==SQLITE_FCNTL_OTA ){
160188 sqlite3ota *pOta = (sqlite3ota*)pArg;
160189
160190 /* First try to find another OTA vfs lower down in the vfs stack. If
160191 ** one is found, this vfs will operate in pass-through mode. The lower
160192 ** level vfs will do the special OTA handling. */
160193 rc = xControl(p->pReal, op, pArg);
160194
160195 if( rc==SQLITE_NOTFOUND ){
160196 /* Now search for a zipvfs instance lower down in the VFS stack. If
160197 ** one is found, this is an error. */
160198 void *dummy = 0;
160199 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
160200 if( rc==SQLITE_OK ){
160201 rc = SQLITE_ERROR;
160202 pOta->zErrmsg = sqlite3_mprintf("ota/zipvfs setup error");
160203 }else if( rc==SQLITE_NOTFOUND ){
160204 pOta->pTargetFd = p;
160205 p->pOta = pOta;
160206 if( p->pWalFd ) p->pWalFd->pOta = pOta;
160207 rc = SQLITE_OK;
160208 }
160209 }
160210 return rc;
160211 }
160212
160213 rc = xControl(p->pReal, op, pArg);
160214 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
160215 ota_vfs *pOtaVfs = p->pOtaVfs;
160216 char *zIn = *(char**)pArg;
160217 char *zOut = sqlite3_mprintf("ota(%s)/%z", pOtaVfs->base.zName, zIn);
160218 *(char**)pArg = zOut;
160219 if( zOut==0 ) rc = SQLITE_NOMEM;
160220 }
160221
160222 return rc;
160223 }
160224
160225 /*
160226 ** Return the sector-size in bytes for an otaVfs-file.
160227 */
160228 static int otaVfsSectorSize(sqlite3_file *pFile){
160229 ota_file *p = (ota_file *)pFile;
160230 return p->pReal->pMethods->xSectorSize(p->pReal);
160231 }
160232
160233 /*
160234 ** Return the device characteristic flags supported by an otaVfs-file.
160235 */
160236 static int otaVfsDeviceCharacteristics(sqlite3_file *pFile){
160237 ota_file *p = (ota_file *)pFile;
160238 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
160239 }
160240
160241 /*
160242 ** Take or release a shared-memory lock.
160243 */
160244 static int otaVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
160245 ota_file *p = (ota_file*)pFile;
160246 sqlite3ota *pOta = p->pOta;
160247 int rc = SQLITE_OK;
160248
160249 #ifdef SQLITE_AMALGAMATION
160250 assert( WAL_CKPT_LOCK==1 );
160251 #endif
160252
160253 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160254 if( pOta && (pOta->eStage==OTA_STAGE_OAL || pOta->eStage==OTA_STAGE_MOVE) ){
160255 /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
160256 ** taking this lock also prevents any checkpoints from occurring.
160257 ** todo: really, it's not clear why this might occur, as
160258 ** wal_autocheckpoint ought to be turned off. */
160259 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
160260 }else{
160261 int bCapture = 0;
160262 if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
160263 && pOta && pOta->eStage==OTA_STAGE_CAPTURE
160264 && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
160265 ){
160266 bCapture = 1;
160267 }
160268
160269 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
160270 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
160271 if( bCapture && rc==SQLITE_OK ){
160272 pOta->mLock |= (1 << ofst);
160273 }
160274 }
160275 }
160276
160277 return rc;
@@ -160278,26 +160480,26 @@
160278 }
160279
160280 /*
160281 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
160282 */
160283 static int otaVfsShmMap(
160284 sqlite3_file *pFile,
160285 int iRegion,
160286 int szRegion,
160287 int isWrite,
160288 void volatile **pp
160289 ){
160290 ota_file *p = (ota_file*)pFile;
160291 int rc = SQLITE_OK;
160292 int eStage = (p->pOta ? p->pOta->eStage : 0);
160293
160294 /* If not in OTA_STAGE_OAL, allow this call to pass through. Or, if this
160295 ** ota is in the OTA_STAGE_OAL state, use heap memory for *-shm space
160296 ** instead of a file on disk. */
160297 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160298 if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
160299 if( iRegion<=p->nShm ){
160300 int nByte = (iRegion+1) * sizeof(char*);
160301 char **apNew = (char**)sqlite3_realloc(p->apShm, nByte);
160302 if( apNew==0 ){
160303 rc = SQLITE_NOMEM;
@@ -160332,29 +160534,29 @@
160332 }
160333
160334 /*
160335 ** Memory barrier.
160336 */
160337 static void otaVfsShmBarrier(sqlite3_file *pFile){
160338 ota_file *p = (ota_file *)pFile;
160339 p->pReal->pMethods->xShmBarrier(p->pReal);
160340 }
160341
160342 /*
160343 ** The xShmUnmap method.
160344 */
160345 static int otaVfsShmUnmap(sqlite3_file *pFile, int delFlag){
160346 ota_file *p = (ota_file*)pFile;
160347 int rc = SQLITE_OK;
160348 int eStage = (p->pOta ? p->pOta->eStage : 0);
160349
160350 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160351 if( eStage==OTA_STAGE_OAL || eStage==OTA_STAGE_MOVE ){
160352 /* no-op */
160353 }else{
160354 /* Release the checkpointer and writer locks */
160355 otaUnlockShm(p);
160356 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
160357 }
160358 return rc;
160359 }
160360
@@ -160362,56 +160564,56 @@
160362 ** Given that zWal points to a buffer containing a wal file name passed to
160363 ** either the xOpen() or xAccess() VFS method, return a pointer to the
160364 ** file-handle opened by the same database connection on the corresponding
160365 ** database file.
160366 */
160367 static ota_file *otaFindMaindb(ota_vfs *pOtaVfs, const char *zWal){
160368 ota_file *pDb;
160369 sqlite3_mutex_enter(pOtaVfs->mutex);
160370 for(pDb=pOtaVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
160371 sqlite3_mutex_leave(pOtaVfs->mutex);
160372 return pDb;
160373 }
160374
160375 /*
160376 ** Open an ota file handle.
160377 */
160378 static int otaVfsOpen(
160379 sqlite3_vfs *pVfs,
160380 const char *zName,
160381 sqlite3_file *pFile,
160382 int flags,
160383 int *pOutFlags
160384 ){
160385 static sqlite3_io_methods otavfs_io_methods = {
160386 2, /* iVersion */
160387 otaVfsClose, /* xClose */
160388 otaVfsRead, /* xRead */
160389 otaVfsWrite, /* xWrite */
160390 otaVfsTruncate, /* xTruncate */
160391 otaVfsSync, /* xSync */
160392 otaVfsFileSize, /* xFileSize */
160393 otaVfsLock, /* xLock */
160394 otaVfsUnlock, /* xUnlock */
160395 otaVfsCheckReservedLock, /* xCheckReservedLock */
160396 otaVfsFileControl, /* xFileControl */
160397 otaVfsSectorSize, /* xSectorSize */
160398 otaVfsDeviceCharacteristics, /* xDeviceCharacteristics */
160399 otaVfsShmMap, /* xShmMap */
160400 otaVfsShmLock, /* xShmLock */
160401 otaVfsShmBarrier, /* xShmBarrier */
160402 otaVfsShmUnmap /* xShmUnmap */
160403 };
160404 ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
160405 sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
160406 ota_file *pFd = (ota_file *)pFile;
160407 int rc = SQLITE_OK;
160408 const char *zOpen = zName;
160409
160410 memset(pFd, 0, sizeof(ota_file));
160411 pFd->pReal = (sqlite3_file*)&pFd[1];
160412 pFd->pOtaVfs = pOtaVfs;
160413 pFd->openFlags = flags;
160414 if( zName ){
160415 if( flags & SQLITE_OPEN_MAIN_DB ){
160416 /* A main database has just been opened. The following block sets
160417 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
@@ -160435,13 +160637,13 @@
160435 }
160436 z += (n + 8 + 1);
160437 pFd->zWal = z;
160438 }
160439 else if( flags & SQLITE_OPEN_WAL ){
160440 ota_file *pDb = otaFindMaindb(pOtaVfs, zName);
160441 if( pDb ){
160442 if( pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
160443 /* This call is to open a *-wal file. Intead, open the *-oal. This
160444 ** code ensures that the string passed to xOpen() is terminated by a
160445 ** pair of '\0' bytes in case the VFS attempts to extract a URI
160446 ** parameter from it. */
160447 int nCopy = strlen(zName);
@@ -160453,11 +160655,11 @@
160453 zCopy[nCopy+1] = '\0';
160454 zOpen = (const char*)(pFd->zDel = zCopy);
160455 }else{
160456 rc = SQLITE_NOMEM;
160457 }
160458 pFd->pOta = pDb->pOta;
160459 }
160460 pDb->pWalFd = pFd;
160461 }
160462 }
160463 }
@@ -160467,16 +160669,16 @@
160467 }
160468 if( pFd->pReal->pMethods ){
160469 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
160470 ** pointer and, if the file is a main database file, link it into the
160471 ** mutex protected linked list of all such files. */
160472 pFile->pMethods = &otavfs_io_methods;
160473 if( flags & SQLITE_OPEN_MAIN_DB ){
160474 sqlite3_mutex_enter(pOtaVfs->mutex);
160475 pFd->pMainNext = pOtaVfs->pMain;
160476 pOtaVfs->pMain = pFd;
160477 sqlite3_mutex_leave(pOtaVfs->mutex);
160478 }
160479 }else{
160480 sqlite3_free(pFd->zDel);
160481 }
160482
@@ -160484,48 +160686,48 @@
160484 }
160485
160486 /*
160487 ** Delete the file located at zPath.
160488 */
160489 static int otaVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
160490 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160491 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
160492 }
160493
160494 /*
160495 ** Test for access permissions. Return true if the requested permission
160496 ** is available, or false otherwise.
160497 */
160498 static int otaVfsAccess(
160499 sqlite3_vfs *pVfs,
160500 const char *zPath,
160501 int flags,
160502 int *pResOut
160503 ){
160504 ota_vfs *pOtaVfs = (ota_vfs*)pVfs;
160505 sqlite3_vfs *pRealVfs = pOtaVfs->pRealVfs;
160506 int rc;
160507
160508 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
160509
160510 /* If this call is to check if a *-wal file associated with an OTA target
160511 ** database connection exists, and the OTA update is in OTA_STAGE_OAL,
160512 ** the following special handling is activated:
160513 **
160514 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
160515 ** ensures that the OTA extension never tries to update a database
160516 ** in wal mode, even if the first page of the database file has
160517 ** been damaged.
160518 **
160519 ** b) if the *-wal file does not exist, claim that it does anyway,
160520 ** causing SQLite to call xOpen() to open it. This call will also
160521 ** be intercepted (see the otaVfsOpen() function) and the *-oal
160522 ** file opened instead.
160523 */
160524 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
160525 ota_file *pDb = otaFindMaindb(pOtaVfs, zPath);
160526 if( pDb && pDb->pOta && pDb->pOta->eStage==OTA_STAGE_OAL ){
160527 if( *pResOut ){
160528 rc = SQLITE_CANTOPEN;
160529 }else{
160530 *pResOut = 1;
160531 }
@@ -160538,151 +160740,151 @@
160538 /*
160539 ** Populate buffer zOut with the full canonical pathname corresponding
160540 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
160541 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
160542 */
160543 static int otaVfsFullPathname(
160544 sqlite3_vfs *pVfs,
160545 const char *zPath,
160546 int nOut,
160547 char *zOut
160548 ){
160549 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160550 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
160551 }
160552
160553 #ifndef SQLITE_OMIT_LOAD_EXTENSION
160554 /*
160555 ** Open the dynamic library located at zPath and return a handle.
160556 */
160557 static void *otaVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
160558 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160559 return pRealVfs->xDlOpen(pRealVfs, zPath);
160560 }
160561
160562 /*
160563 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
160564 ** utf-8 string describing the most recent error encountered associated
160565 ** with dynamic libraries.
160566 */
160567 static void otaVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
160568 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160569 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
160570 }
160571
160572 /*
160573 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
160574 */
160575 static void (*otaVfsDlSym(
160576 sqlite3_vfs *pVfs,
160577 void *pArg,
160578 const char *zSym
160579 ))(void){
160580 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160581 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
160582 }
160583
160584 /*
160585 ** Close the dynamic library handle pHandle.
160586 */
160587 static void otaVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
160588 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160589 return pRealVfs->xDlClose(pRealVfs, pHandle);
160590 }
160591 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
160592
160593 /*
160594 ** Populate the buffer pointed to by zBufOut with nByte bytes of
160595 ** random data.
160596 */
160597 static int otaVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
160598 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160599 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
160600 }
160601
160602 /*
160603 ** Sleep for nMicro microseconds. Return the number of microseconds
160604 ** actually slept.
160605 */
160606 static int otaVfsSleep(sqlite3_vfs *pVfs, int nMicro){
160607 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160608 return pRealVfs->xSleep(pRealVfs, nMicro);
160609 }
160610
160611 /*
160612 ** Return the current time as a Julian Day number in *pTimeOut.
160613 */
160614 static int otaVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
160615 sqlite3_vfs *pRealVfs = ((ota_vfs*)pVfs)->pRealVfs;
160616 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
160617 }
160618
160619 /*
160620 ** No-op.
160621 */
160622 static int otaVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
160623 return 0;
160624 }
160625
160626 /*
160627 ** Deregister and destroy an OTA vfs created by an earlier call to
160628 ** sqlite3ota_create_vfs().
160629 */
160630 SQLITE_API void SQLITE_STDCALL sqlite3ota_destroy_vfs(const char *zName){
160631 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
160632 if( pVfs && pVfs->xOpen==otaVfsOpen ){
160633 sqlite3_mutex_free(((ota_vfs*)pVfs)->mutex);
160634 sqlite3_vfs_unregister(pVfs);
160635 sqlite3_free(pVfs);
160636 }
160637 }
160638
160639 /*
160640 ** Create an OTA VFS named zName that accesses the underlying file-system
160641 ** via existing VFS zParent. The new object is registered as a non-default
160642 ** VFS with SQLite before returning.
160643 */
160644 SQLITE_API int SQLITE_STDCALL sqlite3ota_create_vfs(const char *zName, const char *zParent){
160645
160646 /* Template for VFS */
160647 static sqlite3_vfs vfs_template = {
160648 1, /* iVersion */
160649 0, /* szOsFile */
160650 0, /* mxPathname */
160651 0, /* pNext */
160652 0, /* zName */
160653 0, /* pAppData */
160654 otaVfsOpen, /* xOpen */
160655 otaVfsDelete, /* xDelete */
160656 otaVfsAccess, /* xAccess */
160657 otaVfsFullPathname, /* xFullPathname */
160658
160659 #ifndef SQLITE_OMIT_LOAD_EXTENSION
160660 otaVfsDlOpen, /* xDlOpen */
160661 otaVfsDlError, /* xDlError */
160662 otaVfsDlSym, /* xDlSym */
160663 otaVfsDlClose, /* xDlClose */
160664 #else
160665 0, 0, 0, 0,
160666 #endif
160667
160668 otaVfsRandomness, /* xRandomness */
160669 otaVfsSleep, /* xSleep */
160670 otaVfsCurrentTime, /* xCurrentTime */
160671 otaVfsGetLastError, /* xGetLastError */
160672 0, /* xCurrentTimeInt64 (version 2) */
160673 0, 0, 0 /* Unimplemented version 3 methods */
160674 };
160675
160676 ota_vfs *pNew = 0; /* Newly allocated VFS */
160677 int nName;
160678 int rc = SQLITE_OK;
160679
160680 int nByte;
160681 nName = strlen(zName);
160682 nByte = sizeof(ota_vfs) + nName + 1;
160683 pNew = (ota_vfs*)sqlite3_malloc(nByte);
160684 if( pNew==0 ){
160685 rc = SQLITE_NOMEM;
160686 }else{
160687 sqlite3_vfs *pParent; /* Parent VFS */
160688 memset(pNew, 0, nByte);
@@ -160691,11 +160893,11 @@
160691 rc = SQLITE_NOTFOUND;
160692 }else{
160693 char *zSpace;
160694 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
160695 pNew->base.mxPathname = pParent->mxPathname;
160696 pNew->base.szOsFile = sizeof(ota_file) + pParent->szOsFile;
160697 pNew->pRealVfs = pParent;
160698 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
160699 memcpy(zSpace, zName, nName);
160700
160701 /* Allocate the mutex and register the new VFS (not as the default) */
@@ -160717,13 +160919,13 @@
160717 }
160718
160719
160720 /**************************************************************************/
160721
160722 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_OTA) */
160723
160724 /************** End of sqlite3ota.c ******************************************/
160725 /************** Begin file dbstat.c ******************************************/
160726 /*
160727 ** 2010 July 12
160728 **
160729 ** The author disclaims copyright to this source code. In place of
@@ -160741,10 +160943,11 @@
160741 ** information from an SQLite database in order to implement the
160742 ** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
160743 ** for an example implementation.
160744 */
160745
 
160746 #if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
160747 && !defined(SQLITE_OMIT_VIRTUALTABLE)
160748
160749 /*
160750 ** Page paths:
160751
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -325,11 +325,11 @@
325 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
326 ** [sqlite_version()] and [sqlite_source_id()].
327 */
328 #define SQLITE_VERSION "3.8.11"
329 #define SQLITE_VERSION_NUMBER 3008011
330 #define SQLITE_SOURCE_ID "2015-07-23 20:44:49 017c5019e1ce042025d4f327e50ec50af49f9fa4"
331
332 /*
333 ** CAPI3REF: Run-Time Library Version Numbers
334 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
335 **
@@ -1179,13 +1179,13 @@
1179 **
1180 ** <li>[[SQLITE_FCNTL_ZIPVFS]]
1181 ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1182 ** VFS should return SQLITE_NOTFOUND for this opcode.
1183 **
1184 ** <li>[[SQLITE_FCNTL_RBU]]
1185 ** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
1186 ** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
1187 ** this opcode.
1188 ** </ul>
1189 */
1190 #define SQLITE_FCNTL_LOCKSTATE 1
1191 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -1209,11 +1209,11 @@
1209 #define SQLITE_FCNTL_SYNC 21
1210 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
1211 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
1212 #define SQLITE_FCNTL_WAL_BLOCK 24
1213 #define SQLITE_FCNTL_ZIPVFS 25
1214 #define SQLITE_FCNTL_RBU 26
1215
1216 /* deprecated names */
1217 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1218 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1219 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -8361,11 +8361,11 @@
8361
8362 /*
8363 ** Make sure that the compiler intrinsics we desire are enabled when
8364 ** compiling with an appropriate version of MSVC.
8365 */
8366 #if defined(_MSC_VER) && _MSC_VER>=1300 && !defined(_WIN32_WCE)
8367 # include <intrin.h>
8368 # pragma intrinsic(_byteswap_ushort)
8369 # pragma intrinsic(_byteswap_ulong)
8370 #endif
8371
@@ -9189,11 +9189,13 @@
9189 #endif
9190 #ifndef SQLITE_MAX_MMAP_SIZE
9191 # if defined(__linux__) \
9192 || defined(_WIN32) \
9193 || (defined(__APPLE__) && defined(__MACH__)) \
9194 || defined(__sun) \
9195 || defined(__FreeBSD__) \
9196 || defined(__DragonFly__)
9197 # define SQLITE_MAX_MMAP_SIZE 0x7fff0000 /* 2147418112 */
9198 # else
9199 # define SQLITE_MAX_MMAP_SIZE 0
9200 # endif
9201 # define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
@@ -13883,10 +13885,11 @@
13885 **
13886 *************************************************************************
13887 **
13888 ** This file contains definitions of global variables and constants.
13889 */
13890 /* #include "sqliteInt.h" */
13891
13892 /* An array to map all upper-case characters into their corresponding
13893 ** lower-case character.
13894 **
13895 ** SQLite only considers US-ASCII (or EBCDIC) characters. We do not
@@ -14122,10 +14125,11 @@
14125 */
14126 #ifndef SQLITE_OMIT_WSD
14127 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
14128 #endif
14129
14130 /* #include "opcodes.h" */
14131 /*
14132 ** Properties of opcodes. The OPFLG_INITIALIZER macro is
14133 ** created by mkopcodeh.awk during compilation. Data is obtained
14134 ** from the comments following the "case OP_xxxx:" statements in
14135 ** the vdbe.c file.
@@ -14150,10 +14154,11 @@
14154 ** SQLite was built with.
14155 */
14156
14157 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
14158
14159 /* #include "sqliteInt.h" */
14160
14161 /*
14162 ** An array of names of all compile-time options. This array should
14163 ** be sorted A-Z.
14164 **
@@ -14579,10 +14584,11 @@
14584 *************************************************************************
14585 **
14586 ** This module implements the sqlite3_status() interface and related
14587 ** functionality.
14588 */
14589 /* #include "sqliteInt.h" */
14590 /************** Include vdbeInt.h in the middle of status.c ******************/
14591 /************** Begin file vdbeInt.h *****************************************/
14592 /*
14593 ** 2003 September 6
14594 **
@@ -15462,10 +15468,11 @@
15468 ** Astronomical Algorithms, 2nd Edition, 1998
15469 ** ISBM 0-943396-61-1
15470 ** Willmann-Bell, Inc
15471 ** Richmond, Virginia (USA)
15472 */
15473 /* #include "sqliteInt.h" */
15474 /* #include <stdlib.h> */
15475 /* #include <assert.h> */
15476 #include <time.h>
15477
15478 #ifndef SQLITE_OMIT_DATETIME_FUNCS
@@ -15773,11 +15780,11 @@
15780 Z = (int)((p->iJD + 43200000)/86400000);
15781 A = (int)((Z - 1867216.25)/36524.25);
15782 A = Z + 1 + A - (A/4);
15783 B = A + 1524;
15784 C = (int)((B - 122.1)/365.25);
15785 D = (36525*(C&32767))/100;
15786 E = (int)((B-D)/30.6001);
15787 X1 = (int)(30.6001*E);
15788 p->D = B - D - X1;
15789 p->M = E<14 ? E-1 : E-13;
15790 p->Y = p->M>2 ? C - 4716 : C - 4715;
@@ -16574,10 +16581,11 @@
16581 **
16582 ** This file contains OS interface code that is common to all
16583 ** architectures.
16584 */
16585 #define _SQLITE_OS_C_ 1
16586 /* #include "sqliteInt.h" */
16587 #undef _SQLITE_OS_C_
16588
16589 /*
16590 ** The default SQLite sqlite3_vfs implementations do not allocate
16591 ** memory (actually, os_unix.c allocates a small amount of memory
@@ -16980,10 +16988,11 @@
16988 ** is completely recoverable simply by not carrying out the resize. The
16989 ** hash table will continue to function normally. So a malloc failure
16990 ** during a hash table resize is a benign fault.
16991 */
16992
16993 /* #include "sqliteInt.h" */
16994
16995 #ifndef SQLITE_OMIT_BUILTIN_TEST
16996
16997 /*
16998 ** Global variables.
@@ -17061,10 +17070,11 @@
17070 ** SQLITE_ZERO_MALLOC is defined. The allocation drivers implemented
17071 ** here always fail. SQLite will not operate with these drivers. These
17072 ** are merely placeholders. Real drivers must be substituted using
17073 ** sqlite3_config() before SQLite will operate.
17074 */
17075 /* #include "sqliteInt.h" */
17076
17077 /*
17078 ** This version of the memory allocator is the default. It is
17079 ** used when no other memory allocator is specified using compile-time
17080 ** macros.
@@ -17147,10 +17157,11 @@
17157 ** SQLITE_WITHOUT_MSIZE Set this symbol to disable the use of
17158 ** _msize() on windows systems. This might
17159 ** be necessary when compiling for Delphi,
17160 ** for example.
17161 */
17162 /* #include "sqliteInt.h" */
17163
17164 /*
17165 ** This version of the memory allocator is the default. It is
17166 ** used when no other memory allocator is specified using compile-time
17167 ** macros.
@@ -17422,10 +17433,11 @@
17433 ** leaks and memory usage errors.
17434 **
17435 ** This file contains implementations of the low-level memory allocation
17436 ** routines specified in the sqlite3_mem_methods object.
17437 */
17438 /* #include "sqliteInt.h" */
17439
17440 /*
17441 ** This version of the memory allocator is used only if the
17442 ** SQLITE_MEMDEBUG macro is defined
17443 */
@@ -17956,10 +17968,11 @@
17968 ** be changed.
17969 **
17970 ** This version of the memory allocation subsystem is included
17971 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
17972 */
17973 /* #include "sqliteInt.h" */
17974
17975 /*
17976 ** This version of the memory allocator is only built into the library
17977 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
17978 ** mean that the library will use a memory-pool by default, just that
@@ -18670,10 +18683,11 @@
18683 ** N >= M*(1 + log2(n)/2) - n + 1
18684 **
18685 ** The sqlite3_status() logic tracks the maximum values of n and M so
18686 ** that an application can, at any time, verify this constraint.
18687 */
18688 /* #include "sqliteInt.h" */
18689
18690 /*
18691 ** This version of the memory allocator is used only when
18692 ** SQLITE_ENABLE_MEMSYS5 is defined.
18693 */
@@ -19213,10 +19227,11 @@
19227 *************************************************************************
19228 ** This file contains the C functions that implement mutexes.
19229 **
19230 ** This file contains code that is common across all mutex implementations.
19231 */
19232 /* #include "sqliteInt.h" */
19233
19234 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
19235 /*
19236 ** For debugging purposes, record when the mutex subsystem is initialized
19237 ** and uninitialized so that we can assert() if there is an attempt to
@@ -19386,10 +19401,11 @@
19401 **
19402 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
19403 ** that does error checking on mutexes to make sure they are being
19404 ** called correctly.
19405 */
19406 /* #include "sqliteInt.h" */
19407
19408 #ifndef SQLITE_MUTEX_OMIT
19409
19410 #ifndef SQLITE_DEBUG
19411 /*
@@ -19589,10 +19605,11 @@
19605 ** May you share freely, never taking more than you give.
19606 **
19607 *************************************************************************
19608 ** This file contains the C functions that implement mutexes for pthreads
19609 */
19610 /* #include "sqliteInt.h" */
19611
19612 /*
19613 ** The code in this file is only used if we are compiling threadsafe
19614 ** under unix with pthreads.
19615 **
@@ -19963,10 +19980,11 @@
19980 ** May you share freely, never taking more than you give.
19981 **
19982 *************************************************************************
19983 ** This file contains the C functions that implement mutexes for Win32.
19984 */
19985 /* #include "sqliteInt.h" */
19986
19987 #if SQLITE_OS_WIN
19988 /*
19989 ** Include code that is common to all os_*.c files
19990 */
@@ -20638,10 +20656,11 @@
20656 **
20657 *************************************************************************
20658 **
20659 ** Memory allocation functions used throughout sqlite.
20660 */
20661 /* #include "sqliteInt.h" */
20662 /* #include <stdarg.h> */
20663
20664 /*
20665 ** Attempt to release up to n bytes of non-essential memory currently
20666 ** held by SQLite. An example of non-essential memory is memory used to
@@ -21449,10 +21468,11 @@
21468 ** This file contains code for a set of "printf"-like routines. These
21469 ** routines format strings much like the printf() from the standard C
21470 ** library, though the implementation here has enhancements to support
21471 ** SQLite.
21472 */
21473 /* #include "sqliteInt.h" */
21474
21475 /*
21476 ** Conversion types fall into various categories as defined by the
21477 ** following enumeration.
21478 */
@@ -22451,10 +22471,15 @@
22471 ** stack space on small-stack systems when logging is disabled.
22472 **
22473 ** sqlite3_log() must render into a static buffer. It cannot dynamically
22474 ** allocate memory because it might be called while the memory allocator
22475 ** mutex is held.
22476 **
22477 ** sqlite3VXPrintf() might ask for *temporary* memory allocations for
22478 ** certain format characters (%q) or for very large precisions or widths.
22479 ** Care must be taken that any sqlite3_log() calls that occur while the
22480 ** memory mutex is held do not use these mechanisms.
22481 */
22482 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
22483 StrAccum acc; /* String accumulator */
22484 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
22485
@@ -22526,10 +22551,11 @@
22551 ** analysis.
22552 **
22553 ** The interfaces in this file is only available when compiling
22554 ** with SQLITE_DEBUG.
22555 */
22556 /* #include "sqliteInt.h" */
22557 #ifdef SQLITE_DEBUG
22558
22559 /*
22560 ** Add a new subitem to the tree. The moreToFollow flag indicates that this
22561 ** is not the last item in the tree.
@@ -22957,10 +22983,11 @@
22983 ** generator (PRNG) for SQLite.
22984 **
22985 ** Random numbers are used by some of the database backends in order
22986 ** to generate random integer keys for tables or random filenames.
22987 */
22988 /* #include "sqliteInt.h" */
22989
22990
22991 /* All threads share a single random number generator.
22992 ** This structure is the current state of the generator.
22993 */
@@ -23103,11 +23130,13 @@
23130 ** single threaded systems. Nothing in SQLite requires multiple threads.
23131 ** This interface exists so that applications that want to take advantage
23132 ** of multiple cores can do so, while also allowing applications to stay
23133 ** single-threaded if desired.
23134 */
23135 /* #include "sqliteInt.h" */
23136 #if SQLITE_OS_WIN
23137 /* # include "os_win.h" */
23138 #endif
23139
23140 #if SQLITE_MAX_WORKER_THREADS>0
23141
23142 /********************************* Unix Pthreads ****************************/
@@ -23377,11 +23406,13 @@
23406 ** BOM or Byte Order Mark:
23407 ** 0xff 0xfe little-endian utf-16 follows
23408 ** 0xfe 0xff big-endian utf-16 follows
23409 **
23410 */
23411 /* #include "sqliteInt.h" */
23412 /* #include <assert.h> */
23413 /* #include "vdbeInt.h" */
23414
23415 #ifndef SQLITE_AMALGAMATION
23416 /*
23417 ** The following constant value is used by the SQLITE_BIGENDIAN and
23418 ** SQLITE_LITTLEENDIAN macros.
@@ -23890,10 +23921,11 @@
23921 **
23922 ** This file contains functions for allocating memory, comparing
23923 ** strings, and stuff like that.
23924 **
23925 */
23926 /* #include "sqliteInt.h" */
23927 /* #include <stdarg.h> */
23928 #if HAVE_ISNAN || SQLITE_HAVE_ISNAN
23929 # include <math.h>
23930 #endif
23931
@@ -25286,10 +25318,11 @@
25318 **
25319 *************************************************************************
25320 ** This is the implementation of generic hash-tables
25321 ** used in SQLite.
25322 */
25323 /* #include "sqliteInt.h" */
25324 /* #include <assert.h> */
25325
25326 /* Turn bulk memory into a hash table object by initializing the
25327 ** fields of the Hash structure.
25328 **
@@ -25763,10 +25796,11 @@
25796 ** * sqlite3_vfs method implementations.
25797 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only)
25798 ** * Definitions of sqlite3_vfs objects for all locking methods
25799 ** plus implementations of sqlite3_os_init() and sqlite3_os_end().
25800 */
25801 /* #include "sqliteInt.h" */
25802 #if SQLITE_OS_UNIX /* This file is used on unix only */
25803
25804 /*
25805 ** There are various methods for file locking used for concurrency
25806 ** control:
@@ -33497,10 +33531,11 @@
33531 **
33532 ******************************************************************************
33533 **
33534 ** This file contains code that is specific to Windows.
33535 */
33536 /* #include "sqliteInt.h" */
33537 #if SQLITE_OS_WIN /* This file is used for Windows only */
33538
33539 /*
33540 ** Include code that is common to all os_*.c files
33541 */
@@ -33705,10 +33740,11 @@
33740 /************** Continuing where we left off in os_win.c *********************/
33741
33742 /*
33743 ** Include the header file for the Windows VFS.
33744 */
33745 /* #include "os_win.h" */
33746
33747 /*
33748 ** Compiling and using WAL mode requires several APIs that are only
33749 ** available in Windows platforms based on the NT kernel.
33750 */
@@ -39358,10 +39394,11 @@
39394 ** sometimes grow into tens of thousands or larger. The size of the
39395 ** Bitvec object is the number of pages in the database file at the
39396 ** start of a transaction, and is thus usually less than a few thousand,
39397 ** but can be as large as 2 billion for a really big database.
39398 */
39399 /* #include "sqliteInt.h" */
39400
39401 /* Size of the Bitvec structure in bytes. */
39402 #define BITVEC_SZ 512
39403
39404 /* Round the union size down to the nearest pointer boundary, since that's how
@@ -39747,10 +39784,11 @@
39784 ** May you share freely, never taking more than you give.
39785 **
39786 *************************************************************************
39787 ** This file implements that page cache.
39788 */
39789 /* #include "sqliteInt.h" */
39790
39791 /*
39792 ** A complete page cache is an instance of this structure.
39793 */
39794 struct PCache {
@@ -40488,10 +40526,11 @@
40526 **
40527 ** Earlier versions of SQLite used only methods (1) and (2). But experiments
40528 ** show that method (3) with N==100 provides about a 5% performance boost for
40529 ** common workloads.
40530 */
40531 /* #include "sqliteInt.h" */
40532
40533 typedef struct PCache1 PCache1;
40534 typedef struct PgHdr1 PgHdr1;
40535 typedef struct PgFreeslot PgFreeslot;
40536 typedef struct PGroup PGroup;
@@ -40598,10 +40637,11 @@
40637 ** fixed at sqlite3_initialize() time and do not require mutex protection.
40638 ** The nFreeSlot and pFree values do require mutex protection.
40639 */
40640 int isInit; /* True if initialized */
40641 int separateCache; /* Use a new PGroup for each PCache */
40642 int nInitPage; /* Initial bulk allocation size */
40643 int szSlot; /* Size of each free slot */
40644 int nSlot; /* The number of pcache slots */
40645 int nReserve; /* Try to keep nFreeSlot above this */
40646 void *pStart, *pEnd; /* Bounds of global page cache memory */
40647 /* Above requires no mutex. Use mutex below for variable that follow. */
@@ -40665,10 +40705,47 @@
40705 pBuf = (void*)&((char*)pBuf)[sz];
40706 }
40707 pcache1.pEnd = pBuf;
40708 }
40709 }
40710
40711 /*
40712 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return
40713 ** true if pCache->pFree ends up containing one or more free pages.
40714 */
40715 static int pcache1InitBulk(PCache1 *pCache){
40716 i64 szBulk;
40717 char *zBulk;
40718 if( pcache1.nInitPage==0 ) return 0;
40719 /* Do not bother with a bulk allocation if the cache size very small */
40720 if( pCache->nMax<3 ) return 0;
40721 sqlite3BeginBenignMalloc();
40722 if( pcache1.nInitPage>0 ){
40723 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage;
40724 }else{
40725 szBulk = -1024 * (i64)pcache1.nInitPage;
40726 }
40727 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){
40728 szBulk = pCache->szAlloc*pCache->nMax;
40729 }
40730 zBulk = pCache->pBulk = sqlite3Malloc( szBulk );
40731 sqlite3EndBenignMalloc();
40732 if( zBulk ){
40733 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc;
40734 int i;
40735 for(i=0; i<nBulk; i++){
40736 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage];
40737 pX->page.pBuf = zBulk;
40738 pX->page.pExtra = &pX[1];
40739 pX->isBulkLocal = 1;
40740 pX->pNext = pCache->pFree;
40741 pCache->pFree = pX;
40742 zBulk += pCache->szAlloc;
40743 }
40744 }
40745 return pCache->pFree!=0;
40746 }
40747
40748 /*
40749 ** Malloc function used within this file to allocate space from the buffer
40750 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
40751 ** such buffer exists or there is no space left in it, this function falls
@@ -40766,11 +40843,11 @@
40843 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
40844 PgHdr1 *p = 0;
40845 void *pPg;
40846
40847 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
40848 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
40849 p = pCache->pFree;
40850 pCache->pFree = p->pNext;
40851 p->pNext = 0;
40852 }else{
40853 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
@@ -40970,18 +41047,23 @@
41047
41048 /*
41049 ** If there are currently more than nMaxPage pages allocated, try
41050 ** to recycle pages to reduce the number allocated to nMaxPage.
41051 */
41052 static void pcache1EnforceMaxPage(PCache1 *pCache){
41053 PGroup *pGroup = pCache->pGroup;
41054 assert( sqlite3_mutex_held(pGroup->mutex) );
41055 while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
41056 PgHdr1 *p = pGroup->pLruTail;
41057 assert( p->pCache->pGroup==pGroup );
41058 assert( p->isPinned==0 );
41059 pcache1PinPage(p);
41060 pcache1RemoveFromHash(p, 1);
41061 }
41062 if( pCache->nPage==0 && pCache->pBulk ){
41063 sqlite3_free(pCache->pBulk);
41064 pCache->pBulk = pCache->pFree = 0;
41065 }
41066 }
41067
41068 /*
41069 ** Discard all pages from cache pCache with a page number (key value)
@@ -41054,10 +41136,18 @@
41136 if( sqlite3GlobalConfig.bCoreMutex ){
41137 pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
41138 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
41139 }
41140 #endif
41141 if( pcache1.separateCache
41142 && sqlite3GlobalConfig.nPage!=0
41143 && sqlite3GlobalConfig.pPage==0
41144 ){
41145 pcache1.nInitPage = sqlite3GlobalConfig.nPage;
41146 }else{
41147 pcache1.nInitPage = 0;
41148 }
41149 pcache1.grp.mxPinned = 10;
41150 pcache1.isInit = 1;
41151 return SQLITE_OK;
41152 }
41153
@@ -41108,40 +41198,10 @@
41198 pCache->nMin = 10;
41199 pGroup->nMinPage += pCache->nMin;
41200 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
41201 }
41202 pcache1LeaveMutex(pGroup);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41203 if( pCache->nHash==0 ){
41204 pcache1Destroy((sqlite3_pcache*)pCache);
41205 pCache = 0;
41206 }
41207 }
@@ -41160,11 +41220,11 @@
41220 pcache1EnterMutex(pGroup);
41221 pGroup->nMaxPage += (nMax - pCache->nMax);
41222 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
41223 pCache->nMax = nMax;
41224 pCache->n90pct = pCache->nMax*9/10;
41225 pcache1EnforceMaxPage(pCache);
41226 pcache1LeaveMutex(pGroup);
41227 }
41228 }
41229
41230 /*
@@ -41178,11 +41238,11 @@
41238 PGroup *pGroup = pCache->pGroup;
41239 int savedMaxPage;
41240 pcache1EnterMutex(pGroup);
41241 savedMaxPage = pGroup->nMaxPage;
41242 pGroup->nMaxPage = 0;
41243 pcache1EnforceMaxPage(pCache);
41244 pGroup->nMaxPage = savedMaxPage;
41245 pcache1LeaveMutex(pGroup);
41246 }
41247 }
41248
@@ -41515,11 +41575,11 @@
41575 assert( pGroup->nMaxPage >= pCache->nMax );
41576 pGroup->nMaxPage -= pCache->nMax;
41577 assert( pGroup->nMinPage >= pCache->nMin );
41578 pGroup->nMinPage -= pCache->nMin;
41579 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
41580 pcache1EnforceMaxPage(pCache);
41581 pcache1LeaveMutex(pGroup);
41582 sqlite3_free(pCache->pBulk);
41583 sqlite3_free(pCache->apHash);
41584 sqlite3_free(pCache);
41585 }
@@ -41679,10 +41739,11 @@
41739 ** primitives are constant time. The cost of DESTROY is O(N).
41740 **
41741 ** There is an added cost of O(N) when switching between TEST and
41742 ** SMALLEST primitives.
41743 */
41744 /* #include "sqliteInt.h" */
41745
41746
41747 /*
41748 ** Target size for allocation chunks.
41749 */
@@ -42148,10 +42209,11 @@
42209 ** locking to prevent two processes from writing the same database
42210 ** file simultaneously, or one process from reading the database while
42211 ** another is writing.
42212 */
42213 #ifndef SQLITE_OMIT_DISKIO
42214 /* #include "sqliteInt.h" */
42215 /************** Include wal.h in the middle of pager.c ***********************/
42216 /************** Begin file wal.h *********************************************/
42217 /*
42218 ** 2010 February 1
42219 **
@@ -42169,10 +42231,11 @@
42231 */
42232
42233 #ifndef _WAL_H_
42234 #define _WAL_H_
42235
42236 /* #include "sqliteInt.h" */
42237
42238 /* Additional values that can be added to the sync_flags argument of
42239 ** sqlite3WalFrames():
42240 */
42241 #define WAL_SYNC_TRANSACTIONS 0x20 /* Sync at the end of each transaction */
@@ -49783,10 +49846,11 @@
49846 ** that correspond to frames greater than the new K value are removed
49847 ** from the hash table at this point.
49848 */
49849 #ifndef SQLITE_OMIT_WAL
49850
49851 /* #include "wal.h" */
49852
49853 /*
49854 ** Trace output macros
49855 */
49856 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
@@ -50188,13 +50252,13 @@
50252
50253 assert( pWal->writeLock );
50254 pWal->hdr.isInit = 1;
50255 pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
50256 walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
50257 memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
50258 walShmBarrier(pWal);
50259 memcpy((void*)&aHdr[0], (const void*)&pWal->hdr, sizeof(WalIndexHdr));
50260 }
50261
50262 /*
50263 ** This function encodes a single frame header and writes it to a buffer
50264 ** supplied by the caller. A frame-header is made up of a series of
@@ -50492,17 +50556,17 @@
50556 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
50557 /* Verify that the every entry in the mapping region is still reachable
50558 ** via the hash table even after the cleanup.
50559 */
50560 if( iLimit ){
50561 int j; /* Loop counter */
50562 int iKey; /* Hash key */
50563 for(j=1; j<=iLimit; j++){
50564 for(iKey=walHash(aPgno[j]); aHash[iKey]; iKey=walNextHash(iKey)){
50565 if( aHash[iKey]==j ) break;
50566 }
50567 assert( aHash[iKey]==j );
50568 }
50569 }
50570 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
50571 }
50572
@@ -51000,11 +51064,11 @@
51064
51065 const int nList = *pnList; /* Size of input list */
51066 int nMerge = 0; /* Number of elements in list aMerge */
51067 ht_slot *aMerge = 0; /* List to be merged */
51068 int iList; /* Index into input list */
51069 u32 iSub = 0; /* Index into aSub array */
51070 struct Sublist aSub[13]; /* Array of sub-lists */
51071
51072 memset(aSub, 0, sizeof(aSub));
51073 assert( nList<=HASHTABLE_NPAGE && nList>0 );
51074 assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
@@ -51011,11 +51075,13 @@
51075
51076 for(iList=0; iList<nList; iList++){
51077 nMerge = 1;
51078 aMerge = &aList[iList];
51079 for(iSub=0; iList & (1<<iSub); iSub++){
51080 struct Sublist *p;
51081 assert( iSub<ArraySize(aSub) );
51082 p = &aSub[iSub];
51083 assert( p->aList && p->nList<=(1<<iSub) );
51084 assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
51085 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
51086 }
51087 aSub[iSub].aList = aMerge;
@@ -51022,11 +51088,13 @@
51088 aSub[iSub].nList = nMerge;
51089 }
51090
51091 for(iSub++; iSub<ArraySize(aSub); iSub++){
51092 if( nList & (1<<iSub) ){
51093 struct Sublist *p;
51094 assert( iSub<ArraySize(aSub) );
51095 p = &aSub[iSub];
51096 assert( p->nList<=(1<<iSub) );
51097 assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
51098 walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
51099 }
51100 }
@@ -52933,10 +53001,11 @@
53001 ** SIZE DESCRIPTION
53002 ** 4 Page number of next trunk page
53003 ** 4 Number of leaf pointers on this page
53004 ** * zero or more pages numbers of leaves
53005 */
53006 /* #include "sqliteInt.h" */
53007
53008
53009 /* The following value is the maximum cell size assuming a maximum page
53010 ** size give above.
53011 */
@@ -53730,10 +53799,11 @@
53799 *************************************************************************
53800 ** This file implements an external (disk-based) database using BTrees.
53801 ** See the header comment on "btreeInt.h" for additional information.
53802 ** Including a description of file format and an overview of operation.
53803 */
53804 /* #include "btreeInt.h" */
53805
53806 /*
53807 ** The header string that appears at the beginning of every
53808 ** SQLite database.
53809 */
@@ -62675,11 +62745,11 @@
62745 u32 *heap = 0; /* Min-heap used for checking cell coverage */
62746 u32 x, prev = 0; /* Next and previous entry on the min-heap */
62747 const char *saved_zPfx = pCheck->zPfx;
62748 int saved_v1 = pCheck->v1;
62749 int saved_v2 = pCheck->v2;
62750 u8 savedIsInit = 0;
62751
62752 /* Check that the page exists
62753 */
62754 pBt = pCheck->pBt;
62755 usableSize = pBt->usableSize;
@@ -63311,10 +63381,12 @@
63381 **
63382 *************************************************************************
63383 ** This file contains the implementation of the sqlite3_backup_XXX()
63384 ** API functions and the related features.
63385 */
63386 /* #include "sqliteInt.h" */
63387 /* #include "btreeInt.h" */
63388
63389 /*
63390 ** Structure allocated for each backup operation.
63391 */
63392 struct sqlite3_backup {
@@ -64109,10 +64181,12 @@
64181 ** This file contains code use to manipulate "Mem" structure. A "Mem"
64182 ** stores a single value in the VDBE. Mem is an opaque structure visible
64183 ** only within the VDBE. Interface routines refer to a Mem using the
64184 ** name sqlite_value
64185 */
64186 /* #include "sqliteInt.h" */
64187 /* #include "vdbeInt.h" */
64188
64189 #ifdef SQLITE_DEBUG
64190 /*
64191 ** Check invariants on a Mem object.
64192 **
@@ -65826,10 +65900,12 @@
65900 **
65901 *************************************************************************
65902 ** This file contains code used for creating, destroying, and populating
65903 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)
65904 */
65905 /* #include "sqliteInt.h" */
65906 /* #include "vdbeInt.h" */
65907
65908 /*
65909 ** Create a new virtual database engine.
65910 */
65911 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
@@ -70074,10 +70150,12 @@
70150 *************************************************************************
70151 **
70152 ** This file contains code use to implement APIs that are part of the
70153 ** VDBE.
70154 */
70155 /* #include "sqliteInt.h" */
70156 /* #include "vdbeInt.h" */
70157
70158 #ifndef SQLITE_OMIT_DEPRECATED
70159 /*
70160 ** Return TRUE (non-zero) of the statement supplied as an argument needs
70161 ** to be recompiled. A statement needs to be recompiled whenever the
@@ -70112,10 +70190,35 @@
70190 }else{
70191 return vdbeSafety(p);
70192 }
70193 }
70194
70195 #ifndef SQLITE_OMIT_TRACE
70196 /*
70197 ** Invoke the profile callback. This routine is only called if we already
70198 ** know that the profile callback is defined and needs to be invoked.
70199 */
70200 static SQLITE_NOINLINE void invokeProfileCallback(sqlite3 *db, Vdbe *p){
70201 sqlite3_int64 iNow;
70202 assert( p->startTime>0 );
70203 assert( db->xProfile!=0 );
70204 assert( db->init.busy==0 );
70205 assert( p->zSql!=0 );
70206 sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
70207 db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
70208 p->startTime = 0;
70209 }
70210 /*
70211 ** The checkProfileCallback(DB,P) macro checks to see if a profile callback
70212 ** is needed, and it invokes the callback if it is needed.
70213 */
70214 # define checkProfileCallback(DB,P) \
70215 if( ((P)->startTime)>0 ){ invokeProfileCallback(DB,P); }
70216 #else
70217 # define checkProfileCallback(DB,P) /*no-op*/
70218 #endif
70219
70220 /*
70221 ** The following routine destroys a virtual machine that is created by
70222 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
70223 ** success/failure code that describes the result of executing the virtual
70224 ** machine.
@@ -70132,10 +70235,11 @@
70235 }else{
70236 Vdbe *v = (Vdbe*)pStmt;
70237 sqlite3 *db = v->db;
70238 if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
70239 sqlite3_mutex_enter(db->mutex);
70240 checkProfileCallback(db, v);
70241 rc = sqlite3VdbeFinalize(v);
70242 rc = sqlite3ApiExit(db, rc);
70243 sqlite3LeaveMutexAndCloseZombie(db);
70244 }
70245 return rc;
@@ -70153,16 +70257,18 @@
70257 int rc;
70258 if( pStmt==0 ){
70259 rc = SQLITE_OK;
70260 }else{
70261 Vdbe *v = (Vdbe*)pStmt;
70262 sqlite3 *db = v->db;
70263 sqlite3_mutex_enter(db->mutex);
70264 checkProfileCallback(db, v);
70265 rc = sqlite3VdbeReset(v);
70266 sqlite3VdbeRewind(v);
70267 assert( (rc & (db->errMask))==rc );
70268 rc = sqlite3ApiExit(db, rc);
70269 sqlite3_mutex_leave(db->mutex);
70270 }
70271 return rc;
70272 }
70273
70274 /*
@@ -70508,10 +70614,11 @@
70614 }
70615 }
70616 #endif
70617 return rc;
70618 }
70619
70620
70621 /*
70622 ** Execute the statement pStmt, either until a row of data is ready, the
70623 ** statement is completely executed or an error occurs.
70624 **
@@ -70577,12 +70684,14 @@
70684 assert( db->nVdbeWrite>0 || db->autoCommit==0
70685 || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
70686 );
70687
70688 #ifndef SQLITE_OMIT_TRACE
70689 if( db->xProfile && !db->init.busy && p->zSql ){
70690 sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
70691 }else{
70692 assert( p->startTime==0 );
70693 }
70694 #endif
70695
70696 db->nVdbeActive++;
70697 if( p->readOnly==0 ) db->nVdbeWrite++;
@@ -70602,17 +70711,12 @@
70711 rc = sqlite3VdbeExec(p);
70712 db->nVdbeExec--;
70713 }
70714
70715 #ifndef SQLITE_OMIT_TRACE
70716 /* If the statement completed successfully, invoke the profile callback */
70717 if( rc!=SQLITE_ROW ) checkProfileCallback(db, p);
 
 
 
 
 
70718 #endif
70719
70720 if( rc==SQLITE_DONE ){
70721 assert( p->rc==SQLITE_OK );
70722 p->rc = doWalCallbacks(db);
@@ -71685,10 +71789,12 @@
71789 ** This file contains code used to insert the values of host parameters
71790 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
71791 **
71792 ** The Vdbe parse-tree explainer is also found here.
71793 */
71794 /* #include "sqliteInt.h" */
71795 /* #include "vdbeInt.h" */
71796
71797 #ifndef SQLITE_OMIT_TRACE
71798
71799 /*
71800 ** zSql is a zero-terminated string of UTF-8 SQL text. Return the number of
@@ -71877,10 +71983,12 @@
71983 ** documentation, headers files, or other derived files. The formatting
71984 ** of the code in this file is, therefore, important. See other comments
71985 ** in this file for details. If in doubt, do not deviate from existing
71986 ** commenting and indentation practices when changing or adding code.
71987 */
71988 /* #include "sqliteInt.h" */
71989 /* #include "vdbeInt.h" */
71990
71991 /*
71992 ** Invoke this macro on memory cells just prior to changing the
71993 ** value of the cell. This macro verifies that shallow copies are
71994 ** not misused. A shallow copy of a string or blob just copies a
@@ -78635,10 +78743,12 @@
78743 *************************************************************************
78744 **
78745 ** This file contains code used to implement incremental BLOB I/O.
78746 */
78747
78748 /* #include "sqliteInt.h" */
78749 /* #include "vdbeInt.h" */
78750
78751 #ifndef SQLITE_OMIT_INCRBLOB
78752
78753 /*
78754 ** Valid sqlite3_blob* handles point to Incrblob structures.
@@ -79233,10 +79343,12 @@
79343 ** than one background thread may be created. Specifically, there may be
79344 ** one background thread for each temporary file on disk, and one background
79345 ** thread to merge the output of each of the others to a single PMA for
79346 ** the main thread to read from.
79347 */
79348 /* #include "sqliteInt.h" */
79349 /* #include "vdbeInt.h" */
79350
79351 /*
79352 ** If SQLITE_DEBUG_SORTER_THREADS is defined, this module outputs various
79353 ** messages to stderr that may be helpful in understanding the performance
79354 ** characteristics of the sorter in multi-threaded mode.
@@ -81849,10 +81961,11 @@
81961 ** 1) The in-memory representation grows too large for the allocated
81962 ** buffer, or
81963 ** 2) The sqlite3JournalCreate() function is called.
81964 */
81965 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
81966 /* #include "sqliteInt.h" */
81967
81968
81969 /*
81970 ** A JournalFile object is a subclass of sqlite3_file used by
81971 ** as an open file handle for journal files.
@@ -82096,10 +82209,11 @@
82209 **
82210 ** This file contains code use to implement an in-memory rollback journal.
82211 ** The in-memory rollback journal is used to journal transactions for
82212 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
82213 */
82214 /* #include "sqliteInt.h" */
82215
82216 /* Forward references to internal structures */
82217 typedef struct MemJournal MemJournal;
82218 typedef struct FilePoint FilePoint;
82219 typedef struct FileChunk FileChunk;
@@ -82351,10 +82465,11 @@
82465 **
82466 *************************************************************************
82467 ** This file contains routines used for walking the parser tree for
82468 ** an SQL statement.
82469 */
82470 /* #include "sqliteInt.h" */
82471 /* #include <stdlib.h> */
82472 /* #include <string.h> */
82473
82474
82475 /*
@@ -82509,10 +82624,11 @@
82624 **
82625 ** This file contains routines used for walking the parser tree and
82626 ** resolve all identifiers by associating them with a particular
82627 ** table and column.
82628 */
82629 /* #include "sqliteInt.h" */
82630 /* #include <stdlib.h> */
82631 /* #include <string.h> */
82632
82633 /*
82634 ** Walk the expression tree pExpr and increase the aggregate function
@@ -84021,10 +84137,11 @@
84137 **
84138 *************************************************************************
84139 ** This file contains routines used for analyzing expressions and
84140 ** for generating VDBE code that evaluates expressions in SQLite.
84141 */
84142 /* #include "sqliteInt.h" */
84143
84144 /*
84145 ** Return the 'affinity' of the expression pExpr if any.
84146 **
84147 ** If pExpr is a column, a reference to a column via an 'AS' alias,
@@ -88183,10 +88300,11 @@
88300 **
88301 *************************************************************************
88302 ** This file contains C code routines that used to generate VDBE code
88303 ** that implements the ALTER TABLE command.
88304 */
88305 /* #include "sqliteInt.h" */
88306
88307 /*
88308 ** The code in this file only exists if we are not omitting the
88309 ** ALTER TABLE logic from the build.
88310 */
@@ -89145,10 +89263,11 @@
89263 ** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
89264 ** all contain just a single integer which is the same as the first
89265 ** integer in the equivalent columns in sqlite_stat4.
89266 */
89267 #ifndef SQLITE_OMIT_ANALYZE
89268 /* #include "sqliteInt.h" */
89269
89270 #if defined(SQLITE_ENABLE_STAT4)
89271 # define IsStat4 1
89272 # define IsStat3 0
89273 #elif defined(SQLITE_ENABLE_STAT3)
@@ -90910,10 +91029,11 @@
91029 ** May you share freely, never taking more than you give.
91030 **
91031 *************************************************************************
91032 ** This file contains code used to implement the ATTACH and DETACH commands.
91033 */
91034 /* #include "sqliteInt.h" */
91035
91036 #ifndef SQLITE_OMIT_ATTACH
91037 /*
91038 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
91039 ** is slightly different from resolving a normal SQL expression, because simple
@@ -91499,10 +91619,11 @@
91619 ** This file contains code used to implement the sqlite3_set_authorizer()
91620 ** API. This facility is an optional feature of the library. Embedded
91621 ** systems that do not need this facility may omit it by recompiling
91622 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
91623 */
91624 /* #include "sqliteInt.h" */
91625
91626 /*
91627 ** All of the code in this file may be omitted by defining a single
91628 ** macro.
91629 */
@@ -91769,10 +91890,11 @@
91890 ** creating ID lists
91891 ** BEGIN TRANSACTION
91892 ** COMMIT
91893 ** ROLLBACK
91894 */
91895 /* #include "sqliteInt.h" */
91896
91897 /*
91898 ** This routine is called when a new SQL statement is beginning to
91899 ** be parsed. Initialize the pParse structure as needed.
91900 */
@@ -96089,10 +96211,11 @@
96211 **
96212 ** This file contains functions used to access the internal hash tables
96213 ** of user defined functions and collation sequences.
96214 */
96215
96216 /* #include "sqliteInt.h" */
96217
96218 /*
96219 ** Invoke the 'collation needed' callback to request a collation sequence
96220 ** in the encoding enc of name zName, length nName.
96221 */
@@ -96566,10 +96689,11 @@
96689 **
96690 *************************************************************************
96691 ** This file contains C code routines that are called by the parser
96692 ** in order to generate code for DELETE FROM statements.
96693 */
96694 /* #include "sqliteInt.h" */
96695
96696 /*
96697 ** While a SrcList can in general represent multiple tables and subqueries
96698 ** (as in the FROM clause of a SELECT statement) in this case it contains
96699 ** the name of a single table, as one might find in an INSERT, DELETE,
@@ -97408,12 +97532,14 @@
97532 *************************************************************************
97533 ** This file contains the C-language implementations for many of the SQL
97534 ** functions of SQLite. (Some function, and in particular the date and
97535 ** time functions, are implemented separately.)
97536 */
97537 /* #include "sqliteInt.h" */
97538 /* #include <stdlib.h> */
97539 /* #include <assert.h> */
97540 /* #include "vdbeInt.h" */
97541
97542 /*
97543 ** Return the collating function associated with a function.
97544 */
97545 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
@@ -99205,10 +99331,11 @@
99331 **
99332 *************************************************************************
99333 ** This file contains code used by the compiler to add foreign key
99334 ** support to compiled SQL statements.
99335 */
99336 /* #include "sqliteInt.h" */
99337
99338 #ifndef SQLITE_OMIT_FOREIGN_KEY
99339 #ifndef SQLITE_OMIT_TRIGGER
99340
99341 /*
@@ -100609,10 +100736,11 @@
100736 **
100737 *************************************************************************
100738 ** This file contains C code routines that are called by the parser
100739 ** to handle INSERT statements in SQLite.
100740 */
100741 /* #include "sqliteInt.h" */
100742
100743 /*
100744 ** Generate code that will
100745 **
100746 ** (1) acquire a lock for table pTab then
@@ -102671,10 +102799,11 @@
102799 ** implement the programmer interface to the library. Routines in
102800 ** other files are for internal use by SQLite and should not be
102801 ** accessed by users of the library.
102802 */
102803
102804 /* #include "sqliteInt.h" */
102805
102806 /*
102807 ** Execute SQL code. Return one of the SQLITE_ success/failure
102808 ** codes. Also write an error message into memory obtained from
102809 ** malloc() and make *pzErrMsg point to that message.
@@ -102839,10 +102968,11 @@
102968 ** as extensions by SQLite should #include this file instead of
102969 ** sqlite3.h.
102970 */
102971 #ifndef _SQLITE3EXT_H_
102972 #define _SQLITE3EXT_H_
102973 /* #include "sqlite3.h" */
102974
102975 typedef struct sqlite3_api_routines sqlite3_api_routines;
102976
102977 /*
102978 ** The following structure holds pointers to all of the SQLite API
@@ -103345,10 +103475,11 @@
103475
103476 #endif /* _SQLITE3EXT_H_ */
103477
103478 /************** End of sqlite3ext.h ******************************************/
103479 /************** Continuing where we left off in loadext.c ********************/
103480 /* #include "sqliteInt.h" */
103481 /* #include <string.h> */
103482
103483 #ifndef SQLITE_OMIT_LOAD_EXTENSION
103484
103485 /*
@@ -104114,10 +104245,11 @@
104245 ** May you share freely, never taking more than you give.
104246 **
104247 *************************************************************************
104248 ** This file contains code used to implement the PRAGMA command.
104249 */
104250 /* #include "sqliteInt.h" */
104251
104252 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
104253 # if defined(__APPLE__)
104254 # define SQLITE_ENABLE_LOCKING_STYLE 1
104255 # else
@@ -104220,11 +104352,11 @@
104352 /* ePragFlag: */ 0,
104353 /* iArg: */ 0 },
104354 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
104355 { /* zName: */ "cache_size",
104356 /* ePragTyp: */ PragTyp_CACHE_SIZE,
104357 /* ePragFlag: */ 0,
104358 /* iArg: */ 0 },
104359 #endif
104360 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
104361 { /* zName: */ "cache_spill",
104362 /* ePragTyp: */ PragTyp_FLAG,
@@ -105290,10 +105422,11 @@
105422 ** of memory.
105423 */
105424 case PragTyp_CACHE_SIZE: {
105425 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
105426 if( !zRight ){
105427 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
105428 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
105429 }else{
105430 int size = sqlite3Atoi(zRight);
105431 pDb->pSchema->cache_size = size;
105432 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
@@ -106565,10 +106698,11 @@
106698 *************************************************************************
106699 ** This file contains the implementation of the sqlite3_prepare()
106700 ** interface, and routines that contribute to loading the database schema
106701 ** from disk.
106702 */
106703 /* #include "sqliteInt.h" */
106704
106705 /*
106706 ** Fill the InitData structure with an error message that indicates
106707 ** that the database is corrupt.
106708 */
@@ -107459,10 +107593,11 @@
107593 **
107594 *************************************************************************
107595 ** This file contains C code routines that are called by the parser
107596 ** to handle SELECT statements in SQLite.
107597 */
107598 /* #include "sqliteInt.h" */
107599
107600 /*
107601 ** Trace output macros
107602 */
107603 #if SELECTTRACE_ENABLED
@@ -108528,11 +108663,10 @@
108663 }
108664 }
108665 return pInfo;
108666 }
108667
 
108668 /*
108669 ** Name of the connection operator, used for error messages.
108670 */
108671 static const char *selectOpName(int id){
108672 char *z;
@@ -108542,11 +108676,10 @@
108676 case TK_EXCEPT: z = "EXCEPT"; break;
108677 default: z = "UNION"; break;
108678 }
108679 return z;
108680 }
 
108681
108682 #ifndef SQLITE_OMIT_EXPLAIN
108683 /*
108684 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
108685 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
@@ -109545,23 +109678,10 @@
109678 Parse *pParse, /* Parsing context */
109679 Select *p, /* The right-most of SELECTs to be coded */
109680 SelectDest *pDest /* What to do with query results */
109681 );
109682
 
 
 
 
 
 
 
 
 
 
 
 
 
109683 /*
109684 ** Handle the special case of a compound-select that originates from a
109685 ** VALUES clause. By handling this as a special case, we avoid deep
109686 ** recursion, and thus do not need to enforce the SQLITE_LIMIT_COMPOUND_SELECT
109687 ** on a VALUES clause.
@@ -109983,10 +110103,23 @@
110103 pDest->nSdst = dest.nSdst;
110104 sqlite3SelectDelete(db, pDelete);
110105 return rc;
110106 }
110107 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
110108
110109 /*
110110 ** Error message for when two or more terms of a compound select have different
110111 ** size result sets.
110112 */
110113 SQLITE_PRIVATE void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p){
110114 if( p->selFlags & SF_Values ){
110115 sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
110116 }else{
110117 sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
110118 " do not have the same number of result columns", selectOpName(p->op));
110119 }
110120 }
110121
110122 /*
110123 ** Code an output subroutine for a coroutine implementation of a
110124 ** SELECT statment.
110125 **
@@ -113077,10 +113210,11 @@
113210 ** interface routine of sqlite3_exec().
113211 **
113212 ** These routines are in a separate files so that they will not be linked
113213 ** if they are not used.
113214 */
113215 /* #include "sqliteInt.h" */
113216 /* #include <stdlib.h> */
113217 /* #include <string.h> */
113218
113219 #ifndef SQLITE_OMIT_GET_TABLE
113220
@@ -113273,10 +113407,11 @@
113407 ** May you share freely, never taking more than you give.
113408 **
113409 *************************************************************************
113410 ** This file contains the implementation for TRIGGERs
113411 */
113412 /* #include "sqliteInt.h" */
113413
113414 #ifndef SQLITE_OMIT_TRIGGER
113415 /*
113416 ** Delete a linked list of TriggerStep structures.
113417 */
@@ -114396,10 +114531,11 @@
114531 **
114532 *************************************************************************
114533 ** This file contains C code routines that are called by the parser
114534 ** to handle UPDATE statements.
114535 */
114536 /* #include "sqliteInt.h" */
114537
114538 #ifndef SQLITE_OMIT_VIRTUALTABLE
114539 /* Forward declaration */
114540 static void updateVirtualTable(
114541 Parse *pParse, /* The parsing context */
@@ -115172,10 +115308,12 @@
115308 ** This file contains code used to implement the VACUUM command.
115309 **
115310 ** Most of the code in this file may be omitted by defining the
115311 ** SQLITE_OMIT_VACUUM macro.
115312 */
115313 /* #include "sqliteInt.h" */
115314 /* #include "vdbeInt.h" */
115315
115316 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
115317 /*
115318 ** Finalize a prepared statement. If there was an error, store the
115319 ** text of the error message in *pzErrMsg. Return the result code.
@@ -115544,10 +115682,11 @@
115682 **
115683 *************************************************************************
115684 ** This file contains code used to help implement virtual tables.
115685 */
115686 #ifndef SQLITE_OMIT_VIRTUALTABLE
115687 /* #include "sqliteInt.h" */
115688
115689 /*
115690 ** Before a virtual table xCreate() or xConnect() method is invoked, the
115691 ** sqlite3.pVtabCtx member variable is set to point to an instance of
115692 ** this struct allocated on the stack. It is used by the implementation of
@@ -116702,10 +116841,11 @@
116841 ** This file was split off from where.c on 2015-06-06 in order to reduce the
116842 ** size of where.c and make it easier to edit. This file contains the routines
116843 ** that actually generate the bulk of the WHERE loop code. The original where.c
116844 ** file retains the code that does query planning and analysis.
116845 */
116846 /* #include "sqliteInt.h" */
116847 /************** Include whereInt.h in the middle of wherecode.c **************/
116848 /************** Begin file whereInt.h ****************************************/
116849 /*
116850 ** 2013-11-12
116851 **
@@ -118513,11 +118653,15 @@
118653
118654 /* Read the PK into an array of temp registers. */
118655 r = sqlite3GetTempRange(pParse, nPk);
118656 for(iPk=0; iPk<nPk; iPk++){
118657 int iCol = pPk->aiColumn[iPk];
118658 int rx;
118659 rx = sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur,r+iPk,0);
118660 if( rx!=r+iPk ){
118661 sqlite3VdbeAddOp2(v, OP_SCopy, rx, r+iPk);
118662 }
118663 }
118664
118665 /* Check if the temp table already contains this key. If so,
118666 ** the row has already been included in the result set and
118667 ** can be ignored (by jumping past the Gosub below). Otherwise,
@@ -118737,10 +118881,12 @@
118881 **
118882 ** This file was originally part of where.c but was split out to improve
118883 ** readability and editabiliity. This file contains utility routines for
118884 ** analyzing Expr objects in the WHERE clause.
118885 */
118886 /* #include "sqliteInt.h" */
118887 /* #include "whereInt.h" */
118888
118889 /* Forward declarations */
118890 static void exprAnalyze(SrcList*, WhereClause*, int);
118891
118892 /*
@@ -119987,10 +120133,12 @@
120133 ** generating the code that loops through a table looking for applicable
120134 ** rows. Indices are selected and used to speed the search when doing
120135 ** so is applicable. Because this module is responsible for selecting
120136 ** indices, you might also think of this module as the "query optimizer".
120137 */
120138 /* #include "sqliteInt.h" */
120139 /* #include "whereInt.h" */
120140
120141 /* Forward declaration of methods */
120142 static int whereLoopResize(sqlite3*, WhereLoop*, int);
120143
120144 /* Test variable that can be set to enable WHERE tracing */
@@ -124486,10 +124634,11 @@
124634 */
124635 /* First off, code is included that follows the "include" declaration
124636 ** in the input grammar file. */
124637 /* #include <stdio.h> */
124638
124639 /* #include "sqliteInt.h" */
124640
124641 /*
124642 ** Disable all error recovery processing in the parser push-down
124643 ** automaton.
124644 */
@@ -128071,10 +128220,11 @@
128220 **
128221 ** This file contains C code that splits an SQL input string up into
128222 ** individual tokens and sends those tokens one-by-one over to the
128223 ** parser for analysis.
128224 */
128225 /* #include "sqliteInt.h" */
128226 /* #include <stdlib.h> */
128227
128228 /*
128229 ** The charMap() macro maps alphabetic characters into their
128230 ** lower-case ASCII equivalent. On ASCII machines, this is just
@@ -128880,10 +129030,11 @@
129030 ** This file contains C code that implements the sqlite3_complete() API.
129031 ** This code used to be part of the tokenizer.c source file. But by
129032 ** separating it out, the code will be automatically omitted from
129033 ** static links that do not use it.
129034 */
129035 /* #include "sqliteInt.h" */
129036 #ifndef SQLITE_OMIT_COMPLETE
129037
129038 /*
129039 ** This is defined in tokenize.c. We just have to import the definition.
129040 */
@@ -129170,10 +129321,11 @@
129321 ** Main file for the SQLite library. The routines in this file
129322 ** implement the programmer interface to the library. Routines in
129323 ** other files are for internal use by SQLite and should not be
129324 ** accessed by users of the library.
129325 */
129326 /* #include "sqliteInt.h" */
129327
129328 #ifdef SQLITE_ENABLE_FTS3
129329 /************** Include fts3.h in the middle of main.c ***********************/
129330 /************** Begin file fts3.h ********************************************/
129331 /*
@@ -129189,10 +129341,11 @@
129341 ******************************************************************************
129342 **
129343 ** This header file is used by programs that want to link against the
129344 ** FTS3 library. All it does is declare the sqlite3Fts3Init() interface.
129345 */
129346 /* #include "sqlite3.h" */
129347
129348 #if 0
129349 extern "C" {
129350 #endif /* __cplusplus */
129351
@@ -129221,10 +129374,11 @@
129374 ******************************************************************************
129375 **
129376 ** This header file is used by programs that want to link against the
129377 ** RTREE library. All it does is declare the sqlite3RtreeInit() interface.
129378 */
129379 /* #include "sqlite3.h" */
129380
129381 #if 0
129382 extern "C" {
129383 #endif /* __cplusplus */
129384
@@ -129253,10 +129407,11 @@
129407 ******************************************************************************
129408 **
129409 ** This header file is used by programs that want to link against the
129410 ** ICU extension. All it does is declare the sqlite3IcuInit() interface.
129411 */
129412 /* #include "sqlite3.h" */
129413
129414 #if 0
129415 extern "C" {
129416 #endif /* __cplusplus */
129417
@@ -129885,10 +130040,11 @@
130040 ** space for the lookaside memory is obtained from sqlite3_malloc().
130041 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
130042 ** the lookaside memory.
130043 */
130044 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
130045 #ifndef SQLITE_OMIT_LOOKASIDE
130046 void *pStart;
130047 if( db->lookaside.nOut ){
130048 return SQLITE_BUSY;
130049 }
130050 /* Free any existing lookaside buffer for this handle before
@@ -129935,10 +130091,11 @@
130091 db->lookaside.pStart = db;
130092 db->lookaside.pEnd = db;
130093 db->lookaside.bEnabled = 0;
130094 db->lookaside.bMalloced = 0;
130095 }
130096 #endif /* SQLITE_OMIT_LOOKASIDE */
130097 return SQLITE_OK;
130098 }
130099
130100 /*
130101 ** Return the mutex associated with a database connection.
@@ -133045,10 +133202,12 @@
133202 *************************************************************************
133203 **
133204 ** This file contains the implementation of the sqlite3_unlock_notify()
133205 ** API method and its associated functionality.
133206 */
133207 /* #include "sqliteInt.h" */
133208 /* #include "btreeInt.h" */
133209
133210 /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
133211 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
133212
133213 /*
@@ -133688,13 +133847,15 @@
133847
133848 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
133849
133850 /* If not building as part of the core, include sqlite3ext.h. */
133851 #ifndef SQLITE_CORE
133852 /* # include "sqlite3ext.h" */
133853 SQLITE_EXTENSION_INIT3
133854 #endif
133855
133856 /* #include "sqlite3.h" */
133857 /************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
133858 /************** Begin file fts3_tokenizer.h **********************************/
133859 /*
133860 ** 2006 July 10
133861 **
@@ -133719,10 +133880,11 @@
133880
133881 /* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
133882 ** If tokenizers are to be allowed to call sqlite3_*() functions, then
133883 ** we will need a way to register the API consistently.
133884 */
133885 /* #include "sqlite3.h" */
133886
133887 /*
133888 ** Structures used by the tokenizer interface. When a new tokenizer
133889 ** implementation is registered, the caller provides a pointer to
133890 ** an sqlite3_tokenizer_module containing pointers to the callback
@@ -134559,11 +134721,13 @@
134721 /* #include <stddef.h> */
134722 /* #include <stdio.h> */
134723 /* #include <string.h> */
134724 /* #include <stdarg.h> */
134725
134726 /* #include "fts3.h" */
134727 #ifndef SQLITE_CORE
134728 /* # include "sqlite3ext.h" */
134729 SQLITE_EXTENSION_INIT1
134730 #endif
134731
134732 static int fts3EvalNext(Fts3Cursor *pCsr);
134733 static int fts3EvalStart(Fts3Cursor *pCsr);
@@ -138606,10 +138770,11 @@
138770 if( p==0 ){
138771 p = aDoclist;
138772 p += sqlite3Fts3GetVarint(p, piDocid);
138773 }else{
138774 fts3PoslistCopy(0, &p);
138775 while( p<&aDoclist[nDoclist] && *p==0 ) p++;
138776 if( p>=&aDoclist[nDoclist] ){
138777 *pbEof = 1;
138778 }else{
138779 sqlite3_int64 iVar;
138780 p += sqlite3Fts3GetVarint(p, &iVar);
@@ -140012,14 +140177,14 @@
140177 pIter = pPhrase->doclist.pList;
140178 if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
140179 int rc = SQLITE_OK;
140180 int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
140181 int bOr = 0;
 
140182 u8 bTreeEof = 0;
140183 Fts3Expr *p; /* Used to iterate from pExpr to root */
140184 Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
140185 int bMatch;
140186
140187 /* Check if this phrase descends from an OR expression node. If not,
140188 ** return NULL. Otherwise, the entry that corresponds to docid
140189 ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
140190 ** tree that the node is part of has been marked as EOF, but the node
@@ -140049,35 +140214,51 @@
140214 fts3EvalNextRow(pCsr, pNear, &rc);
140215 }
140216 }
140217 if( rc!=SQLITE_OK ) return rc;
140218
140219 bMatch = 1;
140220 for(p=pNear; p; p=p->pLeft){
140221 u8 bEof = 0;
140222 Fts3Expr *pTest = p;
140223 Fts3Phrase *pPh;
140224 assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
140225 if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
140226 assert( pTest->eType==FTSQUERY_PHRASE );
140227 pPh = pTest->pPhrase;
140228
140229 pIter = pPh->pOrPoslist;
140230 iDocid = pPh->iOrDocid;
140231 if( pCsr->bDesc==bDescDoclist ){
140232 bEof = !pPh->doclist.nAll ||
140233 (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
140234 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
140235 sqlite3Fts3DoclistNext(
140236 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
140237 &pIter, &iDocid, &bEof
140238 );
140239 }
140240 }else{
140241 bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
140242 while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
140243 int dummy;
140244 sqlite3Fts3DoclistPrev(
140245 bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
140246 &pIter, &iDocid, &dummy, &bEof
140247 );
140248 }
140249 }
140250 pPh->pOrPoslist = pIter;
140251 pPh->iOrDocid = iDocid;
140252 if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
140253 }
140254
140255 if( bMatch ){
140256 pIter = pPhrase->pOrPoslist;
140257 }else{
140258 pIter = 0;
140259 }
140260 }
140261 if( pIter==0 ) return SQLITE_OK;
140262
140263 if( *pIter==0x01 ){
140264 pIter++;
@@ -140161,10 +140342,11 @@
140342 ** May you share freely, never taking more than you give.
140343 **
140344 ******************************************************************************
140345 **
140346 */
140347 /* #include "fts3Int.h" */
140348 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
140349
140350 /* #include <string.h> */
140351 /* #include <assert.h> */
140352
@@ -140717,10 +140899,11 @@
140899 ** This module contains code that implements a parser for fts3 query strings
140900 ** (the right-hand argument to the MATCH operator). Because the supported
140901 ** syntax is relatively simple, the whole tokenizer/parser system is
140902 ** hand-coded.
140903 */
140904 /* #include "fts3Int.h" */
140905 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
140906
140907 /*
140908 ** By default, this module parses the legacy syntax that has been
140909 ** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
@@ -142010,16 +142193,18 @@
142193 ** (in which case SQLITE_CORE is not defined), or
142194 **
142195 ** * The FTS3 module is being built into the core of
142196 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
142197 */
142198 /* #include "fts3Int.h" */
142199 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
142200
142201 /* #include <assert.h> */
142202 /* #include <stdlib.h> */
142203 /* #include <string.h> */
142204
142205 /* #include "fts3_hash.h" */
142206
142207 /*
142208 ** Malloc and Free functions
142209 */
142210 static void *fts3HashMalloc(int n){
@@ -142393,17 +142578,19 @@
142578 ** (in which case SQLITE_CORE is not defined), or
142579 **
142580 ** * The FTS3 module is being built into the core of
142581 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
142582 */
142583 /* #include "fts3Int.h" */
142584 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
142585
142586 /* #include <assert.h> */
142587 /* #include <stdlib.h> */
142588 /* #include <stdio.h> */
142589 /* #include <string.h> */
142590
142591 /* #include "fts3_tokenizer.h" */
142592
142593 /*
142594 ** Class derived from sqlite3_tokenizer
142595 */
142596 typedef struct porter_tokenizer {
@@ -143057,10 +143244,11 @@
143244 ** (in which case SQLITE_CORE is not defined), or
143245 **
143246 ** * The FTS3 module is being built into the core of
143247 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
143248 */
143249 /* #include "fts3Int.h" */
143250 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
143251
143252 /* #include <assert.h> */
143253 /* #include <string.h> */
143254
@@ -143552,17 +143740,19 @@
143740 ** (in which case SQLITE_CORE is not defined), or
143741 **
143742 ** * The FTS3 module is being built into the core of
143743 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
143744 */
143745 /* #include "fts3Int.h" */
143746 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
143747
143748 /* #include <assert.h> */
143749 /* #include <stdlib.h> */
143750 /* #include <stdio.h> */
143751 /* #include <string.h> */
143752
143753 /* #include "fts3_tokenizer.h" */
143754
143755 typedef struct simple_tokenizer {
143756 sqlite3_tokenizer base;
143757 char delim[128]; /* flag ASCII delimiters */
143758 } simple_tokenizer;
@@ -143803,10 +143993,11 @@
143993 ** end: Byte offset of the byte immediately following the end of the
143994 ** token within the input string.
143995 ** pos: Token offset of token within input.
143996 **
143997 */
143998 /* #include "fts3Int.h" */
143999 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
144000
144001 /* #include <string.h> */
144002 /* #include <assert.h> */
144003
@@ -144238,10 +144429,11 @@
144429 ** tables. It also contains code to merge FTS3 b-tree segments. Some
144430 ** of the sub-routines used to merge segments are also used by the query
144431 ** code in fts3.c.
144432 */
144433
144434 /* #include "fts3Int.h" */
144435 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
144436
144437 /* #include <string.h> */
144438 /* #include <assert.h> */
144439 /* #include <stdlib.h> */
@@ -149898,10 +150090,11 @@
150090 ** May you share freely, never taking more than you give.
150091 **
150092 ******************************************************************************
150093 */
150094
150095 /* #include "fts3Int.h" */
150096 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
150097
150098 /* #include <string.h> */
150099 /* #include <assert.h> */
150100
@@ -151610,17 +151803,19 @@
151803 ** Implementation of the "unicode" full-text-search tokenizer.
151804 */
151805
151806 #ifndef SQLITE_DISABLE_FTS3_UNICODE
151807
151808 /* #include "fts3Int.h" */
151809 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
151810
151811 /* #include <assert.h> */
151812 /* #include <stdlib.h> */
151813 /* #include <stdio.h> */
151814 /* #include <string.h> */
151815
151816 /* #include "fts3_tokenizer.h" */
151817
151818 /*
151819 ** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
151820 ** from the sqlite3 source file utf.c. If this file is compiled as part
151821 ** of the amalgamation, they are not required.
@@ -152412,12 +152607,14 @@
152607 */
152608
152609 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
152610
152611 #ifndef SQLITE_CORE
152612 /* #include "sqlite3ext.h" */
152613 SQLITE_EXTENSION_INIT1
152614 #else
152615 /* #include "sqlite3.h" */
152616 #endif
152617
152618 /* #include <string.h> */
152619 /* #include <assert.h> */
152620 /* #include <stdio.h> */
@@ -155909,12 +156106,14 @@
156106 #include <unicode/ucol.h>
156107
156108 /* #include <assert.h> */
156109
156110 #ifndef SQLITE_CORE
156111 /* #include "sqlite3ext.h" */
156112 SQLITE_EXTENSION_INIT1
156113 #else
156114 /* #include "sqlite3.h" */
156115 #endif
156116
156117 /*
156118 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
156119 ** operator.
@@ -156386,15 +156585,17 @@
156585 ** May you share freely, never taking more than you give.
156586 **
156587 *************************************************************************
156588 ** This file implements a tokenizer for fts3 based on the ICU library.
156589 */
156590 /* #include "fts3Int.h" */
156591 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
156592 #ifdef SQLITE_ENABLE_ICU
156593
156594 /* #include <assert.h> */
156595 /* #include <string.h> */
156596 /* #include "fts3_tokenizer.h" */
156597
156598 #include <unicode/ubrk.h>
156599 /* #include <unicode/ucol.h> */
156600 /* #include <unicode/ustring.h> */
156601 #include <unicode/utf16.h>
@@ -156635,11 +156836,11 @@
156836
156837 #endif /* defined(SQLITE_ENABLE_ICU) */
156838 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
156839
156840 /************** End of fts3_icu.c ********************************************/
156841 /************** Begin file sqlite3rbu.c **************************************/
156842 /*
156843 ** 2014 August 30
156844 **
156845 ** The author disclaims copyright to this source code. In place of
156846 ** a legal notice, here is a blessing:
@@ -156651,15 +156852,15 @@
156852 *************************************************************************
156853 **
156854 **
156855 ** OVERVIEW
156856 **
156857 ** The RBU extension requires that the RBU update be packaged as an
156858 ** SQLite database. The tables it expects to find are described in
156859 ** sqlite3rbu.h. Essentially, for each table xyz in the target database
156860 ** that the user wishes to write to, a corresponding data_xyz table is
156861 ** created in the RBU database and populated with one row for each row to
156862 ** update, insert or delete from the target table.
156863 **
156864 ** The update proceeds in three stages:
156865 **
156866 ** 1) The database is updated. The modified database pages are written
@@ -156667,70 +156868,71 @@
156868 ** that it is named "<database>-oal" instead of "<database>-wal".
156869 ** Because regular SQLite clients do not look for file named
156870 ** "<database>-oal", they go on using the original database in
156871 ** rollback mode while the *-oal file is being generated.
156872 **
156873 ** During this stage RBU does not update the database by writing
156874 ** directly to the target tables. Instead it creates "imposter"
156875 ** tables using the SQLITE_TESTCTRL_IMPOSTER interface that it uses
156876 ** to update each b-tree individually. All updates required by each
156877 ** b-tree are completed before moving on to the next, and all
156878 ** updates are done in sorted key order.
156879 **
156880 ** 2) The "<database>-oal" file is moved to the equivalent "<database>-wal"
156881 ** location using a call to rename(2). Before doing this the RBU
156882 ** module takes an EXCLUSIVE lock on the database file, ensuring
156883 ** that there are no other active readers.
156884 **
156885 ** Once the EXCLUSIVE lock is released, any other database readers
156886 ** detect the new *-wal file and read the database in wal mode. At
156887 ** this point they see the new version of the database - including
156888 ** the updates made as part of the RBU update.
156889 **
156890 ** 3) The new *-wal file is checkpointed. This proceeds in the same way
156891 ** as a regular database checkpoint, except that a single frame is
156892 ** checkpointed each time sqlite3rbu_step() is called. If the RBU
156893 ** handle is closed before the entire *-wal file is checkpointed,
156894 ** the checkpoint progress is saved in the RBU database and the
156895 ** checkpoint can be resumed by another RBU client at some point in
156896 ** the future.
156897 **
156898 ** POTENTIAL PROBLEMS
156899 **
156900 ** The rename() call might not be portable. And RBU is not currently
156901 ** syncing the directory after renaming the file.
156902 **
156903 ** When state is saved, any commit to the *-oal file and the commit to
156904 ** the RBU update database are not atomic. So if the power fails at the
156905 ** wrong moment they might get out of sync. As the main database will be
156906 ** committed before the RBU update database this will likely either just
156907 ** pass unnoticed, or result in SQLITE_CONSTRAINT errors (due to UNIQUE
156908 ** constraint violations).
156909 **
156910 ** If some client does modify the target database mid RBU update, or some
156911 ** other error occurs, the RBU extension will keep throwing errors. It's
156912 ** not really clear how to get out of this state. The system could just
156913 ** by delete the RBU update database and *-oal file and have the device
156914 ** download the update again and start over.
156915 **
156916 ** At present, for an UPDATE, both the new.* and old.* records are
156917 ** collected in the rbu_xyz table. And for both UPDATEs and DELETEs all
156918 ** fields are collected. This means we're probably writing a lot more
156919 ** data to disk when saving the state of an ongoing update to the RBU
156920 ** update database than is strictly necessary.
156921 **
156922 */
156923
156924 /* #include <assert.h> */
156925 /* #include <string.h> */
156926 /* #include <stdio.h> */
156927 /* #include <unistd.h> */
156928
156929 /* #include "sqlite3.h" */
156930
156931 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU)
156932 /************** Include sqlite3rbu.h in the middle of sqlite3rbu.c ***********/
156933 /************** Begin file sqlite3rbu.h **************************************/
156934 /*
156935 ** 2014 August 30
156936 **
156937 ** The author disclaims copyright to this source code. In place of
156938 ** a legal notice, here is a blessing:
@@ -156739,11 +156941,11 @@
156941 ** May you find forgiveness for yourself and forgive others.
156942 ** May you share freely, never taking more than you give.
156943 **
156944 *************************************************************************
156945 **
156946 ** This file contains the public interface for the RBU extension.
156947 */
156948
156949 /*
156950 ** SUMMARY
156951 **
@@ -156776,18 +156978,18 @@
156978 ** mobile device that is frequently rebooted. Even after the writer process
156979 ** has committed one or more sub-transactions, other database clients continue
156980 ** to read from the original database snapshot. In other words, partially
156981 ** applied transactions are not visible to other clients.
156982 **
156983 ** "RBU" stands for "Over The Air" update. As in a large database update
156984 ** transmitted via a wireless network to a mobile device. A transaction
156985 ** applied using this extension is hence refered to as an "RBU update".
156986 **
156987 **
156988 ** LIMITATIONS
156989 **
156990 ** An "RBU update" transaction is subject to the following limitations:
156991 **
156992 ** * The transaction must consist of INSERT, UPDATE and DELETE operations
156993 ** only.
156994 **
156995 ** * INSERT statements may not use any default values.
@@ -156808,430 +157010,430 @@
157010 ** * No constraint handling mode except for "OR ROLLBACK" is supported.
157011 **
157012 **
157013 ** PREPARATION
157014 **
157015 ** An "RBU update" is stored as a separate SQLite database. A database
157016 ** containing an RBU update is an "RBU database". For each table in the
157017 ** target database to be updated, the RBU database should contain a table
157018 ** named "data_<target name>" containing the same set of columns as the
157019 ** target table, and one more - "rbu_control". The data_% table should
157020 ** have no PRIMARY KEY or UNIQUE constraints, but each column should have
157021 ** the same type as the corresponding column in the target database.
157022 ** The "rbu_control" column should have no type at all. For example, if
157023 ** the target database contains:
157024 **
157025 ** CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c UNIQUE);
157026 **
157027 ** Then the RBU database should contain:
157028 **
157029 ** CREATE TABLE data_t1(a INTEGER, b TEXT, c, rbu_control);
157030 **
157031 ** The order of the columns in the data_% table does not matter.
157032 **
157033 ** If the target database table is a virtual table or a table that has no
157034 ** PRIMARY KEY declaration, the data_% table must also contain a column
157035 ** named "rbu_rowid". This column is mapped to the tables implicit primary
157036 ** key column - "rowid". Virtual tables for which the "rowid" column does
157037 ** not function like a primary key value cannot be updated using RBU. For
157038 ** example, if the target db contains either of the following:
157039 **
157040 ** CREATE VIRTUAL TABLE x1 USING fts3(a, b);
157041 ** CREATE TABLE x1(a, b)
157042 **
157043 ** then the RBU database should contain:
157044 **
157045 ** CREATE TABLE data_x1(a, b, rbu_rowid, rbu_control);
157046 **
157047 ** All non-hidden columns (i.e. all columns matched by "SELECT *") of the
157048 ** target table must be present in the input table. For virtual tables,
157049 ** hidden columns are optional - they are updated by RBU if present in
157050 ** the input table, or not otherwise. For example, to write to an fts4
157051 ** table with a hidden languageid column such as:
157052 **
157053 ** CREATE VIRTUAL TABLE ft1 USING fts4(a, b, languageid='langid');
157054 **
157055 ** Either of the following input table schemas may be used:
157056 **
157057 ** CREATE TABLE data_ft1(a, b, langid, rbu_rowid, rbu_control);
157058 ** CREATE TABLE data_ft1(a, b, rbu_rowid, rbu_control);
157059 **
157060 ** For each row to INSERT into the target database as part of the RBU
157061 ** update, the corresponding data_% table should contain a single record
157062 ** with the "rbu_control" column set to contain integer value 0. The
157063 ** other columns should be set to the values that make up the new record
157064 ** to insert.
157065 **
157066 ** If the target database table has an INTEGER PRIMARY KEY, it is not
157067 ** possible to insert a NULL value into the IPK column. Attempting to
157068 ** do so results in an SQLITE_MISMATCH error.
157069 **
157070 ** For each row to DELETE from the target database as part of the RBU
157071 ** update, the corresponding data_% table should contain a single record
157072 ** with the "rbu_control" column set to contain integer value 1. The
157073 ** real primary key values of the row to delete should be stored in the
157074 ** corresponding columns of the data_% table. The values stored in the
157075 ** other columns are not used.
157076 **
157077 ** For each row to UPDATE from the target database as part of the RBU
157078 ** update, the corresponding data_% table should contain a single record
157079 ** with the "rbu_control" column set to contain a value of type text.
157080 ** The real primary key values identifying the row to update should be
157081 ** stored in the corresponding columns of the data_% table row, as should
157082 ** the new values of all columns being update. The text value in the
157083 ** "rbu_control" column must contain the same number of characters as
157084 ** there are columns in the target database table, and must consist entirely
157085 ** of 'x' and '.' characters (or in some special cases 'd' - see below). For
157086 ** each column that is being updated, the corresponding character is set to
157087 ** 'x'. For those that remain as they are, the corresponding character of the
157088 ** rbu_control value should be set to '.'. For example, given the tables
157089 ** above, the update statement:
157090 **
157091 ** UPDATE t1 SET c = 'usa' WHERE a = 4;
157092 **
157093 ** is represented by the data_t1 row created by:
157094 **
157095 ** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..x');
157096 **
157097 ** Instead of an 'x' character, characters of the rbu_control value specified
157098 ** for UPDATEs may also be set to 'd'. In this case, instead of updating the
157099 ** target table with the value stored in the corresponding data_% column, the
157100 ** user-defined SQL function "rbu_delta()" is invoked and the result stored in
157101 ** the target table column. rbu_delta() is invoked with two arguments - the
157102 ** original value currently stored in the target table column and the
157103 ** value specified in the data_xxx table.
157104 **
157105 ** For example, this row:
157106 **
157107 ** INSERT INTO data_t1(a, b, c, rbu_control) VALUES(4, NULL, 'usa', '..d');
157108 **
157109 ** is similar to an UPDATE statement such as:
157110 **
157111 ** UPDATE t1 SET c = rbu_delta(c, 'usa') WHERE a = 4;
157112 **
157113 ** If the target database table is a virtual table or a table with no PRIMARY
157114 ** KEY, the rbu_control value should not include a character corresponding
157115 ** to the rbu_rowid value. For example, this:
157116 **
157117 ** INSERT INTO data_ft1(a, b, rbu_rowid, rbu_control)
157118 ** VALUES(NULL, 'usa', 12, '.x');
157119 **
157120 ** causes a result similar to:
157121 **
157122 ** UPDATE ft1 SET b = 'usa' WHERE rowid = 12;
157123 **
157124 ** The data_xxx tables themselves should have no PRIMARY KEY declarations.
157125 ** However, RBU is more efficient if reading the rows in from each data_xxx
157126 ** table in "rowid" order is roughly the same as reading them sorted by
157127 ** the PRIMARY KEY of the corresponding target database table. In other
157128 ** words, rows should be sorted using the destination table PRIMARY KEY
157129 ** fields before they are inserted into the data_xxx tables.
157130 **
157131 ** USAGE
157132 **
157133 ** The API declared below allows an application to apply an RBU update
157134 ** stored on disk to an existing target database. Essentially, the
157135 ** application:
157136 **
157137 ** 1) Opens an RBU handle using the sqlite3rbu_open() function.
157138 **
157139 ** 2) Registers any required virtual table modules with the database
157140 ** handle returned by sqlite3rbu_db(). Also, if required, register
157141 ** the rbu_delta() implementation.
157142 **
157143 ** 3) Calls the sqlite3rbu_step() function one or more times on
157144 ** the new handle. Each call to sqlite3rbu_step() performs a single
157145 ** b-tree operation, so thousands of calls may be required to apply
157146 ** a complete update.
157147 **
157148 ** 4) Calls sqlite3rbu_close() to close the RBU update handle. If
157149 ** sqlite3rbu_step() has been called enough times to completely
157150 ** apply the update to the target database, then the RBU database
157151 ** is marked as fully applied. Otherwise, the state of the RBU
157152 ** update application is saved in the RBU database for later
157153 ** resumption.
157154 **
157155 ** See comments below for more detail on APIs.
157156 **
157157 ** If an update is only partially applied to the target database by the
157158 ** time sqlite3rbu_close() is called, various state information is saved
157159 ** within the RBU database. This allows subsequent processes to automatically
157160 ** resume the RBU update from where it left off.
157161 **
157162 ** To remove all RBU extension state information, returning an RBU database
157163 ** to its original contents, it is sufficient to drop all tables that begin
157164 ** with the prefix "rbu_"
157165 **
157166 ** DATABASE LOCKING
157167 **
157168 ** An RBU update may not be applied to a database in WAL mode. Attempting
157169 ** to do so is an error (SQLITE_ERROR).
157170 **
157171 ** While an RBU handle is open, a SHARED lock may be held on the target
157172 ** database file. This means it is possible for other clients to read the
157173 ** database, but not to write it.
157174 **
157175 ** If an RBU update is started and then suspended before it is completed,
157176 ** then an external client writes to the database, then attempting to resume
157177 ** the suspended RBU update is also an error (SQLITE_BUSY).
157178 */
157179
157180 #ifndef _SQLITE3RBU_H
157181 #define _SQLITE3RBU_H
157182
157183 /* #include "sqlite3.h" ** Required for error code definitions ** */
157184
157185 typedef struct sqlite3rbu sqlite3rbu;
157186
157187 /*
157188 ** Open an RBU handle.
157189 **
157190 ** Argument zTarget is the path to the target database. Argument zRbu is
157191 ** the path to the RBU database. Each call to this function must be matched
157192 ** by a call to sqlite3rbu_close(). When opening the databases, RBU passes
157193 ** the SQLITE_CONFIG_URI flag to sqlite3_open_v2(). So if either zTarget
157194 ** or zRbu begin with "file:", it will be interpreted as an SQLite
157195 ** database URI, not a regular file name.
157196 **
157197 ** If the zState argument is passed a NULL value, the RBU extension stores
157198 ** the current state of the update (how many rows have been updated, which
157199 ** indexes are yet to be updated etc.) within the RBU database itself. This
157200 ** can be convenient, as it means that the RBU application does not need to
157201 ** organize removing a separate state file after the update is concluded.
157202 ** Or, if zState is non-NULL, it must be a path to a database file in which
157203 ** the RBU extension can store the state of the update.
157204 **
157205 ** When resuming an RBU update, the zState argument must be passed the same
157206 ** value as when the RBU update was started.
157207 **
157208 ** Once the RBU update is finished, the RBU extension does not
157209 ** automatically remove any zState database file, even if it created it.
157210 **
157211 ** By default, RBU uses the default VFS to access the files on disk. To
157212 ** use a VFS other than the default, an SQLite "file:" URI containing a
157213 ** "vfs=..." option may be passed as the zTarget option.
157214 **
157215 ** IMPORTANT NOTE FOR ZIPVFS USERS: The RBU extension works with all of
157216 ** SQLite's built-in VFSs, including the multiplexor VFS. However it does
157217 ** not work out of the box with zipvfs. Refer to the comment describing
157218 ** the zipvfs_create_vfs() API below for details on using RBU with zipvfs.
157219 */
157220 SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_open(
157221 const char *zTarget,
157222 const char *zRbu,
157223 const char *zState
157224 );
157225
157226 /*
157227 ** Internally, each RBU connection uses a separate SQLite database
157228 ** connection to access the target and rbu update databases. This
157229 ** API allows the application direct access to these database handles.
157230 **
157231 ** The first argument passed to this function must be a valid, open, RBU
157232 ** handle. The second argument should be passed zero to access the target
157233 ** database handle, or non-zero to access the rbu update database handle.
157234 ** Accessing the underlying database handles may be useful in the
157235 ** following scenarios:
157236 **
157237 ** * If any target tables are virtual tables, it may be necessary to
157238 ** call sqlite3_create_module() on the target database handle to
157239 ** register the required virtual table implementations.
157240 **
157241 ** * If the data_xxx tables in the RBU source database are virtual
157242 ** tables, the application may need to call sqlite3_create_module() on
157243 ** the rbu update db handle to any required virtual table
157244 ** implementations.
157245 **
157246 ** * If the application uses the "rbu_delta()" feature described above,
157247 ** it must use sqlite3_create_function() or similar to register the
157248 ** rbu_delta() implementation with the target database handle.
157249 **
157250 ** If an error has occurred, either while opening or stepping the RBU object,
157251 ** this function may return NULL. The error code and message may be collected
157252 ** when sqlite3rbu_close() is called.
157253 */
157254 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3rbu_db(sqlite3rbu*, int bRbu);
157255
157256 /*
157257 ** Do some work towards applying the RBU update to the target db.
157258 **
157259 ** Return SQLITE_DONE if the update has been completely applied, or
157260 ** SQLITE_OK if no error occurs but there remains work to do to apply
157261 ** the RBU update. If an error does occur, some other error code is
157262 ** returned.
157263 **
157264 ** Once a call to sqlite3rbu_step() has returned a value other than
157265 ** SQLITE_OK, all subsequent calls on the same RBU handle are no-ops
157266 ** that immediately return the same value.
157267 */
157268 SQLITE_API int SQLITE_STDCALL sqlite3rbu_step(sqlite3rbu *pRbu);
157269
157270 /*
157271 ** Close an RBU handle.
157272 **
157273 ** If the RBU update has been completely applied, mark the RBU database
157274 ** as fully applied. Otherwise, assuming no error has occurred, save the
157275 ** current state of the RBU update appliation to the RBU database.
157276 **
157277 ** If an error has already occurred as part of an sqlite3rbu_step()
157278 ** or sqlite3rbu_open() call, or if one occurs within this function, an
157279 ** SQLite error code is returned. Additionally, *pzErrmsg may be set to
157280 ** point to a buffer containing a utf-8 formatted English language error
157281 ** message. It is the responsibility of the caller to eventually free any
157282 ** such buffer using sqlite3_free().
157283 **
157284 ** Otherwise, if no error occurs, this function returns SQLITE_OK if the
157285 ** update has been partially applied, or SQLITE_DONE if it has been
157286 ** completely applied.
157287 */
157288 SQLITE_API int SQLITE_STDCALL sqlite3rbu_close(sqlite3rbu *pRbu, char **pzErrmsg);
157289
157290 /*
157291 ** Return the total number of key-value operations (inserts, deletes or
157292 ** updates) that have been performed on the target database since the
157293 ** current RBU update was started.
157294 */
157295 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3rbu_progress(sqlite3rbu *pRbu);
157296
157297 /*
157298 ** Create an RBU VFS named zName that accesses the underlying file-system
157299 ** via existing VFS zParent. Or, if the zParent parameter is passed NULL,
157300 ** then the new RBU VFS uses the default system VFS to access the file-system.
157301 ** The new object is registered as a non-default VFS with SQLite before
157302 ** returning.
157303 **
157304 ** Part of the RBU implementation uses a custom VFS object. Usually, this
157305 ** object is created and deleted automatically by RBU.
157306 **
157307 ** The exception is for applications that also use zipvfs. In this case,
157308 ** the custom VFS must be explicitly created by the user before the RBU
157309 ** handle is opened. The RBU VFS should be installed so that the zipvfs
157310 ** VFS uses the RBU VFS, which in turn uses any other VFS layers in use
157311 ** (for example multiplexor) to access the file-system. For example,
157312 ** to assemble an RBU enabled VFS stack that uses both zipvfs and
157313 ** multiplexor (error checking omitted):
157314 **
157315 ** // Create a VFS named "multiplex" (not the default).
157316 ** sqlite3_multiplex_initialize(0, 0);
157317 **
157318 ** // Create an rbu VFS named "rbu" that uses multiplexor. If the
157319 ** // second argument were replaced with NULL, the "rbu" VFS would
157320 ** // access the file-system via the system default VFS, bypassing the
157321 ** // multiplexor.
157322 ** sqlite3rbu_create_vfs("rbu", "multiplex");
157323 **
157324 ** // Create a zipvfs VFS named "zipvfs" that uses rbu.
157325 ** zipvfs_create_vfs_v3("zipvfs", "rbu", 0, xCompressorAlgorithmDetector);
157326 **
157327 ** // Make zipvfs the default VFS.
157328 ** sqlite3_vfs_register(sqlite3_vfs_find("zipvfs"), 1);
157329 **
157330 ** Because the default VFS created above includes a RBU functionality, it
157331 ** may be used by RBU clients. Attempting to use RBU with a zipvfs VFS stack
157332 ** that does not include the RBU layer results in an error.
157333 **
157334 ** The overhead of adding the "rbu" VFS to the system is negligible for
157335 ** non-RBU users. There is no harm in an application accessing the
157336 ** file-system via "rbu" all the time, even if it only uses RBU functionality
157337 ** occasionally.
157338 */
157339 SQLITE_API int SQLITE_STDCALL sqlite3rbu_create_vfs(const char *zName, const char *zParent);
157340
157341 /*
157342 ** Deregister and destroy an RBU vfs created by an earlier call to
157343 ** sqlite3rbu_create_vfs().
157344 **
157345 ** VFS objects are not reference counted. If a VFS object is destroyed
157346 ** before all database handles that use it have been closed, the results
157347 ** are undefined.
157348 */
157349 SQLITE_API void SQLITE_STDCALL sqlite3rbu_destroy_vfs(const char *zName);
157350
157351 #endif /* _SQLITE3RBU_H */
157352
157353 /************** End of sqlite3rbu.h ******************************************/
157354 /************** Continuing where we left off in sqlite3rbu.c *****************/
 
157355
157356 /* Maximum number of prepared UPDATE statements held by this module */
157357 #define SQLITE_RBU_UPDATE_CACHESIZE 16
157358
157359 /*
157360 ** Swap two objects of type TYPE.
157361 */
157362 #if !defined(SQLITE_AMALGAMATION)
157363 # define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
157364 #endif
157365
157366 /*
157367 ** The rbu_state table is used to save the state of a partially applied
157368 ** update so that it can be resumed later. The table consists of integer
157369 ** keys mapped to values as follows:
157370 **
157371 ** RBU_STATE_STAGE:
157372 ** May be set to integer values 1, 2, 4 or 5. As follows:
157373 ** 1: the *-rbu file is currently under construction.
157374 ** 2: the *-rbu file has been constructed, but not yet moved
157375 ** to the *-wal path.
157376 ** 4: the checkpoint is underway.
157377 ** 5: the rbu update has been checkpointed.
157378 **
157379 ** RBU_STATE_TBL:
157380 ** Only valid if STAGE==1. The target database name of the table
157381 ** currently being written.
157382 **
157383 ** RBU_STATE_IDX:
157384 ** Only valid if STAGE==1. The target database name of the index
157385 ** currently being written, or NULL if the main table is currently being
157386 ** updated.
157387 **
157388 ** RBU_STATE_ROW:
157389 ** Only valid if STAGE==1. Number of rows already processed for the current
157390 ** table/index.
157391 **
157392 ** RBU_STATE_PROGRESS:
157393 ** Trbul number of sqlite3rbu_step() calls made so far as part of this
157394 ** rbu update.
157395 **
157396 ** RBU_STATE_CKPT:
157397 ** Valid if STAGE==4. The 64-bit checksum associated with the wal-index
157398 ** header created by recovering the *-wal file. This is used to detect
157399 ** cases when another client appends frames to the *-wal file in the
157400 ** middle of an incremental checkpoint (an incremental checkpoint cannot
157401 ** be continued if this happens).
157402 **
157403 ** RBU_STATE_COOKIE:
157404 ** Valid if STAGE==1. The current change-counter cookie value in the
157405 ** target db file.
157406 **
157407 ** RBU_STATE_OALSZ:
157408 ** Valid if STAGE==1. The size in bytes of the *-oal file.
157409 */
157410 #define RBU_STATE_STAGE 1
157411 #define RBU_STATE_TBL 2
157412 #define RBU_STATE_IDX 3
157413 #define RBU_STATE_ROW 4
157414 #define RBU_STATE_PROGRESS 5
157415 #define RBU_STATE_CKPT 6
157416 #define RBU_STATE_COOKIE 7
157417 #define RBU_STATE_OALSZ 8
157418
157419 #define RBU_STAGE_OAL 1
157420 #define RBU_STAGE_MOVE 2
157421 #define RBU_STAGE_CAPTURE 3
157422 #define RBU_STAGE_CKPT 4
157423 #define RBU_STAGE_DONE 5
157424
157425
157426 #define RBU_CREATE_STATE \
157427 "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
157428
157429 typedef struct RbuFrame RbuFrame;
157430 typedef struct RbuObjIter RbuObjIter;
157431 typedef struct RbuState RbuState;
157432 typedef struct rbu_vfs rbu_vfs;
157433 typedef struct rbu_file rbu_file;
157434 typedef struct RbuUpdateStmt RbuUpdateStmt;
157435
157436 #if !defined(SQLITE_AMALGAMATION)
157437 typedef unsigned int u32;
157438 typedef unsigned char u8;
157439 typedef sqlite3_int64 i64;
@@ -157245,13 +157447,13 @@
157447 #define WAL_LOCK_WRITE 0
157448 #define WAL_LOCK_CKPT 1
157449 #define WAL_LOCK_READ0 3
157450
157451 /*
157452 ** A structure to store values read from the rbu_state table in memory.
157453 */
157454 struct RbuState {
157455 int eStage;
157456 char *zTbl;
157457 char *zIdx;
157458 i64 iWalCksum;
157459 int nRow;
@@ -157258,14 +157460,14 @@
157460 i64 nProgress;
157461 u32 iCookie;
157462 i64 iOalSz;
157463 };
157464
157465 struct RbuUpdateStmt {
157466 char *zMask; /* Copy of update mask used with pUpdate */
157467 sqlite3_stmt *pUpdate; /* Last update statement (or NULL) */
157468 RbuUpdateStmt *pNext;
157469 };
157470
157471 /*
157472 ** An iterator of this type is used to iterate through all objects in
157473 ** the target database that require updating. For each such table, the
@@ -157280,131 +157482,131 @@
157482 ** it points to an array of flags nTblCol elements in size. The flag is
157483 ** set for each column that is either a part of the PK or a part of an
157484 ** index. Or clear otherwise.
157485 **
157486 */
157487 struct RbuObjIter {
157488 sqlite3_stmt *pTblIter; /* Iterate through tables */
157489 sqlite3_stmt *pIdxIter; /* Index iterator */
157490 int nTblCol; /* Size of azTblCol[] array */
157491 char **azTblCol; /* Array of unquoted target column names */
157492 char **azTblType; /* Array of target column types */
157493 int *aiSrcOrder; /* src table col -> target table col */
157494 u8 *abTblPk; /* Array of flags, set on target PK columns */
157495 u8 *abNotNull; /* Array of flags, set on NOT NULL columns */
157496 u8 *abIndexed; /* Array of flags, set on indexed & PK cols */
157497 int eType; /* Table type - an RBU_PK_XXX value */
157498
157499 /* Output variables. zTbl==0 implies EOF. */
157500 int bCleanup; /* True in "cleanup" state */
157501 const char *zTbl; /* Name of target db table */
157502 const char *zIdx; /* Name of target db index (or null) */
157503 int iTnum; /* Root page of current object */
157504 int iPkTnum; /* If eType==EXTERNAL, root of PK index */
157505 int bUnique; /* Current index is unique */
157506
157507 /* Statements created by rbuObjIterPrepareAll() */
157508 int nCol; /* Number of columns in current object */
157509 sqlite3_stmt *pSelect; /* Source data */
157510 sqlite3_stmt *pInsert; /* Statement for INSERT operations */
157511 sqlite3_stmt *pDelete; /* Statement for DELETE ops */
157512 sqlite3_stmt *pTmpInsert; /* Insert into rbu_tmp_$zTbl */
157513
157514 /* Last UPDATE used (for PK b-tree updates only), or NULL. */
157515 RbuUpdateStmt *pRbuUpdate;
157516 };
157517
157518 /*
157519 ** Values for RbuObjIter.eType
157520 **
157521 ** 0: Table does not exist (error)
157522 ** 1: Table has an implicit rowid.
157523 ** 2: Table has an explicit IPK column.
157524 ** 3: Table has an external PK index.
157525 ** 4: Table is WITHOUT ROWID.
157526 ** 5: Table is a virtual table.
157527 */
157528 #define RBU_PK_NOTABLE 0
157529 #define RBU_PK_NONE 1
157530 #define RBU_PK_IPK 2
157531 #define RBU_PK_EXTERNAL 3
157532 #define RBU_PK_WITHOUT_ROWID 4
157533 #define RBU_PK_VTAB 5
157534
157535
157536 /*
157537 ** Within the RBU_STAGE_OAL stage, each call to sqlite3rbu_step() performs
157538 ** one of the following operations.
157539 */
157540 #define RBU_INSERT 1 /* Insert on a main table b-tree */
157541 #define RBU_DELETE 2 /* Delete a row from a main table b-tree */
157542 #define RBU_IDX_DELETE 3 /* Delete a row from an aux. index b-tree */
157543 #define RBU_IDX_INSERT 4 /* Insert on an aux. index b-tree */
157544 #define RBU_UPDATE 5 /* Update a row in a main table b-tree */
157545
157546
157547 /*
157548 ** A single step of an incremental checkpoint - frame iWalFrame of the wal
157549 ** file should be copied to page iDbPage of the database file.
157550 */
157551 struct RbuFrame {
157552 u32 iDbPage;
157553 u32 iWalFrame;
157554 };
157555
157556 /*
157557 ** RBU handle.
157558 */
157559 struct sqlite3rbu {
157560 int eStage; /* Value of RBU_STATE_STAGE field */
157561 sqlite3 *dbMain; /* target database handle */
157562 sqlite3 *dbRbu; /* rbu database handle */
157563 char *zTarget; /* Path to target db */
157564 char *zRbu; /* Path to rbu db */
157565 char *zState; /* Path to state db (or NULL if zRbu) */
157566 char zStateDb[5]; /* Db name for state ("stat" or "main") */
157567 int rc; /* Value returned by last rbu_step() call */
157568 char *zErrmsg; /* Error message if rc!=SQLITE_OK */
157569 int nStep; /* Rows processed for current object */
157570 int nProgress; /* Rows processed for all objects */
157571 RbuObjIter objiter; /* Iterator for skipping through tbl/idx */
157572 const char *zVfsName; /* Name of automatically created rbu vfs */
157573 rbu_file *pTargetFd; /* File handle open on target db */
157574 i64 iOalSz;
157575
157576 /* The following state variables are used as part of the incremental
157577 ** checkpoint stage (eStage==RBU_STAGE_CKPT). See comments surrounding
157578 ** function rbuSetupCheckpoint() for details. */
157579 u32 iMaxFrame; /* Largest iWalFrame value in aFrame[] */
157580 u32 mLock;
157581 int nFrame; /* Entries in aFrame[] array */
157582 int nFrameAlloc; /* Allocated size of aFrame[] array */
157583 RbuFrame *aFrame;
157584 int pgsz;
157585 u8 *aBuf;
157586 i64 iWalCksum;
157587 };
157588
157589 /*
157590 ** An rbu VFS is implemented using an instance of this structure.
157591 */
157592 struct rbu_vfs {
157593 sqlite3_vfs base; /* rbu VFS shim methods */
157594 sqlite3_vfs *pRealVfs; /* Underlying VFS */
157595 sqlite3_mutex *mutex; /* Mutex to protect pMain */
157596 rbu_file *pMain; /* Linked list of main db files */
157597 };
157598
157599 /*
157600 ** Each file opened by an rbu VFS is represented by an instance of
157601 ** the following structure.
157602 */
157603 struct rbu_file {
157604 sqlite3_file base; /* sqlite3_file methods */
157605 sqlite3_file *pReal; /* Underlying file handle */
157606 rbu_vfs *pRbuVfs; /* Pointer to the rbu_vfs object */
157607 sqlite3rbu *pRbu; /* Pointer to rbu object (rbu target only) */
157608
157609 int openFlags; /* Flags this file was opened with */
157610 u32 iCookie; /* Cookie value for main db files */
157611 u8 iWriteVer; /* "write-version" value for main db files */
157612
@@ -157411,12 +157613,12 @@
157613 int nShm; /* Number of entries in apShm[] array */
157614 char **apShm; /* Array of mmap'd *-shm regions */
157615 char *zDel; /* Delete this when closing file */
157616
157617 const char *zWal; /* Wal filename for this main db file */
157618 rbu_file *pWalFd; /* Wal file descriptor for this main db */
157619 rbu_file *pMainNext; /* Next MAIN_DB file */
157620 };
157621
157622
157623 /*
157624 ** Prepare the SQL statement in buffer zSql against database handle db.
@@ -157490,14 +157692,14 @@
157692 }
157693 return rc;
157694 }
157695
157696 /*
157697 ** Free the RbuObjIter.azTblCol[] and RbuObjIter.abTblPk[] arrays allocated
157698 ** by an earlier call to rbuObjIterCacheTableInfo().
157699 */
157700 static void rbuObjIterFreeCols(RbuObjIter *pIter){
157701 int i;
157702 for(i=0; i<pIter->nTblCol; i++){
157703 sqlite3_free(pIter->azTblCol[i]);
157704 sqlite3_free(pIter->azTblType[i]);
157705 }
@@ -157513,72 +157715,72 @@
157715
157716 /*
157717 ** Finalize all statements and free all allocations that are specific to
157718 ** the current object (table/index pair).
157719 */
157720 static void rbuObjIterClearStatements(RbuObjIter *pIter){
157721 RbuUpdateStmt *pUp;
157722
157723 sqlite3_finalize(pIter->pSelect);
157724 sqlite3_finalize(pIter->pInsert);
157725 sqlite3_finalize(pIter->pDelete);
157726 sqlite3_finalize(pIter->pTmpInsert);
157727 pUp = pIter->pRbuUpdate;
157728 while( pUp ){
157729 RbuUpdateStmt *pTmp = pUp->pNext;
157730 sqlite3_finalize(pUp->pUpdate);
157731 sqlite3_free(pUp);
157732 pUp = pTmp;
157733 }
157734
157735 pIter->pSelect = 0;
157736 pIter->pInsert = 0;
157737 pIter->pDelete = 0;
157738 pIter->pRbuUpdate = 0;
157739 pIter->pTmpInsert = 0;
157740 pIter->nCol = 0;
157741 }
157742
157743 /*
157744 ** Clean up any resources allocated as part of the iterator object passed
157745 ** as the only argument.
157746 */
157747 static void rbuObjIterFinalize(RbuObjIter *pIter){
157748 rbuObjIterClearStatements(pIter);
157749 sqlite3_finalize(pIter->pTblIter);
157750 sqlite3_finalize(pIter->pIdxIter);
157751 rbuObjIterFreeCols(pIter);
157752 memset(pIter, 0, sizeof(RbuObjIter));
157753 }
157754
157755 /*
157756 ** Advance the iterator to the next position.
157757 **
157758 ** If no error occurs, SQLITE_OK is returned and the iterator is left
157759 ** pointing to the next entry. Otherwise, an error code and message is
157760 ** left in the RBU handle passed as the first argument. A copy of the
157761 ** error code is returned.
157762 */
157763 static int rbuObjIterNext(sqlite3rbu *p, RbuObjIter *pIter){
157764 int rc = p->rc;
157765 if( rc==SQLITE_OK ){
157766
157767 /* Free any SQLite statements used while processing the previous object */
157768 rbuObjIterClearStatements(pIter);
157769 if( pIter->zIdx==0 ){
157770 rc = sqlite3_exec(p->dbMain,
157771 "DROP TRIGGER IF EXISTS temp.rbu_insert_tr;"
157772 "DROP TRIGGER IF EXISTS temp.rbu_update1_tr;"
157773 "DROP TRIGGER IF EXISTS temp.rbu_update2_tr;"
157774 "DROP TRIGGER IF EXISTS temp.rbu_delete_tr;"
157775 , 0, 0, &p->zErrmsg
157776 );
157777 }
157778
157779 if( rc==SQLITE_OK ){
157780 if( pIter->bCleanup ){
157781 rbuObjIterFreeCols(pIter);
157782 pIter->bCleanup = 0;
157783 rc = sqlite3_step(pIter->pTblIter);
157784 if( rc!=SQLITE_ROW ){
157785 rc = resetAndCollectError(pIter->pTblIter, &p->zErrmsg);
157786 pIter->zTbl = 0;
@@ -157607,11 +157809,11 @@
157809 }
157810 }
157811 }
157812
157813 if( rc!=SQLITE_OK ){
157814 rbuObjIterFinalize(pIter);
157815 p->rc = rc;
157816 }
157817 return rc;
157818 }
157819
@@ -157618,18 +157820,18 @@
157820 /*
157821 ** Initialize the iterator structure passed as the second argument.
157822 **
157823 ** If no error occurs, SQLITE_OK is returned and the iterator is left
157824 ** pointing to the first entry. Otherwise, an error code and message is
157825 ** left in the RBU handle passed as the first argument. A copy of the
157826 ** error code is returned.
157827 */
157828 static int rbuObjIterFirst(sqlite3rbu *p, RbuObjIter *pIter){
157829 int rc;
157830 memset(pIter, 0, sizeof(RbuObjIter));
157831
157832 rc = prepareAndCollectError(p->dbRbu, &pIter->pTblIter, &p->zErrmsg,
157833 "SELECT substr(name, 6) FROM sqlite_master "
157834 "WHERE type='table' AND name LIKE 'data_%'"
157835 );
157836
157837 if( rc==SQLITE_OK ){
@@ -157640,23 +157842,23 @@
157842 );
157843 }
157844
157845 pIter->bCleanup = 1;
157846 p->rc = rc;
157847 return rbuObjIterNext(p, pIter);
157848 }
157849
157850 /*
157851 ** This is a wrapper around "sqlite3_mprintf(zFmt, ...)". If an OOM occurs,
157852 ** an error code is stored in the RBU handle passed as the first argument.
157853 **
157854 ** If an error has already occurred (p->rc is already set to something other
157855 ** than SQLITE_OK), then this function returns NULL without modifying the
157856 ** stored error code. In this case it still calls sqlite3_free() on any
157857 ** printf() parameters associated with %z conversions.
157858 */
157859 static char *rbuMPrintf(sqlite3rbu *p, const char *zFmt, ...){
157860 char *zSql = 0;
157861 va_list ap;
157862 va_start(ap, zFmt);
157863 zSql = sqlite3_vmprintf(zFmt, ap);
157864 if( p->rc==SQLITE_OK ){
@@ -157671,17 +157873,17 @@
157873
157874 /*
157875 ** Argument zFmt is a sqlite3_mprintf() style format string. The trailing
157876 ** arguments are the usual subsitution values. This function performs
157877 ** the printf() style substitutions and executes the result as an SQL
157878 ** statement on the RBU handles database.
157879 **
157880 ** If an error occurs, an error code and error message is stored in the
157881 ** RBU handle. If an error has already occurred when this function is
157882 ** called, it is a no-op.
157883 */
157884 static int rbuMPrintfExec(sqlite3rbu *p, sqlite3 *db, const char *zFmt, ...){
157885 va_list ap;
157886 va_start(ap, zFmt);
157887 char *zSql = sqlite3_vmprintf(zFmt, ap);
157888 if( p->rc==SQLITE_OK ){
157889 if( zSql==0 ){
@@ -157698,16 +157900,16 @@
157900 /*
157901 ** Attempt to allocate and return a pointer to a zeroed block of nByte
157902 ** bytes.
157903 **
157904 ** If an error (i.e. an OOM condition) occurs, return NULL and leave an
157905 ** error code in the rbu handle passed as the first argument. Or, if an
157906 ** error has already occurred when this function is called, return NULL
157907 ** immediately without attempting the allocation or modifying the stored
157908 ** error code.
157909 */
157910 static void *rbuMalloc(sqlite3rbu *p, int nByte){
157911 void *pRet = 0;
157912 if( p->rc==SQLITE_OK ){
157913 assert( nByte>0 );
157914 pRet = sqlite3_malloc(nByte);
157915 if( pRet==0 ){
@@ -157721,17 +157923,17 @@
157923
157924
157925 /*
157926 ** Allocate and zero the pIter->azTblCol[] and abTblPk[] arrays so that
157927 ** there is room for at least nCol elements. If an OOM occurs, store an
157928 ** error code in the RBU handle passed as the first argument.
157929 */
157930 static void rbuAllocateIterArrays(sqlite3rbu *p, RbuObjIter *pIter, int nCol){
157931 int nByte = (2*sizeof(char*) + sizeof(int) + 3*sizeof(u8)) * nCol;
157932 char **azNew;
157933
157934 azNew = (char**)rbuMalloc(p, nByte);
157935 if( azNew ){
157936 pIter->azTblCol = azNew;
157937 pIter->azTblType = &azNew[nCol];
157938 pIter->aiSrcOrder = (int*)&pIter->azTblType[nCol];
157939 pIter->abTblPk = (u8*)&pIter->aiSrcOrder[nCol];
@@ -157748,11 +157950,11 @@
157950 **
157951 ** If an OOM condition is encountered when attempting to allocate memory,
157952 ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
157953 ** if the allocation succeeds, (*pRc) is left unchanged.
157954 */
157955 static char *rbuStrndup(const char *zStr, int *pRc){
157956 char *zRet = 0;
157957
157958 assert( *pRc==SQLITE_OK );
157959 if( zStr ){
157960 int nCopy = strlen(zStr) + 1;
@@ -157769,14 +157971,14 @@
157971
157972 /*
157973 ** Finalize the statement passed as the second argument.
157974 **
157975 ** If the sqlite3_finalize() call indicates that an error occurs, and the
157976 ** rbu handle error code is not already set, set the error code and error
157977 ** message accordingly.
157978 */
157979 static void rbuFinalize(sqlite3rbu *p, sqlite3_stmt *pStmt){
157980 sqlite3 *db = sqlite3_db_handle(pStmt);
157981 int rc = sqlite3_finalize(pStmt);
157982 if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){
157983 p->rc = rc;
157984 p->zErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
@@ -157787,16 +157989,16 @@
157989 **
157990 ** peType is of type (int*), a pointer to an output parameter of type
157991 ** (int). This call sets the output parameter as follows, depending
157992 ** on the type of the table specified by parameters dbName and zTbl.
157993 **
157994 ** RBU_PK_NOTABLE: No such table.
157995 ** RBU_PK_NONE: Table has an implicit rowid.
157996 ** RBU_PK_IPK: Table has an explicit IPK column.
157997 ** RBU_PK_EXTERNAL: Table has an external PK index.
157998 ** RBU_PK_WITHOUT_ROWID: Table is WITHOUT ROWID.
157999 ** RBU_PK_VTAB: Table is a virtual table.
158000 **
158001 ** Argument *piPk is also of type (int*), and also points to an output
158002 ** parameter. Unless the table has an external primary key index
158003 ** (i.e. unless *peType is set to 3), then *piPk is set to zero. Or,
158004 ** if the table does have an external primary key index, then *piPk
@@ -157804,28 +158006,28 @@
158006 ** returning.
158007 **
158008 ** ALGORITHM:
158009 **
158010 ** if( no entry exists in sqlite_master ){
158011 ** return RBU_PK_NOTABLE
158012 ** }else if( sql for the entry starts with "CREATE VIRTUAL" ){
158013 ** return RBU_PK_VTAB
158014 ** }else if( "PRAGMA index_list()" for the table contains a "pk" index ){
158015 ** if( the index that is the pk exists in sqlite_master ){
158016 ** *piPK = rootpage of that index.
158017 ** return RBU_PK_EXTERNAL
158018 ** }else{
158019 ** return RBU_PK_WITHOUT_ROWID
158020 ** }
158021 ** }else if( "PRAGMA table_info()" lists one or more "pk" columns ){
158022 ** return RBU_PK_IPK
158023 ** }else{
158024 ** return RBU_PK_NONE
158025 ** }
158026 */
158027 static void rbuTableType(
158028 sqlite3rbu *p,
158029 const char *zTab,
158030 int *peType,
158031 int *piTnum,
158032 int *piPk
158033 ){
@@ -157835,11 +158037,11 @@
158037 ** 2) SELECT count(*) FROM sqlite_master where name=%Q
158038 ** 3) PRAGMA table_info = ?
158039 */
158040 sqlite3_stmt *aStmt[4] = {0, 0, 0, 0};
158041
158042 *peType = RBU_PK_NOTABLE;
158043 *piPk = 0;
158044
158045 assert( p->rc==SQLITE_OK );
158046 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[0], &p->zErrmsg,
158047 sqlite3_mprintf(
@@ -157847,22 +158049,22 @@
158049 " FROM sqlite_master"
158050 " WHERE name=%Q", zTab
158051 ));
158052 if( p->rc!=SQLITE_OK || sqlite3_step(aStmt[0])!=SQLITE_ROW ){
158053 /* Either an error, or no such table. */
158054 goto rbuTableType_end;
158055 }
158056 if( sqlite3_column_int(aStmt[0], 0) ){
158057 *peType = RBU_PK_VTAB; /* virtual table */
158058 goto rbuTableType_end;
158059 }
158060 *piTnum = sqlite3_column_int(aStmt[0], 1);
158061
158062 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[1], &p->zErrmsg,
158063 sqlite3_mprintf("PRAGMA index_list=%Q",zTab)
158064 );
158065 if( p->rc ) goto rbuTableType_end;
158066 while( sqlite3_step(aStmt[1])==SQLITE_ROW ){
158067 const u8 *zOrig = sqlite3_column_text(aStmt[1], 3);
158068 const u8 *zIdx = sqlite3_column_text(aStmt[1], 1);
158069 if( zOrig && zIdx && zOrig[0]=='p' ){
158070 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[2], &p->zErrmsg,
@@ -157870,45 +158072,45 @@
158072 "SELECT rootpage FROM sqlite_master WHERE name = %Q", zIdx
158073 ));
158074 if( p->rc==SQLITE_OK ){
158075 if( sqlite3_step(aStmt[2])==SQLITE_ROW ){
158076 *piPk = sqlite3_column_int(aStmt[2], 0);
158077 *peType = RBU_PK_EXTERNAL;
158078 }else{
158079 *peType = RBU_PK_WITHOUT_ROWID;
158080 }
158081 }
158082 goto rbuTableType_end;
158083 }
158084 }
158085
158086 p->rc = prepareFreeAndCollectError(p->dbMain, &aStmt[3], &p->zErrmsg,
158087 sqlite3_mprintf("PRAGMA table_info=%Q",zTab)
158088 );
158089 if( p->rc==SQLITE_OK ){
158090 while( sqlite3_step(aStmt[3])==SQLITE_ROW ){
158091 if( sqlite3_column_int(aStmt[3],5)>0 ){
158092 *peType = RBU_PK_IPK; /* explicit IPK column */
158093 goto rbuTableType_end;
158094 }
158095 }
158096 *peType = RBU_PK_NONE;
158097 }
158098
158099 rbuTableType_end: {
158100 int i;
158101 for(i=0; i<sizeof(aStmt)/sizeof(aStmt[0]); i++){
158102 rbuFinalize(p, aStmt[i]);
158103 }
158104 }
158105 }
158106
158107 /*
158108 ** This is a helper function for rbuObjIterCacheTableInfo(). It populates
158109 ** the pIter->abIndexed[] array.
158110 */
158111 static void rbuObjIterCacheIndexedCols(sqlite3rbu *p, RbuObjIter *pIter){
158112 sqlite3_stmt *pList = 0;
158113 int bIndex = 0;
158114
158115 if( p->rc==SQLITE_OK ){
158116 memcpy(pIter->abIndexed, pIter->abTblPk, sizeof(u8)*pIter->nTblCol);
@@ -157926,15 +158128,15 @@
158128 );
158129 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158130 int iCid = sqlite3_column_int(pXInfo, 1);
158131 if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
158132 }
158133 rbuFinalize(p, pXInfo);
158134 bIndex = 1;
158135 }
158136
158137 rbuFinalize(p, pList);
158138 if( bIndex==0 ) pIter->abIndexed = 0;
158139 }
158140
158141
158142 /*
@@ -157942,67 +158144,67 @@
158144 ** pIter->abTblPk[], pIter->nTblCol and pIter->bRowid variables according to
158145 ** the table (not index) that the iterator currently points to.
158146 **
158147 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. If
158148 ** an error does occur, an error code and error message are also left in
158149 ** the RBU handle.
158150 */
158151 static int rbuObjIterCacheTableInfo(sqlite3rbu *p, RbuObjIter *pIter){
158152 if( pIter->azTblCol==0 ){
158153 sqlite3_stmt *pStmt = 0;
158154 int nCol = 0;
158155 int i; /* for() loop iterator variable */
158156 int bRbuRowid = 0; /* If input table has column "rbu_rowid" */
158157 int iOrder = 0;
158158 int iTnum = 0;
158159
158160 /* Figure out the type of table this step will deal with. */
158161 assert( pIter->eType==0 );
158162 rbuTableType(p, pIter->zTbl, &pIter->eType, &iTnum, &pIter->iPkTnum);
158163 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_NOTABLE ){
158164 p->rc = SQLITE_ERROR;
158165 p->zErrmsg = sqlite3_mprintf("no such table: %s", pIter->zTbl);
158166 }
158167 if( p->rc ) return p->rc;
158168 if( pIter->zIdx==0 ) pIter->iTnum = iTnum;
158169
158170 assert( pIter->eType==RBU_PK_NONE || pIter->eType==RBU_PK_IPK
158171 || pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_WITHOUT_ROWID
158172 || pIter->eType==RBU_PK_VTAB
158173 );
158174
158175 /* Populate the azTblCol[] and nTblCol variables based on the columns
158176 ** of the input table. Ignore any input table columns that begin with
158177 ** "rbu_". */
158178 p->rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
158179 sqlite3_mprintf("SELECT * FROM 'data_%q'", pIter->zTbl)
158180 );
158181 if( p->rc==SQLITE_OK ){
158182 nCol = sqlite3_column_count(pStmt);
158183 rbuAllocateIterArrays(p, pIter, nCol);
158184 }
158185 for(i=0; p->rc==SQLITE_OK && i<nCol; i++){
158186 const char *zName = (const char*)sqlite3_column_name(pStmt, i);
158187 if( sqlite3_strnicmp("rbu_", zName, 4) ){
158188 char *zCopy = rbuStrndup(zName, &p->rc);
158189 pIter->aiSrcOrder[pIter->nTblCol] = pIter->nTblCol;
158190 pIter->azTblCol[pIter->nTblCol++] = zCopy;
158191 }
158192 else if( 0==sqlite3_stricmp("rbu_rowid", zName) ){
158193 bRbuRowid = 1;
158194 }
158195 }
158196 sqlite3_finalize(pStmt);
158197 pStmt = 0;
158198
158199 if( p->rc==SQLITE_OK
158200 && bRbuRowid!=(pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
158201 ){
158202 p->rc = SQLITE_ERROR;
158203 p->zErrmsg = sqlite3_mprintf(
158204 "table data_%q %s rbu_rowid column", pIter->zTbl,
158205 (bRbuRowid ? "may not have" : "requires")
158206 );
158207 }
158208
158209 /* Check that all non-HIDDEN columns in the destination table are also
158210 ** present in the input table. Populate the abTblPk[], azTblType[] and
@@ -158031,20 +158233,20 @@
158233 if( i!=iOrder ){
158234 SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
158235 SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
158236 }
158237
158238 pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
158239 pIter->abTblPk[iOrder] = (iPk!=0);
158240 pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
158241 iOrder++;
158242 }
158243 }
158244
158245 rbuFinalize(p, pStmt);
158246 rbuObjIterCacheIndexedCols(p, pIter);
158247 assert( pIter->eType!=RBU_PK_VTAB || pIter->abIndexed==0 );
158248 }
158249
158250 return p->rc;
158251 }
158252
@@ -158051,29 +158253,29 @@
158253 /*
158254 ** This function constructs and returns a pointer to a nul-terminated
158255 ** string containing some SQL clause or list based on one or more of the
158256 ** column names currently stored in the pIter->azTblCol[] array.
158257 */
158258 static char *rbuObjIterGetCollist(
158259 sqlite3rbu *p, /* RBU object */
158260 RbuObjIter *pIter /* Object iterator for column names */
158261 ){
158262 char *zList = 0;
158263 const char *zSep = "";
158264 int i;
158265 for(i=0; i<pIter->nTblCol; i++){
158266 const char *z = pIter->azTblCol[i];
158267 zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
158268 zSep = ", ";
158269 }
158270 return zList;
158271 }
158272
158273 /*
158274 ** This function is used to create a SELECT list (the list of SQL
158275 ** expressions that follows a SELECT keyword) for a SELECT statement
158276 ** used to read from an data_xxx or rbu_tmp_xxx table while updating the
158277 ** index object currently indicated by the iterator object passed as the
158278 ** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used
158279 ** to obtain the required information.
158280 **
158281 ** If the index is of the following form:
@@ -158090,17 +158292,17 @@
158292 **
158293 ** pzImposterCols: ...
158294 ** pzImposterPk: ...
158295 ** pzWhere: ...
158296 */
158297 static char *rbuObjIterGetIndexCols(
158298 sqlite3rbu *p, /* RBU object */
158299 RbuObjIter *pIter, /* Object iterator for column names */
158300 char **pzImposterCols, /* OUT: Columns for imposter table */
158301 char **pzImposterPk, /* OUT: Imposter PK clause */
158302 char **pzWhere, /* OUT: WHERE clause */
158303 int *pnBind /* OUT: Trbul number of columns */
158304 ){
158305 int rc = p->rc; /* Error code */
158306 int rc2; /* sqlite3_finalize() return code */
158307 char *zRet = 0; /* String to return */
158308 char *zImpCols = 0; /* String to return via *pzImposterCols */
@@ -158125,18 +158327,18 @@
158327 const char *zCol;
158328 const char *zType;
158329
158330 if( iCid<0 ){
158331 /* An integer primary key. If the table has an explicit IPK, use
158332 ** its name. Otherwise, use "rbu_rowid". */
158333 if( pIter->eType==RBU_PK_IPK ){
158334 int i;
158335 for(i=0; pIter->abTblPk[i]==0; i++);
158336 assert( i<pIter->nTblCol );
158337 zCol = pIter->azTblCol[i];
158338 }else{
158339 zCol = "rbu_rowid";
158340 }
158341 zType = "INTEGER";
158342 }else{
158343 zCol = pIter->azTblCol[iCid];
158344 zType = pIter->azTblType[iCid];
@@ -158143,19 +158345,19 @@
158345 }
158346
158347 zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
158348 if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
158349 const char *zOrder = (bDesc ? " DESC" : "");
158350 zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s",
158351 zImpPK, zCom, nBind, zCol, zOrder
158352 );
158353 }
158354 zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q",
158355 zImpCols, zCom, nBind, zCol, zType, zCollate
158356 );
158357 zWhere = sqlite3_mprintf(
158358 "%z%s\"rbu_imp_%d%w\" IS ?", zWhere, zAnd, nBind, zCol
158359 );
158360 if( zRet==0 || zImpPK==0 || zImpCols==0 || zWhere==0 ) rc = SQLITE_NOMEM;
158361 zCom = ", ";
158362 zAnd = " AND ";
158363 nBind++;
@@ -158189,16 +158391,16 @@
158391 **
158392 ** "old.a, old.b, old.b"
158393 **
158394 ** With the column names escaped.
158395 **
158396 ** For tables with implicit rowids - RBU_PK_EXTERNAL and RBU_PK_NONE, append
158397 ** the text ", old._rowid_" to the returned value.
158398 */
158399 static char *rbuObjIterGetOldlist(
158400 sqlite3rbu *p,
158401 RbuObjIter *pIter,
158402 const char *zObj
158403 ){
158404 char *zList = 0;
158405 if( p->rc==SQLITE_OK && pIter->abIndexed ){
158406 const char *zS = "";
@@ -158216,12 +158418,12 @@
158418 break;
158419 }
158420 }
158421
158422 /* For a table with implicit rowids, append "old._rowid_" to the list. */
158423 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158424 zList = rbuMPrintf(p, "%z, %s._rowid_", zList, zObj);
158425 }
158426 }
158427 return zList;
158428 }
158429
@@ -158233,37 +158435,37 @@
158435 **
158436 ** Return the string:
158437 **
158438 ** "b = ?1 AND c = ?2"
158439 */
158440 static char *rbuObjIterGetWhere(
158441 sqlite3rbu *p,
158442 RbuObjIter *pIter
158443 ){
158444 char *zList = 0;
158445 if( pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE ){
158446 zList = rbuMPrintf(p, "_rowid_ = ?%d", pIter->nTblCol+1);
158447 }else if( pIter->eType==RBU_PK_EXTERNAL ){
158448 const char *zSep = "";
158449 int i;
158450 for(i=0; i<pIter->nTblCol; i++){
158451 if( pIter->abTblPk[i] ){
158452 zList = rbuMPrintf(p, "%z%sc%d=?%d", zList, zSep, i, i+1);
158453 zSep = " AND ";
158454 }
158455 }
158456 zList = rbuMPrintf(p,
158457 "_rowid_ = (SELECT id FROM rbu_imposter2 WHERE %z)", zList
158458 );
158459
158460 }else{
158461 const char *zSep = "";
158462 int i;
158463 for(i=0; i<pIter->nTblCol; i++){
158464 if( pIter->abTblPk[i] ){
158465 const char *zCol = pIter->azTblCol[i];
158466 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d", zList, zSep, zCol, i+1);
158467 zSep = " AND ";
158468 }
158469 }
158470 }
158471 return zList;
@@ -158270,60 +158472,60 @@
158472 }
158473
158474 /*
158475 ** The SELECT statement iterating through the keys for the current object
158476 ** (p->objiter.pSelect) currently points to a valid row. However, there
158477 ** is something wrong with the rbu_control value in the rbu_control value
158478 ** stored in the (p->nCol+1)'th column. Set the error code and error message
158479 ** of the RBU handle to something reflecting this.
158480 */
158481 static void rbuBadControlError(sqlite3rbu *p){
158482 p->rc = SQLITE_ERROR;
158483 p->zErrmsg = sqlite3_mprintf("invalid rbu_control value");
158484 }
158485
158486
158487 /*
158488 ** Return a nul-terminated string containing the comma separated list of
158489 ** assignments that should be included following the "SET" keyword of
158490 ** an UPDATE statement used to update the table object that the iterator
158491 ** passed as the second argument currently points to if the rbu_control
158492 ** column of the data_xxx table entry is set to zMask.
158493 **
158494 ** The memory for the returned string is obtained from sqlite3_malloc().
158495 ** It is the responsibility of the caller to eventually free it using
158496 ** sqlite3_free().
158497 **
158498 ** If an OOM error is encountered when allocating space for the new
158499 ** string, an error code is left in the rbu handle passed as the first
158500 ** argument and NULL is returned. Or, if an error has already occurred
158501 ** when this function is called, NULL is returned immediately, without
158502 ** attempting the allocation or modifying the stored error code.
158503 */
158504 static char *rbuObjIterGetSetlist(
158505 sqlite3rbu *p,
158506 RbuObjIter *pIter,
158507 const char *zMask
158508 ){
158509 char *zList = 0;
158510 if( p->rc==SQLITE_OK ){
158511 int i;
158512
158513 if( strlen(zMask)!=pIter->nTblCol ){
158514 rbuBadControlError(p);
158515 }else{
158516 const char *zSep = "";
158517 for(i=0; i<pIter->nTblCol; i++){
158518 char c = zMask[pIter->aiSrcOrder[i]];
158519 if( c=='x' ){
158520 zList = rbuMPrintf(p, "%z%s\"%w\"=?%d",
158521 zList, zSep, pIter->azTblCol[i], i+1
158522 );
158523 zSep = ", ";
158524 }
158525 if( c=='d' ){
158526 zList = rbuMPrintf(p, "%z%s\"%w\"=rbu_delta(\"%w\", ?%d)",
158527 zList, zSep, pIter->azTblCol[i], pIter->azTblCol[i], i+1
158528 );
158529 zSep = ", ";
158530 }
158531 }
@@ -158340,20 +158542,20 @@
158542 ** The memory for the returned string is obtained from sqlite3_malloc().
158543 ** It is the responsibility of the caller to eventually free it using
158544 ** sqlite3_free().
158545 **
158546 ** If an OOM error is encountered when allocating space for the new
158547 ** string, an error code is left in the rbu handle passed as the first
158548 ** argument and NULL is returned. Or, if an error has already occurred
158549 ** when this function is called, NULL is returned immediately, without
158550 ** attempting the allocation or modifying the stored error code.
158551 */
158552 static char *rbuObjIterGetBindlist(sqlite3rbu *p, int nBind){
158553 char *zRet = 0;
158554 int nByte = nBind*2 + 1;
158555
158556 zRet = (char*)rbuMalloc(p, nByte);
158557 if( zRet ){
158558 int i;
158559 for(i=0; i<nBind; i++){
158560 zRet[i*2] = '?';
158561 zRet[i*2+1] = (i+1==nBind) ? '\0' : ',';
@@ -158362,21 +158564,21 @@
158564 return zRet;
158565 }
158566
158567 /*
158568 ** The iterator currently points to a table (not index) of type
158569 ** RBU_PK_WITHOUT_ROWID. This function creates the PRIMARY KEY
158570 ** declaration for the corresponding imposter table. For example,
158571 ** if the iterator points to a table created as:
158572 **
158573 ** CREATE TABLE t1(a, b, c, PRIMARY KEY(b, a DESC)) WITHOUT ROWID
158574 **
158575 ** this function returns:
158576 **
158577 ** PRIMARY KEY("b", "a" DESC)
158578 */
158579 static char *rbuWithoutRowidPK(sqlite3rbu *p, RbuObjIter *pIter){
158580 char *z = 0;
158581 assert( pIter->zIdx==0 );
158582 if( p->rc==SQLITE_OK ){
158583 const char *zSep = "PRIMARY KEY(";
158584 sqlite3_stmt *pXList = 0; /* PRAGMA index_list = (pIter->zTbl) */
@@ -158395,23 +158597,23 @@
158597 );
158598 }
158599 break;
158600 }
158601 }
158602 rbuFinalize(p, pXList);
158603
158604 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158605 if( sqlite3_column_int(pXInfo, 5) ){
158606 /* int iCid = sqlite3_column_int(pXInfo, 0); */
158607 const char *zCol = (const char*)sqlite3_column_text(pXInfo, 2);
158608 const char *zDesc = sqlite3_column_int(pXInfo, 3) ? " DESC" : "";
158609 z = rbuMPrintf(p, "%z%s\"%w\"%s", z, zSep, zCol, zDesc);
158610 zSep = ", ";
158611 }
158612 }
158613 z = rbuMPrintf(p, "%z)", z);
158614 rbuFinalize(p, pXInfo);
158615 }
158616 return z;
158617 }
158618
158619 /*
@@ -158420,23 +158622,23 @@
158622 ** iterator passed as the second argument does not currently point to
158623 ** a table (not index) with an external primary key, this function is a
158624 ** no-op.
158625 **
158626 ** Assuming the iterator does point to a table with an external PK, this
158627 ** function creates a WITHOUT ROWID imposter table named "rbu_imposter2"
158628 ** used to access that PK index. For example, if the target table is
158629 ** declared as follows:
158630 **
158631 ** CREATE TABLE t1(a, b TEXT, c REAL, PRIMARY KEY(b, c));
158632 **
158633 ** then the imposter table schema is:
158634 **
158635 ** CREATE TABLE rbu_imposter2(c1 TEXT, c2 REAL, id INTEGER) WITHOUT ROWID;
158636 **
158637 */
158638 static void rbuCreateImposterTable2(sqlite3rbu *p, RbuObjIter *pIter){
158639 if( p->rc==SQLITE_OK && pIter->eType==RBU_PK_EXTERNAL ){
158640 int tnum = pIter->iPkTnum; /* Root page of PK index */
158641 sqlite3_stmt *pQuery = 0; /* SELECT name ... WHERE rootpage = $tnum */
158642 const char *zIdx = 0; /* Name of PK index */
158643 sqlite3_stmt *pXInfo = 0; /* PRAGMA main.index_xinfo = $zIdx */
158644 const char *zComma = "";
@@ -158458,31 +158660,31 @@
158660 if( zIdx ){
158661 p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
158662 sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
158663 );
158664 }
158665 rbuFinalize(p, pQuery);
158666
158667 while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
158668 int bKey = sqlite3_column_int(pXInfo, 5);
158669 if( bKey ){
158670 int iCid = sqlite3_column_int(pXInfo, 1);
158671 int bDesc = sqlite3_column_int(pXInfo, 3);
158672 const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
158673 zCols = rbuMPrintf(p, "%z%sc%d %s COLLATE %s", zCols, zComma,
158674 iCid, pIter->azTblType[iCid], zCollate
158675 );
158676 zPk = rbuMPrintf(p, "%z%sc%d%s", zPk, zComma, iCid, bDesc?" DESC":"");
158677 zComma = ", ";
158678 }
158679 }
158680 zCols = rbuMPrintf(p, "%z, id INTEGER", zCols);
158681 rbuFinalize(p, pXInfo);
158682
158683 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158684 rbuMPrintfExec(p, p->dbMain,
158685 "CREATE TABLE rbu_imposter2(%z, PRIMARY KEY(%z)) WITHOUT ROWID",
158686 zCols, zPk
158687 );
158688 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158689 }
158690 }
@@ -158489,28 +158691,28 @@
158691
158692 /*
158693 ** If an error has already occurred when this function is called, it
158694 ** immediately returns zero (without doing any work). Or, if an error
158695 ** occurs during the execution of this function, it sets the error code
158696 ** in the sqlite3rbu object indicated by the first argument and returns
158697 ** zero.
158698 **
158699 ** The iterator passed as the second argument is guaranteed to point to
158700 ** a table (not an index) when this function is called. This function
158701 ** attempts to create any imposter table required to write to the main
158702 ** table b-tree of the table before returning. Non-zero is returned if
158703 ** an imposter table are created, or zero otherwise.
158704 **
158705 ** An imposter table is required in all cases except RBU_PK_VTAB. Only
158706 ** virtual tables are written to directly. The imposter table has the
158707 ** same schema as the actual target table (less any UNIQUE constraints).
158708 ** More precisely, the "same schema" means the same columns, types,
158709 ** collation sequences. For tables that do not have an external PRIMARY
158710 ** KEY, it also means the same PRIMARY KEY declaration.
158711 */
158712 static void rbuCreateImposterTable(sqlite3rbu *p, RbuObjIter *pIter){
158713 if( p->rc==SQLITE_OK && pIter->eType!=RBU_PK_VTAB ){
158714 int tnum = pIter->iTnum;
158715 const char *zComma = "";
158716 char *zSql = 0;
158717 int iCol;
158718 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
@@ -158522,73 +158724,73 @@
158724
158725 p->rc = sqlite3_table_column_metadata(
158726 p->dbMain, "main", pIter->zTbl, zCol, 0, &zColl, 0, 0, 0
158727 );
158728
158729 if( pIter->eType==RBU_PK_IPK && pIter->abTblPk[iCol] ){
158730 /* If the target table column is an "INTEGER PRIMARY KEY", add
158731 ** "PRIMARY KEY" to the imposter table column declaration. */
158732 zPk = "PRIMARY KEY ";
158733 }
158734 zSql = rbuMPrintf(p, "%z%s\"%w\" %s %sCOLLATE %s%s",
158735 zSql, zComma, zCol, pIter->azTblType[iCol], zPk, zColl,
158736 (pIter->abNotNull[iCol] ? " NOT NULL" : "")
158737 );
158738 zComma = ", ";
158739 }
158740
158741 if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
158742 char *zPk = rbuWithoutRowidPK(p, pIter);
158743 if( zPk ){
158744 zSql = rbuMPrintf(p, "%z, %z", zSql, zPk);
158745 }
158746 }
158747
158748 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1, tnum);
158749 rbuMPrintfExec(p, p->dbMain, "CREATE TABLE \"rbu_imp_%w\"(%z)%s",
158750 pIter->zTbl, zSql,
158751 (pIter->eType==RBU_PK_WITHOUT_ROWID ? " WITHOUT ROWID" : "")
158752 );
158753 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158754 }
158755 }
158756
158757 /*
158758 ** Prepare a statement used to insert rows into the "rbu_tmp_xxx" table.
158759 ** Specifically a statement of the form:
158760 **
158761 ** INSERT INTO rbu_tmp_xxx VALUES(?, ?, ? ...);
158762 **
158763 ** The number of bound variables is equal to the number of columns in
158764 ** the target table, plus one (for the rbu_control column), plus one more
158765 ** (for the rbu_rowid column) if the target table is an implicit IPK or
158766 ** virtual table.
158767 */
158768 static void rbuObjIterPrepareTmpInsert(
158769 sqlite3rbu *p,
158770 RbuObjIter *pIter,
158771 const char *zCollist,
158772 const char *zRbuRowid
158773 ){
158774 int bRbuRowid = (pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE);
158775 char *zBind = rbuObjIterGetBindlist(p, pIter->nTblCol + 1 + bRbuRowid);
158776 if( zBind ){
158777 assert( pIter->pTmpInsert==0 );
158778 p->rc = prepareFreeAndCollectError(
158779 p->dbRbu, &pIter->pTmpInsert, &p->zErrmsg, sqlite3_mprintf(
158780 "INSERT INTO %s.'rbu_tmp_%q'(rbu_control,%s%s) VALUES(%z)",
158781 p->zStateDb, pIter->zTbl, zCollist, zRbuRowid, zBind
158782 ));
158783 }
158784 }
158785
158786 static void rbuTmpInsertFunc(
158787 sqlite3_context *pCtx,
158788 int nVal,
158789 sqlite3_value **apVal
158790 ){
158791 sqlite3rbu *p = sqlite3_user_data(pCtx);
158792 int rc = SQLITE_OK;
158793 int i;
158794
158795 for(i=0; rc==SQLITE_OK && i<nVal; i++){
158796 rc = sqlite3_bind_value(p->objiter.pTmpInsert, i+1, apVal[i]);
@@ -158606,17 +158808,17 @@
158808 /*
158809 ** Ensure that the SQLite statement handles required to update the
158810 ** target database object currently indicated by the iterator passed
158811 ** as the second argument are available.
158812 */
158813 static int rbuObjIterPrepareAll(
158814 sqlite3rbu *p,
158815 RbuObjIter *pIter,
158816 int nOffset /* Add "LIMIT -1 OFFSET $nOffset" to SELECT */
158817 ){
158818 assert( pIter->bCleanup==0 );
158819 if( pIter->pSelect==0 && rbuObjIterCacheTableInfo(p, pIter)==SQLITE_OK ){
158820 const int tnum = pIter->iTnum;
158821 char *zCollist = 0; /* List of indexed columns */
158822 char **pz = &p->zErrmsg;
158823 const char *zIdx = pIter->zIdx;
158824 char *zLimit = 0;
@@ -158632,104 +158834,104 @@
158834 char *zImposterPK = 0; /* Primary key declaration for imposter */
158835 char *zWhere = 0; /* WHERE clause on PK columns */
158836 char *zBind = 0;
158837 int nBind = 0;
158838
158839 assert( pIter->eType!=RBU_PK_VTAB );
158840 zCollist = rbuObjIterGetIndexCols(
158841 p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
158842 );
158843 zBind = rbuObjIterGetBindlist(p, nBind);
158844
158845 /* Create the imposter table used to write to this index. */
158846 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
158847 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
158848 rbuMPrintfExec(p, p->dbMain,
158849 "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
158850 zTbl, zImposterCols, zImposterPK
158851 );
158852 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 0);
158853
158854 /* Create the statement to insert index entries */
158855 pIter->nCol = nBind;
158856 if( p->rc==SQLITE_OK ){
158857 p->rc = prepareFreeAndCollectError(
158858 p->dbMain, &pIter->pInsert, &p->zErrmsg,
158859 sqlite3_mprintf("INSERT INTO \"rbu_imp_%w\" VALUES(%s)", zTbl, zBind)
158860 );
158861 }
158862
158863 /* And to delete index entries */
158864 if( p->rc==SQLITE_OK ){
158865 p->rc = prepareFreeAndCollectError(
158866 p->dbMain, &pIter->pDelete, &p->zErrmsg,
158867 sqlite3_mprintf("DELETE FROM \"rbu_imp_%w\" WHERE %s", zTbl, zWhere)
158868 );
158869 }
158870
158871 /* Create the SELECT statement to read keys in sorted order */
158872 if( p->rc==SQLITE_OK ){
158873 char *zSql;
158874 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158875 zSql = sqlite3_mprintf(
158876 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' ORDER BY %s%s",
158877 zCollist, p->zStateDb, pIter->zTbl,
158878 zCollist, zLimit
158879 );
158880 }else{
158881 zSql = sqlite3_mprintf(
158882 "SELECT %s, rbu_control FROM 'data_%q' "
158883 "WHERE typeof(rbu_control)='integer' AND rbu_control!=1 "
158884 "UNION ALL "
158885 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' "
158886 "ORDER BY %s%s",
158887 zCollist, pIter->zTbl,
158888 zCollist, p->zStateDb, pIter->zTbl,
158889 zCollist, zLimit
158890 );
158891 }
158892 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
158893 }
158894
158895 sqlite3_free(zImposterCols);
158896 sqlite3_free(zImposterPK);
158897 sqlite3_free(zWhere);
158898 sqlite3_free(zBind);
158899 }else{
158900 int bRbuRowid = (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE);
158901 const char *zTbl = pIter->zTbl; /* Table this step applies to */
158902 const char *zWrite; /* Imposter table name */
158903
158904 char *zBindings = rbuObjIterGetBindlist(p, pIter->nTblCol + bRbuRowid);
158905 char *zWhere = rbuObjIterGetWhere(p, pIter);
158906 char *zOldlist = rbuObjIterGetOldlist(p, pIter, "old");
158907 char *zNewlist = rbuObjIterGetOldlist(p, pIter, "new");
158908
158909 zCollist = rbuObjIterGetCollist(p, pIter);
158910 pIter->nCol = pIter->nTblCol;
158911
158912 /* Create the SELECT statement to read keys from data_xxx */
158913 if( p->rc==SQLITE_OK ){
158914 p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
158915 sqlite3_mprintf(
158916 "SELECT %s, rbu_control%s FROM 'data_%q'%s",
158917 zCollist, (bRbuRowid ? ", rbu_rowid" : ""), zTbl, zLimit
158918 )
158919 );
158920 }
158921
158922 /* Create the imposter table or tables (if required). */
158923 rbuCreateImposterTable(p, pIter);
158924 rbuCreateImposterTable2(p, pIter);
158925 zWrite = (pIter->eType==RBU_PK_VTAB ? "" : "rbu_imp_");
158926
158927 /* Create the INSERT statement to write to the target PK b-tree */
158928 if( p->rc==SQLITE_OK ){
158929 p->rc = prepareFreeAndCollectError(p->dbMain, &pIter->pInsert, pz,
158930 sqlite3_mprintf(
158931 "INSERT INTO \"%s%w\"(%s%s) VALUES(%s)",
158932 zWrite, zTbl, zCollist, (bRbuRowid ? ", _rowid_" : ""), zBindings
158933 )
158934 );
158935 }
158936
158937 /* Create the DELETE statement to write to the target PK b-tree */
@@ -158740,55 +158942,55 @@
158942 )
158943 );
158944 }
158945
158946 if( pIter->abIndexed ){
158947 const char *zRbuRowid = "";
158948 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158949 zRbuRowid = ", rbu_rowid";
158950 }
158951
158952 /* Create the rbu_tmp_xxx table and the triggers to populate it. */
158953 rbuMPrintfExec(p, p->dbRbu,
158954 "CREATE TABLE IF NOT EXISTS %s.'rbu_tmp_%q' AS "
158955 "SELECT *%s FROM 'data_%q' WHERE 0;"
158956 , p->zStateDb
158957 , zTbl, (pIter->eType==RBU_PK_EXTERNAL ? ", 0 AS rbu_rowid" : "")
158958 , zTbl
158959 );
158960
158961 rbuMPrintfExec(p, p->dbMain,
158962 "CREATE TEMP TRIGGER rbu_delete_tr BEFORE DELETE ON \"%s%w\" "
158963 "BEGIN "
158964 " SELECT rbu_tmp_insert(2, %s);"
158965 "END;"
158966
158967 "CREATE TEMP TRIGGER rbu_update1_tr BEFORE UPDATE ON \"%s%w\" "
158968 "BEGIN "
158969 " SELECT rbu_tmp_insert(2, %s);"
158970 "END;"
158971
158972 "CREATE TEMP TRIGGER rbu_update2_tr AFTER UPDATE ON \"%s%w\" "
158973 "BEGIN "
158974 " SELECT rbu_tmp_insert(3, %s);"
158975 "END;",
158976 zWrite, zTbl, zOldlist,
158977 zWrite, zTbl, zOldlist,
158978 zWrite, zTbl, zNewlist
158979 );
158980
158981 if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
158982 rbuMPrintfExec(p, p->dbMain,
158983 "CREATE TEMP TRIGGER rbu_insert_tr AFTER INSERT ON \"%s%w\" "
158984 "BEGIN "
158985 " SELECT rbu_tmp_insert(0, %s);"
158986 "END;",
158987 zWrite, zTbl, zNewlist
158988 );
158989 }
158990
158991 rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
158992 }
158993
158994 sqlite3_free(zWhere);
158995 sqlite3_free(zOldlist);
158996 sqlite3_free(zNewlist);
@@ -158803,68 +159005,68 @@
159005
159006 /*
159007 ** Set output variable *ppStmt to point to an UPDATE statement that may
159008 ** be used to update the imposter table for the main table b-tree of the
159009 ** table object that pIter currently points to, assuming that the
159010 ** rbu_control column of the data_xyz table contains zMask.
159011 **
159012 ** If the zMask string does not specify any columns to update, then this
159013 ** is not an error. Output variable *ppStmt is set to NULL in this case.
159014 */
159015 static int rbuGetUpdateStmt(
159016 sqlite3rbu *p, /* RBU handle */
159017 RbuObjIter *pIter, /* Object iterator */
159018 const char *zMask, /* rbu_control value ('x.x.') */
159019 sqlite3_stmt **ppStmt /* OUT: UPDATE statement handle */
159020 ){
159021 RbuUpdateStmt **pp;
159022 RbuUpdateStmt *pUp = 0;
159023 int nUp = 0;
159024
159025 /* In case an error occurs */
159026 *ppStmt = 0;
159027
159028 /* Search for an existing statement. If one is found, shift it to the front
159029 ** of the LRU queue and return immediately. Otherwise, leave nUp pointing
159030 ** to the number of statements currently in the cache and pUp to the
159031 ** last object in the list. */
159032 for(pp=&pIter->pRbuUpdate; *pp; pp=&((*pp)->pNext)){
159033 pUp = *pp;
159034 if( strcmp(pUp->zMask, zMask)==0 ){
159035 *pp = pUp->pNext;
159036 pUp->pNext = pIter->pRbuUpdate;
159037 pIter->pRbuUpdate = pUp;
159038 *ppStmt = pUp->pUpdate;
159039 return SQLITE_OK;
159040 }
159041 nUp++;
159042 }
159043 assert( pUp==0 || pUp->pNext==0 );
159044
159045 if( nUp>=SQLITE_RBU_UPDATE_CACHESIZE ){
159046 for(pp=&pIter->pRbuUpdate; *pp!=pUp; pp=&((*pp)->pNext));
159047 *pp = 0;
159048 sqlite3_finalize(pUp->pUpdate);
159049 pUp->pUpdate = 0;
159050 }else{
159051 pUp = (RbuUpdateStmt*)rbuMalloc(p, sizeof(RbuUpdateStmt)+pIter->nTblCol+1);
159052 }
159053
159054 if( pUp ){
159055 char *zWhere = rbuObjIterGetWhere(p, pIter);
159056 char *zSet = rbuObjIterGetSetlist(p, pIter, zMask);
159057 char *zUpdate = 0;
159058
159059 pUp->zMask = (char*)&pUp[1];
159060 memcpy(pUp->zMask, zMask, pIter->nTblCol);
159061 pUp->pNext = pIter->pRbuUpdate;
159062 pIter->pRbuUpdate = pUp;
159063
159064 if( zSet ){
159065 const char *zPrefix = "";
159066
159067 if( pIter->eType!=RBU_PK_VTAB ) zPrefix = "rbu_imp_";
159068 zUpdate = sqlite3_mprintf("UPDATE \"%s%w\" SET %s WHERE %s",
159069 zPrefix, pIter->zTbl, zSet, zWhere
159070 );
159071 p->rc = prepareFreeAndCollectError(
159072 p->dbMain, &pUp->pUpdate, &p->zErrmsg, zUpdate
@@ -158876,11 +159078,11 @@
159078 }
159079
159080 return p->rc;
159081 }
159082
159083 static sqlite3 *rbuOpenDbhandle(sqlite3rbu *p, const char *zName){
159084 sqlite3 *db = 0;
159085 if( p->rc==SQLITE_OK ){
159086 const int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_URI;
159087 p->rc = sqlite3_open_v2(zName, &db, flags, p->zVfsName);
159088 if( p->rc ){
@@ -158891,51 +159093,51 @@
159093 }
159094 return db;
159095 }
159096
159097 /*
159098 ** Open the database handle and attach the RBU database as "rbu". If an
159099 ** error occurs, leave an error code and message in the RBU handle.
159100 */
159101 static void rbuOpenDatabase(sqlite3rbu *p){
159102 assert( p->rc==SQLITE_OK );
159103 assert( p->dbMain==0 && p->dbRbu==0 );
159104
159105 p->eStage = 0;
159106 p->dbMain = rbuOpenDbhandle(p, p->zTarget);
159107 p->dbRbu = rbuOpenDbhandle(p, p->zRbu);
159108
159109 /* If using separate RBU and state databases, attach the state database to
159110 ** the RBU db handle now. */
159111 if( p->zState ){
159112 rbuMPrintfExec(p, p->dbRbu, "ATTACH %Q AS stat", p->zState);
159113 memcpy(p->zStateDb, "stat", 4);
159114 }else{
159115 memcpy(p->zStateDb, "main", 4);
159116 }
159117
159118 if( p->rc==SQLITE_OK ){
159119 p->rc = sqlite3_create_function(p->dbMain,
159120 "rbu_tmp_insert", -1, SQLITE_UTF8, (void*)p, rbuTmpInsertFunc, 0, 0
159121 );
159122 }
159123
159124 if( p->rc==SQLITE_OK ){
159125 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
159126 }
159127 rbuMPrintfExec(p, p->dbMain, "SELECT * FROM sqlite_master");
159128
159129 /* Mark the database file just opened as an RBU target database. If
159130 ** this call returns SQLITE_NOTFOUND, then the RBU vfs is not in use.
159131 ** This is an error. */
159132 if( p->rc==SQLITE_OK ){
159133 p->rc = sqlite3_file_control(p->dbMain, "main", SQLITE_FCNTL_RBU, (void*)p);
159134 }
159135
159136 if( p->rc==SQLITE_NOTFOUND ){
159137 p->rc = SQLITE_ERROR;
159138 p->zErrmsg = sqlite3_mprintf("rbu vfs not found");
159139 }
159140 }
159141
159142 /*
159143 ** This routine is a copy of the sqlite3FileSuffix3() routine from the core.
@@ -158955,11 +159157,11 @@
159157 ** test.db-journal => test.nal
159158 ** test.db-wal => test.wal
159159 ** test.db-shm => test.shm
159160 ** test.db-mj7f3319fa => test.9fa
159161 */
159162 static void rbuFileSuffix3(const char *zBase, char *z){
159163 #ifdef SQLITE_ENABLE_8_3_NAMES
159164 #if SQLITE_ENABLE_8_3_NAMES<2
159165 if( sqlite3_uri_boolean(zBase, "8_3_names", 0) )
159166 #endif
159167 {
@@ -158976,11 +159178,11 @@
159178 ** as a 64-bit integer.
159179 **
159180 ** The checksum is store in the first page of xShmMap memory as an 8-byte
159181 ** blob starting at byte offset 40.
159182 */
159183 static i64 rbuShmChecksum(sqlite3rbu *p){
159184 i64 iRet = 0;
159185 if( p->rc==SQLITE_OK ){
159186 sqlite3_file *pDb = p->pTargetFd->pReal;
159187 u32 volatile *ptr;
159188 p->rc = pDb->pMethods->xShmMap(pDb, 0, 32*1024, 0, (void volatile**)&ptr);
@@ -158993,23 +159195,23 @@
159195
159196 /*
159197 ** This function is called as part of initializing or reinitializing an
159198 ** incremental checkpoint.
159199 **
159200 ** It populates the sqlite3rbu.aFrame[] array with the set of
159201 ** (wal frame -> db page) copy operations required to checkpoint the
159202 ** current wal file, and obtains the set of shm locks required to safely
159203 ** perform the copy operations directly on the file-system.
159204 **
159205 ** If argument pState is not NULL, then the incremental checkpoint is
159206 ** being resumed. In this case, if the checksum of the wal-index-header
159207 ** following recovery is not the same as the checksum saved in the RbuState
159208 ** object, then the rbu handle is set to DONE state. This occurs if some
159209 ** other client appends a transaction to the wal file in the middle of
159210 ** an incremental checkpoint.
159211 */
159212 static void rbuSetupCheckpoint(sqlite3rbu *p, RbuState *pState){
159213
159214 /* If pState is NULL, then the wal file may not have been opened and
159215 ** recovered. Running a read-statement here to ensure that doing so
159216 ** does not interfere with the "capture" process below. */
159217 if( pState==0 ){
@@ -159018,20 +159220,20 @@
159220 p->rc = sqlite3_exec(p->dbMain, "SELECT * FROM sqlite_master", 0, 0, 0);
159221 }
159222 }
159223
159224 /* Assuming no error has occurred, run a "restart" checkpoint with the
159225 ** sqlite3rbu.eStage variable set to CAPTURE. This turns on the following
159226 ** special behaviour in the rbu VFS:
159227 **
159228 ** * If the exclusive shm WRITER or READ0 lock cannot be obtained,
159229 ** the checkpoint fails with SQLITE_BUSY (normally SQLite would
159230 ** proceed with running a passive checkpoint instead of failing).
159231 **
159232 ** * Attempts to read from the *-wal file or write to the database file
159233 ** do not perform any IO. Instead, the frame/page combinations that
159234 ** would be read/written are recorded in the sqlite3rbu.aFrame[]
159235 ** array.
159236 **
159237 ** * Calls to xShmLock(UNLOCK) to release the exclusive shm WRITER,
159238 ** READ0 and CHECKPOINT locks taken as part of the checkpoint are
159239 ** no-ops. These locks will not be released until the connection
@@ -159047,76 +159249,76 @@
159249 ** data from the wal file into the database file according to the
159250 ** contents of aFrame[].
159251 */
159252 if( p->rc==SQLITE_OK ){
159253 int rc2;
159254 p->eStage = RBU_STAGE_CAPTURE;
159255 rc2 = sqlite3_exec(p->dbMain, "PRAGMA main.wal_checkpoint=restart", 0, 0,0);
159256 if( rc2!=SQLITE_INTERNAL ) p->rc = rc2;
159257 }
159258
159259 if( p->rc==SQLITE_OK ){
159260 p->eStage = RBU_STAGE_CKPT;
159261 p->nStep = (pState ? pState->nRow : 0);
159262 p->aBuf = rbuMalloc(p, p->pgsz);
159263 p->iWalCksum = rbuShmChecksum(p);
159264 }
159265
159266 if( p->rc==SQLITE_OK && pState && pState->iWalCksum!=p->iWalCksum ){
159267 p->rc = SQLITE_DONE;
159268 p->eStage = RBU_STAGE_DONE;
159269 }
159270 }
159271
159272 /*
159273 ** Called when iAmt bytes are read from offset iOff of the wal file while
159274 ** the rbu object is in capture mode. Record the frame number of the frame
159275 ** being read in the aFrame[] array.
159276 */
159277 static int rbuCaptureWalRead(sqlite3rbu *pRbu, i64 iOff, int iAmt){
159278 const u32 mReq = (1<<WAL_LOCK_WRITE)|(1<<WAL_LOCK_CKPT)|(1<<WAL_LOCK_READ0);
159279 u32 iFrame;
159280
159281 if( pRbu->mLock!=mReq ){
159282 pRbu->rc = SQLITE_BUSY;
159283 return SQLITE_INTERNAL;
159284 }
159285
159286 pRbu->pgsz = iAmt;
159287 if( pRbu->nFrame==pRbu->nFrameAlloc ){
159288 int nNew = (pRbu->nFrameAlloc ? pRbu->nFrameAlloc : 64) * 2;
159289 RbuFrame *aNew;
159290 aNew = (RbuFrame*)sqlite3_realloc(pRbu->aFrame, nNew * sizeof(RbuFrame));
159291 if( aNew==0 ) return SQLITE_NOMEM;
159292 pRbu->aFrame = aNew;
159293 pRbu->nFrameAlloc = nNew;
159294 }
159295
159296 iFrame = (u32)((iOff-32) / (i64)(iAmt+24)) + 1;
159297 if( pRbu->iMaxFrame<iFrame ) pRbu->iMaxFrame = iFrame;
159298 pRbu->aFrame[pRbu->nFrame].iWalFrame = iFrame;
159299 pRbu->aFrame[pRbu->nFrame].iDbPage = 0;
159300 pRbu->nFrame++;
159301 return SQLITE_OK;
159302 }
159303
159304 /*
159305 ** Called when a page of data is written to offset iOff of the database
159306 ** file while the rbu handle is in capture mode. Record the page number
159307 ** of the page being written in the aFrame[] array.
159308 */
159309 static int rbuCaptureDbWrite(sqlite3rbu *pRbu, i64 iOff){
159310 pRbu->aFrame[pRbu->nFrame-1].iDbPage = (u32)(iOff / pRbu->pgsz) + 1;
159311 return SQLITE_OK;
159312 }
159313
159314 /*
159315 ** This is called as part of an incremental checkpoint operation. Copy
159316 ** a single frame of data from the wal file into the database file, as
159317 ** indicated by the RbuFrame object.
159318 */
159319 static void rbuCheckpointFrame(sqlite3rbu *p, RbuFrame *pFrame){
159320 sqlite3_file *pWal = p->pTargetFd->pWalFd->pReal;
159321 sqlite3_file *pDb = p->pTargetFd->pReal;
159322 i64 iOff;
159323
159324 assert( p->rc==SQLITE_OK );
@@ -159130,33 +159332,33 @@
159332
159333
159334 /*
159335 ** Take an EXCLUSIVE lock on the database file.
159336 */
159337 static void rbuLockDatabase(sqlite3rbu *p){
159338 sqlite3_file *pReal = p->pTargetFd->pReal;
159339 assert( p->rc==SQLITE_OK );
159340 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_SHARED);
159341 if( p->rc==SQLITE_OK ){
159342 p->rc = pReal->pMethods->xLock(pReal, SQLITE_LOCK_EXCLUSIVE);
159343 }
159344 }
159345
159346 /*
159347 ** The RBU handle is currently in RBU_STAGE_OAL state, with a SHARED lock
159348 ** on the database file. This proc moves the *-oal file to the *-wal path,
159349 ** then reopens the database file (this time in vanilla, non-oal, WAL mode).
159350 ** If an error occurs, leave an error code and error message in the rbu
159351 ** handle.
159352 */
159353 static void rbuMoveOalFile(sqlite3rbu *p){
159354 const char *zBase = sqlite3_db_filename(p->dbMain, "main");
159355
159356 char *zWal = sqlite3_mprintf("%s-wal", zBase);
159357 char *zOal = sqlite3_mprintf("%s-oal", zBase);
159358
159359 assert( p->eStage==RBU_STAGE_MOVE );
159360 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159361 if( zWal==0 || zOal==0 ){
159362 p->rc = SQLITE_NOMEM;
159363 }else{
159364 /* Move the *-oal file to *-wal. At this point connection p->db is
@@ -159164,25 +159366,25 @@
159366 ** in WAL mode). So no other connection may be writing the db.
159367 **
159368 ** In order to ensure that there are no database readers, an EXCLUSIVE
159369 ** lock is obtained here before the *-oal is moved to *-wal.
159370 */
159371 rbuLockDatabase(p);
159372 if( p->rc==SQLITE_OK ){
159373 rbuFileSuffix3(zBase, zWal);
159374 rbuFileSuffix3(zBase, zOal);
159375
159376 /* Re-open the databases. */
159377 rbuObjIterFinalize(&p->objiter);
159378 sqlite3_close(p->dbMain);
159379 sqlite3_close(p->dbRbu);
159380 p->rc = rename(zOal, zWal) ? SQLITE_IOERR : SQLITE_OK;
159381 if( p->rc==SQLITE_OK ){
159382 p->dbMain = 0;
159383 p->dbRbu = 0;
159384 rbuOpenDatabase(p);
159385 rbuSetupCheckpoint(p, 0);
159386 }
159387 }
159388 }
159389
159390 sqlite3_free(zWal);
@@ -159193,36 +159395,36 @@
159395 ** The SELECT statement iterating through the keys for the current object
159396 ** (p->objiter.pSelect) currently points to a valid row. This function
159397 ** determines the type of operation requested by this row and returns
159398 ** one of the following values to indicate the result:
159399 **
159400 ** * RBU_INSERT
159401 ** * RBU_DELETE
159402 ** * RBU_IDX_DELETE
159403 ** * RBU_UPDATE
159404 **
159405 ** If RBU_UPDATE is returned, then output variable *pzMask is set to
159406 ** point to the text value indicating the columns to update.
159407 **
159408 ** If the rbu_control field contains an invalid value, an error code and
159409 ** message are left in the RBU handle and zero returned.
159410 */
159411 static int rbuStepType(sqlite3rbu *p, const char **pzMask){
159412 int iCol = p->objiter.nCol; /* Index of rbu_control column */
159413 int res = 0; /* Return value */
159414
159415 switch( sqlite3_column_type(p->objiter.pSelect, iCol) ){
159416 case SQLITE_INTEGER: {
159417 int iVal = sqlite3_column_int(p->objiter.pSelect, iCol);
159418 if( iVal==0 ){
159419 res = RBU_INSERT;
159420 }else if( iVal==1 ){
159421 res = RBU_DELETE;
159422 }else if( iVal==2 ){
159423 res = RBU_IDX_DELETE;
159424 }else if( iVal==3 ){
159425 res = RBU_IDX_INSERT;
159426 }
159427 break;
159428 }
159429
159430 case SQLITE_TEXT: {
@@ -159230,21 +159432,21 @@
159432 if( z==0 ){
159433 p->rc = SQLITE_NOMEM;
159434 }else{
159435 *pzMask = (const char*)z;
159436 }
159437 res = RBU_UPDATE;
159438
159439 break;
159440 }
159441
159442 default:
159443 break;
159444 }
159445
159446 if( res==0 ){
159447 rbuBadControlError(p);
159448 }
159449 return res;
159450 }
159451
159452 #ifdef SQLITE_DEBUG
@@ -159258,82 +159460,82 @@
159460 #else
159461 # define assertColumnName(x,y,z)
159462 #endif
159463
159464 /*
159465 ** This function does the work for an sqlite3rbu_step() call.
159466 **
159467 ** The object-iterator (p->objiter) currently points to a valid object,
159468 ** and the input cursor (p->objiter.pSelect) currently points to a valid
159469 ** input row. Perform whatever processing is required and return.
159470 **
159471 ** If no error occurs, SQLITE_OK is returned. Otherwise, an error code
159472 ** and message is left in the RBU handle and a copy of the error code
159473 ** returned.
159474 */
159475 static int rbuStep(sqlite3rbu *p){
159476 RbuObjIter *pIter = &p->objiter;
159477 const char *zMask = 0;
159478 int i;
159479 int eType = rbuStepType(p, &zMask);
159480
159481 if( eType ){
159482 assert( eType!=RBU_UPDATE || pIter->zIdx==0 );
159483
159484 if( pIter->zIdx==0 && eType==RBU_IDX_DELETE ){
159485 rbuBadControlError(p);
159486 }
159487 else if(
159488 eType==RBU_INSERT
159489 || eType==RBU_DELETE
159490 || eType==RBU_IDX_DELETE
159491 || eType==RBU_IDX_INSERT
159492 ){
159493 sqlite3_value *pVal;
159494 sqlite3_stmt *pWriter;
159495
159496 assert( eType!=RBU_UPDATE );
159497 assert( eType!=RBU_DELETE || pIter->zIdx==0 );
159498
159499 if( eType==RBU_IDX_DELETE || eType==RBU_DELETE ){
159500 pWriter = pIter->pDelete;
159501 }else{
159502 pWriter = pIter->pInsert;
159503 }
159504
159505 for(i=0; i<pIter->nCol; i++){
159506 /* If this is an INSERT into a table b-tree and the table has an
159507 ** explicit INTEGER PRIMARY KEY, check that this is not an attempt
159508 ** to write a NULL into the IPK column. That is not permitted. */
159509 if( eType==RBU_INSERT
159510 && pIter->zIdx==0 && pIter->eType==RBU_PK_IPK && pIter->abTblPk[i]
159511 && sqlite3_column_type(pIter->pSelect, i)==SQLITE_NULL
159512 ){
159513 p->rc = SQLITE_MISMATCH;
159514 p->zErrmsg = sqlite3_mprintf("datatype mismatch");
159515 goto step_out;
159516 }
159517
159518 if( eType==RBU_DELETE && pIter->abTblPk[i]==0 ){
159519 continue;
159520 }
159521
159522 pVal = sqlite3_column_value(pIter->pSelect, i);
159523 p->rc = sqlite3_bind_value(pWriter, i+1, pVal);
159524 if( p->rc ) goto step_out;
159525 }
159526 if( pIter->zIdx==0
159527 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
159528 ){
159529 /* For a virtual table, or a table with no primary key, the
159530 ** SELECT statement is:
159531 **
159532 ** SELECT <cols>, rbu_control, rbu_rowid FROM ....
159533 **
159534 ** Hence column_value(pIter->nCol+1).
159535 */
159536 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
159537 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159538 p->rc = sqlite3_bind_value(pWriter, pIter->nCol+1, pVal);
159539 }
159540 if( p->rc==SQLITE_OK ){
159541 sqlite3_step(pWriter);
@@ -159340,25 +159542,25 @@
159542 p->rc = resetAndCollectError(pWriter, &p->zErrmsg);
159543 }
159544 }else{
159545 sqlite3_value *pVal;
159546 sqlite3_stmt *pUpdate = 0;
159547 assert( eType==RBU_UPDATE );
159548 rbuGetUpdateStmt(p, pIter, zMask, &pUpdate);
159549 if( pUpdate ){
159550 for(i=0; p->rc==SQLITE_OK && i<pIter->nCol; i++){
159551 char c = zMask[pIter->aiSrcOrder[i]];
159552 pVal = sqlite3_column_value(pIter->pSelect, i);
159553 if( pIter->abTblPk[i] || c=='x' || c=='d' ){
159554 p->rc = sqlite3_bind_value(pUpdate, i+1, pVal);
159555 }
159556 }
159557 if( p->rc==SQLITE_OK
159558 && (pIter->eType==RBU_PK_VTAB || pIter->eType==RBU_PK_NONE)
159559 ){
159560 /* Bind the rbu_rowid value to column _rowid_ */
159561 assertColumnName(pIter->pSelect, pIter->nCol+1, "rbu_rowid");
159562 pVal = sqlite3_column_value(pIter->pSelect, pIter->nCol+1);
159563 p->rc = sqlite3_bind_value(pUpdate, pIter->nCol+1, pVal);
159564 }
159565 if( p->rc==SQLITE_OK ){
159566 sqlite3_step(pUpdate);
@@ -159373,11 +159575,11 @@
159575 }
159576
159577 /*
159578 ** Increment the schema cookie of the main database opened by p->dbMain.
159579 */
159580 static void rbuIncrSchemaCookie(sqlite3rbu *p){
159581 if( p->rc==SQLITE_OK ){
159582 int iCookie = 1000000;
159583 sqlite3_stmt *pStmt;
159584
159585 p->rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
@@ -159390,49 +159592,49 @@
159592 ** statement reads is page 1, which is guaranteed to be in the cache.
159593 ** And no memory allocations are required. */
159594 if( SQLITE_ROW==sqlite3_step(pStmt) ){
159595 iCookie = sqlite3_column_int(pStmt, 0);
159596 }
159597 rbuFinalize(p, pStmt);
159598 }
159599 if( p->rc==SQLITE_OK ){
159600 rbuMPrintfExec(p, p->dbMain, "PRAGMA schema_version = %d", iCookie+1);
159601 }
159602 }
159603 }
159604
159605 /*
159606 ** Update the contents of the rbu_state table within the rbu database. The
159607 ** value stored in the RBU_STATE_STAGE column is eStage. All other values
159608 ** are determined by inspecting the rbu handle passed as the first argument.
159609 */
159610 static void rbuSaveState(sqlite3rbu *p, int eStage){
159611 if( p->rc==SQLITE_OK || p->rc==SQLITE_DONE ){
159612 sqlite3_stmt *pInsert = 0;
159613 int rc;
159614
159615 assert( p->zErrmsg==0 );
159616 rc = prepareFreeAndCollectError(p->dbRbu, &pInsert, &p->zErrmsg,
159617 sqlite3_mprintf(
159618 "INSERT OR REPLACE INTO %s.rbu_state(k, v) VALUES "
159619 "(%d, %d), "
159620 "(%d, %Q), "
159621 "(%d, %Q), "
159622 "(%d, %d), "
159623 "(%d, %d), "
159624 "(%d, %lld), "
159625 "(%d, %lld), "
159626 "(%d, %lld) ",
159627 p->zStateDb,
159628 RBU_STATE_STAGE, eStage,
159629 RBU_STATE_TBL, p->objiter.zTbl,
159630 RBU_STATE_IDX, p->objiter.zIdx,
159631 RBU_STATE_ROW, p->nStep,
159632 RBU_STATE_PROGRESS, p->nProgress,
159633 RBU_STATE_CKPT, p->iWalCksum,
159634 RBU_STATE_COOKIE, (i64)p->pTargetFd->iCookie,
159635 RBU_STATE_OALSZ, p->iOalSz
159636 )
159637 );
159638 assert( pInsert==0 || rc==SQLITE_OK );
159639
159640 if( rc==SQLITE_OK ){
@@ -159443,71 +159645,71 @@
159645 }
159646 }
159647
159648
159649 /*
159650 ** Step the RBU object.
159651 */
159652 SQLITE_API int SQLITE_STDCALL sqlite3rbu_step(sqlite3rbu *p){
159653 if( p ){
159654 switch( p->eStage ){
159655 case RBU_STAGE_OAL: {
159656 RbuObjIter *pIter = &p->objiter;
159657 while( p->rc==SQLITE_OK && pIter->zTbl ){
159658
159659 if( pIter->bCleanup ){
159660 /* Clean up the rbu_tmp_xxx table for the previous table. It
159661 ** cannot be dropped as there are currently active SQL statements.
159662 ** But the contents can be deleted. */
159663 if( pIter->abIndexed ){
159664 rbuMPrintfExec(p, p->dbRbu,
159665 "DELETE FROM %s.'rbu_tmp_%q'", p->zStateDb, pIter->zTbl
159666 );
159667 }
159668 }else{
159669 rbuObjIterPrepareAll(p, pIter, 0);
159670
159671 /* Advance to the next row to process. */
159672 if( p->rc==SQLITE_OK ){
159673 int rc = sqlite3_step(pIter->pSelect);
159674 if( rc==SQLITE_ROW ){
159675 p->nProgress++;
159676 p->nStep++;
159677 return rbuStep(p);
159678 }
159679 p->rc = sqlite3_reset(pIter->pSelect);
159680 p->nStep = 0;
159681 }
159682 }
159683
159684 rbuObjIterNext(p, pIter);
159685 }
159686
159687 if( p->rc==SQLITE_OK ){
159688 assert( pIter->zTbl==0 );
159689 rbuSaveState(p, RBU_STAGE_MOVE);
159690 rbuIncrSchemaCookie(p);
159691 if( p->rc==SQLITE_OK ){
159692 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
159693 }
159694 if( p->rc==SQLITE_OK ){
159695 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
159696 }
159697 p->eStage = RBU_STAGE_MOVE;
159698 }
159699 break;
159700 }
159701
159702 case RBU_STAGE_MOVE: {
159703 if( p->rc==SQLITE_OK ){
159704 rbuMoveOalFile(p);
159705 p->nProgress++;
159706 }
159707 break;
159708 }
159709
159710 case RBU_STAGE_CKPT: {
159711 if( p->rc==SQLITE_OK ){
159712 if( p->nStep>=p->nFrame ){
159713 sqlite3_file *pDb = p->pTargetFd->pReal;
159714
159715 /* Sync the db file */
@@ -159521,16 +159723,16 @@
159723 ((u32 volatile*)ptr)[24] = p->iMaxFrame;
159724 }
159725 }
159726
159727 if( p->rc==SQLITE_OK ){
159728 p->eStage = RBU_STAGE_DONE;
159729 p->rc = SQLITE_DONE;
159730 }
159731 }else{
159732 RbuFrame *pFrame = &p->aFrame[p->nStep];
159733 rbuCheckpointFrame(p, pFrame);
159734 p->nStep++;
159735 }
159736 p->nProgress++;
159737 }
159738 break;
@@ -159544,78 +159746,78 @@
159746 return SQLITE_NOMEM;
159747 }
159748 }
159749
159750 /*
159751 ** Free an RbuState object allocated by rbuLoadState().
159752 */
159753 static void rbuFreeState(RbuState *p){
159754 if( p ){
159755 sqlite3_free(p->zTbl);
159756 sqlite3_free(p->zIdx);
159757 sqlite3_free(p);
159758 }
159759 }
159760
159761 /*
159762 ** Allocate an RbuState object and load the contents of the rbu_state
159763 ** table into it. Return a pointer to the new object. It is the
159764 ** responsibility of the caller to eventually free the object using
159765 ** sqlite3_free().
159766 **
159767 ** If an error occurs, leave an error code and message in the rbu handle
159768 ** and return NULL.
159769 */
159770 static RbuState *rbuLoadState(sqlite3rbu *p){
159771 RbuState *pRet = 0;
159772 sqlite3_stmt *pStmt = 0;
159773 int rc;
159774 int rc2;
159775
159776 pRet = (RbuState*)rbuMalloc(p, sizeof(RbuState));
159777 if( pRet==0 ) return 0;
159778
159779 rc = prepareFreeAndCollectError(p->dbRbu, &pStmt, &p->zErrmsg,
159780 sqlite3_mprintf("SELECT k, v FROM %s.rbu_state", p->zStateDb)
159781 );
159782 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
159783 switch( sqlite3_column_int(pStmt, 0) ){
159784 case RBU_STATE_STAGE:
159785 pRet->eStage = sqlite3_column_int(pStmt, 1);
159786 if( pRet->eStage!=RBU_STAGE_OAL
159787 && pRet->eStage!=RBU_STAGE_MOVE
159788 && pRet->eStage!=RBU_STAGE_CKPT
159789 ){
159790 p->rc = SQLITE_CORRUPT;
159791 }
159792 break;
159793
159794 case RBU_STATE_TBL:
159795 pRet->zTbl = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159796 break;
159797
159798 case RBU_STATE_IDX:
159799 pRet->zIdx = rbuStrndup((char*)sqlite3_column_text(pStmt, 1), &rc);
159800 break;
159801
159802 case RBU_STATE_ROW:
159803 pRet->nRow = sqlite3_column_int(pStmt, 1);
159804 break;
159805
159806 case RBU_STATE_PROGRESS:
159807 pRet->nProgress = sqlite3_column_int64(pStmt, 1);
159808 break;
159809
159810 case RBU_STATE_CKPT:
159811 pRet->iWalCksum = sqlite3_column_int64(pStmt, 1);
159812 break;
159813
159814 case RBU_STATE_COOKIE:
159815 pRet->iCookie = (u32)sqlite3_column_int64(pStmt, 1);
159816 break;
159817
159818 case RBU_STATE_OALSZ:
159819 pRet->iOalSz = (u32)sqlite3_column_int64(pStmt, 1);
159820 break;
159821
159822 default:
159823 rc = SQLITE_CORRUPT;
@@ -159632,142 +159834,142 @@
159834 /*
159835 ** Compare strings z1 and z2, returning 0 if they are identical, or non-zero
159836 ** otherwise. Either or both argument may be NULL. Two NULL values are
159837 ** considered equal, and NULL is considered distinct from all other values.
159838 */
159839 static int rbuStrCompare(const char *z1, const char *z2){
159840 if( z1==0 && z2==0 ) return 0;
159841 if( z1==0 || z2==0 ) return 1;
159842 return (sqlite3_stricmp(z1, z2)!=0);
159843 }
159844
159845 /*
159846 ** This function is called as part of sqlite3rbu_open() when initializing
159847 ** an rbu handle in OAL stage. If the rbu update has not started (i.e.
159848 ** the rbu_state table was empty) it is a no-op. Otherwise, it arranges
159849 ** things so that the next call to sqlite3rbu_step() continues on from
159850 ** where the previous rbu handle left off.
159851 **
159852 ** If an error occurs, an error code and error message are left in the
159853 ** rbu handle passed as the first argument.
159854 */
159855 static void rbuSetupOal(sqlite3rbu *p, RbuState *pState){
159856 assert( p->rc==SQLITE_OK );
159857 if( pState->zTbl ){
159858 RbuObjIter *pIter = &p->objiter;
159859 int rc = SQLITE_OK;
159860
159861 while( rc==SQLITE_OK && pIter->zTbl && (pIter->bCleanup
159862 || rbuStrCompare(pIter->zIdx, pState->zIdx)
159863 || rbuStrCompare(pIter->zTbl, pState->zTbl)
159864 )){
159865 rc = rbuObjIterNext(p, pIter);
159866 }
159867
159868 if( rc==SQLITE_OK && !pIter->zTbl ){
159869 rc = SQLITE_ERROR;
159870 p->zErrmsg = sqlite3_mprintf("rbu_state mismatch error");
159871 }
159872
159873 if( rc==SQLITE_OK ){
159874 p->nStep = pState->nRow;
159875 rc = rbuObjIterPrepareAll(p, &p->objiter, p->nStep);
159876 }
159877
159878 p->rc = rc;
159879 }
159880 }
159881
159882 /*
159883 ** If there is a "*-oal" file in the file-system corresponding to the
159884 ** target database in the file-system, delete it. If an error occurs,
159885 ** leave an error code and error message in the rbu handle.
159886 */
159887 static void rbuDeleteOalFile(sqlite3rbu *p){
159888 char *zOal = sqlite3_mprintf("%s-oal", p->zTarget);
159889 assert( p->rc==SQLITE_OK && p->zErrmsg==0 );
159890 unlink(zOal);
159891 sqlite3_free(zOal);
159892 }
159893
159894 /*
159895 ** Allocate a private rbu VFS for the rbu handle passed as the only
159896 ** argument. This VFS will be used unless the call to sqlite3rbu_open()
159897 ** specified a URI with a vfs=? option in place of a target database
159898 ** file name.
159899 */
159900 static void rbuCreateVfs(sqlite3rbu *p){
159901 int rnd;
159902 char zRnd[64];
159903
159904 assert( p->rc==SQLITE_OK );
159905 sqlite3_randomness(sizeof(int), (void*)&rnd);
159906 sprintf(zRnd, "rbu_vfs_%d", rnd);
159907 p->rc = sqlite3rbu_create_vfs(zRnd, 0);
159908 if( p->rc==SQLITE_OK ){
159909 sqlite3_vfs *pVfs = sqlite3_vfs_find(zRnd);
159910 assert( pVfs );
159911 p->zVfsName = pVfs->zName;
159912 }
159913 }
159914
159915 /*
159916 ** Destroy the private VFS created for the rbu handle passed as the only
159917 ** argument by an earlier call to rbuCreateVfs().
159918 */
159919 static void rbuDeleteVfs(sqlite3rbu *p){
159920 if( p->zVfsName ){
159921 sqlite3rbu_destroy_vfs(p->zVfsName);
159922 p->zVfsName = 0;
159923 }
159924 }
159925
159926 /*
159927 ** Open and return a new RBU handle.
159928 */
159929 SQLITE_API sqlite3rbu *SQLITE_STDCALL sqlite3rbu_open(
159930 const char *zTarget,
159931 const char *zRbu,
159932 const char *zState
159933 ){
159934 sqlite3rbu *p;
159935 int nTarget = strlen(zTarget);
159936 int nRbu = strlen(zRbu);
159937 int nState = zState ? strlen(zState) : 0;
159938
159939 p = (sqlite3rbu*)sqlite3_malloc(sizeof(sqlite3rbu)+nTarget+1+nRbu+1+nState+1);
159940 if( p ){
159941 RbuState *pState = 0;
159942
159943 /* Create the custom VFS. */
159944 memset(p, 0, sizeof(sqlite3rbu));
159945 rbuCreateVfs(p);
159946
159947 /* Open the target database */
159948 if( p->rc==SQLITE_OK ){
159949 p->zTarget = (char*)&p[1];
159950 memcpy(p->zTarget, zTarget, nTarget+1);
159951 p->zRbu = &p->zTarget[nTarget+1];
159952 memcpy(p->zRbu, zRbu, nRbu+1);
159953 if( zState ){
159954 p->zState = &p->zRbu[nRbu+1];
159955 memcpy(p->zState, zState, nState+1);
159956 }
159957 rbuOpenDatabase(p);
159958 }
159959
159960 /* If it has not already been created, create the rbu_state table */
159961 rbuMPrintfExec(p, p->dbRbu, RBU_CREATE_STATE, p->zStateDb);
159962
159963 if( p->rc==SQLITE_OK ){
159964 pState = rbuLoadState(p);
159965 assert( pState || p->rc!=SQLITE_OK );
159966 if( p->rc==SQLITE_OK ){
159967
159968 if( pState->eStage==0 ){
159969 rbuDeleteOalFile(p);
159970 p->eStage = RBU_STAGE_OAL;
159971 }else{
159972 p->eStage = pState->eStage;
159973 }
159974 p->nProgress = pState->nProgress;
159975 p->iOalSz = pState->iOalSz;
@@ -159774,97 +159976,97 @@
159976 }
159977 }
159978 assert( p->rc!=SQLITE_OK || p->eStage!=0 );
159979
159980 if( p->rc==SQLITE_OK && p->pTargetFd->pWalFd ){
159981 if( p->eStage==RBU_STAGE_OAL ){
159982 p->rc = SQLITE_ERROR;
159983 p->zErrmsg = sqlite3_mprintf("cannot update wal mode database");
159984 }else if( p->eStage==RBU_STAGE_MOVE ){
159985 p->eStage = RBU_STAGE_CKPT;
159986 p->nStep = 0;
159987 }
159988 }
159989
159990 if( p->rc==SQLITE_OK
159991 && (p->eStage==RBU_STAGE_OAL || p->eStage==RBU_STAGE_MOVE)
159992 && pState->eStage!=0 && p->pTargetFd->iCookie!=pState->iCookie
159993 ){
159994 /* At this point (pTargetFd->iCookie) contains the value of the
159995 ** change-counter cookie (the thing that gets incremented when a
159996 ** transaction is committed in rollback mode) currently stored on
159997 ** page 1 of the database file. */
159998 p->rc = SQLITE_BUSY;
159999 p->zErrmsg = sqlite3_mprintf("database modified during rbu update");
160000 }
160001
160002 if( p->rc==SQLITE_OK ){
160003 if( p->eStage==RBU_STAGE_OAL ){
160004
160005 /* Open transactions both databases. The *-oal file is opened or
160006 ** created at this point. */
160007 p->rc = sqlite3_exec(p->dbMain, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160008 if( p->rc==SQLITE_OK ){
160009 p->rc = sqlite3_exec(p->dbRbu, "BEGIN IMMEDIATE", 0, 0, &p->zErrmsg);
160010 }
160011
160012 /* Point the object iterator at the first object */
160013 if( p->rc==SQLITE_OK ){
160014 p->rc = rbuObjIterFirst(p, &p->objiter);
160015 }
160016
160017 /* If the RBU database contains no data_xxx tables, declare the RBU
160018 ** update finished. */
160019 if( p->rc==SQLITE_OK && p->objiter.zTbl==0 ){
160020 p->rc = SQLITE_DONE;
160021 }
160022
160023 if( p->rc==SQLITE_OK ){
160024 rbuSetupOal(p, pState);
160025 }
160026
160027 }else if( p->eStage==RBU_STAGE_MOVE ){
160028 /* no-op */
160029 }else if( p->eStage==RBU_STAGE_CKPT ){
160030 rbuSetupCheckpoint(p, pState);
160031 }else if( p->eStage==RBU_STAGE_DONE ){
160032 p->rc = SQLITE_DONE;
160033 }else{
160034 p->rc = SQLITE_CORRUPT;
160035 }
160036 }
160037
160038 rbuFreeState(pState);
160039 }
160040
160041 return p;
160042 }
160043
160044
160045 /*
160046 ** Return the database handle used by pRbu.
160047 */
160048 SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3rbu_db(sqlite3rbu *pRbu, int bRbu){
160049 sqlite3 *db = 0;
160050 if( pRbu ){
160051 db = (bRbu ? pRbu->dbRbu : pRbu->dbMain);
160052 }
160053 return db;
160054 }
160055
160056
160057 /*
160058 ** If the error code currently stored in the RBU handle is SQLITE_CONSTRAINT,
160059 ** then edit any error message string so as to remove all occurrences of
160060 ** the pattern "rbu_imp_[0-9]*".
160061 */
160062 static void rbuEditErrmsg(sqlite3rbu *p){
160063 if( p->rc==SQLITE_CONSTRAINT && p->zErrmsg ){
160064 int i;
160065 int nErrmsg = strlen(p->zErrmsg);
160066 for(i=0; i<(nErrmsg-8); i++){
160067 if( memcmp(&p->zErrmsg[i], "rbu_imp_", 8)==0 ){
160068 int nDel = 8;
160069 while( p->zErrmsg[i+nDel]>='0' && p->zErrmsg[i+nDel]<='9' ) nDel++;
160070 memmove(&p->zErrmsg[i], &p->zErrmsg[i+nDel], nErrmsg + 1 - i - nDel);
160071 nErrmsg -= nDel;
160072 }
@@ -159871,38 +160073,38 @@
160073 }
160074 }
160075 }
160076
160077 /*
160078 ** Close the RBU handle.
160079 */
160080 SQLITE_API int SQLITE_STDCALL sqlite3rbu_close(sqlite3rbu *p, char **pzErrmsg){
160081 int rc;
160082 if( p ){
160083
160084 /* Commit the transaction to the *-oal file. */
160085 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
160086 p->rc = sqlite3_exec(p->dbMain, "COMMIT", 0, 0, &p->zErrmsg);
160087 }
160088
160089 rbuSaveState(p, p->eStage);
160090
160091 if( p->rc==SQLITE_OK && p->eStage==RBU_STAGE_OAL ){
160092 p->rc = sqlite3_exec(p->dbRbu, "COMMIT", 0, 0, &p->zErrmsg);
160093 }
160094
160095 /* Close any open statement handles. */
160096 rbuObjIterFinalize(&p->objiter);
160097
160098 /* Close the open database handle and VFS object. */
160099 sqlite3_close(p->dbMain);
160100 sqlite3_close(p->dbRbu);
160101 rbuDeleteVfs(p);
160102 sqlite3_free(p->aBuf);
160103 sqlite3_free(p->aFrame);
160104
160105 rbuEditErrmsg(p);
160106 rc = p->rc;
160107 *pzErrmsg = p->zErrmsg;
160108 sqlite3_free(p);
160109 }else{
160110 rc = SQLITE_NOMEM;
@@ -159912,65 +160114,65 @@
160114 }
160115
160116 /*
160117 ** Return the total number of key-value operations (inserts, deletes or
160118 ** updates) that have been performed on the target database since the
160119 ** current RBU update was started.
160120 */
160121 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3rbu_progress(sqlite3rbu *pRbu){
160122 return pRbu->nProgress;
160123 }
160124
160125 /**************************************************************************
160126 ** Beginning of RBU VFS shim methods. The VFS shim modifies the behaviour
160127 ** of a standard VFS in the following ways:
160128 **
160129 ** 1. Whenever the first page of a main database file is read or
160130 ** written, the value of the change-counter cookie is stored in
160131 ** rbu_file.iCookie. Similarly, the value of the "write-version"
160132 ** database header field is stored in rbu_file.iWriteVer. This ensures
160133 ** that the values are always trustworthy within an open transaction.
160134 **
160135 ** 2. Whenever an SQLITE_OPEN_WAL file is opened, the (rbu_file.pWalFd)
160136 ** member variable of the associated database file descriptor is set
160137 ** to point to the new file. A mutex protected linked list of all main
160138 ** db fds opened using a particular RBU VFS is maintained at
160139 ** rbu_vfs.pMain to facilitate this.
160140 **
160141 ** 3. Using a new file-control "SQLITE_FCNTL_RBU", a main db rbu_file
160142 ** object can be marked as the target database of an RBU update. This
160143 ** turns on the following extra special behaviour:
160144 **
160145 ** 3a. If xAccess() is called to check if there exists a *-wal file
160146 ** associated with an RBU target database currently in RBU_STAGE_OAL
160147 ** stage (preparing the *-oal file), the following special handling
160148 ** applies:
160149 **
160150 ** * if the *-wal file does exist, return SQLITE_CANTOPEN. An RBU
160151 ** target database may not be in wal mode already.
160152 **
160153 ** * if the *-wal file does not exist, set the output parameter to
160154 ** non-zero (to tell SQLite that it does exist) anyway.
160155 **
160156 ** Then, when xOpen() is called to open the *-wal file associated with
160157 ** the RBU target in RBU_STAGE_OAL stage, instead of opening the *-wal
160158 ** file, the rbu vfs opens the corresponding *-oal file instead.
160159 **
160160 ** 3b. The *-shm pages returned by xShmMap() for a target db file in
160161 ** RBU_STAGE_OAL mode are actually stored in heap memory. This is to
160162 ** avoid creating a *-shm file on disk. Additionally, xShmLock() calls
160163 ** are no-ops on target database files in RBU_STAGE_OAL mode. This is
160164 ** because assert() statements in some VFS implementations fail if
160165 ** xShmLock() is called before xShmMap().
160166 **
160167 ** 3c. If an EXCLUSIVE lock is attempted on a target database file in any
160168 ** mode except RBU_STAGE_DONE (all work completed and checkpointed), it
160169 ** fails with an SQLITE_BUSY error. This is to stop RBU connections
160170 ** from automatically checkpointing a *-wal (or *-oal) file from within
160171 ** sqlite3_close().
160172 **
160173 ** 3d. In RBU_STAGE_CAPTURE mode, all xRead() calls on the wal file, and
160174 ** all xWrite() calls on the target database file perform no IO.
160175 ** Instead the frame and page numbers that would be read and written
160176 ** are recorded. Additionally, successful attempts to obtain exclusive
160177 ** xShmLock() WRITER, CHECKPOINTER and READ0 locks on the target
160178 ** database file are recorded. xShmLock() calls to unlock the same
@@ -159977,28 +160179,28 @@
160179 ** locks are no-ops (so that once obtained, these locks are never
160180 ** relinquished). Finally, calls to xSync() on the target database
160181 ** file fail with SQLITE_INTERNAL errors.
160182 */
160183
160184 static void rbuUnlockShm(rbu_file *p){
160185 if( p->pRbu ){
160186 int (*xShmLock)(sqlite3_file*,int,int,int) = p->pReal->pMethods->xShmLock;
160187 int i;
160188 for(i=0; i<SQLITE_SHM_NLOCK;i++){
160189 if( (1<<i) & p->pRbu->mLock ){
160190 xShmLock(p->pReal, i, 1, SQLITE_SHM_UNLOCK|SQLITE_SHM_EXCLUSIVE);
160191 }
160192 }
160193 p->pRbu->mLock = 0;
160194 }
160195 }
160196
160197 /*
160198 ** Close an rbu file.
160199 */
160200 static int rbuVfsClose(sqlite3_file *pFile){
160201 rbu_file *p = (rbu_file*)pFile;
160202 int rc;
160203 int i;
160204
160205 /* Free the contents of the apShm[] array. And the array itself. */
160206 for(i=0; i<p->nShm; i++){
@@ -160007,16 +160209,16 @@
160209 sqlite3_free(p->apShm);
160210 p->apShm = 0;
160211 sqlite3_free(p->zDel);
160212
160213 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160214 rbu_file **pp;
160215 sqlite3_mutex_enter(p->pRbuVfs->mutex);
160216 for(pp=&p->pRbuVfs->pMain; *pp!=p; pp=&((*pp)->pMainNext));
160217 *pp = p->pMainNext;
160218 sqlite3_mutex_leave(p->pRbuVfs->mutex);
160219 rbuUnlockShm(p);
160220 p->pReal->pMethods->xShmUnmap(p->pReal, 0);
160221 }
160222
160223 /* Close the underlying file handle */
160224 rc = p->pReal->pMethods->xClose(p->pReal);
@@ -160026,37 +160228,37 @@
160228
160229 /*
160230 ** Read and return an unsigned 32-bit big-endian integer from the buffer
160231 ** passed as the only argument.
160232 */
160233 static u32 rbuGetU32(u8 *aBuf){
160234 return ((u32)aBuf[0] << 24)
160235 + ((u32)aBuf[1] << 16)
160236 + ((u32)aBuf[2] << 8)
160237 + ((u32)aBuf[3]);
160238 }
160239
160240 /*
160241 ** Read data from an rbuVfs-file.
160242 */
160243 static int rbuVfsRead(
160244 sqlite3_file *pFile,
160245 void *zBuf,
160246 int iAmt,
160247 sqlite_int64 iOfst
160248 ){
160249 rbu_file *p = (rbu_file*)pFile;
160250 sqlite3rbu *pRbu = p->pRbu;
160251 int rc;
160252
160253 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
160254 assert( p->openFlags & SQLITE_OPEN_WAL );
160255 rc = rbuCaptureWalRead(p->pRbu, iOfst, iAmt);
160256 }else{
160257 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
160258 && (p->openFlags & SQLITE_OPEN_WAL)
160259 && iOfst>=pRbu->iOalSz
160260 ){
160261 rc = SQLITE_OK;
160262 memset(zBuf, 0, iAmt);
160263 }else{
160264 rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst);
@@ -160063,92 +160265,92 @@
160265 }
160266 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160267 /* These look like magic numbers. But they are stable, as they are part
160268 ** of the definition of the SQLite file format, which may not change. */
160269 u8 *pBuf = (u8*)zBuf;
160270 p->iCookie = rbuGetU32(&pBuf[24]);
160271 p->iWriteVer = pBuf[19];
160272 }
160273 }
160274 return rc;
160275 }
160276
160277 /*
160278 ** Write data to an rbuVfs-file.
160279 */
160280 static int rbuVfsWrite(
160281 sqlite3_file *pFile,
160282 const void *zBuf,
160283 int iAmt,
160284 sqlite_int64 iOfst
160285 ){
160286 rbu_file *p = (rbu_file*)pFile;
160287 sqlite3rbu *pRbu = p->pRbu;
160288 int rc;
160289
160290 if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
160291 assert( p->openFlags & SQLITE_OPEN_MAIN_DB );
160292 rc = rbuCaptureDbWrite(p->pRbu, iOfst);
160293 }else{
160294 if( pRbu && pRbu->eStage==RBU_STAGE_OAL
160295 && (p->openFlags & SQLITE_OPEN_WAL)
160296 && iOfst>=pRbu->iOalSz
160297 ){
160298 pRbu->iOalSz = iAmt + iOfst;
160299 }
160300 rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst);
160301 if( rc==SQLITE_OK && iOfst==0 && (p->openFlags & SQLITE_OPEN_MAIN_DB) ){
160302 /* These look like magic numbers. But they are stable, as they are part
160303 ** of the definition of the SQLite file format, which may not change. */
160304 u8 *pBuf = (u8*)zBuf;
160305 p->iCookie = rbuGetU32(&pBuf[24]);
160306 p->iWriteVer = pBuf[19];
160307 }
160308 }
160309 return rc;
160310 }
160311
160312 /*
160313 ** Truncate an rbuVfs-file.
160314 */
160315 static int rbuVfsTruncate(sqlite3_file *pFile, sqlite_int64 size){
160316 rbu_file *p = (rbu_file*)pFile;
160317 return p->pReal->pMethods->xTruncate(p->pReal, size);
160318 }
160319
160320 /*
160321 ** Sync an rbuVfs-file.
160322 */
160323 static int rbuVfsSync(sqlite3_file *pFile, int flags){
160324 rbu_file *p = (rbu_file *)pFile;
160325 if( p->pRbu && p->pRbu->eStage==RBU_STAGE_CAPTURE ){
160326 if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
160327 return SQLITE_INTERNAL;
160328 }
160329 return SQLITE_OK;
160330 }
160331 return p->pReal->pMethods->xSync(p->pReal, flags);
160332 }
160333
160334 /*
160335 ** Return the current file-size of an rbuVfs-file.
160336 */
160337 static int rbuVfsFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
160338 rbu_file *p = (rbu_file *)pFile;
160339 return p->pReal->pMethods->xFileSize(p->pReal, pSize);
160340 }
160341
160342 /*
160343 ** Lock an rbuVfs-file.
160344 */
160345 static int rbuVfsLock(sqlite3_file *pFile, int eLock){
160346 rbu_file *p = (rbu_file*)pFile;
160347 sqlite3rbu *pRbu = p->pRbu;
160348 int rc = SQLITE_OK;
160349
160350 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160351 if( pRbu && eLock==SQLITE_LOCK_EXCLUSIVE && pRbu->eStage!=RBU_STAGE_DONE ){
160352 /* Do not allow EXCLUSIVE locks. Preventing SQLite from taking this
160353 ** prevents it from checkpointing the database from sqlite3_close(). */
160354 rc = SQLITE_BUSY;
160355 }else{
160356 rc = p->pReal->pMethods->xLock(p->pReal, eLock);
@@ -160156,122 +160358,122 @@
160358
160359 return rc;
160360 }
160361
160362 /*
160363 ** Unlock an rbuVfs-file.
160364 */
160365 static int rbuVfsUnlock(sqlite3_file *pFile, int eLock){
160366 rbu_file *p = (rbu_file *)pFile;
160367 return p->pReal->pMethods->xUnlock(p->pReal, eLock);
160368 }
160369
160370 /*
160371 ** Check if another file-handle holds a RESERVED lock on an rbuVfs-file.
160372 */
160373 static int rbuVfsCheckReservedLock(sqlite3_file *pFile, int *pResOut){
160374 rbu_file *p = (rbu_file *)pFile;
160375 return p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut);
160376 }
160377
160378 /*
160379 ** File control method. For custom operations on an rbuVfs-file.
160380 */
160381 static int rbuVfsFileControl(sqlite3_file *pFile, int op, void *pArg){
160382 rbu_file *p = (rbu_file *)pFile;
160383 int (*xControl)(sqlite3_file*,int,void*) = p->pReal->pMethods->xFileControl;
160384 int rc;
160385
160386 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB)
160387 || p->openFlags & (SQLITE_OPEN_TRANSIENT_DB|SQLITE_OPEN_TEMP_JOURNAL)
160388 );
160389 if( op==SQLITE_FCNTL_RBU ){
160390 sqlite3rbu *pRbu = (sqlite3rbu*)pArg;
160391
160392 /* First try to find another RBU vfs lower down in the vfs stack. If
160393 ** one is found, this vfs will operate in pass-through mode. The lower
160394 ** level vfs will do the special RBU handling. */
160395 rc = xControl(p->pReal, op, pArg);
160396
160397 if( rc==SQLITE_NOTFOUND ){
160398 /* Now search for a zipvfs instance lower down in the VFS stack. If
160399 ** one is found, this is an error. */
160400 void *dummy = 0;
160401 rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
160402 if( rc==SQLITE_OK ){
160403 rc = SQLITE_ERROR;
160404 pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
160405 }else if( rc==SQLITE_NOTFOUND ){
160406 pRbu->pTargetFd = p;
160407 p->pRbu = pRbu;
160408 if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
160409 rc = SQLITE_OK;
160410 }
160411 }
160412 return rc;
160413 }
160414
160415 rc = xControl(p->pReal, op, pArg);
160416 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
160417 rbu_vfs *pRbuVfs = p->pRbuVfs;
160418 char *zIn = *(char**)pArg;
160419 char *zOut = sqlite3_mprintf("rbu(%s)/%z", pRbuVfs->base.zName, zIn);
160420 *(char**)pArg = zOut;
160421 if( zOut==0 ) rc = SQLITE_NOMEM;
160422 }
160423
160424 return rc;
160425 }
160426
160427 /*
160428 ** Return the sector-size in bytes for an rbuVfs-file.
160429 */
160430 static int rbuVfsSectorSize(sqlite3_file *pFile){
160431 rbu_file *p = (rbu_file *)pFile;
160432 return p->pReal->pMethods->xSectorSize(p->pReal);
160433 }
160434
160435 /*
160436 ** Return the device characteristic flags supported by an rbuVfs-file.
160437 */
160438 static int rbuVfsDeviceCharacteristics(sqlite3_file *pFile){
160439 rbu_file *p = (rbu_file *)pFile;
160440 return p->pReal->pMethods->xDeviceCharacteristics(p->pReal);
160441 }
160442
160443 /*
160444 ** Take or release a shared-memory lock.
160445 */
160446 static int rbuVfsShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
160447 rbu_file *p = (rbu_file*)pFile;
160448 sqlite3rbu *pRbu = p->pRbu;
160449 int rc = SQLITE_OK;
160450
160451 #ifdef SQLITE_AMALGAMATION
160452 assert( WAL_CKPT_LOCK==1 );
160453 #endif
160454
160455 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160456 if( pRbu && (pRbu->eStage==RBU_STAGE_OAL || pRbu->eStage==RBU_STAGE_MOVE) ){
160457 /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
160458 ** taking this lock also prevents any checkpoints from occurring.
160459 ** todo: really, it's not clear why this might occur, as
160460 ** wal_autocheckpoint ought to be turned off. */
160461 if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
160462 }else{
160463 int bCapture = 0;
160464 if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
160465 && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
160466 && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
160467 ){
160468 bCapture = 1;
160469 }
160470
160471 if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
160472 rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
160473 if( bCapture && rc==SQLITE_OK ){
160474 pRbu->mLock |= (1 << ofst);
160475 }
160476 }
160477 }
160478
160479 return rc;
@@ -160278,26 +160480,26 @@
160480 }
160481
160482 /*
160483 ** Obtain a pointer to a mapping of a single 32KiB page of the *-shm file.
160484 */
160485 static int rbuVfsShmMap(
160486 sqlite3_file *pFile,
160487 int iRegion,
160488 int szRegion,
160489 int isWrite,
160490 void volatile **pp
160491 ){
160492 rbu_file *p = (rbu_file*)pFile;
160493 int rc = SQLITE_OK;
160494 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
160495
160496 /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
160497 ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space
160498 ** instead of a file on disk. */
160499 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160500 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
160501 if( iRegion<=p->nShm ){
160502 int nByte = (iRegion+1) * sizeof(char*);
160503 char **apNew = (char**)sqlite3_realloc(p->apShm, nByte);
160504 if( apNew==0 ){
160505 rc = SQLITE_NOMEM;
@@ -160332,29 +160534,29 @@
160534 }
160535
160536 /*
160537 ** Memory barrier.
160538 */
160539 static void rbuVfsShmBarrier(sqlite3_file *pFile){
160540 rbu_file *p = (rbu_file *)pFile;
160541 p->pReal->pMethods->xShmBarrier(p->pReal);
160542 }
160543
160544 /*
160545 ** The xShmUnmap method.
160546 */
160547 static int rbuVfsShmUnmap(sqlite3_file *pFile, int delFlag){
160548 rbu_file *p = (rbu_file*)pFile;
160549 int rc = SQLITE_OK;
160550 int eStage = (p->pRbu ? p->pRbu->eStage : 0);
160551
160552 assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
160553 if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
160554 /* no-op */
160555 }else{
160556 /* Release the checkpointer and writer locks */
160557 rbuUnlockShm(p);
160558 rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag);
160559 }
160560 return rc;
160561 }
160562
@@ -160362,56 +160564,56 @@
160564 ** Given that zWal points to a buffer containing a wal file name passed to
160565 ** either the xOpen() or xAccess() VFS method, return a pointer to the
160566 ** file-handle opened by the same database connection on the corresponding
160567 ** database file.
160568 */
160569 static rbu_file *rbuFindMaindb(rbu_vfs *pRbuVfs, const char *zWal){
160570 rbu_file *pDb;
160571 sqlite3_mutex_enter(pRbuVfs->mutex);
160572 for(pDb=pRbuVfs->pMain; pDb && pDb->zWal!=zWal; pDb=pDb->pMainNext);
160573 sqlite3_mutex_leave(pRbuVfs->mutex);
160574 return pDb;
160575 }
160576
160577 /*
160578 ** Open an rbu file handle.
160579 */
160580 static int rbuVfsOpen(
160581 sqlite3_vfs *pVfs,
160582 const char *zName,
160583 sqlite3_file *pFile,
160584 int flags,
160585 int *pOutFlags
160586 ){
160587 static sqlite3_io_methods rbuvfs_io_methods = {
160588 2, /* iVersion */
160589 rbuVfsClose, /* xClose */
160590 rbuVfsRead, /* xRead */
160591 rbuVfsWrite, /* xWrite */
160592 rbuVfsTruncate, /* xTruncate */
160593 rbuVfsSync, /* xSync */
160594 rbuVfsFileSize, /* xFileSize */
160595 rbuVfsLock, /* xLock */
160596 rbuVfsUnlock, /* xUnlock */
160597 rbuVfsCheckReservedLock, /* xCheckReservedLock */
160598 rbuVfsFileControl, /* xFileControl */
160599 rbuVfsSectorSize, /* xSectorSize */
160600 rbuVfsDeviceCharacteristics, /* xDeviceCharacteristics */
160601 rbuVfsShmMap, /* xShmMap */
160602 rbuVfsShmLock, /* xShmLock */
160603 rbuVfsShmBarrier, /* xShmBarrier */
160604 rbuVfsShmUnmap /* xShmUnmap */
160605 };
160606 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
160607 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
160608 rbu_file *pFd = (rbu_file *)pFile;
160609 int rc = SQLITE_OK;
160610 const char *zOpen = zName;
160611
160612 memset(pFd, 0, sizeof(rbu_file));
160613 pFd->pReal = (sqlite3_file*)&pFd[1];
160614 pFd->pRbuVfs = pRbuVfs;
160615 pFd->openFlags = flags;
160616 if( zName ){
160617 if( flags & SQLITE_OPEN_MAIN_DB ){
160618 /* A main database has just been opened. The following block sets
160619 ** (pFd->zWal) to point to a buffer owned by SQLite that contains
@@ -160435,13 +160637,13 @@
160637 }
160638 z += (n + 8 + 1);
160639 pFd->zWal = z;
160640 }
160641 else if( flags & SQLITE_OPEN_WAL ){
160642 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zName);
160643 if( pDb ){
160644 if( pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
160645 /* This call is to open a *-wal file. Intead, open the *-oal. This
160646 ** code ensures that the string passed to xOpen() is terminated by a
160647 ** pair of '\0' bytes in case the VFS attempts to extract a URI
160648 ** parameter from it. */
160649 int nCopy = strlen(zName);
@@ -160453,11 +160655,11 @@
160655 zCopy[nCopy+1] = '\0';
160656 zOpen = (const char*)(pFd->zDel = zCopy);
160657 }else{
160658 rc = SQLITE_NOMEM;
160659 }
160660 pFd->pRbu = pDb->pRbu;
160661 }
160662 pDb->pWalFd = pFd;
160663 }
160664 }
160665 }
@@ -160467,16 +160669,16 @@
160669 }
160670 if( pFd->pReal->pMethods ){
160671 /* The xOpen() operation has succeeded. Set the sqlite3_file.pMethods
160672 ** pointer and, if the file is a main database file, link it into the
160673 ** mutex protected linked list of all such files. */
160674 pFile->pMethods = &rbuvfs_io_methods;
160675 if( flags & SQLITE_OPEN_MAIN_DB ){
160676 sqlite3_mutex_enter(pRbuVfs->mutex);
160677 pFd->pMainNext = pRbuVfs->pMain;
160678 pRbuVfs->pMain = pFd;
160679 sqlite3_mutex_leave(pRbuVfs->mutex);
160680 }
160681 }else{
160682 sqlite3_free(pFd->zDel);
160683 }
160684
@@ -160484,48 +160686,48 @@
160686 }
160687
160688 /*
160689 ** Delete the file located at zPath.
160690 */
160691 static int rbuVfsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
160692 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160693 return pRealVfs->xDelete(pRealVfs, zPath, dirSync);
160694 }
160695
160696 /*
160697 ** Test for access permissions. Return true if the requested permission
160698 ** is available, or false otherwise.
160699 */
160700 static int rbuVfsAccess(
160701 sqlite3_vfs *pVfs,
160702 const char *zPath,
160703 int flags,
160704 int *pResOut
160705 ){
160706 rbu_vfs *pRbuVfs = (rbu_vfs*)pVfs;
160707 sqlite3_vfs *pRealVfs = pRbuVfs->pRealVfs;
160708 int rc;
160709
160710 rc = pRealVfs->xAccess(pRealVfs, zPath, flags, pResOut);
160711
160712 /* If this call is to check if a *-wal file associated with an RBU target
160713 ** database connection exists, and the RBU update is in RBU_STAGE_OAL,
160714 ** the following special handling is activated:
160715 **
160716 ** a) if the *-wal file does exist, return SQLITE_CANTOPEN. This
160717 ** ensures that the RBU extension never tries to update a database
160718 ** in wal mode, even if the first page of the database file has
160719 ** been damaged.
160720 **
160721 ** b) if the *-wal file does not exist, claim that it does anyway,
160722 ** causing SQLite to call xOpen() to open it. This call will also
160723 ** be intercepted (see the rbuVfsOpen() function) and the *-oal
160724 ** file opened instead.
160725 */
160726 if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
160727 rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath);
160728 if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
160729 if( *pResOut ){
160730 rc = SQLITE_CANTOPEN;
160731 }else{
160732 *pResOut = 1;
160733 }
@@ -160538,151 +160740,151 @@
160740 /*
160741 ** Populate buffer zOut with the full canonical pathname corresponding
160742 ** to the pathname in zPath. zOut is guaranteed to point to a buffer
160743 ** of at least (DEVSYM_MAX_PATHNAME+1) bytes.
160744 */
160745 static int rbuVfsFullPathname(
160746 sqlite3_vfs *pVfs,
160747 const char *zPath,
160748 int nOut,
160749 char *zOut
160750 ){
160751 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160752 return pRealVfs->xFullPathname(pRealVfs, zPath, nOut, zOut);
160753 }
160754
160755 #ifndef SQLITE_OMIT_LOAD_EXTENSION
160756 /*
160757 ** Open the dynamic library located at zPath and return a handle.
160758 */
160759 static void *rbuVfsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
160760 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160761 return pRealVfs->xDlOpen(pRealVfs, zPath);
160762 }
160763
160764 /*
160765 ** Populate the buffer zErrMsg (size nByte bytes) with a human readable
160766 ** utf-8 string describing the most recent error encountered associated
160767 ** with dynamic libraries.
160768 */
160769 static void rbuVfsDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
160770 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160771 pRealVfs->xDlError(pRealVfs, nByte, zErrMsg);
160772 }
160773
160774 /*
160775 ** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
160776 */
160777 static void (*rbuVfsDlSym(
160778 sqlite3_vfs *pVfs,
160779 void *pArg,
160780 const char *zSym
160781 ))(void){
160782 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160783 return pRealVfs->xDlSym(pRealVfs, pArg, zSym);
160784 }
160785
160786 /*
160787 ** Close the dynamic library handle pHandle.
160788 */
160789 static void rbuVfsDlClose(sqlite3_vfs *pVfs, void *pHandle){
160790 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160791 return pRealVfs->xDlClose(pRealVfs, pHandle);
160792 }
160793 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
160794
160795 /*
160796 ** Populate the buffer pointed to by zBufOut with nByte bytes of
160797 ** random data.
160798 */
160799 static int rbuVfsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
160800 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160801 return pRealVfs->xRandomness(pRealVfs, nByte, zBufOut);
160802 }
160803
160804 /*
160805 ** Sleep for nMicro microseconds. Return the number of microseconds
160806 ** actually slept.
160807 */
160808 static int rbuVfsSleep(sqlite3_vfs *pVfs, int nMicro){
160809 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160810 return pRealVfs->xSleep(pRealVfs, nMicro);
160811 }
160812
160813 /*
160814 ** Return the current time as a Julian Day number in *pTimeOut.
160815 */
160816 static int rbuVfsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
160817 sqlite3_vfs *pRealVfs = ((rbu_vfs*)pVfs)->pRealVfs;
160818 return pRealVfs->xCurrentTime(pRealVfs, pTimeOut);
160819 }
160820
160821 /*
160822 ** No-op.
160823 */
160824 static int rbuVfsGetLastError(sqlite3_vfs *pVfs, int a, char *b){
160825 return 0;
160826 }
160827
160828 /*
160829 ** Deregister and destroy an RBU vfs created by an earlier call to
160830 ** sqlite3rbu_create_vfs().
160831 */
160832 SQLITE_API void SQLITE_STDCALL sqlite3rbu_destroy_vfs(const char *zName){
160833 sqlite3_vfs *pVfs = sqlite3_vfs_find(zName);
160834 if( pVfs && pVfs->xOpen==rbuVfsOpen ){
160835 sqlite3_mutex_free(((rbu_vfs*)pVfs)->mutex);
160836 sqlite3_vfs_unregister(pVfs);
160837 sqlite3_free(pVfs);
160838 }
160839 }
160840
160841 /*
160842 ** Create an RBU VFS named zName that accesses the underlying file-system
160843 ** via existing VFS zParent. The new object is registered as a non-default
160844 ** VFS with SQLite before returning.
160845 */
160846 SQLITE_API int SQLITE_STDCALL sqlite3rbu_create_vfs(const char *zName, const char *zParent){
160847
160848 /* Template for VFS */
160849 static sqlite3_vfs vfs_template = {
160850 1, /* iVersion */
160851 0, /* szOsFile */
160852 0, /* mxPathname */
160853 0, /* pNext */
160854 0, /* zName */
160855 0, /* pAppData */
160856 rbuVfsOpen, /* xOpen */
160857 rbuVfsDelete, /* xDelete */
160858 rbuVfsAccess, /* xAccess */
160859 rbuVfsFullPathname, /* xFullPathname */
160860
160861 #ifndef SQLITE_OMIT_LOAD_EXTENSION
160862 rbuVfsDlOpen, /* xDlOpen */
160863 rbuVfsDlError, /* xDlError */
160864 rbuVfsDlSym, /* xDlSym */
160865 rbuVfsDlClose, /* xDlClose */
160866 #else
160867 0, 0, 0, 0,
160868 #endif
160869
160870 rbuVfsRandomness, /* xRandomness */
160871 rbuVfsSleep, /* xSleep */
160872 rbuVfsCurrentTime, /* xCurrentTime */
160873 rbuVfsGetLastError, /* xGetLastError */
160874 0, /* xCurrentTimeInt64 (version 2) */
160875 0, 0, 0 /* Unimplemented version 3 methods */
160876 };
160877
160878 rbu_vfs *pNew = 0; /* Newly allocated VFS */
160879 int nName;
160880 int rc = SQLITE_OK;
160881
160882 int nByte;
160883 nName = strlen(zName);
160884 nByte = sizeof(rbu_vfs) + nName + 1;
160885 pNew = (rbu_vfs*)sqlite3_malloc(nByte);
160886 if( pNew==0 ){
160887 rc = SQLITE_NOMEM;
160888 }else{
160889 sqlite3_vfs *pParent; /* Parent VFS */
160890 memset(pNew, 0, nByte);
@@ -160691,11 +160893,11 @@
160893 rc = SQLITE_NOTFOUND;
160894 }else{
160895 char *zSpace;
160896 memcpy(&pNew->base, &vfs_template, sizeof(sqlite3_vfs));
160897 pNew->base.mxPathname = pParent->mxPathname;
160898 pNew->base.szOsFile = sizeof(rbu_file) + pParent->szOsFile;
160899 pNew->pRealVfs = pParent;
160900 pNew->base.zName = (const char*)(zSpace = (char*)&pNew[1]);
160901 memcpy(zSpace, zName, nName);
160902
160903 /* Allocate the mutex and register the new VFS (not as the default) */
@@ -160717,13 +160919,13 @@
160919 }
160920
160921
160922 /**************************************************************************/
160923
160924 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RBU) */
160925
160926 /************** End of sqlite3rbu.c ******************************************/
160927 /************** Begin file dbstat.c ******************************************/
160928 /*
160929 ** 2010 July 12
160930 **
160931 ** The author disclaims copyright to this source code. In place of
@@ -160741,10 +160943,11 @@
160943 ** information from an SQLite database in order to implement the
160944 ** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
160945 ** for an example implementation.
160946 */
160947
160948 /* #include "sqliteInt.h" ** Requires access to internal data structures ** */
160949 #if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
160950 && !defined(SQLITE_OMIT_VIRTUALTABLE)
160951
160952 /*
160953 ** Page paths:
160954
+5 -5
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111111
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112112
** [sqlite_version()] and [sqlite_source_id()].
113113
*/
114114
#define SQLITE_VERSION "3.8.11"
115115
#define SQLITE_VERSION_NUMBER 3008011
116
-#define SQLITE_SOURCE_ID "2015-07-08 16:22:42 5348ffc3fda5168c1e9e14aa88b0c6aedbda7c94"
116
+#define SQLITE_SOURCE_ID "2015-07-23 20:44:49 017c5019e1ce042025d4f327e50ec50af49f9fa4"
117117
118118
/*
119119
** CAPI3REF: Run-Time Library Version Numbers
120120
** KEYWORDS: sqlite3_version, sqlite3_sourceid
121121
**
@@ -965,13 +965,13 @@
965965
**
966966
** <li>[[SQLITE_FCNTL_ZIPVFS]]
967967
** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
968968
** VFS should return SQLITE_NOTFOUND for this opcode.
969969
**
970
-** <li>[[SQLITE_FCNTL_OTA]]
971
-** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
972
-** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
970
+** <li>[[SQLITE_FCNTL_RBU]]
971
+** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
972
+** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
973973
** this opcode.
974974
** </ul>
975975
*/
976976
#define SQLITE_FCNTL_LOCKSTATE 1
977977
#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -995,11 +995,11 @@
995995
#define SQLITE_FCNTL_SYNC 21
996996
#define SQLITE_FCNTL_COMMIT_PHASETWO 22
997997
#define SQLITE_FCNTL_WIN32_SET_HANDLE 23
998998
#define SQLITE_FCNTL_WAL_BLOCK 24
999999
#define SQLITE_FCNTL_ZIPVFS 25
1000
-#define SQLITE_FCNTL_OTA 26
1000
+#define SQLITE_FCNTL_RBU 26
10011001
10021002
/* deprecated names */
10031003
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
10041004
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
10051005
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
10061006
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.8.11"
115 #define SQLITE_VERSION_NUMBER 3008011
116 #define SQLITE_SOURCE_ID "2015-07-08 16:22:42 5348ffc3fda5168c1e9e14aa88b0c6aedbda7c94"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
@@ -965,13 +965,13 @@
965 **
966 ** <li>[[SQLITE_FCNTL_ZIPVFS]]
967 ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
968 ** VFS should return SQLITE_NOTFOUND for this opcode.
969 **
970 ** <li>[[SQLITE_FCNTL_OTA]]
971 ** The [SQLITE_FCNTL_OTA] opcode is implemented by the special VFS used by
972 ** the OTA extension only. All other VFS should return SQLITE_NOTFOUND for
973 ** this opcode.
974 ** </ul>
975 */
976 #define SQLITE_FCNTL_LOCKSTATE 1
977 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -995,11 +995,11 @@
995 #define SQLITE_FCNTL_SYNC 21
996 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
997 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
998 #define SQLITE_FCNTL_WAL_BLOCK 24
999 #define SQLITE_FCNTL_ZIPVFS 25
1000 #define SQLITE_FCNTL_OTA 26
1001
1002 /* deprecated names */
1003 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1004 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1005 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
1006
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.8.11"
115 #define SQLITE_VERSION_NUMBER 3008011
116 #define SQLITE_SOURCE_ID "2015-07-23 20:44:49 017c5019e1ce042025d4f327e50ec50af49f9fa4"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
@@ -965,13 +965,13 @@
965 **
966 ** <li>[[SQLITE_FCNTL_ZIPVFS]]
967 ** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
968 ** VFS should return SQLITE_NOTFOUND for this opcode.
969 **
970 ** <li>[[SQLITE_FCNTL_RBU]]
971 ** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
972 ** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for
973 ** this opcode.
974 ** </ul>
975 */
976 #define SQLITE_FCNTL_LOCKSTATE 1
977 #define SQLITE_FCNTL_GET_LOCKPROXYFILE 2
@@ -995,11 +995,11 @@
995 #define SQLITE_FCNTL_SYNC 21
996 #define SQLITE_FCNTL_COMMIT_PHASETWO 22
997 #define SQLITE_FCNTL_WIN32_SET_HANDLE 23
998 #define SQLITE_FCNTL_WAL_BLOCK 24
999 #define SQLITE_FCNTL_ZIPVFS 25
1000 #define SQLITE_FCNTL_RBU 26
1001
1002 /* deprecated names */
1003 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1004 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1005 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
1006
--- src/th_main.c
+++ src/th_main.c
@@ -337,10 +337,40 @@
337337
return Th_WrongNumArgs(interp, "puts STRING");
338338
}
339339
sendText((char*)argv[1], argl[1], *(unsigned int*)pConvert);
340340
return TH_OK;
341341
}
342
+
343
+/*
344
+** TH1 command: markdown STRING
345
+**
346
+** Renders the input string as markdown. The result is a two-element list.
347
+** The first element is the text-only title string. The second element
348
+** contains the body, rendered as HTML.
349
+*/
350
+static int markdownCmd(
351
+ Th_Interp *interp,
352
+ void *p,
353
+ int argc,
354
+ const char **argv,
355
+ int *argl
356
+){
357
+ Blob src, title, body;
358
+ char *zValue = 0;
359
+ int nValue = 0;
360
+ if( argc!=2 ){
361
+ return Th_WrongNumArgs(interp, "markdown STRING");
362
+ }
363
+ blob_zero(&src);
364
+ blob_init(&src, (char*)argv[1], argl[1]);
365
+ blob_zero(&title); blob_zero(&body);
366
+ markdown_to_html(&src, &title, &body);
367
+ Th_ListAppend(interp, &zValue, &nValue, blob_str(&title), blob_size(&title));
368
+ Th_ListAppend(interp, &zValue, &nValue, blob_str(&body), blob_size(&body));
369
+ Th_SetResult(interp, zValue, nValue);
370
+ return TH_OK;
371
+}
342372
343373
/*
344374
** TH1 command: decorate STRING
345375
** TH1 command: wiki STRING
346376
**
@@ -1613,10 +1643,11 @@
16131643
{"hasfeature", hasfeatureCmd, 0},
16141644
{"html", putsCmd, (void*)&aFlags[0]},
16151645
{"htmlize", htmlizeCmd, 0},
16161646
{"http", httpCmd, 0},
16171647
{"linecount", linecntCmd, 0},
1648
+ {"markdown", markdownCmd, 0},
16181649
{"puts", putsCmd, (void*)&aFlags[1]},
16191650
{"query", queryCmd, 0},
16201651
{"randhex", randhexCmd, 0},
16211652
{"regexp", regexpCmd, 0},
16221653
{"reinitialize", reinitializeCmd, 0},
16231654
--- src/th_main.c
+++ src/th_main.c
@@ -337,10 +337,40 @@
337 return Th_WrongNumArgs(interp, "puts STRING");
338 }
339 sendText((char*)argv[1], argl[1], *(unsigned int*)pConvert);
340 return TH_OK;
341 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
343 /*
344 ** TH1 command: decorate STRING
345 ** TH1 command: wiki STRING
346 **
@@ -1613,10 +1643,11 @@
1613 {"hasfeature", hasfeatureCmd, 0},
1614 {"html", putsCmd, (void*)&aFlags[0]},
1615 {"htmlize", htmlizeCmd, 0},
1616 {"http", httpCmd, 0},
1617 {"linecount", linecntCmd, 0},
 
1618 {"puts", putsCmd, (void*)&aFlags[1]},
1619 {"query", queryCmd, 0},
1620 {"randhex", randhexCmd, 0},
1621 {"regexp", regexpCmd, 0},
1622 {"reinitialize", reinitializeCmd, 0},
1623
--- src/th_main.c
+++ src/th_main.c
@@ -337,10 +337,40 @@
337 return Th_WrongNumArgs(interp, "puts STRING");
338 }
339 sendText((char*)argv[1], argl[1], *(unsigned int*)pConvert);
340 return TH_OK;
341 }
342
343 /*
344 ** TH1 command: markdown STRING
345 **
346 ** Renders the input string as markdown. The result is a two-element list.
347 ** The first element is the text-only title string. The second element
348 ** contains the body, rendered as HTML.
349 */
350 static int markdownCmd(
351 Th_Interp *interp,
352 void *p,
353 int argc,
354 const char **argv,
355 int *argl
356 ){
357 Blob src, title, body;
358 char *zValue = 0;
359 int nValue = 0;
360 if( argc!=2 ){
361 return Th_WrongNumArgs(interp, "markdown STRING");
362 }
363 blob_zero(&src);
364 blob_init(&src, (char*)argv[1], argl[1]);
365 blob_zero(&title); blob_zero(&body);
366 markdown_to_html(&src, &title, &body);
367 Th_ListAppend(interp, &zValue, &nValue, blob_str(&title), blob_size(&title));
368 Th_ListAppend(interp, &zValue, &nValue, blob_str(&body), blob_size(&body));
369 Th_SetResult(interp, zValue, nValue);
370 return TH_OK;
371 }
372
373 /*
374 ** TH1 command: decorate STRING
375 ** TH1 command: wiki STRING
376 **
@@ -1613,10 +1643,11 @@
1643 {"hasfeature", hasfeatureCmd, 0},
1644 {"html", putsCmd, (void*)&aFlags[0]},
1645 {"htmlize", htmlizeCmd, 0},
1646 {"http", httpCmd, 0},
1647 {"linecount", linecntCmd, 0},
1648 {"markdown", markdownCmd, 0},
1649 {"puts", putsCmd, (void*)&aFlags[1]},
1650 {"query", queryCmd, 0},
1651 {"randhex", randhexCmd, 0},
1652 {"regexp", regexpCmd, 0},
1653 {"reinitialize", reinitializeCmd, 0},
1654
+76 -2
--- test/th1.test
+++ test/th1.test
@@ -16,10 +16,14 @@
1616
############################################################################
1717
#
1818
# TH1 Commands
1919
#
2020
21
+set dir [file dirname [info script]]
22
+
23
+###############################################################################
24
+
2125
fossil test-th-eval --open-config "setting th1-hooks"
2226
set th1Hooks [expr {$RESULT eq "1"}]
2327
2428
###############################################################################
2529
@@ -859,12 +863,12 @@
859863
#
860864
fossil test-th-eval "info commands"
861865
test th1-info-commands-1 {$RESULT eq {linecount htmlize date stime\
862866
enable_output uplevel http expr glob_match utime styleFooter catch if\
863867
tclReady searchable reinitialize combobox lindex query html anoncap randhex\
864
-llength for set break regexp styleHeader puts return checkout decorate\
865
-artifact trace wiki proc hascap globalState continue getParameter\
868
+llength for set break regexp markdown styleHeader puts return checkout\
869
+decorate artifact trace wiki proc hascap globalState continue getParameter\
866870
hasfeature setting lsearch breakpoint upvar render repository string unset\
867871
setParameter list error info rename anycap httpize}}
868872
869873
###############################################################################
870874
@@ -1171,5 +1175,75 @@
11711175
11721176
###############################################################################
11731177
11741178
fossil test-th-eval {string is integer 0xC0DEF00Z}
11751179
test th1-string-is-31 {$RESULT eq "0"}
1180
+
1181
+###############################################################################
1182
+
1183
+fossil test-th-eval {markdown}
1184
+test th1-markdown-1 {$RESULT eq \
1185
+{TH_ERROR: wrong # args: should be "markdown STRING"}}
1186
+
1187
+###############################################################################
1188
+
1189
+fossil test-th-eval {markdown one two}
1190
+test th1-markdown-2 {$RESULT eq \
1191
+{TH_ERROR: wrong # args: should be "markdown STRING"}}
1192
+
1193
+###############################################################################
1194
+
1195
+fossil test-th-eval {markdown "*This is a test.*"}
1196
+test th1-markdown-3 {[normalize_result] eq {{} {<div class="markdown">
1197
+
1198
+<p><em>This is a test.</em></p>
1199
+
1200
+</div>
1201
+}}}
1202
+
1203
+###############################################################################
1204
+
1205
+fossil test-th-eval {markdown "Test1\n=====\n*This is a test.*"}
1206
+test th1-markdown-4 {[normalize_result] eq {Test1 {<div class="markdown">
1207
+
1208
+<h1>Test1</h1>
1209
+<p><em>This is a test.</em></p>
1210
+
1211
+</div>
1212
+}}}
1213
+
1214
+###############################################################################
1215
+
1216
+set markdown [read_file [file join $dir markdown-test1.md]]
1217
+fossil test-th-eval [string map \
1218
+ [list %markdown% $markdown] {markdown {%markdown%}}]
1219
+test th1-markdown-5 {[normalize_result] eq \
1220
+{{Markdown Formatter Test Document} {<div class="markdown">
1221
+
1222
+<h1>Markdown Formatter Test Document</h1>
1223
+<p>This document is designed to test the markdown formatter.</p>
1224
+
1225
+<ul>
1226
+<li>A bullet item.
1227
+
1228
+<ul>
1229
+<li>A subitem</li>
1230
+</ul></li>
1231
+<li>Second bullet</li>
1232
+</ul>
1233
+
1234
+<p>More text</p>
1235
+
1236
+<ol>
1237
+<li>Enumeration
1238
+1.1. Subitem 1
1239
+1.2. Subitem 2</li>
1240
+<li>Second enumeration.</li>
1241
+</ol>
1242
+
1243
+<p>Another paragraph.</p>
1244
+
1245
+<h2>Other Features</h2>
1246
+<p>Text can show <em>emphasis</em> or <em>emphasis</em> or <strong>strong emphassis</strong>.</p>
1247
+
1248
+</div>
1249
+}}}
11761250
--- test/th1.test
+++ test/th1.test
@@ -16,10 +16,14 @@
16 ############################################################################
17 #
18 # TH1 Commands
19 #
20
 
 
 
 
21 fossil test-th-eval --open-config "setting th1-hooks"
22 set th1Hooks [expr {$RESULT eq "1"}]
23
24 ###############################################################################
25
@@ -859,12 +863,12 @@
859 #
860 fossil test-th-eval "info commands"
861 test th1-info-commands-1 {$RESULT eq {linecount htmlize date stime\
862 enable_output uplevel http expr glob_match utime styleFooter catch if\
863 tclReady searchable reinitialize combobox lindex query html anoncap randhex\
864 llength for set break regexp styleHeader puts return checkout decorate\
865 artifact trace wiki proc hascap globalState continue getParameter\
866 hasfeature setting lsearch breakpoint upvar render repository string unset\
867 setParameter list error info rename anycap httpize}}
868
869 ###############################################################################
870
@@ -1171,5 +1175,75 @@
1171
1172 ###############################################################################
1173
1174 fossil test-th-eval {string is integer 0xC0DEF00Z}
1175 test th1-string-is-31 {$RESULT eq "0"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1176
--- test/th1.test
+++ test/th1.test
@@ -16,10 +16,14 @@
16 ############################################################################
17 #
18 # TH1 Commands
19 #
20
21 set dir [file dirname [info script]]
22
23 ###############################################################################
24
25 fossil test-th-eval --open-config "setting th1-hooks"
26 set th1Hooks [expr {$RESULT eq "1"}]
27
28 ###############################################################################
29
@@ -859,12 +863,12 @@
863 #
864 fossil test-th-eval "info commands"
865 test th1-info-commands-1 {$RESULT eq {linecount htmlize date stime\
866 enable_output uplevel http expr glob_match utime styleFooter catch if\
867 tclReady searchable reinitialize combobox lindex query html anoncap randhex\
868 llength for set break regexp markdown styleHeader puts return checkout\
869 decorate artifact trace wiki proc hascap globalState continue getParameter\
870 hasfeature setting lsearch breakpoint upvar render repository string unset\
871 setParameter list error info rename anycap httpize}}
872
873 ###############################################################################
874
@@ -1171,5 +1175,75 @@
1175
1176 ###############################################################################
1177
1178 fossil test-th-eval {string is integer 0xC0DEF00Z}
1179 test th1-string-is-31 {$RESULT eq "0"}
1180
1181 ###############################################################################
1182
1183 fossil test-th-eval {markdown}
1184 test th1-markdown-1 {$RESULT eq \
1185 {TH_ERROR: wrong # args: should be "markdown STRING"}}
1186
1187 ###############################################################################
1188
1189 fossil test-th-eval {markdown one two}
1190 test th1-markdown-2 {$RESULT eq \
1191 {TH_ERROR: wrong # args: should be "markdown STRING"}}
1192
1193 ###############################################################################
1194
1195 fossil test-th-eval {markdown "*This is a test.*"}
1196 test th1-markdown-3 {[normalize_result] eq {{} {<div class="markdown">
1197
1198 <p><em>This is a test.</em></p>
1199
1200 </div>
1201 }}}
1202
1203 ###############################################################################
1204
1205 fossil test-th-eval {markdown "Test1\n=====\n*This is a test.*"}
1206 test th1-markdown-4 {[normalize_result] eq {Test1 {<div class="markdown">
1207
1208 <h1>Test1</h1>
1209 <p><em>This is a test.</em></p>
1210
1211 </div>
1212 }}}
1213
1214 ###############################################################################
1215
1216 set markdown [read_file [file join $dir markdown-test1.md]]
1217 fossil test-th-eval [string map \
1218 [list %markdown% $markdown] {markdown {%markdown%}}]
1219 test th1-markdown-5 {[normalize_result] eq \
1220 {{Markdown Formatter Test Document} {<div class="markdown">
1221
1222 <h1>Markdown Formatter Test Document</h1>
1223 <p>This document is designed to test the markdown formatter.</p>
1224
1225 <ul>
1226 <li>A bullet item.
1227
1228 <ul>
1229 <li>A subitem</li>
1230 </ul></li>
1231 <li>Second bullet</li>
1232 </ul>
1233
1234 <p>More text</p>
1235
1236 <ol>
1237 <li>Enumeration
1238 1.1. Subitem 1
1239 1.2. Subitem 2</li>
1240 <li>Second enumeration.</li>
1241 </ol>
1242
1243 <p>Another paragraph.</p>
1244
1245 <h2>Other Features</h2>
1246 <p>Text can show <em>emphasis</em> or <em>emphasis</em> or <strong>strong emphassis</strong>.</p>
1247
1248 </div>
1249 }}}
1250
--- www/changes.wiki
+++ www/changes.wiki
@@ -1,14 +1,17 @@
11
<title>Change Log</title>
22
33
<h2>Changes for Version 1.34 (2015-??-??)</h2>
4
- * Fix --hard option to mv/rm to enable them to work properly with certain
5
- relative paths.
6
- * Make the clean command undoable for files less than 10MiB.
4
+
5
+ * Fix --hard option to [/help?cmd=mv|fossil mv] and [/help?cmd=rm|fossil rm]
6
+ to enable them to work properly with certain relative paths.
7
+ * Make the [/help?cmd=clean|fossil clean] command undoable for files less
8
+ than 10MiB.
79
* Add minimal <nowiki>[lsearch]</nowiki> command to TH1. Only exact
810
case-sensitive matching is supported.
9
- * Add the <nowiki>[glob_match]</nowiki> command to TH1.
11
+ * Add the <nowiki>[glob_match]</nowiki> and <nowiki>[markdown]</nowiki>
12
+ commands to TH1.
1013
* Add the <nowiki>[tclIsSafe] and [tclMakeSafe]</nowiki> TH1 commands to
1114
the Tcl integration subsystem.
1215
* Add 'double', 'integer', and 'list' classes to the
1316
<nowiki>[string is]</nowiki> command in TH1.
1417
* Update internal Unicode character tables, used in regular expression
1518
--- www/changes.wiki
+++ www/changes.wiki
@@ -1,14 +1,17 @@
1 <title>Change Log</title>
2
3 <h2>Changes for Version 1.34 (2015-??-??)</h2>
4 * Fix --hard option to mv/rm to enable them to work properly with certain
5 relative paths.
6 * Make the clean command undoable for files less than 10MiB.
 
 
7 * Add minimal <nowiki>[lsearch]</nowiki> command to TH1. Only exact
8 case-sensitive matching is supported.
9 * Add the <nowiki>[glob_match]</nowiki> command to TH1.
 
10 * Add the <nowiki>[tclIsSafe] and [tclMakeSafe]</nowiki> TH1 commands to
11 the Tcl integration subsystem.
12 * Add 'double', 'integer', and 'list' classes to the
13 <nowiki>[string is]</nowiki> command in TH1.
14 * Update internal Unicode character tables, used in regular expression
15
--- www/changes.wiki
+++ www/changes.wiki
@@ -1,14 +1,17 @@
1 <title>Change Log</title>
2
3 <h2>Changes for Version 1.34 (2015-??-??)</h2>
4
5 * Fix --hard option to [/help?cmd=mv|fossil mv] and [/help?cmd=rm|fossil rm]
6 to enable them to work properly with certain relative paths.
7 * Make the [/help?cmd=clean|fossil clean] command undoable for files less
8 than 10MiB.
9 * Add minimal <nowiki>[lsearch]</nowiki> command to TH1. Only exact
10 case-sensitive matching is supported.
11 * Add the <nowiki>[glob_match]</nowiki> and <nowiki>[markdown]</nowiki>
12 commands to TH1.
13 * Add the <nowiki>[tclIsSafe] and [tclMakeSafe]</nowiki> TH1 commands to
14 the Tcl integration subsystem.
15 * Add 'double', 'integer', and 'list' classes to the
16 <nowiki>[string is]</nowiki> command in TH1.
17 * Update internal Unicode character tables, used in regular expression
18
--- www/settings.wiki
+++ www/settings.wiki
@@ -42,12 +42,13 @@
4242
with the <tt>extras</tt> command.
4343
4444
Because these options can change over time, and the inconvenience of
4545
replicating changes, these settings are "versionable". As well as being
4646
able to be set using the <tt>settings</tt> command or the web interface,
47
-you can created versioned files in the <tt>.fossil-settings</tt>
48
-directory named with the setting name. The contents of the file is the
47
+you can create versioned files in the <tt>.fossil-settings</tt>
48
+subdirectory of the check-out root, named with the setting name.
49
+The contents of the file is the
4950
value of the setting, and these files are checked in, committed, merged,
5051
and so on, as with any other file.
5152
5253
Where a setting is a list of values, such as <tt>ignore-glob</tt>, you
5354
can use a newline as a separator as well as a comma.
5455
--- www/settings.wiki
+++ www/settings.wiki
@@ -42,12 +42,13 @@
42 with the <tt>extras</tt> command.
43
44 Because these options can change over time, and the inconvenience of
45 replicating changes, these settings are "versionable". As well as being
46 able to be set using the <tt>settings</tt> command or the web interface,
47 you can created versioned files in the <tt>.fossil-settings</tt>
48 directory named with the setting name. The contents of the file is the
 
49 value of the setting, and these files are checked in, committed, merged,
50 and so on, as with any other file.
51
52 Where a setting is a list of values, such as <tt>ignore-glob</tt>, you
53 can use a newline as a separator as well as a comma.
54
--- www/settings.wiki
+++ www/settings.wiki
@@ -42,12 +42,13 @@
42 with the <tt>extras</tt> command.
43
44 Because these options can change over time, and the inconvenience of
45 replicating changes, these settings are "versionable". As well as being
46 able to be set using the <tt>settings</tt> command or the web interface,
47 you can create versioned files in the <tt>.fossil-settings</tt>
48 subdirectory of the check-out root, named with the setting name.
49 The contents of the file is the
50 value of the setting, and these files are checked in, committed, merged,
51 and so on, as with any other file.
52
53 Where a setting is a list of values, such as <tt>ignore-glob</tt>, you
54 can use a newline as a separator as well as a comma.
55
+10
--- www/th1.md
+++ www/th1.md
@@ -139,10 +139,11 @@
139139
* html
140140
* htmlize
141141
* http
142142
* httpize
143143
* linecount
144
+ * markdown
144145
* puts
145146
* query
146147
* randhex
147148
* regexp
148149
* reinitialize
@@ -350,10 +351,19 @@
350351
351352
* linecount STRING MAX MIN
352353
353354
Returns one more than the number of \n characters in STRING. But
354355
never returns less than MIN or more than MAX.
356
+
357
+<a name="markdown"></a>TH1 markdown Command
358
+---------------------------------------------
359
+
360
+ * markdown STRING
361
+
362
+Renders the input string as markdown. The result is a two-element list.
363
+The first element contains the body, rendered as HTML. The second element
364
+is the text-only title string.
355365
356366
<a name="puts"></a>TH1 puts Command
357367
-----------------------------------
358368
359369
* puts STRING
360370
--- www/th1.md
+++ www/th1.md
@@ -139,10 +139,11 @@
139 * html
140 * htmlize
141 * http
142 * httpize
143 * linecount
 
144 * puts
145 * query
146 * randhex
147 * regexp
148 * reinitialize
@@ -350,10 +351,19 @@
350
351 * linecount STRING MAX MIN
352
353 Returns one more than the number of \n characters in STRING. But
354 never returns less than MIN or more than MAX.
 
 
 
 
 
 
 
 
 
355
356 <a name="puts"></a>TH1 puts Command
357 -----------------------------------
358
359 * puts STRING
360
--- www/th1.md
+++ www/th1.md
@@ -139,10 +139,11 @@
139 * html
140 * htmlize
141 * http
142 * httpize
143 * linecount
144 * markdown
145 * puts
146 * query
147 * randhex
148 * regexp
149 * reinitialize
@@ -350,10 +351,19 @@
351
352 * linecount STRING MAX MIN
353
354 Returns one more than the number of \n characters in STRING. But
355 never returns less than MIN or more than MAX.
356
357 <a name="markdown"></a>TH1 markdown Command
358 ---------------------------------------------
359
360 * markdown STRING
361
362 Renders the input string as markdown. The result is a two-element list.
363 The first element contains the body, rendered as HTML. The second element
364 is the text-only title string.
365
366 <a name="puts"></a>TH1 puts Command
367 -----------------------------------
368
369 * puts STRING
370
+1 -1
--- www/webui.wiki
+++ www/webui.wiki
@@ -61,11 +61,11 @@
6161
<b>fossil ui</b>
6262
6363
The latter case is a very useful short-cut when you are working on a
6464
Fossil project and you want to quickly do some work with the web interface.
6565
Notice that Fossil automatically finds an unused TCP port to run the
66
-server own and automatically points your web browser to the correct
66
+server on and automatically points your web browser to the correct
6767
URL. So there is never any fumbling around trying to find an open
6868
port or to type arcane strings into your browser URL entry box.
6969
The interface just pops right up, ready to run.
7070
7171
The Fossil web interface is also very easy to setup and run on a
7272
--- www/webui.wiki
+++ www/webui.wiki
@@ -61,11 +61,11 @@
61 <b>fossil ui</b>
62
63 The latter case is a very useful short-cut when you are working on a
64 Fossil project and you want to quickly do some work with the web interface.
65 Notice that Fossil automatically finds an unused TCP port to run the
66 server own and automatically points your web browser to the correct
67 URL. So there is never any fumbling around trying to find an open
68 port or to type arcane strings into your browser URL entry box.
69 The interface just pops right up, ready to run.
70
71 The Fossil web interface is also very easy to setup and run on a
72
--- www/webui.wiki
+++ www/webui.wiki
@@ -61,11 +61,11 @@
61 <b>fossil ui</b>
62
63 The latter case is a very useful short-cut when you are working on a
64 Fossil project and you want to quickly do some work with the web interface.
65 Notice that Fossil automatically finds an unused TCP port to run the
66 server on and automatically points your web browser to the correct
67 URL. So there is never any fumbling around trying to find an open
68 port or to type arcane strings into your browser URL entry box.
69 The interface just pops right up, ready to run.
70
71 The Fossil web interface is also very easy to setup and run on a
72

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button