• Home
  • Raw
  • Download

Lines Matching +full:traces +full:- +full:db

5 ** "shell.c" file used to implement the SQLite command-line shell.
13 ** source file to help make the command-line program easier to compile.
39 ** Optionally #include a user-defined header, whereby compilation options
87 ** Enable large-file support for fopen() and friends on unix.
205 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
207 * overridden with the -batch command line option.
254 /* Return the current wall-clock time */
260 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ in timeOfDay()
261 clockVfs->xCurrentTimeInt64(clockVfs, &t); in timeOfDay()
264 clockVfs->xCurrentTime(clockVfs, &r); in timeOfDay()
285 static sqlite3_int64 iBegin; /* Wall-clock time at start */
299 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + in timeDiff()
300 (double)(pEnd->tv_sec - pStart->tv_sec); in timeDiff()
312 (iEnd - iBegin)*0.001, in endTimer()
379 return (double) ((i64End - i64Start) / 10000000.0); in timeDiff()
391 (ftWallEnd - ftWallBegin)*0.001, in endTimer()
431 ** in order to translate UTF-8 into MBCS. The following variable is
444 ** True if an interrupt (Control-C) has been received.
450 ** Out-of-memory simulator variables
454 static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
473 ** output from UTF-8 into MBCS.
502 /* Indicate out-of-memory and exit. */
515 oomRepeat--; in shellOomFault()
517 oomCounter--; in shellOomFault()
524 ** Out-Of-Memory (OOM) errors for testing purposes.
532 oomCounter--; in oomMalloc()
552 ** Write I/O traces to the following stream.
579 ** then right-justify the text. W is the width in UTF-8 characters, not
586 int aw = w<0 ? -w : w; in utf8_width_print()
599 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); in utf8_width_print()
601 utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); in utf8_width_print()
610 if( *z=='-' || *z=='+' ) z++; in isNumber()
625 if( *z=='+' || *z=='-' ) z++; in isNumber()
635 ** lower 30 bits of a 32-bit signed integer.
640 return 0x3fffffff & (int)(z2 - z); in strlen30()
708 if( fgets(&zLine[n], nLine - n, in)==0 ){ in local_getline()
717 if( n>0 && zLine[n-1]=='\n' ){ in local_getline()
718 n--; in local_getline()
719 if( n>0 && zLine[n-1]=='\r' ) n--; in local_getline()
726 ** multi-byte characterset characters into UTF-8. */ in local_getline()
779 ** Return the value of a hexadecimal digit. Return -1 if the input
783 if( c>='0' && c<='9' ) return c - '0'; in hexDigitValue()
784 if( c>='a' && c<='f' ) return c - 'a' + 10; in hexDigitValue()
785 if( c>='A' && c<='F' ) return c - 'A' + 10; in hexDigitValue()
786 return -1; in hexDigitValue()
807 if( zArg[0]=='-' ){ in integerValue()
822 v = v*10 + zArg[0] - '0'; in integerValue()
832 return isNeg? -v : v; in integerValue()
852 free(p->z); in freeText()
856 /* zIn is either a pointer to a NULL-terminated string in memory obtained
869 len = nAppend+p->n+1; in appendText()
877 if( p->z==0 || p->n+len>=p->nAlloc ){ in appendText()
878 p->nAlloc = p->nAlloc*2 + len + 20; in appendText()
879 p->z = realloc(p->z, p->nAlloc); in appendText()
880 if( p->z==0 ) shell_out_of_memory(); in appendText()
884 char *zCsr = p->z+p->n; in appendText()
891 p->n = (int)(zCsr - p->z); in appendText()
894 memcpy(p->z+p->n, zAppend, nAppend); in appendText()
895 p->n += nAppend; in appendText()
896 p->z[p->n] = '\0'; in appendText()
902 ** because it contains non-alphanumeric characters, or because it is an
922 sqlite3 *db, /* The database connection containing the vtab */ in shellFakeSchema() argument
935 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); in shellFakeSchema()
966 ** Return a fake schema for the table-valued function or eponymous virtual
979 -1, sqlite3_free); in shellModuleSchema()
990 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
1020 sqlite3 *db = sqlite3_context_db_handle(pCtx); in shellAddSchemaName() local
1038 && (zFake = shellFakeSchema(db, zSchema, zName))!=0 in shellAddSchemaName()
1048 sqlite3_result_text(pCtx, z, -1, sqlite3_free); in shellAddSchemaName()
1058 ** The source code for several run-time loadable extensions is inserted
1169 # define BAD_INTPTR_T ((intptr_t)(-1))
1211 ** function. This function is not thread-safe.
1250 ** This function is not thread-safe.
1263 ** The function call to GetEnvironmentVariableA() failed -OR- in windirent_getenv()
1270 ** -AND- the buffer contains the entire value. in windirent_getenv()
1289 /* TODO: Remove this if Unix-style root paths are not used. */ in opendir()
1296 dirp->d_handle = _findfirst(data.name, &data); in opendir()
1298 if( dirp->d_handle==BAD_INTPTR_T ){ in opendir()
1308 if( _findnext(dirp->d_handle, &data)==-1 ){ in opendir()
1317 dirp->d_first.d_attributes = data.attrib; in opendir()
1318 strncpy(dirp->d_first.d_name, data.name, NAME_MAX); in opendir()
1319 dirp->d_first.d_name[NAME_MAX] = '\0'; in opendir()
1334 if( dirp->d_first.d_ino==0 ){ in readdir()
1335 dirp->d_first.d_ino++; in readdir()
1336 dirp->d_next.d_ino++; in readdir()
1338 return &dirp->d_first; in readdir()
1344 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL; in readdir()
1349 dirp->d_next.d_ino++; in readdir()
1350 dirp->d_next.d_attributes = data.attrib; in readdir()
1351 strncpy(dirp->d_next.d_name, data.name, NAME_MAX); in readdir()
1352 dirp->d_next.d_name[NAME_MAX] = '\0'; in readdir()
1354 return &dirp->d_next; in readdir()
1369 if( dirp->d_first.d_ino==0 ){ in readdir_r()
1370 dirp->d_first.d_ino++; in readdir_r()
1371 dirp->d_next.d_ino++; in readdir_r()
1373 entry->d_ino = dirp->d_first.d_ino; in readdir_r()
1374 entry->d_attributes = dirp->d_first.d_attributes; in readdir_r()
1375 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX); in readdir_r()
1376 entry->d_name[NAME_MAX] = '\0'; in readdir_r()
1385 if( _findnext(dirp->d_handle, &data)==-1 ){ in readdir_r()
1393 entry->d_ino = (ino_t)-1; /* not available */ in readdir_r()
1394 entry->d_attributes = data.attrib; in readdir_r()
1395 strncpy(entry->d_name, data.name, NAME_MAX); in readdir_r()
1396 entry->d_name[NAME_MAX] = '\0'; in readdir_r()
1412 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){ in closedir()
1413 result = _findclose(dirp->d_handle); in closedir()
1427 ** 2017-03-08
1450 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
1469 ** and whether or not that determination is run-time or compile-time.
1471 ** For best performance, an attempt is made to guess at the byte-order
1472 ** using C-preprocessor macros. If that is unsuccessful, or if
1473 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1474 ** at run-time.
1505 ** A single step of the Keccak mixing function for a 1600-bit state
1526 # define a00 (p->u.s[0]) in KeccakF1600Step()
1527 # define a01 (p->u.s[1]) in KeccakF1600Step()
1528 # define a02 (p->u.s[2]) in KeccakF1600Step()
1529 # define a03 (p->u.s[3]) in KeccakF1600Step()
1530 # define a04 (p->u.s[4]) in KeccakF1600Step()
1531 # define a10 (p->u.s[5]) in KeccakF1600Step()
1532 # define a11 (p->u.s[6]) in KeccakF1600Step()
1533 # define a12 (p->u.s[7]) in KeccakF1600Step()
1534 # define a13 (p->u.s[8]) in KeccakF1600Step()
1535 # define a14 (p->u.s[9]) in KeccakF1600Step()
1536 # define a20 (p->u.s[10]) in KeccakF1600Step()
1537 # define a21 (p->u.s[11]) in KeccakF1600Step()
1538 # define a22 (p->u.s[12]) in KeccakF1600Step()
1539 # define a23 (p->u.s[13]) in KeccakF1600Step()
1540 # define a24 (p->u.s[14]) in KeccakF1600Step()
1541 # define a30 (p->u.s[15]) in KeccakF1600Step()
1542 # define a31 (p->u.s[16]) in KeccakF1600Step()
1543 # define a32 (p->u.s[17]) in KeccakF1600Step()
1544 # define a33 (p->u.s[18]) in KeccakF1600Step()
1545 # define a34 (p->u.s[19]) in KeccakF1600Step()
1546 # define a40 (p->u.s[20]) in KeccakF1600Step()
1547 # define a41 (p->u.s[21]) in KeccakF1600Step()
1548 # define a42 (p->u.s[22]) in KeccakF1600Step()
1549 # define a43 (p->u.s[23]) in KeccakF1600Step()
1550 # define a44 (p->u.s[24]) in KeccakF1600Step()
1551 # define ROL64(a,x) ((a<<x)|(a>>(64-x))) in KeccakF1600Step()
1832 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; in SHA3Init()
1834 p->nRate = (1600 - 2*256)/8; in SHA3Init()
1837 /* Known to be little-endian at compile-time. No-op */ in SHA3Init()
1839 p->ixMask = 7; /* Big-endian */ in SHA3Init()
1845 p->ixMask = 0; in SHA3Init()
1848 p->ixMask = 7; in SHA3Init()
1865 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ in SHA3Update()
1867 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; in SHA3Update()
1868 p->nLoaded += 8; in SHA3Update()
1869 if( p->nLoaded>=p->nRate ){ in SHA3Update()
1871 p->nLoaded = 0; in SHA3Update()
1878 p->u.x[p->nLoaded] ^= aData[i]; in SHA3Update()
1880 p->u.x[p->nLoaded^0x07] ^= aData[i]; in SHA3Update()
1882 p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; in SHA3Update()
1884 p->nLoaded++; in SHA3Update()
1885 if( p->nLoaded==p->nRate ){ in SHA3Update()
1887 p->nLoaded = 0; in SHA3Update()
1899 if( p->nLoaded==p->nRate-1 ){ in SHA3Final()
1906 p->nLoaded = p->nRate - 1; in SHA3Final()
1909 for(i=0; i<p->nRate; i++){ in SHA3Final()
1910 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; in SHA3Final()
1912 return &p->u.x[p->nRate]; in SHA3Final()
1920 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1922 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1941 "384 512", -1); in sha3Func()
1977 ** argument. The results are hashed using a SIZE-bit SHA3. The default
1986 ** F<ieee-float>
1991 ** the size of that text. The SQL text is UTF-8. A single R character
1993 ** I mean an 8-byte little-endian integer <int>. F is a floating point
1994 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1996 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
2010 sqlite3 *db = sqlite3_context_db_handle(context); in sha3QueryFunc() local
2027 "384 512", -1); in sha3QueryFunc()
2034 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); in sha3QueryFunc()
2037 zSql, sqlite3_errmsg(db)); in sha3QueryFunc()
2039 sqlite3_result_error(context, zMsg, -1); in sha3QueryFunc()
2044 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); in sha3QueryFunc()
2046 sqlite3_result_error(context, zMsg, -1); in sha3QueryFunc()
2073 for(j=8; j>=1; j--){ in sha3QueryFunc()
2087 for(j=8; j>=1; j--){ in sha3QueryFunc()
2122 sqlite3 *db, in sqlite3_shathree_init() argument
2129 rc = sqlite3_create_function(db, "sha3", 1, in sqlite3_shathree_init()
2133 rc = sqlite3_create_function(db, "sha3", 2, in sqlite3_shathree_init()
2138 rc = sqlite3_create_function(db, "sha3_query", 1, in sqlite3_shathree_init()
2143 rc = sqlite3_create_function(db, "sha3_query", 2, in sqlite3_shathree_init()
2153 ** 2014-06-13
2173 ** If the first option argument - MODE - is present, then it must
2190 ** as an integer - the number of seconds since the unix epoch. The
2191 ** modification-time of the target file is set to this value before
2222 ** If a non-NULL value is specified for the optional $dir parameter and
2229 ** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
2266 ** Structure of the fsdir() table-valued function
2293 sqlite3 *db; in readFileContents() local
2304 db = sqlite3_context_db_handle(ctx); in readFileContents()
2305 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1); in readFileContents()
2352 sqlite3_result_error(ctx, zMsg, -1); in ctxErrorMsg()
2360 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2378 fileIntervals.LowPart = pFileTime->dwLowDateTime; in fileTimeToUnixTime()
2379 fileIntervals.HighPart = pFileTime->dwHighDateTime; in fileTimeToUnixTime()
2381 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000; in fileTimeToUnixTime()
2391 int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0); in utf8_to_utf16()
2393 if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) ) in utf8_to_utf16()
2418 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime); in statTimesToUtc()
2419 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime); in statTimesToUtc()
2420 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime); in statTimesToUtc()
2520 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */ in writeFile()
2532 ** be an error though - if there is already a directory at the same in writeFile()
2637 sqlite3_int64 mtime = -1; in writefileFunc()
2641 "wrong number of arguments to function writefile()", -1 in writefileFunc()
2676 ** Given a numberic st_mode from stat(), convert it into a human-readable
2677 ** text string in the style of "ls -l".
2691 z[0] = '-'; in lsModeFunc()
2698 int m = (iMode >> ((2-i)*3)); in lsModeFunc()
2700 a[0] = (m & 0x4) ? 'r' : '-'; in lsModeFunc()
2701 a[1] = (m & 0x2) ? 'w' : '-'; in lsModeFunc()
2702 a[2] = (m & 0x1) ? 'x' : '-'; in lsModeFunc()
2705 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); in lsModeFunc()
2718 char *zDir; /* Name of directory (nul-terminated) */
2722 sqlite3_vtab_cursor base; /* Base class - must be first */
2738 sqlite3_vtab base; /* Base class - must be first */
2745 sqlite3 *db, in fsdirConnect() argument
2757 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA); in fsdirConnect()
2762 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); in fsdirConnect()
2785 pCur->iLvl = -1; in fsdirOpen()
2786 *ppCursor = &pCur->base; in fsdirOpen()
2796 for(i=0; i<=pCur->iLvl; i++){ in fsdirResetCursor()
2797 FsdirLevel *pLvl = &pCur->aLvl[i]; in fsdirResetCursor()
2798 if( pLvl->pDir ) closedir(pLvl->pDir); in fsdirResetCursor()
2799 sqlite3_free(pLvl->zDir); in fsdirResetCursor()
2801 sqlite3_free(pCur->zPath); in fsdirResetCursor()
2802 sqlite3_free(pCur->aLvl); in fsdirResetCursor()
2803 pCur->aLvl = 0; in fsdirResetCursor()
2804 pCur->zPath = 0; in fsdirResetCursor()
2805 pCur->zBase = 0; in fsdirResetCursor()
2806 pCur->nBase = 0; in fsdirResetCursor()
2807 pCur->nLvl = 0; in fsdirResetCursor()
2808 pCur->iLvl = -1; in fsdirResetCursor()
2809 pCur->iRowid = 1; in fsdirResetCursor()
2830 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); in fsdirSetErrmsg()
2840 mode_t m = pCur->sStat.st_mode; in fsdirNext()
2842 pCur->iRowid++; in fsdirNext()
2845 int iNew = pCur->iLvl + 1; in fsdirNext()
2847 if( iNew>=pCur->nLvl ){ in fsdirNext()
2850 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte); in fsdirNext()
2852 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl)); in fsdirNext()
2853 pCur->aLvl = aNew; in fsdirNext()
2854 pCur->nLvl = nNew; in fsdirNext()
2856 pCur->iLvl = iNew; in fsdirNext()
2857 pLvl = &pCur->aLvl[iNew]; in fsdirNext()
2859 pLvl->zDir = pCur->zPath; in fsdirNext()
2860 pCur->zPath = 0; in fsdirNext()
2861 pLvl->pDir = opendir(pLvl->zDir); in fsdirNext()
2862 if( pLvl->pDir==0 ){ in fsdirNext()
2863 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath); in fsdirNext()
2868 while( pCur->iLvl>=0 ){ in fsdirNext()
2869 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl]; in fsdirNext()
2870 struct dirent *pEntry = readdir(pLvl->pDir); in fsdirNext()
2872 if( pEntry->d_name[0]=='.' ){ in fsdirNext()
2873 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue; in fsdirNext()
2874 if( pEntry->d_name[1]=='\0' ) continue; in fsdirNext()
2876 sqlite3_free(pCur->zPath); in fsdirNext()
2877 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name); in fsdirNext()
2878 if( pCur->zPath==0 ) return SQLITE_NOMEM; in fsdirNext()
2879 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ in fsdirNext()
2880 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); in fsdirNext()
2885 closedir(pLvl->pDir); in fsdirNext()
2886 sqlite3_free(pLvl->zDir); in fsdirNext()
2887 pLvl->pDir = 0; in fsdirNext()
2888 pLvl->zDir = 0; in fsdirNext()
2889 pCur->iLvl--; in fsdirNext()
2893 sqlite3_free(pCur->zPath); in fsdirNext()
2894 pCur->zPath = 0; in fsdirNext()
2910 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT); in fsdirColumn()
2915 sqlite3_result_int64(ctx, pCur->sStat.st_mode); in fsdirColumn()
2919 sqlite3_result_int64(ctx, pCur->sStat.st_mtime); in fsdirColumn()
2923 mode_t m = pCur->sStat.st_mode; in fsdirColumn()
2934 n = readlink(pCur->zPath, aBuf, nBuf); in fsdirColumn()
2949 readFileContents(ctx, pCur->zPath); in fsdirColumn()
2969 *pRowid = pCur->iRowid; in fsdirRowid()
2979 return (pCur->zPath==0); in fsdirEof()
3006 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument"); in fsdirFilter()
3010 pCur->zBase = (const char*)sqlite3_value_text(argv[1]); in fsdirFilter()
3012 if( pCur->zBase ){ in fsdirFilter()
3013 pCur->nBase = (int)strlen(pCur->zBase)+1; in fsdirFilter()
3014 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir); in fsdirFilter()
3016 pCur->zPath = sqlite3_mprintf("%s", zDir); in fsdirFilter()
3019 if( pCur->zPath==0 ){ in fsdirFilter()
3022 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ in fsdirFilter()
3023 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); in fsdirFilter()
3049 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */ in fsdirBestIndex()
3050 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */ in fsdirBestIndex()
3056 pConstraint = pIdxInfo->aConstraint; in fsdirBestIndex()
3057 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ in fsdirBestIndex()
3058 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; in fsdirBestIndex()
3059 switch( pConstraint->iColumn ){ in fsdirBestIndex()
3061 if( pConstraint->usable ){ in fsdirBestIndex()
3070 if( pConstraint->usable ){ in fsdirBestIndex()
3086 pIdxInfo->idxNum = 0; in fsdirBestIndex()
3087 /* The pIdxInfo->estimatedCost should have been initialized to a huge in fsdirBestIndex()
3089 pIdxInfo->estimatedRows = 0x7fffffff; in fsdirBestIndex()
3091 pIdxInfo->aConstraintUsage[idxPath].omit = 1; in fsdirBestIndex()
3092 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1; in fsdirBestIndex()
3094 pIdxInfo->aConstraintUsage[idxDir].omit = 1; in fsdirBestIndex()
3095 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2; in fsdirBestIndex()
3096 pIdxInfo->idxNum = 2; in fsdirBestIndex()
3097 pIdxInfo->estimatedCost = 10.0; in fsdirBestIndex()
3099 pIdxInfo->idxNum = 1; in fsdirBestIndex()
3100 pIdxInfo->estimatedCost = 100.0; in fsdirBestIndex()
3110 static int fsdirRegister(sqlite3 *db){ in fsdirRegister() argument
3118 fsdirOpen, /* xOpen - open a cursor */ in fsdirRegister()
3119 fsdirClose, /* xClose - close a cursor */ in fsdirRegister()
3120 fsdirFilter, /* xFilter - configure scan constraints */ in fsdirRegister()
3121 fsdirNext, /* xNext - advance a cursor */ in fsdirRegister()
3122 fsdirEof, /* xEof - check for end of scan */ in fsdirRegister()
3123 fsdirColumn, /* xColumn - read data */ in fsdirRegister()
3124 fsdirRowid, /* xRowid - read data */ in fsdirRegister()
3138 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0); in fsdirRegister()
3149 sqlite3 *db, in sqlite3_fileio_init() argument
3156 rc = sqlite3_create_function(db, "readfile", 1, in sqlite3_fileio_init()
3160 rc = sqlite3_create_function(db, "writefile", -1, in sqlite3_fileio_init()
3165 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0, in sqlite3_fileio_init()
3169 rc = fsdirRegister(db); in sqlite3_fileio_init()
3185 ** 2017-07-10
3232 sqlite3_vtab base; /* Base class - must be first */
3233 sqlite3 *db; /* Database connection for this completion vtab */ member
3242 sqlite3_vtab_cursor base; /* Base class - must be first */
3243 sqlite3 *db; /* Database connection for this cursor */ member
3252 int j; /* inter-phase counter */
3284 sqlite3 *db, in completionConnect() argument
3302 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */ in completionConnect()
3304 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); in completionConnect()
3305 rc = sqlite3_declare_vtab(db, in completionConnect()
3317 pNew->db = db; in completionConnect()
3338 pCur->db = ((completion_vtab*)p)->db; in completionOpen()
3339 *ppCursor = &pCur->base; in completionOpen()
3347 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0; in completionCursorReset()
3348 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0; in completionCursorReset()
3349 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0; in completionCursorReset()
3350 pCur->j = 0; in completionCursorReset()
3365 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3366 ** record the current state of the scan. This routine sets ->zCurrentRow
3368 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3379 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */ in completionNext()
3380 pCur->iRowid++; in completionNext()
3381 while( pCur->ePhase!=COMPLETION_EOF ){ in completionNext()
3382 switch( pCur->ePhase ){ in completionNext()
3384 if( pCur->j >= sqlite3_keyword_count() ){ in completionNext()
3385 pCur->zCurrentRow = 0; in completionNext()
3386 pCur->ePhase = COMPLETION_DATABASES; in completionNext()
3388 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow); in completionNext()
3390 iCol = -1; in completionNext()
3394 if( pCur->pStmt==0 ){ in completionNext()
3395 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, in completionNext()
3396 &pCur->pStmt, 0); in completionNext()
3403 if( pCur->pStmt==0 ){ in completionNext()
3407 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); in completionNext()
3419 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); in completionNext()
3427 if( pCur->pStmt==0 ){ in completionNext()
3431 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); in completionNext()
3445 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); in completionNext()
3455 if( pCur->zCurrentRow==0 ) continue; in completionNext()
3457 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){ in completionNext()
3459 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol); in completionNext()
3460 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol); in completionNext()
3463 sqlite3_finalize(pCur->pStmt); in completionNext()
3464 pCur->pStmt = 0; in completionNext()
3465 pCur->ePhase = eNextPhase; in completionNext()
3469 if( pCur->nPrefix==0 ) break; in completionNext()
3470 if( pCur->nPrefix<=pCur->szRow in completionNext()
3471 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 in completionNext()
3492 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT); in completionColumn()
3496 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT); in completionColumn()
3500 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT); in completionColumn()
3504 sqlite3_result_int(ctx, pCur->ePhase); in completionColumn()
3517 *pRowid = pCur->iRowid; in completionRowid()
3527 return pCur->ePhase >= COMPLETION_EOF; in completionEof()
3547 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]); in completionFilter()
3548 if( pCur->nPrefix>0 ){ in completionFilter()
3549 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); in completionFilter()
3550 if( pCur->zPrefix==0 ) return SQLITE_NOMEM; in completionFilter()
3555 pCur->nLine = sqlite3_value_bytes(argv[iArg]); in completionFilter()
3556 if( pCur->nLine>0 ){ in completionFilter()
3557 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); in completionFilter()
3558 if( pCur->zLine==0 ) return SQLITE_NOMEM; in completionFilter()
3561 if( pCur->zLine!=0 && pCur->zPrefix==0 ){ in completionFilter()
3562 int i = pCur->nLine; in completionFilter()
3563 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){ in completionFilter()
3564 i--; in completionFilter()
3566 pCur->nPrefix = pCur->nLine - i; in completionFilter()
3567 if( pCur->nPrefix>0 ){ in completionFilter()
3568 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i); in completionFilter()
3569 if( pCur->zPrefix==0 ) return SQLITE_NOMEM; in completionFilter()
3572 pCur->iRowid = 0; in completionFilter()
3573 pCur->ePhase = COMPLETION_FIRST_PHASE; in completionFilter()
3583 ** There are two hidden parameters that act as arguments to the table-valued
3593 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */ in completionBestIndex()
3594 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */ in completionBestIndex()
3599 pConstraint = pIdxInfo->aConstraint; in completionBestIndex()
3600 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ in completionBestIndex()
3601 if( pConstraint->usable==0 ) continue; in completionBestIndex()
3602 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; in completionBestIndex()
3603 switch( pConstraint->iColumn ){ in completionBestIndex()
3615 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg; in completionBestIndex()
3616 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1; in completionBestIndex()
3619 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg; in completionBestIndex()
3620 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1; in completionBestIndex()
3622 pIdxInfo->idxNum = idxNum; in completionBestIndex()
3623 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg; in completionBestIndex()
3624 pIdxInfo->estimatedRows = 500 - 100*nArg; in completionBestIndex()
3639 completionOpen, /* xOpen - open a cursor */
3640 completionClose, /* xClose - close a cursor */
3641 completionFilter, /* xFilter - configure scan constraints */
3642 completionNext, /* xNext - advance a cursor */
3643 completionEof, /* xEof - check for end of scan */
3644 completionColumn, /* xColumn - read data */
3645 completionRowid, /* xRowid - read data */
3661 int sqlite3CompletionVtabInit(sqlite3 *db){ in sqlite3CompletionVtabInit() argument
3664 rc = sqlite3_create_module(db, "completion", &completionModule, 0); in sqlite3CompletionVtabInit()
3673 sqlite3 *db, in sqlite3_completion_init() argument
3681 rc = sqlite3CompletionVtabInit(db); in sqlite3_completion_init()
3689 ** 2017-10-20
3717 ** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
3734 ** this shim is a pass-through into the default underlying VFS. (rule 3)
3743 ** Start-Of-SQLite3-NNNNNNNN
3746 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3749 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3755 ** Maximum size of the combined prefix + database + append-mark. This
3766 #define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1))
3775 /* Access to a lower-level VFS that (might) implement dynamic loading,
3778 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3787 ** append-mark.
3791 ** +-------------+---------+----------+-------------+
3792 ** | prefix-file | padding | database | append-mark |
3793 ** +-------------+---------+----------+-------------+
3799 ** "prefix file" - file onto which the database has been appended.
3800 ** "padding" - zero or more bytes inserted so that "database"
3802 ** "database" - The SQLite database file
3803 ** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
3804 ** the offset from the start of prefix-file to the start
3807 ** The size of the database is iMark - iPgOne.
3809 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
3810 ** of iPgOne stored as a big-ending 64-bit integer.
3813 ** Or, iMark is -1 to indicate that it has not yet been written.
3818 sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */
3912 ** Close an apnd-file.
3916 return pFile->pMethods->xClose(pFile); in apndClose()
3920 ** Read data from an apnd-file.
3930 return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst); in apndRead()
3934 ** Add the append-mark onto what should become the end of the file.
3936 * Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
3943 sqlite_int64 iPgOne = paf->iPgOne; in apndWriteMark()
3949 while( --i >= 0 ){ in apndWriteMark()
3953 iWriteEnd += paf->iPgOne; in apndWriteMark()
3954 if( SQLITE_OK==(rc = pFile->pMethods->xWrite in apndWriteMark()
3956 paf->iMark = iWriteEnd; in apndWriteMark()
3962 ** Write data to an apnd-file.
3974 /* If append-mark is absent or will be overwritten, write it. */ in apndWrite()
3975 if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){ in apndWrite()
3979 return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst); in apndWrite()
3983 ** Truncate an apnd-file.
3991 return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE); in apndTruncate()
3995 ** Sync an apnd-file.
3999 return pFile->pMethods->xSync(pFile, flags); in apndSync()
4003 ** Return the current file-size of an apnd-file.
4004 ** If the append mark is not yet there, the file-size is 0.
4008 *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0; in apndFileSize()
4013 ** Lock an apnd-file.
4017 return pFile->pMethods->xLock(pFile, eLock); in apndLock()
4021 ** Unlock an apnd-file.
4025 return pFile->pMethods->xUnlock(pFile, eLock); in apndUnlock()
4029 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
4033 return pFile->pMethods->xCheckReservedLock(pFile, pResOut); in apndCheckReservedLock()
4037 ** File control method. For custom operations on an apnd-file.
4043 if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne; in apndFileControl()
4044 rc = pFile->pMethods->xFileControl(pFile, op, pArg); in apndFileControl()
4046 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg); in apndFileControl()
4052 ** Return the sector-size in bytes for an apnd-file.
4056 return pFile->pMethods->xSectorSize(pFile); in apndSectorSize()
4060 ** Return the device characteristic flags supported by an apnd-file.
4064 return pFile->pMethods->xDeviceCharacteristics(pFile); in apndDeviceCharacteristics()
4076 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp); in apndShmMap()
4079 /* Perform locking on a shared-memory segment */
4082 return pFile->pMethods->xShmLock(pFile,offset,n,flags); in apndShmLock()
4088 pFile->pMethods->xShmBarrier(pFile); in apndShmBarrier()
4094 return pFile->pMethods->xShmUnmap(pFile,deleteFlag); in apndShmUnmap()
4097 /* Fetch a page of a memory-mapped file */
4105 if( p->iMark < 0 || iOfst+iAmt > p->iMark ){ in apndFetch()
4109 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp); in apndFetch()
4112 /* Release a memory-mapped page */
4116 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage); in apndUnfetch()
4120 ** Try to read the append-mark off the end of a file. Return the
4121 ** start of the appended database if the append-mark is present.
4122 ** If there is no valid append-mark, return -1;
4124 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
4126 ** start-of-database value must be a multiple of 512.
4131 int msbs = 8 * (APND_MARK_FOS_SZ-1); in apndReadMark()
4134 if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1; in apndReadMark()
4135 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE); in apndReadMark()
4136 if( rc ) return -1; in apndReadMark()
4137 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1; in apndReadMark()
4140 msbs -= 8; in apndReadMark()
4143 if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1; in apndReadMark()
4144 if( iMark & 0x1ff ) return -1; in apndReadMark()
4158 /* If file has the correct end-marker, the expected odd size, and the in apndIsAppendvfsDatabase()
4159 ** SQLite DB type marker where the end-marker puts it, then it in apndIsAppendvfsDatabase()
4162 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark); in apndIsAppendvfsDatabase()
4182 || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0) in apndIsOrdinaryDatabaseFile()
4211 return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags); in apndOpen()
4214 pFile->pMethods = &apnd_io_methods; in apndOpen()
4215 pApndFile->iMark = -1; /* Append mark not yet written */ in apndOpen()
4217 rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags); in apndOpen()
4219 rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz); in apndOpen()
4221 pBaseFile->pMethods->xClose(pBaseFile); in apndOpen()
4225 pFile->pMethods = 0; in apndOpen()
4229 /* The file being opened appears to be just an ordinary DB. Copy in apndOpen()
4230 ** the base dispatch-table so this instance mimics the base VFS. in apndOpen()
4232 memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile); in apndOpen()
4235 pApndFile->iPgOne = apndReadMark(sz, pFile); in apndOpen()
4236 if( pApndFile->iPgOne>=0 ){ in apndOpen()
4237 pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */ in apndOpen()
4241 pBaseFile->pMethods->xClose(pBaseFile); in apndOpen()
4243 pFile->pMethods = 0; in apndOpen()
4248 ** Until then, paf->iMark value indicates it is not yet written. in apndOpen()
4250 pApndFile->iPgOne = APND_START_ROUNDUP(sz); in apndOpen()
4262 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync); in apndDelete()
4266 ** All other VFS methods are pass-thrus.
4274 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut); in apndAccess()
4282 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut); in apndFullPathname()
4285 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); in apndDlOpen()
4288 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); in apndDlError()
4291 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); in apndDlSym()
4294 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); in apndDlClose()
4297 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); in apndRandomness()
4300 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); in apndSleep()
4303 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); in apndCurrentTime()
4306 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); in apndGetLastError()
4309 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); in apndCurrentTimeInt64()
4316 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall); in apndSetSystemCall()
4322 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName); in apndGetSystemCall()
4325 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName); in apndNextSystemCall()
4337 sqlite3 *db, in sqlite3_appendvfs_init() argument
4345 (void)db; in sqlite3_appendvfs_init()
4348 apnd_vfs.iVersion = pOrig->iVersion; in sqlite3_appendvfs_init()
4350 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile); in sqlite3_appendvfs_init()
4364 ** 2019-01-21
4383 ** This extension is used to implement the --memtrace option of the
4384 ** command-line shell.
4416 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n", in memtraceRealloc()
4475 ** 2020-04-14
4501 ** 64-bit machine integer.
4523 x = zA[i] - zB[j]; in uintCollFunc()
4537 return -1; in uintCollFunc()
4551 return (nKey1 - i) - (nKey2 - j); in uintCollFunc()
4558 sqlite3 *db, in sqlite3_uint_init() argument
4564 return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc); in sqlite3_uint_init()
4570 ** 2020-06-22
4581 ** Routines to implement arbitrary-precision decimal math.
4615 sqlite3_free(p->a); in decimal_clear()
4644 p->sign = 0; in decimal_new()
4645 p->oom = 0; in decimal_new()
4646 p->isInit = 1; in decimal_new()
4647 p->isNull = 0; in decimal_new()
4648 p->nDigit = 0; in decimal_new()
4649 p->nFrac = 0; in decimal_new()
4655 p->a = 0; in decimal_new()
4656 p->isNull = 1; in decimal_new()
4662 p->a = sqlite3_malloc64( n+1 ); in decimal_new()
4663 if( p->a==0 ) goto new_no_mem; in decimal_new()
4665 if( zIn[i]=='-' ){ in decimal_new()
4666 p->sign = 1; in decimal_new()
4675 p->a[p->nDigit++] = c - '0'; in decimal_new()
4677 p->nFrac = p->nDigit + 1; in decimal_new()
4682 if( zIn[j]=='-' ){ in decimal_new()
4690 iExp = iExp*10 + zIn[j] - '0'; in decimal_new()
4694 if( neg ) iExp = -iExp; in decimal_new()
4699 if( p->nFrac ){ in decimal_new()
4700 p->nFrac = p->nDigit - (p->nFrac - 1); in decimal_new()
4703 if( p->nFrac>0 ){ in decimal_new()
4704 if( iExp<=p->nFrac ){ in decimal_new()
4705 p->nFrac -= iExp; in decimal_new()
4708 iExp -= p->nFrac; in decimal_new()
4709 p->nFrac = 0; in decimal_new()
4713 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 ); in decimal_new()
4714 if( p->a==0 ) goto new_no_mem; in decimal_new()
4715 memset(p->a+p->nDigit, 0, iExp); in decimal_new()
4716 p->nDigit += iExp; in decimal_new()
4720 iExp = -iExp; in decimal_new()
4721 nExtra = p->nDigit - p->nFrac - 1; in decimal_new()
4724 p->nFrac += iExp; in decimal_new()
4727 iExp -= nExtra; in decimal_new()
4728 p->nFrac = p->nDigit - 1; in decimal_new()
4732 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 ); in decimal_new()
4733 if( p->a==0 ) goto new_no_mem; in decimal_new()
4734 memmove(p->a+iExp, p->a, p->nDigit); in decimal_new()
4735 memset(p->a, 0, iExp); in decimal_new()
4736 p->nDigit += iExp; in decimal_new()
4737 p->nFrac += iExp; in decimal_new()
4755 if( p==0 || p->oom ){ in decimal_result()
4759 if( p->isNull ){ in decimal_result()
4763 z = sqlite3_malloc( p->nDigit+4 ); in decimal_result()
4769 if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){ in decimal_result()
4770 p->sign = 0; in decimal_result()
4772 if( p->sign ){ in decimal_result()
4773 z[0] = '-'; in decimal_result()
4776 n = p->nDigit - p->nFrac; in decimal_result()
4781 while( n>1 && p->a[j]==0 ){ in decimal_result()
4783 n--; in decimal_result()
4786 z[i++] = p->a[j] + '0'; in decimal_result()
4788 n--; in decimal_result()
4790 if( p->nFrac ){ in decimal_result()
4793 z[i++] = p->a[j] + '0'; in decimal_result()
4795 }while( j<p->nDigit ); in decimal_result()
4824 ** pA->isNull==0
4826 ** pB->isNull==0
4830 if( pA->sign!=pB->sign ){ in decimal_cmp()
4831 return pA->sign ? -1 : +1; in decimal_cmp()
4833 if( pA->sign ){ in decimal_cmp()
4838 nASig = pA->nDigit - pA->nFrac; in decimal_cmp()
4839 nBSig = pB->nDigit - pB->nFrac; in decimal_cmp()
4841 return nASig - nBSig; in decimal_cmp()
4843 n = pA->nDigit; in decimal_cmp()
4844 if( n>pB->nDigit ) n = pB->nDigit; in decimal_cmp()
4845 rc = memcmp(pA->a, pB->a, n); in decimal_cmp()
4847 rc = pA->nDigit - pB->nDigit; in decimal_cmp()
4868 if( pA==0 || pA->isNull ) goto cmp_done; in decimalCmpFunc()
4870 if( pB==0 || pB->isNull ) goto cmp_done; in decimalCmpFunc()
4872 if( rc<0 ) rc = -1; in decimalCmpFunc()
4888 nAddFrac = nFrac - p->nFrac; in decimal_expand()
4889 nAddSig = (nDigit - p->nDigit) - nAddFrac; in decimal_expand()
4891 p->a = sqlite3_realloc64(p->a, nDigit+1); in decimal_expand()
4892 if( p->a==0 ){ in decimal_expand()
4893 p->oom = 1; in decimal_expand()
4897 memmove(p->a+nAddSig, p->a, p->nDigit); in decimal_expand()
4898 memset(p->a, 0, nAddSig); in decimal_expand()
4899 p->nDigit += nAddSig; in decimal_expand()
4902 memset(p->a+p->nDigit, 0, nAddFrac); in decimal_expand()
4903 p->nDigit += nAddFrac; in decimal_expand()
4904 p->nFrac += nAddFrac; in decimal_expand()
4919 if( pA->oom || pB==0 || pB->oom ){ in decimal_add()
4920 pA->oom = 1; in decimal_add()
4923 if( pA->isNull || pB->isNull ){ in decimal_add()
4924 pA->isNull = 1; in decimal_add()
4927 nSig = pA->nDigit - pA->nFrac; in decimal_add()
4928 if( nSig && pA->a[0]==0 ) nSig--; in decimal_add()
4929 if( nSig<pB->nDigit-pB->nFrac ){ in decimal_add()
4930 nSig = pB->nDigit - pB->nFrac; in decimal_add()
4932 nFrac = pA->nFrac; in decimal_add()
4933 if( nFrac<pB->nFrac ) nFrac = pB->nFrac; in decimal_add()
4937 if( pA->oom || pB->oom ){ in decimal_add()
4938 pA->oom = 1; in decimal_add()
4940 if( pA->sign==pB->sign ){ in decimal_add()
4942 for(i=nDigit-1; i>=0; i--){ in decimal_add()
4943 int x = pA->a[i] + pB->a[i] + carry; in decimal_add()
4946 pA->a[i] = x - 10; in decimal_add()
4949 pA->a[i] = x; in decimal_add()
4955 rc = memcmp(pA->a, pB->a, nDigit); in decimal_add()
4957 aA = pB->a; in decimal_add()
4958 aB = pA->a; in decimal_add()
4959 pA->sign = !pA->sign; in decimal_add()
4961 aA = pA->a; in decimal_add()
4962 aB = pB->a; in decimal_add()
4964 for(i=nDigit-1; i>=0; i--){ in decimal_add()
4965 int x = aA[i] - aB[i] - borrow; in decimal_add()
4967 pA->a[i] = x+10; in decimal_add()
4970 pA->a[i] = x; in decimal_add()
5031 pB->sign = !pB->sign; in decimalSubFunc()
5054 if( !p->isInit ){ in decimalSumStep()
5055 p->isInit = 1; in decimalSumStep()
5056 p->a = sqlite3_malloc(2); in decimalSumStep()
5057 if( p->a==0 ){ in decimalSumStep()
5058 p->oom = 1; in decimalSumStep()
5060 p->a[0] = 0; in decimalSumStep()
5062 p->nDigit = 1; in decimalSumStep()
5063 p->nFrac = 0; in decimalSumStep()
5082 if( pArg ) pArg->sign = !pArg->sign; in decimalSumInverse()
5119 if( pA==0 || pA->oom || pA->isNull in decimalMulFunc()
5120 || pB==0 || pB->oom || pB->isNull in decimalMulFunc()
5124 acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 ); in decimalMulFunc()
5129 memset(acc, 0, pA->nDigit + pB->nDigit + 2); in decimalMulFunc()
5130 minFrac = pA->nFrac; in decimalMulFunc()
5131 if( pB->nFrac<minFrac ) minFrac = pB->nFrac; in decimalMulFunc()
5132 for(i=pA->nDigit-1; i>=0; i--){ in decimalMulFunc()
5133 signed char f = pA->a[i]; in decimalMulFunc()
5135 for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){ in decimalMulFunc()
5136 x = acc[k] + f*pB->a[j] + carry; in decimalMulFunc()
5142 acc[k-1] += x/10; in decimalMulFunc()
5144 sqlite3_free(pA->a); in decimalMulFunc()
5145 pA->a = acc; in decimalMulFunc()
5147 pA->nDigit += pB->nDigit + 2; in decimalMulFunc()
5148 pA->nFrac += pB->nFrac; in decimalMulFunc()
5149 pA->sign ^= pB->sign; in decimalMulFunc()
5150 while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){ in decimalMulFunc()
5151 pA->nFrac--; in decimalMulFunc()
5152 pA->nDigit--; in decimalMulFunc()
5166 sqlite3 *db, in sqlite3_decimal_init() argument
5188 rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg, in sqlite3_decimal_init()
5193 rc = sqlite3_create_window_function(db, "decimal_sum", 1, in sqlite3_decimal_init()
5199 rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8, in sqlite3_decimal_init()
5208 ** 2013-04-17
5220 ** and input of IEEE754 Binary64 floating-point numbers.
5225 ** In the first form, the value X should be a floating-point number.
5230 ** base-2 exponent of a new floating point number. The function returns
5231 ** a floating-point value equal to Y*pow(2,Z).
5235 ** ieee754(2.0) -> 'ieee754(2,0)'
5236 ** ieee754(45.25) -> 'ieee754(181,-2)'
5237 ** ieee754(2, 0) -> 2.0
5238 ** ieee754(181, -2) -> 45.25
5240 ** Two additional functions break apart the one-argument ieee754()
5243 ** ieee754_mantissa(45.25) -> 181
5244 ** ieee754_exponent(45.25) -> -2
5248 ** ieee754_from_blob(x'3ff0000000000000') -> 1.0
5249 ** ieee754_to_blob(1.0) -> x'3ff0000000000000'
5251 ** In all single-argument functions, if the argument is an 8-byte blob
5252 ** then that blob is interpreted as a big-endian binary64 value.
5256 ** -----------------------------------------------
5269 ** VALUES(-1,'0.5')
5271 ** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5278 ** ---------------^^^^^---- Replace with whatever you want
5335 r = -r; in ieee754func()
5345 m = a & ((((sqlite3_int64)1)<<52)-1); in ieee754func()
5355 if( isNeg ) m = -m; in ieee754func()
5360 m, e-1075); in ieee754func()
5361 sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT); in ieee754func()
5367 sqlite3_result_int(context, e-1075); in ieee754func()
5377 /* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */ in ieee754func()
5380 }else if( e<-10000 ){ in ieee754func()
5381 e = -10000; in ieee754func()
5386 m = -m; in ieee754func()
5388 }else if( m==0 && e>-1000 && e<1000 ){ in ieee754func()
5398 e--; in ieee754func()
5403 if( 1-e >= 64 ){ in ieee754func()
5406 m >>= 1-e; in ieee754func()
5412 a = m & ((((sqlite3_int64)1)<<52)-1); in ieee754func()
5458 a[sizeof(r)-i] = v&0xff; in ieee754func_to_blob()
5470 sqlite3 *db, in sqlite3_ieee_init() argument
5493 rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg, in sqlite3_ieee_init()
5504 ** 2015-08-18
5515 ** This file demonstrates how to create a table-valued-function using
5571 ** series are well-defined.
5587 sqlite3_vtab_cursor base; /* Base class - must be first */
5610 sqlite3 *db, in seriesConnect() argument
5629 rc = sqlite3_declare_vtab(db, in seriesConnect()
5635 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); in seriesConnect()
5657 *ppCursor = &pCur->base; in seriesOpen()
5675 if( pCur->isDesc ){ in seriesNext()
5676 pCur->iValue -= pCur->iStep; in seriesNext()
5678 pCur->iValue += pCur->iStep; in seriesNext()
5680 pCur->iRowid++; in seriesNext()
5696 case SERIES_COLUMN_START: x = pCur->mnValue; break; in seriesColumn()
5697 case SERIES_COLUMN_STOP: x = pCur->mxValue; break; in seriesColumn()
5698 case SERIES_COLUMN_STEP: x = pCur->iStep; break; in seriesColumn()
5699 default: x = pCur->iValue; break; in seriesColumn()
5712 *pRowid = pCur->iRowid; in seriesRowid()
5722 if( pCur->isDesc ){ in seriesEof()
5723 return pCur->iValue < pCur->mnValue; in seriesEof()
5725 return pCur->iValue > pCur->mxValue; in seriesEof()
5729 /* True to cause run-time checking of the start=, stop=, and/or step=
5768 pCur->mnValue = sqlite3_value_int64(argv[i++]); in seriesFilter()
5770 pCur->mnValue = 0; in seriesFilter()
5773 pCur->mxValue = sqlite3_value_int64(argv[i++]); in seriesFilter()
5775 pCur->mxValue = 0xffffffff; in seriesFilter()
5778 pCur->iStep = sqlite3_value_int64(argv[i++]); in seriesFilter()
5779 if( pCur->iStep==0 ){ in seriesFilter()
5780 pCur->iStep = 1; in seriesFilter()
5781 }else if( pCur->iStep<0 ){ in seriesFilter()
5782 pCur->iStep = -pCur->iStep; in seriesFilter()
5786 pCur->iStep = 1; in seriesFilter()
5792 pCur->mnValue = 1; in seriesFilter()
5793 pCur->mxValue = 0; in seriesFilter()
5798 pCur->isDesc = 1; in seriesFilter()
5799 pCur->iValue = pCur->mxValue; in seriesFilter()
5800 if( pCur->iStep>0 ){ in seriesFilter()
5801 pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep; in seriesFilter()
5804 pCur->isDesc = 0; in seriesFilter()
5805 pCur->iValue = pCur->mnValue; in seriesFilter()
5807 pCur->iRowid = 1; in seriesFilter()
5822 ** (1) start = $value -- constraint exists
5823 ** (2) stop = $value -- constraint exists
5824 ** (4) step = $value -- constraint exists
5844 aIdx[0] = aIdx[1] = aIdx[2] = -1; in seriesBestIndex()
5845 pConstraint = pIdxInfo->aConstraint; in seriesBestIndex()
5846 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ in seriesBestIndex()
5849 if( pConstraint->iColumn<SERIES_COLUMN_START ) continue; in seriesBestIndex()
5850 iCol = pConstraint->iColumn - SERIES_COLUMN_START; in seriesBestIndex()
5854 if( pConstraint->usable==0 ){ in seriesBestIndex()
5857 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in seriesBestIndex()
5864 pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg; in seriesBestIndex()
5865 pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY; in seriesBestIndex()
5870 ** first argument was omitted. Compile with -DZERO_ARGUMENT_GENERATE_SERIES in seriesBestIndex()
5874 sqlite3_free(pVTab->zErrMsg); in seriesBestIndex()
5875 pVTab->zErrMsg = sqlite3_mprintf( in seriesBestIndex()
5889 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0)); in seriesBestIndex()
5890 pIdxInfo->estimatedRows = 1000; in seriesBestIndex()
5891 if( pIdxInfo->nOrderBy==1 ){ in seriesBestIndex()
5892 if( pIdxInfo->aOrderBy[0].desc ){ in seriesBestIndex()
5897 pIdxInfo->orderByConsumed = 1; in seriesBestIndex()
5903 pIdxInfo->estimatedRows = 2147483647; in seriesBestIndex()
5905 pIdxInfo->idxNum = idxNum; in seriesBestIndex()
5920 seriesOpen, /* xOpen - open a cursor */
5921 seriesClose, /* xClose - close a cursor */
5922 seriesFilter, /* xFilter - configure scan constraints */
5923 seriesNext, /* xNext - advance a cursor */
5924 seriesEof, /* xEof - check for end of scan */
5925 seriesColumn, /* xColumn - read data */
5926 seriesRowid, /* xRowid - read data */
5946 sqlite3 *db, in sqlite3_series_init() argument
5958 rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0); in sqlite3_series_init()
5966 ** 2012-11-13
5978 ** efficient regular-expression matcher for posix extended regular
6000 ** \c C-language escapes for c in afnrtv. ex: \t or \n
6005 ** [a-z] Any single character in the range a-z
6006 ** [^a-z] Any single character not in the range a-z
6008 ** \w Word character. [A-Za-z0-9_]
6009 ** \W Non-word character
6011 ** \D Non-digit
6013 ** \S Non-whitespace character
6019 ** to p copies of X following by q-p copies of X? and that the size of the
6030 ** this file to prevent name collisions with C-library functions of the
6037 /* The end-of-input character */
6053 #define RE_OP_WORD 11 /* Perl word character [A-Za-z0-9_] */
6055 #define RE_OP_DIGIT 13 /* digit: [0-9] */
6059 #define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
6102 for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return; in re_add_state()
6103 pSet->aState[pSet->nState++] = (ReStateNumber)newState; in re_add_state()
6113 if( p->i>=p->mx ) return 0; in re_next_char()
6114 c = p->z[p->i++]; in re_next_char()
6116 if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){ in re_next_char()
6117 c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f); in re_next_char()
6119 }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80 in re_next_char()
6120 && (p->z[p->i+1]&0xc0)==0x80 ){ in re_next_char()
6121 c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f); in re_next_char()
6122 p->i += 2; in re_next_char()
6124 }else if( (c&0xf8)==0xf0 && p->i+3<p->mx && (p->z[p->i]&0xc0)==0x80 in re_next_char()
6125 && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){ in re_next_char()
6126 c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6) in re_next_char()
6127 | (p->z[p->i+2]&0x3f); in re_next_char()
6128 p->i += 3; in re_next_char()
6138 if( c>='A' && c<='Z' ) c += 'a' - 'A'; in re_next_char_nocase()
6142 /* Return true if c is a perl "word" character: [A-Za-z0-9_] */
6148 /* Return true if c is a "digit" character: [0-9] */
6158 /* Run a compiled regular expression on the zero-terminated input
6177 if( pRe->nInit ){ in re_match()
6178 unsigned char x = pRe->zInit[0]; in re_match()
6179 while( in.i+pRe->nInit<=in.mx in re_match()
6181 strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0) in re_match()
6185 if( in.i+pRe->nInit>in.mx ) return 0; in re_match()
6188 if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){ in re_match()
6192 pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState ); in re_match()
6193 if( pToFree==0 ) return -1; in re_match()
6196 aStateSet[1].aState = &aStateSet[0].aState[pRe->nState]; in re_match()
6198 pNext->nState = 0; in re_match()
6200 while( c!=RE_EOF && pNext->nState>0 ){ in re_match()
6202 c = pRe->xNextChar(&in); in re_match()
6205 iSwap = 1 - iSwap; in re_match()
6206 pNext->nState = 0; in re_match()
6207 for(i=0; i<pThis->nState; i++){ in re_match()
6208 int x = pThis->aState[i]; in re_match()
6209 switch( pRe->aOp[x] ){ in re_match()
6211 if( pRe->aArg[x]==c ) re_add_state(pNext, x+1); in re_match()
6252 re_add_state(pThis, x+pRe->aArg[x]); in re_match()
6257 re_add_state(pThis, x+pRe->aArg[x]); in re_match()
6266 /* fall-through */ goto re_op_cc_inc; in re_match()
6270 int n = pRe->aArg[x]; in re_match()
6273 if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){ in re_match()
6274 if( pRe->aArg[x+j]==c ){ in re_match()
6276 j = -1; in re_match()
6279 if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){ in re_match()
6281 j = -1; in re_match()
6287 if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit; in re_match()
6294 for(i=0; i<pNext->nState; i++){ in re_match()
6295 if( pRe->aOp[pNext->aState[i]]==RE_OP_ACCEPT ){ rc = 1; break; } in re_match()
6307 aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0])); in re_resize()
6309 p->aOp = aOp; in re_resize()
6310 aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0])); in re_resize()
6312 p->aArg = aArg; in re_resize()
6313 p->nAlloc = N; in re_resize()
6322 if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0; in re_insert()
6323 for(i=p->nState; i>iBefore; i--){ in re_insert()
6324 p->aOp[i] = p->aOp[i-1]; in re_insert()
6325 p->aArg[i] = p->aArg[i-1]; in re_insert()
6327 p->nState++; in re_insert()
6328 p->aOp[iBefore] = (char)op; in re_insert()
6329 p->aArg[iBefore] = arg; in re_insert()
6336 return re_insert(p, p->nState, op, arg); in re_append()
6343 if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return; in re_copy()
6344 memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0])); in re_copy()
6345 memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0])); in re_copy()
6346 p->nState += N; in re_copy()
6349 /* Return true if c is a hexadecimal digit character: [0-9a-fA-F]
6355 c -= '0'; in re_hex()
6357 c -= 'a' - 10; in re_hex()
6359 c -= 'A' - 10; in re_hex()
6375 if( p->sIn.i>=p->sIn.mx ) return 0; in re_esc_char()
6376 c = p->sIn.z[p->sIn.i]; in re_esc_char()
6377 if( c=='u' && p->sIn.i+4<p->sIn.mx ){ in re_esc_char()
6378 const unsigned char *zIn = p->sIn.z + p->sIn.i; in re_esc_char()
6384 p->sIn.i += 5; in re_esc_char()
6388 if( c=='x' && p->sIn.i+2<p->sIn.mx ){ in re_esc_char()
6389 const unsigned char *zIn = p->sIn.z + p->sIn.i; in re_esc_char()
6393 p->sIn.i += 3; in re_esc_char()
6400 p->sIn.i++; in re_esc_char()
6402 p->zErr = "unknown \\ escape"; in re_esc_char()
6412 return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0; in rePeek()
6422 iStart = p->nState; in re_subcompile_re()
6426 iEnd = p->nState; in re_subcompile_re()
6427 re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart); in re_subcompile_re()
6429 p->sIn.i++; in re_subcompile_re()
6432 p->aArg[iGoto] = p->nState - iGoto; in re_subcompile_re()
6442 int iPrev = -1; in re_subcompile_string()
6446 while( (c = p->xNextChar(&p->sIn))!=0 ){ in re_subcompile_string()
6447 iStart = p->nState; in re_subcompile_string()
6452 p->sIn.i--; in re_subcompile_string()
6459 p->sIn.i++; in re_subcompile_string()
6465 p->sIn.i++; in re_subcompile_string()
6473 re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1); in re_subcompile_string()
6474 re_append(p, RE_OP_FORK, iPrev - p->nState + 1); in re_subcompile_string()
6479 re_append(p, RE_OP_FORK, iPrev - p->nState); in re_subcompile_string()
6484 re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1); in re_subcompile_string()
6491 while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; } in re_subcompile_string()
6494 p->sIn.i++; in re_subcompile_string()
6496 while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; } in re_subcompile_string()
6500 p->sIn.i++; in re_subcompile_string()
6501 sz = p->nState - iPrev; in re_subcompile_string()
6505 n--; in re_subcompile_string()
6514 re_append(p, RE_OP_FORK, -sz); in re_subcompile_string()
6519 int iFirst = p->nState; in re_subcompile_string()
6522 p->sIn.i++; in re_subcompile_string()
6526 while( (c = p->xNextChar(&p->sIn))!=0 ){ in re_subcompile_string()
6531 if( rePeek(p)=='-' ){ in re_subcompile_string()
6533 p->sIn.i++; in re_subcompile_string()
6534 c = p->xNextChar(&p->sIn); in re_subcompile_string()
6540 if( rePeek(p)==']' ){ p->sIn.i++; break; } in re_subcompile_string()
6543 p->aArg[iFirst] = p->nState - iFirst; in re_subcompile_string()
6558 p->sIn.i++; in re_subcompile_string()
6582 sqlite3_free(pRe->aOp); in re_free()
6583 sqlite3_free(pRe->aArg); in re_free()
6605 pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char; in re_compile()
6615 pRe->sIn.z = (unsigned char*)zIn; in re_compile()
6616 pRe->sIn.i = 0; in re_compile()
6617 pRe->sIn.mx = (int)strlen(zIn); in re_compile()
6623 if( rePeek(pRe)=='$' && pRe->sIn.i+1>=pRe->sIn.mx ){ in re_compile()
6627 }else if( pRe->sIn.i>=pRe->sIn.mx ){ in re_compile()
6641 ** unicode characters beyond plane 0 - those are very rare and this is in re_compile()
6643 if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){ in re_compile()
6644 for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){ in re_compile()
6645 unsigned x = pRe->aArg[i]; in re_compile()
6647 pRe->zInit[j++] = (unsigned char)x; in re_compile()
6649 pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6)); in re_compile()
6650 pRe->zInit[j++] = 0x80 | (x&0x3f); in re_compile()
6652 pRe->zInit[j++] = (unsigned char)(0xd0 | (x>>12)); in re_compile()
6653 pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f); in re_compile()
6654 pRe->zInit[j++] = 0x80 | (x&0x3f); in re_compile()
6659 if( j>0 && pRe->zInit[j-1]==0 ) j--; in re_compile()
6660 pRe->nInit = j; in re_compile()
6662 return pRe->zErr; in re_compile()
6667 ** the build-in REGEXP operator. The first argument to the function is the
6693 sqlite3_result_error(context, zErr, -1); in re_sql_func()
6704 sqlite3_result_int(context, re_match(pRe, zStr, -1)); in re_sql_func()
6719 sqlite3 *db, in sqlite3_regexp_init() argument
6726 rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8|SQLITE_INNOCUOUS, in sqlite3_regexp_init()
6729 /* The regexpi(PATTERN,STRING) function is a case-insensitive version in sqlite3_regexp_init()
6731 rc = sqlite3_create_function(db, "regexpi", 2, SQLITE_UTF8|SQLITE_INNOCUOUS, in sqlite3_regexp_init()
6732 (void*)db, re_sql_func, 0, 0); in sqlite3_regexp_init()
6741 ** 2017-12-26
6794 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
6795 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
6853 ** Use this value for the "version-made-by" field in new zip file
6856 ** This is what info-zip seems to do.
6859 ** Value for "version-required-to-extract" field of new entries.
6863 ** Value for "general-purpose-bit-flags" field of new entries. Bit
6864 ** 11 means "utf-8 filename and comment".
6884 ** The sizes of the fixed-size part of each of the three main data
6931 *** crc-32 4 bytes
6972 *** crc-32 4 bytes
7000 ZipfileEntry *pNext; /* Next element in in-memory CDS */
7008 sqlite3_vtab_cursor base; /* Base class - must be first */
7011 u8 bNoop; /* If next xNext() call is no-op */
7025 sqlite3_vtab base; /* Base class - must be first */
7027 sqlite3 *db; /* Host database connection */ member
7050 sqlite3_result_error(ctx, zMsg, -1); in zipfileCtxErrorMsg()
7077 ** argv[0] -> module name ("zipfile")
7078 ** argv[1] -> database name
7079 ** argv[2] -> table name
7080 ** argv[...] -> "column name" and other module argument fields.
7083 sqlite3 *db, in zipfileConnect() argument
7115 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA); in zipfileConnect()
7120 pNew->db = db; in zipfileConnect()
7121 pNew->aBuffer = (u8*)&pNew[1]; in zipfileConnect()
7123 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE]; in zipfileConnect()
7124 memcpy(pNew->zFile, zFile, nFile); in zipfileConnect()
7125 zipfileDequote(pNew->zFile); in zipfileConnect()
7128 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); in zipfileConnect()
7138 sqlite3_free(p->cds.zFile); in zipfileEntryFree()
7151 if( pTab->pWriteFd ){ in zipfileCleanupTransaction()
7152 fclose(pTab->pWriteFd); in zipfileCleanupTransaction()
7153 pTab->pWriteFd = 0; in zipfileCleanupTransaction()
7155 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){ in zipfileCleanupTransaction()
7156 pNext = pEntry->pNext; in zipfileCleanupTransaction()
7159 pTab->pFirstEntry = 0; in zipfileCleanupTransaction()
7160 pTab->pLastEntry = 0; in zipfileCleanupTransaction()
7161 pTab->szCurrent = 0; in zipfileCleanupTransaction()
7162 pTab->szOrig = 0; in zipfileCleanupTransaction()
7186 pCsr->iId = ++pTab->iNextCsrid; in zipfileOpen()
7187 pCsr->pCsrNext = pTab->pCsrList; in zipfileOpen()
7188 pTab->pCsrList = pCsr; in zipfileOpen()
7200 pCsr->bEof = 0; in zipfileResetCursor()
7201 if( pCsr->pFile ){ in zipfileResetCursor()
7202 fclose(pCsr->pFile); in zipfileResetCursor()
7203 pCsr->pFile = 0; in zipfileResetCursor()
7204 zipfileEntryFree(pCsr->pCurrent); in zipfileResetCursor()
7205 pCsr->pCurrent = 0; in zipfileResetCursor()
7208 for(p=pCsr->pFreeEntry; p; p=pNext){ in zipfileResetCursor()
7209 pNext = p->pNext; in zipfileResetCursor()
7219 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab); in zipfileClose()
7224 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext)); in zipfileClose()
7225 *pp = pCsr->pCsrNext; in zipfileClose()
7238 sqlite3_free(pTab->base.zErrMsg); in zipfileTableErr()
7239 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap); in zipfileTableErr()
7245 sqlite3_free(pCsr->base.pVtab->zErrMsg); in zipfileCursorErr()
7246 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); in zipfileCursorErr()
7284 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET); in zipfileAppendData()
7285 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd); in zipfileAppendData()
7287 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()"); in zipfileAppendData()
7290 pTab->szCurrent += nWrite; in zipfileAppendData()
7296 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
7303 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
7314 ** Write a 16-bit little endiate integer into buffer aBuf.
7322 ** Write a 32-bit little endiate integer into buffer aBuf.
7331 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
7332 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
7345 ** if the record is not well-formed, or SQLITE_OK otherwise.
7354 pCDS->iVersionMadeBy = zipfileRead16(aRead); in zipfileReadCDS()
7355 pCDS->iVersionExtract = zipfileRead16(aRead); in zipfileReadCDS()
7356 pCDS->flags = zipfileRead16(aRead); in zipfileReadCDS()
7357 pCDS->iCompression = zipfileRead16(aRead); in zipfileReadCDS()
7358 pCDS->mTime = zipfileRead16(aRead); in zipfileReadCDS()
7359 pCDS->mDate = zipfileRead16(aRead); in zipfileReadCDS()
7360 pCDS->crc32 = zipfileRead32(aRead); in zipfileReadCDS()
7361 pCDS->szCompressed = zipfileRead32(aRead); in zipfileReadCDS()
7362 pCDS->szUncompressed = zipfileRead32(aRead); in zipfileReadCDS()
7364 pCDS->nFile = zipfileRead16(aRead); in zipfileReadCDS()
7365 pCDS->nExtra = zipfileRead16(aRead); in zipfileReadCDS()
7366 pCDS->nComment = zipfileRead16(aRead); in zipfileReadCDS()
7367 pCDS->iDiskStart = zipfileRead16(aRead); in zipfileReadCDS()
7368 pCDS->iInternalAttr = zipfileRead16(aRead); in zipfileReadCDS()
7369 pCDS->iExternalAttr = zipfileRead32(aRead); in zipfileReadCDS()
7370 pCDS->iOffset = zipfileRead32(aRead); in zipfileReadCDS()
7379 ** if the record is not well-formed, or SQLITE_OK otherwise.
7392 pLFH->iVersionExtract = zipfileRead16(aRead); in zipfileReadLFH()
7393 pLFH->flags = zipfileRead16(aRead); in zipfileReadLFH()
7394 pLFH->iCompression = zipfileRead16(aRead); in zipfileReadLFH()
7395 pLFH->mTime = zipfileRead16(aRead); in zipfileReadLFH()
7396 pLFH->mDate = zipfileRead16(aRead); in zipfileReadLFH()
7397 pLFH->crc32 = zipfileRead32(aRead); in zipfileReadLFH()
7398 pLFH->szCompressed = zipfileRead32(aRead); in zipfileReadLFH()
7399 pLFH->szUncompressed = zipfileRead32(aRead); in zipfileReadLFH()
7400 pLFH->nFile = zipfileRead16(aRead); in zipfileReadLFH()
7401 pLFH->nExtra = zipfileRead16(aRead); in zipfileReadLFH()
7409 ** Scan through this buffer to find an "extra-timestamp" field. If one
7410 ** exists, extract the 32-bit modification-timestamp from it and store
7413 ** Zero is returned if no extra-timestamp record could be found (and so
7414 ** *pmTime is left unchanged), or non-zero otherwise.
7434 if( b & 0x01 ){ /* 0x01 -> modtime is present */ in zipfileScanExtra()
7448 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
7449 ** fields of the CDS structure passed as the only argument to a 32-bit
7450 ** UNIX seconds-since-the-epoch timestamp. Return the result.
7452 ** "Standard" MS-DOS time format:
7455 ** Bits 00-04: seconds divided by 2
7456 ** Bits 05-10: minute
7457 ** Bits 11-15: hour
7459 ** Bits 00-04: day
7460 ** Bits 05-08: month (1-12)
7461 ** Bits 09-15: years from 1980
7463 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
7468 Y = (1980 + ((pCDS->mDate >> 9) & 0x7F)); in zipfileMtime()
7469 M = ((pCDS->mDate >> 5) & 0x0F); in zipfileMtime()
7470 D = (pCDS->mDate & 0x1F); in zipfileMtime()
7471 sec = (pCDS->mTime & 0x1F)*2; in zipfileMtime()
7472 min = (pCDS->mTime >> 5) & 0x3F; in zipfileMtime()
7473 hr = (pCDS->mTime >> 11) & 0x1F; in zipfileMtime()
7475 Y--; in zipfileMtime()
7481 B = 2 - A + (A/4); in zipfileMtime()
7482 JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec; in zipfileMtime()
7483 return (u32)(JDsec - (i64)24405875*(i64)8640); in zipfileMtime()
7499 A = (int)((JD - 1867216.25)/36524.25); in zipfileMtimeToDos()
7500 A = (int)(JD + 1 + A - (A/4)); in zipfileMtimeToDos()
7502 C = (int)((B - 122.1)/365.25); in zipfileMtimeToDos()
7504 E = (int)((B-D)/30.6001); in zipfileMtimeToDos()
7506 day = B - D - (int)(30.6001*E); in zipfileMtimeToDos()
7507 mon = (E<14 ? E-1 : E-13); in zipfileMtimeToDos()
7508 yr = mon>2 ? C-4716 : C-4715; in zipfileMtimeToDos()
7515 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9)); in zipfileMtimeToDos()
7516 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11)); in zipfileMtimeToDos()
7518 pCds->mDate = pCds->mTime = 0; in zipfileMtimeToDos()
7523 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) in zipfileMtimeToDos()
7531 ** then pFile is a file-handle open on a zip file. In either case, this
7541 const u8 *aBlob, /* Pointer to in-memory file image */ in zipfileGetEntry()
7548 char **pzErr = &pTab->base.zErrMsg; in zipfileGetEntry()
7552 aRead = pTab->aBuffer; in zipfileGetEntry()
7576 rc = zipfileReadCDS(aRead, &pNew->cds); in zipfileGetEntry()
7589 u32 *pt = &pNew->mUnixTime; in zipfileGetEntry()
7590 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); in zipfileGetEntry()
7591 pNew->aExtra = (u8*)&pNew[1]; in zipfileGetEntry()
7592 memcpy(pNew->aExtra, &aRead[nFile], nExtra); in zipfileGetEntry()
7593 if( pNew->cds.zFile==0 ){ in zipfileGetEntry()
7595 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){ in zipfileGetEntry()
7596 pNew->mUnixTime = zipfileMtime(&pNew->cds); in zipfileGetEntry()
7604 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr); in zipfileGetEntry()
7606 aRead = (u8*)&aBlob[pNew->cds.iOffset]; in zipfileGetEntry()
7611 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ; in zipfileGetEntry()
7612 pNew->iDataOff += lfh.nFile + lfh.nExtra; in zipfileGetEntry()
7613 if( aBlob && pNew->cds.szCompressed ){ in zipfileGetEntry()
7614 pNew->aData = &pNew->aExtra[nExtra]; in zipfileGetEntry()
7615 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed); in zipfileGetEntry()
7619 (int)pNew->cds.iOffset in zipfileGetEntry()
7641 if( pCsr->pFile ){ in zipfileNext()
7642 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize; in zipfileNext()
7643 zipfileEntryFree(pCsr->pCurrent); in zipfileNext()
7644 pCsr->pCurrent = 0; in zipfileNext()
7645 if( pCsr->iNextOff>=iEof ){ in zipfileNext()
7646 pCsr->bEof = 1; in zipfileNext()
7649 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab); in zipfileNext()
7650 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p); in zipfileNext()
7652 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ; in zipfileNext()
7653 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment; in zipfileNext()
7655 pCsr->pCurrent = p; in zipfileNext()
7658 if( !pCsr->bNoop ){ in zipfileNext()
7659 pCsr->pCurrent = pCsr->pCurrent->pNext; in zipfileNext()
7661 if( pCsr->pCurrent==0 ){ in zipfileNext()
7662 pCsr->bEof = 1; in zipfileNext()
7666 pCsr->bNoop = 0; in zipfileNext()
7700 err = inflateInit2(&str, -15); in zipfileInflate()
7725 ** code is returned and an error message left in virtual-table handle
7742 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); in zipfileDeflate()
7778 ZipfileCDS *pCDS = &pCsr->pCurrent->cds; in zipfileColumn()
7782 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT); in zipfileColumn()
7787 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16); in zipfileColumn()
7790 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime); in zipfileColumn()
7795 sqlite3_result_int64(ctx, pCDS->szUncompressed); in zipfileColumn()
7802 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){ in zipfileColumn()
7803 int sz = pCDS->szCompressed; in zipfileColumn()
7804 int szFinal = pCDS->szUncompressed; in zipfileColumn()
7808 if( pCsr->pCurrent->aData ){ in zipfileColumn()
7809 aBuf = pCsr->pCurrent->aData; in zipfileColumn()
7815 FILE *pFile = pCsr->pFile; in zipfileColumn()
7817 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd; in zipfileColumn()
7819 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff, in zipfileColumn()
7820 &pCsr->base.pVtab->zErrMsg in zipfileColumn()
7825 if( i==5 && pCDS->iCompression ){ in zipfileColumn()
7833 /* Figure out if this is a directory or a zero-sized file. Consider in zipfileColumn()
7836 u32 mode = pCDS->iExternalAttr >> 16; in zipfileColumn()
7837 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){ in zipfileColumn()
7845 sqlite3_result_int(ctx, pCDS->iCompression); in zipfileColumn()
7849 sqlite3_result_int64(ctx, pCsr->iId); in zipfileColumn()
7861 return pCsr->bEof; in zipfileEof()
7867 ** is guaranteed to be a file-handle open on a zip file.
7872 ** an English language error message may be left in virtual-table pTab.
7876 const u8 *aBlob, /* Pointer to in-memory file image */ in zipfileReadEOCD()
7881 u8 *aRead = pTab->aBuffer; /* Temporary buffer */ in zipfileReadEOCD()
7895 iOff = szFile - nRead; in zipfileReadEOCD()
7896 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg); in zipfileReadEOCD()
7899 aRead = (u8*)&aBlob[nBlob-nRead]; in zipfileReadEOCD()
7906 for(i=nRead-20; i>=0; i--){ in zipfileReadEOCD()
7914 pTab->base.zErrMsg = sqlite3_mprintf( in zipfileReadEOCD()
7921 pEOCD->iDisk = zipfileRead16(aRead); in zipfileReadEOCD()
7922 pEOCD->iFirstDisk = zipfileRead16(aRead); in zipfileReadEOCD()
7923 pEOCD->nEntry = zipfileRead16(aRead); in zipfileReadEOCD()
7924 pEOCD->nEntryTotal = zipfileRead16(aRead); in zipfileReadEOCD()
7925 pEOCD->nSize = zipfileRead32(aRead); in zipfileReadEOCD()
7926 pEOCD->iOffset = zipfileRead32(aRead); in zipfileReadEOCD()
7943 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) ); in zipfileAddEntry()
7944 assert( pNew->pNext==0 ); in zipfileAddEntry()
7946 if( pTab->pFirstEntry==0 ){ in zipfileAddEntry()
7947 pTab->pFirstEntry = pTab->pLastEntry = pNew; in zipfileAddEntry()
7949 assert( pTab->pLastEntry->pNext==0 ); in zipfileAddEntry()
7950 pTab->pLastEntry->pNext = pNew; in zipfileAddEntry()
7951 pTab->pLastEntry = pNew; in zipfileAddEntry()
7955 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext)); in zipfileAddEntry()
7956 pNew->pNext = pBefore; in zipfileAddEntry()
7967 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd); in zipfileLoadDirectory()
7971 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew); in zipfileLoadDirectory()
7976 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment; in zipfileLoadDirectory()
7990 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab; in zipfileFilter()
7994 int bInMemory = 0; /* True for an in-memory zipfile */ in zipfileFilter()
7998 if( pTab->zFile ){ in zipfileFilter()
7999 zFile = pTab->zFile; in zipfileFilter()
8006 assert( pTab->pFirstEntry==0 ); in zipfileFilter()
8008 pCsr->pFreeEntry = pTab->pFirstEntry; in zipfileFilter()
8009 pTab->pFirstEntry = pTab->pLastEntry = 0; in zipfileFilter()
8016 if( 0==pTab->pWriteFd && 0==bInMemory ){ in zipfileFilter()
8017 pCsr->pFile = fopen(zFile, "rb"); in zipfileFilter()
8018 if( pCsr->pFile==0 ){ in zipfileFilter()
8022 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd); in zipfileFilter()
8024 if( pCsr->eocd.nEntry==0 ){ in zipfileFilter()
8025 pCsr->bEof = 1; in zipfileFilter()
8027 pCsr->iNextOff = pCsr->eocd.iOffset; in zipfileFilter()
8033 pCsr->bNoop = 1; in zipfileFilter()
8034 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry; in zipfileFilter()
8049 int idx = -1; in zipfileBestIndex()
8052 for(i=0; i<pIdxInfo->nConstraint; i++){ in zipfileBestIndex()
8053 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i]; in zipfileBestIndex()
8054 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue; in zipfileBestIndex()
8055 if( pCons->usable==0 ){ in zipfileBestIndex()
8057 }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in zipfileBestIndex()
8061 pIdxInfo->estimatedCost = 1000.0; in zipfileBestIndex()
8063 pIdxInfo->aConstraintUsage[idx].argvIndex = 1; in zipfileBestIndex()
8064 pIdxInfo->aConstraintUsage[idx].omit = 1; in zipfileBestIndex()
8065 pIdxInfo->idxNum = 1; in zipfileBestIndex()
8077 pNew->cds.zFile = sqlite3_mprintf("%s", zPath); in zipfileNewEntry()
8078 if( pNew->cds.zFile==0 ){ in zipfileNewEntry()
8087 ZipfileCDS *pCds = &pEntry->cds; in zipfileSerializeLFH()
8090 pCds->nExtra = 9; in zipfileSerializeLFH()
8094 zipfileWrite16(a, pCds->iVersionExtract); in zipfileSerializeLFH()
8095 zipfileWrite16(a, pCds->flags); in zipfileSerializeLFH()
8096 zipfileWrite16(a, pCds->iCompression); in zipfileSerializeLFH()
8097 zipfileWrite16(a, pCds->mTime); in zipfileSerializeLFH()
8098 zipfileWrite16(a, pCds->mDate); in zipfileSerializeLFH()
8099 zipfileWrite32(a, pCds->crc32); in zipfileSerializeLFH()
8100 zipfileWrite32(a, pCds->szCompressed); in zipfileSerializeLFH()
8101 zipfileWrite32(a, pCds->szUncompressed); in zipfileSerializeLFH()
8102 zipfileWrite16(a, (u16)pCds->nFile); in zipfileSerializeLFH()
8103 zipfileWrite16(a, pCds->nExtra); in zipfileSerializeLFH()
8107 memcpy(a, pCds->zFile, (int)pCds->nFile); in zipfileSerializeLFH()
8108 a += (int)pCds->nFile; in zipfileSerializeLFH()
8114 zipfileWrite32(a, pEntry->mUnixTime); in zipfileSerializeLFH()
8116 return a-aBuf; in zipfileSerializeLFH()
8125 u8 *aBuf = pTab->aBuffer; in zipfileAppendEntry()
8132 pEntry->iDataOff = pTab->szCurrent; in zipfileAppendEntry()
8152 const char zTemplate[11] = "-rwxrwxrwx"; in zipfileGetMode()
8156 case '-': mode |= S_IFREG; break; in zipfileGetMode()
8162 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i); in zipfileGetMode()
8163 else if( z[i]!='-' ) goto parse_error; in zipfileGetMode()
8168 ** Or vice-versa - no data but "mode" is a file or symlink. */ in zipfileGetMode()
8181 ** Both (const char*) arguments point to nul-terminated strings. Argument
8186 if( nA>0 && zA[nA-1]=='/' ) nA--; in zipfileComparePath()
8187 if( nB>0 && zB[nB-1]=='/' ) nB--; in zipfileComparePath()
8196 assert( pTab->pWriteFd==0 ); in zipfileBegin()
8197 if( pTab->zFile==0 || pTab->zFile[0]==0 ){ in zipfileBegin()
8198 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename"); in zipfileBegin()
8205 ** in main-memory until the transaction is committed. */ in zipfileBegin()
8206 pTab->pWriteFd = fopen(pTab->zFile, "ab+"); in zipfileBegin()
8207 if( pTab->pWriteFd==0 ){ in zipfileBegin()
8208 pTab->base.zErrMsg = sqlite3_mprintf( in zipfileBegin()
8209 "zipfile: failed to open file %s for writing", pTab->zFile in zipfileBegin()
8213 fseek(pTab->pWriteFd, 0, SEEK_END); in zipfileBegin()
8214 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd); in zipfileBegin()
8226 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
8233 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ in zipfileTime()
8235 pVfs->xCurrentTimeInt64(pVfs, &ms); in zipfileTime()
8236 ret = (u32)((ms/1000) - ((i64)24405875 * 8640)); in zipfileTime()
8239 pVfs->xCurrentTime(pVfs, &day); in zipfileTime()
8240 ret = (u32)((day - 2440587.5) * 86400); in zipfileTime()
8246 ** Return a 32-bit timestamp in UNIX epoch format.
8250 ** cast to a 32-bit unsigned integer.
8260 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
8266 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext)); in zipfileRemoveEntryFromList()
8267 *pp = (*pp)->pNext; in zipfileRemoveEntryFromList()
8283 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */ in zipfileUpdate()
8301 if( pTab->pWriteFd==0 ){ in zipfileUpdate()
8316 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){ in zipfileUpdate()
8317 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){ in zipfileUpdate()
8320 assert( pOld->pNext ); in zipfileUpdate()
8356 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg); in zipfileUpdate()
8371 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg); in zipfileUpdate()
8383 ** '/'. This appears to be required for compatibility with info-zip in zipfileUpdate()
8386 if( nPath<=0 || zPath[nPath-1]!='/' ){ in zipfileUpdate()
8398 /* Check that we're not inserting a duplicate entry -OR- updating an in zipfileUpdate()
8402 for(p=pTab->pFirstEntry; p; p=p->pNext){ in zipfileUpdate()
8403 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){ in zipfileUpdate()
8404 switch( sqlite3_vtab_on_conflict(pTab->db) ){ in zipfileUpdate()
8429 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY; in zipfileUpdate()
8430 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED; in zipfileUpdate()
8431 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS; in zipfileUpdate()
8432 pNew->cds.iCompression = (u16)iMethod; in zipfileUpdate()
8433 zipfileMtimeToDos(&pNew->cds, mTime); in zipfileUpdate()
8434 pNew->cds.crc32 = iCrc32; in zipfileUpdate()
8435 pNew->cds.szCompressed = nData; in zipfileUpdate()
8436 pNew->cds.szUncompressed = (u32)sz; in zipfileUpdate()
8437 pNew->cds.iExternalAttr = (mode<<16); in zipfileUpdate()
8438 pNew->cds.iOffset = (u32)pTab->szCurrent; in zipfileUpdate()
8439 pNew->cds.nFile = (u16)nPath; in zipfileUpdate()
8440 pNew->mUnixTime = (u32)mTime; in zipfileUpdate()
8449 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){ in zipfileUpdate()
8450 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){ in zipfileUpdate()
8451 pCsr->pCurrent = pCsr->pCurrent->pNext; in zipfileUpdate()
8452 pCsr->bNoop = 1; in zipfileUpdate()
8469 zipfileWrite16(a, p->iDisk); in zipfileSerializeEOCD()
8470 zipfileWrite16(a, p->iFirstDisk); in zipfileSerializeEOCD()
8471 zipfileWrite16(a, p->nEntry); in zipfileSerializeEOCD()
8472 zipfileWrite16(a, p->nEntryTotal); in zipfileSerializeEOCD()
8473 zipfileWrite32(a, p->nSize); in zipfileSerializeEOCD()
8474 zipfileWrite32(a, p->iOffset); in zipfileSerializeEOCD()
8477 return a-aBuf; in zipfileSerializeEOCD()
8481 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer); in zipfileAppendEOCD()
8483 return zipfileAppendData(pTab, pTab->aBuffer, nBuf); in zipfileAppendEOCD()
8492 ZipfileCDS *pCDS = &pEntry->cds; in zipfileSerializeCDS()
8494 if( pEntry->aExtra==0 ){ in zipfileSerializeCDS()
8495 pCDS->nExtra = 9; in zipfileSerializeCDS()
8499 zipfileWrite16(a, pCDS->iVersionMadeBy); in zipfileSerializeCDS()
8500 zipfileWrite16(a, pCDS->iVersionExtract); in zipfileSerializeCDS()
8501 zipfileWrite16(a, pCDS->flags); in zipfileSerializeCDS()
8502 zipfileWrite16(a, pCDS->iCompression); in zipfileSerializeCDS()
8503 zipfileWrite16(a, pCDS->mTime); in zipfileSerializeCDS()
8504 zipfileWrite16(a, pCDS->mDate); in zipfileSerializeCDS()
8505 zipfileWrite32(a, pCDS->crc32); in zipfileSerializeCDS()
8506 zipfileWrite32(a, pCDS->szCompressed); in zipfileSerializeCDS()
8507 zipfileWrite32(a, pCDS->szUncompressed); in zipfileSerializeCDS()
8509 zipfileWrite16(a, pCDS->nFile); in zipfileSerializeCDS()
8510 zipfileWrite16(a, pCDS->nExtra); in zipfileSerializeCDS()
8511 zipfileWrite16(a, pCDS->nComment); in zipfileSerializeCDS()
8512 zipfileWrite16(a, pCDS->iDiskStart); in zipfileSerializeCDS()
8513 zipfileWrite16(a, pCDS->iInternalAttr); in zipfileSerializeCDS()
8514 zipfileWrite32(a, pCDS->iExternalAttr); in zipfileSerializeCDS()
8515 zipfileWrite32(a, pCDS->iOffset); in zipfileSerializeCDS()
8517 memcpy(a, pCDS->zFile, pCDS->nFile); in zipfileSerializeCDS()
8518 a += pCDS->nFile; in zipfileSerializeCDS()
8520 if( pEntry->aExtra ){ in zipfileSerializeCDS()
8521 int n = (int)pCDS->nExtra + (int)pCDS->nComment; in zipfileSerializeCDS()
8522 memcpy(a, pEntry->aExtra, n); in zipfileSerializeCDS()
8525 assert( pCDS->nExtra==9 ); in zipfileSerializeCDS()
8529 zipfileWrite32(a, pEntry->mUnixTime); in zipfileSerializeCDS()
8532 return a-aBuf; in zipfileSerializeCDS()
8538 if( pTab->pWriteFd ){ in zipfileCommit()
8539 i64 iOffset = pTab->szCurrent; in zipfileCommit()
8545 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){ in zipfileCommit()
8546 int n = zipfileSerializeCDS(p, pTab->aBuffer); in zipfileCommit()
8547 rc = zipfileAppendData(pTab, pTab->aBuffer, n); in zipfileCommit()
8556 eocd.nSize = (u32)(pTab->szCurrent - iOffset); in zipfileCommit()
8571 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){ in zipfileFindCursor()
8572 if( iId==pCsr->iId ) break; in zipfileFindCursor()
8588 ZipfileCDS *p = &pCsr->pCurrent->cds; in zipfileFunctionCds()
8590 "\"version-made-by\" : %u, " in zipfileFunctionCds()
8591 "\"version-to-extract\" : %u, " in zipfileFunctionCds()
8597 "\"compressed-size\" : %u, " in zipfileFunctionCds()
8598 "\"uncompressed-size\" : %u, " in zipfileFunctionCds()
8599 "\"file-name-length\" : %u, " in zipfileFunctionCds()
8600 "\"extra-field-length\" : %u, " in zipfileFunctionCds()
8601 "\"file-comment-length\" : %u, " in zipfileFunctionCds()
8602 "\"disk-number-start\" : %u, " in zipfileFunctionCds()
8603 "\"internal-attr\" : %u, " in zipfileFunctionCds()
8604 "\"external-attr\" : %u, " in zipfileFunctionCds()
8606 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract, in zipfileFunctionCds()
8607 (u32)p->flags, (u32)p->iCompression, in zipfileFunctionCds()
8608 (u32)p->mTime, (u32)p->mDate, in zipfileFunctionCds()
8609 (u32)p->crc32, (u32)p->szCompressed, in zipfileFunctionCds()
8610 (u32)p->szUncompressed, (u32)p->nFile, in zipfileFunctionCds()
8611 (u32)p->nExtra, (u32)p->nComment, in zipfileFunctionCds()
8612 (u32)p->iDiskStart, (u32)p->iInternalAttr, in zipfileFunctionCds()
8613 (u32)p->iExternalAttr, (u32)p->iOffset in zipfileFunctionCds()
8619 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT); in zipfileFunctionCds()
8658 if( pBuf->n+nByte>pBuf->nAlloc ){ in zipfileBufferGrow()
8660 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512; in zipfileBufferGrow()
8661 int nReq = pBuf->n + nByte; in zipfileBufferGrow()
8664 aNew = sqlite3_realloc64(pBuf->a, nNew); in zipfileBufferGrow()
8666 pBuf->a = aNew; in zipfileBufferGrow()
8667 pBuf->nAlloc = (int)nNew; in zipfileBufferGrow()
8695 int iMethod = -1; /* Compression method to use (0 or 8) */ in zipfileStep()
8734 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL"); in zipfileStep()
8787 if( nName>0 && zName[nName-1]=='/' ){ in zipfileStep()
8788 zErr = sqlite3_mprintf("non-directory name must not end with /"); in zipfileStep()
8793 if( nName==0 || zName[nName-1]!='/' ){ in zipfileStep()
8801 while( nName>1 && zName[nName-2]=='/' ) nName--; in zipfileStep()
8815 e.cds.iOffset = p->body.n; in zipfileStep()
8821 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out; in zipfileStep()
8822 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]); in zipfileStep()
8826 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out; in zipfileStep()
8827 memcpy(&p->body.a[p->body.n], aData, nData); in zipfileStep()
8828 p->body.n += nData; in zipfileStep()
8833 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out; in zipfileStep()
8834 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]); in zipfileStep()
8837 p->nEntry++; in zipfileStep()
8844 sqlite3_result_error(pCtx, zErr, -1); in zipfileStep()
8863 if( p->nEntry>0 ){ in zipfileFinal()
8865 eocd.nEntry = (u16)p->nEntry; in zipfileFinal()
8866 eocd.nEntryTotal = (u16)p->nEntry; in zipfileFinal()
8867 eocd.nSize = p->cds.n; in zipfileFinal()
8868 eocd.iOffset = p->body.n; in zipfileFinal()
8870 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ; in zipfileFinal()
8875 memcpy(aZip, p->body.a, p->body.n); in zipfileFinal()
8876 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n); in zipfileFinal()
8877 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]); in zipfileFinal()
8882 sqlite3_free(p->body.a); in zipfileFinal()
8883 sqlite3_free(p->cds.a); in zipfileFinal()
8890 static int zipfileRegister(sqlite3 *db){ in zipfileRegister() argument
8898 zipfileOpen, /* xOpen - open a cursor */ in zipfileRegister()
8899 zipfileClose, /* xClose - close a cursor */ in zipfileRegister()
8900 zipfileFilter, /* xFilter - configure scan constraints */ in zipfileRegister()
8901 zipfileNext, /* xNext - advance a cursor */ in zipfileRegister()
8902 zipfileEof, /* xEof - check for end of scan */ in zipfileRegister()
8903 zipfileColumn, /* xColumn - read data */ in zipfileRegister()
8904 0, /* xRowid - read data */ in zipfileRegister()
8914 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0); in zipfileRegister()
8915 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1); in zipfileRegister()
8917 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, in zipfileRegister()
8935 sqlite3 *db, in sqlite3_zipfile_init() argument
8941 return zipfileRegister(db); in sqlite3_zipfile_init()
8947 ** 2017-12-17
8959 ** for working with sqlar archives and used by the shell tool's built-in
8975 ** contains a two-byte identification header and a four-byte checksum at
9000 sqlite3_result_error(context, "error in compress()", -1); in sqlarCompressFunc()
9040 sqlite3_result_error(context, "error in uncompress()", -1); in sqlarUncompressFunc()
9053 sqlite3 *db, in sqlite3_sqlar_init() argument
9060 rc = sqlite3_create_function(db, "sqlar_compress", 1, in sqlite3_sqlar_init()
9064 rc = sqlite3_create_function(db, "sqlar_uncompress", 2, in sqlite3_sqlar_init()
9097 ** an English-language error message. In this case it is the responsibility
9101 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
9117 ** the analysis - indexes are recommended based on the database schema only.
9141 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
9142 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
9177 ** possible to add further SQL statements to the object or to re-attempt
9195 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
9203 ** If an out-of-range value (less than zero or equal to or greater than the
9237 ** sqlite3-expert_new().
9302 ** any other type of single-ended range constraint on a column).
9348 ** An object of the following type is created for each unique table/write-op
9349 ** seen. The objects are stored in a singly-linked list beginning at
9384 char *zKey; /* nul-terminated key */
9385 char *zVal; /* nul-terminated value string */
9386 char *zVal2; /* nul-terminated value string 2 */
9400 sqlite3 *db; /* User database */ member
9401 sqlite3 *dbm; /* In-memory db for this analysis */
9447 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){ in idxHashClear()
9448 pNext = pEntry->pHashNext; in idxHashClear()
9449 sqlite3_free(pEntry->zVal2); in idxHashClear()
9470 ** If zKey is already present in the hash table, return non-zero and do
9485 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ in idxHashAdd()
9486 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ in idxHashAdd()
9492 pEntry->zKey = (char*)&pEntry[1]; in idxHashAdd()
9493 memcpy(pEntry->zKey, zKey, nKey); in idxHashAdd()
9495 pEntry->zVal = &pEntry->zKey[nKey+1]; in idxHashAdd()
9496 memcpy(pEntry->zVal, zVal, nVal); in idxHashAdd()
9498 pEntry->pHashNext = pHash->aHash[iHash]; in idxHashAdd()
9499 pHash->aHash[iHash] = pEntry; in idxHashAdd()
9501 pEntry->pNext = pHash->pFirst; in idxHashAdd()
9502 pHash->pFirst = pEntry; in idxHashAdd()
9509 ** hash-entry object.
9517 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ in idxHashFind()
9518 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ in idxHashFind()
9533 if( pEntry ) return pEntry->zVal; in idxHashSearch()
9539 ** variable to point to a copy of nul-terminated string zColl.
9548 pNew->zColl = (char*)&pNew[1]; in idxNewConstraint()
9549 memcpy(pNew->zColl, zColl, nColl+1); in idxNewConstraint()
9555 ** An error associated with database handle db has just occurred. Pass
9559 sqlite3 *db, /* Database handle */ in idxDatabaseError() argument
9562 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); in idxDatabaseError()
9569 sqlite3 *db, /* Database handle to compile against */ in idxPrepareStmt() argument
9574 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); in idxPrepareStmt()
9577 idxDatabaseError(db, pzErrmsg); in idxPrepareStmt()
9586 sqlite3 *db, /* Database handle to compile against */ in idxPrintfPrepareStmt() argument
9600 rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql); in idxPrintfPrepareStmt()
9629 assert( zIn[n-1]=='\'' ); in expertDequote()
9634 for(iIn=1; iIn<(n-1); iIn++){ in expertDequote()
9649 ** methods of the r-tree virtual table.
9651 ** argv[0] -> module name
9652 ** argv[1] -> database name
9653 ** argv[2] -> table name
9654 ** argv[...] -> column names...
9657 sqlite3 *db, in expertConnect() argument
9673 rc = sqlite3_declare_vtab(db, zCreateTable); in expertConnect()
9678 p->pExpert = pExpert; in expertConnect()
9679 p->pTab = pExpert->pTable; in expertConnect()
9680 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 ); in expertConnect()
9713 pScan->pTab = p->pTab; in expertBestIndex()
9714 pScan->pNextScan = p->pExpert->pScan; in expertBestIndex()
9715 p->pExpert->pScan = pScan; in expertBestIndex()
9718 for(i=0; i<pIdxInfo->nConstraint; i++){ in expertBestIndex()
9719 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i]; in expertBestIndex()
9720 if( pCons->usable in expertBestIndex()
9721 && pCons->iColumn>=0 in expertBestIndex()
9722 && p->pTab->aCol[pCons->iColumn].iPk==0 in expertBestIndex()
9723 && (pCons->op & opmask) in expertBestIndex()
9729 pNew->iCol = pCons->iColumn; in expertBestIndex()
9730 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in expertBestIndex()
9731 pNew->pNext = pScan->pEq; in expertBestIndex()
9732 pScan->pEq = pNew; in expertBestIndex()
9734 pNew->bRange = 1; in expertBestIndex()
9735 pNew->pNext = pScan->pRange; in expertBestIndex()
9736 pScan->pRange = pNew; in expertBestIndex()
9740 pIdxInfo->aConstraintUsage[i].argvIndex = n; in expertBestIndex()
9745 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){ in expertBestIndex()
9746 int iCol = pIdxInfo->aOrderBy[i].iColumn; in expertBestIndex()
9748 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl); in expertBestIndex()
9750 pNew->iCol = iCol; in expertBestIndex()
9751 pNew->bDesc = pIdxInfo->aOrderBy[i].desc; in expertBestIndex()
9752 pNew->pNext = pScan->pOrder; in expertBestIndex()
9753 pNew->pLink = pScan->pOrder; in expertBestIndex()
9754 pScan->pOrder = pNew; in expertBestIndex()
9761 pIdxInfo->estimatedCost = 1000000.0 / (n+1); in expertBestIndex()
9795 sqlite3_finalize(pCsr->pData); in expertClose()
9803 ** Return non-zero if the cursor does not currently point to a valid
9808 return pCsr->pData==0; in expertEof()
9818 assert( pCsr->pData ); in expertNext()
9819 rc = sqlite3_step(pCsr->pData); in expertNext()
9821 rc = sqlite3_finalize(pCsr->pData); in expertNext()
9822 pCsr->pData = 0; in expertNext()
9845 pVal = sqlite3_column_value(pCsr->pData, i); in expertColumn()
9861 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab); in expertFilter()
9862 sqlite3expert *pExpert = pVtab->pExpert; in expertFilter()
9869 rc = sqlite3_finalize(pCsr->pData); in expertFilter()
9870 pCsr->pData = 0; in expertFilter()
9872 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg, in expertFilter()
9873 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName in expertFilter()
9886 expertConnect, /* xCreate - create a table */ in idxRegisterVtab()
9887 expertConnect, /* xConnect - connect to an existing table */ in idxRegisterVtab()
9888 expertBestIndex, /* xBestIndex - Determine search strategy */ in idxRegisterVtab()
9889 expertDisconnect, /* xDisconnect - Disconnect from a table */ in idxRegisterVtab()
9890 expertDisconnect, /* xDestroy - Drop a table */ in idxRegisterVtab()
9891 expertOpen, /* xOpen - open a cursor */ in idxRegisterVtab()
9892 expertClose, /* xClose - close a cursor */ in idxRegisterVtab()
9893 expertFilter, /* xFilter - configure scan constraints */ in idxRegisterVtab()
9894 expertNext, /* xNext - advance a cursor */ in idxRegisterVtab()
9896 expertColumn, /* xColumn - read data */ in idxRegisterVtab()
9897 expertRowid, /* xRowid - read data */ in idxRegisterVtab()
9898 expertUpdate, /* xUpdate - write data */ in idxRegisterVtab()
9899 0, /* xBegin - begin transaction */ in idxRegisterVtab()
9900 0, /* xSync - sync transaction */ in idxRegisterVtab()
9901 0, /* xCommit - commit transaction */ in idxRegisterVtab()
9902 0, /* xRollback - rollback transaction */ in idxRegisterVtab()
9903 0, /* xFindFunction - function overloading */ in idxRegisterVtab()
9904 0, /* xRename - rename the table */ in idxRegisterVtab()
9911 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p); in idxRegisterVtab()
9928 ** in the main database of connection db. If successful, set (*ppOut) to
9937 sqlite3 *db, /* Database connection to read details from */ in idxGetTableInfo() argument
9951 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab); in idxGetTableInfo()
9957 db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0 in idxGetTableInfo()
9972 pNew->aCol = (IdxColumn*)&pNew[1]; in idxGetTableInfo()
9973 pNew->nCol = nCol; in idxGetTableInfo()
9974 pCsr = (char*)&pNew->aCol[nCol]; in idxGetTableInfo()
9982 pNew->aCol[nCol].zName = pCsr; in idxGetTableInfo()
9983 pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1); in idxGetTableInfo()
9988 db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0 in idxGetTableInfo()
9993 pNew->aCol[nCol].zColl = pCsr; in idxGetTableInfo()
10006 pNew->zName = pCsr; in idxGetTableInfo()
10007 if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1); in idxGetTableInfo()
10015 ** This function is a no-op if *pRc is set to anything other than
10080 IdxColumn *p = &pTab->aCol[pCons->iCol]; in idxAppendColDefn()
10083 if( idxIdentifierRequiresQuotes(p->zName) ){ in idxAppendColDefn()
10084 zRet = idxAppendText(pRc, zRet, "%Q", p->zName); in idxAppendColDefn()
10086 zRet = idxAppendText(pRc, zRet, "%s", p->zName); in idxAppendColDefn()
10089 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){ in idxAppendColDefn()
10090 if( idxIdentifierRequiresQuotes(pCons->zColl) ){ in idxAppendColDefn()
10091 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl); in idxAppendColDefn()
10093 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl); in idxAppendColDefn()
10097 if( pCons->bDesc ){ in idxAppendColDefn()
10106 ** such an index is found, return non-zero. Or, if no such index is found,
10118 const char *zTbl = pScan->pTab->zName; in idxFindCompatible()
10125 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++; in idxFindCompatible()
10135 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0; in idxFindCompatible()
10144 for(pIter=pEq; pIter; pIter=pIter->pLink){ in idxFindCompatible()
10145 if( pIter->bFlag ) continue; in idxFindCompatible()
10146 if( pIter->iCol!=iCol ) continue; in idxFindCompatible()
10147 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue; in idxFindCompatible()
10148 pIter->bFlag = 1; in idxFindCompatible()
10157 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){ in idxFindCompatible()
10161 pT = pT->pLink; in idxFindCompatible()
10197 sqlite3 *dbm = p->dbm; in idxCreateFromCons()
10200 IdxTable *pTab = pScan->pTab; in idxCreateFromCons()
10207 for(pCons=pEq; pCons; pCons=pCons->pLink){ in idxCreateFromCons()
10210 for(pCons=pTail; pCons; pCons=pCons->pLink){ in idxCreateFromCons()
10216 const char *zTable = pScan->pTab->zName; in idxCreateFromCons()
10258 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg); in idxCreateFromCons()
10262 idxHashAdd(&rc, &p->hIdx, zName, zIdx); in idxCreateFromCons()
10281 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){ in idxFindConstraint()
10282 if( p->iCol==pCmp->iCol ) return 1; in idxFindConstraint()
10297 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){ in idxCreateFromWhere()
10299 pCon->pLink = p1; in idxCreateFromWhere()
10311 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){ in idxCreateFromWhere()
10312 assert( pCon->pLink==0 ); in idxCreateFromWhere()
10324 ** linked-list pScan.
10330 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){ in idxCreateCandidates()
10332 if( rc==SQLITE_OK && pIter->pOrder ){ in idxCreateCandidates()
10333 rc = idxCreateFromWhere(p, pIter, pIter->pOrder); in idxCreateCandidates()
10348 pNext = p->pNext; in idxConstraintFree()
10361 pNext = p->pNextScan; in idxScanFree()
10362 idxConstraintFree(p->pOrder); in idxScanFree()
10363 idxConstraintFree(p->pEq); in idxScanFree()
10364 idxConstraintFree(p->pRange); in idxScanFree()
10377 pNext = p->pNext; in idxStatementFree()
10378 sqlite3_free(p->zEQP); in idxStatementFree()
10379 sqlite3_free(p->zIdx); in idxStatementFree()
10391 pNext = pIter->pNext; in idxTableFree()
10403 pNext = pIter->pNext; in idxWriteFree()
10420 sqlite3 *dbm = p->dbm; in idxFindIndexes()
10426 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){ in idxFindIndexes()
10431 "EXPLAIN QUERY PLAN %s", pStmt->zSql in idxFindIndexes()
10459 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx); in idxFindIndexes()
10468 if( zDetail[0]!='-' ){ in idxFindIndexes()
10469 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail); in idxFindIndexes()
10473 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ in idxFindIndexes()
10474 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey); in idxFindIndexes()
10500 for(pTab=p->pTable; pTab; pTab=pTab->pNext){ in idxAuthCallback()
10501 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break; in idxAuthCallback()
10505 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){ in idxAuthCallback()
10506 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break; in idxAuthCallback()
10511 pWrite->pTab = pTab; in idxAuthCallback()
10512 pWrite->eOp = eOp; in idxAuthCallback()
10513 pWrite->pNext = p->pWrite; in idxAuthCallback()
10514 p->pWrite = pWrite; in idxAuthCallback()
10530 IdxTable *pTab = pWrite->pTab; in idxProcessOneTrigger()
10531 const char *zTab = pTab->zName; in idxProcessOneTrigger()
10541 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab); in idxProcessOneTrigger()
10544 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr); in idxProcessOneTrigger()
10554 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr); in idxProcessOneTrigger()
10559 switch( pWrite->eOp ){ in idxProcessOneTrigger()
10563 for(i=0; i<pTab->nCol; i++){ in idxProcessOneTrigger()
10572 for(i=0; i<pTab->nCol; i++){ in idxProcessOneTrigger()
10574 pTab->aCol[i].zName in idxProcessOneTrigger()
10580 assert( pWrite->eOp==SQLITE_DELETE ); in idxProcessOneTrigger()
10590 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0); in idxProcessOneTrigger()
10593 idxDatabaseError(p->dbv, pzErr); in idxProcessOneTrigger()
10599 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr); in idxProcessOneTrigger()
10608 IdxWrite *pFirst = p->pWrite; in idxProcessTriggers()
10612 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){ in idxProcessTriggers()
10616 pFirst = p->pWrite; in idxProcessTriggers()
10627 /* For each table in the main db schema: in idxCreateVtabSchema()
10629 ** 1) Add an entry to the p->pTable list, and in idxCreateVtabSchema()
10632 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg, in idxCreateVtabSchema()
10647 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg); in idxCreateVtabSchema()
10650 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg); in idxCreateVtabSchema()
10655 pTab->pNext = p->pTable; in idxCreateVtabSchema()
10656 p->pTable = pTab; in idxCreateVtabSchema()
10660 for(i=0; i<pTab->nCol; i++){ in idxCreateVtabSchema()
10662 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl in idxCreateVtabSchema()
10672 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg); in idxCreateVtabSchema()
10700 if( p->nRow==0.0 ){ in idxSampleFunc()
10703 bRet = (p->nRet / p->nRow) <= p->target; in idxSampleFunc()
10707 bRet = ((int)rnd % 100) <= p->iTarget; in idxSampleFunc()
10712 p->nRow += 1.0; in idxSampleFunc()
10713 p->nRet += (double)bRet; in idxSampleFunc()
10742 assert( iSlot<=p->nSlot ); in idxRemFunc()
10743 pSlot = &p->aSlot[iSlot]; in idxRemFunc()
10745 switch( pSlot->eType ){ in idxRemFunc()
10747 /* no-op */ in idxRemFunc()
10751 sqlite3_result_int64(pCtx, pSlot->iVal); in idxRemFunc()
10755 sqlite3_result_double(pCtx, pSlot->rVal); in idxRemFunc()
10759 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT); in idxRemFunc()
10763 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT); in idxRemFunc()
10767 pSlot->eType = sqlite3_value_type(argv[1]); in idxRemFunc()
10768 switch( pSlot->eType ){ in idxRemFunc()
10770 /* no-op */ in idxRemFunc()
10774 pSlot->iVal = sqlite3_value_int64(argv[1]); in idxRemFunc()
10778 pSlot->rVal = sqlite3_value_double(argv[1]); in idxRemFunc()
10784 if( nByte>pSlot->nByte ){ in idxRemFunc()
10785 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2); in idxRemFunc()
10790 pSlot->nByte = nByte*2; in idxRemFunc()
10791 pSlot->z = zNew; in idxRemFunc()
10793 pSlot->n = nByte; in idxRemFunc()
10794 if( pSlot->eType==SQLITE_BLOB ){ in idxRemFunc()
10795 memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte); in idxRemFunc()
10797 memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte); in idxRemFunc()
10804 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){ in idxLargestIndex() argument
10815 rc = idxPrepareStmt(db, &pMax, pzErr, zMax); in idxLargestIndex()
10841 assert( p->iSample>0 ); in idxPopulateOneStat1()
10844 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC); in idxPopulateOneStat1()
10856 if( p->iSample==100 ){ in idxPopulateOneStat1()
10871 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); in idxPopulateOneStat1()
10888 for(/*no-op*/; i<nCol; i++){ in idxPopulateOneStat1()
10903 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC); in idxPopulateOneStat1()
10904 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC); in idxPopulateOneStat1()
10905 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC); in idxPopulateOneStat1()
10910 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx)); in idxPopulateOneStat1()
10912 assert( pEntry->zVal2==0 ); in idxPopulateOneStat1()
10913 pEntry->zVal2 = zStat; in idxPopulateOneStat1()
10928 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); in idxBuildSampleTable()
10935 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0); in idxBuildSampleTable()
10954 i64 iPrev = -100000; in idxPopulateStat1()
10969 if( p->iSample==0 ) return SQLITE_OK; in idxPopulateStat1()
10971 rc = idxLargestIndex(p->dbm, &nMax, pzErr); in idxPopulateStat1()
10974 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0); in idxPopulateStat1()
10982 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); in idxPopulateStat1()
10989 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0 in idxPopulateStat1()
10994 pCtx->nSlot = nMax+1; in idxPopulateStat1()
10995 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex); in idxPopulateStat1()
10998 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo); in idxPopulateStat1()
11001 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite); in idxPopulateStat1()
11008 if( p->iSample<100 && iPrev!=iRowid ){ in idxPopulateStat1()
11009 samplectx.target = (double)p->iSample / 100.0; in idxPopulateStat1()
11010 samplectx.iTarget = p->iSample; in idxPopulateStat1()
11019 if( rc==SQLITE_OK && p->iSample<100 ){ in idxPopulateStat1()
11020 rc = sqlite3_exec(p->dbv, in idxPopulateStat1()
11030 for(i=0; i<pCtx->nSlot; i++){ in idxPopulateStat1()
11031 sqlite3_free(pCtx->aSlot[i].z); in idxPopulateStat1()
11037 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0); in idxPopulateStat1()
11040 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); in idxPopulateStat1()
11047 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){ in sqlite3_expert_new() argument
11053 /* Open two in-memory databases to work with. The "vtab database" (dbv) in sqlite3_expert_new()
11060 pNew->db = db; in sqlite3_expert_new()
11061 pNew->iSample = 100; in sqlite3_expert_new()
11062 rc = sqlite3_open(":memory:", &pNew->dbv); in sqlite3_expert_new()
11065 rc = sqlite3_open(":memory:", &pNew->dbm); in sqlite3_expert_new()
11067 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0); in sqlite3_expert_new()
11072 /* Copy the entire schema of database [db] into [dbm]. */ in sqlite3_expert_new()
11075 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, in sqlite3_expert_new()
11081 rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg); in sqlite3_expert_new()
11093 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew); in sqlite3_expert_new()
11117 p->iSample = iVal; in sqlite3_expert_config()
11137 IdxScan *pScanOrig = p->pScan; in sqlite3_expert_sql()
11138 IdxStatement *pStmtOrig = p->pStatement; in sqlite3_expert_sql()
11142 if( p->bRun ) return SQLITE_MISUSE; in sqlite3_expert_sql()
11146 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt); in sqlite3_expert_sql()
11154 pNew->zSql = (char*)&pNew[1]; in sqlite3_expert_sql()
11155 memcpy(pNew->zSql, z, n+1); in sqlite3_expert_sql()
11156 pNew->pNext = p->pStatement; in sqlite3_expert_sql()
11157 if( p->pStatement ) pNew->iId = p->pStatement->iId+1; in sqlite3_expert_sql()
11158 p->pStatement = pNew; in sqlite3_expert_sql()
11163 idxDatabaseError(p->dbv, pzErr); in sqlite3_expert_sql()
11168 idxScanFree(p->pScan, pScanOrig); in sqlite3_expert_sql()
11169 idxStatementFree(p->pStatement, pStmtOrig); in sqlite3_expert_sql()
11170 p->pScan = pScanOrig; in sqlite3_expert_sql()
11171 p->pStatement = pStmtOrig; in sqlite3_expert_sql()
11184 /* Create candidate indexes within the in-memory database file */ in sqlite3_expert_analyze()
11199 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ in sqlite3_expert_analyze()
11200 p->zCandidates = idxAppendText(&rc, p->zCandidates, in sqlite3_expert_analyze()
11201 "%s;%s%s\n", pEntry->zVal, in sqlite3_expert_analyze()
11202 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2 in sqlite3_expert_analyze()
11213 p->bRun = 1; in sqlite3_expert_analyze()
11224 if( p->pStatement ) nRet = p->pStatement->iId+1; in sqlite3_expert_count()
11235 if( p->bRun==0 ) return 0; in sqlite3_expert_report()
11236 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext); in sqlite3_expert_report()
11239 if( pStmt ) zRet = pStmt->zSql; in sqlite3_expert_report()
11242 if( pStmt ) zRet = pStmt->zIdx; in sqlite3_expert_report()
11245 if( pStmt ) zRet = pStmt->zEQP; in sqlite3_expert_report()
11248 zRet = p->zCandidates; in sqlite3_expert_report()
11259 sqlite3_close(p->dbm); in sqlite3_expert_destroy()
11260 sqlite3_close(p->dbv); in sqlite3_expert_destroy()
11261 idxScanFree(p->pScan, 0); in sqlite3_expert_destroy()
11262 idxStatementFree(p->pStatement, 0); in sqlite3_expert_destroy()
11263 idxTableFree(p->pTable); in sqlite3_expert_destroy()
11264 idxWriteFree(p->pWrite); in sqlite3_expert_destroy()
11265 idxHashClear(&p->hIdx); in sqlite3_expert_destroy()
11266 sqlite3_free(p->zCandidates); in sqlite3_expert_destroy()
11278 ** 2019-04-17
11294 ** sqlite_dbdata is used to extract data directly from a database b-tree
11295 ** page and its associated overflow pages, bypassing the b-tree layer.
11307 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
11311 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
11314 ** each cell on the page. For intkey b-trees, the key value is stored in
11315 ** field -1.
11319 ** CREATE TABLE t1(a, b); -- root page is page 2
11327 ** (2, 0, -1, 5 ),
11330 ** (2, 1, -1, 10 ),
11347 ** It contains one entry for each b-tree pointer between a parent and
11393 sqlite3 *db; /* The database connection */ member
11429 sqlite3 *db, in dbdataConnect() argument
11436 int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA); in dbdataConnect()
11444 pTab->db = db; in dbdataConnect()
11445 pTab->bPtr = (pAux!=0); in dbdataConnect()
11459 sqlite3_finalize(pTab->pStmt); in dbdataDisconnect()
11481 int iSchema = -1; in dbdataBestIndex()
11482 int iPgno = -1; in dbdataBestIndex()
11483 int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA); in dbdataBestIndex()
11485 for(i=0; i<pIdx->nConstraint; i++){ in dbdataBestIndex()
11486 struct sqlite3_index_constraint *p = &pIdx->aConstraint[i]; in dbdataBestIndex()
11487 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in dbdataBestIndex()
11488 if( p->iColumn==colSchema ){ in dbdataBestIndex()
11489 if( p->usable==0 ) return SQLITE_CONSTRAINT; in dbdataBestIndex()
11492 if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){ in dbdataBestIndex()
11499 pIdx->aConstraintUsage[iSchema].argvIndex = 1; in dbdataBestIndex()
11500 pIdx->aConstraintUsage[iSchema].omit = 1; in dbdataBestIndex()
11503 pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0); in dbdataBestIndex()
11504 pIdx->aConstraintUsage[iPgno].omit = 1; in dbdataBestIndex()
11505 pIdx->estimatedCost = 100; in dbdataBestIndex()
11506 pIdx->estimatedRows = 50; in dbdataBestIndex()
11508 if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){ in dbdataBestIndex()
11509 int iCol = pIdx->aOrderBy[0].iColumn; in dbdataBestIndex()
11510 if( pIdx->nOrderBy==1 ){ in dbdataBestIndex()
11511 pIdx->orderByConsumed = (iCol==0 || iCol==1); in dbdataBestIndex()
11512 }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){ in dbdataBestIndex()
11513 pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1); in dbdataBestIndex()
11518 pIdx->estimatedCost = 100000000; in dbdataBestIndex()
11519 pIdx->estimatedRows = 1000000000; in dbdataBestIndex()
11521 pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00); in dbdataBestIndex()
11536 pCsr->base.pVtab = pVTab; in dbdataOpen()
11548 DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab); in dbdataResetCursor()
11549 if( pTab->pStmt==0 ){ in dbdataResetCursor()
11550 pTab->pStmt = pCsr->pStmt; in dbdataResetCursor()
11552 sqlite3_finalize(pCsr->pStmt); in dbdataResetCursor()
11554 pCsr->pStmt = 0; in dbdataResetCursor()
11555 pCsr->iPgno = 1; in dbdataResetCursor()
11556 pCsr->iCell = 0; in dbdataResetCursor()
11557 pCsr->iField = 0; in dbdataResetCursor()
11558 pCsr->bOnePage = 0; in dbdataResetCursor()
11559 sqlite3_free(pCsr->aPage); in dbdataResetCursor()
11560 sqlite3_free(pCsr->pRec); in dbdataResetCursor()
11561 pCsr->pRec = 0; in dbdataResetCursor()
11562 pCsr->aPage = 0; in dbdataResetCursor()
11576 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
11606 sqlite3_stmt *pStmt = pCsr->pStmt; in dbdataLoadPage()
11672 return ((eType-12) / 2); in dbdataValueBytes()
11726 int n = ((eType-12) / 2); in dbdataValue()
11742 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; in dbdataNext()
11744 pCsr->iRowid++; in dbdataNext()
11747 int iOff = (pCsr->iPgno==1 ? 100 : 0); in dbdataNext()
11750 if( pCsr->aPage==0 ){ in dbdataNext()
11752 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK; in dbdataNext()
11753 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage); in dbdataNext()
11755 if( pCsr->aPage ) break; in dbdataNext()
11756 pCsr->iPgno++; in dbdataNext()
11758 pCsr->iCell = pTab->bPtr ? -2 : 0; in dbdataNext()
11759 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]); in dbdataNext()
11762 if( pTab->bPtr ){ in dbdataNext()
11763 if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){ in dbdataNext()
11764 pCsr->iCell = pCsr->nCell; in dbdataNext()
11766 pCsr->iCell++; in dbdataNext()
11767 if( pCsr->iCell>=pCsr->nCell ){ in dbdataNext()
11768 sqlite3_free(pCsr->aPage); in dbdataNext()
11769 pCsr->aPage = 0; in dbdataNext()
11770 if( pCsr->bOnePage ) return SQLITE_OK; in dbdataNext()
11771 pCsr->iPgno++; in dbdataNext()
11777 if( pCsr->pRec==0 ){ in dbdataNext()
11786 switch( pCsr->aPage[iOff] ){ in dbdataNext()
11796 /* This is not a b-tree page with records on it. Continue. */ in dbdataNext()
11797 pCsr->iCell = pCsr->nCell; in dbdataNext()
11801 if( pCsr->iCell>=pCsr->nCell ){ in dbdataNext()
11805 iOff += 8 + nPointer + pCsr->iCell*2; in dbdataNext()
11806 if( iOff>pCsr->nPage ){ in dbdataNext()
11809 iOff = get_uint16(&pCsr->aPage[iOff]); in dbdataNext()
11812 /* For an interior node cell, skip past the child-page number */ in dbdataNext()
11816 if( bNextPage || iOff>pCsr->nPage ){ in dbdataNext()
11819 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload); in dbdataNext()
11823 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){ in dbdataNext()
11824 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey); in dbdataNext()
11828 U = pCsr->nPage; in dbdataNext()
11830 X = U-35; in dbdataNext()
11832 X = ((U-12)*64/255)-23; in dbdataNext()
11838 M = ((U-12)*32/255)-23; in dbdataNext()
11839 K = M+((nPayload-M)%(U-4)); in dbdataNext()
11847 if( bNextPage || nLocal+iOff>pCsr->nPage ){ in dbdataNext()
11854 pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES); in dbdataNext()
11855 if( pCsr->pRec==0 ) return SQLITE_NOMEM; in dbdataNext()
11856 memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES); in dbdataNext()
11857 pCsr->nRec = nPayload; in dbdataNext()
11860 memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal); in dbdataNext()
11865 sqlite3_int64 nRem = nPayload - nLocal; in dbdataNext()
11866 unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]); in dbdataNext()
11872 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage ); in dbdataNext()
11876 nCopy = U-4; in dbdataNext()
11878 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy); in dbdataNext()
11879 nRem -= nCopy; in dbdataNext()
11886 iHdr = dbdataGetVarint(pCsr->pRec, &nHdr); in dbdataNext()
11887 pCsr->nHdr = nHdr; in dbdataNext()
11888 pCsr->pHdrPtr = &pCsr->pRec[iHdr]; in dbdataNext()
11889 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr]; in dbdataNext()
11890 pCsr->iField = (bHasRowid ? -1 : 0); in dbdataNext()
11894 pCsr->iField++; in dbdataNext()
11895 if( pCsr->iField>0 ){ in dbdataNext()
11897 if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){ in dbdataNext()
11900 pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType); in dbdataNext()
11901 pCsr->pPtr += dbdataValueBytes(iType); in dbdataNext()
11907 sqlite3_free(pCsr->aPage); in dbdataNext()
11908 sqlite3_free(pCsr->pRec); in dbdataNext()
11909 pCsr->aPage = 0; in dbdataNext()
11910 pCsr->pRec = 0; in dbdataNext()
11911 if( pCsr->bOnePage ) return SQLITE_OK; in dbdataNext()
11912 pCsr->iPgno++; in dbdataNext()
11914 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){ in dbdataNext()
11920 sqlite3_free(pCsr->pRec); in dbdataNext()
11921 pCsr->pRec = 0; in dbdataNext()
11922 pCsr->iCell++; in dbdataNext()
11936 return pCsr->aPage==0; in dbdataEof()
11942 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
11946 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab; in dbdataDbsize()
11953 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0); in dbdataDbsize()
11956 pCsr->szDb = sqlite3_column_int(pStmt, 0); in dbdataDbsize()
11972 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; in dbdataFilter()
11977 assert( pCsr->iPgno==1 ); in dbdataFilter()
11982 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]); in dbdataFilter()
11983 pCsr->bOnePage = 1; in dbdataFilter()
11985 pCsr->nPage = dbdataDbsize(pCsr, zSchema); in dbdataFilter()
11990 if( pTab->pStmt ){ in dbdataFilter()
11991 pCsr->pStmt = pTab->pStmt; in dbdataFilter()
11992 pTab->pStmt = 0; in dbdataFilter()
11994 rc = sqlite3_prepare_v2(pTab->db, in dbdataFilter()
11995 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1, in dbdataFilter()
11996 &pCsr->pStmt, 0 in dbdataFilter()
12001 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT); in dbdataFilter()
12003 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); in dbdataFilter()
12020 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; in dbdataColumn()
12021 if( pTab->bPtr ){ in dbdataColumn()
12024 sqlite3_result_int64(ctx, pCsr->iPgno); in dbdataColumn()
12027 int iOff = pCsr->iPgno==1 ? 100 : 0; in dbdataColumn()
12028 if( pCsr->iCell<0 ){ in dbdataColumn()
12031 iOff += 12 + pCsr->iCell*2; in dbdataColumn()
12032 if( iOff>pCsr->nPage ) return SQLITE_OK; in dbdataColumn()
12033 iOff = get_uint16(&pCsr->aPage[iOff]); in dbdataColumn()
12035 if( iOff<=pCsr->nPage ){ in dbdataColumn()
12036 sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff])); in dbdataColumn()
12044 sqlite3_result_int64(ctx, pCsr->iPgno); in dbdataColumn()
12047 sqlite3_result_int(ctx, pCsr->iCell); in dbdataColumn()
12050 sqlite3_result_int(ctx, pCsr->iField); in dbdataColumn()
12053 if( pCsr->iField<0 ){ in dbdataColumn()
12054 sqlite3_result_int64(ctx, pCsr->iIntkey); in dbdataColumn()
12057 dbdataGetVarint(pCsr->pHdrPtr, &iType); in dbdataColumn()
12059 ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr in dbdataColumn()
12074 *pRowid = pCsr->iRowid; in dbdataRowid()
12082 static int sqlite3DbdataRegister(sqlite3 *db){ in sqlite3DbdataRegister() argument
12090 dbdataOpen, /* xOpen - open a cursor */ in sqlite3DbdataRegister()
12091 dbdataClose, /* xClose - close a cursor */ in sqlite3DbdataRegister()
12092 dbdataFilter, /* xFilter - configure scan constraints */ in sqlite3DbdataRegister()
12093 dbdataNext, /* xNext - advance a cursor */ in sqlite3DbdataRegister()
12094 dbdataEof, /* xEof - check for end of scan */ in sqlite3DbdataRegister()
12095 dbdataColumn, /* xColumn - read data */ in sqlite3DbdataRegister()
12096 dbdataRowid, /* xRowid - read data */ in sqlite3DbdataRegister()
12110 int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0); in sqlite3DbdataRegister()
12112 rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1); in sqlite3DbdataRegister()
12121 sqlite3 *db, in sqlite3_dbdata_init() argument
12126 return sqlite3DbdataRegister(db); in sqlite3_dbdata_init()
12174 sqlite3 *db; /* The database */ member
12181 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
12185 u8 bSafeModePersist; /* The long-term value of bSafeMode */
12208 sqlite3_int64 szMax; /* --maxsize argument to .open */
12225 sqlite3 *db; /* Connection pointer */ member
12237 char *zNonce; /* Nonce for temporary safe-mode excapes */
12252 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
12256 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
12270 ** top-level SQL statement */
12271 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
12276 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
12278 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
12280 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
12282 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
12290 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
12291 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
12292 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
12304 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
12308 #define MODE_Pretty 11 /* Pretty-print schemas */
12312 #define MODE_Table 15 /* MySQL-style table formatting */
12313 #define MODE_Box 16 /* Unicode box-drawing characters */
12353 if( p->pLog==0 ) return; in shellLog()
12354 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); in shellLog()
12355 fflush(p->pLog); in shellLog()
12371 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0])); in shellPutsFunc()
12384 if( p->bSafeMode ){ in failIfSafeMode()
12390 raw_printf(stderr, "line %d: ", p->lineno); in failIfSafeMode()
12410 ** Also throw an error if the EDITOR program returns a non-zero exit code.
12420 sqlite3 *db; in editFunc() local
12436 sqlite3_result_error(context, "no editor for edit()", -1); in editFunc()
12440 sqlite3_result_error(context, "NULL input to edit()", -1); in editFunc()
12443 db = sqlite3_context_db_handle(context); in editFunc()
12445 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); in editFunc()
12460 sqlite3_result_error(context, "edit() cannot open temp file", -1); in editFunc()
12475 sqlite3_result_error(context, "edit() could not write the whole file", -1); in editFunc()
12486 sqlite3_result_error(context, "EDITOR returned non-zero", -1); in editFunc()
12492 "edit() cannot reopen temp file after edit", -1); in editFunc()
12507 sqlite3_result_error(context, "could not read back the whole file", -1); in editFunc()
12543 p->modePrior = p->mode; in outputModePush()
12544 p->priorShFlgs = p->shellFlgs; in outputModePush()
12545 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); in outputModePush()
12546 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); in outputModePush()
12549 p->mode = p->modePrior; in outputModePop()
12550 p->shellFlgs = p->priorShFlgs; in outputModePop()
12551 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); in outputModePop()
12552 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); in outputModePop()
12556 ** Output the given string as a hex-encoded blob (eg. X'1234' )
12625 ** get corrupted by end-of-line translation facilities in some operating
12727 while( n-- ){ in output_json_string()
12813 ** Output a single term of CSV. Actually, p->colSeparator is used for
12814 ** the separator, which may or may not be a comma. p->nullValue is
12819 FILE *out = p->out; in output_csv()
12821 utf8_printf(out,"%s",p->nullValue); in output_csv()
12830 if( i==0 || strstr(z, p->colSeparator)!=0 ){ in output_csv()
12839 utf8_printf(p->out, "%s", p->colSeparator); in output_csv()
12844 ** This routine runs when the user presses Ctrl-C
12855 ** This routine runs for console events (e.g. Ctrl-C) on Win32
12944 utf8_printf(p->out, "authorizer: %s", azAction[op]); in shellAuth()
12946 raw_printf(p->out, " "); in shellAuth()
12948 output_c_string(p->out, az[i]); in shellAuth()
12950 raw_printf(p->out, "NULL"); in shellAuth()
12953 raw_printf(p->out, "\n"); in shellAuth()
12954 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4); in shellAuth()
12990 if( z[i]=='-' && z[i+1]=='-' ) return 1; in wsToEol()
13002 if( p->autoEQPtest ){ in eqp_append()
13003 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); in eqp_append()
13007 pNew->iEqpId = iEqpId; in eqp_append()
13008 pNew->iParentId = p2; in eqp_append()
13009 memcpy(pNew->zText, zText, nText+1); in eqp_append()
13010 pNew->pNext = 0; in eqp_append()
13011 if( p->sGraph.pLast ){ in eqp_append()
13012 p->sGraph.pLast->pNext = pNew; in eqp_append()
13014 p->sGraph.pRow = pNew; in eqp_append()
13016 p->sGraph.pLast = pNew; in eqp_append()
13021 ** in p->sGraph.
13025 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ in eqp_reset()
13026 pNext = pRow->pNext; in eqp_reset()
13029 memset(&p->sGraph, 0, sizeof(p->sGraph)); in eqp_reset()
13036 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; in eqp_next_row()
13037 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; in eqp_next_row()
13046 int n = strlen30(p->sGraph.zPrefix); in eqp_render_level()
13050 z = pRow->zText; in eqp_render_level()
13051 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, in eqp_render_level()
13052 pNext ? "|--" : "`--", z); in eqp_render_level()
13053 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){ in eqp_render_level()
13054 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); in eqp_render_level()
13055 eqp_render_level(p, pRow->iEqpId); in eqp_render_level()
13056 p->sGraph.zPrefix[n] = 0; in eqp_render_level()
13065 EQPGraphRow *pRow = p->sGraph.pRow; in eqp_render()
13067 if( pRow->zText[0]=='-' ){ in eqp_render()
13068 if( pRow->pNext==0 ){ in eqp_render()
13072 utf8_printf(p->out, "%s\n", pRow->zText+3); in eqp_render()
13073 p->sGraph.pRow = pRow->pNext; in eqp_render()
13076 utf8_printf(p->out, "QUERY PLAN\n"); in eqp_render()
13078 p->sGraph.zPrefix[0] = 0; in eqp_render()
13090 p->nProgress++; in progress_handler()
13091 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ in progress_handler()
13092 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress); in progress_handler()
13093 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; in progress_handler()
13094 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; in progress_handler()
13097 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ in progress_handler()
13098 raw_printf(p->out, "Progress %u\n", p->nProgress); in progress_handler()
13108 const char zDash[] = "--------------------------------------------------"; in print_dashes()
13109 const int nDash = sizeof(zDash) - 1; in print_dashes()
13112 N -= nDash; in print_dashes()
13118 ** Print a markdown or table-style row separator using ascii-art
13127 fputs(zSep, p->out); in print_row_separator()
13128 print_dashes(p->out, p->actualWidth[0]+2); in print_row_separator()
13130 fputs(zSep, p->out); in print_row_separator()
13131 print_dashes(p->out, p->actualWidth[i]+2); in print_row_separator()
13133 fputs(zSep, p->out); in print_row_separator()
13135 fputs("\n", p->out); in print_row_separator()
13153 switch( p->cMode ){ in shell_callback()
13161 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
13163 utf8_printf(p->out,"%*s = %s%s", w, azCol[i], in shell_callback()
13164 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); in shell_callback()
13173 if( p->cnt++==0 ){ in shell_callback()
13176 utf8_width_print(p->out, w, azCol[i]); in shell_callback()
13177 fputs(i==nArg-1 ? "\n" : " ", p->out); in shell_callback()
13181 print_dashes(p->out, w); in shell_callback()
13182 fputs(i==nArg-1 ? "\n" : " ", p->out); in shell_callback()
13188 if( i==nArg-1 ) w = 0; in shell_callback()
13192 if( i==1 && p->aiIndent && p->pStmt ){ in shell_callback()
13193 if( p->iIndent<p->nIndent ){ in shell_callback()
13194 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); in shell_callback()
13196 p->iIndent++; in shell_callback()
13198 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); in shell_callback()
13199 fputs(i==nArg-1 ? "\n" : " ", p->out); in shell_callback()
13204 printSchemaLine(p->out, azArg[0], ";\n"); in shell_callback()
13207 case MODE_Pretty: { /* .schema and .fullschema with --indent */ in shell_callback()
13219 utf8_printf(p->out, "%s;\n", azArg[0]); in shell_callback()
13227 if( z[j-1]=='\r' ) z[j-1] = '\n'; in shell_callback()
13228 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; in shell_callback()
13229 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ in shell_callback()
13230 j--; in shell_callback()
13234 while( j>0 && IsSpace(z[j-1]) ){ j--; } in shell_callback()
13244 }else if( c=='-' && z[i+1]=='-' ){ in shell_callback()
13249 nParen--; in shell_callback()
13251 printSchemaLineN(p->out, z, j, "\n"); in shell_callback()
13259 if( c=='\n' ) j--; in shell_callback()
13260 printSchemaLineN(p->out, z, j, "\n "); in shell_callback()
13268 printSchemaLine(p->out, z, ";\n"); in shell_callback()
13273 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
13275 utf8_printf(p->out,"%s%s",azCol[i], in shell_callback()
13276 i==nArg-1 ? p->rowSeparator : p->colSeparator); in shell_callback()
13282 if( z==0 ) z = p->nullValue; in shell_callback()
13283 utf8_printf(p->out, "%s", z); in shell_callback()
13284 if( i<nArg-1 ){ in shell_callback()
13285 utf8_printf(p->out, "%s", p->colSeparator); in shell_callback()
13287 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
13293 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
13294 raw_printf(p->out,"<TR>"); in shell_callback()
13296 raw_printf(p->out,"<TH>"); in shell_callback()
13297 output_html_string(p->out, azCol[i]); in shell_callback()
13298 raw_printf(p->out,"</TH>\n"); in shell_callback()
13300 raw_printf(p->out,"</TR>\n"); in shell_callback()
13303 raw_printf(p->out,"<TR>"); in shell_callback()
13305 raw_printf(p->out,"<TD>"); in shell_callback()
13306 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); in shell_callback()
13307 raw_printf(p->out,"</TD>\n"); in shell_callback()
13309 raw_printf(p->out,"</TR>\n"); in shell_callback()
13313 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
13315 output_c_string(p->out,azCol[i] ? azCol[i] : ""); in shell_callback()
13316 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); in shell_callback()
13318 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
13322 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); in shell_callback()
13323 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); in shell_callback()
13325 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
13329 setBinaryMode(p->out, 1); in shell_callback()
13330 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
13332 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); in shell_callback()
13334 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
13338 output_csv(p, azArg[i], i<nArg-1); in shell_callback()
13340 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
13342 setTextMode(p->out, 1); in shell_callback()
13347 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); in shell_callback()
13348 if( p->showHeader ){ in shell_callback()
13349 raw_printf(p->out,"("); in shell_callback()
13351 if( i>0 ) raw_printf(p->out, ","); in shell_callback()
13354 utf8_printf(p->out, "%s", z); in shell_callback()
13357 raw_printf(p->out, "%s", azCol[i]); in shell_callback()
13360 raw_printf(p->out,")"); in shell_callback()
13362 p->cnt++; in shell_callback()
13364 raw_printf(p->out, i>0 ? "," : " VALUES("); in shell_callback()
13366 utf8_printf(p->out,"NULL"); in shell_callback()
13369 output_quoted_string(p->out, azArg[i]); in shell_callback()
13371 output_quoted_escaped_string(p->out, azArg[i]); in shell_callback()
13374 utf8_printf(p->out,"%s", azArg[i]); in shell_callback()
13377 double r = sqlite3_column_double(p->pStmt, i); in shell_callback()
13381 raw_printf(p->out, "1e999"); in shell_callback()
13383 raw_printf(p->out, "-1e999"); in shell_callback()
13386 raw_printf(p->out, "%s", z); in shell_callback()
13388 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ in shell_callback()
13389 const void *pBlob = sqlite3_column_blob(p->pStmt, i); in shell_callback()
13390 int nBlob = sqlite3_column_bytes(p->pStmt, i); in shell_callback()
13391 output_hex_blob(p->out, pBlob, nBlob); in shell_callback()
13393 utf8_printf(p->out,"%s", azArg[i]); in shell_callback()
13395 output_quoted_string(p->out, azArg[i]); in shell_callback()
13397 output_quoted_escaped_string(p->out, azArg[i]); in shell_callback()
13400 raw_printf(p->out,");\n"); in shell_callback()
13405 if( p->cnt==0 ){ in shell_callback()
13406 fputs("[{", p->out); in shell_callback()
13408 fputs(",\n{", p->out); in shell_callback()
13410 p->cnt++; in shell_callback()
13412 output_json_string(p->out, azCol[i], -1); in shell_callback()
13413 putc(':', p->out); in shell_callback()
13415 fputs("null",p->out); in shell_callback()
13418 double r = sqlite3_column_double(p->pStmt, i); in shell_callback()
13422 raw_printf(p->out, "1e999"); in shell_callback()
13424 raw_printf(p->out, "-1e999"); in shell_callback()
13427 raw_printf(p->out, "%s", z); in shell_callback()
13429 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ in shell_callback()
13430 const void *pBlob = sqlite3_column_blob(p->pStmt, i); in shell_callback()
13431 int nBlob = sqlite3_column_bytes(p->pStmt, i); in shell_callback()
13432 output_json_string(p->out, pBlob, nBlob); in shell_callback()
13434 output_json_string(p->out, azArg[i], -1); in shell_callback()
13436 utf8_printf(p->out,"%s", azArg[i]); in shell_callback()
13438 if( i<nArg-1 ){ in shell_callback()
13439 putc(',', p->out); in shell_callback()
13442 putc('}', p->out); in shell_callback()
13447 if( p->cnt==0 && p->showHeader ){ in shell_callback()
13449 if( i>0 ) fputs(p->colSeparator, p->out); in shell_callback()
13450 output_quoted_string(p->out, azCol[i]); in shell_callback()
13452 fputs(p->rowSeparator, p->out); in shell_callback()
13454 p->cnt++; in shell_callback()
13456 if( i>0 ) fputs(p->colSeparator, p->out); in shell_callback()
13458 utf8_printf(p->out,"NULL"); in shell_callback()
13460 output_quoted_string(p->out, azArg[i]); in shell_callback()
13462 utf8_printf(p->out,"%s", azArg[i]); in shell_callback()
13465 double r = sqlite3_column_double(p->pStmt, i); in shell_callback()
13467 raw_printf(p->out, "%s", z); in shell_callback()
13468 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ in shell_callback()
13469 const void *pBlob = sqlite3_column_blob(p->pStmt, i); in shell_callback()
13470 int nBlob = sqlite3_column_bytes(p->pStmt, i); in shell_callback()
13471 output_hex_blob(p->out, pBlob, nBlob); in shell_callback()
13473 utf8_printf(p->out,"%s", azArg[i]); in shell_callback()
13475 output_quoted_string(p->out, azArg[i]); in shell_callback()
13478 fputs(p->rowSeparator, p->out); in shell_callback()
13482 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
13484 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); in shell_callback()
13485 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); in shell_callback()
13487 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
13491 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); in shell_callback()
13492 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); in shell_callback()
13494 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
13523 if( p->n ) appendText(p, "|", 0); in captureOutputCallback()
13536 sqlite3_exec(p->db, in createSelftestTable()
13547 " 'memo','Tests generated by --init');\n" in createSelftestTable()
13576 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); in createSelftestTable()
13590 if( p->zDestTable ){ in set_table_name()
13591 free(p->zDestTable); in set_table_name()
13592 p->zDestTable = 0; in set_table_name()
13598 z = p->zDestTable = malloc( n+1 ); in set_table_name()
13613 ** the result columns, comma-separated, on a line and then add a
13616 ** If the number of columns is 1 and that column contains text "--"
13618 ** "--" comment occurs at the end of the statement, the comment
13630 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); in run_table_dump_query()
13632 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, in run_table_dump_query()
13633 sqlite3_errmsg(p->db)); in run_table_dump_query()
13634 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; in run_table_dump_query()
13641 utf8_printf(p->out, "%s", z); in run_table_dump_query()
13643 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); in run_table_dump_query()
13646 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; in run_table_dump_query()
13648 raw_printf(p->out, "\n;\n"); in run_table_dump_query()
13650 raw_printf(p->out, ";\n"); in run_table_dump_query()
13656 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, in run_table_dump_query()
13657 sqlite3_errmsg(p->db)); in run_table_dump_query()
13658 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; in run_table_dump_query()
13667 sqlite3 *db, /* Database to query */ in save_err_msg() argument
13673 return sqlite3_mprintf(zWhen, sqlite3_errmsg(db), rc); in save_err_msg()
13703 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); in displayLinuxIoStats()
13713 ** Display a single line of status using 64-bit values.
13722 sqlite3_int64 iCur = -1; in displayStatLine()
13723 sqlite3_int64 iHiwtr = -1; in displayStatLine()
13735 raw_printf(p->out, "%-36s %s\n", zLabel, zLine); in displayStatLine()
13742 sqlite3 *db, /* Database to query */ in display_stats() argument
13749 if( pArg==0 || pArg->out==0 ) return 0; in display_stats()
13750 out = pArg->out; in display_stats()
13752 if( pArg->pStmt && pArg->statsOn==2 ){ in display_stats()
13754 sqlite3_stmt *pStmt = pArg->pStmt; in display_stats()
13757 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol); in display_stats()
13760 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i)); in display_stats()
13763 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i)); in display_stats()
13767 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i)); in display_stats()
13769 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i)); in display_stats()
13771 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i)); in display_stats()
13776 if( pArg->statsOn==3 ){ in display_stats()
13777 if( pArg->pStmt ){ in display_stats()
13778 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); in display_stats()
13779 raw_printf(pArg->out, "VM-steps: %d\n", iCur); in display_stats()
13788 if( pArg->shellFlgs & SHFLG_Pagecache ){ in display_stats()
13803 if( db ){ in display_stats()
13804 if( pArg->shellFlgs & SHFLG_Lookaside ){ in display_stats()
13805 iHiwtr = iCur = -1; in display_stats()
13806 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, in display_stats()
13808 raw_printf(pArg->out, in display_stats()
13811 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, in display_stats()
13813 raw_printf(pArg->out, "Successful lookaside attempts: %d\n", in display_stats()
13815 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, in display_stats()
13817 raw_printf(pArg->out, "Lookaside failures due to size: %d\n", in display_stats()
13819 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, in display_stats()
13821 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", in display_stats()
13824 iHiwtr = iCur = -1; in display_stats()
13825 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); in display_stats()
13826 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", in display_stats()
13828 iHiwtr = iCur = -1; in display_stats()
13829 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); in display_stats()
13830 raw_printf(pArg->out, "Page cache hits: %d\n", iCur); in display_stats()
13831 iHiwtr = iCur = -1; in display_stats()
13832 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); in display_stats()
13833 raw_printf(pArg->out, "Page cache misses: %d\n", iCur); in display_stats()
13834 iHiwtr = iCur = -1; in display_stats()
13835 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); in display_stats()
13836 raw_printf(pArg->out, "Page cache writes: %d\n", iCur); in display_stats()
13837 iHiwtr = iCur = -1; in display_stats()
13838 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1); in display_stats()
13839 raw_printf(pArg->out, "Page cache spills: %d\n", iCur); in display_stats()
13840 iHiwtr = iCur = -1; in display_stats()
13841 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); in display_stats()
13842 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", in display_stats()
13844 iHiwtr = iCur = -1; in display_stats()
13845 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); in display_stats()
13846 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", in display_stats()
13850 if( pArg->pStmt ){ in display_stats()
13851 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, in display_stats()
13853 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); in display_stats()
13854 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); in display_stats()
13855 raw_printf(pArg->out, "Sort Operations: %d\n", iCur); in display_stats()
13856 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); in display_stats()
13857 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); in display_stats()
13858 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); in display_stats()
13859 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); in display_stats()
13860 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); in display_stats()
13861 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur); in display_stats()
13862 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); in display_stats()
13863 raw_printf(pArg->out, "Number of times run: %d\n", iCur); in display_stats()
13864 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); in display_stats()
13865 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur); in display_stats()
13869 displayLinuxIoStats(pArg->out); in display_stats()
13872 /* Do not remove this machine readable comment: extra-stats-output-here */ in display_stats()
13881 sqlite3 *db, /* Database to query */ in display_scanstats() argument
13885 UNUSED_PARAMETER(db); in display_scanstats()
13889 raw_printf(pArg->out, "-------- scanstats --------\n"); in display_scanstats()
13894 sqlite3_stmt *p = pArg->pStmt; in display_scanstats()
13907 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); in display_scanstats()
13913 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); in display_scanstats()
13915 raw_printf(pArg->out, in display_scanstats()
13916 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", in display_scanstats()
13921 raw_printf(pArg->out, "---------------------------\n"); in display_scanstats()
13926 ** Parameter azArray points to a zero-terminated array of strings. zStr
13927 ** points to a single nul-terminated string. Return non-zero if zStr
13961 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ in explain_data_prepare()
13962 int iOp; /* Index of operation in p->aiIndent[] */ in explain_data_prepare()
13972 p->cMode = p->mode; in explain_data_prepare()
13979 p->cMode = p->mode; in explain_data_prepare()
13991 ** the current instruction is part of a sub-program generated by an in explain_data_prepare()
13994 int p2op = (p2 + (iOp-iAddr)); in explain_data_prepare()
13996 /* Grow the p->aiIndent array as required */ in explain_data_prepare()
14006 p->cMode = p->mode; in explain_data_prepare()
14013 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); in explain_data_prepare()
14014 if( p->aiIndent==0 ) shell_out_of_memory(); in explain_data_prepare()
14019 p->aiIndent[iOp] = 0; in explain_data_prepare()
14020 p->nIndent = iOp+1; in explain_data_prepare()
14023 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; in explain_data_prepare()
14025 if( str_in_array(zOp, azGoto) && p2op<p->nIndent in explain_data_prepare()
14028 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; in explain_data_prepare()
14032 p->iIndent = 0; in explain_data_prepare()
14041 sqlite3_free(p->aiIndent); in explain_data_delete()
14042 p->aiIndent = 0; in explain_data_delete()
14043 p->nIndent = 0; in explain_data_delete()
14044 p->iIndent = 0; in explain_data_delete()
14068 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode); in bind_table_init()
14069 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); in bind_table_init()
14070 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); in bind_table_init()
14071 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); in bind_table_init()
14072 sqlite3_exec(p->db, in bind_table_init()
14078 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); in bind_table_init()
14079 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0); in bind_table_init()
14102 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", in bind_prepared_stmt()
14106 rc = sqlite3_prepare_v2(pArg->db, in bind_prepared_stmt()
14108 " WHERE key=?1", -1, &pQ, 0); in bind_prepared_stmt()
14117 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); in bind_prepared_stmt()
14129 ** UTF8 box-drawing characters. Imagine box lines like this:
14133 ** 4 --+-- 2
14141 #define BOX_24 "\342\224\200" /* U+2500 --- */
14143 #define BOX_23 "\342\224\214" /* U+250c ,- */
14144 #define BOX_34 "\342\224\220" /* U+2510 -, */
14145 #define BOX_12 "\342\224\224" /* U+2514 '- */
14146 #define BOX_14 "\342\224\230" /* U+2518 -' */
14147 #define BOX_123 "\342\224\234" /* U+251c |- */
14148 #define BOX_134 "\342\224\244" /* U+2524 -| */
14149 #define BOX_234 "\342\224\254" /* U+252c -,- */
14150 #define BOX_124 "\342\224\264" /* U+2534 -'- */
14151 #define BOX_1234 "\342\224\274" /* U+253c -|- */
14160 const int nDash = sizeof(zDash) - 1; in print_box_line()
14164 N -= nDash; in print_box_line()
14181 utf8_printf(p->out, "%s", zSep1); in print_box_row_separator()
14182 print_box_line(p->out, p->actualWidth[0]+2); in print_box_row_separator()
14184 utf8_printf(p->out, "%s", zSep2); in print_box_row_separator()
14185 print_box_line(p->out, p->actualWidth[i]+2); in print_box_row_separator()
14187 utf8_printf(p->out, "%s", zSep3); in print_box_row_separator()
14189 fputs("\n", p->out); in print_box_row_separator()
14196 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
14241 if( nColumn>p->nWidth ){ in exec_prepared_stmt_columnar()
14242 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int)); in exec_prepared_stmt_columnar()
14243 if( p->colWidth==0 ) shell_out_of_memory(); in exec_prepared_stmt_columnar()
14244 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0; in exec_prepared_stmt_columnar()
14245 p->nWidth = nColumn; in exec_prepared_stmt_columnar()
14246 p->actualWidth = &p->colWidth[nColumn]; in exec_prepared_stmt_columnar()
14248 memset(p->actualWidth, 0, nColumn*sizeof(int)); in exec_prepared_stmt_columnar()
14250 w = p->colWidth[i]; in exec_prepared_stmt_columnar()
14251 if( w<0 ) w = -w; in exec_prepared_stmt_columnar()
14252 p->actualWidth[i] = w; in exec_prepared_stmt_columnar()
14257 if( z==0 ) z = p->nullValue; in exec_prepared_stmt_columnar()
14260 if( n>p->actualWidth[j] ) p->actualWidth[j] = n; in exec_prepared_stmt_columnar()
14264 switch( p->cMode ){ in exec_prepared_stmt_columnar()
14268 if( p->showHeader ){ in exec_prepared_stmt_columnar()
14270 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
14271 if( p->colWidth[i]<0 ) w = -w; in exec_prepared_stmt_columnar()
14272 utf8_width_print(p->out, w, azData[i]); in exec_prepared_stmt_columnar()
14273 fputs(i==nColumn-1?"\n":" ", p->out); in exec_prepared_stmt_columnar()
14276 print_dashes(p->out, p->actualWidth[i]); in exec_prepared_stmt_columnar()
14277 fputs(i==nColumn-1?"\n":" ", p->out); in exec_prepared_stmt_columnar()
14286 fputs("| ", p->out); in exec_prepared_stmt_columnar()
14288 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
14290 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); in exec_prepared_stmt_columnar()
14291 fputs(i==nColumn-1?" |\n":" | ", p->out); in exec_prepared_stmt_columnar()
14299 fputs("| ", p->out); in exec_prepared_stmt_columnar()
14301 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
14303 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); in exec_prepared_stmt_columnar()
14304 fputs(i==nColumn-1?" |\n":" | ", p->out); in exec_prepared_stmt_columnar()
14313 utf8_printf(p->out, BOX_13 " "); in exec_prepared_stmt_columnar()
14315 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
14317 utf8_printf(p->out, "%*s%s%*s%s", in exec_prepared_stmt_columnar()
14318 (w-n)/2, "", azData[i], (w-n+1)/2, "", in exec_prepared_stmt_columnar()
14319 i==nColumn-1?" "BOX_13"\n":" "BOX_13" "); in exec_prepared_stmt_columnar()
14326 if( j==0 && p->cMode!=MODE_Column ){ in exec_prepared_stmt_columnar()
14327 utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| "); in exec_prepared_stmt_columnar()
14330 if( z==0 ) z = p->nullValue; in exec_prepared_stmt_columnar()
14331 w = p->actualWidth[j]; in exec_prepared_stmt_columnar()
14332 if( p->colWidth[j]<0 ) w = -w; in exec_prepared_stmt_columnar()
14333 utf8_width_print(p->out, w, z); in exec_prepared_stmt_columnar()
14334 if( j==nColumn-1 ){ in exec_prepared_stmt_columnar()
14335 utf8_printf(p->out, "%s", rowSep); in exec_prepared_stmt_columnar()
14336 j = -1; in exec_prepared_stmt_columnar()
14339 utf8_printf(p->out, "%s", colSep); in exec_prepared_stmt_columnar()
14342 if( p->cMode==MODE_Table ){ in exec_prepared_stmt_columnar()
14344 }else if( p->cMode==MODE_Box ){ in exec_prepared_stmt_columnar()
14349 utf8_printf(p->out, "Interrupt\n"); in exec_prepared_stmt_columnar()
14365 if( pArg->cMode==MODE_Column in exec_prepared_stmt()
14366 || pArg->cMode==MODE_Table in exec_prepared_stmt()
14367 || pArg->cMode==MODE_Box in exec_prepared_stmt()
14368 || pArg->cMode==MODE_Markdown in exec_prepared_stmt()
14399 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){ in exec_prepared_stmt()
14421 if( pArg->cMode==MODE_Json ){ in exec_prepared_stmt()
14422 fputs("]\n", pArg->out); in exec_prepared_stmt()
14444 assert( pState->expert.pExpert ); in expertHandleSQL()
14446 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); in expertHandleSQL()
14465 sqlite3expert *p = pState->expert.pExpert; in expertFinish()
14469 FILE *out = pState->out; in expertFinish()
14470 int bVerbose = pState->expert.bVerbose; in expertFinish()
14479 raw_printf(out, "-- Candidates -----------------------------\n"); in expertFinish()
14488 raw_printf(out, "-- Query %d --------------------------------\n",i+1); in expertFinish()
14497 pState->expert.pExpert = 0; in expertFinish()
14514 assert( pState->expert.pExpert==0 ); in expertDotCommand()
14515 memset(&pState->expert, 0, sizeof(ExpertInfo)); in expertDotCommand()
14520 if( z[0]=='-' && z[1]=='-' ) z++; in expertDotCommand()
14522 if( n>=2 && 0==strncmp(z, "-verbose", n) ){ in expertDotCommand()
14523 pState->expert.bVerbose = 1; in expertDotCommand()
14525 else if( n>=2 && 0==strncmp(z, "-sample", n) ){ in expertDotCommand()
14526 if( i==(nArg-1) ){ in expertDotCommand()
14544 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); in expertDotCommand()
14545 if( pState->expert.pExpert==0 ){ in expertDotCommand()
14550 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample in expertDotCommand()
14564 ** This is very similar to SQLite's built-in sqlite3_exec()
14577 sqlite3 *db = pArg->db; in shell_exec() local
14584 if( pArg->expert.pExpert ){ in shell_exec()
14592 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); in shell_exec()
14595 *pzErrMsg = save_err_msg(db, "in prepare, %s (%d)", rc); in shell_exec()
14599 /* this happens for a comment or white-space */ in shell_exec()
14610 pArg->pStmt = pStmt; in shell_exec()
14611 pArg->cnt = 0; in shell_exec()
14616 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); in shell_exec()
14620 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){ in shell_exec()
14625 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); in shell_exec()
14626 if( pArg->autoEQP>=AUTOEQP_trigger ){ in shell_exec()
14627 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0); in shell_exec()
14630 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); in shell_exec()
14637 if( zEQPLine[0]=='-' ) eqp_render(pArg); in shell_exec()
14644 if( pArg->autoEQP>=AUTOEQP_full ){ in shell_exec()
14647 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); in shell_exec()
14649 pArg->cMode = MODE_Explain; in shell_exec()
14657 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){ in shell_exec()
14658 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0); in shell_exec()
14661 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); in shell_exec()
14662 if( pArg ) pArg->pStmt = pStmt; in shell_exec()
14668 pArg->cMode = pArg->mode; in shell_exec()
14669 if( pArg->autoExplain ){ in shell_exec()
14671 pArg->cMode = MODE_Explain; in shell_exec()
14674 pArg->cMode = MODE_EQP; in shell_exec()
14680 if( pArg->cMode==MODE_Explain ){ in shell_exec()
14691 if( pArg && pArg->statsOn ){ in shell_exec()
14692 display_stats(db, pArg, 0); in shell_exec()
14695 /* print loop-counters if required */ in shell_exec()
14696 if( pArg && pArg->scanstatsOn ){ in shell_exec()
14697 display_scanstats(db, pArg); in shell_exec()
14709 *pzErrMsg = save_err_msg(db, "stepping, %s (%d)", rc); in shell_exec()
14714 pArg->pStmt = NULL; in shell_exec()
14759 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in tableColumnList()
14763 if( nCol>=nAlloc-2 ){ in tableColumnList()
14802 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in tableColumnList()
14826 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); in tableColumnList()
14838 static void toggleSelectOrder(sqlite3 *db){ in toggleSelectOrder() argument
14842 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); in toggleSelectOrder()
14849 sqlite3_exec(db, zStmt, 0, 0, 0); in toggleSelectOrder()
14872 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0; in dump_callback()
14873 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0; in dump_callback()
14876 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); in dump_callback()
14878 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n"); in dump_callback()
14882 /* no-op */ in dump_callback()
14885 if( !p->writableSchema ){ in dump_callback()
14886 raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); in dump_callback()
14887 p->writableSchema = 1; in dump_callback()
14893 utf8_printf(p->out, "%s\n", zIns); in dump_callback()
14897 printSchemaLine(p->out, zSql, ";\n"); in dump_callback()
14910 p->nErr++; in dump_callback()
14949 savedDestTable = p->zDestTable; in dump_callback()
14950 savedMode = p->mode; in dump_callback()
14951 p->zDestTable = sTable.z; in dump_callback()
14952 p->mode = p->cMode = MODE_Insert; in dump_callback()
14955 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); in dump_callback()
14956 toggleSelectOrder(p->db); in dump_callback()
14958 toggleSelectOrder(p->db); in dump_callback()
14960 p->zDestTable = savedDestTable; in dump_callback()
14961 p->mode = savedMode; in dump_callback()
14964 if( rc ) p->nErr++; in dump_callback()
14982 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); in run_schema_dump_query()
14986 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); in run_schema_dump_query()
14988 utf8_printf(p->out, "/****** %s ******/\n", zErr); in run_schema_dump_query()
14995 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); in run_schema_dump_query()
14997 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); in run_schema_dump_query()
15020 " -c, --create Create a new archive",
15021 " -u, --update Add or update files with changed mtime",
15022 " -i, --insert Like -u but always add even if unchanged",
15023 " -r, --remove Remove files from archive",
15024 " -t, --list List contents of archive",
15025 " -x, --extract Extract files from archive",
15027 " -v, --verbose Print each filename as it is processed",
15028 " -f FILE, --file FILE Use archive FILE (default is current db)",
15029 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
15030 " -C DIR, --directory DIR Read/extract files from directory DIR",
15031 " -g, --glob Use glob matching for names in archive",
15032 " -n, --dryrun Show the SQL that would have occurred",
15034 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
15035 " .ar -tf ARCHIVE # List members of ARCHIVE",
15036 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
15043 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
15044 " --append Use the appendvfs",
15045 " --async Write to FILE without journal and fsync()",
15055 ".dbinfo ?DB? Show status information about the database",
15058 " --data-only Output only INSERT statements",
15059 " --newlines Allow unescaped newline characters in output",
15060 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
15061 " --preserve-rowids Include ROWID values in the output",
15073 " --bom Put a UTF8 byte-order mark on intermediate file",
15074 ".exit ?CODE? Exit this program with return-code CODE",
15078 " --schema SCHEMA Use SCHEMA instead of \"main\"",
15079 " --help Show CMD details",
15080 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
15082 ".help ?-all? ?PATTERN? Show help text for PATTERN",
15085 " --ascii Use \\037 and \\036 as column and row separators",
15086 " --csv Use , and \\n as column and row separators",
15087 " --skip N Skip the first N rows of input",
15088 " -v \"Verbose\" - increase auxiliary output",
15092 " * If neither --csv or --ascii are used, the input mode is derived",
15108 " fkey-indexes Find missing foreign key indexes",
15116 " box Tables using unicode box-drawing characters",
15117 " csv Comma-separated values",
15126 " table ASCII-art table",
15127 " tabs Tab-separated values",
15133 " --bom Put a UTF8 byte-order mark at the beginning",
15134 " -e Send output to the system text editor",
15135 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
15137 ".oom ?--repeat M? ?N? Simulate an OOM error on the N-th allocation",
15141 " --append Use appendvfs to append database to the end of FILE",
15143 " --deserialize Load into memory using sqlite3_deserialize()",
15144 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
15145 " --maxsize N Maximum size for --hexdb or --deserialized database",
15147 " --new Initialize FILE to an empty database",
15148 " --nofollow Do not follow symbolic links",
15149 " --readonly Open FILE readonly",
15150 " --zip FILE is a ZIP archive",
15154 " --bom Prefix output with a UTF8 byte-order mark",
15155 " -e Send output to the system text editor",
15156 " -x Send output as CSV to a spreadsheet",
15167 " --limit N Interrupt after N progress callbacks",
15168 " --once Do no more than one progress interrupt",
15169 " --quiet|-q No output except at interrupts",
15170 " --reset Reset the count for each input and interrupt",
15176 ".recover Recover as much data as possible from corrupt db.",
15177 " --freelist-corrupt Assume the freelist is corrupt",
15178 " --recovery-db NAME Store recovery metadata in database file NAME",
15179 " --lost-and-found TABLE Alternative name for the lost-and-found table",
15180 " --no-rowids Do not attempt to recover rowid values",
15183 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
15184 ".save FILE Write in-memory database into FILE",
15188 " --indent Try to pretty-print the schema",
15189 " --nosys Omit objects whose names start with \"sqlite_\"",
15192 " --init Create a new SELFTEST table",
15193 " -v Verbose output",
15206 " open DB NAME Open a new session on DB",
15212 " --schema Also hash the sqlite_schema table",
15213 " --sha3-224 Use the sha3-224 algorithm",
15214 " --sha3-256 Use the sha3-256 algorithm (default)",
15215 " --sha3-384 Use the sha3-384 algorithm",
15216 " --sha3-512 Use the sha3-512 algorithm",
15231 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
15242 " --expanded Expand query parameters",
15244 " --normalized Normal the SQL statements",
15246 " --plain Show SQL as it is input",
15247 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
15248 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
15249 " --row Trace each row (SQLITE_TRACE_ROW)",
15250 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
15254 " --allexcept Unregister everything except those named",
15256 ".vfsinfo ?AUX? Information about the top-level VFS",
15260 " Negative values right-justify",
15267 ** If zPattern is NULL, then show all commands, but only give a one-line
15279 || strcmp(zPattern,"-a")==0 in showHelp()
15280 || strcmp(zPattern,"-all")==0 in showHelp()
15281 || strcmp(zPattern,"--all")==0 in showHelp()
15306 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){ in showHelp()
15320 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){ in showHelp()
15344 ** For convenience, a nul-terminator byte is always appended to the data read
15380 sqlite3session_delete(pSession->p); in session_close()
15381 sqlite3_free(pSession->zName); in session_close()
15382 for(i=0; i<pSession->nFilter; i++){ in session_close()
15383 sqlite3_free(pSession->azFilter[i]); in session_close()
15385 sqlite3_free(pSession->azFilter); in session_close()
15396 struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i]; in session_close_all()
15397 for(j=0; j<pAuxDb->nSession; j++){ in session_close_all()
15398 session_close(&pAuxDb->aSession[j]); in session_close_all()
15400 pAuxDb->nSession = 0; in session_close_all()
15414 for(i=0; i<pSession->nFilter; i++){ in session_filter()
15415 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; in session_filter()
15447 fseek(f, -25, SEEK_END); in deduceDatabaseType()
15449 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ in deduceDatabaseType()
15452 fseek(f, -22, SEEK_END); in deduceDatabaseType()
15467 ** Reconstruct an in-memory database using the output from the "dbtotxt"
15468 ** program. Read content from the file in p->aAuxDb[].zDbFilename.
15469 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
15480 const char *zDbFilename = p->pAuxDb->zDbFilename; in readHexDb()
15491 in = p->in; in readHexDb()
15492 nLine = p->lineno; in readHexDb()
15501 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error; in readHexDb()
15502 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ in readHexDb()
15509 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){ in readHexDb()
15534 if( in!=p->in ){ in readHexDb()
15537 p->lineno = nLine; in readHexDb()
15542 if( in!=p->in ){ in readHexDb()
15545 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){ in readHexDb()
15549 p->lineno = nLine; in readHexDb()
15552 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine); in readHexDb()
15559 ** must be a blob. The second a non-negative integer. This function
15560 ** reads and returns a 32-bit big-endian integer from byte
15589 ** using "..." with internal double-quote characters doubled.
15600 sqlite3_result_text(context, z, -1, sqlite3_free); in shellIdQuote()
15620 ** The argument passed to this function is the output of built-in
15727 if( p->db==0 ){ in open_db()
15728 const char *zDbFilename = p->pAuxDb->zDbFilename; in open_db()
15729 if( p->openMode==SHELL_OPEN_UNSPEC ){ in open_db()
15731 p->openMode = SHELL_OPEN_NORMAL; in open_db()
15733 p->openMode = (u8)deduceDatabaseType(zDbFilename, in open_db()
15737 switch( p->openMode ){ in open_db()
15739 sqlite3_open_v2(zDbFilename, &p->db, in open_db()
15740 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs"); in open_db()
15745 sqlite3_open(0, &p->db); in open_db()
15749 sqlite3_open(":memory:", &p->db); in open_db()
15753 sqlite3_open_v2(zDbFilename, &p->db, in open_db()
15754 SQLITE_OPEN_READONLY|p->openFlags, 0); in open_db()
15759 sqlite3_open_v2(zDbFilename, &p->db, in open_db()
15760 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0); in open_db()
15764 globalDb = p->db; in open_db()
15765 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ in open_db()
15767 zDbFilename, sqlite3_errmsg(p->db)); in open_db()
15769 sqlite3_open(":memory:", &p->db); in open_db()
15775 sqlite3_enable_load_extension(p->db, 1); in open_db()
15777 sqlite3_fileio_init(p->db, 0, 0); in open_db()
15778 sqlite3_shathree_init(p->db, 0, 0); in open_db()
15779 sqlite3_completion_init(p->db, 0, 0); in open_db()
15780 sqlite3_uint_init(p->db, 0, 0); in open_db()
15781 sqlite3_decimal_init(p->db, 0, 0); in open_db()
15782 sqlite3_regexp_init(p->db, 0, 0); in open_db()
15783 sqlite3_ieee_init(p->db, 0, 0); in open_db()
15784 sqlite3_series_init(p->db, 0, 0); in open_db()
15786 sqlite3_dbdata_init(p->db, 0, 0); in open_db()
15789 sqlite3_zipfile_init(p->db, 0, 0); in open_db()
15790 sqlite3_sqlar_init(p->db, 0, 0); in open_db()
15792 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, in open_db()
15794 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, in open_db()
15796 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, in open_db()
15798 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0, in open_db()
15800 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0, in open_db()
15802 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0, in open_db()
15804 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0, in open_db()
15807 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, in open_db()
15809 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, in open_db()
15812 if( p->openMode==SHELL_OPEN_ZIPFILE ){ in open_db()
15815 sqlite3_exec(p->db, zSql, 0, 0, 0); in open_db()
15820 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){ in open_db()
15824 if( p->openMode==SHELL_OPEN_DESERIALIZE ){ in open_db()
15832 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData, in open_db()
15838 if( p->szMax>0 ){ in open_db()
15839 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax); in open_db()
15844 if( p->bSafeModePersist && p->db!=0 ){ in open_db()
15845 sqlite3_set_authorizer(p->db, safeModeAuth, p); in open_db()
15852 void close_db(sqlite3 *db){ in close_db() argument
15853 int rc = sqlite3_close(db); in close_db()
15856 rc, sqlite3_errmsg(db)); in close_db()
15872 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); in readline_completion_generator()
15900 if( nLine>sizeof(zBuf)-30 ) return; in linenoise_completion()
15902 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} in linenoise_completion()
15903 if( i==nLine-1 ) return; in linenoise_completion()
15909 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); in linenoise_completion()
15915 if( iStart+nCompletion < sizeof(zBuf)-1 ){ in linenoise_completion()
15925 ** Do C-language style dequoting.
15927 ** \a -> alarm
15928 ** \b -> backspace
15929 ** \t -> tab
15930 ** \n -> newline
15931 ** \v -> vertical tab
15932 ** \f -> form feed
15933 ** \r -> carriage return
15934 ** \s -> space
15935 ** \" -> "
15936 ** \' -> '
15937 ** \\ -> backslash
15938 ** \NNN -> ascii character NNN in octal
15968 c -= '0'; in resolve_backslashes()
15971 c = (c<<3) + z[i] - '0'; in resolve_backslashes()
15974 c = (c<<3) + z[i] - '0'; in resolve_backslashes()
16061 if( p->traceOut==0 ) return 0; in sql_trace_callback()
16063 utf8_printf(p->traceOut, "-- closing database connection\n"); in sql_trace_callback()
16066 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){ in sql_trace_callback()
16070 switch( p->eTraceType ){ in sql_trace_callback()
16089 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; } in sql_trace_callback()
16093 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql); in sql_trace_callback()
16098 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec); in sql_trace_callback()
16107 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
16137 if( p->in!=0 && p->xCloser!=0 ){ in import_cleanup()
16138 p->xCloser(p->in); in import_cleanup()
16139 p->in = 0; in import_cleanup()
16141 sqlite3_free(p->z); in import_cleanup()
16142 p->z = 0; in import_cleanup()
16147 if( p->n+1>=p->nAlloc ){ in import_append_char()
16148 p->nAlloc += p->nAlloc + 100; in import_append_char()
16149 p->z = sqlite3_realloc64(p->z, p->nAlloc); in import_append_char()
16150 if( p->z==0 ) shell_out_of_memory(); in import_append_char()
16152 p->z[p->n++] = (char)c; in import_append_char()
16158 ** + Input comes from p->in.
16159 ** + Store results in p->z of length p->n. Space to hold p->z comes
16161 ** + Use p->cSep as the column separator. The default is ",".
16162 ** + Use p->rSep as the row separator. The default is "\n".
16163 ** + Keep track of the line number in p->nLine.
16164 ** + Store the character that terminates the field in p->cTerm. Store
16165 ** EOF on end-of-file.
16170 int cSep = p->cColSep; in csv_read_one_field()
16171 int rSep = p->cRowSep; in csv_read_one_field()
16172 p->n = 0; in csv_read_one_field()
16173 c = fgetc(p->in); in csv_read_one_field()
16175 p->cTerm = EOF; in csv_read_one_field()
16180 int startLine = p->nLine; in csv_read_one_field()
16184 c = fgetc(p->in); in csv_read_one_field()
16185 if( c==rSep ) p->nLine++; in csv_read_one_field()
16197 do{ p->n--; }while( p->z[p->n]!=cQuote ); in csv_read_one_field()
16198 p->cTerm = c; in csv_read_one_field()
16203 p->zFile, p->nLine, cQuote); in csv_read_one_field()
16206 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", in csv_read_one_field()
16207 p->zFile, startLine, cQuote); in csv_read_one_field()
16208 p->cTerm = c; in csv_read_one_field()
16217 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ in csv_read_one_field()
16218 if( (c&0xff)==0xef && p->bNotFirst==0 ){ in csv_read_one_field()
16220 c = fgetc(p->in); in csv_read_one_field()
16223 c = fgetc(p->in); in csv_read_one_field()
16225 p->bNotFirst = 1; in csv_read_one_field()
16226 p->n = 0; in csv_read_one_field()
16233 c = fgetc(p->in); in csv_read_one_field()
16236 p->nLine++; in csv_read_one_field()
16237 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; in csv_read_one_field()
16239 p->cTerm = c; in csv_read_one_field()
16241 if( p->z ) p->z[p->n] = 0; in csv_read_one_field()
16242 p->bNotFirst = 1; in csv_read_one_field()
16243 return p->z; in csv_read_one_field()
16248 ** + Input comes from p->in.
16249 ** + Store results in p->z of length p->n. Space to hold p->z comes
16251 ** + Use p->cSep as the column separator. The default is "\x1F".
16252 ** + Use p->rSep as the row separator. The default is "\x1E".
16253 ** + Keep track of the row number in p->nLine.
16254 ** + Store the character that terminates the field in p->cTerm. Store
16255 ** EOF on end-of-file.
16260 int cSep = p->cColSep; in ascii_read_one_field()
16261 int rSep = p->cRowSep; in ascii_read_one_field()
16262 p->n = 0; in ascii_read_one_field()
16263 c = fgetc(p->in); in ascii_read_one_field()
16265 p->cTerm = EOF; in ascii_read_one_field()
16270 c = fgetc(p->in); in ascii_read_one_field()
16273 p->nLine++; in ascii_read_one_field()
16275 p->cTerm = c; in ascii_read_one_field()
16276 if( p->z ) p->z[p->n] = 0; in ascii_read_one_field()
16277 return p->z; in ascii_read_one_field()
16302 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneData()
16305 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), in tryToCloneData()
16320 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); in tryToCloneData()
16346 -1, SQLITE_STATIC); in tryToCloneData()
16365 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); in tryToCloneData()
16374 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneData()
16410 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneSchema()
16413 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), in tryToCloneSchema()
16437 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneSchema()
16440 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), in tryToCloneSchema()
16482 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); in tryToClone()
16487 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); in tryToClone()
16495 ** If the p->doXdgOpen flag is set, that means the output was being
16496 ** redirected to a temporary file named by p->zTempFile. In that case,
16497 ** launch start/open/xdg-open on that temporary file.
16500 if( p->outfile[0]=='|' ){ in output_reset()
16502 pclose(p->out); in output_reset()
16505 output_file_close(p->out); in output_reset()
16507 if( p->doXdgOpen ){ in output_reset()
16514 "xdg-open"; in output_reset()
16517 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); in output_reset()
16521 /* Give the start/open/xdg-open command some time to get in output_reset()
16523 ** p->zTempFile data file out from under it */ in output_reset()
16528 p->doXdgOpen = 0; in output_reset()
16532 p->outfile[0] = 0; in output_reset()
16533 p->out = stdout; in output_reset()
16542 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in db_int()
16551 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
16599 if( p->db==0 ) return 1; in shell_dbinfo_command()
16600 rc = sqlite3_prepare_v2(p->db, in shell_dbinfo_command()
16602 -1, &pStmt, 0); in shell_dbinfo_command()
16604 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db)); in shell_dbinfo_command()
16608 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); in shell_dbinfo_command()
16621 utf8_printf(p->out, "%-20s %d\n", "database page size:", i); in shell_dbinfo_command()
16622 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); in shell_dbinfo_command()
16623 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); in shell_dbinfo_command()
16624 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); in shell_dbinfo_command()
16628 utf8_printf(p->out, "%-20s %u", aField[i].zName, val); in shell_dbinfo_command()
16631 if( val==1 ) raw_printf(p->out, " (utf8)"); in shell_dbinfo_command()
16632 if( val==2 ) raw_printf(p->out, " (utf16le)"); in shell_dbinfo_command()
16633 if( val==3 ) raw_printf(p->out, " (utf16be)"); in shell_dbinfo_command()
16636 raw_printf(p->out, "\n"); in shell_dbinfo_command()
16649 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); in shell_dbinfo_command()
16652 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); in shell_dbinfo_command()
16653 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion); in shell_dbinfo_command()
16660 static int shellDatabaseError(sqlite3 *db){ in shellDatabaseError() argument
16661 const char *zErr = sqlite3_errmsg(db); in shellDatabaseError()
16682 ** optional + or - sign in front
16706 while( *z && testcase_glob(zGlob-1,z)==0 ){ in testcase_glob()
16737 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ in testcase_glob()
16751 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; in testcase_glob()
16765 ** Compare the string as a command-line option with either one or two
16766 ** initial "-" characters.
16769 if( zStr[0]!='-' ) return 0; in optionMatch()
16771 if( zStr[0]=='-' ) zStr++; in optionMatch()
16795 if( p->zTempFile==0 ) return; in clearTempFile()
16796 if( p->doXdgOpen ) return; in clearTempFile()
16797 if( shellDeleteFile(p->zTempFile) ) return; in clearTempFile()
16798 sqlite3_free(p->zTempFile); in clearTempFile()
16799 p->zTempFile = 0; in clearTempFile()
16807 sqlite3_free(p->zTempFile); in newTempFile()
16808 p->zTempFile = 0; in newTempFile()
16809 if( p->db ){ in newTempFile()
16810 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); in newTempFile()
16812 if( p->zTempFile==0 ){ in newTempFile()
16813 /* If p->db is an in-memory database then the TEMPFILENAME file-control in newTempFile()
16827 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix); in newTempFile()
16829 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); in newTempFile()
16831 if( p->zTempFile==0 ){ in newTempFile()
16839 ** by the ".lint fkey-indexes" command. This scalar function is always
16840 ** called with four arguments - the parent table name, the parent column name,
16843 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
16849 ** function returns the string " COLLATE <parent-collation>", where
16850 ** <parent-collation> is the default collation sequence of the parent column.
16857 sqlite3 *db = sqlite3_context_db_handle(pCtx); in shellFkeyCollateClause() local
16863 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ in shellFkeyCollateClause()
16872 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); in shellFkeyCollateClause()
16874 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 in shellFkeyCollateClause()
16878 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 in shellFkeyCollateClause()
16884 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); in shellFkeyCollateClause()
16891 ** The implementation of dot-command ".lint fkey-indexes".
16898 sqlite3 *db = pState->db; /* Database handle to query "main" db of */ in lintFkeyIndexes() local
16899 FILE *out = pState->out; /* Stream to write non-error output to */ in lintFkeyIndexes()
16900 int bVerbose = 0; /* If -verbose is present */ in lintFkeyIndexes()
16901 int bGroupByParent = 0; /* If -groupbyparent is present */ in lintFkeyIndexes()
16965 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " in lintFkeyIndexes()
16973 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ in lintFkeyIndexes()
16976 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ in lintFkeyIndexes()
16981 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", in lintFkeyIndexes()
16989 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, in lintFkeyIndexes()
16995 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); in lintFkeyIndexes()
17005 int res = -1; in lintFkeyIndexes()
17014 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); in lintFkeyIndexes()
17034 raw_printf(out, "-- Parent table %s\n", zParent); in lintFkeyIndexes()
17040 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); in lintFkeyIndexes()
17042 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", in lintFkeyIndexes()
17051 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); in lintFkeyIndexes()
17057 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); in lintFkeyIndexes()
17060 raw_printf(stderr, "%s\n", sqlite3_errmsg(db)); in lintFkeyIndexes()
17076 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; in lintDotCommand()
17080 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); in lintDotCommand()
17081 raw_printf(stderr, "Where sub-commands are:\n"); in lintDotCommand()
17082 raw_printf(stderr, " fkey-indexes\n"); in lintDotCommand()
17088 sqlite3 *db, in shellPrepare() argument
17095 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); in shellPrepare()
17098 sqlite3_errmsg(db), sqlite3_errcode(db) in shellPrepare()
17106 ** Create a prepared statement using printf-style arguments for the SQL.
17109 ** depending on compile-time options. By omitting the "static", we avoid
17113 sqlite3 *db, in shellPreparePrintf() argument
17129 shellPrepare(db, pRc, z, ppStmt); in shellPreparePrintf()
17138 ** depending on compile-time options. By omitting the "static", we avoid
17146 sqlite3 *db = sqlite3_db_handle(pStmt); in shellFinalize() local
17150 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); in shellFinalize()
17160 ** depending on compile-time options. By omitting the "static", we avoid
17170 sqlite3 *db = sqlite3_db_handle(pStmt); in shellReset() local
17171 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); in shellReset()
17188 u8 bVerbose; /* True if --verbose */
17190 u8 bDryRun; /* True if --dry-run */
17191 u8 bAppend; /* True if --append */
17192 u8 bGlob; /* True if --glob */
17193 u8 fromCmdLine; /* Run from -A instead of .archive */
17196 const char *zFile; /* --file argument, or NULL */
17197 const char *zDir; /* --directory argument, or NULL */
17200 sqlite3 *db; /* Database containing the archive */ member
17222 if( pAr->fromCmdLine ){ in arErrorMsg()
17223 utf8_printf(stderr, "Use \"-A\" for more help\n"); in arErrorMsg()
17225 utf8_printf(stderr, "Use \".archive --help\" for more help\n"); in arErrorMsg()
17243 ** Other (non-command) switches.
17261 if( pAr->eCmd ){ in arProcessSwitch()
17264 pAr->eCmd = eSwitch; in arProcessSwitch()
17268 pAr->bDryRun = 1; in arProcessSwitch()
17271 pAr->bGlob = 1; in arProcessSwitch()
17274 pAr->bVerbose = 1; in arProcessSwitch()
17277 pAr->bAppend = 1; in arProcessSwitch()
17278 /* Fall thru into --file */ in arProcessSwitch()
17280 pAr->zFile = zArg; in arProcessSwitch()
17283 pAr->zDir = zArg; in arProcessSwitch()
17329 if( z[0]!='-' ){ in arParseCommand()
17337 if( z[i]==pOpt->cShort ) break; in arParseCommand()
17342 if( pOpt->bArg ){ in arParseCommand()
17348 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; in arParseCommand()
17350 pAr->nArg = nArg-iArg; in arParseCommand()
17351 if( pAr->nArg>0 ){ in arParseCommand()
17352 pAr->azArg = &azArg[iArg]; in arParseCommand()
17355 /* Non-traditional invocation */ in arParseCommand()
17360 if( z[0]!='-' ){ in arParseCommand()
17362 pAr->azArg = &azArg[iArg]; in arParseCommand()
17363 pAr->nArg = nArg-iArg; in arParseCommand()
17368 if( z[1]!='-' ){ in arParseCommand()
17375 if( z[i]==pOpt->cShort ) break; in arParseCommand()
17380 if( pOpt->bArg ){ in arParseCommand()
17381 if( i<(n-1) ){ in arParseCommand()
17385 if( iArg>=(nArg-1) ){ in arParseCommand()
17392 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; in arParseCommand()
17395 /* A -- option, indicating that all remaining command line words in arParseCommand()
17397 pAr->azArg = &azArg[iArg+1]; in arParseCommand()
17398 pAr->nArg = nArg-iArg-1; in arParseCommand()
17406 const char *zLong = pOpt->zLong; in arParseCommand()
17407 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ in arParseCommand()
17419 if( pMatch->bArg ){ in arParseCommand()
17420 if( iArg>=(nArg-1) ){ in arParseCommand()
17425 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; in arParseCommand()
17436 ** array refer to archive members, as for the --extract, --list or --remove
17441 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
17442 ** when pAr->bGlob is true.
17450 if( pAr->nArg ){ in arCheckEntries()
17453 const char *zSel = (pAr->bGlob) in arCheckEntries()
17457 shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable); in arCheckEntries()
17459 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ in arCheckEntries()
17460 char *z = pAr->azArg[i]; in arCheckEntries()
17463 while( n>0 && z[n-1]=='/' ) n--; in arCheckEntries()
17465 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); in arCheckEntries()
17485 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
17486 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
17494 const char *zSameOp = (pAr->bGlob)? "GLOB" : "="; in arWhereClause()
17496 if( pAr->nArg==0 ){ in arWhereClause()
17501 for(i=0; i<pAr->nArg; i++){ in arWhereClause()
17502 const char *z = pAr->azArg[i]; in arWhereClause()
17535 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], in arListCommand()
17536 pAr->zSrcTable, zWhere); in arListCommand()
17537 if( pAr->bDryRun ){ in arListCommand()
17538 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); in arListCommand()
17541 if( pAr->bVerbose ){ in arListCommand()
17542 utf8_printf(pAr->p->out, "%s % 10d %s %s\n", in arListCommand()
17549 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); in arListCommand()
17567 if( pAr->nArg ){ in arRemoveCommand()
17575 pAr->zSrcTable, zWhere); in arRemoveCommand()
17576 if( pAr->bDryRun ){ in arRemoveCommand()
17577 utf8_printf(pAr->p->out, "%s\n", zSql); in arRemoveCommand()
17580 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0); in arRemoveCommand()
17582 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); in arRemoveCommand()
17584 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); in arRemoveCommand()
17586 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0); in arRemoveCommand()
17629 if( pAr->zDir ){ in arExtractCommand()
17630 zDir = sqlite3_mprintf("%s/", pAr->zDir); in arExtractCommand()
17637 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, in arExtractCommand()
17638 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere in arExtractCommand()
17643 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); in arExtractCommand()
17653 if( pAr->bDryRun ){ in arExtractCommand()
17654 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); in arExtractCommand()
17657 if( i==0 && pAr->bVerbose ){ in arExtractCommand()
17658 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); in arExtractCommand()
17673 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
17677 if( pAr->bDryRun ){ in arExecSql()
17678 utf8_printf(pAr->p->out, "%s\n", zSql); in arExecSql()
17682 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); in arExecSql()
17695 ** create -> Create a new SQL archive
17696 ** insert -> Insert or reinsert all files listed
17697 ** update -> Insert files that have changed or that were not
17702 ** are added recursively. If argument bVerbose is non-zero, a message is
17707 ** always overwrites every file named on the command-line, where as
17712 int bUpdate, /* true for a --create. */ in arCreateOrUpdateCommand()
17717 " name TEXT PRIMARY KEY, -- name of the file\n" in arCreateOrUpdateCommand()
17718 " mode INT, -- access permissions\n" in arCreateOrUpdateCommand()
17719 " mtime INT, -- last modification time\n" in arCreateOrUpdateCommand()
17720 " sz INT, -- original file size\n" in arCreateOrUpdateCommand()
17721 " data BLOB -- compressed content\n" in arCreateOrUpdateCommand()
17731 " WHEN '-' THEN length(data)\n" in arCreateOrUpdateCommand()
17733 " ELSE -1 END,\n" in arCreateOrUpdateCommand()
17758 if( pAr->bZip ){ in arCreateOrUpdateCommand()
17760 if( pAr->zFile ){ in arCreateOrUpdateCommand()
17767 zTab, pAr->zFile in arCreateOrUpdateCommand()
17794 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ in arCreateOrUpdateCommand()
17795 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, in arCreateOrUpdateCommand()
17796 pAr->bVerbose ? "shell_putsnl(name)" : "name", in arCreateOrUpdateCommand()
17797 pAr->azArg[i], pAr->zDir, zExists); in arCreateOrUpdateCommand()
17803 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); in arCreateOrUpdateCommand()
17806 if( pAr->bZip && pAr->zFile ){ in arCreateOrUpdateCommand()
17821 int fromCmdLine, /* True if -A command-line option, not .ar cmd */ in arDotCommand()
17833 cmd.db = pState->db; in arDotCommand()
17837 eDbType = pState->openMode; in arDotCommand()
17857 cmd.db = 0; in arDotCommand()
17859 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile, in arDotCommand()
17862 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, in arDotCommand()
17866 cmd.zFile, sqlite3_errmsg(cmd.db) in arDotCommand()
17870 sqlite3_fileio_init(cmd.db, 0, 0); in arDotCommand()
17871 sqlite3_sqlar_init(cmd.db, 0, 0); in arDotCommand()
17872 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, in arDotCommand()
17878 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) in arDotCommand()
17901 arUsage(pState->out); in arDotCommand()
17919 if( cmd.db!=pState->db ){ in arDotCommand()
17920 close_db(cmd.db); in arDotCommand()
17932 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
17934 ** database connection db and the error code written to *pRc before
17937 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){ in shellExec() argument
17941 rc = sqlite3_exec(db, zSql, 0, 0, &zErr); in shellExec()
17953 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){ in shellExecPrintf() argument
17963 shellExec(db, pRc, z); in shellExecPrintf()
17970 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
17989 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
18032 sqlite3_free(pTab->zQuoted); in recoverFreeTable()
18033 if( pTab->azlCol ){ in recoverFreeTable()
18035 for(i=0; i<=pTab->nCol; i++){ in recoverFreeTable()
18036 sqlite3_free(pTab->azlCol[i]); in recoverFreeTable()
18038 sqlite3_free(pTab->azlCol); in recoverFreeTable()
18045 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
18111 ** set zPk to the name of the PK column, and pTab->iPk to the index in recoverNewTable()
18112 ** of the column, where columns are 0-numbered from left to right. in recoverNewTable()
18114 ** leave zPk as "_rowid_" and pTab->iPk at -2. */ in recoverNewTable()
18115 pTab->iPk = -2; in recoverNewTable()
18124 pTab->iPk = sqlite3_column_int(pPkFinder, 0); in recoverNewTable()
18129 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName); in recoverNewTable()
18130 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1)); in recoverNewTable()
18131 pTab->nCol = nSqlCol; in recoverNewTable()
18134 pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk); in recoverNewTable()
18136 pTab->azlCol[0] = shellMPrintf(&rc, ""); in recoverNewTable()
18143 bIntkey ? ", " : "", pTab->iPk, in recoverNewTable()
18149 pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText); in recoverNewTable()
18161 if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){ in recoverNewTable()
18173 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
18198 shellPreparePrintf(pState->db, pRc, &pStmt, in recoverFindTable()
18236 ** and so on until a non-clashing name is found. */ in recoverOrphanTable()
18240 shellPrepare(pState->db, pRc, in recoverOrphanTable()
18243 if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT); in recoverOrphanTable()
18248 sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT); in recoverOrphanTable()
18254 pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab); in recoverOrphanTable()
18255 pTab->nCol = nCol; in recoverOrphanTable()
18256 pTab->iPk = -2; in recoverOrphanTable()
18258 pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1)); in recoverOrphanTable()
18259 if( pTab->azlCol ){ in recoverOrphanTable()
18260 pTab->azlCol[nCol] = shellMPrintf(pRc, ""); in recoverOrphanTable()
18261 for(i=nCol-1; i>=0; i--){ in recoverOrphanTable()
18262 pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]); in recoverOrphanTable()
18271 raw_printf(pState->out, in recoverOrphanTable()
18273 "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted in recoverOrphanTable()
18276 raw_printf(pState->out, ", c%d", i); in recoverOrphanTable()
18278 raw_printf(pState->out, ");\n"); in recoverOrphanTable()
18289 ** on stream pState->out.
18299 int nOrphan = -1; in recoverDatabaseCmd()
18302 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */ in recoverDatabaseCmd()
18303 int bRowids = 1; /* 0 if --no-rowids */ in recoverDatabaseCmd()
18307 if( z[0]=='-' && z[1]=='-' ) z++; in recoverDatabaseCmd()
18309 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){ in recoverDatabaseCmd()
18312 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){ in recoverDatabaseCmd()
18316 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){ in recoverDatabaseCmd()
18320 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){ in recoverDatabaseCmd()
18325 showHelp(pState->out, azArg[0]); in recoverDatabaseCmd()
18330 shellExecPrintf(pState->db, &rc, in recoverDatabaseCmd()
18331 /* Attach an in-memory database named 'recovery'. Create an indexed in recoverDatabaseCmd()
18343 shellExec(pState->db, &rc, in recoverDatabaseCmd()
18354 " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno " in recoverDatabaseCmd()
18357 " SELECT data, n-1, shell_int32(data, 2+n) " in recoverDatabaseCmd()
18364 /* If this is an auto-vacuum database, add all pointer-map pages to in recoverDatabaseCmd()
18366 ** --freelist-corrupt was specified. */ in recoverDatabaseCmd()
18367 shellExec(pState->db, &rc, in recoverDatabaseCmd()
18379 shellExec(pState->db, &rc, in recoverDatabaseCmd()
18388 ** a root page, regardless of how corrupt the db is. */ in recoverDatabaseCmd()
18399 ** for dealing with each page in the db that contains one or more in recoverDatabaseCmd()
18413 " SELECT i-1, (" in recoverDatabaseCmd()
18414 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1" in recoverDatabaseCmd()
18448 /* Open a transaction, then print out all non-virtual, non-"sqlite_%" in recoverDatabaseCmd()
18453 ** foreign key constraints to be violated. So disable foreign-key in recoverDatabaseCmd()
18456 raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n"); in recoverDatabaseCmd()
18457 raw_printf(pState->out, "BEGIN;\n"); in recoverDatabaseCmd()
18458 raw_printf(pState->out, "PRAGMA writable_schema = on;\n"); in recoverDatabaseCmd()
18459 shellPrepare(pState->db, &rc, in recoverDatabaseCmd()
18465 raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n", in recoverDatabaseCmd()
18474 shellPrepare(pState->db, &rc, in recoverDatabaseCmd()
18475 "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1" in recoverDatabaseCmd()
18484 shellPrepare(pState->db, &rc, in recoverDatabaseCmd()
18488 shellPrepare(pState->db, &rc, in recoverDatabaseCmd()
18498 shellPrepare(pState->db, &rc, in recoverDatabaseCmd()
18522 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){ in recoverDatabaseCmd()
18523 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n"); in recoverDatabaseCmd()
18526 if( bRowids==0 && pTab->iPk<0 ){ in recoverDatabaseCmd()
18531 sqlite3_bind_int(pCells, 3, pTab->iPk); in recoverDatabaseCmd()
18552 raw_printf(pState->out, in recoverDatabaseCmd()
18554 pTab2->zQuoted, iRoot, iPgno, nField, in recoverDatabaseCmd()
18555 iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField] in recoverDatabaseCmd()
18558 raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", in recoverDatabaseCmd()
18559 pTab2->zQuoted, pTab2->azlCol[nField], zVal in recoverDatabaseCmd()
18576 shellPrepare(pState->db, &rc, in recoverDatabaseCmd()
18588 raw_printf(pState->out, "%s;\n", zPrint); in recoverDatabaseCmd()
18591 raw_printf(pState->out, "%s;\n", zSql); in recoverDatabaseCmd()
18598 raw_printf(pState->out, "PRAGMA writable_schema = off;\n"); in recoverDatabaseCmd()
18599 raw_printf(pState->out, "COMMIT;\n"); in recoverDatabaseCmd()
18601 sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0); in recoverDatabaseCmd()
18620 if( p->expert.pExpert ){ in do_meta_command()
18627 while( zLine[h] && nArg<ArraySize(azArg)-1 ){ in do_meta_command()
18640 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); in do_meta_command()
18645 resolve_backslashes(azArg[nArg-1]); in do_meta_command()
18666 sqlite3_set_authorizer(p->db, shellAuth, p); in do_meta_command()
18667 }else if( p->bSafeModePersist ){ in do_meta_command()
18668 sqlite3_set_authorizer(p->db, safeModeAuth, p); in do_meta_command()
18670 sqlite3_set_authorizer(p->db, 0, 0); in do_meta_command()
18696 if( z[0]=='-' ){ in do_meta_command()
18697 if( z[1]=='-' ) z++; in do_meta_command()
18698 if( strcmp(z, "-append")==0 ){ in do_meta_command()
18701 if( strcmp(z, "-async")==0 ){ in do_meta_command()
18714 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n"); in do_meta_command()
18735 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); in do_meta_command()
18764 setBinaryMode(p->out, 1); in do_meta_command()
18766 setTextMode(p->out, 1); in do_meta_command()
18774 /* The undocumented ".breakpoint" command causes a call to the no-op in do_meta_command()
18811 ** Then read the content of the testcase-out.txt file and compare against in do_meta_command()
18818 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); in do_meta_command()
18820 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ in do_meta_command()
18821 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); in do_meta_command()
18825 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", in do_meta_command()
18826 p->zTestcase, azArg[1], zRes); in do_meta_command()
18829 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); in do_meta_command()
18830 p->nCheck++; in do_meta_command()
18849 for(i=0; i<ArraySize(p->aAuxDb); i++){ in do_meta_command()
18850 const char *zFile = p->aAuxDb[i].zDbFilename; in do_meta_command()
18851 if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){ in do_meta_command()
18856 zFile = "(temporary-file)"; in do_meta_command()
18858 if( p->pAuxDb == &p->aAuxDb[i] ){ in do_meta_command()
18860 }else if( p->aAuxDb[i].db!=0 ){ in do_meta_command()
18865 int i = azArg[1][0] - '0'; in do_meta_command()
18866 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){ in do_meta_command()
18867 p->pAuxDb->db = p->db; in do_meta_command()
18868 p->pAuxDb = &p->aAuxDb[i]; in do_meta_command()
18869 globalDb = p->db = p->pAuxDb->db; in do_meta_command()
18870 p->pAuxDb->db = 0; in do_meta_command()
18874 int i = azArg[2][0] - '0'; in do_meta_command()
18875 if( i<0 || i>=ArraySize(p->aAuxDb) ){ in do_meta_command()
18876 /* No-op */ in do_meta_command()
18877 }else if( p->pAuxDb == &p->aAuxDb[i] ){ in do_meta_command()
18880 }else if( p->aAuxDb[i].db ){ in do_meta_command()
18882 close_db(p->aAuxDb[i].db); in do_meta_command()
18883 p->aAuxDb[i].db = 0; in do_meta_command()
18886 raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n"); in do_meta_command()
18897 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); in do_meta_command()
18899 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
18914 int eTxn = sqlite3_txn_state(p->db, azName[i*2]); in do_meta_command()
18915 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]); in do_meta_command()
18917 utf8_printf(p->out, "%s: %s %s%s\n", in do_meta_command()
18922 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn"); in do_meta_command()
18956 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); in do_meta_command()
18958 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); in do_meta_command()
18959 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off"); in do_meta_command()
18983 int savedShowHeader = p->showHeader; in do_meta_command()
18984 int savedShellFlags = p->shellFlgs; in do_meta_command()
18989 if( azArg[i][0]=='-' ){ in do_meta_command()
18991 if( z[0]=='-' ) z++; in do_meta_command()
18992 if( strcmp(z,"preserve-rowids")==0 ){ in do_meta_command()
18994 raw_printf(stderr, "The --preserve-rowids option is not compatible" in do_meta_command()
19006 if( strcmp(z,"data-only")==0 ){ in do_meta_command()
19043 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ in do_meta_command()
19046 ** So disable foreign-key constraint enforcement to prevent problems. */ in do_meta_command()
19047 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); in do_meta_command()
19048 raw_printf(p->out, "BEGIN TRANSACTION;\n"); in do_meta_command()
19050 p->writableSchema = 0; in do_meta_command()
19051 p->showHeader = 0; in do_meta_command()
19055 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); in do_meta_command()
19056 p->nErr = 0; in do_meta_command()
19067 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ in do_meta_command()
19078 if( p->writableSchema ){ in do_meta_command()
19079 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); in do_meta_command()
19080 p->writableSchema = 0; in do_meta_command()
19082 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); in do_meta_command()
19083 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); in do_meta_command()
19084 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ in do_meta_command()
19085 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n"); in do_meta_command()
19087 p->showHeader = savedShowHeader; in do_meta_command()
19088 p->shellFlgs = savedShellFlags; in do_meta_command()
19102 p->autoEQPtest = 0; in do_meta_command()
19103 if( p->autoEQPtrace ){ in do_meta_command()
19104 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); in do_meta_command()
19105 p->autoEQPtrace = 0; in do_meta_command()
19108 p->autoEQP = AUTOEQP_full; in do_meta_command()
19110 p->autoEQP = AUTOEQP_trigger; in do_meta_command()
19113 p->autoEQP = AUTOEQP_on; in do_meta_command()
19114 p->autoEQPtest = 1; in do_meta_command()
19116 p->autoEQP = AUTOEQP_full; in do_meta_command()
19117 p->autoEQPtrace = 1; in do_meta_command()
19119 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0); in do_meta_command()
19120 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); in do_meta_command()
19123 p->autoEQP = (u8)booleanValue(azArg[1]); in do_meta_command()
19147 if( val==1 && p->mode!=MODE_Explain ){ in do_meta_command()
19148 p->normalMode = p->mode; in do_meta_command()
19149 p->mode = MODE_Explain; in do_meta_command()
19150 p->autoExplain = 0; in do_meta_command()
19152 if( p->mode==MODE_Explain ) p->mode = p->normalMode; in do_meta_command()
19153 p->autoExplain = 0; in do_meta_command()
19155 if( p->mode==MODE_Explain ) p->mode = p->normalMode; in do_meta_command()
19156 p->autoExplain = 1; in do_meta_command()
19169 const char *zCtrlName; /* Name of a test-control option */ in do_meta_command()
19185 int filectrl = -1; in do_meta_command()
19186 int iCtrl = -1; in do_meta_command()
19188 int isOk = 0; /* 0: usage 1: %lld 2: no-result */ in do_meta_command()
19196 if( zCmd[0]=='-' in do_meta_command()
19197 && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0) in do_meta_command()
19201 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i]; in do_meta_command()
19202 nArg -= 2; in do_meta_command()
19206 /* The argument can optionally begin with "-" or "--" */ in do_meta_command()
19207 if( zCmd[0]=='-' && zCmd[1] ){ in do_meta_command()
19209 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; in do_meta_command()
19212 /* --help lists all file-controls */ in do_meta_command()
19214 utf8_printf(p->out, "Available file-controls:\n"); in do_meta_command()
19216 utf8_printf(p->out, " .filectrl %s %s\n", in do_meta_command()
19232 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n" in do_meta_command()
19233 "Use \".filectrl --help\" for help\n", zCmd); in do_meta_command()
19240 utf8_printf(stderr,"Error: unknown file-control: %s\n" in do_meta_command()
19241 "Use \".filectrl --help\" for help\n", zCmd); in do_meta_command()
19246 iRes = nArg==3 ? integerValue(azArg[2]) : -1; in do_meta_command()
19247 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes); in do_meta_command()
19256 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
19264 x = nArg==3 ? booleanValue(azArg[2]) : -1; in do_meta_command()
19265 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
19274 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
19282 sqlite3_file_control(p->db, zSchema, filectrl, &z); in do_meta_command()
19284 utf8_printf(p->out, "%s\n", z); in do_meta_command()
19294 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
19296 x = -1; in do_meta_command()
19297 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
19298 utf8_printf(p->out,"%d\n", x); in do_meta_command()
19305 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); in do_meta_command()
19310 raw_printf(p->out, "%s\n", zBuf); in do_meta_command()
19325 raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); in do_meta_command()
19330 rc = sqlite3_exec(p->db, in do_meta_command()
19341 rc = sqlite3_prepare_v2(p->db, in do_meta_command()
19344 -1, &pStmt, 0); in do_meta_command()
19349 raw_printf(p->out, "/* No STAT tables available */\n"); in do_meta_command()
19351 raw_printf(p->out, "ANALYZE sqlite_schema;\n"); in do_meta_command()
19357 raw_printf(p->out, "ANALYZE sqlite_schema;\n"); in do_meta_command()
19363 p->showHeader = booleanValue(azArg[1]); in do_meta_command()
19364 p->shellFlgs |= SHFLG_HeaderSet; in do_meta_command()
19373 n = showHelp(p->out, azArg[1]); in do_meta_command()
19375 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]); in do_meta_command()
19378 showHelp(p->out, 0); in do_meta_command()
19390 int nSep; /* Number of bytes in p->colSeparator[] */ in do_meta_command()
19405 if( p->mode==MODE_Ascii ){ in do_meta_command()
19412 if( z[0]=='-' && z[1]=='-' ) z++; in do_meta_command()
19413 if( z[0]!='-' ){ in do_meta_command()
19419 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z); in do_meta_command()
19420 showHelp(p->out, "import"); in do_meta_command()
19424 }else if( strcmp(z,"-v")==0 ){ in do_meta_command()
19426 }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){ in do_meta_command()
19428 }else if( strcmp(z,"-ascii")==0 ){ in do_meta_command()
19433 }else if( strcmp(z,"-csv")==0 ){ in do_meta_command()
19439 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z); in do_meta_command()
19440 showHelp(p->out, "import"); in do_meta_command()
19446 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n", in do_meta_command()
19448 showHelp(p->out, "import"); in do_meta_command()
19455 /* If neither the --csv or --ascii options are specified, then set in do_meta_command()
19457 nSep = strlen30(p->colSeparator); in do_meta_command()
19460 "Error: non-null column separator required for import\n"); in do_meta_command()
19466 "Error: multi-character column separators not allowed" in do_meta_command()
19471 nSep = strlen30(p->rowSeparator); in do_meta_command()
19474 "Error: non-null row separator required for import\n"); in do_meta_command()
19478 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){ in do_meta_command()
19483 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
19484 nSep = strlen30(p->rowSeparator); in do_meta_command()
19487 raw_printf(stderr, "Error: multi-character row separators not allowed" in do_meta_command()
19492 sCtx.cColSep = p->colSeparator[0]; in do_meta_command()
19493 sCtx.cRowSep = p->rowSeparator[0]; in do_meta_command()
19521 utf8_printf(p->out, "Column separator "); in do_meta_command()
19522 output_c_string(p->out, zSep); in do_meta_command()
19523 utf8_printf(p->out, ", row separator "); in do_meta_command()
19525 output_c_string(p->out, zSep); in do_meta_command()
19526 utf8_printf(p->out, "\n"); in do_meta_command()
19528 while( (nSkip--)>0 ){ in do_meta_command()
19537 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
19539 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ in do_meta_command()
19556 utf8_printf(p->out, "%s\n", zCreate); in do_meta_command()
19558 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); in do_meta_command()
19562 sqlite3_errmsg(p->db)); in do_meta_command()
19567 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
19572 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
19595 utf8_printf(p->out, "Insert using: %s\n", zSql); in do_meta_command()
19597 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
19600 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
19606 needCommit = sqlite3_get_autocommit(p->db); in do_meta_command()
19607 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); in do_meta_command()
19613 ** Did we reach end-of-file before finding any columns? in do_meta_command()
19618 ** Did we reach end-of-file OR end-of-line before finding any in do_meta_command()
19622 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; in do_meta_command()
19623 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); in do_meta_command()
19624 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ in do_meta_command()
19625 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " in do_meta_command()
19637 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " in do_meta_command()
19646 startLine, sqlite3_errmsg(p->db)); in do_meta_command()
19656 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); in do_meta_command()
19658 utf8_printf(p->out, in do_meta_command()
19660 sCtx.nRow, sCtx.nErr, sCtx.nLine-1); in do_meta_command()
19688 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); in do_meta_command()
19700 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
19708 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
19716 if( sqlite3_column_int(pStmt,1)==-1 ){ in do_meta_command()
19744 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); in do_meta_command()
19746 rc = sqlite3_exec(p->db, zSql, 0, 0, 0); in do_meta_command()
19747 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); in do_meta_command()
19749 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); in do_meta_command()
19772 }else if( strcmp(azArg[1], "-")==0 ){ in do_meta_command()
19811 sqlite3_limit(p->db, aLimit[i].limitCode, -1)); in do_meta_command()
19814 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); in do_meta_command()
19818 int iLimit = -1; in do_meta_command()
19839 sqlite3_limit(p->db, aLimit[iLimit].limitCode, in do_meta_command()
19843 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); in do_meta_command()
19865 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); in do_meta_command()
19881 output_file_close(p->pLog); in do_meta_command()
19882 p->pLog = output_file_open(zFile, 0); in do_meta_command()
19891 p->mode = MODE_Line; in do_meta_command()
19892 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
19894 p->mode = MODE_Column; in do_meta_command()
19895 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){ in do_meta_command()
19896 p->showHeader = 1; in do_meta_command()
19898 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
19900 p->mode = MODE_List; in do_meta_command()
19901 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); in do_meta_command()
19902 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
19904 p->mode = MODE_Html; in do_meta_command()
19906 p->mode = MODE_Tcl; in do_meta_command()
19907 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); in do_meta_command()
19908 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
19910 p->mode = MODE_Csv; in do_meta_command()
19911 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); in do_meta_command()
19912 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); in do_meta_command()
19914 p->mode = MODE_List; in do_meta_command()
19915 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); in do_meta_command()
19917 p->mode = MODE_Insert; in do_meta_command()
19920 p->mode = MODE_Quote; in do_meta_command()
19921 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); in do_meta_command()
19922 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
19924 p->mode = MODE_Ascii; in do_meta_command()
19925 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); in do_meta_command()
19926 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); in do_meta_command()
19928 p->mode = MODE_Markdown; in do_meta_command()
19930 p->mode = MODE_Table; in do_meta_command()
19932 p->mode = MODE_Box; in do_meta_command()
19934 p->mode = MODE_Json; in do_meta_command()
19936 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); in do_meta_command()
19943 p->cMode = p->mode; in do_meta_command()
19950 }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){ in do_meta_command()
19951 raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", p->lineno, azArg[1]); in do_meta_command()
19954 p->bSafeMode = 0; in do_meta_command()
19962 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, in do_meta_command()
19963 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); in do_meta_command()
19975 if( z[0]=='-' && z[1]=='-' ) z++; in do_meta_command()
19976 if( strcmp(z,"-repeat")==0 ){ in do_meta_command()
19977 if( i==nArg-1 ){ in do_meta_command()
19978 raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]); in do_meta_command()
19986 raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]); in do_meta_command()
19987 raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n"); in do_meta_command()
19992 raw_printf(p->out, "oomCounter = %d\n", oomCounter); in do_meta_command()
19993 raw_printf(p->out, "oomRepeat = %d\n", oomRepeat); in do_meta_command()
20004 /* Check for command-line arguments */ in do_meta_command()
20018 p->openFlags |= SQLITE_OPEN_NOFOLLOW; in do_meta_command()
20025 p->szMax = integerValue(azArg[++iName]); in do_meta_command()
20027 }else if( z[0]=='-' ){ in do_meta_command()
20041 session_close_all(p, -1); in do_meta_command()
20042 close_db(p->db); in do_meta_command()
20043 p->db = 0; in do_meta_command()
20044 p->pAuxDb->zDbFilename = 0; in do_meta_command()
20045 sqlite3_free(p->pAuxDb->zFreeOnClose); in do_meta_command()
20046 p->pAuxDb->zFreeOnClose = 0; in do_meta_command()
20047 p->openMode = openMode; in do_meta_command()
20048 p->openFlags = 0; in do_meta_command()
20049 p->szMax = 0; in do_meta_command()
20052 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){ in do_meta_command()
20053 if( newFlag && !p->bSafeMode ) shellDeleteFile(zNewFilename); in do_meta_command()
20054 if( p->bSafeMode in do_meta_command()
20055 && p->openMode!=SHELL_OPEN_HEXDB in do_meta_command()
20059 failIfSafeMode(p, "cannot open disk-based database files in safe mode"); in do_meta_command()
20061 p->pAuxDb->zDbFilename = zNewFilename; in do_meta_command()
20063 if( p->db==0 ){ in do_meta_command()
20067 p->pAuxDb->zFreeOnClose = zNewFilename; in do_meta_command()
20070 if( p->db==0 ){ in do_meta_command()
20071 /* As a fall-back open a TEMP database */ in do_meta_command()
20072 p->pAuxDb->zDbFilename = 0; in do_meta_command()
20097 if( z[0]=='-' ){ in do_meta_command()
20098 if( z[1]=='-' ) z++; in do_meta_command()
20099 if( strcmp(z,"-bom")==0 ){ in do_meta_command()
20101 }else if( c!='e' && strcmp(z,"-x")==0 ){ in do_meta_command()
20103 }else if( c!='e' && strcmp(z,"-e")==0 ){ in do_meta_command()
20106 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", in do_meta_command()
20108 showHelp(p->out, azArg[0]); in do_meta_command()
20119 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n", in do_meta_command()
20121 showHelp(p->out, azArg[0]); in do_meta_command()
20129 p->outCount = 2; in do_meta_command()
20131 p->outCount = 0; in do_meta_command()
20136 p->doXdgOpen = 1; in do_meta_command()
20142 p->mode = MODE_Csv; in do_meta_command()
20143 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); in do_meta_command()
20144 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); in do_meta_command()
20151 zFile = sqlite3_mprintf("%s", p->zTempFile); in do_meta_command()
20158 p->out = stdout; in do_meta_command()
20160 p->out = popen(zFile + 1, "w"); in do_meta_command()
20161 if( p->out==0 ){ in do_meta_command()
20163 p->out = stdout; in do_meta_command()
20166 if( bBOM ) fprintf(p->out,"\357\273\277"); in do_meta_command()
20167 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); in do_meta_command()
20171 p->out = output_file_open(zFile, bTxtMode); in do_meta_command()
20172 if( p->out==0 ){ in do_meta_command()
20176 p->out = stdout; in do_meta_command()
20179 if( bBOM ) fprintf(p->out,"\357\273\277"); in do_meta_command()
20180 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); in do_meta_command()
20194 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", in do_meta_command()
20205 rx = sqlite3_prepare_v2(p->db, in do_meta_command()
20207 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); in do_meta_command()
20215 rx = sqlite3_prepare_v2(p->db, in do_meta_command()
20217 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); in do_meta_command()
20219 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0), in do_meta_command()
20252 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
20261 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
20264 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
20282 sqlite3_exec(p->db, zSql, 0, 0, 0); in do_meta_command()
20287 showHelp(p->out, "parameter"); in do_meta_command()
20293 if( i>1 ) raw_printf(p->out, " "); in do_meta_command()
20294 utf8_printf(p->out, "%s", azArg[i]); in do_meta_command()
20296 raw_printf(p->out, "\n"); in do_meta_command()
20303 p->flgProgress = 0; in do_meta_command()
20304 p->mxProgress = 0; in do_meta_command()
20305 p->nProgress = 0; in do_meta_command()
20308 if( z[0]=='-' ){ in do_meta_command()
20310 if( z[0]=='-' ) z++; in do_meta_command()
20312 p->flgProgress |= SHELL_PROGRESS_QUIET; in do_meta_command()
20316 p->flgProgress |= SHELL_PROGRESS_RESET; in do_meta_command()
20320 p->flgProgress |= SHELL_PROGRESS_ONCE; in do_meta_command()
20325 utf8_printf(stderr, "Error: missing argument on --limit\n"); in do_meta_command()
20329 p->mxProgress = (int)integerValue(azArg[++i]); in do_meta_command()
20341 sqlite3_progress_handler(p->db, nn, progress_handler, p); in do_meta_command()
20347 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); in do_meta_command()
20350 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); in do_meta_command()
20359 FILE *inSaved = p->in; in do_meta_command()
20360 int savedLineno = p->lineno; in do_meta_command()
20371 p->out = stdout; in do_meta_command()
20373 p->in = popen(azArg[1]+1, "r"); in do_meta_command()
20374 if( p->in==0 ){ in do_meta_command()
20379 pclose(p->in); in do_meta_command()
20382 }else if( (p->in = openChrSource(azArg[1]))==0 ){ in do_meta_command()
20387 fclose(p->in); in do_meta_command()
20389 p->in = inSaved; in do_meta_command()
20390 p->lineno = savedLineno; in do_meta_command()
20408 raw_printf(stderr, "Usage: .restore ?DB? FILE\n"); in do_meta_command()
20419 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); in do_meta_command()
20421 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
20439 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
20447 p->scanstatsOn = (u8)booleanValue(azArg[1]); in do_meta_command()
20480 }else if( azArg[ii][0]=='-' ){ in do_meta_command()
20487 raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n"); in do_meta_command()
20516 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", in do_meta_command()
20517 -1, &pStmt, 0); in do_meta_command()
20519 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
20579 utf8_printf(p->out, "SQL: %s;\n", sSelect.z); in do_meta_command()
20581 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); in do_meta_command()
20604 struct AuxDb *pAuxDb = p->pAuxDb; in do_meta_command()
20605 OpenSession *pSession = &pAuxDb->aSession[0]; in do_meta_command()
20608 int nCmd = nArg - 1; in do_meta_command()
20613 for(iSes=0; iSes<pAuxDb->nSession; iSes++){ in do_meta_command()
20614 if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break; in do_meta_command()
20616 if( iSes<pAuxDb->nSession ){ in do_meta_command()
20617 pSession = &pAuxDb->aSession[iSes]; in do_meta_command()
20619 nCmd--; in do_meta_command()
20621 pSession = &pAuxDb->aSession[0]; in do_meta_command()
20632 if( pSession->p==0 ){ in do_meta_command()
20636 rc = sqlite3session_attach(pSession->p, azCmd[1]); in do_meta_command()
20652 if( pSession->p==0 ) goto session_not_open; in do_meta_command()
20661 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); in do_meta_command()
20663 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); in do_meta_command()
20671 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", in do_meta_command()
20684 if( pAuxDb->nSession ){ in do_meta_command()
20686 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession]; in do_meta_command()
20696 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); in do_meta_command()
20697 if( pAuxDb->nSession ){ in do_meta_command()
20698 ii = sqlite3session_enable(pSession->p, ii); in do_meta_command()
20699 utf8_printf(p->out, "session %s enable flag = %d\n", in do_meta_command()
20700 pSession->zName, ii); in do_meta_command()
20710 if( pAuxDb->nSession ){ in do_meta_command()
20711 for(ii=0; ii<pSession->nFilter; ii++){ in do_meta_command()
20712 sqlite3_free(pSession->azFilter[ii]); in do_meta_command()
20714 sqlite3_free(pSession->azFilter); in do_meta_command()
20715 nByte = sizeof(pSession->azFilter[0])*(nCmd-1); in do_meta_command()
20716 pSession->azFilter = sqlite3_malloc( nByte ); in do_meta_command()
20717 if( pSession->azFilter==0 ){ in do_meta_command()
20722 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); in do_meta_command()
20724 pSession->nFilter = ii-1; in do_meta_command()
20734 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); in do_meta_command()
20735 if( pAuxDb->nSession ){ in do_meta_command()
20736 ii = sqlite3session_indirect(pSession->p, ii); in do_meta_command()
20737 utf8_printf(p->out, "session %s indirect flag = %d\n", in do_meta_command()
20738 pSession->zName, ii); in do_meta_command()
20748 if( pAuxDb->nSession ){ in do_meta_command()
20749 ii = sqlite3session_isempty(pSession->p); in do_meta_command()
20750 utf8_printf(p->out, "session %s isempty flag = %d\n", in do_meta_command()
20751 pSession->zName, ii); in do_meta_command()
20759 for(i=0; i<pAuxDb->nSession; i++){ in do_meta_command()
20760 utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName); in do_meta_command()
20764 /* .session open DB NAME in do_meta_command()
20765 ** Open a new session called NAME on the attached database DB. in do_meta_command()
20766 ** DB is normally "main". in do_meta_command()
20773 for(i=0; i<pAuxDb->nSession; i++){ in do_meta_command()
20774 if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){ in do_meta_command()
20779 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){ in do_meta_command()
20780 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession)); in do_meta_command()
20783 pSession = &pAuxDb->aSession[pAuxDb->nSession]; in do_meta_command()
20784 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); in do_meta_command()
20790 pSession->nFilter = 0; in do_meta_command()
20791 sqlite3session_table_filter(pSession->p, session_filter, pSession); in do_meta_command()
20792 pAuxDb->nSession++; in do_meta_command()
20793 pSession->zName = sqlite3_mprintf("%s", zName); in do_meta_command()
20797 showHelp(p->out, "session"); in do_meta_command()
20804 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){ in do_meta_command()
20805 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){ in do_meta_command()
20809 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); in do_meta_command()
20812 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){ in do_meta_command()
20818 utf8_printf(p->out, "%s", zBuf); in do_meta_command()
20837 if( z[0]=='-' && z[1]=='-' ) z++; in do_meta_command()
20838 if( strcmp(z,"-init")==0 ){ in do_meta_command()
20841 if( strcmp(z,"-v")==0 ){ in do_meta_command()
20847 raw_printf(stderr, "Should be one of: --init -v\n"); in do_meta_command()
20852 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) in do_meta_command()
20864 for(k=bSelftestExists; k>=0; k--){ in do_meta_command()
20866 rc = sqlite3_prepare_v2(p->db, in do_meta_command()
20868 -1, &pStmt, 0); in do_meta_command()
20870 rc = sqlite3_prepare_v2(p->db, in do_meta_command()
20871 "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," in do_meta_command()
20873 -1, &pStmt, 0); in do_meta_command()
20894 utf8_printf(p->out, "%s\n", zSql); in do_meta_command()
20900 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); in do_meta_command()
20903 utf8_printf(p->out, "Result: %s\n", str.z); in do_meta_command()
20908 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); in do_meta_command()
20913 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); in do_meta_command()
20914 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); in do_meta_command()
20927 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); in do_meta_command()
20936 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, in do_meta_command()
20937 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); in do_meta_command()
20940 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, in do_meta_command()
20941 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); in do_meta_command()
20960 if( z[0]=='-' ){ in do_meta_command()
20962 if( z[0]=='-' ) z++; in do_meta_command()
20966 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0 in do_meta_command()
20967 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0 in do_meta_command()
20977 showHelp(p->out, azArg[0]); in do_meta_command()
20982 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); in do_meta_command()
21002 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
21052 utf8_printf(p->out, "%s\n", zSql); in do_meta_command()
21091 utf8_printf(p->out, "%12.12s: %s\n","echo", in do_meta_command()
21093 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); in do_meta_command()
21094 utf8_printf(p->out, "%12.12s: %s\n","explain", in do_meta_command()
21095 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); in do_meta_command()
21096 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); in do_meta_command()
21097 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); in do_meta_command()
21098 utf8_printf(p->out, "%12.12s: ", "nullvalue"); in do_meta_command()
21099 output_c_string(p->out, p->nullValue); in do_meta_command()
21100 raw_printf(p->out, "\n"); in do_meta_command()
21101 utf8_printf(p->out,"%12.12s: %s\n","output", in do_meta_command()
21102 strlen30(p->outfile) ? p->outfile : "stdout"); in do_meta_command()
21103 utf8_printf(p->out,"%12.12s: ", "colseparator"); in do_meta_command()
21104 output_c_string(p->out, p->colSeparator); in do_meta_command()
21105 raw_printf(p->out, "\n"); in do_meta_command()
21106 utf8_printf(p->out,"%12.12s: ", "rowseparator"); in do_meta_command()
21107 output_c_string(p->out, p->rowSeparator); in do_meta_command()
21108 raw_printf(p->out, "\n"); in do_meta_command()
21109 switch( p->statsOn ){ in do_meta_command()
21115 utf8_printf(p->out, "%12.12s: %s\n","stats", zOut); in do_meta_command()
21116 utf8_printf(p->out, "%12.12s: ", "width"); in do_meta_command()
21117 for (i=0;i<p->nWidth;i++) { in do_meta_command()
21118 raw_printf(p->out, "%d ", p->colWidth[i]); in do_meta_command()
21120 raw_printf(p->out, "\n"); in do_meta_command()
21121 utf8_printf(p->out, "%12.12s: %s\n", "filename", in do_meta_command()
21122 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : ""); in do_meta_command()
21128 p->statsOn = 2; in do_meta_command()
21130 p->statsOn = 3; in do_meta_command()
21132 p->statsOn = (u8)booleanValue(azArg[1]); in do_meta_command()
21135 display_stats(p->db, p, 0); in do_meta_command()
21153 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); in do_meta_command()
21156 return shellDatabaseError(p->db); in do_meta_command()
21163 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); in do_meta_command()
21193 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); in do_meta_command()
21196 if( rc ) return shellDatabaseError(p->db); in do_meta_command()
21199 ** as an array of nul-terminated strings in azResult[]. */ in do_meta_command()
21203 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); in do_meta_command()
21205 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); in do_meta_command()
21221 rc = shellDatabaseError(p->db); in do_meta_command()
21224 /* Pretty-print the contents of array azResult[] to the output */ in do_meta_command()
21235 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; in do_meta_command()
21239 utf8_printf(p->out, "%s%-*s", zSp, maxlen, in do_meta_command()
21242 raw_printf(p->out, "\n"); in do_meta_command()
21250 /* Begin redirecting output to the file "testcase-out.txt" */ in do_meta_command()
21253 p->out = output_file_open("testcase-out.txt", 0); in do_meta_command()
21254 if( p->out==0 ){ in do_meta_command()
21255 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); in do_meta_command()
21258 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); in do_meta_command()
21260 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); in do_meta_command()
21267 const char *zCtrlName; /* Name of a test-control option */ in do_meta_command()
21269 int unSafe; /* Not valid for --safe mode */ in do_meta_command()
21283 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK" }, in do_meta_command()
21290 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" }, in do_meta_command()
21295 int testctrl = -1; in do_meta_command()
21296 int iCtrl = -1; in do_meta_command()
21297 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ in do_meta_command()
21305 /* The argument can optionally begin with "-" or "--" */ in do_meta_command()
21306 if( zCmd[0]=='-' && zCmd[1] ){ in do_meta_command()
21308 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; in do_meta_command()
21311 /* --help lists all test-controls */ in do_meta_command()
21313 utf8_printf(p->out, "Available test-controls:\n"); in do_meta_command()
21315 utf8_printf(p->out, " .testctrl %s %s\n", in do_meta_command()
21331 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" in do_meta_command()
21332 "Use \".testctrl --help\" for help\n", zCmd); in do_meta_command()
21339 utf8_printf(stderr,"Error: unknown test-control: %s\n" in do_meta_command()
21340 "Use \".testctrl --help\" for help\n", zCmd); in do_meta_command()
21341 }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){ in do_meta_command()
21344 p->lineno, aCtrl[iCtrl].zCtrlName); in do_meta_command()
21349 /* sqlite3_test_control(int, db, int) */ in do_meta_command()
21353 rc2 = sqlite3_test_control(testctrl, p->db, opt); in do_meta_command()
21381 sqlite3 *db; in do_meta_command() local
21384 printf("-- random seed: %d\n", ii); in do_meta_command()
21387 db = 0; in do_meta_command()
21389 db = p->db; in do_meta_command()
21391 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0); in do_meta_command()
21393 rc2 = sqlite3_test_control(testctrl, ii, db); in do_meta_command()
21420 rc2 = sqlite3_test_control(testctrl, p->db); in do_meta_command()
21426 rc2 = sqlite3_test_control(testctrl, p->db, in do_meta_command()
21436 rc2 = sqlite3_test_control(testctrl, p->db, &x); in do_meta_command()
21437 utf8_printf(p->out, "%llu\n", x); in do_meta_command()
21445 sqlite3_test_control(testctrl, p->out); in do_meta_command()
21460 sqlite3_test_control(testctrl, -id, &rc2); in do_meta_command()
21466 rc2 = sqlite3_test_control(testctrl, -id, &val); in do_meta_command()
21468 if( id>1 ) utf8_printf(p->out, " "); in do_meta_command()
21469 utf8_printf(p->out, "%d: %d", id, val); in do_meta_command()
21472 if( id>1 ) utf8_printf(p->out, "\n"); in do_meta_command()
21481 rc2 = sqlite3_test_control(testctrl, p->db, opt); in do_meta_command()
21488 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); in do_meta_command()
21491 raw_printf(p->out, "%d\n", rc2); in do_meta_command()
21493 raw_printf(p->out, "0x%08x\n", rc2); in do_meta_command()
21500 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); in do_meta_command()
21523 if( z[0]=='-' ){ in do_meta_command()
21525 p->eTraceType = SHELL_TRACE_EXPANDED; in do_meta_command()
21529 p->eTraceType = SHELL_TRACE_NORMALIZED; in do_meta_command()
21533 p->eTraceType = SHELL_TRACE_PLAIN; in do_meta_command()
21553 output_file_close(p->traceOut); in do_meta_command()
21554 p->traceOut = output_file_open(azArg[1], 0); in do_meta_command()
21557 if( p->traceOut==0 ){ in do_meta_command()
21558 sqlite3_trace_v2(p->db, 0, 0, 0); in do_meta_command()
21561 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); in do_meta_command()
21572 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n"); in do_meta_command()
21578 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; in do_meta_command()
21580 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){ in do_meta_command()
21582 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); in do_meta_command()
21585 sqlite3_create_module(p->db, azArg[ii], 0, 0); in do_meta_command()
21605 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], in do_meta_command()
21617 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), in do_meta_command()
21620 raw_printf(stderr, "User-Add failed: %d\n", rc); in do_meta_command()
21629 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), in do_meta_command()
21632 raw_printf(stderr, "User-Edit failed: %d\n", rc); in do_meta_command()
21641 rc = sqlite3_user_delete(p->db, azArg[2]); in do_meta_command()
21643 raw_printf(stderr, "User-Delete failed: %d\n", rc); in do_meta_command()
21655 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, in do_meta_command()
21658 utf8_printf(p->out, "zlib version %s\n", zlibVersion()); in do_meta_command()
21663 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." in do_meta_command()
21667 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); in do_meta_command()
21669 utf8_printf(p->out, "gcc-" __VERSION__ "\n"); in do_meta_command()
21676 if( p->db ){ in do_meta_command()
21677 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); in do_meta_command()
21679 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); in do_meta_command()
21680 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); in do_meta_command()
21681 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); in do_meta_command()
21682 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); in do_meta_command()
21690 if( p->db ){ in do_meta_command()
21691 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); in do_meta_command()
21693 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ in do_meta_command()
21694 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, in do_meta_command()
21695 pVfs==pCurrent ? " <--- CURRENT" : ""); in do_meta_command()
21696 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); in do_meta_command()
21697 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); in do_meta_command()
21698 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); in do_meta_command()
21699 if( pVfs->pNext ){ in do_meta_command()
21700 raw_printf(p->out, "-----------------------------------\n"); in do_meta_command()
21708 if( p->db ){ in do_meta_command()
21709 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); in do_meta_command()
21711 utf8_printf(p->out, "%s\n", zVfsName); in do_meta_command()
21725 p->nWidth = nArg-1; in do_meta_command()
21726 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2); in do_meta_command()
21727 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory(); in do_meta_command()
21728 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth]; in do_meta_command()
21730 p->colWidth[j-1] = (int)integerValue(azArg[j]); in do_meta_command()
21741 if( p->outCount ){ in do_meta_command()
21742 p->outCount--; in do_meta_command()
21743 if( p->outCount==0 ) output_reset(p); in do_meta_command()
21745 p->bSafeMode = p->bSafeModePersist; in do_meta_command()
21756 QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
21780 case '-': in quickscan()
21781 if( *zLine!='-' ) in quickscan()
21842 ** than a semi-colon. The SQL Server style "go" command is understood
21860 ** user-friendly, but it does seem to work.
21868 ** ends in the middle of a string literal or C-style comment.
21889 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; in runOneSqlLine()
21906 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); in runOneSqlLine()
21913 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db)); in runOneSqlLine()
21914 raw_printf(p->out, "%s\n", zLineBuf); in runOneSqlLine()
21922 ** is interactive - the user is typing it it. Otherwise, input
21940 p->lineno = 0; in process_input()
21941 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ in process_input()
21942 fflush(p->out); in process_input()
21943 zLine = one_input_line(p->in, zLine, nSql>0); in process_input()
21946 if( p->in==0 && stdin_is_interactive ) printf("\n"); in process_input()
21950 if( p->in!=0 ) break; in process_input()
21953 p->lineno++; in process_input()
21963 /* Just swallow single-line whitespace */ in process_input()
21980 /* No single-line dispositions remain; accumulate line(s). */ in process_input()
21983 /* Grow buffer by half-again increments when big. */ in process_input()
21992 memcpy(zSql, zLine+i, nLine+1-i); in process_input()
21993 startline = p->lineno; in process_input()
21994 nSql = nLine-i; in process_input()
22001 errCnt += runOneSqlLine(p, zSql, p->in, startline); in process_input()
22003 if( p->outCount ){ in process_input()
22005 p->outCount = 0; in process_input()
22009 p->bSafeMode = p->bSafeModePersist; in process_input()
22018 errCnt += runOneSqlLine(p, zSql, p->in, startline); in process_input()
22044 home_dir = pwent->pw_dir; in find_home_dir()
22050 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() in find_home_dir()
22107 FILE *inSaved = p->in; in process_sqliterc()
22108 int savedLineno = p->lineno; in process_sqliterc()
22113 raw_printf(stderr, "-- warning: cannot find home directory;" in process_sqliterc()
22120 p->in = fopen(sqliterc,"rb"); in process_sqliterc()
22121 if( p->in ){ in process_sqliterc()
22123 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); in process_sqliterc()
22126 fclose(p->in); in process_sqliterc()
22131 p->in = inSaved; in process_sqliterc()
22132 p->lineno = savedLineno; in process_sqliterc()
22141 " -A ARGS... run \".archive ARGS\" and exit\n"
22143 " -append append the database to the end of the file\n"
22144 " -ascii set output mode to 'ascii'\n"
22145 " -bail stop after hitting an error\n"
22146 " -batch force batch I/O\n"
22147 " -box set output mode to 'box'\n"
22148 " -column set output mode to 'column'\n"
22149 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
22150 " -csv set output mode to 'csv'\n"
22152 " -deserialize open the database using sqlite3_deserialize()\n"
22154 " -echo print commands before execution\n"
22155 " -init FILENAME read/process named file\n"
22156 " -[no]header turn headers on or off\n"
22158 " -heap SIZE Size of heap for memsys3 or memsys5\n"
22160 " -help show this message\n"
22161 " -html set output mode to HTML\n"
22162 " -interactive force interactive I/O\n"
22163 " -json set output mode to 'json'\n"
22164 " -line set output mode to 'line'\n"
22165 " -list set output mode to 'list'\n"
22166 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
22167 " -markdown set output mode to 'markdown'\n"
22169 " -maxsize N maximum size for a --deserialize database\n"
22171 " -memtrace trace all memory allocations and deallocations\n"
22172 " -mmap N default mmap size set to N\n"
22174 " -multiplex enable the multiplexor VFS\n"
22176 " -newline SEP set output row separator. Default: '\\n'\n"
22177 " -nofollow refuse to open symbolic links to database files\n"
22178 " -nonce STRING set the safe-mode escape nonce\n"
22179 " -nullvalue TEXT set text string for NULL values. Default ''\n"
22180 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
22181 " -quote set output mode to 'quote'\n"
22182 " -readonly open the database read-only\n"
22183 " -safe enable safe-mode\n"
22184 " -separator SEP set output column separator. Default: '|'\n"
22186 " -sorterref SIZE sorter references threshold size\n"
22188 " -stats print memory stats before each finalize\n"
22189 " -table set output mode to 'table'\n"
22190 " -tabs set output mode to 'tabs'\n"
22191 " -version show SQLite version\n"
22192 " -vfs NAME use NAME as the default VFS\n"
22194 " -vfstrace enable tracing of all VFS calls\n"
22197 " -zip open the file as a ZIP Archive\n"
22208 raw_printf(stderr, "Use the -help option for additional information\n"); in usage()
22218 if( sqlite3_config(-1)==SQLITE_MISUSE ){ in verify_uninitialized()
22229 data->normalMode = data->cMode = data->mode = MODE_List; in main_init()
22230 data->autoExplain = 1; in main_init()
22231 data->pAuxDb = &data->aAuxDb[0]; in main_init()
22232 memcpy(data->colSeparator,SEP_Column, 2); in main_init()
22233 memcpy(data->rowSeparator,SEP_Row, 2); in main_init()
22234 data->showHeader = 0; in main_init()
22235 data->shellFlgs = SHFLG_Lookaside; in main_init()
22269 ** Get the argument to an --option. Throw an error and die if no argument
22275 argv[0], argv[argc-1]); in cmdline_option_value()
22305 const char *zVfs = 0; /* Value of -vfs command-line option */
22350 /* On Windows, we must translate command-line arguments into UTF-8.
22391 ** of a C-function that will provide the name of the database file. Use
22392 ** this compile-time option to embed this shell program in larger
22395 SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
22400 /* Do an initial pass through the command-line argument to locate
22409 if( z[0]!='-' ){
22410 if( data.aAuxDb->zDbFilename==0 ){
22411 data.aAuxDb->zDbFilename = z;
22413 /* Excesss arguments are interpreted as SQL (or dot-commands) and
22419 azCmd[nCmd-1] = z;
22422 if( z[1]=='-' ) z++;
22423 if( strcmp(z,"-separator")==0
22424 || strcmp(z,"-nullvalue")==0
22425 || strcmp(z,"-newline")==0
22426 || strcmp(z,"-cmd")==0
22429 }else if( strcmp(z,"-init")==0 ){
22431 }else if( strcmp(z,"-batch")==0 ){
22437 }else if( strcmp(z,"-heap")==0 ){
22449 }else if( strcmp(z,"-pagecache")==0 ){
22461 }else if( strcmp(z,"-lookaside")==0 ){
22469 }else if( strcmp(z,"-threadsafe")==0 ){
22478 }else if( strcmp(z,"-vfstrace")==0 ){
22489 }else if( strcmp(z,"-multiplex")==0 ){
22493 }else if( strcmp(z,"-mmap")==0 ){
22497 }else if( strcmp(z,"-sorterref")==0 ){
22501 }else if( strcmp(z,"-vfs")==0 ){
22504 }else if( strcmp(z,"-zip")==0 ){
22507 }else if( strcmp(z,"-append")==0 ){
22510 }else if( strcmp(z,"-deserialize")==0 ){
22512 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
22515 }else if( strcmp(z,"-readonly")==0 ){
22517 }else if( strcmp(z,"-nofollow")==0 ){
22520 }else if( strncmp(z, "-A",2)==0 ){
22521 /* All remaining command-line arguments are passed to the ".archive"
22525 }else if( strcmp(z, "-memtrace")==0 ){
22527 }else if( strcmp(z,"-bail")==0 ){
22529 }else if( strcmp(z,"-nonce")==0 ){
22532 }else if( strcmp(z,"-safe")==0 ){
22533 /* no-op - catch this on the second pass */
22542 ** of a C-function that will perform initialization actions on SQLite that
22543 ** occur just before or after sqlite3_initialize(). Use this compile-time
22550 ** to call sqlite3_initialize() and process any command line -vfs option. */
22564 if( data.pAuxDb->zDbFilename==0 ){
22566 data.pAuxDb->zDbFilename = ":memory:";
22579 ** to the sqlite command-line tool.
22581 if( access(data.pAuxDb->zDbFilename, 0)==0 ){
22585 /* Process the initialization file if there is one. If no -init option
22591 /* Make a second pass through the command-line argument and set
22593 ** file is processed so that the command-line arguments will override
22598 if( z[0]!='-' ) continue;
22599 if( z[1]=='-' ){ z++; }
22600 if( strcmp(z,"-init")==0 ){
22602 }else if( strcmp(z,"-html")==0 ){
22604 }else if( strcmp(z,"-list")==0 ){
22606 }else if( strcmp(z,"-quote")==0 ){
22610 }else if( strcmp(z,"-line")==0 ){
22612 }else if( strcmp(z,"-column")==0 ){
22614 }else if( strcmp(z,"-json")==0 ){
22616 }else if( strcmp(z,"-markdown")==0 ){
22618 }else if( strcmp(z,"-table")==0 ){
22620 }else if( strcmp(z,"-box")==0 ){
22622 }else if( strcmp(z,"-csv")==0 ){
22626 }else if( strcmp(z,"-zip")==0 ){
22629 }else if( strcmp(z,"-append")==0 ){
22632 }else if( strcmp(z,"-deserialize")==0 ){
22634 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
22637 }else if( strcmp(z,"-readonly")==0 ){
22639 }else if( strcmp(z,"-nofollow")==0 ){
22641 }else if( strcmp(z,"-ascii")==0 ){
22645 }else if( strcmp(z,"-tabs")==0 ){
22649 }else if( strcmp(z,"-separator")==0 ){
22652 }else if( strcmp(z,"-newline")==0 ){
22655 }else if( strcmp(z,"-nullvalue")==0 ){
22658 }else if( strcmp(z,"-header")==0 ){
22661 }else if( strcmp(z,"-noheader")==0 ){
22664 }else if( strcmp(z,"-echo")==0 ){
22666 }else if( strcmp(z,"-eqp")==0 ){
22668 }else if( strcmp(z,"-eqpfull")==0 ){
22670 }else if( strcmp(z,"-stats")==0 ){
22672 }else if( strcmp(z,"-scanstats")==0 ){
22674 }else if( strcmp(z,"-backslash")==0 ){
22675 /* Undocumented command-line option: -backslash
22676 ** Causes C-style backslash escapes to be evaluated in SQL statements
22681 }else if( strcmp(z,"-bail")==0 ){
22682 /* No-op. The bail_on_error flag should already be set. */
22683 }else if( strcmp(z,"-version")==0 ){
22686 }else if( strcmp(z,"-interactive")==0 ){
22688 }else if( strcmp(z,"-batch")==0 ){
22690 }else if( strcmp(z,"-heap")==0 ){
22692 }else if( strcmp(z,"-pagecache")==0 ){
22694 }else if( strcmp(z,"-lookaside")==0 ){
22696 }else if( strcmp(z,"-threadsafe")==0 ){
22698 }else if( strcmp(z,"-nonce")==0 ){
22700 }else if( strcmp(z,"-mmap")==0 ){
22702 }else if( strcmp(z,"-memtrace")==0 ){
22705 }else if( strcmp(z,"-sorterref")==0 ){
22708 }else if( strcmp(z,"-vfs")==0 ){
22711 }else if( strcmp(z,"-vfstrace")==0 ){
22715 }else if( strcmp(z,"-multiplex")==0 ){
22718 }else if( strcmp(z,"-help")==0 ){
22720 }else if( strcmp(z,"-cmd")==0 ){
22721 /* Run commands that follow -cmd first and separately from commands
22722 ** that simply appear on the command-line. This seems goofy. It would
22725 if( i==argc-1 ) break;
22742 }else if( strncmp(z, "-A", 2)==0 ){
22744 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
22751 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
22753 arDotCommand(&data, 1, argv+i, argc-i);
22758 }else if( strcmp(z,"-safe")==0 ){
22762 raw_printf(stderr,"Use -help for a list of options.\n");
22769 /* Run all arguments that do not begin with '-' as if they were separate
22770 ** command-line inputs, except for the argToSkip argument which contains
22803 "SQLite version %s %.19s\n" /*extra-version-info*/
22809 printBold("transient in-memory database");
22842 if( data.db ){
22843 session_close_all(&data, -1);
22844 close_db(data.db);
22848 if( data.aAuxDb[i].db ){
22850 close_db(data.aAuxDb[i].db);