Lines Matching +full:vm +full:- +full:valgrind +full:- +full:pointer
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.
41 ** Optionally #include a user-defined header, whereby compilation options
64 ** the browser-mode build has much different user input requirements
98 ** Enable large-file support for fopen() and friends on unix.
233 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
235 * overridden with the -batch command line option.
281 ** by the SQLite project command-line tools. These interfaces are used
284 ** "single-source" forms or separate compilation then linking.
324 ** Returns the bit-wise OR of SAC_{In,Out,Err}Console values,
328 ** the given streams. As a side-effect, the given inputs are
354 ** Undo any side-effects left by consoleClassifySetup(...).
404 ** console and translation from UTF-8 is necessary, perform
406 ** to the provided stream almost as-is, possibly with newline
417 ** console and translation from UTF-8 is necessary, perform
419 ** provided stream almost as-is, possibly with newline
467 ** An additional side-effect is that if the stream is one passed
488 ** line-editing library or simple console interaction.
500 ** SHELL_ADD_HISTORY(T) as record-worthy input is taken;
520 ** This function may call upon services of a line-editing
529 ** completion candidates for use by the line-editor.
532 ** that does this. Yet, getting line-editing melded into
533 ** console I/O is desirable because a line-editing library
539 /* Skip over as much z[] input char sequence as is valid UTF-8,
542 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
564 ** by the SQLite project command-line tools, as explained in console_io.h .
603 /* Character used to represent a known-incomplete UTF-8 char group (�) */
629 /* Define NULL-like value for things which can validly be 0. */
645 return (ppst->hx != INVALID_HANDLE_VALUE); in pstReachesConsole()
653 if( pstReachesConsole(ppst) ) SetConsoleMode(ppst->hx, ppst->consMode); in restoreConsoleArb()
665 ppst->pf = pf; in streamOfConsole()
669 ppst->hx = (rv)? fh : INVALID_HANDLE_VALUE; in streamOfConsole()
670 ppst->consMode = dwCM; in streamOfConsole()
673 ppst->pf = pf; in streamOfConsole()
674 ppst->reachesConsole = ( (short)isatty(fileno(pf)) ); in streamOfConsole()
675 return ppst->reachesConsole; in streamOfConsole()
695 return (ppst->pf != SHELL_INVALID_FILE_PTR); in isValidStreamInfo()
710 SetConsoleMode(ppst->hx, cm); in maybeSetupAsConsole()
722 &consoleInfo.pstSetup[ix] : &consoleInfo.pstDesignated[ix-3]; in consoleRenewSetup()
734 for( ix = 2; ix >= 0; --ix ){ in consoleClassifySetup()
750 if( pci->sacSetup ){ in consoleRestore()
753 if( pci->sacSetup & (SAC_InConsole<<ix) ){ in consoleRestore()
754 PerStreamTags *ppst = &pci->pstSetup[ix]; in consoleRestore()
755 SetConsoleMode(ppst->hx, ppst->consMode); in consoleRestore()
766 ** readable, and return an associated PerStreamTags pointer
775 if( apst[ix]->pf == pf ) break; in isKnownReadable()
784 ** writable, and return an associated PerStreamTags pointer
794 if( apst[ix]->pf == pf ) break; in isKnownWritable()
859 /* Translation from UTF-8 to UTF-16, then WCHARs out. */ in conZstrEmit()
860 if( WriteConsoleW(ppst->hx, zw,nwc, 0, NULL) ){ in conZstrEmit()
895 ** In either case, ppst references a caller-owned PerStreamTags
909 pfEmit = ppst->pf; in getEmitStreamInfo()
910 }else pfEmit = ppstTry->pf; in getEmitStreamInfo()
991 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst); in fPrintfUtf8()
1015 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst); in fPutsUtf8()
1066 /* Skip over as much z[] input char sequence as is valid UTF-8,
1069 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
1074 int ng = (nAccept<0)? -nAccept : 0; in zSkipValidUtf8()
1077 while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){ in zSkipValidUtf8()
1089 if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){ in zSkipValidUtf8()
1115 if( 0 == isKnownWritable(ppst->pf) ) restoreConsoleArb(ppst); in fPutbUtf8()
1174 while( noc < ncMax-8-1 && !lend ){ in fGetsUtf8()
1175 /* There is room for at least 2 more characters and a 0-terminator. */ in fGetsUtf8()
1176 int na = (ncMax > SHELL_GULP*4+1 + noc)? SHELL_GULP : (ncMax-1 - noc)/4; in fGetsUtf8()
1180 if( bRC && nbr>0 && (wcBuf[nbr-1]&0xF800)==0xD800 ){ in fGetsUtf8()
1181 /* Last WHAR read is first of a UTF-16 surrogate pair. Grab its mate. */ in fGetsUtf8()
1193 /* Fixup line-ends as coded by Windows for CR (or "Enter".) in fGetsUtf8()
1198 if( cBuf[noc-1]=='\n' ){ in fGetsUtf8()
1200 if( noc > 1 && cBuf[noc-2]=='\r' ) cBuf[--noc-1] = '\n'; in fGetsUtf8()
1215 /* If got nothing, (after ^Z chop), must be at end-of-file. */ in fGetsUtf8()
1239 * sputz(s, z) => emit 0-terminated string z to given stream s
1241 * oputz(z) => emit 0-terminated string z to default stream
1243 * eputz(z) => emit 0-terminated string z to error stream
1245 * oputb(b, n) => emit char buffer b[0..n-1] to default stream
1250 * For the stand-alone CLI, it is stdout with no .output redirect.
1285 /* Return the current wall-clock time */
1291 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ in timeOfDay()
1292 clockVfs->xCurrentTimeInt64(clockVfs, &t); in timeOfDay()
1295 clockVfs->xCurrentTime(clockVfs, &r); in timeOfDay()
1316 static sqlite3_int64 iBegin; /* Wall-clock time at start */
1330 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + in timeDiff()
1331 (double)(pEnd->tv_sec - pStart->tv_sec); in timeDiff()
1343 (iEnd - iBegin)*0.001, in endTimer()
1375 ** does, save off a pointer to it and the current process handle. in hasTimer()
1410 return (double) ((i64End - i64Start) / 10000000.0); in timeDiff()
1422 (ftWallEnd - ftWallBegin)*0.001, in endTimer()
1462 ** in order to show that UTF-16 translation is done in the sign-on
1468 ** The following is the open SQLite database. We make a pointer
1475 ** True if an interrupt (Control-C) has been received.
1495 /* This is variant of the standard-library strncpy() routine with the
1496 ** one change that the destination string is always zero-terminated, even
1497 ** if there is no zero-terminator in the first n-1 characters of the source
1502 for(i=0; i<n-1 && src[i]!=0; i++) dest[i] = src[i]; in shell_strncpy()
1510 ** or open parentheses level if non-zero, or continuation prompt as set.
1546 p->inParenLevel += ni; in trackParenLevel()
1547 if( ni==0 ) p->inParenLevel = 0; in trackParenLevel()
1548 p->zScannerAwaits = 0; in trackParenLevel()
1554 p->zScannerAwaits = s; in setLexemeOpen()
1555 p->acAwait[0] = 0; in setLexemeOpen()
1557 p->acAwait[0] = c; in setLexemeOpen()
1558 p->zScannerAwaits = p->acAwait; in setLexemeOpen()
1571 if( ndp > ncp-3 ) return continuePrompt; in dynamicContinuePrompt()
1575 PROMPT_LEN_MAX-4); in dynamicContinuePrompt()
1586 PROMPT_LEN_MAX-4); in dynamicContinuePrompt()
1593 /* Indicate out-of-memory and exit. */
1599 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
1600 ** out-of-memory error.
1634 ** then right-justify the text. W is the width in UTF-8 characters, not
1641 int aw = w<0 ? -w : w; in utf8_width_print()
1655 oputf("%*s%s", aw-n, "", zUtf); in utf8_width_print()
1657 oputf("%s%*s", zUtf, aw-n, ""); in utf8_width_print()
1666 if( *z=='-' || *z=='+' ) z++; in isNumber()
1681 if( *z=='+' || *z=='-' ) z++; in isNumber()
1691 ** lower 30 bits of a 32-bit signed integer.
1696 return 0x3fffffff & (int)(z2 - z); in strlen30()
1747 ** the text in memory obtained from malloc() and returns a pointer
1764 if( fgets(&zLine[n], nLine - n, in)==0 ){ in local_getline()
1773 if( n>0 && zLine[n-1]=='\n' ){ in local_getline()
1774 n--; in local_getline()
1775 if( n>0 && zLine[n-1]=='\r' ) n--; in local_getline()
1831 ** Return the value of a hexadecimal digit. Return -1 if the input
1835 if( c>='0' && c<='9' ) return c - '0'; in hexDigitValue()
1836 if( c>='a' && c<='f' ) return c - 'a' + 10; in hexDigitValue()
1837 if( c>='A' && c<='F' ) return c - 'A' + 10; in hexDigitValue()
1838 return -1; in hexDigitValue()
1859 if( zArg[0]=='-' ){ in integerValue()
1874 v = v*10 + zArg[0] - '0'; in integerValue()
1884 return isNeg? -v : v; in integerValue()
1904 free(p->z); in freeText()
1908 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1909 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1921 len = nAppend+p->n+1; in appendText()
1929 if( p->z==0 || p->n+len>=p->nAlloc ){ in appendText()
1930 p->nAlloc = p->nAlloc*2 + len + 20; in appendText()
1931 p->z = realloc(p->z, p->nAlloc); in appendText()
1932 shell_check_oom(p->z); in appendText()
1936 char *zCsr = p->z+p->n; in appendText()
1943 p->n = (int)(zCsr - p->z); in appendText()
1946 memcpy(p->z+p->n, zAppend, nAppend); in appendText()
1947 p->n += nAppend; in appendText()
1948 p->z[p->n] = '\0'; in appendText()
1954 ** because it contains non-alphanumeric characters, or because it is an
1989 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); in shellFakeSchema()
2021 ** Use the C-library strtod() function to convert string X into a double.
2022 ** Used for comparing the accuracy of SQLite's internal text-to-float conversion
2023 ** routines against the C-library.
2039 ** Use the C-library printf() function to convert real value X into a string.
2040 ** Used for comparing the accuracy of SQLite's internal float-to-text conversion
2041 ** routines against the C-library.
2054 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); in shellDtostr()
2061 ** Return a fake schema for the table-valued function or eponymous virtual
2076 -1, sqlite3_free); in shellModuleSchema()
2087 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
2145 sqlite3_result_text(pCtx, z, -1, sqlite3_free); in shellAddSchemaName()
2155 ** The source code for several run-time loadable extensions is inserted
2266 # define BAD_INTPTR_T ((intptr_t)(-1))
2308 ** function. This function is not thread-safe.
2347 ** This function is not thread-safe.
2360 ** The function call to GetEnvironmentVariableA() failed -OR- in windirent_getenv()
2367 ** -AND- the buffer contains the entire value. in windirent_getenv()
2386 /* TODO: Remove this if Unix-style root paths are not used. */ in opendir()
2393 dirp->d_handle = _findfirst(data.name, &data); in opendir()
2395 if( dirp->d_handle==BAD_INTPTR_T ){ in opendir()
2405 if( _findnext(dirp->d_handle, &data)==-1 ){ in opendir()
2414 dirp->d_first.d_attributes = data.attrib; in opendir()
2415 strncpy(dirp->d_first.d_name, data.name, NAME_MAX); in opendir()
2416 dirp->d_first.d_name[NAME_MAX] = '\0'; in opendir()
2431 if( dirp->d_first.d_ino==0 ){ in readdir()
2432 dirp->d_first.d_ino++; in readdir()
2433 dirp->d_next.d_ino++; in readdir()
2435 return &dirp->d_first; in readdir()
2441 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL; in readdir()
2446 dirp->d_next.d_ino++; in readdir()
2447 dirp->d_next.d_attributes = data.attrib; in readdir()
2448 strncpy(dirp->d_next.d_name, data.name, NAME_MAX); in readdir()
2449 dirp->d_next.d_name[NAME_MAX] = '\0'; in readdir()
2451 return &dirp->d_next; in readdir()
2466 if( dirp->d_first.d_ino==0 ){ in readdir_r()
2467 dirp->d_first.d_ino++; in readdir_r()
2468 dirp->d_next.d_ino++; in readdir_r()
2470 entry->d_ino = dirp->d_first.d_ino; in readdir_r()
2471 entry->d_attributes = dirp->d_first.d_attributes; in readdir_r()
2472 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX); in readdir_r()
2473 entry->d_name[NAME_MAX] = '\0'; in readdir_r()
2482 if( _findnext(dirp->d_handle, &data)==-1 ){ in readdir_r()
2490 entry->d_ino = (ino_t)-1; /* not available */ in readdir_r()
2491 entry->d_attributes = data.attrib; in readdir_r()
2492 strncpy(entry->d_name, data.name, NAME_MAX); in readdir_r()
2493 entry->d_name[NAME_MAX] = '\0'; in readdir_r()
2509 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){ in closedir()
2510 result = _findclose(dirp->d_handle); in closedir()
2524 ** 2019-01-21
2543 ** This extension is used to implement the --memtrace option of the
2544 ** command-line shell.
2576 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n", in memtraceRealloc()
2635 ** 2023-06-21
2654 ** This extension is used to implement the --pcachetrace option of the
2655 ** command-line shell.
2673 fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p) -> %d\n", pArg, nRes); in pcachetraceInit()
2691 fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d) -> %p\n", in pcachetraceCreate()
2709 fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p) -> %d\n", p, nRes); in pcachetracePagecount()
2724 fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d) -> %p\n", in pcachetraceFetch()
2817 ** 2017-03-08
2829 ** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard.
2841 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
2860 ** and whether or not that determination is run-time or compile-time.
2862 ** For best performance, an attempt is made to guess at the byte-order
2863 ** using C-preprocessor macros. If that is unsuccessful, or if
2864 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
2865 ** at run-time.
2896 ** A single step of the Keccak mixing function for a 1600-bit state
2917 # define a00 (p->u.s[0]) in KeccakF1600Step()
2918 # define a01 (p->u.s[1]) in KeccakF1600Step()
2919 # define a02 (p->u.s[2]) in KeccakF1600Step()
2920 # define a03 (p->u.s[3]) in KeccakF1600Step()
2921 # define a04 (p->u.s[4]) in KeccakF1600Step()
2922 # define a10 (p->u.s[5]) in KeccakF1600Step()
2923 # define a11 (p->u.s[6]) in KeccakF1600Step()
2924 # define a12 (p->u.s[7]) in KeccakF1600Step()
2925 # define a13 (p->u.s[8]) in KeccakF1600Step()
2926 # define a14 (p->u.s[9]) in KeccakF1600Step()
2927 # define a20 (p->u.s[10]) in KeccakF1600Step()
2928 # define a21 (p->u.s[11]) in KeccakF1600Step()
2929 # define a22 (p->u.s[12]) in KeccakF1600Step()
2930 # define a23 (p->u.s[13]) in KeccakF1600Step()
2931 # define a24 (p->u.s[14]) in KeccakF1600Step()
2932 # define a30 (p->u.s[15]) in KeccakF1600Step()
2933 # define a31 (p->u.s[16]) in KeccakF1600Step()
2934 # define a32 (p->u.s[17]) in KeccakF1600Step()
2935 # define a33 (p->u.s[18]) in KeccakF1600Step()
2936 # define a34 (p->u.s[19]) in KeccakF1600Step()
2937 # define a40 (p->u.s[20]) in KeccakF1600Step()
2938 # define a41 (p->u.s[21]) in KeccakF1600Step()
2939 # define a42 (p->u.s[22]) in KeccakF1600Step()
2940 # define a43 (p->u.s[23]) in KeccakF1600Step()
2941 # define a44 (p->u.s[24]) in KeccakF1600Step()
2942 # define ROL64(a,x) ((a<<x)|(a>>(64-x))) in KeccakF1600Step()
3223 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; in SHA3Init()
3225 p->nRate = (1600 - 2*256)/8; in SHA3Init()
3228 /* Known to be little-endian at compile-time. No-op */ in SHA3Init()
3230 p->ixMask = 7; /* Big-endian */ in SHA3Init()
3236 p->ixMask = 0; in SHA3Init()
3239 p->ixMask = 7; in SHA3Init()
3257 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ in SHA3Update()
3259 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; in SHA3Update()
3260 p->nLoaded += 8; in SHA3Update()
3261 if( p->nLoaded>=p->nRate ){ in SHA3Update()
3263 p->nLoaded = 0; in SHA3Update()
3270 p->u.x[p->nLoaded] ^= aData[i]; in SHA3Update()
3272 p->u.x[p->nLoaded^0x07] ^= aData[i]; in SHA3Update()
3274 p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; in SHA3Update()
3276 p->nLoaded++; in SHA3Update()
3277 if( p->nLoaded==p->nRate ){ in SHA3Update()
3279 p->nLoaded = 0; in SHA3Update()
3286 ** the final hash. The function returns a pointer to the binary
3291 if( p->nLoaded==p->nRate-1 ){ in SHA3Final()
3298 p->nLoaded = p->nRate - 1; in SHA3Final()
3301 for(i=0; i<p->nRate; i++){ in SHA3Final()
3302 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; in SHA3Final()
3304 return &p->u.x[p->nRate]; in SHA3Final()
3312 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
3314 ** For all other non-NULL types of input, X is converted into a UTF-8 string
3333 "384 512", -1); in sha3Func()
3369 ** argument. The results are hashed using a SIZE-bit SHA3. The default
3378 ** F<ieee-float>
3383 ** the size of that text. The SQL text is UTF-8. A single R character
3385 ** I mean an 8-byte little-endian integer <int>. F is a floating point
3386 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
3388 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
3419 "384 512", -1); in sha3QueryFunc()
3426 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); in sha3QueryFunc()
3431 sqlite3_result_error(context, zMsg, -1); in sha3QueryFunc()
3436 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); in sha3QueryFunc()
3438 sqlite3_result_error(context, zMsg, -1); in sha3QueryFunc()
3465 for(j=8; j>=1; j--){ in sha3QueryFunc()
3479 for(j=8; j>=1; j--){ in sha3QueryFunc()
3545 ** 2020-04-14
3571 ** 64-bit machine integer.
3593 x = zA[i] - zB[j]; in uintCollFunc()
3607 return -1; in uintCollFunc()
3621 return (nKey1 - i) - (nKey2 - j); in uintCollFunc()
3640 ** 2020-06-22
3651 ** Routines to implement arbitrary-precision decimal math.
3685 sqlite3_free(p->a); in decimal_clear()
3709 p->sign = 0; in decimalNewFromText()
3710 p->oom = 0; in decimalNewFromText()
3711 p->isInit = 1; in decimalNewFromText()
3712 p->isNull = 0; in decimalNewFromText()
3713 p->nDigit = 0; in decimalNewFromText()
3714 p->nFrac = 0; in decimalNewFromText()
3715 p->a = sqlite3_malloc64( n+1 ); in decimalNewFromText()
3716 if( p->a==0 ) goto new_from_text_failed; in decimalNewFromText()
3718 if( zIn[i]=='-' ){ in decimalNewFromText()
3719 p->sign = 1; in decimalNewFromText()
3728 p->a[p->nDigit++] = c - '0'; in decimalNewFromText()
3730 p->nFrac = p->nDigit + 1; in decimalNewFromText()
3735 if( zIn[j]=='-' ){ in decimalNewFromText()
3743 iExp = iExp*10 + zIn[j] - '0'; in decimalNewFromText()
3747 if( neg ) iExp = -iExp; in decimalNewFromText()
3752 if( p->nFrac ){ in decimalNewFromText()
3753 p->nFrac = p->nDigit - (p->nFrac - 1); in decimalNewFromText()
3756 if( p->nFrac>0 ){ in decimalNewFromText()
3757 if( iExp<=p->nFrac ){ in decimalNewFromText()
3758 p->nFrac -= iExp; in decimalNewFromText()
3761 iExp -= p->nFrac; in decimalNewFromText()
3762 p->nFrac = 0; in decimalNewFromText()
3766 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 ); in decimalNewFromText()
3767 if( p->a==0 ) goto new_from_text_failed; in decimalNewFromText()
3768 memset(p->a+p->nDigit, 0, iExp); in decimalNewFromText()
3769 p->nDigit += iExp; in decimalNewFromText()
3773 iExp = -iExp; in decimalNewFromText()
3774 nExtra = p->nDigit - p->nFrac - 1; in decimalNewFromText()
3777 p->nFrac += iExp; in decimalNewFromText()
3780 iExp -= nExtra; in decimalNewFromText()
3781 p->nFrac = p->nDigit - 1; in decimalNewFromText()
3785 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 ); in decimalNewFromText()
3786 if( p->a==0 ) goto new_from_text_failed; in decimalNewFromText()
3787 memmove(p->a+iExp, p->a, p->nDigit); in decimalNewFromText()
3788 memset(p->a, 0, iExp); in decimalNewFromText()
3789 p->nDigit += iExp; in decimalNewFromText()
3790 p->nFrac += iExp; in decimalNewFromText()
3797 if( p->a ) sqlite3_free(p->a); in decimalNewFromText()
3807 ** Allocate a new Decimal object from an sqlite3_value. Return a pointer
3877 if( p==0 || p->oom ){ in decimal_result()
3881 if( p->isNull ){ in decimal_result()
3885 z = sqlite3_malloc( p->nDigit+4 ); in decimal_result()
3891 if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){ in decimal_result()
3892 p->sign = 0; in decimal_result()
3894 if( p->sign ){ in decimal_result()
3895 z[0] = '-'; in decimal_result()
3898 n = p->nDigit - p->nFrac; in decimal_result()
3903 while( n>1 && p->a[j]==0 ){ in decimal_result()
3905 n--; in decimal_result()
3908 z[i++] = p->a[j] + '0'; in decimal_result()
3910 n--; in decimal_result()
3912 if( p->nFrac ){ in decimal_result()
3915 z[i++] = p->a[j] + '0'; in decimal_result()
3917 }while( j<p->nDigit ); in decimal_result()
3938 if( p==0 || p->oom ){ in decimal_result_sci()
3942 if( p->isNull ){ in decimal_result_sci()
3946 for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){} in decimal_result_sci()
3947 for(nZero=0; nZero<nDigit && p->a[nZero]==0; nZero++){} in decimal_result_sci()
3948 nFrac = p->nFrac + (nDigit - p->nDigit); in decimal_result_sci()
3949 nDigit -= nZero; in decimal_result_sci()
3961 a = &p->a[nZero]; in decimal_result_sci()
3963 if( p->sign && nDigit>0 ){ in decimal_result_sci()
3964 z[0] = '-'; in decimal_result_sci()
3979 exp = nDigit - nFrac - 1; in decimal_result_sci()
3980 sqlite3_snprintf(nDigit+20-i, &z[i], "e%+03d", exp); in decimal_result_sci()
3981 sqlite3_result_text(pCtx, z, -1, sqlite3_free); in decimal_result_sci()
3991 ** pA->isNull==0
3993 ** pB->isNull==0
3997 if( pA->sign!=pB->sign ){ in decimal_cmp()
3998 return pA->sign ? -1 : +1; in decimal_cmp()
4000 if( pA->sign ){ in decimal_cmp()
4005 nASig = pA->nDigit - pA->nFrac; in decimal_cmp()
4006 nBSig = pB->nDigit - pB->nFrac; in decimal_cmp()
4008 return nASig - nBSig; in decimal_cmp()
4010 n = pA->nDigit; in decimal_cmp()
4011 if( n>pB->nDigit ) n = pB->nDigit; in decimal_cmp()
4012 rc = memcmp(pA->a, pB->a, n); in decimal_cmp()
4014 rc = pA->nDigit - pB->nDigit; in decimal_cmp()
4035 if( pA==0 || pA->isNull ) goto cmp_done; in decimalCmpFunc()
4037 if( pB==0 || pB->isNull ) goto cmp_done; in decimalCmpFunc()
4039 if( rc<0 ) rc = -1; in decimalCmpFunc()
4055 nAddFrac = nFrac - p->nFrac; in decimal_expand()
4056 nAddSig = (nDigit - p->nDigit) - nAddFrac; in decimal_expand()
4058 p->a = sqlite3_realloc64(p->a, nDigit+1); in decimal_expand()
4059 if( p->a==0 ){ in decimal_expand()
4060 p->oom = 1; in decimal_expand()
4064 memmove(p->a+nAddSig, p->a, p->nDigit); in decimal_expand()
4065 memset(p->a, 0, nAddSig); in decimal_expand()
4066 p->nDigit += nAddSig; in decimal_expand()
4069 memset(p->a+p->nDigit, 0, nAddFrac); in decimal_expand()
4070 p->nDigit += nAddFrac; in decimal_expand()
4071 p->nFrac += nAddFrac; in decimal_expand()
4086 if( pA->oom || pB==0 || pB->oom ){ in decimal_add()
4087 pA->oom = 1; in decimal_add()
4090 if( pA->isNull || pB->isNull ){ in decimal_add()
4091 pA->isNull = 1; in decimal_add()
4094 nSig = pA->nDigit - pA->nFrac; in decimal_add()
4095 if( nSig && pA->a[0]==0 ) nSig--; in decimal_add()
4096 if( nSig<pB->nDigit-pB->nFrac ){ in decimal_add()
4097 nSig = pB->nDigit - pB->nFrac; in decimal_add()
4099 nFrac = pA->nFrac; in decimal_add()
4100 if( nFrac<pB->nFrac ) nFrac = pB->nFrac; in decimal_add()
4104 if( pA->oom || pB->oom ){ in decimal_add()
4105 pA->oom = 1; in decimal_add()
4107 if( pA->sign==pB->sign ){ in decimal_add()
4109 for(i=nDigit-1; i>=0; i--){ in decimal_add()
4110 int x = pA->a[i] + pB->a[i] + carry; in decimal_add()
4113 pA->a[i] = x - 10; in decimal_add()
4116 pA->a[i] = x; in decimal_add()
4122 rc = memcmp(pA->a, pB->a, nDigit); in decimal_add()
4124 aA = pB->a; in decimal_add()
4125 aB = pA->a; in decimal_add()
4126 pA->sign = !pA->sign; in decimal_add()
4128 aA = pA->a; in decimal_add()
4129 aB = pB->a; in decimal_add()
4131 for(i=nDigit-1; i>=0; i--){ in decimal_add()
4132 int x = aA[i] - aB[i] - borrow; in decimal_add()
4134 pA->a[i] = x+10; in decimal_add()
4137 pA->a[i] = x; in decimal_add()
4158 if( pA==0 || pA->oom || pA->isNull in decimalMul()
4159 || pB==0 || pB->oom || pB->isNull in decimalMul()
4163 acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 ); in decimalMul()
4165 pA->oom = 1; in decimalMul()
4168 memset(acc, 0, pA->nDigit + pB->nDigit + 2); in decimalMul()
4169 minFrac = pA->nFrac; in decimalMul()
4170 if( pB->nFrac<minFrac ) minFrac = pB->nFrac; in decimalMul()
4171 for(i=pA->nDigit-1; i>=0; i--){ in decimalMul()
4172 signed char f = pA->a[i]; in decimalMul()
4174 for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){ in decimalMul()
4175 x = acc[k] + f*pB->a[j] + carry; in decimalMul()
4181 acc[k-1] += x/10; in decimalMul()
4183 sqlite3_free(pA->a); in decimalMul()
4184 pA->a = acc; in decimalMul()
4186 pA->nDigit += pB->nDigit + 2; in decimalMul()
4187 pA->nFrac += pB->nFrac; in decimalMul()
4188 pA->sign ^= pB->sign; in decimalMul()
4189 while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){ in decimalMul()
4190 pA->nFrac--; in decimalMul()
4191 pA->nDigit--; in decimalMul()
4204 if( N<-20000 || N>20000 ) goto pow2_fault; in decimalPow2()
4206 if( pA==0 || pA->oom ) goto pow2_fault; in decimalPow2()
4211 N = -N; in decimalPow2()
4214 if( pX==0 || pX->oom ) goto pow2_fault; in decimalPow2()
4218 if( pA->oom ) goto pow2_fault; in decimalPow2()
4245 r = -r; in decimalFromDouble()
4255 m = a & ((((sqlite3_int64)1)<<52)-1); in decimalFromDouble()
4265 if( isNeg ) m = -m; in decimalFromDouble()
4266 e = e - 1075; in decimalFromDouble()
4288 ** point value is done. Or if X is an 8-byte blob, it is interpreted
4364 pB->sign = !pB->sign; in decimalSubFunc()
4387 if( !p->isInit ){ in decimalSumStep()
4388 p->isInit = 1; in decimalSumStep()
4389 p->a = sqlite3_malloc(2); in decimalSumStep()
4390 if( p->a==0 ){ in decimalSumStep()
4391 p->oom = 1; in decimalSumStep()
4393 p->a[0] = 0; in decimalSumStep()
4395 p->nDigit = 1; in decimalSumStep()
4396 p->nFrac = 0; in decimalSumStep()
4415 if( pArg ) pArg->sign = !pArg->sign; in decimalSumInverse()
4444 if( pA==0 || pA->oom || pA->isNull in decimalMulFunc()
4445 || pB==0 || pB->oom || pB->isNull in decimalMulFunc()
4450 if( pA->oom ){ in decimalMulFunc()
4463 ** Return the N-th power of 2. N must be an integer.
4529 ** 2022-11-18
4543 ** written as TCL brace-enclosed literals or SQL string literals,
4544 ** and can be used unmodified in XML-like documents.
4551 ** C source specifies that line-feeds are included in the encoded
4553 ** terminate any encoded blob having non-zero length.
4558 ** Generated base64 sequences, with their line-feeds included,
4567 ** This code relies on UTF-8 encoding only with respect to the
4568 ** meaning of the first 128 (7-bit) codes matching that of USASCII.
4570 ** Because it is table-driven, it could be enhanced to handle that,
4575 ** *Nix: gcc -O2 -shared -I$SQDIR -fPIC -o base64.so base64.c
4576 ** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR -o base64.dylib base64.c
4577 ** Win32: gcc -O2 -shared -I%SQDIR% -o base64.dll base64.c
4578 ** Win32: cl /Os -I%SQDIR% base64.c -link -dll -out:base64.dll
4598 #define ND 0x82 /* Not above or digit-value */
4606 /* Decoding table, ASCII (7-bit) value to base 64 digit value or other */
4644 /* Do the bit-shuffle, exploiting unsigned input to avoid masking. */ in toBase64()
4650 nbIn -= 3; in toBase64()
4665 for( nbe=3; nbe>=0; --nbe ){ in toBase64()
4680 while( nc-- > 0 && (c = *s) && !IS_BX_DIGIT(BX_DV_PROTO(c)) ) ++s; in skipNonB64()
4686 if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn; in fromBase64()
4692 ncIn -= (pUse - pIn); in fromBase64()
4695 ncIn -= nti; in fromBase64()
4703 /* Treat dark non-digits as pad, but they terminate decode too. */ in fromBase64()
4712 --nbo; in fromBase64()
4736 SQLITE_LIMIT_LENGTH, -1); in base64()
4744 nc += (nc+(B64_DARK_MAX-1))/B64_DARK_MAX + 1; /* LFs and a 0-terminator */ in base64()
4746 sqlite3_result_error(context, "blob expanded to base64 too big", -1); in base64()
4754 sqlite3_result_text(context,"",-1,SQLITE_STATIC); in base64()
4759 nc = (int)(toBase64(bBuf, nb, cBuf) - cBuf); in base64()
4766 sqlite3_result_error(context, "blob from base64 may be too big", -1); in base64()
4781 nb = (int)(fromBase64(cBuf, nc, bBuf) - bBuf); in base64()
4785 sqlite3_result_error(context, "base64 accepts only blob or text", -1); in base64()
4790 sqlite3_result_error(context, "base64 OOM", -1); in base64()
4827 ** 2022-11-16
4838 ** This is a utility for converting binary to base85 or vice-versa.
4843 ** written as TCL brace-enclosed literals or SQL string literals.
4844 ** It is not suited for unmodified use in XML-like documents.
4852 ** Base85 numerals are taken from the set of 7-bit USASCII codes,
4856 ** Groups of 4 bytes, interpreted as big-endian 32-bit values,
4857 ** are represented as 5-digit base85 numbers with MS to LS digit
4858 ** order. Groups of 1-3 bytes are represented with 2-4 digits,
4859 ** still big-endian but 8-24 bit values. (Using big-endian yields
4861 ** These byte groups can also be considered base-256 numbers.)
4862 ** Groups of 0 bytes are represented with 0 digits and vice-versa.
4864 ** (aka "group") length maps 1-to-1 to the decoded binary length.
4868 ** size, newline is used to separate it into sub-sequences of no
4875 ** a non-base85 character; the conversion to binary will then
4891 ** and variable OPTS to -DOMIT_BASE85_CHECKER if is_base85() unwanted.
4892 ** *Nix: gcc -O2 -shared -I$SQDIR $OPTS -fPIC -o base85.so base85.c
4893 ** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR $OPTS -o base85.dylib base85.c
4894 ** Win32: gcc -O2 -shared -I%SQDIR% %OPTS% -o base85.dll base85.c
4895 ** Win32: cl /Os -I%SQDIR% %OPTS% base85.c -link -dll -out:base85.dll
4898 ** *Nix or OSX: gcc -O2 -DBASE85_STANDALONE base85.c -o base85
4899 ** Win32: gcc -O2 -DBASE85_STANDALONE -o base85.exe base85.c
4900 ** Win32: cl /Os /MD -DBASE85_STANDALONE base85.c
4928 " <dirFlag> is either -r to read or -w to write <binFile>,\n"
4931 " Or, the name '-' refers to the stdin or stdout stream.\n"
4950 static u8 b85_cOffset[] = { 0, '#', 0, '*'-4, 0 };
4958 u8 dv = (u8)(c - '#');
4960 return (dv > 3)? dv-3 : dv;
4970 while( nc-- > 0 && (c = *s) && !IS_B85(c) ) ++s; in skipNonB85()
4975 * Do not use the macro form with argument expression having a side-effect.*/
4978 return (b < 4)? (char)(b + '#') : (char)(b - 4 + '*');
4982 ((char)(((dn) < 4)? (char)((dn) + '#') : (char)((dn) - 4 + '*')))
5003 unsigned char dv = qbv - 85UL*nqv; in toBase85()
5005 pOut[--nco] = base85Numeral(dv); in toBase85()
5007 nbIn -= 4; in toBase85()
5026 pOut[--nco] = base85Numeral(dv); in toBase85()
5037 if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn; in fromBase85()
5043 ncIn -= (pUse - pIn); in fromBase85()
5051 --ncIn; in fromBase85()
5053 qv = 85 * qv + (c - cdo); in fromBase85()
5054 --nti; in fromBase85()
5056 nbo -= nti; /* Adjust for early (non-digit) end of group. */ in fromBase85()
5077 while( len-- > 0 && (c = *p++) != 0 ){ in allBase85()
5102 sqlite3_result_error(context, "is_base85 accepts only text or NULL", -1); in is_base85()
5112 SQLITE_LIMIT_LENGTH, -1); in base85()
5122 sqlite3_result_error(context, "blob expanded to base85 too big", -1); in base85()
5130 sqlite3_result_text(context,"",-1,SQLITE_STATIC); in base85()
5135 nc = (int)(toBase85(bBuf, nb, cBuf, "\n") - cBuf); in base85()
5142 sqlite3_result_error(context, "blob from base85 may be too big", -1); in base85()
5157 nb = (int)(fromBase85(cBuf, nc, bBuf) - bBuf); in base85()
5161 sqlite3_result_error(context, "base85 accepts only blob or text.", -1); in base85()
5166 sqlite3_result_error(context, "base85 OOM", -1); in base85()
5220 if( na < 3 || av[1][0]!='-' || (rw = av[1][1])==0 || (rw!='r' && rw!='w') ){ in main()
5225 if( av[2][0]=='-' && av[2][1]==0 ){ in main()
5254 size_t nbo = fromBase85( cBuf, nc, bBuf ) - bBuf; in main()
5269 fprintf(stderr, "Base85 input had non-base85 dark or control content.\n"); in main()
5280 ** 2013-04-17
5292 ** and input of IEEE754 Binary64 floating-point numbers.
5297 ** In the first form, the value X should be a floating-point number.
5302 ** base-2 exponent of a new floating point number. The function returns
5303 ** a floating-point value equal to Y*pow(2,Z).
5307 ** ieee754(2.0) -> 'ieee754(2,0)'
5308 ** ieee754(45.25) -> 'ieee754(181,-2)'
5309 ** ieee754(2, 0) -> 2.0
5310 ** ieee754(181, -2) -> 45.25
5312 ** Two additional functions break apart the one-argument ieee754()
5315 ** ieee754_mantissa(45.25) -> 181
5316 ** ieee754_exponent(45.25) -> -2
5320 ** ieee754_from_blob(x'3ff0000000000000') -> 1.0
5321 ** ieee754_to_blob(1.0) -> x'3ff0000000000000'
5323 ** In all single-argument functions, if the argument is an 8-byte blob
5324 ** then that blob is interpreted as a big-endian binary64 value.
5328 ** -----------------------------------------------
5341 ** VALUES(-1,'0.5')
5343 ** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5350 ** ---------------^^^^^---- Replace with whatever you want
5407 r = -r; in ieee754func()
5417 m = a & ((((sqlite3_int64)1)<<52)-1); in ieee754func()
5427 if( isNeg ) m = -m; in ieee754func()
5432 m, e-1075); in ieee754func()
5433 sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT); in ieee754func()
5439 sqlite3_result_int(context, e-1075); in ieee754func()
5449 /* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */ in ieee754func()
5452 }else if( e<-10000 ){ in ieee754func()
5453 e = -10000; in ieee754func()
5458 m = -m; in ieee754func()
5460 }else if( m==0 && e>-1000 && e<1000 ){ in ieee754func()
5470 e--; in ieee754func()
5475 if( 1-e >= 64 ){ in ieee754func()
5478 m >>= 1-e; in ieee754func()
5484 a = m & ((((sqlite3_int64)1)<<52)-1); in ieee754func()
5530 a[sizeof(r)-i] = v&0xff; in ieee754func_to_blob()
5543 ** Behind the scenes: this routine merely casts r into a 64-bit unsigned
5607 ** 2015-08-18, 2023-04-28
5618 ** This file demonstrates how to create a table-valued-function using
5621 ** within the limitation that its arguments are signed 64-bit integers.
5625 ** ( V[n] == start + n * step && sgn(V[n] - stop) * sgn(step) >= 0 )
5630 ** The stop parameter defaults to (1<<32)-1 (aka 4294967295 or 0xffffffff)
5648 ** SELECT * FROM generate_series(0,-100,-5);
5650 ** Integers 0 -5 -10 ... -100.
5652 ** SELECT * FROM generate_series(0,-1);
5697 ** series are well-defined.
5707 ** Return that member of a generate_series(...) sequence whose 0-based
5716 ix -= (sqlite3_uint64)LLONG_MAX; in genSeqMember()
5720 smBase += (LLONG_MAX - LLONG_MAX/2) * smStep; in genSeqMember()
5727 ix -= ix2; in genSeqMember()
5752 pss->uSeqIndexMax = 0; in setupSequence()
5753 pss->isNotEOF = 0; in setupSequence()
5754 bSameSigns = (pss->iBase < 0)==(pss->iTerm < 0); in setupSequence()
5755 if( pss->iTerm < pss->iBase ){ in setupSequence()
5758 nuspan = (sqlite3_uint64)(pss->iBase - pss->iTerm); in setupSequence()
5763 nuspan += pss->iBase; in setupSequence()
5764 nuspan += -(pss->iTerm+1); in setupSequence()
5766 if( pss->iStep<0 ){ in setupSequence()
5767 pss->isNotEOF = 1; in setupSequence()
5769 pss->uSeqIndexMax = ( pss->iStep>LLONG_MIN )? nuspan/-pss->iStep : 1; in setupSequence()
5770 }else if( pss->iStep>LLONG_MIN ){ in setupSequence()
5771 pss->uSeqIndexMax = nuspan/-pss->iStep; in setupSequence()
5774 }else if( pss->iTerm > pss->iBase ){ in setupSequence()
5777 puspan = (sqlite3_uint64)(pss->iTerm - pss->iBase); in setupSequence()
5782 puspan += pss->iTerm; in setupSequence()
5783 puspan += -(pss->iBase+1); in setupSequence()
5785 if( pss->iStep>0 ){ in setupSequence()
5786 pss->isNotEOF = 1; in setupSequence()
5787 pss->uSeqIndexMax = puspan/pss->iStep; in setupSequence()
5789 }else if( pss->iTerm == pss->iBase ){ in setupSequence()
5790 pss->isNotEOF = 1; in setupSequence()
5791 pss->uSeqIndexMax = 0; in setupSequence()
5793 pss->uSeqIndexNow = (pss->isReversing)? pss->uSeqIndexMax : 0; in setupSequence()
5794 pss->iValueNow = (pss->isReversing) in setupSequence()
5795 ? genSeqMember(pss->iBase, pss->iStep, pss->uSeqIndexMax) in setupSequence()
5796 : pss->iBase; in setupSequence()
5805 if( !pss->isNotEOF ) return 0; in progressSequence()
5806 if( pss->isReversing ){ in progressSequence()
5807 if( pss->uSeqIndexNow > 0 ){ in progressSequence()
5808 pss->uSeqIndexNow--; in progressSequence()
5809 pss->iValueNow -= pss->iStep; in progressSequence()
5811 pss->isNotEOF = 0; in progressSequence()
5814 if( pss->uSeqIndexNow < pss->uSeqIndexMax ){ in progressSequence()
5815 pss->uSeqIndexNow++; in progressSequence()
5816 pss->iValueNow += pss->iStep; in progressSequence()
5818 pss->isNotEOF = 0; in progressSequence()
5821 return pss->isNotEOF; in progressSequence()
5830 sqlite3_vtab_cursor base; /* Base class - must be first */
5895 *ppCursor = &pCur->base; in seriesOpen()
5913 progressSequence( & pCur->ss ); in seriesNext()
5929 case SERIES_COLUMN_START: x = pCur->ss.iBase; break; in seriesColumn()
5930 case SERIES_COLUMN_STOP: x = pCur->ss.iTerm; break; in seriesColumn()
5931 case SERIES_COLUMN_STEP: x = pCur->ss.iStep; break; in seriesColumn()
5932 default: x = pCur->ss.iValueNow; break; in seriesColumn()
5948 sqlite3_uint64 n = pCur->ss.uSeqIndexNow; in seriesRowid()
5959 return !pCur->ss.isNotEOF; in seriesEof()
5962 /* True to cause run-time checking of the start=, stop=, and/or step=
6001 pCur->ss.iBase = sqlite3_value_int64(argv[i++]); in seriesFilter()
6003 pCur->ss.iBase = 0; in seriesFilter()
6006 pCur->ss.iTerm = sqlite3_value_int64(argv[i++]); in seriesFilter()
6008 pCur->ss.iTerm = 0xffffffff; in seriesFilter()
6011 pCur->ss.iStep = sqlite3_value_int64(argv[i++]); in seriesFilter()
6012 if( pCur->ss.iStep==0 ){ in seriesFilter()
6013 pCur->ss.iStep = 1; in seriesFilter()
6014 }else if( pCur->ss.iStep<0 ){ in seriesFilter()
6018 pCur->ss.iStep = 1; in seriesFilter()
6024 pCur->ss.iBase = 1; in seriesFilter()
6025 pCur->ss.iTerm = 0; in seriesFilter()
6026 pCur->ss.iStep = 1; in seriesFilter()
6031 pCur->ss.isReversing = pCur->ss.iStep > 0; in seriesFilter()
6033 pCur->ss.isReversing = pCur->ss.iStep < 0; in seriesFilter()
6035 setupSequence( &pCur->ss ); in seriesFilter()
6050 ** (1) start = $value -- constraint exists
6051 ** (2) stop = $value -- constraint exists
6052 ** (4) step = $value -- constraint exists
6072 aIdx[0] = aIdx[1] = aIdx[2] = -1; in seriesBestIndex()
6073 pConstraint = pIdxInfo->aConstraint; in seriesBestIndex()
6074 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ in seriesBestIndex()
6077 if( pConstraint->iColumn<SERIES_COLUMN_START ) continue; in seriesBestIndex()
6078 iCol = pConstraint->iColumn - SERIES_COLUMN_START; in seriesBestIndex()
6082 if( pConstraint->usable==0 ){ in seriesBestIndex()
6085 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in seriesBestIndex()
6092 pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg; in seriesBestIndex()
6093 pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY; in seriesBestIndex()
6098 ** first argument was omitted. Compile with -DZERO_ARGUMENT_GENERATE_SERIES in seriesBestIndex()
6102 sqlite3_free(pVTab->zErrMsg); in seriesBestIndex()
6103 pVTab->zErrMsg = sqlite3_mprintf( in seriesBestIndex()
6117 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0)); in seriesBestIndex()
6118 pIdxInfo->estimatedRows = 1000; in seriesBestIndex()
6119 if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){ in seriesBestIndex()
6120 if( pIdxInfo->aOrderBy[0].desc ){ in seriesBestIndex()
6125 pIdxInfo->orderByConsumed = 1; in seriesBestIndex()
6131 pIdxInfo->estimatedRows = 2147483647; in seriesBestIndex()
6133 pIdxInfo->idxNum = idxNum; in seriesBestIndex()
6148 seriesOpen, /* xOpen - open a cursor */
6149 seriesClose, /* xClose - close a cursor */
6150 seriesFilter, /* xFilter - configure scan constraints */
6151 seriesNext, /* xNext - advance a cursor */
6152 seriesEof, /* xEof - check for end of scan */
6153 seriesColumn, /* xColumn - read data */
6154 seriesRowid, /* xRowid - read data */
6195 ** 2012-11-13
6207 ** efficient regular-expression matcher for posix extended regular
6229 ** \c C-language escapes for c in afnrtv. ex: \t or \n
6234 ** [a-z] Any single character in the range a-z
6235 ** [^a-z] Any single character not in the range a-z
6237 ** \w Word character. [A-Za-z0-9_]
6238 ** \W Non-word character
6240 ** \D Non-digit
6242 ** \S Non-whitespace character
6248 ** to p copies of X following by q-p copies of X? and that the size of the
6259 ** this file to prevent name collisions with C-library functions of the
6266 /* The end-of-input character */
6268 #define RE_START 0xfffffff /* Start of input - larger than an UTF-8 */
6283 #define RE_OP_WORD 11 /* Perl word character [A-Za-z0-9_] */
6285 #define RE_OP_DIGIT 13 /* digit: [0-9] */
6289 #define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
6359 for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return; in re_add_state()
6360 pSet->aState[pSet->nState++] = (ReStateNumber)newState; in re_add_state()
6370 if( p->i>=p->mx ) return 0; in re_next_char()
6371 c = p->z[p->i++]; in re_next_char()
6373 if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){ in re_next_char()
6374 c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f); in re_next_char()
6376 }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80 in re_next_char()
6377 && (p->z[p->i+1]&0xc0)==0x80 ){ in re_next_char()
6378 c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f); in re_next_char()
6379 p->i += 2; in re_next_char()
6381 }else if( (c&0xf8)==0xf0 && p->i+2<p->mx && (p->z[p->i]&0xc0)==0x80 in re_next_char()
6382 && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){ in re_next_char()
6383 c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6) in re_next_char()
6384 | (p->z[p->i+2]&0x3f); in re_next_char()
6385 p->i += 3; in re_next_char()
6395 if( c>='A' && c<='Z' ) c += 'a' - 'A'; in re_next_char_nocase()
6399 /* Return true if c is a perl "word" character: [A-Za-z0-9_] */
6405 /* Return true if c is a "digit" character: [0-9] */
6415 /* Run a compiled regular expression on the zero-terminated input
6434 if( pRe->nInit ){ in re_match()
6435 unsigned char x = pRe->zInit[0]; in re_match()
6436 while( in.i+pRe->nInit<=in.mx in re_match()
6438 strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0) in re_match()
6442 if( in.i+pRe->nInit>in.mx ) return 0; in re_match()
6443 c = RE_START-1; in re_match()
6446 if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){ in re_match()
6450 pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState ); in re_match()
6451 if( pToFree==0 ) return -1; in re_match()
6454 aStateSet[1].aState = &aStateSet[0].aState[pRe->nState]; in re_match()
6456 pNext->nState = 0; in re_match()
6458 while( c!=RE_EOF && pNext->nState>0 ){ in re_match()
6460 c = pRe->xNextChar(&in); in re_match()
6463 iSwap = 1 - iSwap; in re_match()
6464 pNext->nState = 0; in re_match()
6465 for(i=0; i<pThis->nState; i++){ in re_match()
6466 int x = pThis->aState[i]; in re_match()
6467 switch( pRe->aOp[x] ){ in re_match()
6469 if( pRe->aArg[x]==c ) re_add_state(pNext, x+1); in re_match()
6514 re_add_state(pThis, x+pRe->aArg[x]); in re_match()
6519 re_add_state(pThis, x+pRe->aArg[x]); in re_match()
6528 /* fall-through */ goto re_op_cc_inc; in re_match()
6532 int n = pRe->aArg[x]; in re_match()
6535 if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){ in re_match()
6536 if( pRe->aArg[x+j]==c ){ in re_match()
6538 j = -1; in re_match()
6541 if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){ in re_match()
6543 j = -1; in re_match()
6549 if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit; in re_match()
6556 for(i=0; i<pNext->nState; i++){ in re_match()
6557 int x = pNext->aState[i]; in re_match()
6558 while( pRe->aOp[x]==RE_OP_GOTO ) x += pRe->aArg[x]; in re_match()
6559 if( pRe->aOp[x]==RE_OP_ACCEPT ){ rc = 1; break; } in re_match()
6571 aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0])); in re_resize()
6573 p->aOp = aOp; in re_resize()
6574 aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0])); in re_resize()
6576 p->aArg = aArg; in re_resize()
6577 p->nAlloc = N; in re_resize()
6586 if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0; in re_insert()
6587 for(i=p->nState; i>iBefore; i--){ in re_insert()
6588 p->aOp[i] = p->aOp[i-1]; in re_insert()
6589 p->aArg[i] = p->aArg[i-1]; in re_insert()
6591 p->nState++; in re_insert()
6592 p->aOp[iBefore] = (char)op; in re_insert()
6593 p->aArg[iBefore] = arg; in re_insert()
6600 return re_insert(p, p->nState, op, arg); in re_append()
6607 if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return; in re_copy()
6608 memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0])); in re_copy()
6609 memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0])); in re_copy()
6610 p->nState += N; in re_copy()
6613 /* Return true if c is a hexadecimal digit character: [0-9a-fA-F]
6619 c -= '0'; in re_hex()
6621 c -= 'a' - 10; in re_hex()
6623 c -= 'A' - 10; in re_hex()
6639 if( p->sIn.i>=p->sIn.mx ) return 0; in re_esc_char()
6640 c = p->sIn.z[p->sIn.i]; in re_esc_char()
6641 if( c=='u' && p->sIn.i+4<p->sIn.mx ){ in re_esc_char()
6642 const unsigned char *zIn = p->sIn.z + p->sIn.i; in re_esc_char()
6648 p->sIn.i += 5; in re_esc_char()
6652 if( c=='x' && p->sIn.i+2<p->sIn.mx ){ in re_esc_char()
6653 const unsigned char *zIn = p->sIn.z + p->sIn.i; in re_esc_char()
6657 p->sIn.i += 3; in re_esc_char()
6664 p->sIn.i++; in re_esc_char()
6666 p->zErr = "unknown \\ escape"; in re_esc_char()
6676 return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0; in rePeek()
6681 ** return a pointer to the error message string.
6686 iStart = p->nState; in re_subcompile_re()
6690 iEnd = p->nState; in re_subcompile_re()
6691 re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart); in re_subcompile_re()
6693 p->sIn.i++; in re_subcompile_re()
6696 p->aArg[iGoto] = p->nState - iGoto; in re_subcompile_re()
6702 ** an operand to the "|" operator). Return NULL on success or a pointer
6706 int iPrev = -1; in re_subcompile_string()
6710 while( (c = p->xNextChar(&p->sIn))!=0 ){ in re_subcompile_string()
6711 iStart = p->nState; in re_subcompile_string()
6715 p->sIn.i--; in re_subcompile_string()
6722 p->sIn.i++; in re_subcompile_string()
6728 p->sIn.i++; in re_subcompile_string()
6736 re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1); in re_subcompile_string()
6737 re_append(p, RE_OP_FORK, iPrev - p->nState + 1); in re_subcompile_string()
6742 re_append(p, RE_OP_FORK, iPrev - p->nState); in re_subcompile_string()
6747 re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1); in re_subcompile_string()
6762 while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; } in re_subcompile_string()
6765 p->sIn.i++; in re_subcompile_string()
6767 while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; } in re_subcompile_string()
6771 p->sIn.i++; in re_subcompile_string()
6772 sz = p->nState - iPrev; in re_subcompile_string()
6777 n--; in re_subcompile_string()
6786 re_append(p, RE_OP_FORK, -sz); in re_subcompile_string()
6791 unsigned int iFirst = p->nState; in re_subcompile_string()
6794 p->sIn.i++; in re_subcompile_string()
6798 while( (c = p->xNextChar(&p->sIn))!=0 ){ in re_subcompile_string()
6803 if( rePeek(p)=='-' ){ in re_subcompile_string()
6805 p->sIn.i++; in re_subcompile_string()
6806 c = p->xNextChar(&p->sIn); in re_subcompile_string()
6812 if( rePeek(p)==']' ){ p->sIn.i++; break; } in re_subcompile_string()
6815 if( p->nState>iFirst ) p->aArg[iFirst] = p->nState - iFirst; in re_subcompile_string()
6830 p->sIn.i++; in re_subcompile_string()
6854 sqlite3_free(pRe->aOp); in re_free()
6855 sqlite3_free(pRe->aArg); in re_free()
6862 ** expression suitable for us by re_match() and return a pointer to the
6877 pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char; in re_compile()
6887 pRe->sIn.z = (unsigned char*)zIn; in re_compile()
6888 pRe->sIn.i = 0; in re_compile()
6889 pRe->sIn.mx = (int)strlen(zIn); in re_compile()
6895 if( pRe->sIn.i>=pRe->sIn.mx ){ in re_compile()
6909 ** unicode characters beyond plane 0 - those are very rare and this is in re_compile()
6911 if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){ in re_compile()
6912 for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){ in re_compile()
6913 unsigned x = pRe->aArg[i]; in re_compile()
6915 pRe->zInit[j++] = (unsigned char)x; in re_compile()
6917 pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6)); in re_compile()
6918 pRe->zInit[j++] = 0x80 | (x&0x3f); in re_compile()
6920 pRe->zInit[j++] = (unsigned char)(0xe0 | (x>>12)); in re_compile()
6921 pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f); in re_compile()
6922 pRe->zInit[j++] = 0x80 | (x&0x3f); in re_compile()
6927 if( j>0 && pRe->zInit[j-1]==0 ) j--; in re_compile()
6928 pRe->nInit = j; in re_compile()
6930 return pRe->zErr; in re_compile()
6935 ** the build-in REGEXP operator. The first argument to the function is the
6961 sqlite3_result_error(context, zErr, -1); in re_sql_func()
6972 sqlite3_result_int(context, re_match(pRe, zStr, -1)); in re_sql_func()
6982 ** if the SQLITE_DEBUG compile-time option is used.
7006 sqlite3_result_error(context, zErr, -1); in re_bytecode_func()
7015 if( pRe->nInit>0 ){ in re_bytecode_func()
7017 for(i=0; i<pRe->nInit; i++){ in re_bytecode_func()
7018 sqlite3_str_appendf(pStr, "%02x", pRe->zInit[i]); in re_bytecode_func()
7022 for(i=0; (unsigned)i<pRe->nState; i++){ in re_bytecode_func()
7023 sqlite3_str_appendf(pStr, "%-8s %4d\n", in re_bytecode_func()
7024 ReOpName[(unsigned char)pRe->aOp[i]], pRe->aArg[i]); in re_bytecode_func()
7031 sqlite3_result_text(context, z, n-1, sqlite3_free); in re_bytecode_func()
7060 /* The regexpi(PATTERN,STRING) function is a case-insensitive version in sqlite3_regexp_init()
7080 ** 2014-06-13
7100 ** If the first option argument - MODE - is present, then it must
7117 ** as an integer - the number of seconds since the unix epoch. The
7118 ** modification-time of the target file is set to this value before
7149 ** If a non-NULL value is specified for the optional $dir parameter and
7156 ** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
7193 ** Structure of the fsdir() table-valued function
7232 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1); in readFileContents()
7279 sqlite3_result_error(ctx, zMsg, -1); in ctxErrorMsg()
7287 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
7305 fileIntervals.LowPart = pFileTime->dwLowDateTime; in fileTimeToUnixTime()
7306 fileIntervals.HighPart = pFileTime->dwHighDateTime; in fileTimeToUnixTime()
7308 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000; in fileTimeToUnixTime()
7318 int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0); in utf8_to_utf16()
7320 if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) ) in utf8_to_utf16()
7345 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime); in statTimesToUtc()
7346 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime); in statTimesToUtc()
7347 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime); in statTimesToUtc()
7447 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */ in writeFile()
7460 ** be an error though - if there is already a directory at the same in writeFile()
7565 sqlite3_int64 mtime = -1; in writefileFunc()
7569 "wrong number of arguments to function writefile()", -1 in writefileFunc()
7604 ** Given a numberic st_mode from stat(), convert it into a human-readable
7605 ** text string in the style of "ls -l".
7619 z[0] = '-'; in lsModeFunc()
7626 int m = (iMode >> ((2-i)*3)); in lsModeFunc()
7628 a[0] = (m & 0x4) ? 'r' : '-'; in lsModeFunc()
7629 a[1] = (m & 0x2) ? 'w' : '-'; in lsModeFunc()
7630 a[2] = (m & 0x1) ? 'x' : '-'; in lsModeFunc()
7633 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); in lsModeFunc()
7646 char *zDir; /* Name of directory (nul-terminated) */
7650 sqlite3_vtab_cursor base; /* Base class - must be first */
7666 sqlite3_vtab base; /* Base class - must be first */
7713 pCur->iLvl = -1; in fsdirOpen()
7714 *ppCursor = &pCur->base; in fsdirOpen()
7724 for(i=0; i<=pCur->iLvl; i++){ in fsdirResetCursor()
7725 FsdirLevel *pLvl = &pCur->aLvl[i]; in fsdirResetCursor()
7726 if( pLvl->pDir ) closedir(pLvl->pDir); in fsdirResetCursor()
7727 sqlite3_free(pLvl->zDir); in fsdirResetCursor()
7729 sqlite3_free(pCur->zPath); in fsdirResetCursor()
7730 sqlite3_free(pCur->aLvl); in fsdirResetCursor()
7731 pCur->aLvl = 0; in fsdirResetCursor()
7732 pCur->zPath = 0; in fsdirResetCursor()
7733 pCur->zBase = 0; in fsdirResetCursor()
7734 pCur->nBase = 0; in fsdirResetCursor()
7735 pCur->nLvl = 0; in fsdirResetCursor()
7736 pCur->iLvl = -1; in fsdirResetCursor()
7737 pCur->iRowid = 1; in fsdirResetCursor()
7758 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); in fsdirSetErrmsg()
7768 mode_t m = pCur->sStat.st_mode; in fsdirNext()
7770 pCur->iRowid++; in fsdirNext()
7773 int iNew = pCur->iLvl + 1; in fsdirNext()
7775 if( iNew>=pCur->nLvl ){ in fsdirNext()
7778 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte); in fsdirNext()
7780 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl)); in fsdirNext()
7781 pCur->aLvl = aNew; in fsdirNext()
7782 pCur->nLvl = nNew; in fsdirNext()
7784 pCur->iLvl = iNew; in fsdirNext()
7785 pLvl = &pCur->aLvl[iNew]; in fsdirNext()
7787 pLvl->zDir = pCur->zPath; in fsdirNext()
7788 pCur->zPath = 0; in fsdirNext()
7789 pLvl->pDir = opendir(pLvl->zDir); in fsdirNext()
7790 if( pLvl->pDir==0 ){ in fsdirNext()
7791 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath); in fsdirNext()
7796 while( pCur->iLvl>=0 ){ in fsdirNext()
7797 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl]; in fsdirNext()
7798 struct dirent *pEntry = readdir(pLvl->pDir); in fsdirNext()
7800 if( pEntry->d_name[0]=='.' ){ in fsdirNext()
7801 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue; in fsdirNext()
7802 if( pEntry->d_name[1]=='\0' ) continue; in fsdirNext()
7804 sqlite3_free(pCur->zPath); in fsdirNext()
7805 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name); in fsdirNext()
7806 if( pCur->zPath==0 ) return SQLITE_NOMEM; in fsdirNext()
7807 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ in fsdirNext()
7808 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); in fsdirNext()
7813 closedir(pLvl->pDir); in fsdirNext()
7814 sqlite3_free(pLvl->zDir); in fsdirNext()
7815 pLvl->pDir = 0; in fsdirNext()
7816 pLvl->zDir = 0; in fsdirNext()
7817 pCur->iLvl--; in fsdirNext()
7821 sqlite3_free(pCur->zPath); in fsdirNext()
7822 pCur->zPath = 0; in fsdirNext()
7838 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT); in fsdirColumn()
7843 sqlite3_result_int64(ctx, pCur->sStat.st_mode); in fsdirColumn()
7847 sqlite3_result_int64(ctx, pCur->sStat.st_mtime); in fsdirColumn()
7851 mode_t m = pCur->sStat.st_mode; in fsdirColumn()
7862 n = readlink(pCur->zPath, aBuf, nBuf); in fsdirColumn()
7877 readFileContents(ctx, pCur->zPath); in fsdirColumn()
7897 *pRowid = pCur->iRowid; in fsdirRowid()
7907 return (pCur->zPath==0); in fsdirEof()
7934 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument"); in fsdirFilter()
7938 pCur->zBase = (const char*)sqlite3_value_text(argv[1]); in fsdirFilter()
7940 if( pCur->zBase ){ in fsdirFilter()
7941 pCur->nBase = (int)strlen(pCur->zBase)+1; in fsdirFilter()
7942 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir); in fsdirFilter()
7944 pCur->zPath = sqlite3_mprintf("%s", zDir); in fsdirFilter()
7947 if( pCur->zPath==0 ){ in fsdirFilter()
7950 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ in fsdirFilter()
7951 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); in fsdirFilter()
7977 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */ in fsdirBestIndex()
7978 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */ in fsdirBestIndex()
7984 pConstraint = pIdxInfo->aConstraint; in fsdirBestIndex()
7985 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ in fsdirBestIndex()
7986 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; in fsdirBestIndex()
7987 switch( pConstraint->iColumn ){ in fsdirBestIndex()
7989 if( pConstraint->usable ){ in fsdirBestIndex()
7998 if( pConstraint->usable ){ in fsdirBestIndex()
8014 pIdxInfo->idxNum = 0; in fsdirBestIndex()
8015 /* The pIdxInfo->estimatedCost should have been initialized to a huge in fsdirBestIndex()
8017 pIdxInfo->estimatedRows = 0x7fffffff; in fsdirBestIndex()
8019 pIdxInfo->aConstraintUsage[idxPath].omit = 1; in fsdirBestIndex()
8020 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1; in fsdirBestIndex()
8022 pIdxInfo->aConstraintUsage[idxDir].omit = 1; in fsdirBestIndex()
8023 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2; in fsdirBestIndex()
8024 pIdxInfo->idxNum = 2; in fsdirBestIndex()
8025 pIdxInfo->estimatedCost = 10.0; in fsdirBestIndex()
8027 pIdxInfo->idxNum = 1; in fsdirBestIndex()
8028 pIdxInfo->estimatedCost = 100.0; in fsdirBestIndex()
8046 fsdirOpen, /* xOpen - open a cursor */ in fsdirRegister()
8047 fsdirClose, /* xClose - close a cursor */ in fsdirRegister()
8048 fsdirFilter, /* xFilter - configure scan constraints */ in fsdirRegister()
8049 fsdirNext, /* xNext - advance a cursor */ in fsdirRegister()
8050 fsdirEof, /* xEof - check for end of scan */ in fsdirRegister()
8051 fsdirColumn, /* xColumn - read data */ in fsdirRegister()
8052 fsdirRowid, /* xRowid - read data */ in fsdirRegister()
8089 rc = sqlite3_create_function(db, "writefile", -1, in sqlite3_fileio_init()
8114 ** 2017-07-10
8161 sqlite3_vtab base; /* Base class - must be first */
8171 sqlite3_vtab_cursor base; /* Base class - must be first */
8181 int j; /* inter-phase counter */
8231 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */ in completionConnect()
8246 pNew->db = db; in completionConnect()
8267 pCur->db = ((completion_vtab*)p)->db; in completionOpen()
8268 *ppCursor = &pCur->base; in completionOpen()
8276 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0; in completionCursorReset()
8277 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0; in completionCursorReset()
8278 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0; in completionCursorReset()
8279 pCur->j = 0; in completionCursorReset()
8294 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
8295 ** record the current state of the scan. This routine sets ->zCurrentRow
8297 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
8308 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */ in completionNext()
8309 pCur->iRowid++; in completionNext()
8310 while( pCur->ePhase!=COMPLETION_EOF ){ in completionNext()
8311 switch( pCur->ePhase ){ in completionNext()
8313 if( pCur->j >= sqlite3_keyword_count() ){ in completionNext()
8314 pCur->zCurrentRow = 0; in completionNext()
8315 pCur->ePhase = COMPLETION_DATABASES; in completionNext()
8317 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow); in completionNext()
8319 iCol = -1; in completionNext()
8323 if( pCur->pStmt==0 ){ in completionNext()
8324 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, in completionNext()
8325 &pCur->pStmt, 0); in completionNext()
8332 if( pCur->pStmt==0 ){ in completionNext()
8336 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); in completionNext()
8348 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); in completionNext()
8356 if( pCur->pStmt==0 ){ in completionNext()
8360 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); in completionNext()
8374 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); in completionNext()
8384 if( pCur->zCurrentRow==0 ) continue; in completionNext()
8386 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){ in completionNext()
8388 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol); in completionNext()
8389 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol); in completionNext()
8392 sqlite3_finalize(pCur->pStmt); in completionNext()
8393 pCur->pStmt = 0; in completionNext()
8394 pCur->ePhase = eNextPhase; in completionNext()
8398 if( pCur->nPrefix==0 ) break; in completionNext()
8399 if( pCur->nPrefix<=pCur->szRow in completionNext()
8400 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 in completionNext()
8421 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT); in completionColumn()
8425 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT); in completionColumn()
8429 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT); in completionColumn()
8433 sqlite3_result_int(ctx, pCur->ePhase); in completionColumn()
8446 *pRowid = pCur->iRowid; in completionRowid()
8456 return pCur->ePhase >= COMPLETION_EOF; in completionEof()
8476 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]); in completionFilter()
8477 if( pCur->nPrefix>0 ){ in completionFilter()
8478 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); in completionFilter()
8479 if( pCur->zPrefix==0 ) return SQLITE_NOMEM; in completionFilter()
8484 pCur->nLine = sqlite3_value_bytes(argv[iArg]); in completionFilter()
8485 if( pCur->nLine>0 ){ in completionFilter()
8486 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); in completionFilter()
8487 if( pCur->zLine==0 ) return SQLITE_NOMEM; in completionFilter()
8490 if( pCur->zLine!=0 && pCur->zPrefix==0 ){ in completionFilter()
8491 int i = pCur->nLine; in completionFilter()
8492 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){ in completionFilter()
8493 i--; in completionFilter()
8495 pCur->nPrefix = pCur->nLine - i; in completionFilter()
8496 if( pCur->nPrefix>0 ){ in completionFilter()
8497 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i); in completionFilter()
8498 if( pCur->zPrefix==0 ) return SQLITE_NOMEM; in completionFilter()
8501 pCur->iRowid = 0; in completionFilter()
8502 pCur->ePhase = COMPLETION_FIRST_PHASE; in completionFilter()
8512 ** There are two hidden parameters that act as arguments to the table-valued
8522 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */ in completionBestIndex()
8523 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */ in completionBestIndex()
8528 pConstraint = pIdxInfo->aConstraint; in completionBestIndex()
8529 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ in completionBestIndex()
8530 if( pConstraint->usable==0 ) continue; in completionBestIndex()
8531 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; in completionBestIndex()
8532 switch( pConstraint->iColumn ){ in completionBestIndex()
8544 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg; in completionBestIndex()
8545 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1; in completionBestIndex()
8548 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg; in completionBestIndex()
8549 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1; in completionBestIndex()
8551 pIdxInfo->idxNum = idxNum; in completionBestIndex()
8552 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg; in completionBestIndex()
8553 pIdxInfo->estimatedRows = 500 - 100*nArg; in completionBestIndex()
8568 completionOpen, /* xOpen - open a cursor */
8569 completionClose, /* xClose - close a cursor */
8570 completionFilter, /* xFilter - configure scan constraints */
8571 completionNext, /* xNext - advance a cursor */
8572 completionEof, /* xEof - check for end of scan */
8573 completionColumn, /* xColumn - read data */
8574 completionRowid, /* xRowid - read data */
8619 ** 2017-10-20
8647 ** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
8664 ** this shim is a pass-through into the default underlying VFS. (rule 3)
8673 ** Start-Of-SQLite3-NNNNNNNN
8676 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
8679 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
8685 ** Maximum size of the combined prefix + database + append-mark. This
8696 #define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1))
8705 /* Access to a lower-level VFS that (might) implement dynamic loading,
8708 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
8717 ** append-mark.
8721 ** +-------------+---------+----------+-------------+
8722 ** | prefix-file | padding | database | append-mark |
8723 ** +-------------+---------+----------+-------------+
8729 ** "prefix file" - file onto which the database has been appended.
8730 ** "padding" - zero or more bytes inserted so that "database"
8732 ** "database" - The SQLite database file
8733 ** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
8734 ** the offset from the start of prefix-file to the start
8737 ** The size of the database is iMark - iPgOne.
8739 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
8740 ** of iPgOne stored as a big-ending 64-bit integer.
8743 ** Or, iMark is -1 to indicate that it has not yet been written.
8748 sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */
8842 ** Close an apnd-file.
8846 return pFile->pMethods->xClose(pFile); in apndClose()
8850 ** Read data from an apnd-file.
8860 return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst); in apndRead()
8864 ** Add the append-mark onto what should become the end of the file.
8866 * Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
8873 sqlite_int64 iPgOne = paf->iPgOne; in apndWriteMark()
8879 while( --i >= 0 ){ in apndWriteMark()
8883 iWriteEnd += paf->iPgOne; in apndWriteMark()
8884 if( SQLITE_OK==(rc = pFile->pMethods->xWrite in apndWriteMark()
8886 paf->iMark = iWriteEnd; in apndWriteMark()
8892 ** Write data to an apnd-file.
8904 /* If append-mark is absent or will be overwritten, write it. */ in apndWrite()
8905 if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){ in apndWrite()
8909 return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst); in apndWrite()
8913 ** Truncate an apnd-file.
8921 return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE); in apndTruncate()
8925 ** Sync an apnd-file.
8929 return pFile->pMethods->xSync(pFile, flags); in apndSync()
8933 ** Return the current file-size of an apnd-file.
8934 ** If the append mark is not yet there, the file-size is 0.
8938 *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0; in apndFileSize()
8943 ** Lock an apnd-file.
8947 return pFile->pMethods->xLock(pFile, eLock); in apndLock()
8951 ** Unlock an apnd-file.
8955 return pFile->pMethods->xUnlock(pFile, eLock); in apndUnlock()
8959 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
8963 return pFile->pMethods->xCheckReservedLock(pFile, pResOut); in apndCheckReservedLock()
8967 ** File control method. For custom operations on an apnd-file.
8973 if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne; in apndFileControl()
8974 rc = pFile->pMethods->xFileControl(pFile, op, pArg); in apndFileControl()
8976 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg); in apndFileControl()
8982 ** Return the sector-size in bytes for an apnd-file.
8986 return pFile->pMethods->xSectorSize(pFile); in apndSectorSize()
8990 ** Return the device characteristic flags supported by an apnd-file.
8994 return pFile->pMethods->xDeviceCharacteristics(pFile); in apndDeviceCharacteristics()
9006 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp); in apndShmMap()
9009 /* Perform locking on a shared-memory segment */
9012 return pFile->pMethods->xShmLock(pFile,offset,n,flags); in apndShmLock()
9018 pFile->pMethods->xShmBarrier(pFile); in apndShmBarrier()
9024 return pFile->pMethods->xShmUnmap(pFile,deleteFlag); in apndShmUnmap()
9027 /* Fetch a page of a memory-mapped file */
9035 if( p->iMark < 0 || iOfst+iAmt > p->iMark ){ in apndFetch()
9039 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp); in apndFetch()
9042 /* Release a memory-mapped page */
9046 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage); in apndUnfetch()
9050 ** Try to read the append-mark off the end of a file. Return the
9051 ** start of the appended database if the append-mark is present.
9052 ** If there is no valid append-mark, return -1;
9054 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
9056 ** start-of-database value must be a multiple of 512.
9061 int msbs = 8 * (APND_MARK_FOS_SZ-1); in apndReadMark()
9064 if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1; in apndReadMark()
9065 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE); in apndReadMark()
9066 if( rc ) return -1; in apndReadMark()
9067 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1; in apndReadMark()
9070 msbs -= 8; in apndReadMark()
9073 if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1; in apndReadMark()
9074 if( iMark & 0x1ff ) return -1; in apndReadMark()
9088 /* If file has the correct end-marker, the expected odd size, and the in apndIsAppendvfsDatabase()
9089 ** SQLite DB type marker where the end-marker puts it, then it in apndIsAppendvfsDatabase()
9092 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark); in apndIsAppendvfsDatabase()
9112 || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0) in apndIsOrdinaryDatabaseFile()
9141 return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags); in apndOpen()
9144 pFile->pMethods = &apnd_io_methods; in apndOpen()
9145 pApndFile->iMark = -1; /* Append mark not yet written */ in apndOpen()
9147 rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags); in apndOpen()
9149 rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz); in apndOpen()
9151 pBaseFile->pMethods->xClose(pBaseFile); in apndOpen()
9155 pFile->pMethods = 0; in apndOpen()
9160 ** the base dispatch-table so this instance mimics the base VFS. in apndOpen()
9162 memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile); in apndOpen()
9165 pApndFile->iPgOne = apndReadMark(sz, pFile); in apndOpen()
9166 if( pApndFile->iPgOne>=0 ){ in apndOpen()
9167 pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */ in apndOpen()
9171 pBaseFile->pMethods->xClose(pBaseFile); in apndOpen()
9173 pFile->pMethods = 0; in apndOpen()
9178 ** Until then, paf->iMark value indicates it is not yet written. in apndOpen()
9180 pApndFile->iPgOne = APND_START_ROUNDUP(sz); in apndOpen()
9192 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync); in apndDelete()
9196 ** All other VFS methods are pass-thrus.
9204 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut); in apndAccess()
9212 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut); in apndFullPathname()
9215 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); in apndDlOpen()
9218 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); in apndDlError()
9221 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); in apndDlSym()
9224 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); in apndDlClose()
9227 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); in apndRandomness()
9230 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); in apndSleep()
9233 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); in apndCurrentTime()
9236 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); in apndGetLastError()
9239 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); in apndCurrentTimeInt64()
9246 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall); in apndSetSystemCall()
9252 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName); in apndGetSystemCall()
9255 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName); in apndNextSystemCall()
9278 apnd_vfs.iVersion = pOrig->iVersion; in sqlite3_appendvfs_init()
9280 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile); in sqlite3_appendvfs_init()
9296 ** 2017-12-26
9350 /* typedef UINT32_TYPE u32; // 4-byte unsigned integer // */
9351 /* typedef UINT16_TYPE u16; // 2-byte unsigned integer // */
9409 ** Use this value for the "version-made-by" field in new zip file
9412 ** This is what info-zip seems to do.
9415 ** Value for "version-required-to-extract" field of new entries.
9419 ** Value for "general-purpose-bit-flags" field of new entries. Bit
9420 ** 11 means "utf-8 filename and comment".
9440 ** The sizes of the fixed-size part of each of the three main data
9487 *** crc-32 4 bytes
9528 *** crc-32 4 bytes
9556 ZipfileEntry *pNext; /* Next element in in-memory CDS */
9564 sqlite3_vtab_cursor base; /* Base class - must be first */
9567 u8 bNoop; /* If next xNext() call is no-op */
9581 sqlite3_vtab base; /* Base class - must be first */
9606 sqlite3_result_error(ctx, zMsg, -1); in zipfileCtxErrorMsg()
9633 ** argv[0] -> module name ("zipfile")
9634 ** argv[1] -> database name
9635 ** argv[2] -> table name
9636 ** argv[...] -> "column name" and other module argument fields.
9677 pNew->db = db; in zipfileConnect()
9678 pNew->aBuffer = (u8*)&pNew[1]; in zipfileConnect()
9680 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE]; in zipfileConnect()
9681 memcpy(pNew->zFile, zFile, nFile); in zipfileConnect()
9682 zipfileDequote(pNew->zFile); in zipfileConnect()
9695 sqlite3_free(p->cds.zFile); in zipfileEntryFree()
9708 if( pTab->pWriteFd ){ in zipfileCleanupTransaction()
9709 fclose(pTab->pWriteFd); in zipfileCleanupTransaction()
9710 pTab->pWriteFd = 0; in zipfileCleanupTransaction()
9712 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){ in zipfileCleanupTransaction()
9713 pNext = pEntry->pNext; in zipfileCleanupTransaction()
9716 pTab->pFirstEntry = 0; in zipfileCleanupTransaction()
9717 pTab->pLastEntry = 0; in zipfileCleanupTransaction()
9718 pTab->szCurrent = 0; in zipfileCleanupTransaction()
9719 pTab->szOrig = 0; in zipfileCleanupTransaction()
9743 pCsr->iId = ++pTab->iNextCsrid; in zipfileOpen()
9744 pCsr->pCsrNext = pTab->pCsrList; in zipfileOpen()
9745 pTab->pCsrList = pCsr; in zipfileOpen()
9757 pCsr->bEof = 0; in zipfileResetCursor()
9758 if( pCsr->pFile ){ in zipfileResetCursor()
9759 fclose(pCsr->pFile); in zipfileResetCursor()
9760 pCsr->pFile = 0; in zipfileResetCursor()
9761 zipfileEntryFree(pCsr->pCurrent); in zipfileResetCursor()
9762 pCsr->pCurrent = 0; in zipfileResetCursor()
9765 for(p=pCsr->pFreeEntry; p; p=pNext){ in zipfileResetCursor()
9766 pNext = p->pNext; in zipfileResetCursor()
9776 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab); in zipfileClose()
9781 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext)); in zipfileClose()
9782 *pp = pCsr->pCsrNext; in zipfileClose()
9795 sqlite3_free(pTab->base.zErrMsg); in zipfileTableErr()
9796 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap); in zipfileTableErr()
9802 sqlite3_free(pCsr->base.pVtab->zErrMsg); in zipfileCursorErr()
9803 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); in zipfileCursorErr()
9841 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET); in zipfileAppendData()
9842 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd); in zipfileAppendData()
9844 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()"); in zipfileAppendData()
9847 pTab->szCurrent += nWrite; in zipfileAppendData()
9853 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
9860 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
9871 ** Write a 16-bit little endiate integer into buffer aBuf.
9879 ** Write a 32-bit little endiate integer into buffer aBuf.
9888 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
9889 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
9902 ** if the record is not well-formed, or SQLITE_OK otherwise.
9911 pCDS->iVersionMadeBy = zipfileRead16(aRead); in zipfileReadCDS()
9912 pCDS->iVersionExtract = zipfileRead16(aRead); in zipfileReadCDS()
9913 pCDS->flags = zipfileRead16(aRead); in zipfileReadCDS()
9914 pCDS->iCompression = zipfileRead16(aRead); in zipfileReadCDS()
9915 pCDS->mTime = zipfileRead16(aRead); in zipfileReadCDS()
9916 pCDS->mDate = zipfileRead16(aRead); in zipfileReadCDS()
9917 pCDS->crc32 = zipfileRead32(aRead); in zipfileReadCDS()
9918 pCDS->szCompressed = zipfileRead32(aRead); in zipfileReadCDS()
9919 pCDS->szUncompressed = zipfileRead32(aRead); in zipfileReadCDS()
9921 pCDS->nFile = zipfileRead16(aRead); in zipfileReadCDS()
9922 pCDS->nExtra = zipfileRead16(aRead); in zipfileReadCDS()
9923 pCDS->nComment = zipfileRead16(aRead); in zipfileReadCDS()
9924 pCDS->iDiskStart = zipfileRead16(aRead); in zipfileReadCDS()
9925 pCDS->iInternalAttr = zipfileRead16(aRead); in zipfileReadCDS()
9926 pCDS->iExternalAttr = zipfileRead32(aRead); in zipfileReadCDS()
9927 pCDS->iOffset = zipfileRead32(aRead); in zipfileReadCDS()
9936 ** if the record is not well-formed, or SQLITE_OK otherwise.
9949 pLFH->iVersionExtract = zipfileRead16(aRead); in zipfileReadLFH()
9950 pLFH->flags = zipfileRead16(aRead); in zipfileReadLFH()
9951 pLFH->iCompression = zipfileRead16(aRead); in zipfileReadLFH()
9952 pLFH->mTime = zipfileRead16(aRead); in zipfileReadLFH()
9953 pLFH->mDate = zipfileRead16(aRead); in zipfileReadLFH()
9954 pLFH->crc32 = zipfileRead32(aRead); in zipfileReadLFH()
9955 pLFH->szCompressed = zipfileRead32(aRead); in zipfileReadLFH()
9956 pLFH->szUncompressed = zipfileRead32(aRead); in zipfileReadLFH()
9957 pLFH->nFile = zipfileRead16(aRead); in zipfileReadLFH()
9958 pLFH->nExtra = zipfileRead16(aRead); in zipfileReadLFH()
9966 ** Scan through this buffer to find an "extra-timestamp" field. If one
9967 ** exists, extract the 32-bit modification-timestamp from it and store
9970 ** Zero is returned if no extra-timestamp record could be found (and so
9971 ** *pmTime is left unchanged), or non-zero otherwise.
9991 if( b & 0x01 ){ /* 0x01 -> modtime is present */ in zipfileScanExtra()
10005 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
10006 ** fields of the CDS structure passed as the only argument to a 32-bit
10007 ** UNIX seconds-since-the-epoch timestamp. Return the result.
10009 ** "Standard" MS-DOS time format:
10012 ** Bits 00-04: seconds divided by 2
10013 ** Bits 05-10: minute
10014 ** Bits 11-15: hour
10016 ** Bits 00-04: day
10017 ** Bits 05-08: month (1-12)
10018 ** Bits 09-15: years from 1980
10020 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
10025 Y = (1980 + ((pCDS->mDate >> 9) & 0x7F)); in zipfileMtime()
10026 M = ((pCDS->mDate >> 5) & 0x0F); in zipfileMtime()
10027 D = (pCDS->mDate & 0x1F); in zipfileMtime()
10028 sec = (pCDS->mTime & 0x1F)*2; in zipfileMtime()
10029 min = (pCDS->mTime >> 5) & 0x3F; in zipfileMtime()
10030 hr = (pCDS->mTime >> 11) & 0x1F; in zipfileMtime()
10032 Y--; in zipfileMtime()
10038 B = 2 - A + (A/4); in zipfileMtime()
10039 JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec; in zipfileMtime()
10040 return (u32)(JDsec - (i64)24405875*(i64)8640); in zipfileMtime()
10056 A = (int)((JD - 1867216.25)/36524.25); in zipfileMtimeToDos()
10057 A = (int)(JD + 1 + A - (A/4)); in zipfileMtimeToDos()
10059 C = (int)((B - 122.1)/365.25); in zipfileMtimeToDos()
10061 E = (int)((B-D)/30.6001); in zipfileMtimeToDos()
10063 day = B - D - (int)(30.6001*E); in zipfileMtimeToDos()
10064 mon = (E<14 ? E-1 : E-13); in zipfileMtimeToDos()
10065 yr = mon>2 ? C-4716 : C-4715; in zipfileMtimeToDos()
10072 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9)); in zipfileMtimeToDos()
10073 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11)); in zipfileMtimeToDos()
10075 pCds->mDate = pCds->mTime = 0; in zipfileMtimeToDos()
10080 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) in zipfileMtimeToDos()
10086 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
10088 ** then pFile is a file-handle open on a zip file. In either case, this
10098 const u8 *aBlob, /* Pointer to in-memory file image */ in zipfileGetEntry()
10102 ZipfileEntry **ppEntry /* OUT: Pointer to new object */ in zipfileGetEntry()
10105 char **pzErr = &pTab->base.zErrMsg; in zipfileGetEntry()
10110 aRead = pTab->aBuffer; in zipfileGetEntry()
10134 rc = zipfileReadCDS(aRead, &pNew->cds); in zipfileGetEntry()
10147 u32 *pt = &pNew->mUnixTime; in zipfileGetEntry()
10148 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); in zipfileGetEntry()
10149 pNew->aExtra = (u8*)&pNew[1]; in zipfileGetEntry()
10150 memcpy(pNew->aExtra, &aRead[nFile], nExtra); in zipfileGetEntry()
10151 if( pNew->cds.zFile==0 ){ in zipfileGetEntry()
10153 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){ in zipfileGetEntry()
10154 pNew->mUnixTime = zipfileMtime(&pNew->cds); in zipfileGetEntry()
10162 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr); in zipfileGetEntry()
10164 aRead = (u8*)&aBlob[pNew->cds.iOffset]; in zipfileGetEntry()
10169 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ; in zipfileGetEntry()
10170 pNew->iDataOff += lfh.nFile + lfh.nExtra; in zipfileGetEntry()
10171 if( aBlob && pNew->cds.szCompressed ){ in zipfileGetEntry()
10172 pNew->aData = &pNew->aExtra[nExtra]; in zipfileGetEntry()
10173 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed); in zipfileGetEntry()
10177 (int)pNew->cds.iOffset in zipfileGetEntry()
10199 if( pCsr->pFile ){ in zipfileNext()
10200 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize; in zipfileNext()
10201 zipfileEntryFree(pCsr->pCurrent); in zipfileNext()
10202 pCsr->pCurrent = 0; in zipfileNext()
10203 if( pCsr->iNextOff>=iEof ){ in zipfileNext()
10204 pCsr->bEof = 1; in zipfileNext()
10207 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab); in zipfileNext()
10208 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p); in zipfileNext()
10210 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ; in zipfileNext()
10211 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment; in zipfileNext()
10213 pCsr->pCurrent = p; in zipfileNext()
10216 if( !pCsr->bNoop ){ in zipfileNext()
10217 pCsr->pCurrent = pCsr->pCurrent->pNext; in zipfileNext()
10219 if( pCsr->pCurrent==0 ){ in zipfileNext()
10220 pCsr->bEof = 1; in zipfileNext()
10224 pCsr->bNoop = 0; in zipfileNext()
10258 err = inflateInit2(&str, -15); in zipfileInflate()
10283 ** code is returned and an error message left in virtual-table handle
10300 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); in zipfileDeflate()
10336 ZipfileCDS *pCDS = &pCsr->pCurrent->cds; in zipfileColumn()
10340 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT); in zipfileColumn()
10345 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16); in zipfileColumn()
10348 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime); in zipfileColumn()
10353 sqlite3_result_int64(ctx, pCDS->szUncompressed); in zipfileColumn()
10360 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){ in zipfileColumn()
10361 int sz = pCDS->szCompressed; in zipfileColumn()
10362 int szFinal = pCDS->szUncompressed; in zipfileColumn()
10366 if( pCsr->pCurrent->aData ){ in zipfileColumn()
10367 aBuf = pCsr->pCurrent->aData; in zipfileColumn()
10373 FILE *pFile = pCsr->pFile; in zipfileColumn()
10375 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd; in zipfileColumn()
10377 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff, in zipfileColumn()
10378 &pCsr->base.pVtab->zErrMsg in zipfileColumn()
10383 if( i==5 && pCDS->iCompression ){ in zipfileColumn()
10391 /* Figure out if this is a directory or a zero-sized file. Consider in zipfileColumn()
10394 u32 mode = pCDS->iExternalAttr >> 16; in zipfileColumn()
10396 && pCDS->nFile>=1 in zipfileColumn()
10397 && pCDS->zFile[pCDS->nFile-1]!='/' in zipfileColumn()
10406 sqlite3_result_int(ctx, pCDS->iCompression); in zipfileColumn()
10410 sqlite3_result_int64(ctx, pCsr->iId); in zipfileColumn()
10422 return pCsr->bEof; in zipfileEof()
10428 ** is guaranteed to be a file-handle open on a zip file.
10433 ** an English language error message may be left in virtual-table pTab.
10437 const u8 *aBlob, /* Pointer to in-memory file image */ in zipfileReadEOCD()
10442 u8 *aRead = pTab->aBuffer; /* Temporary buffer */ in zipfileReadEOCD()
10456 iOff = szFile - nRead; in zipfileReadEOCD()
10457 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg); in zipfileReadEOCD()
10460 aRead = (u8*)&aBlob[nBlob-nRead]; in zipfileReadEOCD()
10467 for(i=nRead-20; i>=0; i--){ in zipfileReadEOCD()
10475 pTab->base.zErrMsg = sqlite3_mprintf( in zipfileReadEOCD()
10482 pEOCD->iDisk = zipfileRead16(aRead); in zipfileReadEOCD()
10483 pEOCD->iFirstDisk = zipfileRead16(aRead); in zipfileReadEOCD()
10484 pEOCD->nEntry = zipfileRead16(aRead); in zipfileReadEOCD()
10485 pEOCD->nEntryTotal = zipfileRead16(aRead); in zipfileReadEOCD()
10486 pEOCD->nSize = zipfileRead32(aRead); in zipfileReadEOCD()
10487 pEOCD->iOffset = zipfileRead32(aRead); in zipfileReadEOCD()
10504 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) ); in zipfileAddEntry()
10505 assert( pNew->pNext==0 ); in zipfileAddEntry()
10507 if( pTab->pFirstEntry==0 ){ in zipfileAddEntry()
10508 pTab->pFirstEntry = pTab->pLastEntry = pNew; in zipfileAddEntry()
10510 assert( pTab->pLastEntry->pNext==0 ); in zipfileAddEntry()
10511 pTab->pLastEntry->pNext = pNew; in zipfileAddEntry()
10512 pTab->pLastEntry = pNew; in zipfileAddEntry()
10516 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext)); in zipfileAddEntry()
10517 pNew->pNext = pBefore; in zipfileAddEntry()
10528 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd); in zipfileLoadDirectory()
10532 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew); in zipfileLoadDirectory()
10537 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment; in zipfileLoadDirectory()
10551 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab; in zipfileFilter()
10555 int bInMemory = 0; /* True for an in-memory zipfile */ in zipfileFilter()
10562 if( pTab->zFile ){ in zipfileFilter()
10563 zFile = pTab->zFile; in zipfileFilter()
10571 assert( pTab->pFirstEntry==0 ); in zipfileFilter()
10577 pCsr->pFreeEntry = pTab->pFirstEntry; in zipfileFilter()
10578 pTab->pFirstEntry = pTab->pLastEntry = 0; in zipfileFilter()
10585 if( 0==pTab->pWriteFd && 0==bInMemory ){ in zipfileFilter()
10586 pCsr->pFile = zFile ? fopen(zFile, "rb") : 0; in zipfileFilter()
10587 if( pCsr->pFile==0 ){ in zipfileFilter()
10591 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd); in zipfileFilter()
10593 if( pCsr->eocd.nEntry==0 ){ in zipfileFilter()
10594 pCsr->bEof = 1; in zipfileFilter()
10596 pCsr->iNextOff = pCsr->eocd.iOffset; in zipfileFilter()
10602 pCsr->bNoop = 1; in zipfileFilter()
10603 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry; in zipfileFilter()
10618 int idx = -1; in zipfileBestIndex()
10622 for(i=0; i<pIdxInfo->nConstraint; i++){ in zipfileBestIndex()
10623 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i]; in zipfileBestIndex()
10624 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue; in zipfileBestIndex()
10625 if( pCons->usable==0 ){ in zipfileBestIndex()
10627 }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in zipfileBestIndex()
10631 pIdxInfo->estimatedCost = 1000.0; in zipfileBestIndex()
10633 pIdxInfo->aConstraintUsage[idx].argvIndex = 1; in zipfileBestIndex()
10634 pIdxInfo->aConstraintUsage[idx].omit = 1; in zipfileBestIndex()
10635 pIdxInfo->idxNum = 1; in zipfileBestIndex()
10647 pNew->cds.zFile = sqlite3_mprintf("%s", zPath); in zipfileNewEntry()
10648 if( pNew->cds.zFile==0 ){ in zipfileNewEntry()
10657 ZipfileCDS *pCds = &pEntry->cds; in zipfileSerializeLFH()
10660 pCds->nExtra = 9; in zipfileSerializeLFH()
10664 zipfileWrite16(a, pCds->iVersionExtract); in zipfileSerializeLFH()
10665 zipfileWrite16(a, pCds->flags); in zipfileSerializeLFH()
10666 zipfileWrite16(a, pCds->iCompression); in zipfileSerializeLFH()
10667 zipfileWrite16(a, pCds->mTime); in zipfileSerializeLFH()
10668 zipfileWrite16(a, pCds->mDate); in zipfileSerializeLFH()
10669 zipfileWrite32(a, pCds->crc32); in zipfileSerializeLFH()
10670 zipfileWrite32(a, pCds->szCompressed); in zipfileSerializeLFH()
10671 zipfileWrite32(a, pCds->szUncompressed); in zipfileSerializeLFH()
10672 zipfileWrite16(a, (u16)pCds->nFile); in zipfileSerializeLFH()
10673 zipfileWrite16(a, pCds->nExtra); in zipfileSerializeLFH()
10677 memcpy(a, pCds->zFile, (int)pCds->nFile); in zipfileSerializeLFH()
10678 a += (int)pCds->nFile; in zipfileSerializeLFH()
10684 zipfileWrite32(a, pEntry->mUnixTime); in zipfileSerializeLFH()
10686 return a-aBuf; in zipfileSerializeLFH()
10695 u8 *aBuf = pTab->aBuffer; in zipfileAppendEntry()
10702 pEntry->iDataOff = pTab->szCurrent; in zipfileAppendEntry()
10722 const char zTemplate[11] = "-rwxrwxrwx"; in zipfileGetMode()
10726 case '-': mode |= S_IFREG; break; in zipfileGetMode()
10732 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i); in zipfileGetMode()
10733 else if( z[i]!='-' ) goto parse_error; in zipfileGetMode()
10738 ** Or vice-versa - no data but "mode" is a file or symlink. */ in zipfileGetMode()
10751 ** Both (const char*) arguments point to nul-terminated strings. Argument
10756 if( nA>0 && zA[nA-1]=='/' ) nA--; in zipfileComparePath()
10757 if( nB>0 && zB[nB-1]=='/' ) nB--; in zipfileComparePath()
10766 assert( pTab->pWriteFd==0 ); in zipfileBegin()
10767 if( pTab->zFile==0 || pTab->zFile[0]==0 ){ in zipfileBegin()
10768 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename"); in zipfileBegin()
10775 ** in main-memory until the transaction is committed. */ in zipfileBegin()
10776 pTab->pWriteFd = fopen(pTab->zFile, "ab+"); in zipfileBegin()
10777 if( pTab->pWriteFd==0 ){ in zipfileBegin()
10778 pTab->base.zErrMsg = sqlite3_mprintf( in zipfileBegin()
10779 "zipfile: failed to open file %s for writing", pTab->zFile in zipfileBegin()
10783 fseek(pTab->pWriteFd, 0, SEEK_END); in zipfileBegin()
10784 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd); in zipfileBegin()
10796 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
10803 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ in zipfileTime()
10805 pVfs->xCurrentTimeInt64(pVfs, &ms); in zipfileTime()
10806 ret = (u32)((ms/1000) - ((i64)24405875 * 8640)); in zipfileTime()
10809 pVfs->xCurrentTime(pVfs, &day); in zipfileTime()
10810 ret = (u32)((day - 2440587.5) * 86400); in zipfileTime()
10816 ** Return a 32-bit timestamp in UNIX epoch format.
10820 ** cast to a 32-bit unsigned integer.
10830 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
10835 if( pTab->pFirstEntry==pOld ){ in zipfileRemoveEntryFromList()
10836 pTab->pFirstEntry = pOld->pNext; in zipfileRemoveEntryFromList()
10837 if( pTab->pLastEntry==pOld ) pTab->pLastEntry = 0; in zipfileRemoveEntryFromList()
10840 for(p=pTab->pFirstEntry; p; p=p->pNext){ in zipfileRemoveEntryFromList()
10841 if( p->pNext==pOld ){ in zipfileRemoveEntryFromList()
10842 p->pNext = pOld->pNext; in zipfileRemoveEntryFromList()
10843 if( pTab->pLastEntry==pOld ) pTab->pLastEntry = p; in zipfileRemoveEntryFromList()
10863 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */ in zipfileUpdate()
10870 const u8 *pData = 0; /* Pointer to buffer containing content */ in zipfileUpdate()
10883 if( pTab->pWriteFd==0 ){ in zipfileUpdate()
10898 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){ in zipfileUpdate()
10899 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){ in zipfileUpdate()
10902 assert( pOld->pNext ); in zipfileUpdate()
10938 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg); in zipfileUpdate()
10953 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg); in zipfileUpdate()
10965 ** '/'. This appears to be required for compatibility with info-zip in zipfileUpdate()
10968 if( nPath<=0 || zPath[nPath-1]!='/' ){ in zipfileUpdate()
10980 /* Check that we're not inserting a duplicate entry -OR- updating an in zipfileUpdate()
10984 for(p=pTab->pFirstEntry; p; p=p->pNext){ in zipfileUpdate()
10985 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){ in zipfileUpdate()
10986 switch( sqlite3_vtab_on_conflict(pTab->db) ){ in zipfileUpdate()
11011 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY; in zipfileUpdate()
11012 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED; in zipfileUpdate()
11013 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS; in zipfileUpdate()
11014 pNew->cds.iCompression = (u16)iMethod; in zipfileUpdate()
11015 zipfileMtimeToDos(&pNew->cds, mTime); in zipfileUpdate()
11016 pNew->cds.crc32 = iCrc32; in zipfileUpdate()
11017 pNew->cds.szCompressed = nData; in zipfileUpdate()
11018 pNew->cds.szUncompressed = (u32)sz; in zipfileUpdate()
11019 pNew->cds.iExternalAttr = (mode<<16); in zipfileUpdate()
11020 pNew->cds.iOffset = (u32)pTab->szCurrent; in zipfileUpdate()
11021 pNew->cds.nFile = (u16)nPath; in zipfileUpdate()
11022 pNew->mUnixTime = (u32)mTime; in zipfileUpdate()
11031 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){ in zipfileUpdate()
11032 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){ in zipfileUpdate()
11033 pCsr->pCurrent = pCsr->pCurrent->pNext; in zipfileUpdate()
11034 pCsr->bNoop = 1; in zipfileUpdate()
11051 zipfileWrite16(a, p->iDisk); in zipfileSerializeEOCD()
11052 zipfileWrite16(a, p->iFirstDisk); in zipfileSerializeEOCD()
11053 zipfileWrite16(a, p->nEntry); in zipfileSerializeEOCD()
11054 zipfileWrite16(a, p->nEntryTotal); in zipfileSerializeEOCD()
11055 zipfileWrite32(a, p->nSize); in zipfileSerializeEOCD()
11056 zipfileWrite32(a, p->iOffset); in zipfileSerializeEOCD()
11059 return a-aBuf; in zipfileSerializeEOCD()
11063 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer); in zipfileAppendEOCD()
11065 return zipfileAppendData(pTab, pTab->aBuffer, nBuf); in zipfileAppendEOCD()
11074 ZipfileCDS *pCDS = &pEntry->cds; in zipfileSerializeCDS()
11076 if( pEntry->aExtra==0 ){ in zipfileSerializeCDS()
11077 pCDS->nExtra = 9; in zipfileSerializeCDS()
11081 zipfileWrite16(a, pCDS->iVersionMadeBy); in zipfileSerializeCDS()
11082 zipfileWrite16(a, pCDS->iVersionExtract); in zipfileSerializeCDS()
11083 zipfileWrite16(a, pCDS->flags); in zipfileSerializeCDS()
11084 zipfileWrite16(a, pCDS->iCompression); in zipfileSerializeCDS()
11085 zipfileWrite16(a, pCDS->mTime); in zipfileSerializeCDS()
11086 zipfileWrite16(a, pCDS->mDate); in zipfileSerializeCDS()
11087 zipfileWrite32(a, pCDS->crc32); in zipfileSerializeCDS()
11088 zipfileWrite32(a, pCDS->szCompressed); in zipfileSerializeCDS()
11089 zipfileWrite32(a, pCDS->szUncompressed); in zipfileSerializeCDS()
11091 zipfileWrite16(a, pCDS->nFile); in zipfileSerializeCDS()
11092 zipfileWrite16(a, pCDS->nExtra); in zipfileSerializeCDS()
11093 zipfileWrite16(a, pCDS->nComment); in zipfileSerializeCDS()
11094 zipfileWrite16(a, pCDS->iDiskStart); in zipfileSerializeCDS()
11095 zipfileWrite16(a, pCDS->iInternalAttr); in zipfileSerializeCDS()
11096 zipfileWrite32(a, pCDS->iExternalAttr); in zipfileSerializeCDS()
11097 zipfileWrite32(a, pCDS->iOffset); in zipfileSerializeCDS()
11099 memcpy(a, pCDS->zFile, pCDS->nFile); in zipfileSerializeCDS()
11100 a += pCDS->nFile; in zipfileSerializeCDS()
11102 if( pEntry->aExtra ){ in zipfileSerializeCDS()
11103 int n = (int)pCDS->nExtra + (int)pCDS->nComment; in zipfileSerializeCDS()
11104 memcpy(a, pEntry->aExtra, n); in zipfileSerializeCDS()
11107 assert( pCDS->nExtra==9 ); in zipfileSerializeCDS()
11111 zipfileWrite32(a, pEntry->mUnixTime); in zipfileSerializeCDS()
11114 return a-aBuf; in zipfileSerializeCDS()
11120 if( pTab->pWriteFd ){ in zipfileCommit()
11121 i64 iOffset = pTab->szCurrent; in zipfileCommit()
11127 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){ in zipfileCommit()
11128 int n = zipfileSerializeCDS(p, pTab->aBuffer); in zipfileCommit()
11129 rc = zipfileAppendData(pTab, pTab->aBuffer, n); in zipfileCommit()
11138 eocd.nSize = (u32)(pTab->szCurrent - iOffset); in zipfileCommit()
11153 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){ in zipfileFindCursor()
11154 if( iId==pCsr->iId ) break; in zipfileFindCursor()
11170 ZipfileCDS *p = &pCsr->pCurrent->cds; in zipfileFunctionCds()
11172 "\"version-made-by\" : %u, " in zipfileFunctionCds()
11173 "\"version-to-extract\" : %u, " in zipfileFunctionCds()
11179 "\"compressed-size\" : %u, " in zipfileFunctionCds()
11180 "\"uncompressed-size\" : %u, " in zipfileFunctionCds()
11181 "\"file-name-length\" : %u, " in zipfileFunctionCds()
11182 "\"extra-field-length\" : %u, " in zipfileFunctionCds()
11183 "\"file-comment-length\" : %u, " in zipfileFunctionCds()
11184 "\"disk-number-start\" : %u, " in zipfileFunctionCds()
11185 "\"internal-attr\" : %u, " in zipfileFunctionCds()
11186 "\"external-attr\" : %u, " in zipfileFunctionCds()
11188 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract, in zipfileFunctionCds()
11189 (u32)p->flags, (u32)p->iCompression, in zipfileFunctionCds()
11190 (u32)p->mTime, (u32)p->mDate, in zipfileFunctionCds()
11191 (u32)p->crc32, (u32)p->szCompressed, in zipfileFunctionCds()
11192 (u32)p->szUncompressed, (u32)p->nFile, in zipfileFunctionCds()
11193 (u32)p->nExtra, (u32)p->nComment, in zipfileFunctionCds()
11194 (u32)p->iDiskStart, (u32)p->iInternalAttr, in zipfileFunctionCds()
11195 (u32)p->iExternalAttr, (u32)p->iOffset in zipfileFunctionCds()
11201 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT); in zipfileFunctionCds()
11228 u8 *a; /* Pointer to buffer */
11241 if( pBuf->n+nByte>pBuf->nAlloc ){ in zipfileBufferGrow()
11243 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512; in zipfileBufferGrow()
11244 int nReq = pBuf->n + nByte; in zipfileBufferGrow()
11247 aNew = sqlite3_realloc64(pBuf->a, nNew); in zipfileBufferGrow()
11249 pBuf->a = aNew; in zipfileBufferGrow()
11250 pBuf->nAlloc = (int)nNew; in zipfileBufferGrow()
11278 int iMethod = -1; /* Compression method to use (0 or 8) */ in zipfileStep()
11317 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL"); in zipfileStep()
11370 if( nName>0 && zName[nName-1]=='/' ){ in zipfileStep()
11371 zErr = sqlite3_mprintf("non-directory name must not end with /"); in zipfileStep()
11376 if( nName==0 || zName[nName-1]!='/' ){ in zipfileStep()
11384 while( nName>1 && zName[nName-2]=='/' ) nName--; in zipfileStep()
11398 e.cds.iOffset = p->body.n; in zipfileStep()
11404 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out; in zipfileStep()
11405 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]); in zipfileStep()
11409 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out; in zipfileStep()
11410 memcpy(&p->body.a[p->body.n], aData, nData); in zipfileStep()
11411 p->body.n += nData; in zipfileStep()
11416 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out; in zipfileStep()
11417 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]); in zipfileStep()
11420 p->nEntry++; in zipfileStep()
11427 sqlite3_result_error(pCtx, zErr, -1); in zipfileStep()
11446 if( p->nEntry>0 ){ in zipfileFinal()
11448 eocd.nEntry = (u16)p->nEntry; in zipfileFinal()
11449 eocd.nEntryTotal = (u16)p->nEntry; in zipfileFinal()
11450 eocd.nSize = p->cds.n; in zipfileFinal()
11451 eocd.iOffset = p->body.n; in zipfileFinal()
11453 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ; in zipfileFinal()
11458 memcpy(aZip, p->body.a, p->body.n); in zipfileFinal()
11459 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n); in zipfileFinal()
11460 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]); in zipfileFinal()
11465 sqlite3_free(p->body.a); in zipfileFinal()
11466 sqlite3_free(p->cds.a); in zipfileFinal()
11481 zipfileOpen, /* xOpen - open a cursor */ in zipfileRegister()
11482 zipfileClose, /* xClose - close a cursor */ in zipfileRegister()
11483 zipfileFilter, /* xFilter - configure scan constraints */ in zipfileRegister()
11484 zipfileNext, /* xNext - advance a cursor */ in zipfileRegister()
11485 zipfileEof, /* xEof - check for end of scan */ in zipfileRegister()
11486 zipfileColumn, /* xColumn - read data */ in zipfileRegister()
11487 0, /* xRowid - read data */ in zipfileRegister()
11503 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1); in zipfileRegister()
11505 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, in zipfileRegister()
11535 ** 2017-12-17
11547 ** for working with sqlar archives and used by the shell tool's built-in
11563 ** contains a two-byte identification header and a four-byte checksum at
11588 sqlite3_result_error(context, "error in compress()", -1); in sqlarCompressFunc()
11630 sqlite3_result_error(context, "error in uncompress()", -1); in sqlarUncompressFunc()
11684 ** If successful, a pointer to the new object is returned and (*pzErr) set
11686 ** an English-language error message. In this case it is the responsibility
11706 ** the analysis - indexes are recommended based on the database schema only.
11730 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
11731 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
11766 ** possible to add further SQL statements to the object or to re-attempt
11784 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
11792 ** If an out-of-range value (less than zero or equal to or greater than the
11809 ** Return a pointer to a buffer containing the CREATE INDEX statements
11826 ** sqlite3-expert_new().
11891 ** any other type of single-ended range constraint on a column).
11937 ** An object of the following type is created for each unique table/write-op
11938 ** seen. The objects are stored in a singly-linked list beginning at
11973 char *zKey; /* nul-terminated key */
11974 char *zVal; /* nul-terminated value string */
11975 char *zVal2; /* nul-terminated value string 2 */
11990 sqlite3 *dbm; /* In-memory db for this analysis */
12036 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){ in idxHashClear()
12037 pNext = pEntry->pHashNext; in idxHashClear()
12038 sqlite3_free(pEntry->zVal2); in idxHashClear()
12059 ** If zKey is already present in the hash table, return non-zero and do
12074 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ in idxHashAdd()
12075 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ in idxHashAdd()
12081 pEntry->zKey = (char*)&pEntry[1]; in idxHashAdd()
12082 memcpy(pEntry->zKey, zKey, nKey); in idxHashAdd()
12084 pEntry->zVal = &pEntry->zKey[nKey+1]; in idxHashAdd()
12085 memcpy(pEntry->zVal, zVal, nVal); in idxHashAdd()
12087 pEntry->pHashNext = pHash->aHash[iHash]; in idxHashAdd()
12088 pHash->aHash[iHash] = pEntry; in idxHashAdd()
12090 pEntry->pNext = pHash->pFirst; in idxHashAdd()
12091 pHash->pFirst = pEntry; in idxHashAdd()
12097 ** If zKey/nKey is present in the hash table, return a pointer to the
12098 ** hash-entry object.
12106 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ in idxHashFind()
12107 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ in idxHashFind()
12116 ** passed as the final two arguments to this function, return a pointer
12122 if( pEntry ) return pEntry->zVal; in idxHashSearch()
12128 ** variable to point to a copy of nul-terminated string zColl.
12137 pNew->zColl = (char*)&pNew[1]; in idxNewConstraint()
12138 memcpy(pNew->zColl, zColl, nColl+1); in idxNewConstraint()
12163 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); in idxPrepareStmt()
12218 assert( zIn[n-1]=='\'' ); in expertDequote()
12223 for(iIn=1; iIn<(n-1); iIn++){ in expertDequote()
12238 ** methods of the r-tree virtual table.
12240 ** argv[0] -> module name
12241 ** argv[1] -> database name
12242 ** argv[2] -> table name
12243 ** argv[...] -> column names...
12267 p->pExpert = pExpert; in expertConnect()
12268 p->pTab = pExpert->pTable; in expertConnect()
12269 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 ); in expertConnect()
12302 pScan->pTab = p->pTab; in expertBestIndex()
12303 pScan->pNextScan = p->pExpert->pScan; in expertBestIndex()
12304 p->pExpert->pScan = pScan; in expertBestIndex()
12307 for(i=0; i<pIdxInfo->nConstraint; i++){ in expertBestIndex()
12308 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i]; in expertBestIndex()
12309 if( pCons->usable in expertBestIndex()
12310 && pCons->iColumn>=0 in expertBestIndex()
12311 && p->pTab->aCol[pCons->iColumn].iPk==0 in expertBestIndex()
12312 && (pCons->op & opmask) in expertBestIndex()
12318 pNew->iCol = pCons->iColumn; in expertBestIndex()
12319 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in expertBestIndex()
12320 pNew->pNext = pScan->pEq; in expertBestIndex()
12321 pScan->pEq = pNew; in expertBestIndex()
12323 pNew->bRange = 1; in expertBestIndex()
12324 pNew->pNext = pScan->pRange; in expertBestIndex()
12325 pScan->pRange = pNew; in expertBestIndex()
12329 pIdxInfo->aConstraintUsage[i].argvIndex = n; in expertBestIndex()
12334 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){ in expertBestIndex()
12335 int iCol = pIdxInfo->aOrderBy[i].iColumn; in expertBestIndex()
12337 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl); in expertBestIndex()
12339 pNew->iCol = iCol; in expertBestIndex()
12340 pNew->bDesc = pIdxInfo->aOrderBy[i].desc; in expertBestIndex()
12341 pNew->pNext = pScan->pOrder; in expertBestIndex()
12342 pNew->pLink = pScan->pOrder; in expertBestIndex()
12343 pScan->pOrder = pNew; in expertBestIndex()
12350 pIdxInfo->estimatedCost = 1000000.0 / (n+1); in expertBestIndex()
12384 sqlite3_finalize(pCsr->pData); in expertClose()
12392 ** Return non-zero if the cursor does not currently point to a valid
12397 return pCsr->pData==0; in expertEof()
12407 assert( pCsr->pData ); in expertNext()
12408 rc = sqlite3_step(pCsr->pData); in expertNext()
12410 rc = sqlite3_finalize(pCsr->pData); in expertNext()
12411 pCsr->pData = 0; in expertNext()
12434 pVal = sqlite3_column_value(pCsr->pData, i); in expertColumn()
12450 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab); in expertFilter()
12451 sqlite3expert *pExpert = pVtab->pExpert; in expertFilter()
12458 rc = sqlite3_finalize(pCsr->pData); in expertFilter()
12459 pCsr->pData = 0; in expertFilter()
12461 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg, in expertFilter()
12462 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName in expertFilter()
12475 expertConnect, /* xCreate - create a table */ in idxRegisterVtab()
12476 expertConnect, /* xConnect - connect to an existing table */ in idxRegisterVtab()
12477 expertBestIndex, /* xBestIndex - Determine search strategy */ in idxRegisterVtab()
12478 expertDisconnect, /* xDisconnect - Disconnect from a table */ in idxRegisterVtab()
12479 expertDisconnect, /* xDestroy - Drop a table */ in idxRegisterVtab()
12480 expertOpen, /* xOpen - open a cursor */ in idxRegisterVtab()
12481 expertClose, /* xClose - close a cursor */ in idxRegisterVtab()
12482 expertFilter, /* xFilter - configure scan constraints */ in idxRegisterVtab()
12483 expertNext, /* xNext - advance a cursor */ in idxRegisterVtab()
12485 expertColumn, /* xColumn - read data */ in idxRegisterVtab()
12486 expertRowid, /* xRowid - read data */ in idxRegisterVtab()
12487 expertUpdate, /* xUpdate - write data */ in idxRegisterVtab()
12488 0, /* xBegin - begin transaction */ in idxRegisterVtab()
12489 0, /* xSync - sync transaction */ in idxRegisterVtab()
12490 0, /* xCommit - commit transaction */ in idxRegisterVtab()
12491 0, /* xRollback - rollback transaction */ in idxRegisterVtab()
12492 0, /* xFindFunction - function overloading */ in idxRegisterVtab()
12493 0, /* xRename - rename the table */ in idxRegisterVtab()
12501 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p); in idxRegisterVtab()
12570 pNew->aCol = (IdxColumn*)&pNew[1]; in idxGetTableInfo()
12571 pNew->nCol = nCol; in idxGetTableInfo()
12572 pCsr = (char*)&pNew->aCol[nCol]; in idxGetTableInfo()
12582 pNew->aCol[nCol].zName = pCsr; in idxGetTableInfo()
12583 pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1); in idxGetTableInfo()
12593 pNew->aCol[nCol].zColl = pCsr; in idxGetTableInfo()
12606 pNew->zName = pCsr; in idxGetTableInfo()
12607 if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1); in idxGetTableInfo()
12615 ** This function is a no-op if *pRc is set to anything other than
12684 IdxColumn *p = &pTab->aCol[pCons->iCol]; in idxAppendColDefn()
12687 if( idxIdentifierRequiresQuotes(p->zName) ){ in idxAppendColDefn()
12688 zRet = idxAppendText(pRc, zRet, "%Q", p->zName); in idxAppendColDefn()
12690 zRet = idxAppendText(pRc, zRet, "%s", p->zName); in idxAppendColDefn()
12693 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){ in idxAppendColDefn()
12694 if( idxIdentifierRequiresQuotes(pCons->zColl) ){ in idxAppendColDefn()
12695 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl); in idxAppendColDefn()
12697 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl); in idxAppendColDefn()
12701 if( pCons->bDesc ){ in idxAppendColDefn()
12710 ** such an index is found, return non-zero. Or, if no such index is found,
12722 const char *zTbl = pScan->pTab->zName; in idxFindCompatible()
12729 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++; in idxFindCompatible()
12740 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0; in idxFindCompatible()
12749 for(pIter=pEq; pIter; pIter=pIter->pLink){ in idxFindCompatible()
12750 if( pIter->bFlag ) continue; in idxFindCompatible()
12751 if( pIter->iCol!=iCol ) continue; in idxFindCompatible()
12752 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue; in idxFindCompatible()
12753 pIter->bFlag = 1; in idxFindCompatible()
12762 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){ in idxFindCompatible()
12766 pT = pT->pLink; in idxFindCompatible()
12802 sqlite3 *dbm = p->dbm; in idxCreateFromCons()
12805 IdxTable *pTab = pScan->pTab; in idxCreateFromCons()
12812 for(pCons=pEq; pCons; pCons=pCons->pLink){ in idxCreateFromCons()
12815 for(pCons=pTail; pCons; pCons=pCons->pLink){ in idxCreateFromCons()
12821 const char *zTable = pScan->pTab->zName; in idxCreateFromCons()
12863 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg); in idxCreateFromCons()
12867 idxHashAdd(&rc, &p->hIdx, zName, zIdx); in idxCreateFromCons()
12886 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){ in idxFindConstraint()
12887 if( p->iCol==pCmp->iCol ) return 1; in idxFindConstraint()
12902 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){ in idxCreateFromWhere()
12904 pCon->pLink = p1; in idxCreateFromWhere()
12916 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){ in idxCreateFromWhere()
12917 assert( pCon->pLink==0 ); in idxCreateFromWhere()
12929 ** linked-list pScan.
12935 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){ in idxCreateCandidates()
12937 if( rc==SQLITE_OK && pIter->pOrder ){ in idxCreateCandidates()
12938 rc = idxCreateFromWhere(p, pIter, pIter->pOrder); in idxCreateCandidates()
12953 pNext = p->pNext; in idxConstraintFree()
12966 pNext = p->pNextScan; in idxScanFree()
12967 idxConstraintFree(p->pOrder); in idxScanFree()
12968 idxConstraintFree(p->pEq); in idxScanFree()
12969 idxConstraintFree(p->pRange); in idxScanFree()
12982 pNext = p->pNext; in idxStatementFree()
12983 sqlite3_free(p->zEQP); in idxStatementFree()
12984 sqlite3_free(p->zIdx); in idxStatementFree()
12996 pNext = pIter->pNext; in idxTableFree()
13008 pNext = pIter->pNext; in idxWriteFree()
13025 sqlite3 *dbm = p->dbm; in idxFindIndexes()
13031 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){ in idxFindIndexes()
13036 "EXPLAIN QUERY PLAN %s", pStmt->zSql in idxFindIndexes()
13064 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx); in idxFindIndexes()
13073 if( zDetail[0]!='-' ){ in idxFindIndexes()
13074 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail); in idxFindIndexes()
13078 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ in idxFindIndexes()
13079 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey); in idxFindIndexes()
13105 for(pTab=p->pTable; pTab; pTab=pTab->pNext){ in idxAuthCallback()
13106 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break; in idxAuthCallback()
13110 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){ in idxAuthCallback()
13111 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break; in idxAuthCallback()
13116 pWrite->pTab = pTab; in idxAuthCallback()
13117 pWrite->eOp = eOp; in idxAuthCallback()
13118 pWrite->pNext = p->pWrite; in idxAuthCallback()
13119 p->pWrite = pWrite; in idxAuthCallback()
13135 IdxTable *pTab = pWrite->pTab; in idxProcessOneTrigger()
13136 const char *zTab = pTab->zName; in idxProcessOneTrigger()
13146 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab); in idxProcessOneTrigger()
13150 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr); in idxProcessOneTrigger()
13160 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr); in idxProcessOneTrigger()
13165 switch( pWrite->eOp ){ in idxProcessOneTrigger()
13169 for(i=0; i<pTab->nCol; i++){ in idxProcessOneTrigger()
13178 for(i=0; i<pTab->nCol; i++){ in idxProcessOneTrigger()
13180 pTab->aCol[i].zName in idxProcessOneTrigger()
13186 assert( pWrite->eOp==SQLITE_DELETE ); in idxProcessOneTrigger()
13196 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0); in idxProcessOneTrigger()
13199 idxDatabaseError(p->dbv, pzErr); in idxProcessOneTrigger()
13205 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr); in idxProcessOneTrigger()
13214 IdxWrite *pFirst = p->pWrite; in idxProcessTriggers()
13218 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){ in idxProcessTriggers()
13222 pFirst = p->pWrite; in idxProcessTriggers()
13235 ** 1) Add an entry to the p->pTable list, and in idxCreateVtabSchema()
13238 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg, in idxCreateVtabSchema()
13254 if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg); in idxCreateVtabSchema()
13257 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg); in idxCreateVtabSchema()
13262 pTab->pNext = p->pTable; in idxCreateVtabSchema()
13263 p->pTable = pTab; in idxCreateVtabSchema()
13267 for(i=0; i<pTab->nCol; i++){ in idxCreateVtabSchema()
13269 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl in idxCreateVtabSchema()
13279 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg); in idxCreateVtabSchema()
13307 if( p->nRow==0.0 ){ in idxSampleFunc()
13310 bRet = (p->nRet / p->nRow) <= p->target; in idxSampleFunc()
13314 bRet = ((int)rnd % 100) <= p->iTarget; in idxSampleFunc()
13319 p->nRow += 1.0; in idxSampleFunc()
13320 p->nRet += (double)bRet; in idxSampleFunc()
13349 assert( iSlot<=p->nSlot ); in idxRemFunc()
13350 pSlot = &p->aSlot[iSlot]; in idxRemFunc()
13352 switch( pSlot->eType ){ in idxRemFunc()
13354 /* no-op */ in idxRemFunc()
13358 sqlite3_result_int64(pCtx, pSlot->iVal); in idxRemFunc()
13362 sqlite3_result_double(pCtx, pSlot->rVal); in idxRemFunc()
13366 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT); in idxRemFunc()
13370 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT); in idxRemFunc()
13374 pSlot->eType = sqlite3_value_type(argv[1]); in idxRemFunc()
13375 switch( pSlot->eType ){ in idxRemFunc()
13377 /* no-op */ in idxRemFunc()
13381 pSlot->iVal = sqlite3_value_int64(argv[1]); in idxRemFunc()
13385 pSlot->rVal = sqlite3_value_double(argv[1]); in idxRemFunc()
13392 if( nByte>pSlot->nByte ){ in idxRemFunc()
13393 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2); in idxRemFunc()
13398 pSlot->nByte = nByte*2; in idxRemFunc()
13399 pSlot->z = zNew; in idxRemFunc()
13401 pSlot->n = nByte; in idxRemFunc()
13402 if( pSlot->eType==SQLITE_BLOB ){ in idxRemFunc()
13404 if( pData ) memcpy(pSlot->z, pData, nByte); in idxRemFunc()
13407 memcpy(pSlot->z, pData, nByte); in idxRemFunc()
13451 assert( p->iSample>0 ); in idxPopulateOneStat1()
13454 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC); in idxPopulateOneStat1()
13466 if( p->iSample==100 ){ in idxPopulateOneStat1()
13481 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); in idxPopulateOneStat1()
13498 for(/*no-op*/; i<nCol; i++){ in idxPopulateOneStat1()
13513 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC); in idxPopulateOneStat1()
13514 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC); in idxPopulateOneStat1()
13515 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC); in idxPopulateOneStat1()
13520 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx)); in idxPopulateOneStat1()
13522 assert( pEntry->zVal2==0 ); in idxPopulateOneStat1()
13523 pEntry->zVal2 = zStat; in idxPopulateOneStat1()
13538 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); in idxBuildSampleTable()
13545 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0); in idxBuildSampleTable()
13564 i64 iPrev = -100000; in idxPopulateStat1()
13579 if( p->iSample==0 ) return SQLITE_OK; in idxPopulateStat1()
13581 rc = idxLargestIndex(p->dbm, &nMax, pzErr); in idxPopulateStat1()
13584 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0); in idxPopulateStat1()
13592 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); in idxPopulateStat1()
13599 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0 in idxPopulateStat1()
13604 pCtx->nSlot = nMax+1; in idxPopulateStat1()
13605 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex); in idxPopulateStat1()
13608 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo); in idxPopulateStat1()
13611 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite); in idxPopulateStat1()
13619 if( p->iSample<100 && iPrev!=iRowid ){ in idxPopulateStat1()
13620 samplectx.target = (double)p->iSample / 100.0; in idxPopulateStat1()
13621 samplectx.iTarget = p->iSample; in idxPopulateStat1()
13630 if( rc==SQLITE_OK && p->iSample<100 ){ in idxPopulateStat1()
13631 rc = sqlite3_exec(p->dbv, in idxPopulateStat1()
13641 for(i=0; i<pCtx->nSlot; i++){ in idxPopulateStat1()
13642 sqlite3_free(pCtx->aSlot[i].z); in idxPopulateStat1()
13648 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0); in idxPopulateStat1()
13651 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); in idxPopulateStat1()
13677 ** dummy functions for no-op implementation of UDFs during expert's work
13698 "WHERE builtin==0", -1, &pStmt, 0); in registerUDFs()
13707 /* no-op. Only happens on OOM */ in registerUDFs()
13746 /* Open two in-memory databases to work with. The "vtab database" (dbv) in sqlite3_expert_new()
13753 pNew->db = db; in sqlite3_expert_new()
13754 pNew->iSample = 100; in sqlite3_expert_new()
13755 rc = sqlite3_open(":memory:", &pNew->dbv); in sqlite3_expert_new()
13758 rc = sqlite3_open(":memory:", &pNew->dbm); in sqlite3_expert_new()
13760 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0); in sqlite3_expert_new()
13765 if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbm,0,useDummyCS); in sqlite3_expert_new()
13766 if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbv,0,useDummyCS); in sqlite3_expert_new()
13772 rc = registerUDFs(pNew->db, pNew->dbm); in sqlite3_expert_new()
13775 rc = registerUDFs(pNew->db, pNew->dbv); in sqlite3_expert_new()
13782 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, in sqlite3_expert_new()
13788 if( zSql ) rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg); in sqlite3_expert_new()
13800 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew); in sqlite3_expert_new()
13824 p->iSample = iVal; in sqlite3_expert_config()
13844 IdxScan *pScanOrig = p->pScan; in sqlite3_expert_sql()
13845 IdxStatement *pStmtOrig = p->pStatement; in sqlite3_expert_sql()
13849 if( p->bRun ) return SQLITE_MISUSE; in sqlite3_expert_sql()
13854 rc = idxPrepareStmt(p->db, &pStmt, pzErr, zStmt); in sqlite3_expert_sql()
13857 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt); in sqlite3_expert_sql()
13865 pNew->zSql = (char*)&pNew[1]; in sqlite3_expert_sql()
13866 memcpy(pNew->zSql, z, n+1); in sqlite3_expert_sql()
13867 pNew->pNext = p->pStatement; in sqlite3_expert_sql()
13868 if( p->pStatement ) pNew->iId = p->pStatement->iId+1; in sqlite3_expert_sql()
13869 p->pStatement = pNew; in sqlite3_expert_sql()
13874 idxDatabaseError(p->dbv, pzErr); in sqlite3_expert_sql()
13879 idxScanFree(p->pScan, pScanOrig); in sqlite3_expert_sql()
13880 idxStatementFree(p->pStatement, pStmtOrig); in sqlite3_expert_sql()
13881 p->pScan = pScanOrig; in sqlite3_expert_sql()
13882 p->pStatement = pStmtOrig; in sqlite3_expert_sql()
13895 /* Create candidate indexes within the in-memory database file */ in sqlite3_expert_analyze()
13910 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ in sqlite3_expert_analyze()
13911 p->zCandidates = idxAppendText(&rc, p->zCandidates, in sqlite3_expert_analyze()
13912 "%s;%s%s\n", pEntry->zVal, in sqlite3_expert_analyze()
13913 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2 in sqlite3_expert_analyze()
13924 p->bRun = 1; in sqlite3_expert_analyze()
13935 if( p->pStatement ) nRet = p->pStatement->iId+1; in sqlite3_expert_count()
13946 if( p->bRun==0 ) return 0; in sqlite3_expert_report()
13947 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext); in sqlite3_expert_report()
13950 if( pStmt ) zRet = pStmt->zSql; in sqlite3_expert_report()
13953 if( pStmt ) zRet = pStmt->zIdx; in sqlite3_expert_report()
13956 if( pStmt ) zRet = pStmt->zEQP; in sqlite3_expert_report()
13959 zRet = p->zCandidates; in sqlite3_expert_report()
13970 sqlite3_close(p->dbm); in sqlite3_expert_destroy()
13971 sqlite3_close(p->dbv); in sqlite3_expert_destroy()
13972 idxScanFree(p->pScan, 0); in sqlite3_expert_destroy()
13973 idxStatementFree(p->pStatement, 0); in sqlite3_expert_destroy()
13974 idxTableFree(p->pTable); in sqlite3_expert_destroy()
13975 idxWriteFree(p->pWrite); in sqlite3_expert_destroy()
13976 idxHashClear(&p->hIdx); in sqlite3_expert_destroy()
13977 sqlite3_free(p->zCandidates); in sqlite3_expert_destroy()
13994 ** 2022-08-27
14005 ** This file contains the public interface to the "recover" extension -
14025 ** error. If it returns some other non-SQLITE_OK value, then an error
14093 ** to this function (pCtx) as its first parameter, and a pointer to a
14094 ** nul-terminated buffer containing the SQL statement formated as UTF-8 as
14099 ** If an out-of-memory error occurs, NULL may be returned instead of
14134 ** of a "lost-and-found" table in the output database, or NULL. If
14135 ** the argument is non-NULL and the database contains seemingly
14138 ** pages to add to the lost-and-found table.
14141 ** The pArg value must actually be a pointer to a value of type
14143 ** (argument is 1) and a lost-and-found table has been configured using
14152 ** The pArg value must actually be a pointer to a value of type
14160 ** The pArg value must actually be a pointer to a value of type
14163 ** module creates and populates non-UNIQUE indexes right at the end of the
14164 ** recovery operation - after all recoverable data has been inserted
14169 ** Or, if this option is set (argument is 1), then non-UNIQUE indexes
14193 ** all further such calls on the same recover handle are no-ops that return
14194 ** the same non-SQLITE_OK value.
14211 ** pointer to a buffer containing an English language explanation of
14212 ** the error. If no error message is available, or if an out-of memory
14247 ** 2019-04-17
14263 ** sqlite_dbdata is used to extract data directly from a database b-tree
14264 ** page and its associated overflow pages, bypassing the b-tree layer.
14276 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
14280 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
14283 ** each cell on the page. For intkey b-trees, the key value is stored in
14284 ** field -1.
14288 ** CREATE TABLE t1(a, b); -- root page is page 2
14296 ** (2, 0, -1, 5 ),
14299 ** (2, 1, -1, 10 ),
14316 ** It contains one entry for each b-tree pointer between a parent and
14421 pTab->db = db; in dbdataConnect()
14422 pTab->bPtr = (pAux!=0); in dbdataConnect()
14436 sqlite3_finalize(pTab->pStmt); in dbdataDisconnect()
14458 int iSchema = -1; in dbdataBestIndex()
14459 int iPgno = -1; in dbdataBestIndex()
14460 int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA); in dbdataBestIndex()
14462 for(i=0; i<pIdx->nConstraint; i++){ in dbdataBestIndex()
14463 struct sqlite3_index_constraint *p = &pIdx->aConstraint[i]; in dbdataBestIndex()
14464 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in dbdataBestIndex()
14465 if( p->iColumn==colSchema ){ in dbdataBestIndex()
14466 if( p->usable==0 ) return SQLITE_CONSTRAINT; in dbdataBestIndex()
14469 if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){ in dbdataBestIndex()
14476 pIdx->aConstraintUsage[iSchema].argvIndex = 1; in dbdataBestIndex()
14477 pIdx->aConstraintUsage[iSchema].omit = 1; in dbdataBestIndex()
14480 pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0); in dbdataBestIndex()
14481 pIdx->aConstraintUsage[iPgno].omit = 1; in dbdataBestIndex()
14482 pIdx->estimatedCost = 100; in dbdataBestIndex()
14483 pIdx->estimatedRows = 50; in dbdataBestIndex()
14485 if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){ in dbdataBestIndex()
14486 int iCol = pIdx->aOrderBy[0].iColumn; in dbdataBestIndex()
14487 if( pIdx->nOrderBy==1 ){ in dbdataBestIndex()
14488 pIdx->orderByConsumed = (iCol==0 || iCol==1); in dbdataBestIndex()
14489 }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){ in dbdataBestIndex()
14490 pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1); in dbdataBestIndex()
14495 pIdx->estimatedCost = 100000000; in dbdataBestIndex()
14496 pIdx->estimatedRows = 1000000000; in dbdataBestIndex()
14498 pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00); in dbdataBestIndex()
14513 pCsr->base.pVtab = pVTab; in dbdataOpen()
14525 DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab); in dbdataResetCursor()
14526 if( pTab->pStmt==0 ){ in dbdataResetCursor()
14527 pTab->pStmt = pCsr->pStmt; in dbdataResetCursor()
14529 sqlite3_finalize(pCsr->pStmt); in dbdataResetCursor()
14531 pCsr->pStmt = 0; in dbdataResetCursor()
14532 pCsr->iPgno = 1; in dbdataResetCursor()
14533 pCsr->iCell = 0; in dbdataResetCursor()
14534 pCsr->iField = 0; in dbdataResetCursor()
14535 pCsr->bOnePage = 0; in dbdataResetCursor()
14536 sqlite3_free(pCsr->aPage); in dbdataResetCursor()
14537 sqlite3_free(pCsr->pRec); in dbdataResetCursor()
14538 pCsr->pRec = 0; in dbdataResetCursor()
14539 pCsr->aPage = 0; in dbdataResetCursor()
14553 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
14578 u8 **ppPage, /* OUT: pointer to page buffer */ in dbdataLoadPage()
14583 sqlite3_stmt *pStmt = pCsr->pStmt; in dbdataLoadPage()
14664 return ((eType-12) / 2); in dbdataValueBytes()
14719 int n = ((eType-12) / 2); in dbdataValue()
14747 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; in dbdataNext()
14749 pCsr->iRowid++; in dbdataNext()
14752 int iOff = (pCsr->iPgno==1 ? 100 : 0); in dbdataNext()
14755 if( pCsr->aPage==0 ){ in dbdataNext()
14757 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK; in dbdataNext()
14758 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage); in dbdataNext()
14760 if( pCsr->aPage && pCsr->nPage>=256 ) break; in dbdataNext()
14761 sqlite3_free(pCsr->aPage); in dbdataNext()
14762 pCsr->aPage = 0; in dbdataNext()
14763 if( pCsr->bOnePage ) return SQLITE_OK; in dbdataNext()
14764 pCsr->iPgno++; in dbdataNext()
14767 assert( iOff+3+2<=pCsr->nPage ); in dbdataNext()
14768 pCsr->iCell = pTab->bPtr ? -2 : 0; in dbdataNext()
14769 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]); in dbdataNext()
14772 if( pTab->bPtr ){ in dbdataNext()
14773 if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){ in dbdataNext()
14774 pCsr->iCell = pCsr->nCell; in dbdataNext()
14776 pCsr->iCell++; in dbdataNext()
14777 if( pCsr->iCell>=pCsr->nCell ){ in dbdataNext()
14778 sqlite3_free(pCsr->aPage); in dbdataNext()
14779 pCsr->aPage = 0; in dbdataNext()
14780 if( pCsr->bOnePage ) return SQLITE_OK; in dbdataNext()
14781 pCsr->iPgno++; in dbdataNext()
14787 if( pCsr->pRec==0 ){ in dbdataNext()
14796 switch( pCsr->aPage[iOff] ){ in dbdataNext()
14806 /* This is not a b-tree page with records on it. Continue. */ in dbdataNext()
14807 pCsr->iCell = pCsr->nCell; in dbdataNext()
14811 if( pCsr->iCell>=pCsr->nCell ){ in dbdataNext()
14815 iOff += 8 + nPointer + pCsr->iCell*2; in dbdataNext()
14816 if( iOff>pCsr->nPage ){ in dbdataNext()
14819 iOff = get_uint16(&pCsr->aPage[iOff]); in dbdataNext()
14822 /* For an interior node cell, skip past the child-page number */ in dbdataNext()
14826 if( bNextPage || iOff>pCsr->nPage ){ in dbdataNext()
14829 iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload); in dbdataNext()
14833 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){ in dbdataNext()
14834 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey); in dbdataNext()
14838 U = pCsr->nPage; in dbdataNext()
14840 X = U-35; in dbdataNext()
14842 X = ((U-12)*64/255)-23; in dbdataNext()
14848 M = ((U-12)*32/255)-23; in dbdataNext()
14849 K = M+((nPayload-M)%(U-4)); in dbdataNext()
14857 if( bNextPage || nLocal+iOff>pCsr->nPage ){ in dbdataNext()
14864 pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES); in dbdataNext()
14865 if( pCsr->pRec==0 ) return SQLITE_NOMEM; in dbdataNext()
14866 memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES); in dbdataNext()
14867 pCsr->nRec = nPayload; in dbdataNext()
14870 memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal); in dbdataNext()
14875 sqlite3_int64 nRem = nPayload - nLocal; in dbdataNext()
14876 u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]); in dbdataNext()
14882 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage ); in dbdataNext()
14886 nCopy = U-4; in dbdataNext()
14888 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy); in dbdataNext()
14889 nRem -= nCopy; in dbdataNext()
14896 iHdr = dbdataGetVarintU32(pCsr->pRec, &nHdr); in dbdataNext()
14898 pCsr->nHdr = nHdr; in dbdataNext()
14899 pCsr->pHdrPtr = &pCsr->pRec[iHdr]; in dbdataNext()
14900 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr]; in dbdataNext()
14901 pCsr->iField = (bHasRowid ? -1 : 0); in dbdataNext()
14905 pCsr->iField++; in dbdataNext()
14906 if( pCsr->iField>0 ){ in dbdataNext()
14908 if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){ in dbdataNext()
14912 pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType); in dbdataNext()
14914 if( (pCsr->nRec - (pCsr->pPtr - pCsr->pRec))<szField ){ in dbdataNext()
14915 pCsr->pPtr = &pCsr->pRec[pCsr->nRec]; in dbdataNext()
14917 pCsr->pPtr += szField; in dbdataNext()
14924 sqlite3_free(pCsr->aPage); in dbdataNext()
14925 sqlite3_free(pCsr->pRec); in dbdataNext()
14926 pCsr->aPage = 0; in dbdataNext()
14927 pCsr->pRec = 0; in dbdataNext()
14928 if( pCsr->bOnePage ) return SQLITE_OK; in dbdataNext()
14929 pCsr->iPgno++; in dbdataNext()
14931 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){ in dbdataNext()
14937 sqlite3_free(pCsr->pRec); in dbdataNext()
14938 pCsr->pRec = 0; in dbdataNext()
14939 pCsr->iCell++; in dbdataNext()
14953 return pCsr->aPage==0; in dbdataEof()
14957 ** Return true if nul-terminated string zSchema ends in "()". Or false
14962 if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){ in dbdataIsFunction()
14963 return (int)n-2; in dbdataIsFunction()
14971 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
14975 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab; in dbdataDbsize()
14988 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0); in dbdataDbsize()
14991 pCsr->szDb = sqlite3_column_int(pStmt, 0); in dbdataDbsize()
15000 ** and inspecting the header field. If successful, set the pCsr->enc variable
15009 pCsr->enc = get_uint32(&aPg1[56]); in dbdataGetEncoding()
15025 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; in dbdataFilter()
15032 assert( pCsr->iPgno==1 ); in dbdataFilter()
15038 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]); in dbdataFilter()
15039 pCsr->bOnePage = 1; in dbdataFilter()
15046 if( pTab->pStmt ){ in dbdataFilter()
15047 pCsr->pStmt = pTab->pStmt; in dbdataFilter()
15048 pTab->pStmt = 0; in dbdataFilter()
15054 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0); in dbdataFilter()
15058 rc = sqlite3_prepare_v2(pTab->db, in dbdataFilter()
15059 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1, in dbdataFilter()
15060 &pCsr->pStmt, 0 in dbdataFilter()
15065 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT); in dbdataFilter()
15075 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); in dbdataFilter()
15093 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; in dbdataColumn()
15094 if( pTab->bPtr ){ in dbdataColumn()
15097 sqlite3_result_int64(ctx, pCsr->iPgno); in dbdataColumn()
15100 int iOff = pCsr->iPgno==1 ? 100 : 0; in dbdataColumn()
15101 if( pCsr->iCell<0 ){ in dbdataColumn()
15104 iOff += 12 + pCsr->iCell*2; in dbdataColumn()
15105 if( iOff>pCsr->nPage ) return SQLITE_OK; in dbdataColumn()
15106 iOff = get_uint16(&pCsr->aPage[iOff]); in dbdataColumn()
15108 if( iOff<=pCsr->nPage ){ in dbdataColumn()
15109 sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff])); in dbdataColumn()
15117 sqlite3_result_int64(ctx, pCsr->iPgno); in dbdataColumn()
15120 sqlite3_result_int(ctx, pCsr->iCell); in dbdataColumn()
15123 sqlite3_result_int(ctx, pCsr->iField); in dbdataColumn()
15126 if( pCsr->iField<0 ){ in dbdataColumn()
15127 sqlite3_result_int64(ctx, pCsr->iIntkey); in dbdataColumn()
15128 }else if( &pCsr->pRec[pCsr->nRec] >= pCsr->pPtr ){ in dbdataColumn()
15130 dbdataGetVarintU32(pCsr->pHdrPtr, &iType); in dbdataColumn()
15132 ctx, pCsr->enc, iType, pCsr->pPtr, in dbdataColumn()
15133 &pCsr->pRec[pCsr->nRec] - pCsr->pPtr in dbdataColumn()
15148 *pRowid = pCsr->iRowid; in dbdataRowid()
15164 dbdataOpen, /* xOpen - open a cursor */ in sqlite3DbdataRegister()
15165 dbdataClose, /* xClose - close a cursor */ in sqlite3DbdataRegister()
15166 dbdataFilter, /* xFilter - configure scan constraints */ in sqlite3DbdataRegister()
15167 dbdataNext, /* xNext - advance a cursor */ in sqlite3DbdataRegister()
15168 dbdataEof, /* xEof - check for end of scan */ in sqlite3DbdataRegister()
15169 dbdataColumn, /* xColumn - read data */ in sqlite3DbdataRegister()
15170 dbdataRowid, /* xRowid - read data */ in sqlite3DbdataRegister()
15206 ** 2022-08-27
15268 ** Otherwise, it contains -1. If the table does contain an INTEGER PRIMARY
15275 ** the output database, before any non-schema data are recovered. They
15276 ** are then stored in a singly-linked list linked by this variable beginning
15294 ** The index of the associated field within database records. Or -1 if
15347 u32 aElem[1]; /* Array of 32-bit bitmasks */
15402 int (*xSql)(void*,const char*); /* Pointer to SQL callback function */
15406 char *zLostAndFound; /* Name of lost-and-found table (or NULL) */
15451 ** Populate the recovery.map table - used to figure out a "root" page
15456 ** Populate the lost-and-found table itself.
15492 ** recoverEnterMutex() - Enter the recovery mutex
15493 ** recoverLeaveMutex() - Leave the recovery mutex
15494 ** recoverAssertMutexHeld() - Assert that the recovery mutex is held
15517 ** Like strlen(). But handles NULL pointer arguments.
15525 ** This function is a no-op if the recover handle passed as the first
15526 ** argument already contains an error (if p->errCode!=SQLITE_OK).
15529 ** bytes in size. If successful, a pointer to the new buffer is returned. Or,
15531 ** (p->errCode) set to SQLITE_NOMEM.
15536 if( p->errCode==SQLITE_OK ){ in recoverMalloc()
15541 p->errCode = SQLITE_NOMEM; in recoverMalloc()
15572 sqlite3_free(p->zErrMsg); in recoverError()
15573 p->zErrMsg = z; in recoverError()
15574 p->errCode = errCode; in recoverError()
15580 ** This function is a no-op if p->errCode is initially other than SQLITE_OK.
15593 pRet->nPg = nPg; in recoverBitmapAlloc()
15609 if( iPg<=pMap->nPg ){ in recoverBitmapSet()
15612 pMap->aElem[iElem] |= (((u32)1) << iBit); in recoverBitmapSet()
15622 if( iPg<=pMap->nPg && iPg>0 ){ in recoverBitmapQuery()
15625 ret = (pMap->aElem[iElem] & (((u32)1) << iBit)) ? 1 : 0; in recoverBitmapQuery()
15640 ** This function is a no-op if recover handle p already contains an error
15641 ** (if p->errCode!=SQLITE_OK).
15654 if( p->errCode==SQLITE_OK ){ in recoverPrepare()
15655 if( sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) ){ in recoverPrepare()
15663 ** This function is a no-op if recover handle p already contains an error
15664 ** (if p->errCode!=SQLITE_OK).
15669 ** the statment handle returned. Or, if an error occurs - either during
15670 ** the printf() formatting or when preparing the resulting SQL - an
15679 if( p->errCode==SQLITE_OK ){ in recoverPreparePrintf()
15686 p->errCode = SQLITE_NOMEM; in recoverPreparePrintf()
15701 ** This function returns a copy of the statement handle pointer passed
15706 if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT && p->errCode==SQLITE_OK ){ in recoverReset()
15721 if( rc!=SQLITE_OK && p->errCode==SQLITE_OK ){ in recoverFinalize()
15727 ** This function is a no-op if recover handle p already contains an error
15728 ** (if p->errCode!=SQLITE_OK). A copy of p->errCode is returned in this
15736 if( p->errCode==SQLITE_OK ){ in recoverExec()
15742 return p->errCode; in recoverExec()
15756 if( p->errCode==SQLITE_OK ){ in recoverBindValue()
15763 ** This function is a no-op if recover handle p already contains an error
15764 ** (if p->errCode!=SQLITE_OK). NULL is returned in this case.
15769 ** sqlite3_malloc(). If successful, a pointer to the buffer is returned.
15782 if( p->errCode==SQLITE_OK ){ in recoverMPrintf()
15783 if( z==0 ) p->errCode = SQLITE_NOMEM; in recoverMPrintf()
15792 ** This function is a no-op if recover handle p already contains an error
15793 ** (if p->errCode!=SQLITE_OK). Zero is returned in this case.
15802 if( p->errCode==SQLITE_OK ){ in recoverPageCount()
15804 pStmt = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.page_count", p->zDb); in recoverPageCount()
15816 ** this function must be a blob. The second a non-negative integer. This
15817 ** function reads and returns a 32-bit big-endian integer from byte
15849 ** lost-and-found table, and it depends on those routines having populated
15852 ** The only argument to this function is a page-number. It returns true
15866 sqlite3_result_int(pCtx, recoverBitmapQuery(p->laf.pUsed, pgno)); in recoverPageIsUsed()
15870 ** The implementation of a user-defined SQL function invoked by the
15880 ** SELECT getpage(0); -- return number of pages in db
15881 ** SELECT getpage(4); -- return page 4 of db as a blob of data
15898 if( p->pGetPage==0 ){ in recoverGetPage()
15899 pStmt = p->pGetPage = recoverPreparePrintf( in recoverGetPage()
15900 p, p->dbIn, "SELECT data FROM sqlite_dbpage(%Q) WHERE pgno=?", p->zDb in recoverGetPage()
15902 }else if( p->errCode==SQLITE_OK ){ in recoverGetPage()
15903 pStmt = p->pGetPage; in recoverGetPage()
15911 assert( p->errCode==SQLITE_OK ); in recoverGetPage()
15914 if( pgno==1 && nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){ in recoverGetPage()
15915 aPg = p->pPage1Disk; in recoverGetPage()
15917 sqlite3_result_blob(pCtx, aPg, nPg-p->nReserve, SQLITE_TRANSIENT); in recoverGetPage()
15923 if( p->errCode ){ in recoverGetPage()
15924 if( p->zErrMsg ) sqlite3_result_error(pCtx, p->zErrMsg, -1); in recoverGetPage()
15925 sqlite3_result_error_code(pCtx, p->errCode); in recoverGetPage()
15930 ** Find a string that is not found anywhere in z[]. Return a pointer
15952 ** this function is the output of built-in function quote(). If the first
16041 ** This function is a no-op if recover handle p already contains an error
16042 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
16054 return recoverExec(p, p->dbOut, in recoverCacheSchema()
16075 ** this function is a no-op. Otherwise, issue a callback with SQL statement
16078 ** If the callback returns non-zero, set the recover handle error code to
16082 if( p->errCode==SQLITE_OK && p->xSql ){ in recoverSqlCallback()
16083 int res = p->xSql(p->pSqlCtx, zSql); in recoverSqlCallback()
16085 recoverError(p, SQLITE_ERROR, "callback returned an error - %d", res); in recoverSqlCallback()
16094 ** + page-size,
16095 ** + auto-vacuum settings,
16097 ** + user-version (PRAGMA user_version), and
16098 ** + application-id (PRAGMA application_id), and
16113 if( p->errCode==SQLITE_OK ){ in recoverTransferSettings()
16124 p1 = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.%s", p->zDb, zPrag); in recoverTransferSettings()
16125 if( p->errCode==SQLITE_OK && sqlite3_step(p1)==SQLITE_ROW ){ in recoverTransferSettings()
16139 if( p->errCode==SQLITE_OK ){ in recoverTransferSettings()
16140 sqlite3 *db = p->dbOut; in recoverTransferSettings()
16143 sqlite3_backup_step(pBackup, -1); in recoverTransferSettings()
16144 p->errCode = sqlite3_backup_finish(pBackup); in recoverTransferSettings()
16155 ** This function is a no-op if recover handle p already contains an error
16156 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
16184 assert( p->dbOut==0 ); in recoverOpenOutput()
16186 if( sqlite3_open_v2(p->zUri, &db, flags, 0) ){ in recoverOpenOutput()
16191 ** These two are registered with the output database handle - this in recoverOpenOutput()
16194 if( p->errCode==SQLITE_OK ){ in recoverOpenOutput()
16195 p->errCode = sqlite3_dbdata_init(db, 0, 0); in recoverOpenOutput()
16198 /* Register the custom user-functions with the output handle. */ in recoverOpenOutput()
16200 p->errCode==SQLITE_OK && ii<(int)(sizeof(aFunc)/sizeof(aFunc[0])); in recoverOpenOutput()
16202 p->errCode = sqlite3_create_function(db, aFunc[ii].zName, in recoverOpenOutput()
16207 p->dbOut = db; in recoverOpenOutput()
16208 return p->errCode; in recoverOpenOutput()
16217 char *zSql = recoverMPrintf(p, "ATTACH %Q AS recovery;", p->zStateDb); in recoverOpenRecovery()
16218 recoverExec(p, p->dbOut, zSql); in recoverOpenRecovery()
16219 recoverExec(p, p->dbOut, in recoverOpenRecovery()
16229 ** This function is a no-op if recover handle p already contains an error
16230 ** (if p->errCode!=SQLITE_OK).
16246 sqlite3_stmt *pStmt = recoverPreparePrintf(p, p->dbOut, in recoverAddTable()
16251 int iPk = -1; in recoverAddTable()
16269 pNew->aCol = (RecoverColumn*)&pNew[1]; in recoverAddTable()
16270 pNew->zTab = csr = (char*)&pNew->aCol[nCol]; in recoverAddTable()
16271 pNew->nCol = nCol; in recoverAddTable()
16272 pNew->iRoot = iRoot; in recoverAddTable()
16283 if( iPk==-1 && iPKF==1 && !sqlite3_stricmp("integer", zType) ) iPk = i; in recoverAddTable()
16284 if( iPKF>1 ) iPk = -2; in recoverAddTable()
16285 pNew->aCol[i].zCol = csr; in recoverAddTable()
16286 pNew->aCol[i].eHidden = eHidden; in recoverAddTable()
16288 pNew->aCol[i].iField = -1; in recoverAddTable()
16290 pNew->aCol[i].iField = iField++; in recoverAddTable()
16295 pNew->aCol[i].iBind = iBind++; in recoverAddTable()
16301 pNew->pNext = p->pTblList; in recoverAddTable()
16302 p->pTblList = pNew; in recoverAddTable()
16303 pNew->bIntkey = 1; in recoverAddTable()
16308 pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName); in recoverAddTable()
16313 assert( iCol<pNew->nCol ); in recoverAddTable()
16314 pNew->aCol[iCol].iField = iField; in recoverAddTable()
16316 pNew->bIntkey = 0; in recoverAddTable()
16317 iPk = -2; in recoverAddTable()
16321 if( p->errCode==SQLITE_OK ){ in recoverAddTable()
16323 pNew->aCol[iPk].bIPK = 1; in recoverAddTable()
16324 }else if( pNew->bIntkey ){ in recoverAddTable()
16325 pNew->iRowidBind = iBind++; in recoverAddTable()
16354 pSelect = recoverPrepare(p, p->dbOut, in recoverWriteSchema1()
16368 pTblname = recoverPrepare(p, p->dbOut, in recoverWriteSchema1()
16374 sqlite3_bind_int(pSelect, 1, p->bSlowIndexes); in recoverWriteSchema1()
16390 rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0); in recoverWriteSchema1()
16401 recoverDbError(p, p->dbOut); in recoverWriteSchema1()
16409 return p->errCode; in recoverWriteSchema1()
16415 ** database by recoverWriteSchema1() - everything except for tables and
16420 ** * non-UNIQUE indexes.
16428 pSelect = recoverPrepare(p, p->dbOut, in recoverWriteSchema2()
16429 p->bSlowIndexes ? in recoverWriteSchema2()
16440 int rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0); in recoverWriteSchema2()
16444 recoverDbError(p, p->dbOut); in recoverWriteSchema2()
16450 return p->errCode; in recoverWriteSchema2()
16454 ** This function is a no-op if recover handle p already contains an error
16455 ** (if p->errCode!=SQLITE_OK). In this case it returns NULL.
16477 ** a single text value - itself an SQL statement similar to the above,
16500 int bSql = p->xSql ? 1 : 0; in recoverInsertStmt()
16504 assert( nField<=pTab->nCol ); in recoverInsertStmt()
16506 zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab); in recoverInsertStmt()
16508 if( pTab->iRowidBind ){ in recoverInsertStmt()
16509 assert( pTab->bIntkey ); in recoverInsertStmt()
16512 zBind = recoverMPrintf(p, "%zquote(?%d)", zBind, pTab->iRowidBind); in recoverInsertStmt()
16514 zBind = recoverMPrintf(p, "%z?%d", zBind, pTab->iRowidBind); in recoverInsertStmt()
16521 int eHidden = pTab->aCol[ii].eHidden; in recoverInsertStmt()
16525 assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 ); in recoverInsertStmt()
16526 zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol); in recoverInsertStmt()
16530 "%z%sescape_crnl(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind in recoverInsertStmt()
16534 zBind = recoverMPrintf(p, "%z%s?%d", zBind, zSep, pTab->aCol[ii].iBind); in recoverInsertStmt()
16548 pRet = recoverPrepare(p, p->dbOut, zFinal); in recoverInsertStmt()
16558 ** Search the list of RecoverTable objects at p->pTblList for one that
16560 ** return a pointer to it. Otherwise, return NULL.
16564 for(pRet=p->pTblList; pRet && pRet->iRoot!=iRoot; pRet=pRet->pNext); in recoverFindTable()
16570 ** output db. If successful, it returns a pointer to a buffer containing
16585 pProbe = recoverPrepare(p, p->dbOut, in recoverLostAndFoundCreate()
16588 for(ii=-1; zTbl==0 && p->errCode==SQLITE_OK && ii<1000; ii++){ in recoverLostAndFoundCreate()
16591 zTbl = recoverMPrintf(p, "%s", p->zLostAndFound); in recoverLostAndFoundCreate()
16593 zTbl = recoverMPrintf(p, "%s_%d", p->zLostAndFound, ii); in recoverLostAndFoundCreate()
16596 if( p->errCode==SQLITE_OK ){ in recoverLostAndFoundCreate()
16597 sqlite3_bind_text(pProbe, 1, zTbl, -1, SQLITE_STATIC); in recoverLostAndFoundCreate()
16618 for(ii=0; p->errCode==SQLITE_OK && ii<nField; ii++){ in recoverLostAndFoundCreate()
16626 recoverExec(p, p->dbOut, zSql); in recoverLostAndFoundCreate()
16629 }else if( p->errCode==SQLITE_OK ){ in recoverLostAndFoundCreate()
16631 p, SQLITE_ERROR, "failed to create %s output table", p->zLostAndFound in recoverLostAndFoundCreate()
16653 if( p->xSql==0 ){ in recoverLostAndFoundInsert()
16658 p, p->dbOut, "INSERT INTO %s VALUES(%s)", zTab, zBind in recoverLostAndFoundInsert()
16667 p, p->dbOut, "SELECT 'INSERT INTO %s VALUES(' || %s || ')'", zTab, zBind in recoverLostAndFoundInsert()
16677 ** lost-and-found table of the output database. This function attempts
16689 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFoundFindRoot()
16691 if( pLaf->pFindRoot==0 ){ in recoverLostAndFoundFindRoot()
16692 pLaf->pFindRoot = recoverPrepare(p, p->dbOut, in recoverLostAndFoundFindRoot()
16702 if( p->errCode==SQLITE_OK ){ in recoverLostAndFoundFindRoot()
16703 sqlite3_bind_int64(pLaf->pFindRoot, 1, iPg); in recoverLostAndFoundFindRoot()
16704 if( sqlite3_step(pLaf->pFindRoot)==SQLITE_ROW ){ in recoverLostAndFoundFindRoot()
16705 *piRoot = sqlite3_column_int64(pLaf->pFindRoot, 0); in recoverLostAndFoundFindRoot()
16709 recoverReset(p, pLaf->pFindRoot); in recoverLostAndFoundFindRoot()
16711 return p->errCode; in recoverLostAndFoundFindRoot()
16716 ** the lost-and-found table in the output database.
16719 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFoundOnePage()
16720 sqlite3_value **apVal = pLaf->apVal; in recoverLostAndFoundOnePage()
16721 sqlite3_stmt *pPageData = pLaf->pPageData; in recoverLostAndFoundOnePage()
16722 sqlite3_stmt *pInsert = pLaf->pInsert; in recoverLostAndFoundOnePage()
16724 int nVal = -1; in recoverLostAndFoundOnePage()
16733 while( p->errCode==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPageData) ){ in recoverLostAndFoundOnePage()
16760 nVal = -1; in recoverLostAndFoundOnePage()
16766 assert( nVal==-1 ); in recoverLostAndFoundOnePage()
16770 }else if( iField<pLaf->nMaxField ){ in recoverLostAndFoundOnePage()
16773 assert( iField==nVal || (nVal==-1 && iField==0) ); in recoverLostAndFoundOnePage()
16793 ** RECOVER_STATE_LOSTANDFOUND3 state - during which the lost-and-found
16798 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound3Step()
16799 if( p->errCode==SQLITE_OK ){ in recoverLostAndFound3Step()
16800 if( pLaf->pInsert==0 ){ in recoverLostAndFound3Step()
16803 if( p->errCode==SQLITE_OK ){ in recoverLostAndFound3Step()
16804 int res = sqlite3_step(pLaf->pAllPage); in recoverLostAndFound3Step()
16806 i64 iPage = sqlite3_column_int64(pLaf->pAllPage, 0); in recoverLostAndFound3Step()
16807 if( recoverBitmapQuery(pLaf->pUsed, iPage)==0 ){ in recoverLostAndFound3Step()
16811 recoverReset(p, pLaf->pAllPage); in recoverLostAndFound3Step()
16822 ** state - during which the lost-and-found table of the output database
16827 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound3Init()
16829 if( pLaf->nMaxField>0 ){ in recoverLostAndFound3Init()
16832 zTab = recoverLostAndFoundCreate(p, pLaf->nMaxField); in recoverLostAndFound3Init()
16833 pLaf->pInsert = recoverLostAndFoundInsert(p, zTab, pLaf->nMaxField); in recoverLostAndFound3Init()
16836 pLaf->pAllPage = recoverPreparePrintf(p, p->dbOut, in recoverLostAndFound3Init()
16840 "SELECT ii FROM seq" , p->laf.nPg in recoverLostAndFound3Init()
16842 pLaf->pPageData = recoverPrepare(p, p->dbOut, in recoverLostAndFound3Init()
16846 "SELECT -1, -1, -1" in recoverLostAndFound3Init()
16849 pLaf->apVal = (sqlite3_value**)recoverMalloc(p, in recoverLostAndFound3Init()
16850 pLaf->nMaxField*sizeof(sqlite3_value*) in recoverLostAndFound3Init()
16856 ** Initialize resources required in RECOVER_STATE_WRITING state - during which
16861 RecoverStateW1 *p1 = &p->w1; in recoverWriteDataInit()
16866 assert( p1->nMax==0 ); in recoverWriteDataInit()
16867 for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){ in recoverWriteDataInit()
16868 if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol; in recoverWriteDataInit()
16873 nByte = sizeof(sqlite3_value*) * (p1->nMax+1); in recoverWriteDataInit()
16874 p1->apVal = (sqlite3_value**)recoverMalloc(p, nByte); in recoverWriteDataInit()
16875 if( p1->apVal==0 ) return p->errCode; in recoverWriteDataInit()
16879 p1->pTbls = recoverPrepare(p, p->dbOut, in recoverWriteDataInit()
16884 p1->pSel = recoverPrepare(p, p->dbOut, in recoverWriteDataInit()
16897 return p->errCode; in recoverWriteDataInit()
16905 RecoverStateW1 *p1 = &p->w1; in recoverWriteDataCleanup()
16907 for(ii=0; ii<p1->nVal; ii++){ in recoverWriteDataCleanup()
16908 sqlite3_value_free(p1->apVal[ii]); in recoverWriteDataCleanup()
16910 sqlite3_free(p1->apVal); in recoverWriteDataCleanup()
16911 recoverFinalize(p, p1->pInsert); in recoverWriteDataCleanup()
16912 recoverFinalize(p, p1->pTbls); in recoverWriteDataCleanup()
16913 recoverFinalize(p, p1->pSel); in recoverWriteDataCleanup()
16920 ** RECOVER_STATE_WRITING state - during which tables recovered from the
16924 RecoverStateW1 *p1 = &p->w1; in recoverWriteDataStep()
16925 sqlite3_stmt *pSel = p1->pSel; in recoverWriteDataStep()
16926 sqlite3_value **apVal = p1->apVal; in recoverWriteDataStep()
16928 if( p->errCode==SQLITE_OK && p1->pTab==0 ){ in recoverWriteDataStep()
16929 if( sqlite3_step(p1->pTbls)==SQLITE_ROW ){ in recoverWriteDataStep()
16930 i64 iRoot = sqlite3_column_int64(p1->pTbls, 0); in recoverWriteDataStep()
16931 p1->pTab = recoverFindTable(p, iRoot); in recoverWriteDataStep()
16933 recoverFinalize(p, p1->pInsert); in recoverWriteDataStep()
16934 p1->pInsert = 0; in recoverWriteDataStep()
16938 if( p1->pTab==0 ) return p->errCode; in recoverWriteDataStep()
16942 ** keys before recovering its contents. The p1->pTbls SELECT statement in recoverWriteDataStep()
16945 if( sqlite3_stricmp("sqlite_sequence", p1->pTab->zTab)==0 ){ in recoverWriteDataStep()
16946 recoverExec(p, p->dbOut, "DELETE FROM sqlite_sequence"); in recoverWriteDataStep()
16951 ** SELECT statement p1->pSel. The SELECT statement will then iterate in recoverWriteDataStep()
16955 p1->nVal = 0; in recoverWriteDataStep()
16956 p1->bHaveRowid = 0; in recoverWriteDataStep()
16957 p1->iPrevPage = -1; in recoverWriteDataStep()
16958 p1->iPrevCell = -1; in recoverWriteDataStep()
16963 assert( p->errCode!=SQLITE_OK || p1->pTab ); in recoverWriteDataStep()
16965 if( p->errCode==SQLITE_OK && sqlite3_step(pSel)==SQLITE_ROW ){ in recoverWriteDataStep()
16966 RecoverTable *pTab = p1->pTab; in recoverWriteDataStep()
16972 int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell); in recoverWriteDataStep()
16974 assert( bNewCell==0 || (iField==-1 || iField==0) ); in recoverWriteDataStep()
16975 assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol ); in recoverWriteDataStep()
16979 if( p1->nVal>=0 ){ in recoverWriteDataStep()
16980 if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){ in recoverWriteDataStep()
16981 recoverFinalize(p, p1->pInsert); in recoverWriteDataStep()
16982 p1->pInsert = recoverInsertStmt(p, pTab, p1->nVal); in recoverWriteDataStep()
16983 p1->nInsert = p1->nVal; in recoverWriteDataStep()
16985 if( p1->nVal>0 ){ in recoverWriteDataStep()
16986 sqlite3_stmt *pInsert = p1->pInsert; in recoverWriteDataStep()
16987 for(ii=0; ii<pTab->nCol; ii++){ in recoverWriteDataStep()
16988 RecoverColumn *pCol = &pTab->aCol[ii]; in recoverWriteDataStep()
16989 int iBind = pCol->iBind; in recoverWriteDataStep()
16991 if( pCol->bIPK ){ in recoverWriteDataStep()
16992 sqlite3_bind_int64(pInsert, iBind, p1->iRowid); in recoverWriteDataStep()
16993 }else if( pCol->iField<p1->nVal ){ in recoverWriteDataStep()
16994 recoverBindValue(p, pInsert, iBind, apVal[pCol->iField]); in recoverWriteDataStep()
16998 if( p->bRecoverRowid && pTab->iRowidBind>0 && p1->bHaveRowid ){ in recoverWriteDataStep()
16999 sqlite3_bind_int64(pInsert, pTab->iRowidBind, p1->iRowid); in recoverWriteDataStep()
17006 assert( p->errCode || pInsert ); in recoverWriteDataStep()
17011 for(ii=0; ii<p1->nVal; ii++){ in recoverWriteDataStep()
17015 p1->nVal = -1; in recoverWriteDataStep()
17016 p1->bHaveRowid = 0; in recoverWriteDataStep()
17021 p1->iRowid = sqlite3_column_int64(pSel, 3); in recoverWriteDataStep()
17022 assert( p1->nVal==-1 ); in recoverWriteDataStep()
17023 p1->nVal = 0; in recoverWriteDataStep()
17024 p1->bHaveRowid = 1; in recoverWriteDataStep()
17025 }else if( iField<pTab->nCol ){ in recoverWriteDataStep()
17031 p1->nVal = iField+1; in recoverWriteDataStep()
17033 p1->iPrevCell = iCell; in recoverWriteDataStep()
17034 p1->iPrevPage = iPage; in recoverWriteDataStep()
17038 p1->pTab = 0; in recoverWriteDataStep()
17041 return p->errCode; in recoverWriteDataStep()
17046 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
17050 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound1Init()
17053 assert( p->laf.pUsed==0 ); in recoverLostAndFound1Init()
17054 pLaf->nPg = recoverPageCount(p); in recoverLostAndFound1Init()
17055 pLaf->pUsed = recoverBitmapAlloc(p, pLaf->nPg); in recoverLostAndFound1Init()
17059 ** if !p->bFreelistCorrupt, add all pages that appear to be part of the in recoverLostAndFound1Init()
17062 p, p->dbOut, in recoverLostAndFound1Init()
17072 " SELECT data, min(16384, read_i32(data, 1)-1), pgno FROM trunkdata" in recoverLostAndFound1Init()
17074 " SELECT data, n-1, read_i32(data, 2+n) FROM freelist WHERE n>=0" in recoverLostAndFound1Init()
17091 if( pStmt ) sqlite3_bind_int(pStmt, 1, p->bFreelistCorrupt); in recoverLostAndFound1Init()
17092 pLaf->pUsedPages = pStmt; in recoverLostAndFound1Init()
17098 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
17102 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound1Step()
17103 int rc = p->errCode; in recoverLostAndFound1Step()
17105 rc = sqlite3_step(pLaf->pUsedPages); in recoverLostAndFound1Step()
17107 i64 iPg = sqlite3_column_int64(pLaf->pUsedPages, 0); in recoverLostAndFound1Step()
17108 recoverBitmapSet(pLaf->pUsed, iPg); in recoverLostAndFound1Step()
17111 recoverFinalize(p, pLaf->pUsedPages); in recoverLostAndFound1Step()
17112 pLaf->pUsedPages = 0; in recoverLostAndFound1Step()
17120 ** state - during which the pages identified in RECOVER_STATE_LOSTANDFOUND1
17124 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound2Init()
17126 assert( p->laf.pAllAndParent==0 ); in recoverLostAndFound2Init()
17127 assert( p->laf.pMapInsert==0 ); in recoverLostAndFound2Init()
17128 assert( p->laf.pMaxField==0 ); in recoverLostAndFound2Init()
17129 assert( p->laf.nMaxField==0 ); in recoverLostAndFound2Init()
17131 pLaf->pMapInsert = recoverPrepare(p, p->dbOut, in recoverLostAndFound2Init()
17134 pLaf->pAllAndParent = recoverPreparePrintf(p, p->dbOut, in recoverLostAndFound2Init()
17140 "SELECT NULL, ii FROM seq", p->laf.nPg in recoverLostAndFound2Init()
17142 pLaf->pMaxField = recoverPreparePrintf(p, p->dbOut, in recoverLostAndFound2Init()
17150 ** RECOVER_STATE_LOSTANDFOUND2 state - during which the pages identified
17155 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound2Step()
17156 if( p->errCode==SQLITE_OK ){ in recoverLostAndFound2Step()
17157 int res = sqlite3_step(pLaf->pAllAndParent); in recoverLostAndFound2Step()
17159 i64 iChild = sqlite3_column_int(pLaf->pAllAndParent, 1); in recoverLostAndFound2Step()
17160 if( recoverBitmapQuery(pLaf->pUsed, iChild)==0 ){ in recoverLostAndFound2Step()
17161 sqlite3_bind_int64(pLaf->pMapInsert, 1, iChild); in recoverLostAndFound2Step()
17162 sqlite3_bind_value(pLaf->pMapInsert, 2, in recoverLostAndFound2Step()
17163 sqlite3_column_value(pLaf->pAllAndParent, 0) in recoverLostAndFound2Step()
17165 sqlite3_step(pLaf->pMapInsert); in recoverLostAndFound2Step()
17166 recoverReset(p, pLaf->pMapInsert); in recoverLostAndFound2Step()
17167 sqlite3_bind_int64(pLaf->pMaxField, 1, iChild); in recoverLostAndFound2Step()
17168 if( SQLITE_ROW==sqlite3_step(pLaf->pMaxField) ){ in recoverLostAndFound2Step()
17169 int nMax = sqlite3_column_int(pLaf->pMaxField, 0); in recoverLostAndFound2Step()
17170 if( nMax>pLaf->nMaxField ) pLaf->nMaxField = nMax; in recoverLostAndFound2Step()
17172 recoverReset(p, pLaf->pMaxField); in recoverLostAndFound2Step()
17175 recoverFinalize(p, pLaf->pAllAndParent); in recoverLostAndFound2Step()
17176 pLaf->pAllAndParent =0; in recoverLostAndFound2Step()
17180 return p->errCode; in recoverLostAndFound2Step()
17188 recoverBitmapFree(p->laf.pUsed); in recoverLostAndFoundCleanup()
17189 p->laf.pUsed = 0; in recoverLostAndFoundCleanup()
17190 sqlite3_finalize(p->laf.pUsedPages); in recoverLostAndFoundCleanup()
17191 sqlite3_finalize(p->laf.pAllAndParent); in recoverLostAndFoundCleanup()
17192 sqlite3_finalize(p->laf.pMapInsert); in recoverLostAndFoundCleanup()
17193 sqlite3_finalize(p->laf.pMaxField); in recoverLostAndFoundCleanup()
17194 sqlite3_finalize(p->laf.pFindRoot); in recoverLostAndFoundCleanup()
17195 sqlite3_finalize(p->laf.pInsert); in recoverLostAndFoundCleanup()
17196 sqlite3_finalize(p->laf.pAllPage); in recoverLostAndFoundCleanup()
17197 sqlite3_finalize(p->laf.pPageData); in recoverLostAndFoundCleanup()
17198 p->laf.pUsedPages = 0; in recoverLostAndFoundCleanup()
17199 p->laf.pAllAndParent = 0; in recoverLostAndFoundCleanup()
17200 p->laf.pMapInsert = 0; in recoverLostAndFoundCleanup()
17201 p->laf.pMaxField = 0; in recoverLostAndFoundCleanup()
17202 p->laf.pFindRoot = 0; in recoverLostAndFoundCleanup()
17203 p->laf.pInsert = 0; in recoverLostAndFoundCleanup()
17204 p->laf.pAllPage = 0; in recoverLostAndFoundCleanup()
17205 p->laf.pPageData = 0; in recoverLostAndFoundCleanup()
17206 sqlite3_free(p->laf.apVal); in recoverLostAndFoundCleanup()
17207 p->laf.apVal = 0; in recoverLostAndFoundCleanup()
17220 for(pTab=p->pTblList; pTab; pTab=pNext){ in recoverFinalCleanup()
17221 pNext = pTab->pNext; in recoverFinalCleanup()
17224 p->pTblList = 0; in recoverFinalCleanup()
17225 sqlite3_finalize(p->pGetPage); in recoverFinalCleanup()
17226 p->pGetPage = 0; in recoverFinalCleanup()
17227 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0); in recoverFinalCleanup()
17233 sqlite3_close(p->dbOut); in recoverFinalCleanup()
17236 p->dbOut = 0; in recoverFinalCleanup()
17240 ** Decode and return an unsigned 16-bit big-endian integer value from
17248 ** Decode and return an unsigned 32-bit big-endian integer value from
17273 ** or a prefix thereof appears to contain a well-formed SQLite b-tree page,
17274 ** return the page-size in bytes. Otherwise, if the buffer does not
17275 ** appear to contain a well-formed b-tree page, return 0.
17302 /* Follow the free-list. This is the same format for all b-tree pages. */ in recoverIsValidPage()
17307 if( iFree>(n-4) ) return 0; in recoverIsValidPage()
17338 int X = (eType==0x0D) ? n-35 : (((n-12)*64/255)-23); in recoverIsValidPage()
17339 int M = ((n-12)*32/255)-23; in recoverIsValidPage()
17340 int K = M+((nPayload-M)%(n-4)); in recoverIsValidPage()
17412 assert( pFd->pMethods!=&recover_methods ); in recoverVfsClose()
17413 return pFd->pMethods->xClose(pFd); in recoverVfsClose()
17417 ** Write value v to buffer a[] as a 16-bit big-endian unsigned integer.
17425 ** Write value v to buffer a[] as a 32-bit big-endian unsigned integer.
17435 ** Detect the page-size of the database opened by file-handle pFd by
17436 ** searching the first part of the file for a well-formed SQLite b-tree
17437 ** page. If parameter nReserve is non-zero, then as well as searching for
17438 ** a b-tree page with zero reserved bytes, this function searches for one
17441 ** If successful, set variable p->detected_pgsz to the detected page-size
17443 ** can be found, return SQLITE_OK but leave p->detected_pgsz set to 0. Or,
17445 ** is returned. The final value of p->detected_pgsz is undefined in this
17450 sqlite3_file *pFd, /* File-handle open on input database */ in recoverVfsDetectPagesize()
17468 nBlk = (nSz+nMax-1)/nMax; in recoverVfsDetectPagesize()
17475 rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax); in recoverVfsDetectPagesize()
17481 if( recoverIsValidPage(aTmp, &aPg[iOff], pgsz2-nReserve) ){ in recoverVfsDetectPagesize()
17489 if( pgsz>(u32)p->detected_pgsz ){ in recoverVfsDetectPagesize()
17490 p->detected_pgsz = pgsz; in recoverVfsDetectPagesize()
17491 p->nReserve = nReserve; in recoverVfsDetectPagesize()
17497 p->detected_pgsz = pgsz; in recoverVfsDetectPagesize()
17508 if( pFd->pMethods==&recover_methods ){ in recoverVfsRead()
17509 pFd->pMethods = recover_g.pMethods; in recoverVfsRead()
17510 rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff); in recoverVfsRead()
17518 ** + Database page size (16-bits at offset 16) in recoverVfsRead()
17519 ** + Size of db in pages (32-bits at offset 28) in recoverVfsRead()
17520 ** + Database encoding (32-bits at offset 56) in recoverVfsRead()
17524 ** + first freelist page (32-bits at offset 32) in recoverVfsRead()
17525 ** + size of freelist (32-bits at offset 36) in recoverVfsRead()
17526 ** + the wal-mode flags (16-bits at offset 18) in recoverVfsRead()
17528 ** We also try to preserve the auto-vacuum, incr-value, user-version in recoverVfsRead()
17529 ** and application-id fields - all 32 bit quantities at offsets in recoverVfsRead()
17532 ** Byte offset 105 should also contain the page-size as a 16-bit in recoverVfsRead()
17564 rc = pFd->pMethods->xFileSize(pFd, &dbFileSize); in recoverVfsRead()
17566 if( rc==SQLITE_OK && p->detected_pgsz==0 ){ in recoverVfsRead()
17569 if( p->detected_pgsz ){ in recoverVfsRead()
17570 pgsz = p->detected_pgsz; in recoverVfsRead()
17571 nReserve = p->nReserve; in recoverVfsRead()
17581 sqlite3_free(p->pPage1Cache); in recoverVfsRead()
17582 p->pPage1Cache = 0; in recoverVfsRead()
17583 p->pPage1Disk = 0; in recoverVfsRead()
17585 p->pgsz = nByte; in recoverVfsRead()
17586 p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2); in recoverVfsRead()
17587 if( p->pPage1Cache ){ in recoverVfsRead()
17588 p->pPage1Disk = &p->pPage1Cache[nByte]; in recoverVfsRead()
17589 memcpy(p->pPage1Disk, aBuf, nByte); in recoverVfsRead()
17594 recoverPutU16(&aHdr[105], pgsz-nReserve); in recoverVfsRead()
17602 memset(&((u8*)aBuf)[sizeof(aHdr)], 0, nByte-sizeof(aHdr)); in recoverVfsRead()
17604 memcpy(p->pPage1Cache, aBuf, nByte); in recoverVfsRead()
17606 rc = p->errCode; in recoverVfsRead()
17610 pFd->pMethods = &recover_methods; in recoverVfsRead()
17612 rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff); in recoverVfsRead()
17622 if( pFd->pMethods==&recover_methods ){ \
17623 pFd->pMethods = recover_g.pMethods; \
17625 pFd->pMethods = &recover_methods; \
17641 pFd->pMethods->xWrite(pFd, aBuf, nByte, iOff) in recoverVfsWrite()
17646 pFd->pMethods->xTruncate(pFd, size) in recoverVfsTruncate()
17651 pFd->pMethods->xSync(pFd, flags) in recoverVfsSync()
17656 pFd->pMethods->xFileSize(pFd, pSize) in recoverVfsFileSize()
17661 pFd->pMethods->xLock(pFd, eLock) in recoverVfsLock()
17666 pFd->pMethods->xUnlock(pFd, eLock) in recoverVfsUnlock()
17671 pFd->pMethods->xCheckReservedLock(pFd, pResOut) in recoverVfsCheckReservedLock()
17676 (pFd->pMethods ? pFd->pMethods->xFileControl(pFd, op, pArg) : SQLITE_NOTFOUND) in recoverVfsFileControl()
17681 pFd->pMethods->xSectorSize(pFd) in recoverVfsSectorSize()
17686 pFd->pMethods->xDeviceCharacteristics(pFd) in recoverVfsDeviceCharacteristics()
17693 pFd->pMethods->xShmMap(pFd, iPg, pgsz, bExtend, pp) in recoverVfsShmMap()
17698 pFd->pMethods->xShmLock(pFd, offset, n, flags) in recoverVfsShmLock()
17702 if( pFd->pMethods==&recover_methods ){ in recoverVfsShmBarrier()
17703 pFd->pMethods = recover_g.pMethods; in recoverVfsShmBarrier()
17704 pFd->pMethods->xShmBarrier(pFd); in recoverVfsShmBarrier()
17705 pFd->pMethods = &recover_methods; in recoverVfsShmBarrier()
17707 pFd->pMethods->xShmBarrier(pFd); in recoverVfsShmBarrier()
17712 pFd->pMethods->xShmUnmap(pFd, deleteFlag) in recoverVfsShmUnmap()
17736 ** Install the VFS wrapper around the file-descriptor open on the input
17744 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd); in recoverInstallWrapper()
17745 assert( pFd==0 || pFd->pMethods!=&recover_methods ); in recoverInstallWrapper()
17746 if( pFd && pFd->pMethods ){ in recoverInstallWrapper()
17747 int iVersion = 1 + (pFd->pMethods->iVersion>1 && pFd->pMethods->xShmMap!=0); in recoverInstallWrapper()
17748 recover_g.pMethods = pFd->pMethods; in recoverInstallWrapper()
17751 pFd->pMethods = &recover_methods; in recoverInstallWrapper()
17756 ** Uninstall the VFS wrapper that was installed around the file-descriptor open
17763 sqlite3_file_control(p->dbIn, p->zDb,SQLITE_FCNTL_FILE_POINTER,(void*)&pFd); in recoverUninstallWrapper()
17764 if( pFd && pFd->pMethods ){ in recoverUninstallWrapper()
17765 pFd->pMethods = recover_g.pMethods; in recoverUninstallWrapper()
17777 assert( p && p->errCode==SQLITE_OK ); in recoverStep()
17778 switch( p->eState ){ in recoverStep()
17793 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0); in recoverStep()
17794 recoverExec(p, p->dbIn, "PRAGMA writable_schema = on"); in recoverStep()
17795 recoverExec(p, p->dbIn, "BEGIN"); in recoverStep()
17796 if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1; in recoverStep()
17797 recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema"); in recoverStep()
17805 recoverExec(p, p->dbOut, "BEGIN"); in recoverStep()
17808 p->eState = RECOVER_STATE_WRITING; in recoverStep()
17812 if( p->w1.pTbls==0 ){ in recoverStep()
17817 if( p->zLostAndFound ){ in recoverStep()
17818 p->eState = RECOVER_STATE_LOSTANDFOUND1; in recoverStep()
17820 p->eState = RECOVER_STATE_SCHEMA2; in recoverStep()
17827 if( p->laf.pUsed==0 ){ in recoverStep()
17831 p->eState = RECOVER_STATE_LOSTANDFOUND2; in recoverStep()
17836 if( p->laf.pAllAndParent==0 ){ in recoverStep()
17840 p->eState = RECOVER_STATE_LOSTANDFOUND3; in recoverStep()
17846 if( p->laf.pInsert==0 ){ in recoverStep()
17850 p->eState = RECOVER_STATE_SCHEMA2; in recoverStep()
17859 p->eState = RECOVER_STATE_DONE; in recoverStep()
17864 recoverExec(p, p->dbOut, "COMMIT"); in recoverStep()
17865 rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0); in recoverStep()
17866 if( p->errCode==SQLITE_OK ) p->errCode = rc; in recoverStep()
17870 p->eState = RECOVER_STATE_DONE; in recoverStep()
17876 /* no-op */ in recoverStep()
17915 pRet->dbIn = db; in recoverInit()
17916 pRet->zDb = (char*)&pRet[1]; in recoverInit()
17917 pRet->zUri = &pRet->zDb[nDb+1]; in recoverInit()
17918 memcpy(pRet->zDb, zDb, nDb); in recoverInit()
17919 if( nUri>0 && zUri ) memcpy(pRet->zUri, zUri, nUri); in recoverInit()
17920 pRet->xSql = xSql; in recoverInit()
17921 pRet->pSqlCtx = pSqlCtx; in recoverInit()
17922 pRet->bRecoverRowid = RECOVER_ROWID_DEFAULT; in recoverInit()
17957 return (p && p->errCode!=SQLITE_NOMEM) ? p->zErrMsg : "out of memory"; in sqlite3_recover_errmsg()
17964 return p ? p->errCode : SQLITE_NOMEM; in sqlite3_recover_errcode()
17974 }else if( p->eState!=RECOVER_STATE_INIT ){ in sqlite3_recover_config()
17980 ** name of the auxiliary database that is ATTACH-ed to the database in sqlite3_recover_config()
17984 sqlite3_free(p->zStateDb); in sqlite3_recover_config()
17985 p->zStateDb = recoverMPrintf(p, "%s", (char*)pArg); in sqlite3_recover_config()
17990 sqlite3_free(p->zLostAndFound); in sqlite3_recover_config()
17992 p->zLostAndFound = recoverMPrintf(p, "%s", zArg); in sqlite3_recover_config()
17994 p->zLostAndFound = 0; in sqlite3_recover_config()
18000 p->bFreelistCorrupt = *(int*)pArg; in sqlite3_recover_config()
18004 p->bRecoverRowid = *(int*)pArg; in sqlite3_recover_config()
18008 p->bSlowIndexes = *(int*)pArg; in sqlite3_recover_config()
18028 if( p->errCode==SQLITE_OK ) recoverStep(p); in sqlite3_recover_step()
18029 if( p->eState==RECOVER_STATE_DONE && p->errCode==SQLITE_OK ){ in sqlite3_recover_step()
18032 return p->errCode; in sqlite3_recover_step()
18060 if( p->bCloseTransaction && sqlite3_get_autocommit(p->dbIn)==0 ){ in sqlite3_recover_finish()
18061 rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0); in sqlite3_recover_finish()
18062 if( p->errCode==SQLITE_OK ) p->errCode = rc; in sqlite3_recover_finish()
18064 rc = p->errCode; in sqlite3_recover_finish()
18065 sqlite3_free(p->zErrMsg); in sqlite3_recover_finish()
18066 sqlite3_free(p->zStateDb); in sqlite3_recover_finish()
18067 sqlite3_free(p->zLostAndFound); in sqlite3_recover_finish()
18068 sqlite3_free(p->pPage1Cache); in sqlite3_recover_finish()
18141 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
18145 u8 bSafeModePersist; /* The long-term value of bSafeMode */
18170 sqlite3_int64 szMax; /* --maxsize argument to .open */
18187 sqlite3 *db; /* Connection pointer */
18199 char *zNonce; /* Nonce for temporary safe-mode escapes */
18225 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
18229 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
18243 ** top-level SQL statement */
18244 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
18249 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
18251 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
18253 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
18255 #define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */
18264 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
18265 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
18266 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
18278 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
18282 #define MODE_Pretty 11 /* Pretty-print schemas */
18286 #define MODE_Table 15 /* MySQL-style table formatting */
18287 #define MODE_Box 16 /* Unicode box-drawing characters */
18290 #define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */
18338 if( p->pLog==0 ) return; in shellLog()
18339 sputf(p->pLog, "(%d) %s\n", iErrCode, zMsg); in shellLog()
18340 fflush(p->pLog); in shellLog()
18369 if( p->bSafeMode ){ in failIfSafeMode()
18375 eputf("line %d: %s\n", p->lineno, zMsg); in failIfSafeMode()
18394 ** Also throw an error if the EDITOR program returns a non-zero exit code.
18420 sqlite3_result_error(context, "no editor for edit()", -1); in editFunc()
18424 sqlite3_result_error(context, "NULL input to edit()", -1); in editFunc()
18444 sqlite3_result_error(context, "edit() cannot open temp file", -1); in editFunc()
18459 sqlite3_result_error(context, "edit() could not write the whole file", -1); in editFunc()
18470 sqlite3_result_error(context, "EDITOR returned non-zero", -1); in editFunc()
18476 "edit() cannot reopen temp file after edit", -1); in editFunc()
18491 sqlite3_result_error(context, "could not read back the whole file", -1); in editFunc()
18528 p->modePrior = p->mode; in outputModePush()
18529 p->priorShFlgs = p->shellFlgs; in outputModePush()
18530 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); in outputModePush()
18531 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); in outputModePush()
18534 p->mode = p->modePrior; in outputModePop()
18535 p->shellFlgs = p->priorShFlgs; in outputModePop()
18536 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); in outputModePop()
18537 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); in outputModePop()
18541 ** Output the given string as a hex-encoded blob (eg. X'1234' )
18565 ** Find a string that is not found anywhere in z[]. Return a pointer
18631 ** get corrupted by end-of-line translation facilities in some operating
18704 ** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
18713 ns = pcFirst - s; in anyOfInStr()
18726 static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */ in output_c_string()
18734 if( pcEnd > z ) oputb(z, (int)(pcEnd-z)); in output_c_string()
18777 const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z); in output_json_string()
18778 const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask); in output_json_string()
18781 oputb(z, (int)(pcEnd-z)); in output_json_string()
18869 ** Output a single term of CSV. Actually, p->colSeparator is used for
18870 ** the separator, which may or may not be a comma. p->nullValue is
18876 oputf("%s",p->nullValue); in output_csv()
18885 if( i==0 || strstr(z, p->colSeparator)!=0 ){ in output_csv()
18895 oputz(p->colSeparator); in output_csv()
18900 ** This routine runs when the user presses Ctrl-C
18910 ** This routine runs for console events (e.g. Ctrl-C) on Win32
19013 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4); in shellAuth()
19024 ** If the schema statement in z[] contains a start-of-comment and if
19032 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){ in printSchemaLine()
19041 zNew[n-1] = 0; in printSchemaLine()
19072 if( z[i]=='-' && z[i+1]=='-' ) return 1; in wsToEol()
19086 if( p->autoEQPtest ){ in eqp_append()
19091 pNew->iEqpId = iEqpId; in eqp_append()
19092 pNew->iParentId = p2; in eqp_append()
19093 memcpy(pNew->zText, zText, nText+1); in eqp_append()
19094 pNew->pNext = 0; in eqp_append()
19095 if( p->sGraph.pLast ){ in eqp_append()
19096 p->sGraph.pLast->pNext = pNew; in eqp_append()
19098 p->sGraph.pRow = pNew; in eqp_append()
19100 p->sGraph.pLast = pNew; in eqp_append()
19105 ** in p->sGraph.
19109 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ in eqp_reset()
19110 pNext = pRow->pNext; in eqp_reset()
19113 memset(&p->sGraph, 0, sizeof(p->sGraph)); in eqp_reset()
19120 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; in eqp_next_row()
19121 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; in eqp_next_row()
19130 i64 n = strlen(p->sGraph.zPrefix); in eqp_render_level()
19134 z = pRow->zText; in eqp_render_level()
19135 oputf("%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z); in eqp_render_level()
19136 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){ in eqp_render_level()
19137 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); in eqp_render_level()
19138 eqp_render_level(p, pRow->iEqpId); in eqp_render_level()
19139 p->sGraph.zPrefix[n] = 0; in eqp_render_level()
19148 EQPGraphRow *pRow = p->sGraph.pRow; in eqp_render()
19150 if( pRow->zText[0]=='-' ){ in eqp_render()
19151 if( pRow->pNext==0 ){ in eqp_render()
19155 oputf("%s\n", pRow->zText+3); in eqp_render()
19156 p->sGraph.pRow = pRow->pNext; in eqp_render()
19163 p->sGraph.zPrefix[0] = 0; in eqp_render()
19175 p->nProgress++; in progress_handler()
19176 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ in progress_handler()
19177 oputf("Progress limit reached (%u)\n", p->nProgress); in progress_handler()
19178 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; in progress_handler()
19179 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; in progress_handler()
19182 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ in progress_handler()
19183 oputf("Progress %u\n", p->nProgress); in progress_handler()
19193 const char zDash[] = "--------------------------------------------------"; in print_dashes()
19194 const int nDash = sizeof(zDash) - 1; in print_dashes()
19197 N -= nDash; in print_dashes()
19203 ** Print a markdown or table-style row separator using ascii-art
19213 print_dashes(p->actualWidth[0]+2); in print_row_separator()
19216 print_dashes(p->actualWidth[i]+2); in print_row_separator()
19238 switch( p->cMode ){ in shell_callback()
19250 if( p->cnt++>0 ) oputz(p->rowSeparator); in shell_callback()
19253 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); in shell_callback()
19269 if( p->cMode==MODE_ScanExp ){ in shell_callback()
19278 if( p->cnt++==0 ){ in shell_callback()
19281 oputz(i==nArg-1 ? "\n" : " "); in shell_callback()
19285 oputz(i==nArg-1 ? "\n" : " "); in shell_callback()
19296 if( i==nArg-1 ) w = 0; in shell_callback()
19301 if( i==iIndent && p->aiIndent && p->pStmt ){ in shell_callback()
19302 if( p->iIndent<p->nIndent ){ in shell_callback()
19303 oputf("%*.s", p->aiIndent[p->iIndent], ""); in shell_callback()
19305 p->iIndent++; in shell_callback()
19307 utf8_width_print(w, zVal ? zVal : p->nullValue); in shell_callback()
19308 oputz(i==nArg-1 ? "\n" : zSep); in shell_callback()
19316 case MODE_Pretty: { /* .schema and .fullschema with --indent */ in shell_callback()
19337 if( z[j-1]=='\r' ) z[j-1] = '\n'; in shell_callback()
19338 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; in shell_callback()
19339 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ in shell_callback()
19340 j--; in shell_callback()
19344 while( j>0 && IsSpace(z[j-1]) ){ j--; } in shell_callback()
19354 }else if( c=='-' && z[i+1]=='-' ){ in shell_callback()
19359 nParen--; in shell_callback()
19369 if( c=='\n' ) j--; in shell_callback()
19383 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
19385 oputf("%s%s",azCol[i], i==nArg-1 ? p->rowSeparator : p->colSeparator); in shell_callback()
19391 if( z==0 ) z = p->nullValue; in shell_callback()
19393 oputz((i<nArg-1)? p->colSeparator : p->rowSeparator); in shell_callback()
19398 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
19411 output_html_string(azArg[i] ? azArg[i] : p->nullValue); in shell_callback()
19418 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
19421 if(i<nArg-1) oputz(p->colSeparator); in shell_callback()
19423 oputz(p->rowSeparator); in shell_callback()
19427 output_c_string(azArg[i] ? azArg[i] : p->nullValue); in shell_callback()
19428 if(i<nArg-1) oputz(p->colSeparator); in shell_callback()
19430 oputz(p->rowSeparator); in shell_callback()
19434 setBinaryMode(p->out, 1); in shell_callback()
19435 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
19437 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); in shell_callback()
19439 oputz(p->rowSeparator); in shell_callback()
19443 output_csv(p, azArg[i], i<nArg-1); in shell_callback()
19445 oputz(p->rowSeparator); in shell_callback()
19447 setTextMode(p->out, 1); in shell_callback()
19452 oputf("INSERT INTO %s",p->zDestTable); in shell_callback()
19453 if( p->showHeader ){ in shell_callback()
19468 p->cnt++; in shell_callback()
19483 double r = sqlite3_column_double(p->pStmt, i); in shell_callback()
19489 oputz("-9.0e+999"); in shell_callback()
19499 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ in shell_callback()
19500 const void *pBlob = sqlite3_column_blob(p->pStmt, i); in shell_callback()
19501 int nBlob = sqlite3_column_bytes(p->pStmt, i); in shell_callback()
19516 if( p->cnt==0 ){ in shell_callback()
19517 fputs("[{", p->out); in shell_callback()
19519 fputs(",\n{", p->out); in shell_callback()
19521 p->cnt++; in shell_callback()
19523 output_json_string(azCol[i], -1); in shell_callback()
19529 double r = sqlite3_column_double(p->pStmt, i); in shell_callback()
19535 oputz("-9.0e+999"); in shell_callback()
19540 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ in shell_callback()
19541 const void *pBlob = sqlite3_column_blob(p->pStmt, i); in shell_callback()
19542 int nBlob = sqlite3_column_bytes(p->pStmt, i); in shell_callback()
19545 output_json_string(azArg[i], -1); in shell_callback()
19549 if( i<nArg-1 ){ in shell_callback()
19558 if( p->cnt==0 && p->showHeader ){ in shell_callback()
19560 if( i>0 ) fputs(p->colSeparator, p->out); in shell_callback()
19563 fputs(p->rowSeparator, p->out); in shell_callback()
19565 p->cnt++; in shell_callback()
19567 if( i>0 ) fputs(p->colSeparator, p->out); in shell_callback()
19576 double r = sqlite3_column_double(p->pStmt, i); in shell_callback()
19579 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ in shell_callback()
19580 const void *pBlob = sqlite3_column_blob(p->pStmt, i); in shell_callback()
19581 int nBlob = sqlite3_column_bytes(p->pStmt, i); in shell_callback()
19589 fputs(p->rowSeparator, p->out); in shell_callback()
19593 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
19595 if( i>0 ) oputz(p->colSeparator); in shell_callback()
19598 oputz(p->rowSeparator); in shell_callback()
19602 if( i>0 ) oputz(p->colSeparator); in shell_callback()
19603 oputz(azArg[i] ? azArg[i] : p->nullValue); in shell_callback()
19605 oputz(p->rowSeparator); in shell_callback()
19634 if( p->n ) appendText(p, "|", 0); in captureOutputCallback()
19647 sqlite3_exec(p->db, in createSelftestTable()
19658 " 'memo','Tests generated by --init');\n" in createSelftestTable()
19687 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); in createSelftestTable()
19701 if( p->zDestTable ){ in set_table_name()
19702 free(p->zDestTable); in set_table_name()
19703 p->zDestTable = 0; in set_table_name()
19709 z = p->zDestTable = malloc( n+1 ); in set_table_name()
19723 ** syntax error. Return a pointer to the text, in memory obtained from
19744 iOffset--; in shell_error_context()
19746 while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; } in shell_error_context()
19751 while( len>0 && (zSql[len]&0xc0)==0x80 ) len--; in shell_error_context()
19757 zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode,iOffset,""); in shell_error_context()
19759 zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode,iOffset-14,""); in shell_error_context()
19767 ** the result columns, comma-separated, on a line and then add a
19770 ** If the number of columns is 1 and that column contains text "--"
19772 ** "--" comment occurs at the end of the statement, the comment
19784 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); in run_table_dump_query()
19786 char *zContext = shell_error_context(zSelect, p->db); in run_table_dump_query()
19788 rc, sqlite3_errmsg(p->db), zContext); in run_table_dump_query()
19790 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; in run_table_dump_query()
19802 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; in run_table_dump_query()
19812 oputf("/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db)); in run_table_dump_query()
19813 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; in run_table_dump_query()
19871 oputf("%-36s %s", aTrans[i].zDesc, &z[n]); in displayLinuxIoStats()
19881 ** Display a single line of status using 64-bit values.
19889 sqlite3_int64 iCur = -1; in displayStatLine()
19890 sqlite3_int64 iHiwtr = -1; in displayStatLine()
19902 oputf("%-36s %s\n", zLabel, zLine); in displayStatLine()
19910 ShellState *pArg, /* Pointer to ShellState */ in display_stats()
19915 if( pArg==0 || pArg->out==0 ) return 0; in display_stats()
19917 if( pArg->pStmt && pArg->statsOn==2 ){ in display_stats()
19919 sqlite3_stmt *pStmt = pArg->pStmt; in display_stats()
19922 oputf("%-36s %d\n", "Number of output columns:", nCol); in display_stats()
19925 oputf("%-36s %s\n", z, sqlite3_column_name(pStmt,i)); in display_stats()
19928 oputf("%-36s %s\n", z, sqlite3_column_decltype(pStmt, i)); in display_stats()
19932 oputf("%-36s %s\n", z, sqlite3_column_database_name(pStmt,i)); in display_stats()
19934 oputf("%-36s %s\n", z, sqlite3_column_table_name(pStmt,i)); in display_stats()
19936 oputf("%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i)); in display_stats()
19941 if( pArg->statsOn==3 ){ in display_stats()
19942 if( pArg->pStmt ){ in display_stats()
19943 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset); in display_stats()
19944 oputf("VM-steps: %d\n", iCur); in display_stats()
19953 if( pArg->shellFlgs & SHFLG_Pagecache ){ in display_stats()
19969 if( pArg->shellFlgs & SHFLG_Lookaside ){ in display_stats()
19970 iHiwtr = iCur = -1; in display_stats()
19984 iHiwtr = iCur = -1; in display_stats()
19987 iHiwtr = iCur = -1; in display_stats()
19990 iHiwtr = iCur = -1; in display_stats()
19993 iHiwtr = iCur = -1; in display_stats()
19996 iHiwtr = iCur = -1; in display_stats()
19999 iHiwtr = iCur = -1; in display_stats()
20002 iHiwtr = iCur = -1; in display_stats()
20007 if( pArg->pStmt ){ in display_stats()
20009 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, in display_stats()
20012 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); in display_stats()
20014 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); in display_stats()
20016 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, in display_stats()
20018 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, in display_stats()
20023 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); in display_stats()
20025 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); in display_stats()
20027 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); in display_stats()
20029 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); in display_stats()
20037 /* Do not remove this machine readable comment: extra-stats-output-here */ in display_stats()
20074 ShellState *pArg /* Pointer to ShellState */ in display_explain_scanstats()
20077 sqlite3_stmt *p = pArg->pStmt; in display_explain_scanstats()
20094 sqlite3_stmt_scanstatus_v2(p, -1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal); in display_explain_scanstats()
20132 if( zName && pArg->scanstatsOn>1 ){ in display_explain_scanstats()
20138 "% *z (%z)", -1*(nWidth-scanStatsHeight(p, ii)*3), zText, z in display_explain_scanstats()
20152 ** Parameter azArray points to a zero-terminated array of strings. zStr
20153 ** points to a single nul-terminated string. Return non-zero if zStr
20177 ** See tag-20220407a in wherecode.c and vdbe.c.
20188 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ in explain_data_prepare()
20189 int iOp; /* Index of operation in p->aiIndent[] */ in explain_data_prepare()
20216 ** different if the current instruction is part of a sub-program generated in explain_data_prepare()
20218 int p2op = (p2 + (iOp-iAddr)); in explain_data_prepare()
20220 /* Grow the p->aiIndent array as required */ in explain_data_prepare()
20223 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); in explain_data_prepare()
20224 shell_check_oom(p->aiIndent); in explain_data_prepare()
20230 p->aiIndent[iOp] = 0; in explain_data_prepare()
20231 p->nIndent = iOp+1; in explain_data_prepare()
20233 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; in explain_data_prepare()
20236 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; in explain_data_prepare()
20240 p->iIndent = 0; in explain_data_prepare()
20249 sqlite3_free(p->aiIndent); in explain_data_delete()
20250 p->aiIndent = 0; in explain_data_delete()
20251 p->nIndent = 0; in explain_data_delete()
20252 p->iIndent = 0; in explain_data_delete()
20262 ShellState *pArg /* Pointer to ShellState */ in display_scanstats()
20268 if( pArg->scanstatsOn==3 ){ in display_scanstats()
20276 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); in display_scanstats()
20278 sqlite3_stmt *pSave = pArg->pStmt; in display_scanstats()
20279 pArg->pStmt = pStmt; in display_scanstats()
20280 sqlite3_bind_pointer(pStmt, 1, pSave, "stmt-pointer", 0); in display_scanstats()
20282 pArg->cnt = 0; in display_scanstats()
20283 pArg->cMode = MODE_ScanExp; in display_scanstats()
20289 pArg->pStmt = pSave; in display_scanstats()
20318 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode); in bind_table_init()
20319 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); in bind_table_init()
20320 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); in bind_table_init()
20321 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); in bind_table_init()
20322 sqlite3_exec(p->db, in bind_table_init()
20328 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); in bind_table_init()
20329 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0); in bind_table_init()
20352 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", in bind_prepared_stmt()
20357 rc = sqlite3_prepare_v2(pArg->db, in bind_prepared_stmt()
20359 " WHERE key=?1", -1, &pQ, 0); in bind_prepared_stmt()
20368 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); in bind_prepared_stmt()
20388 ** UTF8 box-drawing characters. Imagine box lines like this:
20392 ** 4 --+-- 2
20400 #define BOX_24 "\342\224\200" /* U+2500 --- */
20402 #define BOX_23 "\342\224\214" /* U+250c ,- */
20403 #define BOX_34 "\342\224\220" /* U+2510 -, */
20404 #define BOX_12 "\342\224\224" /* U+2514 '- */
20405 #define BOX_14 "\342\224\230" /* U+2518 -' */
20406 #define BOX_123 "\342\224\234" /* U+251c |- */
20407 #define BOX_134 "\342\224\244" /* U+2524 -| */
20408 #define BOX_234 "\342\224\254" /* U+252c -,- */
20409 #define BOX_124 "\342\224\264" /* U+2534 -'- */
20410 #define BOX_1234 "\342\224\274" /* U+253c -|- */
20419 const int nDash = sizeof(zDash) - 1; in print_box_line()
20423 N -= nDash; in print_box_line()
20441 print_box_line(p->actualWidth[0]+2); in print_box_row_separator()
20444 print_box_line(p->actualWidth[i]+2); in print_box_row_separator()
20466 u8 bWordWrap /* If true, avoid breaking mid-word */ in translateForDisplayAndDup()
20478 if( mxWidth<0 ) mxWidth = -mxWidth; in translateForDisplayAndDup()
20499 for(k=i; k>i/2; k--){ in translateForDisplayAndDup()
20500 if( isspace(z[k-1]) ) break; in translateForDisplayAndDup()
20503 for(k=i; k>i/2; k--){ in translateForDisplayAndDup()
20504 if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break; in translateForDisplayAndDup()
20548 /* Extract the value of the i-th current column for pStmt as an SQL literal
20582 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
20591 ShellState *p, /* Pointer to ShellState */ in exec_prepared_stmt_columnar()
20610 int bw = p->cmOpts.bWordWrap; in exec_prepared_stmt_columnar()
20612 const char *zShowNull = p->nullValue; in exec_prepared_stmt_columnar()
20624 if( p->cmOpts.bQuote ){ in exec_prepared_stmt_columnar()
20631 if( nColumn>p->nWidth ){ in exec_prepared_stmt_columnar()
20632 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int)); in exec_prepared_stmt_columnar()
20633 shell_check_oom(p->colWidth); in exec_prepared_stmt_columnar()
20634 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0; in exec_prepared_stmt_columnar()
20635 p->nWidth = nColumn; in exec_prepared_stmt_columnar()
20636 p->actualWidth = &p->colWidth[nColumn]; in exec_prepared_stmt_columnar()
20638 memset(p->actualWidth, 0, nColumn*sizeof(int)); in exec_prepared_stmt_columnar()
20640 w = p->colWidth[i]; in exec_prepared_stmt_columnar()
20641 if( w<0 ) w = -w; in exec_prepared_stmt_columnar()
20642 p->actualWidth[i] = w; in exec_prepared_stmt_columnar()
20646 int wx = p->colWidth[i]; in exec_prepared_stmt_columnar()
20648 wx = p->cmOpts.iWrap; in exec_prepared_stmt_columnar()
20650 if( wx<0 ) wx = -wx; in exec_prepared_stmt_columnar()
20668 int wx = p->colWidth[i]; in exec_prepared_stmt_columnar()
20670 wx = p->cmOpts.iWrap; in exec_prepared_stmt_columnar()
20672 if( wx<0 ) wx = -wx; in exec_prepared_stmt_columnar()
20676 }else if( p->cmOpts.bQuote ){ in exec_prepared_stmt_columnar()
20688 abRowDiv[nRow-1] = 0; in exec_prepared_stmt_columnar()
20699 if( n>p->actualWidth[j] ) p->actualWidth[j] = n; in exec_prepared_stmt_columnar()
20703 switch( p->cMode ){ in exec_prepared_stmt_columnar()
20707 if( p->showHeader ){ in exec_prepared_stmt_columnar()
20709 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
20710 if( p->colWidth[i]<0 ) w = -w; in exec_prepared_stmt_columnar()
20712 fputs(i==nColumn-1?"\n":" ", p->out); in exec_prepared_stmt_columnar()
20715 print_dashes(p->actualWidth[i]); in exec_prepared_stmt_columnar()
20716 fputs(i==nColumn-1?"\n":" ", p->out); in exec_prepared_stmt_columnar()
20725 fputs("| ", p->out); in exec_prepared_stmt_columnar()
20727 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
20729 oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); in exec_prepared_stmt_columnar()
20730 oputz(i==nColumn-1?" |\n":" | "); in exec_prepared_stmt_columnar()
20738 fputs("| ", p->out); in exec_prepared_stmt_columnar()
20740 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
20742 oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); in exec_prepared_stmt_columnar()
20743 oputz(i==nColumn-1?" |\n":" | "); in exec_prepared_stmt_columnar()
20754 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
20757 (w-n)/2, "", azData[i], (w-n+1)/2, "", in exec_prepared_stmt_columnar()
20758 i==nColumn-1?" "BOX_13"\n":" "BOX_13" "); in exec_prepared_stmt_columnar()
20765 if( j==0 && p->cMode!=MODE_Column ){ in exec_prepared_stmt_columnar()
20766 oputz(p->cMode==MODE_Box?BOX_13" ":"| "); in exec_prepared_stmt_columnar()
20769 if( z==0 ) z = p->nullValue; in exec_prepared_stmt_columnar()
20770 w = p->actualWidth[j]; in exec_prepared_stmt_columnar()
20771 if( p->colWidth[j]<0 ) w = -w; in exec_prepared_stmt_columnar()
20773 if( j==nColumn-1 ){ in exec_prepared_stmt_columnar()
20775 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){ in exec_prepared_stmt_columnar()
20776 if( p->cMode==MODE_Table ){ in exec_prepared_stmt_columnar()
20778 }else if( p->cMode==MODE_Box ){ in exec_prepared_stmt_columnar()
20780 }else if( p->cMode==MODE_Column ){ in exec_prepared_stmt_columnar()
20784 j = -1; in exec_prepared_stmt_columnar()
20790 if( p->cMode==MODE_Table ){ in exec_prepared_stmt_columnar()
20792 }else if( p->cMode==MODE_Box ){ in exec_prepared_stmt_columnar()
20817 ShellState *pArg, /* Pointer to ShellState */ in exec_prepared_stmt()
20823 if( pArg->cMode==MODE_Column in exec_prepared_stmt()
20824 || pArg->cMode==MODE_Table in exec_prepared_stmt()
20825 || pArg->cMode==MODE_Box in exec_prepared_stmt()
20826 || pArg->cMode==MODE_Markdown in exec_prepared_stmt()
20860 && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote) in exec_prepared_stmt()
20883 if( pArg->cMode==MODE_Json ){ in exec_prepared_stmt()
20884 fputs("]\n", pArg->out); in exec_prepared_stmt()
20885 }else if( pArg->cMode==MODE_Count ){ in exec_prepared_stmt()
20911 assert( pState->expert.pExpert ); in expertHandleSQL()
20913 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); in expertHandleSQL()
20932 sqlite3expert *p = pState->expert.pExpert; in expertFinish()
20936 int bVerbose = pState->expert.bVerbose; in expertFinish()
20945 oputz("-- Candidates -----------------------------\n"); in expertFinish()
20954 oputf("-- Query %d --------------------------------\n",i+1); in expertFinish()
20963 pState->expert.pExpert = 0; in expertFinish()
20980 assert( pState->expert.pExpert==0 ); in expertDotCommand()
20981 memset(&pState->expert, 0, sizeof(ExpertInfo)); in expertDotCommand()
20986 if( z[0]=='-' && z[1]=='-' ) z++; in expertDotCommand()
20988 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){ in expertDotCommand()
20989 pState->expert.bVerbose = 1; in expertDotCommand()
20991 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){ in expertDotCommand()
20992 if( i==(nArg-1) ){ in expertDotCommand()
21010 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); in expertDotCommand()
21011 if( pState->expert.pExpert==0 ){ in expertDotCommand()
21016 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample in expertDotCommand()
21031 ** This is very similar to SQLite's built-in sqlite3_exec()
21036 ShellState *pArg, /* Pointer to ShellState */ in shell_exec()
21044 sqlite3 *db = pArg->db; in shell_exec()
21051 if( pArg->expert.pExpert ){ in shell_exec()
21059 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); in shell_exec()
21066 /* this happens for a comment or white-space */ in shell_exec()
21077 pArg->pStmt = pStmt; in shell_exec()
21078 pArg->cnt = 0; in shell_exec()
21082 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){ in shell_exec()
21086 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); in shell_exec()
21087 if( pArg->autoEQP>=AUTOEQP_trigger ){ in shell_exec()
21099 if( zEQPLine[0]=='-' ) eqp_render(pArg, 0); in shell_exec()
21104 if( pArg->autoEQP>=AUTOEQP_full ){ in shell_exec()
21109 pArg->cMode = MODE_Explain; in shell_exec()
21116 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){ in shell_exec()
21126 pArg->cMode = pArg->mode; in shell_exec()
21127 if( pArg->autoExplain ){ in shell_exec()
21129 pArg->cMode = MODE_Explain; in shell_exec()
21132 pArg->cMode = MODE_EQP; in shell_exec()
21138 if( pArg->cMode==MODE_Explain && bIsExplain ){ in shell_exec()
21149 if( pArg && pArg->statsOn ){ in shell_exec()
21153 /* print loop-counters if required */ in shell_exec()
21154 if( pArg && pArg->scanstatsOn ){ in shell_exec()
21172 pArg->pStmt = NULL; in shell_exec()
21218 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in tableColumnList()
21222 if( nCol>=nAlloc-2 ){ in tableColumnList()
21263 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in tableColumnList()
21287 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); in tableColumnList()
21303 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); in toggleSelectOrder()
21335 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0; in dump_callback()
21336 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0; in dump_callback()
21345 /* no-op */ in dump_callback()
21348 if( !p->writableSchema ){ in dump_callback()
21350 p->writableSchema = 1; in dump_callback()
21374 p->nErr++; in dump_callback()
21413 savedDestTable = p->zDestTable; in dump_callback()
21414 savedMode = p->mode; in dump_callback()
21415 p->zDestTable = sTable.z; in dump_callback()
21416 p->mode = p->cMode = MODE_Insert; in dump_callback()
21420 toggleSelectOrder(p->db); in dump_callback()
21422 toggleSelectOrder(p->db); in dump_callback()
21424 p->zDestTable = savedDestTable; in dump_callback()
21425 p->mode = savedMode; in dump_callback()
21428 if( rc ) p->nErr++; in dump_callback()
21446 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); in run_schema_dump_query()
21459 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); in run_schema_dump_query()
21485 " -c, --create Create a new archive",
21486 " -u, --update Add or update files with changed mtime",
21487 " -i, --insert Like -u but always add even if unchanged",
21488 " -r, --remove Remove files from archive",
21489 " -t, --list List contents of archive",
21490 " -x, --extract Extract files from archive",
21492 " -v, --verbose Print each filename as it is processed",
21493 " -f FILE, --file FILE Use archive FILE (default is current db)",
21494 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
21495 " -C DIR, --directory DIR Read/extract files from directory DIR",
21496 " -g, --glob Use glob matching for names in archive",
21497 " -n, --dryrun Show the SQL that would have occurred",
21499 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
21500 " .ar -tf ARCHIVE # List members of ARCHIVE",
21501 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
21511 " --append Use the appendvfs",
21512 " --async Write to FILE without journal and fsync()",
21534 " --data-only Output only INSERT statements",
21535 " --newlines Allow unescaped newline characters in output",
21536 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
21537 " --preserve-rowids Include ROWID values in the output",
21550 " --bom Put a UTF8 byte-order mark on intermediate file",
21553 ".exit ?CODE? Exit this program with return-code CODE",
21558 " --schema SCHEMA Use SCHEMA instead of \"main\"",
21559 " --help Show CMD details",
21560 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
21562 ".help ?-all? ?PATTERN? Show help text for PATTERN",
21566 " --ascii Use \\037 and \\036 as column and row separators",
21567 " --csv Use , and \\n as column and row separators",
21568 " --skip N Skip the first N rows of input",
21569 " --schema S Target table to be S.TABLE",
21570 " -v \"Verbose\" - increase auxiliary output",
21574 " * If neither --csv or --ascii are used, the input mode is derived",
21591 " fkey-indexes Find missing foreign key indexes",
21603 " box Tables using unicode box-drawing characters",
21604 " csv Comma-separated values",
21612 " qbox Shorthand for \"box --wrap 60 --quote\"",
21614 " table ASCII-art table",
21615 " tabs Tab-separated values",
21618 " --wrap N Wrap output lines to no longer than N characters",
21619 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
21620 " --ww Shorthand for \"--wordwrap 1\"",
21621 " --quote Quote output text as SQL literals",
21622 " --noquote Do not quote output text",
21631 " --bom Put a UTF8 byte-order mark at the beginning",
21632 " -e Send output to the system text editor",
21633 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
21639 " --append Use appendvfs to append database to the end of FILE",
21642 " --deserialize Load into memory using sqlite3_deserialize()",
21643 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
21644 " --maxsize N Maximum size for --hexdb or --deserialized database",
21646 " --new Initialize FILE to an empty database",
21647 " --nofollow Do not follow symbolic links",
21648 " --readonly Open FILE readonly",
21649 " --zip FILE is a ZIP archive",
21654 " --bom Prefix output with a UTF8 byte-order mark",
21655 " -e Send output to the system text editor",
21656 " -x Send output as CSV to a spreadsheet",
21668 " --limit N Interrupt after N progress callbacks",
21669 " --once Do no more than one progress interrupt",
21670 " --quiet|-q No output except at interrupts",
21671 " --reset Reset the count for each input and interrupt",
21681 " --ignore-freelist Ignore pages that appear to be on db freelist",
21682 " --lost-and-found TABLE Alternative name for the lost-and-found table",
21683 " --no-rowids Do not attempt to recover rowid values",
21693 " --indent Try to pretty-print the schema",
21694 " --nosys Omit objects whose names start with \"sqlite_\"",
21697 " --init Create a new SELFTEST table",
21698 " -v Verbose output",
21717 " --schema Also hash the sqlite_schema table",
21718 " --sha3-224 Use the sha3-224 algorithm",
21719 " --sha3-256 Use the sha3-256 algorithm (default)",
21720 " --sha3-384 Use the sha3-384 algorithm",
21721 " --sha3-512 Use the sha3-512 algorithm",
21737 ",testcase NAME Begin redirecting output to 'testcase-out.txt'",
21749 " --expanded Expand query parameters",
21751 " --normalized Normal the SQL statements",
21753 " --plain Show SQL as it is input",
21754 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
21755 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
21756 " --row Trace each row (SQLITE_TRACE_ROW)",
21757 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
21761 " --allexcept Unregister everything except those named",
21764 ".vfsinfo ?AUX? Information about the top-level VFS",
21768 " Negative values right-justify",
21775 ** If zPattern is NULL, then show all commands, but only give a one-line
21787 || cli_strcmp(zPattern,"-a")==0 in showHelp()
21788 || cli_strcmp(zPattern,"-all")==0 in showHelp()
21789 || cli_strcmp(zPattern,"--all")==0 in showHelp()
21840 while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){ in showHelp()
21853 while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i; in showHelp()
21859 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){ in showHelp()
21877 ** and return a pointer to the buffer. The caller is responsible for freeing
21883 ** For convenience, a nul-terminator byte is always appended to the data read
21930 sqlite3session_delete(pSession->p); in session_close()
21931 sqlite3_free(pSession->zName); in session_close()
21932 for(i=0; i<pSession->nFilter; i++){ in session_close()
21933 sqlite3_free(pSession->azFilter[i]); in session_close()
21935 sqlite3_free(pSession->azFilter); in session_close()
21946 struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i]; in session_close_all()
21947 for(j=0; j<pAuxDb->nSession; j++){ in session_close_all()
21948 session_close(&pAuxDb->aSession[j]); in session_close_all()
21950 pAuxDb->nSession = 0; in session_close_all()
21964 for(i=0; i<pSession->nFilter; i++){ in session_filter()
21965 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; in session_filter()
21997 fseek(f, -25, SEEK_END); in deduceDatabaseType()
21999 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ in deduceDatabaseType()
22002 fseek(f, -22, SEEK_END); in deduceDatabaseType()
22017 ** Reconstruct an in-memory database using the output from the "dbtotxt"
22018 ** program. Read content from the file in p->aAuxDb[].zDbFilename.
22019 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
22030 const char *zDbFilename = p->pAuxDb->zDbFilename; in readHexDb()
22041 in = p->in; in readHexDb()
22042 nLine = p->lineno; in readHexDb()
22051 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error; in readHexDb()
22052 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ in readHexDb()
22056 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){ in readHexDb()
22081 if( in!=p->in ){ in readHexDb()
22084 p->lineno = nLine; in readHexDb()
22089 if( in!=p->in ){ in readHexDb()
22092 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){ in readHexDb()
22096 p->lineno = nLine; in readHexDb()
22099 eputf("Error on line %d of --hexdb input\n", nLine); in readHexDb()
22136 if( p->db==0 ){ in open_db()
22137 const char *zDbFilename = p->pAuxDb->zDbFilename; in open_db()
22138 if( p->openMode==SHELL_OPEN_UNSPEC ){ in open_db()
22140 p->openMode = SHELL_OPEN_NORMAL; in open_db()
22142 p->openMode = (u8)deduceDatabaseType(zDbFilename, in open_db()
22146 switch( p->openMode ){ in open_db()
22148 sqlite3_open_v2(zDbFilename, &p->db, in open_db()
22149 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs"); in open_db()
22154 sqlite3_open(0, &p->db); in open_db()
22158 sqlite3_open(":memory:", &p->db); in open_db()
22162 sqlite3_open_v2(zDbFilename, &p->db, in open_db()
22163 SQLITE_OPEN_READONLY|p->openFlags, 0); in open_db()
22168 sqlite3_open_v2(zDbFilename, &p->db, in open_db()
22169 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0); in open_db()
22173 globalDb = p->db; in open_db()
22174 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ in open_db()
22176 zDbFilename, sqlite3_errmsg(p->db)); in open_db()
22180 sqlite3_close(p->db); in open_db()
22181 sqlite3_open(":memory:", &p->db); in open_db()
22182 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ in open_db()
22183 eputz("Also: unable to open substitute in-memory database.\n"); in open_db()
22186 eputf("Notice: using substitute in-memory database instead of \"%s\"\n", in open_db()
22190 sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0); in open_db()
22192 /* Reflect the use or absence of --unsafe-testing invocation. */ in open_db()
22195 sqlite3_db_config(p->db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, testmode_on,0); in open_db()
22196 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, !testmode_on,0); in open_db()
22200 sqlite3_enable_load_extension(p->db, 1); in open_db()
22202 sqlite3_shathree_init(p->db, 0, 0); in open_db()
22203 sqlite3_uint_init(p->db, 0, 0); in open_db()
22204 sqlite3_decimal_init(p->db, 0, 0); in open_db()
22205 sqlite3_base64_init(p->db, 0, 0); in open_db()
22206 sqlite3_base85_init(p->db, 0, 0); in open_db()
22207 sqlite3_regexp_init(p->db, 0, 0); in open_db()
22208 sqlite3_ieee_init(p->db, 0, 0); in open_db()
22209 sqlite3_series_init(p->db, 0, 0); in open_db()
22211 sqlite3_fileio_init(p->db, 0, 0); in open_db()
22212 sqlite3_completion_init(p->db, 0, 0); in open_db()
22215 if( !p->bSafeModePersist ){ in open_db()
22216 sqlite3_zipfile_init(p->db, 0, 0); in open_db()
22217 sqlite3_sqlar_init(p->db, 0, 0); in open_db()
22223 * This is a short-span macro. See further below for usage. in open_db()
22227 /* Let custom-included extensions get their ..._init() called. in open_db()
22233 int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db); in open_db()
22234 /* Let custom-included extensions expose their functionality. in open_db()
22241 SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0); in open_db()
22248 sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0, in open_db()
22250 sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0, in open_db()
22252 sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0, in open_db()
22254 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, in open_db()
22256 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, in open_db()
22258 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, in open_db()
22260 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0, in open_db()
22263 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, in open_db()
22265 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, in open_db()
22269 if( p->openMode==SHELL_OPEN_ZIPFILE ){ in open_db()
22273 sqlite3_exec(p->db, zSql, 0, 0, 0); in open_db()
22278 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){ in open_db()
22282 if( p->openMode==SHELL_OPEN_DESERIALIZE ){ in open_db()
22290 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData, in open_db()
22296 if( p->szMax>0 ){ in open_db()
22297 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax); in open_db()
22302 if( p->db!=0 ){ in open_db()
22303 if( p->bSafeModePersist ){ in open_db()
22304 sqlite3_set_authorizer(p->db, safeModeAuth, p); in open_db()
22307 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0 in open_db()
22335 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); in readline_completion_generator()
22366 if( nLine>(i64)sizeof(zBuf)-30 ) return; in linenoise_completion()
22368 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} in linenoise_completion()
22369 if( i==nLine-1 ) return; in linenoise_completion()
22376 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); in linenoise_completion()
22382 if( iStart+nCompletion < (i64)sizeof(zBuf)-1 && zCompletion ){ in linenoise_completion()
22392 ** Do C-language style dequoting.
22394 ** \a -> alarm
22395 ** \b -> backspace
22396 ** \t -> tab
22397 ** \n -> newline
22398 ** \v -> vertical tab
22399 ** \f -> form feed
22400 ** \r -> carriage return
22401 ** \s -> space
22402 ** \" -> "
22403 ** \' -> '
22404 ** \\ -> backslash
22405 ** \NNN -> ascii character NNN in octal
22406 ** \xHH -> ascii character HH in hexadecimal
22445 c -= '0'; in resolve_backslashes()
22448 c = (c<<3) + z[i] - '0'; in resolve_backslashes()
22451 c = (c<<3) + z[i] - '0'; in resolve_backslashes()
22529 void *pArg, /* The ShellState pointer */ in sql_trace_callback()
22530 void *pP, /* Usually a pointer to sqlite_stmt */ in sql_trace_callback()
22537 if( p->traceOut==0 ) return 0; in sql_trace_callback()
22539 sputz(p->traceOut, "-- closing database connection\n"); in sql_trace_callback()
22542 if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){ in sql_trace_callback()
22546 switch( p->eTraceType ){ in sql_trace_callback()
22566 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; } in sql_trace_callback()
22570 sputf(p->traceOut, "%.*s;\n", (int)nSql, zSql); in sql_trace_callback()
22575 sputf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec); in sql_trace_callback()
22584 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
22617 if( p->in!=0 && p->xCloser!=0 ){ in import_cleanup()
22618 p->xCloser(p->in); in import_cleanup()
22619 p->in = 0; in import_cleanup()
22621 sqlite3_free(p->z); in import_cleanup()
22622 p->z = 0; in import_cleanup()
22627 if( p->n+1>=p->nAlloc ){ in import_append_char()
22628 p->nAlloc += p->nAlloc + 100; in import_append_char()
22629 p->z = sqlite3_realloc64(p->z, p->nAlloc); in import_append_char()
22630 shell_check_oom(p->z); in import_append_char()
22632 p->z[p->n++] = (char)c; in import_append_char()
22638 ** + Input comes from p->in.
22639 ** + Store results in p->z of length p->n. Space to hold p->z comes
22641 ** + Use p->cSep as the column separator. The default is ",".
22642 ** + Use p->rSep as the row separator. The default is "\n".
22643 ** + Keep track of the line number in p->nLine.
22644 ** + Store the character that terminates the field in p->cTerm. Store
22645 ** EOF on end-of-file.
22650 int cSep = (u8)p->cColSep; in csv_read_one_field()
22651 int rSep = (u8)p->cRowSep; in csv_read_one_field()
22652 p->n = 0; in csv_read_one_field()
22653 c = fgetc(p->in); in csv_read_one_field()
22655 p->cTerm = EOF; in csv_read_one_field()
22660 int startLine = p->nLine; in csv_read_one_field()
22664 c = fgetc(p->in); in csv_read_one_field()
22665 if( c==rSep ) p->nLine++; in csv_read_one_field()
22677 do{ p->n--; }while( p->z[p->n]!=cQuote ); in csv_read_one_field()
22678 p->cTerm = c; in csv_read_one_field()
22682 eputf("%s:%d: unescaped %c character\n", p->zFile, p->nLine, cQuote); in csv_read_one_field()
22685 eputf("%s:%d: unterminated %c-quoted field\n", in csv_read_one_field()
22686 p->zFile, startLine, cQuote); in csv_read_one_field()
22687 p->cTerm = c; in csv_read_one_field()
22696 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ in csv_read_one_field()
22697 if( (c&0xff)==0xef && p->bNotFirst==0 ){ in csv_read_one_field()
22699 c = fgetc(p->in); in csv_read_one_field()
22702 c = fgetc(p->in); in csv_read_one_field()
22704 p->bNotFirst = 1; in csv_read_one_field()
22705 p->n = 0; in csv_read_one_field()
22712 c = fgetc(p->in); in csv_read_one_field()
22715 p->nLine++; in csv_read_one_field()
22716 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; in csv_read_one_field()
22718 p->cTerm = c; in csv_read_one_field()
22720 if( p->z ) p->z[p->n] = 0; in csv_read_one_field()
22721 p->bNotFirst = 1; in csv_read_one_field()
22722 return p->z; in csv_read_one_field()
22727 ** + Input comes from p->in.
22728 ** + Store results in p->z of length p->n. Space to hold p->z comes
22730 ** + Use p->cSep as the column separator. The default is "\x1F".
22731 ** + Use p->rSep as the row separator. The default is "\x1E".
22732 ** + Keep track of the row number in p->nLine.
22733 ** + Store the character that terminates the field in p->cTerm. Store
22734 ** EOF on end-of-file.
22739 int cSep = (u8)p->cColSep; in ascii_read_one_field()
22740 int rSep = (u8)p->cRowSep; in ascii_read_one_field()
22741 p->n = 0; in ascii_read_one_field()
22742 c = fgetc(p->in); in ascii_read_one_field()
22744 p->cTerm = EOF; in ascii_read_one_field()
22749 c = fgetc(p->in); in ascii_read_one_field()
22752 p->nLine++; in ascii_read_one_field()
22754 p->cTerm = c; in ascii_read_one_field()
22755 if( p->z ) p->z[p->n] = 0; in ascii_read_one_field()
22756 return p->z; in ascii_read_one_field()
22782 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneData()
22785 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery); in tryToCloneData()
22799 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); in tryToCloneData()
22824 -1, SQLITE_STATIC); in tryToCloneData()
22843 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); in tryToCloneData()
22853 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneData()
22890 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneSchema()
22892 eputf("Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db), in tryToCloneSchema()
22893 sqlite3_errmsg(p->db), zQuery); in tryToCloneSchema()
22920 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneSchema()
22923 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery); in tryToCloneSchema()
22965 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); in tryToClone()
22970 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); in tryToClone()
22980 if( p->out != stdout ) eputz("Output already redirected.\n"); in output_redir()
22982 p->out = pfNew; in output_redir()
22990 ** If the p->doXdgOpen flag is set, that means the output was being
22991 ** redirected to a temporary file named by p->zTempFile. In that case,
22992 ** launch start/open/xdg-open on that temporary file.
22995 if( p->outfile[0]=='|' ){ in output_reset()
22997 pclose(p->out); in output_reset()
23000 output_file_close(p->out); in output_reset()
23002 if( p->doXdgOpen ){ in output_reset()
23009 "xdg-open"; in output_reset()
23012 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); in output_reset()
23016 /* Give the start/open/xdg-open command some time to get in output_reset()
23018 ** p->zTempFile data file out from under it */ in output_reset()
23023 p->doXdgOpen = 0; in output_reset()
23027 p->outfile[0] = 0; in output_reset()
23028 p->out = stdout; in output_reset()
23042 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); in db_int()
23052 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
23100 if( p->db==0 ) return 1; in shell_dbinfo_command()
23101 rc = sqlite3_prepare_v2(p->db, in shell_dbinfo_command()
23103 -1, &pStmt, 0); in shell_dbinfo_command()
23105 eputf("error: %s\n", sqlite3_errmsg(p->db)); in shell_dbinfo_command()
23109 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); in shell_dbinfo_command()
23124 oputf("%-20s %d\n", "database page size:", i); in shell_dbinfo_command()
23125 oputf("%-20s %d\n", "write format:", aHdr[18]); in shell_dbinfo_command()
23126 oputf("%-20s %d\n", "read format:", aHdr[19]); in shell_dbinfo_command()
23127 oputf("%-20s %d\n", "reserved bytes:", aHdr[20]); in shell_dbinfo_command()
23131 oputf("%-20s %u", aField[i].zName, val); in shell_dbinfo_command()
23150 int val = db_int(p->db, zSql); in shell_dbinfo_command()
23152 oputf("%-20s %d\n", aQuery[i].zName, val); in shell_dbinfo_command()
23155 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); in shell_dbinfo_command()
23156 oputf("%-20s %u\n", "data version", iDataVersion); in shell_dbinfo_command()
23186 ** optional + or - sign in front
23210 while( *z && testcase_glob(zGlob-1,z)==0 ){ in testcase_glob()
23241 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ in testcase_glob()
23255 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; in testcase_glob()
23269 ** Compare the string as a command-line option with either one or two
23270 ** initial "-" characters.
23273 if( zStr[0]!='-' ) return 0; in optionMatch()
23275 if( zStr[0]=='-' ) zStr++; in optionMatch()
23299 if( p->zTempFile==0 ) return; in clearTempFile()
23300 if( p->doXdgOpen ) return; in clearTempFile()
23301 if( shellDeleteFile(p->zTempFile) ) return; in clearTempFile()
23302 sqlite3_free(p->zTempFile); in clearTempFile()
23303 p->zTempFile = 0; in clearTempFile()
23311 sqlite3_free(p->zTempFile); in newTempFile()
23312 p->zTempFile = 0; in newTempFile()
23313 if( p->db ){ in newTempFile()
23314 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); in newTempFile()
23316 if( p->zTempFile==0 ){ in newTempFile()
23317 /* If p->db is an in-memory database then the TEMPFILENAME file-control in newTempFile()
23331 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix); in newTempFile()
23333 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); in newTempFile()
23335 shell_check_oom(p->zTempFile); in newTempFile()
23341 ** by the ".lint fkey-indexes" command. This scalar function is always
23342 ** called with four arguments - the parent table name, the parent column name,
23345 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
23351 ** function returns the string " COLLATE <parent-collation>", where
23352 ** <parent-collation> is the default collation sequence of the parent column.
23365 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ in shellFkeyCollateClause()
23374 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); in shellFkeyCollateClause()
23386 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); in shellFkeyCollateClause()
23393 ** The implementation of dot-command ".lint fkey-indexes".
23400 sqlite3 *db = pState->db; /* Database handle to query "main" db of */ in lintFkeyIndexes()
23401 int bVerbose = 0; /* If -verbose is present */ in lintFkeyIndexes()
23402 int bGroupByParent = 0; /* If -groupbyparent is present */ in lintFkeyIndexes()
23466 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " in lintFkeyIndexes()
23474 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ in lintFkeyIndexes()
23477 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ in lintFkeyIndexes()
23482 eputf("Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]); in lintFkeyIndexes()
23494 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); in lintFkeyIndexes()
23504 int res = -1; in lintFkeyIndexes()
23515 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); in lintFkeyIndexes()
23533 oputf("-- Parent table %s\n", zParent); in lintFkeyIndexes()
23539 oputf("%s%s --> %s\n", zIndent, zCI, zTarget); in lintFkeyIndexes()
23541 oputf("%s/* no extra indexes required for %s -> %s */\n", in lintFkeyIndexes()
23575 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; in lintDotCommand()
23579 eputf("Usage %s sub-command ?switches...?\n", azArg[0]); in lintDotCommand()
23580 eputz("Where sub-commands are:\n"); in lintDotCommand()
23581 eputz(" fkey-indexes\n"); in lintDotCommand()
23594 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); in shellPrepare()
23603 ** Create a prepared statement using printf-style arguments for the SQL.
23606 ** depending on compile-time options. By omitting the "static", we avoid
23635 ** depending on compile-time options. By omitting the "static", we avoid
23657 ** depending on compile-time options. By omitting the "static", we avoid
23685 u8 bVerbose; /* True if --verbose */
23687 u8 bDryRun; /* True if --dry-run */
23688 u8 bAppend; /* True if --append */
23689 u8 bGlob; /* True if --glob */
23690 u8 fromCmdLine; /* Run from -A instead of .archive */
23693 const char *zFile; /* --file argument, or NULL */
23694 const char *zDir; /* --directory argument, or NULL */
23719 if( pAr->fromCmdLine ){ in arErrorMsg()
23720 eputz("Use \"-A\" for more help\n"); in arErrorMsg()
23722 eputz("Use \".archive --help\" for more help\n"); in arErrorMsg()
23740 ** Other (non-command) switches.
23758 if( pAr->eCmd ){ in arProcessSwitch()
23761 pAr->eCmd = eSwitch; in arProcessSwitch()
23765 pAr->bDryRun = 1; in arProcessSwitch()
23768 pAr->bGlob = 1; in arProcessSwitch()
23771 pAr->bVerbose = 1; in arProcessSwitch()
23774 pAr->bAppend = 1; in arProcessSwitch()
23777 pAr->zFile = zArg; in arProcessSwitch()
23780 pAr->zDir = zArg; in arProcessSwitch()
23826 if( z[0]!='-' ){ in arParseCommand()
23834 if( z[i]==pOpt->cShort ) break; in arParseCommand()
23839 if( pOpt->bArg ){ in arParseCommand()
23845 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; in arParseCommand()
23847 pAr->nArg = nArg-iArg; in arParseCommand()
23848 if( pAr->nArg>0 ){ in arParseCommand()
23849 pAr->azArg = &azArg[iArg]; in arParseCommand()
23852 /* Non-traditional invocation */ in arParseCommand()
23857 if( z[0]!='-' ){ in arParseCommand()
23859 pAr->azArg = &azArg[iArg]; in arParseCommand()
23860 pAr->nArg = nArg-iArg; in arParseCommand()
23865 if( z[1]!='-' ){ in arParseCommand()
23872 if( z[i]==pOpt->cShort ) break; in arParseCommand()
23877 if( pOpt->bArg ){ in arParseCommand()
23878 if( i<(n-1) ){ in arParseCommand()
23882 if( iArg>=(nArg-1) ){ in arParseCommand()
23889 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; in arParseCommand()
23892 /* A -- option, indicating that all remaining command line words in arParseCommand()
23894 pAr->azArg = &azArg[iArg+1]; in arParseCommand()
23895 pAr->nArg = nArg-iArg-1; in arParseCommand()
23903 const char *zLong = pOpt->zLong; in arParseCommand()
23904 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ in arParseCommand()
23916 if( pMatch->bArg ){ in arParseCommand()
23917 if( iArg>=(nArg-1) ){ in arParseCommand()
23922 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; in arParseCommand()
23927 if( pAr->eCmd==0 ){ in arParseCommand()
23936 ** array refer to archive members, as for the --extract, --list or --remove
23941 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
23942 ** when pAr->bGlob is true.
23950 if( pAr->nArg ){ in arCheckEntries()
23953 const char *zSel = (pAr->bGlob) in arCheckEntries()
23957 shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable); in arCheckEntries()
23959 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ in arCheckEntries()
23960 char *z = pAr->azArg[i]; in arCheckEntries()
23963 while( n>0 && z[n-1]=='/' ) n--; in arCheckEntries()
23965 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); in arCheckEntries()
23985 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
23986 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
23994 const char *zSameOp = (pAr->bGlob)? "GLOB" : "="; in arWhereClause()
23996 if( pAr->nArg==0 ){ in arWhereClause()
24001 for(i=0; i<pAr->nArg; i++){ in arWhereClause()
24002 const char *z = pAr->azArg[i]; in arWhereClause()
24035 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], in arListCommand()
24036 pAr->zSrcTable, zWhere); in arListCommand()
24037 if( pAr->bDryRun ){ in arListCommand()
24041 if( pAr->bVerbose ){ in arListCommand()
24063 if( pAr->nArg ){ in arRemoveCommand()
24071 pAr->zSrcTable, zWhere); in arRemoveCommand()
24072 if( pAr->bDryRun ){ in arRemoveCommand()
24076 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0); in arRemoveCommand()
24078 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); in arRemoveCommand()
24080 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); in arRemoveCommand()
24082 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0); in arRemoveCommand()
24125 if( pAr->zDir ){ in arExtractCommand()
24126 zDir = sqlite3_mprintf("%s/", pAr->zDir); in arExtractCommand()
24133 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, in arExtractCommand()
24134 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere in arExtractCommand()
24139 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); in arExtractCommand()
24149 if( pAr->bDryRun ){ in arExtractCommand()
24153 if( i==0 && pAr->bVerbose ){ in arExtractCommand()
24169 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
24173 if( pAr->bDryRun ){ in arExecSql()
24178 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); in arExecSql()
24191 ** create -> Create a new SQL archive
24192 ** insert -> Insert or reinsert all files listed
24193 ** update -> Insert files that have changed or that were not
24198 ** are added recursively. If argument bVerbose is non-zero, a message is
24203 ** always overwrites every file named on the command-line, where as
24208 int bUpdate, /* true for a --create. */ in arCreateOrUpdateCommand()
24213 " name TEXT PRIMARY KEY, -- name of the file\n" in arCreateOrUpdateCommand()
24214 " mode INT, -- access permissions\n" in arCreateOrUpdateCommand()
24215 " mtime INT, -- last modification time\n" in arCreateOrUpdateCommand()
24216 " sz INT, -- original file size\n" in arCreateOrUpdateCommand()
24217 " data BLOB -- compressed content\n" in arCreateOrUpdateCommand()
24227 " WHEN '-' THEN length(data)\n" in arCreateOrUpdateCommand()
24229 " ELSE -1 END,\n" in arCreateOrUpdateCommand()
24254 if( pAr->bZip ){ in arCreateOrUpdateCommand()
24256 if( pAr->zFile ){ in arCreateOrUpdateCommand()
24263 zTab, pAr->zFile in arCreateOrUpdateCommand()
24290 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ in arCreateOrUpdateCommand()
24291 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, in arCreateOrUpdateCommand()
24292 pAr->bVerbose ? "shell_putsnl(name)" : "name", in arCreateOrUpdateCommand()
24293 pAr->azArg[i], pAr->zDir, zExists); in arCreateOrUpdateCommand()
24299 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); in arCreateOrUpdateCommand()
24302 if( pAr->bZip && pAr->zFile ){ in arCreateOrUpdateCommand()
24317 int fromCmdLine, /* True if -A command-line option, not .ar cmd */ in arDotCommand()
24329 cmd.db = pState->db; in arDotCommand()
24333 eDbType = pState->openMode; in arDotCommand()
24355 oputf("-- open database '%s'%s\n", cmd.zFile, in arDotCommand()
24395 arUsage(pState->out); in arDotCommand()
24413 if( cmd.db!=pState->db ){ in arDotCommand()
24432 sputf(pState->out, "%s;\n", zSql); in recoverSqlCb()
24439 ** on stream pState->out.
24445 int bFreelist = 1; /* 0 if --ignore-freelist is specified */ in recoverDatabaseCmd()
24446 int bRowids = 1; /* 0 if --no-rowids */ in recoverDatabaseCmd()
24453 if( z[0]=='-' && z[1]=='-' ) z++; in recoverDatabaseCmd()
24455 if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){ in recoverDatabaseCmd()
24458 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){ in recoverDatabaseCmd()
24459 /* This option determines the name of the ATTACH-ed database used in recoverDatabaseCmd()
24467 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){ in recoverDatabaseCmd()
24471 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){ in recoverDatabaseCmd()
24476 showHelp(pState->out, azArg[0]); in recoverDatabaseCmd()
24482 pState->db, "main", recoverSqlCb, (void*)pState in recoverDatabaseCmd()
24511 * pointer is non-zero, its referent will be set to a summary of renames
24561 SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\ in zAutoColumn()
24569 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\ in zAutoColumn()
24577 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\ in zAutoColumn()
24635 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\ in zAutoColumn()
24638 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \ in zAutoColumn()
24643 " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff))," in zAutoColumn()
24664 rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0); in zAutoColumn()
24666 rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0); in zAutoColumn()
24686 rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0); in zAutoColumn()
24694 rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0); in zAutoColumn()
24706 if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0) in zAutoColumn()
24734 if( p->expert.pExpert ){ in do_meta_command()
24741 while( zLine[h] && nArg<ArraySize(azArg)-1 ){ in do_meta_command()
24754 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); in do_meta_command()
24779 sqlite3_set_authorizer(p->db, shellAuth, p); in do_meta_command()
24780 }else if( p->bSafeModePersist ){ in do_meta_command()
24781 sqlite3_set_authorizer(p->db, safeModeAuth, p); in do_meta_command()
24783 sqlite3_set_authorizer(p->db, 0, 0); in do_meta_command()
24811 if( z[0]=='-' ){ in do_meta_command()
24812 if( z[1]=='-' ) z++; in do_meta_command()
24813 if( cli_strcmp(z, "-append")==0 ){ in do_meta_command()
24816 if( cli_strcmp(z, "-async")==0 ){ in do_meta_command()
24850 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); in do_meta_command()
24881 setBinaryMode(p->out, 1); in do_meta_command()
24883 setTextMode(p->out, 1); in do_meta_command()
24892 /* The undocumented ".breakpoint" command causes a call to the no-op in do_meta_command()
24932 ** Then read the content of the testcase-out.txt file and compare against in do_meta_command()
24939 eputz("Usage: .check GLOB-PATTERN\n"); in do_meta_command()
24941 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ in do_meta_command()
24944 eputf("testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", in do_meta_command()
24945 p->zTestcase, azArg[1], zRes); in do_meta_command()
24948 oputf("testcase-%s ok\n", p->zTestcase); in do_meta_command()
24949 p->nCheck++; in do_meta_command()
24971 for(i=0; i<ArraySize(p->aAuxDb); i++){ in do_meta_command()
24972 const char *zFile = p->aAuxDb[i].zDbFilename; in do_meta_command()
24973 if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){ in do_meta_command()
24978 zFile = "(temporary-file)"; in do_meta_command()
24980 if( p->pAuxDb == &p->aAuxDb[i] ){ in do_meta_command()
24982 }else if( p->aAuxDb[i].db!=0 ){ in do_meta_command()
24987 int i = azArg[1][0] - '0'; in do_meta_command()
24988 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){ in do_meta_command()
24989 p->pAuxDb->db = p->db; in do_meta_command()
24990 p->pAuxDb = &p->aAuxDb[i]; in do_meta_command()
24991 globalDb = p->db = p->pAuxDb->db; in do_meta_command()
24992 p->pAuxDb->db = 0; in do_meta_command()
24996 int i = azArg[2][0] - '0'; in do_meta_command()
24997 if( i<0 || i>=ArraySize(p->aAuxDb) ){ in do_meta_command()
24998 /* No-op */ in do_meta_command()
24999 }else if( p->pAuxDb == &p->aAuxDb[i] ){ in do_meta_command()
25002 }else if( p->aAuxDb[i].db ){ in do_meta_command()
25004 close_db(p->aAuxDb[i].db); in do_meta_command()
25005 p->aAuxDb[i].db = 0; in do_meta_command()
25008 eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n"); in do_meta_command()
25016 setTextMode(p->out, 1); in do_meta_command()
25018 setBinaryMode(p->out, 1); in do_meta_command()
25022 eputz("The \".crnl\" is a no-op on non-Windows machines.\n"); in do_meta_command()
25035 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); in do_meta_command()
25037 eputf("Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
25053 int eTxn = sqlite3_txn_state(p->db, azName[i*2]); in do_meta_command()
25054 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]); in do_meta_command()
25059 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn"); in do_meta_command()
25095 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); in do_meta_command()
25097 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); in do_meta_command()
25122 int savedShowHeader = p->showHeader; in do_meta_command()
25123 int savedShellFlags = p->shellFlgs; in do_meta_command()
25128 if( azArg[i][0]=='-' ){ in do_meta_command()
25130 if( z[0]=='-' ) z++; in do_meta_command()
25131 if( cli_strcmp(z,"preserve-rowids")==0 ){ in do_meta_command()
25133 eputz("The --preserve-rowids option is not compatible" in do_meta_command()
25145 if( cli_strcmp(z,"data-only")==0 ){ in do_meta_command()
25182 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ in do_meta_command()
25185 ** So disable foreign-key constraint enforcement to prevent problems. */ in do_meta_command()
25189 p->writableSchema = 0; in do_meta_command()
25190 p->showHeader = 0; in do_meta_command()
25194 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); in do_meta_command()
25195 p->nErr = 0; in do_meta_command()
25206 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ in do_meta_command()
25217 if( p->writableSchema ){ in do_meta_command()
25219 p->writableSchema = 0; in do_meta_command()
25221 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); in do_meta_command()
25222 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); in do_meta_command()
25223 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ in do_meta_command()
25224 oputz(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n"); in do_meta_command()
25226 p->showHeader = savedShowHeader; in do_meta_command()
25227 p->shellFlgs = savedShellFlags; in do_meta_command()
25241 p->autoEQPtest = 0; in do_meta_command()
25242 if( p->autoEQPtrace ){ in do_meta_command()
25243 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); in do_meta_command()
25244 p->autoEQPtrace = 0; in do_meta_command()
25247 p->autoEQP = AUTOEQP_full; in do_meta_command()
25249 p->autoEQP = AUTOEQP_trigger; in do_meta_command()
25252 p->autoEQP = AUTOEQP_on; in do_meta_command()
25253 p->autoEQPtest = 1; in do_meta_command()
25255 p->autoEQP = AUTOEQP_full; in do_meta_command()
25256 p->autoEQPtrace = 1; in do_meta_command()
25258 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0); in do_meta_command()
25259 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); in do_meta_command()
25262 p->autoEQP = (u8)booleanValue(azArg[1]); in do_meta_command()
25288 if( val==1 && p->mode!=MODE_Explain ){ in do_meta_command()
25289 p->normalMode = p->mode; in do_meta_command()
25290 p->mode = MODE_Explain; in do_meta_command()
25291 p->autoExplain = 0; in do_meta_command()
25293 if( p->mode==MODE_Explain ) p->mode = p->normalMode; in do_meta_command()
25294 p->autoExplain = 0; in do_meta_command()
25296 if( p->mode==MODE_Explain ) p->mode = p->normalMode; in do_meta_command()
25297 p->autoExplain = 1; in do_meta_command()
25303 if( p->bSafeMode ){ in do_meta_command()
25316 const char *zCtrlName; /* Name of a test-control option */ in do_meta_command()
25332 int filectrl = -1; in do_meta_command()
25333 int iCtrl = -1; in do_meta_command()
25335 int isOk = 0; /* 0: usage 1: %lld 2: no-result */ in do_meta_command()
25343 if( zCmd[0]=='-' in do_meta_command()
25344 && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0) in do_meta_command()
25348 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i]; in do_meta_command()
25349 nArg -= 2; in do_meta_command()
25353 /* The argument can optionally begin with "-" or "--" */ in do_meta_command()
25354 if( zCmd[0]=='-' && zCmd[1] ){ in do_meta_command()
25356 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; in do_meta_command()
25359 /* --help lists all file-controls */ in do_meta_command()
25361 oputz("Available file-controls:\n"); in do_meta_command()
25378 eputf("Error: ambiguous file-control: \"%s\"\n" in do_meta_command()
25379 "Use \".filectrl --help\" for help\n", zCmd); in do_meta_command()
25386 eputf("Error: unknown file-control: %s\n" in do_meta_command()
25387 "Use \".filectrl --help\" for help\n", zCmd); in do_meta_command()
25392 iRes = nArg==3 ? integerValue(azArg[2]) : -1; in do_meta_command()
25393 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes); in do_meta_command()
25402 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
25410 x = nArg==3 ? booleanValue(azArg[2]) : -1; in do_meta_command()
25411 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
25420 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
25428 sqlite3_file_control(p->db, zSchema, filectrl, &z); in do_meta_command()
25440 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
25442 x = -1; in do_meta_command()
25443 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
25471 eputz("Usage: .fullschema ?--indent?\n"); in do_meta_command()
25476 rc = sqlite3_exec(p->db, in do_meta_command()
25487 rc = sqlite3_prepare_v2(p->db, in do_meta_command()
25490 -1, &pStmt, 0); in do_meta_command()
25511 p->showHeader = booleanValue(azArg[1]); in do_meta_command()
25512 p->shellFlgs |= SHFLG_HeaderSet; in do_meta_command()
25521 n = showHelp(p->out, azArg[1]); in do_meta_command()
25526 showHelp(p->out, 0); in do_meta_command()
25540 int nSep; /* Number of bytes in p->colSeparator[] */ in do_meta_command()
25552 if( p->mode==MODE_Ascii ){ in do_meta_command()
25560 if( z[0]=='-' && z[1]=='-' ) z++; in do_meta_command()
25561 if( z[0]!='-' ){ in do_meta_command()
25568 showHelp(p->out, "import"); in do_meta_command()
25571 }else if( cli_strcmp(z,"-v")==0 ){ in do_meta_command()
25573 }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){ in do_meta_command()
25575 }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){ in do_meta_command()
25577 }else if( cli_strcmp(z,"-ascii")==0 ){ in do_meta_command()
25582 }else if( cli_strcmp(z,"-csv")==0 ){ in do_meta_command()
25589 showHelp(p->out, "import"); in do_meta_command()
25596 showHelp(p->out, "import"); in do_meta_command()
25602 /* If neither the --csv or --ascii options are specified, then set in do_meta_command()
25604 nSep = strlen30(p->colSeparator); in do_meta_command()
25606 eputz("Error: non-null column separator required for import\n"); in do_meta_command()
25610 eputz("Error: multi-character column separators not allowed" in do_meta_command()
25614 nSep = strlen30(p->rowSeparator); in do_meta_command()
25616 eputz("Error: non-null row separator required for import\n"); in do_meta_command()
25619 if( nSep==2 && p->mode==MODE_Csv in do_meta_command()
25620 && cli_strcmp(p->rowSeparator,SEP_CrLf)==0 in do_meta_command()
25626 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
25627 nSep = strlen30(p->rowSeparator); in do_meta_command()
25630 eputz("Error: multi-character row separators not allowed" in do_meta_command()
25634 sCtx.cColSep = (u8)p->colSeparator[0]; in do_meta_command()
25635 sCtx.cRowSep = (u8)p->rowSeparator[0]; in do_meta_command()
25673 while( (nSkip--)>0 ){ in do_meta_command()
25687 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
25689 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ in do_meta_command()
25700 sputf((stdin_is_interactive && p->in==stdin)? p->out : stderr, in do_meta_command()
25720 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); in do_meta_command()
25722 eputf("%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db)); in do_meta_command()
25727 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
25731 eputf("Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
25755 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
25757 eputf("Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
25763 needCommit = sqlite3_get_autocommit(p->db); in do_meta_command()
25764 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); in do_meta_command()
25770 ** Did we reach end-of-file before finding any columns? in do_meta_command()
25775 ** Did we reach end-of-file OR end-of-line before finding any in do_meta_command()
25779 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; in do_meta_command()
25782 ** record terminator but only for last field of multi-field row. in do_meta_command()
25785 if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){ in do_meta_command()
25788 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); in do_meta_command()
25789 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ in do_meta_command()
25791 " - filling the rest with NULL\n", in do_meta_command()
25802 eputf("%s:%d: expected %d columns but found %d - extras ignored\n", in do_meta_command()
25810 sCtx.zFile, startLine, sqlite3_errmsg(p->db)); in do_meta_command()
25820 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); in do_meta_command()
25823 sCtx.nRow, sCtx.nErr, sCtx.nLine-1); in do_meta_command()
25838 eputf(".%s unavailable without --unsafe-testing\n", in do_meta_command()
25858 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); in do_meta_command()
25870 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
25878 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
25886 if( sqlite3_column_int(pStmt,1)==-1 ){ in do_meta_command()
25914 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); in do_meta_command()
25916 rc = sqlite3_exec(p->db, zSql, 0, 0, 0); in do_meta_command()
25917 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); in do_meta_command()
25919 eputf("Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); in do_meta_command()
25940 }else if( cli_strcmp(azArg[1], "-")==0 ){ in do_meta_command()
25979 sqlite3_limit(p->db, aLimit[i].limitCode, -1)); in do_meta_command()
25982 eputz("Usage: .limit NAME ?NEW-VALUE?\n"); in do_meta_command()
25986 int iLimit = -1; in do_meta_command()
26007 sqlite3_limit(p->db, aLimit[iLimit].limitCode, in do_meta_command()
26011 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); in do_meta_command()
26026 /* Must have a non-empty FILE. (Will not load self.) */ in do_meta_command()
26034 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); in do_meta_command()
26049 if( p->bSafeMode in do_meta_command()
26057 output_file_close(p->pLog); in do_meta_command()
26059 p->pLog = output_file_open(zFile, 0); in do_meta_command()
26082 /* Apply defaults for qbox pseudo-mode. If that in do_meta_command()
26083 * overwrites already-set values, user was informed of this. in do_meta_command()
26092 }else if( z[0]=='-' ){ in do_meta_command()
26095 " --noquote\n" in do_meta_command()
26096 " --quote\n" in do_meta_command()
26097 " --wordwrap on/off\n" in do_meta_command()
26098 " --wrap N\n" in do_meta_command()
26099 " --ww\n"); in do_meta_command()
26109 if( p->mode==MODE_Column in do_meta_command()
26110 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) in do_meta_command()
26112 oputf("current output mode: %s --wrap %d --wordwrap %s --%squote\n", in do_meta_command()
26113 modeDescr[p->mode], p->cmOpts.iWrap, in do_meta_command()
26114 p->cmOpts.bWordWrap ? "on" : "off", in do_meta_command()
26115 p->cmOpts.bQuote ? "" : "no"); in do_meta_command()
26117 oputf("current output mode: %s\n", modeDescr[p->mode]); in do_meta_command()
26119 zMode = modeDescr[p->mode]; in do_meta_command()
26123 p->mode = MODE_Line; in do_meta_command()
26124 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
26126 p->mode = MODE_Column; in do_meta_command()
26127 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){ in do_meta_command()
26128 p->showHeader = 1; in do_meta_command()
26130 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
26131 p->cmOpts = cmOpts; in do_meta_command()
26133 p->mode = MODE_List; in do_meta_command()
26134 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); in do_meta_command()
26135 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
26137 p->mode = MODE_Html; in do_meta_command()
26139 p->mode = MODE_Tcl; in do_meta_command()
26140 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); in do_meta_command()
26141 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
26143 p->mode = MODE_Csv; in do_meta_command()
26144 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); in do_meta_command()
26145 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); in do_meta_command()
26147 p->mode = MODE_List; in do_meta_command()
26148 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); in do_meta_command()
26150 p->mode = MODE_Insert; in do_meta_command()
26153 p->mode = MODE_Quote; in do_meta_command()
26154 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); in do_meta_command()
26155 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
26157 p->mode = MODE_Ascii; in do_meta_command()
26158 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); in do_meta_command()
26159 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); in do_meta_command()
26161 p->mode = MODE_Markdown; in do_meta_command()
26162 p->cmOpts = cmOpts; in do_meta_command()
26164 p->mode = MODE_Table; in do_meta_command()
26165 p->cmOpts = cmOpts; in do_meta_command()
26167 p->mode = MODE_Box; in do_meta_command()
26168 p->cmOpts = cmOpts; in do_meta_command()
26170 p->mode = MODE_Count; in do_meta_command()
26172 p->mode = MODE_Off; in do_meta_command()
26174 p->mode = MODE_Json; in do_meta_command()
26181 p->cMode = p->mode; in do_meta_command()
26189 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){ in do_meta_command()
26191 p->lineno, azArg[1]); in do_meta_command()
26194 p->bSafeMode = 0; in do_meta_command()
26203 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, in do_meta_command()
26204 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); in do_meta_command()
26212 const char *zFN = 0; /* Pointer to constant filename */ in do_meta_command()
26218 /* Check for command-line arguments */ in do_meta_command()
26233 p->openFlags |= SQLITE_OPEN_NOFOLLOW; in do_meta_command()
26240 p->szMax = integerValue(azArg[++iName]); in do_meta_command()
26244 if( z[0]=='-' ){ in do_meta_command()
26258 session_close_all(p, -1); in do_meta_command()
26259 close_db(p->db); in do_meta_command()
26260 p->db = 0; in do_meta_command()
26261 p->pAuxDb->zDbFilename = 0; in do_meta_command()
26262 sqlite3_free(p->pAuxDb->zFreeOnClose); in do_meta_command()
26263 p->pAuxDb->zFreeOnClose = 0; in do_meta_command()
26264 p->openMode = openMode; in do_meta_command()
26265 p->openFlags = 0; in do_meta_command()
26266 p->szMax = 0; in do_meta_command()
26269 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){ in do_meta_command()
26270 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN); in do_meta_command()
26272 if( p->bSafeMode in do_meta_command()
26273 && p->openMode!=SHELL_OPEN_HEXDB in do_meta_command()
26277 failIfSafeMode(p, "cannot open disk-based database files in safe mode"); in do_meta_command()
26280 /* WASM mode has its own sandboxed pseudo-filesystem. */ in do_meta_command()
26288 p->pAuxDb->zDbFilename = zNewFilename; in do_meta_command()
26290 if( p->db==0 ){ in do_meta_command()
26294 p->pAuxDb->zFreeOnClose = zNewFilename; in do_meta_command()
26297 if( p->db==0 ){ in do_meta_command()
26298 /* As a fall-back open a TEMP database */ in do_meta_command()
26299 p->pAuxDb->zDbFilename = 0; in do_meta_command()
26327 if( z[0]=='-' ){ in do_meta_command()
26328 if( z[1]=='-' ) z++; in do_meta_command()
26329 if( cli_strcmp(z,"-bom")==0 ){ in do_meta_command()
26331 }else if( c!='e' && cli_strcmp(z,"-x")==0 ){ in do_meta_command()
26333 }else if( c!='e' && cli_strcmp(z,"-e")==0 ){ in do_meta_command()
26337 showHelp(p->out, azArg[0]); in do_meta_command()
26349 showHelp(p->out, azArg[0]); in do_meta_command()
26359 p->outCount = 2; in do_meta_command()
26361 p->outCount = 0; in do_meta_command()
26366 p->doXdgOpen = 1; in do_meta_command()
26372 p->mode = MODE_Csv; in do_meta_command()
26373 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); in do_meta_command()
26374 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); in do_meta_command()
26381 zFile = sqlite3_mprintf("%s", p->zTempFile); in do_meta_command()
26398 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); in do_meta_command()
26411 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); in do_meta_command()
26426 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", in do_meta_command()
26437 rx = sqlite3_prepare_v2(p->db, in do_meta_command()
26439 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); in do_meta_command()
26447 rx = sqlite3_prepare_v2(p->db, in do_meta_command()
26449 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); in do_meta_command()
26451 oputf("%-*s %s\n", len, sqlite3_column_text(pStmt,0), in do_meta_command()
26484 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
26493 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
26496 oputf("Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
26514 sqlite3_exec(p->db, zSql, 0, 0, 0); in do_meta_command()
26519 showHelp(p->out, "parameter"); in do_meta_command()
26535 p->flgProgress = 0; in do_meta_command()
26536 p->mxProgress = 0; in do_meta_command()
26537 p->nProgress = 0; in do_meta_command()
26540 if( z[0]=='-' ){ in do_meta_command()
26542 if( z[0]=='-' ) z++; in do_meta_command()
26544 p->flgProgress |= SHELL_PROGRESS_QUIET; in do_meta_command()
26548 p->flgProgress |= SHELL_PROGRESS_RESET; in do_meta_command()
26552 p->flgProgress |= SHELL_PROGRESS_ONCE; in do_meta_command()
26557 eputz("Error: missing argument on --limit\n"); in do_meta_command()
26561 p->mxProgress = (int)integerValue(azArg[++i]); in do_meta_command()
26573 sqlite3_progress_handler(p->db, nn, progress_handler, p); in do_meta_command()
26579 shell_strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); in do_meta_command()
26582 shell_strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); in do_meta_command()
26594 FILE *inSaved = p->in; in do_meta_command()
26595 int savedLineno = p->lineno; in do_meta_command()
26606 p->out = stdout; in do_meta_command()
26608 p->in = popen(azArg[1]+1, "r"); in do_meta_command()
26609 if( p->in==0 ){ in do_meta_command()
26614 pclose(p->in); in do_meta_command()
26617 }else if( (p->in = openChrSource(azArg[1]))==0 ){ in do_meta_command()
26622 fclose(p->in); in do_meta_command()
26624 p->in = inSaved; in do_meta_command()
26625 p->lineno = savedLineno; in do_meta_command()
26656 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); in do_meta_command()
26658 eputf("Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
26676 eputf("Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
26685 if( cli_strcmp(azArg[1], "vm")==0 ){ in do_meta_command()
26686 p->scanstatsOn = 3; in do_meta_command()
26689 p->scanstatsOn = 2; in do_meta_command()
26691 p->scanstatsOn = (u8)booleanValue(azArg[1]); in do_meta_command()
26695 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0 in do_meta_command()
26700 if( p->scanstatsOn==3 ){ in do_meta_command()
26701 eputz("Warning: \".scanstats vm\" not available in this build.\n"); in do_meta_command()
26733 }else if( azArg[ii][0]=='-' ){ in do_meta_command()
26740 eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n"); in do_meta_command()
26770 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", in do_meta_command()
26771 -1, &pStmt, 0); in do_meta_command()
26773 eputf("Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
26837 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); in do_meta_command()
26862 struct AuxDb *pAuxDb = p->pAuxDb; in do_meta_command()
26863 OpenSession *pSession = &pAuxDb->aSession[0]; in do_meta_command()
26866 int nCmd = nArg - 1; in do_meta_command()
26871 for(iSes=0; iSes<pAuxDb->nSession; iSes++){ in do_meta_command()
26872 if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break; in do_meta_command()
26874 if( iSes<pAuxDb->nSession ){ in do_meta_command()
26875 pSession = &pAuxDb->aSession[iSes]; in do_meta_command()
26877 nCmd--; in do_meta_command()
26879 pSession = &pAuxDb->aSession[0]; in do_meta_command()
26890 if( pSession->p==0 ){ in do_meta_command()
26894 rc = sqlite3session_attach(pSession->p, azCmd[1]); in do_meta_command()
26912 if( pSession->p==0 ) goto session_not_open; in do_meta_command()
26921 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); in do_meta_command()
26923 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); in do_meta_command()
26931 eputf("ERROR: Failed to write entire %d-byte output\n", szChng); in do_meta_command()
26943 if( pAuxDb->nSession ){ in do_meta_command()
26945 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession]; in do_meta_command()
26955 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); in do_meta_command()
26956 if( pAuxDb->nSession ){ in do_meta_command()
26957 ii = sqlite3session_enable(pSession->p, ii); in do_meta_command()
26958 oputf("session %s enable flag = %d\n", pSession->zName, ii); in do_meta_command()
26968 if( pAuxDb->nSession ){ in do_meta_command()
26969 for(ii=0; ii<pSession->nFilter; ii++){ in do_meta_command()
26970 sqlite3_free(pSession->azFilter[ii]); in do_meta_command()
26972 sqlite3_free(pSession->azFilter); in do_meta_command()
26973 nByte = sizeof(pSession->azFilter[0])*(nCmd-1); in do_meta_command()
26974 pSession->azFilter = sqlite3_malloc( nByte ); in do_meta_command()
26975 shell_check_oom( pSession->azFilter ); in do_meta_command()
26977 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); in do_meta_command()
26980 pSession->nFilter = ii-1; in do_meta_command()
26990 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); in do_meta_command()
26991 if( pAuxDb->nSession ){ in do_meta_command()
26992 ii = sqlite3session_indirect(pSession->p, ii); in do_meta_command()
26993 oputf("session %s indirect flag = %d\n", pSession->zName, ii); in do_meta_command()
27003 if( pAuxDb->nSession ){ in do_meta_command()
27004 ii = sqlite3session_isempty(pSession->p); in do_meta_command()
27005 oputf("session %s isempty flag = %d\n", pSession->zName, ii); in do_meta_command()
27013 for(i=0; i<pAuxDb->nSession; i++){ in do_meta_command()
27014 oputf("%d %s\n", i, pAuxDb->aSession[i].zName); in do_meta_command()
27027 for(i=0; i<pAuxDb->nSession; i++){ in do_meta_command()
27028 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){ in do_meta_command()
27033 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){ in do_meta_command()
27034 eputf("Maximum of %d sessions\n", ArraySize(pAuxDb->aSession)); in do_meta_command()
27037 pSession = &pAuxDb->aSession[pAuxDb->nSession]; in do_meta_command()
27038 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); in do_meta_command()
27044 pSession->nFilter = 0; in do_meta_command()
27045 sqlite3session_table_filter(pSession->p, session_filter, pSession); in do_meta_command()
27046 pAuxDb->nSession++; in do_meta_command()
27047 pSession->zName = sqlite3_mprintf("%s", zName); in do_meta_command()
27048 shell_check_oom(pSession->zName); in do_meta_command()
27052 showHelp(p->out, "session"); in do_meta_command()
27059 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){ in do_meta_command()
27060 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){ in do_meta_command()
27067 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){ in do_meta_command()
27092 if( z[0]=='-' && z[1]=='-' ) z++; in do_meta_command()
27093 if( cli_strcmp(z,"-init")==0 ){ in do_meta_command()
27096 if( cli_strcmp(z,"-v")==0 ){ in do_meta_command()
27101 eputz("Should be one of: --init -v\n"); in do_meta_command()
27106 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) in do_meta_command()
27118 for(k=bSelftestExists; k>=0; k--){ in do_meta_command()
27120 rc = sqlite3_prepare_v2(p->db, in do_meta_command()
27122 -1, &pStmt, 0); in do_meta_command()
27124 rc = sqlite3_prepare_v2(p->db, in do_meta_command()
27125 "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," in do_meta_command()
27127 -1, &pStmt, 0); in do_meta_command()
27155 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); in do_meta_command()
27163 oputf("%d: error-code-%d: %s\n", tno, rc, zErrMsg); in do_meta_command()
27190 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, in do_meta_command()
27191 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); in do_meta_command()
27194 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, in do_meta_command()
27195 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); in do_meta_command()
27214 if( z[0]=='-' ){ in do_meta_command()
27216 if( z[0]=='-' ) z++; in do_meta_command()
27220 if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0 in do_meta_command()
27221 || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0 in do_meta_command()
27230 showHelp(p->out, azArg[0]); in do_meta_command()
27235 eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); in do_meta_command()
27255 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
27314 char *zRevText = /* Query for reversible to-blob-to-text check */ in do_meta_command()
27321 /* lower-case query is first run, producing upper-case query. */ in do_meta_command()
27338 lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0); in do_meta_command()
27344 if( zLike ) sqlite3_bind_text(pStmt,1,zLike,-1,SQLITE_STATIC); in do_meta_command()
27349 lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0); in do_meta_command()
27411 oputf("%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); in do_meta_command()
27413 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); in do_meta_command()
27414 oputf("%12.12s: %s\n","headers", azBool[p->showHeader!=0]); in do_meta_command()
27415 if( p->mode==MODE_Column in do_meta_command()
27416 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) in do_meta_command()
27418 oputf("%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode", in do_meta_command()
27419 modeDescr[p->mode], p->cmOpts.iWrap, in do_meta_command()
27420 p->cmOpts.bWordWrap ? "on" : "off", in do_meta_command()
27421 p->cmOpts.bQuote ? "" : "no"); in do_meta_command()
27423 oputf("%12.12s: %s\n","mode", modeDescr[p->mode]); in do_meta_command()
27426 output_c_string(p->nullValue); in do_meta_command()
27429 strlen30(p->outfile) ? p->outfile : "stdout"); in do_meta_command()
27431 output_c_string(p->colSeparator); in do_meta_command()
27434 output_c_string(p->rowSeparator); in do_meta_command()
27436 switch( p->statsOn ){ in do_meta_command()
27444 for (i=0;i<p->nWidth;i++) { in do_meta_command()
27445 oputf("%d ", p->colWidth[i]); in do_meta_command()
27449 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : ""); in do_meta_command()
27455 p->statsOn = 2; in do_meta_command()
27457 p->statsOn = 3; in do_meta_command()
27459 p->statsOn = (u8)booleanValue(azArg[1]); in do_meta_command()
27462 display_stats(p->db, p, 0); in do_meta_command()
27480 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); in do_meta_command()
27483 return shellDatabaseError(p->db); in do_meta_command()
27490 eputz("Usage: .indexes ?LIKE-PATTERN?\n"); in do_meta_command()
27520 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); in do_meta_command()
27523 if( rc ) return shellDatabaseError(p->db); in do_meta_command()
27526 ** as an array of nul-terminated strings in azResult[]. */ in do_meta_command()
27530 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); in do_meta_command()
27532 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); in do_meta_command()
27548 rc = shellDatabaseError(p->db); in do_meta_command()
27551 /* Pretty-print the contents of array azResult[] to the output */ in do_meta_command()
27562 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; in do_meta_command()
27566 oputf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:""); in do_meta_command()
27577 /* Begin redirecting output to the file "testcase-out.txt" */ in do_meta_command()
27580 p->out = output_file_open("testcase-out.txt", 0); in do_meta_command()
27581 if( p->out==0 ){ in do_meta_command()
27582 eputz("Error: cannot open 'testcase-out.txt'\n"); in do_meta_command()
27585 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); in do_meta_command()
27587 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); in do_meta_command()
27595 const char *zCtrlName; /* Name of a test-control option */ in do_meta_command()
27597 int unSafe; /* Not valid unless --unsafe-testing */ in do_meta_command()
27612 {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK" }, in do_meta_command()
27625 int testctrl = -1; in do_meta_command()
27626 int iCtrl = -1; in do_meta_command()
27627 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ in do_meta_command()
27635 /* The argument can optionally begin with "-" or "--" */ in do_meta_command()
27636 if( zCmd[0]=='-' && zCmd[1] ){ in do_meta_command()
27638 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; in do_meta_command()
27641 /* --help lists all test-controls */ in do_meta_command()
27643 oputz("Available test-controls:\n"); in do_meta_command()
27663 eputf("Error: ambiguous test-control: \"%s\"\n" in do_meta_command()
27664 "Use \".testctrl --help\" for help\n", zCmd); in do_meta_command()
27671 eputf("Error: unknown test-control: %s\n" in do_meta_command()
27672 "Use \".testctrl --help\" for help\n", zCmd); in do_meta_command()
27681 rc2 = sqlite3_test_control(testctrl, p->db, opt); in do_meta_command()
27712 sputf(stdout, "-- random seed: %d\n", ii); in do_meta_command()
27717 db = p->db; in do_meta_command()
27748 int opt = -1; in do_meta_command()
27763 rc2 = sqlite3_test_control(testctrl, p->db); in do_meta_command()
27769 rc2 = sqlite3_test_control(testctrl, p->db, in do_meta_command()
27779 rc2 = sqlite3_test_control(testctrl, p->db, &x); in do_meta_command()
27788 sqlite3_test_control(testctrl, p->out); in do_meta_command()
27803 sqlite3_test_control(testctrl, -id, &rc2); in do_meta_command()
27809 rc2 = sqlite3_test_control(testctrl, -id, &val); in do_meta_command()
27824 rc2 = sqlite3_test_control(testctrl, p->db, opt); in do_meta_command()
27843 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); in do_meta_command()
27866 if( z[0]=='-' ){ in do_meta_command()
27868 p->eTraceType = SHELL_TRACE_EXPANDED; in do_meta_command()
27872 p->eTraceType = SHELL_TRACE_NORMALIZED; in do_meta_command()
27876 p->eTraceType = SHELL_TRACE_PLAIN; in do_meta_command()
27896 output_file_close(p->traceOut); in do_meta_command()
27897 p->traceOut = output_file_open(z, 0); in do_meta_command()
27900 if( p->traceOut==0 ){ in do_meta_command()
27901 sqlite3_trace_v2(p->db, 0, 0, 0); in do_meta_command()
27904 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); in do_meta_command()
27915 eputz("Usage: .unmodule [--allexcept] NAME ...\n"); in do_meta_command()
27921 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; in do_meta_command()
27923 if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){ in do_meta_command()
27925 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); in do_meta_command()
27928 sqlite3_create_module(p->db, azArg[ii], 0, 0); in do_meta_command()
27948 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], in do_meta_command()
27960 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), in do_meta_command()
27963 eputf("User-Add failed: %d\n", rc); in do_meta_command()
27972 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), in do_meta_command()
27975 eputf("User-Edit failed: %d\n", rc); in do_meta_command()
27984 rc = sqlite3_user_delete(p->db, azArg[2]); in do_meta_command()
27986 eputf("User-Delete failed: %d\n", rc); in do_meta_command()
27998 char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit"; in do_meta_command()
27999 oputf("SQLite %s %s\n" /*extra-version-info*/, in do_meta_command()
28007 oputf("clang-" CTIMEOPT_VAL(__clang_major__) "." in do_meta_command()
28011 oputf("msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz); in do_meta_command()
28013 oputf("gcc-" __VERSION__ " (%s)\n", zPtrSz); in do_meta_command()
28020 if( p->db ){ in do_meta_command()
28021 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); in do_meta_command()
28023 oputf("vfs.zName = \"%s\"\n", pVfs->zName); in do_meta_command()
28024 oputf("vfs.iVersion = %d\n", pVfs->iVersion); in do_meta_command()
28025 oputf("vfs.szOsFile = %d\n", pVfs->szOsFile); in do_meta_command()
28026 oputf("vfs.mxPathname = %d\n", pVfs->mxPathname); in do_meta_command()
28034 if( p->db ){ in do_meta_command()
28035 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); in do_meta_command()
28037 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ in do_meta_command()
28038 oputf("vfs.zName = \"%s\"%s\n", pVfs->zName, in do_meta_command()
28039 pVfs==pCurrent ? " <--- CURRENT" : ""); in do_meta_command()
28040 oputf("vfs.iVersion = %d\n", pVfs->iVersion); in do_meta_command()
28041 oputf("vfs.szOsFile = %d\n", pVfs->szOsFile); in do_meta_command()
28042 oputf("vfs.mxPathname = %d\n", pVfs->mxPathname); in do_meta_command()
28043 if( pVfs->pNext ){ in do_meta_command()
28044 oputz("-----------------------------------\n"); in do_meta_command()
28052 if( p->db ){ in do_meta_command()
28053 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); in do_meta_command()
28069 p->nWidth = nArg-1; in do_meta_command()
28070 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2); in do_meta_command()
28071 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory(); in do_meta_command()
28072 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth]; in do_meta_command()
28074 p->colWidth[j-1] = (int)integerValue(azArg[j]); in do_meta_command()
28085 if( p->outCount ){ in do_meta_command()
28086 p->outCount--; in do_meta_command()
28087 if( p->outCount==0 ) output_reset(p); in do_meta_command()
28089 p->bSafeMode = p->bSafeModePersist; in do_meta_command()
28100 QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
28125 case '-': in quickscan()
28126 if( *zLine!='-' ) in quickscan()
28156 CONTINUE_PAREN_INCR(pst, -1); in quickscan()
28178 /* Swallow doubled end-delimiter.*/ in quickscan()
28198 ** than a semi-colon. The SQL Server style "go" command is understood
28222 ** ends in the middle of a string literal or C-style comment.
28243 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; in runOneSqlLine()
28253 zErrorTail = sqlite3_errmsg(p->db); in runOneSqlLine()
28278 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db)); in runOneSqlLine()
28309 if(nZ>0 && '\r'==zBegin[nZ-1]){ in one_input_line()
28310 --nZ; in one_input_line()
28323 ** is interactive - the user is typing it it. Otherwise, input
28341 if( p->inputNesting==MAX_INPUT_NESTING ){ in process_input()
28344 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno); in process_input()
28347 ++p->inputNesting; in process_input()
28348 p->lineno = 0; in process_input()
28350 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ in process_input()
28351 fflush(p->out); in process_input()
28352 zLine = one_input_line(p->in, zLine, nSql>0); in process_input()
28355 if( p->in==0 && stdin_is_interactive ) oputz("\n"); in process_input()
28359 if( p->in!=0 ) break; in process_input()
28362 p->lineno++; in process_input()
28370 /* Just swallow single-line whitespace */ in process_input()
28389 /* No single-line dispositions remain; accumulate line(s). */ in process_input()
28392 /* Grow buffer by half-again increments when big. */ in process_input()
28401 memcpy(zSql, zLine+i, nLine+1-i); in process_input()
28402 startline = p->lineno; in process_input()
28403 nSql = nLine-i; in process_input()
28411 errCnt += runOneSqlLine(p, zSql, p->in, startline); in process_input()
28414 if( p->outCount ){ in process_input()
28416 p->outCount = 0; in process_input()
28420 p->bSafeMode = p->bSafeModePersist; in process_input()
28431 errCnt += runOneSqlLine(p, zSql, p->in, startline); in process_input()
28436 --p->inputNesting; in process_input()
28459 home_dir = pwent->pw_dir; in find_home_dir()
28465 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() in find_home_dir()
28510 ** On non-Windows platforms, look for $XDG_CONFIG_HOME.
28556 FILE *inSaved = p->in; in process_sqliterc()
28557 int savedLineno = p->lineno; in process_sqliterc()
28565 eputz("-- warning: cannot find home directory;" in process_sqliterc()
28573 p->in = fopen(sqliterc,"rb"); in process_sqliterc()
28574 if( p->in ){ in process_sqliterc()
28576 eputf("-- Loading resources from %s\n", sqliterc); in process_sqliterc()
28579 fclose(p->in); in process_sqliterc()
28584 p->in = inSaved; in process_sqliterc()
28585 p->lineno = savedLineno; in process_sqliterc()
28593 " -- treat no subsequent arguments as options\n"
28595 " -A ARGS... run \".archive ARGS\" and exit\n"
28597 " -append append the database to the end of the file\n"
28598 " -ascii set output mode to 'ascii'\n"
28599 " -bail stop after hitting an error\n"
28600 " -batch force batch I/O\n"
28601 " -box set output mode to 'box'\n"
28602 " -column set output mode to 'column'\n"
28603 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
28604 " -csv set output mode to 'csv'\n"
28606 " -deserialize open the database using sqlite3_deserialize()\n"
28608 " -echo print inputs before execution\n"
28609 " -init FILENAME read/process named file\n"
28610 " -[no]header turn headers on or off\n"
28612 " -heap SIZE Size of heap for memsys3 or memsys5\n"
28614 " -help show this message\n"
28615 " -html set output mode to HTML\n"
28616 " -interactive force interactive I/O\n"
28617 " -json set output mode to 'json'\n"
28618 " -line set output mode to 'line'\n"
28619 " -list set output mode to 'list'\n"
28620 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
28621 " -markdown set output mode to 'markdown'\n"
28623 " -maxsize N maximum size for a --deserialize database\n"
28625 " -memtrace trace all memory allocations and deallocations\n"
28626 " -mmap N default mmap size set to N\n"
28628 " -multiplex enable the multiplexor VFS\n"
28630 " -newline SEP set output row separator. Default: '\\n'\n"
28631 " -nofollow refuse to open symbolic links to database files\n"
28632 " -nonce STRING set the safe-mode escape nonce\n"
28633 " -no-rowid-in-view Disable rowid-in-view using sqlite3_config()\n"
28634 " -nullvalue TEXT set text string for NULL values. Default ''\n"
28635 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
28636 " -pcachetrace trace all page cache operations\n"
28637 " -quote set output mode to 'quote'\n"
28638 " -readonly open the database read-only\n"
28639 " -safe enable safe-mode\n"
28640 " -separator SEP set output column separator. Default: '|'\n"
28642 " -sorterref SIZE sorter references threshold size\n"
28644 " -stats print memory stats before each finalize\n"
28645 " -table set output mode to 'table'\n"
28646 " -tabs set output mode to 'tabs'\n"
28647 " -unsafe-testing allow unsafe commands and modes for testing\n"
28648 " -version show SQLite version\n"
28649 " -vfs NAME use NAME as the default VFS\n"
28651 " -vfstrace enable tracing of all VFS calls\n"
28654 " -zip open the file as a ZIP Archive\n"
28664 eputz("Use the -help option for additional information\n"); in usage()
28674 if( sqlite3_config(-1)==SQLITE_MISUSE ){ in verify_uninitialized()
28685 data->normalMode = data->cMode = data->mode = MODE_List; in main_init()
28686 data->autoExplain = 1; in main_init()
28687 data->pAuxDb = &data->aAuxDb[0]; in main_init()
28688 memcpy(data->colSeparator,SEP_Column, 2); in main_init()
28689 memcpy(data->rowSeparator,SEP_Row, 2); in main_init()
28690 data->showHeader = 0; in main_init()
28691 data->shellFlgs = SHFLG_Lookaside; in main_init()
28727 ** Get the argument to an --option. Throw an error and die if no argument
28732 eputf("%s: Error: missing argument to %s\n", argv[0], argv[argc-1]); in cmdline_option_value()
28779 const char *zVfs = 0; /* Value of -vfs command-line option */
28837 /* On Windows, we must translate command-line arguments into UTF-8.
28869 ** of a C-function that will provide the name of the database file. Use
28870 ** this compile-time option to embed this shell program in larger
28873 SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
28878 /* Do an initial pass through the command-line argument to locate
28889 if( z[0]!='-' || i>nOptsEnd ){
28890 if( data.aAuxDb->zDbFilename==0 ){
28891 data.aAuxDb->zDbFilename = z;
28893 /* Excess arguments are interpreted as SQL (or dot-commands) and
28899 azCmd[nCmd-1] = z;
28903 if( z[1]=='-' ) z++;
28904 if( cli_strcmp(z, "-")==0 ){
28907 }else if( cli_strcmp(z,"-separator")==0
28908 || cli_strcmp(z,"-nullvalue")==0
28909 || cli_strcmp(z,"-newline")==0
28910 || cli_strcmp(z,"-cmd")==0
28913 }else if( cli_strcmp(z,"-init")==0 ){
28915 }else if( cli_strcmp(z,"-interactive")==0 ){
28920 }else if( cli_strcmp(z,"-batch")==0 ){
28926 }else if( cli_strcmp(z,"-utf8")==0 ){
28927 }else if( cli_strcmp(z,"-no-utf8")==0 ){
28928 }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){
28932 }else if( cli_strcmp(z,"-heap")==0 ){
28945 }else if( cli_strcmp(z,"-pagecache")==0 ){
28958 }else if( cli_strcmp(z,"-lookaside")==0 ){
28967 }else if( cli_strcmp(z,"-threadsafe")==0 ){
28977 }else if( cli_strcmp(z,"-vfstrace")==0 ){
28988 }else if( cli_strcmp(z,"-multiplex")==0 ){
28992 }else if( cli_strcmp(z,"-mmap")==0 ){
28997 }else if( cli_strcmp(z,"-sorterref")==0 ){
29002 }else if( cli_strcmp(z,"-vfs")==0 ){
29005 }else if( cli_strcmp(z,"-zip")==0 ){
29008 }else if( cli_strcmp(z,"-append")==0 ){
29011 }else if( cli_strcmp(z,"-deserialize")==0 ){
29013 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
29016 }else if( cli_strcmp(z,"-readonly")==0 ){
29018 }else if( cli_strcmp(z,"-nofollow")==0 ){
29021 }else if( cli_strncmp(z, "-A",2)==0 ){
29022 /* All remaining command-line arguments are passed to the ".archive"
29026 }else if( cli_strcmp(z, "-memtrace")==0 ){
29028 }else if( cli_strcmp(z, "-pcachetrace")==0 ){
29030 }else if( cli_strcmp(z,"-bail")==0 ){
29032 }else if( cli_strcmp(z,"-nonce")==0 ){
29035 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
29037 }else if( cli_strcmp(z,"-safe")==0 ){
29038 /* no-op - catch this on the second pass */
29049 ** of a C-function that will perform initialization actions on SQLite that
29050 ** occur just before or after sqlite3_initialize(). Use this compile-time
29057 ** to call sqlite3_initialize() and process any command line -vfs option. */
29071 if( data.pAuxDb->zDbFilename==0 ){
29073 data.pAuxDb->zDbFilename = ":memory:";
29088 ** to the sqlite command-line tool.
29090 if( access(data.pAuxDb->zDbFilename, 0)==0 ){
29094 /* Process the initialization file if there is one. If no -init option
29100 /* Make a second pass through the command-line argument and set
29102 ** file is processed so that the command-line arguments will override
29107 if( z[0]!='-' || i>=nOptsEnd ) continue;
29108 if( z[1]=='-' ){ z++; }
29109 if( cli_strcmp(z,"-init")==0 ){
29111 }else if( cli_strcmp(z,"-html")==0 ){
29113 }else if( cli_strcmp(z,"-list")==0 ){
29115 }else if( cli_strcmp(z,"-quote")==0 ){
29119 }else if( cli_strcmp(z,"-line")==0 ){
29121 }else if( cli_strcmp(z,"-column")==0 ){
29123 }else if( cli_strcmp(z,"-json")==0 ){
29125 }else if( cli_strcmp(z,"-markdown")==0 ){
29127 }else if( cli_strcmp(z,"-table")==0 ){
29129 }else if( cli_strcmp(z,"-box")==0 ){
29131 }else if( cli_strcmp(z,"-csv")==0 ){
29135 }else if( cli_strcmp(z,"-zip")==0 ){
29138 }else if( cli_strcmp(z,"-append")==0 ){
29141 }else if( cli_strcmp(z,"-deserialize")==0 ){
29143 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
29146 }else if( cli_strcmp(z,"-readonly")==0 ){
29148 }else if( cli_strcmp(z,"-nofollow")==0 ){
29150 }else if( cli_strcmp(z,"-ascii")==0 ){
29154 }else if( cli_strcmp(z,"-tabs")==0 ){
29158 }else if( cli_strcmp(z,"-separator")==0 ){
29161 }else if( cli_strcmp(z,"-newline")==0 ){
29164 }else if( cli_strcmp(z,"-nullvalue")==0 ){
29167 }else if( cli_strcmp(z,"-header")==0 ){
29170 }else if( cli_strcmp(z,"-noheader")==0 ){
29173 }else if( cli_strcmp(z,"-echo")==0 ){
29175 }else if( cli_strcmp(z,"-eqp")==0 ){
29177 }else if( cli_strcmp(z,"-eqpfull")==0 ){
29179 }else if( cli_strcmp(z,"-stats")==0 ){
29181 }else if( cli_strcmp(z,"-scanstats")==0 ){
29183 }else if( cli_strcmp(z,"-backslash")==0 ){
29184 /* Undocumented command-line option: -backslash
29185 ** Causes C-style backslash escapes to be evaluated in SQL statements
29190 }else if( cli_strcmp(z,"-bail")==0 ){
29191 /* No-op. The bail_on_error flag should already be set. */
29192 }else if( cli_strcmp(z,"-version")==0 ){
29193 oputf("%s %s (%d-bit)\n", sqlite3_libversion(), sqlite3_sourceid(),
29196 }else if( cli_strcmp(z,"-interactive")==0 ){
29198 }else if( cli_strcmp(z,"-batch")==0 ){
29200 }else if( cli_strcmp(z,"-utf8")==0 ){
29202 }else if( cli_strcmp(z,"-no-utf8")==0 ){
29204 }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){
29206 }else if( cli_strcmp(z,"-heap")==0 ){
29208 }else if( cli_strcmp(z,"-pagecache")==0 ){
29210 }else if( cli_strcmp(z,"-lookaside")==0 ){
29212 }else if( cli_strcmp(z,"-threadsafe")==0 ){
29214 }else if( cli_strcmp(z,"-nonce")==0 ){
29216 }else if( cli_strcmp(z,"-mmap")==0 ){
29218 }else if( cli_strcmp(z,"-memtrace")==0 ){
29220 }else if( cli_strcmp(z,"-pcachetrace")==0 ){
29223 }else if( cli_strcmp(z,"-sorterref")==0 ){
29226 }else if( cli_strcmp(z,"-vfs")==0 ){
29229 }else if( cli_strcmp(z,"-vfstrace")==0 ){
29233 }else if( cli_strcmp(z,"-multiplex")==0 ){
29236 }else if( cli_strcmp(z,"-help")==0 ){
29238 }else if( cli_strcmp(z,"-cmd")==0 ){
29239 /* Run commands that follow -cmd first and separately from commands
29240 ** that simply appear on the command-line. This seems goofy. It would
29243 if( i==argc-1 ) break;
29260 }else if( cli_strncmp(z, "-A", 2)==0 ){
29262 eputf("Error: cannot mix regular SQL or dot-commands"
29269 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
29271 arDotCommand(&data, 1, argv+i, argc-i);
29276 }else if( cli_strcmp(z,"-safe")==0 ){
29278 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
29282 eputz("Use -help for a list of options.\n");
29289 /* Run all arguments that do not begin with '-' as if they were separate
29290 ** command-line inputs, except for the argToSkip argument which contains
29324 # define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
29328 oputf("SQLite version %s %.19s%s\n" /*extra-version-info*/
29333 printBold("transient in-memory database");
29370 session_close_all(&data, -1);
29390 /* Clear the global data structure so that valgrind will detect memory
29396 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
29411 ** Returns a pointer to the current DB handle.
29418 ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
29458 ** Completely wipes out the contents of the currently-opened database
29473 ** on success, non-0 on error. This function returns 0 on success,
29474 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
29475 ** code from the callback. Note that this is not thread-friendly: it
29490 rc = pFile->pMethods->xFileSize(pFile, &nSize);
29502 rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);