Fossil SCM

Update the built-in SQLite to the latest 3.39.0 beta for testing.

drh 2022-05-25 15:12 trunk
Commit c8a16fda7f180370223379ed8caa64a772315bb2f68b2a99f0f90e9078fe19dc
3 files changed +2580 -2337 +135 -33 +21 -1
+2580 -2337
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -245,10 +245,20 @@
245245
#else
246246
# define setBinaryMode(X,Y)
247247
# define setTextMode(X,Y)
248248
#endif
249249
250
+/*
251
+** When compiling with emcc (a.k.a. emscripten), we're building a
252
+** WebAssembly (WASM) bundle and need to disable and rewire a few
253
+** things.
254
+*/
255
+#ifdef __EMSCRIPTEN__
256
+#define SQLITE_SHELL_WASM_MODE
257
+#else
258
+#undef SQLITE_SHELL_WASM_MODE
259
+#endif
250260
251261
/* True if the timer is enabled */
252262
static int enableTimer = 0;
253263
254264
/* Return the current wall-clock time */
@@ -707,10 +717,11 @@
707717
**
708718
** The result is stored in space obtained from malloc() and must either
709719
** be freed by the caller or else passed back into this routine via the
710720
** zPrior argument for reuse.
711721
*/
722
+#ifndef SQLITE_SHELL_WASM_MODE
712723
static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
713724
char *zPrompt;
714725
char *zResult;
715726
if( in!=0 ){
716727
zResult = local_getline(zPrior, in);
@@ -726,11 +737,11 @@
726737
if( zResult && *zResult ) shell_add_history(zResult);
727738
#endif
728739
}
729740
return zResult;
730741
}
731
-
742
+#endif /* !SQLITE_SHELL_WASM_MODE */
732743
733744
/*
734745
** Return the value of a hexadecimal digit. Return -1 if the input
735746
** is not a hex digit.
736747
*/
@@ -814,11 +825,11 @@
814825
** zIn, if it was not NULL, is freed.
815826
**
816827
** If the third argument, quote, is not '\0', then it is used as a
817828
** quote character for zAppend.
818829
*/
819
-static void appendText(ShellText *p, char const *zAppend, char quote){
830
+static void appendText(ShellText *p, const char *zAppend, char quote){
820831
int len;
821832
int i;
822833
int nAppend = strlen30(zAppend);
823834
824835
len = nAppend+p->n+1;
@@ -1379,10 +1390,121 @@
13791390
#endif /* defined(WIN32) && defined(_MSC_VER) */
13801391
13811392
/************************* End test_windirent.c ********************/
13821393
#define dirent DIRENT
13831394
#endif
1395
+/************************* Begin ../ext/misc/memtrace.c ******************/
1396
+/*
1397
+** 2019-01-21
1398
+**
1399
+** The author disclaims copyright to this source code. In place of
1400
+** a legal notice, here is a blessing:
1401
+**
1402
+** May you do good and not evil.
1403
+** May you find forgiveness for yourself and forgive others.
1404
+** May you share freely, never taking more than you give.
1405
+**
1406
+*************************************************************************
1407
+**
1408
+** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
1409
+** mechanism to add a tracing layer on top of SQLite. If this extension
1410
+** is registered prior to sqlite3_initialize(), it will cause all memory
1411
+** allocation activities to be logged on standard output, or to some other
1412
+** FILE specified by the initializer.
1413
+**
1414
+** This file needs to be compiled into the application that uses it.
1415
+**
1416
+** This extension is used to implement the --memtrace option of the
1417
+** command-line shell.
1418
+*/
1419
+#include <assert.h>
1420
+#include <string.h>
1421
+#include <stdio.h>
1422
+
1423
+/* The original memory allocation routines */
1424
+static sqlite3_mem_methods memtraceBase;
1425
+static FILE *memtraceOut;
1426
+
1427
+/* Methods that trace memory allocations */
1428
+static void *memtraceMalloc(int n){
1429
+ if( memtraceOut ){
1430
+ fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
1431
+ memtraceBase.xRoundup(n));
1432
+ }
1433
+ return memtraceBase.xMalloc(n);
1434
+}
1435
+static void memtraceFree(void *p){
1436
+ if( p==0 ) return;
1437
+ if( memtraceOut ){
1438
+ fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
1439
+ }
1440
+ memtraceBase.xFree(p);
1441
+}
1442
+static void *memtraceRealloc(void *p, int n){
1443
+ if( p==0 ) return memtraceMalloc(n);
1444
+ if( n==0 ){
1445
+ memtraceFree(p);
1446
+ return 0;
1447
+ }
1448
+ if( memtraceOut ){
1449
+ fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
1450
+ memtraceBase.xSize(p), memtraceBase.xRoundup(n));
1451
+ }
1452
+ return memtraceBase.xRealloc(p, n);
1453
+}
1454
+static int memtraceSize(void *p){
1455
+ return memtraceBase.xSize(p);
1456
+}
1457
+static int memtraceRoundup(int n){
1458
+ return memtraceBase.xRoundup(n);
1459
+}
1460
+static int memtraceInit(void *p){
1461
+ return memtraceBase.xInit(p);
1462
+}
1463
+static void memtraceShutdown(void *p){
1464
+ memtraceBase.xShutdown(p);
1465
+}
1466
+
1467
+/* The substitute memory allocator */
1468
+static sqlite3_mem_methods ersaztMethods = {
1469
+ memtraceMalloc,
1470
+ memtraceFree,
1471
+ memtraceRealloc,
1472
+ memtraceSize,
1473
+ memtraceRoundup,
1474
+ memtraceInit,
1475
+ memtraceShutdown,
1476
+ 0
1477
+};
1478
+
1479
+/* Begin tracing memory allocations to out. */
1480
+int sqlite3MemTraceActivate(FILE *out){
1481
+ int rc = SQLITE_OK;
1482
+ if( memtraceBase.xMalloc==0 ){
1483
+ rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
1484
+ if( rc==SQLITE_OK ){
1485
+ rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
1486
+ }
1487
+ }
1488
+ memtraceOut = out;
1489
+ return rc;
1490
+}
1491
+
1492
+/* Deactivate memory tracing */
1493
+int sqlite3MemTraceDeactivate(void){
1494
+ int rc = SQLITE_OK;
1495
+ if( memtraceBase.xMalloc!=0 ){
1496
+ rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
1497
+ if( rc==SQLITE_OK ){
1498
+ memset(&memtraceBase, 0, sizeof(memtraceBase));
1499
+ }
1500
+ }
1501
+ memtraceOut = 0;
1502
+ return rc;
1503
+}
1504
+
1505
+/************************* End ../ext/misc/memtrace.c ********************/
13841506
/************************* Begin ../ext/misc/shathree.c ******************/
13851507
/*
13861508
** 2017-03-08
13871509
**
13881510
** The author disclaims copyright to this source code. In place of
@@ -2106,2333 +2228,10 @@
21062228
}
21072229
return rc;
21082230
}
21092231
21102232
/************************* End ../ext/misc/shathree.c ********************/
2111
-/************************* Begin ../ext/misc/fileio.c ******************/
2112
-/*
2113
-** 2014-06-13
2114
-**
2115
-** The author disclaims copyright to this source code. In place of
2116
-** a legal notice, here is a blessing:
2117
-**
2118
-** May you do good and not evil.
2119
-** May you find forgiveness for yourself and forgive others.
2120
-** May you share freely, never taking more than you give.
2121
-**
2122
-******************************************************************************
2123
-**
2124
-** This SQLite extension implements SQL functions readfile() and
2125
-** writefile(), and eponymous virtual type "fsdir".
2126
-**
2127
-** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2128
-**
2129
-** If neither of the optional arguments is present, then this UDF
2130
-** function writes blob DATA to file FILE. If successful, the number
2131
-** of bytes written is returned. If an error occurs, NULL is returned.
2132
-**
2133
-** If the first option argument - MODE - is present, then it must
2134
-** be passed an integer value that corresponds to a POSIX mode
2135
-** value (file type + permissions, as returned in the stat.st_mode
2136
-** field by the stat() system call). Three types of files may
2137
-** be written/created:
2138
-**
2139
-** regular files: (mode & 0170000)==0100000
2140
-** symbolic links: (mode & 0170000)==0120000
2141
-** directories: (mode & 0170000)==0040000
2142
-**
2143
-** For a directory, the DATA is ignored. For a symbolic link, it is
2144
-** interpreted as text and used as the target of the link. For a
2145
-** regular file, it is interpreted as a blob and written into the
2146
-** named file. Regardless of the type of file, its permissions are
2147
-** set to (mode & 0777) before returning.
2148
-**
2149
-** If the optional MTIME argument is present, then it is interpreted
2150
-** as an integer - the number of seconds since the unix epoch. The
2151
-** modification-time of the target file is set to this value before
2152
-** returning.
2153
-**
2154
-** If three or more arguments are passed to this function and an
2155
-** error is encountered, an exception is raised.
2156
-**
2157
-** READFILE(FILE):
2158
-**
2159
-** Read and return the contents of file FILE (type blob) from disk.
2160
-**
2161
-** FSDIR:
2162
-**
2163
-** Used as follows:
2164
-**
2165
-** SELECT * FROM fsdir($path [, $dir]);
2166
-**
2167
-** Parameter $path is an absolute or relative pathname. If the file that it
2168
-** refers to does not exist, it is an error. If the path refers to a regular
2169
-** file or symbolic link, it returns a single row. Or, if the path refers
2170
-** to a directory, it returns one row for the directory, and one row for each
2171
-** file within the hierarchy rooted at $path.
2172
-**
2173
-** Each row has the following columns:
2174
-**
2175
-** name: Path to file or directory (text value).
2176
-** mode: Value of stat.st_mode for directory entry (an integer).
2177
-** mtime: Value of stat.st_mtime for directory entry (an integer).
2178
-** data: For a regular file, a blob containing the file data. For a
2179
-** symlink, a text value containing the text of the link. For a
2180
-** directory, NULL.
2181
-**
2182
-** If a non-NULL value is specified for the optional $dir parameter and
2183
-** $path is a relative path, then $path is interpreted relative to $dir.
2184
-** And the paths returned in the "name" column of the table are also
2185
-** relative to directory $dir.
2186
-**
2187
-** Notes on building this extension for Windows:
2188
-** Unless linked statically with the SQLite library, a preprocessor
2189
-** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
2190
-** DLL form of this extension for WIN32. See its use below for details.
2191
-*/
2192
-/* #include "sqlite3ext.h" */
2193
-SQLITE_EXTENSION_INIT1
2194
-#include <stdio.h>
2195
-#include <string.h>
2196
-#include <assert.h>
2197
-
2198
-#include <sys/types.h>
2199
-#include <sys/stat.h>
2200
-#include <fcntl.h>
2201
-#if !defined(_WIN32) && !defined(WIN32)
2202
-# include <unistd.h>
2203
-# include <dirent.h>
2204
-# include <utime.h>
2205
-# include <sys/time.h>
2206
-#else
2207
-# include "windows.h"
2208
-# include <io.h>
2209
-# include <direct.h>
2210
-/* # include "test_windirent.h" */
2211
-# define dirent DIRENT
2212
-# ifndef chmod
2213
-# define chmod _chmod
2214
-# endif
2215
-# ifndef stat
2216
-# define stat _stat
2217
-# endif
2218
-# define mkdir(path,mode) _mkdir(path)
2219
-# define lstat(path,buf) stat(path,buf)
2220
-#endif
2221
-#include <time.h>
2222
-#include <errno.h>
2223
-
2224
-
2225
-/*
2226
-** Structure of the fsdir() table-valued function
2227
-*/
2228
- /* 0 1 2 3 4 5 */
2229
-#define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2230
-#define FSDIR_COLUMN_NAME 0 /* Name of the file */
2231
-#define FSDIR_COLUMN_MODE 1 /* Access mode */
2232
-#define FSDIR_COLUMN_MTIME 2 /* Last modification time */
2233
-#define FSDIR_COLUMN_DATA 3 /* File content */
2234
-#define FSDIR_COLUMN_PATH 4 /* Path to top of search */
2235
-#define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
2236
-
2237
-
2238
-/*
2239
-** Set the result stored by context ctx to a blob containing the
2240
-** contents of file zName. Or, leave the result unchanged (NULL)
2241
-** if the file does not exist or is unreadable.
2242
-**
2243
-** If the file exceeds the SQLite blob size limit, through an
2244
-** SQLITE_TOOBIG error.
2245
-**
2246
-** Throw an SQLITE_IOERR if there are difficulties pulling the file
2247
-** off of disk.
2248
-*/
2249
-static void readFileContents(sqlite3_context *ctx, const char *zName){
2250
- FILE *in;
2251
- sqlite3_int64 nIn;
2252
- void *pBuf;
2253
- sqlite3 *db;
2254
- int mxBlob;
2255
-
2256
- in = fopen(zName, "rb");
2257
- if( in==0 ){
2258
- /* File does not exist or is unreadable. Leave the result set to NULL. */
2259
- return;
2260
- }
2261
- fseek(in, 0, SEEK_END);
2262
- nIn = ftell(in);
2263
- rewind(in);
2264
- db = sqlite3_context_db_handle(ctx);
2265
- mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2266
- if( nIn>mxBlob ){
2267
- sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2268
- fclose(in);
2269
- return;
2270
- }
2271
- pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2272
- if( pBuf==0 ){
2273
- sqlite3_result_error_nomem(ctx);
2274
- fclose(in);
2275
- return;
2276
- }
2277
- if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2278
- sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2279
- }else{
2280
- sqlite3_result_error_code(ctx, SQLITE_IOERR);
2281
- sqlite3_free(pBuf);
2282
- }
2283
- fclose(in);
2284
-}
2285
-
2286
-/*
2287
-** Implementation of the "readfile(X)" SQL function. The entire content
2288
-** of the file named X is read and returned as a BLOB. NULL is returned
2289
-** if the file does not exist or is unreadable.
2290
-*/
2291
-static void readfileFunc(
2292
- sqlite3_context *context,
2293
- int argc,
2294
- sqlite3_value **argv
2295
-){
2296
- const char *zName;
2297
- (void)(argc); /* Unused parameter */
2298
- zName = (const char*)sqlite3_value_text(argv[0]);
2299
- if( zName==0 ) return;
2300
- readFileContents(context, zName);
2301
-}
2302
-
2303
-/*
2304
-** Set the error message contained in context ctx to the results of
2305
-** vprintf(zFmt, ...).
2306
-*/
2307
-static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2308
- char *zMsg = 0;
2309
- va_list ap;
2310
- va_start(ap, zFmt);
2311
- zMsg = sqlite3_vmprintf(zFmt, ap);
2312
- sqlite3_result_error(ctx, zMsg, -1);
2313
- sqlite3_free(zMsg);
2314
- va_end(ap);
2315
-}
2316
-
2317
-#if defined(_WIN32)
2318
-/*
2319
-** This function is designed to convert a Win32 FILETIME structure into the
2320
-** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2321
-*/
2322
-static sqlite3_uint64 fileTimeToUnixTime(
2323
- LPFILETIME pFileTime
2324
-){
2325
- SYSTEMTIME epochSystemTime;
2326
- ULARGE_INTEGER epochIntervals;
2327
- FILETIME epochFileTime;
2328
- ULARGE_INTEGER fileIntervals;
2329
-
2330
- memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2331
- epochSystemTime.wYear = 1970;
2332
- epochSystemTime.wMonth = 1;
2333
- epochSystemTime.wDay = 1;
2334
- SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2335
- epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2336
- epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2337
-
2338
- fileIntervals.LowPart = pFileTime->dwLowDateTime;
2339
- fileIntervals.HighPart = pFileTime->dwHighDateTime;
2340
-
2341
- return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2342
-}
2343
-
2344
-
2345
-#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
2346
-# /* To allow a standalone DLL, use this next replacement function: */
2347
-# undef sqlite3_win32_utf8_to_unicode
2348
-# define sqlite3_win32_utf8_to_unicode utf8_to_utf16
2349
-#
2350
-LPWSTR utf8_to_utf16(const char *z){
2351
- int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0);
2352
- LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR));
2353
- if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) )
2354
- return rv;
2355
- sqlite3_free(rv);
2356
- return 0;
2357
-}
2358
-#endif
2359
-
2360
-/*
2361
-** This function attempts to normalize the time values found in the stat()
2362
-** buffer to UTC. This is necessary on Win32, where the runtime library
2363
-** appears to return these values as local times.
2364
-*/
2365
-static void statTimesToUtc(
2366
- const char *zPath,
2367
- struct stat *pStatBuf
2368
-){
2369
- HANDLE hFindFile;
2370
- WIN32_FIND_DATAW fd;
2371
- LPWSTR zUnicodeName;
2372
- extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2373
- zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2374
- if( zUnicodeName ){
2375
- memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2376
- hFindFile = FindFirstFileW(zUnicodeName, &fd);
2377
- if( hFindFile!=NULL ){
2378
- pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2379
- pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2380
- pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2381
- FindClose(hFindFile);
2382
- }
2383
- sqlite3_free(zUnicodeName);
2384
- }
2385
-}
2386
-#endif
2387
-
2388
-/*
2389
-** This function is used in place of stat(). On Windows, special handling
2390
-** is required in order for the included time to be returned as UTC. On all
2391
-** other systems, this function simply calls stat().
2392
-*/
2393
-static int fileStat(
2394
- const char *zPath,
2395
- struct stat *pStatBuf
2396
-){
2397
-#if defined(_WIN32)
2398
- int rc = stat(zPath, pStatBuf);
2399
- if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2400
- return rc;
2401
-#else
2402
- return stat(zPath, pStatBuf);
2403
-#endif
2404
-}
2405
-
2406
-/*
2407
-** This function is used in place of lstat(). On Windows, special handling
2408
-** is required in order for the included time to be returned as UTC. On all
2409
-** other systems, this function simply calls lstat().
2410
-*/
2411
-static int fileLinkStat(
2412
- const char *zPath,
2413
- struct stat *pStatBuf
2414
-){
2415
-#if defined(_WIN32)
2416
- int rc = lstat(zPath, pStatBuf);
2417
- if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2418
- return rc;
2419
-#else
2420
- return lstat(zPath, pStatBuf);
2421
-#endif
2422
-}
2423
-
2424
-/*
2425
-** Argument zFile is the name of a file that will be created and/or written
2426
-** by SQL function writefile(). This function ensures that the directory
2427
-** zFile will be written to exists, creating it if required. The permissions
2428
-** for any path components created by this function are set in accordance
2429
-** with the current umask.
2430
-**
2431
-** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2432
-** SQLITE_OK is returned if the directory is successfully created, or
2433
-** SQLITE_ERROR otherwise.
2434
-*/
2435
-static int makeDirectory(
2436
- const char *zFile
2437
-){
2438
- char *zCopy = sqlite3_mprintf("%s", zFile);
2439
- int rc = SQLITE_OK;
2440
-
2441
- if( zCopy==0 ){
2442
- rc = SQLITE_NOMEM;
2443
- }else{
2444
- int nCopy = (int)strlen(zCopy);
2445
- int i = 1;
2446
-
2447
- while( rc==SQLITE_OK ){
2448
- struct stat sStat;
2449
- int rc2;
2450
-
2451
- for(; zCopy[i]!='/' && i<nCopy; i++);
2452
- if( i==nCopy ) break;
2453
- zCopy[i] = '\0';
2454
-
2455
- rc2 = fileStat(zCopy, &sStat);
2456
- if( rc2!=0 ){
2457
- if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2458
- }else{
2459
- if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2460
- }
2461
- zCopy[i] = '/';
2462
- i++;
2463
- }
2464
-
2465
- sqlite3_free(zCopy);
2466
- }
2467
-
2468
- return rc;
2469
-}
2470
-
2471
-/*
2472
-** This function does the work for the writefile() UDF. Refer to
2473
-** header comments at the top of this file for details.
2474
-*/
2475
-static int writeFile(
2476
- sqlite3_context *pCtx, /* Context to return bytes written in */
2477
- const char *zFile, /* File to write */
2478
- sqlite3_value *pData, /* Data to write */
2479
- mode_t mode, /* MODE parameter passed to writefile() */
2480
- sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2481
-){
2482
- if( zFile==0 ) return 1;
2483
-#if !defined(_WIN32) && !defined(WIN32)
2484
- if( S_ISLNK(mode) ){
2485
- const char *zTo = (const char*)sqlite3_value_text(pData);
2486
- if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
2487
- }else
2488
-#endif
2489
- {
2490
- if( S_ISDIR(mode) ){
2491
- if( mkdir(zFile, mode) ){
2492
- /* The mkdir() call to create the directory failed. This might not
2493
- ** be an error though - if there is already a directory at the same
2494
- ** path and either the permissions already match or can be changed
2495
- ** to do so using chmod(), it is not an error. */
2496
- struct stat sStat;
2497
- if( errno!=EEXIST
2498
- || 0!=fileStat(zFile, &sStat)
2499
- || !S_ISDIR(sStat.st_mode)
2500
- || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2501
- ){
2502
- return 1;
2503
- }
2504
- }
2505
- }else{
2506
- sqlite3_int64 nWrite = 0;
2507
- const char *z;
2508
- int rc = 0;
2509
- FILE *out = fopen(zFile, "wb");
2510
- if( out==0 ) return 1;
2511
- z = (const char*)sqlite3_value_blob(pData);
2512
- if( z ){
2513
- sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2514
- nWrite = sqlite3_value_bytes(pData);
2515
- if( nWrite!=n ){
2516
- rc = 1;
2517
- }
2518
- }
2519
- fclose(out);
2520
- if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2521
- rc = 1;
2522
- }
2523
- if( rc ) return 2;
2524
- sqlite3_result_int64(pCtx, nWrite);
2525
- }
2526
- }
2527
-
2528
- if( mtime>=0 ){
2529
-#if defined(_WIN32)
2530
-#if !SQLITE_OS_WINRT
2531
- /* Windows */
2532
- FILETIME lastAccess;
2533
- FILETIME lastWrite;
2534
- SYSTEMTIME currentTime;
2535
- LONGLONG intervals;
2536
- HANDLE hFile;
2537
- LPWSTR zUnicodeName;
2538
- extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2539
-
2540
- GetSystemTime(&currentTime);
2541
- SystemTimeToFileTime(&currentTime, &lastAccess);
2542
- intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2543
- lastWrite.dwLowDateTime = (DWORD)intervals;
2544
- lastWrite.dwHighDateTime = intervals >> 32;
2545
- zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2546
- if( zUnicodeName==0 ){
2547
- return 1;
2548
- }
2549
- hFile = CreateFileW(
2550
- zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2551
- FILE_FLAG_BACKUP_SEMANTICS, NULL
2552
- );
2553
- sqlite3_free(zUnicodeName);
2554
- if( hFile!=INVALID_HANDLE_VALUE ){
2555
- BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2556
- CloseHandle(hFile);
2557
- return !bResult;
2558
- }else{
2559
- return 1;
2560
- }
2561
-#endif
2562
-#elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2563
- /* Recent unix */
2564
- struct timespec times[2];
2565
- times[0].tv_nsec = times[1].tv_nsec = 0;
2566
- times[0].tv_sec = time(0);
2567
- times[1].tv_sec = mtime;
2568
- if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2569
- return 1;
2570
- }
2571
-#else
2572
- /* Legacy unix */
2573
- struct timeval times[2];
2574
- times[0].tv_usec = times[1].tv_usec = 0;
2575
- times[0].tv_sec = time(0);
2576
- times[1].tv_sec = mtime;
2577
- if( utimes(zFile, times) ){
2578
- return 1;
2579
- }
2580
-#endif
2581
- }
2582
-
2583
- return 0;
2584
-}
2585
-
2586
-/*
2587
-** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2588
-** Refer to header comments at the top of this file for details.
2589
-*/
2590
-static void writefileFunc(
2591
- sqlite3_context *context,
2592
- int argc,
2593
- sqlite3_value **argv
2594
-){
2595
- const char *zFile;
2596
- mode_t mode = 0;
2597
- int res;
2598
- sqlite3_int64 mtime = -1;
2599
-
2600
- if( argc<2 || argc>4 ){
2601
- sqlite3_result_error(context,
2602
- "wrong number of arguments to function writefile()", -1
2603
- );
2604
- return;
2605
- }
2606
-
2607
- zFile = (const char*)sqlite3_value_text(argv[0]);
2608
- if( zFile==0 ) return;
2609
- if( argc>=3 ){
2610
- mode = (mode_t)sqlite3_value_int(argv[2]);
2611
- }
2612
- if( argc==4 ){
2613
- mtime = sqlite3_value_int64(argv[3]);
2614
- }
2615
-
2616
- res = writeFile(context, zFile, argv[1], mode, mtime);
2617
- if( res==1 && errno==ENOENT ){
2618
- if( makeDirectory(zFile)==SQLITE_OK ){
2619
- res = writeFile(context, zFile, argv[1], mode, mtime);
2620
- }
2621
- }
2622
-
2623
- if( argc>2 && res!=0 ){
2624
- if( S_ISLNK(mode) ){
2625
- ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2626
- }else if( S_ISDIR(mode) ){
2627
- ctxErrorMsg(context, "failed to create directory: %s", zFile);
2628
- }else{
2629
- ctxErrorMsg(context, "failed to write file: %s", zFile);
2630
- }
2631
- }
2632
-}
2633
-
2634
-/*
2635
-** SQL function: lsmode(MODE)
2636
-**
2637
-** Given a numberic st_mode from stat(), convert it into a human-readable
2638
-** text string in the style of "ls -l".
2639
-*/
2640
-static void lsModeFunc(
2641
- sqlite3_context *context,
2642
- int argc,
2643
- sqlite3_value **argv
2644
-){
2645
- int i;
2646
- int iMode = sqlite3_value_int(argv[0]);
2647
- char z[16];
2648
- (void)argc;
2649
- if( S_ISLNK(iMode) ){
2650
- z[0] = 'l';
2651
- }else if( S_ISREG(iMode) ){
2652
- z[0] = '-';
2653
- }else if( S_ISDIR(iMode) ){
2654
- z[0] = 'd';
2655
- }else{
2656
- z[0] = '?';
2657
- }
2658
- for(i=0; i<3; i++){
2659
- int m = (iMode >> ((2-i)*3));
2660
- char *a = &z[1 + i*3];
2661
- a[0] = (m & 0x4) ? 'r' : '-';
2662
- a[1] = (m & 0x2) ? 'w' : '-';
2663
- a[2] = (m & 0x1) ? 'x' : '-';
2664
- }
2665
- z[10] = '\0';
2666
- sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2667
-}
2668
-
2669
-#ifndef SQLITE_OMIT_VIRTUALTABLE
2670
-
2671
-/*
2672
-** Cursor type for recursively iterating through a directory structure.
2673
-*/
2674
-typedef struct fsdir_cursor fsdir_cursor;
2675
-typedef struct FsdirLevel FsdirLevel;
2676
-
2677
-struct FsdirLevel {
2678
- DIR *pDir; /* From opendir() */
2679
- char *zDir; /* Name of directory (nul-terminated) */
2680
-};
2681
-
2682
-struct fsdir_cursor {
2683
- sqlite3_vtab_cursor base; /* Base class - must be first */
2684
-
2685
- int nLvl; /* Number of entries in aLvl[] array */
2686
- int iLvl; /* Index of current entry */
2687
- FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2688
-
2689
- const char *zBase;
2690
- int nBase;
2691
-
2692
- struct stat sStat; /* Current lstat() results */
2693
- char *zPath; /* Path to current entry */
2694
- sqlite3_int64 iRowid; /* Current rowid */
2695
-};
2696
-
2697
-typedef struct fsdir_tab fsdir_tab;
2698
-struct fsdir_tab {
2699
- sqlite3_vtab base; /* Base class - must be first */
2700
-};
2701
-
2702
-/*
2703
-** Construct a new fsdir virtual table object.
2704
-*/
2705
-static int fsdirConnect(
2706
- sqlite3 *db,
2707
- void *pAux,
2708
- int argc, const char *const*argv,
2709
- sqlite3_vtab **ppVtab,
2710
- char **pzErr
2711
-){
2712
- fsdir_tab *pNew = 0;
2713
- int rc;
2714
- (void)pAux;
2715
- (void)argc;
2716
- (void)argv;
2717
- (void)pzErr;
2718
- rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2719
- if( rc==SQLITE_OK ){
2720
- pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2721
- if( pNew==0 ) return SQLITE_NOMEM;
2722
- memset(pNew, 0, sizeof(*pNew));
2723
- sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2724
- }
2725
- *ppVtab = (sqlite3_vtab*)pNew;
2726
- return rc;
2727
-}
2728
-
2729
-/*
2730
-** This method is the destructor for fsdir vtab objects.
2731
-*/
2732
-static int fsdirDisconnect(sqlite3_vtab *pVtab){
2733
- sqlite3_free(pVtab);
2734
- return SQLITE_OK;
2735
-}
2736
-
2737
-/*
2738
-** Constructor for a new fsdir_cursor object.
2739
-*/
2740
-static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2741
- fsdir_cursor *pCur;
2742
- (void)p;
2743
- pCur = sqlite3_malloc( sizeof(*pCur) );
2744
- if( pCur==0 ) return SQLITE_NOMEM;
2745
- memset(pCur, 0, sizeof(*pCur));
2746
- pCur->iLvl = -1;
2747
- *ppCursor = &pCur->base;
2748
- return SQLITE_OK;
2749
-}
2750
-
2751
-/*
2752
-** Reset a cursor back to the state it was in when first returned
2753
-** by fsdirOpen().
2754
-*/
2755
-static void fsdirResetCursor(fsdir_cursor *pCur){
2756
- int i;
2757
- for(i=0; i<=pCur->iLvl; i++){
2758
- FsdirLevel *pLvl = &pCur->aLvl[i];
2759
- if( pLvl->pDir ) closedir(pLvl->pDir);
2760
- sqlite3_free(pLvl->zDir);
2761
- }
2762
- sqlite3_free(pCur->zPath);
2763
- sqlite3_free(pCur->aLvl);
2764
- pCur->aLvl = 0;
2765
- pCur->zPath = 0;
2766
- pCur->zBase = 0;
2767
- pCur->nBase = 0;
2768
- pCur->nLvl = 0;
2769
- pCur->iLvl = -1;
2770
- pCur->iRowid = 1;
2771
-}
2772
-
2773
-/*
2774
-** Destructor for an fsdir_cursor.
2775
-*/
2776
-static int fsdirClose(sqlite3_vtab_cursor *cur){
2777
- fsdir_cursor *pCur = (fsdir_cursor*)cur;
2778
-
2779
- fsdirResetCursor(pCur);
2780
- sqlite3_free(pCur);
2781
- return SQLITE_OK;
2782
-}
2783
-
2784
-/*
2785
-** Set the error message for the virtual table associated with cursor
2786
-** pCur to the results of vprintf(zFmt, ...).
2787
-*/
2788
-static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2789
- va_list ap;
2790
- va_start(ap, zFmt);
2791
- pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2792
- va_end(ap);
2793
-}
2794
-
2795
-
2796
-/*
2797
-** Advance an fsdir_cursor to its next row of output.
2798
-*/
2799
-static int fsdirNext(sqlite3_vtab_cursor *cur){
2800
- fsdir_cursor *pCur = (fsdir_cursor*)cur;
2801
- mode_t m = pCur->sStat.st_mode;
2802
-
2803
- pCur->iRowid++;
2804
- if( S_ISDIR(m) ){
2805
- /* Descend into this directory */
2806
- int iNew = pCur->iLvl + 1;
2807
- FsdirLevel *pLvl;
2808
- if( iNew>=pCur->nLvl ){
2809
- int nNew = iNew+1;
2810
- sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2811
- FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2812
- if( aNew==0 ) return SQLITE_NOMEM;
2813
- memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2814
- pCur->aLvl = aNew;
2815
- pCur->nLvl = nNew;
2816
- }
2817
- pCur->iLvl = iNew;
2818
- pLvl = &pCur->aLvl[iNew];
2819
-
2820
- pLvl->zDir = pCur->zPath;
2821
- pCur->zPath = 0;
2822
- pLvl->pDir = opendir(pLvl->zDir);
2823
- if( pLvl->pDir==0 ){
2824
- fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2825
- return SQLITE_ERROR;
2826
- }
2827
- }
2828
-
2829
- while( pCur->iLvl>=0 ){
2830
- FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2831
- struct dirent *pEntry = readdir(pLvl->pDir);
2832
- if( pEntry ){
2833
- if( pEntry->d_name[0]=='.' ){
2834
- if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2835
- if( pEntry->d_name[1]=='\0' ) continue;
2836
- }
2837
- sqlite3_free(pCur->zPath);
2838
- pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2839
- if( pCur->zPath==0 ) return SQLITE_NOMEM;
2840
- if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2841
- fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2842
- return SQLITE_ERROR;
2843
- }
2844
- return SQLITE_OK;
2845
- }
2846
- closedir(pLvl->pDir);
2847
- sqlite3_free(pLvl->zDir);
2848
- pLvl->pDir = 0;
2849
- pLvl->zDir = 0;
2850
- pCur->iLvl--;
2851
- }
2852
-
2853
- /* EOF */
2854
- sqlite3_free(pCur->zPath);
2855
- pCur->zPath = 0;
2856
- return SQLITE_OK;
2857
-}
2858
-
2859
-/*
2860
-** Return values of columns for the row at which the series_cursor
2861
-** is currently pointing.
2862
-*/
2863
-static int fsdirColumn(
2864
- sqlite3_vtab_cursor *cur, /* The cursor */
2865
- sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2866
- int i /* Which column to return */
2867
-){
2868
- fsdir_cursor *pCur = (fsdir_cursor*)cur;
2869
- switch( i ){
2870
- case FSDIR_COLUMN_NAME: {
2871
- sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2872
- break;
2873
- }
2874
-
2875
- case FSDIR_COLUMN_MODE:
2876
- sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2877
- break;
2878
-
2879
- case FSDIR_COLUMN_MTIME:
2880
- sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2881
- break;
2882
-
2883
- case FSDIR_COLUMN_DATA: {
2884
- mode_t m = pCur->sStat.st_mode;
2885
- if( S_ISDIR(m) ){
2886
- sqlite3_result_null(ctx);
2887
-#if !defined(_WIN32) && !defined(WIN32)
2888
- }else if( S_ISLNK(m) ){
2889
- char aStatic[64];
2890
- char *aBuf = aStatic;
2891
- sqlite3_int64 nBuf = 64;
2892
- int n;
2893
-
2894
- while( 1 ){
2895
- n = readlink(pCur->zPath, aBuf, nBuf);
2896
- if( n<nBuf ) break;
2897
- if( aBuf!=aStatic ) sqlite3_free(aBuf);
2898
- nBuf = nBuf*2;
2899
- aBuf = sqlite3_malloc64(nBuf);
2900
- if( aBuf==0 ){
2901
- sqlite3_result_error_nomem(ctx);
2902
- return SQLITE_NOMEM;
2903
- }
2904
- }
2905
-
2906
- sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2907
- if( aBuf!=aStatic ) sqlite3_free(aBuf);
2908
-#endif
2909
- }else{
2910
- readFileContents(ctx, pCur->zPath);
2911
- }
2912
- }
2913
- case FSDIR_COLUMN_PATH:
2914
- default: {
2915
- /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2916
- ** always return their values as NULL */
2917
- break;
2918
- }
2919
- }
2920
- return SQLITE_OK;
2921
-}
2922
-
2923
-/*
2924
-** Return the rowid for the current row. In this implementation, the
2925
-** first row returned is assigned rowid value 1, and each subsequent
2926
-** row a value 1 more than that of the previous.
2927
-*/
2928
-static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2929
- fsdir_cursor *pCur = (fsdir_cursor*)cur;
2930
- *pRowid = pCur->iRowid;
2931
- return SQLITE_OK;
2932
-}
2933
-
2934
-/*
2935
-** Return TRUE if the cursor has been moved off of the last
2936
-** row of output.
2937
-*/
2938
-static int fsdirEof(sqlite3_vtab_cursor *cur){
2939
- fsdir_cursor *pCur = (fsdir_cursor*)cur;
2940
- return (pCur->zPath==0);
2941
-}
2942
-
2943
-/*
2944
-** xFilter callback.
2945
-**
2946
-** idxNum==1 PATH parameter only
2947
-** idxNum==2 Both PATH and DIR supplied
2948
-*/
2949
-static int fsdirFilter(
2950
- sqlite3_vtab_cursor *cur,
2951
- int idxNum, const char *idxStr,
2952
- int argc, sqlite3_value **argv
2953
-){
2954
- const char *zDir = 0;
2955
- fsdir_cursor *pCur = (fsdir_cursor*)cur;
2956
- (void)idxStr;
2957
- fsdirResetCursor(pCur);
2958
-
2959
- if( idxNum==0 ){
2960
- fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2961
- return SQLITE_ERROR;
2962
- }
2963
-
2964
- assert( argc==idxNum && (argc==1 || argc==2) );
2965
- zDir = (const char*)sqlite3_value_text(argv[0]);
2966
- if( zDir==0 ){
2967
- fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2968
- return SQLITE_ERROR;
2969
- }
2970
- if( argc==2 ){
2971
- pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2972
- }
2973
- if( pCur->zBase ){
2974
- pCur->nBase = (int)strlen(pCur->zBase)+1;
2975
- pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2976
- }else{
2977
- pCur->zPath = sqlite3_mprintf("%s", zDir);
2978
- }
2979
-
2980
- if( pCur->zPath==0 ){
2981
- return SQLITE_NOMEM;
2982
- }
2983
- if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2984
- fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2985
- return SQLITE_ERROR;
2986
- }
2987
-
2988
- return SQLITE_OK;
2989
-}
2990
-
2991
-/*
2992
-** SQLite will invoke this method one or more times while planning a query
2993
-** that uses the generate_series virtual table. This routine needs to create
2994
-** a query plan for each invocation and compute an estimated cost for that
2995
-** plan.
2996
-**
2997
-** In this implementation idxNum is used to represent the
2998
-** query plan. idxStr is unused.
2999
-**
3000
-** The query plan is represented by values of idxNum:
3001
-**
3002
-** (1) The path value is supplied by argv[0]
3003
-** (2) Path is in argv[0] and dir is in argv[1]
3004
-*/
3005
-static int fsdirBestIndex(
3006
- sqlite3_vtab *tab,
3007
- sqlite3_index_info *pIdxInfo
3008
-){
3009
- int i; /* Loop over constraints */
3010
- int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
3011
- int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
3012
- int seenPath = 0; /* True if an unusable PATH= constraint is seen */
3013
- int seenDir = 0; /* True if an unusable DIR= constraint is seen */
3014
- const struct sqlite3_index_constraint *pConstraint;
3015
-
3016
- (void)tab;
3017
- pConstraint = pIdxInfo->aConstraint;
3018
- for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3019
- if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3020
- switch( pConstraint->iColumn ){
3021
- case FSDIR_COLUMN_PATH: {
3022
- if( pConstraint->usable ){
3023
- idxPath = i;
3024
- seenPath = 0;
3025
- }else if( idxPath<0 ){
3026
- seenPath = 1;
3027
- }
3028
- break;
3029
- }
3030
- case FSDIR_COLUMN_DIR: {
3031
- if( pConstraint->usable ){
3032
- idxDir = i;
3033
- seenDir = 0;
3034
- }else if( idxDir<0 ){
3035
- seenDir = 1;
3036
- }
3037
- break;
3038
- }
3039
- }
3040
- }
3041
- if( seenPath || seenDir ){
3042
- /* If input parameters are unusable, disallow this plan */
3043
- return SQLITE_CONSTRAINT;
3044
- }
3045
-
3046
- if( idxPath<0 ){
3047
- pIdxInfo->idxNum = 0;
3048
- /* The pIdxInfo->estimatedCost should have been initialized to a huge
3049
- ** number. Leave it unchanged. */
3050
- pIdxInfo->estimatedRows = 0x7fffffff;
3051
- }else{
3052
- pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3053
- pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3054
- if( idxDir>=0 ){
3055
- pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3056
- pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3057
- pIdxInfo->idxNum = 2;
3058
- pIdxInfo->estimatedCost = 10.0;
3059
- }else{
3060
- pIdxInfo->idxNum = 1;
3061
- pIdxInfo->estimatedCost = 100.0;
3062
- }
3063
- }
3064
-
3065
- return SQLITE_OK;
3066
-}
3067
-
3068
-/*
3069
-** Register the "fsdir" virtual table.
3070
-*/
3071
-static int fsdirRegister(sqlite3 *db){
3072
- static sqlite3_module fsdirModule = {
3073
- 0, /* iVersion */
3074
- 0, /* xCreate */
3075
- fsdirConnect, /* xConnect */
3076
- fsdirBestIndex, /* xBestIndex */
3077
- fsdirDisconnect, /* xDisconnect */
3078
- 0, /* xDestroy */
3079
- fsdirOpen, /* xOpen - open a cursor */
3080
- fsdirClose, /* xClose - close a cursor */
3081
- fsdirFilter, /* xFilter - configure scan constraints */
3082
- fsdirNext, /* xNext - advance a cursor */
3083
- fsdirEof, /* xEof - check for end of scan */
3084
- fsdirColumn, /* xColumn - read data */
3085
- fsdirRowid, /* xRowid - read data */
3086
- 0, /* xUpdate */
3087
- 0, /* xBegin */
3088
- 0, /* xSync */
3089
- 0, /* xCommit */
3090
- 0, /* xRollback */
3091
- 0, /* xFindMethod */
3092
- 0, /* xRename */
3093
- 0, /* xSavepoint */
3094
- 0, /* xRelease */
3095
- 0, /* xRollbackTo */
3096
- 0, /* xShadowName */
3097
- };
3098
-
3099
- int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3100
- return rc;
3101
-}
3102
-#else /* SQLITE_OMIT_VIRTUALTABLE */
3103
-# define fsdirRegister(x) SQLITE_OK
3104
-#endif
3105
-
3106
-#ifdef _WIN32
3107
-
3108
-#endif
3109
-int sqlite3_fileio_init(
3110
- sqlite3 *db,
3111
- char **pzErrMsg,
3112
- const sqlite3_api_routines *pApi
3113
-){
3114
- int rc = SQLITE_OK;
3115
- SQLITE_EXTENSION_INIT2(pApi);
3116
- (void)pzErrMsg; /* Unused parameter */
3117
- rc = sqlite3_create_function(db, "readfile", 1,
3118
- SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3119
- readfileFunc, 0, 0);
3120
- if( rc==SQLITE_OK ){
3121
- rc = sqlite3_create_function(db, "writefile", -1,
3122
- SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3123
- writefileFunc, 0, 0);
3124
- }
3125
- if( rc==SQLITE_OK ){
3126
- rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3127
- lsModeFunc, 0, 0);
3128
- }
3129
- if( rc==SQLITE_OK ){
3130
- rc = fsdirRegister(db);
3131
- }
3132
- return rc;
3133
-}
3134
-
3135
-#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
3136
-/* To allow a standalone DLL, make test_windirent.c use the same
3137
- * redefined SQLite API calls as the above extension code does.
3138
- * Just pull in this .c to accomplish this. As a beneficial side
3139
- * effect, this extension becomes a single translation unit. */
3140
-# include "test_windirent.c"
3141
-#endif
3142
-
3143
-/************************* End ../ext/misc/fileio.c ********************/
3144
-/************************* Begin ../ext/misc/completion.c ******************/
3145
-/*
3146
-** 2017-07-10
3147
-**
3148
-** The author disclaims copyright to this source code. In place of
3149
-** a legal notice, here is a blessing:
3150
-**
3151
-** May you do good and not evil.
3152
-** May you find forgiveness for yourself and forgive others.
3153
-** May you share freely, never taking more than you give.
3154
-**
3155
-*************************************************************************
3156
-**
3157
-** This file implements an eponymous virtual table that returns suggested
3158
-** completions for a partial SQL input.
3159
-**
3160
-** Suggested usage:
3161
-**
3162
-** SELECT DISTINCT candidate COLLATE nocase
3163
-** FROM completion($prefix,$wholeline)
3164
-** ORDER BY 1;
3165
-**
3166
-** The two query parameters are optional. $prefix is the text of the
3167
-** current word being typed and that is to be completed. $wholeline is
3168
-** the complete input line, used for context.
3169
-**
3170
-** The raw completion() table might return the same candidate multiple
3171
-** times, for example if the same column name is used to two or more
3172
-** tables. And the candidates are returned in an arbitrary order. Hence,
3173
-** the DISTINCT and ORDER BY are recommended.
3174
-**
3175
-** This virtual table operates at the speed of human typing, and so there
3176
-** is no attempt to make it fast. Even a slow implementation will be much
3177
-** faster than any human can type.
3178
-**
3179
-*/
3180
-/* #include "sqlite3ext.h" */
3181
-SQLITE_EXTENSION_INIT1
3182
-#include <assert.h>
3183
-#include <string.h>
3184
-#include <ctype.h>
3185
-
3186
-#ifndef SQLITE_OMIT_VIRTUALTABLE
3187
-
3188
-/* completion_vtab is a subclass of sqlite3_vtab which will
3189
-** serve as the underlying representation of a completion virtual table
3190
-*/
3191
-typedef struct completion_vtab completion_vtab;
3192
-struct completion_vtab {
3193
- sqlite3_vtab base; /* Base class - must be first */
3194
- sqlite3 *db; /* Database connection for this completion vtab */
3195
-};
3196
-
3197
-/* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3198
-** serve as the underlying representation of a cursor that scans
3199
-** over rows of the result
3200
-*/
3201
-typedef struct completion_cursor completion_cursor;
3202
-struct completion_cursor {
3203
- sqlite3_vtab_cursor base; /* Base class - must be first */
3204
- sqlite3 *db; /* Database connection for this cursor */
3205
- int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
3206
- char *zPrefix; /* The prefix for the word we want to complete */
3207
- char *zLine; /* The whole that we want to complete */
3208
- const char *zCurrentRow; /* Current output row */
3209
- int szRow; /* Length of the zCurrentRow string */
3210
- sqlite3_stmt *pStmt; /* Current statement */
3211
- sqlite3_int64 iRowid; /* The rowid */
3212
- int ePhase; /* Current phase */
3213
- int j; /* inter-phase counter */
3214
-};
3215
-
3216
-/* Values for ePhase:
3217
-*/
3218
-#define COMPLETION_FIRST_PHASE 1
3219
-#define COMPLETION_KEYWORDS 1
3220
-#define COMPLETION_PRAGMAS 2
3221
-#define COMPLETION_FUNCTIONS 3
3222
-#define COMPLETION_COLLATIONS 4
3223
-#define COMPLETION_INDEXES 5
3224
-#define COMPLETION_TRIGGERS 6
3225
-#define COMPLETION_DATABASES 7
3226
-#define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
3227
-#define COMPLETION_COLUMNS 9
3228
-#define COMPLETION_MODULES 10
3229
-#define COMPLETION_EOF 11
3230
-
3231
-/*
3232
-** The completionConnect() method is invoked to create a new
3233
-** completion_vtab that describes the completion virtual table.
3234
-**
3235
-** Think of this routine as the constructor for completion_vtab objects.
3236
-**
3237
-** All this routine needs to do is:
3238
-**
3239
-** (1) Allocate the completion_vtab object and initialize all fields.
3240
-**
3241
-** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3242
-** result set of queries against completion will look like.
3243
-*/
3244
-static int completionConnect(
3245
- sqlite3 *db,
3246
- void *pAux,
3247
- int argc, const char *const*argv,
3248
- sqlite3_vtab **ppVtab,
3249
- char **pzErr
3250
-){
3251
- completion_vtab *pNew;
3252
- int rc;
3253
-
3254
- (void)(pAux); /* Unused parameter */
3255
- (void)(argc); /* Unused parameter */
3256
- (void)(argv); /* Unused parameter */
3257
- (void)(pzErr); /* Unused parameter */
3258
-
3259
-/* Column numbers */
3260
-#define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
3261
-#define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
3262
-#define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
3263
-#define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
3264
-
3265
- sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3266
- rc = sqlite3_declare_vtab(db,
3267
- "CREATE TABLE x("
3268
- " candidate TEXT,"
3269
- " prefix TEXT HIDDEN,"
3270
- " wholeline TEXT HIDDEN,"
3271
- " phase INT HIDDEN" /* Used for debugging only */
3272
- ")");
3273
- if( rc==SQLITE_OK ){
3274
- pNew = sqlite3_malloc( sizeof(*pNew) );
3275
- *ppVtab = (sqlite3_vtab*)pNew;
3276
- if( pNew==0 ) return SQLITE_NOMEM;
3277
- memset(pNew, 0, sizeof(*pNew));
3278
- pNew->db = db;
3279
- }
3280
- return rc;
3281
-}
3282
-
3283
-/*
3284
-** This method is the destructor for completion_cursor objects.
3285
-*/
3286
-static int completionDisconnect(sqlite3_vtab *pVtab){
3287
- sqlite3_free(pVtab);
3288
- return SQLITE_OK;
3289
-}
3290
-
3291
-/*
3292
-** Constructor for a new completion_cursor object.
3293
-*/
3294
-static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3295
- completion_cursor *pCur;
3296
- pCur = sqlite3_malloc( sizeof(*pCur) );
3297
- if( pCur==0 ) return SQLITE_NOMEM;
3298
- memset(pCur, 0, sizeof(*pCur));
3299
- pCur->db = ((completion_vtab*)p)->db;
3300
- *ppCursor = &pCur->base;
3301
- return SQLITE_OK;
3302
-}
3303
-
3304
-/*
3305
-** Reset the completion_cursor.
3306
-*/
3307
-static void completionCursorReset(completion_cursor *pCur){
3308
- sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3309
- sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3310
- sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3311
- pCur->j = 0;
3312
-}
3313
-
3314
-/*
3315
-** Destructor for a completion_cursor.
3316
-*/
3317
-static int completionClose(sqlite3_vtab_cursor *cur){
3318
- completionCursorReset((completion_cursor*)cur);
3319
- sqlite3_free(cur);
3320
- return SQLITE_OK;
3321
-}
3322
-
3323
-/*
3324
-** Advance a completion_cursor to its next row of output.
3325
-**
3326
-** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3327
-** record the current state of the scan. This routine sets ->zCurrentRow
3328
-** to the current row of output and then returns. If no more rows remain,
3329
-** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3330
-** table that has reached the end of its scan.
3331
-**
3332
-** The current implementation just lists potential identifiers and
3333
-** keywords and filters them by zPrefix. Future enhancements should
3334
-** take zLine into account to try to restrict the set of identifiers and
3335
-** keywords based on what would be legal at the current point of input.
3336
-*/
3337
-static int completionNext(sqlite3_vtab_cursor *cur){
3338
- completion_cursor *pCur = (completion_cursor*)cur;
3339
- int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3340
- int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3341
- pCur->iRowid++;
3342
- while( pCur->ePhase!=COMPLETION_EOF ){
3343
- switch( pCur->ePhase ){
3344
- case COMPLETION_KEYWORDS: {
3345
- if( pCur->j >= sqlite3_keyword_count() ){
3346
- pCur->zCurrentRow = 0;
3347
- pCur->ePhase = COMPLETION_DATABASES;
3348
- }else{
3349
- sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3350
- }
3351
- iCol = -1;
3352
- break;
3353
- }
3354
- case COMPLETION_DATABASES: {
3355
- if( pCur->pStmt==0 ){
3356
- sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3357
- &pCur->pStmt, 0);
3358
- }
3359
- iCol = 1;
3360
- eNextPhase = COMPLETION_TABLES;
3361
- break;
3362
- }
3363
- case COMPLETION_TABLES: {
3364
- if( pCur->pStmt==0 ){
3365
- sqlite3_stmt *pS2;
3366
- char *zSql = 0;
3367
- const char *zSep = "";
3368
- sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3369
- while( sqlite3_step(pS2)==SQLITE_ROW ){
3370
- const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3371
- zSql = sqlite3_mprintf(
3372
- "%z%s"
3373
- "SELECT name FROM \"%w\".sqlite_schema",
3374
- zSql, zSep, zDb
3375
- );
3376
- if( zSql==0 ) return SQLITE_NOMEM;
3377
- zSep = " UNION ";
3378
- }
3379
- sqlite3_finalize(pS2);
3380
- sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3381
- sqlite3_free(zSql);
3382
- }
3383
- iCol = 0;
3384
- eNextPhase = COMPLETION_COLUMNS;
3385
- break;
3386
- }
3387
- case COMPLETION_COLUMNS: {
3388
- if( pCur->pStmt==0 ){
3389
- sqlite3_stmt *pS2;
3390
- char *zSql = 0;
3391
- const char *zSep = "";
3392
- sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3393
- while( sqlite3_step(pS2)==SQLITE_ROW ){
3394
- const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3395
- zSql = sqlite3_mprintf(
3396
- "%z%s"
3397
- "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3398
- " JOIN pragma_table_info(sm.name,%Q) AS pti"
3399
- " WHERE sm.type='table'",
3400
- zSql, zSep, zDb, zDb
3401
- );
3402
- if( zSql==0 ) return SQLITE_NOMEM;
3403
- zSep = " UNION ";
3404
- }
3405
- sqlite3_finalize(pS2);
3406
- sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3407
- sqlite3_free(zSql);
3408
- }
3409
- iCol = 0;
3410
- eNextPhase = COMPLETION_EOF;
3411
- break;
3412
- }
3413
- }
3414
- if( iCol<0 ){
3415
- /* This case is when the phase presets zCurrentRow */
3416
- if( pCur->zCurrentRow==0 ) continue;
3417
- }else{
3418
- if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3419
- /* Extract the next row of content */
3420
- pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3421
- pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3422
- }else{
3423
- /* When all rows are finished, advance to the next phase */
3424
- sqlite3_finalize(pCur->pStmt);
3425
- pCur->pStmt = 0;
3426
- pCur->ePhase = eNextPhase;
3427
- continue;
3428
- }
3429
- }
3430
- if( pCur->nPrefix==0 ) break;
3431
- if( pCur->nPrefix<=pCur->szRow
3432
- && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3433
- ){
3434
- break;
3435
- }
3436
- }
3437
-
3438
- return SQLITE_OK;
3439
-}
3440
-
3441
-/*
3442
-** Return values of columns for the row at which the completion_cursor
3443
-** is currently pointing.
3444
-*/
3445
-static int completionColumn(
3446
- sqlite3_vtab_cursor *cur, /* The cursor */
3447
- sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3448
- int i /* Which column to return */
3449
-){
3450
- completion_cursor *pCur = (completion_cursor*)cur;
3451
- switch( i ){
3452
- case COMPLETION_COLUMN_CANDIDATE: {
3453
- sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3454
- break;
3455
- }
3456
- case COMPLETION_COLUMN_PREFIX: {
3457
- sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3458
- break;
3459
- }
3460
- case COMPLETION_COLUMN_WHOLELINE: {
3461
- sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3462
- break;
3463
- }
3464
- case COMPLETION_COLUMN_PHASE: {
3465
- sqlite3_result_int(ctx, pCur->ePhase);
3466
- break;
3467
- }
3468
- }
3469
- return SQLITE_OK;
3470
-}
3471
-
3472
-/*
3473
-** Return the rowid for the current row. In this implementation, the
3474
-** rowid is the same as the output value.
3475
-*/
3476
-static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3477
- completion_cursor *pCur = (completion_cursor*)cur;
3478
- *pRowid = pCur->iRowid;
3479
- return SQLITE_OK;
3480
-}
3481
-
3482
-/*
3483
-** Return TRUE if the cursor has been moved off of the last
3484
-** row of output.
3485
-*/
3486
-static int completionEof(sqlite3_vtab_cursor *cur){
3487
- completion_cursor *pCur = (completion_cursor*)cur;
3488
- return pCur->ePhase >= COMPLETION_EOF;
3489
-}
3490
-
3491
-/*
3492
-** This method is called to "rewind" the completion_cursor object back
3493
-** to the first row of output. This method is always called at least
3494
-** once prior to any call to completionColumn() or completionRowid() or
3495
-** completionEof().
3496
-*/
3497
-static int completionFilter(
3498
- sqlite3_vtab_cursor *pVtabCursor,
3499
- int idxNum, const char *idxStr,
3500
- int argc, sqlite3_value **argv
3501
-){
3502
- completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3503
- int iArg = 0;
3504
- (void)(idxStr); /* Unused parameter */
3505
- (void)(argc); /* Unused parameter */
3506
- completionCursorReset(pCur);
3507
- if( idxNum & 1 ){
3508
- pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3509
- if( pCur->nPrefix>0 ){
3510
- pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3511
- if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3512
- }
3513
- iArg = 1;
3514
- }
3515
- if( idxNum & 2 ){
3516
- pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3517
- if( pCur->nLine>0 ){
3518
- pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3519
- if( pCur->zLine==0 ) return SQLITE_NOMEM;
3520
- }
3521
- }
3522
- if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3523
- int i = pCur->nLine;
3524
- while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3525
- i--;
3526
- }
3527
- pCur->nPrefix = pCur->nLine - i;
3528
- if( pCur->nPrefix>0 ){
3529
- pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3530
- if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3531
- }
3532
- }
3533
- pCur->iRowid = 0;
3534
- pCur->ePhase = COMPLETION_FIRST_PHASE;
3535
- return completionNext(pVtabCursor);
3536
-}
3537
-
3538
-/*
3539
-** SQLite will invoke this method one or more times while planning a query
3540
-** that uses the completion virtual table. This routine needs to create
3541
-** a query plan for each invocation and compute an estimated cost for that
3542
-** plan.
3543
-**
3544
-** There are two hidden parameters that act as arguments to the table-valued
3545
-** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3546
-** is available and bit 1 is set if "wholeline" is available.
3547
-*/
3548
-static int completionBestIndex(
3549
- sqlite3_vtab *tab,
3550
- sqlite3_index_info *pIdxInfo
3551
-){
3552
- int i; /* Loop over constraints */
3553
- int idxNum = 0; /* The query plan bitmask */
3554
- int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3555
- int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3556
- int nArg = 0; /* Number of arguments that completeFilter() expects */
3557
- const struct sqlite3_index_constraint *pConstraint;
3558
-
3559
- (void)(tab); /* Unused parameter */
3560
- pConstraint = pIdxInfo->aConstraint;
3561
- for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3562
- if( pConstraint->usable==0 ) continue;
3563
- if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3564
- switch( pConstraint->iColumn ){
3565
- case COMPLETION_COLUMN_PREFIX:
3566
- prefixIdx = i;
3567
- idxNum |= 1;
3568
- break;
3569
- case COMPLETION_COLUMN_WHOLELINE:
3570
- wholelineIdx = i;
3571
- idxNum |= 2;
3572
- break;
3573
- }
3574
- }
3575
- if( prefixIdx>=0 ){
3576
- pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3577
- pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3578
- }
3579
- if( wholelineIdx>=0 ){
3580
- pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3581
- pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3582
- }
3583
- pIdxInfo->idxNum = idxNum;
3584
- pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3585
- pIdxInfo->estimatedRows = 500 - 100*nArg;
3586
- return SQLITE_OK;
3587
-}
3588
-
3589
-/*
3590
-** This following structure defines all the methods for the
3591
-** completion virtual table.
3592
-*/
3593
-static sqlite3_module completionModule = {
3594
- 0, /* iVersion */
3595
- 0, /* xCreate */
3596
- completionConnect, /* xConnect */
3597
- completionBestIndex, /* xBestIndex */
3598
- completionDisconnect, /* xDisconnect */
3599
- 0, /* xDestroy */
3600
- completionOpen, /* xOpen - open a cursor */
3601
- completionClose, /* xClose - close a cursor */
3602
- completionFilter, /* xFilter - configure scan constraints */
3603
- completionNext, /* xNext - advance a cursor */
3604
- completionEof, /* xEof - check for end of scan */
3605
- completionColumn, /* xColumn - read data */
3606
- completionRowid, /* xRowid - read data */
3607
- 0, /* xUpdate */
3608
- 0, /* xBegin */
3609
- 0, /* xSync */
3610
- 0, /* xCommit */
3611
- 0, /* xRollback */
3612
- 0, /* xFindMethod */
3613
- 0, /* xRename */
3614
- 0, /* xSavepoint */
3615
- 0, /* xRelease */
3616
- 0, /* xRollbackTo */
3617
- 0 /* xShadowName */
3618
-};
3619
-
3620
-#endif /* SQLITE_OMIT_VIRTUALTABLE */
3621
-
3622
-int sqlite3CompletionVtabInit(sqlite3 *db){
3623
- int rc = SQLITE_OK;
3624
-#ifndef SQLITE_OMIT_VIRTUALTABLE
3625
- rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3626
-#endif
3627
- return rc;
3628
-}
3629
-
3630
-#ifdef _WIN32
3631
-
3632
-#endif
3633
-int sqlite3_completion_init(
3634
- sqlite3 *db,
3635
- char **pzErrMsg,
3636
- const sqlite3_api_routines *pApi
3637
-){
3638
- int rc = SQLITE_OK;
3639
- SQLITE_EXTENSION_INIT2(pApi);
3640
- (void)(pzErrMsg); /* Unused parameter */
3641
-#ifndef SQLITE_OMIT_VIRTUALTABLE
3642
- rc = sqlite3CompletionVtabInit(db);
3643
-#endif
3644
- return rc;
3645
-}
3646
-
3647
-/************************* End ../ext/misc/completion.c ********************/
3648
-/************************* Begin ../ext/misc/appendvfs.c ******************/
3649
-/*
3650
-** 2017-10-20
3651
-**
3652
-** The author disclaims copyright to this source code. In place of
3653
-** a legal notice, here is a blessing:
3654
-**
3655
-** May you do good and not evil.
3656
-** May you find forgiveness for yourself and forgive others.
3657
-** May you share freely, never taking more than you give.
3658
-**
3659
-******************************************************************************
3660
-**
3661
-** This file implements a VFS shim that allows an SQLite database to be
3662
-** appended onto the end of some other file, such as an executable.
3663
-**
3664
-** A special record must appear at the end of the file that identifies the
3665
-** file as an appended database and provides the offset to the first page
3666
-** of the exposed content. (Or, it is the length of the content prefix.)
3667
-** For best performance page 1 should be located at a disk page boundary,
3668
-** though that is not required.
3669
-**
3670
-** When opening a database using this VFS, the connection might treat
3671
-** the file as an ordinary SQLite database, or it might treat it as a
3672
-** database appended onto some other file. The decision is made by
3673
-** applying the following rules in order:
3674
-**
3675
-** (1) An empty file is an ordinary database.
3676
-**
3677
-** (2) If the file ends with the appendvfs trailer string
3678
-** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
3679
-**
3680
-** (3) If the file begins with the standard SQLite prefix string
3681
-** "SQLite format 3", that file is an ordinary database.
3682
-**
3683
-** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3684
-** set, then a new database is appended to the already existing file.
3685
-**
3686
-** (5) Otherwise, SQLITE_CANTOPEN is returned.
3687
-**
3688
-** To avoid unnecessary complications with the PENDING_BYTE, the size of
3689
-** the file containing the database is limited to 1GiB. (1073741824 bytes)
3690
-** This VFS will not read or write past the 1GiB mark. This restriction
3691
-** might be lifted in future versions. For now, if you need a larger
3692
-** database, then keep it in a separate file.
3693
-**
3694
-** If the file being opened is a plain database (not an appended one), then
3695
-** this shim is a pass-through into the default underlying VFS. (rule 3)
3696
-**/
3697
-/* #include "sqlite3ext.h" */
3698
-SQLITE_EXTENSION_INIT1
3699
-#include <string.h>
3700
-#include <assert.h>
3701
-
3702
-/* The append mark at the end of the database is:
3703
-**
3704
-** Start-Of-SQLite3-NNNNNNNN
3705
-** 123456789 123456789 12345
3706
-**
3707
-** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3708
-** the offset to page 1, and also the length of the prefix content.
3709
-*/
3710
-#define APND_MARK_PREFIX "Start-Of-SQLite3-"
3711
-#define APND_MARK_PREFIX_SZ 17
3712
-#define APND_MARK_FOS_SZ 8
3713
-#define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
3714
-
3715
-/*
3716
-** Maximum size of the combined prefix + database + append-mark. This
3717
-** must be less than 0x40000000 to avoid locking issues on Windows.
3718
-*/
3719
-#define APND_MAX_SIZE (0x40000000)
3720
-
3721
-/*
3722
-** Try to align the database to an even multiple of APND_ROUNDUP bytes.
3723
-*/
3724
-#ifndef APND_ROUNDUP
3725
-#define APND_ROUNDUP 4096
3726
-#endif
3727
-#define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1))
3728
-#define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
3729
-
3730
-/*
3731
-** Forward declaration of objects used by this utility
3732
-*/
3733
-typedef struct sqlite3_vfs ApndVfs;
3734
-typedef struct ApndFile ApndFile;
3735
-
3736
-/* Access to a lower-level VFS that (might) implement dynamic loading,
3737
-** access to randomness, etc.
3738
-*/
3739
-#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3740
-#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3741
-
3742
-/* An open appendvfs file
3743
-**
3744
-** An instance of this structure describes the appended database file.
3745
-** A separate sqlite3_file object is always appended. The appended
3746
-** sqlite3_file object (which can be accessed using ORIGFILE()) describes
3747
-** the entire file, including the prefix, the database, and the
3748
-** append-mark.
3749
-**
3750
-** The structure of an AppendVFS database is like this:
3751
-**
3752
-** +-------------+---------+----------+-------------+
3753
-** | prefix-file | padding | database | append-mark |
3754
-** +-------------+---------+----------+-------------+
3755
-** ^ ^
3756
-** | |
3757
-** iPgOne iMark
3758
-**
3759
-**
3760
-** "prefix file" - file onto which the database has been appended.
3761
-** "padding" - zero or more bytes inserted so that "database"
3762
-** starts on an APND_ROUNDUP boundary
3763
-** "database" - The SQLite database file
3764
-** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
3765
-** the offset from the start of prefix-file to the start
3766
-** of "database".
3767
-**
3768
-** The size of the database is iMark - iPgOne.
3769
-**
3770
-** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
3771
-** of iPgOne stored as a big-ending 64-bit integer.
3772
-**
3773
-** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
3774
-** Or, iMark is -1 to indicate that it has not yet been written.
3775
-*/
3776
-struct ApndFile {
3777
- sqlite3_file base; /* Subclass. MUST BE FIRST! */
3778
- sqlite3_int64 iPgOne; /* Offset to the start of the database */
3779
- sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */
3780
- /* Always followed by another sqlite3_file that describes the whole file */
3781
-};
3782
-
3783
-/*
3784
-** Methods for ApndFile
3785
-*/
3786
-static int apndClose(sqlite3_file*);
3787
-static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3788
-static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3789
-static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3790
-static int apndSync(sqlite3_file*, int flags);
3791
-static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3792
-static int apndLock(sqlite3_file*, int);
3793
-static int apndUnlock(sqlite3_file*, int);
3794
-static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3795
-static int apndFileControl(sqlite3_file*, int op, void *pArg);
3796
-static int apndSectorSize(sqlite3_file*);
3797
-static int apndDeviceCharacteristics(sqlite3_file*);
3798
-static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3799
-static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3800
-static void apndShmBarrier(sqlite3_file*);
3801
-static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3802
-static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3803
-static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3804
-
3805
-/*
3806
-** Methods for ApndVfs
3807
-*/
3808
-static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3809
-static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3810
-static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3811
-static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3812
-static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3813
-static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3814
-static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3815
-static void apndDlClose(sqlite3_vfs*, void*);
3816
-static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3817
-static int apndSleep(sqlite3_vfs*, int microseconds);
3818
-static int apndCurrentTime(sqlite3_vfs*, double*);
3819
-static int apndGetLastError(sqlite3_vfs*, int, char *);
3820
-static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3821
-static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3822
-static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3823
-static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3824
-
3825
-static sqlite3_vfs apnd_vfs = {
3826
- 3, /* iVersion (set when registered) */
3827
- 0, /* szOsFile (set when registered) */
3828
- 1024, /* mxPathname */
3829
- 0, /* pNext */
3830
- "apndvfs", /* zName */
3831
- 0, /* pAppData (set when registered) */
3832
- apndOpen, /* xOpen */
3833
- apndDelete, /* xDelete */
3834
- apndAccess, /* xAccess */
3835
- apndFullPathname, /* xFullPathname */
3836
- apndDlOpen, /* xDlOpen */
3837
- apndDlError, /* xDlError */
3838
- apndDlSym, /* xDlSym */
3839
- apndDlClose, /* xDlClose */
3840
- apndRandomness, /* xRandomness */
3841
- apndSleep, /* xSleep */
3842
- apndCurrentTime, /* xCurrentTime */
3843
- apndGetLastError, /* xGetLastError */
3844
- apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3845
- apndSetSystemCall, /* xSetSystemCall */
3846
- apndGetSystemCall, /* xGetSystemCall */
3847
- apndNextSystemCall /* xNextSystemCall */
3848
-};
3849
-
3850
-static const sqlite3_io_methods apnd_io_methods = {
3851
- 3, /* iVersion */
3852
- apndClose, /* xClose */
3853
- apndRead, /* xRead */
3854
- apndWrite, /* xWrite */
3855
- apndTruncate, /* xTruncate */
3856
- apndSync, /* xSync */
3857
- apndFileSize, /* xFileSize */
3858
- apndLock, /* xLock */
3859
- apndUnlock, /* xUnlock */
3860
- apndCheckReservedLock, /* xCheckReservedLock */
3861
- apndFileControl, /* xFileControl */
3862
- apndSectorSize, /* xSectorSize */
3863
- apndDeviceCharacteristics, /* xDeviceCharacteristics */
3864
- apndShmMap, /* xShmMap */
3865
- apndShmLock, /* xShmLock */
3866
- apndShmBarrier, /* xShmBarrier */
3867
- apndShmUnmap, /* xShmUnmap */
3868
- apndFetch, /* xFetch */
3869
- apndUnfetch /* xUnfetch */
3870
-};
3871
-
3872
-/*
3873
-** Close an apnd-file.
3874
-*/
3875
-static int apndClose(sqlite3_file *pFile){
3876
- pFile = ORIGFILE(pFile);
3877
- return pFile->pMethods->xClose(pFile);
3878
-}
3879
-
3880
-/*
3881
-** Read data from an apnd-file.
3882
-*/
3883
-static int apndRead(
3884
- sqlite3_file *pFile,
3885
- void *zBuf,
3886
- int iAmt,
3887
- sqlite_int64 iOfst
3888
-){
3889
- ApndFile *paf = (ApndFile *)pFile;
3890
- pFile = ORIGFILE(pFile);
3891
- return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3892
-}
3893
-
3894
-/*
3895
-** Add the append-mark onto what should become the end of the file.
3896
-* If and only if this succeeds, internal ApndFile.iMark is updated.
3897
-* Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
3898
-*/
3899
-static int apndWriteMark(
3900
- ApndFile *paf,
3901
- sqlite3_file *pFile,
3902
- sqlite_int64 iWriteEnd
3903
-){
3904
- sqlite_int64 iPgOne = paf->iPgOne;
3905
- unsigned char a[APND_MARK_SIZE];
3906
- int i = APND_MARK_FOS_SZ;
3907
- int rc;
3908
- assert(pFile == ORIGFILE(paf));
3909
- memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3910
- while( --i >= 0 ){
3911
- a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
3912
- iPgOne >>= 8;
3913
- }
3914
- iWriteEnd += paf->iPgOne;
3915
- if( SQLITE_OK==(rc = pFile->pMethods->xWrite
3916
- (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
3917
- paf->iMark = iWriteEnd;
3918
- }
3919
- return rc;
3920
-}
3921
-
3922
-/*
3923
-** Write data to an apnd-file.
3924
-*/
3925
-static int apndWrite(
3926
- sqlite3_file *pFile,
3927
- const void *zBuf,
3928
- int iAmt,
3929
- sqlite_int64 iOfst
3930
-){
3931
- ApndFile *paf = (ApndFile *)pFile;
3932
- sqlite_int64 iWriteEnd = iOfst + iAmt;
3933
- if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
3934
- pFile = ORIGFILE(pFile);
3935
- /* If append-mark is absent or will be overwritten, write it. */
3936
- if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
3937
- int rc = apndWriteMark(paf, pFile, iWriteEnd);
3938
- if( SQLITE_OK!=rc ) return rc;
3939
- }
3940
- return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3941
-}
3942
-
3943
-/*
3944
-** Truncate an apnd-file.
3945
-*/
3946
-static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3947
- ApndFile *paf = (ApndFile *)pFile;
3948
- pFile = ORIGFILE(pFile);
3949
- /* The append mark goes out first so truncate failure does not lose it. */
3950
- if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
3951
- /* Truncate underlying file just past append mark */
3952
- return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
3953
-}
3954
-
3955
-/*
3956
-** Sync an apnd-file.
3957
-*/
3958
-static int apndSync(sqlite3_file *pFile, int flags){
3959
- pFile = ORIGFILE(pFile);
3960
- return pFile->pMethods->xSync(pFile, flags);
3961
-}
3962
-
3963
-/*
3964
-** Return the current file-size of an apnd-file.
3965
-** If the append mark is not yet there, the file-size is 0.
3966
-*/
3967
-static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3968
- ApndFile *paf = (ApndFile *)pFile;
3969
- *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
3970
- return SQLITE_OK;
3971
-}
3972
-
3973
-/*
3974
-** Lock an apnd-file.
3975
-*/
3976
-static int apndLock(sqlite3_file *pFile, int eLock){
3977
- pFile = ORIGFILE(pFile);
3978
- return pFile->pMethods->xLock(pFile, eLock);
3979
-}
3980
-
3981
-/*
3982
-** Unlock an apnd-file.
3983
-*/
3984
-static int apndUnlock(sqlite3_file *pFile, int eLock){
3985
- pFile = ORIGFILE(pFile);
3986
- return pFile->pMethods->xUnlock(pFile, eLock);
3987
-}
3988
-
3989
-/*
3990
-** Check if another file-handle holds a RESERVED lock on an apnd-file.
3991
-*/
3992
-static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3993
- pFile = ORIGFILE(pFile);
3994
- return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3995
-}
3996
-
3997
-/*
3998
-** File control method. For custom operations on an apnd-file.
3999
-*/
4000
-static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
4001
- ApndFile *paf = (ApndFile *)pFile;
4002
- int rc;
4003
- pFile = ORIGFILE(pFile);
4004
- if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
4005
- rc = pFile->pMethods->xFileControl(pFile, op, pArg);
4006
- if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
4007
- *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
4008
- }
4009
- return rc;
4010
-}
4011
-
4012
-/*
4013
-** Return the sector-size in bytes for an apnd-file.
4014
-*/
4015
-static int apndSectorSize(sqlite3_file *pFile){
4016
- pFile = ORIGFILE(pFile);
4017
- return pFile->pMethods->xSectorSize(pFile);
4018
-}
4019
-
4020
-/*
4021
-** Return the device characteristic flags supported by an apnd-file.
4022
-*/
4023
-static int apndDeviceCharacteristics(sqlite3_file *pFile){
4024
- pFile = ORIGFILE(pFile);
4025
- return pFile->pMethods->xDeviceCharacteristics(pFile);
4026
-}
4027
-
4028
-/* Create a shared memory file mapping */
4029
-static int apndShmMap(
4030
- sqlite3_file *pFile,
4031
- int iPg,
4032
- int pgsz,
4033
- int bExtend,
4034
- void volatile **pp
4035
-){
4036
- pFile = ORIGFILE(pFile);
4037
- return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
4038
-}
4039
-
4040
-/* Perform locking on a shared-memory segment */
4041
-static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
4042
- pFile = ORIGFILE(pFile);
4043
- return pFile->pMethods->xShmLock(pFile,offset,n,flags);
4044
-}
4045
-
4046
-/* Memory barrier operation on shared memory */
4047
-static void apndShmBarrier(sqlite3_file *pFile){
4048
- pFile = ORIGFILE(pFile);
4049
- pFile->pMethods->xShmBarrier(pFile);
4050
-}
4051
-
4052
-/* Unmap a shared memory segment */
4053
-static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
4054
- pFile = ORIGFILE(pFile);
4055
- return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
4056
-}
4057
-
4058
-/* Fetch a page of a memory-mapped file */
4059
-static int apndFetch(
4060
- sqlite3_file *pFile,
4061
- sqlite3_int64 iOfst,
4062
- int iAmt,
4063
- void **pp
4064
-){
4065
- ApndFile *p = (ApndFile *)pFile;
4066
- if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
4067
- return SQLITE_IOERR; /* Cannot read what is not yet there. */
4068
- }
4069
- pFile = ORIGFILE(pFile);
4070
- return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4071
-}
4072
-
4073
-/* Release a memory-mapped page */
4074
-static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4075
- ApndFile *p = (ApndFile *)pFile;
4076
- pFile = ORIGFILE(pFile);
4077
- return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4078
-}
4079
-
4080
-/*
4081
-** Try to read the append-mark off the end of a file. Return the
4082
-** start of the appended database if the append-mark is present.
4083
-** If there is no valid append-mark, return -1;
4084
-**
4085
-** An append-mark is only valid if the NNNNNNNN start-of-database offset
4086
-** indicates that the appended database contains at least one page. The
4087
-** start-of-database value must be a multiple of 512.
4088
-*/
4089
-static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4090
- int rc, i;
4091
- sqlite3_int64 iMark;
4092
- int msbs = 8 * (APND_MARK_FOS_SZ-1);
4093
- unsigned char a[APND_MARK_SIZE];
4094
-
4095
- if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
4096
- rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4097
- if( rc ) return -1;
4098
- if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4099
- iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
4100
- for(i=1; i<8; i++){
4101
- msbs -= 8;
4102
- iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
4103
- }
4104
- if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
4105
- if( iMark & 0x1ff ) return -1;
4106
- return iMark;
4107
-}
4108
-
4109
-static const char apvfsSqliteHdr[] = "SQLite format 3";
4110
-/*
4111
-** Check to see if the file is an appendvfs SQLite database file.
4112
-** Return true iff it is such. Parameter sz is the file's size.
4113
-*/
4114
-static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
4115
- int rc;
4116
- char zHdr[16];
4117
- sqlite3_int64 iMark = apndReadMark(sz, pFile);
4118
- if( iMark>=0 ){
4119
- /* If file has the correct end-marker, the expected odd size, and the
4120
- ** SQLite DB type marker where the end-marker puts it, then it
4121
- ** is an appendvfs database.
4122
- */
4123
- rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
4124
- if( SQLITE_OK==rc
4125
- && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
4126
- && (sz & 0x1ff) == APND_MARK_SIZE
4127
- && sz>=512+APND_MARK_SIZE
4128
- ){
4129
- return 1; /* It's an appendvfs database */
4130
- }
4131
- }
4132
- return 0;
4133
-}
4134
-
4135
-/*
4136
-** Check to see if the file is an ordinary SQLite database file.
4137
-** Return true iff so. Parameter sz is the file's size.
4138
-*/
4139
-static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4140
- char zHdr[16];
4141
- if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
4142
- || (sz & 0x1ff) != 0
4143
- || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
4144
- || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
4145
- ){
4146
- return 0;
4147
- }else{
4148
- return 1;
4149
- }
4150
-}
4151
-
4152
-/*
4153
-** Open an apnd file handle.
4154
-*/
4155
-static int apndOpen(
4156
- sqlite3_vfs *pApndVfs,
4157
- const char *zName,
4158
- sqlite3_file *pFile,
4159
- int flags,
4160
- int *pOutFlags
4161
-){
4162
- ApndFile *pApndFile = (ApndFile*)pFile;
4163
- sqlite3_file *pBaseFile = ORIGFILE(pFile);
4164
- sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
4165
- int rc;
4166
- sqlite3_int64 sz = 0;
4167
- if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4168
- /* The appendvfs is not to be used for transient or temporary databases.
4169
- ** Just use the base VFS open to initialize the given file object and
4170
- ** open the underlying file. (Appendvfs is then unused for this file.)
4171
- */
4172
- return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
4173
- }
4174
- memset(pApndFile, 0, sizeof(ApndFile));
4175
- pFile->pMethods = &apnd_io_methods;
4176
- pApndFile->iMark = -1; /* Append mark not yet written */
4177
-
4178
- rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
4179
- if( rc==SQLITE_OK ){
4180
- rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
4181
- if( rc ){
4182
- pBaseFile->pMethods->xClose(pBaseFile);
4183
- }
4184
- }
4185
- if( rc ){
4186
- pFile->pMethods = 0;
4187
- return rc;
4188
- }
4189
- if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
4190
- /* The file being opened appears to be just an ordinary DB. Copy
4191
- ** the base dispatch-table so this instance mimics the base VFS.
4192
- */
4193
- memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
4194
- return SQLITE_OK;
4195
- }
4196
- pApndFile->iPgOne = apndReadMark(sz, pFile);
4197
- if( pApndFile->iPgOne>=0 ){
4198
- pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
4199
- return SQLITE_OK;
4200
- }
4201
- if( (flags & SQLITE_OPEN_CREATE)==0 ){
4202
- pBaseFile->pMethods->xClose(pBaseFile);
4203
- rc = SQLITE_CANTOPEN;
4204
- pFile->pMethods = 0;
4205
- }else{
4206
- /* Round newly added appendvfs location to #define'd page boundary.
4207
- ** Note that nothing has yet been written to the underlying file.
4208
- ** The append mark will be written along with first content write.
4209
- ** Until then, paf->iMark value indicates it is not yet written.
4210
- */
4211
- pApndFile->iPgOne = APND_START_ROUNDUP(sz);
4212
- }
4213
- return rc;
4214
-}
4215
-
4216
-/*
4217
-** Delete an apnd file.
4218
-** For an appendvfs, this could mean delete the appendvfs portion,
4219
-** leaving the appendee as it was before it gained an appendvfs.
4220
-** For now, this code deletes the underlying file too.
4221
-*/
4222
-static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4223
- return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4224
-}
4225
-
4226
-/*
4227
-** All other VFS methods are pass-thrus.
4228
-*/
4229
-static int apndAccess(
4230
- sqlite3_vfs *pVfs,
4231
- const char *zPath,
4232
- int flags,
4233
- int *pResOut
4234
-){
4235
- return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4236
-}
4237
-static int apndFullPathname(
4238
- sqlite3_vfs *pVfs,
4239
- const char *zPath,
4240
- int nOut,
4241
- char *zOut
4242
-){
4243
- return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4244
-}
4245
-static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4246
- return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4247
-}
4248
-static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4249
- ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4250
-}
4251
-static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4252
- return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4253
-}
4254
-static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4255
- ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4256
-}
4257
-static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4258
- return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4259
-}
4260
-static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4261
- return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4262
-}
4263
-static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4264
- return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4265
-}
4266
-static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4267
- return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4268
-}
4269
-static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4270
- return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4271
-}
4272
-static int apndSetSystemCall(
4273
- sqlite3_vfs *pVfs,
4274
- const char *zName,
4275
- sqlite3_syscall_ptr pCall
4276
-){
4277
- return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4278
-}
4279
-static sqlite3_syscall_ptr apndGetSystemCall(
4280
- sqlite3_vfs *pVfs,
4281
- const char *zName
4282
-){
4283
- return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4284
-}
4285
-static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4286
- return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4287
-}
4288
-
4289
-
4290
-#ifdef _WIN32
4291
-
4292
-#endif
4293
-/*
4294
-** This routine is called when the extension is loaded.
4295
-** Register the new VFS.
4296
-*/
4297
-int sqlite3_appendvfs_init(
4298
- sqlite3 *db,
4299
- char **pzErrMsg,
4300
- const sqlite3_api_routines *pApi
4301
-){
4302
- int rc = SQLITE_OK;
4303
- sqlite3_vfs *pOrig;
4304
- SQLITE_EXTENSION_INIT2(pApi);
4305
- (void)pzErrMsg;
4306
- (void)db;
4307
- pOrig = sqlite3_vfs_find(0);
4308
- if( pOrig==0 ) return SQLITE_ERROR;
4309
- apnd_vfs.iVersion = pOrig->iVersion;
4310
- apnd_vfs.pAppData = pOrig;
4311
- apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4312
- rc = sqlite3_vfs_register(&apnd_vfs, 0);
4313
-#ifdef APPENDVFS_TEST
4314
- if( rc==SQLITE_OK ){
4315
- rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4316
- }
4317
-#endif
4318
- if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4319
- return rc;
4320
-}
4321
-
4322
-/************************* End ../ext/misc/appendvfs.c ********************/
4323
-/************************* Begin ../ext/misc/memtrace.c ******************/
4324
-/*
4325
-** 2019-01-21
4326
-**
4327
-** The author disclaims copyright to this source code. In place of
4328
-** a legal notice, here is a blessing:
4329
-**
4330
-** May you do good and not evil.
4331
-** May you find forgiveness for yourself and forgive others.
4332
-** May you share freely, never taking more than you give.
4333
-**
4334
-*************************************************************************
4335
-**
4336
-** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4337
-** mechanism to add a tracing layer on top of SQLite. If this extension
4338
-** is registered prior to sqlite3_initialize(), it will cause all memory
4339
-** allocation activities to be logged on standard output, or to some other
4340
-** FILE specified by the initializer.
4341
-**
4342
-** This file needs to be compiled into the application that uses it.
4343
-**
4344
-** This extension is used to implement the --memtrace option of the
4345
-** command-line shell.
4346
-*/
4347
-#include <assert.h>
4348
-#include <string.h>
4349
-#include <stdio.h>
4350
-
4351
-/* The original memory allocation routines */
4352
-static sqlite3_mem_methods memtraceBase;
4353
-static FILE *memtraceOut;
4354
-
4355
-/* Methods that trace memory allocations */
4356
-static void *memtraceMalloc(int n){
4357
- if( memtraceOut ){
4358
- fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4359
- memtraceBase.xRoundup(n));
4360
- }
4361
- return memtraceBase.xMalloc(n);
4362
-}
4363
-static void memtraceFree(void *p){
4364
- if( p==0 ) return;
4365
- if( memtraceOut ){
4366
- fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4367
- }
4368
- memtraceBase.xFree(p);
4369
-}
4370
-static void *memtraceRealloc(void *p, int n){
4371
- if( p==0 ) return memtraceMalloc(n);
4372
- if( n==0 ){
4373
- memtraceFree(p);
4374
- return 0;
4375
- }
4376
- if( memtraceOut ){
4377
- fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4378
- memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4379
- }
4380
- return memtraceBase.xRealloc(p, n);
4381
-}
4382
-static int memtraceSize(void *p){
4383
- return memtraceBase.xSize(p);
4384
-}
4385
-static int memtraceRoundup(int n){
4386
- return memtraceBase.xRoundup(n);
4387
-}
4388
-static int memtraceInit(void *p){
4389
- return memtraceBase.xInit(p);
4390
-}
4391
-static void memtraceShutdown(void *p){
4392
- memtraceBase.xShutdown(p);
4393
-}
4394
-
4395
-/* The substitute memory allocator */
4396
-static sqlite3_mem_methods ersaztMethods = {
4397
- memtraceMalloc,
4398
- memtraceFree,
4399
- memtraceRealloc,
4400
- memtraceSize,
4401
- memtraceRoundup,
4402
- memtraceInit,
4403
- memtraceShutdown,
4404
- 0
4405
-};
4406
-
4407
-/* Begin tracing memory allocations to out. */
4408
-int sqlite3MemTraceActivate(FILE *out){
4409
- int rc = SQLITE_OK;
4410
- if( memtraceBase.xMalloc==0 ){
4411
- rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4412
- if( rc==SQLITE_OK ){
4413
- rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4414
- }
4415
- }
4416
- memtraceOut = out;
4417
- return rc;
4418
-}
4419
-
4420
-/* Deactivate memory tracing */
4421
-int sqlite3MemTraceDeactivate(void){
4422
- int rc = SQLITE_OK;
4423
- if( memtraceBase.xMalloc!=0 ){
4424
- rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4425
- if( rc==SQLITE_OK ){
4426
- memset(&memtraceBase, 0, sizeof(memtraceBase));
4427
- }
4428
- }
4429
- memtraceOut = 0;
4430
- return rc;
4431
-}
4432
-
4433
-/************************* End ../ext/misc/memtrace.c ********************/
44342233
/************************* Begin ../ext/misc/uint.c ******************/
44352234
/*
44362235
** 2020-04-14
44372236
**
44382237
** The author disclaims copyright to this source code. In place of
@@ -6696,10 +4495,2224 @@
66964495
}
66974496
return rc;
66984497
}
66994498
67004499
/************************* End ../ext/misc/regexp.c ********************/
4500
+#ifndef SQLITE_SHELL_WASM_MODE
4501
+/************************* Begin ../ext/misc/fileio.c ******************/
4502
+/*
4503
+** 2014-06-13
4504
+**
4505
+** The author disclaims copyright to this source code. In place of
4506
+** a legal notice, here is a blessing:
4507
+**
4508
+** May you do good and not evil.
4509
+** May you find forgiveness for yourself and forgive others.
4510
+** May you share freely, never taking more than you give.
4511
+**
4512
+******************************************************************************
4513
+**
4514
+** This SQLite extension implements SQL functions readfile() and
4515
+** writefile(), and eponymous virtual type "fsdir".
4516
+**
4517
+** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
4518
+**
4519
+** If neither of the optional arguments is present, then this UDF
4520
+** function writes blob DATA to file FILE. If successful, the number
4521
+** of bytes written is returned. If an error occurs, NULL is returned.
4522
+**
4523
+** If the first option argument - MODE - is present, then it must
4524
+** be passed an integer value that corresponds to a POSIX mode
4525
+** value (file type + permissions, as returned in the stat.st_mode
4526
+** field by the stat() system call). Three types of files may
4527
+** be written/created:
4528
+**
4529
+** regular files: (mode & 0170000)==0100000
4530
+** symbolic links: (mode & 0170000)==0120000
4531
+** directories: (mode & 0170000)==0040000
4532
+**
4533
+** For a directory, the DATA is ignored. For a symbolic link, it is
4534
+** interpreted as text and used as the target of the link. For a
4535
+** regular file, it is interpreted as a blob and written into the
4536
+** named file. Regardless of the type of file, its permissions are
4537
+** set to (mode & 0777) before returning.
4538
+**
4539
+** If the optional MTIME argument is present, then it is interpreted
4540
+** as an integer - the number of seconds since the unix epoch. The
4541
+** modification-time of the target file is set to this value before
4542
+** returning.
4543
+**
4544
+** If three or more arguments are passed to this function and an
4545
+** error is encountered, an exception is raised.
4546
+**
4547
+** READFILE(FILE):
4548
+**
4549
+** Read and return the contents of file FILE (type blob) from disk.
4550
+**
4551
+** FSDIR:
4552
+**
4553
+** Used as follows:
4554
+**
4555
+** SELECT * FROM fsdir($path [, $dir]);
4556
+**
4557
+** Parameter $path is an absolute or relative pathname. If the file that it
4558
+** refers to does not exist, it is an error. If the path refers to a regular
4559
+** file or symbolic link, it returns a single row. Or, if the path refers
4560
+** to a directory, it returns one row for the directory, and one row for each
4561
+** file within the hierarchy rooted at $path.
4562
+**
4563
+** Each row has the following columns:
4564
+**
4565
+** name: Path to file or directory (text value).
4566
+** mode: Value of stat.st_mode for directory entry (an integer).
4567
+** mtime: Value of stat.st_mtime for directory entry (an integer).
4568
+** data: For a regular file, a blob containing the file data. For a
4569
+** symlink, a text value containing the text of the link. For a
4570
+** directory, NULL.
4571
+**
4572
+** If a non-NULL value is specified for the optional $dir parameter and
4573
+** $path is a relative path, then $path is interpreted relative to $dir.
4574
+** And the paths returned in the "name" column of the table are also
4575
+** relative to directory $dir.
4576
+**
4577
+** Notes on building this extension for Windows:
4578
+** Unless linked statically with the SQLite library, a preprocessor
4579
+** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
4580
+** DLL form of this extension for WIN32. See its use below for details.
4581
+*/
4582
+/* #include "sqlite3ext.h" */
4583
+SQLITE_EXTENSION_INIT1
4584
+#include <stdio.h>
4585
+#include <string.h>
4586
+#include <assert.h>
4587
+
4588
+#include <sys/types.h>
4589
+#include <sys/stat.h>
4590
+#include <fcntl.h>
4591
+#if !defined(_WIN32) && !defined(WIN32)
4592
+# include <unistd.h>
4593
+# include <dirent.h>
4594
+# include <utime.h>
4595
+# include <sys/time.h>
4596
+#else
4597
+# include "windows.h"
4598
+# include <io.h>
4599
+# include <direct.h>
4600
+/* # include "test_windirent.h" */
4601
+# define dirent DIRENT
4602
+# ifndef chmod
4603
+# define chmod _chmod
4604
+# endif
4605
+# ifndef stat
4606
+# define stat _stat
4607
+# endif
4608
+# define mkdir(path,mode) _mkdir(path)
4609
+# define lstat(path,buf) stat(path,buf)
4610
+#endif
4611
+#include <time.h>
4612
+#include <errno.h>
4613
+
4614
+
4615
+/*
4616
+** Structure of the fsdir() table-valued function
4617
+*/
4618
+ /* 0 1 2 3 4 5 */
4619
+#define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
4620
+#define FSDIR_COLUMN_NAME 0 /* Name of the file */
4621
+#define FSDIR_COLUMN_MODE 1 /* Access mode */
4622
+#define FSDIR_COLUMN_MTIME 2 /* Last modification time */
4623
+#define FSDIR_COLUMN_DATA 3 /* File content */
4624
+#define FSDIR_COLUMN_PATH 4 /* Path to top of search */
4625
+#define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
4626
+
4627
+
4628
+/*
4629
+** Set the result stored by context ctx to a blob containing the
4630
+** contents of file zName. Or, leave the result unchanged (NULL)
4631
+** if the file does not exist or is unreadable.
4632
+**
4633
+** If the file exceeds the SQLite blob size limit, through an
4634
+** SQLITE_TOOBIG error.
4635
+**
4636
+** Throw an SQLITE_IOERR if there are difficulties pulling the file
4637
+** off of disk.
4638
+*/
4639
+static void readFileContents(sqlite3_context *ctx, const char *zName){
4640
+ FILE *in;
4641
+ sqlite3_int64 nIn;
4642
+ void *pBuf;
4643
+ sqlite3 *db;
4644
+ int mxBlob;
4645
+
4646
+ in = fopen(zName, "rb");
4647
+ if( in==0 ){
4648
+ /* File does not exist or is unreadable. Leave the result set to NULL. */
4649
+ return;
4650
+ }
4651
+ fseek(in, 0, SEEK_END);
4652
+ nIn = ftell(in);
4653
+ rewind(in);
4654
+ db = sqlite3_context_db_handle(ctx);
4655
+ mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
4656
+ if( nIn>mxBlob ){
4657
+ sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
4658
+ fclose(in);
4659
+ return;
4660
+ }
4661
+ pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
4662
+ if( pBuf==0 ){
4663
+ sqlite3_result_error_nomem(ctx);
4664
+ fclose(in);
4665
+ return;
4666
+ }
4667
+ if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
4668
+ sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
4669
+ }else{
4670
+ sqlite3_result_error_code(ctx, SQLITE_IOERR);
4671
+ sqlite3_free(pBuf);
4672
+ }
4673
+ fclose(in);
4674
+}
4675
+
4676
+/*
4677
+** Implementation of the "readfile(X)" SQL function. The entire content
4678
+** of the file named X is read and returned as a BLOB. NULL is returned
4679
+** if the file does not exist or is unreadable.
4680
+*/
4681
+static void readfileFunc(
4682
+ sqlite3_context *context,
4683
+ int argc,
4684
+ sqlite3_value **argv
4685
+){
4686
+ const char *zName;
4687
+ (void)(argc); /* Unused parameter */
4688
+ zName = (const char*)sqlite3_value_text(argv[0]);
4689
+ if( zName==0 ) return;
4690
+ readFileContents(context, zName);
4691
+}
4692
+
4693
+/*
4694
+** Set the error message contained in context ctx to the results of
4695
+** vprintf(zFmt, ...).
4696
+*/
4697
+static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4698
+ char *zMsg = 0;
4699
+ va_list ap;
4700
+ va_start(ap, zFmt);
4701
+ zMsg = sqlite3_vmprintf(zFmt, ap);
4702
+ sqlite3_result_error(ctx, zMsg, -1);
4703
+ sqlite3_free(zMsg);
4704
+ va_end(ap);
4705
+}
4706
+
4707
+#if defined(_WIN32)
4708
+/*
4709
+** This function is designed to convert a Win32 FILETIME structure into the
4710
+** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
4711
+*/
4712
+static sqlite3_uint64 fileTimeToUnixTime(
4713
+ LPFILETIME pFileTime
4714
+){
4715
+ SYSTEMTIME epochSystemTime;
4716
+ ULARGE_INTEGER epochIntervals;
4717
+ FILETIME epochFileTime;
4718
+ ULARGE_INTEGER fileIntervals;
4719
+
4720
+ memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
4721
+ epochSystemTime.wYear = 1970;
4722
+ epochSystemTime.wMonth = 1;
4723
+ epochSystemTime.wDay = 1;
4724
+ SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
4725
+ epochIntervals.LowPart = epochFileTime.dwLowDateTime;
4726
+ epochIntervals.HighPart = epochFileTime.dwHighDateTime;
4727
+
4728
+ fileIntervals.LowPart = pFileTime->dwLowDateTime;
4729
+ fileIntervals.HighPart = pFileTime->dwHighDateTime;
4730
+
4731
+ return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
4732
+}
4733
+
4734
+
4735
+#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
4736
+# /* To allow a standalone DLL, use this next replacement function: */
4737
+# undef sqlite3_win32_utf8_to_unicode
4738
+# define sqlite3_win32_utf8_to_unicode utf8_to_utf16
4739
+#
4740
+LPWSTR utf8_to_utf16(const char *z){
4741
+ int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0);
4742
+ LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR));
4743
+ if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) )
4744
+ return rv;
4745
+ sqlite3_free(rv);
4746
+ return 0;
4747
+}
4748
+#endif
4749
+
4750
+/*
4751
+** This function attempts to normalize the time values found in the stat()
4752
+** buffer to UTC. This is necessary on Win32, where the runtime library
4753
+** appears to return these values as local times.
4754
+*/
4755
+static void statTimesToUtc(
4756
+ const char *zPath,
4757
+ struct stat *pStatBuf
4758
+){
4759
+ HANDLE hFindFile;
4760
+ WIN32_FIND_DATAW fd;
4761
+ LPWSTR zUnicodeName;
4762
+ extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
4763
+ zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
4764
+ if( zUnicodeName ){
4765
+ memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
4766
+ hFindFile = FindFirstFileW(zUnicodeName, &fd);
4767
+ if( hFindFile!=NULL ){
4768
+ pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
4769
+ pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
4770
+ pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
4771
+ FindClose(hFindFile);
4772
+ }
4773
+ sqlite3_free(zUnicodeName);
4774
+ }
4775
+}
4776
+#endif
4777
+
4778
+/*
4779
+** This function is used in place of stat(). On Windows, special handling
4780
+** is required in order for the included time to be returned as UTC. On all
4781
+** other systems, this function simply calls stat().
4782
+*/
4783
+static int fileStat(
4784
+ const char *zPath,
4785
+ struct stat *pStatBuf
4786
+){
4787
+#if defined(_WIN32)
4788
+ int rc = stat(zPath, pStatBuf);
4789
+ if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
4790
+ return rc;
4791
+#else
4792
+ return stat(zPath, pStatBuf);
4793
+#endif
4794
+}
4795
+
4796
+/*
4797
+** This function is used in place of lstat(). On Windows, special handling
4798
+** is required in order for the included time to be returned as UTC. On all
4799
+** other systems, this function simply calls lstat().
4800
+*/
4801
+static int fileLinkStat(
4802
+ const char *zPath,
4803
+ struct stat *pStatBuf
4804
+){
4805
+#if defined(_WIN32)
4806
+ int rc = lstat(zPath, pStatBuf);
4807
+ if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
4808
+ return rc;
4809
+#else
4810
+ return lstat(zPath, pStatBuf);
4811
+#endif
4812
+}
4813
+
4814
+/*
4815
+** Argument zFile is the name of a file that will be created and/or written
4816
+** by SQL function writefile(). This function ensures that the directory
4817
+** zFile will be written to exists, creating it if required. The permissions
4818
+** for any path components created by this function are set in accordance
4819
+** with the current umask.
4820
+**
4821
+** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
4822
+** SQLITE_OK is returned if the directory is successfully created, or
4823
+** SQLITE_ERROR otherwise.
4824
+*/
4825
+static int makeDirectory(
4826
+ const char *zFile
4827
+){
4828
+ char *zCopy = sqlite3_mprintf("%s", zFile);
4829
+ int rc = SQLITE_OK;
4830
+
4831
+ if( zCopy==0 ){
4832
+ rc = SQLITE_NOMEM;
4833
+ }else{
4834
+ int nCopy = (int)strlen(zCopy);
4835
+ int i = 1;
4836
+
4837
+ while( rc==SQLITE_OK ){
4838
+ struct stat sStat;
4839
+ int rc2;
4840
+
4841
+ for(; zCopy[i]!='/' && i<nCopy; i++);
4842
+ if( i==nCopy ) break;
4843
+ zCopy[i] = '\0';
4844
+
4845
+ rc2 = fileStat(zCopy, &sStat);
4846
+ if( rc2!=0 ){
4847
+ if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
4848
+ }else{
4849
+ if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
4850
+ }
4851
+ zCopy[i] = '/';
4852
+ i++;
4853
+ }
4854
+
4855
+ sqlite3_free(zCopy);
4856
+ }
4857
+
4858
+ return rc;
4859
+}
4860
+
4861
+/*
4862
+** This function does the work for the writefile() UDF. Refer to
4863
+** header comments at the top of this file for details.
4864
+*/
4865
+static int writeFile(
4866
+ sqlite3_context *pCtx, /* Context to return bytes written in */
4867
+ const char *zFile, /* File to write */
4868
+ sqlite3_value *pData, /* Data to write */
4869
+ mode_t mode, /* MODE parameter passed to writefile() */
4870
+ sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
4871
+){
4872
+ if( zFile==0 ) return 1;
4873
+#if !defined(_WIN32) && !defined(WIN32)
4874
+ if( S_ISLNK(mode) ){
4875
+ const char *zTo = (const char*)sqlite3_value_text(pData);
4876
+ if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
4877
+ }else
4878
+#endif
4879
+ {
4880
+ if( S_ISDIR(mode) ){
4881
+ if( mkdir(zFile, mode) ){
4882
+ /* The mkdir() call to create the directory failed. This might not
4883
+ ** be an error though - if there is already a directory at the same
4884
+ ** path and either the permissions already match or can be changed
4885
+ ** to do so using chmod(), it is not an error. */
4886
+ struct stat sStat;
4887
+ if( errno!=EEXIST
4888
+ || 0!=fileStat(zFile, &sStat)
4889
+ || !S_ISDIR(sStat.st_mode)
4890
+ || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
4891
+ ){
4892
+ return 1;
4893
+ }
4894
+ }
4895
+ }else{
4896
+ sqlite3_int64 nWrite = 0;
4897
+ const char *z;
4898
+ int rc = 0;
4899
+ FILE *out = fopen(zFile, "wb");
4900
+ if( out==0 ) return 1;
4901
+ z = (const char*)sqlite3_value_blob(pData);
4902
+ if( z ){
4903
+ sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
4904
+ nWrite = sqlite3_value_bytes(pData);
4905
+ if( nWrite!=n ){
4906
+ rc = 1;
4907
+ }
4908
+ }
4909
+ fclose(out);
4910
+ if( rc==0 && mode && chmod(zFile, mode & 0777) ){
4911
+ rc = 1;
4912
+ }
4913
+ if( rc ) return 2;
4914
+ sqlite3_result_int64(pCtx, nWrite);
4915
+ }
4916
+ }
4917
+
4918
+ if( mtime>=0 ){
4919
+#if defined(_WIN32)
4920
+#if !SQLITE_OS_WINRT
4921
+ /* Windows */
4922
+ FILETIME lastAccess;
4923
+ FILETIME lastWrite;
4924
+ SYSTEMTIME currentTime;
4925
+ LONGLONG intervals;
4926
+ HANDLE hFile;
4927
+ LPWSTR zUnicodeName;
4928
+ extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
4929
+
4930
+ GetSystemTime(&currentTime);
4931
+ SystemTimeToFileTime(&currentTime, &lastAccess);
4932
+ intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
4933
+ lastWrite.dwLowDateTime = (DWORD)intervals;
4934
+ lastWrite.dwHighDateTime = intervals >> 32;
4935
+ zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
4936
+ if( zUnicodeName==0 ){
4937
+ return 1;
4938
+ }
4939
+ hFile = CreateFileW(
4940
+ zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
4941
+ FILE_FLAG_BACKUP_SEMANTICS, NULL
4942
+ );
4943
+ sqlite3_free(zUnicodeName);
4944
+ if( hFile!=INVALID_HANDLE_VALUE ){
4945
+ BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
4946
+ CloseHandle(hFile);
4947
+ return !bResult;
4948
+ }else{
4949
+ return 1;
4950
+ }
4951
+#endif
4952
+#elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
4953
+ /* Recent unix */
4954
+ struct timespec times[2];
4955
+ times[0].tv_nsec = times[1].tv_nsec = 0;
4956
+ times[0].tv_sec = time(0);
4957
+ times[1].tv_sec = mtime;
4958
+ if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
4959
+ return 1;
4960
+ }
4961
+#else
4962
+ /* Legacy unix */
4963
+ struct timeval times[2];
4964
+ times[0].tv_usec = times[1].tv_usec = 0;
4965
+ times[0].tv_sec = time(0);
4966
+ times[1].tv_sec = mtime;
4967
+ if( utimes(zFile, times) ){
4968
+ return 1;
4969
+ }
4970
+#endif
4971
+ }
4972
+
4973
+ return 0;
4974
+}
4975
+
4976
+/*
4977
+** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
4978
+** Refer to header comments at the top of this file for details.
4979
+*/
4980
+static void writefileFunc(
4981
+ sqlite3_context *context,
4982
+ int argc,
4983
+ sqlite3_value **argv
4984
+){
4985
+ const char *zFile;
4986
+ mode_t mode = 0;
4987
+ int res;
4988
+ sqlite3_int64 mtime = -1;
4989
+
4990
+ if( argc<2 || argc>4 ){
4991
+ sqlite3_result_error(context,
4992
+ "wrong number of arguments to function writefile()", -1
4993
+ );
4994
+ return;
4995
+ }
4996
+
4997
+ zFile = (const char*)sqlite3_value_text(argv[0]);
4998
+ if( zFile==0 ) return;
4999
+ if( argc>=3 ){
5000
+ mode = (mode_t)sqlite3_value_int(argv[2]);
5001
+ }
5002
+ if( argc==4 ){
5003
+ mtime = sqlite3_value_int64(argv[3]);
5004
+ }
5005
+
5006
+ res = writeFile(context, zFile, argv[1], mode, mtime);
5007
+ if( res==1 && errno==ENOENT ){
5008
+ if( makeDirectory(zFile)==SQLITE_OK ){
5009
+ res = writeFile(context, zFile, argv[1], mode, mtime);
5010
+ }
5011
+ }
5012
+
5013
+ if( argc>2 && res!=0 ){
5014
+ if( S_ISLNK(mode) ){
5015
+ ctxErrorMsg(context, "failed to create symlink: %s", zFile);
5016
+ }else if( S_ISDIR(mode) ){
5017
+ ctxErrorMsg(context, "failed to create directory: %s", zFile);
5018
+ }else{
5019
+ ctxErrorMsg(context, "failed to write file: %s", zFile);
5020
+ }
5021
+ }
5022
+}
5023
+
5024
+/*
5025
+** SQL function: lsmode(MODE)
5026
+**
5027
+** Given a numberic st_mode from stat(), convert it into a human-readable
5028
+** text string in the style of "ls -l".
5029
+*/
5030
+static void lsModeFunc(
5031
+ sqlite3_context *context,
5032
+ int argc,
5033
+ sqlite3_value **argv
5034
+){
5035
+ int i;
5036
+ int iMode = sqlite3_value_int(argv[0]);
5037
+ char z[16];
5038
+ (void)argc;
5039
+ if( S_ISLNK(iMode) ){
5040
+ z[0] = 'l';
5041
+ }else if( S_ISREG(iMode) ){
5042
+ z[0] = '-';
5043
+ }else if( S_ISDIR(iMode) ){
5044
+ z[0] = 'd';
5045
+ }else{
5046
+ z[0] = '?';
5047
+ }
5048
+ for(i=0; i<3; i++){
5049
+ int m = (iMode >> ((2-i)*3));
5050
+ char *a = &z[1 + i*3];
5051
+ a[0] = (m & 0x4) ? 'r' : '-';
5052
+ a[1] = (m & 0x2) ? 'w' : '-';
5053
+ a[2] = (m & 0x1) ? 'x' : '-';
5054
+ }
5055
+ z[10] = '\0';
5056
+ sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
5057
+}
5058
+
5059
+#ifndef SQLITE_OMIT_VIRTUALTABLE
5060
+
5061
+/*
5062
+** Cursor type for recursively iterating through a directory structure.
5063
+*/
5064
+typedef struct fsdir_cursor fsdir_cursor;
5065
+typedef struct FsdirLevel FsdirLevel;
5066
+
5067
+struct FsdirLevel {
5068
+ DIR *pDir; /* From opendir() */
5069
+ char *zDir; /* Name of directory (nul-terminated) */
5070
+};
5071
+
5072
+struct fsdir_cursor {
5073
+ sqlite3_vtab_cursor base; /* Base class - must be first */
5074
+
5075
+ int nLvl; /* Number of entries in aLvl[] array */
5076
+ int iLvl; /* Index of current entry */
5077
+ FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
5078
+
5079
+ const char *zBase;
5080
+ int nBase;
5081
+
5082
+ struct stat sStat; /* Current lstat() results */
5083
+ char *zPath; /* Path to current entry */
5084
+ sqlite3_int64 iRowid; /* Current rowid */
5085
+};
5086
+
5087
+typedef struct fsdir_tab fsdir_tab;
5088
+struct fsdir_tab {
5089
+ sqlite3_vtab base; /* Base class - must be first */
5090
+};
5091
+
5092
+/*
5093
+** Construct a new fsdir virtual table object.
5094
+*/
5095
+static int fsdirConnect(
5096
+ sqlite3 *db,
5097
+ void *pAux,
5098
+ int argc, const char *const*argv,
5099
+ sqlite3_vtab **ppVtab,
5100
+ char **pzErr
5101
+){
5102
+ fsdir_tab *pNew = 0;
5103
+ int rc;
5104
+ (void)pAux;
5105
+ (void)argc;
5106
+ (void)argv;
5107
+ (void)pzErr;
5108
+ rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
5109
+ if( rc==SQLITE_OK ){
5110
+ pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
5111
+ if( pNew==0 ) return SQLITE_NOMEM;
5112
+ memset(pNew, 0, sizeof(*pNew));
5113
+ sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
5114
+ }
5115
+ *ppVtab = (sqlite3_vtab*)pNew;
5116
+ return rc;
5117
+}
5118
+
5119
+/*
5120
+** This method is the destructor for fsdir vtab objects.
5121
+*/
5122
+static int fsdirDisconnect(sqlite3_vtab *pVtab){
5123
+ sqlite3_free(pVtab);
5124
+ return SQLITE_OK;
5125
+}
5126
+
5127
+/*
5128
+** Constructor for a new fsdir_cursor object.
5129
+*/
5130
+static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
5131
+ fsdir_cursor *pCur;
5132
+ (void)p;
5133
+ pCur = sqlite3_malloc( sizeof(*pCur) );
5134
+ if( pCur==0 ) return SQLITE_NOMEM;
5135
+ memset(pCur, 0, sizeof(*pCur));
5136
+ pCur->iLvl = -1;
5137
+ *ppCursor = &pCur->base;
5138
+ return SQLITE_OK;
5139
+}
5140
+
5141
+/*
5142
+** Reset a cursor back to the state it was in when first returned
5143
+** by fsdirOpen().
5144
+*/
5145
+static void fsdirResetCursor(fsdir_cursor *pCur){
5146
+ int i;
5147
+ for(i=0; i<=pCur->iLvl; i++){
5148
+ FsdirLevel *pLvl = &pCur->aLvl[i];
5149
+ if( pLvl->pDir ) closedir(pLvl->pDir);
5150
+ sqlite3_free(pLvl->zDir);
5151
+ }
5152
+ sqlite3_free(pCur->zPath);
5153
+ sqlite3_free(pCur->aLvl);
5154
+ pCur->aLvl = 0;
5155
+ pCur->zPath = 0;
5156
+ pCur->zBase = 0;
5157
+ pCur->nBase = 0;
5158
+ pCur->nLvl = 0;
5159
+ pCur->iLvl = -1;
5160
+ pCur->iRowid = 1;
5161
+}
5162
+
5163
+/*
5164
+** Destructor for an fsdir_cursor.
5165
+*/
5166
+static int fsdirClose(sqlite3_vtab_cursor *cur){
5167
+ fsdir_cursor *pCur = (fsdir_cursor*)cur;
5168
+
5169
+ fsdirResetCursor(pCur);
5170
+ sqlite3_free(pCur);
5171
+ return SQLITE_OK;
5172
+}
5173
+
5174
+/*
5175
+** Set the error message for the virtual table associated with cursor
5176
+** pCur to the results of vprintf(zFmt, ...).
5177
+*/
5178
+static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
5179
+ va_list ap;
5180
+ va_start(ap, zFmt);
5181
+ pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
5182
+ va_end(ap);
5183
+}
5184
+
5185
+
5186
+/*
5187
+** Advance an fsdir_cursor to its next row of output.
5188
+*/
5189
+static int fsdirNext(sqlite3_vtab_cursor *cur){
5190
+ fsdir_cursor *pCur = (fsdir_cursor*)cur;
5191
+ mode_t m = pCur->sStat.st_mode;
5192
+
5193
+ pCur->iRowid++;
5194
+ if( S_ISDIR(m) ){
5195
+ /* Descend into this directory */
5196
+ int iNew = pCur->iLvl + 1;
5197
+ FsdirLevel *pLvl;
5198
+ if( iNew>=pCur->nLvl ){
5199
+ int nNew = iNew+1;
5200
+ sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
5201
+ FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
5202
+ if( aNew==0 ) return SQLITE_NOMEM;
5203
+ memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
5204
+ pCur->aLvl = aNew;
5205
+ pCur->nLvl = nNew;
5206
+ }
5207
+ pCur->iLvl = iNew;
5208
+ pLvl = &pCur->aLvl[iNew];
5209
+
5210
+ pLvl->zDir = pCur->zPath;
5211
+ pCur->zPath = 0;
5212
+ pLvl->pDir = opendir(pLvl->zDir);
5213
+ if( pLvl->pDir==0 ){
5214
+ fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
5215
+ return SQLITE_ERROR;
5216
+ }
5217
+ }
5218
+
5219
+ while( pCur->iLvl>=0 ){
5220
+ FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
5221
+ struct dirent *pEntry = readdir(pLvl->pDir);
5222
+ if( pEntry ){
5223
+ if( pEntry->d_name[0]=='.' ){
5224
+ if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
5225
+ if( pEntry->d_name[1]=='\0' ) continue;
5226
+ }
5227
+ sqlite3_free(pCur->zPath);
5228
+ pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
5229
+ if( pCur->zPath==0 ) return SQLITE_NOMEM;
5230
+ if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
5231
+ fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
5232
+ return SQLITE_ERROR;
5233
+ }
5234
+ return SQLITE_OK;
5235
+ }
5236
+ closedir(pLvl->pDir);
5237
+ sqlite3_free(pLvl->zDir);
5238
+ pLvl->pDir = 0;
5239
+ pLvl->zDir = 0;
5240
+ pCur->iLvl--;
5241
+ }
5242
+
5243
+ /* EOF */
5244
+ sqlite3_free(pCur->zPath);
5245
+ pCur->zPath = 0;
5246
+ return SQLITE_OK;
5247
+}
5248
+
5249
+/*
5250
+** Return values of columns for the row at which the series_cursor
5251
+** is currently pointing.
5252
+*/
5253
+static int fsdirColumn(
5254
+ sqlite3_vtab_cursor *cur, /* The cursor */
5255
+ sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5256
+ int i /* Which column to return */
5257
+){
5258
+ fsdir_cursor *pCur = (fsdir_cursor*)cur;
5259
+ switch( i ){
5260
+ case FSDIR_COLUMN_NAME: {
5261
+ sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
5262
+ break;
5263
+ }
5264
+
5265
+ case FSDIR_COLUMN_MODE:
5266
+ sqlite3_result_int64(ctx, pCur->sStat.st_mode);
5267
+ break;
5268
+
5269
+ case FSDIR_COLUMN_MTIME:
5270
+ sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
5271
+ break;
5272
+
5273
+ case FSDIR_COLUMN_DATA: {
5274
+ mode_t m = pCur->sStat.st_mode;
5275
+ if( S_ISDIR(m) ){
5276
+ sqlite3_result_null(ctx);
5277
+#if !defined(_WIN32) && !defined(WIN32)
5278
+ }else if( S_ISLNK(m) ){
5279
+ char aStatic[64];
5280
+ char *aBuf = aStatic;
5281
+ sqlite3_int64 nBuf = 64;
5282
+ int n;
5283
+
5284
+ while( 1 ){
5285
+ n = readlink(pCur->zPath, aBuf, nBuf);
5286
+ if( n<nBuf ) break;
5287
+ if( aBuf!=aStatic ) sqlite3_free(aBuf);
5288
+ nBuf = nBuf*2;
5289
+ aBuf = sqlite3_malloc64(nBuf);
5290
+ if( aBuf==0 ){
5291
+ sqlite3_result_error_nomem(ctx);
5292
+ return SQLITE_NOMEM;
5293
+ }
5294
+ }
5295
+
5296
+ sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
5297
+ if( aBuf!=aStatic ) sqlite3_free(aBuf);
5298
+#endif
5299
+ }else{
5300
+ readFileContents(ctx, pCur->zPath);
5301
+ }
5302
+ }
5303
+ case FSDIR_COLUMN_PATH:
5304
+ default: {
5305
+ /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
5306
+ ** always return their values as NULL */
5307
+ break;
5308
+ }
5309
+ }
5310
+ return SQLITE_OK;
5311
+}
5312
+
5313
+/*
5314
+** Return the rowid for the current row. In this implementation, the
5315
+** first row returned is assigned rowid value 1, and each subsequent
5316
+** row a value 1 more than that of the previous.
5317
+*/
5318
+static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5319
+ fsdir_cursor *pCur = (fsdir_cursor*)cur;
5320
+ *pRowid = pCur->iRowid;
5321
+ return SQLITE_OK;
5322
+}
5323
+
5324
+/*
5325
+** Return TRUE if the cursor has been moved off of the last
5326
+** row of output.
5327
+*/
5328
+static int fsdirEof(sqlite3_vtab_cursor *cur){
5329
+ fsdir_cursor *pCur = (fsdir_cursor*)cur;
5330
+ return (pCur->zPath==0);
5331
+}
5332
+
5333
+/*
5334
+** xFilter callback.
5335
+**
5336
+** idxNum==1 PATH parameter only
5337
+** idxNum==2 Both PATH and DIR supplied
5338
+*/
5339
+static int fsdirFilter(
5340
+ sqlite3_vtab_cursor *cur,
5341
+ int idxNum, const char *idxStr,
5342
+ int argc, sqlite3_value **argv
5343
+){
5344
+ const char *zDir = 0;
5345
+ fsdir_cursor *pCur = (fsdir_cursor*)cur;
5346
+ (void)idxStr;
5347
+ fsdirResetCursor(pCur);
5348
+
5349
+ if( idxNum==0 ){
5350
+ fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
5351
+ return SQLITE_ERROR;
5352
+ }
5353
+
5354
+ assert( argc==idxNum && (argc==1 || argc==2) );
5355
+ zDir = (const char*)sqlite3_value_text(argv[0]);
5356
+ if( zDir==0 ){
5357
+ fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
5358
+ return SQLITE_ERROR;
5359
+ }
5360
+ if( argc==2 ){
5361
+ pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
5362
+ }
5363
+ if( pCur->zBase ){
5364
+ pCur->nBase = (int)strlen(pCur->zBase)+1;
5365
+ pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
5366
+ }else{
5367
+ pCur->zPath = sqlite3_mprintf("%s", zDir);
5368
+ }
5369
+
5370
+ if( pCur->zPath==0 ){
5371
+ return SQLITE_NOMEM;
5372
+ }
5373
+ if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
5374
+ fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
5375
+ return SQLITE_ERROR;
5376
+ }
5377
+
5378
+ return SQLITE_OK;
5379
+}
5380
+
5381
+/*
5382
+** SQLite will invoke this method one or more times while planning a query
5383
+** that uses the generate_series virtual table. This routine needs to create
5384
+** a query plan for each invocation and compute an estimated cost for that
5385
+** plan.
5386
+**
5387
+** In this implementation idxNum is used to represent the
5388
+** query plan. idxStr is unused.
5389
+**
5390
+** The query plan is represented by values of idxNum:
5391
+**
5392
+** (1) The path value is supplied by argv[0]
5393
+** (2) Path is in argv[0] and dir is in argv[1]
5394
+*/
5395
+static int fsdirBestIndex(
5396
+ sqlite3_vtab *tab,
5397
+ sqlite3_index_info *pIdxInfo
5398
+){
5399
+ int i; /* Loop over constraints */
5400
+ int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
5401
+ int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
5402
+ int seenPath = 0; /* True if an unusable PATH= constraint is seen */
5403
+ int seenDir = 0; /* True if an unusable DIR= constraint is seen */
5404
+ const struct sqlite3_index_constraint *pConstraint;
5405
+
5406
+ (void)tab;
5407
+ pConstraint = pIdxInfo->aConstraint;
5408
+ for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5409
+ if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
5410
+ switch( pConstraint->iColumn ){
5411
+ case FSDIR_COLUMN_PATH: {
5412
+ if( pConstraint->usable ){
5413
+ idxPath = i;
5414
+ seenPath = 0;
5415
+ }else if( idxPath<0 ){
5416
+ seenPath = 1;
5417
+ }
5418
+ break;
5419
+ }
5420
+ case FSDIR_COLUMN_DIR: {
5421
+ if( pConstraint->usable ){
5422
+ idxDir = i;
5423
+ seenDir = 0;
5424
+ }else if( idxDir<0 ){
5425
+ seenDir = 1;
5426
+ }
5427
+ break;
5428
+ }
5429
+ }
5430
+ }
5431
+ if( seenPath || seenDir ){
5432
+ /* If input parameters are unusable, disallow this plan */
5433
+ return SQLITE_CONSTRAINT;
5434
+ }
5435
+
5436
+ if( idxPath<0 ){
5437
+ pIdxInfo->idxNum = 0;
5438
+ /* The pIdxInfo->estimatedCost should have been initialized to a huge
5439
+ ** number. Leave it unchanged. */
5440
+ pIdxInfo->estimatedRows = 0x7fffffff;
5441
+ }else{
5442
+ pIdxInfo->aConstraintUsage[idxPath].omit = 1;
5443
+ pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
5444
+ if( idxDir>=0 ){
5445
+ pIdxInfo->aConstraintUsage[idxDir].omit = 1;
5446
+ pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
5447
+ pIdxInfo->idxNum = 2;
5448
+ pIdxInfo->estimatedCost = 10.0;
5449
+ }else{
5450
+ pIdxInfo->idxNum = 1;
5451
+ pIdxInfo->estimatedCost = 100.0;
5452
+ }
5453
+ }
5454
+
5455
+ return SQLITE_OK;
5456
+}
5457
+
5458
+/*
5459
+** Register the "fsdir" virtual table.
5460
+*/
5461
+static int fsdirRegister(sqlite3 *db){
5462
+ static sqlite3_module fsdirModule = {
5463
+ 0, /* iVersion */
5464
+ 0, /* xCreate */
5465
+ fsdirConnect, /* xConnect */
5466
+ fsdirBestIndex, /* xBestIndex */
5467
+ fsdirDisconnect, /* xDisconnect */
5468
+ 0, /* xDestroy */
5469
+ fsdirOpen, /* xOpen - open a cursor */
5470
+ fsdirClose, /* xClose - close a cursor */
5471
+ fsdirFilter, /* xFilter - configure scan constraints */
5472
+ fsdirNext, /* xNext - advance a cursor */
5473
+ fsdirEof, /* xEof - check for end of scan */
5474
+ fsdirColumn, /* xColumn - read data */
5475
+ fsdirRowid, /* xRowid - read data */
5476
+ 0, /* xUpdate */
5477
+ 0, /* xBegin */
5478
+ 0, /* xSync */
5479
+ 0, /* xCommit */
5480
+ 0, /* xRollback */
5481
+ 0, /* xFindMethod */
5482
+ 0, /* xRename */
5483
+ 0, /* xSavepoint */
5484
+ 0, /* xRelease */
5485
+ 0, /* xRollbackTo */
5486
+ 0, /* xShadowName */
5487
+ };
5488
+
5489
+ int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
5490
+ return rc;
5491
+}
5492
+#else /* SQLITE_OMIT_VIRTUALTABLE */
5493
+# define fsdirRegister(x) SQLITE_OK
5494
+#endif
5495
+
5496
+#ifdef _WIN32
5497
+
5498
+#endif
5499
+int sqlite3_fileio_init(
5500
+ sqlite3 *db,
5501
+ char **pzErrMsg,
5502
+ const sqlite3_api_routines *pApi
5503
+){
5504
+ int rc = SQLITE_OK;
5505
+ SQLITE_EXTENSION_INIT2(pApi);
5506
+ (void)pzErrMsg; /* Unused parameter */
5507
+ rc = sqlite3_create_function(db, "readfile", 1,
5508
+ SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
5509
+ readfileFunc, 0, 0);
5510
+ if( rc==SQLITE_OK ){
5511
+ rc = sqlite3_create_function(db, "writefile", -1,
5512
+ SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
5513
+ writefileFunc, 0, 0);
5514
+ }
5515
+ if( rc==SQLITE_OK ){
5516
+ rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
5517
+ lsModeFunc, 0, 0);
5518
+ }
5519
+ if( rc==SQLITE_OK ){
5520
+ rc = fsdirRegister(db);
5521
+ }
5522
+ return rc;
5523
+}
5524
+
5525
+#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
5526
+/* To allow a standalone DLL, make test_windirent.c use the same
5527
+ * redefined SQLite API calls as the above extension code does.
5528
+ * Just pull in this .c to accomplish this. As a beneficial side
5529
+ * effect, this extension becomes a single translation unit. */
5530
+# include "test_windirent.c"
5531
+#endif
5532
+
5533
+/************************* End ../ext/misc/fileio.c ********************/
5534
+/************************* Begin ../ext/misc/completion.c ******************/
5535
+/*
5536
+** 2017-07-10
5537
+**
5538
+** The author disclaims copyright to this source code. In place of
5539
+** a legal notice, here is a blessing:
5540
+**
5541
+** May you do good and not evil.
5542
+** May you find forgiveness for yourself and forgive others.
5543
+** May you share freely, never taking more than you give.
5544
+**
5545
+*************************************************************************
5546
+**
5547
+** This file implements an eponymous virtual table that returns suggested
5548
+** completions for a partial SQL input.
5549
+**
5550
+** Suggested usage:
5551
+**
5552
+** SELECT DISTINCT candidate COLLATE nocase
5553
+** FROM completion($prefix,$wholeline)
5554
+** ORDER BY 1;
5555
+**
5556
+** The two query parameters are optional. $prefix is the text of the
5557
+** current word being typed and that is to be completed. $wholeline is
5558
+** the complete input line, used for context.
5559
+**
5560
+** The raw completion() table might return the same candidate multiple
5561
+** times, for example if the same column name is used to two or more
5562
+** tables. And the candidates are returned in an arbitrary order. Hence,
5563
+** the DISTINCT and ORDER BY are recommended.
5564
+**
5565
+** This virtual table operates at the speed of human typing, and so there
5566
+** is no attempt to make it fast. Even a slow implementation will be much
5567
+** faster than any human can type.
5568
+**
5569
+*/
5570
+/* #include "sqlite3ext.h" */
5571
+SQLITE_EXTENSION_INIT1
5572
+#include <assert.h>
5573
+#include <string.h>
5574
+#include <ctype.h>
5575
+
5576
+#ifndef SQLITE_OMIT_VIRTUALTABLE
5577
+
5578
+/* completion_vtab is a subclass of sqlite3_vtab which will
5579
+** serve as the underlying representation of a completion virtual table
5580
+*/
5581
+typedef struct completion_vtab completion_vtab;
5582
+struct completion_vtab {
5583
+ sqlite3_vtab base; /* Base class - must be first */
5584
+ sqlite3 *db; /* Database connection for this completion vtab */
5585
+};
5586
+
5587
+/* completion_cursor is a subclass of sqlite3_vtab_cursor which will
5588
+** serve as the underlying representation of a cursor that scans
5589
+** over rows of the result
5590
+*/
5591
+typedef struct completion_cursor completion_cursor;
5592
+struct completion_cursor {
5593
+ sqlite3_vtab_cursor base; /* Base class - must be first */
5594
+ sqlite3 *db; /* Database connection for this cursor */
5595
+ int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
5596
+ char *zPrefix; /* The prefix for the word we want to complete */
5597
+ char *zLine; /* The whole that we want to complete */
5598
+ const char *zCurrentRow; /* Current output row */
5599
+ int szRow; /* Length of the zCurrentRow string */
5600
+ sqlite3_stmt *pStmt; /* Current statement */
5601
+ sqlite3_int64 iRowid; /* The rowid */
5602
+ int ePhase; /* Current phase */
5603
+ int j; /* inter-phase counter */
5604
+};
5605
+
5606
+/* Values for ePhase:
5607
+*/
5608
+#define COMPLETION_FIRST_PHASE 1
5609
+#define COMPLETION_KEYWORDS 1
5610
+#define COMPLETION_PRAGMAS 2
5611
+#define COMPLETION_FUNCTIONS 3
5612
+#define COMPLETION_COLLATIONS 4
5613
+#define COMPLETION_INDEXES 5
5614
+#define COMPLETION_TRIGGERS 6
5615
+#define COMPLETION_DATABASES 7
5616
+#define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
5617
+#define COMPLETION_COLUMNS 9
5618
+#define COMPLETION_MODULES 10
5619
+#define COMPLETION_EOF 11
5620
+
5621
+/*
5622
+** The completionConnect() method is invoked to create a new
5623
+** completion_vtab that describes the completion virtual table.
5624
+**
5625
+** Think of this routine as the constructor for completion_vtab objects.
5626
+**
5627
+** All this routine needs to do is:
5628
+**
5629
+** (1) Allocate the completion_vtab object and initialize all fields.
5630
+**
5631
+** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5632
+** result set of queries against completion will look like.
5633
+*/
5634
+static int completionConnect(
5635
+ sqlite3 *db,
5636
+ void *pAux,
5637
+ int argc, const char *const*argv,
5638
+ sqlite3_vtab **ppVtab,
5639
+ char **pzErr
5640
+){
5641
+ completion_vtab *pNew;
5642
+ int rc;
5643
+
5644
+ (void)(pAux); /* Unused parameter */
5645
+ (void)(argc); /* Unused parameter */
5646
+ (void)(argv); /* Unused parameter */
5647
+ (void)(pzErr); /* Unused parameter */
5648
+
5649
+/* Column numbers */
5650
+#define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
5651
+#define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
5652
+#define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
5653
+#define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
5654
+
5655
+ sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5656
+ rc = sqlite3_declare_vtab(db,
5657
+ "CREATE TABLE x("
5658
+ " candidate TEXT,"
5659
+ " prefix TEXT HIDDEN,"
5660
+ " wholeline TEXT HIDDEN,"
5661
+ " phase INT HIDDEN" /* Used for debugging only */
5662
+ ")");
5663
+ if( rc==SQLITE_OK ){
5664
+ pNew = sqlite3_malloc( sizeof(*pNew) );
5665
+ *ppVtab = (sqlite3_vtab*)pNew;
5666
+ if( pNew==0 ) return SQLITE_NOMEM;
5667
+ memset(pNew, 0, sizeof(*pNew));
5668
+ pNew->db = db;
5669
+ }
5670
+ return rc;
5671
+}
5672
+
5673
+/*
5674
+** This method is the destructor for completion_cursor objects.
5675
+*/
5676
+static int completionDisconnect(sqlite3_vtab *pVtab){
5677
+ sqlite3_free(pVtab);
5678
+ return SQLITE_OK;
5679
+}
5680
+
5681
+/*
5682
+** Constructor for a new completion_cursor object.
5683
+*/
5684
+static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
5685
+ completion_cursor *pCur;
5686
+ pCur = sqlite3_malloc( sizeof(*pCur) );
5687
+ if( pCur==0 ) return SQLITE_NOMEM;
5688
+ memset(pCur, 0, sizeof(*pCur));
5689
+ pCur->db = ((completion_vtab*)p)->db;
5690
+ *ppCursor = &pCur->base;
5691
+ return SQLITE_OK;
5692
+}
5693
+
5694
+/*
5695
+** Reset the completion_cursor.
5696
+*/
5697
+static void completionCursorReset(completion_cursor *pCur){
5698
+ sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
5699
+ sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
5700
+ sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
5701
+ pCur->j = 0;
5702
+}
5703
+
5704
+/*
5705
+** Destructor for a completion_cursor.
5706
+*/
5707
+static int completionClose(sqlite3_vtab_cursor *cur){
5708
+ completionCursorReset((completion_cursor*)cur);
5709
+ sqlite3_free(cur);
5710
+ return SQLITE_OK;
5711
+}
5712
+
5713
+/*
5714
+** Advance a completion_cursor to its next row of output.
5715
+**
5716
+** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
5717
+** record the current state of the scan. This routine sets ->zCurrentRow
5718
+** to the current row of output and then returns. If no more rows remain,
5719
+** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
5720
+** table that has reached the end of its scan.
5721
+**
5722
+** The current implementation just lists potential identifiers and
5723
+** keywords and filters them by zPrefix. Future enhancements should
5724
+** take zLine into account to try to restrict the set of identifiers and
5725
+** keywords based on what would be legal at the current point of input.
5726
+*/
5727
+static int completionNext(sqlite3_vtab_cursor *cur){
5728
+ completion_cursor *pCur = (completion_cursor*)cur;
5729
+ int eNextPhase = 0; /* Next phase to try if current phase reaches end */
5730
+ int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
5731
+ pCur->iRowid++;
5732
+ while( pCur->ePhase!=COMPLETION_EOF ){
5733
+ switch( pCur->ePhase ){
5734
+ case COMPLETION_KEYWORDS: {
5735
+ if( pCur->j >= sqlite3_keyword_count() ){
5736
+ pCur->zCurrentRow = 0;
5737
+ pCur->ePhase = COMPLETION_DATABASES;
5738
+ }else{
5739
+ sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
5740
+ }
5741
+ iCol = -1;
5742
+ break;
5743
+ }
5744
+ case COMPLETION_DATABASES: {
5745
+ if( pCur->pStmt==0 ){
5746
+ sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
5747
+ &pCur->pStmt, 0);
5748
+ }
5749
+ iCol = 1;
5750
+ eNextPhase = COMPLETION_TABLES;
5751
+ break;
5752
+ }
5753
+ case COMPLETION_TABLES: {
5754
+ if( pCur->pStmt==0 ){
5755
+ sqlite3_stmt *pS2;
5756
+ char *zSql = 0;
5757
+ const char *zSep = "";
5758
+ sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
5759
+ while( sqlite3_step(pS2)==SQLITE_ROW ){
5760
+ const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
5761
+ zSql = sqlite3_mprintf(
5762
+ "%z%s"
5763
+ "SELECT name FROM \"%w\".sqlite_schema",
5764
+ zSql, zSep, zDb
5765
+ );
5766
+ if( zSql==0 ) return SQLITE_NOMEM;
5767
+ zSep = " UNION ";
5768
+ }
5769
+ sqlite3_finalize(pS2);
5770
+ sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
5771
+ sqlite3_free(zSql);
5772
+ }
5773
+ iCol = 0;
5774
+ eNextPhase = COMPLETION_COLUMNS;
5775
+ break;
5776
+ }
5777
+ case COMPLETION_COLUMNS: {
5778
+ if( pCur->pStmt==0 ){
5779
+ sqlite3_stmt *pS2;
5780
+ char *zSql = 0;
5781
+ const char *zSep = "";
5782
+ sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
5783
+ while( sqlite3_step(pS2)==SQLITE_ROW ){
5784
+ const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
5785
+ zSql = sqlite3_mprintf(
5786
+ "%z%s"
5787
+ "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
5788
+ " JOIN pragma_table_info(sm.name,%Q) AS pti"
5789
+ " WHERE sm.type='table'",
5790
+ zSql, zSep, zDb, zDb
5791
+ );
5792
+ if( zSql==0 ) return SQLITE_NOMEM;
5793
+ zSep = " UNION ";
5794
+ }
5795
+ sqlite3_finalize(pS2);
5796
+ sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
5797
+ sqlite3_free(zSql);
5798
+ }
5799
+ iCol = 0;
5800
+ eNextPhase = COMPLETION_EOF;
5801
+ break;
5802
+ }
5803
+ }
5804
+ if( iCol<0 ){
5805
+ /* This case is when the phase presets zCurrentRow */
5806
+ if( pCur->zCurrentRow==0 ) continue;
5807
+ }else{
5808
+ if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
5809
+ /* Extract the next row of content */
5810
+ pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
5811
+ pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
5812
+ }else{
5813
+ /* When all rows are finished, advance to the next phase */
5814
+ sqlite3_finalize(pCur->pStmt);
5815
+ pCur->pStmt = 0;
5816
+ pCur->ePhase = eNextPhase;
5817
+ continue;
5818
+ }
5819
+ }
5820
+ if( pCur->nPrefix==0 ) break;
5821
+ if( pCur->nPrefix<=pCur->szRow
5822
+ && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
5823
+ ){
5824
+ break;
5825
+ }
5826
+ }
5827
+
5828
+ return SQLITE_OK;
5829
+}
5830
+
5831
+/*
5832
+** Return values of columns for the row at which the completion_cursor
5833
+** is currently pointing.
5834
+*/
5835
+static int completionColumn(
5836
+ sqlite3_vtab_cursor *cur, /* The cursor */
5837
+ sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5838
+ int i /* Which column to return */
5839
+){
5840
+ completion_cursor *pCur = (completion_cursor*)cur;
5841
+ switch( i ){
5842
+ case COMPLETION_COLUMN_CANDIDATE: {
5843
+ sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
5844
+ break;
5845
+ }
5846
+ case COMPLETION_COLUMN_PREFIX: {
5847
+ sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
5848
+ break;
5849
+ }
5850
+ case COMPLETION_COLUMN_WHOLELINE: {
5851
+ sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
5852
+ break;
5853
+ }
5854
+ case COMPLETION_COLUMN_PHASE: {
5855
+ sqlite3_result_int(ctx, pCur->ePhase);
5856
+ break;
5857
+ }
5858
+ }
5859
+ return SQLITE_OK;
5860
+}
5861
+
5862
+/*
5863
+** Return the rowid for the current row. In this implementation, the
5864
+** rowid is the same as the output value.
5865
+*/
5866
+static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5867
+ completion_cursor *pCur = (completion_cursor*)cur;
5868
+ *pRowid = pCur->iRowid;
5869
+ return SQLITE_OK;
5870
+}
5871
+
5872
+/*
5873
+** Return TRUE if the cursor has been moved off of the last
5874
+** row of output.
5875
+*/
5876
+static int completionEof(sqlite3_vtab_cursor *cur){
5877
+ completion_cursor *pCur = (completion_cursor*)cur;
5878
+ return pCur->ePhase >= COMPLETION_EOF;
5879
+}
5880
+
5881
+/*
5882
+** This method is called to "rewind" the completion_cursor object back
5883
+** to the first row of output. This method is always called at least
5884
+** once prior to any call to completionColumn() or completionRowid() or
5885
+** completionEof().
5886
+*/
5887
+static int completionFilter(
5888
+ sqlite3_vtab_cursor *pVtabCursor,
5889
+ int idxNum, const char *idxStr,
5890
+ int argc, sqlite3_value **argv
5891
+){
5892
+ completion_cursor *pCur = (completion_cursor *)pVtabCursor;
5893
+ int iArg = 0;
5894
+ (void)(idxStr); /* Unused parameter */
5895
+ (void)(argc); /* Unused parameter */
5896
+ completionCursorReset(pCur);
5897
+ if( idxNum & 1 ){
5898
+ pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
5899
+ if( pCur->nPrefix>0 ){
5900
+ pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
5901
+ if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
5902
+ }
5903
+ iArg = 1;
5904
+ }
5905
+ if( idxNum & 2 ){
5906
+ pCur->nLine = sqlite3_value_bytes(argv[iArg]);
5907
+ if( pCur->nLine>0 ){
5908
+ pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
5909
+ if( pCur->zLine==0 ) return SQLITE_NOMEM;
5910
+ }
5911
+ }
5912
+ if( pCur->zLine!=0 && pCur->zPrefix==0 ){
5913
+ int i = pCur->nLine;
5914
+ while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
5915
+ i--;
5916
+ }
5917
+ pCur->nPrefix = pCur->nLine - i;
5918
+ if( pCur->nPrefix>0 ){
5919
+ pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
5920
+ if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
5921
+ }
5922
+ }
5923
+ pCur->iRowid = 0;
5924
+ pCur->ePhase = COMPLETION_FIRST_PHASE;
5925
+ return completionNext(pVtabCursor);
5926
+}
5927
+
5928
+/*
5929
+** SQLite will invoke this method one or more times while planning a query
5930
+** that uses the completion virtual table. This routine needs to create
5931
+** a query plan for each invocation and compute an estimated cost for that
5932
+** plan.
5933
+**
5934
+** There are two hidden parameters that act as arguments to the table-valued
5935
+** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
5936
+** is available and bit 1 is set if "wholeline" is available.
5937
+*/
5938
+static int completionBestIndex(
5939
+ sqlite3_vtab *tab,
5940
+ sqlite3_index_info *pIdxInfo
5941
+){
5942
+ int i; /* Loop over constraints */
5943
+ int idxNum = 0; /* The query plan bitmask */
5944
+ int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
5945
+ int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
5946
+ int nArg = 0; /* Number of arguments that completeFilter() expects */
5947
+ const struct sqlite3_index_constraint *pConstraint;
5948
+
5949
+ (void)(tab); /* Unused parameter */
5950
+ pConstraint = pIdxInfo->aConstraint;
5951
+ for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5952
+ if( pConstraint->usable==0 ) continue;
5953
+ if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
5954
+ switch( pConstraint->iColumn ){
5955
+ case COMPLETION_COLUMN_PREFIX:
5956
+ prefixIdx = i;
5957
+ idxNum |= 1;
5958
+ break;
5959
+ case COMPLETION_COLUMN_WHOLELINE:
5960
+ wholelineIdx = i;
5961
+ idxNum |= 2;
5962
+ break;
5963
+ }
5964
+ }
5965
+ if( prefixIdx>=0 ){
5966
+ pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
5967
+ pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
5968
+ }
5969
+ if( wholelineIdx>=0 ){
5970
+ pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
5971
+ pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
5972
+ }
5973
+ pIdxInfo->idxNum = idxNum;
5974
+ pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
5975
+ pIdxInfo->estimatedRows = 500 - 100*nArg;
5976
+ return SQLITE_OK;
5977
+}
5978
+
5979
+/*
5980
+** This following structure defines all the methods for the
5981
+** completion virtual table.
5982
+*/
5983
+static sqlite3_module completionModule = {
5984
+ 0, /* iVersion */
5985
+ 0, /* xCreate */
5986
+ completionConnect, /* xConnect */
5987
+ completionBestIndex, /* xBestIndex */
5988
+ completionDisconnect, /* xDisconnect */
5989
+ 0, /* xDestroy */
5990
+ completionOpen, /* xOpen - open a cursor */
5991
+ completionClose, /* xClose - close a cursor */
5992
+ completionFilter, /* xFilter - configure scan constraints */
5993
+ completionNext, /* xNext - advance a cursor */
5994
+ completionEof, /* xEof - check for end of scan */
5995
+ completionColumn, /* xColumn - read data */
5996
+ completionRowid, /* xRowid - read data */
5997
+ 0, /* xUpdate */
5998
+ 0, /* xBegin */
5999
+ 0, /* xSync */
6000
+ 0, /* xCommit */
6001
+ 0, /* xRollback */
6002
+ 0, /* xFindMethod */
6003
+ 0, /* xRename */
6004
+ 0, /* xSavepoint */
6005
+ 0, /* xRelease */
6006
+ 0, /* xRollbackTo */
6007
+ 0 /* xShadowName */
6008
+};
6009
+
6010
+#endif /* SQLITE_OMIT_VIRTUALTABLE */
6011
+
6012
+int sqlite3CompletionVtabInit(sqlite3 *db){
6013
+ int rc = SQLITE_OK;
6014
+#ifndef SQLITE_OMIT_VIRTUALTABLE
6015
+ rc = sqlite3_create_module(db, "completion", &completionModule, 0);
6016
+#endif
6017
+ return rc;
6018
+}
6019
+
6020
+#ifdef _WIN32
6021
+
6022
+#endif
6023
+int sqlite3_completion_init(
6024
+ sqlite3 *db,
6025
+ char **pzErrMsg,
6026
+ const sqlite3_api_routines *pApi
6027
+){
6028
+ int rc = SQLITE_OK;
6029
+ SQLITE_EXTENSION_INIT2(pApi);
6030
+ (void)(pzErrMsg); /* Unused parameter */
6031
+#ifndef SQLITE_OMIT_VIRTUALTABLE
6032
+ rc = sqlite3CompletionVtabInit(db);
6033
+#endif
6034
+ return rc;
6035
+}
6036
+
6037
+/************************* End ../ext/misc/completion.c ********************/
6038
+/************************* Begin ../ext/misc/appendvfs.c ******************/
6039
+/*
6040
+** 2017-10-20
6041
+**
6042
+** The author disclaims copyright to this source code. In place of
6043
+** a legal notice, here is a blessing:
6044
+**
6045
+** May you do good and not evil.
6046
+** May you find forgiveness for yourself and forgive others.
6047
+** May you share freely, never taking more than you give.
6048
+**
6049
+******************************************************************************
6050
+**
6051
+** This file implements a VFS shim that allows an SQLite database to be
6052
+** appended onto the end of some other file, such as an executable.
6053
+**
6054
+** A special record must appear at the end of the file that identifies the
6055
+** file as an appended database and provides the offset to the first page
6056
+** of the exposed content. (Or, it is the length of the content prefix.)
6057
+** For best performance page 1 should be located at a disk page boundary,
6058
+** though that is not required.
6059
+**
6060
+** When opening a database using this VFS, the connection might treat
6061
+** the file as an ordinary SQLite database, or it might treat it as a
6062
+** database appended onto some other file. The decision is made by
6063
+** applying the following rules in order:
6064
+**
6065
+** (1) An empty file is an ordinary database.
6066
+**
6067
+** (2) If the file ends with the appendvfs trailer string
6068
+** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
6069
+**
6070
+** (3) If the file begins with the standard SQLite prefix string
6071
+** "SQLite format 3", that file is an ordinary database.
6072
+**
6073
+** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
6074
+** set, then a new database is appended to the already existing file.
6075
+**
6076
+** (5) Otherwise, SQLITE_CANTOPEN is returned.
6077
+**
6078
+** To avoid unnecessary complications with the PENDING_BYTE, the size of
6079
+** the file containing the database is limited to 1GiB. (1073741824 bytes)
6080
+** This VFS will not read or write past the 1GiB mark. This restriction
6081
+** might be lifted in future versions. For now, if you need a larger
6082
+** database, then keep it in a separate file.
6083
+**
6084
+** If the file being opened is a plain database (not an appended one), then
6085
+** this shim is a pass-through into the default underlying VFS. (rule 3)
6086
+**/
6087
+/* #include "sqlite3ext.h" */
6088
+SQLITE_EXTENSION_INIT1
6089
+#include <string.h>
6090
+#include <assert.h>
6091
+
6092
+/* The append mark at the end of the database is:
6093
+**
6094
+** Start-Of-SQLite3-NNNNNNNN
6095
+** 123456789 123456789 12345
6096
+**
6097
+** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
6098
+** the offset to page 1, and also the length of the prefix content.
6099
+*/
6100
+#define APND_MARK_PREFIX "Start-Of-SQLite3-"
6101
+#define APND_MARK_PREFIX_SZ 17
6102
+#define APND_MARK_FOS_SZ 8
6103
+#define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
6104
+
6105
+/*
6106
+** Maximum size of the combined prefix + database + append-mark. This
6107
+** must be less than 0x40000000 to avoid locking issues on Windows.
6108
+*/
6109
+#define APND_MAX_SIZE (0x40000000)
6110
+
6111
+/*
6112
+** Try to align the database to an even multiple of APND_ROUNDUP bytes.
6113
+*/
6114
+#ifndef APND_ROUNDUP
6115
+#define APND_ROUNDUP 4096
6116
+#endif
6117
+#define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1))
6118
+#define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
6119
+
6120
+/*
6121
+** Forward declaration of objects used by this utility
6122
+*/
6123
+typedef struct sqlite3_vfs ApndVfs;
6124
+typedef struct ApndFile ApndFile;
6125
+
6126
+/* Access to a lower-level VFS that (might) implement dynamic loading,
6127
+** access to randomness, etc.
6128
+*/
6129
+#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
6130
+#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
6131
+
6132
+/* An open appendvfs file
6133
+**
6134
+** An instance of this structure describes the appended database file.
6135
+** A separate sqlite3_file object is always appended. The appended
6136
+** sqlite3_file object (which can be accessed using ORIGFILE()) describes
6137
+** the entire file, including the prefix, the database, and the
6138
+** append-mark.
6139
+**
6140
+** The structure of an AppendVFS database is like this:
6141
+**
6142
+** +-------------+---------+----------+-------------+
6143
+** | prefix-file | padding | database | append-mark |
6144
+** +-------------+---------+----------+-------------+
6145
+** ^ ^
6146
+** | |
6147
+** iPgOne iMark
6148
+**
6149
+**
6150
+** "prefix file" - file onto which the database has been appended.
6151
+** "padding" - zero or more bytes inserted so that "database"
6152
+** starts on an APND_ROUNDUP boundary
6153
+** "database" - The SQLite database file
6154
+** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
6155
+** the offset from the start of prefix-file to the start
6156
+** of "database".
6157
+**
6158
+** The size of the database is iMark - iPgOne.
6159
+**
6160
+** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
6161
+** of iPgOne stored as a big-ending 64-bit integer.
6162
+**
6163
+** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
6164
+** Or, iMark is -1 to indicate that it has not yet been written.
6165
+*/
6166
+struct ApndFile {
6167
+ sqlite3_file base; /* Subclass. MUST BE FIRST! */
6168
+ sqlite3_int64 iPgOne; /* Offset to the start of the database */
6169
+ sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */
6170
+ /* Always followed by another sqlite3_file that describes the whole file */
6171
+};
6172
+
6173
+/*
6174
+** Methods for ApndFile
6175
+*/
6176
+static int apndClose(sqlite3_file*);
6177
+static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
6178
+static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
6179
+static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
6180
+static int apndSync(sqlite3_file*, int flags);
6181
+static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
6182
+static int apndLock(sqlite3_file*, int);
6183
+static int apndUnlock(sqlite3_file*, int);
6184
+static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
6185
+static int apndFileControl(sqlite3_file*, int op, void *pArg);
6186
+static int apndSectorSize(sqlite3_file*);
6187
+static int apndDeviceCharacteristics(sqlite3_file*);
6188
+static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
6189
+static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
6190
+static void apndShmBarrier(sqlite3_file*);
6191
+static int apndShmUnmap(sqlite3_file*, int deleteFlag);
6192
+static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
6193
+static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
6194
+
6195
+/*
6196
+** Methods for ApndVfs
6197
+*/
6198
+static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
6199
+static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
6200
+static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
6201
+static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
6202
+static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
6203
+static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
6204
+static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
6205
+static void apndDlClose(sqlite3_vfs*, void*);
6206
+static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
6207
+static int apndSleep(sqlite3_vfs*, int microseconds);
6208
+static int apndCurrentTime(sqlite3_vfs*, double*);
6209
+static int apndGetLastError(sqlite3_vfs*, int, char *);
6210
+static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
6211
+static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
6212
+static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
6213
+static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
6214
+
6215
+static sqlite3_vfs apnd_vfs = {
6216
+ 3, /* iVersion (set when registered) */
6217
+ 0, /* szOsFile (set when registered) */
6218
+ 1024, /* mxPathname */
6219
+ 0, /* pNext */
6220
+ "apndvfs", /* zName */
6221
+ 0, /* pAppData (set when registered) */
6222
+ apndOpen, /* xOpen */
6223
+ apndDelete, /* xDelete */
6224
+ apndAccess, /* xAccess */
6225
+ apndFullPathname, /* xFullPathname */
6226
+ apndDlOpen, /* xDlOpen */
6227
+ apndDlError, /* xDlError */
6228
+ apndDlSym, /* xDlSym */
6229
+ apndDlClose, /* xDlClose */
6230
+ apndRandomness, /* xRandomness */
6231
+ apndSleep, /* xSleep */
6232
+ apndCurrentTime, /* xCurrentTime */
6233
+ apndGetLastError, /* xGetLastError */
6234
+ apndCurrentTimeInt64, /* xCurrentTimeInt64 */
6235
+ apndSetSystemCall, /* xSetSystemCall */
6236
+ apndGetSystemCall, /* xGetSystemCall */
6237
+ apndNextSystemCall /* xNextSystemCall */
6238
+};
6239
+
6240
+static const sqlite3_io_methods apnd_io_methods = {
6241
+ 3, /* iVersion */
6242
+ apndClose, /* xClose */
6243
+ apndRead, /* xRead */
6244
+ apndWrite, /* xWrite */
6245
+ apndTruncate, /* xTruncate */
6246
+ apndSync, /* xSync */
6247
+ apndFileSize, /* xFileSize */
6248
+ apndLock, /* xLock */
6249
+ apndUnlock, /* xUnlock */
6250
+ apndCheckReservedLock, /* xCheckReservedLock */
6251
+ apndFileControl, /* xFileControl */
6252
+ apndSectorSize, /* xSectorSize */
6253
+ apndDeviceCharacteristics, /* xDeviceCharacteristics */
6254
+ apndShmMap, /* xShmMap */
6255
+ apndShmLock, /* xShmLock */
6256
+ apndShmBarrier, /* xShmBarrier */
6257
+ apndShmUnmap, /* xShmUnmap */
6258
+ apndFetch, /* xFetch */
6259
+ apndUnfetch /* xUnfetch */
6260
+};
6261
+
6262
+/*
6263
+** Close an apnd-file.
6264
+*/
6265
+static int apndClose(sqlite3_file *pFile){
6266
+ pFile = ORIGFILE(pFile);
6267
+ return pFile->pMethods->xClose(pFile);
6268
+}
6269
+
6270
+/*
6271
+** Read data from an apnd-file.
6272
+*/
6273
+static int apndRead(
6274
+ sqlite3_file *pFile,
6275
+ void *zBuf,
6276
+ int iAmt,
6277
+ sqlite_int64 iOfst
6278
+){
6279
+ ApndFile *paf = (ApndFile *)pFile;
6280
+ pFile = ORIGFILE(pFile);
6281
+ return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
6282
+}
6283
+
6284
+/*
6285
+** Add the append-mark onto what should become the end of the file.
6286
+* If and only if this succeeds, internal ApndFile.iMark is updated.
6287
+* Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
6288
+*/
6289
+static int apndWriteMark(
6290
+ ApndFile *paf,
6291
+ sqlite3_file *pFile,
6292
+ sqlite_int64 iWriteEnd
6293
+){
6294
+ sqlite_int64 iPgOne = paf->iPgOne;
6295
+ unsigned char a[APND_MARK_SIZE];
6296
+ int i = APND_MARK_FOS_SZ;
6297
+ int rc;
6298
+ assert(pFile == ORIGFILE(paf));
6299
+ memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
6300
+ while( --i >= 0 ){
6301
+ a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
6302
+ iPgOne >>= 8;
6303
+ }
6304
+ iWriteEnd += paf->iPgOne;
6305
+ if( SQLITE_OK==(rc = pFile->pMethods->xWrite
6306
+ (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
6307
+ paf->iMark = iWriteEnd;
6308
+ }
6309
+ return rc;
6310
+}
6311
+
6312
+/*
6313
+** Write data to an apnd-file.
6314
+*/
6315
+static int apndWrite(
6316
+ sqlite3_file *pFile,
6317
+ const void *zBuf,
6318
+ int iAmt,
6319
+ sqlite_int64 iOfst
6320
+){
6321
+ ApndFile *paf = (ApndFile *)pFile;
6322
+ sqlite_int64 iWriteEnd = iOfst + iAmt;
6323
+ if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
6324
+ pFile = ORIGFILE(pFile);
6325
+ /* If append-mark is absent or will be overwritten, write it. */
6326
+ if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
6327
+ int rc = apndWriteMark(paf, pFile, iWriteEnd);
6328
+ if( SQLITE_OK!=rc ) return rc;
6329
+ }
6330
+ return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
6331
+}
6332
+
6333
+/*
6334
+** Truncate an apnd-file.
6335
+*/
6336
+static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
6337
+ ApndFile *paf = (ApndFile *)pFile;
6338
+ pFile = ORIGFILE(pFile);
6339
+ /* The append mark goes out first so truncate failure does not lose it. */
6340
+ if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
6341
+ /* Truncate underlying file just past append mark */
6342
+ return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
6343
+}
6344
+
6345
+/*
6346
+** Sync an apnd-file.
6347
+*/
6348
+static int apndSync(sqlite3_file *pFile, int flags){
6349
+ pFile = ORIGFILE(pFile);
6350
+ return pFile->pMethods->xSync(pFile, flags);
6351
+}
6352
+
6353
+/*
6354
+** Return the current file-size of an apnd-file.
6355
+** If the append mark is not yet there, the file-size is 0.
6356
+*/
6357
+static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
6358
+ ApndFile *paf = (ApndFile *)pFile;
6359
+ *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
6360
+ return SQLITE_OK;
6361
+}
6362
+
6363
+/*
6364
+** Lock an apnd-file.
6365
+*/
6366
+static int apndLock(sqlite3_file *pFile, int eLock){
6367
+ pFile = ORIGFILE(pFile);
6368
+ return pFile->pMethods->xLock(pFile, eLock);
6369
+}
6370
+
6371
+/*
6372
+** Unlock an apnd-file.
6373
+*/
6374
+static int apndUnlock(sqlite3_file *pFile, int eLock){
6375
+ pFile = ORIGFILE(pFile);
6376
+ return pFile->pMethods->xUnlock(pFile, eLock);
6377
+}
6378
+
6379
+/*
6380
+** Check if another file-handle holds a RESERVED lock on an apnd-file.
6381
+*/
6382
+static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
6383
+ pFile = ORIGFILE(pFile);
6384
+ return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
6385
+}
6386
+
6387
+/*
6388
+** File control method. For custom operations on an apnd-file.
6389
+*/
6390
+static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
6391
+ ApndFile *paf = (ApndFile *)pFile;
6392
+ int rc;
6393
+ pFile = ORIGFILE(pFile);
6394
+ if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
6395
+ rc = pFile->pMethods->xFileControl(pFile, op, pArg);
6396
+ if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
6397
+ *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
6398
+ }
6399
+ return rc;
6400
+}
6401
+
6402
+/*
6403
+** Return the sector-size in bytes for an apnd-file.
6404
+*/
6405
+static int apndSectorSize(sqlite3_file *pFile){
6406
+ pFile = ORIGFILE(pFile);
6407
+ return pFile->pMethods->xSectorSize(pFile);
6408
+}
6409
+
6410
+/*
6411
+** Return the device characteristic flags supported by an apnd-file.
6412
+*/
6413
+static int apndDeviceCharacteristics(sqlite3_file *pFile){
6414
+ pFile = ORIGFILE(pFile);
6415
+ return pFile->pMethods->xDeviceCharacteristics(pFile);
6416
+}
6417
+
6418
+/* Create a shared memory file mapping */
6419
+static int apndShmMap(
6420
+ sqlite3_file *pFile,
6421
+ int iPg,
6422
+ int pgsz,
6423
+ int bExtend,
6424
+ void volatile **pp
6425
+){
6426
+ pFile = ORIGFILE(pFile);
6427
+ return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
6428
+}
6429
+
6430
+/* Perform locking on a shared-memory segment */
6431
+static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
6432
+ pFile = ORIGFILE(pFile);
6433
+ return pFile->pMethods->xShmLock(pFile,offset,n,flags);
6434
+}
6435
+
6436
+/* Memory barrier operation on shared memory */
6437
+static void apndShmBarrier(sqlite3_file *pFile){
6438
+ pFile = ORIGFILE(pFile);
6439
+ pFile->pMethods->xShmBarrier(pFile);
6440
+}
6441
+
6442
+/* Unmap a shared memory segment */
6443
+static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
6444
+ pFile = ORIGFILE(pFile);
6445
+ return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
6446
+}
6447
+
6448
+/* Fetch a page of a memory-mapped file */
6449
+static int apndFetch(
6450
+ sqlite3_file *pFile,
6451
+ sqlite3_int64 iOfst,
6452
+ int iAmt,
6453
+ void **pp
6454
+){
6455
+ ApndFile *p = (ApndFile *)pFile;
6456
+ if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
6457
+ return SQLITE_IOERR; /* Cannot read what is not yet there. */
6458
+ }
6459
+ pFile = ORIGFILE(pFile);
6460
+ return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
6461
+}
6462
+
6463
+/* Release a memory-mapped page */
6464
+static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
6465
+ ApndFile *p = (ApndFile *)pFile;
6466
+ pFile = ORIGFILE(pFile);
6467
+ return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
6468
+}
6469
+
6470
+/*
6471
+** Try to read the append-mark off the end of a file. Return the
6472
+** start of the appended database if the append-mark is present.
6473
+** If there is no valid append-mark, return -1;
6474
+**
6475
+** An append-mark is only valid if the NNNNNNNN start-of-database offset
6476
+** indicates that the appended database contains at least one page. The
6477
+** start-of-database value must be a multiple of 512.
6478
+*/
6479
+static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
6480
+ int rc, i;
6481
+ sqlite3_int64 iMark;
6482
+ int msbs = 8 * (APND_MARK_FOS_SZ-1);
6483
+ unsigned char a[APND_MARK_SIZE];
6484
+
6485
+ if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
6486
+ rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
6487
+ if( rc ) return -1;
6488
+ if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
6489
+ iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
6490
+ for(i=1; i<8; i++){
6491
+ msbs -= 8;
6492
+ iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
6493
+ }
6494
+ if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
6495
+ if( iMark & 0x1ff ) return -1;
6496
+ return iMark;
6497
+}
6498
+
6499
+static const char apvfsSqliteHdr[] = "SQLite format 3";
6500
+/*
6501
+** Check to see if the file is an appendvfs SQLite database file.
6502
+** Return true iff it is such. Parameter sz is the file's size.
6503
+*/
6504
+static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
6505
+ int rc;
6506
+ char zHdr[16];
6507
+ sqlite3_int64 iMark = apndReadMark(sz, pFile);
6508
+ if( iMark>=0 ){
6509
+ /* If file has the correct end-marker, the expected odd size, and the
6510
+ ** SQLite DB type marker where the end-marker puts it, then it
6511
+ ** is an appendvfs database.
6512
+ */
6513
+ rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
6514
+ if( SQLITE_OK==rc
6515
+ && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
6516
+ && (sz & 0x1ff) == APND_MARK_SIZE
6517
+ && sz>=512+APND_MARK_SIZE
6518
+ ){
6519
+ return 1; /* It's an appendvfs database */
6520
+ }
6521
+ }
6522
+ return 0;
6523
+}
6524
+
6525
+/*
6526
+** Check to see if the file is an ordinary SQLite database file.
6527
+** Return true iff so. Parameter sz is the file's size.
6528
+*/
6529
+static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
6530
+ char zHdr[16];
6531
+ if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
6532
+ || (sz & 0x1ff) != 0
6533
+ || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
6534
+ || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
6535
+ ){
6536
+ return 0;
6537
+ }else{
6538
+ return 1;
6539
+ }
6540
+}
6541
+
6542
+/*
6543
+** Open an apnd file handle.
6544
+*/
6545
+static int apndOpen(
6546
+ sqlite3_vfs *pApndVfs,
6547
+ const char *zName,
6548
+ sqlite3_file *pFile,
6549
+ int flags,
6550
+ int *pOutFlags
6551
+){
6552
+ ApndFile *pApndFile = (ApndFile*)pFile;
6553
+ sqlite3_file *pBaseFile = ORIGFILE(pFile);
6554
+ sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
6555
+ int rc;
6556
+ sqlite3_int64 sz = 0;
6557
+ if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
6558
+ /* The appendvfs is not to be used for transient or temporary databases.
6559
+ ** Just use the base VFS open to initialize the given file object and
6560
+ ** open the underlying file. (Appendvfs is then unused for this file.)
6561
+ */
6562
+ return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
6563
+ }
6564
+ memset(pApndFile, 0, sizeof(ApndFile));
6565
+ pFile->pMethods = &apnd_io_methods;
6566
+ pApndFile->iMark = -1; /* Append mark not yet written */
6567
+
6568
+ rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
6569
+ if( rc==SQLITE_OK ){
6570
+ rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
6571
+ if( rc ){
6572
+ pBaseFile->pMethods->xClose(pBaseFile);
6573
+ }
6574
+ }
6575
+ if( rc ){
6576
+ pFile->pMethods = 0;
6577
+ return rc;
6578
+ }
6579
+ if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
6580
+ /* The file being opened appears to be just an ordinary DB. Copy
6581
+ ** the base dispatch-table so this instance mimics the base VFS.
6582
+ */
6583
+ memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
6584
+ return SQLITE_OK;
6585
+ }
6586
+ pApndFile->iPgOne = apndReadMark(sz, pFile);
6587
+ if( pApndFile->iPgOne>=0 ){
6588
+ pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
6589
+ return SQLITE_OK;
6590
+ }
6591
+ if( (flags & SQLITE_OPEN_CREATE)==0 ){
6592
+ pBaseFile->pMethods->xClose(pBaseFile);
6593
+ rc = SQLITE_CANTOPEN;
6594
+ pFile->pMethods = 0;
6595
+ }else{
6596
+ /* Round newly added appendvfs location to #define'd page boundary.
6597
+ ** Note that nothing has yet been written to the underlying file.
6598
+ ** The append mark will be written along with first content write.
6599
+ ** Until then, paf->iMark value indicates it is not yet written.
6600
+ */
6601
+ pApndFile->iPgOne = APND_START_ROUNDUP(sz);
6602
+ }
6603
+ return rc;
6604
+}
6605
+
6606
+/*
6607
+** Delete an apnd file.
6608
+** For an appendvfs, this could mean delete the appendvfs portion,
6609
+** leaving the appendee as it was before it gained an appendvfs.
6610
+** For now, this code deletes the underlying file too.
6611
+*/
6612
+static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
6613
+ return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
6614
+}
6615
+
6616
+/*
6617
+** All other VFS methods are pass-thrus.
6618
+*/
6619
+static int apndAccess(
6620
+ sqlite3_vfs *pVfs,
6621
+ const char *zPath,
6622
+ int flags,
6623
+ int *pResOut
6624
+){
6625
+ return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
6626
+}
6627
+static int apndFullPathname(
6628
+ sqlite3_vfs *pVfs,
6629
+ const char *zPath,
6630
+ int nOut,
6631
+ char *zOut
6632
+){
6633
+ return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
6634
+}
6635
+static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
6636
+ return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
6637
+}
6638
+static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
6639
+ ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
6640
+}
6641
+static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
6642
+ return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
6643
+}
6644
+static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
6645
+ ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
6646
+}
6647
+static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
6648
+ return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
6649
+}
6650
+static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
6651
+ return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
6652
+}
6653
+static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
6654
+ return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
6655
+}
6656
+static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
6657
+ return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
6658
+}
6659
+static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
6660
+ return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
6661
+}
6662
+static int apndSetSystemCall(
6663
+ sqlite3_vfs *pVfs,
6664
+ const char *zName,
6665
+ sqlite3_syscall_ptr pCall
6666
+){
6667
+ return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
6668
+}
6669
+static sqlite3_syscall_ptr apndGetSystemCall(
6670
+ sqlite3_vfs *pVfs,
6671
+ const char *zName
6672
+){
6673
+ return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
6674
+}
6675
+static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
6676
+ return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
6677
+}
6678
+
6679
+
6680
+#ifdef _WIN32
6681
+
6682
+#endif
6683
+/*
6684
+** This routine is called when the extension is loaded.
6685
+** Register the new VFS.
6686
+*/
6687
+int sqlite3_appendvfs_init(
6688
+ sqlite3 *db,
6689
+ char **pzErrMsg,
6690
+ const sqlite3_api_routines *pApi
6691
+){
6692
+ int rc = SQLITE_OK;
6693
+ sqlite3_vfs *pOrig;
6694
+ SQLITE_EXTENSION_INIT2(pApi);
6695
+ (void)pzErrMsg;
6696
+ (void)db;
6697
+ pOrig = sqlite3_vfs_find(0);
6698
+ if( pOrig==0 ) return SQLITE_ERROR;
6699
+ apnd_vfs.iVersion = pOrig->iVersion;
6700
+ apnd_vfs.pAppData = pOrig;
6701
+ apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
6702
+ rc = sqlite3_vfs_register(&apnd_vfs, 0);
6703
+#ifdef APPENDVFS_TEST
6704
+ if( rc==SQLITE_OK ){
6705
+ rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
6706
+ }
6707
+#endif
6708
+ if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
6709
+ return rc;
6710
+}
6711
+
6712
+/************************* End ../ext/misc/appendvfs.c ********************/
6713
+#endif
67016714
#ifdef SQLITE_HAVE_ZLIB
67026715
/************************* Begin ../ext/misc/zipfile.c ******************/
67036716
/*
67046717
** 2017-12-26
67056718
**
@@ -12235,11 +12248,21 @@
1223512248
int nIndent; /* Size of array aiIndent[] */
1223612249
int iIndent; /* Index of current op in aiIndent[] */
1223712250
char *zNonce; /* Nonce for temporary safe-mode excapes */
1223812251
EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
1223912252
ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
12253
+#ifdef SQLITE_SHELL_WASM_MODE
12254
+ struct {
12255
+ const char * zInput; /* Input string from wasm/JS proxy */
12256
+ const char * zPos; /* Cursor pos into zInput */
12257
+ } wasm;
12258
+#endif
1224012259
};
12260
+
12261
+#ifdef SQLITE_SHELL_WASM_MODE
12262
+static ShellState shellState;
12263
+#endif
1224112264
1224212265
1224312266
/* Allowed values for ShellState.autoEQP
1224412267
*/
1224512268
#define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
@@ -15304,17 +15327,18 @@
1530415327
1530515328
/*
1530615329
** Text of help messages.
1530715330
**
1530815331
** The help text for each individual command begins with a line that starts
15309
-** with ".". Subsequent lines are supplimental information.
15332
+** with ".". Subsequent lines are supplemental information.
1531015333
**
1531115334
** There must be two or more spaces between the end of the command and the
1531215335
** start of the description of what that command does.
1531315336
*/
1531415337
static const char *(azHelp[]) = {
15315
-#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
15338
+#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
15339
+ && !defined(SQLITE_SHELL_WASM_MODE)
1531615340
".archive ... Manage SQL archives",
1531715341
" Each command must have exactly one of the following options:",
1531815342
" -c, --create Create a new archive",
1531915343
" -u, --update Add or update files with changed mtime",
1532015344
" -i, --insert Like -u but always add even if unchanged",
@@ -15336,20 +15360,26 @@
1533615360
" http://sqlite.org/cli.html#sqlite_archive_support",
1533715361
#endif
1533815362
#ifndef SQLITE_OMIT_AUTHORIZATION
1533915363
".auth ON|OFF Show authorizer callbacks",
1534015364
#endif
15365
+#ifndef SQLITE_SHELL_WASM_MODE
1534115366
".backup ?DB? FILE Backup DB (default \"main\") to FILE",
1534215367
" Options:",
1534315368
" --append Use the appendvfs",
1534415369
" --async Write to FILE without journal and fsync()",
15370
+#endif
1534515371
".bail on|off Stop after hitting an error. Default OFF",
1534615372
".binary on|off Turn binary output on or off. Default OFF",
15373
+#ifndef SQLITE_SHELL_WASM_MODE
1534715374
".cd DIRECTORY Change the working directory to DIRECTORY",
15375
+#endif
1534815376
".changes on|off Show number of rows changed by SQL",
15377
+#ifndef SQLITE_SHELL_WASM_MODE
1534915378
".check GLOB Fail if output since .testcase does not match",
1535015379
".clone NEWDB Clone data into NEWDB from the existing database",
15380
+#endif
1535115381
".connection [close] [#] Open or close an auxiliary database connection",
1535215382
".databases List names and files of attached databases",
1535315383
".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
1535415384
".dbinfo ?DB? Show status information about the database",
1535515385
".dump ?OBJECTS? Render database content as SQL",
@@ -15366,21 +15396,26 @@
1536615396
#ifdef SQLITE_DEBUG
1536715397
" test Show raw EXPLAIN QUERY PLAN output",
1536815398
" trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
1536915399
#endif
1537015400
" trigger Like \"full\" but also show trigger bytecode",
15401
+#ifndef SQLITE_SHELL_WASM_MODE
1537115402
".excel Display the output of next command in spreadsheet",
1537215403
" --bom Put a UTF8 byte-order mark on intermediate file",
15404
+#endif
15405
+#ifndef SQLITE_SHELL_WASM_MODE
1537315406
".exit ?CODE? Exit this program with return-code CODE",
15407
+#endif
1537415408
".expert EXPERIMENTAL. Suggest indexes for queries",
1537515409
".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
1537615410
".filectrl CMD ... Run various sqlite3_file_control() operations",
1537715411
" --schema SCHEMA Use SCHEMA instead of \"main\"",
1537815412
" --help Show CMD details",
1537915413
".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
1538015414
".headers on|off Turn display of headers on or off",
1538115415
".help ?-all? ?PATTERN? Show help text for PATTERN",
15416
+#ifndef SQLITE_SHELL_WASM_MODE
1538215417
".import FILE TABLE Import data from FILE into TABLE",
1538315418
" Options:",
1538415419
" --ascii Use \\037 and \\036 as column and row separators",
1538515420
" --csv Use , and \\n as column and row separators",
1538615421
" --skip N Skip the first N rows of input",
@@ -15391,10 +15426,11 @@
1539115426
" determines the column names.",
1539215427
" * If neither --csv or --ascii are used, the input mode is derived",
1539315428
" from the \".mode\" output mode",
1539415429
" * If FILE begins with \"|\" then it is a command that generates the",
1539515430
" input text.",
15431
+#endif
1539615432
#ifndef SQLITE_OMIT_TEST_CONTROL
1539715433
".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
1539815434
#endif
1539915435
".indexes ?TABLE? Show names of indexes",
1540015436
" If TABLE is specified, only show indexes for",
@@ -15404,14 +15440,16 @@
1540415440
#endif
1540515441
".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
1540615442
".lint OPTIONS Report potential schema issues.",
1540715443
" Options:",
1540815444
" fkey-indexes Find missing foreign key indexes",
15409
-#ifndef SQLITE_OMIT_LOAD_EXTENSION
15445
+#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_WASM_MODE)
1541015446
".load FILE ?ENTRY? Load an extension library",
1541115447
#endif
15448
+#ifndef SQLITE_SHELL_WASM_MODE
1541215449
".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
15450
+#endif
1541315451
".mode MODE ?OPTIONS? Set output mode",
1541415452
" MODE is one of:",
1541515453
" ascii Columns/rows delimited by 0x1F and 0x1E",
1541615454
" box Tables using unicode box-drawing characters",
1541715455
" csv Comma-separated values",
@@ -15432,35 +15470,44 @@
1543215470
" --wordwrap B Wrap or not at word boundaries per B (on/off)",
1543315471
" --ww Shorthand for \"--wordwrap 1\"",
1543415472
" --quote Quote output text as SQL literals",
1543515473
" --noquote Do not quote output text",
1543615474
" TABLE The name of SQL table used for \"insert\" mode",
15475
+#ifndef SQLITE_SHELL_WASM_MODE
1543715476
".nonce STRING Suspend safe mode for one command if nonce matches",
15477
+#endif
1543815478
".nullvalue STRING Use STRING in place of NULL values",
15479
+#ifndef SQLITE_SHELL_WASM_MODE
1543915480
".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
1544015481
" If FILE begins with '|' then open as a pipe",
1544115482
" --bom Put a UTF8 byte-order mark at the beginning",
1544215483
" -e Send output to the system text editor",
1544315484
" -x Send output as CSV to a spreadsheet (same as \".excel\")",
15485
+ /* Note that .open is (partially) available in WASM builds but is
15486
+ ** currently only intended to be used by the fiddle tool, not
15487
+ ** end users, so is "undocumented." */
1544415488
".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
1544515489
" Options:",
1544615490
" --append Use appendvfs to append database to the end of FILE",
15491
+#endif
1544715492
#ifndef SQLITE_OMIT_DESERIALIZE
1544815493
" --deserialize Load into memory using sqlite3_deserialize()",
1544915494
" --hexdb Load the output of \"dbtotxt\" as an in-memory db",
1545015495
" --maxsize N Maximum size for --hexdb or --deserialized database",
1545115496
#endif
1545215497
" --new Initialize FILE to an empty database",
1545315498
" --nofollow Do not follow symbolic links",
1545415499
" --readonly Open FILE readonly",
1545515500
" --zip FILE is a ZIP archive",
15501
+#ifndef SQLITE_SHELL_WASM_MODE
1545615502
".output ?FILE? Send output to FILE or stdout if FILE is omitted",
1545715503
" If FILE begins with '|' then open it as a pipe.",
1545815504
" Options:",
1545915505
" --bom Prefix output with a UTF8 byte-order mark",
1546015506
" -e Send output to the system text editor",
1546115507
" -x Send output as CSV to a spreadsheet",
15508
+#endif
1546215509
".parameter CMD ... Manage SQL parameter bindings",
1546315510
" clear Erase all bindings",
1546415511
" init Initialize the TEMP table that holds bindings",
1546515512
" list List the current parameter bindings",
1546615513
" set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
@@ -15473,23 +15520,27 @@
1547315520
" --once Do no more than one progress interrupt",
1547415521
" --quiet|-q No output except at interrupts",
1547515522
" --reset Reset the count for each input and interrupt",
1547615523
#endif
1547715524
".prompt MAIN CONTINUE Replace the standard prompts",
15525
+#ifndef SQLITE_SHELL_WASM_MODE
1547815526
".quit Exit this program",
1547915527
".read FILE Read input from FILE or command output",
1548015528
" If FILE begins with \"|\", it is a command that generates the input.",
15529
+#endif
1548115530
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1548215531
".recover Recover as much data as possible from corrupt db.",
1548315532
" --freelist-corrupt Assume the freelist is corrupt",
1548415533
" --recovery-db NAME Store recovery metadata in database file NAME",
1548515534
" --lost-and-found TABLE Alternative name for the lost-and-found table",
1548615535
" --no-rowids Do not attempt to recover rowid values",
1548715536
" that are not also INTEGER PRIMARY KEYs",
1548815537
#endif
15538
+#ifndef SQLITE_SHELL_WASM_MODE
1548915539
".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
1549015540
".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
15541
+#endif
1549115542
".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
1549215543
".schema ?PATTERN? Show the CREATE statements matching PATTERN",
1549315544
" Options:",
1549415545
" --indent Try to pretty-print the schema",
1549515546
" --nosys Omit objects whose names start with \"sqlite_\"",
@@ -15519,24 +15570,26 @@
1551915570
" --sha3-224 Use the sha3-224 algorithm",
1552015571
" --sha3-256 Use the sha3-256 algorithm (default)",
1552115572
" --sha3-384 Use the sha3-384 algorithm",
1552215573
" --sha3-512 Use the sha3-512 algorithm",
1552315574
" Any other argument is a LIKE pattern for tables to hash",
15524
-#ifndef SQLITE_NOHAVE_SYSTEM
15575
+#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
1552515576
".shell CMD ARGS... Run CMD ARGS... in a system shell",
1552615577
#endif
1552715578
".show Show the current values for various settings",
1552815579
".stats ?ARG? Show stats or turn stats on or off",
1552915580
" off Turn off automatic stat display",
1553015581
" on Turn on automatic stat display",
1553115582
" stmt Show statement stats",
1553215583
" vmstep Show the virtual machine step count only",
15533
-#ifndef SQLITE_NOHAVE_SYSTEM
15584
+#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
1553415585
".system CMD ARGS... Run CMD ARGS... in a system shell",
1553515586
#endif
1553615587
".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
15588
+#ifndef SQLITE_SHELL_WASM_MODE
1553715589
".testcase NAME Begin redirecting output to 'testcase-out.txt'",
15590
+#endif
1553815591
".testctrl CMD ... Run various sqlite3_test_control() operations",
1553915592
" Run \".testctrl\" with no arguments for details",
1554015593
".timeout MS Try opening locked tables for MS milliseconds",
1554115594
".timer on|off Turn SQL timer on or off",
1554215595
#ifndef SQLITE_OMIT_TRACE
@@ -16077,18 +16130,20 @@
1607716130
exit(1);
1607816131
}
1607916132
#ifndef SQLITE_OMIT_LOAD_EXTENSION
1608016133
sqlite3_enable_load_extension(p->db, 1);
1608116134
#endif
16082
- sqlite3_fileio_init(p->db, 0, 0);
1608316135
sqlite3_shathree_init(p->db, 0, 0);
16084
- sqlite3_completion_init(p->db, 0, 0);
1608516136
sqlite3_uint_init(p->db, 0, 0);
1608616137
sqlite3_decimal_init(p->db, 0, 0);
1608716138
sqlite3_regexp_init(p->db, 0, 0);
1608816139
sqlite3_ieee_init(p->db, 0, 0);
1608916140
sqlite3_series_init(p->db, 0, 0);
16141
+#ifndef SQLITE_SHELL_WASM_MODE
16142
+ sqlite3_fileio_init(p->db, 0, 0);
16143
+ sqlite3_completion_init(p->db, 0, 0);
16144
+#endif
1609016145
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1609116146
sqlite3_dbdata_init(p->db, 0, 0);
1609216147
#endif
1609316148
#ifdef SQLITE_HAVE_ZLIB
1609416149
if( !p->bSafeModePersist ){
@@ -19207,18 +19262,20 @@
1920719262
sqlite3_set_authorizer(p->db, 0, 0);
1920819263
}
1920919264
}else
1921019265
#endif
1921119266
19212
-#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
19267
+#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
19268
+ && !defined(SQLITE_SHELL_WASM_MODE)
1921319269
if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
1921419270
open_db(p, 0);
1921519271
failIfSafeMode(p, "cannot run .archive in safe mode");
1921619272
rc = arDotCommand(p, 0, azArg, nArg);
1921719273
}else
1921819274
#endif
1921919275
19276
+#ifndef SQLITE_SHELL_WASM_MODE
1922019277
if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
1922119278
|| (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
1922219279
){
1922319280
const char *zDestFile = 0;
1922419281
const char *zDb = 0;
@@ -19283,10 +19340,11 @@
1928319340
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
1928419341
rc = 1;
1928519342
}
1928619343
close_db(pDest);
1928719344
}else
19345
+#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
1928819346
1928919347
if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
1929019348
if( nArg==2 ){
1929119349
bail_on_error = booleanValue(azArg[1]);
1929219350
}else{
@@ -19313,10 +19371,11 @@
1931319371
*/
1931419372
if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
1931519373
test_breakpoint();
1931619374
}else
1931719375
19376
+#ifndef SQLITE_SHELL_WASM_MODE
1931819377
if( c=='c' && strcmp(azArg[0],"cd")==0 ){
1931919378
failIfSafeMode(p, "cannot run .cd in safe mode");
1932019379
if( nArg==2 ){
1932119380
#if defined(_WIN32) || defined(WIN32)
1932219381
wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
@@ -19332,10 +19391,11 @@
1933219391
}else{
1933319392
raw_printf(stderr, "Usage: .cd DIRECTORY\n");
1933419393
rc = 1;
1933519394
}
1933619395
}else
19396
+#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
1933719397
1933819398
if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
1933919399
if( nArg==2 ){
1934019400
setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
1934119401
}else{
@@ -19342,10 +19402,11 @@
1934219402
raw_printf(stderr, "Usage: .changes on|off\n");
1934319403
rc = 1;
1934419404
}
1934519405
}else
1934619406
19407
+#ifndef SQLITE_SHELL_WASM_MODE
1934719408
/* Cancel output redirection, if it is currently set (by .testcase)
1934819409
** Then read the content of the testcase-out.txt file and compare against
1934919410
** azArg[1]. If there are differences, report an error and exit.
1935019411
*/
1935119412
if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
@@ -19366,20 +19427,23 @@
1936619427
utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
1936719428
p->nCheck++;
1936819429
}
1936919430
sqlite3_free(zRes);
1937019431
}else
19432
+#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
1937119433
19434
+#ifndef SQLITE_SHELL_WASM_MODE
1937219435
if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
1937319436
failIfSafeMode(p, "cannot run .clone in safe mode");
1937419437
if( nArg==2 ){
1937519438
tryToClone(p, azArg[1]);
1937619439
}else{
1937719440
raw_printf(stderr, "Usage: .clone FILENAME\n");
1937819441
rc = 1;
1937919442
}
1938019443
}else
19444
+#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
1938119445
1938219446
if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
1938319447
if( nArg==1 ){
1938419448
/* List available connections */
1938519449
int i;
@@ -19664,14 +19728,16 @@
1966419728
raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
1966519729
rc = 1;
1966619730
}
1966719731
}else
1966819732
19733
+#ifndef SQLITE_SHELL_WASM_MODE
1966919734
if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
1967019735
if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
1967119736
rc = 2;
1967219737
}else
19738
+#endif
1967319739
1967419740
/* The ".explain" command is automatic now. It is largely pointless. It
1967519741
** retained purely for backwards compatibility */
1967619742
if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
1967719743
int val = 1;
@@ -19922,10 +19988,11 @@
1992219988
}else{
1992319989
showHelp(p->out, 0);
1992419990
}
1992519991
}else
1992619992
19993
+#ifndef SQLITE_SHELL_WASM_MODE
1992719994
if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
1992819995
char *zTable = 0; /* Insert data into this table */
1992919996
char *zSchema = 0; /* within this schema (may default to "main") */
1993019997
char *zFile = 0; /* Name of file to extra content from */
1993119998
sqlite3_stmt *pStmt = NULL; /* A statement */
@@ -20212,10 +20279,11 @@
2021220279
utf8_printf(p->out,
2021320280
"Added %d rows with %d errors using %d lines of input\n",
2021420281
sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
2021520282
}
2021620283
}else
20284
+#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
2021720285
2021820286
#ifndef SQLITE_UNTESTABLE
2021920287
if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
2022020288
char *zSql;
2022120289
char *zCollist = 0;
@@ -20401,11 +20469,11 @@
2040120469
if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
2040220470
open_db(p, 0);
2040320471
lintDotCommand(p, azArg, nArg);
2040420472
}else
2040520473
20406
-#ifndef SQLITE_OMIT_LOAD_EXTENSION
20474
+#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_WASM_MODE)
2040720475
if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
2040820476
const char *zFile, *zProc;
2040920477
char *zErrMsg = 0;
2041020478
failIfSafeMode(p, "cannot run .load in safe mode");
2041120479
if( nArg<2 ){
@@ -20423,10 +20491,11 @@
2042320491
rc = 1;
2042420492
}
2042520493
}else
2042620494
#endif
2042720495
20496
+#ifndef SQLITE_SHELL_WASM_MODE
2042820497
if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
2042920498
failIfSafeMode(p, "cannot run .log in safe mode");
2043020499
if( nArg!=2 ){
2043120500
raw_printf(stderr, "Usage: .log FILENAME\n");
2043220501
rc = 1;
@@ -20434,10 +20503,11 @@
2043420503
const char *zFile = azArg[1];
2043520504
output_file_close(p->pLog);
2043620505
p->pLog = output_file_open(zFile, 0);
2043720506
}
2043820507
}else
20508
+#endif
2043920509
2044020510
if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
2044120511
const char *zMode = 0;
2044220512
const char *zTabname = 0;
2044320513
int i, n2;
@@ -20558,10 +20628,11 @@
2055820628
rc = 1;
2055920629
}
2056020630
p->cMode = p->mode;
2056120631
}else
2056220632
20633
+#ifndef SQLITE_SHELL_WASM_MODE
2056320634
if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
2056420635
if( nArg!=2 ){
2056520636
raw_printf(stderr, "Usage: .nonce NONCE\n");
2056620637
rc = 1;
2056720638
}else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
@@ -20572,10 +20643,11 @@
2057220643
p->bSafeMode = 0;
2057320644
return 0; /* Return immediately to bypass the safe mode reset
2057420645
** at the end of this procedure */
2057520646
}
2057620647
}else
20648
+#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
2057720649
2057820650
if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
2057920651
if( nArg==2 ){
2058020652
sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
2058120653
"%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
@@ -20593,10 +20665,11 @@
2059320665
int openMode = SHELL_OPEN_UNSPEC;
2059420666
2059520667
/* Check for command-line arguments */
2059620668
for(iName=1; iName<nArg; iName++){
2059720669
const char *z = azArg[iName];
20670
+#ifndef SQLITE_SHELL_WASM_MODE
2059820671
if( optionMatch(z,"new") ){
2059920672
newFlag = 1;
2060020673
#ifdef SQLITE_HAVE_ZLIB
2060120674
}else if( optionMatch(z, "zip") ){
2060220675
openMode = SHELL_OPEN_ZIPFILE;
@@ -20613,11 +20686,13 @@
2061320686
}else if( optionMatch(z, "hexdb") ){
2061420687
openMode = SHELL_OPEN_HEXDB;
2061520688
}else if( optionMatch(z, "maxsize") && iName+1<nArg ){
2061620689
p->szMax = integerValue(azArg[++iName]);
2061720690
#endif /* SQLITE_OMIT_DESERIALIZE */
20618
- }else if( z[0]=='-' ){
20691
+ }else
20692
+#endif /* !SQLITE_SHELL_WASM_MODE */
20693
+ if( z[0]=='-' ){
2061920694
utf8_printf(stderr, "unknown option: %s\n", z);
2062020695
rc = 1;
2062120696
goto meta_command_exit;
2062220697
}else if( zFN ){
2062320698
utf8_printf(stderr, "extra argument: \"%s\"\n", z);
@@ -20640,17 +20715,21 @@
2064020715
p->szMax = 0;
2064120716
2064220717
/* If a filename is specified, try to open it first */
2064320718
if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
2064420719
if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
20720
+#ifndef SQLITE_SHELL_WASM_MODE
2064520721
if( p->bSafeMode
2064620722
&& p->openMode!=SHELL_OPEN_HEXDB
2064720723
&& zFN
2064820724
&& strcmp(zFN,":memory:")!=0
2064920725
){
2065020726
failIfSafeMode(p, "cannot open disk-based database files in safe mode");
2065120727
}
20728
+#else
20729
+ /* WASM mode has its own sandboxed pseudo-filesystem. */
20730
+#endif
2065220731
if( zFN ){
2065320732
zNewFilename = sqlite3_mprintf("%s", zFN);
2065420733
shell_check_oom(zNewFilename);
2065520734
}else{
2065620735
zNewFilename = 0;
@@ -20669,10 +20748,11 @@
2066920748
p->pAuxDb->zDbFilename = 0;
2067020749
open_db(p, 0);
2067120750
}
2067220751
}else
2067320752
20753
+#ifndef SQLITE_SHELL_WASM_MODE
2067420754
if( (c=='o'
2067520755
&& (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
2067620756
|| (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
2067720757
){
2067820758
char *zFile = 0;
@@ -20784,10 +20864,11 @@
2078420864
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
2078520865
}
2078620866
}
2078720867
sqlite3_free(zFile);
2078820868
}else
20869
+#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
2078920870
2079020871
if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
2079120872
open_db(p,0);
2079220873
if( nArg<=1 ) goto parameter_syntax_error;
2079320874
@@ -20953,14 +21034,17 @@
2095321034
if( nArg >= 3) {
2095421035
strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
2095521036
}
2095621037
}else
2095721038
21039
+#ifndef SQLITE_SHELL_WASM_MODE
2095821040
if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
2095921041
rc = 2;
2096021042
}else
21043
+#endif
2096121044
21045
+#ifndef SQLITE_SHELL_WASM_MODE
2096221046
if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
2096321047
FILE *inSaved = p->in;
2096421048
int savedLineno = p->lineno;
2096521049
failIfSafeMode(p, "cannot run .read in safe mode");
2096621050
if( nArg!=2 ){
@@ -20991,11 +21075,13 @@
2099121075
fclose(p->in);
2099221076
}
2099321077
p->in = inSaved;
2099421078
p->lineno = savedLineno;
2099521079
}else
21080
+#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
2099621081
21082
+#ifndef SQLITE_SHELL_WASM_MODE
2099721083
if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
2099821084
const char *zSrcFile;
2099921085
const char *zDb;
2100021086
sqlite3 *pSrc;
2100121087
sqlite3_backup *pBackup;
@@ -21043,10 +21129,11 @@
2104321129
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
2104421130
rc = 1;
2104521131
}
2104621132
close_db(pSrc);
2104721133
}else
21134
+#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
2104821135
2104921136
if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
2105021137
if( nArg==2 ){
2105121138
p->scanstatsOn = (u8)booleanValue(azArg[1]);
2105221139
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
@@ -21668,11 +21755,11 @@
2166821755
shell_exec(p, zSql, 0);
2166921756
}
2167021757
sqlite3_free(zSql);
2167121758
}else
2167221759
21673
-#ifndef SQLITE_NOHAVE_SYSTEM
21760
+#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
2167421761
if( c=='s'
2167521762
&& (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
2167621763
){
2167721764
char *zCmd;
2167821765
int i, x;
@@ -21689,11 +21776,11 @@
2168921776
}
2169021777
x = zCmd!=0 ? system(zCmd) : 1;
2169121778
sqlite3_free(zCmd);
2169221779
if( x ) raw_printf(stderr, "System command returns %d\n", x);
2169321780
}else
21694
-#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
21781
+#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE) */
2169521782
2169621783
if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
2169721784
static const char *azBool[] = { "off", "on", "trigger", "full"};
2169821785
const char *zOut;
2169921786
int i;
@@ -21869,10 +21956,11 @@
2186921956
2187021957
for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
2187121958
sqlite3_free(azResult);
2187221959
}else
2187321960
21961
+#ifndef SQLITE_SHELL_WASM_MODE
2187421962
/* Begin redirecting output to the file "testcase-out.txt" */
2187521963
if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
2187621964
output_reset(p);
2187721965
p->out = output_file_open("testcase-out.txt", 0);
2187821966
if( p->out==0 ){
@@ -21882,10 +21970,11 @@
2188221970
sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
2188321971
}else{
2188421972
sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
2188521973
}
2188621974
}else
21975
+#endif /* !defined(SQLITE_SHELL_WASM_MODE) */
2188721976
2188821977
#ifndef SQLITE_UNTESTABLE
2188921978
if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
2189021979
static const struct {
2189121980
const char *zCtrlName; /* Name of a test-control option */
@@ -22553,10 +22642,43 @@
2255322642
2255422643
static void echo_group_input(ShellState *p, const char *zDo){
2255522644
if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
2255622645
}
2255722646
22647
+#ifdef SQLITE_SHELL_WASM_MODE
22648
+/*
22649
+** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
22650
+** because we need the global shellState and cannot access it from that function
22651
+** without moving lots of code around (creating a larger/messier diff).
22652
+*/
22653
+static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
22654
+ /* Parse the next line from shellState.wasm.zInput. */
22655
+ const char *zBegin = shellState.wasm.zPos;
22656
+ const char *z = zBegin;
22657
+ char *zLine = 0;
22658
+ int nZ = 0;
22659
+
22660
+ UNUSED_PARAMETER(in);
22661
+ UNUSED_PARAMETER(isContinuation);
22662
+ if(!z || !*z){
22663
+ return 0;
22664
+ }
22665
+ while(*z && isspace(*z)) ++z;
22666
+ zBegin = z;
22667
+ for(; *z && '\n'!=*z; ++nZ, ++z){}
22668
+ if(nZ>0 && '\r'==zBegin[nZ-1]){
22669
+ --nZ;
22670
+ }
22671
+ shellState.wasm.zPos = z;
22672
+ zLine = realloc(zPrior, nZ+1);
22673
+ shell_check_oom(zLine);
22674
+ memcpy(zLine, zBegin, (size_t)nZ);
22675
+ zLine[nZ] = 0;
22676
+ return zLine;
22677
+}
22678
+#endif /* SQLITE_SHELL_WASM_MODE */
22679
+
2255822680
/*
2255922681
** Read input from *in and process it. If *in==0 then input
2256022682
** is interactive - the user is typing it it. Otherwise, input
2256122683
** is coming from a file or device. A prompt is issued and history
2256222684
** is saved only if input is interactive. An interrupt signal will
@@ -22933,10 +23055,14 @@
2293323055
# define SQLITE_SHELL_IS_UTF8 (0)
2293423056
# else
2293523057
# define SQLITE_SHELL_IS_UTF8 (1)
2293623058
# endif
2293723059
#endif
23060
+
23061
+#ifdef SQLITE_SHELL_WASM_MODE
23062
+# define main fiddle_main
23063
+#endif
2293823064
2293923065
#if SQLITE_SHELL_IS_UTF8
2294023066
int SQLITE_CDECL main(int argc, char **argv){
2294123067
#else
2294223068
int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
@@ -22944,11 +23070,15 @@
2294423070
#endif
2294523071
#ifdef SQLITE_DEBUG
2294623072
sqlite3_uint64 mem_main_enter = sqlite3_memory_used();
2294723073
#endif
2294823074
char *zErrMsg = 0;
23075
+#ifdef SQLITE_SHELL_WASM_MODE
23076
+# define data shellState
23077
+#else
2294923078
ShellState data;
23079
+#endif
2295023080
const char *zInitFile = 0;
2295123081
int i;
2295223082
int rc = 0;
2295323083
int warnInmemoryDb = 0;
2295423084
int readStdin = 1;
@@ -22960,12 +23090,17 @@
2296023090
int argcToFree = 0;
2296123091
#endif
2296223092
2296323093
setBinaryMode(stdin, 0);
2296423094
setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
23095
+#ifdef SQLITE_SHELL_WASM_MODE
23096
+ stdin_is_interactive = 0;
23097
+ stdout_is_console = 1;
23098
+#else
2296523099
stdin_is_interactive = isatty(0);
2296623100
stdout_is_console = isatty(1);
23101
+#endif
2296723102
2296823103
#if !defined(_WIN32_WCE)
2296923104
if( getenv("SQLITE_DEBUG_BREAK") ){
2297023105
if( isatty(0) && isatty(2) ){
2297123106
fprintf(stderr,
@@ -23217,11 +23352,13 @@
2321723352
utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
2321823353
return 1;
2321923354
#endif
2322023355
}
2322123356
data.out = stdout;
23357
+#ifndef SQLITE_SHELL_WASM_MODE
2322223358
sqlite3_appendvfs_init(0,0,0);
23359
+#endif
2322323360
2322423361
/* Go ahead and open the database file if it already exists. If the
2322523362
** file does not exist, delay opening it. This prevents empty database
2322623363
** files from being created if a user mistypes the database name argument
2322723364
** to the sqlite command-line tool.
@@ -23483,10 +23620,13 @@
2348323620
}else{
2348423621
data.in = stdin;
2348523622
rc = process_input(&data);
2348623623
}
2348723624
}
23625
+#ifndef SQLITE_SHELL_WASM_MODE
23626
+ /* In WASM mode we have to leave the db state in place so that
23627
+ ** client code can "push" SQL into it after this call returns. */
2348823628
free(azCmd);
2348923629
set_table_name(&data, 0);
2349023630
if( data.db ){
2349123631
session_close_all(&data, -1);
2349223632
close_db(data.db);
@@ -23515,7 +23655,110 @@
2351523655
if( sqlite3_memory_used()>mem_main_enter ){
2351623656
utf8_printf(stderr, "Memory leaked: %u bytes\n",
2351723657
(unsigned int)(sqlite3_memory_used()-mem_main_enter));
2351823658
}
2351923659
#endif
23660
+#endif /* !SQLITE_SHELL_WASM_MODE */
2352023661
return rc;
2352123662
}
23663
+
23664
+
23665
+#ifdef SQLITE_SHELL_WASM_MODE
23666
+/* Only for emcc experimentation purposes. */
23667
+int fiddle_experiment(int a,int b){
23668
+ return a + b;
23669
+}
23670
+
23671
+/* Only for emcc experimentation purposes.
23672
+
23673
+ Define this function in JS using:
23674
+
23675
+ emcc ... --js-library somefile.js
23676
+
23677
+ containing:
23678
+
23679
+mergeInto(LibraryManager.library, {
23680
+ my_foo: function(){
23681
+ console.debug("my_foo()",arguments);
23682
+ }
23683
+});
23684
+*/
23685
+/*extern void my_foo(sqlite3 *);*/
23686
+/* Only for emcc experimentation purposes. */
23687
+sqlite3 * fiddle_the_db(){
23688
+ printf("fiddle_the_db(%p)\n", (const void*)globalDb);
23689
+ /*my_foo(globalDb);*/
23690
+ return globalDb;
23691
+}
23692
+/* Only for emcc experimentation purposes. */
23693
+sqlite3 * fiddle_db_arg(sqlite3 *arg){
23694
+ printf("fiddle_db_arg(%p)\n", (const void*)arg);
23695
+ return arg;
23696
+}
23697
+
23698
+/*
23699
+** Intended to be called via a SharedWorker() while a separate
23700
+** SharedWorker() (which manages the wasm module) is performing work
23701
+** which should be interrupted. Unfortunately, SharedWorker is not
23702
+** portable enough to make real use of.
23703
+*/
23704
+void fiddle_interrupt(void){
23705
+ if(globalDb) sqlite3_interrupt(globalDb);
23706
+}
23707
+
23708
+/*
23709
+** Returns the filename of the given db name, assuming "main" if
23710
+** zDbName is NULL. Returns NULL if globalDb is not opened.
23711
+*/
23712
+const char * fiddle_db_filename(const char * zDbName){
23713
+ return globalDb
23714
+ ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
23715
+ : NULL;
23716
+}
23717
+
23718
+/*
23719
+** Trivial exportable function for emscripten. Needs to be exported using:
23720
+**
23721
+** emcc ..flags... -sEXPORTED_FUNCTIONS=_fiddle_exec -sEXPORTED_RUNTIME_METHODS=ccall,cwrap
23722
+**
23723
+** (Note the underscore before the function name.) It processes zSql
23724
+** as if it were input to the sqlite3 shell and redirects all output
23725
+** to the wasm binding.
23726
+*/
23727
+void fiddle_exec(const char * zSql){
23728
+ static int once = 0;
23729
+ int rc = 0;
23730
+ if(!once){
23731
+ /* Simulate an argv array for main() */
23732
+ static char * argv[] = {"fiddle",
23733
+ "-bail",
23734
+ "-safe"};
23735
+ rc = fiddle_main((int)(sizeof(argv)/sizeof(argv[0])), argv);
23736
+ once = rc ? -1 : 1;
23737
+ memset(&shellState.wasm, 0, sizeof(shellState.wasm));
23738
+ printf(
23739
+ "SQLite version %s %.19s\n" /*extra-version-info*/,
23740
+ sqlite3_libversion(), sqlite3_sourceid()
23741
+ );
23742
+ puts("WASM shell");
23743
+ puts("Enter \".help\" for usage hints.");
23744
+ if(once>0){
23745
+ shellState.pAuxDb->zDbFilename = "/fiddle.sqlite3";
23746
+ open_db(&shellState, 0);
23747
+ }
23748
+ if(shellState.db){
23749
+ printf("Connected to %s.\n", fiddle_db_filename(NULL));
23750
+ }else{
23751
+ fprintf(stderr,"ERROR initializing db!\n");
23752
+ return;
23753
+ }
23754
+ }
23755
+ if(once<0){
23756
+ puts("DB init failed. Not executing SQL.");
23757
+ }else if(zSql && *zSql){
23758
+ shellState.wasm.zInput = zSql;
23759
+ shellState.wasm.zPos = zSql;
23760
+ process_input(&shellState);
23761
+ memset(&shellState.wasm, 0, sizeof(shellState.wasm));
23762
+ }
23763
+}
23764
+#endif /* SQLITE_SHELL_WASM_MODE */
2352223765
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -245,10 +245,20 @@
245 #else
246 # define setBinaryMode(X,Y)
247 # define setTextMode(X,Y)
248 #endif
249
 
 
 
 
 
 
 
 
 
 
250
251 /* True if the timer is enabled */
252 static int enableTimer = 0;
253
254 /* Return the current wall-clock time */
@@ -707,10 +717,11 @@
707 **
708 ** The result is stored in space obtained from malloc() and must either
709 ** be freed by the caller or else passed back into this routine via the
710 ** zPrior argument for reuse.
711 */
 
712 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
713 char *zPrompt;
714 char *zResult;
715 if( in!=0 ){
716 zResult = local_getline(zPrior, in);
@@ -726,11 +737,11 @@
726 if( zResult && *zResult ) shell_add_history(zResult);
727 #endif
728 }
729 return zResult;
730 }
731
732
733 /*
734 ** Return the value of a hexadecimal digit. Return -1 if the input
735 ** is not a hex digit.
736 */
@@ -814,11 +825,11 @@
814 ** zIn, if it was not NULL, is freed.
815 **
816 ** If the third argument, quote, is not '\0', then it is used as a
817 ** quote character for zAppend.
818 */
819 static void appendText(ShellText *p, char const *zAppend, char quote){
820 int len;
821 int i;
822 int nAppend = strlen30(zAppend);
823
824 len = nAppend+p->n+1;
@@ -1379,10 +1390,121 @@
1379 #endif /* defined(WIN32) && defined(_MSC_VER) */
1380
1381 /************************* End test_windirent.c ********************/
1382 #define dirent DIRENT
1383 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1384 /************************* Begin ../ext/misc/shathree.c ******************/
1385 /*
1386 ** 2017-03-08
1387 **
1388 ** The author disclaims copyright to this source code. In place of
@@ -2106,2333 +2228,10 @@
2106 }
2107 return rc;
2108 }
2109
2110 /************************* End ../ext/misc/shathree.c ********************/
2111 /************************* Begin ../ext/misc/fileio.c ******************/
2112 /*
2113 ** 2014-06-13
2114 **
2115 ** The author disclaims copyright to this source code. In place of
2116 ** a legal notice, here is a blessing:
2117 **
2118 ** May you do good and not evil.
2119 ** May you find forgiveness for yourself and forgive others.
2120 ** May you share freely, never taking more than you give.
2121 **
2122 ******************************************************************************
2123 **
2124 ** This SQLite extension implements SQL functions readfile() and
2125 ** writefile(), and eponymous virtual type "fsdir".
2126 **
2127 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2128 **
2129 ** If neither of the optional arguments is present, then this UDF
2130 ** function writes blob DATA to file FILE. If successful, the number
2131 ** of bytes written is returned. If an error occurs, NULL is returned.
2132 **
2133 ** If the first option argument - MODE - is present, then it must
2134 ** be passed an integer value that corresponds to a POSIX mode
2135 ** value (file type + permissions, as returned in the stat.st_mode
2136 ** field by the stat() system call). Three types of files may
2137 ** be written/created:
2138 **
2139 ** regular files: (mode & 0170000)==0100000
2140 ** symbolic links: (mode & 0170000)==0120000
2141 ** directories: (mode & 0170000)==0040000
2142 **
2143 ** For a directory, the DATA is ignored. For a symbolic link, it is
2144 ** interpreted as text and used as the target of the link. For a
2145 ** regular file, it is interpreted as a blob and written into the
2146 ** named file. Regardless of the type of file, its permissions are
2147 ** set to (mode & 0777) before returning.
2148 **
2149 ** If the optional MTIME argument is present, then it is interpreted
2150 ** as an integer - the number of seconds since the unix epoch. The
2151 ** modification-time of the target file is set to this value before
2152 ** returning.
2153 **
2154 ** If three or more arguments are passed to this function and an
2155 ** error is encountered, an exception is raised.
2156 **
2157 ** READFILE(FILE):
2158 **
2159 ** Read and return the contents of file FILE (type blob) from disk.
2160 **
2161 ** FSDIR:
2162 **
2163 ** Used as follows:
2164 **
2165 ** SELECT * FROM fsdir($path [, $dir]);
2166 **
2167 ** Parameter $path is an absolute or relative pathname. If the file that it
2168 ** refers to does not exist, it is an error. If the path refers to a regular
2169 ** file or symbolic link, it returns a single row. Or, if the path refers
2170 ** to a directory, it returns one row for the directory, and one row for each
2171 ** file within the hierarchy rooted at $path.
2172 **
2173 ** Each row has the following columns:
2174 **
2175 ** name: Path to file or directory (text value).
2176 ** mode: Value of stat.st_mode for directory entry (an integer).
2177 ** mtime: Value of stat.st_mtime for directory entry (an integer).
2178 ** data: For a regular file, a blob containing the file data. For a
2179 ** symlink, a text value containing the text of the link. For a
2180 ** directory, NULL.
2181 **
2182 ** If a non-NULL value is specified for the optional $dir parameter and
2183 ** $path is a relative path, then $path is interpreted relative to $dir.
2184 ** And the paths returned in the "name" column of the table are also
2185 ** relative to directory $dir.
2186 **
2187 ** Notes on building this extension for Windows:
2188 ** Unless linked statically with the SQLite library, a preprocessor
2189 ** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
2190 ** DLL form of this extension for WIN32. See its use below for details.
2191 */
2192 /* #include "sqlite3ext.h" */
2193 SQLITE_EXTENSION_INIT1
2194 #include <stdio.h>
2195 #include <string.h>
2196 #include <assert.h>
2197
2198 #include <sys/types.h>
2199 #include <sys/stat.h>
2200 #include <fcntl.h>
2201 #if !defined(_WIN32) && !defined(WIN32)
2202 # include <unistd.h>
2203 # include <dirent.h>
2204 # include <utime.h>
2205 # include <sys/time.h>
2206 #else
2207 # include "windows.h"
2208 # include <io.h>
2209 # include <direct.h>
2210 /* # include "test_windirent.h" */
2211 # define dirent DIRENT
2212 # ifndef chmod
2213 # define chmod _chmod
2214 # endif
2215 # ifndef stat
2216 # define stat _stat
2217 # endif
2218 # define mkdir(path,mode) _mkdir(path)
2219 # define lstat(path,buf) stat(path,buf)
2220 #endif
2221 #include <time.h>
2222 #include <errno.h>
2223
2224
2225 /*
2226 ** Structure of the fsdir() table-valued function
2227 */
2228 /* 0 1 2 3 4 5 */
2229 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2230 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
2231 #define FSDIR_COLUMN_MODE 1 /* Access mode */
2232 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
2233 #define FSDIR_COLUMN_DATA 3 /* File content */
2234 #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
2235 #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
2236
2237
2238 /*
2239 ** Set the result stored by context ctx to a blob containing the
2240 ** contents of file zName. Or, leave the result unchanged (NULL)
2241 ** if the file does not exist or is unreadable.
2242 **
2243 ** If the file exceeds the SQLite blob size limit, through an
2244 ** SQLITE_TOOBIG error.
2245 **
2246 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2247 ** off of disk.
2248 */
2249 static void readFileContents(sqlite3_context *ctx, const char *zName){
2250 FILE *in;
2251 sqlite3_int64 nIn;
2252 void *pBuf;
2253 sqlite3 *db;
2254 int mxBlob;
2255
2256 in = fopen(zName, "rb");
2257 if( in==0 ){
2258 /* File does not exist or is unreadable. Leave the result set to NULL. */
2259 return;
2260 }
2261 fseek(in, 0, SEEK_END);
2262 nIn = ftell(in);
2263 rewind(in);
2264 db = sqlite3_context_db_handle(ctx);
2265 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2266 if( nIn>mxBlob ){
2267 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2268 fclose(in);
2269 return;
2270 }
2271 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2272 if( pBuf==0 ){
2273 sqlite3_result_error_nomem(ctx);
2274 fclose(in);
2275 return;
2276 }
2277 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2278 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2279 }else{
2280 sqlite3_result_error_code(ctx, SQLITE_IOERR);
2281 sqlite3_free(pBuf);
2282 }
2283 fclose(in);
2284 }
2285
2286 /*
2287 ** Implementation of the "readfile(X)" SQL function. The entire content
2288 ** of the file named X is read and returned as a BLOB. NULL is returned
2289 ** if the file does not exist or is unreadable.
2290 */
2291 static void readfileFunc(
2292 sqlite3_context *context,
2293 int argc,
2294 sqlite3_value **argv
2295 ){
2296 const char *zName;
2297 (void)(argc); /* Unused parameter */
2298 zName = (const char*)sqlite3_value_text(argv[0]);
2299 if( zName==0 ) return;
2300 readFileContents(context, zName);
2301 }
2302
2303 /*
2304 ** Set the error message contained in context ctx to the results of
2305 ** vprintf(zFmt, ...).
2306 */
2307 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2308 char *zMsg = 0;
2309 va_list ap;
2310 va_start(ap, zFmt);
2311 zMsg = sqlite3_vmprintf(zFmt, ap);
2312 sqlite3_result_error(ctx, zMsg, -1);
2313 sqlite3_free(zMsg);
2314 va_end(ap);
2315 }
2316
2317 #if defined(_WIN32)
2318 /*
2319 ** This function is designed to convert a Win32 FILETIME structure into the
2320 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2321 */
2322 static sqlite3_uint64 fileTimeToUnixTime(
2323 LPFILETIME pFileTime
2324 ){
2325 SYSTEMTIME epochSystemTime;
2326 ULARGE_INTEGER epochIntervals;
2327 FILETIME epochFileTime;
2328 ULARGE_INTEGER fileIntervals;
2329
2330 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2331 epochSystemTime.wYear = 1970;
2332 epochSystemTime.wMonth = 1;
2333 epochSystemTime.wDay = 1;
2334 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2335 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2336 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2337
2338 fileIntervals.LowPart = pFileTime->dwLowDateTime;
2339 fileIntervals.HighPart = pFileTime->dwHighDateTime;
2340
2341 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2342 }
2343
2344
2345 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
2346 # /* To allow a standalone DLL, use this next replacement function: */
2347 # undef sqlite3_win32_utf8_to_unicode
2348 # define sqlite3_win32_utf8_to_unicode utf8_to_utf16
2349 #
2350 LPWSTR utf8_to_utf16(const char *z){
2351 int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0);
2352 LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR));
2353 if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) )
2354 return rv;
2355 sqlite3_free(rv);
2356 return 0;
2357 }
2358 #endif
2359
2360 /*
2361 ** This function attempts to normalize the time values found in the stat()
2362 ** buffer to UTC. This is necessary on Win32, where the runtime library
2363 ** appears to return these values as local times.
2364 */
2365 static void statTimesToUtc(
2366 const char *zPath,
2367 struct stat *pStatBuf
2368 ){
2369 HANDLE hFindFile;
2370 WIN32_FIND_DATAW fd;
2371 LPWSTR zUnicodeName;
2372 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2373 zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2374 if( zUnicodeName ){
2375 memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2376 hFindFile = FindFirstFileW(zUnicodeName, &fd);
2377 if( hFindFile!=NULL ){
2378 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2379 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2380 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2381 FindClose(hFindFile);
2382 }
2383 sqlite3_free(zUnicodeName);
2384 }
2385 }
2386 #endif
2387
2388 /*
2389 ** This function is used in place of stat(). On Windows, special handling
2390 ** is required in order for the included time to be returned as UTC. On all
2391 ** other systems, this function simply calls stat().
2392 */
2393 static int fileStat(
2394 const char *zPath,
2395 struct stat *pStatBuf
2396 ){
2397 #if defined(_WIN32)
2398 int rc = stat(zPath, pStatBuf);
2399 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2400 return rc;
2401 #else
2402 return stat(zPath, pStatBuf);
2403 #endif
2404 }
2405
2406 /*
2407 ** This function is used in place of lstat(). On Windows, special handling
2408 ** is required in order for the included time to be returned as UTC. On all
2409 ** other systems, this function simply calls lstat().
2410 */
2411 static int fileLinkStat(
2412 const char *zPath,
2413 struct stat *pStatBuf
2414 ){
2415 #if defined(_WIN32)
2416 int rc = lstat(zPath, pStatBuf);
2417 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2418 return rc;
2419 #else
2420 return lstat(zPath, pStatBuf);
2421 #endif
2422 }
2423
2424 /*
2425 ** Argument zFile is the name of a file that will be created and/or written
2426 ** by SQL function writefile(). This function ensures that the directory
2427 ** zFile will be written to exists, creating it if required. The permissions
2428 ** for any path components created by this function are set in accordance
2429 ** with the current umask.
2430 **
2431 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2432 ** SQLITE_OK is returned if the directory is successfully created, or
2433 ** SQLITE_ERROR otherwise.
2434 */
2435 static int makeDirectory(
2436 const char *zFile
2437 ){
2438 char *zCopy = sqlite3_mprintf("%s", zFile);
2439 int rc = SQLITE_OK;
2440
2441 if( zCopy==0 ){
2442 rc = SQLITE_NOMEM;
2443 }else{
2444 int nCopy = (int)strlen(zCopy);
2445 int i = 1;
2446
2447 while( rc==SQLITE_OK ){
2448 struct stat sStat;
2449 int rc2;
2450
2451 for(; zCopy[i]!='/' && i<nCopy; i++);
2452 if( i==nCopy ) break;
2453 zCopy[i] = '\0';
2454
2455 rc2 = fileStat(zCopy, &sStat);
2456 if( rc2!=0 ){
2457 if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2458 }else{
2459 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2460 }
2461 zCopy[i] = '/';
2462 i++;
2463 }
2464
2465 sqlite3_free(zCopy);
2466 }
2467
2468 return rc;
2469 }
2470
2471 /*
2472 ** This function does the work for the writefile() UDF. Refer to
2473 ** header comments at the top of this file for details.
2474 */
2475 static int writeFile(
2476 sqlite3_context *pCtx, /* Context to return bytes written in */
2477 const char *zFile, /* File to write */
2478 sqlite3_value *pData, /* Data to write */
2479 mode_t mode, /* MODE parameter passed to writefile() */
2480 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2481 ){
2482 if( zFile==0 ) return 1;
2483 #if !defined(_WIN32) && !defined(WIN32)
2484 if( S_ISLNK(mode) ){
2485 const char *zTo = (const char*)sqlite3_value_text(pData);
2486 if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
2487 }else
2488 #endif
2489 {
2490 if( S_ISDIR(mode) ){
2491 if( mkdir(zFile, mode) ){
2492 /* The mkdir() call to create the directory failed. This might not
2493 ** be an error though - if there is already a directory at the same
2494 ** path and either the permissions already match or can be changed
2495 ** to do so using chmod(), it is not an error. */
2496 struct stat sStat;
2497 if( errno!=EEXIST
2498 || 0!=fileStat(zFile, &sStat)
2499 || !S_ISDIR(sStat.st_mode)
2500 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2501 ){
2502 return 1;
2503 }
2504 }
2505 }else{
2506 sqlite3_int64 nWrite = 0;
2507 const char *z;
2508 int rc = 0;
2509 FILE *out = fopen(zFile, "wb");
2510 if( out==0 ) return 1;
2511 z = (const char*)sqlite3_value_blob(pData);
2512 if( z ){
2513 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2514 nWrite = sqlite3_value_bytes(pData);
2515 if( nWrite!=n ){
2516 rc = 1;
2517 }
2518 }
2519 fclose(out);
2520 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2521 rc = 1;
2522 }
2523 if( rc ) return 2;
2524 sqlite3_result_int64(pCtx, nWrite);
2525 }
2526 }
2527
2528 if( mtime>=0 ){
2529 #if defined(_WIN32)
2530 #if !SQLITE_OS_WINRT
2531 /* Windows */
2532 FILETIME lastAccess;
2533 FILETIME lastWrite;
2534 SYSTEMTIME currentTime;
2535 LONGLONG intervals;
2536 HANDLE hFile;
2537 LPWSTR zUnicodeName;
2538 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2539
2540 GetSystemTime(&currentTime);
2541 SystemTimeToFileTime(&currentTime, &lastAccess);
2542 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2543 lastWrite.dwLowDateTime = (DWORD)intervals;
2544 lastWrite.dwHighDateTime = intervals >> 32;
2545 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2546 if( zUnicodeName==0 ){
2547 return 1;
2548 }
2549 hFile = CreateFileW(
2550 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2551 FILE_FLAG_BACKUP_SEMANTICS, NULL
2552 );
2553 sqlite3_free(zUnicodeName);
2554 if( hFile!=INVALID_HANDLE_VALUE ){
2555 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2556 CloseHandle(hFile);
2557 return !bResult;
2558 }else{
2559 return 1;
2560 }
2561 #endif
2562 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2563 /* Recent unix */
2564 struct timespec times[2];
2565 times[0].tv_nsec = times[1].tv_nsec = 0;
2566 times[0].tv_sec = time(0);
2567 times[1].tv_sec = mtime;
2568 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2569 return 1;
2570 }
2571 #else
2572 /* Legacy unix */
2573 struct timeval times[2];
2574 times[0].tv_usec = times[1].tv_usec = 0;
2575 times[0].tv_sec = time(0);
2576 times[1].tv_sec = mtime;
2577 if( utimes(zFile, times) ){
2578 return 1;
2579 }
2580 #endif
2581 }
2582
2583 return 0;
2584 }
2585
2586 /*
2587 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2588 ** Refer to header comments at the top of this file for details.
2589 */
2590 static void writefileFunc(
2591 sqlite3_context *context,
2592 int argc,
2593 sqlite3_value **argv
2594 ){
2595 const char *zFile;
2596 mode_t mode = 0;
2597 int res;
2598 sqlite3_int64 mtime = -1;
2599
2600 if( argc<2 || argc>4 ){
2601 sqlite3_result_error(context,
2602 "wrong number of arguments to function writefile()", -1
2603 );
2604 return;
2605 }
2606
2607 zFile = (const char*)sqlite3_value_text(argv[0]);
2608 if( zFile==0 ) return;
2609 if( argc>=3 ){
2610 mode = (mode_t)sqlite3_value_int(argv[2]);
2611 }
2612 if( argc==4 ){
2613 mtime = sqlite3_value_int64(argv[3]);
2614 }
2615
2616 res = writeFile(context, zFile, argv[1], mode, mtime);
2617 if( res==1 && errno==ENOENT ){
2618 if( makeDirectory(zFile)==SQLITE_OK ){
2619 res = writeFile(context, zFile, argv[1], mode, mtime);
2620 }
2621 }
2622
2623 if( argc>2 && res!=0 ){
2624 if( S_ISLNK(mode) ){
2625 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2626 }else if( S_ISDIR(mode) ){
2627 ctxErrorMsg(context, "failed to create directory: %s", zFile);
2628 }else{
2629 ctxErrorMsg(context, "failed to write file: %s", zFile);
2630 }
2631 }
2632 }
2633
2634 /*
2635 ** SQL function: lsmode(MODE)
2636 **
2637 ** Given a numberic st_mode from stat(), convert it into a human-readable
2638 ** text string in the style of "ls -l".
2639 */
2640 static void lsModeFunc(
2641 sqlite3_context *context,
2642 int argc,
2643 sqlite3_value **argv
2644 ){
2645 int i;
2646 int iMode = sqlite3_value_int(argv[0]);
2647 char z[16];
2648 (void)argc;
2649 if( S_ISLNK(iMode) ){
2650 z[0] = 'l';
2651 }else if( S_ISREG(iMode) ){
2652 z[0] = '-';
2653 }else if( S_ISDIR(iMode) ){
2654 z[0] = 'd';
2655 }else{
2656 z[0] = '?';
2657 }
2658 for(i=0; i<3; i++){
2659 int m = (iMode >> ((2-i)*3));
2660 char *a = &z[1 + i*3];
2661 a[0] = (m & 0x4) ? 'r' : '-';
2662 a[1] = (m & 0x2) ? 'w' : '-';
2663 a[2] = (m & 0x1) ? 'x' : '-';
2664 }
2665 z[10] = '\0';
2666 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2667 }
2668
2669 #ifndef SQLITE_OMIT_VIRTUALTABLE
2670
2671 /*
2672 ** Cursor type for recursively iterating through a directory structure.
2673 */
2674 typedef struct fsdir_cursor fsdir_cursor;
2675 typedef struct FsdirLevel FsdirLevel;
2676
2677 struct FsdirLevel {
2678 DIR *pDir; /* From opendir() */
2679 char *zDir; /* Name of directory (nul-terminated) */
2680 };
2681
2682 struct fsdir_cursor {
2683 sqlite3_vtab_cursor base; /* Base class - must be first */
2684
2685 int nLvl; /* Number of entries in aLvl[] array */
2686 int iLvl; /* Index of current entry */
2687 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2688
2689 const char *zBase;
2690 int nBase;
2691
2692 struct stat sStat; /* Current lstat() results */
2693 char *zPath; /* Path to current entry */
2694 sqlite3_int64 iRowid; /* Current rowid */
2695 };
2696
2697 typedef struct fsdir_tab fsdir_tab;
2698 struct fsdir_tab {
2699 sqlite3_vtab base; /* Base class - must be first */
2700 };
2701
2702 /*
2703 ** Construct a new fsdir virtual table object.
2704 */
2705 static int fsdirConnect(
2706 sqlite3 *db,
2707 void *pAux,
2708 int argc, const char *const*argv,
2709 sqlite3_vtab **ppVtab,
2710 char **pzErr
2711 ){
2712 fsdir_tab *pNew = 0;
2713 int rc;
2714 (void)pAux;
2715 (void)argc;
2716 (void)argv;
2717 (void)pzErr;
2718 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2719 if( rc==SQLITE_OK ){
2720 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2721 if( pNew==0 ) return SQLITE_NOMEM;
2722 memset(pNew, 0, sizeof(*pNew));
2723 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2724 }
2725 *ppVtab = (sqlite3_vtab*)pNew;
2726 return rc;
2727 }
2728
2729 /*
2730 ** This method is the destructor for fsdir vtab objects.
2731 */
2732 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2733 sqlite3_free(pVtab);
2734 return SQLITE_OK;
2735 }
2736
2737 /*
2738 ** Constructor for a new fsdir_cursor object.
2739 */
2740 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2741 fsdir_cursor *pCur;
2742 (void)p;
2743 pCur = sqlite3_malloc( sizeof(*pCur) );
2744 if( pCur==0 ) return SQLITE_NOMEM;
2745 memset(pCur, 0, sizeof(*pCur));
2746 pCur->iLvl = -1;
2747 *ppCursor = &pCur->base;
2748 return SQLITE_OK;
2749 }
2750
2751 /*
2752 ** Reset a cursor back to the state it was in when first returned
2753 ** by fsdirOpen().
2754 */
2755 static void fsdirResetCursor(fsdir_cursor *pCur){
2756 int i;
2757 for(i=0; i<=pCur->iLvl; i++){
2758 FsdirLevel *pLvl = &pCur->aLvl[i];
2759 if( pLvl->pDir ) closedir(pLvl->pDir);
2760 sqlite3_free(pLvl->zDir);
2761 }
2762 sqlite3_free(pCur->zPath);
2763 sqlite3_free(pCur->aLvl);
2764 pCur->aLvl = 0;
2765 pCur->zPath = 0;
2766 pCur->zBase = 0;
2767 pCur->nBase = 0;
2768 pCur->nLvl = 0;
2769 pCur->iLvl = -1;
2770 pCur->iRowid = 1;
2771 }
2772
2773 /*
2774 ** Destructor for an fsdir_cursor.
2775 */
2776 static int fsdirClose(sqlite3_vtab_cursor *cur){
2777 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2778
2779 fsdirResetCursor(pCur);
2780 sqlite3_free(pCur);
2781 return SQLITE_OK;
2782 }
2783
2784 /*
2785 ** Set the error message for the virtual table associated with cursor
2786 ** pCur to the results of vprintf(zFmt, ...).
2787 */
2788 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2789 va_list ap;
2790 va_start(ap, zFmt);
2791 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2792 va_end(ap);
2793 }
2794
2795
2796 /*
2797 ** Advance an fsdir_cursor to its next row of output.
2798 */
2799 static int fsdirNext(sqlite3_vtab_cursor *cur){
2800 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2801 mode_t m = pCur->sStat.st_mode;
2802
2803 pCur->iRowid++;
2804 if( S_ISDIR(m) ){
2805 /* Descend into this directory */
2806 int iNew = pCur->iLvl + 1;
2807 FsdirLevel *pLvl;
2808 if( iNew>=pCur->nLvl ){
2809 int nNew = iNew+1;
2810 sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2811 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2812 if( aNew==0 ) return SQLITE_NOMEM;
2813 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2814 pCur->aLvl = aNew;
2815 pCur->nLvl = nNew;
2816 }
2817 pCur->iLvl = iNew;
2818 pLvl = &pCur->aLvl[iNew];
2819
2820 pLvl->zDir = pCur->zPath;
2821 pCur->zPath = 0;
2822 pLvl->pDir = opendir(pLvl->zDir);
2823 if( pLvl->pDir==0 ){
2824 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2825 return SQLITE_ERROR;
2826 }
2827 }
2828
2829 while( pCur->iLvl>=0 ){
2830 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2831 struct dirent *pEntry = readdir(pLvl->pDir);
2832 if( pEntry ){
2833 if( pEntry->d_name[0]=='.' ){
2834 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2835 if( pEntry->d_name[1]=='\0' ) continue;
2836 }
2837 sqlite3_free(pCur->zPath);
2838 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2839 if( pCur->zPath==0 ) return SQLITE_NOMEM;
2840 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2841 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2842 return SQLITE_ERROR;
2843 }
2844 return SQLITE_OK;
2845 }
2846 closedir(pLvl->pDir);
2847 sqlite3_free(pLvl->zDir);
2848 pLvl->pDir = 0;
2849 pLvl->zDir = 0;
2850 pCur->iLvl--;
2851 }
2852
2853 /* EOF */
2854 sqlite3_free(pCur->zPath);
2855 pCur->zPath = 0;
2856 return SQLITE_OK;
2857 }
2858
2859 /*
2860 ** Return values of columns for the row at which the series_cursor
2861 ** is currently pointing.
2862 */
2863 static int fsdirColumn(
2864 sqlite3_vtab_cursor *cur, /* The cursor */
2865 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2866 int i /* Which column to return */
2867 ){
2868 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2869 switch( i ){
2870 case FSDIR_COLUMN_NAME: {
2871 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2872 break;
2873 }
2874
2875 case FSDIR_COLUMN_MODE:
2876 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2877 break;
2878
2879 case FSDIR_COLUMN_MTIME:
2880 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2881 break;
2882
2883 case FSDIR_COLUMN_DATA: {
2884 mode_t m = pCur->sStat.st_mode;
2885 if( S_ISDIR(m) ){
2886 sqlite3_result_null(ctx);
2887 #if !defined(_WIN32) && !defined(WIN32)
2888 }else if( S_ISLNK(m) ){
2889 char aStatic[64];
2890 char *aBuf = aStatic;
2891 sqlite3_int64 nBuf = 64;
2892 int n;
2893
2894 while( 1 ){
2895 n = readlink(pCur->zPath, aBuf, nBuf);
2896 if( n<nBuf ) break;
2897 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2898 nBuf = nBuf*2;
2899 aBuf = sqlite3_malloc64(nBuf);
2900 if( aBuf==0 ){
2901 sqlite3_result_error_nomem(ctx);
2902 return SQLITE_NOMEM;
2903 }
2904 }
2905
2906 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2907 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2908 #endif
2909 }else{
2910 readFileContents(ctx, pCur->zPath);
2911 }
2912 }
2913 case FSDIR_COLUMN_PATH:
2914 default: {
2915 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2916 ** always return their values as NULL */
2917 break;
2918 }
2919 }
2920 return SQLITE_OK;
2921 }
2922
2923 /*
2924 ** Return the rowid for the current row. In this implementation, the
2925 ** first row returned is assigned rowid value 1, and each subsequent
2926 ** row a value 1 more than that of the previous.
2927 */
2928 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2929 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2930 *pRowid = pCur->iRowid;
2931 return SQLITE_OK;
2932 }
2933
2934 /*
2935 ** Return TRUE if the cursor has been moved off of the last
2936 ** row of output.
2937 */
2938 static int fsdirEof(sqlite3_vtab_cursor *cur){
2939 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2940 return (pCur->zPath==0);
2941 }
2942
2943 /*
2944 ** xFilter callback.
2945 **
2946 ** idxNum==1 PATH parameter only
2947 ** idxNum==2 Both PATH and DIR supplied
2948 */
2949 static int fsdirFilter(
2950 sqlite3_vtab_cursor *cur,
2951 int idxNum, const char *idxStr,
2952 int argc, sqlite3_value **argv
2953 ){
2954 const char *zDir = 0;
2955 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2956 (void)idxStr;
2957 fsdirResetCursor(pCur);
2958
2959 if( idxNum==0 ){
2960 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2961 return SQLITE_ERROR;
2962 }
2963
2964 assert( argc==idxNum && (argc==1 || argc==2) );
2965 zDir = (const char*)sqlite3_value_text(argv[0]);
2966 if( zDir==0 ){
2967 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2968 return SQLITE_ERROR;
2969 }
2970 if( argc==2 ){
2971 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2972 }
2973 if( pCur->zBase ){
2974 pCur->nBase = (int)strlen(pCur->zBase)+1;
2975 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2976 }else{
2977 pCur->zPath = sqlite3_mprintf("%s", zDir);
2978 }
2979
2980 if( pCur->zPath==0 ){
2981 return SQLITE_NOMEM;
2982 }
2983 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2984 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2985 return SQLITE_ERROR;
2986 }
2987
2988 return SQLITE_OK;
2989 }
2990
2991 /*
2992 ** SQLite will invoke this method one or more times while planning a query
2993 ** that uses the generate_series virtual table. This routine needs to create
2994 ** a query plan for each invocation and compute an estimated cost for that
2995 ** plan.
2996 **
2997 ** In this implementation idxNum is used to represent the
2998 ** query plan. idxStr is unused.
2999 **
3000 ** The query plan is represented by values of idxNum:
3001 **
3002 ** (1) The path value is supplied by argv[0]
3003 ** (2) Path is in argv[0] and dir is in argv[1]
3004 */
3005 static int fsdirBestIndex(
3006 sqlite3_vtab *tab,
3007 sqlite3_index_info *pIdxInfo
3008 ){
3009 int i; /* Loop over constraints */
3010 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
3011 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
3012 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
3013 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
3014 const struct sqlite3_index_constraint *pConstraint;
3015
3016 (void)tab;
3017 pConstraint = pIdxInfo->aConstraint;
3018 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3019 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3020 switch( pConstraint->iColumn ){
3021 case FSDIR_COLUMN_PATH: {
3022 if( pConstraint->usable ){
3023 idxPath = i;
3024 seenPath = 0;
3025 }else if( idxPath<0 ){
3026 seenPath = 1;
3027 }
3028 break;
3029 }
3030 case FSDIR_COLUMN_DIR: {
3031 if( pConstraint->usable ){
3032 idxDir = i;
3033 seenDir = 0;
3034 }else if( idxDir<0 ){
3035 seenDir = 1;
3036 }
3037 break;
3038 }
3039 }
3040 }
3041 if( seenPath || seenDir ){
3042 /* If input parameters are unusable, disallow this plan */
3043 return SQLITE_CONSTRAINT;
3044 }
3045
3046 if( idxPath<0 ){
3047 pIdxInfo->idxNum = 0;
3048 /* The pIdxInfo->estimatedCost should have been initialized to a huge
3049 ** number. Leave it unchanged. */
3050 pIdxInfo->estimatedRows = 0x7fffffff;
3051 }else{
3052 pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3053 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3054 if( idxDir>=0 ){
3055 pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3056 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3057 pIdxInfo->idxNum = 2;
3058 pIdxInfo->estimatedCost = 10.0;
3059 }else{
3060 pIdxInfo->idxNum = 1;
3061 pIdxInfo->estimatedCost = 100.0;
3062 }
3063 }
3064
3065 return SQLITE_OK;
3066 }
3067
3068 /*
3069 ** Register the "fsdir" virtual table.
3070 */
3071 static int fsdirRegister(sqlite3 *db){
3072 static sqlite3_module fsdirModule = {
3073 0, /* iVersion */
3074 0, /* xCreate */
3075 fsdirConnect, /* xConnect */
3076 fsdirBestIndex, /* xBestIndex */
3077 fsdirDisconnect, /* xDisconnect */
3078 0, /* xDestroy */
3079 fsdirOpen, /* xOpen - open a cursor */
3080 fsdirClose, /* xClose - close a cursor */
3081 fsdirFilter, /* xFilter - configure scan constraints */
3082 fsdirNext, /* xNext - advance a cursor */
3083 fsdirEof, /* xEof - check for end of scan */
3084 fsdirColumn, /* xColumn - read data */
3085 fsdirRowid, /* xRowid - read data */
3086 0, /* xUpdate */
3087 0, /* xBegin */
3088 0, /* xSync */
3089 0, /* xCommit */
3090 0, /* xRollback */
3091 0, /* xFindMethod */
3092 0, /* xRename */
3093 0, /* xSavepoint */
3094 0, /* xRelease */
3095 0, /* xRollbackTo */
3096 0, /* xShadowName */
3097 };
3098
3099 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3100 return rc;
3101 }
3102 #else /* SQLITE_OMIT_VIRTUALTABLE */
3103 # define fsdirRegister(x) SQLITE_OK
3104 #endif
3105
3106 #ifdef _WIN32
3107
3108 #endif
3109 int sqlite3_fileio_init(
3110 sqlite3 *db,
3111 char **pzErrMsg,
3112 const sqlite3_api_routines *pApi
3113 ){
3114 int rc = SQLITE_OK;
3115 SQLITE_EXTENSION_INIT2(pApi);
3116 (void)pzErrMsg; /* Unused parameter */
3117 rc = sqlite3_create_function(db, "readfile", 1,
3118 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3119 readfileFunc, 0, 0);
3120 if( rc==SQLITE_OK ){
3121 rc = sqlite3_create_function(db, "writefile", -1,
3122 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3123 writefileFunc, 0, 0);
3124 }
3125 if( rc==SQLITE_OK ){
3126 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3127 lsModeFunc, 0, 0);
3128 }
3129 if( rc==SQLITE_OK ){
3130 rc = fsdirRegister(db);
3131 }
3132 return rc;
3133 }
3134
3135 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
3136 /* To allow a standalone DLL, make test_windirent.c use the same
3137 * redefined SQLite API calls as the above extension code does.
3138 * Just pull in this .c to accomplish this. As a beneficial side
3139 * effect, this extension becomes a single translation unit. */
3140 # include "test_windirent.c"
3141 #endif
3142
3143 /************************* End ../ext/misc/fileio.c ********************/
3144 /************************* Begin ../ext/misc/completion.c ******************/
3145 /*
3146 ** 2017-07-10
3147 **
3148 ** The author disclaims copyright to this source code. In place of
3149 ** a legal notice, here is a blessing:
3150 **
3151 ** May you do good and not evil.
3152 ** May you find forgiveness for yourself and forgive others.
3153 ** May you share freely, never taking more than you give.
3154 **
3155 *************************************************************************
3156 **
3157 ** This file implements an eponymous virtual table that returns suggested
3158 ** completions for a partial SQL input.
3159 **
3160 ** Suggested usage:
3161 **
3162 ** SELECT DISTINCT candidate COLLATE nocase
3163 ** FROM completion($prefix,$wholeline)
3164 ** ORDER BY 1;
3165 **
3166 ** The two query parameters are optional. $prefix is the text of the
3167 ** current word being typed and that is to be completed. $wholeline is
3168 ** the complete input line, used for context.
3169 **
3170 ** The raw completion() table might return the same candidate multiple
3171 ** times, for example if the same column name is used to two or more
3172 ** tables. And the candidates are returned in an arbitrary order. Hence,
3173 ** the DISTINCT and ORDER BY are recommended.
3174 **
3175 ** This virtual table operates at the speed of human typing, and so there
3176 ** is no attempt to make it fast. Even a slow implementation will be much
3177 ** faster than any human can type.
3178 **
3179 */
3180 /* #include "sqlite3ext.h" */
3181 SQLITE_EXTENSION_INIT1
3182 #include <assert.h>
3183 #include <string.h>
3184 #include <ctype.h>
3185
3186 #ifndef SQLITE_OMIT_VIRTUALTABLE
3187
3188 /* completion_vtab is a subclass of sqlite3_vtab which will
3189 ** serve as the underlying representation of a completion virtual table
3190 */
3191 typedef struct completion_vtab completion_vtab;
3192 struct completion_vtab {
3193 sqlite3_vtab base; /* Base class - must be first */
3194 sqlite3 *db; /* Database connection for this completion vtab */
3195 };
3196
3197 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3198 ** serve as the underlying representation of a cursor that scans
3199 ** over rows of the result
3200 */
3201 typedef struct completion_cursor completion_cursor;
3202 struct completion_cursor {
3203 sqlite3_vtab_cursor base; /* Base class - must be first */
3204 sqlite3 *db; /* Database connection for this cursor */
3205 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
3206 char *zPrefix; /* The prefix for the word we want to complete */
3207 char *zLine; /* The whole that we want to complete */
3208 const char *zCurrentRow; /* Current output row */
3209 int szRow; /* Length of the zCurrentRow string */
3210 sqlite3_stmt *pStmt; /* Current statement */
3211 sqlite3_int64 iRowid; /* The rowid */
3212 int ePhase; /* Current phase */
3213 int j; /* inter-phase counter */
3214 };
3215
3216 /* Values for ePhase:
3217 */
3218 #define COMPLETION_FIRST_PHASE 1
3219 #define COMPLETION_KEYWORDS 1
3220 #define COMPLETION_PRAGMAS 2
3221 #define COMPLETION_FUNCTIONS 3
3222 #define COMPLETION_COLLATIONS 4
3223 #define COMPLETION_INDEXES 5
3224 #define COMPLETION_TRIGGERS 6
3225 #define COMPLETION_DATABASES 7
3226 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
3227 #define COMPLETION_COLUMNS 9
3228 #define COMPLETION_MODULES 10
3229 #define COMPLETION_EOF 11
3230
3231 /*
3232 ** The completionConnect() method is invoked to create a new
3233 ** completion_vtab that describes the completion virtual table.
3234 **
3235 ** Think of this routine as the constructor for completion_vtab objects.
3236 **
3237 ** All this routine needs to do is:
3238 **
3239 ** (1) Allocate the completion_vtab object and initialize all fields.
3240 **
3241 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3242 ** result set of queries against completion will look like.
3243 */
3244 static int completionConnect(
3245 sqlite3 *db,
3246 void *pAux,
3247 int argc, const char *const*argv,
3248 sqlite3_vtab **ppVtab,
3249 char **pzErr
3250 ){
3251 completion_vtab *pNew;
3252 int rc;
3253
3254 (void)(pAux); /* Unused parameter */
3255 (void)(argc); /* Unused parameter */
3256 (void)(argv); /* Unused parameter */
3257 (void)(pzErr); /* Unused parameter */
3258
3259 /* Column numbers */
3260 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
3261 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
3262 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
3263 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
3264
3265 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3266 rc = sqlite3_declare_vtab(db,
3267 "CREATE TABLE x("
3268 " candidate TEXT,"
3269 " prefix TEXT HIDDEN,"
3270 " wholeline TEXT HIDDEN,"
3271 " phase INT HIDDEN" /* Used for debugging only */
3272 ")");
3273 if( rc==SQLITE_OK ){
3274 pNew = sqlite3_malloc( sizeof(*pNew) );
3275 *ppVtab = (sqlite3_vtab*)pNew;
3276 if( pNew==0 ) return SQLITE_NOMEM;
3277 memset(pNew, 0, sizeof(*pNew));
3278 pNew->db = db;
3279 }
3280 return rc;
3281 }
3282
3283 /*
3284 ** This method is the destructor for completion_cursor objects.
3285 */
3286 static int completionDisconnect(sqlite3_vtab *pVtab){
3287 sqlite3_free(pVtab);
3288 return SQLITE_OK;
3289 }
3290
3291 /*
3292 ** Constructor for a new completion_cursor object.
3293 */
3294 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3295 completion_cursor *pCur;
3296 pCur = sqlite3_malloc( sizeof(*pCur) );
3297 if( pCur==0 ) return SQLITE_NOMEM;
3298 memset(pCur, 0, sizeof(*pCur));
3299 pCur->db = ((completion_vtab*)p)->db;
3300 *ppCursor = &pCur->base;
3301 return SQLITE_OK;
3302 }
3303
3304 /*
3305 ** Reset the completion_cursor.
3306 */
3307 static void completionCursorReset(completion_cursor *pCur){
3308 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3309 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3310 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3311 pCur->j = 0;
3312 }
3313
3314 /*
3315 ** Destructor for a completion_cursor.
3316 */
3317 static int completionClose(sqlite3_vtab_cursor *cur){
3318 completionCursorReset((completion_cursor*)cur);
3319 sqlite3_free(cur);
3320 return SQLITE_OK;
3321 }
3322
3323 /*
3324 ** Advance a completion_cursor to its next row of output.
3325 **
3326 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3327 ** record the current state of the scan. This routine sets ->zCurrentRow
3328 ** to the current row of output and then returns. If no more rows remain,
3329 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3330 ** table that has reached the end of its scan.
3331 **
3332 ** The current implementation just lists potential identifiers and
3333 ** keywords and filters them by zPrefix. Future enhancements should
3334 ** take zLine into account to try to restrict the set of identifiers and
3335 ** keywords based on what would be legal at the current point of input.
3336 */
3337 static int completionNext(sqlite3_vtab_cursor *cur){
3338 completion_cursor *pCur = (completion_cursor*)cur;
3339 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3340 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3341 pCur->iRowid++;
3342 while( pCur->ePhase!=COMPLETION_EOF ){
3343 switch( pCur->ePhase ){
3344 case COMPLETION_KEYWORDS: {
3345 if( pCur->j >= sqlite3_keyword_count() ){
3346 pCur->zCurrentRow = 0;
3347 pCur->ePhase = COMPLETION_DATABASES;
3348 }else{
3349 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3350 }
3351 iCol = -1;
3352 break;
3353 }
3354 case COMPLETION_DATABASES: {
3355 if( pCur->pStmt==0 ){
3356 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3357 &pCur->pStmt, 0);
3358 }
3359 iCol = 1;
3360 eNextPhase = COMPLETION_TABLES;
3361 break;
3362 }
3363 case COMPLETION_TABLES: {
3364 if( pCur->pStmt==0 ){
3365 sqlite3_stmt *pS2;
3366 char *zSql = 0;
3367 const char *zSep = "";
3368 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3369 while( sqlite3_step(pS2)==SQLITE_ROW ){
3370 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3371 zSql = sqlite3_mprintf(
3372 "%z%s"
3373 "SELECT name FROM \"%w\".sqlite_schema",
3374 zSql, zSep, zDb
3375 );
3376 if( zSql==0 ) return SQLITE_NOMEM;
3377 zSep = " UNION ";
3378 }
3379 sqlite3_finalize(pS2);
3380 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3381 sqlite3_free(zSql);
3382 }
3383 iCol = 0;
3384 eNextPhase = COMPLETION_COLUMNS;
3385 break;
3386 }
3387 case COMPLETION_COLUMNS: {
3388 if( pCur->pStmt==0 ){
3389 sqlite3_stmt *pS2;
3390 char *zSql = 0;
3391 const char *zSep = "";
3392 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3393 while( sqlite3_step(pS2)==SQLITE_ROW ){
3394 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3395 zSql = sqlite3_mprintf(
3396 "%z%s"
3397 "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3398 " JOIN pragma_table_info(sm.name,%Q) AS pti"
3399 " WHERE sm.type='table'",
3400 zSql, zSep, zDb, zDb
3401 );
3402 if( zSql==0 ) return SQLITE_NOMEM;
3403 zSep = " UNION ";
3404 }
3405 sqlite3_finalize(pS2);
3406 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3407 sqlite3_free(zSql);
3408 }
3409 iCol = 0;
3410 eNextPhase = COMPLETION_EOF;
3411 break;
3412 }
3413 }
3414 if( iCol<0 ){
3415 /* This case is when the phase presets zCurrentRow */
3416 if( pCur->zCurrentRow==0 ) continue;
3417 }else{
3418 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3419 /* Extract the next row of content */
3420 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3421 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3422 }else{
3423 /* When all rows are finished, advance to the next phase */
3424 sqlite3_finalize(pCur->pStmt);
3425 pCur->pStmt = 0;
3426 pCur->ePhase = eNextPhase;
3427 continue;
3428 }
3429 }
3430 if( pCur->nPrefix==0 ) break;
3431 if( pCur->nPrefix<=pCur->szRow
3432 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3433 ){
3434 break;
3435 }
3436 }
3437
3438 return SQLITE_OK;
3439 }
3440
3441 /*
3442 ** Return values of columns for the row at which the completion_cursor
3443 ** is currently pointing.
3444 */
3445 static int completionColumn(
3446 sqlite3_vtab_cursor *cur, /* The cursor */
3447 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3448 int i /* Which column to return */
3449 ){
3450 completion_cursor *pCur = (completion_cursor*)cur;
3451 switch( i ){
3452 case COMPLETION_COLUMN_CANDIDATE: {
3453 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3454 break;
3455 }
3456 case COMPLETION_COLUMN_PREFIX: {
3457 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3458 break;
3459 }
3460 case COMPLETION_COLUMN_WHOLELINE: {
3461 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3462 break;
3463 }
3464 case COMPLETION_COLUMN_PHASE: {
3465 sqlite3_result_int(ctx, pCur->ePhase);
3466 break;
3467 }
3468 }
3469 return SQLITE_OK;
3470 }
3471
3472 /*
3473 ** Return the rowid for the current row. In this implementation, the
3474 ** rowid is the same as the output value.
3475 */
3476 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3477 completion_cursor *pCur = (completion_cursor*)cur;
3478 *pRowid = pCur->iRowid;
3479 return SQLITE_OK;
3480 }
3481
3482 /*
3483 ** Return TRUE if the cursor has been moved off of the last
3484 ** row of output.
3485 */
3486 static int completionEof(sqlite3_vtab_cursor *cur){
3487 completion_cursor *pCur = (completion_cursor*)cur;
3488 return pCur->ePhase >= COMPLETION_EOF;
3489 }
3490
3491 /*
3492 ** This method is called to "rewind" the completion_cursor object back
3493 ** to the first row of output. This method is always called at least
3494 ** once prior to any call to completionColumn() or completionRowid() or
3495 ** completionEof().
3496 */
3497 static int completionFilter(
3498 sqlite3_vtab_cursor *pVtabCursor,
3499 int idxNum, const char *idxStr,
3500 int argc, sqlite3_value **argv
3501 ){
3502 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3503 int iArg = 0;
3504 (void)(idxStr); /* Unused parameter */
3505 (void)(argc); /* Unused parameter */
3506 completionCursorReset(pCur);
3507 if( idxNum & 1 ){
3508 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3509 if( pCur->nPrefix>0 ){
3510 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3511 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3512 }
3513 iArg = 1;
3514 }
3515 if( idxNum & 2 ){
3516 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3517 if( pCur->nLine>0 ){
3518 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3519 if( pCur->zLine==0 ) return SQLITE_NOMEM;
3520 }
3521 }
3522 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3523 int i = pCur->nLine;
3524 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3525 i--;
3526 }
3527 pCur->nPrefix = pCur->nLine - i;
3528 if( pCur->nPrefix>0 ){
3529 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3530 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3531 }
3532 }
3533 pCur->iRowid = 0;
3534 pCur->ePhase = COMPLETION_FIRST_PHASE;
3535 return completionNext(pVtabCursor);
3536 }
3537
3538 /*
3539 ** SQLite will invoke this method one or more times while planning a query
3540 ** that uses the completion virtual table. This routine needs to create
3541 ** a query plan for each invocation and compute an estimated cost for that
3542 ** plan.
3543 **
3544 ** There are two hidden parameters that act as arguments to the table-valued
3545 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3546 ** is available and bit 1 is set if "wholeline" is available.
3547 */
3548 static int completionBestIndex(
3549 sqlite3_vtab *tab,
3550 sqlite3_index_info *pIdxInfo
3551 ){
3552 int i; /* Loop over constraints */
3553 int idxNum = 0; /* The query plan bitmask */
3554 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3555 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3556 int nArg = 0; /* Number of arguments that completeFilter() expects */
3557 const struct sqlite3_index_constraint *pConstraint;
3558
3559 (void)(tab); /* Unused parameter */
3560 pConstraint = pIdxInfo->aConstraint;
3561 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3562 if( pConstraint->usable==0 ) continue;
3563 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3564 switch( pConstraint->iColumn ){
3565 case COMPLETION_COLUMN_PREFIX:
3566 prefixIdx = i;
3567 idxNum |= 1;
3568 break;
3569 case COMPLETION_COLUMN_WHOLELINE:
3570 wholelineIdx = i;
3571 idxNum |= 2;
3572 break;
3573 }
3574 }
3575 if( prefixIdx>=0 ){
3576 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3577 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3578 }
3579 if( wholelineIdx>=0 ){
3580 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3581 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3582 }
3583 pIdxInfo->idxNum = idxNum;
3584 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3585 pIdxInfo->estimatedRows = 500 - 100*nArg;
3586 return SQLITE_OK;
3587 }
3588
3589 /*
3590 ** This following structure defines all the methods for the
3591 ** completion virtual table.
3592 */
3593 static sqlite3_module completionModule = {
3594 0, /* iVersion */
3595 0, /* xCreate */
3596 completionConnect, /* xConnect */
3597 completionBestIndex, /* xBestIndex */
3598 completionDisconnect, /* xDisconnect */
3599 0, /* xDestroy */
3600 completionOpen, /* xOpen - open a cursor */
3601 completionClose, /* xClose - close a cursor */
3602 completionFilter, /* xFilter - configure scan constraints */
3603 completionNext, /* xNext - advance a cursor */
3604 completionEof, /* xEof - check for end of scan */
3605 completionColumn, /* xColumn - read data */
3606 completionRowid, /* xRowid - read data */
3607 0, /* xUpdate */
3608 0, /* xBegin */
3609 0, /* xSync */
3610 0, /* xCommit */
3611 0, /* xRollback */
3612 0, /* xFindMethod */
3613 0, /* xRename */
3614 0, /* xSavepoint */
3615 0, /* xRelease */
3616 0, /* xRollbackTo */
3617 0 /* xShadowName */
3618 };
3619
3620 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3621
3622 int sqlite3CompletionVtabInit(sqlite3 *db){
3623 int rc = SQLITE_OK;
3624 #ifndef SQLITE_OMIT_VIRTUALTABLE
3625 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3626 #endif
3627 return rc;
3628 }
3629
3630 #ifdef _WIN32
3631
3632 #endif
3633 int sqlite3_completion_init(
3634 sqlite3 *db,
3635 char **pzErrMsg,
3636 const sqlite3_api_routines *pApi
3637 ){
3638 int rc = SQLITE_OK;
3639 SQLITE_EXTENSION_INIT2(pApi);
3640 (void)(pzErrMsg); /* Unused parameter */
3641 #ifndef SQLITE_OMIT_VIRTUALTABLE
3642 rc = sqlite3CompletionVtabInit(db);
3643 #endif
3644 return rc;
3645 }
3646
3647 /************************* End ../ext/misc/completion.c ********************/
3648 /************************* Begin ../ext/misc/appendvfs.c ******************/
3649 /*
3650 ** 2017-10-20
3651 **
3652 ** The author disclaims copyright to this source code. In place of
3653 ** a legal notice, here is a blessing:
3654 **
3655 ** May you do good and not evil.
3656 ** May you find forgiveness for yourself and forgive others.
3657 ** May you share freely, never taking more than you give.
3658 **
3659 ******************************************************************************
3660 **
3661 ** This file implements a VFS shim that allows an SQLite database to be
3662 ** appended onto the end of some other file, such as an executable.
3663 **
3664 ** A special record must appear at the end of the file that identifies the
3665 ** file as an appended database and provides the offset to the first page
3666 ** of the exposed content. (Or, it is the length of the content prefix.)
3667 ** For best performance page 1 should be located at a disk page boundary,
3668 ** though that is not required.
3669 **
3670 ** When opening a database using this VFS, the connection might treat
3671 ** the file as an ordinary SQLite database, or it might treat it as a
3672 ** database appended onto some other file. The decision is made by
3673 ** applying the following rules in order:
3674 **
3675 ** (1) An empty file is an ordinary database.
3676 **
3677 ** (2) If the file ends with the appendvfs trailer string
3678 ** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
3679 **
3680 ** (3) If the file begins with the standard SQLite prefix string
3681 ** "SQLite format 3", that file is an ordinary database.
3682 **
3683 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3684 ** set, then a new database is appended to the already existing file.
3685 **
3686 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
3687 **
3688 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3689 ** the file containing the database is limited to 1GiB. (1073741824 bytes)
3690 ** This VFS will not read or write past the 1GiB mark. This restriction
3691 ** might be lifted in future versions. For now, if you need a larger
3692 ** database, then keep it in a separate file.
3693 **
3694 ** If the file being opened is a plain database (not an appended one), then
3695 ** this shim is a pass-through into the default underlying VFS. (rule 3)
3696 **/
3697 /* #include "sqlite3ext.h" */
3698 SQLITE_EXTENSION_INIT1
3699 #include <string.h>
3700 #include <assert.h>
3701
3702 /* The append mark at the end of the database is:
3703 **
3704 ** Start-Of-SQLite3-NNNNNNNN
3705 ** 123456789 123456789 12345
3706 **
3707 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3708 ** the offset to page 1, and also the length of the prefix content.
3709 */
3710 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3711 #define APND_MARK_PREFIX_SZ 17
3712 #define APND_MARK_FOS_SZ 8
3713 #define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
3714
3715 /*
3716 ** Maximum size of the combined prefix + database + append-mark. This
3717 ** must be less than 0x40000000 to avoid locking issues on Windows.
3718 */
3719 #define APND_MAX_SIZE (0x40000000)
3720
3721 /*
3722 ** Try to align the database to an even multiple of APND_ROUNDUP bytes.
3723 */
3724 #ifndef APND_ROUNDUP
3725 #define APND_ROUNDUP 4096
3726 #endif
3727 #define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1))
3728 #define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
3729
3730 /*
3731 ** Forward declaration of objects used by this utility
3732 */
3733 typedef struct sqlite3_vfs ApndVfs;
3734 typedef struct ApndFile ApndFile;
3735
3736 /* Access to a lower-level VFS that (might) implement dynamic loading,
3737 ** access to randomness, etc.
3738 */
3739 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3740 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3741
3742 /* An open appendvfs file
3743 **
3744 ** An instance of this structure describes the appended database file.
3745 ** A separate sqlite3_file object is always appended. The appended
3746 ** sqlite3_file object (which can be accessed using ORIGFILE()) describes
3747 ** the entire file, including the prefix, the database, and the
3748 ** append-mark.
3749 **
3750 ** The structure of an AppendVFS database is like this:
3751 **
3752 ** +-------------+---------+----------+-------------+
3753 ** | prefix-file | padding | database | append-mark |
3754 ** +-------------+---------+----------+-------------+
3755 ** ^ ^
3756 ** | |
3757 ** iPgOne iMark
3758 **
3759 **
3760 ** "prefix file" - file onto which the database has been appended.
3761 ** "padding" - zero or more bytes inserted so that "database"
3762 ** starts on an APND_ROUNDUP boundary
3763 ** "database" - The SQLite database file
3764 ** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
3765 ** the offset from the start of prefix-file to the start
3766 ** of "database".
3767 **
3768 ** The size of the database is iMark - iPgOne.
3769 **
3770 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
3771 ** of iPgOne stored as a big-ending 64-bit integer.
3772 **
3773 ** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
3774 ** Or, iMark is -1 to indicate that it has not yet been written.
3775 */
3776 struct ApndFile {
3777 sqlite3_file base; /* Subclass. MUST BE FIRST! */
3778 sqlite3_int64 iPgOne; /* Offset to the start of the database */
3779 sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */
3780 /* Always followed by another sqlite3_file that describes the whole file */
3781 };
3782
3783 /*
3784 ** Methods for ApndFile
3785 */
3786 static int apndClose(sqlite3_file*);
3787 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3788 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3789 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3790 static int apndSync(sqlite3_file*, int flags);
3791 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3792 static int apndLock(sqlite3_file*, int);
3793 static int apndUnlock(sqlite3_file*, int);
3794 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3795 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3796 static int apndSectorSize(sqlite3_file*);
3797 static int apndDeviceCharacteristics(sqlite3_file*);
3798 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3799 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3800 static void apndShmBarrier(sqlite3_file*);
3801 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3802 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3803 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3804
3805 /*
3806 ** Methods for ApndVfs
3807 */
3808 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3809 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3810 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3811 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3812 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3813 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3814 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3815 static void apndDlClose(sqlite3_vfs*, void*);
3816 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3817 static int apndSleep(sqlite3_vfs*, int microseconds);
3818 static int apndCurrentTime(sqlite3_vfs*, double*);
3819 static int apndGetLastError(sqlite3_vfs*, int, char *);
3820 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3821 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3822 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3823 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3824
3825 static sqlite3_vfs apnd_vfs = {
3826 3, /* iVersion (set when registered) */
3827 0, /* szOsFile (set when registered) */
3828 1024, /* mxPathname */
3829 0, /* pNext */
3830 "apndvfs", /* zName */
3831 0, /* pAppData (set when registered) */
3832 apndOpen, /* xOpen */
3833 apndDelete, /* xDelete */
3834 apndAccess, /* xAccess */
3835 apndFullPathname, /* xFullPathname */
3836 apndDlOpen, /* xDlOpen */
3837 apndDlError, /* xDlError */
3838 apndDlSym, /* xDlSym */
3839 apndDlClose, /* xDlClose */
3840 apndRandomness, /* xRandomness */
3841 apndSleep, /* xSleep */
3842 apndCurrentTime, /* xCurrentTime */
3843 apndGetLastError, /* xGetLastError */
3844 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3845 apndSetSystemCall, /* xSetSystemCall */
3846 apndGetSystemCall, /* xGetSystemCall */
3847 apndNextSystemCall /* xNextSystemCall */
3848 };
3849
3850 static const sqlite3_io_methods apnd_io_methods = {
3851 3, /* iVersion */
3852 apndClose, /* xClose */
3853 apndRead, /* xRead */
3854 apndWrite, /* xWrite */
3855 apndTruncate, /* xTruncate */
3856 apndSync, /* xSync */
3857 apndFileSize, /* xFileSize */
3858 apndLock, /* xLock */
3859 apndUnlock, /* xUnlock */
3860 apndCheckReservedLock, /* xCheckReservedLock */
3861 apndFileControl, /* xFileControl */
3862 apndSectorSize, /* xSectorSize */
3863 apndDeviceCharacteristics, /* xDeviceCharacteristics */
3864 apndShmMap, /* xShmMap */
3865 apndShmLock, /* xShmLock */
3866 apndShmBarrier, /* xShmBarrier */
3867 apndShmUnmap, /* xShmUnmap */
3868 apndFetch, /* xFetch */
3869 apndUnfetch /* xUnfetch */
3870 };
3871
3872 /*
3873 ** Close an apnd-file.
3874 */
3875 static int apndClose(sqlite3_file *pFile){
3876 pFile = ORIGFILE(pFile);
3877 return pFile->pMethods->xClose(pFile);
3878 }
3879
3880 /*
3881 ** Read data from an apnd-file.
3882 */
3883 static int apndRead(
3884 sqlite3_file *pFile,
3885 void *zBuf,
3886 int iAmt,
3887 sqlite_int64 iOfst
3888 ){
3889 ApndFile *paf = (ApndFile *)pFile;
3890 pFile = ORIGFILE(pFile);
3891 return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3892 }
3893
3894 /*
3895 ** Add the append-mark onto what should become the end of the file.
3896 * If and only if this succeeds, internal ApndFile.iMark is updated.
3897 * Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
3898 */
3899 static int apndWriteMark(
3900 ApndFile *paf,
3901 sqlite3_file *pFile,
3902 sqlite_int64 iWriteEnd
3903 ){
3904 sqlite_int64 iPgOne = paf->iPgOne;
3905 unsigned char a[APND_MARK_SIZE];
3906 int i = APND_MARK_FOS_SZ;
3907 int rc;
3908 assert(pFile == ORIGFILE(paf));
3909 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3910 while( --i >= 0 ){
3911 a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
3912 iPgOne >>= 8;
3913 }
3914 iWriteEnd += paf->iPgOne;
3915 if( SQLITE_OK==(rc = pFile->pMethods->xWrite
3916 (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
3917 paf->iMark = iWriteEnd;
3918 }
3919 return rc;
3920 }
3921
3922 /*
3923 ** Write data to an apnd-file.
3924 */
3925 static int apndWrite(
3926 sqlite3_file *pFile,
3927 const void *zBuf,
3928 int iAmt,
3929 sqlite_int64 iOfst
3930 ){
3931 ApndFile *paf = (ApndFile *)pFile;
3932 sqlite_int64 iWriteEnd = iOfst + iAmt;
3933 if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
3934 pFile = ORIGFILE(pFile);
3935 /* If append-mark is absent or will be overwritten, write it. */
3936 if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
3937 int rc = apndWriteMark(paf, pFile, iWriteEnd);
3938 if( SQLITE_OK!=rc ) return rc;
3939 }
3940 return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3941 }
3942
3943 /*
3944 ** Truncate an apnd-file.
3945 */
3946 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3947 ApndFile *paf = (ApndFile *)pFile;
3948 pFile = ORIGFILE(pFile);
3949 /* The append mark goes out first so truncate failure does not lose it. */
3950 if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
3951 /* Truncate underlying file just past append mark */
3952 return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
3953 }
3954
3955 /*
3956 ** Sync an apnd-file.
3957 */
3958 static int apndSync(sqlite3_file *pFile, int flags){
3959 pFile = ORIGFILE(pFile);
3960 return pFile->pMethods->xSync(pFile, flags);
3961 }
3962
3963 /*
3964 ** Return the current file-size of an apnd-file.
3965 ** If the append mark is not yet there, the file-size is 0.
3966 */
3967 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3968 ApndFile *paf = (ApndFile *)pFile;
3969 *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
3970 return SQLITE_OK;
3971 }
3972
3973 /*
3974 ** Lock an apnd-file.
3975 */
3976 static int apndLock(sqlite3_file *pFile, int eLock){
3977 pFile = ORIGFILE(pFile);
3978 return pFile->pMethods->xLock(pFile, eLock);
3979 }
3980
3981 /*
3982 ** Unlock an apnd-file.
3983 */
3984 static int apndUnlock(sqlite3_file *pFile, int eLock){
3985 pFile = ORIGFILE(pFile);
3986 return pFile->pMethods->xUnlock(pFile, eLock);
3987 }
3988
3989 /*
3990 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3991 */
3992 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3993 pFile = ORIGFILE(pFile);
3994 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3995 }
3996
3997 /*
3998 ** File control method. For custom operations on an apnd-file.
3999 */
4000 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
4001 ApndFile *paf = (ApndFile *)pFile;
4002 int rc;
4003 pFile = ORIGFILE(pFile);
4004 if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
4005 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
4006 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
4007 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
4008 }
4009 return rc;
4010 }
4011
4012 /*
4013 ** Return the sector-size in bytes for an apnd-file.
4014 */
4015 static int apndSectorSize(sqlite3_file *pFile){
4016 pFile = ORIGFILE(pFile);
4017 return pFile->pMethods->xSectorSize(pFile);
4018 }
4019
4020 /*
4021 ** Return the device characteristic flags supported by an apnd-file.
4022 */
4023 static int apndDeviceCharacteristics(sqlite3_file *pFile){
4024 pFile = ORIGFILE(pFile);
4025 return pFile->pMethods->xDeviceCharacteristics(pFile);
4026 }
4027
4028 /* Create a shared memory file mapping */
4029 static int apndShmMap(
4030 sqlite3_file *pFile,
4031 int iPg,
4032 int pgsz,
4033 int bExtend,
4034 void volatile **pp
4035 ){
4036 pFile = ORIGFILE(pFile);
4037 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
4038 }
4039
4040 /* Perform locking on a shared-memory segment */
4041 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
4042 pFile = ORIGFILE(pFile);
4043 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
4044 }
4045
4046 /* Memory barrier operation on shared memory */
4047 static void apndShmBarrier(sqlite3_file *pFile){
4048 pFile = ORIGFILE(pFile);
4049 pFile->pMethods->xShmBarrier(pFile);
4050 }
4051
4052 /* Unmap a shared memory segment */
4053 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
4054 pFile = ORIGFILE(pFile);
4055 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
4056 }
4057
4058 /* Fetch a page of a memory-mapped file */
4059 static int apndFetch(
4060 sqlite3_file *pFile,
4061 sqlite3_int64 iOfst,
4062 int iAmt,
4063 void **pp
4064 ){
4065 ApndFile *p = (ApndFile *)pFile;
4066 if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
4067 return SQLITE_IOERR; /* Cannot read what is not yet there. */
4068 }
4069 pFile = ORIGFILE(pFile);
4070 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4071 }
4072
4073 /* Release a memory-mapped page */
4074 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4075 ApndFile *p = (ApndFile *)pFile;
4076 pFile = ORIGFILE(pFile);
4077 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4078 }
4079
4080 /*
4081 ** Try to read the append-mark off the end of a file. Return the
4082 ** start of the appended database if the append-mark is present.
4083 ** If there is no valid append-mark, return -1;
4084 **
4085 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
4086 ** indicates that the appended database contains at least one page. The
4087 ** start-of-database value must be a multiple of 512.
4088 */
4089 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4090 int rc, i;
4091 sqlite3_int64 iMark;
4092 int msbs = 8 * (APND_MARK_FOS_SZ-1);
4093 unsigned char a[APND_MARK_SIZE];
4094
4095 if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
4096 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4097 if( rc ) return -1;
4098 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4099 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
4100 for(i=1; i<8; i++){
4101 msbs -= 8;
4102 iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
4103 }
4104 if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
4105 if( iMark & 0x1ff ) return -1;
4106 return iMark;
4107 }
4108
4109 static const char apvfsSqliteHdr[] = "SQLite format 3";
4110 /*
4111 ** Check to see if the file is an appendvfs SQLite database file.
4112 ** Return true iff it is such. Parameter sz is the file's size.
4113 */
4114 static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
4115 int rc;
4116 char zHdr[16];
4117 sqlite3_int64 iMark = apndReadMark(sz, pFile);
4118 if( iMark>=0 ){
4119 /* If file has the correct end-marker, the expected odd size, and the
4120 ** SQLite DB type marker where the end-marker puts it, then it
4121 ** is an appendvfs database.
4122 */
4123 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
4124 if( SQLITE_OK==rc
4125 && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
4126 && (sz & 0x1ff) == APND_MARK_SIZE
4127 && sz>=512+APND_MARK_SIZE
4128 ){
4129 return 1; /* It's an appendvfs database */
4130 }
4131 }
4132 return 0;
4133 }
4134
4135 /*
4136 ** Check to see if the file is an ordinary SQLite database file.
4137 ** Return true iff so. Parameter sz is the file's size.
4138 */
4139 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4140 char zHdr[16];
4141 if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
4142 || (sz & 0x1ff) != 0
4143 || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
4144 || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
4145 ){
4146 return 0;
4147 }else{
4148 return 1;
4149 }
4150 }
4151
4152 /*
4153 ** Open an apnd file handle.
4154 */
4155 static int apndOpen(
4156 sqlite3_vfs *pApndVfs,
4157 const char *zName,
4158 sqlite3_file *pFile,
4159 int flags,
4160 int *pOutFlags
4161 ){
4162 ApndFile *pApndFile = (ApndFile*)pFile;
4163 sqlite3_file *pBaseFile = ORIGFILE(pFile);
4164 sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
4165 int rc;
4166 sqlite3_int64 sz = 0;
4167 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4168 /* The appendvfs is not to be used for transient or temporary databases.
4169 ** Just use the base VFS open to initialize the given file object and
4170 ** open the underlying file. (Appendvfs is then unused for this file.)
4171 */
4172 return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
4173 }
4174 memset(pApndFile, 0, sizeof(ApndFile));
4175 pFile->pMethods = &apnd_io_methods;
4176 pApndFile->iMark = -1; /* Append mark not yet written */
4177
4178 rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
4179 if( rc==SQLITE_OK ){
4180 rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
4181 if( rc ){
4182 pBaseFile->pMethods->xClose(pBaseFile);
4183 }
4184 }
4185 if( rc ){
4186 pFile->pMethods = 0;
4187 return rc;
4188 }
4189 if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
4190 /* The file being opened appears to be just an ordinary DB. Copy
4191 ** the base dispatch-table so this instance mimics the base VFS.
4192 */
4193 memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
4194 return SQLITE_OK;
4195 }
4196 pApndFile->iPgOne = apndReadMark(sz, pFile);
4197 if( pApndFile->iPgOne>=0 ){
4198 pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
4199 return SQLITE_OK;
4200 }
4201 if( (flags & SQLITE_OPEN_CREATE)==0 ){
4202 pBaseFile->pMethods->xClose(pBaseFile);
4203 rc = SQLITE_CANTOPEN;
4204 pFile->pMethods = 0;
4205 }else{
4206 /* Round newly added appendvfs location to #define'd page boundary.
4207 ** Note that nothing has yet been written to the underlying file.
4208 ** The append mark will be written along with first content write.
4209 ** Until then, paf->iMark value indicates it is not yet written.
4210 */
4211 pApndFile->iPgOne = APND_START_ROUNDUP(sz);
4212 }
4213 return rc;
4214 }
4215
4216 /*
4217 ** Delete an apnd file.
4218 ** For an appendvfs, this could mean delete the appendvfs portion,
4219 ** leaving the appendee as it was before it gained an appendvfs.
4220 ** For now, this code deletes the underlying file too.
4221 */
4222 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4223 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4224 }
4225
4226 /*
4227 ** All other VFS methods are pass-thrus.
4228 */
4229 static int apndAccess(
4230 sqlite3_vfs *pVfs,
4231 const char *zPath,
4232 int flags,
4233 int *pResOut
4234 ){
4235 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4236 }
4237 static int apndFullPathname(
4238 sqlite3_vfs *pVfs,
4239 const char *zPath,
4240 int nOut,
4241 char *zOut
4242 ){
4243 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4244 }
4245 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4246 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4247 }
4248 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4249 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4250 }
4251 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4252 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4253 }
4254 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4255 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4256 }
4257 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4258 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4259 }
4260 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4261 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4262 }
4263 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4264 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4265 }
4266 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4267 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4268 }
4269 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4270 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4271 }
4272 static int apndSetSystemCall(
4273 sqlite3_vfs *pVfs,
4274 const char *zName,
4275 sqlite3_syscall_ptr pCall
4276 ){
4277 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4278 }
4279 static sqlite3_syscall_ptr apndGetSystemCall(
4280 sqlite3_vfs *pVfs,
4281 const char *zName
4282 ){
4283 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4284 }
4285 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4286 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4287 }
4288
4289
4290 #ifdef _WIN32
4291
4292 #endif
4293 /*
4294 ** This routine is called when the extension is loaded.
4295 ** Register the new VFS.
4296 */
4297 int sqlite3_appendvfs_init(
4298 sqlite3 *db,
4299 char **pzErrMsg,
4300 const sqlite3_api_routines *pApi
4301 ){
4302 int rc = SQLITE_OK;
4303 sqlite3_vfs *pOrig;
4304 SQLITE_EXTENSION_INIT2(pApi);
4305 (void)pzErrMsg;
4306 (void)db;
4307 pOrig = sqlite3_vfs_find(0);
4308 if( pOrig==0 ) return SQLITE_ERROR;
4309 apnd_vfs.iVersion = pOrig->iVersion;
4310 apnd_vfs.pAppData = pOrig;
4311 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4312 rc = sqlite3_vfs_register(&apnd_vfs, 0);
4313 #ifdef APPENDVFS_TEST
4314 if( rc==SQLITE_OK ){
4315 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4316 }
4317 #endif
4318 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4319 return rc;
4320 }
4321
4322 /************************* End ../ext/misc/appendvfs.c ********************/
4323 /************************* Begin ../ext/misc/memtrace.c ******************/
4324 /*
4325 ** 2019-01-21
4326 **
4327 ** The author disclaims copyright to this source code. In place of
4328 ** a legal notice, here is a blessing:
4329 **
4330 ** May you do good and not evil.
4331 ** May you find forgiveness for yourself and forgive others.
4332 ** May you share freely, never taking more than you give.
4333 **
4334 *************************************************************************
4335 **
4336 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4337 ** mechanism to add a tracing layer on top of SQLite. If this extension
4338 ** is registered prior to sqlite3_initialize(), it will cause all memory
4339 ** allocation activities to be logged on standard output, or to some other
4340 ** FILE specified by the initializer.
4341 **
4342 ** This file needs to be compiled into the application that uses it.
4343 **
4344 ** This extension is used to implement the --memtrace option of the
4345 ** command-line shell.
4346 */
4347 #include <assert.h>
4348 #include <string.h>
4349 #include <stdio.h>
4350
4351 /* The original memory allocation routines */
4352 static sqlite3_mem_methods memtraceBase;
4353 static FILE *memtraceOut;
4354
4355 /* Methods that trace memory allocations */
4356 static void *memtraceMalloc(int n){
4357 if( memtraceOut ){
4358 fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4359 memtraceBase.xRoundup(n));
4360 }
4361 return memtraceBase.xMalloc(n);
4362 }
4363 static void memtraceFree(void *p){
4364 if( p==0 ) return;
4365 if( memtraceOut ){
4366 fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4367 }
4368 memtraceBase.xFree(p);
4369 }
4370 static void *memtraceRealloc(void *p, int n){
4371 if( p==0 ) return memtraceMalloc(n);
4372 if( n==0 ){
4373 memtraceFree(p);
4374 return 0;
4375 }
4376 if( memtraceOut ){
4377 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4378 memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4379 }
4380 return memtraceBase.xRealloc(p, n);
4381 }
4382 static int memtraceSize(void *p){
4383 return memtraceBase.xSize(p);
4384 }
4385 static int memtraceRoundup(int n){
4386 return memtraceBase.xRoundup(n);
4387 }
4388 static int memtraceInit(void *p){
4389 return memtraceBase.xInit(p);
4390 }
4391 static void memtraceShutdown(void *p){
4392 memtraceBase.xShutdown(p);
4393 }
4394
4395 /* The substitute memory allocator */
4396 static sqlite3_mem_methods ersaztMethods = {
4397 memtraceMalloc,
4398 memtraceFree,
4399 memtraceRealloc,
4400 memtraceSize,
4401 memtraceRoundup,
4402 memtraceInit,
4403 memtraceShutdown,
4404 0
4405 };
4406
4407 /* Begin tracing memory allocations to out. */
4408 int sqlite3MemTraceActivate(FILE *out){
4409 int rc = SQLITE_OK;
4410 if( memtraceBase.xMalloc==0 ){
4411 rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4412 if( rc==SQLITE_OK ){
4413 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4414 }
4415 }
4416 memtraceOut = out;
4417 return rc;
4418 }
4419
4420 /* Deactivate memory tracing */
4421 int sqlite3MemTraceDeactivate(void){
4422 int rc = SQLITE_OK;
4423 if( memtraceBase.xMalloc!=0 ){
4424 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4425 if( rc==SQLITE_OK ){
4426 memset(&memtraceBase, 0, sizeof(memtraceBase));
4427 }
4428 }
4429 memtraceOut = 0;
4430 return rc;
4431 }
4432
4433 /************************* End ../ext/misc/memtrace.c ********************/
4434 /************************* Begin ../ext/misc/uint.c ******************/
4435 /*
4436 ** 2020-04-14
4437 **
4438 ** The author disclaims copyright to this source code. In place of
@@ -6696,10 +4495,2224 @@
6696 }
6697 return rc;
6698 }
6699
6700 /************************* End ../ext/misc/regexp.c ********************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6701 #ifdef SQLITE_HAVE_ZLIB
6702 /************************* Begin ../ext/misc/zipfile.c ******************/
6703 /*
6704 ** 2017-12-26
6705 **
@@ -12235,11 +12248,21 @@
12235 int nIndent; /* Size of array aiIndent[] */
12236 int iIndent; /* Index of current op in aiIndent[] */
12237 char *zNonce; /* Nonce for temporary safe-mode excapes */
12238 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
12239 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
 
 
 
 
 
 
12240 };
 
 
 
 
12241
12242
12243 /* Allowed values for ShellState.autoEQP
12244 */
12245 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
@@ -15304,17 +15327,18 @@
15304
15305 /*
15306 ** Text of help messages.
15307 **
15308 ** The help text for each individual command begins with a line that starts
15309 ** with ".". Subsequent lines are supplimental information.
15310 **
15311 ** There must be two or more spaces between the end of the command and the
15312 ** start of the description of what that command does.
15313 */
15314 static const char *(azHelp[]) = {
15315 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
 
15316 ".archive ... Manage SQL archives",
15317 " Each command must have exactly one of the following options:",
15318 " -c, --create Create a new archive",
15319 " -u, --update Add or update files with changed mtime",
15320 " -i, --insert Like -u but always add even if unchanged",
@@ -15336,20 +15360,26 @@
15336 " http://sqlite.org/cli.html#sqlite_archive_support",
15337 #endif
15338 #ifndef SQLITE_OMIT_AUTHORIZATION
15339 ".auth ON|OFF Show authorizer callbacks",
15340 #endif
 
15341 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
15342 " Options:",
15343 " --append Use the appendvfs",
15344 " --async Write to FILE without journal and fsync()",
 
15345 ".bail on|off Stop after hitting an error. Default OFF",
15346 ".binary on|off Turn binary output on or off. Default OFF",
 
15347 ".cd DIRECTORY Change the working directory to DIRECTORY",
 
15348 ".changes on|off Show number of rows changed by SQL",
 
15349 ".check GLOB Fail if output since .testcase does not match",
15350 ".clone NEWDB Clone data into NEWDB from the existing database",
 
15351 ".connection [close] [#] Open or close an auxiliary database connection",
15352 ".databases List names and files of attached databases",
15353 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
15354 ".dbinfo ?DB? Show status information about the database",
15355 ".dump ?OBJECTS? Render database content as SQL",
@@ -15366,21 +15396,26 @@
15366 #ifdef SQLITE_DEBUG
15367 " test Show raw EXPLAIN QUERY PLAN output",
15368 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
15369 #endif
15370 " trigger Like \"full\" but also show trigger bytecode",
 
15371 ".excel Display the output of next command in spreadsheet",
15372 " --bom Put a UTF8 byte-order mark on intermediate file",
 
 
15373 ".exit ?CODE? Exit this program with return-code CODE",
 
15374 ".expert EXPERIMENTAL. Suggest indexes for queries",
15375 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
15376 ".filectrl CMD ... Run various sqlite3_file_control() operations",
15377 " --schema SCHEMA Use SCHEMA instead of \"main\"",
15378 " --help Show CMD details",
15379 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
15380 ".headers on|off Turn display of headers on or off",
15381 ".help ?-all? ?PATTERN? Show help text for PATTERN",
 
15382 ".import FILE TABLE Import data from FILE into TABLE",
15383 " Options:",
15384 " --ascii Use \\037 and \\036 as column and row separators",
15385 " --csv Use , and \\n as column and row separators",
15386 " --skip N Skip the first N rows of input",
@@ -15391,10 +15426,11 @@
15391 " determines the column names.",
15392 " * If neither --csv or --ascii are used, the input mode is derived",
15393 " from the \".mode\" output mode",
15394 " * If FILE begins with \"|\" then it is a command that generates the",
15395 " input text.",
 
15396 #ifndef SQLITE_OMIT_TEST_CONTROL
15397 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
15398 #endif
15399 ".indexes ?TABLE? Show names of indexes",
15400 " If TABLE is specified, only show indexes for",
@@ -15404,14 +15440,16 @@
15404 #endif
15405 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
15406 ".lint OPTIONS Report potential schema issues.",
15407 " Options:",
15408 " fkey-indexes Find missing foreign key indexes",
15409 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15410 ".load FILE ?ENTRY? Load an extension library",
15411 #endif
 
15412 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
 
15413 ".mode MODE ?OPTIONS? Set output mode",
15414 " MODE is one of:",
15415 " ascii Columns/rows delimited by 0x1F and 0x1E",
15416 " box Tables using unicode box-drawing characters",
15417 " csv Comma-separated values",
@@ -15432,35 +15470,44 @@
15432 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
15433 " --ww Shorthand for \"--wordwrap 1\"",
15434 " --quote Quote output text as SQL literals",
15435 " --noquote Do not quote output text",
15436 " TABLE The name of SQL table used for \"insert\" mode",
 
15437 ".nonce STRING Suspend safe mode for one command if nonce matches",
 
15438 ".nullvalue STRING Use STRING in place of NULL values",
 
15439 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
15440 " If FILE begins with '|' then open as a pipe",
15441 " --bom Put a UTF8 byte-order mark at the beginning",
15442 " -e Send output to the system text editor",
15443 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
 
 
 
15444 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
15445 " Options:",
15446 " --append Use appendvfs to append database to the end of FILE",
 
15447 #ifndef SQLITE_OMIT_DESERIALIZE
15448 " --deserialize Load into memory using sqlite3_deserialize()",
15449 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
15450 " --maxsize N Maximum size for --hexdb or --deserialized database",
15451 #endif
15452 " --new Initialize FILE to an empty database",
15453 " --nofollow Do not follow symbolic links",
15454 " --readonly Open FILE readonly",
15455 " --zip FILE is a ZIP archive",
 
15456 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
15457 " If FILE begins with '|' then open it as a pipe.",
15458 " Options:",
15459 " --bom Prefix output with a UTF8 byte-order mark",
15460 " -e Send output to the system text editor",
15461 " -x Send output as CSV to a spreadsheet",
 
15462 ".parameter CMD ... Manage SQL parameter bindings",
15463 " clear Erase all bindings",
15464 " init Initialize the TEMP table that holds bindings",
15465 " list List the current parameter bindings",
15466 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
@@ -15473,23 +15520,27 @@
15473 " --once Do no more than one progress interrupt",
15474 " --quiet|-q No output except at interrupts",
15475 " --reset Reset the count for each input and interrupt",
15476 #endif
15477 ".prompt MAIN CONTINUE Replace the standard prompts",
 
15478 ".quit Exit this program",
15479 ".read FILE Read input from FILE or command output",
15480 " If FILE begins with \"|\", it is a command that generates the input.",
 
15481 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15482 ".recover Recover as much data as possible from corrupt db.",
15483 " --freelist-corrupt Assume the freelist is corrupt",
15484 " --recovery-db NAME Store recovery metadata in database file NAME",
15485 " --lost-and-found TABLE Alternative name for the lost-and-found table",
15486 " --no-rowids Do not attempt to recover rowid values",
15487 " that are not also INTEGER PRIMARY KEYs",
15488 #endif
 
15489 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
15490 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
 
15491 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
15492 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
15493 " Options:",
15494 " --indent Try to pretty-print the schema",
15495 " --nosys Omit objects whose names start with \"sqlite_\"",
@@ -15519,24 +15570,26 @@
15519 " --sha3-224 Use the sha3-224 algorithm",
15520 " --sha3-256 Use the sha3-256 algorithm (default)",
15521 " --sha3-384 Use the sha3-384 algorithm",
15522 " --sha3-512 Use the sha3-512 algorithm",
15523 " Any other argument is a LIKE pattern for tables to hash",
15524 #ifndef SQLITE_NOHAVE_SYSTEM
15525 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
15526 #endif
15527 ".show Show the current values for various settings",
15528 ".stats ?ARG? Show stats or turn stats on or off",
15529 " off Turn off automatic stat display",
15530 " on Turn on automatic stat display",
15531 " stmt Show statement stats",
15532 " vmstep Show the virtual machine step count only",
15533 #ifndef SQLITE_NOHAVE_SYSTEM
15534 ".system CMD ARGS... Run CMD ARGS... in a system shell",
15535 #endif
15536 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
 
15537 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
 
15538 ".testctrl CMD ... Run various sqlite3_test_control() operations",
15539 " Run \".testctrl\" with no arguments for details",
15540 ".timeout MS Try opening locked tables for MS milliseconds",
15541 ".timer on|off Turn SQL timer on or off",
15542 #ifndef SQLITE_OMIT_TRACE
@@ -16077,18 +16130,20 @@
16077 exit(1);
16078 }
16079 #ifndef SQLITE_OMIT_LOAD_EXTENSION
16080 sqlite3_enable_load_extension(p->db, 1);
16081 #endif
16082 sqlite3_fileio_init(p->db, 0, 0);
16083 sqlite3_shathree_init(p->db, 0, 0);
16084 sqlite3_completion_init(p->db, 0, 0);
16085 sqlite3_uint_init(p->db, 0, 0);
16086 sqlite3_decimal_init(p->db, 0, 0);
16087 sqlite3_regexp_init(p->db, 0, 0);
16088 sqlite3_ieee_init(p->db, 0, 0);
16089 sqlite3_series_init(p->db, 0, 0);
 
 
 
 
16090 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
16091 sqlite3_dbdata_init(p->db, 0, 0);
16092 #endif
16093 #ifdef SQLITE_HAVE_ZLIB
16094 if( !p->bSafeModePersist ){
@@ -19207,18 +19262,20 @@
19207 sqlite3_set_authorizer(p->db, 0, 0);
19208 }
19209 }else
19210 #endif
19211
19212 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
 
19213 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
19214 open_db(p, 0);
19215 failIfSafeMode(p, "cannot run .archive in safe mode");
19216 rc = arDotCommand(p, 0, azArg, nArg);
19217 }else
19218 #endif
19219
 
19220 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
19221 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
19222 ){
19223 const char *zDestFile = 0;
19224 const char *zDb = 0;
@@ -19283,10 +19340,11 @@
19283 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
19284 rc = 1;
19285 }
19286 close_db(pDest);
19287 }else
 
19288
19289 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
19290 if( nArg==2 ){
19291 bail_on_error = booleanValue(azArg[1]);
19292 }else{
@@ -19313,10 +19371,11 @@
19313 */
19314 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
19315 test_breakpoint();
19316 }else
19317
 
19318 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
19319 failIfSafeMode(p, "cannot run .cd in safe mode");
19320 if( nArg==2 ){
19321 #if defined(_WIN32) || defined(WIN32)
19322 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
@@ -19332,10 +19391,11 @@
19332 }else{
19333 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
19334 rc = 1;
19335 }
19336 }else
 
19337
19338 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
19339 if( nArg==2 ){
19340 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
19341 }else{
@@ -19342,10 +19402,11 @@
19342 raw_printf(stderr, "Usage: .changes on|off\n");
19343 rc = 1;
19344 }
19345 }else
19346
 
19347 /* Cancel output redirection, if it is currently set (by .testcase)
19348 ** Then read the content of the testcase-out.txt file and compare against
19349 ** azArg[1]. If there are differences, report an error and exit.
19350 */
19351 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
@@ -19366,20 +19427,23 @@
19366 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
19367 p->nCheck++;
19368 }
19369 sqlite3_free(zRes);
19370 }else
 
19371
 
19372 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
19373 failIfSafeMode(p, "cannot run .clone in safe mode");
19374 if( nArg==2 ){
19375 tryToClone(p, azArg[1]);
19376 }else{
19377 raw_printf(stderr, "Usage: .clone FILENAME\n");
19378 rc = 1;
19379 }
19380 }else
 
19381
19382 if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
19383 if( nArg==1 ){
19384 /* List available connections */
19385 int i;
@@ -19664,14 +19728,16 @@
19664 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
19665 rc = 1;
19666 }
19667 }else
19668
 
19669 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
19670 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
19671 rc = 2;
19672 }else
 
19673
19674 /* The ".explain" command is automatic now. It is largely pointless. It
19675 ** retained purely for backwards compatibility */
19676 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
19677 int val = 1;
@@ -19922,10 +19988,11 @@
19922 }else{
19923 showHelp(p->out, 0);
19924 }
19925 }else
19926
 
19927 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
19928 char *zTable = 0; /* Insert data into this table */
19929 char *zSchema = 0; /* within this schema (may default to "main") */
19930 char *zFile = 0; /* Name of file to extra content from */
19931 sqlite3_stmt *pStmt = NULL; /* A statement */
@@ -20212,10 +20279,11 @@
20212 utf8_printf(p->out,
20213 "Added %d rows with %d errors using %d lines of input\n",
20214 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
20215 }
20216 }else
 
20217
20218 #ifndef SQLITE_UNTESTABLE
20219 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
20220 char *zSql;
20221 char *zCollist = 0;
@@ -20401,11 +20469,11 @@
20401 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
20402 open_db(p, 0);
20403 lintDotCommand(p, azArg, nArg);
20404 }else
20405
20406 #ifndef SQLITE_OMIT_LOAD_EXTENSION
20407 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
20408 const char *zFile, *zProc;
20409 char *zErrMsg = 0;
20410 failIfSafeMode(p, "cannot run .load in safe mode");
20411 if( nArg<2 ){
@@ -20423,10 +20491,11 @@
20423 rc = 1;
20424 }
20425 }else
20426 #endif
20427
 
20428 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
20429 failIfSafeMode(p, "cannot run .log in safe mode");
20430 if( nArg!=2 ){
20431 raw_printf(stderr, "Usage: .log FILENAME\n");
20432 rc = 1;
@@ -20434,10 +20503,11 @@
20434 const char *zFile = azArg[1];
20435 output_file_close(p->pLog);
20436 p->pLog = output_file_open(zFile, 0);
20437 }
20438 }else
 
20439
20440 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
20441 const char *zMode = 0;
20442 const char *zTabname = 0;
20443 int i, n2;
@@ -20558,10 +20628,11 @@
20558 rc = 1;
20559 }
20560 p->cMode = p->mode;
20561 }else
20562
 
20563 if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
20564 if( nArg!=2 ){
20565 raw_printf(stderr, "Usage: .nonce NONCE\n");
20566 rc = 1;
20567 }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
@@ -20572,10 +20643,11 @@
20572 p->bSafeMode = 0;
20573 return 0; /* Return immediately to bypass the safe mode reset
20574 ** at the end of this procedure */
20575 }
20576 }else
 
20577
20578 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
20579 if( nArg==2 ){
20580 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
20581 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
@@ -20593,10 +20665,11 @@
20593 int openMode = SHELL_OPEN_UNSPEC;
20594
20595 /* Check for command-line arguments */
20596 for(iName=1; iName<nArg; iName++){
20597 const char *z = azArg[iName];
 
20598 if( optionMatch(z,"new") ){
20599 newFlag = 1;
20600 #ifdef SQLITE_HAVE_ZLIB
20601 }else if( optionMatch(z, "zip") ){
20602 openMode = SHELL_OPEN_ZIPFILE;
@@ -20613,11 +20686,13 @@
20613 }else if( optionMatch(z, "hexdb") ){
20614 openMode = SHELL_OPEN_HEXDB;
20615 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
20616 p->szMax = integerValue(azArg[++iName]);
20617 #endif /* SQLITE_OMIT_DESERIALIZE */
20618 }else if( z[0]=='-' ){
 
 
20619 utf8_printf(stderr, "unknown option: %s\n", z);
20620 rc = 1;
20621 goto meta_command_exit;
20622 }else if( zFN ){
20623 utf8_printf(stderr, "extra argument: \"%s\"\n", z);
@@ -20640,17 +20715,21 @@
20640 p->szMax = 0;
20641
20642 /* If a filename is specified, try to open it first */
20643 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
20644 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
 
20645 if( p->bSafeMode
20646 && p->openMode!=SHELL_OPEN_HEXDB
20647 && zFN
20648 && strcmp(zFN,":memory:")!=0
20649 ){
20650 failIfSafeMode(p, "cannot open disk-based database files in safe mode");
20651 }
 
 
 
20652 if( zFN ){
20653 zNewFilename = sqlite3_mprintf("%s", zFN);
20654 shell_check_oom(zNewFilename);
20655 }else{
20656 zNewFilename = 0;
@@ -20669,10 +20748,11 @@
20669 p->pAuxDb->zDbFilename = 0;
20670 open_db(p, 0);
20671 }
20672 }else
20673
 
20674 if( (c=='o'
20675 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
20676 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
20677 ){
20678 char *zFile = 0;
@@ -20784,10 +20864,11 @@
20784 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
20785 }
20786 }
20787 sqlite3_free(zFile);
20788 }else
 
20789
20790 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
20791 open_db(p,0);
20792 if( nArg<=1 ) goto parameter_syntax_error;
20793
@@ -20953,14 +21034,17 @@
20953 if( nArg >= 3) {
20954 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
20955 }
20956 }else
20957
 
20958 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
20959 rc = 2;
20960 }else
 
20961
 
20962 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
20963 FILE *inSaved = p->in;
20964 int savedLineno = p->lineno;
20965 failIfSafeMode(p, "cannot run .read in safe mode");
20966 if( nArg!=2 ){
@@ -20991,11 +21075,13 @@
20991 fclose(p->in);
20992 }
20993 p->in = inSaved;
20994 p->lineno = savedLineno;
20995 }else
 
20996
 
20997 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
20998 const char *zSrcFile;
20999 const char *zDb;
21000 sqlite3 *pSrc;
21001 sqlite3_backup *pBackup;
@@ -21043,10 +21129,11 @@
21043 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
21044 rc = 1;
21045 }
21046 close_db(pSrc);
21047 }else
 
21048
21049 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
21050 if( nArg==2 ){
21051 p->scanstatsOn = (u8)booleanValue(azArg[1]);
21052 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
@@ -21668,11 +21755,11 @@
21668 shell_exec(p, zSql, 0);
21669 }
21670 sqlite3_free(zSql);
21671 }else
21672
21673 #ifndef SQLITE_NOHAVE_SYSTEM
21674 if( c=='s'
21675 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
21676 ){
21677 char *zCmd;
21678 int i, x;
@@ -21689,11 +21776,11 @@
21689 }
21690 x = zCmd!=0 ? system(zCmd) : 1;
21691 sqlite3_free(zCmd);
21692 if( x ) raw_printf(stderr, "System command returns %d\n", x);
21693 }else
21694 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
21695
21696 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
21697 static const char *azBool[] = { "off", "on", "trigger", "full"};
21698 const char *zOut;
21699 int i;
@@ -21869,10 +21956,11 @@
21869
21870 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
21871 sqlite3_free(azResult);
21872 }else
21873
 
21874 /* Begin redirecting output to the file "testcase-out.txt" */
21875 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
21876 output_reset(p);
21877 p->out = output_file_open("testcase-out.txt", 0);
21878 if( p->out==0 ){
@@ -21882,10 +21970,11 @@
21882 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
21883 }else{
21884 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
21885 }
21886 }else
 
21887
21888 #ifndef SQLITE_UNTESTABLE
21889 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
21890 static const struct {
21891 const char *zCtrlName; /* Name of a test-control option */
@@ -22553,10 +22642,43 @@
22553
22554 static void echo_group_input(ShellState *p, const char *zDo){
22555 if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
22556 }
22557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22558 /*
22559 ** Read input from *in and process it. If *in==0 then input
22560 ** is interactive - the user is typing it it. Otherwise, input
22561 ** is coming from a file or device. A prompt is issued and history
22562 ** is saved only if input is interactive. An interrupt signal will
@@ -22933,10 +23055,14 @@
22933 # define SQLITE_SHELL_IS_UTF8 (0)
22934 # else
22935 # define SQLITE_SHELL_IS_UTF8 (1)
22936 # endif
22937 #endif
 
 
 
 
22938
22939 #if SQLITE_SHELL_IS_UTF8
22940 int SQLITE_CDECL main(int argc, char **argv){
22941 #else
22942 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
@@ -22944,11 +23070,15 @@
22944 #endif
22945 #ifdef SQLITE_DEBUG
22946 sqlite3_uint64 mem_main_enter = sqlite3_memory_used();
22947 #endif
22948 char *zErrMsg = 0;
 
 
 
22949 ShellState data;
 
22950 const char *zInitFile = 0;
22951 int i;
22952 int rc = 0;
22953 int warnInmemoryDb = 0;
22954 int readStdin = 1;
@@ -22960,12 +23090,17 @@
22960 int argcToFree = 0;
22961 #endif
22962
22963 setBinaryMode(stdin, 0);
22964 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
 
 
 
 
22965 stdin_is_interactive = isatty(0);
22966 stdout_is_console = isatty(1);
 
22967
22968 #if !defined(_WIN32_WCE)
22969 if( getenv("SQLITE_DEBUG_BREAK") ){
22970 if( isatty(0) && isatty(2) ){
22971 fprintf(stderr,
@@ -23217,11 +23352,13 @@
23217 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
23218 return 1;
23219 #endif
23220 }
23221 data.out = stdout;
 
23222 sqlite3_appendvfs_init(0,0,0);
 
23223
23224 /* Go ahead and open the database file if it already exists. If the
23225 ** file does not exist, delay opening it. This prevents empty database
23226 ** files from being created if a user mistypes the database name argument
23227 ** to the sqlite command-line tool.
@@ -23483,10 +23620,13 @@
23483 }else{
23484 data.in = stdin;
23485 rc = process_input(&data);
23486 }
23487 }
 
 
 
23488 free(azCmd);
23489 set_table_name(&data, 0);
23490 if( data.db ){
23491 session_close_all(&data, -1);
23492 close_db(data.db);
@@ -23515,7 +23655,110 @@
23515 if( sqlite3_memory_used()>mem_main_enter ){
23516 utf8_printf(stderr, "Memory leaked: %u bytes\n",
23517 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
23518 }
23519 #endif
 
23520 return rc;
23521 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23522
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -245,10 +245,20 @@
245 #else
246 # define setBinaryMode(X,Y)
247 # define setTextMode(X,Y)
248 #endif
249
250 /*
251 ** When compiling with emcc (a.k.a. emscripten), we're building a
252 ** WebAssembly (WASM) bundle and need to disable and rewire a few
253 ** things.
254 */
255 #ifdef __EMSCRIPTEN__
256 #define SQLITE_SHELL_WASM_MODE
257 #else
258 #undef SQLITE_SHELL_WASM_MODE
259 #endif
260
261 /* True if the timer is enabled */
262 static int enableTimer = 0;
263
264 /* Return the current wall-clock time */
@@ -707,10 +717,11 @@
717 **
718 ** The result is stored in space obtained from malloc() and must either
719 ** be freed by the caller or else passed back into this routine via the
720 ** zPrior argument for reuse.
721 */
722 #ifndef SQLITE_SHELL_WASM_MODE
723 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
724 char *zPrompt;
725 char *zResult;
726 if( in!=0 ){
727 zResult = local_getline(zPrior, in);
@@ -726,11 +737,11 @@
737 if( zResult && *zResult ) shell_add_history(zResult);
738 #endif
739 }
740 return zResult;
741 }
742 #endif /* !SQLITE_SHELL_WASM_MODE */
743
744 /*
745 ** Return the value of a hexadecimal digit. Return -1 if the input
746 ** is not a hex digit.
747 */
@@ -814,11 +825,11 @@
825 ** zIn, if it was not NULL, is freed.
826 **
827 ** If the third argument, quote, is not '\0', then it is used as a
828 ** quote character for zAppend.
829 */
830 static void appendText(ShellText *p, const char *zAppend, char quote){
831 int len;
832 int i;
833 int nAppend = strlen30(zAppend);
834
835 len = nAppend+p->n+1;
@@ -1379,10 +1390,121 @@
1390 #endif /* defined(WIN32) && defined(_MSC_VER) */
1391
1392 /************************* End test_windirent.c ********************/
1393 #define dirent DIRENT
1394 #endif
1395 /************************* Begin ../ext/misc/memtrace.c ******************/
1396 /*
1397 ** 2019-01-21
1398 **
1399 ** The author disclaims copyright to this source code. In place of
1400 ** a legal notice, here is a blessing:
1401 **
1402 ** May you do good and not evil.
1403 ** May you find forgiveness for yourself and forgive others.
1404 ** May you share freely, never taking more than you give.
1405 **
1406 *************************************************************************
1407 **
1408 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
1409 ** mechanism to add a tracing layer on top of SQLite. If this extension
1410 ** is registered prior to sqlite3_initialize(), it will cause all memory
1411 ** allocation activities to be logged on standard output, or to some other
1412 ** FILE specified by the initializer.
1413 **
1414 ** This file needs to be compiled into the application that uses it.
1415 **
1416 ** This extension is used to implement the --memtrace option of the
1417 ** command-line shell.
1418 */
1419 #include <assert.h>
1420 #include <string.h>
1421 #include <stdio.h>
1422
1423 /* The original memory allocation routines */
1424 static sqlite3_mem_methods memtraceBase;
1425 static FILE *memtraceOut;
1426
1427 /* Methods that trace memory allocations */
1428 static void *memtraceMalloc(int n){
1429 if( memtraceOut ){
1430 fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
1431 memtraceBase.xRoundup(n));
1432 }
1433 return memtraceBase.xMalloc(n);
1434 }
1435 static void memtraceFree(void *p){
1436 if( p==0 ) return;
1437 if( memtraceOut ){
1438 fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
1439 }
1440 memtraceBase.xFree(p);
1441 }
1442 static void *memtraceRealloc(void *p, int n){
1443 if( p==0 ) return memtraceMalloc(n);
1444 if( n==0 ){
1445 memtraceFree(p);
1446 return 0;
1447 }
1448 if( memtraceOut ){
1449 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
1450 memtraceBase.xSize(p), memtraceBase.xRoundup(n));
1451 }
1452 return memtraceBase.xRealloc(p, n);
1453 }
1454 static int memtraceSize(void *p){
1455 return memtraceBase.xSize(p);
1456 }
1457 static int memtraceRoundup(int n){
1458 return memtraceBase.xRoundup(n);
1459 }
1460 static int memtraceInit(void *p){
1461 return memtraceBase.xInit(p);
1462 }
1463 static void memtraceShutdown(void *p){
1464 memtraceBase.xShutdown(p);
1465 }
1466
1467 /* The substitute memory allocator */
1468 static sqlite3_mem_methods ersaztMethods = {
1469 memtraceMalloc,
1470 memtraceFree,
1471 memtraceRealloc,
1472 memtraceSize,
1473 memtraceRoundup,
1474 memtraceInit,
1475 memtraceShutdown,
1476 0
1477 };
1478
1479 /* Begin tracing memory allocations to out. */
1480 int sqlite3MemTraceActivate(FILE *out){
1481 int rc = SQLITE_OK;
1482 if( memtraceBase.xMalloc==0 ){
1483 rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
1484 if( rc==SQLITE_OK ){
1485 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
1486 }
1487 }
1488 memtraceOut = out;
1489 return rc;
1490 }
1491
1492 /* Deactivate memory tracing */
1493 int sqlite3MemTraceDeactivate(void){
1494 int rc = SQLITE_OK;
1495 if( memtraceBase.xMalloc!=0 ){
1496 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
1497 if( rc==SQLITE_OK ){
1498 memset(&memtraceBase, 0, sizeof(memtraceBase));
1499 }
1500 }
1501 memtraceOut = 0;
1502 return rc;
1503 }
1504
1505 /************************* End ../ext/misc/memtrace.c ********************/
1506 /************************* Begin ../ext/misc/shathree.c ******************/
1507 /*
1508 ** 2017-03-08
1509 **
1510 ** The author disclaims copyright to this source code. In place of
@@ -2106,2333 +2228,10 @@
2228 }
2229 return rc;
2230 }
2231
2232 /************************* End ../ext/misc/shathree.c ********************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2233 /************************* Begin ../ext/misc/uint.c ******************/
2234 /*
2235 ** 2020-04-14
2236 **
2237 ** The author disclaims copyright to this source code. In place of
@@ -6696,10 +4495,2224 @@
4495 }
4496 return rc;
4497 }
4498
4499 /************************* End ../ext/misc/regexp.c ********************/
4500 #ifndef SQLITE_SHELL_WASM_MODE
4501 /************************* Begin ../ext/misc/fileio.c ******************/
4502 /*
4503 ** 2014-06-13
4504 **
4505 ** The author disclaims copyright to this source code. In place of
4506 ** a legal notice, here is a blessing:
4507 **
4508 ** May you do good and not evil.
4509 ** May you find forgiveness for yourself and forgive others.
4510 ** May you share freely, never taking more than you give.
4511 **
4512 ******************************************************************************
4513 **
4514 ** This SQLite extension implements SQL functions readfile() and
4515 ** writefile(), and eponymous virtual type "fsdir".
4516 **
4517 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
4518 **
4519 ** If neither of the optional arguments is present, then this UDF
4520 ** function writes blob DATA to file FILE. If successful, the number
4521 ** of bytes written is returned. If an error occurs, NULL is returned.
4522 **
4523 ** If the first option argument - MODE - is present, then it must
4524 ** be passed an integer value that corresponds to a POSIX mode
4525 ** value (file type + permissions, as returned in the stat.st_mode
4526 ** field by the stat() system call). Three types of files may
4527 ** be written/created:
4528 **
4529 ** regular files: (mode & 0170000)==0100000
4530 ** symbolic links: (mode & 0170000)==0120000
4531 ** directories: (mode & 0170000)==0040000
4532 **
4533 ** For a directory, the DATA is ignored. For a symbolic link, it is
4534 ** interpreted as text and used as the target of the link. For a
4535 ** regular file, it is interpreted as a blob and written into the
4536 ** named file. Regardless of the type of file, its permissions are
4537 ** set to (mode & 0777) before returning.
4538 **
4539 ** If the optional MTIME argument is present, then it is interpreted
4540 ** as an integer - the number of seconds since the unix epoch. The
4541 ** modification-time of the target file is set to this value before
4542 ** returning.
4543 **
4544 ** If three or more arguments are passed to this function and an
4545 ** error is encountered, an exception is raised.
4546 **
4547 ** READFILE(FILE):
4548 **
4549 ** Read and return the contents of file FILE (type blob) from disk.
4550 **
4551 ** FSDIR:
4552 **
4553 ** Used as follows:
4554 **
4555 ** SELECT * FROM fsdir($path [, $dir]);
4556 **
4557 ** Parameter $path is an absolute or relative pathname. If the file that it
4558 ** refers to does not exist, it is an error. If the path refers to a regular
4559 ** file or symbolic link, it returns a single row. Or, if the path refers
4560 ** to a directory, it returns one row for the directory, and one row for each
4561 ** file within the hierarchy rooted at $path.
4562 **
4563 ** Each row has the following columns:
4564 **
4565 ** name: Path to file or directory (text value).
4566 ** mode: Value of stat.st_mode for directory entry (an integer).
4567 ** mtime: Value of stat.st_mtime for directory entry (an integer).
4568 ** data: For a regular file, a blob containing the file data. For a
4569 ** symlink, a text value containing the text of the link. For a
4570 ** directory, NULL.
4571 **
4572 ** If a non-NULL value is specified for the optional $dir parameter and
4573 ** $path is a relative path, then $path is interpreted relative to $dir.
4574 ** And the paths returned in the "name" column of the table are also
4575 ** relative to directory $dir.
4576 **
4577 ** Notes on building this extension for Windows:
4578 ** Unless linked statically with the SQLite library, a preprocessor
4579 ** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
4580 ** DLL form of this extension for WIN32. See its use below for details.
4581 */
4582 /* #include "sqlite3ext.h" */
4583 SQLITE_EXTENSION_INIT1
4584 #include <stdio.h>
4585 #include <string.h>
4586 #include <assert.h>
4587
4588 #include <sys/types.h>
4589 #include <sys/stat.h>
4590 #include <fcntl.h>
4591 #if !defined(_WIN32) && !defined(WIN32)
4592 # include <unistd.h>
4593 # include <dirent.h>
4594 # include <utime.h>
4595 # include <sys/time.h>
4596 #else
4597 # include "windows.h"
4598 # include <io.h>
4599 # include <direct.h>
4600 /* # include "test_windirent.h" */
4601 # define dirent DIRENT
4602 # ifndef chmod
4603 # define chmod _chmod
4604 # endif
4605 # ifndef stat
4606 # define stat _stat
4607 # endif
4608 # define mkdir(path,mode) _mkdir(path)
4609 # define lstat(path,buf) stat(path,buf)
4610 #endif
4611 #include <time.h>
4612 #include <errno.h>
4613
4614
4615 /*
4616 ** Structure of the fsdir() table-valued function
4617 */
4618 /* 0 1 2 3 4 5 */
4619 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
4620 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
4621 #define FSDIR_COLUMN_MODE 1 /* Access mode */
4622 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
4623 #define FSDIR_COLUMN_DATA 3 /* File content */
4624 #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
4625 #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
4626
4627
4628 /*
4629 ** Set the result stored by context ctx to a blob containing the
4630 ** contents of file zName. Or, leave the result unchanged (NULL)
4631 ** if the file does not exist or is unreadable.
4632 **
4633 ** If the file exceeds the SQLite blob size limit, through an
4634 ** SQLITE_TOOBIG error.
4635 **
4636 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
4637 ** off of disk.
4638 */
4639 static void readFileContents(sqlite3_context *ctx, const char *zName){
4640 FILE *in;
4641 sqlite3_int64 nIn;
4642 void *pBuf;
4643 sqlite3 *db;
4644 int mxBlob;
4645
4646 in = fopen(zName, "rb");
4647 if( in==0 ){
4648 /* File does not exist or is unreadable. Leave the result set to NULL. */
4649 return;
4650 }
4651 fseek(in, 0, SEEK_END);
4652 nIn = ftell(in);
4653 rewind(in);
4654 db = sqlite3_context_db_handle(ctx);
4655 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
4656 if( nIn>mxBlob ){
4657 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
4658 fclose(in);
4659 return;
4660 }
4661 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
4662 if( pBuf==0 ){
4663 sqlite3_result_error_nomem(ctx);
4664 fclose(in);
4665 return;
4666 }
4667 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
4668 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
4669 }else{
4670 sqlite3_result_error_code(ctx, SQLITE_IOERR);
4671 sqlite3_free(pBuf);
4672 }
4673 fclose(in);
4674 }
4675
4676 /*
4677 ** Implementation of the "readfile(X)" SQL function. The entire content
4678 ** of the file named X is read and returned as a BLOB. NULL is returned
4679 ** if the file does not exist or is unreadable.
4680 */
4681 static void readfileFunc(
4682 sqlite3_context *context,
4683 int argc,
4684 sqlite3_value **argv
4685 ){
4686 const char *zName;
4687 (void)(argc); /* Unused parameter */
4688 zName = (const char*)sqlite3_value_text(argv[0]);
4689 if( zName==0 ) return;
4690 readFileContents(context, zName);
4691 }
4692
4693 /*
4694 ** Set the error message contained in context ctx to the results of
4695 ** vprintf(zFmt, ...).
4696 */
4697 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4698 char *zMsg = 0;
4699 va_list ap;
4700 va_start(ap, zFmt);
4701 zMsg = sqlite3_vmprintf(zFmt, ap);
4702 sqlite3_result_error(ctx, zMsg, -1);
4703 sqlite3_free(zMsg);
4704 va_end(ap);
4705 }
4706
4707 #if defined(_WIN32)
4708 /*
4709 ** This function is designed to convert a Win32 FILETIME structure into the
4710 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
4711 */
4712 static sqlite3_uint64 fileTimeToUnixTime(
4713 LPFILETIME pFileTime
4714 ){
4715 SYSTEMTIME epochSystemTime;
4716 ULARGE_INTEGER epochIntervals;
4717 FILETIME epochFileTime;
4718 ULARGE_INTEGER fileIntervals;
4719
4720 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
4721 epochSystemTime.wYear = 1970;
4722 epochSystemTime.wMonth = 1;
4723 epochSystemTime.wDay = 1;
4724 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
4725 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
4726 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
4727
4728 fileIntervals.LowPart = pFileTime->dwLowDateTime;
4729 fileIntervals.HighPart = pFileTime->dwHighDateTime;
4730
4731 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
4732 }
4733
4734
4735 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
4736 # /* To allow a standalone DLL, use this next replacement function: */
4737 # undef sqlite3_win32_utf8_to_unicode
4738 # define sqlite3_win32_utf8_to_unicode utf8_to_utf16
4739 #
4740 LPWSTR utf8_to_utf16(const char *z){
4741 int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0);
4742 LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR));
4743 if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) )
4744 return rv;
4745 sqlite3_free(rv);
4746 return 0;
4747 }
4748 #endif
4749
4750 /*
4751 ** This function attempts to normalize the time values found in the stat()
4752 ** buffer to UTC. This is necessary on Win32, where the runtime library
4753 ** appears to return these values as local times.
4754 */
4755 static void statTimesToUtc(
4756 const char *zPath,
4757 struct stat *pStatBuf
4758 ){
4759 HANDLE hFindFile;
4760 WIN32_FIND_DATAW fd;
4761 LPWSTR zUnicodeName;
4762 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
4763 zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
4764 if( zUnicodeName ){
4765 memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
4766 hFindFile = FindFirstFileW(zUnicodeName, &fd);
4767 if( hFindFile!=NULL ){
4768 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
4769 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
4770 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
4771 FindClose(hFindFile);
4772 }
4773 sqlite3_free(zUnicodeName);
4774 }
4775 }
4776 #endif
4777
4778 /*
4779 ** This function is used in place of stat(). On Windows, special handling
4780 ** is required in order for the included time to be returned as UTC. On all
4781 ** other systems, this function simply calls stat().
4782 */
4783 static int fileStat(
4784 const char *zPath,
4785 struct stat *pStatBuf
4786 ){
4787 #if defined(_WIN32)
4788 int rc = stat(zPath, pStatBuf);
4789 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
4790 return rc;
4791 #else
4792 return stat(zPath, pStatBuf);
4793 #endif
4794 }
4795
4796 /*
4797 ** This function is used in place of lstat(). On Windows, special handling
4798 ** is required in order for the included time to be returned as UTC. On all
4799 ** other systems, this function simply calls lstat().
4800 */
4801 static int fileLinkStat(
4802 const char *zPath,
4803 struct stat *pStatBuf
4804 ){
4805 #if defined(_WIN32)
4806 int rc = lstat(zPath, pStatBuf);
4807 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
4808 return rc;
4809 #else
4810 return lstat(zPath, pStatBuf);
4811 #endif
4812 }
4813
4814 /*
4815 ** Argument zFile is the name of a file that will be created and/or written
4816 ** by SQL function writefile(). This function ensures that the directory
4817 ** zFile will be written to exists, creating it if required. The permissions
4818 ** for any path components created by this function are set in accordance
4819 ** with the current umask.
4820 **
4821 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
4822 ** SQLITE_OK is returned if the directory is successfully created, or
4823 ** SQLITE_ERROR otherwise.
4824 */
4825 static int makeDirectory(
4826 const char *zFile
4827 ){
4828 char *zCopy = sqlite3_mprintf("%s", zFile);
4829 int rc = SQLITE_OK;
4830
4831 if( zCopy==0 ){
4832 rc = SQLITE_NOMEM;
4833 }else{
4834 int nCopy = (int)strlen(zCopy);
4835 int i = 1;
4836
4837 while( rc==SQLITE_OK ){
4838 struct stat sStat;
4839 int rc2;
4840
4841 for(; zCopy[i]!='/' && i<nCopy; i++);
4842 if( i==nCopy ) break;
4843 zCopy[i] = '\0';
4844
4845 rc2 = fileStat(zCopy, &sStat);
4846 if( rc2!=0 ){
4847 if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
4848 }else{
4849 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
4850 }
4851 zCopy[i] = '/';
4852 i++;
4853 }
4854
4855 sqlite3_free(zCopy);
4856 }
4857
4858 return rc;
4859 }
4860
4861 /*
4862 ** This function does the work for the writefile() UDF. Refer to
4863 ** header comments at the top of this file for details.
4864 */
4865 static int writeFile(
4866 sqlite3_context *pCtx, /* Context to return bytes written in */
4867 const char *zFile, /* File to write */
4868 sqlite3_value *pData, /* Data to write */
4869 mode_t mode, /* MODE parameter passed to writefile() */
4870 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
4871 ){
4872 if( zFile==0 ) return 1;
4873 #if !defined(_WIN32) && !defined(WIN32)
4874 if( S_ISLNK(mode) ){
4875 const char *zTo = (const char*)sqlite3_value_text(pData);
4876 if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
4877 }else
4878 #endif
4879 {
4880 if( S_ISDIR(mode) ){
4881 if( mkdir(zFile, mode) ){
4882 /* The mkdir() call to create the directory failed. This might not
4883 ** be an error though - if there is already a directory at the same
4884 ** path and either the permissions already match or can be changed
4885 ** to do so using chmod(), it is not an error. */
4886 struct stat sStat;
4887 if( errno!=EEXIST
4888 || 0!=fileStat(zFile, &sStat)
4889 || !S_ISDIR(sStat.st_mode)
4890 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
4891 ){
4892 return 1;
4893 }
4894 }
4895 }else{
4896 sqlite3_int64 nWrite = 0;
4897 const char *z;
4898 int rc = 0;
4899 FILE *out = fopen(zFile, "wb");
4900 if( out==0 ) return 1;
4901 z = (const char*)sqlite3_value_blob(pData);
4902 if( z ){
4903 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
4904 nWrite = sqlite3_value_bytes(pData);
4905 if( nWrite!=n ){
4906 rc = 1;
4907 }
4908 }
4909 fclose(out);
4910 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
4911 rc = 1;
4912 }
4913 if( rc ) return 2;
4914 sqlite3_result_int64(pCtx, nWrite);
4915 }
4916 }
4917
4918 if( mtime>=0 ){
4919 #if defined(_WIN32)
4920 #if !SQLITE_OS_WINRT
4921 /* Windows */
4922 FILETIME lastAccess;
4923 FILETIME lastWrite;
4924 SYSTEMTIME currentTime;
4925 LONGLONG intervals;
4926 HANDLE hFile;
4927 LPWSTR zUnicodeName;
4928 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
4929
4930 GetSystemTime(&currentTime);
4931 SystemTimeToFileTime(&currentTime, &lastAccess);
4932 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
4933 lastWrite.dwLowDateTime = (DWORD)intervals;
4934 lastWrite.dwHighDateTime = intervals >> 32;
4935 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
4936 if( zUnicodeName==0 ){
4937 return 1;
4938 }
4939 hFile = CreateFileW(
4940 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
4941 FILE_FLAG_BACKUP_SEMANTICS, NULL
4942 );
4943 sqlite3_free(zUnicodeName);
4944 if( hFile!=INVALID_HANDLE_VALUE ){
4945 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
4946 CloseHandle(hFile);
4947 return !bResult;
4948 }else{
4949 return 1;
4950 }
4951 #endif
4952 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
4953 /* Recent unix */
4954 struct timespec times[2];
4955 times[0].tv_nsec = times[1].tv_nsec = 0;
4956 times[0].tv_sec = time(0);
4957 times[1].tv_sec = mtime;
4958 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
4959 return 1;
4960 }
4961 #else
4962 /* Legacy unix */
4963 struct timeval times[2];
4964 times[0].tv_usec = times[1].tv_usec = 0;
4965 times[0].tv_sec = time(0);
4966 times[1].tv_sec = mtime;
4967 if( utimes(zFile, times) ){
4968 return 1;
4969 }
4970 #endif
4971 }
4972
4973 return 0;
4974 }
4975
4976 /*
4977 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
4978 ** Refer to header comments at the top of this file for details.
4979 */
4980 static void writefileFunc(
4981 sqlite3_context *context,
4982 int argc,
4983 sqlite3_value **argv
4984 ){
4985 const char *zFile;
4986 mode_t mode = 0;
4987 int res;
4988 sqlite3_int64 mtime = -1;
4989
4990 if( argc<2 || argc>4 ){
4991 sqlite3_result_error(context,
4992 "wrong number of arguments to function writefile()", -1
4993 );
4994 return;
4995 }
4996
4997 zFile = (const char*)sqlite3_value_text(argv[0]);
4998 if( zFile==0 ) return;
4999 if( argc>=3 ){
5000 mode = (mode_t)sqlite3_value_int(argv[2]);
5001 }
5002 if( argc==4 ){
5003 mtime = sqlite3_value_int64(argv[3]);
5004 }
5005
5006 res = writeFile(context, zFile, argv[1], mode, mtime);
5007 if( res==1 && errno==ENOENT ){
5008 if( makeDirectory(zFile)==SQLITE_OK ){
5009 res = writeFile(context, zFile, argv[1], mode, mtime);
5010 }
5011 }
5012
5013 if( argc>2 && res!=0 ){
5014 if( S_ISLNK(mode) ){
5015 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
5016 }else if( S_ISDIR(mode) ){
5017 ctxErrorMsg(context, "failed to create directory: %s", zFile);
5018 }else{
5019 ctxErrorMsg(context, "failed to write file: %s", zFile);
5020 }
5021 }
5022 }
5023
5024 /*
5025 ** SQL function: lsmode(MODE)
5026 **
5027 ** Given a numberic st_mode from stat(), convert it into a human-readable
5028 ** text string in the style of "ls -l".
5029 */
5030 static void lsModeFunc(
5031 sqlite3_context *context,
5032 int argc,
5033 sqlite3_value **argv
5034 ){
5035 int i;
5036 int iMode = sqlite3_value_int(argv[0]);
5037 char z[16];
5038 (void)argc;
5039 if( S_ISLNK(iMode) ){
5040 z[0] = 'l';
5041 }else if( S_ISREG(iMode) ){
5042 z[0] = '-';
5043 }else if( S_ISDIR(iMode) ){
5044 z[0] = 'd';
5045 }else{
5046 z[0] = '?';
5047 }
5048 for(i=0; i<3; i++){
5049 int m = (iMode >> ((2-i)*3));
5050 char *a = &z[1 + i*3];
5051 a[0] = (m & 0x4) ? 'r' : '-';
5052 a[1] = (m & 0x2) ? 'w' : '-';
5053 a[2] = (m & 0x1) ? 'x' : '-';
5054 }
5055 z[10] = '\0';
5056 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
5057 }
5058
5059 #ifndef SQLITE_OMIT_VIRTUALTABLE
5060
5061 /*
5062 ** Cursor type for recursively iterating through a directory structure.
5063 */
5064 typedef struct fsdir_cursor fsdir_cursor;
5065 typedef struct FsdirLevel FsdirLevel;
5066
5067 struct FsdirLevel {
5068 DIR *pDir; /* From opendir() */
5069 char *zDir; /* Name of directory (nul-terminated) */
5070 };
5071
5072 struct fsdir_cursor {
5073 sqlite3_vtab_cursor base; /* Base class - must be first */
5074
5075 int nLvl; /* Number of entries in aLvl[] array */
5076 int iLvl; /* Index of current entry */
5077 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
5078
5079 const char *zBase;
5080 int nBase;
5081
5082 struct stat sStat; /* Current lstat() results */
5083 char *zPath; /* Path to current entry */
5084 sqlite3_int64 iRowid; /* Current rowid */
5085 };
5086
5087 typedef struct fsdir_tab fsdir_tab;
5088 struct fsdir_tab {
5089 sqlite3_vtab base; /* Base class - must be first */
5090 };
5091
5092 /*
5093 ** Construct a new fsdir virtual table object.
5094 */
5095 static int fsdirConnect(
5096 sqlite3 *db,
5097 void *pAux,
5098 int argc, const char *const*argv,
5099 sqlite3_vtab **ppVtab,
5100 char **pzErr
5101 ){
5102 fsdir_tab *pNew = 0;
5103 int rc;
5104 (void)pAux;
5105 (void)argc;
5106 (void)argv;
5107 (void)pzErr;
5108 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
5109 if( rc==SQLITE_OK ){
5110 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
5111 if( pNew==0 ) return SQLITE_NOMEM;
5112 memset(pNew, 0, sizeof(*pNew));
5113 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
5114 }
5115 *ppVtab = (sqlite3_vtab*)pNew;
5116 return rc;
5117 }
5118
5119 /*
5120 ** This method is the destructor for fsdir vtab objects.
5121 */
5122 static int fsdirDisconnect(sqlite3_vtab *pVtab){
5123 sqlite3_free(pVtab);
5124 return SQLITE_OK;
5125 }
5126
5127 /*
5128 ** Constructor for a new fsdir_cursor object.
5129 */
5130 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
5131 fsdir_cursor *pCur;
5132 (void)p;
5133 pCur = sqlite3_malloc( sizeof(*pCur) );
5134 if( pCur==0 ) return SQLITE_NOMEM;
5135 memset(pCur, 0, sizeof(*pCur));
5136 pCur->iLvl = -1;
5137 *ppCursor = &pCur->base;
5138 return SQLITE_OK;
5139 }
5140
5141 /*
5142 ** Reset a cursor back to the state it was in when first returned
5143 ** by fsdirOpen().
5144 */
5145 static void fsdirResetCursor(fsdir_cursor *pCur){
5146 int i;
5147 for(i=0; i<=pCur->iLvl; i++){
5148 FsdirLevel *pLvl = &pCur->aLvl[i];
5149 if( pLvl->pDir ) closedir(pLvl->pDir);
5150 sqlite3_free(pLvl->zDir);
5151 }
5152 sqlite3_free(pCur->zPath);
5153 sqlite3_free(pCur->aLvl);
5154 pCur->aLvl = 0;
5155 pCur->zPath = 0;
5156 pCur->zBase = 0;
5157 pCur->nBase = 0;
5158 pCur->nLvl = 0;
5159 pCur->iLvl = -1;
5160 pCur->iRowid = 1;
5161 }
5162
5163 /*
5164 ** Destructor for an fsdir_cursor.
5165 */
5166 static int fsdirClose(sqlite3_vtab_cursor *cur){
5167 fsdir_cursor *pCur = (fsdir_cursor*)cur;
5168
5169 fsdirResetCursor(pCur);
5170 sqlite3_free(pCur);
5171 return SQLITE_OK;
5172 }
5173
5174 /*
5175 ** Set the error message for the virtual table associated with cursor
5176 ** pCur to the results of vprintf(zFmt, ...).
5177 */
5178 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
5179 va_list ap;
5180 va_start(ap, zFmt);
5181 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
5182 va_end(ap);
5183 }
5184
5185
5186 /*
5187 ** Advance an fsdir_cursor to its next row of output.
5188 */
5189 static int fsdirNext(sqlite3_vtab_cursor *cur){
5190 fsdir_cursor *pCur = (fsdir_cursor*)cur;
5191 mode_t m = pCur->sStat.st_mode;
5192
5193 pCur->iRowid++;
5194 if( S_ISDIR(m) ){
5195 /* Descend into this directory */
5196 int iNew = pCur->iLvl + 1;
5197 FsdirLevel *pLvl;
5198 if( iNew>=pCur->nLvl ){
5199 int nNew = iNew+1;
5200 sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
5201 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
5202 if( aNew==0 ) return SQLITE_NOMEM;
5203 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
5204 pCur->aLvl = aNew;
5205 pCur->nLvl = nNew;
5206 }
5207 pCur->iLvl = iNew;
5208 pLvl = &pCur->aLvl[iNew];
5209
5210 pLvl->zDir = pCur->zPath;
5211 pCur->zPath = 0;
5212 pLvl->pDir = opendir(pLvl->zDir);
5213 if( pLvl->pDir==0 ){
5214 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
5215 return SQLITE_ERROR;
5216 }
5217 }
5218
5219 while( pCur->iLvl>=0 ){
5220 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
5221 struct dirent *pEntry = readdir(pLvl->pDir);
5222 if( pEntry ){
5223 if( pEntry->d_name[0]=='.' ){
5224 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
5225 if( pEntry->d_name[1]=='\0' ) continue;
5226 }
5227 sqlite3_free(pCur->zPath);
5228 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
5229 if( pCur->zPath==0 ) return SQLITE_NOMEM;
5230 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
5231 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
5232 return SQLITE_ERROR;
5233 }
5234 return SQLITE_OK;
5235 }
5236 closedir(pLvl->pDir);
5237 sqlite3_free(pLvl->zDir);
5238 pLvl->pDir = 0;
5239 pLvl->zDir = 0;
5240 pCur->iLvl--;
5241 }
5242
5243 /* EOF */
5244 sqlite3_free(pCur->zPath);
5245 pCur->zPath = 0;
5246 return SQLITE_OK;
5247 }
5248
5249 /*
5250 ** Return values of columns for the row at which the series_cursor
5251 ** is currently pointing.
5252 */
5253 static int fsdirColumn(
5254 sqlite3_vtab_cursor *cur, /* The cursor */
5255 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5256 int i /* Which column to return */
5257 ){
5258 fsdir_cursor *pCur = (fsdir_cursor*)cur;
5259 switch( i ){
5260 case FSDIR_COLUMN_NAME: {
5261 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
5262 break;
5263 }
5264
5265 case FSDIR_COLUMN_MODE:
5266 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
5267 break;
5268
5269 case FSDIR_COLUMN_MTIME:
5270 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
5271 break;
5272
5273 case FSDIR_COLUMN_DATA: {
5274 mode_t m = pCur->sStat.st_mode;
5275 if( S_ISDIR(m) ){
5276 sqlite3_result_null(ctx);
5277 #if !defined(_WIN32) && !defined(WIN32)
5278 }else if( S_ISLNK(m) ){
5279 char aStatic[64];
5280 char *aBuf = aStatic;
5281 sqlite3_int64 nBuf = 64;
5282 int n;
5283
5284 while( 1 ){
5285 n = readlink(pCur->zPath, aBuf, nBuf);
5286 if( n<nBuf ) break;
5287 if( aBuf!=aStatic ) sqlite3_free(aBuf);
5288 nBuf = nBuf*2;
5289 aBuf = sqlite3_malloc64(nBuf);
5290 if( aBuf==0 ){
5291 sqlite3_result_error_nomem(ctx);
5292 return SQLITE_NOMEM;
5293 }
5294 }
5295
5296 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
5297 if( aBuf!=aStatic ) sqlite3_free(aBuf);
5298 #endif
5299 }else{
5300 readFileContents(ctx, pCur->zPath);
5301 }
5302 }
5303 case FSDIR_COLUMN_PATH:
5304 default: {
5305 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
5306 ** always return their values as NULL */
5307 break;
5308 }
5309 }
5310 return SQLITE_OK;
5311 }
5312
5313 /*
5314 ** Return the rowid for the current row. In this implementation, the
5315 ** first row returned is assigned rowid value 1, and each subsequent
5316 ** row a value 1 more than that of the previous.
5317 */
5318 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5319 fsdir_cursor *pCur = (fsdir_cursor*)cur;
5320 *pRowid = pCur->iRowid;
5321 return SQLITE_OK;
5322 }
5323
5324 /*
5325 ** Return TRUE if the cursor has been moved off of the last
5326 ** row of output.
5327 */
5328 static int fsdirEof(sqlite3_vtab_cursor *cur){
5329 fsdir_cursor *pCur = (fsdir_cursor*)cur;
5330 return (pCur->zPath==0);
5331 }
5332
5333 /*
5334 ** xFilter callback.
5335 **
5336 ** idxNum==1 PATH parameter only
5337 ** idxNum==2 Both PATH and DIR supplied
5338 */
5339 static int fsdirFilter(
5340 sqlite3_vtab_cursor *cur,
5341 int idxNum, const char *idxStr,
5342 int argc, sqlite3_value **argv
5343 ){
5344 const char *zDir = 0;
5345 fsdir_cursor *pCur = (fsdir_cursor*)cur;
5346 (void)idxStr;
5347 fsdirResetCursor(pCur);
5348
5349 if( idxNum==0 ){
5350 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
5351 return SQLITE_ERROR;
5352 }
5353
5354 assert( argc==idxNum && (argc==1 || argc==2) );
5355 zDir = (const char*)sqlite3_value_text(argv[0]);
5356 if( zDir==0 ){
5357 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
5358 return SQLITE_ERROR;
5359 }
5360 if( argc==2 ){
5361 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
5362 }
5363 if( pCur->zBase ){
5364 pCur->nBase = (int)strlen(pCur->zBase)+1;
5365 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
5366 }else{
5367 pCur->zPath = sqlite3_mprintf("%s", zDir);
5368 }
5369
5370 if( pCur->zPath==0 ){
5371 return SQLITE_NOMEM;
5372 }
5373 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
5374 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
5375 return SQLITE_ERROR;
5376 }
5377
5378 return SQLITE_OK;
5379 }
5380
5381 /*
5382 ** SQLite will invoke this method one or more times while planning a query
5383 ** that uses the generate_series virtual table. This routine needs to create
5384 ** a query plan for each invocation and compute an estimated cost for that
5385 ** plan.
5386 **
5387 ** In this implementation idxNum is used to represent the
5388 ** query plan. idxStr is unused.
5389 **
5390 ** The query plan is represented by values of idxNum:
5391 **
5392 ** (1) The path value is supplied by argv[0]
5393 ** (2) Path is in argv[0] and dir is in argv[1]
5394 */
5395 static int fsdirBestIndex(
5396 sqlite3_vtab *tab,
5397 sqlite3_index_info *pIdxInfo
5398 ){
5399 int i; /* Loop over constraints */
5400 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
5401 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
5402 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
5403 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
5404 const struct sqlite3_index_constraint *pConstraint;
5405
5406 (void)tab;
5407 pConstraint = pIdxInfo->aConstraint;
5408 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5409 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
5410 switch( pConstraint->iColumn ){
5411 case FSDIR_COLUMN_PATH: {
5412 if( pConstraint->usable ){
5413 idxPath = i;
5414 seenPath = 0;
5415 }else if( idxPath<0 ){
5416 seenPath = 1;
5417 }
5418 break;
5419 }
5420 case FSDIR_COLUMN_DIR: {
5421 if( pConstraint->usable ){
5422 idxDir = i;
5423 seenDir = 0;
5424 }else if( idxDir<0 ){
5425 seenDir = 1;
5426 }
5427 break;
5428 }
5429 }
5430 }
5431 if( seenPath || seenDir ){
5432 /* If input parameters are unusable, disallow this plan */
5433 return SQLITE_CONSTRAINT;
5434 }
5435
5436 if( idxPath<0 ){
5437 pIdxInfo->idxNum = 0;
5438 /* The pIdxInfo->estimatedCost should have been initialized to a huge
5439 ** number. Leave it unchanged. */
5440 pIdxInfo->estimatedRows = 0x7fffffff;
5441 }else{
5442 pIdxInfo->aConstraintUsage[idxPath].omit = 1;
5443 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
5444 if( idxDir>=0 ){
5445 pIdxInfo->aConstraintUsage[idxDir].omit = 1;
5446 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
5447 pIdxInfo->idxNum = 2;
5448 pIdxInfo->estimatedCost = 10.0;
5449 }else{
5450 pIdxInfo->idxNum = 1;
5451 pIdxInfo->estimatedCost = 100.0;
5452 }
5453 }
5454
5455 return SQLITE_OK;
5456 }
5457
5458 /*
5459 ** Register the "fsdir" virtual table.
5460 */
5461 static int fsdirRegister(sqlite3 *db){
5462 static sqlite3_module fsdirModule = {
5463 0, /* iVersion */
5464 0, /* xCreate */
5465 fsdirConnect, /* xConnect */
5466 fsdirBestIndex, /* xBestIndex */
5467 fsdirDisconnect, /* xDisconnect */
5468 0, /* xDestroy */
5469 fsdirOpen, /* xOpen - open a cursor */
5470 fsdirClose, /* xClose - close a cursor */
5471 fsdirFilter, /* xFilter - configure scan constraints */
5472 fsdirNext, /* xNext - advance a cursor */
5473 fsdirEof, /* xEof - check for end of scan */
5474 fsdirColumn, /* xColumn - read data */
5475 fsdirRowid, /* xRowid - read data */
5476 0, /* xUpdate */
5477 0, /* xBegin */
5478 0, /* xSync */
5479 0, /* xCommit */
5480 0, /* xRollback */
5481 0, /* xFindMethod */
5482 0, /* xRename */
5483 0, /* xSavepoint */
5484 0, /* xRelease */
5485 0, /* xRollbackTo */
5486 0, /* xShadowName */
5487 };
5488
5489 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
5490 return rc;
5491 }
5492 #else /* SQLITE_OMIT_VIRTUALTABLE */
5493 # define fsdirRegister(x) SQLITE_OK
5494 #endif
5495
5496 #ifdef _WIN32
5497
5498 #endif
5499 int sqlite3_fileio_init(
5500 sqlite3 *db,
5501 char **pzErrMsg,
5502 const sqlite3_api_routines *pApi
5503 ){
5504 int rc = SQLITE_OK;
5505 SQLITE_EXTENSION_INIT2(pApi);
5506 (void)pzErrMsg; /* Unused parameter */
5507 rc = sqlite3_create_function(db, "readfile", 1,
5508 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
5509 readfileFunc, 0, 0);
5510 if( rc==SQLITE_OK ){
5511 rc = sqlite3_create_function(db, "writefile", -1,
5512 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
5513 writefileFunc, 0, 0);
5514 }
5515 if( rc==SQLITE_OK ){
5516 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
5517 lsModeFunc, 0, 0);
5518 }
5519 if( rc==SQLITE_OK ){
5520 rc = fsdirRegister(db);
5521 }
5522 return rc;
5523 }
5524
5525 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
5526 /* To allow a standalone DLL, make test_windirent.c use the same
5527 * redefined SQLite API calls as the above extension code does.
5528 * Just pull in this .c to accomplish this. As a beneficial side
5529 * effect, this extension becomes a single translation unit. */
5530 # include "test_windirent.c"
5531 #endif
5532
5533 /************************* End ../ext/misc/fileio.c ********************/
5534 /************************* Begin ../ext/misc/completion.c ******************/
5535 /*
5536 ** 2017-07-10
5537 **
5538 ** The author disclaims copyright to this source code. In place of
5539 ** a legal notice, here is a blessing:
5540 **
5541 ** May you do good and not evil.
5542 ** May you find forgiveness for yourself and forgive others.
5543 ** May you share freely, never taking more than you give.
5544 **
5545 *************************************************************************
5546 **
5547 ** This file implements an eponymous virtual table that returns suggested
5548 ** completions for a partial SQL input.
5549 **
5550 ** Suggested usage:
5551 **
5552 ** SELECT DISTINCT candidate COLLATE nocase
5553 ** FROM completion($prefix,$wholeline)
5554 ** ORDER BY 1;
5555 **
5556 ** The two query parameters are optional. $prefix is the text of the
5557 ** current word being typed and that is to be completed. $wholeline is
5558 ** the complete input line, used for context.
5559 **
5560 ** The raw completion() table might return the same candidate multiple
5561 ** times, for example if the same column name is used to two or more
5562 ** tables. And the candidates are returned in an arbitrary order. Hence,
5563 ** the DISTINCT and ORDER BY are recommended.
5564 **
5565 ** This virtual table operates at the speed of human typing, and so there
5566 ** is no attempt to make it fast. Even a slow implementation will be much
5567 ** faster than any human can type.
5568 **
5569 */
5570 /* #include "sqlite3ext.h" */
5571 SQLITE_EXTENSION_INIT1
5572 #include <assert.h>
5573 #include <string.h>
5574 #include <ctype.h>
5575
5576 #ifndef SQLITE_OMIT_VIRTUALTABLE
5577
5578 /* completion_vtab is a subclass of sqlite3_vtab which will
5579 ** serve as the underlying representation of a completion virtual table
5580 */
5581 typedef struct completion_vtab completion_vtab;
5582 struct completion_vtab {
5583 sqlite3_vtab base; /* Base class - must be first */
5584 sqlite3 *db; /* Database connection for this completion vtab */
5585 };
5586
5587 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
5588 ** serve as the underlying representation of a cursor that scans
5589 ** over rows of the result
5590 */
5591 typedef struct completion_cursor completion_cursor;
5592 struct completion_cursor {
5593 sqlite3_vtab_cursor base; /* Base class - must be first */
5594 sqlite3 *db; /* Database connection for this cursor */
5595 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
5596 char *zPrefix; /* The prefix for the word we want to complete */
5597 char *zLine; /* The whole that we want to complete */
5598 const char *zCurrentRow; /* Current output row */
5599 int szRow; /* Length of the zCurrentRow string */
5600 sqlite3_stmt *pStmt; /* Current statement */
5601 sqlite3_int64 iRowid; /* The rowid */
5602 int ePhase; /* Current phase */
5603 int j; /* inter-phase counter */
5604 };
5605
5606 /* Values for ePhase:
5607 */
5608 #define COMPLETION_FIRST_PHASE 1
5609 #define COMPLETION_KEYWORDS 1
5610 #define COMPLETION_PRAGMAS 2
5611 #define COMPLETION_FUNCTIONS 3
5612 #define COMPLETION_COLLATIONS 4
5613 #define COMPLETION_INDEXES 5
5614 #define COMPLETION_TRIGGERS 6
5615 #define COMPLETION_DATABASES 7
5616 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
5617 #define COMPLETION_COLUMNS 9
5618 #define COMPLETION_MODULES 10
5619 #define COMPLETION_EOF 11
5620
5621 /*
5622 ** The completionConnect() method is invoked to create a new
5623 ** completion_vtab that describes the completion virtual table.
5624 **
5625 ** Think of this routine as the constructor for completion_vtab objects.
5626 **
5627 ** All this routine needs to do is:
5628 **
5629 ** (1) Allocate the completion_vtab object and initialize all fields.
5630 **
5631 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5632 ** result set of queries against completion will look like.
5633 */
5634 static int completionConnect(
5635 sqlite3 *db,
5636 void *pAux,
5637 int argc, const char *const*argv,
5638 sqlite3_vtab **ppVtab,
5639 char **pzErr
5640 ){
5641 completion_vtab *pNew;
5642 int rc;
5643
5644 (void)(pAux); /* Unused parameter */
5645 (void)(argc); /* Unused parameter */
5646 (void)(argv); /* Unused parameter */
5647 (void)(pzErr); /* Unused parameter */
5648
5649 /* Column numbers */
5650 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
5651 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
5652 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
5653 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
5654
5655 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5656 rc = sqlite3_declare_vtab(db,
5657 "CREATE TABLE x("
5658 " candidate TEXT,"
5659 " prefix TEXT HIDDEN,"
5660 " wholeline TEXT HIDDEN,"
5661 " phase INT HIDDEN" /* Used for debugging only */
5662 ")");
5663 if( rc==SQLITE_OK ){
5664 pNew = sqlite3_malloc( sizeof(*pNew) );
5665 *ppVtab = (sqlite3_vtab*)pNew;
5666 if( pNew==0 ) return SQLITE_NOMEM;
5667 memset(pNew, 0, sizeof(*pNew));
5668 pNew->db = db;
5669 }
5670 return rc;
5671 }
5672
5673 /*
5674 ** This method is the destructor for completion_cursor objects.
5675 */
5676 static int completionDisconnect(sqlite3_vtab *pVtab){
5677 sqlite3_free(pVtab);
5678 return SQLITE_OK;
5679 }
5680
5681 /*
5682 ** Constructor for a new completion_cursor object.
5683 */
5684 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
5685 completion_cursor *pCur;
5686 pCur = sqlite3_malloc( sizeof(*pCur) );
5687 if( pCur==0 ) return SQLITE_NOMEM;
5688 memset(pCur, 0, sizeof(*pCur));
5689 pCur->db = ((completion_vtab*)p)->db;
5690 *ppCursor = &pCur->base;
5691 return SQLITE_OK;
5692 }
5693
5694 /*
5695 ** Reset the completion_cursor.
5696 */
5697 static void completionCursorReset(completion_cursor *pCur){
5698 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
5699 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
5700 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
5701 pCur->j = 0;
5702 }
5703
5704 /*
5705 ** Destructor for a completion_cursor.
5706 */
5707 static int completionClose(sqlite3_vtab_cursor *cur){
5708 completionCursorReset((completion_cursor*)cur);
5709 sqlite3_free(cur);
5710 return SQLITE_OK;
5711 }
5712
5713 /*
5714 ** Advance a completion_cursor to its next row of output.
5715 **
5716 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
5717 ** record the current state of the scan. This routine sets ->zCurrentRow
5718 ** to the current row of output and then returns. If no more rows remain,
5719 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
5720 ** table that has reached the end of its scan.
5721 **
5722 ** The current implementation just lists potential identifiers and
5723 ** keywords and filters them by zPrefix. Future enhancements should
5724 ** take zLine into account to try to restrict the set of identifiers and
5725 ** keywords based on what would be legal at the current point of input.
5726 */
5727 static int completionNext(sqlite3_vtab_cursor *cur){
5728 completion_cursor *pCur = (completion_cursor*)cur;
5729 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
5730 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
5731 pCur->iRowid++;
5732 while( pCur->ePhase!=COMPLETION_EOF ){
5733 switch( pCur->ePhase ){
5734 case COMPLETION_KEYWORDS: {
5735 if( pCur->j >= sqlite3_keyword_count() ){
5736 pCur->zCurrentRow = 0;
5737 pCur->ePhase = COMPLETION_DATABASES;
5738 }else{
5739 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
5740 }
5741 iCol = -1;
5742 break;
5743 }
5744 case COMPLETION_DATABASES: {
5745 if( pCur->pStmt==0 ){
5746 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
5747 &pCur->pStmt, 0);
5748 }
5749 iCol = 1;
5750 eNextPhase = COMPLETION_TABLES;
5751 break;
5752 }
5753 case COMPLETION_TABLES: {
5754 if( pCur->pStmt==0 ){
5755 sqlite3_stmt *pS2;
5756 char *zSql = 0;
5757 const char *zSep = "";
5758 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
5759 while( sqlite3_step(pS2)==SQLITE_ROW ){
5760 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
5761 zSql = sqlite3_mprintf(
5762 "%z%s"
5763 "SELECT name FROM \"%w\".sqlite_schema",
5764 zSql, zSep, zDb
5765 );
5766 if( zSql==0 ) return SQLITE_NOMEM;
5767 zSep = " UNION ";
5768 }
5769 sqlite3_finalize(pS2);
5770 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
5771 sqlite3_free(zSql);
5772 }
5773 iCol = 0;
5774 eNextPhase = COMPLETION_COLUMNS;
5775 break;
5776 }
5777 case COMPLETION_COLUMNS: {
5778 if( pCur->pStmt==0 ){
5779 sqlite3_stmt *pS2;
5780 char *zSql = 0;
5781 const char *zSep = "";
5782 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
5783 while( sqlite3_step(pS2)==SQLITE_ROW ){
5784 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
5785 zSql = sqlite3_mprintf(
5786 "%z%s"
5787 "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
5788 " JOIN pragma_table_info(sm.name,%Q) AS pti"
5789 " WHERE sm.type='table'",
5790 zSql, zSep, zDb, zDb
5791 );
5792 if( zSql==0 ) return SQLITE_NOMEM;
5793 zSep = " UNION ";
5794 }
5795 sqlite3_finalize(pS2);
5796 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
5797 sqlite3_free(zSql);
5798 }
5799 iCol = 0;
5800 eNextPhase = COMPLETION_EOF;
5801 break;
5802 }
5803 }
5804 if( iCol<0 ){
5805 /* This case is when the phase presets zCurrentRow */
5806 if( pCur->zCurrentRow==0 ) continue;
5807 }else{
5808 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
5809 /* Extract the next row of content */
5810 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
5811 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
5812 }else{
5813 /* When all rows are finished, advance to the next phase */
5814 sqlite3_finalize(pCur->pStmt);
5815 pCur->pStmt = 0;
5816 pCur->ePhase = eNextPhase;
5817 continue;
5818 }
5819 }
5820 if( pCur->nPrefix==0 ) break;
5821 if( pCur->nPrefix<=pCur->szRow
5822 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
5823 ){
5824 break;
5825 }
5826 }
5827
5828 return SQLITE_OK;
5829 }
5830
5831 /*
5832 ** Return values of columns for the row at which the completion_cursor
5833 ** is currently pointing.
5834 */
5835 static int completionColumn(
5836 sqlite3_vtab_cursor *cur, /* The cursor */
5837 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5838 int i /* Which column to return */
5839 ){
5840 completion_cursor *pCur = (completion_cursor*)cur;
5841 switch( i ){
5842 case COMPLETION_COLUMN_CANDIDATE: {
5843 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
5844 break;
5845 }
5846 case COMPLETION_COLUMN_PREFIX: {
5847 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
5848 break;
5849 }
5850 case COMPLETION_COLUMN_WHOLELINE: {
5851 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
5852 break;
5853 }
5854 case COMPLETION_COLUMN_PHASE: {
5855 sqlite3_result_int(ctx, pCur->ePhase);
5856 break;
5857 }
5858 }
5859 return SQLITE_OK;
5860 }
5861
5862 /*
5863 ** Return the rowid for the current row. In this implementation, the
5864 ** rowid is the same as the output value.
5865 */
5866 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5867 completion_cursor *pCur = (completion_cursor*)cur;
5868 *pRowid = pCur->iRowid;
5869 return SQLITE_OK;
5870 }
5871
5872 /*
5873 ** Return TRUE if the cursor has been moved off of the last
5874 ** row of output.
5875 */
5876 static int completionEof(sqlite3_vtab_cursor *cur){
5877 completion_cursor *pCur = (completion_cursor*)cur;
5878 return pCur->ePhase >= COMPLETION_EOF;
5879 }
5880
5881 /*
5882 ** This method is called to "rewind" the completion_cursor object back
5883 ** to the first row of output. This method is always called at least
5884 ** once prior to any call to completionColumn() or completionRowid() or
5885 ** completionEof().
5886 */
5887 static int completionFilter(
5888 sqlite3_vtab_cursor *pVtabCursor,
5889 int idxNum, const char *idxStr,
5890 int argc, sqlite3_value **argv
5891 ){
5892 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
5893 int iArg = 0;
5894 (void)(idxStr); /* Unused parameter */
5895 (void)(argc); /* Unused parameter */
5896 completionCursorReset(pCur);
5897 if( idxNum & 1 ){
5898 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
5899 if( pCur->nPrefix>0 ){
5900 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
5901 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
5902 }
5903 iArg = 1;
5904 }
5905 if( idxNum & 2 ){
5906 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
5907 if( pCur->nLine>0 ){
5908 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
5909 if( pCur->zLine==0 ) return SQLITE_NOMEM;
5910 }
5911 }
5912 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
5913 int i = pCur->nLine;
5914 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
5915 i--;
5916 }
5917 pCur->nPrefix = pCur->nLine - i;
5918 if( pCur->nPrefix>0 ){
5919 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
5920 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
5921 }
5922 }
5923 pCur->iRowid = 0;
5924 pCur->ePhase = COMPLETION_FIRST_PHASE;
5925 return completionNext(pVtabCursor);
5926 }
5927
5928 /*
5929 ** SQLite will invoke this method one or more times while planning a query
5930 ** that uses the completion virtual table. This routine needs to create
5931 ** a query plan for each invocation and compute an estimated cost for that
5932 ** plan.
5933 **
5934 ** There are two hidden parameters that act as arguments to the table-valued
5935 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
5936 ** is available and bit 1 is set if "wholeline" is available.
5937 */
5938 static int completionBestIndex(
5939 sqlite3_vtab *tab,
5940 sqlite3_index_info *pIdxInfo
5941 ){
5942 int i; /* Loop over constraints */
5943 int idxNum = 0; /* The query plan bitmask */
5944 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
5945 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
5946 int nArg = 0; /* Number of arguments that completeFilter() expects */
5947 const struct sqlite3_index_constraint *pConstraint;
5948
5949 (void)(tab); /* Unused parameter */
5950 pConstraint = pIdxInfo->aConstraint;
5951 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5952 if( pConstraint->usable==0 ) continue;
5953 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
5954 switch( pConstraint->iColumn ){
5955 case COMPLETION_COLUMN_PREFIX:
5956 prefixIdx = i;
5957 idxNum |= 1;
5958 break;
5959 case COMPLETION_COLUMN_WHOLELINE:
5960 wholelineIdx = i;
5961 idxNum |= 2;
5962 break;
5963 }
5964 }
5965 if( prefixIdx>=0 ){
5966 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
5967 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
5968 }
5969 if( wholelineIdx>=0 ){
5970 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
5971 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
5972 }
5973 pIdxInfo->idxNum = idxNum;
5974 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
5975 pIdxInfo->estimatedRows = 500 - 100*nArg;
5976 return SQLITE_OK;
5977 }
5978
5979 /*
5980 ** This following structure defines all the methods for the
5981 ** completion virtual table.
5982 */
5983 static sqlite3_module completionModule = {
5984 0, /* iVersion */
5985 0, /* xCreate */
5986 completionConnect, /* xConnect */
5987 completionBestIndex, /* xBestIndex */
5988 completionDisconnect, /* xDisconnect */
5989 0, /* xDestroy */
5990 completionOpen, /* xOpen - open a cursor */
5991 completionClose, /* xClose - close a cursor */
5992 completionFilter, /* xFilter - configure scan constraints */
5993 completionNext, /* xNext - advance a cursor */
5994 completionEof, /* xEof - check for end of scan */
5995 completionColumn, /* xColumn - read data */
5996 completionRowid, /* xRowid - read data */
5997 0, /* xUpdate */
5998 0, /* xBegin */
5999 0, /* xSync */
6000 0, /* xCommit */
6001 0, /* xRollback */
6002 0, /* xFindMethod */
6003 0, /* xRename */
6004 0, /* xSavepoint */
6005 0, /* xRelease */
6006 0, /* xRollbackTo */
6007 0 /* xShadowName */
6008 };
6009
6010 #endif /* SQLITE_OMIT_VIRTUALTABLE */
6011
6012 int sqlite3CompletionVtabInit(sqlite3 *db){
6013 int rc = SQLITE_OK;
6014 #ifndef SQLITE_OMIT_VIRTUALTABLE
6015 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
6016 #endif
6017 return rc;
6018 }
6019
6020 #ifdef _WIN32
6021
6022 #endif
6023 int sqlite3_completion_init(
6024 sqlite3 *db,
6025 char **pzErrMsg,
6026 const sqlite3_api_routines *pApi
6027 ){
6028 int rc = SQLITE_OK;
6029 SQLITE_EXTENSION_INIT2(pApi);
6030 (void)(pzErrMsg); /* Unused parameter */
6031 #ifndef SQLITE_OMIT_VIRTUALTABLE
6032 rc = sqlite3CompletionVtabInit(db);
6033 #endif
6034 return rc;
6035 }
6036
6037 /************************* End ../ext/misc/completion.c ********************/
6038 /************************* Begin ../ext/misc/appendvfs.c ******************/
6039 /*
6040 ** 2017-10-20
6041 **
6042 ** The author disclaims copyright to this source code. In place of
6043 ** a legal notice, here is a blessing:
6044 **
6045 ** May you do good and not evil.
6046 ** May you find forgiveness for yourself and forgive others.
6047 ** May you share freely, never taking more than you give.
6048 **
6049 ******************************************************************************
6050 **
6051 ** This file implements a VFS shim that allows an SQLite database to be
6052 ** appended onto the end of some other file, such as an executable.
6053 **
6054 ** A special record must appear at the end of the file that identifies the
6055 ** file as an appended database and provides the offset to the first page
6056 ** of the exposed content. (Or, it is the length of the content prefix.)
6057 ** For best performance page 1 should be located at a disk page boundary,
6058 ** though that is not required.
6059 **
6060 ** When opening a database using this VFS, the connection might treat
6061 ** the file as an ordinary SQLite database, or it might treat it as a
6062 ** database appended onto some other file. The decision is made by
6063 ** applying the following rules in order:
6064 **
6065 ** (1) An empty file is an ordinary database.
6066 **
6067 ** (2) If the file ends with the appendvfs trailer string
6068 ** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
6069 **
6070 ** (3) If the file begins with the standard SQLite prefix string
6071 ** "SQLite format 3", that file is an ordinary database.
6072 **
6073 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
6074 ** set, then a new database is appended to the already existing file.
6075 **
6076 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
6077 **
6078 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
6079 ** the file containing the database is limited to 1GiB. (1073741824 bytes)
6080 ** This VFS will not read or write past the 1GiB mark. This restriction
6081 ** might be lifted in future versions. For now, if you need a larger
6082 ** database, then keep it in a separate file.
6083 **
6084 ** If the file being opened is a plain database (not an appended one), then
6085 ** this shim is a pass-through into the default underlying VFS. (rule 3)
6086 **/
6087 /* #include "sqlite3ext.h" */
6088 SQLITE_EXTENSION_INIT1
6089 #include <string.h>
6090 #include <assert.h>
6091
6092 /* The append mark at the end of the database is:
6093 **
6094 ** Start-Of-SQLite3-NNNNNNNN
6095 ** 123456789 123456789 12345
6096 **
6097 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
6098 ** the offset to page 1, and also the length of the prefix content.
6099 */
6100 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
6101 #define APND_MARK_PREFIX_SZ 17
6102 #define APND_MARK_FOS_SZ 8
6103 #define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
6104
6105 /*
6106 ** Maximum size of the combined prefix + database + append-mark. This
6107 ** must be less than 0x40000000 to avoid locking issues on Windows.
6108 */
6109 #define APND_MAX_SIZE (0x40000000)
6110
6111 /*
6112 ** Try to align the database to an even multiple of APND_ROUNDUP bytes.
6113 */
6114 #ifndef APND_ROUNDUP
6115 #define APND_ROUNDUP 4096
6116 #endif
6117 #define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1))
6118 #define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
6119
6120 /*
6121 ** Forward declaration of objects used by this utility
6122 */
6123 typedef struct sqlite3_vfs ApndVfs;
6124 typedef struct ApndFile ApndFile;
6125
6126 /* Access to a lower-level VFS that (might) implement dynamic loading,
6127 ** access to randomness, etc.
6128 */
6129 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
6130 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
6131
6132 /* An open appendvfs file
6133 **
6134 ** An instance of this structure describes the appended database file.
6135 ** A separate sqlite3_file object is always appended. The appended
6136 ** sqlite3_file object (which can be accessed using ORIGFILE()) describes
6137 ** the entire file, including the prefix, the database, and the
6138 ** append-mark.
6139 **
6140 ** The structure of an AppendVFS database is like this:
6141 **
6142 ** +-------------+---------+----------+-------------+
6143 ** | prefix-file | padding | database | append-mark |
6144 ** +-------------+---------+----------+-------------+
6145 ** ^ ^
6146 ** | |
6147 ** iPgOne iMark
6148 **
6149 **
6150 ** "prefix file" - file onto which the database has been appended.
6151 ** "padding" - zero or more bytes inserted so that "database"
6152 ** starts on an APND_ROUNDUP boundary
6153 ** "database" - The SQLite database file
6154 ** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
6155 ** the offset from the start of prefix-file to the start
6156 ** of "database".
6157 **
6158 ** The size of the database is iMark - iPgOne.
6159 **
6160 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
6161 ** of iPgOne stored as a big-ending 64-bit integer.
6162 **
6163 ** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
6164 ** Or, iMark is -1 to indicate that it has not yet been written.
6165 */
6166 struct ApndFile {
6167 sqlite3_file base; /* Subclass. MUST BE FIRST! */
6168 sqlite3_int64 iPgOne; /* Offset to the start of the database */
6169 sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */
6170 /* Always followed by another sqlite3_file that describes the whole file */
6171 };
6172
6173 /*
6174 ** Methods for ApndFile
6175 */
6176 static int apndClose(sqlite3_file*);
6177 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
6178 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
6179 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
6180 static int apndSync(sqlite3_file*, int flags);
6181 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
6182 static int apndLock(sqlite3_file*, int);
6183 static int apndUnlock(sqlite3_file*, int);
6184 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
6185 static int apndFileControl(sqlite3_file*, int op, void *pArg);
6186 static int apndSectorSize(sqlite3_file*);
6187 static int apndDeviceCharacteristics(sqlite3_file*);
6188 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
6189 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
6190 static void apndShmBarrier(sqlite3_file*);
6191 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
6192 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
6193 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
6194
6195 /*
6196 ** Methods for ApndVfs
6197 */
6198 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
6199 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
6200 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
6201 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
6202 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
6203 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
6204 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
6205 static void apndDlClose(sqlite3_vfs*, void*);
6206 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
6207 static int apndSleep(sqlite3_vfs*, int microseconds);
6208 static int apndCurrentTime(sqlite3_vfs*, double*);
6209 static int apndGetLastError(sqlite3_vfs*, int, char *);
6210 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
6211 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
6212 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
6213 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
6214
6215 static sqlite3_vfs apnd_vfs = {
6216 3, /* iVersion (set when registered) */
6217 0, /* szOsFile (set when registered) */
6218 1024, /* mxPathname */
6219 0, /* pNext */
6220 "apndvfs", /* zName */
6221 0, /* pAppData (set when registered) */
6222 apndOpen, /* xOpen */
6223 apndDelete, /* xDelete */
6224 apndAccess, /* xAccess */
6225 apndFullPathname, /* xFullPathname */
6226 apndDlOpen, /* xDlOpen */
6227 apndDlError, /* xDlError */
6228 apndDlSym, /* xDlSym */
6229 apndDlClose, /* xDlClose */
6230 apndRandomness, /* xRandomness */
6231 apndSleep, /* xSleep */
6232 apndCurrentTime, /* xCurrentTime */
6233 apndGetLastError, /* xGetLastError */
6234 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
6235 apndSetSystemCall, /* xSetSystemCall */
6236 apndGetSystemCall, /* xGetSystemCall */
6237 apndNextSystemCall /* xNextSystemCall */
6238 };
6239
6240 static const sqlite3_io_methods apnd_io_methods = {
6241 3, /* iVersion */
6242 apndClose, /* xClose */
6243 apndRead, /* xRead */
6244 apndWrite, /* xWrite */
6245 apndTruncate, /* xTruncate */
6246 apndSync, /* xSync */
6247 apndFileSize, /* xFileSize */
6248 apndLock, /* xLock */
6249 apndUnlock, /* xUnlock */
6250 apndCheckReservedLock, /* xCheckReservedLock */
6251 apndFileControl, /* xFileControl */
6252 apndSectorSize, /* xSectorSize */
6253 apndDeviceCharacteristics, /* xDeviceCharacteristics */
6254 apndShmMap, /* xShmMap */
6255 apndShmLock, /* xShmLock */
6256 apndShmBarrier, /* xShmBarrier */
6257 apndShmUnmap, /* xShmUnmap */
6258 apndFetch, /* xFetch */
6259 apndUnfetch /* xUnfetch */
6260 };
6261
6262 /*
6263 ** Close an apnd-file.
6264 */
6265 static int apndClose(sqlite3_file *pFile){
6266 pFile = ORIGFILE(pFile);
6267 return pFile->pMethods->xClose(pFile);
6268 }
6269
6270 /*
6271 ** Read data from an apnd-file.
6272 */
6273 static int apndRead(
6274 sqlite3_file *pFile,
6275 void *zBuf,
6276 int iAmt,
6277 sqlite_int64 iOfst
6278 ){
6279 ApndFile *paf = (ApndFile *)pFile;
6280 pFile = ORIGFILE(pFile);
6281 return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
6282 }
6283
6284 /*
6285 ** Add the append-mark onto what should become the end of the file.
6286 * If and only if this succeeds, internal ApndFile.iMark is updated.
6287 * Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
6288 */
6289 static int apndWriteMark(
6290 ApndFile *paf,
6291 sqlite3_file *pFile,
6292 sqlite_int64 iWriteEnd
6293 ){
6294 sqlite_int64 iPgOne = paf->iPgOne;
6295 unsigned char a[APND_MARK_SIZE];
6296 int i = APND_MARK_FOS_SZ;
6297 int rc;
6298 assert(pFile == ORIGFILE(paf));
6299 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
6300 while( --i >= 0 ){
6301 a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
6302 iPgOne >>= 8;
6303 }
6304 iWriteEnd += paf->iPgOne;
6305 if( SQLITE_OK==(rc = pFile->pMethods->xWrite
6306 (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
6307 paf->iMark = iWriteEnd;
6308 }
6309 return rc;
6310 }
6311
6312 /*
6313 ** Write data to an apnd-file.
6314 */
6315 static int apndWrite(
6316 sqlite3_file *pFile,
6317 const void *zBuf,
6318 int iAmt,
6319 sqlite_int64 iOfst
6320 ){
6321 ApndFile *paf = (ApndFile *)pFile;
6322 sqlite_int64 iWriteEnd = iOfst + iAmt;
6323 if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
6324 pFile = ORIGFILE(pFile);
6325 /* If append-mark is absent or will be overwritten, write it. */
6326 if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
6327 int rc = apndWriteMark(paf, pFile, iWriteEnd);
6328 if( SQLITE_OK!=rc ) return rc;
6329 }
6330 return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
6331 }
6332
6333 /*
6334 ** Truncate an apnd-file.
6335 */
6336 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
6337 ApndFile *paf = (ApndFile *)pFile;
6338 pFile = ORIGFILE(pFile);
6339 /* The append mark goes out first so truncate failure does not lose it. */
6340 if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
6341 /* Truncate underlying file just past append mark */
6342 return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
6343 }
6344
6345 /*
6346 ** Sync an apnd-file.
6347 */
6348 static int apndSync(sqlite3_file *pFile, int flags){
6349 pFile = ORIGFILE(pFile);
6350 return pFile->pMethods->xSync(pFile, flags);
6351 }
6352
6353 /*
6354 ** Return the current file-size of an apnd-file.
6355 ** If the append mark is not yet there, the file-size is 0.
6356 */
6357 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
6358 ApndFile *paf = (ApndFile *)pFile;
6359 *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
6360 return SQLITE_OK;
6361 }
6362
6363 /*
6364 ** Lock an apnd-file.
6365 */
6366 static int apndLock(sqlite3_file *pFile, int eLock){
6367 pFile = ORIGFILE(pFile);
6368 return pFile->pMethods->xLock(pFile, eLock);
6369 }
6370
6371 /*
6372 ** Unlock an apnd-file.
6373 */
6374 static int apndUnlock(sqlite3_file *pFile, int eLock){
6375 pFile = ORIGFILE(pFile);
6376 return pFile->pMethods->xUnlock(pFile, eLock);
6377 }
6378
6379 /*
6380 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
6381 */
6382 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
6383 pFile = ORIGFILE(pFile);
6384 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
6385 }
6386
6387 /*
6388 ** File control method. For custom operations on an apnd-file.
6389 */
6390 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
6391 ApndFile *paf = (ApndFile *)pFile;
6392 int rc;
6393 pFile = ORIGFILE(pFile);
6394 if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
6395 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
6396 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
6397 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
6398 }
6399 return rc;
6400 }
6401
6402 /*
6403 ** Return the sector-size in bytes for an apnd-file.
6404 */
6405 static int apndSectorSize(sqlite3_file *pFile){
6406 pFile = ORIGFILE(pFile);
6407 return pFile->pMethods->xSectorSize(pFile);
6408 }
6409
6410 /*
6411 ** Return the device characteristic flags supported by an apnd-file.
6412 */
6413 static int apndDeviceCharacteristics(sqlite3_file *pFile){
6414 pFile = ORIGFILE(pFile);
6415 return pFile->pMethods->xDeviceCharacteristics(pFile);
6416 }
6417
6418 /* Create a shared memory file mapping */
6419 static int apndShmMap(
6420 sqlite3_file *pFile,
6421 int iPg,
6422 int pgsz,
6423 int bExtend,
6424 void volatile **pp
6425 ){
6426 pFile = ORIGFILE(pFile);
6427 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
6428 }
6429
6430 /* Perform locking on a shared-memory segment */
6431 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
6432 pFile = ORIGFILE(pFile);
6433 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
6434 }
6435
6436 /* Memory barrier operation on shared memory */
6437 static void apndShmBarrier(sqlite3_file *pFile){
6438 pFile = ORIGFILE(pFile);
6439 pFile->pMethods->xShmBarrier(pFile);
6440 }
6441
6442 /* Unmap a shared memory segment */
6443 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
6444 pFile = ORIGFILE(pFile);
6445 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
6446 }
6447
6448 /* Fetch a page of a memory-mapped file */
6449 static int apndFetch(
6450 sqlite3_file *pFile,
6451 sqlite3_int64 iOfst,
6452 int iAmt,
6453 void **pp
6454 ){
6455 ApndFile *p = (ApndFile *)pFile;
6456 if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
6457 return SQLITE_IOERR; /* Cannot read what is not yet there. */
6458 }
6459 pFile = ORIGFILE(pFile);
6460 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
6461 }
6462
6463 /* Release a memory-mapped page */
6464 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
6465 ApndFile *p = (ApndFile *)pFile;
6466 pFile = ORIGFILE(pFile);
6467 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
6468 }
6469
6470 /*
6471 ** Try to read the append-mark off the end of a file. Return the
6472 ** start of the appended database if the append-mark is present.
6473 ** If there is no valid append-mark, return -1;
6474 **
6475 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
6476 ** indicates that the appended database contains at least one page. The
6477 ** start-of-database value must be a multiple of 512.
6478 */
6479 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
6480 int rc, i;
6481 sqlite3_int64 iMark;
6482 int msbs = 8 * (APND_MARK_FOS_SZ-1);
6483 unsigned char a[APND_MARK_SIZE];
6484
6485 if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
6486 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
6487 if( rc ) return -1;
6488 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
6489 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
6490 for(i=1; i<8; i++){
6491 msbs -= 8;
6492 iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
6493 }
6494 if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
6495 if( iMark & 0x1ff ) return -1;
6496 return iMark;
6497 }
6498
6499 static const char apvfsSqliteHdr[] = "SQLite format 3";
6500 /*
6501 ** Check to see if the file is an appendvfs SQLite database file.
6502 ** Return true iff it is such. Parameter sz is the file's size.
6503 */
6504 static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
6505 int rc;
6506 char zHdr[16];
6507 sqlite3_int64 iMark = apndReadMark(sz, pFile);
6508 if( iMark>=0 ){
6509 /* If file has the correct end-marker, the expected odd size, and the
6510 ** SQLite DB type marker where the end-marker puts it, then it
6511 ** is an appendvfs database.
6512 */
6513 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
6514 if( SQLITE_OK==rc
6515 && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
6516 && (sz & 0x1ff) == APND_MARK_SIZE
6517 && sz>=512+APND_MARK_SIZE
6518 ){
6519 return 1; /* It's an appendvfs database */
6520 }
6521 }
6522 return 0;
6523 }
6524
6525 /*
6526 ** Check to see if the file is an ordinary SQLite database file.
6527 ** Return true iff so. Parameter sz is the file's size.
6528 */
6529 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
6530 char zHdr[16];
6531 if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
6532 || (sz & 0x1ff) != 0
6533 || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
6534 || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
6535 ){
6536 return 0;
6537 }else{
6538 return 1;
6539 }
6540 }
6541
6542 /*
6543 ** Open an apnd file handle.
6544 */
6545 static int apndOpen(
6546 sqlite3_vfs *pApndVfs,
6547 const char *zName,
6548 sqlite3_file *pFile,
6549 int flags,
6550 int *pOutFlags
6551 ){
6552 ApndFile *pApndFile = (ApndFile*)pFile;
6553 sqlite3_file *pBaseFile = ORIGFILE(pFile);
6554 sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
6555 int rc;
6556 sqlite3_int64 sz = 0;
6557 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
6558 /* The appendvfs is not to be used for transient or temporary databases.
6559 ** Just use the base VFS open to initialize the given file object and
6560 ** open the underlying file. (Appendvfs is then unused for this file.)
6561 */
6562 return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
6563 }
6564 memset(pApndFile, 0, sizeof(ApndFile));
6565 pFile->pMethods = &apnd_io_methods;
6566 pApndFile->iMark = -1; /* Append mark not yet written */
6567
6568 rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
6569 if( rc==SQLITE_OK ){
6570 rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
6571 if( rc ){
6572 pBaseFile->pMethods->xClose(pBaseFile);
6573 }
6574 }
6575 if( rc ){
6576 pFile->pMethods = 0;
6577 return rc;
6578 }
6579 if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
6580 /* The file being opened appears to be just an ordinary DB. Copy
6581 ** the base dispatch-table so this instance mimics the base VFS.
6582 */
6583 memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
6584 return SQLITE_OK;
6585 }
6586 pApndFile->iPgOne = apndReadMark(sz, pFile);
6587 if( pApndFile->iPgOne>=0 ){
6588 pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
6589 return SQLITE_OK;
6590 }
6591 if( (flags & SQLITE_OPEN_CREATE)==0 ){
6592 pBaseFile->pMethods->xClose(pBaseFile);
6593 rc = SQLITE_CANTOPEN;
6594 pFile->pMethods = 0;
6595 }else{
6596 /* Round newly added appendvfs location to #define'd page boundary.
6597 ** Note that nothing has yet been written to the underlying file.
6598 ** The append mark will be written along with first content write.
6599 ** Until then, paf->iMark value indicates it is not yet written.
6600 */
6601 pApndFile->iPgOne = APND_START_ROUNDUP(sz);
6602 }
6603 return rc;
6604 }
6605
6606 /*
6607 ** Delete an apnd file.
6608 ** For an appendvfs, this could mean delete the appendvfs portion,
6609 ** leaving the appendee as it was before it gained an appendvfs.
6610 ** For now, this code deletes the underlying file too.
6611 */
6612 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
6613 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
6614 }
6615
6616 /*
6617 ** All other VFS methods are pass-thrus.
6618 */
6619 static int apndAccess(
6620 sqlite3_vfs *pVfs,
6621 const char *zPath,
6622 int flags,
6623 int *pResOut
6624 ){
6625 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
6626 }
6627 static int apndFullPathname(
6628 sqlite3_vfs *pVfs,
6629 const char *zPath,
6630 int nOut,
6631 char *zOut
6632 ){
6633 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
6634 }
6635 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
6636 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
6637 }
6638 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
6639 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
6640 }
6641 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
6642 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
6643 }
6644 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
6645 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
6646 }
6647 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
6648 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
6649 }
6650 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
6651 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
6652 }
6653 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
6654 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
6655 }
6656 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
6657 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
6658 }
6659 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
6660 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
6661 }
6662 static int apndSetSystemCall(
6663 sqlite3_vfs *pVfs,
6664 const char *zName,
6665 sqlite3_syscall_ptr pCall
6666 ){
6667 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
6668 }
6669 static sqlite3_syscall_ptr apndGetSystemCall(
6670 sqlite3_vfs *pVfs,
6671 const char *zName
6672 ){
6673 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
6674 }
6675 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
6676 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
6677 }
6678
6679
6680 #ifdef _WIN32
6681
6682 #endif
6683 /*
6684 ** This routine is called when the extension is loaded.
6685 ** Register the new VFS.
6686 */
6687 int sqlite3_appendvfs_init(
6688 sqlite3 *db,
6689 char **pzErrMsg,
6690 const sqlite3_api_routines *pApi
6691 ){
6692 int rc = SQLITE_OK;
6693 sqlite3_vfs *pOrig;
6694 SQLITE_EXTENSION_INIT2(pApi);
6695 (void)pzErrMsg;
6696 (void)db;
6697 pOrig = sqlite3_vfs_find(0);
6698 if( pOrig==0 ) return SQLITE_ERROR;
6699 apnd_vfs.iVersion = pOrig->iVersion;
6700 apnd_vfs.pAppData = pOrig;
6701 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
6702 rc = sqlite3_vfs_register(&apnd_vfs, 0);
6703 #ifdef APPENDVFS_TEST
6704 if( rc==SQLITE_OK ){
6705 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
6706 }
6707 #endif
6708 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
6709 return rc;
6710 }
6711
6712 /************************* End ../ext/misc/appendvfs.c ********************/
6713 #endif
6714 #ifdef SQLITE_HAVE_ZLIB
6715 /************************* Begin ../ext/misc/zipfile.c ******************/
6716 /*
6717 ** 2017-12-26
6718 **
@@ -12235,11 +12248,21 @@
12248 int nIndent; /* Size of array aiIndent[] */
12249 int iIndent; /* Index of current op in aiIndent[] */
12250 char *zNonce; /* Nonce for temporary safe-mode excapes */
12251 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
12252 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
12253 #ifdef SQLITE_SHELL_WASM_MODE
12254 struct {
12255 const char * zInput; /* Input string from wasm/JS proxy */
12256 const char * zPos; /* Cursor pos into zInput */
12257 } wasm;
12258 #endif
12259 };
12260
12261 #ifdef SQLITE_SHELL_WASM_MODE
12262 static ShellState shellState;
12263 #endif
12264
12265
12266 /* Allowed values for ShellState.autoEQP
12267 */
12268 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
@@ -15304,17 +15327,18 @@
15327
15328 /*
15329 ** Text of help messages.
15330 **
15331 ** The help text for each individual command begins with a line that starts
15332 ** with ".". Subsequent lines are supplemental information.
15333 **
15334 ** There must be two or more spaces between the end of the command and the
15335 ** start of the description of what that command does.
15336 */
15337 static const char *(azHelp[]) = {
15338 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
15339 && !defined(SQLITE_SHELL_WASM_MODE)
15340 ".archive ... Manage SQL archives",
15341 " Each command must have exactly one of the following options:",
15342 " -c, --create Create a new archive",
15343 " -u, --update Add or update files with changed mtime",
15344 " -i, --insert Like -u but always add even if unchanged",
@@ -15336,20 +15360,26 @@
15360 " http://sqlite.org/cli.html#sqlite_archive_support",
15361 #endif
15362 #ifndef SQLITE_OMIT_AUTHORIZATION
15363 ".auth ON|OFF Show authorizer callbacks",
15364 #endif
15365 #ifndef SQLITE_SHELL_WASM_MODE
15366 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
15367 " Options:",
15368 " --append Use the appendvfs",
15369 " --async Write to FILE without journal and fsync()",
15370 #endif
15371 ".bail on|off Stop after hitting an error. Default OFF",
15372 ".binary on|off Turn binary output on or off. Default OFF",
15373 #ifndef SQLITE_SHELL_WASM_MODE
15374 ".cd DIRECTORY Change the working directory to DIRECTORY",
15375 #endif
15376 ".changes on|off Show number of rows changed by SQL",
15377 #ifndef SQLITE_SHELL_WASM_MODE
15378 ".check GLOB Fail if output since .testcase does not match",
15379 ".clone NEWDB Clone data into NEWDB from the existing database",
15380 #endif
15381 ".connection [close] [#] Open or close an auxiliary database connection",
15382 ".databases List names and files of attached databases",
15383 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
15384 ".dbinfo ?DB? Show status information about the database",
15385 ".dump ?OBJECTS? Render database content as SQL",
@@ -15366,21 +15396,26 @@
15396 #ifdef SQLITE_DEBUG
15397 " test Show raw EXPLAIN QUERY PLAN output",
15398 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
15399 #endif
15400 " trigger Like \"full\" but also show trigger bytecode",
15401 #ifndef SQLITE_SHELL_WASM_MODE
15402 ".excel Display the output of next command in spreadsheet",
15403 " --bom Put a UTF8 byte-order mark on intermediate file",
15404 #endif
15405 #ifndef SQLITE_SHELL_WASM_MODE
15406 ".exit ?CODE? Exit this program with return-code CODE",
15407 #endif
15408 ".expert EXPERIMENTAL. Suggest indexes for queries",
15409 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
15410 ".filectrl CMD ... Run various sqlite3_file_control() operations",
15411 " --schema SCHEMA Use SCHEMA instead of \"main\"",
15412 " --help Show CMD details",
15413 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
15414 ".headers on|off Turn display of headers on or off",
15415 ".help ?-all? ?PATTERN? Show help text for PATTERN",
15416 #ifndef SQLITE_SHELL_WASM_MODE
15417 ".import FILE TABLE Import data from FILE into TABLE",
15418 " Options:",
15419 " --ascii Use \\037 and \\036 as column and row separators",
15420 " --csv Use , and \\n as column and row separators",
15421 " --skip N Skip the first N rows of input",
@@ -15391,10 +15426,11 @@
15426 " determines the column names.",
15427 " * If neither --csv or --ascii are used, the input mode is derived",
15428 " from the \".mode\" output mode",
15429 " * If FILE begins with \"|\" then it is a command that generates the",
15430 " input text.",
15431 #endif
15432 #ifndef SQLITE_OMIT_TEST_CONTROL
15433 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
15434 #endif
15435 ".indexes ?TABLE? Show names of indexes",
15436 " If TABLE is specified, only show indexes for",
@@ -15404,14 +15440,16 @@
15440 #endif
15441 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
15442 ".lint OPTIONS Report potential schema issues.",
15443 " Options:",
15444 " fkey-indexes Find missing foreign key indexes",
15445 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_WASM_MODE)
15446 ".load FILE ?ENTRY? Load an extension library",
15447 #endif
15448 #ifndef SQLITE_SHELL_WASM_MODE
15449 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
15450 #endif
15451 ".mode MODE ?OPTIONS? Set output mode",
15452 " MODE is one of:",
15453 " ascii Columns/rows delimited by 0x1F and 0x1E",
15454 " box Tables using unicode box-drawing characters",
15455 " csv Comma-separated values",
@@ -15432,35 +15470,44 @@
15470 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
15471 " --ww Shorthand for \"--wordwrap 1\"",
15472 " --quote Quote output text as SQL literals",
15473 " --noquote Do not quote output text",
15474 " TABLE The name of SQL table used for \"insert\" mode",
15475 #ifndef SQLITE_SHELL_WASM_MODE
15476 ".nonce STRING Suspend safe mode for one command if nonce matches",
15477 #endif
15478 ".nullvalue STRING Use STRING in place of NULL values",
15479 #ifndef SQLITE_SHELL_WASM_MODE
15480 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
15481 " If FILE begins with '|' then open as a pipe",
15482 " --bom Put a UTF8 byte-order mark at the beginning",
15483 " -e Send output to the system text editor",
15484 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
15485 /* Note that .open is (partially) available in WASM builds but is
15486 ** currently only intended to be used by the fiddle tool, not
15487 ** end users, so is "undocumented." */
15488 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
15489 " Options:",
15490 " --append Use appendvfs to append database to the end of FILE",
15491 #endif
15492 #ifndef SQLITE_OMIT_DESERIALIZE
15493 " --deserialize Load into memory using sqlite3_deserialize()",
15494 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
15495 " --maxsize N Maximum size for --hexdb or --deserialized database",
15496 #endif
15497 " --new Initialize FILE to an empty database",
15498 " --nofollow Do not follow symbolic links",
15499 " --readonly Open FILE readonly",
15500 " --zip FILE is a ZIP archive",
15501 #ifndef SQLITE_SHELL_WASM_MODE
15502 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
15503 " If FILE begins with '|' then open it as a pipe.",
15504 " Options:",
15505 " --bom Prefix output with a UTF8 byte-order mark",
15506 " -e Send output to the system text editor",
15507 " -x Send output as CSV to a spreadsheet",
15508 #endif
15509 ".parameter CMD ... Manage SQL parameter bindings",
15510 " clear Erase all bindings",
15511 " init Initialize the TEMP table that holds bindings",
15512 " list List the current parameter bindings",
15513 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
@@ -15473,23 +15520,27 @@
15520 " --once Do no more than one progress interrupt",
15521 " --quiet|-q No output except at interrupts",
15522 " --reset Reset the count for each input and interrupt",
15523 #endif
15524 ".prompt MAIN CONTINUE Replace the standard prompts",
15525 #ifndef SQLITE_SHELL_WASM_MODE
15526 ".quit Exit this program",
15527 ".read FILE Read input from FILE or command output",
15528 " If FILE begins with \"|\", it is a command that generates the input.",
15529 #endif
15530 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15531 ".recover Recover as much data as possible from corrupt db.",
15532 " --freelist-corrupt Assume the freelist is corrupt",
15533 " --recovery-db NAME Store recovery metadata in database file NAME",
15534 " --lost-and-found TABLE Alternative name for the lost-and-found table",
15535 " --no-rowids Do not attempt to recover rowid values",
15536 " that are not also INTEGER PRIMARY KEYs",
15537 #endif
15538 #ifndef SQLITE_SHELL_WASM_MODE
15539 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
15540 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
15541 #endif
15542 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
15543 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
15544 " Options:",
15545 " --indent Try to pretty-print the schema",
15546 " --nosys Omit objects whose names start with \"sqlite_\"",
@@ -15519,24 +15570,26 @@
15570 " --sha3-224 Use the sha3-224 algorithm",
15571 " --sha3-256 Use the sha3-256 algorithm (default)",
15572 " --sha3-384 Use the sha3-384 algorithm",
15573 " --sha3-512 Use the sha3-512 algorithm",
15574 " Any other argument is a LIKE pattern for tables to hash",
15575 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
15576 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
15577 #endif
15578 ".show Show the current values for various settings",
15579 ".stats ?ARG? Show stats or turn stats on or off",
15580 " off Turn off automatic stat display",
15581 " on Turn on automatic stat display",
15582 " stmt Show statement stats",
15583 " vmstep Show the virtual machine step count only",
15584 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
15585 ".system CMD ARGS... Run CMD ARGS... in a system shell",
15586 #endif
15587 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
15588 #ifndef SQLITE_SHELL_WASM_MODE
15589 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
15590 #endif
15591 ".testctrl CMD ... Run various sqlite3_test_control() operations",
15592 " Run \".testctrl\" with no arguments for details",
15593 ".timeout MS Try opening locked tables for MS milliseconds",
15594 ".timer on|off Turn SQL timer on or off",
15595 #ifndef SQLITE_OMIT_TRACE
@@ -16077,18 +16130,20 @@
16130 exit(1);
16131 }
16132 #ifndef SQLITE_OMIT_LOAD_EXTENSION
16133 sqlite3_enable_load_extension(p->db, 1);
16134 #endif
 
16135 sqlite3_shathree_init(p->db, 0, 0);
 
16136 sqlite3_uint_init(p->db, 0, 0);
16137 sqlite3_decimal_init(p->db, 0, 0);
16138 sqlite3_regexp_init(p->db, 0, 0);
16139 sqlite3_ieee_init(p->db, 0, 0);
16140 sqlite3_series_init(p->db, 0, 0);
16141 #ifndef SQLITE_SHELL_WASM_MODE
16142 sqlite3_fileio_init(p->db, 0, 0);
16143 sqlite3_completion_init(p->db, 0, 0);
16144 #endif
16145 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
16146 sqlite3_dbdata_init(p->db, 0, 0);
16147 #endif
16148 #ifdef SQLITE_HAVE_ZLIB
16149 if( !p->bSafeModePersist ){
@@ -19207,18 +19262,20 @@
19262 sqlite3_set_authorizer(p->db, 0, 0);
19263 }
19264 }else
19265 #endif
19266
19267 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
19268 && !defined(SQLITE_SHELL_WASM_MODE)
19269 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
19270 open_db(p, 0);
19271 failIfSafeMode(p, "cannot run .archive in safe mode");
19272 rc = arDotCommand(p, 0, azArg, nArg);
19273 }else
19274 #endif
19275
19276 #ifndef SQLITE_SHELL_WASM_MODE
19277 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
19278 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
19279 ){
19280 const char *zDestFile = 0;
19281 const char *zDb = 0;
@@ -19283,10 +19340,11 @@
19340 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
19341 rc = 1;
19342 }
19343 close_db(pDest);
19344 }else
19345 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19346
19347 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
19348 if( nArg==2 ){
19349 bail_on_error = booleanValue(azArg[1]);
19350 }else{
@@ -19313,10 +19371,11 @@
19371 */
19372 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
19373 test_breakpoint();
19374 }else
19375
19376 #ifndef SQLITE_SHELL_WASM_MODE
19377 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
19378 failIfSafeMode(p, "cannot run .cd in safe mode");
19379 if( nArg==2 ){
19380 #if defined(_WIN32) || defined(WIN32)
19381 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
@@ -19332,10 +19391,11 @@
19391 }else{
19392 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
19393 rc = 1;
19394 }
19395 }else
19396 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19397
19398 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
19399 if( nArg==2 ){
19400 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
19401 }else{
@@ -19342,10 +19402,11 @@
19402 raw_printf(stderr, "Usage: .changes on|off\n");
19403 rc = 1;
19404 }
19405 }else
19406
19407 #ifndef SQLITE_SHELL_WASM_MODE
19408 /* Cancel output redirection, if it is currently set (by .testcase)
19409 ** Then read the content of the testcase-out.txt file and compare against
19410 ** azArg[1]. If there are differences, report an error and exit.
19411 */
19412 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
@@ -19366,20 +19427,23 @@
19427 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
19428 p->nCheck++;
19429 }
19430 sqlite3_free(zRes);
19431 }else
19432 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19433
19434 #ifndef SQLITE_SHELL_WASM_MODE
19435 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
19436 failIfSafeMode(p, "cannot run .clone in safe mode");
19437 if( nArg==2 ){
19438 tryToClone(p, azArg[1]);
19439 }else{
19440 raw_printf(stderr, "Usage: .clone FILENAME\n");
19441 rc = 1;
19442 }
19443 }else
19444 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
19445
19446 if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
19447 if( nArg==1 ){
19448 /* List available connections */
19449 int i;
@@ -19664,14 +19728,16 @@
19728 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
19729 rc = 1;
19730 }
19731 }else
19732
19733 #ifndef SQLITE_SHELL_WASM_MODE
19734 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
19735 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
19736 rc = 2;
19737 }else
19738 #endif
19739
19740 /* The ".explain" command is automatic now. It is largely pointless. It
19741 ** retained purely for backwards compatibility */
19742 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
19743 int val = 1;
@@ -19922,10 +19988,11 @@
19988 }else{
19989 showHelp(p->out, 0);
19990 }
19991 }else
19992
19993 #ifndef SQLITE_SHELL_WASM_MODE
19994 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
19995 char *zTable = 0; /* Insert data into this table */
19996 char *zSchema = 0; /* within this schema (may default to "main") */
19997 char *zFile = 0; /* Name of file to extra content from */
19998 sqlite3_stmt *pStmt = NULL; /* A statement */
@@ -20212,10 +20279,11 @@
20279 utf8_printf(p->out,
20280 "Added %d rows with %d errors using %d lines of input\n",
20281 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
20282 }
20283 }else
20284 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
20285
20286 #ifndef SQLITE_UNTESTABLE
20287 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
20288 char *zSql;
20289 char *zCollist = 0;
@@ -20401,11 +20469,11 @@
20469 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
20470 open_db(p, 0);
20471 lintDotCommand(p, azArg, nArg);
20472 }else
20473
20474 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_WASM_MODE)
20475 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
20476 const char *zFile, *zProc;
20477 char *zErrMsg = 0;
20478 failIfSafeMode(p, "cannot run .load in safe mode");
20479 if( nArg<2 ){
@@ -20423,10 +20491,11 @@
20491 rc = 1;
20492 }
20493 }else
20494 #endif
20495
20496 #ifndef SQLITE_SHELL_WASM_MODE
20497 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
20498 failIfSafeMode(p, "cannot run .log in safe mode");
20499 if( nArg!=2 ){
20500 raw_printf(stderr, "Usage: .log FILENAME\n");
20501 rc = 1;
@@ -20434,10 +20503,11 @@
20503 const char *zFile = azArg[1];
20504 output_file_close(p->pLog);
20505 p->pLog = output_file_open(zFile, 0);
20506 }
20507 }else
20508 #endif
20509
20510 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
20511 const char *zMode = 0;
20512 const char *zTabname = 0;
20513 int i, n2;
@@ -20558,10 +20628,11 @@
20628 rc = 1;
20629 }
20630 p->cMode = p->mode;
20631 }else
20632
20633 #ifndef SQLITE_SHELL_WASM_MODE
20634 if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
20635 if( nArg!=2 ){
20636 raw_printf(stderr, "Usage: .nonce NONCE\n");
20637 rc = 1;
20638 }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
@@ -20572,10 +20643,11 @@
20643 p->bSafeMode = 0;
20644 return 0; /* Return immediately to bypass the safe mode reset
20645 ** at the end of this procedure */
20646 }
20647 }else
20648 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
20649
20650 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
20651 if( nArg==2 ){
20652 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
20653 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
@@ -20593,10 +20665,11 @@
20665 int openMode = SHELL_OPEN_UNSPEC;
20666
20667 /* Check for command-line arguments */
20668 for(iName=1; iName<nArg; iName++){
20669 const char *z = azArg[iName];
20670 #ifndef SQLITE_SHELL_WASM_MODE
20671 if( optionMatch(z,"new") ){
20672 newFlag = 1;
20673 #ifdef SQLITE_HAVE_ZLIB
20674 }else if( optionMatch(z, "zip") ){
20675 openMode = SHELL_OPEN_ZIPFILE;
@@ -20613,11 +20686,13 @@
20686 }else if( optionMatch(z, "hexdb") ){
20687 openMode = SHELL_OPEN_HEXDB;
20688 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
20689 p->szMax = integerValue(azArg[++iName]);
20690 #endif /* SQLITE_OMIT_DESERIALIZE */
20691 }else
20692 #endif /* !SQLITE_SHELL_WASM_MODE */
20693 if( z[0]=='-' ){
20694 utf8_printf(stderr, "unknown option: %s\n", z);
20695 rc = 1;
20696 goto meta_command_exit;
20697 }else if( zFN ){
20698 utf8_printf(stderr, "extra argument: \"%s\"\n", z);
@@ -20640,17 +20715,21 @@
20715 p->szMax = 0;
20716
20717 /* If a filename is specified, try to open it first */
20718 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
20719 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
20720 #ifndef SQLITE_SHELL_WASM_MODE
20721 if( p->bSafeMode
20722 && p->openMode!=SHELL_OPEN_HEXDB
20723 && zFN
20724 && strcmp(zFN,":memory:")!=0
20725 ){
20726 failIfSafeMode(p, "cannot open disk-based database files in safe mode");
20727 }
20728 #else
20729 /* WASM mode has its own sandboxed pseudo-filesystem. */
20730 #endif
20731 if( zFN ){
20732 zNewFilename = sqlite3_mprintf("%s", zFN);
20733 shell_check_oom(zNewFilename);
20734 }else{
20735 zNewFilename = 0;
@@ -20669,10 +20748,11 @@
20748 p->pAuxDb->zDbFilename = 0;
20749 open_db(p, 0);
20750 }
20751 }else
20752
20753 #ifndef SQLITE_SHELL_WASM_MODE
20754 if( (c=='o'
20755 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
20756 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
20757 ){
20758 char *zFile = 0;
@@ -20784,10 +20864,11 @@
20864 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
20865 }
20866 }
20867 sqlite3_free(zFile);
20868 }else
20869 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
20870
20871 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
20872 open_db(p,0);
20873 if( nArg<=1 ) goto parameter_syntax_error;
20874
@@ -20953,14 +21034,17 @@
21034 if( nArg >= 3) {
21035 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
21036 }
21037 }else
21038
21039 #ifndef SQLITE_SHELL_WASM_MODE
21040 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
21041 rc = 2;
21042 }else
21043 #endif
21044
21045 #ifndef SQLITE_SHELL_WASM_MODE
21046 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
21047 FILE *inSaved = p->in;
21048 int savedLineno = p->lineno;
21049 failIfSafeMode(p, "cannot run .read in safe mode");
21050 if( nArg!=2 ){
@@ -20991,11 +21075,13 @@
21075 fclose(p->in);
21076 }
21077 p->in = inSaved;
21078 p->lineno = savedLineno;
21079 }else
21080 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
21081
21082 #ifndef SQLITE_SHELL_WASM_MODE
21083 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
21084 const char *zSrcFile;
21085 const char *zDb;
21086 sqlite3 *pSrc;
21087 sqlite3_backup *pBackup;
@@ -21043,10 +21129,11 @@
21129 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
21130 rc = 1;
21131 }
21132 close_db(pSrc);
21133 }else
21134 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
21135
21136 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
21137 if( nArg==2 ){
21138 p->scanstatsOn = (u8)booleanValue(azArg[1]);
21139 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
@@ -21668,11 +21755,11 @@
21755 shell_exec(p, zSql, 0);
21756 }
21757 sqlite3_free(zSql);
21758 }else
21759
21760 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE)
21761 if( c=='s'
21762 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
21763 ){
21764 char *zCmd;
21765 int i, x;
@@ -21689,11 +21776,11 @@
21776 }
21777 x = zCmd!=0 ? system(zCmd) : 1;
21778 sqlite3_free(zCmd);
21779 if( x ) raw_printf(stderr, "System command returns %d\n", x);
21780 }else
21781 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_WASM_MODE) */
21782
21783 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
21784 static const char *azBool[] = { "off", "on", "trigger", "full"};
21785 const char *zOut;
21786 int i;
@@ -21869,10 +21956,11 @@
21956
21957 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
21958 sqlite3_free(azResult);
21959 }else
21960
21961 #ifndef SQLITE_SHELL_WASM_MODE
21962 /* Begin redirecting output to the file "testcase-out.txt" */
21963 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
21964 output_reset(p);
21965 p->out = output_file_open("testcase-out.txt", 0);
21966 if( p->out==0 ){
@@ -21882,10 +21970,11 @@
21970 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
21971 }else{
21972 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
21973 }
21974 }else
21975 #endif /* !defined(SQLITE_SHELL_WASM_MODE) */
21976
21977 #ifndef SQLITE_UNTESTABLE
21978 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
21979 static const struct {
21980 const char *zCtrlName; /* Name of a test-control option */
@@ -22553,10 +22642,43 @@
22642
22643 static void echo_group_input(ShellState *p, const char *zDo){
22644 if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
22645 }
22646
22647 #ifdef SQLITE_SHELL_WASM_MODE
22648 /*
22649 ** Alternate one_input_line() impl for wasm mode. This is not in the primary impl
22650 ** because we need the global shellState and cannot access it from that function
22651 ** without moving lots of code around (creating a larger/messier diff).
22652 */
22653 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
22654 /* Parse the next line from shellState.wasm.zInput. */
22655 const char *zBegin = shellState.wasm.zPos;
22656 const char *z = zBegin;
22657 char *zLine = 0;
22658 int nZ = 0;
22659
22660 UNUSED_PARAMETER(in);
22661 UNUSED_PARAMETER(isContinuation);
22662 if(!z || !*z){
22663 return 0;
22664 }
22665 while(*z && isspace(*z)) ++z;
22666 zBegin = z;
22667 for(; *z && '\n'!=*z; ++nZ, ++z){}
22668 if(nZ>0 && '\r'==zBegin[nZ-1]){
22669 --nZ;
22670 }
22671 shellState.wasm.zPos = z;
22672 zLine = realloc(zPrior, nZ+1);
22673 shell_check_oom(zLine);
22674 memcpy(zLine, zBegin, (size_t)nZ);
22675 zLine[nZ] = 0;
22676 return zLine;
22677 }
22678 #endif /* SQLITE_SHELL_WASM_MODE */
22679
22680 /*
22681 ** Read input from *in and process it. If *in==0 then input
22682 ** is interactive - the user is typing it it. Otherwise, input
22683 ** is coming from a file or device. A prompt is issued and history
22684 ** is saved only if input is interactive. An interrupt signal will
@@ -22933,10 +23055,14 @@
23055 # define SQLITE_SHELL_IS_UTF8 (0)
23056 # else
23057 # define SQLITE_SHELL_IS_UTF8 (1)
23058 # endif
23059 #endif
23060
23061 #ifdef SQLITE_SHELL_WASM_MODE
23062 # define main fiddle_main
23063 #endif
23064
23065 #if SQLITE_SHELL_IS_UTF8
23066 int SQLITE_CDECL main(int argc, char **argv){
23067 #else
23068 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
@@ -22944,11 +23070,15 @@
23070 #endif
23071 #ifdef SQLITE_DEBUG
23072 sqlite3_uint64 mem_main_enter = sqlite3_memory_used();
23073 #endif
23074 char *zErrMsg = 0;
23075 #ifdef SQLITE_SHELL_WASM_MODE
23076 # define data shellState
23077 #else
23078 ShellState data;
23079 #endif
23080 const char *zInitFile = 0;
23081 int i;
23082 int rc = 0;
23083 int warnInmemoryDb = 0;
23084 int readStdin = 1;
@@ -22960,12 +23090,17 @@
23090 int argcToFree = 0;
23091 #endif
23092
23093 setBinaryMode(stdin, 0);
23094 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
23095 #ifdef SQLITE_SHELL_WASM_MODE
23096 stdin_is_interactive = 0;
23097 stdout_is_console = 1;
23098 #else
23099 stdin_is_interactive = isatty(0);
23100 stdout_is_console = isatty(1);
23101 #endif
23102
23103 #if !defined(_WIN32_WCE)
23104 if( getenv("SQLITE_DEBUG_BREAK") ){
23105 if( isatty(0) && isatty(2) ){
23106 fprintf(stderr,
@@ -23217,11 +23352,13 @@
23352 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
23353 return 1;
23354 #endif
23355 }
23356 data.out = stdout;
23357 #ifndef SQLITE_SHELL_WASM_MODE
23358 sqlite3_appendvfs_init(0,0,0);
23359 #endif
23360
23361 /* Go ahead and open the database file if it already exists. If the
23362 ** file does not exist, delay opening it. This prevents empty database
23363 ** files from being created if a user mistypes the database name argument
23364 ** to the sqlite command-line tool.
@@ -23483,10 +23620,13 @@
23620 }else{
23621 data.in = stdin;
23622 rc = process_input(&data);
23623 }
23624 }
23625 #ifndef SQLITE_SHELL_WASM_MODE
23626 /* In WASM mode we have to leave the db state in place so that
23627 ** client code can "push" SQL into it after this call returns. */
23628 free(azCmd);
23629 set_table_name(&data, 0);
23630 if( data.db ){
23631 session_close_all(&data, -1);
23632 close_db(data.db);
@@ -23515,7 +23655,110 @@
23655 if( sqlite3_memory_used()>mem_main_enter ){
23656 utf8_printf(stderr, "Memory leaked: %u bytes\n",
23657 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
23658 }
23659 #endif
23660 #endif /* !SQLITE_SHELL_WASM_MODE */
23661 return rc;
23662 }
23663
23664
23665 #ifdef SQLITE_SHELL_WASM_MODE
23666 /* Only for emcc experimentation purposes. */
23667 int fiddle_experiment(int a,int b){
23668 return a + b;
23669 }
23670
23671 /* Only for emcc experimentation purposes.
23672
23673 Define this function in JS using:
23674
23675 emcc ... --js-library somefile.js
23676
23677 containing:
23678
23679 mergeInto(LibraryManager.library, {
23680 my_foo: function(){
23681 console.debug("my_foo()",arguments);
23682 }
23683 });
23684 */
23685 /*extern void my_foo(sqlite3 *);*/
23686 /* Only for emcc experimentation purposes. */
23687 sqlite3 * fiddle_the_db(){
23688 printf("fiddle_the_db(%p)\n", (const void*)globalDb);
23689 /*my_foo(globalDb);*/
23690 return globalDb;
23691 }
23692 /* Only for emcc experimentation purposes. */
23693 sqlite3 * fiddle_db_arg(sqlite3 *arg){
23694 printf("fiddle_db_arg(%p)\n", (const void*)arg);
23695 return arg;
23696 }
23697
23698 /*
23699 ** Intended to be called via a SharedWorker() while a separate
23700 ** SharedWorker() (which manages the wasm module) is performing work
23701 ** which should be interrupted. Unfortunately, SharedWorker is not
23702 ** portable enough to make real use of.
23703 */
23704 void fiddle_interrupt(void){
23705 if(globalDb) sqlite3_interrupt(globalDb);
23706 }
23707
23708 /*
23709 ** Returns the filename of the given db name, assuming "main" if
23710 ** zDbName is NULL. Returns NULL if globalDb is not opened.
23711 */
23712 const char * fiddle_db_filename(const char * zDbName){
23713 return globalDb
23714 ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
23715 : NULL;
23716 }
23717
23718 /*
23719 ** Trivial exportable function for emscripten. Needs to be exported using:
23720 **
23721 ** emcc ..flags... -sEXPORTED_FUNCTIONS=_fiddle_exec -sEXPORTED_RUNTIME_METHODS=ccall,cwrap
23722 **
23723 ** (Note the underscore before the function name.) It processes zSql
23724 ** as if it were input to the sqlite3 shell and redirects all output
23725 ** to the wasm binding.
23726 */
23727 void fiddle_exec(const char * zSql){
23728 static int once = 0;
23729 int rc = 0;
23730 if(!once){
23731 /* Simulate an argv array for main() */
23732 static char * argv[] = {"fiddle",
23733 "-bail",
23734 "-safe"};
23735 rc = fiddle_main((int)(sizeof(argv)/sizeof(argv[0])), argv);
23736 once = rc ? -1 : 1;
23737 memset(&shellState.wasm, 0, sizeof(shellState.wasm));
23738 printf(
23739 "SQLite version %s %.19s\n" /*extra-version-info*/,
23740 sqlite3_libversion(), sqlite3_sourceid()
23741 );
23742 puts("WASM shell");
23743 puts("Enter \".help\" for usage hints.");
23744 if(once>0){
23745 shellState.pAuxDb->zDbFilename = "/fiddle.sqlite3";
23746 open_db(&shellState, 0);
23747 }
23748 if(shellState.db){
23749 printf("Connected to %s.\n", fiddle_db_filename(NULL));
23750 }else{
23751 fprintf(stderr,"ERROR initializing db!\n");
23752 return;
23753 }
23754 }
23755 if(once<0){
23756 puts("DB init failed. Not executing SQL.");
23757 }else if(zSql && *zSql){
23758 shellState.wasm.zInput = zSql;
23759 shellState.wasm.zPos = zSql;
23760 process_input(&shellState);
23761 memset(&shellState.wasm, 0, sizeof(shellState.wasm));
23762 }
23763 }
23764 #endif /* SQLITE_SHELL_WASM_MODE */
23765
+135 -33
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -452,11 +452,11 @@
452452
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453453
** [sqlite_version()] and [sqlite_source_id()].
454454
*/
455455
#define SQLITE_VERSION "3.39.0"
456456
#define SQLITE_VERSION_NUMBER 3039000
457
-#define SQLITE_SOURCE_ID "2022-05-14 19:05:13 2277f9ba7087dd993ac0f4007c523aa9cf74dba187f53af03d8c164886726fee"
457
+#define SQLITE_SOURCE_ID "2022-05-25 13:10:29 c48a735bd4a1dbd541aed5937c25fc0c606c4263d8ee94cae30a06b1a7b26d9a"
458458
459459
/*
460460
** CAPI3REF: Run-Time Library Version Numbers
461461
** KEYWORDS: sqlite3_version sqlite3_sourceid
462462
**
@@ -6580,10 +6580,30 @@
65806580
** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
65816581
** create the statement in the first place.
65826582
*/
65836583
SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
65846584
6585
+/*
6586
+** CAPI3REF: Return The Schema Name For A Database Connection
6587
+** METHOD: sqlite3
6588
+**
6589
+** ^The sqlite3_db_name(D,N) interface returns a pointer to the schema name
6590
+** for the N-th database on database connection D, or a NULL pointer of N is
6591
+** out of range.
6592
+**
6593
+** Space to hold the string that is returned by sqlite3_db_name() is managed
6594
+** by SQLite itself. The string might be deallocated by any operation that
6595
+** changes the schema, including [ATTACH] or [DETACH] or calls to
6596
+** [sqlite3_serialize()] or [sqlite3_deserialize()], even operations that
6597
+** occur on a different thread. Applications that need to
6598
+** remember the string long-term should make their own copy. Applications that
6599
+** are accessing the same database connection simultaneously on multiple
6600
+** threads should mutex-protect calls to this API and should make their own
6601
+** private copy of the result prior to releasing the mutex.
6602
+*/
6603
+SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N);
6604
+
65856605
/*
65866606
** CAPI3REF: Return The Filename For A Database Connection
65876607
** METHOD: sqlite3
65886608
**
65896609
** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
@@ -18373,10 +18393,11 @@
1837318393
u8 jointype; /* Type of join between this table and the previous */
1837418394
unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
1837518395
unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
1837618396
unsigned isTabFunc :1; /* True if table-valued-function syntax */
1837718397
unsigned isCorrelated :1; /* True if sub-query is correlated */
18398
+ unsigned isMaterialized:1; /* This is a materialized view */
1837818399
unsigned viaCoroutine :1; /* Implemented as a co-routine */
1837918400
unsigned isRecursive :1; /* True for recursive reference in WITH */
1838018401
unsigned fromDDL :1; /* Comes from sqlite_schema */
1838118402
unsigned isCte :1; /* This is a CTE */
1838218403
unsigned notCte :1; /* This item may not match a CTE */
@@ -41909,11 +41930,11 @@
4190941930
if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){
4191041931
pPath->rc = SQLITE_CANTOPEN_BKPT;
4191141932
return;
4191241933
}
4191341934
got = osReadlink(zIn, zLnk, sizeof(zLnk)-2);
41914
- if( got<=0 || got>=sizeof(zLnk)-2 ){
41935
+ if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
4191541936
pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
4191641937
return;
4191741938
}
4191841939
zLnk[got] = 0;
4191941940
if( zLnk[0]=='/' ){
@@ -41959,10 +41980,11 @@
4195941980
const char *zPath, /* Possibly relative input path */
4196041981
int nOut, /* Size of output buffer in bytes */
4196141982
char *zOut /* Output buffer */
4196241983
){
4196341984
DbPath path;
41985
+ UNUSED_PARAMETER(pVfs);
4196441986
path.rc = 0;
4196541987
path.nUsed = 0;
4196641988
path.nSymlink = 0;
4196741989
path.nOut = nOut;
4196841990
path.zOut = zOut;
@@ -124330,15 +124352,15 @@
124330124352
}else{
124331124353
ans = log(x);
124332124354
switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){
124333124355
case 1:
124334124356
/* Convert from natural logarithm to log base 10 */
124335
- ans *= 1.0/M_LN10;
124357
+ ans /= M_LN10;
124336124358
break;
124337124359
case 2:
124338124360
/* Convert from natural logarithm to log base 2 */
124339
- ans *= 1.0/M_LN2;
124361
+ ans /= M_LN2;
124340124362
break;
124341124363
default:
124342124364
break;
124343124365
}
124344124366
}
@@ -129758,10 +129780,11 @@
129758129780
/* Version 3.39.0 and later */
129759129781
int (*deserialize)(sqlite3*,const char*,unsigned char*,
129760129782
sqlite3_int64,sqlite3_int64,unsigned);
129761129783
unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
129762129784
unsigned int);
129785
+ const char *(*db_name)(sqlite3*,int);
129763129786
};
129764129787
129765129788
/*
129766129789
** This is the function signature used for all extension entry points. It
129767129790
** is also defined in the file "loadext.c".
@@ -130076,14 +130099,16 @@
130076130099
#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
130077130100
#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
130078130101
#define sqlite3_vtab_in sqlite3_api->vtab_in
130079130102
#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
130080130103
#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
130104
+/* Version 3.39.0 and later */
130081130105
#ifndef SQLITE_OMIT_DESERIALIZE
130082130106
#define sqlite3_deserialize sqlite3_api->deserialize
130083130107
#define sqlite3_serialize sqlite3_api->serialize
130084130108
#endif
130109
+#define sqlite3_db_name sqlite3_api->db_name
130085130110
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
130086130111
130087130112
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
130088130113
/* This case when the file really is being compiled as a loadable
130089130114
** extension */
@@ -130587,15 +130612,16 @@
130587130612
0,
130588130613
#endif
130589130614
/* Version 3.39.0 and later */
130590130615
#ifndef SQLITE_OMIT_DESERIALIZE
130591130616
sqlite3_deserialize,
130592
- sqlite3_serialize
130617
+ sqlite3_serialize,
130593130618
#else
130619
+ 0,
130594130620
0,
130595
- 0
130596130621
#endif
130622
+ sqlite3_db_name
130597130623
};
130598130624
130599130625
/* True if x is the directory separator character
130600130626
*/
130601130627
#if SQLITE_OS_WIN
@@ -141946,10 +141972,33 @@
141946141972
}
141947141973
#endif
141948141974
return 1;
141949141975
}
141950141976
#endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
141977
+
141978
+/*
141979
+** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
141980
+** as pSrcItem but has the same alias as p0, then return true.
141981
+** Otherwise return false.
141982
+*/
141983
+static int sameSrcAlias(SrcItem *p0, SrcList *pSrc){
141984
+ int i;
141985
+ for(i=0; i<pSrc->nSrc; i++){
141986
+ SrcItem *p1 = &pSrc->a[i];
141987
+ if( p1==p0 ) continue;
141988
+ if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
141989
+ return 1;
141990
+ }
141991
+ if( p1->pSelect
141992
+ && (p1->pSelect->selFlags & SF_NestedFrom)!=0
141993
+ && sameSrcAlias(p0, p1->pSelect->pSrc)
141994
+ ){
141995
+ return 1;
141996
+ }
141997
+ }
141998
+ return 0;
141999
+}
141951142000
141952142001
/*
141953142002
** Generate code for the SELECT statement given in the p argument.
141954142003
**
141955142004
** The results are returned according to the SelectDest structure.
@@ -142051,19 +142100,16 @@
142051142100
** systems handle this case differently, and not all the same way,
142052142101
** which is just confusing. To avoid this, we follow PG's lead and
142053142102
** disallow it altogether. */
142054142103
if( p->selFlags & SF_UFSrcCheck ){
142055142104
SrcItem *p0 = &p->pSrc->a[0];
142056
- for(i=1; i<p->pSrc->nSrc; i++){
142057
- SrcItem *p1 = &p->pSrc->a[i];
142058
- if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
142059
- sqlite3ErrorMsg(pParse,
142060
- "target object/alias may not appear in FROM clause: %s",
142061
- p0->zAlias ? p0->zAlias : p0->pTab->zName
142062
- );
142063
- goto select_end;
142064
- }
142105
+ if( sameSrcAlias(p0, p->pSrc) ){
142106
+ sqlite3ErrorMsg(pParse,
142107
+ "target object/alias may not appear in FROM clause: %s",
142108
+ p0->zAlias ? p0->zAlias : p0->pTab->zName
142109
+ );
142110
+ goto select_end;
142065142111
}
142066142112
142067142113
/* Clear the SF_UFSrcCheck flag. The check has already been performed,
142068142114
** and leaving this flag set can cause errors if a compound sub-query
142069142115
** in p->pSrc is flattened into this query and this function called
@@ -142334,11 +142380,11 @@
142334142380
zSavedAuthContext = pParse->zAuthContext;
142335142381
pParse->zAuthContext = pItem->zName;
142336142382
142337142383
/* Generate code to implement the subquery
142338142384
**
142339
- ** The subquery is implemented as a co-routine all of the following are
142385
+ ** The subquery is implemented as a co-routine all if the following are
142340142386
** true:
142341142387
**
142342142388
** (1) the subquery is guaranteed to be the outer loop (so that
142343142389
** it does not need to be computed more than once), and
142344142390
** (2) the subquery is not a CTE that should be materialized
@@ -142392,15 +142438,15 @@
142392142438
/* Materialize the view. If the view is not correlated, generate a
142393142439
** subroutine to do the materialization so that subsequent uses of
142394142440
** the same view can reuse the materialization. */
142395142441
int topAddr;
142396142442
int onceAddr = 0;
142397
- int retAddr;
142398142443
142399142444
pItem->regReturn = ++pParse->nMem;
142400
- topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
142445
+ topAddr = sqlite3VdbeAddOp0(v, OP_Goto);
142401142446
pItem->addrFillSub = topAddr+1;
142447
+ pItem->fg.isMaterialized = 1;
142402142448
if( pItem->fg.isCorrelated==0 ){
142403142449
/* If the subquery is not correlated and if we are not inside of
142404142450
** a trigger, then we only need to compute the value of the subquery
142405142451
** once. */
142406142452
onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
@@ -142411,13 +142457,13 @@
142411142457
sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
142412142458
ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem));
142413142459
sqlite3Select(pParse, pSub, &dest);
142414142460
pItem->pTab->nRowLogEst = pSub->nSelectRow;
142415142461
if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
142416
- retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
142462
+ sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
142417142463
VdbeComment((v, "end %!S", pItem));
142418
- sqlite3VdbeChangeP1(v, topAddr, retAddr);
142464
+ sqlite3VdbeJumpHere(v, topAddr);
142419142465
sqlite3ClearTempRegCache(pParse);
142420142466
if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
142421142467
CteUse *pCteUse = pItem->u2.pCteUse;
142422142468
pCteUse->addrM9e = pItem->addrFillSub;
142423142469
pCteUse->regRtn = pItem->regReturn;
@@ -152973,18 +153019,26 @@
152973153019
sqlite3TreeViewExpr(0,pExpr,0);
152974153020
abort();
152975153021
}
152976153022
#endif
152977153023
152978
- if( ExprHasProperty(pExpr, EP_OuterON) ){
153024
+ if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON) ){
152979153025
Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
152980
- prereqAll |= x;
152981
- extraRight = x-1; /* ON clause terms may not be used with an index
152982
- ** on left table of a LEFT JOIN. Ticket #3015 */
152983
- if( (prereqAll>>1)>=x ){
152984
- sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
152985
- return;
153026
+ if( ExprHasProperty(pExpr, EP_OuterON) ){
153027
+ prereqAll |= x;
153028
+ extraRight = x-1; /* ON clause terms may not be used with an index
153029
+ ** on left table of a LEFT JOIN. Ticket #3015 */
153030
+ if( (prereqAll>>1)>=x ){
153031
+ sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
153032
+ return;
153033
+ }
153034
+ }else if( (prereqAll>>1)>=x ){
153035
+ /* The ON clause of an INNER JOIN references a table to its right.
153036
+ ** Most other SQL database engines raise an error. But all versions
153037
+ ** of SQLite going back to 3.0.0 have just put the ON clause constraint
153038
+ ** into the WHERE clause and carried on. */
153039
+ ExprClearProperty(pExpr, EP_InnerON);
152986153040
}
152987153041
}
152988153042
pTerm->prereqAll = prereqAll;
152989153043
pTerm->leftCursor = -1;
152990153044
pTerm->iParent = -1;
@@ -157368,10 +157422,11 @@
157368157422
pIdxInfo->orderByConsumed = 0;
157369157423
pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
157370157424
*pbIn = 1; assert( (mExclude & WO_IN)==0 );
157371157425
}
157372157426
157427
+ assert( pbRetryLimit || !isLimitTerm(pTerm) );
157373157428
if( isLimitTerm(pTerm) && *pbIn ){
157374157429
/* If there is an IN(...) term handled as an == (separate call to
157375157430
** xFilter for each value on the RHS of the IN) and a LIMIT or
157376157431
** OFFSET term handled as well, the plan is unusable. Set output
157377157432
** variable *pbRetryLimit to true to tell the caller to retry with
@@ -157841,11 +157896,13 @@
157841157896
SrcList *pTabList = pWInfo->pTabList;
157842157897
SrcItem *pItem;
157843157898
SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
157844157899
sqlite3 *db = pWInfo->pParse->db;
157845157900
int rc = SQLITE_OK;
157901
+ int bFirstPastRJ = 0;
157846157902
WhereLoop *pNew;
157903
+
157847157904
157848157905
/* Loop over the tables in the join, from left to right */
157849157906
pNew = pBuilder->pNew;
157850157907
whereLoopInit(pNew);
157851157908
pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
@@ -157852,14 +157909,17 @@
157852157909
for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
157853157910
Bitmask mUnusable = 0;
157854157911
pNew->iTab = iTab;
157855157912
pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
157856157913
pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
157857
- if( (pItem->fg.jointype & (JT_OUTER|JT_CROSS))!=0 ){
157858
- /* This condition is true when pItem is the FROM clause term on the
157859
- ** right-hand-side of a OUTER or CROSS JOIN. */
157914
+ if( bFirstPastRJ || (pItem->fg.jointype & (JT_OUTER|JT_CROSS))!=0 ){
157915
+ /* Add prerequisites to prevent reordering of FROM clause terms
157916
+ ** across CROSS joins and outer joins. The bFirstPastRJ boolean
157917
+ ** prevents the right operand of a RIGHT JOIN from being swapped with
157918
+ ** other elements even further to the right. */
157860157919
mPrereq |= mPrior;
157920
+ bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
157861157921
}
157862157922
#ifndef SQLITE_OMIT_VIRTUALTABLE
157863157923
if( IsVirtual(pItem->pTab) ){
157864157924
SrcItem *p;
157865157925
for(p=&pItem[1]; p<pEnd; p++){
@@ -159551,10 +159611,11 @@
159551159611
op = OP_ReopenIdx;
159552159612
}else{
159553159613
iIndexCur = pParse->nTab++;
159554159614
}
159555159615
pLevel->iIdxCur = iIndexCur;
159616
+ assert( pIx!=0 );
159556159617
assert( pIx->pSchema==pTab->pSchema );
159557159618
assert( iIndexCur>=0 );
159558159619
if( op ){
159559159620
sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
159560159621
sqlite3VdbeSetP4KeyInfo(pParse, pIx);
@@ -159626,13 +159687,24 @@
159626159687
** program.
159627159688
*/
159628159689
for(ii=0; ii<nTabList; ii++){
159629159690
int addrExplain;
159630159691
int wsFlags;
159692
+ SrcItem *pSrc;
159631159693
if( pParse->nErr ) goto whereBeginError;
159632159694
pLevel = &pWInfo->a[ii];
159633159695
wsFlags = pLevel->pWLoop->wsFlags;
159696
+ pSrc = &pTabList->a[pLevel->iFrom];
159697
+ if( pSrc->fg.isMaterialized ){
159698
+ if( pSrc->fg.isCorrelated ){
159699
+ sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
159700
+ }else{
159701
+ int iOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
159702
+ sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
159703
+ sqlite3VdbeJumpHere(v, iOnce);
159704
+ }
159705
+ }
159634159706
if( (wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))!=0 ){
159635159707
if( (wsFlags & WHERE_AUTO_INDEX)!=0 ){
159636159708
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
159637159709
constructAutomaticIndex(pParse, &pWInfo->sWC,
159638159710
&pTabList->a[pLevel->iFrom], notReady, pLevel);
@@ -167347,11 +167419,22 @@
167347167419
break;
167348167420
case 157: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
167349167421
{
167350167422
sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-4].minor.yy0);
167351167423
sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy322,"set list");
167352
- yymsp[-5].minor.yy131 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy131, yymsp[-1].minor.yy131);
167424
+ if( yymsp[-1].minor.yy131 ){
167425
+ SrcList *pFromClause = yymsp[-1].minor.yy131;
167426
+ if( pFromClause->nSrc>1 ){
167427
+ Select *pSubquery;
167428
+ Token as;
167429
+ pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
167430
+ as.n = 0;
167431
+ as.z = 0;
167432
+ pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
167433
+ }
167434
+ yymsp[-5].minor.yy131 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy131, pFromClause);
167435
+ }
167353167436
sqlite3Update(pParse,yymsp[-5].minor.yy131,yymsp[-2].minor.yy322,yymsp[0].minor.yy528,yymsp[-6].minor.yy394,0,0,0);
167354167437
}
167355167438
break;
167356167439
case 158: /* setlist ::= setlist COMMA nm EQ expr */
167357167440
{
@@ -167652,11 +167735,12 @@
167652167735
**
167653167736
** simplify to constants 0 (false) and 1 (true), respectively,
167654167737
** regardless of the value of expr1.
167655167738
*/
167656167739
sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy528);
167657
- yymsp[-4].minor.yy528 = sqlite3Expr(pParse->db, TK_INTEGER, yymsp[-3].minor.yy394 ? "1" : "0");
167740
+ yymsp[-4].minor.yy528 = sqlite3Expr(pParse->db, TK_STRING, yymsp[-3].minor.yy394 ? "true" : "false");
167741
+ if( yymsp[-4].minor.yy528 ) sqlite3ExprIdToTrueFalse(yymsp[-4].minor.yy528);
167658167742
}else{
167659167743
Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
167660167744
if( yymsp[-1].minor.yy322->nExpr==1 && sqlite3ExprIsConstant(pRHS) && yymsp[-4].minor.yy528->op!=TK_VECTOR ){
167661167745
yymsp[-1].minor.yy322->a[0].pExpr = 0;
167662167746
sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
@@ -174920,10 +175004,28 @@
174920175004
*/
174921175005
SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
174922175006
int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
174923175007
return iDb<0 ? 0 : db->aDb[iDb].pBt;
174924175008
}
175009
+
175010
+/*
175011
+** Return the name of the N-th database schema. Return NULL if N is out
175012
+** of range.
175013
+*/
175014
+SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N){
175015
+#ifdef SQLITE_ENABLE_API_ARMOR
175016
+ if( !sqlite3SafetyCheckOk(db) ){
175017
+ (void)SQLITE_MISUSE_BKPT;
175018
+ return 0;
175019
+ }
175020
+#endif
175021
+ if( N<0 || N>=db->nDb ){
175022
+ return 0;
175023
+ }else{
175024
+ return db->aDb[N].zDbSName;
175025
+ }
175026
+}
174925175027
174926175028
/*
174927175029
** Return the filename of the database associated with a database
174928175030
** connection.
174929175031
*/
@@ -236303,11 +236405,11 @@
236303236405
int nArg, /* Number of args */
236304236406
sqlite3_value **apUnused /* Function arguments */
236305236407
){
236306236408
assert( nArg==0 );
236307236409
UNUSED_PARAM2(nArg, apUnused);
236308
- sqlite3_result_text(pCtx, "fts5: 2022-05-14 19:05:13 2277f9ba7087dd993ac0f4007c523aa9cf74dba187f53af03d8c164886726fee", -1, SQLITE_TRANSIENT);
236410
+ sqlite3_result_text(pCtx, "fts5: 2022-05-23 11:51:10 919ba2f0472e12c2d1e82364c1481e778b24ea406615b08992964a4eb80abee7", -1, SQLITE_TRANSIENT);
236309236411
}
236310236412
236311236413
/*
236312236414
** Return true if zName is the extension on one of the shadow tables used
236313236415
** by this module.
236314236416
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -452,11 +452,11 @@
452 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453 ** [sqlite_version()] and [sqlite_source_id()].
454 */
455 #define SQLITE_VERSION "3.39.0"
456 #define SQLITE_VERSION_NUMBER 3039000
457 #define SQLITE_SOURCE_ID "2022-05-14 19:05:13 2277f9ba7087dd993ac0f4007c523aa9cf74dba187f53af03d8c164886726fee"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -6580,10 +6580,30 @@
6580 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6581 ** create the statement in the first place.
6582 */
6583 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6584
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6585 /*
6586 ** CAPI3REF: Return The Filename For A Database Connection
6587 ** METHOD: sqlite3
6588 **
6589 ** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
@@ -18373,10 +18393,11 @@
18373 u8 jointype; /* Type of join between this table and the previous */
18374 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
18375 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
18376 unsigned isTabFunc :1; /* True if table-valued-function syntax */
18377 unsigned isCorrelated :1; /* True if sub-query is correlated */
 
18378 unsigned viaCoroutine :1; /* Implemented as a co-routine */
18379 unsigned isRecursive :1; /* True for recursive reference in WITH */
18380 unsigned fromDDL :1; /* Comes from sqlite_schema */
18381 unsigned isCte :1; /* This is a CTE */
18382 unsigned notCte :1; /* This item may not match a CTE */
@@ -41909,11 +41930,11 @@
41909 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){
41910 pPath->rc = SQLITE_CANTOPEN_BKPT;
41911 return;
41912 }
41913 got = osReadlink(zIn, zLnk, sizeof(zLnk)-2);
41914 if( got<=0 || got>=sizeof(zLnk)-2 ){
41915 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
41916 return;
41917 }
41918 zLnk[got] = 0;
41919 if( zLnk[0]=='/' ){
@@ -41959,10 +41980,11 @@
41959 const char *zPath, /* Possibly relative input path */
41960 int nOut, /* Size of output buffer in bytes */
41961 char *zOut /* Output buffer */
41962 ){
41963 DbPath path;
 
41964 path.rc = 0;
41965 path.nUsed = 0;
41966 path.nSymlink = 0;
41967 path.nOut = nOut;
41968 path.zOut = zOut;
@@ -124330,15 +124352,15 @@
124330 }else{
124331 ans = log(x);
124332 switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){
124333 case 1:
124334 /* Convert from natural logarithm to log base 10 */
124335 ans *= 1.0/M_LN10;
124336 break;
124337 case 2:
124338 /* Convert from natural logarithm to log base 2 */
124339 ans *= 1.0/M_LN2;
124340 break;
124341 default:
124342 break;
124343 }
124344 }
@@ -129758,10 +129780,11 @@
129758 /* Version 3.39.0 and later */
129759 int (*deserialize)(sqlite3*,const char*,unsigned char*,
129760 sqlite3_int64,sqlite3_int64,unsigned);
129761 unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
129762 unsigned int);
 
129763 };
129764
129765 /*
129766 ** This is the function signature used for all extension entry points. It
129767 ** is also defined in the file "loadext.c".
@@ -130076,14 +130099,16 @@
130076 #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
130077 #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
130078 #define sqlite3_vtab_in sqlite3_api->vtab_in
130079 #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
130080 #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
 
130081 #ifndef SQLITE_OMIT_DESERIALIZE
130082 #define sqlite3_deserialize sqlite3_api->deserialize
130083 #define sqlite3_serialize sqlite3_api->serialize
130084 #endif
 
130085 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
130086
130087 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
130088 /* This case when the file really is being compiled as a loadable
130089 ** extension */
@@ -130587,15 +130612,16 @@
130587 0,
130588 #endif
130589 /* Version 3.39.0 and later */
130590 #ifndef SQLITE_OMIT_DESERIALIZE
130591 sqlite3_deserialize,
130592 sqlite3_serialize
130593 #else
 
130594 0,
130595 0
130596 #endif
 
130597 };
130598
130599 /* True if x is the directory separator character
130600 */
130601 #if SQLITE_OS_WIN
@@ -141946,10 +141972,33 @@
141946 }
141947 #endif
141948 return 1;
141949 }
141950 #endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141951
141952 /*
141953 ** Generate code for the SELECT statement given in the p argument.
141954 **
141955 ** The results are returned according to the SelectDest structure.
@@ -142051,19 +142100,16 @@
142051 ** systems handle this case differently, and not all the same way,
142052 ** which is just confusing. To avoid this, we follow PG's lead and
142053 ** disallow it altogether. */
142054 if( p->selFlags & SF_UFSrcCheck ){
142055 SrcItem *p0 = &p->pSrc->a[0];
142056 for(i=1; i<p->pSrc->nSrc; i++){
142057 SrcItem *p1 = &p->pSrc->a[i];
142058 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
142059 sqlite3ErrorMsg(pParse,
142060 "target object/alias may not appear in FROM clause: %s",
142061 p0->zAlias ? p0->zAlias : p0->pTab->zName
142062 );
142063 goto select_end;
142064 }
142065 }
142066
142067 /* Clear the SF_UFSrcCheck flag. The check has already been performed,
142068 ** and leaving this flag set can cause errors if a compound sub-query
142069 ** in p->pSrc is flattened into this query and this function called
@@ -142334,11 +142380,11 @@
142334 zSavedAuthContext = pParse->zAuthContext;
142335 pParse->zAuthContext = pItem->zName;
142336
142337 /* Generate code to implement the subquery
142338 **
142339 ** The subquery is implemented as a co-routine all of the following are
142340 ** true:
142341 **
142342 ** (1) the subquery is guaranteed to be the outer loop (so that
142343 ** it does not need to be computed more than once), and
142344 ** (2) the subquery is not a CTE that should be materialized
@@ -142392,15 +142438,15 @@
142392 /* Materialize the view. If the view is not correlated, generate a
142393 ** subroutine to do the materialization so that subsequent uses of
142394 ** the same view can reuse the materialization. */
142395 int topAddr;
142396 int onceAddr = 0;
142397 int retAddr;
142398
142399 pItem->regReturn = ++pParse->nMem;
142400 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
142401 pItem->addrFillSub = topAddr+1;
 
142402 if( pItem->fg.isCorrelated==0 ){
142403 /* If the subquery is not correlated and if we are not inside of
142404 ** a trigger, then we only need to compute the value of the subquery
142405 ** once. */
142406 onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
@@ -142411,13 +142457,13 @@
142411 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
142412 ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem));
142413 sqlite3Select(pParse, pSub, &dest);
142414 pItem->pTab->nRowLogEst = pSub->nSelectRow;
142415 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
142416 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
142417 VdbeComment((v, "end %!S", pItem));
142418 sqlite3VdbeChangeP1(v, topAddr, retAddr);
142419 sqlite3ClearTempRegCache(pParse);
142420 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
142421 CteUse *pCteUse = pItem->u2.pCteUse;
142422 pCteUse->addrM9e = pItem->addrFillSub;
142423 pCteUse->regRtn = pItem->regReturn;
@@ -152973,18 +153019,26 @@
152973 sqlite3TreeViewExpr(0,pExpr,0);
152974 abort();
152975 }
152976 #endif
152977
152978 if( ExprHasProperty(pExpr, EP_OuterON) ){
152979 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
152980 prereqAll |= x;
152981 extraRight = x-1; /* ON clause terms may not be used with an index
152982 ** on left table of a LEFT JOIN. Ticket #3015 */
152983 if( (prereqAll>>1)>=x ){
152984 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
152985 return;
 
 
 
 
 
 
 
 
152986 }
152987 }
152988 pTerm->prereqAll = prereqAll;
152989 pTerm->leftCursor = -1;
152990 pTerm->iParent = -1;
@@ -157368,10 +157422,11 @@
157368 pIdxInfo->orderByConsumed = 0;
157369 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
157370 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
157371 }
157372
 
157373 if( isLimitTerm(pTerm) && *pbIn ){
157374 /* If there is an IN(...) term handled as an == (separate call to
157375 ** xFilter for each value on the RHS of the IN) and a LIMIT or
157376 ** OFFSET term handled as well, the plan is unusable. Set output
157377 ** variable *pbRetryLimit to true to tell the caller to retry with
@@ -157841,11 +157896,13 @@
157841 SrcList *pTabList = pWInfo->pTabList;
157842 SrcItem *pItem;
157843 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
157844 sqlite3 *db = pWInfo->pParse->db;
157845 int rc = SQLITE_OK;
 
157846 WhereLoop *pNew;
 
157847
157848 /* Loop over the tables in the join, from left to right */
157849 pNew = pBuilder->pNew;
157850 whereLoopInit(pNew);
157851 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
@@ -157852,14 +157909,17 @@
157852 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
157853 Bitmask mUnusable = 0;
157854 pNew->iTab = iTab;
157855 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
157856 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
157857 if( (pItem->fg.jointype & (JT_OUTER|JT_CROSS))!=0 ){
157858 /* This condition is true when pItem is the FROM clause term on the
157859 ** right-hand-side of a OUTER or CROSS JOIN. */
 
 
157860 mPrereq |= mPrior;
 
157861 }
157862 #ifndef SQLITE_OMIT_VIRTUALTABLE
157863 if( IsVirtual(pItem->pTab) ){
157864 SrcItem *p;
157865 for(p=&pItem[1]; p<pEnd; p++){
@@ -159551,10 +159611,11 @@
159551 op = OP_ReopenIdx;
159552 }else{
159553 iIndexCur = pParse->nTab++;
159554 }
159555 pLevel->iIdxCur = iIndexCur;
 
159556 assert( pIx->pSchema==pTab->pSchema );
159557 assert( iIndexCur>=0 );
159558 if( op ){
159559 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
159560 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
@@ -159626,13 +159687,24 @@
159626 ** program.
159627 */
159628 for(ii=0; ii<nTabList; ii++){
159629 int addrExplain;
159630 int wsFlags;
 
159631 if( pParse->nErr ) goto whereBeginError;
159632 pLevel = &pWInfo->a[ii];
159633 wsFlags = pLevel->pWLoop->wsFlags;
 
 
 
 
 
 
 
 
 
 
159634 if( (wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))!=0 ){
159635 if( (wsFlags & WHERE_AUTO_INDEX)!=0 ){
159636 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
159637 constructAutomaticIndex(pParse, &pWInfo->sWC,
159638 &pTabList->a[pLevel->iFrom], notReady, pLevel);
@@ -167347,11 +167419,22 @@
167347 break;
167348 case 157: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
167349 {
167350 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-4].minor.yy0);
167351 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy322,"set list");
167352 yymsp[-5].minor.yy131 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy131, yymsp[-1].minor.yy131);
 
 
 
 
 
 
 
 
 
 
 
167353 sqlite3Update(pParse,yymsp[-5].minor.yy131,yymsp[-2].minor.yy322,yymsp[0].minor.yy528,yymsp[-6].minor.yy394,0,0,0);
167354 }
167355 break;
167356 case 158: /* setlist ::= setlist COMMA nm EQ expr */
167357 {
@@ -167652,11 +167735,12 @@
167652 **
167653 ** simplify to constants 0 (false) and 1 (true), respectively,
167654 ** regardless of the value of expr1.
167655 */
167656 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy528);
167657 yymsp[-4].minor.yy528 = sqlite3Expr(pParse->db, TK_INTEGER, yymsp[-3].minor.yy394 ? "1" : "0");
 
167658 }else{
167659 Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
167660 if( yymsp[-1].minor.yy322->nExpr==1 && sqlite3ExprIsConstant(pRHS) && yymsp[-4].minor.yy528->op!=TK_VECTOR ){
167661 yymsp[-1].minor.yy322->a[0].pExpr = 0;
167662 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
@@ -174920,10 +175004,28 @@
174920 */
174921 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
174922 int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
174923 return iDb<0 ? 0 : db->aDb[iDb].pBt;
174924 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174925
174926 /*
174927 ** Return the filename of the database associated with a database
174928 ** connection.
174929 */
@@ -236303,11 +236405,11 @@
236303 int nArg, /* Number of args */
236304 sqlite3_value **apUnused /* Function arguments */
236305 ){
236306 assert( nArg==0 );
236307 UNUSED_PARAM2(nArg, apUnused);
236308 sqlite3_result_text(pCtx, "fts5: 2022-05-14 19:05:13 2277f9ba7087dd993ac0f4007c523aa9cf74dba187f53af03d8c164886726fee", -1, SQLITE_TRANSIENT);
236309 }
236310
236311 /*
236312 ** Return true if zName is the extension on one of the shadow tables used
236313 ** by this module.
236314
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -452,11 +452,11 @@
452 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
453 ** [sqlite_version()] and [sqlite_source_id()].
454 */
455 #define SQLITE_VERSION "3.39.0"
456 #define SQLITE_VERSION_NUMBER 3039000
457 #define SQLITE_SOURCE_ID "2022-05-25 13:10:29 c48a735bd4a1dbd541aed5937c25fc0c606c4263d8ee94cae30a06b1a7b26d9a"
458
459 /*
460 ** CAPI3REF: Run-Time Library Version Numbers
461 ** KEYWORDS: sqlite3_version sqlite3_sourceid
462 **
@@ -6580,10 +6580,30 @@
6580 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6581 ** create the statement in the first place.
6582 */
6583 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6584
6585 /*
6586 ** CAPI3REF: Return The Schema Name For A Database Connection
6587 ** METHOD: sqlite3
6588 **
6589 ** ^The sqlite3_db_name(D,N) interface returns a pointer to the schema name
6590 ** for the N-th database on database connection D, or a NULL pointer of N is
6591 ** out of range.
6592 **
6593 ** Space to hold the string that is returned by sqlite3_db_name() is managed
6594 ** by SQLite itself. The string might be deallocated by any operation that
6595 ** changes the schema, including [ATTACH] or [DETACH] or calls to
6596 ** [sqlite3_serialize()] or [sqlite3_deserialize()], even operations that
6597 ** occur on a different thread. Applications that need to
6598 ** remember the string long-term should make their own copy. Applications that
6599 ** are accessing the same database connection simultaneously on multiple
6600 ** threads should mutex-protect calls to this API and should make their own
6601 ** private copy of the result prior to releasing the mutex.
6602 */
6603 SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N);
6604
6605 /*
6606 ** CAPI3REF: Return The Filename For A Database Connection
6607 ** METHOD: sqlite3
6608 **
6609 ** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
@@ -18373,10 +18393,11 @@
18393 u8 jointype; /* Type of join between this table and the previous */
18394 unsigned notIndexed :1; /* True if there is a NOT INDEXED clause */
18395 unsigned isIndexedBy :1; /* True if there is an INDEXED BY clause */
18396 unsigned isTabFunc :1; /* True if table-valued-function syntax */
18397 unsigned isCorrelated :1; /* True if sub-query is correlated */
18398 unsigned isMaterialized:1; /* This is a materialized view */
18399 unsigned viaCoroutine :1; /* Implemented as a co-routine */
18400 unsigned isRecursive :1; /* True for recursive reference in WITH */
18401 unsigned fromDDL :1; /* Comes from sqlite_schema */
18402 unsigned isCte :1; /* This is a CTE */
18403 unsigned notCte :1; /* This item may not match a CTE */
@@ -41909,11 +41930,11 @@
41930 if( pPath->nSymlink++ > SQLITE_MAX_SYMLINK ){
41931 pPath->rc = SQLITE_CANTOPEN_BKPT;
41932 return;
41933 }
41934 got = osReadlink(zIn, zLnk, sizeof(zLnk)-2);
41935 if( got<=0 || got>=(ssize_t)sizeof(zLnk)-2 ){
41936 pPath->rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
41937 return;
41938 }
41939 zLnk[got] = 0;
41940 if( zLnk[0]=='/' ){
@@ -41959,10 +41980,11 @@
41980 const char *zPath, /* Possibly relative input path */
41981 int nOut, /* Size of output buffer in bytes */
41982 char *zOut /* Output buffer */
41983 ){
41984 DbPath path;
41985 UNUSED_PARAMETER(pVfs);
41986 path.rc = 0;
41987 path.nUsed = 0;
41988 path.nSymlink = 0;
41989 path.nOut = nOut;
41990 path.zOut = zOut;
@@ -124330,15 +124352,15 @@
124352 }else{
124353 ans = log(x);
124354 switch( SQLITE_PTR_TO_INT(sqlite3_user_data(context)) ){
124355 case 1:
124356 /* Convert from natural logarithm to log base 10 */
124357 ans /= M_LN10;
124358 break;
124359 case 2:
124360 /* Convert from natural logarithm to log base 2 */
124361 ans /= M_LN2;
124362 break;
124363 default:
124364 break;
124365 }
124366 }
@@ -129758,10 +129780,11 @@
129780 /* Version 3.39.0 and later */
129781 int (*deserialize)(sqlite3*,const char*,unsigned char*,
129782 sqlite3_int64,sqlite3_int64,unsigned);
129783 unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
129784 unsigned int);
129785 const char *(*db_name)(sqlite3*,int);
129786 };
129787
129788 /*
129789 ** This is the function signature used for all extension entry points. It
129790 ** is also defined in the file "loadext.c".
@@ -130076,14 +130099,16 @@
130099 #define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
130100 #define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
130101 #define sqlite3_vtab_in sqlite3_api->vtab_in
130102 #define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
130103 #define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
130104 /* Version 3.39.0 and later */
130105 #ifndef SQLITE_OMIT_DESERIALIZE
130106 #define sqlite3_deserialize sqlite3_api->deserialize
130107 #define sqlite3_serialize sqlite3_api->serialize
130108 #endif
130109 #define sqlite3_db_name sqlite3_api->db_name
130110 #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
130111
130112 #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
130113 /* This case when the file really is being compiled as a loadable
130114 ** extension */
@@ -130587,15 +130612,16 @@
130612 0,
130613 #endif
130614 /* Version 3.39.0 and later */
130615 #ifndef SQLITE_OMIT_DESERIALIZE
130616 sqlite3_deserialize,
130617 sqlite3_serialize,
130618 #else
130619 0,
130620 0,
 
130621 #endif
130622 sqlite3_db_name
130623 };
130624
130625 /* True if x is the directory separator character
130626 */
130627 #if SQLITE_OS_WIN
@@ -141946,10 +141972,33 @@
141972 }
141973 #endif
141974 return 1;
141975 }
141976 #endif /* SQLITE_COUNTOFVIEW_OPTIMIZATION */
141977
141978 /*
141979 ** If any term of pSrc, or any SF_NestedFrom sub-query, is not the same
141980 ** as pSrcItem but has the same alias as p0, then return true.
141981 ** Otherwise return false.
141982 */
141983 static int sameSrcAlias(SrcItem *p0, SrcList *pSrc){
141984 int i;
141985 for(i=0; i<pSrc->nSrc; i++){
141986 SrcItem *p1 = &pSrc->a[i];
141987 if( p1==p0 ) continue;
141988 if( p0->pTab==p1->pTab && 0==sqlite3_stricmp(p0->zAlias, p1->zAlias) ){
141989 return 1;
141990 }
141991 if( p1->pSelect
141992 && (p1->pSelect->selFlags & SF_NestedFrom)!=0
141993 && sameSrcAlias(p0, p1->pSelect->pSrc)
141994 ){
141995 return 1;
141996 }
141997 }
141998 return 0;
141999 }
142000
142001 /*
142002 ** Generate code for the SELECT statement given in the p argument.
142003 **
142004 ** The results are returned according to the SelectDest structure.
@@ -142051,19 +142100,16 @@
142100 ** systems handle this case differently, and not all the same way,
142101 ** which is just confusing. To avoid this, we follow PG's lead and
142102 ** disallow it altogether. */
142103 if( p->selFlags & SF_UFSrcCheck ){
142104 SrcItem *p0 = &p->pSrc->a[0];
142105 if( sameSrcAlias(p0, p->pSrc) ){
142106 sqlite3ErrorMsg(pParse,
142107 "target object/alias may not appear in FROM clause: %s",
142108 p0->zAlias ? p0->zAlias : p0->pTab->zName
142109 );
142110 goto select_end;
 
 
 
142111 }
142112
142113 /* Clear the SF_UFSrcCheck flag. The check has already been performed,
142114 ** and leaving this flag set can cause errors if a compound sub-query
142115 ** in p->pSrc is flattened into this query and this function called
@@ -142334,11 +142380,11 @@
142380 zSavedAuthContext = pParse->zAuthContext;
142381 pParse->zAuthContext = pItem->zName;
142382
142383 /* Generate code to implement the subquery
142384 **
142385 ** The subquery is implemented as a co-routine all if the following are
142386 ** true:
142387 **
142388 ** (1) the subquery is guaranteed to be the outer loop (so that
142389 ** it does not need to be computed more than once), and
142390 ** (2) the subquery is not a CTE that should be materialized
@@ -142392,15 +142438,15 @@
142438 /* Materialize the view. If the view is not correlated, generate a
142439 ** subroutine to do the materialization so that subsequent uses of
142440 ** the same view can reuse the materialization. */
142441 int topAddr;
142442 int onceAddr = 0;
 
142443
142444 pItem->regReturn = ++pParse->nMem;
142445 topAddr = sqlite3VdbeAddOp0(v, OP_Goto);
142446 pItem->addrFillSub = topAddr+1;
142447 pItem->fg.isMaterialized = 1;
142448 if( pItem->fg.isCorrelated==0 ){
142449 /* If the subquery is not correlated and if we are not inside of
142450 ** a trigger, then we only need to compute the value of the subquery
142451 ** once. */
142452 onceAddr = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
@@ -142411,13 +142457,13 @@
142457 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
142458 ExplainQueryPlan((pParse, 1, "MATERIALIZE %!S", pItem));
142459 sqlite3Select(pParse, pSub, &dest);
142460 pItem->pTab->nRowLogEst = pSub->nSelectRow;
142461 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
142462 sqlite3VdbeAddOp2(v, OP_Return, pItem->regReturn, topAddr+1);
142463 VdbeComment((v, "end %!S", pItem));
142464 sqlite3VdbeJumpHere(v, topAddr);
142465 sqlite3ClearTempRegCache(pParse);
142466 if( pItem->fg.isCte && pItem->fg.isCorrelated==0 ){
142467 CteUse *pCteUse = pItem->u2.pCteUse;
142468 pCteUse->addrM9e = pItem->addrFillSub;
142469 pCteUse->regRtn = pItem->regReturn;
@@ -152973,18 +153019,26 @@
153019 sqlite3TreeViewExpr(0,pExpr,0);
153020 abort();
153021 }
153022 #endif
153023
153024 if( ExprHasProperty(pExpr, EP_OuterON|EP_InnerON) ){
153025 Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->w.iJoin);
153026 if( ExprHasProperty(pExpr, EP_OuterON) ){
153027 prereqAll |= x;
153028 extraRight = x-1; /* ON clause terms may not be used with an index
153029 ** on left table of a LEFT JOIN. Ticket #3015 */
153030 if( (prereqAll>>1)>=x ){
153031 sqlite3ErrorMsg(pParse, "ON clause references tables to its right");
153032 return;
153033 }
153034 }else if( (prereqAll>>1)>=x ){
153035 /* The ON clause of an INNER JOIN references a table to its right.
153036 ** Most other SQL database engines raise an error. But all versions
153037 ** of SQLite going back to 3.0.0 have just put the ON clause constraint
153038 ** into the WHERE clause and carried on. */
153039 ExprClearProperty(pExpr, EP_InnerON);
153040 }
153041 }
153042 pTerm->prereqAll = prereqAll;
153043 pTerm->leftCursor = -1;
153044 pTerm->iParent = -1;
@@ -157368,10 +157422,11 @@
157422 pIdxInfo->orderByConsumed = 0;
157423 pIdxInfo->idxFlags &= ~SQLITE_INDEX_SCAN_UNIQUE;
157424 *pbIn = 1; assert( (mExclude & WO_IN)==0 );
157425 }
157426
157427 assert( pbRetryLimit || !isLimitTerm(pTerm) );
157428 if( isLimitTerm(pTerm) && *pbIn ){
157429 /* If there is an IN(...) term handled as an == (separate call to
157430 ** xFilter for each value on the RHS of the IN) and a LIMIT or
157431 ** OFFSET term handled as well, the plan is unusable. Set output
157432 ** variable *pbRetryLimit to true to tell the caller to retry with
@@ -157841,11 +157896,13 @@
157896 SrcList *pTabList = pWInfo->pTabList;
157897 SrcItem *pItem;
157898 SrcItem *pEnd = &pTabList->a[pWInfo->nLevel];
157899 sqlite3 *db = pWInfo->pParse->db;
157900 int rc = SQLITE_OK;
157901 int bFirstPastRJ = 0;
157902 WhereLoop *pNew;
157903
157904
157905 /* Loop over the tables in the join, from left to right */
157906 pNew = pBuilder->pNew;
157907 whereLoopInit(pNew);
157908 pBuilder->iPlanLimit = SQLITE_QUERY_PLANNER_LIMIT;
@@ -157852,14 +157909,17 @@
157909 for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
157910 Bitmask mUnusable = 0;
157911 pNew->iTab = iTab;
157912 pBuilder->iPlanLimit += SQLITE_QUERY_PLANNER_LIMIT_INCR;
157913 pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
157914 if( bFirstPastRJ || (pItem->fg.jointype & (JT_OUTER|JT_CROSS))!=0 ){
157915 /* Add prerequisites to prevent reordering of FROM clause terms
157916 ** across CROSS joins and outer joins. The bFirstPastRJ boolean
157917 ** prevents the right operand of a RIGHT JOIN from being swapped with
157918 ** other elements even further to the right. */
157919 mPrereq |= mPrior;
157920 bFirstPastRJ = (pItem->fg.jointype & JT_RIGHT)!=0;
157921 }
157922 #ifndef SQLITE_OMIT_VIRTUALTABLE
157923 if( IsVirtual(pItem->pTab) ){
157924 SrcItem *p;
157925 for(p=&pItem[1]; p<pEnd; p++){
@@ -159551,10 +159611,11 @@
159611 op = OP_ReopenIdx;
159612 }else{
159613 iIndexCur = pParse->nTab++;
159614 }
159615 pLevel->iIdxCur = iIndexCur;
159616 assert( pIx!=0 );
159617 assert( pIx->pSchema==pTab->pSchema );
159618 assert( iIndexCur>=0 );
159619 if( op ){
159620 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
159621 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
@@ -159626,13 +159687,24 @@
159687 ** program.
159688 */
159689 for(ii=0; ii<nTabList; ii++){
159690 int addrExplain;
159691 int wsFlags;
159692 SrcItem *pSrc;
159693 if( pParse->nErr ) goto whereBeginError;
159694 pLevel = &pWInfo->a[ii];
159695 wsFlags = pLevel->pWLoop->wsFlags;
159696 pSrc = &pTabList->a[pLevel->iFrom];
159697 if( pSrc->fg.isMaterialized ){
159698 if( pSrc->fg.isCorrelated ){
159699 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
159700 }else{
159701 int iOnce = sqlite3VdbeAddOp0(v, OP_Once); VdbeCoverage(v);
159702 sqlite3VdbeAddOp2(v, OP_Gosub, pSrc->regReturn, pSrc->addrFillSub);
159703 sqlite3VdbeJumpHere(v, iOnce);
159704 }
159705 }
159706 if( (wsFlags & (WHERE_AUTO_INDEX|WHERE_BLOOMFILTER))!=0 ){
159707 if( (wsFlags & WHERE_AUTO_INDEX)!=0 ){
159708 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
159709 constructAutomaticIndex(pParse, &pWInfo->sWC,
159710 &pTabList->a[pLevel->iFrom], notReady, pLevel);
@@ -167347,11 +167419,22 @@
167419 break;
167420 case 157: /* cmd ::= with UPDATE orconf xfullname indexed_opt SET setlist from where_opt_ret */
167421 {
167422 sqlite3SrcListIndexedBy(pParse, yymsp[-5].minor.yy131, &yymsp[-4].minor.yy0);
167423 sqlite3ExprListCheckLength(pParse,yymsp[-2].minor.yy322,"set list");
167424 if( yymsp[-1].minor.yy131 ){
167425 SrcList *pFromClause = yymsp[-1].minor.yy131;
167426 if( pFromClause->nSrc>1 ){
167427 Select *pSubquery;
167428 Token as;
167429 pSubquery = sqlite3SelectNew(pParse,0,pFromClause,0,0,0,0,SF_NestedFrom,0);
167430 as.n = 0;
167431 as.z = 0;
167432 pFromClause = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&as,pSubquery,0);
167433 }
167434 yymsp[-5].minor.yy131 = sqlite3SrcListAppendList(pParse, yymsp[-5].minor.yy131, pFromClause);
167435 }
167436 sqlite3Update(pParse,yymsp[-5].minor.yy131,yymsp[-2].minor.yy322,yymsp[0].minor.yy528,yymsp[-6].minor.yy394,0,0,0);
167437 }
167438 break;
167439 case 158: /* setlist ::= setlist COMMA nm EQ expr */
167440 {
@@ -167652,11 +167735,12 @@
167735 **
167736 ** simplify to constants 0 (false) and 1 (true), respectively,
167737 ** regardless of the value of expr1.
167738 */
167739 sqlite3ExprUnmapAndDelete(pParse, yymsp[-4].minor.yy528);
167740 yymsp[-4].minor.yy528 = sqlite3Expr(pParse->db, TK_STRING, yymsp[-3].minor.yy394 ? "true" : "false");
167741 if( yymsp[-4].minor.yy528 ) sqlite3ExprIdToTrueFalse(yymsp[-4].minor.yy528);
167742 }else{
167743 Expr *pRHS = yymsp[-1].minor.yy322->a[0].pExpr;
167744 if( yymsp[-1].minor.yy322->nExpr==1 && sqlite3ExprIsConstant(pRHS) && yymsp[-4].minor.yy528->op!=TK_VECTOR ){
167745 yymsp[-1].minor.yy322->a[0].pExpr = 0;
167746 sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322);
@@ -174920,10 +175004,28 @@
175004 */
175005 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
175006 int iDb = zDbName ? sqlite3FindDbName(db, zDbName) : 0;
175007 return iDb<0 ? 0 : db->aDb[iDb].pBt;
175008 }
175009
175010 /*
175011 ** Return the name of the N-th database schema. Return NULL if N is out
175012 ** of range.
175013 */
175014 SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N){
175015 #ifdef SQLITE_ENABLE_API_ARMOR
175016 if( !sqlite3SafetyCheckOk(db) ){
175017 (void)SQLITE_MISUSE_BKPT;
175018 return 0;
175019 }
175020 #endif
175021 if( N<0 || N>=db->nDb ){
175022 return 0;
175023 }else{
175024 return db->aDb[N].zDbSName;
175025 }
175026 }
175027
175028 /*
175029 ** Return the filename of the database associated with a database
175030 ** connection.
175031 */
@@ -236303,11 +236405,11 @@
236405 int nArg, /* Number of args */
236406 sqlite3_value **apUnused /* Function arguments */
236407 ){
236408 assert( nArg==0 );
236409 UNUSED_PARAM2(nArg, apUnused);
236410 sqlite3_result_text(pCtx, "fts5: 2022-05-23 11:51:10 919ba2f0472e12c2d1e82364c1481e778b24ea406615b08992964a4eb80abee7", -1, SQLITE_TRANSIENT);
236411 }
236412
236413 /*
236414 ** Return true if zName is the extension on one of the shadow tables used
236415 ** by this module.
236416
+21 -1
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.39.0"
150150
#define SQLITE_VERSION_NUMBER 3039000
151
-#define SQLITE_SOURCE_ID "2022-05-14 19:05:13 2277f9ba7087dd993ac0f4007c523aa9cf74dba187f53af03d8c164886726fee"
151
+#define SQLITE_SOURCE_ID "2022-05-25 13:10:29 c48a735bd4a1dbd541aed5937c25fc0c606c4263d8ee94cae30a06b1a7b26d9a"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -6274,10 +6274,30 @@
62746274
** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
62756275
** create the statement in the first place.
62766276
*/
62776277
SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
62786278
6279
+/*
6280
+** CAPI3REF: Return The Schema Name For A Database Connection
6281
+** METHOD: sqlite3
6282
+**
6283
+** ^The sqlite3_db_name(D,N) interface returns a pointer to the schema name
6284
+** for the N-th database on database connection D, or a NULL pointer of N is
6285
+** out of range.
6286
+**
6287
+** Space to hold the string that is returned by sqlite3_db_name() is managed
6288
+** by SQLite itself. The string might be deallocated by any operation that
6289
+** changes the schema, including [ATTACH] or [DETACH] or calls to
6290
+** [sqlite3_serialize()] or [sqlite3_deserialize()], even operations that
6291
+** occur on a different thread. Applications that need to
6292
+** remember the string long-term should make their own copy. Applications that
6293
+** are accessing the same database connection simultaneously on multiple
6294
+** threads should mutex-protect calls to this API and should make their own
6295
+** private copy of the result prior to releasing the mutex.
6296
+*/
6297
+SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N);
6298
+
62796299
/*
62806300
** CAPI3REF: Return The Filename For A Database Connection
62816301
** METHOD: sqlite3
62826302
**
62836303
** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
62846304
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.39.0"
150 #define SQLITE_VERSION_NUMBER 3039000
151 #define SQLITE_SOURCE_ID "2022-05-14 19:05:13 2277f9ba7087dd993ac0f4007c523aa9cf74dba187f53af03d8c164886726fee"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -6274,10 +6274,30 @@
6274 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6275 ** create the statement in the first place.
6276 */
6277 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6278
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6279 /*
6280 ** CAPI3REF: Return The Filename For A Database Connection
6281 ** METHOD: sqlite3
6282 **
6283 ** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
6284
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.39.0"
150 #define SQLITE_VERSION_NUMBER 3039000
151 #define SQLITE_SOURCE_ID "2022-05-25 13:10:29 c48a735bd4a1dbd541aed5937c25fc0c606c4263d8ee94cae30a06b1a7b26d9a"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -6274,10 +6274,30 @@
6274 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6275 ** create the statement in the first place.
6276 */
6277 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6278
6279 /*
6280 ** CAPI3REF: Return The Schema Name For A Database Connection
6281 ** METHOD: sqlite3
6282 **
6283 ** ^The sqlite3_db_name(D,N) interface returns a pointer to the schema name
6284 ** for the N-th database on database connection D, or a NULL pointer of N is
6285 ** out of range.
6286 **
6287 ** Space to hold the string that is returned by sqlite3_db_name() is managed
6288 ** by SQLite itself. The string might be deallocated by any operation that
6289 ** changes the schema, including [ATTACH] or [DETACH] or calls to
6290 ** [sqlite3_serialize()] or [sqlite3_deserialize()], even operations that
6291 ** occur on a different thread. Applications that need to
6292 ** remember the string long-term should make their own copy. Applications that
6293 ** are accessing the same database connection simultaneously on multiple
6294 ** threads should mutex-protect calls to this API and should make their own
6295 ** private copy of the result prior to releasing the mutex.
6296 */
6297 SQLITE_API const char *sqlite3_db_name(sqlite3 *db, int N);
6298
6299 /*
6300 ** CAPI3REF: Return The Filename For A Database Connection
6301 ** METHOD: sqlite3
6302 **
6303 ** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
6304

Keyboard Shortcuts

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