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 |
|
|
105 |
#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__) |
|
|
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 |
|
|
2212 |
# include |
|
|
2213 |
# include |
|
|
2214 |
# include |
|
|
2215 |
#else |
|
|
2216 |
# include "windows.h" |
|
|
2217 |
# include |
|
|
2218 |
# include |
|
|
2219 |
/* # include "test_windirent.h" */ |
|
|
2220 |
# define dirent DIRENT |
|
|
2221 |
# ifndef chmod |
|
|
2222 |
# define chmod _chmod |
|
|
2223 |
# endif |
|
|
2224 |
# ifndef stat |
|
|
2225 |
# define stat _stat |
|
|
2226 |
# endif |
|
|
2227 |
# define mkdir(path,mode) _mkdir(path) |
|
|
2228 |
# define lstat(path,buf) stat(path,buf) |
|
|
2229 |
#endif |
|
|
2230 |
#include |
|
|
2231 |
#include |
|
|
2232 |
|
|
|
2233 |
|
|
|
2234 |
/* |
|
|
2235 |
** Structure of the fsdir() table-valued function |
|
|
2236 |
*/ |
|
|
2237 |
/* 0 1 2 3 4 5 */ |
|
|
2238 |
#define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)" |
|
|
2239 |
#define FSDIR_COLUMN_NAME 0 /* Name of the file */ |
|
|
2240 |
#define FSDIR_COLUMN_MODE 1 /* Access mode */ |
|
|
2241 |
#define FSDIR_COLUMN_MTIME 2 /* Last modification time */ |
|
|
2242 |
#define FSDIR_COLUMN_DATA 3 /* File content */ |
|
|
2243 |
#define FSDIR_COLUMN_PATH 4 /* Path to top of search */ |
|
|
2244 |
#define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */ |
|
|
2245 |
|
|
|
2246 |
|
|
|
2247 |
/* |
|
|
2248 |
** Set the result stored by context ctx to a blob containing the |
|
|
2249 |
** contents of file zName. Or, leave the result unchanged (NULL) |
|
|
2250 |
** if the file does not exist or is unreadable. |
|
|
2251 |
** |
|
|
2252 |
** If the file exceeds the SQLite blob size limit, through an |
|
|
2253 |
** SQLITE_TOOBIG error. |
|
|
2254 |
** |
|
|
2255 |
** Throw an SQLITE_IOERR if there are difficulties pulling the file |
|
|
2256 |
** off of disk. |
|
|
2257 |
*/ |
|
|
2258 |
static void readFileContents(sqlite3_context *ctx, const char *zName){ |
|
|
2259 |
FILE *in; |
|
|
2260 |
sqlite3_int64 nIn; |
|
|
2261 |
void *pBuf; |
|
|
2262 |
sqlite3 *db; |
|
|
2263 |
int mxBlob; |
|
|
2264 |
|
|
|
2265 |
in = fopen(zName, "rb"); |
|
|
2266 |
if( in==0 ){ |
|
|
2267 |
/* File does not exist or is unreadable. Leave the result set to NULL. */ |
|
|
2268 |
return; |
|
|
2269 |
} |
|
|
2270 |
fseek(in, 0, SEEK_END); |
|
|
2271 |
nIn = ftell(in); |
|
|
2272 |
rewind(in); |
|
|
2273 |
db = sqlite3_context_db_handle(ctx); |
|
|
2274 |
mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1); |
|
|
2275 |
if( nIn>mxBlob ){ |
|
|
2276 |
sqlite3_result_error_code(ctx, SQLITE_TOOBIG); |
|
|
2277 |
fclose(in); |
|
|
2278 |
return; |
|
|
2279 |
} |
|
|
2280 |
pBuf = sqlite3_malloc64( nIn ? nIn : 1 ); |
|
|
2281 |
if( pBuf==0 ){ |
|
|
2282 |
sqlite3_result_error_nomem(ctx); |
|
|
2283 |
fclose(in); |
|
|
2284 |
return; |
|
|
2285 |
} |
|
|
2286 |
if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){ |
|
|
2287 |
sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free); |
|
|
2288 |
}else{ |
|
|
2289 |
sqlite3_result_error_code(ctx, SQLITE_IOERR); |
|
|
2290 |
sqlite3_free(pBuf); |
|
|
2291 |
} |
|
|
2292 |
fclose(in); |
|
|
2293 |
} |
|
|
2294 |
|
|
|
2295 |
/* |
|
|
2296 |
** Implementation of the "readfile(X)" SQL function. The entire content |
|
|
2297 |
** of the file named X is read and returned as a BLOB. NULL is returned |
|
|
2298 |
** if the file does not exist or is unreadable. |
|
|
2299 |
*/ |
|
|
2300 |
static void readfileFunc( |
|
|
2301 |
sqlite3_context *context, |
|
|
2302 |
int argc, |
|
|
2303 |
sqlite3_value **argv |
|
|
2304 |
){ |
|
|
2305 |
const char *zName; |
|
|
2306 |
(void)(argc); /* Unused parameter */ |
|
|
2307 |
zName = (const char*)sqlite3_value_text(argv[0]); |
|
|
2308 |
if( zName==0 ) return; |
|
|
2309 |
readFileContents(context, zName); |
|
|
2310 |
} |
|
|
2311 |
|
|
|
2312 |
/* |
|
|
2313 |
** Set the error message contained in context ctx to the results of |
|
|
2314 |
** vprintf(zFmt, ...). |
|
|
2315 |
*/ |
|
|
2316 |
static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){ |
|
|
2317 |
char *zMsg = 0; |
|
|
2318 |
va_list ap; |
|
|
2319 |
va_start(ap, zFmt); |
|
|
2320 |
zMsg = sqlite3_vmprintf(zFmt, ap); |
|
|
2321 |
sqlite3_result_error(ctx, zMsg, -1); |
|
|
2322 |
sqlite3_free(zMsg); |
|
|
2323 |
va_end(ap); |
|
|
2324 |
} |
|
|
2325 |
|
|
|
2326 |
#if defined(_WIN32) |
|
|
2327 |
/* |
|
|
2328 |
** This function is designed to convert a Win32 FILETIME structure into the |
|
|
2329 |
** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC). |
|
|
2330 |
*/ |
|
|
2331 |
static sqlite3_uint64 fileTimeToUnixTime( |
|
|
2332 |
LPFILETIME pFileTime |
|
|
2333 |
){ |
|
|
2334 |
SYSTEMTIME epochSystemTime; |
|
|
2335 |
ULARGE_INTEGER epochIntervals; |
|
|
2336 |
FILETIME epochFileTime; |
|
|
2337 |
ULARGE_INTEGER fileIntervals; |
|
|
2338 |
|
|
|
2339 |
memset(&epochSystemTime, 0, sizeof(SYSTEMTIME)); |
|
|
2340 |
epochSystemTime.wYear = 1970; |
|
|
2341 |
epochSystemTime.wMonth = 1; |
|
|
2342 |
epochSystemTime.wDay = 1; |
|
|
2343 |
SystemTimeToFileTime(&epochSystemTime, &epochFileTime); |
|
|
2344 |
epochIntervals.LowPart = epochFileTime.dwLowDateTime; |
|
|
2345 |
epochIntervals.HighPart = epochFileTime.dwHighDateTime; |
|
|
2346 |
|
|
|
2347 |
fileIntervals.LowPart = pFileTime->dwLowDateTime; |
|
|
2348 |
fileIntervals.HighPart = pFileTime->dwHighDateTime; |
|
|
2349 |
|
|
|
2350 |
return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000; |
|
|
2351 |
} |
|
|
2352 |
|
|
|
2353 |
/* |
|
|
2354 |
** This function attempts to normalize the time values found in the stat() |
|
|
2355 |
** buffer to UTC. This is necessary on Win32, where the runtime library |
|
|
2356 |
** appears to return these values as local times. |
|
|
2357 |
*/ |
|
|
2358 |
static void statTimesToUtc( |
|
|
2359 |
const char *zPath, |
|
|
2360 |
struct stat *pStatBuf |
|
|
2361 |
){ |
|
|
2362 |
HANDLE hFindFile; |
|
|
2363 |
WIN32_FIND_DATAW fd; |
|
|
2364 |
LPWSTR zUnicodeName; |
|
|
2365 |
extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*); |
|
|
2366 |
zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath); |
|
|
2367 |
if( zUnicodeName ){ |
|
|
2368 |
memset(&fd, 0, sizeof(WIN32_FIND_DATAW)); |
|
|
2369 |
hFindFile = FindFirstFileW(zUnicodeName, &fd); |
|
|
2370 |
if( hFindFile!=NULL ){ |
|
|
2371 |
pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime); |
|
|
2372 |
pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime); |
|
|
2373 |
pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime); |
|
|
2374 |
FindClose(hFindFile); |
|
|
2375 |
} |
|
|
2376 |
sqlite3_free(zUnicodeName); |
|
|
2377 |
} |
|
|
2378 |
} |
|
|
2379 |
#endif |
|
|
2380 |
|
|
|
2381 |
/* |
|
|
2382 |
** This function is used in place of stat(). On Windows, special handling |
|
|
2383 |
** is required in order for the included time to be returned as UTC. On all |
|
|
2384 |
** other systems, this function simply calls stat(). |
|
|
2385 |
*/ |
|
|
2386 |
static int fileStat( |
|
|
2387 |
const char *zPath, |
|
|
2388 |
struct stat *pStatBuf |
|
|
2389 |
){ |
|
|
2390 |
#if defined(_WIN32) |
|
|
2391 |
int rc = stat(zPath, pStatBuf); |
|
|
2392 |
if( rc==0 ) statTimesToUtc(zPath, pStatBuf); |
|
|
2393 |
return rc; |
|
|
2394 |
#else |
|
|
2395 |
return stat(zPath, pStatBuf); |
|
|
2396 |
#endif |
|
|
2397 |
} |
|
|
2398 |
|
|
|
2399 |
/* |
|
|
2400 |
** This function is used in place of lstat(). On Windows, special handling |
|
|
2401 |
** is required in order for the included time to be returned as UTC. On all |
|
|
2402 |
** other systems, this function simply calls lstat(). |
|
|
2403 |
*/ |
|
|
2404 |
static int fileLinkStat( |
|
|
2405 |
const char *zPath, |
|
|
2406 |
struct stat *pStatBuf |
|
|
2407 |
){ |
|
|
2408 |
#if defined(_WIN32) |
|
|
2409 |
int rc = lstat(zPath, pStatBuf); |
|
|
2410 |
if( rc==0 ) statTimesToUtc(zPath, pStatBuf); |
|
|
2411 |
return rc; |
|
|
2412 |
#else |
|
|
2413 |
return lstat(zPath, pStatBuf); |
|
|
2414 |
#endif |
|
|
2415 |
} |
|
|
2416 |
|
|
|
2417 |
/* |
|
|
2418 |
** Argument zFile is the name of a file that will be created and/or written |
|
|
2419 |
** by SQL function writefile(). This function ensures that the directory |
|
|
2420 |
** zFile will be written to exists, creating it if required. The permissions |
|
|
2421 |
** for any path components created by this function are set in accordance |
|
|
2422 |
** with the current umask. |
|
|
2423 |
** |
|
|
2424 |
** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise, |
|
|
2425 |
** SQLITE_OK is returned if the directory is successfully created, or |
|
|
2426 |
** SQLITE_ERROR otherwise. |
|
|
2427 |
*/ |
|
|
2428 |
static int makeDirectory( |
|
|
2429 |
const char *zFile |
|
|
2430 |
){ |
|
|
2431 |
char *zCopy = sqlite3_mprintf("%s", zFile); |
|
|
2432 |
int rc = SQLITE_OK; |
|
|
2433 |
|
|
|
2434 |
if( zCopy==0 ){ |
|
|
2435 |
rc = SQLITE_NOMEM; |
|
|
2436 |
}else{ |
|
|
2437 |
int nCopy = (int)strlen(zCopy); |
|
|
2438 |
int i = 1; |
|
|
2439 |
|
|
|
2440 |
while( rc==SQLITE_OK ){ |
|
|
2441 |
struct stat sStat; |
|
|
2442 |
int rc2; |
|
|
2443 |
|
|
|
2444 |
for(; zCopy[i]!='/' && i |
|
|
2445 |
if( i==nCopy ) break; |
|
|
2446 |
zCopy[i] = '\0'; |
|
|
2447 |
|
|
|
2448 |
rc2 = fileStat(zCopy, &sStat); |
|
|
2449 |
if( rc2!=0 ){ |
|
|
2450 |
if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR; |
|
|
2451 |
}else{ |
|
|
2452 |
if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR; |
|
|
2453 |
} |
|
|
2454 |
zCopy[i] = '/'; |
|
|
2455 |
i++; |
|
|
2456 |
} |
|
|
2457 |
|
|
|
2458 |
sqlite3_free(zCopy); |
|
|
2459 |
} |
|
|
2460 |
|
|
|
2461 |
return rc; |
|
|
2462 |
} |
|
|
2463 |
|
|
|
2464 |
/* |
|
|
2465 |
** This function does the work for the writefile() UDF. Refer to |
|
|
2466 |
** header comments at the top of this file for details. |
|
|
2467 |
*/ |
|
|
2468 |
static int writeFile( |
|
|
2469 |
sqlite3_context *pCtx, /* Context to return bytes written in */ |
|
|
2470 |
const char *zFile, /* File to write */ |
|
|
2471 |
sqlite3_value *pData, /* Data to write */ |
|
|
2472 |
mode_t mode, /* MODE parameter passed to writefile() */ |
|
|
2473 |
sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */ |
|
|
2474 |
){ |
|
|
2475 |
#if !defined(_WIN32) && !defined(WIN32) && !defined (_KOLIBRI) |
|
|
2476 |
|
|
|
2477 |
if( S_ISLNK(mode) ){ |
|
|
2478 |
const char *zTo = (const char*)sqlite3_value_text(pData); |
|
|
2479 |
if( symlink(zTo, zFile)<0 ) return 1; |
|
|
2480 |
}else |
|
|
2481 |
#endif |
|
|
2482 |
{ |
|
|
2483 |
if( S_ISDIR(mode) ){ |
|
|
2484 |
if( mkdir(zFile, mode) ){ |
|
|
2485 |
/* The mkdir() call to create the directory failed. This might not |
|
|
2486 |
** be an error though - if there is already a directory at the same |
|
|
2487 |
** path and either the permissions already match or can be changed |
|
|
2488 |
** to do so using chmod(), it is not an error. */ |
|
|
2489 |
struct stat sStat; |
|
|
2490 |
if( errno!=EEXIST |
|
|
2491 |
|| 0!=fileStat(zFile, &sStat) |
|
|
2492 |
|| !S_ISDIR(sStat.st_mode) |
|
|
2493 |
|| ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777)) |
|
|
2494 |
){ |
|
|
2495 |
return 1; |
|
|
2496 |
} |
|
|
2497 |
} |
|
|
2498 |
}else{ |
|
|
2499 |
sqlite3_int64 nWrite = 0; |
|
|
2500 |
const char *z; |
|
|
2501 |
int rc = 0; |
|
|
2502 |
FILE *out = fopen(zFile, "wb"); |
|
|
2503 |
if( out==0 ) return 1; |
|
|
2504 |
z = (const char*)sqlite3_value_blob(pData); |
|
|
2505 |
if( z ){ |
|
|
2506 |
sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out); |
|
|
2507 |
nWrite = sqlite3_value_bytes(pData); |
|
|
2508 |
if( nWrite!=n ){ |
|
|
2509 |
rc = 1; |
|
|
2510 |
} |
|
|
2511 |
} |
|
|
2512 |
fclose(out); |
|
|
2513 |
if( rc==0 && mode && chmod(zFile, mode & 0777) ){ |
|
|
2514 |
rc = 1; |
|
|
2515 |
} |
|
|
2516 |
if( rc ) return 2; |
|
|
2517 |
sqlite3_result_int64(pCtx, nWrite); |
|
|
2518 |
} |
|
|
2519 |
} |
|
|
2520 |
|
|
|
2521 |
if( mtime>=0 ){ |
|
|
2522 |
#if defined(_WIN32) |
|
|
2523 |
#if !SQLITE_OS_WINRT |
|
|
2524 |
/* Windows */ |
|
|
2525 |
FILETIME lastAccess; |
|
|
2526 |
FILETIME lastWrite; |
|
|
2527 |
SYSTEMTIME currentTime; |
|
|
2528 |
LONGLONG intervals; |
|
|
2529 |
HANDLE hFile; |
|
|
2530 |
LPWSTR zUnicodeName; |
|
|
2531 |
extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*); |
|
|
2532 |
|
|
|
2533 |
GetSystemTime(¤tTime); |
|
|
2534 |
SystemTimeToFileTime(¤tTime, &lastAccess); |
|
|
2535 |
intervals = Int32x32To64(mtime, 10000000) + 116444736000000000; |
|
|
2536 |
lastWrite.dwLowDateTime = (DWORD)intervals; |
|
|
2537 |
lastWrite.dwHighDateTime = intervals >> 32; |
|
|
2538 |
zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile); |
|
|
2539 |
if( zUnicodeName==0 ){ |
|
|
2540 |
return 1; |
|
|
2541 |
} |
|
|
2542 |
hFile = CreateFileW( |
|
|
2543 |
zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, |
|
|
2544 |
FILE_FLAG_BACKUP_SEMANTICS, NULL |
|
|
2545 |
); |
|
|
2546 |
sqlite3_free(zUnicodeName); |
|
|
2547 |
if( hFile!=INVALID_HANDLE_VALUE ){ |
|
|
2548 |
BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite); |
|
|
2549 |
CloseHandle(hFile); |
|
|
2550 |
return !bResult; |
|
|
2551 |
}else{ |
|
|
2552 |
return 1; |
|
|
2553 |
} |
|
|
2554 |
#endif |
|
|
2555 |
#elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */ |
|
|
2556 |
/* Recent unix */ |
|
|
2557 |
struct timespec times[2]; |
|
|
2558 |
times[0].tv_nsec = times[1].tv_nsec = 0; |
|
|
2559 |
times[0].tv_sec = time(0); |
|
|
2560 |
times[1].tv_sec = mtime; |
|
|
2561 |
if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){ |
|
|
2562 |
return 1; |
|
|
2563 |
} |
|
|
2564 |
|
|
|
2565 |
|
|
|
2566 |
#elif !defined(_KOLIBRI) |
|
|
2567 |
/* Legacy unix */ |
|
|
2568 |
struct timeval times[2]; |
|
|
2569 |
times[0].tv_usec = times[1].tv_usec = 0; |
|
|
2570 |
times[0].tv_sec = time(0); |
|
|
2571 |
times[1].tv_sec = mtime; |
|
|
2572 |
if( utimes(zFile, times) ){ |
|
|
2573 |
return 1; |
|
|
2574 |
} |
|
|
2575 |
#endif |
|
|
2576 |
} |
|
|
2577 |
|
|
|
2578 |
return 0; |
|
|
2579 |
} |
|
|
2580 |
|
|
|
2581 |
/* |
|
|
2582 |
** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function. |
|
|
2583 |
** Refer to header comments at the top of this file for details. |
|
|
2584 |
*/ |
|
|
2585 |
static void writefileFunc( |
|
|
2586 |
sqlite3_context *context, |
|
|
2587 |
int argc, |
|
|
2588 |
sqlite3_value **argv |
|
|
2589 |
){ |
|
|
2590 |
const char *zFile; |
|
|
2591 |
mode_t mode = 0; |
|
|
2592 |
int res; |
|
|
2593 |
sqlite3_int64 mtime = -1; |
|
|
2594 |
|
|
|
2595 |
if( argc<2 || argc>4 ){ |
|
|
2596 |
sqlite3_result_error(context, |
|
|
2597 |
"wrong number of arguments to function writefile()", -1 |
|
|
2598 |
); |
|
|
2599 |
return; |
|
|
2600 |
} |
|
|
2601 |
|
|
|
2602 |
zFile = (const char*)sqlite3_value_text(argv[0]); |
|
|
2603 |
if( zFile==0 ) return; |
|
|
2604 |
if( argc>=3 ){ |
|
|
2605 |
mode = (mode_t)sqlite3_value_int(argv[2]); |
|
|
2606 |
} |
|
|
2607 |
if( argc==4 ){ |
|
|
2608 |
mtime = sqlite3_value_int64(argv[3]); |
|
|
2609 |
} |
|
|
2610 |
|
|
|
2611 |
res = writeFile(context, zFile, argv[1], mode, mtime); |
|
|
2612 |
if( res==1 && errno==ENOENT ){ |
|
|
2613 |
if( makeDirectory(zFile)==SQLITE_OK ){ |
|
|
2614 |
res = writeFile(context, zFile, argv[1], mode, mtime); |
|
|
2615 |
} |
|
|
2616 |
} |
|
|
2617 |
|
|
|
2618 |
if( argc>2 && res!=0 ){ |
|
|
2619 |
if( S_ISLNK(mode) ){ |
|
|
2620 |
ctxErrorMsg(context, "failed to create symlink: %s", zFile); |
|
|
2621 |
}else if( S_ISDIR(mode) ){ |
|
|
2622 |
ctxErrorMsg(context, "failed to create directory: %s", zFile); |
|
|
2623 |
}else{ |
|
|
2624 |
ctxErrorMsg(context, "failed to write file: %s", zFile); |
|
|
2625 |
} |
|
|
2626 |
} |
|
|
2627 |
} |
|
|
2628 |
|
|
|
2629 |
/* |
|
|
2630 |
** SQL function: lsmode(MODE) |
|
|
2631 |
** |
|
|
2632 |
** Given a numberic st_mode from stat(), convert it into a human-readable |
|
|
2633 |
** text string in the style of "ls -l". |
|
|
2634 |
*/ |
|
|
2635 |
static void lsModeFunc( |
|
|
2636 |
sqlite3_context *context, |
|
|
2637 |
int argc, |
|
|
2638 |
sqlite3_value **argv |
|
|
2639 |
){ |
|
|
2640 |
int i; |
|
|
2641 |
int iMode = sqlite3_value_int(argv[0]); |
|
|
2642 |
char z[16]; |
|
|
2643 |
(void)argc; |
|
|
2644 |
if( S_ISLNK(iMode) ){ |
|
|
2645 |
z[0] = 'l'; |
|
|
2646 |
}else if( S_ISREG(iMode) ){ |
|
|
2647 |
z[0] = '-'; |
|
|
2648 |
}else if( S_ISDIR(iMode) ){ |
|
|
2649 |
z[0] = 'd'; |
|
|
2650 |
}else{ |
|
|
2651 |
z[0] = '?'; |
|
|
2652 |
} |
|
|
2653 |
for(i=0; i<3; i++){ |
|
|
2654 |
int m = (iMode >> ((2-i)*3)); |
|
|
2655 |
char *a = &z[1 + i*3]; |
|
|
2656 |
a[0] = (m & 0x4) ? 'r' : '-'; |
|
|
2657 |
a[1] = (m & 0x2) ? 'w' : '-'; |
|
|
2658 |
a[2] = (m & 0x1) ? 'x' : '-'; |
|
|
2659 |
} |
|
|
2660 |
z[10] = '\0'; |
|
|
2661 |
sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); |
|
|
2662 |
} |
|
|
2663 |
|
|
|
2664 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
2665 |
|
|
|
2666 |
/* |
|
|
2667 |
** Cursor type for recursively iterating through a directory structure. |
|
|
2668 |
*/ |
|
|
2669 |
typedef struct fsdir_cursor fsdir_cursor; |
|
|
2670 |
typedef struct FsdirLevel FsdirLevel; |
|
|
2671 |
|
|
|
2672 |
struct FsdirLevel { |
|
|
2673 |
DIR *pDir; /* From opendir() */ |
|
|
2674 |
char *zDir; /* Name of directory (nul-terminated) */ |
|
|
2675 |
}; |
|
|
2676 |
|
|
|
2677 |
struct fsdir_cursor { |
|
|
2678 |
sqlite3_vtab_cursor base; /* Base class - must be first */ |
|
|
2679 |
|
|
|
2680 |
int nLvl; /* Number of entries in aLvl[] array */ |
|
|
2681 |
int iLvl; /* Index of current entry */ |
|
|
2682 |
FsdirLevel *aLvl; /* Hierarchy of directories being traversed */ |
|
|
2683 |
|
|
|
2684 |
const char *zBase; |
|
|
2685 |
int nBase; |
|
|
2686 |
|
|
|
2687 |
struct stat sStat; /* Current lstat() results */ |
|
|
2688 |
char *zPath; /* Path to current entry */ |
|
|
2689 |
sqlite3_int64 iRowid; /* Current rowid */ |
|
|
2690 |
}; |
|
|
2691 |
|
|
|
2692 |
typedef struct fsdir_tab fsdir_tab; |
|
|
2693 |
struct fsdir_tab { |
|
|
2694 |
sqlite3_vtab base; /* Base class - must be first */ |
|
|
2695 |
}; |
|
|
2696 |
|
|
|
2697 |
/* |
|
|
2698 |
** Construct a new fsdir virtual table object. |
|
|
2699 |
*/ |
|
|
2700 |
static int fsdirConnect( |
|
|
2701 |
sqlite3 *db, |
|
|
2702 |
void *pAux, |
|
|
2703 |
int argc, const char *const*argv, |
|
|
2704 |
sqlite3_vtab **ppVtab, |
|
|
2705 |
char **pzErr |
|
|
2706 |
){ |
|
|
2707 |
fsdir_tab *pNew = 0; |
|
|
2708 |
int rc; |
|
|
2709 |
(void)pAux; |
|
|
2710 |
(void)argc; |
|
|
2711 |
(void)argv; |
|
|
2712 |
(void)pzErr; |
|
|
2713 |
rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA); |
|
|
2714 |
if( rc==SQLITE_OK ){ |
|
|
2715 |
pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) ); |
|
|
2716 |
if( pNew==0 ) return SQLITE_NOMEM; |
|
|
2717 |
memset(pNew, 0, sizeof(*pNew)); |
|
|
2718 |
sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); |
|
|
2719 |
} |
|
|
2720 |
*ppVtab = (sqlite3_vtab*)pNew; |
|
|
2721 |
return rc; |
|
|
2722 |
} |
|
|
2723 |
|
|
|
2724 |
/* |
|
|
2725 |
** This method is the destructor for fsdir vtab objects. |
|
|
2726 |
*/ |
|
|
2727 |
static int fsdirDisconnect(sqlite3_vtab *pVtab){ |
|
|
2728 |
sqlite3_free(pVtab); |
|
|
2729 |
return SQLITE_OK; |
|
|
2730 |
} |
|
|
2731 |
|
|
|
2732 |
/* |
|
|
2733 |
** Constructor for a new fsdir_cursor object. |
|
|
2734 |
*/ |
|
|
2735 |
static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
|
|
2736 |
fsdir_cursor *pCur; |
|
|
2737 |
(void)p; |
|
|
2738 |
pCur = sqlite3_malloc( sizeof(*pCur) ); |
|
|
2739 |
if( pCur==0 ) return SQLITE_NOMEM; |
|
|
2740 |
memset(pCur, 0, sizeof(*pCur)); |
|
|
2741 |
pCur->iLvl = -1; |
|
|
2742 |
*ppCursor = &pCur->base; |
|
|
2743 |
return SQLITE_OK; |
|
|
2744 |
} |
|
|
2745 |
|
|
|
2746 |
/* |
|
|
2747 |
** Reset a cursor back to the state it was in when first returned |
|
|
2748 |
** by fsdirOpen(). |
|
|
2749 |
*/ |
|
|
2750 |
static void fsdirResetCursor(fsdir_cursor *pCur){ |
|
|
2751 |
int i; |
|
|
2752 |
for(i=0; i<=pCur->iLvl; i++){ |
|
|
2753 |
FsdirLevel *pLvl = &pCur->aLvl[i]; |
|
|
2754 |
if( pLvl->pDir ) closedir(pLvl->pDir); |
|
|
2755 |
sqlite3_free(pLvl->zDir); |
|
|
2756 |
} |
|
|
2757 |
sqlite3_free(pCur->zPath); |
|
|
2758 |
sqlite3_free(pCur->aLvl); |
|
|
2759 |
pCur->aLvl = 0; |
|
|
2760 |
pCur->zPath = 0; |
|
|
2761 |
pCur->zBase = 0; |
|
|
2762 |
pCur->nBase = 0; |
|
|
2763 |
pCur->nLvl = 0; |
|
|
2764 |
pCur->iLvl = -1; |
|
|
2765 |
pCur->iRowid = 1; |
|
|
2766 |
} |
|
|
2767 |
|
|
|
2768 |
/* |
|
|
2769 |
** Destructor for an fsdir_cursor. |
|
|
2770 |
*/ |
|
|
2771 |
static int fsdirClose(sqlite3_vtab_cursor *cur){ |
|
|
2772 |
fsdir_cursor *pCur = (fsdir_cursor*)cur; |
|
|
2773 |
|
|
|
2774 |
fsdirResetCursor(pCur); |
|
|
2775 |
sqlite3_free(pCur); |
|
|
2776 |
return SQLITE_OK; |
|
|
2777 |
} |
|
|
2778 |
|
|
|
2779 |
/* |
|
|
2780 |
** Set the error message for the virtual table associated with cursor |
|
|
2781 |
** pCur to the results of vprintf(zFmt, ...). |
|
|
2782 |
*/ |
|
|
2783 |
static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){ |
|
|
2784 |
va_list ap; |
|
|
2785 |
va_start(ap, zFmt); |
|
|
2786 |
pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); |
|
|
2787 |
va_end(ap); |
|
|
2788 |
} |
|
|
2789 |
|
|
|
2790 |
|
|
|
2791 |
/* |
|
|
2792 |
** Advance an fsdir_cursor to its next row of output. |
|
|
2793 |
*/ |
|
|
2794 |
static int fsdirNext(sqlite3_vtab_cursor *cur){ |
|
|
2795 |
fsdir_cursor *pCur = (fsdir_cursor*)cur; |
|
|
2796 |
mode_t m = pCur->sStat.st_mode; |
|
|
2797 |
|
|
|
2798 |
pCur->iRowid++; |
|
|
2799 |
if( S_ISDIR(m) ){ |
|
|
2800 |
/* Descend into this directory */ |
|
|
2801 |
int iNew = pCur->iLvl + 1; |
|
|
2802 |
FsdirLevel *pLvl; |
|
|
2803 |
if( iNew>=pCur->nLvl ){ |
|
|
2804 |
int nNew = iNew+1; |
|
|
2805 |
sqlite3_int64 nByte = nNew*sizeof(FsdirLevel); |
|
|
2806 |
FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte); |
|
|
2807 |
if( aNew==0 ) return SQLITE_NOMEM; |
|
|
2808 |
memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl)); |
|
|
2809 |
pCur->aLvl = aNew; |
|
|
2810 |
pCur->nLvl = nNew; |
|
|
2811 |
} |
|
|
2812 |
pCur->iLvl = iNew; |
|
|
2813 |
pLvl = &pCur->aLvl[iNew]; |
|
|
2814 |
|
|
|
2815 |
pLvl->zDir = pCur->zPath; |
|
|
2816 |
pCur->zPath = 0; |
|
|
2817 |
pLvl->pDir = opendir(pLvl->zDir); |
|
|
2818 |
if( pLvl->pDir==0 ){ |
|
|
2819 |
fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath); |
|
|
2820 |
return SQLITE_ERROR; |
|
|
2821 |
} |
|
|
2822 |
} |
|
|
2823 |
|
|
|
2824 |
while( pCur->iLvl>=0 ){ |
|
|
2825 |
FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl]; |
|
|
2826 |
struct dirent *pEntry = readdir(pLvl->pDir); |
|
|
2827 |
if( pEntry ){ |
|
|
2828 |
if( pEntry->d_name[0]=='.' ){ |
|
|
2829 |
if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue; |
|
|
2830 |
if( pEntry->d_name[1]=='\0' ) continue; |
|
|
2831 |
} |
|
|
2832 |
sqlite3_free(pCur->zPath); |
|
|
2833 |
pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name); |
|
|
2834 |
if( pCur->zPath==0 ) return SQLITE_NOMEM; |
|
|
2835 |
if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ |
|
|
2836 |
fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); |
|
|
2837 |
return SQLITE_ERROR; |
|
|
2838 |
} |
|
|
2839 |
return SQLITE_OK; |
|
|
2840 |
} |
|
|
2841 |
closedir(pLvl->pDir); |
|
|
2842 |
sqlite3_free(pLvl->zDir); |
|
|
2843 |
pLvl->pDir = 0; |
|
|
2844 |
pLvl->zDir = 0; |
|
|
2845 |
pCur->iLvl--; |
|
|
2846 |
} |
|
|
2847 |
|
|
|
2848 |
/* EOF */ |
|
|
2849 |
sqlite3_free(pCur->zPath); |
|
|
2850 |
pCur->zPath = 0; |
|
|
2851 |
return SQLITE_OK; |
|
|
2852 |
} |
|
|
2853 |
|
|
|
2854 |
/* |
|
|
2855 |
** Return values of columns for the row at which the series_cursor |
|
|
2856 |
** is currently pointing. |
|
|
2857 |
*/ |
|
|
2858 |
static int fsdirColumn( |
|
|
2859 |
sqlite3_vtab_cursor *cur, /* The cursor */ |
|
|
2860 |
sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
|
|
2861 |
int i /* Which column to return */ |
|
|
2862 |
){ |
|
|
2863 |
fsdir_cursor *pCur = (fsdir_cursor*)cur; |
|
|
2864 |
switch( i ){ |
|
|
2865 |
case FSDIR_COLUMN_NAME: { |
|
|
2866 |
sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT); |
|
|
2867 |
break; |
|
|
2868 |
} |
|
|
2869 |
|
|
|
2870 |
case FSDIR_COLUMN_MODE: |
|
|
2871 |
sqlite3_result_int64(ctx, pCur->sStat.st_mode); |
|
|
2872 |
break; |
|
|
2873 |
|
|
|
2874 |
case FSDIR_COLUMN_MTIME: |
|
|
2875 |
sqlite3_result_int64(ctx, pCur->sStat.st_mtime); |
|
|
2876 |
break; |
|
|
2877 |
|
|
|
2878 |
case FSDIR_COLUMN_DATA: { |
|
|
2879 |
mode_t m = pCur->sStat.st_mode; |
|
|
2880 |
if( S_ISDIR(m) ){ |
|
|
2881 |
sqlite3_result_null(ctx); |
|
|
2882 |
#if !defined(_WIN32) && !defined(WIN32) && !defined(_KOLIBRI) |
|
|
2883 |
}else if( S_ISLNK(m) ){ |
|
|
2884 |
char aStatic[64]; |
|
|
2885 |
char *aBuf = aStatic; |
|
|
2886 |
sqlite3_int64 nBuf = 64; |
|
|
2887 |
int n; |
|
|
2888 |
|
|
|
2889 |
while( 1 ){ |
|
|
2890 |
n = readlink(pCur->zPath, aBuf, nBuf); |
|
|
2891 |
if( n |
|
|
2892 |
if( aBuf!=aStatic ) sqlite3_free(aBuf); |
|
|
2893 |
nBuf = nBuf*2; |
|
|
2894 |
aBuf = sqlite3_malloc64(nBuf); |
|
|
2895 |
if( aBuf==0 ){ |
|
|
2896 |
sqlite3_result_error_nomem(ctx); |
|
|
2897 |
return SQLITE_NOMEM; |
|
|
2898 |
} |
|
|
2899 |
} |
|
|
2900 |
|
|
|
2901 |
sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT); |
|
|
2902 |
if( aBuf!=aStatic ) sqlite3_free(aBuf); |
|
|
2903 |
#endif |
|
|
2904 |
}else{ |
|
|
2905 |
readFileContents(ctx, pCur->zPath); |
|
|
2906 |
} |
|
|
2907 |
} |
|
|
2908 |
case FSDIR_COLUMN_PATH: |
|
|
2909 |
default: { |
|
|
2910 |
/* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters. |
|
|
2911 |
** always return their values as NULL */ |
|
|
2912 |
break; |
|
|
2913 |
} |
|
|
2914 |
} |
|
|
2915 |
return SQLITE_OK; |
|
|
2916 |
} |
|
|
2917 |
|
|
|
2918 |
/* |
|
|
2919 |
** Return the rowid for the current row. In this implementation, the |
|
|
2920 |
** first row returned is assigned rowid value 1, and each subsequent |
|
|
2921 |
** row a value 1 more than that of the previous. |
|
|
2922 |
*/ |
|
|
2923 |
static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
|
|
2924 |
fsdir_cursor *pCur = (fsdir_cursor*)cur; |
|
|
2925 |
*pRowid = pCur->iRowid; |
|
|
2926 |
return SQLITE_OK; |
|
|
2927 |
} |
|
|
2928 |
|
|
|
2929 |
/* |
|
|
2930 |
** Return TRUE if the cursor has been moved off of the last |
|
|
2931 |
** row of output. |
|
|
2932 |
*/ |
|
|
2933 |
static int fsdirEof(sqlite3_vtab_cursor *cur){ |
|
|
2934 |
fsdir_cursor *pCur = (fsdir_cursor*)cur; |
|
|
2935 |
return (pCur->zPath==0); |
|
|
2936 |
} |
|
|
2937 |
|
|
|
2938 |
/* |
|
|
2939 |
** xFilter callback. |
|
|
2940 |
** |
|
|
2941 |
** idxNum==1 PATH parameter only |
|
|
2942 |
** idxNum==2 Both PATH and DIR supplied |
|
|
2943 |
*/ |
|
|
2944 |
static int fsdirFilter( |
|
|
2945 |
sqlite3_vtab_cursor *cur, |
|
|
2946 |
int idxNum, const char *idxStr, |
|
|
2947 |
int argc, sqlite3_value **argv |
|
|
2948 |
){ |
|
|
2949 |
const char *zDir = 0; |
|
|
2950 |
fsdir_cursor *pCur = (fsdir_cursor*)cur; |
|
|
2951 |
(void)idxStr; |
|
|
2952 |
fsdirResetCursor(pCur); |
|
|
2953 |
|
|
|
2954 |
if( idxNum==0 ){ |
|
|
2955 |
fsdirSetErrmsg(pCur, "table function fsdir requires an argument"); |
|
|
2956 |
return SQLITE_ERROR; |
|
|
2957 |
} |
|
|
2958 |
|
|
|
2959 |
assert( argc==idxNum && (argc==1 || argc==2) ); |
|
|
2960 |
zDir = (const char*)sqlite3_value_text(argv[0]); |
|
|
2961 |
if( zDir==0 ){ |
|
|
2962 |
fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument"); |
|
|
2963 |
return SQLITE_ERROR; |
|
|
2964 |
} |
|
|
2965 |
if( argc==2 ){ |
|
|
2966 |
pCur->zBase = (const char*)sqlite3_value_text(argv[1]); |
|
|
2967 |
} |
|
|
2968 |
if( pCur->zBase ){ |
|
|
2969 |
pCur->nBase = (int)strlen(pCur->zBase)+1; |
|
|
2970 |
pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir); |
|
|
2971 |
}else{ |
|
|
2972 |
pCur->zPath = sqlite3_mprintf("%s", zDir); |
|
|
2973 |
} |
|
|
2974 |
|
|
|
2975 |
if( pCur->zPath==0 ){ |
|
|
2976 |
return SQLITE_NOMEM; |
|
|
2977 |
} |
|
|
2978 |
if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ |
|
|
2979 |
fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); |
|
|
2980 |
return SQLITE_ERROR; |
|
|
2981 |
} |
|
|
2982 |
|
|
|
2983 |
return SQLITE_OK; |
|
|
2984 |
} |
|
|
2985 |
|
|
|
2986 |
/* |
|
|
2987 |
** SQLite will invoke this method one or more times while planning a query |
|
|
2988 |
** that uses the generate_series virtual table. This routine needs to create |
|
|
2989 |
** a query plan for each invocation and compute an estimated cost for that |
|
|
2990 |
** plan. |
|
|
2991 |
** |
|
|
2992 |
** In this implementation idxNum is used to represent the |
|
|
2993 |
** query plan. idxStr is unused. |
|
|
2994 |
** |
|
|
2995 |
** The query plan is represented by values of idxNum: |
|
|
2996 |
** |
|
|
2997 |
** (1) The path value is supplied by argv[0] |
|
|
2998 |
** (2) Path is in argv[0] and dir is in argv[1] |
|
|
2999 |
*/ |
|
|
3000 |
static int fsdirBestIndex( |
|
|
3001 |
sqlite3_vtab *tab, |
|
|
3002 |
sqlite3_index_info *pIdxInfo |
|
|
3003 |
){ |
|
|
3004 |
int i; /* Loop over constraints */ |
|
|
3005 |
int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */ |
|
|
3006 |
int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */ |
|
|
3007 |
int seenPath = 0; /* True if an unusable PATH= constraint is seen */ |
|
|
3008 |
int seenDir = 0; /* True if an unusable DIR= constraint is seen */ |
|
|
3009 |
const struct sqlite3_index_constraint *pConstraint; |
|
|
3010 |
|
|
|
3011 |
(void)tab; |
|
|
3012 |
pConstraint = pIdxInfo->aConstraint; |
|
|
3013 |
for(i=0; inConstraint; i++, pConstraint++){ |
|
|
3014 |
if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
|
|
3015 |
switch( pConstraint->iColumn ){ |
|
|
3016 |
case FSDIR_COLUMN_PATH: { |
|
|
3017 |
if( pConstraint->usable ){ |
|
|
3018 |
idxPath = i; |
|
|
3019 |
seenPath = 0; |
|
|
3020 |
}else if( idxPath<0 ){ |
|
|
3021 |
seenPath = 1; |
|
|
3022 |
} |
|
|
3023 |
break; |
|
|
3024 |
} |
|
|
3025 |
case FSDIR_COLUMN_DIR: { |
|
|
3026 |
if( pConstraint->usable ){ |
|
|
3027 |
idxDir = i; |
|
|
3028 |
seenDir = 0; |
|
|
3029 |
}else if( idxDir<0 ){ |
|
|
3030 |
seenDir = 1; |
|
|
3031 |
} |
|
|
3032 |
break; |
|
|
3033 |
} |
|
|
3034 |
} |
|
|
3035 |
} |
|
|
3036 |
if( seenPath || seenDir ){ |
|
|
3037 |
/* If input parameters are unusable, disallow this plan */ |
|
|
3038 |
return SQLITE_CONSTRAINT; |
|
|
3039 |
} |
|
|
3040 |
|
|
|
3041 |
if( idxPath<0 ){ |
|
|
3042 |
pIdxInfo->idxNum = 0; |
|
|
3043 |
/* The pIdxInfo->estimatedCost should have been initialized to a huge |
|
|
3044 |
** number. Leave it unchanged. */ |
|
|
3045 |
pIdxInfo->estimatedRows = 0x7fffffff; |
|
|
3046 |
}else{ |
|
|
3047 |
pIdxInfo->aConstraintUsage[idxPath].omit = 1; |
|
|
3048 |
pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1; |
|
|
3049 |
if( idxDir>=0 ){ |
|
|
3050 |
pIdxInfo->aConstraintUsage[idxDir].omit = 1; |
|
|
3051 |
pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2; |
|
|
3052 |
pIdxInfo->idxNum = 2; |
|
|
3053 |
pIdxInfo->estimatedCost = 10.0; |
|
|
3054 |
}else{ |
|
|
3055 |
pIdxInfo->idxNum = 1; |
|
|
3056 |
pIdxInfo->estimatedCost = 100.0; |
|
|
3057 |
} |
|
|
3058 |
} |
|
|
3059 |
|
|
|
3060 |
return SQLITE_OK; |
|
|
3061 |
} |
|
|
3062 |
|
|
|
3063 |
/* |
|
|
3064 |
** Register the "fsdir" virtual table. |
|
|
3065 |
*/ |
|
|
3066 |
static int fsdirRegister(sqlite3 *db){ |
|
|
3067 |
static sqlite3_module fsdirModule = { |
|
|
3068 |
0, /* iVersion */ |
|
|
3069 |
0, /* xCreate */ |
|
|
3070 |
fsdirConnect, /* xConnect */ |
|
|
3071 |
fsdirBestIndex, /* xBestIndex */ |
|
|
3072 |
fsdirDisconnect, /* xDisconnect */ |
|
|
3073 |
0, /* xDestroy */ |
|
|
3074 |
fsdirOpen, /* xOpen - open a cursor */ |
|
|
3075 |
fsdirClose, /* xClose - close a cursor */ |
|
|
3076 |
fsdirFilter, /* xFilter - configure scan constraints */ |
|
|
3077 |
fsdirNext, /* xNext - advance a cursor */ |
|
|
3078 |
fsdirEof, /* xEof - check for end of scan */ |
|
|
3079 |
fsdirColumn, /* xColumn - read data */ |
|
|
3080 |
fsdirRowid, /* xRowid - read data */ |
|
|
3081 |
0, /* xUpdate */ |
|
|
3082 |
0, /* xBegin */ |
|
|
3083 |
0, /* xSync */ |
|
|
3084 |
0, /* xCommit */ |
|
|
3085 |
0, /* xRollback */ |
|
|
3086 |
0, /* xFindMethod */ |
|
|
3087 |
0, /* xRename */ |
|
|
3088 |
0, /* xSavepoint */ |
|
|
3089 |
0, /* xRelease */ |
|
|
3090 |
0, /* xRollbackTo */ |
|
|
3091 |
0, /* xShadowName */ |
|
|
3092 |
}; |
|
|
3093 |
|
|
|
3094 |
int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0); |
|
|
3095 |
return rc; |
|
|
3096 |
} |
|
|
3097 |
#else /* SQLITE_OMIT_VIRTUALTABLE */ |
|
|
3098 |
# define fsdirRegister(x) SQLITE_OK |
|
|
3099 |
#endif |
|
|
3100 |
|
|
|
3101 |
#ifdef _WIN32 |
|
|
3102 |
|
|
|
3103 |
#endif |
|
|
3104 |
int sqlite3_fileio_init( |
|
|
3105 |
sqlite3 *db, |
|
|
3106 |
char **pzErrMsg, |
|
|
3107 |
const sqlite3_api_routines *pApi |
|
|
3108 |
){ |
|
|
3109 |
int rc = SQLITE_OK; |
|
|
3110 |
SQLITE_EXTENSION_INIT2(pApi); |
|
|
3111 |
(void)pzErrMsg; /* Unused parameter */ |
|
|
3112 |
rc = sqlite3_create_function(db, "readfile", 1, |
|
|
3113 |
SQLITE_UTF8|SQLITE_DIRECTONLY, 0, |
|
|
3114 |
readfileFunc, 0, 0); |
|
|
3115 |
if( rc==SQLITE_OK ){ |
|
|
3116 |
rc = sqlite3_create_function(db, "writefile", -1, |
|
|
3117 |
SQLITE_UTF8|SQLITE_DIRECTONLY, 0, |
|
|
3118 |
writefileFunc, 0, 0); |
|
|
3119 |
} |
|
|
3120 |
if( rc==SQLITE_OK ){ |
|
|
3121 |
rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0, |
|
|
3122 |
lsModeFunc, 0, 0); |
|
|
3123 |
} |
|
|
3124 |
if( rc==SQLITE_OK ){ |
|
|
3125 |
rc = fsdirRegister(db); |
|
|
3126 |
} |
|
|
3127 |
return rc; |
|
|
3128 |
} |
|
|
3129 |
|
|
|
3130 |
/************************* End ../ext/misc/fileio.c ********************/ |
|
|
3131 |
/************************* Begin ../ext/misc/completion.c ******************/ |
|
|
3132 |
/* |
|
|
3133 |
** 2017-07-10 |
|
|
3134 |
** |
|
|
3135 |
** The author disclaims copyright to this source code. In place of |
|
|
3136 |
** a legal notice, here is a blessing: |
|
|
3137 |
** |
|
|
3138 |
** May you do good and not evil. |
|
|
3139 |
** May you find forgiveness for yourself and forgive others. |
|
|
3140 |
** May you share freely, never taking more than you give. |
|
|
3141 |
** |
|
|
3142 |
************************************************************************* |
|
|
3143 |
** |
|
|
3144 |
** This file implements an eponymous virtual table that returns suggested |
|
|
3145 |
** completions for a partial SQL input. |
|
|
3146 |
** |
|
|
3147 |
** Suggested usage: |
|
|
3148 |
** |
|
|
3149 |
** SELECT DISTINCT candidate COLLATE nocase |
|
|
3150 |
** FROM completion($prefix,$wholeline) |
|
|
3151 |
** ORDER BY 1; |
|
|
3152 |
** |
|
|
3153 |
** The two query parameters are optional. $prefix is the text of the |
|
|
3154 |
** current word being typed and that is to be completed. $wholeline is |
|
|
3155 |
** the complete input line, used for context. |
|
|
3156 |
** |
|
|
3157 |
** The raw completion() table might return the same candidate multiple |
|
|
3158 |
** times, for example if the same column name is used to two or more |
|
|
3159 |
** tables. And the candidates are returned in an arbitrary order. Hence, |
|
|
3160 |
** the DISTINCT and ORDER BY are recommended. |
|
|
3161 |
** |
|
|
3162 |
** This virtual table operates at the speed of human typing, and so there |
|
|
3163 |
** is no attempt to make it fast. Even a slow implementation will be much |
|
|
3164 |
** faster than any human can type. |
|
|
3165 |
** |
|
|
3166 |
*/ |
|
|
3167 |
/* #include "sqlite3ext.h" */ |
|
|
3168 |
SQLITE_EXTENSION_INIT1 |
|
|
3169 |
#include |
|
|
3170 |
#include |
|
|
3171 |
#include |
|
|
3172 |
|
|
|
3173 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
3174 |
|
|
|
3175 |
/* completion_vtab is a subclass of sqlite3_vtab which will |
|
|
3176 |
** serve as the underlying representation of a completion virtual table |
|
|
3177 |
*/ |
|
|
3178 |
typedef struct completion_vtab completion_vtab; |
|
|
3179 |
struct completion_vtab { |
|
|
3180 |
sqlite3_vtab base; /* Base class - must be first */ |
|
|
3181 |
sqlite3 *db; /* Database connection for this completion vtab */ |
|
|
3182 |
}; |
|
|
3183 |
|
|
|
3184 |
/* completion_cursor is a subclass of sqlite3_vtab_cursor which will |
|
|
3185 |
** serve as the underlying representation of a cursor that scans |
|
|
3186 |
** over rows of the result |
|
|
3187 |
*/ |
|
|
3188 |
typedef struct completion_cursor completion_cursor; |
|
|
3189 |
struct completion_cursor { |
|
|
3190 |
sqlite3_vtab_cursor base; /* Base class - must be first */ |
|
|
3191 |
sqlite3 *db; /* Database connection for this cursor */ |
|
|
3192 |
int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */ |
|
|
3193 |
char *zPrefix; /* The prefix for the word we want to complete */ |
|
|
3194 |
char *zLine; /* The whole that we want to complete */ |
|
|
3195 |
const char *zCurrentRow; /* Current output row */ |
|
|
3196 |
int szRow; /* Length of the zCurrentRow string */ |
|
|
3197 |
sqlite3_stmt *pStmt; /* Current statement */ |
|
|
3198 |
sqlite3_int64 iRowid; /* The rowid */ |
|
|
3199 |
int ePhase; /* Current phase */ |
|
|
3200 |
int j; /* inter-phase counter */ |
|
|
3201 |
}; |
|
|
3202 |
|
|
|
3203 |
/* Values for ePhase: |
|
|
3204 |
*/ |
|
|
3205 |
#define COMPLETION_FIRST_PHASE 1 |
|
|
3206 |
#define COMPLETION_KEYWORDS 1 |
|
|
3207 |
#define COMPLETION_PRAGMAS 2 |
|
|
3208 |
#define COMPLETION_FUNCTIONS 3 |
|
|
3209 |
#define COMPLETION_COLLATIONS 4 |
|
|
3210 |
#define COMPLETION_INDEXES 5 |
|
|
3211 |
#define COMPLETION_TRIGGERS 6 |
|
|
3212 |
#define COMPLETION_DATABASES 7 |
|
|
3213 |
#define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */ |
|
|
3214 |
#define COMPLETION_COLUMNS 9 |
|
|
3215 |
#define COMPLETION_MODULES 10 |
|
|
3216 |
#define COMPLETION_EOF 11 |
|
|
3217 |
|
|
|
3218 |
/* |
|
|
3219 |
** The completionConnect() method is invoked to create a new |
|
|
3220 |
** completion_vtab that describes the completion virtual table. |
|
|
3221 |
** |
|
|
3222 |
** Think of this routine as the constructor for completion_vtab objects. |
|
|
3223 |
** |
|
|
3224 |
** All this routine needs to do is: |
|
|
3225 |
** |
|
|
3226 |
** (1) Allocate the completion_vtab object and initialize all fields. |
|
|
3227 |
** |
|
|
3228 |
** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the |
|
|
3229 |
** result set of queries against completion will look like. |
|
|
3230 |
*/ |
|
|
3231 |
static int completionConnect( |
|
|
3232 |
sqlite3 *db, |
|
|
3233 |
void *pAux, |
|
|
3234 |
int argc, const char *const*argv, |
|
|
3235 |
sqlite3_vtab **ppVtab, |
|
|
3236 |
char **pzErr |
|
|
3237 |
){ |
|
|
3238 |
completion_vtab *pNew; |
|
|
3239 |
int rc; |
|
|
3240 |
|
|
|
3241 |
(void)(pAux); /* Unused parameter */ |
|
|
3242 |
(void)(argc); /* Unused parameter */ |
|
|
3243 |
(void)(argv); /* Unused parameter */ |
|
|
3244 |
(void)(pzErr); /* Unused parameter */ |
|
|
3245 |
|
|
|
3246 |
/* Column numbers */ |
|
|
3247 |
#define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */ |
|
|
3248 |
#define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */ |
|
|
3249 |
#define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */ |
|
|
3250 |
#define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */ |
|
|
3251 |
|
|
|
3252 |
sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); |
|
|
3253 |
rc = sqlite3_declare_vtab(db, |
|
|
3254 |
"CREATE TABLE x(" |
|
|
3255 |
" candidate TEXT," |
|
|
3256 |
" prefix TEXT HIDDEN," |
|
|
3257 |
" wholeline TEXT HIDDEN," |
|
|
3258 |
" phase INT HIDDEN" /* Used for debugging only */ |
|
|
3259 |
")"); |
|
|
3260 |
if( rc==SQLITE_OK ){ |
|
|
3261 |
pNew = sqlite3_malloc( sizeof(*pNew) ); |
|
|
3262 |
*ppVtab = (sqlite3_vtab*)pNew; |
|
|
3263 |
if( pNew==0 ) return SQLITE_NOMEM; |
|
|
3264 |
memset(pNew, 0, sizeof(*pNew)); |
|
|
3265 |
pNew->db = db; |
|
|
3266 |
} |
|
|
3267 |
return rc; |
|
|
3268 |
} |
|
|
3269 |
|
|
|
3270 |
/* |
|
|
3271 |
** This method is the destructor for completion_cursor objects. |
|
|
3272 |
*/ |
|
|
3273 |
static int completionDisconnect(sqlite3_vtab *pVtab){ |
|
|
3274 |
sqlite3_free(pVtab); |
|
|
3275 |
return SQLITE_OK; |
|
|
3276 |
} |
|
|
3277 |
|
|
|
3278 |
/* |
|
|
3279 |
** Constructor for a new completion_cursor object. |
|
|
3280 |
*/ |
|
|
3281 |
static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
|
|
3282 |
completion_cursor *pCur; |
|
|
3283 |
pCur = sqlite3_malloc( sizeof(*pCur) ); |
|
|
3284 |
if( pCur==0 ) return SQLITE_NOMEM; |
|
|
3285 |
memset(pCur, 0, sizeof(*pCur)); |
|
|
3286 |
pCur->db = ((completion_vtab*)p)->db; |
|
|
3287 |
*ppCursor = &pCur->base; |
|
|
3288 |
return SQLITE_OK; |
|
|
3289 |
} |
|
|
3290 |
|
|
|
3291 |
/* |
|
|
3292 |
** Reset the completion_cursor. |
|
|
3293 |
*/ |
|
|
3294 |
static void completionCursorReset(completion_cursor *pCur){ |
|
|
3295 |
sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0; |
|
|
3296 |
sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0; |
|
|
3297 |
sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0; |
|
|
3298 |
pCur->j = 0; |
|
|
3299 |
} |
|
|
3300 |
|
|
|
3301 |
/* |
|
|
3302 |
** Destructor for a completion_cursor. |
|
|
3303 |
*/ |
|
|
3304 |
static int completionClose(sqlite3_vtab_cursor *cur){ |
|
|
3305 |
completionCursorReset((completion_cursor*)cur); |
|
|
3306 |
sqlite3_free(cur); |
|
|
3307 |
return SQLITE_OK; |
|
|
3308 |
} |
|
|
3309 |
|
|
|
3310 |
/* |
|
|
3311 |
** Advance a completion_cursor to its next row of output. |
|
|
3312 |
** |
|
|
3313 |
** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object |
|
|
3314 |
** record the current state of the scan. This routine sets ->zCurrentRow |
|
|
3315 |
** to the current row of output and then returns. If no more rows remain, |
|
|
3316 |
** then ->ePhase is set to COMPLETION_EOF which will signal the virtual |
|
|
3317 |
** table that has reached the end of its scan. |
|
|
3318 |
** |
|
|
3319 |
** The current implementation just lists potential identifiers and |
|
|
3320 |
** keywords and filters them by zPrefix. Future enhancements should |
|
|
3321 |
** take zLine into account to try to restrict the set of identifiers and |
|
|
3322 |
** keywords based on what would be legal at the current point of input. |
|
|
3323 |
*/ |
|
|
3324 |
static int completionNext(sqlite3_vtab_cursor *cur){ |
|
|
3325 |
completion_cursor *pCur = (completion_cursor*)cur; |
|
|
3326 |
int eNextPhase = 0; /* Next phase to try if current phase reaches end */ |
|
|
3327 |
int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */ |
|
|
3328 |
pCur->iRowid++; |
|
|
3329 |
while( pCur->ePhase!=COMPLETION_EOF ){ |
|
|
3330 |
switch( pCur->ePhase ){ |
|
|
3331 |
case COMPLETION_KEYWORDS: { |
|
|
3332 |
if( pCur->j >= sqlite3_keyword_count() ){ |
|
|
3333 |
pCur->zCurrentRow = 0; |
|
|
3334 |
pCur->ePhase = COMPLETION_DATABASES; |
|
|
3335 |
}else{ |
|
|
3336 |
sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow); |
|
|
3337 |
} |
|
|
3338 |
iCol = -1; |
|
|
3339 |
break; |
|
|
3340 |
} |
|
|
3341 |
case COMPLETION_DATABASES: { |
|
|
3342 |
if( pCur->pStmt==0 ){ |
|
|
3343 |
sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, |
|
|
3344 |
&pCur->pStmt, 0); |
|
|
3345 |
} |
|
|
3346 |
iCol = 1; |
|
|
3347 |
eNextPhase = COMPLETION_TABLES; |
|
|
3348 |
break; |
|
|
3349 |
} |
|
|
3350 |
case COMPLETION_TABLES: { |
|
|
3351 |
if( pCur->pStmt==0 ){ |
|
|
3352 |
sqlite3_stmt *pS2; |
|
|
3353 |
char *zSql = 0; |
|
|
3354 |
const char *zSep = ""; |
|
|
3355 |
sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); |
|
|
3356 |
while( sqlite3_step(pS2)==SQLITE_ROW ){ |
|
|
3357 |
const char *zDb = (const char*)sqlite3_column_text(pS2, 1); |
|
|
3358 |
zSql = sqlite3_mprintf( |
|
|
3359 |
"%z%s" |
|
|
3360 |
"SELECT name FROM \"%w\".sqlite_schema", |
|
|
3361 |
zSql, zSep, zDb |
|
|
3362 |
); |
|
|
3363 |
if( zSql==0 ) return SQLITE_NOMEM; |
|
|
3364 |
zSep = " UNION "; |
|
|
3365 |
} |
|
|
3366 |
sqlite3_finalize(pS2); |
|
|
3367 |
sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); |
|
|
3368 |
sqlite3_free(zSql); |
|
|
3369 |
} |
|
|
3370 |
iCol = 0; |
|
|
3371 |
eNextPhase = COMPLETION_COLUMNS; |
|
|
3372 |
break; |
|
|
3373 |
} |
|
|
3374 |
case COMPLETION_COLUMNS: { |
|
|
3375 |
if( pCur->pStmt==0 ){ |
|
|
3376 |
sqlite3_stmt *pS2; |
|
|
3377 |
char *zSql = 0; |
|
|
3378 |
const char *zSep = ""; |
|
|
3379 |
sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); |
|
|
3380 |
while( sqlite3_step(pS2)==SQLITE_ROW ){ |
|
|
3381 |
const char *zDb = (const char*)sqlite3_column_text(pS2, 1); |
|
|
3382 |
zSql = sqlite3_mprintf( |
|
|
3383 |
"%z%s" |
|
|
3384 |
"SELECT pti.name FROM \"%w\".sqlite_schema AS sm" |
|
|
3385 |
" JOIN pragma_table_info(sm.name,%Q) AS pti" |
|
|
3386 |
" WHERE sm.type='table'", |
|
|
3387 |
zSql, zSep, zDb, zDb |
|
|
3388 |
); |
|
|
3389 |
if( zSql==0 ) return SQLITE_NOMEM; |
|
|
3390 |
zSep = " UNION "; |
|
|
3391 |
} |
|
|
3392 |
sqlite3_finalize(pS2); |
|
|
3393 |
sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); |
|
|
3394 |
sqlite3_free(zSql); |
|
|
3395 |
} |
|
|
3396 |
iCol = 0; |
|
|
3397 |
eNextPhase = COMPLETION_EOF; |
|
|
3398 |
break; |
|
|
3399 |
} |
|
|
3400 |
} |
|
|
3401 |
if( iCol<0 ){ |
|
|
3402 |
/* This case is when the phase presets zCurrentRow */ |
|
|
3403 |
if( pCur->zCurrentRow==0 ) continue; |
|
|
3404 |
}else{ |
|
|
3405 |
if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){ |
|
|
3406 |
/* Extract the next row of content */ |
|
|
3407 |
pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol); |
|
|
3408 |
pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol); |
|
|
3409 |
}else{ |
|
|
3410 |
/* When all rows are finished, advance to the next phase */ |
|
|
3411 |
sqlite3_finalize(pCur->pStmt); |
|
|
3412 |
pCur->pStmt = 0; |
|
|
3413 |
pCur->ePhase = eNextPhase; |
|
|
3414 |
continue; |
|
|
3415 |
} |
|
|
3416 |
} |
|
|
3417 |
if( pCur->nPrefix==0 ) break; |
|
|
3418 |
if( pCur->nPrefix<=pCur->szRow |
|
|
3419 |
&& sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 |
|
|
3420 |
){ |
|
|
3421 |
break; |
|
|
3422 |
} |
|
|
3423 |
} |
|
|
3424 |
|
|
|
3425 |
return SQLITE_OK; |
|
|
3426 |
} |
|
|
3427 |
|
|
|
3428 |
/* |
|
|
3429 |
** Return values of columns for the row at which the completion_cursor |
|
|
3430 |
** is currently pointing. |
|
|
3431 |
*/ |
|
|
3432 |
static int completionColumn( |
|
|
3433 |
sqlite3_vtab_cursor *cur, /* The cursor */ |
|
|
3434 |
sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
|
|
3435 |
int i /* Which column to return */ |
|
|
3436 |
){ |
|
|
3437 |
completion_cursor *pCur = (completion_cursor*)cur; |
|
|
3438 |
switch( i ){ |
|
|
3439 |
case COMPLETION_COLUMN_CANDIDATE: { |
|
|
3440 |
sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT); |
|
|
3441 |
break; |
|
|
3442 |
} |
|
|
3443 |
case COMPLETION_COLUMN_PREFIX: { |
|
|
3444 |
sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT); |
|
|
3445 |
break; |
|
|
3446 |
} |
|
|
3447 |
case COMPLETION_COLUMN_WHOLELINE: { |
|
|
3448 |
sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT); |
|
|
3449 |
break; |
|
|
3450 |
} |
|
|
3451 |
case COMPLETION_COLUMN_PHASE: { |
|
|
3452 |
sqlite3_result_int(ctx, pCur->ePhase); |
|
|
3453 |
break; |
|
|
3454 |
} |
|
|
3455 |
} |
|
|
3456 |
return SQLITE_OK; |
|
|
3457 |
} |
|
|
3458 |
|
|
|
3459 |
/* |
|
|
3460 |
** Return the rowid for the current row. In this implementation, the |
|
|
3461 |
** rowid is the same as the output value. |
|
|
3462 |
*/ |
|
|
3463 |
static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
|
|
3464 |
completion_cursor *pCur = (completion_cursor*)cur; |
|
|
3465 |
*pRowid = pCur->iRowid; |
|
|
3466 |
return SQLITE_OK; |
|
|
3467 |
} |
|
|
3468 |
|
|
|
3469 |
/* |
|
|
3470 |
** Return TRUE if the cursor has been moved off of the last |
|
|
3471 |
** row of output. |
|
|
3472 |
*/ |
|
|
3473 |
static int completionEof(sqlite3_vtab_cursor *cur){ |
|
|
3474 |
completion_cursor *pCur = (completion_cursor*)cur; |
|
|
3475 |
return pCur->ePhase >= COMPLETION_EOF; |
|
|
3476 |
} |
|
|
3477 |
|
|
|
3478 |
/* |
|
|
3479 |
** This method is called to "rewind" the completion_cursor object back |
|
|
3480 |
** to the first row of output. This method is always called at least |
|
|
3481 |
** once prior to any call to completionColumn() or completionRowid() or |
|
|
3482 |
** completionEof(). |
|
|
3483 |
*/ |
|
|
3484 |
static int completionFilter( |
|
|
3485 |
sqlite3_vtab_cursor *pVtabCursor, |
|
|
3486 |
int idxNum, const char *idxStr, |
|
|
3487 |
int argc, sqlite3_value **argv |
|
|
3488 |
){ |
|
|
3489 |
completion_cursor *pCur = (completion_cursor *)pVtabCursor; |
|
|
3490 |
int iArg = 0; |
|
|
3491 |
(void)(idxStr); /* Unused parameter */ |
|
|
3492 |
(void)(argc); /* Unused parameter */ |
|
|
3493 |
completionCursorReset(pCur); |
|
|
3494 |
if( idxNum & 1 ){ |
|
|
3495 |
pCur->nPrefix = sqlite3_value_bytes(argv[iArg]); |
|
|
3496 |
if( pCur->nPrefix>0 ){ |
|
|
3497 |
pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); |
|
|
3498 |
if( pCur->zPrefix==0 ) return SQLITE_NOMEM; |
|
|
3499 |
} |
|
|
3500 |
iArg = 1; |
|
|
3501 |
} |
|
|
3502 |
if( idxNum & 2 ){ |
|
|
3503 |
pCur->nLine = sqlite3_value_bytes(argv[iArg]); |
|
|
3504 |
if( pCur->nLine>0 ){ |
|
|
3505 |
pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); |
|
|
3506 |
if( pCur->zLine==0 ) return SQLITE_NOMEM; |
|
|
3507 |
} |
|
|
3508 |
} |
|
|
3509 |
if( pCur->zLine!=0 && pCur->zPrefix==0 ){ |
|
|
3510 |
int i = pCur->nLine; |
|
|
3511 |
while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){ |
|
|
3512 |
i--; |
|
|
3513 |
} |
|
|
3514 |
pCur->nPrefix = pCur->nLine - i; |
|
|
3515 |
if( pCur->nPrefix>0 ){ |
|
|
3516 |
pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i); |
|
|
3517 |
if( pCur->zPrefix==0 ) return SQLITE_NOMEM; |
|
|
3518 |
} |
|
|
3519 |
} |
|
|
3520 |
pCur->iRowid = 0; |
|
|
3521 |
pCur->ePhase = COMPLETION_FIRST_PHASE; |
|
|
3522 |
return completionNext(pVtabCursor); |
|
|
3523 |
} |
|
|
3524 |
|
|
|
3525 |
/* |
|
|
3526 |
** SQLite will invoke this method one or more times while planning a query |
|
|
3527 |
** that uses the completion virtual table. This routine needs to create |
|
|
3528 |
** a query plan for each invocation and compute an estimated cost for that |
|
|
3529 |
** plan. |
|
|
3530 |
** |
|
|
3531 |
** There are two hidden parameters that act as arguments to the table-valued |
|
|
3532 |
** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix" |
|
|
3533 |
** is available and bit 1 is set if "wholeline" is available. |
|
|
3534 |
*/ |
|
|
3535 |
static int completionBestIndex( |
|
|
3536 |
sqlite3_vtab *tab, |
|
|
3537 |
sqlite3_index_info *pIdxInfo |
|
|
3538 |
){ |
|
|
3539 |
int i; /* Loop over constraints */ |
|
|
3540 |
int idxNum = 0; /* The query plan bitmask */ |
|
|
3541 |
int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */ |
|
|
3542 |
int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */ |
|
|
3543 |
int nArg = 0; /* Number of arguments that completeFilter() expects */ |
|
|
3544 |
const struct sqlite3_index_constraint *pConstraint; |
|
|
3545 |
|
|
|
3546 |
(void)(tab); /* Unused parameter */ |
|
|
3547 |
pConstraint = pIdxInfo->aConstraint; |
|
|
3548 |
for(i=0; inConstraint; i++, pConstraint++){ |
|
|
3549 |
if( pConstraint->usable==0 ) continue; |
|
|
3550 |
if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; |
|
|
3551 |
switch( pConstraint->iColumn ){ |
|
|
3552 |
case COMPLETION_COLUMN_PREFIX: |
|
|
3553 |
prefixIdx = i; |
|
|
3554 |
idxNum |= 1; |
|
|
3555 |
break; |
|
|
3556 |
case COMPLETION_COLUMN_WHOLELINE: |
|
|
3557 |
wholelineIdx = i; |
|
|
3558 |
idxNum |= 2; |
|
|
3559 |
break; |
|
|
3560 |
} |
|
|
3561 |
} |
|
|
3562 |
if( prefixIdx>=0 ){ |
|
|
3563 |
pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg; |
|
|
3564 |
pIdxInfo->aConstraintUsage[prefixIdx].omit = 1; |
|
|
3565 |
} |
|
|
3566 |
if( wholelineIdx>=0 ){ |
|
|
3567 |
pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg; |
|
|
3568 |
pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1; |
|
|
3569 |
} |
|
|
3570 |
pIdxInfo->idxNum = idxNum; |
|
|
3571 |
pIdxInfo->estimatedCost = (double)5000 - 1000*nArg; |
|
|
3572 |
pIdxInfo->estimatedRows = 500 - 100*nArg; |
|
|
3573 |
return SQLITE_OK; |
|
|
3574 |
} |
|
|
3575 |
|
|
|
3576 |
/* |
|
|
3577 |
** This following structure defines all the methods for the |
|
|
3578 |
** completion virtual table. |
|
|
3579 |
*/ |
|
|
3580 |
static sqlite3_module completionModule = { |
|
|
3581 |
0, /* iVersion */ |
|
|
3582 |
0, /* xCreate */ |
|
|
3583 |
completionConnect, /* xConnect */ |
|
|
3584 |
completionBestIndex, /* xBestIndex */ |
|
|
3585 |
completionDisconnect, /* xDisconnect */ |
|
|
3586 |
0, /* xDestroy */ |
|
|
3587 |
completionOpen, /* xOpen - open a cursor */ |
|
|
3588 |
completionClose, /* xClose - close a cursor */ |
|
|
3589 |
completionFilter, /* xFilter - configure scan constraints */ |
|
|
3590 |
completionNext, /* xNext - advance a cursor */ |
|
|
3591 |
completionEof, /* xEof - check for end of scan */ |
|
|
3592 |
completionColumn, /* xColumn - read data */ |
|
|
3593 |
completionRowid, /* xRowid - read data */ |
|
|
3594 |
0, /* xUpdate */ |
|
|
3595 |
0, /* xBegin */ |
|
|
3596 |
0, /* xSync */ |
|
|
3597 |
0, /* xCommit */ |
|
|
3598 |
0, /* xRollback */ |
|
|
3599 |
0, /* xFindMethod */ |
|
|
3600 |
0, /* xRename */ |
|
|
3601 |
0, /* xSavepoint */ |
|
|
3602 |
0, /* xRelease */ |
|
|
3603 |
0, /* xRollbackTo */ |
|
|
3604 |
|
|
|
3605 |
}; |
|
|
3606 |
|
|
|
3607 |
#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
|
|
3608 |
|
|
|
3609 |
int sqlite3CompletionVtabInit(sqlite3 *db){ |
|
|
3610 |
int rc = SQLITE_OK; |
|
|
3611 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
3612 |
rc = sqlite3_create_module(db, "completion", &completionModule, 0); |
|
|
3613 |
#endif |
|
|
3614 |
return rc; |
|
|
3615 |
} |
|
|
3616 |
|
|
|
3617 |
#ifdef _WIN32 |
|
|
3618 |
|
|
|
3619 |
#endif |
|
|
3620 |
int sqlite3_completion_init( |
|
|
3621 |
sqlite3 *db, |
|
|
3622 |
char **pzErrMsg, |
|
|
3623 |
const sqlite3_api_routines *pApi |
|
|
3624 |
){ |
|
|
3625 |
int rc = SQLITE_OK; |
|
|
3626 |
SQLITE_EXTENSION_INIT2(pApi); |
|
|
3627 |
(void)(pzErrMsg); /* Unused parameter */ |
|
|
3628 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
3629 |
rc = sqlite3CompletionVtabInit(db); |
|
|
3630 |
#endif |
|
|
3631 |
return rc; |
|
|
3632 |
} |
|
|
3633 |
|
|
|
3634 |
/************************* End ../ext/misc/completion.c ********************/ |
|
|
3635 |
/************************* Begin ../ext/misc/appendvfs.c ******************/ |
|
|
3636 |
/* |
|
|
3637 |
** 2017-10-20 |
|
|
3638 |
** |
|
|
3639 |
** The author disclaims copyright to this source code. In place of |
|
|
3640 |
** a legal notice, here is a blessing: |
|
|
3641 |
** |
|
|
3642 |
** May you do good and not evil. |
|
|
3643 |
** May you find forgiveness for yourself and forgive others. |
|
|
3644 |
** May you share freely, never taking more than you give. |
|
|
3645 |
** |
|
|
3646 |
****************************************************************************** |
|
|
3647 |
** |
|
|
3648 |
** This file implements a VFS shim that allows an SQLite database to be |
|
|
3649 |
** appended onto the end of some other file, such as an executable. |
|
|
3650 |
** |
|
|
3651 |
** A special record must appear at the end of the file that identifies the |
|
|
3652 |
** file as an appended database and provides the offset to the first page |
|
|
3653 |
** of the exposed content. (Or, it is the length of the content prefix.) |
|
|
3654 |
** For best performance page 1 should be located at a disk page boundary, |
|
|
3655 |
** though that is not required. |
|
|
3656 |
** |
|
|
3657 |
** When opening a database using this VFS, the connection might treat |
|
|
3658 |
** the file as an ordinary SQLite database, or it might treat it as a |
|
|
3659 |
** database appended onto some other file. The decision is made by |
|
|
3660 |
** applying the following rules in order: |
|
|
3661 |
** |
|
|
3662 |
** (1) An empty file is an ordinary database. |
|
|
3663 |
** |
|
|
3664 |
** (2) If the file ends with the appendvfs trailer string |
|
|
3665 |
** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database. |
|
|
3666 |
** |
|
|
3667 |
** (3) If the file begins with the standard SQLite prefix string |
|
|
3668 |
** "SQLite format 3", that file is an ordinary database. |
|
|
3669 |
** |
|
|
3670 |
** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is |
|
|
3671 |
** set, then a new database is appended to the already existing file. |
|
|
3672 |
** |
|
|
3673 |
** (5) Otherwise, SQLITE_CANTOPEN is returned. |
|
|
3674 |
** |
|
|
3675 |
** To avoid unnecessary complications with the PENDING_BYTE, the size of |
|
|
3676 |
** the file containing the database is limited to 1GiB. (1073741824 bytes) |
|
|
3677 |
** This VFS will not read or write past the 1GiB mark. This restriction |
|
|
3678 |
** might be lifted in future versions. For now, if you need a larger |
|
|
3679 |
** database, then keep it in a separate file. |
|
|
3680 |
** |
|
|
3681 |
** If the file being opened is a plain database (not an appended one), then |
|
|
3682 |
** this shim is a pass-through into the default underlying VFS. (rule 3) |
|
|
3683 |
**/ |
|
|
3684 |
/* #include "sqlite3ext.h" */ |
|
|
3685 |
SQLITE_EXTENSION_INIT1 |
|
|
3686 |
#include |
|
|
3687 |
#include |
|
|
3688 |
|
|
|
3689 |
/* The append mark at the end of the database is: |
|
|
3690 |
** |
|
|
3691 |
** Start-Of-SQLite3-NNNNNNNN |
|
|
3692 |
** 123456789 123456789 12345 |
|
|
3693 |
** |
|
|
3694 |
** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is |
|
|
3695 |
** the offset to page 1, and also the length of the prefix content. |
|
|
3696 |
*/ |
|
|
3697 |
#define APND_MARK_PREFIX "Start-Of-SQLite3-" |
|
|
3698 |
#define APND_MARK_PREFIX_SZ 17 |
|
|
3699 |
#define APND_MARK_FOS_SZ 8 |
|
|
3700 |
#define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ) |
|
|
3701 |
|
|
|
3702 |
/* |
|
|
3703 |
** Maximum size of the combined prefix + database + append-mark. This |
|
|
3704 |
** must be less than 0x40000000 to avoid locking issues on Windows. |
|
|
3705 |
*/ |
|
|
3706 |
#define APND_MAX_SIZE (0x40000000) |
|
|
3707 |
|
|
|
3708 |
/* |
|
|
3709 |
** Try to align the database to an even multiple of APND_ROUNDUP bytes. |
|
|
3710 |
*/ |
|
|
3711 |
#ifndef APND_ROUNDUP |
|
|
3712 |
#define APND_ROUNDUP 4096 |
|
|
3713 |
#endif |
|
|
3714 |
#define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1)) |
|
|
3715 |
#define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK) |
|
|
3716 |
|
|
|
3717 |
/* |
|
|
3718 |
** Forward declaration of objects used by this utility |
|
|
3719 |
*/ |
|
|
3720 |
typedef struct sqlite3_vfs ApndVfs; |
|
|
3721 |
typedef struct ApndFile ApndFile; |
|
|
3722 |
|
|
|
3723 |
/* Access to a lower-level VFS that (might) implement dynamic loading, |
|
|
3724 |
** access to randomness, etc. |
|
|
3725 |
*/ |
|
|
3726 |
#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) |
|
|
3727 |
#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1)) |
|
|
3728 |
|
|
|
3729 |
/* An open appendvfs file |
|
|
3730 |
** |
|
|
3731 |
** An instance of this structure describes the appended database file. |
|
|
3732 |
** A separate sqlite3_file object is always appended. The appended |
|
|
3733 |
** sqlite3_file object (which can be accessed using ORIGFILE()) describes |
|
|
3734 |
** the entire file, including the prefix, the database, and the |
|
|
3735 |
** append-mark. |
|
|
3736 |
** |
|
|
3737 |
** The structure of an AppendVFS database is like this: |
|
|
3738 |
** |
|
|
3739 |
** +-------------+---------+----------+-------------+ |
|
|
3740 |
** | prefix-file | padding | database | append-mark | |
|
|
3741 |
** +-------------+---------+----------+-------------+ |
|
|
3742 |
** ^ ^ |
|
|
3743 |
** | | |
|
|
3744 |
** iPgOne iMark |
|
|
3745 |
** |
|
|
3746 |
** |
|
|
3747 |
** "prefix file" - file onto which the database has been appended. |
|
|
3748 |
** "padding" - zero or more bytes inserted so that "database" |
|
|
3749 |
** starts on an APND_ROUNDUP boundary |
|
|
3750 |
** "database" - The SQLite database file |
|
|
3751 |
** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates |
|
|
3752 |
** the offset from the start of prefix-file to the start |
|
|
3753 |
** of "database". |
|
|
3754 |
** |
|
|
3755 |
** The size of the database is iMark - iPgOne. |
|
|
3756 |
** |
|
|
3757 |
** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value |
|
|
3758 |
** of iPgOne stored as a big-ending 64-bit integer. |
|
|
3759 |
** |
|
|
3760 |
** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE). |
|
|
3761 |
** Or, iMark is -1 to indicate that it has not yet been written. |
|
|
3762 |
*/ |
|
|
3763 |
struct ApndFile { |
|
|
3764 |
sqlite3_file base; /* Subclass. MUST BE FIRST! */ |
|
|
3765 |
sqlite3_int64 iPgOne; /* Offset to the start of the database */ |
|
|
3766 |
sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */ |
|
|
3767 |
/* Always followed by another sqlite3_file that describes the whole file */ |
|
|
3768 |
}; |
|
|
3769 |
|
|
|
3770 |
/* |
|
|
3771 |
** Methods for ApndFile |
|
|
3772 |
*/ |
|
|
3773 |
static int apndClose(sqlite3_file*); |
|
|
3774 |
static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); |
|
|
3775 |
static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); |
|
|
3776 |
static int apndTruncate(sqlite3_file*, sqlite3_int64 size); |
|
|
3777 |
static int apndSync(sqlite3_file*, int flags); |
|
|
3778 |
static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize); |
|
|
3779 |
static int apndLock(sqlite3_file*, int); |
|
|
3780 |
static int apndUnlock(sqlite3_file*, int); |
|
|
3781 |
static int apndCheckReservedLock(sqlite3_file*, int *pResOut); |
|
|
3782 |
static int apndFileControl(sqlite3_file*, int op, void *pArg); |
|
|
3783 |
static int apndSectorSize(sqlite3_file*); |
|
|
3784 |
static int apndDeviceCharacteristics(sqlite3_file*); |
|
|
3785 |
static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**); |
|
|
3786 |
static int apndShmLock(sqlite3_file*, int offset, int n, int flags); |
|
|
3787 |
static void apndShmBarrier(sqlite3_file*); |
|
|
3788 |
static int apndShmUnmap(sqlite3_file*, int deleteFlag); |
|
|
3789 |
static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); |
|
|
3790 |
static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p); |
|
|
3791 |
|
|
|
3792 |
/* |
|
|
3793 |
** Methods for ApndVfs |
|
|
3794 |
*/ |
|
|
3795 |
static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); |
|
|
3796 |
static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir); |
|
|
3797 |
static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *); |
|
|
3798 |
static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); |
|
|
3799 |
static void *apndDlOpen(sqlite3_vfs*, const char *zFilename); |
|
|
3800 |
static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg); |
|
|
3801 |
static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void); |
|
|
3802 |
static void apndDlClose(sqlite3_vfs*, void*); |
|
|
3803 |
static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut); |
|
|
3804 |
static int apndSleep(sqlite3_vfs*, int microseconds); |
|
|
3805 |
static int apndCurrentTime(sqlite3_vfs*, double*); |
|
|
3806 |
static int apndGetLastError(sqlite3_vfs*, int, char *); |
|
|
3807 |
static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); |
|
|
3808 |
static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr); |
|
|
3809 |
static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z); |
|
|
3810 |
static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName); |
|
|
3811 |
|
|
|
3812 |
static sqlite3_vfs apnd_vfs = { |
|
|
3813 |
3, /* iVersion (set when registered) */ |
|
|
3814 |
0, /* szOsFile (set when registered) */ |
|
|
3815 |
1024, /* mxPathname */ |
|
|
3816 |
0, /* pNext */ |
|
|
3817 |
"apndvfs", /* zName */ |
|
|
3818 |
0, /* pAppData (set when registered) */ |
|
|
3819 |
apndOpen, /* xOpen */ |
|
|
3820 |
apndDelete, /* xDelete */ |
|
|
3821 |
apndAccess, /* xAccess */ |
|
|
3822 |
apndFullPathname, /* xFullPathname */ |
|
|
3823 |
apndDlOpen, /* xDlOpen */ |
|
|
3824 |
apndDlError, /* xDlError */ |
|
|
3825 |
apndDlSym, /* xDlSym */ |
|
|
3826 |
apndDlClose, /* xDlClose */ |
|
|
3827 |
apndRandomness, /* xRandomness */ |
|
|
3828 |
apndSleep, /* xSleep */ |
|
|
3829 |
apndCurrentTime, /* xCurrentTime */ |
|
|
3830 |
apndGetLastError, /* xGetLastError */ |
|
|
3831 |
apndCurrentTimeInt64, /* xCurrentTimeInt64 */ |
|
|
3832 |
apndSetSystemCall, /* xSetSystemCall */ |
|
|
3833 |
apndGetSystemCall, /* xGetSystemCall */ |
|
|
3834 |
apndNextSystemCall /* xNextSystemCall */ |
|
|
3835 |
}; |
|
|
3836 |
|
|
|
3837 |
static const sqlite3_io_methods apnd_io_methods = { |
|
|
3838 |
3, /* iVersion */ |
|
|
3839 |
apndClose, /* xClose */ |
|
|
3840 |
apndRead, /* xRead */ |
|
|
3841 |
apndWrite, /* xWrite */ |
|
|
3842 |
apndTruncate, /* xTruncate */ |
|
|
3843 |
apndSync, /* xSync */ |
|
|
3844 |
apndFileSize, /* xFileSize */ |
|
|
3845 |
apndLock, /* xLock */ |
|
|
3846 |
apndUnlock, /* xUnlock */ |
|
|
3847 |
apndCheckReservedLock, /* xCheckReservedLock */ |
|
|
3848 |
apndFileControl, /* xFileControl */ |
|
|
3849 |
apndSectorSize, /* xSectorSize */ |
|
|
3850 |
apndDeviceCharacteristics, /* xDeviceCharacteristics */ |
|
|
3851 |
apndShmMap, /* xShmMap */ |
|
|
3852 |
apndShmLock, /* xShmLock */ |
|
|
3853 |
apndShmBarrier, /* xShmBarrier */ |
|
|
3854 |
apndShmUnmap, /* xShmUnmap */ |
|
|
3855 |
apndFetch, /* xFetch */ |
|
|
3856 |
apndUnfetch /* xUnfetch */ |
|
|
3857 |
}; |
|
|
3858 |
|
|
|
3859 |
/* |
|
|
3860 |
** Close an apnd-file. |
|
|
3861 |
*/ |
|
|
3862 |
static int apndClose(sqlite3_file *pFile){ |
|
|
3863 |
pFile = ORIGFILE(pFile); |
|
|
3864 |
return pFile->pMethods->xClose(pFile); |
|
|
3865 |
} |
|
|
3866 |
|
|
|
3867 |
/* |
|
|
3868 |
** Read data from an apnd-file. |
|
|
3869 |
*/ |
|
|
3870 |
static int apndRead( |
|
|
3871 |
sqlite3_file *pFile, |
|
|
3872 |
void *zBuf, |
|
|
3873 |
int iAmt, |
|
|
3874 |
sqlite_int64 iOfst |
|
|
3875 |
){ |
|
|
3876 |
ApndFile *paf = (ApndFile *)pFile; |
|
|
3877 |
pFile = ORIGFILE(pFile); |
|
|
3878 |
return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst); |
|
|
3879 |
} |
|
|
3880 |
|
|
|
3881 |
/* |
|
|
3882 |
** Add the append-mark onto what should become the end of the file. |
|
|
3883 |
* If and only if this succeeds, internal ApndFile.iMark is updated. |
|
|
3884 |
* Parameter iWriteEnd is the appendvfs-relative offset of the new mark. |
|
|
3885 |
*/ |
|
|
3886 |
static int apndWriteMark( |
|
|
3887 |
ApndFile *paf, |
|
|
3888 |
sqlite3_file *pFile, |
|
|
3889 |
sqlite_int64 iWriteEnd |
|
|
3890 |
){ |
|
|
3891 |
sqlite_int64 iPgOne = paf->iPgOne; |
|
|
3892 |
unsigned char a[APND_MARK_SIZE]; |
|
|
3893 |
int i = APND_MARK_FOS_SZ; |
|
|
3894 |
int rc; |
|
|
3895 |
assert(pFile == ORIGFILE(paf)); |
|
|
3896 |
memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ); |
|
|
3897 |
while( --i >= 0 ){ |
|
|
3898 |
a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff); |
|
|
3899 |
iPgOne >>= 8; |
|
|
3900 |
} |
|
|
3901 |
iWriteEnd += paf->iPgOne; |
|
|
3902 |
if( SQLITE_OK==(rc = pFile->pMethods->xWrite |
|
|
3903 |
(pFile, a, APND_MARK_SIZE, iWriteEnd)) ){ |
|
|
3904 |
paf->iMark = iWriteEnd; |
|
|
3905 |
} |
|
|
3906 |
return rc; |
|
|
3907 |
} |
|
|
3908 |
|
|
|
3909 |
/* |
|
|
3910 |
** Write data to an apnd-file. |
|
|
3911 |
*/ |
|
|
3912 |
static int apndWrite( |
|
|
3913 |
sqlite3_file *pFile, |
|
|
3914 |
const void *zBuf, |
|
|
3915 |
int iAmt, |
|
|
3916 |
sqlite_int64 iOfst |
|
|
3917 |
){ |
|
|
3918 |
ApndFile *paf = (ApndFile *)pFile; |
|
|
3919 |
sqlite_int64 iWriteEnd = iOfst + iAmt; |
|
|
3920 |
if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL; |
|
|
3921 |
pFile = ORIGFILE(pFile); |
|
|
3922 |
/* If append-mark is absent or will be overwritten, write it. */ |
|
|
3923 |
if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){ |
|
|
3924 |
int rc = apndWriteMark(paf, pFile, iWriteEnd); |
|
|
3925 |
if( SQLITE_OK!=rc ) return rc; |
|
|
3926 |
} |
|
|
3927 |
return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst); |
|
|
3928 |
} |
|
|
3929 |
|
|
|
3930 |
/* |
|
|
3931 |
** Truncate an apnd-file. |
|
|
3932 |
*/ |
|
|
3933 |
static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
|
|
3934 |
ApndFile *paf = (ApndFile *)pFile; |
|
|
3935 |
pFile = ORIGFILE(pFile); |
|
|
3936 |
/* The append mark goes out first so truncate failure does not lose it. */ |
|
|
3937 |
if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR; |
|
|
3938 |
/* Truncate underlying file just past append mark */ |
|
|
3939 |
return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE); |
|
|
3940 |
} |
|
|
3941 |
|
|
|
3942 |
/* |
|
|
3943 |
** Sync an apnd-file. |
|
|
3944 |
*/ |
|
|
3945 |
static int apndSync(sqlite3_file *pFile, int flags){ |
|
|
3946 |
pFile = ORIGFILE(pFile); |
|
|
3947 |
return pFile->pMethods->xSync(pFile, flags); |
|
|
3948 |
} |
|
|
3949 |
|
|
|
3950 |
/* |
|
|
3951 |
** Return the current file-size of an apnd-file. |
|
|
3952 |
** If the append mark is not yet there, the file-size is 0. |
|
|
3953 |
*/ |
|
|
3954 |
static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
|
|
3955 |
ApndFile *paf = (ApndFile *)pFile; |
|
|
3956 |
*pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0; |
|
|
3957 |
return SQLITE_OK; |
|
|
3958 |
} |
|
|
3959 |
|
|
|
3960 |
/* |
|
|
3961 |
** Lock an apnd-file. |
|
|
3962 |
*/ |
|
|
3963 |
static int apndLock(sqlite3_file *pFile, int eLock){ |
|
|
3964 |
pFile = ORIGFILE(pFile); |
|
|
3965 |
return pFile->pMethods->xLock(pFile, eLock); |
|
|
3966 |
} |
|
|
3967 |
|
|
|
3968 |
/* |
|
|
3969 |
** Unlock an apnd-file. |
|
|
3970 |
*/ |
|
|
3971 |
static int apndUnlock(sqlite3_file *pFile, int eLock){ |
|
|
3972 |
pFile = ORIGFILE(pFile); |
|
|
3973 |
return pFile->pMethods->xUnlock(pFile, eLock); |
|
|
3974 |
} |
|
|
3975 |
|
|
|
3976 |
/* |
|
|
3977 |
** Check if another file-handle holds a RESERVED lock on an apnd-file. |
|
|
3978 |
*/ |
|
|
3979 |
static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){ |
|
|
3980 |
pFile = ORIGFILE(pFile); |
|
|
3981 |
return pFile->pMethods->xCheckReservedLock(pFile, pResOut); |
|
|
3982 |
} |
|
|
3983 |
|
|
|
3984 |
/* |
|
|
3985 |
** File control method. For custom operations on an apnd-file. |
|
|
3986 |
*/ |
|
|
3987 |
static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){ |
|
|
3988 |
ApndFile *paf = (ApndFile *)pFile; |
|
|
3989 |
int rc; |
|
|
3990 |
pFile = ORIGFILE(pFile); |
|
|
3991 |
if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne; |
|
|
3992 |
rc = pFile->pMethods->xFileControl(pFile, op, pArg); |
|
|
3993 |
if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){ |
|
|
3994 |
*(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg); |
|
|
3995 |
} |
|
|
3996 |
return rc; |
|
|
3997 |
} |
|
|
3998 |
|
|
|
3999 |
/* |
|
|
4000 |
** Return the sector-size in bytes for an apnd-file. |
|
|
4001 |
*/ |
|
|
4002 |
static int apndSectorSize(sqlite3_file *pFile){ |
|
|
4003 |
pFile = ORIGFILE(pFile); |
|
|
4004 |
return pFile->pMethods->xSectorSize(pFile); |
|
|
4005 |
} |
|
|
4006 |
|
|
|
4007 |
/* |
|
|
4008 |
** Return the device characteristic flags supported by an apnd-file. |
|
|
4009 |
*/ |
|
|
4010 |
static int apndDeviceCharacteristics(sqlite3_file *pFile){ |
|
|
4011 |
pFile = ORIGFILE(pFile); |
|
|
4012 |
return pFile->pMethods->xDeviceCharacteristics(pFile); |
|
|
4013 |
} |
|
|
4014 |
|
|
|
4015 |
/* Create a shared memory file mapping */ |
|
|
4016 |
static int apndShmMap( |
|
|
4017 |
sqlite3_file *pFile, |
|
|
4018 |
int iPg, |
|
|
4019 |
int pgsz, |
|
|
4020 |
int bExtend, |
|
|
4021 |
void volatile **pp |
|
|
4022 |
){ |
|
|
4023 |
pFile = ORIGFILE(pFile); |
|
|
4024 |
return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp); |
|
|
4025 |
} |
|
|
4026 |
|
|
|
4027 |
/* Perform locking on a shared-memory segment */ |
|
|
4028 |
static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){ |
|
|
4029 |
pFile = ORIGFILE(pFile); |
|
|
4030 |
return pFile->pMethods->xShmLock(pFile,offset,n,flags); |
|
|
4031 |
} |
|
|
4032 |
|
|
|
4033 |
/* Memory barrier operation on shared memory */ |
|
|
4034 |
static void apndShmBarrier(sqlite3_file *pFile){ |
|
|
4035 |
pFile = ORIGFILE(pFile); |
|
|
4036 |
pFile->pMethods->xShmBarrier(pFile); |
|
|
4037 |
} |
|
|
4038 |
|
|
|
4039 |
/* Unmap a shared memory segment */ |
|
|
4040 |
static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){ |
|
|
4041 |
pFile = ORIGFILE(pFile); |
|
|
4042 |
return pFile->pMethods->xShmUnmap(pFile,deleteFlag); |
|
|
4043 |
} |
|
|
4044 |
|
|
|
4045 |
/* Fetch a page of a memory-mapped file */ |
|
|
4046 |
static int apndFetch( |
|
|
4047 |
sqlite3_file *pFile, |
|
|
4048 |
sqlite3_int64 iOfst, |
|
|
4049 |
int iAmt, |
|
|
4050 |
void **pp |
|
|
4051 |
){ |
|
|
4052 |
ApndFile *p = (ApndFile *)pFile; |
|
|
4053 |
if( p->iMark < 0 || iOfst+iAmt > p->iMark ){ |
|
|
4054 |
return SQLITE_IOERR; /* Cannot read what is not yet there. */ |
|
|
4055 |
} |
|
|
4056 |
pFile = ORIGFILE(pFile); |
|
|
4057 |
return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp); |
|
|
4058 |
} |
|
|
4059 |
|
|
|
4060 |
/* Release a memory-mapped page */ |
|
|
4061 |
static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ |
|
|
4062 |
ApndFile *p = (ApndFile *)pFile; |
|
|
4063 |
pFile = ORIGFILE(pFile); |
|
|
4064 |
return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage); |
|
|
4065 |
} |
|
|
4066 |
|
|
|
4067 |
/* |
|
|
4068 |
** Try to read the append-mark off the end of a file. Return the |
|
|
4069 |
** start of the appended database if the append-mark is present. |
|
|
4070 |
** If there is no valid append-mark, return -1; |
|
|
4071 |
** |
|
|
4072 |
** An append-mark is only valid if the NNNNNNNN start-of-database offset |
|
|
4073 |
** indicates that the appended database contains at least one page. The |
|
|
4074 |
** start-of-database value must be a multiple of 512. |
|
|
4075 |
*/ |
|
|
4076 |
static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){ |
|
|
4077 |
int rc, i; |
|
|
4078 |
sqlite3_int64 iMark; |
|
|
4079 |
int msbs = 8 * (APND_MARK_FOS_SZ-1); |
|
|
4080 |
unsigned char a[APND_MARK_SIZE]; |
|
|
4081 |
|
|
|
4082 |
if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1; |
|
|
4083 |
rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE); |
|
|
4084 |
if( rc ) return -1; |
|
|
4085 |
if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1; |
|
|
4086 |
iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs; |
|
|
4087 |
for(i=1; i<8; i++){ |
|
|
4088 |
msbs -= 8; |
|
|
4089 |
iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]< |
|
|
4090 |
} |
|
|
4091 |
if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1; |
|
|
4092 |
if( iMark & 0x1ff ) return -1; |
|
|
4093 |
return iMark; |
|
|
4094 |
} |
|
|
4095 |
|
|
|
4096 |
static const char apvfsSqliteHdr[] = "SQLite format 3"; |
|
|
4097 |
/* |
|
|
4098 |
** Check to see if the file is an appendvfs SQLite database file. |
|
|
4099 |
** Return true iff it is such. Parameter sz is the file's size. |
|
|
4100 |
*/ |
|
|
4101 |
static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){ |
|
|
4102 |
int rc; |
|
|
4103 |
char zHdr[16]; |
|
|
4104 |
sqlite3_int64 iMark = apndReadMark(sz, pFile); |
|
|
4105 |
if( iMark>=0 ){ |
|
|
4106 |
/* If file has the correct end-marker, the expected odd size, and the |
|
|
4107 |
** SQLite DB type marker where the end-marker puts it, then it |
|
|
4108 |
** is an appendvfs database. |
|
|
4109 |
*/ |
|
|
4110 |
rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark); |
|
|
4111 |
if( SQLITE_OK==rc |
|
|
4112 |
&& memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0 |
|
|
4113 |
&& (sz & 0x1ff) == APND_MARK_SIZE |
|
|
4114 |
&& sz>=512+APND_MARK_SIZE |
|
|
4115 |
){ |
|
|
4116 |
return 1; /* It's an appendvfs database */ |
|
|
4117 |
} |
|
|
4118 |
} |
|
|
4119 |
return 0; |
|
|
4120 |
} |
|
|
4121 |
|
|
|
4122 |
/* |
|
|
4123 |
** Check to see if the file is an ordinary SQLite database file. |
|
|
4124 |
** Return true iff so. Parameter sz is the file's size. |
|
|
4125 |
*/ |
|
|
4126 |
static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){ |
|
|
4127 |
char zHdr[16]; |
|
|
4128 |
if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */ |
|
|
4129 |
|| (sz & 0x1ff) != 0 |
|
|
4130 |
|| SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0) |
|
|
4131 |
|| memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0 |
|
|
4132 |
){ |
|
|
4133 |
return 0; |
|
|
4134 |
}else{ |
|
|
4135 |
return 1; |
|
|
4136 |
} |
|
|
4137 |
} |
|
|
4138 |
|
|
|
4139 |
/* |
|
|
4140 |
** Open an apnd file handle. |
|
|
4141 |
*/ |
|
|
4142 |
static int apndOpen( |
|
|
4143 |
sqlite3_vfs *pApndVfs, |
|
|
4144 |
const char *zName, |
|
|
4145 |
sqlite3_file *pFile, |
|
|
4146 |
int flags, |
|
|
4147 |
int *pOutFlags |
|
|
4148 |
){ |
|
|
4149 |
ApndFile *pApndFile = (ApndFile*)pFile; |
|
|
4150 |
sqlite3_file *pBaseFile = ORIGFILE(pFile); |
|
|
4151 |
sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs); |
|
|
4152 |
int rc; |
|
|
4153 |
sqlite3_int64 sz = 0; |
|
|
4154 |
if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){ |
|
|
4155 |
/* The appendvfs is not to be used for transient or temporary databases. |
|
|
4156 |
** Just use the base VFS open to initialize the given file object and |
|
|
4157 |
** open the underlying file. (Appendvfs is then unused for this file.) |
|
|
4158 |
*/ |
|
|
4159 |
return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags); |
|
|
4160 |
} |
|
|
4161 |
memset(pApndFile, 0, sizeof(ApndFile)); |
|
|
4162 |
pFile->pMethods = &apnd_io_methods; |
|
|
4163 |
pApndFile->iMark = -1; /* Append mark not yet written */ |
|
|
4164 |
|
|
|
4165 |
rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags); |
|
|
4166 |
if( rc==SQLITE_OK ){ |
|
|
4167 |
rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz); |
|
|
4168 |
} |
|
|
4169 |
if( rc ){ |
|
|
4170 |
pBaseFile->pMethods->xClose(pBaseFile); |
|
|
4171 |
pFile->pMethods = 0; |
|
|
4172 |
return rc; |
|
|
4173 |
} |
|
|
4174 |
if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){ |
|
|
4175 |
/* The file being opened appears to be just an ordinary DB. Copy |
|
|
4176 |
** the base dispatch-table so this instance mimics the base VFS. |
|
|
4177 |
*/ |
|
|
4178 |
memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile); |
|
|
4179 |
return SQLITE_OK; |
|
|
4180 |
} |
|
|
4181 |
pApndFile->iPgOne = apndReadMark(sz, pFile); |
|
|
4182 |
if( pApndFile->iPgOne>=0 ){ |
|
|
4183 |
pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */ |
|
|
4184 |
return SQLITE_OK; |
|
|
4185 |
} |
|
|
4186 |
if( (flags & SQLITE_OPEN_CREATE)==0 ){ |
|
|
4187 |
pBaseFile->pMethods->xClose(pBaseFile); |
|
|
4188 |
rc = SQLITE_CANTOPEN; |
|
|
4189 |
pFile->pMethods = 0; |
|
|
4190 |
}else{ |
|
|
4191 |
/* Round newly added appendvfs location to #define'd page boundary. |
|
|
4192 |
** Note that nothing has yet been written to the underlying file. |
|
|
4193 |
** The append mark will be written along with first content write. |
|
|
4194 |
** Until then, paf->iMark value indicates it is not yet written. |
|
|
4195 |
*/ |
|
|
4196 |
pApndFile->iPgOne = APND_START_ROUNDUP(sz); |
|
|
4197 |
} |
|
|
4198 |
return rc; |
|
|
4199 |
} |
|
|
4200 |
|
|
|
4201 |
/* |
|
|
4202 |
** Delete an apnd file. |
|
|
4203 |
** For an appendvfs, this could mean delete the appendvfs portion, |
|
|
4204 |
** leaving the appendee as it was before it gained an appendvfs. |
|
|
4205 |
** For now, this code deletes the underlying file too. |
|
|
4206 |
*/ |
|
|
4207 |
static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
|
|
4208 |
return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync); |
|
|
4209 |
} |
|
|
4210 |
|
|
|
4211 |
/* |
|
|
4212 |
** All other VFS methods are pass-thrus. |
|
|
4213 |
*/ |
|
|
4214 |
static int apndAccess( |
|
|
4215 |
sqlite3_vfs *pVfs, |
|
|
4216 |
const char *zPath, |
|
|
4217 |
int flags, |
|
|
4218 |
int *pResOut |
|
|
4219 |
){ |
|
|
4220 |
return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut); |
|
|
4221 |
} |
|
|
4222 |
static int apndFullPathname( |
|
|
4223 |
sqlite3_vfs *pVfs, |
|
|
4224 |
const char *zPath, |
|
|
4225 |
int nOut, |
|
|
4226 |
char *zOut |
|
|
4227 |
){ |
|
|
4228 |
return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut); |
|
|
4229 |
} |
|
|
4230 |
static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
|
|
4231 |
return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); |
|
|
4232 |
} |
|
|
4233 |
static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ |
|
|
4234 |
ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); |
|
|
4235 |
} |
|
|
4236 |
static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ |
|
|
4237 |
return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); |
|
|
4238 |
} |
|
|
4239 |
static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
|
|
4240 |
ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); |
|
|
4241 |
} |
|
|
4242 |
static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
|
|
4243 |
return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); |
|
|
4244 |
} |
|
|
4245 |
static int apndSleep(sqlite3_vfs *pVfs, int nMicro){ |
|
|
4246 |
return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); |
|
|
4247 |
} |
|
|
4248 |
static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ |
|
|
4249 |
return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); |
|
|
4250 |
} |
|
|
4251 |
static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){ |
|
|
4252 |
return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); |
|
|
4253 |
} |
|
|
4254 |
static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){ |
|
|
4255 |
return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); |
|
|
4256 |
} |
|
|
4257 |
static int apndSetSystemCall( |
|
|
4258 |
sqlite3_vfs *pVfs, |
|
|
4259 |
const char *zName, |
|
|
4260 |
sqlite3_syscall_ptr pCall |
|
|
4261 |
){ |
|
|
4262 |
return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall); |
|
|
4263 |
} |
|
|
4264 |
static sqlite3_syscall_ptr apndGetSystemCall( |
|
|
4265 |
sqlite3_vfs *pVfs, |
|
|
4266 |
const char *zName |
|
|
4267 |
){ |
|
|
4268 |
return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName); |
|
|
4269 |
} |
|
|
4270 |
static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){ |
|
|
4271 |
return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName); |
|
|
4272 |
} |
|
|
4273 |
|
|
|
4274 |
|
|
|
4275 |
#ifdef _WIN32 |
|
|
4276 |
|
|
|
4277 |
#endif |
|
|
4278 |
/* |
|
|
4279 |
** This routine is called when the extension is loaded. |
|
|
4280 |
** Register the new VFS. |
|
|
4281 |
*/ |
|
|
4282 |
int sqlite3_appendvfs_init( |
|
|
4283 |
sqlite3 *db, |
|
|
4284 |
char **pzErrMsg, |
|
|
4285 |
const sqlite3_api_routines *pApi |
|
|
4286 |
){ |
|
|
4287 |
int rc = SQLITE_OK; |
|
|
4288 |
sqlite3_vfs *pOrig; |
|
|
4289 |
SQLITE_EXTENSION_INIT2(pApi); |
|
|
4290 |
(void)pzErrMsg; |
|
|
4291 |
(void)db; |
|
|
4292 |
pOrig = sqlite3_vfs_find(0); |
|
|
4293 |
apnd_vfs.iVersion = pOrig->iVersion; |
|
|
4294 |
apnd_vfs.pAppData = pOrig; |
|
|
4295 |
apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile); |
|
|
4296 |
rc = sqlite3_vfs_register(&apnd_vfs, 0); |
|
|
4297 |
#ifdef APPENDVFS_TEST |
|
|
4298 |
if( rc==SQLITE_OK ){ |
|
|
4299 |
rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister); |
|
|
4300 |
} |
|
|
4301 |
#endif |
|
|
4302 |
if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY; |
|
|
4303 |
return rc; |
|
|
4304 |
} |
|
|
4305 |
|
|
|
4306 |
/************************* End ../ext/misc/appendvfs.c ********************/ |
|
|
4307 |
/************************* Begin ../ext/misc/memtrace.c ******************/ |
|
|
4308 |
/* |
|
|
4309 |
** 2019-01-21 |
|
|
4310 |
** |
|
|
4311 |
** The author disclaims copyright to this source code. In place of |
|
|
4312 |
** a legal notice, here is a blessing: |
|
|
4313 |
** |
|
|
4314 |
** May you do good and not evil. |
|
|
4315 |
** May you find forgiveness for yourself and forgive others. |
|
|
4316 |
** May you share freely, never taking more than you give. |
|
|
4317 |
** |
|
|
4318 |
************************************************************************* |
|
|
4319 |
** |
|
|
4320 |
** This file implements an extension that uses the SQLITE_CONFIG_MALLOC |
|
|
4321 |
** mechanism to add a tracing layer on top of SQLite. If this extension |
|
|
4322 |
** is registered prior to sqlite3_initialize(), it will cause all memory |
|
|
4323 |
** allocation activities to be logged on standard output, or to some other |
|
|
4324 |
** FILE specified by the initializer. |
|
|
4325 |
** |
|
|
4326 |
** This file needs to be compiled into the application that uses it. |
|
|
4327 |
** |
|
|
4328 |
** This extension is used to implement the --memtrace option of the |
|
|
4329 |
** command-line shell. |
|
|
4330 |
*/ |
|
|
4331 |
#include |
|
|
4332 |
#include |
|
|
4333 |
#include |
|
|
4334 |
|
|
|
4335 |
/* The original memory allocation routines */ |
|
|
4336 |
static sqlite3_mem_methods memtraceBase; |
|
|
4337 |
static FILE *memtraceOut; |
|
|
4338 |
|
|
|
4339 |
/* Methods that trace memory allocations */ |
|
|
4340 |
static void *memtraceMalloc(int n){ |
|
|
4341 |
if( memtraceOut ){ |
|
|
4342 |
fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", |
|
|
4343 |
memtraceBase.xRoundup(n)); |
|
|
4344 |
} |
|
|
4345 |
return memtraceBase.xMalloc(n); |
|
|
4346 |
} |
|
|
4347 |
static void memtraceFree(void *p){ |
|
|
4348 |
if( p==0 ) return; |
|
|
4349 |
if( memtraceOut ){ |
|
|
4350 |
fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p)); |
|
|
4351 |
} |
|
|
4352 |
memtraceBase.xFree(p); |
|
|
4353 |
} |
|
|
4354 |
static void *memtraceRealloc(void *p, int n){ |
|
|
4355 |
if( p==0 ) return memtraceMalloc(n); |
|
|
4356 |
if( n==0 ){ |
|
|
4357 |
memtraceFree(p); |
|
|
4358 |
return 0; |
|
|
4359 |
} |
|
|
4360 |
if( memtraceOut ){ |
|
|
4361 |
fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n", |
|
|
4362 |
memtraceBase.xSize(p), memtraceBase.xRoundup(n)); |
|
|
4363 |
} |
|
|
4364 |
return memtraceBase.xRealloc(p, n); |
|
|
4365 |
} |
|
|
4366 |
static int memtraceSize(void *p){ |
|
|
4367 |
return memtraceBase.xSize(p); |
|
|
4368 |
} |
|
|
4369 |
static int memtraceRoundup(int n){ |
|
|
4370 |
return memtraceBase.xRoundup(n); |
|
|
4371 |
} |
|
|
4372 |
static int memtraceInit(void *p){ |
|
|
4373 |
return memtraceBase.xInit(p); |
|
|
4374 |
} |
|
|
4375 |
static void memtraceShutdown(void *p){ |
|
|
4376 |
memtraceBase.xShutdown(p); |
|
|
4377 |
} |
|
|
4378 |
|
|
|
4379 |
/* The substitute memory allocator */ |
|
|
4380 |
static sqlite3_mem_methods ersaztMethods = { |
|
|
4381 |
memtraceMalloc, |
|
|
4382 |
memtraceFree, |
|
|
4383 |
memtraceRealloc, |
|
|
4384 |
memtraceSize, |
|
|
4385 |
memtraceRoundup, |
|
|
4386 |
memtraceInit, |
|
|
4387 |
memtraceShutdown, |
|
|
4388 |
|
|
|
4389 |
}; |
|
|
4390 |
|
|
|
4391 |
/* Begin tracing memory allocations to out. */ |
|
|
4392 |
int sqlite3MemTraceActivate(FILE *out){ |
|
|
4393 |
int rc = SQLITE_OK; |
|
|
4394 |
if( memtraceBase.xMalloc==0 ){ |
|
|
4395 |
rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase); |
|
|
4396 |
if( rc==SQLITE_OK ){ |
|
|
4397 |
rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods); |
|
|
4398 |
} |
|
|
4399 |
} |
|
|
4400 |
memtraceOut = out; |
|
|
4401 |
return rc; |
|
|
4402 |
} |
|
|
4403 |
|
|
|
4404 |
/* Deactivate memory tracing */ |
|
|
4405 |
int sqlite3MemTraceDeactivate(void){ |
|
|
4406 |
int rc = SQLITE_OK; |
|
|
4407 |
if( memtraceBase.xMalloc!=0 ){ |
|
|
4408 |
rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase); |
|
|
4409 |
if( rc==SQLITE_OK ){ |
|
|
4410 |
memset(&memtraceBase, 0, sizeof(memtraceBase)); |
|
|
4411 |
} |
|
|
4412 |
} |
|
|
4413 |
memtraceOut = 0; |
|
|
4414 |
return rc; |
|
|
4415 |
} |
|
|
4416 |
|
|
|
4417 |
/************************* End ../ext/misc/memtrace.c ********************/ |
|
|
4418 |
/************************* Begin ../ext/misc/uint.c ******************/ |
|
|
4419 |
/* |
|
|
4420 |
** 2020-04-14 |
|
|
4421 |
** |
|
|
4422 |
** The author disclaims copyright to this source code. In place of |
|
|
4423 |
** a legal notice, here is a blessing: |
|
|
4424 |
** |
|
|
4425 |
** May you do good and not evil. |
|
|
4426 |
** May you find forgiveness for yourself and forgive others. |
|
|
4427 |
** May you share freely, never taking more than you give. |
|
|
4428 |
** |
|
|
4429 |
****************************************************************************** |
|
|
4430 |
** |
|
|
4431 |
** This SQLite extension implements the UINT collating sequence. |
|
|
4432 |
** |
|
|
4433 |
** UINT works like BINARY for text, except that embedded strings |
|
|
4434 |
** of digits compare in numeric order. |
|
|
4435 |
** |
|
|
4436 |
** * Leading zeros are handled properly, in the sense that |
|
|
4437 |
** they do not mess of the maginitude comparison of embedded |
|
|
4438 |
** strings of digits. "x00123y" is equal to "x123y". |
|
|
4439 |
** |
|
|
4440 |
** * Only unsigned integers are recognized. Plus and minus |
|
|
4441 |
** signs are ignored. Decimal points and exponential notation |
|
|
4442 |
** are ignored. |
|
|
4443 |
** |
|
|
4444 |
** * Embedded integers can be of arbitrary length. Comparison |
|
|
4445 |
** is *not* limited integers that can be expressed as a |
|
|
4446 |
** 64-bit machine integer. |
|
|
4447 |
*/ |
|
|
4448 |
/* #include "sqlite3ext.h" */ |
|
|
4449 |
SQLITE_EXTENSION_INIT1 |
|
|
4450 |
#include |
|
|
4451 |
#include |
|
|
4452 |
#include |
|
|
4453 |
|
|
|
4454 |
/* |
|
|
4455 |
** Compare text in lexicographic order, except strings of digits |
|
|
4456 |
** compare in numeric order. |
|
|
4457 |
*/ |
|
|
4458 |
static int uintCollFunc( |
|
|
4459 |
void *notUsed, |
|
|
4460 |
int nKey1, const void *pKey1, |
|
|
4461 |
int nKey2, const void *pKey2 |
|
|
4462 |
){ |
|
|
4463 |
const unsigned char *zA = (const unsigned char*)pKey1; |
|
|
4464 |
const unsigned char *zB = (const unsigned char*)pKey2; |
|
|
4465 |
int i=0, j=0, x; |
|
|
4466 |
(void)notUsed; |
|
|
4467 |
while( i |
|
|
4468 |
x = zA[i] - zB[j]; |
|
|
4469 |
if( isdigit(zA[i]) ){ |
|
|
4470 |
int k; |
|
|
4471 |
if( !isdigit(zB[j]) ) return x; |
|
|
4472 |
while( i |
|
|
4473 |
while( j |
|
|
4474 |
k = 0; |
|
|
4475 |
while( i+k |
|
|
4476 |
&& j+k |
|
|
4477 |
k++; |
|
|
4478 |
} |
|
|
4479 |
if( i+k |
|
|
4480 |
return +1; |
|
|
4481 |
}else if( j+k |
|
|
4482 |
return -1; |
|
|
4483 |
}else{ |
|
|
4484 |
x = memcmp(zA+i, zB+j, k); |
|
|
4485 |
if( x ) return x; |
|
|
4486 |
i += k; |
|
|
4487 |
j += k; |
|
|
4488 |
} |
|
|
4489 |
}else if( x ){ |
|
|
4490 |
return x; |
|
|
4491 |
}else{ |
|
|
4492 |
i++; |
|
|
4493 |
j++; |
|
|
4494 |
} |
|
|
4495 |
} |
|
|
4496 |
return (nKey1 - i) - (nKey2 - j); |
|
|
4497 |
} |
|
|
4498 |
|
|
|
4499 |
#ifdef _WIN32 |
|
|
4500 |
|
|
|
4501 |
#endif |
|
|
4502 |
int sqlite3_uint_init( |
|
|
4503 |
sqlite3 *db, |
|
|
4504 |
char **pzErrMsg, |
|
|
4505 |
const sqlite3_api_routines *pApi |
|
|
4506 |
){ |
|
|
4507 |
SQLITE_EXTENSION_INIT2(pApi); |
|
|
4508 |
(void)pzErrMsg; /* Unused parameter */ |
|
|
4509 |
return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc); |
|
|
4510 |
} |
|
|
4511 |
|
|
|
4512 |
/************************* End ../ext/misc/uint.c ********************/ |
|
|
4513 |
/************************* Begin ../ext/misc/decimal.c ******************/ |
|
|
4514 |
/* |
|
|
4515 |
** 2020-06-22 |
|
|
4516 |
** |
|
|
4517 |
** The author disclaims copyright to this source code. In place of |
|
|
4518 |
** a legal notice, here is a blessing: |
|
|
4519 |
** |
|
|
4520 |
** May you do good and not evil. |
|
|
4521 |
** May you find forgiveness for yourself and forgive others. |
|
|
4522 |
** May you share freely, never taking more than you give. |
|
|
4523 |
** |
|
|
4524 |
****************************************************************************** |
|
|
4525 |
** |
|
|
4526 |
** Routines to implement arbitrary-precision decimal math. |
|
|
4527 |
** |
|
|
4528 |
** The focus here is on simplicity and correctness, not performance. |
|
|
4529 |
*/ |
|
|
4530 |
/* #include "sqlite3ext.h" */ |
|
|
4531 |
SQLITE_EXTENSION_INIT1 |
|
|
4532 |
#include |
|
|
4533 |
#include |
|
|
4534 |
#include |
|
|
4535 |
#include |
|
|
4536 |
|
|
|
4537 |
/* Mark a function parameter as unused, to suppress nuisance compiler |
|
|
4538 |
** warnings. */ |
|
|
4539 |
#ifndef UNUSED_PARAMETER |
|
|
4540 |
# define UNUSED_PARAMETER(X) (void)(X) |
|
|
4541 |
#endif |
|
|
4542 |
|
|
|
4543 |
|
|
|
4544 |
/* A decimal object */ |
|
|
4545 |
typedef struct Decimal Decimal; |
|
|
4546 |
struct Decimal { |
|
|
4547 |
char sign; /* 0 for positive, 1 for negative */ |
|
|
4548 |
char oom; /* True if an OOM is encountered */ |
|
|
4549 |
char isNull; /* True if holds a NULL rather than a number */ |
|
|
4550 |
char isInit; /* True upon initialization */ |
|
|
4551 |
int nDigit; /* Total number of digits */ |
|
|
4552 |
int nFrac; /* Number of digits to the right of the decimal point */ |
|
|
4553 |
signed char *a; /* Array of digits. Most significant first. */ |
|
|
4554 |
}; |
|
|
4555 |
|
|
|
4556 |
/* |
|
|
4557 |
** Release memory held by a Decimal, but do not free the object itself. |
|
|
4558 |
*/ |
|
|
4559 |
static void decimal_clear(Decimal *p){ |
|
|
4560 |
sqlite3_free(p->a); |
|
|
4561 |
} |
|
|
4562 |
|
|
|
4563 |
/* |
|
|
4564 |
** Destroy a Decimal object |
|
|
4565 |
*/ |
|
|
4566 |
static void decimal_free(Decimal *p){ |
|
|
4567 |
if( p ){ |
|
|
4568 |
decimal_clear(p); |
|
|
4569 |
sqlite3_free(p); |
|
|
4570 |
} |
|
|
4571 |
} |
|
|
4572 |
|
|
|
4573 |
/* |
|
|
4574 |
** Allocate a new Decimal object. Initialize it to the number given |
|
|
4575 |
** by the input string. |
|
|
4576 |
*/ |
|
|
4577 |
static Decimal *decimal_new( |
|
|
4578 |
sqlite3_context *pCtx, |
|
|
4579 |
sqlite3_value *pIn, |
|
|
4580 |
int nAlt, |
|
|
4581 |
const unsigned char *zAlt |
|
|
4582 |
){ |
|
|
4583 |
Decimal *p; |
|
|
4584 |
int n, i; |
|
|
4585 |
const unsigned char *zIn; |
|
|
4586 |
int iExp = 0; |
|
|
4587 |
p = sqlite3_malloc( sizeof(*p) ); |
|
|
4588 |
if( p==0 ) goto new_no_mem; |
|
|
4589 |
p->sign = 0; |
|
|
4590 |
p->oom = 0; |
|
|
4591 |
p->isInit = 1; |
|
|
4592 |
p->isNull = 0; |
|
|
4593 |
p->nDigit = 0; |
|
|
4594 |
p->nFrac = 0; |
|
|
4595 |
if( zAlt ){ |
|
|
4596 |
n = nAlt, |
|
|
4597 |
zIn = zAlt; |
|
|
4598 |
}else{ |
|
|
4599 |
if( sqlite3_value_type(pIn)==SQLITE_NULL ){ |
|
|
4600 |
p->a = 0; |
|
|
4601 |
p->isNull = 1; |
|
|
4602 |
return p; |
|
|
4603 |
} |
|
|
4604 |
n = sqlite3_value_bytes(pIn); |
|
|
4605 |
zIn = sqlite3_value_text(pIn); |
|
|
4606 |
} |
|
|
4607 |
p->a = sqlite3_malloc64( n+1 ); |
|
|
4608 |
if( p->a==0 ) goto new_no_mem; |
|
|
4609 |
for(i=0; isspace(zIn[i]); i++){} |
|
|
4610 |
if( zIn[i]=='-' ){ |
|
|
4611 |
p->sign = 1; |
|
|
4612 |
i++; |
|
|
4613 |
}else if( zIn[i]=='+' ){ |
|
|
4614 |
i++; |
|
|
4615 |
} |
|
|
4616 |
while( i |
|
|
4617 |
while( i |
|
|
4618 |
char c = zIn[i]; |
|
|
4619 |
if( c>='0' && c<='9' ){ |
|
|
4620 |
p->a[p->nDigit++] = c - '0'; |
|
|
4621 |
}else if( c=='.' ){ |
|
|
4622 |
p->nFrac = p->nDigit + 1; |
|
|
4623 |
}else if( c=='e' || c=='E' ){ |
|
|
4624 |
int j = i+1; |
|
|
4625 |
int neg = 0; |
|
|
4626 |
if( j>=n ) break; |
|
|
4627 |
if( zIn[j]=='-' ){ |
|
|
4628 |
neg = 1; |
|
|
4629 |
j++; |
|
|
4630 |
}else if( zIn[j]=='+' ){ |
|
|
4631 |
j++; |
|
|
4632 |
} |
|
|
4633 |
while( j |
|
|
4634 |
if( zIn[j]>='0' && zIn[j]<='9' ){ |
|
|
4635 |
iExp = iExp*10 + zIn[j] - '0'; |
|
|
4636 |
} |
|
|
4637 |
j++; |
|
|
4638 |
} |
|
|
4639 |
if( neg ) iExp = -iExp; |
|
|
4640 |
break; |
|
|
4641 |
} |
|
|
4642 |
i++; |
|
|
4643 |
} |
|
|
4644 |
if( p->nFrac ){ |
|
|
4645 |
p->nFrac = p->nDigit - (p->nFrac - 1); |
|
|
4646 |
} |
|
|
4647 |
if( iExp>0 ){ |
|
|
4648 |
if( p->nFrac>0 ){ |
|
|
4649 |
if( iExp<=p->nFrac ){ |
|
|
4650 |
p->nFrac -= iExp; |
|
|
4651 |
iExp = 0; |
|
|
4652 |
}else{ |
|
|
4653 |
iExp -= p->nFrac; |
|
|
4654 |
p->nFrac = 0; |
|
|
4655 |
} |
|
|
4656 |
} |
|
|
4657 |
if( iExp>0 ){ |
|
|
4658 |
p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 ); |
|
|
4659 |
if( p->a==0 ) goto new_no_mem; |
|
|
4660 |
memset(p->a+p->nDigit, 0, iExp); |
|
|
4661 |
p->nDigit += iExp; |
|
|
4662 |
} |
|
|
4663 |
}else if( iExp<0 ){ |
|
|
4664 |
int nExtra; |
|
|
4665 |
iExp = -iExp; |
|
|
4666 |
nExtra = p->nDigit - p->nFrac - 1; |
|
|
4667 |
if( nExtra ){ |
|
|
4668 |
if( nExtra>=iExp ){ |
|
|
4669 |
p->nFrac += iExp; |
|
|
4670 |
iExp = 0; |
|
|
4671 |
}else{ |
|
|
4672 |
iExp -= nExtra; |
|
|
4673 |
p->nFrac = p->nDigit - 1; |
|
|
4674 |
} |
|
|
4675 |
} |
|
|
4676 |
if( iExp>0 ){ |
|
|
4677 |
p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 ); |
|
|
4678 |
if( p->a==0 ) goto new_no_mem; |
|
|
4679 |
memmove(p->a+iExp, p->a, p->nDigit); |
|
|
4680 |
memset(p->a, 0, iExp); |
|
|
4681 |
p->nDigit += iExp; |
|
|
4682 |
p->nFrac += iExp; |
|
|
4683 |
} |
|
|
4684 |
} |
|
|
4685 |
return p; |
|
|
4686 |
|
|
|
4687 |
new_no_mem: |
|
|
4688 |
if( pCtx ) sqlite3_result_error_nomem(pCtx); |
|
|
4689 |
sqlite3_free(p); |
|
|
4690 |
return 0; |
|
|
4691 |
} |
|
|
4692 |
|
|
|
4693 |
/* |
|
|
4694 |
** Make the given Decimal the result. |
|
|
4695 |
*/ |
|
|
4696 |
static void decimal_result(sqlite3_context *pCtx, Decimal *p){ |
|
|
4697 |
char *z; |
|
|
4698 |
int i, j; |
|
|
4699 |
int n; |
|
|
4700 |
if( p==0 || p->oom ){ |
|
|
4701 |
sqlite3_result_error_nomem(pCtx); |
|
|
4702 |
return; |
|
|
4703 |
} |
|
|
4704 |
if( p->isNull ){ |
|
|
4705 |
sqlite3_result_null(pCtx); |
|
|
4706 |
return; |
|
|
4707 |
} |
|
|
4708 |
z = sqlite3_malloc( p->nDigit+4 ); |
|
|
4709 |
if( z==0 ){ |
|
|
4710 |
sqlite3_result_error_nomem(pCtx); |
|
|
4711 |
return; |
|
|
4712 |
} |
|
|
4713 |
i = 0; |
|
|
4714 |
if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){ |
|
|
4715 |
p->sign = 0; |
|
|
4716 |
} |
|
|
4717 |
if( p->sign ){ |
|
|
4718 |
z[0] = '-'; |
|
|
4719 |
i = 1; |
|
|
4720 |
} |
|
|
4721 |
n = p->nDigit - p->nFrac; |
|
|
4722 |
if( n<=0 ){ |
|
|
4723 |
z[i++] = '0'; |
|
|
4724 |
} |
|
|
4725 |
j = 0; |
|
|
4726 |
while( n>1 && p->a[j]==0 ){ |
|
|
4727 |
j++; |
|
|
4728 |
n--; |
|
|
4729 |
} |
|
|
4730 |
while( n>0 ){ |
|
|
4731 |
z[i++] = p->a[j] + '0'; |
|
|
4732 |
j++; |
|
|
4733 |
n--; |
|
|
4734 |
} |
|
|
4735 |
if( p->nFrac ){ |
|
|
4736 |
z[i++] = '.'; |
|
|
4737 |
do{ |
|
|
4738 |
z[i++] = p->a[j] + '0'; |
|
|
4739 |
j++; |
|
|
4740 |
}while( jnDigit ); |
|
|
4741 |
} |
|
|
4742 |
z[i] = 0; |
|
|
4743 |
sqlite3_result_text(pCtx, z, i, sqlite3_free); |
|
|
4744 |
} |
|
|
4745 |
|
|
|
4746 |
/* |
|
|
4747 |
** SQL Function: decimal(X) |
|
|
4748 |
** |
|
|
4749 |
** Convert input X into decimal and then back into text |
|
|
4750 |
*/ |
|
|
4751 |
static void decimalFunc( |
|
|
4752 |
sqlite3_context *context, |
|
|
4753 |
int argc, |
|
|
4754 |
sqlite3_value **argv |
|
|
4755 |
){ |
|
|
4756 |
Decimal *p = decimal_new(context, argv[0], 0, 0); |
|
|
4757 |
UNUSED_PARAMETER(argc); |
|
|
4758 |
decimal_result(context, p); |
|
|
4759 |
decimal_free(p); |
|
|
4760 |
} |
|
|
4761 |
|
|
|
4762 |
/* |
|
|
4763 |
** Compare to Decimal objects. Return negative, 0, or positive if the |
|
|
4764 |
** first object is less than, equal to, or greater than the second. |
|
|
4765 |
** |
|
|
4766 |
** Preconditions for this routine: |
|
|
4767 |
** |
|
|
4768 |
** pA!=0 |
|
|
4769 |
** pA->isNull==0 |
|
|
4770 |
** pB!=0 |
|
|
4771 |
** pB->isNull==0 |
|
|
4772 |
*/ |
|
|
4773 |
static int decimal_cmp(const Decimal *pA, const Decimal *pB){ |
|
|
4774 |
int nASig, nBSig, rc, n; |
|
|
4775 |
if( pA->sign!=pB->sign ){ |
|
|
4776 |
return pA->sign ? -1 : +1; |
|
|
4777 |
} |
|
|
4778 |
if( pA->sign ){ |
|
|
4779 |
const Decimal *pTemp = pA; |
|
|
4780 |
pA = pB; |
|
|
4781 |
pB = pTemp; |
|
|
4782 |
} |
|
|
4783 |
nASig = pA->nDigit - pA->nFrac; |
|
|
4784 |
nBSig = pB->nDigit - pB->nFrac; |
|
|
4785 |
if( nASig!=nBSig ){ |
|
|
4786 |
return nASig - nBSig; |
|
|
4787 |
} |
|
|
4788 |
n = pA->nDigit; |
|
|
4789 |
if( n>pB->nDigit ) n = pB->nDigit; |
|
|
4790 |
rc = memcmp(pA->a, pB->a, n); |
|
|
4791 |
if( rc==0 ){ |
|
|
4792 |
rc = pA->nDigit - pB->nDigit; |
|
|
4793 |
} |
|
|
4794 |
return rc; |
|
|
4795 |
} |
|
|
4796 |
|
|
|
4797 |
/* |
|
|
4798 |
** SQL Function: decimal_cmp(X, Y) |
|
|
4799 |
** |
|
|
4800 |
** Return negative, zero, or positive if X is less then, equal to, or |
|
|
4801 |
** greater than Y. |
|
|
4802 |
*/ |
|
|
4803 |
static void decimalCmpFunc( |
|
|
4804 |
sqlite3_context *context, |
|
|
4805 |
int argc, |
|
|
4806 |
sqlite3_value **argv |
|
|
4807 |
){ |
|
|
4808 |
Decimal *pA = 0, *pB = 0; |
|
|
4809 |
int rc; |
|
|
4810 |
|
|
|
4811 |
UNUSED_PARAMETER(argc); |
|
|
4812 |
pA = decimal_new(context, argv[0], 0, 0); |
|
|
4813 |
if( pA==0 || pA->isNull ) goto cmp_done; |
|
|
4814 |
pB = decimal_new(context, argv[1], 0, 0); |
|
|
4815 |
if( pB==0 || pB->isNull ) goto cmp_done; |
|
|
4816 |
rc = decimal_cmp(pA, pB); |
|
|
4817 |
if( rc<0 ) rc = -1; |
|
|
4818 |
else if( rc>0 ) rc = +1; |
|
|
4819 |
sqlite3_result_int(context, rc); |
|
|
4820 |
cmp_done: |
|
|
4821 |
decimal_free(pA); |
|
|
4822 |
decimal_free(pB); |
|
|
4823 |
} |
|
|
4824 |
|
|
|
4825 |
/* |
|
|
4826 |
** Expand the Decimal so that it has a least nDigit digits and nFrac |
|
|
4827 |
** digits to the right of the decimal point. |
|
|
4828 |
*/ |
|
|
4829 |
static void decimal_expand(Decimal *p, int nDigit, int nFrac){ |
|
|
4830 |
int nAddSig; |
|
|
4831 |
int nAddFrac; |
|
|
4832 |
if( p==0 ) return; |
|
|
4833 |
nAddFrac = nFrac - p->nFrac; |
|
|
4834 |
nAddSig = (nDigit - p->nDigit) - nAddFrac; |
|
|
4835 |
if( nAddFrac==0 && nAddSig==0 ) return; |
|
|
4836 |
p->a = sqlite3_realloc64(p->a, nDigit+1); |
|
|
4837 |
if( p->a==0 ){ |
|
|
4838 |
p->oom = 1; |
|
|
4839 |
return; |
|
|
4840 |
} |
|
|
4841 |
if( nAddSig ){ |
|
|
4842 |
memmove(p->a+nAddSig, p->a, p->nDigit); |
|
|
4843 |
memset(p->a, 0, nAddSig); |
|
|
4844 |
p->nDigit += nAddSig; |
|
|
4845 |
} |
|
|
4846 |
if( nAddFrac ){ |
|
|
4847 |
memset(p->a+p->nDigit, 0, nAddFrac); |
|
|
4848 |
p->nDigit += nAddFrac; |
|
|
4849 |
p->nFrac += nAddFrac; |
|
|
4850 |
} |
|
|
4851 |
} |
|
|
4852 |
|
|
|
4853 |
/* |
|
|
4854 |
** Add the value pB into pA. |
|
|
4855 |
** |
|
|
4856 |
** Both pA and pB might become denormalized by this routine. |
|
|
4857 |
*/ |
|
|
4858 |
static void decimal_add(Decimal *pA, Decimal *pB){ |
|
|
4859 |
int nSig, nFrac, nDigit; |
|
|
4860 |
int i, rc; |
|
|
4861 |
if( pA==0 ){ |
|
|
4862 |
return; |
|
|
4863 |
} |
|
|
4864 |
if( pA->oom || pB==0 || pB->oom ){ |
|
|
4865 |
pA->oom = 1; |
|
|
4866 |
return; |
|
|
4867 |
} |
|
|
4868 |
if( pA->isNull || pB->isNull ){ |
|
|
4869 |
pA->isNull = 1; |
|
|
4870 |
return; |
|
|
4871 |
} |
|
|
4872 |
nSig = pA->nDigit - pA->nFrac; |
|
|
4873 |
if( nSig && pA->a[0]==0 ) nSig--; |
|
|
4874 |
if( nSignDigit-pB->nFrac ){ |
|
|
4875 |
nSig = pB->nDigit - pB->nFrac; |
|
|
4876 |
} |
|
|
4877 |
nFrac = pA->nFrac; |
|
|
4878 |
if( nFracnFrac ) nFrac = pB->nFrac; |
|
|
4879 |
nDigit = nSig + nFrac + 1; |
|
|
4880 |
decimal_expand(pA, nDigit, nFrac); |
|
|
4881 |
decimal_expand(pB, nDigit, nFrac); |
|
|
4882 |
if( pA->oom || pB->oom ){ |
|
|
4883 |
pA->oom = 1; |
|
|
4884 |
}else{ |
|
|
4885 |
if( pA->sign==pB->sign ){ |
|
|
4886 |
int carry = 0; |
|
|
4887 |
for(i=nDigit-1; i>=0; i--){ |
|
|
4888 |
int x = pA->a[i] + pB->a[i] + carry; |
|
|
4889 |
if( x>=10 ){ |
|
|
4890 |
carry = 1; |
|
|
4891 |
pA->a[i] = x - 10; |
|
|
4892 |
}else{ |
|
|
4893 |
carry = 0; |
|
|
4894 |
pA->a[i] = x; |
|
|
4895 |
} |
|
|
4896 |
} |
|
|
4897 |
}else{ |
|
|
4898 |
signed char *aA, *aB; |
|
|
4899 |
int borrow = 0; |
|
|
4900 |
rc = memcmp(pA->a, pB->a, nDigit); |
|
|
4901 |
if( rc<0 ){ |
|
|
4902 |
aA = pB->a; |
|
|
4903 |
aB = pA->a; |
|
|
4904 |
pA->sign = !pA->sign; |
|
|
4905 |
}else{ |
|
|
4906 |
aA = pA->a; |
|
|
4907 |
aB = pB->a; |
|
|
4908 |
} |
|
|
4909 |
for(i=nDigit-1; i>=0; i--){ |
|
|
4910 |
int x = aA[i] - aB[i] - borrow; |
|
|
4911 |
if( x<0 ){ |
|
|
4912 |
pA->a[i] = x+10; |
|
|
4913 |
borrow = 1; |
|
|
4914 |
}else{ |
|
|
4915 |
pA->a[i] = x; |
|
|
4916 |
borrow = 0; |
|
|
4917 |
} |
|
|
4918 |
} |
|
|
4919 |
} |
|
|
4920 |
} |
|
|
4921 |
} |
|
|
4922 |
|
|
|
4923 |
/* |
|
|
4924 |
** Compare text in decimal order. |
|
|
4925 |
*/ |
|
|
4926 |
static int decimalCollFunc( |
|
|
4927 |
void *notUsed, |
|
|
4928 |
int nKey1, const void *pKey1, |
|
|
4929 |
int nKey2, const void *pKey2 |
|
|
4930 |
){ |
|
|
4931 |
const unsigned char *zA = (const unsigned char*)pKey1; |
|
|
4932 |
const unsigned char *zB = (const unsigned char*)pKey2; |
|
|
4933 |
Decimal *pA = decimal_new(0, 0, nKey1, zA); |
|
|
4934 |
Decimal *pB = decimal_new(0, 0, nKey2, zB); |
|
|
4935 |
int rc; |
|
|
4936 |
UNUSED_PARAMETER(notUsed); |
|
|
4937 |
if( pA==0 || pB==0 ){ |
|
|
4938 |
rc = 0; |
|
|
4939 |
}else{ |
|
|
4940 |
rc = decimal_cmp(pA, pB); |
|
|
4941 |
} |
|
|
4942 |
decimal_free(pA); |
|
|
4943 |
decimal_free(pB); |
|
|
4944 |
return rc; |
|
|
4945 |
} |
|
|
4946 |
|
|
|
4947 |
|
|
|
4948 |
/* |
|
|
4949 |
** SQL Function: decimal_add(X, Y) |
|
|
4950 |
** decimal_sub(X, Y) |
|
|
4951 |
** |
|
|
4952 |
** Return the sum or difference of X and Y. |
|
|
4953 |
*/ |
|
|
4954 |
static void decimalAddFunc( |
|
|
4955 |
sqlite3_context *context, |
|
|
4956 |
int argc, |
|
|
4957 |
sqlite3_value **argv |
|
|
4958 |
){ |
|
|
4959 |
Decimal *pA = decimal_new(context, argv[0], 0, 0); |
|
|
4960 |
Decimal *pB = decimal_new(context, argv[1], 0, 0); |
|
|
4961 |
UNUSED_PARAMETER(argc); |
|
|
4962 |
decimal_add(pA, pB); |
|
|
4963 |
decimal_result(context, pA); |
|
|
4964 |
decimal_free(pA); |
|
|
4965 |
decimal_free(pB); |
|
|
4966 |
} |
|
|
4967 |
static void decimalSubFunc( |
|
|
4968 |
sqlite3_context *context, |
|
|
4969 |
int argc, |
|
|
4970 |
sqlite3_value **argv |
|
|
4971 |
){ |
|
|
4972 |
Decimal *pA = decimal_new(context, argv[0], 0, 0); |
|
|
4973 |
Decimal *pB = decimal_new(context, argv[1], 0, 0); |
|
|
4974 |
UNUSED_PARAMETER(argc); |
|
|
4975 |
if( pB==0 ) return; |
|
|
4976 |
pB->sign = !pB->sign; |
|
|
4977 |
decimal_add(pA, pB); |
|
|
4978 |
decimal_result(context, pA); |
|
|
4979 |
decimal_free(pA); |
|
|
4980 |
decimal_free(pB); |
|
|
4981 |
} |
|
|
4982 |
|
|
|
4983 |
/* Aggregate funcion: decimal_sum(X) |
|
|
4984 |
** |
|
|
4985 |
** Works like sum() except that it uses decimal arithmetic for unlimited |
|
|
4986 |
** precision. |
|
|
4987 |
*/ |
|
|
4988 |
static void decimalSumStep( |
|
|
4989 |
sqlite3_context *context, |
|
|
4990 |
int argc, |
|
|
4991 |
sqlite3_value **argv |
|
|
4992 |
){ |
|
|
4993 |
Decimal *p; |
|
|
4994 |
Decimal *pArg; |
|
|
4995 |
UNUSED_PARAMETER(argc); |
|
|
4996 |
p = sqlite3_aggregate_context(context, sizeof(*p)); |
|
|
4997 |
if( p==0 ) return; |
|
|
4998 |
if( !p->isInit ){ |
|
|
4999 |
p->isInit = 1; |
|
|
5000 |
p->a = sqlite3_malloc(2); |
|
|
5001 |
if( p->a==0 ){ |
|
|
5002 |
p->oom = 1; |
|
|
5003 |
}else{ |
|
|
5004 |
p->a[0] = 0; |
|
|
5005 |
} |
|
|
5006 |
p->nDigit = 1; |
|
|
5007 |
p->nFrac = 0; |
|
|
5008 |
} |
|
|
5009 |
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
|
|
5010 |
pArg = decimal_new(context, argv[0], 0, 0); |
|
|
5011 |
decimal_add(p, pArg); |
|
|
5012 |
decimal_free(pArg); |
|
|
5013 |
} |
|
|
5014 |
static void decimalSumInverse( |
|
|
5015 |
sqlite3_context *context, |
|
|
5016 |
int argc, |
|
|
5017 |
sqlite3_value **argv |
|
|
5018 |
){ |
|
|
5019 |
Decimal *p; |
|
|
5020 |
Decimal *pArg; |
|
|
5021 |
UNUSED_PARAMETER(argc); |
|
|
5022 |
p = sqlite3_aggregate_context(context, sizeof(*p)); |
|
|
5023 |
if( p==0 ) return; |
|
|
5024 |
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
|
|
5025 |
pArg = decimal_new(context, argv[0], 0, 0); |
|
|
5026 |
if( pArg ) pArg->sign = !pArg->sign; |
|
|
5027 |
decimal_add(p, pArg); |
|
|
5028 |
decimal_free(pArg); |
|
|
5029 |
} |
|
|
5030 |
static void decimalSumValue(sqlite3_context *context){ |
|
|
5031 |
Decimal *p = sqlite3_aggregate_context(context, 0); |
|
|
5032 |
if( p==0 ) return; |
|
|
5033 |
decimal_result(context, p); |
|
|
5034 |
} |
|
|
5035 |
static void decimalSumFinalize(sqlite3_context *context){ |
|
|
5036 |
Decimal *p = sqlite3_aggregate_context(context, 0); |
|
|
5037 |
if( p==0 ) return; |
|
|
5038 |
decimal_result(context, p); |
|
|
5039 |
decimal_clear(p); |
|
|
5040 |
} |
|
|
5041 |
|
|
|
5042 |
/* |
|
|
5043 |
** SQL Function: decimal_mul(X, Y) |
|
|
5044 |
** |
|
|
5045 |
** Return the product of X and Y. |
|
|
5046 |
** |
|
|
5047 |
** All significant digits after the decimal point are retained. |
|
|
5048 |
** Trailing zeros after the decimal point are omitted as long as |
|
|
5049 |
** the number of digits after the decimal point is no less than |
|
|
5050 |
** either the number of digits in either input. |
|
|
5051 |
*/ |
|
|
5052 |
static void decimalMulFunc( |
|
|
5053 |
sqlite3_context *context, |
|
|
5054 |
int argc, |
|
|
5055 |
sqlite3_value **argv |
|
|
5056 |
){ |
|
|
5057 |
Decimal *pA = decimal_new(context, argv[0], 0, 0); |
|
|
5058 |
Decimal *pB = decimal_new(context, argv[1], 0, 0); |
|
|
5059 |
signed char *acc = 0; |
|
|
5060 |
int i, j, k; |
|
|
5061 |
int minFrac; |
|
|
5062 |
UNUSED_PARAMETER(argc); |
|
|
5063 |
if( pA==0 || pA->oom || pA->isNull |
|
|
5064 |
|| pB==0 || pB->oom || pB->isNull |
|
|
5065 |
){ |
|
|
5066 |
goto mul_end; |
|
|
5067 |
} |
|
|
5068 |
acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 ); |
|
|
5069 |
if( acc==0 ){ |
|
|
5070 |
sqlite3_result_error_nomem(context); |
|
|
5071 |
goto mul_end; |
|
|
5072 |
} |
|
|
5073 |
memset(acc, 0, pA->nDigit + pB->nDigit + 2); |
|
|
5074 |
minFrac = pA->nFrac; |
|
|
5075 |
if( pB->nFracnFrac; |
|
|
5076 |
for(i=pA->nDigit-1; i>=0; i--){ |
|
|
5077 |
signed char f = pA->a[i]; |
|
|
5078 |
int carry = 0, x; |
|
|
5079 |
for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){ |
|
|
5080 |
x = acc[k] + f*pB->a[j] + carry; |
|
|
5081 |
acc[k] = x%10; |
|
|
5082 |
carry = x/10; |
|
|
5083 |
} |
|
|
5084 |
x = acc[k] + carry; |
|
|
5085 |
acc[k] = x%10; |
|
|
5086 |
acc[k-1] += x/10; |
|
|
5087 |
} |
|
|
5088 |
sqlite3_free(pA->a); |
|
|
5089 |
pA->a = acc; |
|
|
5090 |
acc = 0; |
|
|
5091 |
pA->nDigit += pB->nDigit + 2; |
|
|
5092 |
pA->nFrac += pB->nFrac; |
|
|
5093 |
pA->sign ^= pB->sign; |
|
|
5094 |
while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){ |
|
|
5095 |
pA->nFrac--; |
|
|
5096 |
pA->nDigit--; |
|
|
5097 |
} |
|
|
5098 |
decimal_result(context, pA); |
|
|
5099 |
|
|
|
5100 |
mul_end: |
|
|
5101 |
sqlite3_free(acc); |
|
|
5102 |
decimal_free(pA); |
|
|
5103 |
decimal_free(pB); |
|
|
5104 |
} |
|
|
5105 |
|
|
|
5106 |
#ifdef _WIN32 |
|
|
5107 |
|
|
|
5108 |
#endif |
|
|
5109 |
int sqlite3_decimal_init( |
|
|
5110 |
sqlite3 *db, |
|
|
5111 |
char **pzErrMsg, |
|
|
5112 |
const sqlite3_api_routines *pApi |
|
|
5113 |
){ |
|
|
5114 |
int rc = SQLITE_OK; |
|
|
5115 |
static const struct { |
|
|
5116 |
const char *zFuncName; |
|
|
5117 |
int nArg; |
|
|
5118 |
void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
|
|
5119 |
} aFunc[] = { |
|
|
5120 |
{ "decimal", 1, decimalFunc }, |
|
|
5121 |
{ "decimal_cmp", 2, decimalCmpFunc }, |
|
|
5122 |
{ "decimal_add", 2, decimalAddFunc }, |
|
|
5123 |
{ "decimal_sub", 2, decimalSubFunc }, |
|
|
5124 |
{ "decimal_mul", 2, decimalMulFunc }, |
|
|
5125 |
}; |
|
|
5126 |
unsigned int i; |
|
|
5127 |
(void)pzErrMsg; /* Unused parameter */ |
|
|
5128 |
|
|
|
5129 |
SQLITE_EXTENSION_INIT2(pApi); |
|
|
5130 |
|
|
|
5131 |
for(i=0; i |
|
|
5132 |
rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg, |
|
|
5133 |
SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, |
|
|
5134 |
0, aFunc[i].xFunc, 0, 0); |
|
|
5135 |
} |
|
|
5136 |
if( rc==SQLITE_OK ){ |
|
|
5137 |
rc = sqlite3_create_window_function(db, "decimal_sum", 1, |
|
|
5138 |
SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0, |
|
|
5139 |
decimalSumStep, decimalSumFinalize, |
|
|
5140 |
decimalSumValue, decimalSumInverse, 0); |
|
|
5141 |
} |
|
|
5142 |
if( rc==SQLITE_OK ){ |
|
|
5143 |
rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8, |
|
|
5144 |
0, decimalCollFunc); |
|
|
5145 |
} |
|
|
5146 |
return rc; |
|
|
5147 |
} |
|
|
5148 |
|
|
|
5149 |
/************************* End ../ext/misc/decimal.c ********************/ |
|
|
5150 |
/************************* Begin ../ext/misc/ieee754.c ******************/ |
|
|
5151 |
/* |
|
|
5152 |
** 2013-04-17 |
|
|
5153 |
** |
|
|
5154 |
** The author disclaims copyright to this source code. In place of |
|
|
5155 |
** a legal notice, here is a blessing: |
|
|
5156 |
** |
|
|
5157 |
** May you do good and not evil. |
|
|
5158 |
** May you find forgiveness for yourself and forgive others. |
|
|
5159 |
** May you share freely, never taking more than you give. |
|
|
5160 |
** |
|
|
5161 |
****************************************************************************** |
|
|
5162 |
** |
|
|
5163 |
** This SQLite extension implements functions for the exact display |
|
|
5164 |
** and input of IEEE754 Binary64 floating-point numbers. |
|
|
5165 |
** |
|
|
5166 |
** ieee754(X) |
|
|
5167 |
** ieee754(Y,Z) |
|
|
5168 |
** |
|
|
5169 |
** In the first form, the value X should be a floating-point number. |
|
|
5170 |
** The function will return a string of the form 'ieee754(Y,Z)' where |
|
|
5171 |
** Y and Z are integers such that X==Y*pow(2,Z). |
|
|
5172 |
** |
|
|
5173 |
** In the second form, Y and Z are integers which are the mantissa and |
|
|
5174 |
** base-2 exponent of a new floating point number. The function returns |
|
|
5175 |
** a floating-point value equal to Y*pow(2,Z). |
|
|
5176 |
** |
|
|
5177 |
** Examples: |
|
|
5178 |
** |
|
|
5179 |
** ieee754(2.0) -> 'ieee754(2,0)' |
|
|
5180 |
** ieee754(45.25) -> 'ieee754(181,-2)' |
|
|
5181 |
** ieee754(2, 0) -> 2.0 |
|
|
5182 |
** ieee754(181, -2) -> 45.25 |
|
|
5183 |
** |
|
|
5184 |
** Two additional functions break apart the one-argument ieee754() |
|
|
5185 |
** result into separate integer values: |
|
|
5186 |
** |
|
|
5187 |
** ieee754_mantissa(45.25) -> 181 |
|
|
5188 |
** ieee754_exponent(45.25) -> -2 |
|
|
5189 |
** |
|
|
5190 |
** These functions convert binary64 numbers into blobs and back again. |
|
|
5191 |
** |
|
|
5192 |
** ieee754_from_blob(x'3ff0000000000000') -> 1.0 |
|
|
5193 |
** ieee754_to_blob(1.0) -> x'3ff0000000000000' |
|
|
5194 |
** |
|
|
5195 |
** In all single-argument functions, if the argument is an 8-byte blob |
|
|
5196 |
** then that blob is interpreted as a big-endian binary64 value. |
|
|
5197 |
** |
|
|
5198 |
** |
|
|
5199 |
** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES |
|
|
5200 |
** ----------------------------------------------- |
|
|
5201 |
** |
|
|
5202 |
** This extension in combination with the separate 'decimal' extension |
|
|
5203 |
** can be used to compute the exact decimal representation of binary64 |
|
|
5204 |
** values. To begin, first compute a table of exponent values: |
|
|
5205 |
** |
|
|
5206 |
** CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT); |
|
|
5207 |
** WITH RECURSIVE c(x,v) AS ( |
|
|
5208 |
** VALUES(0,'1') |
|
|
5209 |
** UNION ALL |
|
|
5210 |
** SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971 |
|
|
5211 |
** ) INSERT INTO pow2(x,v) SELECT x, v FROM c; |
|
|
5212 |
** WITH RECURSIVE c(x,v) AS ( |
|
|
5213 |
** VALUES(-1,'0.5') |
|
|
5214 |
** UNION ALL |
|
|
5215 |
** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075 |
|
|
5216 |
** ) INSERT INTO pow2(x,v) SELECT x, v FROM c; |
|
|
5217 |
** |
|
|
5218 |
** Then, to compute the exact decimal representation of a floating |
|
|
5219 |
** point value (the value 47.49 is used in the example) do: |
|
|
5220 |
** |
|
|
5221 |
** WITH c(n) AS (VALUES(47.49)) |
|
|
5222 |
** ---------------^^^^^---- Replace with whatever you want |
|
|
5223 |
** SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v) |
|
|
5224 |
** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n); |
|
|
5225 |
** |
|
|
5226 |
** Here is a query to show various boundry values for the binary64 |
|
|
5227 |
** number format: |
|
|
5228 |
** |
|
|
5229 |
** WITH c(name,bin) AS (VALUES |
|
|
5230 |
** ('minimum positive value', x'0000000000000001'), |
|
|
5231 |
** ('maximum subnormal value', x'000fffffffffffff'), |
|
|
5232 |
** ('mininum positive nornal value', x'0010000000000000'), |
|
|
5233 |
** ('maximum value', x'7fefffffffffffff')) |
|
|
5234 |
** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v) |
|
|
5235 |
** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin); |
|
|
5236 |
** |
|
|
5237 |
*/ |
|
|
5238 |
/* #include "sqlite3ext.h" */ |
|
|
5239 |
SQLITE_EXTENSION_INIT1 |
|
|
5240 |
#include |
|
|
5241 |
#include |
|
|
5242 |
|
|
|
5243 |
/* Mark a function parameter as unused, to suppress nuisance compiler |
|
|
5244 |
** warnings. */ |
|
|
5245 |
#ifndef UNUSED_PARAMETER |
|
|
5246 |
# define UNUSED_PARAMETER(X) (void)(X) |
|
|
5247 |
#endif |
|
|
5248 |
|
|
|
5249 |
/* |
|
|
5250 |
** Implementation of the ieee754() function |
|
|
5251 |
*/ |
|
|
5252 |
static void ieee754func( |
|
|
5253 |
sqlite3_context *context, |
|
|
5254 |
int argc, |
|
|
5255 |
sqlite3_value **argv |
|
|
5256 |
){ |
|
|
5257 |
if( argc==1 ){ |
|
|
5258 |
sqlite3_int64 m, a; |
|
|
5259 |
double r; |
|
|
5260 |
int e; |
|
|
5261 |
int isNeg; |
|
|
5262 |
char zResult[100]; |
|
|
5263 |
assert( sizeof(m)==sizeof(r) ); |
|
|
5264 |
if( sqlite3_value_type(argv[0])==SQLITE_BLOB |
|
|
5265 |
&& sqlite3_value_bytes(argv[0])==sizeof(r) |
|
|
5266 |
){ |
|
|
5267 |
const unsigned char *x = sqlite3_value_blob(argv[0]); |
|
|
5268 |
unsigned int i; |
|
|
5269 |
sqlite3_uint64 v = 0; |
|
|
5270 |
for(i=0; i |
|
|
5271 |
v = (v<<8) | x[i]; |
|
|
5272 |
} |
|
|
5273 |
memcpy(&r, &v, sizeof(r)); |
|
|
5274 |
}else{ |
|
|
5275 |
r = sqlite3_value_double(argv[0]); |
|
|
5276 |
} |
|
|
5277 |
if( r<0.0 ){ |
|
|
5278 |
isNeg = 1; |
|
|
5279 |
r = -r; |
|
|
5280 |
}else{ |
|
|
5281 |
isNeg = 0; |
|
|
5282 |
} |
|
|
5283 |
memcpy(&a,&r,sizeof(a)); |
|
|
5284 |
if( a==0 ){ |
|
|
5285 |
e = 0; |
|
|
5286 |
m = 0; |
|
|
5287 |
}else{ |
|
|
5288 |
e = a>>52; |
|
|
5289 |
m = a & ((((sqlite3_int64)1)<<52)-1); |
|
|
5290 |
if( e==0 ){ |
|
|
5291 |
m <<= 1; |
|
|
5292 |
}else{ |
|
|
5293 |
m |= ((sqlite3_int64)1)<<52; |
|
|
5294 |
} |
|
|
5295 |
while( e<1075 && m>0 && (m&1)==0 ){ |
|
|
5296 |
m >>= 1; |
|
|
5297 |
e++; |
|
|
5298 |
} |
|
|
5299 |
if( isNeg ) m = -m; |
|
|
5300 |
} |
|
|
5301 |
switch( *(int*)sqlite3_user_data(context) ){ |
|
|
5302 |
case 0: |
|
|
5303 |
sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)", |
|
|
5304 |
m, e-1075); |
|
|
5305 |
sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT); |
|
|
5306 |
break; |
|
|
5307 |
case 1: |
|
|
5308 |
sqlite3_result_int64(context, m); |
|
|
5309 |
break; |
|
|
5310 |
case 2: |
|
|
5311 |
sqlite3_result_int(context, e-1075); |
|
|
5312 |
break; |
|
|
5313 |
} |
|
|
5314 |
}else{ |
|
|
5315 |
sqlite3_int64 m, e, a; |
|
|
5316 |
double r; |
|
|
5317 |
int isNeg = 0; |
|
|
5318 |
m = sqlite3_value_int64(argv[0]); |
|
|
5319 |
e = sqlite3_value_int64(argv[1]); |
|
|
5320 |
|
|
|
5321 |
/* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */ |
|
|
5322 |
if( e>10000 ){ |
|
|
5323 |
e = 10000; |
|
|
5324 |
}else if( e<-10000 ){ |
|
|
5325 |
e = -10000; |
|
|
5326 |
} |
|
|
5327 |
|
|
|
5328 |
if( m<0 ){ |
|
|
5329 |
isNeg = 1; |
|
|
5330 |
m = -m; |
|
|
5331 |
if( m<0 ) return; |
|
|
5332 |
}else if( m==0 && e>-1000 && e<1000 ){ |
|
|
5333 |
sqlite3_result_double(context, 0.0); |
|
|
5334 |
return; |
|
|
5335 |
} |
|
|
5336 |
while( (m>>32)&0xffe00000 ){ |
|
|
5337 |
m >>= 1; |
|
|
5338 |
e++; |
|
|
5339 |
} |
|
|
5340 |
while( m!=0 && ((m>>32)&0xfff00000)==0 ){ |
|
|
5341 |
m <<= 1; |
|
|
5342 |
e--; |
|
|
5343 |
} |
|
|
5344 |
e += 1075; |
|
|
5345 |
if( e<=0 ){ |
|
|
5346 |
/* Subnormal */ |
|
|
5347 |
m >>= 1-e; |
|
|
5348 |
e = 0; |
|
|
5349 |
}else if( e>0x7ff ){ |
|
|
5350 |
e = 0x7ff; |
|
|
5351 |
} |
|
|
5352 |
a = m & ((((sqlite3_int64)1)<<52)-1); |
|
|
5353 |
a |= e<<52; |
|
|
5354 |
if( isNeg ) a |= ((sqlite3_uint64)1)<<63; |
|
|
5355 |
memcpy(&r, &a, sizeof(r)); |
|
|
5356 |
sqlite3_result_double(context, r); |
|
|
5357 |
} |
|
|
5358 |
} |
|
|
5359 |
|
|
|
5360 |
/* |
|
|
5361 |
** Functions to convert between blobs and floats. |
|
|
5362 |
*/ |
|
|
5363 |
static void ieee754func_from_blob( |
|
|
5364 |
sqlite3_context *context, |
|
|
5365 |
int argc, |
|
|
5366 |
sqlite3_value **argv |
|
|
5367 |
){ |
|
|
5368 |
UNUSED_PARAMETER(argc); |
|
|
5369 |
if( sqlite3_value_type(argv[0])==SQLITE_BLOB |
|
|
5370 |
&& sqlite3_value_bytes(argv[0])==sizeof(double) |
|
|
5371 |
){ |
|
|
5372 |
double r; |
|
|
5373 |
const unsigned char *x = sqlite3_value_blob(argv[0]); |
|
|
5374 |
unsigned int i; |
|
|
5375 |
sqlite3_uint64 v = 0; |
|
|
5376 |
for(i=0; i |
|
|
5377 |
v = (v<<8) | x[i]; |
|
|
5378 |
} |
|
|
5379 |
memcpy(&r, &v, sizeof(r)); |
|
|
5380 |
sqlite3_result_double(context, r); |
|
|
5381 |
} |
|
|
5382 |
} |
|
|
5383 |
static void ieee754func_to_blob( |
|
|
5384 |
sqlite3_context *context, |
|
|
5385 |
int argc, |
|
|
5386 |
sqlite3_value **argv |
|
|
5387 |
){ |
|
|
5388 |
UNUSED_PARAMETER(argc); |
|
|
5389 |
if( sqlite3_value_type(argv[0])==SQLITE_FLOAT |
|
|
5390 |
|| sqlite3_value_type(argv[0])==SQLITE_INTEGER |
|
|
5391 |
){ |
|
|
5392 |
double r = sqlite3_value_double(argv[0]); |
|
|
5393 |
sqlite3_uint64 v; |
|
|
5394 |
unsigned char a[sizeof(r)]; |
|
|
5395 |
unsigned int i; |
|
|
5396 |
memcpy(&v, &r, sizeof(r)); |
|
|
5397 |
for(i=1; i<=sizeof(r); i++){ |
|
|
5398 |
a[sizeof(r)-i] = v&0xff; |
|
|
5399 |
v >>= 8; |
|
|
5400 |
} |
|
|
5401 |
sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT); |
|
|
5402 |
} |
|
|
5403 |
} |
|
|
5404 |
|
|
|
5405 |
|
|
|
5406 |
#ifdef _WIN32 |
|
|
5407 |
|
|
|
5408 |
#endif |
|
|
5409 |
int sqlite3_ieee_init( |
|
|
5410 |
sqlite3 *db, |
|
|
5411 |
char **pzErrMsg, |
|
|
5412 |
const sqlite3_api_routines *pApi |
|
|
5413 |
){ |
|
|
5414 |
static const struct { |
|
|
5415 |
char *zFName; |
|
|
5416 |
int nArg; |
|
|
5417 |
int iAux; |
|
|
5418 |
void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
|
|
5419 |
} aFunc[] = { |
|
|
5420 |
{ "ieee754", 1, 0, ieee754func }, |
|
|
5421 |
{ "ieee754", 2, 0, ieee754func }, |
|
|
5422 |
{ "ieee754_mantissa", 1, 1, ieee754func }, |
|
|
5423 |
{ "ieee754_exponent", 1, 2, ieee754func }, |
|
|
5424 |
{ "ieee754_to_blob", 1, 0, ieee754func_to_blob }, |
|
|
5425 |
{ "ieee754_from_blob", 1, 0, ieee754func_from_blob }, |
|
|
5426 |
|
|
|
5427 |
}; |
|
|
5428 |
unsigned int i; |
|
|
5429 |
int rc = SQLITE_OK; |
|
|
5430 |
SQLITE_EXTENSION_INIT2(pApi); |
|
|
5431 |
(void)pzErrMsg; /* Unused parameter */ |
|
|
5432 |
for(i=0; i |
|
|
5433 |
rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg, |
|
|
5434 |
SQLITE_UTF8|SQLITE_INNOCUOUS, |
|
|
5435 |
(void*)&aFunc[i].iAux, |
|
|
5436 |
aFunc[i].xFunc, 0, 0); |
|
|
5437 |
} |
|
|
5438 |
return rc; |
|
|
5439 |
} |
|
|
5440 |
|
|
|
5441 |
/************************* End ../ext/misc/ieee754.c ********************/ |
|
|
5442 |
/************************* Begin ../ext/misc/series.c ******************/ |
|
|
5443 |
/* |
|
|
5444 |
** 2015-08-18 |
|
|
5445 |
** |
|
|
5446 |
** The author disclaims copyright to this source code. In place of |
|
|
5447 |
** a legal notice, here is a blessing: |
|
|
5448 |
** |
|
|
5449 |
** May you do good and not evil. |
|
|
5450 |
** May you find forgiveness for yourself and forgive others. |
|
|
5451 |
** May you share freely, never taking more than you give. |
|
|
5452 |
** |
|
|
5453 |
************************************************************************* |
|
|
5454 |
** |
|
|
5455 |
** This file demonstrates how to create a table-valued-function using |
|
|
5456 |
** a virtual table. This demo implements the generate_series() function |
|
|
5457 |
** which gives similar results to the eponymous function in PostgreSQL. |
|
|
5458 |
** Examples: |
|
|
5459 |
** |
|
|
5460 |
** SELECT * FROM generate_series(0,100,5); |
|
|
5461 |
** |
|
|
5462 |
** The query above returns integers from 0 through 100 counting by steps |
|
|
5463 |
** of 5. |
|
|
5464 |
** |
|
|
5465 |
** SELECT * FROM generate_series(0,100); |
|
|
5466 |
** |
|
|
5467 |
** Integers from 0 through 100 with a step size of 1. |
|
|
5468 |
** |
|
|
5469 |
** SELECT * FROM generate_series(20) LIMIT 10; |
|
|
5470 |
** |
|
|
5471 |
** Integers 20 through 29. |
|
|
5472 |
** |
|
|
5473 |
** HOW IT WORKS |
|
|
5474 |
** |
|
|
5475 |
** The generate_series "function" is really a virtual table with the |
|
|
5476 |
** following schema: |
|
|
5477 |
** |
|
|
5478 |
** CREATE TABLE generate_series( |
|
|
5479 |
** value, |
|
|
5480 |
** start HIDDEN, |
|
|
5481 |
** stop HIDDEN, |
|
|
5482 |
** step HIDDEN |
|
|
5483 |
** ); |
|
|
5484 |
** |
|
|
5485 |
** Function arguments in queries against this virtual table are translated |
|
|
5486 |
** into equality constraints against successive hidden columns. In other |
|
|
5487 |
** words, the following pairs of queries are equivalent to each other: |
|
|
5488 |
** |
|
|
5489 |
** SELECT * FROM generate_series(0,100,5); |
|
|
5490 |
** SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5; |
|
|
5491 |
** |
|
|
5492 |
** SELECT * FROM generate_series(0,100); |
|
|
5493 |
** SELECT * FROM generate_series WHERE start=0 AND stop=100; |
|
|
5494 |
** |
|
|
5495 |
** SELECT * FROM generate_series(20) LIMIT 10; |
|
|
5496 |
** SELECT * FROM generate_series WHERE start=20 LIMIT 10; |
|
|
5497 |
** |
|
|
5498 |
** The generate_series virtual table implementation leaves the xCreate method |
|
|
5499 |
** set to NULL. This means that it is not possible to do a CREATE VIRTUAL |
|
|
5500 |
** TABLE command with "generate_series" as the USING argument. Instead, there |
|
|
5501 |
** is a single generate_series virtual table that is always available without |
|
|
5502 |
** having to be created first. |
|
|
5503 |
** |
|
|
5504 |
** The xBestIndex method looks for equality constraints against the hidden |
|
|
5505 |
** start, stop, and step columns, and if present, it uses those constraints |
|
|
5506 |
** to bound the sequence of generated values. If the equality constraints |
|
|
5507 |
** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step. |
|
|
5508 |
** xBestIndex returns a small cost when both start and stop are available, |
|
|
5509 |
** and a very large cost if either start or stop are unavailable. This |
|
|
5510 |
** encourages the query planner to order joins such that the bounds of the |
|
|
5511 |
** series are well-defined. |
|
|
5512 |
*/ |
|
|
5513 |
/* #include "sqlite3ext.h" */ |
|
|
5514 |
SQLITE_EXTENSION_INIT1 |
|
|
5515 |
#include |
|
|
5516 |
#include |
|
|
5517 |
|
|
|
5518 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
5519 |
|
|
|
5520 |
|
|
|
5521 |
/* series_cursor is a subclass of sqlite3_vtab_cursor which will |
|
|
5522 |
** serve as the underlying representation of a cursor that scans |
|
|
5523 |
** over rows of the result |
|
|
5524 |
*/ |
|
|
5525 |
typedef struct series_cursor series_cursor; |
|
|
5526 |
struct series_cursor { |
|
|
5527 |
sqlite3_vtab_cursor base; /* Base class - must be first */ |
|
|
5528 |
int isDesc; /* True to count down rather than up */ |
|
|
5529 |
sqlite3_int64 iRowid; /* The rowid */ |
|
|
5530 |
sqlite3_int64 iValue; /* Current value ("value") */ |
|
|
5531 |
sqlite3_int64 mnValue; /* Mimimum value ("start") */ |
|
|
5532 |
sqlite3_int64 mxValue; /* Maximum value ("stop") */ |
|
|
5533 |
sqlite3_int64 iStep; /* Increment ("step") */ |
|
|
5534 |
}; |
|
|
5535 |
|
|
|
5536 |
/* |
|
|
5537 |
** The seriesConnect() method is invoked to create a new |
|
|
5538 |
** series_vtab that describes the generate_series virtual table. |
|
|
5539 |
** |
|
|
5540 |
** Think of this routine as the constructor for series_vtab objects. |
|
|
5541 |
** |
|
|
5542 |
** All this routine needs to do is: |
|
|
5543 |
** |
|
|
5544 |
** (1) Allocate the series_vtab object and initialize all fields. |
|
|
5545 |
** |
|
|
5546 |
** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the |
|
|
5547 |
** result set of queries against generate_series will look like. |
|
|
5548 |
*/ |
|
|
5549 |
static int seriesConnect( |
|
|
5550 |
sqlite3 *db, |
|
|
5551 |
void *pUnused, |
|
|
5552 |
int argcUnused, const char *const*argvUnused, |
|
|
5553 |
sqlite3_vtab **ppVtab, |
|
|
5554 |
char **pzErrUnused |
|
|
5555 |
){ |
|
|
5556 |
sqlite3_vtab *pNew; |
|
|
5557 |
int rc; |
|
|
5558 |
|
|
|
5559 |
/* Column numbers */ |
|
|
5560 |
#define SERIES_COLUMN_VALUE 0 |
|
|
5561 |
#define SERIES_COLUMN_START 1 |
|
|
5562 |
#define SERIES_COLUMN_STOP 2 |
|
|
5563 |
#define SERIES_COLUMN_STEP 3 |
|
|
5564 |
|
|
|
5565 |
(void)pUnused; |
|
|
5566 |
(void)argcUnused; |
|
|
5567 |
(void)argvUnused; |
|
|
5568 |
(void)pzErrUnused; |
|
|
5569 |
rc = sqlite3_declare_vtab(db, |
|
|
5570 |
"CREATE TABLE x(value,start hidden,stop hidden,step hidden)"); |
|
|
5571 |
if( rc==SQLITE_OK ){ |
|
|
5572 |
pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); |
|
|
5573 |
if( pNew==0 ) return SQLITE_NOMEM; |
|
|
5574 |
memset(pNew, 0, sizeof(*pNew)); |
|
|
5575 |
sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); |
|
|
5576 |
} |
|
|
5577 |
return rc; |
|
|
5578 |
} |
|
|
5579 |
|
|
|
5580 |
/* |
|
|
5581 |
** This method is the destructor for series_cursor objects. |
|
|
5582 |
*/ |
|
|
5583 |
static int seriesDisconnect(sqlite3_vtab *pVtab){ |
|
|
5584 |
sqlite3_free(pVtab); |
|
|
5585 |
return SQLITE_OK; |
|
|
5586 |
} |
|
|
5587 |
|
|
|
5588 |
/* |
|
|
5589 |
** Constructor for a new series_cursor object. |
|
|
5590 |
*/ |
|
|
5591 |
static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){ |
|
|
5592 |
series_cursor *pCur; |
|
|
5593 |
(void)pUnused; |
|
|
5594 |
pCur = sqlite3_malloc( sizeof(*pCur) ); |
|
|
5595 |
if( pCur==0 ) return SQLITE_NOMEM; |
|
|
5596 |
memset(pCur, 0, sizeof(*pCur)); |
|
|
5597 |
*ppCursor = &pCur->base; |
|
|
5598 |
return SQLITE_OK; |
|
|
5599 |
} |
|
|
5600 |
|
|
|
5601 |
/* |
|
|
5602 |
** Destructor for a series_cursor. |
|
|
5603 |
*/ |
|
|
5604 |
static int seriesClose(sqlite3_vtab_cursor *cur){ |
|
|
5605 |
sqlite3_free(cur); |
|
|
5606 |
return SQLITE_OK; |
|
|
5607 |
} |
|
|
5608 |
|
|
|
5609 |
|
|
|
5610 |
/* |
|
|
5611 |
** Advance a series_cursor to its next row of output. |
|
|
5612 |
*/ |
|
|
5613 |
static int seriesNext(sqlite3_vtab_cursor *cur){ |
|
|
5614 |
series_cursor *pCur = (series_cursor*)cur; |
|
|
5615 |
if( pCur->isDesc ){ |
|
|
5616 |
pCur->iValue -= pCur->iStep; |
|
|
5617 |
}else{ |
|
|
5618 |
pCur->iValue += pCur->iStep; |
|
|
5619 |
} |
|
|
5620 |
pCur->iRowid++; |
|
|
5621 |
return SQLITE_OK; |
|
|
5622 |
} |
|
|
5623 |
|
|
|
5624 |
/* |
|
|
5625 |
** Return values of columns for the row at which the series_cursor |
|
|
5626 |
** is currently pointing. |
|
|
5627 |
*/ |
|
|
5628 |
static int seriesColumn( |
|
|
5629 |
sqlite3_vtab_cursor *cur, /* The cursor */ |
|
|
5630 |
sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
|
|
5631 |
int i /* Which column to return */ |
|
|
5632 |
){ |
|
|
5633 |
series_cursor *pCur = (series_cursor*)cur; |
|
|
5634 |
sqlite3_int64 x = 0; |
|
|
5635 |
switch( i ){ |
|
|
5636 |
case SERIES_COLUMN_START: x = pCur->mnValue; break; |
|
|
5637 |
case SERIES_COLUMN_STOP: x = pCur->mxValue; break; |
|
|
5638 |
case SERIES_COLUMN_STEP: x = pCur->iStep; break; |
|
|
5639 |
default: x = pCur->iValue; break; |
|
|
5640 |
} |
|
|
5641 |
sqlite3_result_int64(ctx, x); |
|
|
5642 |
return SQLITE_OK; |
|
|
5643 |
} |
|
|
5644 |
|
|
|
5645 |
/* |
|
|
5646 |
** Return the rowid for the current row. In this implementation, the |
|
|
5647 |
** first row returned is assigned rowid value 1, and each subsequent |
|
|
5648 |
** row a value 1 more than that of the previous. |
|
|
5649 |
*/ |
|
|
5650 |
static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
|
|
5651 |
series_cursor *pCur = (series_cursor*)cur; |
|
|
5652 |
*pRowid = pCur->iRowid; |
|
|
5653 |
return SQLITE_OK; |
|
|
5654 |
} |
|
|
5655 |
|
|
|
5656 |
/* |
|
|
5657 |
** Return TRUE if the cursor has been moved off of the last |
|
|
5658 |
** row of output. |
|
|
5659 |
*/ |
|
|
5660 |
static int seriesEof(sqlite3_vtab_cursor *cur){ |
|
|
5661 |
series_cursor *pCur = (series_cursor*)cur; |
|
|
5662 |
if( pCur->isDesc ){ |
|
|
5663 |
return pCur->iValue < pCur->mnValue; |
|
|
5664 |
}else{ |
|
|
5665 |
return pCur->iValue > pCur->mxValue; |
|
|
5666 |
} |
|
|
5667 |
} |
|
|
5668 |
|
|
|
5669 |
/* True to cause run-time checking of the start=, stop=, and/or step= |
|
|
5670 |
** parameters. The only reason to do this is for testing the |
|
|
5671 |
** constraint checking logic for virtual tables in the SQLite core. |
|
|
5672 |
*/ |
|
|
5673 |
#ifndef SQLITE_SERIES_CONSTRAINT_VERIFY |
|
|
5674 |
# define SQLITE_SERIES_CONSTRAINT_VERIFY 0 |
|
|
5675 |
#endif |
|
|
5676 |
|
|
|
5677 |
/* |
|
|
5678 |
** This method is called to "rewind" the series_cursor object back |
|
|
5679 |
** to the first row of output. This method is always called at least |
|
|
5680 |
** once prior to any call to seriesColumn() or seriesRowid() or |
|
|
5681 |
** seriesEof(). |
|
|
5682 |
** |
|
|
5683 |
** The query plan selected by seriesBestIndex is passed in the idxNum |
|
|
5684 |
** parameter. (idxStr is not used in this implementation.) idxNum |
|
|
5685 |
** is a bitmask showing which constraints are available: |
|
|
5686 |
** |
|
|
5687 |
** 1: start=VALUE |
|
|
5688 |
** 2: stop=VALUE |
|
|
5689 |
** 4: step=VALUE |
|
|
5690 |
** |
|
|
5691 |
** Also, if bit 8 is set, that means that the series should be output |
|
|
5692 |
** in descending order rather than in ascending order. If bit 16 is |
|
|
5693 |
** set, then output must appear in ascending order. |
|
|
5694 |
** |
|
|
5695 |
** This routine should initialize the cursor and position it so that it |
|
|
5696 |
** is pointing at the first row, or pointing off the end of the table |
|
|
5697 |
** (so that seriesEof() will return true) if the table is empty. |
|
|
5698 |
*/ |
|
|
5699 |
static int seriesFilter( |
|
|
5700 |
sqlite3_vtab_cursor *pVtabCursor, |
|
|
5701 |
int idxNum, const char *idxStrUnused, |
|
|
5702 |
int argc, sqlite3_value **argv |
|
|
5703 |
){ |
|
|
5704 |
series_cursor *pCur = (series_cursor *)pVtabCursor; |
|
|
5705 |
int i = 0; |
|
|
5706 |
(void)idxStrUnused; |
|
|
5707 |
if( idxNum & 1 ){ |
|
|
5708 |
pCur->mnValue = sqlite3_value_int64(argv[i++]); |
|
|
5709 |
}else{ |
|
|
5710 |
pCur->mnValue = 0; |
|
|
5711 |
} |
|
|
5712 |
if( idxNum & 2 ){ |
|
|
5713 |
pCur->mxValue = sqlite3_value_int64(argv[i++]); |
|
|
5714 |
}else{ |
|
|
5715 |
pCur->mxValue = 0xffffffff; |
|
|
5716 |
} |
|
|
5717 |
if( idxNum & 4 ){ |
|
|
5718 |
pCur->iStep = sqlite3_value_int64(argv[i++]); |
|
|
5719 |
if( pCur->iStep==0 ){ |
|
|
5720 |
pCur->iStep = 1; |
|
|
5721 |
}else if( pCur->iStep<0 ){ |
|
|
5722 |
pCur->iStep = -pCur->iStep; |
|
|
5723 |
if( (idxNum & 16)==0 ) idxNum |= 8; |
|
|
5724 |
} |
|
|
5725 |
}else{ |
|
|
5726 |
pCur->iStep = 1; |
|
|
5727 |
} |
|
|
5728 |
for(i=0; i |
|
|
5729 |
if( sqlite3_value_type(argv[i])==SQLITE_NULL ){ |
|
|
5730 |
/* If any of the constraints have a NULL value, then return no rows. |
|
|
5731 |
** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */ |
|
|
5732 |
pCur->mnValue = 1; |
|
|
5733 |
pCur->mxValue = 0; |
|
|
5734 |
break; |
|
|
5735 |
} |
|
|
5736 |
} |
|
|
5737 |
if( idxNum & 8 ){ |
|
|
5738 |
pCur->isDesc = 1; |
|
|
5739 |
pCur->iValue = pCur->mxValue; |
|
|
5740 |
if( pCur->iStep>0 ){ |
|
|
5741 |
pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep; |
|
|
5742 |
} |
|
|
5743 |
}else{ |
|
|
5744 |
pCur->isDesc = 0; |
|
|
5745 |
pCur->iValue = pCur->mnValue; |
|
|
5746 |
} |
|
|
5747 |
pCur->iRowid = 1; |
|
|
5748 |
return SQLITE_OK; |
|
|
5749 |
} |
|
|
5750 |
|
|
|
5751 |
/* |
|
|
5752 |
** SQLite will invoke this method one or more times while planning a query |
|
|
5753 |
** that uses the generate_series virtual table. This routine needs to create |
|
|
5754 |
** a query plan for each invocation and compute an estimated cost for that |
|
|
5755 |
** plan. |
|
|
5756 |
** |
|
|
5757 |
** In this implementation idxNum is used to represent the |
|
|
5758 |
** query plan. idxStr is unused. |
|
|
5759 |
** |
|
|
5760 |
** The query plan is represented by bits in idxNum: |
|
|
5761 |
** |
|
|
5762 |
** (1) start = $value -- constraint exists |
|
|
5763 |
** (2) stop = $value -- constraint exists |
|
|
5764 |
** (4) step = $value -- constraint exists |
|
|
5765 |
** (8) output in descending order |
|
|
5766 |
*/ |
|
|
5767 |
static int seriesBestIndex( |
|
|
5768 |
sqlite3_vtab *tabUnused, |
|
|
5769 |
sqlite3_index_info *pIdxInfo |
|
|
5770 |
){ |
|
|
5771 |
int i, j; /* Loop over constraints */ |
|
|
5772 |
int idxNum = 0; /* The query plan bitmask */ |
|
|
5773 |
int unusableMask = 0; /* Mask of unusable constraints */ |
|
|
5774 |
int nArg = 0; /* Number of arguments that seriesFilter() expects */ |
|
|
5775 |
int aIdx[3]; /* Constraints on start, stop, and step */ |
|
|
5776 |
const struct sqlite3_index_constraint *pConstraint; |
|
|
5777 |
|
|
|
5778 |
/* This implementation assumes that the start, stop, and step columns |
|
|
5779 |
** are the last three columns in the virtual table. */ |
|
|
5780 |
assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 ); |
|
|
5781 |
assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 ); |
|
|
5782 |
(void)tabUnused; |
|
|
5783 |
aIdx[0] = aIdx[1] = aIdx[2] = -1; |
|
|
5784 |
pConstraint = pIdxInfo->aConstraint; |
|
|
5785 |
for(i=0; inConstraint; i++, pConstraint++){ |
|
|
5786 |
int iCol; /* 0 for start, 1 for stop, 2 for step */ |
|
|
5787 |
int iMask; /* bitmask for those column */ |
|
|
5788 |
if( pConstraint->iColumn |
|
|
5789 |
iCol = pConstraint->iColumn - SERIES_COLUMN_START; |
|
|
5790 |
assert( iCol>=0 && iCol<=2 ); |
|
|
5791 |
iMask = 1 << iCol; |
|
|
5792 |
if( pConstraint->usable==0 ){ |
|
|
5793 |
unusableMask |= iMask; |
|
|
5794 |
continue; |
|
|
5795 |
}else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
|
|
5796 |
idxNum |= iMask; |
|
|
5797 |
aIdx[iCol] = i; |
|
|
5798 |
} |
|
|
5799 |
} |
|
|
5800 |
for(i=0; i<3; i++){ |
|
|
5801 |
if( (j = aIdx[i])>=0 ){ |
|
|
5802 |
pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg; |
|
|
5803 |
pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY; |
|
|
5804 |
} |
|
|
5805 |
} |
|
|
5806 |
if( (unusableMask & ~idxNum)!=0 ){ |
|
|
5807 |
/* The start, stop, and step columns are inputs. Therefore if there |
|
|
5808 |
** are unusable constraints on any of start, stop, or step then |
|
|
5809 |
** this plan is unusable */ |
|
|
5810 |
return SQLITE_CONSTRAINT; |
|
|
5811 |
} |
|
|
5812 |
if( (idxNum & 3)==3 ){ |
|
|
5813 |
/* Both start= and stop= boundaries are available. This is the |
|
|
5814 |
** the preferred case */ |
|
|
5815 |
pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0)); |
|
|
5816 |
pIdxInfo->estimatedRows = 1000; |
|
|
5817 |
if( pIdxInfo->nOrderBy==1 ){ |
|
|
5818 |
if( pIdxInfo->aOrderBy[0].desc ){ |
|
|
5819 |
idxNum |= 8; |
|
|
5820 |
}else{ |
|
|
5821 |
idxNum |= 16; |
|
|
5822 |
} |
|
|
5823 |
pIdxInfo->orderByConsumed = 1; |
|
|
5824 |
} |
|
|
5825 |
}else{ |
|
|
5826 |
/* If either boundary is missing, we have to generate a huge span |
|
|
5827 |
** of numbers. Make this case very expensive so that the query |
|
|
5828 |
** planner will work hard to avoid it. */ |
|
|
5829 |
pIdxInfo->estimatedRows = 2147483647; |
|
|
5830 |
} |
|
|
5831 |
pIdxInfo->idxNum = idxNum; |
|
|
5832 |
return SQLITE_OK; |
|
|
5833 |
} |
|
|
5834 |
|
|
|
5835 |
/* |
|
|
5836 |
** This following structure defines all the methods for the |
|
|
5837 |
** generate_series virtual table. |
|
|
5838 |
*/ |
|
|
5839 |
static sqlite3_module seriesModule = { |
|
|
5840 |
0, /* iVersion */ |
|
|
5841 |
0, /* xCreate */ |
|
|
5842 |
seriesConnect, /* xConnect */ |
|
|
5843 |
seriesBestIndex, /* xBestIndex */ |
|
|
5844 |
seriesDisconnect, /* xDisconnect */ |
|
|
5845 |
0, /* xDestroy */ |
|
|
5846 |
seriesOpen, /* xOpen - open a cursor */ |
|
|
5847 |
seriesClose, /* xClose - close a cursor */ |
|
|
5848 |
seriesFilter, /* xFilter - configure scan constraints */ |
|
|
5849 |
seriesNext, /* xNext - advance a cursor */ |
|
|
5850 |
seriesEof, /* xEof - check for end of scan */ |
|
|
5851 |
seriesColumn, /* xColumn - read data */ |
|
|
5852 |
seriesRowid, /* xRowid - read data */ |
|
|
5853 |
0, /* xUpdate */ |
|
|
5854 |
0, /* xBegin */ |
|
|
5855 |
0, /* xSync */ |
|
|
5856 |
0, /* xCommit */ |
|
|
5857 |
0, /* xRollback */ |
|
|
5858 |
0, /* xFindMethod */ |
|
|
5859 |
0, /* xRename */ |
|
|
5860 |
0, /* xSavepoint */ |
|
|
5861 |
0, /* xRelease */ |
|
|
5862 |
0, /* xRollbackTo */ |
|
|
5863 |
|
|
|
5864 |
}; |
|
|
5865 |
|
|
|
5866 |
#endif /* SQLITE_OMIT_VIRTUALTABLE */ |
|
|
5867 |
|
|
|
5868 |
#ifdef _WIN32 |
|
|
5869 |
|
|
|
5870 |
#endif |
|
|
5871 |
int sqlite3_series_init( |
|
|
5872 |
sqlite3 *db, |
|
|
5873 |
char **pzErrMsg, |
|
|
5874 |
const sqlite3_api_routines *pApi |
|
|
5875 |
){ |
|
|
5876 |
int rc = SQLITE_OK; |
|
|
5877 |
SQLITE_EXTENSION_INIT2(pApi); |
|
|
5878 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
5879 |
if( sqlite3_libversion_number()<3008012 ){ |
|
|
5880 |
*pzErrMsg = sqlite3_mprintf( |
|
|
5881 |
"generate_series() requires SQLite 3.8.12 or later"); |
|
|
5882 |
return SQLITE_ERROR; |
|
|
5883 |
} |
|
|
5884 |
rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0); |
|
|
5885 |
#endif |
|
|
5886 |
return rc; |
|
|
5887 |
} |
|
|
5888 |
|
|
|
5889 |
/************************* End ../ext/misc/series.c ********************/ |
|
|
5890 |
#ifdef SQLITE_HAVE_ZLIB |
|
|
5891 |
/************************* Begin ../ext/misc/zipfile.c ******************/ |
|
|
5892 |
/* |
|
|
5893 |
** 2017-12-26 |
|
|
5894 |
** |
|
|
5895 |
** The author disclaims copyright to this source code. In place of |
|
|
5896 |
** a legal notice, here is a blessing: |
|
|
5897 |
** |
|
|
5898 |
** May you do good and not evil. |
|
|
5899 |
** May you find forgiveness for yourself and forgive others. |
|
|
5900 |
** May you share freely, never taking more than you give. |
|
|
5901 |
** |
|
|
5902 |
****************************************************************************** |
|
|
5903 |
** |
|
|
5904 |
** This file implements a virtual table for reading and writing ZIP archive |
|
|
5905 |
** files. |
|
|
5906 |
** |
|
|
5907 |
** Usage example: |
|
|
5908 |
** |
|
|
5909 |
** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename); |
|
|
5910 |
** |
|
|
5911 |
** Current limitations: |
|
|
5912 |
** |
|
|
5913 |
** * No support for encryption |
|
|
5914 |
** * No support for ZIP archives spanning multiple files |
|
|
5915 |
** * No support for zip64 extensions |
|
|
5916 |
** * Only the "inflate/deflate" (zlib) compression method is supported |
|
|
5917 |
*/ |
|
|
5918 |
/* #include "sqlite3ext.h" */ |
|
|
5919 |
SQLITE_EXTENSION_INIT1 |
|
|
5920 |
#include |
|
|
5921 |
#include |
|
|
5922 |
#include |
|
|
5923 |
|
|
|
5924 |
#include |
|
|
5925 |
|
|
|
5926 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
5927 |
|
|
|
5928 |
#ifndef SQLITE_AMALGAMATION |
|
|
5929 |
|
|
|
5930 |
/* typedef sqlite3_int64 i64; */ |
|
|
5931 |
/* typedef unsigned char u8; */ |
|
|
5932 |
typedef unsigned short u16; |
|
|
5933 |
typedef unsigned long u32; |
|
|
5934 |
#define MIN(a,b) ((a)<(b) ? (a) : (b)) |
|
|
5935 |
|
|
|
5936 |
#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) |
|
|
5937 |
# define ALWAYS(X) (1) |
|
|
5938 |
# define NEVER(X) (0) |
|
|
5939 |
#elif !defined(NDEBUG) |
|
|
5940 |
# define ALWAYS(X) ((X)?1:(assert(0),0)) |
|
|
5941 |
# define NEVER(X) ((X)?(assert(0),1):0) |
|
|
5942 |
#else |
|
|
5943 |
# define ALWAYS(X) (X) |
|
|
5944 |
# define NEVER(X) (X) |
|
|
5945 |
#endif |
|
|
5946 |
|
|
|
5947 |
#endif /* SQLITE_AMALGAMATION */ |
|
|
5948 |
|
|
|
5949 |
/* |
|
|
5950 |
** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK. |
|
|
5951 |
** |
|
|
5952 |
** In some ways it would be better to obtain these values from system |
|
|
5953 |
** header files. But, the dependency is undesirable and (a) these |
|
|
5954 |
** have been stable for decades, (b) the values are part of POSIX and |
|
|
5955 |
** are also made explicit in [man stat], and (c) are part of the |
|
|
5956 |
** file format for zip archives. |
|
|
5957 |
*/ |
|
|
5958 |
#ifndef S_IFDIR |
|
|
5959 |
# define S_IFDIR 0040000 |
|
|
5960 |
#endif |
|
|
5961 |
#ifndef S_IFREG |
|
|
5962 |
# define S_IFREG 0100000 |
|
|
5963 |
#endif |
|
|
5964 |
#ifndef S_IFLNK |
|
|
5965 |
# define S_IFLNK 0120000 |
|
|
5966 |
#endif |
|
|
5967 |
|
|
|
5968 |
static const char ZIPFILE_SCHEMA[] = |
|
|
5969 |
"CREATE TABLE y(" |
|
|
5970 |
"name PRIMARY KEY," /* 0: Name of file in zip archive */ |
|
|
5971 |
"mode," /* 1: POSIX mode for file */ |
|
|
5972 |
"mtime," /* 2: Last modification time (secs since 1970)*/ |
|
|
5973 |
"sz," /* 3: Size of object */ |
|
|
5974 |
"rawdata," /* 4: Raw data */ |
|
|
5975 |
"data," /* 5: Uncompressed data */ |
|
|
5976 |
"method," /* 6: Compression method (integer) */ |
|
|
5977 |
"z HIDDEN" /* 7: Name of zip file */ |
|
|
5978 |
") WITHOUT ROWID;"; |
|
|
5979 |
|
|
|
5980 |
#define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */ |
|
|
5981 |
#define ZIPFILE_BUFFER_SIZE (64*1024) |
|
|
5982 |
|
|
|
5983 |
|
|
|
5984 |
/* |
|
|
5985 |
** Magic numbers used to read and write zip files. |
|
|
5986 |
** |
|
|
5987 |
** ZIPFILE_NEWENTRY_MADEBY: |
|
|
5988 |
** Use this value for the "version-made-by" field in new zip file |
|
|
5989 |
** entries. The upper byte indicates "unix", and the lower byte |
|
|
5990 |
** indicates that the zip file matches pkzip specification 3.0. |
|
|
5991 |
** This is what info-zip seems to do. |
|
|
5992 |
** |
|
|
5993 |
** ZIPFILE_NEWENTRY_REQUIRED: |
|
|
5994 |
** Value for "version-required-to-extract" field of new entries. |
|
|
5995 |
** Version 2.0 is required to support folders and deflate compression. |
|
|
5996 |
** |
|
|
5997 |
** ZIPFILE_NEWENTRY_FLAGS: |
|
|
5998 |
** Value for "general-purpose-bit-flags" field of new entries. Bit |
|
|
5999 |
** 11 means "utf-8 filename and comment". |
|
|
6000 |
** |
|
|
6001 |
** ZIPFILE_SIGNATURE_CDS: |
|
|
6002 |
** First 4 bytes of a valid CDS record. |
|
|
6003 |
** |
|
|
6004 |
** ZIPFILE_SIGNATURE_LFH: |
|
|
6005 |
** First 4 bytes of a valid LFH record. |
|
|
6006 |
** |
|
|
6007 |
** ZIPFILE_SIGNATURE_EOCD |
|
|
6008 |
** First 4 bytes of a valid EOCD record. |
|
|
6009 |
*/ |
|
|
6010 |
#define ZIPFILE_EXTRA_TIMESTAMP 0x5455 |
|
|
6011 |
#define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30) |
|
|
6012 |
#define ZIPFILE_NEWENTRY_REQUIRED 20 |
|
|
6013 |
#define ZIPFILE_NEWENTRY_FLAGS 0x800 |
|
|
6014 |
#define ZIPFILE_SIGNATURE_CDS 0x02014b50 |
|
|
6015 |
#define ZIPFILE_SIGNATURE_LFH 0x04034b50 |
|
|
6016 |
#define ZIPFILE_SIGNATURE_EOCD 0x06054b50 |
|
|
6017 |
|
|
|
6018 |
/* |
|
|
6019 |
** The sizes of the fixed-size part of each of the three main data |
|
|
6020 |
** structures in a zip archive. |
|
|
6021 |
*/ |
|
|
6022 |
#define ZIPFILE_LFH_FIXED_SZ 30 |
|
|
6023 |
#define ZIPFILE_EOCD_FIXED_SZ 22 |
|
|
6024 |
#define ZIPFILE_CDS_FIXED_SZ 46 |
|
|
6025 |
|
|
|
6026 |
/* |
|
|
6027 |
*** 4.3.16 End of central directory record: |
|
|
6028 |
*** |
|
|
6029 |
*** end of central dir signature 4 bytes (0x06054b50) |
|
|
6030 |
*** number of this disk 2 bytes |
|
|
6031 |
*** number of the disk with the |
|
|
6032 |
*** start of the central directory 2 bytes |
|
|
6033 |
*** total number of entries in the |
|
|
6034 |
*** central directory on this disk 2 bytes |
|
|
6035 |
*** total number of entries in |
|
|
6036 |
*** the central directory 2 bytes |
|
|
6037 |
*** size of the central directory 4 bytes |
|
|
6038 |
*** offset of start of central |
|
|
6039 |
*** directory with respect to |
|
|
6040 |
*** the starting disk number 4 bytes |
|
|
6041 |
*** .ZIP file comment length 2 bytes |
|
|
6042 |
*** .ZIP file comment (variable size) |
|
|
6043 |
*/ |
|
|
6044 |
typedef struct ZipfileEOCD ZipfileEOCD; |
|
|
6045 |
struct ZipfileEOCD { |
|
|
6046 |
u16 iDisk; |
|
|
6047 |
u16 iFirstDisk; |
|
|
6048 |
u16 nEntry; |
|
|
6049 |
u16 nEntryTotal; |
|
|
6050 |
u32 nSize; |
|
|
6051 |
u32 iOffset; |
|
|
6052 |
}; |
|
|
6053 |
|
|
|
6054 |
/* |
|
|
6055 |
*** 4.3.12 Central directory structure: |
|
|
6056 |
*** |
|
|
6057 |
*** ... |
|
|
6058 |
*** |
|
|
6059 |
*** central file header signature 4 bytes (0x02014b50) |
|
|
6060 |
*** version made by 2 bytes |
|
|
6061 |
*** version needed to extract 2 bytes |
|
|
6062 |
*** general purpose bit flag 2 bytes |
|
|
6063 |
*** compression method 2 bytes |
|
|
6064 |
*** last mod file time 2 bytes |
|
|
6065 |
*** last mod file date 2 bytes |
|
|
6066 |
*** crc-32 4 bytes |
|
|
6067 |
*** compressed size 4 bytes |
|
|
6068 |
*** uncompressed size 4 bytes |
|
|
6069 |
*** file name length 2 bytes |
|
|
6070 |
*** extra field length 2 bytes |
|
|
6071 |
*** file comment length 2 bytes |
|
|
6072 |
*** disk number start 2 bytes |
|
|
6073 |
*** internal file attributes 2 bytes |
|
|
6074 |
*** external file attributes 4 bytes |
|
|
6075 |
*** relative offset of local header 4 bytes |
|
|
6076 |
*/ |
|
|
6077 |
typedef struct ZipfileCDS ZipfileCDS; |
|
|
6078 |
struct ZipfileCDS { |
|
|
6079 |
u16 iVersionMadeBy; |
|
|
6080 |
u16 iVersionExtract; |
|
|
6081 |
u16 flags; |
|
|
6082 |
u16 iCompression; |
|
|
6083 |
u16 mTime; |
|
|
6084 |
u16 mDate; |
|
|
6085 |
u32 crc32; |
|
|
6086 |
u32 szCompressed; |
|
|
6087 |
u32 szUncompressed; |
|
|
6088 |
u16 nFile; |
|
|
6089 |
u16 nExtra; |
|
|
6090 |
u16 nComment; |
|
|
6091 |
u16 iDiskStart; |
|
|
6092 |
u16 iInternalAttr; |
|
|
6093 |
u32 iExternalAttr; |
|
|
6094 |
u32 iOffset; |
|
|
6095 |
char *zFile; /* Filename (sqlite3_malloc()) */ |
|
|
6096 |
}; |
|
|
6097 |
|
|
|
6098 |
/* |
|
|
6099 |
*** 4.3.7 Local file header: |
|
|
6100 |
*** |
|
|
6101 |
*** local file header signature 4 bytes (0x04034b50) |
|
|
6102 |
*** version needed to extract 2 bytes |
|
|
6103 |
*** general purpose bit flag 2 bytes |
|
|
6104 |
*** compression method 2 bytes |
|
|
6105 |
*** last mod file time 2 bytes |
|
|
6106 |
*** last mod file date 2 bytes |
|
|
6107 |
*** crc-32 4 bytes |
|
|
6108 |
*** compressed size 4 bytes |
|
|
6109 |
*** uncompressed size 4 bytes |
|
|
6110 |
*** file name length 2 bytes |
|
|
6111 |
*** extra field length 2 bytes |
|
|
6112 |
*** |
|
|
6113 |
*/ |
|
|
6114 |
typedef struct ZipfileLFH ZipfileLFH; |
|
|
6115 |
struct ZipfileLFH { |
|
|
6116 |
u16 iVersionExtract; |
|
|
6117 |
u16 flags; |
|
|
6118 |
u16 iCompression; |
|
|
6119 |
u16 mTime; |
|
|
6120 |
u16 mDate; |
|
|
6121 |
u32 crc32; |
|
|
6122 |
u32 szCompressed; |
|
|
6123 |
u32 szUncompressed; |
|
|
6124 |
u16 nFile; |
|
|
6125 |
u16 nExtra; |
|
|
6126 |
}; |
|
|
6127 |
|
|
|
6128 |
typedef struct ZipfileEntry ZipfileEntry; |
|
|
6129 |
struct ZipfileEntry { |
|
|
6130 |
ZipfileCDS cds; /* Parsed CDS record */ |
|
|
6131 |
u32 mUnixTime; /* Modification time, in UNIX format */ |
|
|
6132 |
u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */ |
|
|
6133 |
i64 iDataOff; /* Offset to data in file (if aData==0) */ |
|
|
6134 |
u8 *aData; /* cds.szCompressed bytes of compressed data */ |
|
|
6135 |
ZipfileEntry *pNext; /* Next element in in-memory CDS */ |
|
|
6136 |
}; |
|
|
6137 |
|
|
|
6138 |
/* |
|
|
6139 |
** Cursor type for zipfile tables. |
|
|
6140 |
*/ |
|
|
6141 |
typedef struct ZipfileCsr ZipfileCsr; |
|
|
6142 |
struct ZipfileCsr { |
|
|
6143 |
sqlite3_vtab_cursor base; /* Base class - must be first */ |
|
|
6144 |
i64 iId; /* Cursor ID */ |
|
|
6145 |
u8 bEof; /* True when at EOF */ |
|
|
6146 |
u8 bNoop; /* If next xNext() call is no-op */ |
|
|
6147 |
|
|
|
6148 |
/* Used outside of write transactions */ |
|
|
6149 |
FILE *pFile; /* Zip file */ |
|
|
6150 |
i64 iNextOff; /* Offset of next record in central directory */ |
|
|
6151 |
ZipfileEOCD eocd; /* Parse of central directory record */ |
|
|
6152 |
|
|
|
6153 |
ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */ |
|
|
6154 |
ZipfileEntry *pCurrent; /* Current entry */ |
|
|
6155 |
ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */ |
|
|
6156 |
}; |
|
|
6157 |
|
|
|
6158 |
typedef struct ZipfileTab ZipfileTab; |
|
|
6159 |
struct ZipfileTab { |
|
|
6160 |
sqlite3_vtab base; /* Base class - must be first */ |
|
|
6161 |
char *zFile; /* Zip file this table accesses (may be NULL) */ |
|
|
6162 |
sqlite3 *db; /* Host database connection */ |
|
|
6163 |
u8 *aBuffer; /* Temporary buffer used for various tasks */ |
|
|
6164 |
|
|
|
6165 |
ZipfileCsr *pCsrList; /* List of cursors */ |
|
|
6166 |
i64 iNextCsrid; |
|
|
6167 |
|
|
|
6168 |
/* The following are used by write transactions only */ |
|
|
6169 |
ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */ |
|
|
6170 |
ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */ |
|
|
6171 |
FILE *pWriteFd; /* File handle open on zip archive */ |
|
|
6172 |
i64 szCurrent; /* Current size of zip archive */ |
|
|
6173 |
i64 szOrig; /* Size of archive at start of transaction */ |
|
|
6174 |
}; |
|
|
6175 |
|
|
|
6176 |
/* |
|
|
6177 |
** Set the error message contained in context ctx to the results of |
|
|
6178 |
** vprintf(zFmt, ...). |
|
|
6179 |
*/ |
|
|
6180 |
static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){ |
|
|
6181 |
char *zMsg = 0; |
|
|
6182 |
va_list ap; |
|
|
6183 |
va_start(ap, zFmt); |
|
|
6184 |
zMsg = sqlite3_vmprintf(zFmt, ap); |
|
|
6185 |
sqlite3_result_error(ctx, zMsg, -1); |
|
|
6186 |
sqlite3_free(zMsg); |
|
|
6187 |
va_end(ap); |
|
|
6188 |
} |
|
|
6189 |
|
|
|
6190 |
/* |
|
|
6191 |
** If string zIn is quoted, dequote it in place. Otherwise, if the string |
|
|
6192 |
** is not quoted, do nothing. |
|
|
6193 |
*/ |
|
|
6194 |
static void zipfileDequote(char *zIn){ |
|
|
6195 |
char q = zIn[0]; |
|
|
6196 |
if( q=='"' || q=='\'' || q=='`' || q=='[' ){ |
|
|
6197 |
int iIn = 1; |
|
|
6198 |
int iOut = 0; |
|
|
6199 |
if( q=='[' ) q = ']'; |
|
|
6200 |
while( ALWAYS(zIn[iIn]) ){ |
|
|
6201 |
char c = zIn[iIn++]; |
|
|
6202 |
if( c==q && zIn[iIn++]!=q ) break; |
|
|
6203 |
zIn[iOut++] = c; |
|
|
6204 |
} |
|
|
6205 |
zIn[iOut] = '\0'; |
|
|
6206 |
} |
|
|
6207 |
} |
|
|
6208 |
|
|
|
6209 |
/* |
|
|
6210 |
** Construct a new ZipfileTab virtual table object. |
|
|
6211 |
** |
|
|
6212 |
** argv[0] -> module name ("zipfile") |
|
|
6213 |
** argv[1] -> database name |
|
|
6214 |
** argv[2] -> table name |
|
|
6215 |
** argv[...] -> "column name" and other module argument fields. |
|
|
6216 |
*/ |
|
|
6217 |
static int zipfileConnect( |
|
|
6218 |
sqlite3 *db, |
|
|
6219 |
void *pAux, |
|
|
6220 |
int argc, const char *const*argv, |
|
|
6221 |
sqlite3_vtab **ppVtab, |
|
|
6222 |
char **pzErr |
|
|
6223 |
){ |
|
|
6224 |
int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE; |
|
|
6225 |
int nFile = 0; |
|
|
6226 |
const char *zFile = 0; |
|
|
6227 |
ZipfileTab *pNew = 0; |
|
|
6228 |
int rc; |
|
|
6229 |
|
|
|
6230 |
/* If the table name is not "zipfile", require that the argument be |
|
|
6231 |
** specified. This stops zipfile tables from being created as: |
|
|
6232 |
** |
|
|
6233 |
** CREATE VIRTUAL TABLE zzz USING zipfile(); |
|
|
6234 |
** |
|
|
6235 |
** It does not prevent: |
|
|
6236 |
** |
|
|
6237 |
** CREATE VIRTUAL TABLE zipfile USING zipfile(); |
|
|
6238 |
*/ |
|
|
6239 |
assert( 0==sqlite3_stricmp(argv[0], "zipfile") ); |
|
|
6240 |
if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){ |
|
|
6241 |
*pzErr = sqlite3_mprintf("zipfile constructor requires one argument"); |
|
|
6242 |
return SQLITE_ERROR; |
|
|
6243 |
} |
|
|
6244 |
|
|
|
6245 |
if( argc>3 ){ |
|
|
6246 |
zFile = argv[3]; |
|
|
6247 |
nFile = (int)strlen(zFile)+1; |
|
|
6248 |
} |
|
|
6249 |
|
|
|
6250 |
rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA); |
|
|
6251 |
if( rc==SQLITE_OK ){ |
|
|
6252 |
pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile); |
|
|
6253 |
if( pNew==0 ) return SQLITE_NOMEM; |
|
|
6254 |
memset(pNew, 0, nByte+nFile); |
|
|
6255 |
pNew->db = db; |
|
|
6256 |
pNew->aBuffer = (u8*)&pNew[1]; |
|
|
6257 |
if( zFile ){ |
|
|
6258 |
pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE]; |
|
|
6259 |
memcpy(pNew->zFile, zFile, nFile); |
|
|
6260 |
zipfileDequote(pNew->zFile); |
|
|
6261 |
} |
|
|
6262 |
} |
|
|
6263 |
sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); |
|
|
6264 |
*ppVtab = (sqlite3_vtab*)pNew; |
|
|
6265 |
return rc; |
|
|
6266 |
} |
|
|
6267 |
|
|
|
6268 |
/* |
|
|
6269 |
** Free the ZipfileEntry structure indicated by the only argument. |
|
|
6270 |
*/ |
|
|
6271 |
static void zipfileEntryFree(ZipfileEntry *p){ |
|
|
6272 |
if( p ){ |
|
|
6273 |
sqlite3_free(p->cds.zFile); |
|
|
6274 |
sqlite3_free(p); |
|
|
6275 |
} |
|
|
6276 |
} |
|
|
6277 |
|
|
|
6278 |
/* |
|
|
6279 |
** Release resources that should be freed at the end of a write |
|
|
6280 |
** transaction. |
|
|
6281 |
*/ |
|
|
6282 |
static void zipfileCleanupTransaction(ZipfileTab *pTab){ |
|
|
6283 |
ZipfileEntry *pEntry; |
|
|
6284 |
ZipfileEntry *pNext; |
|
|
6285 |
|
|
|
6286 |
if( pTab->pWriteFd ){ |
|
|
6287 |
fclose(pTab->pWriteFd); |
|
|
6288 |
pTab->pWriteFd = 0; |
|
|
6289 |
} |
|
|
6290 |
for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){ |
|
|
6291 |
pNext = pEntry->pNext; |
|
|
6292 |
zipfileEntryFree(pEntry); |
|
|
6293 |
} |
|
|
6294 |
pTab->pFirstEntry = 0; |
|
|
6295 |
pTab->pLastEntry = 0; |
|
|
6296 |
pTab->szCurrent = 0; |
|
|
6297 |
pTab->szOrig = 0; |
|
|
6298 |
} |
|
|
6299 |
|
|
|
6300 |
/* |
|
|
6301 |
** This method is the destructor for zipfile vtab objects. |
|
|
6302 |
*/ |
|
|
6303 |
static int zipfileDisconnect(sqlite3_vtab *pVtab){ |
|
|
6304 |
zipfileCleanupTransaction((ZipfileTab*)pVtab); |
|
|
6305 |
sqlite3_free(pVtab); |
|
|
6306 |
return SQLITE_OK; |
|
|
6307 |
} |
|
|
6308 |
|
|
|
6309 |
/* |
|
|
6310 |
** Constructor for a new ZipfileCsr object. |
|
|
6311 |
*/ |
|
|
6312 |
static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){ |
|
|
6313 |
ZipfileTab *pTab = (ZipfileTab*)p; |
|
|
6314 |
ZipfileCsr *pCsr; |
|
|
6315 |
pCsr = sqlite3_malloc(sizeof(*pCsr)); |
|
|
6316 |
*ppCsr = (sqlite3_vtab_cursor*)pCsr; |
|
|
6317 |
if( pCsr==0 ){ |
|
|
6318 |
return SQLITE_NOMEM; |
|
|
6319 |
} |
|
|
6320 |
memset(pCsr, 0, sizeof(*pCsr)); |
|
|
6321 |
pCsr->iId = ++pTab->iNextCsrid; |
|
|
6322 |
pCsr->pCsrNext = pTab->pCsrList; |
|
|
6323 |
pTab->pCsrList = pCsr; |
|
|
6324 |
return SQLITE_OK; |
|
|
6325 |
} |
|
|
6326 |
|
|
|
6327 |
/* |
|
|
6328 |
** Reset a cursor back to the state it was in when first returned |
|
|
6329 |
** by zipfileOpen(). |
|
|
6330 |
*/ |
|
|
6331 |
static void zipfileResetCursor(ZipfileCsr *pCsr){ |
|
|
6332 |
ZipfileEntry *p; |
|
|
6333 |
ZipfileEntry *pNext; |
|
|
6334 |
|
|
|
6335 |
pCsr->bEof = 0; |
|
|
6336 |
if( pCsr->pFile ){ |
|
|
6337 |
fclose(pCsr->pFile); |
|
|
6338 |
pCsr->pFile = 0; |
|
|
6339 |
zipfileEntryFree(pCsr->pCurrent); |
|
|
6340 |
pCsr->pCurrent = 0; |
|
|
6341 |
} |
|
|
6342 |
|
|
|
6343 |
for(p=pCsr->pFreeEntry; p; p=pNext){ |
|
|
6344 |
pNext = p->pNext; |
|
|
6345 |
zipfileEntryFree(p); |
|
|
6346 |
} |
|
|
6347 |
} |
|
|
6348 |
|
|
|
6349 |
/* |
|
|
6350 |
** Destructor for an ZipfileCsr. |
|
|
6351 |
*/ |
|
|
6352 |
static int zipfileClose(sqlite3_vtab_cursor *cur){ |
|
|
6353 |
ZipfileCsr *pCsr = (ZipfileCsr*)cur; |
|
|
6354 |
ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab); |
|
|
6355 |
ZipfileCsr **pp; |
|
|
6356 |
zipfileResetCursor(pCsr); |
|
|
6357 |
|
|
|
6358 |
/* Remove this cursor from the ZipfileTab.pCsrList list. */ |
|
|
6359 |
for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext)); |
|
|
6360 |
*pp = pCsr->pCsrNext; |
|
|
6361 |
|
|
|
6362 |
sqlite3_free(pCsr); |
|
|
6363 |
return SQLITE_OK; |
|
|
6364 |
} |
|
|
6365 |
|
|
|
6366 |
/* |
|
|
6367 |
** Set the error message for the virtual table associated with cursor |
|
|
6368 |
** pCsr to the results of vprintf(zFmt, ...). |
|
|
6369 |
*/ |
|
|
6370 |
static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){ |
|
|
6371 |
va_list ap; |
|
|
6372 |
va_start(ap, zFmt); |
|
|
6373 |
sqlite3_free(pTab->base.zErrMsg); |
|
|
6374 |
pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap); |
|
|
6375 |
va_end(ap); |
|
|
6376 |
} |
|
|
6377 |
static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){ |
|
|
6378 |
va_list ap; |
|
|
6379 |
va_start(ap, zFmt); |
|
|
6380 |
sqlite3_free(pCsr->base.pVtab->zErrMsg); |
|
|
6381 |
pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); |
|
|
6382 |
va_end(ap); |
|
|
6383 |
} |
|
|
6384 |
|
|
|
6385 |
/* |
|
|
6386 |
** Read nRead bytes of data from offset iOff of file pFile into buffer |
|
|
6387 |
** aRead[]. Return SQLITE_OK if successful, or an SQLite error code |
|
|
6388 |
** otherwise. |
|
|
6389 |
** |
|
|
6390 |
** If an error does occur, output variable (*pzErrmsg) may be set to point |
|
|
6391 |
** to an English language error message. It is the responsibility of the |
|
|
6392 |
** caller to eventually free this buffer using |
|
|
6393 |
** sqlite3_free(). |
|
|
6394 |
*/ |
|
|
6395 |
static int zipfileReadData( |
|
|
6396 |
FILE *pFile, /* Read from this file */ |
|
|
6397 |
u8 *aRead, /* Read into this buffer */ |
|
|
6398 |
int nRead, /* Number of bytes to read */ |
|
|
6399 |
i64 iOff, /* Offset to read from */ |
|
|
6400 |
char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */ |
|
|
6401 |
){ |
|
|
6402 |
size_t n; |
|
|
6403 |
fseek(pFile, (long)iOff, SEEK_SET); |
|
|
6404 |
n = fread(aRead, 1, nRead, pFile); |
|
|
6405 |
if( (int)n!=nRead ){ |
|
|
6406 |
*pzErrmsg = sqlite3_mprintf("error in fread()"); |
|
|
6407 |
return SQLITE_ERROR; |
|
|
6408 |
} |
|
|
6409 |
return SQLITE_OK; |
|
|
6410 |
} |
|
|
6411 |
|
|
|
6412 |
static int zipfileAppendData( |
|
|
6413 |
ZipfileTab *pTab, |
|
|
6414 |
const u8 *aWrite, |
|
|
6415 |
int nWrite |
|
|
6416 |
){ |
|
|
6417 |
if( nWrite>0 ){ |
|
|
6418 |
size_t n = nWrite; |
|
|
6419 |
fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET); |
|
|
6420 |
n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd); |
|
|
6421 |
if( (int)n!=nWrite ){ |
|
|
6422 |
pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()"); |
|
|
6423 |
return SQLITE_ERROR; |
|
|
6424 |
} |
|
|
6425 |
pTab->szCurrent += nWrite; |
|
|
6426 |
} |
|
|
6427 |
return SQLITE_OK; |
|
|
6428 |
} |
|
|
6429 |
|
|
|
6430 |
/* |
|
|
6431 |
** Read and return a 16-bit little-endian unsigned integer from buffer aBuf. |
|
|
6432 |
*/ |
|
|
6433 |
static u16 zipfileGetU16(const u8 *aBuf){ |
|
|
6434 |
return (aBuf[1] << 8) + aBuf[0]; |
|
|
6435 |
} |
|
|
6436 |
|
|
|
6437 |
/* |
|
|
6438 |
** Read and return a 32-bit little-endian unsigned integer from buffer aBuf. |
|
|
6439 |
*/ |
|
|
6440 |
static u32 zipfileGetU32(const u8 *aBuf){ |
|
|
6441 |
return ((u32)(aBuf[3]) << 24) |
|
|
6442 |
+ ((u32)(aBuf[2]) << 16) |
|
|
6443 |
+ ((u32)(aBuf[1]) << 8) |
|
|
6444 |
+ ((u32)(aBuf[0]) << 0); |
|
|
6445 |
} |
|
|
6446 |
|
|
|
6447 |
/* |
|
|
6448 |
** Write a 16-bit little endiate integer into buffer aBuf. |
|
|
6449 |
*/ |
|
|
6450 |
static void zipfilePutU16(u8 *aBuf, u16 val){ |
|
|
6451 |
aBuf[0] = val & 0xFF; |
|
|
6452 |
aBuf[1] = (val>>8) & 0xFF; |
|
|
6453 |
} |
|
|
6454 |
|
|
|
6455 |
/* |
|
|
6456 |
** Write a 32-bit little endiate integer into buffer aBuf. |
|
|
6457 |
*/ |
|
|
6458 |
static void zipfilePutU32(u8 *aBuf, u32 val){ |
|
|
6459 |
aBuf[0] = val & 0xFF; |
|
|
6460 |
aBuf[1] = (val>>8) & 0xFF; |
|
|
6461 |
aBuf[2] = (val>>16) & 0xFF; |
|
|
6462 |
aBuf[3] = (val>>24) & 0xFF; |
|
|
6463 |
} |
|
|
6464 |
|
|
|
6465 |
#define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) ) |
|
|
6466 |
#define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) ) |
|
|
6467 |
|
|
|
6468 |
#define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; } |
|
|
6469 |
#define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; } |
|
|
6470 |
|
|
|
6471 |
/* |
|
|
6472 |
** Magic numbers used to read CDS records. |
|
|
6473 |
*/ |
|
|
6474 |
#define ZIPFILE_CDS_NFILE_OFF 28 |
|
|
6475 |
#define ZIPFILE_CDS_SZCOMPRESSED_OFF 20 |
|
|
6476 |
|
|
|
6477 |
/* |
|
|
6478 |
** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR |
|
|
6479 |
** if the record is not well-formed, or SQLITE_OK otherwise. |
|
|
6480 |
*/ |
|
|
6481 |
static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){ |
|
|
6482 |
u8 *aRead = aBuf; |
|
|
6483 |
u32 sig = zipfileRead32(aRead); |
|
|
6484 |
int rc = SQLITE_OK; |
|
|
6485 |
if( sig!=ZIPFILE_SIGNATURE_CDS ){ |
|
|
6486 |
rc = SQLITE_ERROR; |
|
|
6487 |
}else{ |
|
|
6488 |
pCDS->iVersionMadeBy = zipfileRead16(aRead); |
|
|
6489 |
pCDS->iVersionExtract = zipfileRead16(aRead); |
|
|
6490 |
pCDS->flags = zipfileRead16(aRead); |
|
|
6491 |
pCDS->iCompression = zipfileRead16(aRead); |
|
|
6492 |
pCDS->mTime = zipfileRead16(aRead); |
|
|
6493 |
pCDS->mDate = zipfileRead16(aRead); |
|
|
6494 |
pCDS->crc32 = zipfileRead32(aRead); |
|
|
6495 |
pCDS->szCompressed = zipfileRead32(aRead); |
|
|
6496 |
pCDS->szUncompressed = zipfileRead32(aRead); |
|
|
6497 |
assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] ); |
|
|
6498 |
pCDS->nFile = zipfileRead16(aRead); |
|
|
6499 |
pCDS->nExtra = zipfileRead16(aRead); |
|
|
6500 |
pCDS->nComment = zipfileRead16(aRead); |
|
|
6501 |
pCDS->iDiskStart = zipfileRead16(aRead); |
|
|
6502 |
pCDS->iInternalAttr = zipfileRead16(aRead); |
|
|
6503 |
pCDS->iExternalAttr = zipfileRead32(aRead); |
|
|
6504 |
pCDS->iOffset = zipfileRead32(aRead); |
|
|
6505 |
assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] ); |
|
|
6506 |
} |
|
|
6507 |
|
|
|
6508 |
return rc; |
|
|
6509 |
} |
|
|
6510 |
|
|
|
6511 |
/* |
|
|
6512 |
** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR |
|
|
6513 |
** if the record is not well-formed, or SQLITE_OK otherwise. |
|
|
6514 |
*/ |
|
|
6515 |
static int zipfileReadLFH( |
|
|
6516 |
u8 *aBuffer, |
|
|
6517 |
ZipfileLFH *pLFH |
|
|
6518 |
){ |
|
|
6519 |
u8 *aRead = aBuffer; |
|
|
6520 |
int rc = SQLITE_OK; |
|
|
6521 |
|
|
|
6522 |
u32 sig = zipfileRead32(aRead); |
|
|
6523 |
if( sig!=ZIPFILE_SIGNATURE_LFH ){ |
|
|
6524 |
rc = SQLITE_ERROR; |
|
|
6525 |
}else{ |
|
|
6526 |
pLFH->iVersionExtract = zipfileRead16(aRead); |
|
|
6527 |
pLFH->flags = zipfileRead16(aRead); |
|
|
6528 |
pLFH->iCompression = zipfileRead16(aRead); |
|
|
6529 |
pLFH->mTime = zipfileRead16(aRead); |
|
|
6530 |
pLFH->mDate = zipfileRead16(aRead); |
|
|
6531 |
pLFH->crc32 = zipfileRead32(aRead); |
|
|
6532 |
pLFH->szCompressed = zipfileRead32(aRead); |
|
|
6533 |
pLFH->szUncompressed = zipfileRead32(aRead); |
|
|
6534 |
pLFH->nFile = zipfileRead16(aRead); |
|
|
6535 |
pLFH->nExtra = zipfileRead16(aRead); |
|
|
6536 |
} |
|
|
6537 |
return rc; |
|
|
6538 |
} |
|
|
6539 |
|
|
|
6540 |
|
|
|
6541 |
/* |
|
|
6542 |
** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields. |
|
|
6543 |
** Scan through this buffer to find an "extra-timestamp" field. If one |
|
|
6544 |
** exists, extract the 32-bit modification-timestamp from it and store |
|
|
6545 |
** the value in output parameter *pmTime. |
|
|
6546 |
** |
|
|
6547 |
** Zero is returned if no extra-timestamp record could be found (and so |
|
|
6548 |
** *pmTime is left unchanged), or non-zero otherwise. |
|
|
6549 |
** |
|
|
6550 |
** The general format of an extra field is: |
|
|
6551 |
** |
|
|
6552 |
** Header ID 2 bytes |
|
|
6553 |
** Data Size 2 bytes |
|
|
6554 |
** Data N bytes |
|
|
6555 |
*/ |
|
|
6556 |
static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){ |
|
|
6557 |
int ret = 0; |
|
|
6558 |
u8 *p = aExtra; |
|
|
6559 |
u8 *pEnd = &aExtra[nExtra]; |
|
|
6560 |
|
|
|
6561 |
while( p |
|
|
6562 |
u16 id = zipfileRead16(p); |
|
|
6563 |
u16 nByte = zipfileRead16(p); |
|
|
6564 |
|
|
|
6565 |
switch( id ){ |
|
|
6566 |
case ZIPFILE_EXTRA_TIMESTAMP: { |
|
|
6567 |
u8 b = p[0]; |
|
|
6568 |
if( b & 0x01 ){ /* 0x01 -> modtime is present */ |
|
|
6569 |
*pmTime = zipfileGetU32(&p[1]); |
|
|
6570 |
ret = 1; |
|
|
6571 |
} |
|
|
6572 |
break; |
|
|
6573 |
} |
|
|
6574 |
} |
|
|
6575 |
|
|
|
6576 |
p += nByte; |
|
|
6577 |
} |
|
|
6578 |
return ret; |
|
|
6579 |
} |
|
|
6580 |
|
|
|
6581 |
/* |
|
|
6582 |
** Convert the standard MS-DOS timestamp stored in the mTime and mDate |
|
|
6583 |
** fields of the CDS structure passed as the only argument to a 32-bit |
|
|
6584 |
** UNIX seconds-since-the-epoch timestamp. Return the result. |
|
|
6585 |
** |
|
|
6586 |
** "Standard" MS-DOS time format: |
|
|
6587 |
** |
|
|
6588 |
** File modification time: |
|
|
6589 |
** Bits 00-04: seconds divided by 2 |
|
|
6590 |
** Bits 05-10: minute |
|
|
6591 |
** Bits 11-15: hour |
|
|
6592 |
** File modification date: |
|
|
6593 |
** Bits 00-04: day |
|
|
6594 |
** Bits 05-08: month (1-12) |
|
|
6595 |
** Bits 09-15: years from 1980 |
|
|
6596 |
** |
|
|
6597 |
** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx |
|
|
6598 |
*/ |
|
|
6599 |
static u32 zipfileMtime(ZipfileCDS *pCDS){ |
|
|
6600 |
int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F)); |
|
|
6601 |
int M = ((pCDS->mDate >> 5) & 0x0F); |
|
|
6602 |
int D = (pCDS->mDate & 0x1F); |
|
|
6603 |
int B = -13; |
|
|
6604 |
|
|
|
6605 |
int sec = (pCDS->mTime & 0x1F)*2; |
|
|
6606 |
int min = (pCDS->mTime >> 5) & 0x3F; |
|
|
6607 |
int hr = (pCDS->mTime >> 11) & 0x1F; |
|
|
6608 |
i64 JD; |
|
|
6609 |
|
|
|
6610 |
/* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */ |
|
|
6611 |
|
|
|
6612 |
/* Calculate the JD in seconds for noon on the day in question */ |
|
|
6613 |
if( M<3 ){ |
|
|
6614 |
Y = Y-1; |
|
|
6615 |
M = M+12; |
|
|
6616 |
} |
|
|
6617 |
JD = (i64)(24*60*60) * ( |
|
|
6618 |
(int)(365.25 * (Y + 4716)) |
|
|
6619 |
+ (int)(30.6001 * (M + 1)) |
|
|
6620 |
+ D + B - 1524 |
|
|
6621 |
); |
|
|
6622 |
|
|
|
6623 |
/* Correct the JD for the time within the day */ |
|
|
6624 |
JD += (hr-12) * 3600 + min * 60 + sec; |
|
|
6625 |
|
|
|
6626 |
/* Convert JD to unix timestamp (the JD epoch is 2440587.5) */ |
|
|
6627 |
return (u32)(JD - (i64)(24405875) * 24*60*6); |
|
|
6628 |
} |
|
|
6629 |
|
|
|
6630 |
/* |
|
|
6631 |
** The opposite of zipfileMtime(). This function populates the mTime and |
|
|
6632 |
** mDate fields of the CDS structure passed as the first argument according |
|
|
6633 |
** to the UNIX timestamp value passed as the second. |
|
|
6634 |
*/ |
|
|
6635 |
static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){ |
|
|
6636 |
/* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */ |
|
|
6637 |
i64 JD = (i64)2440588 + mUnixTime / (24*60*60); |
|
|
6638 |
|
|
|
6639 |
int A, B, C, D, E; |
|
|
6640 |
int yr, mon, day; |
|
|
6641 |
int hr, min, sec; |
|
|
6642 |
|
|
|
6643 |
A = (int)((JD - 1867216.25)/36524.25); |
|
|
6644 |
A = (int)(JD + 1 + A - (A/4)); |
|
|
6645 |
B = A + 1524; |
|
|
6646 |
C = (int)((B - 122.1)/365.25); |
|
|
6647 |
D = (36525*(C&32767))/100; |
|
|
6648 |
E = (int)((B-D)/30.6001); |
|
|
6649 |
|
|
|
6650 |
day = B - D - (int)(30.6001*E); |
|
|
6651 |
mon = (E<14 ? E-1 : E-13); |
|
|
6652 |
yr = mon>2 ? C-4716 : C-4715; |
|
|
6653 |
|
|
|
6654 |
hr = (mUnixTime % (24*60*60)) / (60*60); |
|
|
6655 |
min = (mUnixTime % (60*60)) / 60; |
|
|
6656 |
sec = (mUnixTime % 60); |
|
|
6657 |
|
|
|
6658 |
if( yr>=1980 ){ |
|
|
6659 |
pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9)); |
|
|
6660 |
pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11)); |
|
|
6661 |
}else{ |
|
|
6662 |
pCds->mDate = pCds->mTime = 0; |
|
|
6663 |
} |
|
|
6664 |
|
|
|
6665 |
assert( mUnixTime<315507600 |
|
|
6666 |
|| mUnixTime==zipfileMtime(pCds) |
|
|
6667 |
|| ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) |
|
|
6668 |
/* || (mUnixTime % 2) */ |
|
|
6669 |
); |
|
|
6670 |
} |
|
|
6671 |
|
|
|
6672 |
/* |
|
|
6673 |
** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in |
|
|
6674 |
** size) containing an entire zip archive image. Or, if aBlob is NULL, |
|
|
6675 |
** then pFile is a file-handle open on a zip file. In either case, this |
|
|
6676 |
** function creates a ZipfileEntry object based on the zip archive entry |
|
|
6677 |
** for which the CDS record is at offset iOff. |
|
|
6678 |
** |
|
|
6679 |
** If successful, SQLITE_OK is returned and (*ppEntry) set to point to |
|
|
6680 |
** the new object. Otherwise, an SQLite error code is returned and the |
|
|
6681 |
** final value of (*ppEntry) undefined. |
|
|
6682 |
*/ |
|
|
6683 |
static int zipfileGetEntry( |
|
|
6684 |
ZipfileTab *pTab, /* Store any error message here */ |
|
|
6685 |
const u8 *aBlob, /* Pointer to in-memory file image */ |
|
|
6686 |
int nBlob, /* Size of aBlob[] in bytes */ |
|
|
6687 |
FILE *pFile, /* If aBlob==0, read from this file */ |
|
|
6688 |
i64 iOff, /* Offset of CDS record */ |
|
|
6689 |
ZipfileEntry **ppEntry /* OUT: Pointer to new object */ |
|
|
6690 |
){ |
|
|
6691 |
u8 *aRead; |
|
|
6692 |
char **pzErr = &pTab->base.zErrMsg; |
|
|
6693 |
int rc = SQLITE_OK; |
|
|
6694 |
|
|
|
6695 |
if( aBlob==0 ){ |
|
|
6696 |
aRead = pTab->aBuffer; |
|
|
6697 |
rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr); |
|
|
6698 |
}else{ |
|
|
6699 |
aRead = (u8*)&aBlob[iOff]; |
|
|
6700 |
} |
|
|
6701 |
|
|
|
6702 |
if( rc==SQLITE_OK ){ |
|
|
6703 |
sqlite3_int64 nAlloc; |
|
|
6704 |
ZipfileEntry *pNew; |
|
|
6705 |
|
|
|
6706 |
int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]); |
|
|
6707 |
int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]); |
|
|
6708 |
nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]); |
|
|
6709 |
|
|
|
6710 |
nAlloc = sizeof(ZipfileEntry) + nExtra; |
|
|
6711 |
if( aBlob ){ |
|
|
6712 |
nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]); |
|
|
6713 |
} |
|
|
6714 |
|
|
|
6715 |
pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc); |
|
|
6716 |
if( pNew==0 ){ |
|
|
6717 |
rc = SQLITE_NOMEM; |
|
|
6718 |
}else{ |
|
|
6719 |
memset(pNew, 0, sizeof(ZipfileEntry)); |
|
|
6720 |
rc = zipfileReadCDS(aRead, &pNew->cds); |
|
|
6721 |
if( rc!=SQLITE_OK ){ |
|
|
6722 |
*pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff); |
|
|
6723 |
}else if( aBlob==0 ){ |
|
|
6724 |
rc = zipfileReadData( |
|
|
6725 |
pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr |
|
|
6726 |
); |
|
|
6727 |
}else{ |
|
|
6728 |
aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ]; |
|
|
6729 |
} |
|
|
6730 |
} |
|
|
6731 |
|
|
|
6732 |
if( rc==SQLITE_OK ){ |
|
|
6733 |
u32 *pt = &pNew->mUnixTime; |
|
|
6734 |
pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); |
|
|
6735 |
pNew->aExtra = (u8*)&pNew[1]; |
|
|
6736 |
memcpy(pNew->aExtra, &aRead[nFile], nExtra); |
|
|
6737 |
if( pNew->cds.zFile==0 ){ |
|
|
6738 |
rc = SQLITE_NOMEM; |
|
|
6739 |
}else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){ |
|
|
6740 |
pNew->mUnixTime = zipfileMtime(&pNew->cds); |
|
|
6741 |
} |
|
|
6742 |
} |
|
|
6743 |
|
|
|
6744 |
if( rc==SQLITE_OK ){ |
|
|
6745 |
static const int szFix = ZIPFILE_LFH_FIXED_SZ; |
|
|
6746 |
ZipfileLFH lfh; |
|
|
6747 |
if( pFile ){ |
|
|
6748 |
rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr); |
|
|
6749 |
}else{ |
|
|
6750 |
aRead = (u8*)&aBlob[pNew->cds.iOffset]; |
|
|
6751 |
} |
|
|
6752 |
|
|
|
6753 |
rc = zipfileReadLFH(aRead, &lfh); |
|
|
6754 |
if( rc==SQLITE_OK ){ |
|
|
6755 |
pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ; |
|
|
6756 |
pNew->iDataOff += lfh.nFile + lfh.nExtra; |
|
|
6757 |
if( aBlob && pNew->cds.szCompressed ){ |
|
|
6758 |
pNew->aData = &pNew->aExtra[nExtra]; |
|
|
6759 |
memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed); |
|
|
6760 |
} |
|
|
6761 |
}else{ |
|
|
6762 |
*pzErr = sqlite3_mprintf("failed to read LFH at offset %d", |
|
|
6763 |
(int)pNew->cds.iOffset |
|
|
6764 |
); |
|
|
6765 |
} |
|
|
6766 |
} |
|
|
6767 |
|
|
|
6768 |
if( rc!=SQLITE_OK ){ |
|
|
6769 |
zipfileEntryFree(pNew); |
|
|
6770 |
}else{ |
|
|
6771 |
*ppEntry = pNew; |
|
|
6772 |
} |
|
|
6773 |
} |
|
|
6774 |
|
|
|
6775 |
return rc; |
|
|
6776 |
} |
|
|
6777 |
|
|
|
6778 |
/* |
|
|
6779 |
** Advance an ZipfileCsr to its next row of output. |
|
|
6780 |
*/ |
|
|
6781 |
static int zipfileNext(sqlite3_vtab_cursor *cur){ |
|
|
6782 |
ZipfileCsr *pCsr = (ZipfileCsr*)cur; |
|
|
6783 |
int rc = SQLITE_OK; |
|
|
6784 |
|
|
|
6785 |
if( pCsr->pFile ){ |
|
|
6786 |
i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize; |
|
|
6787 |
zipfileEntryFree(pCsr->pCurrent); |
|
|
6788 |
pCsr->pCurrent = 0; |
|
|
6789 |
if( pCsr->iNextOff>=iEof ){ |
|
|
6790 |
pCsr->bEof = 1; |
|
|
6791 |
}else{ |
|
|
6792 |
ZipfileEntry *p = 0; |
|
|
6793 |
ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab); |
|
|
6794 |
rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p); |
|
|
6795 |
if( rc==SQLITE_OK ){ |
|
|
6796 |
pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ; |
|
|
6797 |
pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment; |
|
|
6798 |
} |
|
|
6799 |
pCsr->pCurrent = p; |
|
|
6800 |
} |
|
|
6801 |
}else{ |
|
|
6802 |
if( !pCsr->bNoop ){ |
|
|
6803 |
pCsr->pCurrent = pCsr->pCurrent->pNext; |
|
|
6804 |
} |
|
|
6805 |
if( pCsr->pCurrent==0 ){ |
|
|
6806 |
pCsr->bEof = 1; |
|
|
6807 |
} |
|
|
6808 |
} |
|
|
6809 |
|
|
|
6810 |
pCsr->bNoop = 0; |
|
|
6811 |
return rc; |
|
|
6812 |
} |
|
|
6813 |
|
|
|
6814 |
static void zipfileFree(void *p) { |
|
|
6815 |
sqlite3_free(p); |
|
|
6816 |
} |
|
|
6817 |
|
|
|
6818 |
/* |
|
|
6819 |
** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the |
|
|
6820 |
** size is nOut bytes. This function uncompresses the data and sets the |
|
|
6821 |
** return value in context pCtx to the result (a blob). |
|
|
6822 |
** |
|
|
6823 |
** If an error occurs, an error code is left in pCtx instead. |
|
|
6824 |
*/ |
|
|
6825 |
static void zipfileInflate( |
|
|
6826 |
sqlite3_context *pCtx, /* Store result here */ |
|
|
6827 |
const u8 *aIn, /* Compressed data */ |
|
|
6828 |
int nIn, /* Size of buffer aIn[] in bytes */ |
|
|
6829 |
int nOut /* Expected output size */ |
|
|
6830 |
){ |
|
|
6831 |
u8 *aRes = sqlite3_malloc(nOut); |
|
|
6832 |
if( aRes==0 ){ |
|
|
6833 |
sqlite3_result_error_nomem(pCtx); |
|
|
6834 |
}else{ |
|
|
6835 |
int err; |
|
|
6836 |
z_stream str; |
|
|
6837 |
memset(&str, 0, sizeof(str)); |
|
|
6838 |
|
|
|
6839 |
str.next_in = (Byte*)aIn; |
|
|
6840 |
str.avail_in = nIn; |
|
|
6841 |
str.next_out = (Byte*)aRes; |
|
|
6842 |
str.avail_out = nOut; |
|
|
6843 |
|
|
|
6844 |
err = inflateInit2(&str, -15); |
|
|
6845 |
if( err!=Z_OK ){ |
|
|
6846 |
zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err); |
|
|
6847 |
}else{ |
|
|
6848 |
err = inflate(&str, Z_NO_FLUSH); |
|
|
6849 |
if( err!=Z_STREAM_END ){ |
|
|
6850 |
zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err); |
|
|
6851 |
}else{ |
|
|
6852 |
sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree); |
|
|
6853 |
aRes = 0; |
|
|
6854 |
} |
|
|
6855 |
} |
|
|
6856 |
sqlite3_free(aRes); |
|
|
6857 |
inflateEnd(&str); |
|
|
6858 |
} |
|
|
6859 |
} |
|
|
6860 |
|
|
|
6861 |
/* |
|
|
6862 |
** Buffer aIn (size nIn bytes) contains uncompressed data. This function |
|
|
6863 |
** compresses it and sets (*ppOut) to point to a buffer containing the |
|
|
6864 |
** compressed data. The caller is responsible for eventually calling |
|
|
6865 |
** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) |
|
|
6866 |
** is set to the size of buffer (*ppOut) in bytes. |
|
|
6867 |
** |
|
|
6868 |
** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error |
|
|
6869 |
** code is returned and an error message left in virtual-table handle |
|
|
6870 |
** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this |
|
|
6871 |
** case. |
|
|
6872 |
*/ |
|
|
6873 |
static int zipfileDeflate( |
|
|
6874 |
const u8 *aIn, int nIn, /* Input */ |
|
|
6875 |
u8 **ppOut, int *pnOut, /* Output */ |
|
|
6876 |
char **pzErr /* OUT: Error message */ |
|
|
6877 |
){ |
|
|
6878 |
int rc = SQLITE_OK; |
|
|
6879 |
sqlite3_int64 nAlloc; |
|
|
6880 |
z_stream str; |
|
|
6881 |
u8 *aOut; |
|
|
6882 |
|
|
|
6883 |
memset(&str, 0, sizeof(str)); |
|
|
6884 |
str.next_in = (Bytef*)aIn; |
|
|
6885 |
str.avail_in = nIn; |
|
|
6886 |
deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); |
|
|
6887 |
|
|
|
6888 |
nAlloc = deflateBound(&str, nIn); |
|
|
6889 |
aOut = (u8*)sqlite3_malloc64(nAlloc); |
|
|
6890 |
if( aOut==0 ){ |
|
|
6891 |
rc = SQLITE_NOMEM; |
|
|
6892 |
}else{ |
|
|
6893 |
int res; |
|
|
6894 |
str.next_out = aOut; |
|
|
6895 |
str.avail_out = nAlloc; |
|
|
6896 |
res = deflate(&str, Z_FINISH); |
|
|
6897 |
if( res==Z_STREAM_END ){ |
|
|
6898 |
*ppOut = aOut; |
|
|
6899 |
*pnOut = (int)str.total_out; |
|
|
6900 |
}else{ |
|
|
6901 |
sqlite3_free(aOut); |
|
|
6902 |
*pzErr = sqlite3_mprintf("zipfile: deflate() error"); |
|
|
6903 |
rc = SQLITE_ERROR; |
|
|
6904 |
} |
|
|
6905 |
deflateEnd(&str); |
|
|
6906 |
} |
|
|
6907 |
|
|
|
6908 |
return rc; |
|
|
6909 |
} |
|
|
6910 |
|
|
|
6911 |
|
|
|
6912 |
/* |
|
|
6913 |
** Return values of columns for the row at which the series_cursor |
|
|
6914 |
** is currently pointing. |
|
|
6915 |
*/ |
|
|
6916 |
static int zipfileColumn( |
|
|
6917 |
sqlite3_vtab_cursor *cur, /* The cursor */ |
|
|
6918 |
sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ |
|
|
6919 |
int i /* Which column to return */ |
|
|
6920 |
){ |
|
|
6921 |
ZipfileCsr *pCsr = (ZipfileCsr*)cur; |
|
|
6922 |
ZipfileCDS *pCDS = &pCsr->pCurrent->cds; |
|
|
6923 |
int rc = SQLITE_OK; |
|
|
6924 |
switch( i ){ |
|
|
6925 |
case 0: /* name */ |
|
|
6926 |
sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT); |
|
|
6927 |
break; |
|
|
6928 |
case 1: /* mode */ |
|
|
6929 |
/* TODO: Whether or not the following is correct surely depends on |
|
|
6930 |
** the platform on which the archive was created. */ |
|
|
6931 |
sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16); |
|
|
6932 |
break; |
|
|
6933 |
case 2: { /* mtime */ |
|
|
6934 |
sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime); |
|
|
6935 |
break; |
|
|
6936 |
} |
|
|
6937 |
case 3: { /* sz */ |
|
|
6938 |
if( sqlite3_vtab_nochange(ctx)==0 ){ |
|
|
6939 |
sqlite3_result_int64(ctx, pCDS->szUncompressed); |
|
|
6940 |
} |
|
|
6941 |
break; |
|
|
6942 |
} |
|
|
6943 |
case 4: /* rawdata */ |
|
|
6944 |
if( sqlite3_vtab_nochange(ctx) ) break; |
|
|
6945 |
case 5: { /* data */ |
|
|
6946 |
if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){ |
|
|
6947 |
int sz = pCDS->szCompressed; |
|
|
6948 |
int szFinal = pCDS->szUncompressed; |
|
|
6949 |
if( szFinal>0 ){ |
|
|
6950 |
u8 *aBuf; |
|
|
6951 |
u8 *aFree = 0; |
|
|
6952 |
if( pCsr->pCurrent->aData ){ |
|
|
6953 |
aBuf = pCsr->pCurrent->aData; |
|
|
6954 |
}else{ |
|
|
6955 |
aBuf = aFree = sqlite3_malloc64(sz); |
|
|
6956 |
if( aBuf==0 ){ |
|
|
6957 |
rc = SQLITE_NOMEM; |
|
|
6958 |
}else{ |
|
|
6959 |
FILE *pFile = pCsr->pFile; |
|
|
6960 |
if( pFile==0 ){ |
|
|
6961 |
pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd; |
|
|
6962 |
} |
|
|
6963 |
rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff, |
|
|
6964 |
&pCsr->base.pVtab->zErrMsg |
|
|
6965 |
); |
|
|
6966 |
} |
|
|
6967 |
} |
|
|
6968 |
if( rc==SQLITE_OK ){ |
|
|
6969 |
if( i==5 && pCDS->iCompression ){ |
|
|
6970 |
zipfileInflate(ctx, aBuf, sz, szFinal); |
|
|
6971 |
}else{ |
|
|
6972 |
sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT); |
|
|
6973 |
} |
|
|
6974 |
} |
|
|
6975 |
sqlite3_free(aFree); |
|
|
6976 |
}else{ |
|
|
6977 |
/* Figure out if this is a directory or a zero-sized file. Consider |
|
|
6978 |
** it to be a directory either if the mode suggests so, or if |
|
|
6979 |
** the final character in the name is '/'. */ |
|
|
6980 |
u32 mode = pCDS->iExternalAttr >> 16; |
|
|
6981 |
if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){ |
|
|
6982 |
sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC); |
|
|
6983 |
} |
|
|
6984 |
} |
|
|
6985 |
} |
|
|
6986 |
break; |
|
|
6987 |
} |
|
|
6988 |
case 6: /* method */ |
|
|
6989 |
sqlite3_result_int(ctx, pCDS->iCompression); |
|
|
6990 |
break; |
|
|
6991 |
default: /* z */ |
|
|
6992 |
assert( i==7 ); |
|
|
6993 |
sqlite3_result_int64(ctx, pCsr->iId); |
|
|
6994 |
break; |
|
|
6995 |
} |
|
|
6996 |
|
|
|
6997 |
return rc; |
|
|
6998 |
} |
|
|
6999 |
|
|
|
7000 |
/* |
|
|
7001 |
** Return TRUE if the cursor is at EOF. |
|
|
7002 |
*/ |
|
|
7003 |
static int zipfileEof(sqlite3_vtab_cursor *cur){ |
|
|
7004 |
ZipfileCsr *pCsr = (ZipfileCsr*)cur; |
|
|
7005 |
return pCsr->bEof; |
|
|
7006 |
} |
|
|
7007 |
|
|
|
7008 |
/* |
|
|
7009 |
** If aBlob is not NULL, then it points to a buffer nBlob bytes in size |
|
|
7010 |
** containing an entire zip archive image. Or, if aBlob is NULL, then pFile |
|
|
7011 |
** is guaranteed to be a file-handle open on a zip file. |
|
|
7012 |
** |
|
|
7013 |
** This function attempts to locate the EOCD record within the zip archive |
|
|
7014 |
** and populate *pEOCD with the results of decoding it. SQLITE_OK is |
|
|
7015 |
** returned if successful. Otherwise, an SQLite error code is returned and |
|
|
7016 |
** an English language error message may be left in virtual-table pTab. |
|
|
7017 |
*/ |
|
|
7018 |
static int zipfileReadEOCD( |
|
|
7019 |
ZipfileTab *pTab, /* Return errors here */ |
|
|
7020 |
const u8 *aBlob, /* Pointer to in-memory file image */ |
|
|
7021 |
int nBlob, /* Size of aBlob[] in bytes */ |
|
|
7022 |
FILE *pFile, /* Read from this file if aBlob==0 */ |
|
|
7023 |
ZipfileEOCD *pEOCD /* Object to populate */ |
|
|
7024 |
){ |
|
|
7025 |
u8 *aRead = pTab->aBuffer; /* Temporary buffer */ |
|
|
7026 |
int nRead; /* Bytes to read from file */ |
|
|
7027 |
int rc = SQLITE_OK; |
|
|
7028 |
|
|
|
7029 |
if( aBlob==0 ){ |
|
|
7030 |
i64 iOff; /* Offset to read from */ |
|
|
7031 |
i64 szFile; /* Total size of file in bytes */ |
|
|
7032 |
fseek(pFile, 0, SEEK_END); |
|
|
7033 |
szFile = (i64)ftell(pFile); |
|
|
7034 |
if( szFile==0 ){ |
|
|
7035 |
memset(pEOCD, 0, sizeof(ZipfileEOCD)); |
|
|
7036 |
return SQLITE_OK; |
|
|
7037 |
} |
|
|
7038 |
nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE)); |
|
|
7039 |
iOff = szFile - nRead; |
|
|
7040 |
rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg); |
|
|
7041 |
}else{ |
|
|
7042 |
nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE)); |
|
|
7043 |
aRead = (u8*)&aBlob[nBlob-nRead]; |
|
|
7044 |
} |
|
|
7045 |
|
|
|
7046 |
if( rc==SQLITE_OK ){ |
|
|
7047 |
int i; |
|
|
7048 |
|
|
|
7049 |
/* Scan backwards looking for the signature bytes */ |
|
|
7050 |
for(i=nRead-20; i>=0; i--){ |
|
|
7051 |
if( aRead[i]==0x50 && aRead[i+1]==0x4b |
|
|
7052 |
&& aRead[i+2]==0x05 && aRead[i+3]==0x06 |
|
|
7053 |
){ |
|
|
7054 |
break; |
|
|
7055 |
} |
|
|
7056 |
} |
|
|
7057 |
if( i<0 ){ |
|
|
7058 |
pTab->base.zErrMsg = sqlite3_mprintf( |
|
|
7059 |
"cannot find end of central directory record" |
|
|
7060 |
); |
|
|
7061 |
return SQLITE_ERROR; |
|
|
7062 |
} |
|
|
7063 |
|
|
|
7064 |
aRead += i+4; |
|
|
7065 |
pEOCD->iDisk = zipfileRead16(aRead); |
|
|
7066 |
pEOCD->iFirstDisk = zipfileRead16(aRead); |
|
|
7067 |
pEOCD->nEntry = zipfileRead16(aRead); |
|
|
7068 |
pEOCD->nEntryTotal = zipfileRead16(aRead); |
|
|
7069 |
pEOCD->nSize = zipfileRead32(aRead); |
|
|
7070 |
pEOCD->iOffset = zipfileRead32(aRead); |
|
|
7071 |
} |
|
|
7072 |
|
|
|
7073 |
return rc; |
|
|
7074 |
} |
|
|
7075 |
|
|
|
7076 |
/* |
|
|
7077 |
** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry |
|
|
7078 |
** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added |
|
|
7079 |
** to the end of the list. Otherwise, it is added to the list immediately |
|
|
7080 |
** before pBefore (which is guaranteed to be a part of said list). |
|
|
7081 |
*/ |
|
|
7082 |
static void zipfileAddEntry( |
|
|
7083 |
ZipfileTab *pTab, |
|
|
7084 |
ZipfileEntry *pBefore, |
|
|
7085 |
ZipfileEntry *pNew |
|
|
7086 |
){ |
|
|
7087 |
assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) ); |
|
|
7088 |
assert( pNew->pNext==0 ); |
|
|
7089 |
if( pBefore==0 ){ |
|
|
7090 |
if( pTab->pFirstEntry==0 ){ |
|
|
7091 |
pTab->pFirstEntry = pTab->pLastEntry = pNew; |
|
|
7092 |
}else{ |
|
|
7093 |
assert( pTab->pLastEntry->pNext==0 ); |
|
|
7094 |
pTab->pLastEntry->pNext = pNew; |
|
|
7095 |
pTab->pLastEntry = pNew; |
|
|
7096 |
} |
|
|
7097 |
}else{ |
|
|
7098 |
ZipfileEntry **pp; |
|
|
7099 |
for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext)); |
|
|
7100 |
pNew->pNext = pBefore; |
|
|
7101 |
*pp = pNew; |
|
|
7102 |
} |
|
|
7103 |
} |
|
|
7104 |
|
|
|
7105 |
static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){ |
|
|
7106 |
ZipfileEOCD eocd; |
|
|
7107 |
int rc; |
|
|
7108 |
int i; |
|
|
7109 |
i64 iOff; |
|
|
7110 |
|
|
|
7111 |
rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd); |
|
|
7112 |
iOff = eocd.iOffset; |
|
|
7113 |
for(i=0; rc==SQLITE_OK && i |
|
|
7114 |
ZipfileEntry *pNew = 0; |
|
|
7115 |
rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew); |
|
|
7116 |
|
|
|
7117 |
if( rc==SQLITE_OK ){ |
|
|
7118 |
zipfileAddEntry(pTab, 0, pNew); |
|
|
7119 |
iOff += ZIPFILE_CDS_FIXED_SZ; |
|
|
7120 |
iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment; |
|
|
7121 |
} |
|
|
7122 |
} |
|
|
7123 |
return rc; |
|
|
7124 |
} |
|
|
7125 |
|
|
|
7126 |
/* |
|
|
7127 |
** xFilter callback. |
|
|
7128 |
*/ |
|
|
7129 |
static int zipfileFilter( |
|
|
7130 |
sqlite3_vtab_cursor *cur, |
|
|
7131 |
int idxNum, const char *idxStr, |
|
|
7132 |
int argc, sqlite3_value **argv |
|
|
7133 |
){ |
|
|
7134 |
ZipfileTab *pTab = (ZipfileTab*)cur->pVtab; |
|
|
7135 |
ZipfileCsr *pCsr = (ZipfileCsr*)cur; |
|
|
7136 |
const char *zFile = 0; /* Zip file to scan */ |
|
|
7137 |
int rc = SQLITE_OK; /* Return Code */ |
|
|
7138 |
int bInMemory = 0; /* True for an in-memory zipfile */ |
|
|
7139 |
|
|
|
7140 |
zipfileResetCursor(pCsr); |
|
|
7141 |
|
|
|
7142 |
if( pTab->zFile ){ |
|
|
7143 |
zFile = pTab->zFile; |
|
|
7144 |
}else if( idxNum==0 ){ |
|
|
7145 |
zipfileCursorErr(pCsr, "zipfile() function requires an argument"); |
|
|
7146 |
return SQLITE_ERROR; |
|
|
7147 |
}else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){ |
|
|
7148 |
const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]); |
|
|
7149 |
int nBlob = sqlite3_value_bytes(argv[0]); |
|
|
7150 |
assert( pTab->pFirstEntry==0 ); |
|
|
7151 |
rc = zipfileLoadDirectory(pTab, aBlob, nBlob); |
|
|
7152 |
pCsr->pFreeEntry = pTab->pFirstEntry; |
|
|
7153 |
pTab->pFirstEntry = pTab->pLastEntry = 0; |
|
|
7154 |
if( rc!=SQLITE_OK ) return rc; |
|
|
7155 |
bInMemory = 1; |
|
|
7156 |
}else{ |
|
|
7157 |
zFile = (const char*)sqlite3_value_text(argv[0]); |
|
|
7158 |
} |
|
|
7159 |
|
|
|
7160 |
if( 0==pTab->pWriteFd && 0==bInMemory ){ |
|
|
7161 |
pCsr->pFile = fopen(zFile, "rb"); |
|
|
7162 |
if( pCsr->pFile==0 ){ |
|
|
7163 |
zipfileCursorErr(pCsr, "cannot open file: %s", zFile); |
|
|
7164 |
rc = SQLITE_ERROR; |
|
|
7165 |
}else{ |
|
|
7166 |
rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd); |
|
|
7167 |
if( rc==SQLITE_OK ){ |
|
|
7168 |
if( pCsr->eocd.nEntry==0 ){ |
|
|
7169 |
pCsr->bEof = 1; |
|
|
7170 |
}else{ |
|
|
7171 |
pCsr->iNextOff = pCsr->eocd.iOffset; |
|
|
7172 |
rc = zipfileNext(cur); |
|
|
7173 |
} |
|
|
7174 |
} |
|
|
7175 |
} |
|
|
7176 |
}else{ |
|
|
7177 |
pCsr->bNoop = 1; |
|
|
7178 |
pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry; |
|
|
7179 |
rc = zipfileNext(cur); |
|
|
7180 |
} |
|
|
7181 |
|
|
|
7182 |
return rc; |
|
|
7183 |
} |
|
|
7184 |
|
|
|
7185 |
/* |
|
|
7186 |
** xBestIndex callback. |
|
|
7187 |
*/ |
|
|
7188 |
static int zipfileBestIndex( |
|
|
7189 |
sqlite3_vtab *tab, |
|
|
7190 |
sqlite3_index_info *pIdxInfo |
|
|
7191 |
){ |
|
|
7192 |
int i; |
|
|
7193 |
int idx = -1; |
|
|
7194 |
int unusable = 0; |
|
|
7195 |
|
|
|
7196 |
for(i=0; inConstraint; i++){ |
|
|
7197 |
const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i]; |
|
|
7198 |
if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue; |
|
|
7199 |
if( pCons->usable==0 ){ |
|
|
7200 |
unusable = 1; |
|
|
7201 |
}else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
|
|
7202 |
idx = i; |
|
|
7203 |
} |
|
|
7204 |
} |
|
|
7205 |
pIdxInfo->estimatedCost = 1000.0; |
|
|
7206 |
if( idx>=0 ){ |
|
|
7207 |
pIdxInfo->aConstraintUsage[idx].argvIndex = 1; |
|
|
7208 |
pIdxInfo->aConstraintUsage[idx].omit = 1; |
|
|
7209 |
pIdxInfo->idxNum = 1; |
|
|
7210 |
}else if( unusable ){ |
|
|
7211 |
return SQLITE_CONSTRAINT; |
|
|
7212 |
} |
|
|
7213 |
return SQLITE_OK; |
|
|
7214 |
} |
|
|
7215 |
|
|
|
7216 |
static ZipfileEntry *zipfileNewEntry(const char *zPath){ |
|
|
7217 |
ZipfileEntry *pNew; |
|
|
7218 |
pNew = sqlite3_malloc(sizeof(ZipfileEntry)); |
|
|
7219 |
if( pNew ){ |
|
|
7220 |
memset(pNew, 0, sizeof(ZipfileEntry)); |
|
|
7221 |
pNew->cds.zFile = sqlite3_mprintf("%s", zPath); |
|
|
7222 |
if( pNew->cds.zFile==0 ){ |
|
|
7223 |
sqlite3_free(pNew); |
|
|
7224 |
pNew = 0; |
|
|
7225 |
} |
|
|
7226 |
} |
|
|
7227 |
return pNew; |
|
|
7228 |
} |
|
|
7229 |
|
|
|
7230 |
static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){ |
|
|
7231 |
ZipfileCDS *pCds = &pEntry->cds; |
|
|
7232 |
u8 *a = aBuf; |
|
|
7233 |
|
|
|
7234 |
pCds->nExtra = 9; |
|
|
7235 |
|
|
|
7236 |
/* Write the LFH itself */ |
|
|
7237 |
zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH); |
|
|
7238 |
zipfileWrite16(a, pCds->iVersionExtract); |
|
|
7239 |
zipfileWrite16(a, pCds->flags); |
|
|
7240 |
zipfileWrite16(a, pCds->iCompression); |
|
|
7241 |
zipfileWrite16(a, pCds->mTime); |
|
|
7242 |
zipfileWrite16(a, pCds->mDate); |
|
|
7243 |
zipfileWrite32(a, pCds->crc32); |
|
|
7244 |
zipfileWrite32(a, pCds->szCompressed); |
|
|
7245 |
zipfileWrite32(a, pCds->szUncompressed); |
|
|
7246 |
zipfileWrite16(a, (u16)pCds->nFile); |
|
|
7247 |
zipfileWrite16(a, pCds->nExtra); |
|
|
7248 |
assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] ); |
|
|
7249 |
|
|
|
7250 |
/* Add the file name */ |
|
|
7251 |
memcpy(a, pCds->zFile, (int)pCds->nFile); |
|
|
7252 |
a += (int)pCds->nFile; |
|
|
7253 |
|
|
|
7254 |
/* The "extra" data */ |
|
|
7255 |
zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP); |
|
|
7256 |
zipfileWrite16(a, 5); |
|
|
7257 |
*a++ = 0x01; |
|
|
7258 |
zipfileWrite32(a, pEntry->mUnixTime); |
|
|
7259 |
|
|
|
7260 |
return a-aBuf; |
|
|
7261 |
} |
|
|
7262 |
|
|
|
7263 |
static int zipfileAppendEntry( |
|
|
7264 |
ZipfileTab *pTab, |
|
|
7265 |
ZipfileEntry *pEntry, |
|
|
7266 |
const u8 *pData, |
|
|
7267 |
int nData |
|
|
7268 |
){ |
|
|
7269 |
u8 *aBuf = pTab->aBuffer; |
|
|
7270 |
int nBuf; |
|
|
7271 |
int rc; |
|
|
7272 |
|
|
|
7273 |
nBuf = zipfileSerializeLFH(pEntry, aBuf); |
|
|
7274 |
rc = zipfileAppendData(pTab, aBuf, nBuf); |
|
|
7275 |
if( rc==SQLITE_OK ){ |
|
|
7276 |
pEntry->iDataOff = pTab->szCurrent; |
|
|
7277 |
rc = zipfileAppendData(pTab, pData, nData); |
|
|
7278 |
} |
|
|
7279 |
|
|
|
7280 |
return rc; |
|
|
7281 |
} |
|
|
7282 |
|
|
|
7283 |
static int zipfileGetMode( |
|
|
7284 |
sqlite3_value *pVal, |
|
|
7285 |
int bIsDir, /* If true, default to directory */ |
|
|
7286 |
u32 *pMode, /* OUT: Mode value */ |
|
|
7287 |
char **pzErr /* OUT: Error message */ |
|
|
7288 |
){ |
|
|
7289 |
const char *z = (const char*)sqlite3_value_text(pVal); |
|
|
7290 |
u32 mode = 0; |
|
|
7291 |
if( z==0 ){ |
|
|
7292 |
mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644)); |
|
|
7293 |
}else if( z[0]>='0' && z[0]<='9' ){ |
|
|
7294 |
mode = (unsigned int)sqlite3_value_int(pVal); |
|
|
7295 |
}else{ |
|
|
7296 |
const char zTemplate[11] = "-rwxrwxrwx"; |
|
|
7297 |
int i; |
|
|
7298 |
if( strlen(z)!=10 ) goto parse_error; |
|
|
7299 |
switch( z[0] ){ |
|
|
7300 |
case '-': mode |= S_IFREG; break; |
|
|
7301 |
case 'd': mode |= S_IFDIR; break; |
|
|
7302 |
case 'l': mode |= S_IFLNK; break; |
|
|
7303 |
default: goto parse_error; |
|
|
7304 |
} |
|
|
7305 |
for(i=1; i<10; i++){ |
|
|
7306 |
if( z[i]==zTemplate[i] ) mode |= 1 << (9-i); |
|
|
7307 |
else if( z[i]!='-' ) goto parse_error; |
|
|
7308 |
} |
|
|
7309 |
} |
|
|
7310 |
if( ((mode & S_IFDIR)==0)==bIsDir ){ |
|
|
7311 |
/* The "mode" attribute is a directory, but data has been specified. |
|
|
7312 |
** Or vice-versa - no data but "mode" is a file or symlink. */ |
|
|
7313 |
*pzErr = sqlite3_mprintf("zipfile: mode does not match data"); |
|
|
7314 |
return SQLITE_CONSTRAINT; |
|
|
7315 |
} |
|
|
7316 |
*pMode = mode; |
|
|
7317 |
return SQLITE_OK; |
|
|
7318 |
|
|
|
7319 |
parse_error: |
|
|
7320 |
*pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z); |
|
|
7321 |
return SQLITE_ERROR; |
|
|
7322 |
} |
|
|
7323 |
|
|
|
7324 |
/* |
|
|
7325 |
** Both (const char*) arguments point to nul-terminated strings. Argument |
|
|
7326 |
** nB is the value of strlen(zB). This function returns 0 if the strings are |
|
|
7327 |
** identical, ignoring any trailing '/' character in either path. */ |
|
|
7328 |
static int zipfileComparePath(const char *zA, const char *zB, int nB){ |
|
|
7329 |
int nA = (int)strlen(zA); |
|
|
7330 |
if( nA>0 && zA[nA-1]=='/' ) nA--; |
|
|
7331 |
if( nB>0 && zB[nB-1]=='/' ) nB--; |
|
|
7332 |
if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0; |
|
|
7333 |
return 1; |
|
|
7334 |
} |
|
|
7335 |
|
|
|
7336 |
static int zipfileBegin(sqlite3_vtab *pVtab){ |
|
|
7337 |
ZipfileTab *pTab = (ZipfileTab*)pVtab; |
|
|
7338 |
int rc = SQLITE_OK; |
|
|
7339 |
|
|
|
7340 |
assert( pTab->pWriteFd==0 ); |
|
|
7341 |
if( pTab->zFile==0 || pTab->zFile[0]==0 ){ |
|
|
7342 |
pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename"); |
|
|
7343 |
return SQLITE_ERROR; |
|
|
7344 |
} |
|
|
7345 |
|
|
|
7346 |
/* Open a write fd on the file. Also load the entire central directory |
|
|
7347 |
** structure into memory. During the transaction any new file data is |
|
|
7348 |
** appended to the archive file, but the central directory is accumulated |
|
|
7349 |
** in main-memory until the transaction is committed. */ |
|
|
7350 |
pTab->pWriteFd = fopen(pTab->zFile, "ab+"); |
|
|
7351 |
if( pTab->pWriteFd==0 ){ |
|
|
7352 |
pTab->base.zErrMsg = sqlite3_mprintf( |
|
|
7353 |
"zipfile: failed to open file %s for writing", pTab->zFile |
|
|
7354 |
); |
|
|
7355 |
rc = SQLITE_ERROR; |
|
|
7356 |
}else{ |
|
|
7357 |
fseek(pTab->pWriteFd, 0, SEEK_END); |
|
|
7358 |
pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd); |
|
|
7359 |
rc = zipfileLoadDirectory(pTab, 0, 0); |
|
|
7360 |
} |
|
|
7361 |
|
|
|
7362 |
if( rc!=SQLITE_OK ){ |
|
|
7363 |
zipfileCleanupTransaction(pTab); |
|
|
7364 |
} |
|
|
7365 |
|
|
|
7366 |
return rc; |
|
|
7367 |
} |
|
|
7368 |
|
|
|
7369 |
/* |
|
|
7370 |
** Return the current time as a 32-bit timestamp in UNIX epoch format (like |
|
|
7371 |
** time(2)). |
|
|
7372 |
*/ |
|
|
7373 |
static u32 zipfileTime(void){ |
|
|
7374 |
sqlite3_vfs *pVfs = sqlite3_vfs_find(0); |
|
|
7375 |
u32 ret; |
|
|
7376 |
if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ |
|
|
7377 |
i64 ms; |
|
|
7378 |
pVfs->xCurrentTimeInt64(pVfs, &ms); |
|
|
7379 |
ret = (u32)((ms/1000) - ((i64)24405875 * 8640)); |
|
|
7380 |
}else{ |
|
|
7381 |
double day; |
|
|
7382 |
pVfs->xCurrentTime(pVfs, &day); |
|
|
7383 |
ret = (u32)((day - 2440587.5) * 86400); |
|
|
7384 |
} |
|
|
7385 |
return ret; |
|
|
7386 |
} |
|
|
7387 |
|
|
|
7388 |
/* |
|
|
7389 |
** Return a 32-bit timestamp in UNIX epoch format. |
|
|
7390 |
** |
|
|
7391 |
** If the value passed as the only argument is either NULL or an SQL NULL, |
|
|
7392 |
** return the current time. Otherwise, return the value stored in (*pVal) |
|
|
7393 |
** cast to a 32-bit unsigned integer. |
|
|
7394 |
*/ |
|
|
7395 |
static u32 zipfileGetTime(sqlite3_value *pVal){ |
|
|
7396 |
if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){ |
|
|
7397 |
return zipfileTime(); |
|
|
7398 |
} |
|
|
7399 |
return (u32)sqlite3_value_int64(pVal); |
|
|
7400 |
} |
|
|
7401 |
|
|
|
7402 |
/* |
|
|
7403 |
** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry |
|
|
7404 |
** linked list. Remove it from the list and free the object. |
|
|
7405 |
*/ |
|
|
7406 |
static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){ |
|
|
7407 |
if( pOld ){ |
|
|
7408 |
ZipfileEntry **pp; |
|
|
7409 |
for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext)); |
|
|
7410 |
*pp = (*pp)->pNext; |
|
|
7411 |
zipfileEntryFree(pOld); |
|
|
7412 |
} |
|
|
7413 |
} |
|
|
7414 |
|
|
|
7415 |
/* |
|
|
7416 |
** xUpdate method. |
|
|
7417 |
*/ |
|
|
7418 |
static int zipfileUpdate( |
|
|
7419 |
sqlite3_vtab *pVtab, |
|
|
7420 |
int nVal, |
|
|
7421 |
sqlite3_value **apVal, |
|
|
7422 |
sqlite_int64 *pRowid |
|
|
7423 |
){ |
|
|
7424 |
ZipfileTab *pTab = (ZipfileTab*)pVtab; |
|
|
7425 |
int rc = SQLITE_OK; /* Return Code */ |
|
|
7426 |
ZipfileEntry *pNew = 0; /* New in-memory CDS entry */ |
|
|
7427 |
|
|
|
7428 |
u32 mode = 0; /* Mode for new entry */ |
|
|
7429 |
u32 mTime = 0; /* Modification time for new entry */ |
|
|
7430 |
i64 sz = 0; /* Uncompressed size */ |
|
|
7431 |
const char *zPath = 0; /* Path for new entry */ |
|
|
7432 |
int nPath = 0; /* strlen(zPath) */ |
|
|
7433 |
const u8 *pData = 0; /* Pointer to buffer containing content */ |
|
|
7434 |
int nData = 0; /* Size of pData buffer in bytes */ |
|
|
7435 |
int iMethod = 0; /* Compression method for new entry */ |
|
|
7436 |
u8 *pFree = 0; /* Free this */ |
|
|
7437 |
char *zFree = 0; /* Also free this */ |
|
|
7438 |
ZipfileEntry *pOld = 0; |
|
|
7439 |
ZipfileEntry *pOld2 = 0; |
|
|
7440 |
int bUpdate = 0; /* True for an update that modifies "name" */ |
|
|
7441 |
int bIsDir = 0; |
|
|
7442 |
u32 iCrc32 = 0; |
|
|
7443 |
|
|
|
7444 |
if( pTab->pWriteFd==0 ){ |
|
|
7445 |
rc = zipfileBegin(pVtab); |
|
|
7446 |
if( rc!=SQLITE_OK ) return rc; |
|
|
7447 |
} |
|
|
7448 |
|
|
|
7449 |
/* If this is a DELETE or UPDATE, find the archive entry to delete. */ |
|
|
7450 |
if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){ |
|
|
7451 |
const char *zDelete = (const char*)sqlite3_value_text(apVal[0]); |
|
|
7452 |
int nDelete = (int)strlen(zDelete); |
|
|
7453 |
if( nVal>1 ){ |
|
|
7454 |
const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]); |
|
|
7455 |
if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){ |
|
|
7456 |
bUpdate = 1; |
|
|
7457 |
} |
|
|
7458 |
} |
|
|
7459 |
for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){ |
|
|
7460 |
if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){ |
|
|
7461 |
break; |
|
|
7462 |
} |
|
|
7463 |
assert( pOld->pNext ); |
|
|
7464 |
} |
|
|
7465 |
} |
|
|
7466 |
|
|
|
7467 |
if( nVal>1 ){ |
|
|
7468 |
/* Check that "sz" and "rawdata" are both NULL: */ |
|
|
7469 |
if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){ |
|
|
7470 |
zipfileTableErr(pTab, "sz must be NULL"); |
|
|
7471 |
rc = SQLITE_CONSTRAINT; |
|
|
7472 |
} |
|
|
7473 |
if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){ |
|
|
7474 |
zipfileTableErr(pTab, "rawdata must be NULL"); |
|
|
7475 |
rc = SQLITE_CONSTRAINT; |
|
|
7476 |
} |
|
|
7477 |
|
|
|
7478 |
if( rc==SQLITE_OK ){ |
|
|
7479 |
if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){ |
|
|
7480 |
/* data=NULL. A directory */ |
|
|
7481 |
bIsDir = 1; |
|
|
7482 |
}else{ |
|
|
7483 |
/* Value specified for "data", and possibly "method". This must be |
|
|
7484 |
** a regular file or a symlink. */ |
|
|
7485 |
const u8 *aIn = sqlite3_value_blob(apVal[7]); |
|
|
7486 |
int nIn = sqlite3_value_bytes(apVal[7]); |
|
|
7487 |
int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL; |
|
|
7488 |
|
|
|
7489 |
iMethod = sqlite3_value_int(apVal[8]); |
|
|
7490 |
sz = nIn; |
|
|
7491 |
pData = aIn; |
|
|
7492 |
nData = nIn; |
|
|
7493 |
if( iMethod!=0 && iMethod!=8 ){ |
|
|
7494 |
zipfileTableErr(pTab, "unknown compression method: %d", iMethod); |
|
|
7495 |
rc = SQLITE_CONSTRAINT; |
|
|
7496 |
}else{ |
|
|
7497 |
if( bAuto || iMethod ){ |
|
|
7498 |
int nCmp; |
|
|
7499 |
rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg); |
|
|
7500 |
if( rc==SQLITE_OK ){ |
|
|
7501 |
if( iMethod || nCmp |
|
|
7502 |
iMethod = 8; |
|
|
7503 |
pData = pFree; |
|
|
7504 |
nData = nCmp; |
|
|
7505 |
} |
|
|
7506 |
} |
|
|
7507 |
} |
|
|
7508 |
iCrc32 = crc32(0, aIn, nIn); |
|
|
7509 |
} |
|
|
7510 |
} |
|
|
7511 |
} |
|
|
7512 |
|
|
|
7513 |
if( rc==SQLITE_OK ){ |
|
|
7514 |
rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg); |
|
|
7515 |
} |
|
|
7516 |
|
|
|
7517 |
if( rc==SQLITE_OK ){ |
|
|
7518 |
zPath = (const char*)sqlite3_value_text(apVal[2]); |
|
|
7519 |
if( zPath==0 ) zPath = ""; |
|
|
7520 |
nPath = (int)strlen(zPath); |
|
|
7521 |
mTime = zipfileGetTime(apVal[4]); |
|
|
7522 |
} |
|
|
7523 |
|
|
|
7524 |
if( rc==SQLITE_OK && bIsDir ){ |
|
|
7525 |
/* For a directory, check that the last character in the path is a |
|
|
7526 |
** '/'. This appears to be required for compatibility with info-zip |
|
|
7527 |
** (the unzip command on unix). It does not create directories |
|
|
7528 |
** otherwise. */ |
|
|
7529 |
if( nPath<=0 || zPath[nPath-1]!='/' ){ |
|
|
7530 |
zFree = sqlite3_mprintf("%s/", zPath); |
|
|
7531 |
zPath = (const char*)zFree; |
|
|
7532 |
if( zFree==0 ){ |
|
|
7533 |
rc = SQLITE_NOMEM; |
|
|
7534 |
nPath = 0; |
|
|
7535 |
}else{ |
|
|
7536 |
nPath = (int)strlen(zPath); |
|
|
7537 |
} |
|
|
7538 |
} |
|
|
7539 |
} |
|
|
7540 |
|
|
|
7541 |
/* Check that we're not inserting a duplicate entry -OR- updating an |
|
|
7542 |
** entry with a path, thereby making it into a duplicate. */ |
|
|
7543 |
if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){ |
|
|
7544 |
ZipfileEntry *p; |
|
|
7545 |
for(p=pTab->pFirstEntry; p; p=p->pNext){ |
|
|
7546 |
if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){ |
|
|
7547 |
switch( sqlite3_vtab_on_conflict(pTab->db) ){ |
|
|
7548 |
case SQLITE_IGNORE: { |
|
|
7549 |
goto zipfile_update_done; |
|
|
7550 |
} |
|
|
7551 |
case SQLITE_REPLACE: { |
|
|
7552 |
pOld2 = p; |
|
|
7553 |
break; |
|
|
7554 |
} |
|
|
7555 |
default: { |
|
|
7556 |
zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath); |
|
|
7557 |
rc = SQLITE_CONSTRAINT; |
|
|
7558 |
break; |
|
|
7559 |
} |
|
|
7560 |
} |
|
|
7561 |
break; |
|
|
7562 |
} |
|
|
7563 |
} |
|
|
7564 |
} |
|
|
7565 |
|
|
|
7566 |
if( rc==SQLITE_OK ){ |
|
|
7567 |
/* Create the new CDS record. */ |
|
|
7568 |
pNew = zipfileNewEntry(zPath); |
|
|
7569 |
if( pNew==0 ){ |
|
|
7570 |
rc = SQLITE_NOMEM; |
|
|
7571 |
}else{ |
|
|
7572 |
pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY; |
|
|
7573 |
pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED; |
|
|
7574 |
pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS; |
|
|
7575 |
pNew->cds.iCompression = (u16)iMethod; |
|
|
7576 |
zipfileMtimeToDos(&pNew->cds, mTime); |
|
|
7577 |
pNew->cds.crc32 = iCrc32; |
|
|
7578 |
pNew->cds.szCompressed = nData; |
|
|
7579 |
pNew->cds.szUncompressed = (u32)sz; |
|
|
7580 |
pNew->cds.iExternalAttr = (mode<<16); |
|
|
7581 |
pNew->cds.iOffset = (u32)pTab->szCurrent; |
|
|
7582 |
pNew->cds.nFile = (u16)nPath; |
|
|
7583 |
pNew->mUnixTime = (u32)mTime; |
|
|
7584 |
rc = zipfileAppendEntry(pTab, pNew, pData, nData); |
|
|
7585 |
zipfileAddEntry(pTab, pOld, pNew); |
|
|
7586 |
} |
|
|
7587 |
} |
|
|
7588 |
} |
|
|
7589 |
|
|
|
7590 |
if( rc==SQLITE_OK && (pOld || pOld2) ){ |
|
|
7591 |
ZipfileCsr *pCsr; |
|
|
7592 |
for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){ |
|
|
7593 |
if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){ |
|
|
7594 |
pCsr->pCurrent = pCsr->pCurrent->pNext; |
|
|
7595 |
pCsr->bNoop = 1; |
|
|
7596 |
} |
|
|
7597 |
} |
|
|
7598 |
|
|
|
7599 |
zipfileRemoveEntryFromList(pTab, pOld); |
|
|
7600 |
zipfileRemoveEntryFromList(pTab, pOld2); |
|
|
7601 |
} |
|
|
7602 |
|
|
|
7603 |
zipfile_update_done: |
|
|
7604 |
sqlite3_free(pFree); |
|
|
7605 |
sqlite3_free(zFree); |
|
|
7606 |
return rc; |
|
|
7607 |
} |
|
|
7608 |
|
|
|
7609 |
static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){ |
|
|
7610 |
u8 *a = aBuf; |
|
|
7611 |
zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD); |
|
|
7612 |
zipfileWrite16(a, p->iDisk); |
|
|
7613 |
zipfileWrite16(a, p->iFirstDisk); |
|
|
7614 |
zipfileWrite16(a, p->nEntry); |
|
|
7615 |
zipfileWrite16(a, p->nEntryTotal); |
|
|
7616 |
zipfileWrite32(a, p->nSize); |
|
|
7617 |
zipfileWrite32(a, p->iOffset); |
|
|
7618 |
zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/ |
|
|
7619 |
|
|
|
7620 |
return a-aBuf; |
|
|
7621 |
} |
|
|
7622 |
|
|
|
7623 |
static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){ |
|
|
7624 |
int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer); |
|
|
7625 |
assert( nBuf==ZIPFILE_EOCD_FIXED_SZ ); |
|
|
7626 |
return zipfileAppendData(pTab, pTab->aBuffer, nBuf); |
|
|
7627 |
} |
|
|
7628 |
|
|
|
7629 |
/* |
|
|
7630 |
** Serialize the CDS structure into buffer aBuf[]. Return the number |
|
|
7631 |
** of bytes written. |
|
|
7632 |
*/ |
|
|
7633 |
static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){ |
|
|
7634 |
u8 *a = aBuf; |
|
|
7635 |
ZipfileCDS *pCDS = &pEntry->cds; |
|
|
7636 |
|
|
|
7637 |
if( pEntry->aExtra==0 ){ |
|
|
7638 |
pCDS->nExtra = 9; |
|
|
7639 |
} |
|
|
7640 |
|
|
|
7641 |
zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS); |
|
|
7642 |
zipfileWrite16(a, pCDS->iVersionMadeBy); |
|
|
7643 |
zipfileWrite16(a, pCDS->iVersionExtract); |
|
|
7644 |
zipfileWrite16(a, pCDS->flags); |
|
|
7645 |
zipfileWrite16(a, pCDS->iCompression); |
|
|
7646 |
zipfileWrite16(a, pCDS->mTime); |
|
|
7647 |
zipfileWrite16(a, pCDS->mDate); |
|
|
7648 |
zipfileWrite32(a, pCDS->crc32); |
|
|
7649 |
zipfileWrite32(a, pCDS->szCompressed); |
|
|
7650 |
zipfileWrite32(a, pCDS->szUncompressed); |
|
|
7651 |
assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] ); |
|
|
7652 |
zipfileWrite16(a, pCDS->nFile); |
|
|
7653 |
zipfileWrite16(a, pCDS->nExtra); |
|
|
7654 |
zipfileWrite16(a, pCDS->nComment); |
|
|
7655 |
zipfileWrite16(a, pCDS->iDiskStart); |
|
|
7656 |
zipfileWrite16(a, pCDS->iInternalAttr); |
|
|
7657 |
zipfileWrite32(a, pCDS->iExternalAttr); |
|
|
7658 |
zipfileWrite32(a, pCDS->iOffset); |
|
|
7659 |
|
|
|
7660 |
memcpy(a, pCDS->zFile, pCDS->nFile); |
|
|
7661 |
a += pCDS->nFile; |
|
|
7662 |
|
|
|
7663 |
if( pEntry->aExtra ){ |
|
|
7664 |
int n = (int)pCDS->nExtra + (int)pCDS->nComment; |
|
|
7665 |
memcpy(a, pEntry->aExtra, n); |
|
|
7666 |
a += n; |
|
|
7667 |
}else{ |
|
|
7668 |
assert( pCDS->nExtra==9 ); |
|
|
7669 |
zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP); |
|
|
7670 |
zipfileWrite16(a, 5); |
|
|
7671 |
*a++ = 0x01; |
|
|
7672 |
zipfileWrite32(a, pEntry->mUnixTime); |
|
|
7673 |
} |
|
|
7674 |
|
|
|
7675 |
return a-aBuf; |
|
|
7676 |
} |
|
|
7677 |
|
|
|
7678 |
static int zipfileCommit(sqlite3_vtab *pVtab){ |
|
|
7679 |
ZipfileTab *pTab = (ZipfileTab*)pVtab; |
|
|
7680 |
int rc = SQLITE_OK; |
|
|
7681 |
if( pTab->pWriteFd ){ |
|
|
7682 |
i64 iOffset = pTab->szCurrent; |
|
|
7683 |
ZipfileEntry *p; |
|
|
7684 |
ZipfileEOCD eocd; |
|
|
7685 |
int nEntry = 0; |
|
|
7686 |
|
|
|
7687 |
/* Write out all entries */ |
|
|
7688 |
for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){ |
|
|
7689 |
int n = zipfileSerializeCDS(p, pTab->aBuffer); |
|
|
7690 |
rc = zipfileAppendData(pTab, pTab->aBuffer, n); |
|
|
7691 |
nEntry++; |
|
|
7692 |
} |
|
|
7693 |
|
|
|
7694 |
/* Write out the EOCD record */ |
|
|
7695 |
eocd.iDisk = 0; |
|
|
7696 |
eocd.iFirstDisk = 0; |
|
|
7697 |
eocd.nEntry = (u16)nEntry; |
|
|
7698 |
eocd.nEntryTotal = (u16)nEntry; |
|
|
7699 |
eocd.nSize = (u32)(pTab->szCurrent - iOffset); |
|
|
7700 |
eocd.iOffset = (u32)iOffset; |
|
|
7701 |
rc = zipfileAppendEOCD(pTab, &eocd); |
|
|
7702 |
|
|
|
7703 |
zipfileCleanupTransaction(pTab); |
|
|
7704 |
} |
|
|
7705 |
return rc; |
|
|
7706 |
} |
|
|
7707 |
|
|
|
7708 |
static int zipfileRollback(sqlite3_vtab *pVtab){ |
|
|
7709 |
return zipfileCommit(pVtab); |
|
|
7710 |
} |
|
|
7711 |
|
|
|
7712 |
static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){ |
|
|
7713 |
ZipfileCsr *pCsr; |
|
|
7714 |
for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){ |
|
|
7715 |
if( iId==pCsr->iId ) break; |
|
|
7716 |
} |
|
|
7717 |
return pCsr; |
|
|
7718 |
} |
|
|
7719 |
|
|
|
7720 |
static void zipfileFunctionCds( |
|
|
7721 |
sqlite3_context *context, |
|
|
7722 |
int argc, |
|
|
7723 |
sqlite3_value **argv |
|
|
7724 |
){ |
|
|
7725 |
ZipfileCsr *pCsr; |
|
|
7726 |
ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context); |
|
|
7727 |
assert( argc>0 ); |
|
|
7728 |
|
|
|
7729 |
pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0])); |
|
|
7730 |
if( pCsr ){ |
|
|
7731 |
ZipfileCDS *p = &pCsr->pCurrent->cds; |
|
|
7732 |
char *zRes = sqlite3_mprintf("{" |
|
|
7733 |
"\"version-made-by\" : %u, " |
|
|
7734 |
"\"version-to-extract\" : %u, " |
|
|
7735 |
"\"flags\" : %u, " |
|
|
7736 |
"\"compression\" : %u, " |
|
|
7737 |
"\"time\" : %u, " |
|
|
7738 |
"\"date\" : %u, " |
|
|
7739 |
"\"crc32\" : %u, " |
|
|
7740 |
"\"compressed-size\" : %u, " |
|
|
7741 |
"\"uncompressed-size\" : %u, " |
|
|
7742 |
"\"file-name-length\" : %u, " |
|
|
7743 |
"\"extra-field-length\" : %u, " |
|
|
7744 |
"\"file-comment-length\" : %u, " |
|
|
7745 |
"\"disk-number-start\" : %u, " |
|
|
7746 |
"\"internal-attr\" : %u, " |
|
|
7747 |
"\"external-attr\" : %u, " |
|
|
7748 |
"\"offset\" : %u }", |
|
|
7749 |
(u32)p->iVersionMadeBy, (u32)p->iVersionExtract, |
|
|
7750 |
(u32)p->flags, (u32)p->iCompression, |
|
|
7751 |
(u32)p->mTime, (u32)p->mDate, |
|
|
7752 |
(u32)p->crc32, (u32)p->szCompressed, |
|
|
7753 |
(u32)p->szUncompressed, (u32)p->nFile, |
|
|
7754 |
(u32)p->nExtra, (u32)p->nComment, |
|
|
7755 |
(u32)p->iDiskStart, (u32)p->iInternalAttr, |
|
|
7756 |
(u32)p->iExternalAttr, (u32)p->iOffset |
|
|
7757 |
); |
|
|
7758 |
|
|
|
7759 |
if( zRes==0 ){ |
|
|
7760 |
sqlite3_result_error_nomem(context); |
|
|
7761 |
}else{ |
|
|
7762 |
sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT); |
|
|
7763 |
sqlite3_free(zRes); |
|
|
7764 |
} |
|
|
7765 |
} |
|
|
7766 |
} |
|
|
7767 |
|
|
|
7768 |
/* |
|
|
7769 |
** xFindFunction method. |
|
|
7770 |
*/ |
|
|
7771 |
static int zipfileFindFunction( |
|
|
7772 |
sqlite3_vtab *pVtab, /* Virtual table handle */ |
|
|
7773 |
int nArg, /* Number of SQL function arguments */ |
|
|
7774 |
const char *zName, /* Name of SQL function */ |
|
|
7775 |
void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */ |
|
|
7776 |
void **ppArg /* OUT: User data for *pxFunc */ |
|
|
7777 |
){ |
|
|
7778 |
if( sqlite3_stricmp("zipfile_cds", zName)==0 ){ |
|
|
7779 |
*pxFunc = zipfileFunctionCds; |
|
|
7780 |
*ppArg = (void*)pVtab; |
|
|
7781 |
return 1; |
|
|
7782 |
} |
|
|
7783 |
return 0; |
|
|
7784 |
} |
|
|
7785 |
|
|
|
7786 |
typedef struct ZipfileBuffer ZipfileBuffer; |
|
|
7787 |
struct ZipfileBuffer { |
|
|
7788 |
u8 *a; /* Pointer to buffer */ |
|
|
7789 |
int n; /* Size of buffer in bytes */ |
|
|
7790 |
int nAlloc; /* Byte allocated at a[] */ |
|
|
7791 |
}; |
|
|
7792 |
|
|
|
7793 |
typedef struct ZipfileCtx ZipfileCtx; |
|
|
7794 |
struct ZipfileCtx { |
|
|
7795 |
int nEntry; |
|
|
7796 |
ZipfileBuffer body; |
|
|
7797 |
ZipfileBuffer cds; |
|
|
7798 |
}; |
|
|
7799 |
|
|
|
7800 |
static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){ |
|
|
7801 |
if( pBuf->n+nByte>pBuf->nAlloc ){ |
|
|
7802 |
u8 *aNew; |
|
|
7803 |
sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512; |
|
|
7804 |
int nReq = pBuf->n + nByte; |
|
|
7805 |
|
|
|
7806 |
while( nNew |
|
|
7807 |
aNew = sqlite3_realloc64(pBuf->a, nNew); |
|
|
7808 |
if( aNew==0 ) return SQLITE_NOMEM; |
|
|
7809 |
pBuf->a = aNew; |
|
|
7810 |
pBuf->nAlloc = (int)nNew; |
|
|
7811 |
} |
|
|
7812 |
return SQLITE_OK; |
|
|
7813 |
} |
|
|
7814 |
|
|
|
7815 |
/* |
|
|
7816 |
** xStep() callback for the zipfile() aggregate. This can be called in |
|
|
7817 |
** any of the following ways: |
|
|
7818 |
** |
|
|
7819 |
** SELECT zipfile(name,data) ... |
|
|
7820 |
** SELECT zipfile(name,mode,mtime,data) ... |
|
|
7821 |
** SELECT zipfile(name,mode,mtime,data,method) ... |
|
|
7822 |
*/ |
|
|
7823 |
void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){ |
|
|
7824 |
ZipfileCtx *p; /* Aggregate function context */ |
|
|
7825 |
ZipfileEntry e; /* New entry to add to zip archive */ |
|
|
7826 |
|
|
|
7827 |
sqlite3_value *pName = 0; |
|
|
7828 |
sqlite3_value *pMode = 0; |
|
|
7829 |
sqlite3_value *pMtime = 0; |
|
|
7830 |
sqlite3_value *pData = 0; |
|
|
7831 |
sqlite3_value *pMethod = 0; |
|
|
7832 |
|
|
|
7833 |
int bIsDir = 0; |
|
|
7834 |
u32 mode; |
|
|
7835 |
int rc = SQLITE_OK; |
|
|
7836 |
char *zErr = 0; |
|
|
7837 |
|
|
|
7838 |
int iMethod = -1; /* Compression method to use (0 or 8) */ |
|
|
7839 |
|
|
|
7840 |
const u8 *aData = 0; /* Possibly compressed data for new entry */ |
|
|
7841 |
int nData = 0; /* Size of aData[] in bytes */ |
|
|
7842 |
int szUncompressed = 0; /* Size of data before compression */ |
|
|
7843 |
u8 *aFree = 0; /* Free this before returning */ |
|
|
7844 |
u32 iCrc32 = 0; /* crc32 of uncompressed data */ |
|
|
7845 |
|
|
|
7846 |
char *zName = 0; /* Path (name) of new entry */ |
|
|
7847 |
int nName = 0; /* Size of zName in bytes */ |
|
|
7848 |
char *zFree = 0; /* Free this before returning */ |
|
|
7849 |
int nByte; |
|
|
7850 |
|
|
|
7851 |
memset(&e, 0, sizeof(e)); |
|
|
7852 |
p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx)); |
|
|
7853 |
if( p==0 ) return; |
|
|
7854 |
|
|
|
7855 |
/* Martial the arguments into stack variables */ |
|
|
7856 |
if( nVal!=2 && nVal!=4 && nVal!=5 ){ |
|
|
7857 |
zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()"); |
|
|
7858 |
rc = SQLITE_ERROR; |
|
|
7859 |
goto zipfile_step_out; |
|
|
7860 |
} |
|
|
7861 |
pName = apVal[0]; |
|
|
7862 |
if( nVal==2 ){ |
|
|
7863 |
pData = apVal[1]; |
|
|
7864 |
}else{ |
|
|
7865 |
pMode = apVal[1]; |
|
|
7866 |
pMtime = apVal[2]; |
|
|
7867 |
pData = apVal[3]; |
|
|
7868 |
if( nVal==5 ){ |
|
|
7869 |
pMethod = apVal[4]; |
|
|
7870 |
} |
|
|
7871 |
} |
|
|
7872 |
|
|
|
7873 |
/* Check that the 'name' parameter looks ok. */ |
|
|
7874 |
zName = (char*)sqlite3_value_text(pName); |
|
|
7875 |
nName = sqlite3_value_bytes(pName); |
|
|
7876 |
if( zName==0 ){ |
|
|
7877 |
zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL"); |
|
|
7878 |
rc = SQLITE_ERROR; |
|
|
7879 |
goto zipfile_step_out; |
|
|
7880 |
} |
|
|
7881 |
|
|
|
7882 |
/* Inspect the 'method' parameter. This must be either 0 (store), 8 (use |
|
|
7883 |
** deflate compression) or NULL (choose automatically). */ |
|
|
7884 |
if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){ |
|
|
7885 |
iMethod = (int)sqlite3_value_int64(pMethod); |
|
|
7886 |
if( iMethod!=0 && iMethod!=8 ){ |
|
|
7887 |
zErr = sqlite3_mprintf("illegal method value: %d", iMethod); |
|
|
7888 |
rc = SQLITE_ERROR; |
|
|
7889 |
goto zipfile_step_out; |
|
|
7890 |
} |
|
|
7891 |
} |
|
|
7892 |
|
|
|
7893 |
/* Now inspect the data. If this is NULL, then the new entry must be a |
|
|
7894 |
** directory. Otherwise, figure out whether or not the data should |
|
|
7895 |
** be deflated or simply stored in the zip archive. */ |
|
|
7896 |
if( sqlite3_value_type(pData)==SQLITE_NULL ){ |
|
|
7897 |
bIsDir = 1; |
|
|
7898 |
iMethod = 0; |
|
|
7899 |
}else{ |
|
|
7900 |
aData = sqlite3_value_blob(pData); |
|
|
7901 |
szUncompressed = nData = sqlite3_value_bytes(pData); |
|
|
7902 |
iCrc32 = crc32(0, aData, nData); |
|
|
7903 |
if( iMethod<0 || iMethod==8 ){ |
|
|
7904 |
int nOut = 0; |
|
|
7905 |
rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr); |
|
|
7906 |
if( rc!=SQLITE_OK ){ |
|
|
7907 |
goto zipfile_step_out; |
|
|
7908 |
} |
|
|
7909 |
if( iMethod==8 || nOut |
|
|
7910 |
aData = aFree; |
|
|
7911 |
nData = nOut; |
|
|
7912 |
iMethod = 8; |
|
|
7913 |
}else{ |
|
|
7914 |
iMethod = 0; |
|
|
7915 |
} |
|
|
7916 |
} |
|
|
7917 |
} |
|
|
7918 |
|
|
|
7919 |
/* Decode the "mode" argument. */ |
|
|
7920 |
rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr); |
|
|
7921 |
if( rc ) goto zipfile_step_out; |
|
|
7922 |
|
|
|
7923 |
/* Decode the "mtime" argument. */ |
|
|
7924 |
e.mUnixTime = zipfileGetTime(pMtime); |
|
|
7925 |
|
|
|
7926 |
/* If this is a directory entry, ensure that there is exactly one '/' |
|
|
7927 |
** at the end of the path. Or, if this is not a directory and the path |
|
|
7928 |
** ends in '/' it is an error. */ |
|
|
7929 |
if( bIsDir==0 ){ |
|
|
7930 |
if( nName>0 && zName[nName-1]=='/' ){ |
|
|
7931 |
zErr = sqlite3_mprintf("non-directory name must not end with /"); |
|
|
7932 |
rc = SQLITE_ERROR; |
|
|
7933 |
goto zipfile_step_out; |
|
|
7934 |
} |
|
|
7935 |
}else{ |
|
|
7936 |
if( nName==0 || zName[nName-1]!='/' ){ |
|
|
7937 |
zName = zFree = sqlite3_mprintf("%s/", zName); |
|
|
7938 |
if( zName==0 ){ |
|
|
7939 |
rc = SQLITE_NOMEM; |
|
|
7940 |
goto zipfile_step_out; |
|
|
7941 |
} |
|
|
7942 |
nName = (int)strlen(zName); |
|
|
7943 |
}else{ |
|
|
7944 |
while( nName>1 && zName[nName-2]=='/' ) nName--; |
|
|
7945 |
} |
|
|
7946 |
} |
|
|
7947 |
|
|
|
7948 |
/* Assemble the ZipfileEntry object for the new zip archive entry */ |
|
|
7949 |
e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY; |
|
|
7950 |
e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED; |
|
|
7951 |
e.cds.flags = ZIPFILE_NEWENTRY_FLAGS; |
|
|
7952 |
e.cds.iCompression = (u16)iMethod; |
|
|
7953 |
zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime); |
|
|
7954 |
e.cds.crc32 = iCrc32; |
|
|
7955 |
e.cds.szCompressed = nData; |
|
|
7956 |
e.cds.szUncompressed = szUncompressed; |
|
|
7957 |
e.cds.iExternalAttr = (mode<<16); |
|
|
7958 |
e.cds.iOffset = p->body.n; |
|
|
7959 |
e.cds.nFile = (u16)nName; |
|
|
7960 |
e.cds.zFile = zName; |
|
|
7961 |
|
|
|
7962 |
/* Append the LFH to the body of the new archive */ |
|
|
7963 |
nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9; |
|
|
7964 |
if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out; |
|
|
7965 |
p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]); |
|
|
7966 |
|
|
|
7967 |
/* Append the data to the body of the new archive */ |
|
|
7968 |
if( nData>0 ){ |
|
|
7969 |
if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out; |
|
|
7970 |
memcpy(&p->body.a[p->body.n], aData, nData); |
|
|
7971 |
p->body.n += nData; |
|
|
7972 |
} |
|
|
7973 |
|
|
|
7974 |
/* Append the CDS record to the directory of the new archive */ |
|
|
7975 |
nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9; |
|
|
7976 |
if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out; |
|
|
7977 |
p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]); |
|
|
7978 |
|
|
|
7979 |
/* Increment the count of entries in the archive */ |
|
|
7980 |
p->nEntry++; |
|
|
7981 |
|
|
|
7982 |
zipfile_step_out: |
|
|
7983 |
sqlite3_free(aFree); |
|
|
7984 |
sqlite3_free(zFree); |
|
|
7985 |
if( rc ){ |
|
|
7986 |
if( zErr ){ |
|
|
7987 |
sqlite3_result_error(pCtx, zErr, -1); |
|
|
7988 |
}else{ |
|
|
7989 |
sqlite3_result_error_code(pCtx, rc); |
|
|
7990 |
} |
|
|
7991 |
} |
|
|
7992 |
sqlite3_free(zErr); |
|
|
7993 |
} |
|
|
7994 |
|
|
|
7995 |
/* |
|
|
7996 |
** xFinalize() callback for zipfile aggregate function. |
|
|
7997 |
*/ |
|
|
7998 |
void zipfileFinal(sqlite3_context *pCtx){ |
|
|
7999 |
ZipfileCtx *p; |
|
|
8000 |
ZipfileEOCD eocd; |
|
|
8001 |
sqlite3_int64 nZip; |
|
|
8002 |
u8 *aZip; |
|
|
8003 |
|
|
|
8004 |
p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx)); |
|
|
8005 |
if( p==0 ) return; |
|
|
8006 |
if( p->nEntry>0 ){ |
|
|
8007 |
memset(&eocd, 0, sizeof(eocd)); |
|
|
8008 |
eocd.nEntry = (u16)p->nEntry; |
|
|
8009 |
eocd.nEntryTotal = (u16)p->nEntry; |
|
|
8010 |
eocd.nSize = p->cds.n; |
|
|
8011 |
eocd.iOffset = p->body.n; |
|
|
8012 |
|
|
|
8013 |
nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ; |
|
|
8014 |
aZip = (u8*)sqlite3_malloc64(nZip); |
|
|
8015 |
if( aZip==0 ){ |
|
|
8016 |
sqlite3_result_error_nomem(pCtx); |
|
|
8017 |
}else{ |
|
|
8018 |
memcpy(aZip, p->body.a, p->body.n); |
|
|
8019 |
memcpy(&aZip[p->body.n], p->cds.a, p->cds.n); |
|
|
8020 |
zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]); |
|
|
8021 |
sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree); |
|
|
8022 |
} |
|
|
8023 |
} |
|
|
8024 |
|
|
|
8025 |
sqlite3_free(p->body.a); |
|
|
8026 |
sqlite3_free(p->cds.a); |
|
|
8027 |
} |
|
|
8028 |
|
|
|
8029 |
|
|
|
8030 |
/* |
|
|
8031 |
** Register the "zipfile" virtual table. |
|
|
8032 |
*/ |
|
|
8033 |
static int zipfileRegister(sqlite3 *db){ |
|
|
8034 |
static sqlite3_module zipfileModule = { |
|
|
8035 |
1, /* iVersion */ |
|
|
8036 |
zipfileConnect, /* xCreate */ |
|
|
8037 |
zipfileConnect, /* xConnect */ |
|
|
8038 |
zipfileBestIndex, /* xBestIndex */ |
|
|
8039 |
zipfileDisconnect, /* xDisconnect */ |
|
|
8040 |
zipfileDisconnect, /* xDestroy */ |
|
|
8041 |
zipfileOpen, /* xOpen - open a cursor */ |
|
|
8042 |
zipfileClose, /* xClose - close a cursor */ |
|
|
8043 |
zipfileFilter, /* xFilter - configure scan constraints */ |
|
|
8044 |
zipfileNext, /* xNext - advance a cursor */ |
|
|
8045 |
zipfileEof, /* xEof - check for end of scan */ |
|
|
8046 |
zipfileColumn, /* xColumn - read data */ |
|
|
8047 |
0, /* xRowid - read data */ |
|
|
8048 |
zipfileUpdate, /* xUpdate */ |
|
|
8049 |
zipfileBegin, /* xBegin */ |
|
|
8050 |
0, /* xSync */ |
|
|
8051 |
zipfileCommit, /* xCommit */ |
|
|
8052 |
zipfileRollback, /* xRollback */ |
|
|
8053 |
zipfileFindFunction, /* xFindMethod */ |
|
|
8054 |
0, /* xRename */ |
|
|
8055 |
}; |
|
|
8056 |
|
|
|
8057 |
int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0); |
|
|
8058 |
if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1); |
|
|
8059 |
if( rc==SQLITE_OK ){ |
|
|
8060 |
rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, |
|
|
8061 |
zipfileStep, zipfileFinal |
|
|
8062 |
); |
|
|
8063 |
} |
|
|
8064 |
return rc; |
|
|
8065 |
} |
|
|
8066 |
#else /* SQLITE_OMIT_VIRTUALTABLE */ |
|
|
8067 |
# define zipfileRegister(x) SQLITE_OK |
|
|
8068 |
#endif |
|
|
8069 |
|
|
|
8070 |
#ifdef _WIN32 |
|
|
8071 |
|
|
|
8072 |
#endif |
|
|
8073 |
int sqlite3_zipfile_init( |
|
|
8074 |
sqlite3 *db, |
|
|
8075 |
char **pzErrMsg, |
|
|
8076 |
const sqlite3_api_routines *pApi |
|
|
8077 |
){ |
|
|
8078 |
SQLITE_EXTENSION_INIT2(pApi); |
|
|
8079 |
(void)pzErrMsg; /* Unused parameter */ |
|
|
8080 |
return zipfileRegister(db); |
|
|
8081 |
} |
|
|
8082 |
|
|
|
8083 |
/************************* End ../ext/misc/zipfile.c ********************/ |
|
|
8084 |
/************************* Begin ../ext/misc/sqlar.c ******************/ |
|
|
8085 |
/* |
|
|
8086 |
** 2017-12-17 |
|
|
8087 |
** |
|
|
8088 |
** The author disclaims copyright to this source code. In place of |
|
|
8089 |
** a legal notice, here is a blessing: |
|
|
8090 |
** |
|
|
8091 |
** May you do good and not evil. |
|
|
8092 |
** May you find forgiveness for yourself and forgive others. |
|
|
8093 |
** May you share freely, never taking more than you give. |
|
|
8094 |
** |
|
|
8095 |
****************************************************************************** |
|
|
8096 |
** |
|
|
8097 |
** Utility functions sqlar_compress() and sqlar_uncompress(). Useful |
|
|
8098 |
** for working with sqlar archives and used by the shell tool's built-in |
|
|
8099 |
** sqlar support. |
|
|
8100 |
*/ |
|
|
8101 |
/* #include "sqlite3ext.h" */ |
|
|
8102 |
SQLITE_EXTENSION_INIT1 |
|
|
8103 |
#include |
|
|
8104 |
#include |
|
|
8105 |
|
|
|
8106 |
/* |
|
|
8107 |
** Implementation of the "sqlar_compress(X)" SQL function. |
|
|
8108 |
** |
|
|
8109 |
** If the type of X is SQLITE_BLOB, and compressing that blob using |
|
|
8110 |
** zlib utility function compress() yields a smaller blob, return the |
|
|
8111 |
** compressed blob. Otherwise, return a copy of X. |
|
|
8112 |
** |
|
|
8113 |
** SQLar uses the "zlib format" for compressed content. The zlib format |
|
|
8114 |
** contains a two-byte identification header and a four-byte checksum at |
|
|
8115 |
** the end. This is different from ZIP which uses the raw deflate format. |
|
|
8116 |
** |
|
|
8117 |
** Future enhancements to SQLar might add support for new compression formats. |
|
|
8118 |
** If so, those new formats will be identified by alternative headers in the |
|
|
8119 |
** compressed data. |
|
|
8120 |
*/ |
|
|
8121 |
static void sqlarCompressFunc( |
|
|
8122 |
sqlite3_context *context, |
|
|
8123 |
int argc, |
|
|
8124 |
sqlite3_value **argv |
|
|
8125 |
){ |
|
|
8126 |
assert( argc==1 ); |
|
|
8127 |
if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){ |
|
|
8128 |
const Bytef *pData = sqlite3_value_blob(argv[0]); |
|
|
8129 |
uLong nData = sqlite3_value_bytes(argv[0]); |
|
|
8130 |
uLongf nOut = compressBound(nData); |
|
|
8131 |
Bytef *pOut; |
|
|
8132 |
|
|
|
8133 |
pOut = (Bytef*)sqlite3_malloc(nOut); |
|
|
8134 |
if( pOut==0 ){ |
|
|
8135 |
sqlite3_result_error_nomem(context); |
|
|
8136 |
return; |
|
|
8137 |
}else{ |
|
|
8138 |
if( Z_OK!=compress(pOut, &nOut, pData, nData) ){ |
|
|
8139 |
sqlite3_result_error(context, "error in compress()", -1); |
|
|
8140 |
}else if( nOut |
|
|
8141 |
sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT); |
|
|
8142 |
}else{ |
|
|
8143 |
sqlite3_result_value(context, argv[0]); |
|
|
8144 |
} |
|
|
8145 |
sqlite3_free(pOut); |
|
|
8146 |
} |
|
|
8147 |
}else{ |
|
|
8148 |
sqlite3_result_value(context, argv[0]); |
|
|
8149 |
} |
|
|
8150 |
} |
|
|
8151 |
|
|
|
8152 |
/* |
|
|
8153 |
** Implementation of the "sqlar_uncompress(X,SZ)" SQL function |
|
|
8154 |
** |
|
|
8155 |
** Parameter SZ is interpreted as an integer. If it is less than or |
|
|
8156 |
** equal to zero, then this function returns a copy of X. Or, if |
|
|
8157 |
** SZ is equal to the size of X when interpreted as a blob, also |
|
|
8158 |
** return a copy of X. Otherwise, decompress blob X using zlib |
|
|
8159 |
** utility function uncompress() and return the results (another |
|
|
8160 |
** blob). |
|
|
8161 |
*/ |
|
|
8162 |
static void sqlarUncompressFunc( |
|
|
8163 |
sqlite3_context *context, |
|
|
8164 |
int argc, |
|
|
8165 |
sqlite3_value **argv |
|
|
8166 |
){ |
|
|
8167 |
uLong nData; |
|
|
8168 |
uLongf sz; |
|
|
8169 |
|
|
|
8170 |
assert( argc==2 ); |
|
|
8171 |
sz = sqlite3_value_int(argv[1]); |
|
|
8172 |
|
|
|
8173 |
if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){ |
|
|
8174 |
sqlite3_result_value(context, argv[0]); |
|
|
8175 |
}else{ |
|
|
8176 |
const Bytef *pData= sqlite3_value_blob(argv[0]); |
|
|
8177 |
Bytef *pOut = sqlite3_malloc(sz); |
|
|
8178 |
if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){ |
|
|
8179 |
sqlite3_result_error(context, "error in uncompress()", -1); |
|
|
8180 |
}else{ |
|
|
8181 |
sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT); |
|
|
8182 |
} |
|
|
8183 |
sqlite3_free(pOut); |
|
|
8184 |
} |
|
|
8185 |
} |
|
|
8186 |
|
|
|
8187 |
|
|
|
8188 |
#ifdef _WIN32 |
|
|
8189 |
|
|
|
8190 |
#endif |
|
|
8191 |
int sqlite3_sqlar_init( |
|
|
8192 |
sqlite3 *db, |
|
|
8193 |
char **pzErrMsg, |
|
|
8194 |
const sqlite3_api_routines *pApi |
|
|
8195 |
){ |
|
|
8196 |
int rc = SQLITE_OK; |
|
|
8197 |
SQLITE_EXTENSION_INIT2(pApi); |
|
|
8198 |
(void)pzErrMsg; /* Unused parameter */ |
|
|
8199 |
rc = sqlite3_create_function(db, "sqlar_compress", 1, |
|
|
8200 |
SQLITE_UTF8|SQLITE_INNOCUOUS, 0, |
|
|
8201 |
sqlarCompressFunc, 0, 0); |
|
|
8202 |
if( rc==SQLITE_OK ){ |
|
|
8203 |
rc = sqlite3_create_function(db, "sqlar_uncompress", 2, |
|
|
8204 |
SQLITE_UTF8|SQLITE_INNOCUOUS, 0, |
|
|
8205 |
sqlarUncompressFunc, 0, 0); |
|
|
8206 |
} |
|
|
8207 |
return rc; |
|
|
8208 |
} |
|
|
8209 |
|
|
|
8210 |
/************************* End ../ext/misc/sqlar.c ********************/ |
|
|
8211 |
#endif |
|
|
8212 |
/************************* Begin ../ext/expert/sqlite3expert.h ******************/ |
|
|
8213 |
/* |
|
|
8214 |
** 2017 April 07 |
|
|
8215 |
** |
|
|
8216 |
** The author disclaims copyright to this source code. In place of |
|
|
8217 |
** a legal notice, here is a blessing: |
|
|
8218 |
** |
|
|
8219 |
** May you do good and not evil. |
|
|
8220 |
** May you find forgiveness for yourself and forgive others. |
|
|
8221 |
** May you share freely, never taking more than you give. |
|
|
8222 |
** |
|
|
8223 |
************************************************************************* |
|
|
8224 |
*/ |
|
|
8225 |
#if !defined(SQLITEEXPERT_H) |
|
|
8226 |
#define SQLITEEXPERT_H 1 |
|
|
8227 |
/* #include "sqlite3.h" */ |
|
|
8228 |
|
|
|
8229 |
typedef struct sqlite3expert sqlite3expert; |
|
|
8230 |
|
|
|
8231 |
/* |
|
|
8232 |
** Create a new sqlite3expert object. |
|
|
8233 |
** |
|
|
8234 |
** If successful, a pointer to the new object is returned and (*pzErr) set |
|
|
8235 |
** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to |
|
|
8236 |
** an English-language error message. In this case it is the responsibility |
|
|
8237 |
** of the caller to eventually free the error message buffer using |
|
|
8238 |
** sqlite3_free(). |
|
|
8239 |
*/ |
|
|
8240 |
sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr); |
|
|
8241 |
|
|
|
8242 |
/* |
|
|
8243 |
** Configure an sqlite3expert object. |
|
|
8244 |
** |
|
|
8245 |
** EXPERT_CONFIG_SAMPLE: |
|
|
8246 |
** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for |
|
|
8247 |
** each candidate index. This involves scanning and sorting the entire |
|
|
8248 |
** contents of each user database table once for each candidate index |
|
|
8249 |
** associated with the table. For large databases, this can be |
|
|
8250 |
** prohibitively slow. This option allows the sqlite3expert object to |
|
|
8251 |
** be configured so that sqlite_stat1 data is instead generated based on a |
|
|
8252 |
** subset of each table, or so that no sqlite_stat1 data is used at all. |
|
|
8253 |
** |
|
|
8254 |
** A single integer argument is passed to this option. If the value is less |
|
|
8255 |
** than or equal to zero, then no sqlite_stat1 data is generated or used by |
|
|
8256 |
** the analysis - indexes are recommended based on the database schema only. |
|
|
8257 |
** Or, if the value is 100 or greater, complete sqlite_stat1 data is |
|
|
8258 |
** generated for each candidate index (this is the default). Finally, if the |
|
|
8259 |
** value falls between 0 and 100, then it represents the percentage of user |
|
|
8260 |
** table rows that should be considered when generating sqlite_stat1 data. |
|
|
8261 |
** |
|
|
8262 |
** Examples: |
|
|
8263 |
** |
|
|
8264 |
** // Do not generate any sqlite_stat1 data |
|
|
8265 |
** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0); |
|
|
8266 |
** |
|
|
8267 |
** // Generate sqlite_stat1 data based on 10% of the rows in each table. |
|
|
8268 |
** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10); |
|
|
8269 |
*/ |
|
|
8270 |
int sqlite3_expert_config(sqlite3expert *p, int op, ...); |
|
|
8271 |
|
|
|
8272 |
#define EXPERT_CONFIG_SAMPLE 1 /* int */ |
|
|
8273 |
|
|
|
8274 |
/* |
|
|
8275 |
** Specify zero or more SQL statements to be included in the analysis. |
|
|
8276 |
** |
|
|
8277 |
** Buffer zSql must contain zero or more complete SQL statements. This |
|
|
8278 |
** function parses all statements contained in the buffer and adds them |
|
|
8279 |
** to the internal list of statements to analyze. If successful, SQLITE_OK |
|
|
8280 |
** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example |
|
|
8281 |
** due to a error in the SQL - an SQLite error code is returned and (*pzErr) |
|
|
8282 |
** may be set to point to an English language error message. In this case |
|
|
8283 |
** the caller is responsible for eventually freeing the error message buffer |
|
|
8284 |
** using sqlite3_free(). |
|
|
8285 |
** |
|
|
8286 |
** If an error does occur while processing one of the statements in the |
|
|
8287 |
** buffer passed as the second argument, none of the statements in the |
|
|
8288 |
** buffer are added to the analysis. |
|
|
8289 |
** |
|
|
8290 |
** This function must be called before sqlite3_expert_analyze(). If a call |
|
|
8291 |
** to this function is made on an sqlite3expert object that has already |
|
|
8292 |
** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned |
|
|
8293 |
** immediately and no statements are added to the analysis. |
|
|
8294 |
*/ |
|
|
8295 |
int sqlite3_expert_sql( |
|
|
8296 |
sqlite3expert *p, /* From a successful sqlite3_expert_new() */ |
|
|
8297 |
const char *zSql, /* SQL statement(s) to add */ |
|
|
8298 |
char **pzErr /* OUT: Error message (if any) */ |
|
|
8299 |
); |
|
|
8300 |
|
|
|
8301 |
|
|
|
8302 |
/* |
|
|
8303 |
** This function is called after the sqlite3expert object has been configured |
|
|
8304 |
** with all SQL statements using sqlite3_expert_sql() to actually perform |
|
|
8305 |
** the analysis. Once this function has been called, it is not possible to |
|
|
8306 |
** add further SQL statements to the analysis. |
|
|
8307 |
** |
|
|
8308 |
** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if |
|
|
8309 |
** an error occurs, an SQLite error code is returned and (*pzErr) set to |
|
|
8310 |
** point to a buffer containing an English language error message. In this |
|
|
8311 |
** case it is the responsibility of the caller to eventually free the buffer |
|
|
8312 |
** using sqlite3_free(). |
|
|
8313 |
** |
|
|
8314 |
** If an error does occur within this function, the sqlite3expert object |
|
|
8315 |
** is no longer useful for any purpose. At that point it is no longer |
|
|
8316 |
** possible to add further SQL statements to the object or to re-attempt |
|
|
8317 |
** the analysis. The sqlite3expert object must still be freed using a call |
|
|
8318 |
** sqlite3_expert_destroy(). |
|
|
8319 |
*/ |
|
|
8320 |
int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr); |
|
|
8321 |
|
|
|
8322 |
/* |
|
|
8323 |
** Return the total number of statements loaded using sqlite3_expert_sql(). |
|
|
8324 |
** The total number of SQL statements may be different from the total number |
|
|
8325 |
** to calls to sqlite3_expert_sql(). |
|
|
8326 |
*/ |
|
|
8327 |
int sqlite3_expert_count(sqlite3expert*); |
|
|
8328 |
|
|
|
8329 |
/* |
|
|
8330 |
** Return a component of the report. |
|
|
8331 |
** |
|
|
8332 |
** This function is called after sqlite3_expert_analyze() to extract the |
|
|
8333 |
** results of the analysis. Each call to this function returns either a |
|
|
8334 |
** NULL pointer or a pointer to a buffer containing a nul-terminated string. |
|
|
8335 |
** The value passed as the third argument must be one of the EXPERT_REPORT_* |
|
|
8336 |
** #define constants defined below. |
|
|
8337 |
** |
|
|
8338 |
** For some EXPERT_REPORT_* parameters, the buffer returned contains |
|
|
8339 |
** information relating to a specific SQL statement. In these cases that |
|
|
8340 |
** SQL statement is identified by the value passed as the second argument. |
|
|
8341 |
** SQL statements are numbered from 0 in the order in which they are parsed. |
|
|
8342 |
** If an out-of-range value (less than zero or equal to or greater than the |
|
|
8343 |
** value returned by sqlite3_expert_count()) is passed as the second argument |
|
|
8344 |
** along with such an EXPERT_REPORT_* parameter, NULL is always returned. |
|
|
8345 |
** |
|
|
8346 |
** EXPERT_REPORT_SQL: |
|
|
8347 |
** Return the text of SQL statement iStmt. |
|
|
8348 |
** |
|
|
8349 |
** EXPERT_REPORT_INDEXES: |
|
|
8350 |
** Return a buffer containing the CREATE INDEX statements for all recommended |
|
|
8351 |
** indexes for statement iStmt. If there are no new recommeded indexes, NULL |
|
|
8352 |
** is returned. |
|
|
8353 |
** |
|
|
8354 |
** EXPERT_REPORT_PLAN: |
|
|
8355 |
** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query |
|
|
8356 |
** iStmt after the proposed indexes have been added to the database schema. |
|
|
8357 |
** |
|
|
8358 |
** EXPERT_REPORT_CANDIDATES: |
|
|
8359 |
** Return a pointer to a buffer containing the CREATE INDEX statements |
|
|
8360 |
** for all indexes that were tested (for all SQL statements). The iStmt |
|
|
8361 |
** parameter is ignored for EXPERT_REPORT_CANDIDATES calls. |
|
|
8362 |
*/ |
|
|
8363 |
const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport); |
|
|
8364 |
|
|
|
8365 |
/* |
|
|
8366 |
** Values for the third argument passed to sqlite3_expert_report(). |
|
|
8367 |
*/ |
|
|
8368 |
#define EXPERT_REPORT_SQL 1 |
|
|
8369 |
#define EXPERT_REPORT_INDEXES 2 |
|
|
8370 |
#define EXPERT_REPORT_PLAN 3 |
|
|
8371 |
#define EXPERT_REPORT_CANDIDATES 4 |
|
|
8372 |
|
|
|
8373 |
/* |
|
|
8374 |
** Free an (sqlite3expert*) handle and all associated resources. There |
|
|
8375 |
** should be one call to this function for each successful call to |
|
|
8376 |
** sqlite3-expert_new(). |
|
|
8377 |
*/ |
|
|
8378 |
void sqlite3_expert_destroy(sqlite3expert*); |
|
|
8379 |
|
|
|
8380 |
#endif /* !defined(SQLITEEXPERT_H) */ |
|
|
8381 |
|
|
|
8382 |
/************************* End ../ext/expert/sqlite3expert.h ********************/ |
|
|
8383 |
/************************* Begin ../ext/expert/sqlite3expert.c ******************/ |
|
|
8384 |
/* |
|
|
8385 |
** 2017 April 09 |
|
|
8386 |
** |
|
|
8387 |
** The author disclaims copyright to this source code. In place of |
|
|
8388 |
** a legal notice, here is a blessing: |
|
|
8389 |
** |
|
|
8390 |
** May you do good and not evil. |
|
|
8391 |
** May you find forgiveness for yourself and forgive others. |
|
|
8392 |
** May you share freely, never taking more than you give. |
|
|
8393 |
** |
|
|
8394 |
************************************************************************* |
|
|
8395 |
*/ |
|
|
8396 |
/* #include "sqlite3expert.h" */ |
|
|
8397 |
#include |
|
|
8398 |
#include |
|
|
8399 |
#include |
|
|
8400 |
|
|
|
8401 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
8402 |
|
|
|
8403 |
/* typedef sqlite3_int64 i64; */ |
|
|
8404 |
/* typedef sqlite3_uint64 u64; */ |
|
|
8405 |
|
|
|
8406 |
typedef struct IdxColumn IdxColumn; |
|
|
8407 |
typedef struct IdxConstraint IdxConstraint; |
|
|
8408 |
typedef struct IdxScan IdxScan; |
|
|
8409 |
typedef struct IdxStatement IdxStatement; |
|
|
8410 |
typedef struct IdxTable IdxTable; |
|
|
8411 |
typedef struct IdxWrite IdxWrite; |
|
|
8412 |
|
|
|
8413 |
#define STRLEN (int)strlen |
|
|
8414 |
|
|
|
8415 |
/* |
|
|
8416 |
** A temp table name that we assume no user database will actually use. |
|
|
8417 |
** If this assumption proves incorrect triggers on the table with the |
|
|
8418 |
** conflicting name will be ignored. |
|
|
8419 |
*/ |
|
|
8420 |
#define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776" |
|
|
8421 |
|
|
|
8422 |
/* |
|
|
8423 |
** A single constraint. Equivalent to either "col = ?" or "col < ?" (or |
|
|
8424 |
** any other type of single-ended range constraint on a column). |
|
|
8425 |
** |
|
|
8426 |
** pLink: |
|
|
8427 |
** Used to temporarily link IdxConstraint objects into lists while |
|
|
8428 |
** creating candidate indexes. |
|
|
8429 |
*/ |
|
|
8430 |
struct IdxConstraint { |
|
|
8431 |
char *zColl; /* Collation sequence */ |
|
|
8432 |
int bRange; /* True for range, false for eq */ |
|
|
8433 |
int iCol; /* Constrained table column */ |
|
|
8434 |
int bFlag; /* Used by idxFindCompatible() */ |
|
|
8435 |
int bDesc; /* True if ORDER BY DESC */ |
|
|
8436 |
IdxConstraint *pNext; /* Next constraint in pEq or pRange list */ |
|
|
8437 |
IdxConstraint *pLink; /* See above */ |
|
|
8438 |
}; |
|
|
8439 |
|
|
|
8440 |
/* |
|
|
8441 |
** A single scan of a single table. |
|
|
8442 |
*/ |
|
|
8443 |
struct IdxScan { |
|
|
8444 |
IdxTable *pTab; /* Associated table object */ |
|
|
8445 |
int iDb; /* Database containing table zTable */ |
|
|
8446 |
i64 covering; /* Mask of columns required for cov. index */ |
|
|
8447 |
IdxConstraint *pOrder; /* ORDER BY columns */ |
|
|
8448 |
IdxConstraint *pEq; /* List of == constraints */ |
|
|
8449 |
IdxConstraint *pRange; /* List of < constraints */ |
|
|
8450 |
IdxScan *pNextScan; /* Next IdxScan object for same analysis */ |
|
|
8451 |
}; |
|
|
8452 |
|
|
|
8453 |
/* |
|
|
8454 |
** Information regarding a single database table. Extracted from |
|
|
8455 |
** "PRAGMA table_info" by function idxGetTableInfo(). |
|
|
8456 |
*/ |
|
|
8457 |
struct IdxColumn { |
|
|
8458 |
char *zName; |
|
|
8459 |
char *zColl; |
|
|
8460 |
int iPk; |
|
|
8461 |
}; |
|
|
8462 |
struct IdxTable { |
|
|
8463 |
int nCol; |
|
|
8464 |
char *zName; /* Table name */ |
|
|
8465 |
IdxColumn *aCol; |
|
|
8466 |
IdxTable *pNext; /* Next table in linked list of all tables */ |
|
|
8467 |
}; |
|
|
8468 |
|
|
|
8469 |
/* |
|
|
8470 |
** An object of the following type is created for each unique table/write-op |
|
|
8471 |
** seen. The objects are stored in a singly-linked list beginning at |
|
|
8472 |
** sqlite3expert.pWrite. |
|
|
8473 |
*/ |
|
|
8474 |
struct IdxWrite { |
|
|
8475 |
IdxTable *pTab; |
|
|
8476 |
int eOp; /* SQLITE_UPDATE, DELETE or INSERT */ |
|
|
8477 |
IdxWrite *pNext; |
|
|
8478 |
}; |
|
|
8479 |
|
|
|
8480 |
/* |
|
|
8481 |
** Each statement being analyzed is represented by an instance of this |
|
|
8482 |
** structure. |
|
|
8483 |
*/ |
|
|
8484 |
struct IdxStatement { |
|
|
8485 |
int iId; /* Statement number */ |
|
|
8486 |
char *zSql; /* SQL statement */ |
|
|
8487 |
char *zIdx; /* Indexes */ |
|
|
8488 |
char *zEQP; /* Plan */ |
|
|
8489 |
IdxStatement *pNext; |
|
|
8490 |
}; |
|
|
8491 |
|
|
|
8492 |
|
|
|
8493 |
/* |
|
|
8494 |
** A hash table for storing strings. With space for a payload string |
|
|
8495 |
** with each entry. Methods are: |
|
|
8496 |
** |
|
|
8497 |
** idxHashInit() |
|
|
8498 |
** idxHashClear() |
|
|
8499 |
** idxHashAdd() |
|
|
8500 |
** idxHashSearch() |
|
|
8501 |
*/ |
|
|
8502 |
#define IDX_HASH_SIZE 1023 |
|
|
8503 |
typedef struct IdxHashEntry IdxHashEntry; |
|
|
8504 |
typedef struct IdxHash IdxHash; |
|
|
8505 |
struct IdxHashEntry { |
|
|
8506 |
char *zKey; /* nul-terminated key */ |
|
|
8507 |
char *zVal; /* nul-terminated value string */ |
|
|
8508 |
char *zVal2; /* nul-terminated value string 2 */ |
|
|
8509 |
IdxHashEntry *pHashNext; /* Next entry in same hash bucket */ |
|
|
8510 |
IdxHashEntry *pNext; /* Next entry in hash */ |
|
|
8511 |
}; |
|
|
8512 |
struct IdxHash { |
|
|
8513 |
IdxHashEntry *pFirst; |
|
|
8514 |
IdxHashEntry *aHash[IDX_HASH_SIZE]; |
|
|
8515 |
}; |
|
|
8516 |
|
|
|
8517 |
/* |
|
|
8518 |
** sqlite3expert object. |
|
|
8519 |
*/ |
|
|
8520 |
struct sqlite3expert { |
|
|
8521 |
int iSample; /* Percentage of tables to sample for stat1 */ |
|
|
8522 |
sqlite3 *db; /* User database */ |
|
|
8523 |
sqlite3 *dbm; /* In-memory db for this analysis */ |
|
|
8524 |
sqlite3 *dbv; /* Vtab schema for this analysis */ |
|
|
8525 |
IdxTable *pTable; /* List of all IdxTable objects */ |
|
|
8526 |
IdxScan *pScan; /* List of scan objects */ |
|
|
8527 |
IdxWrite *pWrite; /* List of write objects */ |
|
|
8528 |
IdxStatement *pStatement; /* List of IdxStatement objects */ |
|
|
8529 |
int bRun; /* True once analysis has run */ |
|
|
8530 |
char **pzErrmsg; |
|
|
8531 |
int rc; /* Error code from whereinfo hook */ |
|
|
8532 |
IdxHash hIdx; /* Hash containing all candidate indexes */ |
|
|
8533 |
char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */ |
|
|
8534 |
}; |
|
|
8535 |
|
|
|
8536 |
|
|
|
8537 |
/* |
|
|
8538 |
** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc(). |
|
|
8539 |
** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL. |
|
|
8540 |
*/ |
|
|
8541 |
static void *idxMalloc(int *pRc, int nByte){ |
|
|
8542 |
void *pRet; |
|
|
8543 |
assert( *pRc==SQLITE_OK ); |
|
|
8544 |
assert( nByte>0 ); |
|
|
8545 |
pRet = sqlite3_malloc(nByte); |
|
|
8546 |
if( pRet ){ |
|
|
8547 |
memset(pRet, 0, nByte); |
|
|
8548 |
}else{ |
|
|
8549 |
*pRc = SQLITE_NOMEM; |
|
|
8550 |
} |
|
|
8551 |
return pRet; |
|
|
8552 |
} |
|
|
8553 |
|
|
|
8554 |
/* |
|
|
8555 |
** Initialize an IdxHash hash table. |
|
|
8556 |
*/ |
|
|
8557 |
static void idxHashInit(IdxHash *pHash){ |
|
|
8558 |
memset(pHash, 0, sizeof(IdxHash)); |
|
|
8559 |
} |
|
|
8560 |
|
|
|
8561 |
/* |
|
|
8562 |
** Reset an IdxHash hash table. |
|
|
8563 |
*/ |
|
|
8564 |
static void idxHashClear(IdxHash *pHash){ |
|
|
8565 |
int i; |
|
|
8566 |
for(i=0; i |
|
|
8567 |
IdxHashEntry *pEntry; |
|
|
8568 |
IdxHashEntry *pNext; |
|
|
8569 |
for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){ |
|
|
8570 |
pNext = pEntry->pHashNext; |
|
|
8571 |
sqlite3_free(pEntry->zVal2); |
|
|
8572 |
sqlite3_free(pEntry); |
|
|
8573 |
} |
|
|
8574 |
} |
|
|
8575 |
memset(pHash, 0, sizeof(IdxHash)); |
|
|
8576 |
} |
|
|
8577 |
|
|
|
8578 |
/* |
|
|
8579 |
** Return the index of the hash bucket that the string specified by the |
|
|
8580 |
** arguments to this function belongs. |
|
|
8581 |
*/ |
|
|
8582 |
static int idxHashString(const char *z, int n){ |
|
|
8583 |
unsigned int ret = 0; |
|
|
8584 |
int i; |
|
|
8585 |
for(i=0; i |
|
|
8586 |
ret += (ret<<3) + (unsigned char)(z[i]); |
|
|
8587 |
} |
|
|
8588 |
return (int)(ret % IDX_HASH_SIZE); |
|
|
8589 |
} |
|
|
8590 |
|
|
|
8591 |
/* |
|
|
8592 |
** If zKey is already present in the hash table, return non-zero and do |
|
|
8593 |
** nothing. Otherwise, add an entry with key zKey and payload string zVal to |
|
|
8594 |
** the hash table passed as the second argument. |
|
|
8595 |
*/ |
|
|
8596 |
static int idxHashAdd( |
|
|
8597 |
int *pRc, |
|
|
8598 |
IdxHash *pHash, |
|
|
8599 |
const char *zKey, |
|
|
8600 |
const char *zVal |
|
|
8601 |
){ |
|
|
8602 |
int nKey = STRLEN(zKey); |
|
|
8603 |
int iHash = idxHashString(zKey, nKey); |
|
|
8604 |
int nVal = (zVal ? STRLEN(zVal) : 0); |
|
|
8605 |
IdxHashEntry *pEntry; |
|
|
8606 |
assert( iHash>=0 ); |
|
|
8607 |
for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ |
|
|
8608 |
if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ |
|
|
8609 |
return 1; |
|
|
8610 |
} |
|
|
8611 |
} |
|
|
8612 |
pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1); |
|
|
8613 |
if( pEntry ){ |
|
|
8614 |
pEntry->zKey = (char*)&pEntry[1]; |
|
|
8615 |
memcpy(pEntry->zKey, zKey, nKey); |
|
|
8616 |
if( zVal ){ |
|
|
8617 |
pEntry->zVal = &pEntry->zKey[nKey+1]; |
|
|
8618 |
memcpy(pEntry->zVal, zVal, nVal); |
|
|
8619 |
} |
|
|
8620 |
pEntry->pHashNext = pHash->aHash[iHash]; |
|
|
8621 |
pHash->aHash[iHash] = pEntry; |
|
|
8622 |
|
|
|
8623 |
pEntry->pNext = pHash->pFirst; |
|
|
8624 |
pHash->pFirst = pEntry; |
|
|
8625 |
} |
|
|
8626 |
return 0; |
|
|
8627 |
} |
|
|
8628 |
|
|
|
8629 |
/* |
|
|
8630 |
** If zKey/nKey is present in the hash table, return a pointer to the |
|
|
8631 |
** hash-entry object. |
|
|
8632 |
*/ |
|
|
8633 |
static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){ |
|
|
8634 |
int iHash; |
|
|
8635 |
IdxHashEntry *pEntry; |
|
|
8636 |
if( nKey<0 ) nKey = STRLEN(zKey); |
|
|
8637 |
iHash = idxHashString(zKey, nKey); |
|
|
8638 |
assert( iHash>=0 ); |
|
|
8639 |
for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ |
|
|
8640 |
if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ |
|
|
8641 |
return pEntry; |
|
|
8642 |
} |
|
|
8643 |
} |
|
|
8644 |
return 0; |
|
|
8645 |
} |
|
|
8646 |
|
|
|
8647 |
/* |
|
|
8648 |
** If the hash table contains an entry with a key equal to the string |
|
|
8649 |
** passed as the final two arguments to this function, return a pointer |
|
|
8650 |
** to the payload string. Otherwise, if zKey/nKey is not present in the |
|
|
8651 |
** hash table, return NULL. |
|
|
8652 |
*/ |
|
|
8653 |
static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){ |
|
|
8654 |
IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey); |
|
|
8655 |
if( pEntry ) return pEntry->zVal; |
|
|
8656 |
return 0; |
|
|
8657 |
} |
|
|
8658 |
|
|
|
8659 |
/* |
|
|
8660 |
** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl |
|
|
8661 |
** variable to point to a copy of nul-terminated string zColl. |
|
|
8662 |
*/ |
|
|
8663 |
static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){ |
|
|
8664 |
IdxConstraint *pNew; |
|
|
8665 |
int nColl = STRLEN(zColl); |
|
|
8666 |
|
|
|
8667 |
assert( *pRc==SQLITE_OK ); |
|
|
8668 |
pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1); |
|
|
8669 |
if( pNew ){ |
|
|
8670 |
pNew->zColl = (char*)&pNew[1]; |
|
|
8671 |
memcpy(pNew->zColl, zColl, nColl+1); |
|
|
8672 |
} |
|
|
8673 |
return pNew; |
|
|
8674 |
} |
|
|
8675 |
|
|
|
8676 |
/* |
|
|
8677 |
** An error associated with database handle db has just occurred. Pass |
|
|
8678 |
** the error message to callback function xOut. |
|
|
8679 |
*/ |
|
|
8680 |
static void idxDatabaseError( |
|
|
8681 |
sqlite3 *db, /* Database handle */ |
|
|
8682 |
char **pzErrmsg /* Write error here */ |
|
|
8683 |
){ |
|
|
8684 |
*pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); |
|
|
8685 |
} |
|
|
8686 |
|
|
|
8687 |
/* |
|
|
8688 |
** Prepare an SQL statement. |
|
|
8689 |
*/ |
|
|
8690 |
static int idxPrepareStmt( |
|
|
8691 |
sqlite3 *db, /* Database handle to compile against */ |
|
|
8692 |
sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */ |
|
|
8693 |
char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */ |
|
|
8694 |
const char *zSql /* SQL statement to compile */ |
|
|
8695 |
){ |
|
|
8696 |
int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); |
|
|
8697 |
if( rc!=SQLITE_OK ){ |
|
|
8698 |
*ppStmt = 0; |
|
|
8699 |
idxDatabaseError(db, pzErrmsg); |
|
|
8700 |
} |
|
|
8701 |
return rc; |
|
|
8702 |
} |
|
|
8703 |
|
|
|
8704 |
/* |
|
|
8705 |
** Prepare an SQL statement using the results of a printf() formatting. |
|
|
8706 |
*/ |
|
|
8707 |
static int idxPrintfPrepareStmt( |
|
|
8708 |
sqlite3 *db, /* Database handle to compile against */ |
|
|
8709 |
sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */ |
|
|
8710 |
char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */ |
|
|
8711 |
const char *zFmt, /* printf() format of SQL statement */ |
|
|
8712 |
... /* Trailing printf() arguments */ |
|
|
8713 |
){ |
|
|
8714 |
va_list ap; |
|
|
8715 |
int rc; |
|
|
8716 |
char *zSql; |
|
|
8717 |
va_start(ap, zFmt); |
|
|
8718 |
zSql = sqlite3_vmprintf(zFmt, ap); |
|
|
8719 |
if( zSql==0 ){ |
|
|
8720 |
rc = SQLITE_NOMEM; |
|
|
8721 |
}else{ |
|
|
8722 |
rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql); |
|
|
8723 |
sqlite3_free(zSql); |
|
|
8724 |
} |
|
|
8725 |
va_end(ap); |
|
|
8726 |
return rc; |
|
|
8727 |
} |
|
|
8728 |
|
|
|
8729 |
|
|
|
8730 |
/************************************************************************* |
|
|
8731 |
** Beginning of virtual table implementation. |
|
|
8732 |
*/ |
|
|
8733 |
typedef struct ExpertVtab ExpertVtab; |
|
|
8734 |
struct ExpertVtab { |
|
|
8735 |
sqlite3_vtab base; |
|
|
8736 |
IdxTable *pTab; |
|
|
8737 |
sqlite3expert *pExpert; |
|
|
8738 |
}; |
|
|
8739 |
|
|
|
8740 |
typedef struct ExpertCsr ExpertCsr; |
|
|
8741 |
struct ExpertCsr { |
|
|
8742 |
sqlite3_vtab_cursor base; |
|
|
8743 |
sqlite3_stmt *pData; |
|
|
8744 |
}; |
|
|
8745 |
|
|
|
8746 |
static char *expertDequote(const char *zIn){ |
|
|
8747 |
int n = STRLEN(zIn); |
|
|
8748 |
char *zRet = sqlite3_malloc(n); |
|
|
8749 |
|
|
|
8750 |
assert( zIn[0]=='\'' ); |
|
|
8751 |
assert( zIn[n-1]=='\'' ); |
|
|
8752 |
|
|
|
8753 |
if( zRet ){ |
|
|
8754 |
int iOut = 0; |
|
|
8755 |
int iIn = 0; |
|
|
8756 |
for(iIn=1; iIn<(n-1); iIn++){ |
|
|
8757 |
if( zIn[iIn]=='\'' ){ |
|
|
8758 |
assert( zIn[iIn+1]=='\'' ); |
|
|
8759 |
iIn++; |
|
|
8760 |
} |
|
|
8761 |
zRet[iOut++] = zIn[iIn]; |
|
|
8762 |
} |
|
|
8763 |
zRet[iOut] = '\0'; |
|
|
8764 |
} |
|
|
8765 |
|
|
|
8766 |
return zRet; |
|
|
8767 |
} |
|
|
8768 |
|
|
|
8769 |
/* |
|
|
8770 |
** This function is the implementation of both the xConnect and xCreate |
|
|
8771 |
** methods of the r-tree virtual table. |
|
|
8772 |
** |
|
|
8773 |
** argv[0] -> module name |
|
|
8774 |
** argv[1] -> database name |
|
|
8775 |
** argv[2] -> table name |
|
|
8776 |
** argv[...] -> column names... |
|
|
8777 |
*/ |
|
|
8778 |
static int expertConnect( |
|
|
8779 |
sqlite3 *db, |
|
|
8780 |
void *pAux, |
|
|
8781 |
int argc, const char *const*argv, |
|
|
8782 |
sqlite3_vtab **ppVtab, |
|
|
8783 |
char **pzErr |
|
|
8784 |
){ |
|
|
8785 |
sqlite3expert *pExpert = (sqlite3expert*)pAux; |
|
|
8786 |
ExpertVtab *p = 0; |
|
|
8787 |
int rc; |
|
|
8788 |
|
|
|
8789 |
if( argc!=4 ){ |
|
|
8790 |
*pzErr = sqlite3_mprintf("internal error!"); |
|
|
8791 |
rc = SQLITE_ERROR; |
|
|
8792 |
}else{ |
|
|
8793 |
char *zCreateTable = expertDequote(argv[3]); |
|
|
8794 |
if( zCreateTable ){ |
|
|
8795 |
rc = sqlite3_declare_vtab(db, zCreateTable); |
|
|
8796 |
if( rc==SQLITE_OK ){ |
|
|
8797 |
p = idxMalloc(&rc, sizeof(ExpertVtab)); |
|
|
8798 |
} |
|
|
8799 |
if( rc==SQLITE_OK ){ |
|
|
8800 |
p->pExpert = pExpert; |
|
|
8801 |
p->pTab = pExpert->pTable; |
|
|
8802 |
assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 ); |
|
|
8803 |
} |
|
|
8804 |
sqlite3_free(zCreateTable); |
|
|
8805 |
}else{ |
|
|
8806 |
rc = SQLITE_NOMEM; |
|
|
8807 |
} |
|
|
8808 |
} |
|
|
8809 |
|
|
|
8810 |
*ppVtab = (sqlite3_vtab*)p; |
|
|
8811 |
return rc; |
|
|
8812 |
} |
|
|
8813 |
|
|
|
8814 |
static int expertDisconnect(sqlite3_vtab *pVtab){ |
|
|
8815 |
ExpertVtab *p = (ExpertVtab*)pVtab; |
|
|
8816 |
sqlite3_free(p); |
|
|
8817 |
return SQLITE_OK; |
|
|
8818 |
} |
|
|
8819 |
|
|
|
8820 |
static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){ |
|
|
8821 |
ExpertVtab *p = (ExpertVtab*)pVtab; |
|
|
8822 |
int rc = SQLITE_OK; |
|
|
8823 |
int n = 0; |
|
|
8824 |
IdxScan *pScan; |
|
|
8825 |
const int opmask = |
|
|
8826 |
SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT | |
|
|
8827 |
SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE | |
|
|
8828 |
SQLITE_INDEX_CONSTRAINT_LE; |
|
|
8829 |
|
|
|
8830 |
pScan = idxMalloc(&rc, sizeof(IdxScan)); |
|
|
8831 |
if( pScan ){ |
|
|
8832 |
int i; |
|
|
8833 |
|
|
|
8834 |
/* Link the new scan object into the list */ |
|
|
8835 |
pScan->pTab = p->pTab; |
|
|
8836 |
pScan->pNextScan = p->pExpert->pScan; |
|
|
8837 |
p->pExpert->pScan = pScan; |
|
|
8838 |
|
|
|
8839 |
/* Add the constraints to the IdxScan object */ |
|
|
8840 |
for(i=0; inConstraint; i++){ |
|
|
8841 |
struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i]; |
|
|
8842 |
if( pCons->usable |
|
|
8843 |
&& pCons->iColumn>=0 |
|
|
8844 |
&& p->pTab->aCol[pCons->iColumn].iPk==0 |
|
|
8845 |
&& (pCons->op & opmask) |
|
|
8846 |
){ |
|
|
8847 |
IdxConstraint *pNew; |
|
|
8848 |
const char *zColl = sqlite3_vtab_collation(pIdxInfo, i); |
|
|
8849 |
pNew = idxNewConstraint(&rc, zColl); |
|
|
8850 |
if( pNew ){ |
|
|
8851 |
pNew->iCol = pCons->iColumn; |
|
|
8852 |
if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
|
|
8853 |
pNew->pNext = pScan->pEq; |
|
|
8854 |
pScan->pEq = pNew; |
|
|
8855 |
}else{ |
|
|
8856 |
pNew->bRange = 1; |
|
|
8857 |
pNew->pNext = pScan->pRange; |
|
|
8858 |
pScan->pRange = pNew; |
|
|
8859 |
} |
|
|
8860 |
} |
|
|
8861 |
n++; |
|
|
8862 |
pIdxInfo->aConstraintUsage[i].argvIndex = n; |
|
|
8863 |
} |
|
|
8864 |
} |
|
|
8865 |
|
|
|
8866 |
/* Add the ORDER BY to the IdxScan object */ |
|
|
8867 |
for(i=pIdxInfo->nOrderBy-1; i>=0; i--){ |
|
|
8868 |
int iCol = pIdxInfo->aOrderBy[i].iColumn; |
|
|
8869 |
if( iCol>=0 ){ |
|
|
8870 |
IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl); |
|
|
8871 |
if( pNew ){ |
|
|
8872 |
pNew->iCol = iCol; |
|
|
8873 |
pNew->bDesc = pIdxInfo->aOrderBy[i].desc; |
|
|
8874 |
pNew->pNext = pScan->pOrder; |
|
|
8875 |
pNew->pLink = pScan->pOrder; |
|
|
8876 |
pScan->pOrder = pNew; |
|
|
8877 |
n++; |
|
|
8878 |
} |
|
|
8879 |
} |
|
|
8880 |
} |
|
|
8881 |
} |
|
|
8882 |
|
|
|
8883 |
pIdxInfo->estimatedCost = 1000000.0 / (n+1); |
|
|
8884 |
return rc; |
|
|
8885 |
} |
|
|
8886 |
|
|
|
8887 |
static int expertUpdate( |
|
|
8888 |
sqlite3_vtab *pVtab, |
|
|
8889 |
int nData, |
|
|
8890 |
sqlite3_value **azData, |
|
|
8891 |
sqlite_int64 *pRowid |
|
|
8892 |
){ |
|
|
8893 |
(void)pVtab; |
|
|
8894 |
(void)nData; |
|
|
8895 |
(void)azData; |
|
|
8896 |
(void)pRowid; |
|
|
8897 |
return SQLITE_OK; |
|
|
8898 |
} |
|
|
8899 |
|
|
|
8900 |
/* |
|
|
8901 |
** Virtual table module xOpen method. |
|
|
8902 |
*/ |
|
|
8903 |
static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
|
|
8904 |
int rc = SQLITE_OK; |
|
|
8905 |
ExpertCsr *pCsr; |
|
|
8906 |
(void)pVTab; |
|
|
8907 |
pCsr = idxMalloc(&rc, sizeof(ExpertCsr)); |
|
|
8908 |
*ppCursor = (sqlite3_vtab_cursor*)pCsr; |
|
|
8909 |
return rc; |
|
|
8910 |
} |
|
|
8911 |
|
|
|
8912 |
/* |
|
|
8913 |
** Virtual table module xClose method. |
|
|
8914 |
*/ |
|
|
8915 |
static int expertClose(sqlite3_vtab_cursor *cur){ |
|
|
8916 |
ExpertCsr *pCsr = (ExpertCsr*)cur; |
|
|
8917 |
sqlite3_finalize(pCsr->pData); |
|
|
8918 |
sqlite3_free(pCsr); |
|
|
8919 |
return SQLITE_OK; |
|
|
8920 |
} |
|
|
8921 |
|
|
|
8922 |
/* |
|
|
8923 |
** Virtual table module xEof method. |
|
|
8924 |
** |
|
|
8925 |
** Return non-zero if the cursor does not currently point to a valid |
|
|
8926 |
** record (i.e if the scan has finished), or zero otherwise. |
|
|
8927 |
*/ |
|
|
8928 |
static int expertEof(sqlite3_vtab_cursor *cur){ |
|
|
8929 |
ExpertCsr *pCsr = (ExpertCsr*)cur; |
|
|
8930 |
return pCsr->pData==0; |
|
|
8931 |
} |
|
|
8932 |
|
|
|
8933 |
/* |
|
|
8934 |
** Virtual table module xNext method. |
|
|
8935 |
*/ |
|
|
8936 |
static int expertNext(sqlite3_vtab_cursor *cur){ |
|
|
8937 |
ExpertCsr *pCsr = (ExpertCsr*)cur; |
|
|
8938 |
int rc = SQLITE_OK; |
|
|
8939 |
|
|
|
8940 |
assert( pCsr->pData ); |
|
|
8941 |
rc = sqlite3_step(pCsr->pData); |
|
|
8942 |
if( rc!=SQLITE_ROW ){ |
|
|
8943 |
rc = sqlite3_finalize(pCsr->pData); |
|
|
8944 |
pCsr->pData = 0; |
|
|
8945 |
}else{ |
|
|
8946 |
rc = SQLITE_OK; |
|
|
8947 |
} |
|
|
8948 |
|
|
|
8949 |
return rc; |
|
|
8950 |
} |
|
|
8951 |
|
|
|
8952 |
/* |
|
|
8953 |
** Virtual table module xRowid method. |
|
|
8954 |
*/ |
|
|
8955 |
static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
|
|
8956 |
(void)cur; |
|
|
8957 |
*pRowid = 0; |
|
|
8958 |
return SQLITE_OK; |
|
|
8959 |
} |
|
|
8960 |
|
|
|
8961 |
/* |
|
|
8962 |
** Virtual table module xColumn method. |
|
|
8963 |
*/ |
|
|
8964 |
static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ |
|
|
8965 |
ExpertCsr *pCsr = (ExpertCsr*)cur; |
|
|
8966 |
sqlite3_value *pVal; |
|
|
8967 |
pVal = sqlite3_column_value(pCsr->pData, i); |
|
|
8968 |
if( pVal ){ |
|
|
8969 |
sqlite3_result_value(ctx, pVal); |
|
|
8970 |
} |
|
|
8971 |
return SQLITE_OK; |
|
|
8972 |
} |
|
|
8973 |
|
|
|
8974 |
/* |
|
|
8975 |
** Virtual table module xFilter method. |
|
|
8976 |
*/ |
|
|
8977 |
static int expertFilter( |
|
|
8978 |
sqlite3_vtab_cursor *cur, |
|
|
8979 |
int idxNum, const char *idxStr, |
|
|
8980 |
int argc, sqlite3_value **argv |
|
|
8981 |
){ |
|
|
8982 |
ExpertCsr *pCsr = (ExpertCsr*)cur; |
|
|
8983 |
ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab); |
|
|
8984 |
sqlite3expert *pExpert = pVtab->pExpert; |
|
|
8985 |
int rc; |
|
|
8986 |
|
|
|
8987 |
(void)idxNum; |
|
|
8988 |
(void)idxStr; |
|
|
8989 |
(void)argc; |
|
|
8990 |
(void)argv; |
|
|
8991 |
rc = sqlite3_finalize(pCsr->pData); |
|
|
8992 |
pCsr->pData = 0; |
|
|
8993 |
if( rc==SQLITE_OK ){ |
|
|
8994 |
rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg, |
|
|
8995 |
"SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName |
|
|
8996 |
); |
|
|
8997 |
} |
|
|
8998 |
|
|
|
8999 |
if( rc==SQLITE_OK ){ |
|
|
9000 |
rc = expertNext(cur); |
|
|
9001 |
} |
|
|
9002 |
return rc; |
|
|
9003 |
} |
|
|
9004 |
|
|
|
9005 |
static int idxRegisterVtab(sqlite3expert *p){ |
|
|
9006 |
static sqlite3_module expertModule = { |
|
|
9007 |
2, /* iVersion */ |
|
|
9008 |
expertConnect, /* xCreate - create a table */ |
|
|
9009 |
expertConnect, /* xConnect - connect to an existing table */ |
|
|
9010 |
expertBestIndex, /* xBestIndex - Determine search strategy */ |
|
|
9011 |
expertDisconnect, /* xDisconnect - Disconnect from a table */ |
|
|
9012 |
expertDisconnect, /* xDestroy - Drop a table */ |
|
|
9013 |
expertOpen, /* xOpen - open a cursor */ |
|
|
9014 |
expertClose, /* xClose - close a cursor */ |
|
|
9015 |
expertFilter, /* xFilter - configure scan constraints */ |
|
|
9016 |
expertNext, /* xNext - advance a cursor */ |
|
|
9017 |
expertEof, /* xEof */ |
|
|
9018 |
expertColumn, /* xColumn - read data */ |
|
|
9019 |
expertRowid, /* xRowid - read data */ |
|
|
9020 |
expertUpdate, /* xUpdate - write data */ |
|
|
9021 |
0, /* xBegin - begin transaction */ |
|
|
9022 |
0, /* xSync - sync transaction */ |
|
|
9023 |
0, /* xCommit - commit transaction */ |
|
|
9024 |
0, /* xRollback - rollback transaction */ |
|
|
9025 |
0, /* xFindFunction - function overloading */ |
|
|
9026 |
0, /* xRename - rename the table */ |
|
|
9027 |
0, /* xSavepoint */ |
|
|
9028 |
0, /* xRelease */ |
|
|
9029 |
0, /* xRollbackTo */ |
|
|
9030 |
0, /* xShadowName */ |
|
|
9031 |
}; |
|
|
9032 |
|
|
|
9033 |
return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p); |
|
|
9034 |
} |
|
|
9035 |
/* |
|
|
9036 |
** End of virtual table implementation. |
|
|
9037 |
*************************************************************************/ |
|
|
9038 |
/* |
|
|
9039 |
** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function |
|
|
9040 |
** is called, set it to the return value of sqlite3_finalize() before |
|
|
9041 |
** returning. Otherwise, discard the sqlite3_finalize() return value. |
|
|
9042 |
*/ |
|
|
9043 |
static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){ |
|
|
9044 |
int rc = sqlite3_finalize(pStmt); |
|
|
9045 |
if( *pRc==SQLITE_OK ) *pRc = rc; |
|
|
9046 |
} |
|
|
9047 |
|
|
|
9048 |
/* |
|
|
9049 |
** Attempt to allocate an IdxTable structure corresponding to table zTab |
|
|
9050 |
** in the main database of connection db. If successful, set (*ppOut) to |
|
|
9051 |
** point to the new object and return SQLITE_OK. Otherwise, return an |
|
|
9052 |
** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be |
|
|
9053 |
** set to point to an error string. |
|
|
9054 |
** |
|
|
9055 |
** It is the responsibility of the caller to eventually free either the |
|
|
9056 |
** IdxTable object or error message using sqlite3_free(). |
|
|
9057 |
*/ |
|
|
9058 |
static int idxGetTableInfo( |
|
|
9059 |
sqlite3 *db, /* Database connection to read details from */ |
|
|
9060 |
const char *zTab, /* Table name */ |
|
|
9061 |
IdxTable **ppOut, /* OUT: New object (if successful) */ |
|
|
9062 |
char **pzErrmsg /* OUT: Error message (if not) */ |
|
|
9063 |
){ |
|
|
9064 |
sqlite3_stmt *p1 = 0; |
|
|
9065 |
int nCol = 0; |
|
|
9066 |
int nTab = STRLEN(zTab); |
|
|
9067 |
int nByte = sizeof(IdxTable) + nTab + 1; |
|
|
9068 |
IdxTable *pNew = 0; |
|
|
9069 |
int rc, rc2; |
|
|
9070 |
char *pCsr = 0; |
|
|
9071 |
int nPk = 0; |
|
|
9072 |
|
|
|
9073 |
rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab); |
|
|
9074 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){ |
|
|
9075 |
const char *zCol = (const char*)sqlite3_column_text(p1, 1); |
|
|
9076 |
nByte += 1 + STRLEN(zCol); |
|
|
9077 |
rc = sqlite3_table_column_metadata( |
|
|
9078 |
db, "main", zTab, zCol, 0, &zCol, 0, 0, 0 |
|
|
9079 |
); |
|
|
9080 |
nByte += 1 + STRLEN(zCol); |
|
|
9081 |
nCol++; |
|
|
9082 |
nPk += (sqlite3_column_int(p1, 5)>0); |
|
|
9083 |
} |
|
|
9084 |
rc2 = sqlite3_reset(p1); |
|
|
9085 |
if( rc==SQLITE_OK ) rc = rc2; |
|
|
9086 |
|
|
|
9087 |
nByte += sizeof(IdxColumn) * nCol; |
|
|
9088 |
if( rc==SQLITE_OK ){ |
|
|
9089 |
pNew = idxMalloc(&rc, nByte); |
|
|
9090 |
} |
|
|
9091 |
if( rc==SQLITE_OK ){ |
|
|
9092 |
pNew->aCol = (IdxColumn*)&pNew[1]; |
|
|
9093 |
pNew->nCol = nCol; |
|
|
9094 |
pCsr = (char*)&pNew->aCol[nCol]; |
|
|
9095 |
} |
|
|
9096 |
|
|
|
9097 |
nCol = 0; |
|
|
9098 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){ |
|
|
9099 |
const char *zCol = (const char*)sqlite3_column_text(p1, 1); |
|
|
9100 |
int nCopy = STRLEN(zCol) + 1; |
|
|
9101 |
pNew->aCol[nCol].zName = pCsr; |
|
|
9102 |
pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1); |
|
|
9103 |
memcpy(pCsr, zCol, nCopy); |
|
|
9104 |
pCsr += nCopy; |
|
|
9105 |
|
|
|
9106 |
rc = sqlite3_table_column_metadata( |
|
|
9107 |
db, "main", zTab, zCol, 0, &zCol, 0, 0, 0 |
|
|
9108 |
); |
|
|
9109 |
if( rc==SQLITE_OK ){ |
|
|
9110 |
nCopy = STRLEN(zCol) + 1; |
|
|
9111 |
pNew->aCol[nCol].zColl = pCsr; |
|
|
9112 |
memcpy(pCsr, zCol, nCopy); |
|
|
9113 |
pCsr += nCopy; |
|
|
9114 |
} |
|
|
9115 |
|
|
|
9116 |
nCol++; |
|
|
9117 |
} |
|
|
9118 |
idxFinalize(&rc, p1); |
|
|
9119 |
|
|
|
9120 |
if( rc!=SQLITE_OK ){ |
|
|
9121 |
sqlite3_free(pNew); |
|
|
9122 |
pNew = 0; |
|
|
9123 |
}else{ |
|
|
9124 |
pNew->zName = pCsr; |
|
|
9125 |
memcpy(pNew->zName, zTab, nTab+1); |
|
|
9126 |
} |
|
|
9127 |
|
|
|
9128 |
*ppOut = pNew; |
|
|
9129 |
return rc; |
|
|
9130 |
} |
|
|
9131 |
|
|
|
9132 |
/* |
|
|
9133 |
** This function is a no-op if *pRc is set to anything other than |
|
|
9134 |
** SQLITE_OK when it is called. |
|
|
9135 |
** |
|
|
9136 |
** If *pRc is initially set to SQLITE_OK, then the text specified by |
|
|
9137 |
** the printf() style arguments is appended to zIn and the result returned |
|
|
9138 |
** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on |
|
|
9139 |
** zIn before returning. |
|
|
9140 |
*/ |
|
|
9141 |
static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){ |
|
|
9142 |
va_list ap; |
|
|
9143 |
char *zAppend = 0; |
|
|
9144 |
char *zRet = 0; |
|
|
9145 |
int nIn = zIn ? STRLEN(zIn) : 0; |
|
|
9146 |
int nAppend = 0; |
|
|
9147 |
va_start(ap, zFmt); |
|
|
9148 |
if( *pRc==SQLITE_OK ){ |
|
|
9149 |
zAppend = sqlite3_vmprintf(zFmt, ap); |
|
|
9150 |
if( zAppend ){ |
|
|
9151 |
nAppend = STRLEN(zAppend); |
|
|
9152 |
zRet = (char*)sqlite3_malloc(nIn + nAppend + 1); |
|
|
9153 |
} |
|
|
9154 |
if( zAppend && zRet ){ |
|
|
9155 |
if( nIn ) memcpy(zRet, zIn, nIn); |
|
|
9156 |
memcpy(&zRet[nIn], zAppend, nAppend+1); |
|
|
9157 |
}else{ |
|
|
9158 |
sqlite3_free(zRet); |
|
|
9159 |
zRet = 0; |
|
|
9160 |
*pRc = SQLITE_NOMEM; |
|
|
9161 |
} |
|
|
9162 |
sqlite3_free(zAppend); |
|
|
9163 |
sqlite3_free(zIn); |
|
|
9164 |
} |
|
|
9165 |
va_end(ap); |
|
|
9166 |
return zRet; |
|
|
9167 |
} |
|
|
9168 |
|
|
|
9169 |
/* |
|
|
9170 |
** Return true if zId must be quoted in order to use it as an SQL |
|
|
9171 |
** identifier, or false otherwise. |
|
|
9172 |
*/ |
|
|
9173 |
static int idxIdentifierRequiresQuotes(const char *zId){ |
|
|
9174 |
int i; |
|
|
9175 |
for(i=0; zId[i]; i++){ |
|
|
9176 |
if( !(zId[i]=='_') |
|
|
9177 |
&& !(zId[i]>='0' && zId[i]<='9') |
|
|
9178 |
&& !(zId[i]>='a' && zId[i]<='z') |
|
|
9179 |
&& !(zId[i]>='A' && zId[i]<='Z') |
|
|
9180 |
){ |
|
|
9181 |
return 1; |
|
|
9182 |
} |
|
|
9183 |
} |
|
|
9184 |
return 0; |
|
|
9185 |
} |
|
|
9186 |
|
|
|
9187 |
/* |
|
|
9188 |
** This function appends an index column definition suitable for constraint |
|
|
9189 |
** pCons to the string passed as zIn and returns the result. |
|
|
9190 |
*/ |
|
|
9191 |
static char *idxAppendColDefn( |
|
|
9192 |
int *pRc, /* IN/OUT: Error code */ |
|
|
9193 |
char *zIn, /* Column defn accumulated so far */ |
|
|
9194 |
IdxTable *pTab, /* Table index will be created on */ |
|
|
9195 |
IdxConstraint *pCons |
|
|
9196 |
){ |
|
|
9197 |
char *zRet = zIn; |
|
|
9198 |
IdxColumn *p = &pTab->aCol[pCons->iCol]; |
|
|
9199 |
if( zRet ) zRet = idxAppendText(pRc, zRet, ", "); |
|
|
9200 |
|
|
|
9201 |
if( idxIdentifierRequiresQuotes(p->zName) ){ |
|
|
9202 |
zRet = idxAppendText(pRc, zRet, "%Q", p->zName); |
|
|
9203 |
}else{ |
|
|
9204 |
zRet = idxAppendText(pRc, zRet, "%s", p->zName); |
|
|
9205 |
} |
|
|
9206 |
|
|
|
9207 |
if( sqlite3_stricmp(p->zColl, pCons->zColl) ){ |
|
|
9208 |
if( idxIdentifierRequiresQuotes(pCons->zColl) ){ |
|
|
9209 |
zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl); |
|
|
9210 |
}else{ |
|
|
9211 |
zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl); |
|
|
9212 |
} |
|
|
9213 |
} |
|
|
9214 |
|
|
|
9215 |
if( pCons->bDesc ){ |
|
|
9216 |
zRet = idxAppendText(pRc, zRet, " DESC"); |
|
|
9217 |
} |
|
|
9218 |
return zRet; |
|
|
9219 |
} |
|
|
9220 |
|
|
|
9221 |
/* |
|
|
9222 |
** Search database dbm for an index compatible with the one idxCreateFromCons() |
|
|
9223 |
** would create from arguments pScan, pEq and pTail. If no error occurs and |
|
|
9224 |
** such an index is found, return non-zero. Or, if no such index is found, |
|
|
9225 |
** return zero. |
|
|
9226 |
** |
|
|
9227 |
** If an error occurs, set *pRc to an SQLite error code and return zero. |
|
|
9228 |
*/ |
|
|
9229 |
static int idxFindCompatible( |
|
|
9230 |
int *pRc, /* OUT: Error code */ |
|
|
9231 |
sqlite3* dbm, /* Database to search */ |
|
|
9232 |
IdxScan *pScan, /* Scan for table to search for index on */ |
|
|
9233 |
IdxConstraint *pEq, /* List of == constraints */ |
|
|
9234 |
IdxConstraint *pTail /* List of range constraints */ |
|
|
9235 |
){ |
|
|
9236 |
const char *zTbl = pScan->pTab->zName; |
|
|
9237 |
sqlite3_stmt *pIdxList = 0; |
|
|
9238 |
IdxConstraint *pIter; |
|
|
9239 |
int nEq = 0; /* Number of elements in pEq */ |
|
|
9240 |
int rc; |
|
|
9241 |
|
|
|
9242 |
/* Count the elements in list pEq */ |
|
|
9243 |
for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++; |
|
|
9244 |
|
|
|
9245 |
rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl); |
|
|
9246 |
while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){ |
|
|
9247 |
int bMatch = 1; |
|
|
9248 |
IdxConstraint *pT = pTail; |
|
|
9249 |
sqlite3_stmt *pInfo = 0; |
|
|
9250 |
const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1); |
|
|
9251 |
|
|
|
9252 |
/* Zero the IdxConstraint.bFlag values in the pEq list */ |
|
|
9253 |
for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0; |
|
|
9254 |
|
|
|
9255 |
rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx); |
|
|
9256 |
while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){ |
|
|
9257 |
int iIdx = sqlite3_column_int(pInfo, 0); |
|
|
9258 |
int iCol = sqlite3_column_int(pInfo, 1); |
|
|
9259 |
const char *zColl = (const char*)sqlite3_column_text(pInfo, 4); |
|
|
9260 |
|
|
|
9261 |
if( iIdx |
|
|
9262 |
for(pIter=pEq; pIter; pIter=pIter->pLink){ |
|
|
9263 |
if( pIter->bFlag ) continue; |
|
|
9264 |
if( pIter->iCol!=iCol ) continue; |
|
|
9265 |
if( sqlite3_stricmp(pIter->zColl, zColl) ) continue; |
|
|
9266 |
pIter->bFlag = 1; |
|
|
9267 |
break; |
|
|
9268 |
} |
|
|
9269 |
if( pIter==0 ){ |
|
|
9270 |
bMatch = 0; |
|
|
9271 |
break; |
|
|
9272 |
} |
|
|
9273 |
}else{ |
|
|
9274 |
if( pT ){ |
|
|
9275 |
if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){ |
|
|
9276 |
bMatch = 0; |
|
|
9277 |
break; |
|
|
9278 |
} |
|
|
9279 |
pT = pT->pLink; |
|
|
9280 |
} |
|
|
9281 |
} |
|
|
9282 |
} |
|
|
9283 |
idxFinalize(&rc, pInfo); |
|
|
9284 |
|
|
|
9285 |
if( rc==SQLITE_OK && bMatch ){ |
|
|
9286 |
sqlite3_finalize(pIdxList); |
|
|
9287 |
return 1; |
|
|
9288 |
} |
|
|
9289 |
} |
|
|
9290 |
idxFinalize(&rc, pIdxList); |
|
|
9291 |
|
|
|
9292 |
*pRc = rc; |
|
|
9293 |
return 0; |
|
|
9294 |
} |
|
|
9295 |
|
|
|
9296 |
static int idxCreateFromCons( |
|
|
9297 |
sqlite3expert *p, |
|
|
9298 |
IdxScan *pScan, |
|
|
9299 |
IdxConstraint *pEq, |
|
|
9300 |
IdxConstraint *pTail |
|
|
9301 |
){ |
|
|
9302 |
sqlite3 *dbm = p->dbm; |
|
|
9303 |
int rc = SQLITE_OK; |
|
|
9304 |
if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){ |
|
|
9305 |
IdxTable *pTab = pScan->pTab; |
|
|
9306 |
char *zCols = 0; |
|
|
9307 |
char *zIdx = 0; |
|
|
9308 |
IdxConstraint *pCons; |
|
|
9309 |
unsigned int h = 0; |
|
|
9310 |
const char *zFmt; |
|
|
9311 |
|
|
|
9312 |
for(pCons=pEq; pCons; pCons=pCons->pLink){ |
|
|
9313 |
zCols = idxAppendColDefn(&rc, zCols, pTab, pCons); |
|
|
9314 |
} |
|
|
9315 |
for(pCons=pTail; pCons; pCons=pCons->pLink){ |
|
|
9316 |
zCols = idxAppendColDefn(&rc, zCols, pTab, pCons); |
|
|
9317 |
} |
|
|
9318 |
|
|
|
9319 |
if( rc==SQLITE_OK ){ |
|
|
9320 |
/* Hash the list of columns to come up with a name for the index */ |
|
|
9321 |
const char *zTable = pScan->pTab->zName; |
|
|
9322 |
char *zName; /* Index name */ |
|
|
9323 |
int i; |
|
|
9324 |
for(i=0; zCols[i]; i++){ |
|
|
9325 |
h += ((h<<3) + zCols[i]); |
|
|
9326 |
} |
|
|
9327 |
zName = sqlite3_mprintf("%s_idx_%08x", zTable, h); |
|
|
9328 |
if( zName==0 ){ |
|
|
9329 |
rc = SQLITE_NOMEM; |
|
|
9330 |
}else{ |
|
|
9331 |
if( idxIdentifierRequiresQuotes(zTable) ){ |
|
|
9332 |
zFmt = "CREATE INDEX '%q' ON %Q(%s)"; |
|
|
9333 |
}else{ |
|
|
9334 |
zFmt = "CREATE INDEX %s ON %s(%s)"; |
|
|
9335 |
} |
|
|
9336 |
zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols); |
|
|
9337 |
if( !zIdx ){ |
|
|
9338 |
rc = SQLITE_NOMEM; |
|
|
9339 |
}else{ |
|
|
9340 |
rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg); |
|
|
9341 |
idxHashAdd(&rc, &p->hIdx, zName, zIdx); |
|
|
9342 |
} |
|
|
9343 |
sqlite3_free(zName); |
|
|
9344 |
sqlite3_free(zIdx); |
|
|
9345 |
} |
|
|
9346 |
} |
|
|
9347 |
|
|
|
9348 |
sqlite3_free(zCols); |
|
|
9349 |
} |
|
|
9350 |
return rc; |
|
|
9351 |
} |
|
|
9352 |
|
|
|
9353 |
/* |
|
|
9354 |
** Return true if list pList (linked by IdxConstraint.pLink) contains |
|
|
9355 |
** a constraint compatible with *p. Otherwise return false. |
|
|
9356 |
*/ |
|
|
9357 |
static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){ |
|
|
9358 |
IdxConstraint *pCmp; |
|
|
9359 |
for(pCmp=pList; pCmp; pCmp=pCmp->pLink){ |
|
|
9360 |
if( p->iCol==pCmp->iCol ) return 1; |
|
|
9361 |
} |
|
|
9362 |
return 0; |
|
|
9363 |
} |
|
|
9364 |
|
|
|
9365 |
static int idxCreateFromWhere( |
|
|
9366 |
sqlite3expert *p, |
|
|
9367 |
IdxScan *pScan, /* Create indexes for this scan */ |
|
|
9368 |
IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */ |
|
|
9369 |
){ |
|
|
9370 |
IdxConstraint *p1 = 0; |
|
|
9371 |
IdxConstraint *pCon; |
|
|
9372 |
int rc; |
|
|
9373 |
|
|
|
9374 |
/* Gather up all the == constraints. */ |
|
|
9375 |
for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){ |
|
|
9376 |
if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){ |
|
|
9377 |
pCon->pLink = p1; |
|
|
9378 |
p1 = pCon; |
|
|
9379 |
} |
|
|
9380 |
} |
|
|
9381 |
|
|
|
9382 |
/* Create an index using the == constraints collected above. And the |
|
|
9383 |
** range constraint/ORDER BY terms passed in by the caller, if any. */ |
|
|
9384 |
rc = idxCreateFromCons(p, pScan, p1, pTail); |
|
|
9385 |
|
|
|
9386 |
/* If no range/ORDER BY passed by the caller, create a version of the |
|
|
9387 |
** index for each range constraint. */ |
|
|
9388 |
if( pTail==0 ){ |
|
|
9389 |
for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){ |
|
|
9390 |
assert( pCon->pLink==0 ); |
|
|
9391 |
if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){ |
|
|
9392 |
rc = idxCreateFromCons(p, pScan, p1, pCon); |
|
|
9393 |
} |
|
|
9394 |
} |
|
|
9395 |
} |
|
|
9396 |
|
|
|
9397 |
return rc; |
|
|
9398 |
} |
|
|
9399 |
|
|
|
9400 |
/* |
|
|
9401 |
** Create candidate indexes in database [dbm] based on the data in |
|
|
9402 |
** linked-list pScan. |
|
|
9403 |
*/ |
|
|
9404 |
static int idxCreateCandidates(sqlite3expert *p){ |
|
|
9405 |
int rc = SQLITE_OK; |
|
|
9406 |
IdxScan *pIter; |
|
|
9407 |
|
|
|
9408 |
for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){ |
|
|
9409 |
rc = idxCreateFromWhere(p, pIter, 0); |
|
|
9410 |
if( rc==SQLITE_OK && pIter->pOrder ){ |
|
|
9411 |
rc = idxCreateFromWhere(p, pIter, pIter->pOrder); |
|
|
9412 |
} |
|
|
9413 |
} |
|
|
9414 |
|
|
|
9415 |
return rc; |
|
|
9416 |
} |
|
|
9417 |
|
|
|
9418 |
/* |
|
|
9419 |
** Free all elements of the linked list starting at pConstraint. |
|
|
9420 |
*/ |
|
|
9421 |
static void idxConstraintFree(IdxConstraint *pConstraint){ |
|
|
9422 |
IdxConstraint *pNext; |
|
|
9423 |
IdxConstraint *p; |
|
|
9424 |
|
|
|
9425 |
for(p=pConstraint; p; p=pNext){ |
|
|
9426 |
pNext = p->pNext; |
|
|
9427 |
sqlite3_free(p); |
|
|
9428 |
} |
|
|
9429 |
} |
|
|
9430 |
|
|
|
9431 |
/* |
|
|
9432 |
** Free all elements of the linked list starting from pScan up until pLast |
|
|
9433 |
** (pLast is not freed). |
|
|
9434 |
*/ |
|
|
9435 |
static void idxScanFree(IdxScan *pScan, IdxScan *pLast){ |
|
|
9436 |
IdxScan *p; |
|
|
9437 |
IdxScan *pNext; |
|
|
9438 |
for(p=pScan; p!=pLast; p=pNext){ |
|
|
9439 |
pNext = p->pNextScan; |
|
|
9440 |
idxConstraintFree(p->pOrder); |
|
|
9441 |
idxConstraintFree(p->pEq); |
|
|
9442 |
idxConstraintFree(p->pRange); |
|
|
9443 |
sqlite3_free(p); |
|
|
9444 |
} |
|
|
9445 |
} |
|
|
9446 |
|
|
|
9447 |
/* |
|
|
9448 |
** Free all elements of the linked list starting from pStatement up |
|
|
9449 |
** until pLast (pLast is not freed). |
|
|
9450 |
*/ |
|
|
9451 |
static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){ |
|
|
9452 |
IdxStatement *p; |
|
|
9453 |
IdxStatement *pNext; |
|
|
9454 |
for(p=pStatement; p!=pLast; p=pNext){ |
|
|
9455 |
pNext = p->pNext; |
|
|
9456 |
sqlite3_free(p->zEQP); |
|
|
9457 |
sqlite3_free(p->zIdx); |
|
|
9458 |
sqlite3_free(p); |
|
|
9459 |
} |
|
|
9460 |
} |
|
|
9461 |
|
|
|
9462 |
/* |
|
|
9463 |
** Free the linked list of IdxTable objects starting at pTab. |
|
|
9464 |
*/ |
|
|
9465 |
static void idxTableFree(IdxTable *pTab){ |
|
|
9466 |
IdxTable *pIter; |
|
|
9467 |
IdxTable *pNext; |
|
|
9468 |
for(pIter=pTab; pIter; pIter=pNext){ |
|
|
9469 |
pNext = pIter->pNext; |
|
|
9470 |
sqlite3_free(pIter); |
|
|
9471 |
} |
|
|
9472 |
} |
|
|
9473 |
|
|
|
9474 |
/* |
|
|
9475 |
** Free the linked list of IdxWrite objects starting at pTab. |
|
|
9476 |
*/ |
|
|
9477 |
static void idxWriteFree(IdxWrite *pTab){ |
|
|
9478 |
IdxWrite *pIter; |
|
|
9479 |
IdxWrite *pNext; |
|
|
9480 |
for(pIter=pTab; pIter; pIter=pNext){ |
|
|
9481 |
pNext = pIter->pNext; |
|
|
9482 |
sqlite3_free(pIter); |
|
|
9483 |
} |
|
|
9484 |
} |
|
|
9485 |
|
|
|
9486 |
|
|
|
9487 |
|
|
|
9488 |
/* |
|
|
9489 |
** This function is called after candidate indexes have been created. It |
|
|
9490 |
** runs all the queries to see which indexes they prefer, and populates |
|
|
9491 |
** IdxStatement.zIdx and IdxStatement.zEQP with the results. |
|
|
9492 |
*/ |
|
|
9493 |
int idxFindIndexes( |
|
|
9494 |
sqlite3expert *p, |
|
|
9495 |
char **pzErr /* OUT: Error message (sqlite3_malloc) */ |
|
|
9496 |
){ |
|
|
9497 |
IdxStatement *pStmt; |
|
|
9498 |
sqlite3 *dbm = p->dbm; |
|
|
9499 |
int rc = SQLITE_OK; |
|
|
9500 |
|
|
|
9501 |
IdxHash hIdx; |
|
|
9502 |
idxHashInit(&hIdx); |
|
|
9503 |
|
|
|
9504 |
for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){ |
|
|
9505 |
IdxHashEntry *pEntry; |
|
|
9506 |
sqlite3_stmt *pExplain = 0; |
|
|
9507 |
idxHashClear(&hIdx); |
|
|
9508 |
rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr, |
|
|
9509 |
"EXPLAIN QUERY PLAN %s", pStmt->zSql |
|
|
9510 |
); |
|
|
9511 |
while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){ |
|
|
9512 |
/* int iId = sqlite3_column_int(pExplain, 0); */ |
|
|
9513 |
/* int iParent = sqlite3_column_int(pExplain, 1); */ |
|
|
9514 |
/* int iNotUsed = sqlite3_column_int(pExplain, 2); */ |
|
|
9515 |
const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3); |
|
|
9516 |
int nDetail; |
|
|
9517 |
int i; |
|
|
9518 |
|
|
|
9519 |
if( !zDetail ) continue; |
|
|
9520 |
nDetail = STRLEN(zDetail); |
|
|
9521 |
|
|
|
9522 |
for(i=0; i |
|
|
9523 |
const char *zIdx = 0; |
|
|
9524 |
if( i+13 |
|
|
9525 |
zIdx = &zDetail[i+13]; |
|
|
9526 |
}else if( i+22 |
|
|
9527 |
&& memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 |
|
|
9528 |
){ |
|
|
9529 |
zIdx = &zDetail[i+22]; |
|
|
9530 |
} |
|
|
9531 |
if( zIdx ){ |
|
|
9532 |
const char *zSql; |
|
|
9533 |
int nIdx = 0; |
|
|
9534 |
while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){ |
|
|
9535 |
nIdx++; |
|
|
9536 |
} |
|
|
9537 |
zSql = idxHashSearch(&p->hIdx, zIdx, nIdx); |
|
|
9538 |
if( zSql ){ |
|
|
9539 |
idxHashAdd(&rc, &hIdx, zSql, 0); |
|
|
9540 |
if( rc ) goto find_indexes_out; |
|
|
9541 |
} |
|
|
9542 |
break; |
|
|
9543 |
} |
|
|
9544 |
} |
|
|
9545 |
|
|
|
9546 |
if( zDetail[0]!='-' ){ |
|
|
9547 |
pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail); |
|
|
9548 |
} |
|
|
9549 |
} |
|
|
9550 |
|
|
|
9551 |
for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ |
|
|
9552 |
pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey); |
|
|
9553 |
} |
|
|
9554 |
|
|
|
9555 |
idxFinalize(&rc, pExplain); |
|
|
9556 |
} |
|
|
9557 |
|
|
|
9558 |
find_indexes_out: |
|
|
9559 |
idxHashClear(&hIdx); |
|
|
9560 |
return rc; |
|
|
9561 |
} |
|
|
9562 |
|
|
|
9563 |
static int idxAuthCallback( |
|
|
9564 |
void *pCtx, |
|
|
9565 |
int eOp, |
|
|
9566 |
const char *z3, |
|
|
9567 |
const char *z4, |
|
|
9568 |
const char *zDb, |
|
|
9569 |
const char *zTrigger |
|
|
9570 |
){ |
|
|
9571 |
int rc = SQLITE_OK; |
|
|
9572 |
(void)z4; |
|
|
9573 |
(void)zTrigger; |
|
|
9574 |
if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){ |
|
|
9575 |
if( sqlite3_stricmp(zDb, "main")==0 ){ |
|
|
9576 |
sqlite3expert *p = (sqlite3expert*)pCtx; |
|
|
9577 |
IdxTable *pTab; |
|
|
9578 |
for(pTab=p->pTable; pTab; pTab=pTab->pNext){ |
|
|
9579 |
if( 0==sqlite3_stricmp(z3, pTab->zName) ) break; |
|
|
9580 |
} |
|
|
9581 |
if( pTab ){ |
|
|
9582 |
IdxWrite *pWrite; |
|
|
9583 |
for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){ |
|
|
9584 |
if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break; |
|
|
9585 |
} |
|
|
9586 |
if( pWrite==0 ){ |
|
|
9587 |
pWrite = idxMalloc(&rc, sizeof(IdxWrite)); |
|
|
9588 |
if( rc==SQLITE_OK ){ |
|
|
9589 |
pWrite->pTab = pTab; |
|
|
9590 |
pWrite->eOp = eOp; |
|
|
9591 |
pWrite->pNext = p->pWrite; |
|
|
9592 |
p->pWrite = pWrite; |
|
|
9593 |
} |
|
|
9594 |
} |
|
|
9595 |
} |
|
|
9596 |
} |
|
|
9597 |
} |
|
|
9598 |
return rc; |
|
|
9599 |
} |
|
|
9600 |
|
|
|
9601 |
static int idxProcessOneTrigger( |
|
|
9602 |
sqlite3expert *p, |
|
|
9603 |
IdxWrite *pWrite, |
|
|
9604 |
char **pzErr |
|
|
9605 |
){ |
|
|
9606 |
static const char *zInt = UNIQUE_TABLE_NAME; |
|
|
9607 |
static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME; |
|
|
9608 |
IdxTable *pTab = pWrite->pTab; |
|
|
9609 |
const char *zTab = pTab->zName; |
|
|
9610 |
const char *zSql = |
|
|
9611 |
"SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema " |
|
|
9612 |
"WHERE tbl_name = %Q AND type IN ('table', 'trigger') " |
|
|
9613 |
"ORDER BY type;"; |
|
|
9614 |
sqlite3_stmt *pSelect = 0; |
|
|
9615 |
int rc = SQLITE_OK; |
|
|
9616 |
char *zWrite = 0; |
|
|
9617 |
|
|
|
9618 |
/* Create the table and its triggers in the temp schema */ |
|
|
9619 |
rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab); |
|
|
9620 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){ |
|
|
9621 |
const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0); |
|
|
9622 |
rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr); |
|
|
9623 |
} |
|
|
9624 |
idxFinalize(&rc, pSelect); |
|
|
9625 |
|
|
|
9626 |
/* Rename the table in the temp schema to zInt */ |
|
|
9627 |
if( rc==SQLITE_OK ){ |
|
|
9628 |
char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt); |
|
|
9629 |
if( z==0 ){ |
|
|
9630 |
rc = SQLITE_NOMEM; |
|
|
9631 |
}else{ |
|
|
9632 |
rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr); |
|
|
9633 |
sqlite3_free(z); |
|
|
9634 |
} |
|
|
9635 |
} |
|
|
9636 |
|
|
|
9637 |
switch( pWrite->eOp ){ |
|
|
9638 |
case SQLITE_INSERT: { |
|
|
9639 |
int i; |
|
|
9640 |
zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt); |
|
|
9641 |
for(i=0; inCol; i++){ |
|
|
9642 |
zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", "); |
|
|
9643 |
} |
|
|
9644 |
zWrite = idxAppendText(&rc, zWrite, ")"); |
|
|
9645 |
break; |
|
|
9646 |
} |
|
|
9647 |
case SQLITE_UPDATE: { |
|
|
9648 |
int i; |
|
|
9649 |
zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt); |
|
|
9650 |
for(i=0; inCol; i++){ |
|
|
9651 |
zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ", |
|
|
9652 |
pTab->aCol[i].zName |
|
|
9653 |
); |
|
|
9654 |
} |
|
|
9655 |
break; |
|
|
9656 |
} |
|
|
9657 |
default: { |
|
|
9658 |
assert( pWrite->eOp==SQLITE_DELETE ); |
|
|
9659 |
if( rc==SQLITE_OK ){ |
|
|
9660 |
zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt); |
|
|
9661 |
if( zWrite==0 ) rc = SQLITE_NOMEM; |
|
|
9662 |
} |
|
|
9663 |
} |
|
|
9664 |
} |
|
|
9665 |
|
|
|
9666 |
if( rc==SQLITE_OK ){ |
|
|
9667 |
sqlite3_stmt *pX = 0; |
|
|
9668 |
rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0); |
|
|
9669 |
idxFinalize(&rc, pX); |
|
|
9670 |
if( rc!=SQLITE_OK ){ |
|
|
9671 |
idxDatabaseError(p->dbv, pzErr); |
|
|
9672 |
} |
|
|
9673 |
} |
|
|
9674 |
sqlite3_free(zWrite); |
|
|
9675 |
|
|
|
9676 |
if( rc==SQLITE_OK ){ |
|
|
9677 |
rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr); |
|
|
9678 |
} |
|
|
9679 |
|
|
|
9680 |
return rc; |
|
|
9681 |
} |
|
|
9682 |
|
|
|
9683 |
static int idxProcessTriggers(sqlite3expert *p, char **pzErr){ |
|
|
9684 |
int rc = SQLITE_OK; |
|
|
9685 |
IdxWrite *pEnd = 0; |
|
|
9686 |
IdxWrite *pFirst = p->pWrite; |
|
|
9687 |
|
|
|
9688 |
while( rc==SQLITE_OK && pFirst!=pEnd ){ |
|
|
9689 |
IdxWrite *pIter; |
|
|
9690 |
for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){ |
|
|
9691 |
rc = idxProcessOneTrigger(p, pIter, pzErr); |
|
|
9692 |
} |
|
|
9693 |
pEnd = pFirst; |
|
|
9694 |
pFirst = p->pWrite; |
|
|
9695 |
} |
|
|
9696 |
|
|
|
9697 |
return rc; |
|
|
9698 |
} |
|
|
9699 |
|
|
|
9700 |
|
|
|
9701 |
static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){ |
|
|
9702 |
int rc = idxRegisterVtab(p); |
|
|
9703 |
sqlite3_stmt *pSchema = 0; |
|
|
9704 |
|
|
|
9705 |
/* For each table in the main db schema: |
|
|
9706 |
** |
|
|
9707 |
** 1) Add an entry to the p->pTable list, and |
|
|
9708 |
** 2) Create the equivalent virtual table in dbv. |
|
|
9709 |
*/ |
|
|
9710 |
rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg, |
|
|
9711 |
"SELECT type, name, sql, 1 FROM sqlite_schema " |
|
|
9712 |
"WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' " |
|
|
9713 |
" UNION ALL " |
|
|
9714 |
"SELECT type, name, sql, 2 FROM sqlite_schema " |
|
|
9715 |
"WHERE type = 'trigger'" |
|
|
9716 |
" AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') " |
|
|
9717 |
"ORDER BY 4, 1" |
|
|
9718 |
); |
|
|
9719 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){ |
|
|
9720 |
const char *zType = (const char*)sqlite3_column_text(pSchema, 0); |
|
|
9721 |
const char *zName = (const char*)sqlite3_column_text(pSchema, 1); |
|
|
9722 |
const char *zSql = (const char*)sqlite3_column_text(pSchema, 2); |
|
|
9723 |
|
|
|
9724 |
if( zType[0]=='v' || zType[1]=='r' ){ |
|
|
9725 |
rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg); |
|
|
9726 |
}else{ |
|
|
9727 |
IdxTable *pTab; |
|
|
9728 |
rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg); |
|
|
9729 |
if( rc==SQLITE_OK ){ |
|
|
9730 |
int i; |
|
|
9731 |
char *zInner = 0; |
|
|
9732 |
char *zOuter = 0; |
|
|
9733 |
pTab->pNext = p->pTable; |
|
|
9734 |
p->pTable = pTab; |
|
|
9735 |
|
|
|
9736 |
/* The statement the vtab will pass to sqlite3_declare_vtab() */ |
|
|
9737 |
zInner = idxAppendText(&rc, 0, "CREATE TABLE x("); |
|
|
9738 |
for(i=0; inCol; i++){ |
|
|
9739 |
zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s", |
|
|
9740 |
(i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl |
|
|
9741 |
); |
|
|
9742 |
} |
|
|
9743 |
zInner = idxAppendText(&rc, zInner, ")"); |
|
|
9744 |
|
|
|
9745 |
/* The CVT statement to create the vtab */ |
|
|
9746 |
zOuter = idxAppendText(&rc, 0, |
|
|
9747 |
"CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner |
|
|
9748 |
); |
|
|
9749 |
if( rc==SQLITE_OK ){ |
|
|
9750 |
rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg); |
|
|
9751 |
} |
|
|
9752 |
sqlite3_free(zInner); |
|
|
9753 |
sqlite3_free(zOuter); |
|
|
9754 |
} |
|
|
9755 |
} |
|
|
9756 |
} |
|
|
9757 |
idxFinalize(&rc, pSchema); |
|
|
9758 |
return rc; |
|
|
9759 |
} |
|
|
9760 |
|
|
|
9761 |
struct IdxSampleCtx { |
|
|
9762 |
int iTarget; |
|
|
9763 |
double target; /* Target nRet/nRow value */ |
|
|
9764 |
double nRow; /* Number of rows seen */ |
|
|
9765 |
double nRet; /* Number of rows returned */ |
|
|
9766 |
}; |
|
|
9767 |
|
|
|
9768 |
static void idxSampleFunc( |
|
|
9769 |
sqlite3_context *pCtx, |
|
|
9770 |
int argc, |
|
|
9771 |
sqlite3_value **argv |
|
|
9772 |
){ |
|
|
9773 |
struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx); |
|
|
9774 |
int bRet; |
|
|
9775 |
|
|
|
9776 |
(void)argv; |
|
|
9777 |
assert( argc==0 ); |
|
|
9778 |
if( p->nRow==0.0 ){ |
|
|
9779 |
bRet = 1; |
|
|
9780 |
}else{ |
|
|
9781 |
bRet = (p->nRet / p->nRow) <= p->target; |
|
|
9782 |
if( bRet==0 ){ |
|
|
9783 |
unsigned short rnd; |
|
|
9784 |
sqlite3_randomness(2, (void*)&rnd); |
|
|
9785 |
bRet = ((int)rnd % 100) <= p->iTarget; |
|
|
9786 |
} |
|
|
9787 |
} |
|
|
9788 |
|
|
|
9789 |
sqlite3_result_int(pCtx, bRet); |
|
|
9790 |
p->nRow += 1.0; |
|
|
9791 |
p->nRet += (double)bRet; |
|
|
9792 |
} |
|
|
9793 |
|
|
|
9794 |
struct IdxRemCtx { |
|
|
9795 |
int nSlot; |
|
|
9796 |
struct IdxRemSlot { |
|
|
9797 |
int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */ |
|
|
9798 |
i64 iVal; /* SQLITE_INTEGER value */ |
|
|
9799 |
double rVal; /* SQLITE_FLOAT value */ |
|
|
9800 |
int nByte; /* Bytes of space allocated at z */ |
|
|
9801 |
int n; /* Size of buffer z */ |
|
|
9802 |
char *z; /* SQLITE_TEXT/BLOB value */ |
|
|
9803 |
} aSlot[1]; |
|
|
9804 |
}; |
|
|
9805 |
|
|
|
9806 |
/* |
|
|
9807 |
** Implementation of scalar function rem(). |
|
|
9808 |
*/ |
|
|
9809 |
static void idxRemFunc( |
|
|
9810 |
sqlite3_context *pCtx, |
|
|
9811 |
int argc, |
|
|
9812 |
sqlite3_value **argv |
|
|
9813 |
){ |
|
|
9814 |
struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx); |
|
|
9815 |
struct IdxRemSlot *pSlot; |
|
|
9816 |
int iSlot; |
|
|
9817 |
assert( argc==2 ); |
|
|
9818 |
|
|
|
9819 |
iSlot = sqlite3_value_int(argv[0]); |
|
|
9820 |
assert( iSlot<=p->nSlot ); |
|
|
9821 |
pSlot = &p->aSlot[iSlot]; |
|
|
9822 |
|
|
|
9823 |
switch( pSlot->eType ){ |
|
|
9824 |
case SQLITE_NULL: |
|
|
9825 |
/* no-op */ |
|
|
9826 |
break; |
|
|
9827 |
|
|
|
9828 |
case SQLITE_INTEGER: |
|
|
9829 |
sqlite3_result_int64(pCtx, pSlot->iVal); |
|
|
9830 |
break; |
|
|
9831 |
|
|
|
9832 |
case SQLITE_FLOAT: |
|
|
9833 |
sqlite3_result_double(pCtx, pSlot->rVal); |
|
|
9834 |
break; |
|
|
9835 |
|
|
|
9836 |
case SQLITE_BLOB: |
|
|
9837 |
sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT); |
|
|
9838 |
break; |
|
|
9839 |
|
|
|
9840 |
case SQLITE_TEXT: |
|
|
9841 |
sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT); |
|
|
9842 |
break; |
|
|
9843 |
} |
|
|
9844 |
|
|
|
9845 |
pSlot->eType = sqlite3_value_type(argv[1]); |
|
|
9846 |
switch( pSlot->eType ){ |
|
|
9847 |
case SQLITE_NULL: |
|
|
9848 |
/* no-op */ |
|
|
9849 |
break; |
|
|
9850 |
|
|
|
9851 |
case SQLITE_INTEGER: |
|
|
9852 |
pSlot->iVal = sqlite3_value_int64(argv[1]); |
|
|
9853 |
break; |
|
|
9854 |
|
|
|
9855 |
case SQLITE_FLOAT: |
|
|
9856 |
pSlot->rVal = sqlite3_value_double(argv[1]); |
|
|
9857 |
break; |
|
|
9858 |
|
|
|
9859 |
case SQLITE_BLOB: |
|
|
9860 |
case SQLITE_TEXT: { |
|
|
9861 |
int nByte = sqlite3_value_bytes(argv[1]); |
|
|
9862 |
if( nByte>pSlot->nByte ){ |
|
|
9863 |
char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2); |
|
|
9864 |
if( zNew==0 ){ |
|
|
9865 |
sqlite3_result_error_nomem(pCtx); |
|
|
9866 |
return; |
|
|
9867 |
} |
|
|
9868 |
pSlot->nByte = nByte*2; |
|
|
9869 |
pSlot->z = zNew; |
|
|
9870 |
} |
|
|
9871 |
pSlot->n = nByte; |
|
|
9872 |
if( pSlot->eType==SQLITE_BLOB ){ |
|
|
9873 |
memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte); |
|
|
9874 |
}else{ |
|
|
9875 |
memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte); |
|
|
9876 |
} |
|
|
9877 |
break; |
|
|
9878 |
} |
|
|
9879 |
} |
|
|
9880 |
} |
|
|
9881 |
|
|
|
9882 |
static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){ |
|
|
9883 |
int rc = SQLITE_OK; |
|
|
9884 |
const char *zMax = |
|
|
9885 |
"SELECT max(i.seqno) FROM " |
|
|
9886 |
" sqlite_schema AS s, " |
|
|
9887 |
" pragma_index_list(s.name) AS l, " |
|
|
9888 |
" pragma_index_info(l.name) AS i " |
|
|
9889 |
"WHERE s.type = 'table'"; |
|
|
9890 |
sqlite3_stmt *pMax = 0; |
|
|
9891 |
|
|
|
9892 |
*pnMax = 0; |
|
|
9893 |
rc = idxPrepareStmt(db, &pMax, pzErr, zMax); |
|
|
9894 |
if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){ |
|
|
9895 |
*pnMax = sqlite3_column_int(pMax, 0) + 1; |
|
|
9896 |
} |
|
|
9897 |
idxFinalize(&rc, pMax); |
|
|
9898 |
|
|
|
9899 |
return rc; |
|
|
9900 |
} |
|
|
9901 |
|
|
|
9902 |
static int idxPopulateOneStat1( |
|
|
9903 |
sqlite3expert *p, |
|
|
9904 |
sqlite3_stmt *pIndexXInfo, |
|
|
9905 |
sqlite3_stmt *pWriteStat, |
|
|
9906 |
const char *zTab, |
|
|
9907 |
const char *zIdx, |
|
|
9908 |
char **pzErr |
|
|
9909 |
){ |
|
|
9910 |
char *zCols = 0; |
|
|
9911 |
char *zOrder = 0; |
|
|
9912 |
char *zQuery = 0; |
|
|
9913 |
int nCol = 0; |
|
|
9914 |
int i; |
|
|
9915 |
sqlite3_stmt *pQuery = 0; |
|
|
9916 |
int *aStat = 0; |
|
|
9917 |
int rc = SQLITE_OK; |
|
|
9918 |
|
|
|
9919 |
assert( p->iSample>0 ); |
|
|
9920 |
|
|
|
9921 |
/* Formulate the query text */ |
|
|
9922 |
sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC); |
|
|
9923 |
while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){ |
|
|
9924 |
const char *zComma = zCols==0 ? "" : ", "; |
|
|
9925 |
const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0); |
|
|
9926 |
const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1); |
|
|
9927 |
zCols = idxAppendText(&rc, zCols, |
|
|
9928 |
"%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl |
|
|
9929 |
); |
|
|
9930 |
zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol); |
|
|
9931 |
} |
|
|
9932 |
sqlite3_reset(pIndexXInfo); |
|
|
9933 |
if( rc==SQLITE_OK ){ |
|
|
9934 |
if( p->iSample==100 ){ |
|
|
9935 |
zQuery = sqlite3_mprintf( |
|
|
9936 |
"SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder |
|
|
9937 |
); |
|
|
9938 |
}else{ |
|
|
9939 |
zQuery = sqlite3_mprintf( |
|
|
9940 |
"SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder |
|
|
9941 |
); |
|
|
9942 |
} |
|
|
9943 |
} |
|
|
9944 |
sqlite3_free(zCols); |
|
|
9945 |
sqlite3_free(zOrder); |
|
|
9946 |
|
|
|
9947 |
/* Formulate the query text */ |
|
|
9948 |
if( rc==SQLITE_OK ){ |
|
|
9949 |
sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); |
|
|
9950 |
rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery); |
|
|
9951 |
} |
|
|
9952 |
sqlite3_free(zQuery); |
|
|
9953 |
|
|
|
9954 |
if( rc==SQLITE_OK ){ |
|
|
9955 |
aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1)); |
|
|
9956 |
} |
|
|
9957 |
if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){ |
|
|
9958 |
IdxHashEntry *pEntry; |
|
|
9959 |
char *zStat = 0; |
|
|
9960 |
for(i=0; i<=nCol; i++) aStat[i] = 1; |
|
|
9961 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){ |
|
|
9962 |
aStat[0]++; |
|
|
9963 |
for(i=0; i |
|
|
9964 |
if( sqlite3_column_int(pQuery, i)==0 ) break; |
|
|
9965 |
} |
|
|
9966 |
for(/*no-op*/; i |
|
|
9967 |
aStat[i+1]++; |
|
|
9968 |
} |
|
|
9969 |
} |
|
|
9970 |
|
|
|
9971 |
if( rc==SQLITE_OK ){ |
|
|
9972 |
int s0 = aStat[0]; |
|
|
9973 |
zStat = sqlite3_mprintf("%d", s0); |
|
|
9974 |
if( zStat==0 ) rc = SQLITE_NOMEM; |
|
|
9975 |
for(i=1; rc==SQLITE_OK && i<=nCol; i++){ |
|
|
9976 |
zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]); |
|
|
9977 |
} |
|
|
9978 |
} |
|
|
9979 |
|
|
|
9980 |
if( rc==SQLITE_OK ){ |
|
|
9981 |
sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC); |
|
|
9982 |
sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC); |
|
|
9983 |
sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC); |
|
|
9984 |
sqlite3_step(pWriteStat); |
|
|
9985 |
rc = sqlite3_reset(pWriteStat); |
|
|
9986 |
} |
|
|
9987 |
|
|
|
9988 |
pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx)); |
|
|
9989 |
if( pEntry ){ |
|
|
9990 |
assert( pEntry->zVal2==0 ); |
|
|
9991 |
pEntry->zVal2 = zStat; |
|
|
9992 |
}else{ |
|
|
9993 |
sqlite3_free(zStat); |
|
|
9994 |
} |
|
|
9995 |
} |
|
|
9996 |
sqlite3_free(aStat); |
|
|
9997 |
idxFinalize(&rc, pQuery); |
|
|
9998 |
|
|
|
9999 |
return rc; |
|
|
10000 |
} |
|
|
10001 |
|
|
|
10002 |
static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){ |
|
|
10003 |
int rc; |
|
|
10004 |
char *zSql; |
|
|
10005 |
|
|
|
10006 |
rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); |
|
|
10007 |
if( rc!=SQLITE_OK ) return rc; |
|
|
10008 |
|
|
|
10009 |
zSql = sqlite3_mprintf( |
|
|
10010 |
"CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab |
|
|
10011 |
); |
|
|
10012 |
if( zSql==0 ) return SQLITE_NOMEM; |
|
|
10013 |
rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0); |
|
|
10014 |
sqlite3_free(zSql); |
|
|
10015 |
|
|
|
10016 |
return rc; |
|
|
10017 |
} |
|
|
10018 |
|
|
|
10019 |
/* |
|
|
10020 |
** This function is called as part of sqlite3_expert_analyze(). Candidate |
|
|
10021 |
** indexes have already been created in database sqlite3expert.dbm, this |
|
|
10022 |
** function populates sqlite_stat1 table in the same database. |
|
|
10023 |
** |
|
|
10024 |
** The stat1 data is generated by querying the |
|
|
10025 |
*/ |
|
|
10026 |
static int idxPopulateStat1(sqlite3expert *p, char **pzErr){ |
|
|
10027 |
int rc = SQLITE_OK; |
|
|
10028 |
int nMax =0; |
|
|
10029 |
struct IdxRemCtx *pCtx = 0; |
|
|
10030 |
struct IdxSampleCtx samplectx; |
|
|
10031 |
int i; |
|
|
10032 |
i64 iPrev = -100000; |
|
|
10033 |
sqlite3_stmt *pAllIndex = 0; |
|
|
10034 |
sqlite3_stmt *pIndexXInfo = 0; |
|
|
10035 |
sqlite3_stmt *pWrite = 0; |
|
|
10036 |
|
|
|
10037 |
const char *zAllIndex = |
|
|
10038 |
"SELECT s.rowid, s.name, l.name FROM " |
|
|
10039 |
" sqlite_schema AS s, " |
|
|
10040 |
" pragma_index_list(s.name) AS l " |
|
|
10041 |
"WHERE s.type = 'table'"; |
|
|
10042 |
const char *zIndexXInfo = |
|
|
10043 |
"SELECT name, coll FROM pragma_index_xinfo(?) WHERE key"; |
|
|
10044 |
const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)"; |
|
|
10045 |
|
|
|
10046 |
/* If iSample==0, no sqlite_stat1 data is required. */ |
|
|
10047 |
if( p->iSample==0 ) return SQLITE_OK; |
|
|
10048 |
|
|
|
10049 |
rc = idxLargestIndex(p->dbm, &nMax, pzErr); |
|
|
10050 |
if( nMax<=0 || rc!=SQLITE_OK ) return rc; |
|
|
10051 |
|
|
|
10052 |
rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0); |
|
|
10053 |
|
|
|
10054 |
if( rc==SQLITE_OK ){ |
|
|
10055 |
int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax); |
|
|
10056 |
pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte); |
|
|
10057 |
} |
|
|
10058 |
|
|
|
10059 |
if( rc==SQLITE_OK ){ |
|
|
10060 |
sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); |
|
|
10061 |
rc = sqlite3_create_function( |
|
|
10062 |
dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0 |
|
|
10063 |
); |
|
|
10064 |
} |
|
|
10065 |
if( rc==SQLITE_OK ){ |
|
|
10066 |
rc = sqlite3_create_function( |
|
|
10067 |
p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0 |
|
|
10068 |
); |
|
|
10069 |
} |
|
|
10070 |
|
|
|
10071 |
if( rc==SQLITE_OK ){ |
|
|
10072 |
pCtx->nSlot = nMax+1; |
|
|
10073 |
rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex); |
|
|
10074 |
} |
|
|
10075 |
if( rc==SQLITE_OK ){ |
|
|
10076 |
rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo); |
|
|
10077 |
} |
|
|
10078 |
if( rc==SQLITE_OK ){ |
|
|
10079 |
rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite); |
|
|
10080 |
} |
|
|
10081 |
|
|
|
10082 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){ |
|
|
10083 |
i64 iRowid = sqlite3_column_int64(pAllIndex, 0); |
|
|
10084 |
const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1); |
|
|
10085 |
const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2); |
|
|
10086 |
if( p->iSample<100 && iPrev!=iRowid ){ |
|
|
10087 |
samplectx.target = (double)p->iSample / 100.0; |
|
|
10088 |
samplectx.iTarget = p->iSample; |
|
|
10089 |
samplectx.nRow = 0.0; |
|
|
10090 |
samplectx.nRet = 0.0; |
|
|
10091 |
rc = idxBuildSampleTable(p, zTab); |
|
|
10092 |
if( rc!=SQLITE_OK ) break; |
|
|
10093 |
} |
|
|
10094 |
rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr); |
|
|
10095 |
iPrev = iRowid; |
|
|
10096 |
} |
|
|
10097 |
if( rc==SQLITE_OK && p->iSample<100 ){ |
|
|
10098 |
rc = sqlite3_exec(p->dbv, |
|
|
10099 |
"DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0 |
|
|
10100 |
); |
|
|
10101 |
} |
|
|
10102 |
|
|
|
10103 |
idxFinalize(&rc, pAllIndex); |
|
|
10104 |
idxFinalize(&rc, pIndexXInfo); |
|
|
10105 |
idxFinalize(&rc, pWrite); |
|
|
10106 |
|
|
|
10107 |
if( pCtx ){ |
|
|
10108 |
for(i=0; inSlot; i++){ |
|
|
10109 |
sqlite3_free(pCtx->aSlot[i].z); |
|
|
10110 |
} |
|
|
10111 |
sqlite3_free(pCtx); |
|
|
10112 |
} |
|
|
10113 |
|
|
|
10114 |
if( rc==SQLITE_OK ){ |
|
|
10115 |
rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0); |
|
|
10116 |
} |
|
|
10117 |
|
|
|
10118 |
sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); |
|
|
10119 |
return rc; |
|
|
10120 |
} |
|
|
10121 |
|
|
|
10122 |
/* |
|
|
10123 |
** Allocate a new sqlite3expert object. |
|
|
10124 |
*/ |
|
|
10125 |
sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){ |
|
|
10126 |
int rc = SQLITE_OK; |
|
|
10127 |
sqlite3expert *pNew; |
|
|
10128 |
|
|
|
10129 |
pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert)); |
|
|
10130 |
|
|
|
10131 |
/* Open two in-memory databases to work with. The "vtab database" (dbv) |
|
|
10132 |
** will contain a virtual table corresponding to each real table in |
|
|
10133 |
** the user database schema, and a copy of each view. It is used to |
|
|
10134 |
** collect information regarding the WHERE, ORDER BY and other clauses |
|
|
10135 |
** of the user's query. |
|
|
10136 |
*/ |
|
|
10137 |
if( rc==SQLITE_OK ){ |
|
|
10138 |
pNew->db = db; |
|
|
10139 |
pNew->iSample = 100; |
|
|
10140 |
rc = sqlite3_open(":memory:", &pNew->dbv); |
|
|
10141 |
} |
|
|
10142 |
if( rc==SQLITE_OK ){ |
|
|
10143 |
rc = sqlite3_open(":memory:", &pNew->dbm); |
|
|
10144 |
if( rc==SQLITE_OK ){ |
|
|
10145 |
sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0); |
|
|
10146 |
} |
|
|
10147 |
} |
|
|
10148 |
|
|
|
10149 |
|
|
|
10150 |
/* Copy the entire schema of database [db] into [dbm]. */ |
|
|
10151 |
if( rc==SQLITE_OK ){ |
|
|
10152 |
sqlite3_stmt *pSql; |
|
|
10153 |
rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, |
|
|
10154 |
"SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'" |
|
|
10155 |
" AND sql NOT LIKE 'CREATE VIRTUAL %%'" |
|
|
10156 |
); |
|
|
10157 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
|
|
10158 |
const char *zSql = (const char*)sqlite3_column_text(pSql, 0); |
|
|
10159 |
rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg); |
|
|
10160 |
} |
|
|
10161 |
idxFinalize(&rc, pSql); |
|
|
10162 |
} |
|
|
10163 |
|
|
|
10164 |
/* Create the vtab schema */ |
|
|
10165 |
if( rc==SQLITE_OK ){ |
|
|
10166 |
rc = idxCreateVtabSchema(pNew, pzErrmsg); |
|
|
10167 |
} |
|
|
10168 |
|
|
|
10169 |
/* Register the auth callback with dbv */ |
|
|
10170 |
if( rc==SQLITE_OK ){ |
|
|
10171 |
sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew); |
|
|
10172 |
} |
|
|
10173 |
|
|
|
10174 |
/* If an error has occurred, free the new object and reutrn NULL. Otherwise, |
|
|
10175 |
** return the new sqlite3expert handle. */ |
|
|
10176 |
if( rc!=SQLITE_OK ){ |
|
|
10177 |
sqlite3_expert_destroy(pNew); |
|
|
10178 |
pNew = 0; |
|
|
10179 |
} |
|
|
10180 |
return pNew; |
|
|
10181 |
} |
|
|
10182 |
|
|
|
10183 |
/* |
|
|
10184 |
** Configure an sqlite3expert object. |
|
|
10185 |
*/ |
|
|
10186 |
int sqlite3_expert_config(sqlite3expert *p, int op, ...){ |
|
|
10187 |
int rc = SQLITE_OK; |
|
|
10188 |
va_list ap; |
|
|
10189 |
va_start(ap, op); |
|
|
10190 |
switch( op ){ |
|
|
10191 |
case EXPERT_CONFIG_SAMPLE: { |
|
|
10192 |
int iVal = va_arg(ap, int); |
|
|
10193 |
if( iVal<0 ) iVal = 0; |
|
|
10194 |
if( iVal>100 ) iVal = 100; |
|
|
10195 |
p->iSample = iVal; |
|
|
10196 |
break; |
|
|
10197 |
} |
|
|
10198 |
default: |
|
|
10199 |
rc = SQLITE_NOTFOUND; |
|
|
10200 |
break; |
|
|
10201 |
} |
|
|
10202 |
|
|
|
10203 |
va_end(ap); |
|
|
10204 |
return rc; |
|
|
10205 |
} |
|
|
10206 |
|
|
|
10207 |
/* |
|
|
10208 |
** Add an SQL statement to the analysis. |
|
|
10209 |
*/ |
|
|
10210 |
int sqlite3_expert_sql( |
|
|
10211 |
sqlite3expert *p, /* From sqlite3_expert_new() */ |
|
|
10212 |
const char *zSql, /* SQL statement to add */ |
|
|
10213 |
char **pzErr /* OUT: Error message (if any) */ |
|
|
10214 |
){ |
|
|
10215 |
IdxScan *pScanOrig = p->pScan; |
|
|
10216 |
IdxStatement *pStmtOrig = p->pStatement; |
|
|
10217 |
int rc = SQLITE_OK; |
|
|
10218 |
const char *zStmt = zSql; |
|
|
10219 |
|
|
|
10220 |
if( p->bRun ) return SQLITE_MISUSE; |
|
|
10221 |
|
|
|
10222 |
while( rc==SQLITE_OK && zStmt && zStmt[0] ){ |
|
|
10223 |
sqlite3_stmt *pStmt = 0; |
|
|
10224 |
rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt); |
|
|
10225 |
if( rc==SQLITE_OK ){ |
|
|
10226 |
if( pStmt ){ |
|
|
10227 |
IdxStatement *pNew; |
|
|
10228 |
const char *z = sqlite3_sql(pStmt); |
|
|
10229 |
int n = STRLEN(z); |
|
|
10230 |
pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1); |
|
|
10231 |
if( rc==SQLITE_OK ){ |
|
|
10232 |
pNew->zSql = (char*)&pNew[1]; |
|
|
10233 |
memcpy(pNew->zSql, z, n+1); |
|
|
10234 |
pNew->pNext = p->pStatement; |
|
|
10235 |
if( p->pStatement ) pNew->iId = p->pStatement->iId+1; |
|
|
10236 |
p->pStatement = pNew; |
|
|
10237 |
} |
|
|
10238 |
sqlite3_finalize(pStmt); |
|
|
10239 |
} |
|
|
10240 |
}else{ |
|
|
10241 |
idxDatabaseError(p->dbv, pzErr); |
|
|
10242 |
} |
|
|
10243 |
} |
|
|
10244 |
|
|
|
10245 |
if( rc!=SQLITE_OK ){ |
|
|
10246 |
idxScanFree(p->pScan, pScanOrig); |
|
|
10247 |
idxStatementFree(p->pStatement, pStmtOrig); |
|
|
10248 |
p->pScan = pScanOrig; |
|
|
10249 |
p->pStatement = pStmtOrig; |
|
|
10250 |
} |
|
|
10251 |
|
|
|
10252 |
return rc; |
|
|
10253 |
} |
|
|
10254 |
|
|
|
10255 |
int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){ |
|
|
10256 |
int rc; |
|
|
10257 |
IdxHashEntry *pEntry; |
|
|
10258 |
|
|
|
10259 |
/* Do trigger processing to collect any extra IdxScan structures */ |
|
|
10260 |
rc = idxProcessTriggers(p, pzErr); |
|
|
10261 |
|
|
|
10262 |
/* Create candidate indexes within the in-memory database file */ |
|
|
10263 |
if( rc==SQLITE_OK ){ |
|
|
10264 |
rc = idxCreateCandidates(p); |
|
|
10265 |
} |
|
|
10266 |
|
|
|
10267 |
/* Generate the stat1 data */ |
|
|
10268 |
if( rc==SQLITE_OK ){ |
|
|
10269 |
rc = idxPopulateStat1(p, pzErr); |
|
|
10270 |
} |
|
|
10271 |
|
|
|
10272 |
/* Formulate the EXPERT_REPORT_CANDIDATES text */ |
|
|
10273 |
for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ |
|
|
10274 |
p->zCandidates = idxAppendText(&rc, p->zCandidates, |
|
|
10275 |
"%s;%s%s\n", pEntry->zVal, |
|
|
10276 |
pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2 |
|
|
10277 |
); |
|
|
10278 |
} |
|
|
10279 |
|
|
|
10280 |
/* Figure out which of the candidate indexes are preferred by the query |
|
|
10281 |
** planner and report the results to the user. */ |
|
|
10282 |
if( rc==SQLITE_OK ){ |
|
|
10283 |
rc = idxFindIndexes(p, pzErr); |
|
|
10284 |
} |
|
|
10285 |
|
|
|
10286 |
if( rc==SQLITE_OK ){ |
|
|
10287 |
p->bRun = 1; |
|
|
10288 |
} |
|
|
10289 |
return rc; |
|
|
10290 |
} |
|
|
10291 |
|
|
|
10292 |
/* |
|
|
10293 |
** Return the total number of statements that have been added to this |
|
|
10294 |
** sqlite3expert using sqlite3_expert_sql(). |
|
|
10295 |
*/ |
|
|
10296 |
int sqlite3_expert_count(sqlite3expert *p){ |
|
|
10297 |
int nRet = 0; |
|
|
10298 |
if( p->pStatement ) nRet = p->pStatement->iId+1; |
|
|
10299 |
return nRet; |
|
|
10300 |
} |
|
|
10301 |
|
|
|
10302 |
/* |
|
|
10303 |
** Return a component of the report. |
|
|
10304 |
*/ |
|
|
10305 |
const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){ |
|
|
10306 |
const char *zRet = 0; |
|
|
10307 |
IdxStatement *pStmt; |
|
|
10308 |
|
|
|
10309 |
if( p->bRun==0 ) return 0; |
|
|
10310 |
for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext); |
|
|
10311 |
switch( eReport ){ |
|
|
10312 |
case EXPERT_REPORT_SQL: |
|
|
10313 |
if( pStmt ) zRet = pStmt->zSql; |
|
|
10314 |
break; |
|
|
10315 |
case EXPERT_REPORT_INDEXES: |
|
|
10316 |
if( pStmt ) zRet = pStmt->zIdx; |
|
|
10317 |
break; |
|
|
10318 |
case EXPERT_REPORT_PLAN: |
|
|
10319 |
if( pStmt ) zRet = pStmt->zEQP; |
|
|
10320 |
break; |
|
|
10321 |
case EXPERT_REPORT_CANDIDATES: |
|
|
10322 |
zRet = p->zCandidates; |
|
|
10323 |
break; |
|
|
10324 |
} |
|
|
10325 |
return zRet; |
|
|
10326 |
} |
|
|
10327 |
|
|
|
10328 |
/* |
|
|
10329 |
** Free an sqlite3expert object. |
|
|
10330 |
*/ |
|
|
10331 |
void sqlite3_expert_destroy(sqlite3expert *p){ |
|
|
10332 |
if( p ){ |
|
|
10333 |
sqlite3_close(p->dbm); |
|
|
10334 |
sqlite3_close(p->dbv); |
|
|
10335 |
idxScanFree(p->pScan, 0); |
|
|
10336 |
idxStatementFree(p->pStatement, 0); |
|
|
10337 |
idxTableFree(p->pTable); |
|
|
10338 |
idxWriteFree(p->pWrite); |
|
|
10339 |
idxHashClear(&p->hIdx); |
|
|
10340 |
sqlite3_free(p->zCandidates); |
|
|
10341 |
sqlite3_free(p); |
|
|
10342 |
} |
|
|
10343 |
} |
|
|
10344 |
|
|
|
10345 |
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
|
|
10346 |
|
|
|
10347 |
/************************* End ../ext/expert/sqlite3expert.c ********************/ |
|
|
10348 |
|
|
|
10349 |
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
|
|
10350 |
/************************* Begin ../ext/misc/dbdata.c ******************/ |
|
|
10351 |
/* |
|
|
10352 |
** 2019-04-17 |
|
|
10353 |
** |
|
|
10354 |
** The author disclaims copyright to this source code. In place of |
|
|
10355 |
** a legal notice, here is a blessing: |
|
|
10356 |
** |
|
|
10357 |
** May you do good and not evil. |
|
|
10358 |
** May you find forgiveness for yourself and forgive others. |
|
|
10359 |
** May you share freely, never taking more than you give. |
|
|
10360 |
** |
|
|
10361 |
****************************************************************************** |
|
|
10362 |
** |
|
|
10363 |
** This file contains an implementation of two eponymous virtual tables, |
|
|
10364 |
** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the |
|
|
10365 |
** "sqlite_dbpage" eponymous virtual table be available. |
|
|
10366 |
** |
|
|
10367 |
** SQLITE_DBDATA: |
|
|
10368 |
** sqlite_dbdata is used to extract data directly from a database b-tree |
|
|
10369 |
** page and its associated overflow pages, bypassing the b-tree layer. |
|
|
10370 |
** The table schema is equivalent to: |
|
|
10371 |
** |
|
|
10372 |
** CREATE TABLE sqlite_dbdata( |
|
|
10373 |
** pgno INTEGER, |
|
|
10374 |
** cell INTEGER, |
|
|
10375 |
** field INTEGER, |
|
|
10376 |
** value ANY, |
|
|
10377 |
** schema TEXT HIDDEN |
|
|
10378 |
** ); |
|
|
10379 |
** |
|
|
10380 |
** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE |
|
|
10381 |
** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND |
|
|
10382 |
** "schema". |
|
|
10383 |
** |
|
|
10384 |
** Each page of the database is inspected. If it cannot be interpreted as |
|
|
10385 |
** a b-tree page, or if it is a b-tree page containing 0 entries, the |
|
|
10386 |
** sqlite_dbdata table contains no rows for that page. Otherwise, the |
|
|
10387 |
** table contains one row for each field in the record associated with |
|
|
10388 |
** each cell on the page. For intkey b-trees, the key value is stored in |
|
|
10389 |
** field -1. |
|
|
10390 |
** |
|
|
10391 |
** For example, for the database: |
|
|
10392 |
** |
|
|
10393 |
** CREATE TABLE t1(a, b); -- root page is page 2 |
|
|
10394 |
** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five'); |
|
|
10395 |
** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten'); |
|
|
10396 |
** |
|
|
10397 |
** the sqlite_dbdata table contains, as well as from entries related to |
|
|
10398 |
** page 1, content equivalent to: |
|
|
10399 |
** |
|
|
10400 |
** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES |
|
|
10401 |
** (2, 0, -1, 5 ), |
|
|
10402 |
** (2, 0, 0, 'v' ), |
|
|
10403 |
** (2, 0, 1, 'five'), |
|
|
10404 |
** (2, 1, -1, 10 ), |
|
|
10405 |
** (2, 1, 0, 'x' ), |
|
|
10406 |
** (2, 1, 1, 'ten' ); |
|
|
10407 |
** |
|
|
10408 |
** If database corruption is encountered, this module does not report an |
|
|
10409 |
** error. Instead, it attempts to extract as much data as possible and |
|
|
10410 |
** ignores the corruption. |
|
|
10411 |
** |
|
|
10412 |
** SQLITE_DBPTR: |
|
|
10413 |
** The sqlite_dbptr table has the following schema: |
|
|
10414 |
** |
|
|
10415 |
** CREATE TABLE sqlite_dbptr( |
|
|
10416 |
** pgno INTEGER, |
|
|
10417 |
** child INTEGER, |
|
|
10418 |
** schema TEXT HIDDEN |
|
|
10419 |
** ); |
|
|
10420 |
** |
|
|
10421 |
** It contains one entry for each b-tree pointer between a parent and |
|
|
10422 |
** child page in the database. |
|
|
10423 |
*/ |
|
|
10424 |
#if !defined(SQLITEINT_H) |
|
|
10425 |
/* #include "sqlite3ext.h" */ |
|
|
10426 |
|
|
|
10427 |
/* typedef unsigned char u8; */ |
|
|
10428 |
|
|
|
10429 |
#endif |
|
|
10430 |
SQLITE_EXTENSION_INIT1 |
|
|
10431 |
#include |
|
|
10432 |
#include |
|
|
10433 |
|
|
|
10434 |
#define DBDATA_PADDING_BYTES 100 |
|
|
10435 |
|
|
|
10436 |
typedef struct DbdataTable DbdataTable; |
|
|
10437 |
typedef struct DbdataCursor DbdataCursor; |
|
|
10438 |
|
|
|
10439 |
/* Cursor object */ |
|
|
10440 |
struct DbdataCursor { |
|
|
10441 |
sqlite3_vtab_cursor base; /* Base class. Must be first */ |
|
|
10442 |
sqlite3_stmt *pStmt; /* For fetching database pages */ |
|
|
10443 |
|
|
|
10444 |
int iPgno; /* Current page number */ |
|
|
10445 |
u8 *aPage; /* Buffer containing page */ |
|
|
10446 |
int nPage; /* Size of aPage[] in bytes */ |
|
|
10447 |
int nCell; /* Number of cells on aPage[] */ |
|
|
10448 |
int iCell; /* Current cell number */ |
|
|
10449 |
int bOnePage; /* True to stop after one page */ |
|
|
10450 |
int szDb; |
|
|
10451 |
sqlite3_int64 iRowid; |
|
|
10452 |
|
|
|
10453 |
/* Only for the sqlite_dbdata table */ |
|
|
10454 |
u8 *pRec; /* Buffer containing current record */ |
|
|
10455 |
int nRec; /* Size of pRec[] in bytes */ |
|
|
10456 |
int nHdr; /* Size of header in bytes */ |
|
|
10457 |
int iField; /* Current field number */ |
|
|
10458 |
u8 *pHdrPtr; |
|
|
10459 |
u8 *pPtr; |
|
|
10460 |
|
|
|
10461 |
sqlite3_int64 iIntkey; /* Integer key value */ |
|
|
10462 |
}; |
|
|
10463 |
|
|
|
10464 |
/* Table object */ |
|
|
10465 |
struct DbdataTable { |
|
|
10466 |
sqlite3_vtab base; /* Base class. Must be first */ |
|
|
10467 |
sqlite3 *db; /* The database connection */ |
|
|
10468 |
sqlite3_stmt *pStmt; /* For fetching database pages */ |
|
|
10469 |
int bPtr; /* True for sqlite3_dbptr table */ |
|
|
10470 |
}; |
|
|
10471 |
|
|
|
10472 |
/* Column and schema definitions for sqlite_dbdata */ |
|
|
10473 |
#define DBDATA_COLUMN_PGNO 0 |
|
|
10474 |
#define DBDATA_COLUMN_CELL 1 |
|
|
10475 |
#define DBDATA_COLUMN_FIELD 2 |
|
|
10476 |
#define DBDATA_COLUMN_VALUE 3 |
|
|
10477 |
#define DBDATA_COLUMN_SCHEMA 4 |
|
|
10478 |
#define DBDATA_SCHEMA \ |
|
|
10479 |
"CREATE TABLE x(" \ |
|
|
10480 |
" pgno INTEGER," \ |
|
|
10481 |
" cell INTEGER," \ |
|
|
10482 |
" field INTEGER," \ |
|
|
10483 |
" value ANY," \ |
|
|
10484 |
" schema TEXT HIDDEN" \ |
|
|
10485 |
")" |
|
|
10486 |
|
|
|
10487 |
/* Column and schema definitions for sqlite_dbptr */ |
|
|
10488 |
#define DBPTR_COLUMN_PGNO 0 |
|
|
10489 |
#define DBPTR_COLUMN_CHILD 1 |
|
|
10490 |
#define DBPTR_COLUMN_SCHEMA 2 |
|
|
10491 |
#define DBPTR_SCHEMA \ |
|
|
10492 |
"CREATE TABLE x(" \ |
|
|
10493 |
" pgno INTEGER," \ |
|
|
10494 |
" child INTEGER," \ |
|
|
10495 |
" schema TEXT HIDDEN" \ |
|
|
10496 |
")" |
|
|
10497 |
|
|
|
10498 |
/* |
|
|
10499 |
** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual |
|
|
10500 |
** table. |
|
|
10501 |
*/ |
|
|
10502 |
static int dbdataConnect( |
|
|
10503 |
sqlite3 *db, |
|
|
10504 |
void *pAux, |
|
|
10505 |
int argc, const char *const*argv, |
|
|
10506 |
sqlite3_vtab **ppVtab, |
|
|
10507 |
char **pzErr |
|
|
10508 |
){ |
|
|
10509 |
DbdataTable *pTab = 0; |
|
|
10510 |
int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA); |
|
|
10511 |
|
|
|
10512 |
if( rc==SQLITE_OK ){ |
|
|
10513 |
pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable)); |
|
|
10514 |
if( pTab==0 ){ |
|
|
10515 |
rc = SQLITE_NOMEM; |
|
|
10516 |
}else{ |
|
|
10517 |
memset(pTab, 0, sizeof(DbdataTable)); |
|
|
10518 |
pTab->db = db; |
|
|
10519 |
pTab->bPtr = (pAux!=0); |
|
|
10520 |
} |
|
|
10521 |
} |
|
|
10522 |
|
|
|
10523 |
*ppVtab = (sqlite3_vtab*)pTab; |
|
|
10524 |
return rc; |
|
|
10525 |
} |
|
|
10526 |
|
|
|
10527 |
/* |
|
|
10528 |
** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table. |
|
|
10529 |
*/ |
|
|
10530 |
static int dbdataDisconnect(sqlite3_vtab *pVtab){ |
|
|
10531 |
DbdataTable *pTab = (DbdataTable*)pVtab; |
|
|
10532 |
if( pTab ){ |
|
|
10533 |
sqlite3_finalize(pTab->pStmt); |
|
|
10534 |
sqlite3_free(pVtab); |
|
|
10535 |
} |
|
|
10536 |
return SQLITE_OK; |
|
|
10537 |
} |
|
|
10538 |
|
|
|
10539 |
/* |
|
|
10540 |
** This function interprets two types of constraints: |
|
|
10541 |
** |
|
|
10542 |
** schema=? |
|
|
10543 |
** pgno=? |
|
|
10544 |
** |
|
|
10545 |
** If neither are present, idxNum is set to 0. If schema=? is present, |
|
|
10546 |
** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit |
|
|
10547 |
** in idxNum is set. |
|
|
10548 |
** |
|
|
10549 |
** If both parameters are present, schema is in position 0 and pgno in |
|
|
10550 |
** position 1. |
|
|
10551 |
*/ |
|
|
10552 |
static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){ |
|
|
10553 |
DbdataTable *pTab = (DbdataTable*)tab; |
|
|
10554 |
int i; |
|
|
10555 |
int iSchema = -1; |
|
|
10556 |
int iPgno = -1; |
|
|
10557 |
int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA); |
|
|
10558 |
|
|
|
10559 |
for(i=0; inConstraint; i++){ |
|
|
10560 |
struct sqlite3_index_constraint *p = &pIdx->aConstraint[i]; |
|
|
10561 |
if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
|
|
10562 |
if( p->iColumn==colSchema ){ |
|
|
10563 |
if( p->usable==0 ) return SQLITE_CONSTRAINT; |
|
|
10564 |
iSchema = i; |
|
|
10565 |
} |
|
|
10566 |
if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){ |
|
|
10567 |
iPgno = i; |
|
|
10568 |
} |
|
|
10569 |
} |
|
|
10570 |
} |
|
|
10571 |
|
|
|
10572 |
if( iSchema>=0 ){ |
|
|
10573 |
pIdx->aConstraintUsage[iSchema].argvIndex = 1; |
|
|
10574 |
pIdx->aConstraintUsage[iSchema].omit = 1; |
|
|
10575 |
} |
|
|
10576 |
if( iPgno>=0 ){ |
|
|
10577 |
pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0); |
|
|
10578 |
pIdx->aConstraintUsage[iPgno].omit = 1; |
|
|
10579 |
pIdx->estimatedCost = 100; |
|
|
10580 |
pIdx->estimatedRows = 50; |
|
|
10581 |
|
|
|
10582 |
if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){ |
|
|
10583 |
int iCol = pIdx->aOrderBy[0].iColumn; |
|
|
10584 |
if( pIdx->nOrderBy==1 ){ |
|
|
10585 |
pIdx->orderByConsumed = (iCol==0 || iCol==1); |
|
|
10586 |
}else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){ |
|
|
10587 |
pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1); |
|
|
10588 |
} |
|
|
10589 |
} |
|
|
10590 |
|
|
|
10591 |
}else{ |
|
|
10592 |
pIdx->estimatedCost = 100000000; |
|
|
10593 |
pIdx->estimatedRows = 1000000000; |
|
|
10594 |
} |
|
|
10595 |
pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00); |
|
|
10596 |
return SQLITE_OK; |
|
|
10597 |
} |
|
|
10598 |
|
|
|
10599 |
/* |
|
|
10600 |
** Open a new sqlite_dbdata or sqlite_dbptr cursor. |
|
|
10601 |
*/ |
|
|
10602 |
static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
|
|
10603 |
DbdataCursor *pCsr; |
|
|
10604 |
|
|
|
10605 |
pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor)); |
|
|
10606 |
if( pCsr==0 ){ |
|
|
10607 |
return SQLITE_NOMEM; |
|
|
10608 |
}else{ |
|
|
10609 |
memset(pCsr, 0, sizeof(DbdataCursor)); |
|
|
10610 |
pCsr->base.pVtab = pVTab; |
|
|
10611 |
} |
|
|
10612 |
|
|
|
10613 |
*ppCursor = (sqlite3_vtab_cursor *)pCsr; |
|
|
10614 |
return SQLITE_OK; |
|
|
10615 |
} |
|
|
10616 |
|
|
|
10617 |
/* |
|
|
10618 |
** Restore a cursor object to the state it was in when first allocated |
|
|
10619 |
** by dbdataOpen(). |
|
|
10620 |
*/ |
|
|
10621 |
static void dbdataResetCursor(DbdataCursor *pCsr){ |
|
|
10622 |
DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab); |
|
|
10623 |
if( pTab->pStmt==0 ){ |
|
|
10624 |
pTab->pStmt = pCsr->pStmt; |
|
|
10625 |
}else{ |
|
|
10626 |
sqlite3_finalize(pCsr->pStmt); |
|
|
10627 |
} |
|
|
10628 |
pCsr->pStmt = 0; |
|
|
10629 |
pCsr->iPgno = 1; |
|
|
10630 |
pCsr->iCell = 0; |
|
|
10631 |
pCsr->iField = 0; |
|
|
10632 |
pCsr->bOnePage = 0; |
|
|
10633 |
sqlite3_free(pCsr->aPage); |
|
|
10634 |
sqlite3_free(pCsr->pRec); |
|
|
10635 |
pCsr->pRec = 0; |
|
|
10636 |
pCsr->aPage = 0; |
|
|
10637 |
} |
|
|
10638 |
|
|
|
10639 |
/* |
|
|
10640 |
** Close an sqlite_dbdata or sqlite_dbptr cursor. |
|
|
10641 |
*/ |
|
|
10642 |
static int dbdataClose(sqlite3_vtab_cursor *pCursor){ |
|
|
10643 |
DbdataCursor *pCsr = (DbdataCursor*)pCursor; |
|
|
10644 |
dbdataResetCursor(pCsr); |
|
|
10645 |
sqlite3_free(pCsr); |
|
|
10646 |
return SQLITE_OK; |
|
|
10647 |
} |
|
|
10648 |
|
|
|
10649 |
/* |
|
|
10650 |
** Utility methods to decode 16 and 32-bit big-endian unsigned integers. |
|
|
10651 |
*/ |
|
|
10652 |
static unsigned int get_uint16(unsigned char *a){ |
|
|
10653 |
return (a[0]<<8)|a[1]; |
|
|
10654 |
} |
|
|
10655 |
static unsigned int get_uint32(unsigned char *a){ |
|
|
10656 |
return ((unsigned int)a[0]<<24) |
|
|
10657 |
| ((unsigned int)a[1]<<16) |
|
|
10658 |
| ((unsigned int)a[2]<<8) |
|
|
10659 |
| ((unsigned int)a[3]); |
|
|
10660 |
} |
|
|
10661 |
|
|
|
10662 |
/* |
|
|
10663 |
** Load page pgno from the database via the sqlite_dbpage virtual table. |
|
|
10664 |
** If successful, set (*ppPage) to point to a buffer containing the page |
|
|
10665 |
** data, (*pnPage) to the size of that buffer in bytes and return |
|
|
10666 |
** SQLITE_OK. In this case it is the responsibility of the caller to |
|
|
10667 |
** eventually free the buffer using sqlite3_free(). |
|
|
10668 |
** |
|
|
10669 |
** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and |
|
|
10670 |
** return an SQLite error code. |
|
|
10671 |
*/ |
|
|
10672 |
static int dbdataLoadPage( |
|
|
10673 |
DbdataCursor *pCsr, /* Cursor object */ |
|
|
10674 |
unsigned int pgno, /* Page number of page to load */ |
|
|
10675 |
u8 **ppPage, /* OUT: pointer to page buffer */ |
|
|
10676 |
int *pnPage /* OUT: Size of (*ppPage) in bytes */ |
|
|
10677 |
){ |
|
|
10678 |
int rc2; |
|
|
10679 |
int rc = SQLITE_OK; |
|
|
10680 |
sqlite3_stmt *pStmt = pCsr->pStmt; |
|
|
10681 |
|
|
|
10682 |
*ppPage = 0; |
|
|
10683 |
*pnPage = 0; |
|
|
10684 |
sqlite3_bind_int64(pStmt, 2, pgno); |
|
|
10685 |
if( SQLITE_ROW==sqlite3_step(pStmt) ){ |
|
|
10686 |
int nCopy = sqlite3_column_bytes(pStmt, 0); |
|
|
10687 |
if( nCopy>0 ){ |
|
|
10688 |
u8 *pPage; |
|
|
10689 |
pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES); |
|
|
10690 |
if( pPage==0 ){ |
|
|
10691 |
rc = SQLITE_NOMEM; |
|
|
10692 |
}else{ |
|
|
10693 |
const u8 *pCopy = sqlite3_column_blob(pStmt, 0); |
|
|
10694 |
memcpy(pPage, pCopy, nCopy); |
|
|
10695 |
memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES); |
|
|
10696 |
} |
|
|
10697 |
*ppPage = pPage; |
|
|
10698 |
*pnPage = nCopy; |
|
|
10699 |
} |
|
|
10700 |
} |
|
|
10701 |
rc2 = sqlite3_reset(pStmt); |
|
|
10702 |
if( rc==SQLITE_OK ) rc = rc2; |
|
|
10703 |
|
|
|
10704 |
return rc; |
|
|
10705 |
} |
|
|
10706 |
|
|
|
10707 |
/* |
|
|
10708 |
** Read a varint. Put the value in *pVal and return the number of bytes. |
|
|
10709 |
*/ |
|
|
10710 |
static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){ |
|
|
10711 |
sqlite3_int64 v = 0; |
|
|
10712 |
int i; |
|
|
10713 |
for(i=0; i<8; i++){ |
|
|
10714 |
v = (v<<7) + (z[i]&0x7f); |
|
|
10715 |
if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; } |
|
|
10716 |
} |
|
|
10717 |
v = (v<<8) + (z[i]&0xff); |
|
|
10718 |
*pVal = v; |
|
|
10719 |
return 9; |
|
|
10720 |
} |
|
|
10721 |
|
|
|
10722 |
/* |
|
|
10723 |
** Return the number of bytes of space used by an SQLite value of type |
|
|
10724 |
** eType. |
|
|
10725 |
*/ |
|
|
10726 |
static int dbdataValueBytes(int eType){ |
|
|
10727 |
switch( eType ){ |
|
|
10728 |
case 0: case 8: case 9: |
|
|
10729 |
case 10: case 11: |
|
|
10730 |
return 0; |
|
|
10731 |
case 1: |
|
|
10732 |
return 1; |
|
|
10733 |
case 2: |
|
|
10734 |
return 2; |
|
|
10735 |
case 3: |
|
|
10736 |
return 3; |
|
|
10737 |
case 4: |
|
|
10738 |
return 4; |
|
|
10739 |
case 5: |
|
|
10740 |
return 6; |
|
|
10741 |
case 6: |
|
|
10742 |
case 7: |
|
|
10743 |
return 8; |
|
|
10744 |
default: |
|
|
10745 |
if( eType>0 ){ |
|
|
10746 |
return ((eType-12) / 2); |
|
|
10747 |
} |
|
|
10748 |
return 0; |
|
|
10749 |
} |
|
|
10750 |
} |
|
|
10751 |
|
|
|
10752 |
/* |
|
|
10753 |
** Load a value of type eType from buffer pData and use it to set the |
|
|
10754 |
** result of context object pCtx. |
|
|
10755 |
*/ |
|
|
10756 |
static void dbdataValue( |
|
|
10757 |
sqlite3_context *pCtx, |
|
|
10758 |
int eType, |
|
|
10759 |
u8 *pData, |
|
|
10760 |
int nData |
|
|
10761 |
){ |
|
|
10762 |
if( eType>=0 && dbdataValueBytes(eType)<=nData ){ |
|
|
10763 |
switch( eType ){ |
|
|
10764 |
case 0: |
|
|
10765 |
case 10: |
|
|
10766 |
case 11: |
|
|
10767 |
sqlite3_result_null(pCtx); |
|
|
10768 |
break; |
|
|
10769 |
|
|
|
10770 |
case 8: |
|
|
10771 |
sqlite3_result_int(pCtx, 0); |
|
|
10772 |
break; |
|
|
10773 |
case 9: |
|
|
10774 |
sqlite3_result_int(pCtx, 1); |
|
|
10775 |
break; |
|
|
10776 |
|
|
|
10777 |
case 1: case 2: case 3: case 4: case 5: case 6: case 7: { |
|
|
10778 |
sqlite3_uint64 v = (signed char)pData[0]; |
|
|
10779 |
pData++; |
|
|
10780 |
switch( eType ){ |
|
|
10781 |
case 7: |
|
|
10782 |
case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; |
|
|
10783 |
case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; |
|
|
10784 |
case 4: v = (v<<8) + pData[0]; pData++; |
|
|
10785 |
case 3: v = (v<<8) + pData[0]; pData++; |
|
|
10786 |
case 2: v = (v<<8) + pData[0]; pData++; |
|
|
10787 |
} |
|
|
10788 |
|
|
|
10789 |
if( eType==7 ){ |
|
|
10790 |
double r; |
|
|
10791 |
memcpy(&r, &v, sizeof(r)); |
|
|
10792 |
sqlite3_result_double(pCtx, r); |
|
|
10793 |
}else{ |
|
|
10794 |
sqlite3_result_int64(pCtx, (sqlite3_int64)v); |
|
|
10795 |
} |
|
|
10796 |
break; |
|
|
10797 |
} |
|
|
10798 |
|
|
|
10799 |
default: { |
|
|
10800 |
int n = ((eType-12) / 2); |
|
|
10801 |
if( eType % 2 ){ |
|
|
10802 |
sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT); |
|
|
10803 |
}else{ |
|
|
10804 |
sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT); |
|
|
10805 |
} |
|
|
10806 |
} |
|
|
10807 |
} |
|
|
10808 |
} |
|
|
10809 |
} |
|
|
10810 |
|
|
|
10811 |
/* |
|
|
10812 |
** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry. |
|
|
10813 |
*/ |
|
|
10814 |
static int dbdataNext(sqlite3_vtab_cursor *pCursor){ |
|
|
10815 |
DbdataCursor *pCsr = (DbdataCursor*)pCursor; |
|
|
10816 |
DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; |
|
|
10817 |
|
|
|
10818 |
pCsr->iRowid++; |
|
|
10819 |
while( 1 ){ |
|
|
10820 |
int rc; |
|
|
10821 |
int iOff = (pCsr->iPgno==1 ? 100 : 0); |
|
|
10822 |
int bNextPage = 0; |
|
|
10823 |
|
|
|
10824 |
if( pCsr->aPage==0 ){ |
|
|
10825 |
while( 1 ){ |
|
|
10826 |
if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK; |
|
|
10827 |
rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage); |
|
|
10828 |
if( rc!=SQLITE_OK ) return rc; |
|
|
10829 |
if( pCsr->aPage ) break; |
|
|
10830 |
pCsr->iPgno++; |
|
|
10831 |
} |
|
|
10832 |
pCsr->iCell = pTab->bPtr ? -2 : 0; |
|
|
10833 |
pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]); |
|
|
10834 |
} |
|
|
10835 |
|
|
|
10836 |
if( pTab->bPtr ){ |
|
|
10837 |
if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){ |
|
|
10838 |
pCsr->iCell = pCsr->nCell; |
|
|
10839 |
} |
|
|
10840 |
pCsr->iCell++; |
|
|
10841 |
if( pCsr->iCell>=pCsr->nCell ){ |
|
|
10842 |
sqlite3_free(pCsr->aPage); |
|
|
10843 |
pCsr->aPage = 0; |
|
|
10844 |
if( pCsr->bOnePage ) return SQLITE_OK; |
|
|
10845 |
pCsr->iPgno++; |
|
|
10846 |
}else{ |
|
|
10847 |
return SQLITE_OK; |
|
|
10848 |
} |
|
|
10849 |
}else{ |
|
|
10850 |
/* If there is no record loaded, load it now. */ |
|
|
10851 |
if( pCsr->pRec==0 ){ |
|
|
10852 |
int bHasRowid = 0; |
|
|
10853 |
int nPointer = 0; |
|
|
10854 |
sqlite3_int64 nPayload = 0; |
|
|
10855 |
sqlite3_int64 nHdr = 0; |
|
|
10856 |
int iHdr; |
|
|
10857 |
int U, X; |
|
|
10858 |
int nLocal; |
|
|
10859 |
|
|
|
10860 |
switch( pCsr->aPage[iOff] ){ |
|
|
10861 |
case 0x02: |
|
|
10862 |
nPointer = 4; |
|
|
10863 |
break; |
|
|
10864 |
case 0x0a: |
|
|
10865 |
break; |
|
|
10866 |
case 0x0d: |
|
|
10867 |
bHasRowid = 1; |
|
|
10868 |
break; |
|
|
10869 |
default: |
|
|
10870 |
/* This is not a b-tree page with records on it. Continue. */ |
|
|
10871 |
pCsr->iCell = pCsr->nCell; |
|
|
10872 |
break; |
|
|
10873 |
} |
|
|
10874 |
|
|
|
10875 |
if( pCsr->iCell>=pCsr->nCell ){ |
|
|
10876 |
bNextPage = 1; |
|
|
10877 |
}else{ |
|
|
10878 |
|
|
|
10879 |
iOff += 8 + nPointer + pCsr->iCell*2; |
|
|
10880 |
if( iOff>pCsr->nPage ){ |
|
|
10881 |
bNextPage = 1; |
|
|
10882 |
}else{ |
|
|
10883 |
iOff = get_uint16(&pCsr->aPage[iOff]); |
|
|
10884 |
} |
|
|
10885 |
|
|
|
10886 |
/* For an interior node cell, skip past the child-page number */ |
|
|
10887 |
iOff += nPointer; |
|
|
10888 |
|
|
|
10889 |
/* Load the "byte of payload including overflow" field */ |
|
|
10890 |
if( bNextPage || iOff>pCsr->nPage ){ |
|
|
10891 |
bNextPage = 1; |
|
|
10892 |
}else{ |
|
|
10893 |
iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload); |
|
|
10894 |
} |
|
|
10895 |
|
|
|
10896 |
/* If this is a leaf intkey cell, load the rowid */ |
|
|
10897 |
if( bHasRowid && !bNextPage && iOffnPage ){ |
|
|
10898 |
iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey); |
|
|
10899 |
} |
|
|
10900 |
|
|
|
10901 |
/* Figure out how much data to read from the local page */ |
|
|
10902 |
U = pCsr->nPage; |
|
|
10903 |
if( bHasRowid ){ |
|
|
10904 |
X = U-35; |
|
|
10905 |
}else{ |
|
|
10906 |
X = ((U-12)*64/255)-23; |
|
|
10907 |
} |
|
|
10908 |
if( nPayload<=X ){ |
|
|
10909 |
nLocal = nPayload; |
|
|
10910 |
}else{ |
|
|
10911 |
int M, K; |
|
|
10912 |
M = ((U-12)*32/255)-23; |
|
|
10913 |
K = M+((nPayload-M)%(U-4)); |
|
|
10914 |
if( K<=X ){ |
|
|
10915 |
nLocal = K; |
|
|
10916 |
}else{ |
|
|
10917 |
nLocal = M; |
|
|
10918 |
} |
|
|
10919 |
} |
|
|
10920 |
|
|
|
10921 |
if( bNextPage || nLocal+iOff>pCsr->nPage ){ |
|
|
10922 |
bNextPage = 1; |
|
|
10923 |
}else{ |
|
|
10924 |
|
|
|
10925 |
/* Allocate space for payload. And a bit more to catch small buffer |
|
|
10926 |
** overruns caused by attempting to read a varint or similar from |
|
|
10927 |
** near the end of a corrupt record. */ |
|
|
10928 |
pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES); |
|
|
10929 |
if( pCsr->pRec==0 ) return SQLITE_NOMEM; |
|
|
10930 |
memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES); |
|
|
10931 |
pCsr->nRec = nPayload; |
|
|
10932 |
|
|
|
10933 |
/* Load the nLocal bytes of payload */ |
|
|
10934 |
memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal); |
|
|
10935 |
iOff += nLocal; |
|
|
10936 |
|
|
|
10937 |
/* Load content from overflow pages */ |
|
|
10938 |
if( nPayload>nLocal ){ |
|
|
10939 |
sqlite3_int64 nRem = nPayload - nLocal; |
|
|
10940 |
unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]); |
|
|
10941 |
while( nRem>0 ){ |
|
|
10942 |
u8 *aOvfl = 0; |
|
|
10943 |
int nOvfl = 0; |
|
|
10944 |
int nCopy; |
|
|
10945 |
rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl); |
|
|
10946 |
assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage ); |
|
|
10947 |
if( rc!=SQLITE_OK ) return rc; |
|
|
10948 |
if( aOvfl==0 ) break; |
|
|
10949 |
|
|
|
10950 |
nCopy = U-4; |
|
|
10951 |
if( nCopy>nRem ) nCopy = nRem; |
|
|
10952 |
memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy); |
|
|
10953 |
nRem -= nCopy; |
|
|
10954 |
|
|
|
10955 |
pgnoOvfl = get_uint32(aOvfl); |
|
|
10956 |
sqlite3_free(aOvfl); |
|
|
10957 |
} |
|
|
10958 |
} |
|
|
10959 |
|
|
|
10960 |
iHdr = dbdataGetVarint(pCsr->pRec, &nHdr); |
|
|
10961 |
pCsr->nHdr = nHdr; |
|
|
10962 |
pCsr->pHdrPtr = &pCsr->pRec[iHdr]; |
|
|
10963 |
pCsr->pPtr = &pCsr->pRec[pCsr->nHdr]; |
|
|
10964 |
pCsr->iField = (bHasRowid ? -1 : 0); |
|
|
10965 |
} |
|
|
10966 |
} |
|
|
10967 |
}else{ |
|
|
10968 |
pCsr->iField++; |
|
|
10969 |
if( pCsr->iField>0 ){ |
|
|
10970 |
sqlite3_int64 iType; |
|
|
10971 |
if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){ |
|
|
10972 |
bNextPage = 1; |
|
|
10973 |
}else{ |
|
|
10974 |
pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType); |
|
|
10975 |
pCsr->pPtr += dbdataValueBytes(iType); |
|
|
10976 |
} |
|
|
10977 |
} |
|
|
10978 |
} |
|
|
10979 |
|
|
|
10980 |
if( bNextPage ){ |
|
|
10981 |
sqlite3_free(pCsr->aPage); |
|
|
10982 |
sqlite3_free(pCsr->pRec); |
|
|
10983 |
pCsr->aPage = 0; |
|
|
10984 |
pCsr->pRec = 0; |
|
|
10985 |
if( pCsr->bOnePage ) return SQLITE_OK; |
|
|
10986 |
pCsr->iPgno++; |
|
|
10987 |
}else{ |
|
|
10988 |
if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){ |
|
|
10989 |
return SQLITE_OK; |
|
|
10990 |
} |
|
|
10991 |
|
|
|
10992 |
/* Advance to the next cell. The next iteration of the loop will load |
|
|
10993 |
** the record and so on. */ |
|
|
10994 |
sqlite3_free(pCsr->pRec); |
|
|
10995 |
pCsr->pRec = 0; |
|
|
10996 |
pCsr->iCell++; |
|
|
10997 |
} |
|
|
10998 |
} |
|
|
10999 |
} |
|
|
11000 |
|
|
|
11001 |
assert( !"can't get here" ); |
|
|
11002 |
return SQLITE_OK; |
|
|
11003 |
} |
|
|
11004 |
|
|
|
11005 |
/* |
|
|
11006 |
** Return true if the cursor is at EOF. |
|
|
11007 |
*/ |
|
|
11008 |
static int dbdataEof(sqlite3_vtab_cursor *pCursor){ |
|
|
11009 |
DbdataCursor *pCsr = (DbdataCursor*)pCursor; |
|
|
11010 |
return pCsr->aPage==0; |
|
|
11011 |
} |
|
|
11012 |
|
|
|
11013 |
/* |
|
|
11014 |
** Determine the size in pages of database zSchema (where zSchema is |
|
|
11015 |
** "main", "temp" or the name of an attached database) and set |
|
|
11016 |
** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise, |
|
|
11017 |
** an SQLite error code. |
|
|
11018 |
*/ |
|
|
11019 |
static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){ |
|
|
11020 |
DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab; |
|
|
11021 |
char *zSql = 0; |
|
|
11022 |
int rc, rc2; |
|
|
11023 |
sqlite3_stmt *pStmt = 0; |
|
|
11024 |
|
|
|
11025 |
zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema); |
|
|
11026 |
if( zSql==0 ) return SQLITE_NOMEM; |
|
|
11027 |
rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0); |
|
|
11028 |
sqlite3_free(zSql); |
|
|
11029 |
if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
11030 |
pCsr->szDb = sqlite3_column_int(pStmt, 0); |
|
|
11031 |
} |
|
|
11032 |
rc2 = sqlite3_finalize(pStmt); |
|
|
11033 |
if( rc==SQLITE_OK ) rc = rc2; |
|
|
11034 |
return rc; |
|
|
11035 |
} |
|
|
11036 |
|
|
|
11037 |
/* |
|
|
11038 |
** xFilter method for sqlite_dbdata and sqlite_dbptr. |
|
|
11039 |
*/ |
|
|
11040 |
static int dbdataFilter( |
|
|
11041 |
sqlite3_vtab_cursor *pCursor, |
|
|
11042 |
int idxNum, const char *idxStr, |
|
|
11043 |
int argc, sqlite3_value **argv |
|
|
11044 |
){ |
|
|
11045 |
DbdataCursor *pCsr = (DbdataCursor*)pCursor; |
|
|
11046 |
DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; |
|
|
11047 |
int rc = SQLITE_OK; |
|
|
11048 |
const char *zSchema = "main"; |
|
|
11049 |
|
|
|
11050 |
dbdataResetCursor(pCsr); |
|
|
11051 |
assert( pCsr->iPgno==1 ); |
|
|
11052 |
if( idxNum & 0x01 ){ |
|
|
11053 |
zSchema = (const char*)sqlite3_value_text(argv[0]); |
|
|
11054 |
} |
|
|
11055 |
if( idxNum & 0x02 ){ |
|
|
11056 |
pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]); |
|
|
11057 |
pCsr->bOnePage = 1; |
|
|
11058 |
}else{ |
|
|
11059 |
pCsr->nPage = dbdataDbsize(pCsr, zSchema); |
|
|
11060 |
rc = dbdataDbsize(pCsr, zSchema); |
|
|
11061 |
} |
|
|
11062 |
|
|
|
11063 |
if( rc==SQLITE_OK ){ |
|
|
11064 |
if( pTab->pStmt ){ |
|
|
11065 |
pCsr->pStmt = pTab->pStmt; |
|
|
11066 |
pTab->pStmt = 0; |
|
|
11067 |
}else{ |
|
|
11068 |
rc = sqlite3_prepare_v2(pTab->db, |
|
|
11069 |
"SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1, |
|
|
11070 |
&pCsr->pStmt, 0 |
|
|
11071 |
); |
|
|
11072 |
} |
|
|
11073 |
} |
|
|
11074 |
if( rc==SQLITE_OK ){ |
|
|
11075 |
rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT); |
|
|
11076 |
}else{ |
|
|
11077 |
pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); |
|
|
11078 |
} |
|
|
11079 |
if( rc==SQLITE_OK ){ |
|
|
11080 |
rc = dbdataNext(pCursor); |
|
|
11081 |
} |
|
|
11082 |
return rc; |
|
|
11083 |
} |
|
|
11084 |
|
|
|
11085 |
/* |
|
|
11086 |
** Return a column for the sqlite_dbdata or sqlite_dbptr table. |
|
|
11087 |
*/ |
|
|
11088 |
static int dbdataColumn( |
|
|
11089 |
sqlite3_vtab_cursor *pCursor, |
|
|
11090 |
sqlite3_context *ctx, |
|
|
11091 |
int i |
|
|
11092 |
){ |
|
|
11093 |
DbdataCursor *pCsr = (DbdataCursor*)pCursor; |
|
|
11094 |
DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; |
|
|
11095 |
if( pTab->bPtr ){ |
|
|
11096 |
switch( i ){ |
|
|
11097 |
case DBPTR_COLUMN_PGNO: |
|
|
11098 |
sqlite3_result_int64(ctx, pCsr->iPgno); |
|
|
11099 |
break; |
|
|
11100 |
case DBPTR_COLUMN_CHILD: { |
|
|
11101 |
int iOff = pCsr->iPgno==1 ? 100 : 0; |
|
|
11102 |
if( pCsr->iCell<0 ){ |
|
|
11103 |
iOff += 8; |
|
|
11104 |
}else{ |
|
|
11105 |
iOff += 12 + pCsr->iCell*2; |
|
|
11106 |
if( iOff>pCsr->nPage ) return SQLITE_OK; |
|
|
11107 |
iOff = get_uint16(&pCsr->aPage[iOff]); |
|
|
11108 |
} |
|
|
11109 |
if( iOff<=pCsr->nPage ){ |
|
|
11110 |
sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff])); |
|
|
11111 |
} |
|
|
11112 |
break; |
|
|
11113 |
} |
|
|
11114 |
} |
|
|
11115 |
}else{ |
|
|
11116 |
switch( i ){ |
|
|
11117 |
case DBDATA_COLUMN_PGNO: |
|
|
11118 |
sqlite3_result_int64(ctx, pCsr->iPgno); |
|
|
11119 |
break; |
|
|
11120 |
case DBDATA_COLUMN_CELL: |
|
|
11121 |
sqlite3_result_int(ctx, pCsr->iCell); |
|
|
11122 |
break; |
|
|
11123 |
case DBDATA_COLUMN_FIELD: |
|
|
11124 |
sqlite3_result_int(ctx, pCsr->iField); |
|
|
11125 |
break; |
|
|
11126 |
case DBDATA_COLUMN_VALUE: { |
|
|
11127 |
if( pCsr->iField<0 ){ |
|
|
11128 |
sqlite3_result_int64(ctx, pCsr->iIntkey); |
|
|
11129 |
}else{ |
|
|
11130 |
sqlite3_int64 iType; |
|
|
11131 |
dbdataGetVarint(pCsr->pHdrPtr, &iType); |
|
|
11132 |
dbdataValue( |
|
|
11133 |
ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr |
|
|
11134 |
); |
|
|
11135 |
} |
|
|
11136 |
break; |
|
|
11137 |
} |
|
|
11138 |
} |
|
|
11139 |
} |
|
|
11140 |
return SQLITE_OK; |
|
|
11141 |
} |
|
|
11142 |
|
|
|
11143 |
/* |
|
|
11144 |
** Return the rowid for an sqlite_dbdata or sqlite_dptr table. |
|
|
11145 |
*/ |
|
|
11146 |
static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ |
|
|
11147 |
DbdataCursor *pCsr = (DbdataCursor*)pCursor; |
|
|
11148 |
*pRowid = pCsr->iRowid; |
|
|
11149 |
return SQLITE_OK; |
|
|
11150 |
} |
|
|
11151 |
|
|
|
11152 |
|
|
|
11153 |
/* |
|
|
11154 |
** Invoke this routine to register the "sqlite_dbdata" virtual table module |
|
|
11155 |
*/ |
|
|
11156 |
static int sqlite3DbdataRegister(sqlite3 *db){ |
|
|
11157 |
static sqlite3_module dbdata_module = { |
|
|
11158 |
0, /* iVersion */ |
|
|
11159 |
0, /* xCreate */ |
|
|
11160 |
dbdataConnect, /* xConnect */ |
|
|
11161 |
dbdataBestIndex, /* xBestIndex */ |
|
|
11162 |
dbdataDisconnect, /* xDisconnect */ |
|
|
11163 |
0, /* xDestroy */ |
|
|
11164 |
dbdataOpen, /* xOpen - open a cursor */ |
|
|
11165 |
dbdataClose, /* xClose - close a cursor */ |
|
|
11166 |
dbdataFilter, /* xFilter - configure scan constraints */ |
|
|
11167 |
dbdataNext, /* xNext - advance a cursor */ |
|
|
11168 |
dbdataEof, /* xEof - check for end of scan */ |
|
|
11169 |
dbdataColumn, /* xColumn - read data */ |
|
|
11170 |
dbdataRowid, /* xRowid - read data */ |
|
|
11171 |
0, /* xUpdate */ |
|
|
11172 |
0, /* xBegin */ |
|
|
11173 |
0, /* xSync */ |
|
|
11174 |
0, /* xCommit */ |
|
|
11175 |
0, /* xRollback */ |
|
|
11176 |
0, /* xFindMethod */ |
|
|
11177 |
0, /* xRename */ |
|
|
11178 |
0, /* xSavepoint */ |
|
|
11179 |
0, /* xRelease */ |
|
|
11180 |
0, /* xRollbackTo */ |
|
|
11181 |
|
|
|
11182 |
}; |
|
|
11183 |
|
|
|
11184 |
int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0); |
|
|
11185 |
if( rc==SQLITE_OK ){ |
|
|
11186 |
rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1); |
|
|
11187 |
} |
|
|
11188 |
return rc; |
|
|
11189 |
} |
|
|
11190 |
|
|
|
11191 |
#ifdef _WIN32 |
|
|
11192 |
|
|
|
11193 |
#endif |
|
|
11194 |
int sqlite3_dbdata_init( |
|
|
11195 |
sqlite3 *db, |
|
|
11196 |
char **pzErrMsg, |
|
|
11197 |
const sqlite3_api_routines *pApi |
|
|
11198 |
){ |
|
|
11199 |
SQLITE_EXTENSION_INIT2(pApi); |
|
|
11200 |
return sqlite3DbdataRegister(db); |
|
|
11201 |
} |
|
|
11202 |
|
|
|
11203 |
/************************* End ../ext/misc/dbdata.c ********************/ |
|
|
11204 |
#endif |
|
|
11205 |
|
|
|
11206 |
#if defined(SQLITE_ENABLE_SESSION) |
|
|
11207 |
/* |
|
|
11208 |
** State information for a single open session |
|
|
11209 |
*/ |
|
|
11210 |
typedef struct OpenSession OpenSession; |
|
|
11211 |
struct OpenSession { |
|
|
11212 |
char *zName; /* Symbolic name for this session */ |
|
|
11213 |
int nFilter; /* Number of xFilter rejection GLOB patterns */ |
|
|
11214 |
char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
|
|
11215 |
sqlite3_session *p; /* The open session */ |
|
|
11216 |
}; |
|
|
11217 |
#endif |
|
|
11218 |
|
|
|
11219 |
typedef struct ExpertInfo ExpertInfo; |
|
|
11220 |
struct ExpertInfo { |
|
|
11221 |
sqlite3expert *pExpert; |
|
|
11222 |
int bVerbose; |
|
|
11223 |
}; |
|
|
11224 |
|
|
|
11225 |
/* A single line in the EQP output */ |
|
|
11226 |
typedef struct EQPGraphRow EQPGraphRow; |
|
|
11227 |
struct EQPGraphRow { |
|
|
11228 |
int iEqpId; /* ID for this row */ |
|
|
11229 |
int iParentId; /* ID of the parent row */ |
|
|
11230 |
EQPGraphRow *pNext; /* Next row in sequence */ |
|
|
11231 |
char zText[1]; /* Text to display for this row */ |
|
|
11232 |
}; |
|
|
11233 |
|
|
|
11234 |
/* All EQP output is collected into an instance of the following */ |
|
|
11235 |
typedef struct EQPGraph EQPGraph; |
|
|
11236 |
struct EQPGraph { |
|
|
11237 |
EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */ |
|
|
11238 |
EQPGraphRow *pLast; /* Last element of the pRow list */ |
|
|
11239 |
char zPrefix[100]; /* Graph prefix */ |
|
|
11240 |
}; |
|
|
11241 |
|
|
|
11242 |
/* |
|
|
11243 |
** State information about the database connection is contained in an |
|
|
11244 |
** instance of the following structure. |
|
|
11245 |
*/ |
|
|
11246 |
typedef struct ShellState ShellState; |
|
|
11247 |
struct ShellState { |
|
|
11248 |
sqlite3 *db; /* The database */ |
|
|
11249 |
u8 autoExplain; /* Automatically turn on .explain mode */ |
|
|
11250 |
u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
|
|
11251 |
u8 autoEQPtest; /* autoEQP is in test mode */ |
|
|
11252 |
u8 autoEQPtrace; /* autoEQP is in trace mode */ |
|
|
11253 |
u8 scanstatsOn; /* True to display scan stats before each finalize */ |
|
|
11254 |
u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */ |
|
|
11255 |
u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */ |
|
|
11256 |
u8 nEqpLevel; /* Depth of the EQP output graph */ |
|
|
11257 |
u8 eTraceType; /* SHELL_TRACE_* value for type of trace */ |
|
|
11258 |
unsigned statsOn; /* True to display memory stats before each finalize */ |
|
|
11259 |
unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */ |
|
|
11260 |
int outCount; /* Revert to stdout when reaching zero */ |
|
|
11261 |
int cnt; /* Number of records displayed so far */ |
|
|
11262 |
int lineno; /* Line number of last line read from in */ |
|
|
11263 |
int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */ |
|
|
11264 |
FILE *in; /* Read commands from this stream */ |
|
|
11265 |
FILE *out; /* Write results here */ |
|
|
11266 |
FILE *traceOut; /* Output for sqlite3_trace() */ |
|
|
11267 |
int nErr; /* Number of errors seen */ |
|
|
11268 |
int mode; /* An output mode setting */ |
|
|
11269 |
int modePrior; /* Saved mode */ |
|
|
11270 |
int cMode; /* temporary output mode for the current query */ |
|
|
11271 |
int normalMode; /* Output mode before ".explain on" */ |
|
|
11272 |
int writableSchema; /* True if PRAGMA writable_schema=ON */ |
|
|
11273 |
int showHeader; /* True to show column names in List or Column mode */ |
|
|
11274 |
int nCheck; /* Number of ".check" commands run */ |
|
|
11275 |
unsigned nProgress; /* Number of progress callbacks encountered */ |
|
|
11276 |
unsigned mxProgress; /* Maximum progress callbacks before failing */ |
|
|
11277 |
unsigned flgProgress; /* Flags for the progress callback */ |
|
|
11278 |
unsigned shellFlgs; /* Various flags */ |
|
|
11279 |
unsigned priorShFlgs; /* Saved copy of flags */ |
|
|
11280 |
sqlite3_int64 szMax; /* --maxsize argument to .open */ |
|
|
11281 |
char *zDestTable; /* Name of destination table when MODE_Insert */ |
|
|
11282 |
char *zTempFile; /* Temporary file that might need deleting */ |
|
|
11283 |
char zTestcase[30]; /* Name of current test case */ |
|
|
11284 |
char colSeparator[20]; /* Column separator character for several modes */ |
|
|
11285 |
char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
|
|
11286 |
char colSepPrior[20]; /* Saved column separator */ |
|
|
11287 |
char rowSepPrior[20]; /* Saved row separator */ |
|
|
11288 |
int *colWidth; /* Requested width of each column in columnar modes */ |
|
|
11289 |
int *actualWidth; /* Actual width of each column */ |
|
|
11290 |
int nWidth; /* Number of slots in colWidth[] and actualWidth[] */ |
|
|
11291 |
char nullValue[20]; /* The text to print when a NULL comes back from |
|
|
11292 |
** the database */ |
|
|
11293 |
char outfile[FILENAME_MAX]; /* Filename for *out */ |
|
|
11294 |
const char *zDbFilename; /* name of the database file */ |
|
|
11295 |
char *zFreeOnClose; /* Filename to free when closing */ |
|
|
11296 |
const char *zVfs; /* Name of VFS to use */ |
|
|
11297 |
sqlite3_stmt *pStmt; /* Current statement if any. */ |
|
|
11298 |
FILE *pLog; /* Write log output here */ |
|
|
11299 |
int *aiIndent; /* Array of indents used in MODE_Explain */ |
|
|
11300 |
int nIndent; /* Size of array aiIndent[] */ |
|
|
11301 |
int iIndent; /* Index of current op in aiIndent[] */ |
|
|
11302 |
EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */ |
|
|
11303 |
#if defined(SQLITE_ENABLE_SESSION) |
|
|
11304 |
int nSession; /* Number of active sessions */ |
|
|
11305 |
OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
|
|
11306 |
#endif |
|
|
11307 |
ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */ |
|
|
11308 |
}; |
|
|
11309 |
|
|
|
11310 |
|
|
|
11311 |
/* Allowed values for ShellState.autoEQP |
|
|
11312 |
*/ |
|
|
11313 |
#define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */ |
|
|
11314 |
#define AUTOEQP_on 1 /* Automatic EQP is on */ |
|
|
11315 |
#define AUTOEQP_trigger 2 /* On and also show plans for triggers */ |
|
|
11316 |
#define AUTOEQP_full 3 /* Show full EXPLAIN */ |
|
|
11317 |
|
|
|
11318 |
/* Allowed values for ShellState.openMode |
|
|
11319 |
*/ |
|
|
11320 |
#define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */ |
|
|
11321 |
#define SHELL_OPEN_NORMAL 1 /* Normal database file */ |
|
|
11322 |
#define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */ |
|
|
11323 |
#define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */ |
|
|
11324 |
#define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */ |
|
|
11325 |
#define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */ |
|
|
11326 |
#define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */ |
|
|
11327 |
|
|
|
11328 |
/* Allowed values for ShellState.eTraceType |
|
|
11329 |
*/ |
|
|
11330 |
#define SHELL_TRACE_PLAIN 0 /* Show input SQL text */ |
|
|
11331 |
#define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */ |
|
|
11332 |
#define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */ |
|
|
11333 |
|
|
|
11334 |
/* Bits in the ShellState.flgProgress variable */ |
|
|
11335 |
#define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */ |
|
|
11336 |
#define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres |
|
|
11337 |
** callback limit is reached, and for each |
|
|
11338 |
** top-level SQL statement */ |
|
|
11339 |
#define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */ |
|
|
11340 |
|
|
|
11341 |
/* |
|
|
11342 |
** These are the allowed shellFlgs values |
|
|
11343 |
*/ |
|
|
11344 |
#define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */ |
|
|
11345 |
#define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */ |
|
|
11346 |
#define SHFLG_Backslash 0x00000004 /* The --backslash option is used */ |
|
|
11347 |
#define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */ |
|
|
11348 |
#define SHFLG_Newlines 0x00000010 /* .dump --newline flag */ |
|
|
11349 |
#define SHFLG_CountChanges 0x00000020 /* .changes setting */ |
|
|
11350 |
#define SHFLG_Echo 0x00000040 /* .echo or --echo setting */ |
|
|
11351 |
#define SHFLG_HeaderSet 0x00000080 /* .header has been used */ |
|
|
11352 |
#define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */ |
|
|
11353 |
#define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */ |
|
|
11354 |
|
|
|
11355 |
/* |
|
|
11356 |
** Macros for testing and setting shellFlgs |
|
|
11357 |
*/ |
|
|
11358 |
#define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) |
|
|
11359 |
#define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) |
|
|
11360 |
#define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) |
|
|
11361 |
|
|
|
11362 |
/* |
|
|
11363 |
** These are the allowed modes. |
|
|
11364 |
*/ |
|
|
11365 |
#define MODE_Line 0 /* One column per line. Blank line between records */ |
|
|
11366 |
#define MODE_Column 1 /* One record per line in neat columns */ |
|
|
11367 |
#define MODE_List 2 /* One record per line with a separator */ |
|
|
11368 |
#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
|
|
11369 |
#define MODE_Html 4 /* Generate an XHTML table */ |
|
|
11370 |
#define MODE_Insert 5 /* Generate SQL "insert" statements */ |
|
|
11371 |
#define MODE_Quote 6 /* Quote values as for SQL */ |
|
|
11372 |
#define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ |
|
|
11373 |
#define MODE_Csv 8 /* Quote strings, numbers are plain */ |
|
|
11374 |
#define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ |
|
|
11375 |
#define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
|
|
11376 |
#define MODE_Pretty 11 /* Pretty-print schemas */ |
|
|
11377 |
#define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */ |
|
|
11378 |
#define MODE_Json 13 /* Output JSON */ |
|
|
11379 |
#define MODE_Markdown 14 /* Markdown formatting */ |
|
|
11380 |
#define MODE_Table 15 /* MySQL-style table formatting */ |
|
|
11381 |
#define MODE_Box 16 /* Unicode box-drawing characters */ |
|
|
11382 |
|
|
|
11383 |
static const char *modeDescr[] = { |
|
|
11384 |
"line", |
|
|
11385 |
"column", |
|
|
11386 |
"list", |
|
|
11387 |
"semi", |
|
|
11388 |
"html", |
|
|
11389 |
"insert", |
|
|
11390 |
"quote", |
|
|
11391 |
"tcl", |
|
|
11392 |
"csv", |
|
|
11393 |
"explain", |
|
|
11394 |
"ascii", |
|
|
11395 |
"prettyprint", |
|
|
11396 |
"eqp", |
|
|
11397 |
"json", |
|
|
11398 |
"markdown", |
|
|
11399 |
"table", |
|
|
11400 |
"box" |
|
|
11401 |
}; |
|
|
11402 |
|
|
|
11403 |
/* |
|
|
11404 |
** These are the column/row/line separators used by the various |
|
|
11405 |
** import/export modes. |
|
|
11406 |
*/ |
|
|
11407 |
#define SEP_Column "|" |
|
|
11408 |
#define SEP_Row "\n" |
|
|
11409 |
#define SEP_Tab "\t" |
|
|
11410 |
#define SEP_Space " " |
|
|
11411 |
#define SEP_Comma "," |
|
|
11412 |
#define SEP_CrLf "\r\n" |
|
|
11413 |
#define SEP_Unit "\x1F" |
|
|
11414 |
#define SEP_Record "\x1E" |
|
|
11415 |
|
|
|
11416 |
/* |
|
|
11417 |
** A callback for the sqlite3_log() interface. |
|
|
11418 |
*/ |
|
|
11419 |
static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
|
|
11420 |
ShellState *p = (ShellState*)pArg; |
|
|
11421 |
if( p->pLog==0 ) return; |
|
|
11422 |
utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
|
|
11423 |
fflush(p->pLog); |
|
|
11424 |
} |
|
|
11425 |
|
|
|
11426 |
/* |
|
|
11427 |
** SQL function: shell_putsnl(X) |
|
|
11428 |
** |
|
|
11429 |
** Write the text X to the screen (or whatever output is being directed) |
|
|
11430 |
** adding a newline at the end, and then return X. |
|
|
11431 |
*/ |
|
|
11432 |
static void shellPutsFunc( |
|
|
11433 |
sqlite3_context *pCtx, |
|
|
11434 |
int nVal, |
|
|
11435 |
sqlite3_value **apVal |
|
|
11436 |
){ |
|
|
11437 |
ShellState *p = (ShellState*)sqlite3_user_data(pCtx); |
|
|
11438 |
(void)nVal; |
|
|
11439 |
utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0])); |
|
|
11440 |
sqlite3_result_value(pCtx, apVal[0]); |
|
|
11441 |
} |
|
|
11442 |
|
|
|
11443 |
/* |
|
|
11444 |
** SQL function: edit(VALUE) |
|
|
11445 |
** edit(VALUE,EDITOR) |
|
|
11446 |
** |
|
|
11447 |
** These steps: |
|
|
11448 |
** |
|
|
11449 |
** (1) Write VALUE into a temporary file. |
|
|
11450 |
** (2) Run program EDITOR on that temporary file. |
|
|
11451 |
** (3) Read the temporary file back and return its content as the result. |
|
|
11452 |
** (4) Delete the temporary file |
|
|
11453 |
** |
|
|
11454 |
** If the EDITOR argument is omitted, use the value in the VISUAL |
|
|
11455 |
** environment variable. If still there is no EDITOR, through an error. |
|
|
11456 |
** |
|
|
11457 |
** Also throw an error if the EDITOR program returns a non-zero exit code. |
|
|
11458 |
*/ |
|
|
11459 |
#ifndef SQLITE_NOHAVE_SYSTEM |
|
|
11460 |
static void editFunc( |
|
|
11461 |
sqlite3_context *context, |
|
|
11462 |
int argc, |
|
|
11463 |
sqlite3_value **argv |
|
|
11464 |
){ |
|
|
11465 |
const char *zEditor; |
|
|
11466 |
char *zTempFile = 0; |
|
|
11467 |
sqlite3 *db; |
|
|
11468 |
char *zCmd = 0; |
|
|
11469 |
int bBin; |
|
|
11470 |
int rc; |
|
|
11471 |
int hasCRNL = 0; |
|
|
11472 |
FILE *f = 0; |
|
|
11473 |
sqlite3_int64 sz; |
|
|
11474 |
sqlite3_int64 x; |
|
|
11475 |
unsigned char *p = 0; |
|
|
11476 |
|
|
|
11477 |
if( argc==2 ){ |
|
|
11478 |
zEditor = (const char*)sqlite3_value_text(argv[1]); |
|
|
11479 |
}else{ |
|
|
11480 |
zEditor = getenv("VISUAL"); |
|
|
11481 |
} |
|
|
11482 |
if( zEditor==0 ){ |
|
|
11483 |
sqlite3_result_error(context, "no editor for edit()", -1); |
|
|
11484 |
return; |
|
|
11485 |
} |
|
|
11486 |
if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ |
|
|
11487 |
sqlite3_result_error(context, "NULL input to edit()", -1); |
|
|
11488 |
return; |
|
|
11489 |
} |
|
|
11490 |
db = sqlite3_context_db_handle(context); |
|
|
11491 |
zTempFile = 0; |
|
|
11492 |
sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); |
|
|
11493 |
if( zTempFile==0 ){ |
|
|
11494 |
sqlite3_uint64 r = 0; |
|
|
11495 |
sqlite3_randomness(sizeof(r), &r); |
|
|
11496 |
zTempFile = sqlite3_mprintf("temp%llx", r); |
|
|
11497 |
if( zTempFile==0 ){ |
|
|
11498 |
sqlite3_result_error_nomem(context); |
|
|
11499 |
return; |
|
|
11500 |
} |
|
|
11501 |
} |
|
|
11502 |
bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; |
|
|
11503 |
/* When writing the file to be edited, do \n to \r\n conversions on systems |
|
|
11504 |
** that want \r\n line endings */ |
|
|
11505 |
f = fopen(zTempFile, bBin ? "wb" : "w"); |
|
|
11506 |
if( f==0 ){ |
|
|
11507 |
sqlite3_result_error(context, "edit() cannot open temp file", -1); |
|
|
11508 |
goto edit_func_end; |
|
|
11509 |
} |
|
|
11510 |
sz = sqlite3_value_bytes(argv[0]); |
|
|
11511 |
if( bBin ){ |
|
|
11512 |
x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f); |
|
|
11513 |
}else{ |
|
|
11514 |
const char *z = (const char*)sqlite3_value_text(argv[0]); |
|
|
11515 |
/* Remember whether or not the value originally contained \r\n */ |
|
|
11516 |
if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1; |
|
|
11517 |
x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f); |
|
|
11518 |
} |
|
|
11519 |
fclose(f); |
|
|
11520 |
f = 0; |
|
|
11521 |
if( x!=sz ){ |
|
|
11522 |
sqlite3_result_error(context, "edit() could not write the whole file", -1); |
|
|
11523 |
goto edit_func_end; |
|
|
11524 |
} |
|
|
11525 |
zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile); |
|
|
11526 |
if( zCmd==0 ){ |
|
|
11527 |
sqlite3_result_error_nomem(context); |
|
|
11528 |
goto edit_func_end; |
|
|
11529 |
} |
|
|
11530 |
rc = system(zCmd); |
|
|
11531 |
sqlite3_free(zCmd); |
|
|
11532 |
if( rc ){ |
|
|
11533 |
sqlite3_result_error(context, "EDITOR returned non-zero", -1); |
|
|
11534 |
goto edit_func_end; |
|
|
11535 |
} |
|
|
11536 |
f = fopen(zTempFile, "rb"); |
|
|
11537 |
if( f==0 ){ |
|
|
11538 |
sqlite3_result_error(context, |
|
|
11539 |
"edit() cannot reopen temp file after edit", -1); |
|
|
11540 |
goto edit_func_end; |
|
|
11541 |
} |
|
|
11542 |
fseek(f, 0, SEEK_END); |
|
|
11543 |
sz = ftell(f); |
|
|
11544 |
rewind(f); |
|
|
11545 |
p = sqlite3_malloc64( sz+1 ); |
|
|
11546 |
if( p==0 ){ |
|
|
11547 |
sqlite3_result_error_nomem(context); |
|
|
11548 |
goto edit_func_end; |
|
|
11549 |
} |
|
|
11550 |
x = fread(p, 1, (size_t)sz, f); |
|
|
11551 |
fclose(f); |
|
|
11552 |
f = 0; |
|
|
11553 |
if( x!=sz ){ |
|
|
11554 |
sqlite3_result_error(context, "could not read back the whole file", -1); |
|
|
11555 |
goto edit_func_end; |
|
|
11556 |
} |
|
|
11557 |
if( bBin ){ |
|
|
11558 |
sqlite3_result_blob64(context, p, sz, sqlite3_free); |
|
|
11559 |
}else{ |
|
|
11560 |
sqlite3_int64 i, j; |
|
|
11561 |
if( hasCRNL ){ |
|
|
11562 |
/* If the original contains \r\n then do no conversions back to \n */ |
|
|
11563 |
j = sz; |
|
|
11564 |
}else{ |
|
|
11565 |
/* If the file did not originally contain \r\n then convert any new |
|
|
11566 |
** \r\n back into \n */ |
|
|
11567 |
for(i=j=0; i |
|
|
11568 |
if( p[i]=='\r' && p[i+1]=='\n' ) i++; |
|
|
11569 |
p[j++] = p[i]; |
|
|
11570 |
} |
|
|
11571 |
sz = j; |
|
|
11572 |
p[sz] = 0; |
|
|
11573 |
} |
|
|
11574 |
sqlite3_result_text64(context, (const char*)p, sz, |
|
|
11575 |
sqlite3_free, SQLITE_UTF8); |
|
|
11576 |
} |
|
|
11577 |
p = 0; |
|
|
11578 |
|
|
|
11579 |
edit_func_end: |
|
|
11580 |
if( f ) fclose(f); |
|
|
11581 |
unlink(zTempFile); |
|
|
11582 |
sqlite3_free(zTempFile); |
|
|
11583 |
sqlite3_free(p); |
|
|
11584 |
} |
|
|
11585 |
#endif /* SQLITE_NOHAVE_SYSTEM */ |
|
|
11586 |
|
|
|
11587 |
/* |
|
|
11588 |
** Save or restore the current output mode |
|
|
11589 |
*/ |
|
|
11590 |
static void outputModePush(ShellState *p){ |
|
|
11591 |
p->modePrior = p->mode; |
|
|
11592 |
p->priorShFlgs = p->shellFlgs; |
|
|
11593 |
memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); |
|
|
11594 |
memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); |
|
|
11595 |
} |
|
|
11596 |
static void outputModePop(ShellState *p){ |
|
|
11597 |
p->mode = p->modePrior; |
|
|
11598 |
p->shellFlgs = p->priorShFlgs; |
|
|
11599 |
memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); |
|
|
11600 |
memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); |
|
|
11601 |
} |
|
|
11602 |
|
|
|
11603 |
/* |
|
|
11604 |
** Output the given string as a hex-encoded blob (eg. X'1234' ) |
|
|
11605 |
*/ |
|
|
11606 |
static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
|
|
11607 |
int i; |
|
|
11608 |
char *zBlob = (char *)pBlob; |
|
|
11609 |
raw_printf(out,"X'"); |
|
|
11610 |
for(i=0; i |
|
|
11611 |
raw_printf(out,"'"); |
|
|
11612 |
} |
|
|
11613 |
|
|
|
11614 |
/* |
|
|
11615 |
** Find a string that is not found anywhere in z[]. Return a pointer |
|
|
11616 |
** to that string. |
|
|
11617 |
** |
|
|
11618 |
** Try to use zA and zB first. If both of those are already found in z[] |
|
|
11619 |
** then make up some string and store it in the buffer zBuf. |
|
|
11620 |
*/ |
|
|
11621 |
static const char *unused_string( |
|
|
11622 |
const char *z, /* Result must not appear anywhere in z */ |
|
|
11623 |
const char *zA, const char *zB, /* Try these first */ |
|
|
11624 |
char *zBuf /* Space to store a generated string */ |
|
|
11625 |
){ |
|
|
11626 |
unsigned i = 0; |
|
|
11627 |
if( strstr(z, zA)==0 ) return zA; |
|
|
11628 |
if( strstr(z, zB)==0 ) return zB; |
|
|
11629 |
do{ |
|
|
11630 |
sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); |
|
|
11631 |
}while( strstr(z,zBuf)!=0 ); |
|
|
11632 |
return zBuf; |
|
|
11633 |
} |
|
|
11634 |
|
|
|
11635 |
/* |
|
|
11636 |
** Output the given string as a quoted string using SQL quoting conventions. |
|
|
11637 |
** |
|
|
11638 |
** See also: output_quoted_escaped_string() |
|
|
11639 |
*/ |
|
|
11640 |
static void output_quoted_string(FILE *out, const char *z){ |
|
|
11641 |
int i; |
|
|
11642 |
char c; |
|
|
11643 |
setBinaryMode(out, 1); |
|
|
11644 |
for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
|
|
11645 |
if( c==0 ){ |
|
|
11646 |
utf8_printf(out,"'%s'",z); |
|
|
11647 |
}else{ |
|
|
11648 |
raw_printf(out, "'"); |
|
|
11649 |
while( *z ){ |
|
|
11650 |
for(i=0; (c = z[i])!=0 && c!='\''; i++){} |
|
|
11651 |
if( c=='\'' ) i++; |
|
|
11652 |
if( i ){ |
|
|
11653 |
utf8_printf(out, "%.*s", i, z); |
|
|
11654 |
z += i; |
|
|
11655 |
} |
|
|
11656 |
if( c=='\'' ){ |
|
|
11657 |
raw_printf(out, "'"); |
|
|
11658 |
continue; |
|
|
11659 |
} |
|
|
11660 |
if( c==0 ){ |
|
|
11661 |
break; |
|
|
11662 |
} |
|
|
11663 |
z++; |
|
|
11664 |
} |
|
|
11665 |
raw_printf(out, "'"); |
|
|
11666 |
} |
|
|
11667 |
setTextMode(out, 1); |
|
|
11668 |
} |
|
|
11669 |
|
|
|
11670 |
/* |
|
|
11671 |
** Output the given string as a quoted string using SQL quoting conventions. |
|
|
11672 |
** Additionallly , escape the "\n" and "\r" characters so that they do not |
|
|
11673 |
** get corrupted by end-of-line translation facilities in some operating |
|
|
11674 |
** systems. |
|
|
11675 |
** |
|
|
11676 |
** This is like output_quoted_string() but with the addition of the \r\n |
|
|
11677 |
** escape mechanism. |
|
|
11678 |
*/ |
|
|
11679 |
static void output_quoted_escaped_string(FILE *out, const char *z){ |
|
|
11680 |
int i; |
|
|
11681 |
char c; |
|
|
11682 |
setBinaryMode(out, 1); |
|
|
11683 |
for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){} |
|
|
11684 |
if( c==0 ){ |
|
|
11685 |
utf8_printf(out,"'%s'",z); |
|
|
11686 |
}else{ |
|
|
11687 |
const char *zNL = 0; |
|
|
11688 |
const char *zCR = 0; |
|
|
11689 |
int nNL = 0; |
|
|
11690 |
int nCR = 0; |
|
|
11691 |
char zBuf1[20], zBuf2[20]; |
|
|
11692 |
for(i=0; z[i]; i++){ |
|
|
11693 |
if( z[i]=='\n' ) nNL++; |
|
|
11694 |
if( z[i]=='\r' ) nCR++; |
|
|
11695 |
} |
|
|
11696 |
if( nNL ){ |
|
|
11697 |
raw_printf(out, "replace("); |
|
|
11698 |
zNL = unused_string(z, "\\n", "\\012", zBuf1); |
|
|
11699 |
} |
|
|
11700 |
if( nCR ){ |
|
|
11701 |
raw_printf(out, "replace("); |
|
|
11702 |
zCR = unused_string(z, "\\r", "\\015", zBuf2); |
|
|
11703 |
} |
|
|
11704 |
raw_printf(out, "'"); |
|
|
11705 |
while( *z ){ |
|
|
11706 |
for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){} |
|
|
11707 |
if( c=='\'' ) i++; |
|
|
11708 |
if( i ){ |
|
|
11709 |
utf8_printf(out, "%.*s", i, z); |
|
|
11710 |
z += i; |
|
|
11711 |
} |
|
|
11712 |
if( c=='\'' ){ |
|
|
11713 |
raw_printf(out, "'"); |
|
|
11714 |
continue; |
|
|
11715 |
} |
|
|
11716 |
if( c==0 ){ |
|
|
11717 |
break; |
|
|
11718 |
} |
|
|
11719 |
z++; |
|
|
11720 |
if( c=='\n' ){ |
|
|
11721 |
raw_printf(out, "%s", zNL); |
|
|
11722 |
continue; |
|
|
11723 |
} |
|
|
11724 |
raw_printf(out, "%s", zCR); |
|
|
11725 |
} |
|
|
11726 |
raw_printf(out, "'"); |
|
|
11727 |
if( nCR ){ |
|
|
11728 |
raw_printf(out, ",'%s',char(13))", zCR); |
|
|
11729 |
} |
|
|
11730 |
if( nNL ){ |
|
|
11731 |
raw_printf(out, ",'%s',char(10))", zNL); |
|
|
11732 |
} |
|
|
11733 |
} |
|
|
11734 |
setTextMode(out, 1); |
|
|
11735 |
} |
|
|
11736 |
|
|
|
11737 |
/* |
|
|
11738 |
** Output the given string as a quoted according to C or TCL quoting rules. |
|
|
11739 |
*/ |
|
|
11740 |
static void output_c_string(FILE *out, const char *z){ |
|
|
11741 |
unsigned int c; |
|
|
11742 |
fputc('"', out); |
|
|
11743 |
while( (c = *(z++))!=0 ){ |
|
|
11744 |
if( c=='\\' ){ |
|
|
11745 |
fputc(c, out); |
|
|
11746 |
fputc(c, out); |
|
|
11747 |
}else if( c=='"' ){ |
|
|
11748 |
fputc('\\', out); |
|
|
11749 |
fputc('"', out); |
|
|
11750 |
}else if( c=='\t' ){ |
|
|
11751 |
fputc('\\', out); |
|
|
11752 |
fputc('t', out); |
|
|
11753 |
}else if( c=='\n' ){ |
|
|
11754 |
fputc('\\', out); |
|
|
11755 |
fputc('n', out); |
|
|
11756 |
}else if( c=='\r' ){ |
|
|
11757 |
fputc('\\', out); |
|
|
11758 |
fputc('r', out); |
|
|
11759 |
}else if( !isprint(c&0xff) ){ |
|
|
11760 |
raw_printf(out, "\\%03o", c&0xff); |
|
|
11761 |
}else{ |
|
|
11762 |
fputc(c, out); |
|
|
11763 |
} |
|
|
11764 |
} |
|
|
11765 |
fputc('"', out); |
|
|
11766 |
} |
|
|
11767 |
|
|
|
11768 |
/* |
|
|
11769 |
** Output the given string as a quoted according to JSON quoting rules. |
|
|
11770 |
*/ |
|
|
11771 |
static void output_json_string(FILE *out, const char *z, int n){ |
|
|
11772 |
unsigned int c; |
|
|
11773 |
if( n<0 ) n = (int)strlen(z); |
|
|
11774 |
fputc('"', out); |
|
|
11775 |
while( n-- ){ |
|
|
11776 |
c = *(z++); |
|
|
11777 |
if( c=='\\' || c=='"' ){ |
|
|
11778 |
fputc('\\', out); |
|
|
11779 |
fputc(c, out); |
|
|
11780 |
}else if( c<=0x1f ){ |
|
|
11781 |
fputc('\\', out); |
|
|
11782 |
if( c=='\b' ){ |
|
|
11783 |
fputc('b', out); |
|
|
11784 |
}else if( c=='\f' ){ |
|
|
11785 |
fputc('f', out); |
|
|
11786 |
}else if( c=='\n' ){ |
|
|
11787 |
fputc('n', out); |
|
|
11788 |
}else if( c=='\r' ){ |
|
|
11789 |
fputc('r', out); |
|
|
11790 |
}else if( c=='\t' ){ |
|
|
11791 |
fputc('t', out); |
|
|
11792 |
}else{ |
|
|
11793 |
raw_printf(out, "u%04x",c); |
|
|
11794 |
} |
|
|
11795 |
}else{ |
|
|
11796 |
fputc(c, out); |
|
|
11797 |
} |
|
|
11798 |
} |
|
|
11799 |
fputc('"', out); |
|
|
11800 |
} |
|
|
11801 |
|
|
|
11802 |
/* |
|
|
11803 |
** Output the given string with characters that are special to |
|
|
11804 |
** HTML escaped. |
|
|
11805 |
*/ |
|
|
11806 |
static void output_html_string(FILE *out, const char *z){ |
|
|
11807 |
int i; |
|
|
11808 |
if( z==0 ) z = ""; |
|
|
11809 |
while( *z ){ |
|
|
11810 |
for(i=0; z[i] |
|
|
11811 |
&& z[i]!='<' |
|
|
11812 |
&& z[i]!='&' |
|
|
11813 |
&& z[i]!='>' |
|
|
11814 |
&& z[i]!='\"' |
|
|
11815 |
&& z[i]!='\''; |
|
|
11816 |
i++){} |
|
|
11817 |
if( i>0 ){ |
|
|
11818 |
utf8_printf(out,"%.*s",i,z); |
|
|
11819 |
} |
|
|
11820 |
if( z[i]=='<' ){ |
|
|
11821 |
raw_printf(out,"<"); |
|
|
11822 |
}else 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{ |
|
|
11831 |
break; |
|
|
11832 |
} |
|
|
11833 |
z += i + 1; |
|
|
11834 |
} |
|
|
11835 |
} |
|
|
11836 |
|
|
|
11837 |
/* |
|
|
11838 |
** If a field contains any character identified by a 1 in the following |
|
|
11839 |
** array, then the string must be quoted for CSV. |
|
|
11840 |
*/ |
|
|
11841 |
static const char needCsvQuote[] = { |
|
|
11842 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
|
11843 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
|
11844 |
1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
11845 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
11846 |
0, 0, 0, 0, 0, 0, 0, 0, 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, 1, |
|
|
11850 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
|
11851 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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 |
}; |
|
|
11859 |
|
|
|
11860 |
/* |
|
|
11861 |
** Output a single term of CSV. Actually, p->colSeparator is used for |
|
|
11862 |
** the separator, which may or may not be a comma. p->nullValue is |
|
|
11863 |
** the null value. Strings are quoted if necessary. The separator |
|
|
11864 |
** is only issued if bSep is true. |
|
|
11865 |
*/ |
|
|
11866 |
static void output_csv(ShellState *p, const char *z, int bSep){ |
|
|
11867 |
FILE *out = p->out; |
|
|
11868 |
if( z==0 ){ |
|
|
11869 |
utf8_printf(out,"%s",p->nullValue); |
|
|
11870 |
}else{ |
|
|
11871 |
int i; |
|
|
11872 |
int nSep = strlen30(p->colSeparator); |
|
|
11873 |
for(i=0; z[i]; i++){ |
|
|
11874 |
if( needCsvQuote[((unsigned char*)z)[i]] |
|
|
11875 |
|| (z[i]==p->colSeparator[0] && |
|
|
11876 |
(nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
|
|
11877 |
i = 0; |
|
|
11878 |
break; |
|
|
11879 |
} |
|
|
11880 |
} |
|
|
11881 |
if( i==0 ){ |
|
|
11882 |
char *zQuoted = sqlite3_mprintf("\"%w\"", z); |
|
|
11883 |
utf8_printf(out, "%s", zQuoted); |
|
|
11884 |
sqlite3_free(zQuoted); |
|
|
11885 |
}else{ |
|
|
11886 |
utf8_printf(out, "%s", z); |
|
|
11887 |
} |
|
|
11888 |
} |
|
|
11889 |
if( bSep ){ |
|
|
11890 |
utf8_printf(p->out, "%s", p->colSeparator); |
|
|
11891 |
} |
|
|
11892 |
} |
|
|
11893 |
|
|
|
11894 |
/* |
|
|
11895 |
** This routine runs when the user presses Ctrl-C |
|
|
11896 |
*/ |
|
|
11897 |
static void interrupt_handler(int NotUsed){ |
|
|
11898 |
UNUSED_PARAMETER(NotUsed); |
|
|
11899 |
seenInterrupt++; |
|
|
11900 |
if( seenInterrupt>2 ) exit(1); |
|
|
11901 |
if( globalDb ) sqlite3_interrupt(globalDb); |
|
|
11902 |
} |
|
|
11903 |
|
|
|
11904 |
#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) |
|
|
11905 |
/* |
|
|
11906 |
** This routine runs for console events (e.g. Ctrl-C) on Win32 |
|
|
11907 |
*/ |
|
|
11908 |
static BOOL WINAPI ConsoleCtrlHandler( |
|
|
11909 |
DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */ |
|
|
11910 |
){ |
|
|
11911 |
if( dwCtrlType==CTRL_C_EVENT ){ |
|
|
11912 |
interrupt_handler(0); |
|
|
11913 |
return TRUE; |
|
|
11914 |
} |
|
|
11915 |
return FALSE; |
|
|
11916 |
} |
|
|
11917 |
#endif |
|
|
11918 |
|
|
|
11919 |
#ifndef SQLITE_OMIT_AUTHORIZATION |
|
|
11920 |
/* |
|
|
11921 |
** When the ".auth ON" is set, the following authorizer callback is |
|
|
11922 |
** invoked. It always returns SQLITE_OK. |
|
|
11923 |
*/ |
|
|
11924 |
static int shellAuth( |
|
|
11925 |
void *pClientData, |
|
|
11926 |
int op, |
|
|
11927 |
const char *zA1, |
|
|
11928 |
const char *zA2, |
|
|
11929 |
const char *zA3, |
|
|
11930 |
const char *zA4 |
|
|
11931 |
){ |
|
|
11932 |
ShellState *p = (ShellState*)pClientData; |
|
|
11933 |
static const char *azAction[] = { 0, |
|
|
11934 |
"CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
|
|
11935 |
"CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
|
|
11936 |
"CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
|
|
11937 |
"DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
|
|
11938 |
"DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
|
|
11939 |
"DROP_TRIGGER", "DROP_VIEW", "INSERT", |
|
|
11940 |
"PRAGMA", "READ", "SELECT", |
|
|
11941 |
"TRANSACTION", "UPDATE", "ATTACH", |
|
|
11942 |
"DETACH", "ALTER_TABLE", "REINDEX", |
|
|
11943 |
"ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
|
|
11944 |
"FUNCTION", "SAVEPOINT", "RECURSIVE" |
|
|
11945 |
}; |
|
|
11946 |
int i; |
|
|
11947 |
const char *az[4]; |
|
|
11948 |
az[0] = zA1; |
|
|
11949 |
az[1] = zA2; |
|
|
11950 |
az[2] = zA3; |
|
|
11951 |
az[3] = zA4; |
|
|
11952 |
utf8_printf(p->out, "authorizer: %s", azAction[op]); |
|
|
11953 |
for(i=0; i<4; i++){ |
|
|
11954 |
raw_printf(p->out, " "); |
|
|
11955 |
if( az[i] ){ |
|
|
11956 |
output_c_string(p->out, az[i]); |
|
|
11957 |
}else{ |
|
|
11958 |
raw_printf(p->out, "NULL"); |
|
|
11959 |
} |
|
|
11960 |
} |
|
|
11961 |
raw_printf(p->out, "\n"); |
|
|
11962 |
return SQLITE_OK; |
|
|
11963 |
} |
|
|
11964 |
#endif |
|
|
11965 |
|
|
|
11966 |
/* |
|
|
11967 |
** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. |
|
|
11968 |
** |
|
|
11969 |
** This routine converts some CREATE TABLE statements for shadow tables |
|
|
11970 |
** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. |
|
|
11971 |
*/ |
|
|
11972 |
static void printSchemaLine(FILE *out, const char *z, const char *zTail){ |
|
|
11973 |
if( z==0 ) return; |
|
|
11974 |
if( zTail==0 ) return; |
|
|
11975 |
if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){ |
|
|
11976 |
utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail); |
|
|
11977 |
}else{ |
|
|
11978 |
utf8_printf(out, "%s%s", z, zTail); |
|
|
11979 |
} |
|
|
11980 |
} |
|
|
11981 |
static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){ |
|
|
11982 |
char c = z[n]; |
|
|
11983 |
z[n] = 0; |
|
|
11984 |
printSchemaLine(out, z, zTail); |
|
|
11985 |
z[n] = c; |
|
|
11986 |
} |
|
|
11987 |
|
|
|
11988 |
/* |
|
|
11989 |
** Return true if string z[] has nothing but whitespace and comments to the |
|
|
11990 |
** end of the first line. |
|
|
11991 |
*/ |
|
|
11992 |
static int wsToEol(const char *z){ |
|
|
11993 |
int i; |
|
|
11994 |
for(i=0; z[i]; i++){ |
|
|
11995 |
if( z[i]=='\n' ) return 1; |
|
|
11996 |
if( IsSpace(z[i]) ) continue; |
|
|
11997 |
if( z[i]=='-' && z[i+1]=='-' ) return 1; |
|
|
11998 |
return 0; |
|
|
11999 |
} |
|
|
12000 |
return 1; |
|
|
12001 |
} |
|
|
12002 |
|
|
|
12003 |
/* |
|
|
12004 |
** Add a new entry to the EXPLAIN QUERY PLAN data |
|
|
12005 |
*/ |
|
|
12006 |
static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){ |
|
|
12007 |
EQPGraphRow *pNew; |
|
|
12008 |
int nText = strlen30(zText); |
|
|
12009 |
if( p->autoEQPtest ){ |
|
|
12010 |
utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); |
|
|
12011 |
} |
|
|
12012 |
pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); |
|
|
12013 |
if( pNew==0 ) shell_out_of_memory(); |
|
|
12014 |
pNew->iEqpId = iEqpId; |
|
|
12015 |
pNew->iParentId = p2; |
|
|
12016 |
memcpy(pNew->zText, zText, nText+1); |
|
|
12017 |
pNew->pNext = 0; |
|
|
12018 |
if( p->sGraph.pLast ){ |
|
|
12019 |
p->sGraph.pLast->pNext = pNew; |
|
|
12020 |
}else{ |
|
|
12021 |
p->sGraph.pRow = pNew; |
|
|
12022 |
} |
|
|
12023 |
p->sGraph.pLast = pNew; |
|
|
12024 |
} |
|
|
12025 |
|
|
|
12026 |
/* |
|
|
12027 |
** Free and reset the EXPLAIN QUERY PLAN data that has been collected |
|
|
12028 |
** in p->sGraph. |
|
|
12029 |
*/ |
|
|
12030 |
static void eqp_reset(ShellState *p){ |
|
|
12031 |
EQPGraphRow *pRow, *pNext; |
|
|
12032 |
for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ |
|
|
12033 |
pNext = pRow->pNext; |
|
|
12034 |
sqlite3_free(pRow); |
|
|
12035 |
} |
|
|
12036 |
memset(&p->sGraph, 0, sizeof(p->sGraph)); |
|
|
12037 |
} |
|
|
12038 |
|
|
|
12039 |
/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after |
|
|
12040 |
** pOld, or return the first such line if pOld is NULL |
|
|
12041 |
*/ |
|
|
12042 |
static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){ |
|
|
12043 |
EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; |
|
|
12044 |
while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; |
|
|
12045 |
return pRow; |
|
|
12046 |
} |
|
|
12047 |
|
|
|
12048 |
/* Render a single level of the graph that has iEqpId as its parent. Called |
|
|
12049 |
** recursively to render sublevels. |
|
|
12050 |
*/ |
|
|
12051 |
static void eqp_render_level(ShellState *p, int iEqpId){ |
|
|
12052 |
EQPGraphRow *pRow, *pNext; |
|
|
12053 |
int n = strlen30(p->sGraph.zPrefix); |
|
|
12054 |
char *z; |
|
|
12055 |
for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){ |
|
|
12056 |
pNext = eqp_next_row(p, iEqpId, pRow); |
|
|
12057 |
z = pRow->zText; |
|
|
12058 |
utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, |
|
|
12059 |
pNext ? "|--" : "`--", z); |
|
|
12060 |
if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){ |
|
|
12061 |
memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); |
|
|
12062 |
eqp_render_level(p, pRow->iEqpId); |
|
|
12063 |
p->sGraph.zPrefix[n] = 0; |
|
|
12064 |
} |
|
|
12065 |
} |
|
|
12066 |
} |
|
|
12067 |
|
|
|
12068 |
/* |
|
|
12069 |
** Display and reset the EXPLAIN QUERY PLAN data |
|
|
12070 |
*/ |
|
|
12071 |
static void eqp_render(ShellState *p){ |
|
|
12072 |
EQPGraphRow *pRow = p->sGraph.pRow; |
|
|
12073 |
if( pRow ){ |
|
|
12074 |
if( pRow->zText[0]=='-' ){ |
|
|
12075 |
if( pRow->pNext==0 ){ |
|
|
12076 |
eqp_reset(p); |
|
|
12077 |
return; |
|
|
12078 |
} |
|
|
12079 |
utf8_printf(p->out, "%s\n", pRow->zText+3); |
|
|
12080 |
p->sGraph.pRow = pRow->pNext; |
|
|
12081 |
sqlite3_free(pRow); |
|
|
12082 |
}else{ |
|
|
12083 |
utf8_printf(p->out, "QUERY PLAN\n"); |
|
|
12084 |
} |
|
|
12085 |
p->sGraph.zPrefix[0] = 0; |
|
|
12086 |
eqp_render_level(p, 0); |
|
|
12087 |
eqp_reset(p); |
|
|
12088 |
} |
|
|
12089 |
} |
|
|
12090 |
|
|
|
12091 |
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
|
|
12092 |
/* |
|
|
12093 |
** Progress handler callback. |
|
|
12094 |
*/ |
|
|
12095 |
static int progress_handler(void *pClientData) { |
|
|
12096 |
ShellState *p = (ShellState*)pClientData; |
|
|
12097 |
p->nProgress++; |
|
|
12098 |
if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ |
|
|
12099 |
raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress); |
|
|
12100 |
if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; |
|
|
12101 |
if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; |
|
|
12102 |
return 1; |
|
|
12103 |
} |
|
|
12104 |
if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ |
|
|
12105 |
raw_printf(p->out, "Progress %u\n", p->nProgress); |
|
|
12106 |
} |
|
|
12107 |
return 0; |
|
|
12108 |
} |
|
|
12109 |
#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ |
|
|
12110 |
|
|
|
12111 |
/* |
|
|
12112 |
** Print N dashes |
|
|
12113 |
*/ |
|
|
12114 |
static void print_dashes(FILE *out, int N){ |
|
|
12115 |
const char zDash[] = "--------------------------------------------------"; |
|
|
12116 |
const int nDash = sizeof(zDash) - 1; |
|
|
12117 |
while( N>nDash ){ |
|
|
12118 |
fputs(zDash, out); |
|
|
12119 |
N -= nDash; |
|
|
12120 |
} |
|
|
12121 |
raw_printf(out, "%.*s", N, zDash); |
|
|
12122 |
} |
|
|
12123 |
|
|
|
12124 |
/* |
|
|
12125 |
** Print a markdown or table-style row separator using ascii-art |
|
|
12126 |
*/ |
|
|
12127 |
static void print_row_separator( |
|
|
12128 |
ShellState *p, |
|
|
12129 |
int nArg, |
|
|
12130 |
const char *zSep |
|
|
12131 |
){ |
|
|
12132 |
int i; |
|
|
12133 |
if( nArg>0 ){ |
|
|
12134 |
fputs(zSep, p->out); |
|
|
12135 |
print_dashes(p->out, p->actualWidth[0]+2); |
|
|
12136 |
for(i=1; i |
|
|
12137 |
fputs(zSep, p->out); |
|
|
12138 |
print_dashes(p->out, p->actualWidth[i]+2); |
|
|
12139 |
} |
|
|
12140 |
fputs(zSep, p->out); |
|
|
12141 |
} |
|
|
12142 |
fputs("\n", p->out); |
|
|
12143 |
} |
|
|
12144 |
|
|
|
12145 |
/* |
|
|
12146 |
** This is the callback routine that the shell |
|
|
12147 |
** invokes for each row of a query result. |
|
|
12148 |
*/ |
|
|
12149 |
static int shell_callback( |
|
|
12150 |
void *pArg, |
|
|
12151 |
int nArg, /* Number of result columns */ |
|
|
12152 |
char **azArg, /* Text of each result column */ |
|
|
12153 |
char **azCol, /* Column names */ |
|
|
12154 |
int *aiType /* Column types. Might be NULL */ |
|
|
12155 |
){ |
|
|
12156 |
int i; |
|
|
12157 |
ShellState *p = (ShellState*)pArg; |
|
|
12158 |
|
|
|
12159 |
if( azArg==0 ) return 0; |
|
|
12160 |
switch( p->cMode ){ |
|
|
12161 |
case MODE_Line: { |
|
|
12162 |
int w = 5; |
|
|
12163 |
if( azArg==0 ) break; |
|
|
12164 |
for(i=0; i |
|
|
12165 |
int len = strlen30(azCol[i] ? azCol[i] : ""); |
|
|
12166 |
if( len>w ) w = len; |
|
|
12167 |
} |
|
|
12168 |
if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
|
|
12169 |
for(i=0; i |
|
|
12170 |
utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
|
|
12171 |
azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
|
|
12172 |
} |
|
|
12173 |
break; |
|
|
12174 |
} |
|
|
12175 |
case MODE_Explain: { |
|
|
12176 |
static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
|
|
12177 |
if( nArg>ArraySize(aExplainWidth) ){ |
|
|
12178 |
nArg = ArraySize(aExplainWidth); |
|
|
12179 |
} |
|
|
12180 |
if( p->cnt++==0 ){ |
|
|
12181 |
for(i=0; i |
|
|
12182 |
int w = aExplainWidth[i]; |
|
|
12183 |
utf8_width_print(p->out, w, azCol[i]); |
|
|
12184 |
fputs(i==nArg-1 ? "\n" : " ", p->out); |
|
|
12185 |
} |
|
|
12186 |
for(i=0; i |
|
|
12187 |
int w = aExplainWidth[i]; |
|
|
12188 |
print_dashes(p->out, w); |
|
|
12189 |
fputs(i==nArg-1 ? "\n" : " ", p->out); |
|
|
12190 |
} |
|
|
12191 |
} |
|
|
12192 |
if( azArg==0 ) break; |
|
|
12193 |
for(i=0; i |
|
|
12194 |
int w = aExplainWidth[i]; |
|
|
12195 |
if( i==nArg-1 ) w = 0; |
|
|
12196 |
if( azArg[i] && strlenChar(azArg[i])>w ){ |
|
|
12197 |
w = strlenChar(azArg[i]); |
|
|
12198 |
} |
|
|
12199 |
if( i==1 && p->aiIndent && p->pStmt ){ |
|
|
12200 |
if( p->iIndentnIndent ){ |
|
|
12201 |
utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
|
|
12202 |
} |
|
|
12203 |
p->iIndent++; |
|
|
12204 |
} |
|
|
12205 |
utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); |
|
|
12206 |
fputs(i==nArg-1 ? "\n" : " ", p->out); |
|
|
12207 |
} |
|
|
12208 |
break; |
|
|
12209 |
} |
|
|
12210 |
case MODE_Semi: { /* .schema and .fullschema output */ |
|
|
12211 |
printSchemaLine(p->out, azArg[0], ";\n"); |
|
|
12212 |
break; |
|
|
12213 |
} |
|
|
12214 |
case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
|
|
12215 |
char *z; |
|
|
12216 |
int j; |
|
|
12217 |
int nParen = 0; |
|
|
12218 |
char cEnd = 0; |
|
|
12219 |
char c; |
|
|
12220 |
int nLine = 0; |
|
|
12221 |
assert( nArg==1 ); |
|
|
12222 |
if( azArg[0]==0 ) break; |
|
|
12223 |
if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
|
|
12224 |
|| sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
|
|
12225 |
){ |
|
|
12226 |
utf8_printf(p->out, "%s;\n", azArg[0]); |
|
|
12227 |
break; |
|
|
12228 |
} |
|
|
12229 |
z = sqlite3_mprintf("%s", azArg[0]); |
|
|
12230 |
j = 0; |
|
|
12231 |
for(i=0; IsSpace(z[i]); i++){} |
|
|
12232 |
for(; (c = z[i])!=0; i++){ |
|
|
12233 |
if( IsSpace(c) ){ |
|
|
12234 |
if( z[j-1]=='\r' ) z[j-1] = '\n'; |
|
|
12235 |
if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
|
|
12236 |
}else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
|
|
12237 |
j--; |
|
|
12238 |
} |
|
|
12239 |
z[j++] = c; |
|
|
12240 |
} |
|
|
12241 |
while( j>0 && IsSpace(z[j-1]) ){ j--; } |
|
|
12242 |
z[j] = 0; |
|
|
12243 |
if( strlen30(z)>=79 ){ |
|
|
12244 |
for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */ |
|
|
12245 |
if( c==cEnd ){ |
|
|
12246 |
cEnd = 0; |
|
|
12247 |
}else if( c=='"' || c=='\'' || c=='`' ){ |
|
|
12248 |
cEnd = c; |
|
|
12249 |
}else if( c=='[' ){ |
|
|
12250 |
cEnd = ']'; |
|
|
12251 |
}else if( c=='-' && z[i+1]=='-' ){ |
|
|
12252 |
cEnd = '\n'; |
|
|
12253 |
}else if( c=='(' ){ |
|
|
12254 |
nParen++; |
|
|
12255 |
}else if( c==')' ){ |
|
|
12256 |
nParen--; |
|
|
12257 |
if( nLine>0 && nParen==0 && j>0 ){ |
|
|
12258 |
printSchemaLineN(p->out, z, j, "\n"); |
|
|
12259 |
j = 0; |
|
|
12260 |
} |
|
|
12261 |
} |
|
|
12262 |
z[j++] = c; |
|
|
12263 |
if( nParen==1 && cEnd==0 |
|
|
12264 |
&& (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) |
|
|
12265 |
){ |
|
|
12266 |
if( c=='\n' ) j--; |
|
|
12267 |
printSchemaLineN(p->out, z, j, "\n "); |
|
|
12268 |
j = 0; |
|
|
12269 |
nLine++; |
|
|
12270 |
while( IsSpace(z[i+1]) ){ i++; } |
|
|
12271 |
} |
|
|
12272 |
} |
|
|
12273 |
z[j] = 0; |
|
|
12274 |
} |
|
|
12275 |
printSchemaLine(p->out, z, ";\n"); |
|
|
12276 |
sqlite3_free(z); |
|
|
12277 |
break; |
|
|
12278 |
} |
|
|
12279 |
case MODE_List: { |
|
|
12280 |
if( p->cnt++==0 && p->showHeader ){ |
|
|
12281 |
for(i=0; i |
|
|
12282 |
utf8_printf(p->out,"%s%s",azCol[i], |
|
|
12283 |
i==nArg-1 ? p->rowSeparator : p->colSeparator); |
|
|
12284 |
} |
|
|
12285 |
} |
|
|
12286 |
if( azArg==0 ) break; |
|
|
12287 |
for(i=0; i |
|
|
12288 |
char *z = azArg[i]; |
|
|
12289 |
if( z==0 ) z = p->nullValue; |
|
|
12290 |
utf8_printf(p->out, "%s", z); |
|
|
12291 |
if( i |
|
|
12292 |
utf8_printf(p->out, "%s", p->colSeparator); |
|
|
12293 |
}else{ |
|
|
12294 |
utf8_printf(p->out, "%s", p->rowSeparator); |
|
|
12295 |
} |
|
|
12296 |
} |
|
|
12297 |
break; |
|
|
12298 |
} |
|
|
12299 |
case MODE_Html: { |
|
|
12300 |
if( p->cnt++==0 && p->showHeader ){ |
|
|
12301 |
raw_printf(p->out,"");
|
|
12302 |
for(i=0; i |
|
|
12303 |
raw_printf(p->out,""); | |
|
|
12304 |
output_html_string(p->out, azCol[i]); |
|
|
12305 |
raw_printf(p->out,"\n"); |
|
|
12306 |
} |
|
|
12307 |
raw_printf(p->out," | \n"); |
|
|
12308 |
} |
|
|
12309 |
if( azArg==0 ) break; |
|
|
12310 |
raw_printf(p->out,"");
|
|
12311 |
for(i=0; i |
|
|
12312 |
raw_printf(p->out,""); |
|
|
|
12313 |
output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
|
|
12314 |
raw_printf(p->out," | \n");
|
|
12315 |
} |
|
|
12316 |
raw_printf(p->out," | \n"); |
|
|
12317 |
break; |
|
|
12318 |
} |
|
|
12319 |
case MODE_Tcl: { |
|
|
12320 |
if( p->cnt++==0 && p->showHeader ){ |
|
|
12321 |
for(i=0; i |
|
|
12322 |
output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
|
|
12323 |
if(iout, "%s", p->colSeparator); |
|
|
12324 |
} |
|
|
12325 |
utf8_printf(p->out, "%s", p->rowSeparator); |
|
|
12326 |
} |
|
|
12327 |
if( azArg==0 ) break; |
|
|
12328 |
for(i=0; i |
|
|
12329 |
output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
|
|
12330 |
if(iout, "%s", p->colSeparator); |
|
|
12331 |
} |
|
|
12332 |
utf8_printf(p->out, "%s", p->rowSeparator); |
|
|
12333 |
break; |
|
|
12334 |
} |
|
|
12335 |
case MODE_Csv: { |
|
|
12336 |
setBinaryMode(p->out, 1); |
|
|
12337 |
if( p->cnt++==0 && p->showHeader ){ |
|
|
12338 |
for(i=0; i |
|
|
12339 |
output_csv(p, azCol[i] ? azCol[i] : "", i |
|
|
12340 |
} |
|
|
12341 |
utf8_printf(p->out, "%s", p->rowSeparator); |
|
|
12342 |
} |
|
|
12343 |
if( nArg>0 ){ |
|
|
12344 |
for(i=0; i |
|
|
12345 |
output_csv(p, azArg[i], i |
|
|
12346 |
} |
|
|
12347 |
utf8_printf(p->out, "%s", p->rowSeparator); |
|
|
12348 |
} |
|
|
12349 |
setTextMode(p->out, 1); |
|
|
12350 |
break; |
|
|
12351 |
} |
|
|
12352 |
case MODE_Insert: { |
|
|
12353 |
if( azArg==0 ) break; |
|
|
12354 |
utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
|
|
12355 |
if( p->showHeader ){ |
|
|
12356 |
raw_printf(p->out,"("); |
|
|
12357 |
for(i=0; i |
|
|
12358 |
if( i>0 ) raw_printf(p->out, ","); |
|
|
12359 |
if( quoteChar(azCol[i]) ){ |
|
|
12360 |
char *z = sqlite3_mprintf("\"%w\"", azCol[i]); |
|
|
12361 |
utf8_printf(p->out, "%s", z); |
|
|
12362 |
sqlite3_free(z); |
|
|
12363 |
}else{ |
|
|
12364 |
raw_printf(p->out, "%s", azCol[i]); |
|
|
12365 |
} |
|
|
12366 |
} |
|
|
12367 |
raw_printf(p->out,")"); |
|
|
12368 |
} |
|
|
12369 |
p->cnt++; |
|
|
12370 |
for(i=0; i |
|
|
12371 |
raw_printf(p->out, i>0 ? "," : " VALUES("); |
|
|
12372 |
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
|
|
12373 |
utf8_printf(p->out,"NULL"); |
|
|
12374 |
}else if( aiType && aiType[i]==SQLITE_TEXT ){ |
|
|
12375 |
if( ShellHasFlag(p, SHFLG_Newlines) ){ |
|
|
12376 |
output_quoted_string(p->out, azArg[i]); |
|
|
12377 |
}else{ |
|
|
12378 |
output_quoted_escaped_string(p->out, azArg[i]); |
|
|
12379 |
} |
|
|
12380 |
}else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
|
|
12381 |
utf8_printf(p->out,"%s", azArg[i]); |
|
|
12382 |
}else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
|
|
12383 |
char z[50]; |
|
|
12384 |
double r = sqlite3_column_double(p->pStmt, i); |
|
|
12385 |
sqlite3_uint64 ur; |
|
|
12386 |
memcpy(&ur,&r,sizeof(r)); |
|
|
12387 |
if( ur==0x7ff0000000000000LL ){ |
|
|
12388 |
raw_printf(p->out, "1e999"); |
|
|
12389 |
}else if( ur==0xfff0000000000000LL ){ |
|
|
12390 |
raw_printf(p->out, "-1e999"); |
|
|
12391 |
}else{ |
|
|
12392 |
sqlite3_snprintf(50,z,"%!.20g", r); |
|
|
12393 |
raw_printf(p->out, "%s", z); |
|
|
12394 |
} |
|
|
12395 |
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
|
|
12396 |
const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
|
|
12397 |
int nBlob = sqlite3_column_bytes(p->pStmt, i); |
|
|
12398 |
output_hex_blob(p->out, pBlob, nBlob); |
|
|
12399 |
}else if( isNumber(azArg[i], 0) ){ |
|
|
12400 |
utf8_printf(p->out,"%s", azArg[i]); |
|
|
12401 |
}else if( ShellHasFlag(p, SHFLG_Newlines) ){ |
|
|
12402 |
output_quoted_string(p->out, azArg[i]); |
|
|
12403 |
}else{ |
|
|
12404 |
output_quoted_escaped_string(p->out, azArg[i]); |
|
|
12405 |
} |
|
|
12406 |
} |
|
|
12407 |
raw_printf(p->out,");\n"); |
|
|
12408 |
break; |
|
|
12409 |
} |
|
|
12410 |
case MODE_Json: { |
|
|
12411 |
if( azArg==0 ) break; |
|
|
12412 |
if( p->cnt==0 ){ |
|
|
12413 |
fputs("[{", p->out); |
|
|
12414 |
}else{ |
|
|
12415 |
fputs(",\n{", p->out); |
|
|
12416 |
} |
|
|
12417 |
p->cnt++; |
|
|
12418 |
for(i=0; i |
|
|
12419 |
output_json_string(p->out, azCol[i], -1); |
|
|
12420 |
putc(':', p->out); |
|
|
12421 |
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
|
|
12422 |
fputs("null",p->out); |
|
|
12423 |
}else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
|
|
12424 |
char z[50]; |
|
|
12425 |
double r = sqlite3_column_double(p->pStmt, i); |
|
|
12426 |
sqlite3_uint64 ur; |
|
|
12427 |
memcpy(&ur,&r,sizeof(r)); |
|
|
12428 |
if( ur==0x7ff0000000000000LL ){ |
|
|
12429 |
raw_printf(p->out, "1e999"); |
|
|
12430 |
}else if( ur==0xfff0000000000000LL ){ |
|
|
12431 |
raw_printf(p->out, "-1e999"); |
|
|
12432 |
}else{ |
|
|
12433 |
sqlite3_snprintf(50,z,"%!.20g", r); |
|
|
12434 |
raw_printf(p->out, "%s", z); |
|
|
12435 |
} |
|
|
12436 |
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
|
|
12437 |
const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
|
|
12438 |
int nBlob = sqlite3_column_bytes(p->pStmt, i); |
|
|
12439 |
output_json_string(p->out, pBlob, nBlob); |
|
|
12440 |
}else if( aiType && aiType[i]==SQLITE_TEXT ){ |
|
|
12441 |
output_json_string(p->out, azArg[i], -1); |
|
|
12442 |
}else{ |
|
|
12443 |
utf8_printf(p->out,"%s", azArg[i]); |
|
|
12444 |
} |
|
|
12445 |
if( i |
|
|
12446 |
putc(',', p->out); |
|
|
12447 |
} |
|
|
12448 |
} |
|
|
12449 |
putc('}', p->out); |
|
|
12450 |
break; |
|
|
12451 |
} |
|
|
12452 |
case MODE_Quote: { |
|
|
12453 |
if( azArg==0 ) break; |
|
|
12454 |
if( p->cnt==0 && p->showHeader ){ |
|
|
12455 |
for(i=0; i |
|
|
12456 |
if( i>0 ) fputs(p->colSeparator, p->out); |
|
|
12457 |
output_quoted_string(p->out, azCol[i]); |
|
|
12458 |
} |
|
|
12459 |
fputs(p->rowSeparator, p->out); |
|
|
12460 |
} |
|
|
12461 |
p->cnt++; |
|
|
12462 |
for(i=0; i |
|
|
12463 |
if( i>0 ) fputs(p->colSeparator, p->out); |
|
|
12464 |
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
|
|
12465 |
utf8_printf(p->out,"NULL"); |
|
|
12466 |
}else if( aiType && aiType[i]==SQLITE_TEXT ){ |
|
|
12467 |
output_quoted_string(p->out, azArg[i]); |
|
|
12468 |
}else if( aiType && aiType[i]==SQLITE_INTEGER ){ |
|
|
12469 |
utf8_printf(p->out,"%s", azArg[i]); |
|
|
12470 |
}else if( aiType && aiType[i]==SQLITE_FLOAT ){ |
|
|
12471 |
char z[50]; |
|
|
12472 |
double r = sqlite3_column_double(p->pStmt, i); |
|
|
12473 |
sqlite3_snprintf(50,z,"%!.20g", r); |
|
|
12474 |
raw_printf(p->out, "%s", z); |
|
|
12475 |
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
|
|
12476 |
const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
|
|
12477 |
int nBlob = sqlite3_column_bytes(p->pStmt, i); |
|
|
12478 |
output_hex_blob(p->out, pBlob, nBlob); |
|
|
12479 |
}else if( isNumber(azArg[i], 0) ){ |
|
|
12480 |
utf8_printf(p->out,"%s", azArg[i]); |
|
|
12481 |
}else{ |
|
|
12482 |
output_quoted_string(p->out, azArg[i]); |
|
|
12483 |
} |
|
|
12484 |
} |
|
|
12485 |
fputs(p->rowSeparator, p->out); |
|
|
12486 |
break; |
|
|
12487 |
} |
|
|
12488 |
case MODE_Ascii: { |
|
|
12489 |
if( p->cnt++==0 && p->showHeader ){ |
|
|
12490 |
for(i=0; i |
|
|
12491 |
if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
|
|
12492 |
utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
|
|
12493 |
} |
|
|
12494 |
utf8_printf(p->out, "%s", p->rowSeparator); |
|
|
12495 |
} |
|
|
12496 |
if( azArg==0 ) break; |
|
|
12497 |
for(i=0; i |
|
|
12498 |
if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
|
|
12499 |
utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
|
|
12500 |
} |
|
|
12501 |
utf8_printf(p->out, "%s", p->rowSeparator); |
|
|
12502 |
break; |
|
|
12503 |
} |
|
|
12504 |
case MODE_EQP: { |
|
|
12505 |
eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]); |
|
|
12506 |
break; |
|
|
12507 |
} |
|
|
12508 |
} |
|
|
12509 |
return 0; |
|
|
12510 |
} |
|
|
12511 |
|
|
|
12512 |
/* |
|
|
12513 |
** This is the callback routine that the SQLite library |
|
|
12514 |
** invokes for each row of a query result. |
|
|
12515 |
*/ |
|
|
12516 |
static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
|
|
12517 |
/* since we don't have type info, call the shell_callback with a NULL value */ |
|
|
12518 |
return shell_callback(pArg, nArg, azArg, azCol, NULL); |
|
|
12519 |
} |
|
|
12520 |
|
|
|
12521 |
/* |
|
|
12522 |
** This is the callback routine from sqlite3_exec() that appends all |
|
|
12523 |
** output onto the end of a ShellText object. |
|
|
12524 |
*/ |
|
|
12525 |
static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ |
|
|
12526 |
ShellText *p = (ShellText*)pArg; |
|
|
12527 |
int i; |
|
|
12528 |
UNUSED_PARAMETER(az); |
|
|
12529 |
if( azArg==0 ) return 0; |
|
|
12530 |
if( p->n ) appendText(p, "|", 0); |
|
|
12531 |
for(i=0; i |
|
|
12532 |
if( i ) appendText(p, ",", 0); |
|
|
12533 |
if( azArg[i] ) appendText(p, azArg[i], 0); |
|
|
12534 |
} |
|
|
12535 |
return 0; |
|
|
12536 |
} |
|
|
12537 |
|
|
|
12538 |
/* |
|
|
12539 |
** Generate an appropriate SELFTEST table in the main database. |
|
|
12540 |
*/ |
|
|
12541 |
static void createSelftestTable(ShellState *p){ |
|
|
12542 |
char *zErrMsg = 0; |
|
|
12543 |
sqlite3_exec(p->db, |
|
|
12544 |
"SAVEPOINT selftest_init;\n" |
|
|
12545 |
"CREATE TABLE IF NOT EXISTS selftest(\n" |
|
|
12546 |
" tno INTEGER PRIMARY KEY,\n" /* Test number */ |
|
|
12547 |
" op TEXT,\n" /* Operator: memo run */ |
|
|
12548 |
" cmd TEXT,\n" /* Command text */ |
|
|
12549 |
" ans TEXT\n" /* Desired answer */ |
|
|
12550 |
");" |
|
|
12551 |
"CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" |
|
|
12552 |
"INSERT INTO [_shell$self](rowid,op,cmd)\n" |
|
|
12553 |
" VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" |
|
|
12554 |
" 'memo','Tests generated by --init');\n" |
|
|
12555 |
"INSERT INTO [_shell$self]\n" |
|
|
12556 |
" SELECT 'run',\n" |
|
|
12557 |
" 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " |
|
|
12558 |
"FROM sqlite_schema ORDER BY 2'',224))',\n" |
|
|
12559 |
" hex(sha3_query('SELECT type,name,tbl_name,sql " |
|
|
12560 |
"FROM sqlite_schema ORDER BY 2',224));\n" |
|
|
12561 |
"INSERT INTO [_shell$self]\n" |
|
|
12562 |
" SELECT 'run'," |
|
|
12563 |
" 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" |
|
|
12564 |
" printf('%w',name) || '\" NOT INDEXED'',224))',\n" |
|
|
12565 |
" hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" |
|
|
12566 |
" FROM (\n" |
|
|
12567 |
" SELECT name FROM sqlite_schema\n" |
|
|
12568 |
" WHERE type='table'\n" |
|
|
12569 |
" AND name<>'selftest'\n" |
|
|
12570 |
" AND coalesce(rootpage,0)>0\n" |
|
|
12571 |
" )\n" |
|
|
12572 |
" ORDER BY name;\n" |
|
|
12573 |
"INSERT INTO [_shell$self]\n" |
|
|
12574 |
" VALUES('run','PRAGMA integrity_check','ok');\n" |
|
|
12575 |
"INSERT INTO selftest(tno,op,cmd,ans)" |
|
|
12576 |
" SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" |
|
|
12577 |
"DROP TABLE [_shell$self];" |
|
|
12578 |
,0,0,&zErrMsg); |
|
|
12579 |
if( zErrMsg ){ |
|
|
12580 |
utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); |
|
|
12581 |
sqlite3_free(zErrMsg); |
|
|
12582 |
} |
|
|
12583 |
sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); |
|
|
12584 |
} |
|
|
12585 |
|
|
|
12586 |
|
|
|
12587 |
/* |
|
|
12588 |
** Set the destination table field of the ShellState structure to |
|
|
12589 |
** the name of the table given. Escape any quote characters in the |
|
|
12590 |
** table name. |
|
|
12591 |
*/ |
|
|
12592 |
static void set_table_name(ShellState *p, const char *zName){ |
|
|
12593 |
int i, n; |
|
|
12594 |
char cQuote; |
|
|
12595 |
char *z; |
|
|
12596 |
|
|
|
12597 |
if( p->zDestTable ){ |
|
|
12598 |
free(p->zDestTable); |
|
|
12599 |
p->zDestTable = 0; |
|
|
12600 |
} |
|
|
12601 |
if( zName==0 ) return; |
|
|
12602 |
cQuote = quoteChar(zName); |
|
|
12603 |
n = strlen30(zName); |
|
|
12604 |
if( cQuote ) n += n+2; |
|
|
12605 |
z = p->zDestTable = malloc( n+1 ); |
|
|
12606 |
if( z==0 ) shell_out_of_memory(); |
|
|
12607 |
n = 0; |
|
|
12608 |
if( cQuote ) z[n++] = cQuote; |
|
|
12609 |
for(i=0; zName[i]; i++){ |
|
|
12610 |
z[n++] = zName[i]; |
|
|
12611 |
if( zName[i]==cQuote ) z[n++] = cQuote; |
|
|
12612 |
} |
|
|
12613 |
if( cQuote ) z[n++] = cQuote; |
|
|
12614 |
z[n] = 0; |
|
|
12615 |
} |
|
|
12616 |
|
|
|
12617 |
|
|
|
12618 |
/* |
|
|
12619 |
** Execute a query statement that will generate SQL output. Print |
|
|
12620 |
** the result columns, comma-separated, on a line and then add a |
|
|
12621 |
** semicolon terminator to the end of that line. |
|
|
12622 |
** |
|
|
12623 |
** If the number of columns is 1 and that column contains text "--" |
|
|
12624 |
** then write the semicolon on a separate line. That way, if a |
|
|
12625 |
** "--" comment occurs at the end of the statement, the comment |
|
|
12626 |
** won't consume the semicolon terminator. |
|
|
12627 |
*/ |
|
|
12628 |
static int run_table_dump_query( |
|
|
12629 |
ShellState *p, /* Query context */ |
|
|
12630 |
const char *zSelect /* SELECT statement to extract content */ |
|
|
12631 |
){ |
|
|
12632 |
sqlite3_stmt *pSelect; |
|
|
12633 |
int rc; |
|
|
12634 |
int nResult; |
|
|
12635 |
int i; |
|
|
12636 |
const char *z; |
|
|
12637 |
rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
|
|
12638 |
if( rc!=SQLITE_OK || !pSelect ){ |
|
|
12639 |
utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
|
|
12640 |
sqlite3_errmsg(p->db)); |
|
|
12641 |
if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
|
|
12642 |
return rc; |
|
|
12643 |
} |
|
|
12644 |
rc = sqlite3_step(pSelect); |
|
|
12645 |
nResult = sqlite3_column_count(pSelect); |
|
|
12646 |
while( rc==SQLITE_ROW ){ |
|
|
12647 |
z = (const char*)sqlite3_column_text(pSelect, 0); |
|
|
12648 |
utf8_printf(p->out, "%s", z); |
|
|
12649 |
for(i=1; i |
|
|
12650 |
utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
|
|
12651 |
} |
|
|
12652 |
if( z==0 ) z = ""; |
|
|
12653 |
while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
|
|
12654 |
if( z[0] ){ |
|
|
12655 |
raw_printf(p->out, "\n;\n"); |
|
|
12656 |
}else{ |
|
|
12657 |
raw_printf(p->out, ";\n"); |
|
|
12658 |
} |
|
|
12659 |
rc = sqlite3_step(pSelect); |
|
|
12660 |
} |
|
|
12661 |
rc = sqlite3_finalize(pSelect); |
|
|
12662 |
if( rc!=SQLITE_OK ){ |
|
|
12663 |
utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
|
|
12664 |
sqlite3_errmsg(p->db)); |
|
|
12665 |
if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
|
|
12666 |
} |
|
|
12667 |
return rc; |
|
|
12668 |
} |
|
|
12669 |
|
|
|
12670 |
/* |
|
|
12671 |
** Allocate space and save off current error string. |
|
|
12672 |
*/ |
|
|
12673 |
static char *save_err_msg( |
|
|
12674 |
sqlite3 *db /* Database to query */ |
|
|
12675 |
){ |
|
|
12676 |
int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
|
|
12677 |
char *zErrMsg = sqlite3_malloc64(nErrMsg); |
|
|
12678 |
if( zErrMsg ){ |
|
|
12679 |
memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
|
|
12680 |
} |
|
|
12681 |
return zErrMsg; |
|
|
12682 |
} |
|
|
12683 |
|
|
|
12684 |
#ifdef __linux__ |
|
|
12685 |
/* |
|
|
12686 |
** Attempt to display I/O stats on Linux using /proc/PID/io |
|
|
12687 |
*/ |
|
|
12688 |
static void displayLinuxIoStats(FILE *out){ |
|
|
12689 |
FILE *in; |
|
|
12690 |
char z[200]; |
|
|
12691 |
sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
|
|
12692 |
in = fopen(z, "rb"); |
|
|
12693 |
if( in==0 ) return; |
|
|
12694 |
while( fgets(z, sizeof(z), in)!=0 ){ |
|
|
12695 |
static const struct { |
|
|
12696 |
const char *zPattern; |
|
|
12697 |
const char *zDesc; |
|
|
12698 |
} aTrans[] = { |
|
|
12699 |
{ "rchar: ", "Bytes received by read():" }, |
|
|
12700 |
{ "wchar: ", "Bytes sent to write():" }, |
|
|
12701 |
{ "syscr: ", "Read() system calls:" }, |
|
|
12702 |
{ "syscw: ", "Write() system calls:" }, |
|
|
12703 |
{ "read_bytes: ", "Bytes read from storage:" }, |
|
|
12704 |
{ "write_bytes: ", "Bytes written to storage:" }, |
|
|
12705 |
{ "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
|
|
12706 |
}; |
|
|
12707 |
int i; |
|
|
12708 |
for(i=0; i |
|
|
12709 |
int n = strlen30(aTrans[i].zPattern); |
|
|
12710 |
if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
|
|
12711 |
utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
|
|
12712 |
break; |
|
|
12713 |
} |
|
|
12714 |
} |
|
|
12715 |
} |
|
|
12716 |
fclose(in); |
|
|
12717 |
} |
|
|
12718 |
#endif |
|
|
12719 |
|
|
|
12720 |
/* |
|
|
12721 |
** Display a single line of status using 64-bit values. |
|
|
12722 |
*/ |
|
|
12723 |
static void displayStatLine( |
|
|
12724 |
ShellState *p, /* The shell context */ |
|
|
12725 |
char *zLabel, /* Label for this one line */ |
|
|
12726 |
char *zFormat, /* Format for the result */ |
|
|
12727 |
int iStatusCtrl, /* Which status to display */ |
|
|
12728 |
int bReset /* True to reset the stats */ |
|
|
12729 |
){ |
|
|
12730 |
sqlite3_int64 iCur = -1; |
|
|
12731 |
sqlite3_int64 iHiwtr = -1; |
|
|
12732 |
int i, nPercent; |
|
|
12733 |
char zLine[200]; |
|
|
12734 |
sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset); |
|
|
12735 |
for(i=0, nPercent=0; zFormat[i]; i++){ |
|
|
12736 |
if( zFormat[i]=='%' ) nPercent++; |
|
|
12737 |
} |
|
|
12738 |
if( nPercent>1 ){ |
|
|
12739 |
sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); |
|
|
12740 |
}else{ |
|
|
12741 |
sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); |
|
|
12742 |
} |
|
|
12743 |
raw_printf(p->out, "%-36s %s\n", zLabel, zLine); |
|
|
12744 |
} |
|
|
12745 |
|
|
|
12746 |
/* |
|
|
12747 |
** Display memory stats. |
|
|
12748 |
*/ |
|
|
12749 |
static int display_stats( |
|
|
12750 |
sqlite3 *db, /* Database to query */ |
|
|
12751 |
ShellState *pArg, /* Pointer to ShellState */ |
|
|
12752 |
int bReset /* True to reset the stats */ |
|
|
12753 |
){ |
|
|
12754 |
int iCur; |
|
|
12755 |
int iHiwtr; |
|
|
12756 |
FILE *out; |
|
|
12757 |
if( pArg==0 || pArg->out==0 ) return 0; |
|
|
12758 |
out = pArg->out; |
|
|
12759 |
|
|
|
12760 |
if( pArg->pStmt && pArg->statsOn==2 ){ |
|
|
12761 |
int nCol, i, x; |
|
|
12762 |
sqlite3_stmt *pStmt = pArg->pStmt; |
|
|
12763 |
char z[100]; |
|
|
12764 |
nCol = sqlite3_column_count(pStmt); |
|
|
12765 |
raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol); |
|
|
12766 |
for(i=0; i |
|
|
12767 |
sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x); |
|
|
12768 |
utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i)); |
|
|
12769 |
#ifndef SQLITE_OMIT_DECLTYPE |
|
|
12770 |
sqlite3_snprintf(30, z+x, "declared type:"); |
|
|
12771 |
utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i)); |
|
|
12772 |
#endif |
|
|
12773 |
#ifdef SQLITE_ENABLE_COLUMN_METADATA |
|
|
12774 |
sqlite3_snprintf(30, z+x, "database name:"); |
|
|
12775 |
utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i)); |
|
|
12776 |
sqlite3_snprintf(30, z+x, "table name:"); |
|
|
12777 |
utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i)); |
|
|
12778 |
sqlite3_snprintf(30, z+x, "origin name:"); |
|
|
12779 |
utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i)); |
|
|
12780 |
#endif |
|
|
12781 |
} |
|
|
12782 |
} |
|
|
12783 |
|
|
|
12784 |
if( pArg->statsOn==3 ){ |
|
|
12785 |
if( pArg->pStmt ){ |
|
|
12786 |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
|
|
12787 |
raw_printf(pArg->out, "VM-steps: %d\n", iCur); |
|
|
12788 |
} |
|
|
12789 |
return 0; |
|
|
12790 |
} |
|
|
12791 |
|
|
|
12792 |
displayStatLine(pArg, "Memory Used:", |
|
|
12793 |
"%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); |
|
|
12794 |
displayStatLine(pArg, "Number of Outstanding Allocations:", |
|
|
12795 |
"%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); |
|
|
12796 |
if( pArg->shellFlgs & SHFLG_Pagecache ){ |
|
|
12797 |
displayStatLine(pArg, "Number of Pcache Pages Used:", |
|
|
12798 |
"%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); |
|
|
12799 |
} |
|
|
12800 |
displayStatLine(pArg, "Number of Pcache Overflow Bytes:", |
|
|
12801 |
"%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); |
|
|
12802 |
displayStatLine(pArg, "Largest Allocation:", |
|
|
12803 |
"%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); |
|
|
12804 |
displayStatLine(pArg, "Largest Pcache Allocation:", |
|
|
12805 |
"%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); |
|
|
12806 |
#ifdef YYTRACKMAXSTACKDEPTH |
|
|
12807 |
displayStatLine(pArg, "Deepest Parser Stack:", |
|
|
12808 |
"%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); |
|
|
12809 |
#endif |
|
|
12810 |
|
|
|
12811 |
if( db ){ |
|
|
12812 |
if( pArg->shellFlgs & SHFLG_Lookaside ){ |
|
|
12813 |
iHiwtr = iCur = -1; |
|
|
12814 |
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
|
|
12815 |
&iCur, &iHiwtr, bReset); |
|
|
12816 |
raw_printf(pArg->out, |
|
|
12817 |
"Lookaside Slots Used: %d (max %d)\n", |
|
|
12818 |
iCur, iHiwtr); |
|
|
12819 |
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
|
|
12820 |
&iCur, &iHiwtr, bReset); |
|
|
12821 |
raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
|
|
12822 |
iHiwtr); |
|
|
12823 |
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
|
|
12824 |
&iCur, &iHiwtr, bReset); |
|
|
12825 |
raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
|
|
12826 |
iHiwtr); |
|
|
12827 |
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
|
|
12828 |
&iCur, &iHiwtr, bReset); |
|
|
12829 |
raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
|
|
12830 |
iHiwtr); |
|
|
12831 |
} |
|
|
12832 |
iHiwtr = iCur = -1; |
|
|
12833 |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
|
|
12834 |
raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
|
|
12835 |
iCur); |
|
|
12836 |
iHiwtr = iCur = -1; |
|
|
12837 |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
|
|
12838 |
raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
|
|
12839 |
iHiwtr = iCur = -1; |
|
|
12840 |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
|
|
12841 |
raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
|
|
12842 |
iHiwtr = iCur = -1; |
|
|
12843 |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
|
|
12844 |
raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
|
|
12845 |
iHiwtr = iCur = -1; |
|
|
12846 |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1); |
|
|
12847 |
raw_printf(pArg->out, "Page cache spills: %d\n", iCur); |
|
|
12848 |
iHiwtr = iCur = -1; |
|
|
12849 |
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
|
|
12850 |
raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
|
|
12851 |
iCur); |
|
|
12852 |
iHiwtr = iCur = -1; |
|
|
12853 |
sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
|
|
12854 |
raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
|
|
12855 |
iCur); |
|
|
12856 |
} |
|
|
12857 |
|
|
|
12858 |
if( pArg->pStmt ){ |
|
|
12859 |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
|
|
12860 |
bReset); |
|
|
12861 |
raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
|
|
12862 |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
|
|
12863 |
raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
|
|
12864 |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
|
|
12865 |
raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
|
|
12866 |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
|
|
12867 |
raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
|
|
12868 |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); |
|
|
12869 |
raw_printf(pArg->out, "Reprepare operations: %d\n", iCur); |
|
|
12870 |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); |
|
|
12871 |
raw_printf(pArg->out, "Number of times run: %d\n", iCur); |
|
|
12872 |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); |
|
|
12873 |
raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur); |
|
|
12874 |
} |
|
|
12875 |
|
|
|
12876 |
#ifdef __linux__ |
|
|
12877 |
displayLinuxIoStats(pArg->out); |
|
|
12878 |
#endif |
|
|
12879 |
|
|
|
12880 |
/* Do not remove this machine readable comment: extra-stats-output-here */ |
|
|
12881 |
|
|
|
12882 |
return 0; |
|
|
12883 |
} |
|
|
12884 |
|
|
|
12885 |
/* |
|
|
12886 |
** Display scan stats. |
|
|
12887 |
*/ |
|
|
12888 |
static void display_scanstats( |
|
|
12889 |
sqlite3 *db, /* Database to query */ |
|
|
12890 |
ShellState *pArg /* Pointer to ShellState */ |
|
|
12891 |
){ |
|
|
12892 |
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
|
|
12893 |
UNUSED_PARAMETER(db); |
|
|
12894 |
UNUSED_PARAMETER(pArg); |
|
|
12895 |
#else |
|
|
12896 |
int i, k, n, mx; |
|
|
12897 |
raw_printf(pArg->out, "-------- scanstats --------\n"); |
|
|
12898 |
mx = 0; |
|
|
12899 |
for(k=0; k<=mx; k++){ |
|
|
12900 |
double rEstLoop = 1.0; |
|
|
12901 |
for(i=n=0; 1; i++){ |
|
|
12902 |
sqlite3_stmt *p = pArg->pStmt; |
|
|
12903 |
sqlite3_int64 nLoop, nVisit; |
|
|
12904 |
double rEst; |
|
|
12905 |
int iSid; |
|
|
12906 |
const char *zExplain; |
|
|
12907 |
if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
|
|
12908 |
break; |
|
|
12909 |
} |
|
|
12910 |
sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
|
|
12911 |
if( iSid>mx ) mx = iSid; |
|
|
12912 |
if( iSid!=k ) continue; |
|
|
12913 |
if( n==0 ){ |
|
|
12914 |
rEstLoop = (double)nLoop; |
|
|
12915 |
if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
|
|
12916 |
} |
|
|
12917 |
n++; |
|
|
12918 |
sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
|
|
12919 |
sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
|
|
12920 |
sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
|
|
12921 |
utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
|
|
12922 |
rEstLoop *= rEst; |
|
|
12923 |
raw_printf(pArg->out, |
|
|
12924 |
" nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
|
|
12925 |
nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
|
|
12926 |
); |
|
|
12927 |
} |
|
|
12928 |
} |
|
|
12929 |
raw_printf(pArg->out, "---------------------------\n"); |
|
|
12930 |
#endif |
|
|
12931 |
} |
|
|
12932 |
|
|
|
12933 |
/* |
|
|
12934 |
** Parameter azArray points to a zero-terminated array of strings. zStr |
|
|
12935 |
** points to a single nul-terminated string. Return non-zero if zStr |
|
|
12936 |
** is equal, according to strcmp(), to any of the strings in the array. |
|
|
12937 |
** Otherwise, return zero. |
|
|
12938 |
*/ |
|
|
12939 |
static int str_in_array(const char *zStr, const char **azArray){ |
|
|
12940 |
int i; |
|
|
12941 |
for(i=0; azArray[i]; i++){ |
|
|
12942 |
if( 0==strcmp(zStr, azArray[i]) ) return 1; |
|
|
12943 |
} |
|
|
12944 |
return 0; |
|
|
12945 |
} |
|
|
12946 |
|
|
|
12947 |
/* |
|
|
12948 |
** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
|
|
12949 |
** and populate the ShellState.aiIndent[] array with the number of |
|
|
12950 |
** spaces each opcode should be indented before it is output. |
|
|
12951 |
** |
|
|
12952 |
** The indenting rules are: |
|
|
12953 |
** |
|
|
12954 |
** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
|
|
12955 |
** all opcodes that occur between the p2 jump destination and the opcode |
|
|
12956 |
** itself by 2 spaces. |
|
|
12957 |
** |
|
|
12958 |
** * For each "Goto", if the jump destination is earlier in the program |
|
|
12959 |
** and ends on one of: |
|
|
12960 |
** Yield SeekGt SeekLt RowSetRead Rewind |
|
|
12961 |
** or if the P1 parameter is one instead of zero, |
|
|
12962 |
** then indent all opcodes between the earlier instruction |
|
|
12963 |
** and "Goto" by 2 spaces. |
|
|
12964 |
*/ |
|
|
12965 |
static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
|
|
12966 |
const char *zSql; /* The text of the SQL statement */ |
|
|
12967 |
const char *z; /* Used to check if this is an EXPLAIN */ |
|
|
12968 |
int *abYield = 0; /* True if op is an OP_Yield */ |
|
|
12969 |
int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
|
|
12970 |
int iOp; /* Index of operation in p->aiIndent[] */ |
|
|
12971 |
|
|
|
12972 |
const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 }; |
|
|
12973 |
const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
|
|
12974 |
"Rewind", 0 }; |
|
|
12975 |
const char *azGoto[] = { "Goto", 0 }; |
|
|
12976 |
|
|
|
12977 |
/* Try to figure out if this is really an EXPLAIN statement. If this |
|
|
12978 |
** cannot be verified, return early. */ |
|
|
12979 |
if( sqlite3_column_count(pSql)!=8 ){ |
|
|
12980 |
p->cMode = p->mode; |
|
|
12981 |
return; |
|
|
12982 |
} |
|
|
12983 |
zSql = sqlite3_sql(pSql); |
|
|
12984 |
if( zSql==0 ) return; |
|
|
12985 |
for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
|
|
12986 |
if( sqlite3_strnicmp(z, "explain", 7) ){ |
|
|
12987 |
p->cMode = p->mode; |
|
|
12988 |
return; |
|
|
12989 |
} |
|
|
12990 |
|
|
|
12991 |
for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
|
|
12992 |
int i; |
|
|
12993 |
int iAddr = sqlite3_column_int(pSql, 0); |
|
|
12994 |
const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
|
|
12995 |
|
|
|
12996 |
/* Set p2 to the P2 field of the current opcode. Then, assuming that |
|
|
12997 |
** p2 is an instruction address, set variable p2op to the index of that |
|
|
12998 |
** instruction in the aiIndent[] array. p2 and p2op may be different if |
|
|
12999 |
** the current instruction is part of a sub-program generated by an |
|
|
13000 |
** SQL trigger or foreign key. */ |
|
|
13001 |
int p2 = sqlite3_column_int(pSql, 3); |
|
|
13002 |
int p2op = (p2 + (iOp-iAddr)); |
|
|
13003 |
|
|
|
13004 |
/* Grow the p->aiIndent array as required */ |
|
|
13005 |
if( iOp>=nAlloc ){ |
|
|
13006 |
if( iOp==0 ){ |
|
|
13007 |
/* Do further verfication that this is explain output. Abort if |
|
|
13008 |
** it is not */ |
|
|
13009 |
static const char *explainCols[] = { |
|
|
13010 |
"addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
|
|
13011 |
int jj; |
|
|
13012 |
for(jj=0; jj |
|
|
13013 |
if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
|
|
13014 |
p->cMode = p->mode; |
|
|
13015 |
sqlite3_reset(pSql); |
|
|
13016 |
return; |
|
|
13017 |
} |
|
|
13018 |
} |
|
|
13019 |
} |
|
|
13020 |
nAlloc += 100; |
|
|
13021 |
p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
|
|
13022 |
if( p->aiIndent==0 ) shell_out_of_memory(); |
|
|
13023 |
abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
|
|
13024 |
if( abYield==0 ) shell_out_of_memory(); |
|
|
13025 |
} |
|
|
13026 |
abYield[iOp] = str_in_array(zOp, azYield); |
|
|
13027 |
p->aiIndent[iOp] = 0; |
|
|
13028 |
p->nIndent = iOp+1; |
|
|
13029 |
|
|
|
13030 |
if( str_in_array(zOp, azNext) ){ |
|
|
13031 |
for(i=p2op; iaiIndent[i] += 2; |
|
|
13032 |
} |
|
|
13033 |
if( str_in_array(zOp, azGoto) && p2opnIndent |
|
|
13034 |
&& (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
|
|
13035 |
){ |
|
|
13036 |
for(i=p2op; iaiIndent[i] += 2; |
|
|
13037 |
} |
|
|
13038 |
} |
|
|
13039 |
|
|
|
13040 |
p->iIndent = 0; |
|
|
13041 |
sqlite3_free(abYield); |
|
|
13042 |
sqlite3_reset(pSql); |
|
|
13043 |
} |
|
|
13044 |
|
|
|
13045 |
/* |
|
|
13046 |
** Free the array allocated by explain_data_prepare(). |
|
|
13047 |
*/ |
|
|
13048 |
static void explain_data_delete(ShellState *p){ |
|
|
13049 |
sqlite3_free(p->aiIndent); |
|
|
13050 |
p->aiIndent = 0; |
|
|
13051 |
p->nIndent = 0; |
|
|
13052 |
p->iIndent = 0; |
|
|
13053 |
} |
|
|
13054 |
|
|
|
13055 |
/* |
|
|
13056 |
** Disable and restore .wheretrace and .selecttrace settings. |
|
|
13057 |
*/ |
|
|
13058 |
static unsigned int savedSelectTrace; |
|
|
13059 |
static unsigned int savedWhereTrace; |
|
|
13060 |
static void disable_debug_trace_modes(void){ |
|
|
13061 |
unsigned int zero = 0; |
|
|
13062 |
sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace); |
|
|
13063 |
sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero); |
|
|
13064 |
sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace); |
|
|
13065 |
sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero); |
|
|
13066 |
} |
|
|
13067 |
static void restore_debug_trace_modes(void){ |
|
|
13068 |
sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace); |
|
|
13069 |
sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace); |
|
|
13070 |
} |
|
|
13071 |
|
|
|
13072 |
/* Create the TEMP table used to store parameter bindings */ |
|
|
13073 |
static void bind_table_init(ShellState *p){ |
|
|
13074 |
int wrSchema = 0; |
|
|
13075 |
int defensiveMode = 0; |
|
|
13076 |
sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode); |
|
|
13077 |
sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); |
|
|
13078 |
sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); |
|
|
13079 |
sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); |
|
|
13080 |
sqlite3_exec(p->db, |
|
|
13081 |
"CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n" |
|
|
13082 |
" key TEXT PRIMARY KEY,\n" |
|
|
13083 |
" value ANY\n" |
|
|
13084 |
") WITHOUT ROWID;", |
|
|
13085 |
0, 0, 0); |
|
|
13086 |
sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); |
|
|
13087 |
sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0); |
|
|
13088 |
} |
|
|
13089 |
|
|
|
13090 |
/* |
|
|
13091 |
** Bind parameters on a prepared statement. |
|
|
13092 |
** |
|
|
13093 |
** Parameter bindings are taken from a TEMP table of the form: |
|
|
13094 |
** |
|
|
13095 |
** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value) |
|
|
13096 |
** WITHOUT ROWID; |
|
|
13097 |
** |
|
|
13098 |
** No bindings occur if this table does not exist. The name of the table |
|
|
13099 |
** begins with "sqlite_" so that it will not collide with ordinary application |
|
|
13100 |
** tables. The table must be in the TEMP schema. |
|
|
13101 |
*/ |
|
|
13102 |
static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){ |
|
|
13103 |
int nVar; |
|
|
13104 |
int i; |
|
|
13105 |
int rc; |
|
|
13106 |
sqlite3_stmt *pQ = 0; |
|
|
13107 |
|
|
|
13108 |
nVar = sqlite3_bind_parameter_count(pStmt); |
|
|
13109 |
if( nVar==0 ) return; /* Nothing to do */ |
|
|
13110 |
if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", |
|
|
13111 |
"key", 0, 0, 0, 0, 0)!=SQLITE_OK ){ |
|
|
13112 |
return; /* Parameter table does not exist */ |
|
|
13113 |
} |
|
|
13114 |
rc = sqlite3_prepare_v2(pArg->db, |
|
|
13115 |
"SELECT value FROM temp.sqlite_parameters" |
|
|
13116 |
" WHERE key=?1", -1, &pQ, 0); |
|
|
13117 |
if( rc || pQ==0 ) return; |
|
|
13118 |
for(i=1; i<=nVar; i++){ |
|
|
13119 |
char zNum[30]; |
|
|
13120 |
const char *zVar = sqlite3_bind_parameter_name(pStmt, i); |
|
|
13121 |
if( zVar==0 ){ |
|
|
13122 |
sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i); |
|
|
13123 |
zVar = zNum; |
|
|
13124 |
} |
|
|
13125 |
sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); |
|
|
13126 |
if( sqlite3_step(pQ)==SQLITE_ROW ){ |
|
|
13127 |
sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0)); |
|
|
13128 |
}else{ |
|
|
13129 |
sqlite3_bind_null(pStmt, i); |
|
|
13130 |
} |
|
|
13131 |
sqlite3_reset(pQ); |
|
|
13132 |
} |
|
|
13133 |
sqlite3_finalize(pQ); |
|
|
13134 |
} |
|
|
13135 |
|
|
|
13136 |
/* |
|
|
13137 |
** UTF8 box-drawing characters. Imagine box lines like this: |
|
|
13138 |
** |
|
|
13139 |
** 1 |
|
|
13140 |
** | |
|
|
13141 |
** 4 --+-- 2 |
|
|
13142 |
** | |
|
|
13143 |
** 3 |
|
|
13144 |
** |
|
|
13145 |
** Each box characters has between 2 and 4 of the lines leading from |
|
|
13146 |
** the center. The characters are here identified by the numbers of |
|
|
13147 |
** their corresponding lines. |
|
|
13148 |
*/ |
|
|
13149 |
#define BOX_24 "\342\224\200" /* U+2500 --- */ |
|
|
13150 |
#define BOX_13 "\342\224\202" /* U+2502 | */ |
|
|
13151 |
#define BOX_23 "\342\224\214" /* U+250c ,- */ |
|
|
13152 |
#define BOX_34 "\342\224\220" /* U+2510 -, */ |
|
|
13153 |
#define BOX_12 "\342\224\224" /* U+2514 '- */ |
|
|
13154 |
#define BOX_14 "\342\224\230" /* U+2518 -' */ |
|
|
13155 |
#define BOX_123 "\342\224\234" /* U+251c |- */ |
|
|
13156 |
#define BOX_134 "\342\224\244" /* U+2524 -| */ |
|
|
13157 |
#define BOX_234 "\342\224\254" /* U+252c -,- */ |
|
|
13158 |
#define BOX_124 "\342\224\264" /* U+2534 -'- */ |
|
|
13159 |
#define BOX_1234 "\342\224\274" /* U+253c -|- */ |
|
|
13160 |
|
|
|
13161 |
/* Draw horizontal line N characters long using unicode box |
|
|
13162 |
** characters |
|
|
13163 |
*/ |
|
|
13164 |
static void print_box_line(FILE *out, int N){ |
|
|
13165 |
const char zDash[] = |
|
|
13166 |
BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 |
|
|
13167 |
BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24; |
|
|
13168 |
const int nDash = sizeof(zDash) - 1; |
|
|
13169 |
N *= 3; |
|
|
13170 |
while( N>nDash ){ |
|
|
13171 |
utf8_printf(out, zDash); |
|
|
13172 |
N -= nDash; |
|
|
13173 |
} |
|
|
13174 |
utf8_printf(out, "%.*s", N, zDash); |
|
|
13175 |
} |
|
|
13176 |
|
|
|
13177 |
/* |
|
|
13178 |
** Draw a horizontal separator for a MODE_Box table. |
|
|
13179 |
*/ |
|
|
13180 |
static void print_box_row_separator( |
|
|
13181 |
ShellState *p, |
|
|
13182 |
int nArg, |
|
|
13183 |
const char *zSep1, |
|
|
13184 |
const char *zSep2, |
|
|
13185 |
const char *zSep3 |
|
|
13186 |
){ |
|
|
13187 |
int i; |
|
|
13188 |
if( nArg>0 ){ |
|
|
13189 |
utf8_printf(p->out, "%s", zSep1); |
|
|
13190 |
print_box_line(p->out, p->actualWidth[0]+2); |
|
|
13191 |
for(i=1; i |
|
|
13192 |
utf8_printf(p->out, "%s", zSep2); |
|
|
13193 |
print_box_line(p->out, p->actualWidth[i]+2); |
|
|
13194 |
} |
|
|
13195 |
utf8_printf(p->out, "%s", zSep3); |
|
|
13196 |
} |
|
|
13197 |
fputs("\n", p->out); |
|
|
13198 |
} |
|
|
13199 |
|
|
|
13200 |
|
|
|
13201 |
|
|
|
13202 |
/* |
|
|
13203 |
** Run a prepared statement and output the result in one of the |
|
|
13204 |
** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table, |
|
|
13205 |
** or MODE_Box. |
|
|
13206 |
** |
|
|
13207 |
** This is different from ordinary exec_prepared_stmt() in that |
|
|
13208 |
** it has to run the entire query and gather the results into memory |
|
|
13209 |
** first, in order to determine column widths, before providing |
|
|
13210 |
** any output. |
|
|
13211 |
*/ |
|
|
13212 |
static void exec_prepared_stmt_columnar( |
|
|
13213 |
ShellState *p, /* Pointer to ShellState */ |
|
|
13214 |
sqlite3_stmt *pStmt /* Statment to run */ |
|
|
13215 |
){ |
|
|
13216 |
sqlite3_int64 nRow = 0; |
|
|
13217 |
int nColumn = 0; |
|
|
13218 |
char **azData = 0; |
|
|
13219 |
sqlite3_int64 nAlloc = 0; |
|
|
13220 |
const char *z; |
|
|
13221 |
int rc; |
|
|
13222 |
sqlite3_int64 i, nData; |
|
|
13223 |
int j, nTotal, w, n; |
|
|
13224 |
const char *colSep = 0; |
|
|
13225 |
const char *rowSep = 0; |
|
|
13226 |
|
|
|
13227 |
rc = sqlite3_step(pStmt); |
|
|
13228 |
if( rc!=SQLITE_ROW ) return; |
|
|
13229 |
nColumn = sqlite3_column_count(pStmt); |
|
|
13230 |
nAlloc = nColumn*4; |
|
|
13231 |
if( nAlloc<=0 ) nAlloc = 1; |
|
|
13232 |
azData = sqlite3_malloc64( nAlloc*sizeof(char*) ); |
|
|
13233 |
if( azData==0 ) shell_out_of_memory(); |
|
|
13234 |
for(i=0; i |
|
|
13235 |
azData[i] = strdup(sqlite3_column_name(pStmt,i)); |
|
|
13236 |
} |
|
|
13237 |
do{ |
|
|
13238 |
if( (nRow+2)*nColumn >= nAlloc ){ |
|
|
13239 |
nAlloc *= 2; |
|
|
13240 |
azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*)); |
|
|
13241 |
if( azData==0 ) shell_out_of_memory(); |
|
|
13242 |
} |
|
|
13243 |
nRow++; |
|
|
13244 |
for(i=0; i |
|
|
13245 |
z = (const char*)sqlite3_column_text(pStmt,i); |
|
|
13246 |
azData[nRow*nColumn + i] = z ? strdup(z) : 0; |
|
|
13247 |
} |
|
|
13248 |
}while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ); |
|
|
13249 |
if( nColumn>p->nWidth ){ |
|
|
13250 |
p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int)); |
|
|
13251 |
if( p->colWidth==0 ) shell_out_of_memory(); |
|
|
13252 |
for(i=p->nWidth; icolWidth[i] = 0; |
|
|
13253 |
p->nWidth = nColumn; |
|
|
13254 |
p->actualWidth = &p->colWidth[nColumn]; |
|
|
13255 |
} |
|
|
13256 |
memset(p->actualWidth, 0, nColumn*sizeof(int)); |
|
|
13257 |
for(i=0; i |
|
|
13258 |
w = p->colWidth[i]; |
|
|
13259 |
if( w<0 ) w = -w; |
|
|
13260 |
p->actualWidth[i] = w; |
|
|
13261 |
} |
|
|
13262 |
nTotal = nColumn*(nRow+1); |
|
|
13263 |
for(i=0; i |
|
|
13264 |
z = azData[i]; |
|
|
13265 |
if( z==0 ) z = p->nullValue; |
|
|
13266 |
n = strlenChar(z); |
|
|
13267 |
j = i%nColumn; |
|
|
13268 |
if( n>p->actualWidth[j] ) p->actualWidth[j] = n; |
|
|
13269 |
} |
|
|
13270 |
if( seenInterrupt ) goto columnar_end; |
|
|
13271 |
if( nColumn==0 ) goto columnar_end; |
|
|
13272 |
switch( p->cMode ){ |
|
|
13273 |
case MODE_Column: { |
|
|
13274 |
colSep = " "; |
|
|
13275 |
rowSep = "\n"; |
|
|
13276 |
if( p->showHeader ){ |
|
|
13277 |
for(i=0; i |
|
|
13278 |
w = p->actualWidth[i]; |
|
|
13279 |
if( p->colWidth[i]<0 ) w = -w; |
|
|
13280 |
utf8_width_print(p->out, w, azData[i]); |
|
|
13281 |
fputs(i==nColumn-1?"\n":" ", p->out); |
|
|
13282 |
} |
|
|
13283 |
for(i=0; i |
|
|
13284 |
print_dashes(p->out, p->actualWidth[i]); |
|
|
13285 |
fputs(i==nColumn-1?"\n":" ", p->out); |
|
|
13286 |
} |
|
|
13287 |
} |
|
|
13288 |
break; |
|
|
13289 |
} |
|
|
13290 |
case MODE_Table: { |
|
|
13291 |
colSep = " | "; |
|
|
13292 |
rowSep = " |\n"; |
|
|
13293 |
print_row_separator(p, nColumn, "+"); |
|
|
13294 |
fputs("| ", p->out); |
|
|
13295 |
for(i=0; i |
|
|
13296 |
w = p->actualWidth[i]; |
|
|
13297 |
n = strlenChar(azData[i]); |
|
|
13298 |
utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); |
|
|
13299 |
fputs(i==nColumn-1?" |\n":" | ", p->out); |
|
|
13300 |
} |
|
|
13301 |
print_row_separator(p, nColumn, "+"); |
|
|
13302 |
break; |
|
|
13303 |
} |
|
|
13304 |
case MODE_Markdown: { |
|
|
13305 |
colSep = " | "; |
|
|
13306 |
rowSep = " |\n"; |
|
|
13307 |
fputs("| ", p->out); |
|
|
13308 |
for(i=0; i |
|
|
13309 |
w = p->actualWidth[i]; |
|
|
13310 |
n = strlenChar(azData[i]); |
|
|
13311 |
utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); |
|
|
13312 |
fputs(i==nColumn-1?" |\n":" | ", p->out); |
|
|
13313 |
} |
|
|
13314 |
print_row_separator(p, nColumn, "|"); |
|
|
13315 |
break; |
|
|
13316 |
} |
|
|
13317 |
case MODE_Box: { |
|
|
13318 |
colSep = " " BOX_13 " "; |
|
|
13319 |
rowSep = " " BOX_13 "\n"; |
|
|
13320 |
print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34); |
|
|
13321 |
utf8_printf(p->out, BOX_13 " "); |
|
|
13322 |
for(i=0; i |
|
|
13323 |
w = p->actualWidth[i]; |
|
|
13324 |
n = strlenChar(azData[i]); |
|
|
13325 |
utf8_printf(p->out, "%*s%s%*s%s", |
|
|
13326 |
(w-n)/2, "", azData[i], (w-n+1)/2, "", |
|
|
13327 |
i==nColumn-1?" "BOX_13"\n":" "BOX_13" "); |
|
|
13328 |
} |
|
|
13329 |
print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); |
|
|
13330 |
break; |
|
|
13331 |
} |
|
|
13332 |
} |
|
|
13333 |
for(i=nColumn, j=0; i |
|
|
13334 |
if( j==0 && p->cMode!=MODE_Column ){ |
|
|
13335 |
utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| "); |
|
|
13336 |
} |
|
|
13337 |
z = azData[i]; |
|
|
13338 |
if( z==0 ) z = p->nullValue; |
|
|
13339 |
w = p->actualWidth[j]; |
|
|
13340 |
if( p->colWidth[j]<0 ) w = -w; |
|
|
13341 |
utf8_width_print(p->out, w, z); |
|
|
13342 |
if( j==nColumn-1 ){ |
|
|
13343 |
utf8_printf(p->out, "%s", rowSep); |
|
|
13344 |
j = -1; |
|
|
13345 |
if( seenInterrupt ) goto columnar_end; |
|
|
13346 |
}else{ |
|
|
13347 |
utf8_printf(p->out, "%s", colSep); |
|
|
13348 |
} |
|
|
13349 |
} |
|
|
13350 |
if( p->cMode==MODE_Table ){ |
|
|
13351 |
print_row_separator(p, nColumn, "+"); |
|
|
13352 |
}else if( p->cMode==MODE_Box ){ |
|
|
13353 |
print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14); |
|
|
13354 |
} |
|
|
13355 |
columnar_end: |
|
|
13356 |
if( seenInterrupt ){ |
|
|
13357 |
utf8_printf(p->out, "Interrupt\n"); |
|
|
13358 |
} |
|
|
13359 |
nData = (nRow+1)*nColumn; |
|
|
13360 |
for(i=0; i |
|
|
13361 |
sqlite3_free(azData); |
|
|
13362 |
} |
|
|
13363 |
|
|
|
13364 |
/* |
|
|
13365 |
** Run a prepared statement |
|
|
13366 |
*/ |
|
|
13367 |
static void exec_prepared_stmt( |
|
|
13368 |
ShellState *pArg, /* Pointer to ShellState */ |
|
|
13369 |
sqlite3_stmt *pStmt /* Statment to run */ |
|
|
13370 |
){ |
|
|
13371 |
int rc; |
|
|
13372 |
|
|
|
13373 |
if( pArg->cMode==MODE_Column |
|
|
13374 |
|| pArg->cMode==MODE_Table |
|
|
13375 |
|| pArg->cMode==MODE_Box |
|
|
13376 |
|| pArg->cMode==MODE_Markdown |
|
|
13377 |
){ |
|
|
13378 |
exec_prepared_stmt_columnar(pArg, pStmt); |
|
|
13379 |
return; |
|
|
13380 |
} |
|
|
13381 |
|
|
|
13382 |
/* perform the first step. this will tell us if we |
|
|
13383 |
** have a result set or not and how wide it is. |
|
|
13384 |
*/ |
|
|
13385 |
rc = sqlite3_step(pStmt); |
|
|
13386 |
/* if we have a result set... */ |
|
|
13387 |
if( SQLITE_ROW == rc ){ |
|
|
13388 |
/* allocate space for col name ptr, value ptr, and type */ |
|
|
13389 |
int nCol = sqlite3_column_count(pStmt); |
|
|
13390 |
void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
|
|
13391 |
if( !pData ){ |
|
|
13392 |
rc = SQLITE_NOMEM; |
|
|
13393 |
}else{ |
|
|
13394 |
char **azCols = (char **)pData; /* Names of result columns */ |
|
|
13395 |
char **azVals = &azCols[nCol]; /* Results */ |
|
|
13396 |
int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
|
|
13397 |
int i, x; |
|
|
13398 |
assert(sizeof(int) <= sizeof(char *)); |
|
|
13399 |
/* save off ptrs to column names */ |
|
|
13400 |
for(i=0; i |
|
|
13401 |
azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
|
|
13402 |
} |
|
|
13403 |
do{ |
|
|
13404 |
/* extract the data and data types */ |
|
|
13405 |
for(i=0; i |
|
|
13406 |
aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
|
|
13407 |
if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
|
|
13408 |
azVals[i] = ""; |
|
|
13409 |
}else{ |
|
|
13410 |
azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
|
|
13411 |
} |
|
|
13412 |
if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
|
|
13413 |
rc = SQLITE_NOMEM; |
|
|
13414 |
break; /* from for */ |
|
|
13415 |
} |
|
|
13416 |
} /* end for */ |
|
|
13417 |
|
|
|
13418 |
/* if data and types extracted successfully... */ |
|
|
13419 |
if( SQLITE_ROW == rc ){ |
|
|
13420 |
/* call the supplied callback with the result row data */ |
|
|
13421 |
if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){ |
|
|
13422 |
rc = SQLITE_ABORT; |
|
|
13423 |
}else{ |
|
|
13424 |
rc = sqlite3_step(pStmt); |
|
|
13425 |
} |
|
|
13426 |
} |
|
|
13427 |
} while( SQLITE_ROW == rc ); |
|
|
13428 |
sqlite3_free(pData); |
|
|
13429 |
if( pArg->cMode==MODE_Json ){ |
|
|
13430 |
fputs("]\n", pArg->out); |
|
|
13431 |
} |
|
|
13432 |
} |
|
|
13433 |
} |
|
|
13434 |
} |
|
|
13435 |
|
|
|
13436 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
13437 |
/* |
|
|
13438 |
** This function is called to process SQL if the previous shell command |
|
|
13439 |
** was ".expert". It passes the SQL in the second argument directly to |
|
|
13440 |
** the sqlite3expert object. |
|
|
13441 |
** |
|
|
13442 |
** If successful, SQLITE_OK is returned. Otherwise, an SQLite error |
|
|
13443 |
** code. In this case, (*pzErr) may be set to point to a buffer containing |
|
|
13444 |
** an English language error message. It is the responsibility of the |
|
|
13445 |
** caller to eventually free this buffer using sqlite3_free(). |
|
|
13446 |
*/ |
|
|
13447 |
static int expertHandleSQL( |
|
|
13448 |
ShellState *pState, |
|
|
13449 |
const char *zSql, |
|
|
13450 |
char **pzErr |
|
|
13451 |
){ |
|
|
13452 |
assert( pState->expert.pExpert ); |
|
|
13453 |
assert( pzErr==0 || *pzErr==0 ); |
|
|
13454 |
return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); |
|
|
13455 |
} |
|
|
13456 |
|
|
|
13457 |
/* |
|
|
13458 |
** This function is called either to silently clean up the object |
|
|
13459 |
** created by the ".expert" command (if bCancel==1), or to generate a |
|
|
13460 |
** report from it and then clean it up (if bCancel==0). |
|
|
13461 |
** |
|
|
13462 |
** If successful, SQLITE_OK is returned. Otherwise, an SQLite error |
|
|
13463 |
** code. In this case, (*pzErr) may be set to point to a buffer containing |
|
|
13464 |
** an English language error message. It is the responsibility of the |
|
|
13465 |
** caller to eventually free this buffer using sqlite3_free(). |
|
|
13466 |
*/ |
|
|
13467 |
static int expertFinish( |
|
|
13468 |
ShellState *pState, |
|
|
13469 |
int bCancel, |
|
|
13470 |
char **pzErr |
|
|
13471 |
){ |
|
|
13472 |
int rc = SQLITE_OK; |
|
|
13473 |
sqlite3expert *p = pState->expert.pExpert; |
|
|
13474 |
assert( p ); |
|
|
13475 |
assert( bCancel || pzErr==0 || *pzErr==0 ); |
|
|
13476 |
if( bCancel==0 ){ |
|
|
13477 |
FILE *out = pState->out; |
|
|
13478 |
int bVerbose = pState->expert.bVerbose; |
|
|
13479 |
|
|
|
13480 |
rc = sqlite3_expert_analyze(p, pzErr); |
|
|
13481 |
if( rc==SQLITE_OK ){ |
|
|
13482 |
int nQuery = sqlite3_expert_count(p); |
|
|
13483 |
int i; |
|
|
13484 |
|
|
|
13485 |
if( bVerbose ){ |
|
|
13486 |
const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES); |
|
|
13487 |
raw_printf(out, "-- Candidates -----------------------------\n"); |
|
|
13488 |
raw_printf(out, "%s\n", zCand); |
|
|
13489 |
} |
|
|
13490 |
for(i=0; i |
|
|
13491 |
const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL); |
|
|
13492 |
const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES); |
|
|
13493 |
const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN); |
|
|
13494 |
if( zIdx==0 ) zIdx = "(no new indexes)\n"; |
|
|
13495 |
if( bVerbose ){ |
|
|
13496 |
raw_printf(out, "-- Query %d --------------------------------\n",i+1); |
|
|
13497 |
raw_printf(out, "%s\n\n", zSql); |
|
|
13498 |
} |
|
|
13499 |
raw_printf(out, "%s\n", zIdx); |
|
|
13500 |
raw_printf(out, "%s\n", zEQP); |
|
|
13501 |
} |
|
|
13502 |
} |
|
|
13503 |
} |
|
|
13504 |
sqlite3_expert_destroy(p); |
|
|
13505 |
pState->expert.pExpert = 0; |
|
|
13506 |
return rc; |
|
|
13507 |
} |
|
|
13508 |
|
|
|
13509 |
/* |
|
|
13510 |
** Implementation of ".expert" dot command. |
|
|
13511 |
*/ |
|
|
13512 |
static int expertDotCommand( |
|
|
13513 |
ShellState *pState, /* Current shell tool state */ |
|
|
13514 |
char **azArg, /* Array of arguments passed to dot command */ |
|
|
13515 |
int nArg /* Number of entries in azArg[] */ |
|
|
13516 |
){ |
|
|
13517 |
int rc = SQLITE_OK; |
|
|
13518 |
char *zErr = 0; |
|
|
13519 |
int i; |
|
|
13520 |
int iSample = 0; |
|
|
13521 |
|
|
|
13522 |
assert( pState->expert.pExpert==0 ); |
|
|
13523 |
memset(&pState->expert, 0, sizeof(ExpertInfo)); |
|
|
13524 |
|
|
|
13525 |
for(i=1; rc==SQLITE_OK && i |
|
|
13526 |
char *z = azArg[i]; |
|
|
13527 |
int n; |
|
|
13528 |
if( z[0]=='-' && z[1]=='-' ) z++; |
|
|
13529 |
n = strlen30(z); |
|
|
13530 |
if( n>=2 && 0==strncmp(z, "-verbose", n) ){ |
|
|
13531 |
pState->expert.bVerbose = 1; |
|
|
13532 |
} |
|
|
13533 |
else if( n>=2 && 0==strncmp(z, "-sample", n) ){ |
|
|
13534 |
if( i==(nArg-1) ){ |
|
|
13535 |
raw_printf(stderr, "option requires an argument: %s\n", z); |
|
|
13536 |
rc = SQLITE_ERROR; |
|
|
13537 |
}else{ |
|
|
13538 |
iSample = (int)integerValue(azArg[++i]); |
|
|
13539 |
if( iSample<0 || iSample>100 ){ |
|
|
13540 |
raw_printf(stderr, "value out of range: %s\n", azArg[i]); |
|
|
13541 |
rc = SQLITE_ERROR; |
|
|
13542 |
} |
|
|
13543 |
} |
|
|
13544 |
} |
|
|
13545 |
else{ |
|
|
13546 |
raw_printf(stderr, "unknown option: %s\n", z); |
|
|
13547 |
rc = SQLITE_ERROR; |
|
|
13548 |
} |
|
|
13549 |
} |
|
|
13550 |
|
|
|
13551 |
if( rc==SQLITE_OK ){ |
|
|
13552 |
pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); |
|
|
13553 |
if( pState->expert.pExpert==0 ){ |
|
|
13554 |
raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr); |
|
|
13555 |
rc = SQLITE_ERROR; |
|
|
13556 |
}else{ |
|
|
13557 |
sqlite3_expert_config( |
|
|
13558 |
pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample |
|
|
13559 |
); |
|
|
13560 |
} |
|
|
13561 |
} |
|
|
13562 |
|
|
|
13563 |
return rc; |
|
|
13564 |
} |
|
|
13565 |
#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ |
|
|
13566 |
|
|
|
13567 |
/* |
|
|
13568 |
** Execute a statement or set of statements. Print |
|
|
13569 |
** any result rows/columns depending on the current mode |
|
|
13570 |
** set via the supplied callback. |
|
|
13571 |
** |
|
|
13572 |
** This is very similar to SQLite's built-in sqlite3_exec() |
|
|
13573 |
** function except it takes a slightly different callback |
|
|
13574 |
** and callback data argument. |
|
|
13575 |
*/ |
|
|
13576 |
static int shell_exec( |
|
|
13577 |
ShellState *pArg, /* Pointer to ShellState */ |
|
|
13578 |
const char *zSql, /* SQL to be evaluated */ |
|
|
13579 |
char **pzErrMsg /* Error msg written here */ |
|
|
13580 |
){ |
|
|
13581 |
sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
|
|
13582 |
int rc = SQLITE_OK; /* Return Code */ |
|
|
13583 |
int rc2; |
|
|
13584 |
const char *zLeftover; /* Tail of unprocessed SQL */ |
|
|
13585 |
sqlite3 *db = pArg->db; |
|
|
13586 |
|
|
|
13587 |
if( pzErrMsg ){ |
|
|
13588 |
*pzErrMsg = NULL; |
|
|
13589 |
} |
|
|
13590 |
|
|
|
13591 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
13592 |
if( pArg->expert.pExpert ){ |
|
|
13593 |
rc = expertHandleSQL(pArg, zSql, pzErrMsg); |
|
|
13594 |
return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg); |
|
|
13595 |
} |
|
|
13596 |
#endif |
|
|
13597 |
|
|
|
13598 |
while( zSql[0] && (SQLITE_OK == rc) ){ |
|
|
13599 |
static const char *zStmtSql; |
|
|
13600 |
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
|
|
13601 |
if( SQLITE_OK != rc ){ |
|
|
13602 |
if( pzErrMsg ){ |
|
|
13603 |
*pzErrMsg = save_err_msg(db); |
|
|
13604 |
} |
|
|
13605 |
}else{ |
|
|
13606 |
if( !pStmt ){ |
|
|
13607 |
/* this happens for a comment or white-space */ |
|
|
13608 |
zSql = zLeftover; |
|
|
13609 |
while( IsSpace(zSql[0]) ) zSql++; |
|
|
13610 |
continue; |
|
|
13611 |
} |
|
|
13612 |
zStmtSql = sqlite3_sql(pStmt); |
|
|
13613 |
if( zStmtSql==0 ) zStmtSql = ""; |
|
|
13614 |
while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
|
|
13615 |
|
|
|
13616 |
/* save off the prepared statment handle and reset row count */ |
|
|
13617 |
if( pArg ){ |
|
|
13618 |
pArg->pStmt = pStmt; |
|
|
13619 |
pArg->cnt = 0; |
|
|
13620 |
} |
|
|
13621 |
|
|
|
13622 |
/* echo the sql statement if echo on */ |
|
|
13623 |
if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ |
|
|
13624 |
utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
|
|
13625 |
} |
|
|
13626 |
|
|
|
13627 |
/* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
|
|
13628 |
if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){ |
|
|
13629 |
sqlite3_stmt *pExplain; |
|
|
13630 |
char *zEQP; |
|
|
13631 |
int triggerEQP = 0; |
|
|
13632 |
disable_debug_trace_modes(); |
|
|
13633 |
sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); |
|
|
13634 |
if( pArg->autoEQP>=AUTOEQP_trigger ){ |
|
|
13635 |
sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0); |
|
|
13636 |
} |
|
|
13637 |
zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
|
|
13638 |
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
|
|
13639 |
if( rc==SQLITE_OK ){ |
|
|
13640 |
while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
|
|
13641 |
const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3); |
|
|
13642 |
int iEqpId = sqlite3_column_int(pExplain, 0); |
|
|
13643 |
int iParentId = sqlite3_column_int(pExplain, 1); |
|
|
13644 |
if( zEQPLine==0 ) zEQPLine = ""; |
|
|
13645 |
if( zEQPLine[0]=='-' ) eqp_render(pArg); |
|
|
13646 |
eqp_append(pArg, iEqpId, iParentId, zEQPLine); |
|
|
13647 |
} |
|
|
13648 |
eqp_render(pArg); |
|
|
13649 |
} |
|
|
13650 |
sqlite3_finalize(pExplain); |
|
|
13651 |
sqlite3_free(zEQP); |
|
|
13652 |
if( pArg->autoEQP>=AUTOEQP_full ){ |
|
|
13653 |
/* Also do an EXPLAIN for ".eqp full" mode */ |
|
|
13654 |
zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
|
|
13655 |
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
|
|
13656 |
if( rc==SQLITE_OK ){ |
|
|
13657 |
pArg->cMode = MODE_Explain; |
|
|
13658 |
explain_data_prepare(pArg, pExplain); |
|
|
13659 |
exec_prepared_stmt(pArg, pExplain); |
|
|
13660 |
explain_data_delete(pArg); |
|
|
13661 |
} |
|
|
13662 |
sqlite3_finalize(pExplain); |
|
|
13663 |
sqlite3_free(zEQP); |
|
|
13664 |
} |
|
|
13665 |
if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){ |
|
|
13666 |
sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0); |
|
|
13667 |
/* Reprepare pStmt before reactiving trace modes */ |
|
|
13668 |
sqlite3_finalize(pStmt); |
|
|
13669 |
sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
|
|
13670 |
if( pArg ) pArg->pStmt = pStmt; |
|
|
13671 |
} |
|
|
13672 |
restore_debug_trace_modes(); |
|
|
13673 |
} |
|
|
13674 |
|
|
|
13675 |
if( pArg ){ |
|
|
13676 |
pArg->cMode = pArg->mode; |
|
|
13677 |
if( pArg->autoExplain ){ |
|
|
13678 |
if( sqlite3_stmt_isexplain(pStmt)==1 ){ |
|
|
13679 |
pArg->cMode = MODE_Explain; |
|
|
13680 |
} |
|
|
13681 |
if( sqlite3_stmt_isexplain(pStmt)==2 ){ |
|
|
13682 |
pArg->cMode = MODE_EQP; |
|
|
13683 |
} |
|
|
13684 |
} |
|
|
13685 |
|
|
|
13686 |
/* If the shell is currently in ".explain" mode, gather the extra |
|
|
13687 |
** data required to add indents to the output.*/ |
|
|
13688 |
if( pArg->cMode==MODE_Explain ){ |
|
|
13689 |
explain_data_prepare(pArg, pStmt); |
|
|
13690 |
} |
|
|
13691 |
} |
|
|
13692 |
|
|
|
13693 |
bind_prepared_stmt(pArg, pStmt); |
|
|
13694 |
exec_prepared_stmt(pArg, pStmt); |
|
|
13695 |
explain_data_delete(pArg); |
|
|
13696 |
eqp_render(pArg); |
|
|
13697 |
|
|
|
13698 |
/* print usage stats if stats on */ |
|
|
13699 |
if( pArg && pArg->statsOn ){ |
|
|
13700 |
display_stats(db, pArg, 0); |
|
|
13701 |
} |
|
|
13702 |
|
|
|
13703 |
/* print loop-counters if required */ |
|
|
13704 |
if( pArg && pArg->scanstatsOn ){ |
|
|
13705 |
display_scanstats(db, pArg); |
|
|
13706 |
} |
|
|
13707 |
|
|
|
13708 |
/* Finalize the statement just executed. If this fails, save a |
|
|
13709 |
** copy of the error message. Otherwise, set zSql to point to the |
|
|
13710 |
** next statement to execute. */ |
|
|
13711 |
rc2 = sqlite3_finalize(pStmt); |
|
|
13712 |
if( rc!=SQLITE_NOMEM ) rc = rc2; |
|
|
13713 |
if( rc==SQLITE_OK ){ |
|
|
13714 |
zSql = zLeftover; |
|
|
13715 |
while( IsSpace(zSql[0]) ) zSql++; |
|
|
13716 |
}else if( pzErrMsg ){ |
|
|
13717 |
*pzErrMsg = save_err_msg(db); |
|
|
13718 |
} |
|
|
13719 |
|
|
|
13720 |
/* clear saved stmt handle */ |
|
|
13721 |
if( pArg ){ |
|
|
13722 |
pArg->pStmt = NULL; |
|
|
13723 |
} |
|
|
13724 |
} |
|
|
13725 |
} /* end while */ |
|
|
13726 |
|
|
|
13727 |
return rc; |
|
|
13728 |
} |
|
|
13729 |
|
|
|
13730 |
/* |
|
|
13731 |
** Release memory previously allocated by tableColumnList(). |
|
|
13732 |
*/ |
|
|
13733 |
static void freeColumnList(char **azCol){ |
|
|
13734 |
int i; |
|
|
13735 |
for(i=1; azCol[i]; i++){ |
|
|
13736 |
sqlite3_free(azCol[i]); |
|
|
13737 |
} |
|
|
13738 |
/* azCol[0] is a static string */ |
|
|
13739 |
sqlite3_free(azCol); |
|
|
13740 |
} |
|
|
13741 |
|
|
|
13742 |
/* |
|
|
13743 |
** Return a list of pointers to strings which are the names of all |
|
|
13744 |
** columns in table zTab. The memory to hold the names is dynamically |
|
|
13745 |
** allocated and must be released by the caller using a subsequent call |
|
|
13746 |
** to freeColumnList(). |
|
|
13747 |
** |
|
|
13748 |
** The azCol[0] entry is usually NULL. However, if zTab contains a rowid |
|
|
13749 |
** value that needs to be preserved, then azCol[0] is filled in with the |
|
|
13750 |
** name of the rowid column. |
|
|
13751 |
** |
|
|
13752 |
** The first regular column in the table is azCol[1]. The list is terminated |
|
|
13753 |
** by an entry with azCol[i]==0. |
|
|
13754 |
*/ |
|
|
13755 |
static char **tableColumnList(ShellState *p, const char *zTab){ |
|
|
13756 |
char **azCol = 0; |
|
|
13757 |
sqlite3_stmt *pStmt; |
|
|
13758 |
char *zSql; |
|
|
13759 |
int nCol = 0; |
|
|
13760 |
int nAlloc = 0; |
|
|
13761 |
int nPK = 0; /* Number of PRIMARY KEY columns seen */ |
|
|
13762 |
int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */ |
|
|
13763 |
int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid); |
|
|
13764 |
int rc; |
|
|
13765 |
|
|
|
13766 |
zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab); |
|
|
13767 |
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
|
13768 |
sqlite3_free(zSql); |
|
|
13769 |
if( rc ) return 0; |
|
|
13770 |
while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
13771 |
if( nCol>=nAlloc-2 ){ |
|
|
13772 |
nAlloc = nAlloc*2 + nCol + 10; |
|
|
13773 |
azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0])); |
|
|
13774 |
if( azCol==0 ) shell_out_of_memory(); |
|
|
13775 |
} |
|
|
13776 |
azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1)); |
|
|
13777 |
if( sqlite3_column_int(pStmt, 5) ){ |
|
|
13778 |
nPK++; |
|
|
13779 |
if( nPK==1 |
|
|
13780 |
&& sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2), |
|
|
13781 |
"INTEGER")==0 |
|
|
13782 |
){ |
|
|
13783 |
isIPK = 1; |
|
|
13784 |
}else{ |
|
|
13785 |
isIPK = 0; |
|
|
13786 |
} |
|
|
13787 |
} |
|
|
13788 |
} |
|
|
13789 |
sqlite3_finalize(pStmt); |
|
|
13790 |
if( azCol==0 ) return 0; |
|
|
13791 |
azCol[0] = 0; |
|
|
13792 |
azCol[nCol+1] = 0; |
|
|
13793 |
|
|
|
13794 |
/* The decision of whether or not a rowid really needs to be preserved |
|
|
13795 |
** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table |
|
|
13796 |
** or a table with an INTEGER PRIMARY KEY. We are unable to preserve |
|
|
13797 |
** rowids on tables where the rowid is inaccessible because there are other |
|
|
13798 |
** columns in the table named "rowid", "_rowid_", and "oid". |
|
|
13799 |
*/ |
|
|
13800 |
if( preserveRowid && isIPK ){ |
|
|
13801 |
/* If a single PRIMARY KEY column with type INTEGER was seen, then it |
|
|
13802 |
** might be an alise for the ROWID. But it might also be a WITHOUT ROWID |
|
|
13803 |
** table or a INTEGER PRIMARY KEY DESC column, neither of which are |
|
|
13804 |
** ROWID aliases. To distinguish these cases, check to see if |
|
|
13805 |
** there is a "pk" entry in "PRAGMA index_list". There will be |
|
|
13806 |
** no "pk" index if the PRIMARY KEY really is an alias for the ROWID. |
|
|
13807 |
*/ |
|
|
13808 |
zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)" |
|
|
13809 |
" WHERE origin='pk'", zTab); |
|
|
13810 |
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
|
13811 |
sqlite3_free(zSql); |
|
|
13812 |
if( rc ){ |
|
|
13813 |
freeColumnList(azCol); |
|
|
13814 |
return 0; |
|
|
13815 |
} |
|
|
13816 |
rc = sqlite3_step(pStmt); |
|
|
13817 |
sqlite3_finalize(pStmt); |
|
|
13818 |
preserveRowid = rc==SQLITE_ROW; |
|
|
13819 |
} |
|
|
13820 |
if( preserveRowid ){ |
|
|
13821 |
/* Only preserve the rowid if we can find a name to use for the |
|
|
13822 |
** rowid */ |
|
|
13823 |
static char *azRowid[] = { "rowid", "_rowid_", "oid" }; |
|
|
13824 |
int i, j; |
|
|
13825 |
for(j=0; j<3; j++){ |
|
|
13826 |
for(i=1; i<=nCol; i++){ |
|
|
13827 |
if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break; |
|
|
13828 |
} |
|
|
13829 |
if( i>nCol ){ |
|
|
13830 |
/* At this point, we know that azRowid[j] is not the name of any |
|
|
13831 |
** ordinary column in the table. Verify that azRowid[j] is a valid |
|
|
13832 |
** name for the rowid before adding it to azCol[0]. WITHOUT ROWID |
|
|
13833 |
** tables will fail this last check */ |
|
|
13834 |
rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); |
|
|
13835 |
if( rc==SQLITE_OK ) azCol[0] = azRowid[j]; |
|
|
13836 |
break; |
|
|
13837 |
} |
|
|
13838 |
} |
|
|
13839 |
} |
|
|
13840 |
return azCol; |
|
|
13841 |
} |
|
|
13842 |
|
|
|
13843 |
/* |
|
|
13844 |
** Toggle the reverse_unordered_selects setting. |
|
|
13845 |
*/ |
|
|
13846 |
static void toggleSelectOrder(sqlite3 *db){ |
|
|
13847 |
sqlite3_stmt *pStmt = 0; |
|
|
13848 |
int iSetting = 0; |
|
|
13849 |
char zStmt[100]; |
|
|
13850 |
sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); |
|
|
13851 |
if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
13852 |
iSetting = sqlite3_column_int(pStmt, 0); |
|
|
13853 |
} |
|
|
13854 |
sqlite3_finalize(pStmt); |
|
|
13855 |
sqlite3_snprintf(sizeof(zStmt), zStmt, |
|
|
13856 |
"PRAGMA reverse_unordered_selects(%d)", !iSetting); |
|
|
13857 |
sqlite3_exec(db, zStmt, 0, 0, 0); |
|
|
13858 |
} |
|
|
13859 |
|
|
|
13860 |
/* |
|
|
13861 |
** This is a different callback routine used for dumping the database. |
|
|
13862 |
** Each row received by this callback consists of a table name, |
|
|
13863 |
** the table type ("index" or "table") and SQL to create the table. |
|
|
13864 |
** This routine should print text sufficient to recreate the table. |
|
|
13865 |
*/ |
|
|
13866 |
static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){ |
|
|
13867 |
int rc; |
|
|
13868 |
const char *zTable; |
|
|
13869 |
const char *zType; |
|
|
13870 |
const char *zSql; |
|
|
13871 |
ShellState *p = (ShellState *)pArg; |
|
|
13872 |
int dataOnly; |
|
|
13873 |
int noSys; |
|
|
13874 |
|
|
|
13875 |
UNUSED_PARAMETER(azNotUsed); |
|
|
13876 |
if( nArg!=3 || azArg==0 ) return 0; |
|
|
13877 |
zTable = azArg[0]; |
|
|
13878 |
zType = azArg[1]; |
|
|
13879 |
zSql = azArg[2]; |
|
|
13880 |
dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0; |
|
|
13881 |
noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0; |
|
|
13882 |
|
|
|
13883 |
if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){ |
|
|
13884 |
if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); |
|
|
13885 |
}else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){ |
|
|
13886 |
if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n"); |
|
|
13887 |
}else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
|
|
13888 |
return 0; |
|
|
13889 |
}else if( dataOnly ){ |
|
|
13890 |
/* no-op */ |
|
|
13891 |
}else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
|
|
13892 |
char *zIns; |
|
|
13893 |
if( !p->writableSchema ){ |
|
|
13894 |
raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
|
|
13895 |
p->writableSchema = 1; |
|
|
13896 |
} |
|
|
13897 |
zIns = sqlite3_mprintf( |
|
|
13898 |
"INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)" |
|
|
13899 |
"VALUES('table','%q','%q',0,'%q');", |
|
|
13900 |
zTable, zTable, zSql); |
|
|
13901 |
utf8_printf(p->out, "%s\n", zIns); |
|
|
13902 |
sqlite3_free(zIns); |
|
|
13903 |
return 0; |
|
|
13904 |
}else{ |
|
|
13905 |
printSchemaLine(p->out, zSql, ";\n"); |
|
|
13906 |
} |
|
|
13907 |
|
|
|
13908 |
if( strcmp(zType, "table")==0 ){ |
|
|
13909 |
ShellText sSelect; |
|
|
13910 |
ShellText sTable; |
|
|
13911 |
char **azCol; |
|
|
13912 |
int i; |
|
|
13913 |
char *savedDestTable; |
|
|
13914 |
int savedMode; |
|
|
13915 |
|
|
|
13916 |
azCol = tableColumnList(p, zTable); |
|
|
13917 |
if( azCol==0 ){ |
|
|
13918 |
p->nErr++; |
|
|
13919 |
return 0; |
|
|
13920 |
} |
|
|
13921 |
|
|
|
13922 |
/* Always quote the table name, even if it appears to be pure ascii, |
|
|
13923 |
** in case it is a keyword. Ex: INSERT INTO "table" ... */ |
|
|
13924 |
initText(&sTable); |
|
|
13925 |
appendText(&sTable, zTable, quoteChar(zTable)); |
|
|
13926 |
/* If preserving the rowid, add a column list after the table name. |
|
|
13927 |
** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)" |
|
|
13928 |
** instead of the usual "INSERT INTO tab VALUES(...)". |
|
|
13929 |
*/ |
|
|
13930 |
if( azCol[0] ){ |
|
|
13931 |
appendText(&sTable, "(", 0); |
|
|
13932 |
appendText(&sTable, azCol[0], 0); |
|
|
13933 |
for(i=1; azCol[i]; i++){ |
|
|
13934 |
appendText(&sTable, ",", 0); |
|
|
13935 |
appendText(&sTable, azCol[i], quoteChar(azCol[i])); |
|
|
13936 |
} |
|
|
13937 |
appendText(&sTable, ")", 0); |
|
|
13938 |
} |
|
|
13939 |
|
|
|
13940 |
/* Build an appropriate SELECT statement */ |
|
|
13941 |
initText(&sSelect); |
|
|
13942 |
appendText(&sSelect, "SELECT ", 0); |
|
|
13943 |
if( azCol[0] ){ |
|
|
13944 |
appendText(&sSelect, azCol[0], 0); |
|
|
13945 |
appendText(&sSelect, ",", 0); |
|
|
13946 |
} |
|
|
13947 |
for(i=1; azCol[i]; i++){ |
|
|
13948 |
appendText(&sSelect, azCol[i], quoteChar(azCol[i])); |
|
|
13949 |
if( azCol[i+1] ){ |
|
|
13950 |
appendText(&sSelect, ",", 0); |
|
|
13951 |
} |
|
|
13952 |
} |
|
|
13953 |
freeColumnList(azCol); |
|
|
13954 |
appendText(&sSelect, " FROM ", 0); |
|
|
13955 |
appendText(&sSelect, zTable, quoteChar(zTable)); |
|
|
13956 |
|
|
|
13957 |
savedDestTable = p->zDestTable; |
|
|
13958 |
savedMode = p->mode; |
|
|
13959 |
p->zDestTable = sTable.z; |
|
|
13960 |
p->mode = p->cMode = MODE_Insert; |
|
|
13961 |
rc = shell_exec(p, sSelect.z, 0); |
|
|
13962 |
if( (rc&0xff)==SQLITE_CORRUPT ){ |
|
|
13963 |
raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
|
|
13964 |
toggleSelectOrder(p->db); |
|
|
13965 |
shell_exec(p, sSelect.z, 0); |
|
|
13966 |
toggleSelectOrder(p->db); |
|
|
13967 |
} |
|
|
13968 |
p->zDestTable = savedDestTable; |
|
|
13969 |
p->mode = savedMode; |
|
|
13970 |
freeText(&sTable); |
|
|
13971 |
freeText(&sSelect); |
|
|
13972 |
if( rc ) p->nErr++; |
|
|
13973 |
} |
|
|
13974 |
return 0; |
|
|
13975 |
} |
|
|
13976 |
|
|
|
13977 |
/* |
|
|
13978 |
** Run zQuery. Use dump_callback() as the callback routine so that |
|
|
13979 |
** the contents of the query are output as SQL statements. |
|
|
13980 |
** |
|
|
13981 |
** If we get a SQLITE_CORRUPT error, rerun the query after appending |
|
|
13982 |
** "ORDER BY rowid DESC" to the end. |
|
|
13983 |
*/ |
|
|
13984 |
static int run_schema_dump_query( |
|
|
13985 |
ShellState *p, |
|
|
13986 |
const char *zQuery |
|
|
13987 |
){ |
|
|
13988 |
int rc; |
|
|
13989 |
char *zErr = 0; |
|
|
13990 |
rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); |
|
|
13991 |
if( rc==SQLITE_CORRUPT ){ |
|
|
13992 |
char *zQ2; |
|
|
13993 |
int len = strlen30(zQuery); |
|
|
13994 |
raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
|
|
13995 |
if( zErr ){ |
|
|
13996 |
utf8_printf(p->out, "/****** %s ******/\n", zErr); |
|
|
13997 |
sqlite3_free(zErr); |
|
|
13998 |
zErr = 0; |
|
|
13999 |
} |
|
|
14000 |
zQ2 = malloc( len+100 ); |
|
|
14001 |
if( zQ2==0 ) return rc; |
|
|
14002 |
sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
|
|
14003 |
rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
|
|
14004 |
if( rc ){ |
|
|
14005 |
utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
|
|
14006 |
}else{ |
|
|
14007 |
rc = SQLITE_CORRUPT; |
|
|
14008 |
} |
|
|
14009 |
sqlite3_free(zErr); |
|
|
14010 |
free(zQ2); |
|
|
14011 |
} |
|
|
14012 |
return rc; |
|
|
14013 |
} |
|
|
14014 |
|
|
|
14015 |
/* |
|
|
14016 |
** Text of help messages. |
|
|
14017 |
** |
|
|
14018 |
** The help text for each individual command begins with a line that starts |
|
|
14019 |
** with ".". Subsequent lines are supplimental information. |
|
|
14020 |
** |
|
|
14021 |
** There must be two or more spaces between the end of the command and the |
|
|
14022 |
** start of the description of what that command does. |
|
|
14023 |
*/ |
|
|
14024 |
static const char *(azHelp[]) = { |
|
|
14025 |
#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
|
|
14026 |
".archive ... Manage SQL archives", |
|
|
14027 |
" Each command must have exactly one of the following options:", |
|
|
14028 |
" -c, --create Create a new archive", |
|
|
14029 |
" -u, --update Add or update files with changed mtime", |
|
|
14030 |
" -i, --insert Like -u but always add even if unchanged", |
|
|
14031 |
" -t, --list List contents of archive", |
|
|
14032 |
" -x, --extract Extract files from archive", |
|
|
14033 |
" Optional arguments:", |
|
|
14034 |
" -v, --verbose Print each filename as it is processed", |
|
|
14035 |
" -f FILE, --file FILE Use archive FILE (default is current db)", |
|
|
14036 |
" -a FILE, --append FILE Open FILE using the apndvfs VFS", |
|
|
14037 |
" -C DIR, --directory DIR Read/extract files from directory DIR", |
|
|
14038 |
" -n, --dryrun Show the SQL that would have occurred", |
|
|
14039 |
" Examples:", |
|
|
14040 |
" .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar", |
|
|
14041 |
" .ar -tf ARCHIVE # List members of ARCHIVE", |
|
|
14042 |
" .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE", |
|
|
14043 |
" See also:", |
|
|
14044 |
" http://sqlite.org/cli.html#sqlar_archive_support", |
|
|
14045 |
#endif |
|
|
14046 |
#ifndef SQLITE_OMIT_AUTHORIZATION |
|
|
14047 |
".auth ON|OFF Show authorizer callbacks", |
|
|
14048 |
#endif |
|
|
14049 |
".backup ?DB? FILE Backup DB (default \"main\") to FILE", |
|
|
14050 |
" --append Use the appendvfs", |
|
|
14051 |
" --async Write to FILE without journal and fsync()", |
|
|
14052 |
".bail on|off Stop after hitting an error. Default OFF", |
|
|
14053 |
".binary on|off Turn binary output on or off. Default OFF", |
|
|
14054 |
".cd DIRECTORY Change the working directory to DIRECTORY", |
|
|
14055 |
".changes on|off Show number of rows changed by SQL", |
|
|
14056 |
".check GLOB Fail if output since .testcase does not match", |
|
|
14057 |
".clone NEWDB Clone data into NEWDB from the existing database", |
|
|
14058 |
".databases List names and files of attached databases", |
|
|
14059 |
".dbconfig ?op? ?val? List or change sqlite3_db_config() options", |
|
|
14060 |
".dbinfo ?DB? Show status information about the database", |
|
|
14061 |
".dump ?OBJECTS? Render database content as SQL", |
|
|
14062 |
" Options:", |
|
|
14063 |
" --data-only Output only INSERT statements", |
|
|
14064 |
" --newlines Allow unescaped newline characters in output", |
|
|
14065 |
" --nosys Omit system tables (ex: \"sqlite_stat1\")", |
|
|
14066 |
" --preserve-rowids Include ROWID values in the output", |
|
|
14067 |
" OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump", |
|
|
14068 |
" Additional LIKE patterns can be given in subsequent arguments", |
|
|
14069 |
".echo on|off Turn command echo on or off", |
|
|
14070 |
".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN", |
|
|
14071 |
" Other Modes:", |
|
|
14072 |
#ifdef SQLITE_DEBUG |
|
|
14073 |
" test Show raw EXPLAIN QUERY PLAN output", |
|
|
14074 |
" trace Like \"full\" but enable \"PRAGMA vdbe_trace\"", |
|
|
14075 |
#endif |
|
|
14076 |
" trigger Like \"full\" but also show trigger bytecode", |
|
|
14077 |
".excel Display the output of next command in spreadsheet", |
|
|
14078 |
" --bom Put a UTF8 byte-order mark on intermediate file", |
|
|
14079 |
".exit ?CODE? Exit this program with return-code CODE", |
|
|
14080 |
".expert EXPERIMENTAL. Suggest indexes for queries", |
|
|
14081 |
".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto", |
|
|
14082 |
".filectrl CMD ... Run various sqlite3_file_control() operations", |
|
|
14083 |
" --schema SCHEMA Use SCHEMA instead of \"main\"", |
|
|
14084 |
" --help Show CMD details", |
|
|
14085 |
".fullschema ?--indent? Show schema and the content of sqlite_stat tables", |
|
|
14086 |
".headers on|off Turn display of headers on or off", |
|
|
14087 |
".help ?-all? ?PATTERN? Show help text for PATTERN", |
|
|
14088 |
".import FILE TABLE Import data from FILE into TABLE", |
|
|
14089 |
" Options:", |
|
|
14090 |
" --ascii Use \\037 and \\036 as column and row separators", |
|
|
14091 |
" --csv Use , and \\n as column and row separators", |
|
|
14092 |
" --skip N Skip the first N rows of input", |
|
|
14093 |
" -v \"Verbose\" - increase auxiliary output", |
|
|
14094 |
" Notes:", |
|
|
14095 |
" * If TABLE does not exist, it is created. The first row of input", |
|
|
14096 |
" determines the column names.", |
|
|
14097 |
" * If neither --csv or --ascii are used, the input mode is derived", |
|
|
14098 |
" from the \".mode\" output mode", |
|
|
14099 |
" * If FILE begins with \"|\" then it is a command that generates the", |
|
|
14100 |
" input text.", |
|
|
14101 |
#ifndef SQLITE_OMIT_TEST_CONTROL |
|
|
14102 |
".imposter INDEX TABLE Create imposter table TABLE on index INDEX", |
|
|
14103 |
#endif |
|
|
14104 |
".indexes ?TABLE? Show names of indexes", |
|
|
14105 |
" If TABLE is specified, only show indexes for", |
|
|
14106 |
" tables matching TABLE using the LIKE operator.", |
|
|
14107 |
#ifdef SQLITE_ENABLE_IOTRACE |
|
|
14108 |
".iotrace FILE Enable I/O diagnostic logging to FILE", |
|
|
14109 |
#endif |
|
|
14110 |
".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT", |
|
|
14111 |
".lint OPTIONS Report potential schema issues.", |
|
|
14112 |
" Options:", |
|
|
14113 |
" fkey-indexes Find missing foreign key indexes", |
|
|
14114 |
#ifndef SQLITE_OMIT_LOAD_EXTENSION |
|
|
14115 |
".load FILE ?ENTRY? Load an extension library", |
|
|
14116 |
#endif |
|
|
14117 |
".log FILE|off Turn logging on or off. FILE can be stderr/stdout", |
|
|
14118 |
".mode MODE ?TABLE? Set output mode", |
|
|
14119 |
" MODE is one of:", |
|
|
14120 |
" ascii Columns/rows delimited by 0x1F and 0x1E", |
|
|
14121 |
" box Tables using unicode box-drawing characters", |
|
|
14122 |
" csv Comma-separated values", |
|
|
14123 |
" column Output in columns. (See .width)", |
|
|
14124 |
" html HTML code",
|
|
14125 |
" insert SQL insert statements for TABLE", |
|
|
14126 |
" json Results in a JSON array", |
|
|
14127 |
" line One value per line", |
|
|
14128 |
" list Values delimited by \"|\"", |
|
|
14129 |
" markdown Markdown table format", |
|
|
14130 |
" quote Escape answers as for SQL", |
|
|
14131 |
" table ASCII-art table", |
|
|
14132 |
" tabs Tab-separated values", |
|
|
14133 |
" tcl TCL list elements", |
|
|
14134 |
".nullvalue STRING Use STRING in place of NULL values", |
|
|
14135 |
".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE", |
|
|
14136 |
" If FILE begins with '|' then open as a pipe", |
|
|
14137 |
" --bom Put a UTF8 byte-order mark at the beginning", |
|
|
14138 |
" -e Send output to the system text editor", |
|
|
14139 |
" -x Send output as CSV to a spreadsheet (same as \".excel\")", |
|
|
14140 |
#ifdef SQLITE_DEBUG |
|
|
14141 |
".oom ?--repeat M? ?N? Simulate an OOM error on the N-th allocation", |
|
|
14142 |
#endif |
|
|
14143 |
".open ?OPTIONS? ?FILE? Close existing database and reopen FILE", |
|
|
14144 |
" Options:", |
|
|
14145 |
" --append Use appendvfs to append database to the end of FILE", |
|
|
14146 |
#ifdef SQLITE_ENABLE_DESERIALIZE |
|
|
14147 |
" --deserialize Load into memory useing sqlite3_deserialize()", |
|
|
14148 |
" --hexdb Load the output of \"dbtotxt\" as an in-memory db", |
|
|
14149 |
" --maxsize N Maximum size for --hexdb or --deserialized database", |
|
|
14150 |
#endif |
|
|
14151 |
" --new Initialize FILE to an empty database", |
|
|
14152 |
" --nofollow Do not follow symbolic links", |
|
|
14153 |
" --readonly Open FILE readonly", |
|
|
14154 |
" --zip FILE is a ZIP archive", |
|
|
14155 |
".output ?FILE? Send output to FILE or stdout if FILE is omitted", |
|
|
14156 |
" If FILE begins with '|' then open it as a pipe.", |
|
|
14157 |
" Options:", |
|
|
14158 |
" --bom Prefix output with a UTF8 byte-order mark", |
|
|
14159 |
" -e Send output to the system text editor", |
|
|
14160 |
" -x Send output as CSV to a spreadsheet", |
|
|
14161 |
".parameter CMD ... Manage SQL parameter bindings", |
|
|
14162 |
" clear Erase all bindings", |
|
|
14163 |
" init Initialize the TEMP table that holds bindings", |
|
|
14164 |
" list List the current parameter bindings", |
|
|
14165 |
" set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE", |
|
|
14166 |
" PARAMETER should start with one of: $ : @ ?", |
|
|
14167 |
" unset PARAMETER Remove PARAMETER from the binding table", |
|
|
14168 |
".print STRING... Print literal STRING", |
|
|
14169 |
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
|
|
14170 |
".progress N Invoke progress handler after every N opcodes", |
|
|
14171 |
" --limit N Interrupt after N progress callbacks", |
|
|
14172 |
" --once Do no more than one progress interrupt", |
|
|
14173 |
" --quiet|-q No output except at interrupts", |
|
|
14174 |
" --reset Reset the count for each input and interrupt", |
|
|
14175 |
#endif |
|
|
14176 |
".prompt MAIN CONTINUE Replace the standard prompts", |
|
|
14177 |
".quit Exit this program", |
|
|
14178 |
".read FILE Read input from FILE", |
|
|
14179 |
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
|
|
14180 |
".recover Recover as much data as possible from corrupt db.", |
|
|
14181 |
" --freelist-corrupt Assume the freelist is corrupt", |
|
|
14182 |
" --recovery-db NAME Store recovery metadata in database file NAME", |
|
|
14183 |
" --lost-and-found TABLE Alternative name for the lost-and-found table", |
|
|
14184 |
" --no-rowids Do not attempt to recover rowid values", |
|
|
14185 |
" that are not also INTEGER PRIMARY KEYs", |
|
|
14186 |
#endif |
|
|
14187 |
".restore ?DB? FILE Restore content of DB (default \"main\") from FILE", |
|
|
14188 |
".save FILE Write in-memory database into FILE", |
|
|
14189 |
".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off", |
|
|
14190 |
".schema ?PATTERN? Show the CREATE statements matching PATTERN", |
|
|
14191 |
" Options:", |
|
|
14192 |
" --indent Try to pretty-print the schema", |
|
|
14193 |
" --nosys Omit objects whose names start with \"sqlite_\"", |
|
|
14194 |
".selftest ?OPTIONS? Run tests defined in the SELFTEST table", |
|
|
14195 |
" Options:", |
|
|
14196 |
" --init Create a new SELFTEST table", |
|
|
14197 |
" -v Verbose output", |
|
|
14198 |
".separator COL ?ROW? Change the column and row separators", |
|
|
14199 |
#if defined(SQLITE_ENABLE_SESSION) |
|
|
14200 |
".session ?NAME? CMD ... Create or control sessions", |
|
|
14201 |
" Subcommands:", |
|
|
14202 |
" attach TABLE Attach TABLE", |
|
|
14203 |
" changeset FILE Write a changeset into FILE", |
|
|
14204 |
" close Close one session", |
|
|
14205 |
" enable ?BOOLEAN? Set or query the enable bit", |
|
|
14206 |
" filter GLOB... Reject tables matching GLOBs", |
|
|
14207 |
" indirect ?BOOLEAN? Mark or query the indirect status", |
|
|
14208 |
" isempty Query whether the session is empty", |
|
|
14209 |
" list List currently open session names", |
|
|
14210 |
" open DB NAME Open a new session on DB", |
|
|
14211 |
" patchset FILE Write a patchset into FILE", |
|
|
14212 |
" If ?NAME? is omitted, the first defined session is used.", |
|
|
14213 |
#endif |
|
|
14214 |
".sha3sum ... Compute a SHA3 hash of database content", |
|
|
14215 |
" Options:", |
|
|
14216 |
" --schema Also hash the sqlite_schema table", |
|
|
14217 |
" --sha3-224 Use the sha3-224 algorithm", |
|
|
14218 |
" --sha3-256 Use the sha3-256 algorithm (default)", |
|
|
14219 |
" --sha3-384 Use the sha3-384 algorithm", |
|
|
14220 |
" --sha3-512 Use the sha3-512 algorithm", |
|
|
14221 |
" Any other argument is a LIKE pattern for tables to hash", |
|
|
14222 |
#ifndef SQLITE_NOHAVE_SYSTEM |
|
|
14223 |
".shell CMD ARGS... Run CMD ARGS... in a system shell", |
|
|
14224 |
#endif |
|
|
14225 |
".show Show the current values for various settings", |
|
|
14226 |
".stats ?ARG? Show stats or turn stats on or off", |
|
|
14227 |
" off Turn off automatic stat display", |
|
|
14228 |
" on Turn on automatic stat display", |
|
|
14229 |
" stmt Show statement stats", |
|
|
14230 |
" vmstep Show the virtual machine step count only", |
|
|
14231 |
#ifndef SQLITE_NOHAVE_SYSTEM |
|
|
14232 |
".system CMD ARGS... Run CMD ARGS... in a system shell", |
|
|
14233 |
#endif |
|
|
14234 |
".tables ?TABLE? List names of tables matching LIKE pattern TABLE", |
|
|
14235 |
".testcase NAME Begin redirecting output to 'testcase-out.txt'", |
|
|
14236 |
".testctrl CMD ... Run various sqlite3_test_control() operations", |
|
|
14237 |
" Run \".testctrl\" with no arguments for details", |
|
|
14238 |
".timeout MS Try opening locked tables for MS milliseconds", |
|
|
14239 |
".timer on|off Turn SQL timer on or off", |
|
|
14240 |
#ifndef SQLITE_OMIT_TRACE |
|
|
14241 |
".trace ?OPTIONS? Output each SQL statement as it is run", |
|
|
14242 |
" FILE Send output to FILE", |
|
|
14243 |
" stdout Send output to stdout", |
|
|
14244 |
" stderr Send output to stderr", |
|
|
14245 |
" off Disable tracing", |
|
|
14246 |
" --expanded Expand query parameters", |
|
|
14247 |
#ifdef SQLITE_ENABLE_NORMALIZE |
|
|
14248 |
" --normalized Normal the SQL statements", |
|
|
14249 |
#endif |
|
|
14250 |
" --plain Show SQL as it is input", |
|
|
14251 |
" --stmt Trace statement execution (SQLITE_TRACE_STMT)", |
|
|
14252 |
" --profile Profile statements (SQLITE_TRACE_PROFILE)", |
|
|
14253 |
" --row Trace each row (SQLITE_TRACE_ROW)", |
|
|
14254 |
" --close Trace connection close (SQLITE_TRACE_CLOSE)", |
|
|
14255 |
#endif /* SQLITE_OMIT_TRACE */ |
|
|
14256 |
#ifdef SQLITE_DEBUG |
|
|
14257 |
".unmodule NAME ... Unregister virtual table modules", |
|
|
14258 |
" --allexcept Unregister everything except those named", |
|
|
14259 |
#endif |
|
|
14260 |
".vfsinfo ?AUX? Information about the top-level VFS", |
|
|
14261 |
".vfslist List all available VFSes", |
|
|
14262 |
".vfsname ?AUX? Print the name of the VFS stack", |
|
|
14263 |
".width NUM1 NUM2 ... Set minimum column widths for columnar output", |
|
|
14264 |
" Negative values right-justify", |
|
|
14265 |
}; |
|
|
14266 |
|
|
|
14267 |
/* |
|
|
14268 |
** Output help text. |
|
|
14269 |
** |
|
|
14270 |
** zPattern describes the set of commands for which help text is provided. |
|
|
14271 |
** If zPattern is NULL, then show all commands, but only give a one-line |
|
|
14272 |
** description of each. |
|
|
14273 |
** |
|
|
14274 |
** Return the number of matches. |
|
|
14275 |
*/ |
|
|
14276 |
static int showHelp(FILE *out, const char *zPattern){ |
|
|
14277 |
int i = 0; |
|
|
14278 |
int j = 0; |
|
|
14279 |
int n = 0; |
|
|
14280 |
char *zPat; |
|
|
14281 |
if( zPattern==0 |
|
|
14282 |
|| zPattern[0]=='0' |
|
|
14283 |
|| strcmp(zPattern,"-a")==0 |
|
|
14284 |
|| strcmp(zPattern,"-all")==0 |
|
|
14285 |
|| strcmp(zPattern,"--all")==0 |
|
|
14286 |
){ |
|
|
14287 |
/* Show all commands, but only one line per command */ |
|
|
14288 |
if( zPattern==0 ) zPattern = ""; |
|
|
14289 |
for(i=0; i |
|
|
14290 |
if( azHelp[i][0]=='.' || zPattern[0] ){ |
|
|
14291 |
utf8_printf(out, "%s\n", azHelp[i]); |
|
|
14292 |
n++; |
|
|
14293 |
} |
|
|
14294 |
} |
|
|
14295 |
}else{ |
|
|
14296 |
/* Look for commands that for which zPattern is an exact prefix */ |
|
|
14297 |
zPat = sqlite3_mprintf(".%s*", zPattern); |
|
|
14298 |
for(i=0; i |
|
|
14299 |
if( sqlite3_strglob(zPat, azHelp[i])==0 ){ |
|
|
14300 |
utf8_printf(out, "%s\n", azHelp[i]); |
|
|
14301 |
j = i+1; |
|
|
14302 |
n++; |
|
|
14303 |
} |
|
|
14304 |
} |
|
|
14305 |
sqlite3_free(zPat); |
|
|
14306 |
if( n ){ |
|
|
14307 |
if( n==1 ){ |
|
|
14308 |
/* when zPattern is a prefix of exactly one command, then include the |
|
|
14309 |
** details of that command, which should begin at offset j */ |
|
|
14310 |
while( j |
|
|
14311 |
utf8_printf(out, "%s\n", azHelp[j]); |
|
|
14312 |
j++; |
|
|
14313 |
} |
|
|
14314 |
} |
|
|
14315 |
return n; |
|
|
14316 |
} |
|
|
14317 |
/* Look for commands that contain zPattern anywhere. Show the complete |
|
|
14318 |
** text of all commands that match. */ |
|
|
14319 |
zPat = sqlite3_mprintf("%%%s%%", zPattern); |
|
|
14320 |
for(i=0; i |
|
|
14321 |
if( azHelp[i][0]=='.' ) j = i; |
|
|
14322 |
if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){ |
|
|
14323 |
utf8_printf(out, "%s\n", azHelp[j]); |
|
|
14324 |
while( j |
|
|
14325 |
j++; |
|
|
14326 |
utf8_printf(out, "%s\n", azHelp[j]); |
|
|
14327 |
} |
|
|
14328 |
i = j; |
|
|
14329 |
n++; |
|
|
14330 |
} |
|
|
14331 |
} |
|
|
14332 |
sqlite3_free(zPat); |
|
|
14333 |
} |
|
|
14334 |
return n; |
|
|
14335 |
} |
|
|
14336 |
|
|
|
14337 |
/* Forward reference */ |
|
|
14338 |
static int process_input(ShellState *p); |
|
|
14339 |
|
|
|
14340 |
/* |
|
|
14341 |
** Read the content of file zName into memory obtained from sqlite3_malloc64() |
|
|
14342 |
** and return a pointer to the buffer. The caller is responsible for freeing |
|
|
14343 |
** the memory. |
|
|
14344 |
** |
|
|
14345 |
** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes |
|
|
14346 |
** read. |
|
|
14347 |
** |
|
|
14348 |
** For convenience, a nul-terminator byte is always appended to the data read |
|
|
14349 |
** from the file before the buffer is returned. This byte is not included in |
|
|
14350 |
** the final value of (*pnByte), if applicable. |
|
|
14351 |
** |
|
|
14352 |
** NULL is returned if any error is encountered. The final value of *pnByte |
|
|
14353 |
** is undefined in this case. |
|
|
14354 |
*/ |
|
|
14355 |
static char *readFile(const char *zName, int *pnByte){ |
|
|
14356 |
FILE *in = fopen(zName, "rb"); |
|
|
14357 |
long nIn; |
|
|
14358 |
size_t nRead; |
|
|
14359 |
char *pBuf; |
|
|
14360 |
if( in==0 ) return 0; |
|
|
14361 |
fseek(in, 0, SEEK_END); |
|
|
14362 |
nIn = ftell(in); |
|
|
14363 |
rewind(in); |
|
|
14364 |
pBuf = sqlite3_malloc64( nIn+1 ); |
|
|
14365 |
if( pBuf==0 ){ fclose(in); return 0; } |
|
|
14366 |
nRead = fread(pBuf, nIn, 1, in); |
|
|
14367 |
fclose(in); |
|
|
14368 |
if( nRead!=1 ){ |
|
|
14369 |
sqlite3_free(pBuf); |
|
|
14370 |
return 0; |
|
|
14371 |
} |
|
|
14372 |
pBuf[nIn] = 0; |
|
|
14373 |
if( pnByte ) *pnByte = nIn; |
|
|
14374 |
return pBuf; |
|
|
14375 |
} |
|
|
14376 |
|
|
|
14377 |
#if defined(SQLITE_ENABLE_SESSION) |
|
|
14378 |
/* |
|
|
14379 |
** Close a single OpenSession object and release all of its associated |
|
|
14380 |
** resources. |
|
|
14381 |
*/ |
|
|
14382 |
static void session_close(OpenSession *pSession){ |
|
|
14383 |
int i; |
|
|
14384 |
sqlite3session_delete(pSession->p); |
|
|
14385 |
sqlite3_free(pSession->zName); |
|
|
14386 |
for(i=0; inFilter; i++){ |
|
|
14387 |
sqlite3_free(pSession->azFilter[i]); |
|
|
14388 |
} |
|
|
14389 |
sqlite3_free(pSession->azFilter); |
|
|
14390 |
memset(pSession, 0, sizeof(OpenSession)); |
|
|
14391 |
} |
|
|
14392 |
#endif |
|
|
14393 |
|
|
|
14394 |
/* |
|
|
14395 |
** Close all OpenSession objects and release all associated resources. |
|
|
14396 |
*/ |
|
|
14397 |
#if defined(SQLITE_ENABLE_SESSION) |
|
|
14398 |
static void session_close_all(ShellState *p){ |
|
|
14399 |
int i; |
|
|
14400 |
for(i=0; inSession; i++){ |
|
|
14401 |
session_close(&p->aSession[i]); |
|
|
14402 |
} |
|
|
14403 |
p->nSession = 0; |
|
|
14404 |
} |
|
|
14405 |
#else |
|
|
14406 |
# define session_close_all(X) |
|
|
14407 |
#endif |
|
|
14408 |
|
|
|
14409 |
/* |
|
|
14410 |
** Implementation of the xFilter function for an open session. Omit |
|
|
14411 |
** any tables named by ".session filter" but let all other table through. |
|
|
14412 |
*/ |
|
|
14413 |
#if defined(SQLITE_ENABLE_SESSION) |
|
|
14414 |
static int session_filter(void *pCtx, const char *zTab){ |
|
|
14415 |
OpenSession *pSession = (OpenSession*)pCtx; |
|
|
14416 |
int i; |
|
|
14417 |
for(i=0; inFilter; i++){ |
|
|
14418 |
if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
|
|
14419 |
} |
|
|
14420 |
return 1; |
|
|
14421 |
} |
|
|
14422 |
#endif |
|
|
14423 |
|
|
|
14424 |
/* |
|
|
14425 |
** Try to deduce the type of file for zName based on its content. Return |
|
|
14426 |
** one of the SHELL_OPEN_* constants. |
|
|
14427 |
** |
|
|
14428 |
** If the file does not exist or is empty but its name looks like a ZIP |
|
|
14429 |
** archive and the dfltZip flag is true, then assume it is a ZIP archive. |
|
|
14430 |
** Otherwise, assume an ordinary database regardless of the filename if |
|
|
14431 |
** the type cannot be determined from content. |
|
|
14432 |
*/ |
|
|
14433 |
int deduceDatabaseType(const char *zName, int dfltZip){ |
|
|
14434 |
FILE *f = fopen(zName, "rb"); |
|
|
14435 |
size_t n; |
|
|
14436 |
int rc = SHELL_OPEN_UNSPEC; |
|
|
14437 |
char zBuf[100]; |
|
|
14438 |
if( f==0 ){ |
|
|
14439 |
if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ |
|
|
14440 |
return SHELL_OPEN_ZIPFILE; |
|
|
14441 |
}else{ |
|
|
14442 |
return SHELL_OPEN_NORMAL; |
|
|
14443 |
} |
|
|
14444 |
} |
|
|
14445 |
n = fread(zBuf, 16, 1, f); |
|
|
14446 |
if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){ |
|
|
14447 |
fclose(f); |
|
|
14448 |
return SHELL_OPEN_NORMAL; |
|
|
14449 |
} |
|
|
14450 |
fseek(f, -25, SEEK_END); |
|
|
14451 |
n = fread(zBuf, 25, 1, f); |
|
|
14452 |
if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ |
|
|
14453 |
rc = SHELL_OPEN_APPENDVFS; |
|
|
14454 |
}else{ |
|
|
14455 |
fseek(f, -22, SEEK_END); |
|
|
14456 |
n = fread(zBuf, 22, 1, f); |
|
|
14457 |
if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05 |
|
|
14458 |
&& zBuf[3]==0x06 ){ |
|
|
14459 |
rc = SHELL_OPEN_ZIPFILE; |
|
|
14460 |
}else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){ |
|
|
14461 |
rc = SHELL_OPEN_ZIPFILE; |
|
|
14462 |
} |
|
|
14463 |
} |
|
|
14464 |
fclose(f); |
|
|
14465 |
return rc; |
|
|
14466 |
} |
|
|
14467 |
|
|
|
14468 |
#ifdef SQLITE_ENABLE_DESERIALIZE |
|
|
14469 |
/* |
|
|
14470 |
** Reconstruct an in-memory database using the output from the "dbtotxt" |
|
|
14471 |
** program. Read content from the file in p->zDbFilename. If p->zDbFilename |
|
|
14472 |
** is 0, then read from standard input. |
|
|
14473 |
*/ |
|
|
14474 |
static unsigned char *readHexDb(ShellState *p, int *pnData){ |
|
|
14475 |
unsigned char *a = 0; |
|
|
14476 |
int nLine; |
|
|
14477 |
int n = 0; |
|
|
14478 |
int pgsz = 0; |
|
|
14479 |
int iOffset = 0; |
|
|
14480 |
int j, k; |
|
|
14481 |
int rc; |
|
|
14482 |
FILE *in; |
|
|
14483 |
unsigned int x[16]; |
|
|
14484 |
char zLine[1000]; |
|
|
14485 |
if( p->zDbFilename ){ |
|
|
14486 |
in = fopen(p->zDbFilename, "r"); |
|
|
14487 |
if( in==0 ){ |
|
|
14488 |
utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename); |
|
|
14489 |
return 0; |
|
|
14490 |
} |
|
|
14491 |
nLine = 0; |
|
|
14492 |
}else{ |
|
|
14493 |
in = p->in; |
|
|
14494 |
nLine = p->lineno; |
|
|
14495 |
if( in==0 ) in = stdin; |
|
|
14496 |
} |
|
|
14497 |
*pnData = 0; |
|
|
14498 |
nLine++; |
|
|
14499 |
if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error; |
|
|
14500 |
rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz); |
|
|
14501 |
if( rc!=2 ) goto readHexDb_error; |
|
|
14502 |
if( n<0 ) goto readHexDb_error; |
|
|
14503 |
if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error; |
|
|
14504 |
n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ |
|
|
14505 |
a = sqlite3_malloc( n ? n : 1 ); |
|
|
14506 |
if( a==0 ){ |
|
|
14507 |
utf8_printf(stderr, "Out of memory!\n"); |
|
|
14508 |
goto readHexDb_error; |
|
|
14509 |
} |
|
|
14510 |
memset(a, 0, n); |
|
|
14511 |
if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){ |
|
|
14512 |
utf8_printf(stderr, "invalid pagesize\n"); |
|
|
14513 |
goto readHexDb_error; |
|
|
14514 |
} |
|
|
14515 |
for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){ |
|
|
14516 |
rc = sscanf(zLine, "| page %d offset %d", &j, &k); |
|
|
14517 |
if( rc==2 ){ |
|
|
14518 |
iOffset = k; |
|
|
14519 |
continue; |
|
|
14520 |
} |
|
|
14521 |
if( strncmp(zLine, "| end ", 6)==0 ){ |
|
|
14522 |
break; |
|
|
14523 |
} |
|
|
14524 |
rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x", |
|
|
14525 |
&j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7], |
|
|
14526 |
&x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]); |
|
|
14527 |
if( rc==17 ){ |
|
|
14528 |
k = iOffset+j; |
|
|
14529 |
if( k+16<=n ){ |
|
|
14530 |
int ii; |
|
|
14531 |
for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff; |
|
|
14532 |
} |
|
|
14533 |
} |
|
|
14534 |
} |
|
|
14535 |
*pnData = n; |
|
|
14536 |
if( in!=p->in ){ |
|
|
14537 |
fclose(in); |
|
|
14538 |
}else{ |
|
|
14539 |
p->lineno = nLine; |
|
|
14540 |
} |
|
|
14541 |
return a; |
|
|
14542 |
|
|
|
14543 |
readHexDb_error: |
|
|
14544 |
if( in!=p->in ){ |
|
|
14545 |
fclose(in); |
|
|
14546 |
}else{ |
|
|
14547 |
while( fgets(zLine, sizeof(zLine), p->in)!=0 ){ |
|
|
14548 |
nLine++; |
|
|
14549 |
if(strncmp(zLine, "| end ", 6)==0 ) break; |
|
|
14550 |
} |
|
|
14551 |
p->lineno = nLine; |
|
|
14552 |
} |
|
|
14553 |
sqlite3_free(a); |
|
|
14554 |
utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine); |
|
|
14555 |
return 0; |
|
|
14556 |
} |
|
|
14557 |
#endif /* SQLITE_ENABLE_DESERIALIZE */ |
|
|
14558 |
|
|
|
14559 |
/* |
|
|
14560 |
** Scalar function "shell_int32". The first argument to this function |
|
|
14561 |
** must be a blob. The second a non-negative integer. This function |
|
|
14562 |
** reads and returns a 32-bit big-endian integer from byte |
|
|
14563 |
** offset (4*) of the blob. |
|
|
14564 |
*/ |
|
|
14565 |
static void shellInt32( |
|
|
14566 |
sqlite3_context *context, |
|
|
14567 |
int argc, |
|
|
14568 |
sqlite3_value **argv |
|
|
14569 |
){ |
|
|
14570 |
const unsigned char *pBlob; |
|
|
14571 |
int nBlob; |
|
|
14572 |
int iInt; |
|
|
14573 |
|
|
|
14574 |
UNUSED_PARAMETER(argc); |
|
|
14575 |
nBlob = sqlite3_value_bytes(argv[0]); |
|
|
14576 |
pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]); |
|
|
14577 |
iInt = sqlite3_value_int(argv[1]); |
|
|
14578 |
|
|
|
14579 |
if( iInt>=0 && (iInt+1)*4<=nBlob ){ |
|
|
14580 |
const unsigned char *a = &pBlob[iInt*4]; |
|
|
14581 |
sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24) |
|
|
14582 |
+ ((sqlite3_int64)a[1]<<16) |
|
|
14583 |
+ ((sqlite3_int64)a[2]<< 8) |
|
|
14584 |
+ ((sqlite3_int64)a[3]<< 0); |
|
|
14585 |
sqlite3_result_int64(context, iVal); |
|
|
14586 |
} |
|
|
14587 |
} |
|
|
14588 |
|
|
|
14589 |
/* |
|
|
14590 |
** Scalar function "shell_idquote(X)" returns string X quoted as an identifier, |
|
|
14591 |
** using "..." with internal double-quote characters doubled. |
|
|
14592 |
*/ |
|
|
14593 |
static void shellIdQuote( |
|
|
14594 |
sqlite3_context *context, |
|
|
14595 |
int argc, |
|
|
14596 |
sqlite3_value **argv |
|
|
14597 |
){ |
|
|
14598 |
const char *zName = (const char*)sqlite3_value_text(argv[0]); |
|
|
14599 |
UNUSED_PARAMETER(argc); |
|
|
14600 |
if( zName ){ |
|
|
14601 |
char *z = sqlite3_mprintf("\"%w\"", zName); |
|
|
14602 |
sqlite3_result_text(context, z, -1, sqlite3_free); |
|
|
14603 |
} |
|
|
14604 |
} |
|
|
14605 |
|
|
|
14606 |
/* |
|
|
14607 |
** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X. |
|
|
14608 |
*/ |
|
|
14609 |
static void shellUSleepFunc( |
|
|
14610 |
sqlite3_context *context, |
|
|
14611 |
int argcUnused, |
|
|
14612 |
sqlite3_value **argv |
|
|
14613 |
){ |
|
|
14614 |
int sleep = sqlite3_value_int(argv[0]); |
|
|
14615 |
(void)argcUnused; |
|
|
14616 |
sqlite3_sleep(sleep/1000); |
|
|
14617 |
sqlite3_result_int(context, sleep); |
|
|
14618 |
} |
|
|
14619 |
|
|
|
14620 |
/* |
|
|
14621 |
** Scalar function "shell_escape_crnl" used by the .recover command. |
|
|
14622 |
** The argument passed to this function is the output of built-in |
|
|
14623 |
** function quote(). If the first character of the input is "'", |
|
|
14624 |
** indicating that the value passed to quote() was a text value, |
|
|
14625 |
** then this function searches the input for "\n" and "\r" characters |
|
|
14626 |
** and adds a wrapper similar to the following: |
|
|
14627 |
** |
|
|
14628 |
** replace(replace(, '\n', char(10), '\r', char(13)); |
|
|
14629 |
** |
|
|
14630 |
** Or, if the first character of the input is not "'", then a copy |
|
|
14631 |
** of the input is returned. |
|
|
14632 |
*/ |
|
|
14633 |
static void shellEscapeCrnl( |
|
|
14634 |
sqlite3_context *context, |
|
|
14635 |
int argc, |
|
|
14636 |
sqlite3_value **argv |
|
|
14637 |
){ |
|
|
14638 |
const char *zText = (const char*)sqlite3_value_text(argv[0]); |
|
|
14639 |
UNUSED_PARAMETER(argc); |
|
|
14640 |
if( zText[0]=='\'' ){ |
|
|
14641 |
int nText = sqlite3_value_bytes(argv[0]); |
|
|
14642 |
int i; |
|
|
14643 |
char zBuf1[20]; |
|
|
14644 |
char zBuf2[20]; |
|
|
14645 |
const char *zNL = 0; |
|
|
14646 |
const char *zCR = 0; |
|
|
14647 |
int nCR = 0; |
|
|
14648 |
int nNL = 0; |
|
|
14649 |
|
|
|
14650 |
for(i=0; zText[i]; i++){ |
|
|
14651 |
if( zNL==0 && zText[i]=='\n' ){ |
|
|
14652 |
zNL = unused_string(zText, "\\n", "\\012", zBuf1); |
|
|
14653 |
nNL = (int)strlen(zNL); |
|
|
14654 |
} |
|
|
14655 |
if( zCR==0 && zText[i]=='\r' ){ |
|
|
14656 |
zCR = unused_string(zText, "\\r", "\\015", zBuf2); |
|
|
14657 |
nCR = (int)strlen(zCR); |
|
|
14658 |
} |
|
|
14659 |
} |
|
|
14660 |
|
|
|
14661 |
if( zNL || zCR ){ |
|
|
14662 |
int iOut = 0; |
|
|
14663 |
i64 nMax = (nNL > nCR) ? nNL : nCR; |
|
|
14664 |
i64 nAlloc = nMax * nText + (nMax+64)*2; |
|
|
14665 |
char *zOut = (char*)sqlite3_malloc64(nAlloc); |
|
|
14666 |
if( zOut==0 ){ |
|
|
14667 |
sqlite3_result_error_nomem(context); |
|
|
14668 |
return; |
|
|
14669 |
} |
|
|
14670 |
|
|
|
14671 |
if( zNL && zCR ){ |
|
|
14672 |
memcpy(&zOut[iOut], "replace(replace(", 16); |
|
|
14673 |
iOut += 16; |
|
|
14674 |
}else{ |
|
|
14675 |
memcpy(&zOut[iOut], "replace(", 8); |
|
|
14676 |
iOut += 8; |
|
|
14677 |
} |
|
|
14678 |
for(i=0; zText[i]; i++){ |
|
|
14679 |
if( zText[i]=='\n' ){ |
|
|
14680 |
memcpy(&zOut[iOut], zNL, nNL); |
|
|
14681 |
iOut += nNL; |
|
|
14682 |
}else if( zText[i]=='\r' ){ |
|
|
14683 |
memcpy(&zOut[iOut], zCR, nCR); |
|
|
14684 |
iOut += nCR; |
|
|
14685 |
}else{ |
|
|
14686 |
zOut[iOut] = zText[i]; |
|
|
14687 |
iOut++; |
|
|
14688 |
} |
|
|
14689 |
} |
|
|
14690 |
|
|
|
14691 |
if( zNL ){ |
|
|
14692 |
memcpy(&zOut[iOut], ",'", 2); iOut += 2; |
|
|
14693 |
memcpy(&zOut[iOut], zNL, nNL); iOut += nNL; |
|
|
14694 |
memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12; |
|
|
14695 |
} |
|
|
14696 |
if( zCR ){ |
|
|
14697 |
memcpy(&zOut[iOut], ",'", 2); iOut += 2; |
|
|
14698 |
memcpy(&zOut[iOut], zCR, nCR); iOut += nCR; |
|
|
14699 |
memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12; |
|
|
14700 |
} |
|
|
14701 |
|
|
|
14702 |
sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT); |
|
|
14703 |
sqlite3_free(zOut); |
|
|
14704 |
return; |
|
|
14705 |
} |
|
|
14706 |
} |
|
|
14707 |
|
|
|
14708 |
sqlite3_result_value(context, argv[0]); |
|
|
14709 |
} |
|
|
14710 |
|
|
|
14711 |
/* Flags for open_db(). |
|
|
14712 |
** |
|
|
14713 |
** The default behavior of open_db() is to exit(1) if the database fails to |
|
|
14714 |
** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error |
|
|
14715 |
** but still returns without calling exit. |
|
|
14716 |
** |
|
|
14717 |
** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a |
|
|
14718 |
** ZIP archive if the file does not exist or is empty and its name matches |
|
|
14719 |
** the *.zip pattern. |
|
|
14720 |
*/ |
|
|
14721 |
#define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */ |
|
|
14722 |
#define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */ |
|
|
14723 |
|
|
|
14724 |
/* |
|
|
14725 |
** Make sure the database is open. If it is not, then open it. If |
|
|
14726 |
** the database fails to open, print an error message and exit. |
|
|
14727 |
*/ |
|
|
14728 |
static void open_db(ShellState *p, int openFlags){ |
|
|
14729 |
if( p->db==0 ){ |
|
|
14730 |
if( p->openMode==SHELL_OPEN_UNSPEC ){ |
|
|
14731 |
if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){ |
|
|
14732 |
p->openMode = SHELL_OPEN_NORMAL; |
|
|
14733 |
}else{ |
|
|
14734 |
p->openMode = (u8)deduceDatabaseType(p->zDbFilename, |
|
|
14735 |
(openFlags & OPEN_DB_ZIPFILE)!=0); |
|
|
14736 |
} |
|
|
14737 |
} |
|
|
14738 |
switch( p->openMode ){ |
|
|
14739 |
case SHELL_OPEN_APPENDVFS: { |
|
|
14740 |
sqlite3_open_v2(p->zDbFilename, &p->db, |
|
|
14741 |
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs"); |
|
|
14742 |
break; |
|
|
14743 |
} |
|
|
14744 |
case SHELL_OPEN_HEXDB: |
|
|
14745 |
case SHELL_OPEN_DESERIALIZE: { |
|
|
14746 |
sqlite3_open(0, &p->db); |
|
|
14747 |
break; |
|
|
14748 |
} |
|
|
14749 |
case SHELL_OPEN_ZIPFILE: { |
|
|
14750 |
sqlite3_open(":memory:", &p->db); |
|
|
14751 |
break; |
|
|
14752 |
} |
|
|
14753 |
case SHELL_OPEN_READONLY: { |
|
|
14754 |
sqlite3_open_v2(p->zDbFilename, &p->db, |
|
|
14755 |
SQLITE_OPEN_READONLY|p->openFlags, 0); |
|
|
14756 |
break; |
|
|
14757 |
} |
|
|
14758 |
case SHELL_OPEN_UNSPEC: |
|
|
14759 |
case SHELL_OPEN_NORMAL: { |
|
|
14760 |
sqlite3_open_v2(p->zDbFilename, &p->db, |
|
|
14761 |
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0); |
|
|
14762 |
break; |
|
|
14763 |
} |
|
|
14764 |
} |
|
|
14765 |
globalDb = p->db; |
|
|
14766 |
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
|
|
14767 |
utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
|
|
14768 |
p->zDbFilename, sqlite3_errmsg(p->db)); |
|
|
14769 |
if( openFlags & OPEN_DB_KEEPALIVE ){ |
|
|
14770 |
sqlite3_open(":memory:", &p->db); |
|
|
14771 |
return; |
|
|
14772 |
} |
|
|
14773 |
exit(1); |
|
|
14774 |
} |
|
|
14775 |
#ifndef SQLITE_OMIT_LOAD_EXTENSION |
|
|
14776 |
sqlite3_enable_load_extension(p->db, 1); |
|
|
14777 |
#endif |
|
|
14778 |
sqlite3_fileio_init(p->db, 0, 0); |
|
|
14779 |
sqlite3_shathree_init(p->db, 0, 0); |
|
|
14780 |
sqlite3_completion_init(p->db, 0, 0); |
|
|
14781 |
sqlite3_uint_init(p->db, 0, 0); |
|
|
14782 |
sqlite3_decimal_init(p->db, 0, 0); |
|
|
14783 |
sqlite3_ieee_init(p->db, 0, 0); |
|
|
14784 |
sqlite3_series_init(p->db, 0, 0); |
|
|
14785 |
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
|
|
14786 |
sqlite3_dbdata_init(p->db, 0, 0); |
|
|
14787 |
#endif |
|
|
14788 |
#ifdef SQLITE_HAVE_ZLIB |
|
|
14789 |
sqlite3_zipfile_init(p->db, 0, 0); |
|
|
14790 |
sqlite3_sqlar_init(p->db, 0, 0); |
|
|
14791 |
#endif |
|
|
14792 |
sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, |
|
|
14793 |
shellAddSchemaName, 0, 0); |
|
|
14794 |
sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, |
|
|
14795 |
shellModuleSchema, 0, 0); |
|
|
14796 |
sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, |
|
|
14797 |
shellPutsFunc, 0, 0); |
|
|
14798 |
sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0, |
|
|
14799 |
shellEscapeCrnl, 0, 0); |
|
|
14800 |
sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0, |
|
|
14801 |
shellInt32, 0, 0); |
|
|
14802 |
sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0, |
|
|
14803 |
shellIdQuote, 0, 0); |
|
|
14804 |
sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0, |
|
|
14805 |
shellUSleepFunc, 0, 0); |
|
|
14806 |
#ifndef SQLITE_NOHAVE_SYSTEM |
|
|
14807 |
sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, |
|
|
14808 |
editFunc, 0, 0); |
|
|
14809 |
sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, |
|
|
14810 |
editFunc, 0, 0); |
|
|
14811 |
#endif |
|
|
14812 |
if( p->openMode==SHELL_OPEN_ZIPFILE ){ |
|
|
14813 |
char *zSql = sqlite3_mprintf( |
|
|
14814 |
"CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename); |
|
|
14815 |
sqlite3_exec(p->db, zSql, 0, 0, 0); |
|
|
14816 |
sqlite3_free(zSql); |
|
|
14817 |
} |
|
|
14818 |
#ifdef SQLITE_ENABLE_DESERIALIZE |
|
|
14819 |
else |
|
|
14820 |
if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){ |
|
|
14821 |
int rc; |
|
|
14822 |
int nData = 0; |
|
|
14823 |
unsigned char *aData; |
|
|
14824 |
if( p->openMode==SHELL_OPEN_DESERIALIZE ){ |
|
|
14825 |
aData = (unsigned char*)readFile(p->zDbFilename, &nData); |
|
|
14826 |
}else{ |
|
|
14827 |
aData = readHexDb(p, &nData); |
|
|
14828 |
if( aData==0 ){ |
|
|
14829 |
return; |
|
|
14830 |
} |
|
|
14831 |
} |
|
|
14832 |
rc = sqlite3_deserialize(p->db, "main", aData, nData, nData, |
|
|
14833 |
SQLITE_DESERIALIZE_RESIZEABLE | |
|
|
14834 |
SQLITE_DESERIALIZE_FREEONCLOSE); |
|
|
14835 |
if( rc ){ |
|
|
14836 |
utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc); |
|
|
14837 |
} |
|
|
14838 |
if( p->szMax>0 ){ |
|
|
14839 |
sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax); |
|
|
14840 |
} |
|
|
14841 |
} |
|
|
14842 |
#endif |
|
|
14843 |
} |
|
|
14844 |
} |
|
|
14845 |
|
|
|
14846 |
/* |
|
|
14847 |
** Attempt to close the databaes connection. Report errors. |
|
|
14848 |
*/ |
|
|
14849 |
void close_db(sqlite3 *db){ |
|
|
14850 |
int rc = sqlite3_close(db); |
|
|
14851 |
if( rc ){ |
|
|
14852 |
utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n", |
|
|
14853 |
rc, sqlite3_errmsg(db)); |
|
|
14854 |
} |
|
|
14855 |
} |
|
|
14856 |
|
|
|
14857 |
#if HAVE_READLINE || HAVE_EDITLINE |
|
|
14858 |
/* |
|
|
14859 |
** Readline completion callbacks |
|
|
14860 |
*/ |
|
|
14861 |
static char *readline_completion_generator(const char *text, int state){ |
|
|
14862 |
static sqlite3_stmt *pStmt = 0; |
|
|
14863 |
char *zRet; |
|
|
14864 |
if( state==0 ){ |
|
|
14865 |
char *zSql; |
|
|
14866 |
sqlite3_finalize(pStmt); |
|
|
14867 |
zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
|
|
14868 |
" FROM completion(%Q) ORDER BY 1", text); |
|
|
14869 |
sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
|
|
14870 |
sqlite3_free(zSql); |
|
|
14871 |
} |
|
|
14872 |
if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
14873 |
zRet = strdup((const char*)sqlite3_column_text(pStmt, 0)); |
|
|
14874 |
}else{ |
|
|
14875 |
sqlite3_finalize(pStmt); |
|
|
14876 |
pStmt = 0; |
|
|
14877 |
zRet = 0; |
|
|
14878 |
} |
|
|
14879 |
return zRet; |
|
|
14880 |
} |
|
|
14881 |
static char **readline_completion(const char *zText, int iStart, int iEnd){ |
|
|
14882 |
rl_attempted_completion_over = 1; |
|
|
14883 |
return rl_completion_matches(zText, readline_completion_generator); |
|
|
14884 |
} |
|
|
14885 |
|
|
|
14886 |
#elif HAVE_LINENOISE |
|
|
14887 |
/* |
|
|
14888 |
** Linenoise completion callback |
|
|
14889 |
*/ |
|
|
14890 |
static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){ |
|
|
14891 |
int nLine = strlen30(zLine); |
|
|
14892 |
int i, iStart; |
|
|
14893 |
sqlite3_stmt *pStmt = 0; |
|
|
14894 |
char *zSql; |
|
|
14895 |
char zBuf[1000]; |
|
|
14896 |
|
|
|
14897 |
if( nLine>sizeof(zBuf)-30 ) return; |
|
|
14898 |
if( zLine[0]=='.' || zLine[0]=='#') return; |
|
|
14899 |
for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} |
|
|
14900 |
if( i==nLine-1 ) return; |
|
|
14901 |
iStart = i+1; |
|
|
14902 |
memcpy(zBuf, zLine, iStart); |
|
|
14903 |
zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase" |
|
|
14904 |
" FROM completion(%Q,%Q) ORDER BY 1", |
|
|
14905 |
&zLine[iStart], zLine); |
|
|
14906 |
sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); |
|
|
14907 |
sqlite3_free(zSql); |
|
|
14908 |
sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */ |
|
|
14909 |
while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
14910 |
const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0); |
|
|
14911 |
int nCompletion = sqlite3_column_bytes(pStmt, 0); |
|
|
14912 |
if( iStart+nCompletion < sizeof(zBuf)-1 ){ |
|
|
14913 |
memcpy(zBuf+iStart, zCompletion, nCompletion+1); |
|
|
14914 |
linenoiseAddCompletion(lc, zBuf); |
|
|
14915 |
} |
|
|
14916 |
} |
|
|
14917 |
sqlite3_finalize(pStmt); |
|
|
14918 |
} |
|
|
14919 |
#endif |
|
|
14920 |
|
|
|
14921 |
/* |
|
|
14922 |
** Do C-language style dequoting. |
|
|
14923 |
** |
|
|
14924 |
** \a -> alarm |
|
|
14925 |
** \b -> backspace |
|
|
14926 |
** \t -> tab |
|
|
14927 |
** \n -> newline |
|
|
14928 |
** \v -> vertical tab |
|
|
14929 |
** \f -> form feed |
|
|
14930 |
** \r -> carriage return |
|
|
14931 |
** \s -> space |
|
|
14932 |
** \" -> " |
|
|
14933 |
** \' -> ' |
|
|
14934 |
** \\ -> backslash |
|
|
14935 |
** \NNN -> ascii character NNN in octal |
|
|
14936 |
*/ |
|
|
14937 |
static void resolve_backslashes(char *z){ |
|
|
14938 |
int i, j; |
|
|
14939 |
char c; |
|
|
14940 |
while( *z && *z!='\\' ) z++; |
|
|
14941 |
for(i=j=0; (c = z[i])!=0; i++, j++){ |
|
|
14942 |
if( c=='\\' && z[i+1]!=0 ){ |
|
|
14943 |
c = z[++i]; |
|
|
14944 |
if( c=='a' ){ |
|
|
14945 |
c = '\a'; |
|
|
14946 |
}else if( c=='b' ){ |
|
|
14947 |
c = '\b'; |
|
|
14948 |
}else if( c=='t' ){ |
|
|
14949 |
c = '\t'; |
|
|
14950 |
}else if( c=='n' ){ |
|
|
14951 |
c = '\n'; |
|
|
14952 |
}else if( c=='v' ){ |
|
|
14953 |
c = '\v'; |
|
|
14954 |
}else if( c=='f' ){ |
|
|
14955 |
c = '\f'; |
|
|
14956 |
}else if( c=='r' ){ |
|
|
14957 |
c = '\r'; |
|
|
14958 |
}else if( c=='"' ){ |
|
|
14959 |
c = '"'; |
|
|
14960 |
}else if( c=='\'' ){ |
|
|
14961 |
c = '\''; |
|
|
14962 |
}else if( c=='\\' ){ |
|
|
14963 |
c = '\\'; |
|
|
14964 |
}else if( c>='0' && c<='7' ){ |
|
|
14965 |
c -= '0'; |
|
|
14966 |
if( z[i+1]>='0' && z[i+1]<='7' ){ |
|
|
14967 |
i++; |
|
|
14968 |
c = (c<<3) + z[i] - '0'; |
|
|
14969 |
if( z[i+1]>='0' && z[i+1]<='7' ){ |
|
|
14970 |
i++; |
|
|
14971 |
c = (c<<3) + z[i] - '0'; |
|
|
14972 |
} |
|
|
14973 |
} |
|
|
14974 |
} |
|
|
14975 |
} |
|
|
14976 |
z[j] = c; |
|
|
14977 |
} |
|
|
14978 |
if( j |
|
|
14979 |
} |
|
|
14980 |
|
|
|
14981 |
/* |
|
|
14982 |
** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
|
|
14983 |
** for TRUE and FALSE. Return the integer value if appropriate. |
|
|
14984 |
*/ |
|
|
14985 |
static int booleanValue(const char *zArg){ |
|
|
14986 |
int i; |
|
|
14987 |
if( zArg[0]=='0' && zArg[1]=='x' ){ |
|
|
14988 |
for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
|
|
14989 |
}else{ |
|
|
14990 |
for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
|
|
14991 |
} |
|
|
14992 |
if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
|
|
14993 |
if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
|
|
14994 |
return 1; |
|
|
14995 |
} |
|
|
14996 |
if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
|
|
14997 |
return 0; |
|
|
14998 |
} |
|
|
14999 |
utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
|
|
15000 |
zArg); |
|
|
15001 |
return 0; |
|
|
15002 |
} |
|
|
15003 |
|
|
|
15004 |
/* |
|
|
15005 |
** Set or clear a shell flag according to a boolean value. |
|
|
15006 |
*/ |
|
|
15007 |
static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){ |
|
|
15008 |
if( booleanValue(zArg) ){ |
|
|
15009 |
ShellSetFlag(p, mFlag); |
|
|
15010 |
}else{ |
|
|
15011 |
ShellClearFlag(p, mFlag); |
|
|
15012 |
} |
|
|
15013 |
} |
|
|
15014 |
|
|
|
15015 |
/* |
|
|
15016 |
** Close an output file, assuming it is not stderr or stdout |
|
|
15017 |
*/ |
|
|
15018 |
static void output_file_close(FILE *f){ |
|
|
15019 |
if( f && f!=stdout && f!=stderr ) fclose(f); |
|
|
15020 |
} |
|
|
15021 |
|
|
|
15022 |
/* |
|
|
15023 |
** Try to open an output file. The names "stdout" and "stderr" are |
|
|
15024 |
** recognized and do the right thing. NULL is returned if the output |
|
|
15025 |
** filename is "off". |
|
|
15026 |
*/ |
|
|
15027 |
static FILE *output_file_open(const char *zFile, int bTextMode){ |
|
|
15028 |
FILE *f; |
|
|
15029 |
if( strcmp(zFile,"stdout")==0 ){ |
|
|
15030 |
f = stdout; |
|
|
15031 |
}else if( strcmp(zFile, "stderr")==0 ){ |
|
|
15032 |
f = stderr; |
|
|
15033 |
}else if( strcmp(zFile, "off")==0 ){ |
|
|
15034 |
f = 0; |
|
|
15035 |
}else{ |
|
|
15036 |
f = fopen(zFile, bTextMode ? "w" : "wb"); |
|
|
15037 |
if( f==0 ){ |
|
|
15038 |
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
|
|
15039 |
} |
|
|
15040 |
} |
|
|
15041 |
return f; |
|
|
15042 |
} |
|
|
15043 |
|
|
|
15044 |
#ifndef SQLITE_OMIT_TRACE |
|
|
15045 |
/* |
|
|
15046 |
** A routine for handling output from sqlite3_trace(). |
|
|
15047 |
*/ |
|
|
15048 |
static int sql_trace_callback( |
|
|
15049 |
unsigned mType, /* The trace type */ |
|
|
15050 |
void *pArg, /* The ShellState pointer */ |
|
|
15051 |
void *pP, /* Usually a pointer to sqlite_stmt */ |
|
|
15052 |
void *pX /* Auxiliary output */ |
|
|
15053 |
){ |
|
|
15054 |
ShellState *p = (ShellState*)pArg; |
|
|
15055 |
sqlite3_stmt *pStmt; |
|
|
15056 |
const char *zSql; |
|
|
15057 |
int nSql; |
|
|
15058 |
if( p->traceOut==0 ) return 0; |
|
|
15059 |
if( mType==SQLITE_TRACE_CLOSE ){ |
|
|
15060 |
utf8_printf(p->traceOut, "-- closing database connection\n"); |
|
|
15061 |
return 0; |
|
|
15062 |
} |
|
|
15063 |
if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){ |
|
|
15064 |
zSql = (const char*)pX; |
|
|
15065 |
}else{ |
|
|
15066 |
pStmt = (sqlite3_stmt*)pP; |
|
|
15067 |
switch( p->eTraceType ){ |
|
|
15068 |
case SHELL_TRACE_EXPANDED: { |
|
|
15069 |
zSql = sqlite3_expanded_sql(pStmt); |
|
|
15070 |
break; |
|
|
15071 |
} |
|
|
15072 |
#ifdef SQLITE_ENABLE_NORMALIZE |
|
|
15073 |
case SHELL_TRACE_NORMALIZED: { |
|
|
15074 |
zSql = sqlite3_normalized_sql(pStmt); |
|
|
15075 |
break; |
|
|
15076 |
} |
|
|
15077 |
#endif |
|
|
15078 |
default: { |
|
|
15079 |
zSql = sqlite3_sql(pStmt); |
|
|
15080 |
break; |
|
|
15081 |
} |
|
|
15082 |
} |
|
|
15083 |
} |
|
|
15084 |
if( zSql==0 ) return 0; |
|
|
15085 |
nSql = strlen30(zSql); |
|
|
15086 |
while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; } |
|
|
15087 |
switch( mType ){ |
|
|
15088 |
case SQLITE_TRACE_ROW: |
|
|
15089 |
case SQLITE_TRACE_STMT: { |
|
|
15090 |
utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql); |
|
|
15091 |
break; |
|
|
15092 |
} |
|
|
15093 |
case SQLITE_TRACE_PROFILE: { |
|
|
15094 |
sqlite3_int64 nNanosec = *(sqlite3_int64*)pX; |
|
|
15095 |
utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec); |
|
|
15096 |
break; |
|
|
15097 |
} |
|
|
15098 |
} |
|
|
15099 |
return 0; |
|
|
15100 |
} |
|
|
15101 |
#endif |
|
|
15102 |
|
|
|
15103 |
/* |
|
|
15104 |
** A no-op routine that runs with the ".breakpoint" doc-command. This is |
|
|
15105 |
** a useful spot to set a debugger breakpoint. |
|
|
15106 |
*/ |
|
|
15107 |
static void test_breakpoint(void){ |
|
|
15108 |
static int nCall = 0; |
|
|
15109 |
nCall++; |
|
|
15110 |
} |
|
|
15111 |
|
|
|
15112 |
/* |
|
|
15113 |
** An object used to read a CSV and other files for import. |
|
|
15114 |
*/ |
|
|
15115 |
typedef struct ImportCtx ImportCtx; |
|
|
15116 |
struct ImportCtx { |
|
|
15117 |
const char *zFile; /* Name of the input file */ |
|
|
15118 |
FILE *in; /* Read the CSV text from this input stream */ |
|
|
15119 |
int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */ |
|
|
15120 |
char *z; /* Accumulated text for a field */ |
|
|
15121 |
int n; /* Number of bytes in z */ |
|
|
15122 |
int nAlloc; /* Space allocated for z[] */ |
|
|
15123 |
int nLine; /* Current line number */ |
|
|
15124 |
int nRow; /* Number of rows imported */ |
|
|
15125 |
int nErr; /* Number of errors encountered */ |
|
|
15126 |
int bNotFirst; /* True if one or more bytes already read */ |
|
|
15127 |
int cTerm; /* Character that terminated the most recent field */ |
|
|
15128 |
int cColSep; /* The column separator character. (Usually ",") */ |
|
|
15129 |
int cRowSep; /* The row separator character. (Usually "\n") */ |
|
|
15130 |
}; |
|
|
15131 |
|
|
|
15132 |
/* Clean up resourced used by an ImportCtx */ |
|
|
15133 |
static void import_cleanup(ImportCtx *p){ |
|
|
15134 |
if( p->in!=0 && p->xCloser!=0 ){ |
|
|
15135 |
p->xCloser(p->in); |
|
|
15136 |
p->in = 0; |
|
|
15137 |
} |
|
|
15138 |
sqlite3_free(p->z); |
|
|
15139 |
p->z = 0; |
|
|
15140 |
} |
|
|
15141 |
|
|
|
15142 |
/* Append a single byte to z[] */ |
|
|
15143 |
static void import_append_char(ImportCtx *p, int c){ |
|
|
15144 |
if( p->n+1>=p->nAlloc ){ |
|
|
15145 |
p->nAlloc += p->nAlloc + 100; |
|
|
15146 |
p->z = sqlite3_realloc64(p->z, p->nAlloc); |
|
|
15147 |
if( p->z==0 ) shell_out_of_memory(); |
|
|
15148 |
} |
|
|
15149 |
p->z[p->n++] = (char)c; |
|
|
15150 |
} |
|
|
15151 |
|
|
|
15152 |
/* Read a single field of CSV text. Compatible with rfc4180 and extended |
|
|
15153 |
** with the option of having a separator other than ",". |
|
|
15154 |
** |
|
|
15155 |
** + Input comes from p->in. |
|
|
15156 |
** + Store results in p->z of length p->n. Space to hold p->z comes |
|
|
15157 |
** from sqlite3_malloc64(). |
|
|
15158 |
** + Use p->cSep as the column separator. The default is ",". |
|
|
15159 |
** + Use p->rSep as the row separator. The default is "\n". |
|
|
15160 |
** + Keep track of the line number in p->nLine. |
|
|
15161 |
** + Store the character that terminates the field in p->cTerm. Store |
|
|
15162 |
** EOF on end-of-file. |
|
|
15163 |
** + Report syntax errors on stderr |
|
|
15164 |
*/ |
|
|
15165 |
static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
|
|
15166 |
int c; |
|
|
15167 |
int cSep = p->cColSep; |
|
|
15168 |
int rSep = p->cRowSep; |
|
|
15169 |
p->n = 0; |
|
|
15170 |
c = fgetc(p->in); |
|
|
15171 |
if( c==EOF || seenInterrupt ){ |
|
|
15172 |
p->cTerm = EOF; |
|
|
15173 |
return 0; |
|
|
15174 |
} |
|
|
15175 |
if( c=='"' ){ |
|
|
15176 |
int pc, ppc; |
|
|
15177 |
int startLine = p->nLine; |
|
|
15178 |
int cQuote = c; |
|
|
15179 |
pc = ppc = 0; |
|
|
15180 |
while( 1 ){ |
|
|
15181 |
c = fgetc(p->in); |
|
|
15182 |
if( c==rSep ) p->nLine++; |
|
|
15183 |
if( c==cQuote ){ |
|
|
15184 |
if( pc==cQuote ){ |
|
|
15185 |
pc = 0; |
|
|
15186 |
continue; |
|
|
15187 |
} |
|
|
15188 |
} |
|
|
15189 |
if( (c==cSep && pc==cQuote) |
|
|
15190 |
|| (c==rSep && pc==cQuote) |
|
|
15191 |
|| (c==rSep && pc=='\r' && ppc==cQuote) |
|
|
15192 |
|| (c==EOF && pc==cQuote) |
|
|
15193 |
){ |
|
|
15194 |
do{ p->n--; }while( p->z[p->n]!=cQuote ); |
|
|
15195 |
p->cTerm = c; |
|
|
15196 |
break; |
|
|
15197 |
} |
|
|
15198 |
if( pc==cQuote && c!='\r' ){ |
|
|
15199 |
utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
|
|
15200 |
p->zFile, p->nLine, cQuote); |
|
|
15201 |
} |
|
|
15202 |
if( c==EOF ){ |
|
|
15203 |
utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
|
|
15204 |
p->zFile, startLine, cQuote); |
|
|
15205 |
p->cTerm = c; |
|
|
15206 |
break; |
|
|
15207 |
} |
|
|
15208 |
import_append_char(p, c); |
|
|
15209 |
ppc = pc; |
|
|
15210 |
pc = c; |
|
|
15211 |
} |
|
|
15212 |
}else{ |
|
|
15213 |
/* If this is the first field being parsed and it begins with the |
|
|
15214 |
** UTF-8 BOM (0xEF BB BF) then skip the BOM */ |
|
|
15215 |
if( (c&0xff)==0xef && p->bNotFirst==0 ){ |
|
|
15216 |
import_append_char(p, c); |
|
|
15217 |
c = fgetc(p->in); |
|
|
15218 |
if( (c&0xff)==0xbb ){ |
|
|
15219 |
import_append_char(p, c); |
|
|
15220 |
c = fgetc(p->in); |
|
|
15221 |
if( (c&0xff)==0xbf ){ |
|
|
15222 |
p->bNotFirst = 1; |
|
|
15223 |
p->n = 0; |
|
|
15224 |
return csv_read_one_field(p); |
|
|
15225 |
} |
|
|
15226 |
} |
|
|
15227 |
} |
|
|
15228 |
while( c!=EOF && c!=cSep && c!=rSep ){ |
|
|
15229 |
import_append_char(p, c); |
|
|
15230 |
c = fgetc(p->in); |
|
|
15231 |
} |
|
|
15232 |
if( c==rSep ){ |
|
|
15233 |
p->nLine++; |
|
|
15234 |
if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
|
|
15235 |
} |
|
|
15236 |
p->cTerm = c; |
|
|
15237 |
} |
|
|
15238 |
if( p->z ) p->z[p->n] = 0; |
|
|
15239 |
p->bNotFirst = 1; |
|
|
15240 |
return p->z; |
|
|
15241 |
} |
|
|
15242 |
|
|
|
15243 |
/* Read a single field of ASCII delimited text. |
|
|
15244 |
** |
|
|
15245 |
** + Input comes from p->in. |
|
|
15246 |
** + Store results in p->z of length p->n. Space to hold p->z comes |
|
|
15247 |
** from sqlite3_malloc64(). |
|
|
15248 |
** + Use p->cSep as the column separator. The default is "\x1F". |
|
|
15249 |
** + Use p->rSep as the row separator. The default is "\x1E". |
|
|
15250 |
** + Keep track of the row number in p->nLine. |
|
|
15251 |
** + Store the character that terminates the field in p->cTerm. Store |
|
|
15252 |
** EOF on end-of-file. |
|
|
15253 |
** + Report syntax errors on stderr |
|
|
15254 |
*/ |
|
|
15255 |
static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
|
|
15256 |
int c; |
|
|
15257 |
int cSep = p->cColSep; |
|
|
15258 |
int rSep = p->cRowSep; |
|
|
15259 |
p->n = 0; |
|
|
15260 |
c = fgetc(p->in); |
|
|
15261 |
if( c==EOF || seenInterrupt ){ |
|
|
15262 |
p->cTerm = EOF; |
|
|
15263 |
return 0; |
|
|
15264 |
} |
|
|
15265 |
while( c!=EOF && c!=cSep && c!=rSep ){ |
|
|
15266 |
import_append_char(p, c); |
|
|
15267 |
c = fgetc(p->in); |
|
|
15268 |
} |
|
|
15269 |
if( c==rSep ){ |
|
|
15270 |
p->nLine++; |
|
|
15271 |
} |
|
|
15272 |
p->cTerm = c; |
|
|
15273 |
if( p->z ) p->z[p->n] = 0; |
|
|
15274 |
return p->z; |
|
|
15275 |
} |
|
|
15276 |
|
|
|
15277 |
/* |
|
|
15278 |
** Try to transfer data for table zTable. If an error is seen while |
|
|
15279 |
** moving forward, try to go backwards. The backwards movement won't |
|
|
15280 |
** work for WITHOUT ROWID tables. |
|
|
15281 |
*/ |
|
|
15282 |
static void tryToCloneData( |
|
|
15283 |
ShellState *p, |
|
|
15284 |
sqlite3 *newDb, |
|
|
15285 |
const char *zTable |
|
|
15286 |
){ |
|
|
15287 |
sqlite3_stmt *pQuery = 0; |
|
|
15288 |
sqlite3_stmt *pInsert = 0; |
|
|
15289 |
char *zQuery = 0; |
|
|
15290 |
char *zInsert = 0; |
|
|
15291 |
int rc; |
|
|
15292 |
int i, j, n; |
|
|
15293 |
int nTable = strlen30(zTable); |
|
|
15294 |
int k = 0; |
|
|
15295 |
int cnt = 0; |
|
|
15296 |
const int spinRate = 10000; |
|
|
15297 |
|
|
|
15298 |
zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
|
|
15299 |
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
|
|
15300 |
if( rc ){ |
|
|
15301 |
utf8_printf(stderr, "Error %d: %s on [%s]\n", |
|
|
15302 |
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
|
|
15303 |
zQuery); |
|
|
15304 |
goto end_data_xfer; |
|
|
15305 |
} |
|
|
15306 |
n = sqlite3_column_count(pQuery); |
|
|
15307 |
zInsert = sqlite3_malloc64(200 + nTable + n*3); |
|
|
15308 |
if( zInsert==0 ) shell_out_of_memory(); |
|
|
15309 |
sqlite3_snprintf(200+nTable,zInsert, |
|
|
15310 |
"INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
|
|
15311 |
i = strlen30(zInsert); |
|
|
15312 |
for(j=1; j |
|
|
15313 |
memcpy(zInsert+i, ",?", 2); |
|
|
15314 |
i += 2; |
|
|
15315 |
} |
|
|
15316 |
memcpy(zInsert+i, ");", 3); |
|
|
15317 |
rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
|
|
15318 |
if( rc ){ |
|
|
15319 |
utf8_printf(stderr, "Error %d: %s on [%s]\n", |
|
|
15320 |
sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
|
|
15321 |
zQuery); |
|
|
15322 |
goto end_data_xfer; |
|
|
15323 |
} |
|
|
15324 |
for(k=0; k<2; k++){ |
|
|
15325 |
while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
|
|
15326 |
for(i=0; i |
|
|
15327 |
switch( sqlite3_column_type(pQuery, i) ){ |
|
|
15328 |
case SQLITE_NULL: { |
|
|
15329 |
sqlite3_bind_null(pInsert, i+1); |
|
|
15330 |
break; |
|
|
15331 |
} |
|
|
15332 |
case SQLITE_INTEGER: { |
|
|
15333 |
sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
|
|
15334 |
break; |
|
|
15335 |
} |
|
|
15336 |
case SQLITE_FLOAT: { |
|
|
15337 |
sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
|
|
15338 |
break; |
|
|
15339 |
} |
|
|
15340 |
case SQLITE_TEXT: { |
|
|
15341 |
sqlite3_bind_text(pInsert, i+1, |
|
|
15342 |
(const char*)sqlite3_column_text(pQuery,i), |
|
|
15343 |
-1, SQLITE_STATIC); |
|
|
15344 |
break; |
|
|
15345 |
} |
|
|
15346 |
case SQLITE_BLOB: { |
|
|
15347 |
sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
|
|
15348 |
sqlite3_column_bytes(pQuery,i), |
|
|
15349 |
SQLITE_STATIC); |
|
|
15350 |
break; |
|
|
15351 |
} |
|
|
15352 |
} |
|
|
15353 |
} /* End for */ |
|
|
15354 |
rc = sqlite3_step(pInsert); |
|
|
15355 |
if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
|
|
15356 |
utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
|
|
15357 |
sqlite3_errmsg(newDb)); |
|
|
15358 |
} |
|
|
15359 |
sqlite3_reset(pInsert); |
|
|
15360 |
cnt++; |
|
|
15361 |
if( (cnt%spinRate)==0 ){ |
|
|
15362 |
printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
|
|
15363 |
fflush(stdout); |
|
|
15364 |
} |
|
|
15365 |
} /* End while */ |
|
|
15366 |
if( rc==SQLITE_DONE ) break; |
|
|
15367 |
sqlite3_finalize(pQuery); |
|
|
15368 |
sqlite3_free(zQuery); |
|
|
15369 |
zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
|
|
15370 |
zTable); |
|
|
15371 |
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
|
|
15372 |
if( rc ){ |
|
|
15373 |
utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
|
|
15374 |
break; |
|
|
15375 |
} |
|
|
15376 |
} /* End for(k=0...) */ |
|
|
15377 |
|
|
|
15378 |
end_data_xfer: |
|
|
15379 |
sqlite3_finalize(pQuery); |
|
|
15380 |
sqlite3_finalize(pInsert); |
|
|
15381 |
sqlite3_free(zQuery); |
|
|
15382 |
sqlite3_free(zInsert); |
|
|
15383 |
} |
|
|
15384 |
|
|
|
15385 |
|
|
|
15386 |
/* |
|
|
15387 |
** Try to transfer all rows of the schema that match zWhere. For |
|
|
15388 |
** each row, invoke xForEach() on the object defined by that row. |
|
|
15389 |
** If an error is encountered while moving forward through the |
|
|
15390 |
** sqlite_schema table, try again moving backwards. |
|
|
15391 |
*/ |
|
|
15392 |
static void tryToCloneSchema( |
|
|
15393 |
ShellState *p, |
|
|
15394 |
sqlite3 *newDb, |
|
|
15395 |
const char *zWhere, |
|
|
15396 |
void (*xForEach)(ShellState*,sqlite3*,const char*) |
|
|
15397 |
){ |
|
|
15398 |
sqlite3_stmt *pQuery = 0; |
|
|
15399 |
char *zQuery = 0; |
|
|
15400 |
int rc; |
|
|
15401 |
const unsigned char *zName; |
|
|
15402 |
const unsigned char *zSql; |
|
|
15403 |
char *zErrMsg = 0; |
|
|
15404 |
|
|
|
15405 |
zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" |
|
|
15406 |
" WHERE %s", zWhere); |
|
|
15407 |
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
|
|
15408 |
if( rc ){ |
|
|
15409 |
utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
|
|
15410 |
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
|
|
15411 |
zQuery); |
|
|
15412 |
goto end_schema_xfer; |
|
|
15413 |
} |
|
|
15414 |
while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
|
|
15415 |
zName = sqlite3_column_text(pQuery, 0); |
|
|
15416 |
zSql = sqlite3_column_text(pQuery, 1); |
|
|
15417 |
printf("%s... ", zName); fflush(stdout); |
|
|
15418 |
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
|
|
15419 |
if( zErrMsg ){ |
|
|
15420 |
utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
|
|
15421 |
sqlite3_free(zErrMsg); |
|
|
15422 |
zErrMsg = 0; |
|
|
15423 |
} |
|
|
15424 |
if( xForEach ){ |
|
|
15425 |
xForEach(p, newDb, (const char*)zName); |
|
|
15426 |
} |
|
|
15427 |
printf("done\n"); |
|
|
15428 |
} |
|
|
15429 |
if( rc!=SQLITE_DONE ){ |
|
|
15430 |
sqlite3_finalize(pQuery); |
|
|
15431 |
sqlite3_free(zQuery); |
|
|
15432 |
zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema" |
|
|
15433 |
" WHERE %s ORDER BY rowid DESC", zWhere); |
|
|
15434 |
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
|
|
15435 |
if( rc ){ |
|
|
15436 |
utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
|
|
15437 |
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
|
|
15438 |
zQuery); |
|
|
15439 |
goto end_schema_xfer; |
|
|
15440 |
} |
|
|
15441 |
while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
|
|
15442 |
zName = sqlite3_column_text(pQuery, 0); |
|
|
15443 |
zSql = sqlite3_column_text(pQuery, 1); |
|
|
15444 |
printf("%s... ", zName); fflush(stdout); |
|
|
15445 |
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
|
|
15446 |
if( zErrMsg ){ |
|
|
15447 |
utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
|
|
15448 |
sqlite3_free(zErrMsg); |
|
|
15449 |
zErrMsg = 0; |
|
|
15450 |
} |
|
|
15451 |
if( xForEach ){ |
|
|
15452 |
xForEach(p, newDb, (const char*)zName); |
|
|
15453 |
} |
|
|
15454 |
printf("done\n"); |
|
|
15455 |
} |
|
|
15456 |
} |
|
|
15457 |
end_schema_xfer: |
|
|
15458 |
sqlite3_finalize(pQuery); |
|
|
15459 |
sqlite3_free(zQuery); |
|
|
15460 |
} |
|
|
15461 |
|
|
|
15462 |
/* |
|
|
15463 |
** Open a new database file named "zNewDb". Try to recover as much information |
|
|
15464 |
** as possible out of the main database (which might be corrupt) and write it |
|
|
15465 |
** into zNewDb. |
|
|
15466 |
*/ |
|
|
15467 |
static void tryToClone(ShellState *p, const char *zNewDb){ |
|
|
15468 |
int rc; |
|
|
15469 |
sqlite3 *newDb = 0; |
|
|
15470 |
if( access(zNewDb,0)==0 ){ |
|
|
15471 |
utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
|
|
15472 |
return; |
|
|
15473 |
} |
|
|
15474 |
rc = sqlite3_open(zNewDb, &newDb); |
|
|
15475 |
if( rc ){ |
|
|
15476 |
utf8_printf(stderr, "Cannot create output database: %s\n", |
|
|
15477 |
sqlite3_errmsg(newDb)); |
|
|
15478 |
}else{ |
|
|
15479 |
sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
|
|
15480 |
sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
|
|
15481 |
tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
|
|
15482 |
tryToCloneSchema(p, newDb, "type!='table'", 0); |
|
|
15483 |
sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
|
|
15484 |
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
|
|
15485 |
} |
|
|
15486 |
close_db(newDb); |
|
|
15487 |
} |
|
|
15488 |
|
|
|
15489 |
/* |
|
|
15490 |
** Change the output file back to stdout. |
|
|
15491 |
** |
|
|
15492 |
** If the p->doXdgOpen flag is set, that means the output was being |
|
|
15493 |
** redirected to a temporary file named by p->zTempFile. In that case, |
|
|
15494 |
** launch start/open/xdg-open on that temporary file. |
|
|
15495 |
*/ |
|
|
15496 |
static void output_reset(ShellState *p){ |
|
|
15497 |
if( p->outfile[0]=='|' ){ |
|
|
15498 |
#ifndef SQLITE_OMIT_POPEN |
|
|
15499 |
pclose(p->out); |
|
|
15500 |
#endif |
|
|
15501 |
}else{ |
|
|
15502 |
output_file_close(p->out); |
|
|
15503 |
#ifndef SQLITE_NOHAVE_SYSTEM |
|
|
15504 |
if( p->doXdgOpen ){ |
|
|
15505 |
const char *zXdgOpenCmd = |
|
|
15506 |
#if defined(_WIN32) |
|
|
15507 |
"start"; |
|
|
15508 |
#elif defined(__APPLE__) |
|
|
15509 |
"open"; |
|
|
15510 |
#else |
|
|
15511 |
"xdg-open"; |
|
|
15512 |
#endif |
|
|
15513 |
char *zCmd; |
|
|
15514 |
zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); |
|
|
15515 |
if( system(zCmd) ){ |
|
|
15516 |
utf8_printf(stderr, "Failed: [%s]\n", zCmd); |
|
|
15517 |
}else{ |
|
|
15518 |
/* Give the start/open/xdg-open command some time to get |
|
|
15519 |
** going before we continue, and potential delete the |
|
|
15520 |
** p->zTempFile data file out from under it */ |
|
|
15521 |
sqlite3_sleep(2000); |
|
|
15522 |
} |
|
|
15523 |
sqlite3_free(zCmd); |
|
|
15524 |
outputModePop(p); |
|
|
15525 |
p->doXdgOpen = 0; |
|
|
15526 |
} |
|
|
15527 |
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ |
|
|
15528 |
} |
|
|
15529 |
p->outfile[0] = 0; |
|
|
15530 |
p->out = stdout; |
|
|
15531 |
} |
|
|
15532 |
|
|
|
15533 |
/* |
|
|
15534 |
** Run an SQL command and return the single integer result. |
|
|
15535 |
*/ |
|
|
15536 |
static int db_int(ShellState *p, const char *zSql){ |
|
|
15537 |
sqlite3_stmt *pStmt; |
|
|
15538 |
int res = 0; |
|
|
15539 |
sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
|
15540 |
if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
15541 |
res = sqlite3_column_int(pStmt,0); |
|
|
15542 |
} |
|
|
15543 |
sqlite3_finalize(pStmt); |
|
|
15544 |
return res; |
|
|
15545 |
} |
|
|
15546 |
|
|
|
15547 |
/* |
|
|
15548 |
** Convert a 2-byte or 4-byte big-endian integer into a native integer |
|
|
15549 |
*/ |
|
|
15550 |
static unsigned int get2byteInt(unsigned char *a){ |
|
|
15551 |
return (a[0]<<8) + a[1]; |
|
|
15552 |
} |
|
|
15553 |
static unsigned int get4byteInt(unsigned char *a){ |
|
|
15554 |
return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
|
|
15555 |
} |
|
|
15556 |
|
|
|
15557 |
/* |
|
|
15558 |
** Implementation of the ".dbinfo" command. |
|
|
15559 |
** |
|
|
15560 |
** Return 1 on error, 2 to exit, and 0 otherwise. |
|
|
15561 |
*/ |
|
|
15562 |
static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
|
|
15563 |
static const struct { const char *zName; int ofst; } aField[] = { |
|
|
15564 |
{ "file change counter:", 24 }, |
|
|
15565 |
{ "database page count:", 28 }, |
|
|
15566 |
{ "freelist page count:", 36 }, |
|
|
15567 |
{ "schema cookie:", 40 }, |
|
|
15568 |
{ "schema format:", 44 }, |
|
|
15569 |
{ "default cache size:", 48 }, |
|
|
15570 |
{ "autovacuum top root:", 52 }, |
|
|
15571 |
{ "incremental vacuum:", 64 }, |
|
|
15572 |
{ "text encoding:", 56 }, |
|
|
15573 |
{ "user version:", 60 }, |
|
|
15574 |
{ "application id:", 68 }, |
|
|
15575 |
{ "software version:", 96 }, |
|
|
15576 |
}; |
|
|
15577 |
static const struct { const char *zName; const char *zSql; } aQuery[] = { |
|
|
15578 |
{ "number of tables:", |
|
|
15579 |
"SELECT count(*) FROM %s WHERE type='table'" }, |
|
|
15580 |
{ "number of indexes:", |
|
|
15581 |
"SELECT count(*) FROM %s WHERE type='index'" }, |
|
|
15582 |
{ "number of triggers:", |
|
|
15583 |
"SELECT count(*) FROM %s WHERE type='trigger'" }, |
|
|
15584 |
{ "number of views:", |
|
|
15585 |
"SELECT count(*) FROM %s WHERE type='view'" }, |
|
|
15586 |
{ "schema size:", |
|
|
15587 |
"SELECT total(length(sql)) FROM %s" }, |
|
|
15588 |
}; |
|
|
15589 |
int i, rc; |
|
|
15590 |
unsigned iDataVersion; |
|
|
15591 |
char *zSchemaTab; |
|
|
15592 |
char *zDb = nArg>=2 ? azArg[1] : "main"; |
|
|
15593 |
sqlite3_stmt *pStmt = 0; |
|
|
15594 |
unsigned char aHdr[100]; |
|
|
15595 |
open_db(p, 0); |
|
|
15596 |
if( p->db==0 ) return 1; |
|
|
15597 |
rc = sqlite3_prepare_v2(p->db, |
|
|
15598 |
"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", |
|
|
15599 |
-1, &pStmt, 0); |
|
|
15600 |
if( rc ){ |
|
|
15601 |
utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db)); |
|
|
15602 |
sqlite3_finalize(pStmt); |
|
|
15603 |
return 1; |
|
|
15604 |
} |
|
|
15605 |
sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); |
|
|
15606 |
if( sqlite3_step(pStmt)==SQLITE_ROW |
|
|
15607 |
&& sqlite3_column_bytes(pStmt,0)>100 |
|
|
15608 |
){ |
|
|
15609 |
memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100); |
|
|
15610 |
sqlite3_finalize(pStmt); |
|
|
15611 |
}else{ |
|
|
15612 |
raw_printf(stderr, "unable to read database header\n"); |
|
|
15613 |
sqlite3_finalize(pStmt); |
|
|
15614 |
return 1; |
|
|
15615 |
} |
|
|
15616 |
i = get2byteInt(aHdr+16); |
|
|
15617 |
if( i==1 ) i = 65536; |
|
|
15618 |
utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
|
|
15619 |
utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
|
|
15620 |
utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
|
|
15621 |
utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
|
|
15622 |
for(i=0; i |
|
|
15623 |
int ofst = aField[i].ofst; |
|
|
15624 |
unsigned int val = get4byteInt(aHdr + ofst); |
|
|
15625 |
utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
|
|
15626 |
switch( ofst ){ |
|
|
15627 |
case 56: { |
|
|
15628 |
if( val==1 ) raw_printf(p->out, " (utf8)"); |
|
|
15629 |
if( val==2 ) raw_printf(p->out, " (utf16le)"); |
|
|
15630 |
if( val==3 ) raw_printf(p->out, " (utf16be)"); |
|
|
15631 |
} |
|
|
15632 |
} |
|
|
15633 |
raw_printf(p->out, "\n"); |
|
|
15634 |
} |
|
|
15635 |
if( zDb==0 ){ |
|
|
15636 |
zSchemaTab = sqlite3_mprintf("main.sqlite_schema"); |
|
|
15637 |
}else if( strcmp(zDb,"temp")==0 ){ |
|
|
15638 |
zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema"); |
|
|
15639 |
}else{ |
|
|
15640 |
zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb); |
|
|
15641 |
} |
|
|
15642 |
for(i=0; i |
|
|
15643 |
char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
|
|
15644 |
int val = db_int(p, zSql); |
|
|
15645 |
sqlite3_free(zSql); |
|
|
15646 |
utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
|
|
15647 |
} |
|
|
15648 |
sqlite3_free(zSchemaTab); |
|
|
15649 |
sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); |
|
|
15650 |
utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion); |
|
|
15651 |
return 0; |
|
|
15652 |
} |
|
|
15653 |
|
|
|
15654 |
/* |
|
|
15655 |
** Print the current sqlite3_errmsg() value to stderr and return 1. |
|
|
15656 |
*/ |
|
|
15657 |
static int shellDatabaseError(sqlite3 *db){ |
|
|
15658 |
const char *zErr = sqlite3_errmsg(db); |
|
|
15659 |
utf8_printf(stderr, "Error: %s\n", zErr); |
|
|
15660 |
return 1; |
|
|
15661 |
} |
|
|
15662 |
|
|
|
15663 |
/* |
|
|
15664 |
** Compare the pattern in zGlob[] against the text in z[]. Return TRUE |
|
|
15665 |
** if they match and FALSE (0) if they do not match. |
|
|
15666 |
** |
|
|
15667 |
** Globbing rules: |
|
|
15668 |
** |
|
|
15669 |
** '*' Matches any sequence of zero or more characters. |
|
|
15670 |
** |
|
|
15671 |
** '?' Matches exactly one character. |
|
|
15672 |
** |
|
|
15673 |
** [...] Matches one character from the enclosed list of |
|
|
15674 |
** characters. |
|
|
15675 |
** |
|
|
15676 |
** [^...] Matches one character not in the enclosed list. |
|
|
15677 |
** |
|
|
15678 |
** '#' Matches any sequence of one or more digits with an |
|
|
15679 |
** optional + or - sign in front |
|
|
15680 |
** |
|
|
15681 |
** ' ' Any span of whitespace matches any other span of |
|
|
15682 |
** whitespace. |
|
|
15683 |
** |
|
|
15684 |
** Extra whitespace at the end of z[] is ignored. |
|
|
15685 |
*/ |
|
|
15686 |
static int testcase_glob(const char *zGlob, const char *z){ |
|
|
15687 |
int c, c2; |
|
|
15688 |
int invert; |
|
|
15689 |
int seen; |
|
|
15690 |
|
|
|
15691 |
while( (c = (*(zGlob++)))!=0 ){ |
|
|
15692 |
if( IsSpace(c) ){ |
|
|
15693 |
if( !IsSpace(*z) ) return 0; |
|
|
15694 |
while( IsSpace(*zGlob) ) zGlob++; |
|
|
15695 |
while( IsSpace(*z) ) z++; |
|
|
15696 |
}else if( c=='*' ){ |
|
|
15697 |
while( (c=(*(zGlob++))) == '*' || c=='?' ){ |
|
|
15698 |
if( c=='?' && (*(z++))==0 ) return 0; |
|
|
15699 |
} |
|
|
15700 |
if( c==0 ){ |
|
|
15701 |
return 1; |
|
|
15702 |
}else if( c=='[' ){ |
|
|
15703 |
while( *z && testcase_glob(zGlob-1,z)==0 ){ |
|
|
15704 |
z++; |
|
|
15705 |
} |
|
|
15706 |
return (*z)!=0; |
|
|
15707 |
} |
|
|
15708 |
while( (c2 = (*(z++)))!=0 ){ |
|
|
15709 |
while( c2!=c ){ |
|
|
15710 |
c2 = *(z++); |
|
|
15711 |
if( c2==0 ) return 0; |
|
|
15712 |
} |
|
|
15713 |
if( testcase_glob(zGlob,z) ) return 1; |
|
|
15714 |
} |
|
|
15715 |
return 0; |
|
|
15716 |
}else if( c=='?' ){ |
|
|
15717 |
if( (*(z++))==0 ) return 0; |
|
|
15718 |
}else if( c=='[' ){ |
|
|
15719 |
int prior_c = 0; |
|
|
15720 |
seen = 0; |
|
|
15721 |
invert = 0; |
|
|
15722 |
c = *(z++); |
|
|
15723 |
if( c==0 ) return 0; |
|
|
15724 |
c2 = *(zGlob++); |
|
|
15725 |
if( c2=='^' ){ |
|
|
15726 |
invert = 1; |
|
|
15727 |
c2 = *(zGlob++); |
|
|
15728 |
} |
|
|
15729 |
if( c2==']' ){ |
|
|
15730 |
if( c==']' ) seen = 1; |
|
|
15731 |
c2 = *(zGlob++); |
|
|
15732 |
} |
|
|
15733 |
while( c2 && c2!=']' ){ |
|
|
15734 |
if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ |
|
|
15735 |
c2 = *(zGlob++); |
|
|
15736 |
if( c>=prior_c && c<=c2 ) seen = 1; |
|
|
15737 |
prior_c = 0; |
|
|
15738 |
}else{ |
|
|
15739 |
if( c==c2 ){ |
|
|
15740 |
seen = 1; |
|
|
15741 |
} |
|
|
15742 |
prior_c = c2; |
|
|
15743 |
} |
|
|
15744 |
c2 = *(zGlob++); |
|
|
15745 |
} |
|
|
15746 |
if( c2==0 || (seen ^ invert)==0 ) return 0; |
|
|
15747 |
}else if( c=='#' ){ |
|
|
15748 |
if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; |
|
|
15749 |
if( !IsDigit(z[0]) ) return 0; |
|
|
15750 |
z++; |
|
|
15751 |
while( IsDigit(z[0]) ){ z++; } |
|
|
15752 |
}else{ |
|
|
15753 |
if( c!=(*(z++)) ) return 0; |
|
|
15754 |
} |
|
|
15755 |
} |
|
|
15756 |
while( IsSpace(*z) ){ z++; } |
|
|
15757 |
return *z==0; |
|
|
15758 |
} |
|
|
15759 |
|
|
|
15760 |
|
|
|
15761 |
/* |
|
|
15762 |
** Compare the string as a command-line option with either one or two |
|
|
15763 |
** initial "-" characters. |
|
|
15764 |
*/ |
|
|
15765 |
static int optionMatch(const char *zStr, const char *zOpt){ |
|
|
15766 |
if( zStr[0]!='-' ) return 0; |
|
|
15767 |
zStr++; |
|
|
15768 |
if( zStr[0]=='-' ) zStr++; |
|
|
15769 |
return strcmp(zStr, zOpt)==0; |
|
|
15770 |
} |
|
|
15771 |
|
|
|
15772 |
/* |
|
|
15773 |
** Delete a file. |
|
|
15774 |
*/ |
|
|
15775 |
int shellDeleteFile(const char *zFilename){ |
|
|
15776 |
int rc; |
|
|
15777 |
#ifdef _WIN32 |
|
|
15778 |
wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); |
|
|
15779 |
rc = _wunlink(z); |
|
|
15780 |
sqlite3_free(z); |
|
|
15781 |
#else |
|
|
15782 |
rc = unlink(zFilename); |
|
|
15783 |
#endif |
|
|
15784 |
return rc; |
|
|
15785 |
} |
|
|
15786 |
|
|
|
15787 |
/* |
|
|
15788 |
** Try to delete the temporary file (if there is one) and free the |
|
|
15789 |
** memory used to hold the name of the temp file. |
|
|
15790 |
*/ |
|
|
15791 |
static void clearTempFile(ShellState *p){ |
|
|
15792 |
if( p->zTempFile==0 ) return; |
|
|
15793 |
if( p->doXdgOpen ) return; |
|
|
15794 |
if( shellDeleteFile(p->zTempFile) ) return; |
|
|
15795 |
sqlite3_free(p->zTempFile); |
|
|
15796 |
p->zTempFile = 0; |
|
|
15797 |
} |
|
|
15798 |
|
|
|
15799 |
/* |
|
|
15800 |
** Create a new temp file name with the given suffix. |
|
|
15801 |
*/ |
|
|
15802 |
static void newTempFile(ShellState *p, const char *zSuffix){ |
|
|
15803 |
clearTempFile(p); |
|
|
15804 |
sqlite3_free(p->zTempFile); |
|
|
15805 |
p->zTempFile = 0; |
|
|
15806 |
if( p->db ){ |
|
|
15807 |
sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); |
|
|
15808 |
} |
|
|
15809 |
if( p->zTempFile==0 ){ |
|
|
15810 |
/* If p->db is an in-memory database then the TEMPFILENAME file-control |
|
|
15811 |
** will not work and we will need to fallback to guessing */ |
|
|
15812 |
char *zTemp; |
|
|
15813 |
sqlite3_uint64 r; |
|
|
15814 |
sqlite3_randomness(sizeof(r), &r); |
|
|
15815 |
zTemp = getenv("TEMP"); |
|
|
15816 |
if( zTemp==0 ) zTemp = getenv("TMP"); |
|
|
15817 |
if( zTemp==0 ){ |
|
|
15818 |
#ifdef _WIN32 |
|
|
15819 |
zTemp = "\\tmp"; |
|
|
15820 |
#else |
|
|
15821 |
zTemp = "/tmp"; |
|
|
15822 |
#endif |
|
|
15823 |
} |
|
|
15824 |
p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix); |
|
|
15825 |
}else{ |
|
|
15826 |
p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); |
|
|
15827 |
} |
|
|
15828 |
if( p->zTempFile==0 ){ |
|
|
15829 |
raw_printf(stderr, "out of memory\n"); |
|
|
15830 |
exit(1); |
|
|
15831 |
} |
|
|
15832 |
} |
|
|
15833 |
|
|
|
15834 |
|
|
|
15835 |
/* |
|
|
15836 |
** The implementation of SQL scalar function fkey_collate_clause(), used |
|
|
15837 |
** by the ".lint fkey-indexes" command. This scalar function is always |
|
|
15838 |
** called with four arguments - the parent table name, the parent column name, |
|
|
15839 |
** the child table name and the child column name. |
|
|
15840 |
** |
|
|
15841 |
** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') |
|
|
15842 |
** |
|
|
15843 |
** If either of the named tables or columns do not exist, this function |
|
|
15844 |
** returns an empty string. An empty string is also returned if both tables |
|
|
15845 |
** and columns exist but have the same default collation sequence. Or, |
|
|
15846 |
** if both exist but the default collation sequences are different, this |
|
|
15847 |
** function returns the string " COLLATE ", where |
|
|
15848 |
** is the default collation sequence of the parent column. |
|
|
15849 |
*/ |
|
|
15850 |
static void shellFkeyCollateClause( |
|
|
15851 |
sqlite3_context *pCtx, |
|
|
15852 |
int nVal, |
|
|
15853 |
sqlite3_value **apVal |
|
|
15854 |
){ |
|
|
15855 |
sqlite3 *db = sqlite3_context_db_handle(pCtx); |
|
|
15856 |
const char *zParent; |
|
|
15857 |
const char *zParentCol; |
|
|
15858 |
const char *zParentSeq; |
|
|
15859 |
const char *zChild; |
|
|
15860 |
const char *zChildCol; |
|
|
15861 |
const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ |
|
|
15862 |
int rc; |
|
|
15863 |
|
|
|
15864 |
assert( nVal==4 ); |
|
|
15865 |
zParent = (const char*)sqlite3_value_text(apVal[0]); |
|
|
15866 |
zParentCol = (const char*)sqlite3_value_text(apVal[1]); |
|
|
15867 |
zChild = (const char*)sqlite3_value_text(apVal[2]); |
|
|
15868 |
zChildCol = (const char*)sqlite3_value_text(apVal[3]); |
|
|
15869 |
|
|
|
15870 |
sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); |
|
|
15871 |
rc = sqlite3_table_column_metadata( |
|
|
15872 |
db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 |
|
|
15873 |
); |
|
|
15874 |
if( rc==SQLITE_OK ){ |
|
|
15875 |
rc = sqlite3_table_column_metadata( |
|
|
15876 |
db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 |
|
|
15877 |
); |
|
|
15878 |
} |
|
|
15879 |
|
|
|
15880 |
if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ |
|
|
15881 |
char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); |
|
|
15882 |
sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); |
|
|
15883 |
sqlite3_free(z); |
|
|
15884 |
} |
|
|
15885 |
} |
|
|
15886 |
|
|
|
15887 |
|
|
|
15888 |
/* |
|
|
15889 |
** The implementation of dot-command ".lint fkey-indexes". |
|
|
15890 |
*/ |
|
|
15891 |
static int lintFkeyIndexes( |
|
|
15892 |
ShellState *pState, /* Current shell tool state */ |
|
|
15893 |
char **azArg, /* Array of arguments passed to dot command */ |
|
|
15894 |
int nArg /* Number of entries in azArg[] */ |
|
|
15895 |
){ |
|
|
15896 |
sqlite3 *db = pState->db; /* Database handle to query "main" db of */ |
|
|
15897 |
FILE *out = pState->out; /* Stream to write non-error output to */ |
|
|
15898 |
int bVerbose = 0; /* If -verbose is present */ |
|
|
15899 |
int bGroupByParent = 0; /* If -groupbyparent is present */ |
|
|
15900 |
int i; /* To iterate through azArg[] */ |
|
|
15901 |
const char *zIndent = ""; /* How much to indent CREATE INDEX by */ |
|
|
15902 |
int rc; /* Return code */ |
|
|
15903 |
sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ |
|
|
15904 |
|
|
|
15905 |
/* |
|
|
15906 |
** This SELECT statement returns one row for each foreign key constraint |
|
|
15907 |
** in the schema of the main database. The column values are: |
|
|
15908 |
** |
|
|
15909 |
** 0. The text of an SQL statement similar to: |
|
|
15910 |
** |
|
|
15911 |
** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?" |
|
|
15912 |
** |
|
|
15913 |
** This SELECT is similar to the one that the foreign keys implementation |
|
|
15914 |
** needs to run internally on child tables. If there is an index that can |
|
|
15915 |
** be used to optimize this query, then it can also be used by the FK |
|
|
15916 |
** implementation to optimize DELETE or UPDATE statements on the parent |
|
|
15917 |
** table. |
|
|
15918 |
** |
|
|
15919 |
** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by |
|
|
15920 |
** the EXPLAIN QUERY PLAN command matches this pattern, then the schema |
|
|
15921 |
** contains an index that can be used to optimize the query. |
|
|
15922 |
** |
|
|
15923 |
** 2. Human readable text that describes the child table and columns. e.g. |
|
|
15924 |
** |
|
|
15925 |
** "child_table(child_key1, child_key2)" |
|
|
15926 |
** |
|
|
15927 |
** 3. Human readable text that describes the parent table and columns. e.g. |
|
|
15928 |
** |
|
|
15929 |
** "parent_table(parent_key1, parent_key2)" |
|
|
15930 |
** |
|
|
15931 |
** 4. A full CREATE INDEX statement for an index that could be used to |
|
|
15932 |
** optimize DELETE or UPDATE statements on the parent table. e.g. |
|
|
15933 |
** |
|
|
15934 |
** "CREATE INDEX child_table_child_key ON child_table(child_key)" |
|
|
15935 |
** |
|
|
15936 |
** 5. The name of the parent table. |
|
|
15937 |
** |
|
|
15938 |
** These six values are used by the C logic below to generate the report. |
|
|
15939 |
*/ |
|
|
15940 |
const char *zSql = |
|
|
15941 |
"SELECT " |
|
|
15942 |
" 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '" |
|
|
15943 |
" || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " |
|
|
15944 |
" || fkey_collate_clause(" |
|
|
15945 |
" f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" |
|
|
15946 |
", " |
|
|
15947 |
" 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('" |
|
|
15948 |
" || group_concat('*=?', ' AND ') || ')'" |
|
|
15949 |
", " |
|
|
15950 |
" s.name || '(' || group_concat(f.[from], ', ') || ')'" |
|
|
15951 |
", " |
|
|
15952 |
" f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" |
|
|
15953 |
", " |
|
|
15954 |
" 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" |
|
|
15955 |
" || ' ON ' || quote(s.name) || '('" |
|
|
15956 |
" || group_concat(quote(f.[from]) ||" |
|
|
15957 |
" fkey_collate_clause(" |
|
|
15958 |
" f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" |
|
|
15959 |
" || ');'" |
|
|
15960 |
", " |
|
|
15961 |
" f.[table] " |
|
|
15962 |
"FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f " |
|
|
15963 |
"LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " |
|
|
15964 |
"GROUP BY s.name, f.id " |
|
|
15965 |
"ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" |
|
|
15966 |
; |
|
|
15967 |
const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)"; |
|
|
15968 |
|
|
|
15969 |
for(i=2; i |
|
|
15970 |
int n = strlen30(azArg[i]); |
|
|
15971 |
if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ |
|
|
15972 |
bVerbose = 1; |
|
|
15973 |
} |
|
|
15974 |
else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ |
|
|
15975 |
bGroupByParent = 1; |
|
|
15976 |
zIndent = " "; |
|
|
15977 |
} |
|
|
15978 |
else{ |
|
|
15979 |
raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", |
|
|
15980 |
azArg[0], azArg[1] |
|
|
15981 |
); |
|
|
15982 |
return SQLITE_ERROR; |
|
|
15983 |
} |
|
|
15984 |
} |
|
|
15985 |
|
|
|
15986 |
/* Register the fkey_collate_clause() SQL function */ |
|
|
15987 |
rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, |
|
|
15988 |
0, shellFkeyCollateClause, 0, 0 |
|
|
15989 |
); |
|
|
15990 |
|
|
|
15991 |
|
|
|
15992 |
if( rc==SQLITE_OK ){ |
|
|
15993 |
rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); |
|
|
15994 |
} |
|
|
15995 |
if( rc==SQLITE_OK ){ |
|
|
15996 |
sqlite3_bind_int(pSql, 1, bGroupByParent); |
|
|
15997 |
} |
|
|
15998 |
|
|
|
15999 |
if( rc==SQLITE_OK ){ |
|
|
16000 |
int rc2; |
|
|
16001 |
char *zPrev = 0; |
|
|
16002 |
while( SQLITE_ROW==sqlite3_step(pSql) ){ |
|
|
16003 |
int res = -1; |
|
|
16004 |
sqlite3_stmt *pExplain = 0; |
|
|
16005 |
const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); |
|
|
16006 |
const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); |
|
|
16007 |
const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); |
|
|
16008 |
const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); |
|
|
16009 |
const char *zCI = (const char*)sqlite3_column_text(pSql, 4); |
|
|
16010 |
const char *zParent = (const char*)sqlite3_column_text(pSql, 5); |
|
|
16011 |
|
|
|
16012 |
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
|
|
16013 |
if( rc!=SQLITE_OK ) break; |
|
|
16014 |
if( SQLITE_ROW==sqlite3_step(pExplain) ){ |
|
|
16015 |
const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); |
|
|
16016 |
res = ( |
|
|
16017 |
0==sqlite3_strglob(zGlob, zPlan) |
|
|
16018 |
|| 0==sqlite3_strglob(zGlobIPK, zPlan) |
|
|
16019 |
); |
|
|
16020 |
} |
|
|
16021 |
rc = sqlite3_finalize(pExplain); |
|
|
16022 |
if( rc!=SQLITE_OK ) break; |
|
|
16023 |
|
|
|
16024 |
if( res<0 ){ |
|
|
16025 |
raw_printf(stderr, "Error: internal error"); |
|
|
16026 |
break; |
|
|
16027 |
}else{ |
|
|
16028 |
if( bGroupByParent |
|
|
16029 |
&& (bVerbose || res==0) |
|
|
16030 |
&& (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) |
|
|
16031 |
){ |
|
|
16032 |
raw_printf(out, "-- Parent table %s\n", zParent); |
|
|
16033 |
sqlite3_free(zPrev); |
|
|
16034 |
zPrev = sqlite3_mprintf("%s", zParent); |
|
|
16035 |
} |
|
|
16036 |
|
|
|
16037 |
if( res==0 ){ |
|
|
16038 |
raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); |
|
|
16039 |
}else if( bVerbose ){ |
|
|
16040 |
raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", |
|
|
16041 |
zIndent, zFrom, zTarget |
|
|
16042 |
); |
|
|
16043 |
} |
|
|
16044 |
} |
|
|
16045 |
} |
|
|
16046 |
sqlite3_free(zPrev); |
|
|
16047 |
|
|
|
16048 |
if( rc!=SQLITE_OK ){ |
|
|
16049 |
raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
|
|
16050 |
} |
|
|
16051 |
|
|
|
16052 |
rc2 = sqlite3_finalize(pSql); |
|
|
16053 |
if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ |
|
|
16054 |
rc = rc2; |
|
|
16055 |
raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
|
|
16056 |
} |
|
|
16057 |
}else{ |
|
|
16058 |
raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); |
|
|
16059 |
} |
|
|
16060 |
|
|
|
16061 |
return rc; |
|
|
16062 |
} |
|
|
16063 |
|
|
|
16064 |
/* |
|
|
16065 |
** Implementation of ".lint" dot command. |
|
|
16066 |
*/ |
|
|
16067 |
static int lintDotCommand( |
|
|
16068 |
ShellState *pState, /* Current shell tool state */ |
|
|
16069 |
char **azArg, /* Array of arguments passed to dot command */ |
|
|
16070 |
int nArg /* Number of entries in azArg[] */ |
|
|
16071 |
){ |
|
|
16072 |
int n; |
|
|
16073 |
n = (nArg>=2 ? strlen30(azArg[1]) : 0); |
|
|
16074 |
if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; |
|
|
16075 |
return lintFkeyIndexes(pState, azArg, nArg); |
|
|
16076 |
|
|
|
16077 |
usage: |
|
|
16078 |
raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); |
|
|
16079 |
raw_printf(stderr, "Where sub-commands are:\n"); |
|
|
16080 |
raw_printf(stderr, " fkey-indexes\n"); |
|
|
16081 |
return SQLITE_ERROR; |
|
|
16082 |
} |
|
|
16083 |
|
|
|
16084 |
#if !defined SQLITE_OMIT_VIRTUALTABLE |
|
|
16085 |
static void shellPrepare( |
|
|
16086 |
sqlite3 *db, |
|
|
16087 |
int *pRc, |
|
|
16088 |
const char *zSql, |
|
|
16089 |
sqlite3_stmt **ppStmt |
|
|
16090 |
){ |
|
|
16091 |
*ppStmt = 0; |
|
|
16092 |
if( *pRc==SQLITE_OK ){ |
|
|
16093 |
int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); |
|
|
16094 |
if( rc!=SQLITE_OK ){ |
|
|
16095 |
raw_printf(stderr, "sql error: %s (%d)\n", |
|
|
16096 |
sqlite3_errmsg(db), sqlite3_errcode(db) |
|
|
16097 |
); |
|
|
16098 |
*pRc = rc; |
|
|
16099 |
} |
|
|
16100 |
} |
|
|
16101 |
} |
|
|
16102 |
|
|
|
16103 |
/* |
|
|
16104 |
** Create a prepared statement using printf-style arguments for the SQL. |
|
|
16105 |
** |
|
|
16106 |
** This routine is could be marked "static". But it is not always used, |
|
|
16107 |
** depending on compile-time options. By omitting the "static", we avoid |
|
|
16108 |
** nuisance compiler warnings about "defined but not used". |
|
|
16109 |
*/ |
|
|
16110 |
void shellPreparePrintf( |
|
|
16111 |
sqlite3 *db, |
|
|
16112 |
int *pRc, |
|
|
16113 |
sqlite3_stmt **ppStmt, |
|
|
16114 |
const char *zFmt, |
|
|
16115 |
... |
|
|
16116 |
){ |
|
|
16117 |
*ppStmt = 0; |
|
|
16118 |
if( *pRc==SQLITE_OK ){ |
|
|
16119 |
va_list ap; |
|
|
16120 |
char *z; |
|
|
16121 |
va_start(ap, zFmt); |
|
|
16122 |
z = sqlite3_vmprintf(zFmt, ap); |
|
|
16123 |
va_end(ap); |
|
|
16124 |
if( z==0 ){ |
|
|
16125 |
*pRc = SQLITE_NOMEM; |
|
|
16126 |
}else{ |
|
|
16127 |
shellPrepare(db, pRc, z, ppStmt); |
|
|
16128 |
sqlite3_free(z); |
|
|
16129 |
} |
|
|
16130 |
} |
|
|
16131 |
} |
|
|
16132 |
|
|
|
16133 |
/* Finalize the prepared statement created using shellPreparePrintf(). |
|
|
16134 |
** |
|
|
16135 |
** This routine is could be marked "static". But it is not always used, |
|
|
16136 |
** depending on compile-time options. By omitting the "static", we avoid |
|
|
16137 |
** nuisance compiler warnings about "defined but not used". |
|
|
16138 |
*/ |
|
|
16139 |
void shellFinalize( |
|
|
16140 |
int *pRc, |
|
|
16141 |
sqlite3_stmt *pStmt |
|
|
16142 |
){ |
|
|
16143 |
if( pStmt ){ |
|
|
16144 |
sqlite3 *db = sqlite3_db_handle(pStmt); |
|
|
16145 |
int rc = sqlite3_finalize(pStmt); |
|
|
16146 |
if( *pRc==SQLITE_OK ){ |
|
|
16147 |
if( rc!=SQLITE_OK ){ |
|
|
16148 |
raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); |
|
|
16149 |
} |
|
|
16150 |
*pRc = rc; |
|
|
16151 |
} |
|
|
16152 |
} |
|
|
16153 |
} |
|
|
16154 |
|
|
|
16155 |
/* Reset the prepared statement created using shellPreparePrintf(). |
|
|
16156 |
** |
|
|
16157 |
** This routine is could be marked "static". But it is not always used, |
|
|
16158 |
** depending on compile-time options. By omitting the "static", we avoid |
|
|
16159 |
** nuisance compiler warnings about "defined but not used". |
|
|
16160 |
*/ |
|
|
16161 |
void shellReset( |
|
|
16162 |
int *pRc, |
|
|
16163 |
sqlite3_stmt *pStmt |
|
|
16164 |
){ |
|
|
16165 |
int rc = sqlite3_reset(pStmt); |
|
|
16166 |
if( *pRc==SQLITE_OK ){ |
|
|
16167 |
if( rc!=SQLITE_OK ){ |
|
|
16168 |
sqlite3 *db = sqlite3_db_handle(pStmt); |
|
|
16169 |
raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); |
|
|
16170 |
} |
|
|
16171 |
*pRc = rc; |
|
|
16172 |
} |
|
|
16173 |
} |
|
|
16174 |
#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */ |
|
|
16175 |
|
|
|
16176 |
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
|
|
16177 |
/****************************************************************************** |
|
|
16178 |
** The ".archive" or ".ar" command. |
|
|
16179 |
*/ |
|
|
16180 |
/* |
|
|
16181 |
** Structure representing a single ".ar" command. |
|
|
16182 |
*/ |
|
|
16183 |
typedef struct ArCommand ArCommand; |
|
|
16184 |
struct ArCommand { |
|
|
16185 |
u8 eCmd; /* An AR_CMD_* value */ |
|
|
16186 |
u8 bVerbose; /* True if --verbose */ |
|
|
16187 |
u8 bZip; /* True if the archive is a ZIP */ |
|
|
16188 |
u8 bDryRun; /* True if --dry-run */ |
|
|
16189 |
u8 bAppend; /* True if --append */ |
|
|
16190 |
u8 fromCmdLine; /* Run from -A instead of .archive */ |
|
|
16191 |
int nArg; /* Number of command arguments */ |
|
|
16192 |
char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */ |
|
|
16193 |
const char *zFile; /* --file argument, or NULL */ |
|
|
16194 |
const char *zDir; /* --directory argument, or NULL */ |
|
|
16195 |
char **azArg; /* Array of command arguments */ |
|
|
16196 |
ShellState *p; /* Shell state */ |
|
|
16197 |
sqlite3 *db; /* Database containing the archive */ |
|
|
16198 |
}; |
|
|
16199 |
|
|
|
16200 |
/* |
|
|
16201 |
** Print a usage message for the .ar command to stderr and return SQLITE_ERROR. |
|
|
16202 |
*/ |
|
|
16203 |
static int arUsage(FILE *f){ |
|
|
16204 |
showHelp(f,"archive"); |
|
|
16205 |
return SQLITE_ERROR; |
|
|
16206 |
} |
|
|
16207 |
|
|
|
16208 |
/* |
|
|
16209 |
** Print an error message for the .ar command to stderr and return |
|
|
16210 |
** SQLITE_ERROR. |
|
|
16211 |
*/ |
|
|
16212 |
static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){ |
|
|
16213 |
va_list ap; |
|
|
16214 |
char *z; |
|
|
16215 |
va_start(ap, zFmt); |
|
|
16216 |
z = sqlite3_vmprintf(zFmt, ap); |
|
|
16217 |
va_end(ap); |
|
|
16218 |
utf8_printf(stderr, "Error: %s\n", z); |
|
|
16219 |
if( pAr->fromCmdLine ){ |
|
|
16220 |
utf8_printf(stderr, "Use \"-A\" for more help\n"); |
|
|
16221 |
}else{ |
|
|
16222 |
utf8_printf(stderr, "Use \".archive --help\" for more help\n"); |
|
|
16223 |
} |
|
|
16224 |
sqlite3_free(z); |
|
|
16225 |
return SQLITE_ERROR; |
|
|
16226 |
} |
|
|
16227 |
|
|
|
16228 |
/* |
|
|
16229 |
** Values for ArCommand.eCmd. |
|
|
16230 |
*/ |
|
|
16231 |
#define AR_CMD_CREATE 1 |
|
|
16232 |
#define AR_CMD_UPDATE 2 |
|
|
16233 |
#define AR_CMD_INSERT 3 |
|
|
16234 |
#define AR_CMD_EXTRACT 4 |
|
|
16235 |
#define AR_CMD_LIST 5 |
|
|
16236 |
#define AR_CMD_HELP 6 |
|
|
16237 |
|
|
|
16238 |
/* |
|
|
16239 |
** Other (non-command) switches. |
|
|
16240 |
*/ |
|
|
16241 |
#define AR_SWITCH_VERBOSE 7 |
|
|
16242 |
#define AR_SWITCH_FILE 8 |
|
|
16243 |
#define AR_SWITCH_DIRECTORY 9 |
|
|
16244 |
#define AR_SWITCH_APPEND 10 |
|
|
16245 |
#define AR_SWITCH_DRYRUN 11 |
|
|
16246 |
|
|
|
16247 |
static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){ |
|
|
16248 |
switch( eSwitch ){ |
|
|
16249 |
case AR_CMD_CREATE: |
|
|
16250 |
case AR_CMD_EXTRACT: |
|
|
16251 |
case AR_CMD_LIST: |
|
|
16252 |
case AR_CMD_UPDATE: |
|
|
16253 |
case AR_CMD_INSERT: |
|
|
16254 |
case AR_CMD_HELP: |
|
|
16255 |
if( pAr->eCmd ){ |
|
|
16256 |
return arErrorMsg(pAr, "multiple command options"); |
|
|
16257 |
} |
|
|
16258 |
pAr->eCmd = eSwitch; |
|
|
16259 |
break; |
|
|
16260 |
|
|
|
16261 |
case AR_SWITCH_DRYRUN: |
|
|
16262 |
pAr->bDryRun = 1; |
|
|
16263 |
break; |
|
|
16264 |
case AR_SWITCH_VERBOSE: |
|
|
16265 |
pAr->bVerbose = 1; |
|
|
16266 |
break; |
|
|
16267 |
case AR_SWITCH_APPEND: |
|
|
16268 |
pAr->bAppend = 1; |
|
|
16269 |
/* Fall thru into --file */ |
|
|
16270 |
case AR_SWITCH_FILE: |
|
|
16271 |
pAr->zFile = zArg; |
|
|
16272 |
break; |
|
|
16273 |
case AR_SWITCH_DIRECTORY: |
|
|
16274 |
pAr->zDir = zArg; |
|
|
16275 |
break; |
|
|
16276 |
} |
|
|
16277 |
|
|
|
16278 |
return SQLITE_OK; |
|
|
16279 |
} |
|
|
16280 |
|
|
|
16281 |
/* |
|
|
16282 |
** Parse the command line for an ".ar" command. The results are written into |
|
|
16283 |
** structure (*pAr). SQLITE_OK is returned if the command line is parsed |
|
|
16284 |
** successfully, otherwise an error message is written to stderr and |
|
|
16285 |
** SQLITE_ERROR returned. |
|
|
16286 |
*/ |
|
|
16287 |
static int arParseCommand( |
|
|
16288 |
char **azArg, /* Array of arguments passed to dot command */ |
|
|
16289 |
int nArg, /* Number of entries in azArg[] */ |
|
|
16290 |
ArCommand *pAr /* Populate this object */ |
|
|
16291 |
){ |
|
|
16292 |
struct ArSwitch { |
|
|
16293 |
const char *zLong; |
|
|
16294 |
char cShort; |
|
|
16295 |
u8 eSwitch; |
|
|
16296 |
u8 bArg; |
|
|
16297 |
} aSwitch[] = { |
|
|
16298 |
{ "create", 'c', AR_CMD_CREATE, 0 }, |
|
|
16299 |
{ "extract", 'x', AR_CMD_EXTRACT, 0 }, |
|
|
16300 |
{ "insert", 'i', AR_CMD_INSERT, 0 }, |
|
|
16301 |
{ "list", 't', AR_CMD_LIST, 0 }, |
|
|
16302 |
{ "update", 'u', AR_CMD_UPDATE, 0 }, |
|
|
16303 |
{ "help", 'h', AR_CMD_HELP, 0 }, |
|
|
16304 |
{ "verbose", 'v', AR_SWITCH_VERBOSE, 0 }, |
|
|
16305 |
{ "file", 'f', AR_SWITCH_FILE, 1 }, |
|
|
16306 |
{ "append", 'a', AR_SWITCH_APPEND, 1 }, |
|
|
16307 |
{ "directory", 'C', AR_SWITCH_DIRECTORY, 1 }, |
|
|
16308 |
{ "dryrun", 'n', AR_SWITCH_DRYRUN, 0 }, |
|
|
16309 |
}; |
|
|
16310 |
int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch); |
|
|
16311 |
struct ArSwitch *pEnd = &aSwitch[nSwitch]; |
|
|
16312 |
|
|
|
16313 |
if( nArg<=1 ){ |
|
|
16314 |
utf8_printf(stderr, "Wrong number of arguments. Usage:\n"); |
|
|
16315 |
return arUsage(stderr); |
|
|
16316 |
}else{ |
|
|
16317 |
char *z = azArg[1]; |
|
|
16318 |
if( z[0]!='-' ){ |
|
|
16319 |
/* Traditional style [tar] invocation */ |
|
|
16320 |
int i; |
|
|
16321 |
int iArg = 2; |
|
|
16322 |
for(i=0; z[i]; i++){ |
|
|
16323 |
const char *zArg = 0; |
|
|
16324 |
struct ArSwitch *pOpt; |
|
|
16325 |
for(pOpt=&aSwitch[0]; pOpt |
|
|
16326 |
if( z[i]==pOpt->cShort ) break; |
|
|
16327 |
} |
|
|
16328 |
if( pOpt==pEnd ){ |
|
|
16329 |
return arErrorMsg(pAr, "unrecognized option: %c", z[i]); |
|
|
16330 |
} |
|
|
16331 |
if( pOpt->bArg ){ |
|
|
16332 |
if( iArg>=nArg ){ |
|
|
16333 |
return arErrorMsg(pAr, "option requires an argument: %c",z[i]); |
|
|
16334 |
} |
|
|
16335 |
zArg = azArg[iArg++]; |
|
|
16336 |
} |
|
|
16337 |
if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; |
|
|
16338 |
} |
|
|
16339 |
pAr->nArg = nArg-iArg; |
|
|
16340 |
if( pAr->nArg>0 ){ |
|
|
16341 |
pAr->azArg = &azArg[iArg]; |
|
|
16342 |
} |
|
|
16343 |
}else{ |
|
|
16344 |
/* Non-traditional invocation */ |
|
|
16345 |
int iArg; |
|
|
16346 |
for(iArg=1; iArg |
|
|
16347 |
int n; |
|
|
16348 |
z = azArg[iArg]; |
|
|
16349 |
if( z[0]!='-' ){ |
|
|
16350 |
/* All remaining command line words are command arguments. */ |
|
|
16351 |
pAr->azArg = &azArg[iArg]; |
|
|
16352 |
pAr->nArg = nArg-iArg; |
|
|
16353 |
break; |
|
|
16354 |
} |
|
|
16355 |
n = strlen30(z); |
|
|
16356 |
|
|
|
16357 |
if( z[1]!='-' ){ |
|
|
16358 |
int i; |
|
|
16359 |
/* One or more short options */ |
|
|
16360 |
for(i=1; i |
|
|
16361 |
const char *zArg = 0; |
|
|
16362 |
struct ArSwitch *pOpt; |
|
|
16363 |
for(pOpt=&aSwitch[0]; pOpt |
|
|
16364 |
if( z[i]==pOpt->cShort ) break; |
|
|
16365 |
} |
|
|
16366 |
if( pOpt==pEnd ){ |
|
|
16367 |
return arErrorMsg(pAr, "unrecognized option: %c", z[i]); |
|
|
16368 |
} |
|
|
16369 |
if( pOpt->bArg ){ |
|
|
16370 |
if( i<(n-1) ){ |
|
|
16371 |
zArg = &z[i+1]; |
|
|
16372 |
i = n; |
|
|
16373 |
}else{ |
|
|
16374 |
if( iArg>=(nArg-1) ){ |
|
|
16375 |
return arErrorMsg(pAr, "option requires an argument: %c", |
|
|
16376 |
z[i]); |
|
|
16377 |
} |
|
|
16378 |
zArg = azArg[++iArg]; |
|
|
16379 |
} |
|
|
16380 |
} |
|
|
16381 |
if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; |
|
|
16382 |
} |
|
|
16383 |
}else if( z[2]=='\0' ){ |
|
|
16384 |
/* A -- option, indicating that all remaining command line words |
|
|
16385 |
** are command arguments. */ |
|
|
16386 |
pAr->azArg = &azArg[iArg+1]; |
|
|
16387 |
pAr->nArg = nArg-iArg-1; |
|
|
16388 |
break; |
|
|
16389 |
}else{ |
|
|
16390 |
/* A long option */ |
|
|
16391 |
const char *zArg = 0; /* Argument for option, if any */ |
|
|
16392 |
struct ArSwitch *pMatch = 0; /* Matching option */ |
|
|
16393 |
struct ArSwitch *pOpt; /* Iterator */ |
|
|
16394 |
for(pOpt=&aSwitch[0]; pOpt |
|
|
16395 |
const char *zLong = pOpt->zLong; |
|
|
16396 |
if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ |
|
|
16397 |
if( pMatch ){ |
|
|
16398 |
return arErrorMsg(pAr, "ambiguous option: %s",z); |
|
|
16399 |
}else{ |
|
|
16400 |
pMatch = pOpt; |
|
|
16401 |
} |
|
|
16402 |
} |
|
|
16403 |
} |
|
|
16404 |
|
|
|
16405 |
if( pMatch==0 ){ |
|
|
16406 |
return arErrorMsg(pAr, "unrecognized option: %s", z); |
|
|
16407 |
} |
|
|
16408 |
if( pMatch->bArg ){ |
|
|
16409 |
if( iArg>=(nArg-1) ){ |
|
|
16410 |
return arErrorMsg(pAr, "option requires an argument: %s", z); |
|
|
16411 |
} |
|
|
16412 |
zArg = azArg[++iArg]; |
|
|
16413 |
} |
|
|
16414 |
if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; |
|
|
16415 |
} |
|
|
16416 |
} |
|
|
16417 |
} |
|
|
16418 |
} |
|
|
16419 |
|
|
|
16420 |
return SQLITE_OK; |
|
|
16421 |
} |
|
|
16422 |
|
|
|
16423 |
/* |
|
|
16424 |
** This function assumes that all arguments within the ArCommand.azArg[] |
|
|
16425 |
** array refer to archive members, as for the --extract or --list commands. |
|
|
16426 |
** It checks that each of them are present. If any specified file is not |
|
|
16427 |
** present in the archive, an error is printed to stderr and an error |
|
|
16428 |
** code returned. Otherwise, if all specified arguments are present in |
|
|
16429 |
** the archive, SQLITE_OK is returned. |
|
|
16430 |
** |
|
|
16431 |
** This function strips any trailing '/' characters from each argument. |
|
|
16432 |
** This is consistent with the way the [tar] command seems to work on |
|
|
16433 |
** Linux. |
|
|
16434 |
*/ |
|
|
16435 |
static int arCheckEntries(ArCommand *pAr){ |
|
|
16436 |
int rc = SQLITE_OK; |
|
|
16437 |
if( pAr->nArg ){ |
|
|
16438 |
int i, j; |
|
|
16439 |
sqlite3_stmt *pTest = 0; |
|
|
16440 |
|
|
|
16441 |
shellPreparePrintf(pAr->db, &rc, &pTest, |
|
|
16442 |
"SELECT name FROM %s WHERE name=$name", |
|
|
16443 |
pAr->zSrcTable |
|
|
16444 |
); |
|
|
16445 |
j = sqlite3_bind_parameter_index(pTest, "$name"); |
|
|
16446 |
for(i=0; inArg && rc==SQLITE_OK; i++){ |
|
|
16447 |
char *z = pAr->azArg[i]; |
|
|
16448 |
int n = strlen30(z); |
|
|
16449 |
int bOk = 0; |
|
|
16450 |
while( n>0 && z[n-1]=='/' ) n--; |
|
|
16451 |
z[n] = '\0'; |
|
|
16452 |
sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); |
|
|
16453 |
if( SQLITE_ROW==sqlite3_step(pTest) ){ |
|
|
16454 |
bOk = 1; |
|
|
16455 |
} |
|
|
16456 |
shellReset(&rc, pTest); |
|
|
16457 |
if( rc==SQLITE_OK && bOk==0 ){ |
|
|
16458 |
utf8_printf(stderr, "not found in archive: %s\n", z); |
|
|
16459 |
rc = SQLITE_ERROR; |
|
|
16460 |
} |
|
|
16461 |
} |
|
|
16462 |
shellFinalize(&rc, pTest); |
|
|
16463 |
} |
|
|
16464 |
return rc; |
|
|
16465 |
} |
|
|
16466 |
|
|
|
16467 |
/* |
|
|
16468 |
** Format a WHERE clause that can be used against the "sqlar" table to |
|
|
16469 |
** identify all archive members that match the command arguments held |
|
|
16470 |
** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning. |
|
|
16471 |
** The caller is responsible for eventually calling sqlite3_free() on |
|
|
16472 |
** any non-NULL (*pzWhere) value. |
|
|
16473 |
*/ |
|
|
16474 |
static void arWhereClause( |
|
|
16475 |
int *pRc, |
|
|
16476 |
ArCommand *pAr, |
|
|
16477 |
char **pzWhere /* OUT: New WHERE clause */ |
|
|
16478 |
){ |
|
|
16479 |
char *zWhere = 0; |
|
|
16480 |
if( *pRc==SQLITE_OK ){ |
|
|
16481 |
if( pAr->nArg==0 ){ |
|
|
16482 |
zWhere = sqlite3_mprintf("1"); |
|
|
16483 |
}else{ |
|
|
16484 |
int i; |
|
|
16485 |
const char *zSep = ""; |
|
|
16486 |
for(i=0; inArg; i++){ |
|
|
16487 |
const char *z = pAr->azArg[i]; |
|
|
16488 |
zWhere = sqlite3_mprintf( |
|
|
16489 |
"%z%s name = '%q' OR substr(name,1,%d) = '%q/'", |
|
|
16490 |
zWhere, zSep, z, strlen30(z)+1, z |
|
|
16491 |
); |
|
|
16492 |
if( zWhere==0 ){ |
|
|
16493 |
*pRc = SQLITE_NOMEM; |
|
|
16494 |
break; |
|
|
16495 |
} |
|
|
16496 |
zSep = " OR "; |
|
|
16497 |
} |
|
|
16498 |
} |
|
|
16499 |
} |
|
|
16500 |
*pzWhere = zWhere; |
|
|
16501 |
} |
|
|
16502 |
|
|
|
16503 |
/* |
|
|
16504 |
** Implementation of .ar "lisT" command. |
|
|
16505 |
*/ |
|
|
16506 |
static int arListCommand(ArCommand *pAr){ |
|
|
16507 |
const char *zSql = "SELECT %s FROM %s WHERE %s"; |
|
|
16508 |
const char *azCols[] = { |
|
|
16509 |
"name", |
|
|
16510 |
"lsmode(mode), sz, datetime(mtime, 'unixepoch'), name" |
|
|
16511 |
}; |
|
|
16512 |
|
|
|
16513 |
char *zWhere = 0; |
|
|
16514 |
sqlite3_stmt *pSql = 0; |
|
|
16515 |
int rc; |
|
|
16516 |
|
|
|
16517 |
rc = arCheckEntries(pAr); |
|
|
16518 |
arWhereClause(&rc, pAr, &zWhere); |
|
|
16519 |
|
|
|
16520 |
shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], |
|
|
16521 |
pAr->zSrcTable, zWhere); |
|
|
16522 |
if( pAr->bDryRun ){ |
|
|
16523 |
utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); |
|
|
16524 |
}else{ |
|
|
16525 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
|
|
16526 |
if( pAr->bVerbose ){ |
|
|
16527 |
utf8_printf(pAr->p->out, "%s % 10d %s %s\n", |
|
|
16528 |
sqlite3_column_text(pSql, 0), |
|
|
16529 |
sqlite3_column_int(pSql, 1), |
|
|
16530 |
sqlite3_column_text(pSql, 2), |
|
|
16531 |
sqlite3_column_text(pSql, 3) |
|
|
16532 |
); |
|
|
16533 |
}else{ |
|
|
16534 |
utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); |
|
|
16535 |
} |
|
|
16536 |
} |
|
|
16537 |
} |
|
|
16538 |
shellFinalize(&rc, pSql); |
|
|
16539 |
sqlite3_free(zWhere); |
|
|
16540 |
return rc; |
|
|
16541 |
} |
|
|
16542 |
|
|
|
16543 |
|
|
|
16544 |
/* |
|
|
16545 |
** Implementation of .ar "eXtract" command. |
|
|
16546 |
*/ |
|
|
16547 |
static int arExtractCommand(ArCommand *pAr){ |
|
|
16548 |
const char *zSql1 = |
|
|
16549 |
"SELECT " |
|
|
16550 |
" ($dir || name)," |
|
|
16551 |
" writefile(($dir || name), %s, mode, mtime) " |
|
|
16552 |
"FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)" |
|
|
16553 |
" AND name NOT GLOB '*..[/\\]*'"; |
|
|
16554 |
|
|
|
16555 |
const char *azExtraArg[] = { |
|
|
16556 |
"sqlar_uncompress(data, sz)", |
|
|
16557 |
"data" |
|
|
16558 |
}; |
|
|
16559 |
|
|
|
16560 |
sqlite3_stmt *pSql = 0; |
|
|
16561 |
int rc = SQLITE_OK; |
|
|
16562 |
char *zDir = 0; |
|
|
16563 |
char *zWhere = 0; |
|
|
16564 |
int i, j; |
|
|
16565 |
|
|
|
16566 |
/* If arguments are specified, check that they actually exist within |
|
|
16567 |
** the archive before proceeding. And formulate a WHERE clause to |
|
|
16568 |
** match them. */ |
|
|
16569 |
rc = arCheckEntries(pAr); |
|
|
16570 |
arWhereClause(&rc, pAr, &zWhere); |
|
|
16571 |
|
|
|
16572 |
if( rc==SQLITE_OK ){ |
|
|
16573 |
if( pAr->zDir ){ |
|
|
16574 |
zDir = sqlite3_mprintf("%s/", pAr->zDir); |
|
|
16575 |
}else{ |
|
|
16576 |
zDir = sqlite3_mprintf(""); |
|
|
16577 |
} |
|
|
16578 |
if( zDir==0 ) rc = SQLITE_NOMEM; |
|
|
16579 |
} |
|
|
16580 |
|
|
|
16581 |
shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, |
|
|
16582 |
azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere |
|
|
16583 |
); |
|
|
16584 |
|
|
|
16585 |
if( rc==SQLITE_OK ){ |
|
|
16586 |
j = sqlite3_bind_parameter_index(pSql, "$dir"); |
|
|
16587 |
sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); |
|
|
16588 |
|
|
|
16589 |
/* Run the SELECT statement twice. The first time, writefile() is called |
|
|
16590 |
** for all archive members that should be extracted. The second time, |
|
|
16591 |
** only for the directories. This is because the timestamps for |
|
|
16592 |
** extracted directories must be reset after they are populated (as |
|
|
16593 |
** populating them changes the timestamp). */ |
|
|
16594 |
for(i=0; i<2; i++){ |
|
|
16595 |
j = sqlite3_bind_parameter_index(pSql, "$dirOnly"); |
|
|
16596 |
sqlite3_bind_int(pSql, j, i); |
|
|
16597 |
if( pAr->bDryRun ){ |
|
|
16598 |
utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); |
|
|
16599 |
}else{ |
|
|
16600 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ |
|
|
16601 |
if( i==0 && pAr->bVerbose ){ |
|
|
16602 |
utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); |
|
|
16603 |
} |
|
|
16604 |
} |
|
|
16605 |
} |
|
|
16606 |
shellReset(&rc, pSql); |
|
|
16607 |
} |
|
|
16608 |
shellFinalize(&rc, pSql); |
|
|
16609 |
} |
|
|
16610 |
|
|
|
16611 |
sqlite3_free(zDir); |
|
|
16612 |
sqlite3_free(zWhere); |
|
|
16613 |
return rc; |
|
|
16614 |
} |
|
|
16615 |
|
|
|
16616 |
/* |
|
|
16617 |
** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out. |
|
|
16618 |
*/ |
|
|
16619 |
static int arExecSql(ArCommand *pAr, const char *zSql){ |
|
|
16620 |
int rc; |
|
|
16621 |
if( pAr->bDryRun ){ |
|
|
16622 |
utf8_printf(pAr->p->out, "%s\n", zSql); |
|
|
16623 |
rc = SQLITE_OK; |
|
|
16624 |
}else{ |
|
|
16625 |
char *zErr = 0; |
|
|
16626 |
rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); |
|
|
16627 |
if( zErr ){ |
|
|
16628 |
utf8_printf(stdout, "ERROR: %s\n", zErr); |
|
|
16629 |
sqlite3_free(zErr); |
|
|
16630 |
} |
|
|
16631 |
} |
|
|
16632 |
return rc; |
|
|
16633 |
} |
|
|
16634 |
|
|
|
16635 |
|
|
|
16636 |
/* |
|
|
16637 |
** Implementation of .ar "create", "insert", and "update" commands. |
|
|
16638 |
** |
|
|
16639 |
** create -> Create a new SQL archive |
|
|
16640 |
** insert -> Insert or reinsert all files listed |
|
|
16641 |
** update -> Insert files that have changed or that were not |
|
|
16642 |
** previously in the archive |
|
|
16643 |
** |
|
|
16644 |
** Create the "sqlar" table in the database if it does not already exist. |
|
|
16645 |
** Then add each file in the azFile[] array to the archive. Directories |
|
|
16646 |
** are added recursively. If argument bVerbose is non-zero, a message is |
|
|
16647 |
** printed on stdout for each file archived. |
|
|
16648 |
** |
|
|
16649 |
** The create command is the same as update, except that it drops |
|
|
16650 |
** any existing "sqlar" table before beginning. The "insert" command |
|
|
16651 |
** always overwrites every file named on the command-line, where as |
|
|
16652 |
** "update" only overwrites if the size or mtime or mode has changed. |
|
|
16653 |
*/ |
|
|
16654 |
static int arCreateOrUpdateCommand( |
|
|
16655 |
ArCommand *pAr, /* Command arguments and options */ |
|
|
16656 |
int bUpdate, /* true for a --create. */ |
|
|
16657 |
int bOnlyIfChanged /* Only update if file has changed */ |
|
|
16658 |
){ |
|
|
16659 |
const char *zCreate = |
|
|
16660 |
"CREATE TABLE IF NOT EXISTS sqlar(\n" |
|
|
16661 |
" name TEXT PRIMARY KEY, -- name of the file\n" |
|
|
16662 |
" mode INT, -- access permissions\n" |
|
|
16663 |
" mtime INT, -- last modification time\n" |
|
|
16664 |
" sz INT, -- original file size\n" |
|
|
16665 |
" data BLOB -- compressed content\n" |
|
|
16666 |
")"; |
|
|
16667 |
const char *zDrop = "DROP TABLE IF EXISTS sqlar"; |
|
|
16668 |
const char *zInsertFmt[2] = { |
|
|
16669 |
"REPLACE INTO %s(name,mode,mtime,sz,data)\n" |
|
|
16670 |
" SELECT\n" |
|
|
16671 |
" %s,\n" |
|
|
16672 |
" mode,\n" |
|
|
16673 |
" mtime,\n" |
|
|
16674 |
" CASE substr(lsmode(mode),1,1)\n" |
|
|
16675 |
" WHEN '-' THEN length(data)\n" |
|
|
16676 |
" WHEN 'd' THEN 0\n" |
|
|
16677 |
" ELSE -1 END,\n" |
|
|
16678 |
" sqlar_compress(data)\n" |
|
|
16679 |
" FROM fsdir(%Q,%Q) AS disk\n" |
|
|
16680 |
" WHERE lsmode(mode) NOT LIKE '?%%'%s;" |
|
|
16681 |
, |
|
|
16682 |
"REPLACE INTO %s(name,mode,mtime,data)\n" |
|
|
16683 |
" SELECT\n" |
|
|
16684 |
" %s,\n" |
|
|
16685 |
" mode,\n" |
|
|
16686 |
" mtime,\n" |
|
|
16687 |
" data\n" |
|
|
16688 |
" FROM fsdir(%Q,%Q) AS disk\n" |
|
|
16689 |
" WHERE lsmode(mode) NOT LIKE '?%%'%s;" |
|
|
16690 |
}; |
|
|
16691 |
int i; /* For iterating through azFile[] */ |
|
|
16692 |
int rc; /* Return code */ |
|
|
16693 |
const char *zTab = 0; /* SQL table into which to insert */ |
|
|
16694 |
char *zSql; |
|
|
16695 |
char zTemp[50]; |
|
|
16696 |
char *zExists = 0; |
|
|
16697 |
|
|
|
16698 |
arExecSql(pAr, "PRAGMA page_size=512"); |
|
|
16699 |
rc = arExecSql(pAr, "SAVEPOINT ar;"); |
|
|
16700 |
if( rc!=SQLITE_OK ) return rc; |
|
|
16701 |
zTemp[0] = 0; |
|
|
16702 |
if( pAr->bZip ){ |
|
|
16703 |
/* Initialize the zipfile virtual table, if necessary */ |
|
|
16704 |
if( pAr->zFile ){ |
|
|
16705 |
sqlite3_uint64 r; |
|
|
16706 |
sqlite3_randomness(sizeof(r),&r); |
|
|
16707 |
sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); |
|
|
16708 |
zTab = zTemp; |
|
|
16709 |
zSql = sqlite3_mprintf( |
|
|
16710 |
"CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", |
|
|
16711 |
zTab, pAr->zFile |
|
|
16712 |
); |
|
|
16713 |
rc = arExecSql(pAr, zSql); |
|
|
16714 |
sqlite3_free(zSql); |
|
|
16715 |
}else{ |
|
|
16716 |
zTab = "zip"; |
|
|
16717 |
} |
|
|
16718 |
}else{ |
|
|
16719 |
/* Initialize the table for an SQLAR */ |
|
|
16720 |
zTab = "sqlar"; |
|
|
16721 |
if( bUpdate==0 ){ |
|
|
16722 |
rc = arExecSql(pAr, zDrop); |
|
|
16723 |
if( rc!=SQLITE_OK ) goto end_ar_transaction; |
|
|
16724 |
} |
|
|
16725 |
rc = arExecSql(pAr, zCreate); |
|
|
16726 |
} |
|
|
16727 |
if( bOnlyIfChanged ){ |
|
|
16728 |
zExists = sqlite3_mprintf( |
|
|
16729 |
" AND NOT EXISTS(" |
|
|
16730 |
"SELECT 1 FROM %s AS mem" |
|
|
16731 |
" WHERE mem.name=disk.name" |
|
|
16732 |
" AND mem.mtime=disk.mtime" |
|
|
16733 |
" AND mem.mode=disk.mode)", zTab); |
|
|
16734 |
}else{ |
|
|
16735 |
zExists = sqlite3_mprintf(""); |
|
|
16736 |
} |
|
|
16737 |
if( zExists==0 ) rc = SQLITE_NOMEM; |
|
|
16738 |
for(i=0; inArg && rc==SQLITE_OK; i++){ |
|
|
16739 |
char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, |
|
|
16740 |
pAr->bVerbose ? "shell_putsnl(name)" : "name", |
|
|
16741 |
pAr->azArg[i], pAr->zDir, zExists); |
|
|
16742 |
rc = arExecSql(pAr, zSql2); |
|
|
16743 |
sqlite3_free(zSql2); |
|
|
16744 |
} |
|
|
16745 |
end_ar_transaction: |
|
|
16746 |
if( rc!=SQLITE_OK ){ |
|
|
16747 |
sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); |
|
|
16748 |
}else{ |
|
|
16749 |
rc = arExecSql(pAr, "RELEASE ar;"); |
|
|
16750 |
if( pAr->bZip && pAr->zFile ){ |
|
|
16751 |
zSql = sqlite3_mprintf("DROP TABLE %s", zTemp); |
|
|
16752 |
arExecSql(pAr, zSql); |
|
|
16753 |
sqlite3_free(zSql); |
|
|
16754 |
} |
|
|
16755 |
} |
|
|
16756 |
sqlite3_free(zExists); |
|
|
16757 |
return rc; |
|
|
16758 |
} |
|
|
16759 |
|
|
|
16760 |
/* |
|
|
16761 |
** Implementation of ".ar" dot command. |
|
|
16762 |
*/ |
|
|
16763 |
static int arDotCommand( |
|
|
16764 |
ShellState *pState, /* Current shell tool state */ |
|
|
16765 |
int fromCmdLine, /* True if -A command-line option, not .ar cmd */ |
|
|
16766 |
char **azArg, /* Array of arguments passed to dot command */ |
|
|
16767 |
int nArg /* Number of entries in azArg[] */ |
|
|
16768 |
){ |
|
|
16769 |
ArCommand cmd; |
|
|
16770 |
int rc; |
|
|
16771 |
memset(&cmd, 0, sizeof(cmd)); |
|
|
16772 |
cmd.fromCmdLine = fromCmdLine; |
|
|
16773 |
rc = arParseCommand(azArg, nArg, &cmd); |
|
|
16774 |
if( rc==SQLITE_OK ){ |
|
|
16775 |
int eDbType = SHELL_OPEN_UNSPEC; |
|
|
16776 |
cmd.p = pState; |
|
|
16777 |
cmd.db = pState->db; |
|
|
16778 |
if( cmd.zFile ){ |
|
|
16779 |
eDbType = deduceDatabaseType(cmd.zFile, 1); |
|
|
16780 |
}else{ |
|
|
16781 |
eDbType = pState->openMode; |
|
|
16782 |
} |
|
|
16783 |
if( eDbType==SHELL_OPEN_ZIPFILE ){ |
|
|
16784 |
if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){ |
|
|
16785 |
if( cmd.zFile==0 ){ |
|
|
16786 |
cmd.zSrcTable = sqlite3_mprintf("zip"); |
|
|
16787 |
}else{ |
|
|
16788 |
cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile); |
|
|
16789 |
} |
|
|
16790 |
} |
|
|
16791 |
cmd.bZip = 1; |
|
|
16792 |
}else if( cmd.zFile ){ |
|
|
16793 |
int flags; |
|
|
16794 |
if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS; |
|
|
16795 |
if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT |
|
|
16796 |
|| cmd.eCmd==AR_CMD_UPDATE ){ |
|
|
16797 |
flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; |
|
|
16798 |
}else{ |
|
|
16799 |
flags = SQLITE_OPEN_READONLY; |
|
|
16800 |
} |
|
|
16801 |
cmd.db = 0; |
|
|
16802 |
if( cmd.bDryRun ){ |
|
|
16803 |
utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile, |
|
|
16804 |
eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : ""); |
|
|
16805 |
} |
|
|
16806 |
rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, |
|
|
16807 |
eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0); |
|
|
16808 |
if( rc!=SQLITE_OK ){ |
|
|
16809 |
utf8_printf(stderr, "cannot open file: %s (%s)\n", |
|
|
16810 |
cmd.zFile, sqlite3_errmsg(cmd.db) |
|
|
16811 |
); |
|
|
16812 |
goto end_ar_command; |
|
|
16813 |
} |
|
|
16814 |
sqlite3_fileio_init(cmd.db, 0, 0); |
|
|
16815 |
sqlite3_sqlar_init(cmd.db, 0, 0); |
|
|
16816 |
sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, |
|
|
16817 |
shellPutsFunc, 0, 0); |
|
|
16818 |
|
|
|
16819 |
} |
|
|
16820 |
if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){ |
|
|
16821 |
if( cmd.eCmd!=AR_CMD_CREATE |
|
|
16822 |
&& sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) |
|
|
16823 |
){ |
|
|
16824 |
utf8_printf(stderr, "database does not contain an 'sqlar' table\n"); |
|
|
16825 |
rc = SQLITE_ERROR; |
|
|
16826 |
goto end_ar_command; |
|
|
16827 |
} |
|
|
16828 |
cmd.zSrcTable = sqlite3_mprintf("sqlar"); |
|
|
16829 |
} |
|
|
16830 |
|
|
|
16831 |
switch( cmd.eCmd ){ |
|
|
16832 |
case AR_CMD_CREATE: |
|
|
16833 |
rc = arCreateOrUpdateCommand(&cmd, 0, 0); |
|
|
16834 |
break; |
|
|
16835 |
|
|
|
16836 |
case AR_CMD_EXTRACT: |
|
|
16837 |
rc = arExtractCommand(&cmd); |
|
|
16838 |
break; |
|
|
16839 |
|
|
|
16840 |
case AR_CMD_LIST: |
|
|
16841 |
rc = arListCommand(&cmd); |
|
|
16842 |
break; |
|
|
16843 |
|
|
|
16844 |
case AR_CMD_HELP: |
|
|
16845 |
arUsage(pState->out); |
|
|
16846 |
break; |
|
|
16847 |
|
|
|
16848 |
case AR_CMD_INSERT: |
|
|
16849 |
rc = arCreateOrUpdateCommand(&cmd, 1, 0); |
|
|
16850 |
break; |
|
|
16851 |
|
|
|
16852 |
default: |
|
|
16853 |
assert( cmd.eCmd==AR_CMD_UPDATE ); |
|
|
16854 |
rc = arCreateOrUpdateCommand(&cmd, 1, 1); |
|
|
16855 |
break; |
|
|
16856 |
} |
|
|
16857 |
} |
|
|
16858 |
end_ar_command: |
|
|
16859 |
if( cmd.db!=pState->db ){ |
|
|
16860 |
close_db(cmd.db); |
|
|
16861 |
} |
|
|
16862 |
sqlite3_free(cmd.zSrcTable); |
|
|
16863 |
|
|
|
16864 |
return rc; |
|
|
16865 |
} |
|
|
16866 |
/* End of the ".archive" or ".ar" command logic |
|
|
16867 |
*******************************************************************************/ |
|
|
16868 |
#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ |
|
|
16869 |
|
|
|
16870 |
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
|
|
16871 |
/* |
|
|
16872 |
** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op. |
|
|
16873 |
** Otherwise, the SQL statement or statements in zSql are executed using |
|
|
16874 |
** database connection db and the error code written to *pRc before |
|
|
16875 |
** this function returns. |
|
|
16876 |
*/ |
|
|
16877 |
static void shellExec(sqlite3 *db, int *pRc, const char *zSql){ |
|
|
16878 |
int rc = *pRc; |
|
|
16879 |
if( rc==SQLITE_OK ){ |
|
|
16880 |
char *zErr = 0; |
|
|
16881 |
rc = sqlite3_exec(db, zSql, 0, 0, &zErr); |
|
|
16882 |
if( rc!=SQLITE_OK ){ |
|
|
16883 |
raw_printf(stderr, "SQL error: %s\n", zErr); |
|
|
16884 |
} |
|
|
16885 |
*pRc = rc; |
|
|
16886 |
} |
|
|
16887 |
} |
|
|
16888 |
|
|
|
16889 |
/* |
|
|
16890 |
** Like shellExec(), except that zFmt is a printf() style format string. |
|
|
16891 |
*/ |
|
|
16892 |
static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){ |
|
|
16893 |
char *z = 0; |
|
|
16894 |
if( *pRc==SQLITE_OK ){ |
|
|
16895 |
va_list ap; |
|
|
16896 |
va_start(ap, zFmt); |
|
|
16897 |
z = sqlite3_vmprintf(zFmt, ap); |
|
|
16898 |
va_end(ap); |
|
|
16899 |
if( z==0 ){ |
|
|
16900 |
*pRc = SQLITE_NOMEM; |
|
|
16901 |
}else{ |
|
|
16902 |
shellExec(db, pRc, z); |
|
|
16903 |
} |
|
|
16904 |
sqlite3_free(z); |
|
|
16905 |
} |
|
|
16906 |
} |
|
|
16907 |
|
|
|
16908 |
/* |
|
|
16909 |
** If *pRc is not SQLITE_OK when this function is called, it is a no-op. |
|
|
16910 |
** Otherwise, an attempt is made to allocate, zero and return a pointer |
|
|
16911 |
** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set |
|
|
16912 |
** to SQLITE_NOMEM and NULL returned. |
|
|
16913 |
*/ |
|
|
16914 |
static void *shellMalloc(int *pRc, sqlite3_int64 nByte){ |
|
|
16915 |
void *pRet = 0; |
|
|
16916 |
if( *pRc==SQLITE_OK ){ |
|
|
16917 |
pRet = sqlite3_malloc64(nByte); |
|
|
16918 |
if( pRet==0 ){ |
|
|
16919 |
*pRc = SQLITE_NOMEM; |
|
|
16920 |
}else{ |
|
|
16921 |
memset(pRet, 0, nByte); |
|
|
16922 |
} |
|
|
16923 |
} |
|
|
16924 |
return pRet; |
|
|
16925 |
} |
|
|
16926 |
|
|
|
16927 |
/* |
|
|
16928 |
** If *pRc is not SQLITE_OK when this function is called, it is a no-op. |
|
|
16929 |
** Otherwise, zFmt is treated as a printf() style string. The result of |
|
|
16930 |
** formatting it along with any trailing arguments is written into a |
|
|
16931 |
** buffer obtained from sqlite3_malloc(), and pointer to which is returned. |
|
|
16932 |
** It is the responsibility of the caller to eventually free this buffer |
|
|
16933 |
** using a call to sqlite3_free(). |
|
|
16934 |
** |
|
|
16935 |
** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL |
|
|
16936 |
** pointer returned. |
|
|
16937 |
*/ |
|
|
16938 |
static char *shellMPrintf(int *pRc, const char *zFmt, ...){ |
|
|
16939 |
char *z = 0; |
|
|
16940 |
if( *pRc==SQLITE_OK ){ |
|
|
16941 |
va_list ap; |
|
|
16942 |
va_start(ap, zFmt); |
|
|
16943 |
z = sqlite3_vmprintf(zFmt, ap); |
|
|
16944 |
va_end(ap); |
|
|
16945 |
if( z==0 ){ |
|
|
16946 |
*pRc = SQLITE_NOMEM; |
|
|
16947 |
} |
|
|
16948 |
} |
|
|
16949 |
return z; |
|
|
16950 |
} |
|
|
16951 |
|
|
|
16952 |
/* |
|
|
16953 |
** When running the ".recover" command, each output table, and the special |
|
|
16954 |
** orphaned row table if it is required, is represented by an instance |
|
|
16955 |
** of the following struct. |
|
|
16956 |
*/ |
|
|
16957 |
typedef struct RecoverTable RecoverTable; |
|
|
16958 |
struct RecoverTable { |
|
|
16959 |
char *zQuoted; /* Quoted version of table name */ |
|
|
16960 |
int nCol; /* Number of columns in table */ |
|
|
16961 |
char **azlCol; /* Array of column lists */ |
|
|
16962 |
int iPk; /* Index of IPK column */ |
|
|
16963 |
}; |
|
|
16964 |
|
|
|
16965 |
/* |
|
|
16966 |
** Free a RecoverTable object allocated by recoverFindTable() or |
|
|
16967 |
** recoverOrphanTable(). |
|
|
16968 |
*/ |
|
|
16969 |
static void recoverFreeTable(RecoverTable *pTab){ |
|
|
16970 |
if( pTab ){ |
|
|
16971 |
sqlite3_free(pTab->zQuoted); |
|
|
16972 |
if( pTab->azlCol ){ |
|
|
16973 |
int i; |
|
|
16974 |
for(i=0; i<=pTab->nCol; i++){ |
|
|
16975 |
sqlite3_free(pTab->azlCol[i]); |
|
|
16976 |
} |
|
|
16977 |
sqlite3_free(pTab->azlCol); |
|
|
16978 |
} |
|
|
16979 |
sqlite3_free(pTab); |
|
|
16980 |
} |
|
|
16981 |
} |
|
|
16982 |
|
|
|
16983 |
/* |
|
|
16984 |
** This function is a no-op if (*pRc) is not SQLITE_OK when it is called. |
|
|
16985 |
** Otherwise, it allocates and returns a RecoverTable object based on the |
|
|
16986 |
** final four arguments passed to this function. It is the responsibility |
|
|
16987 |
** of the caller to eventually free the returned object using |
|
|
16988 |
** recoverFreeTable(). |
|
|
16989 |
*/ |
|
|
16990 |
static RecoverTable *recoverNewTable( |
|
|
16991 |
int *pRc, /* IN/OUT: Error code */ |
|
|
16992 |
const char *zName, /* Name of table */ |
|
|
16993 |
const char *zSql, /* CREATE TABLE statement */ |
|
|
16994 |
int bIntkey, |
|
|
16995 |
int nCol |
|
|
16996 |
){ |
|
|
16997 |
sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */ |
|
|
16998 |
int rc = *pRc; |
|
|
16999 |
RecoverTable *pTab = 0; |
|
|
17000 |
|
|
|
17001 |
pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable)); |
|
|
17002 |
if( rc==SQLITE_OK ){ |
|
|
17003 |
int nSqlCol = 0; |
|
|
17004 |
int bSqlIntkey = 0; |
|
|
17005 |
sqlite3_stmt *pStmt = 0; |
|
|
17006 |
|
|
|
17007 |
rc = sqlite3_open("", &dbtmp); |
|
|
17008 |
if( rc==SQLITE_OK ){ |
|
|
17009 |
sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0, |
|
|
17010 |
shellIdQuote, 0, 0); |
|
|
17011 |
} |
|
|
17012 |
if( rc==SQLITE_OK ){ |
|
|
17013 |
rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0); |
|
|
17014 |
} |
|
|
17015 |
if( rc==SQLITE_OK ){ |
|
|
17016 |
rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0); |
|
|
17017 |
if( rc==SQLITE_ERROR ){ |
|
|
17018 |
rc = SQLITE_OK; |
|
|
17019 |
goto finished; |
|
|
17020 |
} |
|
|
17021 |
} |
|
|
17022 |
shellPreparePrintf(dbtmp, &rc, &pStmt, |
|
|
17023 |
"SELECT count(*) FROM pragma_table_info(%Q)", zName |
|
|
17024 |
); |
|
|
17025 |
if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
|
|
17026 |
nSqlCol = sqlite3_column_int(pStmt, 0); |
|
|
17027 |
} |
|
|
17028 |
shellFinalize(&rc, pStmt); |
|
|
17029 |
|
|
|
17030 |
if( rc!=SQLITE_OK || nSqlCol |
|
|
17031 |
goto finished; |
|
|
17032 |
} |
|
|
17033 |
|
|
|
17034 |
shellPreparePrintf(dbtmp, &rc, &pStmt, |
|
|
17035 |
"SELECT (" |
|
|
17036 |
" SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage" |
|
|
17037 |
") FROM sqlite_schema WHERE name = %Q", zName |
|
|
17038 |
); |
|
|
17039 |
if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
|
|
17040 |
bSqlIntkey = sqlite3_column_int(pStmt, 0); |
|
|
17041 |
} |
|
|
17042 |
shellFinalize(&rc, pStmt); |
|
|
17043 |
|
|
|
17044 |
if( bIntkey==bSqlIntkey ){ |
|
|
17045 |
int i; |
|
|
17046 |
const char *zPk = "_rowid_"; |
|
|
17047 |
sqlite3_stmt *pPkFinder = 0; |
|
|
17048 |
|
|
|
17049 |
/* If this is an intkey table and there is an INTEGER PRIMARY KEY, |
|
|
17050 |
** set zPk to the name of the PK column, and pTab->iPk to the index |
|
|
17051 |
** of the column, where columns are 0-numbered from left to right. |
|
|
17052 |
** Or, if this is a WITHOUT ROWID table or if there is no IPK column, |
|
|
17053 |
** leave zPk as "_rowid_" and pTab->iPk at -2. */ |
|
|
17054 |
pTab->iPk = -2; |
|
|
17055 |
if( bIntkey ){ |
|
|
17056 |
shellPreparePrintf(dbtmp, &rc, &pPkFinder, |
|
|
17057 |
"SELECT cid, name FROM pragma_table_info(%Q) " |
|
|
17058 |
" WHERE pk=1 AND type='integer' COLLATE nocase" |
|
|
17059 |
" AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)" |
|
|
17060 |
, zName, zName |
|
|
17061 |
); |
|
|
17062 |
if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){ |
|
|
17063 |
pTab->iPk = sqlite3_column_int(pPkFinder, 0); |
|
|
17064 |
zPk = (const char*)sqlite3_column_text(pPkFinder, 1); |
|
|
17065 |
} |
|
|
17066 |
} |
|
|
17067 |
|
|
|
17068 |
pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName); |
|
|
17069 |
pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1)); |
|
|
17070 |
pTab->nCol = nSqlCol; |
|
|
17071 |
|
|
|
17072 |
if( bIntkey ){ |
|
|
17073 |
pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk); |
|
|
17074 |
}else{ |
|
|
17075 |
pTab->azlCol[0] = shellMPrintf(&rc, ""); |
|
|
17076 |
} |
|
|
17077 |
i = 1; |
|
|
17078 |
shellPreparePrintf(dbtmp, &rc, &pStmt, |
|
|
17079 |
"SELECT %Q || group_concat(shell_idquote(name), ', ') " |
|
|
17080 |
" FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) " |
|
|
17081 |
"FROM pragma_table_info(%Q)", |
|
|
17082 |
bIntkey ? ", " : "", pTab->iPk, |
|
|
17083 |
bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ", |
|
|
17084 |
zName |
|
|
17085 |
); |
|
|
17086 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
|
|
17087 |
const char *zText = (const char*)sqlite3_column_text(pStmt, 0); |
|
|
17088 |
pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText); |
|
|
17089 |
i++; |
|
|
17090 |
} |
|
|
17091 |
shellFinalize(&rc, pStmt); |
|
|
17092 |
|
|
|
17093 |
shellFinalize(&rc, pPkFinder); |
|
|
17094 |
} |
|
|
17095 |
} |
|
|
17096 |
|
|
|
17097 |
finished: |
|
|
17098 |
sqlite3_close(dbtmp); |
|
|
17099 |
*pRc = rc; |
|
|
17100 |
if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){ |
|
|
17101 |
recoverFreeTable(pTab); |
|
|
17102 |
pTab = 0; |
|
|
17103 |
} |
|
|
17104 |
return pTab; |
|
|
17105 |
} |
|
|
17106 |
|
|
|
17107 |
/* |
|
|
17108 |
** This function is called to search the schema recovered from the |
|
|
17109 |
** sqlite_schema table of the (possibly) corrupt database as part |
|
|
17110 |
** of a ".recover" command. Specifically, for a table with root page |
|
|
17111 |
** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the |
|
|
17112 |
** table must be a WITHOUT ROWID table, or if non-zero, not one of |
|
|
17113 |
** those. |
|
|
17114 |
** |
|
|
17115 |
** If a table is found, a (RecoverTable*) object is returned. Or, if |
|
|
17116 |
** no such table is found, but bIntkey is false and iRoot is the |
|
|
17117 |
** root page of an index in the recovered schema, then (*pbNoop) is |
|
|
17118 |
** set to true and NULL returned. Or, if there is no such table or |
|
|
17119 |
** index, NULL is returned and (*pbNoop) set to 0, indicating that |
|
|
17120 |
** the caller should write data to the orphans table. |
|
|
17121 |
*/ |
|
|
17122 |
static RecoverTable *recoverFindTable( |
|
|
17123 |
ShellState *pState, /* Shell state object */ |
|
|
17124 |
int *pRc, /* IN/OUT: Error code */ |
|
|
17125 |
int iRoot, /* Root page of table */ |
|
|
17126 |
int bIntkey, /* True for an intkey table */ |
|
|
17127 |
int nCol, /* Number of columns in table */ |
|
|
17128 |
int *pbNoop /* OUT: True if iRoot is root of index */ |
|
|
17129 |
){ |
|
|
17130 |
sqlite3_stmt *pStmt = 0; |
|
|
17131 |
RecoverTable *pRet = 0; |
|
|
17132 |
int bNoop = 0; |
|
|
17133 |
const char *zSql = 0; |
|
|
17134 |
const char *zName = 0; |
|
|
17135 |
|
|
|
17136 |
/* Search the recovered schema for an object with root page iRoot. */ |
|
|
17137 |
shellPreparePrintf(pState->db, pRc, &pStmt, |
|
|
17138 |
"SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot |
|
|
17139 |
); |
|
|
17140 |
while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
|
|
17141 |
const char *zType = (const char*)sqlite3_column_text(pStmt, 0); |
|
|
17142 |
if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){ |
|
|
17143 |
bNoop = 1; |
|
|
17144 |
break; |
|
|
17145 |
} |
|
|
17146 |
if( sqlite3_stricmp(zType, "table")==0 ){ |
|
|
17147 |
zName = (const char*)sqlite3_column_text(pStmt, 1); |
|
|
17148 |
zSql = (const char*)sqlite3_column_text(pStmt, 2); |
|
|
17149 |
pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol); |
|
|
17150 |
break; |
|
|
17151 |
} |
|
|
17152 |
} |
|
|
17153 |
|
|
|
17154 |
shellFinalize(pRc, pStmt); |
|
|
17155 |
*pbNoop = bNoop; |
|
|
17156 |
return pRet; |
|
|
17157 |
} |
|
|
17158 |
|
|
|
17159 |
/* |
|
|
17160 |
** Return a RecoverTable object representing the orphans table. |
|
|
17161 |
*/ |
|
|
17162 |
static RecoverTable *recoverOrphanTable( |
|
|
17163 |
ShellState *pState, /* Shell state object */ |
|
|
17164 |
int *pRc, /* IN/OUT: Error code */ |
|
|
17165 |
const char *zLostAndFound, /* Base name for orphans table */ |
|
|
17166 |
int nCol /* Number of user data columns */ |
|
|
17167 |
){ |
|
|
17168 |
RecoverTable *pTab = 0; |
|
|
17169 |
if( nCol>=0 && *pRc==SQLITE_OK ){ |
|
|
17170 |
int i; |
|
|
17171 |
|
|
|
17172 |
/* This block determines the name of the orphan table. The prefered |
|
|
17173 |
** name is zLostAndFound. But if that clashes with another name |
|
|
17174 |
** in the recovered schema, try zLostAndFound_0, zLostAndFound_1 |
|
|
17175 |
** and so on until a non-clashing name is found. */ |
|
|
17176 |
int iTab = 0; |
|
|
17177 |
char *zTab = shellMPrintf(pRc, "%s", zLostAndFound); |
|
|
17178 |
sqlite3_stmt *pTest = 0; |
|
|
17179 |
shellPrepare(pState->db, pRc, |
|
|
17180 |
"SELECT 1 FROM recovery.schema WHERE name=?", &pTest |
|
|
17181 |
); |
|
|
17182 |
if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT); |
|
|
17183 |
while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){ |
|
|
17184 |
shellReset(pRc, pTest); |
|
|
17185 |
sqlite3_free(zTab); |
|
|
17186 |
zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++); |
|
|
17187 |
sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT); |
|
|
17188 |
} |
|
|
17189 |
shellFinalize(pRc, pTest); |
|
|
17190 |
|
|
|
17191 |
pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable)); |
|
|
17192 |
if( pTab ){ |
|
|
17193 |
pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab); |
|
|
17194 |
pTab->nCol = nCol; |
|
|
17195 |
pTab->iPk = -2; |
|
|
17196 |
if( nCol>0 ){ |
|
|
17197 |
pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1)); |
|
|
17198 |
if( pTab->azlCol ){ |
|
|
17199 |
pTab->azlCol[nCol] = shellMPrintf(pRc, ""); |
|
|
17200 |
for(i=nCol-1; i>=0; i--){ |
|
|
17201 |
pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]); |
|
|
17202 |
} |
|
|
17203 |
} |
|
|
17204 |
} |
|
|
17205 |
|
|
|
17206 |
if( *pRc!=SQLITE_OK ){ |
|
|
17207 |
recoverFreeTable(pTab); |
|
|
17208 |
pTab = 0; |
|
|
17209 |
}else{ |
|
|
17210 |
raw_printf(pState->out, |
|
|
17211 |
"CREATE TABLE %s(rootpgno INTEGER, " |
|
|
17212 |
"pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted |
|
|
17213 |
); |
|
|
17214 |
for(i=0; i |
|
|
17215 |
raw_printf(pState->out, ", c%d", i); |
|
|
17216 |
} |
|
|
17217 |
raw_printf(pState->out, ");\n"); |
|
|
17218 |
} |
|
|
17219 |
} |
|
|
17220 |
sqlite3_free(zTab); |
|
|
17221 |
} |
|
|
17222 |
return pTab; |
|
|
17223 |
} |
|
|
17224 |
|
|
|
17225 |
/* |
|
|
17226 |
** This function is called to recover data from the database. A script |
|
|
17227 |
** to construct a new database containing all recovered data is output |
|
|
17228 |
** on stream pState->out. |
|
|
17229 |
*/ |
|
|
17230 |
static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){ |
|
|
17231 |
int rc = SQLITE_OK; |
|
|
17232 |
sqlite3_stmt *pLoop = 0; /* Loop through all root pages */ |
|
|
17233 |
sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */ |
|
|
17234 |
sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */ |
|
|
17235 |
const char *zRecoveryDb = ""; /* Name of "recovery" database */ |
|
|
17236 |
const char *zLostAndFound = "lost_and_found"; |
|
|
17237 |
int i; |
|
|
17238 |
int nOrphan = -1; |
|
|
17239 |
RecoverTable *pOrphan = 0; |
|
|
17240 |
|
|
|
17241 |
int bFreelist = 1; /* 0 if --freelist-corrupt is specified */ |
|
|
17242 |
int bRowids = 1; /* 0 if --no-rowids */ |
|
|
17243 |
for(i=1; i |
|
|
17244 |
char *z = azArg[i]; |
|
|
17245 |
int n; |
|
|
17246 |
if( z[0]=='-' && z[1]=='-' ) z++; |
|
|
17247 |
n = strlen30(z); |
|
|
17248 |
if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){ |
|
|
17249 |
bFreelist = 0; |
|
|
17250 |
}else |
|
|
17251 |
if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){ |
|
|
17252 |
i++; |
|
|
17253 |
zRecoveryDb = azArg[i]; |
|
|
17254 |
}else |
|
|
17255 |
if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){ |
|
|
17256 |
i++; |
|
|
17257 |
zLostAndFound = azArg[i]; |
|
|
17258 |
}else |
|
|
17259 |
if( n<=10 && memcmp("-no-rowids", z, n)==0 ){ |
|
|
17260 |
bRowids = 0; |
|
|
17261 |
} |
|
|
17262 |
else{ |
|
|
17263 |
utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); |
|
|
17264 |
showHelp(pState->out, azArg[0]); |
|
|
17265 |
return 1; |
|
|
17266 |
} |
|
|
17267 |
} |
|
|
17268 |
|
|
|
17269 |
shellExecPrintf(pState->db, &rc, |
|
|
17270 |
/* Attach an in-memory database named 'recovery'. Create an indexed |
|
|
17271 |
** cache of the sqlite_dbptr virtual table. */ |
|
|
17272 |
"PRAGMA writable_schema = on;" |
|
|
17273 |
"ATTACH %Q AS recovery;" |
|
|
17274 |
"DROP TABLE IF EXISTS recovery.dbptr;" |
|
|
17275 |
"DROP TABLE IF EXISTS recovery.freelist;" |
|
|
17276 |
"DROP TABLE IF EXISTS recovery.map;" |
|
|
17277 |
"DROP TABLE IF EXISTS recovery.schema;" |
|
|
17278 |
"CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb |
|
|
17279 |
); |
|
|
17280 |
|
|
|
17281 |
if( bFreelist ){ |
|
|
17282 |
shellExec(pState->db, &rc, |
|
|
17283 |
"WITH trunk(pgno) AS (" |
|
|
17284 |
" SELECT shell_int32(" |
|
|
17285 |
" (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x " |
|
|
17286 |
" WHERE x>0" |
|
|
17287 |
" UNION" |
|
|
17288 |
" SELECT shell_int32(" |
|
|
17289 |
" (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x " |
|
|
17290 |
" FROM trunk WHERE x>0" |
|
|
17291 |
")," |
|
|
17292 |
"freelist(data, n, freepgno) AS (" |
|
|
17293 |
" SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno " |
|
|
17294 |
" FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno" |
|
|
17295 |
" UNION ALL" |
|
|
17296 |
" SELECT data, n-1, shell_int32(data, 2+n) " |
|
|
17297 |
" FROM freelist WHERE n>=0" |
|
|
17298 |
")" |
|
|
17299 |
"REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;" |
|
|
17300 |
); |
|
|
17301 |
} |
|
|
17302 |
|
|
|
17303 |
/* If this is an auto-vacuum database, add all pointer-map pages to |
|
|
17304 |
** the freelist table. Do this regardless of whether or not |
|
|
17305 |
** --freelist-corrupt was specified. */ |
|
|
17306 |
shellExec(pState->db, &rc, |
|
|
17307 |
"WITH ptrmap(pgno) AS (" |
|
|
17308 |
" SELECT 2 WHERE shell_int32(" |
|
|
17309 |
" (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13" |
|
|
17310 |
" )" |
|
|
17311 |
" UNION ALL " |
|
|
17312 |
" SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp " |
|
|
17313 |
" FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)" |
|
|
17314 |
")" |
|
|
17315 |
"REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap" |
|
|
17316 |
); |
|
|
17317 |
|
|
|
17318 |
shellExec(pState->db, &rc, |
|
|
17319 |
"CREATE TABLE recovery.dbptr(" |
|
|
17320 |
" pgno, child, PRIMARY KEY(child, pgno)" |
|
|
17321 |
") WITHOUT ROWID;" |
|
|
17322 |
"INSERT OR IGNORE INTO recovery.dbptr(pgno, child) " |
|
|
17323 |
" SELECT * FROM sqlite_dbptr" |
|
|
17324 |
" WHERE pgno NOT IN freelist AND child NOT IN freelist;" |
|
|
17325 |
|
|
|
17326 |
/* Delete any pointer to page 1. This ensures that page 1 is considered |
|
|
17327 |
** a root page, regardless of how corrupt the db is. */ |
|
|
17328 |
"DELETE FROM recovery.dbptr WHERE child = 1;" |
|
|
17329 |
|
|
|
17330 |
/* Delete all pointers to any pages that have more than one pointer |
|
|
17331 |
** to them. Such pages will be treated as root pages when recovering |
|
|
17332 |
** data. */ |
|
|
17333 |
"DELETE FROM recovery.dbptr WHERE child IN (" |
|
|
17334 |
" SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1" |
|
|
17335 |
");" |
|
|
17336 |
|
|
|
17337 |
/* Create the "map" table that will (eventually) contain instructions |
|
|
17338 |
** for dealing with each page in the db that contains one or more |
|
|
17339 |
** records. */ |
|
|
17340 |
"CREATE TABLE recovery.map(" |
|
|
17341 |
"pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT" |
|
|
17342 |
");" |
|
|
17343 |
|
|
|
17344 |
/* Populate table [map]. If there are circular loops of pages in the |
|
|
17345 |
** database, the following adds all pages in such a loop to the map |
|
|
17346 |
** as individual root pages. This could be handled better. */ |
|
|
17347 |
"WITH pages(i, maxlen) AS (" |
|
|
17348 |
" SELECT page_count, (" |
|
|
17349 |
" SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count" |
|
|
17350 |
" ) FROM pragma_page_count WHERE page_count>0" |
|
|
17351 |
" UNION ALL" |
|
|
17352 |
" SELECT i-1, (" |
|
|
17353 |
" SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1" |
|
|
17354 |
" ) FROM pages WHERE i>=2" |
|
|
17355 |
")" |
|
|
17356 |
"INSERT INTO recovery.map(pgno, maxlen, intkey, root) " |
|
|
17357 |
" SELECT i, maxlen, NULL, (" |
|
|
17358 |
" WITH p(orig, pgno, parent) AS (" |
|
|
17359 |
" SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)" |
|
|
17360 |
" UNION " |
|
|
17361 |
" SELECT i, p.parent, " |
|
|
17362 |
" (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p" |
|
|
17363 |
" )" |
|
|
17364 |
" SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)" |
|
|
17365 |
") " |
|
|
17366 |
"FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;" |
|
|
17367 |
"UPDATE recovery.map AS o SET intkey = (" |
|
|
17368 |
" SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno" |
|
|
17369 |
");" |
|
|
17370 |
|
|
|
17371 |
/* Extract data from page 1 and any linked pages into table |
|
|
17372 |
** recovery.schema. With the same schema as an sqlite_schema table. */ |
|
|
17373 |
"CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);" |
|
|
17374 |
"INSERT INTO recovery.schema SELECT " |
|
|
17375 |
" max(CASE WHEN field=0 THEN value ELSE NULL END)," |
|
|
17376 |
" max(CASE WHEN field=1 THEN value ELSE NULL END)," |
|
|
17377 |
" max(CASE WHEN field=2 THEN value ELSE NULL END)," |
|
|
17378 |
" max(CASE WHEN field=3 THEN value ELSE NULL END)," |
|
|
17379 |
" max(CASE WHEN field=4 THEN value ELSE NULL END)" |
|
|
17380 |
"FROM sqlite_dbdata WHERE pgno IN (" |
|
|
17381 |
" SELECT pgno FROM recovery.map WHERE root=1" |
|
|
17382 |
")" |
|
|
17383 |
"GROUP BY pgno, cell;" |
|
|
17384 |
"CREATE INDEX recovery.schema_rootpage ON schema(rootpage);" |
|
|
17385 |
); |
|
|
17386 |
|
|
|
17387 |
/* Open a transaction, then print out all non-virtual, non-"sqlite_%" |
|
|
17388 |
** CREATE TABLE statements that extracted from the existing schema. */ |
|
|
17389 |
if( rc==SQLITE_OK ){ |
|
|
17390 |
sqlite3_stmt *pStmt = 0; |
|
|
17391 |
/* ".recover" might output content in an order which causes immediate |
|
|
17392 |
** foreign key constraints to be violated. So disable foreign-key |
|
|
17393 |
** constraint enforcement to prevent problems when running the output |
|
|
17394 |
** script. */ |
|
|
17395 |
raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n"); |
|
|
17396 |
raw_printf(pState->out, "BEGIN;\n"); |
|
|
17397 |
raw_printf(pState->out, "PRAGMA writable_schema = on;\n"); |
|
|
17398 |
shellPrepare(pState->db, &rc, |
|
|
17399 |
"SELECT sql FROM recovery.schema " |
|
|
17400 |
"WHERE type='table' AND sql LIKE 'create table%'", &pStmt |
|
|
17401 |
); |
|
|
17402 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
|
|
17403 |
const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0); |
|
|
17404 |
raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n", |
|
|
17405 |
&zCreateTable[12] |
|
|
17406 |
); |
|
|
17407 |
} |
|
|
17408 |
shellFinalize(&rc, pStmt); |
|
|
17409 |
} |
|
|
17410 |
|
|
|
17411 |
/* Figure out if an orphan table will be required. And if so, how many |
|
|
17412 |
** user columns it should contain */ |
|
|
17413 |
shellPrepare(pState->db, &rc, |
|
|
17414 |
"SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1" |
|
|
17415 |
, &pLoop |
|
|
17416 |
); |
|
|
17417 |
if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){ |
|
|
17418 |
nOrphan = sqlite3_column_int(pLoop, 0); |
|
|
17419 |
} |
|
|
17420 |
shellFinalize(&rc, pLoop); |
|
|
17421 |
pLoop = 0; |
|
|
17422 |
|
|
|
17423 |
shellPrepare(pState->db, &rc, |
|
|
17424 |
"SELECT pgno FROM recovery.map WHERE root=?", &pPages |
|
|
17425 |
); |
|
|
17426 |
|
|
|
17427 |
shellPrepare(pState->db, &rc, |
|
|
17428 |
"SELECT max(field), group_concat(shell_escape_crnl(quote" |
|
|
17429 |
"(case when (? AND field<0) then NULL else value end)" |
|
|
17430 |
"), ', ')" |
|
|
17431 |
", min(field) " |
|
|
17432 |
"FROM sqlite_dbdata WHERE pgno = ? AND field != ?" |
|
|
17433 |
"GROUP BY cell", &pCells |
|
|
17434 |
); |
|
|
17435 |
|
|
|
17436 |
/* Loop through each root page. */ |
|
|
17437 |
shellPrepare(pState->db, &rc, |
|
|
17438 |
"SELECT root, intkey, max(maxlen) FROM recovery.map" |
|
|
17439 |
" WHERE root>1 GROUP BY root, intkey ORDER BY root=(" |
|
|
17440 |
" SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'" |
|
|
17441 |
")", &pLoop |
|
|
17442 |
); |
|
|
17443 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){ |
|
|
17444 |
int iRoot = sqlite3_column_int(pLoop, 0); |
|
|
17445 |
int bIntkey = sqlite3_column_int(pLoop, 1); |
|
|
17446 |
int nCol = sqlite3_column_int(pLoop, 2); |
|
|
17447 |
int bNoop = 0; |
|
|
17448 |
RecoverTable *pTab; |
|
|
17449 |
|
|
|
17450 |
assert( bIntkey==0 || bIntkey==1 ); |
|
|
17451 |
pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop); |
|
|
17452 |
if( bNoop || rc ) continue; |
|
|
17453 |
if( pTab==0 ){ |
|
|
17454 |
if( pOrphan==0 ){ |
|
|
17455 |
pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan); |
|
|
17456 |
} |
|
|
17457 |
pTab = pOrphan; |
|
|
17458 |
if( pTab==0 ) break; |
|
|
17459 |
} |
|
|
17460 |
|
|
|
17461 |
if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){ |
|
|
17462 |
raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n"); |
|
|
17463 |
} |
|
|
17464 |
sqlite3_bind_int(pPages, 1, iRoot); |
|
|
17465 |
if( bRowids==0 && pTab->iPk<0 ){ |
|
|
17466 |
sqlite3_bind_int(pCells, 1, 1); |
|
|
17467 |
}else{ |
|
|
17468 |
sqlite3_bind_int(pCells, 1, 0); |
|
|
17469 |
} |
|
|
17470 |
sqlite3_bind_int(pCells, 3, pTab->iPk); |
|
|
17471 |
|
|
|
17472 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){ |
|
|
17473 |
int iPgno = sqlite3_column_int(pPages, 0); |
|
|
17474 |
sqlite3_bind_int(pCells, 2, iPgno); |
|
|
17475 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){ |
|
|
17476 |
int nField = sqlite3_column_int(pCells, 0); |
|
|
17477 |
int iMin = sqlite3_column_int(pCells, 2); |
|
|
17478 |
const char *zVal = (const char*)sqlite3_column_text(pCells, 1); |
|
|
17479 |
|
|
|
17480 |
RecoverTable *pTab2 = pTab; |
|
|
17481 |
if( pTab!=pOrphan && (iMin<0)!=bIntkey ){ |
|
|
17482 |
if( pOrphan==0 ){ |
|
|
17483 |
pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan); |
|
|
17484 |
} |
|
|
17485 |
pTab2 = pOrphan; |
|
|
17486 |
if( pTab2==0 ) break; |
|
|
17487 |
} |
|
|
17488 |
|
|
|
17489 |
nField = nField+1; |
|
|
17490 |
if( pTab2==pOrphan ){ |
|
|
17491 |
raw_printf(pState->out, |
|
|
17492 |
"INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n", |
|
|
17493 |
pTab2->zQuoted, iRoot, iPgno, nField, |
|
|
17494 |
iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField] |
|
|
17495 |
); |
|
|
17496 |
}else{ |
|
|
17497 |
raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", |
|
|
17498 |
pTab2->zQuoted, pTab2->azlCol[nField], zVal |
|
|
17499 |
); |
|
|
17500 |
} |
|
|
17501 |
} |
|
|
17502 |
shellReset(&rc, pCells); |
|
|
17503 |
} |
|
|
17504 |
shellReset(&rc, pPages); |
|
|
17505 |
if( pTab!=pOrphan ) recoverFreeTable(pTab); |
|
|
17506 |
} |
|
|
17507 |
shellFinalize(&rc, pLoop); |
|
|
17508 |
shellFinalize(&rc, pPages); |
|
|
17509 |
shellFinalize(&rc, pCells); |
|
|
17510 |
recoverFreeTable(pOrphan); |
|
|
17511 |
|
|
|
17512 |
/* The rest of the schema */ |
|
|
17513 |
if( rc==SQLITE_OK ){ |
|
|
17514 |
sqlite3_stmt *pStmt = 0; |
|
|
17515 |
shellPrepare(pState->db, &rc, |
|
|
17516 |
"SELECT sql, name FROM recovery.schema " |
|
|
17517 |
"WHERE sql NOT LIKE 'create table%'", &pStmt |
|
|
17518 |
); |
|
|
17519 |
while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ |
|
|
17520 |
const char *zSql = (const char*)sqlite3_column_text(pStmt, 0); |
|
|
17521 |
if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){ |
|
|
17522 |
const char *zName = (const char*)sqlite3_column_text(pStmt, 1); |
|
|
17523 |
char *zPrint = shellMPrintf(&rc, |
|
|
17524 |
"INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)", |
|
|
17525 |
zName, zName, zSql |
|
|
17526 |
); |
|
|
17527 |
raw_printf(pState->out, "%s;\n", zPrint); |
|
|
17528 |
sqlite3_free(zPrint); |
|
|
17529 |
}else{ |
|
|
17530 |
raw_printf(pState->out, "%s;\n", zSql); |
|
|
17531 |
} |
|
|
17532 |
} |
|
|
17533 |
shellFinalize(&rc, pStmt); |
|
|
17534 |
} |
|
|
17535 |
|
|
|
17536 |
if( rc==SQLITE_OK ){ |
|
|
17537 |
raw_printf(pState->out, "PRAGMA writable_schema = off;\n"); |
|
|
17538 |
raw_printf(pState->out, "COMMIT;\n"); |
|
|
17539 |
} |
|
|
17540 |
sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0); |
|
|
17541 |
return rc; |
|
|
17542 |
} |
|
|
17543 |
#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ |
|
|
17544 |
|
|
|
17545 |
|
|
|
17546 |
/* |
|
|
17547 |
** If an input line begins with "." then invoke this routine to |
|
|
17548 |
** process that line. |
|
|
17549 |
** |
|
|
17550 |
** Return 1 on error, 2 to exit, and 0 otherwise. |
|
|
17551 |
*/ |
|
|
17552 |
static int do_meta_command(char *zLine, ShellState *p){ |
|
|
17553 |
int h = 1; |
|
|
17554 |
int nArg = 0; |
|
|
17555 |
int n, c; |
|
|
17556 |
int rc = 0; |
|
|
17557 |
char *azArg[52]; |
|
|
17558 |
|
|
|
17559 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
17560 |
if( p->expert.pExpert ){ |
|
|
17561 |
expertFinish(p, 1, 0); |
|
|
17562 |
} |
|
|
17563 |
#endif |
|
|
17564 |
|
|
|
17565 |
/* Parse the input line into tokens. |
|
|
17566 |
*/ |
|
|
17567 |
while( zLine[h] && nArg |
|
|
17568 |
while( IsSpace(zLine[h]) ){ h++; } |
|
|
17569 |
if( zLine[h]==0 ) break; |
|
|
17570 |
if( zLine[h]=='\'' || zLine[h]=='"' ){ |
|
|
17571 |
int delim = zLine[h++]; |
|
|
17572 |
azArg[nArg++] = &zLine[h]; |
|
|
17573 |
while( zLine[h] && zLine[h]!=delim ){ |
|
|
17574 |
if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
|
|
17575 |
h++; |
|
|
17576 |
} |
|
|
17577 |
if( zLine[h]==delim ){ |
|
|
17578 |
zLine[h++] = 0; |
|
|
17579 |
} |
|
|
17580 |
if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
|
|
17581 |
}else{ |
|
|
17582 |
azArg[nArg++] = &zLine[h]; |
|
|
17583 |
while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
|
|
17584 |
if( zLine[h] ) zLine[h++] = 0; |
|
|
17585 |
resolve_backslashes(azArg[nArg-1]); |
|
|
17586 |
} |
|
|
17587 |
} |
|
|
17588 |
azArg[nArg] = 0; |
|
|
17589 |
|
|
|
17590 |
/* Process the input line. |
|
|
17591 |
*/ |
|
|
17592 |
if( nArg==0 ) return 0; /* no tokens, no error */ |
|
|
17593 |
n = strlen30(azArg[0]); |
|
|
17594 |
c = azArg[0][0]; |
|
|
17595 |
clearTempFile(p); |
|
|
17596 |
|
|
|
17597 |
#ifndef SQLITE_OMIT_AUTHORIZATION |
|
|
17598 |
if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
|
|
17599 |
if( nArg!=2 ){ |
|
|
17600 |
raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
|
|
17601 |
rc = 1; |
|
|
17602 |
goto meta_command_exit; |
|
|
17603 |
} |
|
|
17604 |
open_db(p, 0); |
|
|
17605 |
if( booleanValue(azArg[1]) ){ |
|
|
17606 |
sqlite3_set_authorizer(p->db, shellAuth, p); |
|
|
17607 |
}else{ |
|
|
17608 |
sqlite3_set_authorizer(p->db, 0, 0); |
|
|
17609 |
} |
|
|
17610 |
}else |
|
|
17611 |
#endif |
|
|
17612 |
|
|
|
17613 |
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
|
|
17614 |
if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){ |
|
|
17615 |
open_db(p, 0); |
|
|
17616 |
rc = arDotCommand(p, 0, azArg, nArg); |
|
|
17617 |
}else |
|
|
17618 |
#endif |
|
|
17619 |
|
|
|
17620 |
if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
|
|
17621 |
|| (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
|
|
17622 |
){ |
|
|
17623 |
const char *zDestFile = 0; |
|
|
17624 |
const char *zDb = 0; |
|
|
17625 |
sqlite3 *pDest; |
|
|
17626 |
sqlite3_backup *pBackup; |
|
|
17627 |
int j; |
|
|
17628 |
int bAsync = 0; |
|
|
17629 |
const char *zVfs = 0; |
|
|
17630 |
for(j=1; j |
|
|
17631 |
const char *z = azArg[j]; |
|
|
17632 |
if( z[0]=='-' ){ |
|
|
17633 |
if( z[1]=='-' ) z++; |
|
|
17634 |
if( strcmp(z, "-append")==0 ){ |
|
|
17635 |
zVfs = "apndvfs"; |
|
|
17636 |
}else |
|
|
17637 |
if( strcmp(z, "-async")==0 ){ |
|
|
17638 |
bAsync = 1; |
|
|
17639 |
}else |
|
|
17640 |
{ |
|
|
17641 |
utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
|
|
17642 |
return 1; |
|
|
17643 |
} |
|
|
17644 |
}else if( zDestFile==0 ){ |
|
|
17645 |
zDestFile = azArg[j]; |
|
|
17646 |
}else if( zDb==0 ){ |
|
|
17647 |
zDb = zDestFile; |
|
|
17648 |
zDestFile = azArg[j]; |
|
|
17649 |
}else{ |
|
|
17650 |
raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n"); |
|
|
17651 |
return 1; |
|
|
17652 |
} |
|
|
17653 |
} |
|
|
17654 |
if( zDestFile==0 ){ |
|
|
17655 |
raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
|
|
17656 |
return 1; |
|
|
17657 |
} |
|
|
17658 |
if( zDb==0 ) zDb = "main"; |
|
|
17659 |
rc = sqlite3_open_v2(zDestFile, &pDest, |
|
|
17660 |
SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs); |
|
|
17661 |
if( rc!=SQLITE_OK ){ |
|
|
17662 |
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
|
|
17663 |
close_db(pDest); |
|
|
17664 |
return 1; |
|
|
17665 |
} |
|
|
17666 |
if( bAsync ){ |
|
|
17667 |
sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;", |
|
|
17668 |
0, 0, 0); |
|
|
17669 |
} |
|
|
17670 |
open_db(p, 0); |
|
|
17671 |
pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
|
|
17672 |
if( pBackup==0 ){ |
|
|
17673 |
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
|
|
17674 |
close_db(pDest); |
|
|
17675 |
return 1; |
|
|
17676 |
} |
|
|
17677 |
while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
|
|
17678 |
sqlite3_backup_finish(pBackup); |
|
|
17679 |
if( rc==SQLITE_DONE ){ |
|
|
17680 |
rc = 0; |
|
|
17681 |
}else{ |
|
|
17682 |
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
|
|
17683 |
rc = 1; |
|
|
17684 |
} |
|
|
17685 |
close_db(pDest); |
|
|
17686 |
}else |
|
|
17687 |
|
|
|
17688 |
if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
|
|
17689 |
if( nArg==2 ){ |
|
|
17690 |
bail_on_error = booleanValue(azArg[1]); |
|
|
17691 |
}else{ |
|
|
17692 |
raw_printf(stderr, "Usage: .bail on|off\n"); |
|
|
17693 |
rc = 1; |
|
|
17694 |
} |
|
|
17695 |
}else |
|
|
17696 |
|
|
|
17697 |
if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
|
|
17698 |
if( nArg==2 ){ |
|
|
17699 |
if( booleanValue(azArg[1]) ){ |
|
|
17700 |
setBinaryMode(p->out, 1); |
|
|
17701 |
}else{ |
|
|
17702 |
setTextMode(p->out, 1); |
|
|
17703 |
} |
|
|
17704 |
}else{ |
|
|
17705 |
raw_printf(stderr, "Usage: .binary on|off\n"); |
|
|
17706 |
rc = 1; |
|
|
17707 |
} |
|
|
17708 |
}else |
|
|
17709 |
|
|
|
17710 |
if( c=='c' && strcmp(azArg[0],"cd")==0 ){ |
|
|
17711 |
if( nArg==2 ){ |
|
|
17712 |
#if defined(_WIN32) || defined(WIN32) |
|
|
17713 |
wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); |
|
|
17714 |
rc = !SetCurrentDirectoryW(z); |
|
|
17715 |
sqlite3_free(z); |
|
|
17716 |
#else |
|
|
17717 |
rc = chdir(azArg[1]); |
|
|
17718 |
#endif |
|
|
17719 |
if( rc ){ |
|
|
17720 |
utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]); |
|
|
17721 |
rc = 1; |
|
|
17722 |
} |
|
|
17723 |
}else{ |
|
|
17724 |
raw_printf(stderr, "Usage: .cd DIRECTORY\n"); |
|
|
17725 |
rc = 1; |
|
|
17726 |
} |
|
|
17727 |
}else |
|
|
17728 |
|
|
|
17729 |
/* The undocumented ".breakpoint" command causes a call to the no-op |
|
|
17730 |
** routine named test_breakpoint(). |
|
|
17731 |
*/ |
|
|
17732 |
if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){ |
|
|
17733 |
test_breakpoint(); |
|
|
17734 |
}else |
|
|
17735 |
|
|
|
17736 |
if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
|
|
17737 |
if( nArg==2 ){ |
|
|
17738 |
setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); |
|
|
17739 |
}else{ |
|
|
17740 |
raw_printf(stderr, "Usage: .changes on|off\n"); |
|
|
17741 |
rc = 1; |
|
|
17742 |
} |
|
|
17743 |
}else |
|
|
17744 |
|
|
|
17745 |
/* Cancel output redirection, if it is currently set (by .testcase) |
|
|
17746 |
** Then read the content of the testcase-out.txt file and compare against |
|
|
17747 |
** azArg[1]. If there are differences, report an error and exit. |
|
|
17748 |
*/ |
|
|
17749 |
if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){ |
|
|
17750 |
char *zRes = 0; |
|
|
17751 |
output_reset(p); |
|
|
17752 |
if( nArg!=2 ){ |
|
|
17753 |
raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); |
|
|
17754 |
rc = 2; |
|
|
17755 |
}else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ |
|
|
17756 |
raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); |
|
|
17757 |
rc = 2; |
|
|
17758 |
}else if( testcase_glob(azArg[1],zRes)==0 ){ |
|
|
17759 |
utf8_printf(stderr, |
|
|
17760 |
"testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", |
|
|
17761 |
p->zTestcase, azArg[1], zRes); |
|
|
17762 |
rc = 1; |
|
|
17763 |
}else{ |
|
|
17764 |
utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); |
|
|
17765 |
p->nCheck++; |
|
|
17766 |
} |
|
|
17767 |
sqlite3_free(zRes); |
|
|
17768 |
}else |
|
|
17769 |
|
|
|
17770 |
if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
|
|
17771 |
if( nArg==2 ){ |
|
|
17772 |
tryToClone(p, azArg[1]); |
|
|
17773 |
}else{ |
|
|
17774 |
raw_printf(stderr, "Usage: .clone FILENAME\n"); |
|
|
17775 |
rc = 1; |
|
|
17776 |
} |
|
|
17777 |
}else |
|
|
17778 |
|
|
|
17779 |
if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
|
|
17780 |
char **azName = 0; |
|
|
17781 |
int nName = 0; |
|
|
17782 |
sqlite3_stmt *pStmt; |
|
|
17783 |
int i; |
|
|
17784 |
open_db(p, 0); |
|
|
17785 |
rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
|
|
17786 |
if( rc ){ |
|
|
17787 |
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
|
|
17788 |
rc = 1; |
|
|
17789 |
}else{ |
|
|
17790 |
while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
17791 |
const char *zSchema = (const char *)sqlite3_column_text(pStmt,1); |
|
|
17792 |
const char *zFile = (const char*)sqlite3_column_text(pStmt,2); |
|
|
17793 |
azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*)); |
|
|
17794 |
if( azName==0 ){ shell_out_of_memory(); /* Does not return */ } |
|
|
17795 |
azName[nName*2] = strdup(zSchema); |
|
|
17796 |
azName[nName*2+1] = strdup(zFile); |
|
|
17797 |
nName++; |
|
|
17798 |
} |
|
|
17799 |
} |
|
|
17800 |
sqlite3_finalize(pStmt); |
|
|
17801 |
for(i=0; i |
|
|
17802 |
int eTxn = sqlite3_txn_state(p->db, azName[i*2]); |
|
|
17803 |
int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]); |
|
|
17804 |
const char *z = azName[i*2+1]; |
|
|
17805 |
utf8_printf(p->out, "%s: %s %s%s\n", |
|
|
17806 |
azName[i*2], |
|
|
17807 |
z && z[0] ? z : "\"\"", |
|
|
17808 |
bRdonly ? "r/o" : "r/w", |
|
|
17809 |
eTxn==SQLITE_TXN_NONE ? "" : |
|
|
17810 |
eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn"); |
|
|
17811 |
free(azName[i*2]); |
|
|
17812 |
free(azName[i*2+1]); |
|
|
17813 |
} |
|
|
17814 |
sqlite3_free(azName); |
|
|
17815 |
}else |
|
|
17816 |
|
|
|
17817 |
if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){ |
|
|
17818 |
static const struct DbConfigChoices { |
|
|
17819 |
const char *zName; |
|
|
17820 |
int op; |
|
|
17821 |
} aDbConfig[] = { |
|
|
17822 |
{ "defensive", SQLITE_DBCONFIG_DEFENSIVE }, |
|
|
17823 |
{ "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL }, |
|
|
17824 |
{ "dqs_dml", SQLITE_DBCONFIG_DQS_DML }, |
|
|
17825 |
{ "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, |
|
|
17826 |
{ "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, |
|
|
17827 |
{ "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, |
|
|
17828 |
{ "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW }, |
|
|
17829 |
{ "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, |
|
|
17830 |
{ "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, |
|
|
17831 |
{ "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, |
|
|
17832 |
{ "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, |
|
|
17833 |
{ "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, |
|
|
17834 |
{ "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, |
|
|
17835 |
{ "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, |
|
|
17836 |
{ "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, |
|
|
17837 |
{ "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, |
|
|
17838 |
}; |
|
|
17839 |
int ii, v; |
|
|
17840 |
open_db(p, 0); |
|
|
17841 |
for(ii=0; ii |
|
|
17842 |
if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue; |
|
|
17843 |
if( nArg>=3 ){ |
|
|
17844 |
sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); |
|
|
17845 |
} |
|
|
17846 |
sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); |
|
|
17847 |
utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off"); |
|
|
17848 |
if( nArg>1 ) break; |
|
|
17849 |
} |
|
|
17850 |
if( nArg>1 && ii==ArraySize(aDbConfig) ){ |
|
|
17851 |
utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]); |
|
|
17852 |
utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n"); |
|
|
17853 |
} |
|
|
17854 |
}else |
|
|
17855 |
|
|
|
17856 |
if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){ |
|
|
17857 |
rc = shell_dbinfo_command(p, nArg, azArg); |
|
|
17858 |
}else |
|
|
17859 |
|
|
|
17860 |
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) |
|
|
17861 |
if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){ |
|
|
17862 |
open_db(p, 0); |
|
|
17863 |
rc = recoverDatabaseCmd(p, nArg, azArg); |
|
|
17864 |
}else |
|
|
17865 |
#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */ |
|
|
17866 |
|
|
|
17867 |
if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
|
|
17868 |
char *zLike = 0; |
|
|
17869 |
char *zSql; |
|
|
17870 |
int i; |
|
|
17871 |
int savedShowHeader = p->showHeader; |
|
|
17872 |
int savedShellFlags = p->shellFlgs; |
|
|
17873 |
ShellClearFlag(p, |
|
|
17874 |
SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo |
|
|
17875 |
|SHFLG_DumpDataOnly|SHFLG_DumpNoSys); |
|
|
17876 |
for(i=1; i |
|
|
17877 |
if( azArg[i][0]=='-' ){ |
|
|
17878 |
const char *z = azArg[i]+1; |
|
|
17879 |
if( z[0]=='-' ) z++; |
|
|
17880 |
if( strcmp(z,"preserve-rowids")==0 ){ |
|
|
17881 |
#ifdef SQLITE_OMIT_VIRTUALTABLE |
|
|
17882 |
raw_printf(stderr, "The --preserve-rowids option is not compatible" |
|
|
17883 |
" with SQLITE_OMIT_VIRTUALTABLE\n"); |
|
|
17884 |
rc = 1; |
|
|
17885 |
sqlite3_free(zLike); |
|
|
17886 |
goto meta_command_exit; |
|
|
17887 |
#else |
|
|
17888 |
ShellSetFlag(p, SHFLG_PreserveRowid); |
|
|
17889 |
#endif |
|
|
17890 |
}else |
|
|
17891 |
if( strcmp(z,"newlines")==0 ){ |
|
|
17892 |
ShellSetFlag(p, SHFLG_Newlines); |
|
|
17893 |
}else |
|
|
17894 |
if( strcmp(z,"data-only")==0 ){ |
|
|
17895 |
ShellSetFlag(p, SHFLG_DumpDataOnly); |
|
|
17896 |
}else |
|
|
17897 |
if( strcmp(z,"nosys")==0 ){ |
|
|
17898 |
ShellSetFlag(p, SHFLG_DumpNoSys); |
|
|
17899 |
}else |
|
|
17900 |
{ |
|
|
17901 |
raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]); |
|
|
17902 |
rc = 1; |
|
|
17903 |
sqlite3_free(zLike); |
|
|
17904 |
goto meta_command_exit; |
|
|
17905 |
} |
|
|
17906 |
}else if( zLike ){ |
|
|
17907 |
zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'", |
|
|
17908 |
zLike, azArg[i]); |
|
|
17909 |
}else{ |
|
|
17910 |
zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]); |
|
|
17911 |
} |
|
|
17912 |
} |
|
|
17913 |
|
|
|
17914 |
open_db(p, 0); |
|
|
17915 |
|
|
|
17916 |
if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ |
|
|
17917 |
/* When playing back a "dump", the content might appear in an order |
|
|
17918 |
** which causes immediate foreign key constraints to be violated. |
|
|
17919 |
** So disable foreign-key constraint enforcement to prevent problems. */ |
|
|
17920 |
raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
|
|
17921 |
raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
|
|
17922 |
} |
|
|
17923 |
p->writableSchema = 0; |
|
|
17924 |
p->showHeader = 0; |
|
|
17925 |
/* Set writable_schema=ON since doing so forces SQLite to initialize |
|
|
17926 |
** as much of the schema as it can even if the sqlite_schema table is |
|
|
17927 |
** corrupt. */ |
|
|
17928 |
sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
|
|
17929 |
p->nErr = 0; |
|
|
17930 |
if( zLike==0 ) zLike = sqlite3_mprintf("true"); |
|
|
17931 |
zSql = sqlite3_mprintf( |
|
|
17932 |
"SELECT name, type, sql FROM sqlite_schema " |
|
|
17933 |
"WHERE (%s) AND type=='table'" |
|
|
17934 |
" AND sql NOT NULL" |
|
|
17935 |
" ORDER BY tbl_name='sqlite_sequence', rowid", |
|
|
17936 |
zLike |
|
|
17937 |
); |
|
|
17938 |
run_schema_dump_query(p,zSql); |
|
|
17939 |
sqlite3_free(zSql); |
|
|
17940 |
if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ |
|
|
17941 |
zSql = sqlite3_mprintf( |
|
|
17942 |
"SELECT sql FROM sqlite_schema " |
|
|
17943 |
"WHERE (%s) AND sql NOT NULL" |
|
|
17944 |
" AND type IN ('index','trigger','view')", |
|
|
17945 |
zLike |
|
|
17946 |
); |
|
|
17947 |
run_table_dump_query(p, zSql); |
|
|
17948 |
sqlite3_free(zSql); |
|
|
17949 |
} |
|
|
17950 |
sqlite3_free(zLike); |
|
|
17951 |
if( p->writableSchema ){ |
|
|
17952 |
raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
|
|
17953 |
p->writableSchema = 0; |
|
|
17954 |
} |
|
|
17955 |
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
|
|
17956 |
sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
|
|
17957 |
if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ |
|
|
17958 |
raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n"); |
|
|
17959 |
} |
|
|
17960 |
p->showHeader = savedShowHeader; |
|
|
17961 |
p->shellFlgs = savedShellFlags; |
|
|
17962 |
}else |
|
|
17963 |
|
|
|
17964 |
if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
|
|
17965 |
if( nArg==2 ){ |
|
|
17966 |
setOrClearFlag(p, SHFLG_Echo, azArg[1]); |
|
|
17967 |
}else{ |
|
|
17968 |
raw_printf(stderr, "Usage: .echo on|off\n"); |
|
|
17969 |
rc = 1; |
|
|
17970 |
} |
|
|
17971 |
}else |
|
|
17972 |
|
|
|
17973 |
if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
|
|
17974 |
if( nArg==2 ){ |
|
|
17975 |
p->autoEQPtest = 0; |
|
|
17976 |
if( p->autoEQPtrace ){ |
|
|
17977 |
if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); |
|
|
17978 |
p->autoEQPtrace = 0; |
|
|
17979 |
} |
|
|
17980 |
if( strcmp(azArg[1],"full")==0 ){ |
|
|
17981 |
p->autoEQP = AUTOEQP_full; |
|
|
17982 |
}else if( strcmp(azArg[1],"trigger")==0 ){ |
|
|
17983 |
p->autoEQP = AUTOEQP_trigger; |
|
|
17984 |
#ifdef SQLITE_DEBUG |
|
|
17985 |
}else if( strcmp(azArg[1],"test")==0 ){ |
|
|
17986 |
p->autoEQP = AUTOEQP_on; |
|
|
17987 |
p->autoEQPtest = 1; |
|
|
17988 |
}else if( strcmp(azArg[1],"trace")==0 ){ |
|
|
17989 |
p->autoEQP = AUTOEQP_full; |
|
|
17990 |
p->autoEQPtrace = 1; |
|
|
17991 |
open_db(p, 0); |
|
|
17992 |
sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0); |
|
|
17993 |
sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); |
|
|
17994 |
#endif |
|
|
17995 |
}else{ |
|
|
17996 |
p->autoEQP = (u8)booleanValue(azArg[1]); |
|
|
17997 |
} |
|
|
17998 |
}else{ |
|
|
17999 |
raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n"); |
|
|
18000 |
rc = 1; |
|
|
18001 |
} |
|
|
18002 |
}else |
|
|
18003 |
|
|
|
18004 |
if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
|
|
18005 |
if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
|
|
18006 |
rc = 2; |
|
|
18007 |
}else |
|
|
18008 |
|
|
|
18009 |
/* The ".explain" command is automatic now. It is largely pointless. It |
|
|
18010 |
** retained purely for backwards compatibility */ |
|
|
18011 |
if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
|
|
18012 |
int val = 1; |
|
|
18013 |
if( nArg>=2 ){ |
|
|
18014 |
if( strcmp(azArg[1],"auto")==0 ){ |
|
|
18015 |
val = 99; |
|
|
18016 |
}else{ |
|
|
18017 |
val = booleanValue(azArg[1]); |
|
|
18018 |
} |
|
|
18019 |
} |
|
|
18020 |
if( val==1 && p->mode!=MODE_Explain ){ |
|
|
18021 |
p->normalMode = p->mode; |
|
|
18022 |
p->mode = MODE_Explain; |
|
|
18023 |
p->autoExplain = 0; |
|
|
18024 |
}else if( val==0 ){ |
|
|
18025 |
if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
|
|
18026 |
p->autoExplain = 0; |
|
|
18027 |
}else if( val==99 ){ |
|
|
18028 |
if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
|
|
18029 |
p->autoExplain = 1; |
|
|
18030 |
} |
|
|
18031 |
}else |
|
|
18032 |
|
|
|
18033 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
18034 |
if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){ |
|
|
18035 |
open_db(p, 0); |
|
|
18036 |
expertDotCommand(p, azArg, nArg); |
|
|
18037 |
}else |
|
|
18038 |
#endif |
|
|
18039 |
|
|
|
18040 |
if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){ |
|
|
18041 |
static const struct { |
|
|
18042 |
const char *zCtrlName; /* Name of a test-control option */ |
|
|
18043 |
int ctrlCode; /* Integer code for that option */ |
|
|
18044 |
const char *zUsage; /* Usage notes */ |
|
|
18045 |
} aCtrl[] = { |
|
|
18046 |
{ "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" }, |
|
|
18047 |
{ "data_version", SQLITE_FCNTL_DATA_VERSION, "" }, |
|
|
18048 |
{ "has_moved", SQLITE_FCNTL_HAS_MOVED, "" }, |
|
|
18049 |
{ "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" }, |
|
|
18050 |
{ "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" }, |
|
|
18051 |
/* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/ |
|
|
18052 |
{ "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" }, |
|
|
18053 |
{ "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" }, |
|
|
18054 |
{ "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" }, |
|
|
18055 |
{ "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" }, |
|
|
18056 |
/* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/ |
|
|
18057 |
}; |
|
|
18058 |
int filectrl = -1; |
|
|
18059 |
int iCtrl = -1; |
|
|
18060 |
sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */ |
|
|
18061 |
int isOk = 0; /* 0: usage 1: %lld 2: no-result */ |
|
|
18062 |
int n2, i; |
|
|
18063 |
const char *zCmd = 0; |
|
|
18064 |
const char *zSchema = 0; |
|
|
18065 |
|
|
|
18066 |
open_db(p, 0); |
|
|
18067 |
zCmd = nArg>=2 ? azArg[1] : "help"; |
|
|
18068 |
|
|
|
18069 |
if( zCmd[0]=='-' |
|
|
18070 |
&& (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0) |
|
|
18071 |
&& nArg>=4 |
|
|
18072 |
){ |
|
|
18073 |
zSchema = azArg[2]; |
|
|
18074 |
for(i=3; i |
|
|
18075 |
nArg -= 2; |
|
|
18076 |
zCmd = azArg[1]; |
|
|
18077 |
} |
|
|
18078 |
|
|
|
18079 |
/* The argument can optionally begin with "-" or "--" */ |
|
|
18080 |
if( zCmd[0]=='-' && zCmd[1] ){ |
|
|
18081 |
zCmd++; |
|
|
18082 |
if( zCmd[0]=='-' && zCmd[1] ) zCmd++; |
|
|
18083 |
} |
|
|
18084 |
|
|
|
18085 |
/* --help lists all file-controls */ |
|
|
18086 |
if( strcmp(zCmd,"help")==0 ){ |
|
|
18087 |
utf8_printf(p->out, "Available file-controls:\n"); |
|
|
18088 |
for(i=0; i |
|
|
18089 |
utf8_printf(p->out, " .filectrl %s %s\n", |
|
|
18090 |
aCtrl[i].zCtrlName, aCtrl[i].zUsage); |
|
|
18091 |
} |
|
|
18092 |
rc = 1; |
|
|
18093 |
goto meta_command_exit; |
|
|
18094 |
} |
|
|
18095 |
|
|
|
18096 |
/* convert filectrl text option to value. allow any unique prefix |
|
|
18097 |
** of the option name, or a numerical value. */ |
|
|
18098 |
n2 = strlen30(zCmd); |
|
|
18099 |
for(i=0; i |
|
|
18100 |
if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ |
|
|
18101 |
if( filectrl<0 ){ |
|
|
18102 |
filectrl = aCtrl[i].ctrlCode; |
|
|
18103 |
iCtrl = i; |
|
|
18104 |
}else{ |
|
|
18105 |
utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n" |
|
|
18106 |
"Use \".filectrl --help\" for help\n", zCmd); |
|
|
18107 |
rc = 1; |
|
|
18108 |
goto meta_command_exit; |
|
|
18109 |
} |
|
|
18110 |
} |
|
|
18111 |
} |
|
|
18112 |
if( filectrl<0 ){ |
|
|
18113 |
utf8_printf(stderr,"Error: unknown file-control: %s\n" |
|
|
18114 |
"Use \".filectrl --help\" for help\n", zCmd); |
|
|
18115 |
}else{ |
|
|
18116 |
switch(filectrl){ |
|
|
18117 |
case SQLITE_FCNTL_SIZE_LIMIT: { |
|
|
18118 |
if( nArg!=2 && nArg!=3 ) break; |
|
|
18119 |
iRes = nArg==3 ? integerValue(azArg[2]) : -1; |
|
|
18120 |
sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes); |
|
|
18121 |
isOk = 1; |
|
|
18122 |
break; |
|
|
18123 |
} |
|
|
18124 |
case SQLITE_FCNTL_LOCK_TIMEOUT: |
|
|
18125 |
case SQLITE_FCNTL_CHUNK_SIZE: { |
|
|
18126 |
int x; |
|
|
18127 |
if( nArg!=3 ) break; |
|
|
18128 |
x = (int)integerValue(azArg[2]); |
|
|
18129 |
sqlite3_file_control(p->db, zSchema, filectrl, &x); |
|
|
18130 |
isOk = 2; |
|
|
18131 |
break; |
|
|
18132 |
} |
|
|
18133 |
case SQLITE_FCNTL_PERSIST_WAL: |
|
|
18134 |
case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
|
|
18135 |
int x; |
|
|
18136 |
if( nArg!=2 && nArg!=3 ) break; |
|
|
18137 |
x = nArg==3 ? booleanValue(azArg[2]) : -1; |
|
|
18138 |
sqlite3_file_control(p->db, zSchema, filectrl, &x); |
|
|
18139 |
iRes = x; |
|
|
18140 |
isOk = 1; |
|
|
18141 |
break; |
|
|
18142 |
} |
|
|
18143 |
case SQLITE_FCNTL_DATA_VERSION: |
|
|
18144 |
case SQLITE_FCNTL_HAS_MOVED: { |
|
|
18145 |
int x; |
|
|
18146 |
if( nArg!=2 ) break; |
|
|
18147 |
sqlite3_file_control(p->db, zSchema, filectrl, &x); |
|
|
18148 |
iRes = x; |
|
|
18149 |
isOk = 1; |
|
|
18150 |
break; |
|
|
18151 |
} |
|
|
18152 |
case SQLITE_FCNTL_TEMPFILENAME: { |
|
|
18153 |
char *z = 0; |
|
|
18154 |
if( nArg!=2 ) break; |
|
|
18155 |
sqlite3_file_control(p->db, zSchema, filectrl, &z); |
|
|
18156 |
if( z ){ |
|
|
18157 |
utf8_printf(p->out, "%s\n", z); |
|
|
18158 |
sqlite3_free(z); |
|
|
18159 |
} |
|
|
18160 |
isOk = 2; |
|
|
18161 |
break; |
|
|
18162 |
} |
|
|
18163 |
case SQLITE_FCNTL_RESERVE_BYTES: { |
|
|
18164 |
int x; |
|
|
18165 |
if( nArg>=3 ){ |
|
|
18166 |
x = atoi(azArg[2]); |
|
|
18167 |
sqlite3_file_control(p->db, zSchema, filectrl, &x); |
|
|
18168 |
} |
|
|
18169 |
x = -1; |
|
|
18170 |
sqlite3_file_control(p->db, zSchema, filectrl, &x); |
|
|
18171 |
utf8_printf(p->out,"%d\n", x); |
|
|
18172 |
isOk = 2; |
|
|
18173 |
break; |
|
|
18174 |
} |
|
|
18175 |
} |
|
|
18176 |
} |
|
|
18177 |
if( isOk==0 && iCtrl>=0 ){ |
|
|
18178 |
utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); |
|
|
18179 |
rc = 1; |
|
|
18180 |
}else if( isOk==1 ){ |
|
|
18181 |
char zBuf[100]; |
|
|
18182 |
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes); |
|
|
18183 |
raw_printf(p->out, "%s\n", zBuf); |
|
|
18184 |
} |
|
|
18185 |
}else |
|
|
18186 |
|
|
|
18187 |
if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
|
|
18188 |
ShellState data; |
|
|
18189 |
char *zErrMsg = 0; |
|
|
18190 |
int doStats = 0; |
|
|
18191 |
memcpy(&data, p, sizeof(data)); |
|
|
18192 |
data.showHeader = 0; |
|
|
18193 |
data.cMode = data.mode = MODE_Semi; |
|
|
18194 |
if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
|
|
18195 |
data.cMode = data.mode = MODE_Pretty; |
|
|
18196 |
nArg = 1; |
|
|
18197 |
} |
|
|
18198 |
if( nArg!=1 ){ |
|
|
18199 |
raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
|
|
18200 |
rc = 1; |
|
|
18201 |
goto meta_command_exit; |
|
|
18202 |
} |
|
|
18203 |
open_db(p, 0); |
|
|
18204 |
rc = sqlite3_exec(p->db, |
|
|
18205 |
"SELECT sql FROM" |
|
|
18206 |
" (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
|
|
18207 |
" FROM sqlite_schema UNION ALL" |
|
|
18208 |
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) " |
|
|
18209 |
"WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
|
|
18210 |
"ORDER BY rowid", |
|
|
18211 |
callback, &data, &zErrMsg |
|
|
18212 |
); |
|
|
18213 |
if( rc==SQLITE_OK ){ |
|
|
18214 |
sqlite3_stmt *pStmt; |
|
|
18215 |
rc = sqlite3_prepare_v2(p->db, |
|
|
18216 |
"SELECT rowid FROM sqlite_schema" |
|
|
18217 |
" WHERE name GLOB 'sqlite_stat[134]'", |
|
|
18218 |
-1, &pStmt, 0); |
|
|
18219 |
doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
|
|
18220 |
sqlite3_finalize(pStmt); |
|
|
18221 |
} |
|
|
18222 |
if( doStats==0 ){ |
|
|
18223 |
raw_printf(p->out, "/* No STAT tables available */\n"); |
|
|
18224 |
}else{ |
|
|
18225 |
raw_printf(p->out, "ANALYZE sqlite_schema;\n"); |
|
|
18226 |
sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'", |
|
|
18227 |
callback, &data, &zErrMsg); |
|
|
18228 |
data.cMode = data.mode = MODE_Insert; |
|
|
18229 |
data.zDestTable = "sqlite_stat1"; |
|
|
18230 |
shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg); |
|
|
18231 |
data.zDestTable = "sqlite_stat4"; |
|
|
18232 |
shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg); |
|
|
18233 |
raw_printf(p->out, "ANALYZE sqlite_schema;\n"); |
|
|
18234 |
} |
|
|
18235 |
}else |
|
|
18236 |
|
|
|
18237 |
if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
|
|
18238 |
if( nArg==2 ){ |
|
|
18239 |
p->showHeader = booleanValue(azArg[1]); |
|
|
18240 |
p->shellFlgs |= SHFLG_HeaderSet; |
|
|
18241 |
}else{ |
|
|
18242 |
raw_printf(stderr, "Usage: .headers on|off\n"); |
|
|
18243 |
rc = 1; |
|
|
18244 |
} |
|
|
18245 |
}else |
|
|
18246 |
|
|
|
18247 |
if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
|
|
18248 |
if( nArg>=2 ){ |
|
|
18249 |
n = showHelp(p->out, azArg[1]); |
|
|
18250 |
if( n==0 ){ |
|
|
18251 |
utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]); |
|
|
18252 |
} |
|
|
18253 |
}else{ |
|
|
18254 |
showHelp(p->out, 0); |
|
|
18255 |
} |
|
|
18256 |
}else |
|
|
18257 |
|
|
|
18258 |
if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
|
|
18259 |
char *zTable = 0; /* Insert data into this table */ |
|
|
18260 |
char *zFile = 0; /* Name of file to extra content from */ |
|
|
18261 |
sqlite3_stmt *pStmt = NULL; /* A statement */ |
|
|
18262 |
int nCol; /* Number of columns in the table */ |
|
|
18263 |
int nByte; /* Number of bytes in an SQL string */ |
|
|
18264 |
int i, j; /* Loop counters */ |
|
|
18265 |
int needCommit; /* True to COMMIT or ROLLBACK at end */ |
|
|
18266 |
int nSep; /* Number of bytes in p->colSeparator[] */ |
|
|
18267 |
char *zSql; /* An SQL statement */ |
|
|
18268 |
ImportCtx sCtx; /* Reader context */ |
|
|
18269 |
char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
|
|
18270 |
int eVerbose = 0; /* Larger for more console output */ |
|
|
18271 |
int nSkip = 0; /* Initial lines to skip */ |
|
|
18272 |
int useOutputMode = 1; /* Use output mode to determine separators */ |
|
|
18273 |
|
|
|
18274 |
memset(&sCtx, 0, sizeof(sCtx)); |
|
|
18275 |
if( p->mode==MODE_Ascii ){ |
|
|
18276 |
xRead = ascii_read_one_field; |
|
|
18277 |
}else{ |
|
|
18278 |
xRead = csv_read_one_field; |
|
|
18279 |
} |
|
|
18280 |
for(i=1; i |
|
|
18281 |
char *z = azArg[i]; |
|
|
18282 |
if( z[0]=='-' && z[1]=='-' ) z++; |
|
|
18283 |
if( z[0]!='-' ){ |
|
|
18284 |
if( zFile==0 ){ |
|
|
18285 |
zFile = z; |
|
|
18286 |
}else if( zTable==0 ){ |
|
|
18287 |
zTable = z; |
|
|
18288 |
}else{ |
|
|
18289 |
utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z); |
|
|
18290 |
showHelp(p->out, "import"); |
|
|
18291 |
rc = 1; |
|
|
18292 |
goto meta_command_exit; |
|
|
18293 |
} |
|
|
18294 |
}else if( strcmp(z,"-v")==0 ){ |
|
|
18295 |
eVerbose++; |
|
|
18296 |
}else if( strcmp(z,"-skip")==0 && i |
|
|
18297 |
nSkip = integerValue(azArg[++i]); |
|
|
18298 |
}else if( strcmp(z,"-ascii")==0 ){ |
|
|
18299 |
sCtx.cColSep = SEP_Unit[0]; |
|
|
18300 |
sCtx.cRowSep = SEP_Record[0]; |
|
|
18301 |
xRead = ascii_read_one_field; |
|
|
18302 |
useOutputMode = 0; |
|
|
18303 |
}else if( strcmp(z,"-csv")==0 ){ |
|
|
18304 |
sCtx.cColSep = ','; |
|
|
18305 |
sCtx.cRowSep = '\n'; |
|
|
18306 |
xRead = csv_read_one_field; |
|
|
18307 |
useOutputMode = 0; |
|
|
18308 |
}else{ |
|
|
18309 |
utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z); |
|
|
18310 |
showHelp(p->out, "import"); |
|
|
18311 |
rc = 1; |
|
|
18312 |
goto meta_command_exit; |
|
|
18313 |
} |
|
|
18314 |
} |
|
|
18315 |
if( zTable==0 ){ |
|
|
18316 |
utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n", |
|
|
18317 |
zFile==0 ? "FILE" : "TABLE"); |
|
|
18318 |
showHelp(p->out, "import"); |
|
|
18319 |
rc = 1; |
|
|
18320 |
goto meta_command_exit; |
|
|
18321 |
} |
|
|
18322 |
seenInterrupt = 0; |
|
|
18323 |
open_db(p, 0); |
|
|
18324 |
if( useOutputMode ){ |
|
|
18325 |
/* If neither the --csv or --ascii options are specified, then set |
|
|
18326 |
** the column and row separator characters from the output mode. */ |
|
|
18327 |
nSep = strlen30(p->colSeparator); |
|
|
18328 |
if( nSep==0 ){ |
|
|
18329 |
raw_printf(stderr, |
|
|
18330 |
"Error: non-null column separator required for import\n"); |
|
|
18331 |
rc = 1; |
|
|
18332 |
goto meta_command_exit; |
|
|
18333 |
} |
|
|
18334 |
if( nSep>1 ){ |
|
|
18335 |
raw_printf(stderr, |
|
|
18336 |
"Error: multi-character column separators not allowed" |
|
|
18337 |
" for import\n"); |
|
|
18338 |
rc = 1; |
|
|
18339 |
goto meta_command_exit; |
|
|
18340 |
} |
|
|
18341 |
nSep = strlen30(p->rowSeparator); |
|
|
18342 |
if( nSep==0 ){ |
|
|
18343 |
raw_printf(stderr, |
|
|
18344 |
"Error: non-null row separator required for import\n"); |
|
|
18345 |
rc = 1; |
|
|
18346 |
goto meta_command_exit; |
|
|
18347 |
} |
|
|
18348 |
if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){ |
|
|
18349 |
/* When importing CSV (only), if the row separator is set to the |
|
|
18350 |
** default output row separator, change it to the default input |
|
|
18351 |
** row separator. This avoids having to maintain different input |
|
|
18352 |
** and output row separators. */ |
|
|
18353 |
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
|
|
18354 |
nSep = strlen30(p->rowSeparator); |
|
|
18355 |
} |
|
|
18356 |
if( nSep>1 ){ |
|
|
18357 |
raw_printf(stderr, "Error: multi-character row separators not allowed" |
|
|
18358 |
" for import\n"); |
|
|
18359 |
rc = 1; |
|
|
18360 |
goto meta_command_exit; |
|
|
18361 |
} |
|
|
18362 |
sCtx.cColSep = p->colSeparator[0]; |
|
|
18363 |
sCtx.cRowSep = p->rowSeparator[0]; |
|
|
18364 |
} |
|
|
18365 |
sCtx.zFile = zFile; |
|
|
18366 |
sCtx.nLine = 1; |
|
|
18367 |
if( sCtx.zFile[0]=='|' ){ |
|
|
18368 |
#ifdef SQLITE_OMIT_POPEN |
|
|
18369 |
raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
|
|
18370 |
rc = 1; |
|
|
18371 |
goto meta_command_exit; |
|
|
18372 |
#else |
|
|
18373 |
sCtx.in = popen(sCtx.zFile+1, "r"); |
|
|
18374 |
sCtx.zFile = ""; |
|
|
18375 |
sCtx.xCloser = pclose; |
|
|
18376 |
#endif |
|
|
18377 |
}else{ |
|
|
18378 |
sCtx.in = fopen(sCtx.zFile, "rb"); |
|
|
18379 |
sCtx.xCloser = fclose; |
|
|
18380 |
} |
|
|
18381 |
if( sCtx.in==0 ){ |
|
|
18382 |
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
|
|
18383 |
rc = 1; |
|
|
18384 |
goto meta_command_exit; |
|
|
18385 |
} |
|
|
18386 |
if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){ |
|
|
18387 |
char zSep[2]; |
|
|
18388 |
zSep[1] = 0; |
|
|
18389 |
zSep[0] = sCtx.cColSep; |
|
|
18390 |
utf8_printf(p->out, "Column separator "); |
|
|
18391 |
output_c_string(p->out, zSep); |
|
|
18392 |
utf8_printf(p->out, ", row separator "); |
|
|
18393 |
zSep[0] = sCtx.cRowSep; |
|
|
18394 |
output_c_string(p->out, zSep); |
|
|
18395 |
utf8_printf(p->out, "\n"); |
|
|
18396 |
} |
|
|
18397 |
while( (nSkip--)>0 ){ |
|
|
18398 |
while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){} |
|
|
18399 |
} |
|
|
18400 |
zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
|
|
18401 |
if( zSql==0 ){ |
|
|
18402 |
import_cleanup(&sCtx); |
|
|
18403 |
shell_out_of_memory(); |
|
|
18404 |
} |
|
|
18405 |
nByte = strlen30(zSql); |
|
|
18406 |
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
|
18407 |
import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
|
|
18408 |
if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
|
|
18409 |
char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable); |
|
|
18410 |
char cSep = '('; |
|
|
18411 |
while( xRead(&sCtx) ){ |
|
|
18412 |
zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
|
|
18413 |
cSep = ','; |
|
|
18414 |
if( sCtx.cTerm!=sCtx.cColSep ) break; |
|
|
18415 |
} |
|
|
18416 |
if( cSep=='(' ){ |
|
|
18417 |
sqlite3_free(zCreate); |
|
|
18418 |
import_cleanup(&sCtx); |
|
|
18419 |
utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
|
|
18420 |
rc = 1; |
|
|
18421 |
goto meta_command_exit; |
|
|
18422 |
} |
|
|
18423 |
zCreate = sqlite3_mprintf("%z\n)", zCreate); |
|
|
18424 |
if( eVerbose>=1 ){ |
|
|
18425 |
utf8_printf(p->out, "%s\n", zCreate); |
|
|
18426 |
} |
|
|
18427 |
rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
|
|
18428 |
sqlite3_free(zCreate); |
|
|
18429 |
if( rc ){ |
|
|
18430 |
utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable, |
|
|
18431 |
sqlite3_errmsg(p->db)); |
|
|
18432 |
import_cleanup(&sCtx); |
|
|
18433 |
rc = 1; |
|
|
18434 |
goto meta_command_exit; |
|
|
18435 |
} |
|
|
18436 |
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
|
18437 |
} |
|
|
18438 |
sqlite3_free(zSql); |
|
|
18439 |
if( rc ){ |
|
|
18440 |
if (pStmt) sqlite3_finalize(pStmt); |
|
|
18441 |
utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
|
|
18442 |
import_cleanup(&sCtx); |
|
|
18443 |
rc = 1; |
|
|
18444 |
goto meta_command_exit; |
|
|
18445 |
} |
|
|
18446 |
nCol = sqlite3_column_count(pStmt); |
|
|
18447 |
sqlite3_finalize(pStmt); |
|
|
18448 |
pStmt = 0; |
|
|
18449 |
if( nCol==0 ) return 0; /* no columns, no error */ |
|
|
18450 |
zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
|
|
18451 |
if( zSql==0 ){ |
|
|
18452 |
import_cleanup(&sCtx); |
|
|
18453 |
shell_out_of_memory(); |
|
|
18454 |
} |
|
|
18455 |
sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
|
|
18456 |
j = strlen30(zSql); |
|
|
18457 |
for(i=1; i |
|
|
18458 |
zSql[j++] = ','; |
|
|
18459 |
zSql[j++] = '?'; |
|
|
18460 |
} |
|
|
18461 |
zSql[j++] = ')'; |
|
|
18462 |
zSql[j] = 0; |
|
|
18463 |
if( eVerbose>=2 ){ |
|
|
18464 |
utf8_printf(p->out, "Insert using: %s\n", zSql); |
|
|
18465 |
} |
|
|
18466 |
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
|
18467 |
sqlite3_free(zSql); |
|
|
18468 |
if( rc ){ |
|
|
18469 |
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
|
|
18470 |
if (pStmt) sqlite3_finalize(pStmt); |
|
|
18471 |
import_cleanup(&sCtx); |
|
|
18472 |
rc = 1; |
|
|
18473 |
goto meta_command_exit; |
|
|
18474 |
} |
|
|
18475 |
needCommit = sqlite3_get_autocommit(p->db); |
|
|
18476 |
if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
|
|
18477 |
do{ |
|
|
18478 |
int startLine = sCtx.nLine; |
|
|
18479 |
for(i=0; i |
|
|
18480 |
char *z = xRead(&sCtx); |
|
|
18481 |
/* |
|
|
18482 |
** Did we reach end-of-file before finding any columns? |
|
|
18483 |
** If so, stop instead of NULL filling the remaining columns. |
|
|
18484 |
*/ |
|
|
18485 |
if( z==0 && i==0 ) break; |
|
|
18486 |
/* |
|
|
18487 |
** Did we reach end-of-file OR end-of-line before finding any |
|
|
18488 |
** columns in ASCII mode? If so, stop instead of NULL filling |
|
|
18489 |
** the remaining columns. |
|
|
18490 |
*/ |
|
|
18491 |
if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
|
|
18492 |
sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
|
|
18493 |
if( i |
|
|
18494 |
utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
|
|
18495 |
"filling the rest with NULL\n", |
|
|
18496 |
sCtx.zFile, startLine, nCol, i+1); |
|
|
18497 |
i += 2; |
|
|
18498 |
while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
|
|
18499 |
} |
|
|
18500 |
} |
|
|
18501 |
if( sCtx.cTerm==sCtx.cColSep ){ |
|
|
18502 |
do{ |
|
|
18503 |
xRead(&sCtx); |
|
|
18504 |
i++; |
|
|
18505 |
}while( sCtx.cTerm==sCtx.cColSep ); |
|
|
18506 |
utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
|
|
18507 |
"extras ignored\n", |
|
|
18508 |
sCtx.zFile, startLine, nCol, i); |
|
|
18509 |
} |
|
|
18510 |
if( i>=nCol ){ |
|
|
18511 |
sqlite3_step(pStmt); |
|
|
18512 |
rc = sqlite3_reset(pStmt); |
|
|
18513 |
if( rc!=SQLITE_OK ){ |
|
|
18514 |
utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
|
|
18515 |
startLine, sqlite3_errmsg(p->db)); |
|
|
18516 |
sCtx.nErr++; |
|
|
18517 |
}else{ |
|
|
18518 |
sCtx.nRow++; |
|
|
18519 |
} |
|
|
18520 |
} |
|
|
18521 |
}while( sCtx.cTerm!=EOF ); |
|
|
18522 |
|
|
|
18523 |
import_cleanup(&sCtx); |
|
|
18524 |
sqlite3_finalize(pStmt); |
|
|
18525 |
if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
|
|
18526 |
if( eVerbose>0 ){ |
|
|
18527 |
utf8_printf(p->out, |
|
|
18528 |
"Added %d rows with %d errors using %d lines of input\n", |
|
|
18529 |
sCtx.nRow, sCtx.nErr, sCtx.nLine-1); |
|
|
18530 |
} |
|
|
18531 |
}else |
|
|
18532 |
|
|
|
18533 |
#ifndef SQLITE_UNTESTABLE |
|
|
18534 |
if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){ |
|
|
18535 |
char *zSql; |
|
|
18536 |
char *zCollist = 0; |
|
|
18537 |
sqlite3_stmt *pStmt; |
|
|
18538 |
int tnum = 0; |
|
|
18539 |
int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */ |
|
|
18540 |
int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */ |
|
|
18541 |
int i; |
|
|
18542 |
if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){ |
|
|
18543 |
utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n" |
|
|
18544 |
" .imposter off\n"); |
|
|
18545 |
/* Also allowed, but not documented: |
|
|
18546 |
** |
|
|
18547 |
** .imposter TABLE IMPOSTER |
|
|
18548 |
** |
|
|
18549 |
** where TABLE is a WITHOUT ROWID table. In that case, the |
|
|
18550 |
** imposter is another WITHOUT ROWID table with the columns in |
|
|
18551 |
** storage order. */ |
|
|
18552 |
rc = 1; |
|
|
18553 |
goto meta_command_exit; |
|
|
18554 |
} |
|
|
18555 |
open_db(p, 0); |
|
|
18556 |
if( nArg==2 ){ |
|
|
18557 |
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); |
|
|
18558 |
goto meta_command_exit; |
|
|
18559 |
} |
|
|
18560 |
zSql = sqlite3_mprintf( |
|
|
18561 |
"SELECT rootpage, 0 FROM sqlite_schema" |
|
|
18562 |
" WHERE name='%q' AND type='index'" |
|
|
18563 |
"UNION ALL " |
|
|
18564 |
"SELECT rootpage, 1 FROM sqlite_schema" |
|
|
18565 |
" WHERE name='%q' AND type='table'" |
|
|
18566 |
" AND sql LIKE '%%without%%rowid%%'", |
|
|
18567 |
azArg[1], azArg[1] |
|
|
18568 |
); |
|
|
18569 |
sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
|
18570 |
sqlite3_free(zSql); |
|
|
18571 |
if( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
18572 |
tnum = sqlite3_column_int(pStmt, 0); |
|
|
18573 |
isWO = sqlite3_column_int(pStmt, 1); |
|
|
18574 |
} |
|
|
18575 |
sqlite3_finalize(pStmt); |
|
|
18576 |
zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); |
|
|
18577 |
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
|
18578 |
sqlite3_free(zSql); |
|
|
18579 |
i = 0; |
|
|
18580 |
while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
18581 |
char zLabel[20]; |
|
|
18582 |
const char *zCol = (const char*)sqlite3_column_text(pStmt,2); |
|
|
18583 |
i++; |
|
|
18584 |
if( zCol==0 ){ |
|
|
18585 |
if( sqlite3_column_int(pStmt,1)==-1 ){ |
|
|
18586 |
zCol = "_ROWID_"; |
|
|
18587 |
}else{ |
|
|
18588 |
sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); |
|
|
18589 |
zCol = zLabel; |
|
|
18590 |
} |
|
|
18591 |
} |
|
|
18592 |
if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){ |
|
|
18593 |
lenPK = (int)strlen(zCollist); |
|
|
18594 |
} |
|
|
18595 |
if( zCollist==0 ){ |
|
|
18596 |
zCollist = sqlite3_mprintf("\"%w\"", zCol); |
|
|
18597 |
}else{ |
|
|
18598 |
zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); |
|
|
18599 |
} |
|
|
18600 |
} |
|
|
18601 |
sqlite3_finalize(pStmt); |
|
|
18602 |
if( i==0 || tnum==0 ){ |
|
|
18603 |
utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]); |
|
|
18604 |
rc = 1; |
|
|
18605 |
sqlite3_free(zCollist); |
|
|
18606 |
goto meta_command_exit; |
|
|
18607 |
} |
|
|
18608 |
if( lenPK==0 ) lenPK = 100000; |
|
|
18609 |
zSql = sqlite3_mprintf( |
|
|
18610 |
"CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID", |
|
|
18611 |
azArg[2], zCollist, lenPK, zCollist); |
|
|
18612 |
sqlite3_free(zCollist); |
|
|
18613 |
rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); |
|
|
18614 |
if( rc==SQLITE_OK ){ |
|
|
18615 |
rc = sqlite3_exec(p->db, zSql, 0, 0, 0); |
|
|
18616 |
sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); |
|
|
18617 |
if( rc ){ |
|
|
18618 |
utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); |
|
|
18619 |
}else{ |
|
|
18620 |
utf8_printf(stdout, "%s;\n", zSql); |
|
|
18621 |
raw_printf(stdout, |
|
|
18622 |
"WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n", |
|
|
18623 |
azArg[1], isWO ? "table" : "index" |
|
|
18624 |
); |
|
|
18625 |
} |
|
|
18626 |
}else{ |
|
|
18627 |
raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); |
|
|
18628 |
rc = 1; |
|
|
18629 |
} |
|
|
18630 |
sqlite3_free(zSql); |
|
|
18631 |
}else |
|
|
18632 |
#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ |
|
|
18633 |
|
|
|
18634 |
#ifdef SQLITE_ENABLE_IOTRACE |
|
|
18635 |
if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
|
|
18636 |
SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
|
|
18637 |
if( iotrace && iotrace!=stdout ) fclose(iotrace); |
|
|
18638 |
iotrace = 0; |
|
|
18639 |
if( nArg<2 ){ |
|
|
18640 |
sqlite3IoTrace = 0; |
|
|
18641 |
}else if( strcmp(azArg[1], "-")==0 ){ |
|
|
18642 |
sqlite3IoTrace = iotracePrintf; |
|
|
18643 |
iotrace = stdout; |
|
|
18644 |
}else{ |
|
|
18645 |
iotrace = fopen(azArg[1], "w"); |
|
|
18646 |
if( iotrace==0 ){ |
|
|
18647 |
utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
|
|
18648 |
sqlite3IoTrace = 0; |
|
|
18649 |
rc = 1; |
|
|
18650 |
}else{ |
|
|
18651 |
sqlite3IoTrace = iotracePrintf; |
|
|
18652 |
} |
|
|
18653 |
} |
|
|
18654 |
}else |
|
|
18655 |
#endif |
|
|
18656 |
|
|
|
18657 |
if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
|
|
18658 |
static const struct { |
|
|
18659 |
const char *zLimitName; /* Name of a limit */ |
|
|
18660 |
int limitCode; /* Integer code for that limit */ |
|
|
18661 |
} aLimit[] = { |
|
|
18662 |
{ "length", SQLITE_LIMIT_LENGTH }, |
|
|
18663 |
{ "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
|
|
18664 |
{ "column", SQLITE_LIMIT_COLUMN }, |
|
|
18665 |
{ "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
|
|
18666 |
{ "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
|
|
18667 |
{ "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
|
|
18668 |
{ "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
|
|
18669 |
{ "attached", SQLITE_LIMIT_ATTACHED }, |
|
|
18670 |
{ "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
|
|
18671 |
{ "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
|
|
18672 |
{ "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
|
|
18673 |
{ "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
|
|
18674 |
}; |
|
|
18675 |
int i, n2; |
|
|
18676 |
open_db(p, 0); |
|
|
18677 |
if( nArg==1 ){ |
|
|
18678 |
for(i=0; i |
|
|
18679 |
printf("%20s %d\n", aLimit[i].zLimitName, |
|
|
18680 |
sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
|
|
18681 |
} |
|
|
18682 |
}else if( nArg>3 ){ |
|
|
18683 |
raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
|
|
18684 |
rc = 1; |
|
|
18685 |
goto meta_command_exit; |
|
|
18686 |
}else{ |
|
|
18687 |
int iLimit = -1; |
|
|
18688 |
n2 = strlen30(azArg[1]); |
|
|
18689 |
for(i=0; i |
|
|
18690 |
if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
|
|
18691 |
if( iLimit<0 ){ |
|
|
18692 |
iLimit = i; |
|
|
18693 |
}else{ |
|
|
18694 |
utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
|
|
18695 |
rc = 1; |
|
|
18696 |
goto meta_command_exit; |
|
|
18697 |
} |
|
|
18698 |
} |
|
|
18699 |
} |
|
|
18700 |
if( iLimit<0 ){ |
|
|
18701 |
utf8_printf(stderr, "unknown limit: \"%s\"\n" |
|
|
18702 |
"enter \".limits\" with no arguments for a list.\n", |
|
|
18703 |
azArg[1]); |
|
|
18704 |
rc = 1; |
|
|
18705 |
goto meta_command_exit; |
|
|
18706 |
} |
|
|
18707 |
if( nArg==3 ){ |
|
|
18708 |
sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
|
|
18709 |
(int)integerValue(azArg[2])); |
|
|
18710 |
} |
|
|
18711 |
printf("%20s %d\n", aLimit[iLimit].zLimitName, |
|
|
18712 |
sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
|
|
18713 |
} |
|
|
18714 |
}else |
|
|
18715 |
|
|
|
18716 |
if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){ |
|
|
18717 |
open_db(p, 0); |
|
|
18718 |
lintDotCommand(p, azArg, nArg); |
|
|
18719 |
}else |
|
|
18720 |
|
|
|
18721 |
#ifndef SQLITE_OMIT_LOAD_EXTENSION |
|
|
18722 |
if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
|
|
18723 |
const char *zFile, *zProc; |
|
|
18724 |
char *zErrMsg = 0; |
|
|
18725 |
if( nArg<2 ){ |
|
|
18726 |
raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
|
|
18727 |
rc = 1; |
|
|
18728 |
goto meta_command_exit; |
|
|
18729 |
} |
|
|
18730 |
zFile = azArg[1]; |
|
|
18731 |
zProc = nArg>=3 ? azArg[2] : 0; |
|
|
18732 |
open_db(p, 0); |
|
|
18733 |
rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
|
|
18734 |
if( rc!=SQLITE_OK ){ |
|
|
18735 |
utf8_printf(stderr, "Error: %s\n", zErrMsg); |
|
|
18736 |
sqlite3_free(zErrMsg); |
|
|
18737 |
rc = 1; |
|
|
18738 |
} |
|
|
18739 |
}else |
|
|
18740 |
#endif |
|
|
18741 |
|
|
|
18742 |
if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
|
|
18743 |
if( nArg!=2 ){ |
|
|
18744 |
raw_printf(stderr, "Usage: .log FILENAME\n"); |
|
|
18745 |
rc = 1; |
|
|
18746 |
}else{ |
|
|
18747 |
const char *zFile = azArg[1]; |
|
|
18748 |
output_file_close(p->pLog); |
|
|
18749 |
p->pLog = output_file_open(zFile, 0); |
|
|
18750 |
} |
|
|
18751 |
}else |
|
|
18752 |
|
|
|
18753 |
if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
|
|
18754 |
const char *zMode = nArg>=2 ? azArg[1] : ""; |
|
|
18755 |
int n2 = strlen30(zMode); |
|
|
18756 |
int c2 = zMode[0]; |
|
|
18757 |
if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
|
|
18758 |
p->mode = MODE_Line; |
|
|
18759 |
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
|
|
18760 |
}else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
|
|
18761 |
p->mode = MODE_Column; |
|
|
18762 |
if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){ |
|
|
18763 |
p->showHeader = 1; |
|
|
18764 |
} |
|
|
18765 |
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
|
|
18766 |
}else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
|
|
18767 |
p->mode = MODE_List; |
|
|
18768 |
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); |
|
|
18769 |
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
|
|
18770 |
}else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
|
|
18771 |
p->mode = MODE_Html; |
|
|
18772 |
}else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
|
|
18773 |
p->mode = MODE_Tcl; |
|
|
18774 |
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
|
|
18775 |
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
|
|
18776 |
}else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
|
|
18777 |
p->mode = MODE_Csv; |
|
|
18778 |
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
|
|
18779 |
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
|
|
18780 |
}else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
|
|
18781 |
p->mode = MODE_List; |
|
|
18782 |
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
|
|
18783 |
}else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
|
|
18784 |
p->mode = MODE_Insert; |
|
|
18785 |
set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
|
|
18786 |
}else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){ |
|
|
18787 |
p->mode = MODE_Quote; |
|
|
18788 |
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
|
|
18789 |
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
|
|
18790 |
}else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
|
|
18791 |
p->mode = MODE_Ascii; |
|
|
18792 |
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
|
|
18793 |
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
|
|
18794 |
}else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){ |
|
|
18795 |
p->mode = MODE_Markdown; |
|
|
18796 |
}else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){ |
|
|
18797 |
p->mode = MODE_Table; |
|
|
18798 |
}else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){ |
|
|
18799 |
p->mode = MODE_Box; |
|
|
18800 |
}else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){ |
|
|
18801 |
p->mode = MODE_Json; |
|
|
18802 |
}else if( nArg==1 ){ |
|
|
18803 |
raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); |
|
|
18804 |
}else{ |
|
|
18805 |
raw_printf(stderr, "Error: mode should be one of: " |
|
|
18806 |
"ascii box column csv html insert json line list markdown " |
|
|
18807 |
"quote table tabs tcl\n"); |
|
|
18808 |
rc = 1; |
|
|
18809 |
} |
|
|
18810 |
p->cMode = p->mode; |
|
|
18811 |
}else |
|
|
18812 |
|
|
|
18813 |
if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
|
|
18814 |
if( nArg==2 ){ |
|
|
18815 |
sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
|
|
18816 |
"%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
|
|
18817 |
}else{ |
|
|
18818 |
raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
|
|
18819 |
rc = 1; |
|
|
18820 |
} |
|
|
18821 |
}else |
|
|
18822 |
|
|
|
18823 |
#ifdef SQLITE_DEBUG |
|
|
18824 |
if( c=='o' && strcmp(azArg[0],"oom")==0 ){ |
|
|
18825 |
int i; |
|
|
18826 |
for(i=1; i |
|
|
18827 |
const char *z = azArg[i]; |
|
|
18828 |
if( z[0]=='-' && z[1]=='-' ) z++; |
|
|
18829 |
if( strcmp(z,"-repeat")==0 ){ |
|
|
18830 |
if( i==nArg-1 ){ |
|
|
18831 |
raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]); |
|
|
18832 |
rc = 1; |
|
|
18833 |
}else{ |
|
|
18834 |
oomRepeat = (int)integerValue(azArg[++i]); |
|
|
18835 |
} |
|
|
18836 |
}else if( IsDigit(z[0]) ){ |
|
|
18837 |
oomCounter = (int)integerValue(azArg[i]); |
|
|
18838 |
}else{ |
|
|
18839 |
raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]); |
|
|
18840 |
raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n"); |
|
|
18841 |
rc = 1; |
|
|
18842 |
} |
|
|
18843 |
} |
|
|
18844 |
if( rc==0 ){ |
|
|
18845 |
raw_printf(p->out, "oomCounter = %d\n", oomCounter); |
|
|
18846 |
raw_printf(p->out, "oomRepeat = %d\n", oomRepeat); |
|
|
18847 |
} |
|
|
18848 |
}else |
|
|
18849 |
#endif /* SQLITE_DEBUG */ |
|
|
18850 |
|
|
|
18851 |
if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
|
|
18852 |
char *zNewFilename = 0; /* Name of the database file to open */ |
|
|
18853 |
int iName = 1; /* Index in azArg[] of the filename */ |
|
|
18854 |
int newFlag = 0; /* True to delete file before opening */ |
|
|
18855 |
/* Close the existing database */ |
|
|
18856 |
session_close_all(p); |
|
|
18857 |
close_db(p->db); |
|
|
18858 |
p->db = 0; |
|
|
18859 |
p->zDbFilename = 0; |
|
|
18860 |
sqlite3_free(p->zFreeOnClose); |
|
|
18861 |
p->zFreeOnClose = 0; |
|
|
18862 |
p->openMode = SHELL_OPEN_UNSPEC; |
|
|
18863 |
p->openFlags = 0; |
|
|
18864 |
p->szMax = 0; |
|
|
18865 |
/* Check for command-line arguments */ |
|
|
18866 |
for(iName=1; iName |
|
|
18867 |
const char *z = azArg[iName]; |
|
|
18868 |
if( optionMatch(z,"new") ){ |
|
|
18869 |
newFlag = 1; |
|
|
18870 |
#ifdef SQLITE_HAVE_ZLIB |
|
|
18871 |
}else if( optionMatch(z, "zip") ){ |
|
|
18872 |
p->openMode = SHELL_OPEN_ZIPFILE; |
|
|
18873 |
#endif |
|
|
18874 |
}else if( optionMatch(z, "append") ){ |
|
|
18875 |
p->openMode = SHELL_OPEN_APPENDVFS; |
|
|
18876 |
}else if( optionMatch(z, "readonly") ){ |
|
|
18877 |
p->openMode = SHELL_OPEN_READONLY; |
|
|
18878 |
}else if( optionMatch(z, "nofollow") ){ |
|
|
18879 |
p->openFlags |= SQLITE_OPEN_NOFOLLOW; |
|
|
18880 |
#ifdef SQLITE_ENABLE_DESERIALIZE |
|
|
18881 |
}else if( optionMatch(z, "deserialize") ){ |
|
|
18882 |
p->openMode = SHELL_OPEN_DESERIALIZE; |
|
|
18883 |
}else if( optionMatch(z, "hexdb") ){ |
|
|
18884 |
p->openMode = SHELL_OPEN_HEXDB; |
|
|
18885 |
}else if( optionMatch(z, "maxsize") && iName+1 |
|
|
18886 |
p->szMax = integerValue(azArg[++iName]); |
|
|
18887 |
#endif /* SQLITE_ENABLE_DESERIALIZE */ |
|
|
18888 |
}else if( z[0]=='-' ){ |
|
|
18889 |
utf8_printf(stderr, "unknown option: %s\n", z); |
|
|
18890 |
rc = 1; |
|
|
18891 |
goto meta_command_exit; |
|
|
18892 |
}else if( zNewFilename ){ |
|
|
18893 |
utf8_printf(stderr, "extra argument: \"%s\"\n", z); |
|
|
18894 |
rc = 1; |
|
|
18895 |
goto meta_command_exit; |
|
|
18896 |
}else{ |
|
|
18897 |
zNewFilename = sqlite3_mprintf("%s", z); |
|
|
18898 |
} |
|
|
18899 |
} |
|
|
18900 |
/* If a filename is specified, try to open it first */ |
|
|
18901 |
if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){ |
|
|
18902 |
if( newFlag ) shellDeleteFile(zNewFilename); |
|
|
18903 |
p->zDbFilename = zNewFilename; |
|
|
18904 |
open_db(p, OPEN_DB_KEEPALIVE); |
|
|
18905 |
if( p->db==0 ){ |
|
|
18906 |
utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename); |
|
|
18907 |
sqlite3_free(zNewFilename); |
|
|
18908 |
}else{ |
|
|
18909 |
p->zFreeOnClose = zNewFilename; |
|
|
18910 |
} |
|
|
18911 |
} |
|
|
18912 |
if( p->db==0 ){ |
|
|
18913 |
/* As a fall-back open a TEMP database */ |
|
|
18914 |
p->zDbFilename = 0; |
|
|
18915 |
open_db(p, 0); |
|
|
18916 |
} |
|
|
18917 |
}else |
|
|
18918 |
|
|
|
18919 |
if( (c=='o' |
|
|
18920 |
&& (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0)) |
|
|
18921 |
|| (c=='e' && n==5 && strcmp(azArg[0],"excel")==0) |
|
|
18922 |
){ |
|
|
18923 |
char *zFile = 0; |
|
|
18924 |
int bTxtMode = 0; |
|
|
18925 |
int i; |
|
|
18926 |
int eMode = 0; |
|
|
18927 |
int bBOM = 0; |
|
|
18928 |
int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */ |
|
|
18929 |
|
|
|
18930 |
if( c=='e' ){ |
|
|
18931 |
eMode = 'x'; |
|
|
18932 |
bOnce = 2; |
|
|
18933 |
}else if( strncmp(azArg[0],"once",n)==0 ){ |
|
|
18934 |
bOnce = 1; |
|
|
18935 |
} |
|
|
18936 |
for(i=1; i |
|
|
18937 |
char *z = azArg[i]; |
|
|
18938 |
if( z[0]=='-' ){ |
|
|
18939 |
if( z[1]=='-' ) z++; |
|
|
18940 |
if( strcmp(z,"-bom")==0 ){ |
|
|
18941 |
bBOM = 1; |
|
|
18942 |
}else if( c!='e' && strcmp(z,"-x")==0 ){ |
|
|
18943 |
eMode = 'x'; /* spreadsheet */ |
|
|
18944 |
}else if( c!='e' && strcmp(z,"-e")==0 ){ |
|
|
18945 |
eMode = 'e'; /* text editor */ |
|
|
18946 |
}else{ |
|
|
18947 |
utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", |
|
|
18948 |
azArg[i]); |
|
|
18949 |
showHelp(p->out, azArg[0]); |
|
|
18950 |
rc = 1; |
|
|
18951 |
goto meta_command_exit; |
|
|
18952 |
} |
|
|
18953 |
}else if( zFile==0 && eMode!='e' && eMode!='x' ){ |
|
|
18954 |
zFile = sqlite3_mprintf("%s", z); |
|
|
18955 |
if( zFile[0]=='|' ){ |
|
|
18956 |
while( i+1 |
|
|
18957 |
break; |
|
|
18958 |
} |
|
|
18959 |
}else{ |
|
|
18960 |
utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n", |
|
|
18961 |
azArg[i]); |
|
|
18962 |
showHelp(p->out, azArg[0]); |
|
|
18963 |
rc = 1; |
|
|
18964 |
sqlite3_free(zFile); |
|
|
18965 |
goto meta_command_exit; |
|
|
18966 |
} |
|
|
18967 |
} |
|
|
18968 |
if( zFile==0 ) zFile = sqlite3_mprintf("stdout"); |
|
|
18969 |
if( bOnce ){ |
|
|
18970 |
p->outCount = 2; |
|
|
18971 |
}else{ |
|
|
18972 |
p->outCount = 0; |
|
|
18973 |
} |
|
|
18974 |
output_reset(p); |
|
|
18975 |
#ifndef SQLITE_NOHAVE_SYSTEM |
|
|
18976 |
if( eMode=='e' || eMode=='x' ){ |
|
|
18977 |
p->doXdgOpen = 1; |
|
|
18978 |
outputModePush(p); |
|
|
18979 |
if( eMode=='x' ){ |
|
|
18980 |
/* spreadsheet mode. Output as CSV. */ |
|
|
18981 |
newTempFile(p, "csv"); |
|
|
18982 |
ShellClearFlag(p, SHFLG_Echo); |
|
|
18983 |
p->mode = MODE_Csv; |
|
|
18984 |
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
|
|
18985 |
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
|
|
18986 |
}else{ |
|
|
18987 |
/* text editor mode */ |
|
|
18988 |
newTempFile(p, "txt"); |
|
|
18989 |
bTxtMode = 1; |
|
|
18990 |
} |
|
|
18991 |
sqlite3_free(zFile); |
|
|
18992 |
zFile = sqlite3_mprintf("%s", p->zTempFile); |
|
|
18993 |
} |
|
|
18994 |
#endif /* SQLITE_NOHAVE_SYSTEM */ |
|
|
18995 |
if( zFile[0]=='|' ){ |
|
|
18996 |
#ifdef SQLITE_OMIT_POPEN |
|
|
18997 |
raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
|
|
18998 |
rc = 1; |
|
|
18999 |
p->out = stdout; |
|
|
19000 |
#else |
|
|
19001 |
p->out = popen(zFile + 1, "w"); |
|
|
19002 |
if( p->out==0 ){ |
|
|
19003 |
utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
|
|
19004 |
p->out = stdout; |
|
|
19005 |
rc = 1; |
|
|
19006 |
}else{ |
|
|
19007 |
if( bBOM ) fprintf(p->out,"\357\273\277"); |
|
|
19008 |
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
|
|
19009 |
} |
|
|
19010 |
#endif |
|
|
19011 |
}else{ |
|
|
19012 |
p->out = output_file_open(zFile, bTxtMode); |
|
|
19013 |
if( p->out==0 ){ |
|
|
19014 |
if( strcmp(zFile,"off")!=0 ){ |
|
|
19015 |
utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
|
|
19016 |
} |
|
|
19017 |
p->out = stdout; |
|
|
19018 |
rc = 1; |
|
|
19019 |
} else { |
|
|
19020 |
if( bBOM ) fprintf(p->out,"\357\273\277"); |
|
|
19021 |
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
|
|
19022 |
} |
|
|
19023 |
} |
|
|
19024 |
sqlite3_free(zFile); |
|
|
19025 |
}else |
|
|
19026 |
|
|
|
19027 |
if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){ |
|
|
19028 |
open_db(p,0); |
|
|
19029 |
if( nArg<=1 ) goto parameter_syntax_error; |
|
|
19030 |
|
|
|
19031 |
/* .parameter clear |
|
|
19032 |
** Clear all bind parameters by dropping the TEMP table that holds them. |
|
|
19033 |
*/ |
|
|
19034 |
if( nArg==2 && strcmp(azArg[1],"clear")==0 ){ |
|
|
19035 |
sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", |
|
|
19036 |
0, 0, 0); |
|
|
19037 |
}else |
|
|
19038 |
|
|
|
19039 |
/* .parameter list |
|
|
19040 |
** List all bind parameters. |
|
|
19041 |
*/ |
|
|
19042 |
if( nArg==2 && strcmp(azArg[1],"list")==0 ){ |
|
|
19043 |
sqlite3_stmt *pStmt = 0; |
|
|
19044 |
int rx; |
|
|
19045 |
int len = 0; |
|
|
19046 |
rx = sqlite3_prepare_v2(p->db, |
|
|
19047 |
"SELECT max(length(key)) " |
|
|
19048 |
"FROM temp.sqlite_parameters;", -1, &pStmt, 0); |
|
|
19049 |
if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
19050 |
len = sqlite3_column_int(pStmt, 0); |
|
|
19051 |
if( len>40 ) len = 40; |
|
|
19052 |
} |
|
|
19053 |
sqlite3_finalize(pStmt); |
|
|
19054 |
pStmt = 0; |
|
|
19055 |
if( len ){ |
|
|
19056 |
rx = sqlite3_prepare_v2(p->db, |
|
|
19057 |
"SELECT key, quote(value) " |
|
|
19058 |
"FROM temp.sqlite_parameters;", -1, &pStmt, 0); |
|
|
19059 |
while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
19060 |
utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0), |
|
|
19061 |
sqlite3_column_text(pStmt,1)); |
|
|
19062 |
} |
|
|
19063 |
sqlite3_finalize(pStmt); |
|
|
19064 |
} |
|
|
19065 |
}else |
|
|
19066 |
|
|
|
19067 |
/* .parameter init |
|
|
19068 |
** Make sure the TEMP table used to hold bind parameters exists. |
|
|
19069 |
** Create it if necessary. |
|
|
19070 |
*/ |
|
|
19071 |
if( nArg==2 && strcmp(azArg[1],"init")==0 ){ |
|
|
19072 |
bind_table_init(p); |
|
|
19073 |
}else |
|
|
19074 |
|
|
|
19075 |
/* .parameter set NAME VALUE |
|
|
19076 |
** Set or reset a bind parameter. NAME should be the full parameter |
|
|
19077 |
** name exactly as it appears in the query. (ex: $abc, @def). The |
|
|
19078 |
** VALUE can be in either SQL literal notation, or if not it will be |
|
|
19079 |
** understood to be a text string. |
|
|
19080 |
*/ |
|
|
19081 |
if( nArg==4 && strcmp(azArg[1],"set")==0 ){ |
|
|
19082 |
int rx; |
|
|
19083 |
char *zSql; |
|
|
19084 |
sqlite3_stmt *pStmt; |
|
|
19085 |
const char *zKey = azArg[2]; |
|
|
19086 |
const char *zValue = azArg[3]; |
|
|
19087 |
bind_table_init(p); |
|
|
19088 |
zSql = sqlite3_mprintf( |
|
|
19089 |
"REPLACE INTO temp.sqlite_parameters(key,value)" |
|
|
19090 |
"VALUES(%Q,%s);", zKey, zValue); |
|
|
19091 |
if( zSql==0 ) shell_out_of_memory(); |
|
|
19092 |
pStmt = 0; |
|
|
19093 |
rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
|
19094 |
sqlite3_free(zSql); |
|
|
19095 |
if( rx!=SQLITE_OK ){ |
|
|
19096 |
sqlite3_finalize(pStmt); |
|
|
19097 |
pStmt = 0; |
|
|
19098 |
zSql = sqlite3_mprintf( |
|
|
19099 |
"REPLACE INTO temp.sqlite_parameters(key,value)" |
|
|
19100 |
"VALUES(%Q,%Q);", zKey, zValue); |
|
|
19101 |
if( zSql==0 ) shell_out_of_memory(); |
|
|
19102 |
rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
|
19103 |
sqlite3_free(zSql); |
|
|
19104 |
if( rx!=SQLITE_OK ){ |
|
|
19105 |
utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); |
|
|
19106 |
sqlite3_finalize(pStmt); |
|
|
19107 |
pStmt = 0; |
|
|
19108 |
rc = 1; |
|
|
19109 |
} |
|
|
19110 |
} |
|
|
19111 |
sqlite3_step(pStmt); |
|
|
19112 |
sqlite3_finalize(pStmt); |
|
|
19113 |
}else |
|
|
19114 |
|
|
|
19115 |
/* .parameter unset NAME |
|
|
19116 |
** Remove the NAME binding from the parameter binding table, if it |
|
|
19117 |
** exists. |
|
|
19118 |
*/ |
|
|
19119 |
if( nArg==3 && strcmp(azArg[1],"unset")==0 ){ |
|
|
19120 |
char *zSql = sqlite3_mprintf( |
|
|
19121 |
"DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]); |
|
|
19122 |
if( zSql==0 ) shell_out_of_memory(); |
|
|
19123 |
sqlite3_exec(p->db, zSql, 0, 0, 0); |
|
|
19124 |
sqlite3_free(zSql); |
|
|
19125 |
}else |
|
|
19126 |
/* If no command name matches, show a syntax error */ |
|
|
19127 |
parameter_syntax_error: |
|
|
19128 |
showHelp(p->out, "parameter"); |
|
|
19129 |
}else |
|
|
19130 |
|
|
|
19131 |
if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
|
|
19132 |
int i; |
|
|
19133 |
for(i=1; i |
|
|
19134 |
if( i>1 ) raw_printf(p->out, " "); |
|
|
19135 |
utf8_printf(p->out, "%s", azArg[i]); |
|
|
19136 |
} |
|
|
19137 |
raw_printf(p->out, "\n"); |
|
|
19138 |
}else |
|
|
19139 |
|
|
|
19140 |
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
|
|
19141 |
if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){ |
|
|
19142 |
int i; |
|
|
19143 |
int nn = 0; |
|
|
19144 |
p->flgProgress = 0; |
|
|
19145 |
p->mxProgress = 0; |
|
|
19146 |
p->nProgress = 0; |
|
|
19147 |
for(i=1; i |
|
|
19148 |
const char *z = azArg[i]; |
|
|
19149 |
if( z[0]=='-' ){ |
|
|
19150 |
z++; |
|
|
19151 |
if( z[0]=='-' ) z++; |
|
|
19152 |
if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ |
|
|
19153 |
p->flgProgress |= SHELL_PROGRESS_QUIET; |
|
|
19154 |
continue; |
|
|
19155 |
} |
|
|
19156 |
if( strcmp(z,"reset")==0 ){ |
|
|
19157 |
p->flgProgress |= SHELL_PROGRESS_RESET; |
|
|
19158 |
continue; |
|
|
19159 |
} |
|
|
19160 |
if( strcmp(z,"once")==0 ){ |
|
|
19161 |
p->flgProgress |= SHELL_PROGRESS_ONCE; |
|
|
19162 |
continue; |
|
|
19163 |
} |
|
|
19164 |
if( strcmp(z,"limit")==0 ){ |
|
|
19165 |
if( i+1>=nArg ){ |
|
|
19166 |
utf8_printf(stderr, "Error: missing argument on --limit\n"); |
|
|
19167 |
rc = 1; |
|
|
19168 |
goto meta_command_exit; |
|
|
19169 |
}else{ |
|
|
19170 |
p->mxProgress = (int)integerValue(azArg[++i]); |
|
|
19171 |
} |
|
|
19172 |
continue; |
|
|
19173 |
} |
|
|
19174 |
utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]); |
|
|
19175 |
rc = 1; |
|
|
19176 |
goto meta_command_exit; |
|
|
19177 |
}else{ |
|
|
19178 |
nn = (int)integerValue(z); |
|
|
19179 |
} |
|
|
19180 |
} |
|
|
19181 |
open_db(p, 0); |
|
|
19182 |
sqlite3_progress_handler(p->db, nn, progress_handler, p); |
|
|
19183 |
}else |
|
|
19184 |
#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ |
|
|
19185 |
|
|
|
19186 |
if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
|
|
19187 |
if( nArg >= 2) { |
|
|
19188 |
strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
|
|
19189 |
} |
|
|
19190 |
if( nArg >= 3) { |
|
|
19191 |
strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
|
|
19192 |
} |
|
|
19193 |
}else |
|
|
19194 |
|
|
|
19195 |
if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
|
|
19196 |
rc = 2; |
|
|
19197 |
}else |
|
|
19198 |
|
|
|
19199 |
if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
|
|
19200 |
FILE *inSaved = p->in; |
|
|
19201 |
int savedLineno = p->lineno; |
|
|
19202 |
if( nArg!=2 ){ |
|
|
19203 |
raw_printf(stderr, "Usage: .read FILE\n"); |
|
|
19204 |
rc = 1; |
|
|
19205 |
goto meta_command_exit; |
|
|
19206 |
} |
|
|
19207 |
if( azArg[1][0]=='|' ){ |
|
|
19208 |
#ifdef SQLITE_OMIT_POPEN |
|
|
19209 |
raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
|
|
19210 |
rc = 1; |
|
|
19211 |
p->out = stdout; |
|
|
19212 |
#else |
|
|
19213 |
p->in = popen(azArg[1]+1, "r"); |
|
|
19214 |
if( p->in==0 ){ |
|
|
19215 |
utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
|
|
19216 |
rc = 1; |
|
|
19217 |
}else{ |
|
|
19218 |
rc = process_input(p); |
|
|
19219 |
pclose(p->in); |
|
|
19220 |
} |
|
|
19221 |
#endif |
|
|
19222 |
}else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){ |
|
|
19223 |
utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
|
|
19224 |
rc = 1; |
|
|
19225 |
}else{ |
|
|
19226 |
rc = process_input(p); |
|
|
19227 |
fclose(p->in); |
|
|
19228 |
} |
|
|
19229 |
p->in = inSaved; |
|
|
19230 |
p->lineno = savedLineno; |
|
|
19231 |
}else |
|
|
19232 |
|
|
|
19233 |
if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
|
|
19234 |
const char *zSrcFile; |
|
|
19235 |
const char *zDb; |
|
|
19236 |
sqlite3 *pSrc; |
|
|
19237 |
sqlite3_backup *pBackup; |
|
|
19238 |
int nTimeout = 0; |
|
|
19239 |
|
|
|
19240 |
if( nArg==2 ){ |
|
|
19241 |
zSrcFile = azArg[1]; |
|
|
19242 |
zDb = "main"; |
|
|
19243 |
}else if( nArg==3 ){ |
|
|
19244 |
zSrcFile = azArg[2]; |
|
|
19245 |
zDb = azArg[1]; |
|
|
19246 |
}else{ |
|
|
19247 |
raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
|
|
19248 |
rc = 1; |
|
|
19249 |
goto meta_command_exit; |
|
|
19250 |
} |
|
|
19251 |
rc = sqlite3_open(zSrcFile, &pSrc); |
|
|
19252 |
if( rc!=SQLITE_OK ){ |
|
|
19253 |
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
|
|
19254 |
close_db(pSrc); |
|
|
19255 |
return 1; |
|
|
19256 |
} |
|
|
19257 |
open_db(p, 0); |
|
|
19258 |
pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
|
|
19259 |
if( pBackup==0 ){ |
|
|
19260 |
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
|
|
19261 |
close_db(pSrc); |
|
|
19262 |
return 1; |
|
|
19263 |
} |
|
|
19264 |
while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
|
|
19265 |
|| rc==SQLITE_BUSY ){ |
|
|
19266 |
if( rc==SQLITE_BUSY ){ |
|
|
19267 |
if( nTimeout++ >= 3 ) break; |
|
|
19268 |
sqlite3_sleep(100); |
|
|
19269 |
} |
|
|
19270 |
} |
|
|
19271 |
sqlite3_backup_finish(pBackup); |
|
|
19272 |
if( rc==SQLITE_DONE ){ |
|
|
19273 |
rc = 0; |
|
|
19274 |
}else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
|
|
19275 |
raw_printf(stderr, "Error: source database is busy\n"); |
|
|
19276 |
rc = 1; |
|
|
19277 |
}else{ |
|
|
19278 |
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
|
|
19279 |
rc = 1; |
|
|
19280 |
} |
|
|
19281 |
close_db(pSrc); |
|
|
19282 |
}else |
|
|
19283 |
|
|
|
19284 |
if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
|
|
19285 |
if( nArg==2 ){ |
|
|
19286 |
p->scanstatsOn = (u8)booleanValue(azArg[1]); |
|
|
19287 |
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
|
|
19288 |
raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
|
|
19289 |
#endif |
|
|
19290 |
}else{ |
|
|
19291 |
raw_printf(stderr, "Usage: .scanstats on|off\n"); |
|
|
19292 |
rc = 1; |
|
|
19293 |
} |
|
|
19294 |
}else |
|
|
19295 |
|
|
|
19296 |
if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
|
|
19297 |
ShellText sSelect; |
|
|
19298 |
ShellState data; |
|
|
19299 |
char *zErrMsg = 0; |
|
|
19300 |
const char *zDiv = "("; |
|
|
19301 |
const char *zName = 0; |
|
|
19302 |
int iSchema = 0; |
|
|
19303 |
int bDebug = 0; |
|
|
19304 |
int bNoSystemTabs = 0; |
|
|
19305 |
int ii; |
|
|
19306 |
|
|
|
19307 |
open_db(p, 0); |
|
|
19308 |
memcpy(&data, p, sizeof(data)); |
|
|
19309 |
data.showHeader = 0; |
|
|
19310 |
data.cMode = data.mode = MODE_Semi; |
|
|
19311 |
initText(&sSelect); |
|
|
19312 |
for(ii=1; ii |
|
|
19313 |
if( optionMatch(azArg[ii],"indent") ){ |
|
|
19314 |
data.cMode = data.mode = MODE_Pretty; |
|
|
19315 |
}else if( optionMatch(azArg[ii],"debug") ){ |
|
|
19316 |
bDebug = 1; |
|
|
19317 |
}else if( optionMatch(azArg[ii],"nosys") ){ |
|
|
19318 |
bNoSystemTabs = 1; |
|
|
19319 |
}else if( azArg[ii][0]=='-' ){ |
|
|
19320 |
utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]); |
|
|
19321 |
rc = 1; |
|
|
19322 |
goto meta_command_exit; |
|
|
19323 |
}else if( zName==0 ){ |
|
|
19324 |
zName = azArg[ii]; |
|
|
19325 |
}else{ |
|
|
19326 |
raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n"); |
|
|
19327 |
rc = 1; |
|
|
19328 |
goto meta_command_exit; |
|
|
19329 |
} |
|
|
19330 |
} |
|
|
19331 |
if( zName!=0 ){ |
|
|
19332 |
int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0 |
|
|
19333 |
|| sqlite3_strlike(zName, "sqlite_schema", '\\')==0 |
|
|
19334 |
|| sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 |
|
|
19335 |
|| sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0; |
|
|
19336 |
if( isSchema ){ |
|
|
19337 |
char *new_argv[2], *new_colv[2]; |
|
|
19338 |
new_argv[0] = sqlite3_mprintf( |
|
|
19339 |
"CREATE TABLE %s (\n" |
|
|
19340 |
" type text,\n" |
|
|
19341 |
" name text,\n" |
|
|
19342 |
" tbl_name text,\n" |
|
|
19343 |
" rootpage integer,\n" |
|
|
19344 |
" sql text\n" |
|
|
19345 |
")", zName); |
|
|
19346 |
new_argv[1] = 0; |
|
|
19347 |
new_colv[0] = "sql"; |
|
|
19348 |
new_colv[1] = 0; |
|
|
19349 |
callback(&data, 1, new_argv, new_colv); |
|
|
19350 |
sqlite3_free(new_argv[0]); |
|
|
19351 |
} |
|
|
19352 |
} |
|
|
19353 |
if( zDiv ){ |
|
|
19354 |
sqlite3_stmt *pStmt = 0; |
|
|
19355 |
rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", |
|
|
19356 |
-1, &pStmt, 0); |
|
|
19357 |
if( rc ){ |
|
|
19358 |
utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
|
|
19359 |
sqlite3_finalize(pStmt); |
|
|
19360 |
rc = 1; |
|
|
19361 |
goto meta_command_exit; |
|
|
19362 |
} |
|
|
19363 |
appendText(&sSelect, "SELECT sql FROM", 0); |
|
|
19364 |
iSchema = 0; |
|
|
19365 |
while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
19366 |
const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); |
|
|
19367 |
char zScNum[30]; |
|
|
19368 |
sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); |
|
|
19369 |
appendText(&sSelect, zDiv, 0); |
|
|
19370 |
zDiv = " UNION ALL "; |
|
|
19371 |
appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); |
|
|
19372 |
if( sqlite3_stricmp(zDb, "main")!=0 ){ |
|
|
19373 |
appendText(&sSelect, zDb, '\''); |
|
|
19374 |
}else{ |
|
|
19375 |
appendText(&sSelect, "NULL", 0); |
|
|
19376 |
} |
|
|
19377 |
appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); |
|
|
19378 |
appendText(&sSelect, zScNum, 0); |
|
|
19379 |
appendText(&sSelect, " AS snum, ", 0); |
|
|
19380 |
appendText(&sSelect, zDb, '\''); |
|
|
19381 |
appendText(&sSelect, " AS sname FROM ", 0); |
|
|
19382 |
appendText(&sSelect, zDb, quoteChar(zDb)); |
|
|
19383 |
appendText(&sSelect, ".sqlite_schema", 0); |
|
|
19384 |
} |
|
|
19385 |
sqlite3_finalize(pStmt); |
|
|
19386 |
#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS |
|
|
19387 |
if( zName ){ |
|
|
19388 |
appendText(&sSelect, |
|
|
19389 |
" UNION ALL SELECT shell_module_schema(name)," |
|
|
19390 |
" 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", |
|
|
19391 |
0); |
|
|
19392 |
} |
|
|
19393 |
#endif |
|
|
19394 |
appendText(&sSelect, ") WHERE ", 0); |
|
|
19395 |
if( zName ){ |
|
|
19396 |
char *zQarg = sqlite3_mprintf("%Q", zName); |
|
|
19397 |
int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 || |
|
|
19398 |
strchr(zName, '[') != 0; |
|
|
19399 |
if( strchr(zName, '.') ){ |
|
|
19400 |
appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); |
|
|
19401 |
}else{ |
|
|
19402 |
appendText(&sSelect, "lower(tbl_name)", 0); |
|
|
19403 |
} |
|
|
19404 |
appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0); |
|
|
19405 |
appendText(&sSelect, zQarg, 0); |
|
|
19406 |
if( !bGlob ){ |
|
|
19407 |
appendText(&sSelect, " ESCAPE '\\' ", 0); |
|
|
19408 |
} |
|
|
19409 |
appendText(&sSelect, " AND ", 0); |
|
|
19410 |
sqlite3_free(zQarg); |
|
|
19411 |
} |
|
|
19412 |
if( bNoSystemTabs ){ |
|
|
19413 |
appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0); |
|
|
19414 |
} |
|
|
19415 |
appendText(&sSelect, "sql IS NOT NULL" |
|
|
19416 |
" ORDER BY snum, rowid", 0); |
|
|
19417 |
if( bDebug ){ |
|
|
19418 |
utf8_printf(p->out, "SQL: %s;\n", sSelect.z); |
|
|
19419 |
}else{ |
|
|
19420 |
rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); |
|
|
19421 |
} |
|
|
19422 |
freeText(&sSelect); |
|
|
19423 |
} |
|
|
19424 |
if( zErrMsg ){ |
|
|
19425 |
utf8_printf(stderr,"Error: %s\n", zErrMsg); |
|
|
19426 |
sqlite3_free(zErrMsg); |
|
|
19427 |
rc = 1; |
|
|
19428 |
}else if( rc != SQLITE_OK ){ |
|
|
19429 |
raw_printf(stderr,"Error: querying schema information\n"); |
|
|
19430 |
rc = 1; |
|
|
19431 |
}else{ |
|
|
19432 |
rc = 0; |
|
|
19433 |
} |
|
|
19434 |
}else |
|
|
19435 |
|
|
|
19436 |
if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
|
|
19437 |
unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff; |
|
|
19438 |
sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x); |
|
|
19439 |
}else |
|
|
19440 |
|
|
|
19441 |
#if defined(SQLITE_ENABLE_SESSION) |
|
|
19442 |
if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
|
|
19443 |
OpenSession *pSession = &p->aSession[0]; |
|
|
19444 |
char **azCmd = &azArg[1]; |
|
|
19445 |
int iSes = 0; |
|
|
19446 |
int nCmd = nArg - 1; |
|
|
19447 |
int i; |
|
|
19448 |
if( nArg<=1 ) goto session_syntax_error; |
|
|
19449 |
open_db(p, 0); |
|
|
19450 |
if( nArg>=3 ){ |
|
|
19451 |
for(iSes=0; iSesnSession; iSes++){ |
|
|
19452 |
if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
|
|
19453 |
} |
|
|
19454 |
if( iSesnSession ){ |
|
|
19455 |
pSession = &p->aSession[iSes]; |
|
|
19456 |
azCmd++; |
|
|
19457 |
nCmd--; |
|
|
19458 |
}else{ |
|
|
19459 |
pSession = &p->aSession[0]; |
|
|
19460 |
iSes = 0; |
|
|
19461 |
} |
|
|
19462 |
} |
|
|
19463 |
|
|
|
19464 |
/* .session attach TABLE |
|
|
19465 |
** Invoke the sqlite3session_attach() interface to attach a particular |
|
|
19466 |
** table so that it is never filtered. |
|
|
19467 |
*/ |
|
|
19468 |
if( strcmp(azCmd[0],"attach")==0 ){ |
|
|
19469 |
if( nCmd!=2 ) goto session_syntax_error; |
|
|
19470 |
if( pSession->p==0 ){ |
|
|
19471 |
session_not_open: |
|
|
19472 |
raw_printf(stderr, "ERROR: No sessions are open\n"); |
|
|
19473 |
}else{ |
|
|
19474 |
rc = sqlite3session_attach(pSession->p, azCmd[1]); |
|
|
19475 |
if( rc ){ |
|
|
19476 |
raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
|
|
19477 |
rc = 0; |
|
|
19478 |
} |
|
|
19479 |
} |
|
|
19480 |
}else |
|
|
19481 |
|
|
|
19482 |
/* .session changeset FILE |
|
|
19483 |
** .session patchset FILE |
|
|
19484 |
** Write a changeset or patchset into a file. The file is overwritten. |
|
|
19485 |
*/ |
|
|
19486 |
if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
|
|
19487 |
FILE *out = 0; |
|
|
19488 |
if( nCmd!=2 ) goto session_syntax_error; |
|
|
19489 |
if( pSession->p==0 ) goto session_not_open; |
|
|
19490 |
out = fopen(azCmd[1], "wb"); |
|
|
19491 |
if( out==0 ){ |
|
|
19492 |
utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", |
|
|
19493 |
azCmd[1]); |
|
|
19494 |
}else{ |
|
|
19495 |
int szChng; |
|
|
19496 |
void *pChng; |
|
|
19497 |
if( azCmd[0][0]=='c' ){ |
|
|
19498 |
rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
|
|
19499 |
}else{ |
|
|
19500 |
rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
|
|
19501 |
} |
|
|
19502 |
if( rc ){ |
|
|
19503 |
printf("Error: error code %d\n", rc); |
|
|
19504 |
rc = 0; |
|
|
19505 |
} |
|
|
19506 |
if( pChng |
|
|
19507 |
&& fwrite(pChng, szChng, 1, out)!=1 ){ |
|
|
19508 |
raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
|
|
19509 |
szChng); |
|
|
19510 |
} |
|
|
19511 |
sqlite3_free(pChng); |
|
|
19512 |
fclose(out); |
|
|
19513 |
} |
|
|
19514 |
}else |
|
|
19515 |
|
|
|
19516 |
/* .session close |
|
|
19517 |
** Close the identified session |
|
|
19518 |
*/ |
|
|
19519 |
if( strcmp(azCmd[0], "close")==0 ){ |
|
|
19520 |
if( nCmd!=1 ) goto session_syntax_error; |
|
|
19521 |
if( p->nSession ){ |
|
|
19522 |
session_close(pSession); |
|
|
19523 |
p->aSession[iSes] = p->aSession[--p->nSession]; |
|
|
19524 |
} |
|
|
19525 |
}else |
|
|
19526 |
|
|
|
19527 |
/* .session enable ?BOOLEAN? |
|
|
19528 |
** Query or set the enable flag |
|
|
19529 |
*/ |
|
|
19530 |
if( strcmp(azCmd[0], "enable")==0 ){ |
|
|
19531 |
int ii; |
|
|
19532 |
if( nCmd>2 ) goto session_syntax_error; |
|
|
19533 |
ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
|
|
19534 |
if( p->nSession ){ |
|
|
19535 |
ii = sqlite3session_enable(pSession->p, ii); |
|
|
19536 |
utf8_printf(p->out, "session %s enable flag = %d\n", |
|
|
19537 |
pSession->zName, ii); |
|
|
19538 |
} |
|
|
19539 |
}else |
|
|
19540 |
|
|
|
19541 |
/* .session filter GLOB .... |
|
|
19542 |
** Set a list of GLOB patterns of table names to be excluded. |
|
|
19543 |
*/ |
|
|
19544 |
if( strcmp(azCmd[0], "filter")==0 ){ |
|
|
19545 |
int ii, nByte; |
|
|
19546 |
if( nCmd<2 ) goto session_syntax_error; |
|
|
19547 |
if( p->nSession ){ |
|
|
19548 |
for(ii=0; iinFilter; ii++){ |
|
|
19549 |
sqlite3_free(pSession->azFilter[ii]); |
|
|
19550 |
} |
|
|
19551 |
sqlite3_free(pSession->azFilter); |
|
|
19552 |
nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
|
|
19553 |
pSession->azFilter = sqlite3_malloc( nByte ); |
|
|
19554 |
if( pSession->azFilter==0 ){ |
|
|
19555 |
raw_printf(stderr, "Error: out or memory\n"); |
|
|
19556 |
exit(1); |
|
|
19557 |
} |
|
|
19558 |
for(ii=1; ii |
|
|
19559 |
pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
|
|
19560 |
} |
|
|
19561 |
pSession->nFilter = ii-1; |
|
|
19562 |
} |
|
|
19563 |
}else |
|
|
19564 |
|
|
|
19565 |
/* .session indirect ?BOOLEAN? |
|
|
19566 |
** Query or set the indirect flag |
|
|
19567 |
*/ |
|
|
19568 |
if( strcmp(azCmd[0], "indirect")==0 ){ |
|
|
19569 |
int ii; |
|
|
19570 |
if( nCmd>2 ) goto session_syntax_error; |
|
|
19571 |
ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
|
|
19572 |
if( p->nSession ){ |
|
|
19573 |
ii = sqlite3session_indirect(pSession->p, ii); |
|
|
19574 |
utf8_printf(p->out, "session %s indirect flag = %d\n", |
|
|
19575 |
pSession->zName, ii); |
|
|
19576 |
} |
|
|
19577 |
}else |
|
|
19578 |
|
|
|
19579 |
/* .session isempty |
|
|
19580 |
** Determine if the session is empty |
|
|
19581 |
*/ |
|
|
19582 |
if( strcmp(azCmd[0], "isempty")==0 ){ |
|
|
19583 |
int ii; |
|
|
19584 |
if( nCmd!=1 ) goto session_syntax_error; |
|
|
19585 |
if( p->nSession ){ |
|
|
19586 |
ii = sqlite3session_isempty(pSession->p); |
|
|
19587 |
utf8_printf(p->out, "session %s isempty flag = %d\n", |
|
|
19588 |
pSession->zName, ii); |
|
|
19589 |
} |
|
|
19590 |
}else |
|
|
19591 |
|
|
|
19592 |
/* .session list |
|
|
19593 |
** List all currently open sessions |
|
|
19594 |
*/ |
|
|
19595 |
if( strcmp(azCmd[0],"list")==0 ){ |
|
|
19596 |
for(i=0; inSession; i++){ |
|
|
19597 |
utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
|
|
19598 |
} |
|
|
19599 |
}else |
|
|
19600 |
|
|
|
19601 |
/* .session open DB NAME |
|
|
19602 |
** Open a new session called NAME on the attached database DB. |
|
|
19603 |
** DB is normally "main". |
|
|
19604 |
*/ |
|
|
19605 |
if( strcmp(azCmd[0],"open")==0 ){ |
|
|
19606 |
char *zName; |
|
|
19607 |
if( nCmd!=3 ) goto session_syntax_error; |
|
|
19608 |
zName = azCmd[2]; |
|
|
19609 |
if( zName[0]==0 ) goto session_syntax_error; |
|
|
19610 |
for(i=0; inSession; i++){ |
|
|
19611 |
if( strcmp(p->aSession[i].zName,zName)==0 ){ |
|
|
19612 |
utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
|
|
19613 |
goto meta_command_exit; |
|
|
19614 |
} |
|
|
19615 |
} |
|
|
19616 |
if( p->nSession>=ArraySize(p->aSession) ){ |
|
|
19617 |
raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
|
|
19618 |
goto meta_command_exit; |
|
|
19619 |
} |
|
|
19620 |
pSession = &p->aSession[p->nSession]; |
|
|
19621 |
rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
|
|
19622 |
if( rc ){ |
|
|
19623 |
raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
|
|
19624 |
rc = 0; |
|
|
19625 |
goto meta_command_exit; |
|
|
19626 |
} |
|
|
19627 |
pSession->nFilter = 0; |
|
|
19628 |
sqlite3session_table_filter(pSession->p, session_filter, pSession); |
|
|
19629 |
p->nSession++; |
|
|
19630 |
pSession->zName = sqlite3_mprintf("%s", zName); |
|
|
19631 |
}else |
|
|
19632 |
/* If no command name matches, show a syntax error */ |
|
|
19633 |
session_syntax_error: |
|
|
19634 |
showHelp(p->out, "session"); |
|
|
19635 |
}else |
|
|
19636 |
#endif |
|
|
19637 |
|
|
|
19638 |
#ifdef SQLITE_DEBUG |
|
|
19639 |
/* Undocumented commands for internal testing. Subject to change |
|
|
19640 |
** without notice. */ |
|
|
19641 |
if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
|
|
19642 |
if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
|
|
19643 |
int i, v; |
|
|
19644 |
for(i=1; i |
|
|
19645 |
v = booleanValue(azArg[i]); |
|
|
19646 |
utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
|
|
19647 |
} |
|
|
19648 |
} |
|
|
19649 |
if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
|
|
19650 |
int i; sqlite3_int64 v; |
|
|
19651 |
for(i=1; i |
|
|
19652 |
char zBuf[200]; |
|
|
19653 |
v = integerValue(azArg[i]); |
|
|
19654 |
sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); |
|
|
19655 |
utf8_printf(p->out, "%s", zBuf); |
|
|
19656 |
} |
|
|
19657 |
} |
|
|
19658 |
}else |
|
|
19659 |
#endif |
|
|
19660 |
|
|
|
19661 |
if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){ |
|
|
19662 |
int bIsInit = 0; /* True to initialize the SELFTEST table */ |
|
|
19663 |
int bVerbose = 0; /* Verbose output */ |
|
|
19664 |
int bSelftestExists; /* True if SELFTEST already exists */ |
|
|
19665 |
int i, k; /* Loop counters */ |
|
|
19666 |
int nTest = 0; /* Number of tests runs */ |
|
|
19667 |
int nErr = 0; /* Number of errors seen */ |
|
|
19668 |
ShellText str; /* Answer for a query */ |
|
|
19669 |
sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ |
|
|
19670 |
|
|
|
19671 |
open_db(p,0); |
|
|
19672 |
for(i=1; i |
|
|
19673 |
const char *z = azArg[i]; |
|
|
19674 |
if( z[0]=='-' && z[1]=='-' ) z++; |
|
|
19675 |
if( strcmp(z,"-init")==0 ){ |
|
|
19676 |
bIsInit = 1; |
|
|
19677 |
}else |
|
|
19678 |
if( strcmp(z,"-v")==0 ){ |
|
|
19679 |
bVerbose++; |
|
|
19680 |
}else |
|
|
19681 |
{ |
|
|
19682 |
utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
|
|
19683 |
azArg[i], azArg[0]); |
|
|
19684 |
raw_printf(stderr, "Should be one of: --init -v\n"); |
|
|
19685 |
rc = 1; |
|
|
19686 |
goto meta_command_exit; |
|
|
19687 |
} |
|
|
19688 |
} |
|
|
19689 |
if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) |
|
|
19690 |
!= SQLITE_OK ){ |
|
|
19691 |
bSelftestExists = 0; |
|
|
19692 |
}else{ |
|
|
19693 |
bSelftestExists = 1; |
|
|
19694 |
} |
|
|
19695 |
if( bIsInit ){ |
|
|
19696 |
createSelftestTable(p); |
|
|
19697 |
bSelftestExists = 1; |
|
|
19698 |
} |
|
|
19699 |
initText(&str); |
|
|
19700 |
appendText(&str, "x", 0); |
|
|
19701 |
for(k=bSelftestExists; k>=0; k--){ |
|
|
19702 |
if( k==1 ){ |
|
|
19703 |
rc = sqlite3_prepare_v2(p->db, |
|
|
19704 |
"SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", |
|
|
19705 |
-1, &pStmt, 0); |
|
|
19706 |
}else{ |
|
|
19707 |
rc = sqlite3_prepare_v2(p->db, |
|
|
19708 |
"VALUES(0,'memo','Missing SELFTEST table - default checks only','')," |
|
|
19709 |
" (1,'run','PRAGMA integrity_check','ok')", |
|
|
19710 |
-1, &pStmt, 0); |
|
|
19711 |
} |
|
|
19712 |
if( rc ){ |
|
|
19713 |
raw_printf(stderr, "Error querying the selftest table\n"); |
|
|
19714 |
rc = 1; |
|
|
19715 |
sqlite3_finalize(pStmt); |
|
|
19716 |
goto meta_command_exit; |
|
|
19717 |
} |
|
|
19718 |
for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ |
|
|
19719 |
int tno = sqlite3_column_int(pStmt, 0); |
|
|
19720 |
const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); |
|
|
19721 |
const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); |
|
|
19722 |
const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); |
|
|
19723 |
|
|
|
19724 |
k = 0; |
|
|
19725 |
if( bVerbose>0 ){ |
|
|
19726 |
char *zQuote = sqlite3_mprintf("%q", zSql); |
|
|
19727 |
printf("%d: %s %s\n", tno, zOp, zSql); |
|
|
19728 |
sqlite3_free(zQuote); |
|
|
19729 |
} |
|
|
19730 |
if( strcmp(zOp,"memo")==0 ){ |
|
|
19731 |
utf8_printf(p->out, "%s\n", zSql); |
|
|
19732 |
}else |
|
|
19733 |
if( strcmp(zOp,"run")==0 ){ |
|
|
19734 |
char *zErrMsg = 0; |
|
|
19735 |
str.n = 0; |
|
|
19736 |
str.z[0] = 0; |
|
|
19737 |
rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); |
|
|
19738 |
nTest++; |
|
|
19739 |
if( bVerbose ){ |
|
|
19740 |
utf8_printf(p->out, "Result: %s\n", str.z); |
|
|
19741 |
} |
|
|
19742 |
if( rc || zErrMsg ){ |
|
|
19743 |
nErr++; |
|
|
19744 |
rc = 1; |
|
|
19745 |
utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); |
|
|
19746 |
sqlite3_free(zErrMsg); |
|
|
19747 |
}else if( strcmp(zAns,str.z)!=0 ){ |
|
|
19748 |
nErr++; |
|
|
19749 |
rc = 1; |
|
|
19750 |
utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); |
|
|
19751 |
utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); |
|
|
19752 |
} |
|
|
19753 |
}else |
|
|
19754 |
{ |
|
|
19755 |
utf8_printf(stderr, |
|
|
19756 |
"Unknown operation \"%s\" on selftest line %d\n", zOp, tno); |
|
|
19757 |
rc = 1; |
|
|
19758 |
break; |
|
|
19759 |
} |
|
|
19760 |
} /* End loop over rows of content from SELFTEST */ |
|
|
19761 |
sqlite3_finalize(pStmt); |
|
|
19762 |
} /* End loop over k */ |
|
|
19763 |
freeText(&str); |
|
|
19764 |
utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); |
|
|
19765 |
}else |
|
|
19766 |
|
|
|
19767 |
if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
|
|
19768 |
if( nArg<2 || nArg>3 ){ |
|
|
19769 |
raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
|
|
19770 |
rc = 1; |
|
|
19771 |
} |
|
|
19772 |
if( nArg>=2 ){ |
|
|
19773 |
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
|
|
19774 |
"%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
|
|
19775 |
} |
|
|
19776 |
if( nArg>=3 ){ |
|
|
19777 |
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
|
|
19778 |
"%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
|
|
19779 |
} |
|
|
19780 |
}else |
|
|
19781 |
|
|
|
19782 |
if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){ |
|
|
19783 |
const char *zLike = 0; /* Which table to checksum. 0 means everything */ |
|
|
19784 |
int i; /* Loop counter */ |
|
|
19785 |
int bSchema = 0; /* Also hash the schema */ |
|
|
19786 |
int bSeparate = 0; /* Hash each table separately */ |
|
|
19787 |
int iSize = 224; /* Hash algorithm to use */ |
|
|
19788 |
int bDebug = 0; /* Only show the query that would have run */ |
|
|
19789 |
sqlite3_stmt *pStmt; /* For querying tables names */ |
|
|
19790 |
char *zSql; /* SQL to be run */ |
|
|
19791 |
char *zSep; /* Separator */ |
|
|
19792 |
ShellText sSql; /* Complete SQL for the query to run the hash */ |
|
|
19793 |
ShellText sQuery; /* Set of queries used to read all content */ |
|
|
19794 |
open_db(p, 0); |
|
|
19795 |
for(i=1; i |
|
|
19796 |
const char *z = azArg[i]; |
|
|
19797 |
if( z[0]=='-' ){ |
|
|
19798 |
z++; |
|
|
19799 |
if( z[0]=='-' ) z++; |
|
|
19800 |
if( strcmp(z,"schema")==0 ){ |
|
|
19801 |
bSchema = 1; |
|
|
19802 |
}else |
|
|
19803 |
if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 |
|
|
19804 |
|| strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 |
|
|
19805 |
){ |
|
|
19806 |
iSize = atoi(&z[5]); |
|
|
19807 |
}else |
|
|
19808 |
if( strcmp(z,"debug")==0 ){ |
|
|
19809 |
bDebug = 1; |
|
|
19810 |
}else |
|
|
19811 |
{ |
|
|
19812 |
utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n", |
|
|
19813 |
azArg[i], azArg[0]); |
|
|
19814 |
showHelp(p->out, azArg[0]); |
|
|
19815 |
rc = 1; |
|
|
19816 |
goto meta_command_exit; |
|
|
19817 |
} |
|
|
19818 |
}else if( zLike ){ |
|
|
19819 |
raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); |
|
|
19820 |
rc = 1; |
|
|
19821 |
goto meta_command_exit; |
|
|
19822 |
}else{ |
|
|
19823 |
zLike = z; |
|
|
19824 |
bSeparate = 1; |
|
|
19825 |
if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1; |
|
|
19826 |
} |
|
|
19827 |
} |
|
|
19828 |
if( bSchema ){ |
|
|
19829 |
zSql = "SELECT lower(name) FROM sqlite_schema" |
|
|
19830 |
" WHERE type='table' AND coalesce(rootpage,0)>1" |
|
|
19831 |
" UNION ALL SELECT 'sqlite_schema'" |
|
|
19832 |
" ORDER BY 1 collate nocase"; |
|
|
19833 |
}else{ |
|
|
19834 |
zSql = "SELECT lower(name) FROM sqlite_schema" |
|
|
19835 |
" WHERE type='table' AND coalesce(rootpage,0)>1" |
|
|
19836 |
" AND name NOT LIKE 'sqlite_%'" |
|
|
19837 |
" ORDER BY 1 collate nocase"; |
|
|
19838 |
} |
|
|
19839 |
sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
|
19840 |
initText(&sQuery); |
|
|
19841 |
initText(&sSql); |
|
|
19842 |
appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); |
|
|
19843 |
zSep = "VALUES("; |
|
|
19844 |
while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
|
|
19845 |
const char *zTab = (const char*)sqlite3_column_text(pStmt,0); |
|
|
19846 |
if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; |
|
|
19847 |
if( strncmp(zTab, "sqlite_",7)!=0 ){ |
|
|
19848 |
appendText(&sQuery,"SELECT * FROM ", 0); |
|
|
19849 |
appendText(&sQuery,zTab,'"'); |
|
|
19850 |
appendText(&sQuery," NOT INDEXED;", 0); |
|
|
19851 |
}else if( strcmp(zTab, "sqlite_schema")==0 ){ |
|
|
19852 |
appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema" |
|
|
19853 |
" ORDER BY name;", 0); |
|
|
19854 |
}else if( strcmp(zTab, "sqlite_sequence")==0 ){ |
|
|
19855 |
appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" |
|
|
19856 |
" ORDER BY name;", 0); |
|
|
19857 |
}else if( strcmp(zTab, "sqlite_stat1")==0 ){ |
|
|
19858 |
appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" |
|
|
19859 |
" ORDER BY tbl,idx;", 0); |
|
|
19860 |
}else if( strcmp(zTab, "sqlite_stat4")==0 ){ |
|
|
19861 |
appendText(&sQuery, "SELECT * FROM ", 0); |
|
|
19862 |
appendText(&sQuery, zTab, 0); |
|
|
19863 |
appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); |
|
|
19864 |
} |
|
|
19865 |
appendText(&sSql, zSep, 0); |
|
|
19866 |
appendText(&sSql, sQuery.z, '\''); |
|
|
19867 |
sQuery.n = 0; |
|
|
19868 |
appendText(&sSql, ",", 0); |
|
|
19869 |
appendText(&sSql, zTab, '\''); |
|
|
19870 |
zSep = "),("; |
|
|
19871 |
} |
|
|
19872 |
sqlite3_finalize(pStmt); |
|
|
19873 |
if( bSeparate ){ |
|
|
19874 |
zSql = sqlite3_mprintf( |
|
|
19875 |
"%s))" |
|
|
19876 |
" SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" |
|
|
19877 |
" FROM [sha3sum$query]", |
|
|
19878 |
sSql.z, iSize); |
|
|
19879 |
}else{ |
|
|
19880 |
zSql = sqlite3_mprintf( |
|
|
19881 |
"%s))" |
|
|
19882 |
" SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" |
|
|
19883 |
" FROM [sha3sum$query]", |
|
|
19884 |
sSql.z, iSize); |
|
|
19885 |
} |
|
|
19886 |
freeText(&sQuery); |
|
|
19887 |
freeText(&sSql); |
|
|
19888 |
if( bDebug ){ |
|
|
19889 |
utf8_printf(p->out, "%s\n", zSql); |
|
|
19890 |
}else{ |
|
|
19891 |
shell_exec(p, zSql, 0); |
|
|
19892 |
} |
|
|
19893 |
sqlite3_free(zSql); |
|
|
19894 |
}else |
|
|
19895 |
|
|
|
19896 |
#ifndef SQLITE_NOHAVE_SYSTEM |
|
|
19897 |
if( c=='s' |
|
|
19898 |
&& (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
|
|
19899 |
){ |
|
|
19900 |
char *zCmd; |
|
|
19901 |
int i, x; |
|
|
19902 |
if( nArg<2 ){ |
|
|
19903 |
raw_printf(stderr, "Usage: .system COMMAND\n"); |
|
|
19904 |
rc = 1; |
|
|
19905 |
goto meta_command_exit; |
|
|
19906 |
} |
|
|
19907 |
zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
|
|
19908 |
for(i=2; i |
|
|
19909 |
zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
|
|
19910 |
zCmd, azArg[i]); |
|
|
19911 |
} |
|
|
19912 |
x = system(zCmd); |
|
|
19913 |
sqlite3_free(zCmd); |
|
|
19914 |
if( x ) raw_printf(stderr, "System command returns %d\n", x); |
|
|
19915 |
}else |
|
|
19916 |
#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ |
|
|
19917 |
|
|
|
19918 |
if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
|
|
19919 |
static const char *azBool[] = { "off", "on", "trigger", "full"}; |
|
|
19920 |
const char *zOut; |
|
|
19921 |
int i; |
|
|
19922 |
if( nArg!=1 ){ |
|
|
19923 |
raw_printf(stderr, "Usage: .show\n"); |
|
|
19924 |
rc = 1; |
|
|
19925 |
goto meta_command_exit; |
|
|
19926 |
} |
|
|
19927 |
utf8_printf(p->out, "%12.12s: %s\n","echo", |
|
|
19928 |
azBool[ShellHasFlag(p, SHFLG_Echo)]); |
|
|
19929 |
utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
|
|
19930 |
utf8_printf(p->out, "%12.12s: %s\n","explain", |
|
|
19931 |
p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
|
|
19932 |
utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
|
|
19933 |
utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
|
|
19934 |
utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
|
|
19935 |
output_c_string(p->out, p->nullValue); |
|
|
19936 |
raw_printf(p->out, "\n"); |
|
|
19937 |
utf8_printf(p->out,"%12.12s: %s\n","output", |
|
|
19938 |
strlen30(p->outfile) ? p->outfile : "stdout"); |
|
|
19939 |
utf8_printf(p->out,"%12.12s: ", "colseparator"); |
|
|
19940 |
output_c_string(p->out, p->colSeparator); |
|
|
19941 |
raw_printf(p->out, "\n"); |
|
|
19942 |
utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
|
|
19943 |
output_c_string(p->out, p->rowSeparator); |
|
|
19944 |
raw_printf(p->out, "\n"); |
|
|
19945 |
switch( p->statsOn ){ |
|
|
19946 |
case 0: zOut = "off"; break; |
|
|
19947 |
default: zOut = "on"; break; |
|
|
19948 |
case 2: zOut = "stmt"; break; |
|
|
19949 |
case 3: zOut = "vmstep"; break; |
|
|
19950 |
} |
|
|
19951 |
utf8_printf(p->out, "%12.12s: %s\n","stats", zOut); |
|
|
19952 |
utf8_printf(p->out, "%12.12s: ", "width"); |
|
|
19953 |
for (i=0;inWidth;i++) { |
|
|
19954 |
raw_printf(p->out, "%d ", p->colWidth[i]); |
|
|
19955 |
} |
|
|
19956 |
raw_printf(p->out, "\n"); |
|
|
19957 |
utf8_printf(p->out, "%12.12s: %s\n", "filename", |
|
|
19958 |
p->zDbFilename ? p->zDbFilename : ""); |
|
|
19959 |
}else |
|
|
19960 |
|
|
|
19961 |
if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
|
|
19962 |
if( nArg==2 ){ |
|
|
19963 |
if( strcmp(azArg[1],"stmt")==0 ){ |
|
|
19964 |
p->statsOn = 2; |
|
|
19965 |
}else if( strcmp(azArg[1],"vmstep")==0 ){ |
|
|
19966 |
p->statsOn = 3; |
|
|
19967 |
}else{ |
|
|
19968 |
p->statsOn = (u8)booleanValue(azArg[1]); |
|
|
19969 |
} |
|
|
19970 |
}else if( nArg==1 ){ |
|
|
19971 |
display_stats(p->db, p, 0); |
|
|
19972 |
}else{ |
|
|
19973 |
raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n"); |
|
|
19974 |
rc = 1; |
|
|
19975 |
} |
|
|
19976 |
}else |
|
|
19977 |
|
|
|
19978 |
if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0) |
|
|
19979 |
|| (c=='i' && (strncmp(azArg[0], "indices", n)==0 |
|
|
19980 |
|| strncmp(azArg[0], "indexes", n)==0) ) |
|
|
19981 |
){ |
|
|
19982 |
sqlite3_stmt *pStmt; |
|
|
19983 |
char **azResult; |
|
|
19984 |
int nRow, nAlloc; |
|
|
19985 |
int ii; |
|
|
19986 |
ShellText s; |
|
|
19987 |
initText(&s); |
|
|
19988 |
open_db(p, 0); |
|
|
19989 |
rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
|
|
19990 |
if( rc ){ |
|
|
19991 |
sqlite3_finalize(pStmt); |
|
|
19992 |
return shellDatabaseError(p->db); |
|
|
19993 |
} |
|
|
19994 |
|
|
|
19995 |
if( nArg>2 && c=='i' ){ |
|
|
19996 |
/* It is an historical accident that the .indexes command shows an error |
|
|
19997 |
** when called with the wrong number of arguments whereas the .tables |
|
|
19998 |
** command does not. */ |
|
|
19999 |
raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
|
|
20000 |
rc = 1; |
|
|
20001 |
sqlite3_finalize(pStmt); |
|
|
20002 |
goto meta_command_exit; |
|
|
20003 |
} |
|
|
20004 |
for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ |
|
|
20005 |
const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
|
|
20006 |
if( zDbName==0 ) continue; |
|
|
20007 |
if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); |
|
|
20008 |
if( sqlite3_stricmp(zDbName, "main")==0 ){ |
|
|
20009 |
appendText(&s, "SELECT name FROM ", 0); |
|
|
20010 |
}else{ |
|
|
20011 |
appendText(&s, "SELECT ", 0); |
|
|
20012 |
appendText(&s, zDbName, '\''); |
|
|
20013 |
appendText(&s, "||'.'||name FROM ", 0); |
|
|
20014 |
} |
|
|
20015 |
appendText(&s, zDbName, '"'); |
|
|
20016 |
appendText(&s, ".sqlite_schema ", 0); |
|
|
20017 |
if( c=='t' ){ |
|
|
20018 |
appendText(&s," WHERE type IN ('table','view')" |
|
|
20019 |
" AND name NOT LIKE 'sqlite_%'" |
|
|
20020 |
" AND name LIKE ?1", 0); |
|
|
20021 |
}else{ |
|
|
20022 |
appendText(&s," WHERE type='index'" |
|
|
20023 |
" AND tbl_name LIKE ?1", 0); |
|
|
20024 |
} |
|
|
20025 |
} |
|
|
20026 |
rc = sqlite3_finalize(pStmt); |
|
|
20027 |
appendText(&s, " ORDER BY 1", 0); |
|
|
20028 |
rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); |
|
|
20029 |
freeText(&s); |
|
|
20030 |
if( rc ) return shellDatabaseError(p->db); |
|
|
20031 |
|
|
|
20032 |
/* Run the SQL statement prepared by the above block. Store the results |
|
|
20033 |
** as an array of nul-terminated strings in azResult[]. */ |
|
|
20034 |
nRow = nAlloc = 0; |
|
|
20035 |
azResult = 0; |
|
|
20036 |
if( nArg>1 ){ |
|
|
20037 |
sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); |
|
|
20038 |
}else{ |
|
|
20039 |
sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); |
|
|
20040 |
} |
|
|
20041 |
while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
|
20042 |
if( nRow>=nAlloc ){ |
|
|
20043 |
char **azNew; |
|
|
20044 |
int n2 = nAlloc*2 + 10; |
|
|
20045 |
azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
|
|
20046 |
if( azNew==0 ) shell_out_of_memory(); |
|
|
20047 |
nAlloc = n2; |
|
|
20048 |
azResult = azNew; |
|
|
20049 |
} |
|
|
20050 |
azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
|
|
20051 |
if( 0==azResult[nRow] ) shell_out_of_memory(); |
|
|
20052 |
nRow++; |
|
|
20053 |
} |
|
|
20054 |
if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
|
|
20055 |
rc = shellDatabaseError(p->db); |
|
|
20056 |
} |
|
|
20057 |
|
|
|
20058 |
/* Pretty-print the contents of array azResult[] to the output */ |
|
|
20059 |
if( rc==0 && nRow>0 ){ |
|
|
20060 |
int len, maxlen = 0; |
|
|
20061 |
int i, j; |
|
|
20062 |
int nPrintCol, nPrintRow; |
|
|
20063 |
for(i=0; i |
|
|
20064 |
len = strlen30(azResult[i]); |
|
|
20065 |
if( len>maxlen ) maxlen = len; |
|
|
20066 |
} |
|
|
20067 |
nPrintCol = 80/(maxlen+2); |
|
|
20068 |
if( nPrintCol<1 ) nPrintCol = 1; |
|
|
20069 |
nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
|
|
20070 |
for(i=0; i |
|
|
20071 |
for(j=i; j |
|
|
20072 |
char *zSp = j |
|
|
20073 |
utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
|
|
20074 |
azResult[j] ? azResult[j]:""); |
|
|
20075 |
} |
|
|
20076 |
raw_printf(p->out, "\n"); |
|
|
20077 |
} |
|
|
20078 |
} |
|
|
20079 |
|
|
|
20080 |
for(ii=0; ii |
|
|
20081 |
sqlite3_free(azResult); |
|
|
20082 |
}else |
|
|
20083 |
|
|
|
20084 |
/* Begin redirecting output to the file "testcase-out.txt" */ |
|
|
20085 |
if( c=='t' && strcmp(azArg[0],"testcase")==0 ){ |
|
|
20086 |
output_reset(p); |
|
|
20087 |
p->out = output_file_open("testcase-out.txt", 0); |
|
|
20088 |
if( p->out==0 ){ |
|
|
20089 |
raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); |
|
|
20090 |
} |
|
|
20091 |
if( nArg>=2 ){ |
|
|
20092 |
sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); |
|
|
20093 |
}else{ |
|
|
20094 |
sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); |
|
|
20095 |
} |
|
|
20096 |
}else |
|
|
20097 |
|
|
|
20098 |
#ifndef SQLITE_UNTESTABLE |
|
|
20099 |
if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){ |
|
|
20100 |
static const struct { |
|
|
20101 |
const char *zCtrlName; /* Name of a test-control option */ |
|
|
20102 |
int ctrlCode; /* Integer code for that option */ |
|
|
20103 |
const char *zUsage; /* Usage notes */ |
|
|
20104 |
} aCtrl[] = { |
|
|
20105 |
{ "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" }, |
|
|
20106 |
{ "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" }, |
|
|
20107 |
/*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/ |
|
|
20108 |
/*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/ |
|
|
20109 |
{ "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" }, |
|
|
20110 |
{ "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN" }, |
|
|
20111 |
/*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" },*/ |
|
|
20112 |
{ "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"}, |
|
|
20113 |
{ "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" }, |
|
|
20114 |
{ "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" }, |
|
|
20115 |
{ "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" }, |
|
|
20116 |
{ "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" }, |
|
|
20117 |
#ifdef YYCOVERAGE |
|
|
20118 |
{ "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" }, |
|
|
20119 |
#endif |
|
|
20120 |
{ "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " }, |
|
|
20121 |
{ "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" }, |
|
|
20122 |
{ "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" }, |
|
|
20123 |
{ "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, "SEED ?db?" }, |
|
|
20124 |
{ "seek_count", SQLITE_TESTCTRL_SEEK_COUNT, "" }, |
|
|
20125 |
}; |
|
|
20126 |
int testctrl = -1; |
|
|
20127 |
int iCtrl = -1; |
|
|
20128 |
int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ |
|
|
20129 |
int isOk = 0; |
|
|
20130 |
int i, n2; |
|
|
20131 |
const char *zCmd = 0; |
|
|
20132 |
|
|
|
20133 |
open_db(p, 0); |
|
|
20134 |
zCmd = nArg>=2 ? azArg[1] : "help"; |
|
|
20135 |
|
|
|
20136 |
/* The argument can optionally begin with "-" or "--" */ |
|
|
20137 |
if( zCmd[0]=='-' && zCmd[1] ){ |
|
|
20138 |
zCmd++; |
|
|
20139 |
if( zCmd[0]=='-' && zCmd[1] ) zCmd++; |
|
|
20140 |
} |
|
|
20141 |
|
|
|
20142 |
/* --help lists all test-controls */ |
|
|
20143 |
if( strcmp(zCmd,"help")==0 ){ |
|
|
20144 |
utf8_printf(p->out, "Available test-controls:\n"); |
|
|
20145 |
for(i=0; i |
|
|
20146 |
utf8_printf(p->out, " .testctrl %s %s\n", |
|
|
20147 |
aCtrl[i].zCtrlName, aCtrl[i].zUsage); |
|
|
20148 |
} |
|
|
20149 |
rc = 1; |
|
|
20150 |
goto meta_command_exit; |
|
|
20151 |
} |
|
|
20152 |
|
|
|
20153 |
/* convert testctrl text option to value. allow any unique prefix |
|
|
20154 |
** of the option name, or a numerical value. */ |
|
|
20155 |
n2 = strlen30(zCmd); |
|
|
20156 |
for(i=0; i |
|
|
20157 |
if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ |
|
|
20158 |
if( testctrl<0 ){ |
|
|
20159 |
testctrl = aCtrl[i].ctrlCode; |
|
|
20160 |
iCtrl = i; |
|
|
20161 |
}else{ |
|
|
20162 |
utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" |
|
|
20163 |
"Use \".testctrl --help\" for help\n", zCmd); |
|
|
20164 |
rc = 1; |
|
|
20165 |
goto meta_command_exit; |
|
|
20166 |
} |
|
|
20167 |
} |
|
|
20168 |
} |
|
|
20169 |
if( testctrl<0 ){ |
|
|
20170 |
utf8_printf(stderr,"Error: unknown test-control: %s\n" |
|
|
20171 |
"Use \".testctrl --help\" for help\n", zCmd); |
|
|
20172 |
}else{ |
|
|
20173 |
switch(testctrl){ |
|
|
20174 |
|
|
|
20175 |
/* sqlite3_test_control(int, db, int) */ |
|
|
20176 |
case SQLITE_TESTCTRL_OPTIMIZATIONS: |
|
|
20177 |
if( nArg==3 ){ |
|
|
20178 |
unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0); |
|
|
20179 |
rc2 = sqlite3_test_control(testctrl, p->db, opt); |
|
|
20180 |
isOk = 3; |
|
|
20181 |
} |
|
|
20182 |
break; |
|
|
20183 |
|
|
|
20184 |
/* sqlite3_test_control(int) */ |
|
|
20185 |
case SQLITE_TESTCTRL_PRNG_SAVE: |
|
|
20186 |
case SQLITE_TESTCTRL_PRNG_RESTORE: |
|
|
20187 |
case SQLITE_TESTCTRL_BYTEORDER: |
|
|
20188 |
if( nArg==2 ){ |
|
|
20189 |
rc2 = sqlite3_test_control(testctrl); |
|
|
20190 |
isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; |
|
|
20191 |
} |
|
|
20192 |
break; |
|
|
20193 |
|
|
|
20194 |
/* sqlite3_test_control(int, uint) */ |
|
|
20195 |
case SQLITE_TESTCTRL_PENDING_BYTE: |
|
|
20196 |
if( nArg==3 ){ |
|
|
20197 |
unsigned int opt = (unsigned int)integerValue(azArg[2]); |
|
|
20198 |
rc2 = sqlite3_test_control(testctrl, opt); |
|
|
20199 |
isOk = 3; |
|
|
20200 |
} |
|
|
20201 |
break; |
|
|
20202 |
|
|
|
20203 |
/* sqlite3_test_control(int, int, sqlite3*) */ |
|
|
20204 |
case SQLITE_TESTCTRL_PRNG_SEED: |
|
|
20205 |
if( nArg==3 || nArg==4 ){ |
|
|
20206 |
int ii = (int)integerValue(azArg[2]); |
|
|
20207 |
sqlite3 *db; |
|
|
20208 |
if( ii==0 && strcmp(azArg[2],"random")==0 ){ |
|
|
20209 |
sqlite3_randomness(sizeof(ii),&ii); |
|
|
20210 |
printf("-- random seed: %d\n", ii); |
|
|
20211 |
} |
|
|
20212 |
if( nArg==3 ){ |
|
|
20213 |
db = 0; |
|
|
20214 |
}else{ |
|
|
20215 |
db = p->db; |
|
|
20216 |
/* Make sure the schema has been loaded */ |
|
|
20217 |
sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0); |
|
|
20218 |
} |
|
|
20219 |
rc2 = sqlite3_test_control(testctrl, ii, db); |
|
|
20220 |
isOk = 3; |
|
|
20221 |
} |
|
|
20222 |
break; |
|
|
20223 |
|
|
|
20224 |
/* sqlite3_test_control(int, int) */ |
|
|
20225 |
case SQLITE_TESTCTRL_ASSERT: |
|
|
20226 |
case SQLITE_TESTCTRL_ALWAYS: |
|
|
20227 |
if( nArg==3 ){ |
|
|
20228 |
int opt = booleanValue(azArg[2]); |
|
|
20229 |
rc2 = sqlite3_test_control(testctrl, opt); |
|
|
20230 |
isOk = 1; |
|
|
20231 |
} |
|
|
20232 |
break; |
|
|
20233 |
|
|
|
20234 |
/* sqlite3_test_control(int, int) */ |
|
|
20235 |
case SQLITE_TESTCTRL_LOCALTIME_FAULT: |
|
|
20236 |
case SQLITE_TESTCTRL_NEVER_CORRUPT: |
|
|
20237 |
if( nArg==3 ){ |
|
|
20238 |
int opt = booleanValue(azArg[2]); |
|
|
20239 |
rc2 = sqlite3_test_control(testctrl, opt); |
|
|
20240 |
isOk = 3; |
|
|
20241 |
} |
|
|
20242 |
break; |
|
|
20243 |
|
|
|
20244 |
/* sqlite3_test_control(sqlite3*) */ |
|
|
20245 |
case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: |
|
|
20246 |
rc2 = sqlite3_test_control(testctrl, p->db); |
|
|
20247 |
isOk = 3; |
|
|
20248 |
break; |
|
|
20249 |
|
|
|
20250 |
case SQLITE_TESTCTRL_IMPOSTER: |
|
|
20251 |
if( nArg==5 ){ |
|
|
20252 |
rc2 = sqlite3_test_control(testctrl, p->db, |
|
|
20253 |
azArg[2], |
|
|
20254 |
integerValue(azArg[3]), |
|
|
20255 |
integerValue(azArg[4])); |
|
|
20256 |
isOk = 3; |
|
|
20257 |
} |
|
|
20258 |
break; |
|
|
20259 |
|
|
|
20260 |
case SQLITE_TESTCTRL_SEEK_COUNT: { |
|
|
20261 |
u64 x = 0; |
|
|
20262 |
rc2 = sqlite3_test_control(testctrl, p->db, &x); |
|
|
20263 |
utf8_printf(p->out, "%llu\n", x); |
|
|
20264 |
isOk = 3; |
|
|
20265 |
break; |
|
|
20266 |
} |
|
|
20267 |
|
|
|
20268 |
#ifdef YYCOVERAGE |
|
|
20269 |
case SQLITE_TESTCTRL_PARSER_COVERAGE: |
|
|
20270 |
if( nArg==2 ){ |
|
|
20271 |
sqlite3_test_control(testctrl, p->out); |
|
|
20272 |
isOk = 3; |
|
|
20273 |
} |
|
|
20274 |
#endif |
|
|
20275 |
} |
|
|
20276 |
} |
|
|
20277 |
if( isOk==0 && iCtrl>=0 ){ |
|
|
20278 |
utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); |
|
|
20279 |
rc = 1; |
|
|
20280 |
}else if( isOk==1 ){ |
|
|
20281 |
raw_printf(p->out, "%d\n", rc2); |
|
|
20282 |
}else if( isOk==2 ){ |
|
|
20283 |
raw_printf(p->out, "0x%08x\n", rc2); |
|
|
20284 |
} |
|
|
20285 |
}else |
|
|
20286 |
#endif /* !defined(SQLITE_UNTESTABLE) */ |
|
|
20287 |
|
|
|
20288 |
if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
|
|
20289 |
open_db(p, 0); |
|
|
20290 |
sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
|
|
20291 |
}else |
|
|
20292 |
|
|
|
20293 |
if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
|
|
20294 |
if( nArg==2 ){ |
|
|
20295 |
enableTimer = booleanValue(azArg[1]); |
|
|
20296 |
if( enableTimer && !HAS_TIMER ){ |
|
|
20297 |
raw_printf(stderr, "Error: timer not available on this system.\n"); |
|
|
20298 |
enableTimer = 0; |
|
|
20299 |
} |
|
|
20300 |
}else{ |
|
|
20301 |
raw_printf(stderr, "Usage: .timer on|off\n"); |
|
|
20302 |
rc = 1; |
|
|
20303 |
} |
|
|
20304 |
}else |
|
|
20305 |
|
|
|
20306 |
#ifndef SQLITE_OMIT_TRACE |
|
|
20307 |
if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
|
|
20308 |
int mType = 0; |
|
|
20309 |
int jj; |
|
|
20310 |
open_db(p, 0); |
|
|
20311 |
for(jj=1; jj |
|
|
20312 |
const char *z = azArg[jj]; |
|
|
20313 |
if( z[0]=='-' ){ |
|
|
20314 |
if( optionMatch(z, "expanded") ){ |
|
|
20315 |
p->eTraceType = SHELL_TRACE_EXPANDED; |
|
|
20316 |
} |
|
|
20317 |
#ifdef SQLITE_ENABLE_NORMALIZE |
|
|
20318 |
else if( optionMatch(z, "normalized") ){ |
|
|
20319 |
p->eTraceType = SHELL_TRACE_NORMALIZED; |
|
|
20320 |
} |
|
|
20321 |
#endif |
|
|
20322 |
else if( optionMatch(z, "plain") ){ |
|
|
20323 |
p->eTraceType = SHELL_TRACE_PLAIN; |
|
|
20324 |
} |
|
|
20325 |
else if( optionMatch(z, "profile") ){ |
|
|
20326 |
mType |= SQLITE_TRACE_PROFILE; |
|
|
20327 |
} |
|
|
20328 |
else if( optionMatch(z, "row") ){ |
|
|
20329 |
mType |= SQLITE_TRACE_ROW; |
|
|
20330 |
} |
|
|
20331 |
else if( optionMatch(z, "stmt") ){ |
|
|
20332 |
mType |= SQLITE_TRACE_STMT; |
|
|
20333 |
} |
|
|
20334 |
else if( optionMatch(z, "close") ){ |
|
|
20335 |
mType |= SQLITE_TRACE_CLOSE; |
|
|
20336 |
} |
|
|
20337 |
else { |
|
|
20338 |
raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z); |
|
|
20339 |
rc = 1; |
|
|
20340 |
goto meta_command_exit; |
|
|
20341 |
} |
|
|
20342 |
}else{ |
|
|
20343 |
output_file_close(p->traceOut); |
|
|
20344 |
p->traceOut = output_file_open(azArg[1], 0); |
|
|
20345 |
} |
|
|
20346 |
} |
|
|
20347 |
if( p->traceOut==0 ){ |
|
|
20348 |
sqlite3_trace_v2(p->db, 0, 0, 0); |
|
|
20349 |
}else{ |
|
|
20350 |
if( mType==0 ) mType = SQLITE_TRACE_STMT; |
|
|
20351 |
sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); |
|
|
20352 |
} |
|
|
20353 |
}else |
|
|
20354 |
#endif /* !defined(SQLITE_OMIT_TRACE) */ |
|
|
20355 |
|
|
|
20356 |
#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
|
|
20357 |
if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){ |
|
|
20358 |
int ii; |
|
|
20359 |
int lenOpt; |
|
|
20360 |
char *zOpt; |
|
|
20361 |
if( nArg<2 ){ |
|
|
20362 |
raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n"); |
|
|
20363 |
rc = 1; |
|
|
20364 |
goto meta_command_exit; |
|
|
20365 |
} |
|
|
20366 |
open_db(p, 0); |
|
|
20367 |
zOpt = azArg[1]; |
|
|
20368 |
if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; |
|
|
20369 |
lenOpt = (int)strlen(zOpt); |
|
|
20370 |
if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){ |
|
|
20371 |
assert( azArg[nArg]==0 ); |
|
|
20372 |
sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); |
|
|
20373 |
}else{ |
|
|
20374 |
for(ii=1; ii |
|
|
20375 |
sqlite3_create_module(p->db, azArg[ii], 0, 0); |
|
|
20376 |
} |
|
|
20377 |
} |
|
|
20378 |
}else |
|
|
20379 |
#endif |
|
|
20380 |
|
|
|
20381 |
#if SQLITE_USER_AUTHENTICATION |
|
|
20382 |
if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
|
|
20383 |
if( nArg<2 ){ |
|
|
20384 |
raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
|
|
20385 |
rc = 1; |
|
|
20386 |
goto meta_command_exit; |
|
|
20387 |
} |
|
|
20388 |
open_db(p, 0); |
|
|
20389 |
if( strcmp(azArg[1],"login")==0 ){ |
|
|
20390 |
if( nArg!=4 ){ |
|
|
20391 |
raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
|
|
20392 |
rc = 1; |
|
|
20393 |
goto meta_command_exit; |
|
|
20394 |
} |
|
|
20395 |
rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
|
|
20396 |
strlen30(azArg[3])); |
|
|
20397 |
if( rc ){ |
|
|
20398 |
utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
|
|
20399 |
rc = 1; |
|
|
20400 |
} |
|
|
20401 |
}else if( strcmp(azArg[1],"add")==0 ){ |
|
|
20402 |
if( nArg!=5 ){ |
|
|
20403 |
raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
|
|
20404 |
rc = 1; |
|
|
20405 |
goto meta_command_exit; |
|
|
20406 |
} |
|
|
20407 |
rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), |
|
|
20408 |
booleanValue(azArg[4])); |
|
|
20409 |
if( rc ){ |
|
|
20410 |
raw_printf(stderr, "User-Add failed: %d\n", rc); |
|
|
20411 |
rc = 1; |
|
|
20412 |
} |
|
|
20413 |
}else if( strcmp(azArg[1],"edit")==0 ){ |
|
|
20414 |
if( nArg!=5 ){ |
|
|
20415 |
raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
|
|
20416 |
rc = 1; |
|
|
20417 |
goto meta_command_exit; |
|
|
20418 |
} |
|
|
20419 |
rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), |
|
|
20420 |
booleanValue(azArg[4])); |
|
|
20421 |
if( rc ){ |
|
|
20422 |
raw_printf(stderr, "User-Edit failed: %d\n", rc); |
|
|
20423 |
rc = 1; |
|
|
20424 |
} |
|
|
20425 |
}else if( strcmp(azArg[1],"delete")==0 ){ |
|
|
20426 |
if( nArg!=3 ){ |
|
|
20427 |
raw_printf(stderr, "Usage: .user delete USER\n"); |
|
|
20428 |
rc = 1; |
|
|
20429 |
goto meta_command_exit; |
|
|
20430 |
} |
|
|
20431 |
rc = sqlite3_user_delete(p->db, azArg[2]); |
|
|
20432 |
if( rc ){ |
|
|
20433 |
raw_printf(stderr, "User-Delete failed: %d\n", rc); |
|
|
20434 |
rc = 1; |
|
|
20435 |
} |
|
|
20436 |
}else{ |
|
|
20437 |
raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
|
|
20438 |
rc = 1; |
|
|
20439 |
goto meta_command_exit; |
|
|
20440 |
} |
|
|
20441 |
}else |
|
|
20442 |
#endif /* SQLITE_USER_AUTHENTICATION */ |
|
|
20443 |
|
|
|
20444 |
if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
|
|
20445 |
utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
|
|
20446 |
sqlite3_libversion(), sqlite3_sourceid()); |
|
|
20447 |
#if SQLITE_HAVE_ZLIB |
|
|
20448 |
utf8_printf(p->out, "zlib version %s\n", zlibVersion()); |
|
|
20449 |
#endif |
|
|
20450 |
#define CTIMEOPT_VAL_(opt) #opt |
|
|
20451 |
#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) |
|
|
20452 |
#if defined(__clang__) && defined(__clang_major__) |
|
|
20453 |
utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." |
|
|
20454 |
CTIMEOPT_VAL(__clang_minor__) "." |
|
|
20455 |
CTIMEOPT_VAL(__clang_patchlevel__) "\n"); |
|
|
20456 |
#elif defined(_MSC_VER) |
|
|
20457 |
utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); |
|
|
20458 |
#elif defined(__GNUC__) && defined(__VERSION__) |
|
|
20459 |
utf8_printf(p->out, "gcc-" __VERSION__ "\n"); |
|
|
20460 |
#endif |
|
|
20461 |
}else |
|
|
20462 |
|
|
|
20463 |
if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
|
|
20464 |
const char *zDbName = nArg==2 ? azArg[1] : "main"; |
|
|
20465 |
sqlite3_vfs *pVfs = 0; |
|
|
20466 |
if( p->db ){ |
|
|
20467 |
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
|
|
20468 |
if( pVfs ){ |
|
|
20469 |
utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
|
|
20470 |
raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
|
|
20471 |
raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
|
|
20472 |
raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
|
|
20473 |
} |
|
|
20474 |
} |
|
|
20475 |
}else |
|
|
20476 |
|
|
|
20477 |
if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
|
|
20478 |
sqlite3_vfs *pVfs; |
|
|
20479 |
sqlite3_vfs *pCurrent = 0; |
|
|
20480 |
if( p->db ){ |
|
|
20481 |
sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
|
|
20482 |
} |
|
|
20483 |
for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
|
|
20484 |
utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
|
|
20485 |
pVfs==pCurrent ? " <--- CURRENT" : ""); |
|
|
20486 |
raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
|
|
20487 |
raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
|
|
20488 |
raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
|
|
20489 |
if( pVfs->pNext ){ |
|
|
20490 |
raw_printf(p->out, "-----------------------------------\n"); |
|
|
20491 |
} |
|
|
20492 |
} |
|
|
20493 |
}else |
|
|
20494 |
|
|
|
20495 |
if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
|
|
20496 |
const char *zDbName = nArg==2 ? azArg[1] : "main"; |
|
|
20497 |
char *zVfsName = 0; |
|
|
20498 |
if( p->db ){ |
|
|
20499 |
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
|
|
20500 |
if( zVfsName ){ |
|
|
20501 |
utf8_printf(p->out, "%s\n", zVfsName); |
|
|
20502 |
sqlite3_free(zVfsName); |
|
|
20503 |
} |
|
|
20504 |
} |
|
|
20505 |
}else |
|
|
20506 |
|
|
|
20507 |
if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
|
|
20508 |
unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff; |
|
|
20509 |
sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x); |
|
|
20510 |
}else |
|
|
20511 |
|
|
|
20512 |
if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
|
|
20513 |
int j; |
|
|
20514 |
assert( nArg<=ArraySize(azArg) ); |
|
|
20515 |
p->nWidth = nArg-1; |
|
|
20516 |
p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2); |
|
|
20517 |
if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory(); |
|
|
20518 |
if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth]; |
|
|
20519 |
for(j=1; j |
|
|
20520 |
p->colWidth[j-1] = (int)integerValue(azArg[j]); |
|
|
20521 |
} |
|
|
20522 |
}else |
|
|
20523 |
|
|
|
20524 |
{ |
|
|
20525 |
utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
|
|
20526 |
" \"%s\". Enter \".help\" for help\n", azArg[0]); |
|
|
20527 |
rc = 1; |
|
|
20528 |
} |
|
|
20529 |
|
|
|
20530 |
meta_command_exit: |
|
|
20531 |
if( p->outCount ){ |
|
|
20532 |
p->outCount--; |
|
|
20533 |
if( p->outCount==0 ) output_reset(p); |
|
|
20534 |
} |
|
|
20535 |
return rc; |
|
|
20536 |
} |
|
|
20537 |
|
|
|
20538 |
/* |
|
|
20539 |
** Return TRUE if a semicolon occurs anywhere in the first N characters |
|
|
20540 |
** of string z[]. |
|
|
20541 |
*/ |
|
|
20542 |
static int line_contains_semicolon(const char *z, int N){ |
|
|
20543 |
int i; |
|
|
20544 |
for(i=0; i |
|
|
20545 |
return 0; |
|
|
20546 |
} |
|
|
20547 |
|
|
|
20548 |
/* |
|
|
20549 |
** Test to see if a line consists entirely of whitespace. |
|
|
20550 |
*/ |
|
|
20551 |
static int _all_whitespace(const char *z){ |
|
|
20552 |
for(; *z; z++){ |
|
|
20553 |
if( IsSpace(z[0]) ) continue; |
|
|
20554 |
if( *z=='/' && z[1]=='*' ){ |
|
|
20555 |
z += 2; |
|
|
20556 |
while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
|
|
20557 |
if( *z==0 ) return 0; |
|
|
20558 |
z++; |
|
|
20559 |
continue; |
|
|
20560 |
} |
|
|
20561 |
if( *z=='-' && z[1]=='-' ){ |
|
|
20562 |
z += 2; |
|
|
20563 |
while( *z && *z!='\n' ){ z++; } |
|
|
20564 |
if( *z==0 ) return 1; |
|
|
20565 |
continue; |
|
|
20566 |
} |
|
|
20567 |
return 0; |
|
|
20568 |
} |
|
|
20569 |
return 1; |
|
|
20570 |
} |
|
|
20571 |
|
|
|
20572 |
/* |
|
|
20573 |
** Return TRUE if the line typed in is an SQL command terminator other |
|
|
20574 |
** than a semi-colon. The SQL Server style "go" command is understood |
|
|
20575 |
** as is the Oracle "/". |
|
|
20576 |
*/ |
|
|
20577 |
static int line_is_command_terminator(const char *zLine){ |
|
|
20578 |
while( IsSpace(zLine[0]) ){ zLine++; }; |
|
|
20579 |
if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
|
|
20580 |
return 1; /* Oracle */ |
|
|
20581 |
} |
|
|
20582 |
if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' |
|
|
20583 |
&& _all_whitespace(&zLine[2]) ){ |
|
|
20584 |
return 1; /* SQL Server */ |
|
|
20585 |
} |
|
|
20586 |
return 0; |
|
|
20587 |
} |
|
|
20588 |
|
|
|
20589 |
/* |
|
|
20590 |
** We need a default sqlite3_complete() implementation to use in case |
|
|
20591 |
** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes |
|
|
20592 |
** any arbitrary text is a complete SQL statement. This is not very |
|
|
20593 |
** user-friendly, but it does seem to work. |
|
|
20594 |
*/ |
|
|
20595 |
#ifdef SQLITE_OMIT_COMPLETE |
|
|
20596 |
#define sqlite3_complete(x) 1 |
|
|
20597 |
#endif |
|
|
20598 |
|
|
|
20599 |
/* |
|
|
20600 |
** Return true if zSql is a complete SQL statement. Return false if it |
|
|
20601 |
** ends in the middle of a string literal or C-style comment. |
|
|
20602 |
*/ |
|
|
20603 |
static int line_is_complete(char *zSql, int nSql){ |
|
|
20604 |
int rc; |
|
|
20605 |
if( zSql==0 ) return 1; |
|
|
20606 |
zSql[nSql] = ';'; |
|
|
20607 |
zSql[nSql+1] = 0; |
|
|
20608 |
rc = sqlite3_complete(zSql); |
|
|
20609 |
zSql[nSql] = 0; |
|
|
20610 |
return rc; |
|
|
20611 |
} |
|
|
20612 |
|
|
|
20613 |
/* |
|
|
20614 |
** Run a single line of SQL. Return the number of errors. |
|
|
20615 |
*/ |
|
|
20616 |
static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ |
|
|
20617 |
int rc; |
|
|
20618 |
char *zErrMsg = 0; |
|
|
20619 |
|
|
|
20620 |
open_db(p, 0); |
|
|
20621 |
if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); |
|
|
20622 |
if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; |
|
|
20623 |
BEGIN_TIMER; |
|
|
20624 |
rc = shell_exec(p, zSql, &zErrMsg); |
|
|
20625 |
END_TIMER; |
|
|
20626 |
if( rc || zErrMsg ){ |
|
|
20627 |
char zPrefix[100]; |
|
|
20628 |
if( in!=0 || !stdin_is_interactive ){ |
|
|
20629 |
sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
|
|
20630 |
"Error: near line %d:", startline); |
|
|
20631 |
}else{ |
|
|
20632 |
sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
|
|
20633 |
} |
|
|
20634 |
if( zErrMsg!=0 ){ |
|
|
20635 |
utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
|
|
20636 |
sqlite3_free(zErrMsg); |
|
|
20637 |
zErrMsg = 0; |
|
|
20638 |
}else{ |
|
|
20639 |
utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
|
|
20640 |
} |
|
|
20641 |
return 1; |
|
|
20642 |
}else if( ShellHasFlag(p, SHFLG_CountChanges) ){ |
|
|
20643 |
raw_printf(p->out, "changes: %3d total_changes: %d\n", |
|
|
20644 |
sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
|
|
20645 |
} |
|
|
20646 |
return 0; |
|
|
20647 |
} |
|
|
20648 |
|
|
|
20649 |
|
|
|
20650 |
/* |
|
|
20651 |
** Read input from *in and process it. If *in==0 then input |
|
|
20652 |
** is interactive - the user is typing it it. Otherwise, input |
|
|
20653 |
** is coming from a file or device. A prompt is issued and history |
|
|
20654 |
** is saved only if input is interactive. An interrupt signal will |
|
|
20655 |
** cause this routine to exit immediately, unless input is interactive. |
|
|
20656 |
** |
|
|
20657 |
** Return the number of errors. |
|
|
20658 |
*/ |
|
|
20659 |
static int process_input(ShellState *p){ |
|
|
20660 |
char *zLine = 0; /* A single input line */ |
|
|
20661 |
char *zSql = 0; /* Accumulated SQL text */ |
|
|
20662 |
int nLine; /* Length of current line */ |
|
|
20663 |
int nSql = 0; /* Bytes of zSql[] used */ |
|
|
20664 |
int nAlloc = 0; /* Allocated zSql[] space */ |
|
|
20665 |
int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
|
|
20666 |
int rc; /* Error code */ |
|
|
20667 |
int errCnt = 0; /* Number of errors seen */ |
|
|
20668 |
int startline = 0; /* Line number for start of current input */ |
|
|
20669 |
|
|
|
20670 |
p->lineno = 0; |
|
|
20671 |
while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ |
|
|
20672 |
fflush(p->out); |
|
|
20673 |
zLine = one_input_line(p->in, zLine, nSql>0); |
|
|
20674 |
if( zLine==0 ){ |
|
|
20675 |
/* End of input */ |
|
|
20676 |
if( p->in==0 && stdin_is_interactive ) printf("\n"); |
|
|
20677 |
break; |
|
|
20678 |
} |
|
|
20679 |
if( seenInterrupt ){ |
|
|
20680 |
if( p->in!=0 ) break; |
|
|
20681 |
seenInterrupt = 0; |
|
|
20682 |
} |
|
|
20683 |
p->lineno++; |
|
|
20684 |
if( nSql==0 && _all_whitespace(zLine) ){ |
|
|
20685 |
if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
|
|
20686 |
continue; |
|
|
20687 |
} |
|
|
20688 |
if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){ |
|
|
20689 |
if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine); |
|
|
20690 |
if( zLine[0]=='.' ){ |
|
|
20691 |
rc = do_meta_command(zLine, p); |
|
|
20692 |
if( rc==2 ){ /* exit requested */ |
|
|
20693 |
break; |
|
|
20694 |
}else if( rc ){ |
|
|
20695 |
errCnt++; |
|
|
20696 |
} |
|
|
20697 |
} |
|
|
20698 |
continue; |
|
|
20699 |
} |
|
|
20700 |
if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
|
|
20701 |
memcpy(zLine,";",2); |
|
|
20702 |
} |
|
|
20703 |
nLine = strlen30(zLine); |
|
|
20704 |
if( nSql+nLine+2>=nAlloc ){ |
|
|
20705 |
nAlloc = nSql+nLine+100; |
|
|
20706 |
zSql = realloc(zSql, nAlloc); |
|
|
20707 |
if( zSql==0 ) shell_out_of_memory(); |
|
|
20708 |
} |
|
|
20709 |
nSqlPrior = nSql; |
|
|
20710 |
if( nSql==0 ){ |
|
|
20711 |
int i; |
|
|
20712 |
for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
|
|
20713 |
assert( nAlloc>0 && zSql!=0 ); |
|
|
20714 |
memcpy(zSql, zLine+i, nLine+1-i); |
|
|
20715 |
startline = p->lineno; |
|
|
20716 |
nSql = nLine-i; |
|
|
20717 |
}else{ |
|
|
20718 |
zSql[nSql++] = '\n'; |
|
|
20719 |
memcpy(zSql+nSql, zLine, nLine+1); |
|
|
20720 |
nSql += nLine; |
|
|
20721 |
} |
|
|
20722 |
if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
|
|
20723 |
&& sqlite3_complete(zSql) ){ |
|
|
20724 |
errCnt += runOneSqlLine(p, zSql, p->in, startline); |
|
|
20725 |
nSql = 0; |
|
|
20726 |
if( p->outCount ){ |
|
|
20727 |
output_reset(p); |
|
|
20728 |
p->outCount = 0; |
|
|
20729 |
}else{ |
|
|
20730 |
clearTempFile(p); |
|
|
20731 |
} |
|
|
20732 |
}else if( nSql && _all_whitespace(zSql) ){ |
|
|
20733 |
if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql); |
|
|
20734 |
nSql = 0; |
|
|
20735 |
} |
|
|
20736 |
} |
|
|
20737 |
if( nSql && !_all_whitespace(zSql) ){ |
|
|
20738 |
errCnt += runOneSqlLine(p, zSql, p->in, startline); |
|
|
20739 |
} |
|
|
20740 |
free(zSql); |
|
|
20741 |
free(zLine); |
|
|
20742 |
return errCnt>0; |
|
|
20743 |
} |
|
|
20744 |
|
|
|
20745 |
/* |
|
|
20746 |
** Return a pathname which is the user's home directory. A |
|
|
20747 |
** 0 return indicates an error of some kind. |
|
|
20748 |
*/ |
|
|
20749 |
static char *find_home_dir(int clearFlag){ |
|
|
20750 |
static char *home_dir = NULL; |
|
|
20751 |
if( clearFlag ){ |
|
|
20752 |
free(home_dir); |
|
|
20753 |
home_dir = 0; |
|
|
20754 |
return 0; |
|
|
20755 |
} |
|
|
20756 |
if( home_dir ) return home_dir; |
|
|
20757 |
|
|
|
20758 |
#ifdef _KOLIBRI |
|
|
20759 |
home_dir = "/sys"; |
|
|
20760 |
|
|
|
20761 |
#elif !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
|
|
20762 |
&& !defined(__RTP__) && !defined(_WRS_KERNEL) |
|
|
20763 |
{ |
|
|
20764 |
struct passwd *pwent; |
|
|
20765 |
uid_t uid = getuid(); |
|
|
20766 |
if( (pwent=getpwuid(uid)) != NULL) { |
|
|
20767 |
home_dir = pwent->pw_dir; |
|
|
20768 |
} |
|
|
20769 |
} |
|
|
20770 |
#endif |
|
|
20771 |
|
|
|
20772 |
#if defined(_WIN32_WCE) |
|
|
20773 |
/* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
|
|
20774 |
*/ |
|
|
20775 |
home_dir = "/"; |
|
|
20776 |
#else |
|
|
20777 |
|
|
|
20778 |
#if defined(_WIN32) || defined(WIN32) |
|
|
20779 |
if (!home_dir) { |
|
|
20780 |
home_dir = getenv("USERPROFILE"); |
|
|
20781 |
} |
|
|
20782 |
#endif |
|
|
20783 |
|
|
|
20784 |
if (!home_dir) { |
|
|
20785 |
home_dir = getenv("HOME"); |
|
|
20786 |
} |
|
|
20787 |
|
|
|
20788 |
#if defined(_WIN32) || defined(WIN32) |
|
|
20789 |
if (!home_dir) { |
|
|
20790 |
char *zDrive, *zPath; |
|
|
20791 |
int n; |
|
|
20792 |
zDrive = getenv("HOMEDRIVE"); |
|
|
20793 |
zPath = getenv("HOMEPATH"); |
|
|
20794 |
if( zDrive && zPath ){ |
|
|
20795 |
n = strlen30(zDrive) + strlen30(zPath) + 1; |
|
|
20796 |
home_dir = malloc( n ); |
|
|
20797 |
if( home_dir==0 ) return 0; |
|
|
20798 |
sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
|
|
20799 |
return home_dir; |
|
|
20800 |
} |
|
|
20801 |
home_dir = "c:\\"; |
|
|
20802 |
} |
|
|
20803 |
#endif |
|
|
20804 |
|
|
|
20805 |
#endif /* !_WIN32_WCE */ |
|
|
20806 |
|
|
|
20807 |
if( home_dir ){ |
|
|
20808 |
int n = strlen30(home_dir) + 1; |
|
|
20809 |
char *z = malloc( n ); |
|
|
20810 |
if( z ) memcpy(z, home_dir, n); |
|
|
20811 |
home_dir = z; |
|
|
20812 |
} |
|
|
20813 |
|
|
|
20814 |
return home_dir; |
|
|
20815 |
} |
|
|
20816 |
|
|
|
20817 |
/* |
|
|
20818 |
** Read input from the file given by sqliterc_override. Or if that |
|
|
20819 |
** parameter is NULL, take input from ~/.sqliterc |
|
|
20820 |
** |
|
|
20821 |
** Returns the number of errors. |
|
|
20822 |
*/ |
|
|
20823 |
static void process_sqliterc( |
|
|
20824 |
ShellState *p, /* Configuration data */ |
|
|
20825 |
const char *sqliterc_override /* Name of config file. NULL to use default */ |
|
|
20826 |
){ |
|
|
20827 |
char *home_dir = NULL; |
|
|
20828 |
const char *sqliterc = sqliterc_override; |
|
|
20829 |
char *zBuf = 0; |
|
|
20830 |
FILE *inSaved = p->in; |
|
|
20831 |
int savedLineno = p->lineno; |
|
|
20832 |
|
|
|
20833 |
if (sqliterc == NULL) { |
|
|
20834 |
home_dir = find_home_dir(0); |
|
|
20835 |
if( home_dir==0 ){ |
|
|
20836 |
raw_printf(stderr, "-- warning: cannot find home directory;" |
|
|
20837 |
" cannot read ~/.sqliterc\n"); |
|
|
20838 |
return; |
|
|
20839 |
} |
|
|
20840 |
zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
|
|
20841 |
sqliterc = zBuf; |
|
|
20842 |
} |
|
|
20843 |
p->in = fopen(sqliterc,"rb"); |
|
|
20844 |
if( p->in ){ |
|
|
20845 |
if( stdin_is_interactive ){ |
|
|
20846 |
utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
|
|
20847 |
} |
|
|
20848 |
if( process_input(p) && bail_on_error ) exit(1); |
|
|
20849 |
fclose(p->in); |
|
|
20850 |
}else if( sqliterc_override!=0 ){ |
|
|
20851 |
utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc); |
|
|
20852 |
if( bail_on_error ) exit(1); |
|
|
20853 |
} |
|
|
20854 |
p->in = inSaved; |
|
|
20855 |
p->lineno = savedLineno; |
|
|
20856 |
sqlite3_free(zBuf); |
|
|
20857 |
} |
|
|
20858 |
|
|
|
20859 |
/* |
|
|
20860 |
** Show available command line options |
|
|
20861 |
*/ |
|
|
20862 |
static const char zOptions[] = |
|
|
20863 |
#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) |
|
|
20864 |
" -A ARGS... run \".archive ARGS\" and exit\n" |
|
|
20865 |
#endif |
|
|
20866 |
" -append append the database to the end of the file\n" |
|
|
20867 |
" -ascii set output mode to 'ascii'\n" |
|
|
20868 |
" -bail stop after hitting an error\n" |
|
|
20869 |
" -batch force batch I/O\n" |
|
|
20870 |
" -box set output mode to 'box'\n" |
|
|
20871 |
" -column set output mode to 'column'\n" |
|
|
20872 |
" -cmd COMMAND run \"COMMAND\" before reading stdin\n" |
|
|
20873 |
" -csv set output mode to 'csv'\n" |
|
|
20874 |
#if defined(SQLITE_ENABLE_DESERIALIZE) |
|
|
20875 |
" -deserialize open the database using sqlite3_deserialize()\n" |
|
|
20876 |
#endif |
|
|
20877 |
" -echo print commands before execution\n" |
|
|
20878 |
" -init FILENAME read/process named file\n" |
|
|
20879 |
" -[no]header turn headers on or off\n" |
|
|
20880 |
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
|
|
20881 |
" -heap SIZE Size of heap for memsys3 or memsys5\n" |
|
|
20882 |
#endif |
|
|
20883 |
" -help show this message\n" |
|
|
20884 |
" -html set output mode to HTML\n" |
|
|
20885 |
" -interactive force interactive I/O\n" |
|
|
20886 |
" -json set output mode to 'json'\n" |
|
|
20887 |
" -line set output mode to 'line'\n" |
|
|
20888 |
" -list set output mode to 'list'\n" |
|
|
20889 |
" -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
|
|
20890 |
" -markdown set output mode to 'markdown'\n" |
|
|
20891 |
#if defined(SQLITE_ENABLE_DESERIALIZE) |
|
|
20892 |
" -maxsize N maximum size for a --deserialize database\n" |
|
|
20893 |
#endif |
|
|
20894 |
" -memtrace trace all memory allocations and deallocations\n" |
|
|
20895 |
" -mmap N default mmap size set to N\n" |
|
|
20896 |
#ifdef SQLITE_ENABLE_MULTIPLEX |
|
|
20897 |
" -multiplex enable the multiplexor VFS\n" |
|
|
20898 |
#endif |
|
|
20899 |
" -newline SEP set output row separator. Default: '\\n'\n" |
|
|
20900 |
" -nofollow refuse to open symbolic links to database files\n" |
|
|
20901 |
" -nullvalue TEXT set text string for NULL values. Default ''\n" |
|
|
20902 |
" -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
|
|
20903 |
" -quote set output mode to 'quote'\n" |
|
|
20904 |
" -readonly open the database read-only\n" |
|
|
20905 |
" -separator SEP set output column separator. Default: '|'\n" |
|
|
20906 |
#ifdef SQLITE_ENABLE_SORTER_REFERENCES |
|
|
20907 |
" -sorterref SIZE sorter references threshold size\n" |
|
|
20908 |
#endif |
|
|
20909 |
" -stats print memory stats before each finalize\n" |
|
|
20910 |
" -table set output mode to 'table'\n" |
|
|
20911 |
" -tabs set output mode to 'tabs'\n" |
|
|
20912 |
" -version show SQLite version\n" |
|
|
20913 |
" -vfs NAME use NAME as the default VFS\n" |
|
|
20914 |
#ifdef SQLITE_ENABLE_VFSTRACE |
|
|
20915 |
" -vfstrace enable tracing of all VFS calls\n" |
|
|
20916 |
#endif |
|
|
20917 |
#ifdef SQLITE_HAVE_ZLIB |
|
|
20918 |
" -zip open the file as a ZIP Archive\n" |
|
|
20919 |
#endif |
|
|
20920 |
; |
|
|
20921 |
static void usage(int showDetail){ |
|
|
20922 |
utf8_printf(stderr, |
|
|
20923 |
"Usage: %s [OPTIONS] FILENAME [SQL]\n" |
|
|
20924 |
"FILENAME is the name of an SQLite database. A new database is created\n" |
|
|
20925 |
"if the file does not previously exist.\n", Argv0); |
|
|
20926 |
if( showDetail ){ |
|
|
20927 |
utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
|
|
20928 |
}else{ |
|
|
20929 |
raw_printf(stderr, "Use the -help option for additional information\n"); |
|
|
20930 |
} |
|
|
20931 |
exit(1); |
|
|
20932 |
} |
|
|
20933 |
|
|
|
20934 |
/* |
|
|
20935 |
** Internal check: Verify that the SQLite is uninitialized. Print a |
|
|
20936 |
** error message if it is initialized. |
|
|
20937 |
*/ |
|
|
20938 |
static void verify_uninitialized(void){ |
|
|
20939 |
if( sqlite3_config(-1)==SQLITE_MISUSE ){ |
|
|
20940 |
utf8_printf(stdout, "WARNING: attempt to configure SQLite after" |
|
|
20941 |
" initialization.\n"); |
|
|
20942 |
} |
|
|
20943 |
} |
|
|
20944 |
|
|
|
20945 |
/* |
|
|
20946 |
** Initialize the state information in data |
|
|
20947 |
*/ |
|
|
20948 |
static void main_init(ShellState *data) { |
|
|
20949 |
memset(data, 0, sizeof(*data)); |
|
|
20950 |
data->normalMode = data->cMode = data->mode = MODE_List; |
|
|
20951 |
data->autoExplain = 1; |
|
|
20952 |
memcpy(data->colSeparator,SEP_Column, 2); |
|
|
20953 |
memcpy(data->rowSeparator,SEP_Row, 2); |
|
|
20954 |
data->showHeader = 0; |
|
|
20955 |
data->shellFlgs = SHFLG_Lookaside; |
|
|
20956 |
verify_uninitialized(); |
|
|
20957 |
sqlite3_config(SQLITE_CONFIG_URI, 1); |
|
|
20958 |
sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
|
|
20959 |
sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
|
|
20960 |
sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
|
|
20961 |
sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
|
|
20962 |
} |
|
|
20963 |
|
|
|
20964 |
/* |
|
|
20965 |
** Output text to the console in a font that attracts extra attention. |
|
|
20966 |
*/ |
|
|
20967 |
#ifdef _WIN32 |
|
|
20968 |
static void printBold(const char *zText){ |
|
|
20969 |
#if !SQLITE_OS_WINRT |
|
|
20970 |
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
|
|
20971 |
CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
|
|
20972 |
GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
|
|
20973 |
SetConsoleTextAttribute(out, |
|
|
20974 |
FOREGROUND_RED|FOREGROUND_INTENSITY |
|
|
20975 |
); |
|
|
20976 |
#endif |
|
|
20977 |
printf("%s", zText); |
|
|
20978 |
#if !SQLITE_OS_WINRT |
|
|
20979 |
SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
|
|
20980 |
#endif |
|
|
20981 |
} |
|
|
20982 |
#else |
|
|
20983 |
static void printBold(const char *zText){ |
|
|
20984 |
printf("\033[1m%s\033[0m", zText); |
|
|
20985 |
} |
|
|
20986 |
#endif |
|
|
20987 |
|
|
|
20988 |
/* |
|
|
20989 |
** Get the argument to an --option. Throw an error and die if no argument |
|
|
20990 |
** is available. |
|
|
20991 |
*/ |
|
|
20992 |
static char *cmdline_option_value(int argc, char **argv, int i){ |
|
|
20993 |
if( i==argc ){ |
|
|
20994 |
utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
|
|
20995 |
argv[0], argv[argc-1]); |
|
|
20996 |
exit(1); |
|
|
20997 |
} |
|
|
20998 |
return argv[i]; |
|
|
20999 |
} |
|
|
21000 |
|
|
|
21001 |
#ifndef SQLITE_SHELL_IS_UTF8 |
|
|
21002 |
# if (defined(_WIN32) || defined(WIN32)) \ |
|
|
21003 |
&& (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__))) |
|
|
21004 |
# define SQLITE_SHELL_IS_UTF8 (0) |
|
|
21005 |
# else |
|
|
21006 |
# define SQLITE_SHELL_IS_UTF8 (1) |
|
|
21007 |
# endif |
|
|
21008 |
#endif |
|
|
21009 |
|
8931 |
turbocat |
21010 |
|
8760 |
turbocat |
21011 |
#if SQLITE_SHELL_IS_UTF8 |
|
|
21012 |
int SQLITE_CDECL main(int argc, char **argv){ |
|
|
21013 |
#else |
|
|
21014 |
int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
|
|
21015 |
char **argv; |
|
|
21016 |
#endif |
8931 |
turbocat |
21017 |
con_init_opt(-1,-1,-1,-1, "SQLite3"); |
8760 |
turbocat |
21018 |
char *zErrMsg = 0; |
|
|
21019 |
ShellState data; |
|
|
21020 |
const char *zInitFile = 0; |
|
|
21021 |
int i; |
|
|
21022 |
int rc = 0; |
|
|
21023 |
int warnInmemoryDb = 0; |
|
|
21024 |
int readStdin = 1; |
|
|
21025 |
int nCmd = 0; |
|
|
21026 |
char **azCmd = 0; |
|
|
21027 |
const char *zVfs = 0; /* Value of -vfs command-line option */ |
|
|
21028 |
#if !SQLITE_SHELL_IS_UTF8 |
|
|
21029 |
char **argvToFree = 0; |
|
|
21030 |
int argcToFree = 0; |
|
|
21031 |
#endif |
|
|
21032 |
|
|
|
21033 |
setBinaryMode(stdin, 0); |
|
|
21034 |
setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
|
|
21035 |
stdin_is_interactive = isatty(0); |
|
|
21036 |
stdout_is_console = isatty(1); |
|
|
21037 |
|
|
|
21038 |
#ifdef SQLITE_DEBUG |
|
|
21039 |
registerOomSimulator(); |
|
|
21040 |
#endif |
|
|
21041 |
|
|
|
21042 |
#if !defined(_WIN32_WCE) && ! defined(_KOLIBRI) |
|
|
21043 |
if( getenv("SQLITE_DEBUG_BREAK") ){ |
|
|
21044 |
if( isatty(0) && isatty(2) ){ |
|
|
21045 |
fprintf(stderr, |
|
|
21046 |
"attach debugger to process %d and press any key to continue.\n", |
|
|
21047 |
GETPID()); |
|
|
21048 |
fgetc(stdin); |
|
|
21049 |
}else{ |
|
|
21050 |
#if defined(_WIN32) || defined(WIN32) |
|
|
21051 |
#if SQLITE_OS_WINRT |
|
|
21052 |
__debugbreak(); |
|
|
21053 |
#else |
|
|
21054 |
DebugBreak(); |
|
|
21055 |
#endif |
|
|
21056 |
#elif defined(SIGTRAP) |
|
|
21057 |
raise(SIGTRAP); |
|
|
21058 |
#endif |
|
|
21059 |
} |
|
|
21060 |
} |
|
|
21061 |
#endif |
|
|
21062 |
|
|
|
21063 |
#if USE_SYSTEM_SQLITE+0!=1 |
|
|
21064 |
if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ |
|
|
21065 |
utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", |
|
|
21066 |
sqlite3_sourceid(), SQLITE_SOURCE_ID); |
|
|
21067 |
exit(1); |
|
|
21068 |
} |
|
|
21069 |
#endif |
|
|
21070 |
main_init(&data); |
|
|
21071 |
|
|
|
21072 |
/* On Windows, we must translate command-line arguments into UTF-8. |
|
|
21073 |
** The SQLite memory allocator subsystem has to be enabled in order to |
|
|
21074 |
** do this. But we want to run an sqlite3_shutdown() afterwards so that |
|
|
21075 |
** subsequent sqlite3_config() calls will work. So copy all results into |
|
|
21076 |
** memory that does not come from the SQLite memory allocator. |
|
|
21077 |
*/ |
|
|
21078 |
#if !SQLITE_SHELL_IS_UTF8 |
|
|
21079 |
sqlite3_initialize(); |
|
|
21080 |
argvToFree = malloc(sizeof(argv[0])*argc*2); |
|
|
21081 |
argcToFree = argc; |
|
|
21082 |
argv = argvToFree + argc; |
|
|
21083 |
if( argv==0 ) shell_out_of_memory(); |
|
|
21084 |
for(i=0; i |
|
|
21085 |
char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); |
|
|
21086 |
int n; |
|
|
21087 |
if( z==0 ) shell_out_of_memory(); |
|
|
21088 |
n = (int)strlen(z); |
|
|
21089 |
argv[i] = malloc( n+1 ); |
|
|
21090 |
if( argv[i]==0 ) shell_out_of_memory(); |
|
|
21091 |
memcpy(argv[i], z, n+1); |
|
|
21092 |
argvToFree[i] = argv[i]; |
|
|
21093 |
sqlite3_free(z); |
|
|
21094 |
} |
|
|
21095 |
sqlite3_shutdown(); |
|
|
21096 |
#endif |
|
|
21097 |
|
|
|
21098 |
assert( argc>=1 && argv && argv[0] ); |
|
|
21099 |
Argv0 = argv[0]; |
|
|
21100 |
|
|
|
21101 |
/* Make sure we have a valid signal handler early, before anything |
|
|
21102 |
** else is done. |
|
|
21103 |
*/ |
|
|
21104 |
#ifdef SIGINT |
|
|
21105 |
signal(SIGINT, interrupt_handler); |
|
|
21106 |
#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) |
|
|
21107 |
SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); |
|
|
21108 |
#endif |
|
|
21109 |
|
|
|
21110 |
#ifdef SQLITE_SHELL_DBNAME_PROC |
|
|
21111 |
{ |
|
|
21112 |
/* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
|
|
21113 |
** of a C-function that will provide the name of the database file. Use |
|
|
21114 |
** this compile-time option to embed this shell program in larger |
|
|
21115 |
** applications. */ |
|
|
21116 |
extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
|
|
21117 |
SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
|
|
21118 |
warnInmemoryDb = 0; |
|
|
21119 |
} |
|
|
21120 |
#endif |
|
|
21121 |
|
|
|
21122 |
/* Do an initial pass through the command-line argument to locate |
|
|
21123 |
** the name of the database file, the name of the initialization file, |
|
|
21124 |
** the size of the alternative malloc heap, |
|
|
21125 |
** and the first command to execute. |
|
|
21126 |
*/ |
|
|
21127 |
verify_uninitialized(); |
|
|
21128 |
for(i=1; i |
|
|
21129 |
char *z; |
|
|
21130 |
z = argv[i]; |
|
|
21131 |
if( z[0]!='-' ){ |
|
|
21132 |
if( data.zDbFilename==0 ){ |
|
|
21133 |
data.zDbFilename = z; |
|
|
21134 |
}else{ |
|
|
21135 |
/* Excesss arguments are interpreted as SQL (or dot-commands) and |
|
|
21136 |
** mean that nothing is read from stdin */ |
|
|
21137 |
readStdin = 0; |
|
|
21138 |
nCmd++; |
|
|
21139 |
azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
|
|
21140 |
if( azCmd==0 ) shell_out_of_memory(); |
|
|
21141 |
azCmd[nCmd-1] = z; |
|
|
21142 |
} |
|
|
21143 |
} |
|
|
21144 |
if( z[1]=='-' ) z++; |
|
|
21145 |
if( strcmp(z,"-separator")==0 |
|
|
21146 |
|| strcmp(z,"-nullvalue")==0 |
|
|
21147 |
|| strcmp(z,"-newline")==0 |
|
|
21148 |
|| strcmp(z,"-cmd")==0 |
|
|
21149 |
){ |
|
|
21150 |
(void)cmdline_option_value(argc, argv, ++i); |
|
|
21151 |
}else if( strcmp(z,"-init")==0 ){ |
|
|
21152 |
zInitFile = cmdline_option_value(argc, argv, ++i); |
|
|
21153 |
}else if( strcmp(z,"-batch")==0 ){ |
|
|
21154 |
/* Need to check for batch mode here to so we can avoid printing |
|
|
21155 |
** informational messages (like from process_sqliterc) before |
|
|
21156 |
** we do the actual processing of arguments later in a second pass. |
|
|
21157 |
*/ |
|
|
21158 |
stdin_is_interactive = 0; |
|
|
21159 |
}else if( strcmp(z,"-heap")==0 ){ |
|
|
21160 |
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
|
|
21161 |
const char *zSize; |
|
|
21162 |
sqlite3_int64 szHeap; |
|
|
21163 |
|
|
|
21164 |
zSize = cmdline_option_value(argc, argv, ++i); |
|
|
21165 |
szHeap = integerValue(zSize); |
|
|
21166 |
if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
|
|
21167 |
sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
|
|
21168 |
#else |
|
|
21169 |
(void)cmdline_option_value(argc, argv, ++i); |
|
|
21170 |
#endif |
|
|
21171 |
}else if( strcmp(z,"-pagecache")==0 ){ |
|
|
21172 |
sqlite3_int64 n, sz; |
|
|
21173 |
sz = integerValue(cmdline_option_value(argc,argv,++i)); |
|
|
21174 |
if( sz>70000 ) sz = 70000; |
|
|
21175 |
if( sz<0 ) sz = 0; |
|
|
21176 |
n = integerValue(cmdline_option_value(argc,argv,++i)); |
|
|
21177 |
if( sz>0 && n>0 && 0xffffffffffffLL/sz |
|
|
21178 |
n = 0xffffffffffffLL/sz; |
|
|
21179 |
} |
|
|
21180 |
sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
|
|
21181 |
(n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
|
|
21182 |
data.shellFlgs |= SHFLG_Pagecache; |
|
|
21183 |
}else if( strcmp(z,"-lookaside")==0 ){ |
|
|
21184 |
int n, sz; |
|
|
21185 |
sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
|
|
21186 |
if( sz<0 ) sz = 0; |
|
|
21187 |
n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
|
|
21188 |
if( n<0 ) n = 0; |
|
|
21189 |
sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
|
|
21190 |
if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
|
|
21191 |
#ifdef SQLITE_ENABLE_VFSTRACE |
|
|
21192 |
}else if( strcmp(z,"-vfstrace")==0 ){ |
|
|
21193 |
extern int vfstrace_register( |
|
|
21194 |
const char *zTraceName, |
|
|
21195 |
const char *zOldVfsName, |
|
|
21196 |
int (*xOut)(const char*,void*), |
|
|
21197 |
void *pOutArg, |
|
|
21198 |
int makeDefault |
|
|
21199 |
); |
|
|
21200 |
vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); |
|
|
21201 |
#endif |
|
|
21202 |
#ifdef SQLITE_ENABLE_MULTIPLEX |
|
|
21203 |
}else if( strcmp(z,"-multiplex")==0 ){ |
|
|
21204 |
extern int sqlite3_multiple_initialize(const char*,int); |
|
|
21205 |
sqlite3_multiplex_initialize(0, 1); |
|
|
21206 |
#endif |
|
|
21207 |
}else if( strcmp(z,"-mmap")==0 ){ |
|
|
21208 |
sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
|
|
21209 |
sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
|
|
21210 |
#ifdef SQLITE_ENABLE_SORTER_REFERENCES |
|
|
21211 |
}else if( strcmp(z,"-sorterref")==0 ){ |
|
|
21212 |
sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
|
|
21213 |
sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz); |
|
|
21214 |
#endif |
|
|
21215 |
}else if( strcmp(z,"-vfs")==0 ){ |
|
|
21216 |
zVfs = cmdline_option_value(argc, argv, ++i); |
|
|
21217 |
#ifdef SQLITE_HAVE_ZLIB |
|
|
21218 |
}else if( strcmp(z,"-zip")==0 ){ |
|
|
21219 |
data.openMode = SHELL_OPEN_ZIPFILE; |
|
|
21220 |
#endif |
|
|
21221 |
}else if( strcmp(z,"-append")==0 ){ |
|
|
21222 |
data.openMode = SHELL_OPEN_APPENDVFS; |
|
|
21223 |
#ifdef SQLITE_ENABLE_DESERIALIZE |
|
|
21224 |
}else if( strcmp(z,"-deserialize")==0 ){ |
|
|
21225 |
data.openMode = SHELL_OPEN_DESERIALIZE; |
|
|
21226 |
}else if( strcmp(z,"-maxsize")==0 && i+1 |
|
|
21227 |
data.szMax = integerValue(argv[++i]); |
|
|
21228 |
#endif |
|
|
21229 |
}else if( strcmp(z,"-readonly")==0 ){ |
|
|
21230 |
data.openMode = SHELL_OPEN_READONLY; |
|
|
21231 |
}else if( strcmp(z,"-nofollow")==0 ){ |
|
|
21232 |
data.openFlags = SQLITE_OPEN_NOFOLLOW; |
|
|
21233 |
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
|
|
21234 |
}else if( strncmp(z, "-A",2)==0 ){ |
|
|
21235 |
/* All remaining command-line arguments are passed to the ".archive" |
|
|
21236 |
** command, so ignore them */ |
|
|
21237 |
break; |
|
|
21238 |
#endif |
|
|
21239 |
}else if( strcmp(z, "-memtrace")==0 ){ |
|
|
21240 |
sqlite3MemTraceActivate(stderr); |
|
|
21241 |
}else if( strcmp(z,"-bail")==0 ){ |
|
|
21242 |
bail_on_error = 1; |
|
|
21243 |
} |
|
|
21244 |
} |
|
|
21245 |
verify_uninitialized(); |
|
|
21246 |
|
|
|
21247 |
|
|
|
21248 |
#ifdef SQLITE_SHELL_INIT_PROC |
|
|
21249 |
{ |
|
|
21250 |
/* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name |
|
|
21251 |
** of a C-function that will perform initialization actions on SQLite that |
|
|
21252 |
** occur just before or after sqlite3_initialize(). Use this compile-time |
|
|
21253 |
** option to embed this shell program in larger applications. */ |
|
|
21254 |
extern void SQLITE_SHELL_INIT_PROC(void); |
|
|
21255 |
SQLITE_SHELL_INIT_PROC(); |
|
|
21256 |
} |
|
|
21257 |
#else |
|
|
21258 |
/* All the sqlite3_config() calls have now been made. So it is safe |
|
|
21259 |
** to call sqlite3_initialize() and process any command line -vfs option. */ |
|
|
21260 |
sqlite3_initialize(); |
|
|
21261 |
#endif |
|
|
21262 |
|
|
|
21263 |
if( zVfs ){ |
|
|
21264 |
sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs); |
|
|
21265 |
if( pVfs ){ |
|
|
21266 |
sqlite3_vfs_register(pVfs, 1); |
|
|
21267 |
}else{ |
|
|
21268 |
utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
|
|
21269 |
exit(1); |
|
|
21270 |
} |
|
|
21271 |
} |
|
|
21272 |
|
|
|
21273 |
if( data.zDbFilename==0 ){ |
|
|
21274 |
#ifndef SQLITE_OMIT_MEMORYDB |
|
|
21275 |
data.zDbFilename = ":memory:"; |
|
|
21276 |
warnInmemoryDb = argc==1; |
|
|
21277 |
#else |
|
|
21278 |
utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
|
|
21279 |
return 1; |
|
|
21280 |
#endif |
|
|
21281 |
} |
|
|
21282 |
data.out = stdout; |
|
|
21283 |
sqlite3_appendvfs_init(0,0,0); |
|
|
21284 |
|
|
|
21285 |
/* Go ahead and open the database file if it already exists. If the |
|
|
21286 |
** file does not exist, delay opening it. This prevents empty database |
|
|
21287 |
** files from being created if a user mistypes the database name argument |
|
|
21288 |
** to the sqlite command-line tool. |
|
|
21289 |
*/ |
|
|
21290 |
if( access(data.zDbFilename, 0)==0 ){ |
|
|
21291 |
open_db(&data, 0); |
|
|
21292 |
} |
|
|
21293 |
|
|
|
21294 |
/* Process the initialization file if there is one. If no -init option |
|
|
21295 |
** is given on the command line, look for a file named ~/.sqliterc and |
|
|
21296 |
** try to process it. |
|
|
21297 |
*/ |
|
|
21298 |
process_sqliterc(&data,zInitFile); |
|
|
21299 |
|
|
|
21300 |
/* Make a second pass through the command-line argument and set |
|
|
21301 |
** options. This second pass is delayed until after the initialization |
|
|
21302 |
** file is processed so that the command-line arguments will override |
|
|
21303 |
** settings in the initialization file. |
|
|
21304 |
*/ |
|
|
21305 |
for(i=1; i |
|
|
21306 |
char *z = argv[i]; |
|
|
21307 |
if( z[0]!='-' ) continue; |
|
|
21308 |
if( z[1]=='-' ){ z++; } |
|
|
21309 |
if( strcmp(z,"-init")==0 ){ |
|
|
21310 |
i++; |
|
|
21311 |
}else if( strcmp(z,"-html")==0 ){ |
|
|
21312 |
data.mode = MODE_Html; |
|
|
21313 |
}else if( strcmp(z,"-list")==0 ){ |
|
|
21314 |
data.mode = MODE_List; |
|
|
21315 |
}else if( strcmp(z,"-quote")==0 ){ |
|
|
21316 |
data.mode = MODE_Quote; |
|
|
21317 |
sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma); |
|
|
21318 |
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); |
|
|
21319 |
}else if( strcmp(z,"-line")==0 ){ |
|
|
21320 |
data.mode = MODE_Line; |
|
|
21321 |
}else if( strcmp(z,"-column")==0 ){ |
|
|
21322 |
data.mode = MODE_Column; |
|
|
21323 |
}else if( strcmp(z,"-json")==0 ){ |
|
|
21324 |
data.mode = MODE_Json; |
|
|
21325 |
}else if( strcmp(z,"-markdown")==0 ){ |
|
|
21326 |
data.mode = MODE_Markdown; |
|
|
21327 |
}else if( strcmp(z,"-table")==0 ){ |
|
|
21328 |
data.mode = MODE_Table; |
|
|
21329 |
}else if( strcmp(z,"-box")==0 ){ |
|
|
21330 |
data.mode = MODE_Box; |
|
|
21331 |
}else if( strcmp(z,"-csv")==0 ){ |
|
|
21332 |
data.mode = MODE_Csv; |
|
|
21333 |
memcpy(data.colSeparator,",",2); |
|
|
21334 |
#ifdef SQLITE_HAVE_ZLIB |
|
|
21335 |
}else if( strcmp(z,"-zip")==0 ){ |
|
|
21336 |
data.openMode = SHELL_OPEN_ZIPFILE; |
|
|
21337 |
#endif |
|
|
21338 |
}else if( strcmp(z,"-append")==0 ){ |
|
|
21339 |
data.openMode = SHELL_OPEN_APPENDVFS; |
|
|
21340 |
#ifdef SQLITE_ENABLE_DESERIALIZE |
|
|
21341 |
}else if( strcmp(z,"-deserialize")==0 ){ |
|
|
21342 |
data.openMode = SHELL_OPEN_DESERIALIZE; |
|
|
21343 |
}else if( strcmp(z,"-maxsize")==0 && i+1 |
|
|
21344 |
data.szMax = integerValue(argv[++i]); |
|
|
21345 |
#endif |
|
|
21346 |
}else if( strcmp(z,"-readonly")==0 ){ |
|
|
21347 |
data.openMode = SHELL_OPEN_READONLY; |
|
|
21348 |
}else if( strcmp(z,"-nofollow")==0 ){ |
|
|
21349 |
data.openFlags |= SQLITE_OPEN_NOFOLLOW; |
|
|
21350 |
}else if( strcmp(z,"-ascii")==0 ){ |
|
|
21351 |
data.mode = MODE_Ascii; |
|
|
21352 |
sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit); |
|
|
21353 |
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record); |
|
|
21354 |
}else if( strcmp(z,"-tabs")==0 ){ |
|
|
21355 |
data.mode = MODE_List; |
|
|
21356 |
sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab); |
|
|
21357 |
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); |
|
|
21358 |
}else if( strcmp(z,"-separator")==0 ){ |
|
|
21359 |
sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
|
|
21360 |
"%s",cmdline_option_value(argc,argv,++i)); |
|
|
21361 |
}else if( strcmp(z,"-newline")==0 ){ |
|
|
21362 |
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
|
|
21363 |
"%s",cmdline_option_value(argc,argv,++i)); |
|
|
21364 |
}else if( strcmp(z,"-nullvalue")==0 ){ |
|
|
21365 |
sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
|
|
21366 |
"%s",cmdline_option_value(argc,argv,++i)); |
|
|
21367 |
}else if( strcmp(z,"-header")==0 ){ |
|
|
21368 |
data.showHeader = 1; |
|
|
21369 |
}else if( strcmp(z,"-noheader")==0 ){ |
|
|
21370 |
data.showHeader = 0; |
|
|
21371 |
}else if( strcmp(z,"-echo")==0 ){ |
|
|
21372 |
ShellSetFlag(&data, SHFLG_Echo); |
|
|
21373 |
}else if( strcmp(z,"-eqp")==0 ){ |
|
|
21374 |
data.autoEQP = AUTOEQP_on; |
|
|
21375 |
}else if( strcmp(z,"-eqpfull")==0 ){ |
|
|
21376 |
data.autoEQP = AUTOEQP_full; |
|
|
21377 |
}else if( strcmp(z,"-stats")==0 ){ |
|
|
21378 |
data.statsOn = 1; |
|
|
21379 |
}else if( strcmp(z,"-scanstats")==0 ){ |
|
|
21380 |
data.scanstatsOn = 1; |
|
|
21381 |
}else if( strcmp(z,"-backslash")==0 ){ |
|
|
21382 |
/* Undocumented command-line option: -backslash |
|
|
21383 |
** Causes C-style backslash escapes to be evaluated in SQL statements |
|
|
21384 |
** prior to sending the SQL into SQLite. Useful for injecting |
|
|
21385 |
** crazy bytes in the middle of SQL statements for testing and debugging. |
|
|
21386 |
*/ |
|
|
21387 |
ShellSetFlag(&data, SHFLG_Backslash); |
|
|
21388 |
}else if( strcmp(z,"-bail")==0 ){ |
|
|
21389 |
/* No-op. The bail_on_error flag should already be set. */ |
|
|
21390 |
}else if( strcmp(z,"-version")==0 ){ |
|
|
21391 |
printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
|
|
21392 |
return 0; |
|
|
21393 |
}else if( strcmp(z,"-interactive")==0 ){ |
|
|
21394 |
stdin_is_interactive = 1; |
|
|
21395 |
}else if( strcmp(z,"-batch")==0 ){ |
|
|
21396 |
stdin_is_interactive = 0; |
|
|
21397 |
}else if( strcmp(z,"-heap")==0 ){ |
|
|
21398 |
i++; |
|
|
21399 |
}else if( strcmp(z,"-pagecache")==0 ){ |
|
|
21400 |
i+=2; |
|
|
21401 |
}else if( strcmp(z,"-lookaside")==0 ){ |
|
|
21402 |
i+=2; |
|
|
21403 |
}else if( strcmp(z,"-mmap")==0 ){ |
|
|
21404 |
i++; |
|
|
21405 |
}else if( strcmp(z,"-memtrace")==0 ){ |
|
|
21406 |
i++; |
|
|
21407 |
#ifdef SQLITE_ENABLE_SORTER_REFERENCES |
|
|
21408 |
}else if( strcmp(z,"-sorterref")==0 ){ |
|
|
21409 |
i++; |
|
|
21410 |
#endif |
|
|
21411 |
}else if( strcmp(z,"-vfs")==0 ){ |
|
|
21412 |
i++; |
|
|
21413 |
#ifdef SQLITE_ENABLE_VFSTRACE |
|
|
21414 |
}else if( strcmp(z,"-vfstrace")==0 ){ |
|
|
21415 |
i++; |
|
|
21416 |
#endif |
|
|
21417 |
#ifdef SQLITE_ENABLE_MULTIPLEX |
|
|
21418 |
}else if( strcmp(z,"-multiplex")==0 ){ |
|
|
21419 |
i++; |
|
|
21420 |
#endif |
|
|
21421 |
}else if( strcmp(z,"-help")==0 ){ |
|
|
21422 |
usage(1); |
|
|
21423 |
}else if( strcmp(z,"-cmd")==0 ){ |
|
|
21424 |
/* Run commands that follow -cmd first and separately from commands |
|
|
21425 |
** that simply appear on the command-line. This seems goofy. It would |
|
|
21426 |
** be better if all commands ran in the order that they appear. But |
|
|
21427 |
** we retain the goofy behavior for historical compatibility. */ |
|
|
21428 |
if( i==argc-1 ) break; |
|
|
21429 |
z = cmdline_option_value(argc,argv,++i); |
|
|
21430 |
if( z[0]=='.' ){ |
|
|
21431 |
rc = do_meta_command(z, &data); |
|
|
21432 |
if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
|
|
21433 |
}else{ |
|
|
21434 |
open_db(&data, 0); |
|
|
21435 |
rc = shell_exec(&data, z, &zErrMsg); |
|
|
21436 |
if( zErrMsg!=0 ){ |
|
|
21437 |
utf8_printf(stderr,"Error: %s\n", zErrMsg); |
|
|
21438 |
if( bail_on_error ) return rc!=0 ? rc : 1; |
|
|
21439 |
}else if( rc!=0 ){ |
|
|
21440 |
utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
|
|
21441 |
if( bail_on_error ) return rc; |
|
|
21442 |
} |
|
|
21443 |
} |
|
|
21444 |
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) |
|
|
21445 |
}else if( strncmp(z, "-A", 2)==0 ){ |
|
|
21446 |
if( nCmd>0 ){ |
|
|
21447 |
utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands" |
|
|
21448 |
" with \"%s\"\n", z); |
|
|
21449 |
return 1; |
|
|
21450 |
} |
|
|
21451 |
open_db(&data, OPEN_DB_ZIPFILE); |
|
|
21452 |
if( z[2] ){ |
|
|
21453 |
argv[i] = &z[2]; |
|
|
21454 |
arDotCommand(&data, 1, argv+(i-1), argc-(i-1)); |
|
|
21455 |
}else{ |
|
|
21456 |
arDotCommand(&data, 1, argv+i, argc-i); |
|
|
21457 |
} |
|
|
21458 |
readStdin = 0; |
|
|
21459 |
break; |
|
|
21460 |
#endif |
|
|
21461 |
}else{ |
|
|
21462 |
utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
|
|
21463 |
raw_printf(stderr,"Use -help for a list of options.\n"); |
|
|
21464 |
return 1; |
|
|
21465 |
} |
|
|
21466 |
data.cMode = data.mode; |
|
|
21467 |
} |
|
|
21468 |
|
|
|
21469 |
if( !readStdin ){ |
|
|
21470 |
/* Run all arguments that do not begin with '-' as if they were separate |
|
|
21471 |
** command-line inputs, except for the argToSkip argument which contains |
|
|
21472 |
** the database filename. |
|
|
21473 |
*/ |
|
|
21474 |
for(i=0; i |
|
|
21475 |
if( azCmd[i][0]=='.' ){ |
|
|
21476 |
rc = do_meta_command(azCmd[i], &data); |
|
|
21477 |
if( rc ){ |
|
|
21478 |
free(azCmd); |
|
|
21479 |
return rc==2 ? 0 : rc; |
|
|
21480 |
} |
|
|
21481 |
}else{ |
|
|
21482 |
open_db(&data, 0); |
|
|
21483 |
rc = shell_exec(&data, azCmd[i], &zErrMsg); |
|
|
21484 |
if( zErrMsg || rc ){ |
|
|
21485 |
if( zErrMsg!=0 ){ |
|
|
21486 |
utf8_printf(stderr,"Error: %s\n", zErrMsg); |
|
|
21487 |
}else{ |
|
|
21488 |
utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
|
|
21489 |
} |
|
|
21490 |
sqlite3_free(zErrMsg); |
|
|
21491 |
free(azCmd); |
|
|
21492 |
return rc!=0 ? rc : 1; |
|
|
21493 |
} |
|
|
21494 |
} |
|
|
21495 |
} |
|
|
21496 |
}else{ |
|
|
21497 |
/* Run commands received from standard input |
|
|
21498 |
*/ |
|
|
21499 |
if( stdin_is_interactive ){ |
|
|
21500 |
char *zHome; |
|
|
21501 |
char *zHistory; |
|
|
21502 |
int nHistory; |
|
|
21503 |
printf( |
|
|
21504 |
"SQLite version %s %.19s\n" /*extra-version-info*/ |
|
|
21505 |
"Enter \".help\" for usage hints.\n", |
|
|
21506 |
sqlite3_libversion(), sqlite3_sourceid() |
|
|
21507 |
); |
|
|
21508 |
if( warnInmemoryDb ){ |
|
|
21509 |
printf("Connected to a "); |
|
|
21510 |
printBold("transient in-memory database"); |
|
|
21511 |
printf(".\nUse \".open FILENAME\" to reopen on a " |
|
|
21512 |
"persistent database.\n"); |
|
|
21513 |
} |
|
|
21514 |
zHistory = getenv("SQLITE_HISTORY"); |
|
|
21515 |
if( zHistory ){ |
|
|
21516 |
zHistory = strdup(zHistory); |
|
|
21517 |
}else if( (zHome = find_home_dir(0))!=0 ){ |
|
|
21518 |
nHistory = strlen30(zHome) + 20; |
|
|
21519 |
if( (zHistory = malloc(nHistory))!=0 ){ |
|
|
21520 |
sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
|
|
21521 |
} |
|
|
21522 |
} |
|
|
21523 |
if( zHistory ){ shell_read_history(zHistory); } |
|
|
21524 |
#if HAVE_READLINE || HAVE_EDITLINE |
|
|
21525 |
rl_attempted_completion_function = readline_completion; |
|
|
21526 |
#elif HAVE_LINENOISE |
|
|
21527 |
linenoiseSetCompletionCallback(linenoise_completion); |
|
|
21528 |
#endif |
|
|
21529 |
data.in = 0; |
|
|
21530 |
rc = process_input(&data); |
|
|
21531 |
if( zHistory ){ |
|
|
21532 |
shell_stifle_history(2000); |
|
|
21533 |
shell_write_history(zHistory); |
|
|
21534 |
free(zHistory); |
|
|
21535 |
} |
|
|
21536 |
}else{ |
|
|
21537 |
data.in = stdin; |
|
|
21538 |
rc = process_input(&data); |
|
|
21539 |
} |
|
|
21540 |
} |
|
|
21541 |
free(azCmd); |
|
|
21542 |
set_table_name(&data, 0); |
|
|
21543 |
if( data.db ){ |
|
|
21544 |
session_close_all(&data); |
|
|
21545 |
close_db(data.db); |
|
|
21546 |
} |
|
|
21547 |
sqlite3_free(data.zFreeOnClose); |
|
|
21548 |
find_home_dir(1); |
|
|
21549 |
output_reset(&data); |
|
|
21550 |
data.doXdgOpen = 0; |
|
|
21551 |
clearTempFile(&data); |
|
|
21552 |
#if !SQLITE_SHELL_IS_UTF8 |
|
|
21553 |
for(i=0; i |
|
|
21554 |
free(argvToFree); |
|
|
21555 |
#endif |
|
|
21556 |
free(data.colWidth); |
|
|
21557 |
/* Clear the global data structure so that valgrind will detect memory |
|
|
21558 |
** leaks */ |
|
|
21559 |
memset(&data, 0, sizeof(data)); |
|
|
21560 |
return rc; |
|
|
21561 |
}0>0>0>=ArraySize(azArg)>--->2>2>0>0>1>2>2>2>=1>=1>2>0>0>2>=nCol>0>0>0>0)!=bIntkey>0>0)>=(SELECT>=10>(nArg-1)>=15>(nArg-1)>=12>=17>=pTab->2;>=strlen30(zLong)>(n-1)>=1>1>0>=c2>8)><8)>16)><16)>24)><24)>8)><8)>2;>='9';>3)><3)>='7'>3)><3)>='7'>='7'>>><>><>16) |
|