version 1.3, 2013/01/28 01:47:36
|
version 1.3.14.1, 2016/09/29 14:38:29
|
Line 18
|
Line 18
|
#endif |
#endif |
|
|
/* |
/* |
|
** If requested, include the SQLite compiler options file for MSVC. |
|
*/ |
|
#if defined(INCLUDE_MSVC_H) |
|
#include "msvc.h" |
|
#endif |
|
|
|
/* |
|
** No support for loadable extensions in VxWorks. |
|
*/ |
|
#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION |
|
# define SQLITE_OMIT_LOAD_EXTENSION 1 |
|
#endif |
|
|
|
/* |
** Enable large-file support for fopen() and friends on unix. |
** Enable large-file support for fopen() and friends on unix. |
*/ |
*/ |
#ifndef SQLITE_DISABLE_LFS |
#ifndef SQLITE_DISABLE_LFS |
Line 33
|
Line 47
|
#include <stdio.h> |
#include <stdio.h> |
#include <assert.h> |
#include <assert.h> |
#include "sqlite3.h" |
#include "sqlite3.h" |
|
#if SQLITE_USER_AUTHENTICATION |
|
# include "sqlite3userauth.h" |
|
#endif |
#include <ctype.h> |
#include <ctype.h> |
#include <stdarg.h> |
#include <stdarg.h> |
|
|
Line 45
|
Line 62
|
# include <sys/types.h> |
# include <sys/types.h> |
#endif |
#endif |
|
|
#ifdef HAVE_EDITLINE | #if HAVE_READLINE |
# include <editline/editline.h> | |
#endif | |
#if defined(HAVE_READLINE) && HAVE_READLINE==1 | |
# include <readline/readline.h> |
# include <readline/readline.h> |
# include <readline/history.h> |
# include <readline/history.h> |
#endif |
#endif |
#if !defined(HAVE_EDITLINE) && (!defined(HAVE_READLINE) || HAVE_READLINE!=1) | |
# define readline(p) local_getline(p,stdin,0) | #if HAVE_EDITLINE |
# define add_history(X) | # include <editline/readline.h> |
# define read_history(X) | |
# define write_history(X) | |
# define stifle_history(X) | |
#endif |
#endif |
|
|
|
#if HAVE_EDITLINE || HAVE_READLINE |
|
|
|
# define shell_add_history(X) add_history(X) |
|
# define shell_read_history(X) read_history(X) |
|
# define shell_write_history(X) write_history(X) |
|
# define shell_stifle_history(X) stifle_history(X) |
|
# define shell_readline(X) readline(X) |
|
|
|
#elif HAVE_LINENOISE |
|
|
|
# include "linenoise.h" |
|
# define shell_add_history(X) linenoiseHistoryAdd(X) |
|
# define shell_read_history(X) linenoiseHistoryLoad(X) |
|
# define shell_write_history(X) linenoiseHistorySave(X) |
|
# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) |
|
# define shell_readline(X) linenoise(X) |
|
|
|
#else |
|
|
|
# define shell_read_history(X) |
|
# define shell_write_history(X) |
|
# define shell_stifle_history(X) |
|
|
|
# define SHELL_USE_LOCAL_GETLINE 1 |
|
#endif |
|
|
|
|
#if defined(_WIN32) || defined(WIN32) |
#if defined(_WIN32) || defined(WIN32) |
# include <io.h> |
# include <io.h> |
#define isatty(h) _isatty(h) | # include <fcntl.h> |
#define access(f,m) _access((f),(m)) | # define isatty(h) _isatty(h) |
#undef popen | # ifndef access |
#define popen(a,b) _popen((a),(b)) | # define access(f,m) _access((f),(m)) |
#undef pclose | # endif |
#define pclose(x) _pclose(x) | # undef popen |
| # define popen _popen |
| # undef pclose |
| # define pclose _pclose |
#else |
#else |
/* Make sure isatty() has a prototype. | /* Make sure isatty() has a prototype. */ |
*/ | extern int isatty(int); |
extern int isatty(int); | |
| # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| /* popen and pclose are not C89 functions and so are |
| ** sometimes omitted from the <stdio.h> header */ |
| extern FILE *popen(const char*,const char*); |
| extern int pclose(FILE*); |
| # else |
| # define SQLITE_OMIT_POPEN 1 |
| # endif |
#endif |
#endif |
|
|
#if defined(_WIN32_WCE) |
#if defined(_WIN32_WCE) |
Line 82 extern int isatty(int);
|
Line 131 extern int isatty(int);
|
#define isatty(x) 1 |
#define isatty(x) 1 |
#endif |
#endif |
|
|
/* True if the timer is enabled */ |
|
static int enableTimer = 0; |
|
|
|
/* ctype macros that work with signed characters */ |
/* ctype macros that work with signed characters */ |
#define IsSpace(X) isspace((unsigned char)X) |
#define IsSpace(X) isspace((unsigned char)X) |
#define IsDigit(X) isdigit((unsigned char)X) |
#define IsDigit(X) isdigit((unsigned char)X) |
#define ToLower(X) (char)tolower((unsigned char)X) |
#define ToLower(X) (char)tolower((unsigned char)X) |
|
|
#if !defined(_WIN32) && !defined(WIN32) && !defined(_WRS_KERNEL) | #if defined(_WIN32) || defined(WIN32) |
| #include <windows.h> |
| |
| /* string conversion routines only needed on Win32 */ |
| extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); |
| extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int); |
| extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int); |
| #endif |
| |
| /* On Windows, we normally run with output mode of TEXT so that \n characters |
| ** are automatically translated into \r\n. However, this behavior needs |
| ** to be disabled in some cases (ex: when generating CSV output and when |
| ** rendering quoted strings that contain \n characters). The following |
| ** routines take care of that. |
| */ |
| #if defined(_WIN32) || defined(WIN32) |
| static void setBinaryMode(FILE *file, int isOutput){ |
| if( isOutput ) fflush(file); |
| _setmode(_fileno(file), _O_BINARY); |
| } |
| static void setTextMode(FILE *file, int isOutput){ |
| if( isOutput ) fflush(file); |
| _setmode(_fileno(file), _O_TEXT); |
| } |
| #else |
| # define setBinaryMode(X,Y) |
| # define setTextMode(X,Y) |
| #endif |
| |
| |
| /* True if the timer is enabled */ |
| static int enableTimer = 0; |
| |
| /* Return the current wall-clock time */ |
| static sqlite3_int64 timeOfDay(void){ |
| static sqlite3_vfs *clockVfs = 0; |
| sqlite3_int64 t; |
| if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
| if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
| clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| }else{ |
| double r; |
| clockVfs->xCurrentTime(clockVfs, &r); |
| t = (sqlite3_int64)(r*86400000.0); |
| } |
| return t; |
| } |
| |
| #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) |
#include <sys/time.h> |
#include <sys/time.h> |
#include <sys/resource.h> |
#include <sys/resource.h> |
|
|
|
/* VxWorks does not support getrusage() as far as we can determine */ |
|
#if defined(_WRS_KERNEL) || defined(__RTP__) |
|
struct rusage { |
|
struct timeval ru_utime; /* user CPU time used */ |
|
struct timeval ru_stime; /* system CPU time used */ |
|
}; |
|
#define getrusage(A,B) memset(B,0,sizeof(*B)) |
|
#endif |
|
|
/* Saved resource information for the beginning of an operation */ |
/* Saved resource information for the beginning of an operation */ |
static struct rusage sBegin; | static struct rusage sBegin; /* CPU time at start */ |
| static sqlite3_int64 iBegin; /* Wall-clock time at start */ |
|
|
/* |
/* |
** Begin timing an operation |
** Begin timing an operation |
Line 103 static struct rusage sBegin;
|
Line 207 static struct rusage sBegin;
|
static void beginTimer(void){ |
static void beginTimer(void){ |
if( enableTimer ){ |
if( enableTimer ){ |
getrusage(RUSAGE_SELF, &sBegin); |
getrusage(RUSAGE_SELF, &sBegin); |
|
iBegin = timeOfDay(); |
} |
} |
} |
} |
|
|
/* Return the difference of two time_structs in seconds */ |
/* Return the difference of two time_structs in seconds */ |
static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + | return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + |
(double)(pEnd->tv_sec - pStart->tv_sec); |
(double)(pEnd->tv_sec - pStart->tv_sec); |
} |
} |
|
|
Line 117 static double timeDiff(struct timeval *pStart, struct
|
Line 222 static double timeDiff(struct timeval *pStart, struct
|
*/ |
*/ |
static void endTimer(void){ |
static void endTimer(void){ |
if( enableTimer ){ |
if( enableTimer ){ |
|
sqlite3_int64 iEnd = timeOfDay(); |
struct rusage sEnd; |
struct rusage sEnd; |
getrusage(RUSAGE_SELF, &sEnd); |
getrusage(RUSAGE_SELF, &sEnd); |
printf("CPU Time: user %f sys %f\n", | printf("Run Time: real %.3f user %f sys %f\n", |
| (iEnd - iBegin)*0.001, |
timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
} |
} |
Line 131 static void endTimer(void){
|
Line 238 static void endTimer(void){
|
|
|
#elif (defined(_WIN32) || defined(WIN32)) |
#elif (defined(_WIN32) || defined(WIN32)) |
|
|
#include <windows.h> |
|
|
|
/* Saved resource information for the beginning of an operation */ |
/* Saved resource information for the beginning of an operation */ |
static HANDLE hProcess; |
static HANDLE hProcess; |
static FILETIME ftKernelBegin; |
static FILETIME ftKernelBegin; |
static FILETIME ftUserBegin; |
static FILETIME ftUserBegin; |
typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME); | static sqlite3_int64 ftWallBegin; |
| typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, |
| LPFILETIME, LPFILETIME); |
static GETPROCTIMES getProcessTimesAddr = NULL; |
static GETPROCTIMES getProcessTimesAddr = NULL; |
|
|
/* |
/* |
Line 148 static int hasTimer(void){
|
Line 255 static int hasTimer(void){
|
if( getProcessTimesAddr ){ |
if( getProcessTimesAddr ){ |
return 1; |
return 1; |
} else { |
} else { |
/* GetProcessTimes() isn't supported in WIN95 and some other Windows versions. | /* GetProcessTimes() isn't supported in WIN95 and some other Windows |
** See if the version we are running on has it, and if it does, save off | ** versions. See if the version we are running on has it, and if it |
** a pointer to it and the current process handle. | ** does, save off a pointer to it and the current process handle. |
*/ |
*/ |
hProcess = GetCurrentProcess(); |
hProcess = GetCurrentProcess(); |
if( hProcess ){ |
if( hProcess ){ |
HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); |
HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); |
if( NULL != hinstLib ){ |
if( NULL != hinstLib ){ |
getProcessTimesAddr = (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); | getProcessTimesAddr = |
| (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); |
if( NULL != getProcessTimesAddr ){ |
if( NULL != getProcessTimesAddr ){ |
return 1; |
return 1; |
} |
} |
FreeLibrary(hinstLib); | FreeLibrary(hinstLib); |
} |
} |
} |
} |
} |
} |
Line 173 static int hasTimer(void){
|
Line 281 static int hasTimer(void){
|
static void beginTimer(void){ |
static void beginTimer(void){ |
if( enableTimer && getProcessTimesAddr ){ |
if( enableTimer && getProcessTimesAddr ){ |
FILETIME ftCreation, ftExit; |
FILETIME ftCreation, ftExit; |
getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin); | getProcessTimesAddr(hProcess,&ftCreation,&ftExit, |
| &ftKernelBegin,&ftUserBegin); |
| ftWallBegin = timeOfDay(); |
} |
} |
} |
} |
|
|
Line 190 static double timeDiff(FILETIME *pStart, FILETIME *pEn
|
Line 300 static double timeDiff(FILETIME *pStart, FILETIME *pEn
|
static void endTimer(void){ |
static void endTimer(void){ |
if( enableTimer && getProcessTimesAddr){ |
if( enableTimer && getProcessTimesAddr){ |
FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd); | sqlite3_int64 ftWallEnd = timeOfDay(); |
printf("CPU Time: user %f sys %f\n", | getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); |
| printf("Run Time: real %.3f user %f sys %f\n", |
| (ftWallEnd - ftWallBegin)*0.001, |
timeDiff(&ftUserBegin, &ftUserEnd), |
timeDiff(&ftUserBegin, &ftUserEnd), |
timeDiff(&ftKernelBegin, &ftKernelEnd)); |
timeDiff(&ftKernelBegin, &ftKernelEnd)); |
} |
} |
Line 202 static void endTimer(void){
|
Line 314 static void endTimer(void){
|
#define HAS_TIMER hasTimer() |
#define HAS_TIMER hasTimer() |
|
|
#else |
#else |
#define BEGIN_TIMER | #define BEGIN_TIMER |
#define END_TIMER |
#define END_TIMER |
#define HAS_TIMER 0 |
#define HAS_TIMER 0 |
#endif |
#endif |
Line 225 static int bail_on_error = 0;
|
Line 337 static int bail_on_error = 0;
|
static int stdin_is_interactive = 1; |
static int stdin_is_interactive = 1; |
|
|
/* |
/* |
|
** On Windows systems we have to know if standard output is a console |
|
** in order to translate UTF-8 into MBCS. The following variable is |
|
** true if translation is required. |
|
*/ |
|
static int stdout_is_console = 1; |
|
|
|
/* |
** The following is the open SQLite database. We make a pointer |
** The following is the open SQLite database. We make a pointer |
** to this database a static variable so that it can be accessed |
** to this database a static variable so that it can be accessed |
** by the SIGINT handler to interrupt database processing. |
** by the SIGINT handler to interrupt database processing. |
*/ |
*/ |
static sqlite3 *db = 0; | static sqlite3 *globalDb = 0; |
|
|
/* |
/* |
** True if an interrupt (Control-C) has been received. |
** True if an interrupt (Control-C) has been received. |
Line 250 static char mainPrompt[20]; /* First line prompt.
|
Line 369 static char mainPrompt[20]; /* First line prompt.
|
static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
|
|
/* |
/* |
|
** Render output like fprintf(). Except, if the output is going to the |
|
** console and if this is running on a Windows machine, translate the |
|
** output from UTF-8 into MBCS. |
|
*/ |
|
#if defined(_WIN32) || defined(WIN32) |
|
void utf8_printf(FILE *out, const char *zFormat, ...){ |
|
va_list ap; |
|
va_start(ap, zFormat); |
|
if( stdout_is_console && (out==stdout || out==stderr) ){ |
|
char *z1 = sqlite3_vmprintf(zFormat, ap); |
|
char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0); |
|
sqlite3_free(z1); |
|
fputs(z2, out); |
|
sqlite3_free(z2); |
|
}else{ |
|
vfprintf(out, zFormat, ap); |
|
} |
|
va_end(ap); |
|
} |
|
#elif !defined(utf8_printf) |
|
# define utf8_printf fprintf |
|
#endif |
|
|
|
/* |
|
** Render output like fprintf(). This should not be used on anything that |
|
** includes string formatting (e.g. "%s"). |
|
*/ |
|
#if !defined(raw_printf) |
|
# define raw_printf fprintf |
|
#endif |
|
|
|
/* |
** Write I/O traces to the following stream. |
** Write I/O traces to the following stream. |
*/ |
*/ |
#ifdef SQLITE_ENABLE_IOTRACE |
#ifdef SQLITE_ENABLE_IOTRACE |
Line 263 static FILE *iotrace = 0;
|
Line 414 static FILE *iotrace = 0;
|
** is written to iotrace. |
** is written to iotrace. |
*/ |
*/ |
#ifdef SQLITE_ENABLE_IOTRACE |
#ifdef SQLITE_ENABLE_IOTRACE |
static void iotracePrintf(const char *zFormat, ...){ | static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ |
va_list ap; |
va_list ap; |
char *z; |
char *z; |
if( iotrace==0 ) return; |
if( iotrace==0 ) return; |
va_start(ap, zFormat); |
va_start(ap, zFormat); |
z = sqlite3_vmprintf(zFormat, ap); |
z = sqlite3_vmprintf(zFormat, ap); |
va_end(ap); |
va_end(ap); |
fprintf(iotrace, "%s", z); | utf8_printf(iotrace, "%s", z); |
sqlite3_free(z); |
sqlite3_free(z); |
} |
} |
#endif |
#endif |
Line 304 static int isNumber(const char *z, int *realnum){
|
Line 455 static int isNumber(const char *z, int *realnum){
|
} |
} |
|
|
/* |
/* |
** A global char* and an SQL function to access its current value | ** A global char* and an SQL function to access its current value |
** from within an SQL statement. This program used to use the | ** from within an SQL statement. This program used to use the |
** sqlite_exec_printf() API to substitue a string into an SQL statement. |
** sqlite_exec_printf() API to substitue a string into an SQL statement. |
** The correct way to do this with sqlite3 is to use the bind API, but |
** The correct way to do this with sqlite3 is to use the bind API, but |
** since the shell is built around the callback paradigm it would be a lot |
** since the shell is built around the callback paradigm it would be a lot |
Line 326 static void shellstaticFunc(
|
Line 477 static void shellstaticFunc(
|
|
|
|
|
/* |
/* |
|
** Compute a string length that is limited to what can be stored in |
|
** lower 30 bits of a 32-bit signed integer. |
|
*/ |
|
static int strlen30(const char *z){ |
|
const char *z2 = z; |
|
while( *z2 ){ z2++; } |
|
return 0x3fffffff & (int)(z2 - z); |
|
} |
|
|
|
/* |
** This routine reads a line of text from FILE in, stores |
** This routine reads a line of text from FILE in, stores |
** the text in memory obtained from malloc() and returns a pointer |
** the text in memory obtained from malloc() and returns a pointer |
** to the text. NULL is returned at end of file, or if malloc() |
** to the text. NULL is returned at end of file, or if malloc() |
** fails. |
** fails. |
** |
** |
** The interface is like "readline" but no command-line editing | ** If zLine is not NULL then it is a malloced buffer returned from |
** is done. | ** a previous call to this routine that may be reused. |
*/ |
*/ |
static char *local_getline(char *zPrompt, FILE *in, int csvFlag){ | static char *local_getline(char *zLine, FILE *in){ |
char *zLine; | int nLine = zLine==0 ? 0 : 100; |
int nLine; | int n = 0; |
int n; | |
int inQuote = 0; | |
|
|
if( zPrompt && *zPrompt ){ |
|
printf("%s",zPrompt); |
|
fflush(stdout); |
|
} |
|
nLine = 100; |
|
zLine = malloc( nLine ); |
|
if( zLine==0 ) return 0; |
|
n = 0; |
|
while( 1 ){ |
while( 1 ){ |
if( n+100>nLine ){ |
if( n+100>nLine ){ |
nLine = nLine*2 + 100; |
nLine = nLine*2 + 100; |
Line 362 static char *local_getline(char *zPrompt, FILE *in, in
|
Line 513 static char *local_getline(char *zPrompt, FILE *in, in
|
zLine[n] = 0; |
zLine[n] = 0; |
break; |
break; |
} |
} |
while( zLine[n] ){ | while( zLine[n] ) n++; |
if( zLine[n]=='"' ) inQuote = !inQuote; | if( n>0 && zLine[n-1]=='\n' ){ |
n++; | |
} | |
if( n>0 && zLine[n-1]=='\n' && (!inQuote || !csvFlag) ){ | |
n--; |
n--; |
if( n>0 && zLine[n-1]=='\r' ) n--; |
if( n>0 && zLine[n-1]=='\r' ) n--; |
zLine[n] = 0; |
zLine[n] = 0; |
break; |
break; |
} |
} |
} |
} |
zLine = realloc( zLine, n+1 ); | #if defined(_WIN32) || defined(WIN32) |
| /* For interactive input on Windows systems, translate the |
| ** multi-byte characterset characters into UTF-8. */ |
| if( stdin_is_interactive && in==stdin ){ |
| char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0); |
| if( zTrans ){ |
| int nTrans = strlen30(zTrans)+1; |
| if( nTrans>nLine ){ |
| zLine = realloc(zLine, nTrans); |
| if( zLine==0 ){ |
| sqlite3_free(zTrans); |
| return 0; |
| } |
| } |
| memcpy(zLine, zTrans, nTrans); |
| sqlite3_free(zTrans); |
| } |
| } |
| #endif /* defined(_WIN32) || defined(WIN32) */ |
return zLine; |
return zLine; |
} |
} |
|
|
/* |
/* |
** Retrieve a single line of input text. |
** Retrieve a single line of input text. |
** |
** |
** zPrior is a string of prior text retrieved. If not the empty | ** If in==0 then read from standard input and prompt before each line. |
** string, then issue a continuation prompt. | ** If isContinuation is true, then a continuation prompt is appropriate. |
| ** If isContinuation is zero, then the main prompt should be used. |
| ** |
| ** If zPrior is not NULL then it is a buffer from a prior call to this |
| ** routine that can be reused. |
| ** |
| ** The result is stored in space obtained from malloc() and must either |
| ** be freed by the caller or else passed back into this routine via the |
| ** zPrior argument for reuse. |
*/ |
*/ |
static char *one_input_line(const char *zPrior, FILE *in){ | static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ |
char *zPrompt; |
char *zPrompt; |
char *zResult; |
char *zResult; |
if( in!=0 ){ |
if( in!=0 ){ |
return local_getline(0, in, 0); | zResult = local_getline(zPrior, in); |
} | |
if( zPrior && zPrior[0] ){ | |
zPrompt = continuePrompt; | |
}else{ |
}else{ |
zPrompt = mainPrompt; | zPrompt = isContinuation ? continuePrompt : mainPrompt; |
} | #if SHELL_USE_LOCAL_GETLINE |
zResult = readline(zPrompt); | printf("%s", zPrompt); |
#if defined(HAVE_READLINE) && HAVE_READLINE==1 | fflush(stdout); |
if( zResult && *zResult ) add_history(zResult); | zResult = local_getline(zPrior, stdin); |
| #else |
| free(zPrior); |
| zResult = shell_readline(zPrompt); |
| if( zResult && *zResult ) shell_add_history(zResult); |
#endif |
#endif |
|
} |
return zResult; |
return zResult; |
} |
} |
|
|
struct previous_mode_data { | #if defined(SQLITE_ENABLE_SESSION) |
int valid; /* Is there legit data in here? */ | /* |
int mode; | ** State information for a single open session |
int showHeader; | */ |
int colWidth[100]; | typedef struct OpenSession OpenSession; |
| struct OpenSession { |
| char *zName; /* Symbolic name for this session */ |
| int nFilter; /* Number of xFilter rejection GLOB patterns */ |
| char **azFilter; /* Array of xFilter rejection GLOB patterns */ |
| sqlite3_session *p; /* The open session */ |
}; |
}; |
|
#endif |
|
|
/* |
/* |
** An pointer to an instance of this structure is passed from | ** Shell output mode information from before ".explain on", |
** the main program to the callback. This is used to communicate | ** saved so that it can be restored by ".explain off" |
** state and mode information. | |
*/ |
*/ |
struct callback_data { | typedef struct SavedModeInfo SavedModeInfo; |
| struct SavedModeInfo { |
| int valid; /* Is there legit data in here? */ |
| int mode; /* Mode prior to ".explain on" */ |
| int showHeader; /* The ".header" setting prior to ".explain on" */ |
| int colWidth[100]; /* Column widths prior to ".explain on" */ |
| }; |
| |
| /* |
| ** State information about the database connection is contained in an |
| ** instance of the following structure. |
| */ |
| typedef struct ShellState ShellState; |
| struct ShellState { |
sqlite3 *db; /* The database */ |
sqlite3 *db; /* The database */ |
int echoOn; /* True to echo input commands */ |
int echoOn; /* True to echo input commands */ |
|
int autoExplain; /* Automatically turn on .explain mode */ |
|
int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */ |
int statsOn; /* True to display memory stats before each finalize */ |
int statsOn; /* True to display memory stats before each finalize */ |
|
int scanstatsOn; /* True to display scan stats before each finalize */ |
|
int countChanges; /* True to display change counts */ |
|
int backslashOn; /* Resolve C-style \x escapes in SQL input text */ |
|
int outCount; /* Revert to stdout when reaching zero */ |
int cnt; /* Number of records displayed so far */ |
int cnt; /* Number of records displayed so far */ |
FILE *out; /* Write results here */ |
FILE *out; /* Write results here */ |
FILE *traceOut; /* Output for sqlite3_trace() */ |
FILE *traceOut; /* Output for sqlite3_trace() */ |
int nErr; /* Number of errors seen */ |
int nErr; /* Number of errors seen */ |
int mode; /* An output mode setting */ |
int mode; /* An output mode setting */ |
|
int cMode; /* temporary output mode for the current query */ |
|
int normalMode; /* Output mode before ".explain on" */ |
int writableSchema; /* True if PRAGMA writable_schema=ON */ |
int writableSchema; /* True if PRAGMA writable_schema=ON */ |
int showHeader; /* True to show column names in List or Column mode */ |
int showHeader; /* True to show column names in List or Column mode */ |
|
unsigned shellFlgs; /* Various flags */ |
char *zDestTable; /* Name of destination table when MODE_Insert */ |
char *zDestTable; /* Name of destination table when MODE_Insert */ |
char separator[20]; /* Separator character for MODE_List */ | char colSeparator[20]; /* Column separator character for several modes */ |
| char rowSeparator[20]; /* Row separator character for MODE_Ascii */ |
int colWidth[100]; /* Requested width of each column when in column mode*/ |
int colWidth[100]; /* Requested width of each column when in column mode*/ |
int actualWidth[100]; /* Actual width of each column */ |
int actualWidth[100]; /* Actual width of each column */ |
char nullvalue[20]; /* The text to print when a NULL comes back from | char nullValue[20]; /* The text to print when a NULL comes back from |
** the database */ |
** the database */ |
struct previous_mode_data explainPrev; |
|
/* Holds the mode information just before |
|
** .explain ON */ |
|
char outfile[FILENAME_MAX]; /* Filename for *out */ |
char outfile[FILENAME_MAX]; /* Filename for *out */ |
const char *zDbFilename; /* name of the database file */ |
const char *zDbFilename; /* name of the database file */ |
|
char *zFreeOnClose; /* Filename to free when closing */ |
const char *zVfs; /* Name of VFS to use */ |
const char *zVfs; /* Name of VFS to use */ |
sqlite3_stmt *pStmt; /* Current statement if any. */ |
sqlite3_stmt *pStmt; /* Current statement if any. */ |
FILE *pLog; /* Write log output here */ |
FILE *pLog; /* Write log output here */ |
|
int *aiIndent; /* Array of indents used in MODE_Explain */ |
|
int nIndent; /* Size of array aiIndent[] */ |
|
int iIndent; /* Index of current op in aiIndent[] */ |
|
#if defined(SQLITE_ENABLE_SESSION) |
|
int nSession; /* Number of active sessions */ |
|
OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ |
|
#endif |
}; |
}; |
|
|
/* |
/* |
|
** These are the allowed shellFlgs values |
|
*/ |
|
#define SHFLG_Scratch 0x00001 /* The --scratch option is used */ |
|
#define SHFLG_Pagecache 0x00002 /* The --pagecache option is used */ |
|
#define SHFLG_Lookaside 0x00004 /* Lookaside memory is used */ |
|
|
|
/* |
** These are the allowed modes. |
** These are the allowed modes. |
*/ |
*/ |
#define MODE_Line 0 /* One column per line. Blank line between records */ |
#define MODE_Line 0 /* One column per line. Blank line between records */ |
Line 452 struct callback_data {
|
Line 668 struct callback_data {
|
#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */ |
#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */ |
#define MODE_Csv 7 /* Quote strings, numbers are plain */ |
#define MODE_Csv 7 /* Quote strings, numbers are plain */ |
#define MODE_Explain 8 /* Like MODE_Column, but do not truncate data */ |
#define MODE_Explain 8 /* Like MODE_Column, but do not truncate data */ |
|
#define MODE_Ascii 9 /* Use ASCII unit and record separators (0x1F/0x1E) */ |
|
#define MODE_Pretty 10 /* Pretty-print schemas */ |
|
|
static const char *modeDescr[] = { |
static const char *modeDescr[] = { |
"line", |
"line", |
Line 463 static const char *modeDescr[] = {
|
Line 681 static const char *modeDescr[] = {
|
"tcl", |
"tcl", |
"csv", |
"csv", |
"explain", |
"explain", |
|
"ascii", |
|
"prettyprint", |
}; |
}; |
|
|
/* |
/* |
** Number of elements in an array | ** These are the column/row/line separators used by the various |
| ** import/export modes. |
*/ |
*/ |
#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) | #define SEP_Column "|" |
| #define SEP_Row "\n" |
| #define SEP_Tab "\t" |
| #define SEP_Space " " |
| #define SEP_Comma "," |
| #define SEP_CrLf "\r\n" |
| #define SEP_Unit "\x1F" |
| #define SEP_Record "\x1E" |
|
|
/* |
/* |
** Compute a string length that is limited to what can be stored in | ** Number of elements in an array |
** lower 30 bits of a 32-bit signed integer. | |
*/ |
*/ |
static int strlen30(const char *z){ | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
const char *z2 = z; | |
while( *z2 ){ z2++; } | |
return 0x3fffffff & (int)(z2 - z); | |
} | |
|
|
/* |
/* |
** A callback for the sqlite3_log() interface. |
** A callback for the sqlite3_log() interface. |
*/ |
*/ |
static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
static void shellLog(void *pArg, int iErrCode, const char *zMsg){ |
struct callback_data *p = (struct callback_data*)pArg; | ShellState *p = (ShellState*)pArg; |
if( p->pLog==0 ) return; |
if( p->pLog==0 ) return; |
fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg); | utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); |
fflush(p->pLog); |
fflush(p->pLog); |
} |
} |
|
|
Line 496 static void shellLog(void *pArg, int iErrCode, const c
|
Line 719 static void shellLog(void *pArg, int iErrCode, const c
|
static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ |
int i; |
int i; |
char *zBlob = (char *)pBlob; |
char *zBlob = (char *)pBlob; |
fprintf(out,"X'"); | raw_printf(out,"X'"); |
for(i=0; i<nBlob; i++){ fprintf(out,"%02x",zBlob[i]&0xff); } | for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); } |
fprintf(out,"'"); | raw_printf(out,"'"); |
} |
} |
|
|
/* |
/* |
Line 507 static void output_hex_blob(FILE *out, const void *pBl
|
Line 730 static void output_hex_blob(FILE *out, const void *pBl
|
static void output_quoted_string(FILE *out, const char *z){ |
static void output_quoted_string(FILE *out, const char *z){ |
int i; |
int i; |
int nSingle = 0; |
int nSingle = 0; |
|
setBinaryMode(out, 1); |
for(i=0; z[i]; i++){ |
for(i=0; z[i]; i++){ |
if( z[i]=='\'' ) nSingle++; |
if( z[i]=='\'' ) nSingle++; |
} |
} |
if( nSingle==0 ){ |
if( nSingle==0 ){ |
fprintf(out,"'%s'",z); | utf8_printf(out,"'%s'",z); |
}else{ |
}else{ |
fprintf(out,"'"); | raw_printf(out,"'"); |
while( *z ){ |
while( *z ){ |
for(i=0; z[i] && z[i]!='\''; i++){} |
for(i=0; z[i] && z[i]!='\''; i++){} |
if( i==0 ){ |
if( i==0 ){ |
fprintf(out,"''"); | raw_printf(out,"''"); |
z++; |
z++; |
}else if( z[i]=='\'' ){ |
}else if( z[i]=='\'' ){ |
fprintf(out,"%.*s''",i,z); | utf8_printf(out,"%.*s''",i,z); |
z += i+1; |
z += i+1; |
}else{ |
}else{ |
fprintf(out,"%s",z); | utf8_printf(out,"%s",z); |
break; |
break; |
} |
} |
} |
} |
fprintf(out,"'"); | raw_printf(out,"'"); |
} |
} |
|
setTextMode(out, 1); |
} |
} |
|
|
/* |
/* |
Line 553 static void output_c_string(FILE *out, const char *z){
|
Line 778 static void output_c_string(FILE *out, const char *z){
|
}else if( c=='\r' ){ |
}else if( c=='\r' ){ |
fputc('\\', out); |
fputc('\\', out); |
fputc('r', out); |
fputc('r', out); |
}else if( !isprint(c) ){ | }else if( !isprint(c&0xff) ){ |
fprintf(out, "\\%03o", c&0xff); | raw_printf(out, "\\%03o", c&0xff); |
}else{ |
}else{ |
fputc(c, out); |
fputc(c, out); |
} |
} |
Line 568 static void output_c_string(FILE *out, const char *z){
|
Line 793 static void output_c_string(FILE *out, const char *z){
|
*/ |
*/ |
static void output_html_string(FILE *out, const char *z){ |
static void output_html_string(FILE *out, const char *z){ |
int i; |
int i; |
|
if( z==0 ) z = ""; |
while( *z ){ |
while( *z ){ |
for(i=0; z[i] | for(i=0; z[i] |
&& z[i]!='<' | && z[i]!='<' |
&& z[i]!='&' | && z[i]!='&' |
&& z[i]!='>' | && z[i]!='>' |
&& z[i]!='\"' | && z[i]!='\"' |
&& z[i]!='\''; |
&& z[i]!='\''; |
i++){} |
i++){} |
if( i>0 ){ |
if( i>0 ){ |
fprintf(out,"%.*s",i,z); | utf8_printf(out,"%.*s",i,z); |
} |
} |
if( z[i]=='<' ){ |
if( z[i]=='<' ){ |
fprintf(out,"<"); | raw_printf(out,"<"); |
}else if( z[i]=='&' ){ |
}else if( z[i]=='&' ){ |
fprintf(out,"&"); | raw_printf(out,"&"); |
}else if( z[i]=='>' ){ |
}else if( z[i]=='>' ){ |
fprintf(out,">"); | raw_printf(out,">"); |
}else if( z[i]=='\"' ){ |
}else if( z[i]=='\"' ){ |
fprintf(out,"""); | raw_printf(out,"""); |
}else if( z[i]=='\'' ){ |
}else if( z[i]=='\'' ){ |
fprintf(out,"'"); | raw_printf(out,"'"); |
}else{ |
}else{ |
break; |
break; |
} |
} |
Line 601 static void output_html_string(FILE *out, const char *
|
Line 827 static void output_html_string(FILE *out, const char *
|
** array, then the string must be quoted for CSV. |
** array, then the string must be quoted for CSV. |
*/ |
*/ |
static const char needCsvQuote[] = { |
static const char needCsvQuote[] = { |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
}; |
}; |
|
|
/* |
/* |
** Output a single term of CSV. Actually, p->separator is used for | ** Output a single term of CSV. Actually, p->colSeparator is used for |
** the separator, which may or may not be a comma. p->nullvalue is | ** the separator, which may or may not be a comma. p->nullValue is |
** the null value. Strings are quoted if necessary. | ** the null value. Strings are quoted if necessary. The separator |
| ** is only issued if bSep is true. |
*/ |
*/ |
static void output_csv(struct callback_data *p, const char *z, int bSep){ | static void output_csv(ShellState *p, const char *z, int bSep){ |
FILE *out = p->out; |
FILE *out = p->out; |
if( z==0 ){ |
if( z==0 ){ |
fprintf(out,"%s",p->nullvalue); | utf8_printf(out,"%s",p->nullValue); |
}else{ |
}else{ |
int i; |
int i; |
int nSep = strlen30(p->separator); | int nSep = strlen30(p->colSeparator); |
for(i=0; z[i]; i++){ |
for(i=0; z[i]; i++){ |
if( needCsvQuote[((unsigned char*)z)[i]] | if( needCsvQuote[((unsigned char*)z)[i]] |
|| (z[i]==p->separator[0] && | || (z[i]==p->colSeparator[0] && |
(nSep==1 || memcmp(z, p->separator, nSep)==0)) ){ | (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){ |
i = 0; |
i = 0; |
break; |
break; |
} |
} |
Line 647 static void output_csv(struct callback_data *p, const
|
Line 874 static void output_csv(struct callback_data *p, const
|
} |
} |
putc('"', out); |
putc('"', out); |
}else{ |
}else{ |
fprintf(out, "%s", z); | utf8_printf(out, "%s", z); |
} |
} |
} |
} |
if( bSep ){ |
if( bSep ){ |
fprintf(p->out, "%s", p->separator); | utf8_printf(p->out, "%s", p->colSeparator); |
} |
} |
} |
} |
|
|
Line 661 static void output_csv(struct callback_data *p, const
|
Line 888 static void output_csv(struct callback_data *p, const
|
*/ |
*/ |
static void interrupt_handler(int NotUsed){ |
static void interrupt_handler(int NotUsed){ |
UNUSED_PARAMETER(NotUsed); |
UNUSED_PARAMETER(NotUsed); |
seenInterrupt = 1; | seenInterrupt++; |
if( db ) sqlite3_interrupt(db); | if( seenInterrupt>2 ) exit(1); |
| if( globalDb ) sqlite3_interrupt(globalDb); |
} |
} |
#endif |
#endif |
|
|
/* |
/* |
|
** When the ".auth ON" is set, the following authorizer callback is |
|
** invoked. It always returns SQLITE_OK. |
|
*/ |
|
static int shellAuth( |
|
void *pClientData, |
|
int op, |
|
const char *zA1, |
|
const char *zA2, |
|
const char *zA3, |
|
const char *zA4 |
|
){ |
|
ShellState *p = (ShellState*)pClientData; |
|
static const char *azAction[] = { 0, |
|
"CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX", |
|
"CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW", |
|
"CREATE_TRIGGER", "CREATE_VIEW", "DELETE", |
|
"DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX", |
|
"DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW", |
|
"DROP_TRIGGER", "DROP_VIEW", "INSERT", |
|
"PRAGMA", "READ", "SELECT", |
|
"TRANSACTION", "UPDATE", "ATTACH", |
|
"DETACH", "ALTER_TABLE", "REINDEX", |
|
"ANALYZE", "CREATE_VTABLE", "DROP_VTABLE", |
|
"FUNCTION", "SAVEPOINT", "RECURSIVE" |
|
}; |
|
int i; |
|
const char *az[4]; |
|
az[0] = zA1; |
|
az[1] = zA2; |
|
az[2] = zA3; |
|
az[3] = zA4; |
|
raw_printf(p->out, "authorizer: %s", azAction[op]); |
|
for(i=0; i<4; i++){ |
|
raw_printf(p->out, " "); |
|
if( az[i] ){ |
|
output_c_string(p->out, az[i]); |
|
}else{ |
|
raw_printf(p->out, "NULL"); |
|
} |
|
} |
|
raw_printf(p->out, "\n"); |
|
return SQLITE_OK; |
|
} |
|
|
|
|
|
/* |
** This is the callback routine that the shell |
** This is the callback routine that the shell |
** invokes for each row of a query result. |
** invokes for each row of a query result. |
*/ |
*/ |
static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){ | static int shell_callback( |
| void *pArg, |
| int nArg, /* Number of result columns */ |
| char **azArg, /* Text of each result column */ |
| char **azCol, /* Column names */ |
| int *aiType /* Column types */ |
| ){ |
int i; |
int i; |
struct callback_data *p = (struct callback_data*)pArg; | ShellState *p = (ShellState*)pArg; |
|
|
switch( p->mode ){ | switch( p->cMode ){ |
case MODE_Line: { |
case MODE_Line: { |
int w = 5; |
int w = 5; |
if( azArg==0 ) break; |
if( azArg==0 ) break; |
Line 682 static int shell_callback(void *pArg, int nArg, char *
|
Line 962 static int shell_callback(void *pArg, int nArg, char *
|
int len = strlen30(azCol[i] ? azCol[i] : ""); |
int len = strlen30(azCol[i] ? azCol[i] : ""); |
if( len>w ) w = len; |
if( len>w ) w = len; |
} |
} |
if( p->cnt++>0 ) fprintf(p->out,"\n"); | if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
fprintf(p->out,"%*s = %s\n", w, azCol[i], | utf8_printf(p->out,"%*s = %s%s", w, azCol[i], |
azArg[i] ? azArg[i] : p->nullvalue); | azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); |
} |
} |
break; |
break; |
} |
} |
case MODE_Explain: |
case MODE_Explain: |
case MODE_Column: { |
case MODE_Column: { |
|
static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
|
const int *colWidth; |
|
int showHdr; |
|
char *rowSep; |
|
if( p->cMode==MODE_Column ){ |
|
colWidth = p->colWidth; |
|
showHdr = p->showHeader; |
|
rowSep = p->rowSeparator; |
|
}else{ |
|
colWidth = aExplainWidths; |
|
showHdr = 1; |
|
rowSep = SEP_Row; |
|
} |
if( p->cnt++==0 ){ |
if( p->cnt++==0 ){ |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
int w, n; |
int w, n; |
if( i<ArraySize(p->colWidth) ){ |
if( i<ArraySize(p->colWidth) ){ |
w = p->colWidth[i]; | w = colWidth[i]; |
}else{ |
}else{ |
w = 0; |
w = 0; |
} |
} |
if( w==0 ){ |
if( w==0 ){ |
w = strlen30(azCol[i] ? azCol[i] : ""); |
w = strlen30(azCol[i] ? azCol[i] : ""); |
if( w<10 ) w = 10; |
if( w<10 ) w = 10; |
n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue); | n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullValue); |
if( w<n ) w = n; |
if( w<n ) w = n; |
} |
} |
if( i<ArraySize(p->actualWidth) ){ |
if( i<ArraySize(p->actualWidth) ){ |
p->actualWidth[i] = w; |
p->actualWidth[i] = w; |
} |
} |
if( p->showHeader ){ | if( showHdr ){ |
if( w<0 ){ |
if( w<0 ){ |
fprintf(p->out,"%*.*s%s",-w,-w,azCol[i], i==nArg-1 ? "\n": " "); | utf8_printf(p->out,"%*.*s%s",-w,-w,azCol[i], |
| i==nArg-1 ? rowSep : " "); |
}else{ |
}else{ |
fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " "); | utf8_printf(p->out,"%-*.*s%s",w,w,azCol[i], |
| i==nArg-1 ? rowSep : " "); |
} |
} |
} |
} |
} |
} |
if( p->showHeader ){ | if( showHdr ){ |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
int w; |
int w; |
if( i<ArraySize(p->actualWidth) ){ |
if( i<ArraySize(p->actualWidth) ){ |
Line 725 static int shell_callback(void *pArg, int nArg, char *
|
Line 1020 static int shell_callback(void *pArg, int nArg, char *
|
}else{ |
}else{ |
w = 10; |
w = 10; |
} |
} |
fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------" | utf8_printf(p->out,"%-*.*s%s",w,w, |
| "----------------------------------------------------------" |
"----------------------------------------------------------", |
"----------------------------------------------------------", |
i==nArg-1 ? "\n": " "); | i==nArg-1 ? rowSep : " "); |
} |
} |
} |
} |
} |
} |
Line 739 static int shell_callback(void *pArg, int nArg, char *
|
Line 1035 static int shell_callback(void *pArg, int nArg, char *
|
}else{ |
}else{ |
w = 10; |
w = 10; |
} |
} |
if( p->mode==MODE_Explain && azArg[i] && | if( p->cMode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){ |
strlen30(azArg[i])>w ){ | |
w = strlen30(azArg[i]); |
w = strlen30(azArg[i]); |
} |
} |
|
if( i==1 && p->aiIndent && p->pStmt ){ |
|
if( p->iIndent<p->nIndent ){ |
|
utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); |
|
} |
|
p->iIndent++; |
|
} |
if( w<0 ){ |
if( w<0 ){ |
fprintf(p->out,"%*.*s%s",-w,-w, | utf8_printf(p->out,"%*.*s%s",-w,-w, |
azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " "); | azArg[i] ? azArg[i] : p->nullValue, |
| i==nArg-1 ? rowSep : " "); |
}else{ |
}else{ |
fprintf(p->out,"%-*.*s%s",w,w, | utf8_printf(p->out,"%-*.*s%s",w,w, |
azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " "); | azArg[i] ? azArg[i] : p->nullValue, |
| i==nArg-1 ? rowSep : " "); |
} |
} |
} |
} |
break; |
break; |
} |
} |
case MODE_Semi: | case MODE_Semi: { /* .schema and .fullschema output */ |
| utf8_printf(p->out, "%s;\n", azArg[0]); |
| break; |
| } |
| case MODE_Pretty: { /* .schema and .fullschema with --indent */ |
| char *z; |
| int j; |
| int nParen = 0; |
| char cEnd = 0; |
| char c; |
| int nLine = 0; |
| assert( nArg==1 ); |
| if( azArg[0]==0 ) break; |
| if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 |
| || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 |
| ){ |
| utf8_printf(p->out, "%s;\n", azArg[0]); |
| break; |
| } |
| z = sqlite3_mprintf("%s", azArg[0]); |
| j = 0; |
| for(i=0; IsSpace(z[i]); i++){} |
| for(; (c = z[i])!=0; i++){ |
| if( IsSpace(c) ){ |
| if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; |
| }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ |
| j--; |
| } |
| z[j++] = c; |
| } |
| while( j>0 && IsSpace(z[j-1]) ){ j--; } |
| z[j] = 0; |
| if( strlen30(z)>=79 ){ |
| for(i=j=0; (c = z[i])!=0; i++){ |
| if( c==cEnd ){ |
| cEnd = 0; |
| }else if( c=='"' || c=='\'' || c=='`' ){ |
| cEnd = c; |
| }else if( c=='[' ){ |
| cEnd = ']'; |
| }else if( c=='(' ){ |
| nParen++; |
| }else if( c==')' ){ |
| nParen--; |
| if( nLine>0 && nParen==0 && j>0 ){ |
| utf8_printf(p->out, "%.*s\n", j, z); |
| j = 0; |
| } |
| } |
| z[j++] = c; |
| if( nParen==1 && (c=='(' || c==',' || c=='\n') ){ |
| if( c=='\n' ) j--; |
| utf8_printf(p->out, "%.*s\n ", j, z); |
| j = 0; |
| nLine++; |
| while( IsSpace(z[i+1]) ){ i++; } |
| } |
| } |
| z[j] = 0; |
| } |
| utf8_printf(p->out, "%s;\n", z); |
| sqlite3_free(z); |
| break; |
| } |
case MODE_List: { |
case MODE_List: { |
if( p->cnt++==0 && p->showHeader ){ |
if( p->cnt++==0 && p->showHeader ){ |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator); | utf8_printf(p->out,"%s%s",azCol[i], |
| i==nArg-1 ? p->rowSeparator : p->colSeparator); |
} |
} |
} |
} |
if( azArg==0 ) break; |
if( azArg==0 ) break; |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
char *z = azArg[i]; |
char *z = azArg[i]; |
if( z==0 ) z = p->nullvalue; | if( z==0 ) z = p->nullValue; |
fprintf(p->out, "%s", z); | utf8_printf(p->out, "%s", z); |
if( i<nArg-1 ){ |
if( i<nArg-1 ){ |
fprintf(p->out, "%s", p->separator); | utf8_printf(p->out, "%s", p->colSeparator); |
}else if( p->mode==MODE_Semi ){ | |
fprintf(p->out, ";\n"); | |
}else{ |
}else{ |
fprintf(p->out, "\n"); | utf8_printf(p->out, "%s", p->rowSeparator); |
} |
} |
} |
} |
break; |
break; |
} |
} |
case MODE_Html: { |
case MODE_Html: { |
if( p->cnt++==0 && p->showHeader ){ |
if( p->cnt++==0 && p->showHeader ){ |
fprintf(p->out,"<TR>"); | raw_printf(p->out,"<TR>"); |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
fprintf(p->out,"<TH>"); | raw_printf(p->out,"<TH>"); |
output_html_string(p->out, azCol[i]); |
output_html_string(p->out, azCol[i]); |
fprintf(p->out,"</TH>\n"); | raw_printf(p->out,"</TH>\n"); |
} |
} |
fprintf(p->out,"</TR>\n"); | raw_printf(p->out,"</TR>\n"); |
} |
} |
if( azArg==0 ) break; |
if( azArg==0 ) break; |
fprintf(p->out,"<TR>"); | raw_printf(p->out,"<TR>"); |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
fprintf(p->out,"<TD>"); | raw_printf(p->out,"<TD>"); |
output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
fprintf(p->out,"</TD>\n"); | raw_printf(p->out,"</TD>\n"); |
} |
} |
fprintf(p->out,"</TR>\n"); | raw_printf(p->out,"</TR>\n"); |
break; |
break; |
} |
} |
case MODE_Tcl: { |
case MODE_Tcl: { |
if( p->cnt++==0 && p->showHeader ){ |
if( p->cnt++==0 && p->showHeader ){ |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
if(i<nArg-1) fprintf(p->out, "%s", p->separator); | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
} |
} |
fprintf(p->out,"\n"); | utf8_printf(p->out, "%s", p->rowSeparator); |
} |
} |
if( azArg==0 ) break; |
if( azArg==0 ) break; |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); |
if(i<nArg-1) fprintf(p->out, "%s", p->separator); | if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); |
} |
} |
fprintf(p->out,"\n"); | utf8_printf(p->out, "%s", p->rowSeparator); |
break; |
break; |
} |
} |
case MODE_Csv: { |
case MODE_Csv: { |
|
setBinaryMode(p->out, 1); |
if( p->cnt++==0 && p->showHeader ){ |
if( p->cnt++==0 && p->showHeader ){ |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
} |
} |
fprintf(p->out,"\n"); | utf8_printf(p->out, "%s", p->rowSeparator); |
} |
} |
if( azArg==0 ) break; | if( nArg>0 ){ |
for(i=0; i<nArg; i++){ | for(i=0; i<nArg; i++){ |
output_csv(p, azArg[i], i<nArg-1); | output_csv(p, azArg[i], i<nArg-1); |
| } |
| utf8_printf(p->out, "%s", p->rowSeparator); |
} |
} |
fprintf(p->out,"\n"); | setTextMode(p->out, 1); |
break; |
break; |
} |
} |
case MODE_Insert: { |
case MODE_Insert: { |
p->cnt++; |
p->cnt++; |
if( azArg==0 ) break; |
if( azArg==0 ) break; |
fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable); | utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); |
| if( p->showHeader ){ |
| raw_printf(p->out,"("); |
| for(i=0; i<nArg; i++){ |
| char *zSep = i>0 ? ",": ""; |
| utf8_printf(p->out, "%s%s", zSep, azCol[i]); |
| } |
| raw_printf(p->out,")"); |
| } |
| raw_printf(p->out," VALUES("); |
for(i=0; i<nArg; i++){ |
for(i=0; i<nArg; i++){ |
char *zSep = i>0 ? ",": ""; |
char *zSep = i>0 ? ",": ""; |
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ |
fprintf(p->out,"%sNULL",zSep); | utf8_printf(p->out,"%sNULL",zSep); |
}else if( aiType && aiType[i]==SQLITE_TEXT ){ |
}else if( aiType && aiType[i]==SQLITE_TEXT ){ |
if( zSep[0] ) fprintf(p->out,"%s",zSep); | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
output_quoted_string(p->out, azArg[i]); |
output_quoted_string(p->out, azArg[i]); |
}else if( aiType && (aiType[i]==SQLITE_INTEGER || aiType[i]==SQLITE_FLOAT) ){ | }else if( aiType && (aiType[i]==SQLITE_INTEGER |
fprintf(p->out,"%s%s",zSep, azArg[i]); | || aiType[i]==SQLITE_FLOAT) ){ |
| utf8_printf(p->out,"%s%s",zSep, azArg[i]); |
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ |
const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
const void *pBlob = sqlite3_column_blob(p->pStmt, i); |
int nBlob = sqlite3_column_bytes(p->pStmt, i); |
int nBlob = sqlite3_column_bytes(p->pStmt, i); |
if( zSep[0] ) fprintf(p->out,"%s",zSep); | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
output_hex_blob(p->out, pBlob, nBlob); |
output_hex_blob(p->out, pBlob, nBlob); |
}else if( isNumber(azArg[i], 0) ){ |
}else if( isNumber(azArg[i], 0) ){ |
fprintf(p->out,"%s%s",zSep, azArg[i]); | utf8_printf(p->out,"%s%s",zSep, azArg[i]); |
}else{ |
}else{ |
if( zSep[0] ) fprintf(p->out,"%s",zSep); | if( zSep[0] ) utf8_printf(p->out,"%s",zSep); |
output_quoted_string(p->out, azArg[i]); |
output_quoted_string(p->out, azArg[i]); |
} |
} |
} |
} |
fprintf(p->out,");\n"); | raw_printf(p->out,");\n"); |
break; |
break; |
} |
} |
|
case MODE_Ascii: { |
|
if( p->cnt++==0 && p->showHeader ){ |
|
for(i=0; i<nArg; i++){ |
|
if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
|
utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); |
|
} |
|
utf8_printf(p->out, "%s", p->rowSeparator); |
|
} |
|
if( azArg==0 ) break; |
|
for(i=0; i<nArg; i++){ |
|
if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); |
|
utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); |
|
} |
|
utf8_printf(p->out, "%s", p->rowSeparator); |
|
break; |
|
} |
} |
} |
return 0; |
return 0; |
} |
} |
Line 867 static int callback(void *pArg, int nArg, char **azArg
|
Line 1261 static int callback(void *pArg, int nArg, char **azArg
|
} |
} |
|
|
/* |
/* |
** Set the destination table field of the callback_data structure to | ** Set the destination table field of the ShellState structure to |
** the name of the table given. Escape any quote characters in the |
** the name of the table given. Escape any quote characters in the |
** table name. |
** table name. |
*/ |
*/ |
static void set_table_name(struct callback_data *p, const char *zName){ | static void set_table_name(ShellState *p, const char *zName){ |
int i, n; |
int i, n; |
int needQuote; |
int needQuote; |
char *z; |
char *z; |
Line 891 static void set_table_name(struct callback_data *p, co
|
Line 1285 static void set_table_name(struct callback_data *p, co
|
if( needQuote ) n += 2; |
if( needQuote ) n += 2; |
z = p->zDestTable = malloc( n+1 ); |
z = p->zDestTable = malloc( n+1 ); |
if( z==0 ){ |
if( z==0 ){ |
fprintf(stderr,"Error: out of memory\n"); | raw_printf(stderr,"Error: out of memory\n"); |
exit(1); |
exit(1); |
} |
} |
n = 0; |
n = 0; |
Line 909 static void set_table_name(struct callback_data *p, co
|
Line 1303 static void set_table_name(struct callback_data *p, co
|
** added to zIn, and the result returned in memory obtained from malloc(). |
** added to zIn, and the result returned in memory obtained from malloc(). |
** zIn, if it was not NULL, is freed. |
** zIn, if it was not NULL, is freed. |
** |
** |
** If the third argument, quote, is not '\0', then it is used as a | ** If the third argument, quote, is not '\0', then it is used as a |
** quote character for zAppend. |
** quote character for zAppend. |
*/ |
*/ |
static char *appendText(char *zIn, char const *zAppend, char quote){ |
static char *appendText(char *zIn, char const *zAppend, char quote){ |
Line 956 static char *appendText(char *zIn, char const *zAppend
|
Line 1350 static char *appendText(char *zIn, char const *zAppend
|
** semicolon terminator to the end of that line. |
** semicolon terminator to the end of that line. |
** |
** |
** If the number of columns is 1 and that column contains text "--" |
** If the number of columns is 1 and that column contains text "--" |
** then write the semicolon on a separate line. That way, if a | ** then write the semicolon on a separate line. That way, if a |
** "--" comment occurs at the end of the statement, the comment |
** "--" comment occurs at the end of the statement, the comment |
** won't consume the semicolon terminator. |
** won't consume the semicolon terminator. |
*/ |
*/ |
static int run_table_dump_query( |
static int run_table_dump_query( |
struct callback_data *p, /* Query context */ | ShellState *p, /* Query context */ |
const char *zSelect, /* SELECT statement to extract content */ |
const char *zSelect, /* SELECT statement to extract content */ |
const char *zFirstRow /* Print before first row, if not NULL */ |
const char *zFirstRow /* Print before first row, if not NULL */ |
){ |
){ |
Line 970 static int run_table_dump_query(
|
Line 1364 static int run_table_dump_query(
|
int nResult; |
int nResult; |
int i; |
int i; |
const char *z; |
const char *z; |
rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0); | rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); |
if( rc!=SQLITE_OK || !pSelect ){ |
if( rc!=SQLITE_OK || !pSelect ){ |
fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db)); | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
p->nErr++; | sqlite3_errmsg(p->db)); |
| if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
return rc; |
return rc; |
} |
} |
rc = sqlite3_step(pSelect); |
rc = sqlite3_step(pSelect); |
nResult = sqlite3_column_count(pSelect); |
nResult = sqlite3_column_count(pSelect); |
while( rc==SQLITE_ROW ){ |
while( rc==SQLITE_ROW ){ |
if( zFirstRow ){ |
if( zFirstRow ){ |
fprintf(p->out, "%s", zFirstRow); | utf8_printf(p->out, "%s", zFirstRow); |
zFirstRow = 0; |
zFirstRow = 0; |
} |
} |
z = (const char*)sqlite3_column_text(pSelect, 0); |
z = (const char*)sqlite3_column_text(pSelect, 0); |
fprintf(p->out, "%s", z); | utf8_printf(p->out, "%s", z); |
for(i=1; i<nResult; i++){ | for(i=1; i<nResult; i++){ |
fprintf(p->out, ",%s", sqlite3_column_text(pSelect, i)); | utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); |
} |
} |
if( z==0 ) z = ""; |
if( z==0 ) z = ""; |
while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; |
if( z[0] ){ |
if( z[0] ){ |
fprintf(p->out, "\n;\n"); | raw_printf(p->out, "\n;\n"); |
}else{ |
}else{ |
fprintf(p->out, ";\n"); | raw_printf(p->out, ";\n"); |
} | } |
rc = sqlite3_step(pSelect); |
rc = sqlite3_step(pSelect); |
} |
} |
rc = sqlite3_finalize(pSelect); |
rc = sqlite3_finalize(pSelect); |
if( rc!=SQLITE_OK ){ |
if( rc!=SQLITE_OK ){ |
fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db)); | utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, |
p->nErr++; | sqlite3_errmsg(p->db)); |
| if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; |
} |
} |
return rc; |
return rc; |
} |
} |
Line 1012 static char *save_err_msg(
|
Line 1408 static char *save_err_msg(
|
sqlite3 *db /* Database to query */ |
sqlite3 *db /* Database to query */ |
){ |
){ |
int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
int nErrMsg = 1+strlen30(sqlite3_errmsg(db)); |
char *zErrMsg = sqlite3_malloc(nErrMsg); | char *zErrMsg = sqlite3_malloc64(nErrMsg); |
if( zErrMsg ){ |
if( zErrMsg ){ |
memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg); |
} |
} |
return zErrMsg; |
return zErrMsg; |
} |
} |
|
|
|
#ifdef __linux__ |
/* |
/* |
|
** Attempt to display I/O stats on Linux using /proc/PID/io |
|
*/ |
|
static void displayLinuxIoStats(FILE *out){ |
|
FILE *in; |
|
char z[200]; |
|
sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
|
in = fopen(z, "rb"); |
|
if( in==0 ) return; |
|
while( fgets(z, sizeof(z), in)!=0 ){ |
|
static const struct { |
|
const char *zPattern; |
|
const char *zDesc; |
|
} aTrans[] = { |
|
{ "rchar: ", "Bytes received by read():" }, |
|
{ "wchar: ", "Bytes sent to write():" }, |
|
{ "syscr: ", "Read() system calls:" }, |
|
{ "syscw: ", "Write() system calls:" }, |
|
{ "read_bytes: ", "Bytes read from storage:" }, |
|
{ "write_bytes: ", "Bytes written to storage:" }, |
|
{ "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
|
}; |
|
int i; |
|
for(i=0; i<ArraySize(aTrans); i++){ |
|
int n = (int)strlen(aTrans[i].zPattern); |
|
if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
|
raw_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); |
|
break; |
|
} |
|
} |
|
} |
|
fclose(in); |
|
} |
|
#endif |
|
|
|
|
|
/* |
** Display memory stats. |
** Display memory stats. |
*/ |
*/ |
static int display_stats( |
static int display_stats( |
sqlite3 *db, /* Database to query */ |
sqlite3 *db, /* Database to query */ |
struct callback_data *pArg, /* Pointer to struct callback_data */ | ShellState *pArg, /* Pointer to ShellState */ |
int bReset /* True to reset the stats */ |
int bReset /* True to reset the stats */ |
){ |
){ |
int iCur; |
int iCur; |
int iHiwtr; |
int iHiwtr; |
|
|
if( pArg && pArg->out ){ |
if( pArg && pArg->out ){ |
| |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset); |
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset); |
fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr); | raw_printf(pArg->out, |
| "Memory Used: %d (max %d) bytes\n", |
| iCur, iHiwtr); |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset); |
sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset); |
fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr); | raw_printf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", |
/* | iCur, iHiwtr); |
** Not currently used by the CLI. | if( pArg->shellFlgs & SHFLG_Pagecache ){ |
** iHiwtr = iCur = -1; | iHiwtr = iCur = -1; |
** sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset); | sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset); |
** fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr); | raw_printf(pArg->out, |
*/ | "Number of Pcache Pages Used: %d (max %d) pages\n", |
| iCur, iHiwtr); |
| } |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset); |
sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset); |
fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr); | raw_printf(pArg->out, |
/* | "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", |
** Not currently used by the CLI. | iCur, iHiwtr); |
** iHiwtr = iCur = -1; | if( pArg->shellFlgs & SHFLG_Scratch ){ |
** sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset); | iHiwtr = iCur = -1; |
** fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr); | sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset); |
*/ | raw_printf(pArg->out, |
| "Number of Scratch Allocations Used: %d (max %d)\n", |
| iCur, iHiwtr); |
| } |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset); |
sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset); |
fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr); | raw_printf(pArg->out, |
| "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", |
| iCur, iHiwtr); |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset); |
sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset); |
fprintf(pArg->out, "Largest Allocation: %d bytes\n", iHiwtr); | raw_printf(pArg->out, "Largest Allocation: %d bytes\n", |
| iHiwtr); |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset); |
sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset); |
fprintf(pArg->out, "Largest Pcache Allocation: %d bytes\n", iHiwtr); | raw_printf(pArg->out, "Largest Pcache Allocation: %d bytes\n", |
| iHiwtr); |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset); |
sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset); |
fprintf(pArg->out, "Largest Scratch Allocation: %d bytes\n", iHiwtr); | raw_printf(pArg->out, "Largest Scratch Allocation: %d bytes\n", |
| iHiwtr); |
#ifdef YYTRACKMAXSTACKDEPTH |
#ifdef YYTRACKMAXSTACKDEPTH |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset); |
sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset); |
fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr); | raw_printf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", |
| iCur, iHiwtr); |
#endif |
#endif |
} |
} |
|
|
if( pArg && pArg->out && db ){ |
if( pArg && pArg->out && db ){ |
|
if( pArg->shellFlgs & SHFLG_Lookaside ){ |
|
iHiwtr = iCur = -1; |
|
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, |
|
&iCur, &iHiwtr, bReset); |
|
raw_printf(pArg->out, |
|
"Lookaside Slots Used: %d (max %d)\n", |
|
iCur, iHiwtr); |
|
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, |
|
&iCur, &iHiwtr, bReset); |
|
raw_printf(pArg->out, "Successful lookaside attempts: %d\n", |
|
iHiwtr); |
|
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, |
|
&iCur, &iHiwtr, bReset); |
|
raw_printf(pArg->out, "Lookaside failures due to size: %d\n", |
|
iHiwtr); |
|
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, |
|
&iCur, &iHiwtr, bReset); |
|
raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", |
|
iHiwtr); |
|
} |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset); |
|
fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr); |
|
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset); |
|
fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr); |
|
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset); |
|
fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr); |
|
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset); |
|
fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr); |
|
iHiwtr = iCur = -1; |
|
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); |
fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1; | raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", |
| iCur); |
| iHiwtr = iCur = -1; |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); |
fprintf(pArg->out, "Page cache hits: %d\n", iCur); | raw_printf(pArg->out, "Page cache hits: %d\n", iCur); |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); |
fprintf(pArg->out, "Page cache misses: %d\n", iCur); | raw_printf(pArg->out, "Page cache misses: %d\n", iCur); |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); |
fprintf(pArg->out, "Page cache writes: %d\n", iCur); | raw_printf(pArg->out, "Page cache writes: %d\n", iCur); |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); |
fprintf(pArg->out, "Schema Heap Usage: %d bytes\n", iCur); | raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", |
| iCur); |
iHiwtr = iCur = -1; |
iHiwtr = iCur = -1; |
sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); |
fprintf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", iCur); | raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", |
| iCur); |
} |
} |
|
|
if( pArg && pArg->out && db && pArg->pStmt ){ |
if( pArg && pArg->out && db && pArg->pStmt ){ |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, bReset); | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, |
fprintf(pArg->out, "Fullscan Steps: %d\n", iCur); | bReset); |
| raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); |
fprintf(pArg->out, "Sort Operations: %d\n", iCur); | raw_printf(pArg->out, "Sort Operations: %d\n", iCur); |
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX, bReset); | iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); |
fprintf(pArg->out, "Autoindex Inserts: %d\n", iCur); | raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); |
| iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); |
| raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); |
} |
} |
|
|
|
#ifdef __linux__ |
|
displayLinuxIoStats(pArg->out); |
|
#endif |
|
|
|
/* Do not remove this machine readable comment: extra-stats-output-here */ |
|
|
return 0; |
return 0; |
} |
} |
|
|
/* |
/* |
** Execute a statement or set of statements. Print | ** Display scan stats. |
** any result rows/columns depending on the current mode | */ |
| static void display_scanstats( |
| sqlite3 *db, /* Database to query */ |
| ShellState *pArg /* Pointer to ShellState */ |
| ){ |
| #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| UNUSED_PARAMETER(db); |
| UNUSED_PARAMETER(pArg); |
| #else |
| int i, k, n, mx; |
| raw_printf(pArg->out, "-------- scanstats --------\n"); |
| mx = 0; |
| for(k=0; k<=mx; k++){ |
| double rEstLoop = 1.0; |
| for(i=n=0; 1; i++){ |
| sqlite3_stmt *p = pArg->pStmt; |
| sqlite3_int64 nLoop, nVisit; |
| double rEst; |
| int iSid; |
| const char *zExplain; |
| if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){ |
| break; |
| } |
| sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid); |
| if( iSid>mx ) mx = iSid; |
| if( iSid!=k ) continue; |
| if( n==0 ){ |
| rEstLoop = (double)nLoop; |
| if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); |
| } |
| n++; |
| sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit); |
| sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst); |
| sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain); |
| utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); |
| rEstLoop *= rEst; |
| raw_printf(pArg->out, |
| " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", |
| nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst |
| ); |
| } |
| } |
| raw_printf(pArg->out, "---------------------------\n"); |
| #endif |
| } |
| |
| /* |
| ** Parameter azArray points to a zero-terminated array of strings. zStr |
| ** points to a single nul-terminated string. Return non-zero if zStr |
| ** is equal, according to strcmp(), to any of the strings in the array. |
| ** Otherwise, return zero. |
| */ |
| static int str_in_array(const char *zStr, const char **azArray){ |
| int i; |
| for(i=0; azArray[i]; i++){ |
| if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| } |
| return 0; |
| } |
| |
| /* |
| ** If compiled statement pSql appears to be an EXPLAIN statement, allocate |
| ** and populate the ShellState.aiIndent[] array with the number of |
| ** spaces each opcode should be indented before it is output. |
| ** |
| ** The indenting rules are: |
| ** |
| ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| ** all opcodes that occur between the p2 jump destination and the opcode |
| ** itself by 2 spaces. |
| ** |
| ** * For each "Goto", if the jump destination is earlier in the program |
| ** and ends on one of: |
| ** Yield SeekGt SeekLt RowSetRead Rewind |
| ** or if the P1 parameter is one instead of zero, |
| ** then indent all opcodes between the earlier instruction |
| ** and "Goto" by 2 spaces. |
| */ |
| static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ |
| const char *zSql; /* The text of the SQL statement */ |
| const char *z; /* Used to check if this is an EXPLAIN */ |
| int *abYield = 0; /* True if op is an OP_Yield */ |
| int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ |
| int iOp; /* Index of operation in p->aiIndent[] */ |
| |
| const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| "NextIfOpen", "PrevIfOpen", 0 }; |
| const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| "Rewind", 0 }; |
| const char *azGoto[] = { "Goto", 0 }; |
| |
| /* Try to figure out if this is really an EXPLAIN statement. If this |
| ** cannot be verified, return early. */ |
| if( sqlite3_column_count(pSql)!=8 ){ |
| p->cMode = p->mode; |
| return; |
| } |
| zSql = sqlite3_sql(pSql); |
| if( zSql==0 ) return; |
| for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++); |
| if( sqlite3_strnicmp(z, "explain", 7) ){ |
| p->cMode = p->mode; |
| return; |
| } |
| |
| for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ |
| int i; |
| int iAddr = sqlite3_column_int(pSql, 0); |
| const char *zOp = (const char*)sqlite3_column_text(pSql, 1); |
| |
| /* Set p2 to the P2 field of the current opcode. Then, assuming that |
| ** p2 is an instruction address, set variable p2op to the index of that |
| ** instruction in the aiIndent[] array. p2 and p2op may be different if |
| ** the current instruction is part of a sub-program generated by an |
| ** SQL trigger or foreign key. */ |
| int p2 = sqlite3_column_int(pSql, 3); |
| int p2op = (p2 + (iOp-iAddr)); |
| |
| /* Grow the p->aiIndent array as required */ |
| if( iOp>=nAlloc ){ |
| if( iOp==0 ){ |
| /* Do further verfication that this is explain output. Abort if |
| ** it is not */ |
| static const char *explainCols[] = { |
| "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" }; |
| int jj; |
| for(jj=0; jj<ArraySize(explainCols); jj++){ |
| if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){ |
| p->cMode = p->mode; |
| sqlite3_reset(pSql); |
| return; |
| } |
| } |
| } |
| nAlloc += 100; |
| p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); |
| abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
| } |
| abYield[iOp] = str_in_array(zOp, azYield); |
| p->aiIndent[iOp] = 0; |
| p->nIndent = iOp+1; |
| |
| if( str_in_array(zOp, azNext) ){ |
| for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| } |
| if( str_in_array(zOp, azGoto) && p2op<p->nIndent |
| && (abYield[p2op] || sqlite3_column_int(pSql, 2)) |
| ){ |
| for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; |
| } |
| } |
| |
| p->iIndent = 0; |
| sqlite3_free(abYield); |
| sqlite3_reset(pSql); |
| } |
| |
| /* |
| ** Free the array allocated by explain_data_prepare(). |
| */ |
| static void explain_data_delete(ShellState *p){ |
| sqlite3_free(p->aiIndent); |
| p->aiIndent = 0; |
| p->nIndent = 0; |
| p->iIndent = 0; |
| } |
| |
| /* |
| ** Disable and restore .wheretrace and .selecttrace settings. |
| */ |
| #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| extern int sqlite3SelectTrace; |
| static int savedSelectTrace; |
| #endif |
| #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| extern int sqlite3WhereTrace; |
| static int savedWhereTrace; |
| #endif |
| static void disable_debug_trace_modes(void){ |
| #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| savedSelectTrace = sqlite3SelectTrace; |
| sqlite3SelectTrace = 0; |
| #endif |
| #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| savedWhereTrace = sqlite3WhereTrace; |
| sqlite3WhereTrace = 0; |
| #endif |
| } |
| static void restore_debug_trace_modes(void){ |
| #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
| sqlite3SelectTrace = savedSelectTrace; |
| #endif |
| #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
| sqlite3WhereTrace = savedWhereTrace; |
| #endif |
| } |
| |
| /* |
| ** Run a prepared statement |
| */ |
| static void exec_prepared_stmt( |
| ShellState *pArg, /* Pointer to ShellState */ |
| sqlite3_stmt *pStmt, /* Statment to run */ |
| int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */ |
| ){ |
| int rc; |
| |
| /* perform the first step. this will tell us if we |
| ** have a result set or not and how wide it is. |
| */ |
| rc = sqlite3_step(pStmt); |
| /* if we have a result set... */ |
| if( SQLITE_ROW == rc ){ |
| /* if we have a callback... */ |
| if( xCallback ){ |
| /* allocate space for col name ptr, value ptr, and type */ |
| int nCol = sqlite3_column_count(pStmt); |
| void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); |
| if( !pData ){ |
| rc = SQLITE_NOMEM; |
| }else{ |
| char **azCols = (char **)pData; /* Names of result columns */ |
| char **azVals = &azCols[nCol]; /* Results */ |
| int *aiTypes = (int *)&azVals[nCol]; /* Result types */ |
| int i, x; |
| assert(sizeof(int) <= sizeof(char *)); |
| /* save off ptrs to column names */ |
| for(i=0; i<nCol; i++){ |
| azCols[i] = (char *)sqlite3_column_name(pStmt, i); |
| } |
| do{ |
| /* extract the data and data types */ |
| for(i=0; i<nCol; i++){ |
| aiTypes[i] = x = sqlite3_column_type(pStmt, i); |
| if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ |
| azVals[i] = ""; |
| }else{ |
| azVals[i] = (char*)sqlite3_column_text(pStmt, i); |
| } |
| if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ |
| rc = SQLITE_NOMEM; |
| break; /* from for */ |
| } |
| } /* end for */ |
| |
| /* if data and types extracted successfully... */ |
| if( SQLITE_ROW == rc ){ |
| /* call the supplied callback with the result row data */ |
| if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ |
| rc = SQLITE_ABORT; |
| }else{ |
| rc = sqlite3_step(pStmt); |
| } |
| } |
| } while( SQLITE_ROW == rc ); |
| sqlite3_free(pData); |
| } |
| }else{ |
| do{ |
| rc = sqlite3_step(pStmt); |
| } while( rc == SQLITE_ROW ); |
| } |
| } |
| } |
| |
| /* |
| ** Execute a statement or set of statements. Print |
| ** any result rows/columns depending on the current mode |
** set via the supplied callback. |
** set via the supplied callback. |
** |
** |
** This is very similar to SQLite's built-in sqlite3_exec() | ** This is very similar to SQLite's built-in sqlite3_exec() |
** function except it takes a slightly different callback | ** function except it takes a slightly different callback |
** and callback data argument. |
** and callback data argument. |
*/ |
*/ |
static int shell_exec( |
static int shell_exec( |
sqlite3 *db, /* An open database */ | sqlite3 *db, /* An open database */ |
const char *zSql, /* SQL to be evaluated */ | const char *zSql, /* SQL to be evaluated */ |
int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ |
int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ |
/* (not the same as sqlite3_exec) */ | /* (not the same as sqlite3_exec) */ |
struct callback_data *pArg, /* Pointer to struct callback_data */ | ShellState *pArg, /* Pointer to ShellState */ |
char **pzErrMsg /* Error msg written here */ | char **pzErrMsg /* Error msg written here */ |
){ |
){ |
sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
int rc = SQLITE_OK; /* Return Code */ |
int rc = SQLITE_OK; /* Return Code */ |
Line 1140 static int shell_exec(
|
Line 1877 static int shell_exec(
|
} |
} |
|
|
while( zSql[0] && (SQLITE_OK == rc) ){ |
while( zSql[0] && (SQLITE_OK == rc) ){ |
|
static const char *zStmtSql; |
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
if( SQLITE_OK != rc ){ |
if( SQLITE_OK != rc ){ |
if( pzErrMsg ){ |
if( pzErrMsg ){ |
Line 1152 static int shell_exec(
|
Line 1890 static int shell_exec(
|
while( IsSpace(zSql[0]) ) zSql++; |
while( IsSpace(zSql[0]) ) zSql++; |
continue; |
continue; |
} |
} |
|
zStmtSql = sqlite3_sql(pStmt); |
|
while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
|
|
/* save off the prepared statment handle and reset row count */ |
/* save off the prepared statment handle and reset row count */ |
if( pArg ){ |
if( pArg ){ |
Line 1161 static int shell_exec(
|
Line 1901 static int shell_exec(
|
|
|
/* echo the sql statement if echo on */ |
/* echo the sql statement if echo on */ |
if( pArg && pArg->echoOn ){ |
if( pArg && pArg->echoOn ){ |
const char *zStmtSql = sqlite3_sql(pStmt); | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); | |
} |
} |
|
|
/* Output TESTCTRL_EXPLAIN text of requested */ | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
if( pArg && pArg->mode==MODE_Explain ){ | if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){ |
const char *zExplain = 0; | sqlite3_stmt *pExplain; |
sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain); | char *zEQP; |
if( zExplain && zExplain[0] ){ | disable_debug_trace_modes(); |
fprintf(pArg->out, "%s", zExplain); | zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql); |
| rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
| if( rc==SQLITE_OK ){ |
| while( sqlite3_step(pExplain)==SQLITE_ROW ){ |
| raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0)); |
| raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1)); |
| raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2)); |
| utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3)); |
| } |
} |
} |
|
sqlite3_finalize(pExplain); |
|
sqlite3_free(zEQP); |
|
if( pArg->autoEQP>=2 ){ |
|
/* Also do an EXPLAIN for ".eqp full" mode */ |
|
zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql); |
|
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); |
|
if( rc==SQLITE_OK ){ |
|
pArg->cMode = MODE_Explain; |
|
explain_data_prepare(pArg, pExplain); |
|
exec_prepared_stmt(pArg, pExplain, xCallback); |
|
explain_data_delete(pArg); |
|
} |
|
sqlite3_finalize(pExplain); |
|
sqlite3_free(zEQP); |
|
} |
|
restore_debug_trace_modes(); |
} |
} |
|
|
/* perform the first step. this will tell us if we | if( pArg ){ |
** have a result set or not and how wide it is. | pArg->cMode = pArg->mode; |
*/ | if( pArg->autoExplain |
rc = sqlite3_step(pStmt); | && sqlite3_column_count(pStmt)==8 |
/* if we have a result set... */ | && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0 |
if( SQLITE_ROW == rc ){ | ){ |
/* if we have a callback... */ | pArg->cMode = MODE_Explain; |
if( xCallback ){ | } |
/* allocate space for col name ptr, value ptr, and type */ | |
int nCol = sqlite3_column_count(pStmt); | |
void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1); | |
if( !pData ){ | |
rc = SQLITE_NOMEM; | |
}else{ | |
char **azCols = (char **)pData; /* Names of result columns */ | |
char **azVals = &azCols[nCol]; /* Results */ | |
int *aiTypes = (int *)&azVals[nCol]; /* Result types */ | |
int i; | |
assert(sizeof(int) <= sizeof(char *)); | |
/* save off ptrs to column names */ | |
for(i=0; i<nCol; i++){ | |
azCols[i] = (char *)sqlite3_column_name(pStmt, i); | |
} | |
do{ | |
/* extract the data and data types */ | |
for(i=0; i<nCol; i++){ | |
azVals[i] = (char *)sqlite3_column_text(pStmt, i); | |
aiTypes[i] = sqlite3_column_type(pStmt, i); | |
if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ | |
rc = SQLITE_NOMEM; | |
break; /* from for */ | |
} | |
} /* end for */ | |
|
|
/* if data and types extracted successfully... */ | /* If the shell is currently in ".explain" mode, gather the extra |
if( SQLITE_ROW == rc ){ | ** data required to add indents to the output.*/ |
/* call the supplied callback with the result row data */ | if( pArg->cMode==MODE_Explain ){ |
if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){ | explain_data_prepare(pArg, pStmt); |
rc = SQLITE_ABORT; | |
}else{ | |
rc = sqlite3_step(pStmt); | |
} | |
} | |
} while( SQLITE_ROW == rc ); | |
sqlite3_free(pData); | |
} | |
}else{ | |
do{ | |
rc = sqlite3_step(pStmt); | |
} while( rc == SQLITE_ROW ); | |
} |
} |
} |
} |
|
|
|
exec_prepared_stmt(pArg, pStmt, xCallback); |
|
explain_data_delete(pArg); |
|
|
/* print usage stats if stats on */ |
/* print usage stats if stats on */ |
if( pArg && pArg->statsOn ){ |
if( pArg && pArg->statsOn ){ |
display_stats(db, pArg, 0); |
display_stats(db, pArg, 0); |
} |
} |
|
|
/* Finalize the statement just executed. If this fails, save a | /* print loop-counters if required */ |
| if( pArg && pArg->scanstatsOn ){ |
| display_scanstats(db, pArg); |
| } |
| |
| /* Finalize the statement just executed. If this fails, save a |
** copy of the error message. Otherwise, set zSql to point to the |
** copy of the error message. Otherwise, set zSql to point to the |
** next statement to execute. */ |
** next statement to execute. */ |
rc2 = sqlite3_finalize(pStmt); |
rc2 = sqlite3_finalize(pStmt); |
Line 1267 static int dump_callback(void *pArg, int nArg, char **
|
Line 2001 static int dump_callback(void *pArg, int nArg, char **
|
const char *zType; |
const char *zType; |
const char *zSql; |
const char *zSql; |
const char *zPrepStmt = 0; |
const char *zPrepStmt = 0; |
struct callback_data *p = (struct callback_data *)pArg; | ShellState *p = (ShellState *)pArg; |
|
|
UNUSED_PARAMETER(azCol); |
UNUSED_PARAMETER(azCol); |
if( nArg!=3 ) return 1; |
if( nArg!=3 ) return 1; |
zTable = azArg[0]; |
zTable = azArg[0]; |
zType = azArg[1]; |
zType = azArg[1]; |
zSql = azArg[2]; |
zSql = azArg[2]; |
| |
if( strcmp(zTable, "sqlite_sequence")==0 ){ |
if( strcmp(zTable, "sqlite_sequence")==0 ){ |
zPrepStmt = "DELETE FROM sqlite_sequence;\n"; |
zPrepStmt = "DELETE FROM sqlite_sequence;\n"; |
}else if( strcmp(zTable, "sqlite_stat1")==0 ){ | }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){ |
fprintf(p->out, "ANALYZE sqlite_master;\n"); | raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
}else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
}else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
return 0; |
return 0; |
}else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
}else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
char *zIns; |
char *zIns; |
if( !p->writableSchema ){ |
if( !p->writableSchema ){ |
fprintf(p->out, "PRAGMA writable_schema=ON;\n"); | raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); |
p->writableSchema = 1; |
p->writableSchema = 1; |
} |
} |
zIns = sqlite3_mprintf( |
zIns = sqlite3_mprintf( |
"INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
"INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
"VALUES('table','%q','%q',0,'%q');", |
"VALUES('table','%q','%q',0,'%q');", |
zTable, zTable, zSql); |
zTable, zTable, zSql); |
fprintf(p->out, "%s\n", zIns); | utf8_printf(p->out, "%s\n", zIns); |
sqlite3_free(zIns); |
sqlite3_free(zIns); |
return 0; |
return 0; |
}else{ |
}else{ |
fprintf(p->out, "%s;\n", zSql); | utf8_printf(p->out, "%s;\n", zSql); |
} |
} |
|
|
if( strcmp(zType, "table")==0 ){ |
if( strcmp(zType, "table")==0 ){ |
Line 1304 static int dump_callback(void *pArg, int nArg, char **
|
Line 2038 static int dump_callback(void *pArg, int nArg, char **
|
char *zTableInfo = 0; |
char *zTableInfo = 0; |
char *zTmp = 0; |
char *zTmp = 0; |
int nRow = 0; |
int nRow = 0; |
| |
zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0); |
zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0); |
zTableInfo = appendText(zTableInfo, zTable, '"'); |
zTableInfo = appendText(zTableInfo, zTable, '"'); |
zTableInfo = appendText(zTableInfo, ");", 0); |
zTableInfo = appendText(zTableInfo, ");", 0); |
|
|
rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0); | rc = sqlite3_prepare_v2(p->db, zTableInfo, -1, &pTableInfo, 0); |
free(zTableInfo); |
free(zTableInfo); |
if( rc!=SQLITE_OK || !pTableInfo ){ |
if( rc!=SQLITE_OK || !pTableInfo ){ |
return 1; |
return 1; |
Line 1363 static int dump_callback(void *pArg, int nArg, char **
|
Line 2097 static int dump_callback(void *pArg, int nArg, char **
|
** "ORDER BY rowid DESC" to the end. |
** "ORDER BY rowid DESC" to the end. |
*/ |
*/ |
static int run_schema_dump_query( |
static int run_schema_dump_query( |
struct callback_data *p, | ShellState *p, |
const char *zQuery |
const char *zQuery |
){ |
){ |
int rc; |
int rc; |
Line 1372 static int run_schema_dump_query(
|
Line 2106 static int run_schema_dump_query(
|
if( rc==SQLITE_CORRUPT ){ |
if( rc==SQLITE_CORRUPT ){ |
char *zQ2; |
char *zQ2; |
int len = strlen30(zQuery); |
int len = strlen30(zQuery); |
fprintf(p->out, "/****** CORRUPTION ERROR *******/\n"); | raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); |
if( zErr ){ |
if( zErr ){ |
fprintf(p->out, "/****** %s ******/\n", zErr); | utf8_printf(p->out, "/****** %s ******/\n", zErr); |
sqlite3_free(zErr); |
sqlite3_free(zErr); |
zErr = 0; |
zErr = 0; |
} |
} |
Line 1383 static int run_schema_dump_query(
|
Line 2117 static int run_schema_dump_query(
|
sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery); |
rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); |
if( rc ){ |
if( rc ){ |
fprintf(p->out, "/****** ERROR: %s ******/\n", zErr); | utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); |
}else{ |
}else{ |
rc = SQLITE_CORRUPT; |
rc = SQLITE_CORRUPT; |
} |
} |
Line 1397 static int run_schema_dump_query(
|
Line 2131 static int run_schema_dump_query(
|
** Text of a help message |
** Text of a help message |
*/ |
*/ |
static char zHelp[] = |
static char zHelp[] = |
|
".auth ON|OFF Show authorizer callbacks\n" |
".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
".bail ON|OFF Stop after hitting an error. Default OFF\n" | ".bail on|off Stop after hitting an error. Default OFF\n" |
| ".binary on|off Turn binary output on or off. Default OFF\n" |
| ".changes on|off Show number of rows changed by SQL\n" |
| ".clone NEWDB Clone data into NEWDB from the existing database\n" |
".databases List names and files of attached databases\n" |
".databases List names and files of attached databases\n" |
|
".dbinfo ?DB? Show status information about the database\n" |
".dump ?TABLE? ... Dump the database in an SQL text format\n" |
".dump ?TABLE? ... Dump the database in an SQL text format\n" |
" If TABLE specified, only dump tables matching\n" |
" If TABLE specified, only dump tables matching\n" |
" LIKE pattern TABLE.\n" |
" LIKE pattern TABLE.\n" |
".echo ON|OFF Turn command echo on or off\n" | ".echo on|off Turn command echo on or off\n" |
| ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" |
".exit Exit this program\n" |
".exit Exit this program\n" |
".explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.\n" | ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n" |
" With no args, it turns EXPLAIN on.\n" | ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" |
".header(s) ON|OFF Turn display of headers on or off\n" | ".headers on|off Turn display of headers on or off\n" |
".help Show this message\n" |
".help Show this message\n" |
".import FILE TABLE Import data from FILE into TABLE\n" |
".import FILE TABLE Import data from FILE into TABLE\n" |
".indices ?TABLE? Show names of all indices\n" | ".indexes ?TABLE? Show names of all indexes\n" |
" If TABLE specified, only show indices for tables\n" | " If TABLE specified, only show indexes for tables\n" |
" matching LIKE pattern TABLE.\n" |
" matching LIKE pattern TABLE.\n" |
#ifdef SQLITE_ENABLE_IOTRACE |
#ifdef SQLITE_ENABLE_IOTRACE |
".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
#endif |
#endif |
|
".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" |
#ifndef SQLITE_OMIT_LOAD_EXTENSION |
#ifndef SQLITE_OMIT_LOAD_EXTENSION |
".load FILE ?ENTRY? Load an extension library\n" |
".load FILE ?ENTRY? Load an extension library\n" |
#endif |
#endif |
".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n" |
".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n" |
".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
|
" ascii Columns/rows delimited by 0x1F and 0x1E\n" |
" csv Comma-separated values\n" |
" csv Comma-separated values\n" |
" column Left-aligned columns. (See .width)\n" |
" column Left-aligned columns. (See .width)\n" |
" html HTML <table> code\n" |
" html HTML <table> code\n" |
" insert SQL insert statements for TABLE\n" |
" insert SQL insert statements for TABLE\n" |
" line One value per line\n" |
" line One value per line\n" |
" list Values delimited by .separator string\n" | " list Values delimited by .separator strings\n" |
" tabs Tab-separated values\n" |
" tabs Tab-separated values\n" |
" tcl TCL list elements\n" |
" tcl TCL list elements\n" |
".nullvalue STRING Use STRING in place of NULL values\n" |
".nullvalue STRING Use STRING in place of NULL values\n" |
".output FILENAME Send output to FILENAME\n" | ".once FILENAME Output for the next SQL command only to FILENAME\n" |
".output stdout Send output to the screen\n" | ".open ?FILENAME? Close existing database and reopen FILENAME\n" |
| ".output ?FILENAME? Send output to FILENAME or stdout\n" |
".print STRING... Print literal STRING\n" |
".print STRING... Print literal STRING\n" |
".prompt MAIN CONTINUE Replace the standard prompts\n" |
".prompt MAIN CONTINUE Replace the standard prompts\n" |
".quit Exit this program\n" |
".quit Exit this program\n" |
".read FILENAME Execute SQL in FILENAME\n" |
".read FILENAME Execute SQL in FILENAME\n" |
".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
".schema ?TABLE? Show the CREATE statements\n" | ".save FILE Write in-memory database into FILE\n" |
" If TABLE specified, only show tables matching\n" | ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" |
" LIKE pattern TABLE.\n" | ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" |
".separator STRING Change separator used by output mode and .import\n" | " Add --indent for pretty-printing\n" |
| ".separator COL ?ROW? Change the column separator and optionally the row\n" |
| " separator for both the output mode and .import\n" |
| #if defined(SQLITE_ENABLE_SESSION) |
| ".session CMD ... Create or control sessions\n" |
| #endif |
| ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" |
".show Show the current values for various settings\n" |
".show Show the current values for various settings\n" |
".stats ON|OFF Turn stats on or off\n" | ".stats ?on|off? Show stats or turn stats on or off\n" |
| ".system CMD ARGS... Run CMD ARGS... in a system shell\n" |
".tables ?TABLE? List names of tables\n" |
".tables ?TABLE? List names of tables\n" |
" If TABLE specified, only list tables matching\n" |
" If TABLE specified, only list tables matching\n" |
" LIKE pattern TABLE.\n" |
" LIKE pattern TABLE.\n" |
".timeout MS Try opening locked tables for MS milliseconds\n" |
".timeout MS Try opening locked tables for MS milliseconds\n" |
|
".timer on|off Turn SQL timer on or off\n" |
".trace FILE|off Output each SQL statement as it is run\n" |
".trace FILE|off Output each SQL statement as it is run\n" |
|
".vfsinfo ?AUX? Information about the top-level VFS\n" |
|
".vfslist List all available VFSes\n" |
".vfsname ?AUX? Print the name of the VFS stack\n" |
".vfsname ?AUX? Print the name of the VFS stack\n" |
".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" |
|
" Negative values right-justify\n" |
; |
; |
|
|
static char zTimerHelp[] = | #if defined(SQLITE_ENABLE_SESSION) |
".timer ON|OFF Turn the CPU timer measurement on or off\n" | /* |
; | ** Print help information for the ".sessions" command |
| */ |
| void session_help(ShellState *p){ |
| raw_printf(p->out, |
| ".session ?NAME? SUBCOMMAND ?ARGS...?\n" |
| "If ?NAME? is omitted, the first defined session is used.\n" |
| "Subcommands:\n" |
| " attach TABLE Attach TABLE\n" |
| " changeset FILE Write a changeset into FILE\n" |
| " close Close one session\n" |
| " enable ?BOOLEAN? Set or query the enable bit\n" |
| " filter GLOB... Reject tables matching GLOBs\n" |
| " indirect ?BOOLEAN? Mark or query the indirect status\n" |
| " isempty Query whether the session is empty\n" |
| " list List currently open session names\n" |
| " open DB NAME Open a new session on DB\n" |
| " patchset FILE Write a patchset into FILE\n" |
| ); |
| } |
| #endif |
|
|
|
|
/* Forward reference */ |
/* Forward reference */ |
static int process_input(struct callback_data *p, FILE *in); | static int process_input(ShellState *p, FILE *in); |
| /* |
| ** Implementation of the "readfile(X)" SQL function. The entire content |
| ** of the file named X is read and returned as a BLOB. NULL is returned |
| ** if the file does not exist or is unreadable. |
| */ |
| static void readfileFunc( |
| sqlite3_context *context, |
| int argc, |
| sqlite3_value **argv |
| ){ |
| const char *zName; |
| FILE *in; |
| long nIn; |
| void *pBuf; |
|
|
|
UNUSED_PARAMETER(argc); |
|
zName = (const char*)sqlite3_value_text(argv[0]); |
|
if( zName==0 ) return; |
|
in = fopen(zName, "rb"); |
|
if( in==0 ) return; |
|
fseek(in, 0, SEEK_END); |
|
nIn = ftell(in); |
|
rewind(in); |
|
pBuf = sqlite3_malloc64( nIn ); |
|
if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ |
|
sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); |
|
}else{ |
|
sqlite3_free(pBuf); |
|
} |
|
fclose(in); |
|
} |
|
|
/* |
/* |
|
** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
|
** is written into file X. The number of bytes written is returned. Or |
|
** NULL is returned if something goes wrong, such as being unable to open |
|
** file X for writing. |
|
*/ |
|
static void writefileFunc( |
|
sqlite3_context *context, |
|
int argc, |
|
sqlite3_value **argv |
|
){ |
|
FILE *out; |
|
const char *z; |
|
sqlite3_int64 rc; |
|
const char *zFile; |
|
|
|
UNUSED_PARAMETER(argc); |
|
zFile = (const char*)sqlite3_value_text(argv[0]); |
|
if( zFile==0 ) return; |
|
out = fopen(zFile, "wb"); |
|
if( out==0 ) return; |
|
z = (const char*)sqlite3_value_blob(argv[1]); |
|
if( z==0 ){ |
|
rc = 0; |
|
}else{ |
|
rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
|
} |
|
fclose(out); |
|
sqlite3_result_int64(context, rc); |
|
} |
|
|
|
#if defined(SQLITE_ENABLE_SESSION) |
|
/* |
|
** Close a single OpenSession object and release all of its associated |
|
** resources. |
|
*/ |
|
static void session_close(OpenSession *pSession){ |
|
int i; |
|
sqlite3session_delete(pSession->p); |
|
sqlite3_free(pSession->zName); |
|
for(i=0; i<pSession->nFilter; i++){ |
|
sqlite3_free(pSession->azFilter[i]); |
|
} |
|
sqlite3_free(pSession->azFilter); |
|
memset(pSession, 0, sizeof(OpenSession)); |
|
} |
|
#endif |
|
|
|
/* |
|
** Close all OpenSession objects and release all associated resources. |
|
*/ |
|
#if defined(SQLITE_ENABLE_SESSION) |
|
static void session_close_all(ShellState *p){ |
|
int i; |
|
for(i=0; i<p->nSession; i++){ |
|
session_close(&p->aSession[i]); |
|
} |
|
p->nSession = 0; |
|
} |
|
#else |
|
# define session_close_all(X) |
|
#endif |
|
|
|
/* |
|
** Implementation of the xFilter function for an open session. Omit |
|
** any tables named by ".session filter" but let all other table through. |
|
*/ |
|
#if defined(SQLITE_ENABLE_SESSION) |
|
static int session_filter(void *pCtx, const char *zTab){ |
|
OpenSession *pSession = (OpenSession*)pCtx; |
|
int i; |
|
for(i=0; i<pSession->nFilter; i++){ |
|
if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; |
|
} |
|
return 1; |
|
} |
|
#endif |
|
|
|
/* |
** Make sure the database is open. If it is not, then open it. If |
** Make sure the database is open. If it is not, then open it. If |
** the database fails to open, print an error message and exit. |
** the database fails to open, print an error message and exit. |
*/ |
*/ |
static void open_db(struct callback_data *p){ | static void open_db(ShellState *p, int keepAlive){ |
if( p->db==0 ){ |
if( p->db==0 ){ |
sqlite3_initialize(); |
sqlite3_initialize(); |
sqlite3_open(p->zDbFilename, &p->db); |
sqlite3_open(p->zDbFilename, &p->db); |
db = p->db; | globalDb = p->db; |
if( db && sqlite3_errcode(db)==SQLITE_OK ){ | if( p->db && sqlite3_errcode(p->db)==SQLITE_OK ){ |
sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0, | sqlite3_create_function(p->db, "shellstatic", 0, SQLITE_UTF8, 0, |
shellstaticFunc, 0, 0); |
shellstaticFunc, 0, 0); |
} |
} |
if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){ | if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ |
fprintf(stderr,"Error: unable to open database \"%s\": %s\n", | utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n", |
p->zDbFilename, sqlite3_errmsg(db)); | p->zDbFilename, sqlite3_errmsg(p->db)); |
| if( keepAlive ) return; |
exit(1); |
exit(1); |
} |
} |
#ifndef SQLITE_OMIT_LOAD_EXTENSION |
#ifndef SQLITE_OMIT_LOAD_EXTENSION |
sqlite3_enable_load_extension(p->db, 1); |
sqlite3_enable_load_extension(p->db, 1); |
#endif |
#endif |
|
sqlite3_create_function(p->db, "readfile", 1, SQLITE_UTF8, 0, |
|
readfileFunc, 0, 0); |
|
sqlite3_create_function(p->db, "writefile", 2, SQLITE_UTF8, 0, |
|
writefileFunc, 0, 0); |
} |
} |
} |
} |
|
|
/* |
/* |
** Do C-language style dequoting. |
** Do C-language style dequoting. |
** |
** |
|
** \a -> alarm |
|
** \b -> backspace |
** \t -> tab |
** \t -> tab |
** \n -> newline |
** \n -> newline |
|
** \v -> vertical tab |
|
** \f -> form feed |
** \r -> carriage return |
** \r -> carriage return |
** \NNN -> ascii character NNN in octal | ** \s -> space |
| ** \" -> " |
| ** \' -> ' |
** \\ -> backslash |
** \\ -> backslash |
|
** \NNN -> ascii character NNN in octal |
*/ |
*/ |
static void resolve_backslashes(char *z){ |
static void resolve_backslashes(char *z){ |
int i, j; |
int i, j; |
char c; |
char c; |
|
while( *z && *z!='\\' ) z++; |
for(i=j=0; (c = z[i])!=0; i++, j++){ |
for(i=j=0; (c = z[i])!=0; i++, j++){ |
if( c=='\\' ){ | if( c=='\\' && z[i+1]!=0 ){ |
c = z[++i]; |
c = z[++i]; |
if( c=='n' ){ | if( c=='a' ){ |
c = '\n'; | c = '\a'; |
| }else if( c=='b' ){ |
| c = '\b'; |
}else if( c=='t' ){ |
}else if( c=='t' ){ |
c = '\t'; |
c = '\t'; |
|
}else if( c=='n' ){ |
|
c = '\n'; |
|
}else if( c=='v' ){ |
|
c = '\v'; |
|
}else if( c=='f' ){ |
|
c = '\f'; |
}else if( c=='r' ){ |
}else if( c=='r' ){ |
c = '\r'; |
c = '\r'; |
|
}else if( c=='"' ){ |
|
c = '"'; |
|
}else if( c=='\'' ){ |
|
c = '\''; |
|
}else if( c=='\\' ){ |
|
c = '\\'; |
}else if( c>='0' && c<='7' ){ |
}else if( c>='0' && c<='7' ){ |
c -= '0'; |
c -= '0'; |
if( z[i+1]>='0' && z[i+1]<='7' ){ |
if( z[i+1]>='0' && z[i+1]<='7' ){ |
Line 1518 static void resolve_backslashes(char *z){
|
Line 2428 static void resolve_backslashes(char *z){
|
} |
} |
z[j] = c; |
z[j] = c; |
} |
} |
z[j] = 0; | if( j<i ) z[j] = 0; |
} |
} |
|
|
/* |
/* |
** Interpret zArg as a boolean value. Return either 0 or 1. | ** Return the value of a hexadecimal digit. Return -1 if the input |
| ** is not a hex digit. |
*/ |
*/ |
|
static int hexDigitValue(char c){ |
|
if( c>='0' && c<='9' ) return c - '0'; |
|
if( c>='a' && c<='f' ) return c - 'a' + 10; |
|
if( c>='A' && c<='F' ) return c - 'A' + 10; |
|
return -1; |
|
} |
|
|
|
/* |
|
** Interpret zArg as an integer value, possibly with suffixes. |
|
*/ |
|
static sqlite3_int64 integerValue(const char *zArg){ |
|
sqlite3_int64 v = 0; |
|
static const struct { char *zSuffix; int iMult; } aMult[] = { |
|
{ "KiB", 1024 }, |
|
{ "MiB", 1024*1024 }, |
|
{ "GiB", 1024*1024*1024 }, |
|
{ "KB", 1000 }, |
|
{ "MB", 1000000 }, |
|
{ "GB", 1000000000 }, |
|
{ "K", 1000 }, |
|
{ "M", 1000000 }, |
|
{ "G", 1000000000 }, |
|
}; |
|
int i; |
|
int isNeg = 0; |
|
if( zArg[0]=='-' ){ |
|
isNeg = 1; |
|
zArg++; |
|
}else if( zArg[0]=='+' ){ |
|
zArg++; |
|
} |
|
if( zArg[0]=='0' && zArg[1]=='x' ){ |
|
int x; |
|
zArg += 2; |
|
while( (x = hexDigitValue(zArg[0]))>=0 ){ |
|
v = (v<<4) + x; |
|
zArg++; |
|
} |
|
}else{ |
|
while( IsDigit(zArg[0]) ){ |
|
v = v*10 + zArg[0] - '0'; |
|
zArg++; |
|
} |
|
} |
|
for(i=0; i<ArraySize(aMult); i++){ |
|
if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
|
v *= aMult[i].iMult; |
|
break; |
|
} |
|
} |
|
return isNeg? -v : v; |
|
} |
|
|
|
/* |
|
** Interpret zArg as either an integer or a boolean value. Return 1 or 0 |
|
** for TRUE and FALSE. Return the integer value if appropriate. |
|
*/ |
static int booleanValue(char *zArg){ |
static int booleanValue(char *zArg){ |
int val = atoi(zArg); | int i; |
int j; | if( zArg[0]=='0' && zArg[1]=='x' ){ |
for(j=0; zArg[j]; j++){ | for(i=2; hexDigitValue(zArg[i])>=0; i++){} |
zArg[j] = ToLower(zArg[j]); | }else{ |
| for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} |
} |
} |
if( strcmp(zArg,"on")==0 ){ | if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff); |
val = 1; | if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ |
}else if( strcmp(zArg,"yes")==0 ){ | return 1; |
val = 1; | |
} |
} |
return val; | if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ |
| return 0; |
| } |
| utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", |
| zArg); |
| return 0; |
} |
} |
|
|
/* |
/* |
Line 1547 static void output_file_close(FILE *f){
|
Line 2520 static void output_file_close(FILE *f){
|
|
|
/* |
/* |
** Try to open an output file. The names "stdout" and "stderr" are |
** Try to open an output file. The names "stdout" and "stderr" are |
** recognized and do the right thing. NULL is returned if the output | ** recognized and do the right thing. NULL is returned if the output |
** filename is "off". |
** filename is "off". |
*/ |
*/ |
static FILE *output_file_open(const char *zFile){ |
static FILE *output_file_open(const char *zFile){ |
Line 1561 static FILE *output_file_open(const char *zFile){
|
Line 2534 static FILE *output_file_open(const char *zFile){
|
}else{ |
}else{ |
f = fopen(zFile, "wb"); |
f = fopen(zFile, "wb"); |
if( f==0 ){ |
if( f==0 ){ |
fprintf(stderr, "Error: cannot open \"%s\"\n", zFile); | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
} |
} |
} |
} |
return f; |
return f; |
Line 1570 static FILE *output_file_open(const char *zFile){
|
Line 2543 static FILE *output_file_open(const char *zFile){
|
/* |
/* |
** A routine for handling output from sqlite3_trace(). |
** A routine for handling output from sqlite3_trace(). |
*/ |
*/ |
static void sql_trace_callback(void *pArg, const char *z){ | static int sql_trace_callback( |
| unsigned mType, |
| void *pArg, |
| void *pP, |
| void *pX |
| ){ |
FILE *f = (FILE*)pArg; |
FILE *f = (FILE*)pArg; |
if( f ) fprintf(f, "%s\n", z); | UNUSED_PARAMETER(mType); |
| UNUSED_PARAMETER(pP); |
| if( f ){ |
| const char *z = (const char*)pX; |
| int i = (int)strlen(z); |
| while( i>0 && z[i-1]==';' ){ i--; } |
| utf8_printf(f, "%.*s;\n", i, z); |
| } |
| return 0; |
} |
} |
|
|
/* |
/* |
Line 1585 static void test_breakpoint(void){
|
Line 2571 static void test_breakpoint(void){
|
} |
} |
|
|
/* |
/* |
|
** An object used to read a CSV and other files for import. |
|
*/ |
|
typedef struct ImportCtx ImportCtx; |
|
struct ImportCtx { |
|
const char *zFile; /* Name of the input file */ |
|
FILE *in; /* Read the CSV text from this input stream */ |
|
char *z; /* Accumulated text for a field */ |
|
int n; /* Number of bytes in z */ |
|
int nAlloc; /* Space allocated for z[] */ |
|
int nLine; /* Current line number */ |
|
int cTerm; /* Character that terminated the most recent field */ |
|
int cColSep; /* The column separator character. (Usually ",") */ |
|
int cRowSep; /* The row separator character. (Usually "\n") */ |
|
}; |
|
|
|
/* Append a single byte to z[] */ |
|
static void import_append_char(ImportCtx *p, int c){ |
|
if( p->n+1>=p->nAlloc ){ |
|
p->nAlloc += p->nAlloc + 100; |
|
p->z = sqlite3_realloc64(p->z, p->nAlloc); |
|
if( p->z==0 ){ |
|
raw_printf(stderr, "out of memory\n"); |
|
exit(1); |
|
} |
|
} |
|
p->z[p->n++] = (char)c; |
|
} |
|
|
|
/* Read a single field of CSV text. Compatible with rfc4180 and extended |
|
** with the option of having a separator other than ",". |
|
** |
|
** + Input comes from p->in. |
|
** + Store results in p->z of length p->n. Space to hold p->z comes |
|
** from sqlite3_malloc64(). |
|
** + Use p->cSep as the column separator. The default is ",". |
|
** + Use p->rSep as the row separator. The default is "\n". |
|
** + Keep track of the line number in p->nLine. |
|
** + Store the character that terminates the field in p->cTerm. Store |
|
** EOF on end-of-file. |
|
** + Report syntax errors on stderr |
|
*/ |
|
static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){ |
|
int c; |
|
int cSep = p->cColSep; |
|
int rSep = p->cRowSep; |
|
p->n = 0; |
|
c = fgetc(p->in); |
|
if( c==EOF || seenInterrupt ){ |
|
p->cTerm = EOF; |
|
return 0; |
|
} |
|
if( c=='"' ){ |
|
int pc, ppc; |
|
int startLine = p->nLine; |
|
int cQuote = c; |
|
pc = ppc = 0; |
|
while( 1 ){ |
|
c = fgetc(p->in); |
|
if( c==rSep ) p->nLine++; |
|
if( c==cQuote ){ |
|
if( pc==cQuote ){ |
|
pc = 0; |
|
continue; |
|
} |
|
} |
|
if( (c==cSep && pc==cQuote) |
|
|| (c==rSep && pc==cQuote) |
|
|| (c==rSep && pc=='\r' && ppc==cQuote) |
|
|| (c==EOF && pc==cQuote) |
|
){ |
|
do{ p->n--; }while( p->z[p->n]!=cQuote ); |
|
p->cTerm = c; |
|
break; |
|
} |
|
if( pc==cQuote && c!='\r' ){ |
|
utf8_printf(stderr, "%s:%d: unescaped %c character\n", |
|
p->zFile, p->nLine, cQuote); |
|
} |
|
if( c==EOF ){ |
|
utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", |
|
p->zFile, startLine, cQuote); |
|
p->cTerm = c; |
|
break; |
|
} |
|
import_append_char(p, c); |
|
ppc = pc; |
|
pc = c; |
|
} |
|
}else{ |
|
while( c!=EOF && c!=cSep && c!=rSep ){ |
|
import_append_char(p, c); |
|
c = fgetc(p->in); |
|
} |
|
if( c==rSep ){ |
|
p->nLine++; |
|
if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; |
|
} |
|
p->cTerm = c; |
|
} |
|
if( p->z ) p->z[p->n] = 0; |
|
return p->z; |
|
} |
|
|
|
/* Read a single field of ASCII delimited text. |
|
** |
|
** + Input comes from p->in. |
|
** + Store results in p->z of length p->n. Space to hold p->z comes |
|
** from sqlite3_malloc64(). |
|
** + Use p->cSep as the column separator. The default is "\x1F". |
|
** + Use p->rSep as the row separator. The default is "\x1E". |
|
** + Keep track of the row number in p->nLine. |
|
** + Store the character that terminates the field in p->cTerm. Store |
|
** EOF on end-of-file. |
|
** + Report syntax errors on stderr |
|
*/ |
|
static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){ |
|
int c; |
|
int cSep = p->cColSep; |
|
int rSep = p->cRowSep; |
|
p->n = 0; |
|
c = fgetc(p->in); |
|
if( c==EOF || seenInterrupt ){ |
|
p->cTerm = EOF; |
|
return 0; |
|
} |
|
while( c!=EOF && c!=cSep && c!=rSep ){ |
|
import_append_char(p, c); |
|
c = fgetc(p->in); |
|
} |
|
if( c==rSep ){ |
|
p->nLine++; |
|
} |
|
p->cTerm = c; |
|
if( p->z ) p->z[p->n] = 0; |
|
return p->z; |
|
} |
|
|
|
/* |
|
** Try to transfer data for table zTable. If an error is seen while |
|
** moving forward, try to go backwards. The backwards movement won't |
|
** work for WITHOUT ROWID tables. |
|
*/ |
|
static void tryToCloneData( |
|
ShellState *p, |
|
sqlite3 *newDb, |
|
const char *zTable |
|
){ |
|
sqlite3_stmt *pQuery = 0; |
|
sqlite3_stmt *pInsert = 0; |
|
char *zQuery = 0; |
|
char *zInsert = 0; |
|
int rc; |
|
int i, j, n; |
|
int nTable = (int)strlen(zTable); |
|
int k = 0; |
|
int cnt = 0; |
|
const int spinRate = 10000; |
|
|
|
zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable); |
|
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
|
if( rc ){ |
|
utf8_printf(stderr, "Error %d: %s on [%s]\n", |
|
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
|
zQuery); |
|
goto end_data_xfer; |
|
} |
|
n = sqlite3_column_count(pQuery); |
|
zInsert = sqlite3_malloc64(200 + nTable + n*3); |
|
if( zInsert==0 ){ |
|
raw_printf(stderr, "out of memory\n"); |
|
goto end_data_xfer; |
|
} |
|
sqlite3_snprintf(200+nTable,zInsert, |
|
"INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable); |
|
i = (int)strlen(zInsert); |
|
for(j=1; j<n; j++){ |
|
memcpy(zInsert+i, ",?", 2); |
|
i += 2; |
|
} |
|
memcpy(zInsert+i, ");", 3); |
|
rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); |
|
if( rc ){ |
|
utf8_printf(stderr, "Error %d: %s on [%s]\n", |
|
sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), |
|
zQuery); |
|
goto end_data_xfer; |
|
} |
|
for(k=0; k<2; k++){ |
|
while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
|
for(i=0; i<n; i++){ |
|
switch( sqlite3_column_type(pQuery, i) ){ |
|
case SQLITE_NULL: { |
|
sqlite3_bind_null(pInsert, i+1); |
|
break; |
|
} |
|
case SQLITE_INTEGER: { |
|
sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i)); |
|
break; |
|
} |
|
case SQLITE_FLOAT: { |
|
sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i)); |
|
break; |
|
} |
|
case SQLITE_TEXT: { |
|
sqlite3_bind_text(pInsert, i+1, |
|
(const char*)sqlite3_column_text(pQuery,i), |
|
-1, SQLITE_STATIC); |
|
break; |
|
} |
|
case SQLITE_BLOB: { |
|
sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i), |
|
sqlite3_column_bytes(pQuery,i), |
|
SQLITE_STATIC); |
|
break; |
|
} |
|
} |
|
} /* End for */ |
|
rc = sqlite3_step(pInsert); |
|
if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){ |
|
utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb), |
|
sqlite3_errmsg(newDb)); |
|
} |
|
sqlite3_reset(pInsert); |
|
cnt++; |
|
if( (cnt%spinRate)==0 ){ |
|
printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); |
|
fflush(stdout); |
|
} |
|
} /* End while */ |
|
if( rc==SQLITE_DONE ) break; |
|
sqlite3_finalize(pQuery); |
|
sqlite3_free(zQuery); |
|
zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;", |
|
zTable); |
|
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
|
if( rc ){ |
|
utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable); |
|
break; |
|
} |
|
} /* End for(k=0...) */ |
|
|
|
end_data_xfer: |
|
sqlite3_finalize(pQuery); |
|
sqlite3_finalize(pInsert); |
|
sqlite3_free(zQuery); |
|
sqlite3_free(zInsert); |
|
} |
|
|
|
|
|
/* |
|
** Try to transfer all rows of the schema that match zWhere. For |
|
** each row, invoke xForEach() on the object defined by that row. |
|
** If an error is encountered while moving forward through the |
|
** sqlite_master table, try again moving backwards. |
|
*/ |
|
static void tryToCloneSchema( |
|
ShellState *p, |
|
sqlite3 *newDb, |
|
const char *zWhere, |
|
void (*xForEach)(ShellState*,sqlite3*,const char*) |
|
){ |
|
sqlite3_stmt *pQuery = 0; |
|
char *zQuery = 0; |
|
int rc; |
|
const unsigned char *zName; |
|
const unsigned char *zSql; |
|
char *zErrMsg = 0; |
|
|
|
zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
|
" WHERE %s", zWhere); |
|
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
|
if( rc ){ |
|
utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
|
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
|
zQuery); |
|
goto end_schema_xfer; |
|
} |
|
while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
|
zName = sqlite3_column_text(pQuery, 0); |
|
zSql = sqlite3_column_text(pQuery, 1); |
|
printf("%s... ", zName); fflush(stdout); |
|
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
|
if( zErrMsg ){ |
|
utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
|
sqlite3_free(zErrMsg); |
|
zErrMsg = 0; |
|
} |
|
if( xForEach ){ |
|
xForEach(p, newDb, (const char*)zName); |
|
} |
|
printf("done\n"); |
|
} |
|
if( rc!=SQLITE_DONE ){ |
|
sqlite3_finalize(pQuery); |
|
sqlite3_free(zQuery); |
|
zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master" |
|
" WHERE %s ORDER BY rowid DESC", zWhere); |
|
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); |
|
if( rc ){ |
|
utf8_printf(stderr, "Error: (%d) %s on [%s]\n", |
|
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), |
|
zQuery); |
|
goto end_schema_xfer; |
|
} |
|
while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){ |
|
zName = sqlite3_column_text(pQuery, 0); |
|
zSql = sqlite3_column_text(pQuery, 1); |
|
printf("%s... ", zName); fflush(stdout); |
|
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg); |
|
if( zErrMsg ){ |
|
utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql); |
|
sqlite3_free(zErrMsg); |
|
zErrMsg = 0; |
|
} |
|
if( xForEach ){ |
|
xForEach(p, newDb, (const char*)zName); |
|
} |
|
printf("done\n"); |
|
} |
|
} |
|
end_schema_xfer: |
|
sqlite3_finalize(pQuery); |
|
sqlite3_free(zQuery); |
|
} |
|
|
|
/* |
|
** Open a new database file named "zNewDb". Try to recover as much information |
|
** as possible out of the main database (which might be corrupt) and write it |
|
** into zNewDb. |
|
*/ |
|
static void tryToClone(ShellState *p, const char *zNewDb){ |
|
int rc; |
|
sqlite3 *newDb = 0; |
|
if( access(zNewDb,0)==0 ){ |
|
utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb); |
|
return; |
|
} |
|
rc = sqlite3_open(zNewDb, &newDb); |
|
if( rc ){ |
|
utf8_printf(stderr, "Cannot create output database: %s\n", |
|
sqlite3_errmsg(newDb)); |
|
}else{ |
|
sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); |
|
sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0); |
|
tryToCloneSchema(p, newDb, "type='table'", tryToCloneData); |
|
tryToCloneSchema(p, newDb, "type!='table'", 0); |
|
sqlite3_exec(newDb, "COMMIT;", 0, 0, 0); |
|
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
|
} |
|
sqlite3_close(newDb); |
|
} |
|
|
|
/* |
|
** Change the output file back to stdout |
|
*/ |
|
static void output_reset(ShellState *p){ |
|
if( p->outfile[0]=='|' ){ |
|
#ifndef SQLITE_OMIT_POPEN |
|
pclose(p->out); |
|
#endif |
|
}else{ |
|
output_file_close(p->out); |
|
} |
|
p->outfile[0] = 0; |
|
p->out = stdout; |
|
} |
|
|
|
/* |
|
** Run an SQL command and return the single integer result. |
|
*/ |
|
static int db_int(ShellState *p, const char *zSql){ |
|
sqlite3_stmt *pStmt; |
|
int res = 0; |
|
sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
|
if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
|
res = sqlite3_column_int(pStmt,0); |
|
} |
|
sqlite3_finalize(pStmt); |
|
return res; |
|
} |
|
|
|
/* |
|
** Convert a 2-byte or 4-byte big-endian integer into a native integer |
|
*/ |
|
static unsigned int get2byteInt(unsigned char *a){ |
|
return (a[0]<<8) + a[1]; |
|
} |
|
static unsigned int get4byteInt(unsigned char *a){ |
|
return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
|
} |
|
|
|
/* |
|
** Implementation of the ".info" command. |
|
** |
|
** Return 1 on error, 2 to exit, and 0 otherwise. |
|
*/ |
|
static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ |
|
static const struct { const char *zName; int ofst; } aField[] = { |
|
{ "file change counter:", 24 }, |
|
{ "database page count:", 28 }, |
|
{ "freelist page count:", 36 }, |
|
{ "schema cookie:", 40 }, |
|
{ "schema format:", 44 }, |
|
{ "default cache size:", 48 }, |
|
{ "autovacuum top root:", 52 }, |
|
{ "incremental vacuum:", 64 }, |
|
{ "text encoding:", 56 }, |
|
{ "user version:", 60 }, |
|
{ "application id:", 68 }, |
|
{ "software version:", 96 }, |
|
}; |
|
static const struct { const char *zName; const char *zSql; } aQuery[] = { |
|
{ "number of tables:", |
|
"SELECT count(*) FROM %s WHERE type='table'" }, |
|
{ "number of indexes:", |
|
"SELECT count(*) FROM %s WHERE type='index'" }, |
|
{ "number of triggers:", |
|
"SELECT count(*) FROM %s WHERE type='trigger'" }, |
|
{ "number of views:", |
|
"SELECT count(*) FROM %s WHERE type='view'" }, |
|
{ "schema size:", |
|
"SELECT total(length(sql)) FROM %s" }, |
|
}; |
|
sqlite3_file *pFile = 0; |
|
int i; |
|
char *zSchemaTab; |
|
char *zDb = nArg>=2 ? azArg[1] : "main"; |
|
unsigned char aHdr[100]; |
|
open_db(p, 0); |
|
if( p->db==0 ) return 1; |
|
sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile); |
|
if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){ |
|
return 1; |
|
} |
|
i = pFile->pMethods->xRead(pFile, aHdr, 100, 0); |
|
if( i!=SQLITE_OK ){ |
|
raw_printf(stderr, "unable to read database header\n"); |
|
return 1; |
|
} |
|
i = get2byteInt(aHdr+16); |
|
if( i==1 ) i = 65536; |
|
utf8_printf(p->out, "%-20s %d\n", "database page size:", i); |
|
utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); |
|
utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); |
|
utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); |
|
for(i=0; i<ArraySize(aField); i++){ |
|
int ofst = aField[i].ofst; |
|
unsigned int val = get4byteInt(aHdr + ofst); |
|
utf8_printf(p->out, "%-20s %u", aField[i].zName, val); |
|
switch( ofst ){ |
|
case 56: { |
|
if( val==1 ) raw_printf(p->out, " (utf8)"); |
|
if( val==2 ) raw_printf(p->out, " (utf16le)"); |
|
if( val==3 ) raw_printf(p->out, " (utf16be)"); |
|
} |
|
} |
|
raw_printf(p->out, "\n"); |
|
} |
|
if( zDb==0 ){ |
|
zSchemaTab = sqlite3_mprintf("main.sqlite_master"); |
|
}else if( strcmp(zDb,"temp")==0 ){ |
|
zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master"); |
|
}else{ |
|
zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb); |
|
} |
|
for(i=0; i<ArraySize(aQuery); i++){ |
|
char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab); |
|
int val = db_int(p, zSql); |
|
sqlite3_free(zSql); |
|
utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); |
|
} |
|
sqlite3_free(zSchemaTab); |
|
return 0; |
|
} |
|
|
|
/* |
|
** Print the current sqlite3_errmsg() value to stderr and return 1. |
|
*/ |
|
static int shellDatabaseError(sqlite3 *db){ |
|
const char *zErr = sqlite3_errmsg(db); |
|
utf8_printf(stderr, "Error: %s\n", zErr); |
|
return 1; |
|
} |
|
|
|
/* |
|
** Print an out-of-memory message to stderr and return 1. |
|
*/ |
|
static int shellNomemError(void){ |
|
raw_printf(stderr, "Error: out of memory\n"); |
|
return 1; |
|
} |
|
|
|
/* |
|
** Compare the string as a command-line option with either one or two |
|
** initial "-" characters. |
|
*/ |
|
static int optionMatch(const char *zStr, const char *zOpt){ |
|
if( zStr[0]!='-' ) return 0; |
|
zStr++; |
|
if( zStr[0]=='-' ) zStr++; |
|
return strcmp(zStr, zOpt)==0; |
|
} |
|
|
|
/* |
** If an input line begins with "." then invoke this routine to |
** If an input line begins with "." then invoke this routine to |
** process that line. |
** process that line. |
** |
** |
** Return 1 on error, 2 to exit, and 0 otherwise. |
** Return 1 on error, 2 to exit, and 0 otherwise. |
*/ |
*/ |
static int do_meta_command(char *zLine, struct callback_data *p){ | static int do_meta_command(char *zLine, ShellState *p){ |
int i = 1; | int h = 1; |
int nArg = 0; |
int nArg = 0; |
int n, c; |
int n, c; |
int rc = 0; |
int rc = 0; |
Line 1599 static int do_meta_command(char *zLine, struct callbac
|
Line 3089 static int do_meta_command(char *zLine, struct callbac
|
|
|
/* Parse the input line into tokens. |
/* Parse the input line into tokens. |
*/ |
*/ |
while( zLine[i] && nArg<ArraySize(azArg) ){ | while( zLine[h] && nArg<ArraySize(azArg) ){ |
while( IsSpace(zLine[i]) ){ i++; } | while( IsSpace(zLine[h]) ){ h++; } |
if( zLine[i]==0 ) break; | if( zLine[h]==0 ) break; |
if( zLine[i]=='\'' || zLine[i]=='"' ){ | if( zLine[h]=='\'' || zLine[h]=='"' ){ |
int delim = zLine[i++]; | int delim = zLine[h++]; |
azArg[nArg++] = &zLine[i]; | azArg[nArg++] = &zLine[h]; |
while( zLine[i] && zLine[i]!=delim ){ i++; } | while( zLine[h] && zLine[h]!=delim ){ |
if( zLine[i]==delim ){ | if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++; |
zLine[i++] = 0; | h++; |
} |
} |
|
if( zLine[h]==delim ){ |
|
zLine[h++] = 0; |
|
} |
if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
}else{ |
}else{ |
azArg[nArg++] = &zLine[i]; | azArg[nArg++] = &zLine[h]; |
while( zLine[i] && !IsSpace(zLine[i]) ){ i++; } | while( zLine[h] && !IsSpace(zLine[h]) ){ h++; } |
if( zLine[i] ) zLine[i++] = 0; | if( zLine[h] ) zLine[h++] = 0; |
resolve_backslashes(azArg[nArg-1]); |
resolve_backslashes(azArg[nArg-1]); |
} |
} |
} |
} |
Line 1623 static int do_meta_command(char *zLine, struct callbac
|
Line 3116 static int do_meta_command(char *zLine, struct callbac
|
if( nArg==0 ) return 0; /* no tokens, no error */ |
if( nArg==0 ) return 0; /* no tokens, no error */ |
n = strlen30(azArg[0]); |
n = strlen30(azArg[0]); |
c = azArg[0][0]; |
c = azArg[0][0]; |
if( c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0 && nArg>1 && nArg<4){ | |
const char *zDestFile; | if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){ |
const char *zDb; | if( nArg!=2 ){ |
| raw_printf(stderr, "Usage: .auth ON|OFF\n"); |
| rc = 1; |
| goto meta_command_exit; |
| } |
| open_db(p, 0); |
| if( booleanValue(azArg[1]) ){ |
| sqlite3_set_authorizer(p->db, shellAuth, p); |
| }else{ |
| sqlite3_set_authorizer(p->db, 0, 0); |
| } |
| }else |
| |
| if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0) |
| || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0) |
| ){ |
| const char *zDestFile = 0; |
| const char *zDb = 0; |
sqlite3 *pDest; |
sqlite3 *pDest; |
sqlite3_backup *pBackup; |
sqlite3_backup *pBackup; |
if( nArg==2 ){ | int j; |
zDestFile = azArg[1]; | for(j=1; j<nArg; j++){ |
zDb = "main"; | const char *z = azArg[j]; |
}else{ | if( z[0]=='-' ){ |
zDestFile = azArg[2]; | while( z[0]=='-' ) z++; |
zDb = azArg[1]; | /* No options to process at this time */ |
| { |
| utf8_printf(stderr, "unknown option: %s\n", azArg[j]); |
| return 1; |
| } |
| }else if( zDestFile==0 ){ |
| zDestFile = azArg[j]; |
| }else if( zDb==0 ){ |
| zDb = zDestFile; |
| zDestFile = azArg[j]; |
| }else{ |
| raw_printf(stderr, "too many arguments to .backup\n"); |
| return 1; |
| } |
} |
} |
|
if( zDestFile==0 ){ |
|
raw_printf(stderr, "missing FILENAME argument on .backup\n"); |
|
return 1; |
|
} |
|
if( zDb==0 ) zDb = "main"; |
rc = sqlite3_open(zDestFile, &pDest); |
rc = sqlite3_open(zDestFile, &pDest); |
if( rc!=SQLITE_OK ){ |
if( rc!=SQLITE_OK ){ |
fprintf(stderr, "Error: cannot open \"%s\"\n", zDestFile); | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile); |
sqlite3_close(pDest); |
sqlite3_close(pDest); |
return 1; |
return 1; |
} |
} |
open_db(p); | open_db(p, 0); |
pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
if( pBackup==0 ){ |
if( pBackup==0 ){ |
fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
sqlite3_close(pDest); |
sqlite3_close(pDest); |
return 1; |
return 1; |
} |
} |
Line 1653 static int do_meta_command(char *zLine, struct callbac
|
Line 3181 static int do_meta_command(char *zLine, struct callbac
|
if( rc==SQLITE_DONE ){ |
if( rc==SQLITE_DONE ){ |
rc = 0; |
rc = 0; |
}else{ |
}else{ |
fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
rc = 1; |
rc = 1; |
} |
} |
sqlite3_close(pDest); |
sqlite3_close(pDest); |
}else |
}else |
|
|
if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 && nArg<3 ){ | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){ |
bail_on_error = booleanValue(azArg[1]); | if( nArg==2 ){ |
| bail_on_error = booleanValue(azArg[1]); |
| }else{ |
| raw_printf(stderr, "Usage: .bail on|off\n"); |
| rc = 1; |
| } |
}else |
}else |
|
|
|
if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){ |
|
if( nArg==2 ){ |
|
if( booleanValue(azArg[1]) ){ |
|
setBinaryMode(p->out, 1); |
|
}else{ |
|
setTextMode(p->out, 1); |
|
} |
|
}else{ |
|
raw_printf(stderr, "Usage: .binary on|off\n"); |
|
rc = 1; |
|
} |
|
}else |
|
|
/* The undocumented ".breakpoint" command causes a call to the no-op |
/* The undocumented ".breakpoint" command causes a call to the no-op |
** routine named test_breakpoint(). |
** routine named test_breakpoint(). |
*/ |
*/ |
Line 1670 static int do_meta_command(char *zLine, struct callbac
|
Line 3216 static int do_meta_command(char *zLine, struct callbac
|
test_breakpoint(); |
test_breakpoint(); |
}else |
}else |
|
|
if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){ | if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){ |
struct callback_data data; | if( nArg==2 ){ |
| p->countChanges = booleanValue(azArg[1]); |
| }else{ |
| raw_printf(stderr, "Usage: .changes on|off\n"); |
| rc = 1; |
| } |
| }else |
| |
| if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){ |
| if( nArg==2 ){ |
| tryToClone(p, azArg[1]); |
| }else{ |
| raw_printf(stderr, "Usage: .clone FILENAME\n"); |
| rc = 1; |
| } |
| }else |
| |
| if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
| ShellState data; |
char *zErrMsg = 0; |
char *zErrMsg = 0; |
open_db(p); | open_db(p, 0); |
memcpy(&data, p, sizeof(data)); |
memcpy(&data, p, sizeof(data)); |
data.showHeader = 1; |
data.showHeader = 1; |
data.mode = MODE_Column; | data.cMode = data.mode = MODE_Column; |
data.colWidth[0] = 3; |
data.colWidth[0] = 3; |
data.colWidth[1] = 15; |
data.colWidth[1] = 15; |
data.colWidth[2] = 58; |
data.colWidth[2] = 58; |
data.cnt = 0; |
data.cnt = 0; |
sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg); |
sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg); |
if( zErrMsg ){ |
if( zErrMsg ){ |
fprintf(stderr,"Error: %s\n", zErrMsg); | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
sqlite3_free(zErrMsg); |
sqlite3_free(zErrMsg); |
rc = 1; |
rc = 1; |
} |
} |
}else |
}else |
|
|
if( c=='d' && strncmp(azArg[0], "dump", n)==0 && nArg<3 ){ | if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){ |
open_db(p); | rc = shell_dbinfo_command(p, nArg, azArg); |
| }else |
| |
| if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
| open_db(p, 0); |
/* When playing back a "dump", the content might appear in an order |
/* When playing back a "dump", the content might appear in an order |
** which causes immediate foreign key constraints to be violated. |
** which causes immediate foreign key constraints to be violated. |
** So disable foreign-key constraint enforcement to prevent problems. */ |
** So disable foreign-key constraint enforcement to prevent problems. */ |
fprintf(p->out, "PRAGMA foreign_keys=OFF;\n"); | if( nArg!=1 && nArg!=2 ){ |
fprintf(p->out, "BEGIN TRANSACTION;\n"); | raw_printf(stderr, "Usage: .dump ?LIKE-PATTERN?\n"); |
| rc = 1; |
| goto meta_command_exit; |
| } |
| raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
| raw_printf(p->out, "BEGIN TRANSACTION;\n"); |
p->writableSchema = 0; |
p->writableSchema = 0; |
sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); |
p->nErr = 0; |
p->nErr = 0; |
if( nArg==1 ){ |
if( nArg==1 ){ |
run_schema_dump_query(p, | run_schema_dump_query(p, |
"SELECT name, type, sql FROM sqlite_master " |
"SELECT name, type, sql FROM sqlite_master " |
"WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
"WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" |
); |
); |
run_schema_dump_query(p, | run_schema_dump_query(p, |
"SELECT name, type, sql FROM sqlite_master " |
"SELECT name, type, sql FROM sqlite_master " |
"WHERE name=='sqlite_sequence'" |
"WHERE name=='sqlite_sequence'" |
); |
); |
Line 1730 static int do_meta_command(char *zLine, struct callbac
|
Line 3303 static int do_meta_command(char *zLine, struct callbac
|
} |
} |
} |
} |
if( p->writableSchema ){ |
if( p->writableSchema ){ |
fprintf(p->out, "PRAGMA writable_schema=OFF;\n"); | raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); |
p->writableSchema = 0; |
p->writableSchema = 0; |
} |
} |
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); |
sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); |
fprintf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); | raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n"); |
}else |
}else |
|
|
if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 && nArg<3 ){ | if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){ |
p->echoOn = booleanValue(azArg[1]); | if( nArg==2 ){ |
| p->echoOn = booleanValue(azArg[1]); |
| }else{ |
| raw_printf(stderr, "Usage: .echo on|off\n"); |
| rc = 1; |
| } |
}else |
}else |
|
|
if( c=='e' && strncmp(azArg[0], "exit", n)==0 && nArg==1 ){ | if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){ |
| if( nArg==2 ){ |
| if( strcmp(azArg[1],"full")==0 ){ |
| p->autoEQP = 2; |
| }else{ |
| p->autoEQP = booleanValue(azArg[1]); |
| } |
| }else{ |
| raw_printf(stderr, "Usage: .eqp on|off|full\n"); |
| rc = 1; |
| } |
| }else |
| |
| if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
| if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); |
rc = 2; |
rc = 2; |
}else |
}else |
|
|
if( c=='e' && strncmp(azArg[0], "explain", n)==0 && nArg<3 ){ | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
int val = nArg>=2 ? booleanValue(azArg[1]) : 1; | int val = 1; |
if(val == 1) { | if( nArg>=2 ){ |
if(!p->explainPrev.valid) { | if( strcmp(azArg[1],"auto")==0 ){ |
p->explainPrev.valid = 1; | val = 99; |
p->explainPrev.mode = p->mode; | }else{ |
p->explainPrev.showHeader = p->showHeader; | val = booleanValue(azArg[1]); |
memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth)); | |
} |
} |
/* We could put this code under the !p->explainValid | } |
** condition so that it does not execute if we are already in | if( val==1 && p->mode!=MODE_Explain ){ |
** explain mode. However, always executing it allows us an easy | p->normalMode = p->mode; |
** was to reset to explain mode in case the user previously | |
** did an .explain followed by a .width, .mode or .header | |
** command. | |
*/ | |
p->mode = MODE_Explain; |
p->mode = MODE_Explain; |
p->showHeader = 1; | p->autoExplain = 0; |
memset(p->colWidth,0,ArraySize(p->colWidth)); | }else if( val==0 ){ |
p->colWidth[0] = 4; /* addr */ | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
p->colWidth[1] = 13; /* opcode */ | p->autoExplain = 0; |
p->colWidth[2] = 4; /* P1 */ | }else if( val==99 ){ |
p->colWidth[3] = 4; /* P2 */ | if( p->mode==MODE_Explain ) p->mode = p->normalMode; |
p->colWidth[4] = 4; /* P3 */ | p->autoExplain = 1; |
p->colWidth[5] = 13; /* P4 */ | |
p->colWidth[6] = 2; /* P5 */ | |
p->colWidth[7] = 13; /* Comment */ | |
}else if (p->explainPrev.valid) { | |
p->explainPrev.valid = 0; | |
p->mode = p->explainPrev.mode; | |
p->showHeader = p->explainPrev.showHeader; | |
memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth)); | |
} |
} |
}else |
}else |
|
|
if( c=='h' && (strncmp(azArg[0], "header", n)==0 || | if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){ |
strncmp(azArg[0], "headers", n)==0) && nArg>1 && nArg<3 ){ | ShellState data; |
p->showHeader = booleanValue(azArg[1]); | char *zErrMsg = 0; |
| int doStats = 0; |
| memcpy(&data, p, sizeof(data)); |
| data.showHeader = 0; |
| data.cMode = data.mode = MODE_Semi; |
| if( nArg==2 && optionMatch(azArg[1], "indent") ){ |
| data.cMode = data.mode = MODE_Pretty; |
| nArg = 1; |
| } |
| if( nArg!=1 ){ |
| raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); |
| rc = 1; |
| goto meta_command_exit; |
| } |
| open_db(p, 0); |
| rc = sqlite3_exec(p->db, |
| "SELECT sql FROM" |
| " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
| " FROM sqlite_master UNION ALL" |
| " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
| "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
| "ORDER BY rowid", |
| callback, &data, &zErrMsg |
| ); |
| if( rc==SQLITE_OK ){ |
| sqlite3_stmt *pStmt; |
| rc = sqlite3_prepare_v2(p->db, |
| "SELECT rowid FROM sqlite_master" |
| " WHERE name GLOB 'sqlite_stat[134]'", |
| -1, &pStmt, 0); |
| doStats = sqlite3_step(pStmt)==SQLITE_ROW; |
| sqlite3_finalize(pStmt); |
| } |
| if( doStats==0 ){ |
| raw_printf(p->out, "/* No STAT tables available */\n"); |
| }else{ |
| raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'", |
| callback, &data, &zErrMsg); |
| data.cMode = data.mode = MODE_Insert; |
| data.zDestTable = "sqlite_stat1"; |
| shell_exec(p->db, "SELECT * FROM sqlite_stat1", |
| shell_callback, &data,&zErrMsg); |
| data.zDestTable = "sqlite_stat3"; |
| shell_exec(p->db, "SELECT * FROM sqlite_stat3", |
| shell_callback, &data,&zErrMsg); |
| data.zDestTable = "sqlite_stat4"; |
| shell_exec(p->db, "SELECT * FROM sqlite_stat4", |
| shell_callback, &data, &zErrMsg); |
| raw_printf(p->out, "ANALYZE sqlite_master;\n"); |
| } |
}else |
}else |
|
|
if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ | if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){ |
fprintf(stderr,"%s",zHelp); | if( nArg==2 ){ |
if( HAS_TIMER ){ | p->showHeader = booleanValue(azArg[1]); |
fprintf(stderr,"%s",zTimerHelp); | }else{ |
| raw_printf(stderr, "Usage: .headers on|off\n"); |
| rc = 1; |
} |
} |
}else |
}else |
|
|
if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg==3 ){ | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
char *zTable = azArg[2]; /* Insert data into this table */ | utf8_printf(p->out, "%s", zHelp); |
char *zFile = azArg[1]; /* The file from which to extract data */ | }else |
| |
| if( c=='i' && strncmp(azArg[0], "import", n)==0 ){ |
| char *zTable; /* Insert data into this table */ |
| char *zFile; /* Name of file to extra content from */ |
sqlite3_stmt *pStmt = NULL; /* A statement */ |
sqlite3_stmt *pStmt = NULL; /* A statement */ |
int nCol; /* Number of columns in the table */ |
int nCol; /* Number of columns in the table */ |
int nByte; /* Number of bytes in an SQL string */ |
int nByte; /* Number of bytes in an SQL string */ |
int i, j; /* Loop counters */ |
int i, j; /* Loop counters */ |
int nSep; /* Number of bytes in p->separator[] */ | int needCommit; /* True to COMMIT or ROLLBACK at end */ |
| int nSep; /* Number of bytes in p->colSeparator[] */ |
char *zSql; /* An SQL statement */ |
char *zSql; /* An SQL statement */ |
char *zLine; /* A single line of input from the file */ | ImportCtx sCtx; /* Reader context */ |
char **azCol; /* zLine[] broken up into columns */ | char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ |
char *zCommit; /* How to commit changes */ | int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */ |
FILE *in; /* The input file */ | |
int lineno = 0; /* Line number of input file */ | |
|
|
open_db(p); | if( nArg!=3 ){ |
nSep = strlen30(p->separator); | raw_printf(stderr, "Usage: .import FILE TABLE\n"); |
| goto meta_command_exit; |
| } |
| zFile = azArg[1]; |
| zTable = azArg[2]; |
| seenInterrupt = 0; |
| memset(&sCtx, 0, sizeof(sCtx)); |
| open_db(p, 0); |
| nSep = strlen30(p->colSeparator); |
if( nSep==0 ){ |
if( nSep==0 ){ |
fprintf(stderr, "Error: non-null separator required for import\n"); | raw_printf(stderr, |
| "Error: non-null column separator required for import\n"); |
return 1; |
return 1; |
} |
} |
|
if( nSep>1 ){ |
|
raw_printf(stderr, "Error: multi-character column separators not allowed" |
|
" for import\n"); |
|
return 1; |
|
} |
|
nSep = strlen30(p->rowSeparator); |
|
if( nSep==0 ){ |
|
raw_printf(stderr, "Error: non-null row separator required for import\n"); |
|
return 1; |
|
} |
|
if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){ |
|
/* When importing CSV (only), if the row separator is set to the |
|
** default output row separator, change it to the default input |
|
** row separator. This avoids having to maintain different input |
|
** and output row separators. */ |
|
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); |
|
nSep = strlen30(p->rowSeparator); |
|
} |
|
if( nSep>1 ){ |
|
raw_printf(stderr, "Error: multi-character row separators not allowed" |
|
" for import\n"); |
|
return 1; |
|
} |
|
sCtx.zFile = zFile; |
|
sCtx.nLine = 1; |
|
if( sCtx.zFile[0]=='|' ){ |
|
#ifdef SQLITE_OMIT_POPEN |
|
raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
|
return 1; |
|
#else |
|
sCtx.in = popen(sCtx.zFile+1, "r"); |
|
sCtx.zFile = "<pipe>"; |
|
xCloser = pclose; |
|
#endif |
|
}else{ |
|
sCtx.in = fopen(sCtx.zFile, "rb"); |
|
xCloser = fclose; |
|
} |
|
if( p->mode==MODE_Ascii ){ |
|
xRead = ascii_read_one_field; |
|
}else{ |
|
xRead = csv_read_one_field; |
|
} |
|
if( sCtx.in==0 ){ |
|
utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile); |
|
return 1; |
|
} |
|
sCtx.cColSep = p->colSeparator[0]; |
|
sCtx.cRowSep = p->rowSeparator[0]; |
zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
zSql = sqlite3_mprintf("SELECT * FROM %s", zTable); |
if( zSql==0 ){ |
if( zSql==0 ){ |
fprintf(stderr, "Error: out of memory\n"); | raw_printf(stderr, "Error: out of memory\n"); |
| xCloser(sCtx.in); |
return 1; |
return 1; |
} |
} |
nByte = strlen30(zSql); |
nByte = strlen30(zSql); |
rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0); | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ |
| if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ |
| char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable); |
| char cSep = '('; |
| while( xRead(&sCtx) ){ |
| zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z); |
| cSep = ','; |
| if( sCtx.cTerm!=sCtx.cColSep ) break; |
| } |
| if( cSep=='(' ){ |
| sqlite3_free(zCreate); |
| sqlite3_free(sCtx.z); |
| xCloser(sCtx.in); |
| utf8_printf(stderr,"%s: empty file\n", sCtx.zFile); |
| return 1; |
| } |
| zCreate = sqlite3_mprintf("%z\n)", zCreate); |
| rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); |
| sqlite3_free(zCreate); |
| if( rc ){ |
| utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable, |
| sqlite3_errmsg(p->db)); |
| sqlite3_free(sCtx.z); |
| xCloser(sCtx.in); |
| return 1; |
| } |
| rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| } |
sqlite3_free(zSql); |
sqlite3_free(zSql); |
if( rc ){ |
if( rc ){ |
if (pStmt) sqlite3_finalize(pStmt); |
if (pStmt) sqlite3_finalize(pStmt); |
fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db)); | utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); |
| xCloser(sCtx.in); |
return 1; |
return 1; |
} |
} |
nCol = sqlite3_column_count(pStmt); |
nCol = sqlite3_column_count(pStmt); |
sqlite3_finalize(pStmt); |
sqlite3_finalize(pStmt); |
pStmt = 0; |
pStmt = 0; |
if( nCol==0 ) return 0; /* no columns, no error */ |
if( nCol==0 ) return 0; /* no columns, no error */ |
zSql = malloc( nByte + 20 + nCol*2 ); | zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 ); |
if( zSql==0 ){ |
if( zSql==0 ){ |
fprintf(stderr, "Error: out of memory\n"); | raw_printf(stderr, "Error: out of memory\n"); |
| xCloser(sCtx.in); |
return 1; |
return 1; |
} |
} |
sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zTable); | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); |
j = strlen30(zSql); |
j = strlen30(zSql); |
for(i=1; i<nCol; i++){ |
for(i=1; i<nCol; i++){ |
zSql[j++] = ','; |
zSql[j++] = ','; |
Line 1844 static int do_meta_command(char *zLine, struct callbac
|
Line 3567 static int do_meta_command(char *zLine, struct callbac
|
} |
} |
zSql[j++] = ')'; |
zSql[j++] = ')'; |
zSql[j] = 0; |
zSql[j] = 0; |
rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0); | rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
free(zSql); | sqlite3_free(zSql); |
if( rc ){ |
if( rc ){ |
fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db)); | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
if (pStmt) sqlite3_finalize(pStmt); |
if (pStmt) sqlite3_finalize(pStmt); |
|
xCloser(sCtx.in); |
return 1; |
return 1; |
} |
} |
in = fopen(zFile, "rb"); | needCommit = sqlite3_get_autocommit(p->db); |
if( in==0 ){ | if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
fprintf(stderr, "Error: cannot open \"%s\"\n", zFile); | do{ |
sqlite3_finalize(pStmt); | int startLine = sCtx.nLine; |
return 1; | |
} | |
azCol = malloc( sizeof(azCol[0])*(nCol+1) ); | |
if( azCol==0 ){ | |
fprintf(stderr, "Error: out of memory\n"); | |
fclose(in); | |
sqlite3_finalize(pStmt); | |
return 1; | |
} | |
sqlite3_exec(p->db, "BEGIN", 0, 0, 0); | |
zCommit = "COMMIT"; | |
while( (zLine = local_getline(0, in, 1))!=0 ){ | |
char *z, c; | |
int inQuote = 0; | |
lineno++; | |
azCol[0] = zLine; | |
for(i=0, z=zLine; (c = *z)!=0; z++){ | |
if( c=='"' ) inQuote = !inQuote; | |
if( c=='\n' ) lineno++; | |
if( !inQuote && c==p->separator[0] && strncmp(z,p->separator,nSep)==0 ){ | |
*z = 0; | |
i++; | |
if( i<nCol ){ | |
azCol[i] = &z[nSep]; | |
z += nSep-1; | |
} | |
} | |
} /* end for */ | |
*z = 0; | |
if( i+1!=nCol ){ | |
fprintf(stderr, | |
"Error: %s line %d: expected %d columns of data but found %d\n", | |
zFile, lineno, nCol, i+1); | |
zCommit = "ROLLBACK"; | |
free(zLine); | |
rc = 1; | |
break; /* from while */ | |
} | |
for(i=0; i<nCol; i++){ |
for(i=0; i<nCol; i++){ |
if( azCol[i][0]=='"' ){ | char *z = xRead(&sCtx); |
int k; | /* |
for(z=azCol[i], j=1, k=0; z[j]; j++){ | ** Did we reach end-of-file before finding any columns? |
if( z[j]=='"' ){ j++; if( z[j]==0 ) break; } | ** If so, stop instead of NULL filling the remaining columns. |
z[k++] = z[j]; | */ |
} | if( z==0 && i==0 ) break; |
z[k] = 0; | /* |
| ** Did we reach end-of-file OR end-of-line before finding any |
| ** columns in ASCII mode? If so, stop instead of NULL filling |
| ** the remaining columns. |
| */ |
| if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; |
| sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); |
| if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ |
| utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
| "filling the rest with NULL\n", |
| sCtx.zFile, startLine, nCol, i+1); |
| i += 2; |
| while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; } |
} |
} |
sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); |
|
} |
} |
sqlite3_step(pStmt); | if( sCtx.cTerm==sCtx.cColSep ){ |
rc = sqlite3_reset(pStmt); | do{ |
free(zLine); | xRead(&sCtx); |
if( rc!=SQLITE_OK ){ | i++; |
fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db)); | }while( sCtx.cTerm==sCtx.cColSep ); |
zCommit = "ROLLBACK"; | utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " |
rc = 1; | "extras ignored\n", |
break; /* from while */ | sCtx.zFile, startLine, nCol, i); |
} |
} |
} /* end while */ | if( i>=nCol ){ |
free(azCol); | sqlite3_step(pStmt); |
fclose(in); | rc = sqlite3_reset(pStmt); |
| if( rc!=SQLITE_OK ){ |
| utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, |
| startLine, sqlite3_errmsg(p->db)); |
| } |
| } |
| }while( sCtx.cTerm!=EOF ); |
| |
| xCloser(sCtx.in); |
| sqlite3_free(sCtx.z); |
sqlite3_finalize(pStmt); |
sqlite3_finalize(pStmt); |
sqlite3_exec(p->db, zCommit, 0, 0, 0); | if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); |
}else |
}else |
|
|
if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg<3 ){ | if( c=='i' && (strncmp(azArg[0], "indices", n)==0 |
struct callback_data data; | || strncmp(azArg[0], "indexes", n)==0) ){ |
| ShellState data; |
char *zErrMsg = 0; |
char *zErrMsg = 0; |
open_db(p); | open_db(p, 0); |
memcpy(&data, p, sizeof(data)); |
memcpy(&data, p, sizeof(data)); |
data.showHeader = 0; |
data.showHeader = 0; |
data.mode = MODE_List; | data.cMode = data.mode = MODE_List; |
if( nArg==1 ){ |
if( nArg==1 ){ |
rc = sqlite3_exec(p->db, |
rc = sqlite3_exec(p->db, |
"SELECT name FROM sqlite_master " |
"SELECT name FROM sqlite_master " |
Line 1937 static int do_meta_command(char *zLine, struct callbac
|
Line 3644 static int do_meta_command(char *zLine, struct callbac
|
"ORDER BY 1", |
"ORDER BY 1", |
callback, &data, &zErrMsg |
callback, &data, &zErrMsg |
); |
); |
}else{ | }else if( nArg==2 ){ |
zShellStatic = azArg[1]; |
zShellStatic = azArg[1]; |
rc = sqlite3_exec(p->db, |
rc = sqlite3_exec(p->db, |
"SELECT name FROM sqlite_master " |
"SELECT name FROM sqlite_master " |
Line 1949 static int do_meta_command(char *zLine, struct callbac
|
Line 3656 static int do_meta_command(char *zLine, struct callbac
|
callback, &data, &zErrMsg |
callback, &data, &zErrMsg |
); |
); |
zShellStatic = 0; |
zShellStatic = 0; |
|
}else{ |
|
raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); |
|
rc = 1; |
|
goto meta_command_exit; |
} |
} |
if( zErrMsg ){ |
if( zErrMsg ){ |
fprintf(stderr,"Error: %s\n", zErrMsg); | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
sqlite3_free(zErrMsg); |
sqlite3_free(zErrMsg); |
rc = 1; |
rc = 1; |
}else if( rc != SQLITE_OK ){ |
}else if( rc != SQLITE_OK ){ |
fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n"); | raw_printf(stderr, |
| "Error: querying sqlite_master and sqlite_temp_master\n"); |
rc = 1; |
rc = 1; |
} |
} |
}else |
}else |
|
|
#ifdef SQLITE_ENABLE_IOTRACE |
#ifdef SQLITE_ENABLE_IOTRACE |
if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
extern void (*sqlite3IoTrace)(const char*, ...); | SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); |
if( iotrace && iotrace!=stdout ) fclose(iotrace); |
if( iotrace && iotrace!=stdout ) fclose(iotrace); |
iotrace = 0; |
iotrace = 0; |
if( nArg<2 ){ |
if( nArg<2 ){ |
Line 1973 static int do_meta_command(char *zLine, struct callbac
|
Line 3685 static int do_meta_command(char *zLine, struct callbac
|
}else{ |
}else{ |
iotrace = fopen(azArg[1], "w"); |
iotrace = fopen(azArg[1], "w"); |
if( iotrace==0 ){ |
if( iotrace==0 ){ |
fprintf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); | utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); |
sqlite3IoTrace = 0; |
sqlite3IoTrace = 0; |
rc = 1; |
rc = 1; |
}else{ |
}else{ |
Line 1982 static int do_meta_command(char *zLine, struct callbac
|
Line 3694 static int do_meta_command(char *zLine, struct callbac
|
} |
} |
}else |
}else |
#endif |
#endif |
|
if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){ |
|
static const struct { |
|
const char *zLimitName; /* Name of a limit */ |
|
int limitCode; /* Integer code for that limit */ |
|
} aLimit[] = { |
|
{ "length", SQLITE_LIMIT_LENGTH }, |
|
{ "sql_length", SQLITE_LIMIT_SQL_LENGTH }, |
|
{ "column", SQLITE_LIMIT_COLUMN }, |
|
{ "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, |
|
{ "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, |
|
{ "vdbe_op", SQLITE_LIMIT_VDBE_OP }, |
|
{ "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, |
|
{ "attached", SQLITE_LIMIT_ATTACHED }, |
|
{ "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, |
|
{ "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, |
|
{ "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, |
|
{ "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, |
|
}; |
|
int i, n2; |
|
open_db(p, 0); |
|
if( nArg==1 ){ |
|
for(i=0; i<ArraySize(aLimit); i++){ |
|
printf("%20s %d\n", aLimit[i].zLimitName, |
|
sqlite3_limit(p->db, aLimit[i].limitCode, -1)); |
|
} |
|
}else if( nArg>3 ){ |
|
raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); |
|
rc = 1; |
|
goto meta_command_exit; |
|
}else{ |
|
int iLimit = -1; |
|
n2 = strlen30(azArg[1]); |
|
for(i=0; i<ArraySize(aLimit); i++){ |
|
if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){ |
|
if( iLimit<0 ){ |
|
iLimit = i; |
|
}else{ |
|
utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]); |
|
rc = 1; |
|
goto meta_command_exit; |
|
} |
|
} |
|
} |
|
if( iLimit<0 ){ |
|
utf8_printf(stderr, "unknown limit: \"%s\"\n" |
|
"enter \".limits\" with no arguments for a list.\n", |
|
azArg[1]); |
|
rc = 1; |
|
goto meta_command_exit; |
|
} |
|
if( nArg==3 ){ |
|
sqlite3_limit(p->db, aLimit[iLimit].limitCode, |
|
(int)integerValue(azArg[2])); |
|
} |
|
printf("%20s %d\n", aLimit[iLimit].zLimitName, |
|
sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); |
|
} |
|
}else |
|
|
#ifndef SQLITE_OMIT_LOAD_EXTENSION |
#ifndef SQLITE_OMIT_LOAD_EXTENSION |
if( c=='l' && strncmp(azArg[0], "load", n)==0 && nArg>=2 ){ | if( c=='l' && strncmp(azArg[0], "load", n)==0 ){ |
const char *zFile, *zProc; |
const char *zFile, *zProc; |
char *zErrMsg = 0; |
char *zErrMsg = 0; |
|
if( nArg<2 ){ |
|
raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n"); |
|
rc = 1; |
|
goto meta_command_exit; |
|
} |
zFile = azArg[1]; |
zFile = azArg[1]; |
zProc = nArg>=3 ? azArg[2] : 0; |
zProc = nArg>=3 ? azArg[2] : 0; |
open_db(p); | open_db(p, 0); |
rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
if( rc!=SQLITE_OK ){ |
if( rc!=SQLITE_OK ){ |
fprintf(stderr, "Error: %s\n", zErrMsg); | utf8_printf(stderr, "Error: %s\n", zErrMsg); |
sqlite3_free(zErrMsg); |
sqlite3_free(zErrMsg); |
rc = 1; |
rc = 1; |
} |
} |
}else |
}else |
#endif |
#endif |
|
|
if( c=='l' && strncmp(azArg[0], "log", n)==0 && nArg>=2 ){ | if( c=='l' && strncmp(azArg[0], "log", n)==0 ){ |
const char *zFile = azArg[1]; | if( nArg!=2 ){ |
output_file_close(p->pLog); | raw_printf(stderr, "Usage: .log FILENAME\n"); |
p->pLog = output_file_open(zFile); | rc = 1; |
| }else{ |
| const char *zFile = azArg[1]; |
| output_file_close(p->pLog); |
| p->pLog = output_file_open(zFile); |
| } |
}else |
}else |
|
|
if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==2 ){ | if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){ |
int n2 = strlen30(azArg[1]); | const char *zMode = nArg>=2 ? azArg[1] : ""; |
if( (n2==4 && strncmp(azArg[1],"line",n2)==0) | int n2 = (int)strlen(zMode); |
|| | int c2 = zMode[0]; |
(n2==5 && strncmp(azArg[1],"lines",n2)==0) ){ | if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){ |
p->mode = MODE_Line; |
p->mode = MODE_Line; |
}else if( (n2==6 && strncmp(azArg[1],"column",n2)==0) | }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){ |
|| | |
(n2==7 && strncmp(azArg[1],"columns",n2)==0) ){ | |
p->mode = MODE_Column; |
p->mode = MODE_Column; |
}else if( n2==4 && strncmp(azArg[1],"list",n2)==0 ){ | }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){ |
p->mode = MODE_List; |
p->mode = MODE_List; |
}else if( n2==4 && strncmp(azArg[1],"html",n2)==0 ){ | }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){ |
p->mode = MODE_Html; |
p->mode = MODE_Html; |
}else if( n2==3 && strncmp(azArg[1],"tcl",n2)==0 ){ | }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){ |
p->mode = MODE_Tcl; |
p->mode = MODE_Tcl; |
sqlite3_snprintf(sizeof(p->separator), p->separator, " "); | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); |
}else if( n2==3 && strncmp(azArg[1],"csv",n2)==0 ){ | }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){ |
p->mode = MODE_Csv; |
p->mode = MODE_Csv; |
sqlite3_snprintf(sizeof(p->separator), p->separator, ","); | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); |
}else if( n2==4 && strncmp(azArg[1],"tabs",n2)==0 ){ | sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); |
| }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){ |
p->mode = MODE_List; |
p->mode = MODE_List; |
sqlite3_snprintf(sizeof(p->separator), p->separator, "\t"); | sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); |
}else if( n2==6 && strncmp(azArg[1],"insert",n2)==0 ){ | }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){ |
p->mode = MODE_Insert; |
p->mode = MODE_Insert; |
set_table_name(p, "table"); | set_table_name(p, nArg>=3 ? azArg[2] : "table"); |
| }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){ |
| p->mode = MODE_Ascii; |
| sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); |
| sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); |
}else { |
}else { |
fprintf(stderr,"Error: mode should be one of: " | raw_printf(stderr, "Error: mode should be one of: " |
"column csv html insert line list tabs tcl\n"); | "ascii column csv html insert line list tabs tcl\n"); |
rc = 1; |
rc = 1; |
} |
} |
|
p->cMode = p->mode; |
}else |
}else |
|
|
if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==3 ){ | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){ |
int n2 = strlen30(azArg[1]); | if( nArg==2 ){ |
if( n2==6 && strncmp(azArg[1],"insert",n2)==0 ){ | sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, |
p->mode = MODE_Insert; | "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); |
set_table_name(p, azArg[2]); | }else{ |
}else { | raw_printf(stderr, "Usage: .nullvalue STRING\n"); |
fprintf(stderr, "Error: invalid arguments: " | |
" \"%s\". Enter \".help\" for help\n", azArg[2]); | |
rc = 1; |
rc = 1; |
} |
} |
}else |
}else |
|
|
if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) { | if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){ |
sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue, | sqlite3 *savedDb = p->db; |
"%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]); | const char *zSavedFilename = p->zDbFilename; |
| char *zNewFilename = 0; |
| p->db = 0; |
| if( nArg>=2 ) zNewFilename = sqlite3_mprintf("%s", azArg[1]); |
| p->zDbFilename = zNewFilename; |
| open_db(p, 1); |
| if( p->db!=0 ){ |
| session_close_all(p); |
| sqlite3_close(savedDb); |
| sqlite3_free(p->zFreeOnClose); |
| p->zFreeOnClose = zNewFilename; |
| }else{ |
| sqlite3_free(zNewFilename); |
| p->db = savedDb; |
| p->zDbFilename = zSavedFilename; |
| } |
}else |
}else |
|
|
if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){ | if( c=='o' |
if( p->outfile[0]=='|' ){ | && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0) |
pclose(p->out); | ){ |
| const char *zFile = nArg>=2 ? azArg[1] : "stdout"; |
| if( nArg>2 ){ |
| utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]); |
| rc = 1; |
| goto meta_command_exit; |
| } |
| if( n>1 && strncmp(azArg[0], "once", n)==0 ){ |
| if( nArg<2 ){ |
| raw_printf(stderr, "Usage: .once FILE\n"); |
| rc = 1; |
| goto meta_command_exit; |
| } |
| p->outCount = 2; |
}else{ |
}else{ |
output_file_close(p->out); | p->outCount = 0; |
} |
} |
p->outfile[0] = 0; | output_reset(p); |
if( azArg[1][0]=='|' ){ | if( zFile[0]=='|' ){ |
p->out = popen(&azArg[1][1], "w"); | #ifdef SQLITE_OMIT_POPEN |
| raw_printf(stderr, "Error: pipes are not supported in this OS\n"); |
| rc = 1; |
| p->out = stdout; |
| #else |
| p->out = popen(zFile + 1, "w"); |
if( p->out==0 ){ |
if( p->out==0 ){ |
fprintf(stderr,"Error: cannot open pipe \"%s\"\n", &azArg[1][1]); | utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); |
p->out = stdout; |
p->out = stdout; |
rc = 1; |
rc = 1; |
}else{ |
}else{ |
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]); | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
} |
} |
|
#endif |
}else{ |
}else{ |
p->out = output_file_open(azArg[1]); | p->out = output_file_open(zFile); |
if( p->out==0 ){ |
if( p->out==0 ){ |
if( strcmp(azArg[1],"off")!=0 ){ | if( strcmp(zFile,"off")!=0 ){ |
fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]); | utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile); |
} |
} |
p->out = stdout; |
p->out = stdout; |
rc = 1; |
rc = 1; |
} else { |
} else { |
sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]); | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); |
} |
} |
} |
} |
}else |
}else |
Line 2088 static int do_meta_command(char *zLine, struct callbac
|
Line 3904 static int do_meta_command(char *zLine, struct callbac
|
if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){ |
int i; |
int i; |
for(i=1; i<nArg; i++){ |
for(i=1; i<nArg; i++){ |
if( i>1 ) fprintf(p->out, " "); | if( i>1 ) raw_printf(p->out, " "); |
fprintf(p->out, "%s", azArg[i]); | utf8_printf(p->out, "%s", azArg[i]); |
} |
} |
fprintf(p->out, "\n"); | raw_printf(p->out, "\n"); |
}else |
}else |
|
|
if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){ | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){ |
if( nArg >= 2) { |
if( nArg >= 2) { |
strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
} |
} |
Line 2103 static int do_meta_command(char *zLine, struct callbac
|
Line 3919 static int do_meta_command(char *zLine, struct callbac
|
} |
} |
}else |
}else |
|
|
if( c=='q' && strncmp(azArg[0], "quit", n)==0 && nArg==1 ){ | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
rc = 2; |
rc = 2; |
}else |
}else |
|
|
if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 && nArg==2 ){ | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){ |
FILE *alt = fopen(azArg[1], "rb"); | FILE *alt; |
| if( nArg!=2 ){ |
| raw_printf(stderr, "Usage: .read FILE\n"); |
| rc = 1; |
| goto meta_command_exit; |
| } |
| alt = fopen(azArg[1], "rb"); |
if( alt==0 ){ |
if( alt==0 ){ |
fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); | utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); |
rc = 1; |
rc = 1; |
}else{ |
}else{ |
rc = process_input(p, alt); |
rc = process_input(p, alt); |
Line 2118 static int do_meta_command(char *zLine, struct callbac
|
Line 3940 static int do_meta_command(char *zLine, struct callbac
|
} |
} |
}else |
}else |
|
|
if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 && nArg>1 && nArg<4){ | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){ |
const char *zSrcFile; |
const char *zSrcFile; |
const char *zDb; |
const char *zDb; |
sqlite3 *pSrc; |
sqlite3 *pSrc; |
Line 2128 static int do_meta_command(char *zLine, struct callbac
|
Line 3950 static int do_meta_command(char *zLine, struct callbac
|
if( nArg==2 ){ |
if( nArg==2 ){ |
zSrcFile = azArg[1]; |
zSrcFile = azArg[1]; |
zDb = "main"; |
zDb = "main"; |
}else{ | }else if( nArg==3 ){ |
zSrcFile = azArg[2]; |
zSrcFile = azArg[2]; |
zDb = azArg[1]; |
zDb = azArg[1]; |
|
}else{ |
|
raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); |
|
rc = 1; |
|
goto meta_command_exit; |
} |
} |
rc = sqlite3_open(zSrcFile, &pSrc); |
rc = sqlite3_open(zSrcFile, &pSrc); |
if( rc!=SQLITE_OK ){ |
if( rc!=SQLITE_OK ){ |
fprintf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); | utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); |
sqlite3_close(pSrc); |
sqlite3_close(pSrc); |
return 1; |
return 1; |
} |
} |
open_db(p); | open_db(p, 0); |
pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
if( pBackup==0 ){ |
if( pBackup==0 ){ |
fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
sqlite3_close(pSrc); |
sqlite3_close(pSrc); |
return 1; |
return 1; |
} |
} |
Line 2156 static int do_meta_command(char *zLine, struct callbac
|
Line 3982 static int do_meta_command(char *zLine, struct callbac
|
if( rc==SQLITE_DONE ){ |
if( rc==SQLITE_DONE ){ |
rc = 0; |
rc = 0; |
}else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
}else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
fprintf(stderr, "Error: source database is busy\n"); | raw_printf(stderr, "Error: source database is busy\n"); |
rc = 1; |
rc = 1; |
}else{ |
}else{ |
fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); | utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
rc = 1; |
rc = 1; |
} |
} |
sqlite3_close(pSrc); |
sqlite3_close(pSrc); |
}else |
}else |
|
|
if( c=='s' && strncmp(azArg[0], "schema", n)==0 && nArg<3 ){ | |
struct callback_data data; | if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){ |
| if( nArg==2 ){ |
| p->scanstatsOn = booleanValue(azArg[1]); |
| #ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| raw_printf(stderr, "Warning: .scanstats not available in this build.\n"); |
| #endif |
| }else{ |
| raw_printf(stderr, "Usage: .scanstats on|off\n"); |
| rc = 1; |
| } |
| }else |
| |
| if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
| ShellState data; |
char *zErrMsg = 0; |
char *zErrMsg = 0; |
open_db(p); | open_db(p, 0); |
memcpy(&data, p, sizeof(data)); |
memcpy(&data, p, sizeof(data)); |
data.showHeader = 0; |
data.showHeader = 0; |
data.mode = MODE_Semi; | data.cMode = data.mode = MODE_Semi; |
if( nArg>1 ){ | if( nArg>=2 && optionMatch(azArg[1], "indent") ){ |
| data.cMode = data.mode = MODE_Pretty; |
| nArg--; |
| if( nArg==2 ) azArg[1] = azArg[2]; |
| } |
| if( nArg==2 && azArg[1][0]!='-' ){ |
int i; |
int i; |
for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]); |
for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]); |
if( strcmp(azArg[1],"sqlite_master")==0 ){ |
if( strcmp(azArg[1],"sqlite_master")==0 ){ |
Line 2212 static int do_meta_command(char *zLine, struct callbac
|
Line 4056 static int do_meta_command(char *zLine, struct callbac
|
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
"WHERE lower(tbl_name) LIKE shellstatic()" |
"WHERE lower(tbl_name) LIKE shellstatic()" |
" AND type!='meta' AND sql NOTNULL " |
" AND type!='meta' AND sql NOTNULL " |
"ORDER BY substr(type,2,1), " | "ORDER BY rowid", |
" CASE type WHEN 'view' THEN rowid ELSE name END", | |
callback, &data, &zErrMsg); |
callback, &data, &zErrMsg); |
zShellStatic = 0; |
zShellStatic = 0; |
} |
} |
}else{ | }else if( nArg==1 ){ |
rc = sqlite3_exec(p->db, |
rc = sqlite3_exec(p->db, |
"SELECT sql FROM " |
"SELECT sql FROM " |
" (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
" (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" |
" FROM sqlite_master UNION ALL" |
" FROM sqlite_master UNION ALL" |
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " |
"WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'" | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " |
"ORDER BY substr(type,2,1)," | "ORDER BY rowid", |
" CASE type WHEN 'view' THEN rowid ELSE name END", | |
callback, &data, &zErrMsg |
callback, &data, &zErrMsg |
); |
); |
|
}else{ |
|
raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n"); |
|
rc = 1; |
|
goto meta_command_exit; |
} |
} |
if( zErrMsg ){ |
if( zErrMsg ){ |
fprintf(stderr,"Error: %s\n", zErrMsg); | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
sqlite3_free(zErrMsg); |
sqlite3_free(zErrMsg); |
rc = 1; |
rc = 1; |
}else if( rc != SQLITE_OK ){ |
}else if( rc != SQLITE_OK ){ |
fprintf(stderr,"Error: querying schema information\n"); | raw_printf(stderr,"Error: querying schema information\n"); |
rc = 1; |
rc = 1; |
}else{ |
}else{ |
rc = 0; |
rc = 0; |
} |
} |
}else |
}else |
|
|
if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){ | #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) |
sqlite3_snprintf(sizeof(p->separator), p->separator, | if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){ |
"%.*s", (int)sizeof(p->separator)-1, azArg[1]); | sqlite3SelectTrace = integerValue(azArg[1]); |
}else |
}else |
|
#endif |
|
|
if( c=='s' && strncmp(azArg[0], "show", n)==0 && nArg==1 ){ | #if defined(SQLITE_ENABLE_SESSION) |
| if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){ |
| OpenSession *pSession = &p->aSession[0]; |
| char **azCmd = &azArg[1]; |
| int iSes = 0; |
| int nCmd = nArg - 1; |
int i; |
int i; |
fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off"); | if( nArg<=1 ) goto session_syntax_error; |
fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off"); | open_db(p, 0); |
fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off"); | if( nArg>=3 ){ |
fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]); | for(iSes=0; iSes<p->nSession; iSes++){ |
fprintf(p->out,"%9.9s: ", "nullvalue"); | if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break; |
output_c_string(p->out, p->nullvalue); | } |
fprintf(p->out, "\n"); | if( iSes<p->nSession ){ |
fprintf(p->out,"%9.9s: %s\n","output", | pSession = &p->aSession[iSes]; |
| azCmd++; |
| nCmd--; |
| }else{ |
| pSession = &p->aSession[0]; |
| iSes = 0; |
| } |
| } |
| |
| /* .session attach TABLE |
| ** Invoke the sqlite3session_attach() interface to attach a particular |
| ** table so that it is never filtered. |
| */ |
| if( strcmp(azCmd[0],"attach")==0 ){ |
| if( nCmd!=2 ) goto session_syntax_error; |
| if( pSession->p==0 ){ |
| session_not_open: |
| raw_printf(stderr, "ERROR: No sessions are open\n"); |
| }else{ |
| rc = sqlite3session_attach(pSession->p, azCmd[1]); |
| if( rc ){ |
| raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc); |
| rc = 0; |
| } |
| } |
| }else |
| |
| /* .session changeset FILE |
| ** .session patchset FILE |
| ** Write a changeset or patchset into a file. The file is overwritten. |
| */ |
| if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){ |
| FILE *out = 0; |
| if( nCmd!=2 ) goto session_syntax_error; |
| if( pSession->p==0 ) goto session_not_open; |
| out = fopen(azCmd[1], "wb"); |
| if( out==0 ){ |
| utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]); |
| }else{ |
| int szChng; |
| void *pChng; |
| if( azCmd[0][0]=='c' ){ |
| rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); |
| }else{ |
| rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); |
| } |
| if( rc ){ |
| printf("Error: error code %d\n", rc); |
| rc = 0; |
| } |
| if( pChng |
| && fwrite(pChng, szChng, 1, out)!=1 ){ |
| raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", |
| szChng); |
| } |
| sqlite3_free(pChng); |
| fclose(out); |
| } |
| }else |
| |
| /* .session close |
| ** Close the identified session |
| */ |
| if( strcmp(azCmd[0], "close")==0 ){ |
| if( nCmd!=1 ) goto session_syntax_error; |
| if( p->nSession ){ |
| session_close(pSession); |
| p->aSession[iSes] = p->aSession[--p->nSession]; |
| } |
| }else |
| |
| /* .session enable ?BOOLEAN? |
| ** Query or set the enable flag |
| */ |
| if( strcmp(azCmd[0], "enable")==0 ){ |
| int ii; |
| if( nCmd>2 ) goto session_syntax_error; |
| ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| if( p->nSession ){ |
| ii = sqlite3session_enable(pSession->p, ii); |
| utf8_printf(p->out, "session %s enable flag = %d\n", |
| pSession->zName, ii); |
| } |
| }else |
| |
| /* .session filter GLOB .... |
| ** Set a list of GLOB patterns of table names to be excluded. |
| */ |
| if( strcmp(azCmd[0], "filter")==0 ){ |
| int ii, nByte; |
| if( nCmd<2 ) goto session_syntax_error; |
| if( p->nSession ){ |
| for(ii=0; ii<pSession->nFilter; ii++){ |
| sqlite3_free(pSession->azFilter[ii]); |
| } |
| sqlite3_free(pSession->azFilter); |
| nByte = sizeof(pSession->azFilter[0])*(nCmd-1); |
| pSession->azFilter = sqlite3_malloc( nByte ); |
| if( pSession->azFilter==0 ){ |
| raw_printf(stderr, "Error: out or memory\n"); |
| exit(1); |
| } |
| for(ii=1; ii<nCmd; ii++){ |
| pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); |
| } |
| pSession->nFilter = ii-1; |
| } |
| }else |
| |
| /* .session indirect ?BOOLEAN? |
| ** Query or set the indirect flag |
| */ |
| if( strcmp(azCmd[0], "indirect")==0 ){ |
| int ii; |
| if( nCmd>2 ) goto session_syntax_error; |
| ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); |
| if( p->nSession ){ |
| ii = sqlite3session_indirect(pSession->p, ii); |
| utf8_printf(p->out, "session %s indirect flag = %d\n", |
| pSession->zName, ii); |
| } |
| }else |
| |
| /* .session isempty |
| ** Determine if the session is empty |
| */ |
| if( strcmp(azCmd[0], "isempty")==0 ){ |
| int ii; |
| if( nCmd!=1 ) goto session_syntax_error; |
| if( p->nSession ){ |
| ii = sqlite3session_isempty(pSession->p); |
| utf8_printf(p->out, "session %s isempty flag = %d\n", |
| pSession->zName, ii); |
| } |
| }else |
| |
| /* .session list |
| ** List all currently open sessions |
| */ |
| if( strcmp(azCmd[0],"list")==0 ){ |
| for(i=0; i<p->nSession; i++){ |
| utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName); |
| } |
| }else |
| |
| /* .session open DB NAME |
| ** Open a new session called NAME on the attached database DB. |
| ** DB is normally "main". |
| */ |
| if( strcmp(azCmd[0],"open")==0 ){ |
| char *zName; |
| if( nCmd!=3 ) goto session_syntax_error; |
| zName = azCmd[2]; |
| if( zName[0]==0 ) goto session_syntax_error; |
| for(i=0; i<p->nSession; i++){ |
| if( strcmp(p->aSession[i].zName,zName)==0 ){ |
| utf8_printf(stderr, "Session \"%s\" already exists\n", zName); |
| goto meta_command_exit; |
| } |
| } |
| if( p->nSession>=ArraySize(p->aSession) ){ |
| raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession)); |
| goto meta_command_exit; |
| } |
| pSession = &p->aSession[p->nSession]; |
| rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); |
| if( rc ){ |
| raw_printf(stderr, "Cannot open session: error code=%d\n", rc); |
| rc = 0; |
| goto meta_command_exit; |
| } |
| pSession->nFilter = 0; |
| sqlite3session_table_filter(pSession->p, session_filter, pSession); |
| p->nSession++; |
| pSession->zName = sqlite3_mprintf("%s", zName); |
| }else |
| /* If no command name matches, show a syntax error */ |
| session_syntax_error: |
| session_help(p); |
| }else |
| #endif |
| |
| #ifdef SQLITE_DEBUG |
| /* Undocumented commands for internal testing. Subject to change |
| ** without notice. */ |
| if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ |
| if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ |
| int i, v; |
| for(i=1; i<nArg; i++){ |
| v = booleanValue(azArg[i]); |
| utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); |
| } |
| } |
| if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ |
| int i; sqlite3_int64 v; |
| for(i=1; i<nArg; i++){ |
| char zBuf[200]; |
| v = integerValue(azArg[i]); |
| sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); |
| utf8_printf(p->out, "%s", zBuf); |
| } |
| } |
| }else |
| #endif |
| |
| if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){ |
| if( nArg<2 || nArg>3 ){ |
| raw_printf(stderr, "Usage: .separator COL ?ROW?\n"); |
| rc = 1; |
| } |
| if( nArg>=2 ){ |
| sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, |
| "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); |
| } |
| if( nArg>=3 ){ |
| sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, |
| "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); |
| } |
| }else |
| |
| if( c=='s' |
| && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0) |
| ){ |
| char *zCmd; |
| int i, x; |
| if( nArg<2 ){ |
| raw_printf(stderr, "Usage: .system COMMAND\n"); |
| rc = 1; |
| goto meta_command_exit; |
| } |
| zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); |
| for(i=2; i<nArg; i++){ |
| zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", |
| zCmd, azArg[i]); |
| } |
| x = system(zCmd); |
| sqlite3_free(zCmd); |
| if( x ) raw_printf(stderr, "System command returns %d\n", x); |
| }else |
| |
| if( c=='s' && strncmp(azArg[0], "show", n)==0 ){ |
| static const char *azBool[] = { "off", "on", "full", "unk" }; |
| int i; |
| if( nArg!=1 ){ |
| raw_printf(stderr, "Usage: .show\n"); |
| rc = 1; |
| goto meta_command_exit; |
| } |
| utf8_printf(p->out, "%12.12s: %s\n","echo", azBool[p->echoOn!=0]); |
| utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); |
| utf8_printf(p->out, "%12.12s: %s\n","explain", |
| p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); |
| utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); |
| utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); |
| utf8_printf(p->out, "%12.12s: ", "nullvalue"); |
| output_c_string(p->out, p->nullValue); |
| raw_printf(p->out, "\n"); |
| utf8_printf(p->out,"%12.12s: %s\n","output", |
strlen30(p->outfile) ? p->outfile : "stdout"); |
strlen30(p->outfile) ? p->outfile : "stdout"); |
fprintf(p->out,"%9.9s: ", "separator"); | utf8_printf(p->out,"%12.12s: ", "colseparator"); |
output_c_string(p->out, p->separator); | output_c_string(p->out, p->colSeparator); |
fprintf(p->out, "\n"); | raw_printf(p->out, "\n"); |
fprintf(p->out,"%9.9s: %s\n","stats", p->statsOn ? "on" : "off"); | utf8_printf(p->out,"%12.12s: ", "rowseparator"); |
fprintf(p->out,"%9.9s: ","width"); | output_c_string(p->out, p->rowSeparator); |
| raw_printf(p->out, "\n"); |
| utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]); |
| utf8_printf(p->out, "%12.12s: ", "width"); |
for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
fprintf(p->out,"%d ",p->colWidth[i]); | raw_printf(p->out, "%d ", p->colWidth[i]); |
} |
} |
fprintf(p->out,"\n"); | raw_printf(p->out, "\n"); |
}else |
}else |
|
|
if( c=='s' && strncmp(azArg[0], "stats", n)==0 && nArg>1 && nArg<3 ){ | if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){ |
p->statsOn = booleanValue(azArg[1]); | if( nArg==2 ){ |
| p->statsOn = booleanValue(azArg[1]); |
| }else if( nArg==1 ){ |
| display_stats(p->db, p, 0); |
| }else{ |
| raw_printf(stderr, "Usage: .stats ?on|off?\n"); |
| rc = 1; |
| } |
}else |
}else |
|
|
if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 && nArg<3 ){ | if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){ |
sqlite3_stmt *pStmt; |
sqlite3_stmt *pStmt; |
char **azResult; |
char **azResult; |
int nRow, nAlloc; |
int nRow, nAlloc; |
char *zSql = 0; |
char *zSql = 0; |
int ii; |
int ii; |
open_db(p); | open_db(p, 0); |
rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); |
if( rc ) return rc; | if( rc ) return shellDatabaseError(p->db); |
| |
| /* Create an SQL statement to query for the list of tables in the |
| ** main and all attached databases where the table name matches the |
| ** LIKE pattern bound to variable "?1". */ |
zSql = sqlite3_mprintf( |
zSql = sqlite3_mprintf( |
"SELECT name FROM sqlite_master" |
"SELECT name FROM sqlite_master" |
" WHERE type IN ('table','view')" |
" WHERE type IN ('table','view')" |
" AND name NOT LIKE 'sqlite_%%'" |
" AND name NOT LIKE 'sqlite_%%'" |
" AND name LIKE ?1"); |
" AND name LIKE ?1"); |
while( sqlite3_step(pStmt)==SQLITE_ROW ){ | while( zSql && sqlite3_step(pStmt)==SQLITE_ROW ){ |
const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); |
if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue; |
if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue; |
if( strcmp(zDbName,"temp")==0 ){ |
if( strcmp(zDbName,"temp")==0 ){ |
Line 2305 static int do_meta_command(char *zLine, struct callbac
|
Line 4428 static int do_meta_command(char *zLine, struct callbac
|
" AND name LIKE ?1", zSql, zDbName, zDbName); |
" AND name LIKE ?1", zSql, zDbName, zDbName); |
} |
} |
} |
} |
sqlite3_finalize(pStmt); | rc = sqlite3_finalize(pStmt); |
zSql = sqlite3_mprintf("%z ORDER BY 1", zSql); | if( zSql && rc==SQLITE_OK ){ |
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); | zSql = sqlite3_mprintf("%z ORDER BY 1", zSql); |
| if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); |
| } |
sqlite3_free(zSql); |
sqlite3_free(zSql); |
if( rc ) return rc; | if( !zSql ) return shellNomemError(); |
| if( rc ) return shellDatabaseError(p->db); |
| |
| /* Run the SQL statement prepared by the above block. Store the results |
| ** as an array of nul-terminated strings in azResult[]. */ |
nRow = nAlloc = 0; |
nRow = nAlloc = 0; |
azResult = 0; |
azResult = 0; |
if( nArg>1 ){ |
if( nArg>1 ){ |
Line 2320 static int do_meta_command(char *zLine, struct callbac
|
Line 4449 static int do_meta_command(char *zLine, struct callbac
|
while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
if( nRow>=nAlloc ){ |
if( nRow>=nAlloc ){ |
char **azNew; |
char **azNew; |
int n = nAlloc*2 + 10; | int n2 = nAlloc*2 + 10; |
azNew = sqlite3_realloc(azResult, sizeof(azResult[0])*n); | azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); |
if( azNew==0 ){ |
if( azNew==0 ){ |
fprintf(stderr, "Error: out of memory\n"); | rc = shellNomemError(); |
break; |
break; |
} |
} |
nAlloc = n; | nAlloc = n2; |
azResult = azNew; |
azResult = azNew; |
} |
} |
azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); |
if( azResult[nRow] ) nRow++; | if( 0==azResult[nRow] ){ |
| rc = shellNomemError(); |
| break; |
| } |
| nRow++; |
} |
} |
sqlite3_finalize(pStmt); | if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ |
if( nRow>0 ){ | rc = shellDatabaseError(p->db); |
| } |
| |
| /* Pretty-print the contents of array azResult[] to the output */ |
| if( rc==0 && nRow>0 ){ |
int len, maxlen = 0; |
int len, maxlen = 0; |
int i, j; |
int i, j; |
int nPrintCol, nPrintRow; |
int nPrintCol, nPrintRow; |
Line 2347 static int do_meta_command(char *zLine, struct callbac
|
Line 4484 static int do_meta_command(char *zLine, struct callbac
|
for(i=0; i<nPrintRow; i++){ |
for(i=0; i<nPrintRow; i++){ |
for(j=i; j<nRow; j+=nPrintRow){ |
for(j=i; j<nRow; j+=nPrintRow){ |
char *zSp = j<nPrintRow ? "" : " "; |
char *zSp = j<nPrintRow ? "" : " "; |
printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : ""); | utf8_printf(p->out, "%s%-*s", zSp, maxlen, |
| azResult[j] ? azResult[j]:""); |
} |
} |
printf("\n"); | raw_printf(p->out, "\n"); |
} |
} |
} |
} |
|
|
for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); |
sqlite3_free(azResult); |
sqlite3_free(azResult); |
}else |
}else |
Line 2374 static int do_meta_command(char *zLine, struct callbac
|
Line 4513 static int do_meta_command(char *zLine, struct callbac
|
{ "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, |
{ "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS }, |
{ "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, |
{ "iskeyword", SQLITE_TESTCTRL_ISKEYWORD }, |
{ "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, |
{ "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC }, |
|
{ "byteorder", SQLITE_TESTCTRL_BYTEORDER }, |
|
{ "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT }, |
|
{ "imposter", SQLITE_TESTCTRL_IMPOSTER }, |
}; |
}; |
int testctrl = -1; |
int testctrl = -1; |
int rc = 0; | int rc2 = 0; |
int i, n; | int i, n2; |
open_db(p); | open_db(p, 0); |
|
|
/* convert testctrl text option to value. allow any unique prefix |
/* convert testctrl text option to value. allow any unique prefix |
** of the option name, or a numerical value. */ |
** of the option name, or a numerical value. */ |
n = strlen30(azArg[1]); | n2 = strlen30(azArg[1]); |
for(i=0; i<(int)(sizeof(aCtrl)/sizeof(aCtrl[0])); i++){ | for(i=0; i<ArraySize(aCtrl); i++){ |
if( strncmp(azArg[1], aCtrl[i].zCtrlName, n)==0 ){ | if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){ |
if( testctrl<0 ){ |
if( testctrl<0 ){ |
testctrl = aCtrl[i].ctrlCode; |
testctrl = aCtrl[i].ctrlCode; |
}else{ |
}else{ |
fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]); | utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]); |
testctrl = -1; |
testctrl = -1; |
break; |
break; |
} |
} |
} |
} |
} |
} |
if( testctrl<0 ) testctrl = atoi(azArg[1]); | if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]); |
if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){ |
if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){ |
fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); | utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); |
}else{ |
}else{ |
switch(testctrl){ |
switch(testctrl){ |
|
|
/* sqlite3_test_control(int, db, int) */ |
/* sqlite3_test_control(int, db, int) */ |
case SQLITE_TESTCTRL_OPTIMIZATIONS: |
case SQLITE_TESTCTRL_OPTIMIZATIONS: |
case SQLITE_TESTCTRL_RESERVE: | case SQLITE_TESTCTRL_RESERVE: |
if( nArg==3 ){ |
if( nArg==3 ){ |
int opt = (int)strtol(azArg[2], 0, 0); | int opt = (int)strtol(azArg[2], 0, 0); |
rc = sqlite3_test_control(testctrl, p->db, opt); | rc2 = sqlite3_test_control(testctrl, p->db, opt); |
printf("%d (0x%08x)\n", rc, rc); | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
} else { |
} else { |
fprintf(stderr,"Error: testctrl %s takes a single int option\n", | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
azArg[1]); |
azArg[1]); |
} |
} |
break; |
break; |
|
|
/* sqlite3_test_control(int) */ |
/* sqlite3_test_control(int) */ |
case SQLITE_TESTCTRL_PRNG_SAVE: | case SQLITE_TESTCTRL_PRNG_SAVE: |
case SQLITE_TESTCTRL_PRNG_RESTORE: | case SQLITE_TESTCTRL_PRNG_RESTORE: |
case SQLITE_TESTCTRL_PRNG_RESET: |
case SQLITE_TESTCTRL_PRNG_RESET: |
|
case SQLITE_TESTCTRL_BYTEORDER: |
if( nArg==2 ){ |
if( nArg==2 ){ |
rc = sqlite3_test_control(testctrl); | rc2 = sqlite3_test_control(testctrl); |
printf("%d (0x%08x)\n", rc, rc); | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
} else { |
} else { |
fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]); | utf8_printf(stderr,"Error: testctrl %s takes no options\n", |
| azArg[1]); |
} |
} |
break; |
break; |
|
|
/* sqlite3_test_control(int, uint) */ |
/* sqlite3_test_control(int, uint) */ |
case SQLITE_TESTCTRL_PENDING_BYTE: | case SQLITE_TESTCTRL_PENDING_BYTE: |
if( nArg==3 ){ |
if( nArg==3 ){ |
unsigned int opt = (unsigned int)atoi(azArg[2]); | unsigned int opt = (unsigned int)integerValue(azArg[2]); |
rc = sqlite3_test_control(testctrl, opt); | rc2 = sqlite3_test_control(testctrl, opt); |
printf("%d (0x%08x)\n", rc, rc); | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
} else { |
} else { |
fprintf(stderr,"Error: testctrl %s takes a single unsigned" | utf8_printf(stderr,"Error: testctrl %s takes a single unsigned" |
" int option\n", azArg[1]); |
" int option\n", azArg[1]); |
} |
} |
break; |
break; |
| |
/* sqlite3_test_control(int, int) */ |
/* sqlite3_test_control(int, int) */ |
case SQLITE_TESTCTRL_ASSERT: | case SQLITE_TESTCTRL_ASSERT: |
case SQLITE_TESTCTRL_ALWAYS: | case SQLITE_TESTCTRL_ALWAYS: |
| case SQLITE_TESTCTRL_NEVER_CORRUPT: |
if( nArg==3 ){ |
if( nArg==3 ){ |
int opt = atoi(azArg[2]); | int opt = booleanValue(azArg[2]); |
rc = sqlite3_test_control(testctrl, opt); | rc2 = sqlite3_test_control(testctrl, opt); |
printf("%d (0x%08x)\n", rc, rc); | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
} else { |
} else { |
fprintf(stderr,"Error: testctrl %s takes a single int option\n", | utf8_printf(stderr,"Error: testctrl %s takes a single int option\n", |
azArg[1]); |
azArg[1]); |
} |
} |
break; |
break; |
|
|
/* sqlite3_test_control(int, char *) */ |
/* sqlite3_test_control(int, char *) */ |
#ifdef SQLITE_N_KEYWORD |
#ifdef SQLITE_N_KEYWORD |
case SQLITE_TESTCTRL_ISKEYWORD: | case SQLITE_TESTCTRL_ISKEYWORD: |
if( nArg==3 ){ |
if( nArg==3 ){ |
const char *opt = azArg[2]; | const char *opt = azArg[2]; |
rc = sqlite3_test_control(testctrl, opt); | rc2 = sqlite3_test_control(testctrl, opt); |
printf("%d (0x%08x)\n", rc, rc); | raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
} else { |
} else { |
fprintf(stderr,"Error: testctrl %s takes a single char * option\n", | utf8_printf(stderr, |
azArg[1]); | "Error: testctrl %s takes a single char * option\n", |
| azArg[1]); |
} |
} |
break; |
break; |
#endif |
#endif |
|
|
case SQLITE_TESTCTRL_BITVEC_TEST: | case SQLITE_TESTCTRL_IMPOSTER: |
case SQLITE_TESTCTRL_FAULT_INSTALL: | if( nArg==5 ){ |
case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: | rc2 = sqlite3_test_control(testctrl, p->db, |
case SQLITE_TESTCTRL_SCRATCHMALLOC: | azArg[2], |
| integerValue(azArg[3]), |
| integerValue(azArg[4])); |
| raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2); |
| }else{ |
| raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n"); |
| } |
| break; |
| |
| case SQLITE_TESTCTRL_BITVEC_TEST: |
| case SQLITE_TESTCTRL_FAULT_INSTALL: |
| case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: |
| case SQLITE_TESTCTRL_SCRATCHMALLOC: |
default: |
default: |
fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n", | utf8_printf(stderr, |
azArg[1]); | "Error: CLI support for testctrl %s not implemented\n", |
| azArg[1]); |
break; |
break; |
} |
} |
} |
} |
}else |
}else |
|
|
if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg==2 ){ | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){ |
open_db(p); | open_db(p, 0); |
sqlite3_busy_timeout(p->db, atoi(azArg[1])); | sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); |
}else |
}else |
| |
if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 | if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){ |
&& nArg==2 | if( nArg==2 ){ |
){ | enableTimer = booleanValue(azArg[1]); |
enableTimer = booleanValue(azArg[1]); | if( enableTimer && !HAS_TIMER ){ |
| raw_printf(stderr, "Error: timer not available on this system.\n"); |
| enableTimer = 0; |
| } |
| }else{ |
| raw_printf(stderr, "Usage: .timer on|off\n"); |
| rc = 1; |
| } |
}else |
}else |
| |
if( c=='t' && strncmp(azArg[0], "trace", n)==0 && nArg>1 ){ | if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){ |
open_db(p); | open_db(p, 0); |
| if( nArg!=2 ){ |
| raw_printf(stderr, "Usage: .trace FILE|off\n"); |
| rc = 1; |
| goto meta_command_exit; |
| } |
output_file_close(p->traceOut); |
output_file_close(p->traceOut); |
p->traceOut = output_file_open(azArg[1]); |
p->traceOut = output_file_open(azArg[1]); |
#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) |
if( p->traceOut==0 ){ |
if( p->traceOut==0 ){ |
sqlite3_trace(p->db, 0, 0); | sqlite3_trace_v2(p->db, 0, 0, 0); |
}else{ |
}else{ |
sqlite3_trace(p->db, sql_trace_callback, p->traceOut); | sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut); |
} |
} |
#endif |
#endif |
}else |
}else |
|
|
|
#if SQLITE_USER_AUTHENTICATION |
|
if( c=='u' && strncmp(azArg[0], "user", n)==0 ){ |
|
if( nArg<2 ){ |
|
raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n"); |
|
rc = 1; |
|
goto meta_command_exit; |
|
} |
|
open_db(p, 0); |
|
if( strcmp(azArg[1],"login")==0 ){ |
|
if( nArg!=4 ){ |
|
raw_printf(stderr, "Usage: .user login USER PASSWORD\n"); |
|
rc = 1; |
|
goto meta_command_exit; |
|
} |
|
rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], |
|
(int)strlen(azArg[3])); |
|
if( rc ){ |
|
utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]); |
|
rc = 1; |
|
} |
|
}else if( strcmp(azArg[1],"add")==0 ){ |
|
if( nArg!=5 ){ |
|
raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n"); |
|
rc = 1; |
|
goto meta_command_exit; |
|
} |
|
rc = sqlite3_user_add(p->db, azArg[2], |
|
azArg[3], (int)strlen(azArg[3]), |
|
booleanValue(azArg[4])); |
|
if( rc ){ |
|
raw_printf(stderr, "User-Add failed: %d\n", rc); |
|
rc = 1; |
|
} |
|
}else if( strcmp(azArg[1],"edit")==0 ){ |
|
if( nArg!=5 ){ |
|
raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n"); |
|
rc = 1; |
|
goto meta_command_exit; |
|
} |
|
rc = sqlite3_user_change(p->db, azArg[2], |
|
azArg[3], (int)strlen(azArg[3]), |
|
booleanValue(azArg[4])); |
|
if( rc ){ |
|
raw_printf(stderr, "User-Edit failed: %d\n", rc); |
|
rc = 1; |
|
} |
|
}else if( strcmp(azArg[1],"delete")==0 ){ |
|
if( nArg!=3 ){ |
|
raw_printf(stderr, "Usage: .user delete USER\n"); |
|
rc = 1; |
|
goto meta_command_exit; |
|
} |
|
rc = sqlite3_user_delete(p->db, azArg[2]); |
|
if( rc ){ |
|
raw_printf(stderr, "User-Delete failed: %d\n", rc); |
|
rc = 1; |
|
} |
|
}else{ |
|
raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n"); |
|
rc = 1; |
|
goto meta_command_exit; |
|
} |
|
}else |
|
#endif /* SQLITE_USER_AUTHENTICATION */ |
|
|
if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
if( c=='v' && strncmp(azArg[0], "version", n)==0 ){ |
printf("SQLite %s %s\n" /*extra-version-info*/, | utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, |
sqlite3_libversion(), sqlite3_sourceid()); |
sqlite3_libversion(), sqlite3_sourceid()); |
}else |
}else |
|
|
|
if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){ |
|
const char *zDbName = nArg==2 ? azArg[1] : "main"; |
|
sqlite3_vfs *pVfs; |
|
if( p->db ){ |
|
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); |
|
if( pVfs ){ |
|
utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); |
|
raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
|
raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
|
raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
|
} |
|
} |
|
}else |
|
|
|
if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){ |
|
sqlite3_vfs *pVfs; |
|
sqlite3_vfs *pCurrent = 0; |
|
if( p->db ){ |
|
sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); |
|
} |
|
for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ |
|
utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, |
|
pVfs==pCurrent ? " <--- CURRENT" : ""); |
|
raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); |
|
raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); |
|
raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); |
|
if( pVfs->pNext ){ |
|
raw_printf(p->out, "-----------------------------------\n"); |
|
} |
|
} |
|
}else |
|
|
if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){ |
const char *zDbName = nArg==2 ? azArg[1] : "main"; |
const char *zDbName = nArg==2 ? azArg[1] : "main"; |
char *zVfsName = 0; |
char *zVfsName = 0; |
if( p->db ){ |
if( p->db ){ |
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); |
if( zVfsName ){ |
if( zVfsName ){ |
printf("%s\n", zVfsName); | utf8_printf(p->out, "%s\n", zVfsName); |
sqlite3_free(zVfsName); |
sqlite3_free(zVfsName); |
} |
} |
} |
} |
Line 2519 static int do_meta_command(char *zLine, struct callbac
|
Line 4787 static int do_meta_command(char *zLine, struct callbac
|
|
|
#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) |
if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){ |
extern int sqlite3WhereTrace; | sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff; |
sqlite3WhereTrace = atoi(azArg[1]); | |
}else |
}else |
#endif |
#endif |
|
|
if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){ | if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
int j; |
int j; |
assert( nArg<=ArraySize(azArg) ); |
assert( nArg<=ArraySize(azArg) ); |
for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
p->colWidth[j-1] = atoi(azArg[j]); | p->colWidth[j-1] = (int)integerValue(azArg[j]); |
} |
} |
}else |
}else |
|
|
{ |
{ |
fprintf(stderr, "Error: unknown command or invalid arguments: " | utf8_printf(stderr, "Error: unknown command or invalid arguments: " |
" \"%s\". Enter \".help\" for help\n", azArg[0]); |
" \"%s\". Enter \".help\" for help\n", azArg[0]); |
rc = 1; |
rc = 1; |
} |
} |
|
|
|
meta_command_exit: |
|
if( p->outCount ){ |
|
p->outCount--; |
|
if( p->outCount==0 ) output_reset(p); |
|
} |
return rc; |
return rc; |
} |
} |
|
|
Line 2545 static int do_meta_command(char *zLine, struct callbac
|
Line 4817 static int do_meta_command(char *zLine, struct callbac
|
** Return TRUE if a semicolon occurs anywhere in the first N characters |
** Return TRUE if a semicolon occurs anywhere in the first N characters |
** of string z[]. |
** of string z[]. |
*/ |
*/ |
static int _contains_semicolon(const char *z, int N){ | static int line_contains_semicolon(const char *z, int N){ |
int i; |
int i; |
for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
return 0; |
return 0; |
Line 2580 static int _all_whitespace(const char *z){
|
Line 4852 static int _all_whitespace(const char *z){
|
** than a semi-colon. The SQL Server style "go" command is understood |
** than a semi-colon. The SQL Server style "go" command is understood |
** as is the Oracle "/". |
** as is the Oracle "/". |
*/ |
*/ |
static int _is_command_terminator(const char *zLine){ | static int line_is_command_terminator(const char *zLine){ |
while( IsSpace(zLine[0]) ){ zLine++; }; |
while( IsSpace(zLine[0]) ){ zLine++; }; |
if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
return 1; /* Oracle */ |
return 1; /* Oracle */ |
Line 2596 static int _is_command_terminator(const char *zLine){
|
Line 4868 static int _is_command_terminator(const char *zLine){
|
** Return true if zSql is a complete SQL statement. Return false if it |
** Return true if zSql is a complete SQL statement. Return false if it |
** ends in the middle of a string literal or C-style comment. |
** ends in the middle of a string literal or C-style comment. |
*/ |
*/ |
static int _is_complete(char *zSql, int nSql){ | static int line_is_complete(char *zSql, int nSql){ |
int rc; |
int rc; |
if( zSql==0 ) return 1; |
if( zSql==0 ) return 1; |
zSql[nSql] = ';'; |
zSql[nSql] = ';'; |
Line 2615 static int _is_complete(char *zSql, int nSql){
|
Line 4887 static int _is_complete(char *zSql, int nSql){
|
** |
** |
** Return the number of errors. |
** Return the number of errors. |
*/ |
*/ |
static int process_input(struct callback_data *p, FILE *in){ | static int process_input(ShellState *p, FILE *in){ |
char *zLine = 0; | char *zLine = 0; /* A single input line */ |
char *zSql = 0; | char *zSql = 0; /* Accumulated SQL text */ |
int nSql = 0; | int nLine; /* Length of current line */ |
int nSqlPrior = 0; | int nSql = 0; /* Bytes of zSql[] used */ |
char *zErrMsg; | int nAlloc = 0; /* Allocated zSql[] space */ |
int rc; | int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */ |
int errCnt = 0; | char *zErrMsg; /* Error message returned */ |
int lineno = 0; | int rc; /* Error code */ |
int startline = 0; | int errCnt = 0; /* Number of errors seen */ |
| int lineno = 0; /* Current line number */ |
| int startline = 0; /* Line number for start of current input */ |
|
|
while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
fflush(p->out); |
fflush(p->out); |
free(zLine); | zLine = one_input_line(in, zLine, nSql>0); |
zLine = one_input_line(zSql, in); | |
if( zLine==0 ){ |
if( zLine==0 ){ |
/* End of input */ |
/* End of input */ |
if( stdin_is_interactive ) printf("\n"); | if( in==0 && stdin_is_interactive ) printf("\n"); |
break; |
break; |
} |
} |
if( seenInterrupt ){ |
if( seenInterrupt ){ |
Line 2640 static int process_input(struct callback_data *p, FILE
|
Line 4913 static int process_input(struct callback_data *p, FILE
|
seenInterrupt = 0; |
seenInterrupt = 0; |
} |
} |
lineno++; |
lineno++; |
if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue; | if( nSql==0 && _all_whitespace(zLine) ){ |
| if( p->echoOn ) printf("%s\n", zLine); |
| continue; |
| } |
if( zLine && zLine[0]=='.' && nSql==0 ){ |
if( zLine && zLine[0]=='.' && nSql==0 ){ |
if( p->echoOn ) printf("%s\n", zLine); |
if( p->echoOn ) printf("%s\n", zLine); |
rc = do_meta_command(zLine, p); |
rc = do_meta_command(zLine, p); |
Line 2651 static int process_input(struct callback_data *p, FILE
|
Line 4927 static int process_input(struct callback_data *p, FILE
|
} |
} |
continue; |
continue; |
} |
} |
if( _is_command_terminator(zLine) && _is_complete(zSql, nSql) ){ | if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){ |
memcpy(zLine,";",2); |
memcpy(zLine,";",2); |
} |
} |
|
nLine = strlen30(zLine); |
|
if( nSql+nLine+2>=nAlloc ){ |
|
nAlloc = nSql+nLine+100; |
|
zSql = realloc(zSql, nAlloc); |
|
if( zSql==0 ){ |
|
raw_printf(stderr, "Error: out of memory\n"); |
|
exit(1); |
|
} |
|
} |
nSqlPrior = nSql; |
nSqlPrior = nSql; |
if( zSql==0 ){ | if( nSql==0 ){ |
int i; |
int i; |
for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} |
if( zLine[i]!=0 ){ | assert( nAlloc>0 && zSql!=0 ); |
nSql = strlen30(zLine); | memcpy(zSql, zLine+i, nLine+1-i); |
zSql = malloc( nSql+3 ); | startline = lineno; |
if( zSql==0 ){ | nSql = nLine-i; |
fprintf(stderr, "Error: out of memory\n"); | |
exit(1); | |
} | |
memcpy(zSql, zLine, nSql+1); | |
startline = lineno; | |
} | |
}else{ |
}else{ |
int len = strlen30(zLine); |
|
zSql = realloc( zSql, nSql + len + 4 ); |
|
if( zSql==0 ){ |
|
fprintf(stderr,"Error: out of memory\n"); |
|
exit(1); |
|
} |
|
zSql[nSql++] = '\n'; |
zSql[nSql++] = '\n'; |
memcpy(&zSql[nSql], zLine, len+1); | memcpy(zSql+nSql, zLine, nLine+1); |
nSql += len; | nSql += nLine; |
} |
} |
if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) | if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
&& sqlite3_complete(zSql) ){ |
&& sqlite3_complete(zSql) ){ |
p->cnt = 0; |
p->cnt = 0; |
open_db(p); | open_db(p, 0); |
| if( p->backslashOn ) resolve_backslashes(zSql); |
BEGIN_TIMER; |
BEGIN_TIMER; |
rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); |
END_TIMER; |
END_TIMER; |
if( rc || zErrMsg ){ |
if( rc || zErrMsg ){ |
char zPrefix[100]; |
char zPrefix[100]; |
if( in!=0 || !stdin_is_interactive ){ |
if( in!=0 || !stdin_is_interactive ){ |
sqlite3_snprintf(sizeof(zPrefix), zPrefix, | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
"Error: near line %d:", startline); |
"Error: near line %d:", startline); |
}else{ |
}else{ |
sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); |
} |
} |
if( zErrMsg!=0 ){ |
if( zErrMsg!=0 ){ |
fprintf(stderr, "%s %s\n", zPrefix, zErrMsg); | utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg); |
sqlite3_free(zErrMsg); |
sqlite3_free(zErrMsg); |
zErrMsg = 0; |
zErrMsg = 0; |
}else{ |
}else{ |
fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); | utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
} |
} |
errCnt++; |
errCnt++; |
|
}else if( p->countChanges ){ |
|
raw_printf(p->out, "changes: %3d total_changes: %d\n", |
|
sqlite3_changes(p->db), sqlite3_total_changes(p->db)); |
} |
} |
free(zSql); |
|
zSql = 0; |
|
nSql = 0; |
nSql = 0; |
|
if( p->outCount ){ |
|
output_reset(p); |
|
p->outCount = 0; |
|
} |
|
}else if( nSql && _all_whitespace(zSql) ){ |
|
if( p->echoOn ) printf("%s\n", zSql); |
|
nSql = 0; |
} |
} |
} |
} |
if( zSql ){ | if( nSql ){ |
if( !_all_whitespace(zSql) ){ |
if( !_all_whitespace(zSql) ){ |
fprintf(stderr, "Error: incomplete SQL: %s\n", zSql); | utf8_printf(stderr, "Error: incomplete SQL: %s\n", zSql); |
| errCnt++; |
} |
} |
free(zSql); |
|
} |
} |
|
free(zSql); |
free(zLine); |
free(zLine); |
return errCnt>0; |
return errCnt>0; |
} |
} |
Line 2726 static char *find_home_dir(void){
|
Line 5009 static char *find_home_dir(void){
|
static char *home_dir = NULL; |
static char *home_dir = NULL; |
if( home_dir ) return home_dir; |
if( home_dir ) return home_dir; |
|
|
#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL) | #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ |
| && !defined(__RTP__) && !defined(_WRS_KERNEL) |
{ |
{ |
struct passwd *pwent; |
struct passwd *pwent; |
uid_t uid = getuid(); |
uid_t uid = getuid(); |
Line 2787 static char *find_home_dir(void){
|
Line 5071 static char *find_home_dir(void){
|
** |
** |
** Returns the number of errors. |
** Returns the number of errors. |
*/ |
*/ |
static int process_sqliterc( | static void process_sqliterc( |
struct callback_data *p, /* Configuration data */ | ShellState *p, /* Configuration data */ |
const char *sqliterc_override /* Name of config file. NULL to use default */ |
const char *sqliterc_override /* Name of config file. NULL to use default */ |
){ |
){ |
char *home_dir = NULL; |
char *home_dir = NULL; |
const char *sqliterc = sqliterc_override; |
const char *sqliterc = sqliterc_override; |
char *zBuf = 0; |
char *zBuf = 0; |
FILE *in = NULL; |
FILE *in = NULL; |
int rc = 0; |
|
|
|
if (sqliterc == NULL) { |
if (sqliterc == NULL) { |
home_dir = find_home_dir(); |
home_dir = find_home_dir(); |
if( home_dir==0 ){ |
if( home_dir==0 ){ |
#if !defined(__RTP__) && !defined(_WRS_KERNEL) | raw_printf(stderr, "-- warning: cannot find home directory;" |
fprintf(stderr,"%s: Error: cannot locate your home directory\n", Argv0); | " cannot read ~/.sqliterc\n"); |
#endif | return; |
return 1; | |
} |
} |
sqlite3_initialize(); |
sqlite3_initialize(); |
zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); |
Line 2812 static int process_sqliterc(
|
Line 5094 static int process_sqliterc(
|
in = fopen(sqliterc,"rb"); |
in = fopen(sqliterc,"rb"); |
if( in ){ |
if( in ){ |
if( stdin_is_interactive ){ |
if( stdin_is_interactive ){ |
fprintf(stderr,"-- Loading resources from %s\n",sqliterc); | utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); |
} |
} |
rc = process_input(p,in); | process_input(p,in); |
fclose(in); |
fclose(in); |
} |
} |
sqlite3_free(zBuf); |
sqlite3_free(zBuf); |
return rc; |
|
} |
} |
|
|
/* |
/* |
** Show available command line options |
** Show available command line options |
*/ |
*/ |
static const char zOptions[] = | static const char zOptions[] = |
| " -ascii set output mode to 'ascii'\n" |
" -bail stop after hitting an error\n" |
" -bail stop after hitting an error\n" |
" -batch force batch I/O\n" |
" -batch force batch I/O\n" |
" -column set output mode to 'column'\n" |
" -column set output mode to 'column'\n" |
Line 2841 static const char zOptions[] =
|
Line 5123 static const char zOptions[] =
|
" -interactive force interactive I/O\n" |
" -interactive force interactive I/O\n" |
" -line set output mode to 'line'\n" |
" -line set output mode to 'line'\n" |
" -list set output mode to 'list'\n" |
" -list set output mode to 'list'\n" |
|
" -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" |
|
" -mmap N default mmap size set to N\n" |
#ifdef SQLITE_ENABLE_MULTIPLEX |
#ifdef SQLITE_ENABLE_MULTIPLEX |
" -multiplex enable the multiplexor VFS\n" |
" -multiplex enable the multiplexor VFS\n" |
#endif |
#endif |
|
" -newline SEP set output row separator. Default: '\\n'\n" |
" -nullvalue TEXT set text string for NULL values. Default ''\n" |
" -nullvalue TEXT set text string for NULL values. Default ''\n" |
" -separator SEP set output field separator. Default: '|'\n" | " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" |
| " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n" |
| " -separator SEP set output column separator. Default: '|'\n" |
" -stats print memory stats before each finalize\n" |
" -stats print memory stats before each finalize\n" |
" -version show SQLite version\n" |
" -version show SQLite version\n" |
" -vfs NAME use NAME as the default VFS\n" |
" -vfs NAME use NAME as the default VFS\n" |
Line 2854 static const char zOptions[] =
|
Line 5141 static const char zOptions[] =
|
#endif |
#endif |
; |
; |
static void usage(int showDetail){ |
static void usage(int showDetail){ |
fprintf(stderr, | utf8_printf(stderr, |
"Usage: %s [OPTIONS] FILENAME [SQL]\n" | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
"FILENAME is the name of an SQLite database. A new database is created\n" |
"FILENAME is the name of an SQLite database. A new database is created\n" |
"if the file does not previously exist.\n", Argv0); |
"if the file does not previously exist.\n", Argv0); |
if( showDetail ){ |
if( showDetail ){ |
fprintf(stderr, "OPTIONS include:\n%s", zOptions); | utf8_printf(stderr, "OPTIONS include:\n%s", zOptions); |
}else{ |
}else{ |
fprintf(stderr, "Use the -help option for additional information\n"); | raw_printf(stderr, "Use the -help option for additional information\n"); |
} |
} |
exit(1); |
exit(1); |
} |
} |
Line 2869 static void usage(int showDetail){
|
Line 5156 static void usage(int showDetail){
|
/* |
/* |
** Initialize the state information in data |
** Initialize the state information in data |
*/ |
*/ |
static void main_init(struct callback_data *data) { | static void main_init(ShellState *data) { |
memset(data, 0, sizeof(*data)); |
memset(data, 0, sizeof(*data)); |
data->mode = MODE_List; | data->normalMode = data->cMode = data->mode = MODE_List; |
memcpy(data->separator,"|", 2); | data->autoExplain = 1; |
| memcpy(data->colSeparator,SEP_Column, 2); |
| memcpy(data->rowSeparator,SEP_Row, 2); |
data->showHeader = 0; |
data->showHeader = 0; |
|
data->shellFlgs = SHFLG_Lookaside; |
sqlite3_config(SQLITE_CONFIG_URI, 1); |
sqlite3_config(SQLITE_CONFIG_URI, 1); |
sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); |
|
sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); |
|
} |
} |
|
|
/* |
/* |
|
** Output text to the console in a font that attracts extra attention. |
|
*/ |
|
#ifdef _WIN32 |
|
static void printBold(const char *zText){ |
|
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
|
CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; |
|
GetConsoleScreenBufferInfo(out, &defaultScreenInfo); |
|
SetConsoleTextAttribute(out, |
|
FOREGROUND_RED|FOREGROUND_INTENSITY |
|
); |
|
printf("%s", zText); |
|
SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); |
|
} |
|
#else |
|
static void printBold(const char *zText){ |
|
printf("\033[1m%s\033[0m", zText); |
|
} |
|
#endif |
|
|
|
/* |
** Get the argument to an --option. Throw an error and die if no argument |
** Get the argument to an --option. Throw an error and die if no argument |
** is available. |
** is available. |
*/ |
*/ |
static char *cmdline_option_value(int argc, char **argv, int i){ |
static char *cmdline_option_value(int argc, char **argv, int i){ |
if( i==argc ){ |
if( i==argc ){ |
fprintf(stderr, "%s: Error: missing argument to %s\n", | utf8_printf(stderr, "%s: Error: missing argument to %s\n", |
argv[0], argv[argc-1]); |
argv[0], argv[argc-1]); |
exit(1); |
exit(1); |
} |
} |
return argv[i]; |
return argv[i]; |
} |
} |
|
|
int main(int argc, char **argv){ | #ifndef SQLITE_SHELL_IS_UTF8 |
| # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) |
| # define SQLITE_SHELL_IS_UTF8 (0) |
| # else |
| # define SQLITE_SHELL_IS_UTF8 (1) |
| # endif |
| #endif |
| |
| #if SQLITE_SHELL_IS_UTF8 |
| int SQLITE_CDECL main(int argc, char **argv){ |
| #else |
| int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ |
| char **argv; |
| #endif |
char *zErrMsg = 0; |
char *zErrMsg = 0; |
struct callback_data data; | ShellState data; |
const char *zInitFile = 0; |
const char *zInitFile = 0; |
char *zFirstCmd = 0; |
|
int i; |
int i; |
int rc = 0; |
int rc = 0; |
|
int warnInmemoryDb = 0; |
|
int readStdin = 1; |
|
int nCmd = 0; |
|
char **azCmd = 0; |
|
|
|
setBinaryMode(stdin, 0); |
|
setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ |
|
stdin_is_interactive = isatty(0); |
|
stdout_is_console = isatty(1); |
|
|
|
#if USE_SYSTEM_SQLITE+0!=1 |
if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ |
if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){ |
fprintf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", | utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n", |
sqlite3_sourceid(), SQLITE_SOURCE_ID); |
sqlite3_sourceid(), SQLITE_SOURCE_ID); |
exit(1); |
exit(1); |
} |
} |
Argv0 = argv[0]; | #endif |
main_init(&data); |
main_init(&data); |
stdin_is_interactive = isatty(0); | #if !SQLITE_SHELL_IS_UTF8 |
| sqlite3_initialize(); |
| argv = sqlite3_malloc64(sizeof(argv[0])*argc); |
| if( argv==0 ){ |
| raw_printf(stderr, "out of memory\n"); |
| exit(1); |
| } |
| for(i=0; i<argc; i++){ |
| argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]); |
| if( argv[i]==0 ){ |
| raw_printf(stderr, "out of memory\n"); |
| exit(1); |
| } |
| } |
| #endif |
| assert( argc>=1 && argv && argv[0] ); |
| Argv0 = argv[0]; |
|
|
/* Make sure we have a valid signal handler early, before anything |
/* Make sure we have a valid signal handler early, before anything |
** else is done. |
** else is done. |
Line 2918 int main(int argc, char **argv){
|
Line 5266 int main(int argc, char **argv){
|
signal(SIGINT, interrupt_handler); |
signal(SIGINT, interrupt_handler); |
#endif |
#endif |
|
|
|
#ifdef SQLITE_SHELL_DBNAME_PROC |
|
{ |
|
/* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name |
|
** of a C-function that will provide the name of the database file. Use |
|
** this compile-time option to embed this shell program in larger |
|
** applications. */ |
|
extern void SQLITE_SHELL_DBNAME_PROC(const char**); |
|
SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename); |
|
warnInmemoryDb = 0; |
|
} |
|
#endif |
|
|
/* Do an initial pass through the command-line argument to locate |
/* Do an initial pass through the command-line argument to locate |
** the name of the database file, the name of the initialization file, |
** the name of the database file, the name of the initialization file, |
** the size of the alternative malloc heap, |
** the size of the alternative malloc heap, |
Line 2929 int main(int argc, char **argv){
|
Line 5289 int main(int argc, char **argv){
|
if( z[0]!='-' ){ |
if( z[0]!='-' ){ |
if( data.zDbFilename==0 ){ |
if( data.zDbFilename==0 ){ |
data.zDbFilename = z; |
data.zDbFilename = z; |
continue; | }else{ |
| /* Excesss arguments are interpreted as SQL (or dot-commands) and |
| ** mean that nothing is read from stdin */ |
| readStdin = 0; |
| nCmd++; |
| azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); |
| if( azCmd==0 ){ |
| raw_printf(stderr, "out of memory\n"); |
| exit(1); |
| } |
| azCmd[nCmd-1] = z; |
} |
} |
if( zFirstCmd==0 ){ |
|
zFirstCmd = z; |
|
continue; |
|
} |
|
fprintf(stderr,"%s: Error: too many options: \"%s\"\n", Argv0, argv[i]); |
|
fprintf(stderr,"Use -help for a list of options.\n"); |
|
return 1; |
|
} |
} |
if( z[1]=='-' ) z++; |
if( z[1]=='-' ) z++; |
if( strcmp(z,"-separator")==0 |
if( strcmp(z,"-separator")==0 |
|| strcmp(z,"-nullvalue")==0 |
|| strcmp(z,"-nullvalue")==0 |
|
|| strcmp(z,"-newline")==0 |
|| strcmp(z,"-cmd")==0 |
|| strcmp(z,"-cmd")==0 |
){ |
){ |
(void)cmdline_option_value(argc, argv, ++i); |
(void)cmdline_option_value(argc, argv, ++i); |
Line 2949 int main(int argc, char **argv){
|
Line 5313 int main(int argc, char **argv){
|
zInitFile = cmdline_option_value(argc, argv, ++i); |
zInitFile = cmdline_option_value(argc, argv, ++i); |
}else if( strcmp(z,"-batch")==0 ){ |
}else if( strcmp(z,"-batch")==0 ){ |
/* Need to check for batch mode here to so we can avoid printing |
/* Need to check for batch mode here to so we can avoid printing |
** informational messages (like from process_sqliterc) before | ** informational messages (like from process_sqliterc) before |
** we do the actual processing of arguments later in a second pass. |
** we do the actual processing of arguments later in a second pass. |
*/ |
*/ |
stdin_is_interactive = 0; |
stdin_is_interactive = 0; |
}else if( strcmp(z,"-heap")==0 ){ |
}else if( strcmp(z,"-heap")==0 ){ |
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) |
int j, c; |
|
const char *zSize; |
const char *zSize; |
sqlite3_int64 szHeap; |
sqlite3_int64 szHeap; |
|
|
zSize = cmdline_option_value(argc, argv, ++i); |
zSize = cmdline_option_value(argc, argv, ++i); |
szHeap = atoi(zSize); | szHeap = integerValue(zSize); |
for(j=0; (c = zSize[j])!=0; j++){ | |
if( c=='M' ){ szHeap *= 1000000; break; } | |
if( c=='K' ){ szHeap *= 1000; break; } | |
if( c=='G' ){ szHeap *= 1000000000; break; } | |
} | |
if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; |
sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); |
|
#else |
|
(void)cmdline_option_value(argc, argv, ++i); |
#endif |
#endif |
|
}else if( strcmp(z,"-scratch")==0 ){ |
|
int n, sz; |
|
sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
|
if( sz>400000 ) sz = 400000; |
|
if( sz<2500 ) sz = 2500; |
|
n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
|
if( n>10 ) n = 10; |
|
if( n<1 ) n = 1; |
|
sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n); |
|
data.shellFlgs |= SHFLG_Scratch; |
|
}else if( strcmp(z,"-pagecache")==0 ){ |
|
int n, sz; |
|
sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
|
if( sz>70000 ) sz = 70000; |
|
if( sz<0 ) sz = 0; |
|
n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
|
sqlite3_config(SQLITE_CONFIG_PAGECACHE, |
|
(n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); |
|
data.shellFlgs |= SHFLG_Pagecache; |
|
}else if( strcmp(z,"-lookaside")==0 ){ |
|
int n, sz; |
|
sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
|
if( sz<0 ) sz = 0; |
|
n = (int)integerValue(cmdline_option_value(argc,argv,++i)); |
|
if( n<0 ) n = 0; |
|
sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); |
|
if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; |
#ifdef SQLITE_ENABLE_VFSTRACE |
#ifdef SQLITE_ENABLE_VFSTRACE |
}else if( strcmp(z,"-vfstrace")==0 ){ |
}else if( strcmp(z,"-vfstrace")==0 ){ |
extern int vfstrace_register( |
extern int vfstrace_register( |
Line 2985 int main(int argc, char **argv){
|
Line 5372 int main(int argc, char **argv){
|
extern int sqlite3_multiple_initialize(const char*,int); |
extern int sqlite3_multiple_initialize(const char*,int); |
sqlite3_multiplex_initialize(0, 1); |
sqlite3_multiplex_initialize(0, 1); |
#endif |
#endif |
|
}else if( strcmp(z,"-mmap")==0 ){ |
|
sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); |
|
sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); |
}else if( strcmp(z,"-vfs")==0 ){ |
}else if( strcmp(z,"-vfs")==0 ){ |
sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i)); |
if( pVfs ){ |
if( pVfs ){ |
sqlite3_vfs_register(pVfs, 1); |
sqlite3_vfs_register(pVfs, 1); |
}else{ |
}else{ |
fprintf(stderr, "no such VFS: \"%s\"\n", argv[i]); | utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]); |
exit(1); |
exit(1); |
} |
} |
} |
} |
Line 2998 int main(int argc, char **argv){
|
Line 5388 int main(int argc, char **argv){
|
if( data.zDbFilename==0 ){ |
if( data.zDbFilename==0 ){ |
#ifndef SQLITE_OMIT_MEMORYDB |
#ifndef SQLITE_OMIT_MEMORYDB |
data.zDbFilename = ":memory:"; |
data.zDbFilename = ":memory:"; |
|
warnInmemoryDb = argc==1; |
#else |
#else |
fprintf(stderr,"%s: Error: no database filename specified\n", Argv0); | utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0); |
return 1; |
return 1; |
#endif |
#endif |
} |
} |
Line 3011 int main(int argc, char **argv){
|
Line 5402 int main(int argc, char **argv){
|
** to the sqlite command-line tool. |
** to the sqlite command-line tool. |
*/ |
*/ |
if( access(data.zDbFilename, 0)==0 ){ |
if( access(data.zDbFilename, 0)==0 ){ |
open_db(&data); | open_db(&data, 0); |
} |
} |
|
|
/* Process the initialization file if there is one. If no -init option |
/* Process the initialization file if there is one. If no -init option |
** is given on the command line, look for a file named ~/.sqliterc and |
** is given on the command line, look for a file named ~/.sqliterc and |
** try to process it. |
** try to process it. |
*/ |
*/ |
rc = process_sqliterc(&data,zInitFile); | process_sqliterc(&data,zInitFile); |
if( rc>0 ){ | |
return rc; | |
} | |
|
|
/* Make a second pass through the command-line argument and set |
/* Make a second pass through the command-line argument and set |
** options. This second pass is delayed until after the initialization |
** options. This second pass is delayed until after the initialization |
Line 3044 int main(int argc, char **argv){
|
Line 5432 int main(int argc, char **argv){
|
data.mode = MODE_Column; |
data.mode = MODE_Column; |
}else if( strcmp(z,"-csv")==0 ){ |
}else if( strcmp(z,"-csv")==0 ){ |
data.mode = MODE_Csv; |
data.mode = MODE_Csv; |
memcpy(data.separator,",",2); | memcpy(data.colSeparator,",",2); |
| }else if( strcmp(z,"-ascii")==0 ){ |
| data.mode = MODE_Ascii; |
| sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
| SEP_Unit); |
| sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
| SEP_Record); |
}else if( strcmp(z,"-separator")==0 ){ |
}else if( strcmp(z,"-separator")==0 ){ |
sqlite3_snprintf(sizeof(data.separator), data.separator, | sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, |
"%s",cmdline_option_value(argc,argv,++i)); |
"%s",cmdline_option_value(argc,argv,++i)); |
|
}else if( strcmp(z,"-newline")==0 ){ |
|
sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, |
|
"%s",cmdline_option_value(argc,argv,++i)); |
}else if( strcmp(z,"-nullvalue")==0 ){ |
}else if( strcmp(z,"-nullvalue")==0 ){ |
sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue, | sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, |
"%s",cmdline_option_value(argc,argv,++i)); |
"%s",cmdline_option_value(argc,argv,++i)); |
}else if( strcmp(z,"-header")==0 ){ |
}else if( strcmp(z,"-header")==0 ){ |
data.showHeader = 1; |
data.showHeader = 1; |
Line 3057 int main(int argc, char **argv){
|
Line 5454 int main(int argc, char **argv){
|
data.showHeader = 0; |
data.showHeader = 0; |
}else if( strcmp(z,"-echo")==0 ){ |
}else if( strcmp(z,"-echo")==0 ){ |
data.echoOn = 1; |
data.echoOn = 1; |
|
}else if( strcmp(z,"-eqp")==0 ){ |
|
data.autoEQP = 1; |
|
}else if( strcmp(z,"-eqpfull")==0 ){ |
|
data.autoEQP = 2; |
}else if( strcmp(z,"-stats")==0 ){ |
}else if( strcmp(z,"-stats")==0 ){ |
data.statsOn = 1; |
data.statsOn = 1; |
|
}else if( strcmp(z,"-scanstats")==0 ){ |
|
data.scanstatsOn = 1; |
|
}else if( strcmp(z,"-backslash")==0 ){ |
|
/* Undocumented command-line option: -backslash |
|
** Causes C-style backslash escapes to be evaluated in SQL statements |
|
** prior to sending the SQL into SQLite. Useful for injecting |
|
** crazy bytes in the middle of SQL statements for testing and debugging. |
|
*/ |
|
data.backslashOn = 1; |
}else if( strcmp(z,"-bail")==0 ){ |
}else if( strcmp(z,"-bail")==0 ){ |
bail_on_error = 1; |
bail_on_error = 1; |
}else if( strcmp(z,"-version")==0 ){ |
}else if( strcmp(z,"-version")==0 ){ |
Line 3070 int main(int argc, char **argv){
|
Line 5480 int main(int argc, char **argv){
|
stdin_is_interactive = 0; |
stdin_is_interactive = 0; |
}else if( strcmp(z,"-heap")==0 ){ |
}else if( strcmp(z,"-heap")==0 ){ |
i++; |
i++; |
|
}else if( strcmp(z,"-scratch")==0 ){ |
|
i+=2; |
|
}else if( strcmp(z,"-pagecache")==0 ){ |
|
i+=2; |
|
}else if( strcmp(z,"-lookaside")==0 ){ |
|
i+=2; |
|
}else if( strcmp(z,"-mmap")==0 ){ |
|
i++; |
}else if( strcmp(z,"-vfs")==0 ){ |
}else if( strcmp(z,"-vfs")==0 ){ |
i++; |
i++; |
#ifdef SQLITE_ENABLE_VFSTRACE |
#ifdef SQLITE_ENABLE_VFSTRACE |
Line 3083 int main(int argc, char **argv){
|
Line 5501 int main(int argc, char **argv){
|
}else if( strcmp(z,"-help")==0 ){ |
}else if( strcmp(z,"-help")==0 ){ |
usage(1); |
usage(1); |
}else if( strcmp(z,"-cmd")==0 ){ |
}else if( strcmp(z,"-cmd")==0 ){ |
|
/* Run commands that follow -cmd first and separately from commands |
|
** that simply appear on the command-line. This seems goofy. It would |
|
** be better if all commands ran in the order that they appear. But |
|
** we retain the goofy behavior for historical compatibility. */ |
if( i==argc-1 ) break; |
if( i==argc-1 ) break; |
z = cmdline_option_value(argc,argv,++i); |
z = cmdline_option_value(argc,argv,++i); |
if( z[0]=='.' ){ |
if( z[0]=='.' ){ |
rc = do_meta_command(z, &data); |
rc = do_meta_command(z, &data); |
if( rc && bail_on_error ) return rc; | if( rc && bail_on_error ) return rc==2 ? 0 : rc; |
}else{ |
}else{ |
open_db(&data); | open_db(&data, 0); |
rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg); |
rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg); |
if( zErrMsg!=0 ){ |
if( zErrMsg!=0 ){ |
fprintf(stderr,"Error: %s\n", zErrMsg); | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
if( bail_on_error ) return rc!=0 ? rc : 1; |
if( bail_on_error ) return rc!=0 ? rc : 1; |
}else if( rc!=0 ){ |
}else if( rc!=0 ){ |
fprintf(stderr,"Error: unable to process SQL \"%s\"\n", z); | utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z); |
if( bail_on_error ) return rc; |
if( bail_on_error ) return rc; |
} |
} |
} |
} |
}else{ |
}else{ |
fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); | utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); |
fprintf(stderr,"Use -help for a list of options.\n"); | raw_printf(stderr,"Use -help for a list of options.\n"); |
return 1; |
return 1; |
} |
} |
|
data.cMode = data.mode; |
} |
} |
|
|
if( zFirstCmd ){ | if( !readStdin ){ |
/* Run just the command that follows the database name | /* Run all arguments that do not begin with '-' as if they were separate |
| ** command-line inputs, except for the argToSkip argument which contains |
| ** the database filename. |
*/ |
*/ |
if( zFirstCmd[0]=='.' ){ | for(i=0; i<nCmd; i++){ |
rc = do_meta_command(zFirstCmd, &data); | if( azCmd[i][0]=='.' ){ |
}else{ | rc = do_meta_command(azCmd[i], &data); |
open_db(&data); | if( rc ) return rc==2 ? 0 : rc; |
rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg); | }else{ |
if( zErrMsg!=0 ){ | open_db(&data, 0); |
fprintf(stderr,"Error: %s\n", zErrMsg); | rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg); |
return rc!=0 ? rc : 1; | if( zErrMsg!=0 ){ |
}else if( rc!=0 ){ | utf8_printf(stderr,"Error: %s\n", zErrMsg); |
fprintf(stderr,"Error: unable to process SQL \"%s\"\n", zFirstCmd); | return rc!=0 ? rc : 1; |
return rc; | }else if( rc!=0 ){ |
| utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]); |
| return rc; |
| } |
} |
} |
} |
} |
|
free(azCmd); |
}else{ |
}else{ |
/* Run commands received from standard input |
/* Run commands received from standard input |
*/ |
*/ |
Line 3131 int main(int argc, char **argv){
|
Line 5560 int main(int argc, char **argv){
|
int nHistory; |
int nHistory; |
printf( |
printf( |
"SQLite version %s %.19s\n" /*extra-version-info*/ |
"SQLite version %s %.19s\n" /*extra-version-info*/ |
"Enter \".help\" for instructions\n" | "Enter \".help\" for usage hints.\n", |
"Enter SQL statements terminated with a \";\"\n", | |
sqlite3_libversion(), sqlite3_sourceid() |
sqlite3_libversion(), sqlite3_sourceid() |
); |
); |
|
if( warnInmemoryDb ){ |
|
printf("Connected to a "); |
|
printBold("transient in-memory database"); |
|
printf(".\nUse \".open FILENAME\" to reopen on a " |
|
"persistent database.\n"); |
|
} |
zHome = find_home_dir(); |
zHome = find_home_dir(); |
if( zHome ){ |
if( zHome ){ |
nHistory = strlen30(zHome) + 20; |
nHistory = strlen30(zHome) + 20; |
Line 3142 int main(int argc, char **argv){
|
Line 5576 int main(int argc, char **argv){
|
sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
} |
} |
} |
} |
#if defined(HAVE_READLINE) && HAVE_READLINE==1 | if( zHistory ){ shell_read_history(zHistory); } |
if( zHistory ) read_history(zHistory); | |
#endif | |
rc = process_input(&data, 0); |
rc = process_input(&data, 0); |
if( zHistory ){ |
if( zHistory ){ |
stifle_history(100); | shell_stifle_history(100); |
write_history(zHistory); | shell_write_history(zHistory); |
free(zHistory); |
free(zHistory); |
} |
} |
}else{ |
}else{ |
Line 3157 int main(int argc, char **argv){
|
Line 5589 int main(int argc, char **argv){
|
} |
} |
set_table_name(&data, 0); |
set_table_name(&data, 0); |
if( data.db ){ |
if( data.db ){ |
|
session_close_all(&data); |
sqlite3_close(data.db); |
sqlite3_close(data.db); |
} |
} |
|
sqlite3_free(data.zFreeOnClose); |
|
#if !SQLITE_SHELL_IS_UTF8 |
|
for(i=0; i<argc; i++) sqlite3_free(argv[i]); |
|
sqlite3_free(argv); |
|
#endif |
return rc; |
return rc; |
} |
} |