Subversion Repositories Kolibri OS

Rev

Rev 8941 | Details | Compare with Previous | Last modification | View Log | RSS feed

");\n");");\n");\n");
Rev Author Line No. Line
8760 turbocat 1
/* DO NOT EDIT!
2
** This file is automatically generated by the script in the canonical
3
** SQLite source tree at tool/mkshellc.tcl.  That script combines source
4
** code from various constituent source files of SQLite into this single
5
** "shell.c" file used to implement the SQLite command-line shell.
6
**
7
** Most of the code found below comes from the "src/shell.c.in" file in
8
** the canonical SQLite source tree.  That main file contains "INCLUDE"
9
** lines that specify other files in the canonical source tree that are
10
** inserted to getnerate this complete program source file.
11
**
12
** The code from multiple files is combined into this single "shell.c"
13
** source file to help make the command-line program easier to compile.
14
**
15
** To modify this program, get a copy of the canonical SQLite source tree,
16
** edit the src/shell.c.in" and/or some of the other files that are included
17
** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script.
18
*/
19
/*
20
** 2001 September 15
21
**
22
** The author disclaims copyright to this source code.  In place of
23
** a legal notice, here is a blessing:
24
**
25
**    May you do good and not evil.
26
**    May you find forgiveness for yourself and forgive others.
27
**    May you share freely, never taking more than you give.
28
**
29
*************************************************************************
30
** This file contains code to implement the "sqlite" command line
31
** utility for accessing SQLite databases.
32
*/
33
#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
34
/* This needs to come before any includes for MSVC compiler */
35
#define _CRT_SECURE_NO_WARNINGS
36
#endif
37
 
38
/*
39
** Determine if we are dealing with WinRT, which provides only a subset of
40
** the full Win32 API.
41
*/
42
#if !defined(SQLITE_OS_WINRT)
43
# define SQLITE_OS_WINRT 0
44
#endif
45
 
46
/*
47
** Warning pragmas copied from msvc.h in the core.
48
*/
49
#if defined(_MSC_VER)
50
#pragma warning(disable : 4054)
51
#pragma warning(disable : 4055)
52
#pragma warning(disable : 4100)
53
#pragma warning(disable : 4127)
54
#pragma warning(disable : 4130)
55
#pragma warning(disable : 4152)
56
#pragma warning(disable : 4189)
57
#pragma warning(disable : 4206)
58
#pragma warning(disable : 4210)
59
#pragma warning(disable : 4232)
60
#pragma warning(disable : 4244)
61
#pragma warning(disable : 4305)
62
#pragma warning(disable : 4306)
63
#pragma warning(disable : 4702)
64
#pragma warning(disable : 4706)
65
#endif /* defined(_MSC_VER) */
66
 
67
/*
68
** No support for loadable extensions in VxWorks.
69
*/
70
#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
71
# define SQLITE_OMIT_LOAD_EXTENSION 1
72
#endif
73
 
74
/*
75
** Enable large-file support for fopen() and friends on unix.
76
*/
77
#ifndef SQLITE_DISABLE_LFS
78
# define _LARGE_FILE       1
79
# ifndef _FILE_OFFSET_BITS
80
#   define _FILE_OFFSET_BITS 64
81
# endif
82
# define _LARGEFILE_SOURCE 1
83
#endif
84
 
85
#include 
86
#include 
87
#include 
88
#include 
89
#include "sqlite3.h"
90
typedef sqlite3_int64 i64;
91
typedef sqlite3_uint64 u64;
92
typedef unsigned char u8;
93
#if SQLITE_USER_AUTHENTICATION
94
# include "sqlite3userauth.h"
95
#endif
96
#include 
97
#include 
98
 
99
#if !defined(_WIN32) && !defined(WIN32)
100
# include 
101
# if !defined(__RTP__) && !defined(_WRS_KERNEL)
102
#  include 
103
# endif
104
#endif
9952 turbocat 105
#if (!defined(_WIN32) && !defined(WIN32) && !defined (_KOLIBRI)) || defined(__MINGW32__)
8760 turbocat 106
# include 
107
# include 
108
# define GETPID getpid
109
# if defined(__MINGW32__)
110
#  define DIRENT dirent
111
#  ifndef S_ISLNK
112
#   define S_ISLNK(mode) (0)
113
#  endif
114
# endif
115
#else
116
# define GETPID (int)GetCurrentProcessId
117
#endif
118
#include 
119
#include 
120
 
121
#if HAVE_READLINE
122
# include 
123
# include 
124
#endif
125
 
126
#if HAVE_EDITLINE
127
# include 
128
#endif
129
 
130
#if HAVE_EDITLINE || HAVE_READLINE
131
 
132
# define shell_add_history(X) add_history(X)
133
# define shell_read_history(X) read_history(X)
134
# define shell_write_history(X) write_history(X)
135
# define shell_stifle_history(X) stifle_history(X)
136
# define shell_readline(X) readline(X)
137
 
138
#elif HAVE_LINENOISE
139
 
140
# include "linenoise.h"
141
# define shell_add_history(X) linenoiseHistoryAdd(X)
142
# define shell_read_history(X) linenoiseHistoryLoad(X)
143
# define shell_write_history(X) linenoiseHistorySave(X)
144
# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
145
# define shell_readline(X) linenoise(X)
146
 
147
#else
148
 
149
# define shell_read_history(X)
150
# define shell_write_history(X)
151
# define shell_stifle_history(X)
152
 
153
# define SHELL_USE_LOCAL_GETLINE 1
154
#endif
155
 
156
 
157
#if defined(_WIN32) || defined(WIN32)
158
# if SQLITE_OS_WINRT
159
#  define SQLITE_OMIT_POPEN 1
160
# else
161
#  include 
162
#  include 
163
#  define isatty(h) _isatty(h)
164
#  ifndef access
165
#   define access(f,m) _access((f),(m))
166
#  endif
167
#  ifndef unlink
168
#   define unlink _unlink
169
#  endif
170
#  ifndef strdup
171
#   define strdup _strdup
172
#  endif
173
#  undef popen
174
#  define popen _popen
175
#  undef pclose
176
#  define pclose _pclose
177
# endif
178
#else
179
 /* Make sure isatty() has a prototype. */
180
 extern int isatty(int);
181
 
182
# if !defined(__RTP__) && !defined(_WRS_KERNEL)
183
  /* popen and pclose are not C89 functions and so are
184
  ** sometimes omitted from the  header */
185
   extern FILE *popen(const char*,const char*);
186
   extern int pclose(FILE*);
187
# else
188
#  define SQLITE_OMIT_POPEN 1
189
# endif
190
#endif
191
 
192
#if defined(_WIN32_WCE)
193
/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
194
 * thus we always assume that we have a console. That can be
195
 * overridden with the -batch command line option.
196
 */
197
#define isatty(x) 1
198
#endif
199
 
200
/* ctype macros that work with signed characters */
201
#define IsSpace(X)  isspace((unsigned char)X)
202
#define IsDigit(X)  isdigit((unsigned char)X)
203
#define ToLower(X)  (char)tolower((unsigned char)X)
204
 
205
#if defined(_WIN32) || defined(WIN32)
206
#if SQLITE_OS_WINRT
207
#include 
208
#endif
209
//#include 
210
 
211
#define stderr stdout
212
 
213
/* string conversion routines only needed on Win32 */
214
extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
215
extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
216
extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
217
extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
218
#endif
219
 
220
/* On Windows, we normally run with output mode of TEXT so that \n characters
221
** are automatically translated into \r\n.  However, this behavior needs
222
** to be disabled in some cases (ex: when generating CSV output and when
223
** rendering quoted strings that contain \n characters).  The following
224
** routines take care of that.
225
*/
226
#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
227
static void setBinaryMode(FILE *file, int isOutput){
228
  if( isOutput ) fflush(file);
229
  _setmode(_fileno(file), _O_BINARY);
230
}
231
static void setTextMode(FILE *file, int isOutput){
232
  if( isOutput ) fflush(file);
233
  _setmode(_fileno(file), _O_TEXT);
234
}
235
#else
236
# define setBinaryMode(X,Y)
237
# define setTextMode(X,Y)
238
#endif
239
 
240
 
241
/* True if the timer is enabled */
242
static int enableTimer = 0;
243
 
244
/* Return the current wall-clock time */
245
static sqlite3_int64 timeOfDay(void){
246
  static sqlite3_vfs *clockVfs = 0;
247
  sqlite3_int64 t;
248
  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
249
  if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
250
    clockVfs->xCurrentTimeInt64(clockVfs, &t);
251
  }else{
252
    double r;
253
    clockVfs->xCurrentTime(clockVfs, &r);
254
    t = (sqlite3_int64)(r*86400000.0);
255
  }
256
  return t;
257
}
258
 
259
#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
260
#include 
261
#include 
262
 
263
/* VxWorks does not support getrusage() as far as we can determine */
264
#if defined(_WRS_KERNEL) || defined(__RTP__)
265
struct rusage {
266
  struct timeval ru_utime; /* user CPU time used */
267
  struct timeval ru_stime; /* system CPU time used */
268
};
269
#define getrusage(A,B) memset(B,0,sizeof(*B))
270
#endif
271
 
272
#ifdef _KOLIBRI
273
#define getrusage(A,B) memset(B,0,sizeof(*B))
274
#endif
275
 
276
/* Saved resource information for the beginning of an operation */
277
static struct rusage sBegin;  /* CPU time at start */
278
static sqlite3_int64 iBegin;  /* Wall-clock time at start */
279
 
280
/*
281
** Begin timing an operation
282
*/
283
static void beginTimer(void){
284
  if( enableTimer ){
285
    getrusage(RUSAGE_SELF, &sBegin);
286
    iBegin = timeOfDay();
287
  }
288
}
289
 
290
/* Return the difference of two time_structs in seconds */
291
static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
292
  return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
293
         (double)(pEnd->tv_sec - pStart->tv_sec);
294
}
295
 
296
/*
297
** Print the timing results.
298
*/
299
static void endTimer(void){
300
  if( enableTimer ){
301
    sqlite3_int64 iEnd = timeOfDay();
302
    struct rusage sEnd;
303
    getrusage(RUSAGE_SELF, &sEnd);
304
    printf("Run Time: real %.3f user %f sys %f\n",
305
       (iEnd - iBegin)*0.001,
306
       timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
307
       timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
308
  }
309
}
310
 
311
#define BEGIN_TIMER beginTimer()
312
#define END_TIMER endTimer()
313
#define HAS_TIMER 1
314
 
315
#elif (defined(_WIN32) || defined(WIN32))
316
 
317
/* Saved resource information for the beginning of an operation */
318
static HANDLE hProcess;
319
static FILETIME ftKernelBegin;
320
static FILETIME ftUserBegin;
321
static sqlite3_int64 ftWallBegin;
322
typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
323
                                    LPFILETIME, LPFILETIME);
324
static GETPROCTIMES getProcessTimesAddr = NULL;
325
 
326
/*
327
** Check to see if we have timer support.  Return 1 if necessary
328
** support found (or found previously).
329
*/
330
static int hasTimer(void){
331
  if( getProcessTimesAddr ){
332
    return 1;
333
  } else {
334
#if !SQLITE_OS_WINRT
335
    /* GetProcessTimes() isn't supported in WIN95 and some other Windows
336
    ** versions. See if the version we are running on has it, and if it
337
    ** does, save off a pointer to it and the current process handle.
338
    */
339
    hProcess = GetCurrentProcess();
340
    if( hProcess ){
341
      HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
342
      if( NULL != hinstLib ){
343
        getProcessTimesAddr =
344
            (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
345
        if( NULL != getProcessTimesAddr ){
346
          return 1;
347
        }
348
        FreeLibrary(hinstLib);
349
      }
350
    }
351
#endif
352
  }
353
  return 0;
354
}
355
 
356
/*
357
** Begin timing an operation
358
*/
359
static void beginTimer(void){
360
  if( enableTimer && getProcessTimesAddr ){
361
    FILETIME ftCreation, ftExit;
362
    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
363
                        &ftKernelBegin,&ftUserBegin);
364
    ftWallBegin = timeOfDay();
365
  }
366
}
367
 
368
/* Return the difference of two FILETIME structs in seconds */
369
static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
370
  sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
371
  sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
372
  return (double) ((i64End - i64Start) / 10000000.0);
373
}
374
 
375
/*
376
** Print the timing results.
377
*/
378
static void endTimer(void){
379
  if( enableTimer && getProcessTimesAddr){
380
    FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
381
    sqlite3_int64 ftWallEnd = timeOfDay();
382
    getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
383
    printf("Run Time: real %.3f user %f sys %f\n",
384
       (ftWallEnd - ftWallBegin)*0.001,
385
       timeDiff(&ftUserBegin, &ftUserEnd),
386
       timeDiff(&ftKernelBegin, &ftKernelEnd));
387
  }
388
}
389
 
390
#define BEGIN_TIMER beginTimer()
391
#define END_TIMER endTimer()
392
#define HAS_TIMER hasTimer()
393
 
394
#else
395
#define BEGIN_TIMER
396
#define END_TIMER
397
#define HAS_TIMER 0
398
#endif
399
 
400
/*
401
** Used to prevent warnings about unused parameters
402
*/
403
#define UNUSED_PARAMETER(x) (void)(x)
404
 
405
/*
406
** Number of elements in an array
407
*/
408
#define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
409
 
410
/*
411
** If the following flag is set, then command execution stops
412
** at an error if we are not interactive.
413
*/
414
static int bail_on_error = 0;
415
 
416
/*
417
** Threat stdin as an interactive input if the following variable
418
** is true.  Otherwise, assume stdin is connected to a file or pipe.
419
*/
420
static int stdin_is_interactive = 1;
421
 
422
/*
423
** On Windows systems we have to know if standard output is a console
424
** in order to translate UTF-8 into MBCS.  The following variable is
425
** true if translation is required.
426
*/
427
static int stdout_is_console = 1;
428
 
429
/*
430
** The following is the open SQLite database.  We make a pointer
431
** to this database a static variable so that it can be accessed
432
** by the SIGINT handler to interrupt database processing.
433
*/
434
static sqlite3 *globalDb = 0;
435
 
436
/*
437
** True if an interrupt (Control-C) has been received.
438
*/
439
static volatile int seenInterrupt = 0;
440
 
441
#ifdef SQLITE_DEBUG
442
/*
443
** Out-of-memory simulator variables
444
*/
445
static unsigned int oomCounter = 0;    /* Simulate OOM when equals 1 */
446
static unsigned int oomRepeat = 0;     /* Number of OOMs in a row */
447
static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
448
#endif /* SQLITE_DEBUG */
449
 
450
/*
451
** This is the name of our program. It is set in main(), used
452
** in a number of other places, mostly for error messages.
453
*/
454
static char *Argv0;
455
 
456
/*
457
** Prompt strings. Initialized in main. Settable with
458
**   .prompt main continue
459
*/
460
static char mainPrompt[20];     /* First line prompt. default: "sqlite> "*/
461
static char continuePrompt[20]; /* Continuation prompt. default: "   ...> " */
462
 
463
/*
464
** Render output like fprintf().  Except, if the output is going to the
465
** console and if this is running on a Windows machine, translate the
466
** output from UTF-8 into MBCS.
467
*/
468
#if defined(_WIN32) || defined(WIN32)
469
void utf8_printf(FILE *out, const char *zFormat, ...){
470
  va_list ap;
471
  va_start(ap, zFormat);
472
  if( stdout_is_console && (out==stdout || out==stderr) ){
473
    char *z1 = sqlite3_vmprintf(zFormat, ap);
474
    char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
475
    sqlite3_free(z1);
476
    fputs(z2, out);
477
    sqlite3_free(z2);
478
  }else{
479
    vfprintf(out, zFormat, ap);
480
  }
481
  va_end(ap);
482
}
483
#elif !defined(utf8_printf)
484
# define utf8_printf fprintf
485
#endif
486
 
487
/*
488
** Render output like fprintf().  This should not be used on anything that
489
** includes string formatting (e.g. "%s").
490
*/
491
#if !defined(raw_printf)
492
# define raw_printf fprintf
493
#endif
494
 
495
/* Indicate out-of-memory and exit. */
496
static void shell_out_of_memory(void){
497
  raw_printf(stderr,"Error: out of memory\n");
498
  exit(1);
499
}
500
 
501
#ifdef SQLITE_DEBUG
502
/* This routine is called when a simulated OOM occurs.  It is broken
503
** out as a separate routine to make it easy to set a breakpoint on
504
** the OOM
505
*/
506
void shellOomFault(void){
507
  if( oomRepeat>0 ){
508
    oomRepeat--;
509
  }else{
510
    oomCounter--;
511
  }
512
}
513
#endif /* SQLITE_DEBUG */
514
 
515
#ifdef SQLITE_DEBUG
516
/* This routine is a replacement malloc() that is used to simulate
517
** Out-Of-Memory (OOM) errors for testing purposes.
518
*/
519
static void *oomMalloc(int nByte){
520
  if( oomCounter ){
521
    if( oomCounter==1 ){
522
      shellOomFault();
523
      return 0;
524
    }else{
525
      oomCounter--;
526
    }
527
  }
528
  return defaultMalloc(nByte);
529
}
530
#endif /* SQLITE_DEBUG */
531
 
532
#ifdef SQLITE_DEBUG
533
/* Register the OOM simulator.  This must occur before any memory
534
** allocations */
535
static void registerOomSimulator(void){
536
  sqlite3_mem_methods mem;
537
  sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
538
  defaultMalloc = mem.xMalloc;
539
  mem.xMalloc = oomMalloc;
540
  sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
541
}
542
#endif
543
 
544
/*
545
** Write I/O traces to the following stream.
546
*/
547
#ifdef SQLITE_ENABLE_IOTRACE
548
static FILE *iotrace = 0;
549
#endif
550
 
551
/*
552
** This routine works like printf in that its first argument is a
553
** format string and subsequent arguments are values to be substituted
554
** in place of % fields.  The result of formatting this string
555
** is written to iotrace.
556
*/
557
#ifdef SQLITE_ENABLE_IOTRACE
558
static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
559
  va_list ap;
560
  char *z;
561
  if( iotrace==0 ) return;
562
  va_start(ap, zFormat);
563
  z = sqlite3_vmprintf(zFormat, ap);
564
  va_end(ap);
565
  utf8_printf(iotrace, "%s", z);
566
  sqlite3_free(z);
567
}
568
#endif
569
 
570
/*
571
** Output string zUtf to stream pOut as w characters.  If w is negative,
572
** then right-justify the text.  W is the width in UTF-8 characters, not
573
** in bytes.  This is different from the %*.*s specification in printf
574
** since with %*.*s the width is measured in bytes, not characters.
575
*/
576
static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
577
  int i;
578
  int n;
579
  int aw = w<0 ? -w : w;
580
  for(i=n=0; zUtf[i]; i++){
581
    if( (zUtf[i]&0xc0)!=0x80 ){
582
      n++;
583
      if( n==aw ){
584
        do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
585
        break;
586
      }
587
    }
588
  }
589
  if( n>=aw ){
590
    utf8_printf(pOut, "%.*s", i, zUtf);
591
  }else if( w<0 ){
592
    utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
593
  }else{
594
    utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
595
  }
596
}
597
 
598
 
599
/*
600
** Determines if a string is a number of not.
601
*/
602
static int isNumber(const char *z, int *realnum){
603
  if( *z=='-' || *z=='+' ) z++;
604
  if( !IsDigit(*z) ){
605
    return 0;
606
  }
607
  z++;
608
  if( realnum ) *realnum = 0;
609
  while( IsDigit(*z) ){ z++; }
610
  if( *z=='.' ){
611
    z++;
612
    if( !IsDigit(*z) ) return 0;
613
    while( IsDigit(*z) ){ z++; }
614
    if( realnum ) *realnum = 1;
615
  }
616
  if( *z=='e' || *z=='E' ){
617
    z++;
618
    if( *z=='+' || *z=='-' ) z++;
619
    if( !IsDigit(*z) ) return 0;
620
    while( IsDigit(*z) ){ z++; }
621
    if( realnum ) *realnum = 1;
622
  }
623
  return *z==0;
624
}
625
 
626
/*
627
** Compute a string length that is limited to what can be stored in
628
** lower 30 bits of a 32-bit signed integer.
629
*/
630
static int strlen30(const char *z){
631
  const char *z2 = z;
632
  while( *z2 ){ z2++; }
633
  return 0x3fffffff & (int)(z2 - z);
634
}
635
 
636
/*
637
** Return the length of a string in characters.  Multibyte UTF8 characters
638
** count as a single character.
639
*/
640
static int strlenChar(const char *z){
641
  int n = 0;
642
  while( *z ){
643
    if( (0xc0&*(z++))!=0x80 ) n++;
644
  }
645
  return n;
646
}
647
 
648
/*
649
** Return true if zFile does not exist or if it is not an ordinary file.
650
*/
651
#ifdef _WIN32
652
# define notNormalFile(X) 0
653
#else
654
static int notNormalFile(const char *zFile){
655
  struct stat x;
656
  int rc;
657
  memset(&x, 0, sizeof(x));
658
  rc = stat(zFile, &x);
659
  return rc || !S_ISREG(x.st_mode);
660
}
661
#endif
662
 
663
/*
664
** This routine reads a line of text from FILE in, stores
665
** the text in memory obtained from malloc() and returns a pointer
666
** to the text.  NULL is returned at end of file, or if malloc()
667
** fails.
668
**
669
** If zLine is not NULL then it is a malloced buffer returned from
670
** a previous call to this routine that may be reused.
671
*/
672
static char *local_getline(char *zLine, FILE *in){
673
  int nLine = zLine==0 ? 0 : 100;
674
  int n = 0;
675
 
676
  while( 1 ){
677
    if( n+100>nLine ){
678
      nLine = nLine*2 + 100;
679
      zLine = realloc(zLine, nLine);
680
      if( zLine==0 ) shell_out_of_memory();
681
    }
682
    if( fgets(&zLine[n], nLine - n, in)==0 ){
683
      if( n==0 ){
684
        free(zLine);
685
        return 0;
686
      }
687
      zLine[n] = 0;
688
      break;
689
    }
690
    while( zLine[n] ) n++;
691
    if( n>0 && zLine[n-1]=='\n' ){
692
      n--;
693
      if( n>0 && zLine[n-1]=='\r' ) n--;
694
      zLine[n] = 0;
695
      break;
696
    }
697
  }
698
#if defined(_WIN32) || defined(WIN32)
699
  /* For interactive input on Windows systems, translate the
700
  ** multi-byte characterset characters into UTF-8. */
701
  if( stdin_is_interactive && in==stdin ){
702
    char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
703
    if( zTrans ){
704
      int nTrans = strlen30(zTrans)+1;
705
      if( nTrans>nLine ){
706
        zLine = realloc(zLine, nTrans);
707
        if( zLine==0 ) shell_out_of_memory();
708
      }
709
      memcpy(zLine, zTrans, nTrans);
710
      sqlite3_free(zTrans);
711
    }
712
  }
713
#endif /* defined(_WIN32) || defined(WIN32) */
714
  return zLine;
715
}
716
 
717
/*
718
** Retrieve a single line of input text.
719
**
720
** If in==0 then read from standard input and prompt before each line.
721
** If isContinuation is true, then a continuation prompt is appropriate.
722
** If isContinuation is zero, then the main prompt should be used.
723
**
724
** If zPrior is not NULL then it is a buffer from a prior call to this
725
** routine that can be reused.
726
**
727
** The result is stored in space obtained from malloc() and must either
728
** be freed by the caller or else passed back into this routine via the
729
** zPrior argument for reuse.
730
*/
731
static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
732
  char *zPrompt;
733
  char *zResult;
734
  if( in!=0 ){
735
    zResult = local_getline(zPrior, in);
736
  }else{
737
    zPrompt = isContinuation ? continuePrompt : mainPrompt;
738
#if SHELL_USE_LOCAL_GETLINE
739
    printf("%s", zPrompt);
740
    fflush(stdout);
741
    zResult = local_getline(zPrior, stdin);
742
#else
743
    free(zPrior);
744
    zResult = shell_readline(zPrompt);
745
    if( zResult && *zResult ) shell_add_history(zResult);
746
#endif
747
  }
748
  return zResult;
749
}
750
 
751
 
752
/*
753
** Return the value of a hexadecimal digit.  Return -1 if the input
754
** is not a hex digit.
755
*/
756
static int hexDigitValue(char c){
757
  if( c>='0' && c<='9' ) return c - '0';
758
  if( c>='a' && c<='f' ) return c - 'a' + 10;
759
  if( c>='A' && c<='F' ) return c - 'A' + 10;
760
  return -1;
761
}
762
 
763
/*
764
** Interpret zArg as an integer value, possibly with suffixes.
765
*/
766
static sqlite3_int64 integerValue(const char *zArg){
767
  sqlite3_int64 v = 0;
768
  static const struct { char *zSuffix; int iMult; } aMult[] = {
769
    { "KiB", 1024 },
770
    { "MiB", 1024*1024 },
771
    { "GiB", 1024*1024*1024 },
772
    { "KB",  1000 },
773
    { "MB",  1000000 },
774
    { "GB",  1000000000 },
775
    { "K",   1000 },
776
    { "M",   1000000 },
777
    { "G",   1000000000 },
778
  };
779
  int i;
780
  int isNeg = 0;
781
  if( zArg[0]=='-' ){
782
    isNeg = 1;
783
    zArg++;
784
  }else if( zArg[0]=='+' ){
785
    zArg++;
786
  }
787
  if( zArg[0]=='0' && zArg[1]=='x' ){
788
    int x;
789
    zArg += 2;
790
    while( (x = hexDigitValue(zArg[0]))>=0 ){
791
      v = (v<<4) + x;
792
      zArg++;
793
    }
794
  }else{
795
    while( IsDigit(zArg[0]) ){
796
      v = v*10 + zArg[0] - '0';
797
      zArg++;
798
    }
799
  }
800
  for(i=0; i
801
    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
802
      v *= aMult[i].iMult;
803
      break;
804
    }
805
  }
806
  return isNeg? -v : v;
807
}
808
 
809
/*
810
** A variable length string to which one can append text.
811
*/
812
typedef struct ShellText ShellText;
813
struct ShellText {
814
  char *z;
815
  int n;
816
  int nAlloc;
817
};
818
 
819
/*
820
** Initialize and destroy a ShellText object
821
*/
822
static void initText(ShellText *p){
823
  memset(p, 0, sizeof(*p));
824
}
825
static void freeText(ShellText *p){
826
  free(p->z);
827
  initText(p);
828
}
829
 
830
/* zIn is either a pointer to a NULL-terminated string in memory obtained
831
** from malloc(), or a NULL pointer. The string pointed to by zAppend is
832
** added to zIn, and the result returned in memory obtained from malloc().
833
** zIn, if it was not NULL, is freed.
834
**
835
** If the third argument, quote, is not '\0', then it is used as a
836
** quote character for zAppend.
837
*/
838
static void appendText(ShellText *p, char const *zAppend, char quote){
839
  int len;
840
  int i;
841
  int nAppend = strlen30(zAppend);
842
 
843
  len = nAppend+p->n+1;
844
  if( quote ){
845
    len += 2;
846
    for(i=0; i
847
      if( zAppend[i]==quote ) len++;
848
    }
849
  }
850
 
851
  if( p->n+len>=p->nAlloc ){
852
    p->nAlloc = p->nAlloc*2 + len + 20;
853
    p->z = realloc(p->z, p->nAlloc);
854
    if( p->z==0 ) shell_out_of_memory();
855
  }
856
 
857
  if( quote ){
858
    char *zCsr = p->z+p->n;
859
    *zCsr++ = quote;
860
    for(i=0; i
861
      *zCsr++ = zAppend[i];
862
      if( zAppend[i]==quote ) *zCsr++ = quote;
863
    }
864
    *zCsr++ = quote;
865
    p->n = (int)(zCsr - p->z);
866
    *zCsr = '\0';
867
  }else{
868
    memcpy(p->z+p->n, zAppend, nAppend);
869
    p->n += nAppend;
870
    p->z[p->n] = '\0';
871
  }
872
}
873
 
874
/*
875
** Attempt to determine if identifier zName needs to be quoted, either
876
** because it contains non-alphanumeric characters, or because it is an
877
** SQLite keyword.  Be conservative in this estimate:  When in doubt assume
878
** that quoting is required.
879
**
880
** Return '"' if quoting is required.  Return 0 if no quoting is required.
881
*/
882
static char quoteChar(const char *zName){
883
  int i;
884
  if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
885
  for(i=0; zName[i]; i++){
886
    if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
887
  }
888
  return sqlite3_keyword_check(zName, i) ? '"' : 0;
889
}
890
 
891
/*
892
** Construct a fake object name and column list to describe the structure
893
** of the view, virtual table, or table valued function zSchema.zName.
894
*/
895
static char *shellFakeSchema(
896
  sqlite3 *db,            /* The database connection containing the vtab */
897
  const char *zSchema,    /* Schema of the database holding the vtab */
898
  const char *zName       /* The name of the virtual table */
899
){
900
  sqlite3_stmt *pStmt = 0;
901
  char *zSql;
902
  ShellText s;
903
  char cQuote;
904
  char *zDiv = "(";
905
  int nRow = 0;
906
 
907
  zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
908
                         zSchema ? zSchema : "main", zName);
909
  sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
910
  sqlite3_free(zSql);
911
  initText(&s);
912
  if( zSchema ){
913
    cQuote = quoteChar(zSchema);
914
    if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
915
    appendText(&s, zSchema, cQuote);
916
    appendText(&s, ".", 0);
917
  }
918
  cQuote = quoteChar(zName);
919
  appendText(&s, zName, cQuote);
920
  while( sqlite3_step(pStmt)==SQLITE_ROW ){
921
    const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
922
    nRow++;
923
    appendText(&s, zDiv, 0);
924
    zDiv = ",";
925
    cQuote = quoteChar(zCol);
926
    appendText(&s, zCol, cQuote);
927
  }
928
  appendText(&s, ")", 0);
929
  sqlite3_finalize(pStmt);
930
  if( nRow==0 ){
931
    freeText(&s);
932
    s.z = 0;
933
  }
934
  return s.z;
935
}
936
 
937
/*
938
** SQL function:  shell_module_schema(X)
939
**
940
** Return a fake schema for the table-valued function or eponymous virtual
941
** table X.
942
*/
943
static void shellModuleSchema(
944
  sqlite3_context *pCtx,
945
  int nVal,
946
  sqlite3_value **apVal
947
){
948
  const char *zName = (const char*)sqlite3_value_text(apVal[0]);
949
  char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
950
  UNUSED_PARAMETER(nVal);
951
  if( zFake ){
952
    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
953
                        -1, sqlite3_free);
954
    free(zFake);
955
  }
956
}
957
 
958
/*
959
** SQL function:  shell_add_schema(S,X)
960
**
961
** Add the schema name X to the CREATE statement in S and return the result.
962
** Examples:
963
**
964
**    CREATE TABLE t1(x)   ->   CREATE TABLE xyz.t1(x);
965
**
966
** Also works on
967
**
968
**    CREATE INDEX
969
**    CREATE UNIQUE INDEX
970
**    CREATE VIEW
971
**    CREATE TRIGGER
972
**    CREATE VIRTUAL TABLE
973
**
974
** This UDF is used by the .schema command to insert the schema name of
975
** attached databases into the middle of the sqlite_schema.sql field.
976
*/
977
static void shellAddSchemaName(
978
  sqlite3_context *pCtx,
979
  int nVal,
980
  sqlite3_value **apVal
981
){
982
  static const char *aPrefix[] = {
983
     "TABLE",
984
     "INDEX",
985
     "UNIQUE INDEX",
986
     "VIEW",
987
     "TRIGGER",
988
     "VIRTUAL TABLE"
989
  };
990
  int i = 0;
991
  const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
992
  const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
993
  const char *zName = (const char*)sqlite3_value_text(apVal[2]);
994
  sqlite3 *db = sqlite3_context_db_handle(pCtx);
995
  UNUSED_PARAMETER(nVal);
996
  if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
997
    for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
998
      int n = strlen30(aPrefix[i]);
999
      if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
1000
        char *z = 0;
1001
        char *zFake = 0;
1002
        if( zSchema ){
1003
          char cQuote = quoteChar(zSchema);
1004
          if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1005
            z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1006
          }else{
1007
            z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1008
          }
1009
        }
1010
        if( zName
1011
         && aPrefix[i][0]=='V'
1012
         && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1013
        ){
1014
          if( z==0 ){
1015
            z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1016
          }else{
1017
            z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1018
          }
1019
          free(zFake);
1020
        }
1021
        if( z ){
1022
          sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1023
          return;
1024
        }
1025
      }
1026
    }
1027
  }
1028
  sqlite3_result_value(pCtx, apVal[0]);
1029
}
1030
 
1031
/*
1032
** The source code for several run-time loadable extensions is inserted
1033
** below by the ../tool/mkshellc.tcl script.  Before processing that included
1034
** code, we need to override some macros to make the included program code
1035
** work here in the middle of this regular program.
1036
*/
1037
#define SQLITE_EXTENSION_INIT1
1038
#define SQLITE_EXTENSION_INIT2(X) (void)(X)
1039
 
1040
#if defined(_WIN32) && defined(_MSC_VER)
1041
/************************* Begin test_windirent.h ******************/
1042
/*
1043
** 2015 November 30
1044
**
1045
** The author disclaims copyright to this source code.  In place of
1046
** a legal notice, here is a blessing:
1047
**
1048
**    May you do good and not evil.
1049
**    May you find forgiveness for yourself and forgive others.
1050
**    May you share freely, never taking more than you give.
1051
**
1052
*************************************************************************
1053
** This file contains declarations for most of the opendir() family of
1054
** POSIX functions on Win32 using the MSVCRT.
1055
*/
1056
 
1057
#if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1058
#define SQLITE_WINDIRENT_H
1059
 
1060
/*
1061
** We need several data types from the Windows SDK header.
1062
*/
1063
 
1064
#ifndef WIN32_LEAN_AND_MEAN
1065
#define WIN32_LEAN_AND_MEAN
1066
#endif
1067
 
1068
//#include "windows.h"
1069
 
1070
/*
1071
** We need several support functions from the SQLite core.
1072
*/
1073
 
1074
/* #include "sqlite3.h" */
1075
 
1076
/*
1077
** We need several things from the ANSI and MSVCRT headers.
1078
*/
1079
 
1080
#include 
1081
#include 
1082
#include 
1083
#include 
1084
#include 
1085
#include 
1086
#include 
1087
 
1088
/*
1089
** We may need several defines that should have been in "sys/stat.h".
1090
*/
1091
 
1092
#ifndef S_ISREG
1093
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1094
#endif
1095
 
1096
#ifndef S_ISDIR
1097
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1098
#endif
1099
 
1100
#ifndef S_ISLNK
1101
#define S_ISLNK(mode) (0)
1102
#endif
1103
 
1104
/*
1105
** We may need to provide the "mode_t" type.
1106
*/
1107
 
1108
#ifndef MODE_T_DEFINED
1109
  #define MODE_T_DEFINED
1110
  typedef unsigned short mode_t;
1111
#endif
1112
 
1113
/*
1114
** We may need to provide the "ino_t" type.
1115
*/
1116
 
1117
#ifndef INO_T_DEFINED
1118
  #define INO_T_DEFINED
1119
  typedef unsigned short ino_t;
1120
#endif
1121
 
1122
/*
1123
** We need to define "NAME_MAX" if it was not present in "limits.h".
1124
*/
1125
 
1126
#ifndef NAME_MAX
1127
#  ifdef FILENAME_MAX
1128
#    define NAME_MAX (FILENAME_MAX)
1129
#  else
1130
#    define NAME_MAX (260)
1131
#  endif
1132
#endif
1133
 
1134
/*
1135
** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1136
*/
1137
 
1138
#ifndef NULL_INTPTR_T
1139
#  define NULL_INTPTR_T ((intptr_t)(0))
1140
#endif
1141
 
1142
#ifndef BAD_INTPTR_T
1143
#  define BAD_INTPTR_T ((intptr_t)(-1))
1144
#endif
1145
 
1146
/*
1147
** We need to provide the necessary structures and related types.
1148
*/
1149
 
1150
#ifndef DIRENT_DEFINED
1151
#define DIRENT_DEFINED
1152
typedef struct DIRENT DIRENT;
1153
typedef DIRENT *LPDIRENT;
1154
struct DIRENT {
1155
  ino_t d_ino;               /* Sequence number, do not use. */
1156
  unsigned d_attributes;     /* Win32 file attributes. */
1157
  char d_name[NAME_MAX + 1]; /* Name within the directory. */
1158
};
1159
#endif
1160
 
1161
#ifndef DIR_DEFINED
1162
#define DIR_DEFINED
1163
typedef struct DIR DIR;
1164
typedef DIR *LPDIR;
1165
struct DIR {
1166
  intptr_t d_handle; /* Value returned by "_findfirst". */
1167
  DIRENT d_first;    /* DIRENT constructed based on "_findfirst". */
1168
  DIRENT d_next;     /* DIRENT constructed based on "_findnext". */
1169
};
1170
#endif
1171
 
1172
/*
1173
** Provide a macro, for use by the implementation, to determine if a
1174
** particular directory entry should be skipped over when searching for
1175
** the next directory entry that should be returned by the readdir() or
1176
** readdir_r() functions.
1177
*/
1178
 
1179
#ifndef is_filtered
1180
#  define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1181
#endif
1182
 
1183
/*
1184
** Provide the function prototype for the POSIX compatiable getenv()
1185
** function.  This function is not thread-safe.
1186
*/
1187
 
1188
extern const char *windirent_getenv(const char *name);
1189
 
1190
/*
1191
** Finally, we can provide the function prototypes for the opendir(),
1192
** readdir(), readdir_r(), and closedir() POSIX functions.
1193
*/
1194
 
1195
extern LPDIR opendir(const char *dirname);
1196
extern LPDIRENT readdir(LPDIR dirp);
1197
extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1198
extern INT closedir(LPDIR dirp);
1199
 
1200
#endif /* defined(WIN32) && defined(_MSC_VER) */
1201
 
1202
/************************* End test_windirent.h ********************/
1203
/************************* Begin test_windirent.c ******************/
1204
/*
1205
** 2015 November 30
1206
**
1207
** The author disclaims copyright to this source code.  In place of
1208
** a legal notice, here is a blessing:
1209
**
1210
**    May you do good and not evil.
1211
**    May you find forgiveness for yourself and forgive others.
1212
**    May you share freely, never taking more than you give.
1213
**
1214
*************************************************************************
1215
** This file contains code to implement most of the opendir() family of
1216
** POSIX functions on Win32 using the MSVCRT.
1217
*/
1218
 
1219
#if defined(_WIN32) && defined(_MSC_VER)
1220
/* #include "test_windirent.h" */
1221
 
1222
/*
1223
** Implementation of the POSIX getenv() function using the Win32 API.
1224
** This function is not thread-safe.
1225
*/
1226
const char *windirent_getenv(
1227
  const char *name
1228
){
1229
  static char value[32768]; /* Maximum length, per MSDN */
1230
  DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1231
  DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1232
 
1233
  memset(value, 0, sizeof(value));
1234
  dwRet = GetEnvironmentVariableA(name, value, dwSize);
1235
  if( dwRet==0 || dwRet>dwSize ){
1236
    /*
1237
    ** The function call to GetEnvironmentVariableA() failed -OR-
1238
    ** the buffer is not large enough.  Either way, return NULL.
1239
    */
1240
    return 0;
1241
  }else{
1242
    /*
1243
    ** The function call to GetEnvironmentVariableA() succeeded
1244
    ** -AND- the buffer contains the entire value.
1245
    */
1246
    return value;
1247
  }
1248
}
1249
 
1250
/*
1251
** Implementation of the POSIX opendir() function using the MSVCRT.
1252
*/
1253
LPDIR opendir(
1254
  const char *dirname
1255
){
1256
  struct _finddata_t data;
1257
  LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1258
  SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1259
 
1260
  if( dirp==NULL ) return NULL;
1261
  memset(dirp, 0, sizeof(DIR));
1262
 
1263
  /* TODO: Remove this if Unix-style root paths are not used. */
1264
  if( sqlite3_stricmp(dirname, "/")==0 ){
1265
    dirname = windirent_getenv("SystemDrive");
1266
  }
1267
 
1268
  memset(&data, 0, sizeof(struct _finddata_t));
1269
  _snprintf(data.name, namesize, "%s\\*", dirname);
1270
  dirp->d_handle = _findfirst(data.name, &data);
1271
 
1272
  if( dirp->d_handle==BAD_INTPTR_T ){
1273
    closedir(dirp);
1274
    return NULL;
1275
  }
1276
 
1277
  /* TODO: Remove this block to allow hidden and/or system files. */
1278
  if( is_filtered(data) ){
1279
next:
1280
 
1281
    memset(&data, 0, sizeof(struct _finddata_t));
1282
    if( _findnext(dirp->d_handle, &data)==-1 ){
1283
      closedir(dirp);
1284
      return NULL;
1285
    }
1286
 
1287
    /* TODO: Remove this block to allow hidden and/or system files. */
1288
    if( is_filtered(data) ) goto next;
1289
  }
1290
 
1291
  dirp->d_first.d_attributes = data.attrib;
1292
  strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1293
  dirp->d_first.d_name[NAME_MAX] = '\0';
1294
 
1295
  return dirp;
1296
}
1297
 
1298
/*
1299
** Implementation of the POSIX readdir() function using the MSVCRT.
1300
*/
1301
LPDIRENT readdir(
1302
  LPDIR dirp
1303
){
1304
  struct _finddata_t data;
1305
 
1306
  if( dirp==NULL ) return NULL;
1307
 
1308
  if( dirp->d_first.d_ino==0 ){
1309
    dirp->d_first.d_ino++;
1310
    dirp->d_next.d_ino++;
1311
 
1312
    return &dirp->d_first;
1313
  }
1314
 
1315
next:
1316
 
1317
  memset(&data, 0, sizeof(struct _finddata_t));
1318
  if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1319
 
1320
  /* TODO: Remove this block to allow hidden and/or system files. */
1321
  if( is_filtered(data) ) goto next;
1322
 
1323
  dirp->d_next.d_ino++;
1324
  dirp->d_next.d_attributes = data.attrib;
1325
  strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1326
  dirp->d_next.d_name[NAME_MAX] = '\0';
1327
 
1328
  return &dirp->d_next;
1329
}
1330
 
1331
/*
1332
** Implementation of the POSIX readdir_r() function using the MSVCRT.
1333
*/
1334
INT readdir_r(
1335
  LPDIR dirp,
1336
  LPDIRENT entry,
1337
  LPDIRENT *result
1338
){
1339
  struct _finddata_t data;
1340
 
1341
  if( dirp==NULL ) return EBADF;
1342
 
1343
  if( dirp->d_first.d_ino==0 ){
1344
    dirp->d_first.d_ino++;
1345
    dirp->d_next.d_ino++;
1346
 
1347
    entry->d_ino = dirp->d_first.d_ino;
1348
    entry->d_attributes = dirp->d_first.d_attributes;
1349
    strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1350
    entry->d_name[NAME_MAX] = '\0';
1351
 
1352
    *result = entry;
1353
    return 0;
1354
  }
1355
 
1356
next:
1357
 
1358
  memset(&data, 0, sizeof(struct _finddata_t));
1359
  if( _findnext(dirp->d_handle, &data)==-1 ){
1360
    *result = NULL;
1361
    return ENOENT;
1362
  }
1363
 
1364
  /* TODO: Remove this block to allow hidden and/or system files. */
1365
  if( is_filtered(data) ) goto next;
1366
 
1367
  entry->d_ino = (ino_t)-1; /* not available */
1368
  entry->d_attributes = data.attrib;
1369
  strncpy(entry->d_name, data.name, NAME_MAX);
1370
  entry->d_name[NAME_MAX] = '\0';
1371
 
1372
  *result = entry;
1373
  return 0;
1374
}
1375
 
1376
/*
1377
** Implementation of the POSIX closedir() function using the MSVCRT.
1378
*/
1379
INT closedir(
1380
  LPDIR dirp
1381
){
1382
  INT result = 0;
1383
 
1384
  if( dirp==NULL ) return EINVAL;
1385
 
1386
  if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1387
    result = _findclose(dirp->d_handle);
1388
  }
1389
 
1390
  sqlite3_free(dirp);
1391
  return result;
1392
}
1393
 
1394
#endif /* defined(WIN32) && defined(_MSC_VER) */
1395
 
1396
/************************* End test_windirent.c ********************/
1397
#define dirent DIRENT
1398
#endif
1399
/************************* Begin ../ext/misc/shathree.c ******************/
1400
/*
1401
** 2017-03-08
1402
**
1403
** The author disclaims copyright to this source code.  In place of
1404
** a legal notice, here is a blessing:
1405
**
1406
**    May you do good and not evil.
1407
**    May you find forgiveness for yourself and forgive others.
1408
**    May you share freely, never taking more than you give.
1409
**
1410
******************************************************************************
1411
**
1412
** This SQLite extension implements functions that compute SHA3 hashes.
1413
** Two SQL functions are implemented:
1414
**
1415
**     sha3(X,SIZE)
1416
**     sha3_query(Y,SIZE)
1417
**
1418
** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1419
** X is NULL.
1420
**
1421
** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1422
** and returns a hash of their results.
1423
**
1424
** The SIZE argument is optional.  If omitted, the SHA3-256 hash algorithm
1425
** is used.  If SIZE is included it must be one of the integers 224, 256,
1426
** 384, or 512, to determine SHA3 hash variant that is computed.
1427
*/
1428
/* #include "sqlite3ext.h" */
1429
SQLITE_EXTENSION_INIT1
1430
#include 
1431
#include 
1432
#include 
1433
 
1434
#ifndef SQLITE_AMALGAMATION
1435
/* typedef sqlite3_uint64 u64; */
1436
#endif /* SQLITE_AMALGAMATION */
1437
 
1438
/******************************************************************************
1439
** The Hash Engine
1440
*/
1441
/*
1442
** Macros to determine whether the machine is big or little endian,
1443
** and whether or not that determination is run-time or compile-time.
1444
**
1445
** For best performance, an attempt is made to guess at the byte-order
1446
** using C-preprocessor macros.  If that is unsuccessful, or if
1447
** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1448
** at run-time.
1449
*/
1450
#ifndef SHA3_BYTEORDER
1451
# if defined(i386)     || defined(__i386__)   || defined(_M_IX86) ||    \
1452
     defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)  ||    \
1453
     defined(_M_AMD64) || defined(_M_ARM)     || defined(__x86)   ||    \
1454
     defined(__arm__)
1455
#   define SHA3_BYTEORDER    1234
1456
# elif defined(sparc)    || defined(__ppc__)
1457
#   define SHA3_BYTEORDER    4321
1458
# else
1459
#   define SHA3_BYTEORDER 0
1460
# endif
1461
#endif
1462
 
1463
 
1464
/*
1465
** State structure for a SHA3 hash in progress
1466
*/
1467
typedef struct SHA3Context SHA3Context;
1468
struct SHA3Context {
1469
  union {
1470
    u64 s[25];                /* Keccak state. 5x5 lines of 64 bits each */
1471
    unsigned char x[1600];    /* ... or 1600 bytes */
1472
  } u;
1473
  unsigned nRate;        /* Bytes of input accepted per Keccak iteration */
1474
  unsigned nLoaded;      /* Input bytes loaded into u.x[] so far this cycle */
1475
  unsigned ixMask;       /* Insert next input into u.x[nLoaded^ixMask]. */
1476
};
1477
 
1478
/*
1479
** A single step of the Keccak mixing function for a 1600-bit state
1480
*/
1481
static void KeccakF1600Step(SHA3Context *p){
1482
  int i;
1483
  u64 b0, b1, b2, b3, b4;
1484
  u64 c0, c1, c2, c3, c4;
1485
  u64 d0, d1, d2, d3, d4;
1486
  static const u64 RC[] = {
1487
    0x0000000000000001ULL,  0x0000000000008082ULL,
1488
    0x800000000000808aULL,  0x8000000080008000ULL,
1489
    0x000000000000808bULL,  0x0000000080000001ULL,
1490
    0x8000000080008081ULL,  0x8000000000008009ULL,
1491
    0x000000000000008aULL,  0x0000000000000088ULL,
1492
    0x0000000080008009ULL,  0x000000008000000aULL,
1493
    0x000000008000808bULL,  0x800000000000008bULL,
1494
    0x8000000000008089ULL,  0x8000000000008003ULL,
1495
    0x8000000000008002ULL,  0x8000000000000080ULL,
1496
    0x000000000000800aULL,  0x800000008000000aULL,
1497
    0x8000000080008081ULL,  0x8000000000008080ULL,
1498
    0x0000000080000001ULL,  0x8000000080008008ULL
1499
  };
1500
# define a00 (p->u.s[0])
1501
# define a01 (p->u.s[1])
1502
# define a02 (p->u.s[2])
1503
# define a03 (p->u.s[3])
1504
# define a04 (p->u.s[4])
1505
# define a10 (p->u.s[5])
1506
# define a11 (p->u.s[6])
1507
# define a12 (p->u.s[7])
1508
# define a13 (p->u.s[8])
1509
# define a14 (p->u.s[9])
1510
# define a20 (p->u.s[10])
1511
# define a21 (p->u.s[11])
1512
# define a22 (p->u.s[12])
1513
# define a23 (p->u.s[13])
1514
# define a24 (p->u.s[14])
1515
# define a30 (p->u.s[15])
1516
# define a31 (p->u.s[16])
1517
# define a32 (p->u.s[17])
1518
# define a33 (p->u.s[18])
1519
# define a34 (p->u.s[19])
1520
# define a40 (p->u.s[20])
1521
# define a41 (p->u.s[21])
1522
# define a42 (p->u.s[22])
1523
# define a43 (p->u.s[23])
1524
# define a44 (p->u.s[24])
1525
# define ROL64(a,x) ((a<>(64-x)))
1526
 
1527
  for(i=0; i<24; i+=4){
1528
    c0 = a00^a10^a20^a30^a40;
1529
    c1 = a01^a11^a21^a31^a41;
1530
    c2 = a02^a12^a22^a32^a42;
1531
    c3 = a03^a13^a23^a33^a43;
1532
    c4 = a04^a14^a24^a34^a44;
1533
    d0 = c4^ROL64(c1, 1);
1534
    d1 = c0^ROL64(c2, 1);
1535
    d2 = c1^ROL64(c3, 1);
1536
    d3 = c2^ROL64(c4, 1);
1537
    d4 = c3^ROL64(c0, 1);
1538
 
1539
    b0 = (a00^d0);
1540
    b1 = ROL64((a11^d1), 44);
1541
    b2 = ROL64((a22^d2), 43);
1542
    b3 = ROL64((a33^d3), 21);
1543
    b4 = ROL64((a44^d4), 14);
1544
    a00 =   b0 ^((~b1)&  b2 );
1545
    a00 ^= RC[i];
1546
    a11 =   b1 ^((~b2)&  b3 );
1547
    a22 =   b2 ^((~b3)&  b4 );
1548
    a33 =   b3 ^((~b4)&  b0 );
1549
    a44 =   b4 ^((~b0)&  b1 );
1550
 
1551
    b2 = ROL64((a20^d0), 3);
1552
    b3 = ROL64((a31^d1), 45);
1553
    b4 = ROL64((a42^d2), 61);
1554
    b0 = ROL64((a03^d3), 28);
1555
    b1 = ROL64((a14^d4), 20);
1556
    a20 =   b0 ^((~b1)&  b2 );
1557
    a31 =   b1 ^((~b2)&  b3 );
1558
    a42 =   b2 ^((~b3)&  b4 );
1559
    a03 =   b3 ^((~b4)&  b0 );
1560
    a14 =   b4 ^((~b0)&  b1 );
1561
 
1562
    b4 = ROL64((a40^d0), 18);
1563
    b0 = ROL64((a01^d1), 1);
1564
    b1 = ROL64((a12^d2), 6);
1565
    b2 = ROL64((a23^d3), 25);
1566
    b3 = ROL64((a34^d4), 8);
1567
    a40 =   b0 ^((~b1)&  b2 );
1568
    a01 =   b1 ^((~b2)&  b3 );
1569
    a12 =   b2 ^((~b3)&  b4 );
1570
    a23 =   b3 ^((~b4)&  b0 );
1571
    a34 =   b4 ^((~b0)&  b1 );
1572
 
1573
    b1 = ROL64((a10^d0), 36);
1574
    b2 = ROL64((a21^d1), 10);
1575
    b3 = ROL64((a32^d2), 15);
1576
    b4 = ROL64((a43^d3), 56);
1577
    b0 = ROL64((a04^d4), 27);
1578
    a10 =   b0 ^((~b1)&  b2 );
1579
    a21 =   b1 ^((~b2)&  b3 );
1580
    a32 =   b2 ^((~b3)&  b4 );
1581
    a43 =   b3 ^((~b4)&  b0 );
1582
    a04 =   b4 ^((~b0)&  b1 );
1583
 
1584
    b3 = ROL64((a30^d0), 41);
1585
    b4 = ROL64((a41^d1), 2);
1586
    b0 = ROL64((a02^d2), 62);
1587
    b1 = ROL64((a13^d3), 55);
1588
    b2 = ROL64((a24^d4), 39);
1589
    a30 =   b0 ^((~b1)&  b2 );
1590
    a41 =   b1 ^((~b2)&  b3 );
1591
    a02 =   b2 ^((~b3)&  b4 );
1592
    a13 =   b3 ^((~b4)&  b0 );
1593
    a24 =   b4 ^((~b0)&  b1 );
1594
 
1595
    c0 = a00^a20^a40^a10^a30;
1596
    c1 = a11^a31^a01^a21^a41;
1597
    c2 = a22^a42^a12^a32^a02;
1598
    c3 = a33^a03^a23^a43^a13;
1599
    c4 = a44^a14^a34^a04^a24;
1600
    d0 = c4^ROL64(c1, 1);
1601
    d1 = c0^ROL64(c2, 1);
1602
    d2 = c1^ROL64(c3, 1);
1603
    d3 = c2^ROL64(c4, 1);
1604
    d4 = c3^ROL64(c0, 1);
1605
 
1606
    b0 = (a00^d0);
1607
    b1 = ROL64((a31^d1), 44);
1608
    b2 = ROL64((a12^d2), 43);
1609
    b3 = ROL64((a43^d3), 21);
1610
    b4 = ROL64((a24^d4), 14);
1611
    a00 =   b0 ^((~b1)&  b2 );
1612
    a00 ^= RC[i+1];
1613
    a31 =   b1 ^((~b2)&  b3 );
1614
    a12 =   b2 ^((~b3)&  b4 );
1615
    a43 =   b3 ^((~b4)&  b0 );
1616
    a24 =   b4 ^((~b0)&  b1 );
1617
 
1618
    b2 = ROL64((a40^d0), 3);
1619
    b3 = ROL64((a21^d1), 45);
1620
    b4 = ROL64((a02^d2), 61);
1621
    b0 = ROL64((a33^d3), 28);
1622
    b1 = ROL64((a14^d4), 20);
1623
    a40 =   b0 ^((~b1)&  b2 );
1624
    a21 =   b1 ^((~b2)&  b3 );
1625
    a02 =   b2 ^((~b3)&  b4 );
1626
    a33 =   b3 ^((~b4)&  b0 );
1627
    a14 =   b4 ^((~b0)&  b1 );
1628
 
1629
    b4 = ROL64((a30^d0), 18);
1630
    b0 = ROL64((a11^d1), 1);
1631
    b1 = ROL64((a42^d2), 6);
1632
    b2 = ROL64((a23^d3), 25);
1633
    b3 = ROL64((a04^d4), 8);
1634
    a30 =   b0 ^((~b1)&  b2 );
1635
    a11 =   b1 ^((~b2)&  b3 );
1636
    a42 =   b2 ^((~b3)&  b4 );
1637
    a23 =   b3 ^((~b4)&  b0 );
1638
    a04 =   b4 ^((~b0)&  b1 );
1639
 
1640
    b1 = ROL64((a20^d0), 36);
1641
    b2 = ROL64((a01^d1), 10);
1642
    b3 = ROL64((a32^d2), 15);
1643
    b4 = ROL64((a13^d3), 56);
1644
    b0 = ROL64((a44^d4), 27);
1645
    a20 =   b0 ^((~b1)&  b2 );
1646
    a01 =   b1 ^((~b2)&  b3 );
1647
    a32 =   b2 ^((~b3)&  b4 );
1648
    a13 =   b3 ^((~b4)&  b0 );
1649
    a44 =   b4 ^((~b0)&  b1 );
1650
 
1651
    b3 = ROL64((a10^d0), 41);
1652
    b4 = ROL64((a41^d1), 2);
1653
    b0 = ROL64((a22^d2), 62);
1654
    b1 = ROL64((a03^d3), 55);
1655
    b2 = ROL64((a34^d4), 39);
1656
    a10 =   b0 ^((~b1)&  b2 );
1657
    a41 =   b1 ^((~b2)&  b3 );
1658
    a22 =   b2 ^((~b3)&  b4 );
1659
    a03 =   b3 ^((~b4)&  b0 );
1660
    a34 =   b4 ^((~b0)&  b1 );
1661
 
1662
    c0 = a00^a40^a30^a20^a10;
1663
    c1 = a31^a21^a11^a01^a41;
1664
    c2 = a12^a02^a42^a32^a22;
1665
    c3 = a43^a33^a23^a13^a03;
1666
    c4 = a24^a14^a04^a44^a34;
1667
    d0 = c4^ROL64(c1, 1);
1668
    d1 = c0^ROL64(c2, 1);
1669
    d2 = c1^ROL64(c3, 1);
1670
    d3 = c2^ROL64(c4, 1);
1671
    d4 = c3^ROL64(c0, 1);
1672
 
1673
    b0 = (a00^d0);
1674
    b1 = ROL64((a21^d1), 44);
1675
    b2 = ROL64((a42^d2), 43);
1676
    b3 = ROL64((a13^d3), 21);
1677
    b4 = ROL64((a34^d4), 14);
1678
    a00 =   b0 ^((~b1)&  b2 );
1679
    a00 ^= RC[i+2];
1680
    a21 =   b1 ^((~b2)&  b3 );
1681
    a42 =   b2 ^((~b3)&  b4 );
1682
    a13 =   b3 ^((~b4)&  b0 );
1683
    a34 =   b4 ^((~b0)&  b1 );
1684
 
1685
    b2 = ROL64((a30^d0), 3);
1686
    b3 = ROL64((a01^d1), 45);
1687
    b4 = ROL64((a22^d2), 61);
1688
    b0 = ROL64((a43^d3), 28);
1689
    b1 = ROL64((a14^d4), 20);
1690
    a30 =   b0 ^((~b1)&  b2 );
1691
    a01 =   b1 ^((~b2)&  b3 );
1692
    a22 =   b2 ^((~b3)&  b4 );
1693
    a43 =   b3 ^((~b4)&  b0 );
1694
    a14 =   b4 ^((~b0)&  b1 );
1695
 
1696
    b4 = ROL64((a10^d0), 18);
1697
    b0 = ROL64((a31^d1), 1);
1698
    b1 = ROL64((a02^d2), 6);
1699
    b2 = ROL64((a23^d3), 25);
1700
    b3 = ROL64((a44^d4), 8);
1701
    a10 =   b0 ^((~b1)&  b2 );
1702
    a31 =   b1 ^((~b2)&  b3 );
1703
    a02 =   b2 ^((~b3)&  b4 );
1704
    a23 =   b3 ^((~b4)&  b0 );
1705
    a44 =   b4 ^((~b0)&  b1 );
1706
 
1707
    b1 = ROL64((a40^d0), 36);
1708
    b2 = ROL64((a11^d1), 10);
1709
    b3 = ROL64((a32^d2), 15);
1710
    b4 = ROL64((a03^d3), 56);
1711
    b0 = ROL64((a24^d4), 27);
1712
    a40 =   b0 ^((~b1)&  b2 );
1713
    a11 =   b1 ^((~b2)&  b3 );
1714
    a32 =   b2 ^((~b3)&  b4 );
1715
    a03 =   b3 ^((~b4)&  b0 );
1716
    a24 =   b4 ^((~b0)&  b1 );
1717
 
1718
    b3 = ROL64((a20^d0), 41);
1719
    b4 = ROL64((a41^d1), 2);
1720
    b0 = ROL64((a12^d2), 62);
1721
    b1 = ROL64((a33^d3), 55);
1722
    b2 = ROL64((a04^d4), 39);
1723
    a20 =   b0 ^((~b1)&  b2 );
1724
    a41 =   b1 ^((~b2)&  b3 );
1725
    a12 =   b2 ^((~b3)&  b4 );
1726
    a33 =   b3 ^((~b4)&  b0 );
1727
    a04 =   b4 ^((~b0)&  b1 );
1728
 
1729
    c0 = a00^a30^a10^a40^a20;
1730
    c1 = a21^a01^a31^a11^a41;
1731
    c2 = a42^a22^a02^a32^a12;
1732
    c3 = a13^a43^a23^a03^a33;
1733
    c4 = a34^a14^a44^a24^a04;
1734
    d0 = c4^ROL64(c1, 1);
1735
    d1 = c0^ROL64(c2, 1);
1736
    d2 = c1^ROL64(c3, 1);
1737
    d3 = c2^ROL64(c4, 1);
1738
    d4 = c3^ROL64(c0, 1);
1739
 
1740
    b0 = (a00^d0);
1741
    b1 = ROL64((a01^d1), 44);
1742
    b2 = ROL64((a02^d2), 43);
1743
    b3 = ROL64((a03^d3), 21);
1744
    b4 = ROL64((a04^d4), 14);
1745
    a00 =   b0 ^((~b1)&  b2 );
1746
    a00 ^= RC[i+3];
1747
    a01 =   b1 ^((~b2)&  b3 );
1748
    a02 =   b2 ^((~b3)&  b4 );
1749
    a03 =   b3 ^((~b4)&  b0 );
1750
    a04 =   b4 ^((~b0)&  b1 );
1751
 
1752
    b2 = ROL64((a10^d0), 3);
1753
    b3 = ROL64((a11^d1), 45);
1754
    b4 = ROL64((a12^d2), 61);
1755
    b0 = ROL64((a13^d3), 28);
1756
    b1 = ROL64((a14^d4), 20);
1757
    a10 =   b0 ^((~b1)&  b2 );
1758
    a11 =   b1 ^((~b2)&  b3 );
1759
    a12 =   b2 ^((~b3)&  b4 );
1760
    a13 =   b3 ^((~b4)&  b0 );
1761
    a14 =   b4 ^((~b0)&  b1 );
1762
 
1763
    b4 = ROL64((a20^d0), 18);
1764
    b0 = ROL64((a21^d1), 1);
1765
    b1 = ROL64((a22^d2), 6);
1766
    b2 = ROL64((a23^d3), 25);
1767
    b3 = ROL64((a24^d4), 8);
1768
    a20 =   b0 ^((~b1)&  b2 );
1769
    a21 =   b1 ^((~b2)&  b3 );
1770
    a22 =   b2 ^((~b3)&  b4 );
1771
    a23 =   b3 ^((~b4)&  b0 );
1772
    a24 =   b4 ^((~b0)&  b1 );
1773
 
1774
    b1 = ROL64((a30^d0), 36);
1775
    b2 = ROL64((a31^d1), 10);
1776
    b3 = ROL64((a32^d2), 15);
1777
    b4 = ROL64((a33^d3), 56);
1778
    b0 = ROL64((a34^d4), 27);
1779
    a30 =   b0 ^((~b1)&  b2 );
1780
    a31 =   b1 ^((~b2)&  b3 );
1781
    a32 =   b2 ^((~b3)&  b4 );
1782
    a33 =   b3 ^((~b4)&  b0 );
1783
    a34 =   b4 ^((~b0)&  b1 );
1784
 
1785
    b3 = ROL64((a40^d0), 41);
1786
    b4 = ROL64((a41^d1), 2);
1787
    b0 = ROL64((a42^d2), 62);
1788
    b1 = ROL64((a43^d3), 55);
1789
    b2 = ROL64((a44^d4), 39);
1790
    a40 =   b0 ^((~b1)&  b2 );
1791
    a41 =   b1 ^((~b2)&  b3 );
1792
    a42 =   b2 ^((~b3)&  b4 );
1793
    a43 =   b3 ^((~b4)&  b0 );
1794
    a44 =   b4 ^((~b0)&  b1 );
1795
  }
1796
}
1797
 
1798
/*
1799
** Initialize a new hash.  iSize determines the size of the hash
1800
** in bits and should be one of 224, 256, 384, or 512.  Or iSize
1801
** can be zero to use the default hash size of 256 bits.
1802
*/
1803
static void SHA3Init(SHA3Context *p, int iSize){
1804
  memset(p, 0, sizeof(*p));
1805
  if( iSize>=128 && iSize<=512 ){
1806
    p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1807
  }else{
1808
    p->nRate = (1600 - 2*256)/8;
1809
  }
1810
#if SHA3_BYTEORDER==1234
1811
  /* Known to be little-endian at compile-time. No-op */
1812
#elif SHA3_BYTEORDER==4321
1813
  p->ixMask = 7;  /* Big-endian */
1814
#else
1815
  {
1816
    static unsigned int one = 1;
1817
    if( 1==*(unsigned char*)&one ){
1818
      /* Little endian.  No byte swapping. */
1819
      p->ixMask = 0;
1820
    }else{
1821
      /* Big endian.  Byte swap. */
1822
      p->ixMask = 7;
1823
    }
1824
  }
1825
#endif
1826
}
1827
 
1828
/*
1829
** Make consecutive calls to the SHA3Update function to add new content
1830
** to the hash
1831
*/
1832
static void SHA3Update(
1833
  SHA3Context *p,
1834
  const unsigned char *aData,
1835
  unsigned int nData
1836
){
1837
  unsigned int i = 0;
1838
#if SHA3_BYTEORDER==1234
1839
  if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1840
    for(; i+7
1841
      p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1842
      p->nLoaded += 8;
1843
      if( p->nLoaded>=p->nRate ){
1844
        KeccakF1600Step(p);
1845
        p->nLoaded = 0;
1846
      }
1847
    }
1848
  }
1849
#endif
1850
  for(; i
1851
#if SHA3_BYTEORDER==1234
1852
    p->u.x[p->nLoaded] ^= aData[i];
1853
#elif SHA3_BYTEORDER==4321
1854
    p->u.x[p->nLoaded^0x07] ^= aData[i];
1855
#else
1856
    p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1857
#endif
1858
    p->nLoaded++;
1859
    if( p->nLoaded==p->nRate ){
1860
      KeccakF1600Step(p);
1861
      p->nLoaded = 0;
1862
    }
1863
  }
1864
}
1865
 
1866
/*
1867
** After all content has been added, invoke SHA3Final() to compute
1868
** the final hash.  The function returns a pointer to the binary
1869
** hash value.
1870
*/
1871
static unsigned char *SHA3Final(SHA3Context *p){
1872
  unsigned int i;
1873
  if( p->nLoaded==p->nRate-1 ){
1874
    const unsigned char c1 = 0x86;
1875
    SHA3Update(p, &c1, 1);
1876
  }else{
1877
    const unsigned char c2 = 0x06;
1878
    const unsigned char c3 = 0x80;
1879
    SHA3Update(p, &c2, 1);
1880
    p->nLoaded = p->nRate - 1;
1881
    SHA3Update(p, &c3, 1);
1882
  }
1883
  for(i=0; inRate; i++){
1884
    p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1885
  }
1886
  return &p->u.x[p->nRate];
1887
}
1888
/* End of the hashing logic
1889
*****************************************************************************/
1890
 
1891
/*
1892
** Implementation of the sha3(X,SIZE) function.
1893
**
1894
** Return a BLOB which is the SIZE-bit SHA3 hash of X.  The default
1895
** size is 256.  If X is a BLOB, it is hashed as is.
1896
** For all other non-NULL types of input, X is converted into a UTF-8 string
1897
** and the string is hashed without the trailing 0x00 terminator.  The hash
1898
** of a NULL value is NULL.
1899
*/
1900
static void sha3Func(
1901
  sqlite3_context *context,
1902
  int argc,
1903
  sqlite3_value **argv
1904
){
1905
  SHA3Context cx;
1906
  int eType = sqlite3_value_type(argv[0]);
1907
  int nByte = sqlite3_value_bytes(argv[0]);
1908
  int iSize;
1909
  if( argc==1 ){
1910
    iSize = 256;
1911
  }else{
1912
    iSize = sqlite3_value_int(argv[1]);
1913
    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1914
      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1915
                                    "384 512", -1);
1916
      return;
1917
    }
1918
  }
1919
  if( eType==SQLITE_NULL ) return;
1920
  SHA3Init(&cx, iSize);
1921
  if( eType==SQLITE_BLOB ){
1922
    SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1923
  }else{
1924
    SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1925
  }
1926
  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1927
}
1928
 
1929
/* Compute a string using sqlite3_vsnprintf() with a maximum length
1930
** of 50 bytes and add it to the hash.
1931
*/
1932
static void hash_step_vformat(
1933
  SHA3Context *p,                 /* Add content to this context */
1934
  const char *zFormat,
1935
  ...
1936
){
1937
  va_list ap;
1938
  int n;
1939
  char zBuf[50];
1940
  va_start(ap, zFormat);
1941
  sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1942
  va_end(ap);
1943
  n = (int)strlen(zBuf);
1944
  SHA3Update(p, (unsigned char*)zBuf, n);
1945
}
1946
 
1947
/*
1948
** Implementation of the sha3_query(SQL,SIZE) function.
1949
**
1950
** This function compiles and runs the SQL statement(s) given in the
1951
** argument. The results are hashed using a SIZE-bit SHA3.  The default
1952
** size is 256.
1953
**
1954
** The format of the byte stream that is hashed is summarized as follows:
1955
**
1956
**       S:
1957
**       R
1958
**       N
1959
**       I
1960
**       F
1961
**       B:
1962
**       T:
1963
**
1964
**  is the original SQL text for each statement run and  is
1965
** the size of that text.  The SQL text is UTF-8.  A single R character
1966
** occurs before the start of each row.  N means a NULL value.
1967
** I mean an 8-byte little-endian integer .  F is a floating point
1968
** number with an 8-byte little-endian IEEE floating point value .
1969
** B means blobs of  bytes.  T means text rendered as 
1970
** bytes of UTF-8.  The  and  values are expressed as an ASCII
1971
** text integers.
1972
**
1973
** For each SQL statement in the X input, there is one S segment.  Each
1974
** S segment is followed by zero or more R segments, one for each row in the
1975
** result set.  After each R, there are one or more N, I, F, B, or T segments,
1976
** one for each column in the result set.  Segments are concatentated directly
1977
** with no delimiters of any kind.
1978
*/
1979
static void sha3QueryFunc(
1980
  sqlite3_context *context,
1981
  int argc,
1982
  sqlite3_value **argv
1983
){
1984
  sqlite3 *db = sqlite3_context_db_handle(context);
1985
  const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1986
  sqlite3_stmt *pStmt = 0;
1987
  int nCol;                   /* Number of columns in the result set */
1988
  int i;                      /* Loop counter */
1989
  int rc;
1990
  int n;
1991
  const char *z;
1992
  SHA3Context cx;
1993
  int iSize;
1994
 
1995
  if( argc==1 ){
1996
    iSize = 256;
1997
  }else{
1998
    iSize = sqlite3_value_int(argv[1]);
1999
    if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
2000
      sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
2001
                                    "384 512", -1);
2002
      return;
2003
    }
2004
  }
2005
  if( zSql==0 ) return;
2006
  SHA3Init(&cx, iSize);
2007
  while( zSql[0] ){
2008
    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
2009
    if( rc ){
2010
      char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
2011
                                   zSql, sqlite3_errmsg(db));
2012
      sqlite3_finalize(pStmt);
2013
      sqlite3_result_error(context, zMsg, -1);
2014
      sqlite3_free(zMsg);
2015
      return;
2016
    }
2017
    if( !sqlite3_stmt_readonly(pStmt) ){
2018
      char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
2019
      sqlite3_finalize(pStmt);
2020
      sqlite3_result_error(context, zMsg, -1);
2021
      sqlite3_free(zMsg);
2022
      return;
2023
    }
2024
    nCol = sqlite3_column_count(pStmt);
2025
    z = sqlite3_sql(pStmt);
2026
    if( z ){
2027
      n = (int)strlen(z);
2028
      hash_step_vformat(&cx,"S%d:",n);
2029
      SHA3Update(&cx,(unsigned char*)z,n);
2030
    }
2031
 
2032
    /* Compute a hash over the result of the query */
2033
    while( SQLITE_ROW==sqlite3_step(pStmt) ){
2034
      SHA3Update(&cx,(const unsigned char*)"R",1);
2035
      for(i=0; i
2036
        switch( sqlite3_column_type(pStmt,i) ){
2037
          case SQLITE_NULL: {
2038
            SHA3Update(&cx, (const unsigned char*)"N",1);
2039
            break;
2040
          }
2041
          case SQLITE_INTEGER: {
2042
            sqlite3_uint64 u;
2043
            int j;
2044
            unsigned char x[9];
2045
            sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2046
            memcpy(&u, &v, 8);
2047
            for(j=8; j>=1; j--){
2048
              x[j] = u & 0xff;
2049
              u >>= 8;
2050
            }
2051
            x[0] = 'I';
2052
            SHA3Update(&cx, x, 9);
2053
            break;
2054
          }
2055
          case SQLITE_FLOAT: {
2056
            sqlite3_uint64 u;
2057
            int j;
2058
            unsigned char x[9];
2059
            double r = sqlite3_column_double(pStmt,i);
2060
            memcpy(&u, &r, 8);
2061
            for(j=8; j>=1; j--){
2062
              x[j] = u & 0xff;
2063
              u >>= 8;
2064
            }
2065
            x[0] = 'F';
2066
            SHA3Update(&cx,x,9);
2067
            break;
2068
          }
2069
          case SQLITE_TEXT: {
2070
            int n2 = sqlite3_column_bytes(pStmt, i);
2071
            const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2072
            hash_step_vformat(&cx,"T%d:",n2);
2073
            SHA3Update(&cx, z2, n2);
2074
            break;
2075
          }
2076
          case SQLITE_BLOB: {
2077
            int n2 = sqlite3_column_bytes(pStmt, i);
2078
            const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2079
            hash_step_vformat(&cx,"B%d:",n2);
2080
            SHA3Update(&cx, z2, n2);
2081
            break;
2082
          }
2083
        }
2084
      }
2085
    }
2086
    sqlite3_finalize(pStmt);
2087
  }
2088
  sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2089
}
2090
 
2091
 
2092
#ifdef _WIN32
2093
 
2094
#endif
2095
int sqlite3_shathree_init(
2096
  sqlite3 *db,
2097
  char **pzErrMsg,
2098
  const sqlite3_api_routines *pApi
2099
){
2100
  int rc = SQLITE_OK;
2101
  SQLITE_EXTENSION_INIT2(pApi);
2102
  (void)pzErrMsg;  /* Unused parameter */
2103
  rc = sqlite3_create_function(db, "sha3", 1,
2104
                      SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2105
                      0, sha3Func, 0, 0);
2106
  if( rc==SQLITE_OK ){
2107
    rc = sqlite3_create_function(db, "sha3", 2,
2108
                      SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2109
                      0, sha3Func, 0, 0);
2110
  }
2111
  if( rc==SQLITE_OK ){
2112
    rc = sqlite3_create_function(db, "sha3_query", 1,
2113
                      SQLITE_UTF8 | SQLITE_DIRECTONLY,
2114
                      0, sha3QueryFunc, 0, 0);
2115
  }
2116
  if( rc==SQLITE_OK ){
2117
    rc = sqlite3_create_function(db, "sha3_query", 2,
2118
                      SQLITE_UTF8 | SQLITE_DIRECTONLY,
2119
                      0, sha3QueryFunc, 0, 0);
2120
  }
2121
  return rc;
2122
}
2123
 
2124
/************************* End ../ext/misc/shathree.c ********************/
2125
/************************* Begin ../ext/misc/fileio.c ******************/
2126
/*
2127
** 2014-06-13
2128
**
2129
** The author disclaims copyright to this source code.  In place of
2130
** a legal notice, here is a blessing:
2131
**
2132
**    May you do good and not evil.
2133
**    May you find forgiveness for yourself and forgive others.
2134
**    May you share freely, never taking more than you give.
2135
**
2136
******************************************************************************
2137
**
2138
** This SQLite extension implements SQL functions readfile() and
2139
** writefile(), and eponymous virtual type "fsdir".
2140
**
2141
** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2142
**
2143
**   If neither of the optional arguments is present, then this UDF
2144
**   function writes blob DATA to file FILE. If successful, the number
2145
**   of bytes written is returned. If an error occurs, NULL is returned.
2146
**
2147
**   If the first option argument - MODE - is present, then it must
2148
**   be passed an integer value that corresponds to a POSIX mode
2149
**   value (file type + permissions, as returned in the stat.st_mode
2150
**   field by the stat() system call). Three types of files may
2151
**   be written/created:
2152
**
2153
**     regular files:  (mode & 0170000)==0100000
2154
**     symbolic links: (mode & 0170000)==0120000
2155
**     directories:    (mode & 0170000)==0040000
2156
**
2157
**   For a directory, the DATA is ignored. For a symbolic link, it is
2158
**   interpreted as text and used as the target of the link. For a
2159
**   regular file, it is interpreted as a blob and written into the
2160
**   named file. Regardless of the type of file, its permissions are
2161
**   set to (mode & 0777) before returning.
2162
**
2163
**   If the optional MTIME argument is present, then it is interpreted
2164
**   as an integer - the number of seconds since the unix epoch. The
2165
**   modification-time of the target file is set to this value before
2166
**   returning.
2167
**
2168
**   If three or more arguments are passed to this function and an
2169
**   error is encountered, an exception is raised.
2170
**
2171
** READFILE(FILE):
2172
**
2173
**   Read and return the contents of file FILE (type blob) from disk.
2174
**
2175
** FSDIR:
2176
**
2177
**   Used as follows:
2178
**
2179
**     SELECT * FROM fsdir($path [, $dir]);
2180
**
2181
**   Parameter $path is an absolute or relative pathname. If the file that it
2182
**   refers to does not exist, it is an error. If the path refers to a regular
2183
**   file or symbolic link, it returns a single row. Or, if the path refers
2184
**   to a directory, it returns one row for the directory, and one row for each
2185
**   file within the hierarchy rooted at $path.
2186
**
2187
**   Each row has the following columns:
2188
**
2189
**     name:  Path to file or directory (text value).
2190
**     mode:  Value of stat.st_mode for directory entry (an integer).
2191
**     mtime: Value of stat.st_mtime for directory entry (an integer).
2192
**     data:  For a regular file, a blob containing the file data. For a
2193
**            symlink, a text value containing the text of the link. For a
2194
**            directory, NULL.
2195
**
2196
**   If a non-NULL value is specified for the optional $dir parameter and
2197
**   $path is a relative path, then $path is interpreted relative to $dir.
2198
**   And the paths returned in the "name" column of the table are also
2199
**   relative to directory $dir.
2200
*/
2201
/* #include "sqlite3ext.h" */
2202
SQLITE_EXTENSION_INIT1
2203
#include 
2204
#include 
2205
#include 
2206
 
2207
#include 
2208
#include 
2209
#include 
2210
#if !defined(_WIN32) && !defined(WIN32)
2211
#  include 
9952 turbocat 2212
#ifndef _KOLIBRI
8760 turbocat 2213
#  include 
9952 turbocat 2214
#endif
8760 turbocat 2215
#  include 
2216
#  include 
2217
#else
2218
#  include "windows.h"
2219
#  include 
2220
#  include 
2221
/* #  include "test_windirent.h" */
2222
#  define dirent DIRENT
2223
#  ifndef chmod
2224
#    define chmod _chmod
2225
#  endif
2226
#  ifndef stat
2227
#    define stat _stat
2228
#  endif
2229
#  define mkdir(path,mode) _mkdir(path)
2230
#  define lstat(path,buf) stat(path,buf)
2231
#endif
2232
#include 
2233
#include 
2234
 
2235
 
2236
/*
2237
** Structure of the fsdir() table-valued function
2238
*/
2239
                 /*    0    1    2     3    4           5             */
2240
#define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2241
#define FSDIR_COLUMN_NAME     0     /* Name of the file */
2242
#define FSDIR_COLUMN_MODE     1     /* Access mode */
2243
#define FSDIR_COLUMN_MTIME    2     /* Last modification time */
2244
#define FSDIR_COLUMN_DATA     3     /* File content */
2245
#define FSDIR_COLUMN_PATH     4     /* Path to top of search */
2246
#define FSDIR_COLUMN_DIR      5     /* Path is relative to this directory */
2247
 
2248
 
2249
/*
2250
** Set the result stored by context ctx to a blob containing the
2251
** contents of file zName.  Or, leave the result unchanged (NULL)
2252
** if the file does not exist or is unreadable.
2253
**
2254
** If the file exceeds the SQLite blob size limit, through an
2255
** SQLITE_TOOBIG error.
2256
**
2257
** Throw an SQLITE_IOERR if there are difficulties pulling the file
2258
** off of disk.
2259
*/
2260
static void readFileContents(sqlite3_context *ctx, const char *zName){
2261
  FILE *in;
2262
  sqlite3_int64 nIn;
2263
  void *pBuf;
2264
  sqlite3 *db;
2265
  int mxBlob;
2266
 
2267
  in = fopen(zName, "rb");
2268
  if( in==0 ){
2269
    /* File does not exist or is unreadable. Leave the result set to NULL. */
2270
    return;
2271
  }
2272
  fseek(in, 0, SEEK_END);
2273
  nIn = ftell(in);
2274
  rewind(in);
2275
  db = sqlite3_context_db_handle(ctx);
2276
  mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2277
  if( nIn>mxBlob ){
2278
    sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2279
    fclose(in);
2280
    return;
2281
  }
2282
  pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2283
  if( pBuf==0 ){
2284
    sqlite3_result_error_nomem(ctx);
2285
    fclose(in);
2286
    return;
2287
  }
2288
  if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2289
    sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2290
  }else{
2291
    sqlite3_result_error_code(ctx, SQLITE_IOERR);
2292
    sqlite3_free(pBuf);
2293
  }
2294
  fclose(in);
2295
}
2296
 
2297
/*
2298
** Implementation of the "readfile(X)" SQL function.  The entire content
2299
** of the file named X is read and returned as a BLOB.  NULL is returned
2300
** if the file does not exist or is unreadable.
2301
*/
2302
static void readfileFunc(
2303
  sqlite3_context *context,
2304
  int argc,
2305
  sqlite3_value **argv
2306
){
2307
  const char *zName;
2308
  (void)(argc);  /* Unused parameter */
2309
  zName = (const char*)sqlite3_value_text(argv[0]);
2310
  if( zName==0 ) return;
2311
  readFileContents(context, zName);
2312
}
2313
 
2314
/*
2315
** Set the error message contained in context ctx to the results of
2316
** vprintf(zFmt, ...).
2317
*/
2318
static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2319
  char *zMsg = 0;
2320
  va_list ap;
2321
  va_start(ap, zFmt);
2322
  zMsg = sqlite3_vmprintf(zFmt, ap);
2323
  sqlite3_result_error(ctx, zMsg, -1);
2324
  sqlite3_free(zMsg);
2325
  va_end(ap);
2326
}
2327
 
2328
#if defined(_WIN32)
2329
/*
2330
** This function is designed to convert a Win32 FILETIME structure into the
2331
** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2332
*/
2333
static sqlite3_uint64 fileTimeToUnixTime(
2334
  LPFILETIME pFileTime
2335
){
2336
  SYSTEMTIME epochSystemTime;
2337
  ULARGE_INTEGER epochIntervals;
2338
  FILETIME epochFileTime;
2339
  ULARGE_INTEGER fileIntervals;
2340
 
2341
  memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2342
  epochSystemTime.wYear = 1970;
2343
  epochSystemTime.wMonth = 1;
2344
  epochSystemTime.wDay = 1;
2345
  SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2346
  epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2347
  epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2348
 
2349
  fileIntervals.LowPart = pFileTime->dwLowDateTime;
2350
  fileIntervals.HighPart = pFileTime->dwHighDateTime;
2351
 
2352
  return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2353
}
2354
 
2355
/*
2356
** This function attempts to normalize the time values found in the stat()
2357
** buffer to UTC.  This is necessary on Win32, where the runtime library
2358
** appears to return these values as local times.
2359
*/
2360
static void statTimesToUtc(
2361
  const char *zPath,
2362
  struct stat *pStatBuf
2363
){
2364
  HANDLE hFindFile;
2365
  WIN32_FIND_DATAW fd;
2366
  LPWSTR zUnicodeName;
2367
  extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2368
  zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2369
  if( zUnicodeName ){
2370
    memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2371
    hFindFile = FindFirstFileW(zUnicodeName, &fd);
2372
    if( hFindFile!=NULL ){
2373
      pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2374
      pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2375
      pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2376
      FindClose(hFindFile);
2377
    }
2378
    sqlite3_free(zUnicodeName);
2379
  }
2380
}
2381
#endif
2382
 
2383
/*
2384
** This function is used in place of stat().  On Windows, special handling
2385
** is required in order for the included time to be returned as UTC.  On all
2386
** other systems, this function simply calls stat().
2387
*/
2388
static int fileStat(
2389
  const char *zPath,
2390
  struct stat *pStatBuf
2391
){
2392
#if defined(_WIN32)
2393
  int rc = stat(zPath, pStatBuf);
2394
  if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2395
  return rc;
2396
#else
2397
  return stat(zPath, pStatBuf);
2398
#endif
2399
}
2400
 
2401
/*
2402
** This function is used in place of lstat().  On Windows, special handling
2403
** is required in order for the included time to be returned as UTC.  On all
2404
** other systems, this function simply calls lstat().
2405
*/
2406
static int fileLinkStat(
2407
  const char *zPath,
2408
  struct stat *pStatBuf
2409
){
2410
#if defined(_WIN32)
2411
  int rc = lstat(zPath, pStatBuf);
2412
  if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2413
  return rc;
2414
#else
2415
  return lstat(zPath, pStatBuf);
2416
#endif
2417
}
2418
 
2419
/*
2420
** Argument zFile is the name of a file that will be created and/or written
2421
** by SQL function writefile(). This function ensures that the directory
2422
** zFile will be written to exists, creating it if required. The permissions
2423
** for any path components created by this function are set in accordance
2424
** with the current umask.
2425
**
2426
** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2427
** SQLITE_OK is returned if the directory is successfully created, or
2428
** SQLITE_ERROR otherwise.
2429
*/
2430
static int makeDirectory(
2431
  const char *zFile
2432
){
2433
  char *zCopy = sqlite3_mprintf("%s", zFile);
2434
  int rc = SQLITE_OK;
2435
 
2436
  if( zCopy==0 ){
2437
    rc = SQLITE_NOMEM;
2438
  }else{
2439
    int nCopy = (int)strlen(zCopy);
2440
    int i = 1;
2441
 
2442
    while( rc==SQLITE_OK ){
2443
      struct stat sStat;
2444
      int rc2;
2445
 
2446
      for(; zCopy[i]!='/' && i
2447
      if( i==nCopy ) break;
2448
      zCopy[i] = '\0';
2449
 
2450
      rc2 = fileStat(zCopy, &sStat);
2451
      if( rc2!=0 ){
2452
        if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2453
      }else{
2454
        if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2455
      }
2456
      zCopy[i] = '/';
2457
      i++;
2458
    }
2459
 
2460
    sqlite3_free(zCopy);
2461
  }
2462
 
2463
  return rc;
2464
}
2465
 
2466
/*
2467
** This function does the work for the writefile() UDF. Refer to
2468
** header comments at the top of this file for details.
2469
*/
2470
static int writeFile(
2471
  sqlite3_context *pCtx,          /* Context to return bytes written in */
2472
  const char *zFile,              /* File to write */
2473
  sqlite3_value *pData,           /* Data to write */
2474
  mode_t mode,                    /* MODE parameter passed to writefile() */
2475
  sqlite3_int64 mtime             /* MTIME parameter (or -1 to not set time) */
2476
){
2477
#if !defined(_WIN32) && !defined(WIN32) && !defined (_KOLIBRI)
2478
 
2479
    if( S_ISLNK(mode) ){
2480
    const char *zTo = (const char*)sqlite3_value_text(pData);
2481
    if( symlink(zTo, zFile)<0 ) return 1;
2482
  }else
2483
#endif
2484
  {
2485
    if( S_ISDIR(mode) ){
2486
      if( mkdir(zFile, mode) ){
2487
        /* The mkdir() call to create the directory failed. This might not
2488
        ** be an error though - if there is already a directory at the same
2489
        ** path and either the permissions already match or can be changed
2490
        ** to do so using chmod(), it is not an error.  */
2491
        struct stat sStat;
2492
        if( errno!=EEXIST
2493
         || 0!=fileStat(zFile, &sStat)
2494
         || !S_ISDIR(sStat.st_mode)
2495
         || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2496
        ){
2497
          return 1;
2498
        }
2499
      }
2500
    }else{
2501
      sqlite3_int64 nWrite = 0;
2502
      const char *z;
2503
      int rc = 0;
2504
      FILE *out = fopen(zFile, "wb");
2505
      if( out==0 ) return 1;
2506
      z = (const char*)sqlite3_value_blob(pData);
2507
      if( z ){
2508
        sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2509
        nWrite = sqlite3_value_bytes(pData);
2510
        if( nWrite!=n ){
2511
          rc = 1;
2512
        }
2513
      }
2514
      fclose(out);
2515
      if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2516
        rc = 1;
2517
      }
2518
      if( rc ) return 2;
2519
      sqlite3_result_int64(pCtx, nWrite);
2520
    }
2521
  }
2522
 
2523
  if( mtime>=0 ){
2524
#if defined(_WIN32)
2525
#if !SQLITE_OS_WINRT
2526
    /* Windows */
2527
    FILETIME lastAccess;
2528
    FILETIME lastWrite;
2529
    SYSTEMTIME currentTime;
2530
    LONGLONG intervals;
2531
    HANDLE hFile;
2532
    LPWSTR zUnicodeName;
2533
    extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2534
 
2535
    GetSystemTime(¤tTime);
2536
    SystemTimeToFileTime(¤tTime, &lastAccess);
2537
    intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2538
    lastWrite.dwLowDateTime = (DWORD)intervals;
2539
    lastWrite.dwHighDateTime = intervals >> 32;
2540
    zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2541
    if( zUnicodeName==0 ){
2542
      return 1;
2543
    }
2544
    hFile = CreateFileW(
2545
      zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2546
      FILE_FLAG_BACKUP_SEMANTICS, NULL
2547
    );
2548
    sqlite3_free(zUnicodeName);
2549
    if( hFile!=INVALID_HANDLE_VALUE ){
2550
      BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2551
      CloseHandle(hFile);
2552
      return !bResult;
2553
    }else{
2554
      return 1;
2555
    }
2556
#endif
2557
#elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2558
    /* Recent unix */
2559
    struct timespec times[2];
2560
    times[0].tv_nsec = times[1].tv_nsec = 0;
2561
    times[0].tv_sec = time(0);
2562
    times[1].tv_sec = mtime;
2563
    if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2564
      return 1;
2565
    }
2566
 
2567
 
2568
#elif !defined(_KOLIBRI)
2569
    /* Legacy unix */
2570
    struct timeval times[2];
2571
    times[0].tv_usec = times[1].tv_usec = 0;
2572
    times[0].tv_sec = time(0);
2573
    times[1].tv_sec = mtime;
2574
    if( utimes(zFile, times) ){
2575
      return 1;
2576
    }
2577
#endif
2578
  }
2579
 
2580
  return 0;
2581
}
2582
 
2583
/*
2584
** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2585
** Refer to header comments at the top of this file for details.
2586
*/
2587
static void writefileFunc(
2588
  sqlite3_context *context,
2589
  int argc,
2590
  sqlite3_value **argv
2591
){
2592
  const char *zFile;
2593
  mode_t mode = 0;
2594
  int res;
2595
  sqlite3_int64 mtime = -1;
2596
 
2597
  if( argc<2 || argc>4 ){
2598
    sqlite3_result_error(context,
2599
        "wrong number of arguments to function writefile()", -1
2600
    );
2601
    return;
2602
  }
2603
 
2604
  zFile = (const char*)sqlite3_value_text(argv[0]);
2605
  if( zFile==0 ) return;
2606
  if( argc>=3 ){
2607
    mode = (mode_t)sqlite3_value_int(argv[2]);
2608
  }
2609
  if( argc==4 ){
2610
    mtime = sqlite3_value_int64(argv[3]);
2611
  }
2612
 
2613
  res = writeFile(context, zFile, argv[1], mode, mtime);
2614
  if( res==1 && errno==ENOENT ){
2615
    if( makeDirectory(zFile)==SQLITE_OK ){
2616
      res = writeFile(context, zFile, argv[1], mode, mtime);
2617
    }
2618
  }
2619
 
2620
  if( argc>2 && res!=0 ){
2621
    if( S_ISLNK(mode) ){
2622
      ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2623
    }else if( S_ISDIR(mode) ){
2624
      ctxErrorMsg(context, "failed to create directory: %s", zFile);
2625
    }else{
2626
      ctxErrorMsg(context, "failed to write file: %s", zFile);
2627
    }
2628
  }
2629
}
2630
 
2631
/*
2632
** SQL function:   lsmode(MODE)
2633
**
2634
** Given a numberic st_mode from stat(), convert it into a human-readable
2635
** text string in the style of "ls -l".
2636
*/
2637
static void lsModeFunc(
2638
  sqlite3_context *context,
2639
  int argc,
2640
  sqlite3_value **argv
2641
){
2642
  int i;
2643
  int iMode = sqlite3_value_int(argv[0]);
2644
  char z[16];
2645
  (void)argc;
2646
  if( S_ISLNK(iMode) ){
2647
    z[0] = 'l';
2648
  }else if( S_ISREG(iMode) ){
2649
    z[0] = '-';
2650
  }else if( S_ISDIR(iMode) ){
2651
    z[0] = 'd';
2652
  }else{
2653
    z[0] = '?';
2654
  }
2655
  for(i=0; i<3; i++){
2656
    int m = (iMode >> ((2-i)*3));
2657
    char *a = &z[1 + i*3];
2658
    a[0] = (m & 0x4) ? 'r' : '-';
2659
    a[1] = (m & 0x2) ? 'w' : '-';
2660
    a[2] = (m & 0x1) ? 'x' : '-';
2661
  }
2662
  z[10] = '\0';
2663
  sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2664
}
2665
 
2666
#ifndef SQLITE_OMIT_VIRTUALTABLE
2667
 
2668
/*
2669
** Cursor type for recursively iterating through a directory structure.
2670
*/
2671
typedef struct fsdir_cursor fsdir_cursor;
2672
typedef struct FsdirLevel FsdirLevel;
2673
 
2674
struct FsdirLevel {
2675
  DIR *pDir;                 /* From opendir() */
2676
  char *zDir;                /* Name of directory (nul-terminated) */
2677
};
2678
 
2679
struct fsdir_cursor {
2680
  sqlite3_vtab_cursor base;  /* Base class - must be first */
2681
 
2682
  int nLvl;                  /* Number of entries in aLvl[] array */
2683
  int iLvl;                  /* Index of current entry */
2684
  FsdirLevel *aLvl;          /* Hierarchy of directories being traversed */
2685
 
2686
  const char *zBase;
2687
  int nBase;
2688
 
2689
  struct stat sStat;         /* Current lstat() results */
2690
  char *zPath;               /* Path to current entry */
2691
  sqlite3_int64 iRowid;      /* Current rowid */
2692
};
2693
 
2694
typedef struct fsdir_tab fsdir_tab;
2695
struct fsdir_tab {
2696
  sqlite3_vtab base;         /* Base class - must be first */
2697
};
2698
 
2699
/*
2700
** Construct a new fsdir virtual table object.
2701
*/
2702
static int fsdirConnect(
2703
  sqlite3 *db,
2704
  void *pAux,
2705
  int argc, const char *const*argv,
2706
  sqlite3_vtab **ppVtab,
2707
  char **pzErr
2708
){
2709
  fsdir_tab *pNew = 0;
2710
  int rc;
2711
  (void)pAux;
2712
  (void)argc;
2713
  (void)argv;
2714
  (void)pzErr;
2715
  rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2716
  if( rc==SQLITE_OK ){
2717
    pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2718
    if( pNew==0 ) return SQLITE_NOMEM;
2719
    memset(pNew, 0, sizeof(*pNew));
2720
    sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2721
  }
2722
  *ppVtab = (sqlite3_vtab*)pNew;
2723
  return rc;
2724
}
2725
 
2726
/*
2727
** This method is the destructor for fsdir vtab objects.
2728
*/
2729
static int fsdirDisconnect(sqlite3_vtab *pVtab){
2730
  sqlite3_free(pVtab);
2731
  return SQLITE_OK;
2732
}
2733
 
2734
/*
2735
** Constructor for a new fsdir_cursor object.
2736
*/
2737
static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2738
  fsdir_cursor *pCur;
2739
  (void)p;
2740
  pCur = sqlite3_malloc( sizeof(*pCur) );
2741
  if( pCur==0 ) return SQLITE_NOMEM;
2742
  memset(pCur, 0, sizeof(*pCur));
2743
  pCur->iLvl = -1;
2744
  *ppCursor = &pCur->base;
2745
  return SQLITE_OK;
2746
}
2747
 
2748
/*
2749
** Reset a cursor back to the state it was in when first returned
2750
** by fsdirOpen().
2751
*/
2752
static void fsdirResetCursor(fsdir_cursor *pCur){
2753
  int i;
2754
  for(i=0; i<=pCur->iLvl; i++){
2755
    FsdirLevel *pLvl = &pCur->aLvl[i];
2756
    if( pLvl->pDir ) closedir(pLvl->pDir);
2757
    sqlite3_free(pLvl->zDir);
2758
  }
2759
  sqlite3_free(pCur->zPath);
2760
  sqlite3_free(pCur->aLvl);
2761
  pCur->aLvl = 0;
2762
  pCur->zPath = 0;
2763
  pCur->zBase = 0;
2764
  pCur->nBase = 0;
2765
  pCur->nLvl = 0;
2766
  pCur->iLvl = -1;
2767
  pCur->iRowid = 1;
2768
}
2769
 
2770
/*
2771
** Destructor for an fsdir_cursor.
2772
*/
2773
static int fsdirClose(sqlite3_vtab_cursor *cur){
2774
  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2775
 
2776
  fsdirResetCursor(pCur);
2777
  sqlite3_free(pCur);
2778
  return SQLITE_OK;
2779
}
2780
 
2781
/*
2782
** Set the error message for the virtual table associated with cursor
2783
** pCur to the results of vprintf(zFmt, ...).
2784
*/
2785
static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2786
  va_list ap;
2787
  va_start(ap, zFmt);
2788
  pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2789
  va_end(ap);
2790
}
2791
 
2792
 
2793
/*
2794
** Advance an fsdir_cursor to its next row of output.
2795
*/
2796
static int fsdirNext(sqlite3_vtab_cursor *cur){
2797
  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2798
  mode_t m = pCur->sStat.st_mode;
2799
 
2800
  pCur->iRowid++;
2801
  if( S_ISDIR(m) ){
2802
    /* Descend into this directory */
2803
    int iNew = pCur->iLvl + 1;
2804
    FsdirLevel *pLvl;
2805
    if( iNew>=pCur->nLvl ){
2806
      int nNew = iNew+1;
2807
      sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2808
      FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2809
      if( aNew==0 ) return SQLITE_NOMEM;
2810
      memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2811
      pCur->aLvl = aNew;
2812
      pCur->nLvl = nNew;
2813
    }
2814
    pCur->iLvl = iNew;
2815
    pLvl = &pCur->aLvl[iNew];
2816
 
2817
    pLvl->zDir = pCur->zPath;
2818
    pCur->zPath = 0;
2819
    pLvl->pDir = opendir(pLvl->zDir);
2820
    if( pLvl->pDir==0 ){
2821
      fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2822
      return SQLITE_ERROR;
2823
    }
2824
  }
2825
 
2826
  while( pCur->iLvl>=0 ){
2827
    FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2828
    struct dirent *pEntry = readdir(pLvl->pDir);
2829
    if( pEntry ){
2830
      if( pEntry->d_name[0]=='.' ){
2831
       if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2832
       if( pEntry->d_name[1]=='\0' ) continue;
2833
      }
2834
      sqlite3_free(pCur->zPath);
2835
      pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2836
      if( pCur->zPath==0 ) return SQLITE_NOMEM;
2837
      if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2838
        fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2839
        return SQLITE_ERROR;
2840
      }
2841
      return SQLITE_OK;
2842
    }
2843
    closedir(pLvl->pDir);
2844
    sqlite3_free(pLvl->zDir);
2845
    pLvl->pDir = 0;
2846
    pLvl->zDir = 0;
2847
    pCur->iLvl--;
2848
  }
2849
 
2850
  /* EOF */
2851
  sqlite3_free(pCur->zPath);
2852
  pCur->zPath = 0;
2853
  return SQLITE_OK;
2854
}
2855
 
2856
/*
2857
** Return values of columns for the row at which the series_cursor
2858
** is currently pointing.
2859
*/
2860
static int fsdirColumn(
2861
  sqlite3_vtab_cursor *cur,   /* The cursor */
2862
  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
2863
  int i                       /* Which column to return */
2864
){
2865
  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2866
  switch( i ){
2867
    case FSDIR_COLUMN_NAME: {
2868
      sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2869
      break;
2870
    }
2871
 
2872
    case FSDIR_COLUMN_MODE:
2873
      sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2874
      break;
2875
 
2876
    case FSDIR_COLUMN_MTIME:
2877
      sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2878
      break;
2879
 
2880
    case FSDIR_COLUMN_DATA: {
2881
      mode_t m = pCur->sStat.st_mode;
2882
      if( S_ISDIR(m) ){
2883
        sqlite3_result_null(ctx);
2884
#if !defined(_WIN32) && !defined(WIN32) && !defined(_KOLIBRI)
2885
      }else if( S_ISLNK(m) ){
2886
        char aStatic[64];
2887
        char *aBuf = aStatic;
2888
        sqlite3_int64 nBuf = 64;
2889
        int n;
2890
 
2891
        while( 1 ){
2892
          n = readlink(pCur->zPath, aBuf, nBuf);
2893
          if( n
2894
          if( aBuf!=aStatic ) sqlite3_free(aBuf);
2895
          nBuf = nBuf*2;
2896
          aBuf = sqlite3_malloc64(nBuf);
2897
          if( aBuf==0 ){
2898
            sqlite3_result_error_nomem(ctx);
2899
            return SQLITE_NOMEM;
2900
          }
2901
        }
2902
 
2903
        sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2904
        if( aBuf!=aStatic ) sqlite3_free(aBuf);
2905
#endif
2906
      }else{
2907
        readFileContents(ctx, pCur->zPath);
2908
      }
2909
    }
2910
    case FSDIR_COLUMN_PATH:
2911
    default: {
2912
      /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2913
      ** always return their values as NULL */
2914
      break;
2915
    }
2916
  }
2917
  return SQLITE_OK;
2918
}
2919
 
2920
/*
2921
** Return the rowid for the current row. In this implementation, the
2922
** first row returned is assigned rowid value 1, and each subsequent
2923
** row a value 1 more than that of the previous.
2924
*/
2925
static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2926
  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2927
  *pRowid = pCur->iRowid;
2928
  return SQLITE_OK;
2929
}
2930
 
2931
/*
2932
** Return TRUE if the cursor has been moved off of the last
2933
** row of output.
2934
*/
2935
static int fsdirEof(sqlite3_vtab_cursor *cur){
2936
  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2937
  return (pCur->zPath==0);
2938
}
2939
 
2940
/*
2941
** xFilter callback.
2942
**
2943
** idxNum==1   PATH parameter only
2944
** idxNum==2   Both PATH and DIR supplied
2945
*/
2946
static int fsdirFilter(
2947
  sqlite3_vtab_cursor *cur,
2948
  int idxNum, const char *idxStr,
2949
  int argc, sqlite3_value **argv
2950
){
2951
  const char *zDir = 0;
2952
  fsdir_cursor *pCur = (fsdir_cursor*)cur;
2953
  (void)idxStr;
2954
  fsdirResetCursor(pCur);
2955
 
2956
  if( idxNum==0 ){
2957
    fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2958
    return SQLITE_ERROR;
2959
  }
2960
 
2961
  assert( argc==idxNum && (argc==1 || argc==2) );
2962
  zDir = (const char*)sqlite3_value_text(argv[0]);
2963
  if( zDir==0 ){
2964
    fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2965
    return SQLITE_ERROR;
2966
  }
2967
  if( argc==2 ){
2968
    pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2969
  }
2970
  if( pCur->zBase ){
2971
    pCur->nBase = (int)strlen(pCur->zBase)+1;
2972
    pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2973
  }else{
2974
    pCur->zPath = sqlite3_mprintf("%s", zDir);
2975
  }
2976
 
2977
  if( pCur->zPath==0 ){
2978
    return SQLITE_NOMEM;
2979
  }
2980
  if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2981
    fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2982
    return SQLITE_ERROR;
2983
  }
2984
 
2985
  return SQLITE_OK;
2986
}
2987
 
2988
/*
2989
** SQLite will invoke this method one or more times while planning a query
2990
** that uses the generate_series virtual table.  This routine needs to create
2991
** a query plan for each invocation and compute an estimated cost for that
2992
** plan.
2993
**
2994
** In this implementation idxNum is used to represent the
2995
** query plan.  idxStr is unused.
2996
**
2997
** The query plan is represented by values of idxNum:
2998
**
2999
**  (1)  The path value is supplied by argv[0]
3000
**  (2)  Path is in argv[0] and dir is in argv[1]
3001
*/
3002
static int fsdirBestIndex(
3003
  sqlite3_vtab *tab,
3004
  sqlite3_index_info *pIdxInfo
3005
){
3006
  int i;                 /* Loop over constraints */
3007
  int idxPath = -1;      /* Index in pIdxInfo->aConstraint of PATH= */
3008
  int idxDir = -1;       /* Index in pIdxInfo->aConstraint of DIR= */
3009
  int seenPath = 0;      /* True if an unusable PATH= constraint is seen */
3010
  int seenDir = 0;       /* True if an unusable DIR= constraint is seen */
3011
  const struct sqlite3_index_constraint *pConstraint;
3012
 
3013
  (void)tab;
3014
  pConstraint = pIdxInfo->aConstraint;
3015
  for(i=0; inConstraint; i++, pConstraint++){
3016
    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3017
    switch( pConstraint->iColumn ){
3018
      case FSDIR_COLUMN_PATH: {
3019
        if( pConstraint->usable ){
3020
          idxPath = i;
3021
          seenPath = 0;
3022
        }else if( idxPath<0 ){
3023
          seenPath = 1;
3024
        }
3025
        break;
3026
      }
3027
      case FSDIR_COLUMN_DIR: {
3028
        if( pConstraint->usable ){
3029
          idxDir = i;
3030
          seenDir = 0;
3031
        }else if( idxDir<0 ){
3032
          seenDir = 1;
3033
        }
3034
        break;
3035
      }
3036
    }
3037
  }
3038
  if( seenPath || seenDir ){
3039
    /* If input parameters are unusable, disallow this plan */
3040
    return SQLITE_CONSTRAINT;
3041
  }
3042
 
3043
  if( idxPath<0 ){
3044
    pIdxInfo->idxNum = 0;
3045
    /* The pIdxInfo->estimatedCost should have been initialized to a huge
3046
    ** number.  Leave it unchanged. */
3047
    pIdxInfo->estimatedRows = 0x7fffffff;
3048
  }else{
3049
    pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3050
    pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3051
    if( idxDir>=0 ){
3052
      pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3053
      pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3054
      pIdxInfo->idxNum = 2;
3055
      pIdxInfo->estimatedCost = 10.0;
3056
    }else{
3057
      pIdxInfo->idxNum = 1;
3058
      pIdxInfo->estimatedCost = 100.0;
3059
    }
3060
  }
3061
 
3062
  return SQLITE_OK;
3063
}
3064
 
3065
/*
3066
** Register the "fsdir" virtual table.
3067
*/
3068
static int fsdirRegister(sqlite3 *db){
3069
  static sqlite3_module fsdirModule = {
3070
    0,                         /* iVersion */
3071
    0,                         /* xCreate */
3072
    fsdirConnect,              /* xConnect */
3073
    fsdirBestIndex,            /* xBestIndex */
3074
    fsdirDisconnect,           /* xDisconnect */
3075
    0,                         /* xDestroy */
3076
    fsdirOpen,                 /* xOpen - open a cursor */
3077
    fsdirClose,                /* xClose - close a cursor */
3078
    fsdirFilter,               /* xFilter - configure scan constraints */
3079
    fsdirNext,                 /* xNext - advance a cursor */
3080
    fsdirEof,                  /* xEof - check for end of scan */
3081
    fsdirColumn,               /* xColumn - read data */
3082
    fsdirRowid,                /* xRowid - read data */
3083
    0,                         /* xUpdate */
3084
    0,                         /* xBegin */
3085
    0,                         /* xSync */
3086
    0,                         /* xCommit */
3087
    0,                         /* xRollback */
3088
    0,                         /* xFindMethod */
3089
    0,                         /* xRename */
3090
    0,                         /* xSavepoint */
3091
    0,                         /* xRelease */
3092
    0,                         /* xRollbackTo */
3093
    0,                         /* xShadowName */
3094
  };
3095
 
3096
  int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3097
  return rc;
3098
}
3099
#else         /* SQLITE_OMIT_VIRTUALTABLE */
3100
# define fsdirRegister(x) SQLITE_OK
3101
#endif
3102
 
3103
#ifdef _WIN32
3104
 
3105
#endif
3106
int sqlite3_fileio_init(
3107
  sqlite3 *db,
3108
  char **pzErrMsg,
3109
  const sqlite3_api_routines *pApi
3110
){
3111
  int rc = SQLITE_OK;
3112
  SQLITE_EXTENSION_INIT2(pApi);
3113
  (void)pzErrMsg;  /* Unused parameter */
3114
  rc = sqlite3_create_function(db, "readfile", 1,
3115
                               SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3116
                               readfileFunc, 0, 0);
3117
  if( rc==SQLITE_OK ){
3118
    rc = sqlite3_create_function(db, "writefile", -1,
3119
                                 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3120
                                 writefileFunc, 0, 0);
3121
  }
3122
  if( rc==SQLITE_OK ){
3123
    rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3124
                                 lsModeFunc, 0, 0);
3125
  }
3126
  if( rc==SQLITE_OK ){
3127
    rc = fsdirRegister(db);
3128
  }
3129
  return rc;
3130
}
3131
 
3132
/************************* End ../ext/misc/fileio.c ********************/
3133
/************************* Begin ../ext/misc/completion.c ******************/
3134
/*
3135
** 2017-07-10
3136
**
3137
** The author disclaims copyright to this source code.  In place of
3138
** a legal notice, here is a blessing:
3139
**
3140
**    May you do good and not evil.
3141
**    May you find forgiveness for yourself and forgive others.
3142
**    May you share freely, never taking more than you give.
3143
**
3144
*************************************************************************
3145
**
3146
** This file implements an eponymous virtual table that returns suggested
3147
** completions for a partial SQL input.
3148
**
3149
** Suggested usage:
3150
**
3151
**     SELECT DISTINCT candidate COLLATE nocase
3152
**       FROM completion($prefix,$wholeline)
3153
**      ORDER BY 1;
3154
**
3155
** The two query parameters are optional.  $prefix is the text of the
3156
** current word being typed and that is to be completed.  $wholeline is
3157
** the complete input line, used for context.
3158
**
3159
** The raw completion() table might return the same candidate multiple
3160
** times, for example if the same column name is used to two or more
3161
** tables.  And the candidates are returned in an arbitrary order.  Hence,
3162
** the DISTINCT and ORDER BY are recommended.
3163
**
3164
** This virtual table operates at the speed of human typing, and so there
3165
** is no attempt to make it fast.  Even a slow implementation will be much
3166
** faster than any human can type.
3167
**
3168
*/
3169
/* #include "sqlite3ext.h" */
3170
SQLITE_EXTENSION_INIT1
3171
#include 
3172
#include 
3173
#include 
3174
 
3175
#ifndef SQLITE_OMIT_VIRTUALTABLE
3176
 
3177
/* completion_vtab is a subclass of sqlite3_vtab which will
3178
** serve as the underlying representation of a completion virtual table
3179
*/
3180
typedef struct completion_vtab completion_vtab;
3181
struct completion_vtab {
3182
  sqlite3_vtab base;  /* Base class - must be first */
3183
  sqlite3 *db;        /* Database connection for this completion vtab */
3184
};
3185
 
3186
/* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3187
** serve as the underlying representation of a cursor that scans
3188
** over rows of the result
3189
*/
3190
typedef struct completion_cursor completion_cursor;
3191
struct completion_cursor {
3192
  sqlite3_vtab_cursor base;  /* Base class - must be first */
3193
  sqlite3 *db;               /* Database connection for this cursor */
3194
  int nPrefix, nLine;        /* Number of bytes in zPrefix and zLine */
3195
  char *zPrefix;             /* The prefix for the word we want to complete */
3196
  char *zLine;               /* The whole that we want to complete */
3197
  const char *zCurrentRow;   /* Current output row */
3198
  int szRow;                 /* Length of the zCurrentRow string */
3199
  sqlite3_stmt *pStmt;       /* Current statement */
3200
  sqlite3_int64 iRowid;      /* The rowid */
3201
  int ePhase;                /* Current phase */
3202
  int j;                     /* inter-phase counter */
3203
};
3204
 
3205
/* Values for ePhase:
3206
*/
3207
#define COMPLETION_FIRST_PHASE   1
3208
#define COMPLETION_KEYWORDS      1
3209
#define COMPLETION_PRAGMAS       2
3210
#define COMPLETION_FUNCTIONS     3
3211
#define COMPLETION_COLLATIONS    4
3212
#define COMPLETION_INDEXES       5
3213
#define COMPLETION_TRIGGERS      6
3214
#define COMPLETION_DATABASES     7
3215
#define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
3216
#define COMPLETION_COLUMNS       9
3217
#define COMPLETION_MODULES       10
3218
#define COMPLETION_EOF           11
3219
 
3220
/*
3221
** The completionConnect() method is invoked to create a new
3222
** completion_vtab that describes the completion virtual table.
3223
**
3224
** Think of this routine as the constructor for completion_vtab objects.
3225
**
3226
** All this routine needs to do is:
3227
**
3228
**    (1) Allocate the completion_vtab object and initialize all fields.
3229
**
3230
**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3231
**        result set of queries against completion will look like.
3232
*/
3233
static int completionConnect(
3234
  sqlite3 *db,
3235
  void *pAux,
3236
  int argc, const char *const*argv,
3237
  sqlite3_vtab **ppVtab,
3238
  char **pzErr
3239
){
3240
  completion_vtab *pNew;
3241
  int rc;
3242
 
3243
  (void)(pAux);    /* Unused parameter */
3244
  (void)(argc);    /* Unused parameter */
3245
  (void)(argv);    /* Unused parameter */
3246
  (void)(pzErr);   /* Unused parameter */
3247
 
3248
/* Column numbers */
3249
#define COMPLETION_COLUMN_CANDIDATE 0  /* Suggested completion of the input */
3250
#define COMPLETION_COLUMN_PREFIX    1  /* Prefix of the word to be completed */
3251
#define COMPLETION_COLUMN_WHOLELINE 2  /* Entire line seen so far */
3252
#define COMPLETION_COLUMN_PHASE     3  /* ePhase - used for debugging only */
3253
 
3254
  sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3255
  rc = sqlite3_declare_vtab(db,
3256
      "CREATE TABLE x("
3257
      "  candidate TEXT,"
3258
      "  prefix TEXT HIDDEN,"
3259
      "  wholeline TEXT HIDDEN,"
3260
      "  phase INT HIDDEN"        /* Used for debugging only */
3261
      ")");
3262
  if( rc==SQLITE_OK ){
3263
    pNew = sqlite3_malloc( sizeof(*pNew) );
3264
    *ppVtab = (sqlite3_vtab*)pNew;
3265
    if( pNew==0 ) return SQLITE_NOMEM;
3266
    memset(pNew, 0, sizeof(*pNew));
3267
    pNew->db = db;
3268
  }
3269
  return rc;
3270
}
3271
 
3272
/*
3273
** This method is the destructor for completion_cursor objects.
3274
*/
3275
static int completionDisconnect(sqlite3_vtab *pVtab){
3276
  sqlite3_free(pVtab);
3277
  return SQLITE_OK;
3278
}
3279
 
3280
/*
3281
** Constructor for a new completion_cursor object.
3282
*/
3283
static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3284
  completion_cursor *pCur;
3285
  pCur = sqlite3_malloc( sizeof(*pCur) );
3286
  if( pCur==0 ) return SQLITE_NOMEM;
3287
  memset(pCur, 0, sizeof(*pCur));
3288
  pCur->db = ((completion_vtab*)p)->db;
3289
  *ppCursor = &pCur->base;
3290
  return SQLITE_OK;
3291
}
3292
 
3293
/*
3294
** Reset the completion_cursor.
3295
*/
3296
static void completionCursorReset(completion_cursor *pCur){
3297
  sqlite3_free(pCur->zPrefix);   pCur->zPrefix = 0;  pCur->nPrefix = 0;
3298
  sqlite3_free(pCur->zLine);     pCur->zLine = 0;    pCur->nLine = 0;
3299
  sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3300
  pCur->j = 0;
3301
}
3302
 
3303
/*
3304
** Destructor for a completion_cursor.
3305
*/
3306
static int completionClose(sqlite3_vtab_cursor *cur){
3307
  completionCursorReset((completion_cursor*)cur);
3308
  sqlite3_free(cur);
3309
  return SQLITE_OK;
3310
}
3311
 
3312
/*
3313
** Advance a completion_cursor to its next row of output.
3314
**
3315
** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3316
** record the current state of the scan.  This routine sets ->zCurrentRow
3317
** to the current row of output and then returns.  If no more rows remain,
3318
** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3319
** table that has reached the end of its scan.
3320
**
3321
** The current implementation just lists potential identifiers and
3322
** keywords and filters them by zPrefix.  Future enhancements should
3323
** take zLine into account to try to restrict the set of identifiers and
3324
** keywords based on what would be legal at the current point of input.
3325
*/
3326
static int completionNext(sqlite3_vtab_cursor *cur){
3327
  completion_cursor *pCur = (completion_cursor*)cur;
3328
  int eNextPhase = 0;  /* Next phase to try if current phase reaches end */
3329
  int iCol = -1;       /* If >=0, step pCur->pStmt and use the i-th column */
3330
  pCur->iRowid++;
3331
  while( pCur->ePhase!=COMPLETION_EOF ){
3332
    switch( pCur->ePhase ){
3333
      case COMPLETION_KEYWORDS: {
3334
        if( pCur->j >= sqlite3_keyword_count() ){
3335
          pCur->zCurrentRow = 0;
3336
          pCur->ePhase = COMPLETION_DATABASES;
3337
        }else{
3338
          sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3339
        }
3340
        iCol = -1;
3341
        break;
3342
      }
3343
      case COMPLETION_DATABASES: {
3344
        if( pCur->pStmt==0 ){
3345
          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3346
                             &pCur->pStmt, 0);
3347
        }
3348
        iCol = 1;
3349
        eNextPhase = COMPLETION_TABLES;
3350
        break;
3351
      }
3352
      case COMPLETION_TABLES: {
3353
        if( pCur->pStmt==0 ){
3354
          sqlite3_stmt *pS2;
3355
          char *zSql = 0;
3356
          const char *zSep = "";
3357
          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3358
          while( sqlite3_step(pS2)==SQLITE_ROW ){
3359
            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3360
            zSql = sqlite3_mprintf(
3361
               "%z%s"
3362
               "SELECT name FROM \"%w\".sqlite_schema",
3363
               zSql, zSep, zDb
3364
            );
3365
            if( zSql==0 ) return SQLITE_NOMEM;
3366
            zSep = " UNION ";
3367
          }
3368
          sqlite3_finalize(pS2);
3369
          sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3370
          sqlite3_free(zSql);
3371
        }
3372
        iCol = 0;
3373
        eNextPhase = COMPLETION_COLUMNS;
3374
        break;
3375
      }
3376
      case COMPLETION_COLUMNS: {
3377
        if( pCur->pStmt==0 ){
3378
          sqlite3_stmt *pS2;
3379
          char *zSql = 0;
3380
          const char *zSep = "";
3381
          sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3382
          while( sqlite3_step(pS2)==SQLITE_ROW ){
3383
            const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3384
            zSql = sqlite3_mprintf(
3385
               "%z%s"
3386
               "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3387
                       " JOIN pragma_table_info(sm.name,%Q) AS pti"
3388
               " WHERE sm.type='table'",
3389
               zSql, zSep, zDb, zDb
3390
            );
3391
            if( zSql==0 ) return SQLITE_NOMEM;
3392
            zSep = " UNION ";
3393
          }
3394
          sqlite3_finalize(pS2);
3395
          sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3396
          sqlite3_free(zSql);
3397
        }
3398
        iCol = 0;
3399
        eNextPhase = COMPLETION_EOF;
3400
        break;
3401
      }
3402
    }
3403
    if( iCol<0 ){
3404
      /* This case is when the phase presets zCurrentRow */
3405
      if( pCur->zCurrentRow==0 ) continue;
3406
    }else{
3407
      if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3408
        /* Extract the next row of content */
3409
        pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3410
        pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3411
      }else{
3412
        /* When all rows are finished, advance to the next phase */
3413
        sqlite3_finalize(pCur->pStmt);
3414
        pCur->pStmt = 0;
3415
        pCur->ePhase = eNextPhase;
3416
        continue;
3417
      }
3418
    }
3419
    if( pCur->nPrefix==0 ) break;
3420
    if( pCur->nPrefix<=pCur->szRow
3421
     && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3422
    ){
3423
      break;
3424
    }
3425
  }
3426
 
3427
  return SQLITE_OK;
3428
}
3429
 
3430
/*
3431
** Return values of columns for the row at which the completion_cursor
3432
** is currently pointing.
3433
*/
3434
static int completionColumn(
3435
  sqlite3_vtab_cursor *cur,   /* The cursor */
3436
  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
3437
  int i                       /* Which column to return */
3438
){
3439
  completion_cursor *pCur = (completion_cursor*)cur;
3440
  switch( i ){
3441
    case COMPLETION_COLUMN_CANDIDATE: {
3442
      sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3443
      break;
3444
    }
3445
    case COMPLETION_COLUMN_PREFIX: {
3446
      sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3447
      break;
3448
    }
3449
    case COMPLETION_COLUMN_WHOLELINE: {
3450
      sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3451
      break;
3452
    }
3453
    case COMPLETION_COLUMN_PHASE: {
3454
      sqlite3_result_int(ctx, pCur->ePhase);
3455
      break;
3456
    }
3457
  }
3458
  return SQLITE_OK;
3459
}
3460
 
3461
/*
3462
** Return the rowid for the current row.  In this implementation, the
3463
** rowid is the same as the output value.
3464
*/
3465
static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3466
  completion_cursor *pCur = (completion_cursor*)cur;
3467
  *pRowid = pCur->iRowid;
3468
  return SQLITE_OK;
3469
}
3470
 
3471
/*
3472
** Return TRUE if the cursor has been moved off of the last
3473
** row of output.
3474
*/
3475
static int completionEof(sqlite3_vtab_cursor *cur){
3476
  completion_cursor *pCur = (completion_cursor*)cur;
3477
  return pCur->ePhase >= COMPLETION_EOF;
3478
}
3479
 
3480
/*
3481
** This method is called to "rewind" the completion_cursor object back
3482
** to the first row of output.  This method is always called at least
3483
** once prior to any call to completionColumn() or completionRowid() or
3484
** completionEof().
3485
*/
3486
static int completionFilter(
3487
  sqlite3_vtab_cursor *pVtabCursor,
3488
  int idxNum, const char *idxStr,
3489
  int argc, sqlite3_value **argv
3490
){
3491
  completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3492
  int iArg = 0;
3493
  (void)(idxStr);   /* Unused parameter */
3494
  (void)(argc);     /* Unused parameter */
3495
  completionCursorReset(pCur);
3496
  if( idxNum & 1 ){
3497
    pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3498
    if( pCur->nPrefix>0 ){
3499
      pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3500
      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3501
    }
3502
    iArg = 1;
3503
  }
3504
  if( idxNum & 2 ){
3505
    pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3506
    if( pCur->nLine>0 ){
3507
      pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3508
      if( pCur->zLine==0 ) return SQLITE_NOMEM;
3509
    }
3510
  }
3511
  if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3512
    int i = pCur->nLine;
3513
    while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3514
      i--;
3515
    }
3516
    pCur->nPrefix = pCur->nLine - i;
3517
    if( pCur->nPrefix>0 ){
3518
      pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3519
      if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3520
    }
3521
  }
3522
  pCur->iRowid = 0;
3523
  pCur->ePhase = COMPLETION_FIRST_PHASE;
3524
  return completionNext(pVtabCursor);
3525
}
3526
 
3527
/*
3528
** SQLite will invoke this method one or more times while planning a query
3529
** that uses the completion virtual table.  This routine needs to create
3530
** a query plan for each invocation and compute an estimated cost for that
3531
** plan.
3532
**
3533
** There are two hidden parameters that act as arguments to the table-valued
3534
** function:  "prefix" and "wholeline".  Bit 0 of idxNum is set if "prefix"
3535
** is available and bit 1 is set if "wholeline" is available.
3536
*/
3537
static int completionBestIndex(
3538
  sqlite3_vtab *tab,
3539
  sqlite3_index_info *pIdxInfo
3540
){
3541
  int i;                 /* Loop over constraints */
3542
  int idxNum = 0;        /* The query plan bitmask */
3543
  int prefixIdx = -1;    /* Index of the start= constraint, or -1 if none */
3544
  int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3545
  int nArg = 0;          /* Number of arguments that completeFilter() expects */
3546
  const struct sqlite3_index_constraint *pConstraint;
3547
 
3548
  (void)(tab);    /* Unused parameter */
3549
  pConstraint = pIdxInfo->aConstraint;
3550
  for(i=0; inConstraint; i++, pConstraint++){
3551
    if( pConstraint->usable==0 ) continue;
3552
    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3553
    switch( pConstraint->iColumn ){
3554
      case COMPLETION_COLUMN_PREFIX:
3555
        prefixIdx = i;
3556
        idxNum |= 1;
3557
        break;
3558
      case COMPLETION_COLUMN_WHOLELINE:
3559
        wholelineIdx = i;
3560
        idxNum |= 2;
3561
        break;
3562
    }
3563
  }
3564
  if( prefixIdx>=0 ){
3565
    pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3566
    pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3567
  }
3568
  if( wholelineIdx>=0 ){
3569
    pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3570
    pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3571
  }
3572
  pIdxInfo->idxNum = idxNum;
3573
  pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3574
  pIdxInfo->estimatedRows = 500 - 100*nArg;
3575
  return SQLITE_OK;
3576
}
3577
 
3578
/*
3579
** This following structure defines all the methods for the
3580
** completion virtual table.
3581
*/
3582
static sqlite3_module completionModule = {
3583
  0,                         /* iVersion */
3584
  0,                         /* xCreate */
3585
  completionConnect,         /* xConnect */
3586
  completionBestIndex,       /* xBestIndex */
3587
  completionDisconnect,      /* xDisconnect */
3588
  0,                         /* xDestroy */
3589
  completionOpen,            /* xOpen - open a cursor */
3590
  completionClose,           /* xClose - close a cursor */
3591
  completionFilter,          /* xFilter - configure scan constraints */
3592
  completionNext,            /* xNext - advance a cursor */
3593
  completionEof,             /* xEof - check for end of scan */
3594
  completionColumn,          /* xColumn - read data */
3595
  completionRowid,           /* xRowid - read data */
3596
  0,                         /* xUpdate */
3597
  0,                         /* xBegin */
3598
  0,                         /* xSync */
3599
  0,                         /* xCommit */
3600
  0,                         /* xRollback */
3601
  0,                         /* xFindMethod */
3602
  0,                         /* xRename */
3603
  0,                         /* xSavepoint */
3604
  0,                         /* xRelease */
3605
  0,                         /* xRollbackTo */
3606
 
3607
};
3608
 
3609
#endif /* SQLITE_OMIT_VIRTUALTABLE */
3610
 
3611
int sqlite3CompletionVtabInit(sqlite3 *db){
3612
  int rc = SQLITE_OK;
3613
#ifndef SQLITE_OMIT_VIRTUALTABLE
3614
  rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3615
#endif
3616
  return rc;
3617
}
3618
 
3619
#ifdef _WIN32
3620
 
3621
#endif
3622
int sqlite3_completion_init(
3623
  sqlite3 *db,
3624
  char **pzErrMsg,
3625
  const sqlite3_api_routines *pApi
3626
){
3627
  int rc = SQLITE_OK;
3628
  SQLITE_EXTENSION_INIT2(pApi);
3629
  (void)(pzErrMsg);  /* Unused parameter */
3630
#ifndef SQLITE_OMIT_VIRTUALTABLE
3631
  rc = sqlite3CompletionVtabInit(db);
3632
#endif
3633
  return rc;
3634
}
3635
 
3636
/************************* End ../ext/misc/completion.c ********************/
3637
/************************* Begin ../ext/misc/appendvfs.c ******************/
3638
/*
3639
** 2017-10-20
3640
**
3641
** The author disclaims copyright to this source code.  In place of
3642
** a legal notice, here is a blessing:
3643
**
3644
**    May you do good and not evil.
3645
**    May you find forgiveness for yourself and forgive others.
3646
**    May you share freely, never taking more than you give.
3647
**
3648
******************************************************************************
3649
**
3650
** This file implements a VFS shim that allows an SQLite database to be
3651
** appended onto the end of some other file, such as an executable.
3652
**
3653
** A special record must appear at the end of the file that identifies the
3654
** file as an appended database and provides the offset to the first page
3655
** of the exposed content. (Or, it is the length of the content prefix.)
3656
** For best performance page 1 should be located at a disk page boundary,
3657
** though that is not required.
3658
**
3659
** When opening a database using this VFS, the connection might treat
3660
** the file as an ordinary SQLite database, or it might treat it as a
3661
** database appended onto some other file.  The decision is made by
3662
** applying the following rules in order:
3663
**
3664
**  (1)  An empty file is an ordinary database.
3665
**
3666
**  (2)  If the file ends with the appendvfs trailer string
3667
**       "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
3668
**
3669
**  (3)  If the file begins with the standard SQLite prefix string
3670
**       "SQLite format 3", that file is an ordinary database.
3671
**
3672
**  (4)  If none of the above apply and the SQLITE_OPEN_CREATE flag is
3673
**       set, then a new database is appended to the already existing file.
3674
**
3675
**  (5)  Otherwise, SQLITE_CANTOPEN is returned.
3676
**
3677
** To avoid unnecessary complications with the PENDING_BYTE, the size of
3678
** the file containing the database is limited to 1GiB. (1073741824 bytes)
3679
** This VFS will not read or write past the 1GiB mark.  This restriction
3680
** might be lifted in future versions.  For now, if you need a larger
3681
** database, then keep it in a separate file.
3682
**
3683
** If the file being opened is a plain database (not an appended one), then
3684
** this shim is a pass-through into the default underlying VFS. (rule 3)
3685
**/
3686
/* #include "sqlite3ext.h" */
3687
SQLITE_EXTENSION_INIT1
3688
#include 
3689
#include 
3690
 
3691
/* The append mark at the end of the database is:
3692
**
3693
**     Start-Of-SQLite3-NNNNNNNN
3694
**     123456789 123456789 12345
3695
**
3696
** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3697
** the offset to page 1, and also the length of the prefix content.
3698
*/
3699
#define APND_MARK_PREFIX     "Start-Of-SQLite3-"
3700
#define APND_MARK_PREFIX_SZ  17
3701
#define APND_MARK_FOS_SZ      8
3702
#define APND_MARK_SIZE       (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
3703
 
3704
/*
3705
** Maximum size of the combined prefix + database + append-mark.  This
3706
** must be less than 0x40000000 to avoid locking issues on Windows.
3707
*/
3708
#define APND_MAX_SIZE  (0x40000000)
3709
 
3710
/*
3711
** Try to align the database to an even multiple of APND_ROUNDUP bytes.
3712
*/
3713
#ifndef APND_ROUNDUP
3714
#define APND_ROUNDUP 4096
3715
#endif
3716
#define APND_ALIGN_MASK         ((sqlite3_int64)(APND_ROUNDUP-1))
3717
#define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
3718
 
3719
/*
3720
** Forward declaration of objects used by this utility
3721
*/
3722
typedef struct sqlite3_vfs ApndVfs;
3723
typedef struct ApndFile ApndFile;
3724
 
3725
/* Access to a lower-level VFS that (might) implement dynamic loading,
3726
** access to randomness, etc.
3727
*/
3728
#define ORIGVFS(p)  ((sqlite3_vfs*)((p)->pAppData))
3729
#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3730
 
3731
/* An open appendvfs file
3732
**
3733
** An instance of this structure describes the appended database file.
3734
** A separate sqlite3_file object is always appended. The appended
3735
** sqlite3_file object (which can be accessed using ORIGFILE()) describes
3736
** the entire file, including the prefix, the database, and the
3737
** append-mark.
3738
**
3739
** The structure of an AppendVFS database is like this:
3740
**
3741
**   +-------------+---------+----------+-------------+
3742
**   | prefix-file | padding | database | append-mark |
3743
**   +-------------+---------+----------+-------------+
3744
**                           ^          ^
3745
**                           |          |
3746
**                         iPgOne      iMark
3747
**
3748
**
3749
** "prefix file" -  file onto which the database has been appended.
3750
** "padding"     -  zero or more bytes inserted so that "database"
3751
**                  starts on an APND_ROUNDUP boundary
3752
** "database"    -  The SQLite database file
3753
** "append-mark" -  The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
3754
**                  the offset from the start of prefix-file to the start
3755
**                  of "database".
3756
**
3757
** The size of the database is iMark - iPgOne.
3758
**
3759
** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
3760
** of iPgOne stored as a big-ending 64-bit integer.
3761
**
3762
** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
3763
** Or, iMark is -1 to indicate that it has not yet been written.
3764
*/
3765
struct ApndFile {
3766
  sqlite3_file base;        /* Subclass.  MUST BE FIRST! */
3767
  sqlite3_int64 iPgOne;     /* Offset to the start of the database */
3768
  sqlite3_int64 iMark;      /* Offset of the append mark.  -1 if unwritten */
3769
  /* Always followed by another sqlite3_file that describes the whole file */
3770
};
3771
 
3772
/*
3773
** Methods for ApndFile
3774
*/
3775
static int apndClose(sqlite3_file*);
3776
static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3777
static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3778
static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3779
static int apndSync(sqlite3_file*, int flags);
3780
static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3781
static int apndLock(sqlite3_file*, int);
3782
static int apndUnlock(sqlite3_file*, int);
3783
static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3784
static int apndFileControl(sqlite3_file*, int op, void *pArg);
3785
static int apndSectorSize(sqlite3_file*);
3786
static int apndDeviceCharacteristics(sqlite3_file*);
3787
static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3788
static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3789
static void apndShmBarrier(sqlite3_file*);
3790
static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3791
static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3792
static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3793
 
3794
/*
3795
** Methods for ApndVfs
3796
*/
3797
static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3798
static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3799
static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3800
static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3801
static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3802
static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3803
static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3804
static void apndDlClose(sqlite3_vfs*, void*);
3805
static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3806
static int apndSleep(sqlite3_vfs*, int microseconds);
3807
static int apndCurrentTime(sqlite3_vfs*, double*);
3808
static int apndGetLastError(sqlite3_vfs*, int, char *);
3809
static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3810
static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3811
static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3812
static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3813
 
3814
static sqlite3_vfs apnd_vfs = {
3815
  3,                            /* iVersion (set when registered) */
3816
  0,                            /* szOsFile (set when registered) */
3817
  1024,                         /* mxPathname */
3818
  0,                            /* pNext */
3819
  "apndvfs",                    /* zName */
3820
  0,                            /* pAppData (set when registered) */
3821
  apndOpen,                     /* xOpen */
3822
  apndDelete,                   /* xDelete */
3823
  apndAccess,                   /* xAccess */
3824
  apndFullPathname,             /* xFullPathname */
3825
  apndDlOpen,                   /* xDlOpen */
3826
  apndDlError,                  /* xDlError */
3827
  apndDlSym,                    /* xDlSym */
3828
  apndDlClose,                  /* xDlClose */
3829
  apndRandomness,               /* xRandomness */
3830
  apndSleep,                    /* xSleep */
3831
  apndCurrentTime,              /* xCurrentTime */
3832
  apndGetLastError,             /* xGetLastError */
3833
  apndCurrentTimeInt64,         /* xCurrentTimeInt64 */
3834
  apndSetSystemCall,            /* xSetSystemCall */
3835
  apndGetSystemCall,            /* xGetSystemCall */
3836
  apndNextSystemCall            /* xNextSystemCall */
3837
};
3838
 
3839
static const sqlite3_io_methods apnd_io_methods = {
3840
  3,                              /* iVersion */
3841
  apndClose,                      /* xClose */
3842
  apndRead,                       /* xRead */
3843
  apndWrite,                      /* xWrite */
3844
  apndTruncate,                   /* xTruncate */
3845
  apndSync,                       /* xSync */
3846
  apndFileSize,                   /* xFileSize */
3847
  apndLock,                       /* xLock */
3848
  apndUnlock,                     /* xUnlock */
3849
  apndCheckReservedLock,          /* xCheckReservedLock */
3850
  apndFileControl,                /* xFileControl */
3851
  apndSectorSize,                 /* xSectorSize */
3852
  apndDeviceCharacteristics,      /* xDeviceCharacteristics */
3853
  apndShmMap,                     /* xShmMap */
3854
  apndShmLock,                    /* xShmLock */
3855
  apndShmBarrier,                 /* xShmBarrier */
3856
  apndShmUnmap,                   /* xShmUnmap */
3857
  apndFetch,                      /* xFetch */
3858
  apndUnfetch                     /* xUnfetch */
3859
};
3860
 
3861
/*
3862
** Close an apnd-file.
3863
*/
3864
static int apndClose(sqlite3_file *pFile){
3865
  pFile = ORIGFILE(pFile);
3866
  return pFile->pMethods->xClose(pFile);
3867
}
3868
 
3869
/*
3870
** Read data from an apnd-file.
3871
*/
3872
static int apndRead(
3873
  sqlite3_file *pFile,
3874
  void *zBuf,
3875
  int iAmt,
3876
  sqlite_int64 iOfst
3877
){
3878
  ApndFile *paf = (ApndFile *)pFile;
3879
  pFile = ORIGFILE(pFile);
3880
  return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3881
}
3882
 
3883
/*
3884
** Add the append-mark onto what should become the end of the file.
3885
*  If and only if this succeeds, internal ApndFile.iMark is updated.
3886
*  Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
3887
*/
3888
static int apndWriteMark(
3889
  ApndFile *paf,
3890
  sqlite3_file *pFile,
3891
  sqlite_int64 iWriteEnd
3892
){
3893
  sqlite_int64 iPgOne = paf->iPgOne;
3894
  unsigned char a[APND_MARK_SIZE];
3895
  int i = APND_MARK_FOS_SZ;
3896
  int rc;
3897
  assert(pFile == ORIGFILE(paf));
3898
  memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3899
  while( --i >= 0 ){
3900
    a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
3901
    iPgOne >>= 8;
3902
  }
3903
  iWriteEnd += paf->iPgOne;
3904
  if( SQLITE_OK==(rc = pFile->pMethods->xWrite
3905
                  (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
3906
    paf->iMark = iWriteEnd;
3907
  }
3908
  return rc;
3909
}
3910
 
3911
/*
3912
** Write data to an apnd-file.
3913
*/
3914
static int apndWrite(
3915
  sqlite3_file *pFile,
3916
  const void *zBuf,
3917
  int iAmt,
3918
  sqlite_int64 iOfst
3919
){
3920
  ApndFile *paf = (ApndFile *)pFile;
3921
  sqlite_int64 iWriteEnd = iOfst + iAmt;
3922
  if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
3923
  pFile = ORIGFILE(pFile);
3924
  /* If append-mark is absent or will be overwritten, write it. */
3925
  if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
3926
    int rc = apndWriteMark(paf, pFile, iWriteEnd);
3927
    if( SQLITE_OK!=rc ) return rc;
3928
  }
3929
  return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3930
}
3931
 
3932
/*
3933
** Truncate an apnd-file.
3934
*/
3935
static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3936
  ApndFile *paf = (ApndFile *)pFile;
3937
  pFile = ORIGFILE(pFile);
3938
  /* The append mark goes out first so truncate failure does not lose it. */
3939
  if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
3940
  /* Truncate underlying file just past append mark */
3941
  return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
3942
}
3943
 
3944
/*
3945
** Sync an apnd-file.
3946
*/
3947
static int apndSync(sqlite3_file *pFile, int flags){
3948
  pFile = ORIGFILE(pFile);
3949
  return pFile->pMethods->xSync(pFile, flags);
3950
}
3951
 
3952
/*
3953
** Return the current file-size of an apnd-file.
3954
** If the append mark is not yet there, the file-size is 0.
3955
*/
3956
static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3957
  ApndFile *paf = (ApndFile *)pFile;
3958
  *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
3959
  return SQLITE_OK;
3960
}
3961
 
3962
/*
3963
** Lock an apnd-file.
3964
*/
3965
static int apndLock(sqlite3_file *pFile, int eLock){
3966
  pFile = ORIGFILE(pFile);
3967
  return pFile->pMethods->xLock(pFile, eLock);
3968
}
3969
 
3970
/*
3971
** Unlock an apnd-file.
3972
*/
3973
static int apndUnlock(sqlite3_file *pFile, int eLock){
3974
  pFile = ORIGFILE(pFile);
3975
  return pFile->pMethods->xUnlock(pFile, eLock);
3976
}
3977
 
3978
/*
3979
** Check if another file-handle holds a RESERVED lock on an apnd-file.
3980
*/
3981
static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3982
  pFile = ORIGFILE(pFile);
3983
  return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3984
}
3985
 
3986
/*
3987
** File control method. For custom operations on an apnd-file.
3988
*/
3989
static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3990
  ApndFile *paf = (ApndFile *)pFile;
3991
  int rc;
3992
  pFile = ORIGFILE(pFile);
3993
  if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
3994
  rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3995
  if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3996
    *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
3997
  }
3998
  return rc;
3999
}
4000
 
4001
/*
4002
** Return the sector-size in bytes for an apnd-file.
4003
*/
4004
static int apndSectorSize(sqlite3_file *pFile){
4005
  pFile = ORIGFILE(pFile);
4006
  return pFile->pMethods->xSectorSize(pFile);
4007
}
4008
 
4009
/*
4010
** Return the device characteristic flags supported by an apnd-file.
4011
*/
4012
static int apndDeviceCharacteristics(sqlite3_file *pFile){
4013
  pFile = ORIGFILE(pFile);
4014
  return pFile->pMethods->xDeviceCharacteristics(pFile);
4015
}
4016
 
4017
/* Create a shared memory file mapping */
4018
static int apndShmMap(
4019
  sqlite3_file *pFile,
4020
  int iPg,
4021
  int pgsz,
4022
  int bExtend,
4023
  void volatile **pp
4024
){
4025
  pFile = ORIGFILE(pFile);
4026
  return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
4027
}
4028
 
4029
/* Perform locking on a shared-memory segment */
4030
static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
4031
  pFile = ORIGFILE(pFile);
4032
  return pFile->pMethods->xShmLock(pFile,offset,n,flags);
4033
}
4034
 
4035
/* Memory barrier operation on shared memory */
4036
static void apndShmBarrier(sqlite3_file *pFile){
4037
  pFile = ORIGFILE(pFile);
4038
  pFile->pMethods->xShmBarrier(pFile);
4039
}
4040
 
4041
/* Unmap a shared memory segment */
4042
static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
4043
  pFile = ORIGFILE(pFile);
4044
  return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
4045
}
4046
 
4047
/* Fetch a page of a memory-mapped file */
4048
static int apndFetch(
4049
  sqlite3_file *pFile,
4050
  sqlite3_int64 iOfst,
4051
  int iAmt,
4052
  void **pp
4053
){
4054
  ApndFile *p = (ApndFile *)pFile;
4055
  if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
4056
    return SQLITE_IOERR; /* Cannot read what is not yet there. */
4057
  }
4058
  pFile = ORIGFILE(pFile);
4059
  return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4060
}
4061
 
4062
/* Release a memory-mapped page */
4063
static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4064
  ApndFile *p = (ApndFile *)pFile;
4065
  pFile = ORIGFILE(pFile);
4066
  return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4067
}
4068
 
4069
/*
4070
** Try to read the append-mark off the end of a file.  Return the
4071
** start of the appended database if the append-mark is present.
4072
** If there is no valid append-mark, return -1;
4073
**
4074
** An append-mark is only valid if the NNNNNNNN start-of-database offset
4075
** indicates that the appended database contains at least one page.  The
4076
** start-of-database value must be a multiple of 512.
4077
*/
4078
static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4079
  int rc, i;
4080
  sqlite3_int64 iMark;
4081
  int msbs = 8 * (APND_MARK_FOS_SZ-1);
4082
  unsigned char a[APND_MARK_SIZE];
4083
 
4084
  if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
4085
  rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4086
  if( rc ) return -1;
4087
  if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4088
  iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
4089
  for(i=1; i<8; i++){
4090
    msbs -= 8;
4091
    iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<
4092
  }
4093
  if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
4094
  if( iMark & 0x1ff ) return -1;
4095
  return iMark;
4096
}
4097
 
4098
static const char apvfsSqliteHdr[] = "SQLite format 3";
4099
/*
4100
** Check to see if the file is an appendvfs SQLite database file.
4101
** Return true iff it is such. Parameter sz is the file's size.
4102
*/
4103
static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
4104
  int rc;
4105
  char zHdr[16];
4106
  sqlite3_int64 iMark = apndReadMark(sz, pFile);
4107
  if( iMark>=0 ){
4108
    /* If file has the correct end-marker, the expected odd size, and the
4109
    ** SQLite DB type marker where the end-marker puts it, then it
4110
    ** is an appendvfs database.
4111
    */
4112
    rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
4113
    if( SQLITE_OK==rc
4114
     && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
4115
     && (sz & 0x1ff) == APND_MARK_SIZE
4116
     && sz>=512+APND_MARK_SIZE
4117
    ){
4118
      return 1; /* It's an appendvfs database */
4119
    }
4120
  }
4121
  return 0;
4122
}
4123
 
4124
/*
4125
** Check to see if the file is an ordinary SQLite database file.
4126
** Return true iff so. Parameter sz is the file's size.
4127
*/
4128
static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4129
  char zHdr[16];
4130
  if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
4131
   || (sz & 0x1ff) != 0
4132
   || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
4133
   || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
4134
  ){
4135
    return 0;
4136
  }else{
4137
    return 1;
4138
  }
4139
}
4140
 
4141
/*
4142
** Open an apnd file handle.
4143
*/
4144
static int apndOpen(
4145
  sqlite3_vfs *pApndVfs,
4146
  const char *zName,
4147
  sqlite3_file *pFile,
4148
  int flags,
4149
  int *pOutFlags
4150
){
4151
  ApndFile *pApndFile = (ApndFile*)pFile;
4152
  sqlite3_file *pBaseFile = ORIGFILE(pFile);
4153
  sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
4154
  int rc;
4155
  sqlite3_int64 sz = 0;
4156
  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4157
    /* The appendvfs is not to be used for transient or temporary databases.
4158
    ** Just use the base VFS open to initialize the given file object and
4159
    ** open the underlying file. (Appendvfs is then unused for this file.)
4160
    */
4161
    return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
4162
  }
4163
  memset(pApndFile, 0, sizeof(ApndFile));
4164
  pFile->pMethods = &apnd_io_methods;
4165
  pApndFile->iMark = -1;    /* Append mark not yet written */
4166
 
4167
  rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
4168
  if( rc==SQLITE_OK ){
4169
    rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
4170
  }
4171
  if( rc ){
4172
    pBaseFile->pMethods->xClose(pBaseFile);
4173
    pFile->pMethods = 0;
4174
    return rc;
4175
  }
4176
  if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
4177
    /* The file being opened appears to be just an ordinary DB. Copy
4178
    ** the base dispatch-table so this instance mimics the base VFS.
4179
    */
4180
    memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
4181
    return SQLITE_OK;
4182
  }
4183
  pApndFile->iPgOne = apndReadMark(sz, pFile);
4184
  if( pApndFile->iPgOne>=0 ){
4185
    pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
4186
    return SQLITE_OK;
4187
  }
4188
  if( (flags & SQLITE_OPEN_CREATE)==0 ){
4189
    pBaseFile->pMethods->xClose(pBaseFile);
4190
    rc = SQLITE_CANTOPEN;
4191
    pFile->pMethods = 0;
4192
  }else{
4193
    /* Round newly added appendvfs location to #define'd page boundary.
4194
    ** Note that nothing has yet been written to the underlying file.
4195
    ** The append mark will be written along with first content write.
4196
    ** Until then, paf->iMark value indicates it is not yet written.
4197
    */
4198
    pApndFile->iPgOne = APND_START_ROUNDUP(sz);
4199
  }
4200
  return rc;
4201
}
4202
 
4203
/*
4204
** Delete an apnd file.
4205
** For an appendvfs, this could mean delete the appendvfs portion,
4206
** leaving the appendee as it was before it gained an appendvfs.
4207
** For now, this code deletes the underlying file too.
4208
*/
4209
static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4210
  return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4211
}
4212
 
4213
/*
4214
** All other VFS methods are pass-thrus.
4215
*/
4216
static int apndAccess(
4217
  sqlite3_vfs *pVfs,
4218
  const char *zPath,
4219
  int flags,
4220
  int *pResOut
4221
){
4222
  return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4223
}
4224
static int apndFullPathname(
4225
  sqlite3_vfs *pVfs,
4226
  const char *zPath,
4227
  int nOut,
4228
  char *zOut
4229
){
4230
  return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4231
}
4232
static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4233
  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4234
}
4235
static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4236
  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4237
}
4238
static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4239
  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4240
}
4241
static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4242
  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4243
}
4244
static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4245
  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4246
}
4247
static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4248
  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4249
}
4250
static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4251
  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4252
}
4253
static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4254
  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4255
}
4256
static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4257
  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4258
}
4259
static int apndSetSystemCall(
4260
  sqlite3_vfs *pVfs,
4261
  const char *zName,
4262
  sqlite3_syscall_ptr pCall
4263
){
4264
  return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4265
}
4266
static sqlite3_syscall_ptr apndGetSystemCall(
4267
  sqlite3_vfs *pVfs,
4268
  const char *zName
4269
){
4270
  return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4271
}
4272
static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4273
  return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4274
}
4275
 
4276
 
4277
#ifdef _WIN32
4278
 
4279
#endif
4280
/*
4281
** This routine is called when the extension is loaded.
4282
** Register the new VFS.
4283
*/
4284
int sqlite3_appendvfs_init(
4285
  sqlite3 *db,
4286
  char **pzErrMsg,
4287
  const sqlite3_api_routines *pApi
4288
){
4289
  int rc = SQLITE_OK;
4290
  sqlite3_vfs *pOrig;
4291
  SQLITE_EXTENSION_INIT2(pApi);
4292
  (void)pzErrMsg;
4293
  (void)db;
4294
  pOrig = sqlite3_vfs_find(0);
4295
  apnd_vfs.iVersion = pOrig->iVersion;
4296
  apnd_vfs.pAppData = pOrig;
4297
  apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4298
  rc = sqlite3_vfs_register(&apnd_vfs, 0);
4299
#ifdef APPENDVFS_TEST
4300
  if( rc==SQLITE_OK ){
4301
    rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4302
  }
4303
#endif
4304
  if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4305
  return rc;
4306
}
4307
 
4308
/************************* End ../ext/misc/appendvfs.c ********************/
4309
/************************* Begin ../ext/misc/memtrace.c ******************/
4310
/*
4311
** 2019-01-21
4312
**
4313
** The author disclaims copyright to this source code.  In place of
4314
** a legal notice, here is a blessing:
4315
**
4316
**    May you do good and not evil.
4317
**    May you find forgiveness for yourself and forgive others.
4318
**    May you share freely, never taking more than you give.
4319
**
4320
*************************************************************************
4321
**
4322
** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4323
** mechanism to add a tracing layer on top of SQLite.  If this extension
4324
** is registered prior to sqlite3_initialize(), it will cause all memory
4325
** allocation activities to be logged on standard output, or to some other
4326
** FILE specified by the initializer.
4327
**
4328
** This file needs to be compiled into the application that uses it.
4329
**
4330
** This extension is used to implement the --memtrace option of the
4331
** command-line shell.
4332
*/
4333
#include 
4334
#include 
4335
#include 
4336
 
4337
/* The original memory allocation routines */
4338
static sqlite3_mem_methods memtraceBase;
4339
static FILE *memtraceOut;
4340
 
4341
/* Methods that trace memory allocations */
4342
static void *memtraceMalloc(int n){
4343
  if( memtraceOut ){
4344
    fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4345
            memtraceBase.xRoundup(n));
4346
  }
4347
  return memtraceBase.xMalloc(n);
4348
}
4349
static void memtraceFree(void *p){
4350
  if( p==0 ) return;
4351
  if( memtraceOut ){
4352
    fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4353
  }
4354
  memtraceBase.xFree(p);
4355
}
4356
static void *memtraceRealloc(void *p, int n){
4357
  if( p==0 ) return memtraceMalloc(n);
4358
  if( n==0 ){
4359
    memtraceFree(p);
4360
    return 0;
4361
  }
4362
  if( memtraceOut ){
4363
    fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4364
            memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4365
  }
4366
  return memtraceBase.xRealloc(p, n);
4367
}
4368
static int memtraceSize(void *p){
4369
  return memtraceBase.xSize(p);
4370
}
4371
static int memtraceRoundup(int n){
4372
  return memtraceBase.xRoundup(n);
4373
}
4374
static int memtraceInit(void *p){
4375
  return memtraceBase.xInit(p);
4376
}
4377
static void memtraceShutdown(void *p){
4378
  memtraceBase.xShutdown(p);
4379
}
4380
 
4381
/* The substitute memory allocator */
4382
static sqlite3_mem_methods ersaztMethods = {
4383
  memtraceMalloc,
4384
  memtraceFree,
4385
  memtraceRealloc,
4386
  memtraceSize,
4387
  memtraceRoundup,
4388
  memtraceInit,
4389
  memtraceShutdown,
4390
 
4391
};
4392
 
4393
/* Begin tracing memory allocations to out. */
4394
int sqlite3MemTraceActivate(FILE *out){
4395
  int rc = SQLITE_OK;
4396
  if( memtraceBase.xMalloc==0 ){
4397
    rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4398
    if( rc==SQLITE_OK ){
4399
      rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4400
    }
4401
  }
4402
  memtraceOut = out;
4403
  return rc;
4404
}
4405
 
4406
/* Deactivate memory tracing */
4407
int sqlite3MemTraceDeactivate(void){
4408
  int rc = SQLITE_OK;
4409
  if( memtraceBase.xMalloc!=0 ){
4410
    rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4411
    if( rc==SQLITE_OK ){
4412
      memset(&memtraceBase, 0, sizeof(memtraceBase));
4413
    }
4414
  }
4415
  memtraceOut = 0;
4416
  return rc;
4417
}
4418
 
4419
/************************* End ../ext/misc/memtrace.c ********************/
4420
/************************* Begin ../ext/misc/uint.c ******************/
4421
/*
4422
** 2020-04-14
4423
**
4424
** The author disclaims copyright to this source code.  In place of
4425
** a legal notice, here is a blessing:
4426
**
4427
**    May you do good and not evil.
4428
**    May you find forgiveness for yourself and forgive others.
4429
**    May you share freely, never taking more than you give.
4430
**
4431
******************************************************************************
4432
**
4433
** This SQLite extension implements the UINT collating sequence.
4434
**
4435
** UINT works like BINARY for text, except that embedded strings
4436
** of digits compare in numeric order.
4437
**
4438
**     *   Leading zeros are handled properly, in the sense that
4439
**         they do not mess of the maginitude comparison of embedded
4440
**         strings of digits.  "x00123y" is equal to "x123y".
4441
**
4442
**     *   Only unsigned integers are recognized.  Plus and minus
4443
**         signs are ignored.  Decimal points and exponential notation
4444
**         are ignored.
4445
**
4446
**     *   Embedded integers can be of arbitrary length.  Comparison
4447
**         is *not* limited integers that can be expressed as a
4448
**         64-bit machine integer.
4449
*/
4450
/* #include "sqlite3ext.h" */
4451
SQLITE_EXTENSION_INIT1
4452
#include 
4453
#include 
4454
#include 
4455
 
4456
/*
4457
** Compare text in lexicographic order, except strings of digits
4458
** compare in numeric order.
4459
*/
4460
static int uintCollFunc(
4461
  void *notUsed,
4462
  int nKey1, const void *pKey1,
4463
  int nKey2, const void *pKey2
4464
){
4465
  const unsigned char *zA = (const unsigned char*)pKey1;
4466
  const unsigned char *zB = (const unsigned char*)pKey2;
4467
  int i=0, j=0, x;
4468
  (void)notUsed;
4469
  while( i
4470
    x = zA[i] - zB[j];
4471
    if( isdigit(zA[i]) ){
4472
      int k;
4473
      if( !isdigit(zB[j]) ) return x;
4474
      while( i
4475
      while( j
4476
      k = 0;
4477
      while( i+k
4478
             && j+k
4479
        k++;
4480
      }
4481
      if( i+k
4482
        return +1;
4483
      }else if( j+k
4484
        return -1;
4485
      }else{
4486
        x = memcmp(zA+i, zB+j, k);
4487
        if( x ) return x;
4488
        i += k;
4489
        j += k;
4490
      }
4491
    }else if( x ){
4492
      return x;
4493
    }else{
4494
      i++;
4495
      j++;
4496
    }
4497
  }
4498
  return (nKey1 - i) - (nKey2 - j);
4499
}
4500
 
4501
#ifdef _WIN32
4502
 
4503
#endif
4504
int sqlite3_uint_init(
4505
  sqlite3 *db,
4506
  char **pzErrMsg,
4507
  const sqlite3_api_routines *pApi
4508
){
4509
  SQLITE_EXTENSION_INIT2(pApi);
4510
  (void)pzErrMsg;  /* Unused parameter */
4511
  return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4512
}
4513
 
4514
/************************* End ../ext/misc/uint.c ********************/
4515
/************************* Begin ../ext/misc/decimal.c ******************/
4516
/*
4517
** 2020-06-22
4518
**
4519
** The author disclaims copyright to this source code.  In place of
4520
** a legal notice, here is a blessing:
4521
**
4522
**    May you do good and not evil.
4523
**    May you find forgiveness for yourself and forgive others.
4524
**    May you share freely, never taking more than you give.
4525
**
4526
******************************************************************************
4527
**
4528
** Routines to implement arbitrary-precision decimal math.
4529
**
4530
** The focus here is on simplicity and correctness, not performance.
4531
*/
4532
/* #include "sqlite3ext.h" */
4533
SQLITE_EXTENSION_INIT1
4534
#include 
4535
#include 
4536
#include 
4537
#include 
4538
 
4539
/* Mark a function parameter as unused, to suppress nuisance compiler
4540
** warnings. */
4541
#ifndef UNUSED_PARAMETER
4542
# define UNUSED_PARAMETER(X)  (void)(X)
4543
#endif
4544
 
4545
 
4546
/* A decimal object */
4547
typedef struct Decimal Decimal;
4548
struct Decimal {
4549
  char sign;        /* 0 for positive, 1 for negative */
4550
  char oom;         /* True if an OOM is encountered */
4551
  char isNull;      /* True if holds a NULL rather than a number */
4552
  char isInit;      /* True upon initialization */
4553
  int nDigit;       /* Total number of digits */
4554
  int nFrac;        /* Number of digits to the right of the decimal point */
4555
  signed char *a;   /* Array of digits.  Most significant first. */
4556
};
4557
 
4558
/*
4559
** Release memory held by a Decimal, but do not free the object itself.
4560
*/
4561
static void decimal_clear(Decimal *p){
4562
  sqlite3_free(p->a);
4563
}
4564
 
4565
/*
4566
** Destroy a Decimal object
4567
*/
4568
static void decimal_free(Decimal *p){
4569
  if( p ){
4570
    decimal_clear(p);
4571
    sqlite3_free(p);
4572
  }
4573
}
4574
 
4575
/*
4576
** Allocate a new Decimal object.  Initialize it to the number given
4577
** by the input string.
4578
*/
4579
static Decimal *decimal_new(
4580
  sqlite3_context *pCtx,
4581
  sqlite3_value *pIn,
4582
  int nAlt,
4583
  const unsigned char *zAlt
4584
){
4585
  Decimal *p;
4586
  int n, i;
4587
  const unsigned char *zIn;
4588
  int iExp = 0;
4589
  p = sqlite3_malloc( sizeof(*p) );
4590
  if( p==0 ) goto new_no_mem;
4591
  p->sign = 0;
4592
  p->oom = 0;
4593
  p->isInit = 1;
4594
  p->isNull = 0;
4595
  p->nDigit = 0;
4596
  p->nFrac = 0;
4597
  if( zAlt ){
4598
    n = nAlt,
4599
    zIn = zAlt;
4600
  }else{
4601
    if( sqlite3_value_type(pIn)==SQLITE_NULL ){
4602
      p->a = 0;
4603
      p->isNull = 1;
4604
      return p;
4605
    }
4606
    n = sqlite3_value_bytes(pIn);
4607
    zIn = sqlite3_value_text(pIn);
4608
  }
4609
  p->a = sqlite3_malloc64( n+1 );
4610
  if( p->a==0 ) goto new_no_mem;
4611
  for(i=0; isspace(zIn[i]); i++){}
4612
  if( zIn[i]=='-' ){
4613
    p->sign = 1;
4614
    i++;
4615
  }else if( zIn[i]=='+' ){
4616
    i++;
4617
  }
4618
  while( i
4619
  while( i
4620
    char c = zIn[i];
4621
    if( c>='0' && c<='9' ){
4622
      p->a[p->nDigit++] = c - '0';
4623
    }else if( c=='.' ){
4624
      p->nFrac = p->nDigit + 1;
4625
    }else if( c=='e' || c=='E' ){
4626
      int j = i+1;
4627
      int neg = 0;
4628
      if( j>=n ) break;
4629
      if( zIn[j]=='-' ){
4630
        neg = 1;
4631
        j++;
4632
      }else if( zIn[j]=='+' ){
4633
        j++;
4634
      }
4635
      while( j
4636
        if( zIn[j]>='0' && zIn[j]<='9' ){
4637
          iExp = iExp*10 + zIn[j] - '0';
4638
        }
4639
        j++;
4640
      }
4641
      if( neg ) iExp = -iExp;
4642
      break;
4643
    }
4644
    i++;
4645
  }
4646
  if( p->nFrac ){
4647
    p->nFrac = p->nDigit - (p->nFrac - 1);
4648
  }
4649
  if( iExp>0 ){
4650
    if( p->nFrac>0 ){
4651
      if( iExp<=p->nFrac ){
4652
        p->nFrac -= iExp;
4653
        iExp = 0;
4654
      }else{
4655
        iExp -= p->nFrac;
4656
        p->nFrac = 0;
4657
      }
4658
    }
4659
    if( iExp>0 ){
4660
      p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4661
      if( p->a==0 ) goto new_no_mem;
4662
      memset(p->a+p->nDigit, 0, iExp);
4663
      p->nDigit += iExp;
4664
    }
4665
  }else if( iExp<0 ){
4666
    int nExtra;
4667
    iExp = -iExp;
4668
    nExtra = p->nDigit - p->nFrac - 1;
4669
    if( nExtra ){
4670
      if( nExtra>=iExp ){
4671
        p->nFrac += iExp;
4672
        iExp  = 0;
4673
      }else{
4674
        iExp -= nExtra;
4675
        p->nFrac = p->nDigit - 1;
4676
      }
4677
    }
4678
    if( iExp>0 ){
4679
      p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4680
      if( p->a==0 ) goto new_no_mem;
4681
      memmove(p->a+iExp, p->a, p->nDigit);
4682
      memset(p->a, 0, iExp);
4683
      p->nDigit += iExp;
4684
      p->nFrac += iExp;
4685
    }
4686
  }
4687
  return p;
4688
 
4689
new_no_mem:
4690
  if( pCtx ) sqlite3_result_error_nomem(pCtx);
4691
  sqlite3_free(p);
4692
  return 0;
4693
}
4694
 
4695
/*
4696
** Make the given Decimal the result.
4697
*/
4698
static void decimal_result(sqlite3_context *pCtx, Decimal *p){
4699
  char *z;
4700
  int i, j;
4701
  int n;
4702
  if( p==0 || p->oom ){
4703
    sqlite3_result_error_nomem(pCtx);
4704
    return;
4705
  }
4706
  if( p->isNull ){
4707
    sqlite3_result_null(pCtx);
4708
    return;
4709
  }
4710
  z = sqlite3_malloc( p->nDigit+4 );
4711
  if( z==0 ){
4712
    sqlite3_result_error_nomem(pCtx);
4713
    return;
4714
  }
4715
  i = 0;
4716
  if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
4717
    p->sign = 0;
4718
  }
4719
  if( p->sign ){
4720
    z[0] = '-';
4721
    i = 1;
4722
  }
4723
  n = p->nDigit - p->nFrac;
4724
  if( n<=0 ){
4725
    z[i++] = '0';
4726
  }
4727
  j = 0;
4728
  while( n>1 && p->a[j]==0 ){
4729
    j++;
4730
    n--;
4731
  }
4732
  while( n>0  ){
4733
    z[i++] = p->a[j] + '0';
4734
    j++;
4735
    n--;
4736
  }
4737
  if( p->nFrac ){
4738
    z[i++] = '.';
4739
    do{
4740
      z[i++] = p->a[j] + '0';
4741
      j++;
4742
    }while( jnDigit );
4743
  }
4744
  z[i] = 0;
4745
  sqlite3_result_text(pCtx, z, i, sqlite3_free);
4746
}
4747
 
4748
/*
4749
** SQL Function:   decimal(X)
4750
**
4751
** Convert input X into decimal and then back into text
4752
*/
4753
static void decimalFunc(
4754
  sqlite3_context *context,
4755
  int argc,
4756
  sqlite3_value **argv
4757
){
4758
  Decimal *p = decimal_new(context, argv[0], 0, 0);
4759
  UNUSED_PARAMETER(argc);
4760
  decimal_result(context, p);
4761
  decimal_free(p);
4762
}
4763
 
4764
/*
4765
** Compare to Decimal objects.  Return negative, 0, or positive if the
4766
** first object is less than, equal to, or greater than the second.
4767
**
4768
** Preconditions for this routine:
4769
**
4770
**    pA!=0
4771
**    pA->isNull==0
4772
**    pB!=0
4773
**    pB->isNull==0
4774
*/
4775
static int decimal_cmp(const Decimal *pA, const Decimal *pB){
4776
  int nASig, nBSig, rc, n;
4777
  if( pA->sign!=pB->sign ){
4778
    return pA->sign ? -1 : +1;
4779
  }
4780
  if( pA->sign ){
4781
    const Decimal *pTemp = pA;
4782
    pA = pB;
4783
    pB = pTemp;
4784
  }
4785
  nASig = pA->nDigit - pA->nFrac;
4786
  nBSig = pB->nDigit - pB->nFrac;
4787
  if( nASig!=nBSig ){
4788
    return nASig - nBSig;
4789
  }
4790
  n = pA->nDigit;
4791
  if( n>pB->nDigit ) n = pB->nDigit;
4792
  rc = memcmp(pA->a, pB->a, n);
4793
  if( rc==0 ){
4794
    rc = pA->nDigit - pB->nDigit;
4795
  }
4796
  return rc;
4797
}
4798
 
4799
/*
4800
** SQL Function:   decimal_cmp(X, Y)
4801
**
4802
** Return negative, zero, or positive if X is less then, equal to, or
4803
** greater than Y.
4804
*/
4805
static void decimalCmpFunc(
4806
  sqlite3_context *context,
4807
  int argc,
4808
  sqlite3_value **argv
4809
){
4810
  Decimal *pA = 0, *pB = 0;
4811
  int rc;
4812
 
4813
  UNUSED_PARAMETER(argc);
4814
  pA = decimal_new(context, argv[0], 0, 0);
4815
  if( pA==0 || pA->isNull ) goto cmp_done;
4816
  pB = decimal_new(context, argv[1], 0, 0);
4817
  if( pB==0 || pB->isNull ) goto cmp_done;
4818
  rc = decimal_cmp(pA, pB);
4819
  if( rc<0 ) rc = -1;
4820
  else if( rc>0 ) rc = +1;
4821
  sqlite3_result_int(context, rc);
4822
cmp_done:
4823
  decimal_free(pA);
4824
  decimal_free(pB);
4825
}
4826
 
4827
/*
4828
** Expand the Decimal so that it has a least nDigit digits and nFrac
4829
** digits to the right of the decimal point.
4830
*/
4831
static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4832
  int nAddSig;
4833
  int nAddFrac;
4834
  if( p==0 ) return;
4835
  nAddFrac = nFrac - p->nFrac;
4836
  nAddSig = (nDigit - p->nDigit) - nAddFrac;
4837
  if( nAddFrac==0 && nAddSig==0 ) return;
4838
  p->a = sqlite3_realloc64(p->a, nDigit+1);
4839
  if( p->a==0 ){
4840
    p->oom = 1;
4841
    return;
4842
  }
4843
  if( nAddSig ){
4844
    memmove(p->a+nAddSig, p->a, p->nDigit);
4845
    memset(p->a, 0, nAddSig);
4846
    p->nDigit += nAddSig;
4847
  }
4848
  if( nAddFrac ){
4849
    memset(p->a+p->nDigit, 0, nAddFrac);
4850
    p->nDigit += nAddFrac;
4851
    p->nFrac += nAddFrac;
4852
  }
4853
}
4854
 
4855
/*
4856
** Add the value pB into pA.
4857
**
4858
** Both pA and pB might become denormalized by this routine.
4859
*/
4860
static void decimal_add(Decimal *pA, Decimal *pB){
4861
  int nSig, nFrac, nDigit;
4862
  int i, rc;
4863
  if( pA==0 ){
4864
    return;
4865
  }
4866
  if( pA->oom || pB==0 || pB->oom ){
4867
    pA->oom = 1;
4868
    return;
4869
  }
4870
  if( pA->isNull || pB->isNull ){
4871
    pA->isNull = 1;
4872
    return;
4873
  }
4874
  nSig = pA->nDigit - pA->nFrac;
4875
  if( nSig && pA->a[0]==0 ) nSig--;
4876
  if( nSignDigit-pB->nFrac ){
4877
    nSig = pB->nDigit - pB->nFrac;
4878
  }
4879
  nFrac = pA->nFrac;
4880
  if( nFracnFrac ) nFrac = pB->nFrac;
4881
  nDigit = nSig + nFrac + 1;
4882
  decimal_expand(pA, nDigit, nFrac);
4883
  decimal_expand(pB, nDigit, nFrac);
4884
  if( pA->oom || pB->oom ){
4885
    pA->oom = 1;
4886
  }else{
4887
    if( pA->sign==pB->sign ){
4888
      int carry = 0;
4889
      for(i=nDigit-1; i>=0; i--){
4890
        int x = pA->a[i] + pB->a[i] + carry;
4891
        if( x>=10 ){
4892
          carry = 1;
4893
          pA->a[i] = x - 10;
4894
        }else{
4895
          carry = 0;
4896
          pA->a[i] = x;
4897
        }
4898
      }
4899
    }else{
4900
      signed char *aA, *aB;
4901
      int borrow = 0;
4902
      rc = memcmp(pA->a, pB->a, nDigit);
4903
      if( rc<0 ){
4904
        aA = pB->a;
4905
        aB = pA->a;
4906
        pA->sign = !pA->sign;
4907
      }else{
4908
        aA = pA->a;
4909
        aB = pB->a;
4910
      }
4911
      for(i=nDigit-1; i>=0; i--){
4912
        int x = aA[i] - aB[i] - borrow;
4913
        if( x<0 ){
4914
          pA->a[i] = x+10;
4915
          borrow = 1;
4916
        }else{
4917
          pA->a[i] = x;
4918
          borrow = 0;
4919
        }
4920
      }
4921
    }
4922
  }
4923
}
4924
 
4925
/*
4926
** Compare text in decimal order.
4927
*/
4928
static int decimalCollFunc(
4929
  void *notUsed,
4930
  int nKey1, const void *pKey1,
4931
  int nKey2, const void *pKey2
4932
){
4933
  const unsigned char *zA = (const unsigned char*)pKey1;
4934
  const unsigned char *zB = (const unsigned char*)pKey2;
4935
  Decimal *pA = decimal_new(0, 0, nKey1, zA);
4936
  Decimal *pB = decimal_new(0, 0, nKey2, zB);
4937
  int rc;
4938
  UNUSED_PARAMETER(notUsed);
4939
  if( pA==0 || pB==0 ){
4940
    rc = 0;
4941
  }else{
4942
    rc = decimal_cmp(pA, pB);
4943
  }
4944
  decimal_free(pA);
4945
  decimal_free(pB);
4946
  return rc;
4947
}
4948
 
4949
 
4950
/*
4951
** SQL Function:   decimal_add(X, Y)
4952
**                 decimal_sub(X, Y)
4953
**
4954
** Return the sum or difference of X and Y.
4955
*/
4956
static void decimalAddFunc(
4957
  sqlite3_context *context,
4958
  int argc,
4959
  sqlite3_value **argv
4960
){
4961
  Decimal *pA = decimal_new(context, argv[0], 0, 0);
4962
  Decimal *pB = decimal_new(context, argv[1], 0, 0);
4963
  UNUSED_PARAMETER(argc);
4964
  decimal_add(pA, pB);
4965
  decimal_result(context, pA);
4966
  decimal_free(pA);
4967
  decimal_free(pB);
4968
}
4969
static void decimalSubFunc(
4970
  sqlite3_context *context,
4971
  int argc,
4972
  sqlite3_value **argv
4973
){
4974
  Decimal *pA = decimal_new(context, argv[0], 0, 0);
4975
  Decimal *pB = decimal_new(context, argv[1], 0, 0);
4976
  UNUSED_PARAMETER(argc);
4977
  if( pB==0 ) return;
4978
  pB->sign = !pB->sign;
4979
  decimal_add(pA, pB);
4980
  decimal_result(context, pA);
4981
  decimal_free(pA);
4982
  decimal_free(pB);
4983
}
4984
 
4985
/* Aggregate funcion:   decimal_sum(X)
4986
**
4987
** Works like sum() except that it uses decimal arithmetic for unlimited
4988
** precision.
4989
*/
4990
static void decimalSumStep(
4991
  sqlite3_context *context,
4992
  int argc,
4993
  sqlite3_value **argv
4994
){
4995
  Decimal *p;
4996
  Decimal *pArg;
4997
  UNUSED_PARAMETER(argc);
4998
  p = sqlite3_aggregate_context(context, sizeof(*p));
4999
  if( p==0 ) return;
5000
  if( !p->isInit ){
5001
    p->isInit = 1;
5002
    p->a = sqlite3_malloc(2);
5003
    if( p->a==0 ){
5004
      p->oom = 1;
5005
    }else{
5006
      p->a[0] = 0;
5007
    }
5008
    p->nDigit = 1;
5009
    p->nFrac = 0;
5010
  }
5011
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5012
  pArg = decimal_new(context, argv[0], 0, 0);
5013
  decimal_add(p, pArg);
5014
  decimal_free(pArg);
5015
}
5016
static void decimalSumInverse(
5017
  sqlite3_context *context,
5018
  int argc,
5019
  sqlite3_value **argv
5020
){
5021
  Decimal *p;
5022
  Decimal *pArg;
5023
  UNUSED_PARAMETER(argc);
5024
  p = sqlite3_aggregate_context(context, sizeof(*p));
5025
  if( p==0 ) return;
5026
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5027
  pArg = decimal_new(context, argv[0], 0, 0);
5028
  if( pArg ) pArg->sign = !pArg->sign;
5029
  decimal_add(p, pArg);
5030
  decimal_free(pArg);
5031
}
5032
static void decimalSumValue(sqlite3_context *context){
5033
  Decimal *p = sqlite3_aggregate_context(context, 0);
5034
  if( p==0 ) return;
5035
  decimal_result(context, p);
5036
}
5037
static void decimalSumFinalize(sqlite3_context *context){
5038
  Decimal *p = sqlite3_aggregate_context(context, 0);
5039
  if( p==0 ) return;
5040
  decimal_result(context, p);
5041
  decimal_clear(p);
5042
}
5043
 
5044
/*
5045
** SQL Function:   decimal_mul(X, Y)
5046
**
5047
** Return the product of X and Y.
5048
**
5049
** All significant digits after the decimal point are retained.
5050
** Trailing zeros after the decimal point are omitted as long as
5051
** the number of digits after the decimal point is no less than
5052
** either the number of digits in either input.
5053
*/
5054
static void decimalMulFunc(
5055
  sqlite3_context *context,
5056
  int argc,
5057
  sqlite3_value **argv
5058
){
5059
  Decimal *pA = decimal_new(context, argv[0], 0, 0);
5060
  Decimal *pB = decimal_new(context, argv[1], 0, 0);
5061
  signed char *acc = 0;
5062
  int i, j, k;
5063
  int minFrac;
5064
  UNUSED_PARAMETER(argc);
5065
  if( pA==0 || pA->oom || pA->isNull
5066
   || pB==0 || pB->oom || pB->isNull
5067
  ){
5068
    goto mul_end;
5069
  }
5070
  acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
5071
  if( acc==0 ){
5072
    sqlite3_result_error_nomem(context);
5073
    goto mul_end;
5074
  }
5075
  memset(acc, 0, pA->nDigit + pB->nDigit + 2);
5076
  minFrac = pA->nFrac;
5077
  if( pB->nFracnFrac;
5078
  for(i=pA->nDigit-1; i>=0; i--){
5079
    signed char f = pA->a[i];
5080
    int carry = 0, x;
5081
    for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
5082
      x = acc[k] + f*pB->a[j] + carry;
5083
      acc[k] = x%10;
5084
      carry = x/10;
5085
    }
5086
    x = acc[k] + carry;
5087
    acc[k] = x%10;
5088
    acc[k-1] += x/10;
5089
  }
5090
  sqlite3_free(pA->a);
5091
  pA->a = acc;
5092
  acc = 0;
5093
  pA->nDigit += pB->nDigit + 2;
5094
  pA->nFrac += pB->nFrac;
5095
  pA->sign ^= pB->sign;
5096
  while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
5097
    pA->nFrac--;
5098
    pA->nDigit--;
5099
  }
5100
  decimal_result(context, pA);
5101
 
5102
mul_end:
5103
  sqlite3_free(acc);
5104
  decimal_free(pA);
5105
  decimal_free(pB);
5106
}
5107
 
5108
#ifdef _WIN32
5109
 
5110
#endif
5111
int sqlite3_decimal_init(
5112
  sqlite3 *db,
5113
  char **pzErrMsg,
5114
  const sqlite3_api_routines *pApi
5115
){
5116
  int rc = SQLITE_OK;
5117
  static const struct {
5118
    const char *zFuncName;
5119
    int nArg;
5120
    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5121
  } aFunc[] = {
5122
    { "decimal",       1,   decimalFunc        },
5123
    { "decimal_cmp",   2,   decimalCmpFunc     },
5124
    { "decimal_add",   2,   decimalAddFunc     },
5125
    { "decimal_sub",   2,   decimalSubFunc     },
5126
    { "decimal_mul",   2,   decimalMulFunc     },
5127
  };
5128
  unsigned int i;
5129
  (void)pzErrMsg;  /* Unused parameter */
5130
 
5131
  SQLITE_EXTENSION_INIT2(pApi);
5132
 
5133
  for(i=0; i
5134
    rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
5135
                   SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
5136
                   0, aFunc[i].xFunc, 0, 0);
5137
  }
5138
  if( rc==SQLITE_OK ){
5139
    rc = sqlite3_create_window_function(db, "decimal_sum", 1,
5140
                   SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
5141
                   decimalSumStep, decimalSumFinalize,
5142
                   decimalSumValue, decimalSumInverse, 0);
5143
  }
5144
  if( rc==SQLITE_OK ){
5145
    rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
5146
                                  0, decimalCollFunc);
5147
  }
5148
  return rc;
5149
}
5150
 
5151
/************************* End ../ext/misc/decimal.c ********************/
5152
/************************* Begin ../ext/misc/ieee754.c ******************/
5153
/*
5154
** 2013-04-17
5155
**
5156
** The author disclaims copyright to this source code.  In place of
5157
** a legal notice, here is a blessing:
5158
**
5159
**    May you do good and not evil.
5160
**    May you find forgiveness for yourself and forgive others.
5161
**    May you share freely, never taking more than you give.
5162
**
5163
******************************************************************************
5164
**
5165
** This SQLite extension implements functions for the exact display
5166
** and input of IEEE754 Binary64 floating-point numbers.
5167
**
5168
**   ieee754(X)
5169
**   ieee754(Y,Z)
5170
**
5171
** In the first form, the value X should be a floating-point number.
5172
** The function will return a string of the form 'ieee754(Y,Z)' where
5173
** Y and Z are integers such that X==Y*pow(2,Z).
5174
**
5175
** In the second form, Y and Z are integers which are the mantissa and
5176
** base-2 exponent of a new floating point number.  The function returns
5177
** a floating-point value equal to Y*pow(2,Z).
5178
**
5179
** Examples:
5180
**
5181
**     ieee754(2.0)             ->     'ieee754(2,0)'
5182
**     ieee754(45.25)           ->     'ieee754(181,-2)'
5183
**     ieee754(2, 0)            ->     2.0
5184
**     ieee754(181, -2)         ->     45.25
5185
**
5186
** Two additional functions break apart the one-argument ieee754()
5187
** result into separate integer values:
5188
**
5189
**     ieee754_mantissa(45.25)  ->     181
5190
**     ieee754_exponent(45.25)  ->     -2
5191
**
5192
** These functions convert binary64 numbers into blobs and back again.
5193
**
5194
**     ieee754_from_blob(x'3ff0000000000000')  ->  1.0
5195
**     ieee754_to_blob(1.0)                    ->  x'3ff0000000000000'
5196
**
5197
** In all single-argument functions, if the argument is an 8-byte blob
5198
** then that blob is interpreted as a big-endian binary64 value.
5199
**
5200
**
5201
** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5202
** -----------------------------------------------
5203
**
5204
** This extension in combination with the separate 'decimal' extension
5205
** can be used to compute the exact decimal representation of binary64
5206
** values.  To begin, first compute a table of exponent values:
5207
**
5208
**    CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5209
**    WITH RECURSIVE c(x,v) AS (
5210
**      VALUES(0,'1')
5211
**      UNION ALL
5212
**      SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5213
**    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5214
**    WITH RECURSIVE c(x,v) AS (
5215
**      VALUES(-1,'0.5')
5216
**      UNION ALL
5217
**      SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5218
**    ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5219
**
5220
** Then, to compute the exact decimal representation of a floating
5221
** point value (the value 47.49 is used in the example) do:
5222
**
5223
**    WITH c(n) AS (VALUES(47.49))
5224
**          ---------------^^^^^---- Replace with whatever you want
5225
**    SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5226
**      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5227
**
5228
** Here is a query to show various boundry values for the binary64
5229
** number format:
5230
**
5231
**    WITH c(name,bin) AS (VALUES
5232
**       ('minimum positive value',        x'0000000000000001'),
5233
**       ('maximum subnormal value',       x'000fffffffffffff'),
5234
**       ('mininum positive nornal value', x'0010000000000000'),
5235
**       ('maximum value',                 x'7fefffffffffffff'))
5236
**    SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5237
**      FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5238
**
5239
*/
5240
/* #include "sqlite3ext.h" */
5241
SQLITE_EXTENSION_INIT1
5242
#include 
5243
#include 
5244
 
5245
/* Mark a function parameter as unused, to suppress nuisance compiler
5246
** warnings. */
5247
#ifndef UNUSED_PARAMETER
5248
# define UNUSED_PARAMETER(X)  (void)(X)
5249
#endif
5250
 
5251
/*
5252
** Implementation of the ieee754() function
5253
*/
5254
static void ieee754func(
5255
  sqlite3_context *context,
5256
  int argc,
5257
  sqlite3_value **argv
5258
){
5259
  if( argc==1 ){
5260
    sqlite3_int64 m, a;
5261
    double r;
5262
    int e;
5263
    int isNeg;
5264
    char zResult[100];
5265
    assert( sizeof(m)==sizeof(r) );
5266
    if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5267
     && sqlite3_value_bytes(argv[0])==sizeof(r)
5268
    ){
5269
      const unsigned char *x = sqlite3_value_blob(argv[0]);
5270
      unsigned int i;
5271
      sqlite3_uint64 v = 0;
5272
      for(i=0; i
5273
        v = (v<<8) | x[i];
5274
      }
5275
      memcpy(&r, &v, sizeof(r));
5276
    }else{
5277
      r = sqlite3_value_double(argv[0]);
5278
    }
5279
    if( r<0.0 ){
5280
      isNeg = 1;
5281
      r = -r;
5282
    }else{
5283
      isNeg = 0;
5284
    }
5285
    memcpy(&a,&r,sizeof(a));
5286
    if( a==0 ){
5287
      e = 0;
5288
      m = 0;
5289
    }else{
5290
      e = a>>52;
5291
      m = a & ((((sqlite3_int64)1)<<52)-1);
5292
      if( e==0 ){
5293
        m <<= 1;
5294
      }else{
5295
        m |= ((sqlite3_int64)1)<<52;
5296
      }
5297
      while( e<1075 && m>0 && (m&1)==0 ){
5298
        m >>= 1;
5299
        e++;
5300
      }
5301
      if( isNeg ) m = -m;
5302
    }
5303
    switch( *(int*)sqlite3_user_data(context) ){
5304
      case 0:
5305
        sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5306
                         m, e-1075);
5307
        sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5308
        break;
5309
      case 1:
5310
        sqlite3_result_int64(context, m);
5311
        break;
5312
      case 2:
5313
        sqlite3_result_int(context, e-1075);
5314
        break;
5315
    }
5316
  }else{
5317
    sqlite3_int64 m, e, a;
5318
    double r;
5319
    int isNeg = 0;
5320
    m = sqlite3_value_int64(argv[0]);
5321
    e = sqlite3_value_int64(argv[1]);
5322
 
5323
    /* Limit the range of e.  Ticket 22dea1cfdb9151e4 2021-03-02 */
5324
    if( e>10000 ){
5325
      e = 10000;
5326
    }else if( e<-10000 ){
5327
      e = -10000;
5328
    }
5329
 
5330
    if( m<0 ){
5331
      isNeg = 1;
5332
      m = -m;
5333
      if( m<0 ) return;
5334
    }else if( m==0 && e>-1000 && e<1000 ){
5335
      sqlite3_result_double(context, 0.0);
5336
      return;
5337
    }
5338
    while( (m>>32)&0xffe00000 ){
5339
      m >>= 1;
5340
      e++;
5341
    }
5342
    while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5343
      m <<= 1;
5344
      e--;
5345
    }
5346
    e += 1075;
5347
    if( e<=0 ){
5348
      /* Subnormal */
5349
      m >>= 1-e;
5350
      e = 0;
5351
    }else if( e>0x7ff ){
5352
      e = 0x7ff;
5353
    }
5354
    a = m & ((((sqlite3_int64)1)<<52)-1);
5355
    a |= e<<52;
5356
    if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5357
    memcpy(&r, &a, sizeof(r));
5358
    sqlite3_result_double(context, r);
5359
  }
5360
}
5361
 
5362
/*
5363
** Functions to convert between blobs and floats.
5364
*/
5365
static void ieee754func_from_blob(
5366
  sqlite3_context *context,
5367
  int argc,
5368
  sqlite3_value **argv
5369
){
5370
  UNUSED_PARAMETER(argc);
5371
  if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5372
   && sqlite3_value_bytes(argv[0])==sizeof(double)
5373
  ){
5374
    double r;
5375
    const unsigned char *x = sqlite3_value_blob(argv[0]);
5376
    unsigned int i;
5377
    sqlite3_uint64 v = 0;
5378
    for(i=0; i
5379
      v = (v<<8) | x[i];
5380
    }
5381
    memcpy(&r, &v, sizeof(r));
5382
    sqlite3_result_double(context, r);
5383
  }
5384
}
5385
static void ieee754func_to_blob(
5386
  sqlite3_context *context,
5387
  int argc,
5388
  sqlite3_value **argv
5389
){
5390
  UNUSED_PARAMETER(argc);
5391
  if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5392
   || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5393
  ){
5394
    double r = sqlite3_value_double(argv[0]);
5395
    sqlite3_uint64 v;
5396
    unsigned char a[sizeof(r)];
5397
    unsigned int i;
5398
    memcpy(&v, &r, sizeof(r));
5399
    for(i=1; i<=sizeof(r); i++){
5400
      a[sizeof(r)-i] = v&0xff;
5401
      v >>= 8;
5402
    }
5403
    sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5404
  }
5405
}
5406
 
5407
 
5408
#ifdef _WIN32
5409
 
5410
#endif
5411
int sqlite3_ieee_init(
5412
  sqlite3 *db,
5413
  char **pzErrMsg,
5414
  const sqlite3_api_routines *pApi
5415
){
5416
  static const struct {
5417
    char *zFName;
5418
    int nArg;
5419
    int iAux;
5420
    void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5421
  } aFunc[] = {
5422
    { "ieee754",           1,   0, ieee754func },
5423
    { "ieee754",           2,   0, ieee754func },
5424
    { "ieee754_mantissa",  1,   1, ieee754func },
5425
    { "ieee754_exponent",  1,   2, ieee754func },
5426
    { "ieee754_to_blob",   1,   0, ieee754func_to_blob },
5427
    { "ieee754_from_blob", 1,   0, ieee754func_from_blob },
5428
 
5429
  };
5430
  unsigned int i;
5431
  int rc = SQLITE_OK;
5432
  SQLITE_EXTENSION_INIT2(pApi);
5433
  (void)pzErrMsg;  /* Unused parameter */
5434
  for(i=0; i
5435
    rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,
5436
                               SQLITE_UTF8|SQLITE_INNOCUOUS,
5437
                               (void*)&aFunc[i].iAux,
5438
                               aFunc[i].xFunc, 0, 0);
5439
  }
5440
  return rc;
5441
}
5442
 
5443
/************************* End ../ext/misc/ieee754.c ********************/
5444
/************************* Begin ../ext/misc/series.c ******************/
5445
/*
5446
** 2015-08-18
5447
**
5448
** The author disclaims copyright to this source code.  In place of
5449
** a legal notice, here is a blessing:
5450
**
5451
**    May you do good and not evil.
5452
**    May you find forgiveness for yourself and forgive others.
5453
**    May you share freely, never taking more than you give.
5454
**
5455
*************************************************************************
5456
**
5457
** This file demonstrates how to create a table-valued-function using
5458
** a virtual table.  This demo implements the generate_series() function
5459
** which gives similar results to the eponymous function in PostgreSQL.
5460
** Examples:
5461
**
5462
**      SELECT * FROM generate_series(0,100,5);
5463
**
5464
** The query above returns integers from 0 through 100 counting by steps
5465
** of 5.
5466
**
5467
**      SELECT * FROM generate_series(0,100);
5468
**
5469
** Integers from 0 through 100 with a step size of 1.
5470
**
5471
**      SELECT * FROM generate_series(20) LIMIT 10;
5472
**
5473
** Integers 20 through 29.
5474
**
5475
** HOW IT WORKS
5476
**
5477
** The generate_series "function" is really a virtual table with the
5478
** following schema:
5479
**
5480
**     CREATE TABLE generate_series(
5481
**       value,
5482
**       start HIDDEN,
5483
**       stop HIDDEN,
5484
**       step HIDDEN
5485
**     );
5486
**
5487
** Function arguments in queries against this virtual table are translated
5488
** into equality constraints against successive hidden columns.  In other
5489
** words, the following pairs of queries are equivalent to each other:
5490
**
5491
**    SELECT * FROM generate_series(0,100,5);
5492
**    SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5493
**
5494
**    SELECT * FROM generate_series(0,100);
5495
**    SELECT * FROM generate_series WHERE start=0 AND stop=100;
5496
**
5497
**    SELECT * FROM generate_series(20) LIMIT 10;
5498
**    SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5499
**
5500
** The generate_series virtual table implementation leaves the xCreate method
5501
** set to NULL.  This means that it is not possible to do a CREATE VIRTUAL
5502
** TABLE command with "generate_series" as the USING argument.  Instead, there
5503
** is a single generate_series virtual table that is always available without
5504
** having to be created first.
5505
**
5506
** The xBestIndex method looks for equality constraints against the hidden
5507
** start, stop, and step columns, and if present, it uses those constraints
5508
** to bound the sequence of generated values.  If the equality constraints
5509
** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5510
** xBestIndex returns a small cost when both start and stop are available,
5511
** and a very large cost if either start or stop are unavailable.  This
5512
** encourages the query planner to order joins such that the bounds of the
5513
** series are well-defined.
5514
*/
5515
/* #include "sqlite3ext.h" */
5516
SQLITE_EXTENSION_INIT1
5517
#include 
5518
#include 
5519
 
5520
#ifndef SQLITE_OMIT_VIRTUALTABLE
5521
 
5522
 
5523
/* series_cursor is a subclass of sqlite3_vtab_cursor which will
5524
** serve as the underlying representation of a cursor that scans
5525
** over rows of the result
5526
*/
5527
typedef struct series_cursor series_cursor;
5528
struct series_cursor {
5529
  sqlite3_vtab_cursor base;  /* Base class - must be first */
5530
  int isDesc;                /* True to count down rather than up */
5531
  sqlite3_int64 iRowid;      /* The rowid */
5532
  sqlite3_int64 iValue;      /* Current value ("value") */
5533
  sqlite3_int64 mnValue;     /* Mimimum value ("start") */
5534
  sqlite3_int64 mxValue;     /* Maximum value ("stop") */
5535
  sqlite3_int64 iStep;       /* Increment ("step") */
5536
};
5537
 
5538
/*
5539
** The seriesConnect() method is invoked to create a new
5540
** series_vtab that describes the generate_series virtual table.
5541
**
5542
** Think of this routine as the constructor for series_vtab objects.
5543
**
5544
** All this routine needs to do is:
5545
**
5546
**    (1) Allocate the series_vtab object and initialize all fields.
5547
**
5548
**    (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5549
**        result set of queries against generate_series will look like.
5550
*/
5551
static int seriesConnect(
5552
  sqlite3 *db,
5553
  void *pUnused,
5554
  int argcUnused, const char *const*argvUnused,
5555
  sqlite3_vtab **ppVtab,
5556
  char **pzErrUnused
5557
){
5558
  sqlite3_vtab *pNew;
5559
  int rc;
5560
 
5561
/* Column numbers */
5562
#define SERIES_COLUMN_VALUE 0
5563
#define SERIES_COLUMN_START 1
5564
#define SERIES_COLUMN_STOP  2
5565
#define SERIES_COLUMN_STEP  3
5566
 
5567
  (void)pUnused;
5568
  (void)argcUnused;
5569
  (void)argvUnused;
5570
  (void)pzErrUnused;
5571
  rc = sqlite3_declare_vtab(db,
5572
     "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5573
  if( rc==SQLITE_OK ){
5574
    pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5575
    if( pNew==0 ) return SQLITE_NOMEM;
5576
    memset(pNew, 0, sizeof(*pNew));
5577
    sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5578
  }
5579
  return rc;
5580
}
5581
 
5582
/*
5583
** This method is the destructor for series_cursor objects.
5584
*/
5585
static int seriesDisconnect(sqlite3_vtab *pVtab){
5586
  sqlite3_free(pVtab);
5587
  return SQLITE_OK;
5588
}
5589
 
5590
/*
5591
** Constructor for a new series_cursor object.
5592
*/
5593
static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5594
  series_cursor *pCur;
5595
  (void)pUnused;
5596
  pCur = sqlite3_malloc( sizeof(*pCur) );
5597
  if( pCur==0 ) return SQLITE_NOMEM;
5598
  memset(pCur, 0, sizeof(*pCur));
5599
  *ppCursor = &pCur->base;
5600
  return SQLITE_OK;
5601
}
5602
 
5603
/*
5604
** Destructor for a series_cursor.
5605
*/
5606
static int seriesClose(sqlite3_vtab_cursor *cur){
5607
  sqlite3_free(cur);
5608
  return SQLITE_OK;
5609
}
5610
 
5611
 
5612
/*
5613
** Advance a series_cursor to its next row of output.
5614
*/
5615
static int seriesNext(sqlite3_vtab_cursor *cur){
5616
  series_cursor *pCur = (series_cursor*)cur;
5617
  if( pCur->isDesc ){
5618
    pCur->iValue -= pCur->iStep;
5619
  }else{
5620
    pCur->iValue += pCur->iStep;
5621
  }
5622
  pCur->iRowid++;
5623
  return SQLITE_OK;
5624
}
5625
 
5626
/*
5627
** Return values of columns for the row at which the series_cursor
5628
** is currently pointing.
5629
*/
5630
static int seriesColumn(
5631
  sqlite3_vtab_cursor *cur,   /* The cursor */
5632
  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
5633
  int i                       /* Which column to return */
5634
){
5635
  series_cursor *pCur = (series_cursor*)cur;
5636
  sqlite3_int64 x = 0;
5637
  switch( i ){
5638
    case SERIES_COLUMN_START:  x = pCur->mnValue; break;
5639
    case SERIES_COLUMN_STOP:   x = pCur->mxValue; break;
5640
    case SERIES_COLUMN_STEP:   x = pCur->iStep;   break;
5641
    default:                   x = pCur->iValue;  break;
5642
  }
5643
  sqlite3_result_int64(ctx, x);
5644
  return SQLITE_OK;
5645
}
5646
 
5647
/*
5648
** Return the rowid for the current row. In this implementation, the
5649
** first row returned is assigned rowid value 1, and each subsequent
5650
** row a value 1 more than that of the previous.
5651
*/
5652
static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5653
  series_cursor *pCur = (series_cursor*)cur;
5654
  *pRowid = pCur->iRowid;
5655
  return SQLITE_OK;
5656
}
5657
 
5658
/*
5659
** Return TRUE if the cursor has been moved off of the last
5660
** row of output.
5661
*/
5662
static int seriesEof(sqlite3_vtab_cursor *cur){
5663
  series_cursor *pCur = (series_cursor*)cur;
5664
  if( pCur->isDesc ){
5665
    return pCur->iValue < pCur->mnValue;
5666
  }else{
5667
    return pCur->iValue > pCur->mxValue;
5668
  }
5669
}
5670
 
5671
/* True to cause run-time checking of the start=, stop=, and/or step=
5672
** parameters.  The only reason to do this is for testing the
5673
** constraint checking logic for virtual tables in the SQLite core.
5674
*/
5675
#ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5676
# define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5677
#endif
5678
 
5679
/*
5680
** This method is called to "rewind" the series_cursor object back
5681
** to the first row of output.  This method is always called at least
5682
** once prior to any call to seriesColumn() or seriesRowid() or
5683
** seriesEof().
5684
**
5685
** The query plan selected by seriesBestIndex is passed in the idxNum
5686
** parameter.  (idxStr is not used in this implementation.)  idxNum
5687
** is a bitmask showing which constraints are available:
5688
**
5689
**    1:    start=VALUE
5690
**    2:    stop=VALUE
5691
**    4:    step=VALUE
5692
**
5693
** Also, if bit 8 is set, that means that the series should be output
5694
** in descending order rather than in ascending order.  If bit 16 is
5695
** set, then output must appear in ascending order.
5696
**
5697
** This routine should initialize the cursor and position it so that it
5698
** is pointing at the first row, or pointing off the end of the table
5699
** (so that seriesEof() will return true) if the table is empty.
5700
*/
5701
static int seriesFilter(
5702
  sqlite3_vtab_cursor *pVtabCursor,
5703
  int idxNum, const char *idxStrUnused,
5704
  int argc, sqlite3_value **argv
5705
){
5706
  series_cursor *pCur = (series_cursor *)pVtabCursor;
5707
  int i = 0;
5708
  (void)idxStrUnused;
5709
  if( idxNum & 1 ){
5710
    pCur->mnValue = sqlite3_value_int64(argv[i++]);
5711
  }else{
5712
    pCur->mnValue = 0;
5713
  }
5714
  if( idxNum & 2 ){
5715
    pCur->mxValue = sqlite3_value_int64(argv[i++]);
5716
  }else{
5717
    pCur->mxValue = 0xffffffff;
5718
  }
5719
  if( idxNum & 4 ){
5720
    pCur->iStep = sqlite3_value_int64(argv[i++]);
5721
    if( pCur->iStep==0 ){
5722
      pCur->iStep = 1;
5723
    }else if( pCur->iStep<0 ){
5724
      pCur->iStep = -pCur->iStep;
5725
      if( (idxNum & 16)==0 ) idxNum |= 8;
5726
    }
5727
  }else{
5728
    pCur->iStep = 1;
5729
  }
5730
  for(i=0; i
5731
    if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
5732
      /* If any of the constraints have a NULL value, then return no rows.
5733
      ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
5734
      pCur->mnValue = 1;
5735
      pCur->mxValue = 0;
5736
      break;
5737
    }
5738
  }
5739
  if( idxNum & 8 ){
5740
    pCur->isDesc = 1;
5741
    pCur->iValue = pCur->mxValue;
5742
    if( pCur->iStep>0 ){
5743
      pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep;
5744
    }
5745
  }else{
5746
    pCur->isDesc = 0;
5747
    pCur->iValue = pCur->mnValue;
5748
  }
5749
  pCur->iRowid = 1;
5750
  return SQLITE_OK;
5751
}
5752
 
5753
/*
5754
** SQLite will invoke this method one or more times while planning a query
5755
** that uses the generate_series virtual table.  This routine needs to create
5756
** a query plan for each invocation and compute an estimated cost for that
5757
** plan.
5758
**
5759
** In this implementation idxNum is used to represent the
5760
** query plan.  idxStr is unused.
5761
**
5762
** The query plan is represented by bits in idxNum:
5763
**
5764
**  (1)  start = $value  -- constraint exists
5765
**  (2)  stop = $value   -- constraint exists
5766
**  (4)  step = $value   -- constraint exists
5767
**  (8)  output in descending order
5768
*/
5769
static int seriesBestIndex(
5770
  sqlite3_vtab *tabUnused,
5771
  sqlite3_index_info *pIdxInfo
5772
){
5773
  int i, j;              /* Loop over constraints */
5774
  int idxNum = 0;        /* The query plan bitmask */
5775
  int unusableMask = 0;  /* Mask of unusable constraints */
5776
  int nArg = 0;          /* Number of arguments that seriesFilter() expects */
5777
  int aIdx[3];           /* Constraints on start, stop, and step */
5778
  const struct sqlite3_index_constraint *pConstraint;
5779
 
5780
  /* This implementation assumes that the start, stop, and step columns
5781
  ** are the last three columns in the virtual table. */
5782
  assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
5783
  assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
5784
  (void)tabUnused;
5785
  aIdx[0] = aIdx[1] = aIdx[2] = -1;
5786
  pConstraint = pIdxInfo->aConstraint;
5787
  for(i=0; inConstraint; i++, pConstraint++){
5788
    int iCol;    /* 0 for start, 1 for stop, 2 for step */
5789
    int iMask;   /* bitmask for those column */
5790
    if( pConstraint->iColumn
5791
    iCol = pConstraint->iColumn - SERIES_COLUMN_START;
5792
    assert( iCol>=0 && iCol<=2 );
5793
    iMask = 1 << iCol;
5794
    if( pConstraint->usable==0 ){
5795
      unusableMask |=  iMask;
5796
      continue;
5797
    }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5798
      idxNum |= iMask;
5799
      aIdx[iCol] = i;
5800
    }
5801
  }
5802
  for(i=0; i<3; i++){
5803
    if( (j = aIdx[i])>=0 ){
5804
      pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
5805
      pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
5806
    }
5807
  }
5808
  if( (unusableMask & ~idxNum)!=0 ){
5809
    /* The start, stop, and step columns are inputs.  Therefore if there
5810
    ** are unusable constraints on any of start, stop, or step then
5811
    ** this plan is unusable */
5812
    return SQLITE_CONSTRAINT;
5813
  }
5814
  if( (idxNum & 3)==3 ){
5815
    /* Both start= and stop= boundaries are available.  This is the
5816
    ** the preferred case */
5817
    pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5818
    pIdxInfo->estimatedRows = 1000;
5819
    if( pIdxInfo->nOrderBy==1 ){
5820
      if( pIdxInfo->aOrderBy[0].desc ){
5821
        idxNum |= 8;
5822
      }else{
5823
        idxNum |= 16;
5824
      }
5825
      pIdxInfo->orderByConsumed = 1;
5826
    }
5827
  }else{
5828
    /* If either boundary is missing, we have to generate a huge span
5829
    ** of numbers.  Make this case very expensive so that the query
5830
    ** planner will work hard to avoid it. */
5831
    pIdxInfo->estimatedRows = 2147483647;
5832
  }
5833
  pIdxInfo->idxNum = idxNum;
5834
  return SQLITE_OK;
5835
}
5836
 
5837
/*
5838
** This following structure defines all the methods for the
5839
** generate_series virtual table.
5840
*/
5841
static sqlite3_module seriesModule = {
5842
  0,                         /* iVersion */
5843
  0,                         /* xCreate */
5844
  seriesConnect,             /* xConnect */
5845
  seriesBestIndex,           /* xBestIndex */
5846
  seriesDisconnect,          /* xDisconnect */
5847
  0,                         /* xDestroy */
5848
  seriesOpen,                /* xOpen - open a cursor */
5849
  seriesClose,               /* xClose - close a cursor */
5850
  seriesFilter,              /* xFilter - configure scan constraints */
5851
  seriesNext,                /* xNext - advance a cursor */
5852
  seriesEof,                 /* xEof - check for end of scan */
5853
  seriesColumn,              /* xColumn - read data */
5854
  seriesRowid,               /* xRowid - read data */
5855
  0,                         /* xUpdate */
5856
  0,                         /* xBegin */
5857
  0,                         /* xSync */
5858
  0,                         /* xCommit */
5859
  0,                         /* xRollback */
5860
  0,                         /* xFindMethod */
5861
  0,                         /* xRename */
5862
  0,                         /* xSavepoint */
5863
  0,                         /* xRelease */
5864
  0,                         /* xRollbackTo */
5865
 
5866
};
5867
 
5868
#endif /* SQLITE_OMIT_VIRTUALTABLE */
5869
 
5870
#ifdef _WIN32
5871
 
5872
#endif
5873
int sqlite3_series_init(
5874
  sqlite3 *db,
5875
  char **pzErrMsg,
5876
  const sqlite3_api_routines *pApi
5877
){
5878
  int rc = SQLITE_OK;
5879
  SQLITE_EXTENSION_INIT2(pApi);
5880
#ifndef SQLITE_OMIT_VIRTUALTABLE
5881
  if( sqlite3_libversion_number()<3008012 ){
5882
    *pzErrMsg = sqlite3_mprintf(
5883
        "generate_series() requires SQLite 3.8.12 or later");
5884
    return SQLITE_ERROR;
5885
  }
5886
  rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
5887
#endif
5888
  return rc;
5889
}
5890
 
5891
/************************* End ../ext/misc/series.c ********************/
5892
#ifdef SQLITE_HAVE_ZLIB
5893
/************************* Begin ../ext/misc/zipfile.c ******************/
5894
/*
5895
** 2017-12-26
5896
**
5897
** The author disclaims copyright to this source code.  In place of
5898
** a legal notice, here is a blessing:
5899
**
5900
**    May you do good and not evil.
5901
**    May you find forgiveness for yourself and forgive others.
5902
**    May you share freely, never taking more than you give.
5903
**
5904
******************************************************************************
5905
**
5906
** This file implements a virtual table for reading and writing ZIP archive
5907
** files.
5908
**
5909
** Usage example:
5910
**
5911
**     SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
5912
**
5913
** Current limitations:
5914
**
5915
**    *  No support for encryption
5916
**    *  No support for ZIP archives spanning multiple files
5917
**    *  No support for zip64 extensions
5918
**    *  Only the "inflate/deflate" (zlib) compression method is supported
5919
*/
5920
/* #include "sqlite3ext.h" */
5921
SQLITE_EXTENSION_INIT1
5922
#include 
5923
#include 
5924
#include 
5925
 
5926
#include 
5927
 
5928
#ifndef SQLITE_OMIT_VIRTUALTABLE
5929
 
5930
#ifndef SQLITE_AMALGAMATION
5931
 
5932
/* typedef sqlite3_int64 i64; */
5933
/* typedef unsigned char u8; */
5934
typedef unsigned short u16;
5935
typedef unsigned long u32;
5936
#define MIN(a,b) ((a)<(b) ? (a) : (b))
5937
 
5938
#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
5939
# define ALWAYS(X)      (1)
5940
# define NEVER(X)       (0)
5941
#elif !defined(NDEBUG)
5942
# define ALWAYS(X)      ((X)?1:(assert(0),0))
5943
# define NEVER(X)       ((X)?(assert(0),1):0)
5944
#else
5945
# define ALWAYS(X)      (X)
5946
# define NEVER(X)       (X)
5947
#endif
5948
 
5949
#endif   /* SQLITE_AMALGAMATION */
5950
 
5951
/*
5952
** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
5953
**
5954
** In some ways it would be better to obtain these values from system
5955
** header files. But, the dependency is undesirable and (a) these
5956
** have been stable for decades, (b) the values are part of POSIX and
5957
** are also made explicit in [man stat], and (c) are part of the
5958
** file format for zip archives.
5959
*/
5960
#ifndef S_IFDIR
5961
# define S_IFDIR 0040000
5962
#endif
5963
#ifndef S_IFREG
5964
# define S_IFREG 0100000
5965
#endif
5966
#ifndef S_IFLNK
5967
# define S_IFLNK 0120000
5968
#endif
5969
 
5970
static const char ZIPFILE_SCHEMA[] =
5971
  "CREATE TABLE y("
5972
    "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
5973
    "mode,"              /* 1: POSIX mode for file */
5974
    "mtime,"             /* 2: Last modification time (secs since 1970)*/
5975
    "sz,"                /* 3: Size of object */
5976
    "rawdata,"           /* 4: Raw data */
5977
    "data,"              /* 5: Uncompressed data */
5978
    "method,"            /* 6: Compression method (integer) */
5979
    "z HIDDEN"           /* 7: Name of zip file */
5980
  ") WITHOUT ROWID;";
5981
 
5982
#define ZIPFILE_F_COLUMN_IDX 7    /* Index of column "file" in the above */
5983
#define ZIPFILE_BUFFER_SIZE (64*1024)
5984
 
5985
 
5986
/*
5987
** Magic numbers used to read and write zip files.
5988
**
5989
** ZIPFILE_NEWENTRY_MADEBY:
5990
**   Use this value for the "version-made-by" field in new zip file
5991
**   entries. The upper byte indicates "unix", and the lower byte
5992
**   indicates that the zip file matches pkzip specification 3.0.
5993
**   This is what info-zip seems to do.
5994
**
5995
** ZIPFILE_NEWENTRY_REQUIRED:
5996
**   Value for "version-required-to-extract" field of new entries.
5997
**   Version 2.0 is required to support folders and deflate compression.
5998
**
5999
** ZIPFILE_NEWENTRY_FLAGS:
6000
**   Value for "general-purpose-bit-flags" field of new entries. Bit
6001
**   11 means "utf-8 filename and comment".
6002
**
6003
** ZIPFILE_SIGNATURE_CDS:
6004
**   First 4 bytes of a valid CDS record.
6005
**
6006
** ZIPFILE_SIGNATURE_LFH:
6007
**   First 4 bytes of a valid LFH record.
6008
**
6009
** ZIPFILE_SIGNATURE_EOCD
6010
**   First 4 bytes of a valid EOCD record.
6011
*/
6012
#define ZIPFILE_EXTRA_TIMESTAMP   0x5455
6013
#define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
6014
#define ZIPFILE_NEWENTRY_REQUIRED 20
6015
#define ZIPFILE_NEWENTRY_FLAGS    0x800
6016
#define ZIPFILE_SIGNATURE_CDS     0x02014b50
6017
#define ZIPFILE_SIGNATURE_LFH     0x04034b50
6018
#define ZIPFILE_SIGNATURE_EOCD    0x06054b50
6019
 
6020
/*
6021
** The sizes of the fixed-size part of each of the three main data
6022
** structures in a zip archive.
6023
*/
6024
#define ZIPFILE_LFH_FIXED_SZ      30
6025
#define ZIPFILE_EOCD_FIXED_SZ     22
6026
#define ZIPFILE_CDS_FIXED_SZ      46
6027
 
6028
/*
6029
*** 4.3.16  End of central directory record:
6030
***
6031
***   end of central dir signature    4 bytes  (0x06054b50)
6032
***   number of this disk             2 bytes
6033
***   number of the disk with the
6034
***   start of the central directory  2 bytes
6035
***   total number of entries in the
6036
***   central directory on this disk  2 bytes
6037
***   total number of entries in
6038
***   the central directory           2 bytes
6039
***   size of the central directory   4 bytes
6040
***   offset of start of central
6041
***   directory with respect to
6042
***   the starting disk number        4 bytes
6043
***   .ZIP file comment length        2 bytes
6044
***   .ZIP file comment       (variable size)
6045
*/
6046
typedef struct ZipfileEOCD ZipfileEOCD;
6047
struct ZipfileEOCD {
6048
  u16 iDisk;
6049
  u16 iFirstDisk;
6050
  u16 nEntry;
6051
  u16 nEntryTotal;
6052
  u32 nSize;
6053
  u32 iOffset;
6054
};
6055
 
6056
/*
6057
*** 4.3.12  Central directory structure:
6058
***
6059
*** ...
6060
***
6061
***   central file header signature   4 bytes  (0x02014b50)
6062
***   version made by                 2 bytes
6063
***   version needed to extract       2 bytes
6064
***   general purpose bit flag        2 bytes
6065
***   compression method              2 bytes
6066
***   last mod file time              2 bytes
6067
***   last mod file date              2 bytes
6068
***   crc-32                          4 bytes
6069
***   compressed size                 4 bytes
6070
***   uncompressed size               4 bytes
6071
***   file name length                2 bytes
6072
***   extra field length              2 bytes
6073
***   file comment length             2 bytes
6074
***   disk number start               2 bytes
6075
***   internal file attributes        2 bytes
6076
***   external file attributes        4 bytes
6077
***   relative offset of local header 4 bytes
6078
*/
6079
typedef struct ZipfileCDS ZipfileCDS;
6080
struct ZipfileCDS {
6081
  u16 iVersionMadeBy;
6082
  u16 iVersionExtract;
6083
  u16 flags;
6084
  u16 iCompression;
6085
  u16 mTime;
6086
  u16 mDate;
6087
  u32 crc32;
6088
  u32 szCompressed;
6089
  u32 szUncompressed;
6090
  u16 nFile;
6091
  u16 nExtra;
6092
  u16 nComment;
6093
  u16 iDiskStart;
6094
  u16 iInternalAttr;
6095
  u32 iExternalAttr;
6096
  u32 iOffset;
6097
  char *zFile;                    /* Filename (sqlite3_malloc()) */
6098
};
6099
 
6100
/*
6101
*** 4.3.7  Local file header:
6102
***
6103
***   local file header signature     4 bytes  (0x04034b50)
6104
***   version needed to extract       2 bytes
6105
***   general purpose bit flag        2 bytes
6106
***   compression method              2 bytes
6107
***   last mod file time              2 bytes
6108
***   last mod file date              2 bytes
6109
***   crc-32                          4 bytes
6110
***   compressed size                 4 bytes
6111
***   uncompressed size               4 bytes
6112
***   file name length                2 bytes
6113
***   extra field length              2 bytes
6114
***
6115
*/
6116
typedef struct ZipfileLFH ZipfileLFH;
6117
struct ZipfileLFH {
6118
  u16 iVersionExtract;
6119
  u16 flags;
6120
  u16 iCompression;
6121
  u16 mTime;
6122
  u16 mDate;
6123
  u32 crc32;
6124
  u32 szCompressed;
6125
  u32 szUncompressed;
6126
  u16 nFile;
6127
  u16 nExtra;
6128
};
6129
 
6130
typedef struct ZipfileEntry ZipfileEntry;
6131
struct ZipfileEntry {
6132
  ZipfileCDS cds;            /* Parsed CDS record */
6133
  u32 mUnixTime;             /* Modification time, in UNIX format */
6134
  u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
6135
  i64 iDataOff;              /* Offset to data in file (if aData==0) */
6136
  u8 *aData;                 /* cds.szCompressed bytes of compressed data */
6137
  ZipfileEntry *pNext;       /* Next element in in-memory CDS */
6138
};
6139
 
6140
/*
6141
** Cursor type for zipfile tables.
6142
*/
6143
typedef struct ZipfileCsr ZipfileCsr;
6144
struct ZipfileCsr {
6145
  sqlite3_vtab_cursor base;  /* Base class - must be first */
6146
  i64 iId;                   /* Cursor ID */
6147
  u8 bEof;                   /* True when at EOF */
6148
  u8 bNoop;                  /* If next xNext() call is no-op */
6149
 
6150
  /* Used outside of write transactions */
6151
  FILE *pFile;               /* Zip file */
6152
  i64 iNextOff;              /* Offset of next record in central directory */
6153
  ZipfileEOCD eocd;          /* Parse of central directory record */
6154
 
6155
  ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
6156
  ZipfileEntry *pCurrent;    /* Current entry */
6157
  ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
6158
};
6159
 
6160
typedef struct ZipfileTab ZipfileTab;
6161
struct ZipfileTab {
6162
  sqlite3_vtab base;         /* Base class - must be first */
6163
  char *zFile;               /* Zip file this table accesses (may be NULL) */
6164
  sqlite3 *db;               /* Host database connection */
6165
  u8 *aBuffer;               /* Temporary buffer used for various tasks */
6166
 
6167
  ZipfileCsr *pCsrList;      /* List of cursors */
6168
  i64 iNextCsrid;
6169
 
6170
  /* The following are used by write transactions only */
6171
  ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
6172
  ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
6173
  FILE *pWriteFd;            /* File handle open on zip archive */
6174
  i64 szCurrent;             /* Current size of zip archive */
6175
  i64 szOrig;                /* Size of archive at start of transaction */
6176
};
6177
 
6178
/*
6179
** Set the error message contained in context ctx to the results of
6180
** vprintf(zFmt, ...).
6181
*/
6182
static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
6183
  char *zMsg = 0;
6184
  va_list ap;
6185
  va_start(ap, zFmt);
6186
  zMsg = sqlite3_vmprintf(zFmt, ap);
6187
  sqlite3_result_error(ctx, zMsg, -1);
6188
  sqlite3_free(zMsg);
6189
  va_end(ap);
6190
}
6191
 
6192
/*
6193
** If string zIn is quoted, dequote it in place. Otherwise, if the string
6194
** is not quoted, do nothing.
6195
*/
6196
static void zipfileDequote(char *zIn){
6197
  char q = zIn[0];
6198
  if( q=='"' || q=='\'' || q=='`' || q=='[' ){
6199
    int iIn = 1;
6200
    int iOut = 0;
6201
    if( q=='[' ) q = ']';
6202
    while( ALWAYS(zIn[iIn]) ){
6203
      char c = zIn[iIn++];
6204
      if( c==q && zIn[iIn++]!=q ) break;
6205
      zIn[iOut++] = c;
6206
    }
6207
    zIn[iOut] = '\0';
6208
  }
6209
}
6210
 
6211
/*
6212
** Construct a new ZipfileTab virtual table object.
6213
**
6214
**   argv[0]   -> module name  ("zipfile")
6215
**   argv[1]   -> database name
6216
**   argv[2]   -> table name
6217
**   argv[...] -> "column name" and other module argument fields.
6218
*/
6219
static int zipfileConnect(
6220
  sqlite3 *db,
6221
  void *pAux,
6222
  int argc, const char *const*argv,
6223
  sqlite3_vtab **ppVtab,
6224
  char **pzErr
6225
){
6226
  int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
6227
  int nFile = 0;
6228
  const char *zFile = 0;
6229
  ZipfileTab *pNew = 0;
6230
  int rc;
6231
 
6232
  /* If the table name is not "zipfile", require that the argument be
6233
  ** specified. This stops zipfile tables from being created as:
6234
  **
6235
  **   CREATE VIRTUAL TABLE zzz USING zipfile();
6236
  **
6237
  ** It does not prevent:
6238
  **
6239
  **   CREATE VIRTUAL TABLE zipfile USING zipfile();
6240
  */
6241
  assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
6242
  if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
6243
    *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
6244
    return SQLITE_ERROR;
6245
  }
6246
 
6247
  if( argc>3 ){
6248
    zFile = argv[3];
6249
    nFile = (int)strlen(zFile)+1;
6250
  }
6251
 
6252
  rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
6253
  if( rc==SQLITE_OK ){
6254
    pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
6255
    if( pNew==0 ) return SQLITE_NOMEM;
6256
    memset(pNew, 0, nByte+nFile);
6257
    pNew->db = db;
6258
    pNew->aBuffer = (u8*)&pNew[1];
6259
    if( zFile ){
6260
      pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
6261
      memcpy(pNew->zFile, zFile, nFile);
6262
      zipfileDequote(pNew->zFile);
6263
    }
6264
  }
6265
  sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
6266
  *ppVtab = (sqlite3_vtab*)pNew;
6267
  return rc;
6268
}
6269
 
6270
/*
6271
** Free the ZipfileEntry structure indicated by the only argument.
6272
*/
6273
static void zipfileEntryFree(ZipfileEntry *p){
6274
  if( p ){
6275
    sqlite3_free(p->cds.zFile);
6276
    sqlite3_free(p);
6277
  }
6278
}
6279
 
6280
/*
6281
** Release resources that should be freed at the end of a write
6282
** transaction.
6283
*/
6284
static void zipfileCleanupTransaction(ZipfileTab *pTab){
6285
  ZipfileEntry *pEntry;
6286
  ZipfileEntry *pNext;
6287
 
6288
  if( pTab->pWriteFd ){
6289
    fclose(pTab->pWriteFd);
6290
    pTab->pWriteFd = 0;
6291
  }
6292
  for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
6293
    pNext = pEntry->pNext;
6294
    zipfileEntryFree(pEntry);
6295
  }
6296
  pTab->pFirstEntry = 0;
6297
  pTab->pLastEntry = 0;
6298
  pTab->szCurrent = 0;
6299
  pTab->szOrig = 0;
6300
}
6301
 
6302
/*
6303
** This method is the destructor for zipfile vtab objects.
6304
*/
6305
static int zipfileDisconnect(sqlite3_vtab *pVtab){
6306
  zipfileCleanupTransaction((ZipfileTab*)pVtab);
6307
  sqlite3_free(pVtab);
6308
  return SQLITE_OK;
6309
}
6310
 
6311
/*
6312
** Constructor for a new ZipfileCsr object.
6313
*/
6314
static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
6315
  ZipfileTab *pTab = (ZipfileTab*)p;
6316
  ZipfileCsr *pCsr;
6317
  pCsr = sqlite3_malloc(sizeof(*pCsr));
6318
  *ppCsr = (sqlite3_vtab_cursor*)pCsr;
6319
  if( pCsr==0 ){
6320
    return SQLITE_NOMEM;
6321
  }
6322
  memset(pCsr, 0, sizeof(*pCsr));
6323
  pCsr->iId = ++pTab->iNextCsrid;
6324
  pCsr->pCsrNext = pTab->pCsrList;
6325
  pTab->pCsrList = pCsr;
6326
  return SQLITE_OK;
6327
}
6328
 
6329
/*
6330
** Reset a cursor back to the state it was in when first returned
6331
** by zipfileOpen().
6332
*/
6333
static void zipfileResetCursor(ZipfileCsr *pCsr){
6334
  ZipfileEntry *p;
6335
  ZipfileEntry *pNext;
6336
 
6337
  pCsr->bEof = 0;
6338
  if( pCsr->pFile ){
6339
    fclose(pCsr->pFile);
6340
    pCsr->pFile = 0;
6341
    zipfileEntryFree(pCsr->pCurrent);
6342
    pCsr->pCurrent = 0;
6343
  }
6344
 
6345
  for(p=pCsr->pFreeEntry; p; p=pNext){
6346
    pNext = p->pNext;
6347
    zipfileEntryFree(p);
6348
  }
6349
}
6350
 
6351
/*
6352
** Destructor for an ZipfileCsr.
6353
*/
6354
static int zipfileClose(sqlite3_vtab_cursor *cur){
6355
  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6356
  ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
6357
  ZipfileCsr **pp;
6358
  zipfileResetCursor(pCsr);
6359
 
6360
  /* Remove this cursor from the ZipfileTab.pCsrList list. */
6361
  for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
6362
  *pp = pCsr->pCsrNext;
6363
 
6364
  sqlite3_free(pCsr);
6365
  return SQLITE_OK;
6366
}
6367
 
6368
/*
6369
** Set the error message for the virtual table associated with cursor
6370
** pCsr to the results of vprintf(zFmt, ...).
6371
*/
6372
static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
6373
  va_list ap;
6374
  va_start(ap, zFmt);
6375
  sqlite3_free(pTab->base.zErrMsg);
6376
  pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
6377
  va_end(ap);
6378
}
6379
static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
6380
  va_list ap;
6381
  va_start(ap, zFmt);
6382
  sqlite3_free(pCsr->base.pVtab->zErrMsg);
6383
  pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
6384
  va_end(ap);
6385
}
6386
 
6387
/*
6388
** Read nRead bytes of data from offset iOff of file pFile into buffer
6389
** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
6390
** otherwise.
6391
**
6392
** If an error does occur, output variable (*pzErrmsg) may be set to point
6393
** to an English language error message. It is the responsibility of the
6394
** caller to eventually free this buffer using
6395
** sqlite3_free().
6396
*/
6397
static int zipfileReadData(
6398
  FILE *pFile,                    /* Read from this file */
6399
  u8 *aRead,                      /* Read into this buffer */
6400
  int nRead,                      /* Number of bytes to read */
6401
  i64 iOff,                       /* Offset to read from */
6402
  char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
6403
){
6404
  size_t n;
6405
  fseek(pFile, (long)iOff, SEEK_SET);
6406
  n = fread(aRead, 1, nRead, pFile);
6407
  if( (int)n!=nRead ){
6408
    *pzErrmsg = sqlite3_mprintf("error in fread()");
6409
    return SQLITE_ERROR;
6410
  }
6411
  return SQLITE_OK;
6412
}
6413
 
6414
static int zipfileAppendData(
6415
  ZipfileTab *pTab,
6416
  const u8 *aWrite,
6417
  int nWrite
6418
){
6419
  if( nWrite>0 ){
6420
    size_t n = nWrite;
6421
    fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
6422
    n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
6423
    if( (int)n!=nWrite ){
6424
      pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
6425
      return SQLITE_ERROR;
6426
    }
6427
    pTab->szCurrent += nWrite;
6428
  }
6429
  return SQLITE_OK;
6430
}
6431
 
6432
/*
6433
** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
6434
*/
6435
static u16 zipfileGetU16(const u8 *aBuf){
6436
  return (aBuf[1] << 8) + aBuf[0];
6437
}
6438
 
6439
/*
6440
** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
6441
*/
6442
static u32 zipfileGetU32(const u8 *aBuf){
6443
  return ((u32)(aBuf[3]) << 24)
6444
       + ((u32)(aBuf[2]) << 16)
6445
       + ((u32)(aBuf[1]) <<  8)
6446
       + ((u32)(aBuf[0]) <<  0);
6447
}
6448
 
6449
/*
6450
** Write a 16-bit little endiate integer into buffer aBuf.
6451
*/
6452
static void zipfilePutU16(u8 *aBuf, u16 val){
6453
  aBuf[0] = val & 0xFF;
6454
  aBuf[1] = (val>>8) & 0xFF;
6455
}
6456
 
6457
/*
6458
** Write a 32-bit little endiate integer into buffer aBuf.
6459
*/
6460
static void zipfilePutU32(u8 *aBuf, u32 val){
6461
  aBuf[0] = val & 0xFF;
6462
  aBuf[1] = (val>>8) & 0xFF;
6463
  aBuf[2] = (val>>16) & 0xFF;
6464
  aBuf[3] = (val>>24) & 0xFF;
6465
}
6466
 
6467
#define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
6468
#define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
6469
 
6470
#define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
6471
#define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
6472
 
6473
/*
6474
** Magic numbers used to read CDS records.
6475
*/
6476
#define ZIPFILE_CDS_NFILE_OFF        28
6477
#define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
6478
 
6479
/*
6480
** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
6481
** if the record is not well-formed, or SQLITE_OK otherwise.
6482
*/
6483
static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
6484
  u8 *aRead = aBuf;
6485
  u32 sig = zipfileRead32(aRead);
6486
  int rc = SQLITE_OK;
6487
  if( sig!=ZIPFILE_SIGNATURE_CDS ){
6488
    rc = SQLITE_ERROR;
6489
  }else{
6490
    pCDS->iVersionMadeBy = zipfileRead16(aRead);
6491
    pCDS->iVersionExtract = zipfileRead16(aRead);
6492
    pCDS->flags = zipfileRead16(aRead);
6493
    pCDS->iCompression = zipfileRead16(aRead);
6494
    pCDS->mTime = zipfileRead16(aRead);
6495
    pCDS->mDate = zipfileRead16(aRead);
6496
    pCDS->crc32 = zipfileRead32(aRead);
6497
    pCDS->szCompressed = zipfileRead32(aRead);
6498
    pCDS->szUncompressed = zipfileRead32(aRead);
6499
    assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
6500
    pCDS->nFile = zipfileRead16(aRead);
6501
    pCDS->nExtra = zipfileRead16(aRead);
6502
    pCDS->nComment = zipfileRead16(aRead);
6503
    pCDS->iDiskStart = zipfileRead16(aRead);
6504
    pCDS->iInternalAttr = zipfileRead16(aRead);
6505
    pCDS->iExternalAttr = zipfileRead32(aRead);
6506
    pCDS->iOffset = zipfileRead32(aRead);
6507
    assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
6508
  }
6509
 
6510
  return rc;
6511
}
6512
 
6513
/*
6514
** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
6515
** if the record is not well-formed, or SQLITE_OK otherwise.
6516
*/
6517
static int zipfileReadLFH(
6518
  u8 *aBuffer,
6519
  ZipfileLFH *pLFH
6520
){
6521
  u8 *aRead = aBuffer;
6522
  int rc = SQLITE_OK;
6523
 
6524
  u32 sig = zipfileRead32(aRead);
6525
  if( sig!=ZIPFILE_SIGNATURE_LFH ){
6526
    rc = SQLITE_ERROR;
6527
  }else{
6528
    pLFH->iVersionExtract = zipfileRead16(aRead);
6529
    pLFH->flags = zipfileRead16(aRead);
6530
    pLFH->iCompression = zipfileRead16(aRead);
6531
    pLFH->mTime = zipfileRead16(aRead);
6532
    pLFH->mDate = zipfileRead16(aRead);
6533
    pLFH->crc32 = zipfileRead32(aRead);
6534
    pLFH->szCompressed = zipfileRead32(aRead);
6535
    pLFH->szUncompressed = zipfileRead32(aRead);
6536
    pLFH->nFile = zipfileRead16(aRead);
6537
    pLFH->nExtra = zipfileRead16(aRead);
6538
  }
6539
  return rc;
6540
}
6541
 
6542
 
6543
/*
6544
** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
6545
** Scan through this buffer to find an "extra-timestamp" field. If one
6546
** exists, extract the 32-bit modification-timestamp from it and store
6547
** the value in output parameter *pmTime.
6548
**
6549
** Zero is returned if no extra-timestamp record could be found (and so
6550
** *pmTime is left unchanged), or non-zero otherwise.
6551
**
6552
** The general format of an extra field is:
6553
**
6554
**   Header ID    2 bytes
6555
**   Data Size    2 bytes
6556
**   Data         N bytes
6557
*/
6558
static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
6559
  int ret = 0;
6560
  u8 *p = aExtra;
6561
  u8 *pEnd = &aExtra[nExtra];
6562
 
6563
  while( p
6564
    u16 id = zipfileRead16(p);
6565
    u16 nByte = zipfileRead16(p);
6566
 
6567
    switch( id ){
6568
      case ZIPFILE_EXTRA_TIMESTAMP: {
6569
        u8 b = p[0];
6570
        if( b & 0x01 ){     /* 0x01 -> modtime is present */
6571
          *pmTime = zipfileGetU32(&p[1]);
6572
          ret = 1;
6573
        }
6574
        break;
6575
      }
6576
    }
6577
 
6578
    p += nByte;
6579
  }
6580
  return ret;
6581
}
6582
 
6583
/*
6584
** Convert the standard MS-DOS timestamp stored in the mTime and mDate
6585
** fields of the CDS structure passed as the only argument to a 32-bit
6586
** UNIX seconds-since-the-epoch timestamp. Return the result.
6587
**
6588
** "Standard" MS-DOS time format:
6589
**
6590
**   File modification time:
6591
**     Bits 00-04: seconds divided by 2
6592
**     Bits 05-10: minute
6593
**     Bits 11-15: hour
6594
**   File modification date:
6595
**     Bits 00-04: day
6596
**     Bits 05-08: month (1-12)
6597
**     Bits 09-15: years from 1980
6598
**
6599
** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
6600
*/
6601
static u32 zipfileMtime(ZipfileCDS *pCDS){
6602
  int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
6603
  int M = ((pCDS->mDate >> 5) & 0x0F);
6604
  int D = (pCDS->mDate & 0x1F);
6605
  int B = -13;
6606
 
6607
  int sec = (pCDS->mTime & 0x1F)*2;
6608
  int min = (pCDS->mTime >> 5) & 0x3F;
6609
  int hr = (pCDS->mTime >> 11) & 0x1F;
6610
  i64 JD;
6611
 
6612
  /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
6613
 
6614
  /* Calculate the JD in seconds for noon on the day in question */
6615
  if( M<3 ){
6616
    Y = Y-1;
6617
    M = M+12;
6618
  }
6619
  JD = (i64)(24*60*60) * (
6620
      (int)(365.25 * (Y + 4716))
6621
    + (int)(30.6001 * (M + 1))
6622
    + D + B - 1524
6623
  );
6624
 
6625
  /* Correct the JD for the time within the day */
6626
  JD += (hr-12) * 3600 + min * 60 + sec;
6627
 
6628
  /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
6629
  return (u32)(JD - (i64)(24405875) * 24*60*6);
6630
}
6631
 
6632
/*
6633
** The opposite of zipfileMtime(). This function populates the mTime and
6634
** mDate fields of the CDS structure passed as the first argument according
6635
** to the UNIX timestamp value passed as the second.
6636
*/
6637
static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
6638
  /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
6639
  i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
6640
 
6641
  int A, B, C, D, E;
6642
  int yr, mon, day;
6643
  int hr, min, sec;
6644
 
6645
  A = (int)((JD - 1867216.25)/36524.25);
6646
  A = (int)(JD + 1 + A - (A/4));
6647
  B = A + 1524;
6648
  C = (int)((B - 122.1)/365.25);
6649
  D = (36525*(C&32767))/100;
6650
  E = (int)((B-D)/30.6001);
6651
 
6652
  day = B - D - (int)(30.6001*E);
6653
  mon = (E<14 ? E-1 : E-13);
6654
  yr = mon>2 ? C-4716 : C-4715;
6655
 
6656
  hr = (mUnixTime % (24*60*60)) / (60*60);
6657
  min = (mUnixTime % (60*60)) / 60;
6658
  sec = (mUnixTime % 60);
6659
 
6660
  if( yr>=1980 ){
6661
    pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
6662
    pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
6663
  }else{
6664
    pCds->mDate = pCds->mTime = 0;
6665
  }
6666
 
6667
  assert( mUnixTime<315507600
6668
       || mUnixTime==zipfileMtime(pCds)
6669
       || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
6670
       /* || (mUnixTime % 2) */
6671
  );
6672
}
6673
 
6674
/*
6675
** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
6676
** size) containing an entire zip archive image. Or, if aBlob is NULL,
6677
** then pFile is a file-handle open on a zip file. In either case, this
6678
** function creates a ZipfileEntry object based on the zip archive entry
6679
** for which the CDS record is at offset iOff.
6680
**
6681
** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
6682
** the new object. Otherwise, an SQLite error code is returned and the
6683
** final value of (*ppEntry) undefined.
6684
*/
6685
static int zipfileGetEntry(
6686
  ZipfileTab *pTab,               /* Store any error message here */
6687
  const u8 *aBlob,                /* Pointer to in-memory file image */
6688
  int nBlob,                      /* Size of aBlob[] in bytes */
6689
  FILE *pFile,                    /* If aBlob==0, read from this file */
6690
  i64 iOff,                       /* Offset of CDS record */
6691
  ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
6692
){
6693
  u8 *aRead;
6694
  char **pzErr = &pTab->base.zErrMsg;
6695
  int rc = SQLITE_OK;
6696
 
6697
  if( aBlob==0 ){
6698
    aRead = pTab->aBuffer;
6699
    rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
6700
  }else{
6701
    aRead = (u8*)&aBlob[iOff];
6702
  }
6703
 
6704
  if( rc==SQLITE_OK ){
6705
    sqlite3_int64 nAlloc;
6706
    ZipfileEntry *pNew;
6707
 
6708
    int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
6709
    int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
6710
    nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
6711
 
6712
    nAlloc = sizeof(ZipfileEntry) + nExtra;
6713
    if( aBlob ){
6714
      nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
6715
    }
6716
 
6717
    pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
6718
    if( pNew==0 ){
6719
      rc = SQLITE_NOMEM;
6720
    }else{
6721
      memset(pNew, 0, sizeof(ZipfileEntry));
6722
      rc = zipfileReadCDS(aRead, &pNew->cds);
6723
      if( rc!=SQLITE_OK ){
6724
        *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
6725
      }else if( aBlob==0 ){
6726
        rc = zipfileReadData(
6727
            pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
6728
        );
6729
      }else{
6730
        aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
6731
      }
6732
    }
6733
 
6734
    if( rc==SQLITE_OK ){
6735
      u32 *pt = &pNew->mUnixTime;
6736
      pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
6737
      pNew->aExtra = (u8*)&pNew[1];
6738
      memcpy(pNew->aExtra, &aRead[nFile], nExtra);
6739
      if( pNew->cds.zFile==0 ){
6740
        rc = SQLITE_NOMEM;
6741
      }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
6742
        pNew->mUnixTime = zipfileMtime(&pNew->cds);
6743
      }
6744
    }
6745
 
6746
    if( rc==SQLITE_OK ){
6747
      static const int szFix = ZIPFILE_LFH_FIXED_SZ;
6748
      ZipfileLFH lfh;
6749
      if( pFile ){
6750
        rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
6751
      }else{
6752
        aRead = (u8*)&aBlob[pNew->cds.iOffset];
6753
      }
6754
 
6755
      rc = zipfileReadLFH(aRead, &lfh);
6756
      if( rc==SQLITE_OK ){
6757
        pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
6758
        pNew->iDataOff += lfh.nFile + lfh.nExtra;
6759
        if( aBlob && pNew->cds.szCompressed ){
6760
          pNew->aData = &pNew->aExtra[nExtra];
6761
          memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
6762
        }
6763
      }else{
6764
        *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
6765
            (int)pNew->cds.iOffset
6766
        );
6767
      }
6768
    }
6769
 
6770
    if( rc!=SQLITE_OK ){
6771
      zipfileEntryFree(pNew);
6772
    }else{
6773
      *ppEntry = pNew;
6774
    }
6775
  }
6776
 
6777
  return rc;
6778
}
6779
 
6780
/*
6781
** Advance an ZipfileCsr to its next row of output.
6782
*/
6783
static int zipfileNext(sqlite3_vtab_cursor *cur){
6784
  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6785
  int rc = SQLITE_OK;
6786
 
6787
  if( pCsr->pFile ){
6788
    i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
6789
    zipfileEntryFree(pCsr->pCurrent);
6790
    pCsr->pCurrent = 0;
6791
    if( pCsr->iNextOff>=iEof ){
6792
      pCsr->bEof = 1;
6793
    }else{
6794
      ZipfileEntry *p = 0;
6795
      ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
6796
      rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
6797
      if( rc==SQLITE_OK ){
6798
        pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
6799
        pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
6800
      }
6801
      pCsr->pCurrent = p;
6802
    }
6803
  }else{
6804
    if( !pCsr->bNoop ){
6805
      pCsr->pCurrent = pCsr->pCurrent->pNext;
6806
    }
6807
    if( pCsr->pCurrent==0 ){
6808
      pCsr->bEof = 1;
6809
    }
6810
  }
6811
 
6812
  pCsr->bNoop = 0;
6813
  return rc;
6814
}
6815
 
6816
static void zipfileFree(void *p) {
6817
  sqlite3_free(p);
6818
}
6819
 
6820
/*
6821
** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
6822
** size is nOut bytes. This function uncompresses the data and sets the
6823
** return value in context pCtx to the result (a blob).
6824
**
6825
** If an error occurs, an error code is left in pCtx instead.
6826
*/
6827
static void zipfileInflate(
6828
  sqlite3_context *pCtx,          /* Store result here */
6829
  const u8 *aIn,                  /* Compressed data */
6830
  int nIn,                        /* Size of buffer aIn[] in bytes */
6831
  int nOut                        /* Expected output size */
6832
){
6833
  u8 *aRes = sqlite3_malloc(nOut);
6834
  if( aRes==0 ){
6835
    sqlite3_result_error_nomem(pCtx);
6836
  }else{
6837
    int err;
6838
    z_stream str;
6839
    memset(&str, 0, sizeof(str));
6840
 
6841
    str.next_in = (Byte*)aIn;
6842
    str.avail_in = nIn;
6843
    str.next_out = (Byte*)aRes;
6844
    str.avail_out = nOut;
6845
 
6846
    err = inflateInit2(&str, -15);
6847
    if( err!=Z_OK ){
6848
      zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
6849
    }else{
6850
      err = inflate(&str, Z_NO_FLUSH);
6851
      if( err!=Z_STREAM_END ){
6852
        zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
6853
      }else{
6854
        sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
6855
        aRes = 0;
6856
      }
6857
    }
6858
    sqlite3_free(aRes);
6859
    inflateEnd(&str);
6860
  }
6861
}
6862
 
6863
/*
6864
** Buffer aIn (size nIn bytes) contains uncompressed data. This function
6865
** compresses it and sets (*ppOut) to point to a buffer containing the
6866
** compressed data. The caller is responsible for eventually calling
6867
** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
6868
** is set to the size of buffer (*ppOut) in bytes.
6869
**
6870
** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
6871
** code is returned and an error message left in virtual-table handle
6872
** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
6873
** case.
6874
*/
6875
static int zipfileDeflate(
6876
  const u8 *aIn, int nIn,         /* Input */
6877
  u8 **ppOut, int *pnOut,         /* Output */
6878
  char **pzErr                    /* OUT: Error message */
6879
){
6880
  int rc = SQLITE_OK;
6881
  sqlite3_int64 nAlloc;
6882
  z_stream str;
6883
  u8 *aOut;
6884
 
6885
  memset(&str, 0, sizeof(str));
6886
  str.next_in = (Bytef*)aIn;
6887
  str.avail_in = nIn;
6888
  deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
6889
 
6890
  nAlloc = deflateBound(&str, nIn);
6891
  aOut = (u8*)sqlite3_malloc64(nAlloc);
6892
  if( aOut==0 ){
6893
    rc = SQLITE_NOMEM;
6894
  }else{
6895
    int res;
6896
    str.next_out = aOut;
6897
    str.avail_out = nAlloc;
6898
    res = deflate(&str, Z_FINISH);
6899
    if( res==Z_STREAM_END ){
6900
      *ppOut = aOut;
6901
      *pnOut = (int)str.total_out;
6902
    }else{
6903
      sqlite3_free(aOut);
6904
      *pzErr = sqlite3_mprintf("zipfile: deflate() error");
6905
      rc = SQLITE_ERROR;
6906
    }
6907
    deflateEnd(&str);
6908
  }
6909
 
6910
  return rc;
6911
}
6912
 
6913
 
6914
/*
6915
** Return values of columns for the row at which the series_cursor
6916
** is currently pointing.
6917
*/
6918
static int zipfileColumn(
6919
  sqlite3_vtab_cursor *cur,   /* The cursor */
6920
  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
6921
  int i                       /* Which column to return */
6922
){
6923
  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6924
  ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
6925
  int rc = SQLITE_OK;
6926
  switch( i ){
6927
    case 0:   /* name */
6928
      sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
6929
      break;
6930
    case 1:   /* mode */
6931
      /* TODO: Whether or not the following is correct surely depends on
6932
      ** the platform on which the archive was created.  */
6933
      sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
6934
      break;
6935
    case 2: { /* mtime */
6936
      sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
6937
      break;
6938
    }
6939
    case 3: { /* sz */
6940
      if( sqlite3_vtab_nochange(ctx)==0 ){
6941
        sqlite3_result_int64(ctx, pCDS->szUncompressed);
6942
      }
6943
      break;
6944
    }
6945
    case 4:   /* rawdata */
6946
      if( sqlite3_vtab_nochange(ctx) ) break;
6947
    case 5: { /* data */
6948
      if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
6949
        int sz = pCDS->szCompressed;
6950
        int szFinal = pCDS->szUncompressed;
6951
        if( szFinal>0 ){
6952
          u8 *aBuf;
6953
          u8 *aFree = 0;
6954
          if( pCsr->pCurrent->aData ){
6955
            aBuf = pCsr->pCurrent->aData;
6956
          }else{
6957
            aBuf = aFree = sqlite3_malloc64(sz);
6958
            if( aBuf==0 ){
6959
              rc = SQLITE_NOMEM;
6960
            }else{
6961
              FILE *pFile = pCsr->pFile;
6962
              if( pFile==0 ){
6963
                pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
6964
              }
6965
              rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
6966
                  &pCsr->base.pVtab->zErrMsg
6967
              );
6968
            }
6969
          }
6970
          if( rc==SQLITE_OK ){
6971
            if( i==5 && pCDS->iCompression ){
6972
              zipfileInflate(ctx, aBuf, sz, szFinal);
6973
            }else{
6974
              sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
6975
            }
6976
          }
6977
          sqlite3_free(aFree);
6978
        }else{
6979
          /* Figure out if this is a directory or a zero-sized file. Consider
6980
          ** it to be a directory either if the mode suggests so, or if
6981
          ** the final character in the name is '/'.  */
6982
          u32 mode = pCDS->iExternalAttr >> 16;
6983
          if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
6984
            sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
6985
          }
6986
        }
6987
      }
6988
      break;
6989
    }
6990
    case 6:   /* method */
6991
      sqlite3_result_int(ctx, pCDS->iCompression);
6992
      break;
6993
    default:  /* z */
6994
      assert( i==7 );
6995
      sqlite3_result_int64(ctx, pCsr->iId);
6996
      break;
6997
  }
6998
 
6999
  return rc;
7000
}
7001
 
7002
/*
7003
** Return TRUE if the cursor is at EOF.
7004
*/
7005
static int zipfileEof(sqlite3_vtab_cursor *cur){
7006
  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7007
  return pCsr->bEof;
7008
}
7009
 
7010
/*
7011
** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
7012
** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
7013
** is guaranteed to be a file-handle open on a zip file.
7014
**
7015
** This function attempts to locate the EOCD record within the zip archive
7016
** and populate *pEOCD with the results of decoding it. SQLITE_OK is
7017
** returned if successful. Otherwise, an SQLite error code is returned and
7018
** an English language error message may be left in virtual-table pTab.
7019
*/
7020
static int zipfileReadEOCD(
7021
  ZipfileTab *pTab,               /* Return errors here */
7022
  const u8 *aBlob,                /* Pointer to in-memory file image */
7023
  int nBlob,                      /* Size of aBlob[] in bytes */
7024
  FILE *pFile,                    /* Read from this file if aBlob==0 */
7025
  ZipfileEOCD *pEOCD              /* Object to populate */
7026
){
7027
  u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
7028
  int nRead;                      /* Bytes to read from file */
7029
  int rc = SQLITE_OK;
7030
 
7031
  if( aBlob==0 ){
7032
    i64 iOff;                     /* Offset to read from */
7033
    i64 szFile;                   /* Total size of file in bytes */
7034
    fseek(pFile, 0, SEEK_END);
7035
    szFile = (i64)ftell(pFile);
7036
    if( szFile==0 ){
7037
      memset(pEOCD, 0, sizeof(ZipfileEOCD));
7038
      return SQLITE_OK;
7039
    }
7040
    nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
7041
    iOff = szFile - nRead;
7042
    rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
7043
  }else{
7044
    nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
7045
    aRead = (u8*)&aBlob[nBlob-nRead];
7046
  }
7047
 
7048
  if( rc==SQLITE_OK ){
7049
    int i;
7050
 
7051
    /* Scan backwards looking for the signature bytes */
7052
    for(i=nRead-20; i>=0; i--){
7053
      if( aRead[i]==0x50 && aRead[i+1]==0x4b
7054
       && aRead[i+2]==0x05 && aRead[i+3]==0x06
7055
      ){
7056
        break;
7057
      }
7058
    }
7059
    if( i<0 ){
7060
      pTab->base.zErrMsg = sqlite3_mprintf(
7061
          "cannot find end of central directory record"
7062
      );
7063
      return SQLITE_ERROR;
7064
    }
7065
 
7066
    aRead += i+4;
7067
    pEOCD->iDisk = zipfileRead16(aRead);
7068
    pEOCD->iFirstDisk = zipfileRead16(aRead);
7069
    pEOCD->nEntry = zipfileRead16(aRead);
7070
    pEOCD->nEntryTotal = zipfileRead16(aRead);
7071
    pEOCD->nSize = zipfileRead32(aRead);
7072
    pEOCD->iOffset = zipfileRead32(aRead);
7073
  }
7074
 
7075
  return rc;
7076
}
7077
 
7078
/*
7079
** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
7080
** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
7081
** to the end of the list. Otherwise, it is added to the list immediately
7082
** before pBefore (which is guaranteed to be a part of said list).
7083
*/
7084
static void zipfileAddEntry(
7085
  ZipfileTab *pTab,
7086
  ZipfileEntry *pBefore,
7087
  ZipfileEntry *pNew
7088
){
7089
  assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
7090
  assert( pNew->pNext==0 );
7091
  if( pBefore==0 ){
7092
    if( pTab->pFirstEntry==0 ){
7093
      pTab->pFirstEntry = pTab->pLastEntry = pNew;
7094
    }else{
7095
      assert( pTab->pLastEntry->pNext==0 );
7096
      pTab->pLastEntry->pNext = pNew;
7097
      pTab->pLastEntry = pNew;
7098
    }
7099
  }else{
7100
    ZipfileEntry **pp;
7101
    for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
7102
    pNew->pNext = pBefore;
7103
    *pp = pNew;
7104
  }
7105
}
7106
 
7107
static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
7108
  ZipfileEOCD eocd;
7109
  int rc;
7110
  int i;
7111
  i64 iOff;
7112
 
7113
  rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
7114
  iOff = eocd.iOffset;
7115
  for(i=0; rc==SQLITE_OK && i
7116
    ZipfileEntry *pNew = 0;
7117
    rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
7118
 
7119
    if( rc==SQLITE_OK ){
7120
      zipfileAddEntry(pTab, 0, pNew);
7121
      iOff += ZIPFILE_CDS_FIXED_SZ;
7122
      iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
7123
    }
7124
  }
7125
  return rc;
7126
}
7127
 
7128
/*
7129
** xFilter callback.
7130
*/
7131
static int zipfileFilter(
7132
  sqlite3_vtab_cursor *cur,
7133
  int idxNum, const char *idxStr,
7134
  int argc, sqlite3_value **argv
7135
){
7136
  ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
7137
  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7138
  const char *zFile = 0;          /* Zip file to scan */
7139
  int rc = SQLITE_OK;             /* Return Code */
7140
  int bInMemory = 0;              /* True for an in-memory zipfile */
7141
 
7142
  zipfileResetCursor(pCsr);
7143
 
7144
  if( pTab->zFile ){
7145
    zFile = pTab->zFile;
7146
  }else if( idxNum==0 ){
7147
    zipfileCursorErr(pCsr, "zipfile() function requires an argument");
7148
    return SQLITE_ERROR;
7149
  }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
7150
    const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
7151
    int nBlob = sqlite3_value_bytes(argv[0]);
7152
    assert( pTab->pFirstEntry==0 );
7153
    rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
7154
    pCsr->pFreeEntry = pTab->pFirstEntry;
7155
    pTab->pFirstEntry = pTab->pLastEntry = 0;
7156
    if( rc!=SQLITE_OK ) return rc;
7157
    bInMemory = 1;
7158
  }else{
7159
    zFile = (const char*)sqlite3_value_text(argv[0]);
7160
  }
7161
 
7162
  if( 0==pTab->pWriteFd && 0==bInMemory ){
7163
    pCsr->pFile = fopen(zFile, "rb");
7164
    if( pCsr->pFile==0 ){
7165
      zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
7166
      rc = SQLITE_ERROR;
7167
    }else{
7168
      rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
7169
      if( rc==SQLITE_OK ){
7170
        if( pCsr->eocd.nEntry==0 ){
7171
          pCsr->bEof = 1;
7172
        }else{
7173
          pCsr->iNextOff = pCsr->eocd.iOffset;
7174
          rc = zipfileNext(cur);
7175
        }
7176
      }
7177
    }
7178
  }else{
7179
    pCsr->bNoop = 1;
7180
    pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
7181
    rc = zipfileNext(cur);
7182
  }
7183
 
7184
  return rc;
7185
}
7186
 
7187
/*
7188
** xBestIndex callback.
7189
*/
7190
static int zipfileBestIndex(
7191
  sqlite3_vtab *tab,
7192
  sqlite3_index_info *pIdxInfo
7193
){
7194
  int i;
7195
  int idx = -1;
7196
  int unusable = 0;
7197
 
7198
  for(i=0; inConstraint; i++){
7199
    const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7200
    if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
7201
    if( pCons->usable==0 ){
7202
      unusable = 1;
7203
    }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7204
      idx = i;
7205
    }
7206
  }
7207
  pIdxInfo->estimatedCost = 1000.0;
7208
  if( idx>=0 ){
7209
    pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
7210
    pIdxInfo->aConstraintUsage[idx].omit = 1;
7211
    pIdxInfo->idxNum = 1;
7212
  }else if( unusable ){
7213
    return SQLITE_CONSTRAINT;
7214
  }
7215
  return SQLITE_OK;
7216
}
7217
 
7218
static ZipfileEntry *zipfileNewEntry(const char *zPath){
7219
  ZipfileEntry *pNew;
7220
  pNew = sqlite3_malloc(sizeof(ZipfileEntry));
7221
  if( pNew ){
7222
    memset(pNew, 0, sizeof(ZipfileEntry));
7223
    pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
7224
    if( pNew->cds.zFile==0 ){
7225
      sqlite3_free(pNew);
7226
      pNew = 0;
7227
    }
7228
  }
7229
  return pNew;
7230
}
7231
 
7232
static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
7233
  ZipfileCDS *pCds = &pEntry->cds;
7234
  u8 *a = aBuf;
7235
 
7236
  pCds->nExtra = 9;
7237
 
7238
  /* Write the LFH itself */
7239
  zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
7240
  zipfileWrite16(a, pCds->iVersionExtract);
7241
  zipfileWrite16(a, pCds->flags);
7242
  zipfileWrite16(a, pCds->iCompression);
7243
  zipfileWrite16(a, pCds->mTime);
7244
  zipfileWrite16(a, pCds->mDate);
7245
  zipfileWrite32(a, pCds->crc32);
7246
  zipfileWrite32(a, pCds->szCompressed);
7247
  zipfileWrite32(a, pCds->szUncompressed);
7248
  zipfileWrite16(a, (u16)pCds->nFile);
7249
  zipfileWrite16(a, pCds->nExtra);
7250
  assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
7251
 
7252
  /* Add the file name */
7253
  memcpy(a, pCds->zFile, (int)pCds->nFile);
7254
  a += (int)pCds->nFile;
7255
 
7256
  /* The "extra" data */
7257
  zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
7258
  zipfileWrite16(a, 5);
7259
  *a++ = 0x01;
7260
  zipfileWrite32(a, pEntry->mUnixTime);
7261
 
7262
  return a-aBuf;
7263
}
7264
 
7265
static int zipfileAppendEntry(
7266
  ZipfileTab *pTab,
7267
  ZipfileEntry *pEntry,
7268
  const u8 *pData,
7269
  int nData
7270
){
7271
  u8 *aBuf = pTab->aBuffer;
7272
  int nBuf;
7273
  int rc;
7274
 
7275
  nBuf = zipfileSerializeLFH(pEntry, aBuf);
7276
  rc = zipfileAppendData(pTab, aBuf, nBuf);
7277
  if( rc==SQLITE_OK ){
7278
    pEntry->iDataOff = pTab->szCurrent;
7279
    rc = zipfileAppendData(pTab, pData, nData);
7280
  }
7281
 
7282
  return rc;
7283
}
7284
 
7285
static int zipfileGetMode(
7286
  sqlite3_value *pVal,
7287
  int bIsDir,                     /* If true, default to directory */
7288
  u32 *pMode,                     /* OUT: Mode value */
7289
  char **pzErr                    /* OUT: Error message */
7290
){
7291
  const char *z = (const char*)sqlite3_value_text(pVal);
7292
  u32 mode = 0;
7293
  if( z==0 ){
7294
    mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
7295
  }else if( z[0]>='0' && z[0]<='9' ){
7296
    mode = (unsigned int)sqlite3_value_int(pVal);
7297
  }else{
7298
    const char zTemplate[11] = "-rwxrwxrwx";
7299
    int i;
7300
    if( strlen(z)!=10 ) goto parse_error;
7301
    switch( z[0] ){
7302
      case '-': mode |= S_IFREG; break;
7303
      case 'd': mode |= S_IFDIR; break;
7304
      case 'l': mode |= S_IFLNK; break;
7305
      default: goto parse_error;
7306
    }
7307
    for(i=1; i<10; i++){
7308
      if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
7309
      else if( z[i]!='-' ) goto parse_error;
7310
    }
7311
  }
7312
  if( ((mode & S_IFDIR)==0)==bIsDir ){
7313
    /* The "mode" attribute is a directory, but data has been specified.
7314
    ** Or vice-versa - no data but "mode" is a file or symlink.  */
7315
    *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
7316
    return SQLITE_CONSTRAINT;
7317
  }
7318
  *pMode = mode;
7319
  return SQLITE_OK;
7320
 
7321
 parse_error:
7322
  *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
7323
  return SQLITE_ERROR;
7324
}
7325
 
7326
/*
7327
** Both (const char*) arguments point to nul-terminated strings. Argument
7328
** nB is the value of strlen(zB). This function returns 0 if the strings are
7329
** identical, ignoring any trailing '/' character in either path.  */
7330
static int zipfileComparePath(const char *zA, const char *zB, int nB){
7331
  int nA = (int)strlen(zA);
7332
  if( nA>0 && zA[nA-1]=='/' ) nA--;
7333
  if( nB>0 && zB[nB-1]=='/' ) nB--;
7334
  if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
7335
  return 1;
7336
}
7337
 
7338
static int zipfileBegin(sqlite3_vtab *pVtab){
7339
  ZipfileTab *pTab = (ZipfileTab*)pVtab;
7340
  int rc = SQLITE_OK;
7341
 
7342
  assert( pTab->pWriteFd==0 );
7343
  if( pTab->zFile==0 || pTab->zFile[0]==0 ){
7344
    pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
7345
    return SQLITE_ERROR;
7346
  }
7347
 
7348
  /* Open a write fd on the file. Also load the entire central directory
7349
  ** structure into memory. During the transaction any new file data is
7350
  ** appended to the archive file, but the central directory is accumulated
7351
  ** in main-memory until the transaction is committed.  */
7352
  pTab->pWriteFd = fopen(pTab->zFile, "ab+");
7353
  if( pTab->pWriteFd==0 ){
7354
    pTab->base.zErrMsg = sqlite3_mprintf(
7355
        "zipfile: failed to open file %s for writing", pTab->zFile
7356
        );
7357
    rc = SQLITE_ERROR;
7358
  }else{
7359
    fseek(pTab->pWriteFd, 0, SEEK_END);
7360
    pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
7361
    rc = zipfileLoadDirectory(pTab, 0, 0);
7362
  }
7363
 
7364
  if( rc!=SQLITE_OK ){
7365
    zipfileCleanupTransaction(pTab);
7366
  }
7367
 
7368
  return rc;
7369
}
7370
 
7371
/*
7372
** Return the current time as a 32-bit timestamp in UNIX epoch format (like
7373
** time(2)).
7374
*/
7375
static u32 zipfileTime(void){
7376
  sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
7377
  u32 ret;
7378
  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
7379
    i64 ms;
7380
    pVfs->xCurrentTimeInt64(pVfs, &ms);
7381
    ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
7382
  }else{
7383
    double day;
7384
    pVfs->xCurrentTime(pVfs, &day);
7385
    ret = (u32)((day - 2440587.5) * 86400);
7386
  }
7387
  return ret;
7388
}
7389
 
7390
/*
7391
** Return a 32-bit timestamp in UNIX epoch format.
7392
**
7393
** If the value passed as the only argument is either NULL or an SQL NULL,
7394
** return the current time. Otherwise, return the value stored in (*pVal)
7395
** cast to a 32-bit unsigned integer.
7396
*/
7397
static u32 zipfileGetTime(sqlite3_value *pVal){
7398
  if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
7399
    return zipfileTime();
7400
  }
7401
  return (u32)sqlite3_value_int64(pVal);
7402
}
7403
 
7404
/*
7405
** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
7406
** linked list.  Remove it from the list and free the object.
7407
*/
7408
static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
7409
  if( pOld ){
7410
    ZipfileEntry **pp;
7411
    for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
7412
    *pp = (*pp)->pNext;
7413
    zipfileEntryFree(pOld);
7414
  }
7415
}
7416
 
7417
/*
7418
** xUpdate method.
7419
*/
7420
static int zipfileUpdate(
7421
  sqlite3_vtab *pVtab,
7422
  int nVal,
7423
  sqlite3_value **apVal,
7424
  sqlite_int64 *pRowid
7425
){
7426
  ZipfileTab *pTab = (ZipfileTab*)pVtab;
7427
  int rc = SQLITE_OK;             /* Return Code */
7428
  ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
7429
 
7430
  u32 mode = 0;                   /* Mode for new entry */
7431
  u32 mTime = 0;                  /* Modification time for new entry */
7432
  i64 sz = 0;                     /* Uncompressed size */
7433
  const char *zPath = 0;          /* Path for new entry */
7434
  int nPath = 0;                  /* strlen(zPath) */
7435
  const u8 *pData = 0;            /* Pointer to buffer containing content */
7436
  int nData = 0;                  /* Size of pData buffer in bytes */
7437
  int iMethod = 0;                /* Compression method for new entry */
7438
  u8 *pFree = 0;                  /* Free this */
7439
  char *zFree = 0;                /* Also free this */
7440
  ZipfileEntry *pOld = 0;
7441
  ZipfileEntry *pOld2 = 0;
7442
  int bUpdate = 0;                /* True for an update that modifies "name" */
7443
  int bIsDir = 0;
7444
  u32 iCrc32 = 0;
7445
 
7446
  if( pTab->pWriteFd==0 ){
7447
    rc = zipfileBegin(pVtab);
7448
    if( rc!=SQLITE_OK ) return rc;
7449
  }
7450
 
7451
  /* If this is a DELETE or UPDATE, find the archive entry to delete. */
7452
  if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
7453
    const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
7454
    int nDelete = (int)strlen(zDelete);
7455
    if( nVal>1 ){
7456
      const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
7457
      if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
7458
        bUpdate = 1;
7459
      }
7460
    }
7461
    for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
7462
      if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
7463
        break;
7464
      }
7465
      assert( pOld->pNext );
7466
    }
7467
  }
7468
 
7469
  if( nVal>1 ){
7470
    /* Check that "sz" and "rawdata" are both NULL: */
7471
    if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
7472
      zipfileTableErr(pTab, "sz must be NULL");
7473
      rc = SQLITE_CONSTRAINT;
7474
    }
7475
    if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
7476
      zipfileTableErr(pTab, "rawdata must be NULL");
7477
      rc = SQLITE_CONSTRAINT;
7478
    }
7479
 
7480
    if( rc==SQLITE_OK ){
7481
      if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
7482
        /* data=NULL. A directory */
7483
        bIsDir = 1;
7484
      }else{
7485
        /* Value specified for "data", and possibly "method". This must be
7486
        ** a regular file or a symlink. */
7487
        const u8 *aIn = sqlite3_value_blob(apVal[7]);
7488
        int nIn = sqlite3_value_bytes(apVal[7]);
7489
        int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
7490
 
7491
        iMethod = sqlite3_value_int(apVal[8]);
7492
        sz = nIn;
7493
        pData = aIn;
7494
        nData = nIn;
7495
        if( iMethod!=0 && iMethod!=8 ){
7496
          zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
7497
          rc = SQLITE_CONSTRAINT;
7498
        }else{
7499
          if( bAuto || iMethod ){
7500
            int nCmp;
7501
            rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
7502
            if( rc==SQLITE_OK ){
7503
              if( iMethod || nCmp
7504
                iMethod = 8;
7505
                pData = pFree;
7506
                nData = nCmp;
7507
              }
7508
            }
7509
          }
7510
          iCrc32 = crc32(0, aIn, nIn);
7511
        }
7512
      }
7513
    }
7514
 
7515
    if( rc==SQLITE_OK ){
7516
      rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
7517
    }
7518
 
7519
    if( rc==SQLITE_OK ){
7520
      zPath = (const char*)sqlite3_value_text(apVal[2]);
7521
      if( zPath==0 ) zPath = "";
7522
      nPath = (int)strlen(zPath);
7523
      mTime = zipfileGetTime(apVal[4]);
7524
    }
7525
 
7526
    if( rc==SQLITE_OK && bIsDir ){
7527
      /* For a directory, check that the last character in the path is a
7528
      ** '/'. This appears to be required for compatibility with info-zip
7529
      ** (the unzip command on unix). It does not create directories
7530
      ** otherwise.  */
7531
      if( nPath<=0 || zPath[nPath-1]!='/' ){
7532
        zFree = sqlite3_mprintf("%s/", zPath);
7533
        zPath = (const char*)zFree;
7534
        if( zFree==0 ){
7535
          rc = SQLITE_NOMEM;
7536
          nPath = 0;
7537
        }else{
7538
          nPath = (int)strlen(zPath);
7539
        }
7540
      }
7541
    }
7542
 
7543
    /* Check that we're not inserting a duplicate entry -OR- updating an
7544
    ** entry with a path, thereby making it into a duplicate. */
7545
    if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
7546
      ZipfileEntry *p;
7547
      for(p=pTab->pFirstEntry; p; p=p->pNext){
7548
        if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
7549
          switch( sqlite3_vtab_on_conflict(pTab->db) ){
7550
            case SQLITE_IGNORE: {
7551
              goto zipfile_update_done;
7552
            }
7553
            case SQLITE_REPLACE: {
7554
              pOld2 = p;
7555
              break;
7556
            }
7557
            default: {
7558
              zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
7559
              rc = SQLITE_CONSTRAINT;
7560
              break;
7561
            }
7562
          }
7563
          break;
7564
        }
7565
      }
7566
    }
7567
 
7568
    if( rc==SQLITE_OK ){
7569
      /* Create the new CDS record. */
7570
      pNew = zipfileNewEntry(zPath);
7571
      if( pNew==0 ){
7572
        rc = SQLITE_NOMEM;
7573
      }else{
7574
        pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
7575
        pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
7576
        pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
7577
        pNew->cds.iCompression = (u16)iMethod;
7578
        zipfileMtimeToDos(&pNew->cds, mTime);
7579
        pNew->cds.crc32 = iCrc32;
7580
        pNew->cds.szCompressed = nData;
7581
        pNew->cds.szUncompressed = (u32)sz;
7582
        pNew->cds.iExternalAttr = (mode<<16);
7583
        pNew->cds.iOffset = (u32)pTab->szCurrent;
7584
        pNew->cds.nFile = (u16)nPath;
7585
        pNew->mUnixTime = (u32)mTime;
7586
        rc = zipfileAppendEntry(pTab, pNew, pData, nData);
7587
        zipfileAddEntry(pTab, pOld, pNew);
7588
      }
7589
    }
7590
  }
7591
 
7592
  if( rc==SQLITE_OK && (pOld || pOld2) ){
7593
    ZipfileCsr *pCsr;
7594
    for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
7595
      if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
7596
        pCsr->pCurrent = pCsr->pCurrent->pNext;
7597
        pCsr->bNoop = 1;
7598
      }
7599
    }
7600
 
7601
    zipfileRemoveEntryFromList(pTab, pOld);
7602
    zipfileRemoveEntryFromList(pTab, pOld2);
7603
  }
7604
 
7605
zipfile_update_done:
7606
  sqlite3_free(pFree);
7607
  sqlite3_free(zFree);
7608
  return rc;
7609
}
7610
 
7611
static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
7612
  u8 *a = aBuf;
7613
  zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
7614
  zipfileWrite16(a, p->iDisk);
7615
  zipfileWrite16(a, p->iFirstDisk);
7616
  zipfileWrite16(a, p->nEntry);
7617
  zipfileWrite16(a, p->nEntryTotal);
7618
  zipfileWrite32(a, p->nSize);
7619
  zipfileWrite32(a, p->iOffset);
7620
  zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
7621
 
7622
  return a-aBuf;
7623
}
7624
 
7625
static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
7626
  int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
7627
  assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
7628
  return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
7629
}
7630
 
7631
/*
7632
** Serialize the CDS structure into buffer aBuf[]. Return the number
7633
** of bytes written.
7634
*/
7635
static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
7636
  u8 *a = aBuf;
7637
  ZipfileCDS *pCDS = &pEntry->cds;
7638
 
7639
  if( pEntry->aExtra==0 ){
7640
    pCDS->nExtra = 9;
7641
  }
7642
 
7643
  zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
7644
  zipfileWrite16(a, pCDS->iVersionMadeBy);
7645
  zipfileWrite16(a, pCDS->iVersionExtract);
7646
  zipfileWrite16(a, pCDS->flags);
7647
  zipfileWrite16(a, pCDS->iCompression);
7648
  zipfileWrite16(a, pCDS->mTime);
7649
  zipfileWrite16(a, pCDS->mDate);
7650
  zipfileWrite32(a, pCDS->crc32);
7651
  zipfileWrite32(a, pCDS->szCompressed);
7652
  zipfileWrite32(a, pCDS->szUncompressed);
7653
  assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
7654
  zipfileWrite16(a, pCDS->nFile);
7655
  zipfileWrite16(a, pCDS->nExtra);
7656
  zipfileWrite16(a, pCDS->nComment);
7657
  zipfileWrite16(a, pCDS->iDiskStart);
7658
  zipfileWrite16(a, pCDS->iInternalAttr);
7659
  zipfileWrite32(a, pCDS->iExternalAttr);
7660
  zipfileWrite32(a, pCDS->iOffset);
7661
 
7662
  memcpy(a, pCDS->zFile, pCDS->nFile);
7663
  a += pCDS->nFile;
7664
 
7665
  if( pEntry->aExtra ){
7666
    int n = (int)pCDS->nExtra + (int)pCDS->nComment;
7667
    memcpy(a, pEntry->aExtra, n);
7668
    a += n;
7669
  }else{
7670
    assert( pCDS->nExtra==9 );
7671
    zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
7672
    zipfileWrite16(a, 5);
7673
    *a++ = 0x01;
7674
    zipfileWrite32(a, pEntry->mUnixTime);
7675
  }
7676
 
7677
  return a-aBuf;
7678
}
7679
 
7680
static int zipfileCommit(sqlite3_vtab *pVtab){
7681
  ZipfileTab *pTab = (ZipfileTab*)pVtab;
7682
  int rc = SQLITE_OK;
7683
  if( pTab->pWriteFd ){
7684
    i64 iOffset = pTab->szCurrent;
7685
    ZipfileEntry *p;
7686
    ZipfileEOCD eocd;
7687
    int nEntry = 0;
7688
 
7689
    /* Write out all entries */
7690
    for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
7691
      int n = zipfileSerializeCDS(p, pTab->aBuffer);
7692
      rc = zipfileAppendData(pTab, pTab->aBuffer, n);
7693
      nEntry++;
7694
    }
7695
 
7696
    /* Write out the EOCD record */
7697
    eocd.iDisk = 0;
7698
    eocd.iFirstDisk = 0;
7699
    eocd.nEntry = (u16)nEntry;
7700
    eocd.nEntryTotal = (u16)nEntry;
7701
    eocd.nSize = (u32)(pTab->szCurrent - iOffset);
7702
    eocd.iOffset = (u32)iOffset;
7703
    rc = zipfileAppendEOCD(pTab, &eocd);
7704
 
7705
    zipfileCleanupTransaction(pTab);
7706
  }
7707
  return rc;
7708
}
7709
 
7710
static int zipfileRollback(sqlite3_vtab *pVtab){
7711
  return zipfileCommit(pVtab);
7712
}
7713
 
7714
static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
7715
  ZipfileCsr *pCsr;
7716
  for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
7717
    if( iId==pCsr->iId ) break;
7718
  }
7719
  return pCsr;
7720
}
7721
 
7722
static void zipfileFunctionCds(
7723
  sqlite3_context *context,
7724
  int argc,
7725
  sqlite3_value **argv
7726
){
7727
  ZipfileCsr *pCsr;
7728
  ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
7729
  assert( argc>0 );
7730
 
7731
  pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
7732
  if( pCsr ){
7733
    ZipfileCDS *p = &pCsr->pCurrent->cds;
7734
    char *zRes = sqlite3_mprintf("{"
7735
        "\"version-made-by\" : %u, "
7736
        "\"version-to-extract\" : %u, "
7737
        "\"flags\" : %u, "
7738
        "\"compression\" : %u, "
7739
        "\"time\" : %u, "
7740
        "\"date\" : %u, "
7741
        "\"crc32\" : %u, "
7742
        "\"compressed-size\" : %u, "
7743
        "\"uncompressed-size\" : %u, "
7744
        "\"file-name-length\" : %u, "
7745
        "\"extra-field-length\" : %u, "
7746
        "\"file-comment-length\" : %u, "
7747
        "\"disk-number-start\" : %u, "
7748
        "\"internal-attr\" : %u, "
7749
        "\"external-attr\" : %u, "
7750
        "\"offset\" : %u }",
7751
        (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
7752
        (u32)p->flags, (u32)p->iCompression,
7753
        (u32)p->mTime, (u32)p->mDate,
7754
        (u32)p->crc32, (u32)p->szCompressed,
7755
        (u32)p->szUncompressed, (u32)p->nFile,
7756
        (u32)p->nExtra, (u32)p->nComment,
7757
        (u32)p->iDiskStart, (u32)p->iInternalAttr,
7758
        (u32)p->iExternalAttr, (u32)p->iOffset
7759
    );
7760
 
7761
    if( zRes==0 ){
7762
      sqlite3_result_error_nomem(context);
7763
    }else{
7764
      sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
7765
      sqlite3_free(zRes);
7766
    }
7767
  }
7768
}
7769
 
7770
/*
7771
** xFindFunction method.
7772
*/
7773
static int zipfileFindFunction(
7774
  sqlite3_vtab *pVtab,            /* Virtual table handle */
7775
  int nArg,                       /* Number of SQL function arguments */
7776
  const char *zName,              /* Name of SQL function */
7777
  void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
7778
  void **ppArg                    /* OUT: User data for *pxFunc */
7779
){
7780
  if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
7781
    *pxFunc = zipfileFunctionCds;
7782
    *ppArg = (void*)pVtab;
7783
    return 1;
7784
  }
7785
  return 0;
7786
}
7787
 
7788
typedef struct ZipfileBuffer ZipfileBuffer;
7789
struct ZipfileBuffer {
7790
  u8 *a;                          /* Pointer to buffer */
7791
  int n;                          /* Size of buffer in bytes */
7792
  int nAlloc;                     /* Byte allocated at a[] */
7793
};
7794
 
7795
typedef struct ZipfileCtx ZipfileCtx;
7796
struct ZipfileCtx {
7797
  int nEntry;
7798
  ZipfileBuffer body;
7799
  ZipfileBuffer cds;
7800
};
7801
 
7802
static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
7803
  if( pBuf->n+nByte>pBuf->nAlloc ){
7804
    u8 *aNew;
7805
    sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
7806
    int nReq = pBuf->n + nByte;
7807
 
7808
    while( nNew
7809
    aNew = sqlite3_realloc64(pBuf->a, nNew);
7810
    if( aNew==0 ) return SQLITE_NOMEM;
7811
    pBuf->a = aNew;
7812
    pBuf->nAlloc = (int)nNew;
7813
  }
7814
  return SQLITE_OK;
7815
}
7816
 
7817
/*
7818
** xStep() callback for the zipfile() aggregate. This can be called in
7819
** any of the following ways:
7820
**
7821
**   SELECT zipfile(name,data) ...
7822
**   SELECT zipfile(name,mode,mtime,data) ...
7823
**   SELECT zipfile(name,mode,mtime,data,method) ...
7824
*/
7825
void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
7826
  ZipfileCtx *p;                  /* Aggregate function context */
7827
  ZipfileEntry e;                 /* New entry to add to zip archive */
7828
 
7829
  sqlite3_value *pName = 0;
7830
  sqlite3_value *pMode = 0;
7831
  sqlite3_value *pMtime = 0;
7832
  sqlite3_value *pData = 0;
7833
  sqlite3_value *pMethod = 0;
7834
 
7835
  int bIsDir = 0;
7836
  u32 mode;
7837
  int rc = SQLITE_OK;
7838
  char *zErr = 0;
7839
 
7840
  int iMethod = -1;               /* Compression method to use (0 or 8) */
7841
 
7842
  const u8 *aData = 0;            /* Possibly compressed data for new entry */
7843
  int nData = 0;                  /* Size of aData[] in bytes */
7844
  int szUncompressed = 0;         /* Size of data before compression */
7845
  u8 *aFree = 0;                  /* Free this before returning */
7846
  u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
7847
 
7848
  char *zName = 0;                /* Path (name) of new entry */
7849
  int nName = 0;                  /* Size of zName in bytes */
7850
  char *zFree = 0;                /* Free this before returning */
7851
  int nByte;
7852
 
7853
  memset(&e, 0, sizeof(e));
7854
  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
7855
  if( p==0 ) return;
7856
 
7857
  /* Martial the arguments into stack variables */
7858
  if( nVal!=2 && nVal!=4 && nVal!=5 ){
7859
    zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
7860
    rc = SQLITE_ERROR;
7861
    goto zipfile_step_out;
7862
  }
7863
  pName = apVal[0];
7864
  if( nVal==2 ){
7865
    pData = apVal[1];
7866
  }else{
7867
    pMode = apVal[1];
7868
    pMtime = apVal[2];
7869
    pData = apVal[3];
7870
    if( nVal==5 ){
7871
      pMethod = apVal[4];
7872
    }
7873
  }
7874
 
7875
  /* Check that the 'name' parameter looks ok. */
7876
  zName = (char*)sqlite3_value_text(pName);
7877
  nName = sqlite3_value_bytes(pName);
7878
  if( zName==0 ){
7879
    zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
7880
    rc = SQLITE_ERROR;
7881
    goto zipfile_step_out;
7882
  }
7883
 
7884
  /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
7885
  ** deflate compression) or NULL (choose automatically).  */
7886
  if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
7887
    iMethod = (int)sqlite3_value_int64(pMethod);
7888
    if( iMethod!=0 && iMethod!=8 ){
7889
      zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
7890
      rc = SQLITE_ERROR;
7891
      goto zipfile_step_out;
7892
    }
7893
  }
7894
 
7895
  /* Now inspect the data. If this is NULL, then the new entry must be a
7896
  ** directory.  Otherwise, figure out whether or not the data should
7897
  ** be deflated or simply stored in the zip archive. */
7898
  if( sqlite3_value_type(pData)==SQLITE_NULL ){
7899
    bIsDir = 1;
7900
    iMethod = 0;
7901
  }else{
7902
    aData = sqlite3_value_blob(pData);
7903
    szUncompressed = nData = sqlite3_value_bytes(pData);
7904
    iCrc32 = crc32(0, aData, nData);
7905
    if( iMethod<0 || iMethod==8 ){
7906
      int nOut = 0;
7907
      rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
7908
      if( rc!=SQLITE_OK ){
7909
        goto zipfile_step_out;
7910
      }
7911
      if( iMethod==8 || nOut
7912
        aData = aFree;
7913
        nData = nOut;
7914
        iMethod = 8;
7915
      }else{
7916
        iMethod = 0;
7917
      }
7918
    }
7919
  }
7920
 
7921
  /* Decode the "mode" argument. */
7922
  rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
7923
  if( rc ) goto zipfile_step_out;
7924
 
7925
  /* Decode the "mtime" argument. */
7926
  e.mUnixTime = zipfileGetTime(pMtime);
7927
 
7928
  /* If this is a directory entry, ensure that there is exactly one '/'
7929
  ** at the end of the path. Or, if this is not a directory and the path
7930
  ** ends in '/' it is an error. */
7931
  if( bIsDir==0 ){
7932
    if( nName>0 && zName[nName-1]=='/' ){
7933
      zErr = sqlite3_mprintf("non-directory name must not end with /");
7934
      rc = SQLITE_ERROR;
7935
      goto zipfile_step_out;
7936
    }
7937
  }else{
7938
    if( nName==0 || zName[nName-1]!='/' ){
7939
      zName = zFree = sqlite3_mprintf("%s/", zName);
7940
      if( zName==0 ){
7941
        rc = SQLITE_NOMEM;
7942
        goto zipfile_step_out;
7943
      }
7944
      nName = (int)strlen(zName);
7945
    }else{
7946
      while( nName>1 && zName[nName-2]=='/' ) nName--;
7947
    }
7948
  }
7949
 
7950
  /* Assemble the ZipfileEntry object for the new zip archive entry */
7951
  e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
7952
  e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
7953
  e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
7954
  e.cds.iCompression = (u16)iMethod;
7955
  zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
7956
  e.cds.crc32 = iCrc32;
7957
  e.cds.szCompressed = nData;
7958
  e.cds.szUncompressed = szUncompressed;
7959
  e.cds.iExternalAttr = (mode<<16);
7960
  e.cds.iOffset = p->body.n;
7961
  e.cds.nFile = (u16)nName;
7962
  e.cds.zFile = zName;
7963
 
7964
  /* Append the LFH to the body of the new archive */
7965
  nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
7966
  if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
7967
  p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
7968
 
7969
  /* Append the data to the body of the new archive */
7970
  if( nData>0 ){
7971
    if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
7972
    memcpy(&p->body.a[p->body.n], aData, nData);
7973
    p->body.n += nData;
7974
  }
7975
 
7976
  /* Append the CDS record to the directory of the new archive */
7977
  nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
7978
  if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
7979
  p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
7980
 
7981
  /* Increment the count of entries in the archive */
7982
  p->nEntry++;
7983
 
7984
 zipfile_step_out:
7985
  sqlite3_free(aFree);
7986
  sqlite3_free(zFree);
7987
  if( rc ){
7988
    if( zErr ){
7989
      sqlite3_result_error(pCtx, zErr, -1);
7990
    }else{
7991
      sqlite3_result_error_code(pCtx, rc);
7992
    }
7993
  }
7994
  sqlite3_free(zErr);
7995
}
7996
 
7997
/*
7998
** xFinalize() callback for zipfile aggregate function.
7999
*/
8000
void zipfileFinal(sqlite3_context *pCtx){
8001
  ZipfileCtx *p;
8002
  ZipfileEOCD eocd;
8003
  sqlite3_int64 nZip;
8004
  u8 *aZip;
8005
 
8006
  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
8007
  if( p==0 ) return;
8008
  if( p->nEntry>0 ){
8009
    memset(&eocd, 0, sizeof(eocd));
8010
    eocd.nEntry = (u16)p->nEntry;
8011
    eocd.nEntryTotal = (u16)p->nEntry;
8012
    eocd.nSize = p->cds.n;
8013
    eocd.iOffset = p->body.n;
8014
 
8015
    nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
8016
    aZip = (u8*)sqlite3_malloc64(nZip);
8017
    if( aZip==0 ){
8018
      sqlite3_result_error_nomem(pCtx);
8019
    }else{
8020
      memcpy(aZip, p->body.a, p->body.n);
8021
      memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
8022
      zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
8023
      sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
8024
    }
8025
  }
8026
 
8027
  sqlite3_free(p->body.a);
8028
  sqlite3_free(p->cds.a);
8029
}
8030
 
8031
 
8032
/*
8033
** Register the "zipfile" virtual table.
8034
*/
8035
static int zipfileRegister(sqlite3 *db){
8036
  static sqlite3_module zipfileModule = {
8037
    1,                         /* iVersion */
8038
    zipfileConnect,            /* xCreate */
8039
    zipfileConnect,            /* xConnect */
8040
    zipfileBestIndex,          /* xBestIndex */
8041
    zipfileDisconnect,         /* xDisconnect */
8042
    zipfileDisconnect,         /* xDestroy */
8043
    zipfileOpen,               /* xOpen - open a cursor */
8044
    zipfileClose,              /* xClose - close a cursor */
8045
    zipfileFilter,             /* xFilter - configure scan constraints */
8046
    zipfileNext,               /* xNext - advance a cursor */
8047
    zipfileEof,                /* xEof - check for end of scan */
8048
    zipfileColumn,             /* xColumn - read data */
8049
    0,                         /* xRowid - read data */
8050
    zipfileUpdate,             /* xUpdate */
8051
    zipfileBegin,              /* xBegin */
8052
    0,                         /* xSync */
8053
    zipfileCommit,             /* xCommit */
8054
    zipfileRollback,           /* xRollback */
8055
    zipfileFindFunction,       /* xFindMethod */
8056
    0,                         /* xRename */
8057
  };
8058
 
8059
  int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
8060
  if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
8061
  if( rc==SQLITE_OK ){
8062
    rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
8063
        zipfileStep, zipfileFinal
8064
    );
8065
  }
8066
  return rc;
8067
}
8068
#else         /* SQLITE_OMIT_VIRTUALTABLE */
8069
# define zipfileRegister(x) SQLITE_OK
8070
#endif
8071
 
8072
#ifdef _WIN32
8073
 
8074
#endif
8075
int sqlite3_zipfile_init(
8076
  sqlite3 *db,
8077
  char **pzErrMsg,
8078
  const sqlite3_api_routines *pApi
8079
){
8080
  SQLITE_EXTENSION_INIT2(pApi);
8081
  (void)pzErrMsg;  /* Unused parameter */
8082
  return zipfileRegister(db);
8083
}
8084
 
8085
/************************* End ../ext/misc/zipfile.c ********************/
8086
/************************* Begin ../ext/misc/sqlar.c ******************/
8087
/*
8088
** 2017-12-17
8089
**
8090
** The author disclaims copyright to this source code.  In place of
8091
** a legal notice, here is a blessing:
8092
**
8093
**    May you do good and not evil.
8094
**    May you find forgiveness for yourself and forgive others.
8095
**    May you share freely, never taking more than you give.
8096
**
8097
******************************************************************************
8098
**
8099
** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
8100
** for working with sqlar archives and used by the shell tool's built-in
8101
** sqlar support.
8102
*/
8103
/* #include "sqlite3ext.h" */
8104
SQLITE_EXTENSION_INIT1
8105
#include 
8106
#include 
8107
 
8108
/*
8109
** Implementation of the "sqlar_compress(X)" SQL function.
8110
**
8111
** If the type of X is SQLITE_BLOB, and compressing that blob using
8112
** zlib utility function compress() yields a smaller blob, return the
8113
** compressed blob. Otherwise, return a copy of X.
8114
**
8115
** SQLar uses the "zlib format" for compressed content.  The zlib format
8116
** contains a two-byte identification header and a four-byte checksum at
8117
** the end.  This is different from ZIP which uses the raw deflate format.
8118
**
8119
** Future enhancements to SQLar might add support for new compression formats.
8120
** If so, those new formats will be identified by alternative headers in the
8121
** compressed data.
8122
*/
8123
static void sqlarCompressFunc(
8124
  sqlite3_context *context,
8125
  int argc,
8126
  sqlite3_value **argv
8127
){
8128
  assert( argc==1 );
8129
  if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
8130
    const Bytef *pData = sqlite3_value_blob(argv[0]);
8131
    uLong nData = sqlite3_value_bytes(argv[0]);
8132
    uLongf nOut = compressBound(nData);
8133
    Bytef *pOut;
8134
 
8135
    pOut = (Bytef*)sqlite3_malloc(nOut);
8136
    if( pOut==0 ){
8137
      sqlite3_result_error_nomem(context);
8138
      return;
8139
    }else{
8140
      if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
8141
        sqlite3_result_error(context, "error in compress()", -1);
8142
      }else if( nOut
8143
        sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
8144
      }else{
8145
        sqlite3_result_value(context, argv[0]);
8146
      }
8147
      sqlite3_free(pOut);
8148
    }
8149
  }else{
8150
    sqlite3_result_value(context, argv[0]);
8151
  }
8152
}
8153
 
8154
/*
8155
** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
8156
**
8157
** Parameter SZ is interpreted as an integer. If it is less than or
8158
** equal to zero, then this function returns a copy of X. Or, if
8159
** SZ is equal to the size of X when interpreted as a blob, also
8160
** return a copy of X. Otherwise, decompress blob X using zlib
8161
** utility function uncompress() and return the results (another
8162
** blob).
8163
*/
8164
static void sqlarUncompressFunc(
8165
  sqlite3_context *context,
8166
  int argc,
8167
  sqlite3_value **argv
8168
){
8169
  uLong nData;
8170
  uLongf sz;
8171
 
8172
  assert( argc==2 );
8173
  sz = sqlite3_value_int(argv[1]);
8174
 
8175
  if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
8176
    sqlite3_result_value(context, argv[0]);
8177
  }else{
8178
    const Bytef *pData= sqlite3_value_blob(argv[0]);
8179
    Bytef *pOut = sqlite3_malloc(sz);
8180
    if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
8181
      sqlite3_result_error(context, "error in uncompress()", -1);
8182
    }else{
8183
      sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
8184
    }
8185
    sqlite3_free(pOut);
8186
  }
8187
}
8188
 
8189
 
8190
#ifdef _WIN32
8191
 
8192
#endif
8193
int sqlite3_sqlar_init(
8194
  sqlite3 *db,
8195
  char **pzErrMsg,
8196
  const sqlite3_api_routines *pApi
8197
){
8198
  int rc = SQLITE_OK;
8199
  SQLITE_EXTENSION_INIT2(pApi);
8200
  (void)pzErrMsg;  /* Unused parameter */
8201
  rc = sqlite3_create_function(db, "sqlar_compress", 1,
8202
                               SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8203
                               sqlarCompressFunc, 0, 0);
8204
  if( rc==SQLITE_OK ){
8205
    rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
8206
                                 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8207
                                 sqlarUncompressFunc, 0, 0);
8208
  }
8209
  return rc;
8210
}
8211
 
8212
/************************* End ../ext/misc/sqlar.c ********************/
8213
#endif
8214
/************************* Begin ../ext/expert/sqlite3expert.h ******************/
8215
/*
8216
** 2017 April 07
8217
**
8218
** The author disclaims copyright to this source code.  In place of
8219
** a legal notice, here is a blessing:
8220
**
8221
**    May you do good and not evil.
8222
**    May you find forgiveness for yourself and forgive others.
8223
**    May you share freely, never taking more than you give.
8224
**
8225
*************************************************************************
8226
*/
8227
#if !defined(SQLITEEXPERT_H)
8228
#define SQLITEEXPERT_H 1
8229
/* #include "sqlite3.h" */
8230
 
8231
typedef struct sqlite3expert sqlite3expert;
8232
 
8233
/*
8234
** Create a new sqlite3expert object.
8235
**
8236
** If successful, a pointer to the new object is returned and (*pzErr) set
8237
** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
8238
** an English-language error message. In this case it is the responsibility
8239
** of the caller to eventually free the error message buffer using
8240
** sqlite3_free().
8241
*/
8242
sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
8243
 
8244
/*
8245
** Configure an sqlite3expert object.
8246
**
8247
** EXPERT_CONFIG_SAMPLE:
8248
**   By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
8249
**   each candidate index. This involves scanning and sorting the entire
8250
**   contents of each user database table once for each candidate index
8251
**   associated with the table. For large databases, this can be
8252
**   prohibitively slow. This option allows the sqlite3expert object to
8253
**   be configured so that sqlite_stat1 data is instead generated based on a
8254
**   subset of each table, or so that no sqlite_stat1 data is used at all.
8255
**
8256
**   A single integer argument is passed to this option. If the value is less
8257
**   than or equal to zero, then no sqlite_stat1 data is generated or used by
8258
**   the analysis - indexes are recommended based on the database schema only.
8259
**   Or, if the value is 100 or greater, complete sqlite_stat1 data is
8260
**   generated for each candidate index (this is the default). Finally, if the
8261
**   value falls between 0 and 100, then it represents the percentage of user
8262
**   table rows that should be considered when generating sqlite_stat1 data.
8263
**
8264
**   Examples:
8265
**
8266
**     // Do not generate any sqlite_stat1 data
8267
**     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
8268
**
8269
**     // Generate sqlite_stat1 data based on 10% of the rows in each table.
8270
**     sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
8271
*/
8272
int sqlite3_expert_config(sqlite3expert *p, int op, ...);
8273
 
8274
#define EXPERT_CONFIG_SAMPLE 1    /* int */
8275
 
8276
/*
8277
** Specify zero or more SQL statements to be included in the analysis.
8278
**
8279
** Buffer zSql must contain zero or more complete SQL statements. This
8280
** function parses all statements contained in the buffer and adds them
8281
** to the internal list of statements to analyze. If successful, SQLITE_OK
8282
** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
8283
** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
8284
** may be set to point to an English language error message. In this case
8285
** the caller is responsible for eventually freeing the error message buffer
8286
** using sqlite3_free().
8287
**
8288
** If an error does occur while processing one of the statements in the
8289
** buffer passed as the second argument, none of the statements in the
8290
** buffer are added to the analysis.
8291
**
8292
** This function must be called before sqlite3_expert_analyze(). If a call
8293
** to this function is made on an sqlite3expert object that has already
8294
** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
8295
** immediately and no statements are added to the analysis.
8296
*/
8297
int sqlite3_expert_sql(
8298
  sqlite3expert *p,               /* From a successful sqlite3_expert_new() */
8299
  const char *zSql,               /* SQL statement(s) to add */
8300
  char **pzErr                    /* OUT: Error message (if any) */
8301
);
8302
 
8303
 
8304
/*
8305
** This function is called after the sqlite3expert object has been configured
8306
** with all SQL statements using sqlite3_expert_sql() to actually perform
8307
** the analysis. Once this function has been called, it is not possible to
8308
** add further SQL statements to the analysis.
8309
**
8310
** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
8311
** an error occurs, an SQLite error code is returned and (*pzErr) set to
8312
** point to a buffer containing an English language error message. In this
8313
** case it is the responsibility of the caller to eventually free the buffer
8314
** using sqlite3_free().
8315
**
8316
** If an error does occur within this function, the sqlite3expert object
8317
** is no longer useful for any purpose. At that point it is no longer
8318
** possible to add further SQL statements to the object or to re-attempt
8319
** the analysis. The sqlite3expert object must still be freed using a call
8320
** sqlite3_expert_destroy().
8321
*/
8322
int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
8323
 
8324
/*
8325
** Return the total number of statements loaded using sqlite3_expert_sql().
8326
** The total number of SQL statements may be different from the total number
8327
** to calls to sqlite3_expert_sql().
8328
*/
8329
int sqlite3_expert_count(sqlite3expert*);
8330
 
8331
/*
8332
** Return a component of the report.
8333
**
8334
** This function is called after sqlite3_expert_analyze() to extract the
8335
** results of the analysis. Each call to this function returns either a
8336
** NULL pointer or a pointer to a buffer containing a nul-terminated string.
8337
** The value passed as the third argument must be one of the EXPERT_REPORT_*
8338
** #define constants defined below.
8339
**
8340
** For some EXPERT_REPORT_* parameters, the buffer returned contains
8341
** information relating to a specific SQL statement. In these cases that
8342
** SQL statement is identified by the value passed as the second argument.
8343
** SQL statements are numbered from 0 in the order in which they are parsed.
8344
** If an out-of-range value (less than zero or equal to or greater than the
8345
** value returned by sqlite3_expert_count()) is passed as the second argument
8346
** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
8347
**
8348
** EXPERT_REPORT_SQL:
8349
**   Return the text of SQL statement iStmt.
8350
**
8351
** EXPERT_REPORT_INDEXES:
8352
**   Return a buffer containing the CREATE INDEX statements for all recommended
8353
**   indexes for statement iStmt. If there are no new recommeded indexes, NULL
8354
**   is returned.
8355
**
8356
** EXPERT_REPORT_PLAN:
8357
**   Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
8358
**   iStmt after the proposed indexes have been added to the database schema.
8359
**
8360
** EXPERT_REPORT_CANDIDATES:
8361
**   Return a pointer to a buffer containing the CREATE INDEX statements
8362
**   for all indexes that were tested (for all SQL statements). The iStmt
8363
**   parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
8364
*/
8365
const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
8366
 
8367
/*
8368
** Values for the third argument passed to sqlite3_expert_report().
8369
*/
8370
#define EXPERT_REPORT_SQL        1
8371
#define EXPERT_REPORT_INDEXES    2
8372
#define EXPERT_REPORT_PLAN       3
8373
#define EXPERT_REPORT_CANDIDATES 4
8374
 
8375
/*
8376
** Free an (sqlite3expert*) handle and all associated resources. There
8377
** should be one call to this function for each successful call to
8378
** sqlite3-expert_new().
8379
*/
8380
void sqlite3_expert_destroy(sqlite3expert*);
8381
 
8382
#endif  /* !defined(SQLITEEXPERT_H) */
8383
 
8384
/************************* End ../ext/expert/sqlite3expert.h ********************/
8385
/************************* Begin ../ext/expert/sqlite3expert.c ******************/
8386
/*
8387
** 2017 April 09
8388
**
8389
** The author disclaims copyright to this source code.  In place of
8390
** a legal notice, here is a blessing:
8391
**
8392
**    May you do good and not evil.
8393
**    May you find forgiveness for yourself and forgive others.
8394
**    May you share freely, never taking more than you give.
8395
**
8396
*************************************************************************
8397
*/
8398
/* #include "sqlite3expert.h" */
8399
#include 
8400
#include 
8401
#include 
8402
 
8403
#ifndef SQLITE_OMIT_VIRTUALTABLE
8404
 
8405
/* typedef sqlite3_int64 i64; */
8406
/* typedef sqlite3_uint64 u64; */
8407
 
8408
typedef struct IdxColumn IdxColumn;
8409
typedef struct IdxConstraint IdxConstraint;
8410
typedef struct IdxScan IdxScan;
8411
typedef struct IdxStatement IdxStatement;
8412
typedef struct IdxTable IdxTable;
8413
typedef struct IdxWrite IdxWrite;
8414
 
8415
#define STRLEN  (int)strlen
8416
 
8417
/*
8418
** A temp table name that we assume no user database will actually use.
8419
** If this assumption proves incorrect triggers on the table with the
8420
** conflicting name will be ignored.
8421
*/
8422
#define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
8423
 
8424
/*
8425
** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
8426
** any other type of single-ended range constraint on a column).
8427
**
8428
** pLink:
8429
**   Used to temporarily link IdxConstraint objects into lists while
8430
**   creating candidate indexes.
8431
*/
8432
struct IdxConstraint {
8433
  char *zColl;                    /* Collation sequence */
8434
  int bRange;                     /* True for range, false for eq */
8435
  int iCol;                       /* Constrained table column */
8436
  int bFlag;                      /* Used by idxFindCompatible() */
8437
  int bDesc;                      /* True if ORDER BY  DESC */
8438
  IdxConstraint *pNext;           /* Next constraint in pEq or pRange list */
8439
  IdxConstraint *pLink;           /* See above */
8440
};
8441
 
8442
/*
8443
** A single scan of a single table.
8444
*/
8445
struct IdxScan {
8446
  IdxTable *pTab;                 /* Associated table object */
8447
  int iDb;                        /* Database containing table zTable */
8448
  i64 covering;                   /* Mask of columns required for cov. index */
8449
  IdxConstraint *pOrder;          /* ORDER BY columns */
8450
  IdxConstraint *pEq;             /* List of == constraints */
8451
  IdxConstraint *pRange;          /* List of < constraints */
8452
  IdxScan *pNextScan;             /* Next IdxScan object for same analysis */
8453
};
8454
 
8455
/*
8456
** Information regarding a single database table. Extracted from
8457
** "PRAGMA table_info" by function idxGetTableInfo().
8458
*/
8459
struct IdxColumn {
8460
  char *zName;
8461
  char *zColl;
8462
  int iPk;
8463
};
8464
struct IdxTable {
8465
  int nCol;
8466
  char *zName;                    /* Table name */
8467
  IdxColumn *aCol;
8468
  IdxTable *pNext;                /* Next table in linked list of all tables */
8469
};
8470
 
8471
/*
8472
** An object of the following type is created for each unique table/write-op
8473
** seen. The objects are stored in a singly-linked list beginning at
8474
** sqlite3expert.pWrite.
8475
*/
8476
struct IdxWrite {
8477
  IdxTable *pTab;
8478
  int eOp;                        /* SQLITE_UPDATE, DELETE or INSERT */
8479
  IdxWrite *pNext;
8480
};
8481
 
8482
/*
8483
** Each statement being analyzed is represented by an instance of this
8484
** structure.
8485
*/
8486
struct IdxStatement {
8487
  int iId;                        /* Statement number */
8488
  char *zSql;                     /* SQL statement */
8489
  char *zIdx;                     /* Indexes */
8490
  char *zEQP;                     /* Plan */
8491
  IdxStatement *pNext;
8492
};
8493
 
8494
 
8495
/*
8496
** A hash table for storing strings. With space for a payload string
8497
** with each entry. Methods are:
8498
**
8499
**   idxHashInit()
8500
**   idxHashClear()
8501
**   idxHashAdd()
8502
**   idxHashSearch()
8503
*/
8504
#define IDX_HASH_SIZE 1023
8505
typedef struct IdxHashEntry IdxHashEntry;
8506
typedef struct IdxHash IdxHash;
8507
struct IdxHashEntry {
8508
  char *zKey;                     /* nul-terminated key */
8509
  char *zVal;                     /* nul-terminated value string */
8510
  char *zVal2;                    /* nul-terminated value string 2 */
8511
  IdxHashEntry *pHashNext;        /* Next entry in same hash bucket */
8512
  IdxHashEntry *pNext;            /* Next entry in hash */
8513
};
8514
struct IdxHash {
8515
  IdxHashEntry *pFirst;
8516
  IdxHashEntry *aHash[IDX_HASH_SIZE];
8517
};
8518
 
8519
/*
8520
** sqlite3expert object.
8521
*/
8522
struct sqlite3expert {
8523
  int iSample;                    /* Percentage of tables to sample for stat1 */
8524
  sqlite3 *db;                    /* User database */
8525
  sqlite3 *dbm;                   /* In-memory db for this analysis */
8526
  sqlite3 *dbv;                   /* Vtab schema for this analysis */
8527
  IdxTable *pTable;               /* List of all IdxTable objects */
8528
  IdxScan *pScan;                 /* List of scan objects */
8529
  IdxWrite *pWrite;               /* List of write objects */
8530
  IdxStatement *pStatement;       /* List of IdxStatement objects */
8531
  int bRun;                       /* True once analysis has run */
8532
  char **pzErrmsg;
8533
  int rc;                         /* Error code from whereinfo hook */
8534
  IdxHash hIdx;                   /* Hash containing all candidate indexes */
8535
  char *zCandidates;              /* For EXPERT_REPORT_CANDIDATES */
8536
};
8537
 
8538
 
8539
/*
8540
** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
8541
** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
8542
*/
8543
static void *idxMalloc(int *pRc, int nByte){
8544
  void *pRet;
8545
  assert( *pRc==SQLITE_OK );
8546
  assert( nByte>0 );
8547
  pRet = sqlite3_malloc(nByte);
8548
  if( pRet ){
8549
    memset(pRet, 0, nByte);
8550
  }else{
8551
    *pRc = SQLITE_NOMEM;
8552
  }
8553
  return pRet;
8554
}
8555
 
8556
/*
8557
** Initialize an IdxHash hash table.
8558
*/
8559
static void idxHashInit(IdxHash *pHash){
8560
  memset(pHash, 0, sizeof(IdxHash));
8561
}
8562
 
8563
/*
8564
** Reset an IdxHash hash table.
8565
*/
8566
static void idxHashClear(IdxHash *pHash){
8567
  int i;
8568
  for(i=0; i
8569
    IdxHashEntry *pEntry;
8570
    IdxHashEntry *pNext;
8571
    for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
8572
      pNext = pEntry->pHashNext;
8573
      sqlite3_free(pEntry->zVal2);
8574
      sqlite3_free(pEntry);
8575
    }
8576
  }
8577
  memset(pHash, 0, sizeof(IdxHash));
8578
}
8579
 
8580
/*
8581
** Return the index of the hash bucket that the string specified by the
8582
** arguments to this function belongs.
8583
*/
8584
static int idxHashString(const char *z, int n){
8585
  unsigned int ret = 0;
8586
  int i;
8587
  for(i=0; i
8588
    ret += (ret<<3) + (unsigned char)(z[i]);
8589
  }
8590
  return (int)(ret % IDX_HASH_SIZE);
8591
}
8592
 
8593
/*
8594
** If zKey is already present in the hash table, return non-zero and do
8595
** nothing. Otherwise, add an entry with key zKey and payload string zVal to
8596
** the hash table passed as the second argument.
8597
*/
8598
static int idxHashAdd(
8599
  int *pRc,
8600
  IdxHash *pHash,
8601
  const char *zKey,
8602
  const char *zVal
8603
){
8604
  int nKey = STRLEN(zKey);
8605
  int iHash = idxHashString(zKey, nKey);
8606
  int nVal = (zVal ? STRLEN(zVal) : 0);
8607
  IdxHashEntry *pEntry;
8608
  assert( iHash>=0 );
8609
  for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
8610
    if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
8611
      return 1;
8612
    }
8613
  }
8614
  pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
8615
  if( pEntry ){
8616
    pEntry->zKey = (char*)&pEntry[1];
8617
    memcpy(pEntry->zKey, zKey, nKey);
8618
    if( zVal ){
8619
      pEntry->zVal = &pEntry->zKey[nKey+1];
8620
      memcpy(pEntry->zVal, zVal, nVal);
8621
    }
8622
    pEntry->pHashNext = pHash->aHash[iHash];
8623
    pHash->aHash[iHash] = pEntry;
8624
 
8625
    pEntry->pNext = pHash->pFirst;
8626
    pHash->pFirst = pEntry;
8627
  }
8628
  return 0;
8629
}
8630
 
8631
/*
8632
** If zKey/nKey is present in the hash table, return a pointer to the
8633
** hash-entry object.
8634
*/
8635
static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
8636
  int iHash;
8637
  IdxHashEntry *pEntry;
8638
  if( nKey<0 ) nKey = STRLEN(zKey);
8639
  iHash = idxHashString(zKey, nKey);
8640
  assert( iHash>=0 );
8641
  for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
8642
    if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
8643
      return pEntry;
8644
    }
8645
  }
8646
  return 0;
8647
}
8648
 
8649
/*
8650
** If the hash table contains an entry with a key equal to the string
8651
** passed as the final two arguments to this function, return a pointer
8652
** to the payload string. Otherwise, if zKey/nKey is not present in the
8653
** hash table, return NULL.
8654
*/
8655
static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
8656
  IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
8657
  if( pEntry ) return pEntry->zVal;
8658
  return 0;
8659
}
8660
 
8661
/*
8662
** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
8663
** variable to point to a copy of nul-terminated string zColl.
8664
*/
8665
static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
8666
  IdxConstraint *pNew;
8667
  int nColl = STRLEN(zColl);
8668
 
8669
  assert( *pRc==SQLITE_OK );
8670
  pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
8671
  if( pNew ){
8672
    pNew->zColl = (char*)&pNew[1];
8673
    memcpy(pNew->zColl, zColl, nColl+1);
8674
  }
8675
  return pNew;
8676
}
8677
 
8678
/*
8679
** An error associated with database handle db has just occurred. Pass
8680
** the error message to callback function xOut.
8681
*/
8682
static void idxDatabaseError(
8683
  sqlite3 *db,                    /* Database handle */
8684
  char **pzErrmsg                 /* Write error here */
8685
){
8686
  *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
8687
}
8688
 
8689
/*
8690
** Prepare an SQL statement.
8691
*/
8692
static int idxPrepareStmt(
8693
  sqlite3 *db,                    /* Database handle to compile against */
8694
  sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
8695
  char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
8696
  const char *zSql                /* SQL statement to compile */
8697
){
8698
  int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
8699
  if( rc!=SQLITE_OK ){
8700
    *ppStmt = 0;
8701
    idxDatabaseError(db, pzErrmsg);
8702
  }
8703
  return rc;
8704
}
8705
 
8706
/*
8707
** Prepare an SQL statement using the results of a printf() formatting.
8708
*/
8709
static int idxPrintfPrepareStmt(
8710
  sqlite3 *db,                    /* Database handle to compile against */
8711
  sqlite3_stmt **ppStmt,          /* OUT: Compiled SQL statement */
8712
  char **pzErrmsg,                /* OUT: sqlite3_malloc()ed error message */
8713
  const char *zFmt,               /* printf() format of SQL statement */
8714
  ...                             /* Trailing printf() arguments */
8715
){
8716
  va_list ap;
8717
  int rc;
8718
  char *zSql;
8719
  va_start(ap, zFmt);
8720
  zSql = sqlite3_vmprintf(zFmt, ap);
8721
  if( zSql==0 ){
8722
    rc = SQLITE_NOMEM;
8723
  }else{
8724
    rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
8725
    sqlite3_free(zSql);
8726
  }
8727
  va_end(ap);
8728
  return rc;
8729
}
8730
 
8731
 
8732
/*************************************************************************
8733
** Beginning of virtual table implementation.
8734
*/
8735
typedef struct ExpertVtab ExpertVtab;
8736
struct ExpertVtab {
8737
  sqlite3_vtab base;
8738
  IdxTable *pTab;
8739
  sqlite3expert *pExpert;
8740
};
8741
 
8742
typedef struct ExpertCsr ExpertCsr;
8743
struct ExpertCsr {
8744
  sqlite3_vtab_cursor base;
8745
  sqlite3_stmt *pData;
8746
};
8747
 
8748
static char *expertDequote(const char *zIn){
8749
  int n = STRLEN(zIn);
8750
  char *zRet = sqlite3_malloc(n);
8751
 
8752
  assert( zIn[0]=='\'' );
8753
  assert( zIn[n-1]=='\'' );
8754
 
8755
  if( zRet ){
8756
    int iOut = 0;
8757
    int iIn = 0;
8758
    for(iIn=1; iIn<(n-1); iIn++){
8759
      if( zIn[iIn]=='\'' ){
8760
        assert( zIn[iIn+1]=='\'' );
8761
        iIn++;
8762
      }
8763
      zRet[iOut++] = zIn[iIn];
8764
    }
8765
    zRet[iOut] = '\0';
8766
  }
8767
 
8768
  return zRet;
8769
}
8770
 
8771
/*
8772
** This function is the implementation of both the xConnect and xCreate
8773
** methods of the r-tree virtual table.
8774
**
8775
**   argv[0]   -> module name
8776
**   argv[1]   -> database name
8777
**   argv[2]   -> table name
8778
**   argv[...] -> column names...
8779
*/
8780
static int expertConnect(
8781
  sqlite3 *db,
8782
  void *pAux,
8783
  int argc, const char *const*argv,
8784
  sqlite3_vtab **ppVtab,
8785
  char **pzErr
8786
){
8787
  sqlite3expert *pExpert = (sqlite3expert*)pAux;
8788
  ExpertVtab *p = 0;
8789
  int rc;
8790
 
8791
  if( argc!=4 ){
8792
    *pzErr = sqlite3_mprintf("internal error!");
8793
    rc = SQLITE_ERROR;
8794
  }else{
8795
    char *zCreateTable = expertDequote(argv[3]);
8796
    if( zCreateTable ){
8797
      rc = sqlite3_declare_vtab(db, zCreateTable);
8798
      if( rc==SQLITE_OK ){
8799
        p = idxMalloc(&rc, sizeof(ExpertVtab));
8800
      }
8801
      if( rc==SQLITE_OK ){
8802
        p->pExpert = pExpert;
8803
        p->pTab = pExpert->pTable;
8804
        assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
8805
      }
8806
      sqlite3_free(zCreateTable);
8807
    }else{
8808
      rc = SQLITE_NOMEM;
8809
    }
8810
  }
8811
 
8812
  *ppVtab = (sqlite3_vtab*)p;
8813
  return rc;
8814
}
8815
 
8816
static int expertDisconnect(sqlite3_vtab *pVtab){
8817
  ExpertVtab *p = (ExpertVtab*)pVtab;
8818
  sqlite3_free(p);
8819
  return SQLITE_OK;
8820
}
8821
 
8822
static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
8823
  ExpertVtab *p = (ExpertVtab*)pVtab;
8824
  int rc = SQLITE_OK;
8825
  int n = 0;
8826
  IdxScan *pScan;
8827
  const int opmask =
8828
    SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
8829
    SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
8830
    SQLITE_INDEX_CONSTRAINT_LE;
8831
 
8832
  pScan = idxMalloc(&rc, sizeof(IdxScan));
8833
  if( pScan ){
8834
    int i;
8835
 
8836
    /* Link the new scan object into the list */
8837
    pScan->pTab = p->pTab;
8838
    pScan->pNextScan = p->pExpert->pScan;
8839
    p->pExpert->pScan = pScan;
8840
 
8841
    /* Add the constraints to the IdxScan object */
8842
    for(i=0; inConstraint; i++){
8843
      struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
8844
      if( pCons->usable
8845
       && pCons->iColumn>=0
8846
       && p->pTab->aCol[pCons->iColumn].iPk==0
8847
       && (pCons->op & opmask)
8848
      ){
8849
        IdxConstraint *pNew;
8850
        const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
8851
        pNew = idxNewConstraint(&rc, zColl);
8852
        if( pNew ){
8853
          pNew->iCol = pCons->iColumn;
8854
          if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
8855
            pNew->pNext = pScan->pEq;
8856
            pScan->pEq = pNew;
8857
          }else{
8858
            pNew->bRange = 1;
8859
            pNew->pNext = pScan->pRange;
8860
            pScan->pRange = pNew;
8861
          }
8862
        }
8863
        n++;
8864
        pIdxInfo->aConstraintUsage[i].argvIndex = n;
8865
      }
8866
    }
8867
 
8868
    /* Add the ORDER BY to the IdxScan object */
8869
    for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
8870
      int iCol = pIdxInfo->aOrderBy[i].iColumn;
8871
      if( iCol>=0 ){
8872
        IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
8873
        if( pNew ){
8874
          pNew->iCol = iCol;
8875
          pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
8876
          pNew->pNext = pScan->pOrder;
8877
          pNew->pLink = pScan->pOrder;
8878
          pScan->pOrder = pNew;
8879
          n++;
8880
        }
8881
      }
8882
    }
8883
  }
8884
 
8885
  pIdxInfo->estimatedCost = 1000000.0 / (n+1);
8886
  return rc;
8887
}
8888
 
8889
static int expertUpdate(
8890
  sqlite3_vtab *pVtab,
8891
  int nData,
8892
  sqlite3_value **azData,
8893
  sqlite_int64 *pRowid
8894
){
8895
  (void)pVtab;
8896
  (void)nData;
8897
  (void)azData;
8898
  (void)pRowid;
8899
  return SQLITE_OK;
8900
}
8901
 
8902
/*
8903
** Virtual table module xOpen method.
8904
*/
8905
static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
8906
  int rc = SQLITE_OK;
8907
  ExpertCsr *pCsr;
8908
  (void)pVTab;
8909
  pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
8910
  *ppCursor = (sqlite3_vtab_cursor*)pCsr;
8911
  return rc;
8912
}
8913
 
8914
/*
8915
** Virtual table module xClose method.
8916
*/
8917
static int expertClose(sqlite3_vtab_cursor *cur){
8918
  ExpertCsr *pCsr = (ExpertCsr*)cur;
8919
  sqlite3_finalize(pCsr->pData);
8920
  sqlite3_free(pCsr);
8921
  return SQLITE_OK;
8922
}
8923
 
8924
/*
8925
** Virtual table module xEof method.
8926
**
8927
** Return non-zero if the cursor does not currently point to a valid
8928
** record (i.e if the scan has finished), or zero otherwise.
8929
*/
8930
static int expertEof(sqlite3_vtab_cursor *cur){
8931
  ExpertCsr *pCsr = (ExpertCsr*)cur;
8932
  return pCsr->pData==0;
8933
}
8934
 
8935
/*
8936
** Virtual table module xNext method.
8937
*/
8938
static int expertNext(sqlite3_vtab_cursor *cur){
8939
  ExpertCsr *pCsr = (ExpertCsr*)cur;
8940
  int rc = SQLITE_OK;
8941
 
8942
  assert( pCsr->pData );
8943
  rc = sqlite3_step(pCsr->pData);
8944
  if( rc!=SQLITE_ROW ){
8945
    rc = sqlite3_finalize(pCsr->pData);
8946
    pCsr->pData = 0;
8947
  }else{
8948
    rc = SQLITE_OK;
8949
  }
8950
 
8951
  return rc;
8952
}
8953
 
8954
/*
8955
** Virtual table module xRowid method.
8956
*/
8957
static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
8958
  (void)cur;
8959
  *pRowid = 0;
8960
  return SQLITE_OK;
8961
}
8962
 
8963
/*
8964
** Virtual table module xColumn method.
8965
*/
8966
static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
8967
  ExpertCsr *pCsr = (ExpertCsr*)cur;
8968
  sqlite3_value *pVal;
8969
  pVal = sqlite3_column_value(pCsr->pData, i);
8970
  if( pVal ){
8971
    sqlite3_result_value(ctx, pVal);
8972
  }
8973
  return SQLITE_OK;
8974
}
8975
 
8976
/*
8977
** Virtual table module xFilter method.
8978
*/
8979
static int expertFilter(
8980
  sqlite3_vtab_cursor *cur,
8981
  int idxNum, const char *idxStr,
8982
  int argc, sqlite3_value **argv
8983
){
8984
  ExpertCsr *pCsr = (ExpertCsr*)cur;
8985
  ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
8986
  sqlite3expert *pExpert = pVtab->pExpert;
8987
  int rc;
8988
 
8989
  (void)idxNum;
8990
  (void)idxStr;
8991
  (void)argc;
8992
  (void)argv;
8993
  rc = sqlite3_finalize(pCsr->pData);
8994
  pCsr->pData = 0;
8995
  if( rc==SQLITE_OK ){
8996
    rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
8997
        "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
8998
    );
8999
  }
9000
 
9001
  if( rc==SQLITE_OK ){
9002
    rc = expertNext(cur);
9003
  }
9004
  return rc;
9005
}
9006
 
9007
static int idxRegisterVtab(sqlite3expert *p){
9008
  static sqlite3_module expertModule = {
9009
    2,                            /* iVersion */
9010
    expertConnect,                /* xCreate - create a table */
9011
    expertConnect,                /* xConnect - connect to an existing table */
9012
    expertBestIndex,              /* xBestIndex - Determine search strategy */
9013
    expertDisconnect,             /* xDisconnect - Disconnect from a table */
9014
    expertDisconnect,             /* xDestroy - Drop a table */
9015
    expertOpen,                   /* xOpen - open a cursor */
9016
    expertClose,                  /* xClose - close a cursor */
9017
    expertFilter,                 /* xFilter - configure scan constraints */
9018
    expertNext,                   /* xNext - advance a cursor */
9019
    expertEof,                    /* xEof */
9020
    expertColumn,                 /* xColumn - read data */
9021
    expertRowid,                  /* xRowid - read data */
9022
    expertUpdate,                 /* xUpdate - write data */
9023
    0,                            /* xBegin - begin transaction */
9024
    0,                            /* xSync - sync transaction */
9025
    0,                            /* xCommit - commit transaction */
9026
    0,                            /* xRollback - rollback transaction */
9027
    0,                            /* xFindFunction - function overloading */
9028
    0,                            /* xRename - rename the table */
9029
    0,                            /* xSavepoint */
9030
    0,                            /* xRelease */
9031
    0,                            /* xRollbackTo */
9032
    0,                            /* xShadowName */
9033
  };
9034
 
9035
  return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
9036
}
9037
/*
9038
** End of virtual table implementation.
9039
*************************************************************************/
9040
/*
9041
** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
9042
** is called, set it to the return value of sqlite3_finalize() before
9043
** returning. Otherwise, discard the sqlite3_finalize() return value.
9044
*/
9045
static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
9046
  int rc = sqlite3_finalize(pStmt);
9047
  if( *pRc==SQLITE_OK ) *pRc = rc;
9048
}
9049
 
9050
/*
9051
** Attempt to allocate an IdxTable structure corresponding to table zTab
9052
** in the main database of connection db. If successful, set (*ppOut) to
9053
** point to the new object and return SQLITE_OK. Otherwise, return an
9054
** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
9055
** set to point to an error string.
9056
**
9057
** It is the responsibility of the caller to eventually free either the
9058
** IdxTable object or error message using sqlite3_free().
9059
*/
9060
static int idxGetTableInfo(
9061
  sqlite3 *db,                    /* Database connection to read details from */
9062
  const char *zTab,               /* Table name */
9063
  IdxTable **ppOut,               /* OUT: New object (if successful) */
9064
  char **pzErrmsg                 /* OUT: Error message (if not) */
9065
){
9066
  sqlite3_stmt *p1 = 0;
9067
  int nCol = 0;
9068
  int nTab = STRLEN(zTab);
9069
  int nByte = sizeof(IdxTable) + nTab + 1;
9070
  IdxTable *pNew = 0;
9071
  int rc, rc2;
9072
  char *pCsr = 0;
9073
  int nPk = 0;
9074
 
9075
  rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
9076
  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9077
    const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9078
    nByte += 1 + STRLEN(zCol);
9079
    rc = sqlite3_table_column_metadata(
9080
        db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
9081
    );
9082
    nByte += 1 + STRLEN(zCol);
9083
    nCol++;
9084
    nPk += (sqlite3_column_int(p1, 5)>0);
9085
  }
9086
  rc2 = sqlite3_reset(p1);
9087
  if( rc==SQLITE_OK ) rc = rc2;
9088
 
9089
  nByte += sizeof(IdxColumn) * nCol;
9090
  if( rc==SQLITE_OK ){
9091
    pNew = idxMalloc(&rc, nByte);
9092
  }
9093
  if( rc==SQLITE_OK ){
9094
    pNew->aCol = (IdxColumn*)&pNew[1];
9095
    pNew->nCol = nCol;
9096
    pCsr = (char*)&pNew->aCol[nCol];
9097
  }
9098
 
9099
  nCol = 0;
9100
  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9101
    const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9102
    int nCopy = STRLEN(zCol) + 1;
9103
    pNew->aCol[nCol].zName = pCsr;
9104
    pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
9105
    memcpy(pCsr, zCol, nCopy);
9106
    pCsr += nCopy;
9107
 
9108
    rc = sqlite3_table_column_metadata(
9109
        db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
9110
    );
9111
    if( rc==SQLITE_OK ){
9112
      nCopy = STRLEN(zCol) + 1;
9113
      pNew->aCol[nCol].zColl = pCsr;
9114
      memcpy(pCsr, zCol, nCopy);
9115
      pCsr += nCopy;
9116
    }
9117
 
9118
    nCol++;
9119
  }
9120
  idxFinalize(&rc, p1);
9121
 
9122
  if( rc!=SQLITE_OK ){
9123
    sqlite3_free(pNew);
9124
    pNew = 0;
9125
  }else{
9126
    pNew->zName = pCsr;
9127
    memcpy(pNew->zName, zTab, nTab+1);
9128
  }
9129
 
9130
  *ppOut = pNew;
9131
  return rc;
9132
}
9133
 
9134
/*
9135
** This function is a no-op if *pRc is set to anything other than
9136
** SQLITE_OK when it is called.
9137
**
9138
** If *pRc is initially set to SQLITE_OK, then the text specified by
9139
** the printf() style arguments is appended to zIn and the result returned
9140
** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
9141
** zIn before returning.
9142
*/
9143
static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
9144
  va_list ap;
9145
  char *zAppend = 0;
9146
  char *zRet = 0;
9147
  int nIn = zIn ? STRLEN(zIn) : 0;
9148
  int nAppend = 0;
9149
  va_start(ap, zFmt);
9150
  if( *pRc==SQLITE_OK ){
9151
    zAppend = sqlite3_vmprintf(zFmt, ap);
9152
    if( zAppend ){
9153
      nAppend = STRLEN(zAppend);
9154
      zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
9155
    }
9156
    if( zAppend && zRet ){
9157
      if( nIn ) memcpy(zRet, zIn, nIn);
9158
      memcpy(&zRet[nIn], zAppend, nAppend+1);
9159
    }else{
9160
      sqlite3_free(zRet);
9161
      zRet = 0;
9162
      *pRc = SQLITE_NOMEM;
9163
    }
9164
    sqlite3_free(zAppend);
9165
    sqlite3_free(zIn);
9166
  }
9167
  va_end(ap);
9168
  return zRet;
9169
}
9170
 
9171
/*
9172
** Return true if zId must be quoted in order to use it as an SQL
9173
** identifier, or false otherwise.
9174
*/
9175
static int idxIdentifierRequiresQuotes(const char *zId){
9176
  int i;
9177
  for(i=0; zId[i]; i++){
9178
    if( !(zId[i]=='_')
9179
     && !(zId[i]>='0' && zId[i]<='9')
9180
     && !(zId[i]>='a' && zId[i]<='z')
9181
     && !(zId[i]>='A' && zId[i]<='Z')
9182
    ){
9183
      return 1;
9184
    }
9185
  }
9186
  return 0;
9187
}
9188
 
9189
/*
9190
** This function appends an index column definition suitable for constraint
9191
** pCons to the string passed as zIn and returns the result.
9192
*/
9193
static char *idxAppendColDefn(
9194
  int *pRc,                       /* IN/OUT: Error code */
9195
  char *zIn,                      /* Column defn accumulated so far */
9196
  IdxTable *pTab,                 /* Table index will be created on */
9197
  IdxConstraint *pCons
9198
){
9199
  char *zRet = zIn;
9200
  IdxColumn *p = &pTab->aCol[pCons->iCol];
9201
  if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
9202
 
9203
  if( idxIdentifierRequiresQuotes(p->zName) ){
9204
    zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
9205
  }else{
9206
    zRet = idxAppendText(pRc, zRet, "%s", p->zName);
9207
  }
9208
 
9209
  if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
9210
    if( idxIdentifierRequiresQuotes(pCons->zColl) ){
9211
      zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
9212
    }else{
9213
      zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
9214
    }
9215
  }
9216
 
9217
  if( pCons->bDesc ){
9218
    zRet = idxAppendText(pRc, zRet, " DESC");
9219
  }
9220
  return zRet;
9221
}
9222
 
9223
/*
9224
** Search database dbm for an index compatible with the one idxCreateFromCons()
9225
** would create from arguments pScan, pEq and pTail. If no error occurs and
9226
** such an index is found, return non-zero. Or, if no such index is found,
9227
** return zero.
9228
**
9229
** If an error occurs, set *pRc to an SQLite error code and return zero.
9230
*/
9231
static int idxFindCompatible(
9232
  int *pRc,                       /* OUT: Error code */
9233
  sqlite3* dbm,                   /* Database to search */
9234
  IdxScan *pScan,                 /* Scan for table to search for index on */
9235
  IdxConstraint *pEq,             /* List of == constraints */
9236
  IdxConstraint *pTail            /* List of range constraints */
9237
){
9238
  const char *zTbl = pScan->pTab->zName;
9239
  sqlite3_stmt *pIdxList = 0;
9240
  IdxConstraint *pIter;
9241
  int nEq = 0;                    /* Number of elements in pEq */
9242
  int rc;
9243
 
9244
  /* Count the elements in list pEq */
9245
  for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
9246
 
9247
  rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
9248
  while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
9249
    int bMatch = 1;
9250
    IdxConstraint *pT = pTail;
9251
    sqlite3_stmt *pInfo = 0;
9252
    const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
9253
 
9254
    /* Zero the IdxConstraint.bFlag values in the pEq list */
9255
    for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
9256
 
9257
    rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
9258
    while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
9259
      int iIdx = sqlite3_column_int(pInfo, 0);
9260
      int iCol = sqlite3_column_int(pInfo, 1);
9261
      const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
9262
 
9263
      if( iIdx
9264
        for(pIter=pEq; pIter; pIter=pIter->pLink){
9265
          if( pIter->bFlag ) continue;
9266
          if( pIter->iCol!=iCol ) continue;
9267
          if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
9268
          pIter->bFlag = 1;
9269
          break;
9270
        }
9271
        if( pIter==0 ){
9272
          bMatch = 0;
9273
          break;
9274
        }
9275
      }else{
9276
        if( pT ){
9277
          if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
9278
            bMatch = 0;
9279
            break;
9280
          }
9281
          pT = pT->pLink;
9282
        }
9283
      }
9284
    }
9285
    idxFinalize(&rc, pInfo);
9286
 
9287
    if( rc==SQLITE_OK && bMatch ){
9288
      sqlite3_finalize(pIdxList);
9289
      return 1;
9290
    }
9291
  }
9292
  idxFinalize(&rc, pIdxList);
9293
 
9294
  *pRc = rc;
9295
  return 0;
9296
}
9297
 
9298
static int idxCreateFromCons(
9299
  sqlite3expert *p,
9300
  IdxScan *pScan,
9301
  IdxConstraint *pEq,
9302
  IdxConstraint *pTail
9303
){
9304
  sqlite3 *dbm = p->dbm;
9305
  int rc = SQLITE_OK;
9306
  if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
9307
    IdxTable *pTab = pScan->pTab;
9308
    char *zCols = 0;
9309
    char *zIdx = 0;
9310
    IdxConstraint *pCons;
9311
    unsigned int h = 0;
9312
    const char *zFmt;
9313
 
9314
    for(pCons=pEq; pCons; pCons=pCons->pLink){
9315
      zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
9316
    }
9317
    for(pCons=pTail; pCons; pCons=pCons->pLink){
9318
      zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
9319
    }
9320
 
9321
    if( rc==SQLITE_OK ){
9322
      /* Hash the list of columns to come up with a name for the index */
9323
      const char *zTable = pScan->pTab->zName;
9324
      char *zName;                /* Index name */
9325
      int i;
9326
      for(i=0; zCols[i]; i++){
9327
        h += ((h<<3) + zCols[i]);
9328
      }
9329
      zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
9330
      if( zName==0 ){
9331
        rc = SQLITE_NOMEM;
9332
      }else{
9333
        if( idxIdentifierRequiresQuotes(zTable) ){
9334
          zFmt = "CREATE INDEX '%q' ON %Q(%s)";
9335
        }else{
9336
          zFmt = "CREATE INDEX %s ON %s(%s)";
9337
        }
9338
        zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
9339
        if( !zIdx ){
9340
          rc = SQLITE_NOMEM;
9341
        }else{
9342
          rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
9343
          idxHashAdd(&rc, &p->hIdx, zName, zIdx);
9344
        }
9345
        sqlite3_free(zName);
9346
        sqlite3_free(zIdx);
9347
      }
9348
    }
9349
 
9350
    sqlite3_free(zCols);
9351
  }
9352
  return rc;
9353
}
9354
 
9355
/*
9356
** Return true if list pList (linked by IdxConstraint.pLink) contains
9357
** a constraint compatible with *p. Otherwise return false.
9358
*/
9359
static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
9360
  IdxConstraint *pCmp;
9361
  for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
9362
    if( p->iCol==pCmp->iCol ) return 1;
9363
  }
9364
  return 0;
9365
}
9366
 
9367
static int idxCreateFromWhere(
9368
  sqlite3expert *p,
9369
  IdxScan *pScan,                 /* Create indexes for this scan */
9370
  IdxConstraint *pTail            /* range/ORDER BY constraints for inclusion */
9371
){
9372
  IdxConstraint *p1 = 0;
9373
  IdxConstraint *pCon;
9374
  int rc;
9375
 
9376
  /* Gather up all the == constraints. */
9377
  for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
9378
    if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
9379
      pCon->pLink = p1;
9380
      p1 = pCon;
9381
    }
9382
  }
9383
 
9384
  /* Create an index using the == constraints collected above. And the
9385
  ** range constraint/ORDER BY terms passed in by the caller, if any. */
9386
  rc = idxCreateFromCons(p, pScan, p1, pTail);
9387
 
9388
  /* If no range/ORDER BY passed by the caller, create a version of the
9389
  ** index for each range constraint.  */
9390
  if( pTail==0 ){
9391
    for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
9392
      assert( pCon->pLink==0 );
9393
      if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
9394
        rc = idxCreateFromCons(p, pScan, p1, pCon);
9395
      }
9396
    }
9397
  }
9398
 
9399
  return rc;
9400
}
9401
 
9402
/*
9403
** Create candidate indexes in database [dbm] based on the data in
9404
** linked-list pScan.
9405
*/
9406
static int idxCreateCandidates(sqlite3expert *p){
9407
  int rc = SQLITE_OK;
9408
  IdxScan *pIter;
9409
 
9410
  for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
9411
    rc = idxCreateFromWhere(p, pIter, 0);
9412
    if( rc==SQLITE_OK && pIter->pOrder ){
9413
      rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
9414
    }
9415
  }
9416
 
9417
  return rc;
9418
}
9419
 
9420
/*
9421
** Free all elements of the linked list starting at pConstraint.
9422
*/
9423
static void idxConstraintFree(IdxConstraint *pConstraint){
9424
  IdxConstraint *pNext;
9425
  IdxConstraint *p;
9426
 
9427
  for(p=pConstraint; p; p=pNext){
9428
    pNext = p->pNext;
9429
    sqlite3_free(p);
9430
  }
9431
}
9432
 
9433
/*
9434
** Free all elements of the linked list starting from pScan up until pLast
9435
** (pLast is not freed).
9436
*/
9437
static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
9438
  IdxScan *p;
9439
  IdxScan *pNext;
9440
  for(p=pScan; p!=pLast; p=pNext){
9441
    pNext = p->pNextScan;
9442
    idxConstraintFree(p->pOrder);
9443
    idxConstraintFree(p->pEq);
9444
    idxConstraintFree(p->pRange);
9445
    sqlite3_free(p);
9446
  }
9447
}
9448
 
9449
/*
9450
** Free all elements of the linked list starting from pStatement up
9451
** until pLast (pLast is not freed).
9452
*/
9453
static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
9454
  IdxStatement *p;
9455
  IdxStatement *pNext;
9456
  for(p=pStatement; p!=pLast; p=pNext){
9457
    pNext = p->pNext;
9458
    sqlite3_free(p->zEQP);
9459
    sqlite3_free(p->zIdx);
9460
    sqlite3_free(p);
9461
  }
9462
}
9463
 
9464
/*
9465
** Free the linked list of IdxTable objects starting at pTab.
9466
*/
9467
static void idxTableFree(IdxTable *pTab){
9468
  IdxTable *pIter;
9469
  IdxTable *pNext;
9470
  for(pIter=pTab; pIter; pIter=pNext){
9471
    pNext = pIter->pNext;
9472
    sqlite3_free(pIter);
9473
  }
9474
}
9475
 
9476
/*
9477
** Free the linked list of IdxWrite objects starting at pTab.
9478
*/
9479
static void idxWriteFree(IdxWrite *pTab){
9480
  IdxWrite *pIter;
9481
  IdxWrite *pNext;
9482
  for(pIter=pTab; pIter; pIter=pNext){
9483
    pNext = pIter->pNext;
9484
    sqlite3_free(pIter);
9485
  }
9486
}
9487
 
9488
 
9489
 
9490
/*
9491
** This function is called after candidate indexes have been created. It
9492
** runs all the queries to see which indexes they prefer, and populates
9493
** IdxStatement.zIdx and IdxStatement.zEQP with the results.
9494
*/
9495
int idxFindIndexes(
9496
  sqlite3expert *p,
9497
  char **pzErr                         /* OUT: Error message (sqlite3_malloc) */
9498
){
9499
  IdxStatement *pStmt;
9500
  sqlite3 *dbm = p->dbm;
9501
  int rc = SQLITE_OK;
9502
 
9503
  IdxHash hIdx;
9504
  idxHashInit(&hIdx);
9505
 
9506
  for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
9507
    IdxHashEntry *pEntry;
9508
    sqlite3_stmt *pExplain = 0;
9509
    idxHashClear(&hIdx);
9510
    rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
9511
        "EXPLAIN QUERY PLAN %s", pStmt->zSql
9512
    );
9513
    while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
9514
      /* int iId = sqlite3_column_int(pExplain, 0); */
9515
      /* int iParent = sqlite3_column_int(pExplain, 1); */
9516
      /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
9517
      const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
9518
      int nDetail;
9519
      int i;
9520
 
9521
      if( !zDetail ) continue;
9522
      nDetail = STRLEN(zDetail);
9523
 
9524
      for(i=0; i
9525
        const char *zIdx = 0;
9526
        if( i+13
9527
          zIdx = &zDetail[i+13];
9528
        }else if( i+22
9529
            && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0
9530
        ){
9531
          zIdx = &zDetail[i+22];
9532
        }
9533
        if( zIdx ){
9534
          const char *zSql;
9535
          int nIdx = 0;
9536
          while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
9537
            nIdx++;
9538
          }
9539
          zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
9540
          if( zSql ){
9541
            idxHashAdd(&rc, &hIdx, zSql, 0);
9542
            if( rc ) goto find_indexes_out;
9543
          }
9544
          break;
9545
        }
9546
      }
9547
 
9548
      if( zDetail[0]!='-' ){
9549
        pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
9550
      }
9551
    }
9552
 
9553
    for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
9554
      pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
9555
    }
9556
 
9557
    idxFinalize(&rc, pExplain);
9558
  }
9559
 
9560
 find_indexes_out:
9561
  idxHashClear(&hIdx);
9562
  return rc;
9563
}
9564
 
9565
static int idxAuthCallback(
9566
  void *pCtx,
9567
  int eOp,
9568
  const char *z3,
9569
  const char *z4,
9570
  const char *zDb,
9571
  const char *zTrigger
9572
){
9573
  int rc = SQLITE_OK;
9574
  (void)z4;
9575
  (void)zTrigger;
9576
  if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
9577
    if( sqlite3_stricmp(zDb, "main")==0 ){
9578
      sqlite3expert *p = (sqlite3expert*)pCtx;
9579
      IdxTable *pTab;
9580
      for(pTab=p->pTable; pTab; pTab=pTab->pNext){
9581
        if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
9582
      }
9583
      if( pTab ){
9584
        IdxWrite *pWrite;
9585
        for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
9586
          if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
9587
        }
9588
        if( pWrite==0 ){
9589
          pWrite = idxMalloc(&rc, sizeof(IdxWrite));
9590
          if( rc==SQLITE_OK ){
9591
            pWrite->pTab = pTab;
9592
            pWrite->eOp = eOp;
9593
            pWrite->pNext = p->pWrite;
9594
            p->pWrite = pWrite;
9595
          }
9596
        }
9597
      }
9598
    }
9599
  }
9600
  return rc;
9601
}
9602
 
9603
static int idxProcessOneTrigger(
9604
  sqlite3expert *p,
9605
  IdxWrite *pWrite,
9606
  char **pzErr
9607
){
9608
  static const char *zInt = UNIQUE_TABLE_NAME;
9609
  static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
9610
  IdxTable *pTab = pWrite->pTab;
9611
  const char *zTab = pTab->zName;
9612
  const char *zSql =
9613
    "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
9614
    "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
9615
    "ORDER BY type;";
9616
  sqlite3_stmt *pSelect = 0;
9617
  int rc = SQLITE_OK;
9618
  char *zWrite = 0;
9619
 
9620
  /* Create the table and its triggers in the temp schema */
9621
  rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
9622
  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
9623
    const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
9624
    rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
9625
  }
9626
  idxFinalize(&rc, pSelect);
9627
 
9628
  /* Rename the table in the temp schema to zInt */
9629
  if( rc==SQLITE_OK ){
9630
    char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
9631
    if( z==0 ){
9632
      rc = SQLITE_NOMEM;
9633
    }else{
9634
      rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
9635
      sqlite3_free(z);
9636
    }
9637
  }
9638
 
9639
  switch( pWrite->eOp ){
9640
    case SQLITE_INSERT: {
9641
      int i;
9642
      zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
9643
      for(i=0; inCol; i++){
9644
        zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
9645
      }
9646
      zWrite = idxAppendText(&rc, zWrite, ")");
9647
      break;
9648
    }
9649
    case SQLITE_UPDATE: {
9650
      int i;
9651
      zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
9652
      for(i=0; inCol; i++){
9653
        zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
9654
            pTab->aCol[i].zName
9655
        );
9656
      }
9657
      break;
9658
    }
9659
    default: {
9660
      assert( pWrite->eOp==SQLITE_DELETE );
9661
      if( rc==SQLITE_OK ){
9662
        zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
9663
        if( zWrite==0 ) rc = SQLITE_NOMEM;
9664
      }
9665
    }
9666
  }
9667
 
9668
  if( rc==SQLITE_OK ){
9669
    sqlite3_stmt *pX = 0;
9670
    rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
9671
    idxFinalize(&rc, pX);
9672
    if( rc!=SQLITE_OK ){
9673
      idxDatabaseError(p->dbv, pzErr);
9674
    }
9675
  }
9676
  sqlite3_free(zWrite);
9677
 
9678
  if( rc==SQLITE_OK ){
9679
    rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
9680
  }
9681
 
9682
  return rc;
9683
}
9684
 
9685
static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
9686
  int rc = SQLITE_OK;
9687
  IdxWrite *pEnd = 0;
9688
  IdxWrite *pFirst = p->pWrite;
9689
 
9690
  while( rc==SQLITE_OK && pFirst!=pEnd ){
9691
    IdxWrite *pIter;
9692
    for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
9693
      rc = idxProcessOneTrigger(p, pIter, pzErr);
9694
    }
9695
    pEnd = pFirst;
9696
    pFirst = p->pWrite;
9697
  }
9698
 
9699
  return rc;
9700
}
9701
 
9702
 
9703
static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
9704
  int rc = idxRegisterVtab(p);
9705
  sqlite3_stmt *pSchema = 0;
9706
 
9707
  /* For each table in the main db schema:
9708
  **
9709
  **   1) Add an entry to the p->pTable list, and
9710
  **   2) Create the equivalent virtual table in dbv.
9711
  */
9712
  rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
9713
      "SELECT type, name, sql, 1 FROM sqlite_schema "
9714
      "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
9715
      " UNION ALL "
9716
      "SELECT type, name, sql, 2 FROM sqlite_schema "
9717
      "WHERE type = 'trigger'"
9718
      "  AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
9719
      "ORDER BY 4, 1"
9720
  );
9721
  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
9722
    const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
9723
    const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
9724
    const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
9725
 
9726
    if( zType[0]=='v' || zType[1]=='r' ){
9727
      rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
9728
    }else{
9729
      IdxTable *pTab;
9730
      rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
9731
      if( rc==SQLITE_OK ){
9732
        int i;
9733
        char *zInner = 0;
9734
        char *zOuter = 0;
9735
        pTab->pNext = p->pTable;
9736
        p->pTable = pTab;
9737
 
9738
        /* The statement the vtab will pass to sqlite3_declare_vtab() */
9739
        zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
9740
        for(i=0; inCol; i++){
9741
          zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
9742
              (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
9743
          );
9744
        }
9745
        zInner = idxAppendText(&rc, zInner, ")");
9746
 
9747
        /* The CVT statement to create the vtab */
9748
        zOuter = idxAppendText(&rc, 0,
9749
            "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
9750
        );
9751
        if( rc==SQLITE_OK ){
9752
          rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
9753
        }
9754
        sqlite3_free(zInner);
9755
        sqlite3_free(zOuter);
9756
      }
9757
    }
9758
  }
9759
  idxFinalize(&rc, pSchema);
9760
  return rc;
9761
}
9762
 
9763
struct IdxSampleCtx {
9764
  int iTarget;
9765
  double target;                  /* Target nRet/nRow value */
9766
  double nRow;                    /* Number of rows seen */
9767
  double nRet;                    /* Number of rows returned */
9768
};
9769
 
9770
static void idxSampleFunc(
9771
  sqlite3_context *pCtx,
9772
  int argc,
9773
  sqlite3_value **argv
9774
){
9775
  struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
9776
  int bRet;
9777
 
9778
  (void)argv;
9779
  assert( argc==0 );
9780
  if( p->nRow==0.0 ){
9781
    bRet = 1;
9782
  }else{
9783
    bRet = (p->nRet / p->nRow) <= p->target;
9784
    if( bRet==0 ){
9785
      unsigned short rnd;
9786
      sqlite3_randomness(2, (void*)&rnd);
9787
      bRet = ((int)rnd % 100) <= p->iTarget;
9788
    }
9789
  }
9790
 
9791
  sqlite3_result_int(pCtx, bRet);
9792
  p->nRow += 1.0;
9793
  p->nRet += (double)bRet;
9794
}
9795
 
9796
struct IdxRemCtx {
9797
  int nSlot;
9798
  struct IdxRemSlot {
9799
    int eType;                    /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
9800
    i64 iVal;                     /* SQLITE_INTEGER value */
9801
    double rVal;                  /* SQLITE_FLOAT value */
9802
    int nByte;                    /* Bytes of space allocated at z */
9803
    int n;                        /* Size of buffer z */
9804
    char *z;                      /* SQLITE_TEXT/BLOB value */
9805
  } aSlot[1];
9806
};
9807
 
9808
/*
9809
** Implementation of scalar function rem().
9810
*/
9811
static void idxRemFunc(
9812
  sqlite3_context *pCtx,
9813
  int argc,
9814
  sqlite3_value **argv
9815
){
9816
  struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
9817
  struct IdxRemSlot *pSlot;
9818
  int iSlot;
9819
  assert( argc==2 );
9820
 
9821
  iSlot = sqlite3_value_int(argv[0]);
9822
  assert( iSlot<=p->nSlot );
9823
  pSlot = &p->aSlot[iSlot];
9824
 
9825
  switch( pSlot->eType ){
9826
    case SQLITE_NULL:
9827
      /* no-op */
9828
      break;
9829
 
9830
    case SQLITE_INTEGER:
9831
      sqlite3_result_int64(pCtx, pSlot->iVal);
9832
      break;
9833
 
9834
    case SQLITE_FLOAT:
9835
      sqlite3_result_double(pCtx, pSlot->rVal);
9836
      break;
9837
 
9838
    case SQLITE_BLOB:
9839
      sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
9840
      break;
9841
 
9842
    case SQLITE_TEXT:
9843
      sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
9844
      break;
9845
  }
9846
 
9847
  pSlot->eType = sqlite3_value_type(argv[1]);
9848
  switch( pSlot->eType ){
9849
    case SQLITE_NULL:
9850
      /* no-op */
9851
      break;
9852
 
9853
    case SQLITE_INTEGER:
9854
      pSlot->iVal = sqlite3_value_int64(argv[1]);
9855
      break;
9856
 
9857
    case SQLITE_FLOAT:
9858
      pSlot->rVal = sqlite3_value_double(argv[1]);
9859
      break;
9860
 
9861
    case SQLITE_BLOB:
9862
    case SQLITE_TEXT: {
9863
      int nByte = sqlite3_value_bytes(argv[1]);
9864
      if( nByte>pSlot->nByte ){
9865
        char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
9866
        if( zNew==0 ){
9867
          sqlite3_result_error_nomem(pCtx);
9868
          return;
9869
        }
9870
        pSlot->nByte = nByte*2;
9871
        pSlot->z = zNew;
9872
      }
9873
      pSlot->n = nByte;
9874
      if( pSlot->eType==SQLITE_BLOB ){
9875
        memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
9876
      }else{
9877
        memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
9878
      }
9879
      break;
9880
    }
9881
  }
9882
}
9883
 
9884
static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
9885
  int rc = SQLITE_OK;
9886
  const char *zMax =
9887
    "SELECT max(i.seqno) FROM "
9888
    "  sqlite_schema AS s, "
9889
    "  pragma_index_list(s.name) AS l, "
9890
    "  pragma_index_info(l.name) AS i "
9891
    "WHERE s.type = 'table'";
9892
  sqlite3_stmt *pMax = 0;
9893
 
9894
  *pnMax = 0;
9895
  rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
9896
  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
9897
    *pnMax = sqlite3_column_int(pMax, 0) + 1;
9898
  }
9899
  idxFinalize(&rc, pMax);
9900
 
9901
  return rc;
9902
}
9903
 
9904
static int idxPopulateOneStat1(
9905
  sqlite3expert *p,
9906
  sqlite3_stmt *pIndexXInfo,
9907
  sqlite3_stmt *pWriteStat,
9908
  const char *zTab,
9909
  const char *zIdx,
9910
  char **pzErr
9911
){
9912
  char *zCols = 0;
9913
  char *zOrder = 0;
9914
  char *zQuery = 0;
9915
  int nCol = 0;
9916
  int i;
9917
  sqlite3_stmt *pQuery = 0;
9918
  int *aStat = 0;
9919
  int rc = SQLITE_OK;
9920
 
9921
  assert( p->iSample>0 );
9922
 
9923
  /* Formulate the query text */
9924
  sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
9925
  while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
9926
    const char *zComma = zCols==0 ? "" : ", ";
9927
    const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
9928
    const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
9929
    zCols = idxAppendText(&rc, zCols,
9930
        "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
9931
    );
9932
    zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
9933
  }
9934
  sqlite3_reset(pIndexXInfo);
9935
  if( rc==SQLITE_OK ){
9936
    if( p->iSample==100 ){
9937
      zQuery = sqlite3_mprintf(
9938
          "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
9939
      );
9940
    }else{
9941
      zQuery = sqlite3_mprintf(
9942
          "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
9943
      );
9944
    }
9945
  }
9946
  sqlite3_free(zCols);
9947
  sqlite3_free(zOrder);
9948
 
9949
  /* Formulate the query text */
9950
  if( rc==SQLITE_OK ){
9951
    sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
9952
    rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
9953
  }
9954
  sqlite3_free(zQuery);
9955
 
9956
  if( rc==SQLITE_OK ){
9957
    aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
9958
  }
9959
  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
9960
    IdxHashEntry *pEntry;
9961
    char *zStat = 0;
9962
    for(i=0; i<=nCol; i++) aStat[i] = 1;
9963
    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
9964
      aStat[0]++;
9965
      for(i=0; i
9966
        if( sqlite3_column_int(pQuery, i)==0 ) break;
9967
      }
9968
      for(/*no-op*/; i
9969
        aStat[i+1]++;
9970
      }
9971
    }
9972
 
9973
    if( rc==SQLITE_OK ){
9974
      int s0 = aStat[0];
9975
      zStat = sqlite3_mprintf("%d", s0);
9976
      if( zStat==0 ) rc = SQLITE_NOMEM;
9977
      for(i=1; rc==SQLITE_OK && i<=nCol; i++){
9978
        zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
9979
      }
9980
    }
9981
 
9982
    if( rc==SQLITE_OK ){
9983
      sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
9984
      sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
9985
      sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
9986
      sqlite3_step(pWriteStat);
9987
      rc = sqlite3_reset(pWriteStat);
9988
    }
9989
 
9990
    pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
9991
    if( pEntry ){
9992
      assert( pEntry->zVal2==0 );
9993
      pEntry->zVal2 = zStat;
9994
    }else{
9995
      sqlite3_free(zStat);
9996
    }
9997
  }
9998
  sqlite3_free(aStat);
9999
  idxFinalize(&rc, pQuery);
10000
 
10001
  return rc;
10002
}
10003
 
10004
static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
10005
  int rc;
10006
  char *zSql;
10007
 
10008
  rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
10009
  if( rc!=SQLITE_OK ) return rc;
10010
 
10011
  zSql = sqlite3_mprintf(
10012
      "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
10013
  );
10014
  if( zSql==0 ) return SQLITE_NOMEM;
10015
  rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
10016
  sqlite3_free(zSql);
10017
 
10018
  return rc;
10019
}
10020
 
10021
/*
10022
** This function is called as part of sqlite3_expert_analyze(). Candidate
10023
** indexes have already been created in database sqlite3expert.dbm, this
10024
** function populates sqlite_stat1 table in the same database.
10025
**
10026
** The stat1 data is generated by querying the
10027
*/
10028
static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
10029
  int rc = SQLITE_OK;
10030
  int nMax =0;
10031
  struct IdxRemCtx *pCtx = 0;
10032
  struct IdxSampleCtx samplectx;
10033
  int i;
10034
  i64 iPrev = -100000;
10035
  sqlite3_stmt *pAllIndex = 0;
10036
  sqlite3_stmt *pIndexXInfo = 0;
10037
  sqlite3_stmt *pWrite = 0;
10038
 
10039
  const char *zAllIndex =
10040
    "SELECT s.rowid, s.name, l.name FROM "
10041
    "  sqlite_schema AS s, "
10042
    "  pragma_index_list(s.name) AS l "
10043
    "WHERE s.type = 'table'";
10044
  const char *zIndexXInfo =
10045
    "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
10046
  const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
10047
 
10048
  /* If iSample==0, no sqlite_stat1 data is required. */
10049
  if( p->iSample==0 ) return SQLITE_OK;
10050
 
10051
  rc = idxLargestIndex(p->dbm, &nMax, pzErr);
10052
  if( nMax<=0 || rc!=SQLITE_OK ) return rc;
10053
 
10054
  rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
10055
 
10056
  if( rc==SQLITE_OK ){
10057
    int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
10058
    pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
10059
  }
10060
 
10061
  if( rc==SQLITE_OK ){
10062
    sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10063
    rc = sqlite3_create_function(
10064
        dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
10065
    );
10066
  }
10067
  if( rc==SQLITE_OK ){
10068
    rc = sqlite3_create_function(
10069
        p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
10070
    );
10071
  }
10072
 
10073
  if( rc==SQLITE_OK ){
10074
    pCtx->nSlot = nMax+1;
10075
    rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
10076
  }
10077
  if( rc==SQLITE_OK ){
10078
    rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
10079
  }
10080
  if( rc==SQLITE_OK ){
10081
    rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
10082
  }
10083
 
10084
  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
10085
    i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
10086
    const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
10087
    const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
10088
    if( p->iSample<100 && iPrev!=iRowid ){
10089
      samplectx.target = (double)p->iSample / 100.0;
10090
      samplectx.iTarget = p->iSample;
10091
      samplectx.nRow = 0.0;
10092
      samplectx.nRet = 0.0;
10093
      rc = idxBuildSampleTable(p, zTab);
10094
      if( rc!=SQLITE_OK ) break;
10095
    }
10096
    rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
10097
    iPrev = iRowid;
10098
  }
10099
  if( rc==SQLITE_OK && p->iSample<100 ){
10100
    rc = sqlite3_exec(p->dbv,
10101
        "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
10102
    );
10103
  }
10104
 
10105
  idxFinalize(&rc, pAllIndex);
10106
  idxFinalize(&rc, pIndexXInfo);
10107
  idxFinalize(&rc, pWrite);
10108
 
10109
  if( pCtx ){
10110
    for(i=0; inSlot; i++){
10111
      sqlite3_free(pCtx->aSlot[i].z);
10112
    }
10113
    sqlite3_free(pCtx);
10114
  }
10115
 
10116
  if( rc==SQLITE_OK ){
10117
    rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
10118
  }
10119
 
10120
  sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
10121
  return rc;
10122
}
10123
 
10124
/*
10125
** Allocate a new sqlite3expert object.
10126
*/
10127
sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
10128
  int rc = SQLITE_OK;
10129
  sqlite3expert *pNew;
10130
 
10131
  pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
10132
 
10133
  /* Open two in-memory databases to work with. The "vtab database" (dbv)
10134
  ** will contain a virtual table corresponding to each real table in
10135
  ** the user database schema, and a copy of each view. It is used to
10136
  ** collect information regarding the WHERE, ORDER BY and other clauses
10137
  ** of the user's query.
10138
  */
10139
  if( rc==SQLITE_OK ){
10140
    pNew->db = db;
10141
    pNew->iSample = 100;
10142
    rc = sqlite3_open(":memory:", &pNew->dbv);
10143
  }
10144
  if( rc==SQLITE_OK ){
10145
    rc = sqlite3_open(":memory:", &pNew->dbm);
10146
    if( rc==SQLITE_OK ){
10147
      sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
10148
    }
10149
  }
10150
 
10151
 
10152
  /* Copy the entire schema of database [db] into [dbm]. */
10153
  if( rc==SQLITE_OK ){
10154
    sqlite3_stmt *pSql;
10155
    rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
10156
        "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
10157
        " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
10158
    );
10159
    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
10160
      const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
10161
      rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
10162
    }
10163
    idxFinalize(&rc, pSql);
10164
  }
10165
 
10166
  /* Create the vtab schema */
10167
  if( rc==SQLITE_OK ){
10168
    rc = idxCreateVtabSchema(pNew, pzErrmsg);
10169
  }
10170
 
10171
  /* Register the auth callback with dbv */
10172
  if( rc==SQLITE_OK ){
10173
    sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
10174
  }
10175
 
10176
  /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
10177
  ** return the new sqlite3expert handle.  */
10178
  if( rc!=SQLITE_OK ){
10179
    sqlite3_expert_destroy(pNew);
10180
    pNew = 0;
10181
  }
10182
  return pNew;
10183
}
10184
 
10185
/*
10186
** Configure an sqlite3expert object.
10187
*/
10188
int sqlite3_expert_config(sqlite3expert *p, int op, ...){
10189
  int rc = SQLITE_OK;
10190
  va_list ap;
10191
  va_start(ap, op);
10192
  switch( op ){
10193
    case EXPERT_CONFIG_SAMPLE: {
10194
      int iVal = va_arg(ap, int);
10195
      if( iVal<0 ) iVal = 0;
10196
      if( iVal>100 ) iVal = 100;
10197
      p->iSample = iVal;
10198
      break;
10199
    }
10200
    default:
10201
      rc = SQLITE_NOTFOUND;
10202
      break;
10203
  }
10204
 
10205
  va_end(ap);
10206
  return rc;
10207
}
10208
 
10209
/*
10210
** Add an SQL statement to the analysis.
10211
*/
10212
int sqlite3_expert_sql(
10213
  sqlite3expert *p,               /* From sqlite3_expert_new() */
10214
  const char *zSql,               /* SQL statement to add */
10215
  char **pzErr                    /* OUT: Error message (if any) */
10216
){
10217
  IdxScan *pScanOrig = p->pScan;
10218
  IdxStatement *pStmtOrig = p->pStatement;
10219
  int rc = SQLITE_OK;
10220
  const char *zStmt = zSql;
10221
 
10222
  if( p->bRun ) return SQLITE_MISUSE;
10223
 
10224
  while( rc==SQLITE_OK && zStmt && zStmt[0] ){
10225
    sqlite3_stmt *pStmt = 0;
10226
    rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
10227
    if( rc==SQLITE_OK ){
10228
      if( pStmt ){
10229
        IdxStatement *pNew;
10230
        const char *z = sqlite3_sql(pStmt);
10231
        int n = STRLEN(z);
10232
        pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
10233
        if( rc==SQLITE_OK ){
10234
          pNew->zSql = (char*)&pNew[1];
10235
          memcpy(pNew->zSql, z, n+1);
10236
          pNew->pNext = p->pStatement;
10237
          if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
10238
          p->pStatement = pNew;
10239
        }
10240
        sqlite3_finalize(pStmt);
10241
      }
10242
    }else{
10243
      idxDatabaseError(p->dbv, pzErr);
10244
    }
10245
  }
10246
 
10247
  if( rc!=SQLITE_OK ){
10248
    idxScanFree(p->pScan, pScanOrig);
10249
    idxStatementFree(p->pStatement, pStmtOrig);
10250
    p->pScan = pScanOrig;
10251
    p->pStatement = pStmtOrig;
10252
  }
10253
 
10254
  return rc;
10255
}
10256
 
10257
int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
10258
  int rc;
10259
  IdxHashEntry *pEntry;
10260
 
10261
  /* Do trigger processing to collect any extra IdxScan structures */
10262
  rc = idxProcessTriggers(p, pzErr);
10263
 
10264
  /* Create candidate indexes within the in-memory database file */
10265
  if( rc==SQLITE_OK ){
10266
    rc = idxCreateCandidates(p);
10267
  }
10268
 
10269
  /* Generate the stat1 data */
10270
  if( rc==SQLITE_OK ){
10271
    rc = idxPopulateStat1(p, pzErr);
10272
  }
10273
 
10274
  /* Formulate the EXPERT_REPORT_CANDIDATES text */
10275
  for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
10276
    p->zCandidates = idxAppendText(&rc, p->zCandidates,
10277
        "%s;%s%s\n", pEntry->zVal,
10278
        pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
10279
    );
10280
  }
10281
 
10282
  /* Figure out which of the candidate indexes are preferred by the query
10283
  ** planner and report the results to the user.  */
10284
  if( rc==SQLITE_OK ){
10285
    rc = idxFindIndexes(p, pzErr);
10286
  }
10287
 
10288
  if( rc==SQLITE_OK ){
10289
    p->bRun = 1;
10290
  }
10291
  return rc;
10292
}
10293
 
10294
/*
10295
** Return the total number of statements that have been added to this
10296
** sqlite3expert using sqlite3_expert_sql().
10297
*/
10298
int sqlite3_expert_count(sqlite3expert *p){
10299
  int nRet = 0;
10300
  if( p->pStatement ) nRet = p->pStatement->iId+1;
10301
  return nRet;
10302
}
10303
 
10304
/*
10305
** Return a component of the report.
10306
*/
10307
const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
10308
  const char *zRet = 0;
10309
  IdxStatement *pStmt;
10310
 
10311
  if( p->bRun==0 ) return 0;
10312
  for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
10313
  switch( eReport ){
10314
    case EXPERT_REPORT_SQL:
10315
      if( pStmt ) zRet = pStmt->zSql;
10316
      break;
10317
    case EXPERT_REPORT_INDEXES:
10318
      if( pStmt ) zRet = pStmt->zIdx;
10319
      break;
10320
    case EXPERT_REPORT_PLAN:
10321
      if( pStmt ) zRet = pStmt->zEQP;
10322
      break;
10323
    case EXPERT_REPORT_CANDIDATES:
10324
      zRet = p->zCandidates;
10325
      break;
10326
  }
10327
  return zRet;
10328
}
10329
 
10330
/*
10331
** Free an sqlite3expert object.
10332
*/
10333
void sqlite3_expert_destroy(sqlite3expert *p){
10334
  if( p ){
10335
    sqlite3_close(p->dbm);
10336
    sqlite3_close(p->dbv);
10337
    idxScanFree(p->pScan, 0);
10338
    idxStatementFree(p->pStatement, 0);
10339
    idxTableFree(p->pTable);
10340
    idxWriteFree(p->pWrite);
10341
    idxHashClear(&p->hIdx);
10342
    sqlite3_free(p->zCandidates);
10343
    sqlite3_free(p);
10344
  }
10345
}
10346
 
10347
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10348
 
10349
/************************* End ../ext/expert/sqlite3expert.c ********************/
10350
 
10351
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
10352
/************************* Begin ../ext/misc/dbdata.c ******************/
10353
/*
10354
** 2019-04-17
10355
**
10356
** The author disclaims copyright to this source code.  In place of
10357
** a legal notice, here is a blessing:
10358
**
10359
**    May you do good and not evil.
10360
**    May you find forgiveness for yourself and forgive others.
10361
**    May you share freely, never taking more than you give.
10362
**
10363
******************************************************************************
10364
**
10365
** This file contains an implementation of two eponymous virtual tables,
10366
** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
10367
** "sqlite_dbpage" eponymous virtual table be available.
10368
**
10369
** SQLITE_DBDATA:
10370
**   sqlite_dbdata is used to extract data directly from a database b-tree
10371
**   page and its associated overflow pages, bypassing the b-tree layer.
10372
**   The table schema is equivalent to:
10373
**
10374
**     CREATE TABLE sqlite_dbdata(
10375
**       pgno INTEGER,
10376
**       cell INTEGER,
10377
**       field INTEGER,
10378
**       value ANY,
10379
**       schema TEXT HIDDEN
10380
**     );
10381
**
10382
**   IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
10383
**   FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
10384
**   "schema".
10385
**
10386
**   Each page of the database is inspected. If it cannot be interpreted as
10387
**   a b-tree page, or if it is a b-tree page containing 0 entries, the
10388
**   sqlite_dbdata table contains no rows for that page.  Otherwise, the
10389
**   table contains one row for each field in the record associated with
10390
**   each cell on the page. For intkey b-trees, the key value is stored in
10391
**   field -1.
10392
**
10393
**   For example, for the database:
10394
**
10395
**     CREATE TABLE t1(a, b);     -- root page is page 2
10396
**     INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
10397
**     INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
10398
**
10399
**   the sqlite_dbdata table contains, as well as from entries related to
10400
**   page 1, content equivalent to:
10401
**
10402
**     INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
10403
**         (2, 0, -1, 5     ),
10404
**         (2, 0,  0, 'v'   ),
10405
**         (2, 0,  1, 'five'),
10406
**         (2, 1, -1, 10    ),
10407
**         (2, 1,  0, 'x'   ),
10408
**         (2, 1,  1, 'ten' );
10409
**
10410
**   If database corruption is encountered, this module does not report an
10411
**   error. Instead, it attempts to extract as much data as possible and
10412
**   ignores the corruption.
10413
**
10414
** SQLITE_DBPTR:
10415
**   The sqlite_dbptr table has the following schema:
10416
**
10417
**     CREATE TABLE sqlite_dbptr(
10418
**       pgno INTEGER,
10419
**       child INTEGER,
10420
**       schema TEXT HIDDEN
10421
**     );
10422
**
10423
**   It contains one entry for each b-tree pointer between a parent and
10424
**   child page in the database.
10425
*/
10426
#if !defined(SQLITEINT_H)
10427
/* #include "sqlite3ext.h" */
10428
 
10429
/* typedef unsigned char u8; */
10430
 
10431
#endif
10432
SQLITE_EXTENSION_INIT1
10433
#include 
10434
#include 
10435
 
10436
#define DBDATA_PADDING_BYTES 100
10437
 
10438
typedef struct DbdataTable DbdataTable;
10439
typedef struct DbdataCursor DbdataCursor;
10440
 
10441
/* Cursor object */
10442
struct DbdataCursor {
10443
  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
10444
  sqlite3_stmt *pStmt;            /* For fetching database pages */
10445
 
10446
  int iPgno;                      /* Current page number */
10447
  u8 *aPage;                      /* Buffer containing page */
10448
  int nPage;                      /* Size of aPage[] in bytes */
10449
  int nCell;                      /* Number of cells on aPage[] */
10450
  int iCell;                      /* Current cell number */
10451
  int bOnePage;                   /* True to stop after one page */
10452
  int szDb;
10453
  sqlite3_int64 iRowid;
10454
 
10455
  /* Only for the sqlite_dbdata table */
10456
  u8 *pRec;                       /* Buffer containing current record */
10457
  int nRec;                       /* Size of pRec[] in bytes */
10458
  int nHdr;                       /* Size of header in bytes */
10459
  int iField;                     /* Current field number */
10460
  u8 *pHdrPtr;
10461
  u8 *pPtr;
10462
 
10463
  sqlite3_int64 iIntkey;          /* Integer key value */
10464
};
10465
 
10466
/* Table object */
10467
struct DbdataTable {
10468
  sqlite3_vtab base;              /* Base class.  Must be first */
10469
  sqlite3 *db;                    /* The database connection */
10470
  sqlite3_stmt *pStmt;            /* For fetching database pages */
10471
  int bPtr;                       /* True for sqlite3_dbptr table */
10472
};
10473
 
10474
/* Column and schema definitions for sqlite_dbdata */
10475
#define DBDATA_COLUMN_PGNO        0
10476
#define DBDATA_COLUMN_CELL        1
10477
#define DBDATA_COLUMN_FIELD       2
10478
#define DBDATA_COLUMN_VALUE       3
10479
#define DBDATA_COLUMN_SCHEMA      4
10480
#define DBDATA_SCHEMA             \
10481
      "CREATE TABLE x("           \
10482
      "  pgno INTEGER,"           \
10483
      "  cell INTEGER,"           \
10484
      "  field INTEGER,"          \
10485
      "  value ANY,"              \
10486
      "  schema TEXT HIDDEN"      \
10487
      ")"
10488
 
10489
/* Column and schema definitions for sqlite_dbptr */
10490
#define DBPTR_COLUMN_PGNO         0
10491
#define DBPTR_COLUMN_CHILD        1
10492
#define DBPTR_COLUMN_SCHEMA       2
10493
#define DBPTR_SCHEMA              \
10494
      "CREATE TABLE x("           \
10495
      "  pgno INTEGER,"           \
10496
      "  child INTEGER,"          \
10497
      "  schema TEXT HIDDEN"      \
10498
      ")"
10499
 
10500
/*
10501
** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
10502
** table.
10503
*/
10504
static int dbdataConnect(
10505
  sqlite3 *db,
10506
  void *pAux,
10507
  int argc, const char *const*argv,
10508
  sqlite3_vtab **ppVtab,
10509
  char **pzErr
10510
){
10511
  DbdataTable *pTab = 0;
10512
  int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
10513
 
10514
  if( rc==SQLITE_OK ){
10515
    pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
10516
    if( pTab==0 ){
10517
      rc = SQLITE_NOMEM;
10518
    }else{
10519
      memset(pTab, 0, sizeof(DbdataTable));
10520
      pTab->db = db;
10521
      pTab->bPtr = (pAux!=0);
10522
    }
10523
  }
10524
 
10525
  *ppVtab = (sqlite3_vtab*)pTab;
10526
  return rc;
10527
}
10528
 
10529
/*
10530
** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
10531
*/
10532
static int dbdataDisconnect(sqlite3_vtab *pVtab){
10533
  DbdataTable *pTab = (DbdataTable*)pVtab;
10534
  if( pTab ){
10535
    sqlite3_finalize(pTab->pStmt);
10536
    sqlite3_free(pVtab);
10537
  }
10538
  return SQLITE_OK;
10539
}
10540
 
10541
/*
10542
** This function interprets two types of constraints:
10543
**
10544
**       schema=?
10545
**       pgno=?
10546
**
10547
** If neither are present, idxNum is set to 0. If schema=? is present,
10548
** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
10549
** in idxNum is set.
10550
**
10551
** If both parameters are present, schema is in position 0 and pgno in
10552
** position 1.
10553
*/
10554
static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
10555
  DbdataTable *pTab = (DbdataTable*)tab;
10556
  int i;
10557
  int iSchema = -1;
10558
  int iPgno = -1;
10559
  int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
10560
 
10561
  for(i=0; inConstraint; i++){
10562
    struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
10563
    if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
10564
      if( p->iColumn==colSchema ){
10565
        if( p->usable==0 ) return SQLITE_CONSTRAINT;
10566
        iSchema = i;
10567
      }
10568
      if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
10569
        iPgno = i;
10570
      }
10571
    }
10572
  }
10573
 
10574
  if( iSchema>=0 ){
10575
    pIdx->aConstraintUsage[iSchema].argvIndex = 1;
10576
    pIdx->aConstraintUsage[iSchema].omit = 1;
10577
  }
10578
  if( iPgno>=0 ){
10579
    pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
10580
    pIdx->aConstraintUsage[iPgno].omit = 1;
10581
    pIdx->estimatedCost = 100;
10582
    pIdx->estimatedRows =  50;
10583
 
10584
    if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
10585
      int iCol = pIdx->aOrderBy[0].iColumn;
10586
      if( pIdx->nOrderBy==1 ){
10587
        pIdx->orderByConsumed = (iCol==0 || iCol==1);
10588
      }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
10589
        pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
10590
      }
10591
    }
10592
 
10593
  }else{
10594
    pIdx->estimatedCost = 100000000;
10595
    pIdx->estimatedRows = 1000000000;
10596
  }
10597
  pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
10598
  return SQLITE_OK;
10599
}
10600
 
10601
/*
10602
** Open a new sqlite_dbdata or sqlite_dbptr cursor.
10603
*/
10604
static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
10605
  DbdataCursor *pCsr;
10606
 
10607
  pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
10608
  if( pCsr==0 ){
10609
    return SQLITE_NOMEM;
10610
  }else{
10611
    memset(pCsr, 0, sizeof(DbdataCursor));
10612
    pCsr->base.pVtab = pVTab;
10613
  }
10614
 
10615
  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
10616
  return SQLITE_OK;
10617
}
10618
 
10619
/*
10620
** Restore a cursor object to the state it was in when first allocated
10621
** by dbdataOpen().
10622
*/
10623
static void dbdataResetCursor(DbdataCursor *pCsr){
10624
  DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
10625
  if( pTab->pStmt==0 ){
10626
    pTab->pStmt = pCsr->pStmt;
10627
  }else{
10628
    sqlite3_finalize(pCsr->pStmt);
10629
  }
10630
  pCsr->pStmt = 0;
10631
  pCsr->iPgno = 1;
10632
  pCsr->iCell = 0;
10633
  pCsr->iField = 0;
10634
  pCsr->bOnePage = 0;
10635
  sqlite3_free(pCsr->aPage);
10636
  sqlite3_free(pCsr->pRec);
10637
  pCsr->pRec = 0;
10638
  pCsr->aPage = 0;
10639
}
10640
 
10641
/*
10642
** Close an sqlite_dbdata or sqlite_dbptr cursor.
10643
*/
10644
static int dbdataClose(sqlite3_vtab_cursor *pCursor){
10645
  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10646
  dbdataResetCursor(pCsr);
10647
  sqlite3_free(pCsr);
10648
  return SQLITE_OK;
10649
}
10650
 
10651
/*
10652
** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
10653
*/
10654
static unsigned int get_uint16(unsigned char *a){
10655
  return (a[0]<<8)|a[1];
10656
}
10657
static unsigned int get_uint32(unsigned char *a){
10658
  return ((unsigned int)a[0]<<24)
10659
       | ((unsigned int)a[1]<<16)
10660
       | ((unsigned int)a[2]<<8)
10661
       | ((unsigned int)a[3]);
10662
}
10663
 
10664
/*
10665
** Load page pgno from the database via the sqlite_dbpage virtual table.
10666
** If successful, set (*ppPage) to point to a buffer containing the page
10667
** data, (*pnPage) to the size of that buffer in bytes and return
10668
** SQLITE_OK. In this case it is the responsibility of the caller to
10669
** eventually free the buffer using sqlite3_free().
10670
**
10671
** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
10672
** return an SQLite error code.
10673
*/
10674
static int dbdataLoadPage(
10675
  DbdataCursor *pCsr,             /* Cursor object */
10676
  unsigned int pgno,              /* Page number of page to load */
10677
  u8 **ppPage,                    /* OUT: pointer to page buffer */
10678
  int *pnPage                     /* OUT: Size of (*ppPage) in bytes */
10679
){
10680
  int rc2;
10681
  int rc = SQLITE_OK;
10682
  sqlite3_stmt *pStmt = pCsr->pStmt;
10683
 
10684
  *ppPage = 0;
10685
  *pnPage = 0;
10686
  sqlite3_bind_int64(pStmt, 2, pgno);
10687
  if( SQLITE_ROW==sqlite3_step(pStmt) ){
10688
    int nCopy = sqlite3_column_bytes(pStmt, 0);
10689
    if( nCopy>0 ){
10690
      u8 *pPage;
10691
      pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
10692
      if( pPage==0 ){
10693
        rc = SQLITE_NOMEM;
10694
      }else{
10695
        const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
10696
        memcpy(pPage, pCopy, nCopy);
10697
        memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
10698
      }
10699
      *ppPage = pPage;
10700
      *pnPage = nCopy;
10701
    }
10702
  }
10703
  rc2 = sqlite3_reset(pStmt);
10704
  if( rc==SQLITE_OK ) rc = rc2;
10705
 
10706
  return rc;
10707
}
10708
 
10709
/*
10710
** Read a varint.  Put the value in *pVal and return the number of bytes.
10711
*/
10712
static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
10713
  sqlite3_int64 v = 0;
10714
  int i;
10715
  for(i=0; i<8; i++){
10716
    v = (v<<7) + (z[i]&0x7f);
10717
    if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
10718
  }
10719
  v = (v<<8) + (z[i]&0xff);
10720
  *pVal = v;
10721
  return 9;
10722
}
10723
 
10724
/*
10725
** Return the number of bytes of space used by an SQLite value of type
10726
** eType.
10727
*/
10728
static int dbdataValueBytes(int eType){
10729
  switch( eType ){
10730
    case 0: case 8: case 9:
10731
    case 10: case 11:
10732
      return 0;
10733
    case 1:
10734
      return 1;
10735
    case 2:
10736
      return 2;
10737
    case 3:
10738
      return 3;
10739
    case 4:
10740
      return 4;
10741
    case 5:
10742
      return 6;
10743
    case 6:
10744
    case 7:
10745
      return 8;
10746
    default:
10747
      if( eType>0 ){
10748
        return ((eType-12) / 2);
10749
      }
10750
      return 0;
10751
  }
10752
}
10753
 
10754
/*
10755
** Load a value of type eType from buffer pData and use it to set the
10756
** result of context object pCtx.
10757
*/
10758
static void dbdataValue(
10759
  sqlite3_context *pCtx,
10760
  int eType,
10761
  u8 *pData,
10762
  int nData
10763
){
10764
  if( eType>=0 && dbdataValueBytes(eType)<=nData ){
10765
    switch( eType ){
10766
      case 0:
10767
      case 10:
10768
      case 11:
10769
        sqlite3_result_null(pCtx);
10770
        break;
10771
 
10772
      case 8:
10773
        sqlite3_result_int(pCtx, 0);
10774
        break;
10775
      case 9:
10776
        sqlite3_result_int(pCtx, 1);
10777
        break;
10778
 
10779
      case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
10780
        sqlite3_uint64 v = (signed char)pData[0];
10781
        pData++;
10782
        switch( eType ){
10783
          case 7:
10784
          case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
10785
          case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
10786
          case 4:  v = (v<<8) + pData[0];  pData++;
10787
          case 3:  v = (v<<8) + pData[0];  pData++;
10788
          case 2:  v = (v<<8) + pData[0];  pData++;
10789
        }
10790
 
10791
        if( eType==7 ){
10792
          double r;
10793
          memcpy(&r, &v, sizeof(r));
10794
          sqlite3_result_double(pCtx, r);
10795
        }else{
10796
          sqlite3_result_int64(pCtx, (sqlite3_int64)v);
10797
        }
10798
        break;
10799
      }
10800
 
10801
      default: {
10802
        int n = ((eType-12) / 2);
10803
        if( eType % 2 ){
10804
          sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
10805
        }else{
10806
          sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
10807
        }
10808
      }
10809
    }
10810
  }
10811
}
10812
 
10813
/*
10814
** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
10815
*/
10816
static int dbdataNext(sqlite3_vtab_cursor *pCursor){
10817
  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10818
  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
10819
 
10820
  pCsr->iRowid++;
10821
  while( 1 ){
10822
    int rc;
10823
    int iOff = (pCsr->iPgno==1 ? 100 : 0);
10824
    int bNextPage = 0;
10825
 
10826
    if( pCsr->aPage==0 ){
10827
      while( 1 ){
10828
        if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
10829
        rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
10830
        if( rc!=SQLITE_OK ) return rc;
10831
        if( pCsr->aPage ) break;
10832
        pCsr->iPgno++;
10833
      }
10834
      pCsr->iCell = pTab->bPtr ? -2 : 0;
10835
      pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
10836
    }
10837
 
10838
    if( pTab->bPtr ){
10839
      if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
10840
        pCsr->iCell = pCsr->nCell;
10841
      }
10842
      pCsr->iCell++;
10843
      if( pCsr->iCell>=pCsr->nCell ){
10844
        sqlite3_free(pCsr->aPage);
10845
        pCsr->aPage = 0;
10846
        if( pCsr->bOnePage ) return SQLITE_OK;
10847
        pCsr->iPgno++;
10848
      }else{
10849
        return SQLITE_OK;
10850
      }
10851
    }else{
10852
      /* If there is no record loaded, load it now. */
10853
      if( pCsr->pRec==0 ){
10854
        int bHasRowid = 0;
10855
        int nPointer = 0;
10856
        sqlite3_int64 nPayload = 0;
10857
        sqlite3_int64 nHdr = 0;
10858
        int iHdr;
10859
        int U, X;
10860
        int nLocal;
10861
 
10862
        switch( pCsr->aPage[iOff] ){
10863
          case 0x02:
10864
            nPointer = 4;
10865
            break;
10866
          case 0x0a:
10867
            break;
10868
          case 0x0d:
10869
            bHasRowid = 1;
10870
            break;
10871
          default:
10872
            /* This is not a b-tree page with records on it. Continue. */
10873
            pCsr->iCell = pCsr->nCell;
10874
            break;
10875
        }
10876
 
10877
        if( pCsr->iCell>=pCsr->nCell ){
10878
          bNextPage = 1;
10879
        }else{
10880
 
10881
          iOff += 8 + nPointer + pCsr->iCell*2;
10882
          if( iOff>pCsr->nPage ){
10883
            bNextPage = 1;
10884
          }else{
10885
            iOff = get_uint16(&pCsr->aPage[iOff]);
10886
          }
10887
 
10888
          /* For an interior node cell, skip past the child-page number */
10889
          iOff += nPointer;
10890
 
10891
          /* Load the "byte of payload including overflow" field */
10892
          if( bNextPage || iOff>pCsr->nPage ){
10893
            bNextPage = 1;
10894
          }else{
10895
            iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
10896
          }
10897
 
10898
          /* If this is a leaf intkey cell, load the rowid */
10899
          if( bHasRowid && !bNextPage && iOffnPage ){
10900
            iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
10901
          }
10902
 
10903
          /* Figure out how much data to read from the local page */
10904
          U = pCsr->nPage;
10905
          if( bHasRowid ){
10906
            X = U-35;
10907
          }else{
10908
            X = ((U-12)*64/255)-23;
10909
          }
10910
          if( nPayload<=X ){
10911
            nLocal = nPayload;
10912
          }else{
10913
            int M, K;
10914
            M = ((U-12)*32/255)-23;
10915
            K = M+((nPayload-M)%(U-4));
10916
            if( K<=X ){
10917
              nLocal = K;
10918
            }else{
10919
              nLocal = M;
10920
            }
10921
          }
10922
 
10923
          if( bNextPage || nLocal+iOff>pCsr->nPage ){
10924
            bNextPage = 1;
10925
          }else{
10926
 
10927
            /* Allocate space for payload. And a bit more to catch small buffer
10928
            ** overruns caused by attempting to read a varint or similar from
10929
            ** near the end of a corrupt record.  */
10930
            pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
10931
            if( pCsr->pRec==0 ) return SQLITE_NOMEM;
10932
            memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
10933
            pCsr->nRec = nPayload;
10934
 
10935
            /* Load the nLocal bytes of payload */
10936
            memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
10937
            iOff += nLocal;
10938
 
10939
            /* Load content from overflow pages */
10940
            if( nPayload>nLocal ){
10941
              sqlite3_int64 nRem = nPayload - nLocal;
10942
              unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
10943
              while( nRem>0 ){
10944
                u8 *aOvfl = 0;
10945
                int nOvfl = 0;
10946
                int nCopy;
10947
                rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
10948
                assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
10949
                if( rc!=SQLITE_OK ) return rc;
10950
                if( aOvfl==0 ) break;
10951
 
10952
                nCopy = U-4;
10953
                if( nCopy>nRem ) nCopy = nRem;
10954
                memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
10955
                nRem -= nCopy;
10956
 
10957
                pgnoOvfl = get_uint32(aOvfl);
10958
                sqlite3_free(aOvfl);
10959
              }
10960
            }
10961
 
10962
            iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
10963
            pCsr->nHdr = nHdr;
10964
            pCsr->pHdrPtr = &pCsr->pRec[iHdr];
10965
            pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
10966
            pCsr->iField = (bHasRowid ? -1 : 0);
10967
          }
10968
        }
10969
      }else{
10970
        pCsr->iField++;
10971
        if( pCsr->iField>0 ){
10972
          sqlite3_int64 iType;
10973
          if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
10974
            bNextPage = 1;
10975
          }else{
10976
            pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
10977
            pCsr->pPtr += dbdataValueBytes(iType);
10978
          }
10979
        }
10980
      }
10981
 
10982
      if( bNextPage ){
10983
        sqlite3_free(pCsr->aPage);
10984
        sqlite3_free(pCsr->pRec);
10985
        pCsr->aPage = 0;
10986
        pCsr->pRec = 0;
10987
        if( pCsr->bOnePage ) return SQLITE_OK;
10988
        pCsr->iPgno++;
10989
      }else{
10990
        if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
10991
          return SQLITE_OK;
10992
        }
10993
 
10994
        /* Advance to the next cell. The next iteration of the loop will load
10995
        ** the record and so on. */
10996
        sqlite3_free(pCsr->pRec);
10997
        pCsr->pRec = 0;
10998
        pCsr->iCell++;
10999
      }
11000
    }
11001
  }
11002
 
11003
  assert( !"can't get here" );
11004
  return SQLITE_OK;
11005
}
11006
 
11007
/*
11008
** Return true if the cursor is at EOF.
11009
*/
11010
static int dbdataEof(sqlite3_vtab_cursor *pCursor){
11011
  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11012
  return pCsr->aPage==0;
11013
}
11014
 
11015
/*
11016
** Determine the size in pages of database zSchema (where zSchema is
11017
** "main", "temp" or the name of an attached database) and set
11018
** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
11019
** an SQLite error code.
11020
*/
11021
static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
11022
  DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
11023
  char *zSql = 0;
11024
  int rc, rc2;
11025
  sqlite3_stmt *pStmt = 0;
11026
 
11027
  zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
11028
  if( zSql==0 ) return SQLITE_NOMEM;
11029
  rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
11030
  sqlite3_free(zSql);
11031
  if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
11032
    pCsr->szDb = sqlite3_column_int(pStmt, 0);
11033
  }
11034
  rc2 = sqlite3_finalize(pStmt);
11035
  if( rc==SQLITE_OK ) rc = rc2;
11036
  return rc;
11037
}
11038
 
11039
/*
11040
** xFilter method for sqlite_dbdata and sqlite_dbptr.
11041
*/
11042
static int dbdataFilter(
11043
  sqlite3_vtab_cursor *pCursor,
11044
  int idxNum, const char *idxStr,
11045
  int argc, sqlite3_value **argv
11046
){
11047
  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11048
  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11049
  int rc = SQLITE_OK;
11050
  const char *zSchema = "main";
11051
 
11052
  dbdataResetCursor(pCsr);
11053
  assert( pCsr->iPgno==1 );
11054
  if( idxNum & 0x01 ){
11055
    zSchema = (const char*)sqlite3_value_text(argv[0]);
11056
  }
11057
  if( idxNum & 0x02 ){
11058
    pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
11059
    pCsr->bOnePage = 1;
11060
  }else{
11061
    pCsr->nPage = dbdataDbsize(pCsr, zSchema);
11062
    rc = dbdataDbsize(pCsr, zSchema);
11063
  }
11064
 
11065
  if( rc==SQLITE_OK ){
11066
    if( pTab->pStmt ){
11067
      pCsr->pStmt = pTab->pStmt;
11068
      pTab->pStmt = 0;
11069
    }else{
11070
      rc = sqlite3_prepare_v2(pTab->db,
11071
          "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
11072
          &pCsr->pStmt, 0
11073
      );
11074
    }
11075
  }
11076
  if( rc==SQLITE_OK ){
11077
    rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
11078
  }else{
11079
    pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
11080
  }
11081
  if( rc==SQLITE_OK ){
11082
    rc = dbdataNext(pCursor);
11083
  }
11084
  return rc;
11085
}
11086
 
11087
/*
11088
** Return a column for the sqlite_dbdata or sqlite_dbptr table.
11089
*/
11090
static int dbdataColumn(
11091
  sqlite3_vtab_cursor *pCursor,
11092
  sqlite3_context *ctx,
11093
  int i
11094
){
11095
  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11096
  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11097
  if( pTab->bPtr ){
11098
    switch( i ){
11099
      case DBPTR_COLUMN_PGNO:
11100
        sqlite3_result_int64(ctx, pCsr->iPgno);
11101
        break;
11102
      case DBPTR_COLUMN_CHILD: {
11103
        int iOff = pCsr->iPgno==1 ? 100 : 0;
11104
        if( pCsr->iCell<0 ){
11105
          iOff += 8;
11106
        }else{
11107
          iOff += 12 + pCsr->iCell*2;
11108
          if( iOff>pCsr->nPage ) return SQLITE_OK;
11109
          iOff = get_uint16(&pCsr->aPage[iOff]);
11110
        }
11111
        if( iOff<=pCsr->nPage ){
11112
          sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
11113
        }
11114
        break;
11115
      }
11116
    }
11117
  }else{
11118
    switch( i ){
11119
      case DBDATA_COLUMN_PGNO:
11120
        sqlite3_result_int64(ctx, pCsr->iPgno);
11121
        break;
11122
      case DBDATA_COLUMN_CELL:
11123
        sqlite3_result_int(ctx, pCsr->iCell);
11124
        break;
11125
      case DBDATA_COLUMN_FIELD:
11126
        sqlite3_result_int(ctx, pCsr->iField);
11127
        break;
11128
      case DBDATA_COLUMN_VALUE: {
11129
        if( pCsr->iField<0 ){
11130
          sqlite3_result_int64(ctx, pCsr->iIntkey);
11131
        }else{
11132
          sqlite3_int64 iType;
11133
          dbdataGetVarint(pCsr->pHdrPtr, &iType);
11134
          dbdataValue(
11135
              ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
11136
          );
11137
        }
11138
        break;
11139
      }
11140
    }
11141
  }
11142
  return SQLITE_OK;
11143
}
11144
 
11145
/*
11146
** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
11147
*/
11148
static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
11149
  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11150
  *pRowid = pCsr->iRowid;
11151
  return SQLITE_OK;
11152
}
11153
 
11154
 
11155
/*
11156
** Invoke this routine to register the "sqlite_dbdata" virtual table module
11157
*/
11158
static int sqlite3DbdataRegister(sqlite3 *db){
11159
  static sqlite3_module dbdata_module = {
11160
    0,                            /* iVersion */
11161
    0,                            /* xCreate */
11162
    dbdataConnect,                /* xConnect */
11163
    dbdataBestIndex,              /* xBestIndex */
11164
    dbdataDisconnect,             /* xDisconnect */
11165
    0,                            /* xDestroy */
11166
    dbdataOpen,                   /* xOpen - open a cursor */
11167
    dbdataClose,                  /* xClose - close a cursor */
11168
    dbdataFilter,                 /* xFilter - configure scan constraints */
11169
    dbdataNext,                   /* xNext - advance a cursor */
11170
    dbdataEof,                    /* xEof - check for end of scan */
11171
    dbdataColumn,                 /* xColumn - read data */
11172
    dbdataRowid,                  /* xRowid - read data */
11173
    0,                            /* xUpdate */
11174
    0,                            /* xBegin */
11175
    0,                            /* xSync */
11176
    0,                            /* xCommit */
11177
    0,                            /* xRollback */
11178
    0,                            /* xFindMethod */
11179
    0,                            /* xRename */
11180
    0,                            /* xSavepoint */
11181
    0,                            /* xRelease */
11182
    0,                            /* xRollbackTo */
11183
 
11184
  };
11185
 
11186
  int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
11187
  if( rc==SQLITE_OK ){
11188
    rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
11189
  }
11190
  return rc;
11191
}
11192
 
11193
#ifdef _WIN32
11194
 
11195
#endif
11196
int sqlite3_dbdata_init(
11197
  sqlite3 *db,
11198
  char **pzErrMsg,
11199
  const sqlite3_api_routines *pApi
11200
){
11201
  SQLITE_EXTENSION_INIT2(pApi);
11202
  return sqlite3DbdataRegister(db);
11203
}
11204
 
11205
/************************* End ../ext/misc/dbdata.c ********************/
11206
#endif
11207
 
11208
#if defined(SQLITE_ENABLE_SESSION)
11209
/*
11210
** State information for a single open session
11211
*/
11212
typedef struct OpenSession OpenSession;
11213
struct OpenSession {
11214
  char *zName;             /* Symbolic name for this session */
11215
  int nFilter;             /* Number of xFilter rejection GLOB patterns */
11216
  char **azFilter;         /* Array of xFilter rejection GLOB patterns */
11217
  sqlite3_session *p;      /* The open session */
11218
};
11219
#endif
11220
 
11221
typedef struct ExpertInfo ExpertInfo;
11222
struct ExpertInfo {
11223
  sqlite3expert *pExpert;
11224
  int bVerbose;
11225
};
11226
 
11227
/* A single line in the EQP output */
11228
typedef struct EQPGraphRow EQPGraphRow;
11229
struct EQPGraphRow {
11230
  int iEqpId;           /* ID for this row */
11231
  int iParentId;        /* ID of the parent row */
11232
  EQPGraphRow *pNext;   /* Next row in sequence */
11233
  char zText[1];        /* Text to display for this row */
11234
};
11235
 
11236
/* All EQP output is collected into an instance of the following */
11237
typedef struct EQPGraph EQPGraph;
11238
struct EQPGraph {
11239
  EQPGraphRow *pRow;    /* Linked list of all rows of the EQP output */
11240
  EQPGraphRow *pLast;   /* Last element of the pRow list */
11241
  char zPrefix[100];    /* Graph prefix */
11242
};
11243
 
11244
/*
11245
** State information about the database connection is contained in an
11246
** instance of the following structure.
11247
*/
11248
typedef struct ShellState ShellState;
11249
struct ShellState {
11250
  sqlite3 *db;           /* The database */
11251
  u8 autoExplain;        /* Automatically turn on .explain mode */
11252
  u8 autoEQP;            /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
11253
  u8 autoEQPtest;        /* autoEQP is in test mode */
11254
  u8 autoEQPtrace;       /* autoEQP is in trace mode */
11255
  u8 scanstatsOn;        /* True to display scan stats before each finalize */
11256
  u8 openMode;           /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
11257
  u8 doXdgOpen;          /* Invoke start/open/xdg-open in output_reset() */
11258
  u8 nEqpLevel;          /* Depth of the EQP output graph */
11259
  u8 eTraceType;         /* SHELL_TRACE_* value for type of trace */
11260
  unsigned statsOn;      /* True to display memory stats before each finalize */
11261
  unsigned mEqpLines;    /* Mask of veritical lines in the EQP output graph */
11262
  int outCount;          /* Revert to stdout when reaching zero */
11263
  int cnt;               /* Number of records displayed so far */
11264
  int lineno;            /* Line number of last line read from in */
11265
  int openFlags;         /* Additional flags to open.  (SQLITE_OPEN_NOFOLLOW) */
11266
  FILE *in;              /* Read commands from this stream */
11267
  FILE *out;             /* Write results here */
11268
  FILE *traceOut;        /* Output for sqlite3_trace() */
11269
  int nErr;              /* Number of errors seen */
11270
  int mode;              /* An output mode setting */
11271
  int modePrior;         /* Saved mode */
11272
  int cMode;             /* temporary output mode for the current query */
11273
  int normalMode;        /* Output mode before ".explain on" */
11274
  int writableSchema;    /* True if PRAGMA writable_schema=ON */
11275
  int showHeader;        /* True to show column names in List or Column mode */
11276
  int nCheck;            /* Number of ".check" commands run */
11277
  unsigned nProgress;    /* Number of progress callbacks encountered */
11278
  unsigned mxProgress;   /* Maximum progress callbacks before failing */
11279
  unsigned flgProgress;  /* Flags for the progress callback */
11280
  unsigned shellFlgs;    /* Various flags */
11281
  unsigned priorShFlgs;  /* Saved copy of flags */
11282
  sqlite3_int64 szMax;   /* --maxsize argument to .open */
11283
  char *zDestTable;      /* Name of destination table when MODE_Insert */
11284
  char *zTempFile;       /* Temporary file that might need deleting */
11285
  char zTestcase[30];    /* Name of current test case */
11286
  char colSeparator[20]; /* Column separator character for several modes */
11287
  char rowSeparator[20]; /* Row separator character for MODE_Ascii */
11288
  char colSepPrior[20];  /* Saved column separator */
11289
  char rowSepPrior[20];  /* Saved row separator */
11290
  int *colWidth;         /* Requested width of each column in columnar modes */
11291
  int *actualWidth;      /* Actual width of each column */
11292
  int nWidth;            /* Number of slots in colWidth[] and actualWidth[] */
11293
  char nullValue[20];    /* The text to print when a NULL comes back from
11294
                         ** the database */
11295
  char outfile[FILENAME_MAX]; /* Filename for *out */
11296
  const char *zDbFilename;    /* name of the database file */
11297
  char *zFreeOnClose;         /* Filename to free when closing */
11298
  const char *zVfs;           /* Name of VFS to use */
11299
  sqlite3_stmt *pStmt;   /* Current statement if any. */
11300
  FILE *pLog;            /* Write log output here */
11301
  int *aiIndent;         /* Array of indents used in MODE_Explain */
11302
  int nIndent;           /* Size of array aiIndent[] */
11303
  int iIndent;           /* Index of current op in aiIndent[] */
11304
  EQPGraph sGraph;       /* Information for the graphical EXPLAIN QUERY PLAN */
11305
#if defined(SQLITE_ENABLE_SESSION)
11306
  int nSession;             /* Number of active sessions */
11307
  OpenSession aSession[4];  /* Array of sessions.  [0] is in focus. */
11308
#endif
11309
  ExpertInfo expert;        /* Valid if previous command was ".expert OPT..." */
11310
};
11311
 
11312
 
11313
/* Allowed values for ShellState.autoEQP
11314
*/
11315
#define AUTOEQP_off      0           /* Automatic EXPLAIN QUERY PLAN is off */
11316
#define AUTOEQP_on       1           /* Automatic EQP is on */
11317
#define AUTOEQP_trigger  2           /* On and also show plans for triggers */
11318
#define AUTOEQP_full     3           /* Show full EXPLAIN */
11319
 
11320
/* Allowed values for ShellState.openMode
11321
*/
11322
#define SHELL_OPEN_UNSPEC      0      /* No open-mode specified */
11323
#define SHELL_OPEN_NORMAL      1      /* Normal database file */
11324
#define SHELL_OPEN_APPENDVFS   2      /* Use appendvfs */
11325
#define SHELL_OPEN_ZIPFILE     3      /* Use the zipfile virtual table */
11326
#define SHELL_OPEN_READONLY    4      /* Open a normal database read-only */
11327
#define SHELL_OPEN_DESERIALIZE 5      /* Open using sqlite3_deserialize() */
11328
#define SHELL_OPEN_HEXDB       6      /* Use "dbtotxt" output as data source */
11329
 
11330
/* Allowed values for ShellState.eTraceType
11331
*/
11332
#define SHELL_TRACE_PLAIN      0      /* Show input SQL text */
11333
#define SHELL_TRACE_EXPANDED   1      /* Show expanded SQL text */
11334
#define SHELL_TRACE_NORMALIZED 2      /* Show normalized SQL text */
11335
 
11336
/* Bits in the ShellState.flgProgress variable */
11337
#define SHELL_PROGRESS_QUIET 0x01  /* Omit announcing every progress callback */
11338
#define SHELL_PROGRESS_RESET 0x02  /* Reset the count when the progres
11339
                                   ** callback limit is reached, and for each
11340
                                   ** top-level SQL statement */
11341
#define SHELL_PROGRESS_ONCE  0x04  /* Cancel the --limit after firing once */
11342
 
11343
/*
11344
** These are the allowed shellFlgs values
11345
*/
11346
#define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
11347
#define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
11348
#define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
11349
#define SHFLG_PreserveRowid  0x00000008 /* .dump preserves rowid values */
11350
#define SHFLG_Newlines       0x00000010 /* .dump --newline flag */
11351
#define SHFLG_CountChanges   0x00000020 /* .changes setting */
11352
#define SHFLG_Echo           0x00000040 /* .echo or --echo setting */
11353
#define SHFLG_HeaderSet      0x00000080 /* .header has been used */
11354
#define SHFLG_DumpDataOnly   0x00000100 /* .dump show data only */
11355
#define SHFLG_DumpNoSys      0x00000200 /* .dump omits system tables */
11356
 
11357
/*
11358
** Macros for testing and setting shellFlgs
11359
*/
11360
#define ShellHasFlag(P,X)    (((P)->shellFlgs & (X))!=0)
11361
#define ShellSetFlag(P,X)    ((P)->shellFlgs|=(X))
11362
#define ShellClearFlag(P,X)  ((P)->shellFlgs&=(~(X)))
11363
 
11364
/*
11365
** These are the allowed modes.
11366
*/
11367
#define MODE_Line     0  /* One column per line.  Blank line between records */
11368
#define MODE_Column   1  /* One record per line in neat columns */
11369
#define MODE_List     2  /* One record per line with a separator */
11370
#define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
11371
#define MODE_Html     4  /* Generate an XHTML table */
11372
#define MODE_Insert   5  /* Generate SQL "insert" statements */
11373
#define MODE_Quote    6  /* Quote values as for SQL */
11374
#define MODE_Tcl      7  /* Generate ANSI-C or TCL quoted elements */
11375
#define MODE_Csv      8  /* Quote strings, numbers are plain */
11376
#define MODE_Explain  9  /* Like MODE_Column, but do not truncate data */
11377
#define MODE_Ascii   10  /* Use ASCII unit and record separators (0x1F/0x1E) */
11378
#define MODE_Pretty  11  /* Pretty-print schemas */
11379
#define MODE_EQP     12  /* Converts EXPLAIN QUERY PLAN output into a graph */
11380
#define MODE_Json    13  /* Output JSON */
11381
#define MODE_Markdown 14 /* Markdown formatting */
11382
#define MODE_Table   15  /* MySQL-style table formatting */
11383
#define MODE_Box     16  /* Unicode box-drawing characters */
11384
 
11385
static const char *modeDescr[] = {
11386
  "line",
11387
  "column",
11388
  "list",
11389
  "semi",
11390
  "html",
11391
  "insert",
11392
  "quote",
11393
  "tcl",
11394
  "csv",
11395
  "explain",
11396
  "ascii",
11397
  "prettyprint",
11398
  "eqp",
11399
  "json",
11400
  "markdown",
11401
  "table",
11402
  "box"
11403
};
11404
 
11405
/*
11406
** These are the column/row/line separators used by the various
11407
** import/export modes.
11408
*/
11409
#define SEP_Column    "|"
11410
#define SEP_Row       "\n"
11411
#define SEP_Tab       "\t"
11412
#define SEP_Space     " "
11413
#define SEP_Comma     ","
11414
#define SEP_CrLf      "\r\n"
11415
#define SEP_Unit      "\x1F"
11416
#define SEP_Record    "\x1E"
11417
 
11418
/*
11419
** A callback for the sqlite3_log() interface.
11420
*/
11421
static void shellLog(void *pArg, int iErrCode, const char *zMsg){
11422
  ShellState *p = (ShellState*)pArg;
11423
  if( p->pLog==0 ) return;
11424
  utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
11425
  fflush(p->pLog);
11426
}
11427
 
11428
/*
11429
** SQL function:  shell_putsnl(X)
11430
**
11431
** Write the text X to the screen (or whatever output is being directed)
11432
** adding a newline at the end, and then return X.
11433
*/
11434
static void shellPutsFunc(
11435
  sqlite3_context *pCtx,
11436
  int nVal,
11437
  sqlite3_value **apVal
11438
){
11439
  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
11440
  (void)nVal;
11441
  utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
11442
  sqlite3_result_value(pCtx, apVal[0]);
11443
}
11444
 
11445
/*
11446
** SQL function:   edit(VALUE)
11447
**                 edit(VALUE,EDITOR)
11448
**
11449
** These steps:
11450
**
11451
**     (1) Write VALUE into a temporary file.
11452
**     (2) Run program EDITOR on that temporary file.
11453
**     (3) Read the temporary file back and return its content as the result.
11454
**     (4) Delete the temporary file
11455
**
11456
** If the EDITOR argument is omitted, use the value in the VISUAL
11457
** environment variable.  If still there is no EDITOR, through an error.
11458
**
11459
** Also throw an error if the EDITOR program returns a non-zero exit code.
11460
*/
11461
#ifndef SQLITE_NOHAVE_SYSTEM
11462
static void editFunc(
11463
  sqlite3_context *context,
11464
  int argc,
11465
  sqlite3_value **argv
11466
){
11467
  const char *zEditor;
11468
  char *zTempFile = 0;
11469
  sqlite3 *db;
11470
  char *zCmd = 0;
11471
  int bBin;
11472
  int rc;
11473
  int hasCRNL = 0;
11474
  FILE *f = 0;
11475
  sqlite3_int64 sz;
11476
  sqlite3_int64 x;
11477
  unsigned char *p = 0;
11478
 
11479
  if( argc==2 ){
11480
    zEditor = (const char*)sqlite3_value_text(argv[1]);
11481
  }else{
11482
    zEditor = getenv("VISUAL");
11483
  }
11484
  if( zEditor==0 ){
11485
    sqlite3_result_error(context, "no editor for edit()", -1);
11486
    return;
11487
  }
11488
  if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
11489
    sqlite3_result_error(context, "NULL input to edit()", -1);
11490
    return;
11491
  }
11492
  db = sqlite3_context_db_handle(context);
11493
  zTempFile = 0;
11494
  sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
11495
  if( zTempFile==0 ){
11496
    sqlite3_uint64 r = 0;
11497
    sqlite3_randomness(sizeof(r), &r);
11498
    zTempFile = sqlite3_mprintf("temp%llx", r);
11499
    if( zTempFile==0 ){
11500
      sqlite3_result_error_nomem(context);
11501
      return;
11502
    }
11503
  }
11504
  bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
11505
  /* When writing the file to be edited, do \n to \r\n conversions on systems
11506
  ** that want \r\n line endings */
11507
  f = fopen(zTempFile, bBin ? "wb" : "w");
11508
  if( f==0 ){
11509
    sqlite3_result_error(context, "edit() cannot open temp file", -1);
11510
    goto edit_func_end;
11511
  }
11512
  sz = sqlite3_value_bytes(argv[0]);
11513
  if( bBin ){
11514
    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
11515
  }else{
11516
    const char *z = (const char*)sqlite3_value_text(argv[0]);
11517
    /* Remember whether or not the value originally contained \r\n */
11518
    if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
11519
    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
11520
  }
11521
  fclose(f);
11522
  f = 0;
11523
  if( x!=sz ){
11524
    sqlite3_result_error(context, "edit() could not write the whole file", -1);
11525
    goto edit_func_end;
11526
  }
11527
  zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
11528
  if( zCmd==0 ){
11529
    sqlite3_result_error_nomem(context);
11530
    goto edit_func_end;
11531
  }
11532
  rc = system(zCmd);
11533
  sqlite3_free(zCmd);
11534
  if( rc ){
11535
    sqlite3_result_error(context, "EDITOR returned non-zero", -1);
11536
    goto edit_func_end;
11537
  }
11538
  f = fopen(zTempFile, "rb");
11539
  if( f==0 ){
11540
    sqlite3_result_error(context,
11541
      "edit() cannot reopen temp file after edit", -1);
11542
    goto edit_func_end;
11543
  }
11544
  fseek(f, 0, SEEK_END);
11545
  sz = ftell(f);
11546
  rewind(f);
11547
  p = sqlite3_malloc64( sz+1 );
11548
  if( p==0 ){
11549
    sqlite3_result_error_nomem(context);
11550
    goto edit_func_end;
11551
  }
11552
  x = fread(p, 1, (size_t)sz, f);
11553
  fclose(f);
11554
  f = 0;
11555
  if( x!=sz ){
11556
    sqlite3_result_error(context, "could not read back the whole file", -1);
11557
    goto edit_func_end;
11558
  }
11559
  if( bBin ){
11560
    sqlite3_result_blob64(context, p, sz, sqlite3_free);
11561
  }else{
11562
    sqlite3_int64 i, j;
11563
    if( hasCRNL ){
11564
      /* If the original contains \r\n then do no conversions back to \n */
11565
      j = sz;
11566
    }else{
11567
      /* If the file did not originally contain \r\n then convert any new
11568
      ** \r\n back into \n */
11569
      for(i=j=0; i
11570
        if( p[i]=='\r' && p[i+1]=='\n' ) i++;
11571
        p[j++] = p[i];
11572
      }
11573
      sz = j;
11574
      p[sz] = 0;
11575
    }
11576
    sqlite3_result_text64(context, (const char*)p, sz,
11577
                          sqlite3_free, SQLITE_UTF8);
11578
  }
11579
  p = 0;
11580
 
11581
edit_func_end:
11582
  if( f ) fclose(f);
11583
  unlink(zTempFile);
11584
  sqlite3_free(zTempFile);
11585
  sqlite3_free(p);
11586
}
11587
#endif /* SQLITE_NOHAVE_SYSTEM */
11588
 
11589
/*
11590
** Save or restore the current output mode
11591
*/
11592
static void outputModePush(ShellState *p){
11593
  p->modePrior = p->mode;
11594
  p->priorShFlgs = p->shellFlgs;
11595
  memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
11596
  memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
11597
}
11598
static void outputModePop(ShellState *p){
11599
  p->mode = p->modePrior;
11600
  p->shellFlgs = p->priorShFlgs;
11601
  memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
11602
  memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
11603
}
11604
 
11605
/*
11606
** Output the given string as a hex-encoded blob (eg. X'1234' )
11607
*/
11608
static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
11609
  int i;
11610
  char *zBlob = (char *)pBlob;
11611
  raw_printf(out,"X'");
11612
  for(i=0; i
11613
  raw_printf(out,"'");
11614
}
11615
 
11616
/*
11617
** Find a string that is not found anywhere in z[].  Return a pointer
11618
** to that string.
11619
**
11620
** Try to use zA and zB first.  If both of those are already found in z[]
11621
** then make up some string and store it in the buffer zBuf.
11622
*/
11623
static const char *unused_string(
11624
  const char *z,                    /* Result must not appear anywhere in z */
11625
  const char *zA, const char *zB,   /* Try these first */
11626
  char *zBuf                        /* Space to store a generated string */
11627
){
11628
  unsigned i = 0;
11629
  if( strstr(z, zA)==0 ) return zA;
11630
  if( strstr(z, zB)==0 ) return zB;
11631
  do{
11632
    sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
11633
  }while( strstr(z,zBuf)!=0 );
11634
  return zBuf;
11635
}
11636
 
11637
/*
11638
** Output the given string as a quoted string using SQL quoting conventions.
11639
**
11640
** See also: output_quoted_escaped_string()
11641
*/
11642
static void output_quoted_string(FILE *out, const char *z){
11643
  int i;
11644
  char c;
11645
  setBinaryMode(out, 1);
11646
  for(i=0; (c = z[i])!=0 && c!='\''; i++){}
11647
  if( c==0 ){
11648
    utf8_printf(out,"'%s'",z);
11649
  }else{
11650
    raw_printf(out, "'");
11651
    while( *z ){
11652
      for(i=0; (c = z[i])!=0 && c!='\''; i++){}
11653
      if( c=='\'' ) i++;
11654
      if( i ){
11655
        utf8_printf(out, "%.*s", i, z);
11656
        z += i;
11657
      }
11658
      if( c=='\'' ){
11659
        raw_printf(out, "'");
11660
        continue;
11661
      }
11662
      if( c==0 ){
11663
        break;
11664
      }
11665
      z++;
11666
    }
11667
    raw_printf(out, "'");
11668
  }
11669
  setTextMode(out, 1);
11670
}
11671
 
11672
/*
11673
** Output the given string as a quoted string using SQL quoting conventions.
11674
** Additionallly , escape the "\n" and "\r" characters so that they do not
11675
** get corrupted by end-of-line translation facilities in some operating
11676
** systems.
11677
**
11678
** This is like output_quoted_string() but with the addition of the \r\n
11679
** escape mechanism.
11680
*/
11681
static void output_quoted_escaped_string(FILE *out, const char *z){
11682
  int i;
11683
  char c;
11684
  setBinaryMode(out, 1);
11685
  for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
11686
  if( c==0 ){
11687
    utf8_printf(out,"'%s'",z);
11688
  }else{
11689
    const char *zNL = 0;
11690
    const char *zCR = 0;
11691
    int nNL = 0;
11692
    int nCR = 0;
11693
    char zBuf1[20], zBuf2[20];
11694
    for(i=0; z[i]; i++){
11695
      if( z[i]=='\n' ) nNL++;
11696
      if( z[i]=='\r' ) nCR++;
11697
    }
11698
    if( nNL ){
11699
      raw_printf(out, "replace(");
11700
      zNL = unused_string(z, "\\n", "\\012", zBuf1);
11701
    }
11702
    if( nCR ){
11703
      raw_printf(out, "replace(");
11704
      zCR = unused_string(z, "\\r", "\\015", zBuf2);
11705
    }
11706
    raw_printf(out, "'");
11707
    while( *z ){
11708
      for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
11709
      if( c=='\'' ) i++;
11710
      if( i ){
11711
        utf8_printf(out, "%.*s", i, z);
11712
        z += i;
11713
      }
11714
      if( c=='\'' ){
11715
        raw_printf(out, "'");
11716
        continue;
11717
      }
11718
      if( c==0 ){
11719
        break;
11720
      }
11721
      z++;
11722
      if( c=='\n' ){
11723
        raw_printf(out, "%s", zNL);
11724
        continue;
11725
      }
11726
      raw_printf(out, "%s", zCR);
11727
    }
11728
    raw_printf(out, "'");
11729
    if( nCR ){
11730
      raw_printf(out, ",'%s',char(13))", zCR);
11731
    }
11732
    if( nNL ){
11733
      raw_printf(out, ",'%s',char(10))", zNL);
11734
    }
11735
  }
11736
  setTextMode(out, 1);
11737
}
11738
 
11739
/*
11740
** Output the given string as a quoted according to C or TCL quoting rules.
11741
*/
11742
static void output_c_string(FILE *out, const char *z){
11743
  unsigned int c;
11744
  fputc('"', out);
11745
  while( (c = *(z++))!=0 ){
11746
    if( c=='\\' ){
11747
      fputc(c, out);
11748
      fputc(c, out);
11749
    }else if( c=='"' ){
11750
      fputc('\\', out);
11751
      fputc('"', out);
11752
    }else if( c=='\t' ){
11753
      fputc('\\', out);
11754
      fputc('t', out);
11755
    }else if( c=='\n' ){
11756
      fputc('\\', out);
11757
      fputc('n', out);
11758
    }else if( c=='\r' ){
11759
      fputc('\\', out);
11760
      fputc('r', out);
11761
    }else if( !isprint(c&0xff) ){
11762
      raw_printf(out, "\\%03o", c&0xff);
11763
    }else{
11764
      fputc(c, out);
11765
    }
11766
  }
11767
  fputc('"', out);
11768
}
11769
 
11770
/*
11771
** Output the given string as a quoted according to JSON quoting rules.
11772
*/
11773
static void output_json_string(FILE *out, const char *z, int n){
11774
  unsigned int c;
11775
  if( n<0 ) n = (int)strlen(z);
11776
  fputc('"', out);
11777
  while( n-- ){
11778
    c = *(z++);
11779
    if( c=='\\' || c=='"' ){
11780
      fputc('\\', out);
11781
      fputc(c, out);
11782
    }else if( c<=0x1f ){
11783
      fputc('\\', out);
11784
      if( c=='\b' ){
11785
        fputc('b', out);
11786
      }else if( c=='\f' ){
11787
        fputc('f', out);
11788
      }else if( c=='\n' ){
11789
        fputc('n', out);
11790
      }else if( c=='\r' ){
11791
        fputc('r', out);
11792
      }else if( c=='\t' ){
11793
        fputc('t', out);
11794
      }else{
11795
         raw_printf(out, "u%04x",c);
11796
      }
11797
    }else{
11798
      fputc(c, out);
11799
    }
11800
  }
11801
  fputc('"', out);
11802
}
11803
 
11804
/*
11805
** Output the given string with characters that are special to
11806
** HTML escaped.
11807
*/
11808
static void output_html_string(FILE *out, const char *z){
11809
  int i;
11810
  if( z==0 ) z = "";
11811
  while( *z ){
11812
    for(i=0;   z[i]
11813
            && z[i]!='<'
11814
            && z[i]!='&'
11815
            && z[i]!='>'
11816
            && z[i]!='\"'
11817
            && z[i]!='\'';
11818
        i++){}
11819
    if( i>0 ){
11820
      utf8_printf(out,"%.*s",i,z);
11821
    }
11822
    if( z[i]=='<' ){
11823
      raw_printf(out,"<");
11824
    }else if( z[i]=='&' ){
11825
      raw_printf(out,"&");
11826
    }else if( z[i]=='>' ){
11827
      raw_printf(out,">");
11828
    }else if( z[i]=='\"' ){
11829
      raw_printf(out,""");
11830
    }else if( z[i]=='\'' ){
11831
      raw_printf(out,"'");
11832
    }else{
11833
      break;
11834
    }
11835
    z += i + 1;
11836
  }
11837
}
11838
 
11839
/*
11840
** If a field contains any character identified by a 1 in the following
11841
** array, then the string must be quoted for CSV.
11842
*/
11843
static const char needCsvQuote[] = {
11844
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11845
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11846
  1, 0, 1, 0, 0, 0, 0, 1,   0, 0, 0, 0, 0, 0, 0, 0,
11847
  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
11848
  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
11849
  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
11850
  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,
11851
  0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 1,
11852
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11853
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11854
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11855
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11856
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11857
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11858
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11859
  1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,
11860
};
11861
 
11862
/*
11863
** Output a single term of CSV.  Actually, p->colSeparator is used for
11864
** the separator, which may or may not be a comma.  p->nullValue is
11865
** the null value.  Strings are quoted if necessary.  The separator
11866
** is only issued if bSep is true.
11867
*/
11868
static void output_csv(ShellState *p, const char *z, int bSep){
11869
  FILE *out = p->out;
11870
  if( z==0 ){
11871
    utf8_printf(out,"%s",p->nullValue);
11872
  }else{
11873
    int i;
11874
    int nSep = strlen30(p->colSeparator);
11875
    for(i=0; z[i]; i++){
11876
      if( needCsvQuote[((unsigned char*)z)[i]]
11877
         || (z[i]==p->colSeparator[0] &&
11878
             (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
11879
        i = 0;
11880
        break;
11881
      }
11882
    }
11883
    if( i==0 ){
11884
      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
11885
      utf8_printf(out, "%s", zQuoted);
11886
      sqlite3_free(zQuoted);
11887
    }else{
11888
      utf8_printf(out, "%s", z);
11889
    }
11890
  }
11891
  if( bSep ){
11892
    utf8_printf(p->out, "%s", p->colSeparator);
11893
  }
11894
}
11895
 
11896
/*
11897
** This routine runs when the user presses Ctrl-C
11898
*/
11899
static void interrupt_handler(int NotUsed){
11900
  UNUSED_PARAMETER(NotUsed);
11901
  seenInterrupt++;
11902
  if( seenInterrupt>2 ) exit(1);
11903
  if( globalDb ) sqlite3_interrupt(globalDb);
11904
}
11905
 
11906
#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
11907
/*
11908
** This routine runs for console events (e.g. Ctrl-C) on Win32
11909
*/
11910
static BOOL WINAPI ConsoleCtrlHandler(
11911
  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
11912
){
11913
  if( dwCtrlType==CTRL_C_EVENT ){
11914
    interrupt_handler(0);
11915
    return TRUE;
11916
  }
11917
  return FALSE;
11918
}
11919
#endif
11920
 
11921
#ifndef SQLITE_OMIT_AUTHORIZATION
11922
/*
11923
** When the ".auth ON" is set, the following authorizer callback is
11924
** invoked.  It always returns SQLITE_OK.
11925
*/
11926
static int shellAuth(
11927
  void *pClientData,
11928
  int op,
11929
  const char *zA1,
11930
  const char *zA2,
11931
  const char *zA3,
11932
  const char *zA4
11933
){
11934
  ShellState *p = (ShellState*)pClientData;
11935
  static const char *azAction[] = { 0,
11936
     "CREATE_INDEX",         "CREATE_TABLE",         "CREATE_TEMP_INDEX",
11937
     "CREATE_TEMP_TABLE",    "CREATE_TEMP_TRIGGER",  "CREATE_TEMP_VIEW",
11938
     "CREATE_TRIGGER",       "CREATE_VIEW",          "DELETE",
11939
     "DROP_INDEX",           "DROP_TABLE",           "DROP_TEMP_INDEX",
11940
     "DROP_TEMP_TABLE",      "DROP_TEMP_TRIGGER",    "DROP_TEMP_VIEW",
11941
     "DROP_TRIGGER",         "DROP_VIEW",            "INSERT",
11942
     "PRAGMA",               "READ",                 "SELECT",
11943
     "TRANSACTION",          "UPDATE",               "ATTACH",
11944
     "DETACH",               "ALTER_TABLE",          "REINDEX",
11945
     "ANALYZE",              "CREATE_VTABLE",        "DROP_VTABLE",
11946
     "FUNCTION",             "SAVEPOINT",            "RECURSIVE"
11947
  };
11948
  int i;
11949
  const char *az[4];
11950
  az[0] = zA1;
11951
  az[1] = zA2;
11952
  az[2] = zA3;
11953
  az[3] = zA4;
11954
  utf8_printf(p->out, "authorizer: %s", azAction[op]);
11955
  for(i=0; i<4; i++){
11956
    raw_printf(p->out, " ");
11957
    if( az[i] ){
11958
      output_c_string(p->out, az[i]);
11959
    }else{
11960
      raw_printf(p->out, "NULL");
11961
    }
11962
  }
11963
  raw_printf(p->out, "\n");
11964
  return SQLITE_OK;
11965
}
11966
#endif
11967
 
11968
/*
11969
** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
11970
**
11971
** This routine converts some CREATE TABLE statements for shadow tables
11972
** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
11973
*/
11974
static void printSchemaLine(FILE *out, const char *z, const char *zTail){
11975
  if( z==0 ) return;
11976
  if( zTail==0 ) return;
11977
  if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
11978
    utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
11979
  }else{
11980
    utf8_printf(out, "%s%s", z, zTail);
11981
  }
11982
}
11983
static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
11984
  char c = z[n];
11985
  z[n] = 0;
11986
  printSchemaLine(out, z, zTail);
11987
  z[n] = c;
11988
}
11989
 
11990
/*
11991
** Return true if string z[] has nothing but whitespace and comments to the
11992
** end of the first line.
11993
*/
11994
static int wsToEol(const char *z){
11995
  int i;
11996
  for(i=0; z[i]; i++){
11997
    if( z[i]=='\n' ) return 1;
11998
    if( IsSpace(z[i]) ) continue;
11999
    if( z[i]=='-' && z[i+1]=='-' ) return 1;
12000
    return 0;
12001
  }
12002
  return 1;
12003
}
12004
 
12005
/*
12006
** Add a new entry to the EXPLAIN QUERY PLAN data
12007
*/
12008
static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
12009
  EQPGraphRow *pNew;
12010
  int nText = strlen30(zText);
12011
  if( p->autoEQPtest ){
12012
    utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
12013
  }
12014
  pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
12015
  if( pNew==0 ) shell_out_of_memory();
12016
  pNew->iEqpId = iEqpId;
12017
  pNew->iParentId = p2;
12018
  memcpy(pNew->zText, zText, nText+1);
12019
  pNew->pNext = 0;
12020
  if( p->sGraph.pLast ){
12021
    p->sGraph.pLast->pNext = pNew;
12022
  }else{
12023
    p->sGraph.pRow = pNew;
12024
  }
12025
  p->sGraph.pLast = pNew;
12026
}
12027
 
12028
/*
12029
** Free and reset the EXPLAIN QUERY PLAN data that has been collected
12030
** in p->sGraph.
12031
*/
12032
static void eqp_reset(ShellState *p){
12033
  EQPGraphRow *pRow, *pNext;
12034
  for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
12035
    pNext = pRow->pNext;
12036
    sqlite3_free(pRow);
12037
  }
12038
  memset(&p->sGraph, 0, sizeof(p->sGraph));
12039
}
12040
 
12041
/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
12042
** pOld, or return the first such line if pOld is NULL
12043
*/
12044
static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
12045
  EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
12046
  while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
12047
  return pRow;
12048
}
12049
 
12050
/* Render a single level of the graph that has iEqpId as its parent.  Called
12051
** recursively to render sublevels.
12052
*/
12053
static void eqp_render_level(ShellState *p, int iEqpId){
12054
  EQPGraphRow *pRow, *pNext;
12055
  int n = strlen30(p->sGraph.zPrefix);
12056
  char *z;
12057
  for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
12058
    pNext = eqp_next_row(p, iEqpId, pRow);
12059
    z = pRow->zText;
12060
    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
12061
                pNext ? "|--" : "`--", z);
12062
    if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
12063
      memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
12064
      eqp_render_level(p, pRow->iEqpId);
12065
      p->sGraph.zPrefix[n] = 0;
12066
    }
12067
  }
12068
}
12069
 
12070
/*
12071
** Display and reset the EXPLAIN QUERY PLAN data
12072
*/
12073
static void eqp_render(ShellState *p){
12074
  EQPGraphRow *pRow = p->sGraph.pRow;
12075
  if( pRow ){
12076
    if( pRow->zText[0]=='-' ){
12077
      if( pRow->pNext==0 ){
12078
        eqp_reset(p);
12079
        return;
12080
      }
12081
      utf8_printf(p->out, "%s\n", pRow->zText+3);
12082
      p->sGraph.pRow = pRow->pNext;
12083
      sqlite3_free(pRow);
12084
    }else{
12085
      utf8_printf(p->out, "QUERY PLAN\n");
12086
    }
12087
    p->sGraph.zPrefix[0] = 0;
12088
    eqp_render_level(p, 0);
12089
    eqp_reset(p);
12090
  }
12091
}
12092
 
12093
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
12094
/*
12095
** Progress handler callback.
12096
*/
12097
static int progress_handler(void *pClientData) {
12098
  ShellState *p = (ShellState*)pClientData;
12099
  p->nProgress++;
12100
  if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
12101
    raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
12102
    if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
12103
    if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
12104
    return 1;
12105
  }
12106
  if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
12107
    raw_printf(p->out, "Progress %u\n", p->nProgress);
12108
  }
12109
  return 0;
12110
}
12111
#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
12112
 
12113
/*
12114
** Print N dashes
12115
*/
12116
static void print_dashes(FILE *out, int N){
12117
  const char zDash[] = "--------------------------------------------------";
12118
  const int nDash = sizeof(zDash) - 1;
12119
  while( N>nDash ){
12120
    fputs(zDash, out);
12121
    N -= nDash;
12122
  }
12123
  raw_printf(out, "%.*s", N, zDash);
12124
}
12125
 
12126
/*
12127
** Print a markdown or table-style row separator using ascii-art
12128
*/
12129
static void print_row_separator(
12130
  ShellState *p,
12131
  int nArg,
12132
  const char *zSep
12133
){
12134
  int i;
12135
  if( nArg>0 ){
12136
    fputs(zSep, p->out);
12137
    print_dashes(p->out, p->actualWidth[0]+2);
12138
    for(i=1; i
12139
      fputs(zSep, p->out);
12140
      print_dashes(p->out, p->actualWidth[i]+2);
12141
    }
12142
    fputs(zSep, p->out);
12143
  }
12144
  fputs("\n", p->out);
12145
}
12146
 
12147
/*
12148
** This is the callback routine that the shell
12149
** invokes for each row of a query result.
12150
*/
12151
static int shell_callback(
12152
  void *pArg,
12153
  int nArg,        /* Number of result columns */
12154
  char **azArg,    /* Text of each result column */
12155
  char **azCol,    /* Column names */
12156
  int *aiType      /* Column types.  Might be NULL */
12157
){
12158
  int i;
12159
  ShellState *p = (ShellState*)pArg;
12160
 
12161
  if( azArg==0 ) return 0;
12162
  switch( p->cMode ){
12163
    case MODE_Line: {
12164
      int w = 5;
12165
      if( azArg==0 ) break;
12166
      for(i=0; i
12167
        int len = strlen30(azCol[i] ? azCol[i] : "");
12168
        if( len>w ) w = len;
12169
      }
12170
      if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
12171
      for(i=0; i
12172
        utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
12173
                azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
12174
      }
12175
      break;
12176
    }
12177
    case MODE_Explain: {
12178
      static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
12179
      if( nArg>ArraySize(aExplainWidth) ){
12180
        nArg = ArraySize(aExplainWidth);
12181
      }
12182
      if( p->cnt++==0 ){
12183
        for(i=0; i
12184
          int w = aExplainWidth[i];
12185
          utf8_width_print(p->out, w, azCol[i]);
12186
          fputs(i==nArg-1 ? "\n" : "  ", p->out);
12187
        }
12188
        for(i=0; i
12189
          int w = aExplainWidth[i];
12190
          print_dashes(p->out, w);
12191
          fputs(i==nArg-1 ? "\n" : "  ", p->out);
12192
        }
12193
      }
12194
      if( azArg==0 ) break;
12195
      for(i=0; i
12196
        int w = aExplainWidth[i];
12197
        if( i==nArg-1 ) w = 0;
12198
        if( azArg[i] && strlenChar(azArg[i])>w ){
12199
          w = strlenChar(azArg[i]);
12200
        }
12201
        if( i==1 && p->aiIndent && p->pStmt ){
12202
          if( p->iIndentnIndent ){
12203
            utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
12204
          }
12205
          p->iIndent++;
12206
        }
12207
        utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
12208
        fputs(i==nArg-1 ? "\n" : "  ", p->out);
12209
      }
12210
      break;
12211
    }
12212
    case MODE_Semi: {   /* .schema and .fullschema output */
12213
      printSchemaLine(p->out, azArg[0], ";\n");
12214
      break;
12215
    }
12216
    case MODE_Pretty: {  /* .schema and .fullschema with --indent */
12217
      char *z;
12218
      int j;
12219
      int nParen = 0;
12220
      char cEnd = 0;
12221
      char c;
12222
      int nLine = 0;
12223
      assert( nArg==1 );
12224
      if( azArg[0]==0 ) break;
12225
      if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
12226
       || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
12227
      ){
12228
        utf8_printf(p->out, "%s;\n", azArg[0]);
12229
        break;
12230
      }
12231
      z = sqlite3_mprintf("%s", azArg[0]);
12232
      j = 0;
12233
      for(i=0; IsSpace(z[i]); i++){}
12234
      for(; (c = z[i])!=0; i++){
12235
        if( IsSpace(c) ){
12236
          if( z[j-1]=='\r' ) z[j-1] = '\n';
12237
          if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
12238
        }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
12239
          j--;
12240
        }
12241
        z[j++] = c;
12242
      }
12243
      while( j>0 && IsSpace(z[j-1]) ){ j--; }
12244
      z[j] = 0;
12245
      if( strlen30(z)>=79 ){
12246
        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
12247
          if( c==cEnd ){
12248
            cEnd = 0;
12249
          }else if( c=='"' || c=='\'' || c=='`' ){
12250
            cEnd = c;
12251
          }else if( c=='[' ){
12252
            cEnd = ']';
12253
          }else if( c=='-' && z[i+1]=='-' ){
12254
            cEnd = '\n';
12255
          }else if( c=='(' ){
12256
            nParen++;
12257
          }else if( c==')' ){
12258
            nParen--;
12259
            if( nLine>0 && nParen==0 && j>0 ){
12260
              printSchemaLineN(p->out, z, j, "\n");
12261
              j = 0;
12262
            }
12263
          }
12264
          z[j++] = c;
12265
          if( nParen==1 && cEnd==0
12266
           && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
12267
          ){
12268
            if( c=='\n' ) j--;
12269
            printSchemaLineN(p->out, z, j, "\n  ");
12270
            j = 0;
12271
            nLine++;
12272
            while( IsSpace(z[i+1]) ){ i++; }
12273
          }
12274
        }
12275
        z[j] = 0;
12276
      }
12277
      printSchemaLine(p->out, z, ";\n");
12278
      sqlite3_free(z);
12279
      break;
12280
    }
12281
    case MODE_List: {
12282
      if( p->cnt++==0 && p->showHeader ){
12283
        for(i=0; i
12284
          utf8_printf(p->out,"%s%s",azCol[i],
12285
                  i==nArg-1 ? p->rowSeparator : p->colSeparator);
12286
        }
12287
      }
12288
      if( azArg==0 ) break;
12289
      for(i=0; i
12290
        char *z = azArg[i];
12291
        if( z==0 ) z = p->nullValue;
12292
        utf8_printf(p->out, "%s", z);
12293
        if( i
12294
          utf8_printf(p->out, "%s", p->colSeparator);
12295
        }else{
12296
          utf8_printf(p->out, "%s", p->rowSeparator);
12297
        }
12298
      }
12299
      break;
12300
    }
12301
    case MODE_Html: {
12302
      if( p->cnt++==0 && p->showHeader ){
12303
        raw_printf(p->out,"
12304
        for(i=0; i
12305
          raw_printf(p->out,"
");
12306
          output_html_string(p->out, azCol[i]);
12307
          raw_printf(p->out,"\n");
12308
        }
12309
        raw_printf(p->out,"
12310
      }
12311
      if( azArg==0 ) break;
12312
      raw_printf(p->out,"
12313
      for(i=0; i
12314
        raw_printf(p->out,"
");
12315
        output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
12316
        raw_printf(p->out,"
12317
      }
12318
      raw_printf(p->out,"
12319
      break;
12320
    }
12321
    case MODE_Tcl: {
12322
      if( p->cnt++==0 && p->showHeader ){
12323
        for(i=0; i
12324
          output_c_string(p->out,azCol[i] ? azCol[i] : "");
12325
          if(iout, "%s", p->colSeparator);
12326
        }
12327
        utf8_printf(p->out, "%s", p->rowSeparator);
12328
      }
12329
      if( azArg==0 ) break;
12330
      for(i=0; i
12331
        output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
12332
        if(iout, "%s", p->colSeparator);
12333
      }
12334
      utf8_printf(p->out, "%s", p->rowSeparator);
12335
      break;
12336
    }
12337
    case MODE_Csv: {
12338
      setBinaryMode(p->out, 1);
12339
      if( p->cnt++==0 && p->showHeader ){
12340
        for(i=0; i
12341
          output_csv(p, azCol[i] ? azCol[i] : "", i
12342
        }
12343
        utf8_printf(p->out, "%s", p->rowSeparator);
12344
      }
12345
      if( nArg>0 ){
12346
        for(i=0; i
12347
          output_csv(p, azArg[i], i
12348
        }
12349
        utf8_printf(p->out, "%s", p->rowSeparator);
12350
      }
12351
      setTextMode(p->out, 1);
12352
      break;
12353
    }
12354
    case MODE_Insert: {
12355
      if( azArg==0 ) break;
12356
      utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
12357
      if( p->showHeader ){
12358
        raw_printf(p->out,"(");
12359
        for(i=0; i
12360
          if( i>0 ) raw_printf(p->out, ",");
12361
          if( quoteChar(azCol[i]) ){
12362
            char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
12363
            utf8_printf(p->out, "%s", z);
12364
            sqlite3_free(z);
12365
          }else{
12366
            raw_printf(p->out, "%s", azCol[i]);
12367
          }
12368
        }
12369
        raw_printf(p->out,")");
12370
      }
12371
      p->cnt++;
12372
      for(i=0; i
12373
        raw_printf(p->out, i>0 ? "," : " VALUES(");
12374
        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12375
          utf8_printf(p->out,"NULL");
12376
        }else if( aiType && aiType[i]==SQLITE_TEXT ){
12377
          if( ShellHasFlag(p, SHFLG_Newlines) ){
12378
            output_quoted_string(p->out, azArg[i]);
12379
          }else{
12380
            output_quoted_escaped_string(p->out, azArg[i]);
12381
          }
12382
        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
12383
          utf8_printf(p->out,"%s", azArg[i]);
12384
        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12385
          char z[50];
12386
          double r = sqlite3_column_double(p->pStmt, i);
12387
          sqlite3_uint64 ur;
12388
          memcpy(&ur,&r,sizeof(r));
12389
          if( ur==0x7ff0000000000000LL ){
12390
            raw_printf(p->out, "1e999");
12391
          }else if( ur==0xfff0000000000000LL ){
12392
            raw_printf(p->out, "-1e999");
12393
          }else{
12394
            sqlite3_snprintf(50,z,"%!.20g", r);
12395
            raw_printf(p->out, "%s", z);
12396
          }
12397
        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12398
          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12399
          int nBlob = sqlite3_column_bytes(p->pStmt, i);
12400
          output_hex_blob(p->out, pBlob, nBlob);
12401
        }else if( isNumber(azArg[i], 0) ){
12402
          utf8_printf(p->out,"%s", azArg[i]);
12403
        }else if( ShellHasFlag(p, SHFLG_Newlines) ){
12404
          output_quoted_string(p->out, azArg[i]);
12405
        }else{
12406
          output_quoted_escaped_string(p->out, azArg[i]);
12407
        }
12408
      }
12409
      raw_printf(p->out,");\n");
12410
      break;
12411
    }
12412
    case MODE_Json: {
12413
      if( azArg==0 ) break;
12414
      if( p->cnt==0 ){
12415
        fputs("[{", p->out);
12416
      }else{
12417
        fputs(",\n{", p->out);
12418
      }
12419
      p->cnt++;
12420
      for(i=0; i
12421
        output_json_string(p->out, azCol[i], -1);
12422
        putc(':', p->out);
12423
        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12424
          fputs("null",p->out);
12425
        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12426
          char z[50];
12427
          double r = sqlite3_column_double(p->pStmt, i);
12428
          sqlite3_uint64 ur;
12429
          memcpy(&ur,&r,sizeof(r));
12430
          if( ur==0x7ff0000000000000LL ){
12431
            raw_printf(p->out, "1e999");
12432
          }else if( ur==0xfff0000000000000LL ){
12433
            raw_printf(p->out, "-1e999");
12434
          }else{
12435
            sqlite3_snprintf(50,z,"%!.20g", r);
12436
            raw_printf(p->out, "%s", z);
12437
          }
12438
        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12439
          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12440
          int nBlob = sqlite3_column_bytes(p->pStmt, i);
12441
          output_json_string(p->out, pBlob, nBlob);
12442
        }else if( aiType && aiType[i]==SQLITE_TEXT ){
12443
          output_json_string(p->out, azArg[i], -1);
12444
        }else{
12445
          utf8_printf(p->out,"%s", azArg[i]);
12446
        }
12447
        if( i
12448
          putc(',', p->out);
12449
        }
12450
      }
12451
      putc('}', p->out);
12452
      break;
12453
    }
12454
    case MODE_Quote: {
12455
      if( azArg==0 ) break;
12456
      if( p->cnt==0 && p->showHeader ){
12457
        for(i=0; i
12458
          if( i>0 ) fputs(p->colSeparator, p->out);
12459
          output_quoted_string(p->out, azCol[i]);
12460
        }
12461
        fputs(p->rowSeparator, p->out);
12462
      }
12463
      p->cnt++;
12464
      for(i=0; i
12465
        if( i>0 ) fputs(p->colSeparator, p->out);
12466
        if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12467
          utf8_printf(p->out,"NULL");
12468
        }else if( aiType && aiType[i]==SQLITE_TEXT ){
12469
          output_quoted_string(p->out, azArg[i]);
12470
        }else if( aiType && aiType[i]==SQLITE_INTEGER ){
12471
          utf8_printf(p->out,"%s", azArg[i]);
12472
        }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12473
          char z[50];
12474
          double r = sqlite3_column_double(p->pStmt, i);
12475
          sqlite3_snprintf(50,z,"%!.20g", r);
12476
          raw_printf(p->out, "%s", z);
12477
        }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12478
          const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12479
          int nBlob = sqlite3_column_bytes(p->pStmt, i);
12480
          output_hex_blob(p->out, pBlob, nBlob);
12481
        }else if( isNumber(azArg[i], 0) ){
12482
          utf8_printf(p->out,"%s", azArg[i]);
12483
        }else{
12484
          output_quoted_string(p->out, azArg[i]);
12485
        }
12486
      }
12487
      fputs(p->rowSeparator, p->out);
12488
      break;
12489
    }
12490
    case MODE_Ascii: {
12491
      if( p->cnt++==0 && p->showHeader ){
12492
        for(i=0; i
12493
          if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
12494
          utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
12495
        }
12496
        utf8_printf(p->out, "%s", p->rowSeparator);
12497
      }
12498
      if( azArg==0 ) break;
12499
      for(i=0; i
12500
        if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
12501
        utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
12502
      }
12503
      utf8_printf(p->out, "%s", p->rowSeparator);
12504
      break;
12505
    }
12506
    case MODE_EQP: {
12507
      eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
12508
      break;
12509
    }
12510
  }
12511
  return 0;
12512
}
12513
 
12514
/*
12515
** This is the callback routine that the SQLite library
12516
** invokes for each row of a query result.
12517
*/
12518
static int callback(void *pArg, int nArg, char **azArg, char **azCol){
12519
  /* since we don't have type info, call the shell_callback with a NULL value */
12520
  return shell_callback(pArg, nArg, azArg, azCol, NULL);
12521
}
12522
 
12523
/*
12524
** This is the callback routine from sqlite3_exec() that appends all
12525
** output onto the end of a ShellText object.
12526
*/
12527
static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
12528
  ShellText *p = (ShellText*)pArg;
12529
  int i;
12530
  UNUSED_PARAMETER(az);
12531
  if( azArg==0 ) return 0;
12532
  if( p->n ) appendText(p, "|", 0);
12533
  for(i=0; i
12534
    if( i ) appendText(p, ",", 0);
12535
    if( azArg[i] ) appendText(p, azArg[i], 0);
12536
  }
12537
  return 0;
12538
}
12539
 
12540
/*
12541
** Generate an appropriate SELFTEST table in the main database.
12542
*/
12543
static void createSelftestTable(ShellState *p){
12544
  char *zErrMsg = 0;
12545
  sqlite3_exec(p->db,
12546
    "SAVEPOINT selftest_init;\n"
12547
    "CREATE TABLE IF NOT EXISTS selftest(\n"
12548
    "  tno INTEGER PRIMARY KEY,\n"   /* Test number */
12549
    "  op TEXT,\n"                   /* Operator:  memo run */
12550
    "  cmd TEXT,\n"                  /* Command text */
12551
    "  ans TEXT\n"                   /* Desired answer */
12552
    ");"
12553
    "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
12554
    "INSERT INTO [_shell$self](rowid,op,cmd)\n"
12555
    "  VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
12556
    "         'memo','Tests generated by --init');\n"
12557
    "INSERT INTO [_shell$self]\n"
12558
    "  SELECT 'run',\n"
12559
    "    'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
12560
                                 "FROM sqlite_schema ORDER BY 2'',224))',\n"
12561
    "    hex(sha3_query('SELECT type,name,tbl_name,sql "
12562
                          "FROM sqlite_schema ORDER BY 2',224));\n"
12563
    "INSERT INTO [_shell$self]\n"
12564
    "  SELECT 'run',"
12565
    "    'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
12566
    "        printf('%w',name) || '\" NOT INDEXED'',224))',\n"
12567
    "    hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
12568
    "  FROM (\n"
12569
    "    SELECT name FROM sqlite_schema\n"
12570
    "     WHERE type='table'\n"
12571
    "       AND name<>'selftest'\n"
12572
    "       AND coalesce(rootpage,0)>0\n"
12573
    "  )\n"
12574
    " ORDER BY name;\n"
12575
    "INSERT INTO [_shell$self]\n"
12576
    "  VALUES('run','PRAGMA integrity_check','ok');\n"
12577
    "INSERT INTO selftest(tno,op,cmd,ans)"
12578
    "  SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
12579
    "DROP TABLE [_shell$self];"
12580
    ,0,0,&zErrMsg);
12581
  if( zErrMsg ){
12582
    utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
12583
    sqlite3_free(zErrMsg);
12584
  }
12585
  sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
12586
}
12587
 
12588
 
12589
/*
12590
** Set the destination table field of the ShellState structure to
12591
** the name of the table given.  Escape any quote characters in the
12592
** table name.
12593
*/
12594
static void set_table_name(ShellState *p, const char *zName){
12595
  int i, n;
12596
  char cQuote;
12597
  char *z;
12598
 
12599
  if( p->zDestTable ){
12600
    free(p->zDestTable);
12601
    p->zDestTable = 0;
12602
  }
12603
  if( zName==0 ) return;
12604
  cQuote = quoteChar(zName);
12605
  n = strlen30(zName);
12606
  if( cQuote ) n += n+2;
12607
  z = p->zDestTable = malloc( n+1 );
12608
  if( z==0 ) shell_out_of_memory();
12609
  n = 0;
12610
  if( cQuote ) z[n++] = cQuote;
12611
  for(i=0; zName[i]; i++){
12612
    z[n++] = zName[i];
12613
    if( zName[i]==cQuote ) z[n++] = cQuote;
12614
  }
12615
  if( cQuote ) z[n++] = cQuote;
12616
  z[n] = 0;
12617
}
12618
 
12619
 
12620
/*
12621
** Execute a query statement that will generate SQL output.  Print
12622
** the result columns, comma-separated, on a line and then add a
12623
** semicolon terminator to the end of that line.
12624
**
12625
** If the number of columns is 1 and that column contains text "--"
12626
** then write the semicolon on a separate line.  That way, if a
12627
** "--" comment occurs at the end of the statement, the comment
12628
** won't consume the semicolon terminator.
12629
*/
12630
static int run_table_dump_query(
12631
  ShellState *p,           /* Query context */
12632
  const char *zSelect      /* SELECT statement to extract content */
12633
){
12634
  sqlite3_stmt *pSelect;
12635
  int rc;
12636
  int nResult;
12637
  int i;
12638
  const char *z;
12639
  rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
12640
  if( rc!=SQLITE_OK || !pSelect ){
12641
    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
12642
                sqlite3_errmsg(p->db));
12643
    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
12644
    return rc;
12645
  }
12646
  rc = sqlite3_step(pSelect);
12647
  nResult = sqlite3_column_count(pSelect);
12648
  while( rc==SQLITE_ROW ){
12649
    z = (const char*)sqlite3_column_text(pSelect, 0);
12650
    utf8_printf(p->out, "%s", z);
12651
    for(i=1; i
12652
      utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
12653
    }
12654
    if( z==0 ) z = "";
12655
    while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
12656
    if( z[0] ){
12657
      raw_printf(p->out, "\n;\n");
12658
    }else{
12659
      raw_printf(p->out, ";\n");
12660
    }
12661
    rc = sqlite3_step(pSelect);
12662
  }
12663
  rc = sqlite3_finalize(pSelect);
12664
  if( rc!=SQLITE_OK ){
12665
    utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
12666
                sqlite3_errmsg(p->db));
12667
    if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
12668
  }
12669
  return rc;
12670
}
12671
 
12672
/*
12673
** Allocate space and save off current error string.
12674
*/
12675
static char *save_err_msg(
12676
  sqlite3 *db            /* Database to query */
12677
){
12678
  int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
12679
  char *zErrMsg = sqlite3_malloc64(nErrMsg);
12680
  if( zErrMsg ){
12681
    memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
12682
  }
12683
  return zErrMsg;
12684
}
12685
 
12686
#ifdef __linux__
12687
/*
12688
** Attempt to display I/O stats on Linux using /proc/PID/io
12689
*/
12690
static void displayLinuxIoStats(FILE *out){
12691
  FILE *in;
12692
  char z[200];
12693
  sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
12694
  in = fopen(z, "rb");
12695
  if( in==0 ) return;
12696
  while( fgets(z, sizeof(z), in)!=0 ){
12697
    static const struct {
12698
      const char *zPattern;
12699
      const char *zDesc;
12700
    } aTrans[] = {
12701
      { "rchar: ",                  "Bytes received by read():" },
12702
      { "wchar: ",                  "Bytes sent to write():"    },
12703
      { "syscr: ",                  "Read() system calls:"      },
12704
      { "syscw: ",                  "Write() system calls:"     },
12705
      { "read_bytes: ",             "Bytes read from storage:"  },
12706
      { "write_bytes: ",            "Bytes written to storage:" },
12707
      { "cancelled_write_bytes: ",  "Cancelled write bytes:"    },
12708
    };
12709
    int i;
12710
    for(i=0; i
12711
      int n = strlen30(aTrans[i].zPattern);
12712
      if( strncmp(aTrans[i].zPattern, z, n)==0 ){
12713
        utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
12714
        break;
12715
      }
12716
    }
12717
  }
12718
  fclose(in);
12719
}
12720
#endif
12721
 
12722
/*
12723
** Display a single line of status using 64-bit values.
12724
*/
12725
static void displayStatLine(
12726
  ShellState *p,            /* The shell context */
12727
  char *zLabel,             /* Label for this one line */
12728
  char *zFormat,            /* Format for the result */
12729
  int iStatusCtrl,          /* Which status to display */
12730
  int bReset                /* True to reset the stats */
12731
){
12732
  sqlite3_int64 iCur = -1;
12733
  sqlite3_int64 iHiwtr = -1;
12734
  int i, nPercent;
12735
  char zLine[200];
12736
  sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
12737
  for(i=0, nPercent=0; zFormat[i]; i++){
12738
    if( zFormat[i]=='%' ) nPercent++;
12739
  }
12740
  if( nPercent>1 ){
12741
    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
12742
  }else{
12743
    sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
12744
  }
12745
  raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
12746
}
12747
 
12748
/*
12749
** Display memory stats.
12750
*/
12751
static int display_stats(
12752
  sqlite3 *db,                /* Database to query */
12753
  ShellState *pArg,           /* Pointer to ShellState */
12754
  int bReset                  /* True to reset the stats */
12755
){
12756
  int iCur;
12757
  int iHiwtr;
12758
  FILE *out;
12759
  if( pArg==0 || pArg->out==0 ) return 0;
12760
  out = pArg->out;
12761
 
12762
  if( pArg->pStmt && pArg->statsOn==2 ){
12763
    int nCol, i, x;
12764
    sqlite3_stmt *pStmt = pArg->pStmt;
12765
    char z[100];
12766
    nCol = sqlite3_column_count(pStmt);
12767
    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
12768
    for(i=0; i
12769
      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
12770
      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
12771
#ifndef SQLITE_OMIT_DECLTYPE
12772
      sqlite3_snprintf(30, z+x, "declared type:");
12773
      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
12774
#endif
12775
#ifdef SQLITE_ENABLE_COLUMN_METADATA
12776
      sqlite3_snprintf(30, z+x, "database name:");
12777
      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
12778
      sqlite3_snprintf(30, z+x, "table name:");
12779
      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
12780
      sqlite3_snprintf(30, z+x, "origin name:");
12781
      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
12782
#endif
12783
    }
12784
  }
12785
 
12786
  if( pArg->statsOn==3 ){
12787
    if( pArg->pStmt ){
12788
      iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
12789
      raw_printf(pArg->out, "VM-steps: %d\n", iCur);
12790
    }
12791
    return 0;
12792
  }
12793
 
12794
  displayStatLine(pArg, "Memory Used:",
12795
     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
12796
  displayStatLine(pArg, "Number of Outstanding Allocations:",
12797
     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
12798
  if( pArg->shellFlgs & SHFLG_Pagecache ){
12799
    displayStatLine(pArg, "Number of Pcache Pages Used:",
12800
       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
12801
  }
12802
  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
12803
     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
12804
  displayStatLine(pArg, "Largest Allocation:",
12805
     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
12806
  displayStatLine(pArg, "Largest Pcache Allocation:",
12807
     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
12808
#ifdef YYTRACKMAXSTACKDEPTH
12809
  displayStatLine(pArg, "Deepest Parser Stack:",
12810
     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
12811
#endif
12812
 
12813
  if( db ){
12814
    if( pArg->shellFlgs & SHFLG_Lookaside ){
12815
      iHiwtr = iCur = -1;
12816
      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
12817
                        &iCur, &iHiwtr, bReset);
12818
      raw_printf(pArg->out,
12819
              "Lookaside Slots Used:                %d (max %d)\n",
12820
              iCur, iHiwtr);
12821
      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
12822
                        &iCur, &iHiwtr, bReset);
12823
      raw_printf(pArg->out, "Successful lookaside attempts:       %d\n",
12824
              iHiwtr);
12825
      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
12826
                        &iCur, &iHiwtr, bReset);
12827
      raw_printf(pArg->out, "Lookaside failures due to size:      %d\n",
12828
              iHiwtr);
12829
      sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
12830
                        &iCur, &iHiwtr, bReset);
12831
      raw_printf(pArg->out, "Lookaside failures due to OOM:       %d\n",
12832
              iHiwtr);
12833
    }
12834
    iHiwtr = iCur = -1;
12835
    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
12836
    raw_printf(pArg->out, "Pager Heap Usage:                    %d bytes\n",
12837
            iCur);
12838
    iHiwtr = iCur = -1;
12839
    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
12840
    raw_printf(pArg->out, "Page cache hits:                     %d\n", iCur);
12841
    iHiwtr = iCur = -1;
12842
    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
12843
    raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
12844
    iHiwtr = iCur = -1;
12845
    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
12846
    raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
12847
    iHiwtr = iCur = -1;
12848
    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
12849
    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
12850
    iHiwtr = iCur = -1;
12851
    sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
12852
    raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
12853
            iCur);
12854
    iHiwtr = iCur = -1;
12855
    sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
12856
    raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
12857
            iCur);
12858
  }
12859
 
12860
  if( pArg->pStmt ){
12861
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
12862
                               bReset);
12863
    raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
12864
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
12865
    raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
12866
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
12867
    raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
12868
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
12869
    raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
12870
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
12871
    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
12872
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
12873
    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
12874
    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
12875
    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
12876
  }
12877
 
12878
#ifdef __linux__
12879
  displayLinuxIoStats(pArg->out);
12880
#endif
12881
 
12882
  /* Do not remove this machine readable comment: extra-stats-output-here */
12883
 
12884
  return 0;
12885
}
12886
 
12887
/*
12888
** Display scan stats.
12889
*/
12890
static void display_scanstats(
12891
  sqlite3 *db,                    /* Database to query */
12892
  ShellState *pArg                /* Pointer to ShellState */
12893
){
12894
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
12895
  UNUSED_PARAMETER(db);
12896
  UNUSED_PARAMETER(pArg);
12897
#else
12898
  int i, k, n, mx;
12899
  raw_printf(pArg->out, "-------- scanstats --------\n");
12900
  mx = 0;
12901
  for(k=0; k<=mx; k++){
12902
    double rEstLoop = 1.0;
12903
    for(i=n=0; 1; i++){
12904
      sqlite3_stmt *p = pArg->pStmt;
12905
      sqlite3_int64 nLoop, nVisit;
12906
      double rEst;
12907
      int iSid;
12908
      const char *zExplain;
12909
      if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
12910
        break;
12911
      }
12912
      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
12913
      if( iSid>mx ) mx = iSid;
12914
      if( iSid!=k ) continue;
12915
      if( n==0 ){
12916
        rEstLoop = (double)nLoop;
12917
        if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
12918
      }
12919
      n++;
12920
      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
12921
      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
12922
      sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
12923
      utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
12924
      rEstLoop *= rEst;
12925
      raw_printf(pArg->out,
12926
          "         nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
12927
          nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
12928
      );
12929
    }
12930
  }
12931
  raw_printf(pArg->out, "---------------------------\n");
12932
#endif
12933
}
12934
 
12935
/*
12936
** Parameter azArray points to a zero-terminated array of strings. zStr
12937
** points to a single nul-terminated string. Return non-zero if zStr
12938
** is equal, according to strcmp(), to any of the strings in the array.
12939
** Otherwise, return zero.
12940
*/
12941
static int str_in_array(const char *zStr, const char **azArray){
12942
  int i;
12943
  for(i=0; azArray[i]; i++){
12944
    if( 0==strcmp(zStr, azArray[i]) ) return 1;
12945
  }
12946
  return 0;
12947
}
12948
 
12949
/*
12950
** If compiled statement pSql appears to be an EXPLAIN statement, allocate
12951
** and populate the ShellState.aiIndent[] array with the number of
12952
** spaces each opcode should be indented before it is output.
12953
**
12954
** The indenting rules are:
12955
**
12956
**     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
12957
**       all opcodes that occur between the p2 jump destination and the opcode
12958
**       itself by 2 spaces.
12959
**
12960
**     * For each "Goto", if the jump destination is earlier in the program
12961
**       and ends on one of:
12962
**          Yield  SeekGt  SeekLt  RowSetRead  Rewind
12963
**       or if the P1 parameter is one instead of zero,
12964
**       then indent all opcodes between the earlier instruction
12965
**       and "Goto" by 2 spaces.
12966
*/
12967
static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
12968
  const char *zSql;               /* The text of the SQL statement */
12969
  const char *z;                  /* Used to check if this is an EXPLAIN */
12970
  int *abYield = 0;               /* True if op is an OP_Yield */
12971
  int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
12972
  int iOp;                        /* Index of operation in p->aiIndent[] */
12973
 
12974
  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
12975
  const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
12976
                            "Rewind", 0 };
12977
  const char *azGoto[] = { "Goto", 0 };
12978
 
12979
  /* Try to figure out if this is really an EXPLAIN statement. If this
12980
  ** cannot be verified, return early.  */
12981
  if( sqlite3_column_count(pSql)!=8 ){
12982
    p->cMode = p->mode;
12983
    return;
12984
  }
12985
  zSql = sqlite3_sql(pSql);
12986
  if( zSql==0 ) return;
12987
  for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
12988
  if( sqlite3_strnicmp(z, "explain", 7) ){
12989
    p->cMode = p->mode;
12990
    return;
12991
  }
12992
 
12993
  for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
12994
    int i;
12995
    int iAddr = sqlite3_column_int(pSql, 0);
12996
    const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
12997
 
12998
    /* Set p2 to the P2 field of the current opcode. Then, assuming that
12999
    ** p2 is an instruction address, set variable p2op to the index of that
13000
    ** instruction in the aiIndent[] array. p2 and p2op may be different if
13001
    ** the current instruction is part of a sub-program generated by an
13002
    ** SQL trigger or foreign key.  */
13003
    int p2 = sqlite3_column_int(pSql, 3);
13004
    int p2op = (p2 + (iOp-iAddr));
13005
 
13006
    /* Grow the p->aiIndent array as required */
13007
    if( iOp>=nAlloc ){
13008
      if( iOp==0 ){
13009
        /* Do further verfication that this is explain output.  Abort if
13010
        ** it is not */
13011
        static const char *explainCols[] = {
13012
           "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
13013
        int jj;
13014
        for(jj=0; jj
13015
          if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
13016
            p->cMode = p->mode;
13017
            sqlite3_reset(pSql);
13018
            return;
13019
          }
13020
        }
13021
      }
13022
      nAlloc += 100;
13023
      p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
13024
      if( p->aiIndent==0 ) shell_out_of_memory();
13025
      abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
13026
      if( abYield==0 ) shell_out_of_memory();
13027
    }
13028
    abYield[iOp] = str_in_array(zOp, azYield);
13029
    p->aiIndent[iOp] = 0;
13030
    p->nIndent = iOp+1;
13031
 
13032
    if( str_in_array(zOp, azNext) ){
13033
      for(i=p2op; iaiIndent[i] += 2;
13034
    }
13035
    if( str_in_array(zOp, azGoto) && p2opnIndent
13036
     && (abYield[p2op] || sqlite3_column_int(pSql, 2))
13037
    ){
13038
      for(i=p2op; iaiIndent[i] += 2;
13039
    }
13040
  }
13041
 
13042
  p->iIndent = 0;
13043
  sqlite3_free(abYield);
13044
  sqlite3_reset(pSql);
13045
}
13046
 
13047
/*
13048
** Free the array allocated by explain_data_prepare().
13049
*/
13050
static void explain_data_delete(ShellState *p){
13051
  sqlite3_free(p->aiIndent);
13052
  p->aiIndent = 0;
13053
  p->nIndent = 0;
13054
  p->iIndent = 0;
13055
}
13056
 
13057
/*
13058
** Disable and restore .wheretrace and .selecttrace settings.
13059
*/
13060
static unsigned int savedSelectTrace;
13061
static unsigned int savedWhereTrace;
13062
static void disable_debug_trace_modes(void){
13063
  unsigned int zero = 0;
13064
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
13065
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
13066
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
13067
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
13068
}
13069
static void restore_debug_trace_modes(void){
13070
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
13071
  sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
13072
}
13073
 
13074
/* Create the TEMP table used to store parameter bindings */
13075
static void bind_table_init(ShellState *p){
13076
  int wrSchema = 0;
13077
  int defensiveMode = 0;
13078
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
13079
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
13080
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
13081
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
13082
  sqlite3_exec(p->db,
13083
    "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
13084
    "  key TEXT PRIMARY KEY,\n"
13085
    "  value ANY\n"
13086
    ") WITHOUT ROWID;",
13087
    0, 0, 0);
13088
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
13089
  sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
13090
}
13091
 
13092
/*
13093
** Bind parameters on a prepared statement.
13094
**
13095
** Parameter bindings are taken from a TEMP table of the form:
13096
**
13097
**    CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
13098
**    WITHOUT ROWID;
13099
**
13100
** No bindings occur if this table does not exist.  The name of the table
13101
** begins with "sqlite_" so that it will not collide with ordinary application
13102
** tables.  The table must be in the TEMP schema.
13103
*/
13104
static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
13105
  int nVar;
13106
  int i;
13107
  int rc;
13108
  sqlite3_stmt *pQ = 0;
13109
 
13110
  nVar = sqlite3_bind_parameter_count(pStmt);
13111
  if( nVar==0 ) return;  /* Nothing to do */
13112
  if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
13113
                                    "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
13114
    return; /* Parameter table does not exist */
13115
  }
13116
  rc = sqlite3_prepare_v2(pArg->db,
13117
          "SELECT value FROM temp.sqlite_parameters"
13118
          " WHERE key=?1", -1, &pQ, 0);
13119
  if( rc || pQ==0 ) return;
13120
  for(i=1; i<=nVar; i++){
13121
    char zNum[30];
13122
    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
13123
    if( zVar==0 ){
13124
      sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
13125
      zVar = zNum;
13126
    }
13127
    sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
13128
    if( sqlite3_step(pQ)==SQLITE_ROW ){
13129
      sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
13130
    }else{
13131
      sqlite3_bind_null(pStmt, i);
13132
    }
13133
    sqlite3_reset(pQ);
13134
  }
13135
  sqlite3_finalize(pQ);
13136
}
13137
 
13138
/*
13139
** UTF8 box-drawing characters.  Imagine box lines like this:
13140
**
13141
**           1
13142
**           |
13143
**       4 --+-- 2
13144
**           |
13145
**           3
13146
**
13147
** Each box characters has between 2 and 4 of the lines leading from
13148
** the center.  The characters are here identified by the numbers of
13149
** their corresponding lines.
13150
*/
13151
#define BOX_24   "\342\224\200"  /* U+2500 --- */
13152
#define BOX_13   "\342\224\202"  /* U+2502  |  */
13153
#define BOX_23   "\342\224\214"  /* U+250c  ,- */
13154
#define BOX_34   "\342\224\220"  /* U+2510 -,  */
13155
#define BOX_12   "\342\224\224"  /* U+2514  '- */
13156
#define BOX_14   "\342\224\230"  /* U+2518 -'  */
13157
#define BOX_123  "\342\224\234"  /* U+251c  |- */
13158
#define BOX_134  "\342\224\244"  /* U+2524 -|  */
13159
#define BOX_234  "\342\224\254"  /* U+252c -,- */
13160
#define BOX_124  "\342\224\264"  /* U+2534 -'- */
13161
#define BOX_1234 "\342\224\274"  /* U+253c -|- */
13162
 
13163
/* Draw horizontal line N characters long using unicode box
13164
** characters
13165
*/
13166
static void print_box_line(FILE *out, int N){
13167
  const char zDash[] =
13168
      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
13169
      BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
13170
  const int nDash = sizeof(zDash) - 1;
13171
  N *= 3;
13172
  while( N>nDash ){
13173
    utf8_printf(out, zDash);
13174
    N -= nDash;
13175
  }
13176
  utf8_printf(out, "%.*s", N, zDash);
13177
}
13178
 
13179
/*
13180
** Draw a horizontal separator for a MODE_Box table.
13181
*/
13182
static void print_box_row_separator(
13183
  ShellState *p,
13184
  int nArg,
13185
  const char *zSep1,
13186
  const char *zSep2,
13187
  const char *zSep3
13188
){
13189
  int i;
13190
  if( nArg>0 ){
13191
    utf8_printf(p->out, "%s", zSep1);
13192
    print_box_line(p->out, p->actualWidth[0]+2);
13193
    for(i=1; i
13194
      utf8_printf(p->out, "%s", zSep2);
13195
      print_box_line(p->out, p->actualWidth[i]+2);
13196
    }
13197
    utf8_printf(p->out, "%s", zSep3);
13198
  }
13199
  fputs("\n", p->out);
13200
}
13201
 
13202
 
13203
 
13204
/*
13205
** Run a prepared statement and output the result in one of the
13206
** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
13207
** or MODE_Box.
13208
**
13209
** This is different from ordinary exec_prepared_stmt() in that
13210
** it has to run the entire query and gather the results into memory
13211
** first, in order to determine column widths, before providing
13212
** any output.
13213
*/
13214
static void exec_prepared_stmt_columnar(
13215
  ShellState *p,                        /* Pointer to ShellState */
13216
  sqlite3_stmt *pStmt                   /* Statment to run */
13217
){
13218
  sqlite3_int64 nRow = 0;
13219
  int nColumn = 0;
13220
  char **azData = 0;
13221
  sqlite3_int64 nAlloc = 0;
13222
  const char *z;
13223
  int rc;
13224
  sqlite3_int64 i, nData;
13225
  int j, nTotal, w, n;
13226
  const char *colSep = 0;
13227
  const char *rowSep = 0;
13228
 
13229
  rc = sqlite3_step(pStmt);
13230
  if( rc!=SQLITE_ROW ) return;
13231
  nColumn = sqlite3_column_count(pStmt);
13232
  nAlloc = nColumn*4;
13233
  if( nAlloc<=0 ) nAlloc = 1;
13234
  azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
13235
  if( azData==0 ) shell_out_of_memory();
13236
  for(i=0; i
13237
    azData[i] = strdup(sqlite3_column_name(pStmt,i));
13238
  }
13239
  do{
13240
    if( (nRow+2)*nColumn >= nAlloc ){
13241
      nAlloc *= 2;
13242
      azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
13243
      if( azData==0 ) shell_out_of_memory();
13244
    }
13245
    nRow++;
13246
    for(i=0; i
13247
      z = (const char*)sqlite3_column_text(pStmt,i);
13248
      azData[nRow*nColumn + i] = z ? strdup(z) : 0;
13249
    }
13250
  }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
13251
  if( nColumn>p->nWidth ){
13252
    p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
13253
    if( p->colWidth==0 ) shell_out_of_memory();
13254
    for(i=p->nWidth; icolWidth[i] = 0;
13255
    p->nWidth = nColumn;
13256
    p->actualWidth = &p->colWidth[nColumn];
13257
  }
13258
  memset(p->actualWidth, 0, nColumn*sizeof(int));
13259
  for(i=0; i
13260
    w = p->colWidth[i];
13261
    if( w<0 ) w = -w;
13262
    p->actualWidth[i] = w;
13263
  }
13264
  nTotal = nColumn*(nRow+1);
13265
  for(i=0; i
13266
    z = azData[i];
13267
    if( z==0 ) z = p->nullValue;
13268
    n = strlenChar(z);
13269
    j = i%nColumn;
13270
    if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
13271
  }
13272
  if( seenInterrupt ) goto columnar_end;
13273
  if( nColumn==0 ) goto columnar_end;
13274
  switch( p->cMode ){
13275
    case MODE_Column: {
13276
      colSep = "  ";
13277
      rowSep = "\n";
13278
      if( p->showHeader ){
13279
        for(i=0; i
13280
          w = p->actualWidth[i];
13281
          if( p->colWidth[i]<0 ) w = -w;
13282
          utf8_width_print(p->out, w, azData[i]);
13283
          fputs(i==nColumn-1?"\n":"  ", p->out);
13284
        }
13285
        for(i=0; i
13286
          print_dashes(p->out, p->actualWidth[i]);
13287
          fputs(i==nColumn-1?"\n":"  ", p->out);
13288
        }
13289
      }
13290
      break;
13291
    }
13292
    case MODE_Table: {
13293
      colSep = " | ";
13294
      rowSep = " |\n";
13295
      print_row_separator(p, nColumn, "+");
13296
      fputs("| ", p->out);
13297
      for(i=0; i
13298
        w = p->actualWidth[i];
13299
        n = strlenChar(azData[i]);
13300
        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
13301
        fputs(i==nColumn-1?" |\n":" | ", p->out);
13302
      }
13303
      print_row_separator(p, nColumn, "+");
13304
      break;
13305
    }
13306
    case MODE_Markdown: {
13307
      colSep = " | ";
13308
      rowSep = " |\n";
13309
      fputs("| ", p->out);
13310
      for(i=0; i
13311
        w = p->actualWidth[i];
13312
        n = strlenChar(azData[i]);
13313
        utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
13314
        fputs(i==nColumn-1?" |\n":" | ", p->out);
13315
      }
13316
      print_row_separator(p, nColumn, "|");
13317
      break;
13318
    }
13319
    case MODE_Box: {
13320
      colSep = " " BOX_13 " ";
13321
      rowSep = " " BOX_13 "\n";
13322
      print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
13323
      utf8_printf(p->out, BOX_13 " ");
13324
      for(i=0; i
13325
        w = p->actualWidth[i];
13326
        n = strlenChar(azData[i]);
13327
        utf8_printf(p->out, "%*s%s%*s%s",
13328
            (w-n)/2, "", azData[i], (w-n+1)/2, "",
13329
            i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
13330
      }
13331
      print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
13332
      break;
13333
    }
13334
  }
13335
  for(i=nColumn, j=0; i
13336
    if( j==0 && p->cMode!=MODE_Column ){
13337
      utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
13338
    }
13339
    z = azData[i];
13340
    if( z==0 ) z = p->nullValue;
13341
    w = p->actualWidth[j];
13342
    if( p->colWidth[j]<0 ) w = -w;
13343
    utf8_width_print(p->out, w, z);
13344
    if( j==nColumn-1 ){
13345
      utf8_printf(p->out, "%s", rowSep);
13346
      j = -1;
13347
      if( seenInterrupt ) goto columnar_end;
13348
    }else{
13349
      utf8_printf(p->out, "%s", colSep);
13350
    }
13351
  }
13352
  if( p->cMode==MODE_Table ){
13353
    print_row_separator(p, nColumn, "+");
13354
  }else if( p->cMode==MODE_Box ){
13355
    print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
13356
  }
13357
columnar_end:
13358
  if( seenInterrupt ){
13359
    utf8_printf(p->out, "Interrupt\n");
13360
  }
13361
  nData = (nRow+1)*nColumn;
13362
  for(i=0; i
13363
  sqlite3_free(azData);
13364
}
13365
 
13366
/*
13367
** Run a prepared statement
13368
*/
13369
static void exec_prepared_stmt(
13370
  ShellState *pArg,                                /* Pointer to ShellState */
13371
  sqlite3_stmt *pStmt                              /* Statment to run */
13372
){
13373
  int rc;
13374
 
13375
  if( pArg->cMode==MODE_Column
13376
   || pArg->cMode==MODE_Table
13377
   || pArg->cMode==MODE_Box
13378
   || pArg->cMode==MODE_Markdown
13379
  ){
13380
    exec_prepared_stmt_columnar(pArg, pStmt);
13381
    return;
13382
  }
13383
 
13384
  /* perform the first step.  this will tell us if we
13385
  ** have a result set or not and how wide it is.
13386
  */
13387
  rc = sqlite3_step(pStmt);
13388
  /* if we have a result set... */
13389
  if( SQLITE_ROW == rc ){
13390
    /* allocate space for col name ptr, value ptr, and type */
13391
    int nCol = sqlite3_column_count(pStmt);
13392
    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
13393
    if( !pData ){
13394
      rc = SQLITE_NOMEM;
13395
    }else{
13396
      char **azCols = (char **)pData;      /* Names of result columns */
13397
      char **azVals = &azCols[nCol];       /* Results */
13398
      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
13399
      int i, x;
13400
      assert(sizeof(int) <= sizeof(char *));
13401
      /* save off ptrs to column names */
13402
      for(i=0; i
13403
        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
13404
      }
13405
      do{
13406
        /* extract the data and data types */
13407
        for(i=0; i
13408
          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
13409
          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
13410
            azVals[i] = "";
13411
          }else{
13412
            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
13413
          }
13414
          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
13415
            rc = SQLITE_NOMEM;
13416
            break; /* from for */
13417
          }
13418
        } /* end for */
13419
 
13420
        /* if data and types extracted successfully... */
13421
        if( SQLITE_ROW == rc ){
13422
          /* call the supplied callback with the result row data */
13423
          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
13424
            rc = SQLITE_ABORT;
13425
          }else{
13426
            rc = sqlite3_step(pStmt);
13427
          }
13428
        }
13429
      } while( SQLITE_ROW == rc );
13430
      sqlite3_free(pData);
13431
      if( pArg->cMode==MODE_Json ){
13432
        fputs("]\n", pArg->out);
13433
      }
13434
    }
13435
  }
13436
}
13437
 
13438
#ifndef SQLITE_OMIT_VIRTUALTABLE
13439
/*
13440
** This function is called to process SQL if the previous shell command
13441
** was ".expert". It passes the SQL in the second argument directly to
13442
** the sqlite3expert object.
13443
**
13444
** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
13445
** code. In this case, (*pzErr) may be set to point to a buffer containing
13446
** an English language error message. It is the responsibility of the
13447
** caller to eventually free this buffer using sqlite3_free().
13448
*/
13449
static int expertHandleSQL(
13450
  ShellState *pState,
13451
  const char *zSql,
13452
  char **pzErr
13453
){
13454
  assert( pState->expert.pExpert );
13455
  assert( pzErr==0 || *pzErr==0 );
13456
  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
13457
}
13458
 
13459
/*
13460
** This function is called either to silently clean up the object
13461
** created by the ".expert" command (if bCancel==1), or to generate a
13462
** report from it and then clean it up (if bCancel==0).
13463
**
13464
** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
13465
** code. In this case, (*pzErr) may be set to point to a buffer containing
13466
** an English language error message. It is the responsibility of the
13467
** caller to eventually free this buffer using sqlite3_free().
13468
*/
13469
static int expertFinish(
13470
  ShellState *pState,
13471
  int bCancel,
13472
  char **pzErr
13473
){
13474
  int rc = SQLITE_OK;
13475
  sqlite3expert *p = pState->expert.pExpert;
13476
  assert( p );
13477
  assert( bCancel || pzErr==0 || *pzErr==0 );
13478
  if( bCancel==0 ){
13479
    FILE *out = pState->out;
13480
    int bVerbose = pState->expert.bVerbose;
13481
 
13482
    rc = sqlite3_expert_analyze(p, pzErr);
13483
    if( rc==SQLITE_OK ){
13484
      int nQuery = sqlite3_expert_count(p);
13485
      int i;
13486
 
13487
      if( bVerbose ){
13488
        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
13489
        raw_printf(out, "-- Candidates -----------------------------\n");
13490
        raw_printf(out, "%s\n", zCand);
13491
      }
13492
      for(i=0; i
13493
        const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
13494
        const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
13495
        const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
13496
        if( zIdx==0 ) zIdx = "(no new indexes)\n";
13497
        if( bVerbose ){
13498
          raw_printf(out, "-- Query %d --------------------------------\n",i+1);
13499
          raw_printf(out, "%s\n\n", zSql);
13500
        }
13501
        raw_printf(out, "%s\n", zIdx);
13502
        raw_printf(out, "%s\n", zEQP);
13503
      }
13504
    }
13505
  }
13506
  sqlite3_expert_destroy(p);
13507
  pState->expert.pExpert = 0;
13508
  return rc;
13509
}
13510
 
13511
/*
13512
** Implementation of ".expert" dot command.
13513
*/
13514
static int expertDotCommand(
13515
  ShellState *pState,             /* Current shell tool state */
13516
  char **azArg,                   /* Array of arguments passed to dot command */
13517
  int nArg                        /* Number of entries in azArg[] */
13518
){
13519
  int rc = SQLITE_OK;
13520
  char *zErr = 0;
13521
  int i;
13522
  int iSample = 0;
13523
 
13524
  assert( pState->expert.pExpert==0 );
13525
  memset(&pState->expert, 0, sizeof(ExpertInfo));
13526
 
13527
  for(i=1; rc==SQLITE_OK && i
13528
    char *z = azArg[i];
13529
    int n;
13530
    if( z[0]=='-' && z[1]=='-' ) z++;
13531
    n = strlen30(z);
13532
    if( n>=2 && 0==strncmp(z, "-verbose", n) ){
13533
      pState->expert.bVerbose = 1;
13534
    }
13535
    else if( n>=2 && 0==strncmp(z, "-sample", n) ){
13536
      if( i==(nArg-1) ){
13537
        raw_printf(stderr, "option requires an argument: %s\n", z);
13538
        rc = SQLITE_ERROR;
13539
      }else{
13540
        iSample = (int)integerValue(azArg[++i]);
13541
        if( iSample<0 || iSample>100 ){
13542
          raw_printf(stderr, "value out of range: %s\n", azArg[i]);
13543
          rc = SQLITE_ERROR;
13544
        }
13545
      }
13546
    }
13547
    else{
13548
      raw_printf(stderr, "unknown option: %s\n", z);
13549
      rc = SQLITE_ERROR;
13550
    }
13551
  }
13552
 
13553
  if( rc==SQLITE_OK ){
13554
    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
13555
    if( pState->expert.pExpert==0 ){
13556
      raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
13557
      rc = SQLITE_ERROR;
13558
    }else{
13559
      sqlite3_expert_config(
13560
          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
13561
      );
13562
    }
13563
  }
13564
 
13565
  return rc;
13566
}
13567
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
13568
 
13569
/*
13570
** Execute a statement or set of statements.  Print
13571
** any result rows/columns depending on the current mode
13572
** set via the supplied callback.
13573
**
13574
** This is very similar to SQLite's built-in sqlite3_exec()
13575
** function except it takes a slightly different callback
13576
** and callback data argument.
13577
*/
13578
static int shell_exec(
13579
  ShellState *pArg,                         /* Pointer to ShellState */
13580
  const char *zSql,                         /* SQL to be evaluated */
13581
  char **pzErrMsg                           /* Error msg written here */
13582
){
13583
  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
13584
  int rc = SQLITE_OK;             /* Return Code */
13585
  int rc2;
13586
  const char *zLeftover;          /* Tail of unprocessed SQL */
13587
  sqlite3 *db = pArg->db;
13588
 
13589
  if( pzErrMsg ){
13590
    *pzErrMsg = NULL;
13591
  }
13592
 
13593
#ifndef SQLITE_OMIT_VIRTUALTABLE
13594
  if( pArg->expert.pExpert ){
13595
    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
13596
    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
13597
  }
13598
#endif
13599
 
13600
  while( zSql[0] && (SQLITE_OK == rc) ){
13601
    static const char *zStmtSql;
13602
    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
13603
    if( SQLITE_OK != rc ){
13604
      if( pzErrMsg ){
13605
        *pzErrMsg = save_err_msg(db);
13606
      }
13607
    }else{
13608
      if( !pStmt ){
13609
        /* this happens for a comment or white-space */
13610
        zSql = zLeftover;
13611
        while( IsSpace(zSql[0]) ) zSql++;
13612
        continue;
13613
      }
13614
      zStmtSql = sqlite3_sql(pStmt);
13615
      if( zStmtSql==0 ) zStmtSql = "";
13616
      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
13617
 
13618
      /* save off the prepared statment handle and reset row count */
13619
      if( pArg ){
13620
        pArg->pStmt = pStmt;
13621
        pArg->cnt = 0;
13622
      }
13623
 
13624
      /* echo the sql statement if echo on */
13625
      if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
13626
        utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
13627
      }
13628
 
13629
      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
13630
      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
13631
        sqlite3_stmt *pExplain;
13632
        char *zEQP;
13633
        int triggerEQP = 0;
13634
        disable_debug_trace_modes();
13635
        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
13636
        if( pArg->autoEQP>=AUTOEQP_trigger ){
13637
          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
13638
        }
13639
        zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
13640
        rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13641
        if( rc==SQLITE_OK ){
13642
          while( sqlite3_step(pExplain)==SQLITE_ROW ){
13643
            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
13644
            int iEqpId = sqlite3_column_int(pExplain, 0);
13645
            int iParentId = sqlite3_column_int(pExplain, 1);
13646
            if( zEQPLine==0 ) zEQPLine = "";
13647
            if( zEQPLine[0]=='-' ) eqp_render(pArg);
13648
            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
13649
          }
13650
          eqp_render(pArg);
13651
        }
13652
        sqlite3_finalize(pExplain);
13653
        sqlite3_free(zEQP);
13654
        if( pArg->autoEQP>=AUTOEQP_full ){
13655
          /* Also do an EXPLAIN for ".eqp full" mode */
13656
          zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
13657
          rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13658
          if( rc==SQLITE_OK ){
13659
            pArg->cMode = MODE_Explain;
13660
            explain_data_prepare(pArg, pExplain);
13661
            exec_prepared_stmt(pArg, pExplain);
13662
            explain_data_delete(pArg);
13663
          }
13664
          sqlite3_finalize(pExplain);
13665
          sqlite3_free(zEQP);
13666
        }
13667
        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
13668
          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
13669
          /* Reprepare pStmt before reactiving trace modes */
13670
          sqlite3_finalize(pStmt);
13671
          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
13672
          if( pArg ) pArg->pStmt = pStmt;
13673
        }
13674
        restore_debug_trace_modes();
13675
      }
13676
 
13677
      if( pArg ){
13678
        pArg->cMode = pArg->mode;
13679
        if( pArg->autoExplain ){
13680
          if( sqlite3_stmt_isexplain(pStmt)==1 ){
13681
            pArg->cMode = MODE_Explain;
13682
          }
13683
          if( sqlite3_stmt_isexplain(pStmt)==2 ){
13684
            pArg->cMode = MODE_EQP;
13685
          }
13686
        }
13687
 
13688
        /* If the shell is currently in ".explain" mode, gather the extra
13689
        ** data required to add indents to the output.*/
13690
        if( pArg->cMode==MODE_Explain ){
13691
          explain_data_prepare(pArg, pStmt);
13692
        }
13693
      }
13694
 
13695
      bind_prepared_stmt(pArg, pStmt);
13696
      exec_prepared_stmt(pArg, pStmt);
13697
      explain_data_delete(pArg);
13698
      eqp_render(pArg);
13699
 
13700
      /* print usage stats if stats on */
13701
      if( pArg && pArg->statsOn ){
13702
        display_stats(db, pArg, 0);
13703
      }
13704
 
13705
      /* print loop-counters if required */
13706
      if( pArg && pArg->scanstatsOn ){
13707
        display_scanstats(db, pArg);
13708
      }
13709
 
13710
      /* Finalize the statement just executed. If this fails, save a
13711
      ** copy of the error message. Otherwise, set zSql to point to the
13712
      ** next statement to execute. */
13713
      rc2 = sqlite3_finalize(pStmt);
13714
      if( rc!=SQLITE_NOMEM ) rc = rc2;
13715
      if( rc==SQLITE_OK ){
13716
        zSql = zLeftover;
13717
        while( IsSpace(zSql[0]) ) zSql++;
13718
      }else if( pzErrMsg ){
13719
        *pzErrMsg = save_err_msg(db);
13720
      }
13721
 
13722
      /* clear saved stmt handle */
13723
      if( pArg ){
13724
        pArg->pStmt = NULL;
13725
      }
13726
    }
13727
  } /* end while */
13728
 
13729
  return rc;
13730
}
13731
 
13732
/*
13733
** Release memory previously allocated by tableColumnList().
13734
*/
13735
static void freeColumnList(char **azCol){
13736
  int i;
13737
  for(i=1; azCol[i]; i++){
13738
    sqlite3_free(azCol[i]);
13739
  }
13740
  /* azCol[0] is a static string */
13741
  sqlite3_free(azCol);
13742
}
13743
 
13744
/*
13745
** Return a list of pointers to strings which are the names of all
13746
** columns in table zTab.   The memory to hold the names is dynamically
13747
** allocated and must be released by the caller using a subsequent call
13748
** to freeColumnList().
13749
**
13750
** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
13751
** value that needs to be preserved, then azCol[0] is filled in with the
13752
** name of the rowid column.
13753
**
13754
** The first regular column in the table is azCol[1].  The list is terminated
13755
** by an entry with azCol[i]==0.
13756
*/
13757
static char **tableColumnList(ShellState *p, const char *zTab){
13758
  char **azCol = 0;
13759
  sqlite3_stmt *pStmt;
13760
  char *zSql;
13761
  int nCol = 0;
13762
  int nAlloc = 0;
13763
  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
13764
  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
13765
  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
13766
  int rc;
13767
 
13768
  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
13769
  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13770
  sqlite3_free(zSql);
13771
  if( rc ) return 0;
13772
  while( sqlite3_step(pStmt)==SQLITE_ROW ){
13773
    if( nCol>=nAlloc-2 ){
13774
      nAlloc = nAlloc*2 + nCol + 10;
13775
      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
13776
      if( azCol==0 ) shell_out_of_memory();
13777
    }
13778
    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
13779
    if( sqlite3_column_int(pStmt, 5) ){
13780
      nPK++;
13781
      if( nPK==1
13782
       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
13783
                          "INTEGER")==0
13784
      ){
13785
        isIPK = 1;
13786
      }else{
13787
        isIPK = 0;
13788
      }
13789
    }
13790
  }
13791
  sqlite3_finalize(pStmt);
13792
  if( azCol==0 ) return 0;
13793
  azCol[0] = 0;
13794
  azCol[nCol+1] = 0;
13795
 
13796
  /* The decision of whether or not a rowid really needs to be preserved
13797
  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
13798
  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
13799
  ** rowids on tables where the rowid is inaccessible because there are other
13800
  ** columns in the table named "rowid", "_rowid_", and "oid".
13801
  */
13802
  if( preserveRowid && isIPK ){
13803
    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
13804
    ** might be an alise for the ROWID.  But it might also be a WITHOUT ROWID
13805
    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
13806
    ** ROWID aliases.  To distinguish these cases, check to see if
13807
    ** there is a "pk" entry in "PRAGMA index_list".  There will be
13808
    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
13809
    */
13810
    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
13811
                           " WHERE origin='pk'", zTab);
13812
    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13813
    sqlite3_free(zSql);
13814
    if( rc ){
13815
      freeColumnList(azCol);
13816
      return 0;
13817
    }
13818
    rc = sqlite3_step(pStmt);
13819
    sqlite3_finalize(pStmt);
13820
    preserveRowid = rc==SQLITE_ROW;
13821
  }
13822
  if( preserveRowid ){
13823
    /* Only preserve the rowid if we can find a name to use for the
13824
    ** rowid */
13825
    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
13826
    int i, j;
13827
    for(j=0; j<3; j++){
13828
      for(i=1; i<=nCol; i++){
13829
        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
13830
      }
13831
      if( i>nCol ){
13832
        /* At this point, we know that azRowid[j] is not the name of any
13833
        ** ordinary column in the table.  Verify that azRowid[j] is a valid
13834
        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
13835
        ** tables will fail this last check */
13836
        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
13837
        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
13838
        break;
13839
      }
13840
    }
13841
  }
13842
  return azCol;
13843
}
13844
 
13845
/*
13846
** Toggle the reverse_unordered_selects setting.
13847
*/
13848
static void toggleSelectOrder(sqlite3 *db){
13849
  sqlite3_stmt *pStmt = 0;
13850
  int iSetting = 0;
13851
  char zStmt[100];
13852
  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
13853
  if( sqlite3_step(pStmt)==SQLITE_ROW ){
13854
    iSetting = sqlite3_column_int(pStmt, 0);
13855
  }
13856
  sqlite3_finalize(pStmt);
13857
  sqlite3_snprintf(sizeof(zStmt), zStmt,
13858
       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
13859
  sqlite3_exec(db, zStmt, 0, 0, 0);
13860
}
13861
 
13862
/*
13863
** This is a different callback routine used for dumping the database.
13864
** Each row received by this callback consists of a table name,
13865
** the table type ("index" or "table") and SQL to create the table.
13866
** This routine should print text sufficient to recreate the table.
13867
*/
13868
static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
13869
  int rc;
13870
  const char *zTable;
13871
  const char *zType;
13872
  const char *zSql;
13873
  ShellState *p = (ShellState *)pArg;
13874
  int dataOnly;
13875
  int noSys;
13876
 
13877
  UNUSED_PARAMETER(azNotUsed);
13878
  if( nArg!=3 || azArg==0 ) return 0;
13879
  zTable = azArg[0];
13880
  zType = azArg[1];
13881
  zSql = azArg[2];
13882
  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
13883
  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
13884
 
13885
  if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
13886
    if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
13887
  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
13888
    if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
13889
  }else if( strncmp(zTable, "sqlite_", 7)==0 ){
13890
    return 0;
13891
  }else if( dataOnly ){
13892
    /* no-op */
13893
  }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
13894
    char *zIns;
13895
    if( !p->writableSchema ){
13896
      raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
13897
      p->writableSchema = 1;
13898
    }
13899
    zIns = sqlite3_mprintf(
13900
       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
13901
       "VALUES('table','%q','%q',0,'%q');",
13902
       zTable, zTable, zSql);
13903
    utf8_printf(p->out, "%s\n", zIns);
13904
    sqlite3_free(zIns);
13905
    return 0;
13906
  }else{
13907
    printSchemaLine(p->out, zSql, ";\n");
13908
  }
13909
 
13910
  if( strcmp(zType, "table")==0 ){
13911
    ShellText sSelect;
13912
    ShellText sTable;
13913
    char **azCol;
13914
    int i;
13915
    char *savedDestTable;
13916
    int savedMode;
13917
 
13918
    azCol = tableColumnList(p, zTable);
13919
    if( azCol==0 ){
13920
      p->nErr++;
13921
      return 0;
13922
    }
13923
 
13924
    /* Always quote the table name, even if it appears to be pure ascii,
13925
    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
13926
    initText(&sTable);
13927
    appendText(&sTable, zTable, quoteChar(zTable));
13928
    /* If preserving the rowid, add a column list after the table name.
13929
    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
13930
    ** instead of the usual "INSERT INTO tab VALUES(...)".
13931
    */
13932
    if( azCol[0] ){
13933
      appendText(&sTable, "(", 0);
13934
      appendText(&sTable, azCol[0], 0);
13935
      for(i=1; azCol[i]; i++){
13936
        appendText(&sTable, ",", 0);
13937
        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
13938
      }
13939
      appendText(&sTable, ")", 0);
13940
    }
13941
 
13942
    /* Build an appropriate SELECT statement */
13943
    initText(&sSelect);
13944
    appendText(&sSelect, "SELECT ", 0);
13945
    if( azCol[0] ){
13946
      appendText(&sSelect, azCol[0], 0);
13947
      appendText(&sSelect, ",", 0);
13948
    }
13949
    for(i=1; azCol[i]; i++){
13950
      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
13951
      if( azCol[i+1] ){
13952
        appendText(&sSelect, ",", 0);
13953
      }
13954
    }
13955
    freeColumnList(azCol);
13956
    appendText(&sSelect, " FROM ", 0);
13957
    appendText(&sSelect, zTable, quoteChar(zTable));
13958
 
13959
    savedDestTable = p->zDestTable;
13960
    savedMode = p->mode;
13961
    p->zDestTable = sTable.z;
13962
    p->mode = p->cMode = MODE_Insert;
13963
    rc = shell_exec(p, sSelect.z, 0);
13964
    if( (rc&0xff)==SQLITE_CORRUPT ){
13965
      raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
13966
      toggleSelectOrder(p->db);
13967
      shell_exec(p, sSelect.z, 0);
13968
      toggleSelectOrder(p->db);
13969
    }
13970
    p->zDestTable = savedDestTable;
13971
    p->mode = savedMode;
13972
    freeText(&sTable);
13973
    freeText(&sSelect);
13974
    if( rc ) p->nErr++;
13975
  }
13976
  return 0;
13977
}
13978
 
13979
/*
13980
** Run zQuery.  Use dump_callback() as the callback routine so that
13981
** the contents of the query are output as SQL statements.
13982
**
13983
** If we get a SQLITE_CORRUPT error, rerun the query after appending
13984
** "ORDER BY rowid DESC" to the end.
13985
*/
13986
static int run_schema_dump_query(
13987
  ShellState *p,
13988
  const char *zQuery
13989
){
13990
  int rc;
13991
  char *zErr = 0;
13992
  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
13993
  if( rc==SQLITE_CORRUPT ){
13994
    char *zQ2;
13995
    int len = strlen30(zQuery);
13996
    raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
13997
    if( zErr ){
13998
      utf8_printf(p->out, "/****** %s ******/\n", zErr);
13999
      sqlite3_free(zErr);
14000
      zErr = 0;
14001
    }
14002
    zQ2 = malloc( len+100 );
14003
    if( zQ2==0 ) return rc;
14004
    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
14005
    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
14006
    if( rc ){
14007
      utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
14008
    }else{
14009
      rc = SQLITE_CORRUPT;
14010
    }
14011
    sqlite3_free(zErr);
14012
    free(zQ2);
14013
  }
14014
  return rc;
14015
}
14016
 
14017
/*
14018
** Text of help messages.
14019
**
14020
** The help text for each individual command begins with a line that starts
14021
** with ".".  Subsequent lines are supplimental information.
14022
**
14023
** There must be two or more spaces between the end of the command and the
14024
** start of the description of what that command does.
14025
*/
14026
static const char *(azHelp[]) = {
14027
#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
14028
  ".archive ...             Manage SQL archives",
14029
  "   Each command must have exactly one of the following options:",
14030
  "     -c, --create               Create a new archive",
14031
  "     -u, --update               Add or update files with changed mtime",
14032
  "     -i, --insert               Like -u but always add even if unchanged",
14033
  "     -t, --list                 List contents of archive",
14034
  "     -x, --extract              Extract files from archive",
14035
  "   Optional arguments:",
14036
  "     -v, --verbose              Print each filename as it is processed",
14037
  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
14038
  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
14039
  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
14040
  "     -n, --dryrun               Show the SQL that would have occurred",
14041
  "   Examples:",
14042
  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
14043
  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
14044
  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
14045
  "   See also:",
14046
  "      http://sqlite.org/cli.html#sqlar_archive_support",
14047
#endif
14048
#ifndef SQLITE_OMIT_AUTHORIZATION
14049
  ".auth ON|OFF             Show authorizer callbacks",
14050
#endif
14051
  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
14052
  "       --append            Use the appendvfs",
14053
  "       --async             Write to FILE without journal and fsync()",
14054
  ".bail on|off             Stop after hitting an error.  Default OFF",
14055
  ".binary on|off           Turn binary output on or off.  Default OFF",
14056
  ".cd DIRECTORY            Change the working directory to DIRECTORY",
14057
  ".changes on|off          Show number of rows changed by SQL",
14058
  ".check GLOB              Fail if output since .testcase does not match",
14059
  ".clone NEWDB             Clone data into NEWDB from the existing database",
14060
  ".databases               List names and files of attached databases",
14061
  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
14062
  ".dbinfo ?DB?             Show status information about the database",
14063
  ".dump ?OBJECTS?          Render database content as SQL",
14064
  "   Options:",
14065
  "     --data-only            Output only INSERT statements",
14066
  "     --newlines             Allow unescaped newline characters in output",
14067
  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
14068
  "     --preserve-rowids      Include ROWID values in the output",
14069
  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
14070
  "   Additional LIKE patterns can be given in subsequent arguments",
14071
  ".echo on|off             Turn command echo on or off",
14072
  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
14073
  "   Other Modes:",
14074
#ifdef SQLITE_DEBUG
14075
  "      test                  Show raw EXPLAIN QUERY PLAN output",
14076
  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
14077
#endif
14078
  "      trigger               Like \"full\" but also show trigger bytecode",
14079
  ".excel                   Display the output of next command in spreadsheet",
14080
  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
14081
  ".exit ?CODE?             Exit this program with return-code CODE",
14082
  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
14083
  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
14084
  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
14085
  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
14086
  "   --help                  Show CMD details",
14087
  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
14088
  ".headers on|off          Turn display of headers on or off",
14089
  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
14090
  ".import FILE TABLE       Import data from FILE into TABLE",
14091
  "   Options:",
14092
  "     --ascii               Use \\037 and \\036 as column and row separators",
14093
  "     --csv                 Use , and \\n as column and row separators",
14094
  "     --skip N              Skip the first N rows of input",
14095
  "     -v                    \"Verbose\" - increase auxiliary output",
14096
  "   Notes:",
14097
  "     *  If TABLE does not exist, it is created.  The first row of input",
14098
  "        determines the column names.",
14099
  "     *  If neither --csv or --ascii are used, the input mode is derived",
14100
  "        from the \".mode\" output mode",
14101
  "     *  If FILE begins with \"|\" then it is a command that generates the",
14102
  "        input text.",
14103
#ifndef SQLITE_OMIT_TEST_CONTROL
14104
  ".imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
14105
#endif
14106
  ".indexes ?TABLE?         Show names of indexes",
14107
  "                           If TABLE is specified, only show indexes for",
14108
  "                           tables matching TABLE using the LIKE operator.",
14109
#ifdef SQLITE_ENABLE_IOTRACE
14110
  ".iotrace FILE            Enable I/O diagnostic logging to FILE",
14111
#endif
14112
  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
14113
  ".lint OPTIONS            Report potential schema issues.",
14114
  "     Options:",
14115
  "        fkey-indexes     Find missing foreign key indexes",
14116
#ifndef SQLITE_OMIT_LOAD_EXTENSION
14117
  ".load FILE ?ENTRY?       Load an extension library",
14118
#endif
14119
  ".log FILE|off            Turn logging on or off.  FILE can be stderr/stdout",
14120
  ".mode MODE ?TABLE?       Set output mode",
14121
  "   MODE is one of:",
14122
  "     ascii     Columns/rows delimited by 0x1F and 0x1E",
14123
  "     box       Tables using unicode box-drawing characters",
14124
  "     csv       Comma-separated values",
14125
  "     column    Output in columns.  (See .width)",
14126
  "     html      HTML  code",
14127
  "     insert    SQL insert statements for TABLE",
14128
  "     json      Results in a JSON array",
14129
  "     line      One value per line",
14130
  "     list      Values delimited by \"|\"",
14131
  "     markdown  Markdown table format",
14132
  "     quote     Escape answers as for SQL",
14133
  "     table     ASCII-art table",
14134
  "     tabs      Tab-separated values",
14135
  "     tcl       TCL list elements",
14136
  ".nullvalue STRING        Use STRING in place of NULL values",
14137
  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
14138
  "     If FILE begins with '|' then open as a pipe",
14139
  "       --bom  Put a UTF8 byte-order mark at the beginning",
14140
  "       -e     Send output to the system text editor",
14141
  "       -x     Send output as CSV to a spreadsheet (same as \".excel\")",
14142
#ifdef SQLITE_DEBUG
14143
  ".oom ?--repeat M? ?N?    Simulate an OOM error on the N-th allocation",
14144
#endif
14145
  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
14146
  "     Options:",
14147
  "        --append        Use appendvfs to append database to the end of FILE",
14148
#ifdef SQLITE_ENABLE_DESERIALIZE
14149
  "        --deserialize   Load into memory useing sqlite3_deserialize()",
14150
  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
14151
  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
14152
#endif
14153
  "        --new           Initialize FILE to an empty database",
14154
  "        --nofollow      Do not follow symbolic links",
14155
  "        --readonly      Open FILE readonly",
14156
  "        --zip           FILE is a ZIP archive",
14157
  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
14158
  "   If FILE begins with '|' then open it as a pipe.",
14159
  "   Options:",
14160
  "     --bom                 Prefix output with a UTF8 byte-order mark",
14161
  "     -e                    Send output to the system text editor",
14162
  "     -x                    Send output as CSV to a spreadsheet",
14163
  ".parameter CMD ...       Manage SQL parameter bindings",
14164
  "   clear                   Erase all bindings",
14165
  "   init                    Initialize the TEMP table that holds bindings",
14166
  "   list                    List the current parameter bindings",
14167
  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
14168
  "                           PARAMETER should start with one of: $ : @ ?",
14169
  "   unset PARAMETER         Remove PARAMETER from the binding table",
14170
  ".print STRING...         Print literal STRING",
14171
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
14172
  ".progress N              Invoke progress handler after every N opcodes",
14173
  "   --limit N                 Interrupt after N progress callbacks",
14174
  "   --once                    Do no more than one progress interrupt",
14175
  "   --quiet|-q                No output except at interrupts",
14176
  "   --reset                   Reset the count for each input and interrupt",
14177
#endif
14178
  ".prompt MAIN CONTINUE    Replace the standard prompts",
14179
  ".quit                    Exit this program",
14180
  ".read FILE               Read input from FILE",
14181
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14182
  ".recover                 Recover as much data as possible from corrupt db.",
14183
  "   --freelist-corrupt       Assume the freelist is corrupt",
14184
  "   --recovery-db NAME       Store recovery metadata in database file NAME",
14185
  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
14186
  "   --no-rowids              Do not attempt to recover rowid values",
14187
  "                            that are not also INTEGER PRIMARY KEYs",
14188
#endif
14189
  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
14190
  ".save FILE               Write in-memory database into FILE",
14191
  ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
14192
  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
14193
  "   Options:",
14194
  "      --indent             Try to pretty-print the schema",
14195
  "      --nosys              Omit objects whose names start with \"sqlite_\"",
14196
  ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
14197
  "    Options:",
14198
  "       --init               Create a new SELFTEST table",
14199
  "       -v                   Verbose output",
14200
  ".separator COL ?ROW?     Change the column and row separators",
14201
#if defined(SQLITE_ENABLE_SESSION)
14202
  ".session ?NAME? CMD ...  Create or control sessions",
14203
  "   Subcommands:",
14204
  "     attach TABLE             Attach TABLE",
14205
  "     changeset FILE           Write a changeset into FILE",
14206
  "     close                    Close one session",
14207
  "     enable ?BOOLEAN?         Set or query the enable bit",
14208
  "     filter GLOB...           Reject tables matching GLOBs",
14209
  "     indirect ?BOOLEAN?       Mark or query the indirect status",
14210
  "     isempty                  Query whether the session is empty",
14211
  "     list                     List currently open session names",
14212
  "     open DB NAME             Open a new session on DB",
14213
  "     patchset FILE            Write a patchset into FILE",
14214
  "   If ?NAME? is omitted, the first defined session is used.",
14215
#endif
14216
  ".sha3sum ...             Compute a SHA3 hash of database content",
14217
  "    Options:",
14218
  "      --schema              Also hash the sqlite_schema table",
14219
  "      --sha3-224            Use the sha3-224 algorithm",
14220
  "      --sha3-256            Use the sha3-256 algorithm (default)",
14221
  "      --sha3-384            Use the sha3-384 algorithm",
14222
  "      --sha3-512            Use the sha3-512 algorithm",
14223
  "    Any other argument is a LIKE pattern for tables to hash",
14224
#ifndef SQLITE_NOHAVE_SYSTEM
14225
  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
14226
#endif
14227
  ".show                    Show the current values for various settings",
14228
  ".stats ?ARG?             Show stats or turn stats on or off",
14229
  "   off                      Turn off automatic stat display",
14230
  "   on                       Turn on automatic stat display",
14231
  "   stmt                     Show statement stats",
14232
  "   vmstep                   Show the virtual machine step count only",
14233
#ifndef SQLITE_NOHAVE_SYSTEM
14234
  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
14235
#endif
14236
  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
14237
  ".testcase NAME           Begin redirecting output to 'testcase-out.txt'",
14238
  ".testctrl CMD ...        Run various sqlite3_test_control() operations",
14239
  "                           Run \".testctrl\" with no arguments for details",
14240
  ".timeout MS              Try opening locked tables for MS milliseconds",
14241
  ".timer on|off            Turn SQL timer on or off",
14242
#ifndef SQLITE_OMIT_TRACE
14243
  ".trace ?OPTIONS?         Output each SQL statement as it is run",
14244
  "    FILE                    Send output to FILE",
14245
  "    stdout                  Send output to stdout",
14246
  "    stderr                  Send output to stderr",
14247
  "    off                     Disable tracing",
14248
  "    --expanded              Expand query parameters",
14249
#ifdef SQLITE_ENABLE_NORMALIZE
14250
  "    --normalized            Normal the SQL statements",
14251
#endif
14252
  "    --plain                 Show SQL as it is input",
14253
  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
14254
  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
14255
  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
14256
  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
14257
#endif /* SQLITE_OMIT_TRACE */
14258
#ifdef SQLITE_DEBUG
14259
  ".unmodule NAME ...       Unregister virtual table modules",
14260
  "    --allexcept             Unregister everything except those named",
14261
#endif
14262
  ".vfsinfo ?AUX?           Information about the top-level VFS",
14263
  ".vfslist                 List all available VFSes",
14264
  ".vfsname ?AUX?           Print the name of the VFS stack",
14265
  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
14266
  "     Negative values right-justify",
14267
};
14268
 
14269
/*
14270
** Output help text.
14271
**
14272
** zPattern describes the set of commands for which help text is provided.
14273
** If zPattern is NULL, then show all commands, but only give a one-line
14274
** description of each.
14275
**
14276
** Return the number of matches.
14277
*/
14278
static int showHelp(FILE *out, const char *zPattern){
14279
  int i = 0;
14280
  int j = 0;
14281
  int n = 0;
14282
  char *zPat;
14283
  if( zPattern==0
14284
   || zPattern[0]=='0'
14285
   || strcmp(zPattern,"-a")==0
14286
   || strcmp(zPattern,"-all")==0
14287
   || strcmp(zPattern,"--all")==0
14288
  ){
14289
    /* Show all commands, but only one line per command */
14290
    if( zPattern==0 ) zPattern = "";
14291
    for(i=0; i
14292
      if( azHelp[i][0]=='.' || zPattern[0] ){
14293
        utf8_printf(out, "%s\n", azHelp[i]);
14294
        n++;
14295
      }
14296
    }
14297
  }else{
14298
    /* Look for commands that for which zPattern is an exact prefix */
14299
    zPat = sqlite3_mprintf(".%s*", zPattern);
14300
    for(i=0; i
14301
      if( sqlite3_strglob(zPat, azHelp[i])==0 ){
14302
        utf8_printf(out, "%s\n", azHelp[i]);
14303
        j = i+1;
14304
        n++;
14305
      }
14306
    }
14307
    sqlite3_free(zPat);
14308
    if( n ){
14309
      if( n==1 ){
14310
        /* when zPattern is a prefix of exactly one command, then include the
14311
        ** details of that command, which should begin at offset j */
14312
        while( j
14313
          utf8_printf(out, "%s\n", azHelp[j]);
14314
          j++;
14315
        }
14316
      }
14317
      return n;
14318
    }
14319
    /* Look for commands that contain zPattern anywhere.  Show the complete
14320
    ** text of all commands that match. */
14321
    zPat = sqlite3_mprintf("%%%s%%", zPattern);
14322
    for(i=0; i
14323
      if( azHelp[i][0]=='.' ) j = i;
14324
      if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
14325
        utf8_printf(out, "%s\n", azHelp[j]);
14326
        while( j
14327
          j++;
14328
          utf8_printf(out, "%s\n", azHelp[j]);
14329
        }
14330
        i = j;
14331
        n++;
14332
      }
14333
    }
14334
    sqlite3_free(zPat);
14335
  }
14336
  return n;
14337
}
14338
 
14339
/* Forward reference */
14340
static int process_input(ShellState *p);
14341
 
14342
/*
14343
** Read the content of file zName into memory obtained from sqlite3_malloc64()
14344
** and return a pointer to the buffer. The caller is responsible for freeing
14345
** the memory.
14346
**
14347
** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
14348
** read.
14349
**
14350
** For convenience, a nul-terminator byte is always appended to the data read
14351
** from the file before the buffer is returned. This byte is not included in
14352
** the final value of (*pnByte), if applicable.
14353
**
14354
** NULL is returned if any error is encountered. The final value of *pnByte
14355
** is undefined in this case.
14356
*/
14357
static char *readFile(const char *zName, int *pnByte){
14358
  FILE *in = fopen(zName, "rb");
14359
  long nIn;
14360
  size_t nRead;
14361
  char *pBuf;
14362
  if( in==0 ) return 0;
14363
  fseek(in, 0, SEEK_END);
14364
  nIn = ftell(in);
14365
  rewind(in);
14366
  pBuf = sqlite3_malloc64( nIn+1 );
14367
  if( pBuf==0 ){ fclose(in); return 0; }
14368
  nRead = fread(pBuf, nIn, 1, in);
14369
  fclose(in);
14370
  if( nRead!=1 ){
14371
    sqlite3_free(pBuf);
14372
    return 0;
14373
  }
14374
  pBuf[nIn] = 0;
14375
  if( pnByte ) *pnByte = nIn;
14376
  return pBuf;
14377
}
14378
 
14379
#if defined(SQLITE_ENABLE_SESSION)
14380
/*
14381
** Close a single OpenSession object and release all of its associated
14382
** resources.
14383
*/
14384
static void session_close(OpenSession *pSession){
14385
  int i;
14386
  sqlite3session_delete(pSession->p);
14387
  sqlite3_free(pSession->zName);
14388
  for(i=0; inFilter; i++){
14389
    sqlite3_free(pSession->azFilter[i]);
14390
  }
14391
  sqlite3_free(pSession->azFilter);
14392
  memset(pSession, 0, sizeof(OpenSession));
14393
}
14394
#endif
14395
 
14396
/*
14397
** Close all OpenSession objects and release all associated resources.
14398
*/
14399
#if defined(SQLITE_ENABLE_SESSION)
14400
static void session_close_all(ShellState *p){
14401
  int i;
14402
  for(i=0; inSession; i++){
14403
    session_close(&p->aSession[i]);
14404
  }
14405
  p->nSession = 0;
14406
}
14407
#else
14408
# define session_close_all(X)
14409
#endif
14410
 
14411
/*
14412
** Implementation of the xFilter function for an open session.  Omit
14413
** any tables named by ".session filter" but let all other table through.
14414
*/
14415
#if defined(SQLITE_ENABLE_SESSION)
14416
static int session_filter(void *pCtx, const char *zTab){
14417
  OpenSession *pSession = (OpenSession*)pCtx;
14418
  int i;
14419
  for(i=0; inFilter; i++){
14420
    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
14421
  }
14422
  return 1;
14423
}
14424
#endif
14425
 
14426
/*
14427
** Try to deduce the type of file for zName based on its content.  Return
14428
** one of the SHELL_OPEN_* constants.
14429
**
14430
** If the file does not exist or is empty but its name looks like a ZIP
14431
** archive and the dfltZip flag is true, then assume it is a ZIP archive.
14432
** Otherwise, assume an ordinary database regardless of the filename if
14433
** the type cannot be determined from content.
14434
*/
14435
int deduceDatabaseType(const char *zName, int dfltZip){
14436
  FILE *f = fopen(zName, "rb");
14437
  size_t n;
14438
  int rc = SHELL_OPEN_UNSPEC;
14439
  char zBuf[100];
14440
  if( f==0 ){
14441
    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
14442
       return SHELL_OPEN_ZIPFILE;
14443
    }else{
14444
       return SHELL_OPEN_NORMAL;
14445
    }
14446
  }
14447
  n = fread(zBuf, 16, 1, f);
14448
  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
14449
    fclose(f);
14450
    return SHELL_OPEN_NORMAL;
14451
  }
14452
  fseek(f, -25, SEEK_END);
14453
  n = fread(zBuf, 25, 1, f);
14454
  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
14455
    rc = SHELL_OPEN_APPENDVFS;
14456
  }else{
14457
    fseek(f, -22, SEEK_END);
14458
    n = fread(zBuf, 22, 1, f);
14459
    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
14460
       && zBuf[3]==0x06 ){
14461
      rc = SHELL_OPEN_ZIPFILE;
14462
    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
14463
      rc = SHELL_OPEN_ZIPFILE;
14464
    }
14465
  }
14466
  fclose(f);
14467
  return rc;
14468
}
14469
 
14470
#ifdef SQLITE_ENABLE_DESERIALIZE
14471
/*
14472
** Reconstruct an in-memory database using the output from the "dbtotxt"
14473
** program.  Read content from the file in p->zDbFilename.  If p->zDbFilename
14474
** is 0, then read from standard input.
14475
*/
14476
static unsigned char *readHexDb(ShellState *p, int *pnData){
14477
  unsigned char *a = 0;
14478
  int nLine;
14479
  int n = 0;
14480
  int pgsz = 0;
14481
  int iOffset = 0;
14482
  int j, k;
14483
  int rc;
14484
  FILE *in;
14485
  unsigned int x[16];
14486
  char zLine[1000];
14487
  if( p->zDbFilename ){
14488
    in = fopen(p->zDbFilename, "r");
14489
    if( in==0 ){
14490
      utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
14491
      return 0;
14492
    }
14493
    nLine = 0;
14494
  }else{
14495
    in = p->in;
14496
    nLine = p->lineno;
14497
    if( in==0 ) in = stdin;
14498
  }
14499
  *pnData = 0;
14500
  nLine++;
14501
  if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
14502
  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
14503
  if( rc!=2 ) goto readHexDb_error;
14504
  if( n<0 ) goto readHexDb_error;
14505
  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
14506
  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
14507
  a = sqlite3_malloc( n ? n : 1 );
14508
  if( a==0 ){
14509
    utf8_printf(stderr, "Out of memory!\n");
14510
    goto readHexDb_error;
14511
  }
14512
  memset(a, 0, n);
14513
  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
14514
    utf8_printf(stderr, "invalid pagesize\n");
14515
    goto readHexDb_error;
14516
  }
14517
  for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
14518
    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
14519
    if( rc==2 ){
14520
      iOffset = k;
14521
      continue;
14522
    }
14523
    if( strncmp(zLine, "| end ", 6)==0 ){
14524
      break;
14525
    }
14526
    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
14527
                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
14528
                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
14529
    if( rc==17 ){
14530
      k = iOffset+j;
14531
      if( k+16<=n ){
14532
        int ii;
14533
        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
14534
      }
14535
    }
14536
  }
14537
  *pnData = n;
14538
  if( in!=p->in ){
14539
    fclose(in);
14540
  }else{
14541
    p->lineno = nLine;
14542
  }
14543
  return a;
14544
 
14545
readHexDb_error:
14546
  if( in!=p->in ){
14547
    fclose(in);
14548
  }else{
14549
    while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
14550
      nLine++;
14551
      if(strncmp(zLine, "| end ", 6)==0 ) break;
14552
    }
14553
    p->lineno = nLine;
14554
  }
14555
  sqlite3_free(a);
14556
  utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
14557
  return 0;
14558
}
14559
#endif /* SQLITE_ENABLE_DESERIALIZE */
14560
 
14561
/*
14562
** Scalar function "shell_int32". The first argument to this function
14563
** must be a blob. The second a non-negative integer. This function
14564
** reads and returns a 32-bit big-endian integer from byte
14565
** offset (4*) of the blob.
14566
*/
14567
static void shellInt32(
14568
  sqlite3_context *context,
14569
  int argc,
14570
  sqlite3_value **argv
14571
){
14572
  const unsigned char *pBlob;
14573
  int nBlob;
14574
  int iInt;
14575
 
14576
  UNUSED_PARAMETER(argc);
14577
  nBlob = sqlite3_value_bytes(argv[0]);
14578
  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
14579
  iInt = sqlite3_value_int(argv[1]);
14580
 
14581
  if( iInt>=0 && (iInt+1)*4<=nBlob ){
14582
    const unsigned char *a = &pBlob[iInt*4];
14583
    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
14584
                       + ((sqlite3_int64)a[1]<<16)
14585
                       + ((sqlite3_int64)a[2]<< 8)
14586
                       + ((sqlite3_int64)a[3]<< 0);
14587
    sqlite3_result_int64(context, iVal);
14588
  }
14589
}
14590
 
14591
/*
14592
** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
14593
** using "..." with internal double-quote characters doubled.
14594
*/
14595
static void shellIdQuote(
14596
  sqlite3_context *context,
14597
  int argc,
14598
  sqlite3_value **argv
14599
){
14600
  const char *zName = (const char*)sqlite3_value_text(argv[0]);
14601
  UNUSED_PARAMETER(argc);
14602
  if( zName ){
14603
    char *z = sqlite3_mprintf("\"%w\"", zName);
14604
    sqlite3_result_text(context, z, -1, sqlite3_free);
14605
  }
14606
}
14607
 
14608
/*
14609
** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
14610
*/
14611
static void shellUSleepFunc(
14612
  sqlite3_context *context,
14613
  int argcUnused,
14614
  sqlite3_value **argv
14615
){
14616
  int sleep = sqlite3_value_int(argv[0]);
14617
  (void)argcUnused;
14618
  sqlite3_sleep(sleep/1000);
14619
  sqlite3_result_int(context, sleep);
14620
}
14621
 
14622
/*
14623
** Scalar function "shell_escape_crnl" used by the .recover command.
14624
** The argument passed to this function is the output of built-in
14625
** function quote(). If the first character of the input is "'",
14626
** indicating that the value passed to quote() was a text value,
14627
** then this function searches the input for "\n" and "\r" characters
14628
** and adds a wrapper similar to the following:
14629
**
14630
**   replace(replace(, '\n', char(10), '\r', char(13));
14631
**
14632
** Or, if the first character of the input is not "'", then a copy
14633
** of the input is returned.
14634
*/
14635
static void shellEscapeCrnl(
14636
  sqlite3_context *context,
14637
  int argc,
14638
  sqlite3_value **argv
14639
){
14640
  const char *zText = (const char*)sqlite3_value_text(argv[0]);
14641
  UNUSED_PARAMETER(argc);
14642
  if( zText[0]=='\'' ){
14643
    int nText = sqlite3_value_bytes(argv[0]);
14644
    int i;
14645
    char zBuf1[20];
14646
    char zBuf2[20];
14647
    const char *zNL = 0;
14648
    const char *zCR = 0;
14649
    int nCR = 0;
14650
    int nNL = 0;
14651
 
14652
    for(i=0; zText[i]; i++){
14653
      if( zNL==0 && zText[i]=='\n' ){
14654
        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
14655
        nNL = (int)strlen(zNL);
14656
      }
14657
      if( zCR==0 && zText[i]=='\r' ){
14658
        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
14659
        nCR = (int)strlen(zCR);
14660
      }
14661
    }
14662
 
14663
    if( zNL || zCR ){
14664
      int iOut = 0;
14665
      i64 nMax = (nNL > nCR) ? nNL : nCR;
14666
      i64 nAlloc = nMax * nText + (nMax+64)*2;
14667
      char *zOut = (char*)sqlite3_malloc64(nAlloc);
14668
      if( zOut==0 ){
14669
        sqlite3_result_error_nomem(context);
14670
        return;
14671
      }
14672
 
14673
      if( zNL && zCR ){
14674
        memcpy(&zOut[iOut], "replace(replace(", 16);
14675
        iOut += 16;
14676
      }else{
14677
        memcpy(&zOut[iOut], "replace(", 8);
14678
        iOut += 8;
14679
      }
14680
      for(i=0; zText[i]; i++){
14681
        if( zText[i]=='\n' ){
14682
          memcpy(&zOut[iOut], zNL, nNL);
14683
          iOut += nNL;
14684
        }else if( zText[i]=='\r' ){
14685
          memcpy(&zOut[iOut], zCR, nCR);
14686
          iOut += nCR;
14687
        }else{
14688
          zOut[iOut] = zText[i];
14689
          iOut++;
14690
        }
14691
      }
14692
 
14693
      if( zNL ){
14694
        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
14695
        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
14696
        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
14697
      }
14698
      if( zCR ){
14699
        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
14700
        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
14701
        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
14702
      }
14703
 
14704
      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
14705
      sqlite3_free(zOut);
14706
      return;
14707
    }
14708
  }
14709
 
14710
  sqlite3_result_value(context, argv[0]);
14711
}
14712
 
14713
/* Flags for open_db().
14714
**
14715
** The default behavior of open_db() is to exit(1) if the database fails to
14716
** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
14717
** but still returns without calling exit.
14718
**
14719
** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
14720
** ZIP archive if the file does not exist or is empty and its name matches
14721
** the *.zip pattern.
14722
*/
14723
#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
14724
#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
14725
 
14726
/*
14727
** Make sure the database is open.  If it is not, then open it.  If
14728
** the database fails to open, print an error message and exit.
14729
*/
14730
static void open_db(ShellState *p, int openFlags){
14731
  if( p->db==0 ){
14732
    if( p->openMode==SHELL_OPEN_UNSPEC ){
14733
      if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
14734
        p->openMode = SHELL_OPEN_NORMAL;
14735
      }else{
14736
        p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
14737
                             (openFlags & OPEN_DB_ZIPFILE)!=0);
14738
      }
14739
    }
14740
    switch( p->openMode ){
14741
      case SHELL_OPEN_APPENDVFS: {
14742
        sqlite3_open_v2(p->zDbFilename, &p->db,
14743
           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
14744
        break;
14745
      }
14746
      case SHELL_OPEN_HEXDB:
14747
      case SHELL_OPEN_DESERIALIZE: {
14748
        sqlite3_open(0, &p->db);
14749
        break;
14750
      }
14751
      case SHELL_OPEN_ZIPFILE: {
14752
        sqlite3_open(":memory:", &p->db);
14753
        break;
14754
      }
14755
      case SHELL_OPEN_READONLY: {
14756
        sqlite3_open_v2(p->zDbFilename, &p->db,
14757
            SQLITE_OPEN_READONLY|p->openFlags, 0);
14758
        break;
14759
      }
14760
      case SHELL_OPEN_UNSPEC:
14761
      case SHELL_OPEN_NORMAL: {
14762
        sqlite3_open_v2(p->zDbFilename, &p->db,
14763
           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
14764
        break;
14765
      }
14766
    }
14767
    globalDb = p->db;
14768
    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
14769
      utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
14770
          p->zDbFilename, sqlite3_errmsg(p->db));
14771
      if( openFlags & OPEN_DB_KEEPALIVE ){
14772
        sqlite3_open(":memory:", &p->db);
14773
        return;
14774
      }
14775
      exit(1);
14776
    }
14777
#ifndef SQLITE_OMIT_LOAD_EXTENSION
14778
    sqlite3_enable_load_extension(p->db, 1);
14779
#endif
14780
    sqlite3_fileio_init(p->db, 0, 0);
14781
    sqlite3_shathree_init(p->db, 0, 0);
14782
    sqlite3_completion_init(p->db, 0, 0);
14783
    sqlite3_uint_init(p->db, 0, 0);
14784
    sqlite3_decimal_init(p->db, 0, 0);
14785
    sqlite3_ieee_init(p->db, 0, 0);
14786
    sqlite3_series_init(p->db, 0, 0);
14787
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14788
    sqlite3_dbdata_init(p->db, 0, 0);
14789
#endif
14790
#ifdef SQLITE_HAVE_ZLIB
14791
    sqlite3_zipfile_init(p->db, 0, 0);
14792
    sqlite3_sqlar_init(p->db, 0, 0);
14793
#endif
14794
    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
14795
                            shellAddSchemaName, 0, 0);
14796
    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
14797
                            shellModuleSchema, 0, 0);
14798
    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
14799
                            shellPutsFunc, 0, 0);
14800
    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
14801
                            shellEscapeCrnl, 0, 0);
14802
    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
14803
                            shellInt32, 0, 0);
14804
    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
14805
                            shellIdQuote, 0, 0);
14806
    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
14807
                            shellUSleepFunc, 0, 0);
14808
#ifndef SQLITE_NOHAVE_SYSTEM
14809
    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
14810
                            editFunc, 0, 0);
14811
    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
14812
                            editFunc, 0, 0);
14813
#endif
14814
    if( p->openMode==SHELL_OPEN_ZIPFILE ){
14815
      char *zSql = sqlite3_mprintf(
14816
         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
14817
      sqlite3_exec(p->db, zSql, 0, 0, 0);
14818
      sqlite3_free(zSql);
14819
    }
14820
#ifdef SQLITE_ENABLE_DESERIALIZE
14821
    else
14822
    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
14823
      int rc;
14824
      int nData = 0;
14825
      unsigned char *aData;
14826
      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
14827
        aData = (unsigned char*)readFile(p->zDbFilename, &nData);
14828
      }else{
14829
        aData = readHexDb(p, &nData);
14830
        if( aData==0 ){
14831
          return;
14832
        }
14833
      }
14834
      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
14835
                   SQLITE_DESERIALIZE_RESIZEABLE |
14836
                   SQLITE_DESERIALIZE_FREEONCLOSE);
14837
      if( rc ){
14838
        utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
14839
      }
14840
      if( p->szMax>0 ){
14841
        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
14842
      }
14843
    }
14844
#endif
14845
  }
14846
}
14847
 
14848
/*
14849
** Attempt to close the databaes connection.  Report errors.
14850
*/
14851
void close_db(sqlite3 *db){
14852
  int rc = sqlite3_close(db);
14853
  if( rc ){
14854
    utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
14855
        rc, sqlite3_errmsg(db));
14856
  }
14857
}
14858
 
14859
#if HAVE_READLINE || HAVE_EDITLINE
14860
/*
14861
** Readline completion callbacks
14862
*/
14863
static char *readline_completion_generator(const char *text, int state){
14864
  static sqlite3_stmt *pStmt = 0;
14865
  char *zRet;
14866
  if( state==0 ){
14867
    char *zSql;
14868
    sqlite3_finalize(pStmt);
14869
    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
14870
                           "  FROM completion(%Q) ORDER BY 1", text);
14871
    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
14872
    sqlite3_free(zSql);
14873
  }
14874
  if( sqlite3_step(pStmt)==SQLITE_ROW ){
14875
    zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
14876
  }else{
14877
    sqlite3_finalize(pStmt);
14878
    pStmt = 0;
14879
    zRet = 0;
14880
  }
14881
  return zRet;
14882
}
14883
static char **readline_completion(const char *zText, int iStart, int iEnd){
14884
  rl_attempted_completion_over = 1;
14885
  return rl_completion_matches(zText, readline_completion_generator);
14886
}
14887
 
14888
#elif HAVE_LINENOISE
14889
/*
14890
** Linenoise completion callback
14891
*/
14892
static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
14893
  int nLine = strlen30(zLine);
14894
  int i, iStart;
14895
  sqlite3_stmt *pStmt = 0;
14896
  char *zSql;
14897
  char zBuf[1000];
14898
 
14899
  if( nLine>sizeof(zBuf)-30 ) return;
14900
  if( zLine[0]=='.' || zLine[0]=='#') return;
14901
  for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
14902
  if( i==nLine-1 ) return;
14903
  iStart = i+1;
14904
  memcpy(zBuf, zLine, iStart);
14905
  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
14906
                         "  FROM completion(%Q,%Q) ORDER BY 1",
14907
                         &zLine[iStart], zLine);
14908
  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
14909
  sqlite3_free(zSql);
14910
  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
14911
  while( sqlite3_step(pStmt)==SQLITE_ROW ){
14912
    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
14913
    int nCompletion = sqlite3_column_bytes(pStmt, 0);
14914
    if( iStart+nCompletion < sizeof(zBuf)-1 ){
14915
      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
14916
      linenoiseAddCompletion(lc, zBuf);
14917
    }
14918
  }
14919
  sqlite3_finalize(pStmt);
14920
}
14921
#endif
14922
 
14923
/*
14924
** Do C-language style dequoting.
14925
**
14926
**    \a    -> alarm
14927
**    \b    -> backspace
14928
**    \t    -> tab
14929
**    \n    -> newline
14930
**    \v    -> vertical tab
14931
**    \f    -> form feed
14932
**    \r    -> carriage return
14933
**    \s    -> space
14934
**    \"    -> "
14935
**    \'    -> '
14936
**    \\    -> backslash
14937
**    \NNN  -> ascii character NNN in octal
14938
*/
14939
static void resolve_backslashes(char *z){
14940
  int i, j;
14941
  char c;
14942
  while( *z && *z!='\\' ) z++;
14943
  for(i=j=0; (c = z[i])!=0; i++, j++){
14944
    if( c=='\\' && z[i+1]!=0 ){
14945
      c = z[++i];
14946
      if( c=='a' ){
14947
        c = '\a';
14948
      }else if( c=='b' ){
14949
        c = '\b';
14950
      }else if( c=='t' ){
14951
        c = '\t';
14952
      }else if( c=='n' ){
14953
        c = '\n';
14954
      }else if( c=='v' ){
14955
        c = '\v';
14956
      }else if( c=='f' ){
14957
        c = '\f';
14958
      }else if( c=='r' ){
14959
        c = '\r';
14960
      }else if( c=='"' ){
14961
        c = '"';
14962
      }else if( c=='\'' ){
14963
        c = '\'';
14964
      }else if( c=='\\' ){
14965
        c = '\\';
14966
      }else if( c>='0' && c<='7' ){
14967
        c -= '0';
14968
        if( z[i+1]>='0' && z[i+1]<='7' ){
14969
          i++;
14970
          c = (c<<3) + z[i] - '0';
14971
          if( z[i+1]>='0' && z[i+1]<='7' ){
14972
            i++;
14973
            c = (c<<3) + z[i] - '0';
14974
          }
14975
        }
14976
      }
14977
    }
14978
    z[j] = c;
14979
  }
14980
  if( j
14981
}
14982
 
14983
/*
14984
** Interpret zArg as either an integer or a boolean value.  Return 1 or 0
14985
** for TRUE and FALSE.  Return the integer value if appropriate.
14986
*/
14987
static int booleanValue(const char *zArg){
14988
  int i;
14989
  if( zArg[0]=='0' && zArg[1]=='x' ){
14990
    for(i=2; hexDigitValue(zArg[i])>=0; i++){}
14991
  }else{
14992
    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
14993
  }
14994
  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
14995
  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
14996
    return 1;
14997
  }
14998
  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
14999
    return 0;
15000
  }
15001
  utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
15002
          zArg);
15003
  return 0;
15004
}
15005
 
15006
/*
15007
** Set or clear a shell flag according to a boolean value.
15008
*/
15009
static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
15010
  if( booleanValue(zArg) ){
15011
    ShellSetFlag(p, mFlag);
15012
  }else{
15013
    ShellClearFlag(p, mFlag);
15014
  }
15015
}
15016
 
15017
/*
15018
** Close an output file, assuming it is not stderr or stdout
15019
*/
15020
static void output_file_close(FILE *f){
15021
  if( f && f!=stdout && f!=stderr ) fclose(f);
15022
}
15023
 
15024
/*
15025
** Try to open an output file.   The names "stdout" and "stderr" are
15026
** recognized and do the right thing.  NULL is returned if the output
15027
** filename is "off".
15028
*/
15029
static FILE *output_file_open(const char *zFile, int bTextMode){
15030
  FILE *f;
15031
  if( strcmp(zFile,"stdout")==0 ){
15032
    f = stdout;
15033
  }else if( strcmp(zFile, "stderr")==0 ){
15034
    f = stderr;
15035
  }else if( strcmp(zFile, "off")==0 ){
15036
    f = 0;
15037
  }else{
15038
    f = fopen(zFile, bTextMode ? "w" : "wb");
15039
    if( f==0 ){
15040
      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
15041
    }
15042
  }
15043
  return f;
15044
}
15045
 
15046
#ifndef SQLITE_OMIT_TRACE
15047
/*
15048
** A routine for handling output from sqlite3_trace().
15049
*/
15050
static int sql_trace_callback(
15051
  unsigned mType,         /* The trace type */
15052
  void *pArg,             /* The ShellState pointer */
15053
  void *pP,               /* Usually a pointer to sqlite_stmt */
15054
  void *pX                /* Auxiliary output */
15055
){
15056
  ShellState *p = (ShellState*)pArg;
15057
  sqlite3_stmt *pStmt;
15058
  const char *zSql;
15059
  int nSql;
15060
  if( p->traceOut==0 ) return 0;
15061
  if( mType==SQLITE_TRACE_CLOSE ){
15062
    utf8_printf(p->traceOut, "-- closing database connection\n");
15063
    return 0;
15064
  }
15065
  if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
15066
    zSql = (const char*)pX;
15067
  }else{
15068
    pStmt = (sqlite3_stmt*)pP;
15069
    switch( p->eTraceType ){
15070
      case SHELL_TRACE_EXPANDED: {
15071
        zSql = sqlite3_expanded_sql(pStmt);
15072
        break;
15073
      }
15074
#ifdef SQLITE_ENABLE_NORMALIZE
15075
      case SHELL_TRACE_NORMALIZED: {
15076
        zSql = sqlite3_normalized_sql(pStmt);
15077
        break;
15078
      }
15079
#endif
15080
      default: {
15081
        zSql = sqlite3_sql(pStmt);
15082
        break;
15083
      }
15084
    }
15085
  }
15086
  if( zSql==0 ) return 0;
15087
  nSql = strlen30(zSql);
15088
  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
15089
  switch( mType ){
15090
    case SQLITE_TRACE_ROW:
15091
    case SQLITE_TRACE_STMT: {
15092
      utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
15093
      break;
15094
    }
15095
    case SQLITE_TRACE_PROFILE: {
15096
      sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
15097
      utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
15098
      break;
15099
    }
15100
  }
15101
  return 0;
15102
}
15103
#endif
15104
 
15105
/*
15106
** A no-op routine that runs with the ".breakpoint" doc-command.  This is
15107
** a useful spot to set a debugger breakpoint.
15108
*/
15109
static void test_breakpoint(void){
15110
  static int nCall = 0;
15111
  nCall++;
15112
}
15113
 
15114
/*
15115
** An object used to read a CSV and other files for import.
15116
*/
15117
typedef struct ImportCtx ImportCtx;
15118
struct ImportCtx {
15119
  const char *zFile;  /* Name of the input file */
15120
  FILE *in;           /* Read the CSV text from this input stream */
15121
  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
15122
  char *z;            /* Accumulated text for a field */
15123
  int n;              /* Number of bytes in z */
15124
  int nAlloc;         /* Space allocated for z[] */
15125
  int nLine;          /* Current line number */
15126
  int nRow;           /* Number of rows imported */
15127
  int nErr;           /* Number of errors encountered */
15128
  int bNotFirst;      /* True if one or more bytes already read */
15129
  int cTerm;          /* Character that terminated the most recent field */
15130
  int cColSep;        /* The column separator character.  (Usually ",") */
15131
  int cRowSep;        /* The row separator character.  (Usually "\n") */
15132
};
15133
 
15134
/* Clean up resourced used by an ImportCtx */
15135
static void import_cleanup(ImportCtx *p){
15136
  if( p->in!=0 && p->xCloser!=0 ){
15137
    p->xCloser(p->in);
15138
    p->in = 0;
15139
  }
15140
  sqlite3_free(p->z);
15141
  p->z = 0;
15142
}
15143
 
15144
/* Append a single byte to z[] */
15145
static void import_append_char(ImportCtx *p, int c){
15146
  if( p->n+1>=p->nAlloc ){
15147
    p->nAlloc += p->nAlloc + 100;
15148
    p->z = sqlite3_realloc64(p->z, p->nAlloc);
15149
    if( p->z==0 ) shell_out_of_memory();
15150
  }
15151
  p->z[p->n++] = (char)c;
15152
}
15153
 
15154
/* Read a single field of CSV text.  Compatible with rfc4180 and extended
15155
** with the option of having a separator other than ",".
15156
**
15157
**   +  Input comes from p->in.
15158
**   +  Store results in p->z of length p->n.  Space to hold p->z comes
15159
**      from sqlite3_malloc64().
15160
**   +  Use p->cSep as the column separator.  The default is ",".
15161
**   +  Use p->rSep as the row separator.  The default is "\n".
15162
**   +  Keep track of the line number in p->nLine.
15163
**   +  Store the character that terminates the field in p->cTerm.  Store
15164
**      EOF on end-of-file.
15165
**   +  Report syntax errors on stderr
15166
*/
15167
static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
15168
  int c;
15169
  int cSep = p->cColSep;
15170
  int rSep = p->cRowSep;
15171
  p->n = 0;
15172
  c = fgetc(p->in);
15173
  if( c==EOF || seenInterrupt ){
15174
    p->cTerm = EOF;
15175
    return 0;
15176
  }
15177
  if( c=='"' ){
15178
    int pc, ppc;
15179
    int startLine = p->nLine;
15180
    int cQuote = c;
15181
    pc = ppc = 0;
15182
    while( 1 ){
15183
      c = fgetc(p->in);
15184
      if( c==rSep ) p->nLine++;
15185
      if( c==cQuote ){
15186
        if( pc==cQuote ){
15187
          pc = 0;
15188
          continue;
15189
        }
15190
      }
15191
      if( (c==cSep && pc==cQuote)
15192
       || (c==rSep && pc==cQuote)
15193
       || (c==rSep && pc=='\r' && ppc==cQuote)
15194
       || (c==EOF && pc==cQuote)
15195
      ){
15196
        do{ p->n--; }while( p->z[p->n]!=cQuote );
15197
        p->cTerm = c;
15198
        break;
15199
      }
15200
      if( pc==cQuote && c!='\r' ){
15201
        utf8_printf(stderr, "%s:%d: unescaped %c character\n",
15202
                p->zFile, p->nLine, cQuote);
15203
      }
15204
      if( c==EOF ){
15205
        utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
15206
                p->zFile, startLine, cQuote);
15207
        p->cTerm = c;
15208
        break;
15209
      }
15210
      import_append_char(p, c);
15211
      ppc = pc;
15212
      pc = c;
15213
    }
15214
  }else{
15215
    /* If this is the first field being parsed and it begins with the
15216
    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
15217
    if( (c&0xff)==0xef && p->bNotFirst==0 ){
15218
      import_append_char(p, c);
15219
      c = fgetc(p->in);
15220
      if( (c&0xff)==0xbb ){
15221
        import_append_char(p, c);
15222
        c = fgetc(p->in);
15223
        if( (c&0xff)==0xbf ){
15224
          p->bNotFirst = 1;
15225
          p->n = 0;
15226
          return csv_read_one_field(p);
15227
        }
15228
      }
15229
    }
15230
    while( c!=EOF && c!=cSep && c!=rSep ){
15231
      import_append_char(p, c);
15232
      c = fgetc(p->in);
15233
    }
15234
    if( c==rSep ){
15235
      p->nLine++;
15236
      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
15237
    }
15238
    p->cTerm = c;
15239
  }
15240
  if( p->z ) p->z[p->n] = 0;
15241
  p->bNotFirst = 1;
15242
  return p->z;
15243
}
15244
 
15245
/* Read a single field of ASCII delimited text.
15246
**
15247
**   +  Input comes from p->in.
15248
**   +  Store results in p->z of length p->n.  Space to hold p->z comes
15249
**      from sqlite3_malloc64().
15250
**   +  Use p->cSep as the column separator.  The default is "\x1F".
15251
**   +  Use p->rSep as the row separator.  The default is "\x1E".
15252
**   +  Keep track of the row number in p->nLine.
15253
**   +  Store the character that terminates the field in p->cTerm.  Store
15254
**      EOF on end-of-file.
15255
**   +  Report syntax errors on stderr
15256
*/
15257
static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
15258
  int c;
15259
  int cSep = p->cColSep;
15260
  int rSep = p->cRowSep;
15261
  p->n = 0;
15262
  c = fgetc(p->in);
15263
  if( c==EOF || seenInterrupt ){
15264
    p->cTerm = EOF;
15265
    return 0;
15266
  }
15267
  while( c!=EOF && c!=cSep && c!=rSep ){
15268
    import_append_char(p, c);
15269
    c = fgetc(p->in);
15270
  }
15271
  if( c==rSep ){
15272
    p->nLine++;
15273
  }
15274
  p->cTerm = c;
15275
  if( p->z ) p->z[p->n] = 0;
15276
  return p->z;
15277
}
15278
 
15279
/*
15280
** Try to transfer data for table zTable.  If an error is seen while
15281
** moving forward, try to go backwards.  The backwards movement won't
15282
** work for WITHOUT ROWID tables.
15283
*/
15284
static void tryToCloneData(
15285
  ShellState *p,
15286
  sqlite3 *newDb,
15287
  const char *zTable
15288
){
15289
  sqlite3_stmt *pQuery = 0;
15290
  sqlite3_stmt *pInsert = 0;
15291
  char *zQuery = 0;
15292
  char *zInsert = 0;
15293
  int rc;
15294
  int i, j, n;
15295
  int nTable = strlen30(zTable);
15296
  int k = 0;
15297
  int cnt = 0;
15298
  const int spinRate = 10000;
15299
 
15300
  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
15301
  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15302
  if( rc ){
15303
    utf8_printf(stderr, "Error %d: %s on [%s]\n",
15304
            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15305
            zQuery);
15306
    goto end_data_xfer;
15307
  }
15308
  n = sqlite3_column_count(pQuery);
15309
  zInsert = sqlite3_malloc64(200 + nTable + n*3);
15310
  if( zInsert==0 ) shell_out_of_memory();
15311
  sqlite3_snprintf(200+nTable,zInsert,
15312
                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
15313
  i = strlen30(zInsert);
15314
  for(j=1; j
15315
    memcpy(zInsert+i, ",?", 2);
15316
    i += 2;
15317
  }
15318
  memcpy(zInsert+i, ");", 3);
15319
  rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
15320
  if( rc ){
15321
    utf8_printf(stderr, "Error %d: %s on [%s]\n",
15322
            sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
15323
            zQuery);
15324
    goto end_data_xfer;
15325
  }
15326
  for(k=0; k<2; k++){
15327
    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15328
      for(i=0; i
15329
        switch( sqlite3_column_type(pQuery, i) ){
15330
          case SQLITE_NULL: {
15331
            sqlite3_bind_null(pInsert, i+1);
15332
            break;
15333
          }
15334
          case SQLITE_INTEGER: {
15335
            sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
15336
            break;
15337
          }
15338
          case SQLITE_FLOAT: {
15339
            sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
15340
            break;
15341
          }
15342
          case SQLITE_TEXT: {
15343
            sqlite3_bind_text(pInsert, i+1,
15344
                             (const char*)sqlite3_column_text(pQuery,i),
15345
                             -1, SQLITE_STATIC);
15346
            break;
15347
          }
15348
          case SQLITE_BLOB: {
15349
            sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
15350
                                            sqlite3_column_bytes(pQuery,i),
15351
                                            SQLITE_STATIC);
15352
            break;
15353
          }
15354
        }
15355
      } /* End for */
15356
      rc = sqlite3_step(pInsert);
15357
      if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
15358
        utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
15359
                        sqlite3_errmsg(newDb));
15360
      }
15361
      sqlite3_reset(pInsert);
15362
      cnt++;
15363
      if( (cnt%spinRate)==0 ){
15364
        printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
15365
        fflush(stdout);
15366
      }
15367
    } /* End while */
15368
    if( rc==SQLITE_DONE ) break;
15369
    sqlite3_finalize(pQuery);
15370
    sqlite3_free(zQuery);
15371
    zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
15372
                             zTable);
15373
    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15374
    if( rc ){
15375
      utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
15376
      break;
15377
    }
15378
  } /* End for(k=0...) */
15379
 
15380
end_data_xfer:
15381
  sqlite3_finalize(pQuery);
15382
  sqlite3_finalize(pInsert);
15383
  sqlite3_free(zQuery);
15384
  sqlite3_free(zInsert);
15385
}
15386
 
15387
 
15388
/*
15389
** Try to transfer all rows of the schema that match zWhere.  For
15390
** each row, invoke xForEach() on the object defined by that row.
15391
** If an error is encountered while moving forward through the
15392
** sqlite_schema table, try again moving backwards.
15393
*/
15394
static void tryToCloneSchema(
15395
  ShellState *p,
15396
  sqlite3 *newDb,
15397
  const char *zWhere,
15398
  void (*xForEach)(ShellState*,sqlite3*,const char*)
15399
){
15400
  sqlite3_stmt *pQuery = 0;
15401
  char *zQuery = 0;
15402
  int rc;
15403
  const unsigned char *zName;
15404
  const unsigned char *zSql;
15405
  char *zErrMsg = 0;
15406
 
15407
  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
15408
                           " WHERE %s", zWhere);
15409
  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15410
  if( rc ){
15411
    utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
15412
                    sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15413
                    zQuery);
15414
    goto end_schema_xfer;
15415
  }
15416
  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15417
    zName = sqlite3_column_text(pQuery, 0);
15418
    zSql = sqlite3_column_text(pQuery, 1);
15419
    printf("%s... ", zName); fflush(stdout);
15420
    sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
15421
    if( zErrMsg ){
15422
      utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
15423
      sqlite3_free(zErrMsg);
15424
      zErrMsg = 0;
15425
    }
15426
    if( xForEach ){
15427
      xForEach(p, newDb, (const char*)zName);
15428
    }
15429
    printf("done\n");
15430
  }
15431
  if( rc!=SQLITE_DONE ){
15432
    sqlite3_finalize(pQuery);
15433
    sqlite3_free(zQuery);
15434
    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
15435
                             " WHERE %s ORDER BY rowid DESC", zWhere);
15436
    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15437
    if( rc ){
15438
      utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
15439
                      sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15440
                      zQuery);
15441
      goto end_schema_xfer;
15442
    }
15443
    while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15444
      zName = sqlite3_column_text(pQuery, 0);
15445
      zSql = sqlite3_column_text(pQuery, 1);
15446
      printf("%s... ", zName); fflush(stdout);
15447
      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
15448
      if( zErrMsg ){
15449
        utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
15450
        sqlite3_free(zErrMsg);
15451
        zErrMsg = 0;
15452
      }
15453
      if( xForEach ){
15454
        xForEach(p, newDb, (const char*)zName);
15455
      }
15456
      printf("done\n");
15457
    }
15458
  }
15459
end_schema_xfer:
15460
  sqlite3_finalize(pQuery);
15461
  sqlite3_free(zQuery);
15462
}
15463
 
15464
/*
15465
** Open a new database file named "zNewDb".  Try to recover as much information
15466
** as possible out of the main database (which might be corrupt) and write it
15467
** into zNewDb.
15468
*/
15469
static void tryToClone(ShellState *p, const char *zNewDb){
15470
  int rc;
15471
  sqlite3 *newDb = 0;
15472
  if( access(zNewDb,0)==0 ){
15473
    utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
15474
    return;
15475
  }
15476
  rc = sqlite3_open(zNewDb, &newDb);
15477
  if( rc ){
15478
    utf8_printf(stderr, "Cannot create output database: %s\n",
15479
            sqlite3_errmsg(newDb));
15480
  }else{
15481
    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
15482
    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
15483
    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
15484
    tryToCloneSchema(p, newDb, "type!='table'", 0);
15485
    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
15486
    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
15487
  }
15488
  close_db(newDb);
15489
}
15490
 
15491
/*
15492
** Change the output file back to stdout.
15493
**
15494
** If the p->doXdgOpen flag is set, that means the output was being
15495
** redirected to a temporary file named by p->zTempFile.  In that case,
15496
** launch start/open/xdg-open on that temporary file.
15497
*/
15498
static void output_reset(ShellState *p){
15499
  if( p->outfile[0]=='|' ){
15500
#ifndef SQLITE_OMIT_POPEN
15501
    pclose(p->out);
15502
#endif
15503
  }else{
15504
    output_file_close(p->out);
15505
#ifndef SQLITE_NOHAVE_SYSTEM
15506
    if( p->doXdgOpen ){
15507
      const char *zXdgOpenCmd =
15508
#if defined(_WIN32)
15509
      "start";
15510
#elif defined(__APPLE__)
15511
      "open";
15512
#else
15513
      "xdg-open";
15514
#endif
15515
      char *zCmd;
15516
      zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
15517
      if( system(zCmd) ){
15518
        utf8_printf(stderr, "Failed: [%s]\n", zCmd);
15519
      }else{
15520
        /* Give the start/open/xdg-open command some time to get
15521
        ** going before we continue, and potential delete the
15522
        ** p->zTempFile data file out from under it */
15523
        sqlite3_sleep(2000);
15524
      }
15525
      sqlite3_free(zCmd);
15526
      outputModePop(p);
15527
      p->doXdgOpen = 0;
15528
    }
15529
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
15530
  }
15531
  p->outfile[0] = 0;
15532
  p->out = stdout;
15533
}
15534
 
15535
/*
15536
** Run an SQL command and return the single integer result.
15537
*/
15538
static int db_int(ShellState *p, const char *zSql){
15539
  sqlite3_stmt *pStmt;
15540
  int res = 0;
15541
  sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
15542
  if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
15543
    res = sqlite3_column_int(pStmt,0);
15544
  }
15545
  sqlite3_finalize(pStmt);
15546
  return res;
15547
}
15548
 
15549
/*
15550
** Convert a 2-byte or 4-byte big-endian integer into a native integer
15551
*/
15552
static unsigned int get2byteInt(unsigned char *a){
15553
  return (a[0]<<8) + a[1];
15554
}
15555
static unsigned int get4byteInt(unsigned char *a){
15556
  return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
15557
}
15558
 
15559
/*
15560
** Implementation of the ".dbinfo" command.
15561
**
15562
** Return 1 on error, 2 to exit, and 0 otherwise.
15563
*/
15564
static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
15565
  static const struct { const char *zName; int ofst; } aField[] = {
15566
     { "file change counter:",  24  },
15567
     { "database page count:",  28  },
15568
     { "freelist page count:",  36  },
15569
     { "schema cookie:",        40  },
15570
     { "schema format:",        44  },
15571
     { "default cache size:",   48  },
15572
     { "autovacuum top root:",  52  },
15573
     { "incremental vacuum:",   64  },
15574
     { "text encoding:",        56  },
15575
     { "user version:",         60  },
15576
     { "application id:",       68  },
15577
     { "software version:",     96  },
15578
  };
15579
  static const struct { const char *zName; const char *zSql; } aQuery[] = {
15580
     { "number of tables:",
15581
       "SELECT count(*) FROM %s WHERE type='table'" },
15582
     { "number of indexes:",
15583
       "SELECT count(*) FROM %s WHERE type='index'" },
15584
     { "number of triggers:",
15585
       "SELECT count(*) FROM %s WHERE type='trigger'" },
15586
     { "number of views:",
15587
       "SELECT count(*) FROM %s WHERE type='view'" },
15588
     { "schema size:",
15589
       "SELECT total(length(sql)) FROM %s" },
15590
  };
15591
  int i, rc;
15592
  unsigned iDataVersion;
15593
  char *zSchemaTab;
15594
  char *zDb = nArg>=2 ? azArg[1] : "main";
15595
  sqlite3_stmt *pStmt = 0;
15596
  unsigned char aHdr[100];
15597
  open_db(p, 0);
15598
  if( p->db==0 ) return 1;
15599
  rc = sqlite3_prepare_v2(p->db,
15600
             "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
15601
             -1, &pStmt, 0);
15602
  if( rc ){
15603
    utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
15604
    sqlite3_finalize(pStmt);
15605
    return 1;
15606
  }
15607
  sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
15608
  if( sqlite3_step(pStmt)==SQLITE_ROW
15609
   && sqlite3_column_bytes(pStmt,0)>100
15610
  ){
15611
    memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
15612
    sqlite3_finalize(pStmt);
15613
  }else{
15614
    raw_printf(stderr, "unable to read database header\n");
15615
    sqlite3_finalize(pStmt);
15616
    return 1;
15617
  }
15618
  i = get2byteInt(aHdr+16);
15619
  if( i==1 ) i = 65536;
15620
  utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
15621
  utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
15622
  utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
15623
  utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
15624
  for(i=0; i
15625
    int ofst = aField[i].ofst;
15626
    unsigned int val = get4byteInt(aHdr + ofst);
15627
    utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
15628
    switch( ofst ){
15629
      case 56: {
15630
        if( val==1 ) raw_printf(p->out, " (utf8)");
15631
        if( val==2 ) raw_printf(p->out, " (utf16le)");
15632
        if( val==3 ) raw_printf(p->out, " (utf16be)");
15633
      }
15634
    }
15635
    raw_printf(p->out, "\n");
15636
  }
15637
  if( zDb==0 ){
15638
    zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
15639
  }else if( strcmp(zDb,"temp")==0 ){
15640
    zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
15641
  }else{
15642
    zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
15643
  }
15644
  for(i=0; i
15645
    char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
15646
    int val = db_int(p, zSql);
15647
    sqlite3_free(zSql);
15648
    utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
15649
  }
15650
  sqlite3_free(zSchemaTab);
15651
  sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
15652
  utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
15653
  return 0;
15654
}
15655
 
15656
/*
15657
** Print the current sqlite3_errmsg() value to stderr and return 1.
15658
*/
15659
static int shellDatabaseError(sqlite3 *db){
15660
  const char *zErr = sqlite3_errmsg(db);
15661
  utf8_printf(stderr, "Error: %s\n", zErr);
15662
  return 1;
15663
}
15664
 
15665
/*
15666
** Compare the pattern in zGlob[] against the text in z[].  Return TRUE
15667
** if they match and FALSE (0) if they do not match.
15668
**
15669
** Globbing rules:
15670
**
15671
**      '*'       Matches any sequence of zero or more characters.
15672
**
15673
**      '?'       Matches exactly one character.
15674
**
15675
**     [...]      Matches one character from the enclosed list of
15676
**                characters.
15677
**
15678
**     [^...]     Matches one character not in the enclosed list.
15679
**
15680
**      '#'       Matches any sequence of one or more digits with an
15681
**                optional + or - sign in front
15682
**
15683
**      ' '       Any span of whitespace matches any other span of
15684
**                whitespace.
15685
**
15686
** Extra whitespace at the end of z[] is ignored.
15687
*/
15688
static int testcase_glob(const char *zGlob, const char *z){
15689
  int c, c2;
15690
  int invert;
15691
  int seen;
15692
 
15693
  while( (c = (*(zGlob++)))!=0 ){
15694
    if( IsSpace(c) ){
15695
      if( !IsSpace(*z) ) return 0;
15696
      while( IsSpace(*zGlob) ) zGlob++;
15697
      while( IsSpace(*z) ) z++;
15698
    }else if( c=='*' ){
15699
      while( (c=(*(zGlob++))) == '*' || c=='?' ){
15700
        if( c=='?' && (*(z++))==0 ) return 0;
15701
      }
15702
      if( c==0 ){
15703
        return 1;
15704
      }else if( c=='[' ){
15705
        while( *z && testcase_glob(zGlob-1,z)==0 ){
15706
          z++;
15707
        }
15708
        return (*z)!=0;
15709
      }
15710
      while( (c2 = (*(z++)))!=0 ){
15711
        while( c2!=c ){
15712
          c2 = *(z++);
15713
          if( c2==0 ) return 0;
15714
        }
15715
        if( testcase_glob(zGlob,z) ) return 1;
15716
      }
15717
      return 0;
15718
    }else if( c=='?' ){
15719
      if( (*(z++))==0 ) return 0;
15720
    }else if( c=='[' ){
15721
      int prior_c = 0;
15722
      seen = 0;
15723
      invert = 0;
15724
      c = *(z++);
15725
      if( c==0 ) return 0;
15726
      c2 = *(zGlob++);
15727
      if( c2=='^' ){
15728
        invert = 1;
15729
        c2 = *(zGlob++);
15730
      }
15731
      if( c2==']' ){
15732
        if( c==']' ) seen = 1;
15733
        c2 = *(zGlob++);
15734
      }
15735
      while( c2 && c2!=']' ){
15736
        if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
15737
          c2 = *(zGlob++);
15738
          if( c>=prior_c && c<=c2 ) seen = 1;
15739
          prior_c = 0;
15740
        }else{
15741
          if( c==c2 ){
15742
            seen = 1;
15743
          }
15744
          prior_c = c2;
15745
        }
15746
        c2 = *(zGlob++);
15747
      }
15748
      if( c2==0 || (seen ^ invert)==0 ) return 0;
15749
    }else if( c=='#' ){
15750
      if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
15751
      if( !IsDigit(z[0]) ) return 0;
15752
      z++;
15753
      while( IsDigit(z[0]) ){ z++; }
15754
    }else{
15755
      if( c!=(*(z++)) ) return 0;
15756
    }
15757
  }
15758
  while( IsSpace(*z) ){ z++; }
15759
  return *z==0;
15760
}
15761
 
15762
 
15763
/*
15764
** Compare the string as a command-line option with either one or two
15765
** initial "-" characters.
15766
*/
15767
static int optionMatch(const char *zStr, const char *zOpt){
15768
  if( zStr[0]!='-' ) return 0;
15769
  zStr++;
15770
  if( zStr[0]=='-' ) zStr++;
15771
  return strcmp(zStr, zOpt)==0;
15772
}
15773
 
15774
/*
15775
** Delete a file.
15776
*/
15777
int shellDeleteFile(const char *zFilename){
15778
  int rc;
15779
#ifdef _WIN32
15780
  wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
15781
  rc = _wunlink(z);
15782
  sqlite3_free(z);
15783
#else
15784
  rc = unlink(zFilename);
15785
#endif
15786
  return rc;
15787
}
15788
 
15789
/*
15790
** Try to delete the temporary file (if there is one) and free the
15791
** memory used to hold the name of the temp file.
15792
*/
15793
static void clearTempFile(ShellState *p){
15794
  if( p->zTempFile==0 ) return;
15795
  if( p->doXdgOpen ) return;
15796
  if( shellDeleteFile(p->zTempFile) ) return;
15797
  sqlite3_free(p->zTempFile);
15798
  p->zTempFile = 0;
15799
}
15800
 
15801
/*
15802
** Create a new temp file name with the given suffix.
15803
*/
15804
static void newTempFile(ShellState *p, const char *zSuffix){
15805
  clearTempFile(p);
15806
  sqlite3_free(p->zTempFile);
15807
  p->zTempFile = 0;
15808
  if( p->db ){
15809
    sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
15810
  }
15811
  if( p->zTempFile==0 ){
15812
    /* If p->db is an in-memory database then the TEMPFILENAME file-control
15813
    ** will not work and we will need to fallback to guessing */
15814
    char *zTemp;
15815
    sqlite3_uint64 r;
15816
    sqlite3_randomness(sizeof(r), &r);
15817
    zTemp = getenv("TEMP");
15818
    if( zTemp==0 ) zTemp = getenv("TMP");
15819
    if( zTemp==0 ){
15820
#ifdef _WIN32
15821
      zTemp = "\\tmp";
15822
#else
15823
      zTemp = "/tmp";
15824
#endif
15825
    }
15826
    p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
15827
  }else{
15828
    p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
15829
  }
15830
  if( p->zTempFile==0 ){
15831
    raw_printf(stderr, "out of memory\n");
15832
    exit(1);
15833
  }
15834
}
15835
 
15836
 
15837
/*
15838
** The implementation of SQL scalar function fkey_collate_clause(), used
15839
** by the ".lint fkey-indexes" command. This scalar function is always
15840
** called with four arguments - the parent table name, the parent column name,
15841
** the child table name and the child column name.
15842
**
15843
**   fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
15844
**
15845
** If either of the named tables or columns do not exist, this function
15846
** returns an empty string. An empty string is also returned if both tables
15847
** and columns exist but have the same default collation sequence. Or,
15848
** if both exist but the default collation sequences are different, this
15849
** function returns the string " COLLATE ", where
15850
**  is the default collation sequence of the parent column.
15851
*/
15852
static void shellFkeyCollateClause(
15853
  sqlite3_context *pCtx,
15854
  int nVal,
15855
  sqlite3_value **apVal
15856
){
15857
  sqlite3 *db = sqlite3_context_db_handle(pCtx);
15858
  const char *zParent;
15859
  const char *zParentCol;
15860
  const char *zParentSeq;
15861
  const char *zChild;
15862
  const char *zChildCol;
15863
  const char *zChildSeq = 0;  /* Initialize to avoid false-positive warning */
15864
  int rc;
15865
 
15866
  assert( nVal==4 );
15867
  zParent = (const char*)sqlite3_value_text(apVal[0]);
15868
  zParentCol = (const char*)sqlite3_value_text(apVal[1]);
15869
  zChild = (const char*)sqlite3_value_text(apVal[2]);
15870
  zChildCol = (const char*)sqlite3_value_text(apVal[3]);
15871
 
15872
  sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
15873
  rc = sqlite3_table_column_metadata(
15874
      db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
15875
  );
15876
  if( rc==SQLITE_OK ){
15877
    rc = sqlite3_table_column_metadata(
15878
        db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
15879
    );
15880
  }
15881
 
15882
  if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
15883
    char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
15884
    sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
15885
    sqlite3_free(z);
15886
  }
15887
}
15888
 
15889
 
15890
/*
15891
** The implementation of dot-command ".lint fkey-indexes".
15892
*/
15893
static int lintFkeyIndexes(
15894
  ShellState *pState,             /* Current shell tool state */
15895
  char **azArg,                   /* Array of arguments passed to dot command */
15896
  int nArg                        /* Number of entries in azArg[] */
15897
){
15898
  sqlite3 *db = pState->db;       /* Database handle to query "main" db of */
15899
  FILE *out = pState->out;        /* Stream to write non-error output to */
15900
  int bVerbose = 0;               /* If -verbose is present */
15901
  int bGroupByParent = 0;         /* If -groupbyparent is present */
15902
  int i;                          /* To iterate through azArg[] */
15903
  const char *zIndent = "";       /* How much to indent CREATE INDEX by */
15904
  int rc;                         /* Return code */
15905
  sqlite3_stmt *pSql = 0;         /* Compiled version of SQL statement below */
15906
 
15907
  /*
15908
  ** This SELECT statement returns one row for each foreign key constraint
15909
  ** in the schema of the main database. The column values are:
15910
  **
15911
  ** 0. The text of an SQL statement similar to:
15912
  **
15913
  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
15914
  **
15915
  **    This SELECT is similar to the one that the foreign keys implementation
15916
  **    needs to run internally on child tables. If there is an index that can
15917
  **    be used to optimize this query, then it can also be used by the FK
15918
  **    implementation to optimize DELETE or UPDATE statements on the parent
15919
  **    table.
15920
  **
15921
  ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
15922
  **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
15923
  **    contains an index that can be used to optimize the query.
15924
  **
15925
  ** 2. Human readable text that describes the child table and columns. e.g.
15926
  **
15927
  **       "child_table(child_key1, child_key2)"
15928
  **
15929
  ** 3. Human readable text that describes the parent table and columns. e.g.
15930
  **
15931
  **       "parent_table(parent_key1, parent_key2)"
15932
  **
15933
  ** 4. A full CREATE INDEX statement for an index that could be used to
15934
  **    optimize DELETE or UPDATE statements on the parent table. e.g.
15935
  **
15936
  **       "CREATE INDEX child_table_child_key ON child_table(child_key)"
15937
  **
15938
  ** 5. The name of the parent table.
15939
  **
15940
  ** These six values are used by the C logic below to generate the report.
15941
  */
15942
  const char *zSql =
15943
  "SELECT "
15944
    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
15945
    "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
15946
    "  || fkey_collate_clause("
15947
    "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
15948
    ", "
15949
    "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
15950
    "  || group_concat('*=?', ' AND ') || ')'"
15951
    ", "
15952
    "     s.name  || '(' || group_concat(f.[from],  ', ') || ')'"
15953
    ", "
15954
    "     f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
15955
    ", "
15956
    "     'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
15957
    "  || ' ON ' || quote(s.name) || '('"
15958
    "  || group_concat(quote(f.[from]) ||"
15959
    "        fkey_collate_clause("
15960
    "          f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
15961
    "  || ');'"
15962
    ", "
15963
    "     f.[table] "
15964
    "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
15965
    "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
15966
    "GROUP BY s.name, f.id "
15967
    "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
15968
  ;
15969
  const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
15970
 
15971
  for(i=2; i
15972
    int n = strlen30(azArg[i]);
15973
    if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
15974
      bVerbose = 1;
15975
    }
15976
    else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
15977
      bGroupByParent = 1;
15978
      zIndent = "    ";
15979
    }
15980
    else{
15981
      raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
15982
          azArg[0], azArg[1]
15983
      );
15984
      return SQLITE_ERROR;
15985
    }
15986
  }
15987
 
15988
  /* Register the fkey_collate_clause() SQL function */
15989
  rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
15990
      0, shellFkeyCollateClause, 0, 0
15991
  );
15992
 
15993
 
15994
  if( rc==SQLITE_OK ){
15995
    rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
15996
  }
15997
  if( rc==SQLITE_OK ){
15998
    sqlite3_bind_int(pSql, 1, bGroupByParent);
15999
  }
16000
 
16001
  if( rc==SQLITE_OK ){
16002
    int rc2;
16003
    char *zPrev = 0;
16004
    while( SQLITE_ROW==sqlite3_step(pSql) ){
16005
      int res = -1;
16006
      sqlite3_stmt *pExplain = 0;
16007
      const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
16008
      const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
16009
      const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
16010
      const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
16011
      const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
16012
      const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
16013
 
16014
      rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
16015
      if( rc!=SQLITE_OK ) break;
16016
      if( SQLITE_ROW==sqlite3_step(pExplain) ){
16017
        const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
16018
        res = (
16019
              0==sqlite3_strglob(zGlob, zPlan)
16020
           || 0==sqlite3_strglob(zGlobIPK, zPlan)
16021
        );
16022
      }
16023
      rc = sqlite3_finalize(pExplain);
16024
      if( rc!=SQLITE_OK ) break;
16025
 
16026
      if( res<0 ){
16027
        raw_printf(stderr, "Error: internal error");
16028
        break;
16029
      }else{
16030
        if( bGroupByParent
16031
        && (bVerbose || res==0)
16032
        && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
16033
        ){
16034
          raw_printf(out, "-- Parent table %s\n", zParent);
16035
          sqlite3_free(zPrev);
16036
          zPrev = sqlite3_mprintf("%s", zParent);
16037
        }
16038
 
16039
        if( res==0 ){
16040
          raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
16041
        }else if( bVerbose ){
16042
          raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
16043
              zIndent, zFrom, zTarget
16044
          );
16045
        }
16046
      }
16047
    }
16048
    sqlite3_free(zPrev);
16049
 
16050
    if( rc!=SQLITE_OK ){
16051
      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16052
    }
16053
 
16054
    rc2 = sqlite3_finalize(pSql);
16055
    if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
16056
      rc = rc2;
16057
      raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16058
    }
16059
  }else{
16060
    raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16061
  }
16062
 
16063
  return rc;
16064
}
16065
 
16066
/*
16067
** Implementation of ".lint" dot command.
16068
*/
16069
static int lintDotCommand(
16070
  ShellState *pState,             /* Current shell tool state */
16071
  char **azArg,                   /* Array of arguments passed to dot command */
16072
  int nArg                        /* Number of entries in azArg[] */
16073
){
16074
  int n;
16075
  n = (nArg>=2 ? strlen30(azArg[1]) : 0);
16076
  if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
16077
  return lintFkeyIndexes(pState, azArg, nArg);
16078
 
16079
 usage:
16080
  raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
16081
  raw_printf(stderr, "Where sub-commands are:\n");
16082
  raw_printf(stderr, "    fkey-indexes\n");
16083
  return SQLITE_ERROR;
16084
}
16085
 
16086
#if !defined SQLITE_OMIT_VIRTUALTABLE
16087
static void shellPrepare(
16088
  sqlite3 *db,
16089
  int *pRc,
16090
  const char *zSql,
16091
  sqlite3_stmt **ppStmt
16092
){
16093
  *ppStmt = 0;
16094
  if( *pRc==SQLITE_OK ){
16095
    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
16096
    if( rc!=SQLITE_OK ){
16097
      raw_printf(stderr, "sql error: %s (%d)\n",
16098
          sqlite3_errmsg(db), sqlite3_errcode(db)
16099
      );
16100
      *pRc = rc;
16101
    }
16102
  }
16103
}
16104
 
16105
/*
16106
** Create a prepared statement using printf-style arguments for the SQL.
16107
**
16108
** This routine is could be marked "static".  But it is not always used,
16109
** depending on compile-time options.  By omitting the "static", we avoid
16110
** nuisance compiler warnings about "defined but not used".
16111
*/
16112
void shellPreparePrintf(
16113
  sqlite3 *db,
16114
  int *pRc,
16115
  sqlite3_stmt **ppStmt,
16116
  const char *zFmt,
16117
  ...
16118
){
16119
  *ppStmt = 0;
16120
  if( *pRc==SQLITE_OK ){
16121
    va_list ap;
16122
    char *z;
16123
    va_start(ap, zFmt);
16124
    z = sqlite3_vmprintf(zFmt, ap);
16125
    va_end(ap);
16126
    if( z==0 ){
16127
      *pRc = SQLITE_NOMEM;
16128
    }else{
16129
      shellPrepare(db, pRc, z, ppStmt);
16130
      sqlite3_free(z);
16131
    }
16132
  }
16133
}
16134
 
16135
/* Finalize the prepared statement created using shellPreparePrintf().
16136
**
16137
** This routine is could be marked "static".  But it is not always used,
16138
** depending on compile-time options.  By omitting the "static", we avoid
16139
** nuisance compiler warnings about "defined but not used".
16140
*/
16141
void shellFinalize(
16142
  int *pRc,
16143
  sqlite3_stmt *pStmt
16144
){
16145
  if( pStmt ){
16146
    sqlite3 *db = sqlite3_db_handle(pStmt);
16147
    int rc = sqlite3_finalize(pStmt);
16148
    if( *pRc==SQLITE_OK ){
16149
      if( rc!=SQLITE_OK ){
16150
        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16151
      }
16152
      *pRc = rc;
16153
    }
16154
  }
16155
}
16156
 
16157
/* Reset the prepared statement created using shellPreparePrintf().
16158
**
16159
** This routine is could be marked "static".  But it is not always used,
16160
** depending on compile-time options.  By omitting the "static", we avoid
16161
** nuisance compiler warnings about "defined but not used".
16162
*/
16163
void shellReset(
16164
  int *pRc,
16165
  sqlite3_stmt *pStmt
16166
){
16167
  int rc = sqlite3_reset(pStmt);
16168
  if( *pRc==SQLITE_OK ){
16169
    if( rc!=SQLITE_OK ){
16170
      sqlite3 *db = sqlite3_db_handle(pStmt);
16171
      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16172
    }
16173
    *pRc = rc;
16174
  }
16175
}
16176
#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
16177
 
16178
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16179
/******************************************************************************
16180
** The ".archive" or ".ar" command.
16181
*/
16182
/*
16183
** Structure representing a single ".ar" command.
16184
*/
16185
typedef struct ArCommand ArCommand;
16186
struct ArCommand {
16187
  u8 eCmd;                        /* An AR_CMD_* value */
16188
  u8 bVerbose;                    /* True if --verbose */
16189
  u8 bZip;                        /* True if the archive is a ZIP */
16190
  u8 bDryRun;                     /* True if --dry-run */
16191
  u8 bAppend;                     /* True if --append */
16192
  u8 fromCmdLine;                 /* Run from -A instead of .archive */
16193
  int nArg;                       /* Number of command arguments */
16194
  char *zSrcTable;                /* "sqlar", "zipfile($file)" or "zip" */
16195
  const char *zFile;              /* --file argument, or NULL */
16196
  const char *zDir;               /* --directory argument, or NULL */
16197
  char **azArg;                   /* Array of command arguments */
16198
  ShellState *p;                  /* Shell state */
16199
  sqlite3 *db;                    /* Database containing the archive */
16200
};
16201
 
16202
/*
16203
** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
16204
*/
16205
static int arUsage(FILE *f){
16206
  showHelp(f,"archive");
16207
  return SQLITE_ERROR;
16208
}
16209
 
16210
/*
16211
** Print an error message for the .ar command to stderr and return
16212
** SQLITE_ERROR.
16213
*/
16214
static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
16215
  va_list ap;
16216
  char *z;
16217
  va_start(ap, zFmt);
16218
  z = sqlite3_vmprintf(zFmt, ap);
16219
  va_end(ap);
16220
  utf8_printf(stderr, "Error: %s\n", z);
16221
  if( pAr->fromCmdLine ){
16222
    utf8_printf(stderr, "Use \"-A\" for more help\n");
16223
  }else{
16224
    utf8_printf(stderr, "Use \".archive --help\" for more help\n");
16225
  }
16226
  sqlite3_free(z);
16227
  return SQLITE_ERROR;
16228
}
16229
 
16230
/*
16231
** Values for ArCommand.eCmd.
16232
*/
16233
#define AR_CMD_CREATE       1
16234
#define AR_CMD_UPDATE       2
16235
#define AR_CMD_INSERT       3
16236
#define AR_CMD_EXTRACT      4
16237
#define AR_CMD_LIST         5
16238
#define AR_CMD_HELP         6
16239
 
16240
/*
16241
** Other (non-command) switches.
16242
*/
16243
#define AR_SWITCH_VERBOSE     7
16244
#define AR_SWITCH_FILE        8
16245
#define AR_SWITCH_DIRECTORY   9
16246
#define AR_SWITCH_APPEND     10
16247
#define AR_SWITCH_DRYRUN     11
16248
 
16249
static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
16250
  switch( eSwitch ){
16251
    case AR_CMD_CREATE:
16252
    case AR_CMD_EXTRACT:
16253
    case AR_CMD_LIST:
16254
    case AR_CMD_UPDATE:
16255
    case AR_CMD_INSERT:
16256
    case AR_CMD_HELP:
16257
      if( pAr->eCmd ){
16258
        return arErrorMsg(pAr, "multiple command options");
16259
      }
16260
      pAr->eCmd = eSwitch;
16261
      break;
16262
 
16263
    case AR_SWITCH_DRYRUN:
16264
      pAr->bDryRun = 1;
16265
      break;
16266
    case AR_SWITCH_VERBOSE:
16267
      pAr->bVerbose = 1;
16268
      break;
16269
    case AR_SWITCH_APPEND:
16270
      pAr->bAppend = 1;
16271
      /* Fall thru into --file */
16272
    case AR_SWITCH_FILE:
16273
      pAr->zFile = zArg;
16274
      break;
16275
    case AR_SWITCH_DIRECTORY:
16276
      pAr->zDir = zArg;
16277
      break;
16278
  }
16279
 
16280
  return SQLITE_OK;
16281
}
16282
 
16283
/*
16284
** Parse the command line for an ".ar" command. The results are written into
16285
** structure (*pAr). SQLITE_OK is returned if the command line is parsed
16286
** successfully, otherwise an error message is written to stderr and
16287
** SQLITE_ERROR returned.
16288
*/
16289
static int arParseCommand(
16290
  char **azArg,                   /* Array of arguments passed to dot command */
16291
  int nArg,                       /* Number of entries in azArg[] */
16292
  ArCommand *pAr                  /* Populate this object */
16293
){
16294
  struct ArSwitch {
16295
    const char *zLong;
16296
    char cShort;
16297
    u8 eSwitch;
16298
    u8 bArg;
16299
  } aSwitch[] = {
16300
    { "create",    'c', AR_CMD_CREATE,       0 },
16301
    { "extract",   'x', AR_CMD_EXTRACT,      0 },
16302
    { "insert",    'i', AR_CMD_INSERT,       0 },
16303
    { "list",      't', AR_CMD_LIST,         0 },
16304
    { "update",    'u', AR_CMD_UPDATE,       0 },
16305
    { "help",      'h', AR_CMD_HELP,         0 },
16306
    { "verbose",   'v', AR_SWITCH_VERBOSE,   0 },
16307
    { "file",      'f', AR_SWITCH_FILE,      1 },
16308
    { "append",    'a', AR_SWITCH_APPEND,    1 },
16309
    { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
16310
    { "dryrun",    'n', AR_SWITCH_DRYRUN,    0 },
16311
  };
16312
  int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
16313
  struct ArSwitch *pEnd = &aSwitch[nSwitch];
16314
 
16315
  if( nArg<=1 ){
16316
    utf8_printf(stderr, "Wrong number of arguments.  Usage:\n");
16317
    return arUsage(stderr);
16318
  }else{
16319
    char *z = azArg[1];
16320
    if( z[0]!='-' ){
16321
      /* Traditional style [tar] invocation */
16322
      int i;
16323
      int iArg = 2;
16324
      for(i=0; z[i]; i++){
16325
        const char *zArg = 0;
16326
        struct ArSwitch *pOpt;
16327
        for(pOpt=&aSwitch[0]; pOpt
16328
          if( z[i]==pOpt->cShort ) break;
16329
        }
16330
        if( pOpt==pEnd ){
16331
          return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
16332
        }
16333
        if( pOpt->bArg ){
16334
          if( iArg>=nArg ){
16335
            return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
16336
          }
16337
          zArg = azArg[iArg++];
16338
        }
16339
        if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
16340
      }
16341
      pAr->nArg = nArg-iArg;
16342
      if( pAr->nArg>0 ){
16343
        pAr->azArg = &azArg[iArg];
16344
      }
16345
    }else{
16346
      /* Non-traditional invocation */
16347
      int iArg;
16348
      for(iArg=1; iArg
16349
        int n;
16350
        z = azArg[iArg];
16351
        if( z[0]!='-' ){
16352
          /* All remaining command line words are command arguments. */
16353
          pAr->azArg = &azArg[iArg];
16354
          pAr->nArg = nArg-iArg;
16355
          break;
16356
        }
16357
        n = strlen30(z);
16358
 
16359
        if( z[1]!='-' ){
16360
          int i;
16361
          /* One or more short options */
16362
          for(i=1; i
16363
            const char *zArg = 0;
16364
            struct ArSwitch *pOpt;
16365
            for(pOpt=&aSwitch[0]; pOpt
16366
              if( z[i]==pOpt->cShort ) break;
16367
            }
16368
            if( pOpt==pEnd ){
16369
              return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
16370
            }
16371
            if( pOpt->bArg ){
16372
              if( i<(n-1) ){
16373
                zArg = &z[i+1];
16374
                i = n;
16375
              }else{
16376
                if( iArg>=(nArg-1) ){
16377
                  return arErrorMsg(pAr, "option requires an argument: %c",
16378
                                    z[i]);
16379
                }
16380
                zArg = azArg[++iArg];
16381
              }
16382
            }
16383
            if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
16384
          }
16385
        }else if( z[2]=='\0' ){
16386
          /* A -- option, indicating that all remaining command line words
16387
          ** are command arguments.  */
16388
          pAr->azArg = &azArg[iArg+1];
16389
          pAr->nArg = nArg-iArg-1;
16390
          break;
16391
        }else{
16392
          /* A long option */
16393
          const char *zArg = 0;             /* Argument for option, if any */
16394
          struct ArSwitch *pMatch = 0;      /* Matching option */
16395
          struct ArSwitch *pOpt;            /* Iterator */
16396
          for(pOpt=&aSwitch[0]; pOpt
16397
            const char *zLong = pOpt->zLong;
16398
            if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
16399
              if( pMatch ){
16400
                return arErrorMsg(pAr, "ambiguous option: %s",z);
16401
              }else{
16402
                pMatch = pOpt;
16403
              }
16404
            }
16405
          }
16406
 
16407
          if( pMatch==0 ){
16408
            return arErrorMsg(pAr, "unrecognized option: %s", z);
16409
          }
16410
          if( pMatch->bArg ){
16411
            if( iArg>=(nArg-1) ){
16412
              return arErrorMsg(pAr, "option requires an argument: %s", z);
16413
            }
16414
            zArg = azArg[++iArg];
16415
          }
16416
          if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
16417
        }
16418
      }
16419
    }
16420
  }
16421
 
16422
  return SQLITE_OK;
16423
}
16424
 
16425
/*
16426
** This function assumes that all arguments within the ArCommand.azArg[]
16427
** array refer to archive members, as for the --extract or --list commands.
16428
** It checks that each of them are present. If any specified file is not
16429
** present in the archive, an error is printed to stderr and an error
16430
** code returned. Otherwise, if all specified arguments are present in
16431
** the archive, SQLITE_OK is returned.
16432
**
16433
** This function strips any trailing '/' characters from each argument.
16434
** This is consistent with the way the [tar] command seems to work on
16435
** Linux.
16436
*/
16437
static int arCheckEntries(ArCommand *pAr){
16438
  int rc = SQLITE_OK;
16439
  if( pAr->nArg ){
16440
    int i, j;
16441
    sqlite3_stmt *pTest = 0;
16442
 
16443
    shellPreparePrintf(pAr->db, &rc, &pTest,
16444
        "SELECT name FROM %s WHERE name=$name",
16445
        pAr->zSrcTable
16446
    );
16447
    j = sqlite3_bind_parameter_index(pTest, "$name");
16448
    for(i=0; inArg && rc==SQLITE_OK; i++){
16449
      char *z = pAr->azArg[i];
16450
      int n = strlen30(z);
16451
      int bOk = 0;
16452
      while( n>0 && z[n-1]=='/' ) n--;
16453
      z[n] = '\0';
16454
      sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
16455
      if( SQLITE_ROW==sqlite3_step(pTest) ){
16456
        bOk = 1;
16457
      }
16458
      shellReset(&rc, pTest);
16459
      if( rc==SQLITE_OK && bOk==0 ){
16460
        utf8_printf(stderr, "not found in archive: %s\n", z);
16461
        rc = SQLITE_ERROR;
16462
      }
16463
    }
16464
    shellFinalize(&rc, pTest);
16465
  }
16466
  return rc;
16467
}
16468
 
16469
/*
16470
** Format a WHERE clause that can be used against the "sqlar" table to
16471
** identify all archive members that match the command arguments held
16472
** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
16473
** The caller is responsible for eventually calling sqlite3_free() on
16474
** any non-NULL (*pzWhere) value.
16475
*/
16476
static void arWhereClause(
16477
  int *pRc,
16478
  ArCommand *pAr,
16479
  char **pzWhere                  /* OUT: New WHERE clause */
16480
){
16481
  char *zWhere = 0;
16482
  if( *pRc==SQLITE_OK ){
16483
    if( pAr->nArg==0 ){
16484
      zWhere = sqlite3_mprintf("1");
16485
    }else{
16486
      int i;
16487
      const char *zSep = "";
16488
      for(i=0; inArg; i++){
16489
        const char *z = pAr->azArg[i];
16490
        zWhere = sqlite3_mprintf(
16491
          "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
16492
          zWhere, zSep, z, strlen30(z)+1, z
16493
        );
16494
        if( zWhere==0 ){
16495
          *pRc = SQLITE_NOMEM;
16496
          break;
16497
        }
16498
        zSep = " OR ";
16499
      }
16500
    }
16501
  }
16502
  *pzWhere = zWhere;
16503
}
16504
 
16505
/*
16506
** Implementation of .ar "lisT" command.
16507
*/
16508
static int arListCommand(ArCommand *pAr){
16509
  const char *zSql = "SELECT %s FROM %s WHERE %s";
16510
  const char *azCols[] = {
16511
    "name",
16512
    "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
16513
  };
16514
 
16515
  char *zWhere = 0;
16516
  sqlite3_stmt *pSql = 0;
16517
  int rc;
16518
 
16519
  rc = arCheckEntries(pAr);
16520
  arWhereClause(&rc, pAr, &zWhere);
16521
 
16522
  shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
16523
                     pAr->zSrcTable, zWhere);
16524
  if( pAr->bDryRun ){
16525
    utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
16526
  }else{
16527
    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
16528
      if( pAr->bVerbose ){
16529
        utf8_printf(pAr->p->out, "%s % 10d  %s  %s\n",
16530
            sqlite3_column_text(pSql, 0),
16531
            sqlite3_column_int(pSql, 1),
16532
            sqlite3_column_text(pSql, 2),
16533
            sqlite3_column_text(pSql, 3)
16534
        );
16535
      }else{
16536
        utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
16537
      }
16538
    }
16539
  }
16540
  shellFinalize(&rc, pSql);
16541
  sqlite3_free(zWhere);
16542
  return rc;
16543
}
16544
 
16545
 
16546
/*
16547
** Implementation of .ar "eXtract" command.
16548
*/
16549
static int arExtractCommand(ArCommand *pAr){
16550
  const char *zSql1 =
16551
    "SELECT "
16552
    " ($dir || name),"
16553
    " writefile(($dir || name), %s, mode, mtime) "
16554
    "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
16555
    " AND name NOT GLOB '*..[/\\]*'";
16556
 
16557
  const char *azExtraArg[] = {
16558
    "sqlar_uncompress(data, sz)",
16559
    "data"
16560
  };
16561
 
16562
  sqlite3_stmt *pSql = 0;
16563
  int rc = SQLITE_OK;
16564
  char *zDir = 0;
16565
  char *zWhere = 0;
16566
  int i, j;
16567
 
16568
  /* If arguments are specified, check that they actually exist within
16569
  ** the archive before proceeding. And formulate a WHERE clause to
16570
  ** match them.  */
16571
  rc = arCheckEntries(pAr);
16572
  arWhereClause(&rc, pAr, &zWhere);
16573
 
16574
  if( rc==SQLITE_OK ){
16575
    if( pAr->zDir ){
16576
      zDir = sqlite3_mprintf("%s/", pAr->zDir);
16577
    }else{
16578
      zDir = sqlite3_mprintf("");
16579
    }
16580
    if( zDir==0 ) rc = SQLITE_NOMEM;
16581
  }
16582
 
16583
  shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
16584
      azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
16585
  );
16586
 
16587
  if( rc==SQLITE_OK ){
16588
    j = sqlite3_bind_parameter_index(pSql, "$dir");
16589
    sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
16590
 
16591
    /* Run the SELECT statement twice. The first time, writefile() is called
16592
    ** for all archive members that should be extracted. The second time,
16593
    ** only for the directories. This is because the timestamps for
16594
    ** extracted directories must be reset after they are populated (as
16595
    ** populating them changes the timestamp).  */
16596
    for(i=0; i<2; i++){
16597
      j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
16598
      sqlite3_bind_int(pSql, j, i);
16599
      if( pAr->bDryRun ){
16600
        utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
16601
      }else{
16602
        while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
16603
          if( i==0 && pAr->bVerbose ){
16604
            utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
16605
          }
16606
        }
16607
      }
16608
      shellReset(&rc, pSql);
16609
    }
16610
    shellFinalize(&rc, pSql);
16611
  }
16612
 
16613
  sqlite3_free(zDir);
16614
  sqlite3_free(zWhere);
16615
  return rc;
16616
}
16617
 
16618
/*
16619
** Run the SQL statement in zSql.  Or if doing a --dryrun, merely print it out.
16620
*/
16621
static int arExecSql(ArCommand *pAr, const char *zSql){
16622
  int rc;
16623
  if( pAr->bDryRun ){
16624
    utf8_printf(pAr->p->out, "%s\n", zSql);
16625
    rc = SQLITE_OK;
16626
  }else{
16627
    char *zErr = 0;
16628
    rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
16629
    if( zErr ){
16630
      utf8_printf(stdout, "ERROR: %s\n", zErr);
16631
      sqlite3_free(zErr);
16632
    }
16633
  }
16634
  return rc;
16635
}
16636
 
16637
 
16638
/*
16639
** Implementation of .ar "create", "insert", and "update" commands.
16640
**
16641
**     create    ->     Create a new SQL archive
16642
**     insert    ->     Insert or reinsert all files listed
16643
**     update    ->     Insert files that have changed or that were not
16644
**                      previously in the archive
16645
**
16646
** Create the "sqlar" table in the database if it does not already exist.
16647
** Then add each file in the azFile[] array to the archive. Directories
16648
** are added recursively. If argument bVerbose is non-zero, a message is
16649
** printed on stdout for each file archived.
16650
**
16651
** The create command is the same as update, except that it drops
16652
** any existing "sqlar" table before beginning.  The "insert" command
16653
** always overwrites every file named on the command-line, where as
16654
** "update" only overwrites if the size or mtime or mode has changed.
16655
*/
16656
static int arCreateOrUpdateCommand(
16657
  ArCommand *pAr,                 /* Command arguments and options */
16658
  int bUpdate,                    /* true for a --create. */
16659
  int bOnlyIfChanged              /* Only update if file has changed */
16660
){
16661
  const char *zCreate =
16662
      "CREATE TABLE IF NOT EXISTS sqlar(\n"
16663
      "  name TEXT PRIMARY KEY,  -- name of the file\n"
16664
      "  mode INT,               -- access permissions\n"
16665
      "  mtime INT,              -- last modification time\n"
16666
      "  sz INT,                 -- original file size\n"
16667
      "  data BLOB               -- compressed content\n"
16668
      ")";
16669
  const char *zDrop = "DROP TABLE IF EXISTS sqlar";
16670
  const char *zInsertFmt[2] = {
16671
     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
16672
     "  SELECT\n"
16673
     "    %s,\n"
16674
     "    mode,\n"
16675
     "    mtime,\n"
16676
     "    CASE substr(lsmode(mode),1,1)\n"
16677
     "      WHEN '-' THEN length(data)\n"
16678
     "      WHEN 'd' THEN 0\n"
16679
     "      ELSE -1 END,\n"
16680
     "    sqlar_compress(data)\n"
16681
     "  FROM fsdir(%Q,%Q) AS disk\n"
16682
     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
16683
     ,
16684
     "REPLACE INTO %s(name,mode,mtime,data)\n"
16685
     "  SELECT\n"
16686
     "    %s,\n"
16687
     "    mode,\n"
16688
     "    mtime,\n"
16689
     "    data\n"
16690
     "  FROM fsdir(%Q,%Q) AS disk\n"
16691
     "  WHERE lsmode(mode) NOT LIKE '?%%'%s;"
16692
  };
16693
  int i;                          /* For iterating through azFile[] */
16694
  int rc;                         /* Return code */
16695
  const char *zTab = 0;           /* SQL table into which to insert */
16696
  char *zSql;
16697
  char zTemp[50];
16698
  char *zExists = 0;
16699
 
16700
  arExecSql(pAr, "PRAGMA page_size=512");
16701
  rc = arExecSql(pAr, "SAVEPOINT ar;");
16702
  if( rc!=SQLITE_OK ) return rc;
16703
  zTemp[0] = 0;
16704
  if( pAr->bZip ){
16705
    /* Initialize the zipfile virtual table, if necessary */
16706
    if( pAr->zFile ){
16707
      sqlite3_uint64 r;
16708
      sqlite3_randomness(sizeof(r),&r);
16709
      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
16710
      zTab = zTemp;
16711
      zSql = sqlite3_mprintf(
16712
         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
16713
         zTab, pAr->zFile
16714
      );
16715
      rc = arExecSql(pAr, zSql);
16716
      sqlite3_free(zSql);
16717
    }else{
16718
      zTab = "zip";
16719
    }
16720
  }else{
16721
    /* Initialize the table for an SQLAR */
16722
    zTab = "sqlar";
16723
    if( bUpdate==0 ){
16724
      rc = arExecSql(pAr, zDrop);
16725
      if( rc!=SQLITE_OK ) goto end_ar_transaction;
16726
    }
16727
    rc = arExecSql(pAr, zCreate);
16728
  }
16729
  if( bOnlyIfChanged ){
16730
    zExists = sqlite3_mprintf(
16731
      " AND NOT EXISTS("
16732
          "SELECT 1 FROM %s AS mem"
16733
          " WHERE mem.name=disk.name"
16734
          " AND mem.mtime=disk.mtime"
16735
          " AND mem.mode=disk.mode)", zTab);
16736
  }else{
16737
    zExists = sqlite3_mprintf("");
16738
  }
16739
  if( zExists==0 ) rc = SQLITE_NOMEM;
16740
  for(i=0; inArg && rc==SQLITE_OK; i++){
16741
    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
16742
        pAr->bVerbose ? "shell_putsnl(name)" : "name",
16743
        pAr->azArg[i], pAr->zDir, zExists);
16744
    rc = arExecSql(pAr, zSql2);
16745
    sqlite3_free(zSql2);
16746
  }
16747
end_ar_transaction:
16748
  if( rc!=SQLITE_OK ){
16749
    sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
16750
  }else{
16751
    rc = arExecSql(pAr, "RELEASE ar;");
16752
    if( pAr->bZip && pAr->zFile ){
16753
      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
16754
      arExecSql(pAr, zSql);
16755
      sqlite3_free(zSql);
16756
    }
16757
  }
16758
  sqlite3_free(zExists);
16759
  return rc;
16760
}
16761
 
16762
/*
16763
** Implementation of ".ar" dot command.
16764
*/
16765
static int arDotCommand(
16766
  ShellState *pState,          /* Current shell tool state */
16767
  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
16768
  char **azArg,                /* Array of arguments passed to dot command */
16769
  int nArg                     /* Number of entries in azArg[] */
16770
){
16771
  ArCommand cmd;
16772
  int rc;
16773
  memset(&cmd, 0, sizeof(cmd));
16774
  cmd.fromCmdLine = fromCmdLine;
16775
  rc = arParseCommand(azArg, nArg, &cmd);
16776
  if( rc==SQLITE_OK ){
16777
    int eDbType = SHELL_OPEN_UNSPEC;
16778
    cmd.p = pState;
16779
    cmd.db = pState->db;
16780
    if( cmd.zFile ){
16781
      eDbType = deduceDatabaseType(cmd.zFile, 1);
16782
    }else{
16783
      eDbType = pState->openMode;
16784
    }
16785
    if( eDbType==SHELL_OPEN_ZIPFILE ){
16786
      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
16787
        if( cmd.zFile==0 ){
16788
          cmd.zSrcTable = sqlite3_mprintf("zip");
16789
        }else{
16790
          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
16791
        }
16792
      }
16793
      cmd.bZip = 1;
16794
    }else if( cmd.zFile ){
16795
      int flags;
16796
      if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
16797
      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
16798
           || cmd.eCmd==AR_CMD_UPDATE ){
16799
        flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
16800
      }else{
16801
        flags = SQLITE_OPEN_READONLY;
16802
      }
16803
      cmd.db = 0;
16804
      if( cmd.bDryRun ){
16805
        utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
16806
             eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
16807
      }
16808
      rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
16809
             eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
16810
      if( rc!=SQLITE_OK ){
16811
        utf8_printf(stderr, "cannot open file: %s (%s)\n",
16812
            cmd.zFile, sqlite3_errmsg(cmd.db)
16813
        );
16814
        goto end_ar_command;
16815
      }
16816
      sqlite3_fileio_init(cmd.db, 0, 0);
16817
      sqlite3_sqlar_init(cmd.db, 0, 0);
16818
      sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
16819
                              shellPutsFunc, 0, 0);
16820
 
16821
    }
16822
    if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
16823
      if( cmd.eCmd!=AR_CMD_CREATE
16824
       && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
16825
      ){
16826
        utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
16827
        rc = SQLITE_ERROR;
16828
        goto end_ar_command;
16829
      }
16830
      cmd.zSrcTable = sqlite3_mprintf("sqlar");
16831
    }
16832
 
16833
    switch( cmd.eCmd ){
16834
      case AR_CMD_CREATE:
16835
        rc = arCreateOrUpdateCommand(&cmd, 0, 0);
16836
        break;
16837
 
16838
      case AR_CMD_EXTRACT:
16839
        rc = arExtractCommand(&cmd);
16840
        break;
16841
 
16842
      case AR_CMD_LIST:
16843
        rc = arListCommand(&cmd);
16844
        break;
16845
 
16846
      case AR_CMD_HELP:
16847
        arUsage(pState->out);
16848
        break;
16849
 
16850
      case AR_CMD_INSERT:
16851
        rc = arCreateOrUpdateCommand(&cmd, 1, 0);
16852
        break;
16853
 
16854
      default:
16855
        assert( cmd.eCmd==AR_CMD_UPDATE );
16856
        rc = arCreateOrUpdateCommand(&cmd, 1, 1);
16857
        break;
16858
    }
16859
  }
16860
end_ar_command:
16861
  if( cmd.db!=pState->db ){
16862
    close_db(cmd.db);
16863
  }
16864
  sqlite3_free(cmd.zSrcTable);
16865
 
16866
  return rc;
16867
}
16868
/* End of the ".archive" or ".ar" command logic
16869
*******************************************************************************/
16870
#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
16871
 
16872
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
16873
/*
16874
** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
16875
** Otherwise, the SQL statement or statements in zSql are executed using
16876
** database connection db and the error code written to *pRc before
16877
** this function returns.
16878
*/
16879
static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
16880
  int rc = *pRc;
16881
  if( rc==SQLITE_OK ){
16882
    char *zErr = 0;
16883
    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
16884
    if( rc!=SQLITE_OK ){
16885
      raw_printf(stderr, "SQL error: %s\n", zErr);
16886
    }
16887
    *pRc = rc;
16888
  }
16889
}
16890
 
16891
/*
16892
** Like shellExec(), except that zFmt is a printf() style format string.
16893
*/
16894
static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
16895
  char *z = 0;
16896
  if( *pRc==SQLITE_OK ){
16897
    va_list ap;
16898
    va_start(ap, zFmt);
16899
    z = sqlite3_vmprintf(zFmt, ap);
16900
    va_end(ap);
16901
    if( z==0 ){
16902
      *pRc = SQLITE_NOMEM;
16903
    }else{
16904
      shellExec(db, pRc, z);
16905
    }
16906
    sqlite3_free(z);
16907
  }
16908
}
16909
 
16910
/*
16911
** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
16912
** Otherwise, an attempt is made to allocate, zero and return a pointer
16913
** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
16914
** to SQLITE_NOMEM and NULL returned.
16915
*/
16916
static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
16917
  void *pRet = 0;
16918
  if( *pRc==SQLITE_OK ){
16919
    pRet = sqlite3_malloc64(nByte);
16920
    if( pRet==0 ){
16921
      *pRc = SQLITE_NOMEM;
16922
    }else{
16923
      memset(pRet, 0, nByte);
16924
    }
16925
  }
16926
  return pRet;
16927
}
16928
 
16929
/*
16930
** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
16931
** Otherwise, zFmt is treated as a printf() style string. The result of
16932
** formatting it along with any trailing arguments is written into a
16933
** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
16934
** It is the responsibility of the caller to eventually free this buffer
16935
** using a call to sqlite3_free().
16936
**
16937
** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
16938
** pointer returned.
16939
*/
16940
static char *shellMPrintf(int *pRc, const char *zFmt, ...){
16941
  char *z = 0;
16942
  if( *pRc==SQLITE_OK ){
16943
    va_list ap;
16944
    va_start(ap, zFmt);
16945
    z = sqlite3_vmprintf(zFmt, ap);
16946
    va_end(ap);
16947
    if( z==0 ){
16948
      *pRc = SQLITE_NOMEM;
16949
    }
16950
  }
16951
  return z;
16952
}
16953
 
16954
/*
16955
** When running the ".recover" command, each output table, and the special
16956
** orphaned row table if it is required, is represented by an instance
16957
** of the following struct.
16958
*/
16959
typedef struct RecoverTable RecoverTable;
16960
struct RecoverTable {
16961
  char *zQuoted;                  /* Quoted version of table name */
16962
  int nCol;                       /* Number of columns in table */
16963
  char **azlCol;                  /* Array of column lists */
16964
  int iPk;                        /* Index of IPK column */
16965
};
16966
 
16967
/*
16968
** Free a RecoverTable object allocated by recoverFindTable() or
16969
** recoverOrphanTable().
16970
*/
16971
static void recoverFreeTable(RecoverTable *pTab){
16972
  if( pTab ){
16973
    sqlite3_free(pTab->zQuoted);
16974
    if( pTab->azlCol ){
16975
      int i;
16976
      for(i=0; i<=pTab->nCol; i++){
16977
        sqlite3_free(pTab->azlCol[i]);
16978
      }
16979
      sqlite3_free(pTab->azlCol);
16980
    }
16981
    sqlite3_free(pTab);
16982
  }
16983
}
16984
 
16985
/*
16986
** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
16987
** Otherwise, it allocates and returns a RecoverTable object based on the
16988
** final four arguments passed to this function. It is the responsibility
16989
** of the caller to eventually free the returned object using
16990
** recoverFreeTable().
16991
*/
16992
static RecoverTable *recoverNewTable(
16993
  int *pRc,                       /* IN/OUT: Error code */
16994
  const char *zName,              /* Name of table */
16995
  const char *zSql,               /* CREATE TABLE statement */
16996
  int bIntkey,
16997
  int nCol
16998
){
16999
  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
17000
  int rc = *pRc;
17001
  RecoverTable *pTab = 0;
17002
 
17003
  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
17004
  if( rc==SQLITE_OK ){
17005
    int nSqlCol = 0;
17006
    int bSqlIntkey = 0;
17007
    sqlite3_stmt *pStmt = 0;
17008
 
17009
    rc = sqlite3_open("", &dbtmp);
17010
    if( rc==SQLITE_OK ){
17011
      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
17012
                              shellIdQuote, 0, 0);
17013
    }
17014
    if( rc==SQLITE_OK ){
17015
      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
17016
    }
17017
    if( rc==SQLITE_OK ){
17018
      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
17019
      if( rc==SQLITE_ERROR ){
17020
        rc = SQLITE_OK;
17021
        goto finished;
17022
      }
17023
    }
17024
    shellPreparePrintf(dbtmp, &rc, &pStmt,
17025
        "SELECT count(*) FROM pragma_table_info(%Q)", zName
17026
    );
17027
    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17028
      nSqlCol = sqlite3_column_int(pStmt, 0);
17029
    }
17030
    shellFinalize(&rc, pStmt);
17031
 
17032
    if( rc!=SQLITE_OK || nSqlCol
17033
      goto finished;
17034
    }
17035
 
17036
    shellPreparePrintf(dbtmp, &rc, &pStmt,
17037
      "SELECT ("
17038
      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
17039
      ") FROM sqlite_schema WHERE name = %Q", zName
17040
    );
17041
    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17042
      bSqlIntkey = sqlite3_column_int(pStmt, 0);
17043
    }
17044
    shellFinalize(&rc, pStmt);
17045
 
17046
    if( bIntkey==bSqlIntkey ){
17047
      int i;
17048
      const char *zPk = "_rowid_";
17049
      sqlite3_stmt *pPkFinder = 0;
17050
 
17051
      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
17052
      ** set zPk to the name of the PK column, and pTab->iPk to the index
17053
      ** of the column, where columns are 0-numbered from left to right.
17054
      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
17055
      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
17056
      pTab->iPk = -2;
17057
      if( bIntkey ){
17058
        shellPreparePrintf(dbtmp, &rc, &pPkFinder,
17059
          "SELECT cid, name FROM pragma_table_info(%Q) "
17060
          "  WHERE pk=1 AND type='integer' COLLATE nocase"
17061
          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
17062
          , zName, zName
17063
        );
17064
        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
17065
          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
17066
          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
17067
        }
17068
      }
17069
 
17070
      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
17071
      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
17072
      pTab->nCol = nSqlCol;
17073
 
17074
      if( bIntkey ){
17075
        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
17076
      }else{
17077
        pTab->azlCol[0] = shellMPrintf(&rc, "");
17078
      }
17079
      i = 1;
17080
      shellPreparePrintf(dbtmp, &rc, &pStmt,
17081
          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
17082
          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
17083
          "FROM pragma_table_info(%Q)",
17084
          bIntkey ? ", " : "", pTab->iPk,
17085
          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
17086
          zName
17087
      );
17088
      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17089
        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
17090
        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
17091
        i++;
17092
      }
17093
      shellFinalize(&rc, pStmt);
17094
 
17095
      shellFinalize(&rc, pPkFinder);
17096
    }
17097
  }
17098
 
17099
 finished:
17100
  sqlite3_close(dbtmp);
17101
  *pRc = rc;
17102
  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
17103
    recoverFreeTable(pTab);
17104
    pTab = 0;
17105
  }
17106
  return pTab;
17107
}
17108
 
17109
/*
17110
** This function is called to search the schema recovered from the
17111
** sqlite_schema table of the (possibly) corrupt database as part
17112
** of a ".recover" command. Specifically, for a table with root page
17113
** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
17114
** table must be a WITHOUT ROWID table, or if non-zero, not one of
17115
** those.
17116
**
17117
** If a table is found, a (RecoverTable*) object is returned. Or, if
17118
** no such table is found, but bIntkey is false and iRoot is the
17119
** root page of an index in the recovered schema, then (*pbNoop) is
17120
** set to true and NULL returned. Or, if there is no such table or
17121
** index, NULL is returned and (*pbNoop) set to 0, indicating that
17122
** the caller should write data to the orphans table.
17123
*/
17124
static RecoverTable *recoverFindTable(
17125
  ShellState *pState,             /* Shell state object */
17126
  int *pRc,                       /* IN/OUT: Error code */
17127
  int iRoot,                      /* Root page of table */
17128
  int bIntkey,                    /* True for an intkey table */
17129
  int nCol,                       /* Number of columns in table */
17130
  int *pbNoop                     /* OUT: True if iRoot is root of index */
17131
){
17132
  sqlite3_stmt *pStmt = 0;
17133
  RecoverTable *pRet = 0;
17134
  int bNoop = 0;
17135
  const char *zSql = 0;
17136
  const char *zName = 0;
17137
 
17138
  /* Search the recovered schema for an object with root page iRoot. */
17139
  shellPreparePrintf(pState->db, pRc, &pStmt,
17140
      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
17141
  );
17142
  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17143
    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
17144
    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
17145
      bNoop = 1;
17146
      break;
17147
    }
17148
    if( sqlite3_stricmp(zType, "table")==0 ){
17149
      zName = (const char*)sqlite3_column_text(pStmt, 1);
17150
      zSql = (const char*)sqlite3_column_text(pStmt, 2);
17151
      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
17152
      break;
17153
    }
17154
  }
17155
 
17156
  shellFinalize(pRc, pStmt);
17157
  *pbNoop = bNoop;
17158
  return pRet;
17159
}
17160
 
17161
/*
17162
** Return a RecoverTable object representing the orphans table.
17163
*/
17164
static RecoverTable *recoverOrphanTable(
17165
  ShellState *pState,             /* Shell state object */
17166
  int *pRc,                       /* IN/OUT: Error code */
17167
  const char *zLostAndFound,      /* Base name for orphans table */
17168
  int nCol                        /* Number of user data columns */
17169
){
17170
  RecoverTable *pTab = 0;
17171
  if( nCol>=0 && *pRc==SQLITE_OK ){
17172
    int i;
17173
 
17174
    /* This block determines the name of the orphan table. The prefered
17175
    ** name is zLostAndFound. But if that clashes with another name
17176
    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
17177
    ** and so on until a non-clashing name is found.  */
17178
    int iTab = 0;
17179
    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
17180
    sqlite3_stmt *pTest = 0;
17181
    shellPrepare(pState->db, pRc,
17182
        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
17183
    );
17184
    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17185
    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
17186
      shellReset(pRc, pTest);
17187
      sqlite3_free(zTab);
17188
      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
17189
      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17190
    }
17191
    shellFinalize(pRc, pTest);
17192
 
17193
    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
17194
    if( pTab ){
17195
      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
17196
      pTab->nCol = nCol;
17197
      pTab->iPk = -2;
17198
      if( nCol>0 ){
17199
        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
17200
        if( pTab->azlCol ){
17201
          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
17202
          for(i=nCol-1; i>=0; i--){
17203
            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
17204
          }
17205
        }
17206
      }
17207
 
17208
      if( *pRc!=SQLITE_OK ){
17209
        recoverFreeTable(pTab);
17210
        pTab = 0;
17211
      }else{
17212
        raw_printf(pState->out,
17213
            "CREATE TABLE %s(rootpgno INTEGER, "
17214
            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
17215
        );
17216
        for(i=0; i
17217
          raw_printf(pState->out, ", c%d", i);
17218
        }
17219
        raw_printf(pState->out, ");\n");
17220
      }
17221
    }
17222
    sqlite3_free(zTab);
17223
  }
17224
  return pTab;
17225
}
17226
 
17227
/*
17228
** This function is called to recover data from the database. A script
17229
** to construct a new database containing all recovered data is output
17230
** on stream pState->out.
17231
*/
17232
static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
17233
  int rc = SQLITE_OK;
17234
  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
17235
  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
17236
  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
17237
  const char *zRecoveryDb = "";   /* Name of "recovery" database */
17238
  const char *zLostAndFound = "lost_and_found";
17239
  int i;
17240
  int nOrphan = -1;
17241
  RecoverTable *pOrphan = 0;
17242
 
17243
  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
17244
  int bRowids = 1;                /* 0 if --no-rowids */
17245
  for(i=1; i
17246
    char *z = azArg[i];
17247
    int n;
17248
    if( z[0]=='-' && z[1]=='-' ) z++;
17249
    n = strlen30(z);
17250
    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
17251
      bFreelist = 0;
17252
    }else
17253
    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
17254
      i++;
17255
      zRecoveryDb = azArg[i];
17256
    }else
17257
    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
17258
      i++;
17259
      zLostAndFound = azArg[i];
17260
    }else
17261
    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
17262
      bRowids = 0;
17263
    }
17264
    else{
17265
      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
17266
      showHelp(pState->out, azArg[0]);
17267
      return 1;
17268
    }
17269
  }
17270
 
17271
  shellExecPrintf(pState->db, &rc,
17272
    /* Attach an in-memory database named 'recovery'. Create an indexed
17273
    ** cache of the sqlite_dbptr virtual table. */
17274
    "PRAGMA writable_schema = on;"
17275
    "ATTACH %Q AS recovery;"
17276
    "DROP TABLE IF EXISTS recovery.dbptr;"
17277
    "DROP TABLE IF EXISTS recovery.freelist;"
17278
    "DROP TABLE IF EXISTS recovery.map;"
17279
    "DROP TABLE IF EXISTS recovery.schema;"
17280
    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
17281
  );
17282
 
17283
  if( bFreelist ){
17284
    shellExec(pState->db, &rc,
17285
      "WITH trunk(pgno) AS ("
17286
      "  SELECT shell_int32("
17287
      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
17288
      "      WHERE x>0"
17289
      "    UNION"
17290
      "  SELECT shell_int32("
17291
      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
17292
      "      FROM trunk WHERE x>0"
17293
      "),"
17294
      "freelist(data, n, freepgno) AS ("
17295
      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
17296
      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
17297
      "    UNION ALL"
17298
      "  SELECT data, n-1, shell_int32(data, 2+n) "
17299
      "      FROM freelist WHERE n>=0"
17300
      ")"
17301
      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
17302
    );
17303
  }
17304
 
17305
  /* If this is an auto-vacuum database, add all pointer-map pages to
17306
  ** the freelist table. Do this regardless of whether or not
17307
  ** --freelist-corrupt was specified.  */
17308
  shellExec(pState->db, &rc,
17309
    "WITH ptrmap(pgno) AS ("
17310
    "  SELECT 2 WHERE shell_int32("
17311
    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
17312
    "  )"
17313
    "    UNION ALL "
17314
    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
17315
    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
17316
    ")"
17317
    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
17318
  );
17319
 
17320
  shellExec(pState->db, &rc,
17321
    "CREATE TABLE recovery.dbptr("
17322
    "      pgno, child, PRIMARY KEY(child, pgno)"
17323
    ") WITHOUT ROWID;"
17324
    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
17325
    "    SELECT * FROM sqlite_dbptr"
17326
    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
17327
 
17328
    /* Delete any pointer to page 1. This ensures that page 1 is considered
17329
    ** a root page, regardless of how corrupt the db is. */
17330
    "DELETE FROM recovery.dbptr WHERE child = 1;"
17331
 
17332
    /* Delete all pointers to any pages that have more than one pointer
17333
    ** to them. Such pages will be treated as root pages when recovering
17334
    ** data.  */
17335
    "DELETE FROM recovery.dbptr WHERE child IN ("
17336
    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
17337
    ");"
17338
 
17339
    /* Create the "map" table that will (eventually) contain instructions
17340
    ** for dealing with each page in the db that contains one or more
17341
    ** records. */
17342
    "CREATE TABLE recovery.map("
17343
      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
17344
    ");"
17345
 
17346
    /* Populate table [map]. If there are circular loops of pages in the
17347
    ** database, the following adds all pages in such a loop to the map
17348
    ** as individual root pages. This could be handled better.  */
17349
    "WITH pages(i, maxlen) AS ("
17350
    "  SELECT page_count, ("
17351
    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
17352
    "  ) FROM pragma_page_count WHERE page_count>0"
17353
    "    UNION ALL"
17354
    "  SELECT i-1, ("
17355
    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
17356
    "  ) FROM pages WHERE i>=2"
17357
    ")"
17358
    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
17359
    "  SELECT i, maxlen, NULL, ("
17360
    "    WITH p(orig, pgno, parent) AS ("
17361
    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
17362
    "        UNION "
17363
    "      SELECT i, p.parent, "
17364
    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
17365
    "    )"
17366
    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
17367
    ") "
17368
    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
17369
    "UPDATE recovery.map AS o SET intkey = ("
17370
    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
17371
    ");"
17372
 
17373
    /* Extract data from page 1 and any linked pages into table
17374
    ** recovery.schema. With the same schema as an sqlite_schema table.  */
17375
    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
17376
    "INSERT INTO recovery.schema SELECT "
17377
    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
17378
    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
17379
    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
17380
    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
17381
    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
17382
    "FROM sqlite_dbdata WHERE pgno IN ("
17383
    "  SELECT pgno FROM recovery.map WHERE root=1"
17384
    ")"
17385
    "GROUP BY pgno, cell;"
17386
    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
17387
  );
17388
 
17389
  /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
17390
  ** CREATE TABLE statements that extracted from the existing schema.  */
17391
  if( rc==SQLITE_OK ){
17392
    sqlite3_stmt *pStmt = 0;
17393
    /* ".recover" might output content in an order which causes immediate
17394
    ** foreign key constraints to be violated. So disable foreign-key
17395
    ** constraint enforcement to prevent problems when running the output
17396
    ** script. */
17397
    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
17398
    raw_printf(pState->out, "BEGIN;\n");
17399
    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
17400
    shellPrepare(pState->db, &rc,
17401
        "SELECT sql FROM recovery.schema "
17402
        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
17403
    );
17404
    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17405
      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
17406
      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
17407
          &zCreateTable[12]
17408
      );
17409
    }
17410
    shellFinalize(&rc, pStmt);
17411
  }
17412
 
17413
  /* Figure out if an orphan table will be required. And if so, how many
17414
  ** user columns it should contain */
17415
  shellPrepare(pState->db, &rc,
17416
      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
17417
      , &pLoop
17418
  );
17419
  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
17420
    nOrphan = sqlite3_column_int(pLoop, 0);
17421
  }
17422
  shellFinalize(&rc, pLoop);
17423
  pLoop = 0;
17424
 
17425
  shellPrepare(pState->db, &rc,
17426
      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
17427
  );
17428
 
17429
  shellPrepare(pState->db, &rc,
17430
      "SELECT max(field), group_concat(shell_escape_crnl(quote"
17431
      "(case when (? AND field<0) then NULL else value end)"
17432
      "), ', ')"
17433
      ", min(field) "
17434
      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
17435
      "GROUP BY cell", &pCells
17436
  );
17437
 
17438
  /* Loop through each root page. */
17439
  shellPrepare(pState->db, &rc,
17440
      "SELECT root, intkey, max(maxlen) FROM recovery.map"
17441
      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
17442
      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
17443
      ")", &pLoop
17444
  );
17445
  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
17446
    int iRoot = sqlite3_column_int(pLoop, 0);
17447
    int bIntkey = sqlite3_column_int(pLoop, 1);
17448
    int nCol = sqlite3_column_int(pLoop, 2);
17449
    int bNoop = 0;
17450
    RecoverTable *pTab;
17451
 
17452
    assert( bIntkey==0 || bIntkey==1 );
17453
    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
17454
    if( bNoop || rc ) continue;
17455
    if( pTab==0 ){
17456
      if( pOrphan==0 ){
17457
        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
17458
      }
17459
      pTab = pOrphan;
17460
      if( pTab==0 ) break;
17461
    }
17462
 
17463
    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
17464
      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
17465
    }
17466
    sqlite3_bind_int(pPages, 1, iRoot);
17467
    if( bRowids==0 && pTab->iPk<0 ){
17468
      sqlite3_bind_int(pCells, 1, 1);
17469
    }else{
17470
      sqlite3_bind_int(pCells, 1, 0);
17471
    }
17472
    sqlite3_bind_int(pCells, 3, pTab->iPk);
17473
 
17474
    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
17475
      int iPgno = sqlite3_column_int(pPages, 0);
17476
      sqlite3_bind_int(pCells, 2, iPgno);
17477
      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
17478
        int nField = sqlite3_column_int(pCells, 0);
17479
        int iMin = sqlite3_column_int(pCells, 2);
17480
        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
17481
 
17482
        RecoverTable *pTab2 = pTab;
17483
        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
17484
          if( pOrphan==0 ){
17485
            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
17486
          }
17487
          pTab2 = pOrphan;
17488
          if( pTab2==0 ) break;
17489
        }
17490
 
17491
        nField = nField+1;
17492
        if( pTab2==pOrphan ){
17493
          raw_printf(pState->out,
17494
              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
17495
              pTab2->zQuoted, iRoot, iPgno, nField,
17496
              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
17497
          );
17498
        }else{
17499
          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
17500
              pTab2->zQuoted, pTab2->azlCol[nField], zVal
17501
          );
17502
        }
17503
      }
17504
      shellReset(&rc, pCells);
17505
    }
17506
    shellReset(&rc, pPages);
17507
    if( pTab!=pOrphan ) recoverFreeTable(pTab);
17508
  }
17509
  shellFinalize(&rc, pLoop);
17510
  shellFinalize(&rc, pPages);
17511
  shellFinalize(&rc, pCells);
17512
  recoverFreeTable(pOrphan);
17513
 
17514
  /* The rest of the schema */
17515
  if( rc==SQLITE_OK ){
17516
    sqlite3_stmt *pStmt = 0;
17517
    shellPrepare(pState->db, &rc,
17518
        "SELECT sql, name FROM recovery.schema "
17519
        "WHERE sql NOT LIKE 'create table%'", &pStmt
17520
    );
17521
    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17522
      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
17523
      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
17524
        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
17525
        char *zPrint = shellMPrintf(&rc,
17526
          "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
17527
          zName, zName, zSql
17528
        );
17529
        raw_printf(pState->out, "%s;\n", zPrint);
17530
        sqlite3_free(zPrint);
17531
      }else{
17532
        raw_printf(pState->out, "%s;\n", zSql);
17533
      }
17534
    }
17535
    shellFinalize(&rc, pStmt);
17536
  }
17537
 
17538
  if( rc==SQLITE_OK ){
17539
    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
17540
    raw_printf(pState->out, "COMMIT;\n");
17541
  }
17542
  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
17543
  return rc;
17544
}
17545
#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
17546
 
17547
 
17548
/*
17549
** If an input line begins with "." then invoke this routine to
17550
** process that line.
17551
**
17552
** Return 1 on error, 2 to exit, and 0 otherwise.
17553
*/
17554
static int do_meta_command(char *zLine, ShellState *p){
17555
  int h = 1;
17556
  int nArg = 0;
17557
  int n, c;
17558
  int rc = 0;
17559
  char *azArg[52];
17560
 
17561
#ifndef SQLITE_OMIT_VIRTUALTABLE
17562
  if( p->expert.pExpert ){
17563
    expertFinish(p, 1, 0);
17564
  }
17565
#endif
17566
 
17567
  /* Parse the input line into tokens.
17568
  */
17569
  while( zLine[h] && nArg
17570
    while( IsSpace(zLine[h]) ){ h++; }
17571
    if( zLine[h]==0 ) break;
17572
    if( zLine[h]=='\'' || zLine[h]=='"' ){
17573
      int delim = zLine[h++];
17574
      azArg[nArg++] = &zLine[h];
17575
      while( zLine[h] && zLine[h]!=delim ){
17576
        if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
17577
        h++;
17578
      }
17579
      if( zLine[h]==delim ){
17580
        zLine[h++] = 0;
17581
      }
17582
      if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
17583
    }else{
17584
      azArg[nArg++] = &zLine[h];
17585
      while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
17586
      if( zLine[h] ) zLine[h++] = 0;
17587
      resolve_backslashes(azArg[nArg-1]);
17588
    }
17589
  }
17590
  azArg[nArg] = 0;
17591
 
17592
  /* Process the input line.
17593
  */
17594
  if( nArg==0 ) return 0; /* no tokens, no error */
17595
  n = strlen30(azArg[0]);
17596
  c = azArg[0][0];
17597
  clearTempFile(p);
17598
 
17599
#ifndef SQLITE_OMIT_AUTHORIZATION
17600
  if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
17601
    if( nArg!=2 ){
17602
      raw_printf(stderr, "Usage: .auth ON|OFF\n");
17603
      rc = 1;
17604
      goto meta_command_exit;
17605
    }
17606
    open_db(p, 0);
17607
    if( booleanValue(azArg[1]) ){
17608
      sqlite3_set_authorizer(p->db, shellAuth, p);
17609
    }else{
17610
      sqlite3_set_authorizer(p->db, 0, 0);
17611
    }
17612
  }else
17613
#endif
17614
 
17615
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
17616
  if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
17617
    open_db(p, 0);
17618
    rc = arDotCommand(p, 0, azArg, nArg);
17619
  }else
17620
#endif
17621
 
17622
  if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
17623
   || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
17624
  ){
17625
    const char *zDestFile = 0;
17626
    const char *zDb = 0;
17627
    sqlite3 *pDest;
17628
    sqlite3_backup *pBackup;
17629
    int j;
17630
    int bAsync = 0;
17631
    const char *zVfs = 0;
17632
    for(j=1; j
17633
      const char *z = azArg[j];
17634
      if( z[0]=='-' ){
17635
        if( z[1]=='-' ) z++;
17636
        if( strcmp(z, "-append")==0 ){
17637
          zVfs = "apndvfs";
17638
        }else
17639
        if( strcmp(z, "-async")==0 ){
17640
          bAsync = 1;
17641
        }else
17642
        {
17643
          utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
17644
          return 1;
17645
        }
17646
      }else if( zDestFile==0 ){
17647
        zDestFile = azArg[j];
17648
      }else if( zDb==0 ){
17649
        zDb = zDestFile;
17650
        zDestFile = azArg[j];
17651
      }else{
17652
        raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
17653
        return 1;
17654
      }
17655
    }
17656
    if( zDestFile==0 ){
17657
      raw_printf(stderr, "missing FILENAME argument on .backup\n");
17658
      return 1;
17659
    }
17660
    if( zDb==0 ) zDb = "main";
17661
    rc = sqlite3_open_v2(zDestFile, &pDest,
17662
                  SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
17663
    if( rc!=SQLITE_OK ){
17664
      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
17665
      close_db(pDest);
17666
      return 1;
17667
    }
17668
    if( bAsync ){
17669
      sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
17670
                   0, 0, 0);
17671
    }
17672
    open_db(p, 0);
17673
    pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
17674
    if( pBackup==0 ){
17675
      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
17676
      close_db(pDest);
17677
      return 1;
17678
    }
17679
    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
17680
    sqlite3_backup_finish(pBackup);
17681
    if( rc==SQLITE_DONE ){
17682
      rc = 0;
17683
    }else{
17684
      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
17685
      rc = 1;
17686
    }
17687
    close_db(pDest);
17688
  }else
17689
 
17690
  if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
17691
    if( nArg==2 ){
17692
      bail_on_error = booleanValue(azArg[1]);
17693
    }else{
17694
      raw_printf(stderr, "Usage: .bail on|off\n");
17695
      rc = 1;
17696
    }
17697
  }else
17698
 
17699
  if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
17700
    if( nArg==2 ){
17701
      if( booleanValue(azArg[1]) ){
17702
        setBinaryMode(p->out, 1);
17703
      }else{
17704
        setTextMode(p->out, 1);
17705
      }
17706
    }else{
17707
      raw_printf(stderr, "Usage: .binary on|off\n");
17708
      rc = 1;
17709
    }
17710
  }else
17711
 
17712
  if( c=='c' && strcmp(azArg[0],"cd")==0 ){
17713
    if( nArg==2 ){
17714
#if defined(_WIN32) || defined(WIN32)
17715
      wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
17716
      rc = !SetCurrentDirectoryW(z);
17717
      sqlite3_free(z);
17718
#else
17719
      rc = chdir(azArg[1]);
17720
#endif
17721
      if( rc ){
17722
        utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
17723
        rc = 1;
17724
      }
17725
    }else{
17726
      raw_printf(stderr, "Usage: .cd DIRECTORY\n");
17727
      rc = 1;
17728
    }
17729
  }else
17730
 
17731
  /* The undocumented ".breakpoint" command causes a call to the no-op
17732
  ** routine named test_breakpoint().
17733
  */
17734
  if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
17735
    test_breakpoint();
17736
  }else
17737
 
17738
  if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
17739
    if( nArg==2 ){
17740
      setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
17741
    }else{
17742
      raw_printf(stderr, "Usage: .changes on|off\n");
17743
      rc = 1;
17744
    }
17745
  }else
17746
 
17747
  /* Cancel output redirection, if it is currently set (by .testcase)
17748
  ** Then read the content of the testcase-out.txt file and compare against
17749
  ** azArg[1].  If there are differences, report an error and exit.
17750
  */
17751
  if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
17752
    char *zRes = 0;
17753
    output_reset(p);
17754
    if( nArg!=2 ){
17755
      raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
17756
      rc = 2;
17757
    }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
17758
      raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
17759
      rc = 2;
17760
    }else if( testcase_glob(azArg[1],zRes)==0 ){
17761
      utf8_printf(stderr,
17762
                 "testcase-%s FAILED\n Expected: [%s]\n      Got: [%s]\n",
17763
                 p->zTestcase, azArg[1], zRes);
17764
      rc = 1;
17765
    }else{
17766
      utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
17767
      p->nCheck++;
17768
    }
17769
    sqlite3_free(zRes);
17770
  }else
17771
 
17772
  if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
17773
    if( nArg==2 ){
17774
      tryToClone(p, azArg[1]);
17775
    }else{
17776
      raw_printf(stderr, "Usage: .clone FILENAME\n");
17777
      rc = 1;
17778
    }
17779
  }else
17780
 
17781
  if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
17782
    char **azName = 0;
17783
    int nName = 0;
17784
    sqlite3_stmt *pStmt;
17785
    int i;
17786
    open_db(p, 0);
17787
    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
17788
    if( rc ){
17789
      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17790
      rc = 1;
17791
    }else{
17792
      while( sqlite3_step(pStmt)==SQLITE_ROW ){
17793
        const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
17794
        const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
17795
        azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
17796
        if( azName==0 ){ shell_out_of_memory();  /* Does not return */ }
17797
        azName[nName*2] = strdup(zSchema);
17798
        azName[nName*2+1] = strdup(zFile);
17799
        nName++;
17800
      }
17801
    }
17802
    sqlite3_finalize(pStmt);
17803
    for(i=0; i
17804
      int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
17805
      int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
17806
      const char *z = azName[i*2+1];
17807
      utf8_printf(p->out, "%s: %s %s%s\n",
17808
         azName[i*2],
17809
         z && z[0] ? z : "\"\"",
17810
         bRdonly ? "r/o" : "r/w",
17811
         eTxn==SQLITE_TXN_NONE ? "" :
17812
            eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
17813
      free(azName[i*2]);
17814
      free(azName[i*2+1]);
17815
    }
17816
    sqlite3_free(azName);
17817
  }else
17818
 
17819
  if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
17820
    static const struct DbConfigChoices {
17821
      const char *zName;
17822
      int op;
17823
    } aDbConfig[] = {
17824
        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
17825
        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
17826
        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
17827
        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
17828
        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
17829
        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
17830
        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
17831
        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
17832
        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
17833
        { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    },
17834
        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
17835
        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
17836
        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
17837
        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
17838
        { "trusted_schema",     SQLITE_DBCONFIG_TRUSTED_SCHEMA        },
17839
        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
17840
    };
17841
    int ii, v;
17842
    open_db(p, 0);
17843
    for(ii=0; ii
17844
      if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
17845
      if( nArg>=3 ){
17846
        sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
17847
      }
17848
      sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
17849
      utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
17850
      if( nArg>1 ) break;
17851
    }
17852
    if( nArg>1 && ii==ArraySize(aDbConfig) ){
17853
      utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
17854
      utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
17855
    }
17856
  }else
17857
 
17858
  if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
17859
    rc = shell_dbinfo_command(p, nArg, azArg);
17860
  }else
17861
 
17862
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
17863
  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
17864
    open_db(p, 0);
17865
    rc = recoverDatabaseCmd(p, nArg, azArg);
17866
  }else
17867
#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
17868
 
17869
  if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
17870
    char *zLike = 0;
17871
    char *zSql;
17872
    int i;
17873
    int savedShowHeader = p->showHeader;
17874
    int savedShellFlags = p->shellFlgs;
17875
    ShellClearFlag(p,
17876
       SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
17877
       |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
17878
    for(i=1; i
17879
      if( azArg[i][0]=='-' ){
17880
        const char *z = azArg[i]+1;
17881
        if( z[0]=='-' ) z++;
17882
        if( strcmp(z,"preserve-rowids")==0 ){
17883
#ifdef SQLITE_OMIT_VIRTUALTABLE
17884
          raw_printf(stderr, "The --preserve-rowids option is not compatible"
17885
                             " with SQLITE_OMIT_VIRTUALTABLE\n");
17886
          rc = 1;
17887
          sqlite3_free(zLike);
17888
          goto meta_command_exit;
17889
#else
17890
          ShellSetFlag(p, SHFLG_PreserveRowid);
17891
#endif
17892
        }else
17893
        if( strcmp(z,"newlines")==0 ){
17894
          ShellSetFlag(p, SHFLG_Newlines);
17895
        }else
17896
        if( strcmp(z,"data-only")==0 ){
17897
          ShellSetFlag(p, SHFLG_DumpDataOnly);
17898
        }else
17899
        if( strcmp(z,"nosys")==0 ){
17900
          ShellSetFlag(p, SHFLG_DumpNoSys);
17901
        }else
17902
        {
17903
          raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
17904
          rc = 1;
17905
          sqlite3_free(zLike);
17906
          goto meta_command_exit;
17907
        }
17908
      }else if( zLike ){
17909
        zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
17910
                zLike, azArg[i]);
17911
      }else{
17912
        zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
17913
      }
17914
    }
17915
 
17916
    open_db(p, 0);
17917
 
17918
    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17919
      /* When playing back a "dump", the content might appear in an order
17920
      ** which causes immediate foreign key constraints to be violated.
17921
      ** So disable foreign-key constraint enforcement to prevent problems. */
17922
      raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
17923
      raw_printf(p->out, "BEGIN TRANSACTION;\n");
17924
    }
17925
    p->writableSchema = 0;
17926
    p->showHeader = 0;
17927
    /* Set writable_schema=ON since doing so forces SQLite to initialize
17928
    ** as much of the schema as it can even if the sqlite_schema table is
17929
    ** corrupt. */
17930
    sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
17931
    p->nErr = 0;
17932
    if( zLike==0 ) zLike = sqlite3_mprintf("true");
17933
    zSql = sqlite3_mprintf(
17934
      "SELECT name, type, sql FROM sqlite_schema "
17935
      "WHERE (%s) AND type=='table'"
17936
      "  AND sql NOT NULL"
17937
      " ORDER BY tbl_name='sqlite_sequence', rowid",
17938
      zLike
17939
    );
17940
    run_schema_dump_query(p,zSql);
17941
    sqlite3_free(zSql);
17942
    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17943
      zSql = sqlite3_mprintf(
17944
        "SELECT sql FROM sqlite_schema "
17945
        "WHERE (%s) AND sql NOT NULL"
17946
        "  AND type IN ('index','trigger','view')",
17947
        zLike
17948
      );
17949
      run_table_dump_query(p, zSql);
17950
      sqlite3_free(zSql);
17951
    }
17952
    sqlite3_free(zLike);
17953
    if( p->writableSchema ){
17954
      raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
17955
      p->writableSchema = 0;
17956
    }
17957
    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
17958
    sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
17959
    if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17960
      raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
17961
    }
17962
    p->showHeader = savedShowHeader;
17963
    p->shellFlgs = savedShellFlags;
17964
  }else
17965
 
17966
  if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
17967
    if( nArg==2 ){
17968
      setOrClearFlag(p, SHFLG_Echo, azArg[1]);
17969
    }else{
17970
      raw_printf(stderr, "Usage: .echo on|off\n");
17971
      rc = 1;
17972
    }
17973
  }else
17974
 
17975
  if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
17976
    if( nArg==2 ){
17977
      p->autoEQPtest = 0;
17978
      if( p->autoEQPtrace ){
17979
        if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
17980
        p->autoEQPtrace = 0;
17981
      }
17982
      if( strcmp(azArg[1],"full")==0 ){
17983
        p->autoEQP = AUTOEQP_full;
17984
      }else if( strcmp(azArg[1],"trigger")==0 ){
17985
        p->autoEQP = AUTOEQP_trigger;
17986
#ifdef SQLITE_DEBUG
17987
      }else if( strcmp(azArg[1],"test")==0 ){
17988
        p->autoEQP = AUTOEQP_on;
17989
        p->autoEQPtest = 1;
17990
      }else if( strcmp(azArg[1],"trace")==0 ){
17991
        p->autoEQP = AUTOEQP_full;
17992
        p->autoEQPtrace = 1;
17993
        open_db(p, 0);
17994
        sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
17995
        sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
17996
#endif
17997
      }else{
17998
        p->autoEQP = (u8)booleanValue(azArg[1]);
17999
      }
18000
    }else{
18001
      raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
18002
      rc = 1;
18003
    }
18004
  }else
18005
 
18006
  if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
18007
    if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
18008
    rc = 2;
18009
  }else
18010
 
18011
  /* The ".explain" command is automatic now.  It is largely pointless.  It
18012
  ** retained purely for backwards compatibility */
18013
  if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
18014
    int val = 1;
18015
    if( nArg>=2 ){
18016
      if( strcmp(azArg[1],"auto")==0 ){
18017
        val = 99;
18018
      }else{
18019
        val =  booleanValue(azArg[1]);
18020
      }
18021
    }
18022
    if( val==1 && p->mode!=MODE_Explain ){
18023
      p->normalMode = p->mode;
18024
      p->mode = MODE_Explain;
18025
      p->autoExplain = 0;
18026
    }else if( val==0 ){
18027
      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
18028
      p->autoExplain = 0;
18029
    }else if( val==99 ){
18030
      if( p->mode==MODE_Explain ) p->mode = p->normalMode;
18031
      p->autoExplain = 1;
18032
    }
18033
  }else
18034
 
18035
#ifndef SQLITE_OMIT_VIRTUALTABLE
18036
  if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
18037
    open_db(p, 0);
18038
    expertDotCommand(p, azArg, nArg);
18039
  }else
18040
#endif
18041
 
18042
  if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
18043
    static const struct {
18044
       const char *zCtrlName;   /* Name of a test-control option */
18045
       int ctrlCode;            /* Integer code for that option */
18046
       const char *zUsage;      /* Usage notes */
18047
    } aCtrl[] = {
18048
      { "chunk_size",     SQLITE_FCNTL_CHUNK_SIZE,      "SIZE"           },
18049
      { "data_version",   SQLITE_FCNTL_DATA_VERSION,    ""               },
18050
      { "has_moved",      SQLITE_FCNTL_HAS_MOVED,       ""               },
18051
      { "lock_timeout",   SQLITE_FCNTL_LOCK_TIMEOUT,    "MILLISEC"       },
18052
      { "persist_wal",    SQLITE_FCNTL_PERSIST_WAL,     "[BOOLEAN]"      },
18053
   /* { "pragma",         SQLITE_FCNTL_PRAGMA,          "NAME ARG"       },*/
18054
      { "psow",       SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]"      },
18055
      { "reserve_bytes",  SQLITE_FCNTL_RESERVE_BYTES,   "[N]"            },
18056
      { "size_limit",     SQLITE_FCNTL_SIZE_LIMIT,      "[LIMIT]"        },
18057
      { "tempfilename",   SQLITE_FCNTL_TEMPFILENAME,    ""               },
18058
   /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY,  "COUNT DELAY"    },*/
18059
    };
18060
    int filectrl = -1;
18061
    int iCtrl = -1;
18062
    sqlite3_int64 iRes = 0;  /* Integer result to display if rc2==1 */
18063
    int isOk = 0;            /* 0: usage  1: %lld  2: no-result */
18064
    int n2, i;
18065
    const char *zCmd = 0;
18066
    const char *zSchema = 0;
18067
 
18068
    open_db(p, 0);
18069
    zCmd = nArg>=2 ? azArg[1] : "help";
18070
 
18071
    if( zCmd[0]=='-'
18072
     && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
18073
     && nArg>=4
18074
    ){
18075
      zSchema = azArg[2];
18076
      for(i=3; i
18077
      nArg -= 2;
18078
      zCmd = azArg[1];
18079
    }
18080
 
18081
    /* The argument can optionally begin with "-" or "--" */
18082
    if( zCmd[0]=='-' && zCmd[1] ){
18083
      zCmd++;
18084
      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
18085
    }
18086
 
18087
    /* --help lists all file-controls */
18088
    if( strcmp(zCmd,"help")==0 ){
18089
      utf8_printf(p->out, "Available file-controls:\n");
18090
      for(i=0; i
18091
        utf8_printf(p->out, "  .filectrl %s %s\n",
18092
                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
18093
      }
18094
      rc = 1;
18095
      goto meta_command_exit;
18096
    }
18097
 
18098
    /* convert filectrl text option to value. allow any unique prefix
18099
    ** of the option name, or a numerical value. */
18100
    n2 = strlen30(zCmd);
18101
    for(i=0; i
18102
      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
18103
        if( filectrl<0 ){
18104
          filectrl = aCtrl[i].ctrlCode;
18105
          iCtrl = i;
18106
        }else{
18107
          utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
18108
                              "Use \".filectrl --help\" for help\n", zCmd);
18109
          rc = 1;
18110
          goto meta_command_exit;
18111
        }
18112
      }
18113
    }
18114
    if( filectrl<0 ){
18115
      utf8_printf(stderr,"Error: unknown file-control: %s\n"
18116
                         "Use \".filectrl --help\" for help\n", zCmd);
18117
    }else{
18118
      switch(filectrl){
18119
        case SQLITE_FCNTL_SIZE_LIMIT: {
18120
          if( nArg!=2 && nArg!=3 ) break;
18121
          iRes = nArg==3 ? integerValue(azArg[2]) : -1;
18122
          sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
18123
          isOk = 1;
18124
          break;
18125
        }
18126
        case SQLITE_FCNTL_LOCK_TIMEOUT:
18127
        case SQLITE_FCNTL_CHUNK_SIZE: {
18128
          int x;
18129
          if( nArg!=3 ) break;
18130
          x = (int)integerValue(azArg[2]);
18131
          sqlite3_file_control(p->db, zSchema, filectrl, &x);
18132
          isOk = 2;
18133
          break;
18134
        }
18135
        case SQLITE_FCNTL_PERSIST_WAL:
18136
        case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
18137
          int x;
18138
          if( nArg!=2 && nArg!=3 ) break;
18139
          x = nArg==3 ? booleanValue(azArg[2]) : -1;
18140
          sqlite3_file_control(p->db, zSchema, filectrl, &x);
18141
          iRes = x;
18142
          isOk = 1;
18143
          break;
18144
        }
18145
        case SQLITE_FCNTL_DATA_VERSION:
18146
        case SQLITE_FCNTL_HAS_MOVED: {
18147
          int x;
18148
          if( nArg!=2 ) break;
18149
          sqlite3_file_control(p->db, zSchema, filectrl, &x);
18150
          iRes = x;
18151
          isOk = 1;
18152
          break;
18153
        }
18154
        case SQLITE_FCNTL_TEMPFILENAME: {
18155
          char *z = 0;
18156
          if( nArg!=2 ) break;
18157
          sqlite3_file_control(p->db, zSchema, filectrl, &z);
18158
          if( z ){
18159
            utf8_printf(p->out, "%s\n", z);
18160
            sqlite3_free(z);
18161
          }
18162
          isOk = 2;
18163
          break;
18164
        }
18165
        case SQLITE_FCNTL_RESERVE_BYTES: {
18166
          int x;
18167
          if( nArg>=3 ){
18168
            x = atoi(azArg[2]);
18169
            sqlite3_file_control(p->db, zSchema, filectrl, &x);
18170
          }
18171
          x = -1;
18172
          sqlite3_file_control(p->db, zSchema, filectrl, &x);
18173
          utf8_printf(p->out,"%d\n", x);
18174
          isOk = 2;
18175
          break;
18176
        }
18177
      }
18178
    }
18179
    if( isOk==0 && iCtrl>=0 ){
18180
      utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
18181
      rc = 1;
18182
    }else if( isOk==1 ){
18183
      char zBuf[100];
18184
      sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
18185
      raw_printf(p->out, "%s\n", zBuf);
18186
    }
18187
  }else
18188
 
18189
  if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
18190
    ShellState data;
18191
    char *zErrMsg = 0;
18192
    int doStats = 0;
18193
    memcpy(&data, p, sizeof(data));
18194
    data.showHeader = 0;
18195
    data.cMode = data.mode = MODE_Semi;
18196
    if( nArg==2 && optionMatch(azArg[1], "indent") ){
18197
      data.cMode = data.mode = MODE_Pretty;
18198
      nArg = 1;
18199
    }
18200
    if( nArg!=1 ){
18201
      raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
18202
      rc = 1;
18203
      goto meta_command_exit;
18204
    }
18205
    open_db(p, 0);
18206
    rc = sqlite3_exec(p->db,
18207
       "SELECT sql FROM"
18208
       "  (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
18209
       "     FROM sqlite_schema UNION ALL"
18210
       "   SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
18211
       "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
18212
       "ORDER BY rowid",
18213
       callback, &data, &zErrMsg
18214
    );
18215
    if( rc==SQLITE_OK ){
18216
      sqlite3_stmt *pStmt;
18217
      rc = sqlite3_prepare_v2(p->db,
18218
               "SELECT rowid FROM sqlite_schema"
18219
               " WHERE name GLOB 'sqlite_stat[134]'",
18220
               -1, &pStmt, 0);
18221
      doStats = sqlite3_step(pStmt)==SQLITE_ROW;
18222
      sqlite3_finalize(pStmt);
18223
    }
18224
    if( doStats==0 ){
18225
      raw_printf(p->out, "/* No STAT tables available */\n");
18226
    }else{
18227
      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
18228
      sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
18229
                   callback, &data, &zErrMsg);
18230
      data.cMode = data.mode = MODE_Insert;
18231
      data.zDestTable = "sqlite_stat1";
18232
      shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
18233
      data.zDestTable = "sqlite_stat4";
18234
      shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
18235
      raw_printf(p->out, "ANALYZE sqlite_schema;\n");
18236
    }
18237
  }else
18238
 
18239
  if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
18240
    if( nArg==2 ){
18241
      p->showHeader = booleanValue(azArg[1]);
18242
      p->shellFlgs |= SHFLG_HeaderSet;
18243
    }else{
18244
      raw_printf(stderr, "Usage: .headers on|off\n");
18245
      rc = 1;
18246
    }
18247
  }else
18248
 
18249
  if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
18250
    if( nArg>=2 ){
18251
      n = showHelp(p->out, azArg[1]);
18252
      if( n==0 ){
18253
        utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
18254
      }
18255
    }else{
18256
      showHelp(p->out, 0);
18257
    }
18258
  }else
18259
 
18260
  if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
18261
    char *zTable = 0;           /* Insert data into this table */
18262
    char *zFile = 0;            /* Name of file to extra content from */
18263
    sqlite3_stmt *pStmt = NULL; /* A statement */
18264
    int nCol;                   /* Number of columns in the table */
18265
    int nByte;                  /* Number of bytes in an SQL string */
18266
    int i, j;                   /* Loop counters */
18267
    int needCommit;             /* True to COMMIT or ROLLBACK at end */
18268
    int nSep;                   /* Number of bytes in p->colSeparator[] */
18269
    char *zSql;                 /* An SQL statement */
18270
    ImportCtx sCtx;             /* Reader context */
18271
    char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
18272
    int eVerbose = 0;           /* Larger for more console output */
18273
    int nSkip = 0;              /* Initial lines to skip */
18274
    int useOutputMode = 1;      /* Use output mode to determine separators */
18275
 
18276
    memset(&sCtx, 0, sizeof(sCtx));
18277
    if( p->mode==MODE_Ascii ){
18278
      xRead = ascii_read_one_field;
18279
    }else{
18280
      xRead = csv_read_one_field;
18281
    }
18282
    for(i=1; i
18283
      char *z = azArg[i];
18284
      if( z[0]=='-' && z[1]=='-' ) z++;
18285
      if( z[0]!='-' ){
18286
        if( zFile==0 ){
18287
          zFile = z;
18288
        }else if( zTable==0 ){
18289
          zTable = z;
18290
        }else{
18291
          utf8_printf(p->out, "ERROR: extra argument: \"%s\".  Usage:\n", z);
18292
          showHelp(p->out, "import");
18293
          rc = 1;
18294
          goto meta_command_exit;
18295
        }
18296
      }else if( strcmp(z,"-v")==0 ){
18297
        eVerbose++;
18298
      }else if( strcmp(z,"-skip")==0 && i
18299
        nSkip = integerValue(azArg[++i]);
18300
      }else if( strcmp(z,"-ascii")==0 ){
18301
        sCtx.cColSep = SEP_Unit[0];
18302
        sCtx.cRowSep = SEP_Record[0];
18303
        xRead = ascii_read_one_field;
18304
        useOutputMode = 0;
18305
      }else if( strcmp(z,"-csv")==0 ){
18306
        sCtx.cColSep = ',';
18307
        sCtx.cRowSep = '\n';
18308
        xRead = csv_read_one_field;
18309
        useOutputMode = 0;
18310
      }else{
18311
        utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n", z);
18312
        showHelp(p->out, "import");
18313
        rc = 1;
18314
        goto meta_command_exit;
18315
      }
18316
    }
18317
    if( zTable==0 ){
18318
      utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
18319
                  zFile==0 ? "FILE" : "TABLE");
18320
      showHelp(p->out, "import");
18321
      rc = 1;
18322
      goto meta_command_exit;
18323
    }
18324
    seenInterrupt = 0;
18325
    open_db(p, 0);
18326
    if( useOutputMode ){
18327
      /* If neither the --csv or --ascii options are specified, then set
18328
      ** the column and row separator characters from the output mode. */
18329
      nSep = strlen30(p->colSeparator);
18330
      if( nSep==0 ){
18331
        raw_printf(stderr,
18332
                   "Error: non-null column separator required for import\n");
18333
        rc = 1;
18334
        goto meta_command_exit;
18335
      }
18336
      if( nSep>1 ){
18337
        raw_printf(stderr,
18338
              "Error: multi-character column separators not allowed"
18339
              " for import\n");
18340
        rc = 1;
18341
        goto meta_command_exit;
18342
      }
18343
      nSep = strlen30(p->rowSeparator);
18344
      if( nSep==0 ){
18345
        raw_printf(stderr,
18346
            "Error: non-null row separator required for import\n");
18347
        rc = 1;
18348
        goto meta_command_exit;
18349
      }
18350
      if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
18351
        /* When importing CSV (only), if the row separator is set to the
18352
        ** default output row separator, change it to the default input
18353
        ** row separator.  This avoids having to maintain different input
18354
        ** and output row separators. */
18355
        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18356
        nSep = strlen30(p->rowSeparator);
18357
      }
18358
      if( nSep>1 ){
18359
        raw_printf(stderr, "Error: multi-character row separators not allowed"
18360
                           " for import\n");
18361
        rc = 1;
18362
        goto meta_command_exit;
18363
      }
18364
      sCtx.cColSep = p->colSeparator[0];
18365
      sCtx.cRowSep = p->rowSeparator[0];
18366
    }
18367
    sCtx.zFile = zFile;
18368
    sCtx.nLine = 1;
18369
    if( sCtx.zFile[0]=='|' ){
18370
#ifdef SQLITE_OMIT_POPEN
18371
      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
18372
      rc = 1;
18373
      goto meta_command_exit;
18374
#else
18375
      sCtx.in = popen(sCtx.zFile+1, "r");
18376
      sCtx.zFile = "";
18377
      sCtx.xCloser = pclose;
18378
#endif
18379
    }else{
18380
      sCtx.in = fopen(sCtx.zFile, "rb");
18381
      sCtx.xCloser = fclose;
18382
    }
18383
    if( sCtx.in==0 ){
18384
      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
18385
      rc = 1;
18386
      goto meta_command_exit;
18387
    }
18388
    if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
18389
      char zSep[2];
18390
      zSep[1] = 0;
18391
      zSep[0] = sCtx.cColSep;
18392
      utf8_printf(p->out, "Column separator ");
18393
      output_c_string(p->out, zSep);
18394
      utf8_printf(p->out, ", row separator ");
18395
      zSep[0] = sCtx.cRowSep;
18396
      output_c_string(p->out, zSep);
18397
      utf8_printf(p->out, "\n");
18398
    }
18399
    while( (nSkip--)>0 ){
18400
      while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
18401
    }
18402
    zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
18403
    if( zSql==0 ){
18404
      import_cleanup(&sCtx);
18405
      shell_out_of_memory();
18406
    }
18407
    nByte = strlen30(zSql);
18408
    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18409
    import_append_char(&sCtx, 0);    /* To ensure sCtx.z is allocated */
18410
    if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
18411
      char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
18412
      char cSep = '(';
18413
      while( xRead(&sCtx) ){
18414
        zCreate = sqlite3_mprintf("%z%c\n  \"%w\" TEXT", zCreate, cSep, sCtx.z);
18415
        cSep = ',';
18416
        if( sCtx.cTerm!=sCtx.cColSep ) break;
18417
      }
18418
      if( cSep=='(' ){
18419
        sqlite3_free(zCreate);
18420
        import_cleanup(&sCtx);
18421
        utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
18422
        rc = 1;
18423
        goto meta_command_exit;
18424
      }
18425
      zCreate = sqlite3_mprintf("%z\n)", zCreate);
18426
      if( eVerbose>=1 ){
18427
        utf8_printf(p->out, "%s\n", zCreate);
18428
      }
18429
      rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
18430
      sqlite3_free(zCreate);
18431
      if( rc ){
18432
        utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
18433
                sqlite3_errmsg(p->db));
18434
        import_cleanup(&sCtx);
18435
        rc = 1;
18436
        goto meta_command_exit;
18437
      }
18438
      rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18439
    }
18440
    sqlite3_free(zSql);
18441
    if( rc ){
18442
      if (pStmt) sqlite3_finalize(pStmt);
18443
      utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
18444
      import_cleanup(&sCtx);
18445
      rc = 1;
18446
      goto meta_command_exit;
18447
    }
18448
    nCol = sqlite3_column_count(pStmt);
18449
    sqlite3_finalize(pStmt);
18450
    pStmt = 0;
18451
    if( nCol==0 ) return 0; /* no columns, no error */
18452
    zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
18453
    if( zSql==0 ){
18454
      import_cleanup(&sCtx);
18455
      shell_out_of_memory();
18456
    }
18457
    sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
18458
    j = strlen30(zSql);
18459
    for(i=1; i
18460
      zSql[j++] = ',';
18461
      zSql[j++] = '?';
18462
    }
18463
    zSql[j++] = ')';
18464
    zSql[j] = 0;
18465
    if( eVerbose>=2 ){
18466
      utf8_printf(p->out, "Insert using: %s\n", zSql);
18467
    }
18468
    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18469
    sqlite3_free(zSql);
18470
    if( rc ){
18471
      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
18472
      if (pStmt) sqlite3_finalize(pStmt);
18473
      import_cleanup(&sCtx);
18474
      rc = 1;
18475
      goto meta_command_exit;
18476
    }
18477
    needCommit = sqlite3_get_autocommit(p->db);
18478
    if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
18479
    do{
18480
      int startLine = sCtx.nLine;
18481
      for(i=0; i
18482
        char *z = xRead(&sCtx);
18483
        /*
18484
        ** Did we reach end-of-file before finding any columns?
18485
        ** If so, stop instead of NULL filling the remaining columns.
18486
        */
18487
        if( z==0 && i==0 ) break;
18488
        /*
18489
        ** Did we reach end-of-file OR end-of-line before finding any
18490
        ** columns in ASCII mode?  If so, stop instead of NULL filling
18491
        ** the remaining columns.
18492
        */
18493
        if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
18494
        sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
18495
        if( i
18496
          utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
18497
                          "filling the rest with NULL\n",
18498
                          sCtx.zFile, startLine, nCol, i+1);
18499
          i += 2;
18500
          while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
18501
        }
18502
      }
18503
      if( sCtx.cTerm==sCtx.cColSep ){
18504
        do{
18505
          xRead(&sCtx);
18506
          i++;
18507
        }while( sCtx.cTerm==sCtx.cColSep );
18508
        utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
18509
                        "extras ignored\n",
18510
                        sCtx.zFile, startLine, nCol, i);
18511
      }
18512
      if( i>=nCol ){
18513
        sqlite3_step(pStmt);
18514
        rc = sqlite3_reset(pStmt);
18515
        if( rc!=SQLITE_OK ){
18516
          utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
18517
                      startLine, sqlite3_errmsg(p->db));
18518
          sCtx.nErr++;
18519
        }else{
18520
          sCtx.nRow++;
18521
        }
18522
      }
18523
    }while( sCtx.cTerm!=EOF );
18524
 
18525
    import_cleanup(&sCtx);
18526
    sqlite3_finalize(pStmt);
18527
    if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
18528
    if( eVerbose>0 ){
18529
      utf8_printf(p->out,
18530
          "Added %d rows with %d errors using %d lines of input\n",
18531
          sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
18532
    }
18533
  }else
18534
 
18535
#ifndef SQLITE_UNTESTABLE
18536
  if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
18537
    char *zSql;
18538
    char *zCollist = 0;
18539
    sqlite3_stmt *pStmt;
18540
    int tnum = 0;
18541
    int isWO = 0;  /* True if making an imposter of a WITHOUT ROWID table */
18542
    int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
18543
    int i;
18544
    if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
18545
      utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
18546
                          "       .imposter off\n");
18547
      /* Also allowed, but not documented:
18548
      **
18549
      **    .imposter TABLE IMPOSTER
18550
      **
18551
      ** where TABLE is a WITHOUT ROWID table.  In that case, the
18552
      ** imposter is another WITHOUT ROWID table with the columns in
18553
      ** storage order. */
18554
      rc = 1;
18555
      goto meta_command_exit;
18556
    }
18557
    open_db(p, 0);
18558
    if( nArg==2 ){
18559
      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
18560
      goto meta_command_exit;
18561
    }
18562
    zSql = sqlite3_mprintf(
18563
      "SELECT rootpage, 0 FROM sqlite_schema"
18564
      " WHERE name='%q' AND type='index'"
18565
      "UNION ALL "
18566
      "SELECT rootpage, 1 FROM sqlite_schema"
18567
      " WHERE name='%q' AND type='table'"
18568
      "   AND sql LIKE '%%without%%rowid%%'",
18569
      azArg[1], azArg[1]
18570
    );
18571
    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18572
    sqlite3_free(zSql);
18573
    if( sqlite3_step(pStmt)==SQLITE_ROW ){
18574
      tnum = sqlite3_column_int(pStmt, 0);
18575
      isWO = sqlite3_column_int(pStmt, 1);
18576
    }
18577
    sqlite3_finalize(pStmt);
18578
    zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
18579
    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18580
    sqlite3_free(zSql);
18581
    i = 0;
18582
    while( sqlite3_step(pStmt)==SQLITE_ROW ){
18583
      char zLabel[20];
18584
      const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
18585
      i++;
18586
      if( zCol==0 ){
18587
        if( sqlite3_column_int(pStmt,1)==-1 ){
18588
          zCol = "_ROWID_";
18589
        }else{
18590
          sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
18591
          zCol = zLabel;
18592
        }
18593
      }
18594
      if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
18595
        lenPK = (int)strlen(zCollist);
18596
      }
18597
      if( zCollist==0 ){
18598
        zCollist = sqlite3_mprintf("\"%w\"", zCol);
18599
      }else{
18600
        zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
18601
      }
18602
    }
18603
    sqlite3_finalize(pStmt);
18604
    if( i==0 || tnum==0 ){
18605
      utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
18606
      rc = 1;
18607
      sqlite3_free(zCollist);
18608
      goto meta_command_exit;
18609
    }
18610
    if( lenPK==0 ) lenPK = 100000;
18611
    zSql = sqlite3_mprintf(
18612
          "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
18613
          azArg[2], zCollist, lenPK, zCollist);
18614
    sqlite3_free(zCollist);
18615
    rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
18616
    if( rc==SQLITE_OK ){
18617
      rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
18618
      sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
18619
      if( rc ){
18620
        utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
18621
      }else{
18622
        utf8_printf(stdout, "%s;\n", zSql);
18623
        raw_printf(stdout,
18624
          "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
18625
          azArg[1], isWO ? "table" : "index"
18626
        );
18627
      }
18628
    }else{
18629
      raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
18630
      rc = 1;
18631
    }
18632
    sqlite3_free(zSql);
18633
  }else
18634
#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
18635
 
18636
#ifdef SQLITE_ENABLE_IOTRACE
18637
  if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
18638
    SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
18639
    if( iotrace && iotrace!=stdout ) fclose(iotrace);
18640
    iotrace = 0;
18641
    if( nArg<2 ){
18642
      sqlite3IoTrace = 0;
18643
    }else if( strcmp(azArg[1], "-")==0 ){
18644
      sqlite3IoTrace = iotracePrintf;
18645
      iotrace = stdout;
18646
    }else{
18647
      iotrace = fopen(azArg[1], "w");
18648
      if( iotrace==0 ){
18649
        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
18650
        sqlite3IoTrace = 0;
18651
        rc = 1;
18652
      }else{
18653
        sqlite3IoTrace = iotracePrintf;
18654
      }
18655
    }
18656
  }else
18657
#endif
18658
 
18659
  if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
18660
    static const struct {
18661
       const char *zLimitName;   /* Name of a limit */
18662
       int limitCode;            /* Integer code for that limit */
18663
    } aLimit[] = {
18664
      { "length",                SQLITE_LIMIT_LENGTH                    },
18665
      { "sql_length",            SQLITE_LIMIT_SQL_LENGTH                },
18666
      { "column",                SQLITE_LIMIT_COLUMN                    },
18667
      { "expr_depth",            SQLITE_LIMIT_EXPR_DEPTH                },
18668
      { "compound_select",       SQLITE_LIMIT_COMPOUND_SELECT           },
18669
      { "vdbe_op",               SQLITE_LIMIT_VDBE_OP                   },
18670
      { "function_arg",          SQLITE_LIMIT_FUNCTION_ARG              },
18671
      { "attached",              SQLITE_LIMIT_ATTACHED                  },
18672
      { "like_pattern_length",   SQLITE_LIMIT_LIKE_PATTERN_LENGTH       },
18673
      { "variable_number",       SQLITE_LIMIT_VARIABLE_NUMBER           },
18674
      { "trigger_depth",         SQLITE_LIMIT_TRIGGER_DEPTH             },
18675
      { "worker_threads",        SQLITE_LIMIT_WORKER_THREADS            },
18676
    };
18677
    int i, n2;
18678
    open_db(p, 0);
18679
    if( nArg==1 ){
18680
      for(i=0; i
18681
        printf("%20s %d\n", aLimit[i].zLimitName,
18682
               sqlite3_limit(p->db, aLimit[i].limitCode, -1));
18683
      }
18684
    }else if( nArg>3 ){
18685
      raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
18686
      rc = 1;
18687
      goto meta_command_exit;
18688
    }else{
18689
      int iLimit = -1;
18690
      n2 = strlen30(azArg[1]);
18691
      for(i=0; i
18692
        if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
18693
          if( iLimit<0 ){
18694
            iLimit = i;
18695
          }else{
18696
            utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
18697
            rc = 1;
18698
            goto meta_command_exit;
18699
          }
18700
        }
18701
      }
18702
      if( iLimit<0 ){
18703
        utf8_printf(stderr, "unknown limit: \"%s\"\n"
18704
                        "enter \".limits\" with no arguments for a list.\n",
18705
                         azArg[1]);
18706
        rc = 1;
18707
        goto meta_command_exit;
18708
      }
18709
      if( nArg==3 ){
18710
        sqlite3_limit(p->db, aLimit[iLimit].limitCode,
18711
                      (int)integerValue(azArg[2]));
18712
      }
18713
      printf("%20s %d\n", aLimit[iLimit].zLimitName,
18714
             sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
18715
    }
18716
  }else
18717
 
18718
  if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
18719
    open_db(p, 0);
18720
    lintDotCommand(p, azArg, nArg);
18721
  }else
18722
 
18723
#ifndef SQLITE_OMIT_LOAD_EXTENSION
18724
  if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
18725
    const char *zFile, *zProc;
18726
    char *zErrMsg = 0;
18727
    if( nArg<2 ){
18728
      raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
18729
      rc = 1;
18730
      goto meta_command_exit;
18731
    }
18732
    zFile = azArg[1];
18733
    zProc = nArg>=3 ? azArg[2] : 0;
18734
    open_db(p, 0);
18735
    rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
18736
    if( rc!=SQLITE_OK ){
18737
      utf8_printf(stderr, "Error: %s\n", zErrMsg);
18738
      sqlite3_free(zErrMsg);
18739
      rc = 1;
18740
    }
18741
  }else
18742
#endif
18743
 
18744
  if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
18745
    if( nArg!=2 ){
18746
      raw_printf(stderr, "Usage: .log FILENAME\n");
18747
      rc = 1;
18748
    }else{
18749
      const char *zFile = azArg[1];
18750
      output_file_close(p->pLog);
18751
      p->pLog = output_file_open(zFile, 0);
18752
    }
18753
  }else
18754
 
18755
  if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
18756
    const char *zMode = nArg>=2 ? azArg[1] : "";
18757
    int n2 = strlen30(zMode);
18758
    int c2 = zMode[0];
18759
    if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
18760
      p->mode = MODE_Line;
18761
      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18762
    }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
18763
      p->mode = MODE_Column;
18764
      if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
18765
        p->showHeader = 1;
18766
      }
18767
      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18768
    }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
18769
      p->mode = MODE_List;
18770
      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
18771
      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18772
    }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
18773
      p->mode = MODE_Html;
18774
    }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
18775
      p->mode = MODE_Tcl;
18776
      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
18777
      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18778
    }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
18779
      p->mode = MODE_Csv;
18780
      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18781
      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
18782
    }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
18783
      p->mode = MODE_List;
18784
      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
18785
    }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
18786
      p->mode = MODE_Insert;
18787
      set_table_name(p, nArg>=3 ? azArg[2] : "table");
18788
    }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
18789
      p->mode = MODE_Quote;
18790
      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18791
      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18792
    }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
18793
      p->mode = MODE_Ascii;
18794
      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
18795
      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
18796
    }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
18797
      p->mode = MODE_Markdown;
18798
    }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
18799
      p->mode = MODE_Table;
18800
    }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
18801
      p->mode = MODE_Box;
18802
    }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
18803
      p->mode = MODE_Json;
18804
    }else if( nArg==1 ){
18805
      raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
18806
    }else{
18807
      raw_printf(stderr, "Error: mode should be one of: "
18808
         "ascii box column csv html insert json line list markdown "
18809
         "quote table tabs tcl\n");
18810
      rc = 1;
18811
    }
18812
    p->cMode = p->mode;
18813
  }else
18814
 
18815
  if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
18816
    if( nArg==2 ){
18817
      sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
18818
                       "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
18819
    }else{
18820
      raw_printf(stderr, "Usage: .nullvalue STRING\n");
18821
      rc = 1;
18822
    }
18823
  }else
18824
 
18825
#ifdef SQLITE_DEBUG
18826
  if( c=='o' && strcmp(azArg[0],"oom")==0 ){
18827
    int i;
18828
    for(i=1; i
18829
      const char *z = azArg[i];
18830
      if( z[0]=='-' && z[1]=='-' ) z++;
18831
      if( strcmp(z,"-repeat")==0 ){
18832
        if( i==nArg-1 ){
18833
          raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
18834
          rc = 1;
18835
        }else{
18836
          oomRepeat = (int)integerValue(azArg[++i]);
18837
        }
18838
      }else if( IsDigit(z[0]) ){
18839
        oomCounter = (int)integerValue(azArg[i]);
18840
      }else{
18841
        raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
18842
        raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
18843
        rc = 1;
18844
      }
18845
    }
18846
    if( rc==0 ){
18847
      raw_printf(p->out, "oomCounter = %d\n", oomCounter);
18848
      raw_printf(p->out, "oomRepeat  = %d\n", oomRepeat);
18849
    }
18850
  }else
18851
#endif /* SQLITE_DEBUG */
18852
 
18853
  if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
18854
    char *zNewFilename = 0;  /* Name of the database file to open */
18855
    int iName = 1;           /* Index in azArg[] of the filename */
18856
    int newFlag = 0;         /* True to delete file before opening */
18857
    /* Close the existing database */
18858
    session_close_all(p);
18859
    close_db(p->db);
18860
    p->db = 0;
18861
    p->zDbFilename = 0;
18862
    sqlite3_free(p->zFreeOnClose);
18863
    p->zFreeOnClose = 0;
18864
    p->openMode = SHELL_OPEN_UNSPEC;
18865
    p->openFlags = 0;
18866
    p->szMax = 0;
18867
    /* Check for command-line arguments */
18868
    for(iName=1; iName
18869
      const char *z = azArg[iName];
18870
      if( optionMatch(z,"new") ){
18871
        newFlag = 1;
18872
#ifdef SQLITE_HAVE_ZLIB
18873
      }else if( optionMatch(z, "zip") ){
18874
        p->openMode = SHELL_OPEN_ZIPFILE;
18875
#endif
18876
      }else if( optionMatch(z, "append") ){
18877
        p->openMode = SHELL_OPEN_APPENDVFS;
18878
      }else if( optionMatch(z, "readonly") ){
18879
        p->openMode = SHELL_OPEN_READONLY;
18880
      }else if( optionMatch(z, "nofollow") ){
18881
        p->openFlags |= SQLITE_OPEN_NOFOLLOW;
18882
#ifdef SQLITE_ENABLE_DESERIALIZE
18883
      }else if( optionMatch(z, "deserialize") ){
18884
        p->openMode = SHELL_OPEN_DESERIALIZE;
18885
      }else if( optionMatch(z, "hexdb") ){
18886
        p->openMode = SHELL_OPEN_HEXDB;
18887
      }else if( optionMatch(z, "maxsize") && iName+1
18888
        p->szMax = integerValue(azArg[++iName]);
18889
#endif /* SQLITE_ENABLE_DESERIALIZE */
18890
      }else if( z[0]=='-' ){
18891
        utf8_printf(stderr, "unknown option: %s\n", z);
18892
        rc = 1;
18893
        goto meta_command_exit;
18894
      }else if( zNewFilename ){
18895
        utf8_printf(stderr, "extra argument: \"%s\"\n", z);
18896
        rc = 1;
18897
        goto meta_command_exit;
18898
      }else{
18899
        zNewFilename = sqlite3_mprintf("%s", z);
18900
      }
18901
    }
18902
    /* If a filename is specified, try to open it first */
18903
    if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
18904
      if( newFlag ) shellDeleteFile(zNewFilename);
18905
      p->zDbFilename = zNewFilename;
18906
      open_db(p, OPEN_DB_KEEPALIVE);
18907
      if( p->db==0 ){
18908
        utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
18909
        sqlite3_free(zNewFilename);
18910
      }else{
18911
        p->zFreeOnClose = zNewFilename;
18912
      }
18913
    }
18914
    if( p->db==0 ){
18915
      /* As a fall-back open a TEMP database */
18916
      p->zDbFilename = 0;
18917
      open_db(p, 0);
18918
    }
18919
  }else
18920
 
18921
  if( (c=='o'
18922
        && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
18923
   || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
18924
  ){
18925
    char *zFile = 0;
18926
    int bTxtMode = 0;
18927
    int i;
18928
    int eMode = 0;
18929
    int bBOM = 0;
18930
    int bOnce = 0;  /* 0: .output, 1: .once, 2: .excel */
18931
 
18932
    if( c=='e' ){
18933
      eMode = 'x';
18934
      bOnce = 2;
18935
    }else if( strncmp(azArg[0],"once",n)==0 ){
18936
      bOnce = 1;
18937
    }
18938
    for(i=1; i
18939
      char *z = azArg[i];
18940
      if( z[0]=='-' ){
18941
        if( z[1]=='-' ) z++;
18942
        if( strcmp(z,"-bom")==0 ){
18943
          bBOM = 1;
18944
        }else if( c!='e' && strcmp(z,"-x")==0 ){
18945
          eMode = 'x';  /* spreadsheet */
18946
        }else if( c!='e' && strcmp(z,"-e")==0 ){
18947
          eMode = 'e';  /* text editor */
18948
        }else{
18949
          utf8_printf(p->out, "ERROR: unknown option: \"%s\".  Usage:\n",
18950
                      azArg[i]);
18951
          showHelp(p->out, azArg[0]);
18952
          rc = 1;
18953
          goto meta_command_exit;
18954
        }
18955
      }else if( zFile==0 && eMode!='e' && eMode!='x' ){
18956
        zFile = sqlite3_mprintf("%s", z);
18957
        if( zFile[0]=='|' ){
18958
          while( i+1
18959
          break;
18960
        }
18961
      }else{
18962
        utf8_printf(p->out,"ERROR: extra parameter: \"%s\".  Usage:\n",
18963
                    azArg[i]);
18964
        showHelp(p->out, azArg[0]);
18965
        rc = 1;
18966
        sqlite3_free(zFile);
18967
        goto meta_command_exit;
18968
      }
18969
    }
18970
    if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
18971
    if( bOnce ){
18972
      p->outCount = 2;
18973
    }else{
18974
      p->outCount = 0;
18975
    }
18976
    output_reset(p);
18977
#ifndef SQLITE_NOHAVE_SYSTEM
18978
    if( eMode=='e' || eMode=='x' ){
18979
      p->doXdgOpen = 1;
18980
      outputModePush(p);
18981
      if( eMode=='x' ){
18982
        /* spreadsheet mode.  Output as CSV. */
18983
        newTempFile(p, "csv");
18984
        ShellClearFlag(p, SHFLG_Echo);
18985
        p->mode = MODE_Csv;
18986
        sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18987
        sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
18988
      }else{
18989
        /* text editor mode */
18990
        newTempFile(p, "txt");
18991
        bTxtMode = 1;
18992
      }
18993
      sqlite3_free(zFile);
18994
      zFile = sqlite3_mprintf("%s", p->zTempFile);
18995
    }
18996
#endif /* SQLITE_NOHAVE_SYSTEM */
18997
    if( zFile[0]=='|' ){
18998
#ifdef SQLITE_OMIT_POPEN
18999
      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
19000
      rc = 1;
19001
      p->out = stdout;
19002
#else
19003
      p->out = popen(zFile + 1, "w");
19004
      if( p->out==0 ){
19005
        utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
19006
        p->out = stdout;
19007
        rc = 1;
19008
      }else{
19009
        if( bBOM ) fprintf(p->out,"\357\273\277");
19010
        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
19011
      }
19012
#endif
19013
    }else{
19014
      p->out = output_file_open(zFile, bTxtMode);
19015
      if( p->out==0 ){
19016
        if( strcmp(zFile,"off")!=0 ){
19017
          utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
19018
        }
19019
        p->out = stdout;
19020
        rc = 1;
19021
      } else {
19022
        if( bBOM ) fprintf(p->out,"\357\273\277");
19023
        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
19024
      }
19025
    }
19026
    sqlite3_free(zFile);
19027
  }else
19028
 
19029
  if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
19030
    open_db(p,0);
19031
    if( nArg<=1 ) goto parameter_syntax_error;
19032
 
19033
    /* .parameter clear
19034
    ** Clear all bind parameters by dropping the TEMP table that holds them.
19035
    */
19036
    if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
19037
      sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
19038
                   0, 0, 0);
19039
    }else
19040
 
19041
    /* .parameter list
19042
    ** List all bind parameters.
19043
    */
19044
    if( nArg==2 && strcmp(azArg[1],"list")==0 ){
19045
      sqlite3_stmt *pStmt = 0;
19046
      int rx;
19047
      int len = 0;
19048
      rx = sqlite3_prepare_v2(p->db,
19049
             "SELECT max(length(key)) "
19050
             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
19051
      if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
19052
        len = sqlite3_column_int(pStmt, 0);
19053
        if( len>40 ) len = 40;
19054
      }
19055
      sqlite3_finalize(pStmt);
19056
      pStmt = 0;
19057
      if( len ){
19058
        rx = sqlite3_prepare_v2(p->db,
19059
             "SELECT key, quote(value) "
19060
             "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
19061
        while( sqlite3_step(pStmt)==SQLITE_ROW ){
19062
          utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
19063
                      sqlite3_column_text(pStmt,1));
19064
        }
19065
        sqlite3_finalize(pStmt);
19066
      }
19067
    }else
19068
 
19069
    /* .parameter init
19070
    ** Make sure the TEMP table used to hold bind parameters exists.
19071
    ** Create it if necessary.
19072
    */
19073
    if( nArg==2 && strcmp(azArg[1],"init")==0 ){
19074
      bind_table_init(p);
19075
    }else
19076
 
19077
    /* .parameter set NAME VALUE
19078
    ** Set or reset a bind parameter.  NAME should be the full parameter
19079
    ** name exactly as it appears in the query.  (ex: $abc, @def).  The
19080
    ** VALUE can be in either SQL literal notation, or if not it will be
19081
    ** understood to be a text string.
19082
    */
19083
    if( nArg==4 && strcmp(azArg[1],"set")==0 ){
19084
      int rx;
19085
      char *zSql;
19086
      sqlite3_stmt *pStmt;
19087
      const char *zKey = azArg[2];
19088
      const char *zValue = azArg[3];
19089
      bind_table_init(p);
19090
      zSql = sqlite3_mprintf(
19091
                  "REPLACE INTO temp.sqlite_parameters(key,value)"
19092
                  "VALUES(%Q,%s);", zKey, zValue);
19093
      if( zSql==0 ) shell_out_of_memory();
19094
      pStmt = 0;
19095
      rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19096
      sqlite3_free(zSql);
19097
      if( rx!=SQLITE_OK ){
19098
        sqlite3_finalize(pStmt);
19099
        pStmt = 0;
19100
        zSql = sqlite3_mprintf(
19101
                   "REPLACE INTO temp.sqlite_parameters(key,value)"
19102
                   "VALUES(%Q,%Q);", zKey, zValue);
19103
        if( zSql==0 ) shell_out_of_memory();
19104
        rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19105
        sqlite3_free(zSql);
19106
        if( rx!=SQLITE_OK ){
19107
          utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
19108
          sqlite3_finalize(pStmt);
19109
          pStmt = 0;
19110
          rc = 1;
19111
        }
19112
      }
19113
      sqlite3_step(pStmt);
19114
      sqlite3_finalize(pStmt);
19115
    }else
19116
 
19117
    /* .parameter unset NAME
19118
    ** Remove the NAME binding from the parameter binding table, if it
19119
    ** exists.
19120
    */
19121
    if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
19122
      char *zSql = sqlite3_mprintf(
19123
          "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
19124
      if( zSql==0 ) shell_out_of_memory();
19125
      sqlite3_exec(p->db, zSql, 0, 0, 0);
19126
      sqlite3_free(zSql);
19127
    }else
19128
    /* If no command name matches, show a syntax error */
19129
    parameter_syntax_error:
19130
    showHelp(p->out, "parameter");
19131
  }else
19132
 
19133
  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
19134
    int i;
19135
    for(i=1; i
19136
      if( i>1 ) raw_printf(p->out, " ");
19137
      utf8_printf(p->out, "%s", azArg[i]);
19138
    }
19139
    raw_printf(p->out, "\n");
19140
  }else
19141
 
19142
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
19143
  if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
19144
    int i;
19145
    int nn = 0;
19146
    p->flgProgress = 0;
19147
    p->mxProgress = 0;
19148
    p->nProgress = 0;
19149
    for(i=1; i
19150
      const char *z = azArg[i];
19151
      if( z[0]=='-' ){
19152
        z++;
19153
        if( z[0]=='-' ) z++;
19154
        if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
19155
          p->flgProgress |= SHELL_PROGRESS_QUIET;
19156
          continue;
19157
        }
19158
        if( strcmp(z,"reset")==0 ){
19159
          p->flgProgress |= SHELL_PROGRESS_RESET;
19160
          continue;
19161
        }
19162
        if( strcmp(z,"once")==0 ){
19163
          p->flgProgress |= SHELL_PROGRESS_ONCE;
19164
          continue;
19165
        }
19166
        if( strcmp(z,"limit")==0 ){
19167
          if( i+1>=nArg ){
19168
            utf8_printf(stderr, "Error: missing argument on --limit\n");
19169
            rc = 1;
19170
            goto meta_command_exit;
19171
          }else{
19172
            p->mxProgress = (int)integerValue(azArg[++i]);
19173
          }
19174
          continue;
19175
        }
19176
        utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
19177
        rc = 1;
19178
        goto meta_command_exit;
19179
      }else{
19180
        nn = (int)integerValue(z);
19181
      }
19182
    }
19183
    open_db(p, 0);
19184
    sqlite3_progress_handler(p->db, nn, progress_handler, p);
19185
  }else
19186
#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
19187
 
19188
  if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
19189
    if( nArg >= 2) {
19190
      strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
19191
    }
19192
    if( nArg >= 3) {
19193
      strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
19194
    }
19195
  }else
19196
 
19197
  if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
19198
    rc = 2;
19199
  }else
19200
 
19201
  if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
19202
    FILE *inSaved = p->in;
19203
    int savedLineno = p->lineno;
19204
    if( nArg!=2 ){
19205
      raw_printf(stderr, "Usage: .read FILE\n");
19206
      rc = 1;
19207
      goto meta_command_exit;
19208
    }
19209
    if( azArg[1][0]=='|' ){
19210
#ifdef SQLITE_OMIT_POPEN
19211
      raw_printf(stderr, "Error: pipes are not supported in this OS\n");
19212
      rc = 1;
19213
      p->out = stdout;
19214
#else
19215
      p->in = popen(azArg[1]+1, "r");
19216
      if( p->in==0 ){
19217
        utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
19218
        rc = 1;
19219
      }else{
19220
        rc = process_input(p);
19221
        pclose(p->in);
19222
      }
19223
#endif
19224
    }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
19225
      utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
19226
      rc = 1;
19227
    }else{
19228
      rc = process_input(p);
19229
      fclose(p->in);
19230
    }
19231
    p->in = inSaved;
19232
    p->lineno = savedLineno;
19233
  }else
19234
 
19235
  if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
19236
    const char *zSrcFile;
19237
    const char *zDb;
19238
    sqlite3 *pSrc;
19239
    sqlite3_backup *pBackup;
19240
    int nTimeout = 0;
19241
 
19242
    if( nArg==2 ){
19243
      zSrcFile = azArg[1];
19244
      zDb = "main";
19245
    }else if( nArg==3 ){
19246
      zSrcFile = azArg[2];
19247
      zDb = azArg[1];
19248
    }else{
19249
      raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
19250
      rc = 1;
19251
      goto meta_command_exit;
19252
    }
19253
    rc = sqlite3_open(zSrcFile, &pSrc);
19254
    if( rc!=SQLITE_OK ){
19255
      utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
19256
      close_db(pSrc);
19257
      return 1;
19258
    }
19259
    open_db(p, 0);
19260
    pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
19261
    if( pBackup==0 ){
19262
      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19263
      close_db(pSrc);
19264
      return 1;
19265
    }
19266
    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
19267
          || rc==SQLITE_BUSY  ){
19268
      if( rc==SQLITE_BUSY ){
19269
        if( nTimeout++ >= 3 ) break;
19270
        sqlite3_sleep(100);
19271
      }
19272
    }
19273
    sqlite3_backup_finish(pBackup);
19274
    if( rc==SQLITE_DONE ){
19275
      rc = 0;
19276
    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
19277
      raw_printf(stderr, "Error: source database is busy\n");
19278
      rc = 1;
19279
    }else{
19280
      utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19281
      rc = 1;
19282
    }
19283
    close_db(pSrc);
19284
  }else
19285
 
19286
  if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
19287
    if( nArg==2 ){
19288
      p->scanstatsOn = (u8)booleanValue(azArg[1]);
19289
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
19290
      raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
19291
#endif
19292
    }else{
19293
      raw_printf(stderr, "Usage: .scanstats on|off\n");
19294
      rc = 1;
19295
    }
19296
  }else
19297
 
19298
  if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
19299
    ShellText sSelect;
19300
    ShellState data;
19301
    char *zErrMsg = 0;
19302
    const char *zDiv = "(";
19303
    const char *zName = 0;
19304
    int iSchema = 0;
19305
    int bDebug = 0;
19306
    int bNoSystemTabs = 0;
19307
    int ii;
19308
 
19309
    open_db(p, 0);
19310
    memcpy(&data, p, sizeof(data));
19311
    data.showHeader = 0;
19312
    data.cMode = data.mode = MODE_Semi;
19313
    initText(&sSelect);
19314
    for(ii=1; ii
19315
      if( optionMatch(azArg[ii],"indent") ){
19316
        data.cMode = data.mode = MODE_Pretty;
19317
      }else if( optionMatch(azArg[ii],"debug") ){
19318
        bDebug = 1;
19319
      }else if( optionMatch(azArg[ii],"nosys") ){
19320
        bNoSystemTabs = 1;
19321
      }else if( azArg[ii][0]=='-' ){
19322
        utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
19323
        rc = 1;
19324
        goto meta_command_exit;
19325
      }else if( zName==0 ){
19326
        zName = azArg[ii];
19327
      }else{
19328
        raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
19329
        rc = 1;
19330
        goto meta_command_exit;
19331
      }
19332
    }
19333
    if( zName!=0 ){
19334
      int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
19335
                  || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
19336
                  || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
19337
                  || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
19338
      if( isSchema ){
19339
        char *new_argv[2], *new_colv[2];
19340
        new_argv[0] = sqlite3_mprintf(
19341
                      "CREATE TABLE %s (\n"
19342
                      "  type text,\n"
19343
                      "  name text,\n"
19344
                      "  tbl_name text,\n"
19345
                      "  rootpage integer,\n"
19346
                      "  sql text\n"
19347
                      ")", zName);
19348
        new_argv[1] = 0;
19349
        new_colv[0] = "sql";
19350
        new_colv[1] = 0;
19351
        callback(&data, 1, new_argv, new_colv);
19352
        sqlite3_free(new_argv[0]);
19353
      }
19354
    }
19355
    if( zDiv ){
19356
      sqlite3_stmt *pStmt = 0;
19357
      rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
19358
                              -1, &pStmt, 0);
19359
      if( rc ){
19360
        utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19361
        sqlite3_finalize(pStmt);
19362
        rc = 1;
19363
        goto meta_command_exit;
19364
      }
19365
      appendText(&sSelect, "SELECT sql FROM", 0);
19366
      iSchema = 0;
19367
      while( sqlite3_step(pStmt)==SQLITE_ROW ){
19368
        const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
19369
        char zScNum[30];
19370
        sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
19371
        appendText(&sSelect, zDiv, 0);
19372
        zDiv = " UNION ALL ";
19373
        appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
19374
        if( sqlite3_stricmp(zDb, "main")!=0 ){
19375
          appendText(&sSelect, zDb, '\'');
19376
        }else{
19377
          appendText(&sSelect, "NULL", 0);
19378
        }
19379
        appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
19380
        appendText(&sSelect, zScNum, 0);
19381
        appendText(&sSelect, " AS snum, ", 0);
19382
        appendText(&sSelect, zDb, '\'');
19383
        appendText(&sSelect, " AS sname FROM ", 0);
19384
        appendText(&sSelect, zDb, quoteChar(zDb));
19385
        appendText(&sSelect, ".sqlite_schema", 0);
19386
      }
19387
      sqlite3_finalize(pStmt);
19388
#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
19389
      if( zName ){
19390
        appendText(&sSelect,
19391
           " UNION ALL SELECT shell_module_schema(name),"
19392
           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
19393
        0);
19394
      }
19395
#endif
19396
      appendText(&sSelect, ") WHERE ", 0);
19397
      if( zName ){
19398
        char *zQarg = sqlite3_mprintf("%Q", zName);
19399
        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
19400
                    strchr(zName, '[') != 0;
19401
        if( strchr(zName, '.') ){
19402
          appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
19403
        }else{
19404
          appendText(&sSelect, "lower(tbl_name)", 0);
19405
        }
19406
        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
19407
        appendText(&sSelect, zQarg, 0);
19408
        if( !bGlob ){
19409
          appendText(&sSelect, " ESCAPE '\\' ", 0);
19410
        }
19411
        appendText(&sSelect, " AND ", 0);
19412
        sqlite3_free(zQarg);
19413
      }
19414
      if( bNoSystemTabs ){
19415
        appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
19416
      }
19417
      appendText(&sSelect, "sql IS NOT NULL"
19418
                           " ORDER BY snum, rowid", 0);
19419
      if( bDebug ){
19420
        utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
19421
      }else{
19422
        rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
19423
      }
19424
      freeText(&sSelect);
19425
    }
19426
    if( zErrMsg ){
19427
      utf8_printf(stderr,"Error: %s\n", zErrMsg);
19428
      sqlite3_free(zErrMsg);
19429
      rc = 1;
19430
    }else if( rc != SQLITE_OK ){
19431
      raw_printf(stderr,"Error: querying schema information\n");
19432
      rc = 1;
19433
    }else{
19434
      rc = 0;
19435
    }
19436
  }else
19437
 
19438
  if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
19439
    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
19440
    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
19441
  }else
19442
 
19443
#if defined(SQLITE_ENABLE_SESSION)
19444
  if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
19445
    OpenSession *pSession = &p->aSession[0];
19446
    char **azCmd = &azArg[1];
19447
    int iSes = 0;
19448
    int nCmd = nArg - 1;
19449
    int i;
19450
    if( nArg<=1 ) goto session_syntax_error;
19451
    open_db(p, 0);
19452
    if( nArg>=3 ){
19453
      for(iSes=0; iSesnSession; iSes++){
19454
        if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
19455
      }
19456
      if( iSesnSession ){
19457
        pSession = &p->aSession[iSes];
19458
        azCmd++;
19459
        nCmd--;
19460
      }else{
19461
        pSession = &p->aSession[0];
19462
        iSes = 0;
19463
      }
19464
    }
19465
 
19466
    /* .session attach TABLE
19467
    ** Invoke the sqlite3session_attach() interface to attach a particular
19468
    ** table so that it is never filtered.
19469
    */
19470
    if( strcmp(azCmd[0],"attach")==0 ){
19471
      if( nCmd!=2 ) goto session_syntax_error;
19472
      if( pSession->p==0 ){
19473
        session_not_open:
19474
        raw_printf(stderr, "ERROR: No sessions are open\n");
19475
      }else{
19476
        rc = sqlite3session_attach(pSession->p, azCmd[1]);
19477
        if( rc ){
19478
          raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
19479
          rc = 0;
19480
        }
19481
      }
19482
    }else
19483
 
19484
    /* .session changeset FILE
19485
    ** .session patchset FILE
19486
    ** Write a changeset or patchset into a file.  The file is overwritten.
19487
    */
19488
    if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
19489
      FILE *out = 0;
19490
      if( nCmd!=2 ) goto session_syntax_error;
19491
      if( pSession->p==0 ) goto session_not_open;
19492
      out = fopen(azCmd[1], "wb");
19493
      if( out==0 ){
19494
        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
19495
                    azCmd[1]);
19496
      }else{
19497
        int szChng;
19498
        void *pChng;
19499
        if( azCmd[0][0]=='c' ){
19500
          rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
19501
        }else{
19502
          rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
19503
        }
19504
        if( rc ){
19505
          printf("Error: error code %d\n", rc);
19506
          rc = 0;
19507
        }
19508
        if( pChng
19509
          && fwrite(pChng, szChng, 1, out)!=1 ){
19510
          raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
19511
                  szChng);
19512
        }
19513
        sqlite3_free(pChng);
19514
        fclose(out);
19515
      }
19516
    }else
19517
 
19518
    /* .session close
19519
    ** Close the identified session
19520
    */
19521
    if( strcmp(azCmd[0], "close")==0 ){
19522
      if( nCmd!=1 ) goto session_syntax_error;
19523
      if( p->nSession ){
19524
        session_close(pSession);
19525
        p->aSession[iSes] = p->aSession[--p->nSession];
19526
      }
19527
    }else
19528
 
19529
    /* .session enable ?BOOLEAN?
19530
    ** Query or set the enable flag
19531
    */
19532
    if( strcmp(azCmd[0], "enable")==0 ){
19533
      int ii;
19534
      if( nCmd>2 ) goto session_syntax_error;
19535
      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
19536
      if( p->nSession ){
19537
        ii = sqlite3session_enable(pSession->p, ii);
19538
        utf8_printf(p->out, "session %s enable flag = %d\n",
19539
                    pSession->zName, ii);
19540
      }
19541
    }else
19542
 
19543
    /* .session filter GLOB ....
19544
    ** Set a list of GLOB patterns of table names to be excluded.
19545
    */
19546
    if( strcmp(azCmd[0], "filter")==0 ){
19547
      int ii, nByte;
19548
      if( nCmd<2 ) goto session_syntax_error;
19549
      if( p->nSession ){
19550
        for(ii=0; iinFilter; ii++){
19551
          sqlite3_free(pSession->azFilter[ii]);
19552
        }
19553
        sqlite3_free(pSession->azFilter);
19554
        nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
19555
        pSession->azFilter = sqlite3_malloc( nByte );
19556
        if( pSession->azFilter==0 ){
19557
          raw_printf(stderr, "Error: out or memory\n");
19558
          exit(1);
19559
        }
19560
        for(ii=1; ii
19561
          pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
19562
        }
19563
        pSession->nFilter = ii-1;
19564
      }
19565
    }else
19566
 
19567
    /* .session indirect ?BOOLEAN?
19568
    ** Query or set the indirect flag
19569
    */
19570
    if( strcmp(azCmd[0], "indirect")==0 ){
19571
      int ii;
19572
      if( nCmd>2 ) goto session_syntax_error;
19573
      ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
19574
      if( p->nSession ){
19575
        ii = sqlite3session_indirect(pSession->p, ii);
19576
        utf8_printf(p->out, "session %s indirect flag = %d\n",
19577
                    pSession->zName, ii);
19578
      }
19579
    }else
19580
 
19581
    /* .session isempty
19582
    ** Determine if the session is empty
19583
    */
19584
    if( strcmp(azCmd[0], "isempty")==0 ){
19585
      int ii;
19586
      if( nCmd!=1 ) goto session_syntax_error;
19587
      if( p->nSession ){
19588
        ii = sqlite3session_isempty(pSession->p);
19589
        utf8_printf(p->out, "session %s isempty flag = %d\n",
19590
                    pSession->zName, ii);
19591
      }
19592
    }else
19593
 
19594
    /* .session list
19595
    ** List all currently open sessions
19596
    */
19597
    if( strcmp(azCmd[0],"list")==0 ){
19598
      for(i=0; inSession; i++){
19599
        utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
19600
      }
19601
    }else
19602
 
19603
    /* .session open DB NAME
19604
    ** Open a new session called NAME on the attached database DB.
19605
    ** DB is normally "main".
19606
    */
19607
    if( strcmp(azCmd[0],"open")==0 ){
19608
      char *zName;
19609
      if( nCmd!=3 ) goto session_syntax_error;
19610
      zName = azCmd[2];
19611
      if( zName[0]==0 ) goto session_syntax_error;
19612
      for(i=0; inSession; i++){
19613
        if( strcmp(p->aSession[i].zName,zName)==0 ){
19614
          utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
19615
          goto meta_command_exit;
19616
        }
19617
      }
19618
      if( p->nSession>=ArraySize(p->aSession) ){
19619
        raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
19620
        goto meta_command_exit;
19621
      }
19622
      pSession = &p->aSession[p->nSession];
19623
      rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
19624
      if( rc ){
19625
        raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
19626
        rc = 0;
19627
        goto meta_command_exit;
19628
      }
19629
      pSession->nFilter = 0;
19630
      sqlite3session_table_filter(pSession->p, session_filter, pSession);
19631
      p->nSession++;
19632
      pSession->zName = sqlite3_mprintf("%s", zName);
19633
    }else
19634
    /* If no command name matches, show a syntax error */
19635
    session_syntax_error:
19636
    showHelp(p->out, "session");
19637
  }else
19638
#endif
19639
 
19640
#ifdef SQLITE_DEBUG
19641
  /* Undocumented commands for internal testing.  Subject to change
19642
  ** without notice. */
19643
  if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
19644
    if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
19645
      int i, v;
19646
      for(i=1; i
19647
        v = booleanValue(azArg[i]);
19648
        utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
19649
      }
19650
    }
19651
    if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
19652
      int i; sqlite3_int64 v;
19653
      for(i=1; i
19654
        char zBuf[200];
19655
        v = integerValue(azArg[i]);
19656
        sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
19657
        utf8_printf(p->out, "%s", zBuf);
19658
      }
19659
    }
19660
  }else
19661
#endif
19662
 
19663
  if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
19664
    int bIsInit = 0;         /* True to initialize the SELFTEST table */
19665
    int bVerbose = 0;        /* Verbose output */
19666
    int bSelftestExists;     /* True if SELFTEST already exists */
19667
    int i, k;                /* Loop counters */
19668
    int nTest = 0;           /* Number of tests runs */
19669
    int nErr = 0;            /* Number of errors seen */
19670
    ShellText str;           /* Answer for a query */
19671
    sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
19672
 
19673
    open_db(p,0);
19674
    for(i=1; i
19675
      const char *z = azArg[i];
19676
      if( z[0]=='-' && z[1]=='-' ) z++;
19677
      if( strcmp(z,"-init")==0 ){
19678
        bIsInit = 1;
19679
      }else
19680
      if( strcmp(z,"-v")==0 ){
19681
        bVerbose++;
19682
      }else
19683
      {
19684
        utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
19685
                    azArg[i], azArg[0]);
19686
        raw_printf(stderr, "Should be one of: --init -v\n");
19687
        rc = 1;
19688
        goto meta_command_exit;
19689
      }
19690
    }
19691
    if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
19692
           != SQLITE_OK ){
19693
      bSelftestExists = 0;
19694
    }else{
19695
      bSelftestExists = 1;
19696
    }
19697
    if( bIsInit ){
19698
      createSelftestTable(p);
19699
      bSelftestExists = 1;
19700
    }
19701
    initText(&str);
19702
    appendText(&str, "x", 0);
19703
    for(k=bSelftestExists; k>=0; k--){
19704
      if( k==1 ){
19705
        rc = sqlite3_prepare_v2(p->db,
19706
            "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
19707
            -1, &pStmt, 0);
19708
      }else{
19709
        rc = sqlite3_prepare_v2(p->db,
19710
          "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
19711
          "      (1,'run','PRAGMA integrity_check','ok')",
19712
          -1, &pStmt, 0);
19713
      }
19714
      if( rc ){
19715
        raw_printf(stderr, "Error querying the selftest table\n");
19716
        rc = 1;
19717
        sqlite3_finalize(pStmt);
19718
        goto meta_command_exit;
19719
      }
19720
      for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
19721
        int tno = sqlite3_column_int(pStmt, 0);
19722
        const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
19723
        const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
19724
        const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
19725
 
19726
        k = 0;
19727
        if( bVerbose>0 ){
19728
          char *zQuote = sqlite3_mprintf("%q", zSql);
19729
          printf("%d: %s %s\n", tno, zOp, zSql);
19730
          sqlite3_free(zQuote);
19731
        }
19732
        if( strcmp(zOp,"memo")==0 ){
19733
          utf8_printf(p->out, "%s\n", zSql);
19734
        }else
19735
        if( strcmp(zOp,"run")==0 ){
19736
          char *zErrMsg = 0;
19737
          str.n = 0;
19738
          str.z[0] = 0;
19739
          rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
19740
          nTest++;
19741
          if( bVerbose ){
19742
            utf8_printf(p->out, "Result: %s\n", str.z);
19743
          }
19744
          if( rc || zErrMsg ){
19745
            nErr++;
19746
            rc = 1;
19747
            utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
19748
            sqlite3_free(zErrMsg);
19749
          }else if( strcmp(zAns,str.z)!=0 ){
19750
            nErr++;
19751
            rc = 1;
19752
            utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
19753
            utf8_printf(p->out, "%d:      Got: [%s]\n", tno, str.z);
19754
          }
19755
        }else
19756
        {
19757
          utf8_printf(stderr,
19758
            "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
19759
          rc = 1;
19760
          break;
19761
        }
19762
      } /* End loop over rows of content from SELFTEST */
19763
      sqlite3_finalize(pStmt);
19764
    } /* End loop over k */
19765
    freeText(&str);
19766
    utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
19767
  }else
19768
 
19769
  if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
19770
    if( nArg<2 || nArg>3 ){
19771
      raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
19772
      rc = 1;
19773
    }
19774
    if( nArg>=2 ){
19775
      sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
19776
                       "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
19777
    }
19778
    if( nArg>=3 ){
19779
      sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
19780
                       "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
19781
    }
19782
  }else
19783
 
19784
  if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
19785
    const char *zLike = 0;   /* Which table to checksum. 0 means everything */
19786
    int i;                   /* Loop counter */
19787
    int bSchema = 0;         /* Also hash the schema */
19788
    int bSeparate = 0;       /* Hash each table separately */
19789
    int iSize = 224;         /* Hash algorithm to use */
19790
    int bDebug = 0;          /* Only show the query that would have run */
19791
    sqlite3_stmt *pStmt;     /* For querying tables names */
19792
    char *zSql;              /* SQL to be run */
19793
    char *zSep;              /* Separator */
19794
    ShellText sSql;          /* Complete SQL for the query to run the hash */
19795
    ShellText sQuery;        /* Set of queries used to read all content */
19796
    open_db(p, 0);
19797
    for(i=1; i
19798
      const char *z = azArg[i];
19799
      if( z[0]=='-' ){
19800
        z++;
19801
        if( z[0]=='-' ) z++;
19802
        if( strcmp(z,"schema")==0 ){
19803
          bSchema = 1;
19804
        }else
19805
        if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
19806
         || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
19807
        ){
19808
          iSize = atoi(&z[5]);
19809
        }else
19810
        if( strcmp(z,"debug")==0 ){
19811
          bDebug = 1;
19812
        }else
19813
        {
19814
          utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
19815
                      azArg[i], azArg[0]);
19816
          showHelp(p->out, azArg[0]);
19817
          rc = 1;
19818
          goto meta_command_exit;
19819
        }
19820
      }else if( zLike ){
19821
        raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
19822
        rc = 1;
19823
        goto meta_command_exit;
19824
      }else{
19825
        zLike = z;
19826
        bSeparate = 1;
19827
        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
19828
      }
19829
    }
19830
    if( bSchema ){
19831
      zSql = "SELECT lower(name) FROM sqlite_schema"
19832
             " WHERE type='table' AND coalesce(rootpage,0)>1"
19833
             " UNION ALL SELECT 'sqlite_schema'"
19834
             " ORDER BY 1 collate nocase";
19835
    }else{
19836
      zSql = "SELECT lower(name) FROM sqlite_schema"
19837
             " WHERE type='table' AND coalesce(rootpage,0)>1"
19838
             " AND name NOT LIKE 'sqlite_%'"
19839
             " ORDER BY 1 collate nocase";
19840
    }
19841
    sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19842
    initText(&sQuery);
19843
    initText(&sSql);
19844
    appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
19845
    zSep = "VALUES(";
19846
    while( SQLITE_ROW==sqlite3_step(pStmt) ){
19847
      const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
19848
      if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
19849
      if( strncmp(zTab, "sqlite_",7)!=0 ){
19850
        appendText(&sQuery,"SELECT * FROM ", 0);
19851
        appendText(&sQuery,zTab,'"');
19852
        appendText(&sQuery," NOT INDEXED;", 0);
19853
      }else if( strcmp(zTab, "sqlite_schema")==0 ){
19854
        appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
19855
                           " ORDER BY name;", 0);
19856
      }else if( strcmp(zTab, "sqlite_sequence")==0 ){
19857
        appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
19858
                           " ORDER BY name;", 0);
19859
      }else if( strcmp(zTab, "sqlite_stat1")==0 ){
19860
        appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
19861
                           " ORDER BY tbl,idx;", 0);
19862
      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
19863
        appendText(&sQuery, "SELECT * FROM ", 0);
19864
        appendText(&sQuery, zTab, 0);
19865
        appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
19866
      }
19867
      appendText(&sSql, zSep, 0);
19868
      appendText(&sSql, sQuery.z, '\'');
19869
      sQuery.n = 0;
19870
      appendText(&sSql, ",", 0);
19871
      appendText(&sSql, zTab, '\'');
19872
      zSep = "),(";
19873
    }
19874
    sqlite3_finalize(pStmt);
19875
    if( bSeparate ){
19876
      zSql = sqlite3_mprintf(
19877
          "%s))"
19878
          " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
19879
          "   FROM [sha3sum$query]",
19880
          sSql.z, iSize);
19881
    }else{
19882
      zSql = sqlite3_mprintf(
19883
          "%s))"
19884
          " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
19885
          "   FROM [sha3sum$query]",
19886
          sSql.z, iSize);
19887
    }
19888
    freeText(&sQuery);
19889
    freeText(&sSql);
19890
    if( bDebug ){
19891
      utf8_printf(p->out, "%s\n", zSql);
19892
    }else{
19893
      shell_exec(p, zSql, 0);
19894
    }
19895
    sqlite3_free(zSql);
19896
  }else
19897
 
19898
#ifndef SQLITE_NOHAVE_SYSTEM
19899
  if( c=='s'
19900
   && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
19901
  ){
19902
    char *zCmd;
19903
    int i, x;
19904
    if( nArg<2 ){
19905
      raw_printf(stderr, "Usage: .system COMMAND\n");
19906
      rc = 1;
19907
      goto meta_command_exit;
19908
    }
19909
    zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
19910
    for(i=2; i
19911
      zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
19912
                             zCmd, azArg[i]);
19913
    }
19914
    x = system(zCmd);
19915
    sqlite3_free(zCmd);
19916
    if( x ) raw_printf(stderr, "System command returns %d\n", x);
19917
  }else
19918
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
19919
 
19920
  if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
19921
    static const char *azBool[] = { "off", "on", "trigger", "full"};
19922
    const char *zOut;
19923
    int i;
19924
    if( nArg!=1 ){
19925
      raw_printf(stderr, "Usage: .show\n");
19926
      rc = 1;
19927
      goto meta_command_exit;
19928
    }
19929
    utf8_printf(p->out, "%12.12s: %s\n","echo",
19930
                                  azBool[ShellHasFlag(p, SHFLG_Echo)]);
19931
    utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
19932
    utf8_printf(p->out, "%12.12s: %s\n","explain",
19933
         p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
19934
    utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
19935
    utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
19936
    utf8_printf(p->out, "%12.12s: ", "nullvalue");
19937
      output_c_string(p->out, p->nullValue);
19938
      raw_printf(p->out, "\n");
19939
    utf8_printf(p->out,"%12.12s: %s\n","output",
19940
            strlen30(p->outfile) ? p->outfile : "stdout");
19941
    utf8_printf(p->out,"%12.12s: ", "colseparator");
19942
      output_c_string(p->out, p->colSeparator);
19943
      raw_printf(p->out, "\n");
19944
    utf8_printf(p->out,"%12.12s: ", "rowseparator");
19945
      output_c_string(p->out, p->rowSeparator);
19946
      raw_printf(p->out, "\n");
19947
    switch( p->statsOn ){
19948
      case 0:  zOut = "off";     break;
19949
      default: zOut = "on";      break;
19950
      case 2:  zOut = "stmt";    break;
19951
      case 3:  zOut = "vmstep";  break;
19952
    }
19953
    utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
19954
    utf8_printf(p->out, "%12.12s: ", "width");
19955
    for (i=0;inWidth;i++) {
19956
      raw_printf(p->out, "%d ", p->colWidth[i]);
19957
    }
19958
    raw_printf(p->out, "\n");
19959
    utf8_printf(p->out, "%12.12s: %s\n", "filename",
19960
                p->zDbFilename ? p->zDbFilename : "");
19961
  }else
19962
 
19963
  if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
19964
    if( nArg==2 ){
19965
      if( strcmp(azArg[1],"stmt")==0 ){
19966
        p->statsOn = 2;
19967
      }else if( strcmp(azArg[1],"vmstep")==0 ){
19968
        p->statsOn = 3;
19969
      }else{
19970
        p->statsOn = (u8)booleanValue(azArg[1]);
19971
      }
19972
    }else if( nArg==1 ){
19973
      display_stats(p->db, p, 0);
19974
    }else{
19975
      raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
19976
      rc = 1;
19977
    }
19978
  }else
19979
 
19980
  if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
19981
   || (c=='i' && (strncmp(azArg[0], "indices", n)==0
19982
                 || strncmp(azArg[0], "indexes", n)==0) )
19983
  ){
19984
    sqlite3_stmt *pStmt;
19985
    char **azResult;
19986
    int nRow, nAlloc;
19987
    int ii;
19988
    ShellText s;
19989
    initText(&s);
19990
    open_db(p, 0);
19991
    rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
19992
    if( rc ){
19993
      sqlite3_finalize(pStmt);
19994
      return shellDatabaseError(p->db);
19995
    }
19996
 
19997
    if( nArg>2 && c=='i' ){
19998
      /* It is an historical accident that the .indexes command shows an error
19999
      ** when called with the wrong number of arguments whereas the .tables
20000
      ** command does not. */
20001
      raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
20002
      rc = 1;
20003
      sqlite3_finalize(pStmt);
20004
      goto meta_command_exit;
20005
    }
20006
    for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
20007
      const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
20008
      if( zDbName==0 ) continue;
20009
      if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
20010
      if( sqlite3_stricmp(zDbName, "main")==0 ){
20011
        appendText(&s, "SELECT name FROM ", 0);
20012
      }else{
20013
        appendText(&s, "SELECT ", 0);
20014
        appendText(&s, zDbName, '\'');
20015
        appendText(&s, "||'.'||name FROM ", 0);
20016
      }
20017
      appendText(&s, zDbName, '"');
20018
      appendText(&s, ".sqlite_schema ", 0);
20019
      if( c=='t' ){
20020
        appendText(&s," WHERE type IN ('table','view')"
20021
                      "   AND name NOT LIKE 'sqlite_%'"
20022
                      "   AND name LIKE ?1", 0);
20023
      }else{
20024
        appendText(&s," WHERE type='index'"
20025
                      "   AND tbl_name LIKE ?1", 0);
20026
      }
20027
    }
20028
    rc = sqlite3_finalize(pStmt);
20029
    appendText(&s, " ORDER BY 1", 0);
20030
    rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
20031
    freeText(&s);
20032
    if( rc ) return shellDatabaseError(p->db);
20033
 
20034
    /* Run the SQL statement prepared by the above block. Store the results
20035
    ** as an array of nul-terminated strings in azResult[].  */
20036
    nRow = nAlloc = 0;
20037
    azResult = 0;
20038
    if( nArg>1 ){
20039
      sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
20040
    }else{
20041
      sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
20042
    }
20043
    while( sqlite3_step(pStmt)==SQLITE_ROW ){
20044
      if( nRow>=nAlloc ){
20045
        char **azNew;
20046
        int n2 = nAlloc*2 + 10;
20047
        azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
20048
        if( azNew==0 ) shell_out_of_memory();
20049
        nAlloc = n2;
20050
        azResult = azNew;
20051
      }
20052
      azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
20053
      if( 0==azResult[nRow] ) shell_out_of_memory();
20054
      nRow++;
20055
    }
20056
    if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
20057
      rc = shellDatabaseError(p->db);
20058
    }
20059
 
20060
    /* Pretty-print the contents of array azResult[] to the output */
20061
    if( rc==0 && nRow>0 ){
20062
      int len, maxlen = 0;
20063
      int i, j;
20064
      int nPrintCol, nPrintRow;
20065
      for(i=0; i
20066
        len = strlen30(azResult[i]);
20067
        if( len>maxlen ) maxlen = len;
20068
      }
20069
      nPrintCol = 80/(maxlen+2);
20070
      if( nPrintCol<1 ) nPrintCol = 1;
20071
      nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
20072
      for(i=0; i
20073
        for(j=i; j
20074
          char *zSp = j
20075
          utf8_printf(p->out, "%s%-*s", zSp, maxlen,
20076
                      azResult[j] ? azResult[j]:"");
20077
        }
20078
        raw_printf(p->out, "\n");
20079
      }
20080
    }
20081
 
20082
    for(ii=0; ii
20083
    sqlite3_free(azResult);
20084
  }else
20085
 
20086
  /* Begin redirecting output to the file "testcase-out.txt" */
20087
  if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
20088
    output_reset(p);
20089
    p->out = output_file_open("testcase-out.txt", 0);
20090
    if( p->out==0 ){
20091
      raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
20092
    }
20093
    if( nArg>=2 ){
20094
      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
20095
    }else{
20096
      sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
20097
    }
20098
  }else
20099
 
20100
#ifndef SQLITE_UNTESTABLE
20101
  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
20102
    static const struct {
20103
       const char *zCtrlName;   /* Name of a test-control option */
20104
       int ctrlCode;            /* Integer code for that option */
20105
       const char *zUsage;      /* Usage notes */
20106
    } aCtrl[] = {
20107
      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
20108
      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
20109
    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
20110
    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
20111
      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
20112
      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
20113
    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
20114
      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
20115
      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
20116
      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
20117
      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
20118
      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
20119
#ifdef YYCOVERAGE
20120
      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
20121
#endif
20122
      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
20123
      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
20124
      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
20125
      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
20126
      { "seek_count",         SQLITE_TESTCTRL_SEEK_COUNT,    ""               },
20127
    };
20128
    int testctrl = -1;
20129
    int iCtrl = -1;
20130
    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
20131
    int isOk = 0;
20132
    int i, n2;
20133
    const char *zCmd = 0;
20134
 
20135
    open_db(p, 0);
20136
    zCmd = nArg>=2 ? azArg[1] : "help";
20137
 
20138
    /* The argument can optionally begin with "-" or "--" */
20139
    if( zCmd[0]=='-' && zCmd[1] ){
20140
      zCmd++;
20141
      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
20142
    }
20143
 
20144
    /* --help lists all test-controls */
20145
    if( strcmp(zCmd,"help")==0 ){
20146
      utf8_printf(p->out, "Available test-controls:\n");
20147
      for(i=0; i
20148
        utf8_printf(p->out, "  .testctrl %s %s\n",
20149
                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
20150
      }
20151
      rc = 1;
20152
      goto meta_command_exit;
20153
    }
20154
 
20155
    /* convert testctrl text option to value. allow any unique prefix
20156
    ** of the option name, or a numerical value. */
20157
    n2 = strlen30(zCmd);
20158
    for(i=0; i
20159
      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
20160
        if( testctrl<0 ){
20161
          testctrl = aCtrl[i].ctrlCode;
20162
          iCtrl = i;
20163
        }else{
20164
          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
20165
                              "Use \".testctrl --help\" for help\n", zCmd);
20166
          rc = 1;
20167
          goto meta_command_exit;
20168
        }
20169
      }
20170
    }
20171
    if( testctrl<0 ){
20172
      utf8_printf(stderr,"Error: unknown test-control: %s\n"
20173
                         "Use \".testctrl --help\" for help\n", zCmd);
20174
    }else{
20175
      switch(testctrl){
20176
 
20177
        /* sqlite3_test_control(int, db, int) */
20178
        case SQLITE_TESTCTRL_OPTIMIZATIONS:
20179
          if( nArg==3 ){
20180
            unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
20181
            rc2 = sqlite3_test_control(testctrl, p->db, opt);
20182
            isOk = 3;
20183
          }
20184
          break;
20185
 
20186
        /* sqlite3_test_control(int) */
20187
        case SQLITE_TESTCTRL_PRNG_SAVE:
20188
        case SQLITE_TESTCTRL_PRNG_RESTORE:
20189
        case SQLITE_TESTCTRL_BYTEORDER:
20190
          if( nArg==2 ){
20191
            rc2 = sqlite3_test_control(testctrl);
20192
            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
20193
          }
20194
          break;
20195
 
20196
        /* sqlite3_test_control(int, uint) */
20197
        case SQLITE_TESTCTRL_PENDING_BYTE:
20198
          if( nArg==3 ){
20199
            unsigned int opt = (unsigned int)integerValue(azArg[2]);
20200
            rc2 = sqlite3_test_control(testctrl, opt);
20201
            isOk = 3;
20202
          }
20203
          break;
20204
 
20205
        /* sqlite3_test_control(int, int, sqlite3*) */
20206
        case SQLITE_TESTCTRL_PRNG_SEED:
20207
          if( nArg==3 || nArg==4 ){
20208
            int ii = (int)integerValue(azArg[2]);
20209
            sqlite3 *db;
20210
            if( ii==0 && strcmp(azArg[2],"random")==0 ){
20211
              sqlite3_randomness(sizeof(ii),&ii);
20212
              printf("-- random seed: %d\n", ii);
20213
            }
20214
            if( nArg==3 ){
20215
              db = 0;
20216
            }else{
20217
              db = p->db;
20218
              /* Make sure the schema has been loaded */
20219
              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
20220
            }
20221
            rc2 = sqlite3_test_control(testctrl, ii, db);
20222
            isOk = 3;
20223
          }
20224
          break;
20225
 
20226
        /* sqlite3_test_control(int, int) */
20227
        case SQLITE_TESTCTRL_ASSERT:
20228
        case SQLITE_TESTCTRL_ALWAYS:
20229
          if( nArg==3 ){
20230
            int opt = booleanValue(azArg[2]);
20231
            rc2 = sqlite3_test_control(testctrl, opt);
20232
            isOk = 1;
20233
          }
20234
          break;
20235
 
20236
        /* sqlite3_test_control(int, int) */
20237
        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
20238
        case SQLITE_TESTCTRL_NEVER_CORRUPT:
20239
          if( nArg==3 ){
20240
            int opt = booleanValue(azArg[2]);
20241
            rc2 = sqlite3_test_control(testctrl, opt);
20242
            isOk = 3;
20243
          }
20244
          break;
20245
 
20246
        /* sqlite3_test_control(sqlite3*) */
20247
        case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
20248
          rc2 = sqlite3_test_control(testctrl, p->db);
20249
          isOk = 3;
20250
          break;
20251
 
20252
        case SQLITE_TESTCTRL_IMPOSTER:
20253
          if( nArg==5 ){
20254
            rc2 = sqlite3_test_control(testctrl, p->db,
20255
                          azArg[2],
20256
                          integerValue(azArg[3]),
20257
                          integerValue(azArg[4]));
20258
            isOk = 3;
20259
          }
20260
          break;
20261
 
20262
        case SQLITE_TESTCTRL_SEEK_COUNT: {
20263
          u64 x = 0;
20264
          rc2 = sqlite3_test_control(testctrl, p->db, &x);
20265
          utf8_printf(p->out, "%llu\n", x);
20266
          isOk = 3;
20267
          break;
20268
        }
20269
 
20270
#ifdef YYCOVERAGE
20271
        case SQLITE_TESTCTRL_PARSER_COVERAGE:
20272
          if( nArg==2 ){
20273
            sqlite3_test_control(testctrl, p->out);
20274
            isOk = 3;
20275
          }
20276
#endif
20277
      }
20278
    }
20279
    if( isOk==0 && iCtrl>=0 ){
20280
      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
20281
      rc = 1;
20282
    }else if( isOk==1 ){
20283
      raw_printf(p->out, "%d\n", rc2);
20284
    }else if( isOk==2 ){
20285
      raw_printf(p->out, "0x%08x\n", rc2);
20286
    }
20287
  }else
20288
#endif /* !defined(SQLITE_UNTESTABLE) */
20289
 
20290
  if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
20291
    open_db(p, 0);
20292
    sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
20293
  }else
20294
 
20295
  if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
20296
    if( nArg==2 ){
20297
      enableTimer = booleanValue(azArg[1]);
20298
      if( enableTimer && !HAS_TIMER ){
20299
        raw_printf(stderr, "Error: timer not available on this system.\n");
20300
        enableTimer = 0;
20301
      }
20302
    }else{
20303
      raw_printf(stderr, "Usage: .timer on|off\n");
20304
      rc = 1;
20305
    }
20306
  }else
20307
 
20308
#ifndef SQLITE_OMIT_TRACE
20309
  if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
20310
    int mType = 0;
20311
    int jj;
20312
    open_db(p, 0);
20313
    for(jj=1; jj
20314
      const char *z = azArg[jj];
20315
      if( z[0]=='-' ){
20316
        if( optionMatch(z, "expanded") ){
20317
          p->eTraceType = SHELL_TRACE_EXPANDED;
20318
        }
20319
#ifdef SQLITE_ENABLE_NORMALIZE
20320
        else if( optionMatch(z, "normalized") ){
20321
          p->eTraceType = SHELL_TRACE_NORMALIZED;
20322
        }
20323
#endif
20324
        else if( optionMatch(z, "plain") ){
20325
          p->eTraceType = SHELL_TRACE_PLAIN;
20326
        }
20327
        else if( optionMatch(z, "profile") ){
20328
          mType |= SQLITE_TRACE_PROFILE;
20329
        }
20330
        else if( optionMatch(z, "row") ){
20331
          mType |= SQLITE_TRACE_ROW;
20332
        }
20333
        else if( optionMatch(z, "stmt") ){
20334
          mType |= SQLITE_TRACE_STMT;
20335
        }
20336
        else if( optionMatch(z, "close") ){
20337
          mType |= SQLITE_TRACE_CLOSE;
20338
        }
20339
        else {
20340
          raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
20341
          rc = 1;
20342
          goto meta_command_exit;
20343
        }
20344
      }else{
20345
        output_file_close(p->traceOut);
20346
        p->traceOut = output_file_open(azArg[1], 0);
20347
      }
20348
    }
20349
    if( p->traceOut==0 ){
20350
      sqlite3_trace_v2(p->db, 0, 0, 0);
20351
    }else{
20352
      if( mType==0 ) mType = SQLITE_TRACE_STMT;
20353
      sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
20354
    }
20355
  }else
20356
#endif /* !defined(SQLITE_OMIT_TRACE) */
20357
 
20358
#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
20359
  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
20360
    int ii;
20361
    int lenOpt;
20362
    char *zOpt;
20363
    if( nArg<2 ){
20364
      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
20365
      rc = 1;
20366
      goto meta_command_exit;
20367
    }
20368
    open_db(p, 0);
20369
    zOpt = azArg[1];
20370
    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
20371
    lenOpt = (int)strlen(zOpt);
20372
    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
20373
      assert( azArg[nArg]==0 );
20374
      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
20375
    }else{
20376
      for(ii=1; ii
20377
        sqlite3_create_module(p->db, azArg[ii], 0, 0);
20378
      }
20379
    }
20380
  }else
20381
#endif
20382
 
20383
#if SQLITE_USER_AUTHENTICATION
20384
  if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
20385
    if( nArg<2 ){
20386
      raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
20387
      rc = 1;
20388
      goto meta_command_exit;
20389
    }
20390
    open_db(p, 0);
20391
    if( strcmp(azArg[1],"login")==0 ){
20392
      if( nArg!=4 ){
20393
        raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
20394
        rc = 1;
20395
        goto meta_command_exit;
20396
      }
20397
      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
20398
                                     strlen30(azArg[3]));
20399
      if( rc ){
20400
        utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
20401
        rc = 1;
20402
      }
20403
    }else if( strcmp(azArg[1],"add")==0 ){
20404
      if( nArg!=5 ){
20405
        raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
20406
        rc = 1;
20407
        goto meta_command_exit;
20408
      }
20409
      rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
20410
                            booleanValue(azArg[4]));
20411
      if( rc ){
20412
        raw_printf(stderr, "User-Add failed: %d\n", rc);
20413
        rc = 1;
20414
      }
20415
    }else if( strcmp(azArg[1],"edit")==0 ){
20416
      if( nArg!=5 ){
20417
        raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
20418
        rc = 1;
20419
        goto meta_command_exit;
20420
      }
20421
      rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
20422
                              booleanValue(azArg[4]));
20423
      if( rc ){
20424
        raw_printf(stderr, "User-Edit failed: %d\n", rc);
20425
        rc = 1;
20426
      }
20427
    }else if( strcmp(azArg[1],"delete")==0 ){
20428
      if( nArg!=3 ){
20429
        raw_printf(stderr, "Usage: .user delete USER\n");
20430
        rc = 1;
20431
        goto meta_command_exit;
20432
      }
20433
      rc = sqlite3_user_delete(p->db, azArg[2]);
20434
      if( rc ){
20435
        raw_printf(stderr, "User-Delete failed: %d\n", rc);
20436
        rc = 1;
20437
      }
20438
    }else{
20439
      raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
20440
      rc = 1;
20441
      goto meta_command_exit;
20442
    }
20443
  }else
20444
#endif /* SQLITE_USER_AUTHENTICATION */
20445
 
20446
  if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
20447
    utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
20448
        sqlite3_libversion(), sqlite3_sourceid());
20449
#if SQLITE_HAVE_ZLIB
20450
    utf8_printf(p->out, "zlib version %s\n", zlibVersion());
20451
#endif
20452
#define CTIMEOPT_VAL_(opt) #opt
20453
#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
20454
#if defined(__clang__) && defined(__clang_major__)
20455
    utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
20456
                    CTIMEOPT_VAL(__clang_minor__) "."
20457
                    CTIMEOPT_VAL(__clang_patchlevel__) "\n");
20458
#elif defined(_MSC_VER)
20459
    utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
20460
#elif defined(__GNUC__) && defined(__VERSION__)
20461
    utf8_printf(p->out, "gcc-" __VERSION__ "\n");
20462
#endif
20463
  }else
20464
 
20465
  if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
20466
    const char *zDbName = nArg==2 ? azArg[1] : "main";
20467
    sqlite3_vfs *pVfs = 0;
20468
    if( p->db ){
20469
      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
20470
      if( pVfs ){
20471
        utf8_printf(p->out, "vfs.zName      = \"%s\"\n", pVfs->zName);
20472
        raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
20473
        raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
20474
        raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
20475
      }
20476
    }
20477
  }else
20478
 
20479
  if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
20480
    sqlite3_vfs *pVfs;
20481
    sqlite3_vfs *pCurrent = 0;
20482
    if( p->db ){
20483
      sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
20484
    }
20485
    for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
20486
      utf8_printf(p->out, "vfs.zName      = \"%s\"%s\n", pVfs->zName,
20487
           pVfs==pCurrent ? "  <--- CURRENT" : "");
20488
      raw_printf(p->out, "vfs.iVersion   = %d\n", pVfs->iVersion);
20489
      raw_printf(p->out, "vfs.szOsFile   = %d\n", pVfs->szOsFile);
20490
      raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
20491
      if( pVfs->pNext ){
20492
        raw_printf(p->out, "-----------------------------------\n");
20493
      }
20494
    }
20495
  }else
20496
 
20497
  if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
20498
    const char *zDbName = nArg==2 ? azArg[1] : "main";
20499
    char *zVfsName = 0;
20500
    if( p->db ){
20501
      sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
20502
      if( zVfsName ){
20503
        utf8_printf(p->out, "%s\n", zVfsName);
20504
        sqlite3_free(zVfsName);
20505
      }
20506
    }
20507
  }else
20508
 
20509
  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
20510
    unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
20511
    sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
20512
  }else
20513
 
20514
  if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
20515
    int j;
20516
    assert( nArg<=ArraySize(azArg) );
20517
    p->nWidth = nArg-1;
20518
    p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
20519
    if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
20520
    if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
20521
    for(j=1; j
20522
      p->colWidth[j-1] = (int)integerValue(azArg[j]);
20523
    }
20524
  }else
20525
 
20526
  {
20527
    utf8_printf(stderr, "Error: unknown command or invalid arguments: "
20528
      " \"%s\". Enter \".help\" for help\n", azArg[0]);
20529
    rc = 1;
20530
  }
20531
 
20532
meta_command_exit:
20533
  if( p->outCount ){
20534
    p->outCount--;
20535
    if( p->outCount==0 ) output_reset(p);
20536
  }
20537
  return rc;
20538
}
20539
 
20540
/*
20541
** Return TRUE if a semicolon occurs anywhere in the first N characters
20542
** of string z[].
20543
*/
20544
static int line_contains_semicolon(const char *z, int N){
20545
  int i;
20546
  for(i=0; i
20547
  return 0;
20548
}
20549
 
20550
/*
20551
** Test to see if a line consists entirely of whitespace.
20552
*/
20553
static int _all_whitespace(const char *z){
20554
  for(; *z; z++){
20555
    if( IsSpace(z[0]) ) continue;
20556
    if( *z=='/' && z[1]=='*' ){
20557
      z += 2;
20558
      while( *z && (*z!='*' || z[1]!='/') ){ z++; }
20559
      if( *z==0 ) return 0;
20560
      z++;
20561
      continue;
20562
    }
20563
    if( *z=='-' && z[1]=='-' ){
20564
      z += 2;
20565
      while( *z && *z!='\n' ){ z++; }
20566
      if( *z==0 ) return 1;
20567
      continue;
20568
    }
20569
    return 0;
20570
  }
20571
  return 1;
20572
}
20573
 
20574
/*
20575
** Return TRUE if the line typed in is an SQL command terminator other
20576
** than a semi-colon.  The SQL Server style "go" command is understood
20577
** as is the Oracle "/".
20578
*/
20579
static int line_is_command_terminator(const char *zLine){
20580
  while( IsSpace(zLine[0]) ){ zLine++; };
20581
  if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
20582
    return 1;  /* Oracle */
20583
  }
20584
  if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
20585
         && _all_whitespace(&zLine[2]) ){
20586
    return 1;  /* SQL Server */
20587
  }
20588
  return 0;
20589
}
20590
 
20591
/*
20592
** We need a default sqlite3_complete() implementation to use in case
20593
** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
20594
** any arbitrary text is a complete SQL statement.  This is not very
20595
** user-friendly, but it does seem to work.
20596
*/
20597
#ifdef SQLITE_OMIT_COMPLETE
20598
#define sqlite3_complete(x) 1
20599
#endif
20600
 
20601
/*
20602
** Return true if zSql is a complete SQL statement.  Return false if it
20603
** ends in the middle of a string literal or C-style comment.
20604
*/
20605
static int line_is_complete(char *zSql, int nSql){
20606
  int rc;
20607
  if( zSql==0 ) return 1;
20608
  zSql[nSql] = ';';
20609
  zSql[nSql+1] = 0;
20610
  rc = sqlite3_complete(zSql);
20611
  zSql[nSql] = 0;
20612
  return rc;
20613
}
20614
 
20615
/*
20616
** Run a single line of SQL.  Return the number of errors.
20617
*/
20618
static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
20619
  int rc;
20620
  char *zErrMsg = 0;
20621
 
20622
  open_db(p, 0);
20623
  if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
20624
  if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
20625
  BEGIN_TIMER;
20626
  rc = shell_exec(p, zSql, &zErrMsg);
20627
  END_TIMER;
20628
  if( rc || zErrMsg ){
20629
    char zPrefix[100];
20630
    if( in!=0 || !stdin_is_interactive ){
20631
      sqlite3_snprintf(sizeof(zPrefix), zPrefix,
20632
                       "Error: near line %d:", startline);
20633
    }else{
20634
      sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
20635
    }
20636
    if( zErrMsg!=0 ){
20637
      utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
20638
      sqlite3_free(zErrMsg);
20639
      zErrMsg = 0;
20640
    }else{
20641
      utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
20642
    }
20643
    return 1;
20644
  }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
20645
    raw_printf(p->out, "changes: %3d   total_changes: %d\n",
20646
            sqlite3_changes(p->db), sqlite3_total_changes(p->db));
20647
  }
20648
  return 0;
20649
}
20650
 
20651
 
20652
/*
20653
** Read input from *in and process it.  If *in==0 then input
20654
** is interactive - the user is typing it it.  Otherwise, input
20655
** is coming from a file or device.  A prompt is issued and history
20656
** is saved only if input is interactive.  An interrupt signal will
20657
** cause this routine to exit immediately, unless input is interactive.
20658
**
20659
** Return the number of errors.
20660
*/
20661
static int process_input(ShellState *p){
20662
  char *zLine = 0;          /* A single input line */
20663
  char *zSql = 0;           /* Accumulated SQL text */
20664
  int nLine;                /* Length of current line */
20665
  int nSql = 0;             /* Bytes of zSql[] used */
20666
  int nAlloc = 0;           /* Allocated zSql[] space */
20667
  int nSqlPrior = 0;        /* Bytes of zSql[] used by prior line */
20668
  int rc;                   /* Error code */
20669
  int errCnt = 0;           /* Number of errors seen */
20670
  int startline = 0;        /* Line number for start of current input */
20671
 
20672
  p->lineno = 0;
20673
  while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
20674
    fflush(p->out);
20675
    zLine = one_input_line(p->in, zLine, nSql>0);
20676
    if( zLine==0 ){
20677
      /* End of input */
20678
      if( p->in==0 && stdin_is_interactive ) printf("\n");
20679
      break;
20680
    }
20681
    if( seenInterrupt ){
20682
      if( p->in!=0 ) break;
20683
      seenInterrupt = 0;
20684
    }
20685
    p->lineno++;
20686
    if( nSql==0 && _all_whitespace(zLine) ){
20687
      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
20688
      continue;
20689
    }
20690
    if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
20691
      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
20692
      if( zLine[0]=='.' ){
20693
        rc = do_meta_command(zLine, p);
20694
        if( rc==2 ){ /* exit requested */
20695
          break;
20696
        }else if( rc ){
20697
          errCnt++;
20698
        }
20699
      }
20700
      continue;
20701
    }
20702
    if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
20703
      memcpy(zLine,";",2);
20704
    }
20705
    nLine = strlen30(zLine);
20706
    if( nSql+nLine+2>=nAlloc ){
20707
      nAlloc = nSql+nLine+100;
20708
      zSql = realloc(zSql, nAlloc);
20709
      if( zSql==0 ) shell_out_of_memory();
20710
    }
20711
    nSqlPrior = nSql;
20712
    if( nSql==0 ){
20713
      int i;
20714
      for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
20715
      assert( nAlloc>0 && zSql!=0 );
20716
      memcpy(zSql, zLine+i, nLine+1-i);
20717
      startline = p->lineno;
20718
      nSql = nLine-i;
20719
    }else{
20720
      zSql[nSql++] = '\n';
20721
      memcpy(zSql+nSql, zLine, nLine+1);
20722
      nSql += nLine;
20723
    }
20724
    if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
20725
                && sqlite3_complete(zSql) ){
20726
      errCnt += runOneSqlLine(p, zSql, p->in, startline);
20727
      nSql = 0;
20728
      if( p->outCount ){
20729
        output_reset(p);
20730
        p->outCount = 0;
20731
      }else{
20732
        clearTempFile(p);
20733
      }
20734
    }else if( nSql && _all_whitespace(zSql) ){
20735
      if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
20736
      nSql = 0;
20737
    }
20738
  }
20739
  if( nSql && !_all_whitespace(zSql) ){
20740
    errCnt += runOneSqlLine(p, zSql, p->in, startline);
20741
  }
20742
  free(zSql);
20743
  free(zLine);
20744
  return errCnt>0;
20745
}
20746
 
20747
/*
20748
** Return a pathname which is the user's home directory.  A
20749
** 0 return indicates an error of some kind.
20750
*/
20751
static char *find_home_dir(int clearFlag){
20752
  static char *home_dir = NULL;
20753
  if( clearFlag ){
20754
    free(home_dir);
20755
    home_dir = 0;
20756
    return 0;
20757
  }
20758
  if( home_dir ) return home_dir;
20759
 
20760
#ifdef _KOLIBRI
20761
  home_dir = "/sys";
20762
 
20763
#elif !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
20764
     && !defined(__RTP__) && !defined(_WRS_KERNEL)
20765
  {
20766
    struct passwd *pwent;
20767
    uid_t uid = getuid();
20768
    if( (pwent=getpwuid(uid)) != NULL) {
20769
      home_dir = pwent->pw_dir;
20770
    }
20771
  }
20772
#endif
20773
 
20774
#if defined(_WIN32_WCE)
20775
  /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
20776
   */
20777
  home_dir = "/";
20778
#else
20779
 
20780
#if defined(_WIN32) || defined(WIN32)
20781
  if (!home_dir) {
20782
    home_dir = getenv("USERPROFILE");
20783
  }
20784
#endif
20785
 
20786
  if (!home_dir) {
20787
    home_dir = getenv("HOME");
20788
  }
20789
 
20790
#if defined(_WIN32) || defined(WIN32)
20791
  if (!home_dir) {
20792
    char *zDrive, *zPath;
20793
    int n;
20794
    zDrive = getenv("HOMEDRIVE");
20795
    zPath = getenv("HOMEPATH");
20796
    if( zDrive && zPath ){
20797
      n = strlen30(zDrive) + strlen30(zPath) + 1;
20798
      home_dir = malloc( n );
20799
      if( home_dir==0 ) return 0;
20800
      sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
20801
      return home_dir;
20802
    }
20803
    home_dir = "c:\\";
20804
  }
20805
#endif
20806
 
20807
#endif /* !_WIN32_WCE */
20808
 
20809
  if( home_dir ){
20810
    int n = strlen30(home_dir) + 1;
20811
    char *z = malloc( n );
20812
    if( z ) memcpy(z, home_dir, n);
20813
    home_dir = z;
20814
  }
20815
 
20816
  return home_dir;
20817
}
20818
 
20819
/*
20820
** Read input from the file given by sqliterc_override.  Or if that
20821
** parameter is NULL, take input from ~/.sqliterc
20822
**
20823
** Returns the number of errors.
20824
*/
20825
static void process_sqliterc(
20826
  ShellState *p,                  /* Configuration data */
20827
  const char *sqliterc_override   /* Name of config file. NULL to use default */
20828
){
20829
  char *home_dir = NULL;
20830
  const char *sqliterc = sqliterc_override;
20831
  char *zBuf = 0;
20832
  FILE *inSaved = p->in;
20833
  int savedLineno = p->lineno;
20834
 
20835
  if (sqliterc == NULL) {
20836
    home_dir = find_home_dir(0);
20837
    if( home_dir==0 ){
20838
      raw_printf(stderr, "-- warning: cannot find home directory;"
20839
                      " cannot read ~/.sqliterc\n");
20840
      return;
20841
    }
20842
    zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
20843
    sqliterc = zBuf;
20844
  }
20845
  p->in = fopen(sqliterc,"rb");
20846
  if( p->in ){
20847
    if( stdin_is_interactive ){
20848
      utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
20849
    }
20850
    if( process_input(p) && bail_on_error ) exit(1);
20851
    fclose(p->in);
20852
  }else if( sqliterc_override!=0 ){
20853
    utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
20854
    if( bail_on_error ) exit(1);
20855
  }
20856
  p->in = inSaved;
20857
  p->lineno = savedLineno;
20858
  sqlite3_free(zBuf);
20859
}
20860
 
20861
/*
20862
** Show available command line options
20863
*/
20864
static const char zOptions[] =
20865
#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
20866
  "   -A ARGS...           run \".archive ARGS\" and exit\n"
20867
#endif
20868
  "   -append              append the database to the end of the file\n"
20869
  "   -ascii               set output mode to 'ascii'\n"
20870
  "   -bail                stop after hitting an error\n"
20871
  "   -batch               force batch I/O\n"
20872
  "   -box                 set output mode to 'box'\n"
20873
  "   -column              set output mode to 'column'\n"
20874
  "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
20875
  "   -csv                 set output mode to 'csv'\n"
20876
#if defined(SQLITE_ENABLE_DESERIALIZE)
20877
  "   -deserialize         open the database using sqlite3_deserialize()\n"
20878
#endif
20879
  "   -echo                print commands before execution\n"
20880
  "   -init FILENAME       read/process named file\n"
20881
  "   -[no]header          turn headers on or off\n"
20882
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
20883
  "   -heap SIZE           Size of heap for memsys3 or memsys5\n"
20884
#endif
20885
  "   -help                show this message\n"
20886
  "   -html                set output mode to HTML\n"
20887
  "   -interactive         force interactive I/O\n"
20888
  "   -json                set output mode to 'json'\n"
20889
  "   -line                set output mode to 'line'\n"
20890
  "   -list                set output mode to 'list'\n"
20891
  "   -lookaside SIZE N    use N entries of SZ bytes for lookaside memory\n"
20892
  "   -markdown            set output mode to 'markdown'\n"
20893
#if defined(SQLITE_ENABLE_DESERIALIZE)
20894
  "   -maxsize N           maximum size for a --deserialize database\n"
20895
#endif
20896
  "   -memtrace            trace all memory allocations and deallocations\n"
20897
  "   -mmap N              default mmap size set to N\n"
20898
#ifdef SQLITE_ENABLE_MULTIPLEX
20899
  "   -multiplex           enable the multiplexor VFS\n"
20900
#endif
20901
  "   -newline SEP         set output row separator. Default: '\\n'\n"
20902
  "   -nofollow            refuse to open symbolic links to database files\n"
20903
  "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
20904
  "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
20905
  "   -quote               set output mode to 'quote'\n"
20906
  "   -readonly            open the database read-only\n"
20907
  "   -separator SEP       set output column separator. Default: '|'\n"
20908
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
20909
  "   -sorterref SIZE      sorter references threshold size\n"
20910
#endif
20911
  "   -stats               print memory stats before each finalize\n"
20912
  "   -table               set output mode to 'table'\n"
20913
  "   -tabs                set output mode to 'tabs'\n"
20914
  "   -version             show SQLite version\n"
20915
  "   -vfs NAME            use NAME as the default VFS\n"
20916
#ifdef SQLITE_ENABLE_VFSTRACE
20917
  "   -vfstrace            enable tracing of all VFS calls\n"
20918
#endif
20919
#ifdef SQLITE_HAVE_ZLIB
20920
  "   -zip                 open the file as a ZIP Archive\n"
20921
#endif
20922
;
20923
static void usage(int showDetail){
20924
  utf8_printf(stderr,
20925
      "Usage: %s [OPTIONS] FILENAME [SQL]\n"
20926
      "FILENAME is the name of an SQLite database. A new database is created\n"
20927
      "if the file does not previously exist.\n", Argv0);
20928
  if( showDetail ){
20929
    utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
20930
  }else{
20931
    raw_printf(stderr, "Use the -help option for additional information\n");
20932
  }
20933
  exit(1);
20934
}
20935
 
20936
/*
20937
** Internal check:  Verify that the SQLite is uninitialized.  Print a
20938
** error message if it is initialized.
20939
*/
20940
static void verify_uninitialized(void){
20941
  if( sqlite3_config(-1)==SQLITE_MISUSE ){
20942
    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
20943
                        " initialization.\n");
20944
  }
20945
}
20946
 
20947
/*
20948
** Initialize the state information in data
20949
*/
20950
static void main_init(ShellState *data) {
20951
  memset(data, 0, sizeof(*data));
20952
  data->normalMode = data->cMode = data->mode = MODE_List;
20953
  data->autoExplain = 1;
20954
  memcpy(data->colSeparator,SEP_Column, 2);
20955
  memcpy(data->rowSeparator,SEP_Row, 2);
20956
  data->showHeader = 0;
20957
  data->shellFlgs = SHFLG_Lookaside;
20958
  verify_uninitialized();
20959
  sqlite3_config(SQLITE_CONFIG_URI, 1);
20960
  sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
20961
  sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
20962
  sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
20963
  sqlite3_snprintf(sizeof(continuePrompt), continuePrompt,"   ...> ");
20964
}
20965
 
20966
/*
20967
** Output text to the console in a font that attracts extra attention.
20968
*/
20969
#ifdef _WIN32
20970
static void printBold(const char *zText){
20971
#if !SQLITE_OS_WINRT
20972
  HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
20973
  CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
20974
  GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
20975
  SetConsoleTextAttribute(out,
20976
         FOREGROUND_RED|FOREGROUND_INTENSITY
20977
  );
20978
#endif
20979
  printf("%s", zText);
20980
#if !SQLITE_OS_WINRT
20981
  SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
20982
#endif
20983
}
20984
#else
20985
static void printBold(const char *zText){
20986
  printf("\033[1m%s\033[0m", zText);
20987
}
20988
#endif
20989
 
20990
/*
20991
** Get the argument to an --option.  Throw an error and die if no argument
20992
** is available.
20993
*/
20994
static char *cmdline_option_value(int argc, char **argv, int i){
20995
  if( i==argc ){
20996
    utf8_printf(stderr, "%s: Error: missing argument to %s\n",
20997
            argv[0], argv[argc-1]);
20998
    exit(1);
20999
  }
21000
  return argv[i];
21001
}
21002
 
21003
#ifndef SQLITE_SHELL_IS_UTF8
21004
#  if (defined(_WIN32) || defined(WIN32)) \
21005
   && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
21006
#    define SQLITE_SHELL_IS_UTF8          (0)
21007
#  else
21008
#    define SQLITE_SHELL_IS_UTF8          (1)
21009
#  endif
21010
#endif
21011
 
8931 turbocat 21012
 
8760 turbocat 21013
#if SQLITE_SHELL_IS_UTF8
21014
int SQLITE_CDECL main(int argc, char **argv){
21015
#else
21016
int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
21017
  char **argv;
21018
#endif
8941 turbocat 21019
//  con_init_opt(-1,-1,-1,-1, "SQLite3");
8760 turbocat 21020
  char *zErrMsg = 0;
21021
  ShellState data;
21022
  const char *zInitFile = 0;
21023
  int i;
21024
  int rc = 0;
21025
  int warnInmemoryDb = 0;
21026
  int readStdin = 1;
21027
  int nCmd = 0;
21028
  char **azCmd = 0;
21029
  const char *zVfs = 0;           /* Value of -vfs command-line option */
21030
#if !SQLITE_SHELL_IS_UTF8
21031
  char **argvToFree = 0;
21032
  int argcToFree = 0;
21033
#endif
21034
 
21035
  setBinaryMode(stdin, 0);
21036
  setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
21037
  stdin_is_interactive = isatty(0);
21038
  stdout_is_console = isatty(1);
21039
 
21040
#ifdef SQLITE_DEBUG
21041
  registerOomSimulator();
21042
#endif
21043
 
21044
#if !defined(_WIN32_WCE) && ! defined(_KOLIBRI)
21045
  if( getenv("SQLITE_DEBUG_BREAK") ){
21046
    if( isatty(0) && isatty(2) ){
21047
      fprintf(stderr,
21048
          "attach debugger to process %d and press any key to continue.\n",
21049
          GETPID());
21050
      fgetc(stdin);
21051
    }else{
21052
#if defined(_WIN32) || defined(WIN32)
21053
#if SQLITE_OS_WINRT
21054
      __debugbreak();
21055
#else
21056
      DebugBreak();
21057
#endif
21058
#elif defined(SIGTRAP)
21059
      raise(SIGTRAP);
21060
#endif
21061
    }
21062
  }
21063
#endif
21064
 
21065
#if USE_SYSTEM_SQLITE+0!=1
21066
  if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
21067
    utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
21068
            sqlite3_sourceid(), SQLITE_SOURCE_ID);
21069
    exit(1);
21070
  }
21071
#endif
21072
  main_init(&data);
21073
 
21074
  /* On Windows, we must translate command-line arguments into UTF-8.
21075
  ** The SQLite memory allocator subsystem has to be enabled in order to
21076
  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
21077
  ** subsequent sqlite3_config() calls will work.  So copy all results into
21078
  ** memory that does not come from the SQLite memory allocator.
21079
  */
21080
#if !SQLITE_SHELL_IS_UTF8
21081
  sqlite3_initialize();
21082
  argvToFree = malloc(sizeof(argv[0])*argc*2);
21083
  argcToFree = argc;
21084
  argv = argvToFree + argc;
21085
  if( argv==0 ) shell_out_of_memory();
21086
  for(i=0; i
21087
    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
21088
    int n;
21089
    if( z==0 ) shell_out_of_memory();
21090
    n = (int)strlen(z);
21091
    argv[i] = malloc( n+1 );
21092
    if( argv[i]==0 ) shell_out_of_memory();
21093
    memcpy(argv[i], z, n+1);
21094
    argvToFree[i] = argv[i];
21095
    sqlite3_free(z);
21096
  }
21097
  sqlite3_shutdown();
21098
#endif
21099
 
21100
  assert( argc>=1 && argv && argv[0] );
21101
  Argv0 = argv[0];
21102
 
21103
  /* Make sure we have a valid signal handler early, before anything
21104
  ** else is done.
21105
  */
21106
#ifdef SIGINT
21107
  signal(SIGINT, interrupt_handler);
21108
#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
21109
  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
21110
#endif
21111
 
21112
#ifdef SQLITE_SHELL_DBNAME_PROC
21113
  {
21114
    /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
21115
    ** of a C-function that will provide the name of the database file.  Use
21116
    ** this compile-time option to embed this shell program in larger
21117
    ** applications. */
21118
    extern void SQLITE_SHELL_DBNAME_PROC(const char**);
21119
    SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
21120
    warnInmemoryDb = 0;
21121
  }
21122
#endif
21123
 
21124
  /* Do an initial pass through the command-line argument to locate
21125
  ** the name of the database file, the name of the initialization file,
21126
  ** the size of the alternative malloc heap,
21127
  ** and the first command to execute.
21128
  */
21129
  verify_uninitialized();
21130
  for(i=1; i
21131
    char *z;
21132
    z = argv[i];
21133
    if( z[0]!='-' ){
21134
      if( data.zDbFilename==0 ){
21135
        data.zDbFilename = z;
21136
      }else{
21137
        /* Excesss arguments are interpreted as SQL (or dot-commands) and
21138
        ** mean that nothing is read from stdin */
21139
        readStdin = 0;
21140
        nCmd++;
21141
        azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
21142
        if( azCmd==0 ) shell_out_of_memory();
21143
        azCmd[nCmd-1] = z;
21144
      }
21145
    }
21146
    if( z[1]=='-' ) z++;
21147
    if( strcmp(z,"-separator")==0
21148
     || strcmp(z,"-nullvalue")==0
21149
     || strcmp(z,"-newline")==0
21150
     || strcmp(z,"-cmd")==0
21151
    ){
21152
      (void)cmdline_option_value(argc, argv, ++i);
21153
    }else if( strcmp(z,"-init")==0 ){
21154
      zInitFile = cmdline_option_value(argc, argv, ++i);
21155
    }else if( strcmp(z,"-batch")==0 ){
21156
      /* Need to check for batch mode here to so we can avoid printing
21157
      ** informational messages (like from process_sqliterc) before
21158
      ** we do the actual processing of arguments later in a second pass.
21159
      */
21160
      stdin_is_interactive = 0;
21161
    }else if( strcmp(z,"-heap")==0 ){
21162
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
21163
      const char *zSize;
21164
      sqlite3_int64 szHeap;
21165
 
21166
      zSize = cmdline_option_value(argc, argv, ++i);
21167
      szHeap = integerValue(zSize);
21168
      if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
21169
      sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
21170
#else
21171
      (void)cmdline_option_value(argc, argv, ++i);
21172
#endif
21173
    }else if( strcmp(z,"-pagecache")==0 ){
21174
      sqlite3_int64 n, sz;
21175
      sz = integerValue(cmdline_option_value(argc,argv,++i));
21176
      if( sz>70000 ) sz = 70000;
21177
      if( sz<0 ) sz = 0;
21178
      n = integerValue(cmdline_option_value(argc,argv,++i));
21179
      if( sz>0 && n>0 && 0xffffffffffffLL/sz
21180
        n = 0xffffffffffffLL/sz;
21181
      }
21182
      sqlite3_config(SQLITE_CONFIG_PAGECACHE,
21183
                    (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
21184
      data.shellFlgs |= SHFLG_Pagecache;
21185
    }else if( strcmp(z,"-lookaside")==0 ){
21186
      int n, sz;
21187
      sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
21188
      if( sz<0 ) sz = 0;
21189
      n = (int)integerValue(cmdline_option_value(argc,argv,++i));
21190
      if( n<0 ) n = 0;
21191
      sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
21192
      if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
21193
#ifdef SQLITE_ENABLE_VFSTRACE
21194
    }else if( strcmp(z,"-vfstrace")==0 ){
21195
      extern int vfstrace_register(
21196
         const char *zTraceName,
21197
         const char *zOldVfsName,
21198
         int (*xOut)(const char*,void*),
21199
         void *pOutArg,
21200
         int makeDefault
21201
      );
21202
      vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
21203
#endif
21204
#ifdef SQLITE_ENABLE_MULTIPLEX
21205
    }else if( strcmp(z,"-multiplex")==0 ){
21206
      extern int sqlite3_multiple_initialize(const char*,int);
21207
      sqlite3_multiplex_initialize(0, 1);
21208
#endif
21209
    }else if( strcmp(z,"-mmap")==0 ){
21210
      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
21211
      sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
21212
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
21213
    }else if( strcmp(z,"-sorterref")==0 ){
21214
      sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
21215
      sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
21216
#endif
21217
    }else if( strcmp(z,"-vfs")==0 ){
21218
      zVfs = cmdline_option_value(argc, argv, ++i);
21219
#ifdef SQLITE_HAVE_ZLIB
21220
    }else if( strcmp(z,"-zip")==0 ){
21221
      data.openMode = SHELL_OPEN_ZIPFILE;
21222
#endif
21223
    }else if( strcmp(z,"-append")==0 ){
21224
      data.openMode = SHELL_OPEN_APPENDVFS;
21225
#ifdef SQLITE_ENABLE_DESERIALIZE
21226
    }else if( strcmp(z,"-deserialize")==0 ){
21227
      data.openMode = SHELL_OPEN_DESERIALIZE;
21228
    }else if( strcmp(z,"-maxsize")==0 && i+1
21229
      data.szMax = integerValue(argv[++i]);
21230
#endif
21231
    }else if( strcmp(z,"-readonly")==0 ){
21232
      data.openMode = SHELL_OPEN_READONLY;
21233
    }else if( strcmp(z,"-nofollow")==0 ){
21234
      data.openFlags = SQLITE_OPEN_NOFOLLOW;
21235
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
21236
    }else if( strncmp(z, "-A",2)==0 ){
21237
      /* All remaining command-line arguments are passed to the ".archive"
21238
      ** command, so ignore them */
21239
      break;
21240
#endif
21241
    }else if( strcmp(z, "-memtrace")==0 ){
21242
      sqlite3MemTraceActivate(stderr);
21243
    }else if( strcmp(z,"-bail")==0 ){
21244
      bail_on_error = 1;
21245
    }
21246
  }
21247
  verify_uninitialized();
21248
 
21249
 
21250
#ifdef SQLITE_SHELL_INIT_PROC
21251
  {
21252
    /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
21253
    ** of a C-function that will perform initialization actions on SQLite that
21254
    ** occur just before or after sqlite3_initialize(). Use this compile-time
21255
    ** option to embed this shell program in larger applications. */
21256
    extern void SQLITE_SHELL_INIT_PROC(void);
21257
    SQLITE_SHELL_INIT_PROC();
21258
  }
21259
#else
21260
  /* All the sqlite3_config() calls have now been made. So it is safe
21261
  ** to call sqlite3_initialize() and process any command line -vfs option. */
21262
  sqlite3_initialize();
21263
#endif
21264
 
21265
  if( zVfs ){
21266
    sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
21267
    if( pVfs ){
21268
      sqlite3_vfs_register(pVfs, 1);
21269
    }else{
21270
      utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
21271
      exit(1);
21272
    }
21273
  }
21274
 
21275
  if( data.zDbFilename==0 ){
21276
#ifndef SQLITE_OMIT_MEMORYDB
21277
    data.zDbFilename = ":memory:";
21278
    warnInmemoryDb = argc==1;
21279
#else
21280
    utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
21281
    return 1;
21282
#endif
21283
  }
21284
  data.out = stdout;
21285
  sqlite3_appendvfs_init(0,0,0);
21286
 
21287
  /* Go ahead and open the database file if it already exists.  If the
21288
  ** file does not exist, delay opening it.  This prevents empty database
21289
  ** files from being created if a user mistypes the database name argument
21290
  ** to the sqlite command-line tool.
21291
  */
21292
  if( access(data.zDbFilename, 0)==0 ){
21293
    open_db(&data, 0);
21294
  }
21295
 
21296
  /* Process the initialization file if there is one.  If no -init option
21297
  ** is given on the command line, look for a file named ~/.sqliterc and
21298
  ** try to process it.
21299
  */
21300
  process_sqliterc(&data,zInitFile);
21301
 
21302
  /* Make a second pass through the command-line argument and set
21303
  ** options.  This second pass is delayed until after the initialization
21304
  ** file is processed so that the command-line arguments will override
21305
  ** settings in the initialization file.
21306
  */
21307
  for(i=1; i
21308
    char *z = argv[i];
21309
    if( z[0]!='-' ) continue;
21310
    if( z[1]=='-' ){ z++; }
21311
    if( strcmp(z,"-init")==0 ){
21312
      i++;
21313
    }else if( strcmp(z,"-html")==0 ){
21314
      data.mode = MODE_Html;
21315
    }else if( strcmp(z,"-list")==0 ){
21316
      data.mode = MODE_List;
21317
    }else if( strcmp(z,"-quote")==0 ){
21318
      data.mode = MODE_Quote;
21319
      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
21320
      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
21321
    }else if( strcmp(z,"-line")==0 ){
21322
      data.mode = MODE_Line;
21323
    }else if( strcmp(z,"-column")==0 ){
21324
      data.mode = MODE_Column;
21325
    }else if( strcmp(z,"-json")==0 ){
21326
      data.mode = MODE_Json;
21327
    }else if( strcmp(z,"-markdown")==0 ){
21328
      data.mode = MODE_Markdown;
21329
    }else if( strcmp(z,"-table")==0 ){
21330
      data.mode = MODE_Table;
21331
    }else if( strcmp(z,"-box")==0 ){
21332
      data.mode = MODE_Box;
21333
    }else if( strcmp(z,"-csv")==0 ){
21334
      data.mode = MODE_Csv;
21335
      memcpy(data.colSeparator,",",2);
21336
#ifdef SQLITE_HAVE_ZLIB
21337
    }else if( strcmp(z,"-zip")==0 ){
21338
      data.openMode = SHELL_OPEN_ZIPFILE;
21339
#endif
21340
    }else if( strcmp(z,"-append")==0 ){
21341
      data.openMode = SHELL_OPEN_APPENDVFS;
21342
#ifdef SQLITE_ENABLE_DESERIALIZE
21343
    }else if( strcmp(z,"-deserialize")==0 ){
21344
      data.openMode = SHELL_OPEN_DESERIALIZE;
21345
    }else if( strcmp(z,"-maxsize")==0 && i+1
21346
      data.szMax = integerValue(argv[++i]);
21347
#endif
21348
    }else if( strcmp(z,"-readonly")==0 ){
21349
      data.openMode = SHELL_OPEN_READONLY;
21350
    }else if( strcmp(z,"-nofollow")==0 ){
21351
      data.openFlags |= SQLITE_OPEN_NOFOLLOW;
21352
    }else if( strcmp(z,"-ascii")==0 ){
21353
      data.mode = MODE_Ascii;
21354
      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
21355
      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
21356
    }else if( strcmp(z,"-tabs")==0 ){
21357
      data.mode = MODE_List;
21358
      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
21359
      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
21360
    }else if( strcmp(z,"-separator")==0 ){
21361
      sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
21362
                       "%s",cmdline_option_value(argc,argv,++i));
21363
    }else if( strcmp(z,"-newline")==0 ){
21364
      sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
21365
                       "%s",cmdline_option_value(argc,argv,++i));
21366
    }else if( strcmp(z,"-nullvalue")==0 ){
21367
      sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
21368
                       "%s",cmdline_option_value(argc,argv,++i));
21369
    }else if( strcmp(z,"-header")==0 ){
21370
      data.showHeader = 1;
21371
    }else if( strcmp(z,"-noheader")==0 ){
21372
      data.showHeader = 0;
21373
    }else if( strcmp(z,"-echo")==0 ){
21374
      ShellSetFlag(&data, SHFLG_Echo);
21375
    }else if( strcmp(z,"-eqp")==0 ){
21376
      data.autoEQP = AUTOEQP_on;
21377
    }else if( strcmp(z,"-eqpfull")==0 ){
21378
      data.autoEQP = AUTOEQP_full;
21379
    }else if( strcmp(z,"-stats")==0 ){
21380
      data.statsOn = 1;
21381
    }else if( strcmp(z,"-scanstats")==0 ){
21382
      data.scanstatsOn = 1;
21383
    }else if( strcmp(z,"-backslash")==0 ){
21384
      /* Undocumented command-line option: -backslash
21385
      ** Causes C-style backslash escapes to be evaluated in SQL statements
21386
      ** prior to sending the SQL into SQLite.  Useful for injecting
21387
      ** crazy bytes in the middle of SQL statements for testing and debugging.
21388
      */
21389
      ShellSetFlag(&data, SHFLG_Backslash);
21390
    }else if( strcmp(z,"-bail")==0 ){
21391
      /* No-op.  The bail_on_error flag should already be set. */
21392
    }else if( strcmp(z,"-version")==0 ){
21393
      printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
21394
      return 0;
21395
    }else if( strcmp(z,"-interactive")==0 ){
21396
      stdin_is_interactive = 1;
21397
    }else if( strcmp(z,"-batch")==0 ){
21398
      stdin_is_interactive = 0;
21399
    }else if( strcmp(z,"-heap")==0 ){
21400
      i++;
21401
    }else if( strcmp(z,"-pagecache")==0 ){
21402
      i+=2;
21403
    }else if( strcmp(z,"-lookaside")==0 ){
21404
      i+=2;
21405
    }else if( strcmp(z,"-mmap")==0 ){
21406
      i++;
21407
    }else if( strcmp(z,"-memtrace")==0 ){
21408
      i++;
21409
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
21410
    }else if( strcmp(z,"-sorterref")==0 ){
21411
      i++;
21412
#endif
21413
    }else if( strcmp(z,"-vfs")==0 ){
21414
      i++;
21415
#ifdef SQLITE_ENABLE_VFSTRACE
21416
    }else if( strcmp(z,"-vfstrace")==0 ){
21417
      i++;
21418
#endif
21419
#ifdef SQLITE_ENABLE_MULTIPLEX
21420
    }else if( strcmp(z,"-multiplex")==0 ){
21421
      i++;
21422
#endif
21423
    }else if( strcmp(z,"-help")==0 ){
21424
      usage(1);
21425
    }else if( strcmp(z,"-cmd")==0 ){
21426
      /* Run commands that follow -cmd first and separately from commands
21427
      ** that simply appear on the command-line.  This seems goofy.  It would
21428
      ** be better if all commands ran in the order that they appear.  But
21429
      ** we retain the goofy behavior for historical compatibility. */
21430
      if( i==argc-1 ) break;
21431
      z = cmdline_option_value(argc,argv,++i);
21432
      if( z[0]=='.' ){
21433
        rc = do_meta_command(z, &data);
21434
        if( rc && bail_on_error ) return rc==2 ? 0 : rc;
21435
      }else{
21436
        open_db(&data, 0);
21437
        rc = shell_exec(&data, z, &zErrMsg);
21438
        if( zErrMsg!=0 ){
21439
          utf8_printf(stderr,"Error: %s\n", zErrMsg);
21440
          if( bail_on_error ) return rc!=0 ? rc : 1;
21441
        }else if( rc!=0 ){
21442
          utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
21443
          if( bail_on_error ) return rc;
21444
        }
21445
      }
21446
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
21447
    }else if( strncmp(z, "-A", 2)==0 ){
21448
      if( nCmd>0 ){
21449
        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
21450
                            " with \"%s\"\n", z);
21451
        return 1;
21452
      }
21453
      open_db(&data, OPEN_DB_ZIPFILE);
21454
      if( z[2] ){
21455
        argv[i] = &z[2];
21456
        arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
21457
      }else{
21458
        arDotCommand(&data, 1, argv+i, argc-i);
21459
      }
21460
      readStdin = 0;
21461
      break;
21462
#endif
21463
    }else{
21464
      utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
21465
      raw_printf(stderr,"Use -help for a list of options.\n");
21466
      return 1;
21467
    }
21468
    data.cMode = data.mode;
21469
  }
21470
 
21471
  if( !readStdin ){
21472
    /* Run all arguments that do not begin with '-' as if they were separate
21473
    ** command-line inputs, except for the argToSkip argument which contains
21474
    ** the database filename.
21475
    */
21476
    for(i=0; i
21477
      if( azCmd[i][0]=='.' ){
21478
        rc = do_meta_command(azCmd[i], &data);
21479
        if( rc ){
21480
          free(azCmd);
21481
          return rc==2 ? 0 : rc;
21482
        }
21483
      }else{
21484
        open_db(&data, 0);
21485
        rc = shell_exec(&data, azCmd[i], &zErrMsg);
21486
        if( zErrMsg || rc ){
21487
          if( zErrMsg!=0 ){
21488
            utf8_printf(stderr,"Error: %s\n", zErrMsg);
21489
          }else{
21490
            utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
21491
          }
21492
          sqlite3_free(zErrMsg);
21493
          free(azCmd);
21494
          return rc!=0 ? rc : 1;
21495
        }
21496
      }
21497
    }
21498
  }else{
21499
    /* Run commands received from standard input
21500
    */
21501
    if( stdin_is_interactive ){
21502
      char *zHome;
21503
      char *zHistory;
21504
      int nHistory;
21505
      printf(
21506
        "SQLite version %s %.19s\n" /*extra-version-info*/
21507
        "Enter \".help\" for usage hints.\n",
21508
        sqlite3_libversion(), sqlite3_sourceid()
21509
      );
21510
      if( warnInmemoryDb ){
21511
        printf("Connected to a ");
21512
        printBold("transient in-memory database");
21513
        printf(".\nUse \".open FILENAME\" to reopen on a "
21514
               "persistent database.\n");
21515
      }
21516
      zHistory = getenv("SQLITE_HISTORY");
21517
      if( zHistory ){
21518
        zHistory = strdup(zHistory);
21519
      }else if( (zHome = find_home_dir(0))!=0 ){
21520
        nHistory = strlen30(zHome) + 20;
21521
        if( (zHistory = malloc(nHistory))!=0 ){
21522
          sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
21523
        }
21524
      }
21525
      if( zHistory ){ shell_read_history(zHistory); }
21526
#if HAVE_READLINE || HAVE_EDITLINE
21527
      rl_attempted_completion_function = readline_completion;
21528
#elif HAVE_LINENOISE
21529
      linenoiseSetCompletionCallback(linenoise_completion);
21530
#endif
21531
      data.in = 0;
21532
      rc = process_input(&data);
21533
      if( zHistory ){
21534
        shell_stifle_history(2000);
21535
        shell_write_history(zHistory);
21536
        free(zHistory);
21537
      }
21538
    }else{
21539
      data.in = stdin;
21540
      rc = process_input(&data);
21541
    }
21542
  }
21543
  free(azCmd);
21544
  set_table_name(&data, 0);
21545
  if( data.db ){
21546
    session_close_all(&data);
21547
    close_db(data.db);
21548
  }
21549
  sqlite3_free(data.zFreeOnClose);
21550
  find_home_dir(1);
21551
  output_reset(&data);
21552
  data.doXdgOpen = 0;
21553
  clearTempFile(&data);
21554
#if !SQLITE_SHELL_IS_UTF8
21555
  for(i=0; i
21556
  free(argvToFree);
21557
#endif
21558
  free(data.colWidth);
21559
  /* Clear the global data structure so that valgrind will detect memory
21560
  ** leaks */
21561
  memset(&data, 0, sizeof(data));
21562
  return rc;
21563
}