15 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS) 17 #define _CRT_SECURE_NO_WARNINGS 23 #if defined(INCLUDE_MSVC_H) 30 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION 31 # define SQLITE_OMIT_LOAD_EXTENSION 1 37 #ifndef SQLITE_DISABLE_LFS 38 # define _LARGE_FILE 1 39 # ifndef _FILE_OFFSET_BITS 40 # define _FILE_OFFSET_BITS 64 42 # define _LARGEFILE_SOURCE 1 50 #if SQLITE_USER_AUTHENTICATION 51 # include "sqlite3userauth.h" 56 #if !defined(_WIN32) && !defined(WIN32) 58 # if !defined(__RTP__) && !defined(_WRS_KERNEL) 62 # include <sys/types.h> 66 # include <readline/readline.h> 67 # include <readline/history.h> 71 # include <editline/readline.h> 74 #if HAVE_EDITLINE || HAVE_READLINE 76 # define shell_add_history(X) add_history(X) 77 # define shell_read_history(X) read_history(X) 78 # define shell_write_history(X) write_history(X) 79 # define shell_stifle_history(X) stifle_history(X) 80 # define shell_readline(X) readline(X) 84 # include "linenoise.h" 85 # define shell_add_history(X) linenoiseHistoryAdd(X) 86 # define shell_read_history(X) linenoiseHistoryLoad(X) 87 # define shell_write_history(X) linenoiseHistorySave(X) 88 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) 89 # define shell_readline(X) linenoise(X) 93 # define shell_read_history(X) 94 # define shell_write_history(X) 95 # define shell_stifle_history(X) 97 # define SHELL_USE_LOCAL_GETLINE 1 101 #if defined(_WIN32) || defined(WIN32) 104 # define isatty(h) _isatty(h) 106 # define access(f,m) _access((f),(m)) 109 # define popen _popen 111 # define pclose _pclose 116 # if !defined(__RTP__) && !defined(_WRS_KERNEL) 119 extern FILE *
popen(
const char*,
const char*);
122 # define SQLITE_OMIT_POPEN 1 126 #if defined(_WIN32_WCE) 135 #define IsSpace(X) isspace((unsigned char)X) 136 #define IsDigit(X) isdigit((unsigned char)X) 137 #define ToLower(X) (char)tolower((unsigned char)X) 139 #if defined(_WIN32) || defined(WIN32) 143 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
144 extern char *sqlite3_win32_mbcs_to_utf8_v2(
const char *,
int);
145 extern char *sqlite3_win32_utf8_to_mbcs_v2(
const char *,
int);
146 extern LPWSTR sqlite3_win32_utf8_to_unicode(
const char *zText);
155 #if defined(_WIN32) || defined(WIN32) 157 if( isOutput ) fflush(file);
158 _setmode(_fileno(file), _O_BINARY);
161 if( isOutput ) fflush(file);
162 _setmode(_fileno(file), _O_TEXT);
165 # define setBinaryMode(X,Y) 166 # define setTextMode(X,Y) 188 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) 189 #include <sys/time.h> 190 #include <sys/resource.h> 193 #if defined(_WRS_KERNEL) || defined(__RTP__) 195 struct timeval ru_utime;
196 struct timeval ru_stime;
198 #define getrusage(A,B) memset(B,0,sizeof(*B)) 210 getrusage(RUSAGE_SELF, &
sBegin);
216 static double timeDiff(
struct timeval *pStart,
struct timeval *pEnd){
217 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
218 (double)(pEnd->tv_sec - pStart->tv_sec);
228 getrusage(RUSAGE_SELF, &sEnd);
229 printf(
"Run Time: real %.3f user %f sys %f\n",
230 (iEnd - iBegin)*0.001,
236 #define BEGIN_TIMER beginTimer() 237 #define END_TIMER endTimer() 240 #elif (defined(_WIN32) || defined(WIN32)) 243 static HANDLE hProcess;
244 static FILETIME ftKernelBegin;
245 static FILETIME ftUserBegin;
247 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
248 LPFILETIME, LPFILETIME);
249 static GETPROCTIMES getProcessTimesAddr = NULL;
255 static int hasTimer(
void){
256 if( getProcessTimesAddr ){
263 hProcess = GetCurrentProcess();
265 HINSTANCE hinstLib = LoadLibrary(TEXT(
"Kernel32.dll"));
266 if( NULL != hinstLib ){
267 getProcessTimesAddr =
268 (GETPROCTIMES) GetProcAddress(hinstLib,
"GetProcessTimes");
269 if( NULL != getProcessTimesAddr ){
272 FreeLibrary(hinstLib);
283 if( enableTimer && getProcessTimesAddr ){
284 FILETIME ftCreation, ftExit;
285 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
286 &ftKernelBegin,&ftUserBegin);
292 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
295 return (
double) ((i64End - i64Start) / 10000000.0);
302 if( enableTimer && getProcessTimesAddr){
303 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
305 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
306 printf(
"Run Time: real %.3f user %f sys %f\n",
307 (ftWallEnd - ftWallBegin)*0.001,
309 timeDiff(&ftKernelBegin, &ftKernelEnd));
313 #define BEGIN_TIMER beginTimer() 314 #define END_TIMER endTimer() 315 #define HAS_TIMER hasTimer() 326 #define UNUSED_PARAMETER(x) (void)(x) 377 #if defined(_WIN32) || defined(WIN32) 378 void utf8_printf(FILE *out,
const char *zFormat, ...){
380 va_start(ap, zFormat);
381 if( stdout_is_console && (out==stdout || out==stderr) ){
383 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
388 vfprintf(out, zFormat, ap);
392 #elif !defined(utf8_printf) 393 # define utf8_printf fprintf 400 #if !defined(raw_printf) 401 # define raw_printf fprintf 407 #ifdef SQLITE_ENABLE_IOTRACE 408 static FILE *iotrace = 0;
417 #ifdef SQLITE_ENABLE_IOTRACE 418 static void SQLITE_CDECL iotracePrintf(
const char *zFormat, ...){
421 if( iotrace==0 )
return;
422 va_start(ap, zFormat);
435 if( *z==
'-' || *z==
'+' ) z++;
440 if( realnum ) *realnum = 0;
446 if( realnum ) *realnum = 1;
448 if( *z==
'e' || *z==
'E' ){
450 if( *z==
'+' || *z==
'-' ) z++;
453 if( realnum ) *realnum = 1;
473 assert( zShellStatic );
486 while( *z2 ){ z2++; }
487 return 0x3fffffff & (int)(z2 - z);
500 int nLine = zLine==0 ? 0 : 100;
505 nLine = nLine*2 + 100;
506 zLine = realloc(zLine, nLine);
507 if( zLine==0 )
return 0;
509 if( fgets(&zLine[n], nLine - n, in)==0 ){
517 while( zLine[n] ) n++;
518 if( n>0 && zLine[n-1]==
'\n' ){
520 if( n>0 && zLine[n-1]==
'\r' ) n--;
525 #if defined(_WIN32) || defined(WIN32) 528 if( stdin_is_interactive && in==stdin ){
529 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
533 zLine = realloc(zLine, nTrans);
539 memcpy(zLine, zTrans, nTrans);
567 zPrompt = isContinuation ? continuePrompt :
mainPrompt;
568 #if SHELL_USE_LOCAL_GETLINE 569 printf(
"%s", zPrompt);
574 zResult = shell_readline(zPrompt);
575 if( zResult && *zResult ) shell_add_history(zResult);
581 #if defined(SQLITE_ENABLE_SESSION) 585 typedef struct OpenSession OpenSession;
634 char colSeparator[20];
635 char rowSeparator[20];
637 int actualWidth[100];
640 char outfile[FILENAME_MAX];
649 #if defined(SQLITE_ENABLE_SESSION) 651 OpenSession aSession[4];
658 #define SHFLG_Scratch 0x00001 659 #define SHFLG_Pagecache 0x00002 660 #define SHFLG_Lookaside 0x00004 666 #define MODE_Column 1 670 #define MODE_Insert 5 673 #define MODE_Explain 8 675 #define MODE_Pretty 10 695 #define SEP_Column "|" 698 #define SEP_Space " " 699 #define SEP_Comma "," 700 #define SEP_CrLf "\r\n" 701 #define SEP_Unit "\x1F" 702 #define SEP_Record "\x1E" 707 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) 712 static void shellLog(
void *pArg,
int iErrCode,
const char *zMsg){
714 if( p->
pLog==0 )
return;
724 char *zBlob = (
char *)pBlob;
726 for(i=0; i<nBlob; i++){
raw_printf(out,
"%02x",zBlob[i]&0xff); }
738 if( z[i]==
'\'' ) nSingle++;
745 for(i=0; z[i] && z[i]!=
'\''; i++){}
749 }
else if( z[i]==
'\'' ){
768 while( (c = *(z++))!=0 ){
784 }
else if( !isprint(c&0xff) ){
813 }
else if( z[i]==
'&' ){
815 }
else if( z[i]==
'>' ){
817 }
else if( z[i]==
'\"' ){
819 }
else if( z[i]==
'\'' ){
833 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
834 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
835 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
836 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
837 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
838 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
839 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
840 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
841 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
842 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
843 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
844 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
845 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
846 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
847 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
848 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
865 if( needCsvQuote[((
unsigned char*)z)[i]]
875 if( z[i]==
'"' ) putc(
'"', out);
892 static void interrupt_handler(
int NotUsed){
895 if( seenInterrupt>2 ) exit(1);
900 #ifndef SQLITE_OMIT_AUTHORIZATION 914 static const char *azAction[] = { 0,
915 "CREATE_INDEX",
"CREATE_TABLE",
"CREATE_TEMP_INDEX",
916 "CREATE_TEMP_TABLE",
"CREATE_TEMP_TRIGGER",
"CREATE_TEMP_VIEW",
917 "CREATE_TRIGGER",
"CREATE_VIEW",
"DELETE",
918 "DROP_INDEX",
"DROP_TABLE",
"DROP_TEMP_INDEX",
919 "DROP_TEMP_TABLE",
"DROP_TEMP_TRIGGER",
"DROP_TEMP_VIEW",
920 "DROP_TRIGGER",
"DROP_VIEW",
"INSERT",
921 "PRAGMA",
"READ",
"SELECT",
922 "TRANSACTION",
"UPDATE",
"ATTACH",
923 "DETACH",
"ALTER_TABLE",
"REINDEX",
924 "ANALYZE",
"CREATE_VTABLE",
"DROP_VTABLE",
925 "FUNCTION",
"SAVEPOINT",
"RECURSIVE" 965 if( azArg==0 )
break;
966 for(i=0; i<nArg; i++){
967 int len =
strlen30(azCol[i] ? azCol[i] :
"");
971 for(i=0; i<nArg; i++){
979 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
988 colWidth = aExplainWidths;
993 for(i=0; i<nArg; i++){
1001 w =
strlen30(azCol[i] ? azCol[i] :
"");
1012 i==nArg-1 ? rowSep :
" ");
1015 i==nArg-1 ? rowSep :
" ");
1020 for(i=0; i<nArg; i++){
1029 "----------------------------------------------------------" 1030 "----------------------------------------------------------",
1031 i==nArg-1 ? rowSep :
" ");
1035 if( azArg==0 )
break;
1036 for(i=0; i<nArg; i++){
1055 i==nArg-1 ? rowSep :
" ");
1059 i==nArg-1 ? rowSep :
" ");
1076 if( azArg[0]==0 )
break;
1085 for(i=0;
IsSpace(z[i]); i++){}
1086 for(; (c = z[i])!=0; i++){
1088 if(
IsSpace(z[j-1]) || z[j-1]==
'(' )
continue;
1089 }
else if( (c==
'(' || c==
')') && j>0 &&
IsSpace(z[j-1]) ){
1094 while( j>0 &&
IsSpace(z[j-1]) ){ j--; }
1097 for(i=j=0; (c = z[i])!=0; i++){
1100 }
else if( c==
'"' || c==
'\'' || c==
'`' ){
1108 if( nLine>0 && nParen==0 && j>0 ){
1114 if( nParen==1 && (c==
'(' || c==
',' || c==
'\n') ){
1119 while(
IsSpace(z[i+1]) ){ i++; }
1130 for(i=0; i<nArg; i++){
1135 if( azArg==0 )
break;
1136 for(i=0; i<nArg; i++){
1151 for(i=0; i<nArg; i++){
1158 if( azArg==0 )
break;
1160 for(i=0; i<nArg; i++){
1170 for(i=0; i<nArg; i++){
1176 if( azArg==0 )
break;
1177 for(i=0; i<nArg; i++){
1187 for(i=0; i<nArg; i++){
1188 output_csv(p, azCol[i] ? azCol[i] :
"", i<nArg-1);
1193 for(i=0; i<nArg; i++){
1203 if( azArg==0 )
break;
1207 for(i=0; i<nArg; i++){
1208 char *zSep = i>0 ?
",":
"";
1214 for(i=0; i<nArg; i++){
1215 char *zSep = i>0 ?
",":
"";
1216 if( (azArg[i]==0) || (aiType && aiType[i]==
SQLITE_NULL) ){
1241 for(i=0; i<nArg; i++){
1247 if( azArg==0 )
break;
1248 for(i=0; i<nArg; i++){
1263 static int callback(
void *pArg,
int nArg,
char **azArg,
char **azCol){
1282 if( zName==0 )
return;
1283 needQuote = !isalpha((
unsigned char)*zName) && *zName!=
'_';
1284 for(i=n=0; zName[i]; i++, n++){
1285 if( !isalnum((
unsigned char)zName[i]) && zName[i]!=
'_' ){
1287 if( zName[i]==
'\'' ) n++;
1290 if( needQuote ) n += 2;
1297 if( needQuote ) z[n++] =
'\'';
1298 for(i=0; zName[i]; i++){
1300 if( zName[i]==
'\'' ) z[n++] =
'\'';
1302 if( needQuote ) z[n++] =
'\'';
1314 static char *
appendText(
char *zIn,
char const *zAppend,
char quote){
1320 len = nAppend+nIn+1;
1323 for(i=0; i<nAppend; i++){
1324 if( zAppend[i]==quote ) len++;
1328 zIn = (
char *)realloc(zIn, len);
1334 char *zCsr = &zIn[nIn];
1336 for(i=0; i<nAppend; i++){
1337 *zCsr++ = zAppend[i];
1338 if( zAppend[i]==quote ) *zCsr++ = quote;
1342 assert( (zCsr-zIn)==len );
1344 memcpy(&zIn[nIn], zAppend, nAppend);
1364 const char *zSelect,
1365 const char *zFirstRow
1388 for(i=1; i<nResult; i++){
1392 while( z[0] && (z[0]!=
'-' || z[1]!=
'-') ) z++;
1427 static void displayLinuxIoStats(FILE *out){
1431 in = fopen(z,
"rb");
1433 while( fgets(z,
sizeof(z), in)!=0 ){
1434 static const struct {
1435 const char *zPattern;
1438 {
"rchar: ",
"Bytes received by read():" },
1439 {
"wchar: ",
"Bytes sent to write():" },
1440 {
"syscr: ",
"Read() system calls:" },
1441 {
"syscw: ",
"Write() system calls:" },
1442 {
"read_bytes: ",
"Bytes read from storage:" },
1443 {
"write_bytes: ",
"Bytes written to storage:" },
1444 {
"cancelled_write_bytes: ",
"Cancelled write bytes:" },
1448 int n = (int)strlen(aTrans[i].zPattern);
1449 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
1450 utf8_printf(out,
"%-36s %s", aTrans[i].zDesc, &z[n]);
1471 if( pArg && pArg->
out ){
1476 "Memory Used: %d (max %d) bytes\n",
1480 raw_printf(pArg->
out,
"Number of Outstanding Allocations: %d (max %d)\n",
1486 "Number of Pcache Pages Used: %d (max %d) pages\n",
1492 "Number of Pcache Overflow Bytes: %d (max %d) bytes\n",
1498 "Number of Scratch Allocations Used: %d (max %d)\n",
1504 "Number of Scratch Overflow Bytes: %d (max %d) bytes\n",
1512 raw_printf(pArg->
out,
"Largest Pcache Allocation: %d bytes\n",
1516 raw_printf(pArg->
out,
"Largest Scratch Allocation: %d bytes\n",
1518 #ifdef YYTRACKMAXSTACKDEPTH 1521 raw_printf(pArg->
out,
"Deepest Parser Stack: %d (max %d)\n",
1526 if( pArg && pArg->
out && db ){
1530 &iCur, &iHiwtr, bReset);
1532 "Lookaside Slots Used: %d (max %d)\n",
1535 &iCur, &iHiwtr, bReset);
1536 raw_printf(pArg->
out,
"Successful lookaside attempts: %d\n",
1539 &iCur, &iHiwtr, bReset);
1540 raw_printf(pArg->
out,
"Lookaside failures due to size: %d\n",
1543 &iCur, &iHiwtr, bReset);
1544 raw_printf(pArg->
out,
"Lookaside failures due to OOM: %d\n",
1566 raw_printf(pArg->
out,
"Statement Heap/Lookaside Usage: %d bytes\n",
1570 if( pArg && pArg->
out && db && pArg->
pStmt ){
1583 displayLinuxIoStats(pArg->
out);
1598 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS 1605 for(k=0; k<=mx; k++){
1606 double rEstLoop = 1.0;
1612 const char *zExplain;
1617 if( iSid>mx ) mx = iSid;
1618 if( iSid!=k )
continue;
1620 rEstLoop = (double)nLoop;
1621 if( k>0 )
raw_printf(pArg->
out,
"-------- subquery %d -------\n", k);
1630 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
1647 for(i=0; azArray[i]; i++){
1648 if( 0==strcmp(zStr, azArray[i]) )
return 1;
1678 const char *azNext[] = {
"Next",
"Prev",
"VPrev",
"VNext",
"SorterNext",
1679 "NextIfOpen",
"PrevIfOpen", 0 };
1680 const char *azYield[] = {
"Yield",
"SeekLT",
"SeekGT",
"RowSetRead",
1682 const char *azGoto[] = {
"Goto", 0 };
1691 if( zSql==0 )
return;
1692 for(z=zSql; *z==
' ' || *z==
'\t' || *z==
'\n' || *z==
'\f' || *z==
'\r'; z++);
1709 int p2op = (p2 + (iOp-iAddr));
1716 static const char *explainCols[] = {
1717 "addr",
"opcode",
"p1",
"p2",
"p3",
"p4",
"p5",
"comment" };
1719 for(jj=0; jj<
ArraySize(explainCols); jj++){
1736 for(i=p2op; i<iOp; i++) p->
aiIndent[i] += 2;
1741 for(i=p2op; i<iOp; i++) p->
aiIndent[i] += 2;
1763 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 1764 extern int sqlite3SelectTrace;
1765 static int savedSelectTrace;
1767 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 1768 extern int sqlite3WhereTrace;
1769 static int savedWhereTrace;
1772 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 1773 savedSelectTrace = sqlite3SelectTrace;
1774 sqlite3SelectTrace = 0;
1776 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 1777 savedWhereTrace = sqlite3WhereTrace;
1778 sqlite3WhereTrace = 0;
1782 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 1783 sqlite3SelectTrace = savedSelectTrace;
1785 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 1786 sqlite3WhereTrace = savedWhereTrace;
1796 int (*xCallback)(
void*,
int,
char**,
char**,
int*)
1814 char **azCols = (
char **)pData;
1815 char **azVals = &azCols[nCol];
1816 int *aiTypes = (
int *)&azVals[nCol];
1818 assert(
sizeof(
int) <=
sizeof(
char *));
1820 for(i=0; i<nCol; i++){
1825 for(i=0; i<nCol; i++){
1841 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
1870 int (*xCallback)(
void*,
int,
char**,
char**,
int*),
1878 const char *zLeftover;
1885 static const char *zStmtSql;
1895 while(
IsSpace(zSql[0]) ) zSql++;
1899 while(
IsSpace(zStmtSql[0]) ) zStmtSql++;
1903 pArg->
pStmt = pStmt;
1908 if( pArg && pArg->
echoOn ){
1981 while(
IsSpace(zSql[0]) ) zSql++;
1982 }
else if( pzErrMsg ){
2008 const char *zPrepStmt = 0;
2012 if( nArg!=3 )
return 1;
2017 if( strcmp(zTable,
"sqlite_sequence")==0 ){
2018 zPrepStmt =
"DELETE FROM sqlite_sequence;\n";
2021 }
else if( strncmp(zTable,
"sqlite_", 7)==0 ){
2023 }
else if( strncmp(zSql,
"CREATE VIRTUAL TABLE", 20)==0 ){
2030 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" 2031 "VALUES('table','%q','%q',0,'%q');",
2032 zTable, zTable, zSql);
2040 if( strcmp(zType,
"table")==0 ){
2043 char *zTableInfo = 0;
2047 zTableInfo =
appendText(zTableInfo,
"PRAGMA table_info(", 0);
2048 zTableInfo =
appendText(zTableInfo, zTable,
'"');
2049 zTableInfo =
appendText(zTableInfo,
");", 0);
2057 zSelect =
appendText(zSelect,
"SELECT 'INSERT INTO ' || ", 0);
2065 zSelect =
appendText(zSelect,
" || ' VALUES(' || ", 0);
2084 zSelect =
appendText(zSelect,
"|| ')' FROM ", 0);
2089 zSelect =
appendText(zSelect,
" ORDER BY rowid DESC", 0);
2120 zQ2 = malloc( len+100 );
2121 if( zQ2==0 )
return rc;
2139 #ifndef SQLITE_OMIT_AUTHORIZATION 2140 ".auth ON|OFF Show authorizer callbacks\n" 2142 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" 2143 ".bail on|off Stop after hitting an error. Default OFF\n" 2144 ".binary on|off Turn binary output on or off. Default OFF\n" 2145 ".changes on|off Show number of rows changed by SQL\n" 2146 ".check GLOB Fail if output since .testcase does not match\n" 2147 ".clone NEWDB Clone data into NEWDB from the existing database\n" 2148 ".databases List names and files of attached databases\n" 2149 ".dbinfo ?DB? Show status information about the database\n" 2150 ".dump ?TABLE? ... Dump the database in an SQL text format\n" 2151 " If TABLE specified, only dump tables matching\n" 2152 " LIKE pattern TABLE.\n" 2153 ".echo on|off Turn command echo on or off\n" 2154 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n" 2155 ".exit Exit this program\n" 2156 ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n" 2157 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n" 2158 ".headers on|off Turn display of headers on or off\n" 2159 ".help Show this message\n" 2160 ".import FILE TABLE Import data from FILE into TABLE\n" 2161 ".indexes ?TABLE? Show names of all indexes\n" 2162 " If TABLE specified, only show indexes for tables\n" 2163 " matching LIKE pattern TABLE.\n" 2164 #ifdef SQLITE_ENABLE_IOTRACE 2165 ".iotrace FILE Enable I/O diagnostic logging to FILE\n" 2167 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n" 2168 #ifndef SQLITE_OMIT_LOAD_EXTENSION 2169 ".load FILE ?ENTRY? Load an extension library\n" 2171 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n" 2172 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" 2173 " ascii Columns/rows delimited by 0x1F and 0x1E\n" 2174 " csv Comma-separated values\n" 2175 " column Left-aligned columns. (See .width)\n" 2176 " html HTML <table> code\n" 2177 " insert SQL insert statements for TABLE\n" 2178 " line One value per line\n" 2179 " list Values delimited by .separator strings\n" 2180 " tabs Tab-separated values\n" 2181 " tcl TCL list elements\n" 2182 ".nullvalue STRING Use STRING in place of NULL values\n" 2183 ".once FILENAME Output for the next SQL command only to FILENAME\n" 2184 ".open ?--new? ?FILE? Close existing database and reopen FILE\n" 2185 " The --new starts with an empty file\n" 2186 ".output ?FILENAME? Send output to FILENAME or stdout\n" 2187 ".print STRING... Print literal STRING\n" 2188 ".prompt MAIN CONTINUE Replace the standard prompts\n" 2189 ".quit Exit this program\n" 2190 ".read FILENAME Execute SQL in FILENAME\n" 2191 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" 2192 ".save FILE Write in-memory database into FILE\n" 2193 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n" 2194 ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n" 2195 " Add --indent for pretty-printing\n" 2196 ".separator COL ?ROW? Change the column separator and optionally the row\n" 2197 " separator for both the output mode and .import\n" 2198 #if defined(SQLITE_ENABLE_SESSION) 2199 ".session CMD ... Create or control sessions\n" 2201 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n" 2202 ".show Show the current values for various settings\n" 2203 ".stats ?on|off? Show stats or turn stats on or off\n" 2204 ".system CMD ARGS... Run CMD ARGS... in a system shell\n" 2205 ".tables ?TABLE? List names of tables\n" 2206 " If TABLE specified, only list tables matching\n" 2207 " LIKE pattern TABLE.\n" 2208 ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n" 2209 ".timeout MS Try opening locked tables for MS milliseconds\n" 2210 ".timer on|off Turn SQL timer on or off\n" 2211 ".trace FILE|off Output each SQL statement as it is run\n" 2212 ".vfsinfo ?AUX? Information about the top-level VFS\n" 2213 ".vfslist List all available VFSes\n" 2214 ".vfsname ?AUX? Print the name of the VFS stack\n" 2215 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" 2216 " Negative values right-justify\n" 2219 #if defined(SQLITE_ENABLE_SESSION) 2225 ".session ?NAME? SUBCOMMAND ?ARGS...?\n" 2226 "If ?NAME? is omitted, the first defined session is used.\n" 2228 " attach TABLE Attach TABLE\n" 2229 " changeset FILE Write a changeset into FILE\n" 2230 " close Close one session\n" 2231 " enable ?BOOLEAN? Set or query the enable bit\n" 2232 " filter GLOB... Reject tables matching GLOBs\n" 2233 " indirect ?BOOLEAN? Mark or query the indirect status\n" 2234 " isempty Query whether the session is empty\n" 2235 " list List currently open session names\n" 2236 " open DB NAME Open a new session on DB\n" 2237 " patchset FILE Write a patchset into FILE\n" 2254 FILE *in = fopen(zName,
"rb");
2258 if( in==0 )
return 0;
2259 fseek(in, 0, SEEK_END);
2263 if( pBuf==0 )
return 0;
2264 nRead = fread(pBuf, nIn, 1, in);
2289 if( zName==0 )
return;
2312 if( zFile==0 )
return;
2313 out = fopen(zFile,
"wb");
2314 if( out==0 )
return;
2325 #if defined(SQLITE_ENABLE_SESSION) 2330 static void session_close(OpenSession *pSession){
2332 sqlite3session_delete(pSession->p);
2334 for(i=0; i<pSession->nFilter; i++){
2338 memset(pSession, 0,
sizeof(OpenSession));
2345 #if defined(SQLITE_ENABLE_SESSION) 2348 for(i=0; i<p->nSession; i++){
2349 session_close(&p->aSession[i]);
2354 # define session_close_all(X) 2361 #if defined(SQLITE_ENABLE_SESSION) 2362 static int session_filter(
void *pCtx,
const char *zTab){
2363 OpenSession *pSession = (OpenSession*)pCtx;
2365 for(i=0; i<pSession->nFilter; i++){
2386 utf8_printf(stderr,
"Error: unable to open database \"%s\": %s\n",
2388 if( keepAlive )
return;
2391 #ifndef SQLITE_OMIT_LOAD_EXTENSION 2420 while( *z && *z!=
'\\' ) z++;
2421 for(i=j=0; (c = z[i])!=0; i++, j++){
2422 if( c==
'\\' && z[i+1]!=0 ){
2440 }
else if( c==
'\'' ){
2442 }
else if( c==
'\\' ){
2444 }
else if( c>=
'0' && c<=
'7' ){
2446 if( z[i+1]>=
'0' && z[i+1]<=
'7' ){
2448 c = (c<<3) + z[i] -
'0';
2449 if( z[i+1]>=
'0' && z[i+1]<=
'7' ){
2451 c = (c<<3) + z[i] -
'0';
2466 if( c>=
'0' && c<=
'9' )
return c -
'0';
2467 if( c>=
'a' && c<=
'f' )
return c -
'a' + 10;
2468 if( c>=
'A' && c<=
'F' )
return c -
'A' + 10;
2477 static const struct {
char *zSuffix;
int iMult; } aMult[] = {
2479 {
"MiB", 1024*1024 },
2480 {
"GiB", 1024*1024*1024 },
2483 {
"GB", 1000000000 },
2486 {
"G", 1000000000 },
2493 }
else if( zArg[0]==
'+' ){
2496 if( zArg[0]==
'0' && zArg[1]==
'x' ){
2505 v = v*10 + zArg[0] -
'0';
2511 v *= aMult[i].iMult;
2515 return isNeg? -v : v;
2524 if( zArg[0]==
'0' && zArg[1]==
'x' ){
2527 for(i=0; zArg[i]>=
'0' && zArg[i]<=
'9'; i++){}
2529 if( i>0 && zArg[i]==0 )
return (
int)(
integerValue(zArg) & 0xffffffff);
2536 utf8_printf(stderr,
"ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
2545 if( f && f!=stdout && f!=stderr ) fclose(f);
2555 if( strcmp(zFile,
"stdout")==0 ){
2557 }
else if( strcmp(zFile,
"stderr")==0 ){
2559 }
else if( strcmp(zFile,
"off")==0 ){
2562 f = fopen(zFile,
"wb");
2564 utf8_printf(stderr,
"Error: cannot open \"%s\"\n", zFile);
2579 FILE *
f = (FILE*)pArg;
2583 const char *
z = (
const char*)pX;
2584 int i = (int)strlen(z);
2585 while( i>0 && z[i-1]==
';' ){ i--; }
2596 static int nCall = 0;
2626 p->
z[p->
n++] = (char)c;
2648 if( c==EOF || seenInterrupt ){
2654 int startLine = p->
nLine;
2659 if( c==rSep ) p->
nLine++;
2666 if( (c==cSep && pc==cQuote)
2667 || (c==rSep && pc==cQuote)
2668 || (c==rSep && pc==
'\r' && ppc==cQuote)
2669 || (c==EOF && pc==cQuote)
2671 do{ p->
n--; }
while( p->
z[p->
n]!=cQuote );
2675 if( pc==cQuote && c!=
'\r' ){
2676 utf8_printf(stderr,
"%s:%d: unescaped %c character\n",
2680 utf8_printf(stderr,
"%s:%d: unterminated %c-quoted field\n",
2681 p->
zFile, startLine, cQuote);
2690 while( c!=EOF && c!=cSep && c!=rSep ){
2696 if( p->
n>0 && p->
z[p->
n-1]==
'\r' ) p->
n--;
2700 if( p->
z ) p->
z[p->
n] = 0;
2722 if( c==EOF || seenInterrupt ){
2726 while( c!=EOF && c!=cSep && c!=rSep ){
2734 if( p->
z ) p->
z[p->
n] = 0;
2754 int nTable = (int)strlen(zTable);
2757 const int spinRate = 10000;
2774 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
2775 i = (int)strlen(zInsert);
2777 memcpy(zInsert+i,
",?", 2);
2780 memcpy(zInsert+i,
");", 3);
2825 if( (cnt%spinRate)==0 ){
2826 printf(
"%c\b",
"|/-\\"[(cnt/spinRate)%4]);
2837 utf8_printf(stderr,
"Warning: cannot step \"%s\" backwards", zTable);
2865 const unsigned char *zName;
2866 const unsigned char *zSql;
2870 " WHERE %s", zWhere);
2876 goto end_schema_xfer;
2881 printf(
"%s... ", zName); fflush(stdout);
2882 sqlite3_exec(newDb, (
const char*)zSql, 0, 0, &zErrMsg);
2884 utf8_printf(stderr,
"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
2889 xForEach(p, newDb, (
const char*)zName);
2897 " WHERE %s ORDER BY rowid DESC", zWhere);
2903 goto end_schema_xfer;
2908 printf(
"%s... ", zName); fflush(stdout);
2909 sqlite3_exec(newDb, (
const char*)zSql, 0, 0, &zErrMsg);
2911 utf8_printf(stderr,
"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
2916 xForEach(p, newDb, (
const char*)zName);
2934 if( access(zNewDb,0)==0 ){
2935 utf8_printf(stderr,
"File \"%s\" already exists.\n", zNewDb);
2940 utf8_printf(stderr,
"Cannot create output database: %s\n",
2958 #ifndef SQLITE_OMIT_POPEN 2986 return (a[0]<<8) + a[1];
2989 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
2998 static const struct {
const char *zName;
int ofst; } aField[] = {
2999 {
"file change counter:", 24 },
3000 {
"database page count:", 28 },
3001 {
"freelist page count:", 36 },
3002 {
"schema cookie:", 40 },
3003 {
"schema format:", 44 },
3004 {
"default cache size:", 48 },
3005 {
"autovacuum top root:", 52 },
3006 {
"incremental vacuum:", 64 },
3007 {
"text encoding:", 56 },
3008 {
"user version:", 60 },
3009 {
"application id:", 68 },
3010 {
"software version:", 96 },
3012 static const struct {
const char *zName;
const char *zSql; } aQuery[] = {
3013 {
"number of tables:",
3014 "SELECT count(*) FROM %s WHERE type='table'" },
3015 {
"number of indexes:",
3016 "SELECT count(*) FROM %s WHERE type='index'" },
3017 {
"number of triggers:",
3018 "SELECT count(*) FROM %s WHERE type='trigger'" },
3019 {
"number of views:",
3020 "SELECT count(*) FROM %s WHERE type='view'" },
3022 "SELECT total(length(sql)) FROM %s" },
3027 char *zDb = nArg>=2 ? azArg[1] :
"main";
3028 unsigned char aHdr[100];
3030 if( p->
db==0 )
return 1;
3037 raw_printf(stderr,
"unable to read database header\n");
3041 if( i==1 ) i = 65536;
3047 int ofst = aField[i].ofst;
3061 }
else if( strcmp(zDb,
"temp")==0 ){
3068 int val =
db_int(p, zSql);
3089 raw_printf(stderr,
"Error: out of memory\n");
3121 while( (c = (*(zGlob++)))!=0 ){
3124 while(
IsSpace(*zGlob) ) zGlob++;
3127 while( (c=(*(zGlob++))) ==
'*' || c==
'?' ){
3128 if( c==
'?' && (*(z++))==0 )
return 0;
3138 while( (c2 = (*(z++)))!=0 ){
3141 if( c2==0 )
return 0;
3147 if( (*(z++))==0 )
return 0;
3153 if( c==0 )
return 0;
3160 if( c==
']' ) seen = 1;
3163 while( c2 && c2!=
']' ){
3164 if( c2==
'-' && zGlob[0]!=
']' && zGlob[0]!=0 && prior_c>0 ){
3166 if( c>=prior_c && c<=c2 ) seen = 1;
3176 if( c2==0 || (seen ^ invert)==0 )
return 0;
3178 if( (z[0]==
'-' || z[0]==
'+') &&
IsDigit(z[1]) ) z++;
3179 if( !
IsDigit(z[0]) )
return 0;
3181 while(
IsDigit(z[0]) ){ z++; }
3183 if( c!=(*(z++)) )
return 0;
3196 if( zStr[0]!=
'-' )
return 0;
3198 if( zStr[0]==
'-' ) zStr++;
3199 return strcmp(zStr, zOpt)==0;
3208 wchar_t *
z = sqlite3_win32_utf8_to_unicode(zFilename);
3212 rc = unlink(zFilename);
3232 while( zLine[h] && nArg<
ArraySize(azArg) ){
3233 while(
IsSpace(zLine[h]) ){ h++; }
3234 if( zLine[h]==0 )
break;
3235 if( zLine[h]==
'\'' || zLine[h]==
'"' ){
3236 int delim = zLine[h++];
3237 azArg[nArg++] = &zLine[h];
3238 while( zLine[h] && zLine[h]!=delim ){
3239 if( zLine[h]==
'\\' && delim==
'"' && zLine[h+1]!=0 ) h++;
3242 if( zLine[h]==delim ){
3247 azArg[nArg++] = &zLine[h];
3248 while( zLine[h] && !
IsSpace(zLine[h]) ){ h++; }
3249 if( zLine[h] ) zLine[h++] = 0;
3256 if( nArg==0 )
return 0;
3260 #ifndef SQLITE_OMIT_AUTHORIZATION 3261 if( c==
'a' && strncmp(azArg[0],
"auth", n)==0 ){
3265 goto meta_command_exit;
3276 if( (c==
'b' && n>=3 && strncmp(azArg[0],
"backup", n)==0)
3277 || (c==
's' && n>=3 && strncmp(azArg[0],
"save", n)==0)
3279 const char *zDestFile = 0;
3280 const char *zDb = 0;
3284 for(j=1; j<nArg; j++){
3285 const char *
z = azArg[j];
3287 while( z[0]==
'-' ) z++;
3290 utf8_printf(stderr,
"unknown option: %s\n", azArg[j]);
3293 }
else if( zDestFile==0 ){
3294 zDestFile = azArg[j];
3297 zDestFile = azArg[j];
3299 raw_printf(stderr,
"too many arguments to .backup\n");
3304 raw_printf(stderr,
"missing FILENAME argument on .backup\n");
3307 if( zDb==0 ) zDb =
"main";
3310 utf8_printf(stderr,
"Error: cannot open \"%s\"\n", zDestFile);
3332 if( c==
'b' && n>=3 && strncmp(azArg[0],
"bail", n)==0 ){
3341 if( c==
'b' && n>=3 && strncmp(azArg[0],
"binary", n)==0 ){
3349 raw_printf(stderr,
"Usage: .binary on|off\n");
3357 if( c==
'b' && n>=3 && strncmp(azArg[0],
"breakpoint", n)==0 ){
3361 if( c==
'c' && n>=3 && strncmp(azArg[0],
"changes", n)==0 ){
3365 raw_printf(stderr,
"Usage: .changes on|off\n");
3374 if( c==
'c' && n>=3 && strncmp(azArg[0],
"check", n)==0 ){
3378 raw_printf(stderr,
"Usage: .check GLOB-PATTERN\n");
3380 }
else if( (zRes =
readFile(
"testcase-out.txt"))==0 ){
3381 raw_printf(stderr,
"Error: cannot read 'testcase-out.txt'\n");
3385 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
3395 if( c==
'c' && strncmp(azArg[0],
"clone", n)==0 ){
3399 raw_printf(stderr,
"Usage: .clone FILENAME\n");
3404 if( c==
'd' && n>1 && strncmp(azArg[0],
"databases", n)==0 ){
3408 memcpy(&data, p,
sizeof(data));
3423 if( c==
'd' && strncmp(azArg[0],
"dbinfo", n)==0 ){
3427 if( c==
'd' && strncmp(azArg[0],
"dump", n)==0 ){
3432 if( nArg!=1 && nArg!=2 ){
3433 raw_printf(stderr,
"Usage: .dump ?LIKE-PATTERN?\n");
3435 goto meta_command_exit;
3440 sqlite3_exec(p->
db,
"SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
3444 "SELECT name, type, sql FROM sqlite_master " 3445 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'" 3448 "SELECT name, type, sql FROM sqlite_master " 3449 "WHERE name=='sqlite_sequence'" 3452 "SELECT sql FROM sqlite_master " 3453 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
3457 for(i=1; i<nArg; i++){
3458 zShellStatic = azArg[i];
3460 "SELECT name, type, sql FROM sqlite_master " 3461 "WHERE tbl_name LIKE shellstatic() AND type=='table'" 3462 " AND sql NOT NULL");
3464 "SELECT sql FROM sqlite_master " 3465 "WHERE sql NOT NULL" 3466 " AND type IN ('index','trigger','view')" 3467 " AND tbl_name LIKE shellstatic()", 0
3481 if( c==
'e' && strncmp(azArg[0],
"echo", n)==0 ){
3490 if( c==
'e' && strncmp(azArg[0],
"eqp", n)==0 ){
3492 if( strcmp(azArg[1],
"full")==0 ){
3498 raw_printf(stderr,
"Usage: .eqp on|off|full\n");
3503 if( c==
'e' && strncmp(azArg[0],
"exit", n)==0 ){
3504 if( nArg>1 && (rc = (
int)
integerValue(azArg[1]))!=0 ) exit(rc);
3508 if( c==
'e' && strncmp(azArg[0],
"explain", n)==0 ){
3511 if( strcmp(azArg[1],
"auto")==0 ){
3524 }
else if( val==99 ){
3530 if( c==
'f' && strncmp(azArg[0],
"fullschema", n)==0 ){
3534 memcpy(&data, p,
sizeof(data));
3542 raw_printf(stderr,
"Usage: .fullschema ?--indent?\n");
3544 goto meta_command_exit;
3549 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" 3550 " FROM sqlite_master UNION ALL" 3551 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " 3552 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " 3559 "SELECT rowid FROM sqlite_master" 3560 " WHERE name GLOB 'sqlite_stat[134]'",
3585 if( c==
'h' && strncmp(azArg[0],
"headers", n)==0 ){
3589 raw_printf(stderr,
"Usage: .headers on|off\n");
3594 if( c==
'h' && strncmp(azArg[0],
"help", n)==0 ){
3598 if( c==
'i' && strncmp(azArg[0],
"import", n)==0 ){
3613 raw_printf(stderr,
"Usage: .import FILE TABLE\n");
3614 goto meta_command_exit;
3619 memset(&sCtx, 0,
sizeof(sCtx));
3624 "Error: non-null column separator required for import\n");
3628 raw_printf(stderr,
"Error: multi-character column separators not allowed" 3634 raw_printf(stderr,
"Error: non-null row separator required for import\n");
3646 raw_printf(stderr,
"Error: multi-character row separators not allowed" 3652 if( sCtx.
zFile[0]==
'|' ){
3653 #ifdef SQLITE_OMIT_POPEN 3654 raw_printf(stderr,
"Error: pipes are not supported in this OS\n");
3658 sCtx.
zFile =
"<pipe>";
3662 sCtx.
in = fopen(sCtx.
zFile,
"rb");
3671 utf8_printf(stderr,
"Error: cannot open \"%s\"\n", zFile);
3678 raw_printf(stderr,
"Error: out of memory\n");
3688 while( xRead(&sCtx) ){
3704 utf8_printf(stderr,
"CREATE TABLE %s(...) failed: %s\n", zTable,
3722 if( nCol==0 )
return 0;
3725 raw_printf(stderr,
"Error: out of memory\n");
3731 for(i=1; i<nCol; i++){
3748 int startLine = sCtx.
nLine;
3749 for(i=0; i<nCol; i++){
3750 char *
z = xRead(&sCtx);
3755 if( z==0 && i==0 )
break;
3764 utf8_printf(stderr,
"%s:%d: expected %d columns but found %d - " 3765 "filling the rest with NULL\n",
3766 sCtx.
zFile, startLine, nCol, i+1);
3776 utf8_printf(stderr,
"%s:%d: expected %d columns but found %d - " 3778 sCtx.
zFile, startLine, nCol, i);
3788 }
while( sCtx.
cTerm!=EOF );
3796 if( c==
'i' && (strncmp(azArg[0],
"indices", n)==0
3797 || strncmp(azArg[0],
"indexes", n)==0) ){
3801 memcpy(&data, p,
sizeof(data));
3806 "SELECT name FROM sqlite_master " 3807 "WHERE type='index' AND name NOT LIKE 'sqlite_%' " 3809 "SELECT name FROM sqlite_temp_master " 3810 "WHERE type='index' " 3814 }
else if( nArg==2 ){
3815 zShellStatic = azArg[1];
3817 "SELECT name FROM sqlite_master " 3818 "WHERE type='index' AND tbl_name LIKE shellstatic() " 3820 "SELECT name FROM sqlite_temp_master " 3821 "WHERE type='index' AND tbl_name LIKE shellstatic() " 3827 raw_printf(stderr,
"Usage: .indexes ?LIKE-PATTERN?\n");
3829 goto meta_command_exit;
3837 "Error: querying sqlite_master and sqlite_temp_master\n");
3842 #ifdef SQLITE_ENABLE_IOTRACE 3843 if( c==
'i' && strncmp(azArg[0],
"iotrace", n)==0 ){
3845 if( iotrace && iotrace!=stdout ) fclose(iotrace);
3849 }
else if( strcmp(azArg[1],
"-")==0 ){
3850 sqlite3IoTrace = iotracePrintf;
3853 iotrace = fopen(azArg[1],
"w");
3855 utf8_printf(stderr,
"Error: cannot open \"%s\"\n", azArg[1]);
3859 sqlite3IoTrace = iotracePrintf;
3864 if( c==
'l' && n>=5 && strncmp(azArg[0],
"limits", n)==0 ){
3865 static const struct {
3866 const char *zLimitName;
3886 printf(
"%20s %d\n", aLimit[i].zLimitName,
3890 raw_printf(stderr,
"Usage: .limit NAME ?NEW-VALUE?\n");
3892 goto meta_command_exit;
3901 utf8_printf(stderr,
"ambiguous limit: \"%s\"\n", azArg[1]);
3903 goto meta_command_exit;
3909 "enter \".limits\" with no arguments for a list.\n",
3912 goto meta_command_exit;
3918 printf(
"%20s %d\n", aLimit[iLimit].zLimitName,
3923 #ifndef SQLITE_OMIT_LOAD_EXTENSION 3924 if( c==
'l' && strncmp(azArg[0],
"load", n)==0 ){
3925 const char *zFile, *zProc;
3928 raw_printf(stderr,
"Usage: .load FILE ?ENTRYPOINT?\n");
3930 goto meta_command_exit;
3933 zProc = nArg>=3 ? azArg[2] : 0;
3944 if( c==
'l' && strncmp(azArg[0],
"log", n)==0 ){
3946 raw_printf(stderr,
"Usage: .log FILENAME\n");
3949 const char *zFile = azArg[1];
3955 if( c==
'm' && strncmp(azArg[0],
"mode", n)==0 ){
3956 const char *zMode = nArg>=2 ? azArg[1] :
"";
3957 int n2 = (int)strlen(zMode);
3959 if( c2==
'l' && n2>2 && strncmp(azArg[1],
"lines",n2)==0 ){
3961 }
else if( c2==
'c' && strncmp(azArg[1],
"columns",n2)==0 ){
3963 }
else if( c2==
'l' && n2>2 && strncmp(azArg[1],
"list",n2)==0 ){
3965 }
else if( c2==
'h' && strncmp(azArg[1],
"html",n2)==0 ){
3967 }
else if( c2==
't' && strncmp(azArg[1],
"tcl",n2)==0 ){
3970 }
else if( c2==
'c' && strncmp(azArg[1],
"csv",n2)==0 ){
3974 }
else if( c2==
't' && strncmp(azArg[1],
"tabs",n2)==0 ){
3977 }
else if( c2==
'i' && strncmp(azArg[1],
"insert",n2)==0 ){
3980 }
else if( c2==
'a' && strncmp(azArg[1],
"ascii",n2)==0 ){
3985 raw_printf(stderr,
"Error: mode should be one of: " 3986 "ascii column csv html insert line list tabs tcl\n");
3992 if( c==
'n' && strncmp(azArg[0],
"nullvalue", n)==0 ){
3997 raw_printf(stderr,
"Usage: .nullvalue STRING\n");
4002 if( c==
'o' && strncmp(azArg[0],
"open", n)==0 && n>=2 ){
4013 for(iName=1; iName<nArg && azArg[iName][0]==
'-'; iName++){
4014 const char *
z = azArg[iName];
4017 }
else if( z[0]==
'-' ){
4020 goto meta_command_exit;
4030 utf8_printf(stderr,
"Error: cannot open '%s'\n", zNewFilename);
4044 && (strncmp(azArg[0],
"output", n)==0 || strncmp(azArg[0],
"once", n)==0)
4046 const char *zFile = nArg>=2 ? azArg[1] :
"stdout";
4048 utf8_printf(stderr,
"Usage: .%s FILE\n", azArg[0]);
4050 goto meta_command_exit;
4052 if( n>1 && strncmp(azArg[0],
"once", n)==0 ){
4056 goto meta_command_exit;
4063 if( zFile[0]==
'|' ){
4064 #ifdef SQLITE_OMIT_POPEN 4065 raw_printf(stderr,
"Error: pipes are not supported in this OS\n");
4071 utf8_printf(stderr,
"Error: cannot open pipe \"%s\"\n", zFile + 1);
4081 if( strcmp(zFile,
"off")!=0 ){
4082 utf8_printf(stderr,
"Error: cannot write to \"%s\"\n", zFile);
4092 if( c==
'p' && n>=3 && strncmp(azArg[0],
"print", n)==0 ){
4094 for(i=1; i<nArg; i++){
4101 if( c==
'p' && strncmp(azArg[0],
"prompt", n)==0 ){
4103 strncpy(mainPrompt,azArg[1],(
int)
ArraySize(mainPrompt)-1);
4106 strncpy(continuePrompt,azArg[2],(
int)
ArraySize(continuePrompt)-1);
4110 if( c==
'q' && strncmp(azArg[0],
"quit", n)==0 ){
4114 if( c==
'r' && n>=3 && strncmp(azArg[0],
"read", n)==0 ){
4119 goto meta_command_exit;
4121 alt = fopen(azArg[1],
"rb");
4123 utf8_printf(stderr,
"Error: cannot open \"%s\"\n", azArg[1]);
4131 if( c==
'r' && n>=3 && strncmp(azArg[0],
"restore", n)==0 ){
4132 const char *zSrcFile;
4139 zSrcFile = azArg[1];
4141 }
else if( nArg==3 ){
4142 zSrcFile = azArg[2];
4145 raw_printf(stderr,
"Usage: .restore ?DB? FILE\n");
4147 goto meta_command_exit;
4151 utf8_printf(stderr,
"Error: cannot open \"%s\"\n", zSrcFile);
4165 if( nTimeout++ >= 3 )
break;
4173 raw_printf(stderr,
"Error: source database is busy\n");
4183 if( c==
's' && strncmp(azArg[0],
"scanstats", n)==0 ){
4186 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS 4187 raw_printf(stderr,
"Warning: .scanstats not available in this build.\n");
4190 raw_printf(stderr,
"Usage: .scanstats on|off\n");
4195 if( c==
's' && strncmp(azArg[0],
"schema", n)==0 ){
4199 memcpy(&data, p,
sizeof(data));
4205 if( nArg==2 ) azArg[1] = azArg[2];
4207 if( nArg==2 && azArg[1][0]!=
'-' ){
4209 for(i=0; azArg[1][i]; i++) azArg[1][i] =
ToLower(azArg[1][i]);
4210 if( strcmp(azArg[1],
"sqlite_master")==0 ){
4211 char *new_argv[2], *new_colv[2];
4212 new_argv[0] =
"CREATE TABLE sqlite_master (\n" 4216 " rootpage integer,\n" 4220 new_colv[0] =
"sql";
4222 callback(&data, 1, new_argv, new_colv);
4224 }
else if( strcmp(azArg[1],
"sqlite_temp_master")==0 ){
4225 char *new_argv[2], *new_colv[2];
4226 new_argv[0] =
"CREATE TEMP TABLE sqlite_temp_master (\n" 4230 " rootpage integer,\n" 4234 new_colv[0] =
"sql";
4236 callback(&data, 1, new_argv, new_colv);
4239 zShellStatic = azArg[1];
4242 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" 4243 " FROM sqlite_master UNION ALL" 4244 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " 4245 "WHERE lower(tbl_name) LIKE shellstatic()" 4246 " AND type!='meta' AND sql NOTNULL " 4251 }
else if( nArg==1 ){
4254 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" 4255 " FROM sqlite_master UNION ALL" 4256 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) " 4257 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " 4262 raw_printf(stderr,
"Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
4264 goto meta_command_exit;
4271 raw_printf(stderr,
"Error: querying schema information\n");
4278 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE) 4279 if( c==
's' && n==11 && strncmp(azArg[0],
"selecttrace", n)==0 ){
4284 #if defined(SQLITE_ENABLE_SESSION) 4285 if( c==
's' && strncmp(azArg[0],
"session",n)==0 && n>=3 ){
4286 OpenSession *pSession = &p->aSession[0];
4287 char **azCmd = &azArg[1];
4289 int nCmd = nArg - 1;
4291 if( nArg<=1 )
goto session_syntax_error;
4294 for(iSes=0; iSes<p->nSession; iSes++){
4295 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 )
break;
4297 if( iSes<p->nSession ){
4298 pSession = &p->aSession[iSes];
4302 pSession = &p->aSession[0];
4311 if( strcmp(azCmd[0],
"attach")==0 ){
4312 if( nCmd!=2 )
goto session_syntax_error;
4313 if( pSession->p==0 ){
4315 raw_printf(stderr,
"ERROR: No sessions are open\n");
4317 rc = sqlite3session_attach(pSession->p, azCmd[1]);
4319 raw_printf(stderr,
"ERROR: sqlite3session_attach() returns %d\n", rc);
4329 if( strcmp(azCmd[0],
"changeset")==0 || strcmp(azCmd[0],
"patchset")==0 ){
4331 if( nCmd!=2 )
goto session_syntax_error;
4332 if( pSession->p==0 )
goto session_not_open;
4333 out = fopen(azCmd[1],
"wb");
4335 utf8_printf(stderr,
"ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
4339 if( azCmd[0][0]==
'c' ){
4340 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
4342 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
4345 printf(
"Error: error code %d\n", rc);
4349 && fwrite(pChng, szChng, 1, out)!=1 ){
4350 raw_printf(stderr,
"ERROR: Failed to write entire %d-byte output\n",
4361 if( strcmp(azCmd[0],
"close")==0 ){
4362 if( nCmd!=1 )
goto session_syntax_error;
4364 session_close(pSession);
4365 p->aSession[iSes] = p->aSession[--p->nSession];
4372 if( strcmp(azCmd[0],
"enable")==0 ){
4374 if( nCmd>2 )
goto session_syntax_error;
4377 ii = sqlite3session_enable(pSession->p, ii);
4379 pSession->zName, ii);
4386 if( strcmp(azCmd[0],
"filter")==0 ){
4388 if( nCmd<2 )
goto session_syntax_error;
4390 for(ii=0; ii<pSession->nFilter; ii++){
4394 nByte =
sizeof(pSession->azFilter[0])*(nCmd-1);
4396 if( pSession->azFilter==0 ){
4397 raw_printf(stderr,
"Error: out or memory\n");
4400 for(ii=1; ii<nCmd; ii++){
4403 pSession->nFilter = ii-1;
4410 if( strcmp(azCmd[0],
"indirect")==0 ){
4412 if( nCmd>2 )
goto session_syntax_error;
4415 ii = sqlite3session_indirect(pSession->p, ii);
4417 pSession->zName, ii);
4424 if( strcmp(azCmd[0],
"isempty")==0 ){
4426 if( nCmd!=1 )
goto session_syntax_error;
4428 ii = sqlite3session_isempty(pSession->p);
4430 pSession->zName, ii);
4437 if( strcmp(azCmd[0],
"list")==0 ){
4438 for(i=0; i<p->nSession; i++){
4447 if( strcmp(azCmd[0],
"open")==0 ){
4449 if( nCmd!=3 )
goto session_syntax_error;
4451 if( zName[0]==0 )
goto session_syntax_error;
4452 for(i=0; i<p->nSession; i++){
4453 if( strcmp(p->aSession[i].zName,zName)==0 ){
4454 utf8_printf(stderr,
"Session \"%s\" already exists\n", zName);
4455 goto meta_command_exit;
4458 if( p->nSession>=
ArraySize(p->aSession) ){
4460 goto meta_command_exit;
4462 pSession = &p->aSession[p->nSession];
4463 rc = sqlite3session_create(p->
db, azCmd[1], &pSession->p);
4465 raw_printf(stderr,
"Cannot open session: error code=%d\n", rc);
4467 goto meta_command_exit;
4469 pSession->nFilter = 0;
4470 sqlite3session_table_filter(pSession->p, session_filter, pSession);
4475 session_syntax_error:
4483 if( c==
's' && n>=10 && strncmp(azArg[0],
"selftest-", 9)==0 ){
4484 if( strncmp(azArg[0]+9,
"boolean", n-9)==0 ){
4486 for(i=1; i<nArg; i++){
4491 if( strncmp(azArg[0]+9,
"integer", n-9)==0 ){
4493 for(i=1; i<nArg; i++){
4503 if( c==
's' && strncmp(azArg[0],
"separator", n)==0 ){
4504 if( nArg<2 || nArg>3 ){
4505 raw_printf(stderr,
"Usage: .separator COL ?ROW?\n");
4519 && (strncmp(azArg[0],
"shell", n)==0 || strncmp(azArg[0],
"system",n)==0)
4524 raw_printf(stderr,
"Usage: .system COMMAND\n");
4526 goto meta_command_exit;
4528 zCmd =
sqlite3_mprintf(strchr(azArg[1],
' ')==0?
"%s":
"\"%s\"", azArg[1]);
4529 for(i=2; i<nArg; i++){
4535 if( x )
raw_printf(stderr,
"System command returns %d\n", x);
4538 if( c==
's' && strncmp(azArg[0],
"show", n)==0 ){
4539 static const char *azBool[] = {
"off",
"on",
"full",
"unk" };
4544 goto meta_command_exit;
4573 if( c==
's' && strncmp(azArg[0],
"stats", n)==0 ){
4576 }
else if( nArg==1 ){
4579 raw_printf(stderr,
"Usage: .stats ?on|off?\n");
4584 if( c==
't' && n>1 && strncmp(azArg[0],
"tables", n)==0 ){
4598 "SELECT name FROM sqlite_master" 4599 " WHERE type IN ('table','view')" 4600 " AND name NOT LIKE 'sqlite_%%'" 4601 " AND name LIKE ?1");
4604 if( zDbName==0 || strcmp(zDbName,
"main")==0 )
continue;
4605 if( strcmp(zDbName,
"temp")==0 ){
4608 "SELECT 'temp.' || name FROM sqlite_temp_master" 4609 " WHERE type IN ('table','view')" 4610 " AND name NOT LIKE 'sqlite_%%'" 4611 " AND name LIKE ?1", zSql);
4615 "SELECT '%q.' || name FROM \"%w\".sqlite_master" 4616 " WHERE type IN ('table','view')" 4617 " AND name NOT LIKE 'sqlite_%%'" 4618 " AND name LIKE ?1", zSql, zDbName, zDbName);
4642 int n2 = nAlloc*2 + 10;
4652 if( 0==azResult[nRow] ){
4663 if( rc==0 && nRow>0 ){
4664 int len, maxlen = 0;
4666 int nPrintCol, nPrintRow;
4667 for(i=0; i<nRow; i++){
4669 if( len>maxlen ) maxlen = len;
4671 nPrintCol = 80/(maxlen+2);
4672 if( nPrintCol<1 ) nPrintCol = 1;
4673 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
4674 for(i=0; i<nPrintRow; i++){
4675 for(j=i; j<nRow; j+=nPrintRow){
4676 char *zSp = j<nPrintRow ?
"" :
" ";
4678 azResult[j] ? azResult[j]:
"");
4689 if( c==
't' && strcmp(azArg[0],
"testcase")==0 ){
4693 utf8_printf(stderr,
"Error: cannot open 'testcase-out.txt'\n");
4702 if( c==
't' && n>=8 && strncmp(azArg[0],
"testctrl", n)==0 && nArg>=2 ){
4703 static const struct {
4704 const char *zCtrlName;
4733 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){
4735 testctrl = aCtrl[i].ctrlCode;
4737 utf8_printf(stderr,
"ambiguous option name: \"%s\"\n", azArg[1]);
4743 if( testctrl<0 ) testctrl = (int)
integerValue(azArg[1]);
4745 utf8_printf(stderr,
"Error: invalid testctrl option: %s\n", azArg[1]);
4753 int opt = (int)strtol(azArg[2], 0, 0);
4757 utf8_printf(stderr,
"Error: testctrl %s takes a single int option\n",
4771 utf8_printf(stderr,
"Error: testctrl %s takes no options\n",
4779 unsigned int opt = (
unsigned int)
integerValue(azArg[2]);
4783 utf8_printf(stderr,
"Error: testctrl %s takes a single unsigned" 4784 " int option\n", azArg[1]);
4797 utf8_printf(stderr,
"Error: testctrl %s takes a single int option\n",
4803 #ifdef SQLITE_N_KEYWORD 4806 const char *opt = azArg[2];
4811 "Error: testctrl %s takes a single char * option\n",
4825 raw_printf(stderr,
"Usage: .testctrl imposter dbName onoff tnum\n");
4835 "Error: CLI support for testctrl %s not implemented\n",
4842 if( c==
't' && n>4 && strncmp(azArg[0],
"timeout", n)==0 ){
4847 if( c==
't' && n>=5 && strncmp(azArg[0],
"timer", n)==0 ){
4851 raw_printf(stderr,
"Error: timer not available on this system.\n");
4855 raw_printf(stderr,
"Usage: .timer on|off\n");
4860 if( c==
't' && strncmp(azArg[0],
"trace", n)==0 ){
4863 raw_printf(stderr,
"Usage: .trace FILE|off\n");
4865 goto meta_command_exit;
4869 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT) 4878 #if SQLITE_USER_AUTHENTICATION 4879 if( c==
'u' && strncmp(azArg[0],
"user", n)==0 ){
4881 raw_printf(stderr,
"Usage: .user SUBCOMMAND ...\n");
4883 goto meta_command_exit;
4886 if( strcmp(azArg[1],
"login")==0 ){
4888 raw_printf(stderr,
"Usage: .user login USER PASSWORD\n");
4890 goto meta_command_exit;
4892 rc = sqlite3_user_authenticate(p->
db, azArg[2], azArg[3],
4893 (
int)strlen(azArg[3]));
4895 utf8_printf(stderr,
"Authentication failed for user %s\n", azArg[2]);
4898 }
else if( strcmp(azArg[1],
"add")==0 ){
4900 raw_printf(stderr,
"Usage: .user add USER PASSWORD ISADMIN\n");
4902 goto meta_command_exit;
4904 rc = sqlite3_user_add(p->
db, azArg[2],
4905 azArg[3], (
int)strlen(azArg[3]),
4908 raw_printf(stderr,
"User-Add failed: %d\n", rc);
4911 }
else if( strcmp(azArg[1],
"edit")==0 ){
4913 raw_printf(stderr,
"Usage: .user edit USER PASSWORD ISADMIN\n");
4915 goto meta_command_exit;
4917 rc = sqlite3_user_change(p->
db, azArg[2],
4918 azArg[3], (
int)strlen(azArg[3]),
4921 raw_printf(stderr,
"User-Edit failed: %d\n", rc);
4924 }
else if( strcmp(azArg[1],
"delete")==0 ){
4926 raw_printf(stderr,
"Usage: .user delete USER\n");
4928 goto meta_command_exit;
4930 rc = sqlite3_user_delete(p->
db, azArg[2]);
4932 raw_printf(stderr,
"User-Delete failed: %d\n", rc);
4936 raw_printf(stderr,
"Usage: .user login|add|edit|delete ...\n");
4938 goto meta_command_exit;
4943 if( c==
'v' && strncmp(azArg[0],
"version", n)==0 ){
4948 if( c==
'v' && strncmp(azArg[0],
"vfsinfo", n)==0 ){
4949 const char *zDbName = nArg==2 ? azArg[1] :
"main";
4962 if( c==
'v' && strncmp(azArg[0],
"vfslist", n)==0 ){
4970 pVfs==pCurrent ?
" <--- CURRENT" :
"");
4975 raw_printf(p->
out,
"-----------------------------------\n");
4980 if( c==
'v' && strncmp(azArg[0],
"vfsname", n)==0 ){
4981 const char *zDbName = nArg==2 ? azArg[1] :
"main";
4992 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE) 4993 if( c==
'w' && strncmp(azArg[0],
"wheretrace", n)==0 ){
4994 sqlite3WhereTrace = nArg>=2 ?
booleanValue(azArg[1]) : 0xff;
4998 if( c==
'w' && strncmp(azArg[0],
"width", n)==0 ){
5007 utf8_printf(stderr,
"Error: unknown command or invalid arguments: " 5008 " \"%s\". Enter \".help\" for help\n", azArg[0]);
5026 for(i=0; i<N; i++){
if( z[i]==
';' )
return 1; }
5036 if( *z==
'/' && z[1]==
'*' ){
5038 while( *z && (*z!=
'*' || z[1]!=
'/') ){ z++; }
5039 if( *z==0 )
return 0;
5043 if( *z==
'-' && z[1]==
'-' ){
5045 while( *z && *z!=
'\n' ){ z++; }
5046 if( *z==0 )
return 1;
5060 while(
IsSpace(zLine[0]) ){ zLine++; };
5077 if( zSql==0 )
return 1;
5107 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
5112 if( in==0 && stdin_is_interactive ) printf(
"\n");
5115 if( seenInterrupt ){
5121 if( p->
echoOn ) printf(
"%s\n", zLine);
5124 if( zLine && zLine[0]==
'.' && nSql==0 ){
5125 if( p->
echoOn ) printf(
"%s\n", zLine);
5135 memcpy(zLine,
";",2);
5138 if( nSql+nLine+2>=nAlloc ){
5139 nAlloc = nSql+nLine+100;
5140 zSql = realloc(zSql, nAlloc);
5142 raw_printf(stderr,
"Error: out of memory\n");
5149 for(i=0; zLine[i] &&
IsSpace(zLine[i]); i++){}
5150 assert( nAlloc>0 && zSql!=0 );
5151 memcpy(zSql, zLine+i, nLine+1-i);
5155 zSql[nSql++] =
'\n';
5156 memcpy(zSql+nSql, zLine, nLine+1);
5167 if( rc || zErrMsg ){
5169 if( in!=0 || !stdin_is_interactive ){
5171 "Error: near line %d:", startline);
5193 if( p->
echoOn ) printf(
"%s\n", zSql);
5199 utf8_printf(stderr,
"Error: incomplete SQL: %s\n", zSql);
5213 static char *home_dir = NULL;
5219 if( home_dir )
return home_dir;
5221 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ 5222 && !defined(__RTP__) && !defined(_WRS_KERNEL) 5224 struct passwd *pwent;
5225 uid_t uid = getuid();
5226 if( (pwent=getpwuid(uid)) != NULL) {
5227 home_dir = pwent->pw_dir;
5232 #if defined(_WIN32_WCE) 5238 #if defined(_WIN32) || defined(WIN32) 5240 home_dir = getenv(
"USERPROFILE");
5245 home_dir = getenv(
"HOME");
5248 #if defined(_WIN32) || defined(WIN32) 5250 char *zDrive, *zPath;
5252 zDrive = getenv(
"HOMEDRIVE");
5253 zPath = getenv(
"HOMEPATH");
5254 if( zDrive && zPath ){
5256 home_dir = malloc( n );
5257 if( home_dir==0 )
return 0;
5269 char *
z = malloc( n );
5270 if( z ) memcpy(z, home_dir, n);
5285 const char *sqliterc_override
5287 char *home_dir = NULL;
5288 const char *sqliterc = sqliterc_override;
5292 if (sqliterc == NULL) {
5295 raw_printf(stderr,
"-- warning: cannot find home directory;" 5296 " cannot read ~/.sqliterc\n");
5303 in = fopen(sqliterc,
"rb");
5305 if( stdin_is_interactive ){
5306 utf8_printf(stderr,
"-- Loading resources from %s\n",sqliterc);
5318 " -ascii set output mode to 'ascii'\n" 5319 " -bail stop after hitting an error\n" 5320 " -batch force batch I/O\n" 5321 " -column set output mode to 'column'\n" 5322 " -cmd COMMAND run \"COMMAND\" before reading stdin\n" 5323 " -csv set output mode to 'csv'\n" 5324 " -echo print commands before execution\n" 5325 " -init FILENAME read/process named file\n" 5326 " -[no]header turn headers on or off\n" 5327 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 5328 " -heap SIZE Size of heap for memsys3 or memsys5\n" 5330 " -help show this message\n" 5331 " -html set output mode to HTML\n" 5332 " -interactive force interactive I/O\n" 5333 " -line set output mode to 'line'\n" 5334 " -list set output mode to 'list'\n" 5335 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" 5336 " -mmap N default mmap size set to N\n" 5337 #ifdef SQLITE_ENABLE_MULTIPLEX 5338 " -multiplex enable the multiplexor VFS\n" 5340 " -newline SEP set output row separator. Default: '\\n'\n" 5341 " -nullvalue TEXT set text string for NULL values. Default ''\n" 5342 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" 5343 " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n" 5344 " -separator SEP set output column separator. Default: '|'\n" 5345 " -stats print memory stats before each finalize\n" 5346 " -version show SQLite version\n" 5347 " -vfs NAME use NAME as the default VFS\n" 5348 #ifdef SQLITE_ENABLE_VFSTRACE 5349 " -vfstrace enable tracing of all VFS calls\n" 5354 "Usage: %s [OPTIONS] FILENAME [SQL]\n" 5355 "FILENAME is the name of an SQLite database. A new database is created\n" 5356 "if the file does not previously exist.\n", Argv0);
5358 utf8_printf(stderr,
"OPTIONS include:\n%s", zOptions);
5360 raw_printf(stderr,
"Use the -help option for additional information\n");
5369 memset(data, 0,
sizeof(*data));
5387 static void printBold(
const char *zText){
5388 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
5389 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
5390 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
5391 SetConsoleTextAttribute(out,
5392 FOREGROUND_RED|FOREGROUND_INTENSITY
5394 printf(
"%s", zText);
5395 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
5399 printf(
"\033[1m%s\033[0m", zText);
5409 utf8_printf(stderr,
"%s: Error: missing argument to %s\n",
5410 argv[0], argv[argc-1]);
5416 #ifndef SQLITE_SHELL_IS_UTF8 5417 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER) 5418 # define SQLITE_SHELL_IS_UTF8 (0) 5420 # define SQLITE_SHELL_IS_UTF8 (1) 5424 #if SQLITE_SHELL_IS_UTF8 5432 const char *zInitFile = 0;
5435 int warnInmemoryDb = 0;
5441 setvbuf(stderr, 0, _IONBF, 0);
5442 stdin_is_interactive =
isatty(0);
5443 stdout_is_console =
isatty(1);
5445 #if USE_SYSTEM_SQLITE+0!=1 5447 utf8_printf(stderr,
"SQLite header and source version mismatch\n%s\n%s\n",
5453 #if !SQLITE_SHELL_IS_UTF8 5460 for(i=0; i<argc; i++){
5461 argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
5468 assert( argc>=1 && argv && argv[0] );
5475 signal(SIGINT, interrupt_handler);
5478 #ifdef SQLITE_SHELL_DBNAME_PROC 5484 extern void SQLITE_SHELL_DBNAME_PROC(
const char**);
5495 for(i=1; i<argc; i++){
5506 azCmd = realloc(azCmd,
sizeof(azCmd[0])*nCmd);
5514 if( z[1]==
'-' ) z++;
5515 if( strcmp(z,
"-separator")==0
5516 || strcmp(z,
"-nullvalue")==0
5517 || strcmp(z,
"-newline")==0
5518 || strcmp(z,
"-cmd")==0
5521 }
else if( strcmp(z,
"-init")==0 ){
5523 }
else if( strcmp(z,
"-batch")==0 ){
5528 stdin_is_interactive = 0;
5529 }
else if( strcmp(z,
"-heap")==0 ){
5530 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) 5536 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
5541 }
else if( strcmp(z,
"-scratch")==0 ){
5544 if( sz>400000 ) sz = 400000;
5545 if( sz<2500 ) sz = 2500;
5551 }
else if( strcmp(z,
"-pagecache")==0 ){
5554 if( sz>70000 ) sz = 70000;
5558 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
5560 }
else if( strcmp(z,
"-lookaside")==0 ){
5568 #ifdef SQLITE_ENABLE_VFSTRACE 5569 }
else if( strcmp(z,
"-vfstrace")==0 ){
5570 extern int vfstrace_register(
5571 const char *zTraceName,
5572 const char *zOldVfsName,
5573 int (*xOut)(
const char*,
void*),
5577 vfstrace_register(
"trace",0,(
int(*)(
const char*,
void*))fputs,stderr,1);
5579 #ifdef SQLITE_ENABLE_MULTIPLEX 5580 }
else if( strcmp(z,
"-multiplex")==0 ){
5581 extern int sqlite3_multiple_initialize(
const char*,
int);
5582 sqlite3_multiplex_initialize(0, 1);
5584 }
else if( strcmp(z,
"-mmap")==0 ){
5587 }
else if( strcmp(z,
"-vfs")==0 ){
5592 utf8_printf(stderr,
"no such VFS: \"%s\"\n", argv[i]);
5598 #ifndef SQLITE_OMIT_MEMORYDB 5600 warnInmemoryDb = argc==1;
5602 utf8_printf(stderr,
"%s: Error: no database filename specified\n", Argv0);
5628 for(i=1; i<argc; i++){
5630 if( z[0]!=
'-' )
continue;
5631 if( z[1]==
'-' ){ z++; }
5632 if( strcmp(z,
"-init")==0 ){
5634 }
else if( strcmp(z,
"-html")==0 ){
5636 }
else if( strcmp(z,
"-list")==0 ){
5638 }
else if( strcmp(z,
"-line")==0 ){
5640 }
else if( strcmp(z,
"-column")==0 ){
5642 }
else if( strcmp(z,
"-csv")==0 ){
5645 }
else if( strcmp(z,
"-ascii")==0 ){
5651 }
else if( strcmp(z,
"-separator")==0 ){
5654 }
else if( strcmp(z,
"-newline")==0 ){
5657 }
else if( strcmp(z,
"-nullvalue")==0 ){
5660 }
else if( strcmp(z,
"-header")==0 ){
5662 }
else if( strcmp(z,
"-noheader")==0 ){
5664 }
else if( strcmp(z,
"-echo")==0 ){
5666 }
else if( strcmp(z,
"-eqp")==0 ){
5668 }
else if( strcmp(z,
"-eqpfull")==0 ){
5670 }
else if( strcmp(z,
"-stats")==0 ){
5672 }
else if( strcmp(z,
"-scanstats")==0 ){
5674 }
else if( strcmp(z,
"-backslash")==0 ){
5681 }
else if( strcmp(z,
"-bail")==0 ){
5683 }
else if( strcmp(z,
"-version")==0 ){
5686 }
else if( strcmp(z,
"-interactive")==0 ){
5687 stdin_is_interactive = 1;
5688 }
else if( strcmp(z,
"-batch")==0 ){
5689 stdin_is_interactive = 0;
5690 }
else if( strcmp(z,
"-heap")==0 ){
5692 }
else if( strcmp(z,
"-scratch")==0 ){
5694 }
else if( strcmp(z,
"-pagecache")==0 ){
5696 }
else if( strcmp(z,
"-lookaside")==0 ){
5698 }
else if( strcmp(z,
"-mmap")==0 ){
5700 }
else if( strcmp(z,
"-vfs")==0 ){
5702 #ifdef SQLITE_ENABLE_VFSTRACE 5703 }
else if( strcmp(z,
"-vfstrace")==0 ){
5706 #ifdef SQLITE_ENABLE_MULTIPLEX 5707 }
else if( strcmp(z,
"-multiplex")==0 ){
5710 }
else if( strcmp(z,
"-help")==0 ){
5712 }
else if( strcmp(z,
"-cmd")==0 ){
5717 if( i==argc-1 )
break;
5721 if( rc && bail_on_error )
return rc==2 ? 0 : rc;
5727 if( bail_on_error )
return rc!=0 ? rc : 1;
5729 utf8_printf(stderr,
"Error: unable to process SQL \"%s\"\n", z);
5730 if( bail_on_error )
return rc;
5734 utf8_printf(stderr,
"%s: Error: unknown option: %s\n", Argv0, z);
5735 raw_printf(stderr,
"Use -help for a list of options.\n");
5746 for(i=0; i<nCmd; i++){
5747 if( azCmd[i][0]==
'.' ){
5749 if( rc )
return rc==2 ? 0 : rc;
5755 return rc!=0 ? rc : 1;
5757 utf8_printf(stderr,
"Error: unable to process SQL: %s\n", azCmd[i]);
5766 if( stdin_is_interactive ){
5771 "SQLite version %s %.19s\n" 5772 "Enter \".help\" for usage hints.\n",
5775 if( warnInmemoryDb ){
5776 printf(
"Connected to a ");
5777 printBold(
"transient in-memory database");
5778 printf(
".\nUse \".open FILENAME\" to reopen on a " 5779 "persistent database.\n");
5784 if( (zHistory = malloc(nHistory))!=0 ){
5806 #if !SQLITE_SHELL_IS_UTF8 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p)
#define UNUSED_PARAMETER(x)
#define sqlite3_total_changes
char outfile[FILENAME_MAX]
#define SQLITE_DBSTATUS_CACHE_HIT
static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol)
int(* xRead)(sqlite3_file *, void *, int iAmt, sqlite3_int64 iOfst)
static int shellDatabaseError(sqlite3 *db)
#define SQLITE_STMTSTATUS_AUTOINDEX
static int shellAuth(void *pClientData, int op, const char *zA1, const char *zA2, const char *zA3, const char *zA4)
#define SQLITE_STATUS_PAGECACHE_SIZE
#define SQLITE_LIMIT_ATTACHED
int(* xCurrentTime)(sqlite3_vfs *, double *)
static FILE * output_file_open(const char *zFile)
#define sqlite3_set_authorizer
#define SQLITE_DBSTATUS_CACHE_WRITE
static int hexDigitValue(char c)
static int shellNomemError(void)
#define sqlite3_get_autocommit
#define SQLITE_DBSTATUS_LOOKASIDE_HIT
static const char zOptions[]
static int sql_trace_callback(unsigned mType, void *pArg, void *pP, void *pX)
static void main_init(ShellState *data)
static char * find_home_dir(int clearFlag)
static void display_scanstats(sqlite3 *db, ShellState *pArg)
#define SQLITE_TESTCTRL_ISKEYWORD
#define SQLITE_SCANSTAT_NLOOP
#define shell_read_history(X)
#define SQLITE_STATUS_MALLOC_SIZE
#define SQLITE_STMTSTATUS_SORT
static void disable_debug_trace_modes(void)
static void open_db(ShellState *p, int keepAlive)
#define sqlite3_backup_finish
#define sqlite3_value_text
static const char * zShellStatic
#define sqlite3_backup_init
#define sqlite3_column_name
int SQLITE_CDECL main(int argc, char **argv)
SQLITE_API int sqlite3_config(int,...)
static void exec_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt, int(*xCallback)(void *, int, char **, char **, int *))
#define SQLITE_TESTCTRL_ASSERT
static unsigned int get2byteInt(unsigned char *a)
#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
#define SQLITE_CONFIG_PAGECACHE
static const char needCsvQuote[]
static void writefileFunc(sqlite3_context *context, int argc, sqlite3_value **argv)
#define SQLITE_DBSTATUS_STMT_USED
#define SQLITE_TESTCTRL_RESERVE
#define sqlite3_result_int64
#define SQLITE_LIMIT_VARIABLE_NUMBER
static int line_is_complete(char *zSql, int nSql)
static void output_reset(ShellState *p)
static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType)
static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql)
#define sqlite3_realloc64
#define sqlite3_create_function
static int stdout_is_console
#define session_close_all(X)
static char * readFile(const char *zName)
#define sqlite3_column_int64
#define SQLITE_TESTCTRL_PRNG_SAVE
static double timeDiff(struct timeval *pStart, struct timeval *pEnd)
#define setBinaryMode(X, Y)
static volatile int seenInterrupt
static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p)
#define SQLITE_STATUS_SCRATCH_OVERFLOW
#define SQLITE_LIMIT_COLUMN
static void explain_data_delete(ShellState *p)
#define SQLITE_DBSTATUS_LOOKASIDE_USED
static void set_table_name(ShellState *p, const char *zName)
#define SQLITE_CONFIG_SCRATCH
SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff)
#define sqlite3_column_int
#define SQLITE_TESTCTRL_LAST
int shellDeleteFile(const char *zFilename)
static int optionMatch(const char *zStr, const char *zOpt)
#define SQLITE_CONFIG_HEAP
#define sqlite3_column_text
static void output_quoted_string(FILE *out, const char *z)
#define SQLITE_LIMIT_COMPOUND_SELECT
static unsigned int get4byteInt(unsigned char *a)
static int isNumber(const char *z, int *realnum)
#define SQLITE_TESTCTRL_PRNG_RESTORE
static int line_contains_semicolon(const char *z, int N)
#define SQLITE_LIMIT_LENGTH
struct sqlite3_stmt sqlite3_stmt
#define SQLITE_TESTCTRL_BYTEORDER
static char * save_err_msg(sqlite3 *db)
static int strlen30(const char *z)
#define SQLITE_CONFIG_URI
#define SQLITE_STATUS_PARSER_STACK
#define SQLITE_TESTCTRL_SCRATCHMALLOC
#define sqlite3_bind_int64
#define SQLITE_CONFIG_LOOKASIDE
static void endTimer(void)
static struct rusage sBegin
static int shell_exec(sqlite3 *db, const char *zSql, int(*xCallback)(void *, int, char **, char **, int *), ShellState *pArg, char **pzErrMsg)
#define SQLITE_FCNTL_VFSNAME
#define SQLITE_LIMIT_WORKER_THREADS
sqlite_int64 sqlite3_int64
#define SQLITE_STATUS_SCRATCH_USED
#define sqlite3_bind_double
static void tryToCloneSchema(ShellState *p, sqlite3 *newDb, const char *zWhere, void(*xForEach)(ShellState *, sqlite3 *, const char *))
static void resolve_backslashes(char *z)
static sqlite3 * globalDb
#define sqlite3_libversion
static char * cmdline_option_value(int argc, char **argv, int i)
#define SQLITE_CONFIG_LOG
#define SQLITE_STMTSTATUS_VM_STEP
#define sqlite3_value_blob
SQLITE_API int sqlite3_initialize(void)
static int testcase_glob(const char *zGlob, const char *z)
static void tryToCloneData(ShellState *p, sqlite3 *newDb, const char *zTable)
#define sqlite3_bind_null
#define sqlite3_result_text
#define SQLITE_DBSTATUS_CACHE_USED
#define SQLITE_DBSTATUS_SCHEMA_USED
#define SQLITE_TESTCTRL_BITVEC_TEST
#define SQLITE_SCANSTAT_NVISIT
static int line_is_command_terminator(const char *zLine)
static char mainPrompt[20]
int(* xCurrentTimeInt64)(sqlite3_vfs *, sqlite3_int64 *)
#define SQLITE_CONFIG_MMAP_SIZE
static void beginTimer(void)
static int run_table_dump_query(ShellState *p, const char *zSelect, const char *zFirstRow)
TFSIMD_FORCE_INLINE const tfScalar & x() const
#define sqlite3_column_count
static int _all_whitespace(const char *z)
static int stdin_is_interactive
#define sqlite3_interrupt
#define sqlite3_column_type
#define sqlite3_bind_blob
#define sqlite3_column_double
#define shell_write_history(X)
#define sqlite3_db_status
static char * one_input_line(FILE *in, char *zPrior, int isContinuation)
#define SQLITE_CONFIG_MULTITHREAD
static void output_file_close(FILE *f)
static char * appendText(char *zIn, char const *zAppend, char quote)
const struct sqlite3_io_methods * pMethods
static void import_append_char(ImportCtx *p, int c)
TFSIMD_FORCE_INLINE const tfScalar & z() const
#define SQLITE_STATUS_MALLOC_COUNT
FILE * popen(const char *, const char *)
SQLITE_API int sqlite3_stmt_scanstatus(sqlite3_stmt *pStmt, int idx, int iScanStatusOp, void *pOut)
#define SQLITE_TESTCTRL_NEVER_CORRUPT
static int process_input(ShellState *p, FILE *in)
#define SQLITE_FCNTL_FILE_POINTER
static void output_csv(ShellState *p, const char *z, int bSep)
#define sqlite3_prepare_v2
#define shell_stifle_history(X)
#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS
long long int sqlite_int64
#define sqlite3_test_control
#define SQLITE_TRACE_STMT
TFSIMD_FORCE_INLINE const tfScalar & w() const
#define SQLITE_TESTCTRL_PENDING_BYTE
#define SQLITE_TESTCTRL_FIRST
static void shellLog(void *pArg, int iErrCode, const char *zMsg)
#define sqlite3_extended_errcode
#define SQLITE_FCNTL_VFS_POINTER
static int run_schema_dump_query(ShellState *p, const char *zQuery)
#define sqlite3_result_blob
static void readfileFunc(sqlite3_context *context, int argc, sqlite3_value **argv)
#define SQLITE_STATUS_MEMORY_USED
static int booleanValue(char *zArg)
#define sqlite3_load_extension
#define SQLITE_STATUS_SCRATCH_SIZE
#define sqlite3_vfs_register
#define SQLITE_SCANSTAT_EXPLAIN
#define SQLITE_LIMIT_TRIGGER_DEPTH
static int do_meta_command(char *zLine, ShellState *p)
#define sqlite3_busy_timeout
static const char * modeDescr[]
static void process_sqliterc(ShellState *p, const char *sqliterc_override)
#define sqlite3_stmt_status
static void output_c_string(FILE *out, const char *z)
static int str_in_array(const char *zStr, const char **azArray)
static void test_breakpoint(void)
static void shellstaticFunc(sqlite3_context *context, int argc, sqlite3_value **argv)
static int db_int(ShellState *p, const char *zSql)
static void tryToClone(ShellState *p, const char *zNewDb)
#define SQLITE_LIMIT_EXPR_DEPTH
#define SQLITE_STATUS_PAGECACHE_USED
#define SQLITE_TESTCTRL_ALWAYS
#define sqlite3_column_bytes
static void output_hex_blob(FILE *out, const void *pBlob, int nBlob)
#define sqlite3_backup_step
#define SQLITE_STATUS_PAGECACHE_OVERFLOW
#define SQLITE_TESTCTRL_PRNG_RESET
#define sqlite3_file_control
static char continuePrompt[20]
#define SQLITE_LIMIT_FUNCTION_ARG
static void restore_debug_trace_modes(void)
#define SQLITE_STMTSTATUS_FULLSCAN_STEP
static void printBold(const char *zText)
#define SQLITE_TESTCTRL_OPTIMIZATIONS
#define sqlite3_bind_text
#define SQLITE_TESTCTRL_FAULT_INSTALL
#define SQLITE_LIMIT_SQL_LENGTH
#define SQLITE_SCANSTAT_SELECTID
static sqlite3_int64 iBegin
#define setTextMode(X, Y)
#define SQLITE_DBSTATUS_CACHE_MISS
static int display_stats(sqlite3 *db, ShellState *pArg, int bReset)
static sqlite3_int64 timeOfDay(void)
#define SQLITE_TESTCTRL_IMPOSTER
static void output_html_string(FILE *out, const char *z)
#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg)
#define SQLITE_SCANSTAT_EST
static void usage(int showDetail)
#define SQLITE_LIMIT_VDBE_OP
static int callback(void *pArg, int nArg, char **azArg, char **azCol)
#define sqlite3_value_bytes
static char * local_getline(char *zLine, FILE *in)
#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH
static sqlite3_int64 integerValue(const char *zArg)
#define sqlite3_column_blob