• Home
  • Raw
  • Download

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.
224 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
226 * overridden with the -batch command line option.
284 /* Return the current wall-clock time */
290 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ in timeOfDay()
291 clockVfs->xCurrentTimeInt64(clockVfs, &t); in timeOfDay()
294 clockVfs->xCurrentTime(clockVfs, &r); in timeOfDay()
315 static sqlite3_int64 iBegin; /* Wall-clock time at start */
329 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + in timeDiff()
330 (double)(pEnd->tv_sec - pStart->tv_sec); in timeDiff()
342 (iEnd - iBegin)*0.001, in endTimer()
374 ** does, save off a pointer to it and the current process handle. in hasTimer()
409 return (double) ((i64End - i64Start) / 10000000.0); in timeDiff()
421 (ftWallEnd - ftWallBegin)*0.001, in endTimer()
461 ** in order to translate UTF-8 into MBCS. The following variable is
467 ** The following is the open SQLite database. We make a pointer
474 ** True if an interrupt (Control-C) has been received.
494 ** output from UTF-8 into MBCS.
523 /* Indicate out-of-memory and exit. */
529 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
530 ** out-of-memory error.
564 ** then right-justify the text. W is the width in UTF-8 characters, not
571 int aw = w<0 ? -w : w; in utf8_width_print()
585 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf); in utf8_width_print()
587 utf8_printf(pOut, "%s%*s", zUtf, aw-n, ""); in utf8_width_print()
596 if( *z=='-' || *z=='+' ) z++; in isNumber()
611 if( *z=='+' || *z=='-' ) z++; in isNumber()
621 ** lower 30 bits of a 32-bit signed integer.
626 return 0x3fffffff & (int)(z2 - z); in strlen30()
677 ** the text in memory obtained from malloc() and returns a pointer
694 if( fgets(&zLine[n], nLine - n, in)==0 ){ in local_getline()
703 if( n>0 && zLine[n-1]=='\n' ){ in local_getline()
704 n--; in local_getline()
705 if( n>0 && zLine[n-1]=='\r' ) n--; in local_getline()
712 ** multi-byte characterset characters into UTF-8. */ in local_getline()
766 ** Return the value of a hexadecimal digit. Return -1 if the input
770 if( c>='0' && c<='9' ) return c - '0'; in hexDigitValue()
771 if( c>='a' && c<='f' ) return c - 'a' + 10; in hexDigitValue()
772 if( c>='A' && c<='F' ) return c - 'A' + 10; in hexDigitValue()
773 return -1; in hexDigitValue()
794 if( zArg[0]=='-' ){ in integerValue()
809 v = v*10 + zArg[0] - '0'; in integerValue()
819 return isNeg? -v : v; in integerValue()
839 free(p->z); in freeText()
843 /* zIn is either a pointer to a NULL-terminated string in memory obtained
844 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
856 len = nAppend+p->n+1; in appendText()
864 if( p->z==0 || p->n+len>=p->nAlloc ){ in appendText()
865 p->nAlloc = p->nAlloc*2 + len + 20; in appendText()
866 p->z = realloc(p->z, p->nAlloc); in appendText()
867 shell_check_oom(p->z); in appendText()
871 char *zCsr = p->z+p->n; in appendText()
878 p->n = (int)(zCsr - p->z); in appendText()
881 memcpy(p->z+p->n, zAppend, nAppend); in appendText()
882 p->n += nAppend; in appendText()
883 p->z[p->n] = '\0'; in appendText()
889 ** because it contains non-alphanumeric characters, or because it is an
923 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); in shellFakeSchema()
955 ** Return a fake schema for the table-valued function or eponymous virtual
970 -1, sqlite3_free); in shellModuleSchema()
981 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
1039 sqlite3_result_text(pCtx, z, -1, sqlite3_free); in shellAddSchemaName()
1049 ** The source code for several run-time loadable extensions is inserted
1160 # define BAD_INTPTR_T ((intptr_t)(-1))
1202 ** function. This function is not thread-safe.
1241 ** This function is not thread-safe.
1254 ** The function call to GetEnvironmentVariableA() failed -OR- in windirent_getenv()
1261 ** -AND- the buffer contains the entire value. in windirent_getenv()
1280 /* TODO: Remove this if Unix-style root paths are not used. */ in opendir()
1287 dirp->d_handle = _findfirst(data.name, &data); in opendir()
1289 if( dirp->d_handle==BAD_INTPTR_T ){ in opendir()
1299 if( _findnext(dirp->d_handle, &data)==-1 ){ in opendir()
1308 dirp->d_first.d_attributes = data.attrib; in opendir()
1309 strncpy(dirp->d_first.d_name, data.name, NAME_MAX); in opendir()
1310 dirp->d_first.d_name[NAME_MAX] = '\0'; in opendir()
1325 if( dirp->d_first.d_ino==0 ){ in readdir()
1326 dirp->d_first.d_ino++; in readdir()
1327 dirp->d_next.d_ino++; in readdir()
1329 return &dirp->d_first; in readdir()
1335 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL; in readdir()
1340 dirp->d_next.d_ino++; in readdir()
1341 dirp->d_next.d_attributes = data.attrib; in readdir()
1342 strncpy(dirp->d_next.d_name, data.name, NAME_MAX); in readdir()
1343 dirp->d_next.d_name[NAME_MAX] = '\0'; in readdir()
1345 return &dirp->d_next; in readdir()
1360 if( dirp->d_first.d_ino==0 ){ in readdir_r()
1361 dirp->d_first.d_ino++; in readdir_r()
1362 dirp->d_next.d_ino++; in readdir_r()
1364 entry->d_ino = dirp->d_first.d_ino; in readdir_r()
1365 entry->d_attributes = dirp->d_first.d_attributes; in readdir_r()
1366 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX); in readdir_r()
1367 entry->d_name[NAME_MAX] = '\0'; in readdir_r()
1376 if( _findnext(dirp->d_handle, &data)==-1 ){ in readdir_r()
1384 entry->d_ino = (ino_t)-1; /* not available */ in readdir_r()
1385 entry->d_attributes = data.attrib; in readdir_r()
1386 strncpy(entry->d_name, data.name, NAME_MAX); in readdir_r()
1387 entry->d_name[NAME_MAX] = '\0'; in readdir_r()
1403 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){ in closedir()
1404 result = _findclose(dirp->d_handle); in closedir()
1418 ** 2019-01-21
1437 ** This extension is used to implement the --memtrace option of the
1438 ** command-line shell.
1470 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n", in memtraceRealloc()
1529 ** 2017-03-08
1552 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
1571 ** and whether or not that determination is run-time or compile-time.
1573 ** For best performance, an attempt is made to guess at the byte-order
1574 ** using C-preprocessor macros. If that is unsuccessful, or if
1575 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1576 ** at run-time.
1607 ** A single step of the Keccak mixing function for a 1600-bit state
1628 # define a00 (p->u.s[0]) in KeccakF1600Step()
1629 # define a01 (p->u.s[1]) in KeccakF1600Step()
1630 # define a02 (p->u.s[2]) in KeccakF1600Step()
1631 # define a03 (p->u.s[3]) in KeccakF1600Step()
1632 # define a04 (p->u.s[4]) in KeccakF1600Step()
1633 # define a10 (p->u.s[5]) in KeccakF1600Step()
1634 # define a11 (p->u.s[6]) in KeccakF1600Step()
1635 # define a12 (p->u.s[7]) in KeccakF1600Step()
1636 # define a13 (p->u.s[8]) in KeccakF1600Step()
1637 # define a14 (p->u.s[9]) in KeccakF1600Step()
1638 # define a20 (p->u.s[10]) in KeccakF1600Step()
1639 # define a21 (p->u.s[11]) in KeccakF1600Step()
1640 # define a22 (p->u.s[12]) in KeccakF1600Step()
1641 # define a23 (p->u.s[13]) in KeccakF1600Step()
1642 # define a24 (p->u.s[14]) in KeccakF1600Step()
1643 # define a30 (p->u.s[15]) in KeccakF1600Step()
1644 # define a31 (p->u.s[16]) in KeccakF1600Step()
1645 # define a32 (p->u.s[17]) in KeccakF1600Step()
1646 # define a33 (p->u.s[18]) in KeccakF1600Step()
1647 # define a34 (p->u.s[19]) in KeccakF1600Step()
1648 # define a40 (p->u.s[20]) in KeccakF1600Step()
1649 # define a41 (p->u.s[21]) in KeccakF1600Step()
1650 # define a42 (p->u.s[22]) in KeccakF1600Step()
1651 # define a43 (p->u.s[23]) in KeccakF1600Step()
1652 # define a44 (p->u.s[24]) in KeccakF1600Step()
1653 # define ROL64(a,x) ((a<<x)|(a>>(64-x))) in KeccakF1600Step()
1934 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; in SHA3Init()
1936 p->nRate = (1600 - 2*256)/8; in SHA3Init()
1939 /* Known to be little-endian at compile-time. No-op */ in SHA3Init()
1941 p->ixMask = 7; /* Big-endian */ in SHA3Init()
1947 p->ixMask = 0; in SHA3Init()
1950 p->ixMask = 7; in SHA3Init()
1968 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ in SHA3Update()
1970 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; in SHA3Update()
1971 p->nLoaded += 8; in SHA3Update()
1972 if( p->nLoaded>=p->nRate ){ in SHA3Update()
1974 p->nLoaded = 0; in SHA3Update()
1981 p->u.x[p->nLoaded] ^= aData[i]; in SHA3Update()
1983 p->u.x[p->nLoaded^0x07] ^= aData[i]; in SHA3Update()
1985 p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; in SHA3Update()
1987 p->nLoaded++; in SHA3Update()
1988 if( p->nLoaded==p->nRate ){ in SHA3Update()
1990 p->nLoaded = 0; in SHA3Update()
1997 ** the final hash. The function returns a pointer to the binary
2002 if( p->nLoaded==p->nRate-1 ){ in SHA3Final()
2009 p->nLoaded = p->nRate - 1; in SHA3Final()
2012 for(i=0; i<p->nRate; i++){ in SHA3Final()
2013 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; in SHA3Final()
2015 return &p->u.x[p->nRate]; in SHA3Final()
2023 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
2025 ** For all other non-NULL types of input, X is converted into a UTF-8 string
2044 "384 512", -1); in sha3Func()
2080 ** argument. The results are hashed using a SIZE-bit SHA3. The default
2089 ** F<ieee-float>
2094 ** the size of that text. The SQL text is UTF-8. A single R character
2096 ** I mean an 8-byte little-endian integer <int>. F is a floating point
2097 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
2099 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
2130 "384 512", -1); in sha3QueryFunc()
2137 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); in sha3QueryFunc()
2142 sqlite3_result_error(context, zMsg, -1); in sha3QueryFunc()
2147 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); in sha3QueryFunc()
2149 sqlite3_result_error(context, zMsg, -1); in sha3QueryFunc()
2176 for(j=8; j>=1; j--){ in sha3QueryFunc()
2190 for(j=8; j>=1; j--){ in sha3QueryFunc()
2256 ** 2020-04-14
2282 ** 64-bit machine integer.
2304 x = zA[i] - zB[j]; in uintCollFunc()
2318 return -1; in uintCollFunc()
2332 return (nKey1 - i) - (nKey2 - j); in uintCollFunc()
2351 ** 2020-06-22
2362 ** Routines to implement arbitrary-precision decimal math.
2396 sqlite3_free(p->a); in decimal_clear()
2425 p->sign = 0; in decimal_new()
2426 p->oom = 0; in decimal_new()
2427 p->isInit = 1; in decimal_new()
2428 p->isNull = 0; in decimal_new()
2429 p->nDigit = 0; in decimal_new()
2430 p->nFrac = 0; in decimal_new()
2436 p->a = 0; in decimal_new()
2437 p->isNull = 1; in decimal_new()
2443 p->a = sqlite3_malloc64( n+1 ); in decimal_new()
2444 if( p->a==0 ) goto new_no_mem; in decimal_new()
2446 if( zIn[i]=='-' ){ in decimal_new()
2447 p->sign = 1; in decimal_new()
2456 p->a[p->nDigit++] = c - '0'; in decimal_new()
2458 p->nFrac = p->nDigit + 1; in decimal_new()
2463 if( zIn[j]=='-' ){ in decimal_new()
2471 iExp = iExp*10 + zIn[j] - '0'; in decimal_new()
2475 if( neg ) iExp = -iExp; in decimal_new()
2480 if( p->nFrac ){ in decimal_new()
2481 p->nFrac = p->nDigit - (p->nFrac - 1); in decimal_new()
2484 if( p->nFrac>0 ){ in decimal_new()
2485 if( iExp<=p->nFrac ){ in decimal_new()
2486 p->nFrac -= iExp; in decimal_new()
2489 iExp -= p->nFrac; in decimal_new()
2490 p->nFrac = 0; in decimal_new()
2494 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 ); in decimal_new()
2495 if( p->a==0 ) goto new_no_mem; in decimal_new()
2496 memset(p->a+p->nDigit, 0, iExp); in decimal_new()
2497 p->nDigit += iExp; in decimal_new()
2501 iExp = -iExp; in decimal_new()
2502 nExtra = p->nDigit - p->nFrac - 1; in decimal_new()
2505 p->nFrac += iExp; in decimal_new()
2508 iExp -= nExtra; in decimal_new()
2509 p->nFrac = p->nDigit - 1; in decimal_new()
2513 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 ); in decimal_new()
2514 if( p->a==0 ) goto new_no_mem; in decimal_new()
2515 memmove(p->a+iExp, p->a, p->nDigit); in decimal_new()
2516 memset(p->a, 0, iExp); in decimal_new()
2517 p->nDigit += iExp; in decimal_new()
2518 p->nFrac += iExp; in decimal_new()
2536 if( p==0 || p->oom ){ in decimal_result()
2540 if( p->isNull ){ in decimal_result()
2544 z = sqlite3_malloc( p->nDigit+4 ); in decimal_result()
2550 if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){ in decimal_result()
2551 p->sign = 0; in decimal_result()
2553 if( p->sign ){ in decimal_result()
2554 z[0] = '-'; in decimal_result()
2557 n = p->nDigit - p->nFrac; in decimal_result()
2562 while( n>1 && p->a[j]==0 ){ in decimal_result()
2564 n--; in decimal_result()
2567 z[i++] = p->a[j] + '0'; in decimal_result()
2569 n--; in decimal_result()
2571 if( p->nFrac ){ in decimal_result()
2574 z[i++] = p->a[j] + '0'; in decimal_result()
2576 }while( j<p->nDigit ); in decimal_result()
2605 ** pA->isNull==0
2607 ** pB->isNull==0
2611 if( pA->sign!=pB->sign ){ in decimal_cmp()
2612 return pA->sign ? -1 : +1; in decimal_cmp()
2614 if( pA->sign ){ in decimal_cmp()
2619 nASig = pA->nDigit - pA->nFrac; in decimal_cmp()
2620 nBSig = pB->nDigit - pB->nFrac; in decimal_cmp()
2622 return nASig - nBSig; in decimal_cmp()
2624 n = pA->nDigit; in decimal_cmp()
2625 if( n>pB->nDigit ) n = pB->nDigit; in decimal_cmp()
2626 rc = memcmp(pA->a, pB->a, n); in decimal_cmp()
2628 rc = pA->nDigit - pB->nDigit; in decimal_cmp()
2649 if( pA==0 || pA->isNull ) goto cmp_done; in decimalCmpFunc()
2651 if( pB==0 || pB->isNull ) goto cmp_done; in decimalCmpFunc()
2653 if( rc<0 ) rc = -1; in decimalCmpFunc()
2669 nAddFrac = nFrac - p->nFrac; in decimal_expand()
2670 nAddSig = (nDigit - p->nDigit) - nAddFrac; in decimal_expand()
2672 p->a = sqlite3_realloc64(p->a, nDigit+1); in decimal_expand()
2673 if( p->a==0 ){ in decimal_expand()
2674 p->oom = 1; in decimal_expand()
2678 memmove(p->a+nAddSig, p->a, p->nDigit); in decimal_expand()
2679 memset(p->a, 0, nAddSig); in decimal_expand()
2680 p->nDigit += nAddSig; in decimal_expand()
2683 memset(p->a+p->nDigit, 0, nAddFrac); in decimal_expand()
2684 p->nDigit += nAddFrac; in decimal_expand()
2685 p->nFrac += nAddFrac; in decimal_expand()
2700 if( pA->oom || pB==0 || pB->oom ){ in decimal_add()
2701 pA->oom = 1; in decimal_add()
2704 if( pA->isNull || pB->isNull ){ in decimal_add()
2705 pA->isNull = 1; in decimal_add()
2708 nSig = pA->nDigit - pA->nFrac; in decimal_add()
2709 if( nSig && pA->a[0]==0 ) nSig--; in decimal_add()
2710 if( nSig<pB->nDigit-pB->nFrac ){ in decimal_add()
2711 nSig = pB->nDigit - pB->nFrac; in decimal_add()
2713 nFrac = pA->nFrac; in decimal_add()
2714 if( nFrac<pB->nFrac ) nFrac = pB->nFrac; in decimal_add()
2718 if( pA->oom || pB->oom ){ in decimal_add()
2719 pA->oom = 1; in decimal_add()
2721 if( pA->sign==pB->sign ){ in decimal_add()
2723 for(i=nDigit-1; i>=0; i--){ in decimal_add()
2724 int x = pA->a[i] + pB->a[i] + carry; in decimal_add()
2727 pA->a[i] = x - 10; in decimal_add()
2730 pA->a[i] = x; in decimal_add()
2736 rc = memcmp(pA->a, pB->a, nDigit); in decimal_add()
2738 aA = pB->a; in decimal_add()
2739 aB = pA->a; in decimal_add()
2740 pA->sign = !pA->sign; in decimal_add()
2742 aA = pA->a; in decimal_add()
2743 aB = pB->a; in decimal_add()
2745 for(i=nDigit-1; i>=0; i--){ in decimal_add()
2746 int x = aA[i] - aB[i] - borrow; in decimal_add()
2748 pA->a[i] = x+10; in decimal_add()
2751 pA->a[i] = x; in decimal_add()
2812 pB->sign = !pB->sign; in decimalSubFunc()
2835 if( !p->isInit ){ in decimalSumStep()
2836 p->isInit = 1; in decimalSumStep()
2837 p->a = sqlite3_malloc(2); in decimalSumStep()
2838 if( p->a==0 ){ in decimalSumStep()
2839 p->oom = 1; in decimalSumStep()
2841 p->a[0] = 0; in decimalSumStep()
2843 p->nDigit = 1; in decimalSumStep()
2844 p->nFrac = 0; in decimalSumStep()
2863 if( pArg ) pArg->sign = !pArg->sign; in decimalSumInverse()
2900 if( pA==0 || pA->oom || pA->isNull in decimalMulFunc()
2901 || pB==0 || pB->oom || pB->isNull in decimalMulFunc()
2905 acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 ); in decimalMulFunc()
2910 memset(acc, 0, pA->nDigit + pB->nDigit + 2); in decimalMulFunc()
2911 minFrac = pA->nFrac; in decimalMulFunc()
2912 if( pB->nFrac<minFrac ) minFrac = pB->nFrac; in decimalMulFunc()
2913 for(i=pA->nDigit-1; i>=0; i--){ in decimalMulFunc()
2914 signed char f = pA->a[i]; in decimalMulFunc()
2916 for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){ in decimalMulFunc()
2917 x = acc[k] + f*pB->a[j] + carry; in decimalMulFunc()
2923 acc[k-1] += x/10; in decimalMulFunc()
2925 sqlite3_free(pA->a); in decimalMulFunc()
2926 pA->a = acc; in decimalMulFunc()
2928 pA->nDigit += pB->nDigit + 2; in decimalMulFunc()
2929 pA->nFrac += pB->nFrac; in decimalMulFunc()
2930 pA->sign ^= pB->sign; in decimalMulFunc()
2931 while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){ in decimalMulFunc()
2932 pA->nFrac--; in decimalMulFunc()
2933 pA->nDigit--; in decimalMulFunc()
2989 ** 2013-04-17
3001 ** and input of IEEE754 Binary64 floating-point numbers.
3006 ** In the first form, the value X should be a floating-point number.
3011 ** base-2 exponent of a new floating point number. The function returns
3012 ** a floating-point value equal to Y*pow(2,Z).
3016 ** ieee754(2.0) -> 'ieee754(2,0)'
3017 ** ieee754(45.25) -> 'ieee754(181,-2)'
3018 ** ieee754(2, 0) -> 2.0
3019 ** ieee754(181, -2) -> 45.25
3021 ** Two additional functions break apart the one-argument ieee754()
3024 ** ieee754_mantissa(45.25) -> 181
3025 ** ieee754_exponent(45.25) -> -2
3029 ** ieee754_from_blob(x'3ff0000000000000') -> 1.0
3030 ** ieee754_to_blob(1.0) -> x'3ff0000000000000'
3032 ** In all single-argument functions, if the argument is an 8-byte blob
3033 ** then that blob is interpreted as a big-endian binary64 value.
3037 ** -----------------------------------------------
3050 ** VALUES(-1,'0.5')
3052 ** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
3059 ** ---------------^^^^^---- Replace with whatever you want
3116 r = -r; in ieee754func()
3126 m = a & ((((sqlite3_int64)1)<<52)-1); in ieee754func()
3136 if( isNeg ) m = -m; in ieee754func()
3141 m, e-1075); in ieee754func()
3142 sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT); in ieee754func()
3148 sqlite3_result_int(context, e-1075); in ieee754func()
3158 /* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */ in ieee754func()
3161 }else if( e<-10000 ){ in ieee754func()
3162 e = -10000; in ieee754func()
3167 m = -m; in ieee754func()
3169 }else if( m==0 && e>-1000 && e<1000 ){ in ieee754func()
3179 e--; in ieee754func()
3184 if( 1-e >= 64 ){ in ieee754func()
3187 m >>= 1-e; in ieee754func()
3193 a = m & ((((sqlite3_int64)1)<<52)-1); in ieee754func()
3239 a[sizeof(r)-i] = v&0xff; in ieee754func_to_blob()
3285 ** 2015-08-18
3296 ** This file demonstrates how to create a table-valued-function using
3352 ** series are well-defined.
3368 sqlite3_vtab_cursor base; /* Base class - must be first */
3438 *ppCursor = &pCur->base; in seriesOpen()
3456 if( pCur->isDesc ){ in seriesNext()
3457 pCur->iValue -= pCur->iStep; in seriesNext()
3459 pCur->iValue += pCur->iStep; in seriesNext()
3461 pCur->iRowid++; in seriesNext()
3477 case SERIES_COLUMN_START: x = pCur->mnValue; break; in seriesColumn()
3478 case SERIES_COLUMN_STOP: x = pCur->mxValue; break; in seriesColumn()
3479 case SERIES_COLUMN_STEP: x = pCur->iStep; break; in seriesColumn()
3480 default: x = pCur->iValue; break; in seriesColumn()
3493 *pRowid = pCur->iRowid; in seriesRowid()
3503 if( pCur->isDesc ){ in seriesEof()
3504 return pCur->iValue < pCur->mnValue; in seriesEof()
3506 return pCur->iValue > pCur->mxValue; in seriesEof()
3510 /* True to cause run-time checking of the start=, stop=, and/or step=
3549 pCur->mnValue = sqlite3_value_int64(argv[i++]); in seriesFilter()
3551 pCur->mnValue = 0; in seriesFilter()
3554 pCur->mxValue = sqlite3_value_int64(argv[i++]); in seriesFilter()
3556 pCur->mxValue = 0xffffffff; in seriesFilter()
3559 pCur->iStep = sqlite3_value_int64(argv[i++]); in seriesFilter()
3560 if( pCur->iStep==0 ){ in seriesFilter()
3561 pCur->iStep = 1; in seriesFilter()
3562 }else if( pCur->iStep<0 ){ in seriesFilter()
3563 pCur->iStep = -pCur->iStep; in seriesFilter()
3567 pCur->iStep = 1; in seriesFilter()
3573 pCur->mnValue = 1; in seriesFilter()
3574 pCur->mxValue = 0; in seriesFilter()
3579 pCur->isDesc = 1; in seriesFilter()
3580 pCur->iValue = pCur->mxValue; in seriesFilter()
3581 if( pCur->iStep>0 ){ in seriesFilter()
3582 pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep; in seriesFilter()
3585 pCur->isDesc = 0; in seriesFilter()
3586 pCur->iValue = pCur->mnValue; in seriesFilter()
3588 pCur->iRowid = 1; in seriesFilter()
3603 ** (1) start = $value -- constraint exists
3604 ** (2) stop = $value -- constraint exists
3605 ** (4) step = $value -- constraint exists
3625 aIdx[0] = aIdx[1] = aIdx[2] = -1; in seriesBestIndex()
3626 pConstraint = pIdxInfo->aConstraint; in seriesBestIndex()
3627 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ in seriesBestIndex()
3630 if( pConstraint->iColumn<SERIES_COLUMN_START ) continue; in seriesBestIndex()
3631 iCol = pConstraint->iColumn - SERIES_COLUMN_START; in seriesBestIndex()
3635 if( pConstraint->usable==0 ){ in seriesBestIndex()
3638 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in seriesBestIndex()
3645 pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg; in seriesBestIndex()
3646 pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY; in seriesBestIndex()
3651 ** first argument was omitted. Compile with -DZERO_ARGUMENT_GENERATE_SERIES in seriesBestIndex()
3655 sqlite3_free(pVTab->zErrMsg); in seriesBestIndex()
3656 pVTab->zErrMsg = sqlite3_mprintf( in seriesBestIndex()
3670 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0)); in seriesBestIndex()
3671 pIdxInfo->estimatedRows = 1000; in seriesBestIndex()
3672 if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){ in seriesBestIndex()
3673 if( pIdxInfo->aOrderBy[0].desc ){ in seriesBestIndex()
3678 pIdxInfo->orderByConsumed = 1; in seriesBestIndex()
3684 pIdxInfo->estimatedRows = 2147483647; in seriesBestIndex()
3686 pIdxInfo->idxNum = idxNum; in seriesBestIndex()
3701 seriesOpen, /* xOpen - open a cursor */
3702 seriesClose, /* xClose - close a cursor */
3703 seriesFilter, /* xFilter - configure scan constraints */
3704 seriesNext, /* xNext - advance a cursor */
3705 seriesEof, /* xEof - check for end of scan */
3706 seriesColumn, /* xColumn - read data */
3707 seriesRowid, /* xRowid - read data */
3747 ** 2012-11-13
3759 ** efficient regular-expression matcher for posix extended regular
3781 ** \c C-language escapes for c in afnrtv. ex: \t or \n
3786 ** [a-z] Any single character in the range a-z
3787 ** [^a-z] Any single character not in the range a-z
3789 ** \w Word character. [A-Za-z0-9_]
3790 ** \W Non-word character
3792 ** \D Non-digit
3794 ** \S Non-whitespace character
3800 ** to p copies of X following by q-p copies of X? and that the size of the
3811 ** this file to prevent name collisions with C-library functions of the
3818 /* The end-of-input character */
3820 #define RE_START 0xfffffff /* Start of input - larger than an UTF-8 */
3835 #define RE_OP_WORD 11 /* Perl word character [A-Za-z0-9_] */
3837 #define RE_OP_DIGIT 13 /* digit: [0-9] */
3841 #define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
3911 for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return; in re_add_state()
3912 pSet->aState[pSet->nState++] = (ReStateNumber)newState; in re_add_state()
3922 if( p->i>=p->mx ) return 0; in re_next_char()
3923 c = p->z[p->i++]; in re_next_char()
3925 if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){ in re_next_char()
3926 c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f); in re_next_char()
3928 }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80 in re_next_char()
3929 && (p->z[p->i+1]&0xc0)==0x80 ){ in re_next_char()
3930 c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f); in re_next_char()
3931 p->i += 2; in re_next_char()
3933 }else if( (c&0xf8)==0xf0 && p->i+2<p->mx && (p->z[p->i]&0xc0)==0x80 in re_next_char()
3934 && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){ in re_next_char()
3935 c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6) in re_next_char()
3936 | (p->z[p->i+2]&0x3f); in re_next_char()
3937 p->i += 3; in re_next_char()
3947 if( c>='A' && c<='Z' ) c += 'a' - 'A'; in re_next_char_nocase()
3951 /* Return true if c is a perl "word" character: [A-Za-z0-9_] */
3957 /* Return true if c is a "digit" character: [0-9] */
3967 /* Run a compiled regular expression on the zero-terminated input
3986 if( pRe->nInit ){ in re_match()
3987 unsigned char x = pRe->zInit[0]; in re_match()
3988 while( in.i+pRe->nInit<=in.mx in re_match()
3990 strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0) in re_match()
3994 if( in.i+pRe->nInit>in.mx ) return 0; in re_match()
3995 c = RE_START-1; in re_match()
3998 if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){ in re_match()
4002 pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState ); in re_match()
4003 if( pToFree==0 ) return -1; in re_match()
4006 aStateSet[1].aState = &aStateSet[0].aState[pRe->nState]; in re_match()
4008 pNext->nState = 0; in re_match()
4010 while( c!=RE_EOF && pNext->nState>0 ){ in re_match()
4012 c = pRe->xNextChar(&in); in re_match()
4015 iSwap = 1 - iSwap; in re_match()
4016 pNext->nState = 0; in re_match()
4017 for(i=0; i<pThis->nState; i++){ in re_match()
4018 int x = pThis->aState[i]; in re_match()
4019 switch( pRe->aOp[x] ){ in re_match()
4021 if( pRe->aArg[x]==c ) re_add_state(pNext, x+1); in re_match()
4066 re_add_state(pThis, x+pRe->aArg[x]); in re_match()
4071 re_add_state(pThis, x+pRe->aArg[x]); in re_match()
4080 /* fall-through */ goto re_op_cc_inc; in re_match()
4084 int n = pRe->aArg[x]; in re_match()
4087 if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){ in re_match()
4088 if( pRe->aArg[x+j]==c ){ in re_match()
4090 j = -1; in re_match()
4093 if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){ in re_match()
4095 j = -1; in re_match()
4101 if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit; in re_match()
4108 for(i=0; i<pNext->nState; i++){ in re_match()
4109 int x = pNext->aState[i]; in re_match()
4110 while( pRe->aOp[x]==RE_OP_GOTO ) x += pRe->aArg[x]; in re_match()
4111 if( pRe->aOp[x]==RE_OP_ACCEPT ){ rc = 1; break; } in re_match()
4123 aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0])); in re_resize()
4125 p->aOp = aOp; in re_resize()
4126 aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0])); in re_resize()
4128 p->aArg = aArg; in re_resize()
4129 p->nAlloc = N; in re_resize()
4138 if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0; in re_insert()
4139 for(i=p->nState; i>iBefore; i--){ in re_insert()
4140 p->aOp[i] = p->aOp[i-1]; in re_insert()
4141 p->aArg[i] = p->aArg[i-1]; in re_insert()
4143 p->nState++; in re_insert()
4144 p->aOp[iBefore] = (char)op; in re_insert()
4145 p->aArg[iBefore] = arg; in re_insert()
4152 return re_insert(p, p->nState, op, arg); in re_append()
4159 if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return; in re_copy()
4160 memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0])); in re_copy()
4161 memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0])); in re_copy()
4162 p->nState += N; in re_copy()
4165 /* Return true if c is a hexadecimal digit character: [0-9a-fA-F]
4171 c -= '0'; in re_hex()
4173 c -= 'a' - 10; in re_hex()
4175 c -= 'A' - 10; in re_hex()
4191 if( p->sIn.i>=p->sIn.mx ) return 0; in re_esc_char()
4192 c = p->sIn.z[p->sIn.i]; in re_esc_char()
4193 if( c=='u' && p->sIn.i+4<p->sIn.mx ){ in re_esc_char()
4194 const unsigned char *zIn = p->sIn.z + p->sIn.i; in re_esc_char()
4200 p->sIn.i += 5; in re_esc_char()
4204 if( c=='x' && p->sIn.i+2<p->sIn.mx ){ in re_esc_char()
4205 const unsigned char *zIn = p->sIn.z + p->sIn.i; in re_esc_char()
4209 p->sIn.i += 3; in re_esc_char()
4216 p->sIn.i++; in re_esc_char()
4218 p->zErr = "unknown \\ escape"; in re_esc_char()
4228 return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0; in rePeek()
4233 ** return a pointer to the error message string.
4238 iStart = p->nState; in re_subcompile_re()
4242 iEnd = p->nState; in re_subcompile_re()
4243 re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart); in re_subcompile_re()
4245 p->sIn.i++; in re_subcompile_re()
4248 p->aArg[iGoto] = p->nState - iGoto; in re_subcompile_re()
4254 ** an operand to the "|" operator). Return NULL on success or a pointer
4258 int iPrev = -1; in re_subcompile_string()
4262 while( (c = p->xNextChar(&p->sIn))!=0 ){ in re_subcompile_string()
4263 iStart = p->nState; in re_subcompile_string()
4267 p->sIn.i--; in re_subcompile_string()
4274 p->sIn.i++; in re_subcompile_string()
4280 p->sIn.i++; in re_subcompile_string()
4288 re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1); in re_subcompile_string()
4289 re_append(p, RE_OP_FORK, iPrev - p->nState + 1); in re_subcompile_string()
4294 re_append(p, RE_OP_FORK, iPrev - p->nState); in re_subcompile_string()
4299 re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1); in re_subcompile_string()
4314 while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; } in re_subcompile_string()
4317 p->sIn.i++; in re_subcompile_string()
4319 while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; } in re_subcompile_string()
4323 p->sIn.i++; in re_subcompile_string()
4324 sz = p->nState - iPrev; in re_subcompile_string()
4329 n--; in re_subcompile_string()
4338 re_append(p, RE_OP_FORK, -sz); in re_subcompile_string()
4343 int iFirst = p->nState; in re_subcompile_string()
4346 p->sIn.i++; in re_subcompile_string()
4350 while( (c = p->xNextChar(&p->sIn))!=0 ){ in re_subcompile_string()
4355 if( rePeek(p)=='-' ){ in re_subcompile_string()
4357 p->sIn.i++; in re_subcompile_string()
4358 c = p->xNextChar(&p->sIn); in re_subcompile_string()
4364 if( rePeek(p)==']' ){ p->sIn.i++; break; } in re_subcompile_string()
4367 p->aArg[iFirst] = p->nState - iFirst; in re_subcompile_string()
4382 p->sIn.i++; in re_subcompile_string()
4406 sqlite3_free(pRe->aOp); in re_free()
4407 sqlite3_free(pRe->aArg); in re_free()
4414 ** expression suitable for us by re_match() and return a pointer to the
4429 pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char; in re_compile()
4439 pRe->sIn.z = (unsigned char*)zIn; in re_compile()
4440 pRe->sIn.i = 0; in re_compile()
4441 pRe->sIn.mx = (int)strlen(zIn); in re_compile()
4447 if( pRe->sIn.i>=pRe->sIn.mx ){ in re_compile()
4461 ** unicode characters beyond plane 0 - those are very rare and this is in re_compile()
4463 if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){ in re_compile()
4464 for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){ in re_compile()
4465 unsigned x = pRe->aArg[i]; in re_compile()
4467 pRe->zInit[j++] = (unsigned char)x; in re_compile()
4469 pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6)); in re_compile()
4470 pRe->zInit[j++] = 0x80 | (x&0x3f); in re_compile()
4472 pRe->zInit[j++] = (unsigned char)(0xe0 | (x>>12)); in re_compile()
4473 pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f); in re_compile()
4474 pRe->zInit[j++] = 0x80 | (x&0x3f); in re_compile()
4479 if( j>0 && pRe->zInit[j-1]==0 ) j--; in re_compile()
4480 pRe->nInit = j; in re_compile()
4482 return pRe->zErr; in re_compile()
4487 ** the build-in REGEXP operator. The first argument to the function is the
4513 sqlite3_result_error(context, zErr, -1); in re_sql_func()
4524 sqlite3_result_int(context, re_match(pRe, zStr, -1)); in re_sql_func()
4534 ** if the SQLITE_DEBUG compile-time option is used.
4557 sqlite3_result_error(context, zErr, -1); in re_bytecode_func()
4566 if( pRe->nInit>0 ){ in re_bytecode_func()
4568 for(i=0; i<pRe->nInit; i++){ in re_bytecode_func()
4569 sqlite3_str_appendf(pStr, "%02x", pRe->zInit[i]); in re_bytecode_func()
4573 for(i=0; (unsigned)i<pRe->nState; i++){ in re_bytecode_func()
4574 sqlite3_str_appendf(pStr, "%-8s %4d\n", in re_bytecode_func()
4575 ReOpName[(unsigned char)pRe->aOp[i]], pRe->aArg[i]); in re_bytecode_func()
4582 sqlite3_result_text(context, z, n-1, sqlite3_free); in re_bytecode_func()
4611 /* The regexpi(PATTERN,STRING) function is a case-insensitive version in sqlite3_regexp_init()
4631 ** 2014-06-13
4651 ** If the first option argument - MODE - is present, then it must
4668 ** as an integer - the number of seconds since the unix epoch. The
4669 ** modification-time of the target file is set to this value before
4700 ** If a non-NULL value is specified for the optional $dir parameter and
4707 ** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
4744 ** Structure of the fsdir() table-valued function
4783 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1); in readFileContents()
4830 sqlite3_result_error(ctx, zMsg, -1); in ctxErrorMsg()
4838 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
4856 fileIntervals.LowPart = pFileTime->dwLowDateTime; in fileTimeToUnixTime()
4857 fileIntervals.HighPart = pFileTime->dwHighDateTime; in fileTimeToUnixTime()
4859 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000; in fileTimeToUnixTime()
4869 int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0); in utf8_to_utf16()
4871 if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) ) in utf8_to_utf16()
4896 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime); in statTimesToUtc()
4897 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime); in statTimesToUtc()
4898 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime); in statTimesToUtc()
4998 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */ in writeFile()
5011 ** be an error though - if there is already a directory at the same in writeFile()
5116 sqlite3_int64 mtime = -1; in writefileFunc()
5120 "wrong number of arguments to function writefile()", -1 in writefileFunc()
5155 ** Given a numberic st_mode from stat(), convert it into a human-readable
5156 ** text string in the style of "ls -l".
5170 z[0] = '-'; in lsModeFunc()
5177 int m = (iMode >> ((2-i)*3)); in lsModeFunc()
5179 a[0] = (m & 0x4) ? 'r' : '-'; in lsModeFunc()
5180 a[1] = (m & 0x2) ? 'w' : '-'; in lsModeFunc()
5181 a[2] = (m & 0x1) ? 'x' : '-'; in lsModeFunc()
5184 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); in lsModeFunc()
5197 char *zDir; /* Name of directory (nul-terminated) */
5201 sqlite3_vtab_cursor base; /* Base class - must be first */
5217 sqlite3_vtab base; /* Base class - must be first */
5264 pCur->iLvl = -1; in fsdirOpen()
5265 *ppCursor = &pCur->base; in fsdirOpen()
5275 for(i=0; i<=pCur->iLvl; i++){ in fsdirResetCursor()
5276 FsdirLevel *pLvl = &pCur->aLvl[i]; in fsdirResetCursor()
5277 if( pLvl->pDir ) closedir(pLvl->pDir); in fsdirResetCursor()
5278 sqlite3_free(pLvl->zDir); in fsdirResetCursor()
5280 sqlite3_free(pCur->zPath); in fsdirResetCursor()
5281 sqlite3_free(pCur->aLvl); in fsdirResetCursor()
5282 pCur->aLvl = 0; in fsdirResetCursor()
5283 pCur->zPath = 0; in fsdirResetCursor()
5284 pCur->zBase = 0; in fsdirResetCursor()
5285 pCur->nBase = 0; in fsdirResetCursor()
5286 pCur->nLvl = 0; in fsdirResetCursor()
5287 pCur->iLvl = -1; in fsdirResetCursor()
5288 pCur->iRowid = 1; in fsdirResetCursor()
5309 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); in fsdirSetErrmsg()
5319 mode_t m = pCur->sStat.st_mode; in fsdirNext()
5321 pCur->iRowid++; in fsdirNext()
5324 int iNew = pCur->iLvl + 1; in fsdirNext()
5326 if( iNew>=pCur->nLvl ){ in fsdirNext()
5329 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte); in fsdirNext()
5331 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl)); in fsdirNext()
5332 pCur->aLvl = aNew; in fsdirNext()
5333 pCur->nLvl = nNew; in fsdirNext()
5335 pCur->iLvl = iNew; in fsdirNext()
5336 pLvl = &pCur->aLvl[iNew]; in fsdirNext()
5338 pLvl->zDir = pCur->zPath; in fsdirNext()
5339 pCur->zPath = 0; in fsdirNext()
5340 pLvl->pDir = opendir(pLvl->zDir); in fsdirNext()
5341 if( pLvl->pDir==0 ){ in fsdirNext()
5342 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath); in fsdirNext()
5347 while( pCur->iLvl>=0 ){ in fsdirNext()
5348 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl]; in fsdirNext()
5349 struct dirent *pEntry = readdir(pLvl->pDir); in fsdirNext()
5351 if( pEntry->d_name[0]=='.' ){ in fsdirNext()
5352 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue; in fsdirNext()
5353 if( pEntry->d_name[1]=='\0' ) continue; in fsdirNext()
5355 sqlite3_free(pCur->zPath); in fsdirNext()
5356 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name); in fsdirNext()
5357 if( pCur->zPath==0 ) return SQLITE_NOMEM; in fsdirNext()
5358 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ in fsdirNext()
5359 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); in fsdirNext()
5364 closedir(pLvl->pDir); in fsdirNext()
5365 sqlite3_free(pLvl->zDir); in fsdirNext()
5366 pLvl->pDir = 0; in fsdirNext()
5367 pLvl->zDir = 0; in fsdirNext()
5368 pCur->iLvl--; in fsdirNext()
5372 sqlite3_free(pCur->zPath); in fsdirNext()
5373 pCur->zPath = 0; in fsdirNext()
5389 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT); in fsdirColumn()
5394 sqlite3_result_int64(ctx, pCur->sStat.st_mode); in fsdirColumn()
5398 sqlite3_result_int64(ctx, pCur->sStat.st_mtime); in fsdirColumn()
5402 mode_t m = pCur->sStat.st_mode; in fsdirColumn()
5413 n = readlink(pCur->zPath, aBuf, nBuf); in fsdirColumn()
5428 readFileContents(ctx, pCur->zPath); in fsdirColumn()
5448 *pRowid = pCur->iRowid; in fsdirRowid()
5458 return (pCur->zPath==0); in fsdirEof()
5485 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument"); in fsdirFilter()
5489 pCur->zBase = (const char*)sqlite3_value_text(argv[1]); in fsdirFilter()
5491 if( pCur->zBase ){ in fsdirFilter()
5492 pCur->nBase = (int)strlen(pCur->zBase)+1; in fsdirFilter()
5493 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir); in fsdirFilter()
5495 pCur->zPath = sqlite3_mprintf("%s", zDir); in fsdirFilter()
5498 if( pCur->zPath==0 ){ in fsdirFilter()
5501 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ in fsdirFilter()
5502 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); in fsdirFilter()
5528 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */ in fsdirBestIndex()
5529 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */ in fsdirBestIndex()
5535 pConstraint = pIdxInfo->aConstraint; in fsdirBestIndex()
5536 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ in fsdirBestIndex()
5537 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; in fsdirBestIndex()
5538 switch( pConstraint->iColumn ){ in fsdirBestIndex()
5540 if( pConstraint->usable ){ in fsdirBestIndex()
5549 if( pConstraint->usable ){ in fsdirBestIndex()
5565 pIdxInfo->idxNum = 0; in fsdirBestIndex()
5566 /* The pIdxInfo->estimatedCost should have been initialized to a huge in fsdirBestIndex()
5568 pIdxInfo->estimatedRows = 0x7fffffff; in fsdirBestIndex()
5570 pIdxInfo->aConstraintUsage[idxPath].omit = 1; in fsdirBestIndex()
5571 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1; in fsdirBestIndex()
5573 pIdxInfo->aConstraintUsage[idxDir].omit = 1; in fsdirBestIndex()
5574 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2; in fsdirBestIndex()
5575 pIdxInfo->idxNum = 2; in fsdirBestIndex()
5576 pIdxInfo->estimatedCost = 10.0; in fsdirBestIndex()
5578 pIdxInfo->idxNum = 1; in fsdirBestIndex()
5579 pIdxInfo->estimatedCost = 100.0; in fsdirBestIndex()
5597 fsdirOpen, /* xOpen - open a cursor */ in fsdirRegister()
5598 fsdirClose, /* xClose - close a cursor */ in fsdirRegister()
5599 fsdirFilter, /* xFilter - configure scan constraints */ in fsdirRegister()
5600 fsdirNext, /* xNext - advance a cursor */ in fsdirRegister()
5601 fsdirEof, /* xEof - check for end of scan */ in fsdirRegister()
5602 fsdirColumn, /* xColumn - read data */ in fsdirRegister()
5603 fsdirRowid, /* xRowid - read data */ in fsdirRegister()
5639 rc = sqlite3_create_function(db, "writefile", -1, in sqlite3_fileio_init()
5664 ** 2017-07-10
5711 sqlite3_vtab base; /* Base class - must be first */
5721 sqlite3_vtab_cursor base; /* Base class - must be first */
5731 int j; /* inter-phase counter */
5781 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */ in completionConnect()
5796 pNew->db = db; in completionConnect()
5817 pCur->db = ((completion_vtab*)p)->db; in completionOpen()
5818 *ppCursor = &pCur->base; in completionOpen()
5826 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0; in completionCursorReset()
5827 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0; in completionCursorReset()
5828 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0; in completionCursorReset()
5829 pCur->j = 0; in completionCursorReset()
5844 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
5845 ** record the current state of the scan. This routine sets ->zCurrentRow
5847 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
5858 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */ in completionNext()
5859 pCur->iRowid++; in completionNext()
5860 while( pCur->ePhase!=COMPLETION_EOF ){ in completionNext()
5861 switch( pCur->ePhase ){ in completionNext()
5863 if( pCur->j >= sqlite3_keyword_count() ){ in completionNext()
5864 pCur->zCurrentRow = 0; in completionNext()
5865 pCur->ePhase = COMPLETION_DATABASES; in completionNext()
5867 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow); in completionNext()
5869 iCol = -1; in completionNext()
5873 if( pCur->pStmt==0 ){ in completionNext()
5874 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, in completionNext()
5875 &pCur->pStmt, 0); in completionNext()
5882 if( pCur->pStmt==0 ){ in completionNext()
5886 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); in completionNext()
5898 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); in completionNext()
5906 if( pCur->pStmt==0 ){ in completionNext()
5910 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); in completionNext()
5924 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); in completionNext()
5934 if( pCur->zCurrentRow==0 ) continue; in completionNext()
5936 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){ in completionNext()
5938 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol); in completionNext()
5939 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol); in completionNext()
5942 sqlite3_finalize(pCur->pStmt); in completionNext()
5943 pCur->pStmt = 0; in completionNext()
5944 pCur->ePhase = eNextPhase; in completionNext()
5948 if( pCur->nPrefix==0 ) break; in completionNext()
5949 if( pCur->nPrefix<=pCur->szRow in completionNext()
5950 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 in completionNext()
5971 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT); in completionColumn()
5975 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT); in completionColumn()
5979 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT); in completionColumn()
5983 sqlite3_result_int(ctx, pCur->ePhase); in completionColumn()
5996 *pRowid = pCur->iRowid; in completionRowid()
6006 return pCur->ePhase >= COMPLETION_EOF; in completionEof()
6026 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]); in completionFilter()
6027 if( pCur->nPrefix>0 ){ in completionFilter()
6028 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); in completionFilter()
6029 if( pCur->zPrefix==0 ) return SQLITE_NOMEM; in completionFilter()
6034 pCur->nLine = sqlite3_value_bytes(argv[iArg]); in completionFilter()
6035 if( pCur->nLine>0 ){ in completionFilter()
6036 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); in completionFilter()
6037 if( pCur->zLine==0 ) return SQLITE_NOMEM; in completionFilter()
6040 if( pCur->zLine!=0 && pCur->zPrefix==0 ){ in completionFilter()
6041 int i = pCur->nLine; in completionFilter()
6042 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){ in completionFilter()
6043 i--; in completionFilter()
6045 pCur->nPrefix = pCur->nLine - i; in completionFilter()
6046 if( pCur->nPrefix>0 ){ in completionFilter()
6047 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i); in completionFilter()
6048 if( pCur->zPrefix==0 ) return SQLITE_NOMEM; in completionFilter()
6051 pCur->iRowid = 0; in completionFilter()
6052 pCur->ePhase = COMPLETION_FIRST_PHASE; in completionFilter()
6062 ** There are two hidden parameters that act as arguments to the table-valued
6072 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */ in completionBestIndex()
6073 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */ in completionBestIndex()
6078 pConstraint = pIdxInfo->aConstraint; in completionBestIndex()
6079 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){ in completionBestIndex()
6080 if( pConstraint->usable==0 ) continue; in completionBestIndex()
6081 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; in completionBestIndex()
6082 switch( pConstraint->iColumn ){ in completionBestIndex()
6094 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg; in completionBestIndex()
6095 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1; in completionBestIndex()
6098 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg; in completionBestIndex()
6099 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1; in completionBestIndex()
6101 pIdxInfo->idxNum = idxNum; in completionBestIndex()
6102 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg; in completionBestIndex()
6103 pIdxInfo->estimatedRows = 500 - 100*nArg; in completionBestIndex()
6118 completionOpen, /* xOpen - open a cursor */
6119 completionClose, /* xClose - close a cursor */
6120 completionFilter, /* xFilter - configure scan constraints */
6121 completionNext, /* xNext - advance a cursor */
6122 completionEof, /* xEof - check for end of scan */
6123 completionColumn, /* xColumn - read data */
6124 completionRowid, /* xRowid - read data */
6168 ** 2017-10-20
6196 ** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
6213 ** this shim is a pass-through into the default underlying VFS. (rule 3)
6222 ** Start-Of-SQLite3-NNNNNNNN
6225 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
6228 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
6234 ** Maximum size of the combined prefix + database + append-mark. This
6245 #define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1))
6254 /* Access to a lower-level VFS that (might) implement dynamic loading,
6257 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
6266 ** append-mark.
6270 ** +-------------+---------+----------+-------------+
6271 ** | prefix-file | padding | database | append-mark |
6272 ** +-------------+---------+----------+-------------+
6278 ** "prefix file" - file onto which the database has been appended.
6279 ** "padding" - zero or more bytes inserted so that "database"
6281 ** "database" - The SQLite database file
6282 ** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
6283 ** the offset from the start of prefix-file to the start
6286 ** The size of the database is iMark - iPgOne.
6288 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
6289 ** of iPgOne stored as a big-ending 64-bit integer.
6292 ** Or, iMark is -1 to indicate that it has not yet been written.
6297 sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */
6391 ** Close an apnd-file.
6395 return pFile->pMethods->xClose(pFile); in apndClose()
6399 ** Read data from an apnd-file.
6409 return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst); in apndRead()
6413 ** Add the append-mark onto what should become the end of the file.
6415 * Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
6422 sqlite_int64 iPgOne = paf->iPgOne; in apndWriteMark()
6428 while( --i >= 0 ){ in apndWriteMark()
6432 iWriteEnd += paf->iPgOne; in apndWriteMark()
6433 if( SQLITE_OK==(rc = pFile->pMethods->xWrite in apndWriteMark()
6435 paf->iMark = iWriteEnd; in apndWriteMark()
6441 ** Write data to an apnd-file.
6453 /* If append-mark is absent or will be overwritten, write it. */ in apndWrite()
6454 if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){ in apndWrite()
6458 return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst); in apndWrite()
6462 ** Truncate an apnd-file.
6470 return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE); in apndTruncate()
6474 ** Sync an apnd-file.
6478 return pFile->pMethods->xSync(pFile, flags); in apndSync()
6482 ** Return the current file-size of an apnd-file.
6483 ** If the append mark is not yet there, the file-size is 0.
6487 *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0; in apndFileSize()
6492 ** Lock an apnd-file.
6496 return pFile->pMethods->xLock(pFile, eLock); in apndLock()
6500 ** Unlock an apnd-file.
6504 return pFile->pMethods->xUnlock(pFile, eLock); in apndUnlock()
6508 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
6512 return pFile->pMethods->xCheckReservedLock(pFile, pResOut); in apndCheckReservedLock()
6516 ** File control method. For custom operations on an apnd-file.
6522 if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne; in apndFileControl()
6523 rc = pFile->pMethods->xFileControl(pFile, op, pArg); in apndFileControl()
6525 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg); in apndFileControl()
6531 ** Return the sector-size in bytes for an apnd-file.
6535 return pFile->pMethods->xSectorSize(pFile); in apndSectorSize()
6539 ** Return the device characteristic flags supported by an apnd-file.
6543 return pFile->pMethods->xDeviceCharacteristics(pFile); in apndDeviceCharacteristics()
6555 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp); in apndShmMap()
6558 /* Perform locking on a shared-memory segment */
6561 return pFile->pMethods->xShmLock(pFile,offset,n,flags); in apndShmLock()
6567 pFile->pMethods->xShmBarrier(pFile); in apndShmBarrier()
6573 return pFile->pMethods->xShmUnmap(pFile,deleteFlag); in apndShmUnmap()
6576 /* Fetch a page of a memory-mapped file */
6584 if( p->iMark < 0 || iOfst+iAmt > p->iMark ){ in apndFetch()
6588 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp); in apndFetch()
6591 /* Release a memory-mapped page */
6595 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage); in apndUnfetch()
6599 ** Try to read the append-mark off the end of a file. Return the
6600 ** start of the appended database if the append-mark is present.
6601 ** If there is no valid append-mark, return -1;
6603 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
6605 ** start-of-database value must be a multiple of 512.
6610 int msbs = 8 * (APND_MARK_FOS_SZ-1); in apndReadMark()
6613 if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1; in apndReadMark()
6614 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE); in apndReadMark()
6615 if( rc ) return -1; in apndReadMark()
6616 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1; in apndReadMark()
6619 msbs -= 8; in apndReadMark()
6622 if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1; in apndReadMark()
6623 if( iMark & 0x1ff ) return -1; in apndReadMark()
6637 /* If file has the correct end-marker, the expected odd size, and the in apndIsAppendvfsDatabase()
6638 ** SQLite DB type marker where the end-marker puts it, then it in apndIsAppendvfsDatabase()
6641 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark); in apndIsAppendvfsDatabase()
6661 || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0) in apndIsOrdinaryDatabaseFile()
6690 return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags); in apndOpen()
6693 pFile->pMethods = &apnd_io_methods; in apndOpen()
6694 pApndFile->iMark = -1; /* Append mark not yet written */ in apndOpen()
6696 rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags); in apndOpen()
6698 rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz); in apndOpen()
6700 pBaseFile->pMethods->xClose(pBaseFile); in apndOpen()
6704 pFile->pMethods = 0; in apndOpen()
6709 ** the base dispatch-table so this instance mimics the base VFS. in apndOpen()
6711 memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile); in apndOpen()
6714 pApndFile->iPgOne = apndReadMark(sz, pFile); in apndOpen()
6715 if( pApndFile->iPgOne>=0 ){ in apndOpen()
6716 pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */ in apndOpen()
6720 pBaseFile->pMethods->xClose(pBaseFile); in apndOpen()
6722 pFile->pMethods = 0; in apndOpen()
6727 ** Until then, paf->iMark value indicates it is not yet written. in apndOpen()
6729 pApndFile->iPgOne = APND_START_ROUNDUP(sz); in apndOpen()
6741 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync); in apndDelete()
6745 ** All other VFS methods are pass-thrus.
6753 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut); in apndAccess()
6761 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut); in apndFullPathname()
6764 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); in apndDlOpen()
6767 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); in apndDlError()
6770 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); in apndDlSym()
6773 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); in apndDlClose()
6776 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); in apndRandomness()
6779 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); in apndSleep()
6782 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); in apndCurrentTime()
6785 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); in apndGetLastError()
6788 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); in apndCurrentTimeInt64()
6795 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall); in apndSetSystemCall()
6801 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName); in apndGetSystemCall()
6804 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName); in apndNextSystemCall()
6827 apnd_vfs.iVersion = pOrig->iVersion; in sqlite3_appendvfs_init()
6829 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile); in sqlite3_appendvfs_init()
6845 ** 2017-12-26
6898 /* typedef UINT32_TYPE u32; // 4-byte unsigned integer // */
6899 /* typedef UINT16_TYPE u16; // 2-byte unsigned integer // */
6957 ** Use this value for the "version-made-by" field in new zip file
6960 ** This is what info-zip seems to do.
6963 ** Value for "version-required-to-extract" field of new entries.
6967 ** Value for "general-purpose-bit-flags" field of new entries. Bit
6968 ** 11 means "utf-8 filename and comment".
6988 ** The sizes of the fixed-size part of each of the three main data
7035 *** crc-32 4 bytes
7076 *** crc-32 4 bytes
7104 ZipfileEntry *pNext; /* Next element in in-memory CDS */
7112 sqlite3_vtab_cursor base; /* Base class - must be first */
7115 u8 bNoop; /* If next xNext() call is no-op */
7129 sqlite3_vtab base; /* Base class - must be first */
7154 sqlite3_result_error(ctx, zMsg, -1); in zipfileCtxErrorMsg()
7181 ** argv[0] -> module name ("zipfile")
7182 ** argv[1] -> database name
7183 ** argv[2] -> table name
7184 ** argv[...] -> "column name" and other module argument fields.
7224 pNew->db = db; in zipfileConnect()
7225 pNew->aBuffer = (u8*)&pNew[1]; in zipfileConnect()
7227 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE]; in zipfileConnect()
7228 memcpy(pNew->zFile, zFile, nFile); in zipfileConnect()
7229 zipfileDequote(pNew->zFile); in zipfileConnect()
7242 sqlite3_free(p->cds.zFile); in zipfileEntryFree()
7255 if( pTab->pWriteFd ){ in zipfileCleanupTransaction()
7256 fclose(pTab->pWriteFd); in zipfileCleanupTransaction()
7257 pTab->pWriteFd = 0; in zipfileCleanupTransaction()
7259 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){ in zipfileCleanupTransaction()
7260 pNext = pEntry->pNext; in zipfileCleanupTransaction()
7263 pTab->pFirstEntry = 0; in zipfileCleanupTransaction()
7264 pTab->pLastEntry = 0; in zipfileCleanupTransaction()
7265 pTab->szCurrent = 0; in zipfileCleanupTransaction()
7266 pTab->szOrig = 0; in zipfileCleanupTransaction()
7290 pCsr->iId = ++pTab->iNextCsrid; in zipfileOpen()
7291 pCsr->pCsrNext = pTab->pCsrList; in zipfileOpen()
7292 pTab->pCsrList = pCsr; in zipfileOpen()
7304 pCsr->bEof = 0; in zipfileResetCursor()
7305 if( pCsr->pFile ){ in zipfileResetCursor()
7306 fclose(pCsr->pFile); in zipfileResetCursor()
7307 pCsr->pFile = 0; in zipfileResetCursor()
7308 zipfileEntryFree(pCsr->pCurrent); in zipfileResetCursor()
7309 pCsr->pCurrent = 0; in zipfileResetCursor()
7312 for(p=pCsr->pFreeEntry; p; p=pNext){ in zipfileResetCursor()
7313 pNext = p->pNext; in zipfileResetCursor()
7323 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab); in zipfileClose()
7328 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext)); in zipfileClose()
7329 *pp = pCsr->pCsrNext; in zipfileClose()
7342 sqlite3_free(pTab->base.zErrMsg); in zipfileTableErr()
7343 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap); in zipfileTableErr()
7349 sqlite3_free(pCsr->base.pVtab->zErrMsg); in zipfileCursorErr()
7350 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); in zipfileCursorErr()
7388 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET); in zipfileAppendData()
7389 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd); in zipfileAppendData()
7391 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()"); in zipfileAppendData()
7394 pTab->szCurrent += nWrite; in zipfileAppendData()
7400 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
7407 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
7418 ** Write a 16-bit little endiate integer into buffer aBuf.
7426 ** Write a 32-bit little endiate integer into buffer aBuf.
7435 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
7436 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
7449 ** if the record is not well-formed, or SQLITE_OK otherwise.
7458 pCDS->iVersionMadeBy = zipfileRead16(aRead); in zipfileReadCDS()
7459 pCDS->iVersionExtract = zipfileRead16(aRead); in zipfileReadCDS()
7460 pCDS->flags = zipfileRead16(aRead); in zipfileReadCDS()
7461 pCDS->iCompression = zipfileRead16(aRead); in zipfileReadCDS()
7462 pCDS->mTime = zipfileRead16(aRead); in zipfileReadCDS()
7463 pCDS->mDate = zipfileRead16(aRead); in zipfileReadCDS()
7464 pCDS->crc32 = zipfileRead32(aRead); in zipfileReadCDS()
7465 pCDS->szCompressed = zipfileRead32(aRead); in zipfileReadCDS()
7466 pCDS->szUncompressed = zipfileRead32(aRead); in zipfileReadCDS()
7468 pCDS->nFile = zipfileRead16(aRead); in zipfileReadCDS()
7469 pCDS->nExtra = zipfileRead16(aRead); in zipfileReadCDS()
7470 pCDS->nComment = zipfileRead16(aRead); in zipfileReadCDS()
7471 pCDS->iDiskStart = zipfileRead16(aRead); in zipfileReadCDS()
7472 pCDS->iInternalAttr = zipfileRead16(aRead); in zipfileReadCDS()
7473 pCDS->iExternalAttr = zipfileRead32(aRead); in zipfileReadCDS()
7474 pCDS->iOffset = zipfileRead32(aRead); in zipfileReadCDS()
7483 ** if the record is not well-formed, or SQLITE_OK otherwise.
7496 pLFH->iVersionExtract = zipfileRead16(aRead); in zipfileReadLFH()
7497 pLFH->flags = zipfileRead16(aRead); in zipfileReadLFH()
7498 pLFH->iCompression = zipfileRead16(aRead); in zipfileReadLFH()
7499 pLFH->mTime = zipfileRead16(aRead); in zipfileReadLFH()
7500 pLFH->mDate = zipfileRead16(aRead); in zipfileReadLFH()
7501 pLFH->crc32 = zipfileRead32(aRead); in zipfileReadLFH()
7502 pLFH->szCompressed = zipfileRead32(aRead); in zipfileReadLFH()
7503 pLFH->szUncompressed = zipfileRead32(aRead); in zipfileReadLFH()
7504 pLFH->nFile = zipfileRead16(aRead); in zipfileReadLFH()
7505 pLFH->nExtra = zipfileRead16(aRead); in zipfileReadLFH()
7513 ** Scan through this buffer to find an "extra-timestamp" field. If one
7514 ** exists, extract the 32-bit modification-timestamp from it and store
7517 ** Zero is returned if no extra-timestamp record could be found (and so
7518 ** *pmTime is left unchanged), or non-zero otherwise.
7538 if( b & 0x01 ){ /* 0x01 -> modtime is present */ in zipfileScanExtra()
7552 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
7553 ** fields of the CDS structure passed as the only argument to a 32-bit
7554 ** UNIX seconds-since-the-epoch timestamp. Return the result.
7556 ** "Standard" MS-DOS time format:
7559 ** Bits 00-04: seconds divided by 2
7560 ** Bits 05-10: minute
7561 ** Bits 11-15: hour
7563 ** Bits 00-04: day
7564 ** Bits 05-08: month (1-12)
7565 ** Bits 09-15: years from 1980
7567 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
7572 Y = (1980 + ((pCDS->mDate >> 9) & 0x7F)); in zipfileMtime()
7573 M = ((pCDS->mDate >> 5) & 0x0F); in zipfileMtime()
7574 D = (pCDS->mDate & 0x1F); in zipfileMtime()
7575 sec = (pCDS->mTime & 0x1F)*2; in zipfileMtime()
7576 min = (pCDS->mTime >> 5) & 0x3F; in zipfileMtime()
7577 hr = (pCDS->mTime >> 11) & 0x1F; in zipfileMtime()
7579 Y--; in zipfileMtime()
7585 B = 2 - A + (A/4); in zipfileMtime()
7586 JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec; in zipfileMtime()
7587 return (u32)(JDsec - (i64)24405875*(i64)8640); in zipfileMtime()
7603 A = (int)((JD - 1867216.25)/36524.25); in zipfileMtimeToDos()
7604 A = (int)(JD + 1 + A - (A/4)); in zipfileMtimeToDos()
7606 C = (int)((B - 122.1)/365.25); in zipfileMtimeToDos()
7608 E = (int)((B-D)/30.6001); in zipfileMtimeToDos()
7610 day = B - D - (int)(30.6001*E); in zipfileMtimeToDos()
7611 mon = (E<14 ? E-1 : E-13); in zipfileMtimeToDos()
7612 yr = mon>2 ? C-4716 : C-4715; in zipfileMtimeToDos()
7619 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9)); in zipfileMtimeToDos()
7620 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11)); in zipfileMtimeToDos()
7622 pCds->mDate = pCds->mTime = 0; in zipfileMtimeToDos()
7627 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) in zipfileMtimeToDos()
7633 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
7635 ** then pFile is a file-handle open on a zip file. In either case, this
7645 const u8 *aBlob, /* Pointer to in-memory file image */ in zipfileGetEntry()
7649 ZipfileEntry **ppEntry /* OUT: Pointer to new object */ in zipfileGetEntry()
7652 char **pzErr = &pTab->base.zErrMsg; in zipfileGetEntry()
7656 aRead = pTab->aBuffer; in zipfileGetEntry()
7680 rc = zipfileReadCDS(aRead, &pNew->cds); in zipfileGetEntry()
7693 u32 *pt = &pNew->mUnixTime; in zipfileGetEntry()
7694 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); in zipfileGetEntry()
7695 pNew->aExtra = (u8*)&pNew[1]; in zipfileGetEntry()
7696 memcpy(pNew->aExtra, &aRead[nFile], nExtra); in zipfileGetEntry()
7697 if( pNew->cds.zFile==0 ){ in zipfileGetEntry()
7699 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){ in zipfileGetEntry()
7700 pNew->mUnixTime = zipfileMtime(&pNew->cds); in zipfileGetEntry()
7708 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr); in zipfileGetEntry()
7710 aRead = (u8*)&aBlob[pNew->cds.iOffset]; in zipfileGetEntry()
7715 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ; in zipfileGetEntry()
7716 pNew->iDataOff += lfh.nFile + lfh.nExtra; in zipfileGetEntry()
7717 if( aBlob && pNew->cds.szCompressed ){ in zipfileGetEntry()
7718 pNew->aData = &pNew->aExtra[nExtra]; in zipfileGetEntry()
7719 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed); in zipfileGetEntry()
7723 (int)pNew->cds.iOffset in zipfileGetEntry()
7745 if( pCsr->pFile ){ in zipfileNext()
7746 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize; in zipfileNext()
7747 zipfileEntryFree(pCsr->pCurrent); in zipfileNext()
7748 pCsr->pCurrent = 0; in zipfileNext()
7749 if( pCsr->iNextOff>=iEof ){ in zipfileNext()
7750 pCsr->bEof = 1; in zipfileNext()
7753 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab); in zipfileNext()
7754 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p); in zipfileNext()
7756 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ; in zipfileNext()
7757 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment; in zipfileNext()
7759 pCsr->pCurrent = p; in zipfileNext()
7762 if( !pCsr->bNoop ){ in zipfileNext()
7763 pCsr->pCurrent = pCsr->pCurrent->pNext; in zipfileNext()
7765 if( pCsr->pCurrent==0 ){ in zipfileNext()
7766 pCsr->bEof = 1; in zipfileNext()
7770 pCsr->bNoop = 0; in zipfileNext()
7804 err = inflateInit2(&str, -15); in zipfileInflate()
7829 ** code is returned and an error message left in virtual-table handle
7846 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); in zipfileDeflate()
7882 ZipfileCDS *pCDS = &pCsr->pCurrent->cds; in zipfileColumn()
7886 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT); in zipfileColumn()
7891 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16); in zipfileColumn()
7894 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime); in zipfileColumn()
7899 sqlite3_result_int64(ctx, pCDS->szUncompressed); in zipfileColumn()
7906 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){ in zipfileColumn()
7907 int sz = pCDS->szCompressed; in zipfileColumn()
7908 int szFinal = pCDS->szUncompressed; in zipfileColumn()
7912 if( pCsr->pCurrent->aData ){ in zipfileColumn()
7913 aBuf = pCsr->pCurrent->aData; in zipfileColumn()
7919 FILE *pFile = pCsr->pFile; in zipfileColumn()
7921 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd; in zipfileColumn()
7923 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff, in zipfileColumn()
7924 &pCsr->base.pVtab->zErrMsg in zipfileColumn()
7929 if( i==5 && pCDS->iCompression ){ in zipfileColumn()
7937 /* Figure out if this is a directory or a zero-sized file. Consider in zipfileColumn()
7940 u32 mode = pCDS->iExternalAttr >> 16; in zipfileColumn()
7941 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){ in zipfileColumn()
7949 sqlite3_result_int(ctx, pCDS->iCompression); in zipfileColumn()
7953 sqlite3_result_int64(ctx, pCsr->iId); in zipfileColumn()
7965 return pCsr->bEof; in zipfileEof()
7971 ** is guaranteed to be a file-handle open on a zip file.
7976 ** an English language error message may be left in virtual-table pTab.
7980 const u8 *aBlob, /* Pointer to in-memory file image */ in zipfileReadEOCD()
7985 u8 *aRead = pTab->aBuffer; /* Temporary buffer */ in zipfileReadEOCD()
7999 iOff = szFile - nRead; in zipfileReadEOCD()
8000 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg); in zipfileReadEOCD()
8003 aRead = (u8*)&aBlob[nBlob-nRead]; in zipfileReadEOCD()
8010 for(i=nRead-20; i>=0; i--){ in zipfileReadEOCD()
8018 pTab->base.zErrMsg = sqlite3_mprintf( in zipfileReadEOCD()
8025 pEOCD->iDisk = zipfileRead16(aRead); in zipfileReadEOCD()
8026 pEOCD->iFirstDisk = zipfileRead16(aRead); in zipfileReadEOCD()
8027 pEOCD->nEntry = zipfileRead16(aRead); in zipfileReadEOCD()
8028 pEOCD->nEntryTotal = zipfileRead16(aRead); in zipfileReadEOCD()
8029 pEOCD->nSize = zipfileRead32(aRead); in zipfileReadEOCD()
8030 pEOCD->iOffset = zipfileRead32(aRead); in zipfileReadEOCD()
8047 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) ); in zipfileAddEntry()
8048 assert( pNew->pNext==0 ); in zipfileAddEntry()
8050 if( pTab->pFirstEntry==0 ){ in zipfileAddEntry()
8051 pTab->pFirstEntry = pTab->pLastEntry = pNew; in zipfileAddEntry()
8053 assert( pTab->pLastEntry->pNext==0 ); in zipfileAddEntry()
8054 pTab->pLastEntry->pNext = pNew; in zipfileAddEntry()
8055 pTab->pLastEntry = pNew; in zipfileAddEntry()
8059 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext)); in zipfileAddEntry()
8060 pNew->pNext = pBefore; in zipfileAddEntry()
8071 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd); in zipfileLoadDirectory()
8075 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew); in zipfileLoadDirectory()
8080 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment; in zipfileLoadDirectory()
8094 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab; in zipfileFilter()
8098 int bInMemory = 0; /* True for an in-memory zipfile */ in zipfileFilter()
8102 if( pTab->zFile ){ in zipfileFilter()
8103 zFile = pTab->zFile; in zipfileFilter()
8111 assert( pTab->pFirstEntry==0 ); in zipfileFilter()
8117 pCsr->pFreeEntry = pTab->pFirstEntry; in zipfileFilter()
8118 pTab->pFirstEntry = pTab->pLastEntry = 0; in zipfileFilter()
8125 if( 0==pTab->pWriteFd && 0==bInMemory ){ in zipfileFilter()
8126 pCsr->pFile = fopen(zFile, "rb"); in zipfileFilter()
8127 if( pCsr->pFile==0 ){ in zipfileFilter()
8131 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd); in zipfileFilter()
8133 if( pCsr->eocd.nEntry==0 ){ in zipfileFilter()
8134 pCsr->bEof = 1; in zipfileFilter()
8136 pCsr->iNextOff = pCsr->eocd.iOffset; in zipfileFilter()
8142 pCsr->bNoop = 1; in zipfileFilter()
8143 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry; in zipfileFilter()
8158 int idx = -1; in zipfileBestIndex()
8161 for(i=0; i<pIdxInfo->nConstraint; i++){ in zipfileBestIndex()
8162 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i]; in zipfileBestIndex()
8163 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue; in zipfileBestIndex()
8164 if( pCons->usable==0 ){ in zipfileBestIndex()
8166 }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in zipfileBestIndex()
8170 pIdxInfo->estimatedCost = 1000.0; in zipfileBestIndex()
8172 pIdxInfo->aConstraintUsage[idx].argvIndex = 1; in zipfileBestIndex()
8173 pIdxInfo->aConstraintUsage[idx].omit = 1; in zipfileBestIndex()
8174 pIdxInfo->idxNum = 1; in zipfileBestIndex()
8186 pNew->cds.zFile = sqlite3_mprintf("%s", zPath); in zipfileNewEntry()
8187 if( pNew->cds.zFile==0 ){ in zipfileNewEntry()
8196 ZipfileCDS *pCds = &pEntry->cds; in zipfileSerializeLFH()
8199 pCds->nExtra = 9; in zipfileSerializeLFH()
8203 zipfileWrite16(a, pCds->iVersionExtract); in zipfileSerializeLFH()
8204 zipfileWrite16(a, pCds->flags); in zipfileSerializeLFH()
8205 zipfileWrite16(a, pCds->iCompression); in zipfileSerializeLFH()
8206 zipfileWrite16(a, pCds->mTime); in zipfileSerializeLFH()
8207 zipfileWrite16(a, pCds->mDate); in zipfileSerializeLFH()
8208 zipfileWrite32(a, pCds->crc32); in zipfileSerializeLFH()
8209 zipfileWrite32(a, pCds->szCompressed); in zipfileSerializeLFH()
8210 zipfileWrite32(a, pCds->szUncompressed); in zipfileSerializeLFH()
8211 zipfileWrite16(a, (u16)pCds->nFile); in zipfileSerializeLFH()
8212 zipfileWrite16(a, pCds->nExtra); in zipfileSerializeLFH()
8216 memcpy(a, pCds->zFile, (int)pCds->nFile); in zipfileSerializeLFH()
8217 a += (int)pCds->nFile; in zipfileSerializeLFH()
8223 zipfileWrite32(a, pEntry->mUnixTime); in zipfileSerializeLFH()
8225 return a-aBuf; in zipfileSerializeLFH()
8234 u8 *aBuf = pTab->aBuffer; in zipfileAppendEntry()
8241 pEntry->iDataOff = pTab->szCurrent; in zipfileAppendEntry()
8261 const char zTemplate[11] = "-rwxrwxrwx"; in zipfileGetMode()
8265 case '-': mode |= S_IFREG; break; in zipfileGetMode()
8271 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i); in zipfileGetMode()
8272 else if( z[i]!='-' ) goto parse_error; in zipfileGetMode()
8277 ** Or vice-versa - no data but "mode" is a file or symlink. */ in zipfileGetMode()
8290 ** Both (const char*) arguments point to nul-terminated strings. Argument
8295 if( nA>0 && zA[nA-1]=='/' ) nA--; in zipfileComparePath()
8296 if( nB>0 && zB[nB-1]=='/' ) nB--; in zipfileComparePath()
8305 assert( pTab->pWriteFd==0 ); in zipfileBegin()
8306 if( pTab->zFile==0 || pTab->zFile[0]==0 ){ in zipfileBegin()
8307 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename"); in zipfileBegin()
8314 ** in main-memory until the transaction is committed. */ in zipfileBegin()
8315 pTab->pWriteFd = fopen(pTab->zFile, "ab+"); in zipfileBegin()
8316 if( pTab->pWriteFd==0 ){ in zipfileBegin()
8317 pTab->base.zErrMsg = sqlite3_mprintf( in zipfileBegin()
8318 "zipfile: failed to open file %s for writing", pTab->zFile in zipfileBegin()
8322 fseek(pTab->pWriteFd, 0, SEEK_END); in zipfileBegin()
8323 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd); in zipfileBegin()
8335 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
8342 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ in zipfileTime()
8344 pVfs->xCurrentTimeInt64(pVfs, &ms); in zipfileTime()
8345 ret = (u32)((ms/1000) - ((i64)24405875 * 8640)); in zipfileTime()
8348 pVfs->xCurrentTime(pVfs, &day); in zipfileTime()
8349 ret = (u32)((day - 2440587.5) * 86400); in zipfileTime()
8355 ** Return a 32-bit timestamp in UNIX epoch format.
8359 ** cast to a 32-bit unsigned integer.
8369 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
8375 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext)); in zipfileRemoveEntryFromList()
8376 *pp = (*pp)->pNext; in zipfileRemoveEntryFromList()
8392 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */ in zipfileUpdate()
8399 const u8 *pData = 0; /* Pointer to buffer containing content */ in zipfileUpdate()
8410 if( pTab->pWriteFd==0 ){ in zipfileUpdate()
8425 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){ in zipfileUpdate()
8426 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){ in zipfileUpdate()
8429 assert( pOld->pNext ); in zipfileUpdate()
8465 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg); in zipfileUpdate()
8480 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg); in zipfileUpdate()
8492 ** '/'. This appears to be required for compatibility with info-zip in zipfileUpdate()
8495 if( nPath<=0 || zPath[nPath-1]!='/' ){ in zipfileUpdate()
8507 /* Check that we're not inserting a duplicate entry -OR- updating an in zipfileUpdate()
8511 for(p=pTab->pFirstEntry; p; p=p->pNext){ in zipfileUpdate()
8512 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){ in zipfileUpdate()
8513 switch( sqlite3_vtab_on_conflict(pTab->db) ){ in zipfileUpdate()
8538 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY; in zipfileUpdate()
8539 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED; in zipfileUpdate()
8540 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS; in zipfileUpdate()
8541 pNew->cds.iCompression = (u16)iMethod; in zipfileUpdate()
8542 zipfileMtimeToDos(&pNew->cds, mTime); in zipfileUpdate()
8543 pNew->cds.crc32 = iCrc32; in zipfileUpdate()
8544 pNew->cds.szCompressed = nData; in zipfileUpdate()
8545 pNew->cds.szUncompressed = (u32)sz; in zipfileUpdate()
8546 pNew->cds.iExternalAttr = (mode<<16); in zipfileUpdate()
8547 pNew->cds.iOffset = (u32)pTab->szCurrent; in zipfileUpdate()
8548 pNew->cds.nFile = (u16)nPath; in zipfileUpdate()
8549 pNew->mUnixTime = (u32)mTime; in zipfileUpdate()
8558 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){ in zipfileUpdate()
8559 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){ in zipfileUpdate()
8560 pCsr->pCurrent = pCsr->pCurrent->pNext; in zipfileUpdate()
8561 pCsr->bNoop = 1; in zipfileUpdate()
8578 zipfileWrite16(a, p->iDisk); in zipfileSerializeEOCD()
8579 zipfileWrite16(a, p->iFirstDisk); in zipfileSerializeEOCD()
8580 zipfileWrite16(a, p->nEntry); in zipfileSerializeEOCD()
8581 zipfileWrite16(a, p->nEntryTotal); in zipfileSerializeEOCD()
8582 zipfileWrite32(a, p->nSize); in zipfileSerializeEOCD()
8583 zipfileWrite32(a, p->iOffset); in zipfileSerializeEOCD()
8586 return a-aBuf; in zipfileSerializeEOCD()
8590 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer); in zipfileAppendEOCD()
8592 return zipfileAppendData(pTab, pTab->aBuffer, nBuf); in zipfileAppendEOCD()
8601 ZipfileCDS *pCDS = &pEntry->cds; in zipfileSerializeCDS()
8603 if( pEntry->aExtra==0 ){ in zipfileSerializeCDS()
8604 pCDS->nExtra = 9; in zipfileSerializeCDS()
8608 zipfileWrite16(a, pCDS->iVersionMadeBy); in zipfileSerializeCDS()
8609 zipfileWrite16(a, pCDS->iVersionExtract); in zipfileSerializeCDS()
8610 zipfileWrite16(a, pCDS->flags); in zipfileSerializeCDS()
8611 zipfileWrite16(a, pCDS->iCompression); in zipfileSerializeCDS()
8612 zipfileWrite16(a, pCDS->mTime); in zipfileSerializeCDS()
8613 zipfileWrite16(a, pCDS->mDate); in zipfileSerializeCDS()
8614 zipfileWrite32(a, pCDS->crc32); in zipfileSerializeCDS()
8615 zipfileWrite32(a, pCDS->szCompressed); in zipfileSerializeCDS()
8616 zipfileWrite32(a, pCDS->szUncompressed); in zipfileSerializeCDS()
8618 zipfileWrite16(a, pCDS->nFile); in zipfileSerializeCDS()
8619 zipfileWrite16(a, pCDS->nExtra); in zipfileSerializeCDS()
8620 zipfileWrite16(a, pCDS->nComment); in zipfileSerializeCDS()
8621 zipfileWrite16(a, pCDS->iDiskStart); in zipfileSerializeCDS()
8622 zipfileWrite16(a, pCDS->iInternalAttr); in zipfileSerializeCDS()
8623 zipfileWrite32(a, pCDS->iExternalAttr); in zipfileSerializeCDS()
8624 zipfileWrite32(a, pCDS->iOffset); in zipfileSerializeCDS()
8626 memcpy(a, pCDS->zFile, pCDS->nFile); in zipfileSerializeCDS()
8627 a += pCDS->nFile; in zipfileSerializeCDS()
8629 if( pEntry->aExtra ){ in zipfileSerializeCDS()
8630 int n = (int)pCDS->nExtra + (int)pCDS->nComment; in zipfileSerializeCDS()
8631 memcpy(a, pEntry->aExtra, n); in zipfileSerializeCDS()
8634 assert( pCDS->nExtra==9 ); in zipfileSerializeCDS()
8638 zipfileWrite32(a, pEntry->mUnixTime); in zipfileSerializeCDS()
8641 return a-aBuf; in zipfileSerializeCDS()
8647 if( pTab->pWriteFd ){ in zipfileCommit()
8648 i64 iOffset = pTab->szCurrent; in zipfileCommit()
8654 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){ in zipfileCommit()
8655 int n = zipfileSerializeCDS(p, pTab->aBuffer); in zipfileCommit()
8656 rc = zipfileAppendData(pTab, pTab->aBuffer, n); in zipfileCommit()
8665 eocd.nSize = (u32)(pTab->szCurrent - iOffset); in zipfileCommit()
8680 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){ in zipfileFindCursor()
8681 if( iId==pCsr->iId ) break; in zipfileFindCursor()
8697 ZipfileCDS *p = &pCsr->pCurrent->cds; in zipfileFunctionCds()
8699 "\"version-made-by\" : %u, " in zipfileFunctionCds()
8700 "\"version-to-extract\" : %u, " in zipfileFunctionCds()
8706 "\"compressed-size\" : %u, " in zipfileFunctionCds()
8707 "\"uncompressed-size\" : %u, " in zipfileFunctionCds()
8708 "\"file-name-length\" : %u, " in zipfileFunctionCds()
8709 "\"extra-field-length\" : %u, " in zipfileFunctionCds()
8710 "\"file-comment-length\" : %u, " in zipfileFunctionCds()
8711 "\"disk-number-start\" : %u, " in zipfileFunctionCds()
8712 "\"internal-attr\" : %u, " in zipfileFunctionCds()
8713 "\"external-attr\" : %u, " in zipfileFunctionCds()
8715 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract, in zipfileFunctionCds()
8716 (u32)p->flags, (u32)p->iCompression, in zipfileFunctionCds()
8717 (u32)p->mTime, (u32)p->mDate, in zipfileFunctionCds()
8718 (u32)p->crc32, (u32)p->szCompressed, in zipfileFunctionCds()
8719 (u32)p->szUncompressed, (u32)p->nFile, in zipfileFunctionCds()
8720 (u32)p->nExtra, (u32)p->nComment, in zipfileFunctionCds()
8721 (u32)p->iDiskStart, (u32)p->iInternalAttr, in zipfileFunctionCds()
8722 (u32)p->iExternalAttr, (u32)p->iOffset in zipfileFunctionCds()
8728 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT); in zipfileFunctionCds()
8754 u8 *a; /* Pointer to buffer */
8767 if( pBuf->n+nByte>pBuf->nAlloc ){ in zipfileBufferGrow()
8769 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512; in zipfileBufferGrow()
8770 int nReq = pBuf->n + nByte; in zipfileBufferGrow()
8773 aNew = sqlite3_realloc64(pBuf->a, nNew); in zipfileBufferGrow()
8775 pBuf->a = aNew; in zipfileBufferGrow()
8776 pBuf->nAlloc = (int)nNew; in zipfileBufferGrow()
8804 int iMethod = -1; /* Compression method to use (0 or 8) */ in zipfileStep()
8843 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL"); in zipfileStep()
8896 if( nName>0 && zName[nName-1]=='/' ){ in zipfileStep()
8897 zErr = sqlite3_mprintf("non-directory name must not end with /"); in zipfileStep()
8902 if( nName==0 || zName[nName-1]!='/' ){ in zipfileStep()
8910 while( nName>1 && zName[nName-2]=='/' ) nName--; in zipfileStep()
8924 e.cds.iOffset = p->body.n; in zipfileStep()
8930 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out; in zipfileStep()
8931 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]); in zipfileStep()
8935 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out; in zipfileStep()
8936 memcpy(&p->body.a[p->body.n], aData, nData); in zipfileStep()
8937 p->body.n += nData; in zipfileStep()
8942 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out; in zipfileStep()
8943 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]); in zipfileStep()
8946 p->nEntry++; in zipfileStep()
8953 sqlite3_result_error(pCtx, zErr, -1); in zipfileStep()
8972 if( p->nEntry>0 ){ in zipfileFinal()
8974 eocd.nEntry = (u16)p->nEntry; in zipfileFinal()
8975 eocd.nEntryTotal = (u16)p->nEntry; in zipfileFinal()
8976 eocd.nSize = p->cds.n; in zipfileFinal()
8977 eocd.iOffset = p->body.n; in zipfileFinal()
8979 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ; in zipfileFinal()
8984 memcpy(aZip, p->body.a, p->body.n); in zipfileFinal()
8985 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n); in zipfileFinal()
8986 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]); in zipfileFinal()
8991 sqlite3_free(p->body.a); in zipfileFinal()
8992 sqlite3_free(p->cds.a); in zipfileFinal()
9007 zipfileOpen, /* xOpen - open a cursor */ in zipfileRegister()
9008 zipfileClose, /* xClose - close a cursor */ in zipfileRegister()
9009 zipfileFilter, /* xFilter - configure scan constraints */ in zipfileRegister()
9010 zipfileNext, /* xNext - advance a cursor */ in zipfileRegister()
9011 zipfileEof, /* xEof - check for end of scan */ in zipfileRegister()
9012 zipfileColumn, /* xColumn - read data */ in zipfileRegister()
9013 0, /* xRowid - read data */ in zipfileRegister()
9028 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1); in zipfileRegister()
9030 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, in zipfileRegister()
9060 ** 2017-12-17
9072 ** for working with sqlar archives and used by the shell tool's built-in
9088 ** contains a two-byte identification header and a four-byte checksum at
9113 sqlite3_result_error(context, "error in compress()", -1); in sqlarCompressFunc()
9153 sqlite3_result_error(context, "error in uncompress()", -1); in sqlarUncompressFunc()
9208 ** If successful, a pointer to the new object is returned and (*pzErr) set
9210 ** an English-language error message. In this case it is the responsibility
9230 ** the analysis - indexes are recommended based on the database schema only.
9254 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
9255 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
9290 ** possible to add further SQL statements to the object or to re-attempt
9308 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
9316 ** If an out-of-range value (less than zero or equal to or greater than the
9333 ** Return a pointer to a buffer containing the CREATE INDEX statements
9350 ** sqlite3-expert_new().
9415 ** any other type of single-ended range constraint on a column).
9461 ** An object of the following type is created for each unique table/write-op
9462 ** seen. The objects are stored in a singly-linked list beginning at
9497 char *zKey; /* nul-terminated key */
9498 char *zVal; /* nul-terminated value string */
9499 char *zVal2; /* nul-terminated value string 2 */
9514 sqlite3 *dbm; /* In-memory db for this analysis */
9560 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){ in idxHashClear()
9561 pNext = pEntry->pHashNext; in idxHashClear()
9562 sqlite3_free(pEntry->zVal2); in idxHashClear()
9583 ** If zKey is already present in the hash table, return non-zero and do
9598 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ in idxHashAdd()
9599 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ in idxHashAdd()
9605 pEntry->zKey = (char*)&pEntry[1]; in idxHashAdd()
9606 memcpy(pEntry->zKey, zKey, nKey); in idxHashAdd()
9608 pEntry->zVal = &pEntry->zKey[nKey+1]; in idxHashAdd()
9609 memcpy(pEntry->zVal, zVal, nVal); in idxHashAdd()
9611 pEntry->pHashNext = pHash->aHash[iHash]; in idxHashAdd()
9612 pHash->aHash[iHash] = pEntry; in idxHashAdd()
9614 pEntry->pNext = pHash->pFirst; in idxHashAdd()
9615 pHash->pFirst = pEntry; in idxHashAdd()
9621 ** If zKey/nKey is present in the hash table, return a pointer to the
9622 ** hash-entry object.
9630 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ in idxHashFind()
9631 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ in idxHashFind()
9640 ** passed as the final two arguments to this function, return a pointer
9646 if( pEntry ) return pEntry->zVal; in idxHashSearch()
9652 ** variable to point to a copy of nul-terminated string zColl.
9661 pNew->zColl = (char*)&pNew[1]; in idxNewConstraint()
9662 memcpy(pNew->zColl, zColl, nColl+1); in idxNewConstraint()
9687 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); in idxPrepareStmt()
9742 assert( zIn[n-1]=='\'' ); in expertDequote()
9747 for(iIn=1; iIn<(n-1); iIn++){ in expertDequote()
9762 ** methods of the r-tree virtual table.
9764 ** argv[0] -> module name
9765 ** argv[1] -> database name
9766 ** argv[2] -> table name
9767 ** argv[...] -> column names...
9791 p->pExpert = pExpert; in expertConnect()
9792 p->pTab = pExpert->pTable; in expertConnect()
9793 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 ); in expertConnect()
9826 pScan->pTab = p->pTab; in expertBestIndex()
9827 pScan->pNextScan = p->pExpert->pScan; in expertBestIndex()
9828 p->pExpert->pScan = pScan; in expertBestIndex()
9831 for(i=0; i<pIdxInfo->nConstraint; i++){ in expertBestIndex()
9832 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i]; in expertBestIndex()
9833 if( pCons->usable in expertBestIndex()
9834 && pCons->iColumn>=0 in expertBestIndex()
9835 && p->pTab->aCol[pCons->iColumn].iPk==0 in expertBestIndex()
9836 && (pCons->op & opmask) in expertBestIndex()
9842 pNew->iCol = pCons->iColumn; in expertBestIndex()
9843 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in expertBestIndex()
9844 pNew->pNext = pScan->pEq; in expertBestIndex()
9845 pScan->pEq = pNew; in expertBestIndex()
9847 pNew->bRange = 1; in expertBestIndex()
9848 pNew->pNext = pScan->pRange; in expertBestIndex()
9849 pScan->pRange = pNew; in expertBestIndex()
9853 pIdxInfo->aConstraintUsage[i].argvIndex = n; in expertBestIndex()
9858 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){ in expertBestIndex()
9859 int iCol = pIdxInfo->aOrderBy[i].iColumn; in expertBestIndex()
9861 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl); in expertBestIndex()
9863 pNew->iCol = iCol; in expertBestIndex()
9864 pNew->bDesc = pIdxInfo->aOrderBy[i].desc; in expertBestIndex()
9865 pNew->pNext = pScan->pOrder; in expertBestIndex()
9866 pNew->pLink = pScan->pOrder; in expertBestIndex()
9867 pScan->pOrder = pNew; in expertBestIndex()
9874 pIdxInfo->estimatedCost = 1000000.0 / (n+1); in expertBestIndex()
9908 sqlite3_finalize(pCsr->pData); in expertClose()
9916 ** Return non-zero if the cursor does not currently point to a valid
9921 return pCsr->pData==0; in expertEof()
9931 assert( pCsr->pData ); in expertNext()
9932 rc = sqlite3_step(pCsr->pData); in expertNext()
9934 rc = sqlite3_finalize(pCsr->pData); in expertNext()
9935 pCsr->pData = 0; in expertNext()
9958 pVal = sqlite3_column_value(pCsr->pData, i); in expertColumn()
9974 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab); in expertFilter()
9975 sqlite3expert *pExpert = pVtab->pExpert; in expertFilter()
9982 rc = sqlite3_finalize(pCsr->pData); in expertFilter()
9983 pCsr->pData = 0; in expertFilter()
9985 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg, in expertFilter()
9986 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName in expertFilter()
9999 expertConnect, /* xCreate - create a table */ in idxRegisterVtab()
10000 expertConnect, /* xConnect - connect to an existing table */ in idxRegisterVtab()
10001 expertBestIndex, /* xBestIndex - Determine search strategy */ in idxRegisterVtab()
10002 expertDisconnect, /* xDisconnect - Disconnect from a table */ in idxRegisterVtab()
10003 expertDisconnect, /* xDestroy - Drop a table */ in idxRegisterVtab()
10004 expertOpen, /* xOpen - open a cursor */ in idxRegisterVtab()
10005 expertClose, /* xClose - close a cursor */ in idxRegisterVtab()
10006 expertFilter, /* xFilter - configure scan constraints */ in idxRegisterVtab()
10007 expertNext, /* xNext - advance a cursor */ in idxRegisterVtab()
10009 expertColumn, /* xColumn - read data */ in idxRegisterVtab()
10010 expertRowid, /* xRowid - read data */ in idxRegisterVtab()
10011 expertUpdate, /* xUpdate - write data */ in idxRegisterVtab()
10012 0, /* xBegin - begin transaction */ in idxRegisterVtab()
10013 0, /* xSync - sync transaction */ in idxRegisterVtab()
10014 0, /* xCommit - commit transaction */ in idxRegisterVtab()
10015 0, /* xRollback - rollback transaction */ in idxRegisterVtab()
10016 0, /* xFindFunction - function overloading */ in idxRegisterVtab()
10017 0, /* xRename - rename the table */ in idxRegisterVtab()
10024 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p); in idxRegisterVtab()
10093 pNew->aCol = (IdxColumn*)&pNew[1]; in idxGetTableInfo()
10094 pNew->nCol = nCol; in idxGetTableInfo()
10095 pCsr = (char*)&pNew->aCol[nCol]; in idxGetTableInfo()
10105 pNew->aCol[nCol].zName = pCsr; in idxGetTableInfo()
10106 pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1); in idxGetTableInfo()
10116 pNew->aCol[nCol].zColl = pCsr; in idxGetTableInfo()
10129 pNew->zName = pCsr; in idxGetTableInfo()
10130 if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1); in idxGetTableInfo()
10138 ** This function is a no-op if *pRc is set to anything other than
10207 IdxColumn *p = &pTab->aCol[pCons->iCol]; in idxAppendColDefn()
10210 if( idxIdentifierRequiresQuotes(p->zName) ){ in idxAppendColDefn()
10211 zRet = idxAppendText(pRc, zRet, "%Q", p->zName); in idxAppendColDefn()
10213 zRet = idxAppendText(pRc, zRet, "%s", p->zName); in idxAppendColDefn()
10216 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){ in idxAppendColDefn()
10217 if( idxIdentifierRequiresQuotes(pCons->zColl) ){ in idxAppendColDefn()
10218 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl); in idxAppendColDefn()
10220 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl); in idxAppendColDefn()
10224 if( pCons->bDesc ){ in idxAppendColDefn()
10233 ** such an index is found, return non-zero. Or, if no such index is found,
10245 const char *zTbl = pScan->pTab->zName; in idxFindCompatible()
10252 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++; in idxFindCompatible()
10263 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0; in idxFindCompatible()
10272 for(pIter=pEq; pIter; pIter=pIter->pLink){ in idxFindCompatible()
10273 if( pIter->bFlag ) continue; in idxFindCompatible()
10274 if( pIter->iCol!=iCol ) continue; in idxFindCompatible()
10275 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue; in idxFindCompatible()
10276 pIter->bFlag = 1; in idxFindCompatible()
10285 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){ in idxFindCompatible()
10289 pT = pT->pLink; in idxFindCompatible()
10325 sqlite3 *dbm = p->dbm; in idxCreateFromCons()
10328 IdxTable *pTab = pScan->pTab; in idxCreateFromCons()
10335 for(pCons=pEq; pCons; pCons=pCons->pLink){ in idxCreateFromCons()
10338 for(pCons=pTail; pCons; pCons=pCons->pLink){ in idxCreateFromCons()
10344 const char *zTable = pScan->pTab->zName; in idxCreateFromCons()
10386 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg); in idxCreateFromCons()
10390 idxHashAdd(&rc, &p->hIdx, zName, zIdx); in idxCreateFromCons()
10409 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){ in idxFindConstraint()
10410 if( p->iCol==pCmp->iCol ) return 1; in idxFindConstraint()
10425 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){ in idxCreateFromWhere()
10427 pCon->pLink = p1; in idxCreateFromWhere()
10439 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){ in idxCreateFromWhere()
10440 assert( pCon->pLink==0 ); in idxCreateFromWhere()
10452 ** linked-list pScan.
10458 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){ in idxCreateCandidates()
10460 if( rc==SQLITE_OK && pIter->pOrder ){ in idxCreateCandidates()
10461 rc = idxCreateFromWhere(p, pIter, pIter->pOrder); in idxCreateCandidates()
10476 pNext = p->pNext; in idxConstraintFree()
10489 pNext = p->pNextScan; in idxScanFree()
10490 idxConstraintFree(p->pOrder); in idxScanFree()
10491 idxConstraintFree(p->pEq); in idxScanFree()
10492 idxConstraintFree(p->pRange); in idxScanFree()
10505 pNext = p->pNext; in idxStatementFree()
10506 sqlite3_free(p->zEQP); in idxStatementFree()
10507 sqlite3_free(p->zIdx); in idxStatementFree()
10519 pNext = pIter->pNext; in idxTableFree()
10531 pNext = pIter->pNext; in idxWriteFree()
10548 sqlite3 *dbm = p->dbm; in idxFindIndexes()
10554 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){ in idxFindIndexes()
10559 "EXPLAIN QUERY PLAN %s", pStmt->zSql in idxFindIndexes()
10587 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx); in idxFindIndexes()
10596 if( zDetail[0]!='-' ){ in idxFindIndexes()
10597 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail); in idxFindIndexes()
10601 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ in idxFindIndexes()
10602 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey); in idxFindIndexes()
10628 for(pTab=p->pTable; pTab; pTab=pTab->pNext){ in idxAuthCallback()
10629 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break; in idxAuthCallback()
10633 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){ in idxAuthCallback()
10634 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break; in idxAuthCallback()
10639 pWrite->pTab = pTab; in idxAuthCallback()
10640 pWrite->eOp = eOp; in idxAuthCallback()
10641 pWrite->pNext = p->pWrite; in idxAuthCallback()
10642 p->pWrite = pWrite; in idxAuthCallback()
10658 IdxTable *pTab = pWrite->pTab; in idxProcessOneTrigger()
10659 const char *zTab = pTab->zName; in idxProcessOneTrigger()
10669 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab); in idxProcessOneTrigger()
10673 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr); in idxProcessOneTrigger()
10683 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr); in idxProcessOneTrigger()
10688 switch( pWrite->eOp ){ in idxProcessOneTrigger()
10692 for(i=0; i<pTab->nCol; i++){ in idxProcessOneTrigger()
10701 for(i=0; i<pTab->nCol; i++){ in idxProcessOneTrigger()
10703 pTab->aCol[i].zName in idxProcessOneTrigger()
10709 assert( pWrite->eOp==SQLITE_DELETE ); in idxProcessOneTrigger()
10719 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0); in idxProcessOneTrigger()
10722 idxDatabaseError(p->dbv, pzErr); in idxProcessOneTrigger()
10728 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr); in idxProcessOneTrigger()
10737 IdxWrite *pFirst = p->pWrite; in idxProcessTriggers()
10741 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){ in idxProcessTriggers()
10745 pFirst = p->pWrite; in idxProcessTriggers()
10758 ** 1) Add an entry to the p->pTable list, and in idxCreateVtabSchema()
10761 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg, in idxCreateVtabSchema()
10777 if( zSql ) rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg); in idxCreateVtabSchema()
10780 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg); in idxCreateVtabSchema()
10785 pTab->pNext = p->pTable; in idxCreateVtabSchema()
10786 p->pTable = pTab; in idxCreateVtabSchema()
10790 for(i=0; i<pTab->nCol; i++){ in idxCreateVtabSchema()
10792 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl in idxCreateVtabSchema()
10802 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg); in idxCreateVtabSchema()
10830 if( p->nRow==0.0 ){ in idxSampleFunc()
10833 bRet = (p->nRet / p->nRow) <= p->target; in idxSampleFunc()
10837 bRet = ((int)rnd % 100) <= p->iTarget; in idxSampleFunc()
10842 p->nRow += 1.0; in idxSampleFunc()
10843 p->nRet += (double)bRet; in idxSampleFunc()
10872 assert( iSlot<=p->nSlot ); in idxRemFunc()
10873 pSlot = &p->aSlot[iSlot]; in idxRemFunc()
10875 switch( pSlot->eType ){ in idxRemFunc()
10877 /* no-op */ in idxRemFunc()
10881 sqlite3_result_int64(pCtx, pSlot->iVal); in idxRemFunc()
10885 sqlite3_result_double(pCtx, pSlot->rVal); in idxRemFunc()
10889 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT); in idxRemFunc()
10893 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT); in idxRemFunc()
10897 pSlot->eType = sqlite3_value_type(argv[1]); in idxRemFunc()
10898 switch( pSlot->eType ){ in idxRemFunc()
10900 /* no-op */ in idxRemFunc()
10904 pSlot->iVal = sqlite3_value_int64(argv[1]); in idxRemFunc()
10908 pSlot->rVal = sqlite3_value_double(argv[1]); in idxRemFunc()
10915 if( nByte>pSlot->nByte ){ in idxRemFunc()
10916 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2); in idxRemFunc()
10921 pSlot->nByte = nByte*2; in idxRemFunc()
10922 pSlot->z = zNew; in idxRemFunc()
10924 pSlot->n = nByte; in idxRemFunc()
10925 if( pSlot->eType==SQLITE_BLOB ){ in idxRemFunc()
10927 if( pData ) memcpy(pSlot->z, pData, nByte); in idxRemFunc()
10930 memcpy(pSlot->z, pData, nByte); in idxRemFunc()
10974 assert( p->iSample>0 ); in idxPopulateOneStat1()
10977 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC); in idxPopulateOneStat1()
10989 if( p->iSample==100 ){ in idxPopulateOneStat1()
11004 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); in idxPopulateOneStat1()
11021 for(/*no-op*/; i<nCol; i++){ in idxPopulateOneStat1()
11036 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC); in idxPopulateOneStat1()
11037 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC); in idxPopulateOneStat1()
11038 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC); in idxPopulateOneStat1()
11043 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx)); in idxPopulateOneStat1()
11045 assert( pEntry->zVal2==0 ); in idxPopulateOneStat1()
11046 pEntry->zVal2 = zStat; in idxPopulateOneStat1()
11061 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); in idxBuildSampleTable()
11068 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0); in idxBuildSampleTable()
11087 i64 iPrev = -100000; in idxPopulateStat1()
11102 if( p->iSample==0 ) return SQLITE_OK; in idxPopulateStat1()
11104 rc = idxLargestIndex(p->dbm, &nMax, pzErr); in idxPopulateStat1()
11107 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0); in idxPopulateStat1()
11115 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); in idxPopulateStat1()
11122 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0 in idxPopulateStat1()
11127 pCtx->nSlot = nMax+1; in idxPopulateStat1()
11128 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex); in idxPopulateStat1()
11131 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo); in idxPopulateStat1()
11134 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite); in idxPopulateStat1()
11142 if( p->iSample<100 && iPrev!=iRowid ){ in idxPopulateStat1()
11143 samplectx.target = (double)p->iSample / 100.0; in idxPopulateStat1()
11144 samplectx.iTarget = p->iSample; in idxPopulateStat1()
11153 if( rc==SQLITE_OK && p->iSample<100 ){ in idxPopulateStat1()
11154 rc = sqlite3_exec(p->dbv, in idxPopulateStat1()
11164 for(i=0; i<pCtx->nSlot; i++){ in idxPopulateStat1()
11165 sqlite3_free(pCtx->aSlot[i].z); in idxPopulateStat1()
11171 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0); in idxPopulateStat1()
11174 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); in idxPopulateStat1()
11187 /* Open two in-memory databases to work with. The "vtab database" (dbv) in sqlite3_expert_new()
11194 pNew->db = db; in sqlite3_expert_new()
11195 pNew->iSample = 100; in sqlite3_expert_new()
11196 rc = sqlite3_open(":memory:", &pNew->dbv); in sqlite3_expert_new()
11199 rc = sqlite3_open(":memory:", &pNew->dbm); in sqlite3_expert_new()
11201 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0); in sqlite3_expert_new()
11209 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, in sqlite3_expert_new()
11215 if( zSql ) rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg); in sqlite3_expert_new()
11227 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew); in sqlite3_expert_new()
11251 p->iSample = iVal; in sqlite3_expert_config()
11271 IdxScan *pScanOrig = p->pScan; in sqlite3_expert_sql()
11272 IdxStatement *pStmtOrig = p->pStatement; in sqlite3_expert_sql()
11276 if( p->bRun ) return SQLITE_MISUSE; in sqlite3_expert_sql()
11280 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt); in sqlite3_expert_sql()
11288 pNew->zSql = (char*)&pNew[1]; in sqlite3_expert_sql()
11289 memcpy(pNew->zSql, z, n+1); in sqlite3_expert_sql()
11290 pNew->pNext = p->pStatement; in sqlite3_expert_sql()
11291 if( p->pStatement ) pNew->iId = p->pStatement->iId+1; in sqlite3_expert_sql()
11292 p->pStatement = pNew; in sqlite3_expert_sql()
11297 idxDatabaseError(p->dbv, pzErr); in sqlite3_expert_sql()
11302 idxScanFree(p->pScan, pScanOrig); in sqlite3_expert_sql()
11303 idxStatementFree(p->pStatement, pStmtOrig); in sqlite3_expert_sql()
11304 p->pScan = pScanOrig; in sqlite3_expert_sql()
11305 p->pStatement = pStmtOrig; in sqlite3_expert_sql()
11318 /* Create candidate indexes within the in-memory database file */ in sqlite3_expert_analyze()
11333 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ in sqlite3_expert_analyze()
11334 p->zCandidates = idxAppendText(&rc, p->zCandidates, in sqlite3_expert_analyze()
11335 "%s;%s%s\n", pEntry->zVal, in sqlite3_expert_analyze()
11336 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2 in sqlite3_expert_analyze()
11347 p->bRun = 1; in sqlite3_expert_analyze()
11358 if( p->pStatement ) nRet = p->pStatement->iId+1; in sqlite3_expert_count()
11369 if( p->bRun==0 ) return 0; in sqlite3_expert_report()
11370 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext); in sqlite3_expert_report()
11373 if( pStmt ) zRet = pStmt->zSql; in sqlite3_expert_report()
11376 if( pStmt ) zRet = pStmt->zIdx; in sqlite3_expert_report()
11379 if( pStmt ) zRet = pStmt->zEQP; in sqlite3_expert_report()
11382 zRet = p->zCandidates; in sqlite3_expert_report()
11393 sqlite3_close(p->dbm); in sqlite3_expert_destroy()
11394 sqlite3_close(p->dbv); in sqlite3_expert_destroy()
11395 idxScanFree(p->pScan, 0); in sqlite3_expert_destroy()
11396 idxStatementFree(p->pStatement, 0); in sqlite3_expert_destroy()
11397 idxTableFree(p->pTable); in sqlite3_expert_destroy()
11398 idxWriteFree(p->pWrite); in sqlite3_expert_destroy()
11399 idxHashClear(&p->hIdx); in sqlite3_expert_destroy()
11400 sqlite3_free(p->zCandidates); in sqlite3_expert_destroy()
11417 ** 2019-04-17
11433 ** sqlite_dbdata is used to extract data directly from a database b-tree
11434 ** page and its associated overflow pages, bypassing the b-tree layer.
11446 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
11450 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
11453 ** each cell on the page. For intkey b-trees, the key value is stored in
11454 ** field -1.
11458 ** CREATE TABLE t1(a, b); -- root page is page 2
11466 ** (2, 0, -1, 5 ),
11469 ** (2, 1, -1, 10 ),
11486 ** It contains one entry for each b-tree pointer between a parent and
11588 pTab->db = db; in dbdataConnect()
11589 pTab->bPtr = (pAux!=0); in dbdataConnect()
11603 sqlite3_finalize(pTab->pStmt); in dbdataDisconnect()
11625 int iSchema = -1; in dbdataBestIndex()
11626 int iPgno = -1; in dbdataBestIndex()
11627 int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA); in dbdataBestIndex()
11629 for(i=0; i<pIdx->nConstraint; i++){ in dbdataBestIndex()
11630 struct sqlite3_index_constraint *p = &pIdx->aConstraint[i]; in dbdataBestIndex()
11631 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ in dbdataBestIndex()
11632 if( p->iColumn==colSchema ){ in dbdataBestIndex()
11633 if( p->usable==0 ) return SQLITE_CONSTRAINT; in dbdataBestIndex()
11636 if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){ in dbdataBestIndex()
11643 pIdx->aConstraintUsage[iSchema].argvIndex = 1; in dbdataBestIndex()
11644 pIdx->aConstraintUsage[iSchema].omit = 1; in dbdataBestIndex()
11647 pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0); in dbdataBestIndex()
11648 pIdx->aConstraintUsage[iPgno].omit = 1; in dbdataBestIndex()
11649 pIdx->estimatedCost = 100; in dbdataBestIndex()
11650 pIdx->estimatedRows = 50; in dbdataBestIndex()
11652 if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){ in dbdataBestIndex()
11653 int iCol = pIdx->aOrderBy[0].iColumn; in dbdataBestIndex()
11654 if( pIdx->nOrderBy==1 ){ in dbdataBestIndex()
11655 pIdx->orderByConsumed = (iCol==0 || iCol==1); in dbdataBestIndex()
11656 }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){ in dbdataBestIndex()
11657 pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1); in dbdataBestIndex()
11662 pIdx->estimatedCost = 100000000; in dbdataBestIndex()
11663 pIdx->estimatedRows = 1000000000; in dbdataBestIndex()
11665 pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00); in dbdataBestIndex()
11680 pCsr->base.pVtab = pVTab; in dbdataOpen()
11692 DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab); in dbdataResetCursor()
11693 if( pTab->pStmt==0 ){ in dbdataResetCursor()
11694 pTab->pStmt = pCsr->pStmt; in dbdataResetCursor()
11696 sqlite3_finalize(pCsr->pStmt); in dbdataResetCursor()
11698 pCsr->pStmt = 0; in dbdataResetCursor()
11699 pCsr->iPgno = 1; in dbdataResetCursor()
11700 pCsr->iCell = 0; in dbdataResetCursor()
11701 pCsr->iField = 0; in dbdataResetCursor()
11702 pCsr->bOnePage = 0; in dbdataResetCursor()
11703 sqlite3_free(pCsr->aPage); in dbdataResetCursor()
11704 sqlite3_free(pCsr->pRec); in dbdataResetCursor()
11705 pCsr->pRec = 0; in dbdataResetCursor()
11706 pCsr->aPage = 0; in dbdataResetCursor()
11720 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
11745 u8 **ppPage, /* OUT: pointer to page buffer */ in dbdataLoadPage()
11750 sqlite3_stmt *pStmt = pCsr->pStmt; in dbdataLoadPage()
11831 return ((eType-12) / 2); in dbdataValueBytes()
11886 int n = ((eType-12) / 2); in dbdataValue()
11914 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; in dbdataNext()
11916 pCsr->iRowid++; in dbdataNext()
11919 int iOff = (pCsr->iPgno==1 ? 100 : 0); in dbdataNext()
11922 if( pCsr->aPage==0 ){ in dbdataNext()
11924 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK; in dbdataNext()
11925 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage); in dbdataNext()
11927 if( pCsr->aPage ) break; in dbdataNext()
11928 if( pCsr->bOnePage ) return SQLITE_OK; in dbdataNext()
11929 pCsr->iPgno++; in dbdataNext()
11931 pCsr->iCell = pTab->bPtr ? -2 : 0; in dbdataNext()
11932 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]); in dbdataNext()
11935 if( pTab->bPtr ){ in dbdataNext()
11936 if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){ in dbdataNext()
11937 pCsr->iCell = pCsr->nCell; in dbdataNext()
11939 pCsr->iCell++; in dbdataNext()
11940 if( pCsr->iCell>=pCsr->nCell ){ in dbdataNext()
11941 sqlite3_free(pCsr->aPage); in dbdataNext()
11942 pCsr->aPage = 0; in dbdataNext()
11943 if( pCsr->bOnePage ) return SQLITE_OK; in dbdataNext()
11944 pCsr->iPgno++; in dbdataNext()
11950 if( pCsr->pRec==0 ){ in dbdataNext()
11959 switch( pCsr->aPage[iOff] ){ in dbdataNext()
11969 /* This is not a b-tree page with records on it. Continue. */ in dbdataNext()
11970 pCsr->iCell = pCsr->nCell; in dbdataNext()
11974 if( pCsr->iCell>=pCsr->nCell ){ in dbdataNext()
11978 iOff += 8 + nPointer + pCsr->iCell*2; in dbdataNext()
11979 if( iOff>pCsr->nPage ){ in dbdataNext()
11982 iOff = get_uint16(&pCsr->aPage[iOff]); in dbdataNext()
11985 /* For an interior node cell, skip past the child-page number */ in dbdataNext()
11989 if( bNextPage || iOff>pCsr->nPage ){ in dbdataNext()
11992 iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload); in dbdataNext()
11996 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){ in dbdataNext()
11997 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey); in dbdataNext()
12001 U = pCsr->nPage; in dbdataNext()
12003 X = U-35; in dbdataNext()
12005 X = ((U-12)*64/255)-23; in dbdataNext()
12011 M = ((U-12)*32/255)-23; in dbdataNext()
12012 K = M+((nPayload-M)%(U-4)); in dbdataNext()
12020 if( bNextPage || nLocal+iOff>pCsr->nPage ){ in dbdataNext()
12027 pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES); in dbdataNext()
12028 if( pCsr->pRec==0 ) return SQLITE_NOMEM; in dbdataNext()
12029 memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES); in dbdataNext()
12030 pCsr->nRec = nPayload; in dbdataNext()
12033 memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal); in dbdataNext()
12038 sqlite3_int64 nRem = nPayload - nLocal; in dbdataNext()
12039 u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]); in dbdataNext()
12045 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage ); in dbdataNext()
12049 nCopy = U-4; in dbdataNext()
12051 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy); in dbdataNext()
12052 nRem -= nCopy; in dbdataNext()
12059 iHdr = dbdataGetVarintU32(pCsr->pRec, &nHdr); in dbdataNext()
12061 pCsr->nHdr = nHdr; in dbdataNext()
12062 pCsr->pHdrPtr = &pCsr->pRec[iHdr]; in dbdataNext()
12063 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr]; in dbdataNext()
12064 pCsr->iField = (bHasRowid ? -1 : 0); in dbdataNext()
12068 pCsr->iField++; in dbdataNext()
12069 if( pCsr->iField>0 ){ in dbdataNext()
12071 if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){ in dbdataNext()
12074 pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType); in dbdataNext()
12075 pCsr->pPtr += dbdataValueBytes(iType); in dbdataNext()
12081 sqlite3_free(pCsr->aPage); in dbdataNext()
12082 sqlite3_free(pCsr->pRec); in dbdataNext()
12083 pCsr->aPage = 0; in dbdataNext()
12084 pCsr->pRec = 0; in dbdataNext()
12085 if( pCsr->bOnePage ) return SQLITE_OK; in dbdataNext()
12086 pCsr->iPgno++; in dbdataNext()
12088 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){ in dbdataNext()
12094 sqlite3_free(pCsr->pRec); in dbdataNext()
12095 pCsr->pRec = 0; in dbdataNext()
12096 pCsr->iCell++; in dbdataNext()
12110 return pCsr->aPage==0; in dbdataEof()
12114 ** Return true if nul-terminated string zSchema ends in "()". Or false
12119 if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){ in dbdataIsFunction()
12120 return (int)n-2; in dbdataIsFunction()
12128 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
12132 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab; in dbdataDbsize()
12145 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0); in dbdataDbsize()
12148 pCsr->szDb = sqlite3_column_int(pStmt, 0); in dbdataDbsize()
12157 ** and inspecting the header field. If successful, set the pCsr->enc variable
12167 pCsr->enc = get_uint32(&aPg1[56]); in dbdataGetEncoding()
12183 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; in dbdataFilter()
12188 assert( pCsr->iPgno==1 ); in dbdataFilter()
12194 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]); in dbdataFilter()
12195 pCsr->bOnePage = 1; in dbdataFilter()
12202 if( pTab->pStmt ){ in dbdataFilter()
12203 pCsr->pStmt = pTab->pStmt; in dbdataFilter()
12204 pTab->pStmt = 0; in dbdataFilter()
12210 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0); in dbdataFilter()
12214 rc = sqlite3_prepare_v2(pTab->db, in dbdataFilter()
12215 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1, in dbdataFilter()
12216 &pCsr->pStmt, 0 in dbdataFilter()
12221 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT); in dbdataFilter()
12223 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); in dbdataFilter()
12247 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; in dbdataColumn()
12248 if( pTab->bPtr ){ in dbdataColumn()
12251 sqlite3_result_int64(ctx, pCsr->iPgno); in dbdataColumn()
12254 int iOff = pCsr->iPgno==1 ? 100 : 0; in dbdataColumn()
12255 if( pCsr->iCell<0 ){ in dbdataColumn()
12258 iOff += 12 + pCsr->iCell*2; in dbdataColumn()
12259 if( iOff>pCsr->nPage ) return SQLITE_OK; in dbdataColumn()
12260 iOff = get_uint16(&pCsr->aPage[iOff]); in dbdataColumn()
12262 if( iOff<=pCsr->nPage ){ in dbdataColumn()
12263 sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff])); in dbdataColumn()
12271 sqlite3_result_int64(ctx, pCsr->iPgno); in dbdataColumn()
12274 sqlite3_result_int(ctx, pCsr->iCell); in dbdataColumn()
12277 sqlite3_result_int(ctx, pCsr->iField); in dbdataColumn()
12280 if( pCsr->iField<0 ){ in dbdataColumn()
12281 sqlite3_result_int64(ctx, pCsr->iIntkey); in dbdataColumn()
12282 }else if( &pCsr->pRec[pCsr->nRec] >= pCsr->pPtr ){ in dbdataColumn()
12284 dbdataGetVarintU32(pCsr->pHdrPtr, &iType); in dbdataColumn()
12286 ctx, pCsr->enc, iType, pCsr->pPtr, in dbdataColumn()
12287 &pCsr->pRec[pCsr->nRec] - pCsr->pPtr in dbdataColumn()
12302 *pRowid = pCsr->iRowid; in dbdataRowid()
12318 dbdataOpen, /* xOpen - open a cursor */ in sqlite3DbdataRegister()
12319 dbdataClose, /* xClose - close a cursor */ in sqlite3DbdataRegister()
12320 dbdataFilter, /* xFilter - configure scan constraints */ in sqlite3DbdataRegister()
12321 dbdataNext, /* xNext - advance a cursor */ in sqlite3DbdataRegister()
12322 dbdataEof, /* xEof - check for end of scan */ in sqlite3DbdataRegister()
12323 dbdataColumn, /* xColumn - read data */ in sqlite3DbdataRegister()
12324 dbdataRowid, /* xRowid - read data */ in sqlite3DbdataRegister()
12362 ** 2022-08-27
12373 ** This file contains the public interface to the "recover" extension -
12393 ** error. If it returns some other non-SQLITE_OK value, then an error
12461 ** to this function (pCtx) as its first parameter, and a pointer to a
12462 ** nul-terminated buffer containing the SQL statement formated as UTF-8 as
12467 ** If an out-of-memory error occurs, NULL may be returned instead of
12502 ** of a "lost-and-found" table in the output database, or NULL. If
12503 ** the argument is non-NULL and the database contains seemingly
12506 ** pages to add to the lost-and-found table.
12509 ** The pArg value must actually be a pointer to a value of type
12511 ** (argument is 1) and a lost-and-found table has been configured using
12520 ** The pArg value must actually be a pointer to a value of type
12528 ** The pArg value must actually be a pointer to a value of type
12531 ** module creates and populates non-UNIQUE indexes right at the end of the
12532 ** recovery operation - after all recoverable data has been inserted
12537 ** Or, if this option is set (argument is 1), then non-UNIQUE indexes
12561 ** all further such calls on the same recover handle are no-ops that return
12562 ** the same non-SQLITE_OK value.
12579 ** pointer to a buffer containing an English language explanation of
12580 ** the error. If no error message is available, or if an out-of memory
12614 ** 2022-08-27
12676 ** Otherwise, it contains -1. If the table does contain an INTEGER PRIMARY
12683 ** the output database, before any non-schema data are recovered. They
12684 ** are then stored in a singly-linked list linked by this variable beginning
12702 ** The index of the associated field within database records. Or -1 if
12755 u32 aElem[1]; /* Array of 32-bit bitmasks */
12810 int (*xSql)(void*,const char*); /* Pointer to SQL callback function */
12814 char *zLostAndFound; /* Name of lost-and-found table (or NULL) */
12859 ** Populate the recovery.map table - used to figure out a "root" page
12864 ** Populate the lost-and-found table itself.
12900 ** recoverEnterMutex() - Enter the recovery mutex
12901 ** recoverLeaveMutex() - Leave the recovery mutex
12902 ** recoverAssertMutexHeld() - Assert that the recovery mutex is held
12925 ** Like strlen(). But handles NULL pointer arguments.
12933 ** This function is a no-op if the recover handle passed as the first
12934 ** argument already contains an error (if p->errCode!=SQLITE_OK).
12937 ** bytes in size. If successful, a pointer to the new buffer is returned. Or,
12939 ** (p->errCode) set to SQLITE_NOMEM.
12944 if( p->errCode==SQLITE_OK ){ in recoverMalloc()
12949 p->errCode = SQLITE_NOMEM; in recoverMalloc()
12980 sqlite3_free(p->zErrMsg); in recoverError()
12981 p->zErrMsg = z; in recoverError()
12982 p->errCode = errCode; in recoverError()
12988 ** This function is a no-op if p->errCode is initially other than SQLITE_OK.
13001 pRet->nPg = nPg; in recoverBitmapAlloc()
13017 if( iPg<=pMap->nPg ){ in recoverBitmapSet()
13020 pMap->aElem[iElem] |= (((u32)1) << iBit); in recoverBitmapSet()
13030 if( iPg<=pMap->nPg && iPg>0 ){ in recoverBitmapQuery()
13033 ret = (pMap->aElem[iElem] & (((u32)1) << iBit)) ? 1 : 0; in recoverBitmapQuery()
13048 ** This function is a no-op if recover handle p already contains an error
13049 ** (if p->errCode!=SQLITE_OK).
13062 if( p->errCode==SQLITE_OK ){ in recoverPrepare()
13063 if( sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) ){ in recoverPrepare()
13071 ** This function is a no-op if recover handle p already contains an error
13072 ** (if p->errCode!=SQLITE_OK).
13077 ** the statment handle returned. Or, if an error occurs - either during
13078 ** the printf() formatting or when preparing the resulting SQL - an
13087 if( p->errCode==SQLITE_OK ){ in recoverPreparePrintf()
13094 p->errCode = SQLITE_NOMEM; in recoverPreparePrintf()
13109 ** This function returns a copy of the statement handle pointer passed
13114 if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT && p->errCode==SQLITE_OK ){ in recoverReset()
13129 if( rc!=SQLITE_OK && p->errCode==SQLITE_OK ){ in recoverFinalize()
13135 ** This function is a no-op if recover handle p already contains an error
13136 ** (if p->errCode!=SQLITE_OK). A copy of p->errCode is returned in this
13144 if( p->errCode==SQLITE_OK ){ in recoverExec()
13150 return p->errCode; in recoverExec()
13164 if( p->errCode==SQLITE_OK ){ in recoverBindValue()
13171 ** This function is a no-op if recover handle p already contains an error
13172 ** (if p->errCode!=SQLITE_OK). NULL is returned in this case.
13177 ** sqlite3_malloc(). If successful, a pointer to the buffer is returned.
13190 if( p->errCode==SQLITE_OK ){ in recoverMPrintf()
13191 if( z==0 ) p->errCode = SQLITE_NOMEM; in recoverMPrintf()
13200 ** This function is a no-op if recover handle p already contains an error
13201 ** (if p->errCode!=SQLITE_OK). Zero is returned in this case.
13210 if( p->errCode==SQLITE_OK ){ in recoverPageCount()
13212 pStmt = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.page_count", p->zDb); in recoverPageCount()
13224 ** this function must be a blob. The second a non-negative integer. This
13225 ** function reads and returns a 32-bit big-endian integer from byte
13257 ** lost-and-found table, and it depends on those routines having populated
13260 ** The only argument to this function is a page-number. It returns true
13274 sqlite3_result_int(pCtx, recoverBitmapQuery(p->laf.pUsed, pgno)); in recoverPageIsUsed()
13278 ** The implementation of a user-defined SQL function invoked by the
13288 ** SELECT getpage(0); -- return number of pages in db
13289 ** SELECT getpage(4); -- return page 4 of db as a blob of data
13306 if( p->pGetPage==0 ){ in recoverGetPage()
13307 pStmt = p->pGetPage = recoverPreparePrintf( in recoverGetPage()
13308 p, p->dbIn, "SELECT data FROM sqlite_dbpage(%Q) WHERE pgno=?", p->zDb in recoverGetPage()
13310 }else if( p->errCode==SQLITE_OK ){ in recoverGetPage()
13311 pStmt = p->pGetPage; in recoverGetPage()
13319 assert( p->errCode==SQLITE_OK ); in recoverGetPage()
13322 if( pgno==1 && nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){ in recoverGetPage()
13323 aPg = p->pPage1Disk; in recoverGetPage()
13325 sqlite3_result_blob(pCtx, aPg, nPg-p->nReserve, SQLITE_TRANSIENT); in recoverGetPage()
13331 if( p->errCode ){ in recoverGetPage()
13332 if( p->zErrMsg ) sqlite3_result_error(pCtx, p->zErrMsg, -1); in recoverGetPage()
13333 sqlite3_result_error_code(pCtx, p->errCode); in recoverGetPage()
13338 ** Find a string that is not found anywhere in z[]. Return a pointer
13360 ** this function is the output of built-in function quote(). If the first
13448 ** This function is a no-op if recover handle p already contains an error
13449 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
13461 return recoverExec(p, p->dbOut, in recoverCacheSchema()
13482 ** this function is a no-op. Otherwise, issue a callback with SQL statement
13485 ** If the callback returns non-zero, set the recover handle error code to
13489 if( p->errCode==SQLITE_OK && p->xSql ){ in recoverSqlCallback()
13490 int res = p->xSql(p->pSqlCtx, zSql); in recoverSqlCallback()
13492 recoverError(p, SQLITE_ERROR, "callback returned an error - %d", res); in recoverSqlCallback()
13501 ** + page-size,
13502 ** + auto-vacuum settings,
13504 ** + user-version (PRAGMA user_version), and
13505 ** + application-id (PRAGMA application_id), and
13520 if( p->errCode==SQLITE_OK ){ in recoverTransferSettings()
13531 p1 = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.%s", p->zDb, zPrag); in recoverTransferSettings()
13532 if( p->errCode==SQLITE_OK && sqlite3_step(p1)==SQLITE_ROW ){ in recoverTransferSettings()
13546 if( p->errCode==SQLITE_OK ){ in recoverTransferSettings()
13547 sqlite3 *db = p->dbOut; in recoverTransferSettings()
13550 sqlite3_backup_step(pBackup, -1); in recoverTransferSettings()
13551 p->errCode = sqlite3_backup_finish(pBackup); in recoverTransferSettings()
13562 ** This function is a no-op if recover handle p already contains an error
13563 ** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in
13591 assert( p->dbOut==0 ); in recoverOpenOutput()
13593 if( sqlite3_open_v2(p->zUri, &db, flags, 0) ){ in recoverOpenOutput()
13598 ** These two are registered with the output database handle - this in recoverOpenOutput()
13601 if( p->errCode==SQLITE_OK ){ in recoverOpenOutput()
13602 p->errCode = sqlite3_dbdata_init(db, 0, 0); in recoverOpenOutput()
13605 /* Register the custom user-functions with the output handle. */ in recoverOpenOutput()
13606 for(ii=0; p->errCode==SQLITE_OK && ii<sizeof(aFunc)/sizeof(aFunc[0]); ii++){ in recoverOpenOutput()
13607 p->errCode = sqlite3_create_function(db, aFunc[ii].zName, in recoverOpenOutput()
13612 p->dbOut = db; in recoverOpenOutput()
13613 return p->errCode; in recoverOpenOutput()
13622 char *zSql = recoverMPrintf(p, "ATTACH %Q AS recovery;", p->zStateDb); in recoverOpenRecovery()
13623 recoverExec(p, p->dbOut, zSql); in recoverOpenRecovery()
13624 recoverExec(p, p->dbOut, in recoverOpenRecovery()
13634 ** This function is a no-op if recover handle p already contains an error
13635 ** (if p->errCode!=SQLITE_OK).
13651 sqlite3_stmt *pStmt = recoverPreparePrintf(p, p->dbOut, in recoverAddTable()
13656 int iPk = -1; in recoverAddTable()
13674 pNew->aCol = (RecoverColumn*)&pNew[1]; in recoverAddTable()
13675 pNew->zTab = csr = (char*)&pNew->aCol[nCol]; in recoverAddTable()
13676 pNew->nCol = nCol; in recoverAddTable()
13677 pNew->iRoot = iRoot; in recoverAddTable()
13688 if( iPk==-1 && iPKF==1 && !sqlite3_stricmp("integer", zType) ) iPk = i; in recoverAddTable()
13689 if( iPKF>1 ) iPk = -2; in recoverAddTable()
13690 pNew->aCol[i].zCol = csr; in recoverAddTable()
13691 pNew->aCol[i].eHidden = eHidden; in recoverAddTable()
13693 pNew->aCol[i].iField = -1; in recoverAddTable()
13695 pNew->aCol[i].iField = iField++; in recoverAddTable()
13700 pNew->aCol[i].iBind = iBind++; in recoverAddTable()
13706 pNew->pNext = p->pTblList; in recoverAddTable()
13707 p->pTblList = pNew; in recoverAddTable()
13708 pNew->bIntkey = 1; in recoverAddTable()
13713 pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName); in recoverAddTable()
13718 assert( iField<pNew->nCol && iCol<pNew->nCol ); in recoverAddTable()
13719 pNew->aCol[iCol].iField = iField; in recoverAddTable()
13721 pNew->bIntkey = 0; in recoverAddTable()
13722 iPk = -2; in recoverAddTable()
13726 if( p->errCode==SQLITE_OK ){ in recoverAddTable()
13728 pNew->aCol[iPk].bIPK = 1; in recoverAddTable()
13729 }else if( pNew->bIntkey ){ in recoverAddTable()
13730 pNew->iRowidBind = iBind++; in recoverAddTable()
13759 pSelect = recoverPrepare(p, p->dbOut, in recoverWriteSchema1()
13773 pTblname = recoverPrepare(p, p->dbOut, in recoverWriteSchema1()
13779 sqlite3_bind_int(pSelect, 1, p->bSlowIndexes); in recoverWriteSchema1()
13795 rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0); in recoverWriteSchema1()
13806 recoverDbError(p, p->dbOut); in recoverWriteSchema1()
13814 return p->errCode; in recoverWriteSchema1()
13820 ** database by recoverWriteSchema1() - everything except for tables and
13825 ** * non-UNIQUE indexes.
13833 pSelect = recoverPrepare(p, p->dbOut, in recoverWriteSchema2()
13834 p->bSlowIndexes ? in recoverWriteSchema2()
13845 int rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0); in recoverWriteSchema2()
13849 recoverDbError(p, p->dbOut); in recoverWriteSchema2()
13855 return p->errCode; in recoverWriteSchema2()
13859 ** This function is a no-op if recover handle p already contains an error
13860 ** (if p->errCode!=SQLITE_OK). In this case it returns NULL.
13882 ** a single text value - itself an SQL statement similar to the above,
13905 int bSql = p->xSql ? 1 : 0; in recoverInsertStmt()
13909 assert( nField<=pTab->nCol ); in recoverInsertStmt()
13911 zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab); in recoverInsertStmt()
13913 if( pTab->iRowidBind ){ in recoverInsertStmt()
13914 assert( pTab->bIntkey ); in recoverInsertStmt()
13917 zBind = recoverMPrintf(p, "%zquote(?%d)", zBind, pTab->iRowidBind); in recoverInsertStmt()
13919 zBind = recoverMPrintf(p, "%z?%d", zBind, pTab->iRowidBind); in recoverInsertStmt()
13926 int eHidden = pTab->aCol[ii].eHidden; in recoverInsertStmt()
13930 assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 ); in recoverInsertStmt()
13931 zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol); in recoverInsertStmt()
13935 "%z%sescape_crnl(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind in recoverInsertStmt()
13939 zBind = recoverMPrintf(p, "%z%s?%d", zBind, zSep, pTab->aCol[ii].iBind); in recoverInsertStmt()
13953 pRet = recoverPrepare(p, p->dbOut, zFinal); in recoverInsertStmt()
13963 ** Search the list of RecoverTable objects at p->pTblList for one that
13965 ** return a pointer to it. Otherwise, return NULL.
13969 for(pRet=p->pTblList; pRet && pRet->iRoot!=iRoot; pRet=pRet->pNext); in recoverFindTable()
13975 ** output db. If successful, it returns a pointer to a buffer containing
13990 pProbe = recoverPrepare(p, p->dbOut, in recoverLostAndFoundCreate()
13993 for(ii=-1; zTbl==0 && p->errCode==SQLITE_OK && ii<1000; ii++){ in recoverLostAndFoundCreate()
13996 zTbl = recoverMPrintf(p, "%s", p->zLostAndFound); in recoverLostAndFoundCreate()
13998 zTbl = recoverMPrintf(p, "%s_%d", p->zLostAndFound, ii); in recoverLostAndFoundCreate()
14001 if( p->errCode==SQLITE_OK ){ in recoverLostAndFoundCreate()
14002 sqlite3_bind_text(pProbe, 1, zTbl, -1, SQLITE_STATIC); in recoverLostAndFoundCreate()
14023 for(ii=0; p->errCode==SQLITE_OK && ii<nField; ii++){ in recoverLostAndFoundCreate()
14031 recoverExec(p, p->dbOut, zSql); in recoverLostAndFoundCreate()
14034 }else if( p->errCode==SQLITE_OK ){ in recoverLostAndFoundCreate()
14036 p, SQLITE_ERROR, "failed to create %s output table", p->zLostAndFound in recoverLostAndFoundCreate()
14058 if( p->xSql==0 ){ in recoverLostAndFoundInsert()
14063 p, p->dbOut, "INSERT INTO %s VALUES(%s)", zTab, zBind in recoverLostAndFoundInsert()
14072 p, p->dbOut, "SELECT 'INSERT INTO %s VALUES(' || %s || ')'", zTab, zBind in recoverLostAndFoundInsert()
14082 ** lost-and-found table of the output database. This function attempts
14094 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFoundFindRoot()
14096 if( pLaf->pFindRoot==0 ){ in recoverLostAndFoundFindRoot()
14097 pLaf->pFindRoot = recoverPrepare(p, p->dbOut, in recoverLostAndFoundFindRoot()
14107 if( p->errCode==SQLITE_OK ){ in recoverLostAndFoundFindRoot()
14108 sqlite3_bind_int64(pLaf->pFindRoot, 1, iPg); in recoverLostAndFoundFindRoot()
14109 if( sqlite3_step(pLaf->pFindRoot)==SQLITE_ROW ){ in recoverLostAndFoundFindRoot()
14110 *piRoot = sqlite3_column_int64(pLaf->pFindRoot, 0); in recoverLostAndFoundFindRoot()
14114 recoverReset(p, pLaf->pFindRoot); in recoverLostAndFoundFindRoot()
14116 return p->errCode; in recoverLostAndFoundFindRoot()
14121 ** the lost-and-found table in the output database.
14124 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFoundOnePage()
14125 sqlite3_value **apVal = pLaf->apVal; in recoverLostAndFoundOnePage()
14126 sqlite3_stmt *pPageData = pLaf->pPageData; in recoverLostAndFoundOnePage()
14127 sqlite3_stmt *pInsert = pLaf->pInsert; in recoverLostAndFoundOnePage()
14129 int nVal = -1; in recoverLostAndFoundOnePage()
14138 while( p->errCode==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPageData) ){ in recoverLostAndFoundOnePage()
14165 nVal = -1; in recoverLostAndFoundOnePage()
14171 assert( nVal==-1 ); in recoverLostAndFoundOnePage()
14175 }else if( iField<pLaf->nMaxField ){ in recoverLostAndFoundOnePage()
14178 assert( iField==nVal || (nVal==-1 && iField==0) ); in recoverLostAndFoundOnePage()
14198 ** RECOVER_STATE_LOSTANDFOUND3 state - during which the lost-and-found
14203 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound3Step()
14204 if( p->errCode==SQLITE_OK ){ in recoverLostAndFound3Step()
14205 if( pLaf->pInsert==0 ){ in recoverLostAndFound3Step()
14208 if( p->errCode==SQLITE_OK ){ in recoverLostAndFound3Step()
14209 int res = sqlite3_step(pLaf->pAllPage); in recoverLostAndFound3Step()
14211 i64 iPage = sqlite3_column_int64(pLaf->pAllPage, 0); in recoverLostAndFound3Step()
14212 if( recoverBitmapQuery(pLaf->pUsed, iPage)==0 ){ in recoverLostAndFound3Step()
14216 recoverReset(p, pLaf->pAllPage); in recoverLostAndFound3Step()
14227 ** state - during which the lost-and-found table of the output database
14232 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound3Init()
14234 if( pLaf->nMaxField>0 ){ in recoverLostAndFound3Init()
14237 zTab = recoverLostAndFoundCreate(p, pLaf->nMaxField); in recoverLostAndFound3Init()
14238 pLaf->pInsert = recoverLostAndFoundInsert(p, zTab, pLaf->nMaxField); in recoverLostAndFound3Init()
14241 pLaf->pAllPage = recoverPreparePrintf(p, p->dbOut, in recoverLostAndFound3Init()
14245 "SELECT ii FROM seq" , p->laf.nPg in recoverLostAndFound3Init()
14247 pLaf->pPageData = recoverPrepare(p, p->dbOut, in recoverLostAndFound3Init()
14251 "SELECT -1, -1, -1" in recoverLostAndFound3Init()
14254 pLaf->apVal = (sqlite3_value**)recoverMalloc(p, in recoverLostAndFound3Init()
14255 pLaf->nMaxField*sizeof(sqlite3_value*) in recoverLostAndFound3Init()
14261 ** Initialize resources required in RECOVER_STATE_WRITING state - during which
14266 RecoverStateW1 *p1 = &p->w1; in recoverWriteDataInit()
14271 assert( p1->nMax==0 ); in recoverWriteDataInit()
14272 for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){ in recoverWriteDataInit()
14273 if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol; in recoverWriteDataInit()
14278 nByte = sizeof(sqlite3_value*) * (p1->nMax+1); in recoverWriteDataInit()
14279 p1->apVal = (sqlite3_value**)recoverMalloc(p, nByte); in recoverWriteDataInit()
14280 if( p1->apVal==0 ) return p->errCode; in recoverWriteDataInit()
14284 p1->pTbls = recoverPrepare(p, p->dbOut, in recoverWriteDataInit()
14289 p1->pSel = recoverPrepare(p, p->dbOut, in recoverWriteDataInit()
14302 return p->errCode; in recoverWriteDataInit()
14310 RecoverStateW1 *p1 = &p->w1; in recoverWriteDataCleanup()
14312 for(ii=0; ii<p1->nVal; ii++){ in recoverWriteDataCleanup()
14313 sqlite3_value_free(p1->apVal[ii]); in recoverWriteDataCleanup()
14315 sqlite3_free(p1->apVal); in recoverWriteDataCleanup()
14316 recoverFinalize(p, p1->pInsert); in recoverWriteDataCleanup()
14317 recoverFinalize(p, p1->pTbls); in recoverWriteDataCleanup()
14318 recoverFinalize(p, p1->pSel); in recoverWriteDataCleanup()
14325 ** RECOVER_STATE_WRITING state - during which tables recovered from the
14329 RecoverStateW1 *p1 = &p->w1; in recoverWriteDataStep()
14330 sqlite3_stmt *pSel = p1->pSel; in recoverWriteDataStep()
14331 sqlite3_value **apVal = p1->apVal; in recoverWriteDataStep()
14333 if( p->errCode==SQLITE_OK && p1->pTab==0 ){ in recoverWriteDataStep()
14334 if( sqlite3_step(p1->pTbls)==SQLITE_ROW ){ in recoverWriteDataStep()
14335 i64 iRoot = sqlite3_column_int64(p1->pTbls, 0); in recoverWriteDataStep()
14336 p1->pTab = recoverFindTable(p, iRoot); in recoverWriteDataStep()
14338 recoverFinalize(p, p1->pInsert); in recoverWriteDataStep()
14339 p1->pInsert = 0; in recoverWriteDataStep()
14343 if( p1->pTab==0 ) return p->errCode; in recoverWriteDataStep()
14347 ** keys before recovering its contents. The p1->pTbls SELECT statement in recoverWriteDataStep()
14350 if( sqlite3_stricmp("sqlite_sequence", p1->pTab->zTab)==0 ){ in recoverWriteDataStep()
14351 recoverExec(p, p->dbOut, "DELETE FROM sqlite_sequence"); in recoverWriteDataStep()
14356 ** SELECT statement p1->pSel. The SELECT statement will then iterate in recoverWriteDataStep()
14360 p1->nVal = 0; in recoverWriteDataStep()
14361 p1->bHaveRowid = 0; in recoverWriteDataStep()
14362 p1->iPrevPage = -1; in recoverWriteDataStep()
14363 p1->iPrevCell = -1; in recoverWriteDataStep()
14368 assert( p->errCode!=SQLITE_OK || p1->pTab ); in recoverWriteDataStep()
14370 if( p->errCode==SQLITE_OK && sqlite3_step(pSel)==SQLITE_ROW ){ in recoverWriteDataStep()
14371 RecoverTable *pTab = p1->pTab; in recoverWriteDataStep()
14377 int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell); in recoverWriteDataStep()
14379 assert( bNewCell==0 || (iField==-1 || iField==0) ); in recoverWriteDataStep()
14380 assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol ); in recoverWriteDataStep()
14384 if( p1->nVal>=0 ){ in recoverWriteDataStep()
14385 if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){ in recoverWriteDataStep()
14386 recoverFinalize(p, p1->pInsert); in recoverWriteDataStep()
14387 p1->pInsert = recoverInsertStmt(p, pTab, p1->nVal); in recoverWriteDataStep()
14388 p1->nInsert = p1->nVal; in recoverWriteDataStep()
14390 if( p1->nVal>0 ){ in recoverWriteDataStep()
14391 sqlite3_stmt *pInsert = p1->pInsert; in recoverWriteDataStep()
14392 for(ii=0; ii<pTab->nCol; ii++){ in recoverWriteDataStep()
14393 RecoverColumn *pCol = &pTab->aCol[ii]; in recoverWriteDataStep()
14394 int iBind = pCol->iBind; in recoverWriteDataStep()
14396 if( pCol->bIPK ){ in recoverWriteDataStep()
14397 sqlite3_bind_int64(pInsert, iBind, p1->iRowid); in recoverWriteDataStep()
14398 }else if( pCol->iField<p1->nVal ){ in recoverWriteDataStep()
14399 recoverBindValue(p, pInsert, iBind, apVal[pCol->iField]); in recoverWriteDataStep()
14403 if( p->bRecoverRowid && pTab->iRowidBind>0 && p1->bHaveRowid ){ in recoverWriteDataStep()
14404 sqlite3_bind_int64(pInsert, pTab->iRowidBind, p1->iRowid); in recoverWriteDataStep()
14411 assert( p->errCode || pInsert ); in recoverWriteDataStep()
14416 for(ii=0; ii<p1->nVal; ii++){ in recoverWriteDataStep()
14420 p1->nVal = -1; in recoverWriteDataStep()
14421 p1->bHaveRowid = 0; in recoverWriteDataStep()
14426 p1->iRowid = sqlite3_column_int64(pSel, 3); in recoverWriteDataStep()
14427 assert( p1->nVal==-1 ); in recoverWriteDataStep()
14428 p1->nVal = 0; in recoverWriteDataStep()
14429 p1->bHaveRowid = 1; in recoverWriteDataStep()
14430 }else if( iField<pTab->nCol ){ in recoverWriteDataStep()
14436 p1->nVal = iField+1; in recoverWriteDataStep()
14438 p1->iPrevCell = iCell; in recoverWriteDataStep()
14439 p1->iPrevPage = iPage; in recoverWriteDataStep()
14443 p1->pTab = 0; in recoverWriteDataStep()
14446 return p->errCode; in recoverWriteDataStep()
14451 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
14455 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound1Init()
14458 assert( p->laf.pUsed==0 ); in recoverLostAndFound1Init()
14459 pLaf->nPg = recoverPageCount(p); in recoverLostAndFound1Init()
14460 pLaf->pUsed = recoverBitmapAlloc(p, pLaf->nPg); in recoverLostAndFound1Init()
14464 ** if !p->bFreelistCorrupt, add all pages that appear to be part of the in recoverLostAndFound1Init()
14467 p, p->dbOut, in recoverLostAndFound1Init()
14477 " SELECT data, min(16384, read_i32(data, 1)-1), pgno FROM trunkdata" in recoverLostAndFound1Init()
14479 " SELECT data, n-1, read_i32(data, 2+n) FROM freelist WHERE n>=0" in recoverLostAndFound1Init()
14496 if( pStmt ) sqlite3_bind_int(pStmt, 1, p->bFreelistCorrupt); in recoverLostAndFound1Init()
14497 pLaf->pUsedPages = pStmt; in recoverLostAndFound1Init()
14503 ** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not
14507 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound1Step()
14508 int rc = p->errCode; in recoverLostAndFound1Step()
14510 rc = sqlite3_step(pLaf->pUsedPages); in recoverLostAndFound1Step()
14512 i64 iPg = sqlite3_column_int64(pLaf->pUsedPages, 0); in recoverLostAndFound1Step()
14513 recoverBitmapSet(pLaf->pUsed, iPg); in recoverLostAndFound1Step()
14516 recoverFinalize(p, pLaf->pUsedPages); in recoverLostAndFound1Step()
14517 pLaf->pUsedPages = 0; in recoverLostAndFound1Step()
14525 ** state - during which the pages identified in RECOVER_STATE_LOSTANDFOUND1
14529 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound2Init()
14531 assert( p->laf.pAllAndParent==0 ); in recoverLostAndFound2Init()
14532 assert( p->laf.pMapInsert==0 ); in recoverLostAndFound2Init()
14533 assert( p->laf.pMaxField==0 ); in recoverLostAndFound2Init()
14534 assert( p->laf.nMaxField==0 ); in recoverLostAndFound2Init()
14536 pLaf->pMapInsert = recoverPrepare(p, p->dbOut, in recoverLostAndFound2Init()
14539 pLaf->pAllAndParent = recoverPreparePrintf(p, p->dbOut, in recoverLostAndFound2Init()
14545 "SELECT NULL, ii FROM seq", p->laf.nPg in recoverLostAndFound2Init()
14547 pLaf->pMaxField = recoverPreparePrintf(p, p->dbOut, in recoverLostAndFound2Init()
14555 ** RECOVER_STATE_LOSTANDFOUND2 state - during which the pages identified
14560 RecoverStateLAF *pLaf = &p->laf; in recoverLostAndFound2Step()
14561 if( p->errCode==SQLITE_OK ){ in recoverLostAndFound2Step()
14562 int res = sqlite3_step(pLaf->pAllAndParent); in recoverLostAndFound2Step()
14564 i64 iChild = sqlite3_column_int(pLaf->pAllAndParent, 1); in recoverLostAndFound2Step()
14565 if( recoverBitmapQuery(pLaf->pUsed, iChild)==0 ){ in recoverLostAndFound2Step()
14566 sqlite3_bind_int64(pLaf->pMapInsert, 1, iChild); in recoverLostAndFound2Step()
14567 sqlite3_bind_value(pLaf->pMapInsert, 2, in recoverLostAndFound2Step()
14568 sqlite3_column_value(pLaf->pAllAndParent, 0) in recoverLostAndFound2Step()
14570 sqlite3_step(pLaf->pMapInsert); in recoverLostAndFound2Step()
14571 recoverReset(p, pLaf->pMapInsert); in recoverLostAndFound2Step()
14572 sqlite3_bind_int64(pLaf->pMaxField, 1, iChild); in recoverLostAndFound2Step()
14573 if( SQLITE_ROW==sqlite3_step(pLaf->pMaxField) ){ in recoverLostAndFound2Step()
14574 int nMax = sqlite3_column_int(pLaf->pMaxField, 0); in recoverLostAndFound2Step()
14575 if( nMax>pLaf->nMaxField ) pLaf->nMaxField = nMax; in recoverLostAndFound2Step()
14577 recoverReset(p, pLaf->pMaxField); in recoverLostAndFound2Step()
14580 recoverFinalize(p, pLaf->pAllAndParent); in recoverLostAndFound2Step()
14581 pLaf->pAllAndParent =0; in recoverLostAndFound2Step()
14585 return p->errCode; in recoverLostAndFound2Step()
14593 recoverBitmapFree(p->laf.pUsed); in recoverLostAndFoundCleanup()
14594 p->laf.pUsed = 0; in recoverLostAndFoundCleanup()
14595 sqlite3_finalize(p->laf.pUsedPages); in recoverLostAndFoundCleanup()
14596 sqlite3_finalize(p->laf.pAllAndParent); in recoverLostAndFoundCleanup()
14597 sqlite3_finalize(p->laf.pMapInsert); in recoverLostAndFoundCleanup()
14598 sqlite3_finalize(p->laf.pMaxField); in recoverLostAndFoundCleanup()
14599 sqlite3_finalize(p->laf.pFindRoot); in recoverLostAndFoundCleanup()
14600 sqlite3_finalize(p->laf.pInsert); in recoverLostAndFoundCleanup()
14601 sqlite3_finalize(p->laf.pAllPage); in recoverLostAndFoundCleanup()
14602 sqlite3_finalize(p->laf.pPageData); in recoverLostAndFoundCleanup()
14603 p->laf.pUsedPages = 0; in recoverLostAndFoundCleanup()
14604 p->laf.pAllAndParent = 0; in recoverLostAndFoundCleanup()
14605 p->laf.pMapInsert = 0; in recoverLostAndFoundCleanup()
14606 p->laf.pMaxField = 0; in recoverLostAndFoundCleanup()
14607 p->laf.pFindRoot = 0; in recoverLostAndFoundCleanup()
14608 p->laf.pInsert = 0; in recoverLostAndFoundCleanup()
14609 p->laf.pAllPage = 0; in recoverLostAndFoundCleanup()
14610 p->laf.pPageData = 0; in recoverLostAndFoundCleanup()
14611 sqlite3_free(p->laf.apVal); in recoverLostAndFoundCleanup()
14612 p->laf.apVal = 0; in recoverLostAndFoundCleanup()
14625 for(pTab=p->pTblList; pTab; pTab=pNext){ in recoverFinalCleanup()
14626 pNext = pTab->pNext; in recoverFinalCleanup()
14629 p->pTblList = 0; in recoverFinalCleanup()
14630 sqlite3_finalize(p->pGetPage); in recoverFinalCleanup()
14631 p->pGetPage = 0; in recoverFinalCleanup()
14632 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0); in recoverFinalCleanup()
14638 sqlite3_close(p->dbOut); in recoverFinalCleanup()
14641 p->dbOut = 0; in recoverFinalCleanup()
14645 ** Decode and return an unsigned 16-bit big-endian integer value from
14653 ** Decode and return an unsigned 32-bit big-endian integer value from
14678 ** or a prefix thereof appears to contain a well-formed SQLite b-tree page,
14679 ** return the page-size in bytes. Otherwise, if the buffer does not
14680 ** appear to contain a well-formed b-tree page, return 0.
14707 /* Follow the free-list. This is the same format for all b-tree pages. */ in recoverIsValidPage()
14712 if( iFree>(n-4) ) return 0; in recoverIsValidPage()
14743 int X = (eType==0x0D) ? n-35 : (((n-12)*64/255)-23); in recoverIsValidPage()
14744 int M = ((n-12)*32/255)-23; in recoverIsValidPage()
14745 int K = M+((nPayload-M)%(n-4)); in recoverIsValidPage()
14817 assert( pFd->pMethods!=&recover_methods ); in recoverVfsClose()
14818 return pFd->pMethods->xClose(pFd); in recoverVfsClose()
14822 ** Write value v to buffer a[] as a 16-bit big-endian unsigned integer.
14830 ** Write value v to buffer a[] as a 32-bit big-endian unsigned integer.
14840 ** Detect the page-size of the database opened by file-handle pFd by
14841 ** searching the first part of the file for a well-formed SQLite b-tree
14842 ** page. If parameter nReserve is non-zero, then as well as searching for
14843 ** a b-tree page with zero reserved bytes, this function searches for one
14846 ** If successful, set variable p->detected_pgsz to the detected page-size
14848 ** can be found, return SQLITE_OK but leave p->detected_pgsz set to 0. Or,
14850 ** is returned. The final value of p->detected_pgsz is undefined in this
14855 sqlite3_file *pFd, /* File-handle open on input database */ in recoverVfsDetectPagesize()
14873 nBlk = (nSz+nMax-1)/nMax; in recoverVfsDetectPagesize()
14880 rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax); in recoverVfsDetectPagesize()
14886 if( recoverIsValidPage(aTmp, &aPg[iOff], pgsz2-nReserve) ){ in recoverVfsDetectPagesize()
14894 if( pgsz>(u32)p->detected_pgsz ){ in recoverVfsDetectPagesize()
14895 p->detected_pgsz = pgsz; in recoverVfsDetectPagesize()
14896 p->nReserve = nReserve; in recoverVfsDetectPagesize()
14902 p->detected_pgsz = pgsz; in recoverVfsDetectPagesize()
14913 if( pFd->pMethods==&recover_methods ){ in recoverVfsRead()
14914 pFd->pMethods = recover_g.pMethods; in recoverVfsRead()
14915 rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff); in recoverVfsRead()
14923 ** + Database page size (16-bits at offset 16) in recoverVfsRead()
14924 ** + Size of db in pages (32-bits at offset 28) in recoverVfsRead()
14925 ** + Database encoding (32-bits at offset 56) in recoverVfsRead()
14929 ** + first freelist page (32-bits at offset 32) in recoverVfsRead()
14930 ** + size of freelist (32-bits at offset 36) in recoverVfsRead()
14931 ** + the wal-mode flags (16-bits at offset 18) in recoverVfsRead()
14933 ** We also try to preserve the auto-vacuum, incr-value, user-version in recoverVfsRead()
14934 ** and application-id fields - all 32 bit quantities at offsets in recoverVfsRead()
14937 ** Byte offset 105 should also contain the page-size as a 16-bit in recoverVfsRead()
14969 rc = pFd->pMethods->xFileSize(pFd, &dbFileSize); in recoverVfsRead()
14971 if( rc==SQLITE_OK && p->detected_pgsz==0 ){ in recoverVfsRead()
14974 if( p->detected_pgsz ){ in recoverVfsRead()
14975 pgsz = p->detected_pgsz; in recoverVfsRead()
14976 nReserve = p->nReserve; in recoverVfsRead()
14986 sqlite3_free(p->pPage1Cache); in recoverVfsRead()
14987 p->pPage1Cache = 0; in recoverVfsRead()
14988 p->pPage1Disk = 0; in recoverVfsRead()
14990 p->pgsz = nByte; in recoverVfsRead()
14991 p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2); in recoverVfsRead()
14992 if( p->pPage1Cache ){ in recoverVfsRead()
14993 p->pPage1Disk = &p->pPage1Cache[nByte]; in recoverVfsRead()
14994 memcpy(p->pPage1Disk, aBuf, nByte); in recoverVfsRead()
14999 recoverPutU16(&aHdr[105], pgsz-nReserve); in recoverVfsRead()
15007 memset(&((u8*)aBuf)[sizeof(aHdr)], 0, nByte-sizeof(aHdr)); in recoverVfsRead()
15009 memcpy(p->pPage1Cache, aBuf, nByte); in recoverVfsRead()
15011 rc = p->errCode; in recoverVfsRead()
15015 pFd->pMethods = &recover_methods; in recoverVfsRead()
15017 rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff); in recoverVfsRead()
15027 if( pFd->pMethods==&recover_methods ){ \
15028 pFd->pMethods = recover_g.pMethods; \
15030 pFd->pMethods = &recover_methods; \
15046 pFd->pMethods->xWrite(pFd, aBuf, nByte, iOff) in recoverVfsWrite()
15051 pFd->pMethods->xTruncate(pFd, size) in recoverVfsTruncate()
15056 pFd->pMethods->xSync(pFd, flags) in recoverVfsSync()
15061 pFd->pMethods->xFileSize(pFd, pSize) in recoverVfsFileSize()
15066 pFd->pMethods->xLock(pFd, eLock) in recoverVfsLock()
15071 pFd->pMethods->xUnlock(pFd, eLock) in recoverVfsUnlock()
15076 pFd->pMethods->xCheckReservedLock(pFd, pResOut) in recoverVfsCheckReservedLock()
15081 (pFd->pMethods ? pFd->pMethods->xFileControl(pFd, op, pArg) : SQLITE_NOTFOUND) in recoverVfsFileControl()
15086 pFd->pMethods->xSectorSize(pFd) in recoverVfsSectorSize()
15091 pFd->pMethods->xDeviceCharacteristics(pFd) in recoverVfsDeviceCharacteristics()
15098 pFd->pMethods->xShmMap(pFd, iPg, pgsz, bExtend, pp) in recoverVfsShmMap()
15103 pFd->pMethods->xShmLock(pFd, offset, n, flags) in recoverVfsShmLock()
15107 if( pFd->pMethods==&recover_methods ){ in recoverVfsShmBarrier()
15108 pFd->pMethods = recover_g.pMethods; in recoverVfsShmBarrier()
15109 pFd->pMethods->xShmBarrier(pFd); in recoverVfsShmBarrier()
15110 pFd->pMethods = &recover_methods; in recoverVfsShmBarrier()
15112 pFd->pMethods->xShmBarrier(pFd); in recoverVfsShmBarrier()
15117 pFd->pMethods->xShmUnmap(pFd, deleteFlag) in recoverVfsShmUnmap()
15135 ** Install the VFS wrapper around the file-descriptor open on the input
15143 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd); in recoverInstallWrapper()
15144 assert( pFd==0 || pFd->pMethods!=&recover_methods ); in recoverInstallWrapper()
15145 if( pFd && pFd->pMethods ){ in recoverInstallWrapper()
15146 int iVersion = 1 + (pFd->pMethods->iVersion>1 && pFd->pMethods->xShmMap!=0); in recoverInstallWrapper()
15147 recover_g.pMethods = pFd->pMethods; in recoverInstallWrapper()
15150 pFd->pMethods = &recover_methods; in recoverInstallWrapper()
15155 ** Uninstall the VFS wrapper that was installed around the file-descriptor open
15162 sqlite3_file_control(p->dbIn, p->zDb,SQLITE_FCNTL_FILE_POINTER,(void*)&pFd); in recoverUninstallWrapper()
15163 if( pFd && pFd->pMethods ){ in recoverUninstallWrapper()
15164 pFd->pMethods = recover_g.pMethods; in recoverUninstallWrapper()
15176 assert( p && p->errCode==SQLITE_OK ); in recoverStep()
15177 switch( p->eState ){ in recoverStep()
15192 sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0); in recoverStep()
15193 recoverExec(p, p->dbIn, "PRAGMA writable_schema = on"); in recoverStep()
15194 recoverExec(p, p->dbIn, "BEGIN"); in recoverStep()
15195 if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1; in recoverStep()
15196 recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema"); in recoverStep()
15204 recoverExec(p, p->dbOut, "BEGIN"); in recoverStep()
15207 p->eState = RECOVER_STATE_WRITING; in recoverStep()
15211 if( p->w1.pTbls==0 ){ in recoverStep()
15216 if( p->zLostAndFound ){ in recoverStep()
15217 p->eState = RECOVER_STATE_LOSTANDFOUND1; in recoverStep()
15219 p->eState = RECOVER_STATE_SCHEMA2; in recoverStep()
15226 if( p->laf.pUsed==0 ){ in recoverStep()
15230 p->eState = RECOVER_STATE_LOSTANDFOUND2; in recoverStep()
15235 if( p->laf.pAllAndParent==0 ){ in recoverStep()
15239 p->eState = RECOVER_STATE_LOSTANDFOUND3; in recoverStep()
15245 if( p->laf.pInsert==0 ){ in recoverStep()
15249 p->eState = RECOVER_STATE_SCHEMA2; in recoverStep()
15258 p->eState = RECOVER_STATE_DONE; in recoverStep()
15263 recoverExec(p, p->dbOut, "COMMIT"); in recoverStep()
15264 rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0); in recoverStep()
15265 if( p->errCode==SQLITE_OK ) p->errCode = rc; in recoverStep()
15269 p->eState = RECOVER_STATE_DONE; in recoverStep()
15275 /* no-op */ in recoverStep()
15314 pRet->dbIn = db; in recoverInit()
15315 pRet->zDb = (char*)&pRet[1]; in recoverInit()
15316 pRet->zUri = &pRet->zDb[nDb+1]; in recoverInit()
15317 memcpy(pRet->zDb, zDb, nDb); in recoverInit()
15318 if( nUri>0 && zUri ) memcpy(pRet->zUri, zUri, nUri); in recoverInit()
15319 pRet->xSql = xSql; in recoverInit()
15320 pRet->pSqlCtx = pSqlCtx; in recoverInit()
15321 pRet->bRecoverRowid = RECOVER_ROWID_DEFAULT; in recoverInit()
15356 return (p && p->errCode!=SQLITE_NOMEM) ? p->zErrMsg : "out of memory"; in sqlite3_recover_errmsg()
15363 return p ? p->errCode : SQLITE_NOMEM; in sqlite3_recover_errcode()
15373 }else if( p->eState!=RECOVER_STATE_INIT ){ in sqlite3_recover_config()
15379 ** name of the auxiliary database that is ATTACH-ed to the database in sqlite3_recover_config()
15383 sqlite3_free(p->zStateDb); in sqlite3_recover_config()
15384 p->zStateDb = recoverMPrintf(p, "%s", (char*)pArg); in sqlite3_recover_config()
15389 sqlite3_free(p->zLostAndFound); in sqlite3_recover_config()
15391 p->zLostAndFound = recoverMPrintf(p, "%s", zArg); in sqlite3_recover_config()
15393 p->zLostAndFound = 0; in sqlite3_recover_config()
15399 p->bFreelistCorrupt = *(int*)pArg; in sqlite3_recover_config()
15403 p->bRecoverRowid = *(int*)pArg; in sqlite3_recover_config()
15407 p->bSlowIndexes = *(int*)pArg; in sqlite3_recover_config()
15427 if( p->errCode==SQLITE_OK ) recoverStep(p); in sqlite3_recover_step()
15428 if( p->eState==RECOVER_STATE_DONE && p->errCode==SQLITE_OK ){ in sqlite3_recover_step()
15431 return p->errCode; in sqlite3_recover_step()
15459 if( p->bCloseTransaction && sqlite3_get_autocommit(p->dbIn)==0 ){ in sqlite3_recover_finish()
15460 rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0); in sqlite3_recover_finish()
15461 if( p->errCode==SQLITE_OK ) p->errCode = rc; in sqlite3_recover_finish()
15463 rc = p->errCode; in sqlite3_recover_finish()
15464 sqlite3_free(p->zErrMsg); in sqlite3_recover_finish()
15465 sqlite3_free(p->zStateDb); in sqlite3_recover_finish()
15466 sqlite3_free(p->zLostAndFound); in sqlite3_recover_finish()
15467 sqlite3_free(p->pPage1Cache); in sqlite3_recover_finish()
15536 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
15540 u8 bSafeModePersist; /* The long-term value of bSafeMode */
15565 sqlite3_int64 szMax; /* --maxsize argument to .open */
15582 sqlite3 *db; /* Connection pointer */
15594 char *zNonce; /* Nonce for temporary safe-mode excapes */
15620 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
15624 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
15638 ** top-level SQL statement */
15639 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
15644 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
15646 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
15648 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
15650 #define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */
15658 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
15659 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
15660 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
15672 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
15676 #define MODE_Pretty 11 /* Pretty-print schemas */
15680 #define MODE_Table 15 /* MySQL-style table formatting */
15681 #define MODE_Box 16 /* Unicode box-drawing characters */
15731 if( p->pLog==0 ) return; in shellLog()
15732 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg); in shellLog()
15733 fflush(p->pLog); in shellLog()
15749 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0])); in shellPutsFunc()
15762 if( p->bSafeMode ){ in failIfSafeMode()
15768 raw_printf(stderr, "line %d: ", p->lineno); in failIfSafeMode()
15788 ** Also throw an error if the EDITOR program returns a non-zero exit code.
15814 sqlite3_result_error(context, "no editor for edit()", -1); in editFunc()
15818 sqlite3_result_error(context, "NULL input to edit()", -1); in editFunc()
15838 sqlite3_result_error(context, "edit() cannot open temp file", -1); in editFunc()
15853 sqlite3_result_error(context, "edit() could not write the whole file", -1); in editFunc()
15864 sqlite3_result_error(context, "EDITOR returned non-zero", -1); in editFunc()
15870 "edit() cannot reopen temp file after edit", -1); in editFunc()
15885 sqlite3_result_error(context, "could not read back the whole file", -1); in editFunc()
15921 p->modePrior = p->mode; in outputModePush()
15922 p->priorShFlgs = p->shellFlgs; in outputModePush()
15923 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); in outputModePush()
15924 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); in outputModePush()
15927 p->mode = p->modePrior; in outputModePop()
15928 p->shellFlgs = p->priorShFlgs; in outputModePop()
15929 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); in outputModePop()
15930 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); in outputModePop()
15934 ** Output the given string as a hex-encoded blob (eg. X'1234' )
15958 ** Find a string that is not found anywhere in z[]. Return a pointer
16016 ** get corrupted by end-of-line translation facilities in some operating
16118 while( n-- ){ in output_json_string()
16204 ** Output a single term of CSV. Actually, p->colSeparator is used for
16205 ** the separator, which may or may not be a comma. p->nullValue is
16210 FILE *out = p->out; in output_csv()
16212 utf8_printf(out,"%s",p->nullValue); in output_csv()
16221 if( i==0 || strstr(z, p->colSeparator)!=0 ){ in output_csv()
16231 utf8_printf(p->out, "%s", p->colSeparator); in output_csv()
16236 ** This routine runs when the user presses Ctrl-C
16247 ** This routine runs for console events (e.g. Ctrl-C) on Win32
16340 utf8_printf(p->out, "authorizer: %s", azAction[op]); in shellAuth()
16342 raw_printf(p->out, " "); in shellAuth()
16344 output_c_string(p->out, az[i]); in shellAuth()
16346 raw_printf(p->out, "NULL"); in shellAuth()
16349 raw_printf(p->out, "\n"); in shellAuth()
16350 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4); in shellAuth()
16361 ** If the schema statement in z[] contains a start-of-comment and if
16369 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){ in printSchemaLine()
16377 zNew[n-1] = 0; in printSchemaLine()
16408 if( z[i]=='-' && z[i+1]=='-' ) return 1; in wsToEol()
16422 if( p->autoEQPtest ){ in eqp_append()
16423 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); in eqp_append()
16427 pNew->iEqpId = iEqpId; in eqp_append()
16428 pNew->iParentId = p2; in eqp_append()
16429 memcpy(pNew->zText, zText, nText+1); in eqp_append()
16430 pNew->pNext = 0; in eqp_append()
16431 if( p->sGraph.pLast ){ in eqp_append()
16432 p->sGraph.pLast->pNext = pNew; in eqp_append()
16434 p->sGraph.pRow = pNew; in eqp_append()
16436 p->sGraph.pLast = pNew; in eqp_append()
16441 ** in p->sGraph.
16445 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ in eqp_reset()
16446 pNext = pRow->pNext; in eqp_reset()
16449 memset(&p->sGraph, 0, sizeof(p->sGraph)); in eqp_reset()
16456 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; in eqp_next_row()
16457 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; in eqp_next_row()
16466 i64 n = strlen(p->sGraph.zPrefix); in eqp_render_level()
16470 z = pRow->zText; in eqp_render_level()
16471 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, in eqp_render_level()
16472 pNext ? "|--" : "`--", z); in eqp_render_level()
16473 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){ in eqp_render_level()
16474 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); in eqp_render_level()
16475 eqp_render_level(p, pRow->iEqpId); in eqp_render_level()
16476 p->sGraph.zPrefix[n] = 0; in eqp_render_level()
16485 EQPGraphRow *pRow = p->sGraph.pRow; in eqp_render()
16487 if( pRow->zText[0]=='-' ){ in eqp_render()
16488 if( pRow->pNext==0 ){ in eqp_render()
16492 utf8_printf(p->out, "%s\n", pRow->zText+3); in eqp_render()
16493 p->sGraph.pRow = pRow->pNext; in eqp_render()
16496 utf8_printf(p->out, "QUERY PLAN\n"); in eqp_render()
16498 p->sGraph.zPrefix[0] = 0; in eqp_render()
16510 p->nProgress++; in progress_handler()
16511 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ in progress_handler()
16512 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress); in progress_handler()
16513 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; in progress_handler()
16514 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; in progress_handler()
16517 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ in progress_handler()
16518 raw_printf(p->out, "Progress %u\n", p->nProgress); in progress_handler()
16528 const char zDash[] = "--------------------------------------------------"; in print_dashes()
16529 const int nDash = sizeof(zDash) - 1; in print_dashes()
16532 N -= nDash; in print_dashes()
16538 ** Print a markdown or table-style row separator using ascii-art
16547 fputs(zSep, p->out); in print_row_separator()
16548 print_dashes(p->out, p->actualWidth[0]+2); in print_row_separator()
16550 fputs(zSep, p->out); in print_row_separator()
16551 print_dashes(p->out, p->actualWidth[i]+2); in print_row_separator()
16553 fputs(zSep, p->out); in print_row_separator()
16555 fputs("\n", p->out); in print_row_separator()
16573 switch( p->cMode ){ in shell_callback()
16585 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
16587 utf8_printf(p->out,"%*s = %s%s", w, azCol[i], in shell_callback()
16588 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator); in shell_callback()
16597 if( p->cnt++==0 ){ in shell_callback()
16600 utf8_width_print(p->out, w, azCol[i]); in shell_callback()
16601 fputs(i==nArg-1 ? "\n" : " ", p->out); in shell_callback()
16605 print_dashes(p->out, w); in shell_callback()
16606 fputs(i==nArg-1 ? "\n" : " ", p->out); in shell_callback()
16612 if( i==nArg-1 ) w = 0; in shell_callback()
16616 if( i==1 && p->aiIndent && p->pStmt ){ in shell_callback()
16617 if( p->iIndent<p->nIndent ){ in shell_callback()
16618 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); in shell_callback()
16620 p->iIndent++; in shell_callback()
16622 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue); in shell_callback()
16623 fputs(i==nArg-1 ? "\n" : " ", p->out); in shell_callback()
16628 printSchemaLine(p->out, azArg[0], ";\n"); in shell_callback()
16631 case MODE_Pretty: { /* .schema and .fullschema with --indent */ in shell_callback()
16643 utf8_printf(p->out, "%s;\n", azArg[0]); in shell_callback()
16652 if( z[j-1]=='\r' ) z[j-1] = '\n'; in shell_callback()
16653 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; in shell_callback()
16654 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ in shell_callback()
16655 j--; in shell_callback()
16659 while( j>0 && IsSpace(z[j-1]) ){ j--; } in shell_callback()
16669 }else if( c=='-' && z[i+1]=='-' ){ in shell_callback()
16674 nParen--; in shell_callback()
16676 printSchemaLineN(p->out, z, j, "\n"); in shell_callback()
16684 if( c=='\n' ) j--; in shell_callback()
16685 printSchemaLineN(p->out, z, j, "\n "); in shell_callback()
16693 printSchemaLine(p->out, z, ";\n"); in shell_callback()
16698 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
16700 utf8_printf(p->out,"%s%s",azCol[i], in shell_callback()
16701 i==nArg-1 ? p->rowSeparator : p->colSeparator); in shell_callback()
16707 if( z==0 ) z = p->nullValue; in shell_callback()
16708 utf8_printf(p->out, "%s", z); in shell_callback()
16709 if( i<nArg-1 ){ in shell_callback()
16710 utf8_printf(p->out, "%s", p->colSeparator); in shell_callback()
16712 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
16718 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
16719 raw_printf(p->out,"<TR>"); in shell_callback()
16721 raw_printf(p->out,"<TH>"); in shell_callback()
16722 output_html_string(p->out, azCol[i]); in shell_callback()
16723 raw_printf(p->out,"</TH>\n"); in shell_callback()
16725 raw_printf(p->out,"</TR>\n"); in shell_callback()
16728 raw_printf(p->out,"<TR>"); in shell_callback()
16730 raw_printf(p->out,"<TD>"); in shell_callback()
16731 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); in shell_callback()
16732 raw_printf(p->out,"</TD>\n"); in shell_callback()
16734 raw_printf(p->out,"</TR>\n"); in shell_callback()
16738 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
16740 output_c_string(p->out,azCol[i] ? azCol[i] : ""); in shell_callback()
16741 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); in shell_callback()
16743 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
16747 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue); in shell_callback()
16748 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator); in shell_callback()
16750 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
16754 setBinaryMode(p->out, 1); in shell_callback()
16755 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
16757 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); in shell_callback()
16759 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
16763 output_csv(p, azArg[i], i<nArg-1); in shell_callback()
16765 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
16767 setTextMode(p->out, 1); in shell_callback()
16772 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable); in shell_callback()
16773 if( p->showHeader ){ in shell_callback()
16774 raw_printf(p->out,"("); in shell_callback()
16776 if( i>0 ) raw_printf(p->out, ","); in shell_callback()
16780 utf8_printf(p->out, "%s", z); in shell_callback()
16783 raw_printf(p->out, "%s", azCol[i]); in shell_callback()
16786 raw_printf(p->out,")"); in shell_callback()
16788 p->cnt++; in shell_callback()
16790 raw_printf(p->out, i>0 ? "," : " VALUES("); in shell_callback()
16792 utf8_printf(p->out,"NULL"); in shell_callback()
16795 output_quoted_string(p->out, azArg[i]); in shell_callback()
16797 output_quoted_escaped_string(p->out, azArg[i]); in shell_callback()
16800 utf8_printf(p->out,"%s", azArg[i]); in shell_callback()
16803 double r = sqlite3_column_double(p->pStmt, i); in shell_callback()
16807 raw_printf(p->out, "1e999"); in shell_callback()
16809 raw_printf(p->out, "-1e999"); in shell_callback()
16817 raw_printf(p->out, "%s", z); in shell_callback()
16819 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ in shell_callback()
16820 const void *pBlob = sqlite3_column_blob(p->pStmt, i); in shell_callback()
16821 int nBlob = sqlite3_column_bytes(p->pStmt, i); in shell_callback()
16822 output_hex_blob(p->out, pBlob, nBlob); in shell_callback()
16824 utf8_printf(p->out,"%s", azArg[i]); in shell_callback()
16826 output_quoted_string(p->out, azArg[i]); in shell_callback()
16828 output_quoted_escaped_string(p->out, azArg[i]); in shell_callback()
16831 raw_printf(p->out,");\n"); in shell_callback()
16836 if( p->cnt==0 ){ in shell_callback()
16837 fputs("[{", p->out); in shell_callback()
16839 fputs(",\n{", p->out); in shell_callback()
16841 p->cnt++; in shell_callback()
16843 output_json_string(p->out, azCol[i], -1); in shell_callback()
16844 putc(':', p->out); in shell_callback()
16846 fputs("null",p->out); in shell_callback()
16849 double r = sqlite3_column_double(p->pStmt, i); in shell_callback()
16853 raw_printf(p->out, "1e999"); in shell_callback()
16855 raw_printf(p->out, "-1e999"); in shell_callback()
16858 raw_printf(p->out, "%s", z); in shell_callback()
16860 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ in shell_callback()
16861 const void *pBlob = sqlite3_column_blob(p->pStmt, i); in shell_callback()
16862 int nBlob = sqlite3_column_bytes(p->pStmt, i); in shell_callback()
16863 output_json_string(p->out, pBlob, nBlob); in shell_callback()
16865 output_json_string(p->out, azArg[i], -1); in shell_callback()
16867 utf8_printf(p->out,"%s", azArg[i]); in shell_callback()
16869 if( i<nArg-1 ){ in shell_callback()
16870 putc(',', p->out); in shell_callback()
16873 putc('}', p->out); in shell_callback()
16878 if( p->cnt==0 && p->showHeader ){ in shell_callback()
16880 if( i>0 ) fputs(p->colSeparator, p->out); in shell_callback()
16881 output_quoted_string(p->out, azCol[i]); in shell_callback()
16883 fputs(p->rowSeparator, p->out); in shell_callback()
16885 p->cnt++; in shell_callback()
16887 if( i>0 ) fputs(p->colSeparator, p->out); in shell_callback()
16889 utf8_printf(p->out,"NULL"); in shell_callback()
16891 output_quoted_string(p->out, azArg[i]); in shell_callback()
16893 utf8_printf(p->out,"%s", azArg[i]); in shell_callback()
16896 double r = sqlite3_column_double(p->pStmt, i); in shell_callback()
16898 raw_printf(p->out, "%s", z); in shell_callback()
16899 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ in shell_callback()
16900 const void *pBlob = sqlite3_column_blob(p->pStmt, i); in shell_callback()
16901 int nBlob = sqlite3_column_bytes(p->pStmt, i); in shell_callback()
16902 output_hex_blob(p->out, pBlob, nBlob); in shell_callback()
16904 utf8_printf(p->out,"%s", azArg[i]); in shell_callback()
16906 output_quoted_string(p->out, azArg[i]); in shell_callback()
16909 fputs(p->rowSeparator, p->out); in shell_callback()
16913 if( p->cnt++==0 && p->showHeader ){ in shell_callback()
16915 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); in shell_callback()
16916 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : ""); in shell_callback()
16918 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
16922 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator); in shell_callback()
16923 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue); in shell_callback()
16925 utf8_printf(p->out, "%s", p->rowSeparator); in shell_callback()
16954 if( p->n ) appendText(p, "|", 0); in captureOutputCallback()
16967 sqlite3_exec(p->db, in createSelftestTable()
16978 " 'memo','Tests generated by --init');\n" in createSelftestTable()
17007 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); in createSelftestTable()
17021 if( p->zDestTable ){ in set_table_name()
17022 free(p->zDestTable); in set_table_name()
17023 p->zDestTable = 0; in set_table_name()
17029 z = p->zDestTable = malloc( n+1 ); in set_table_name()
17043 ** syntax error. Return a pointer to the text, in memory obtained from
17063 iOffset--; in shell_error_context()
17065 while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; } in shell_error_context()
17070 while( (zSql[len]&0xc0)==0x80 ) len--; in shell_error_context()
17076 zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode, iOffset, ""); in shell_error_context()
17078 zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode, iOffset-14, ""); in shell_error_context()
17086 ** the result columns, comma-separated, on a line and then add a
17089 ** If the number of columns is 1 and that column contains text "--"
17091 ** "--" comment occurs at the end of the statement, the comment
17103 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); in run_table_dump_query()
17105 char *zContext = shell_error_context(zSelect, p->db); in run_table_dump_query()
17106 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc, in run_table_dump_query()
17107 sqlite3_errmsg(p->db), zContext); in run_table_dump_query()
17109 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; in run_table_dump_query()
17116 utf8_printf(p->out, "%s", z); in run_table_dump_query()
17118 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i)); in run_table_dump_query()
17121 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; in run_table_dump_query()
17123 raw_printf(p->out, "\n;\n"); in run_table_dump_query()
17125 raw_printf(p->out, ";\n"); in run_table_dump_query()
17131 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, in run_table_dump_query()
17132 sqlite3_errmsg(p->db)); in run_table_dump_query()
17133 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; in run_table_dump_query()
17191 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]); in displayLinuxIoStats()
17201 ** Display a single line of status using 64-bit values.
17210 sqlite3_int64 iCur = -1; in displayStatLine()
17211 sqlite3_int64 iHiwtr = -1; in displayStatLine()
17223 raw_printf(p->out, "%-36s %s\n", zLabel, zLine); in displayStatLine()
17231 ShellState *pArg, /* Pointer to ShellState */ in display_stats()
17237 if( pArg==0 || pArg->out==0 ) return 0; in display_stats()
17238 out = pArg->out; in display_stats()
17240 if( pArg->pStmt && pArg->statsOn==2 ){ in display_stats()
17242 sqlite3_stmt *pStmt = pArg->pStmt; in display_stats()
17245 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol); in display_stats()
17248 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i)); in display_stats()
17251 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i)); in display_stats()
17255 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i)); in display_stats()
17257 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i)); in display_stats()
17259 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i)); in display_stats()
17264 if( pArg->statsOn==3 ){ in display_stats()
17265 if( pArg->pStmt ){ in display_stats()
17266 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); in display_stats()
17267 raw_printf(pArg->out, "VM-steps: %d\n", iCur); in display_stats()
17276 if( pArg->shellFlgs & SHFLG_Pagecache ){ in display_stats()
17292 if( pArg->shellFlgs & SHFLG_Lookaside ){ in display_stats()
17293 iHiwtr = iCur = -1; in display_stats()
17296 raw_printf(pArg->out, in display_stats()
17301 raw_printf(pArg->out, "Successful lookaside attempts: %d\n", in display_stats()
17305 raw_printf(pArg->out, "Lookaside failures due to size: %d\n", in display_stats()
17309 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n", in display_stats()
17312 iHiwtr = iCur = -1; in display_stats()
17314 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n", in display_stats()
17316 iHiwtr = iCur = -1; in display_stats()
17318 raw_printf(pArg->out, "Page cache hits: %d\n", iCur); in display_stats()
17319 iHiwtr = iCur = -1; in display_stats()
17321 raw_printf(pArg->out, "Page cache misses: %d\n", iCur); in display_stats()
17322 iHiwtr = iCur = -1; in display_stats()
17324 raw_printf(pArg->out, "Page cache writes: %d\n", iCur); in display_stats()
17325 iHiwtr = iCur = -1; in display_stats()
17327 raw_printf(pArg->out, "Page cache spills: %d\n", iCur); in display_stats()
17328 iHiwtr = iCur = -1; in display_stats()
17330 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n", in display_stats()
17332 iHiwtr = iCur = -1; in display_stats()
17334 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", in display_stats()
17338 if( pArg->pStmt ){ in display_stats()
17340 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, in display_stats()
17342 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur); in display_stats()
17343 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); in display_stats()
17344 raw_printf(pArg->out, "Sort Operations: %d\n", iCur); in display_stats()
17345 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); in display_stats()
17346 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur); in display_stats()
17347 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, bReset); in display_stats()
17348 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, bReset); in display_stats()
17350 raw_printf(pArg->out, "Bloom filter bypass taken: %d/%d\n", in display_stats()
17353 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); in display_stats()
17354 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur); in display_stats()
17355 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); in display_stats()
17356 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur); in display_stats()
17357 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); in display_stats()
17358 raw_printf(pArg->out, "Number of times run: %d\n", iCur); in display_stats()
17359 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); in display_stats()
17360 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur); in display_stats()
17364 displayLinuxIoStats(pArg->out); in display_stats()
17367 /* Do not remove this machine readable comment: extra-stats-output-here */ in display_stats()
17377 ShellState *pArg /* Pointer to ShellState */ in display_scanstats()
17384 raw_printf(pArg->out, "-------- scanstats --------\n"); in display_scanstats()
17389 sqlite3_stmt *p = pArg->pStmt; in display_scanstats()
17402 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k); in display_scanstats()
17408 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain); in display_scanstats()
17410 raw_printf(pArg->out, in display_scanstats()
17411 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n", in display_scanstats()
17416 raw_printf(pArg->out, "---------------------------\n"); in display_scanstats()
17421 ** Parameter azArray points to a zero-terminated array of strings. zStr
17422 ** points to a single nul-terminated string. Return non-zero if zStr
17446 ** See tag-20220407a in wherecode.c and vdbe.c.
17459 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ in explain_data_prepare()
17460 int iOp; /* Index of operation in p->aiIndent[] */ in explain_data_prepare()
17471 p->cMode = p->mode; in explain_data_prepare()
17478 p->cMode = p->mode; in explain_data_prepare()
17490 ** the current instruction is part of a sub-program generated by an in explain_data_prepare()
17493 int p2op = (p2 + (iOp-iAddr)); in explain_data_prepare()
17495 /* Grow the p->aiIndent array as required */ in explain_data_prepare()
17505 p->cMode = p->mode; in explain_data_prepare()
17512 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); in explain_data_prepare()
17513 shell_check_oom(p->aiIndent); in explain_data_prepare()
17518 p->aiIndent[iOp] = 0; in explain_data_prepare()
17519 p->nIndent = iOp+1; in explain_data_prepare()
17522 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; in explain_data_prepare()
17524 if( str_in_array(zOp, azGoto) && p2op<p->nIndent in explain_data_prepare()
17527 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2; in explain_data_prepare()
17531 p->iIndent = 0; in explain_data_prepare()
17540 sqlite3_free(p->aiIndent); in explain_data_delete()
17541 p->aiIndent = 0; in explain_data_delete()
17542 p->nIndent = 0; in explain_data_delete()
17543 p->iIndent = 0; in explain_data_delete()
17567 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode); in bind_table_init()
17568 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); in bind_table_init()
17569 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); in bind_table_init()
17570 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); in bind_table_init()
17571 sqlite3_exec(p->db, in bind_table_init()
17577 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); in bind_table_init()
17578 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0); in bind_table_init()
17601 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", in bind_prepared_stmt()
17605 rc = sqlite3_prepare_v2(pArg->db, in bind_prepared_stmt()
17607 " WHERE key=?1", -1, &pQ, 0); in bind_prepared_stmt()
17616 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); in bind_prepared_stmt()
17628 ** UTF8 box-drawing characters. Imagine box lines like this:
17632 ** 4 --+-- 2
17640 #define BOX_24 "\342\224\200" /* U+2500 --- */
17642 #define BOX_23 "\342\224\214" /* U+250c ,- */
17643 #define BOX_34 "\342\224\220" /* U+2510 -, */
17644 #define BOX_12 "\342\224\224" /* U+2514 '- */
17645 #define BOX_14 "\342\224\230" /* U+2518 -' */
17646 #define BOX_123 "\342\224\234" /* U+251c |- */
17647 #define BOX_134 "\342\224\244" /* U+2524 -| */
17648 #define BOX_234 "\342\224\254" /* U+252c -,- */
17649 #define BOX_124 "\342\224\264" /* U+2534 -'- */
17650 #define BOX_1234 "\342\224\274" /* U+253c -|- */
17659 const int nDash = sizeof(zDash) - 1; in print_box_line()
17663 N -= nDash; in print_box_line()
17680 utf8_printf(p->out, "%s", zSep1); in print_box_row_separator()
17681 print_box_line(p->out, p->actualWidth[0]+2); in print_box_row_separator()
17683 utf8_printf(p->out, "%s", zSep2); in print_box_row_separator()
17684 print_box_line(p->out, p->actualWidth[i]+2); in print_box_row_separator()
17686 utf8_printf(p->out, "%s", zSep3); in print_box_row_separator()
17688 fputs("\n", p->out); in print_box_row_separator()
17706 u8 bWordWrap /* If true, avoid breaking mid-word */ in translateForDisplayAndDup()
17718 if( mxWidth<0 ) mxWidth = -mxWidth; in translateForDisplayAndDup()
17739 for(k=i; k>i/2; k--){ in translateForDisplayAndDup()
17740 if( isspace(z[k-1]) ) break; in translateForDisplayAndDup()
17743 for(k=i; k>i/2; k--){ in translateForDisplayAndDup()
17744 if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break; in translateForDisplayAndDup()
17788 /* Extract the value of the i-th current column for pStmt as an SQL literal
17822 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
17831 ShellState *p, /* Pointer to ShellState */ in exec_prepared_stmt_columnar()
17850 int bw = p->cmOpts.bWordWrap; in exec_prepared_stmt_columnar()
17852 const char *zShowNull = p->nullValue; in exec_prepared_stmt_columnar()
17864 if( p->cmOpts.bQuote ){ in exec_prepared_stmt_columnar()
17871 if( nColumn>p->nWidth ){ in exec_prepared_stmt_columnar()
17872 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int)); in exec_prepared_stmt_columnar()
17873 shell_check_oom(p->colWidth); in exec_prepared_stmt_columnar()
17874 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0; in exec_prepared_stmt_columnar()
17875 p->nWidth = nColumn; in exec_prepared_stmt_columnar()
17876 p->actualWidth = &p->colWidth[nColumn]; in exec_prepared_stmt_columnar()
17878 memset(p->actualWidth, 0, nColumn*sizeof(int)); in exec_prepared_stmt_columnar()
17880 w = p->colWidth[i]; in exec_prepared_stmt_columnar()
17881 if( w<0 ) w = -w; in exec_prepared_stmt_columnar()
17882 p->actualWidth[i] = w; in exec_prepared_stmt_columnar()
17886 int wx = p->colWidth[i]; in exec_prepared_stmt_columnar()
17888 wx = p->cmOpts.iWrap; in exec_prepared_stmt_columnar()
17890 if( wx<0 ) wx = -wx; in exec_prepared_stmt_columnar()
17907 int wx = p->colWidth[i]; in exec_prepared_stmt_columnar()
17909 wx = p->cmOpts.iWrap; in exec_prepared_stmt_columnar()
17911 if( wx<0 ) wx = -wx; in exec_prepared_stmt_columnar()
17915 }else if( p->cmOpts.bQuote ){ in exec_prepared_stmt_columnar()
17927 abRowDiv[nRow-1] = 0; in exec_prepared_stmt_columnar()
17938 if( n>p->actualWidth[j] ) p->actualWidth[j] = n; in exec_prepared_stmt_columnar()
17942 switch( p->cMode ){ in exec_prepared_stmt_columnar()
17946 if( p->showHeader ){ in exec_prepared_stmt_columnar()
17948 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
17949 if( p->colWidth[i]<0 ) w = -w; in exec_prepared_stmt_columnar()
17950 utf8_width_print(p->out, w, azData[i]); in exec_prepared_stmt_columnar()
17951 fputs(i==nColumn-1?"\n":" ", p->out); in exec_prepared_stmt_columnar()
17954 print_dashes(p->out, p->actualWidth[i]); in exec_prepared_stmt_columnar()
17955 fputs(i==nColumn-1?"\n":" ", p->out); in exec_prepared_stmt_columnar()
17964 fputs("| ", p->out); in exec_prepared_stmt_columnar()
17966 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
17968 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); in exec_prepared_stmt_columnar()
17969 fputs(i==nColumn-1?" |\n":" | ", p->out); in exec_prepared_stmt_columnar()
17977 fputs("| ", p->out); in exec_prepared_stmt_columnar()
17979 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
17981 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, ""); in exec_prepared_stmt_columnar()
17982 fputs(i==nColumn-1?" |\n":" | ", p->out); in exec_prepared_stmt_columnar()
17991 utf8_printf(p->out, BOX_13 " "); in exec_prepared_stmt_columnar()
17993 w = p->actualWidth[i]; in exec_prepared_stmt_columnar()
17995 utf8_printf(p->out, "%*s%s%*s%s", in exec_prepared_stmt_columnar()
17996 (w-n)/2, "", azData[i], (w-n+1)/2, "", in exec_prepared_stmt_columnar()
17997 i==nColumn-1?" "BOX_13"\n":" "BOX_13" "); in exec_prepared_stmt_columnar()
18004 if( j==0 && p->cMode!=MODE_Column ){ in exec_prepared_stmt_columnar()
18005 utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| "); in exec_prepared_stmt_columnar()
18008 if( z==0 ) z = p->nullValue; in exec_prepared_stmt_columnar()
18009 w = p->actualWidth[j]; in exec_prepared_stmt_columnar()
18010 if( p->colWidth[j]<0 ) w = -w; in exec_prepared_stmt_columnar()
18011 utf8_width_print(p->out, w, z); in exec_prepared_stmt_columnar()
18012 if( j==nColumn-1 ){ in exec_prepared_stmt_columnar()
18013 utf8_printf(p->out, "%s", rowSep); in exec_prepared_stmt_columnar()
18014 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){ in exec_prepared_stmt_columnar()
18015 if( p->cMode==MODE_Table ){ in exec_prepared_stmt_columnar()
18017 }else if( p->cMode==MODE_Box ){ in exec_prepared_stmt_columnar()
18019 }else if( p->cMode==MODE_Column ){ in exec_prepared_stmt_columnar()
18020 raw_printf(p->out, "\n"); in exec_prepared_stmt_columnar()
18023 j = -1; in exec_prepared_stmt_columnar()
18026 utf8_printf(p->out, "%s", colSep); in exec_prepared_stmt_columnar()
18029 if( p->cMode==MODE_Table ){ in exec_prepared_stmt_columnar()
18031 }else if( p->cMode==MODE_Box ){ in exec_prepared_stmt_columnar()
18036 utf8_printf(p->out, "Interrupt\n"); in exec_prepared_stmt_columnar()
18056 ShellState *pArg, /* Pointer to ShellState */ in exec_prepared_stmt()
18062 if( pArg->cMode==MODE_Column in exec_prepared_stmt()
18063 || pArg->cMode==MODE_Table in exec_prepared_stmt()
18064 || pArg->cMode==MODE_Box in exec_prepared_stmt()
18065 || pArg->cMode==MODE_Markdown in exec_prepared_stmt()
18099 && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote) in exec_prepared_stmt()
18122 if( pArg->cMode==MODE_Json ){ in exec_prepared_stmt()
18123 fputs("]\n", pArg->out); in exec_prepared_stmt()
18124 }else if( pArg->cMode==MODE_Count ){ in exec_prepared_stmt()
18150 assert( pState->expert.pExpert ); in expertHandleSQL()
18152 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr); in expertHandleSQL()
18171 sqlite3expert *p = pState->expert.pExpert; in expertFinish()
18175 FILE *out = pState->out; in expertFinish()
18176 int bVerbose = pState->expert.bVerbose; in expertFinish()
18185 raw_printf(out, "-- Candidates -----------------------------\n"); in expertFinish()
18194 raw_printf(out, "-- Query %d --------------------------------\n",i+1); in expertFinish()
18203 pState->expert.pExpert = 0; in expertFinish()
18220 assert( pState->expert.pExpert==0 ); in expertDotCommand()
18221 memset(&pState->expert, 0, sizeof(ExpertInfo)); in expertDotCommand()
18226 if( z[0]=='-' && z[1]=='-' ) z++; in expertDotCommand()
18228 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){ in expertDotCommand()
18229 pState->expert.bVerbose = 1; in expertDotCommand()
18231 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){ in expertDotCommand()
18232 if( i==(nArg-1) ){ in expertDotCommand()
18250 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr); in expertDotCommand()
18251 if( pState->expert.pExpert==0 ){ in expertDotCommand()
18256 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample in expertDotCommand()
18271 ** This is very similar to SQLite's built-in sqlite3_exec()
18276 ShellState *pArg, /* Pointer to ShellState */ in shell_exec()
18284 sqlite3 *db = pArg->db; in shell_exec()
18291 if( pArg->expert.pExpert ){ in shell_exec()
18299 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); in shell_exec()
18306 /* this happens for a comment or white-space */ in shell_exec()
18317 pArg->pStmt = pStmt; in shell_exec()
18318 pArg->cnt = 0; in shell_exec()
18322 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){ in shell_exec()
18327 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); in shell_exec()
18328 if( pArg->autoEQP>=AUTOEQP_trigger ){ in shell_exec()
18333 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); in shell_exec()
18340 if( zEQPLine[0]=='-' ) eqp_render(pArg); in shell_exec()
18347 if( pArg->autoEQP>=AUTOEQP_full ){ in shell_exec()
18351 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); in shell_exec()
18353 pArg->cMode = MODE_Explain; in shell_exec()
18361 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){ in shell_exec()
18365 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); in shell_exec()
18366 if( pArg ) pArg->pStmt = pStmt; in shell_exec()
18372 pArg->cMode = pArg->mode; in shell_exec()
18373 if( pArg->autoExplain ){ in shell_exec()
18375 pArg->cMode = MODE_Explain; in shell_exec()
18378 pArg->cMode = MODE_EQP; in shell_exec()
18384 if( pArg->cMode==MODE_Explain ){ in shell_exec()
18395 if( pArg && pArg->statsOn ){ in shell_exec()
18399 /* print loop-counters if required */ in shell_exec()
18400 if( pArg && pArg->scanstatsOn ){ in shell_exec()
18418 pArg->pStmt = NULL; in shell_exec()
18464 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in tableColumnList()
18468 if( nCol>=nAlloc-2 ){ in tableColumnList()
18509 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in tableColumnList()
18533 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0); in tableColumnList()
18549 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0); in toggleSelectOrder()
18581 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0; in dump_callback()
18582 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0; in dump_callback()
18585 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n"); in dump_callback()
18587 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n"); in dump_callback()
18591 /* no-op */ in dump_callback()
18594 if( !p->writableSchema ){ in dump_callback()
18595 raw_printf(p->out, "PRAGMA writable_schema=ON;\n"); in dump_callback()
18596 p->writableSchema = 1; in dump_callback()
18603 utf8_printf(p->out, "%s\n", zIns); in dump_callback()
18607 printSchemaLine(p->out, zSql, ";\n"); in dump_callback()
18620 p->nErr++; in dump_callback()
18659 savedDestTable = p->zDestTable; in dump_callback()
18660 savedMode = p->mode; in dump_callback()
18661 p->zDestTable = sTable.z; in dump_callback()
18662 p->mode = p->cMode = MODE_Insert; in dump_callback()
18665 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); in dump_callback()
18666 toggleSelectOrder(p->db); in dump_callback()
18668 toggleSelectOrder(p->db); in dump_callback()
18670 p->zDestTable = savedDestTable; in dump_callback()
18671 p->mode = savedMode; in dump_callback()
18674 if( rc ) p->nErr++; in dump_callback()
18692 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr); in run_schema_dump_query()
18696 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n"); in run_schema_dump_query()
18698 utf8_printf(p->out, "/****** %s ******/\n", zErr); in run_schema_dump_query()
18705 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr); in run_schema_dump_query()
18707 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr); in run_schema_dump_query()
18731 " -c, --create Create a new archive",
18732 " -u, --update Add or update files with changed mtime",
18733 " -i, --insert Like -u but always add even if unchanged",
18734 " -r, --remove Remove files from archive",
18735 " -t, --list List contents of archive",
18736 " -x, --extract Extract files from archive",
18738 " -v, --verbose Print each filename as it is processed",
18739 " -f FILE, --file FILE Use archive FILE (default is current db)",
18740 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
18741 " -C DIR, --directory DIR Read/extract files from directory DIR",
18742 " -g, --glob Use glob matching for names in archive",
18743 " -n, --dryrun Show the SQL that would have occurred",
18745 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
18746 " .ar -tf ARCHIVE # List members of ARCHIVE",
18747 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
18757 " --append Use the appendvfs",
18758 " --async Write to FILE without journal and fsync()",
18778 " --data-only Output only INSERT statements",
18779 " --newlines Allow unescaped newline characters in output",
18780 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
18781 " --preserve-rowids Include ROWID values in the output",
18794 " --bom Put a UTF8 byte-order mark on intermediate file",
18797 ".exit ?CODE? Exit this program with return-code CODE",
18802 " --schema SCHEMA Use SCHEMA instead of \"main\"",
18803 " --help Show CMD details",
18804 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
18806 ".help ?-all? ?PATTERN? Show help text for PATTERN",
18810 " --ascii Use \\037 and \\036 as column and row separators",
18811 " --csv Use , and \\n as column and row separators",
18812 " --skip N Skip the first N rows of input",
18813 " --schema S Target table to be S.TABLE",
18814 " -v \"Verbose\" - increase auxiliary output",
18818 " * If neither --csv or --ascii are used, the input mode is derived",
18835 " fkey-indexes Find missing foreign key indexes",
18845 " box Tables using unicode box-drawing characters",
18846 " csv Comma-separated values",
18854 " qbox Shorthand for \"box --wrap 60 --quote\"",
18856 " table ASCII-art table",
18857 " tabs Tab-separated values",
18860 " --wrap N Wrap output lines to no longer than N characters",
18861 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
18862 " --ww Shorthand for \"--wordwrap 1\"",
18863 " --quote Quote output text as SQL literals",
18864 " --noquote Do not quote output text",
18873 " --bom Put a UTF8 byte-order mark at the beginning",
18874 " -e Send output to the system text editor",
18875 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
18881 " --append Use appendvfs to append database to the end of FILE",
18884 " --deserialize Load into memory using sqlite3_deserialize()",
18885 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
18886 " --maxsize N Maximum size for --hexdb or --deserialized database",
18888 " --new Initialize FILE to an empty database",
18889 " --nofollow Do not follow symbolic links",
18890 " --readonly Open FILE readonly",
18891 " --zip FILE is a ZIP archive",
18896 " --bom Prefix output with a UTF8 byte-order mark",
18897 " -e Send output to the system text editor",
18898 " -x Send output as CSV to a spreadsheet",
18910 " --limit N Interrupt after N progress callbacks",
18911 " --once Do no more than one progress interrupt",
18912 " --quiet|-q No output except at interrupts",
18913 " --reset Reset the count for each input and interrupt",
18923 " --ignore-freelist Ignore pages that appear to be on db freelist",
18924 " --lost-and-found TABLE Alternative name for the lost-and-found table",
18925 " --no-rowids Do not attempt to recover rowid values",
18935 " --indent Try to pretty-print the schema",
18936 " --nosys Omit objects whose names start with \"sqlite_\"",
18939 " --init Create a new SELFTEST table",
18940 " -v Verbose output",
18959 " --schema Also hash the sqlite_schema table",
18960 " --sha3-224 Use the sha3-224 algorithm",
18961 " --sha3-256 Use the sha3-256 algorithm (default)",
18962 " --sha3-384 Use the sha3-384 algorithm",
18963 " --sha3-512 Use the sha3-512 algorithm",
18979 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
18991 " --expanded Expand query parameters",
18993 " --normalized Normal the SQL statements",
18995 " --plain Show SQL as it is input",
18996 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
18997 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
18998 " --row Trace each row (SQLITE_TRACE_ROW)",
18999 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
19003 " --allexcept Unregister everything except those named",
19005 ".vfsinfo ?AUX? Information about the top-level VFS",
19009 " Negative values right-justify",
19016 ** If zPattern is NULL, then show all commands, but only give a one-line
19028 || cli_strcmp(zPattern,"-a")==0 in showHelp()
19029 || cli_strcmp(zPattern,"-all")==0 in showHelp()
19030 || cli_strcmp(zPattern,"--all")==0 in showHelp()
19056 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){ in showHelp()
19071 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){ in showHelp()
19089 ** and return a pointer to the buffer. The caller is responsible for freeing
19095 ** For convenience, a nul-terminator byte is always appended to the data read
19131 sqlite3session_delete(pSession->p); in session_close()
19132 sqlite3_free(pSession->zName); in session_close()
19133 for(i=0; i<pSession->nFilter; i++){ in session_close()
19134 sqlite3_free(pSession->azFilter[i]); in session_close()
19136 sqlite3_free(pSession->azFilter); in session_close()
19147 struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i]; in session_close_all()
19148 for(j=0; j<pAuxDb->nSession; j++){ in session_close_all()
19149 session_close(&pAuxDb->aSession[j]); in session_close_all()
19151 pAuxDb->nSession = 0; in session_close_all()
19165 for(i=0; i<pSession->nFilter; i++){ in session_filter()
19166 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0; in session_filter()
19198 fseek(f, -25, SEEK_END); in deduceDatabaseType()
19200 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){ in deduceDatabaseType()
19203 fseek(f, -22, SEEK_END); in deduceDatabaseType()
19218 ** Reconstruct an in-memory database using the output from the "dbtotxt"
19219 ** program. Read content from the file in p->aAuxDb[].zDbFilename.
19220 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
19231 const char *zDbFilename = p->pAuxDb->zDbFilename; in readHexDb()
19242 in = p->in; in readHexDb()
19243 nLine = p->lineno; in readHexDb()
19252 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error; in readHexDb()
19253 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */ in readHexDb()
19257 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){ in readHexDb()
19282 if( in!=p->in ){ in readHexDb()
19285 p->lineno = nLine; in readHexDb()
19290 if( in!=p->in ){ in readHexDb()
19293 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){ in readHexDb()
19297 p->lineno = nLine; in readHexDb()
19300 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine); in readHexDb()
19307 ** must be a blob. The second a non-negative integer. This function
19308 ** reads and returns a 32-bit big-endian integer from byte
19337 ** using "..." with internal double-quote characters doubled.
19348 sqlite3_result_text(context, z, -1, sqlite3_free); in shellIdQuote()
19368 ** The argument passed to this function is the output of built-in
19475 if( p->db==0 ){ in open_db()
19476 const char *zDbFilename = p->pAuxDb->zDbFilename; in open_db()
19477 if( p->openMode==SHELL_OPEN_UNSPEC ){ in open_db()
19479 p->openMode = SHELL_OPEN_NORMAL; in open_db()
19481 p->openMode = (u8)deduceDatabaseType(zDbFilename, in open_db()
19485 switch( p->openMode ){ in open_db()
19487 sqlite3_open_v2(zDbFilename, &p->db, in open_db()
19488 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs"); in open_db()
19493 sqlite3_open(0, &p->db); in open_db()
19497 sqlite3_open(":memory:", &p->db); in open_db()
19501 sqlite3_open_v2(zDbFilename, &p->db, in open_db()
19502 SQLITE_OPEN_READONLY|p->openFlags, 0); in open_db()
19507 sqlite3_open_v2(zDbFilename, &p->db, in open_db()
19508 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0); in open_db()
19512 globalDb = p->db; in open_db()
19513 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){ in open_db()
19515 zDbFilename, sqlite3_errmsg(p->db)); in open_db()
19517 sqlite3_open(":memory:", &p->db); in open_db()
19523 sqlite3_enable_load_extension(p->db, 1); in open_db()
19525 sqlite3_shathree_init(p->db, 0, 0); in open_db()
19526 sqlite3_uint_init(p->db, 0, 0); in open_db()
19527 sqlite3_decimal_init(p->db, 0, 0); in open_db()
19528 sqlite3_regexp_init(p->db, 0, 0); in open_db()
19529 sqlite3_ieee_init(p->db, 0, 0); in open_db()
19530 sqlite3_series_init(p->db, 0, 0); in open_db()
19532 sqlite3_fileio_init(p->db, 0, 0); in open_db()
19533 sqlite3_completion_init(p->db, 0, 0); in open_db()
19536 sqlite3_dbdata_init(p->db, 0, 0); in open_db()
19539 if( !p->bSafeModePersist ){ in open_db()
19540 sqlite3_zipfile_init(p->db, 0, 0); in open_db()
19541 sqlite3_sqlar_init(p->db, 0, 0); in open_db()
19544 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0, in open_db()
19546 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0, in open_db()
19548 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p, in open_db()
19550 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0, in open_db()
19552 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0, in open_db()
19554 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0, in open_db()
19556 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0, in open_db()
19559 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0, in open_db()
19561 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0, in open_db()
19564 if( p->openMode==SHELL_OPEN_ZIPFILE ){ in open_db()
19568 sqlite3_exec(p->db, zSql, 0, 0, 0); in open_db()
19573 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){ in open_db()
19577 if( p->openMode==SHELL_OPEN_DESERIALIZE ){ in open_db()
19585 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData, in open_db()
19591 if( p->szMax>0 ){ in open_db()
19592 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax); in open_db()
19597 if( p->bSafeModePersist && p->db!=0 ){ in open_db()
19598 sqlite3_set_authorizer(p->db, safeModeAuth, p); in open_db()
19626 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); in readline_completion_generator()
19655 if( nLine>sizeof(zBuf)-30 ) return; in linenoise_completion()
19657 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){} in linenoise_completion()
19658 if( i==nLine-1 ) return; in linenoise_completion()
19665 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0); in linenoise_completion()
19671 if( iStart+nCompletion < sizeof(zBuf)-1 && zCompletion ){ in linenoise_completion()
19681 ** Do C-language style dequoting.
19683 ** \a -> alarm
19684 ** \b -> backspace
19685 ** \t -> tab
19686 ** \n -> newline
19687 ** \v -> vertical tab
19688 ** \f -> form feed
19689 ** \r -> carriage return
19690 ** \s -> space
19691 ** \" -> "
19692 ** \' -> '
19693 ** \\ -> backslash
19694 ** \NNN -> ascii character NNN in octal
19724 c -= '0'; in resolve_backslashes()
19727 c = (c<<3) + z[i] - '0'; in resolve_backslashes()
19730 c = (c<<3) + z[i] - '0'; in resolve_backslashes()
19809 void *pArg, /* The ShellState pointer */ in sql_trace_callback()
19810 void *pP, /* Usually a pointer to sqlite_stmt */ in sql_trace_callback()
19817 if( p->traceOut==0 ) return 0; in sql_trace_callback()
19819 utf8_printf(p->traceOut, "-- closing database connection\n"); in sql_trace_callback()
19822 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){ in sql_trace_callback()
19826 switch( p->eTraceType ){ in sql_trace_callback()
19846 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; } in sql_trace_callback()
19850 utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql); in sql_trace_callback()
19855 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec); in sql_trace_callback()
19864 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
19894 if( p->in!=0 && p->xCloser!=0 ){ in import_cleanup()
19895 p->xCloser(p->in); in import_cleanup()
19896 p->in = 0; in import_cleanup()
19898 sqlite3_free(p->z); in import_cleanup()
19899 p->z = 0; in import_cleanup()
19904 if( p->n+1>=p->nAlloc ){ in import_append_char()
19905 p->nAlloc += p->nAlloc + 100; in import_append_char()
19906 p->z = sqlite3_realloc64(p->z, p->nAlloc); in import_append_char()
19907 shell_check_oom(p->z); in import_append_char()
19909 p->z[p->n++] = (char)c; in import_append_char()
19915 ** + Input comes from p->in.
19916 ** + Store results in p->z of length p->n. Space to hold p->z comes
19918 ** + Use p->cSep as the column separator. The default is ",".
19919 ** + Use p->rSep as the row separator. The default is "\n".
19920 ** + Keep track of the line number in p->nLine.
19921 ** + Store the character that terminates the field in p->cTerm. Store
19922 ** EOF on end-of-file.
19927 int cSep = p->cColSep; in csv_read_one_field()
19928 int rSep = p->cRowSep; in csv_read_one_field()
19929 p->n = 0; in csv_read_one_field()
19930 c = fgetc(p->in); in csv_read_one_field()
19932 p->cTerm = EOF; in csv_read_one_field()
19937 int startLine = p->nLine; in csv_read_one_field()
19941 c = fgetc(p->in); in csv_read_one_field()
19942 if( c==rSep ) p->nLine++; in csv_read_one_field()
19954 do{ p->n--; }while( p->z[p->n]!=cQuote ); in csv_read_one_field()
19955 p->cTerm = c; in csv_read_one_field()
19960 p->zFile, p->nLine, cQuote); in csv_read_one_field()
19963 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n", in csv_read_one_field()
19964 p->zFile, startLine, cQuote); in csv_read_one_field()
19965 p->cTerm = c; in csv_read_one_field()
19974 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */ in csv_read_one_field()
19975 if( (c&0xff)==0xef && p->bNotFirst==0 ){ in csv_read_one_field()
19977 c = fgetc(p->in); in csv_read_one_field()
19980 c = fgetc(p->in); in csv_read_one_field()
19982 p->bNotFirst = 1; in csv_read_one_field()
19983 p->n = 0; in csv_read_one_field()
19990 c = fgetc(p->in); in csv_read_one_field()
19993 p->nLine++; in csv_read_one_field()
19994 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--; in csv_read_one_field()
19996 p->cTerm = c; in csv_read_one_field()
19998 if( p->z ) p->z[p->n] = 0; in csv_read_one_field()
19999 p->bNotFirst = 1; in csv_read_one_field()
20000 return p->z; in csv_read_one_field()
20005 ** + Input comes from p->in.
20006 ** + Store results in p->z of length p->n. Space to hold p->z comes
20008 ** + Use p->cSep as the column separator. The default is "\x1F".
20009 ** + Use p->rSep as the row separator. The default is "\x1E".
20010 ** + Keep track of the row number in p->nLine.
20011 ** + Store the character that terminates the field in p->cTerm. Store
20012 ** EOF on end-of-file.
20017 int cSep = p->cColSep; in ascii_read_one_field()
20018 int rSep = p->cRowSep; in ascii_read_one_field()
20019 p->n = 0; in ascii_read_one_field()
20020 c = fgetc(p->in); in ascii_read_one_field()
20022 p->cTerm = EOF; in ascii_read_one_field()
20027 c = fgetc(p->in); in ascii_read_one_field()
20030 p->nLine++; in ascii_read_one_field()
20032 p->cTerm = c; in ascii_read_one_field()
20033 if( p->z ) p->z[p->n] = 0; in ascii_read_one_field()
20034 return p->z; in ascii_read_one_field()
20060 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneData()
20063 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), in tryToCloneData()
20078 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0); in tryToCloneData()
20104 -1, SQLITE_STATIC); in tryToCloneData()
20123 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]); in tryToCloneData()
20133 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneData()
20170 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneSchema()
20173 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), in tryToCloneSchema()
20199 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0); in tryToCloneSchema()
20202 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), in tryToCloneSchema()
20245 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0); in tryToClone()
20250 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); in tryToClone()
20258 ** If the p->doXdgOpen flag is set, that means the output was being
20259 ** redirected to a temporary file named by p->zTempFile. In that case,
20260 ** launch start/open/xdg-open on that temporary file.
20263 if( p->outfile[0]=='|' ){ in output_reset()
20265 pclose(p->out); in output_reset()
20268 output_file_close(p->out); in output_reset()
20270 if( p->doXdgOpen ){ in output_reset()
20277 "xdg-open"; in output_reset()
20280 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); in output_reset()
20284 /* Give the start/open/xdg-open command some time to get in output_reset()
20286 ** p->zTempFile data file out from under it */ in output_reset()
20291 p->doXdgOpen = 0; in output_reset()
20295 p->outfile[0] = 0; in output_reset()
20296 p->out = stdout; in output_reset()
20305 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); in db_int()
20315 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
20363 if( p->db==0 ) return 1; in shell_dbinfo_command()
20364 rc = sqlite3_prepare_v2(p->db, in shell_dbinfo_command()
20366 -1, &pStmt, 0); in shell_dbinfo_command()
20368 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db)); in shell_dbinfo_command()
20372 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); in shell_dbinfo_command()
20385 utf8_printf(p->out, "%-20s %d\n", "database page size:", i); in shell_dbinfo_command()
20386 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]); in shell_dbinfo_command()
20387 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]); in shell_dbinfo_command()
20388 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); in shell_dbinfo_command()
20392 utf8_printf(p->out, "%-20s %u", aField[i].zName, val); in shell_dbinfo_command()
20395 if( val==1 ) raw_printf(p->out, " (utf8)"); in shell_dbinfo_command()
20396 if( val==2 ) raw_printf(p->out, " (utf16le)"); in shell_dbinfo_command()
20397 if( val==3 ) raw_printf(p->out, " (utf16be)"); in shell_dbinfo_command()
20400 raw_printf(p->out, "\n"); in shell_dbinfo_command()
20411 int val = db_int(p->db, zSql); in shell_dbinfo_command()
20413 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val); in shell_dbinfo_command()
20416 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); in shell_dbinfo_command()
20417 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion); in shell_dbinfo_command()
20447 ** optional + or - sign in front
20471 while( *z && testcase_glob(zGlob-1,z)==0 ){ in testcase_glob()
20502 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ in testcase_glob()
20516 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; in testcase_glob()
20530 ** Compare the string as a command-line option with either one or two
20531 ** initial "-" characters.
20534 if( zStr[0]!='-' ) return 0; in optionMatch()
20536 if( zStr[0]=='-' ) zStr++; in optionMatch()
20560 if( p->zTempFile==0 ) return; in clearTempFile()
20561 if( p->doXdgOpen ) return; in clearTempFile()
20562 if( shellDeleteFile(p->zTempFile) ) return; in clearTempFile()
20563 sqlite3_free(p->zTempFile); in clearTempFile()
20564 p->zTempFile = 0; in clearTempFile()
20572 sqlite3_free(p->zTempFile); in newTempFile()
20573 p->zTempFile = 0; in newTempFile()
20574 if( p->db ){ in newTempFile()
20575 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); in newTempFile()
20577 if( p->zTempFile==0 ){ in newTempFile()
20578 /* If p->db is an in-memory database then the TEMPFILENAME file-control in newTempFile()
20592 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix); in newTempFile()
20594 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); in newTempFile()
20596 shell_check_oom(p->zTempFile); in newTempFile()
20602 ** by the ".lint fkey-indexes" command. This scalar function is always
20603 ** called with four arguments - the parent table name, the parent column name,
20606 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
20612 ** function returns the string " COLLATE <parent-collation>", where
20613 ** <parent-collation> is the default collation sequence of the parent column.
20626 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ in shellFkeyCollateClause()
20635 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); in shellFkeyCollateClause()
20647 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); in shellFkeyCollateClause()
20654 ** The implementation of dot-command ".lint fkey-indexes".
20661 sqlite3 *db = pState->db; /* Database handle to query "main" db of */ in lintFkeyIndexes()
20662 FILE *out = pState->out; /* Stream to write non-error output to */ in lintFkeyIndexes()
20663 int bVerbose = 0; /* If -verbose is present */ in lintFkeyIndexes()
20664 int bGroupByParent = 0; /* If -groupbyparent is present */ in lintFkeyIndexes()
20728 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " in lintFkeyIndexes()
20736 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ in lintFkeyIndexes()
20739 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ in lintFkeyIndexes()
20744 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n", in lintFkeyIndexes()
20758 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); in lintFkeyIndexes()
20768 int res = -1; in lintFkeyIndexes()
20779 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); in lintFkeyIndexes()
20797 raw_printf(out, "-- Parent table %s\n", zParent); in lintFkeyIndexes()
20803 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); in lintFkeyIndexes()
20805 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n", in lintFkeyIndexes()
20839 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; in lintDotCommand()
20843 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]); in lintDotCommand()
20844 raw_printf(stderr, "Where sub-commands are:\n"); in lintDotCommand()
20845 raw_printf(stderr, " fkey-indexes\n"); in lintDotCommand()
20858 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); in shellPrepare()
20869 ** Create a prepared statement using printf-style arguments for the SQL.
20872 ** depending on compile-time options. By omitting the "static", we avoid
20901 ** depending on compile-time options. By omitting the "static", we avoid
20923 ** depending on compile-time options. By omitting the "static", we avoid
20951 u8 bVerbose; /* True if --verbose */
20953 u8 bDryRun; /* True if --dry-run */
20954 u8 bAppend; /* True if --append */
20955 u8 bGlob; /* True if --glob */
20956 u8 fromCmdLine; /* Run from -A instead of .archive */
20959 const char *zFile; /* --file argument, or NULL */
20960 const char *zDir; /* --directory argument, or NULL */
20985 if( pAr->fromCmdLine ){ in arErrorMsg()
20986 utf8_printf(stderr, "Use \"-A\" for more help\n"); in arErrorMsg()
20988 utf8_printf(stderr, "Use \".archive --help\" for more help\n"); in arErrorMsg()
21006 ** Other (non-command) switches.
21024 if( pAr->eCmd ){ in arProcessSwitch()
21027 pAr->eCmd = eSwitch; in arProcessSwitch()
21031 pAr->bDryRun = 1; in arProcessSwitch()
21034 pAr->bGlob = 1; in arProcessSwitch()
21037 pAr->bVerbose = 1; in arProcessSwitch()
21040 pAr->bAppend = 1; in arProcessSwitch()
21041 /* Fall thru into --file */ in arProcessSwitch()
21043 pAr->zFile = zArg; in arProcessSwitch()
21046 pAr->zDir = zArg; in arProcessSwitch()
21092 if( z[0]!='-' ){ in arParseCommand()
21100 if( z[i]==pOpt->cShort ) break; in arParseCommand()
21105 if( pOpt->bArg ){ in arParseCommand()
21111 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; in arParseCommand()
21113 pAr->nArg = nArg-iArg; in arParseCommand()
21114 if( pAr->nArg>0 ){ in arParseCommand()
21115 pAr->azArg = &azArg[iArg]; in arParseCommand()
21118 /* Non-traditional invocation */ in arParseCommand()
21123 if( z[0]!='-' ){ in arParseCommand()
21125 pAr->azArg = &azArg[iArg]; in arParseCommand()
21126 pAr->nArg = nArg-iArg; in arParseCommand()
21131 if( z[1]!='-' ){ in arParseCommand()
21138 if( z[i]==pOpt->cShort ) break; in arParseCommand()
21143 if( pOpt->bArg ){ in arParseCommand()
21144 if( i<(n-1) ){ in arParseCommand()
21148 if( iArg>=(nArg-1) ){ in arParseCommand()
21155 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; in arParseCommand()
21158 /* A -- option, indicating that all remaining command line words in arParseCommand()
21160 pAr->azArg = &azArg[iArg+1]; in arParseCommand()
21161 pAr->nArg = nArg-iArg-1; in arParseCommand()
21169 const char *zLong = pOpt->zLong; in arParseCommand()
21170 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ in arParseCommand()
21182 if( pMatch->bArg ){ in arParseCommand()
21183 if( iArg>=(nArg-1) ){ in arParseCommand()
21188 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; in arParseCommand()
21199 ** array refer to archive members, as for the --extract, --list or --remove
21204 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
21205 ** when pAr->bGlob is true.
21213 if( pAr->nArg ){ in arCheckEntries()
21216 const char *zSel = (pAr->bGlob) in arCheckEntries()
21220 shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable); in arCheckEntries()
21222 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ in arCheckEntries()
21223 char *z = pAr->azArg[i]; in arCheckEntries()
21226 while( n>0 && z[n-1]=='/' ) n--; in arCheckEntries()
21228 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); in arCheckEntries()
21248 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
21249 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
21257 const char *zSameOp = (pAr->bGlob)? "GLOB" : "="; in arWhereClause()
21259 if( pAr->nArg==0 ){ in arWhereClause()
21264 for(i=0; i<pAr->nArg; i++){ in arWhereClause()
21265 const char *z = pAr->azArg[i]; in arWhereClause()
21298 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], in arListCommand()
21299 pAr->zSrcTable, zWhere); in arListCommand()
21300 if( pAr->bDryRun ){ in arListCommand()
21301 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); in arListCommand()
21304 if( pAr->bVerbose ){ in arListCommand()
21305 utf8_printf(pAr->p->out, "%s % 10d %s %s\n", in arListCommand()
21312 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); in arListCommand()
21330 if( pAr->nArg ){ in arRemoveCommand()
21338 pAr->zSrcTable, zWhere); in arRemoveCommand()
21339 if( pAr->bDryRun ){ in arRemoveCommand()
21340 utf8_printf(pAr->p->out, "%s\n", zSql); in arRemoveCommand()
21343 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0); in arRemoveCommand()
21345 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); in arRemoveCommand()
21347 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); in arRemoveCommand()
21349 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0); in arRemoveCommand()
21392 if( pAr->zDir ){ in arExtractCommand()
21393 zDir = sqlite3_mprintf("%s/", pAr->zDir); in arExtractCommand()
21400 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, in arExtractCommand()
21401 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere in arExtractCommand()
21406 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); in arExtractCommand()
21416 if( pAr->bDryRun ){ in arExtractCommand()
21417 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql)); in arExtractCommand()
21420 if( i==0 && pAr->bVerbose ){ in arExtractCommand()
21421 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0)); in arExtractCommand()
21436 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
21440 if( pAr->bDryRun ){ in arExecSql()
21441 utf8_printf(pAr->p->out, "%s\n", zSql); in arExecSql()
21445 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); in arExecSql()
21458 ** create -> Create a new SQL archive
21459 ** insert -> Insert or reinsert all files listed
21460 ** update -> Insert files that have changed or that were not
21465 ** are added recursively. If argument bVerbose is non-zero, a message is
21470 ** always overwrites every file named on the command-line, where as
21475 int bUpdate, /* true for a --create. */ in arCreateOrUpdateCommand()
21480 " name TEXT PRIMARY KEY, -- name of the file\n" in arCreateOrUpdateCommand()
21481 " mode INT, -- access permissions\n" in arCreateOrUpdateCommand()
21482 " mtime INT, -- last modification time\n" in arCreateOrUpdateCommand()
21483 " sz INT, -- original file size\n" in arCreateOrUpdateCommand()
21484 " data BLOB -- compressed content\n" in arCreateOrUpdateCommand()
21494 " WHEN '-' THEN length(data)\n" in arCreateOrUpdateCommand()
21496 " ELSE -1 END,\n" in arCreateOrUpdateCommand()
21521 if( pAr->bZip ){ in arCreateOrUpdateCommand()
21523 if( pAr->zFile ){ in arCreateOrUpdateCommand()
21530 zTab, pAr->zFile in arCreateOrUpdateCommand()
21557 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){ in arCreateOrUpdateCommand()
21558 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, in arCreateOrUpdateCommand()
21559 pAr->bVerbose ? "shell_putsnl(name)" : "name", in arCreateOrUpdateCommand()
21560 pAr->azArg[i], pAr->zDir, zExists); in arCreateOrUpdateCommand()
21566 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); in arCreateOrUpdateCommand()
21569 if( pAr->bZip && pAr->zFile ){ in arCreateOrUpdateCommand()
21584 int fromCmdLine, /* True if -A command-line option, not .ar cmd */ in arDotCommand()
21596 cmd.db = pState->db; in arDotCommand()
21600 eDbType = pState->openMode; in arDotCommand()
21622 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile, in arDotCommand()
21664 arUsage(pState->out); in arDotCommand()
21682 if( cmd.db!=pState->db ){ in arDotCommand()
21701 utf8_printf(pState->out, "%s;\n", zSql); in recoverSqlCb()
21708 ** on stream pState->out.
21714 int bFreelist = 1; /* 0 if --ignore-freelist is specified */ in recoverDatabaseCmd()
21715 int bRowids = 1; /* 0 if --no-rowids */ in recoverDatabaseCmd()
21722 if( z[0]=='-' && z[1]=='-' ) z++; in recoverDatabaseCmd()
21724 if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){ in recoverDatabaseCmd()
21727 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){ in recoverDatabaseCmd()
21728 /* This option determines the name of the ATTACH-ed database used in recoverDatabaseCmd()
21736 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){ in recoverDatabaseCmd()
21740 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){ in recoverDatabaseCmd()
21745 showHelp(pState->out, azArg[0]); in recoverDatabaseCmd()
21751 pState->db, "main", recoverSqlCb, (void*)pState in recoverDatabaseCmd()
21780 * pointer is non-zero, its referent will be set to a summary of renames
21830 SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\ in zAutoColumn()
21838 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\ in zAutoColumn()
21846 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\ in zAutoColumn()
21904 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\ in zAutoColumn()
21907 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \ in zAutoColumn()
21912 " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff))," in zAutoColumn()
21932 rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0); in zAutoColumn()
21934 rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0); in zAutoColumn()
21954 rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0); in zAutoColumn()
21962 rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0); in zAutoColumn()
21974 if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0) in zAutoColumn()
22002 if( p->expert.pExpert ){ in do_meta_command()
22009 while( zLine[h] && nArg<ArraySize(azArg)-1 ){ in do_meta_command()
22022 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); in do_meta_command()
22027 resolve_backslashes(azArg[nArg-1]); in do_meta_command()
22048 sqlite3_set_authorizer(p->db, shellAuth, p); in do_meta_command()
22049 }else if( p->bSafeModePersist ){ in do_meta_command()
22050 sqlite3_set_authorizer(p->db, safeModeAuth, p); in do_meta_command()
22052 sqlite3_set_authorizer(p->db, 0, 0); in do_meta_command()
22080 if( z[0]=='-' ){ in do_meta_command()
22081 if( z[1]=='-' ) z++; in do_meta_command()
22082 if( cli_strcmp(z, "-append")==0 ){ in do_meta_command()
22085 if( cli_strcmp(z, "-async")==0 ){ in do_meta_command()
22119 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); in do_meta_command()
22149 setBinaryMode(p->out, 1); in do_meta_command()
22151 setTextMode(p->out, 1); in do_meta_command()
22159 /* The undocumented ".breakpoint" command causes a call to the no-op in do_meta_command()
22199 ** Then read the content of the testcase-out.txt file and compare against in do_meta_command()
22206 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); in do_meta_command()
22208 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ in do_meta_command()
22209 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); in do_meta_command()
22213 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", in do_meta_command()
22214 p->zTestcase, azArg[1], zRes); in do_meta_command()
22217 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase); in do_meta_command()
22218 p->nCheck++; in do_meta_command()
22240 for(i=0; i<ArraySize(p->aAuxDb); i++){ in do_meta_command()
22241 const char *zFile = p->aAuxDb[i].zDbFilename; in do_meta_command()
22242 if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){ in do_meta_command()
22247 zFile = "(temporary-file)"; in do_meta_command()
22249 if( p->pAuxDb == &p->aAuxDb[i] ){ in do_meta_command()
22251 }else if( p->aAuxDb[i].db!=0 ){ in do_meta_command()
22256 int i = azArg[1][0] - '0'; in do_meta_command()
22257 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){ in do_meta_command()
22258 p->pAuxDb->db = p->db; in do_meta_command()
22259 p->pAuxDb = &p->aAuxDb[i]; in do_meta_command()
22260 globalDb = p->db = p->pAuxDb->db; in do_meta_command()
22261 p->pAuxDb->db = 0; in do_meta_command()
22265 int i = azArg[2][0] - '0'; in do_meta_command()
22266 if( i<0 || i>=ArraySize(p->aAuxDb) ){ in do_meta_command()
22267 /* No-op */ in do_meta_command()
22268 }else if( p->pAuxDb == &p->aAuxDb[i] ){ in do_meta_command()
22271 }else if( p->aAuxDb[i].db ){ in do_meta_command()
22273 close_db(p->aAuxDb[i].db); in do_meta_command()
22274 p->aAuxDb[i].db = 0; in do_meta_command()
22277 raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n"); in do_meta_command()
22288 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); in do_meta_command()
22290 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
22306 int eTxn = sqlite3_txn_state(p->db, azName[i*2]); in do_meta_command()
22307 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]); in do_meta_command()
22309 utf8_printf(p->out, "%s: %s %s%s\n", in do_meta_command()
22314 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn"); in do_meta_command()
22348 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); in do_meta_command()
22350 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); in do_meta_command()
22351 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off"); in do_meta_command()
22375 int savedShowHeader = p->showHeader; in do_meta_command()
22376 int savedShellFlags = p->shellFlgs; in do_meta_command()
22381 if( azArg[i][0]=='-' ){ in do_meta_command()
22383 if( z[0]=='-' ) z++; in do_meta_command()
22384 if( cli_strcmp(z,"preserve-rowids")==0 ){ in do_meta_command()
22386 raw_printf(stderr, "The --preserve-rowids option is not compatible" in do_meta_command()
22398 if( cli_strcmp(z,"data-only")==0 ){ in do_meta_command()
22435 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ in do_meta_command()
22438 ** So disable foreign-key constraint enforcement to prevent problems. */ in do_meta_command()
22439 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n"); in do_meta_command()
22440 raw_printf(p->out, "BEGIN TRANSACTION;\n"); in do_meta_command()
22442 p->writableSchema = 0; in do_meta_command()
22443 p->showHeader = 0; in do_meta_command()
22447 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); in do_meta_command()
22448 p->nErr = 0; in do_meta_command()
22459 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ in do_meta_command()
22470 if( p->writableSchema ){ in do_meta_command()
22471 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n"); in do_meta_command()
22472 p->writableSchema = 0; in do_meta_command()
22474 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); in do_meta_command()
22475 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); in do_meta_command()
22476 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ in do_meta_command()
22477 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n"); in do_meta_command()
22479 p->showHeader = savedShowHeader; in do_meta_command()
22480 p->shellFlgs = savedShellFlags; in do_meta_command()
22494 p->autoEQPtest = 0; in do_meta_command()
22495 if( p->autoEQPtrace ){ in do_meta_command()
22496 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); in do_meta_command()
22497 p->autoEQPtrace = 0; in do_meta_command()
22500 p->autoEQP = AUTOEQP_full; in do_meta_command()
22502 p->autoEQP = AUTOEQP_trigger; in do_meta_command()
22505 p->autoEQP = AUTOEQP_on; in do_meta_command()
22506 p->autoEQPtest = 1; in do_meta_command()
22508 p->autoEQP = AUTOEQP_full; in do_meta_command()
22509 p->autoEQPtrace = 1; in do_meta_command()
22511 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0); in do_meta_command()
22512 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); in do_meta_command()
22515 p->autoEQP = (u8)booleanValue(azArg[1]); in do_meta_command()
22541 if( val==1 && p->mode!=MODE_Explain ){ in do_meta_command()
22542 p->normalMode = p->mode; in do_meta_command()
22543 p->mode = MODE_Explain; in do_meta_command()
22544 p->autoExplain = 0; in do_meta_command()
22546 if( p->mode==MODE_Explain ) p->mode = p->normalMode; in do_meta_command()
22547 p->autoExplain = 0; in do_meta_command()
22549 if( p->mode==MODE_Explain ) p->mode = p->normalMode; in do_meta_command()
22550 p->autoExplain = 1; in do_meta_command()
22556 if( p->bSafeMode ){ in do_meta_command()
22570 const char *zCtrlName; /* Name of a test-control option */ in do_meta_command()
22586 int filectrl = -1; in do_meta_command()
22587 int iCtrl = -1; in do_meta_command()
22589 int isOk = 0; /* 0: usage 1: %lld 2: no-result */ in do_meta_command()
22597 if( zCmd[0]=='-' in do_meta_command()
22598 && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0) in do_meta_command()
22602 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i]; in do_meta_command()
22603 nArg -= 2; in do_meta_command()
22607 /* The argument can optionally begin with "-" or "--" */ in do_meta_command()
22608 if( zCmd[0]=='-' && zCmd[1] ){ in do_meta_command()
22610 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; in do_meta_command()
22613 /* --help lists all file-controls */ in do_meta_command()
22615 utf8_printf(p->out, "Available file-controls:\n"); in do_meta_command()
22617 utf8_printf(p->out, " .filectrl %s %s\n", in do_meta_command()
22633 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n" in do_meta_command()
22634 "Use \".filectrl --help\" for help\n", zCmd); in do_meta_command()
22641 utf8_printf(stderr,"Error: unknown file-control: %s\n" in do_meta_command()
22642 "Use \".filectrl --help\" for help\n", zCmd); in do_meta_command()
22647 iRes = nArg==3 ? integerValue(azArg[2]) : -1; in do_meta_command()
22648 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes); in do_meta_command()
22657 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
22665 x = nArg==3 ? booleanValue(azArg[2]) : -1; in do_meta_command()
22666 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
22675 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
22683 sqlite3_file_control(p->db, zSchema, filectrl, &z); in do_meta_command()
22685 utf8_printf(p->out, "%s\n", z); in do_meta_command()
22695 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
22697 x = -1; in do_meta_command()
22698 sqlite3_file_control(p->db, zSchema, filectrl, &x); in do_meta_command()
22699 utf8_printf(p->out,"%d\n", x); in do_meta_command()
22706 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); in do_meta_command()
22711 raw_printf(p->out, "%s\n", zBuf); in do_meta_command()
22726 raw_printf(stderr, "Usage: .fullschema ?--indent?\n"); in do_meta_command()
22731 rc = sqlite3_exec(p->db, in do_meta_command()
22742 rc = sqlite3_prepare_v2(p->db, in do_meta_command()
22745 -1, &pStmt, 0); in do_meta_command()
22750 raw_printf(p->out, "/* No STAT tables available */\n"); in do_meta_command()
22752 raw_printf(p->out, "ANALYZE sqlite_schema;\n"); in do_meta_command()
22758 raw_printf(p->out, "ANALYZE sqlite_schema;\n"); in do_meta_command()
22764 p->showHeader = booleanValue(azArg[1]); in do_meta_command()
22765 p->shellFlgs |= SHFLG_HeaderSet; in do_meta_command()
22774 n = showHelp(p->out, azArg[1]); in do_meta_command()
22776 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]); in do_meta_command()
22779 showHelp(p->out, 0); in do_meta_command()
22793 int nSep; /* Number of bytes in p->colSeparator[] */ in do_meta_command()
22805 if( p->mode==MODE_Ascii ){ in do_meta_command()
22813 if( z[0]=='-' && z[1]=='-' ) z++; in do_meta_command()
22814 if( z[0]!='-' ){ in do_meta_command()
22820 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z); in do_meta_command()
22821 showHelp(p->out, "import"); in do_meta_command()
22824 }else if( cli_strcmp(z,"-v")==0 ){ in do_meta_command()
22826 }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){ in do_meta_command()
22828 }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){ in do_meta_command()
22830 }else if( cli_strcmp(z,"-ascii")==0 ){ in do_meta_command()
22835 }else if( cli_strcmp(z,"-csv")==0 ){ in do_meta_command()
22841 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z); in do_meta_command()
22842 showHelp(p->out, "import"); in do_meta_command()
22847 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n", in do_meta_command()
22849 showHelp(p->out, "import"); in do_meta_command()
22855 /* If neither the --csv or --ascii options are specified, then set in do_meta_command()
22857 nSep = strlen30(p->colSeparator); in do_meta_command()
22860 "Error: non-null column separator required for import\n"); in do_meta_command()
22865 "Error: multi-character column separators not allowed" in do_meta_command()
22869 nSep = strlen30(p->rowSeparator); in do_meta_command()
22872 "Error: non-null row separator required for import\n"); in do_meta_command()
22875 if( nSep==2 && p->mode==MODE_Csv in do_meta_command()
22876 && cli_strcmp(p->rowSeparator,SEP_CrLf)==0 in do_meta_command()
22882 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
22883 nSep = strlen30(p->rowSeparator); in do_meta_command()
22886 raw_printf(stderr, "Error: multi-character row separators not allowed" in do_meta_command()
22890 sCtx.cColSep = p->colSeparator[0]; in do_meta_command()
22891 sCtx.cRowSep = p->rowSeparator[0]; in do_meta_command()
22916 utf8_printf(p->out, "Column separator "); in do_meta_command()
22917 output_c_string(p->out, zSep); in do_meta_command()
22918 utf8_printf(p->out, ", row separator "); in do_meta_command()
22920 output_c_string(p->out, zSep); in do_meta_command()
22921 utf8_printf(p->out, "\n"); in do_meta_command()
22929 while( (nSkip--)>0 ){ in do_meta_command()
22943 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
22945 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){ in do_meta_command()
22956 utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr, in do_meta_command()
22974 utf8_printf(p->out, "%s\n", zCreate); in do_meta_command()
22976 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); in do_meta_command()
22978 utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db)); in do_meta_command()
22983 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
22987 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
23009 utf8_printf(p->out, "Insert using: %s\n", zSql); in do_meta_command()
23011 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
23013 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
23019 needCommit = sqlite3_get_autocommit(p->db); in do_meta_command()
23020 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); in do_meta_command()
23026 ** Did we reach end-of-file before finding any columns? in do_meta_command()
23031 ** Did we reach end-of-file OR end-of-line before finding any in do_meta_command()
23035 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; in do_meta_command()
23036 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); in do_meta_command()
23037 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){ in do_meta_command()
23038 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " in do_meta_command()
23050 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - " in do_meta_command()
23059 startLine, sqlite3_errmsg(p->db)); in do_meta_command()
23069 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); in do_meta_command()
23071 utf8_printf(p->out, in do_meta_command()
23073 sCtx.nRow, sCtx.nErr, sCtx.nLine-1); in do_meta_command()
23102 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); in do_meta_command()
23114 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
23122 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
23130 if( sqlite3_column_int(pStmt,1)==-1 ){ in do_meta_command()
23158 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); in do_meta_command()
23160 rc = sqlite3_exec(p->db, zSql, 0, 0, 0); in do_meta_command()
23161 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); in do_meta_command()
23163 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); in do_meta_command()
23186 }else if( cli_strcmp(azArg[1], "-")==0 ){ in do_meta_command()
23225 sqlite3_limit(p->db, aLimit[i].limitCode, -1)); in do_meta_command()
23228 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n"); in do_meta_command()
23232 int iLimit = -1; in do_meta_command()
23253 sqlite3_limit(p->db, aLimit[iLimit].limitCode, in do_meta_command()
23257 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); in do_meta_command()
23279 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); in do_meta_command()
23296 output_file_close(p->pLog); in do_meta_command()
23297 p->pLog = output_file_open(zFile, 0); in do_meta_command()
23321 /* Apply defaults for qbox pseudo-mode. If that in do_meta_command()
23322 * overwrites already-set values, user was informed of this. in do_meta_command()
23331 }else if( z[0]=='-' ){ in do_meta_command()
23334 " --noquote\n" in do_meta_command()
23335 " --quote\n" in do_meta_command()
23336 " --wordwrap on/off\n" in do_meta_command()
23337 " --wrap N\n" in do_meta_command()
23338 " --ww\n"); in do_meta_command()
23348 if( p->mode==MODE_Column in do_meta_command()
23349 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) in do_meta_command()
23352 (p->out, in do_meta_command()
23353 "current output mode: %s --wrap %d --wordwrap %s --%squote\n", in do_meta_command()
23354 modeDescr[p->mode], p->cmOpts.iWrap, in do_meta_command()
23355 p->cmOpts.bWordWrap ? "on" : "off", in do_meta_command()
23356 p->cmOpts.bQuote ? "" : "no"); in do_meta_command()
23358 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]); in do_meta_command()
23360 zMode = modeDescr[p->mode]; in do_meta_command()
23364 p->mode = MODE_Line; in do_meta_command()
23365 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
23367 p->mode = MODE_Column; in do_meta_command()
23368 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){ in do_meta_command()
23369 p->showHeader = 1; in do_meta_command()
23371 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
23372 p->cmOpts = cmOpts; in do_meta_command()
23374 p->mode = MODE_List; in do_meta_command()
23375 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); in do_meta_command()
23376 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
23378 p->mode = MODE_Html; in do_meta_command()
23380 p->mode = MODE_Tcl; in do_meta_command()
23381 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); in do_meta_command()
23382 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
23384 p->mode = MODE_Csv; in do_meta_command()
23385 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); in do_meta_command()
23386 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); in do_meta_command()
23388 p->mode = MODE_List; in do_meta_command()
23389 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); in do_meta_command()
23391 p->mode = MODE_Insert; in do_meta_command()
23394 p->mode = MODE_Quote; in do_meta_command()
23395 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); in do_meta_command()
23396 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); in do_meta_command()
23398 p->mode = MODE_Ascii; in do_meta_command()
23399 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); in do_meta_command()
23400 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); in do_meta_command()
23402 p->mode = MODE_Markdown; in do_meta_command()
23403 p->cmOpts = cmOpts; in do_meta_command()
23405 p->mode = MODE_Table; in do_meta_command()
23406 p->cmOpts = cmOpts; in do_meta_command()
23408 p->mode = MODE_Box; in do_meta_command()
23409 p->cmOpts = cmOpts; in do_meta_command()
23411 p->mode = MODE_Count; in do_meta_command()
23413 p->mode = MODE_Off; in do_meta_command()
23415 p->mode = MODE_Json; in do_meta_command()
23422 p->cMode = p->mode; in do_meta_command()
23430 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){ in do_meta_command()
23432 p->lineno, azArg[1]); in do_meta_command()
23435 p->bSafeMode = 0; in do_meta_command()
23444 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, in do_meta_command()
23445 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); in do_meta_command()
23453 const char *zFN = 0; /* Pointer to constant filename */ in do_meta_command()
23459 /* Check for command-line arguments */ in do_meta_command()
23474 p->openFlags |= SQLITE_OPEN_NOFOLLOW; in do_meta_command()
23481 p->szMax = integerValue(azArg[++iName]); in do_meta_command()
23485 if( z[0]=='-' ){ in do_meta_command()
23499 session_close_all(p, -1); in do_meta_command()
23500 close_db(p->db); in do_meta_command()
23501 p->db = 0; in do_meta_command()
23502 p->pAuxDb->zDbFilename = 0; in do_meta_command()
23503 sqlite3_free(p->pAuxDb->zFreeOnClose); in do_meta_command()
23504 p->pAuxDb->zFreeOnClose = 0; in do_meta_command()
23505 p->openMode = openMode; in do_meta_command()
23506 p->openFlags = 0; in do_meta_command()
23507 p->szMax = 0; in do_meta_command()
23510 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){ in do_meta_command()
23511 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN); in do_meta_command()
23513 if( p->bSafeMode in do_meta_command()
23514 && p->openMode!=SHELL_OPEN_HEXDB in do_meta_command()
23518 failIfSafeMode(p, "cannot open disk-based database files in safe mode"); in do_meta_command()
23521 /* WASM mode has its own sandboxed pseudo-filesystem. */ in do_meta_command()
23529 p->pAuxDb->zDbFilename = zNewFilename; in do_meta_command()
23531 if( p->db==0 ){ in do_meta_command()
23535 p->pAuxDb->zFreeOnClose = zNewFilename; in do_meta_command()
23538 if( p->db==0 ){ in do_meta_command()
23539 /* As a fall-back open a TEMP database */ in do_meta_command()
23540 p->pAuxDb->zDbFilename = 0; in do_meta_command()
23556 unsigned char zBOM[4]; /* Byte-order mark to using if --bom is present */ in do_meta_command()
23568 if( z[0]=='-' ){ in do_meta_command()
23569 if( z[1]=='-' ) z++; in do_meta_command()
23570 if( cli_strcmp(z,"-bom")==0 ){ in do_meta_command()
23575 }else if( c!='e' && cli_strcmp(z,"-x")==0 ){ in do_meta_command()
23577 }else if( c!='e' && cli_strcmp(z,"-e")==0 ){ in do_meta_command()
23580 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", in do_meta_command()
23582 showHelp(p->out, azArg[0]); in do_meta_command()
23593 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n", in do_meta_command()
23595 showHelp(p->out, azArg[0]); in do_meta_command()
23605 p->outCount = 2; in do_meta_command()
23607 p->outCount = 0; in do_meta_command()
23612 p->doXdgOpen = 1; in do_meta_command()
23618 p->mode = MODE_Csv; in do_meta_command()
23619 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); in do_meta_command()
23620 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); in do_meta_command()
23627 zFile = sqlite3_mprintf("%s", p->zTempFile); in do_meta_command()
23635 p->out = stdout; in do_meta_command()
23637 p->out = popen(zFile + 1, "w"); in do_meta_command()
23638 if( p->out==0 ){ in do_meta_command()
23640 p->out = stdout; in do_meta_command()
23643 if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out); in do_meta_command()
23644 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); in do_meta_command()
23648 p->out = output_file_open(zFile, bTxtMode); in do_meta_command()
23649 if( p->out==0 ){ in do_meta_command()
23653 p->out = stdout; in do_meta_command()
23656 if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out); in do_meta_command()
23657 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); in do_meta_command()
23672 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", in do_meta_command()
23683 rx = sqlite3_prepare_v2(p->db, in do_meta_command()
23685 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); in do_meta_command()
23693 rx = sqlite3_prepare_v2(p->db, in do_meta_command()
23695 "FROM temp.sqlite_parameters;", -1, &pStmt, 0); in do_meta_command()
23697 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0), in do_meta_command()
23730 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
23739 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
23742 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
23760 sqlite3_exec(p->db, zSql, 0, 0, 0); in do_meta_command()
23765 showHelp(p->out, "parameter"); in do_meta_command()
23771 if( i>1 ) raw_printf(p->out, " "); in do_meta_command()
23772 utf8_printf(p->out, "%s", azArg[i]); in do_meta_command()
23774 raw_printf(p->out, "\n"); in do_meta_command()
23781 p->flgProgress = 0; in do_meta_command()
23782 p->mxProgress = 0; in do_meta_command()
23783 p->nProgress = 0; in do_meta_command()
23786 if( z[0]=='-' ){ in do_meta_command()
23788 if( z[0]=='-' ) z++; in do_meta_command()
23790 p->flgProgress |= SHELL_PROGRESS_QUIET; in do_meta_command()
23794 p->flgProgress |= SHELL_PROGRESS_RESET; in do_meta_command()
23798 p->flgProgress |= SHELL_PROGRESS_ONCE; in do_meta_command()
23803 utf8_printf(stderr, "Error: missing argument on --limit\n"); in do_meta_command()
23807 p->mxProgress = (int)integerValue(azArg[++i]); in do_meta_command()
23819 sqlite3_progress_handler(p->db, nn, progress_handler, p); in do_meta_command()
23825 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); in do_meta_command()
23828 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); in do_meta_command()
23840 FILE *inSaved = p->in; in do_meta_command()
23841 int savedLineno = p->lineno; in do_meta_command()
23852 p->out = stdout; in do_meta_command()
23854 p->in = popen(azArg[1]+1, "r"); in do_meta_command()
23855 if( p->in==0 ){ in do_meta_command()
23860 pclose(p->in); in do_meta_command()
23863 }else if( (p->in = openChrSource(azArg[1]))==0 ){ in do_meta_command()
23868 fclose(p->in); in do_meta_command()
23870 p->in = inSaved; in do_meta_command()
23871 p->lineno = savedLineno; in do_meta_command()
23902 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); in do_meta_command()
23904 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
23922 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
23931 p->scanstatsOn = (u8)booleanValue(azArg[1]); in do_meta_command()
23964 }else if( azArg[ii][0]=='-' ){ in do_meta_command()
23971 raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n"); in do_meta_command()
24001 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", in do_meta_command()
24002 -1, &pStmt, 0); in do_meta_command()
24004 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); in do_meta_command()
24066 utf8_printf(p->out, "SQL: %s;\n", sSelect.z); in do_meta_command()
24068 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); in do_meta_command()
24093 struct AuxDb *pAuxDb = p->pAuxDb; in do_meta_command()
24094 OpenSession *pSession = &pAuxDb->aSession[0]; in do_meta_command()
24097 int nCmd = nArg - 1; in do_meta_command()
24102 for(iSes=0; iSes<pAuxDb->nSession; iSes++){ in do_meta_command()
24103 if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break; in do_meta_command()
24105 if( iSes<pAuxDb->nSession ){ in do_meta_command()
24106 pSession = &pAuxDb->aSession[iSes]; in do_meta_command()
24108 nCmd--; in do_meta_command()
24110 pSession = &pAuxDb->aSession[0]; in do_meta_command()
24121 if( pSession->p==0 ){ in do_meta_command()
24125 rc = sqlite3session_attach(pSession->p, azCmd[1]); in do_meta_command()
24143 if( pSession->p==0 ) goto session_not_open; in do_meta_command()
24152 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); in do_meta_command()
24154 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); in do_meta_command()
24162 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n", in do_meta_command()
24175 if( pAuxDb->nSession ){ in do_meta_command()
24177 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession]; in do_meta_command()
24187 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); in do_meta_command()
24188 if( pAuxDb->nSession ){ in do_meta_command()
24189 ii = sqlite3session_enable(pSession->p, ii); in do_meta_command()
24190 utf8_printf(p->out, "session %s enable flag = %d\n", in do_meta_command()
24191 pSession->zName, ii); in do_meta_command()
24201 if( pAuxDb->nSession ){ in do_meta_command()
24202 for(ii=0; ii<pSession->nFilter; ii++){ in do_meta_command()
24203 sqlite3_free(pSession->azFilter[ii]); in do_meta_command()
24205 sqlite3_free(pSession->azFilter); in do_meta_command()
24206 nByte = sizeof(pSession->azFilter[0])*(nCmd-1); in do_meta_command()
24207 pSession->azFilter = sqlite3_malloc( nByte ); in do_meta_command()
24208 if( pSession->azFilter==0 ){ in do_meta_command()
24213 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); in do_meta_command()
24216 pSession->nFilter = ii-1; in do_meta_command()
24226 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); in do_meta_command()
24227 if( pAuxDb->nSession ){ in do_meta_command()
24228 ii = sqlite3session_indirect(pSession->p, ii); in do_meta_command()
24229 utf8_printf(p->out, "session %s indirect flag = %d\n", in do_meta_command()
24230 pSession->zName, ii); in do_meta_command()
24240 if( pAuxDb->nSession ){ in do_meta_command()
24241 ii = sqlite3session_isempty(pSession->p); in do_meta_command()
24242 utf8_printf(p->out, "session %s isempty flag = %d\n", in do_meta_command()
24243 pSession->zName, ii); in do_meta_command()
24251 for(i=0; i<pAuxDb->nSession; i++){ in do_meta_command()
24252 utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName); in do_meta_command()
24265 for(i=0; i<pAuxDb->nSession; i++){ in do_meta_command()
24266 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){ in do_meta_command()
24271 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){ in do_meta_command()
24272 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession)); in do_meta_command()
24275 pSession = &pAuxDb->aSession[pAuxDb->nSession]; in do_meta_command()
24276 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); in do_meta_command()
24282 pSession->nFilter = 0; in do_meta_command()
24283 sqlite3session_table_filter(pSession->p, session_filter, pSession); in do_meta_command()
24284 pAuxDb->nSession++; in do_meta_command()
24285 pSession->zName = sqlite3_mprintf("%s", zName); in do_meta_command()
24286 shell_check_oom(pSession->zName); in do_meta_command()
24290 showHelp(p->out, "session"); in do_meta_command()
24297 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){ in do_meta_command()
24298 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){ in do_meta_command()
24302 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); in do_meta_command()
24305 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){ in do_meta_command()
24311 utf8_printf(p->out, "%s", zBuf); in do_meta_command()
24330 if( z[0]=='-' && z[1]=='-' ) z++; in do_meta_command()
24331 if( cli_strcmp(z,"-init")==0 ){ in do_meta_command()
24334 if( cli_strcmp(z,"-v")==0 ){ in do_meta_command()
24340 raw_printf(stderr, "Should be one of: --init -v\n"); in do_meta_command()
24345 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) in do_meta_command()
24357 for(k=bSelftestExists; k>=0; k--){ in do_meta_command()
24359 rc = sqlite3_prepare_v2(p->db, in do_meta_command()
24361 -1, &pStmt, 0); in do_meta_command()
24363 rc = sqlite3_prepare_v2(p->db, in do_meta_command()
24364 "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," in do_meta_command()
24366 -1, &pStmt, 0); in do_meta_command()
24388 utf8_printf(p->out, "%s\n", zSql); in do_meta_command()
24394 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); in do_meta_command()
24397 utf8_printf(p->out, "Result: %s\n", str.z); in do_meta_command()
24402 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg); in do_meta_command()
24407 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns); in do_meta_command()
24408 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z); in do_meta_command()
24421 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest); in do_meta_command()
24430 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, in do_meta_command()
24431 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); in do_meta_command()
24434 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, in do_meta_command()
24435 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); in do_meta_command()
24454 if( z[0]=='-' ){ in do_meta_command()
24456 if( z[0]=='-' ) z++; in do_meta_command()
24460 if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0 in do_meta_command()
24461 || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0 in do_meta_command()
24471 showHelp(p->out, azArg[0]); in do_meta_command()
24476 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); in do_meta_command()
24496 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); in do_meta_command()
24548 utf8_printf(p->out, "%s\n", zSql); in do_meta_command()
24588 utf8_printf(p->out, "%12.12s: %s\n","echo", in do_meta_command()
24590 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); in do_meta_command()
24591 utf8_printf(p->out, "%12.12s: %s\n","explain", in do_meta_command()
24592 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); in do_meta_command()
24593 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]); in do_meta_command()
24594 if( p->mode==MODE_Column in do_meta_command()
24595 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) in do_meta_command()
24598 (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode", in do_meta_command()
24599 modeDescr[p->mode], p->cmOpts.iWrap, in do_meta_command()
24600 p->cmOpts.bWordWrap ? "on" : "off", in do_meta_command()
24601 p->cmOpts.bQuote ? "" : "no"); in do_meta_command()
24603 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); in do_meta_command()
24605 utf8_printf(p->out, "%12.12s: ", "nullvalue"); in do_meta_command()
24606 output_c_string(p->out, p->nullValue); in do_meta_command()
24607 raw_printf(p->out, "\n"); in do_meta_command()
24608 utf8_printf(p->out,"%12.12s: %s\n","output", in do_meta_command()
24609 strlen30(p->outfile) ? p->outfile : "stdout"); in do_meta_command()
24610 utf8_printf(p->out,"%12.12s: ", "colseparator"); in do_meta_command()
24611 output_c_string(p->out, p->colSeparator); in do_meta_command()
24612 raw_printf(p->out, "\n"); in do_meta_command()
24613 utf8_printf(p->out,"%12.12s: ", "rowseparator"); in do_meta_command()
24614 output_c_string(p->out, p->rowSeparator); in do_meta_command()
24615 raw_printf(p->out, "\n"); in do_meta_command()
24616 switch( p->statsOn ){ in do_meta_command()
24622 utf8_printf(p->out, "%12.12s: %s\n","stats", zOut); in do_meta_command()
24623 utf8_printf(p->out, "%12.12s: ", "width"); in do_meta_command()
24624 for (i=0;i<p->nWidth;i++) { in do_meta_command()
24625 raw_printf(p->out, "%d ", p->colWidth[i]); in do_meta_command()
24627 raw_printf(p->out, "\n"); in do_meta_command()
24628 utf8_printf(p->out, "%12.12s: %s\n", "filename", in do_meta_command()
24629 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : ""); in do_meta_command()
24635 p->statsOn = 2; in do_meta_command()
24637 p->statsOn = 3; in do_meta_command()
24639 p->statsOn = (u8)booleanValue(azArg[1]); in do_meta_command()
24642 display_stats(p->db, p, 0); in do_meta_command()
24660 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); in do_meta_command()
24663 return shellDatabaseError(p->db); in do_meta_command()
24670 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n"); in do_meta_command()
24700 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); in do_meta_command()
24703 if( rc ) return shellDatabaseError(p->db); in do_meta_command()
24706 ** as an array of nul-terminated strings in azResult[]. */ in do_meta_command()
24710 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); in do_meta_command()
24712 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); in do_meta_command()
24728 rc = shellDatabaseError(p->db); in do_meta_command()
24731 /* Pretty-print the contents of array azResult[] to the output */ in do_meta_command()
24742 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; in do_meta_command()
24746 utf8_printf(p->out, "%s%-*s", zSp, maxlen, in do_meta_command()
24749 raw_printf(p->out, "\n"); in do_meta_command()
24758 /* Begin redirecting output to the file "testcase-out.txt" */ in do_meta_command()
24761 p->out = output_file_open("testcase-out.txt", 0); in do_meta_command()
24762 if( p->out==0 ){ in do_meta_command()
24763 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n"); in do_meta_command()
24766 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); in do_meta_command()
24768 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); in do_meta_command()
24776 const char *zCtrlName; /* Name of a test-control option */ in do_meta_command()
24778 int unSafe; /* Not valid for --safe mode */ in do_meta_command()
24792 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK" }, in do_meta_command()
24804 int testctrl = -1; in do_meta_command()
24805 int iCtrl = -1; in do_meta_command()
24806 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ in do_meta_command()
24814 /* The argument can optionally begin with "-" or "--" */ in do_meta_command()
24815 if( zCmd[0]=='-' && zCmd[1] ){ in do_meta_command()
24817 if( zCmd[0]=='-' && zCmd[1] ) zCmd++; in do_meta_command()
24820 /* --help lists all test-controls */ in do_meta_command()
24822 utf8_printf(p->out, "Available test-controls:\n"); in do_meta_command()
24824 utf8_printf(p->out, " .testctrl %s %s\n", in do_meta_command()
24840 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n" in do_meta_command()
24841 "Use \".testctrl --help\" for help\n", zCmd); in do_meta_command()
24848 utf8_printf(stderr,"Error: unknown test-control: %s\n" in do_meta_command()
24849 "Use \".testctrl --help\" for help\n", zCmd); in do_meta_command()
24850 }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){ in do_meta_command()
24853 p->lineno, aCtrl[iCtrl].zCtrlName); in do_meta_command()
24862 rc2 = sqlite3_test_control(testctrl, p->db, opt); in do_meta_command()
24893 printf("-- random seed: %d\n", ii); in do_meta_command()
24898 db = p->db; in do_meta_command()
24929 rc2 = sqlite3_test_control(testctrl, p->db); in do_meta_command()
24935 rc2 = sqlite3_test_control(testctrl, p->db, in do_meta_command()
24945 rc2 = sqlite3_test_control(testctrl, p->db, &x); in do_meta_command()
24946 utf8_printf(p->out, "%llu\n", x); in do_meta_command()
24954 sqlite3_test_control(testctrl, p->out); in do_meta_command()
24969 sqlite3_test_control(testctrl, -id, &rc2); in do_meta_command()
24975 rc2 = sqlite3_test_control(testctrl, -id, &val); in do_meta_command()
24977 if( id>1 ) utf8_printf(p->out, " "); in do_meta_command()
24978 utf8_printf(p->out, "%d: %d", id, val); in do_meta_command()
24981 if( id>1 ) utf8_printf(p->out, "\n"); in do_meta_command()
24990 rc2 = sqlite3_test_control(testctrl, p->db, opt); in do_meta_command()
24997 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); in do_meta_command()
25000 raw_printf(p->out, "%d\n", rc2); in do_meta_command()
25002 raw_printf(p->out, "0x%08x\n", rc2); in do_meta_command()
25009 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); in do_meta_command()
25032 if( z[0]=='-' ){ in do_meta_command()
25034 p->eTraceType = SHELL_TRACE_EXPANDED; in do_meta_command()
25038 p->eTraceType = SHELL_TRACE_NORMALIZED; in do_meta_command()
25042 p->eTraceType = SHELL_TRACE_PLAIN; in do_meta_command()
25062 output_file_close(p->traceOut); in do_meta_command()
25063 p->traceOut = output_file_open(azArg[1], 0); in do_meta_command()
25066 if( p->traceOut==0 ){ in do_meta_command()
25067 sqlite3_trace_v2(p->db, 0, 0, 0); in do_meta_command()
25070 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); in do_meta_command()
25081 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n"); in do_meta_command()
25087 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; in do_meta_command()
25089 if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){ in do_meta_command()
25091 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); in do_meta_command()
25094 sqlite3_create_module(p->db, azArg[ii], 0, 0); in do_meta_command()
25114 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], in do_meta_command()
25126 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]), in do_meta_command()
25129 raw_printf(stderr, "User-Add failed: %d\n", rc); in do_meta_command()
25138 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]), in do_meta_command()
25141 raw_printf(stderr, "User-Edit failed: %d\n", rc); in do_meta_command()
25150 rc = sqlite3_user_delete(p->db, azArg[2]); in do_meta_command()
25152 raw_printf(stderr, "User-Delete failed: %d\n", rc); in do_meta_command()
25164 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/, in do_meta_command()
25167 utf8_printf(p->out, "zlib version %s\n", zlibVersion()); in do_meta_command()
25172 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." in do_meta_command()
25176 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n"); in do_meta_command()
25178 utf8_printf(p->out, "gcc-" __VERSION__ "\n"); in do_meta_command()
25185 if( p->db ){ in do_meta_command()
25186 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); in do_meta_command()
25188 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); in do_meta_command()
25189 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); in do_meta_command()
25190 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); in do_meta_command()
25191 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); in do_meta_command()
25199 if( p->db ){ in do_meta_command()
25200 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); in do_meta_command()
25202 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ in do_meta_command()
25203 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, in do_meta_command()
25204 pVfs==pCurrent ? " <--- CURRENT" : ""); in do_meta_command()
25205 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); in do_meta_command()
25206 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); in do_meta_command()
25207 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); in do_meta_command()
25208 if( pVfs->pNext ){ in do_meta_command()
25209 raw_printf(p->out, "-----------------------------------\n"); in do_meta_command()
25217 if( p->db ){ in do_meta_command()
25218 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); in do_meta_command()
25220 utf8_printf(p->out, "%s\n", zVfsName); in do_meta_command()
25234 p->nWidth = nArg-1; in do_meta_command()
25235 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2); in do_meta_command()
25236 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory(); in do_meta_command()
25237 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth]; in do_meta_command()
25239 p->colWidth[j-1] = (int)integerValue(azArg[j]); in do_meta_command()
25250 if( p->outCount ){ in do_meta_command()
25251 p->outCount--; in do_meta_command()
25252 if( p->outCount==0 ) output_reset(p); in do_meta_command()
25254 p->bSafeMode = p->bSafeModePersist; in do_meta_command()
25265 QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
25289 case '-': in quickscan()
25290 if( *zLine!='-' ) in quickscan()
25351 ** than a semi-colon. The SQL Server style "go" command is understood
25369 ** user-friendly, but it does seem to work.
25377 ** ends in the middle of a string literal or C-style comment.
25398 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; in runOneSqlLine()
25408 zErrorTail = sqlite3_errmsg(p->db); in runOneSqlLine()
25433 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db)); in runOneSqlLine()
25434 raw_printf(p->out, "%s\n", zLineBuf); in runOneSqlLine()
25440 if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo); in echo_group_input()
25464 if(nZ>0 && '\r'==zBegin[nZ-1]){ in one_input_line()
25465 --nZ; in one_input_line()
25478 ** is interactive - the user is typing it it. Otherwise, input
25496 if( p->inputNesting==MAX_INPUT_NESTING ){ in process_input()
25499 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno); in process_input()
25502 ++p->inputNesting; in process_input()
25503 p->lineno = 0; in process_input()
25504 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ in process_input()
25505 fflush(p->out); in process_input()
25506 zLine = one_input_line(p->in, zLine, nSql>0); in process_input()
25509 if( p->in==0 && stdin_is_interactive ) printf("\n"); in process_input()
25513 if( p->in!=0 ) break; in process_input()
25516 p->lineno++; in process_input()
25524 /* Just swallow single-line whitespace */ in process_input()
25542 /* No single-line dispositions remain; accumulate line(s). */ in process_input()
25545 /* Grow buffer by half-again increments when big. */ in process_input()
25554 memcpy(zSql, zLine+i, nLine+1-i); in process_input()
25555 startline = p->lineno; in process_input()
25556 nSql = nLine-i; in process_input()
25564 errCnt += runOneSqlLine(p, zSql, p->in, startline); in process_input()
25566 if( p->outCount ){ in process_input()
25568 p->outCount = 0; in process_input()
25572 p->bSafeMode = p->bSafeModePersist; in process_input()
25583 errCnt += runOneSqlLine(p, zSql, p->in, startline); in process_input()
25587 --p->inputNesting; in process_input()
25610 home_dir = pwent->pw_dir; in find_home_dir()
25616 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() in find_home_dir()
25673 FILE *inSaved = p->in; in process_sqliterc()
25674 int savedLineno = p->lineno; in process_sqliterc()
25679 raw_printf(stderr, "-- warning: cannot find home directory;" in process_sqliterc()
25687 p->in = fopen(sqliterc,"rb"); in process_sqliterc()
25688 if( p->in ){ in process_sqliterc()
25690 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc); in process_sqliterc()
25693 fclose(p->in); in process_sqliterc()
25698 p->in = inSaved; in process_sqliterc()
25699 p->lineno = savedLineno; in process_sqliterc()
25708 " -A ARGS... run \".archive ARGS\" and exit\n"
25710 " -append append the database to the end of the file\n"
25711 " -ascii set output mode to 'ascii'\n"
25712 " -bail stop after hitting an error\n"
25713 " -batch force batch I/O\n"
25714 " -box set output mode to 'box'\n"
25715 " -column set output mode to 'column'\n"
25716 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
25717 " -csv set output mode to 'csv'\n"
25719 " -deserialize open the database using sqlite3_deserialize()\n"
25721 " -echo print inputs before execution\n"
25722 " -init FILENAME read/process named file\n"
25723 " -[no]header turn headers on or off\n"
25725 " -heap SIZE Size of heap for memsys3 or memsys5\n"
25727 " -help show this message\n"
25728 " -html set output mode to HTML\n"
25729 " -interactive force interactive I/O\n"
25730 " -json set output mode to 'json'\n"
25731 " -line set output mode to 'line'\n"
25732 " -list set output mode to 'list'\n"
25733 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
25734 " -markdown set output mode to 'markdown'\n"
25736 " -maxsize N maximum size for a --deserialize database\n"
25738 " -memtrace trace all memory allocations and deallocations\n"
25739 " -mmap N default mmap size set to N\n"
25741 " -multiplex enable the multiplexor VFS\n"
25743 " -newline SEP set output row separator. Default: '\\n'\n"
25744 " -nofollow refuse to open symbolic links to database files\n"
25745 " -nonce STRING set the safe-mode escape nonce\n"
25746 " -nullvalue TEXT set text string for NULL values. Default ''\n"
25747 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
25748 " -quote set output mode to 'quote'\n"
25749 " -readonly open the database read-only\n"
25750 " -safe enable safe-mode\n"
25751 " -separator SEP set output column separator. Default: '|'\n"
25753 " -sorterref SIZE sorter references threshold size\n"
25755 " -stats print memory stats before each finalize\n"
25756 " -table set output mode to 'table'\n"
25757 " -tabs set output mode to 'tabs'\n"
25758 " -version show SQLite version\n"
25759 " -vfs NAME use NAME as the default VFS\n"
25761 " -vfstrace enable tracing of all VFS calls\n"
25764 " -zip open the file as a ZIP Archive\n"
25775 raw_printf(stderr, "Use the -help option for additional information\n"); in usage()
25785 if( sqlite3_config(-1)==SQLITE_MISUSE ){ in verify_uninitialized()
25796 data->normalMode = data->cMode = data->mode = MODE_List; in main_init()
25797 data->autoExplain = 1; in main_init()
25798 data->pAuxDb = &data->aAuxDb[0]; in main_init()
25799 memcpy(data->colSeparator,SEP_Column, 2); in main_init()
25800 memcpy(data->rowSeparator,SEP_Row, 2); in main_init()
25801 data->showHeader = 0; in main_init()
25802 data->shellFlgs = SHFLG_Lookaside; in main_init()
25836 ** Get the argument to an --option. Throw an error and die if no argument
25842 argv[0], argv[argc-1]); in cmdline_option_value()
25883 const char *zVfs = 0; /* Value of -vfs command-line option */
25930 /* On Windows, we must translate command-line arguments into UTF-8.
25971 ** of a C-function that will provide the name of the database file. Use
25972 ** this compile-time option to embed this shell program in larger
25975 SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
25980 /* Do an initial pass through the command-line argument to locate
25989 if( z[0]!='-' ){
25990 if( data.aAuxDb->zDbFilename==0 ){
25991 data.aAuxDb->zDbFilename = z;
25993 /* Excesss arguments are interpreted as SQL (or dot-commands) and
25999 azCmd[nCmd-1] = z;
26002 if( z[1]=='-' ) z++;
26003 if( cli_strcmp(z,"-separator")==0
26004 || cli_strcmp(z,"-nullvalue")==0
26005 || cli_strcmp(z,"-newline")==0
26006 || cli_strcmp(z,"-cmd")==0
26009 }else if( cli_strcmp(z,"-init")==0 ){
26011 }else if( cli_strcmp(z,"-batch")==0 ){
26017 }else if( cli_strcmp(z,"-heap")==0 ){
26029 }else if( cli_strcmp(z,"-pagecache")==0 ){
26041 }else if( cli_strcmp(z,"-lookaside")==0 ){
26049 }else if( cli_strcmp(z,"-threadsafe")==0 ){
26058 }else if( cli_strcmp(z,"-vfstrace")==0 ){
26069 }else if( cli_strcmp(z,"-multiplex")==0 ){
26073 }else if( cli_strcmp(z,"-mmap")==0 ){
26077 }else if( cli_strcmp(z,"-sorterref")==0 ){
26081 }else if( cli_strcmp(z,"-vfs")==0 ){
26084 }else if( cli_strcmp(z,"-zip")==0 ){
26087 }else if( cli_strcmp(z,"-append")==0 ){
26090 }else if( cli_strcmp(z,"-deserialize")==0 ){
26092 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
26095 }else if( cli_strcmp(z,"-readonly")==0 ){
26097 }else if( cli_strcmp(z,"-nofollow")==0 ){
26100 }else if( cli_strncmp(z, "-A",2)==0 ){
26101 /* All remaining command-line arguments are passed to the ".archive"
26105 }else if( cli_strcmp(z, "-memtrace")==0 ){
26107 }else if( cli_strcmp(z,"-bail")==0 ){
26109 }else if( cli_strcmp(z,"-nonce")==0 ){
26112 }else if( cli_strcmp(z,"-safe")==0 ){
26113 /* no-op - catch this on the second pass */
26122 ** of a C-function that will perform initialization actions on SQLite that
26123 ** occur just before or after sqlite3_initialize(). Use this compile-time
26130 ** to call sqlite3_initialize() and process any command line -vfs option. */
26144 if( data.pAuxDb->zDbFilename==0 ){
26146 data.pAuxDb->zDbFilename = ":memory:";
26161 ** to the sqlite command-line tool.
26163 if( access(data.pAuxDb->zDbFilename, 0)==0 ){
26167 /* Process the initialization file if there is one. If no -init option
26173 /* Make a second pass through the command-line argument and set
26175 ** file is processed so that the command-line arguments will override
26180 if( z[0]!='-' ) continue;
26181 if( z[1]=='-' ){ z++; }
26182 if( cli_strcmp(z,"-init")==0 ){
26184 }else if( cli_strcmp(z,"-html")==0 ){
26186 }else if( cli_strcmp(z,"-list")==0 ){
26188 }else if( cli_strcmp(z,"-quote")==0 ){
26192 }else if( cli_strcmp(z,"-line")==0 ){
26194 }else if( cli_strcmp(z,"-column")==0 ){
26196 }else if( cli_strcmp(z,"-json")==0 ){
26198 }else if( cli_strcmp(z,"-markdown")==0 ){
26200 }else if( cli_strcmp(z,"-table")==0 ){
26202 }else if( cli_strcmp(z,"-box")==0 ){
26204 }else if( cli_strcmp(z,"-csv")==0 ){
26208 }else if( cli_strcmp(z,"-zip")==0 ){
26211 }else if( cli_strcmp(z,"-append")==0 ){
26214 }else if( cli_strcmp(z,"-deserialize")==0 ){
26216 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
26219 }else if( cli_strcmp(z,"-readonly")==0 ){
26221 }else if( cli_strcmp(z,"-nofollow")==0 ){
26223 }else if( cli_strcmp(z,"-ascii")==0 ){
26227 }else if( cli_strcmp(z,"-tabs")==0 ){
26231 }else if( cli_strcmp(z,"-separator")==0 ){
26234 }else if( cli_strcmp(z,"-newline")==0 ){
26237 }else if( cli_strcmp(z,"-nullvalue")==0 ){
26240 }else if( cli_strcmp(z,"-header")==0 ){
26243 }else if( cli_strcmp(z,"-noheader")==0 ){
26246 }else if( cli_strcmp(z,"-echo")==0 ){
26248 }else if( cli_strcmp(z,"-eqp")==0 ){
26250 }else if( cli_strcmp(z,"-eqpfull")==0 ){
26252 }else if( cli_strcmp(z,"-stats")==0 ){
26254 }else if( cli_strcmp(z,"-scanstats")==0 ){
26256 }else if( cli_strcmp(z,"-backslash")==0 ){
26257 /* Undocumented command-line option: -backslash
26258 ** Causes C-style backslash escapes to be evaluated in SQL statements
26263 }else if( cli_strcmp(z,"-bail")==0 ){
26264 /* No-op. The bail_on_error flag should already be set. */
26265 }else if( cli_strcmp(z,"-version")==0 ){
26268 }else if( cli_strcmp(z,"-interactive")==0 ){
26270 }else if( cli_strcmp(z,"-batch")==0 ){
26272 }else if( cli_strcmp(z,"-heap")==0 ){
26274 }else if( cli_strcmp(z,"-pagecache")==0 ){
26276 }else if( cli_strcmp(z,"-lookaside")==0 ){
26278 }else if( cli_strcmp(z,"-threadsafe")==0 ){
26280 }else if( cli_strcmp(z,"-nonce")==0 ){
26282 }else if( cli_strcmp(z,"-mmap")==0 ){
26284 }else if( cli_strcmp(z,"-memtrace")==0 ){
26287 }else if( cli_strcmp(z,"-sorterref")==0 ){
26290 }else if( cli_strcmp(z,"-vfs")==0 ){
26293 }else if( cli_strcmp(z,"-vfstrace")==0 ){
26297 }else if( cli_strcmp(z,"-multiplex")==0 ){
26300 }else if( cli_strcmp(z,"-help")==0 ){
26302 }else if( cli_strcmp(z,"-cmd")==0 ){
26303 /* Run commands that follow -cmd first and separately from commands
26304 ** that simply appear on the command-line. This seems goofy. It would
26307 if( i==argc-1 ) break;
26324 }else if( cli_strncmp(z, "-A", 2)==0 ){
26326 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
26333 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
26335 arDotCommand(&data, 1, argv+i, argc-i);
26340 }else if( cli_strcmp(z,"-safe")==0 ){
26344 raw_printf(stderr,"Use -help for a list of options.\n");
26351 /* Run all arguments that do not begin with '-' as if they were separate
26352 ** command-line inputs, except for the argToSkip argument which contains
26385 "SQLite version %s %.19s\n" /*extra-version-info*/
26391 printBold("transient in-memory database");
26428 session_close_all(&data, -1);
26448 /* Clear the global data structure so that valgrind will detect memory
26454 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
26469 ** Returns a pointer to the current DB handle.
26476 ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
26516 ** Completely wipes out the contents of the currently-opened database
26531 ** on success, non-0 on error. This function returns 0 on success,
26532 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
26533 ** code from the callback. Note that this is not thread-friendly: it
26548 rc = pFile->pMethods->xFileSize(pFile, &nSize);
26560 rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);