| | @@ -1,8 +1,8 @@ |
| 1 | 1 | /****************************************************************************** |
| 2 | 2 | ** This file is an amalgamation of many separate C source files from SQLite |
| 3 | | -** version 3.8.9. By combining all the individual C code files into this |
| 3 | +** version 3.8.10.2. By combining all the individual C code files into this |
| 4 | 4 | ** single large file, the entire code can be compiled as a single translation |
| 5 | 5 | ** unit. This allows many compilers to do optimizations that would not be |
| 6 | 6 | ** possible if the files were compiled separately. Performance improvements |
| 7 | 7 | ** of 5% or more are commonly seen when SQLite is compiled as a single |
| 8 | 8 | ** translation unit. |
| | @@ -68,10 +68,11 @@ |
| 68 | 68 | #if defined(_MSC_VER) |
| 69 | 69 | #pragma warning(disable : 4054) |
| 70 | 70 | #pragma warning(disable : 4055) |
| 71 | 71 | #pragma warning(disable : 4100) |
| 72 | 72 | #pragma warning(disable : 4127) |
| 73 | +#pragma warning(disable : 4130) |
| 73 | 74 | #pragma warning(disable : 4152) |
| 74 | 75 | #pragma warning(disable : 4189) |
| 75 | 76 | #pragma warning(disable : 4206) |
| 76 | 77 | #pragma warning(disable : 4210) |
| 77 | 78 | #pragma warning(disable : 4232) |
| | @@ -315,13 +316,13 @@ |
| 315 | 316 | ** |
| 316 | 317 | ** See also: [sqlite3_libversion()], |
| 317 | 318 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 318 | 319 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 319 | 320 | */ |
| 320 | | -#define SQLITE_VERSION "3.8.9" |
| 321 | | -#define SQLITE_VERSION_NUMBER 3008009 |
| 322 | | -#define SQLITE_SOURCE_ID "2015-04-08 12:16:33 8a8ffc862e96f57aa698f93de10dee28e69f6e09" |
| 321 | +#define SQLITE_VERSION "3.8.10.2" |
| 322 | +#define SQLITE_VERSION_NUMBER 3008010 |
| 323 | +#define SQLITE_SOURCE_ID "2015-05-20 18:17:19 2ef4f3a5b1d1d0c4338f8243d40a2452cc1f7fe4" |
| 323 | 324 | |
| 324 | 325 | /* |
| 325 | 326 | ** CAPI3REF: Run-Time Library Version Numbers |
| 326 | 327 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 327 | 328 | ** |
| | @@ -474,10 +475,11 @@ |
| 474 | 475 | # define double sqlite3_int64 |
| 475 | 476 | #endif |
| 476 | 477 | |
| 477 | 478 | /* |
| 478 | 479 | ** CAPI3REF: Closing A Database Connection |
| 480 | +** DESTRUCTOR: sqlite3 |
| 479 | 481 | ** |
| 480 | 482 | ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors |
| 481 | 483 | ** for the [sqlite3] object. |
| 482 | 484 | ** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if |
| 483 | 485 | ** the [sqlite3] object is successfully destroyed and all associated |
| | @@ -525,10 +527,11 @@ |
| 525 | 527 | */ |
| 526 | 528 | typedef int (*sqlite3_callback)(void*,int,char**, char**); |
| 527 | 529 | |
| 528 | 530 | /* |
| 529 | 531 | ** CAPI3REF: One-Step Query Execution Interface |
| 532 | +** METHOD: sqlite3 |
| 530 | 533 | ** |
| 531 | 534 | ** The sqlite3_exec() interface is a convenience wrapper around |
| 532 | 535 | ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()], |
| 533 | 536 | ** that allows an application to run multiple statements of SQL |
| 534 | 537 | ** without having to use a lot of C code. |
| | @@ -1582,10 +1585,11 @@ |
| 1582 | 1585 | */ |
| 1583 | 1586 | SQLITE_API int SQLITE_CDECL sqlite3_config(int, ...); |
| 1584 | 1587 | |
| 1585 | 1588 | /* |
| 1586 | 1589 | ** CAPI3REF: Configure database connections |
| 1590 | +** METHOD: sqlite3 |
| 1587 | 1591 | ** |
| 1588 | 1592 | ** The sqlite3_db_config() interface is used to make configuration |
| 1589 | 1593 | ** changes to a [database connection]. The interface is similar to |
| 1590 | 1594 | ** [sqlite3_config()] except that the changes apply to a single |
| 1591 | 1595 | ** [database connection] (specified in the first argument). |
| | @@ -2079,19 +2083,21 @@ |
| 2079 | 2083 | #define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */ |
| 2080 | 2084 | |
| 2081 | 2085 | |
| 2082 | 2086 | /* |
| 2083 | 2087 | ** CAPI3REF: Enable Or Disable Extended Result Codes |
| 2088 | +** METHOD: sqlite3 |
| 2084 | 2089 | ** |
| 2085 | 2090 | ** ^The sqlite3_extended_result_codes() routine enables or disables the |
| 2086 | 2091 | ** [extended result codes] feature of SQLite. ^The extended result |
| 2087 | 2092 | ** codes are disabled by default for historical compatibility. |
| 2088 | 2093 | */ |
| 2089 | 2094 | SQLITE_API int SQLITE_STDCALL sqlite3_extended_result_codes(sqlite3*, int onoff); |
| 2090 | 2095 | |
| 2091 | 2096 | /* |
| 2092 | 2097 | ** CAPI3REF: Last Insert Rowid |
| 2098 | +** METHOD: sqlite3 |
| 2093 | 2099 | ** |
| 2094 | 2100 | ** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables) |
| 2095 | 2101 | ** has a unique 64-bit signed |
| 2096 | 2102 | ** integer key called the [ROWID | "rowid"]. ^The rowid is always available |
| 2097 | 2103 | ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those |
| | @@ -2139,10 +2145,11 @@ |
| 2139 | 2145 | */ |
| 2140 | 2146 | SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_last_insert_rowid(sqlite3*); |
| 2141 | 2147 | |
| 2142 | 2148 | /* |
| 2143 | 2149 | ** CAPI3REF: Count The Number Of Rows Modified |
| 2150 | +** METHOD: sqlite3 |
| 2144 | 2151 | ** |
| 2145 | 2152 | ** ^This function returns the number of rows modified, inserted or |
| 2146 | 2153 | ** deleted by the most recently completed INSERT, UPDATE or DELETE |
| 2147 | 2154 | ** statement on the database connection specified by the only parameter. |
| 2148 | 2155 | ** ^Executing any other type of SQL statement does not modify the value |
| | @@ -2191,10 +2198,11 @@ |
| 2191 | 2198 | */ |
| 2192 | 2199 | SQLITE_API int SQLITE_STDCALL sqlite3_changes(sqlite3*); |
| 2193 | 2200 | |
| 2194 | 2201 | /* |
| 2195 | 2202 | ** CAPI3REF: Total Number Of Rows Modified |
| 2203 | +** METHOD: sqlite3 |
| 2196 | 2204 | ** |
| 2197 | 2205 | ** ^This function returns the total number of rows inserted, modified or |
| 2198 | 2206 | ** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed |
| 2199 | 2207 | ** since the database connection was opened, including those executed as |
| 2200 | 2208 | ** part of trigger programs. ^Executing any other type of SQL statement |
| | @@ -2214,10 +2222,11 @@ |
| 2214 | 2222 | */ |
| 2215 | 2223 | SQLITE_API int SQLITE_STDCALL sqlite3_total_changes(sqlite3*); |
| 2216 | 2224 | |
| 2217 | 2225 | /* |
| 2218 | 2226 | ** CAPI3REF: Interrupt A Long-Running Query |
| 2227 | +** METHOD: sqlite3 |
| 2219 | 2228 | ** |
| 2220 | 2229 | ** ^This function causes any pending database operation to abort and |
| 2221 | 2230 | ** return at its earliest opportunity. This routine is typically |
| 2222 | 2231 | ** called in response to a user action such as pressing "Cancel" |
| 2223 | 2232 | ** or Ctrl-C where the user wants a long query operation to halt |
| | @@ -2290,10 +2299,11 @@ |
| 2290 | 2299 | SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *sql); |
| 2291 | 2300 | |
| 2292 | 2301 | /* |
| 2293 | 2302 | ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors |
| 2294 | 2303 | ** KEYWORDS: {busy-handler callback} {busy handler} |
| 2304 | +** METHOD: sqlite3 |
| 2295 | 2305 | ** |
| 2296 | 2306 | ** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X |
| 2297 | 2307 | ** that might be invoked with argument P whenever |
| 2298 | 2308 | ** an attempt is made to access a database table associated with |
| 2299 | 2309 | ** [database connection] D when another thread |
| | @@ -2349,10 +2359,11 @@ |
| 2349 | 2359 | */ |
| 2350 | 2360 | SQLITE_API int SQLITE_STDCALL sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); |
| 2351 | 2361 | |
| 2352 | 2362 | /* |
| 2353 | 2363 | ** CAPI3REF: Set A Busy Timeout |
| 2364 | +** METHOD: sqlite3 |
| 2354 | 2365 | ** |
| 2355 | 2366 | ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps |
| 2356 | 2367 | ** for a specified amount of time when a table is locked. ^The handler |
| 2357 | 2368 | ** will sleep multiple times until at least "ms" milliseconds of sleeping |
| 2358 | 2369 | ** have accumulated. ^After at least "ms" milliseconds of sleeping, |
| | @@ -2371,10 +2382,11 @@ |
| 2371 | 2382 | */ |
| 2372 | 2383 | SQLITE_API int SQLITE_STDCALL sqlite3_busy_timeout(sqlite3*, int ms); |
| 2373 | 2384 | |
| 2374 | 2385 | /* |
| 2375 | 2386 | ** CAPI3REF: Convenience Routines For Running Queries |
| 2387 | +** METHOD: sqlite3 |
| 2376 | 2388 | ** |
| 2377 | 2389 | ** This is a legacy interface that is preserved for backwards compatibility. |
| 2378 | 2390 | ** Use of this interface is not recommended. |
| 2379 | 2391 | ** |
| 2380 | 2392 | ** Definition: A <b>result table</b> is memory data structure created by the |
| | @@ -2706,10 +2718,11 @@ |
| 2706 | 2718 | */ |
| 2707 | 2719 | SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *P); |
| 2708 | 2720 | |
| 2709 | 2721 | /* |
| 2710 | 2722 | ** CAPI3REF: Compile-Time Authorization Callbacks |
| 2723 | +** METHOD: sqlite3 |
| 2711 | 2724 | ** |
| 2712 | 2725 | ** ^This routine registers an authorizer callback with a particular |
| 2713 | 2726 | ** [database connection], supplied in the first argument. |
| 2714 | 2727 | ** ^The authorizer callback is invoked as SQL statements are being compiled |
| 2715 | 2728 | ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], |
| | @@ -2862,10 +2875,11 @@ |
| 2862 | 2875 | #define SQLITE_COPY 0 /* No longer used */ |
| 2863 | 2876 | #define SQLITE_RECURSIVE 33 /* NULL NULL */ |
| 2864 | 2877 | |
| 2865 | 2878 | /* |
| 2866 | 2879 | ** CAPI3REF: Tracing And Profiling Functions |
| 2880 | +** METHOD: sqlite3 |
| 2867 | 2881 | ** |
| 2868 | 2882 | ** These routines register callback functions that can be used for |
| 2869 | 2883 | ** tracing and profiling the execution of SQL statements. |
| 2870 | 2884 | ** |
| 2871 | 2885 | ** ^The callback function registered by sqlite3_trace() is invoked at |
| | @@ -2894,10 +2908,11 @@ |
| 2894 | 2908 | SQLITE_API SQLITE_EXPERIMENTAL void *SQLITE_STDCALL sqlite3_profile(sqlite3*, |
| 2895 | 2909 | void(*xProfile)(void*,const char*,sqlite3_uint64), void*); |
| 2896 | 2910 | |
| 2897 | 2911 | /* |
| 2898 | 2912 | ** CAPI3REF: Query Progress Callbacks |
| 2913 | +** METHOD: sqlite3 |
| 2899 | 2914 | ** |
| 2900 | 2915 | ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback |
| 2901 | 2916 | ** function X to be invoked periodically during long running calls to |
| 2902 | 2917 | ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for |
| 2903 | 2918 | ** database connection D. An example use for this |
| | @@ -2927,10 +2942,11 @@ |
| 2927 | 2942 | */ |
| 2928 | 2943 | SQLITE_API void SQLITE_STDCALL sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); |
| 2929 | 2944 | |
| 2930 | 2945 | /* |
| 2931 | 2946 | ** CAPI3REF: Opening A New Database Connection |
| 2947 | +** CONSTRUCTOR: sqlite3 |
| 2932 | 2948 | ** |
| 2933 | 2949 | ** ^These routines open an SQLite database file as specified by the |
| 2934 | 2950 | ** filename argument. ^The filename argument is interpreted as UTF-8 for |
| 2935 | 2951 | ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte |
| 2936 | 2952 | ** order for sqlite3_open16(). ^(A [database connection] handle is usually |
| | @@ -3212,10 +3228,11 @@ |
| 3212 | 3228 | SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_uri_int64(const char*, const char*, sqlite3_int64); |
| 3213 | 3229 | |
| 3214 | 3230 | |
| 3215 | 3231 | /* |
| 3216 | 3232 | ** CAPI3REF: Error Codes And Messages |
| 3233 | +** METHOD: sqlite3 |
| 3217 | 3234 | ** |
| 3218 | 3235 | ** ^If the most recent sqlite3_* API call associated with |
| 3219 | 3236 | ** [database connection] D failed, then the sqlite3_errcode(D) interface |
| 3220 | 3237 | ** returns the numeric [result code] or [extended result code] for that |
| 3221 | 3238 | ** API call. |
| | @@ -3257,37 +3274,38 @@ |
| 3257 | 3274 | SQLITE_API const char *SQLITE_STDCALL sqlite3_errmsg(sqlite3*); |
| 3258 | 3275 | SQLITE_API const void *SQLITE_STDCALL sqlite3_errmsg16(sqlite3*); |
| 3259 | 3276 | SQLITE_API const char *SQLITE_STDCALL sqlite3_errstr(int); |
| 3260 | 3277 | |
| 3261 | 3278 | /* |
| 3262 | | -** CAPI3REF: SQL Statement Object |
| 3279 | +** CAPI3REF: Prepared Statement Object |
| 3263 | 3280 | ** KEYWORDS: {prepared statement} {prepared statements} |
| 3264 | 3281 | ** |
| 3265 | | -** An instance of this object represents a single SQL statement. |
| 3266 | | -** This object is variously known as a "prepared statement" or a |
| 3267 | | -** "compiled SQL statement" or simply as a "statement". |
| 3282 | +** An instance of this object represents a single SQL statement that |
| 3283 | +** has been compiled into binary form and is ready to be evaluated. |
| 3268 | 3284 | ** |
| 3269 | | -** The life of a statement object goes something like this: |
| 3285 | +** Think of each SQL statement as a separate computer program. The |
| 3286 | +** original SQL text is source code. A prepared statement object |
| 3287 | +** is the compiled object code. All SQL must be converted into a |
| 3288 | +** prepared statement before it can be run. |
| 3289 | +** |
| 3290 | +** The life-cycle of a prepared statement object usually goes like this: |
| 3270 | 3291 | ** |
| 3271 | 3292 | ** <ol> |
| 3272 | | -** <li> Create the object using [sqlite3_prepare_v2()] or a related |
| 3273 | | -** function. |
| 3274 | | -** <li> Bind values to [host parameters] using the sqlite3_bind_*() |
| 3293 | +** <li> Create the prepared statement object using [sqlite3_prepare_v2()]. |
| 3294 | +** <li> Bind values to [parameters] using the sqlite3_bind_*() |
| 3275 | 3295 | ** interfaces. |
| 3276 | 3296 | ** <li> Run the SQL by calling [sqlite3_step()] one or more times. |
| 3277 | | -** <li> Reset the statement using [sqlite3_reset()] then go back |
| 3297 | +** <li> Reset the prepared statement using [sqlite3_reset()] then go back |
| 3278 | 3298 | ** to step 2. Do this zero or more times. |
| 3279 | 3299 | ** <li> Destroy the object using [sqlite3_finalize()]. |
| 3280 | 3300 | ** </ol> |
| 3281 | | -** |
| 3282 | | -** Refer to documentation on individual methods above for additional |
| 3283 | | -** information. |
| 3284 | 3301 | */ |
| 3285 | 3302 | typedef struct sqlite3_stmt sqlite3_stmt; |
| 3286 | 3303 | |
| 3287 | 3304 | /* |
| 3288 | 3305 | ** CAPI3REF: Run-time Limits |
| 3306 | +** METHOD: sqlite3 |
| 3289 | 3307 | ** |
| 3290 | 3308 | ** ^(This interface allows the size of various constructs to be limited |
| 3291 | 3309 | ** on a connection by connection basis. The first parameter is the |
| 3292 | 3310 | ** [database connection] whose limit is to be set or queried. The |
| 3293 | 3311 | ** second parameter is one of the [limit categories] that define a |
| | @@ -3395,10 +3413,12 @@ |
| 3395 | 3413 | #define SQLITE_LIMIT_WORKER_THREADS 11 |
| 3396 | 3414 | |
| 3397 | 3415 | /* |
| 3398 | 3416 | ** CAPI3REF: Compiling An SQL Statement |
| 3399 | 3417 | ** KEYWORDS: {SQL statement compiler} |
| 3418 | +** METHOD: sqlite3 |
| 3419 | +** CONSTRUCTOR: sqlite3_stmt |
| 3400 | 3420 | ** |
| 3401 | 3421 | ** To execute an SQL query, it must first be compiled into a byte-code |
| 3402 | 3422 | ** program using one of these routines. |
| 3403 | 3423 | ** |
| 3404 | 3424 | ** The first argument, "db", is a [database connection] obtained from a |
| | @@ -3502,19 +3522,21 @@ |
| 3502 | 3522 | const void **pzTail /* OUT: Pointer to unused portion of zSql */ |
| 3503 | 3523 | ); |
| 3504 | 3524 | |
| 3505 | 3525 | /* |
| 3506 | 3526 | ** CAPI3REF: Retrieving Statement SQL |
| 3527 | +** METHOD: sqlite3_stmt |
| 3507 | 3528 | ** |
| 3508 | 3529 | ** ^This interface can be used to retrieve a saved copy of the original |
| 3509 | 3530 | ** SQL text used to create a [prepared statement] if that statement was |
| 3510 | 3531 | ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()]. |
| 3511 | 3532 | */ |
| 3512 | 3533 | SQLITE_API const char *SQLITE_STDCALL sqlite3_sql(sqlite3_stmt *pStmt); |
| 3513 | 3534 | |
| 3514 | 3535 | /* |
| 3515 | 3536 | ** CAPI3REF: Determine If An SQL Statement Writes The Database |
| 3537 | +** METHOD: sqlite3_stmt |
| 3516 | 3538 | ** |
| 3517 | 3539 | ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if |
| 3518 | 3540 | ** and only if the [prepared statement] X makes no direct changes to |
| 3519 | 3541 | ** the content of the database file. |
| 3520 | 3542 | ** |
| | @@ -3542,10 +3564,11 @@ |
| 3542 | 3564 | */ |
| 3543 | 3565 | SQLITE_API int SQLITE_STDCALL sqlite3_stmt_readonly(sqlite3_stmt *pStmt); |
| 3544 | 3566 | |
| 3545 | 3567 | /* |
| 3546 | 3568 | ** CAPI3REF: Determine If A Prepared Statement Has Been Reset |
| 3569 | +** METHOD: sqlite3_stmt |
| 3547 | 3570 | ** |
| 3548 | 3571 | ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the |
| 3549 | 3572 | ** [prepared statement] S has been stepped at least once using |
| 3550 | 3573 | ** [sqlite3_step(S)] but has not run to completion and/or has not |
| 3551 | 3574 | ** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S) |
| | @@ -3616,10 +3639,11 @@ |
| 3616 | 3639 | |
| 3617 | 3640 | /* |
| 3618 | 3641 | ** CAPI3REF: Binding Values To Prepared Statements |
| 3619 | 3642 | ** KEYWORDS: {host parameter} {host parameters} {host parameter name} |
| 3620 | 3643 | ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding} |
| 3644 | +** METHOD: sqlite3_stmt |
| 3621 | 3645 | ** |
| 3622 | 3646 | ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants, |
| 3623 | 3647 | ** literals may be replaced by a [parameter] that matches one of following |
| 3624 | 3648 | ** templates: |
| 3625 | 3649 | ** |
| | @@ -3734,10 +3758,11 @@ |
| 3734 | 3758 | SQLITE_API int SQLITE_STDCALL sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); |
| 3735 | 3759 | SQLITE_API int SQLITE_STDCALL sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); |
| 3736 | 3760 | |
| 3737 | 3761 | /* |
| 3738 | 3762 | ** CAPI3REF: Number Of SQL Parameters |
| 3763 | +** METHOD: sqlite3_stmt |
| 3739 | 3764 | ** |
| 3740 | 3765 | ** ^This routine can be used to find the number of [SQL parameters] |
| 3741 | 3766 | ** in a [prepared statement]. SQL parameters are tokens of the |
| 3742 | 3767 | ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as |
| 3743 | 3768 | ** placeholders for values that are [sqlite3_bind_blob | bound] |
| | @@ -3754,10 +3779,11 @@ |
| 3754 | 3779 | */ |
| 3755 | 3780 | SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_count(sqlite3_stmt*); |
| 3756 | 3781 | |
| 3757 | 3782 | /* |
| 3758 | 3783 | ** CAPI3REF: Name Of A Host Parameter |
| 3784 | +** METHOD: sqlite3_stmt |
| 3759 | 3785 | ** |
| 3760 | 3786 | ** ^The sqlite3_bind_parameter_name(P,N) interface returns |
| 3761 | 3787 | ** the name of the N-th [SQL parameter] in the [prepared statement] P. |
| 3762 | 3788 | ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA" |
| 3763 | 3789 | ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA" |
| | @@ -3781,10 +3807,11 @@ |
| 3781 | 3807 | */ |
| 3782 | 3808 | SQLITE_API const char *SQLITE_STDCALL sqlite3_bind_parameter_name(sqlite3_stmt*, int); |
| 3783 | 3809 | |
| 3784 | 3810 | /* |
| 3785 | 3811 | ** CAPI3REF: Index Of A Parameter With A Given Name |
| 3812 | +** METHOD: sqlite3_stmt |
| 3786 | 3813 | ** |
| 3787 | 3814 | ** ^Return the index of an SQL parameter given its name. ^The |
| 3788 | 3815 | ** index value returned is suitable for use as the second |
| 3789 | 3816 | ** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero |
| 3790 | 3817 | ** is returned if no matching parameter is found. ^The parameter |
| | @@ -3797,19 +3824,21 @@ |
| 3797 | 3824 | */ |
| 3798 | 3825 | SQLITE_API int SQLITE_STDCALL sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); |
| 3799 | 3826 | |
| 3800 | 3827 | /* |
| 3801 | 3828 | ** CAPI3REF: Reset All Bindings On A Prepared Statement |
| 3829 | +** METHOD: sqlite3_stmt |
| 3802 | 3830 | ** |
| 3803 | 3831 | ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset |
| 3804 | 3832 | ** the [sqlite3_bind_blob | bindings] on a [prepared statement]. |
| 3805 | 3833 | ** ^Use this routine to reset all host parameters to NULL. |
| 3806 | 3834 | */ |
| 3807 | 3835 | SQLITE_API int SQLITE_STDCALL sqlite3_clear_bindings(sqlite3_stmt*); |
| 3808 | 3836 | |
| 3809 | 3837 | /* |
| 3810 | 3838 | ** CAPI3REF: Number Of Columns In A Result Set |
| 3839 | +** METHOD: sqlite3_stmt |
| 3811 | 3840 | ** |
| 3812 | 3841 | ** ^Return the number of columns in the result set returned by the |
| 3813 | 3842 | ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL |
| 3814 | 3843 | ** statement that does not return data (for example an [UPDATE]). |
| 3815 | 3844 | ** |
| | @@ -3817,10 +3846,11 @@ |
| 3817 | 3846 | */ |
| 3818 | 3847 | SQLITE_API int SQLITE_STDCALL sqlite3_column_count(sqlite3_stmt *pStmt); |
| 3819 | 3848 | |
| 3820 | 3849 | /* |
| 3821 | 3850 | ** CAPI3REF: Column Names In A Result Set |
| 3851 | +** METHOD: sqlite3_stmt |
| 3822 | 3852 | ** |
| 3823 | 3853 | ** ^These routines return the name assigned to a particular column |
| 3824 | 3854 | ** in the result set of a [SELECT] statement. ^The sqlite3_column_name() |
| 3825 | 3855 | ** interface returns a pointer to a zero-terminated UTF-8 string |
| 3826 | 3856 | ** and sqlite3_column_name16() returns a pointer to a zero-terminated |
| | @@ -3846,10 +3876,11 @@ |
| 3846 | 3876 | SQLITE_API const char *SQLITE_STDCALL sqlite3_column_name(sqlite3_stmt*, int N); |
| 3847 | 3877 | SQLITE_API const void *SQLITE_STDCALL sqlite3_column_name16(sqlite3_stmt*, int N); |
| 3848 | 3878 | |
| 3849 | 3879 | /* |
| 3850 | 3880 | ** CAPI3REF: Source Of Data In A Query Result |
| 3881 | +** METHOD: sqlite3_stmt |
| 3851 | 3882 | ** |
| 3852 | 3883 | ** ^These routines provide a means to determine the database, table, and |
| 3853 | 3884 | ** table column that is the origin of a particular result column in |
| 3854 | 3885 | ** [SELECT] statement. |
| 3855 | 3886 | ** ^The name of the database or table or column can be returned as |
| | @@ -3898,10 +3929,11 @@ |
| 3898 | 3929 | SQLITE_API const char *SQLITE_STDCALL sqlite3_column_origin_name(sqlite3_stmt*,int); |
| 3899 | 3930 | SQLITE_API const void *SQLITE_STDCALL sqlite3_column_origin_name16(sqlite3_stmt*,int); |
| 3900 | 3931 | |
| 3901 | 3932 | /* |
| 3902 | 3933 | ** CAPI3REF: Declared Datatype Of A Query Result |
| 3934 | +** METHOD: sqlite3_stmt |
| 3903 | 3935 | ** |
| 3904 | 3936 | ** ^(The first parameter is a [prepared statement]. |
| 3905 | 3937 | ** If this statement is a [SELECT] statement and the Nth column of the |
| 3906 | 3938 | ** returned result set of that [SELECT] is a table column (not an |
| 3907 | 3939 | ** expression or subquery) then the declared type of the table |
| | @@ -3930,10 +3962,11 @@ |
| 3930 | 3962 | SQLITE_API const char *SQLITE_STDCALL sqlite3_column_decltype(sqlite3_stmt*,int); |
| 3931 | 3963 | SQLITE_API const void *SQLITE_STDCALL sqlite3_column_decltype16(sqlite3_stmt*,int); |
| 3932 | 3964 | |
| 3933 | 3965 | /* |
| 3934 | 3966 | ** CAPI3REF: Evaluate An SQL Statement |
| 3967 | +** METHOD: sqlite3_stmt |
| 3935 | 3968 | ** |
| 3936 | 3969 | ** After a [prepared statement] has been prepared using either |
| 3937 | 3970 | ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy |
| 3938 | 3971 | ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function |
| 3939 | 3972 | ** must be called one or more times to evaluate the statement. |
| | @@ -4009,10 +4042,11 @@ |
| 4009 | 4042 | */ |
| 4010 | 4043 | SQLITE_API int SQLITE_STDCALL sqlite3_step(sqlite3_stmt*); |
| 4011 | 4044 | |
| 4012 | 4045 | /* |
| 4013 | 4046 | ** CAPI3REF: Number of columns in a result set |
| 4047 | +** METHOD: sqlite3_stmt |
| 4014 | 4048 | ** |
| 4015 | 4049 | ** ^The sqlite3_data_count(P) interface returns the number of columns in the |
| 4016 | 4050 | ** current row of the result set of [prepared statement] P. |
| 4017 | 4051 | ** ^If prepared statement P does not have results ready to return |
| 4018 | 4052 | ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of |
| | @@ -4062,10 +4096,11 @@ |
| 4062 | 4096 | #define SQLITE3_TEXT 3 |
| 4063 | 4097 | |
| 4064 | 4098 | /* |
| 4065 | 4099 | ** CAPI3REF: Result Values From A Query |
| 4066 | 4100 | ** KEYWORDS: {column access functions} |
| 4101 | +** METHOD: sqlite3_stmt |
| 4067 | 4102 | ** |
| 4068 | 4103 | ** These routines form the "result set" interface. |
| 4069 | 4104 | ** |
| 4070 | 4105 | ** ^These routines return information about a single column of the current |
| 4071 | 4106 | ** result row of a query. ^In every case the first argument is a pointer |
| | @@ -4234,10 +4269,11 @@ |
| 4234 | 4269 | SQLITE_API int SQLITE_STDCALL sqlite3_column_type(sqlite3_stmt*, int iCol); |
| 4235 | 4270 | SQLITE_API sqlite3_value *SQLITE_STDCALL sqlite3_column_value(sqlite3_stmt*, int iCol); |
| 4236 | 4271 | |
| 4237 | 4272 | /* |
| 4238 | 4273 | ** CAPI3REF: Destroy A Prepared Statement Object |
| 4274 | +** DESTRUCTOR: sqlite3_stmt |
| 4239 | 4275 | ** |
| 4240 | 4276 | ** ^The sqlite3_finalize() function is called to delete a [prepared statement]. |
| 4241 | 4277 | ** ^If the most recent evaluation of the statement encountered no errors |
| 4242 | 4278 | ** or if the statement is never been evaluated, then sqlite3_finalize() returns |
| 4243 | 4279 | ** SQLITE_OK. ^If the most recent evaluation of statement S failed, then |
| | @@ -4261,10 +4297,11 @@ |
| 4261 | 4297 | */ |
| 4262 | 4298 | SQLITE_API int SQLITE_STDCALL sqlite3_finalize(sqlite3_stmt *pStmt); |
| 4263 | 4299 | |
| 4264 | 4300 | /* |
| 4265 | 4301 | ** CAPI3REF: Reset A Prepared Statement Object |
| 4302 | +** METHOD: sqlite3_stmt |
| 4266 | 4303 | ** |
| 4267 | 4304 | ** The sqlite3_reset() function is called to reset a [prepared statement] |
| 4268 | 4305 | ** object back to its initial state, ready to be re-executed. |
| 4269 | 4306 | ** ^Any SQL statement variables that had values bound to them using |
| 4270 | 4307 | ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. |
| | @@ -4290,10 +4327,11 @@ |
| 4290 | 4327 | /* |
| 4291 | 4328 | ** CAPI3REF: Create Or Redefine SQL Functions |
| 4292 | 4329 | ** KEYWORDS: {function creation routines} |
| 4293 | 4330 | ** KEYWORDS: {application-defined SQL function} |
| 4294 | 4331 | ** KEYWORDS: {application-defined SQL functions} |
| 4332 | +** METHOD: sqlite3 |
| 4295 | 4333 | ** |
| 4296 | 4334 | ** ^These functions (collectively known as "function creation routines") |
| 4297 | 4335 | ** are used to add SQL functions or aggregates or to redefine the behavior |
| 4298 | 4336 | ** of existing SQL functions or aggregates. The only differences between |
| 4299 | 4337 | ** these routines are the text encoding expected for |
| | @@ -4459,10 +4497,11 @@ |
| 4459 | 4497 | void*,sqlite3_int64); |
| 4460 | 4498 | #endif |
| 4461 | 4499 | |
| 4462 | 4500 | /* |
| 4463 | 4501 | ** CAPI3REF: Obtaining SQL Function Parameter Values |
| 4502 | +** METHOD: sqlite3_value |
| 4464 | 4503 | ** |
| 4465 | 4504 | ** The C-language implementation of SQL functions and aggregates uses |
| 4466 | 4505 | ** this set of interface routines to access the parameter values on |
| 4467 | 4506 | ** the function or aggregate. |
| 4468 | 4507 | ** |
| | @@ -4517,10 +4556,11 @@ |
| 4517 | 4556 | SQLITE_API int SQLITE_STDCALL sqlite3_value_type(sqlite3_value*); |
| 4518 | 4557 | SQLITE_API int SQLITE_STDCALL sqlite3_value_numeric_type(sqlite3_value*); |
| 4519 | 4558 | |
| 4520 | 4559 | /* |
| 4521 | 4560 | ** CAPI3REF: Obtain Aggregate Function Context |
| 4561 | +** METHOD: sqlite3_context |
| 4522 | 4562 | ** |
| 4523 | 4563 | ** Implementations of aggregate SQL functions use this |
| 4524 | 4564 | ** routine to allocate memory for storing their state. |
| 4525 | 4565 | ** |
| 4526 | 4566 | ** ^The first time the sqlite3_aggregate_context(C,N) routine is called |
| | @@ -4561,10 +4601,11 @@ |
| 4561 | 4601 | */ |
| 4562 | 4602 | SQLITE_API void *SQLITE_STDCALL sqlite3_aggregate_context(sqlite3_context*, int nBytes); |
| 4563 | 4603 | |
| 4564 | 4604 | /* |
| 4565 | 4605 | ** CAPI3REF: User Data For Functions |
| 4606 | +** METHOD: sqlite3_context |
| 4566 | 4607 | ** |
| 4567 | 4608 | ** ^The sqlite3_user_data() interface returns a copy of |
| 4568 | 4609 | ** the pointer that was the pUserData parameter (the 5th parameter) |
| 4569 | 4610 | ** of the [sqlite3_create_function()] |
| 4570 | 4611 | ** and [sqlite3_create_function16()] routines that originally |
| | @@ -4575,10 +4616,11 @@ |
| 4575 | 4616 | */ |
| 4576 | 4617 | SQLITE_API void *SQLITE_STDCALL sqlite3_user_data(sqlite3_context*); |
| 4577 | 4618 | |
| 4578 | 4619 | /* |
| 4579 | 4620 | ** CAPI3REF: Database Connection For Functions |
| 4621 | +** METHOD: sqlite3_context |
| 4580 | 4622 | ** |
| 4581 | 4623 | ** ^The sqlite3_context_db_handle() interface returns a copy of |
| 4582 | 4624 | ** the pointer to the [database connection] (the 1st parameter) |
| 4583 | 4625 | ** of the [sqlite3_create_function()] |
| 4584 | 4626 | ** and [sqlite3_create_function16()] routines that originally |
| | @@ -4586,10 +4628,11 @@ |
| 4586 | 4628 | */ |
| 4587 | 4629 | SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_context_db_handle(sqlite3_context*); |
| 4588 | 4630 | |
| 4589 | 4631 | /* |
| 4590 | 4632 | ** CAPI3REF: Function Auxiliary Data |
| 4633 | +** METHOD: sqlite3_context |
| 4591 | 4634 | ** |
| 4592 | 4635 | ** These functions may be used by (non-aggregate) SQL functions to |
| 4593 | 4636 | ** associate metadata with argument values. If the same value is passed to |
| 4594 | 4637 | ** multiple invocations of the same SQL function during query execution, under |
| 4595 | 4638 | ** some circumstances the associated metadata may be preserved. An example |
| | @@ -4658,10 +4701,11 @@ |
| 4658 | 4701 | #define SQLITE_STATIC ((sqlite3_destructor_type)0) |
| 4659 | 4702 | #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) |
| 4660 | 4703 | |
| 4661 | 4704 | /* |
| 4662 | 4705 | ** CAPI3REF: Setting The Result Of An SQL Function |
| 4706 | +** METHOD: sqlite3_context |
| 4663 | 4707 | ** |
| 4664 | 4708 | ** These routines are used by the xFunc or xFinal callbacks that |
| 4665 | 4709 | ** implement SQL functions and aggregates. See |
| 4666 | 4710 | ** [sqlite3_create_function()] and [sqlite3_create_function16()] |
| 4667 | 4711 | ** for additional information. |
| | @@ -4793,10 +4837,11 @@ |
| 4793 | 4837 | SQLITE_API void SQLITE_STDCALL sqlite3_result_value(sqlite3_context*, sqlite3_value*); |
| 4794 | 4838 | SQLITE_API void SQLITE_STDCALL sqlite3_result_zeroblob(sqlite3_context*, int n); |
| 4795 | 4839 | |
| 4796 | 4840 | /* |
| 4797 | 4841 | ** CAPI3REF: Define New Collating Sequences |
| 4842 | +** METHOD: sqlite3 |
| 4798 | 4843 | ** |
| 4799 | 4844 | ** ^These functions add, remove, or modify a [collation] associated |
| 4800 | 4845 | ** with the [database connection] specified as the first argument. |
| 4801 | 4846 | ** |
| 4802 | 4847 | ** ^The name of the collation is a UTF-8 string |
| | @@ -4895,10 +4940,11 @@ |
| 4895 | 4940 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 4896 | 4941 | ); |
| 4897 | 4942 | |
| 4898 | 4943 | /* |
| 4899 | 4944 | ** CAPI3REF: Collation Needed Callbacks |
| 4945 | +** METHOD: sqlite3 |
| 4900 | 4946 | ** |
| 4901 | 4947 | ** ^To avoid having to register all collation sequences before a database |
| 4902 | 4948 | ** can be used, a single callback function may be registered with the |
| 4903 | 4949 | ** [database connection] to be invoked whenever an undefined collation |
| 4904 | 4950 | ** sequence is required. |
| | @@ -5102,10 +5148,11 @@ |
| 5102 | 5148 | SQLITE_API char *sqlite3_data_directory; |
| 5103 | 5149 | |
| 5104 | 5150 | /* |
| 5105 | 5151 | ** CAPI3REF: Test For Auto-Commit Mode |
| 5106 | 5152 | ** KEYWORDS: {autocommit mode} |
| 5153 | +** METHOD: sqlite3 |
| 5107 | 5154 | ** |
| 5108 | 5155 | ** ^The sqlite3_get_autocommit() interface returns non-zero or |
| 5109 | 5156 | ** zero if the given database connection is or is not in autocommit mode, |
| 5110 | 5157 | ** respectively. ^Autocommit mode is on by default. |
| 5111 | 5158 | ** ^Autocommit mode is disabled by a [BEGIN] statement. |
| | @@ -5124,10 +5171,11 @@ |
| 5124 | 5171 | */ |
| 5125 | 5172 | SQLITE_API int SQLITE_STDCALL sqlite3_get_autocommit(sqlite3*); |
| 5126 | 5173 | |
| 5127 | 5174 | /* |
| 5128 | 5175 | ** CAPI3REF: Find The Database Handle Of A Prepared Statement |
| 5176 | +** METHOD: sqlite3_stmt |
| 5129 | 5177 | ** |
| 5130 | 5178 | ** ^The sqlite3_db_handle interface returns the [database connection] handle |
| 5131 | 5179 | ** to which a [prepared statement] belongs. ^The [database connection] |
| 5132 | 5180 | ** returned by sqlite3_db_handle is the same [database connection] |
| 5133 | 5181 | ** that was the first argument |
| | @@ -5136,10 +5184,11 @@ |
| 5136 | 5184 | */ |
| 5137 | 5185 | SQLITE_API sqlite3 *SQLITE_STDCALL sqlite3_db_handle(sqlite3_stmt*); |
| 5138 | 5186 | |
| 5139 | 5187 | /* |
| 5140 | 5188 | ** CAPI3REF: Return The Filename For A Database Connection |
| 5189 | +** METHOD: sqlite3 |
| 5141 | 5190 | ** |
| 5142 | 5191 | ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename |
| 5143 | 5192 | ** associated with database N of connection D. ^The main database file |
| 5144 | 5193 | ** has the name "main". If there is no attached database N on the database |
| 5145 | 5194 | ** connection D, or if database N is a temporary or in-memory database, then |
| | @@ -5152,19 +5201,21 @@ |
| 5152 | 5201 | */ |
| 5153 | 5202 | SQLITE_API const char *SQLITE_STDCALL sqlite3_db_filename(sqlite3 *db, const char *zDbName); |
| 5154 | 5203 | |
| 5155 | 5204 | /* |
| 5156 | 5205 | ** CAPI3REF: Determine if a database is read-only |
| 5206 | +** METHOD: sqlite3 |
| 5157 | 5207 | ** |
| 5158 | 5208 | ** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N |
| 5159 | 5209 | ** of connection D is read-only, 0 if it is read/write, or -1 if N is not |
| 5160 | 5210 | ** the name of a database on connection D. |
| 5161 | 5211 | */ |
| 5162 | 5212 | SQLITE_API int SQLITE_STDCALL sqlite3_db_readonly(sqlite3 *db, const char *zDbName); |
| 5163 | 5213 | |
| 5164 | 5214 | /* |
| 5165 | 5215 | ** CAPI3REF: Find the next prepared statement |
| 5216 | +** METHOD: sqlite3 |
| 5166 | 5217 | ** |
| 5167 | 5218 | ** ^This interface returns a pointer to the next [prepared statement] after |
| 5168 | 5219 | ** pStmt associated with the [database connection] pDb. ^If pStmt is NULL |
| 5169 | 5220 | ** then this interface returns a pointer to the first prepared statement |
| 5170 | 5221 | ** associated with the database connection pDb. ^If no prepared statement |
| | @@ -5176,10 +5227,11 @@ |
| 5176 | 5227 | */ |
| 5177 | 5228 | SQLITE_API sqlite3_stmt *SQLITE_STDCALL sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt); |
| 5178 | 5229 | |
| 5179 | 5230 | /* |
| 5180 | 5231 | ** CAPI3REF: Commit And Rollback Notification Callbacks |
| 5232 | +** METHOD: sqlite3 |
| 5181 | 5233 | ** |
| 5182 | 5234 | ** ^The sqlite3_commit_hook() interface registers a callback |
| 5183 | 5235 | ** function to be invoked whenever a transaction is [COMMIT | committed]. |
| 5184 | 5236 | ** ^Any callback set by a previous call to sqlite3_commit_hook() |
| 5185 | 5237 | ** for the same database connection is overridden. |
| | @@ -5225,10 +5277,11 @@ |
| 5225 | 5277 | SQLITE_API void *SQLITE_STDCALL sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); |
| 5226 | 5278 | SQLITE_API void *SQLITE_STDCALL sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); |
| 5227 | 5279 | |
| 5228 | 5280 | /* |
| 5229 | 5281 | ** CAPI3REF: Data Change Notification Callbacks |
| 5282 | +** METHOD: sqlite3 |
| 5230 | 5283 | ** |
| 5231 | 5284 | ** ^The sqlite3_update_hook() interface registers a callback function |
| 5232 | 5285 | ** with the [database connection] identified by the first argument |
| 5233 | 5286 | ** to be invoked whenever a row is updated, inserted or deleted in |
| 5234 | 5287 | ** a rowid table. |
| | @@ -5331,10 +5384,11 @@ |
| 5331 | 5384 | */ |
| 5332 | 5385 | SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int); |
| 5333 | 5386 | |
| 5334 | 5387 | /* |
| 5335 | 5388 | ** CAPI3REF: Free Memory Used By A Database Connection |
| 5389 | +** METHOD: sqlite3 |
| 5336 | 5390 | ** |
| 5337 | 5391 | ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap |
| 5338 | 5392 | ** memory as possible from database connection D. Unlike the |
| 5339 | 5393 | ** [sqlite3_release_memory()] interface, this interface is in effect even |
| 5340 | 5394 | ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is |
| | @@ -5408,10 +5462,11 @@ |
| 5408 | 5462 | SQLITE_API SQLITE_DEPRECATED void SQLITE_STDCALL sqlite3_soft_heap_limit(int N); |
| 5409 | 5463 | |
| 5410 | 5464 | |
| 5411 | 5465 | /* |
| 5412 | 5466 | ** CAPI3REF: Extract Metadata About A Column Of A Table |
| 5467 | +** METHOD: sqlite3 |
| 5413 | 5468 | ** |
| 5414 | 5469 | ** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns |
| 5415 | 5470 | ** information about column C of table T in database D |
| 5416 | 5471 | ** on [database connection] X.)^ ^The sqlite3_table_column_metadata() |
| 5417 | 5472 | ** interface returns SQLITE_OK and fills in the non-NULL pointers in |
| | @@ -5486,10 +5541,11 @@ |
| 5486 | 5541 | int *pAutoinc /* OUTPUT: True if column is auto-increment */ |
| 5487 | 5542 | ); |
| 5488 | 5543 | |
| 5489 | 5544 | /* |
| 5490 | 5545 | ** CAPI3REF: Load An Extension |
| 5546 | +** METHOD: sqlite3 |
| 5491 | 5547 | ** |
| 5492 | 5548 | ** ^This interface loads an SQLite extension library from the named file. |
| 5493 | 5549 | ** |
| 5494 | 5550 | ** ^The sqlite3_load_extension() interface attempts to load an |
| 5495 | 5551 | ** [SQLite extension] library contained in the file zFile. If |
| | @@ -5527,10 +5583,11 @@ |
| 5527 | 5583 | char **pzErrMsg /* Put error message here if not 0 */ |
| 5528 | 5584 | ); |
| 5529 | 5585 | |
| 5530 | 5586 | /* |
| 5531 | 5587 | ** CAPI3REF: Enable Or Disable Extension Loading |
| 5588 | +** METHOD: sqlite3 |
| 5532 | 5589 | ** |
| 5533 | 5590 | ** ^So as not to open security holes in older applications that are |
| 5534 | 5591 | ** unprepared to deal with [extension loading], and as a means of disabling |
| 5535 | 5592 | ** [extension loading] while evaluating user-entered SQL, the following API |
| 5536 | 5593 | ** is provided to turn the [sqlite3_load_extension()] mechanism on and off. |
| | @@ -5776,10 +5833,11 @@ |
| 5776 | 5833 | #define SQLITE_INDEX_CONSTRAINT_GE 32 |
| 5777 | 5834 | #define SQLITE_INDEX_CONSTRAINT_MATCH 64 |
| 5778 | 5835 | |
| 5779 | 5836 | /* |
| 5780 | 5837 | ** CAPI3REF: Register A Virtual Table Implementation |
| 5838 | +** METHOD: sqlite3 |
| 5781 | 5839 | ** |
| 5782 | 5840 | ** ^These routines are used to register a new [virtual table module] name. |
| 5783 | 5841 | ** ^Module names must be registered before |
| 5784 | 5842 | ** creating a new [virtual table] using the module and before using a |
| 5785 | 5843 | ** preexisting [virtual table] for the module. |
| | @@ -5872,10 +5930,11 @@ |
| 5872 | 5930 | */ |
| 5873 | 5931 | SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3*, const char *zSQL); |
| 5874 | 5932 | |
| 5875 | 5933 | /* |
| 5876 | 5934 | ** CAPI3REF: Overload A Function For A Virtual Table |
| 5935 | +** METHOD: sqlite3 |
| 5877 | 5936 | ** |
| 5878 | 5937 | ** ^(Virtual tables can provide alternative implementations of functions |
| 5879 | 5938 | ** using the [xFindFunction] method of the [virtual table module]. |
| 5880 | 5939 | ** But global versions of those functions |
| 5881 | 5940 | ** must exist in order to be overloaded.)^ |
| | @@ -5914,10 +5973,12 @@ |
| 5914 | 5973 | */ |
| 5915 | 5974 | typedef struct sqlite3_blob sqlite3_blob; |
| 5916 | 5975 | |
| 5917 | 5976 | /* |
| 5918 | 5977 | ** CAPI3REF: Open A BLOB For Incremental I/O |
| 5978 | +** METHOD: sqlite3 |
| 5979 | +** CONSTRUCTOR: sqlite3_blob |
| 5919 | 5980 | ** |
| 5920 | 5981 | ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located |
| 5921 | 5982 | ** in row iRow, column zColumn, table zTable in database zDb; |
| 5922 | 5983 | ** in other words, the same BLOB that would be selected by: |
| 5923 | 5984 | ** |
| | @@ -5995,10 +6056,11 @@ |
| 5995 | 6056 | sqlite3_blob **ppBlob |
| 5996 | 6057 | ); |
| 5997 | 6058 | |
| 5998 | 6059 | /* |
| 5999 | 6060 | ** CAPI3REF: Move a BLOB Handle to a New Row |
| 6061 | +** METHOD: sqlite3_blob |
| 6000 | 6062 | ** |
| 6001 | 6063 | ** ^This function is used to move an existing blob handle so that it points |
| 6002 | 6064 | ** to a different row of the same database table. ^The new row is identified |
| 6003 | 6065 | ** by the rowid value passed as the second argument. Only the row can be |
| 6004 | 6066 | ** changed. ^The database, table and column on which the blob handle is open |
| | @@ -6019,10 +6081,11 @@ |
| 6019 | 6081 | */ |
| 6020 | 6082 | SQLITE_API SQLITE_EXPERIMENTAL int SQLITE_STDCALL sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); |
| 6021 | 6083 | |
| 6022 | 6084 | /* |
| 6023 | 6085 | ** CAPI3REF: Close A BLOB Handle |
| 6086 | +** DESTRUCTOR: sqlite3_blob |
| 6024 | 6087 | ** |
| 6025 | 6088 | ** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed |
| 6026 | 6089 | ** unconditionally. Even if this routine returns an error code, the |
| 6027 | 6090 | ** handle is still closed.)^ |
| 6028 | 6091 | ** |
| | @@ -6041,10 +6104,11 @@ |
| 6041 | 6104 | */ |
| 6042 | 6105 | SQLITE_API int SQLITE_STDCALL sqlite3_blob_close(sqlite3_blob *); |
| 6043 | 6106 | |
| 6044 | 6107 | /* |
| 6045 | 6108 | ** CAPI3REF: Return The Size Of An Open BLOB |
| 6109 | +** METHOD: sqlite3_blob |
| 6046 | 6110 | ** |
| 6047 | 6111 | ** ^Returns the size in bytes of the BLOB accessible via the |
| 6048 | 6112 | ** successfully opened [BLOB handle] in its only argument. ^The |
| 6049 | 6113 | ** incremental blob I/O routines can only read or overwriting existing |
| 6050 | 6114 | ** blob content; they cannot change the size of a blob. |
| | @@ -6056,10 +6120,11 @@ |
| 6056 | 6120 | */ |
| 6057 | 6121 | SQLITE_API int SQLITE_STDCALL sqlite3_blob_bytes(sqlite3_blob *); |
| 6058 | 6122 | |
| 6059 | 6123 | /* |
| 6060 | 6124 | ** CAPI3REF: Read Data From A BLOB Incrementally |
| 6125 | +** METHOD: sqlite3_blob |
| 6061 | 6126 | ** |
| 6062 | 6127 | ** ^(This function is used to read data from an open [BLOB handle] into a |
| 6063 | 6128 | ** caller-supplied buffer. N bytes of data are copied into buffer Z |
| 6064 | 6129 | ** from the open BLOB, starting at offset iOffset.)^ |
| 6065 | 6130 | ** |
| | @@ -6084,10 +6149,11 @@ |
| 6084 | 6149 | */ |
| 6085 | 6150 | SQLITE_API int SQLITE_STDCALL sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); |
| 6086 | 6151 | |
| 6087 | 6152 | /* |
| 6088 | 6153 | ** CAPI3REF: Write Data Into A BLOB Incrementally |
| 6154 | +** METHOD: sqlite3_blob |
| 6089 | 6155 | ** |
| 6090 | 6156 | ** ^(This function is used to write data into an open [BLOB handle] from a |
| 6091 | 6157 | ** caller-supplied buffer. N bytes of data are copied from the buffer Z |
| 6092 | 6158 | ** into the open BLOB, starting at offset iOffset.)^ |
| 6093 | 6159 | ** |
| | @@ -6411,10 +6477,11 @@ |
| 6411 | 6477 | #define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */ |
| 6412 | 6478 | #define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */ |
| 6413 | 6479 | |
| 6414 | 6480 | /* |
| 6415 | 6481 | ** CAPI3REF: Retrieve the mutex for a database connection |
| 6482 | +** METHOD: sqlite3 |
| 6416 | 6483 | ** |
| 6417 | 6484 | ** ^This interface returns a pointer the [sqlite3_mutex] object that |
| 6418 | 6485 | ** serializes access to the [database connection] given in the argument |
| 6419 | 6486 | ** when the [threading mode] is Serialized. |
| 6420 | 6487 | ** ^If the [threading mode] is Single-thread or Multi-thread then this |
| | @@ -6422,10 +6489,11 @@ |
| 6422 | 6489 | */ |
| 6423 | 6490 | SQLITE_API sqlite3_mutex *SQLITE_STDCALL sqlite3_db_mutex(sqlite3*); |
| 6424 | 6491 | |
| 6425 | 6492 | /* |
| 6426 | 6493 | ** CAPI3REF: Low-Level Control Of Database Files |
| 6494 | +** METHOD: sqlite3 |
| 6427 | 6495 | ** |
| 6428 | 6496 | ** ^The [sqlite3_file_control()] interface makes a direct call to the |
| 6429 | 6497 | ** xFileControl method for the [sqlite3_io_methods] object associated |
| 6430 | 6498 | ** with a particular database identified by the second argument. ^The |
| 6431 | 6499 | ** name of the database is "main" for the main database or "temp" for the |
| | @@ -6638,10 +6706,11 @@ |
| 6638 | 6706 | #define SQLITE_STATUS_SCRATCH_SIZE 8 |
| 6639 | 6707 | #define SQLITE_STATUS_MALLOC_COUNT 9 |
| 6640 | 6708 | |
| 6641 | 6709 | /* |
| 6642 | 6710 | ** CAPI3REF: Database Connection Status |
| 6711 | +** METHOD: sqlite3 |
| 6643 | 6712 | ** |
| 6644 | 6713 | ** ^This interface is used to retrieve runtime status information |
| 6645 | 6714 | ** about a single [database connection]. ^The first argument is the |
| 6646 | 6715 | ** database connection object to be interrogated. ^The second argument |
| 6647 | 6716 | ** is an integer constant, taken from the set of |
| | @@ -6766,10 +6835,11 @@ |
| 6766 | 6835 | #define SQLITE_DBSTATUS_MAX 10 /* Largest defined DBSTATUS */ |
| 6767 | 6836 | |
| 6768 | 6837 | |
| 6769 | 6838 | /* |
| 6770 | 6839 | ** CAPI3REF: Prepared Statement Status |
| 6840 | +** METHOD: sqlite3_stmt |
| 6771 | 6841 | ** |
| 6772 | 6842 | ** ^(Each prepared statement maintains various |
| 6773 | 6843 | ** [SQLITE_STMTSTATUS counters] that measure the number |
| 6774 | 6844 | ** of times it has performed specific operations.)^ These counters can |
| 6775 | 6845 | ** be used to monitor the performance characteristics of the prepared |
| | @@ -7269,10 +7339,11 @@ |
| 7269 | 7339 | SQLITE_API int SQLITE_STDCALL sqlite3_backup_remaining(sqlite3_backup *p); |
| 7270 | 7340 | SQLITE_API int SQLITE_STDCALL sqlite3_backup_pagecount(sqlite3_backup *p); |
| 7271 | 7341 | |
| 7272 | 7342 | /* |
| 7273 | 7343 | ** CAPI3REF: Unlock Notification |
| 7344 | +** METHOD: sqlite3 |
| 7274 | 7345 | ** |
| 7275 | 7346 | ** ^When running in shared-cache mode, a database operation may fail with |
| 7276 | 7347 | ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or |
| 7277 | 7348 | ** individual tables within the shared-cache cannot be obtained. See |
| 7278 | 7349 | ** [SQLite Shared-Cache Mode] for a description of shared-cache locking. |
| | @@ -7439,10 +7510,11 @@ |
| 7439 | 7510 | */ |
| 7440 | 7511 | SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...); |
| 7441 | 7512 | |
| 7442 | 7513 | /* |
| 7443 | 7514 | ** CAPI3REF: Write-Ahead Log Commit Hook |
| 7515 | +** METHOD: sqlite3 |
| 7444 | 7516 | ** |
| 7445 | 7517 | ** ^The [sqlite3_wal_hook()] function is used to register a callback that |
| 7446 | 7518 | ** is invoked each time data is committed to a database in wal mode. |
| 7447 | 7519 | ** |
| 7448 | 7520 | ** ^(The callback is invoked by SQLite after the commit has taken place and |
| | @@ -7478,10 +7550,11 @@ |
| 7478 | 7550 | void* |
| 7479 | 7551 | ); |
| 7480 | 7552 | |
| 7481 | 7553 | /* |
| 7482 | 7554 | ** CAPI3REF: Configure an auto-checkpoint |
| 7555 | +** METHOD: sqlite3 |
| 7483 | 7556 | ** |
| 7484 | 7557 | ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around |
| 7485 | 7558 | ** [sqlite3_wal_hook()] that causes any database on [database connection] D |
| 7486 | 7559 | ** to automatically [checkpoint] |
| 7487 | 7560 | ** after committing a transaction if there are N or |
| | @@ -7508,10 +7581,11 @@ |
| 7508 | 7581 | */ |
| 7509 | 7582 | SQLITE_API int SQLITE_STDCALL sqlite3_wal_autocheckpoint(sqlite3 *db, int N); |
| 7510 | 7583 | |
| 7511 | 7584 | /* |
| 7512 | 7585 | ** CAPI3REF: Checkpoint a database |
| 7586 | +** METHOD: sqlite3 |
| 7513 | 7587 | ** |
| 7514 | 7588 | ** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to |
| 7515 | 7589 | ** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^ |
| 7516 | 7590 | ** |
| 7517 | 7591 | ** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the |
| | @@ -7529,10 +7603,11 @@ |
| 7529 | 7603 | */ |
| 7530 | 7604 | SQLITE_API int SQLITE_STDCALL sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); |
| 7531 | 7605 | |
| 7532 | 7606 | /* |
| 7533 | 7607 | ** CAPI3REF: Checkpoint a database |
| 7608 | +** METHOD: sqlite3 |
| 7534 | 7609 | ** |
| 7535 | 7610 | ** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint |
| 7536 | 7611 | ** operation on database X of [database connection] D in mode M. Status |
| 7537 | 7612 | ** information is written back into integers pointed to by L and C.)^ |
| 7538 | 7613 | ** ^(The M parameter must be a valid [checkpoint mode]:)^ |
| | @@ -7783,10 +7858,11 @@ |
| 7783 | 7858 | #define SQLITE_SCANSTAT_EXPLAIN 4 |
| 7784 | 7859 | #define SQLITE_SCANSTAT_SELECTID 5 |
| 7785 | 7860 | |
| 7786 | 7861 | /* |
| 7787 | 7862 | ** CAPI3REF: Prepared Statement Scan Status |
| 7863 | +** METHOD: sqlite3_stmt |
| 7788 | 7864 | ** |
| 7789 | 7865 | ** This interface returns information about the predicted and measured |
| 7790 | 7866 | ** performance for pStmt. Advanced applications can use this |
| 7791 | 7867 | ** interface to compare the predicted and the measured performance and |
| 7792 | 7868 | ** issue warnings and/or rerun [ANALYZE] if discrepancies are found. |
| | @@ -7820,10 +7896,11 @@ |
| 7820 | 7896 | void *pOut /* Result written here */ |
| 7821 | 7897 | ); |
| 7822 | 7898 | |
| 7823 | 7899 | /* |
| 7824 | 7900 | ** CAPI3REF: Zero Scan-Status Counters |
| 7901 | +** METHOD: sqlite3_stmt |
| 7825 | 7902 | ** |
| 7826 | 7903 | ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters. |
| 7827 | 7904 | ** |
| 7828 | 7905 | ** This API is only available if the library is built with pre-processor |
| 7829 | 7906 | ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. |
| | @@ -8430,10 +8507,36 @@ |
| 8430 | 8507 | #else |
| 8431 | 8508 | # define ALWAYS(X) (X) |
| 8432 | 8509 | # define NEVER(X) (X) |
| 8433 | 8510 | #endif |
| 8434 | 8511 | |
| 8512 | +/* |
| 8513 | +** Declarations used for tracing the operating system interfaces. |
| 8514 | +*/ |
| 8515 | +#if defined(SQLITE_FORCE_OS_TRACE) || defined(SQLITE_TEST) || \ |
| 8516 | + (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) |
| 8517 | + extern int sqlite3OSTrace; |
| 8518 | +# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X |
| 8519 | +# define SQLITE_HAVE_OS_TRACE |
| 8520 | +#else |
| 8521 | +# define OSTRACE(X) |
| 8522 | +# undef SQLITE_HAVE_OS_TRACE |
| 8523 | +#endif |
| 8524 | + |
| 8525 | +/* |
| 8526 | +** Is the sqlite3ErrName() function needed in the build? Currently, |
| 8527 | +** it is needed by "mutex_w32.c" (when debugging), "os_win.c" (when |
| 8528 | +** OSTRACE is enabled), and by several "test*.c" files (which are |
| 8529 | +** compiled using SQLITE_TEST). |
| 8530 | +*/ |
| 8531 | +#if defined(SQLITE_HAVE_OS_TRACE) || defined(SQLITE_TEST) || \ |
| 8532 | + (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) |
| 8533 | +# define SQLITE_NEED_ERR_NAME |
| 8534 | +#else |
| 8535 | +# undef SQLITE_NEED_ERR_NAME |
| 8536 | +#endif |
| 8537 | + |
| 8435 | 8538 | /* |
| 8436 | 8539 | ** Return true (non-zero) if the input is an integer that is too large |
| 8437 | 8540 | ** to fit in 32-bits. This macro is used inside of various testcase() |
| 8438 | 8541 | ** macros to verify that we have tested SQLite for large-file support. |
| 8439 | 8542 | */ |
| | @@ -9841,37 +9944,36 @@ |
| 9841 | 9944 | /* Properties such as "out2" or "jump" that are specified in |
| 9842 | 9945 | ** comments following the "case" for each opcode in the vdbe.c |
| 9843 | 9946 | ** are encoded into bitvectors as follows: |
| 9844 | 9947 | */ |
| 9845 | 9948 | #define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */ |
| 9846 | | -#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */ |
| 9847 | | -#define OPFLG_IN1 0x0004 /* in1: P1 is an input */ |
| 9848 | | -#define OPFLG_IN2 0x0008 /* in2: P2 is an input */ |
| 9849 | | -#define OPFLG_IN3 0x0010 /* in3: P3 is an input */ |
| 9850 | | -#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */ |
| 9851 | | -#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */ |
| 9949 | +#define OPFLG_IN1 0x0002 /* in1: P1 is an input */ |
| 9950 | +#define OPFLG_IN2 0x0004 /* in2: P2 is an input */ |
| 9951 | +#define OPFLG_IN3 0x0008 /* in3: P3 is an input */ |
| 9952 | +#define OPFLG_OUT2 0x0010 /* out2: P2 is an output */ |
| 9953 | +#define OPFLG_OUT3 0x0020 /* out3: P3 is an output */ |
| 9852 | 9954 | #define OPFLG_INITIALIZER {\ |
| 9853 | 9955 | /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\ |
| 9854 | | -/* 8 */ 0x01, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00,\ |
| 9855 | | -/* 16 */ 0x01, 0x01, 0x04, 0x24, 0x01, 0x04, 0x05, 0x10,\ |
| 9856 | | -/* 24 */ 0x00, 0x02, 0x02, 0x02, 0x02, 0x00, 0x02, 0x02,\ |
| 9857 | | -/* 32 */ 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x05, 0x04,\ |
| 9858 | | -/* 40 */ 0x04, 0x00, 0x00, 0x01, 0x01, 0x05, 0x05, 0x00,\ |
| 9859 | | -/* 48 */ 0x00, 0x00, 0x02, 0x02, 0x10, 0x00, 0x00, 0x00,\ |
| 9860 | | -/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\ |
| 9861 | | -/* 64 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x4c,\ |
| 9862 | | -/* 72 */ 0x4c, 0x02, 0x02, 0x00, 0x05, 0x05, 0x15, 0x15,\ |
| 9863 | | -/* 80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\ |
| 9864 | | -/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\ |
| 9865 | | -/* 96 */ 0x24, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,\ |
| 9866 | | -/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x08, 0x08, 0x00,\ |
| 9867 | | -/* 112 */ 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00,\ |
| 9868 | | -/* 120 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 9869 | | -/* 128 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x02, 0x00, 0x01,\ |
| 9870 | | -/* 136 */ 0x08, 0x05, 0x05, 0x05, 0x05, 0x05, 0x00, 0x01,\ |
| 9956 | +/* 8 */ 0x01, 0x01, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00,\ |
| 9957 | +/* 16 */ 0x01, 0x01, 0x02, 0x12, 0x01, 0x02, 0x03, 0x08,\ |
| 9958 | +/* 24 */ 0x00, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x10,\ |
| 9959 | +/* 32 */ 0x00, 0x00, 0x10, 0x00, 0x00, 0x02, 0x03, 0x02,\ |
| 9960 | +/* 40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\ |
| 9961 | +/* 48 */ 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00, 0x00,\ |
| 9962 | +/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x09,\ |
| 9963 | +/* 64 */ 0x09, 0x09, 0x04, 0x09, 0x09, 0x09, 0x09, 0x26,\ |
| 9964 | +/* 72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\ |
| 9965 | +/* 80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\ |
| 9966 | +/* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\ |
| 9967 | +/* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\ |
| 9968 | +/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\ |
| 9969 | +/* 112 */ 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00, 0x00,\ |
| 9970 | +/* 120 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ |
| 9971 | +/* 128 */ 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00, 0x01,\ |
| 9972 | +/* 136 */ 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x01,\ |
| 9871 | 9973 | /* 144 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,\ |
| 9872 | | -/* 152 */ 0x00, 0x02, 0x02, 0x01, 0x00, 0x00,} |
| 9974 | +/* 152 */ 0x00, 0x10, 0x10, 0x01, 0x00, 0x00,} |
| 9873 | 9975 | |
| 9874 | 9976 | /************** End of opcodes.h *********************************************/ |
| 9875 | 9977 | /************** Continuing where we left off in vdbe.h ***********************/ |
| 9876 | 9978 | |
| 9877 | 9979 | /* |
| | @@ -9926,10 +10028,11 @@ |
| 9926 | 10028 | #endif |
| 9927 | 10029 | SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*); |
| 9928 | 10030 | |
| 9929 | 10031 | SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*); |
| 9930 | 10032 | SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*); |
| 10033 | +SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int); |
| 9931 | 10034 | SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **); |
| 9932 | 10035 | |
| 9933 | 10036 | typedef int (*RecordCompare)(int,const void*,UnpackedRecord*); |
| 9934 | 10037 | SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*); |
| 9935 | 10038 | |
| | @@ -11063,10 +11166,11 @@ |
| 11063 | 11166 | #define SQLITE_LoadExtension 0x00400000 /* Enable load_extension */ |
| 11064 | 11167 | #define SQLITE_EnableTrigger 0x00800000 /* True to enable triggers */ |
| 11065 | 11168 | #define SQLITE_DeferFKs 0x01000000 /* Defer all FK constraints */ |
| 11066 | 11169 | #define SQLITE_QueryOnly 0x02000000 /* Disable database changes */ |
| 11067 | 11170 | #define SQLITE_VdbeEQP 0x04000000 /* Debug EXPLAIN QUERY PLAN */ |
| 11171 | +#define SQLITE_Vacuum 0x08000000 /* Currently in a VACUUM */ |
| 11068 | 11172 | |
| 11069 | 11173 | |
| 11070 | 11174 | /* |
| 11071 | 11175 | ** Bits of the sqlite3.dbOptFlags field that are used by the |
| 11072 | 11176 | ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to |
| | @@ -11393,38 +11497,12 @@ |
| 11393 | 11497 | int iSavepoint; /* Depth of the SAVEPOINT stack */ |
| 11394 | 11498 | VTable *pNext; /* Next in linked list (see above) */ |
| 11395 | 11499 | }; |
| 11396 | 11500 | |
| 11397 | 11501 | /* |
| 11398 | | -** Each SQL table is represented in memory by an instance of the |
| 11399 | | -** following structure. |
| 11400 | | -** |
| 11401 | | -** Table.zName is the name of the table. The case of the original |
| 11402 | | -** CREATE TABLE statement is stored, but case is not significant for |
| 11403 | | -** comparisons. |
| 11404 | | -** |
| 11405 | | -** Table.nCol is the number of columns in this table. Table.aCol is a |
| 11406 | | -** pointer to an array of Column structures, one for each column. |
| 11407 | | -** |
| 11408 | | -** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of |
| 11409 | | -** the column that is that key. Otherwise Table.iPKey is negative. Note |
| 11410 | | -** that the datatype of the PRIMARY KEY must be INTEGER for this field to |
| 11411 | | -** be set. An INTEGER PRIMARY KEY is used as the rowid for each row of |
| 11412 | | -** the table. If a table has no INTEGER PRIMARY KEY, then a random rowid |
| 11413 | | -** is generated for each row of the table. TF_HasPrimaryKey is set if |
| 11414 | | -** the table has any PRIMARY KEY, INTEGER or otherwise. |
| 11415 | | -** |
| 11416 | | -** Table.tnum is the page number for the root BTree page of the table in the |
| 11417 | | -** database file. If Table.iDb is the index of the database table backend |
| 11418 | | -** in sqlite.aDb[]. 0 is for the main database and 1 is for the file that |
| 11419 | | -** holds temporary tables and indices. If TF_Ephemeral is set |
| 11420 | | -** then the table is stored in a file that is automatically deleted |
| 11421 | | -** when the VDBE cursor to the table is closed. In this case Table.tnum |
| 11422 | | -** refers VDBE cursor number that holds the table open, not to the root |
| 11423 | | -** page number. Transient tables are used to hold the results of a |
| 11424 | | -** sub-query that appears instead of a real table name in the FROM clause |
| 11425 | | -** of a SELECT statement. |
| 11502 | +** The schema for each SQL table and view is represented in memory |
| 11503 | +** by an instance of the following structure. |
| 11426 | 11504 | */ |
| 11427 | 11505 | struct Table { |
| 11428 | 11506 | char *zName; /* Name of the table or view */ |
| 11429 | 11507 | Column *aCol; /* Information about each column */ |
| 11430 | 11508 | Index *pIndex; /* List of SQL indexes on this table. */ |
| | @@ -11432,15 +11510,15 @@ |
| 11432 | 11510 | FKey *pFKey; /* Linked list of all foreign keys in this table */ |
| 11433 | 11511 | char *zColAff; /* String defining the affinity of each column */ |
| 11434 | 11512 | #ifndef SQLITE_OMIT_CHECK |
| 11435 | 11513 | ExprList *pCheck; /* All CHECK constraints */ |
| 11436 | 11514 | #endif |
| 11437 | | - LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */ |
| 11438 | | - int tnum; /* Root BTree node for this table (see note above) */ |
| 11439 | | - i16 iPKey; /* If not negative, use aCol[iPKey] as the primary key */ |
| 11515 | + int tnum; /* Root BTree page for this table */ |
| 11516 | + i16 iPKey; /* If not negative, use aCol[iPKey] as the rowid */ |
| 11440 | 11517 | i16 nCol; /* Number of columns in this table */ |
| 11441 | 11518 | u16 nRef; /* Number of pointers to this Table */ |
| 11519 | + LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */ |
| 11442 | 11520 | LogEst szTabRow; /* Estimated size of each table row in bytes */ |
| 11443 | 11521 | #ifdef SQLITE_ENABLE_COSTMULT |
| 11444 | 11522 | LogEst costMult; /* Cost multiplier for using this table */ |
| 11445 | 11523 | #endif |
| 11446 | 11524 | u8 tabFlags; /* Mask of TF_* values */ |
| | @@ -11458,17 +11536,24 @@ |
| 11458 | 11536 | Table *pNextZombie; /* Next on the Parse.pZombieTab list */ |
| 11459 | 11537 | }; |
| 11460 | 11538 | |
| 11461 | 11539 | /* |
| 11462 | 11540 | ** Allowed values for Table.tabFlags. |
| 11541 | +** |
| 11542 | +** TF_OOOHidden applies to virtual tables that have hidden columns that are |
| 11543 | +** followed by non-hidden columns. Example: "CREATE VIRTUAL TABLE x USING |
| 11544 | +** vtab1(a HIDDEN, b);". Since "b" is a non-hidden column but "a" is hidden, |
| 11545 | +** the TF_OOOHidden attribute would apply in this case. Such tables require |
| 11546 | +** special handling during INSERT processing. |
| 11463 | 11547 | */ |
| 11464 | 11548 | #define TF_Readonly 0x01 /* Read-only system table */ |
| 11465 | 11549 | #define TF_Ephemeral 0x02 /* An ephemeral table */ |
| 11466 | 11550 | #define TF_HasPrimaryKey 0x04 /* Table has a primary key */ |
| 11467 | 11551 | #define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */ |
| 11468 | 11552 | #define TF_Virtual 0x10 /* Is a virtual table */ |
| 11469 | 11553 | #define TF_WithoutRowid 0x20 /* No rowid used. PRIMARY KEY is the key */ |
| 11554 | +#define TF_OOOHidden 0x40 /* Out-of-Order hidden columns */ |
| 11470 | 11555 | |
| 11471 | 11556 | |
| 11472 | 11557 | /* |
| 11473 | 11558 | ** Test to see whether or not a table is a virtual table. This is |
| 11474 | 11559 | ** done as a macro so that it will be optimized out when virtual |
| | @@ -12221,11 +12306,11 @@ |
| 12221 | 12306 | #define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */ |
| 12222 | 12307 | #define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */ |
| 12223 | 12308 | #define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */ |
| 12224 | 12309 | #define SF_Compound 0x0040 /* Part of a compound query */ |
| 12225 | 12310 | #define SF_Values 0x0080 /* Synthesized from VALUES clause */ |
| 12226 | | -#define SF_AllValues 0x0100 /* All terms of compound are VALUES */ |
| 12311 | +#define SF_MultiValue 0x0100 /* Single VALUES term with multiple rows */ |
| 12227 | 12312 | #define SF_NestedFrom 0x0200 /* Part of a parenthesized FROM clause */ |
| 12228 | 12313 | #define SF_MaybeConvert 0x0400 /* Need convertCompoundSelectToSubquery() */ |
| 12229 | 12314 | #define SF_Recursive 0x0800 /* The recursive part of a recursive CTE */ |
| 12230 | 12315 | #define SF_MinMaxAgg 0x1000 /* Aggregate containing min() or max() */ |
| 12231 | 12316 | #define SF_Converted 0x2000 /* By convertCompoundSelectToSubquery() */ |
| | @@ -12605,24 +12690,24 @@ |
| 12605 | 12690 | * |
| 12606 | 12691 | * (op == TK_INSERT) |
| 12607 | 12692 | * orconf -> stores the ON CONFLICT algorithm |
| 12608 | 12693 | * pSelect -> If this is an INSERT INTO ... SELECT ... statement, then |
| 12609 | 12694 | * this stores a pointer to the SELECT statement. Otherwise NULL. |
| 12610 | | - * target -> A token holding the quoted name of the table to insert into. |
| 12695 | + * zTarget -> Dequoted name of the table to insert into. |
| 12611 | 12696 | * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then |
| 12612 | 12697 | * this stores values to be inserted. Otherwise NULL. |
| 12613 | 12698 | * pIdList -> If this is an INSERT INTO ... (<column-names>) VALUES ... |
| 12614 | 12699 | * statement, then this stores the column-names to be |
| 12615 | 12700 | * inserted into. |
| 12616 | 12701 | * |
| 12617 | 12702 | * (op == TK_DELETE) |
| 12618 | | - * target -> A token holding the quoted name of the table to delete from. |
| 12703 | + * zTarget -> Dequoted name of the table to delete from. |
| 12619 | 12704 | * pWhere -> The WHERE clause of the DELETE statement if one is specified. |
| 12620 | 12705 | * Otherwise NULL. |
| 12621 | 12706 | * |
| 12622 | 12707 | * (op == TK_UPDATE) |
| 12623 | | - * target -> A token holding the quoted name of the table to update rows of. |
| 12708 | + * zTarget -> Dequoted name of the table to update. |
| 12624 | 12709 | * pWhere -> The WHERE clause of the UPDATE statement if one is specified. |
| 12625 | 12710 | * Otherwise NULL. |
| 12626 | 12711 | * pExprList -> A list of the columns to update and the expressions to update |
| 12627 | 12712 | * them to. See sqlite3Update() documentation of "pChanges" |
| 12628 | 12713 | * argument. |
| | @@ -12630,12 +12715,12 @@ |
| 12630 | 12715 | */ |
| 12631 | 12716 | struct TriggerStep { |
| 12632 | 12717 | u8 op; /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */ |
| 12633 | 12718 | u8 orconf; /* OE_Rollback etc. */ |
| 12634 | 12719 | Trigger *pTrig; /* The trigger that this step is a part of */ |
| 12635 | | - Select *pSelect; /* SELECT statment or RHS of INSERT INTO .. SELECT ... */ |
| 12636 | | - Token target; /* Target table for DELETE, UPDATE, INSERT */ |
| 12720 | + Select *pSelect; /* SELECT statement or RHS of INSERT INTO SELECT ... */ |
| 12721 | + char *zTarget; /* Target table for DELETE, UPDATE, INSERT */ |
| 12637 | 12722 | Expr *pWhere; /* The WHERE clause for DELETE or UPDATE steps */ |
| 12638 | 12723 | ExprList *pExprList; /* SET clause for UPDATE. */ |
| 12639 | 12724 | IdList *pIdList; /* Column names for INSERT */ |
| 12640 | 12725 | TriggerStep *pNext; /* Next in the link-list */ |
| 12641 | 12726 | TriggerStep *pLast; /* Last element in link-list. Valid for 1st elem only */ |
| | @@ -12664,12 +12749,11 @@ |
| 12664 | 12749 | sqlite3 *db; /* Optional database for lookaside. Can be NULL */ |
| 12665 | 12750 | char *zBase; /* A base allocation. Not from malloc. */ |
| 12666 | 12751 | char *zText; /* The string collected so far */ |
| 12667 | 12752 | int nChar; /* Length of the string so far */ |
| 12668 | 12753 | int nAlloc; /* Amount of space allocated in zText */ |
| 12669 | | - int mxAlloc; /* Maximum allowed string length */ |
| 12670 | | - u8 useMalloc; /* 0: none, 1: sqlite3DbMalloc, 2: sqlite3_malloc */ |
| 12754 | + int mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */ |
| 12671 | 12755 | u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */ |
| 12672 | 12756 | }; |
| 12673 | 12757 | #define STRACCUM_NOMEM 1 |
| 12674 | 12758 | #define STRACCUM_TOOBIG 2 |
| 12675 | 12759 | |
| | @@ -12982,11 +13066,11 @@ |
| 12982 | 13066 | SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list); |
| 12983 | 13067 | SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...); |
| 12984 | 13068 | SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...); |
| 12985 | 13069 | SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list); |
| 12986 | 13070 | SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...); |
| 12987 | | -#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 13071 | +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 12988 | 13072 | SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...); |
| 12989 | 13073 | #endif |
| 12990 | 13074 | #if defined(SQLITE_TEST) |
| 12991 | 13075 | SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*); |
| 12992 | 13076 | #endif |
| | @@ -13329,11 +13413,11 @@ |
| 13329 | 13413 | SQLITE_PRIVATE void sqlite3Error(sqlite3*,int); |
| 13330 | 13414 | SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n); |
| 13331 | 13415 | SQLITE_PRIVATE u8 sqlite3HexToInt(int h); |
| 13332 | 13416 | SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **); |
| 13333 | 13417 | |
| 13334 | | -#if defined(SQLITE_TEST) |
| 13418 | +#if defined(SQLITE_NEED_ERR_NAME) |
| 13335 | 13419 | SQLITE_PRIVATE const char *sqlite3ErrName(int); |
| 13336 | 13420 | #endif |
| 13337 | 13421 | |
| 13338 | 13422 | SQLITE_PRIVATE const char *sqlite3ErrStr(int); |
| 13339 | 13423 | SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse); |
| | @@ -13423,11 +13507,11 @@ |
| 13423 | 13507 | FuncDestructor *pDestructor |
| 13424 | 13508 | ); |
| 13425 | 13509 | SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int); |
| 13426 | 13510 | SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *); |
| 13427 | 13511 | |
| 13428 | | -SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int); |
| 13512 | +SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int); |
| 13429 | 13513 | SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int); |
| 13430 | 13514 | SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*); |
| 13431 | 13515 | SQLITE_PRIVATE void sqlite3AppendChar(StrAccum*,int,char); |
| 13432 | 13516 | SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*); |
| 13433 | 13517 | SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*); |
| | @@ -14039,10 +14123,13 @@ |
| 14039 | 14123 | #if SQLITE_ENABLE_CEROD |
| 14040 | 14124 | "ENABLE_CEROD", |
| 14041 | 14125 | #endif |
| 14042 | 14126 | #if SQLITE_ENABLE_COLUMN_METADATA |
| 14043 | 14127 | "ENABLE_COLUMN_METADATA", |
| 14128 | +#endif |
| 14129 | +#if SQLITE_ENABLE_DBSTAT_VTAB |
| 14130 | + "ENABLE_DBSTAT_VTAB", |
| 14044 | 14131 | #endif |
| 14045 | 14132 | #if SQLITE_ENABLE_EXPENSIVE_ASSERT |
| 14046 | 14133 | "ENABLE_EXPENSIVE_ASSERT", |
| 14047 | 14134 | #endif |
| 14048 | 14135 | #if SQLITE_ENABLE_FTS1 |
| | @@ -19807,20 +19894,10 @@ |
| 19807 | 19894 | */ |
| 19808 | 19895 | #ifdef MEMORY_DEBUG |
| 19809 | 19896 | # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." |
| 19810 | 19897 | #endif |
| 19811 | 19898 | |
| 19812 | | -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 19813 | | -# ifndef SQLITE_DEBUG_OS_TRACE |
| 19814 | | -# define SQLITE_DEBUG_OS_TRACE 0 |
| 19815 | | -# endif |
| 19816 | | - int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE; |
| 19817 | | -# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X |
| 19818 | | -#else |
| 19819 | | -# define OSTRACE(X) |
| 19820 | | -#endif |
| 19821 | | - |
| 19822 | 19899 | /* |
| 19823 | 19900 | ** Macros for performance tracing. Normally turned off. Only works |
| 19824 | 19901 | ** on i486 hardware. |
| 19825 | 19902 | */ |
| 19826 | 19903 | #ifdef SQLITE_PERFORMANCE_TRACE |
| | @@ -21401,10 +21478,11 @@ |
| 21401 | 21478 | |
| 21402 | 21479 | /* |
| 21403 | 21480 | ** Set the StrAccum object to an error mode. |
| 21404 | 21481 | */ |
| 21405 | 21482 | static void setStrAccumError(StrAccum *p, u8 eError){ |
| 21483 | + assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG ); |
| 21406 | 21484 | p->accError = eError; |
| 21407 | 21485 | p->nAlloc = 0; |
| 21408 | 21486 | } |
| 21409 | 21487 | |
| 21410 | 21488 | /* |
| | @@ -21515,11 +21593,10 @@ |
| 21515 | 21593 | case '0': flag_zeropad = 1; break; |
| 21516 | 21594 | default: done = 1; break; |
| 21517 | 21595 | } |
| 21518 | 21596 | }while( !done && (c=(*++fmt))!=0 ); |
| 21519 | 21597 | /* Get the field width */ |
| 21520 | | - width = 0; |
| 21521 | 21598 | if( c=='*' ){ |
| 21522 | 21599 | if( bArgList ){ |
| 21523 | 21600 | width = (int)getIntArg(pArgList); |
| 21524 | 21601 | }else{ |
| 21525 | 21602 | width = va_arg(ap,int); |
| | @@ -21539,11 +21616,10 @@ |
| 21539 | 21616 | width = wx & 0x7fffffff; |
| 21540 | 21617 | } |
| 21541 | 21618 | |
| 21542 | 21619 | /* Get the precision */ |
| 21543 | 21620 | if( c=='.' ){ |
| 21544 | | - precision = 0; |
| 21545 | 21621 | c = *++fmt; |
| 21546 | 21622 | if( c=='*' ){ |
| 21547 | 21623 | if( bArgList ){ |
| 21548 | 21624 | precision = (int)getIntArg(pArgList); |
| 21549 | 21625 | }else{ |
| | @@ -22018,11 +22094,11 @@ |
| 22018 | 22094 | if( p->accError ){ |
| 22019 | 22095 | testcase(p->accError==STRACCUM_TOOBIG); |
| 22020 | 22096 | testcase(p->accError==STRACCUM_NOMEM); |
| 22021 | 22097 | return 0; |
| 22022 | 22098 | } |
| 22023 | | - if( !p->useMalloc ){ |
| 22099 | + if( p->mxAlloc==0 ){ |
| 22024 | 22100 | N = p->nAlloc - p->nChar - 1; |
| 22025 | 22101 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 22026 | 22102 | return N; |
| 22027 | 22103 | }else{ |
| 22028 | 22104 | char *zOld = (p->zText==p->zBase ? 0 : p->zText); |
| | @@ -22038,14 +22114,14 @@ |
| 22038 | 22114 | setStrAccumError(p, STRACCUM_TOOBIG); |
| 22039 | 22115 | return 0; |
| 22040 | 22116 | }else{ |
| 22041 | 22117 | p->nAlloc = (int)szNew; |
| 22042 | 22118 | } |
| 22043 | | - if( p->useMalloc==1 ){ |
| 22119 | + if( p->db ){ |
| 22044 | 22120 | zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); |
| 22045 | 22121 | }else{ |
| 22046 | | - zNew = sqlite3_realloc(zOld, p->nAlloc); |
| 22122 | + zNew = sqlite3_realloc64(zOld, p->nAlloc); |
| 22047 | 22123 | } |
| 22048 | 22124 | if( zNew ){ |
| 22049 | 22125 | assert( p->zText!=0 || p->nChar==0 ); |
| 22050 | 22126 | if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 22051 | 22127 | p->zText = zNew; |
| | @@ -22089,11 +22165,11 @@ |
| 22089 | 22165 | /* |
| 22090 | 22166 | ** Append N bytes of text from z to the StrAccum object. Increase the |
| 22091 | 22167 | ** size of the memory allocation for StrAccum if necessary. |
| 22092 | 22168 | */ |
| 22093 | 22169 | SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ |
| 22094 | | - assert( z!=0 ); |
| 22170 | + assert( z!=0 || N==0 ); |
| 22095 | 22171 | assert( p->zText!=0 || p->nChar==0 || p->accError ); |
| 22096 | 22172 | assert( N>=0 ); |
| 22097 | 22173 | assert( p->accError==0 || p->nAlloc==0 ); |
| 22098 | 22174 | if( p->nChar+N >= p->nAlloc ){ |
| 22099 | 22175 | enlargeAndAppend(p,z,N); |
| | @@ -22118,16 +22194,12 @@ |
| 22118 | 22194 | ** pointer if any kind of error was encountered. |
| 22119 | 22195 | */ |
| 22120 | 22196 | SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){ |
| 22121 | 22197 | if( p->zText ){ |
| 22122 | 22198 | p->zText[p->nChar] = 0; |
| 22123 | | - if( p->useMalloc && p->zText==p->zBase ){ |
| 22124 | | - if( p->useMalloc==1 ){ |
| 22125 | | - p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); |
| 22126 | | - }else{ |
| 22127 | | - p->zText = sqlite3_malloc(p->nChar+1); |
| 22128 | | - } |
| 22199 | + if( p->mxAlloc>0 && p->zText==p->zBase ){ |
| 22200 | + p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); |
| 22129 | 22201 | if( p->zText ){ |
| 22130 | 22202 | memcpy(p->zText, p->zBase, p->nChar+1); |
| 22131 | 22203 | }else{ |
| 22132 | 22204 | setStrAccumError(p, STRACCUM_NOMEM); |
| 22133 | 22205 | } |
| | @@ -22139,29 +22211,35 @@ |
| 22139 | 22211 | /* |
| 22140 | 22212 | ** Reset an StrAccum string. Reclaim all malloced memory. |
| 22141 | 22213 | */ |
| 22142 | 22214 | SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){ |
| 22143 | 22215 | if( p->zText!=p->zBase ){ |
| 22144 | | - if( p->useMalloc==1 ){ |
| 22145 | | - sqlite3DbFree(p->db, p->zText); |
| 22146 | | - }else{ |
| 22147 | | - sqlite3_free(p->zText); |
| 22148 | | - } |
| 22216 | + sqlite3DbFree(p->db, p->zText); |
| 22149 | 22217 | } |
| 22150 | 22218 | p->zText = 0; |
| 22151 | 22219 | } |
| 22152 | 22220 | |
| 22153 | 22221 | /* |
| 22154 | | -** Initialize a string accumulator |
| 22222 | +** Initialize a string accumulator. |
| 22223 | +** |
| 22224 | +** p: The accumulator to be initialized. |
| 22225 | +** db: Pointer to a database connection. May be NULL. Lookaside |
| 22226 | +** memory is used if not NULL. db->mallocFailed is set appropriately |
| 22227 | +** when not NULL. |
| 22228 | +** zBase: An initial buffer. May be NULL in which case the initial buffer |
| 22229 | +** is malloced. |
| 22230 | +** n: Size of zBase in bytes. If total space requirements never exceed |
| 22231 | +** n then no memory allocations ever occur. |
| 22232 | +** mx: Maximum number of bytes to accumulate. If mx==0 then no memory |
| 22233 | +** allocations will ever occur. |
| 22155 | 22234 | */ |
| 22156 | | -SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){ |
| 22235 | +SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ |
| 22157 | 22236 | p->zText = p->zBase = zBase; |
| 22158 | | - p->db = 0; |
| 22237 | + p->db = db; |
| 22159 | 22238 | p->nChar = 0; |
| 22160 | 22239 | p->nAlloc = n; |
| 22161 | 22240 | p->mxAlloc = mx; |
| 22162 | | - p->useMalloc = 1; |
| 22163 | 22241 | p->accError = 0; |
| 22164 | 22242 | } |
| 22165 | 22243 | |
| 22166 | 22244 | /* |
| 22167 | 22245 | ** Print into memory obtained from sqliteMalloc(). Use the internal |
| | @@ -22170,13 +22248,12 @@ |
| 22170 | 22248 | SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){ |
| 22171 | 22249 | char *z; |
| 22172 | 22250 | char zBase[SQLITE_PRINT_BUF_SIZE]; |
| 22173 | 22251 | StrAccum acc; |
| 22174 | 22252 | assert( db!=0 ); |
| 22175 | | - sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), |
| 22253 | + sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), |
| 22176 | 22254 | db->aLimit[SQLITE_LIMIT_LENGTH]); |
| 22177 | | - acc.db = db; |
| 22178 | 22255 | sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap); |
| 22179 | 22256 | z = sqlite3StrAccumFinish(&acc); |
| 22180 | 22257 | if( acc.accError==STRACCUM_NOMEM ){ |
| 22181 | 22258 | db->mallocFailed = 1; |
| 22182 | 22259 | } |
| | @@ -22230,12 +22307,11 @@ |
| 22230 | 22307 | } |
| 22231 | 22308 | #endif |
| 22232 | 22309 | #ifndef SQLITE_OMIT_AUTOINIT |
| 22233 | 22310 | if( sqlite3_initialize() ) return 0; |
| 22234 | 22311 | #endif |
| 22235 | | - sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
| 22236 | | - acc.useMalloc = 2; |
| 22312 | + sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
| 22237 | 22313 | sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 22238 | 22314 | z = sqlite3StrAccumFinish(&acc); |
| 22239 | 22315 | return z; |
| 22240 | 22316 | } |
| 22241 | 22317 | |
| | @@ -22276,12 +22352,11 @@ |
| 22276 | 22352 | (void)SQLITE_MISUSE_BKPT; |
| 22277 | 22353 | if( zBuf ) zBuf[0] = 0; |
| 22278 | 22354 | return zBuf; |
| 22279 | 22355 | } |
| 22280 | 22356 | #endif |
| 22281 | | - sqlite3StrAccumInit(&acc, zBuf, n, 0); |
| 22282 | | - acc.useMalloc = 0; |
| 22357 | + sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); |
| 22283 | 22358 | sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 22284 | 22359 | return sqlite3StrAccumFinish(&acc); |
| 22285 | 22360 | } |
| 22286 | 22361 | SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){ |
| 22287 | 22362 | char *z; |
| | @@ -22303,12 +22378,11 @@ |
| 22303 | 22378 | */ |
| 22304 | 22379 | static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ |
| 22305 | 22380 | StrAccum acc; /* String accumulator */ |
| 22306 | 22381 | char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ |
| 22307 | 22382 | |
| 22308 | | - sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0); |
| 22309 | | - acc.useMalloc = 0; |
| 22383 | + sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); |
| 22310 | 22384 | sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 22311 | 22385 | sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, |
| 22312 | 22386 | sqlite3StrAccumFinish(&acc)); |
| 22313 | 22387 | } |
| 22314 | 22388 | |
| | @@ -22322,22 +22396,21 @@ |
| 22322 | 22396 | renderLogMsg(iErrCode, zFormat, ap); |
| 22323 | 22397 | va_end(ap); |
| 22324 | 22398 | } |
| 22325 | 22399 | } |
| 22326 | 22400 | |
| 22327 | | -#if defined(SQLITE_DEBUG) |
| 22401 | +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 22328 | 22402 | /* |
| 22329 | 22403 | ** A version of printf() that understands %lld. Used for debugging. |
| 22330 | 22404 | ** The printf() built into some versions of windows does not understand %lld |
| 22331 | 22405 | ** and segfaults if you give it a long long int. |
| 22332 | 22406 | */ |
| 22333 | 22407 | SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){ |
| 22334 | 22408 | va_list ap; |
| 22335 | 22409 | StrAccum acc; |
| 22336 | 22410 | char zBuf[500]; |
| 22337 | | - sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); |
| 22338 | | - acc.useMalloc = 0; |
| 22411 | + sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 22339 | 22412 | va_start(ap,zFormat); |
| 22340 | 22413 | sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 22341 | 22414 | va_end(ap); |
| 22342 | 22415 | sqlite3StrAccumFinish(&acc); |
| 22343 | 22416 | fprintf(stdout,"%s", zBuf); |
| | @@ -22360,11 +22433,11 @@ |
| 22360 | 22433 | */ |
| 22361 | 22434 | /* Add a new subitem to the tree. The moreToFollow flag indicates that this |
| 22362 | 22435 | ** is not the last item in the tree. */ |
| 22363 | 22436 | SQLITE_PRIVATE TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ |
| 22364 | 22437 | if( p==0 ){ |
| 22365 | | - p = sqlite3_malloc( sizeof(*p) ); |
| 22438 | + p = sqlite3_malloc64( sizeof(*p) ); |
| 22366 | 22439 | if( p==0 ) return 0; |
| 22367 | 22440 | memset(p, 0, sizeof(*p)); |
| 22368 | 22441 | }else{ |
| 22369 | 22442 | p->iLevel++; |
| 22370 | 22443 | } |
| | @@ -22383,12 +22456,11 @@ |
| 22383 | 22456 | SQLITE_PRIVATE void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ |
| 22384 | 22457 | va_list ap; |
| 22385 | 22458 | int i; |
| 22386 | 22459 | StrAccum acc; |
| 22387 | 22460 | char zBuf[500]; |
| 22388 | | - sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0); |
| 22389 | | - acc.useMalloc = 0; |
| 22461 | + sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 22390 | 22462 | if( p ){ |
| 22391 | 22463 | for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){ |
| 22392 | 22464 | sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4); |
| 22393 | 22465 | } |
| 22394 | 22466 | sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); |
| | @@ -24007,10 +24079,11 @@ |
| 24007 | 24079 | }else{ |
| 24008 | 24080 | return 0; |
| 24009 | 24081 | } |
| 24010 | 24082 | } |
| 24011 | 24083 | #endif |
| 24084 | + while( zNum[0]=='0' ) zNum++; |
| 24012 | 24085 | for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ |
| 24013 | 24086 | v = v*10 + c; |
| 24014 | 24087 | } |
| 24015 | 24088 | |
| 24016 | 24089 | /* The longest decimal representation of a 32 bit integer is 10 digits: |
| | @@ -25261,10 +25334,21 @@ |
| 25261 | 25334 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 25262 | 25335 | # include <sys/ioctl.h> |
| 25263 | 25336 | # include <sys/file.h> |
| 25264 | 25337 | # include <sys/param.h> |
| 25265 | 25338 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
| 25339 | + |
| 25340 | +#if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \ |
| 25341 | + (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000)) |
| 25342 | +# if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \ |
| 25343 | + && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0)) |
| 25344 | +# define HAVE_GETHOSTUUID 1 |
| 25345 | +# else |
| 25346 | +# warning "gethostuuid() is disabled." |
| 25347 | +# endif |
| 25348 | +#endif |
| 25349 | + |
| 25266 | 25350 | |
| 25267 | 25351 | #if OS_VXWORKS |
| 25268 | 25352 | /* # include <sys/ioctl.h> */ |
| 25269 | 25353 | # include <semaphore.h> |
| 25270 | 25354 | # include <limits.h> |
| | @@ -25457,20 +25541,10 @@ |
| 25457 | 25541 | */ |
| 25458 | 25542 | #ifdef MEMORY_DEBUG |
| 25459 | 25543 | # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." |
| 25460 | 25544 | #endif |
| 25461 | 25545 | |
| 25462 | | -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 25463 | | -# ifndef SQLITE_DEBUG_OS_TRACE |
| 25464 | | -# define SQLITE_DEBUG_OS_TRACE 0 |
| 25465 | | -# endif |
| 25466 | | - int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE; |
| 25467 | | -# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X |
| 25468 | | -#else |
| 25469 | | -# define OSTRACE(X) |
| 25470 | | -#endif |
| 25471 | | - |
| 25472 | 25546 | /* |
| 25473 | 25547 | ** Macros for performance tracing. Normally turned off. Only works |
| 25474 | 25548 | ** on i486 hardware. |
| 25475 | 25549 | */ |
| 25476 | 25550 | #ifdef SQLITE_PERFORMANCE_TRACE |
| | @@ -26009,11 +26083,11 @@ |
| 26009 | 26083 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 26010 | 26084 | } |
| 26011 | 26085 | #endif |
| 26012 | 26086 | |
| 26013 | 26087 | |
| 26014 | | -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 26088 | +#ifdef SQLITE_HAVE_OS_TRACE |
| 26015 | 26089 | /* |
| 26016 | 26090 | ** Helper function for printing out trace information from debugging |
| 26017 | 26091 | ** binaries. This returns the string representation of the supplied |
| 26018 | 26092 | ** integer lock-type. |
| 26019 | 26093 | */ |
| | @@ -26272,11 +26346,11 @@ |
| 26272 | 26346 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 26273 | 26347 | int n; /* Length of zAbsoluteName string */ |
| 26274 | 26348 | |
| 26275 | 26349 | assert( zAbsoluteName[0]=='/' ); |
| 26276 | 26350 | n = (int)strlen(zAbsoluteName); |
| 26277 | | - pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 26351 | + pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) ); |
| 26278 | 26352 | if( pNew==0 ) return 0; |
| 26279 | 26353 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 26280 | 26354 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 26281 | 26355 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 26282 | 26356 | |
| | @@ -26676,11 +26750,11 @@ |
| 26676 | 26750 | pInode = inodeList; |
| 26677 | 26751 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 26678 | 26752 | pInode = pInode->pNext; |
| 26679 | 26753 | } |
| 26680 | 26754 | if( pInode==0 ){ |
| 26681 | | - pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 26755 | + pInode = sqlite3_malloc64( sizeof(*pInode) ); |
| 26682 | 26756 | if( pInode==0 ){ |
| 26683 | 26757 | return SQLITE_NOMEM; |
| 26684 | 26758 | } |
| 26685 | 26759 | memset(pInode, 0, sizeof(*pInode)); |
| 26686 | 26760 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| | @@ -29197,11 +29271,11 @@ |
| 29197 | 29271 | case SQLITE_FCNTL_VFSNAME: { |
| 29198 | 29272 | *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 29199 | 29273 | return SQLITE_OK; |
| 29200 | 29274 | } |
| 29201 | 29275 | case SQLITE_FCNTL_TEMPFILENAME: { |
| 29202 | | - char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname ); |
| 29276 | + char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname ); |
| 29203 | 29277 | if( zTFile ){ |
| 29204 | 29278 | unixGetTempname(pFile->pVfs->mxPathname, zTFile); |
| 29205 | 29279 | *(char**)pArg = zTFile; |
| 29206 | 29280 | } |
| 29207 | 29281 | return SQLITE_OK; |
| | @@ -29638,11 +29712,11 @@ |
| 29638 | 29712 | unixInodeInfo *pInode; /* The inode of fd */ |
| 29639 | 29713 | char *zShmFilename; /* Name of the file used for SHM */ |
| 29640 | 29714 | int nShmFilename; /* Size of the SHM filename in bytes */ |
| 29641 | 29715 | |
| 29642 | 29716 | /* Allocate space for the new unixShm object. */ |
| 29643 | | - p = sqlite3_malloc( sizeof(*p) ); |
| 29717 | + p = sqlite3_malloc64( sizeof(*p) ); |
| 29644 | 29718 | if( p==0 ) return SQLITE_NOMEM; |
| 29645 | 29719 | memset(p, 0, sizeof(*p)); |
| 29646 | 29720 | assert( pDbFd->pShm==0 ); |
| 29647 | 29721 | |
| 29648 | 29722 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| | @@ -29669,11 +29743,11 @@ |
| 29669 | 29743 | #ifdef SQLITE_SHM_DIRECTORY |
| 29670 | 29744 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
| 29671 | 29745 | #else |
| 29672 | 29746 | nShmFilename = 6 + (int)strlen(zBasePath); |
| 29673 | 29747 | #endif |
| 29674 | | - pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); |
| 29748 | + pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename ); |
| 29675 | 29749 | if( pShmNode==0 ){ |
| 29676 | 29750 | rc = SQLITE_NOMEM; |
| 29677 | 29751 | goto shm_open_err; |
| 29678 | 29752 | } |
| 29679 | 29753 | memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
| | @@ -29879,11 +29953,11 @@ |
| 29879 | 29953 | if( pMem==MAP_FAILED ){ |
| 29880 | 29954 | rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
| 29881 | 29955 | goto shmpage_out; |
| 29882 | 29956 | } |
| 29883 | 29957 | }else{ |
| 29884 | | - pMem = sqlite3_malloc(szRegion); |
| 29958 | + pMem = sqlite3_malloc64(szRegion); |
| 29885 | 29959 | if( pMem==0 ){ |
| 29886 | 29960 | rc = SQLITE_NOMEM; |
| 29887 | 29961 | goto shmpage_out; |
| 29888 | 29962 | } |
| 29889 | 29963 | memset(pMem, 0, szRegion); |
| | @@ -30716,11 +30790,11 @@ |
| 30716 | 30790 | else if( pLockingStyle == &afpIoMethods ){ |
| 30717 | 30791 | /* AFP locking uses the file path so it needs to be included in |
| 30718 | 30792 | ** the afpLockingContext. |
| 30719 | 30793 | */ |
| 30720 | 30794 | afpLockingContext *pCtx; |
| 30721 | | - pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 30795 | + pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
| 30722 | 30796 | if( pCtx==0 ){ |
| 30723 | 30797 | rc = SQLITE_NOMEM; |
| 30724 | 30798 | }else{ |
| 30725 | 30799 | /* NB: zFilename exists and remains valid until the file is closed |
| 30726 | 30800 | ** according to requirement F11141. So we do not need to make a |
| | @@ -30746,11 +30820,11 @@ |
| 30746 | 30820 | */ |
| 30747 | 30821 | char *zLockFile; |
| 30748 | 30822 | int nFilename; |
| 30749 | 30823 | assert( zFilename!=0 ); |
| 30750 | 30824 | nFilename = (int)strlen(zFilename) + 6; |
| 30751 | | - zLockFile = (char *)sqlite3_malloc(nFilename); |
| 30825 | + zLockFile = (char *)sqlite3_malloc64(nFilename); |
| 30752 | 30826 | if( zLockFile==0 ){ |
| 30753 | 30827 | rc = SQLITE_NOMEM; |
| 30754 | 30828 | }else{ |
| 30755 | 30829 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
| 30756 | 30830 | } |
| | @@ -31123,11 +31197,11 @@ |
| 31123 | 31197 | UnixUnusedFd *pUnused; |
| 31124 | 31198 | pUnused = findReusableFd(zName, flags); |
| 31125 | 31199 | if( pUnused ){ |
| 31126 | 31200 | fd = pUnused->fd; |
| 31127 | 31201 | }else{ |
| 31128 | | - pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 31202 | + pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
| 31129 | 31203 | if( !pUnused ){ |
| 31130 | 31204 | return SQLITE_NOMEM; |
| 31131 | 31205 | } |
| 31132 | 31206 | } |
| 31133 | 31207 | p->pUnused = pUnused; |
| | @@ -31503,11 +31577,11 @@ |
| 31503 | 31577 | ** that we always use the same random number sequence. This makes the |
| 31504 | 31578 | ** tests repeatable. |
| 31505 | 31579 | */ |
| 31506 | 31580 | memset(zBuf, 0, nBuf); |
| 31507 | 31581 | randomnessPid = osGetpid(0); |
| 31508 | | -#if !defined(SQLITE_TEST) |
| 31582 | +#if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) |
| 31509 | 31583 | { |
| 31510 | 31584 | int fd, got; |
| 31511 | 31585 | fd = robust_open("/dev/urandom", O_RDONLY, 0); |
| 31512 | 31586 | if( fd<0 ){ |
| 31513 | 31587 | time_t t; |
| | @@ -31915,11 +31989,11 @@ |
| 31915 | 31989 | */ |
| 31916 | 31990 | pUnused = findReusableFd(path, openFlags); |
| 31917 | 31991 | if( pUnused ){ |
| 31918 | 31992 | fd = pUnused->fd; |
| 31919 | 31993 | }else{ |
| 31920 | | - pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 31994 | + pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
| 31921 | 31995 | if( !pUnused ){ |
| 31922 | 31996 | return SQLITE_NOMEM; |
| 31923 | 31997 | } |
| 31924 | 31998 | } |
| 31925 | 31999 | if( fd<0 ){ |
| | @@ -31948,11 +32022,11 @@ |
| 31948 | 32022 | default: |
| 31949 | 32023 | return SQLITE_CANTOPEN_BKPT; |
| 31950 | 32024 | } |
| 31951 | 32025 | } |
| 31952 | 32026 | |
| 31953 | | - pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 32027 | + pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew)); |
| 31954 | 32028 | if( pNew==NULL ){ |
| 31955 | 32029 | rc = SQLITE_NOMEM; |
| 31956 | 32030 | goto end_create_proxy; |
| 31957 | 32031 | } |
| 31958 | 32032 | memset(pNew, 0, sizeof(unixFile)); |
| | @@ -31981,21 +32055,22 @@ |
| 31981 | 32055 | SQLITE_API int sqlite3_hostid_num = 0; |
| 31982 | 32056 | #endif |
| 31983 | 32057 | |
| 31984 | 32058 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 31985 | 32059 | |
| 32060 | +#ifdef HAVE_GETHOSTUUID |
| 31986 | 32061 | /* Not always defined in the headers as it ought to be */ |
| 31987 | 32062 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
| 32063 | +#endif |
| 31988 | 32064 | |
| 31989 | 32065 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 31990 | 32066 | ** bytes of writable memory. |
| 31991 | 32067 | */ |
| 31992 | 32068 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
| 31993 | 32069 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 31994 | 32070 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
| 31995 | | -# if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \ |
| 31996 | | - (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000)) |
| 32071 | +#ifdef HAVE_GETHOSTUUID |
| 31997 | 32072 | { |
| 31998 | 32073 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 31999 | 32074 | if( gethostuuid(pHostID, &timeout) ){ |
| 32000 | 32075 | int err = errno; |
| 32001 | 32076 | if( pError ){ |
| | @@ -32409,11 +32484,11 @@ |
| 32409 | 32484 | return rc; |
| 32410 | 32485 | } |
| 32411 | 32486 | |
| 32412 | 32487 | /* |
| 32413 | 32488 | ** Given the name of a database file, compute the name of its conch file. |
| 32414 | | -** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 32489 | +** Store the conch filename in memory obtained from sqlite3_malloc64(). |
| 32415 | 32490 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 32416 | 32491 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 32417 | 32492 | ** |
| 32418 | 32493 | ** The caller is responsible for ensuring that the allocated memory |
| 32419 | 32494 | ** space is eventually freed. |
| | @@ -32425,11 +32500,11 @@ |
| 32425 | 32500 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
| 32426 | 32501 | char *conchPath; /* buffer in which to construct conch name */ |
| 32427 | 32502 | |
| 32428 | 32503 | /* Allocate space for the conch filename and initialize the name to |
| 32429 | 32504 | ** the name of the original database file. */ |
| 32430 | | - *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 32505 | + *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8); |
| 32431 | 32506 | if( conchPath==0 ){ |
| 32432 | 32507 | return SQLITE_NOMEM; |
| 32433 | 32508 | } |
| 32434 | 32509 | memcpy(conchPath, dbPath, len+1); |
| 32435 | 32510 | |
| | @@ -32541,11 +32616,11 @@ |
| 32541 | 32616 | } |
| 32542 | 32617 | |
| 32543 | 32618 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 32544 | 32619 | (lockPath ? lockPath : ":auto:"), osGetpid(0))); |
| 32545 | 32620 | |
| 32546 | | - pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 32621 | + pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
| 32547 | 32622 | if( pCtx==0 ){ |
| 32548 | 32623 | return SQLITE_NOMEM; |
| 32549 | 32624 | } |
| 32550 | 32625 | memset(pCtx, 0, sizeof(*pCtx)); |
| 32551 | 32626 | |
| | @@ -32985,20 +33060,10 @@ |
| 32985 | 33060 | */ |
| 32986 | 33061 | #ifdef MEMORY_DEBUG |
| 32987 | 33062 | # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." |
| 32988 | 33063 | #endif |
| 32989 | 33064 | |
| 32990 | | -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 32991 | | -# ifndef SQLITE_DEBUG_OS_TRACE |
| 32992 | | -# define SQLITE_DEBUG_OS_TRACE 0 |
| 32993 | | -# endif |
| 32994 | | - int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE; |
| 32995 | | -# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X |
| 32996 | | -#else |
| 32997 | | -# define OSTRACE(X) |
| 32998 | | -#endif |
| 32999 | | - |
| 33000 | 33065 | /* |
| 33001 | 33066 | ** Macros for performance tracing. Normally turned off. Only works |
| 33002 | 33067 | ** on i486 hardware. |
| 33003 | 33068 | */ |
| 33004 | 33069 | #ifdef SQLITE_PERFORMANCE_TRACE |
| | @@ -35898,11 +35963,11 @@ |
| 35898 | 35963 | ** Used only when SQLITE_NO_SYNC is not defined. |
| 35899 | 35964 | */ |
| 35900 | 35965 | BOOL rc; |
| 35901 | 35966 | #endif |
| 35902 | 35967 | #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \ |
| 35903 | | - (defined(SQLITE_TEST) && defined(SQLITE_DEBUG)) |
| 35968 | + defined(SQLITE_HAVE_OS_TRACE) |
| 35904 | 35969 | /* |
| 35905 | 35970 | ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or |
| 35906 | 35971 | ** OSTRACE() macros. |
| 35907 | 35972 | */ |
| 35908 | 35973 | winFile *pFile = (winFile*)id; |
| | @@ -36575,11 +36640,11 @@ |
| 36575 | 36640 | DWORD lastErrno; /* The Windows errno from the last I/O error */ |
| 36576 | 36641 | |
| 36577 | 36642 | int nRef; /* Number of winShm objects pointing to this */ |
| 36578 | 36643 | winShm *pFirst; /* All winShm objects pointing to this */ |
| 36579 | 36644 | winShmNode *pNext; /* Next in list of all winShmNode objects */ |
| 36580 | | -#ifdef SQLITE_DEBUG |
| 36645 | +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 36581 | 36646 | u8 nextShmId; /* Next available winShm.id value */ |
| 36582 | 36647 | #endif |
| 36583 | 36648 | }; |
| 36584 | 36649 | |
| 36585 | 36650 | /* |
| | @@ -36606,11 +36671,11 @@ |
| 36606 | 36671 | winShmNode *pShmNode; /* The underlying winShmNode object */ |
| 36607 | 36672 | winShm *pNext; /* Next winShm with the same winShmNode */ |
| 36608 | 36673 | u8 hasMutex; /* True if holding the winShmNode mutex */ |
| 36609 | 36674 | u16 sharedMask; /* Mask of shared locks held */ |
| 36610 | 36675 | u16 exclMask; /* Mask of exclusive locks held */ |
| 36611 | | -#ifdef SQLITE_DEBUG |
| 36676 | +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 36612 | 36677 | u8 id; /* Id of this connection with its winShmNode */ |
| 36613 | 36678 | #endif |
| 36614 | 36679 | }; |
| 36615 | 36680 | |
| 36616 | 36681 | /* |
| | @@ -36797,11 +36862,11 @@ |
| 36797 | 36862 | if( rc ) goto shm_open_err; |
| 36798 | 36863 | } |
| 36799 | 36864 | |
| 36800 | 36865 | /* Make the new connection a child of the winShmNode */ |
| 36801 | 36866 | p->pShmNode = pShmNode; |
| 36802 | | -#ifdef SQLITE_DEBUG |
| 36867 | +#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 36803 | 36868 | p->id = pShmNode->nextShmId++; |
| 36804 | 36869 | #endif |
| 36805 | 36870 | pShmNode->nRef++; |
| 36806 | 36871 | pDbFd->pShm = p; |
| 36807 | 36872 | winShmLeaveMutex(); |
| | @@ -37066,11 +37131,11 @@ |
| 37066 | 37131 | goto shmpage_out; |
| 37067 | 37132 | } |
| 37068 | 37133 | } |
| 37069 | 37134 | |
| 37070 | 37135 | /* Map the requested memory region into this processes address space. */ |
| 37071 | | - apNew = (struct ShmRegion *)sqlite3_realloc( |
| 37136 | + apNew = (struct ShmRegion *)sqlite3_realloc64( |
| 37072 | 37137 | pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0]) |
| 37073 | 37138 | ); |
| 37074 | 37139 | if( !apNew ){ |
| 37075 | 37140 | rc = SQLITE_IOERR_NOMEM; |
| 37076 | 37141 | goto shmpage_out; |
| | @@ -38513,11 +38578,11 @@ |
| 38513 | 38578 | ** Write up to nBuf bytes of randomness into zBuf. |
| 38514 | 38579 | */ |
| 38515 | 38580 | static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
| 38516 | 38581 | int n = 0; |
| 38517 | 38582 | UNUSED_PARAMETER(pVfs); |
| 38518 | | -#if defined(SQLITE_TEST) |
| 38583 | +#if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) |
| 38519 | 38584 | n = nBuf; |
| 38520 | 38585 | memset(zBuf, 0, nBuf); |
| 38521 | 38586 | #else |
| 38522 | 38587 | if( sizeof(SYSTEMTIME)<=nBuf-n ){ |
| 38523 | 38588 | SYSTEMTIME x; |
| | @@ -38547,11 +38612,10 @@ |
| 38547 | 38612 | LARGE_INTEGER i; |
| 38548 | 38613 | osQueryPerformanceCounter(&i); |
| 38549 | 38614 | memcpy(&zBuf[n], &i, sizeof(i)); |
| 38550 | 38615 | n += sizeof(i); |
| 38551 | 38616 | } |
| 38552 | | -#endif |
| 38553 | 38617 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID |
| 38554 | 38618 | if( sizeof(UUID)<=nBuf-n ){ |
| 38555 | 38619 | UUID id; |
| 38556 | 38620 | memset(&id, 0, sizeof(UUID)); |
| 38557 | 38621 | osUuidCreate(&id); |
| | @@ -38564,10 +38628,11 @@ |
| 38564 | 38628 | osUuidCreateSequential(&id); |
| 38565 | 38629 | memcpy(zBuf, &id, sizeof(UUID)); |
| 38566 | 38630 | n += sizeof(UUID); |
| 38567 | 38631 | } |
| 38568 | 38632 | #endif |
| 38633 | +#endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */ |
| 38569 | 38634 | return n; |
| 38570 | 38635 | } |
| 38571 | 38636 | |
| 38572 | 38637 | |
| 38573 | 38638 | /* |
| | @@ -39118,11 +39183,11 @@ |
| 39118 | 39183 | |
| 39119 | 39184 | /* Allocate the Bitvec to be tested and a linear array of |
| 39120 | 39185 | ** bits to act as the reference */ |
| 39121 | 39186 | pBitvec = sqlite3BitvecCreate( sz ); |
| 39122 | 39187 | pV = sqlite3MallocZero( (sz+7)/8 + 1 ); |
| 39123 | | - pTmpSpace = sqlite3_malloc(BITVEC_SZ); |
| 39188 | + pTmpSpace = sqlite3_malloc64(BITVEC_SZ); |
| 39124 | 39189 | if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end; |
| 39125 | 39190 | |
| 39126 | 39191 | /* NULL pBitvec tests */ |
| 39127 | 39192 | sqlite3BitvecSet(0, 1); |
| 39128 | 39193 | sqlite3BitvecClear(0, 1, pTmpSpace); |
| | @@ -44607,13 +44672,11 @@ |
| 44607 | 44672 | Pgno nTruncate, /* Database size after this commit */ |
| 44608 | 44673 | int isCommit /* True if this is a commit */ |
| 44609 | 44674 | ){ |
| 44610 | 44675 | int rc; /* Return code */ |
| 44611 | 44676 | int nList; /* Number of pages in pList */ |
| 44612 | | -#if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES) |
| 44613 | 44677 | PgHdr *p; /* For looping over pages */ |
| 44614 | | -#endif |
| 44615 | 44678 | |
| 44616 | 44679 | assert( pPager->pWal ); |
| 44617 | 44680 | assert( pList ); |
| 44618 | 44681 | #ifdef SQLITE_DEBUG |
| 44619 | 44682 | /* Verify that the page list is in accending order */ |
| | @@ -44626,11 +44689,10 @@ |
| 44626 | 44689 | if( isCommit ){ |
| 44627 | 44690 | /* If a WAL transaction is being committed, there is no point in writing |
| 44628 | 44691 | ** any pages with page numbers greater than nTruncate into the WAL file. |
| 44629 | 44692 | ** They will never be read by any client. So remove them from the pDirty |
| 44630 | 44693 | ** list here. */ |
| 44631 | | - PgHdr *p; |
| 44632 | 44694 | PgHdr **ppNext = &pList; |
| 44633 | 44695 | nList = 0; |
| 44634 | 44696 | for(p=pList; (*ppNext = p)!=0; p=p->pDirty){ |
| 44635 | 44697 | if( p->pgno<=nTruncate ){ |
| 44636 | 44698 | ppNext = &p->pDirty; |
| | @@ -44646,11 +44708,10 @@ |
| 44646 | 44708 | if( pList->pgno==1 ) pager_write_changecounter(pList); |
| 44647 | 44709 | rc = sqlite3WalFrames(pPager->pWal, |
| 44648 | 44710 | pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags |
| 44649 | 44711 | ); |
| 44650 | 44712 | if( rc==SQLITE_OK && pPager->pBackup ){ |
| 44651 | | - PgHdr *p; |
| 44652 | 44713 | for(p=pList; p; p=p->pDirty){ |
| 44653 | 44714 | sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData); |
| 44654 | 44715 | } |
| 44655 | 44716 | } |
| 44656 | 44717 | |
| | @@ -48577,10 +48638,12 @@ |
| 48577 | 48638 | }else if( state==PAGER_OPEN ){ |
| 48578 | 48639 | pager_unlock(pPager); |
| 48579 | 48640 | } |
| 48580 | 48641 | assert( state==pPager->eState ); |
| 48581 | 48642 | } |
| 48643 | + }else if( eMode==PAGER_JOURNALMODE_OFF ){ |
| 48644 | + sqlite3OsClose(pPager->jfd); |
| 48582 | 48645 | } |
| 48583 | 48646 | } |
| 48584 | 48647 | |
| 48585 | 48648 | /* Return the new journal mode */ |
| 48586 | 48649 | return (int)pPager->journalMode; |
| | @@ -49359,11 +49422,11 @@ |
| 49359 | 49422 | |
| 49360 | 49423 | /* Enlarge the pWal->apWiData[] array if required */ |
| 49361 | 49424 | if( pWal->nWiData<=iPage ){ |
| 49362 | 49425 | int nByte = sizeof(u32*)*(iPage+1); |
| 49363 | 49426 | volatile u32 **apNew; |
| 49364 | | - apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte); |
| 49427 | + apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte); |
| 49365 | 49428 | if( !apNew ){ |
| 49366 | 49429 | *ppPage = 0; |
| 49367 | 49430 | return SQLITE_NOMEM; |
| 49368 | 49431 | } |
| 49369 | 49432 | memset((void*)&apNew[pWal->nWiData], 0, |
| | @@ -49984,11 +50047,11 @@ |
| 49984 | 50047 | goto finished; |
| 49985 | 50048 | } |
| 49986 | 50049 | |
| 49987 | 50050 | /* Malloc a buffer to read frames into. */ |
| 49988 | 50051 | szFrame = szPage + WAL_FRAME_HDRSIZE; |
| 49989 | | - aFrame = (u8 *)sqlite3_malloc(szFrame); |
| 50052 | + aFrame = (u8 *)sqlite3_malloc64(szFrame); |
| 49990 | 50053 | if( !aFrame ){ |
| 49991 | 50054 | rc = SQLITE_NOMEM; |
| 49992 | 50055 | goto recovery_error; |
| 49993 | 50056 | } |
| 49994 | 50057 | aData = &aFrame[WAL_FRAME_HDRSIZE]; |
| | @@ -50377,21 +50440,21 @@ |
| 50377 | 50440 | /* Allocate space for the WalIterator object. */ |
| 50378 | 50441 | nSegment = walFramePage(iLast) + 1; |
| 50379 | 50442 | nByte = sizeof(WalIterator) |
| 50380 | 50443 | + (nSegment-1)*sizeof(struct WalSegment) |
| 50381 | 50444 | + iLast*sizeof(ht_slot); |
| 50382 | | - p = (WalIterator *)sqlite3_malloc(nByte); |
| 50445 | + p = (WalIterator *)sqlite3_malloc64(nByte); |
| 50383 | 50446 | if( !p ){ |
| 50384 | 50447 | return SQLITE_NOMEM; |
| 50385 | 50448 | } |
| 50386 | 50449 | memset(p, 0, nByte); |
| 50387 | 50450 | p->nSegment = nSegment; |
| 50388 | 50451 | |
| 50389 | 50452 | /* Allocate temporary space used by the merge-sort routine. This block |
| 50390 | 50453 | ** of memory will be freed before this function returns. |
| 50391 | 50454 | */ |
| 50392 | | - aTmp = (ht_slot *)sqlite3_malloc( |
| 50455 | + aTmp = (ht_slot *)sqlite3_malloc64( |
| 50393 | 50456 | sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast) |
| 50394 | 50457 | ); |
| 50395 | 50458 | if( !aTmp ){ |
| 50396 | 50459 | rc = SQLITE_NOMEM; |
| 50397 | 50460 | } |
| | @@ -50567,10 +50630,18 @@ |
| 50567 | 50630 | ** cannot be backfilled from the WAL. |
| 50568 | 50631 | */ |
| 50569 | 50632 | mxSafeFrame = pWal->hdr.mxFrame; |
| 50570 | 50633 | mxPage = pWal->hdr.nPage; |
| 50571 | 50634 | for(i=1; i<WAL_NREADER; i++){ |
| 50635 | + /* Thread-sanitizer reports that the following is an unsafe read, |
| 50636 | + ** as some other thread may be in the process of updating the value |
| 50637 | + ** of the aReadMark[] slot. The assumption here is that if that is |
| 50638 | + ** happening, the other client may only be increasing the value, |
| 50639 | + ** not decreasing it. So assuming either that either the "old" or |
| 50640 | + ** "new" version of the value is read, and not some arbitrary value |
| 50641 | + ** that would never be written by a real client, things are still |
| 50642 | + ** safe. */ |
| 50572 | 50643 | u32 y = pInfo->aReadMark[i]; |
| 50573 | 50644 | if( mxSafeFrame>y ){ |
| 50574 | 50645 | assert( y<=pWal->hdr.mxFrame ); |
| 50575 | 50646 | rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1); |
| 50576 | 50647 | if( rc==SQLITE_OK ){ |
| | @@ -55407,11 +55478,11 @@ |
| 55407 | 55478 | } |
| 55408 | 55479 | assert( nReserve>=0 && nReserve<=255 ); |
| 55409 | 55480 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 55410 | 55481 | ((pageSize-1)&pageSize)==0 ){ |
| 55411 | 55482 | assert( (pageSize & 7)==0 ); |
| 55412 | | - assert( !pBt->pPage1 && !pBt->pCursor ); |
| 55483 | + assert( !pBt->pCursor ); |
| 55413 | 55484 | pBt->pageSize = (u32)pageSize; |
| 55414 | 55485 | freeTempSpace(pBt); |
| 55415 | 55486 | } |
| 55416 | 55487 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); |
| 55417 | 55488 | pBt->usableSize = pBt->pageSize - (u16)nReserve; |
| | @@ -57429,17 +57500,22 @@ |
| 57429 | 57500 | */ |
| 57430 | 57501 | static const void *fetchPayload( |
| 57431 | 57502 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 57432 | 57503 | u32 *pAmt /* Write the number of available bytes here */ |
| 57433 | 57504 | ){ |
| 57505 | + u32 amt; |
| 57434 | 57506 | assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); |
| 57435 | 57507 | assert( pCur->eState==CURSOR_VALID ); |
| 57436 | 57508 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 57437 | 57509 | assert( cursorHoldsMutex(pCur) ); |
| 57438 | 57510 | assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell ); |
| 57439 | 57511 | assert( pCur->info.nSize>0 ); |
| 57440 | | - *pAmt = pCur->info.nLocal; |
| 57512 | + assert( pCur->info.pPayload>pCur->apPage[pCur->iPage]->aData || CORRUPT_DB ); |
| 57513 | + assert( pCur->info.pPayload<pCur->apPage[pCur->iPage]->aDataEnd ||CORRUPT_DB); |
| 57514 | + amt = (int)(pCur->apPage[pCur->iPage]->aDataEnd - pCur->info.pPayload); |
| 57515 | + if( pCur->info.nLocal<amt ) amt = pCur->info.nLocal; |
| 57516 | + *pAmt = amt; |
| 57441 | 57517 | return (void*)pCur->info.pPayload; |
| 57442 | 57518 | } |
| 57443 | 57519 | |
| 57444 | 57520 | |
| 57445 | 57521 | /* |
| | @@ -59713,11 +59789,10 @@ |
| 59713 | 59789 | if( iParentIdx==0 ){ |
| 59714 | 59790 | nxDiv = 0; |
| 59715 | 59791 | }else if( iParentIdx==i ){ |
| 59716 | 59792 | nxDiv = i-2+bBulk; |
| 59717 | 59793 | }else{ |
| 59718 | | - assert( bBulk==0 ); |
| 59719 | 59794 | nxDiv = iParentIdx-1; |
| 59720 | 59795 | } |
| 59721 | 59796 | i = 2-bBulk; |
| 59722 | 59797 | } |
| 59723 | 59798 | nOld = i+1; |
| | @@ -61501,10 +61576,61 @@ |
| 61501 | 61576 | iPage = get4byte(pOvflData); |
| 61502 | 61577 | sqlite3PagerUnref(pOvflPage); |
| 61503 | 61578 | } |
| 61504 | 61579 | } |
| 61505 | 61580 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
| 61581 | + |
| 61582 | +/* |
| 61583 | +** An implementation of a min-heap. |
| 61584 | +** |
| 61585 | +** aHeap[0] is the number of elements on the heap. aHeap[1] is the |
| 61586 | +** root element. The daughter nodes of aHeap[N] are aHeap[N*2] |
| 61587 | +** and aHeap[N*2+1]. |
| 61588 | +** |
| 61589 | +** The heap property is this: Every node is less than or equal to both |
| 61590 | +** of its daughter nodes. A consequence of the heap property is that the |
| 61591 | +** root node aHeap[1] is always the minimum value currently in the heap. |
| 61592 | +** |
| 61593 | +** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto |
| 61594 | +** the heap, preserving the heap property. The btreeHeapPull() routine |
| 61595 | +** removes the root element from the heap (the minimum value in the heap) |
| 61596 | +** and then moves other nodes around as necessary to preserve the heap |
| 61597 | +** property. |
| 61598 | +** |
| 61599 | +** This heap is used for cell overlap and coverage testing. Each u32 |
| 61600 | +** entry represents the span of a cell or freeblock on a btree page. |
| 61601 | +** The upper 16 bits are the index of the first byte of a range and the |
| 61602 | +** lower 16 bits are the index of the last byte of that range. |
| 61603 | +*/ |
| 61604 | +static void btreeHeapInsert(u32 *aHeap, u32 x){ |
| 61605 | + u32 j, i = ++aHeap[0]; |
| 61606 | + aHeap[i] = x; |
| 61607 | + while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){ |
| 61608 | + x = aHeap[j]; |
| 61609 | + aHeap[j] = aHeap[i]; |
| 61610 | + aHeap[i] = x; |
| 61611 | + i = j; |
| 61612 | + } |
| 61613 | +} |
| 61614 | +static int btreeHeapPull(u32 *aHeap, u32 *pOut){ |
| 61615 | + u32 j, i, x; |
| 61616 | + if( (x = aHeap[0])==0 ) return 0; |
| 61617 | + *pOut = aHeap[1]; |
| 61618 | + aHeap[1] = aHeap[x]; |
| 61619 | + aHeap[x] = 0xffffffff; |
| 61620 | + aHeap[0]--; |
| 61621 | + i = 1; |
| 61622 | + while( (j = i*2)<=aHeap[0] ){ |
| 61623 | + if( aHeap[j]>aHeap[j+1] ) j++; |
| 61624 | + if( aHeap[i]<aHeap[j] ) break; |
| 61625 | + x = aHeap[i]; |
| 61626 | + aHeap[i] = aHeap[j]; |
| 61627 | + aHeap[j] = x; |
| 61628 | + i = j; |
| 61629 | + } |
| 61630 | + return 1; |
| 61631 | +} |
| 61506 | 61632 | |
| 61507 | 61633 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
| 61508 | 61634 | /* |
| 61509 | 61635 | ** Do various sanity checks on a single page of a tree. Return |
| 61510 | 61636 | ** the tree depth. Root pages return 0. Parents of root pages |
| | @@ -61534,11 +61660,12 @@ |
| 61534 | 61660 | int hdr, cellStart; |
| 61535 | 61661 | int nCell; |
| 61536 | 61662 | u8 *data; |
| 61537 | 61663 | BtShared *pBt; |
| 61538 | 61664 | int usableSize; |
| 61539 | | - char *hit = 0; |
| 61665 | + u32 *heap = 0; |
| 61666 | + u32 x, prev = 0; |
| 61540 | 61667 | i64 nMinKey = 0; |
| 61541 | 61668 | i64 nMaxKey = 0; |
| 61542 | 61669 | const char *saved_zPfx = pCheck->zPfx; |
| 61543 | 61670 | int saved_v1 = pCheck->v1; |
| 61544 | 61671 | int saved_v2 = pCheck->v2; |
| | @@ -61679,19 +61806,19 @@ |
| 61679 | 61806 | |
| 61680 | 61807 | /* Check for complete coverage of the page |
| 61681 | 61808 | */ |
| 61682 | 61809 | data = pPage->aData; |
| 61683 | 61810 | hdr = pPage->hdrOffset; |
| 61684 | | - hit = sqlite3PageMalloc( pBt->pageSize ); |
| 61811 | + heap = (u32*)sqlite3PageMalloc( pBt->pageSize ); |
| 61685 | 61812 | pCheck->zPfx = 0; |
| 61686 | | - if( hit==0 ){ |
| 61813 | + if( heap==0 ){ |
| 61687 | 61814 | pCheck->mallocFailed = 1; |
| 61688 | 61815 | }else{ |
| 61689 | 61816 | int contentOffset = get2byteNotZero(&data[hdr+5]); |
| 61690 | 61817 | assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ |
| 61691 | | - memset(hit+contentOffset, 0, usableSize-contentOffset); |
| 61692 | | - memset(hit, 1, contentOffset); |
| 61818 | + heap[0] = 0; |
| 61819 | + btreeHeapInsert(heap, contentOffset-1); |
| 61693 | 61820 | /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the |
| 61694 | 61821 | ** number of cells on the page. */ |
| 61695 | 61822 | nCell = get2byte(&data[hdr+3]); |
| 61696 | 61823 | /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page |
| 61697 | 61824 | ** immediately follows the b-tree page header. */ |
| | @@ -61699,20 +61826,19 @@ |
| 61699 | 61826 | /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte |
| 61700 | 61827 | ** integer offsets to the cell contents. */ |
| 61701 | 61828 | for(i=0; i<nCell; i++){ |
| 61702 | 61829 | int pc = get2byte(&data[cellStart+i*2]); |
| 61703 | 61830 | u32 size = 65536; |
| 61704 | | - int j; |
| 61705 | 61831 | if( pc<=usableSize-4 ){ |
| 61706 | 61832 | size = cellSizePtr(pPage, &data[pc]); |
| 61707 | 61833 | } |
| 61708 | 61834 | if( (int)(pc+size-1)>=usableSize ){ |
| 61709 | 61835 | pCheck->zPfx = 0; |
| 61710 | 61836 | checkAppendMsg(pCheck, |
| 61711 | 61837 | "Corruption detected in cell %d on page %d",i,iPage); |
| 61712 | 61838 | }else{ |
| 61713 | | - for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 61839 | + btreeHeapInsert(heap, (pc<<16)|(pc+size-1)); |
| 61714 | 61840 | } |
| 61715 | 61841 | } |
| 61716 | 61842 | /* EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header |
| 61717 | 61843 | ** is the offset of the first freeblock, or zero if there are no |
| 61718 | 61844 | ** freeblocks on the page. */ |
| | @@ -61720,11 +61846,11 @@ |
| 61720 | 61846 | while( i>0 ){ |
| 61721 | 61847 | int size, j; |
| 61722 | 61848 | assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 61723 | 61849 | size = get2byte(&data[i+2]); |
| 61724 | 61850 | assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */ |
| 61725 | | - for(j=i+size-1; j>=i; j--) hit[j]++; |
| 61851 | + btreeHeapInsert(heap, (i<<16)|(i+size-1)); |
| 61726 | 61852 | /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a |
| 61727 | 61853 | ** big-endian integer which is the offset in the b-tree page of the next |
| 61728 | 61854 | ** freeblock in the chain, or zero if the freeblock is the last on the |
| 61729 | 61855 | ** chain. */ |
| 61730 | 61856 | j = get2byte(&data[i]); |
| | @@ -61732,31 +61858,37 @@ |
| 61732 | 61858 | ** increasing offset. */ |
| 61733 | 61859 | assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ |
| 61734 | 61860 | assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */ |
| 61735 | 61861 | i = j; |
| 61736 | 61862 | } |
| 61737 | | - for(i=cnt=0; i<usableSize; i++){ |
| 61738 | | - if( hit[i]==0 ){ |
| 61739 | | - cnt++; |
| 61740 | | - }else if( hit[i]>1 ){ |
| 61863 | + cnt = 0; |
| 61864 | + assert( heap[0]>0 ); |
| 61865 | + assert( (heap[1]>>16)==0 ); |
| 61866 | + btreeHeapPull(heap,&prev); |
| 61867 | + while( btreeHeapPull(heap,&x) ){ |
| 61868 | + if( (prev&0xffff)+1>(x>>16) ){ |
| 61741 | 61869 | checkAppendMsg(pCheck, |
| 61742 | | - "Multiple uses for byte %d of page %d", i, iPage); |
| 61870 | + "Multiple uses for byte %u of page %d", x>>16, iPage); |
| 61743 | 61871 | break; |
| 61872 | + }else{ |
| 61873 | + cnt += (x>>16) - (prev&0xffff) - 1; |
| 61874 | + prev = x; |
| 61744 | 61875 | } |
| 61745 | 61876 | } |
| 61877 | + cnt += usableSize - (prev&0xffff) - 1; |
| 61746 | 61878 | /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments |
| 61747 | 61879 | ** is stored in the fifth field of the b-tree page header. |
| 61748 | 61880 | ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the |
| 61749 | 61881 | ** number of fragmented free bytes within the cell content area. |
| 61750 | 61882 | */ |
| 61751 | | - if( cnt!=data[hdr+7] ){ |
| 61883 | + if( heap[0]==0 && cnt!=data[hdr+7] ){ |
| 61752 | 61884 | checkAppendMsg(pCheck, |
| 61753 | 61885 | "Fragmentation of %d bytes reported as %d on page %d", |
| 61754 | 61886 | cnt, data[hdr+7], iPage); |
| 61755 | 61887 | } |
| 61756 | 61888 | } |
| 61757 | | - sqlite3PageFree(hit); |
| 61889 | + sqlite3PageFree(heap); |
| 61758 | 61890 | releasePage(pPage); |
| 61759 | 61891 | |
| 61760 | 61892 | end_of_check: |
| 61761 | 61893 | pCheck->zPfx = saved_zPfx; |
| 61762 | 61894 | pCheck->v1 = saved_v1; |
| | @@ -61816,12 +61948,11 @@ |
| 61816 | 61948 | sqlite3BtreeLeave(p); |
| 61817 | 61949 | return 0; |
| 61818 | 61950 | } |
| 61819 | 61951 | i = PENDING_BYTE_PAGE(pBt); |
| 61820 | 61952 | if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i); |
| 61821 | | - sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); |
| 61822 | | - sCheck.errMsg.useMalloc = 2; |
| 61953 | + sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); |
| 61823 | 61954 | |
| 61824 | 61955 | /* Check the integrity of the freelist |
| 61825 | 61956 | */ |
| 61826 | 61957 | sCheck.zPfx = "Main freelist: "; |
| 61827 | 61958 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| | @@ -63153,14 +63284,15 @@ |
| 63153 | 63284 | return SQLITE_NOMEM; |
| 63154 | 63285 | } |
| 63155 | 63286 | pMem->z[pMem->n] = 0; |
| 63156 | 63287 | pMem->z[pMem->n+1] = 0; |
| 63157 | 63288 | pMem->flags |= MEM_Term; |
| 63289 | + } |
| 63290 | + pMem->flags &= ~MEM_Ephem; |
| 63158 | 63291 | #ifdef SQLITE_DEBUG |
| 63159 | | - pMem->pScopyFrom = 0; |
| 63292 | + pMem->pScopyFrom = 0; |
| 63160 | 63293 | #endif |
| 63161 | | - } |
| 63162 | 63294 | |
| 63163 | 63295 | return SQLITE_OK; |
| 63164 | 63296 | } |
| 63165 | 63297 | |
| 63166 | 63298 | /* |
| | @@ -64600,11 +64732,11 @@ |
| 64600 | 64732 | int i; |
| 64601 | 64733 | int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField; |
| 64602 | 64734 | Mem *aMem = pRec->aMem; |
| 64603 | 64735 | sqlite3 *db = aMem[0].db; |
| 64604 | 64736 | for(i=0; i<nCol; i++){ |
| 64605 | | - if( aMem[i].szMalloc ) sqlite3DbFree(db, aMem[i].zMalloc); |
| 64737 | + sqlite3VdbeMemRelease(&aMem[i]); |
| 64606 | 64738 | } |
| 64607 | 64739 | sqlite3KeyInfoUnref(pRec->pKeyInfo); |
| 64608 | 64740 | sqlite3DbFree(db, pRec); |
| 64609 | 64741 | } |
| 64610 | 64742 | } |
| | @@ -66436,18 +66568,35 @@ |
| 66436 | 66568 | pVtabCursor->pVtab->nRef--; |
| 66437 | 66569 | pModule->xClose(pVtabCursor); |
| 66438 | 66570 | } |
| 66439 | 66571 | #endif |
| 66440 | 66572 | } |
| 66573 | + |
| 66574 | +/* |
| 66575 | +** Close all cursors in the current frame. |
| 66576 | +*/ |
| 66577 | +static void closeCursorsInFrame(Vdbe *p){ |
| 66578 | + if( p->apCsr ){ |
| 66579 | + int i; |
| 66580 | + for(i=0; i<p->nCursor; i++){ |
| 66581 | + VdbeCursor *pC = p->apCsr[i]; |
| 66582 | + if( pC ){ |
| 66583 | + sqlite3VdbeFreeCursor(p, pC); |
| 66584 | + p->apCsr[i] = 0; |
| 66585 | + } |
| 66586 | + } |
| 66587 | + } |
| 66588 | +} |
| 66441 | 66589 | |
| 66442 | 66590 | /* |
| 66443 | 66591 | ** Copy the values stored in the VdbeFrame structure to its Vdbe. This |
| 66444 | 66592 | ** is used, for example, when a trigger sub-program is halted to restore |
| 66445 | 66593 | ** control to the main program. |
| 66446 | 66594 | */ |
| 66447 | 66595 | SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){ |
| 66448 | 66596 | Vdbe *v = pFrame->v; |
| 66597 | + closeCursorsInFrame(v); |
| 66449 | 66598 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 66450 | 66599 | v->anExec = pFrame->anExec; |
| 66451 | 66600 | #endif |
| 66452 | 66601 | v->aOnceFlag = pFrame->aOnceFlag; |
| 66453 | 66602 | v->nOnceFlag = pFrame->nOnceFlag; |
| | @@ -66478,21 +66627,11 @@ |
| 66478 | 66627 | sqlite3VdbeFrameRestore(pFrame); |
| 66479 | 66628 | p->pFrame = 0; |
| 66480 | 66629 | p->nFrame = 0; |
| 66481 | 66630 | } |
| 66482 | 66631 | assert( p->nFrame==0 ); |
| 66483 | | - |
| 66484 | | - if( p->apCsr ){ |
| 66485 | | - int i; |
| 66486 | | - for(i=0; i<p->nCursor; i++){ |
| 66487 | | - VdbeCursor *pC = p->apCsr[i]; |
| 66488 | | - if( pC ){ |
| 66489 | | - sqlite3VdbeFreeCursor(p, pC); |
| 66490 | | - p->apCsr[i] = 0; |
| 66491 | | - } |
| 66492 | | - } |
| 66493 | | - } |
| 66632 | + closeCursorsInFrame(p); |
| 66494 | 66633 | if( p->aMem ){ |
| 66495 | 66634 | releaseMemArray(&p->aMem[1], p->nMem); |
| 66496 | 66635 | } |
| 66497 | 66636 | while( p->pDelFrame ){ |
| 66498 | 66637 | VdbeFrame *pDel = p->pDelFrame; |
| | @@ -68233,11 +68372,11 @@ |
| 68233 | 68372 | ** If database corruption is discovered, set pPKey2->errCode to |
| 68234 | 68373 | ** SQLITE_CORRUPT and return 0. If an OOM error is encountered, |
| 68235 | 68374 | ** pPKey2->errCode is set to SQLITE_NOMEM and, if it is not NULL, the |
| 68236 | 68375 | ** malloc-failed flag set on database handle (pPKey2->pKeyInfo->db). |
| 68237 | 68376 | */ |
| 68238 | | -static int vdbeRecordCompareWithSkip( |
| 68377 | +SQLITE_PRIVATE int sqlite3VdbeRecordCompareWithSkip( |
| 68239 | 68378 | int nKey1, const void *pKey1, /* Left key */ |
| 68240 | 68379 | UnpackedRecord *pPKey2, /* Right key */ |
| 68241 | 68380 | int bSkip /* If true, skip the first field */ |
| 68242 | 68381 | ){ |
| 68243 | 68382 | u32 d1; /* Offset into aKey[] of next data element */ |
| | @@ -68419,11 +68558,11 @@ |
| 68419 | 68558 | } |
| 68420 | 68559 | SQLITE_PRIVATE int sqlite3VdbeRecordCompare( |
| 68421 | 68560 | int nKey1, const void *pKey1, /* Left key */ |
| 68422 | 68561 | UnpackedRecord *pPKey2 /* Right key */ |
| 68423 | 68562 | ){ |
| 68424 | | - return vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0); |
| 68563 | + return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 0); |
| 68425 | 68564 | } |
| 68426 | 68565 | |
| 68427 | 68566 | |
| 68428 | 68567 | /* |
| 68429 | 68568 | ** This function is an optimized version of sqlite3VdbeRecordCompare() |
| | @@ -68507,11 +68646,11 @@ |
| 68507 | 68646 | }else if( v<lhs ){ |
| 68508 | 68647 | res = pPKey2->r2; |
| 68509 | 68648 | }else if( pPKey2->nField>1 ){ |
| 68510 | 68649 | /* The first fields of the two keys are equal. Compare the trailing |
| 68511 | 68650 | ** fields. */ |
| 68512 | | - res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
| 68651 | + res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
| 68513 | 68652 | }else{ |
| 68514 | 68653 | /* The first fields of the two keys are equal and there are no trailing |
| 68515 | 68654 | ** fields. Return pPKey2->default_rc in this case. */ |
| 68516 | 68655 | res = pPKey2->default_rc; |
| 68517 | 68656 | } |
| | @@ -68555,11 +68694,11 @@ |
| 68555 | 68694 | |
| 68556 | 68695 | if( res==0 ){ |
| 68557 | 68696 | res = nStr - pPKey2->aMem[0].n; |
| 68558 | 68697 | if( res==0 ){ |
| 68559 | 68698 | if( pPKey2->nField>1 ){ |
| 68560 | | - res = vdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
| 68699 | + res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1); |
| 68561 | 68700 | }else{ |
| 68562 | 68701 | res = pPKey2->default_rc; |
| 68563 | 68702 | } |
| 68564 | 68703 | }else if( res>0 ){ |
| 68565 | 68704 | res = pPKey2->r2; |
| | @@ -70497,21 +70636,22 @@ |
| 70497 | 70636 | Mem *pVar; /* Value of a host parameter */ |
| 70498 | 70637 | StrAccum out; /* Accumulate the output here */ |
| 70499 | 70638 | char zBase[100]; /* Initial working space */ |
| 70500 | 70639 | |
| 70501 | 70640 | db = p->db; |
| 70502 | | - sqlite3StrAccumInit(&out, zBase, sizeof(zBase), |
| 70641 | + sqlite3StrAccumInit(&out, db, zBase, sizeof(zBase), |
| 70503 | 70642 | db->aLimit[SQLITE_LIMIT_LENGTH]); |
| 70504 | | - out.db = db; |
| 70505 | 70643 | if( db->nVdbeExec>1 ){ |
| 70506 | 70644 | while( *zRawSql ){ |
| 70507 | 70645 | const char *zStart = zRawSql; |
| 70508 | 70646 | while( *(zRawSql++)!='\n' && *zRawSql ); |
| 70509 | 70647 | sqlite3StrAccumAppend(&out, "-- ", 3); |
| 70510 | 70648 | assert( (zRawSql - zStart) > 0 ); |
| 70511 | 70649 | sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart)); |
| 70512 | 70650 | } |
| 70651 | + }else if( p->nVar==0 ){ |
| 70652 | + sqlite3StrAccumAppend(&out, zRawSql, sqlite3Strlen30(zRawSql)); |
| 70513 | 70653 | }else{ |
| 70514 | 70654 | while( zRawSql[0] ){ |
| 70515 | 70655 | n = findNextHostParameter(zRawSql, &nToken); |
| 70516 | 70656 | assert( n>0 ); |
| 70517 | 70657 | sqlite3StrAccumAppend(&out, zRawSql, n); |
| | @@ -70524,14 +70664,16 @@ |
| 70524 | 70664 | sqlite3GetInt32(&zRawSql[1], &idx); |
| 70525 | 70665 | }else{ |
| 70526 | 70666 | idx = nextIndex; |
| 70527 | 70667 | } |
| 70528 | 70668 | }else{ |
| 70529 | | - assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' ); |
| 70669 | + assert( zRawSql[0]==':' || zRawSql[0]=='$' || |
| 70670 | + zRawSql[0]=='@' || zRawSql[0]=='#' ); |
| 70530 | 70671 | testcase( zRawSql[0]==':' ); |
| 70531 | 70672 | testcase( zRawSql[0]=='$' ); |
| 70532 | 70673 | testcase( zRawSql[0]=='@' ); |
| 70674 | + testcase( zRawSql[0]=='#' ); |
| 70533 | 70675 | idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken); |
| 70534 | 70676 | assert( idx>0 ); |
| 70535 | 70677 | } |
| 70536 | 70678 | zRawSql += nToken; |
| 70537 | 70679 | nextIndex = idx + 1; |
| | @@ -70895,10 +71037,11 @@ |
| 70895 | 71037 | ** representation. |
| 70896 | 71038 | */ |
| 70897 | 71039 | if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){ |
| 70898 | 71040 | sqlite3VdbeMemStringify(pRec, enc, 1); |
| 70899 | 71041 | } |
| 71042 | + pRec->flags &= ~(MEM_Real|MEM_Int); |
| 70900 | 71043 | } |
| 70901 | 71044 | } |
| 70902 | 71045 | |
| 70903 | 71046 | /* |
| 70904 | 71047 | ** Try to convert the type of a function argument or a result column |
| | @@ -71202,21 +71345,38 @@ |
| 71202 | 71345 | assert( n==(db->nSavepoint + db->isTransactionSavepoint) ); |
| 71203 | 71346 | return 1; |
| 71204 | 71347 | } |
| 71205 | 71348 | #endif |
| 71206 | 71349 | |
| 71350 | +/* |
| 71351 | +** Return the register of pOp->p2 after first preparing it to be |
| 71352 | +** overwritten with an integer value. |
| 71353 | +*/ |
| 71354 | +static Mem *out2Prerelease(Vdbe *p, VdbeOp *pOp){ |
| 71355 | + Mem *pOut; |
| 71356 | + assert( pOp->p2>0 ); |
| 71357 | + assert( pOp->p2<=(p->nMem-p->nCursor) ); |
| 71358 | + pOut = &p->aMem[pOp->p2]; |
| 71359 | + memAboutToChange(p, pOut); |
| 71360 | + if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut); |
| 71361 | + pOut->flags = MEM_Int; |
| 71362 | + return pOut; |
| 71363 | +} |
| 71364 | + |
| 71207 | 71365 | |
| 71208 | 71366 | /* |
| 71209 | 71367 | ** Execute as much of a VDBE program as we can. |
| 71210 | 71368 | ** This is the core of sqlite3_step(). |
| 71211 | 71369 | */ |
| 71212 | 71370 | SQLITE_PRIVATE int sqlite3VdbeExec( |
| 71213 | 71371 | Vdbe *p /* The VDBE */ |
| 71214 | 71372 | ){ |
| 71215 | | - int pc=0; /* The program counter */ |
| 71216 | 71373 | Op *aOp = p->aOp; /* Copy of p->aOp */ |
| 71217 | | - Op *pOp; /* Current operation */ |
| 71374 | + Op *pOp = aOp; /* Current operation */ |
| 71375 | +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 71376 | + Op *pOrigOp; /* Value of pOp at the top of the loop */ |
| 71377 | +#endif |
| 71218 | 71378 | int rc = SQLITE_OK; /* Value to return */ |
| 71219 | 71379 | sqlite3 *db = p->db; /* The database */ |
| 71220 | 71380 | u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */ |
| 71221 | 71381 | u8 encoding = ENC(db); /* The database encoding */ |
| 71222 | 71382 | int iCompare = 0; /* Result of last OP_Compare operation */ |
| | @@ -71288,27 +71448,26 @@ |
| 71288 | 71448 | } |
| 71289 | 71449 | if( p->db->flags & SQLITE_VdbeTrace ) printf("VDBE Trace:\n"); |
| 71290 | 71450 | } |
| 71291 | 71451 | sqlite3EndBenignMalloc(); |
| 71292 | 71452 | #endif |
| 71293 | | - for(pc=p->pc; rc==SQLITE_OK; pc++){ |
| 71294 | | - assert( pc>=0 && pc<p->nOp ); |
| 71453 | + for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){ |
| 71454 | + assert( pOp>=aOp && pOp<&aOp[p->nOp]); |
| 71295 | 71455 | if( db->mallocFailed ) goto no_mem; |
| 71296 | 71456 | #ifdef VDBE_PROFILE |
| 71297 | 71457 | start = sqlite3Hwtime(); |
| 71298 | 71458 | #endif |
| 71299 | 71459 | nVmStep++; |
| 71300 | | - pOp = &aOp[pc]; |
| 71301 | 71460 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 71302 | | - if( p->anExec ) p->anExec[pc]++; |
| 71461 | + if( p->anExec ) p->anExec[(int)(pOp-aOp)]++; |
| 71303 | 71462 | #endif |
| 71304 | 71463 | |
| 71305 | 71464 | /* Only allow tracing if SQLITE_DEBUG is defined. |
| 71306 | 71465 | */ |
| 71307 | 71466 | #ifdef SQLITE_DEBUG |
| 71308 | 71467 | if( db->flags & SQLITE_VdbeTrace ){ |
| 71309 | | - sqlite3VdbePrintOp(stdout, pc, pOp); |
| 71468 | + sqlite3VdbePrintOp(stdout, (int)(pOp - aOp), pOp); |
| 71310 | 71469 | } |
| 71311 | 71470 | #endif |
| 71312 | 71471 | |
| 71313 | 71472 | |
| 71314 | 71473 | /* Check to see if we need to simulate an interrupt. This only happens |
| | @@ -71321,27 +71480,13 @@ |
| 71321 | 71480 | sqlite3_interrupt(db); |
| 71322 | 71481 | } |
| 71323 | 71482 | } |
| 71324 | 71483 | #endif |
| 71325 | 71484 | |
| 71326 | | - /* On any opcode with the "out2-prerelease" tag, free any |
| 71327 | | - ** external allocations out of mem[p2] and set mem[p2] to be |
| 71328 | | - ** an undefined integer. Opcodes will either fill in the integer |
| 71329 | | - ** value or convert mem[p2] to a different type. |
| 71330 | | - */ |
| 71331 | | - assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] ); |
| 71332 | | - if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){ |
| 71333 | | - assert( pOp->p2>0 ); |
| 71334 | | - assert( pOp->p2<=(p->nMem-p->nCursor) ); |
| 71335 | | - pOut = &aMem[pOp->p2]; |
| 71336 | | - memAboutToChange(p, pOut); |
| 71337 | | - if( VdbeMemDynamic(pOut) ) sqlite3VdbeMemSetNull(pOut); |
| 71338 | | - pOut->flags = MEM_Int; |
| 71339 | | - } |
| 71340 | | - |
| 71341 | 71485 | /* Sanity checking on other operands */ |
| 71342 | 71486 | #ifdef SQLITE_DEBUG |
| 71487 | + assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] ); |
| 71343 | 71488 | if( (pOp->opflags & OPFLG_IN1)!=0 ){ |
| 71344 | 71489 | assert( pOp->p1>0 ); |
| 71345 | 71490 | assert( pOp->p1<=(p->nMem-p->nCursor) ); |
| 71346 | 71491 | assert( memIsValid(&aMem[pOp->p1]) ); |
| 71347 | 71492 | assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) ); |
| | @@ -71370,10 +71515,13 @@ |
| 71370 | 71515 | assert( pOp->p3>0 ); |
| 71371 | 71516 | assert( pOp->p3<=(p->nMem-p->nCursor) ); |
| 71372 | 71517 | memAboutToChange(p, &aMem[pOp->p3]); |
| 71373 | 71518 | } |
| 71374 | 71519 | #endif |
| 71520 | +#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE) |
| 71521 | + pOrigOp = pOp; |
| 71522 | +#endif |
| 71375 | 71523 | |
| 71376 | 71524 | switch( pOp->opcode ){ |
| 71377 | 71525 | |
| 71378 | 71526 | /***************************************************************************** |
| 71379 | 71527 | ** What follows is a massive switch statement where each case implements a |
| | @@ -71393,11 +71541,11 @@ |
| 71393 | 71541 | ** case statement is followed by a comment of the form "/# same as ... #/" |
| 71394 | 71542 | ** that comment is used to determine the particular value of the opcode. |
| 71395 | 71543 | ** |
| 71396 | 71544 | ** Other keywords in the comment that follows each case are used to |
| 71397 | 71545 | ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[]. |
| 71398 | | -** Keywords include: in1, in2, in3, out2_prerelease, out2, out3. See |
| 71546 | +** Keywords include: in1, in2, in3, out2, out3. See |
| 71399 | 71547 | ** the mkopcodeh.awk script for additional information. |
| 71400 | 71548 | ** |
| 71401 | 71549 | ** Documentation about VDBE opcodes is generated by scanning this file |
| 71402 | 71550 | ** for lines of that contain "Opcode:". That line and all subsequent |
| 71403 | 71551 | ** comment lines are used in the generation of the opcode.html documentation |
| | @@ -71421,11 +71569,12 @@ |
| 71421 | 71569 | ** is sometimes set to 1 instead of 0 as a hint to the command-line shell |
| 71422 | 71570 | ** that this Goto is the bottom of a loop and that the lines from P2 down |
| 71423 | 71571 | ** to the current line should be indented for EXPLAIN output. |
| 71424 | 71572 | */ |
| 71425 | 71573 | case OP_Goto: { /* jump */ |
| 71426 | | - pc = pOp->p2 - 1; |
| 71574 | +jump_to_p2_and_check_for_interrupt: |
| 71575 | + pOp = &aOp[pOp->p2 - 1]; |
| 71427 | 71576 | |
| 71428 | 71577 | /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev, |
| 71429 | 71578 | ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon |
| 71430 | 71579 | ** completion. Check to see if sqlite3_interrupt() has been called |
| 71431 | 71580 | ** or if the progress callback needs to be invoked. |
| | @@ -71466,13 +71615,17 @@ |
| 71466 | 71615 | assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) ); |
| 71467 | 71616 | pIn1 = &aMem[pOp->p1]; |
| 71468 | 71617 | assert( VdbeMemDynamic(pIn1)==0 ); |
| 71469 | 71618 | memAboutToChange(p, pIn1); |
| 71470 | 71619 | pIn1->flags = MEM_Int; |
| 71471 | | - pIn1->u.i = pc; |
| 71620 | + pIn1->u.i = (int)(pOp-aOp); |
| 71472 | 71621 | REGISTER_TRACE(pOp->p1, pIn1); |
| 71473 | | - pc = pOp->p2 - 1; |
| 71622 | + |
| 71623 | + /* Most jump operations do a goto to this spot in order to update |
| 71624 | + ** the pOp pointer. */ |
| 71625 | +jump_to_p2: |
| 71626 | + pOp = &aOp[pOp->p2 - 1]; |
| 71474 | 71627 | break; |
| 71475 | 71628 | } |
| 71476 | 71629 | |
| 71477 | 71630 | /* Opcode: Return P1 * * * * |
| 71478 | 71631 | ** |
| | @@ -71480,11 +71633,11 @@ |
| 71480 | 71633 | ** the jump, register P1 becomes undefined. |
| 71481 | 71634 | */ |
| 71482 | 71635 | case OP_Return: { /* in1 */ |
| 71483 | 71636 | pIn1 = &aMem[pOp->p1]; |
| 71484 | 71637 | assert( pIn1->flags==MEM_Int ); |
| 71485 | | - pc = (int)pIn1->u.i; |
| 71638 | + pOp = &aOp[pIn1->u.i]; |
| 71486 | 71639 | pIn1->flags = MEM_Undefined; |
| 71487 | 71640 | break; |
| 71488 | 71641 | } |
| 71489 | 71642 | |
| 71490 | 71643 | /* Opcode: InitCoroutine P1 P2 P3 * * |
| | @@ -71504,11 +71657,11 @@ |
| 71504 | 71657 | assert( pOp->p3>=0 && pOp->p3<p->nOp ); |
| 71505 | 71658 | pOut = &aMem[pOp->p1]; |
| 71506 | 71659 | assert( !VdbeMemDynamic(pOut) ); |
| 71507 | 71660 | pOut->u.i = pOp->p3 - 1; |
| 71508 | 71661 | pOut->flags = MEM_Int; |
| 71509 | | - if( pOp->p2 ) pc = pOp->p2 - 1; |
| 71662 | + if( pOp->p2 ) goto jump_to_p2; |
| 71510 | 71663 | break; |
| 71511 | 71664 | } |
| 71512 | 71665 | |
| 71513 | 71666 | /* Opcode: EndCoroutine P1 * * * * |
| 71514 | 71667 | ** |
| | @@ -71524,11 +71677,11 @@ |
| 71524 | 71677 | assert( pIn1->flags==MEM_Int ); |
| 71525 | 71678 | assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp ); |
| 71526 | 71679 | pCaller = &aOp[pIn1->u.i]; |
| 71527 | 71680 | assert( pCaller->opcode==OP_Yield ); |
| 71528 | 71681 | assert( pCaller->p2>=0 && pCaller->p2<p->nOp ); |
| 71529 | | - pc = pCaller->p2 - 1; |
| 71682 | + pOp = &aOp[pCaller->p2 - 1]; |
| 71530 | 71683 | pIn1->flags = MEM_Undefined; |
| 71531 | 71684 | break; |
| 71532 | 71685 | } |
| 71533 | 71686 | |
| 71534 | 71687 | /* Opcode: Yield P1 P2 * * * |
| | @@ -71548,13 +71701,13 @@ |
| 71548 | 71701 | int pcDest; |
| 71549 | 71702 | pIn1 = &aMem[pOp->p1]; |
| 71550 | 71703 | assert( VdbeMemDynamic(pIn1)==0 ); |
| 71551 | 71704 | pIn1->flags = MEM_Int; |
| 71552 | 71705 | pcDest = (int)pIn1->u.i; |
| 71553 | | - pIn1->u.i = pc; |
| 71706 | + pIn1->u.i = (int)(pOp - aOp); |
| 71554 | 71707 | REGISTER_TRACE(pOp->p1, pIn1); |
| 71555 | | - pc = pcDest; |
| 71708 | + pOp = &aOp[pcDest]; |
| 71556 | 71709 | break; |
| 71557 | 71710 | } |
| 71558 | 71711 | |
| 71559 | 71712 | /* Opcode: HaltIfNull P1 P2 P3 P4 P5 |
| 71560 | 71713 | ** Synopsis: if r[P3]=null halt |
| | @@ -71601,34 +71754,38 @@ |
| 71601 | 71754 | ** is the same as executing Halt. |
| 71602 | 71755 | */ |
| 71603 | 71756 | case OP_Halt: { |
| 71604 | 71757 | const char *zType; |
| 71605 | 71758 | const char *zLogFmt; |
| 71759 | + VdbeFrame *pFrame; |
| 71760 | + int pcx; |
| 71606 | 71761 | |
| 71762 | + pcx = (int)(pOp - aOp); |
| 71607 | 71763 | if( pOp->p1==SQLITE_OK && p->pFrame ){ |
| 71608 | 71764 | /* Halt the sub-program. Return control to the parent frame. */ |
| 71609 | | - VdbeFrame *pFrame = p->pFrame; |
| 71765 | + pFrame = p->pFrame; |
| 71610 | 71766 | p->pFrame = pFrame->pParent; |
| 71611 | 71767 | p->nFrame--; |
| 71612 | 71768 | sqlite3VdbeSetChanges(db, p->nChange); |
| 71613 | | - pc = sqlite3VdbeFrameRestore(pFrame); |
| 71769 | + pcx = sqlite3VdbeFrameRestore(pFrame); |
| 71614 | 71770 | lastRowid = db->lastRowid; |
| 71615 | 71771 | if( pOp->p2==OE_Ignore ){ |
| 71616 | | - /* Instruction pc is the OP_Program that invoked the sub-program |
| 71772 | + /* Instruction pcx is the OP_Program that invoked the sub-program |
| 71617 | 71773 | ** currently being halted. If the p2 instruction of this OP_Halt |
| 71618 | 71774 | ** instruction is set to OE_Ignore, then the sub-program is throwing |
| 71619 | 71775 | ** an IGNORE exception. In this case jump to the address specified |
| 71620 | 71776 | ** as the p2 of the calling OP_Program. */ |
| 71621 | | - pc = p->aOp[pc].p2-1; |
| 71777 | + pcx = p->aOp[pcx].p2-1; |
| 71622 | 71778 | } |
| 71623 | 71779 | aOp = p->aOp; |
| 71624 | 71780 | aMem = p->aMem; |
| 71781 | + pOp = &aOp[pcx]; |
| 71625 | 71782 | break; |
| 71626 | 71783 | } |
| 71627 | 71784 | p->rc = pOp->p1; |
| 71628 | 71785 | p->errorAction = (u8)pOp->p2; |
| 71629 | | - p->pc = pc; |
| 71786 | + p->pc = pcx; |
| 71630 | 71787 | if( p->rc ){ |
| 71631 | 71788 | if( pOp->p5 ){ |
| 71632 | 71789 | static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK", |
| 71633 | 71790 | "FOREIGN KEY" }; |
| 71634 | 71791 | assert( pOp->p5>=1 && pOp->p5<=4 ); |
| | @@ -71648,11 +71805,11 @@ |
| 71648 | 71805 | }else if( pOp->p4.z ){ |
| 71649 | 71806 | sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z); |
| 71650 | 71807 | }else{ |
| 71651 | 71808 | sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType); |
| 71652 | 71809 | } |
| 71653 | | - sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg); |
| 71810 | + sqlite3_log(pOp->p1, zLogFmt, pcx, p->zSql, p->zErrMsg); |
| 71654 | 71811 | } |
| 71655 | 71812 | rc = sqlite3VdbeHalt(p); |
| 71656 | 71813 | assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR ); |
| 71657 | 71814 | if( rc==SQLITE_BUSY ){ |
| 71658 | 71815 | p->rc = rc = SQLITE_BUSY; |
| | @@ -71667,11 +71824,12 @@ |
| 71667 | 71824 | /* Opcode: Integer P1 P2 * * * |
| 71668 | 71825 | ** Synopsis: r[P2]=P1 |
| 71669 | 71826 | ** |
| 71670 | 71827 | ** The 32-bit integer value P1 is written into register P2. |
| 71671 | 71828 | */ |
| 71672 | | -case OP_Integer: { /* out2-prerelease */ |
| 71829 | +case OP_Integer: { /* out2 */ |
| 71830 | + pOut = out2Prerelease(p, pOp); |
| 71673 | 71831 | pOut->u.i = pOp->p1; |
| 71674 | 71832 | break; |
| 71675 | 71833 | } |
| 71676 | 71834 | |
| 71677 | 71835 | /* Opcode: Int64 * P2 * P4 * |
| | @@ -71678,11 +71836,12 @@ |
| 71678 | 71836 | ** Synopsis: r[P2]=P4 |
| 71679 | 71837 | ** |
| 71680 | 71838 | ** P4 is a pointer to a 64-bit integer value. |
| 71681 | 71839 | ** Write that value into register P2. |
| 71682 | 71840 | */ |
| 71683 | | -case OP_Int64: { /* out2-prerelease */ |
| 71841 | +case OP_Int64: { /* out2 */ |
| 71842 | + pOut = out2Prerelease(p, pOp); |
| 71684 | 71843 | assert( pOp->p4.pI64!=0 ); |
| 71685 | 71844 | pOut->u.i = *pOp->p4.pI64; |
| 71686 | 71845 | break; |
| 71687 | 71846 | } |
| 71688 | 71847 | |
| | @@ -71691,11 +71850,12 @@ |
| 71691 | 71850 | ** Synopsis: r[P2]=P4 |
| 71692 | 71851 | ** |
| 71693 | 71852 | ** P4 is a pointer to a 64-bit floating point value. |
| 71694 | 71853 | ** Write that value into register P2. |
| 71695 | 71854 | */ |
| 71696 | | -case OP_Real: { /* same as TK_FLOAT, out2-prerelease */ |
| 71855 | +case OP_Real: { /* same as TK_FLOAT, out2 */ |
| 71856 | + pOut = out2Prerelease(p, pOp); |
| 71697 | 71857 | pOut->flags = MEM_Real; |
| 71698 | 71858 | assert( !sqlite3IsNaN(*pOp->p4.pReal) ); |
| 71699 | 71859 | pOut->u.r = *pOp->p4.pReal; |
| 71700 | 71860 | break; |
| 71701 | 71861 | } |
| | @@ -71707,12 +71867,13 @@ |
| 71707 | 71867 | ** P4 points to a nul terminated UTF-8 string. This opcode is transformed |
| 71708 | 71868 | ** into a String opcode before it is executed for the first time. During |
| 71709 | 71869 | ** this transformation, the length of string P4 is computed and stored |
| 71710 | 71870 | ** as the P1 parameter. |
| 71711 | 71871 | */ |
| 71712 | | -case OP_String8: { /* same as TK_STRING, out2-prerelease */ |
| 71872 | +case OP_String8: { /* same as TK_STRING, out2 */ |
| 71713 | 71873 | assert( pOp->p4.z!=0 ); |
| 71874 | + pOut = out2Prerelease(p, pOp); |
| 71714 | 71875 | pOp->opcode = OP_String; |
| 71715 | 71876 | pOp->p1 = sqlite3Strlen30(pOp->p4.z); |
| 71716 | 71877 | |
| 71717 | 71878 | #ifndef SQLITE_OMIT_UTF16 |
| 71718 | 71879 | if( encoding!=SQLITE_UTF8 ){ |
| | @@ -71745,12 +71906,13 @@ |
| 71745 | 71906 | ** If P5!=0 and the content of register P3 is greater than zero, then |
| 71746 | 71907 | ** the datatype of the register P2 is converted to BLOB. The content is |
| 71747 | 71908 | ** the same sequence of bytes, it is merely interpreted as a BLOB instead |
| 71748 | 71909 | ** of a string, as if it had been CAST. |
| 71749 | 71910 | */ |
| 71750 | | -case OP_String: { /* out2-prerelease */ |
| 71911 | +case OP_String: { /* out2 */ |
| 71751 | 71912 | assert( pOp->p4.z!=0 ); |
| 71913 | + pOut = out2Prerelease(p, pOp); |
| 71752 | 71914 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 71753 | 71915 | pOut->z = pOp->p4.z; |
| 71754 | 71916 | pOut->n = pOp->p1; |
| 71755 | 71917 | pOut->enc = encoding; |
| 71756 | 71918 | UPDATE_MAX_BLOBSIZE(pOut); |
| | @@ -71774,13 +71936,14 @@ |
| 71774 | 71936 | ** |
| 71775 | 71937 | ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that |
| 71776 | 71938 | ** NULL values will not compare equal even if SQLITE_NULLEQ is set on |
| 71777 | 71939 | ** OP_Ne or OP_Eq. |
| 71778 | 71940 | */ |
| 71779 | | -case OP_Null: { /* out2-prerelease */ |
| 71941 | +case OP_Null: { /* out2 */ |
| 71780 | 71942 | int cnt; |
| 71781 | 71943 | u16 nullFlag; |
| 71944 | + pOut = out2Prerelease(p, pOp); |
| 71782 | 71945 | cnt = pOp->p3-pOp->p2; |
| 71783 | 71946 | assert( pOp->p3<=(p->nMem-p->nCursor) ); |
| 71784 | 71947 | pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null; |
| 71785 | 71948 | while( cnt>0 ){ |
| 71786 | 71949 | pOut++; |
| | @@ -71811,12 +71974,13 @@ |
| 71811 | 71974 | ** Synopsis: r[P2]=P4 (len=P1) |
| 71812 | 71975 | ** |
| 71813 | 71976 | ** P4 points to a blob of data P1 bytes long. Store this |
| 71814 | 71977 | ** blob in register P2. |
| 71815 | 71978 | */ |
| 71816 | | -case OP_Blob: { /* out2-prerelease */ |
| 71979 | +case OP_Blob: { /* out2 */ |
| 71817 | 71980 | assert( pOp->p1 <= SQLITE_MAX_LENGTH ); |
| 71981 | + pOut = out2Prerelease(p, pOp); |
| 71818 | 71982 | sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0); |
| 71819 | 71983 | pOut->enc = encoding; |
| 71820 | 71984 | UPDATE_MAX_BLOBSIZE(pOut); |
| 71821 | 71985 | break; |
| 71822 | 71986 | } |
| | @@ -71827,19 +71991,20 @@ |
| 71827 | 71991 | ** Transfer the values of bound parameter P1 into register P2 |
| 71828 | 71992 | ** |
| 71829 | 71993 | ** If the parameter is named, then its name appears in P4. |
| 71830 | 71994 | ** The P4 value is used by sqlite3_bind_parameter_name(). |
| 71831 | 71995 | */ |
| 71832 | | -case OP_Variable: { /* out2-prerelease */ |
| 71996 | +case OP_Variable: { /* out2 */ |
| 71833 | 71997 | Mem *pVar; /* Value being transferred */ |
| 71834 | 71998 | |
| 71835 | 71999 | assert( pOp->p1>0 && pOp->p1<=p->nVar ); |
| 71836 | 72000 | assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] ); |
| 71837 | 72001 | pVar = &p->aVar[pOp->p1 - 1]; |
| 71838 | 72002 | if( sqlite3VdbeMemTooBig(pVar) ){ |
| 71839 | 72003 | goto too_big; |
| 71840 | 72004 | } |
| 72005 | + pOut = out2Prerelease(p, pOp); |
| 71841 | 72006 | sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static); |
| 71842 | 72007 | UPDATE_MAX_BLOBSIZE(pOut); |
| 71843 | 72008 | break; |
| 71844 | 72009 | } |
| 71845 | 72010 | |
| | @@ -71870,14 +72035,15 @@ |
| 71870 | 72035 | assert( pIn1<=&aMem[(p->nMem-p->nCursor)] ); |
| 71871 | 72036 | assert( memIsValid(pIn1) ); |
| 71872 | 72037 | memAboutToChange(p, pOut); |
| 71873 | 72038 | sqlite3VdbeMemMove(pOut, pIn1); |
| 71874 | 72039 | #ifdef SQLITE_DEBUG |
| 71875 | | - if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){ |
| 71876 | | - pOut->pScopyFrom += p1 - pOp->p2; |
| 72040 | + if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<pOut ){ |
| 72041 | + pOut->pScopyFrom += pOp->p2 - p1; |
| 71877 | 72042 | } |
| 71878 | 72043 | #endif |
| 72044 | + Deephemeralize(pOut); |
| 71879 | 72045 | REGISTER_TRACE(p2++, pOut); |
| 71880 | 72046 | pIn1++; |
| 71881 | 72047 | pOut++; |
| 71882 | 72048 | }while( --n ); |
| 71883 | 72049 | break; |
| | @@ -72012,11 +72178,11 @@ |
| 72012 | 72178 | } |
| 72013 | 72179 | if( db->mallocFailed ) goto no_mem; |
| 72014 | 72180 | |
| 72015 | 72181 | /* Return SQLITE_ROW |
| 72016 | 72182 | */ |
| 72017 | | - p->pc = pc + 1; |
| 72183 | + p->pc = (int)(pOp - aOp) + 1; |
| 72018 | 72184 | rc = SQLITE_ROW; |
| 72019 | 72185 | goto vdbe_return; |
| 72020 | 72186 | } |
| 72021 | 72187 | |
| 72022 | 72188 | /* Opcode: Concat P1 P2 P3 * * |
| | @@ -72258,11 +72424,11 @@ |
| 72258 | 72424 | REGISTER_TRACE(pOp->p2+i, pArg); |
| 72259 | 72425 | } |
| 72260 | 72426 | |
| 72261 | 72427 | assert( pOp->p4type==P4_FUNCDEF ); |
| 72262 | 72428 | ctx.pFunc = pOp->p4.pFunc; |
| 72263 | | - ctx.iOp = pc; |
| 72429 | + ctx.iOp = (int)(pOp - aOp); |
| 72264 | 72430 | ctx.pVdbe = p; |
| 72265 | 72431 | MemSetTypeFlag(ctx.pOut, MEM_Null); |
| 72266 | 72432 | ctx.fErrorOrAux = 0; |
| 72267 | 72433 | db->lastRowid = lastRowid; |
| 72268 | 72434 | (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
| | @@ -72272,11 +72438,11 @@ |
| 72272 | 72438 | if( ctx.fErrorOrAux ){ |
| 72273 | 72439 | if( ctx.isError ){ |
| 72274 | 72440 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut)); |
| 72275 | 72441 | rc = ctx.isError; |
| 72276 | 72442 | } |
| 72277 | | - sqlite3VdbeDeleteAuxData(p, pc, pOp->p1); |
| 72443 | + sqlite3VdbeDeleteAuxData(p, (int)(pOp - aOp), pOp->p1); |
| 72278 | 72444 | } |
| 72279 | 72445 | |
| 72280 | 72446 | /* Copy the result of the function into register P3 */ |
| 72281 | 72447 | sqlite3VdbeChangeEncoding(ctx.pOut, encoding); |
| 72282 | 72448 | if( sqlite3VdbeMemTooBig(ctx.pOut) ){ |
| | @@ -72401,12 +72567,11 @@ |
| 72401 | 72567 | if( (pIn1->flags & MEM_Int)==0 ){ |
| 72402 | 72568 | if( pOp->p2==0 ){ |
| 72403 | 72569 | rc = SQLITE_MISMATCH; |
| 72404 | 72570 | goto abort_due_to_error; |
| 72405 | 72571 | }else{ |
| 72406 | | - pc = pOp->p2 - 1; |
| 72407 | | - break; |
| 72572 | + goto jump_to_p2; |
| 72408 | 72573 | } |
| 72409 | 72574 | } |
| 72410 | 72575 | } |
| 72411 | 72576 | MemSetTypeFlag(pIn1, MEM_Int); |
| 72412 | 72577 | break; |
| | @@ -72588,11 +72753,11 @@ |
| 72588 | 72753 | MemSetTypeFlag(pOut, MEM_Null); |
| 72589 | 72754 | REGISTER_TRACE(pOp->p2, pOut); |
| 72590 | 72755 | }else{ |
| 72591 | 72756 | VdbeBranchTaken(2,3); |
| 72592 | 72757 | if( pOp->p5 & SQLITE_JUMPIFNULL ){ |
| 72593 | | - pc = pOp->p2-1; |
| 72758 | + goto jump_to_p2; |
| 72594 | 72759 | } |
| 72595 | 72760 | } |
| 72596 | 72761 | break; |
| 72597 | 72762 | } |
| 72598 | 72763 | }else{ |
| | @@ -72639,10 +72804,16 @@ |
| 72639 | 72804 | case OP_Lt: res = res<0; break; |
| 72640 | 72805 | case OP_Le: res = res<=0; break; |
| 72641 | 72806 | case OP_Gt: res = res>0; break; |
| 72642 | 72807 | default: res = res>=0; break; |
| 72643 | 72808 | } |
| 72809 | + |
| 72810 | + /* Undo any changes made by applyAffinity() to the input registers. */ |
| 72811 | + assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 72812 | + pIn1->flags = flags1; |
| 72813 | + assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); |
| 72814 | + pIn3->flags = flags3; |
| 72644 | 72815 | |
| 72645 | 72816 | if( pOp->p5 & SQLITE_STOREP2 ){ |
| 72646 | 72817 | pOut = &aMem[pOp->p2]; |
| 72647 | 72818 | memAboutToChange(p, pOut); |
| 72648 | 72819 | MemSetTypeFlag(pOut, MEM_Int); |
| | @@ -72649,18 +72820,13 @@ |
| 72649 | 72820 | pOut->u.i = res; |
| 72650 | 72821 | REGISTER_TRACE(pOp->p2, pOut); |
| 72651 | 72822 | }else{ |
| 72652 | 72823 | VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3); |
| 72653 | 72824 | if( res ){ |
| 72654 | | - pc = pOp->p2-1; |
| 72825 | + goto jump_to_p2; |
| 72655 | 72826 | } |
| 72656 | 72827 | } |
| 72657 | | - /* Undo any changes made by applyAffinity() to the input registers. */ |
| 72658 | | - assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) ); |
| 72659 | | - pIn1->flags = flags1; |
| 72660 | | - assert( (pIn3->flags & MEM_Dyn) == (flags3 & MEM_Dyn) ); |
| 72661 | | - pIn3->flags = flags3; |
| 72662 | 72828 | break; |
| 72663 | 72829 | } |
| 72664 | 72830 | |
| 72665 | 72831 | /* Opcode: Permutation * * * P4 * |
| 72666 | 72832 | ** |
| | @@ -72751,15 +72917,15 @@ |
| 72751 | 72917 | ** in the most recent OP_Compare instruction the P1 vector was less than |
| 72752 | 72918 | ** equal to, or greater than the P2 vector, respectively. |
| 72753 | 72919 | */ |
| 72754 | 72920 | case OP_Jump: { /* jump */ |
| 72755 | 72921 | if( iCompare<0 ){ |
| 72756 | | - pc = pOp->p1 - 1; VdbeBranchTaken(0,3); |
| 72922 | + VdbeBranchTaken(0,3); pOp = &aOp[pOp->p1 - 1]; |
| 72757 | 72923 | }else if( iCompare==0 ){ |
| 72758 | | - pc = pOp->p2 - 1; VdbeBranchTaken(1,3); |
| 72924 | + VdbeBranchTaken(1,3); pOp = &aOp[pOp->p2 - 1]; |
| 72759 | 72925 | }else{ |
| 72760 | | - pc = pOp->p3 - 1; VdbeBranchTaken(2,3); |
| 72926 | + VdbeBranchTaken(2,3); pOp = &aOp[pOp->p3 - 1]; |
| 72761 | 72927 | } |
| 72762 | 72928 | break; |
| 72763 | 72929 | } |
| 72764 | 72930 | |
| 72765 | 72931 | /* Opcode: And P1 P2 P3 * * |
| | @@ -72865,11 +73031,11 @@ |
| 72865 | 73031 | */ |
| 72866 | 73032 | case OP_Once: { /* jump */ |
| 72867 | 73033 | assert( pOp->p1<p->nOnceFlag ); |
| 72868 | 73034 | VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2); |
| 72869 | 73035 | if( p->aOnceFlag[pOp->p1] ){ |
| 72870 | | - pc = pOp->p2-1; |
| 73036 | + goto jump_to_p2; |
| 72871 | 73037 | }else{ |
| 72872 | 73038 | p->aOnceFlag[pOp->p1] = 1; |
| 72873 | 73039 | } |
| 72874 | 73040 | break; |
| 72875 | 73041 | } |
| | @@ -72900,11 +73066,11 @@ |
| 72900 | 73066 | #endif |
| 72901 | 73067 | if( pOp->opcode==OP_IfNot ) c = !c; |
| 72902 | 73068 | } |
| 72903 | 73069 | VdbeBranchTaken(c!=0, 2); |
| 72904 | 73070 | if( c ){ |
| 72905 | | - pc = pOp->p2-1; |
| 73071 | + goto jump_to_p2; |
| 72906 | 73072 | } |
| 72907 | 73073 | break; |
| 72908 | 73074 | } |
| 72909 | 73075 | |
| 72910 | 73076 | /* Opcode: IsNull P1 P2 * * * |
| | @@ -72914,11 +73080,11 @@ |
| 72914 | 73080 | */ |
| 72915 | 73081 | case OP_IsNull: { /* same as TK_ISNULL, jump, in1 */ |
| 72916 | 73082 | pIn1 = &aMem[pOp->p1]; |
| 72917 | 73083 | VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2); |
| 72918 | 73084 | if( (pIn1->flags & MEM_Null)!=0 ){ |
| 72919 | | - pc = pOp->p2 - 1; |
| 73085 | + goto jump_to_p2; |
| 72920 | 73086 | } |
| 72921 | 73087 | break; |
| 72922 | 73088 | } |
| 72923 | 73089 | |
| 72924 | 73090 | /* Opcode: NotNull P1 P2 * * * |
| | @@ -72928,11 +73094,11 @@ |
| 72928 | 73094 | */ |
| 72929 | 73095 | case OP_NotNull: { /* same as TK_NOTNULL, jump, in1 */ |
| 72930 | 73096 | pIn1 = &aMem[pOp->p1]; |
| 72931 | 73097 | VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2); |
| 72932 | 73098 | if( (pIn1->flags & MEM_Null)==0 ){ |
| 72933 | | - pc = pOp->p2 - 1; |
| 73099 | + goto jump_to_p2; |
| 72934 | 73100 | } |
| 72935 | 73101 | break; |
| 72936 | 73102 | } |
| 72937 | 73103 | |
| 72938 | 73104 | /* Opcode: Column P1 P2 P3 P4 P5 |
| | @@ -73142,11 +73308,11 @@ |
| 73142 | 73308 | rc = SQLITE_CORRUPT_BKPT; |
| 73143 | 73309 | goto op_column_error; |
| 73144 | 73310 | } |
| 73145 | 73311 | } |
| 73146 | 73312 | |
| 73147 | | - /* If after trying to extra new entries from the header, nHdrParsed is |
| 73313 | + /* If after trying to extract new entries from the header, nHdrParsed is |
| 73148 | 73314 | ** still not up to p2, that means that the record has fewer than p2 |
| 73149 | 73315 | ** columns. So the result will be either the default value or a NULL. |
| 73150 | 73316 | */ |
| 73151 | 73317 | if( pC->nHdrParsed<=p2 ){ |
| 73152 | 73318 | if( pOp->p4type==P4_MEM ){ |
| | @@ -73266,11 +73432,11 @@ |
| 73266 | 73432 | u8 *zNewRecord; /* A buffer to hold the data for the new record */ |
| 73267 | 73433 | Mem *pRec; /* The new record */ |
| 73268 | 73434 | u64 nData; /* Number of bytes of data space */ |
| 73269 | 73435 | int nHdr; /* Number of bytes of header space */ |
| 73270 | 73436 | i64 nByte; /* Data space required for this record */ |
| 73271 | | - int nZero; /* Number of zero bytes at the end of the record */ |
| 73437 | + i64 nZero; /* Number of zero bytes at the end of the record */ |
| 73272 | 73438 | int nVarint; /* Number of bytes in a varint */ |
| 73273 | 73439 | u32 serial_type; /* Type field */ |
| 73274 | 73440 | Mem *pData0; /* First field to be combined into the record */ |
| 73275 | 73441 | Mem *pLast; /* Last field of the record */ |
| 73276 | 73442 | int nField; /* Number of fields in the record */ |
| | @@ -73358,11 +73524,11 @@ |
| 73358 | 73524 | nVarint = sqlite3VarintLen(nHdr); |
| 73359 | 73525 | nHdr += nVarint; |
| 73360 | 73526 | if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++; |
| 73361 | 73527 | } |
| 73362 | 73528 | nByte = nHdr+nData; |
| 73363 | | - if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 73529 | + if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){ |
| 73364 | 73530 | goto too_big; |
| 73365 | 73531 | } |
| 73366 | 73532 | |
| 73367 | 73533 | /* Make sure the output register has a buffer large enough to store |
| 73368 | 73534 | ** the new record. The output register (pOp->p3) is not allowed to |
| | @@ -73409,18 +73575,19 @@ |
| 73409 | 73575 | ** |
| 73410 | 73576 | ** Store the number of entries (an integer value) in the table or index |
| 73411 | 73577 | ** opened by cursor P1 in register P2 |
| 73412 | 73578 | */ |
| 73413 | 73579 | #ifndef SQLITE_OMIT_BTREECOUNT |
| 73414 | | -case OP_Count: { /* out2-prerelease */ |
| 73580 | +case OP_Count: { /* out2 */ |
| 73415 | 73581 | i64 nEntry; |
| 73416 | 73582 | BtCursor *pCrsr; |
| 73417 | 73583 | |
| 73418 | 73584 | pCrsr = p->apCsr[pOp->p1]->pCursor; |
| 73419 | 73585 | assert( pCrsr ); |
| 73420 | 73586 | nEntry = 0; /* Not needed. Only used to silence a warning. */ |
| 73421 | 73587 | rc = sqlite3BtreeCount(pCrsr, &nEntry); |
| 73588 | + pOut = out2Prerelease(p, pOp); |
| 73422 | 73589 | pOut->u.i = nEntry; |
| 73423 | 73590 | break; |
| 73424 | 73591 | } |
| 73425 | 73592 | #endif |
| 73426 | 73593 | |
| | @@ -73530,11 +73697,11 @@ |
| 73530 | 73697 | if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
| 73531 | 73698 | goto vdbe_return; |
| 73532 | 73699 | } |
| 73533 | 73700 | db->autoCommit = 1; |
| 73534 | 73701 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 73535 | | - p->pc = pc; |
| 73702 | + p->pc = (int)(pOp - aOp); |
| 73536 | 73703 | db->autoCommit = 0; |
| 73537 | 73704 | p->rc = rc = SQLITE_BUSY; |
| 73538 | 73705 | goto vdbe_return; |
| 73539 | 73706 | } |
| 73540 | 73707 | db->isTransactionSavepoint = 0; |
| | @@ -73589,11 +73756,11 @@ |
| 73589 | 73756 | }else{ |
| 73590 | 73757 | db->nDeferredCons = pSavepoint->nDeferredCons; |
| 73591 | 73758 | db->nDeferredImmCons = pSavepoint->nDeferredImmCons; |
| 73592 | 73759 | } |
| 73593 | 73760 | |
| 73594 | | - if( !isTransaction ){ |
| 73761 | + if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){ |
| 73595 | 73762 | rc = sqlite3VtabSavepoint(db, p1, iSavepoint); |
| 73596 | 73763 | if( rc!=SQLITE_OK ) goto abort_due_to_error; |
| 73597 | 73764 | } |
| 73598 | 73765 | } |
| 73599 | 73766 | } |
| | @@ -73649,11 +73816,11 @@ |
| 73649 | 73816 | }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){ |
| 73650 | 73817 | goto vdbe_return; |
| 73651 | 73818 | }else{ |
| 73652 | 73819 | db->autoCommit = (u8)desiredAutoCommit; |
| 73653 | 73820 | if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){ |
| 73654 | | - p->pc = pc; |
| 73821 | + p->pc = (int)(pOp - aOp); |
| 73655 | 73822 | db->autoCommit = (u8)(1-desiredAutoCommit); |
| 73656 | 73823 | p->rc = rc = SQLITE_BUSY; |
| 73657 | 73824 | goto vdbe_return; |
| 73658 | 73825 | } |
| 73659 | 73826 | } |
| | @@ -73726,11 +73893,11 @@ |
| 73726 | 73893 | pBt = db->aDb[pOp->p1].pBt; |
| 73727 | 73894 | |
| 73728 | 73895 | if( pBt ){ |
| 73729 | 73896 | rc = sqlite3BtreeBeginTrans(pBt, pOp->p2); |
| 73730 | 73897 | if( rc==SQLITE_BUSY ){ |
| 73731 | | - p->pc = pc; |
| 73898 | + p->pc = (int)(pOp - aOp); |
| 73732 | 73899 | p->rc = rc = SQLITE_BUSY; |
| 73733 | 73900 | goto vdbe_return; |
| 73734 | 73901 | } |
| 73735 | 73902 | if( rc!=SQLITE_OK ){ |
| 73736 | 73903 | goto abort_due_to_error; |
| | @@ -73805,11 +73972,11 @@ |
| 73805 | 73972 | ** |
| 73806 | 73973 | ** There must be a read-lock on the database (either a transaction |
| 73807 | 73974 | ** must be started or there must be an open cursor) before |
| 73808 | 73975 | ** executing this instruction. |
| 73809 | 73976 | */ |
| 73810 | | -case OP_ReadCookie: { /* out2-prerelease */ |
| 73977 | +case OP_ReadCookie: { /* out2 */ |
| 73811 | 73978 | int iMeta; |
| 73812 | 73979 | int iDb; |
| 73813 | 73980 | int iCookie; |
| 73814 | 73981 | |
| 73815 | 73982 | assert( p->bIsReader ); |
| | @@ -73819,10 +73986,11 @@ |
| 73819 | 73986 | assert( iDb>=0 && iDb<db->nDb ); |
| 73820 | 73987 | assert( db->aDb[iDb].pBt!=0 ); |
| 73821 | 73988 | assert( DbMaskTest(p->btreeMask, iDb) ); |
| 73822 | 73989 | |
| 73823 | 73990 | sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta); |
| 73991 | + pOut = out2Prerelease(p, pOp); |
| 73824 | 73992 | pOut->u.i = iMeta; |
| 73825 | 73993 | break; |
| 73826 | 73994 | } |
| 73827 | 73995 | |
| 73828 | 73996 | /* Opcode: SetCookie P1 P2 P3 * * |
| | @@ -74140,11 +74308,11 @@ |
| 74140 | 74308 | VdbeCursor *pC; |
| 74141 | 74309 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 74142 | 74310 | pC = p->apCsr[pOp->p1]; |
| 74143 | 74311 | assert( pC->pSorter ); |
| 74144 | 74312 | if( (pC->seqCount++)==0 ){ |
| 74145 | | - pc = pOp->p2 - 1; |
| 74313 | + goto jump_to_p2; |
| 74146 | 74314 | } |
| 74147 | 74315 | break; |
| 74148 | 74316 | } |
| 74149 | 74317 | |
| 74150 | 74318 | /* Opcode: OpenPseudo P1 P2 P3 * * |
| | @@ -74317,11 +74485,11 @@ |
| 74317 | 74485 | ** loss of information, then special processing is required... */ |
| 74318 | 74486 | if( (pIn3->flags & MEM_Int)==0 ){ |
| 74319 | 74487 | if( (pIn3->flags & MEM_Real)==0 ){ |
| 74320 | 74488 | /* If the P3 value cannot be converted into any kind of a number, |
| 74321 | 74489 | ** then the seek is not possible, so jump to P2 */ |
| 74322 | | - pc = pOp->p2 - 1; VdbeBranchTaken(1,2); |
| 74490 | + VdbeBranchTaken(1,2); goto jump_to_p2; |
| 74323 | 74491 | break; |
| 74324 | 74492 | } |
| 74325 | 74493 | |
| 74326 | 74494 | /* If the approximation iKey is larger than the actual real search |
| 74327 | 74495 | ** term, substitute >= for > and < for <=. e.g. if the search term |
| | @@ -74408,11 +74576,11 @@ |
| 74408 | 74576 | } |
| 74409 | 74577 | } |
| 74410 | 74578 | assert( pOp->p2>0 ); |
| 74411 | 74579 | VdbeBranchTaken(res!=0,2); |
| 74412 | 74580 | if( res ){ |
| 74413 | | - pc = pOp->p2 - 1; |
| 74581 | + goto jump_to_p2; |
| 74414 | 74582 | } |
| 74415 | 74583 | break; |
| 74416 | 74584 | } |
| 74417 | 74585 | |
| 74418 | 74586 | /* Opcode: Seek P1 P2 * * * |
| | @@ -74502,10 +74670,11 @@ |
| 74502 | 74670 | */ |
| 74503 | 74671 | case OP_NoConflict: /* jump, in3 */ |
| 74504 | 74672 | case OP_NotFound: /* jump, in3 */ |
| 74505 | 74673 | case OP_Found: { /* jump, in3 */ |
| 74506 | 74674 | int alreadyExists; |
| 74675 | + int takeJump; |
| 74507 | 74676 | int ii; |
| 74508 | 74677 | VdbeCursor *pC; |
| 74509 | 74678 | int res; |
| 74510 | 74679 | char *pFree; |
| 74511 | 74680 | UnpackedRecord *pIdxKey; |
| | @@ -74524,11 +74693,11 @@ |
| 74524 | 74693 | pC->seekOp = pOp->opcode; |
| 74525 | 74694 | #endif |
| 74526 | 74695 | pIn3 = &aMem[pOp->p3]; |
| 74527 | 74696 | assert( pC->pCursor!=0 ); |
| 74528 | 74697 | assert( pC->isTable==0 ); |
| 74529 | | - pFree = 0; /* Not needed. Only used to suppress a compiler warning. */ |
| 74698 | + pFree = 0; |
| 74530 | 74699 | if( pOp->p4.i>0 ){ |
| 74531 | 74700 | r.pKeyInfo = pC->pKeyInfo; |
| 74532 | 74701 | r.nField = (u16)pOp->p4.i; |
| 74533 | 74702 | r.aMem = pIn3; |
| 74534 | 74703 | for(ii=0; ii<r.nField; ii++){ |
| | @@ -74547,25 +74716,24 @@ |
| 74547 | 74716 | assert( pIn3->flags & MEM_Blob ); |
| 74548 | 74717 | ExpandBlob(pIn3); |
| 74549 | 74718 | sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey); |
| 74550 | 74719 | } |
| 74551 | 74720 | pIdxKey->default_rc = 0; |
| 74721 | + takeJump = 0; |
| 74552 | 74722 | if( pOp->opcode==OP_NoConflict ){ |
| 74553 | 74723 | /* For the OP_NoConflict opcode, take the jump if any of the |
| 74554 | 74724 | ** input fields are NULL, since any key with a NULL will not |
| 74555 | 74725 | ** conflict */ |
| 74556 | 74726 | for(ii=0; ii<pIdxKey->nField; ii++){ |
| 74557 | 74727 | if( pIdxKey->aMem[ii].flags & MEM_Null ){ |
| 74558 | | - pc = pOp->p2 - 1; VdbeBranchTaken(1,2); |
| 74728 | + takeJump = 1; |
| 74559 | 74729 | break; |
| 74560 | 74730 | } |
| 74561 | 74731 | } |
| 74562 | 74732 | } |
| 74563 | 74733 | rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res); |
| 74564 | | - if( pOp->p4.i==0 ){ |
| 74565 | | - sqlite3DbFree(db, pFree); |
| 74566 | | - } |
| 74734 | + sqlite3DbFree(db, pFree); |
| 74567 | 74735 | if( rc!=SQLITE_OK ){ |
| 74568 | 74736 | break; |
| 74569 | 74737 | } |
| 74570 | 74738 | pC->seekResult = res; |
| 74571 | 74739 | alreadyExists = (res==0); |
| | @@ -74572,14 +74740,14 @@ |
| 74572 | 74740 | pC->nullRow = 1-alreadyExists; |
| 74573 | 74741 | pC->deferredMoveto = 0; |
| 74574 | 74742 | pC->cacheStatus = CACHE_STALE; |
| 74575 | 74743 | if( pOp->opcode==OP_Found ){ |
| 74576 | 74744 | VdbeBranchTaken(alreadyExists!=0,2); |
| 74577 | | - if( alreadyExists ) pc = pOp->p2 - 1; |
| 74745 | + if( alreadyExists ) goto jump_to_p2; |
| 74578 | 74746 | }else{ |
| 74579 | | - VdbeBranchTaken(alreadyExists==0,2); |
| 74580 | | - if( !alreadyExists ) pc = pOp->p2 - 1; |
| 74747 | + VdbeBranchTaken(takeJump||alreadyExists==0,2); |
| 74748 | + if( takeJump || !alreadyExists ) goto jump_to_p2; |
| 74581 | 74749 | } |
| 74582 | 74750 | break; |
| 74583 | 74751 | } |
| 74584 | 74752 | |
| 74585 | 74753 | /* Opcode: NotExists P1 P2 P3 * * |
| | @@ -74624,14 +74792,12 @@ |
| 74624 | 74792 | pC->movetoTarget = iKey; /* Used by OP_Delete */ |
| 74625 | 74793 | pC->nullRow = 0; |
| 74626 | 74794 | pC->cacheStatus = CACHE_STALE; |
| 74627 | 74795 | pC->deferredMoveto = 0; |
| 74628 | 74796 | VdbeBranchTaken(res!=0,2); |
| 74629 | | - if( res!=0 ){ |
| 74630 | | - pc = pOp->p2 - 1; |
| 74631 | | - } |
| 74632 | 74797 | pC->seekResult = res; |
| 74798 | + if( res!=0 ) goto jump_to_p2; |
| 74633 | 74799 | break; |
| 74634 | 74800 | } |
| 74635 | 74801 | |
| 74636 | 74802 | /* Opcode: Sequence P1 P2 * * * |
| 74637 | 74803 | ** Synopsis: r[P2]=cursor[P1].ctr++ |
| | @@ -74639,13 +74805,14 @@ |
| 74639 | 74805 | ** Find the next available sequence number for cursor P1. |
| 74640 | 74806 | ** Write the sequence number into register P2. |
| 74641 | 74807 | ** The sequence number on the cursor is incremented after this |
| 74642 | 74808 | ** instruction. |
| 74643 | 74809 | */ |
| 74644 | | -case OP_Sequence: { /* out2-prerelease */ |
| 74810 | +case OP_Sequence: { /* out2 */ |
| 74645 | 74811 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 74646 | 74812 | assert( p->apCsr[pOp->p1]!=0 ); |
| 74813 | + pOut = out2Prerelease(p, pOp); |
| 74647 | 74814 | pOut->u.i = p->apCsr[pOp->p1]->seqCount++; |
| 74648 | 74815 | break; |
| 74649 | 74816 | } |
| 74650 | 74817 | |
| 74651 | 74818 | |
| | @@ -74662,20 +74829,21 @@ |
| 74662 | 74829 | ** allowed to be less than this value. When this value reaches its maximum, |
| 74663 | 74830 | ** an SQLITE_FULL error is generated. The P3 register is updated with the ' |
| 74664 | 74831 | ** generated record number. This P3 mechanism is used to help implement the |
| 74665 | 74832 | ** AUTOINCREMENT feature. |
| 74666 | 74833 | */ |
| 74667 | | -case OP_NewRowid: { /* out2-prerelease */ |
| 74834 | +case OP_NewRowid: { /* out2 */ |
| 74668 | 74835 | i64 v; /* The new rowid */ |
| 74669 | 74836 | VdbeCursor *pC; /* Cursor of table to get the new rowid */ |
| 74670 | 74837 | int res; /* Result of an sqlite3BtreeLast() */ |
| 74671 | 74838 | int cnt; /* Counter to limit the number of searches */ |
| 74672 | 74839 | Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */ |
| 74673 | 74840 | VdbeFrame *pFrame; /* Root frame of VDBE */ |
| 74674 | 74841 | |
| 74675 | 74842 | v = 0; |
| 74676 | 74843 | res = 0; |
| 74844 | + pOut = out2Prerelease(p, pOp); |
| 74677 | 74845 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 74678 | 74846 | pC = p->apCsr[pOp->p1]; |
| 74679 | 74847 | assert( pC!=0 ); |
| 74680 | 74848 | if( NEVER(pC->pCursor==0) ){ |
| 74681 | 74849 | /* The zero initialization above is all that is needed */ |
| | @@ -74985,13 +75153,11 @@ |
| 74985 | 75153 | pIn3 = &aMem[pOp->p3]; |
| 74986 | 75154 | nKeyCol = pOp->p4.i; |
| 74987 | 75155 | res = 0; |
| 74988 | 75156 | rc = sqlite3VdbeSorterCompare(pC, pIn3, nKeyCol, &res); |
| 74989 | 75157 | VdbeBranchTaken(res!=0,2); |
| 74990 | | - if( res ){ |
| 74991 | | - pc = pOp->p2-1; |
| 74992 | | - } |
| 75158 | + if( res ) goto jump_to_p2; |
| 74993 | 75159 | break; |
| 74994 | 75160 | }; |
| 74995 | 75161 | |
| 74996 | 75162 | /* Opcode: SorterData P1 P2 P3 * * |
| 74997 | 75163 | ** Synopsis: r[P2]=data |
| | @@ -75116,16 +75282,17 @@ |
| 75116 | 75282 | ** |
| 75117 | 75283 | ** P1 can be either an ordinary table or a virtual table. There used to |
| 75118 | 75284 | ** be a separate OP_VRowid opcode for use with virtual tables, but this |
| 75119 | 75285 | ** one opcode now works for both table types. |
| 75120 | 75286 | */ |
| 75121 | | -case OP_Rowid: { /* out2-prerelease */ |
| 75287 | +case OP_Rowid: { /* out2 */ |
| 75122 | 75288 | VdbeCursor *pC; |
| 75123 | 75289 | i64 v; |
| 75124 | 75290 | sqlite3_vtab *pVtab; |
| 75125 | 75291 | const sqlite3_module *pModule; |
| 75126 | 75292 | |
| 75293 | + pOut = out2Prerelease(p, pOp); |
| 75127 | 75294 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 75128 | 75295 | pC = p->apCsr[pOp->p1]; |
| 75129 | 75296 | assert( pC!=0 ); |
| 75130 | 75297 | assert( pC->pseudoTableReg==0 || pC->nullRow ); |
| 75131 | 75298 | if( pC->nullRow ){ |
| | @@ -75174,11 +75341,11 @@ |
| 75174 | 75341 | sqlite3BtreeClearCursor(pC->pCursor); |
| 75175 | 75342 | } |
| 75176 | 75343 | break; |
| 75177 | 75344 | } |
| 75178 | 75345 | |
| 75179 | | -/* Opcode: Last P1 P2 * * * |
| 75346 | +/* Opcode: Last P1 P2 P3 * * |
| 75180 | 75347 | ** |
| 75181 | 75348 | ** The next use of the Rowid or Column or Prev instruction for P1 |
| 75182 | 75349 | ** will refer to the last entry in the database table or index. |
| 75183 | 75350 | ** If the table or index is empty and P2>0, then jump immediately to P2. |
| 75184 | 75351 | ** If P2 is 0 or if the table or index is not empty, fall through |
| | @@ -75201,16 +75368,17 @@ |
| 75201 | 75368 | assert( pCrsr!=0 ); |
| 75202 | 75369 | rc = sqlite3BtreeLast(pCrsr, &res); |
| 75203 | 75370 | pC->nullRow = (u8)res; |
| 75204 | 75371 | pC->deferredMoveto = 0; |
| 75205 | 75372 | pC->cacheStatus = CACHE_STALE; |
| 75373 | + pC->seekResult = pOp->p3; |
| 75206 | 75374 | #ifdef SQLITE_DEBUG |
| 75207 | 75375 | pC->seekOp = OP_Last; |
| 75208 | 75376 | #endif |
| 75209 | 75377 | if( pOp->p2>0 ){ |
| 75210 | 75378 | VdbeBranchTaken(res!=0,2); |
| 75211 | | - if( res ) pc = pOp->p2 - 1; |
| 75379 | + if( res ) goto jump_to_p2; |
| 75212 | 75380 | } |
| 75213 | 75381 | break; |
| 75214 | 75382 | } |
| 75215 | 75383 | |
| 75216 | 75384 | |
| | @@ -75270,13 +75438,11 @@ |
| 75270 | 75438 | pC->cacheStatus = CACHE_STALE; |
| 75271 | 75439 | } |
| 75272 | 75440 | pC->nullRow = (u8)res; |
| 75273 | 75441 | assert( pOp->p2>0 && pOp->p2<p->nOp ); |
| 75274 | 75442 | VdbeBranchTaken(res!=0,2); |
| 75275 | | - if( res ){ |
| 75276 | | - pc = pOp->p2 - 1; |
| 75277 | | - } |
| 75443 | + if( res ) goto jump_to_p2; |
| 75278 | 75444 | break; |
| 75279 | 75445 | } |
| 75280 | 75446 | |
| 75281 | 75447 | /* Opcode: Next P1 P2 P3 P4 P5 |
| 75282 | 75448 | ** |
| | @@ -75383,15 +75549,15 @@ |
| 75383 | 75549 | next_tail: |
| 75384 | 75550 | pC->cacheStatus = CACHE_STALE; |
| 75385 | 75551 | VdbeBranchTaken(res==0,2); |
| 75386 | 75552 | if( res==0 ){ |
| 75387 | 75553 | pC->nullRow = 0; |
| 75388 | | - pc = pOp->p2 - 1; |
| 75389 | 75554 | p->aCounter[pOp->p5]++; |
| 75390 | 75555 | #ifdef SQLITE_TEST |
| 75391 | 75556 | sqlite3_search_count++; |
| 75392 | 75557 | #endif |
| 75558 | + goto jump_to_p2_and_check_for_interrupt; |
| 75393 | 75559 | }else{ |
| 75394 | 75560 | pC->nullRow = 1; |
| 75395 | 75561 | } |
| 75396 | 75562 | goto check_for_interrupt; |
| 75397 | 75563 | } |
| | @@ -75495,15 +75661,16 @@ |
| 75495 | 75661 | ** the end of the index key pointed to by cursor P1. This integer should be |
| 75496 | 75662 | ** the rowid of the table entry to which this index entry points. |
| 75497 | 75663 | ** |
| 75498 | 75664 | ** See also: Rowid, MakeRecord. |
| 75499 | 75665 | */ |
| 75500 | | -case OP_IdxRowid: { /* out2-prerelease */ |
| 75666 | +case OP_IdxRowid: { /* out2 */ |
| 75501 | 75667 | BtCursor *pCrsr; |
| 75502 | 75668 | VdbeCursor *pC; |
| 75503 | 75669 | i64 rowid; |
| 75504 | 75670 | |
| 75671 | + pOut = out2Prerelease(p, pOp); |
| 75505 | 75672 | assert( pOp->p1>=0 && pOp->p1<p->nCursor ); |
| 75506 | 75673 | pC = p->apCsr[pOp->p1]; |
| 75507 | 75674 | assert( pC!=0 ); |
| 75508 | 75675 | pCrsr = pC->pCursor; |
| 75509 | 75676 | assert( pCrsr!=0 ); |
| | @@ -75612,13 +75779,11 @@ |
| 75612 | 75779 | }else{ |
| 75613 | 75780 | assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT ); |
| 75614 | 75781 | res++; |
| 75615 | 75782 | } |
| 75616 | 75783 | VdbeBranchTaken(res>0,2); |
| 75617 | | - if( res>0 ){ |
| 75618 | | - pc = pOp->p2 - 1 ; |
| 75619 | | - } |
| 75784 | + if( res>0 ) goto jump_to_p2; |
| 75620 | 75785 | break; |
| 75621 | 75786 | } |
| 75622 | 75787 | |
| 75623 | 75788 | /* Opcode: Destroy P1 P2 P3 * * |
| 75624 | 75789 | ** |
| | @@ -75638,15 +75803,16 @@ |
| 75638 | 75803 | ** the last one in the database) then a zero is stored in register P2. |
| 75639 | 75804 | ** If AUTOVACUUM is disabled then a zero is stored in register P2. |
| 75640 | 75805 | ** |
| 75641 | 75806 | ** See also: Clear |
| 75642 | 75807 | */ |
| 75643 | | -case OP_Destroy: { /* out2-prerelease */ |
| 75808 | +case OP_Destroy: { /* out2 */ |
| 75644 | 75809 | int iMoved; |
| 75645 | 75810 | int iDb; |
| 75646 | 75811 | |
| 75647 | 75812 | assert( p->readOnly==0 ); |
| 75813 | + pOut = out2Prerelease(p, pOp); |
| 75648 | 75814 | pOut->flags = MEM_Null; |
| 75649 | 75815 | if( db->nVdbeRead > db->nVDestroy+1 ){ |
| 75650 | 75816 | rc = SQLITE_LOCKED; |
| 75651 | 75817 | p->errorAction = OE_Abort; |
| 75652 | 75818 | }else{ |
| | @@ -75751,16 +75917,17 @@ |
| 75751 | 75917 | ** P1>1. Write the root page number of the new table into |
| 75752 | 75918 | ** register P2. |
| 75753 | 75919 | ** |
| 75754 | 75920 | ** See documentation on OP_CreateTable for additional information. |
| 75755 | 75921 | */ |
| 75756 | | -case OP_CreateIndex: /* out2-prerelease */ |
| 75757 | | -case OP_CreateTable: { /* out2-prerelease */ |
| 75922 | +case OP_CreateIndex: /* out2 */ |
| 75923 | +case OP_CreateTable: { /* out2 */ |
| 75758 | 75924 | int pgno; |
| 75759 | 75925 | int flags; |
| 75760 | 75926 | Db *pDb; |
| 75761 | 75927 | |
| 75928 | + pOut = out2Prerelease(p, pOp); |
| 75762 | 75929 | pgno = 0; |
| 75763 | 75930 | assert( pOp->p1>=0 && pOp->p1<db->nDb ); |
| 75764 | 75931 | assert( DbMaskTest(p->btreeMask, pOp->p1) ); |
| 75765 | 75932 | assert( p->readOnly==0 ); |
| 75766 | 75933 | pDb = &db->aDb[pOp->p1]; |
| | @@ -75982,16 +76149,16 @@ |
| 75982 | 76149 | if( (pIn1->flags & MEM_RowSet)==0 |
| 75983 | 76150 | || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0 |
| 75984 | 76151 | ){ |
| 75985 | 76152 | /* The boolean index is empty */ |
| 75986 | 76153 | sqlite3VdbeMemSetNull(pIn1); |
| 75987 | | - pc = pOp->p2 - 1; |
| 75988 | 76154 | VdbeBranchTaken(1,2); |
| 76155 | + goto jump_to_p2_and_check_for_interrupt; |
| 75989 | 76156 | }else{ |
| 75990 | 76157 | /* A value was pulled from the index */ |
| 75991 | | - sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); |
| 75992 | 76158 | VdbeBranchTaken(0,2); |
| 76159 | + sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val); |
| 75993 | 76160 | } |
| 75994 | 76161 | goto check_for_interrupt; |
| 75995 | 76162 | } |
| 75996 | 76163 | |
| 75997 | 76164 | /* Opcode: RowSetTest P1 P2 P3 P4 |
| | @@ -76038,14 +76205,11 @@ |
| 76038 | 76205 | assert( pOp->p4type==P4_INT32 ); |
| 76039 | 76206 | assert( iSet==-1 || iSet>=0 ); |
| 76040 | 76207 | if( iSet ){ |
| 76041 | 76208 | exists = sqlite3RowSetTest(pIn1->u.pRowSet, iSet, pIn3->u.i); |
| 76042 | 76209 | VdbeBranchTaken(exists!=0,2); |
| 76043 | | - if( exists ){ |
| 76044 | | - pc = pOp->p2 - 1; |
| 76045 | | - break; |
| 76046 | | - } |
| 76210 | + if( exists ) goto jump_to_p2; |
| 76047 | 76211 | } |
| 76048 | 76212 | if( iSet>=0 ){ |
| 76049 | 76213 | sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i); |
| 76050 | 76214 | } |
| 76051 | 76215 | break; |
| | @@ -76130,11 +76294,11 @@ |
| 76130 | 76294 | pRt->u.pFrame = pFrame; |
| 76131 | 76295 | |
| 76132 | 76296 | pFrame->v = p; |
| 76133 | 76297 | pFrame->nChildMem = nMem; |
| 76134 | 76298 | pFrame->nChildCsr = pProgram->nCsr; |
| 76135 | | - pFrame->pc = pc; |
| 76299 | + pFrame->pc = (int)(pOp - aOp); |
| 76136 | 76300 | pFrame->aMem = p->aMem; |
| 76137 | 76301 | pFrame->nMem = p->nMem; |
| 76138 | 76302 | pFrame->apCsr = p->apCsr; |
| 76139 | 76303 | pFrame->nCursor = p->nCursor; |
| 76140 | 76304 | pFrame->aOp = p->aOp; |
| | @@ -76153,11 +76317,11 @@ |
| 76153 | 76317 | } |
| 76154 | 76318 | }else{ |
| 76155 | 76319 | pFrame = pRt->u.pFrame; |
| 76156 | 76320 | assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem ); |
| 76157 | 76321 | assert( pProgram->nCsr==pFrame->nChildCsr ); |
| 76158 | | - assert( pc==pFrame->pc ); |
| 76322 | + assert( (int)(pOp - aOp)==pFrame->pc ); |
| 76159 | 76323 | } |
| 76160 | 76324 | |
| 76161 | 76325 | p->nFrame++; |
| 76162 | 76326 | pFrame->pParent = p->pFrame; |
| 76163 | 76327 | pFrame->lastRowid = lastRowid; |
| | @@ -76174,11 +76338,11 @@ |
| 76174 | 76338 | p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor]; |
| 76175 | 76339 | p->nOnceFlag = pProgram->nOnce; |
| 76176 | 76340 | #ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 76177 | 76341 | p->anExec = 0; |
| 76178 | 76342 | #endif |
| 76179 | | - pc = -1; |
| 76343 | + pOp = &aOp[-1]; |
| 76180 | 76344 | memset(p->aOnceFlag, 0, p->nOnceFlag); |
| 76181 | 76345 | |
| 76182 | 76346 | break; |
| 76183 | 76347 | } |
| 76184 | 76348 | |
| | @@ -76192,13 +76356,14 @@ |
| 76192 | 76356 | ** |
| 76193 | 76357 | ** The address of the cell in the parent frame is determined by adding |
| 76194 | 76358 | ** the value of the P1 argument to the value of the P1 argument to the |
| 76195 | 76359 | ** calling OP_Program instruction. |
| 76196 | 76360 | */ |
| 76197 | | -case OP_Param: { /* out2-prerelease */ |
| 76361 | +case OP_Param: { /* out2 */ |
| 76198 | 76362 | VdbeFrame *pFrame; |
| 76199 | 76363 | Mem *pIn; |
| 76364 | + pOut = out2Prerelease(p, pOp); |
| 76200 | 76365 | pFrame = p->pFrame; |
| 76201 | 76366 | pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1]; |
| 76202 | 76367 | sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem); |
| 76203 | 76368 | break; |
| 76204 | 76369 | } |
| | @@ -76238,14 +76403,14 @@ |
| 76238 | 76403 | ** (immediate foreign key constraint violations). |
| 76239 | 76404 | */ |
| 76240 | 76405 | case OP_FkIfZero: { /* jump */ |
| 76241 | 76406 | if( pOp->p1 ){ |
| 76242 | 76407 | VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2); |
| 76243 | | - if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1; |
| 76408 | + if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
| 76244 | 76409 | }else{ |
| 76245 | 76410 | VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2); |
| 76246 | | - if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1; |
| 76411 | + if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) goto jump_to_p2; |
| 76247 | 76412 | } |
| 76248 | 76413 | break; |
| 76249 | 76414 | } |
| 76250 | 76415 | #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */ |
| 76251 | 76416 | |
| | @@ -76292,13 +76457,11 @@ |
| 76292 | 76457 | */ |
| 76293 | 76458 | case OP_IfPos: { /* jump, in1 */ |
| 76294 | 76459 | pIn1 = &aMem[pOp->p1]; |
| 76295 | 76460 | assert( pIn1->flags&MEM_Int ); |
| 76296 | 76461 | VdbeBranchTaken( pIn1->u.i>0, 2); |
| 76297 | | - if( pIn1->u.i>0 ){ |
| 76298 | | - pc = pOp->p2 - 1; |
| 76299 | | - } |
| 76462 | + if( pIn1->u.i>0 ) goto jump_to_p2; |
| 76300 | 76463 | break; |
| 76301 | 76464 | } |
| 76302 | 76465 | |
| 76303 | 76466 | /* Opcode: IfNeg P1 P2 P3 * * |
| 76304 | 76467 | ** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2 |
| | @@ -76309,13 +76472,11 @@ |
| 76309 | 76472 | case OP_IfNeg: { /* jump, in1 */ |
| 76310 | 76473 | pIn1 = &aMem[pOp->p1]; |
| 76311 | 76474 | assert( pIn1->flags&MEM_Int ); |
| 76312 | 76475 | pIn1->u.i += pOp->p3; |
| 76313 | 76476 | VdbeBranchTaken(pIn1->u.i<0, 2); |
| 76314 | | - if( pIn1->u.i<0 ){ |
| 76315 | | - pc = pOp->p2 - 1; |
| 76316 | | - } |
| 76477 | + if( pIn1->u.i<0 ) goto jump_to_p2; |
| 76317 | 76478 | break; |
| 76318 | 76479 | } |
| 76319 | 76480 | |
| 76320 | 76481 | /* Opcode: IfNotZero P1 P2 P3 * * |
| 76321 | 76482 | ** Synopsis: if r[P1]!=0 then r[P1]+=P3, goto P2 |
| | @@ -76328,11 +76489,11 @@ |
| 76328 | 76489 | pIn1 = &aMem[pOp->p1]; |
| 76329 | 76490 | assert( pIn1->flags&MEM_Int ); |
| 76330 | 76491 | VdbeBranchTaken(pIn1->u.i<0, 2); |
| 76331 | 76492 | if( pIn1->u.i ){ |
| 76332 | 76493 | pIn1->u.i += pOp->p3; |
| 76333 | | - pc = pOp->p2 - 1; |
| 76494 | + goto jump_to_p2; |
| 76334 | 76495 | } |
| 76335 | 76496 | break; |
| 76336 | 76497 | } |
| 76337 | 76498 | |
| 76338 | 76499 | /* Opcode: DecrJumpZero P1 P2 * * * |
| | @@ -76344,13 +76505,11 @@ |
| 76344 | 76505 | case OP_DecrJumpZero: { /* jump, in1 */ |
| 76345 | 76506 | pIn1 = &aMem[pOp->p1]; |
| 76346 | 76507 | assert( pIn1->flags&MEM_Int ); |
| 76347 | 76508 | pIn1->u.i--; |
| 76348 | 76509 | VdbeBranchTaken(pIn1->u.i==0, 2); |
| 76349 | | - if( pIn1->u.i==0 ){ |
| 76350 | | - pc = pOp->p2 - 1; |
| 76351 | | - } |
| 76510 | + if( pIn1->u.i==0 ) goto jump_to_p2; |
| 76352 | 76511 | break; |
| 76353 | 76512 | } |
| 76354 | 76513 | |
| 76355 | 76514 | |
| 76356 | 76515 | /* Opcode: JumpZeroIncr P1 P2 * * * |
| | @@ -76362,13 +76521,11 @@ |
| 76362 | 76521 | */ |
| 76363 | 76522 | case OP_JumpZeroIncr: { /* jump, in1 */ |
| 76364 | 76523 | pIn1 = &aMem[pOp->p1]; |
| 76365 | 76524 | assert( pIn1->flags&MEM_Int ); |
| 76366 | 76525 | VdbeBranchTaken(pIn1->u.i==0, 2); |
| 76367 | | - if( (pIn1->u.i++)==0 ){ |
| 76368 | | - pc = pOp->p2 - 1; |
| 76369 | | - } |
| 76526 | + if( (pIn1->u.i++)==0 ) goto jump_to_p2; |
| 76370 | 76527 | break; |
| 76371 | 76528 | } |
| 76372 | 76529 | |
| 76373 | 76530 | /* Opcode: AggStep * P2 P3 P4 P5 |
| 76374 | 76531 | ** Synopsis: accum=r[P3] step(r[P2@P5]) |
| | @@ -76406,11 +76563,11 @@ |
| 76406 | 76563 | pMem->n++; |
| 76407 | 76564 | sqlite3VdbeMemInit(&t, db, MEM_Null); |
| 76408 | 76565 | ctx.pOut = &t; |
| 76409 | 76566 | ctx.isError = 0; |
| 76410 | 76567 | ctx.pVdbe = p; |
| 76411 | | - ctx.iOp = pc; |
| 76568 | + ctx.iOp = (int)(pOp - aOp); |
| 76412 | 76569 | ctx.skipFlag = 0; |
| 76413 | 76570 | (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */ |
| 76414 | 76571 | if( ctx.isError ){ |
| 76415 | 76572 | sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t)); |
| 76416 | 76573 | rc = ctx.isError; |
| | @@ -76501,19 +76658,20 @@ |
| 76501 | 76658 | ** |
| 76502 | 76659 | ** If changing into or out of WAL mode the procedure is more complicated. |
| 76503 | 76660 | ** |
| 76504 | 76661 | ** Write a string containing the final journal-mode to register P2. |
| 76505 | 76662 | */ |
| 76506 | | -case OP_JournalMode: { /* out2-prerelease */ |
| 76663 | +case OP_JournalMode: { /* out2 */ |
| 76507 | 76664 | Btree *pBt; /* Btree to change journal mode of */ |
| 76508 | 76665 | Pager *pPager; /* Pager associated with pBt */ |
| 76509 | 76666 | int eNew; /* New journal mode */ |
| 76510 | 76667 | int eOld; /* The old journal mode */ |
| 76511 | 76668 | #ifndef SQLITE_OMIT_WAL |
| 76512 | 76669 | const char *zFilename; /* Name of database file for pPager */ |
| 76513 | 76670 | #endif |
| 76514 | 76671 | |
| 76672 | + pOut = out2Prerelease(p, pOp); |
| 76515 | 76673 | eNew = pOp->p3; |
| 76516 | 76674 | assert( eNew==PAGER_JOURNALMODE_DELETE |
| 76517 | 76675 | || eNew==PAGER_JOURNALMODE_TRUNCATE |
| 76518 | 76676 | || eNew==PAGER_JOURNALMODE_PERSIST |
| 76519 | 76677 | || eNew==PAGER_JOURNALMODE_OFF |
| | @@ -76585,11 +76743,10 @@ |
| 76585 | 76743 | if( rc ){ |
| 76586 | 76744 | eNew = eOld; |
| 76587 | 76745 | } |
| 76588 | 76746 | eNew = sqlite3PagerSetJournalMode(pPager, eNew); |
| 76589 | 76747 | |
| 76590 | | - pOut = &aMem[pOp->p2]; |
| 76591 | 76748 | pOut->flags = MEM_Str|MEM_Static|MEM_Term; |
| 76592 | 76749 | pOut->z = (char *)sqlite3JournalModename(eNew); |
| 76593 | 76750 | pOut->n = sqlite3Strlen30(pOut->z); |
| 76594 | 76751 | pOut->enc = SQLITE_UTF8; |
| 76595 | 76752 | sqlite3VdbeChangeEncoding(pOut, encoding); |
| | @@ -76626,12 +76783,12 @@ |
| 76626 | 76783 | assert( p->readOnly==0 ); |
| 76627 | 76784 | pBt = db->aDb[pOp->p1].pBt; |
| 76628 | 76785 | rc = sqlite3BtreeIncrVacuum(pBt); |
| 76629 | 76786 | VdbeBranchTaken(rc==SQLITE_DONE,2); |
| 76630 | 76787 | if( rc==SQLITE_DONE ){ |
| 76631 | | - pc = pOp->p2 - 1; |
| 76632 | 76788 | rc = SQLITE_OK; |
| 76789 | + goto jump_to_p2; |
| 76633 | 76790 | } |
| 76634 | 76791 | break; |
| 76635 | 76792 | } |
| 76636 | 76793 | #endif |
| 76637 | 76794 | |
| | @@ -76780,12 +76937,13 @@ |
| 76780 | 76937 | pCur = allocateCursor(p, pOp->p1, 0, -1, 0); |
| 76781 | 76938 | if( pCur ){ |
| 76782 | 76939 | pCur->pVtabCursor = pVtabCursor; |
| 76783 | 76940 | pVtab->nRef++; |
| 76784 | 76941 | }else{ |
| 76785 | | - db->mallocFailed = 1; |
| 76942 | + assert( db->mallocFailed ); |
| 76786 | 76943 | pModule->xClose(pVtabCursor); |
| 76944 | + goto no_mem; |
| 76787 | 76945 | } |
| 76788 | 76946 | } |
| 76789 | 76947 | break; |
| 76790 | 76948 | } |
| 76791 | 76949 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| | @@ -76837,29 +76995,23 @@ |
| 76837 | 76995 | assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int ); |
| 76838 | 76996 | nArg = (int)pArgc->u.i; |
| 76839 | 76997 | iQuery = (int)pQuery->u.i; |
| 76840 | 76998 | |
| 76841 | 76999 | /* Invoke the xFilter method */ |
| 76842 | | - { |
| 76843 | | - res = 0; |
| 76844 | | - apArg = p->apArg; |
| 76845 | | - for(i = 0; i<nArg; i++){ |
| 76846 | | - apArg[i] = &pArgc[i+1]; |
| 76847 | | - } |
| 76848 | | - |
| 76849 | | - rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); |
| 76850 | | - sqlite3VtabImportErrmsg(p, pVtab); |
| 76851 | | - if( rc==SQLITE_OK ){ |
| 76852 | | - res = pModule->xEof(pVtabCursor); |
| 76853 | | - } |
| 76854 | | - VdbeBranchTaken(res!=0,2); |
| 76855 | | - if( res ){ |
| 76856 | | - pc = pOp->p2 - 1; |
| 76857 | | - } |
| 77000 | + res = 0; |
| 77001 | + apArg = p->apArg; |
| 77002 | + for(i = 0; i<nArg; i++){ |
| 77003 | + apArg[i] = &pArgc[i+1]; |
| 77004 | + } |
| 77005 | + rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg); |
| 77006 | + sqlite3VtabImportErrmsg(p, pVtab); |
| 77007 | + if( rc==SQLITE_OK ){ |
| 77008 | + res = pModule->xEof(pVtabCursor); |
| 76858 | 77009 | } |
| 76859 | 77010 | pCur->nullRow = 0; |
| 76860 | | - |
| 77011 | + VdbeBranchTaken(res!=0,2); |
| 77012 | + if( res ) goto jump_to_p2; |
| 76861 | 77013 | break; |
| 76862 | 77014 | } |
| 76863 | 77015 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 76864 | 77016 | |
| 76865 | 77017 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| | @@ -76942,11 +77094,11 @@ |
| 76942 | 77094 | res = pModule->xEof(pCur->pVtabCursor); |
| 76943 | 77095 | } |
| 76944 | 77096 | VdbeBranchTaken(!res,2); |
| 76945 | 77097 | if( !res ){ |
| 76946 | 77098 | /* If there is data, jump to P2 */ |
| 76947 | | - pc = pOp->p2 - 1; |
| 77099 | + goto jump_to_p2_and_check_for_interrupt; |
| 76948 | 77100 | } |
| 76949 | 77101 | goto check_for_interrupt; |
| 76950 | 77102 | } |
| 76951 | 77103 | #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 76952 | 77104 | |
| | @@ -77065,11 +77217,12 @@ |
| 77065 | 77217 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
| 77066 | 77218 | /* Opcode: Pagecount P1 P2 * * * |
| 77067 | 77219 | ** |
| 77068 | 77220 | ** Write the current number of pages in database P1 to memory cell P2. |
| 77069 | 77221 | */ |
| 77070 | | -case OP_Pagecount: { /* out2-prerelease */ |
| 77222 | +case OP_Pagecount: { /* out2 */ |
| 77223 | + pOut = out2Prerelease(p, pOp); |
| 77071 | 77224 | pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt); |
| 77072 | 77225 | break; |
| 77073 | 77226 | } |
| 77074 | 77227 | #endif |
| 77075 | 77228 | |
| | @@ -77081,14 +77234,15 @@ |
| 77081 | 77234 | ** Do not let the maximum page count fall below the current page count and |
| 77082 | 77235 | ** do not change the maximum page count value if P3==0. |
| 77083 | 77236 | ** |
| 77084 | 77237 | ** Store the maximum page count after the change in register P2. |
| 77085 | 77238 | */ |
| 77086 | | -case OP_MaxPgcnt: { /* out2-prerelease */ |
| 77239 | +case OP_MaxPgcnt: { /* out2 */ |
| 77087 | 77240 | unsigned int newMax; |
| 77088 | 77241 | Btree *pBt; |
| 77089 | 77242 | |
| 77243 | + pOut = out2Prerelease(p, pOp); |
| 77090 | 77244 | pBt = db->aDb[pOp->p1].pBt; |
| 77091 | 77245 | newMax = 0; |
| 77092 | 77246 | if( pOp->p3 ){ |
| 77093 | 77247 | newMax = sqlite3BtreeLastPage(pBt); |
| 77094 | 77248 | if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3; |
| | @@ -77113,13 +77267,10 @@ |
| 77113 | 77267 | */ |
| 77114 | 77268 | case OP_Init: { /* jump */ |
| 77115 | 77269 | char *zTrace; |
| 77116 | 77270 | char *z; |
| 77117 | 77271 | |
| 77118 | | - if( pOp->p2 ){ |
| 77119 | | - pc = pOp->p2 - 1; |
| 77120 | | - } |
| 77121 | 77272 | #ifndef SQLITE_OMIT_TRACE |
| 77122 | 77273 | if( db->xTrace |
| 77123 | 77274 | && !p->doingRerun |
| 77124 | 77275 | && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 |
| 77125 | 77276 | ){ |
| | @@ -77143,10 +77294,11 @@ |
| 77143 | 77294 | ){ |
| 77144 | 77295 | sqlite3DebugPrintf("SQL-trace: %s\n", zTrace); |
| 77145 | 77296 | } |
| 77146 | 77297 | #endif /* SQLITE_DEBUG */ |
| 77147 | 77298 | #endif /* SQLITE_OMIT_TRACE */ |
| 77299 | + if( pOp->p2 ) goto jump_to_p2; |
| 77148 | 77300 | break; |
| 77149 | 77301 | } |
| 77150 | 77302 | |
| 77151 | 77303 | |
| 77152 | 77304 | /* Opcode: Noop * * * * * |
| | @@ -77174,31 +77326,31 @@ |
| 77174 | 77326 | } |
| 77175 | 77327 | |
| 77176 | 77328 | #ifdef VDBE_PROFILE |
| 77177 | 77329 | { |
| 77178 | 77330 | u64 endTime = sqlite3Hwtime(); |
| 77179 | | - if( endTime>start ) pOp->cycles += endTime - start; |
| 77180 | | - pOp->cnt++; |
| 77331 | + if( endTime>start ) pOrigOp->cycles += endTime - start; |
| 77332 | + pOrigOp->cnt++; |
| 77181 | 77333 | } |
| 77182 | 77334 | #endif |
| 77183 | 77335 | |
| 77184 | 77336 | /* The following code adds nothing to the actual functionality |
| 77185 | 77337 | ** of the program. It is only here for testing and debugging. |
| 77186 | 77338 | ** On the other hand, it does burn CPU cycles every time through |
| 77187 | 77339 | ** the evaluator loop. So we can leave it out when NDEBUG is defined. |
| 77188 | 77340 | */ |
| 77189 | 77341 | #ifndef NDEBUG |
| 77190 | | - assert( pc>=-1 && pc<p->nOp ); |
| 77342 | + assert( pOp>=&aOp[-1] && pOp<&aOp[p->nOp-1] ); |
| 77191 | 77343 | |
| 77192 | 77344 | #ifdef SQLITE_DEBUG |
| 77193 | 77345 | if( db->flags & SQLITE_VdbeTrace ){ |
| 77194 | 77346 | if( rc!=0 ) printf("rc=%d\n",rc); |
| 77195 | | - if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){ |
| 77196 | | - registerTrace(pOp->p2, &aMem[pOp->p2]); |
| 77347 | + if( pOrigOp->opflags & (OPFLG_OUT2) ){ |
| 77348 | + registerTrace(pOrigOp->p2, &aMem[pOrigOp->p2]); |
| 77197 | 77349 | } |
| 77198 | | - if( pOp->opflags & OPFLG_OUT3 ){ |
| 77199 | | - registerTrace(pOp->p3, &aMem[pOp->p3]); |
| 77350 | + if( pOrigOp->opflags & OPFLG_OUT3 ){ |
| 77351 | + registerTrace(pOrigOp->p3, &aMem[pOrigOp->p3]); |
| 77200 | 77352 | } |
| 77201 | 77353 | } |
| 77202 | 77354 | #endif /* SQLITE_DEBUG */ |
| 77203 | 77355 | #endif /* NDEBUG */ |
| 77204 | 77356 | } /* The end of the for(;;) loop the loops through opcodes */ |
| | @@ -77209,11 +77361,11 @@ |
| 77209 | 77361 | vdbe_error_halt: |
| 77210 | 77362 | assert( rc ); |
| 77211 | 77363 | p->rc = rc; |
| 77212 | 77364 | testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 77213 | 77365 | sqlite3_log(rc, "statement aborts at %d: [%s] %s", |
| 77214 | | - pc, p->zSql, p->zErrMsg); |
| 77366 | + (int)(pOp - aOp), p->zSql, p->zErrMsg); |
| 77215 | 77367 | sqlite3VdbeHalt(p); |
| 77216 | 77368 | if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1; |
| 77217 | 77369 | rc = SQLITE_ERROR; |
| 77218 | 77370 | if( resetSchemaOnFault>0 ){ |
| 77219 | 77371 | sqlite3ResetOneSchema(db, resetSchemaOnFault-1); |
| | @@ -78035,20 +78187,23 @@ |
| 78035 | 78187 | ** |
| 78036 | 78188 | ** In both cases, the effects of the main thread seeing (bDone==0) even |
| 78037 | 78189 | ** after the thread has finished are not dire. So we don't worry about |
| 78038 | 78190 | ** memory barriers and such here. |
| 78039 | 78191 | */ |
| 78192 | +typedef int (*SorterCompare)(SortSubtask*,int*,const void*,int,const void*,int); |
| 78040 | 78193 | struct SortSubtask { |
| 78041 | 78194 | SQLiteThread *pThread; /* Background thread, if any */ |
| 78042 | 78195 | int bDone; /* Set if thread is finished but not joined */ |
| 78043 | 78196 | VdbeSorter *pSorter; /* Sorter that owns this sub-task */ |
| 78044 | 78197 | UnpackedRecord *pUnpacked; /* Space to unpack a record */ |
| 78045 | 78198 | SorterList list; /* List for thread to write to a PMA */ |
| 78046 | 78199 | int nPMA; /* Number of PMAs currently in file */ |
| 78200 | + SorterCompare xCompare; /* Compare function to use */ |
| 78047 | 78201 | SorterFile file; /* Temp file for level-0 PMAs */ |
| 78048 | 78202 | SorterFile file2; /* Space for other PMAs */ |
| 78049 | 78203 | }; |
| 78204 | + |
| 78050 | 78205 | |
| 78051 | 78206 | /* |
| 78052 | 78207 | ** Main sorter structure. A single instance of this is allocated for each |
| 78053 | 78208 | ** sorter cursor created by the VDBE. |
| 78054 | 78209 | ** |
| | @@ -78072,12 +78227,16 @@ |
| 78072 | 78227 | int nMemory; /* Size of list.aMemory allocation in bytes */ |
| 78073 | 78228 | u8 bUsePMA; /* True if one or more PMAs created */ |
| 78074 | 78229 | u8 bUseThreads; /* True to use background threads */ |
| 78075 | 78230 | u8 iPrev; /* Previous thread used to flush PMA */ |
| 78076 | 78231 | u8 nTask; /* Size of aTask[] array */ |
| 78232 | + u8 typeMask; |
| 78077 | 78233 | SortSubtask aTask[1]; /* One or more subtasks */ |
| 78078 | 78234 | }; |
| 78235 | + |
| 78236 | +#define SORTER_TYPE_INTEGER 0x01 |
| 78237 | +#define SORTER_TYPE_TEXT 0x02 |
| 78079 | 78238 | |
| 78080 | 78239 | /* |
| 78081 | 78240 | ** An instance of the following object is used to read records out of a |
| 78082 | 78241 | ** PMA, in sorted order. The next key to be read is cached in nKey/aKey. |
| 78083 | 78242 | ** aKey might point into aMap or into aBuffer. If neither of those locations |
| | @@ -78486,35 +78645,165 @@ |
| 78486 | 78645 | rc = vdbePmaReaderNext(pReadr); |
| 78487 | 78646 | } |
| 78488 | 78647 | return rc; |
| 78489 | 78648 | } |
| 78490 | 78649 | |
| 78650 | +/* |
| 78651 | +** A version of vdbeSorterCompare() that assumes that it has already been |
| 78652 | +** determined that the first field of key1 is equal to the first field of |
| 78653 | +** key2. |
| 78654 | +*/ |
| 78655 | +static int vdbeSorterCompareTail( |
| 78656 | + SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ |
| 78657 | + int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ |
| 78658 | + const void *pKey1, int nKey1, /* Left side of comparison */ |
| 78659 | + const void *pKey2, int nKey2 /* Right side of comparison */ |
| 78660 | +){ |
| 78661 | + UnpackedRecord *r2 = pTask->pUnpacked; |
| 78662 | + if( *pbKey2Cached==0 ){ |
| 78663 | + sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2); |
| 78664 | + *pbKey2Cached = 1; |
| 78665 | + } |
| 78666 | + return sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, r2, 1); |
| 78667 | +} |
| 78491 | 78668 | |
| 78492 | 78669 | /* |
| 78493 | 78670 | ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, |
| 78494 | 78671 | ** size nKey2 bytes). Use (pTask->pKeyInfo) for the collation sequences |
| 78495 | 78672 | ** used by the comparison. Return the result of the comparison. |
| 78496 | 78673 | ** |
| 78497 | | -** Before returning, object (pTask->pUnpacked) is populated with the |
| 78498 | | -** unpacked version of key2. Or, if pKey2 is passed a NULL pointer, then it |
| 78499 | | -** is assumed that the (pTask->pUnpacked) structure already contains the |
| 78500 | | -** unpacked key to use as key2. |
| 78674 | +** If IN/OUT parameter *pbKey2Cached is true when this function is called, |
| 78675 | +** it is assumed that (pTask->pUnpacked) contains the unpacked version |
| 78676 | +** of key2. If it is false, (pTask->pUnpacked) is populated with the unpacked |
| 78677 | +** version of key2 and *pbKey2Cached set to true before returning. |
| 78501 | 78678 | ** |
| 78502 | 78679 | ** If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set |
| 78503 | 78680 | ** to SQLITE_NOMEM. |
| 78504 | 78681 | */ |
| 78505 | 78682 | static int vdbeSorterCompare( |
| 78506 | 78683 | SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ |
| 78684 | + int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ |
| 78507 | 78685 | const void *pKey1, int nKey1, /* Left side of comparison */ |
| 78508 | 78686 | const void *pKey2, int nKey2 /* Right side of comparison */ |
| 78509 | 78687 | ){ |
| 78510 | 78688 | UnpackedRecord *r2 = pTask->pUnpacked; |
| 78511 | | - if( pKey2 ){ |
| 78689 | + if( !*pbKey2Cached ){ |
| 78512 | 78690 | sqlite3VdbeRecordUnpack(pTask->pSorter->pKeyInfo, nKey2, pKey2, r2); |
| 78691 | + *pbKey2Cached = 1; |
| 78513 | 78692 | } |
| 78514 | 78693 | return sqlite3VdbeRecordCompare(nKey1, pKey1, r2); |
| 78515 | 78694 | } |
| 78695 | + |
| 78696 | +/* |
| 78697 | +** A specially optimized version of vdbeSorterCompare() that assumes that |
| 78698 | +** the first field of each key is a TEXT value and that the collation |
| 78699 | +** sequence to compare them with is BINARY. |
| 78700 | +*/ |
| 78701 | +static int vdbeSorterCompareText( |
| 78702 | + SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ |
| 78703 | + int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ |
| 78704 | + const void *pKey1, int nKey1, /* Left side of comparison */ |
| 78705 | + const void *pKey2, int nKey2 /* Right side of comparison */ |
| 78706 | +){ |
| 78707 | + const u8 * const p1 = (const u8 * const)pKey1; |
| 78708 | + const u8 * const p2 = (const u8 * const)pKey2; |
| 78709 | + const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */ |
| 78710 | + const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */ |
| 78711 | + |
| 78712 | + int n1; |
| 78713 | + int n2; |
| 78714 | + int res; |
| 78715 | + |
| 78716 | + getVarint32(&p1[1], n1); n1 = (n1 - 13) / 2; |
| 78717 | + getVarint32(&p2[1], n2); n2 = (n2 - 13) / 2; |
| 78718 | + res = memcmp(v1, v2, MIN(n1, n2)); |
| 78719 | + if( res==0 ){ |
| 78720 | + res = n1 - n2; |
| 78721 | + } |
| 78722 | + |
| 78723 | + if( res==0 ){ |
| 78724 | + if( pTask->pSorter->pKeyInfo->nField>1 ){ |
| 78725 | + res = vdbeSorterCompareTail( |
| 78726 | + pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2 |
| 78727 | + ); |
| 78728 | + } |
| 78729 | + }else{ |
| 78730 | + if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){ |
| 78731 | + res = res * -1; |
| 78732 | + } |
| 78733 | + } |
| 78734 | + |
| 78735 | + return res; |
| 78736 | +} |
| 78737 | + |
| 78738 | +/* |
| 78739 | +** A specially optimized version of vdbeSorterCompare() that assumes that |
| 78740 | +** the first field of each key is an INTEGER value. |
| 78741 | +*/ |
| 78742 | +static int vdbeSorterCompareInt( |
| 78743 | + SortSubtask *pTask, /* Subtask context (for pKeyInfo) */ |
| 78744 | + int *pbKey2Cached, /* True if pTask->pUnpacked is pKey2 */ |
| 78745 | + const void *pKey1, int nKey1, /* Left side of comparison */ |
| 78746 | + const void *pKey2, int nKey2 /* Right side of comparison */ |
| 78747 | +){ |
| 78748 | + const u8 * const p1 = (const u8 * const)pKey1; |
| 78749 | + const u8 * const p2 = (const u8 * const)pKey2; |
| 78750 | + const int s1 = p1[1]; /* Left hand serial type */ |
| 78751 | + const int s2 = p2[1]; /* Right hand serial type */ |
| 78752 | + const u8 * const v1 = &p1[ p1[0] ]; /* Pointer to value 1 */ |
| 78753 | + const u8 * const v2 = &p2[ p2[0] ]; /* Pointer to value 2 */ |
| 78754 | + int res; /* Return value */ |
| 78755 | + |
| 78756 | + assert( (s1>0 && s1<7) || s1==8 || s1==9 ); |
| 78757 | + assert( (s2>0 && s2<7) || s2==8 || s2==9 ); |
| 78758 | + |
| 78759 | + if( s1>7 && s2>7 ){ |
| 78760 | + res = s1 - s2; |
| 78761 | + }else{ |
| 78762 | + if( s1==s2 ){ |
| 78763 | + if( (*v1 ^ *v2) & 0x80 ){ |
| 78764 | + /* The two values have different signs */ |
| 78765 | + res = (*v1 & 0x80) ? -1 : +1; |
| 78766 | + }else{ |
| 78767 | + /* The two values have the same sign. Compare using memcmp(). */ |
| 78768 | + static const u8 aLen[] = {0, 1, 2, 3, 4, 6, 8 }; |
| 78769 | + int i; |
| 78770 | + res = 0; |
| 78771 | + for(i=0; i<aLen[s1]; i++){ |
| 78772 | + if( (res = v1[i] - v2[i]) ) break; |
| 78773 | + } |
| 78774 | + } |
| 78775 | + }else{ |
| 78776 | + if( s2>7 ){ |
| 78777 | + res = +1; |
| 78778 | + }else if( s1>7 ){ |
| 78779 | + res = -1; |
| 78780 | + }else{ |
| 78781 | + res = s1 - s2; |
| 78782 | + } |
| 78783 | + assert( res!=0 ); |
| 78784 | + |
| 78785 | + if( res>0 ){ |
| 78786 | + if( *v1 & 0x80 ) res = -1; |
| 78787 | + }else{ |
| 78788 | + if( *v2 & 0x80 ) res = +1; |
| 78789 | + } |
| 78790 | + } |
| 78791 | + } |
| 78792 | + |
| 78793 | + if( res==0 ){ |
| 78794 | + if( pTask->pSorter->pKeyInfo->nField>1 ){ |
| 78795 | + res = vdbeSorterCompareTail( |
| 78796 | + pTask, pbKey2Cached, pKey1, nKey1, pKey2, nKey2 |
| 78797 | + ); |
| 78798 | + } |
| 78799 | + }else if( pTask->pSorter->pKeyInfo->aSortOrder[0] ){ |
| 78800 | + res = res * -1; |
| 78801 | + } |
| 78802 | + |
| 78803 | + return res; |
| 78804 | +} |
| 78516 | 78805 | |
| 78517 | 78806 | /* |
| 78518 | 78807 | ** Initialize the temporary index cursor just opened as a sorter cursor. |
| 78519 | 78808 | ** |
| 78520 | 78809 | ** Usually, the sorter module uses the value of (pCsr->pKeyInfo->nField) |
| | @@ -78579,13 +78868,17 @@ |
| 78579 | 78868 | rc = SQLITE_NOMEM; |
| 78580 | 78869 | }else{ |
| 78581 | 78870 | pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz); |
| 78582 | 78871 | memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo); |
| 78583 | 78872 | pKeyInfo->db = 0; |
| 78584 | | - if( nField && nWorker==0 ) pKeyInfo->nField = nField; |
| 78873 | + if( nField && nWorker==0 ){ |
| 78874 | + pKeyInfo->nXField += (pKeyInfo->nField - nField); |
| 78875 | + pKeyInfo->nField = nField; |
| 78876 | + } |
| 78585 | 78877 | pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt); |
| 78586 | 78878 | pSorter->nTask = nWorker + 1; |
| 78879 | + pSorter->iPrev = nWorker-1; |
| 78587 | 78880 | pSorter->bUseThreads = (pSorter->nTask>1); |
| 78588 | 78881 | pSorter->db = db; |
| 78589 | 78882 | for(i=0; i<pSorter->nTask; i++){ |
| 78590 | 78883 | SortSubtask *pTask = &pSorter->aTask[i]; |
| 78591 | 78884 | pTask->pSorter = pSorter; |
| | @@ -78607,10 +78900,16 @@ |
| 78607 | 78900 | pSorter->nMemory = pgsz; |
| 78608 | 78901 | pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz); |
| 78609 | 78902 | if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM; |
| 78610 | 78903 | } |
| 78611 | 78904 | } |
| 78905 | + |
| 78906 | + if( (pKeyInfo->nField+pKeyInfo->nXField)<13 |
| 78907 | + && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl) |
| 78908 | + ){ |
| 78909 | + pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT; |
| 78910 | + } |
| 78612 | 78911 | } |
| 78613 | 78912 | |
| 78614 | 78913 | return rc; |
| 78615 | 78914 | } |
| 78616 | 78915 | #undef nWorker /* Defined at the top of this function */ |
| | @@ -78631,34 +78930,28 @@ |
| 78631 | 78930 | ** Free all resources owned by the object indicated by argument pTask. All |
| 78632 | 78931 | ** fields of *pTask are zeroed before returning. |
| 78633 | 78932 | */ |
| 78634 | 78933 | static void vdbeSortSubtaskCleanup(sqlite3 *db, SortSubtask *pTask){ |
| 78635 | 78934 | sqlite3DbFree(db, pTask->pUnpacked); |
| 78636 | | - pTask->pUnpacked = 0; |
| 78637 | 78935 | #if SQLITE_MAX_WORKER_THREADS>0 |
| 78638 | 78936 | /* pTask->list.aMemory can only be non-zero if it was handed memory |
| 78639 | 78937 | ** from the main thread. That only occurs SQLITE_MAX_WORKER_THREADS>0 */ |
| 78640 | 78938 | if( pTask->list.aMemory ){ |
| 78641 | 78939 | sqlite3_free(pTask->list.aMemory); |
| 78642 | | - pTask->list.aMemory = 0; |
| 78643 | 78940 | }else |
| 78644 | 78941 | #endif |
| 78645 | 78942 | { |
| 78646 | 78943 | assert( pTask->list.aMemory==0 ); |
| 78647 | 78944 | vdbeSorterRecordFree(0, pTask->list.pList); |
| 78648 | 78945 | } |
| 78649 | | - pTask->list.pList = 0; |
| 78650 | 78946 | if( pTask->file.pFd ){ |
| 78651 | 78947 | sqlite3OsCloseFree(pTask->file.pFd); |
| 78652 | | - pTask->file.pFd = 0; |
| 78653 | | - pTask->file.iEof = 0; |
| 78654 | 78948 | } |
| 78655 | 78949 | if( pTask->file2.pFd ){ |
| 78656 | 78950 | sqlite3OsCloseFree(pTask->file2.pFd); |
| 78657 | | - pTask->file2.pFd = 0; |
| 78658 | | - pTask->file2.iEof = 0; |
| 78659 | 78951 | } |
| 78952 | + memset(pTask, 0, sizeof(SortSubtask)); |
| 78660 | 78953 | } |
| 78661 | 78954 | |
| 78662 | 78955 | #ifdef SQLITE_DEBUG_SORTER_THREADS |
| 78663 | 78956 | static void vdbeSorterWorkDebug(SortSubtask *pTask, const char *zEvent){ |
| 78664 | 78957 | i64 t; |
| | @@ -78834,10 +79127,11 @@ |
| 78834 | 79127 | vdbeMergeEngineFree(pSorter->pMerger); |
| 78835 | 79128 | pSorter->pMerger = 0; |
| 78836 | 79129 | for(i=0; i<pSorter->nTask; i++){ |
| 78837 | 79130 | SortSubtask *pTask = &pSorter->aTask[i]; |
| 78838 | 79131 | vdbeSortSubtaskCleanup(db, pTask); |
| 79132 | + pTask->pSorter = pSorter; |
| 78839 | 79133 | } |
| 78840 | 79134 | if( pSorter->list.aMemory==0 ){ |
| 78841 | 79135 | vdbeSorterRecordFree(0, pSorter->list.pList); |
| 78842 | 79136 | } |
| 78843 | 79137 | pSorter->list.pList = 0; |
| | @@ -78943,31 +79237,45 @@ |
| 78943 | 79237 | SorterRecord *p2, /* Second list to merge */ |
| 78944 | 79238 | SorterRecord **ppOut /* OUT: Head of merged list */ |
| 78945 | 79239 | ){ |
| 78946 | 79240 | SorterRecord *pFinal = 0; |
| 78947 | 79241 | SorterRecord **pp = &pFinal; |
| 78948 | | - void *pVal2 = p2 ? SRVAL(p2) : 0; |
| 79242 | + int bCached = 0; |
| 78949 | 79243 | |
| 78950 | 79244 | while( p1 && p2 ){ |
| 78951 | 79245 | int res; |
| 78952 | | - res = vdbeSorterCompare(pTask, SRVAL(p1), p1->nVal, pVal2, p2->nVal); |
| 79246 | + res = pTask->xCompare( |
| 79247 | + pTask, &bCached, SRVAL(p1), p1->nVal, SRVAL(p2), p2->nVal |
| 79248 | + ); |
| 79249 | + |
| 78953 | 79250 | if( res<=0 ){ |
| 78954 | 79251 | *pp = p1; |
| 78955 | 79252 | pp = &p1->u.pNext; |
| 78956 | 79253 | p1 = p1->u.pNext; |
| 78957 | | - pVal2 = 0; |
| 78958 | 79254 | }else{ |
| 78959 | 79255 | *pp = p2; |
| 78960 | | - pp = &p2->u.pNext; |
| 79256 | + pp = &p2->u.pNext; |
| 78961 | 79257 | p2 = p2->u.pNext; |
| 78962 | | - if( p2==0 ) break; |
| 78963 | | - pVal2 = SRVAL(p2); |
| 79258 | + bCached = 0; |
| 78964 | 79259 | } |
| 78965 | 79260 | } |
| 78966 | 79261 | *pp = p1 ? p1 : p2; |
| 78967 | 79262 | *ppOut = pFinal; |
| 78968 | 79263 | } |
| 79264 | + |
| 79265 | +/* |
| 79266 | +** Return the SorterCompare function to compare values collected by the |
| 79267 | +** sorter object passed as the only argument. |
| 79268 | +*/ |
| 79269 | +static SorterCompare vdbeSorterGetCompare(VdbeSorter *p){ |
| 79270 | + if( p->typeMask==SORTER_TYPE_INTEGER ){ |
| 79271 | + return vdbeSorterCompareInt; |
| 79272 | + }else if( p->typeMask==SORTER_TYPE_TEXT ){ |
| 79273 | + return vdbeSorterCompareText; |
| 79274 | + } |
| 79275 | + return vdbeSorterCompare; |
| 79276 | +} |
| 78969 | 79277 | |
| 78970 | 79278 | /* |
| 78971 | 79279 | ** Sort the linked list of records headed at pTask->pList. Return |
| 78972 | 79280 | ** SQLITE_OK if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if |
| 78973 | 79281 | ** an error occurs. |
| | @@ -78979,16 +79287,18 @@ |
| 78979 | 79287 | int rc; |
| 78980 | 79288 | |
| 78981 | 79289 | rc = vdbeSortAllocUnpacked(pTask); |
| 78982 | 79290 | if( rc!=SQLITE_OK ) return rc; |
| 78983 | 79291 | |
| 79292 | + p = pList->pList; |
| 79293 | + pTask->xCompare = vdbeSorterGetCompare(pTask->pSorter); |
| 79294 | + |
| 78984 | 79295 | aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *)); |
| 78985 | 79296 | if( !aSlot ){ |
| 78986 | 79297 | return SQLITE_NOMEM; |
| 78987 | 79298 | } |
| 78988 | 79299 | |
| 78989 | | - p = pList->pList; |
| 78990 | 79300 | while( p ){ |
| 78991 | 79301 | SorterRecord *pNext; |
| 78992 | 79302 | if( pList->aMemory ){ |
| 78993 | 79303 | if( (u8*)p==pList->aMemory ){ |
| 78994 | 79304 | pNext = 0; |
| | @@ -79198,28 +79508,27 @@ |
| 79198 | 79508 | /* Update contents of aTree[] */ |
| 79199 | 79509 | if( rc==SQLITE_OK ){ |
| 79200 | 79510 | int i; /* Index of aTree[] to recalculate */ |
| 79201 | 79511 | PmaReader *pReadr1; /* First PmaReader to compare */ |
| 79202 | 79512 | PmaReader *pReadr2; /* Second PmaReader to compare */ |
| 79203 | | - u8 *pKey2; /* To pReadr2->aKey, or 0 if record cached */ |
| 79513 | + int bCached = 0; |
| 79204 | 79514 | |
| 79205 | 79515 | /* Find the first two PmaReaders to compare. The one that was just |
| 79206 | 79516 | ** advanced (iPrev) and the one next to it in the array. */ |
| 79207 | 79517 | pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)]; |
| 79208 | 79518 | pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)]; |
| 79209 | | - pKey2 = pReadr2->aKey; |
| 79210 | 79519 | |
| 79211 | 79520 | for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){ |
| 79212 | 79521 | /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */ |
| 79213 | 79522 | int iRes; |
| 79214 | 79523 | if( pReadr1->pFd==0 ){ |
| 79215 | 79524 | iRes = +1; |
| 79216 | 79525 | }else if( pReadr2->pFd==0 ){ |
| 79217 | 79526 | iRes = -1; |
| 79218 | 79527 | }else{ |
| 79219 | | - iRes = vdbeSorterCompare(pTask, |
| 79220 | | - pReadr1->aKey, pReadr1->nKey, pKey2, pReadr2->nKey |
| 79528 | + iRes = pTask->xCompare(pTask, &bCached, |
| 79529 | + pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey |
| 79221 | 79530 | ); |
| 79222 | 79531 | } |
| 79223 | 79532 | |
| 79224 | 79533 | /* If pReadr1 contained the smaller value, set aTree[i] to its index. |
| 79225 | 79534 | ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this |
| | @@ -79237,13 +79546,13 @@ |
| 79237 | 79546 | ** is sorted from oldest to newest, so pReadr1 contains older values |
| 79238 | 79547 | ** than pReadr2 iff (pReadr1<pReadr2). */ |
| 79239 | 79548 | if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){ |
| 79240 | 79549 | pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr); |
| 79241 | 79550 | pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ]; |
| 79242 | | - pKey2 = pReadr2->aKey; |
| 79551 | + bCached = 0; |
| 79243 | 79552 | }else{ |
| 79244 | | - if( pReadr1->pFd ) pKey2 = 0; |
| 79553 | + if( pReadr1->pFd ) bCached = 0; |
| 79245 | 79554 | pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr); |
| 79246 | 79555 | pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ]; |
| 79247 | 79556 | } |
| 79248 | 79557 | } |
| 79249 | 79558 | *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0); |
| | @@ -79346,10 +79655,20 @@ |
| 79346 | 79655 | SorterRecord *pNew; /* New list element */ |
| 79347 | 79656 | |
| 79348 | 79657 | int bFlush; /* True to flush contents of memory to PMA */ |
| 79349 | 79658 | int nReq; /* Bytes of memory required */ |
| 79350 | 79659 | int nPMA; /* Bytes of PMA space required */ |
| 79660 | + int t; /* serial type of first record field */ |
| 79661 | + |
| 79662 | + getVarint32((const u8*)&pVal->z[1], t); |
| 79663 | + if( t>0 && t<10 && t!=7 ){ |
| 79664 | + pSorter->typeMask &= SORTER_TYPE_INTEGER; |
| 79665 | + }else if( t>10 && (t & 0x01) ){ |
| 79666 | + pSorter->typeMask &= SORTER_TYPE_TEXT; |
| 79667 | + }else{ |
| 79668 | + pSorter->typeMask = 0; |
| 79669 | + } |
| 79351 | 79670 | |
| 79352 | 79671 | assert( pSorter ); |
| 79353 | 79672 | |
| 79354 | 79673 | /* Figure out whether or not the current contents of memory should be |
| 79355 | 79674 | ** flushed to a PMA before continuing. If so, do so. |
| | @@ -79611,14 +79930,16 @@ |
| 79611 | 79930 | if( p1->pFd==0 ){ |
| 79612 | 79931 | iRes = i2; |
| 79613 | 79932 | }else if( p2->pFd==0 ){ |
| 79614 | 79933 | iRes = i1; |
| 79615 | 79934 | }else{ |
| 79935 | + SortSubtask *pTask = pMerger->pTask; |
| 79936 | + int bCached = 0; |
| 79616 | 79937 | int res; |
| 79617 | | - assert( pMerger->pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */ |
| 79618 | | - res = vdbeSorterCompare( |
| 79619 | | - pMerger->pTask, p1->aKey, p1->nKey, p2->aKey, p2->nKey |
| 79938 | + assert( pTask->pUnpacked!=0 ); /* from vdbeSortSubtaskMain() */ |
| 79939 | + res = pTask->xCompare( |
| 79940 | + pTask, &bCached, p1->aKey, p1->nKey, p2->aKey, p2->nKey |
| 79620 | 79941 | ); |
| 79621 | 79942 | if( res<=0 ){ |
| 79622 | 79943 | iRes = i1; |
| 79623 | 79944 | }else{ |
| 79624 | 79945 | iRes = i2; |
| | @@ -79638,15 +79959,16 @@ |
| 79638 | 79959 | */ |
| 79639 | 79960 | #define INCRINIT_NORMAL 0 |
| 79640 | 79961 | #define INCRINIT_TASK 1 |
| 79641 | 79962 | #define INCRINIT_ROOT 2 |
| 79642 | 79963 | |
| 79643 | | -/* Forward reference. |
| 79644 | | -** The vdbeIncrMergeInit() and vdbePmaReaderIncrMergeInit() routines call each |
| 79645 | | -** other (when building a merge tree). |
| 79964 | +/* |
| 79965 | +** Forward reference required as the vdbeIncrMergeInit() and |
| 79966 | +** vdbePmaReaderIncrInit() routines are called mutually recursively when |
| 79967 | +** building a merge tree. |
| 79646 | 79968 | */ |
| 79647 | | -static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode); |
| 79969 | +static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode); |
| 79648 | 79970 | |
| 79649 | 79971 | /* |
| 79650 | 79972 | ** Initialize the MergeEngine object passed as the second argument. Once this |
| 79651 | 79973 | ** function returns, the first key of merged data may be read from the |
| 79652 | 79974 | ** MergeEngine object in the usual fashion. |
| | @@ -79689,11 +80011,11 @@ |
| 79689 | 80011 | ** the main thread to fill its buffer. So calling PmaReaderNext() |
| 79690 | 80012 | ** on this PmaReader before any of the multi-threaded PmaReaders takes |
| 79691 | 80013 | ** better advantage of multi-processor hardware. */ |
| 79692 | 80014 | rc = vdbePmaReaderNext(&pMerger->aReadr[nTree-i-1]); |
| 79693 | 80015 | }else{ |
| 79694 | | - rc = vdbePmaReaderIncrMergeInit(&pMerger->aReadr[i], INCRINIT_NORMAL); |
| 80016 | + rc = vdbePmaReaderIncrInit(&pMerger->aReadr[i], INCRINIT_NORMAL); |
| 79695 | 80017 | } |
| 79696 | 80018 | if( rc!=SQLITE_OK ) return rc; |
| 79697 | 80019 | } |
| 79698 | 80020 | |
| 79699 | 80021 | for(i=pMerger->nTree-1; i>0; i--){ |
| | @@ -79701,21 +80023,19 @@ |
| 79701 | 80023 | } |
| 79702 | 80024 | return pTask->pUnpacked->errCode; |
| 79703 | 80025 | } |
| 79704 | 80026 | |
| 79705 | 80027 | /* |
| 79706 | | -** Initialize the IncrMerge field of a PmaReader. |
| 79707 | | -** |
| 79708 | | -** If the PmaReader passed as the first argument is not an incremental-reader |
| 79709 | | -** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it serves |
| 79710 | | -** to open and/or initialize the temp file related fields of the IncrMerge |
| 80028 | +** The PmaReader passed as the first argument is guaranteed to be an |
| 80029 | +** incremental-reader (pReadr->pIncr!=0). This function serves to open |
| 80030 | +** and/or initialize the temp file related fields of the IncrMerge |
| 79711 | 80031 | ** object at (pReadr->pIncr). |
| 79712 | 80032 | ** |
| 79713 | 80033 | ** If argument eMode is set to INCRINIT_NORMAL, then all PmaReaders |
| 79714 | | -** in the sub-tree headed by pReadr are also initialized. Data is then loaded |
| 79715 | | -** into the buffers belonging to pReadr and it is set to |
| 79716 | | -** point to the first key in its range. |
| 80034 | +** in the sub-tree headed by pReadr are also initialized. Data is then |
| 80035 | +** loaded into the buffers belonging to pReadr and it is set to point to |
| 80036 | +** the first key in its range. |
| 79717 | 80037 | ** |
| 79718 | 80038 | ** If argument eMode is set to INCRINIT_TASK, then pReadr is guaranteed |
| 79719 | 80039 | ** to be a multi-threaded PmaReader and this function is being called in a |
| 79720 | 80040 | ** background thread. In this case all PmaReaders in the sub-tree are |
| 79721 | 80041 | ** initialized as for INCRINIT_NORMAL and the aFile[1] buffer belonging to |
| | @@ -79738,93 +80058,112 @@ |
| 79738 | 80058 | ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. |
| 79739 | 80059 | */ |
| 79740 | 80060 | static int vdbePmaReaderIncrMergeInit(PmaReader *pReadr, int eMode){ |
| 79741 | 80061 | int rc = SQLITE_OK; |
| 79742 | 80062 | IncrMerger *pIncr = pReadr->pIncr; |
| 80063 | + SortSubtask *pTask = pIncr->pTask; |
| 80064 | + sqlite3 *db = pTask->pSorter->db; |
| 79743 | 80065 | |
| 79744 | 80066 | /* eMode is always INCRINIT_NORMAL in single-threaded mode */ |
| 79745 | 80067 | assert( SQLITE_MAX_WORKER_THREADS>0 || eMode==INCRINIT_NORMAL ); |
| 79746 | 80068 | |
| 79747 | | - if( pIncr ){ |
| 79748 | | - SortSubtask *pTask = pIncr->pTask; |
| 79749 | | - sqlite3 *db = pTask->pSorter->db; |
| 79750 | | - |
| 79751 | | - rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode); |
| 79752 | | - |
| 79753 | | - /* Set up the required files for pIncr. A multi-theaded IncrMerge object |
| 79754 | | - ** requires two temp files to itself, whereas a single-threaded object |
| 79755 | | - ** only requires a region of pTask->file2. */ |
| 79756 | | - if( rc==SQLITE_OK ){ |
| 79757 | | - int mxSz = pIncr->mxSz; |
| 79758 | | -#if SQLITE_MAX_WORKER_THREADS>0 |
| 79759 | | - if( pIncr->bUseThread ){ |
| 79760 | | - rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd); |
| 79761 | | - if( rc==SQLITE_OK ){ |
| 79762 | | - rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd); |
| 79763 | | - } |
| 79764 | | - }else |
| 79765 | | -#endif |
| 79766 | | - /*if( !pIncr->bUseThread )*/{ |
| 79767 | | - if( pTask->file2.pFd==0 ){ |
| 79768 | | - assert( pTask->file2.iEof>0 ); |
| 79769 | | - rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd); |
| 79770 | | - pTask->file2.iEof = 0; |
| 79771 | | - } |
| 79772 | | - if( rc==SQLITE_OK ){ |
| 79773 | | - pIncr->aFile[1].pFd = pTask->file2.pFd; |
| 79774 | | - pIncr->iStartOff = pTask->file2.iEof; |
| 79775 | | - pTask->file2.iEof += mxSz; |
| 79776 | | - } |
| 79777 | | - } |
| 79778 | | - } |
| 79779 | | - |
| 79780 | | -#if SQLITE_MAX_WORKER_THREADS>0 |
| 79781 | | - if( rc==SQLITE_OK && pIncr->bUseThread ){ |
| 79782 | | - /* Use the current thread to populate aFile[1], even though this |
| 79783 | | - ** PmaReader is multi-threaded. The reason being that this function |
| 79784 | | - ** is already running in background thread pIncr->pTask->thread. */ |
| 79785 | | - assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK ); |
| 79786 | | - rc = vdbeIncrPopulate(pIncr); |
| 79787 | | - } |
| 79788 | | -#endif |
| 79789 | | - |
| 79790 | | - if( rc==SQLITE_OK |
| 79791 | | - && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) |
| 79792 | | - ){ |
| 79793 | | - rc = vdbePmaReaderNext(pReadr); |
| 79794 | | - } |
| 79795 | | - } |
| 80069 | + rc = vdbeMergeEngineInit(pTask, pIncr->pMerger, eMode); |
| 80070 | + |
| 80071 | + /* Set up the required files for pIncr. A multi-theaded IncrMerge object |
| 80072 | + ** requires two temp files to itself, whereas a single-threaded object |
| 80073 | + ** only requires a region of pTask->file2. */ |
| 80074 | + if( rc==SQLITE_OK ){ |
| 80075 | + int mxSz = pIncr->mxSz; |
| 80076 | +#if SQLITE_MAX_WORKER_THREADS>0 |
| 80077 | + if( pIncr->bUseThread ){ |
| 80078 | + rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[0].pFd); |
| 80079 | + if( rc==SQLITE_OK ){ |
| 80080 | + rc = vdbeSorterOpenTempFile(db, mxSz, &pIncr->aFile[1].pFd); |
| 80081 | + } |
| 80082 | + }else |
| 80083 | +#endif |
| 80084 | + /*if( !pIncr->bUseThread )*/{ |
| 80085 | + if( pTask->file2.pFd==0 ){ |
| 80086 | + assert( pTask->file2.iEof>0 ); |
| 80087 | + rc = vdbeSorterOpenTempFile(db, pTask->file2.iEof, &pTask->file2.pFd); |
| 80088 | + pTask->file2.iEof = 0; |
| 80089 | + } |
| 80090 | + if( rc==SQLITE_OK ){ |
| 80091 | + pIncr->aFile[1].pFd = pTask->file2.pFd; |
| 80092 | + pIncr->iStartOff = pTask->file2.iEof; |
| 80093 | + pTask->file2.iEof += mxSz; |
| 80094 | + } |
| 80095 | + } |
| 80096 | + } |
| 80097 | + |
| 80098 | +#if SQLITE_MAX_WORKER_THREADS>0 |
| 80099 | + if( rc==SQLITE_OK && pIncr->bUseThread ){ |
| 80100 | + /* Use the current thread to populate aFile[1], even though this |
| 80101 | + ** PmaReader is multi-threaded. If this is an INCRINIT_TASK object, |
| 80102 | + ** then this function is already running in background thread |
| 80103 | + ** pIncr->pTask->thread. |
| 80104 | + ** |
| 80105 | + ** If this is the INCRINIT_ROOT object, then it is running in the |
| 80106 | + ** main VDBE thread. But that is Ok, as that thread cannot return |
| 80107 | + ** control to the VDBE or proceed with anything useful until the |
| 80108 | + ** first results are ready from this merger object anyway. |
| 80109 | + */ |
| 80110 | + assert( eMode==INCRINIT_ROOT || eMode==INCRINIT_TASK ); |
| 80111 | + rc = vdbeIncrPopulate(pIncr); |
| 80112 | + } |
| 80113 | +#endif |
| 80114 | + |
| 80115 | + if( rc==SQLITE_OK && (SQLITE_MAX_WORKER_THREADS==0 || eMode!=INCRINIT_TASK) ){ |
| 80116 | + rc = vdbePmaReaderNext(pReadr); |
| 80117 | + } |
| 80118 | + |
| 79796 | 80119 | return rc; |
| 79797 | 80120 | } |
| 79798 | 80121 | |
| 79799 | 80122 | #if SQLITE_MAX_WORKER_THREADS>0 |
| 79800 | 80123 | /* |
| 79801 | 80124 | ** The main routine for vdbePmaReaderIncrMergeInit() operations run in |
| 79802 | 80125 | ** background threads. |
| 79803 | 80126 | */ |
| 79804 | | -static void *vdbePmaReaderBgInit(void *pCtx){ |
| 80127 | +static void *vdbePmaReaderBgIncrInit(void *pCtx){ |
| 79805 | 80128 | PmaReader *pReader = (PmaReader*)pCtx; |
| 79806 | 80129 | void *pRet = SQLITE_INT_TO_PTR( |
| 79807 | 80130 | vdbePmaReaderIncrMergeInit(pReader,INCRINIT_TASK) |
| 79808 | 80131 | ); |
| 79809 | 80132 | pReader->pIncr->pTask->bDone = 1; |
| 79810 | 80133 | return pRet; |
| 79811 | 80134 | } |
| 80135 | +#endif |
| 79812 | 80136 | |
| 79813 | 80137 | /* |
| 79814 | | -** Use a background thread to invoke vdbePmaReaderIncrMergeInit(INCRINIT_TASK) |
| 79815 | | -** on the PmaReader object passed as the first argument. |
| 79816 | | -** |
| 79817 | | -** This call will initialize the various fields of the pReadr->pIncr |
| 79818 | | -** structure and, if it is a multi-threaded IncrMerger, launch a |
| 79819 | | -** background thread to populate aFile[1]. |
| 80138 | +** If the PmaReader passed as the first argument is not an incremental-reader |
| 80139 | +** (if pReadr->pIncr==0), then this function is a no-op. Otherwise, it invokes |
| 80140 | +** the vdbePmaReaderIncrMergeInit() function with the parameters passed to |
| 80141 | +** this routine to initialize the incremental merge. |
| 80142 | +** |
| 80143 | +** If the IncrMerger object is multi-threaded (IncrMerger.bUseThread==1), |
| 80144 | +** then a background thread is launched to call vdbePmaReaderIncrMergeInit(). |
| 80145 | +** Or, if the IncrMerger is single threaded, the same function is called |
| 80146 | +** using the current thread. |
| 79820 | 80147 | */ |
| 79821 | | -static int vdbePmaReaderBgIncrInit(PmaReader *pReadr){ |
| 79822 | | - void *pCtx = (void*)pReadr; |
| 79823 | | - return vdbeSorterCreateThread(pReadr->pIncr->pTask, vdbePmaReaderBgInit, pCtx); |
| 80148 | +static int vdbePmaReaderIncrInit(PmaReader *pReadr, int eMode){ |
| 80149 | + IncrMerger *pIncr = pReadr->pIncr; /* Incremental merger */ |
| 80150 | + int rc = SQLITE_OK; /* Return code */ |
| 80151 | + if( pIncr ){ |
| 80152 | +#if SQLITE_MAX_WORKER_THREADS>0 |
| 80153 | + assert( pIncr->bUseThread==0 || eMode==INCRINIT_TASK ); |
| 80154 | + if( pIncr->bUseThread ){ |
| 80155 | + void *pCtx = (void*)pReadr; |
| 80156 | + rc = vdbeSorterCreateThread(pIncr->pTask, vdbePmaReaderBgIncrInit, pCtx); |
| 80157 | + }else |
| 80158 | +#endif |
| 80159 | + { |
| 80160 | + rc = vdbePmaReaderIncrMergeInit(pReadr, eMode); |
| 80161 | + } |
| 80162 | + } |
| 80163 | + return rc; |
| 79824 | 80164 | } |
| 79825 | | -#endif |
| 79826 | 80165 | |
| 79827 | 80166 | /* |
| 79828 | 80167 | ** Allocate a new MergeEngine object to merge the contents of nPMA level-0 |
| 79829 | 80168 | ** PMAs from pTask->file. If no error occurs, set *ppOut to point to |
| 79830 | 80169 | ** the new object and return SQLITE_OK. Or, if an error does occur, set *ppOut |
| | @@ -80032,10 +80371,15 @@ |
| 80032 | 80371 | int rc; /* Return code */ |
| 80033 | 80372 | SortSubtask *pTask0 = &pSorter->aTask[0]; |
| 80034 | 80373 | MergeEngine *pMain = 0; |
| 80035 | 80374 | #if SQLITE_MAX_WORKER_THREADS |
| 80036 | 80375 | sqlite3 *db = pTask0->pSorter->db; |
| 80376 | + int i; |
| 80377 | + SorterCompare xCompare = vdbeSorterGetCompare(pSorter); |
| 80378 | + for(i=0; i<pSorter->nTask; i++){ |
| 80379 | + pSorter->aTask[i].xCompare = xCompare; |
| 80380 | + } |
| 80037 | 80381 | #endif |
| 80038 | 80382 | |
| 80039 | 80383 | rc = vdbeSorterMergeTreeBuild(pSorter, &pMain); |
| 80040 | 80384 | if( rc==SQLITE_OK ){ |
| 80041 | 80385 | #if SQLITE_MAX_WORKER_THREADS |
| | @@ -80060,19 +80404,25 @@ |
| 80060 | 80404 | vdbeIncrMergerSetThreads(pIncr); |
| 80061 | 80405 | assert( pIncr->pTask!=pLast ); |
| 80062 | 80406 | } |
| 80063 | 80407 | } |
| 80064 | 80408 | for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){ |
| 80409 | + /* Check that: |
| 80410 | + ** |
| 80411 | + ** a) The incremental merge object is configured to use the |
| 80412 | + ** right task, and |
| 80413 | + ** b) If it is using task (nTask-1), it is configured to run |
| 80414 | + ** in single-threaded mode. This is important, as the |
| 80415 | + ** root merge (INCRINIT_ROOT) will be using the same task |
| 80416 | + ** object. |
| 80417 | + */ |
| 80065 | 80418 | PmaReader *p = &pMain->aReadr[iTask]; |
| 80066 | | - assert( p->pIncr==0 || p->pIncr->pTask==&pSorter->aTask[iTask] ); |
| 80067 | | - if( p->pIncr ){ |
| 80068 | | - if( iTask==pSorter->nTask-1 ){ |
| 80069 | | - rc = vdbePmaReaderIncrMergeInit(p, INCRINIT_TASK); |
| 80070 | | - }else{ |
| 80071 | | - rc = vdbePmaReaderBgIncrInit(p); |
| 80072 | | - } |
| 80073 | | - } |
| 80419 | + assert( p->pIncr==0 || ( |
| 80420 | + (p->pIncr->pTask==&pSorter->aTask[iTask]) /* a */ |
| 80421 | + && (iTask!=pSorter->nTask-1 || p->pIncr->bUseThread==0) /* b */ |
| 80422 | + )); |
| 80423 | + rc = vdbePmaReaderIncrInit(p, INCRINIT_TASK); |
| 80074 | 80424 | } |
| 80075 | 80425 | } |
| 80076 | 80426 | pMain = 0; |
| 80077 | 80427 | } |
| 80078 | 80428 | if( rc==SQLITE_OK ){ |
| | @@ -81023,11 +81373,11 @@ |
| 81023 | 81373 | ** Should be transformed into: |
| 81024 | 81374 | ** |
| 81025 | 81375 | ** SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase; |
| 81026 | 81376 | ** |
| 81027 | 81377 | ** The nSubquery parameter specifies how many levels of subquery the |
| 81028 | | -** alias is removed from the original expression. The usually value is |
| 81378 | +** alias is removed from the original expression. The usual value is |
| 81029 | 81379 | ** zero but it might be more if the alias is contained within a subquery |
| 81030 | 81380 | ** of the original expression. The Expr.op2 field of TK_AGG_FUNCTION |
| 81031 | 81381 | ** structures must be increased by the nSubquery amount. |
| 81032 | 81382 | */ |
| 81033 | 81383 | static void resolveAlias( |
| | @@ -81043,11 +81393,10 @@ |
| 81043 | 81393 | sqlite3 *db; /* The database connection */ |
| 81044 | 81394 | |
| 81045 | 81395 | assert( iCol>=0 && iCol<pEList->nExpr ); |
| 81046 | 81396 | pOrig = pEList->a[iCol].pExpr; |
| 81047 | 81397 | assert( pOrig!=0 ); |
| 81048 | | - assert( pOrig->flags & EP_Resolved ); |
| 81049 | 81398 | db = pParse->db; |
| 81050 | 81399 | pDup = sqlite3ExprDup(db, pOrig, 0); |
| 81051 | 81400 | if( pDup==0 ) return; |
| 81052 | 81401 | if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){ |
| 81053 | 81402 | incrAggFunctionDepth(pDup, nSubquery); |
| | @@ -81937,13 +82286,15 @@ |
| 81937 | 82286 | pNew->flags |= EP_IntValue; |
| 81938 | 82287 | pNew->u.iValue = iCol; |
| 81939 | 82288 | if( pItem->pExpr==pE ){ |
| 81940 | 82289 | pItem->pExpr = pNew; |
| 81941 | 82290 | }else{ |
| 81942 | | - assert( pItem->pExpr->op==TK_COLLATE ); |
| 81943 | | - assert( pItem->pExpr->pLeft==pE ); |
| 81944 | | - pItem->pExpr->pLeft = pNew; |
| 82291 | + Expr *pParent = pItem->pExpr; |
| 82292 | + assert( pParent->op==TK_COLLATE ); |
| 82293 | + while( pParent->pLeft->op==TK_COLLATE ) pParent = pParent->pLeft; |
| 82294 | + assert( pParent->pLeft==pE ); |
| 82295 | + pParent->pLeft = pNew; |
| 81945 | 82296 | } |
| 81946 | 82297 | sqlite3ExprDelete(db, pE); |
| 81947 | 82298 | pItem->u.x.iOrderByCol = (u16)iCol; |
| 81948 | 82299 | pItem->done = 1; |
| 81949 | 82300 | }else{ |
| | @@ -82139,11 +82490,11 @@ |
| 82139 | 82490 | ** as if it were part of the sub-query, not the parent. This block |
| 82140 | 82491 | ** moves the pOrderBy down to the sub-query. It will be moved back |
| 82141 | 82492 | ** after the names have been resolved. */ |
| 82142 | 82493 | if( p->selFlags & SF_Converted ){ |
| 82143 | 82494 | Select *pSub = p->pSrc->a[0].pSelect; |
| 82144 | | - assert( p->pSrc->nSrc==1 && isCompound==0 && p->pOrderBy ); |
| 82495 | + assert( p->pSrc->nSrc==1 && p->pOrderBy ); |
| 82145 | 82496 | assert( pSub->pPrior && pSub->pOrderBy==0 ); |
| 82146 | 82497 | pSub->pOrderBy = p->pOrderBy; |
| 82147 | 82498 | p->pOrderBy = 0; |
| 82148 | 82499 | } |
| 82149 | 82500 | |
| | @@ -82241,12 +82592,19 @@ |
| 82241 | 82592 | |
| 82242 | 82593 | /* Process the ORDER BY clause for singleton SELECT statements. |
| 82243 | 82594 | ** The ORDER BY clause for compounds SELECT statements is handled |
| 82244 | 82595 | ** below, after all of the result-sets for all of the elements of |
| 82245 | 82596 | ** the compound have been resolved. |
| 82597 | + ** |
| 82598 | + ** If there is an ORDER BY clause on a term of a compound-select other |
| 82599 | + ** than the right-most term, then that is a syntax error. But the error |
| 82600 | + ** is not detected until much later, and so we need to go ahead and |
| 82601 | + ** resolve those symbols on the incorrect ORDER BY for consistency. |
| 82246 | 82602 | */ |
| 82247 | | - if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){ |
| 82603 | + if( isCompound<=nCompound /* Defer right-most ORDER BY of a compound */ |
| 82604 | + && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") |
| 82605 | + ){ |
| 82248 | 82606 | return WRC_Abort; |
| 82249 | 82607 | } |
| 82250 | 82608 | if( db->mallocFailed ){ |
| 82251 | 82609 | return WRC_Abort; |
| 82252 | 82610 | } |
| | @@ -83694,11 +84052,12 @@ |
| 83694 | 84052 | SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList *pList){ |
| 83695 | 84053 | int i; |
| 83696 | 84054 | u32 m = 0; |
| 83697 | 84055 | if( pList ){ |
| 83698 | 84056 | for(i=0; i<pList->nExpr; i++){ |
| 83699 | | - m |= pList->a[i].pExpr->flags; |
| 84057 | + Expr *pExpr = pList->a[i].pExpr; |
| 84058 | + if( ALWAYS(pExpr) ) m |= pExpr->flags; |
| 83700 | 84059 | } |
| 83701 | 84060 | } |
| 83702 | 84061 | return m; |
| 83703 | 84062 | } |
| 83704 | 84063 | |
| | @@ -84134,11 +84493,11 @@ |
| 84134 | 84493 | /* Check to see if an existing table or index can be used to |
| 84135 | 84494 | ** satisfy the query. This is preferable to generating a new |
| 84136 | 84495 | ** ephemeral table. |
| 84137 | 84496 | */ |
| 84138 | 84497 | p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0); |
| 84139 | | - if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){ |
| 84498 | + if( pParse->nErr==0 && isCandidateForInOpt(p) ){ |
| 84140 | 84499 | sqlite3 *db = pParse->db; /* Database connection */ |
| 84141 | 84500 | Table *pTab; /* Table <table>. */ |
| 84142 | 84501 | Expr *pExpr; /* Expression <column> */ |
| 84143 | 84502 | i16 iCol; /* Index of column <column> */ |
| 84144 | 84503 | i16 iDb; /* Database idx for pTab */ |
| | @@ -84459,10 +84818,11 @@ |
| 84459 | 84818 | } |
| 84460 | 84819 | sqlite3ExprDelete(pParse->db, pSel->pLimit); |
| 84461 | 84820 | pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, |
| 84462 | 84821 | &sqlite3IntTokens[1]); |
| 84463 | 84822 | pSel->iLimit = 0; |
| 84823 | + pSel->selFlags &= ~SF_MultiValue; |
| 84464 | 84824 | if( sqlite3Select(pParse, pSel, &dest) ){ |
| 84465 | 84825 | return 0; |
| 84466 | 84826 | } |
| 84467 | 84827 | rReg = dest.iSDParm; |
| 84468 | 84828 | ExprSetVVAProperty(pExpr, EP_NoReduce); |
| | @@ -85824,11 +86184,11 @@ |
| 85824 | 86184 | sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken); |
| 85825 | 86185 | sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 85826 | 86186 | break; |
| 85827 | 86187 | } |
| 85828 | 86188 | case TK_ID: { |
| 85829 | | - sqlite3TreeViewLine(pView,"ID %Q", pExpr->u.zToken); |
| 86189 | + sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); |
| 85830 | 86190 | break; |
| 85831 | 86191 | } |
| 85832 | 86192 | #ifndef SQLITE_OMIT_CAST |
| 85833 | 86193 | case TK_CAST: { |
| 85834 | 86194 | /* Expressions of the form: CAST(pLeft AS token) */ |
| | @@ -86459,11 +86819,11 @@ |
| 86459 | 86819 | if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ |
| 86460 | 86820 | if( combinedFlags & EP_xIsSelect ) return 2; |
| 86461 | 86821 | if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; |
| 86462 | 86822 | if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; |
| 86463 | 86823 | if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; |
| 86464 | | - if( ALWAYS((combinedFlags & EP_Reduced)==0) ){ |
| 86824 | + if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){ |
| 86465 | 86825 | if( pA->iColumn!=pB->iColumn ) return 2; |
| 86466 | 86826 | if( pA->iTable!=pB->iTable |
| 86467 | 86827 | && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; |
| 86468 | 86828 | } |
| 86469 | 86829 | } |
| | @@ -86991,10 +87351,11 @@ |
| 86991 | 87351 | do { |
| 86992 | 87352 | z += n; |
| 86993 | 87353 | n = sqlite3GetToken(z, &token); |
| 86994 | 87354 | }while( token==TK_SPACE ); |
| 86995 | 87355 | |
| 87356 | + if( token==TK_ILLEGAL ) break; |
| 86996 | 87357 | zParent = sqlite3DbStrNDup(db, (const char *)z, n); |
| 86997 | 87358 | if( zParent==0 ) break; |
| 86998 | 87359 | sqlite3Dequote(zParent); |
| 86999 | 87360 | if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){ |
| 87000 | 87361 | char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", |
| | @@ -89217,18 +89578,21 @@ |
| 89217 | 89578 | pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); |
| 89218 | 89579 | } |
| 89219 | 89580 | z = argv[2]; |
| 89220 | 89581 | |
| 89221 | 89582 | if( pIndex ){ |
| 89583 | + tRowcnt *aiRowEst = 0; |
| 89222 | 89584 | int nCol = pIndex->nKeyCol+1; |
| 89223 | 89585 | #ifdef SQLITE_ENABLE_STAT3_OR_STAT4 |
| 89224 | | - tRowcnt * const aiRowEst = pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero( |
| 89225 | | - sizeof(tRowcnt) * nCol |
| 89226 | | - ); |
| 89227 | | - if( aiRowEst==0 ) pInfo->db->mallocFailed = 1; |
| 89228 | | -#else |
| 89229 | | - tRowcnt * const aiRowEst = 0; |
| 89586 | + /* Index.aiRowEst may already be set here if there are duplicate |
| 89587 | + ** sqlite_stat1 entries for this index. In that case just clobber |
| 89588 | + ** the old data with the new instead of allocating a new array. */ |
| 89589 | + if( pIndex->aiRowEst==0 ){ |
| 89590 | + pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol); |
| 89591 | + if( pIndex->aiRowEst==0 ) pInfo->db->mallocFailed = 1; |
| 89592 | + } |
| 89593 | + aiRowEst = pIndex->aiRowEst; |
| 89230 | 89594 | #endif |
| 89231 | 89595 | pIndex->bUnordered = 0; |
| 89232 | 89596 | decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex); |
| 89233 | 89597 | if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0]; |
| 89234 | 89598 | }else{ |
| | @@ -89887,11 +90251,11 @@ |
| 89887 | 90251 | } |
| 89888 | 90252 | |
| 89889 | 90253 | sqlite3BtreeClose(pDb->pBt); |
| 89890 | 90254 | pDb->pBt = 0; |
| 89891 | 90255 | pDb->pSchema = 0; |
| 89892 | | - sqlite3ResetAllSchemasOfConnection(db); |
| 90256 | + sqlite3CollapseDatabaseArray(db); |
| 89893 | 90257 | return; |
| 89894 | 90258 | |
| 89895 | 90259 | detach_error: |
| 89896 | 90260 | sqlite3_result_error(context, zErr, -1); |
| 89897 | 90261 | } |
| | @@ -89921,11 +90285,10 @@ |
| 89921 | 90285 | if( |
| 89922 | 90286 | SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) || |
| 89923 | 90287 | SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) || |
| 89924 | 90288 | SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey)) |
| 89925 | 90289 | ){ |
| 89926 | | - pParse->nErr++; |
| 89927 | 90290 | goto attach_end; |
| 89928 | 90291 | } |
| 89929 | 90292 | |
| 89930 | 90293 | #ifndef SQLITE_OMIT_AUTHORIZATION |
| 89931 | 90294 | if( pAuthArg ){ |
| | @@ -90580,13 +90943,15 @@ |
| 90580 | 90943 | sqlite3 *db; |
| 90581 | 90944 | Vdbe *v; |
| 90582 | 90945 | |
| 90583 | 90946 | assert( pParse->pToplevel==0 ); |
| 90584 | 90947 | db = pParse->db; |
| 90585 | | - if( db->mallocFailed ) return; |
| 90586 | 90948 | if( pParse->nested ) return; |
| 90587 | | - if( pParse->nErr ) return; |
| 90949 | + if( db->mallocFailed || pParse->nErr ){ |
| 90950 | + if( pParse->rc==SQLITE_OK ) pParse->rc = SQLITE_ERROR; |
| 90951 | + return; |
| 90952 | + } |
| 90588 | 90953 | |
| 90589 | 90954 | /* Begin by generating some termination code at the end of the |
| 90590 | 90955 | ** vdbe program |
| 90591 | 90956 | */ |
| 90592 | 90957 | v = sqlite3GetVdbe(pParse); |
| | @@ -90664,11 +91029,11 @@ |
| 90664 | 91029 | } |
| 90665 | 91030 | |
| 90666 | 91031 | |
| 90667 | 91032 | /* Get the VDBE program ready for execution |
| 90668 | 91033 | */ |
| 90669 | | - if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){ |
| 91034 | + if( v && pParse->nErr==0 && !db->mallocFailed ){ |
| 90670 | 91035 | assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ |
| 90671 | 91036 | /* A minimum of one cursor is required if autoincrement is used |
| 90672 | 91037 | * See ticket [a696379c1f08866] */ |
| 90673 | 91038 | if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; |
| 90674 | 91039 | sqlite3VdbeMakeReady(v, pParse); |
| | @@ -91199,18 +91564,16 @@ |
| 91199 | 91564 | sqlite3 *db = pParse->db; |
| 91200 | 91565 | |
| 91201 | 91566 | if( ALWAYS(pName2!=0) && pName2->n>0 ){ |
| 91202 | 91567 | if( db->init.busy ) { |
| 91203 | 91568 | sqlite3ErrorMsg(pParse, "corrupt database"); |
| 91204 | | - pParse->nErr++; |
| 91205 | 91569 | return -1; |
| 91206 | 91570 | } |
| 91207 | 91571 | *pUnqual = pName2; |
| 91208 | 91572 | iDb = sqlite3FindDb(db, pName1); |
| 91209 | 91573 | if( iDb<0 ){ |
| 91210 | 91574 | sqlite3ErrorMsg(pParse, "unknown database %T", pName1); |
| 91211 | | - pParse->nErr++; |
| 91212 | 91575 | return -1; |
| 91213 | 91576 | } |
| 91214 | 91577 | }else{ |
| 91215 | 91578 | assert( db->init.iDb==0 || db->init.busy ); |
| 91216 | 91579 | iDb = db->init.iDb; |
| | @@ -91365,11 +91728,11 @@ |
| 91365 | 91728 | pTable = sqlite3FindTable(db, zName, zDb); |
| 91366 | 91729 | if( pTable ){ |
| 91367 | 91730 | if( !noErr ){ |
| 91368 | 91731 | sqlite3ErrorMsg(pParse, "table %T already exists", pName); |
| 91369 | 91732 | }else{ |
| 91370 | | - assert( !db->init.busy ); |
| 91733 | + assert( !db->init.busy || CORRUPT_DB ); |
| 91371 | 91734 | sqlite3CodeVerifySchema(pParse, iDb); |
| 91372 | 91735 | } |
| 91373 | 91736 | goto begin_table_error; |
| 91374 | 91737 | } |
| 91375 | 91738 | if( sqlite3FindIndex(db, zName, zDb)!=0 ){ |
| | @@ -91654,11 +92017,12 @@ |
| 91654 | 92017 | Column *pCol; |
| 91655 | 92018 | |
| 91656 | 92019 | p = pParse->pNewTable; |
| 91657 | 92020 | if( p==0 || NEVER(p->nCol<1) ) return; |
| 91658 | 92021 | pCol = &p->aCol[p->nCol-1]; |
| 91659 | | - assert( pCol->zType==0 ); |
| 92022 | + assert( pCol->zType==0 || CORRUPT_DB ); |
| 92023 | + sqlite3DbFree(pParse->db, pCol->zType); |
| 91660 | 92024 | pCol->zType = sqlite3NameFromToken(pParse->db, pType); |
| 91661 | 92025 | pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst); |
| 91662 | 92026 | } |
| 91663 | 92027 | |
| 91664 | 92028 | /* |
| | @@ -92888,10 +93252,11 @@ |
| 92888 | 93252 | if( db->mallocFailed ){ |
| 92889 | 93253 | goto exit_drop_table; |
| 92890 | 93254 | } |
| 92891 | 93255 | assert( pParse->nErr==0 ); |
| 92892 | 93256 | assert( pName->nSrc==1 ); |
| 93257 | + if( sqlite3ReadSchema(pParse) ) goto exit_drop_table; |
| 92893 | 93258 | if( noErr ) db->suppressErr++; |
| 92894 | 93259 | pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]); |
| 92895 | 93260 | if( noErr ) db->suppressErr--; |
| 92896 | 93261 | |
| 92897 | 93262 | if( pTab==0 ){ |
| | @@ -93201,11 +93566,12 @@ |
| 93201 | 93566 | sqlite3UniqueConstraint(pParse, OE_Abort, pIndex); |
| 93202 | 93567 | }else{ |
| 93203 | 93568 | addr2 = sqlite3VdbeCurrentAddr(v); |
| 93204 | 93569 | } |
| 93205 | 93570 | sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx); |
| 93206 | | - sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1); |
| 93571 | + sqlite3VdbeAddOp3(v, OP_Last, iIdx, 0, -1); |
| 93572 | + sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0); |
| 93207 | 93573 | sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); |
| 93208 | 93574 | sqlite3ReleaseTempReg(pParse, regRecord); |
| 93209 | 93575 | sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v); |
| 93210 | 93576 | sqlite3VdbeJumpHere(v, addr1); |
| 93211 | 93577 | |
| | @@ -93294,12 +93660,11 @@ |
| 93294 | 93660 | int nExtra = 0; /* Space allocated for zExtra[] */ |
| 93295 | 93661 | int nExtraCol; /* Number of extra columns needed */ |
| 93296 | 93662 | char *zExtra = 0; /* Extra space after the Index object */ |
| 93297 | 93663 | Index *pPk = 0; /* PRIMARY KEY index for WITHOUT ROWID tables */ |
| 93298 | 93664 | |
| 93299 | | - assert( pParse->nErr==0 ); /* Never called with prior errors */ |
| 93300 | | - if( db->mallocFailed || IN_DECLARE_VTAB ){ |
| 93665 | + if( db->mallocFailed || IN_DECLARE_VTAB || pParse->nErr>0 ){ |
| 93301 | 93666 | goto exit_create_index; |
| 93302 | 93667 | } |
| 93303 | 93668 | if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ |
| 93304 | 93669 | goto exit_create_index; |
| 93305 | 93670 | } |
| | @@ -94214,11 +94579,10 @@ |
| 94214 | 94579 | ** operator with A. This routine shifts that operator over to B. |
| 94215 | 94580 | */ |
| 94216 | 94581 | SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){ |
| 94217 | 94582 | if( p ){ |
| 94218 | 94583 | int i; |
| 94219 | | - assert( p->a || p->nSrc==0 ); |
| 94220 | 94584 | for(i=p->nSrc-1; i>0; i--){ |
| 94221 | 94585 | p->a[i].jointype = p->a[i-1].jointype; |
| 94222 | 94586 | } |
| 94223 | 94587 | p->a[0].jointype = 0; |
| 94224 | 94588 | } |
| | @@ -94461,12 +94825,11 @@ |
| 94461 | 94825 | char *zErr; |
| 94462 | 94826 | int j; |
| 94463 | 94827 | StrAccum errMsg; |
| 94464 | 94828 | Table *pTab = pIdx->pTable; |
| 94465 | 94829 | |
| 94466 | | - sqlite3StrAccumInit(&errMsg, 0, 0, 200); |
| 94467 | | - errMsg.db = pParse->db; |
| 94830 | + sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200); |
| 94468 | 94831 | for(j=0; j<pIdx->nKeyCol; j++){ |
| 94469 | 94832 | char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; |
| 94470 | 94833 | if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2); |
| 94471 | 94834 | sqlite3StrAccumAppendAll(&errMsg, pTab->zName); |
| 94472 | 94835 | sqlite3StrAccumAppend(&errMsg, ".", 1); |
| | @@ -96291,17 +96654,17 @@ |
| 96291 | 96654 | ){ |
| 96292 | 96655 | PrintfArguments x; |
| 96293 | 96656 | StrAccum str; |
| 96294 | 96657 | const char *zFormat; |
| 96295 | 96658 | int n; |
| 96659 | + sqlite3 *db = sqlite3_context_db_handle(context); |
| 96296 | 96660 | |
| 96297 | 96661 | if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){ |
| 96298 | 96662 | x.nArg = argc-1; |
| 96299 | 96663 | x.nUsed = 0; |
| 96300 | 96664 | x.apArg = argv+1; |
| 96301 | | - sqlite3StrAccumInit(&str, 0, 0, SQLITE_MAX_LENGTH); |
| 96302 | | - str.db = sqlite3_context_db_handle(context); |
| 96665 | + sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]); |
| 96303 | 96666 | sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x); |
| 96304 | 96667 | n = str.nChar; |
| 96305 | 96668 | sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n, |
| 96306 | 96669 | SQLITE_DYNAMIC); |
| 96307 | 96670 | } |
| | @@ -96447,11 +96810,11 @@ |
| 96447 | 96810 | sqlite3_result_double(context, r); |
| 96448 | 96811 | } |
| 96449 | 96812 | #endif |
| 96450 | 96813 | |
| 96451 | 96814 | /* |
| 96452 | | -** Allocate nByte bytes of space using sqlite3_malloc(). If the |
| 96815 | +** Allocate nByte bytes of space using sqlite3Malloc(). If the |
| 96453 | 96816 | ** allocation fails, call sqlite3_result_error_nomem() to notify |
| 96454 | 96817 | ** the database handle that malloc() has failed and return NULL. |
| 96455 | 96818 | ** If nByte is larger than the maximum string or blob length, then |
| 96456 | 96819 | ** raise an SQLITE_TOOBIG exception and return NULL. |
| 96457 | 96820 | */ |
| | @@ -97116,11 +97479,11 @@ |
| 97116 | 97479 | int argc, |
| 97117 | 97480 | sqlite3_value **argv |
| 97118 | 97481 | ){ |
| 97119 | 97482 | unsigned char *z, *zOut; |
| 97120 | 97483 | int i; |
| 97121 | | - zOut = z = sqlite3_malloc( argc*4+1 ); |
| 97484 | + zOut = z = sqlite3_malloc64( argc*4+1 ); |
| 97122 | 97485 | if( z==0 ){ |
| 97123 | 97486 | sqlite3_result_error_nomem(context); |
| 97124 | 97487 | return; |
| 97125 | 97488 | } |
| 97126 | 97489 | for(i=0; i<argc; i++){ |
| | @@ -97264,11 +97627,11 @@ |
| 97264 | 97627 | sqlite3_result_error_toobig(context); |
| 97265 | 97628 | sqlite3_free(zOut); |
| 97266 | 97629 | return; |
| 97267 | 97630 | } |
| 97268 | 97631 | zOld = zOut; |
| 97269 | | - zOut = sqlite3_realloc(zOut, (int)nOut); |
| 97632 | + zOut = sqlite3_realloc64(zOut, (int)nOut); |
| 97270 | 97633 | if( zOut==0 ){ |
| 97271 | 97634 | sqlite3_result_error_nomem(context); |
| 97272 | 97635 | sqlite3_free(zOld); |
| 97273 | 97636 | return; |
| 97274 | 97637 | } |
| | @@ -97626,12 +97989,11 @@ |
| 97626 | 97989 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 97627 | 97990 | pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); |
| 97628 | 97991 | |
| 97629 | 97992 | if( pAccum ){ |
| 97630 | 97993 | sqlite3 *db = sqlite3_context_db_handle(context); |
| 97631 | | - int firstTerm = pAccum->useMalloc==0; |
| 97632 | | - pAccum->useMalloc = 2; |
| 97994 | + int firstTerm = pAccum->mxAlloc==0; |
| 97633 | 97995 | pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; |
| 97634 | 97996 | if( !firstTerm ){ |
| 97635 | 97997 | if( argc==2 ){ |
| 97636 | 97998 | zSep = (char*)sqlite3_value_text(argv[1]); |
| 97637 | 97999 | nSep = sqlite3_value_bytes(argv[1]); |
| | @@ -99047,11 +99409,12 @@ |
| 99047 | 99409 | int iFromCol; /* Idx of column in child table */ |
| 99048 | 99410 | Expr *pEq; /* tFromCol = OLD.tToCol */ |
| 99049 | 99411 | |
| 99050 | 99412 | iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; |
| 99051 | 99413 | assert( iFromCol>=0 ); |
| 99052 | | - tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid"; |
| 99414 | + assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) ); |
| 99415 | + tToCol.z = pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName; |
| 99053 | 99416 | tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName; |
| 99054 | 99417 | |
| 99055 | 99418 | tToCol.n = sqlite3Strlen30(tToCol.z); |
| 99056 | 99419 | tFromCol.n = sqlite3Strlen30(tFromCol.z); |
| 99057 | 99420 | |
| | @@ -99059,14 +99422,14 @@ |
| 99059 | 99422 | ** that the "OLD.zToCol" term is on the LHS of the = operator, so |
| 99060 | 99423 | ** that the affinity and collation sequence associated with the |
| 99061 | 99424 | ** parent table are used for the comparison. */ |
| 99062 | 99425 | pEq = sqlite3PExpr(pParse, TK_EQ, |
| 99063 | 99426 | sqlite3PExpr(pParse, TK_DOT, |
| 99064 | | - sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld), |
| 99065 | | - sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol) |
| 99427 | + sqlite3ExprAlloc(db, TK_ID, &tOld, 0), |
| 99428 | + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0) |
| 99066 | 99429 | , 0), |
| 99067 | | - sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol) |
| 99430 | + sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0) |
| 99068 | 99431 | , 0); |
| 99069 | 99432 | pWhere = sqlite3ExprAnd(db, pWhere, pEq); |
| 99070 | 99433 | |
| 99071 | 99434 | /* For ON UPDATE, construct the next term of the WHEN clause. |
| 99072 | 99435 | ** The final WHEN clause will be like this: |
| | @@ -99074,27 +99437,27 @@ |
| 99074 | 99437 | ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN) |
| 99075 | 99438 | */ |
| 99076 | 99439 | if( pChanges ){ |
| 99077 | 99440 | pEq = sqlite3PExpr(pParse, TK_IS, |
| 99078 | 99441 | sqlite3PExpr(pParse, TK_DOT, |
| 99079 | | - sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld), |
| 99080 | | - sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol), |
| 99442 | + sqlite3ExprAlloc(db, TK_ID, &tOld, 0), |
| 99443 | + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0), |
| 99081 | 99444 | 0), |
| 99082 | 99445 | sqlite3PExpr(pParse, TK_DOT, |
| 99083 | | - sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew), |
| 99084 | | - sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol), |
| 99446 | + sqlite3ExprAlloc(db, TK_ID, &tNew, 0), |
| 99447 | + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0), |
| 99085 | 99448 | 0), |
| 99086 | 99449 | 0); |
| 99087 | 99450 | pWhen = sqlite3ExprAnd(db, pWhen, pEq); |
| 99088 | 99451 | } |
| 99089 | 99452 | |
| 99090 | 99453 | if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){ |
| 99091 | 99454 | Expr *pNew; |
| 99092 | 99455 | if( action==OE_Cascade ){ |
| 99093 | 99456 | pNew = sqlite3PExpr(pParse, TK_DOT, |
| 99094 | | - sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew), |
| 99095 | | - sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol) |
| 99457 | + sqlite3ExprAlloc(db, TK_ID, &tNew, 0), |
| 99458 | + sqlite3ExprAlloc(db, TK_ID, &tToCol, 0) |
| 99096 | 99459 | , 0); |
| 99097 | 99460 | }else if( action==OE_SetDflt ){ |
| 99098 | 99461 | Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt; |
| 99099 | 99462 | if( pDflt ){ |
| 99100 | 99463 | pNew = sqlite3ExprDup(db, pDflt, 0); |
| | @@ -99137,17 +99500,16 @@ |
| 99137 | 99500 | db->lookaside.bEnabled = 0; |
| 99138 | 99501 | |
| 99139 | 99502 | pTrigger = (Trigger *)sqlite3DbMallocZero(db, |
| 99140 | 99503 | sizeof(Trigger) + /* struct Trigger */ |
| 99141 | 99504 | sizeof(TriggerStep) + /* Single step in trigger program */ |
| 99142 | | - nFrom + 1 /* Space for pStep->target.z */ |
| 99505 | + nFrom + 1 /* Space for pStep->zTarget */ |
| 99143 | 99506 | ); |
| 99144 | 99507 | if( pTrigger ){ |
| 99145 | 99508 | pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1]; |
| 99146 | | - pStep->target.z = (char *)&pStep[1]; |
| 99147 | | - pStep->target.n = nFrom; |
| 99148 | | - memcpy((char *)pStep->target.z, zFrom, nFrom); |
| 99509 | + pStep->zTarget = (char *)&pStep[1]; |
| 99510 | + memcpy((char *)pStep->zTarget, zFrom, nFrom); |
| 99149 | 99511 | |
| 99150 | 99512 | pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); |
| 99151 | 99513 | pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE); |
| 99152 | 99514 | pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); |
| 99153 | 99515 | if( pWhen ){ |
| | @@ -99608,24 +99970,27 @@ |
| 99608 | 99970 | ); |
| 99609 | 99971 | |
| 99610 | 99972 | /* |
| 99611 | 99973 | ** This routine is called to handle SQL of the following forms: |
| 99612 | 99974 | ** |
| 99613 | | -** insert into TABLE (IDLIST) values(EXPRLIST) |
| 99975 | +** insert into TABLE (IDLIST) values(EXPRLIST),(EXPRLIST),... |
| 99614 | 99976 | ** insert into TABLE (IDLIST) select |
| 99977 | +** insert into TABLE (IDLIST) default values |
| 99615 | 99978 | ** |
| 99616 | 99979 | ** The IDLIST following the table name is always optional. If omitted, |
| 99617 | | -** then a list of all columns for the table is substituted. The IDLIST |
| 99618 | | -** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. |
| 99980 | +** then a list of all (non-hidden) columns for the table is substituted. |
| 99981 | +** The IDLIST appears in the pColumn parameter. pColumn is NULL if IDLIST |
| 99982 | +** is omitted. |
| 99619 | 99983 | ** |
| 99620 | | -** The pList parameter holds EXPRLIST in the first form of the INSERT |
| 99621 | | -** statement above, and pSelect is NULL. For the second form, pList is |
| 99622 | | -** NULL and pSelect is a pointer to the select statement used to generate |
| 99623 | | -** data for the insert. |
| 99984 | +** For the pSelect parameter holds the values to be inserted for the |
| 99985 | +** first two forms shown above. A VALUES clause is really just short-hand |
| 99986 | +** for a SELECT statement that omits the FROM clause and everything else |
| 99987 | +** that follows. If the pSelect parameter is NULL, that means that the |
| 99988 | +** DEFAULT VALUES form of the INSERT statement is intended. |
| 99624 | 99989 | ** |
| 99625 | 99990 | ** The code generated follows one of four templates. For a simple |
| 99626 | | -** insert with data coming from a VALUES clause, the code executes |
| 99991 | +** insert with data coming from a single-row VALUES clause, the code executes |
| 99627 | 99992 | ** once straight down through. Pseudo-code follows (we call this |
| 99628 | 99993 | ** the "1st template"): |
| 99629 | 99994 | ** |
| 99630 | 99995 | ** open write cursor to <table> and its indices |
| 99631 | 99996 | ** put VALUES clause expressions into registers |
| | @@ -99728,11 +100093,11 @@ |
| 99728 | 100093 | int iDb; /* Index of database holding TABLE */ |
| 99729 | 100094 | Db *pDb; /* The database containing table being inserted into */ |
| 99730 | 100095 | u8 useTempTable = 0; /* Store SELECT results in intermediate table */ |
| 99731 | 100096 | u8 appendFlag = 0; /* True if the insert is likely to be an append */ |
| 99732 | 100097 | u8 withoutRowid; /* 0 for normal table. 1 for WITHOUT ROWID table */ |
| 99733 | | - u8 bIdListInOrder = 1; /* True if IDLIST is in table order */ |
| 100098 | + u8 bIdListInOrder; /* True if IDLIST is in table order */ |
| 99734 | 100099 | ExprList *pList = 0; /* List of VALUES() to be inserted */ |
| 99735 | 100100 | |
| 99736 | 100101 | /* Register allocations */ |
| 99737 | 100102 | int regFromSelect = 0;/* Base register for data coming from SELECT */ |
| 99738 | 100103 | int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ |
| | @@ -99753,12 +100118,12 @@ |
| 99753 | 100118 | if( pParse->nErr || db->mallocFailed ){ |
| 99754 | 100119 | goto insert_cleanup; |
| 99755 | 100120 | } |
| 99756 | 100121 | |
| 99757 | 100122 | /* If the Select object is really just a simple VALUES() list with a |
| 99758 | | - ** single row values (the common case) then keep that one row of values |
| 99759 | | - ** and go ahead and discard the Select object |
| 100123 | + ** single row (the common case) then keep that one row of values |
| 100124 | + ** and discard the other (unused) parts of the pSelect object |
| 99760 | 100125 | */ |
| 99761 | 100126 | if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){ |
| 99762 | 100127 | pList = pSelect->pEList; |
| 99763 | 100128 | pSelect->pEList = 0; |
| 99764 | 100129 | sqlite3SelectDelete(db, pSelect); |
| | @@ -99862,10 +100227,11 @@ |
| 99862 | 100227 | ** the index into IDLIST of the primary key column. ipkColumn is |
| 99863 | 100228 | ** the index of the primary key as it appears in IDLIST, not as |
| 99864 | 100229 | ** is appears in the original table. (The index of the INTEGER |
| 99865 | 100230 | ** PRIMARY KEY in the original table is pTab->iPKey.) |
| 99866 | 100231 | */ |
| 100232 | + bIdListInOrder = (pTab->tabFlags & TF_OOOHidden)==0; |
| 99867 | 100233 | if( pColumn ){ |
| 99868 | 100234 | for(i=0; i<pColumn->nId; i++){ |
| 99869 | 100235 | pColumn->a[i].idx = -1; |
| 99870 | 100236 | } |
| 99871 | 100237 | for(i=0; i<pColumn->nId; i++){ |
| | @@ -99897,11 +100263,12 @@ |
| 99897 | 100263 | ** is coming from a SELECT statement, then generate a co-routine that |
| 99898 | 100264 | ** produces a single row of the SELECT on each invocation. The |
| 99899 | 100265 | ** co-routine is the common header to the 3rd and 4th templates. |
| 99900 | 100266 | */ |
| 99901 | 100267 | if( pSelect ){ |
| 99902 | | - /* Data is coming from a SELECT. Generate a co-routine to run the SELECT */ |
| 100268 | + /* Data is coming from a SELECT or from a multi-row VALUES clause. |
| 100269 | + ** Generate a co-routine to run the SELECT. */ |
| 99903 | 100270 | int regYield; /* Register holding co-routine entry-point */ |
| 99904 | 100271 | int addrTop; /* Top of the co-routine */ |
| 99905 | 100272 | int rc; /* Result code */ |
| 99906 | 100273 | |
| 99907 | 100274 | regYield = ++pParse->nMem; |
| | @@ -99910,12 +100277,11 @@ |
| 99910 | 100277 | sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield); |
| 99911 | 100278 | dest.iSdst = bIdListInOrder ? regData : 0; |
| 99912 | 100279 | dest.nSdst = pTab->nCol; |
| 99913 | 100280 | rc = sqlite3Select(pParse, pSelect, &dest); |
| 99914 | 100281 | regFromSelect = dest.iSdst; |
| 99915 | | - assert( pParse->nErr==0 || rc ); |
| 99916 | | - if( rc || db->mallocFailed ) goto insert_cleanup; |
| 100282 | + if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup; |
| 99917 | 100283 | sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield); |
| 99918 | 100284 | sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */ |
| 99919 | 100285 | assert( pSelect->pEList ); |
| 99920 | 100286 | nColumn = pSelect->pEList->nExpr; |
| 99921 | 100287 | |
| | @@ -99959,12 +100325,12 @@ |
| 99959 | 100325 | sqlite3VdbeJumpHere(v, addrL); |
| 99960 | 100326 | sqlite3ReleaseTempReg(pParse, regRec); |
| 99961 | 100327 | sqlite3ReleaseTempReg(pParse, regTempRowid); |
| 99962 | 100328 | } |
| 99963 | 100329 | }else{ |
| 99964 | | - /* This is the case if the data for the INSERT is coming from a VALUES |
| 99965 | | - ** clause |
| 100330 | + /* This is the case if the data for the INSERT is coming from a |
| 100331 | + ** single-row VALUES clause |
| 99966 | 100332 | */ |
| 99967 | 100333 | NameContext sNC; |
| 99968 | 100334 | memset(&sNC, 0, sizeof(sNC)); |
| 99969 | 100335 | sNC.pParse = pParse; |
| 99970 | 100336 | srcTab = -1; |
| | @@ -101031,10 +101397,11 @@ |
| 101031 | 101397 | Table *pDest, /* The table we are inserting into */ |
| 101032 | 101398 | Select *pSelect, /* A SELECT statement to use as the data source */ |
| 101033 | 101399 | int onError, /* How to handle constraint errors */ |
| 101034 | 101400 | int iDbDest /* The database of pDest */ |
| 101035 | 101401 | ){ |
| 101402 | + sqlite3 *db = pParse->db; |
| 101036 | 101403 | ExprList *pEList; /* The result set of the SELECT */ |
| 101037 | 101404 | Table *pSrc; /* The table in the FROM clause of SELECT */ |
| 101038 | 101405 | Index *pSrcIdx, *pDestIdx; /* Source and destination indices */ |
| 101039 | 101406 | struct SrcList_item *pItem; /* An element of pSelect->pSrc */ |
| 101040 | 101407 | int i; /* Loop counter */ |
| | @@ -101178,15 +101545,15 @@ |
| 101178 | 101545 | ** But the main beneficiary of the transfer optimization is the VACUUM |
| 101179 | 101546 | ** command, and the VACUUM command disables foreign key constraints. So |
| 101180 | 101547 | ** the extra complication to make this rule less restrictive is probably |
| 101181 | 101548 | ** not worth the effort. Ticket [6284df89debdfa61db8073e062908af0c9b6118e] |
| 101182 | 101549 | */ |
| 101183 | | - if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){ |
| 101550 | + if( (db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){ |
| 101184 | 101551 | return 0; |
| 101185 | 101552 | } |
| 101186 | 101553 | #endif |
| 101187 | | - if( (pParse->db->flags & SQLITE_CountRows)!=0 ){ |
| 101554 | + if( (db->flags & SQLITE_CountRows)!=0 ){ |
| 101188 | 101555 | return 0; /* xfer opt does not play well with PRAGMA count_changes */ |
| 101189 | 101556 | } |
| 101190 | 101557 | |
| 101191 | 101558 | /* If we get this far, it means that the xfer optimization is at |
| 101192 | 101559 | ** least a possibility, though it might only work if the destination |
| | @@ -101193,28 +101560,32 @@ |
| 101193 | 101560 | ** table (tab1) is initially empty. |
| 101194 | 101561 | */ |
| 101195 | 101562 | #ifdef SQLITE_TEST |
| 101196 | 101563 | sqlite3_xferopt_count++; |
| 101197 | 101564 | #endif |
| 101198 | | - iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema); |
| 101565 | + iDbSrc = sqlite3SchemaToIndex(db, pSrc->pSchema); |
| 101199 | 101566 | v = sqlite3GetVdbe(pParse); |
| 101200 | 101567 | sqlite3CodeVerifySchema(pParse, iDbSrc); |
| 101201 | 101568 | iSrc = pParse->nTab++; |
| 101202 | 101569 | iDest = pParse->nTab++; |
| 101203 | 101570 | regAutoinc = autoIncBegin(pParse, iDbDest, pDest); |
| 101204 | 101571 | regData = sqlite3GetTempReg(pParse); |
| 101205 | 101572 | regRowid = sqlite3GetTempReg(pParse); |
| 101206 | 101573 | sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); |
| 101207 | 101574 | assert( HasRowid(pDest) || destHasUniqueIdx ); |
| 101208 | | - if( (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */ |
| 101575 | + if( (db->flags & SQLITE_Vacuum)==0 && ( |
| 101576 | + (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */ |
| 101209 | 101577 | || destHasUniqueIdx /* (2) */ |
| 101210 | 101578 | || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */ |
| 101211 | | - ){ |
| 101579 | + )){ |
| 101212 | 101580 | /* In some circumstances, we are able to run the xfer optimization |
| 101213 | | - ** only if the destination table is initially empty. This code makes |
| 101214 | | - ** that determination. Conditions under which the destination must |
| 101215 | | - ** be empty: |
| 101581 | + ** only if the destination table is initially empty. Unless the |
| 101582 | + ** SQLITE_Vacuum flag is set, this block generates code to make |
| 101583 | + ** that determination. If SQLITE_Vacuum is set, then the destination |
| 101584 | + ** table is always empty. |
| 101585 | + ** |
| 101586 | + ** Conditions under which the destination must be empty: |
| 101216 | 101587 | ** |
| 101217 | 101588 | ** (1) There is no INTEGER PRIMARY KEY but there are indices. |
| 101218 | 101589 | ** (If the destination is not initially empty, the rowid fields |
| 101219 | 101590 | ** of index entries might need to change.) |
| 101220 | 101591 | ** |
| | @@ -101253,10 +101624,11 @@ |
| 101253 | 101624 | }else{ |
| 101254 | 101625 | sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName); |
| 101255 | 101626 | sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName); |
| 101256 | 101627 | } |
| 101257 | 101628 | for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ |
| 101629 | + u8 useSeekResult = 0; |
| 101258 | 101630 | for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){ |
| 101259 | 101631 | if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; |
| 101260 | 101632 | } |
| 101261 | 101633 | assert( pSrcIdx ); |
| 101262 | 101634 | sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc); |
| | @@ -101266,11 +101638,37 @@ |
| 101266 | 101638 | sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); |
| 101267 | 101639 | sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); |
| 101268 | 101640 | VdbeComment((v, "%s", pDestIdx->zName)); |
| 101269 | 101641 | addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v); |
| 101270 | 101642 | sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData); |
| 101643 | + if( db->flags & SQLITE_Vacuum ){ |
| 101644 | + /* This INSERT command is part of a VACUUM operation, which guarantees |
| 101645 | + ** that the destination table is empty. If all indexed columns use |
| 101646 | + ** collation sequence BINARY, then it can also be assumed that the |
| 101647 | + ** index will be populated by inserting keys in strictly sorted |
| 101648 | + ** order. In this case, instead of seeking within the b-tree as part |
| 101649 | + ** of every OP_IdxInsert opcode, an OP_Last is added before the |
| 101650 | + ** OP_IdxInsert to seek to the point within the b-tree where each key |
| 101651 | + ** should be inserted. This is faster. |
| 101652 | + ** |
| 101653 | + ** If any of the indexed columns use a collation sequence other than |
| 101654 | + ** BINARY, this optimization is disabled. This is because the user |
| 101655 | + ** might change the definition of a collation sequence and then run |
| 101656 | + ** a VACUUM command. In that case keys may not be written in strictly |
| 101657 | + ** sorted order. */ |
| 101658 | + for(i=0; i<pSrcIdx->nColumn; i++){ |
| 101659 | + char *zColl = pSrcIdx->azColl[i]; |
| 101660 | + assert( zColl!=0 ); |
| 101661 | + if( sqlite3_stricmp("BINARY", zColl) ) break; |
| 101662 | + } |
| 101663 | + if( i==pSrcIdx->nColumn ){ |
| 101664 | + useSeekResult = OPFLAG_USESEEKRESULT; |
| 101665 | + sqlite3VdbeAddOp3(v, OP_Last, iDest, 0, -1); |
| 101666 | + } |
| 101667 | + } |
| 101271 | 101668 | sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); |
| 101669 | + sqlite3VdbeChangeP5(v, useSeekResult); |
| 101272 | 101670 | sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v); |
| 101273 | 101671 | sqlite3VdbeJumpHere(v, addr1); |
| 101274 | 101672 | sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); |
| 101275 | 101673 | sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); |
| 101276 | 101674 | } |
| | @@ -102385,11 +102783,11 @@ |
| 102385 | 102783 | int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); |
| 102386 | 102784 | char *zErrmsg = 0; |
| 102387 | 102785 | const char *zEntry; |
| 102388 | 102786 | char *zAltEntry = 0; |
| 102389 | 102787 | void **aHandle; |
| 102390 | | - int nMsg = 300 + sqlite3Strlen30(zFile); |
| 102788 | + u64 nMsg = 300 + sqlite3Strlen30(zFile); |
| 102391 | 102789 | int ii; |
| 102392 | 102790 | |
| 102393 | 102791 | /* Shared library endings to try if zFile cannot be loaded as written */ |
| 102394 | 102792 | static const char *azEndings[] = { |
| 102395 | 102793 | #if SQLITE_OS_WIN |
| | @@ -102428,11 +102826,11 @@ |
| 102428 | 102826 | sqlite3_free(zAltFile); |
| 102429 | 102827 | } |
| 102430 | 102828 | #endif |
| 102431 | 102829 | if( handle==0 ){ |
| 102432 | 102830 | if( pzErrMsg ){ |
| 102433 | | - *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg); |
| 102831 | + *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); |
| 102434 | 102832 | if( zErrmsg ){ |
| 102435 | 102833 | sqlite3_snprintf(nMsg, zErrmsg, |
| 102436 | 102834 | "unable to open shared library [%s]", zFile); |
| 102437 | 102835 | sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
| 102438 | 102836 | } |
| | @@ -102454,11 +102852,11 @@ |
| 102454 | 102852 | ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init |
| 102455 | 102853 | */ |
| 102456 | 102854 | if( xInit==0 && zProc==0 ){ |
| 102457 | 102855 | int iFile, iEntry, c; |
| 102458 | 102856 | int ncFile = sqlite3Strlen30(zFile); |
| 102459 | | - zAltEntry = sqlite3_malloc(ncFile+30); |
| 102857 | + zAltEntry = sqlite3_malloc64(ncFile+30); |
| 102460 | 102858 | if( zAltEntry==0 ){ |
| 102461 | 102859 | sqlite3OsDlClose(pVfs, handle); |
| 102462 | 102860 | return SQLITE_NOMEM; |
| 102463 | 102861 | } |
| 102464 | 102862 | memcpy(zAltEntry, "sqlite3_", 8); |
| | @@ -102476,11 +102874,11 @@ |
| 102476 | 102874 | sqlite3OsDlSym(pVfs, handle, zEntry); |
| 102477 | 102875 | } |
| 102478 | 102876 | if( xInit==0 ){ |
| 102479 | 102877 | if( pzErrMsg ){ |
| 102480 | 102878 | nMsg += sqlite3Strlen30(zEntry); |
| 102481 | | - *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg); |
| 102879 | + *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); |
| 102482 | 102880 | if( zErrmsg ){ |
| 102483 | 102881 | sqlite3_snprintf(nMsg, zErrmsg, |
| 102484 | 102882 | "no entry point [%s] in shared library [%s]", zEntry, zFile); |
| 102485 | 102883 | sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
| 102486 | 102884 | } |
| | @@ -102575,11 +102973,11 @@ |
| 102575 | 102973 | ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER |
| 102576 | 102974 | ** mutex must be held while accessing this list. |
| 102577 | 102975 | */ |
| 102578 | 102976 | typedef struct sqlite3AutoExtList sqlite3AutoExtList; |
| 102579 | 102977 | static SQLITE_WSD struct sqlite3AutoExtList { |
| 102580 | | - int nExt; /* Number of entries in aExt[] */ |
| 102978 | + u32 nExt; /* Number of entries in aExt[] */ |
| 102581 | 102979 | void (**aExt)(void); /* Pointers to the extension init functions */ |
| 102582 | 102980 | } sqlite3Autoext = { 0, 0 }; |
| 102583 | 102981 | |
| 102584 | 102982 | /* The "wsdAutoext" macro will resolve to the autoextension |
| 102585 | 102983 | ** state vector. If writable static data is unsupported on the target, |
| | @@ -102608,23 +103006,23 @@ |
| 102608 | 103006 | if( rc ){ |
| 102609 | 103007 | return rc; |
| 102610 | 103008 | }else |
| 102611 | 103009 | #endif |
| 102612 | 103010 | { |
| 102613 | | - int i; |
| 103011 | + u32 i; |
| 102614 | 103012 | #if SQLITE_THREADSAFE |
| 102615 | 103013 | sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 102616 | 103014 | #endif |
| 102617 | 103015 | wsdAutoextInit; |
| 102618 | 103016 | sqlite3_mutex_enter(mutex); |
| 102619 | 103017 | for(i=0; i<wsdAutoext.nExt; i++){ |
| 102620 | 103018 | if( wsdAutoext.aExt[i]==xInit ) break; |
| 102621 | 103019 | } |
| 102622 | 103020 | if( i==wsdAutoext.nExt ){ |
| 102623 | | - int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]); |
| 103021 | + u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]); |
| 102624 | 103022 | void (**aNew)(void); |
| 102625 | | - aNew = sqlite3_realloc(wsdAutoext.aExt, nByte); |
| 103023 | + aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte); |
| 102626 | 103024 | if( aNew==0 ){ |
| 102627 | 103025 | rc = SQLITE_NOMEM; |
| 102628 | 103026 | }else{ |
| 102629 | 103027 | wsdAutoext.aExt = aNew; |
| 102630 | 103028 | wsdAutoext.aExt[wsdAutoext.nExt] = xInit; |
| | @@ -102652,11 +103050,11 @@ |
| 102652 | 103050 | #endif |
| 102653 | 103051 | int i; |
| 102654 | 103052 | int n = 0; |
| 102655 | 103053 | wsdAutoextInit; |
| 102656 | 103054 | sqlite3_mutex_enter(mutex); |
| 102657 | | - for(i=wsdAutoext.nExt-1; i>=0; i--){ |
| 103055 | + for(i=(int)wsdAutoext.nExt-1; i>=0; i--){ |
| 102658 | 103056 | if( wsdAutoext.aExt[i]==xInit ){ |
| 102659 | 103057 | wsdAutoext.nExt--; |
| 102660 | 103058 | wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt]; |
| 102661 | 103059 | n++; |
| 102662 | 103060 | break; |
| | @@ -102690,11 +103088,11 @@ |
| 102690 | 103088 | ** Load all automatic extensions. |
| 102691 | 103089 | ** |
| 102692 | 103090 | ** If anything goes wrong, set an error in the database connection. |
| 102693 | 103091 | */ |
| 102694 | 103092 | SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){ |
| 102695 | | - int i; |
| 103093 | + u32 i; |
| 102696 | 103094 | int go = 1; |
| 102697 | 103095 | int rc; |
| 102698 | 103096 | int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); |
| 102699 | 103097 | |
| 102700 | 103098 | wsdAutoextInit; |
| | @@ -103354,19 +103752,19 @@ |
| 103354 | 103752 | /* |
| 103355 | 103753 | ** Generate code to return a single integer value. |
| 103356 | 103754 | */ |
| 103357 | 103755 | static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){ |
| 103358 | 103756 | Vdbe *v = sqlite3GetVdbe(pParse); |
| 103359 | | - int mem = ++pParse->nMem; |
| 103757 | + int nMem = ++pParse->nMem; |
| 103360 | 103758 | i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value)); |
| 103361 | 103759 | if( pI64 ){ |
| 103362 | 103760 | memcpy(pI64, &value, sizeof(value)); |
| 103363 | 103761 | } |
| 103364 | | - sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64); |
| 103762 | + sqlite3VdbeAddOp4(v, OP_Int64, 0, nMem, 0, (char*)pI64, P4_INT64); |
| 103365 | 103763 | sqlite3VdbeSetNumCols(v, 1); |
| 103366 | 103764 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC); |
| 103367 | | - sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1); |
| 103765 | + sqlite3VdbeAddOp2(v, OP_ResultRow, nMem, 1); |
| 103368 | 103766 | } |
| 103369 | 103767 | |
| 103370 | 103768 | |
| 103371 | 103769 | /* |
| 103372 | 103770 | ** Set the safety_level and pager flags for pager iDb. Or if iDb<0 |
| | @@ -103527,15 +103925,15 @@ |
| 103527 | 103925 | aFcntl[3] = 0; |
| 103528 | 103926 | db->busyHandler.nBusy = 0; |
| 103529 | 103927 | rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl); |
| 103530 | 103928 | if( rc==SQLITE_OK ){ |
| 103531 | 103929 | if( aFcntl[0] ){ |
| 103532 | | - int mem = ++pParse->nMem; |
| 103533 | | - sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0); |
| 103930 | + int nMem = ++pParse->nMem; |
| 103931 | + sqlite3VdbeAddOp4(v, OP_String8, 0, nMem, 0, aFcntl[0], 0); |
| 103534 | 103932 | sqlite3VdbeSetNumCols(v, 1); |
| 103535 | 103933 | sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC); |
| 103536 | | - sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1); |
| 103934 | + sqlite3VdbeAddOp2(v, OP_ResultRow, nMem, 1); |
| 103537 | 103935 | sqlite3_free(aFcntl[0]); |
| 103538 | 103936 | } |
| 103539 | 103937 | goto pragma_out; |
| 103540 | 103938 | } |
| 103541 | 103939 | if( rc!=SQLITE_NOTFOUND ){ |
| | @@ -104136,11 +104534,13 @@ |
| 104136 | 104534 | }else{ |
| 104137 | 104535 | if( !db->autoCommit ){ |
| 104138 | 104536 | sqlite3ErrorMsg(pParse, |
| 104139 | 104537 | "Safety level may not be changed inside a transaction"); |
| 104140 | 104538 | }else{ |
| 104141 | | - pDb->safety_level = getSafetyLevel(zRight,0,1)+1; |
| 104539 | + int iLevel = (getSafetyLevel(zRight,0,1)+1) & PAGER_SYNCHRONOUS_MASK; |
| 104540 | + if( iLevel==0 ) iLevel = 1; |
| 104541 | + pDb->safety_level = iLevel; |
| 104142 | 104542 | setAllPagerFlags(db); |
| 104143 | 104543 | } |
| 104144 | 104544 | } |
| 104145 | 104545 | break; |
| 104146 | 104546 | } |
| | @@ -104231,11 +104631,11 @@ |
| 104231 | 104631 | if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){ |
| 104232 | 104632 | k = 0; |
| 104233 | 104633 | }else if( pPk==0 ){ |
| 104234 | 104634 | k = 1; |
| 104235 | 104635 | }else{ |
| 104236 | | - for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){} |
| 104636 | + for(k=1; k<=pTab->nCol && pPk->aiColumn[k-1]!=i; k++){} |
| 104237 | 104637 | } |
| 104238 | 104638 | sqlite3VdbeAddOp2(v, OP_Integer, k, 6); |
| 104239 | 104639 | sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6); |
| 104240 | 104640 | } |
| 104241 | 104641 | } |
| | @@ -105237,11 +105637,11 @@ |
| 105237 | 105637 | |
| 105238 | 105638 | assert( iDb>=0 && iDb<db->nDb ); |
| 105239 | 105639 | if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
| 105240 | 105640 | if( argv[1]==0 ){ |
| 105241 | 105641 | corruptSchema(pData, argv[0], 0); |
| 105242 | | - }else if( argv[2] && argv[2][0] ){ |
| 105642 | + }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){ |
| 105243 | 105643 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
| 105244 | 105644 | ** But because db->init.busy is set to 1, no VDBE code is generated |
| 105245 | 105645 | ** or executed. All the parser does is build the internal data |
| 105246 | 105646 | ** structures that describe the table, index, or view. |
| 105247 | 105647 | */ |
| | @@ -105268,12 +105668,12 @@ |
| 105268 | 105668 | corruptSchema(pData, argv[0], sqlite3_errmsg(db)); |
| 105269 | 105669 | } |
| 105270 | 105670 | } |
| 105271 | 105671 | } |
| 105272 | 105672 | sqlite3_finalize(pStmt); |
| 105273 | | - }else if( argv[0]==0 ){ |
| 105274 | | - corruptSchema(pData, 0, 0); |
| 105673 | + }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){ |
| 105674 | + corruptSchema(pData, argv[0], 0); |
| 105275 | 105675 | }else{ |
| 105276 | 105676 | /* If the SQL column is blank it means this is an index that |
| 105277 | 105677 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
| 105278 | 105678 | ** constraint for a CREATE TABLE. The index should have already |
| 105279 | 105679 | ** been created when we processed the CREATE TABLE. All we have |
| | @@ -106176,11 +106576,10 @@ |
| 106176 | 106576 | ){ |
| 106177 | 106577 | Select *pNew; |
| 106178 | 106578 | Select standin; |
| 106179 | 106579 | sqlite3 *db = pParse->db; |
| 106180 | 106580 | pNew = sqlite3DbMallocZero(db, sizeof(*pNew) ); |
| 106181 | | - assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */ |
| 106182 | 106581 | if( pNew==0 ){ |
| 106183 | 106582 | assert( db->mallocFailed ); |
| 106184 | 106583 | pNew = &standin; |
| 106185 | 106584 | memset(pNew, 0, sizeof(*pNew)); |
| 106186 | 106585 | } |
| | @@ -106196,11 +106595,11 @@ |
| 106196 | 106595 | pNew->pOrderBy = pOrderBy; |
| 106197 | 106596 | pNew->selFlags = selFlags; |
| 106198 | 106597 | pNew->op = TK_SELECT; |
| 106199 | 106598 | pNew->pLimit = pLimit; |
| 106200 | 106599 | pNew->pOffset = pOffset; |
| 106201 | | - assert( pOffset==0 || pLimit!=0 ); |
| 106600 | + assert( pOffset==0 || pLimit!=0 || pParse->nErr>0 || db->mallocFailed!=0 ); |
| 106202 | 106601 | pNew->addrOpenEphm[0] = -1; |
| 106203 | 106602 | pNew->addrOpenEphm[1] = -1; |
| 106204 | 106603 | if( db->mallocFailed ) { |
| 106205 | 106604 | clearSelect(db, pNew, pNew!=&standin); |
| 106206 | 106605 | pNew = 0; |
| | @@ -107446,11 +107845,11 @@ |
| 107446 | 107845 | if( pS ){ |
| 107447 | 107846 | /* The "table" is actually a sub-select or a view in the FROM clause |
| 107448 | 107847 | ** of the SELECT statement. Return the declaration type and origin |
| 107449 | 107848 | ** data for the result-set column of the sub-select. |
| 107450 | 107849 | */ |
| 107451 | | - if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){ |
| 107850 | + if( iCol>=0 && iCol<pS->pEList->nExpr ){ |
| 107452 | 107851 | /* If iCol is less than zero, then the expression requests the |
| 107453 | 107852 | ** rowid of the sub-select or view. This expression is legal (see |
| 107454 | 107853 | ** test case misc2.2.2) - it always evaluates to NULL. |
| 107455 | 107854 | */ |
| 107456 | 107855 | NameContext sNC; |
| | @@ -107766,16 +108165,18 @@ |
| 107766 | 108165 | memset(&sNC, 0, sizeof(sNC)); |
| 107767 | 108166 | sNC.pSrcList = pSelect->pSrc; |
| 107768 | 108167 | a = pSelect->pEList->a; |
| 107769 | 108168 | for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){ |
| 107770 | 108169 | p = a[i].pExpr; |
| 107771 | | - pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst)); |
| 108170 | + if( pCol->zType==0 ){ |
| 108171 | + pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst)); |
| 108172 | + } |
| 107772 | 108173 | szAll += pCol->szEst; |
| 107773 | 108174 | pCol->affinity = sqlite3ExprAffinity(p); |
| 107774 | 108175 | if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE; |
| 107775 | 108176 | pColl = sqlite3ExprCollSeq(pParse, p); |
| 107776 | | - if( pColl ){ |
| 108177 | + if( pColl && pCol->zColl==0 ){ |
| 107777 | 108178 | pCol->zColl = sqlite3DbStrDup(db, pColl->zName); |
| 107778 | 108179 | } |
| 107779 | 108180 | } |
| 107780 | 108181 | pTab->szTabRow = sqlite3LogEst(szAll*4); |
| 107781 | 108182 | } |
| | @@ -108173,12 +108574,11 @@ |
| 108173 | 108574 | ){ |
| 108174 | 108575 | Select *pPrior; |
| 108175 | 108576 | int nExpr = p->pEList->nExpr; |
| 108176 | 108577 | int nRow = 1; |
| 108177 | 108578 | int rc = 0; |
| 108178 | | - assert( p->pNext==0 ); |
| 108179 | | - assert( p->selFlags & SF_AllValues ); |
| 108579 | + assert( p->selFlags & SF_MultiValue ); |
| 108180 | 108580 | do{ |
| 108181 | 108581 | assert( p->selFlags & SF_Values ); |
| 108182 | 108582 | assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) ); |
| 108183 | 108583 | assert( p->pLimit==0 ); |
| 108184 | 108584 | assert( p->pOffset==0 ); |
| | @@ -108283,11 +108683,11 @@ |
| 108283 | 108683 | dest.eDest = SRT_Table; |
| 108284 | 108684 | } |
| 108285 | 108685 | |
| 108286 | 108686 | /* Special handling for a compound-select that originates as a VALUES clause. |
| 108287 | 108687 | */ |
| 108288 | | - if( p->selFlags & SF_AllValues ){ |
| 108688 | + if( p->selFlags & SF_MultiValue ){ |
| 108289 | 108689 | rc = multiSelectValues(pParse, p, &dest); |
| 108290 | 108690 | goto multi_select_end; |
| 108291 | 108691 | } |
| 108292 | 108692 | |
| 108293 | 108693 | /* Make sure all SELECTs in the statement have the same number of elements |
| | @@ -108668,11 +109068,11 @@ |
| 108668 | 109068 | ** then there should be a single item on the stack. Write this |
| 108669 | 109069 | ** item into the set table with bogus data. |
| 108670 | 109070 | */ |
| 108671 | 109071 | case SRT_Set: { |
| 108672 | 109072 | int r1; |
| 108673 | | - assert( pIn->nSdst==1 ); |
| 109073 | + assert( pIn->nSdst==1 || pParse->nErr>0 ); |
| 108674 | 109074 | pDest->affSdst = |
| 108675 | 109075 | sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst); |
| 108676 | 109076 | r1 = sqlite3GetTempReg(pParse); |
| 108677 | 109077 | sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1); |
| 108678 | 109078 | sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1); |
| | @@ -108694,11 +109094,11 @@ |
| 108694 | 109094 | /* If this is a scalar select that is part of an expression, then |
| 108695 | 109095 | ** store the results in the appropriate memory cell and break out |
| 108696 | 109096 | ** of the scan loop. |
| 108697 | 109097 | */ |
| 108698 | 109098 | case SRT_Mem: { |
| 108699 | | - assert( pIn->nSdst==1 ); |
| 109099 | + assert( pIn->nSdst==1 || pParse->nErr>0 ); testcase( pIn->nSdst!=1 ); |
| 108700 | 109100 | sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1); |
| 108701 | 109101 | /* The LIMIT clause will jump out of the loop for us */ |
| 108702 | 109102 | break; |
| 108703 | 109103 | } |
| 108704 | 109104 | #endif /* #ifndef SQLITE_OMIT_SUBQUERY */ |
| | @@ -108709,11 +109109,11 @@ |
| 108709 | 109109 | case SRT_Coroutine: { |
| 108710 | 109110 | if( pDest->iSdst==0 ){ |
| 108711 | 109111 | pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst); |
| 108712 | 109112 | pDest->nSdst = pIn->nSdst; |
| 108713 | 109113 | } |
| 108714 | | - sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pDest->nSdst); |
| 109114 | + sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pIn->nSdst); |
| 108715 | 109115 | sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm); |
| 108716 | 109116 | break; |
| 108717 | 109117 | } |
| 108718 | 109118 | |
| 108719 | 109119 | /* If none of the above, then the result destination must be |
| | @@ -108925,12 +109325,14 @@ |
| 108925 | 109325 | */ |
| 108926 | 109326 | aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy); |
| 108927 | 109327 | if( aPermute ){ |
| 108928 | 109328 | struct ExprList_item *pItem; |
| 108929 | 109329 | for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){ |
| 108930 | | - assert( pItem->u.x.iOrderByCol>0 |
| 108931 | | - && pItem->u.x.iOrderByCol<=p->pEList->nExpr ); |
| 109330 | + assert( pItem->u.x.iOrderByCol>0 ); |
| 109331 | + /* assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr ) is also true |
| 109332 | + ** but only for well-formed SELECT statements. */ |
| 109333 | + testcase( pItem->u.x.iOrderByCol > p->pEList->nExpr ); |
| 108932 | 109334 | aPermute[i] = pItem->u.x.iOrderByCol - 1; |
| 108933 | 109335 | } |
| 108934 | 109336 | pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1); |
| 108935 | 109337 | }else{ |
| 108936 | 109338 | pKeyMerge = 0; |
| | @@ -109136,11 +109538,11 @@ |
| 109136 | 109538 | pPrior->pNext = p; |
| 109137 | 109539 | |
| 109138 | 109540 | /*** TBD: Insert subroutine calls to close cursors on incomplete |
| 109139 | 109541 | **** subqueries ****/ |
| 109140 | 109542 | explainComposite(pParse, p->op, iSub1, iSub2, 0); |
| 109141 | | - return SQLITE_OK; |
| 109543 | + return pParse->nErr!=0; |
| 109142 | 109544 | } |
| 109143 | 109545 | #endif |
| 109144 | 109546 | |
| 109145 | 109547 | #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) |
| 109146 | 109548 | /* Forward Declarations */ |
| | @@ -109948,10 +110350,11 @@ |
| 109948 | 110350 | pNew->pGroupBy = 0; |
| 109949 | 110351 | pNew->pHaving = 0; |
| 109950 | 110352 | pNew->pOrderBy = 0; |
| 109951 | 110353 | p->pPrior = 0; |
| 109952 | 110354 | p->pNext = 0; |
| 110355 | + p->pWith = 0; |
| 109953 | 110356 | p->selFlags &= ~SF_Compound; |
| 109954 | 110357 | assert( (p->selFlags & SF_Converted)==0 ); |
| 109955 | 110358 | p->selFlags |= SF_Converted; |
| 109956 | 110359 | assert( pNew->pPrior!=0 ); |
| 109957 | 110360 | pNew->pPrior->pNext = pNew; |
| | @@ -110486,11 +110889,11 @@ |
| 110486 | 110889 | if( pParse->hasCompound ){ |
| 110487 | 110890 | w.xSelectCallback = convertCompoundSelectToSubquery; |
| 110488 | 110891 | sqlite3WalkSelect(&w, pSelect); |
| 110489 | 110892 | } |
| 110490 | 110893 | w.xSelectCallback = selectExpander; |
| 110491 | | - if( (pSelect->selFlags & SF_AllValues)==0 ){ |
| 110894 | + if( (pSelect->selFlags & SF_MultiValue)==0 ){ |
| 110492 | 110895 | w.xSelectCallback2 = selectPopWith; |
| 110493 | 110896 | } |
| 110494 | 110897 | sqlite3WalkSelect(&w, pSelect); |
| 110495 | 110898 | } |
| 110496 | 110899 | |
| | @@ -110672,11 +111075,12 @@ |
| 110672 | 111075 | nArg = 0; |
| 110673 | 111076 | regAgg = 0; |
| 110674 | 111077 | } |
| 110675 | 111078 | if( pF->iDistinct>=0 ){ |
| 110676 | 111079 | addrNext = sqlite3VdbeMakeLabel(v); |
| 110677 | | - assert( nArg==1 ); |
| 111080 | + testcase( nArg==0 ); /* Error condition */ |
| 111081 | + testcase( nArg>1 ); /* Also an error */ |
| 110678 | 111082 | codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg); |
| 110679 | 111083 | } |
| 110680 | 111084 | if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){ |
| 110681 | 111085 | CollSeq *pColl = 0; |
| 110682 | 111086 | struct ExprList_item *pItem; |
| | @@ -111547,14 +111951,13 @@ |
| 111547 | 111951 | |
| 111548 | 111952 | /* Jump here to skip this query |
| 111549 | 111953 | */ |
| 111550 | 111954 | sqlite3VdbeResolveLabel(v, iEnd); |
| 111551 | 111955 | |
| 111552 | | - /* The SELECT was successfully coded. Set the return code to 0 |
| 111553 | | - ** to indicate no errors. |
| 111554 | | - */ |
| 111555 | | - rc = 0; |
| 111956 | + /* The SELECT has been coded. If there is an error in the Parse structure, |
| 111957 | + ** set the return code to 1. Otherwise 0. */ |
| 111958 | + rc = (pParse->nErr>0); |
| 111556 | 111959 | |
| 111557 | 111960 | /* Control jumps to here if an error is encountered above, or upon |
| 111558 | 111961 | ** successful coding of the SELECT. |
| 111559 | 111962 | */ |
| 111560 | 111963 | select_end: |
| | @@ -111601,11 +112004,11 @@ |
| 111601 | 112004 | sqlite3TreeViewLine(pView, "FROM"); |
| 111602 | 112005 | for(i=0; i<p->pSrc->nSrc; i++){ |
| 111603 | 112006 | struct SrcList_item *pItem = &p->pSrc->a[i]; |
| 111604 | 112007 | StrAccum x; |
| 111605 | 112008 | char zLine[100]; |
| 111606 | | - sqlite3StrAccumInit(&x, zLine, sizeof(zLine), 0); |
| 112009 | + sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); |
| 111607 | 112010 | sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor); |
| 111608 | 112011 | if( pItem->zDatabase ){ |
| 111609 | 112012 | sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName); |
| 111610 | 112013 | }else if( pItem->zName ){ |
| 111611 | 112014 | sqlite3XPrintf(&x, 0, " %s", pItem->zName); |
| | @@ -111760,11 +112163,11 @@ |
| 111760 | 112163 | for(i=0; i<nCol; i++){ |
| 111761 | 112164 | if( argv[i]==0 ){ |
| 111762 | 112165 | z = 0; |
| 111763 | 112166 | }else{ |
| 111764 | 112167 | int n = sqlite3Strlen30(argv[i])+1; |
| 111765 | | - z = sqlite3_malloc( n ); |
| 112168 | + z = sqlite3_malloc64( n ); |
| 111766 | 112169 | if( z==0 ) goto malloc_failed; |
| 111767 | 112170 | memcpy(z, argv[i], n); |
| 111768 | 112171 | } |
| 111769 | 112172 | p->azResult[p->nData++] = z; |
| 111770 | 112173 | } |
| | @@ -111809,11 +112212,11 @@ |
| 111809 | 112212 | res.nRow = 0; |
| 111810 | 112213 | res.nColumn = 0; |
| 111811 | 112214 | res.nData = 1; |
| 111812 | 112215 | res.nAlloc = 20; |
| 111813 | 112216 | res.rc = SQLITE_OK; |
| 111814 | | - res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc ); |
| 112217 | + res.azResult = sqlite3_malloc64(sizeof(char*)*res.nAlloc ); |
| 111815 | 112218 | if( res.azResult==0 ){ |
| 111816 | 112219 | db->errCode = SQLITE_NOMEM; |
| 111817 | 112220 | return SQLITE_NOMEM; |
| 111818 | 112221 | } |
| 111819 | 112222 | res.azResult[0] = 0; |
| | @@ -111837,11 +112240,11 @@ |
| 111837 | 112240 | sqlite3_free_table(&res.azResult[1]); |
| 111838 | 112241 | return rc; |
| 111839 | 112242 | } |
| 111840 | 112243 | if( res.nAlloc>res.nData ){ |
| 111841 | 112244 | char **azNew; |
| 111842 | | - azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData ); |
| 112245 | + azNew = sqlite3_realloc64( res.azResult, sizeof(char*)*res.nData ); |
| 111843 | 112246 | if( azNew==0 ){ |
| 111844 | 112247 | sqlite3_free_table(&res.azResult[1]); |
| 111845 | 112248 | db->errCode = SQLITE_NOMEM; |
| 111846 | 112249 | return SQLITE_NOMEM; |
| 111847 | 112250 | } |
| | @@ -112065,11 +112468,10 @@ |
| 112065 | 112468 | } |
| 112066 | 112469 | |
| 112067 | 112470 | /* Do not create a trigger on a system table */ |
| 112068 | 112471 | if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ |
| 112069 | 112472 | sqlite3ErrorMsg(pParse, "cannot create trigger on system table"); |
| 112070 | | - pParse->nErr++; |
| 112071 | 112473 | goto trigger_cleanup; |
| 112072 | 112474 | } |
| 112073 | 112475 | |
| 112074 | 112476 | /* INSTEAD of triggers are only for views and views only support INSTEAD |
| 112075 | 112477 | ** of triggers. |
| | @@ -112245,16 +112647,16 @@ |
| 112245 | 112647 | u8 op, /* Trigger opcode */ |
| 112246 | 112648 | Token *pName /* The target name */ |
| 112247 | 112649 | ){ |
| 112248 | 112650 | TriggerStep *pTriggerStep; |
| 112249 | 112651 | |
| 112250 | | - pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n); |
| 112652 | + pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n + 1); |
| 112251 | 112653 | if( pTriggerStep ){ |
| 112252 | 112654 | char *z = (char*)&pTriggerStep[1]; |
| 112253 | 112655 | memcpy(z, pName->z, pName->n); |
| 112254 | | - pTriggerStep->target.z = z; |
| 112255 | | - pTriggerStep->target.n = pName->n; |
| 112656 | + sqlite3Dequote(z); |
| 112657 | + pTriggerStep->zTarget = z; |
| 112256 | 112658 | pTriggerStep->op = op; |
| 112257 | 112659 | } |
| 112258 | 112660 | return pTriggerStep; |
| 112259 | 112661 | } |
| 112260 | 112662 | |
| | @@ -112533,11 +112935,11 @@ |
| 112533 | 112935 | } |
| 112534 | 112936 | return (mask ? pList : 0); |
| 112535 | 112937 | } |
| 112536 | 112938 | |
| 112537 | 112939 | /* |
| 112538 | | -** Convert the pStep->target token into a SrcList and return a pointer |
| 112940 | +** Convert the pStep->zTarget string into a SrcList and return a pointer |
| 112539 | 112941 | ** to that SrcList. |
| 112540 | 112942 | ** |
| 112541 | 112943 | ** This routine adds a specific database name, if needed, to the target when |
| 112542 | 112944 | ** forming the SrcList. This prevents a trigger in one database from |
| 112543 | 112945 | ** referring to a target in another database. An exception is when the |
| | @@ -112546,21 +112948,21 @@ |
| 112546 | 112948 | */ |
| 112547 | 112949 | static SrcList *targetSrcList( |
| 112548 | 112950 | Parse *pParse, /* The parsing context */ |
| 112549 | 112951 | TriggerStep *pStep /* The trigger containing the target token */ |
| 112550 | 112952 | ){ |
| 112953 | + sqlite3 *db = pParse->db; |
| 112551 | 112954 | int iDb; /* Index of the database to use */ |
| 112552 | 112955 | SrcList *pSrc; /* SrcList to be returned */ |
| 112553 | 112956 | |
| 112554 | | - pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0); |
| 112957 | + pSrc = sqlite3SrcListAppend(db, 0, 0, 0); |
| 112555 | 112958 | if( pSrc ){ |
| 112556 | 112959 | assert( pSrc->nSrc>0 ); |
| 112557 | | - assert( pSrc->a!=0 ); |
| 112558 | | - iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema); |
| 112960 | + pSrc->a[pSrc->nSrc-1].zName = sqlite3DbStrDup(db, pStep->zTarget); |
| 112961 | + iDb = sqlite3SchemaToIndex(db, pStep->pTrig->pSchema); |
| 112559 | 112962 | if( iDb==0 || iDb>=2 ){ |
| 112560 | | - sqlite3 *db = pParse->db; |
| 112561 | | - assert( iDb<pParse->db->nDb ); |
| 112963 | + assert( iDb<db->nDb ); |
| 112562 | 112964 | pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName); |
| 112563 | 112965 | } |
| 112564 | 112966 | } |
| 112565 | 112967 | return pSrc; |
| 112566 | 112968 | } |
| | @@ -112668,10 +113070,11 @@ |
| 112668 | 113070 | assert( pFrom->zErrMsg==0 || pFrom->nErr ); |
| 112669 | 113071 | assert( pTo->zErrMsg==0 || pTo->nErr ); |
| 112670 | 113072 | if( pTo->nErr==0 ){ |
| 112671 | 113073 | pTo->zErrMsg = pFrom->zErrMsg; |
| 112672 | 113074 | pTo->nErr = pFrom->nErr; |
| 113075 | + pTo->rc = pFrom->rc; |
| 112673 | 113076 | }else{ |
| 112674 | 113077 | sqlite3DbFree(pFrom->db, pFrom->zErrMsg); |
| 112675 | 113078 | } |
| 112676 | 113079 | } |
| 112677 | 113080 | |
| | @@ -114018,17 +114421,21 @@ |
| 114018 | 114421 | |
| 114019 | 114422 | /* Loop through the tables in the main database. For each, do |
| 114020 | 114423 | ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy |
| 114021 | 114424 | ** the contents to the temporary database. |
| 114022 | 114425 | */ |
| 114426 | + assert( (db->flags & SQLITE_Vacuum)==0 ); |
| 114427 | + db->flags |= SQLITE_Vacuum; |
| 114023 | 114428 | rc = execExecSql(db, pzErrMsg, |
| 114024 | 114429 | "SELECT 'INSERT INTO vacuum_db.' || quote(name) " |
| 114025 | 114430 | "|| ' SELECT * FROM main.' || quote(name) || ';'" |
| 114026 | 114431 | "FROM main.sqlite_master " |
| 114027 | 114432 | "WHERE type = 'table' AND name!='sqlite_sequence' " |
| 114028 | 114433 | " AND coalesce(rootpage,1)>0" |
| 114029 | 114434 | ); |
| 114435 | + assert( (db->flags & SQLITE_Vacuum)!=0 ); |
| 114436 | + db->flags &= ~SQLITE_Vacuum; |
| 114030 | 114437 | if( rc!=SQLITE_OK ) goto end_of_vacuum; |
| 114031 | 114438 | |
| 114032 | 114439 | /* Copy over the sequence table |
| 114033 | 114440 | */ |
| 114034 | 114441 | rc = execExecSql(db, pzErrMsg, |
| | @@ -114163,10 +114570,12 @@ |
| 114163 | 114570 | ** are invoked only from within xCreate and xConnect methods. |
| 114164 | 114571 | */ |
| 114165 | 114572 | struct VtabCtx { |
| 114166 | 114573 | VTable *pVTable; /* The virtual table being constructed */ |
| 114167 | 114574 | Table *pTab; /* The Table object to which the virtual table belongs */ |
| 114575 | + VtabCtx *pPrior; /* Parent context (if any) */ |
| 114576 | + int bDeclared; /* True after sqlite3_declare_vtab() is called */ |
| 114168 | 114577 | }; |
| 114169 | 114578 | |
| 114170 | 114579 | /* |
| 114171 | 114580 | ** The actual function that does the work of creating a new module. |
| 114172 | 114581 | ** This function implements the sqlite3_create_module() and |
| | @@ -114609,11 +115018,11 @@ |
| 114609 | 115018 | Token *pArg = &pParse->sArg; |
| 114610 | 115019 | if( pArg->z==0 ){ |
| 114611 | 115020 | pArg->z = p->z; |
| 114612 | 115021 | pArg->n = p->n; |
| 114613 | 115022 | }else{ |
| 114614 | | - assert(pArg->z < p->z); |
| 115023 | + assert(pArg->z <= p->z); |
| 114615 | 115024 | pArg->n = (int)(&p->z[p->n] - pArg->z); |
| 114616 | 115025 | } |
| 114617 | 115026 | } |
| 114618 | 115027 | |
| 114619 | 115028 | /* |
| | @@ -114626,19 +115035,31 @@ |
| 114626 | 115035 | Table *pTab, |
| 114627 | 115036 | Module *pMod, |
| 114628 | 115037 | int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), |
| 114629 | 115038 | char **pzErr |
| 114630 | 115039 | ){ |
| 114631 | | - VtabCtx sCtx, *pPriorCtx; |
| 115040 | + VtabCtx sCtx; |
| 114632 | 115041 | VTable *pVTable; |
| 114633 | 115042 | int rc; |
| 114634 | 115043 | const char *const*azArg = (const char *const*)pTab->azModuleArg; |
| 114635 | 115044 | int nArg = pTab->nModuleArg; |
| 114636 | 115045 | char *zErr = 0; |
| 114637 | | - char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); |
| 115046 | + char *zModuleName; |
| 114638 | 115047 | int iDb; |
| 115048 | + VtabCtx *pCtx; |
| 114639 | 115049 | |
| 115050 | + /* Check that the virtual-table is not already being initialized */ |
| 115051 | + for(pCtx=db->pVtabCtx; pCtx; pCtx=pCtx->pPrior){ |
| 115052 | + if( pCtx->pTab==pTab ){ |
| 115053 | + *pzErr = sqlite3MPrintf(db, |
| 115054 | + "vtable constructor called recursively: %s", pTab->zName |
| 115055 | + ); |
| 115056 | + return SQLITE_LOCKED; |
| 115057 | + } |
| 115058 | + } |
| 115059 | + |
| 115060 | + zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); |
| 114640 | 115061 | if( !zModuleName ){ |
| 114641 | 115062 | return SQLITE_NOMEM; |
| 114642 | 115063 | } |
| 114643 | 115064 | |
| 114644 | 115065 | pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); |
| | @@ -114655,15 +115076,17 @@ |
| 114655 | 115076 | /* Invoke the virtual table constructor */ |
| 114656 | 115077 | assert( &db->pVtabCtx ); |
| 114657 | 115078 | assert( xConstruct ); |
| 114658 | 115079 | sCtx.pTab = pTab; |
| 114659 | 115080 | sCtx.pVTable = pVTable; |
| 114660 | | - pPriorCtx = db->pVtabCtx; |
| 115081 | + sCtx.pPrior = db->pVtabCtx; |
| 115082 | + sCtx.bDeclared = 0; |
| 114661 | 115083 | db->pVtabCtx = &sCtx; |
| 114662 | 115084 | rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); |
| 114663 | | - db->pVtabCtx = pPriorCtx; |
| 115085 | + db->pVtabCtx = sCtx.pPrior; |
| 114664 | 115086 | if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; |
| 115087 | + assert( sCtx.pTab==pTab ); |
| 114665 | 115088 | |
| 114666 | 115089 | if( SQLITE_OK!=rc ){ |
| 114667 | 115090 | if( zErr==0 ){ |
| 114668 | 115091 | *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); |
| 114669 | 115092 | }else { |
| | @@ -114675,17 +115098,18 @@ |
| 114675 | 115098 | /* Justification of ALWAYS(): A correct vtab constructor must allocate |
| 114676 | 115099 | ** the sqlite3_vtab object if successful. */ |
| 114677 | 115100 | memset(pVTable->pVtab, 0, sizeof(pVTable->pVtab[0])); |
| 114678 | 115101 | pVTable->pVtab->pModule = pMod->pModule; |
| 114679 | 115102 | pVTable->nRef = 1; |
| 114680 | | - if( sCtx.pTab ){ |
| 115103 | + if( sCtx.bDeclared==0 ){ |
| 114681 | 115104 | const char *zFormat = "vtable constructor did not declare schema: %s"; |
| 114682 | 115105 | *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); |
| 114683 | 115106 | sqlite3VtabUnlock(pVTable); |
| 114684 | 115107 | rc = SQLITE_ERROR; |
| 114685 | 115108 | }else{ |
| 114686 | 115109 | int iCol; |
| 115110 | + u8 oooHidden = 0; |
| 114687 | 115111 | /* If everything went according to plan, link the new VTable structure |
| 114688 | 115112 | ** into the linked list headed by pTab->pVTable. Then loop through the |
| 114689 | 115113 | ** columns of the table to see if any of them contain the token "hidden". |
| 114690 | 115114 | ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from |
| 114691 | 115115 | ** the type string. */ |
| | @@ -114694,11 +115118,14 @@ |
| 114694 | 115118 | |
| 114695 | 115119 | for(iCol=0; iCol<pTab->nCol; iCol++){ |
| 114696 | 115120 | char *zType = pTab->aCol[iCol].zType; |
| 114697 | 115121 | int nType; |
| 114698 | 115122 | int i = 0; |
| 114699 | | - if( !zType ) continue; |
| 115123 | + if( !zType ){ |
| 115124 | + pTab->tabFlags |= oooHidden; |
| 115125 | + continue; |
| 115126 | + } |
| 114700 | 115127 | nType = sqlite3Strlen30(zType); |
| 114701 | 115128 | if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){ |
| 114702 | 115129 | for(i=0; i<nType; i++){ |
| 114703 | 115130 | if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7)) |
| 114704 | 115131 | && (zType[i+7]=='\0' || zType[i+7]==' ') |
| | @@ -114717,10 +115144,13 @@ |
| 114717 | 115144 | if( zType[i]=='\0' && i>0 ){ |
| 114718 | 115145 | assert(zType[i-1]==' '); |
| 114719 | 115146 | zType[i-1] = '\0'; |
| 114720 | 115147 | } |
| 114721 | 115148 | pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN; |
| 115149 | + oooHidden = TF_OOOHidden; |
| 115150 | + }else{ |
| 115151 | + pTab->tabFlags |= oooHidden; |
| 114722 | 115152 | } |
| 114723 | 115153 | } |
| 114724 | 115154 | } |
| 114725 | 115155 | } |
| 114726 | 115156 | |
| | @@ -114845,12 +115275,12 @@ |
| 114845 | 115275 | ** This function is used to set the schema of a virtual table. It is only |
| 114846 | 115276 | ** valid to call this function from within the xCreate() or xConnect() of a |
| 114847 | 115277 | ** virtual table module. |
| 114848 | 115278 | */ |
| 114849 | 115279 | SQLITE_API int SQLITE_STDCALL sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ |
| 115280 | + VtabCtx *pCtx; |
| 114850 | 115281 | Parse *pParse; |
| 114851 | | - |
| 114852 | 115282 | int rc = SQLITE_OK; |
| 114853 | 115283 | Table *pTab; |
| 114854 | 115284 | char *zErr = 0; |
| 114855 | 115285 | |
| 114856 | 115286 | #ifdef SQLITE_ENABLE_API_ARMOR |
| | @@ -114857,15 +115287,17 @@ |
| 114857 | 115287 | if( !sqlite3SafetyCheckOk(db) || zCreateTable==0 ){ |
| 114858 | 115288 | return SQLITE_MISUSE_BKPT; |
| 114859 | 115289 | } |
| 114860 | 115290 | #endif |
| 114861 | 115291 | sqlite3_mutex_enter(db->mutex); |
| 114862 | | - if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){ |
| 115292 | + pCtx = db->pVtabCtx; |
| 115293 | + if( !pCtx || pCtx->bDeclared ){ |
| 114863 | 115294 | sqlite3Error(db, SQLITE_MISUSE); |
| 114864 | 115295 | sqlite3_mutex_leave(db->mutex); |
| 114865 | 115296 | return SQLITE_MISUSE_BKPT; |
| 114866 | 115297 | } |
| 115298 | + pTab = pCtx->pTab; |
| 114867 | 115299 | assert( (pTab->tabFlags & TF_Virtual)!=0 ); |
| 114868 | 115300 | |
| 114869 | 115301 | pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); |
| 114870 | 115302 | if( pParse==0 ){ |
| 114871 | 115303 | rc = SQLITE_NOMEM; |
| | @@ -114884,11 +115316,11 @@ |
| 114884 | 115316 | pTab->aCol = pParse->pNewTable->aCol; |
| 114885 | 115317 | pTab->nCol = pParse->pNewTable->nCol; |
| 114886 | 115318 | pParse->pNewTable->nCol = 0; |
| 114887 | 115319 | pParse->pNewTable->aCol = 0; |
| 114888 | 115320 | } |
| 114889 | | - db->pVtabCtx->pTab = 0; |
| 115321 | + pCtx->bDeclared = 1; |
| 114890 | 115322 | }else{ |
| 114891 | 115323 | sqlite3ErrorWithMsg(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr); |
| 114892 | 115324 | sqlite3DbFree(db, zErr); |
| 114893 | 115325 | rc = SQLITE_ERROR; |
| 114894 | 115326 | } |
| | @@ -115078,11 +115510,11 @@ |
| 115078 | 115510 | */ |
| 115079 | 115511 | SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){ |
| 115080 | 115512 | int rc = SQLITE_OK; |
| 115081 | 115513 | |
| 115082 | 115514 | assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN ); |
| 115083 | | - assert( iSavepoint>=0 ); |
| 115515 | + assert( iSavepoint>=-1 ); |
| 115084 | 115516 | if( db->aVTrans ){ |
| 115085 | 115517 | int i; |
| 115086 | 115518 | for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){ |
| 115087 | 115519 | VTable *pVTab = db->aVTrans[i]; |
| 115088 | 115520 | const sqlite3_module *pMod = pVTab->pMod->pModule; |
| | @@ -115196,11 +115628,11 @@ |
| 115196 | 115628 | assert( IsVirtual(pTab) ); |
| 115197 | 115629 | for(i=0; i<pToplevel->nVtabLock; i++){ |
| 115198 | 115630 | if( pTab==pToplevel->apVtabLock[i] ) return; |
| 115199 | 115631 | } |
| 115200 | 115632 | n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); |
| 115201 | | - apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n); |
| 115633 | + apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n); |
| 115202 | 115634 | if( apVtabLock ){ |
| 115203 | 115635 | pToplevel->apVtabLock = apVtabLock; |
| 115204 | 115636 | pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; |
| 115205 | 115637 | }else{ |
| 115206 | 115638 | pToplevel->db->mallocFailed = 1; |
| | @@ -115995,17 +116427,18 @@ |
| 115995 | 116427 | ** In the previous sentence and in the diagram, "slot[]" refers to |
| 115996 | 116428 | ** the WhereClause.a[] array. The slot[] array grows as needed to contain |
| 115997 | 116429 | ** all terms of the WHERE clause. |
| 115998 | 116430 | */ |
| 115999 | 116431 | static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){ |
| 116432 | + Expr *pE2 = sqlite3ExprSkipCollate(pExpr); |
| 116000 | 116433 | pWC->op = op; |
| 116001 | | - if( pExpr==0 ) return; |
| 116002 | | - if( pExpr->op!=op ){ |
| 116434 | + if( pE2==0 ) return; |
| 116435 | + if( pE2->op!=op ){ |
| 116003 | 116436 | whereClauseInsert(pWC, pExpr, 0); |
| 116004 | 116437 | }else{ |
| 116005 | | - whereSplit(pWC, pExpr->pLeft, op); |
| 116006 | | - whereSplit(pWC, pExpr->pRight, op); |
| 116438 | + whereSplit(pWC, pE2->pLeft, op); |
| 116439 | + whereSplit(pWC, pE2->pRight, op); |
| 116007 | 116440 | } |
| 116008 | 116441 | } |
| 116009 | 116442 | |
| 116010 | 116443 | /* |
| 116011 | 116444 | ** Initialize a WhereMaskSet object |
| | @@ -117272,11 +117705,11 @@ |
| 117272 | 117705 | if( p->op==TK_COLUMN |
| 117273 | 117706 | && p->iColumn==pIdx->aiColumn[iCol] |
| 117274 | 117707 | && p->iTable==iBase |
| 117275 | 117708 | ){ |
| 117276 | 117709 | CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr); |
| 117277 | | - if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
| 117710 | + if( pColl && 0==sqlite3StrICmp(pColl->zName, zColl) ){ |
| 117278 | 117711 | return i; |
| 117279 | 117712 | } |
| 117280 | 117713 | } |
| 117281 | 117714 | } |
| 117282 | 117715 | |
| | @@ -117546,11 +117979,11 @@ |
| 117546 | 117979 | if( (idxCols & cMask)==0 ){ |
| 117547 | 117980 | Expr *pX = pTerm->pExpr; |
| 117548 | 117981 | idxCols |= cMask; |
| 117549 | 117982 | pIdx->aiColumn[n] = pTerm->u.leftColumn; |
| 117550 | 117983 | pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); |
| 117551 | | - pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; |
| 117984 | + pIdx->azColl[n] = pColl ? pColl->zName : "BINARY"; |
| 117552 | 117985 | n++; |
| 117553 | 117986 | } |
| 117554 | 117987 | } |
| 117555 | 117988 | } |
| 117556 | 117989 | assert( (u32)n==pLoop->u.btree.nEq ); |
| | @@ -118842,12 +119275,11 @@ |
| 118842 | 119275 | |
| 118843 | 119276 | isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 |
| 118844 | 119277 | || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) |
| 118845 | 119278 | || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); |
| 118846 | 119279 | |
| 118847 | | - sqlite3StrAccumInit(&str, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
| 118848 | | - str.db = db; |
| 119280 | + sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); |
| 118849 | 119281 | sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); |
| 118850 | 119282 | if( pItem->pSelect ){ |
| 118851 | 119283 | sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId); |
| 118852 | 119284 | }else{ |
| 118853 | 119285 | sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName); |
| | @@ -120042,10 +120474,17 @@ |
| 120042 | 120474 | /* |
| 120043 | 120475 | ** Free a WhereInfo structure |
| 120044 | 120476 | */ |
| 120045 | 120477 | static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ |
| 120046 | 120478 | if( ALWAYS(pWInfo) ){ |
| 120479 | + int i; |
| 120480 | + for(i=0; i<pWInfo->nLevel; i++){ |
| 120481 | + WhereLevel *pLevel = &pWInfo->a[i]; |
| 120482 | + if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){ |
| 120483 | + sqlite3DbFree(db, pLevel->u.in.aInLoop); |
| 120484 | + } |
| 120485 | + } |
| 120047 | 120486 | whereClauseClear(&pWInfo->sWC); |
| 120048 | 120487 | while( pWInfo->pLoops ){ |
| 120049 | 120488 | WhereLoop *p = pWInfo->pLoops; |
| 120050 | 120489 | pWInfo->pLoops = p->pNextLoop; |
| 120051 | 120490 | whereLoopDelete(db, p); |
| | @@ -120521,11 +120960,11 @@ |
| 120521 | 120960 | ** changes "x IN (?)" into "x=?". */ |
| 120522 | 120961 | |
| 120523 | 120962 | }else if( eOp & (WO_EQ) ){ |
| 120524 | 120963 | pNew->wsFlags |= WHERE_COLUMN_EQ; |
| 120525 | 120964 | if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){ |
| 120526 | | - if( iCol>=0 && !IsUniqueIndex(pProbe) ){ |
| 120965 | + if( iCol>=0 && pProbe->uniqNotNull==0 ){ |
| 120527 | 120966 | pNew->wsFlags |= WHERE_UNQ_WANTED; |
| 120528 | 120967 | }else{ |
| 120529 | 120968 | pNew->wsFlags |= WHERE_ONEROW; |
| 120530 | 120969 | } |
| 120531 | 120970 | } |
| | @@ -121981,11 +122420,11 @@ |
| 121981 | 122420 | pWInfo->nOBSat = pFrom->isOrdered; |
| 121982 | 122421 | if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0; |
| 121983 | 122422 | pWInfo->revMask = pFrom->revLoop; |
| 121984 | 122423 | } |
| 121985 | 122424 | if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP) |
| 121986 | | - && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr |
| 122425 | + && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr && nLoop>0 |
| 121987 | 122426 | ){ |
| 121988 | 122427 | Bitmask revMask = 0; |
| 121989 | 122428 | int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy, |
| 121990 | 122429 | pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &revMask |
| 121991 | 122430 | ); |
| | @@ -122386,11 +122825,10 @@ |
| 122386 | 122825 | if( pParse->nErr || NEVER(db->mallocFailed) ){ |
| 122387 | 122826 | goto whereBeginError; |
| 122388 | 122827 | } |
| 122389 | 122828 | #ifdef WHERETRACE_ENABLED /* !=0 */ |
| 122390 | 122829 | if( sqlite3WhereTrace ){ |
| 122391 | | - int ii; |
| 122392 | 122830 | sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut); |
| 122393 | 122831 | if( pWInfo->nOBSat>0 ){ |
| 122394 | 122832 | sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask); |
| 122395 | 122833 | } |
| 122396 | 122834 | switch( pWInfo->eDistinct ){ |
| | @@ -122639,11 +123077,10 @@ |
| 122639 | 123077 | VdbeCoverage(v); |
| 122640 | 123078 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen); |
| 122641 | 123079 | VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen); |
| 122642 | 123080 | sqlite3VdbeJumpHere(v, pIn->addrInTop-1); |
| 122643 | 123081 | } |
| 122644 | | - sqlite3DbFree(db, pLevel->u.in.aInLoop); |
| 122645 | 123082 | } |
| 122646 | 123083 | sqlite3VdbeResolveLabel(v, pLevel->addrBrk); |
| 122647 | 123084 | if( pLevel->addrSkip ){ |
| 122648 | 123085 | sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip); |
| 122649 | 123086 | VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName)); |
| | @@ -122850,10 +123287,32 @@ |
| 122850 | 123287 | /* |
| 122851 | 123288 | ** An instance of this structure holds the ATTACH key and the key type. |
| 122852 | 123289 | */ |
| 122853 | 123290 | struct AttachKey { int type; Token key; }; |
| 122854 | 123291 | |
| 123292 | + |
| 123293 | + /* |
| 123294 | + ** For a compound SELECT statement, make sure p->pPrior->pNext==p for |
| 123295 | + ** all elements in the list. And make sure list length does not exceed |
| 123296 | + ** SQLITE_LIMIT_COMPOUND_SELECT. |
| 123297 | + */ |
| 123298 | + static void parserDoubleLinkSelect(Parse *pParse, Select *p){ |
| 123299 | + if( p->pPrior ){ |
| 123300 | + Select *pNext = 0, *pLoop; |
| 123301 | + int mxSelect, cnt = 0; |
| 123302 | + for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){ |
| 123303 | + pLoop->pNext = pNext; |
| 123304 | + pLoop->selFlags |= SF_Compound; |
| 123305 | + } |
| 123306 | + if( (p->selFlags & SF_MultiValue)==0 && |
| 123307 | + (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 && |
| 123308 | + cnt>mxSelect |
| 123309 | + ){ |
| 123310 | + sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); |
| 123311 | + } |
| 123312 | + } |
| 123313 | + } |
| 122855 | 123314 | |
| 122856 | 123315 | /* This is a utility routine used to set the ExprSpan.zStart and |
| 122857 | 123316 | ** ExprSpan.zEnd values of pOut so that the span covers the complete |
| 122858 | 123317 | ** range of text beginning with pStart and going to the end of pEnd. |
| 122859 | 123318 | */ |
| | @@ -125167,31 +125626,14 @@ |
| 125167 | 125626 | sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3); |
| 125168 | 125627 | } |
| 125169 | 125628 | break; |
| 125170 | 125629 | case 112: /* select ::= with selectnowith */ |
| 125171 | 125630 | { |
| 125172 | | - Select *p = yymsp[0].minor.yy3, *pNext, *pLoop; |
| 125631 | + Select *p = yymsp[0].minor.yy3; |
| 125173 | 125632 | if( p ){ |
| 125174 | | - int cnt = 0, mxSelect; |
| 125175 | 125633 | p->pWith = yymsp[-1].minor.yy59; |
| 125176 | | - if( p->pPrior ){ |
| 125177 | | - u16 allValues = SF_Values; |
| 125178 | | - pNext = 0; |
| 125179 | | - for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){ |
| 125180 | | - pLoop->pNext = pNext; |
| 125181 | | - pLoop->selFlags |= SF_Compound; |
| 125182 | | - allValues &= pLoop->selFlags; |
| 125183 | | - } |
| 125184 | | - if( allValues ){ |
| 125185 | | - p->selFlags |= SF_AllValues; |
| 125186 | | - }else if( |
| 125187 | | - (mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT])>0 |
| 125188 | | - && cnt>mxSelect |
| 125189 | | - ){ |
| 125190 | | - sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); |
| 125191 | | - } |
| 125192 | | - } |
| 125634 | + parserDoubleLinkSelect(pParse, p); |
| 125193 | 125635 | }else{ |
| 125194 | 125636 | sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy59); |
| 125195 | 125637 | } |
| 125196 | 125638 | yygotominor.yy3 = p; |
| 125197 | 125639 | } |
| | @@ -125205,16 +125647,18 @@ |
| 125205 | 125647 | Select *pRhs = yymsp[0].minor.yy3; |
| 125206 | 125648 | if( pRhs && pRhs->pPrior ){ |
| 125207 | 125649 | SrcList *pFrom; |
| 125208 | 125650 | Token x; |
| 125209 | 125651 | x.n = 0; |
| 125652 | + parserDoubleLinkSelect(pParse, pRhs); |
| 125210 | 125653 | pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0); |
| 125211 | 125654 | pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0); |
| 125212 | 125655 | } |
| 125213 | 125656 | if( pRhs ){ |
| 125214 | 125657 | pRhs->op = (u8)yymsp[-1].minor.yy328; |
| 125215 | 125658 | pRhs->pPrior = yymsp[-2].minor.yy3; |
| 125659 | + pRhs->selFlags &= ~SF_MultiValue; |
| 125216 | 125660 | if( yymsp[-1].minor.yy328!=TK_ALL ) pParse->hasCompound = 1; |
| 125217 | 125661 | }else{ |
| 125218 | 125662 | sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy3); |
| 125219 | 125663 | } |
| 125220 | 125664 | yygotominor.yy3 = pRhs; |
| | @@ -125257,17 +125701,20 @@ |
| 125257 | 125701 | yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0); |
| 125258 | 125702 | } |
| 125259 | 125703 | break; |
| 125260 | 125704 | case 121: /* values ::= values COMMA LP exprlist RP */ |
| 125261 | 125705 | { |
| 125262 | | - Select *pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0); |
| 125706 | + Select *pRight, *pLeft = yymsp[-4].minor.yy3; |
| 125707 | + pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values|SF_MultiValue,0,0); |
| 125708 | + if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue; |
| 125263 | 125709 | if( pRight ){ |
| 125264 | 125710 | pRight->op = TK_ALL; |
| 125265 | | - pRight->pPrior = yymsp[-4].minor.yy3; |
| 125711 | + pLeft = yymsp[-4].minor.yy3; |
| 125712 | + pRight->pPrior = pLeft; |
| 125266 | 125713 | yygotominor.yy3 = pRight; |
| 125267 | 125714 | }else{ |
| 125268 | | - yygotominor.yy3 = yymsp[-4].minor.yy3; |
| 125715 | + yygotominor.yy3 = pLeft; |
| 125269 | 125716 | } |
| 125270 | 125717 | } |
| 125271 | 125718 | break; |
| 125272 | 125719 | case 122: /* distinct ::= DISTINCT */ |
| 125273 | 125720 | {yygotominor.yy381 = SF_Distinct;} |
| | @@ -127067,14 +127514,12 @@ |
| 127067 | 127514 | goto abort_parse; |
| 127068 | 127515 | } |
| 127069 | 127516 | break; |
| 127070 | 127517 | } |
| 127071 | 127518 | case TK_ILLEGAL: { |
| 127072 | | - sqlite3DbFree(db, *pzErrMsg); |
| 127073 | | - *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"", |
| 127519 | + sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"", |
| 127074 | 127520 | &pParse->sLastToken); |
| 127075 | | - nErr++; |
| 127076 | 127521 | goto abort_parse; |
| 127077 | 127522 | } |
| 127078 | 127523 | case TK_SEMI: { |
| 127079 | 127524 | pParse->zTail = &zSql[i]; |
| 127080 | 127525 | /* Fall thru into the default case */ |
| | @@ -127088,16 +127533,19 @@ |
| 127088 | 127533 | break; |
| 127089 | 127534 | } |
| 127090 | 127535 | } |
| 127091 | 127536 | } |
| 127092 | 127537 | abort_parse: |
| 127093 | | - if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ |
| 127538 | + assert( nErr==0 ); |
| 127539 | + if( zSql[i]==0 && pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ |
| 127094 | 127540 | if( lastTokenParsed!=TK_SEMI ){ |
| 127095 | 127541 | sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); |
| 127096 | 127542 | pParse->zTail = &zSql[i]; |
| 127097 | 127543 | } |
| 127098 | | - sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); |
| 127544 | + if( pParse->rc==SQLITE_OK && db->mallocFailed==0 ){ |
| 127545 | + sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); |
| 127546 | + } |
| 127099 | 127547 | } |
| 127100 | 127548 | #ifdef YYTRACKMAXSTACKDEPTH |
| 127101 | 127549 | sqlite3_mutex_enter(sqlite3MallocMutex()); |
| 127102 | 127550 | sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK, |
| 127103 | 127551 | sqlite3ParserStackPeak(pEngine) |
| | @@ -127154,13 +127602,11 @@ |
| 127154 | 127602 | while( pParse->pZombieTab ){ |
| 127155 | 127603 | Table *p = pParse->pZombieTab; |
| 127156 | 127604 | pParse->pZombieTab = p->pNextZombie; |
| 127157 | 127605 | sqlite3DeleteTable(db, p); |
| 127158 | 127606 | } |
| 127159 | | - if( nErr>0 && pParse->rc==SQLITE_OK ){ |
| 127160 | | - pParse->rc = SQLITE_ERROR; |
| 127161 | | - } |
| 127607 | + assert( nErr==0 || pParse->rc!=SQLITE_OK ); |
| 127162 | 127608 | return nErr; |
| 127163 | 127609 | } |
| 127164 | 127610 | |
| 127165 | 127611 | /************** End of tokenize.c ********************************************/ |
| 127166 | 127612 | /************** Begin file complete.c ****************************************/ |
| | @@ -127432,11 +127878,11 @@ |
| 127432 | 127878 | ** UTF-8. |
| 127433 | 127879 | */ |
| 127434 | 127880 | SQLITE_API int SQLITE_STDCALL sqlite3_complete16(const void *zSql){ |
| 127435 | 127881 | sqlite3_value *pVal; |
| 127436 | 127882 | char const *zSql8; |
| 127437 | | - int rc = SQLITE_NOMEM; |
| 127883 | + int rc; |
| 127438 | 127884 | |
| 127439 | 127885 | #ifndef SQLITE_OMIT_AUTOINIT |
| 127440 | 127886 | rc = sqlite3_initialize(); |
| 127441 | 127887 | if( rc ) return rc; |
| 127442 | 127888 | #endif |
| | @@ -127598,10 +128044,22 @@ |
| 127598 | 128044 | ** zero if and only if SQLite was compiled with mutexing code omitted due to |
| 127599 | 128045 | ** the SQLITE_THREADSAFE compile-time option being set to 0. |
| 127600 | 128046 | */ |
| 127601 | 128047 | SQLITE_API int SQLITE_STDCALL sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } |
| 127602 | 128048 | |
| 128049 | +/* |
| 128050 | +** When compiling the test fixture or with debugging enabled (on Win32), |
| 128051 | +** this variable being set to non-zero will cause OSTRACE macros to emit |
| 128052 | +** extra diagnostic information. |
| 128053 | +*/ |
| 128054 | +#ifdef SQLITE_HAVE_OS_TRACE |
| 128055 | +# ifndef SQLITE_DEBUG_OS_TRACE |
| 128056 | +# define SQLITE_DEBUG_OS_TRACE 0 |
| 128057 | +# endif |
| 128058 | + int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE; |
| 128059 | +#endif |
| 128060 | + |
| 127603 | 128061 | #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) |
| 127604 | 128062 | /* |
| 127605 | 128063 | ** If the following function pointer is not NULL and if |
| 127606 | 128064 | ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing |
| 127607 | 128065 | ** I/O active are written using this function. These messages |
| | @@ -128737,11 +129195,11 @@ |
| 128737 | 129195 | |
| 128738 | 129196 | /* |
| 128739 | 129197 | ** Return a static string containing the name corresponding to the error code |
| 128740 | 129198 | ** specified in the argument. |
| 128741 | 129199 | */ |
| 128742 | | -#if (defined(SQLITE_DEBUG) && SQLITE_OS_WIN) || defined(SQLITE_TEST) |
| 129200 | +#if defined(SQLITE_NEED_ERR_NAME) |
| 128743 | 129201 | SQLITE_PRIVATE const char *sqlite3ErrName(int rc){ |
| 128744 | 129202 | const char *zName = 0; |
| 128745 | 129203 | int i, origRc = rc; |
| 128746 | 129204 | for(i=0; i<2 && zName==0; i++, rc &= 0xff){ |
| 128747 | 129205 | switch( rc ){ |
| | @@ -129962,18 +130420,18 @@ |
| 129962 | 130420 | ){ |
| 129963 | 130421 | char *zOpt; |
| 129964 | 130422 | int eState; /* Parser state when parsing URI */ |
| 129965 | 130423 | int iIn; /* Input character index */ |
| 129966 | 130424 | int iOut = 0; /* Output character index */ |
| 129967 | | - int nByte = nUri+2; /* Bytes of space to allocate */ |
| 130425 | + u64 nByte = nUri+2; /* Bytes of space to allocate */ |
| 129968 | 130426 | |
| 129969 | 130427 | /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen |
| 129970 | 130428 | ** method that there may be extra parameters following the file-name. */ |
| 129971 | 130429 | flags |= SQLITE_OPEN_URI; |
| 129972 | 130430 | |
| 129973 | 130431 | for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&'); |
| 129974 | | - zFile = sqlite3_malloc(nByte); |
| 130432 | + zFile = sqlite3_malloc64(nByte); |
| 129975 | 130433 | if( !zFile ) return SQLITE_NOMEM; |
| 129976 | 130434 | |
| 129977 | 130435 | iIn = 5; |
| 129978 | 130436 | #ifdef SQLITE_ALLOW_URI_AUTHORITY |
| 129979 | 130437 | if( strncmp(zUri+5, "///", 3)==0 ){ |
| | @@ -130135,11 +130593,11 @@ |
| 130135 | 130593 | |
| 130136 | 130594 | zOpt = &zVal[nVal+1]; |
| 130137 | 130595 | } |
| 130138 | 130596 | |
| 130139 | 130597 | }else{ |
| 130140 | | - zFile = sqlite3_malloc(nUri+2); |
| 130598 | + zFile = sqlite3_malloc64(nUri+2); |
| 130141 | 130599 | if( !zFile ) return SQLITE_NOMEM; |
| 130142 | 130600 | memcpy(zFile, zUri, nUri); |
| 130143 | 130601 | zFile[nUri] = '\0'; |
| 130144 | 130602 | zFile[nUri+1] = '\0'; |
| 130145 | 130603 | flags &= ~SQLITE_OPEN_URI; |
| | @@ -130406,10 +130864,17 @@ |
| 130406 | 130864 | #ifdef SQLITE_ENABLE_RTREE |
| 130407 | 130865 | if( !db->mallocFailed && rc==SQLITE_OK){ |
| 130408 | 130866 | rc = sqlite3RtreeInit(db); |
| 130409 | 130867 | } |
| 130410 | 130868 | #endif |
| 130869 | + |
| 130870 | +#ifdef SQLITE_ENABLE_DBSTAT_VTAB |
| 130871 | + if( !db->mallocFailed && rc==SQLITE_OK){ |
| 130872 | + int sqlite3_dbstat_register(sqlite3*); |
| 130873 | + rc = sqlite3_dbstat_register(db); |
| 130874 | + } |
| 130875 | +#endif |
| 130411 | 130876 | |
| 130412 | 130877 | /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking |
| 130413 | 130878 | ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking |
| 130414 | 130879 | ** mode. Doing nothing at all also makes NORMAL the default. |
| 130415 | 130880 | */ |
| | @@ -132344,10 +132809,15 @@ |
| 132344 | 132809 | ** false. |
| 132345 | 132810 | */ |
| 132346 | 132811 | #ifdef SQLITE_COVERAGE_TEST |
| 132347 | 132812 | # define ALWAYS(x) (1) |
| 132348 | 132813 | # define NEVER(X) (0) |
| 132814 | +#elif defined(SQLITE_DEBUG) |
| 132815 | +# define ALWAYS(x) sqlite3Fts3Always((x)!=0) |
| 132816 | +# define NEVER(x) sqlite3Fts3Never((x)!=0) |
| 132817 | +SQLITE_PRIVATE int sqlite3Fts3Always(int b); |
| 132818 | +SQLITE_PRIVATE int sqlite3Fts3Never(int b); |
| 132349 | 132819 | #else |
| 132350 | 132820 | # define ALWAYS(x) (x) |
| 132351 | 132821 | # define NEVER(x) (x) |
| 132352 | 132822 | #endif |
| 132353 | 132823 | |
| | @@ -132744,10 +133214,11 @@ |
| 132744 | 133214 | #define fts3GetVarint32(p, piVal) ( \ |
| 132745 | 133215 | (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \ |
| 132746 | 133216 | ) |
| 132747 | 133217 | |
| 132748 | 133218 | /* fts3.c */ |
| 133219 | +SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char**,const char*,...); |
| 132749 | 133220 | SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64); |
| 132750 | 133221 | SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *); |
| 132751 | 133222 | SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *); |
| 132752 | 133223 | SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64); |
| 132753 | 133224 | SQLITE_PRIVATE void sqlite3Fts3Dequote(char *); |
| | @@ -132832,10 +133303,17 @@ |
| 132832 | 133303 | |
| 132833 | 133304 | static int fts3EvalNext(Fts3Cursor *pCsr); |
| 132834 | 133305 | static int fts3EvalStart(Fts3Cursor *pCsr); |
| 132835 | 133306 | static int fts3TermSegReaderCursor( |
| 132836 | 133307 | Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **); |
| 133308 | + |
| 133309 | +#ifndef SQLITE_AMALGAMATION |
| 133310 | +# if defined(SQLITE_DEBUG) |
| 133311 | +SQLITE_PRIVATE int sqlite3Fts3Always(int b) { assert( b ); return b; } |
| 133312 | +SQLITE_PRIVATE int sqlite3Fts3Never(int b) { assert( !b ); return b; } |
| 133313 | +# endif |
| 133314 | +#endif |
| 132837 | 133315 | |
| 132838 | 133316 | /* |
| 132839 | 133317 | ** Write a 64-bit variable-length integer to memory starting at p[0]. |
| 132840 | 133318 | ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes. |
| 132841 | 133319 | ** The number of bytes written is returned. |
| | @@ -132942,11 +133420,11 @@ |
| 132942 | 133420 | int iOut = 0; /* Index of next byte to write to output */ |
| 132943 | 133421 | |
| 132944 | 133422 | /* If the first byte was a '[', then the close-quote character is a ']' */ |
| 132945 | 133423 | if( quote=='[' ) quote = ']'; |
| 132946 | 133424 | |
| 132947 | | - while( ALWAYS(z[iIn]) ){ |
| 133425 | + while( z[iIn] ){ |
| 132948 | 133426 | if( z[iIn]==quote ){ |
| 132949 | 133427 | if( z[iIn+1]!=quote ) break; |
| 132950 | 133428 | z[iOut++] = quote; |
| 132951 | 133429 | iIn += 2; |
| 132952 | 133430 | }else{ |
| | @@ -133020,10 +133498,21 @@ |
| 133020 | 133498 | p->pTokenizer->pModule->xDestroy(p->pTokenizer); |
| 133021 | 133499 | |
| 133022 | 133500 | sqlite3_free(p); |
| 133023 | 133501 | return SQLITE_OK; |
| 133024 | 133502 | } |
| 133503 | + |
| 133504 | +/* |
| 133505 | +** Write an error message into *pzErr |
| 133506 | +*/ |
| 133507 | +SQLITE_PRIVATE void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){ |
| 133508 | + va_list ap; |
| 133509 | + sqlite3_free(*pzErr); |
| 133510 | + va_start(ap, zFormat); |
| 133511 | + *pzErr = sqlite3_vmprintf(zFormat, ap); |
| 133512 | + va_end(ap); |
| 133513 | +} |
| 133025 | 133514 | |
| 133026 | 133515 | /* |
| 133027 | 133516 | ** Construct one or more SQL statements from the format string given |
| 133028 | 133517 | ** and then evaluate those statements. The success code is written |
| 133029 | 133518 | ** into *pRc. |
| | @@ -133539,11 +134028,12 @@ |
| 133539 | 134028 | sqlite3 *db, /* Database handle */ |
| 133540 | 134029 | const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */ |
| 133541 | 134030 | const char *zTbl, /* Name of content table */ |
| 133542 | 134031 | const char ***pazCol, /* OUT: Malloc'd array of column names */ |
| 133543 | 134032 | int *pnCol, /* OUT: Size of array *pazCol */ |
| 133544 | | - int *pnStr /* OUT: Bytes of string content */ |
| 134033 | + int *pnStr, /* OUT: Bytes of string content */ |
| 134034 | + char **pzErr /* OUT: error message */ |
| 133545 | 134035 | ){ |
| 133546 | 134036 | int rc = SQLITE_OK; /* Return code */ |
| 133547 | 134037 | char *zSql; /* "SELECT *" statement on zTbl */ |
| 133548 | 134038 | sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */ |
| 133549 | 134039 | |
| | @@ -133550,10 +134040,13 @@ |
| 133550 | 134040 | zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl); |
| 133551 | 134041 | if( !zSql ){ |
| 133552 | 134042 | rc = SQLITE_NOMEM; |
| 133553 | 134043 | }else{ |
| 133554 | 134044 | rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 134045 | + if( rc!=SQLITE_OK ){ |
| 134046 | + sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db)); |
| 134047 | + } |
| 133555 | 134048 | } |
| 133556 | 134049 | sqlite3_free(zSql); |
| 133557 | 134050 | |
| 133558 | 134051 | if( rc==SQLITE_OK ){ |
| 133559 | 134052 | const char **azCol; /* Output array */ |
| | @@ -133716,17 +134209,17 @@ |
| 133716 | 134209 | if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){ |
| 133717 | 134210 | break; |
| 133718 | 134211 | } |
| 133719 | 134212 | } |
| 133720 | 134213 | if( iOpt==SizeofArray(aFts4Opt) ){ |
| 133721 | | - *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z); |
| 134214 | + sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z); |
| 133722 | 134215 | rc = SQLITE_ERROR; |
| 133723 | 134216 | }else{ |
| 133724 | 134217 | switch( iOpt ){ |
| 133725 | 134218 | case 0: /* MATCHINFO */ |
| 133726 | 134219 | if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){ |
| 133727 | | - *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal); |
| 134220 | + sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal); |
| 133728 | 134221 | rc = SQLITE_ERROR; |
| 133729 | 134222 | } |
| 133730 | 134223 | bNoDocsize = 1; |
| 133731 | 134224 | break; |
| 133732 | 134225 | |
| | @@ -133750,11 +134243,11 @@ |
| 133750 | 134243 | |
| 133751 | 134244 | case 4: /* ORDER */ |
| 133752 | 134245 | if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3)) |
| 133753 | 134246 | && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4)) |
| 133754 | 134247 | ){ |
| 133755 | | - *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal); |
| 134248 | + sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal); |
| 133756 | 134249 | rc = SQLITE_ERROR; |
| 133757 | 134250 | } |
| 133758 | 134251 | bDescIdx = (zVal[0]=='d' || zVal[0]=='D'); |
| 133759 | 134252 | break; |
| 133760 | 134253 | |
| | @@ -133801,11 +134294,11 @@ |
| 133801 | 134294 | zCompress = 0; |
| 133802 | 134295 | zUncompress = 0; |
| 133803 | 134296 | if( nCol==0 ){ |
| 133804 | 134297 | sqlite3_free((void*)aCol); |
| 133805 | 134298 | aCol = 0; |
| 133806 | | - rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString); |
| 134299 | + rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr); |
| 133807 | 134300 | |
| 133808 | 134301 | /* If a languageid= option was specified, remove the language id |
| 133809 | 134302 | ** column from the aCol[] array. */ |
| 133810 | 134303 | if( rc==SQLITE_OK && zLanguageid ){ |
| 133811 | 134304 | int j; |
| | @@ -133836,11 +134329,11 @@ |
| 133836 | 134329 | assert( pTokenizer ); |
| 133837 | 134330 | |
| 133838 | 134331 | rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex); |
| 133839 | 134332 | if( rc==SQLITE_ERROR ){ |
| 133840 | 134333 | assert( zPrefix ); |
| 133841 | | - *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix); |
| 134334 | + sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix); |
| 133842 | 134335 | } |
| 133843 | 134336 | if( rc!=SQLITE_OK ) goto fts3_init_out; |
| 133844 | 134337 | |
| 133845 | 134338 | /* Allocate and populate the Fts3Table structure. */ |
| 133846 | 134339 | nByte = sizeof(Fts3Table) + /* Fts3Table */ |
| | @@ -133918,19 +134411,19 @@ |
| 133918 | 134411 | } |
| 133919 | 134412 | } |
| 133920 | 134413 | } |
| 133921 | 134414 | for(i=0; i<nNotindexed; i++){ |
| 133922 | 134415 | if( azNotindexed[i] ){ |
| 133923 | | - *pzErr = sqlite3_mprintf("no such column: %s", azNotindexed[i]); |
| 134416 | + sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]); |
| 133924 | 134417 | rc = SQLITE_ERROR; |
| 133925 | 134418 | } |
| 133926 | 134419 | } |
| 133927 | 134420 | |
| 133928 | 134421 | if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){ |
| 133929 | 134422 | char const *zMiss = (zCompress==0 ? "compress" : "uncompress"); |
| 133930 | 134423 | rc = SQLITE_ERROR; |
| 133931 | | - *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss); |
| 134424 | + sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss); |
| 133932 | 134425 | } |
| 133933 | 134426 | p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc); |
| 133934 | 134427 | p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc); |
| 133935 | 134428 | if( rc!=SQLITE_OK ) goto fts3_init_out; |
| 133936 | 134429 | |
| | @@ -135319,11 +135812,11 @@ |
| 135319 | 135812 | ** Fts3SegReaderPending might segfault, as the data structures used by |
| 135320 | 135813 | ** fts4aux are not completely populated. So it's easiest to filter these |
| 135321 | 135814 | ** calls out here. */ |
| 135322 | 135815 | if( iLevel<0 && p->aIndex ){ |
| 135323 | 135816 | Fts3SegReader *pSeg = 0; |
| 135324 | | - rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg); |
| 135817 | + rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg); |
| 135325 | 135818 | if( rc==SQLITE_OK && pSeg ){ |
| 135326 | 135819 | rc = fts3SegReaderCursorAppend(pCsr, pSeg); |
| 135327 | 135820 | } |
| 135328 | 135821 | } |
| 135329 | 135822 | |
| | @@ -135968,15 +136461,35 @@ |
| 135968 | 136461 | */ |
| 135969 | 136462 | static void fts3ReversePoslist(char *pStart, char **ppPoslist){ |
| 135970 | 136463 | char *p = &(*ppPoslist)[-2]; |
| 135971 | 136464 | char c = 0; |
| 135972 | 136465 | |
| 136466 | + /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */ |
| 135973 | 136467 | while( p>pStart && (c=*p--)==0 ); |
| 136468 | + |
| 136469 | + /* Search backwards for a varint with value zero (the end of the previous |
| 136470 | + ** poslist). This is an 0x00 byte preceded by some byte that does not |
| 136471 | + ** have the 0x80 bit set. */ |
| 135974 | 136472 | while( p>pStart && (*p & 0x80) | c ){ |
| 135975 | 136473 | c = *p--; |
| 135976 | 136474 | } |
| 135977 | | - if( p>pStart ){ p = &p[2]; } |
| 136475 | + assert( p==pStart || c==0 ); |
| 136476 | + |
| 136477 | + /* At this point p points to that preceding byte without the 0x80 bit |
| 136478 | + ** set. So to find the start of the poslist, skip forward 2 bytes then |
| 136479 | + ** over a varint. |
| 136480 | + ** |
| 136481 | + ** Normally. The other case is that p==pStart and the poslist to return |
| 136482 | + ** is the first in the doclist. In this case do not skip forward 2 bytes. |
| 136483 | + ** The second part of the if condition (c==0 && *ppPoslist>&p[2]) |
| 136484 | + ** is required for cases where the first byte of a doclist and the |
| 136485 | + ** doclist is empty. For example, if the first docid is 10, a doclist |
| 136486 | + ** that begins with: |
| 136487 | + ** |
| 136488 | + ** 0x0A 0x00 <next docid delta varint> |
| 136489 | + */ |
| 136490 | + if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; } |
| 135978 | 136491 | while( *p++&0x80 ); |
| 135979 | 136492 | *ppPoslist = p; |
| 135980 | 136493 | } |
| 135981 | 136494 | |
| 135982 | 136495 | /* |
| | @@ -136043,10 +136556,12 @@ |
| 136043 | 136556 | case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]); |
| 136044 | 136557 | case 2: zStart = (const char*)sqlite3_value_text(apVal[1]); |
| 136045 | 136558 | } |
| 136046 | 136559 | if( !zEllipsis || !zEnd || !zStart ){ |
| 136047 | 136560 | sqlite3_result_error_nomem(pContext); |
| 136561 | + }else if( nToken==0 ){ |
| 136562 | + sqlite3_result_text(pContext, "", -1, SQLITE_STATIC); |
| 136048 | 136563 | }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){ |
| 136049 | 136564 | sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken); |
| 136050 | 136565 | } |
| 136051 | 136566 | } |
| 136052 | 136567 | |
| | @@ -137104,16 +137619,18 @@ |
| 137104 | 137619 | Fts3Expr *pExpr, /* Expression to initialize phrases in */ |
| 137105 | 137620 | int *pRc /* IN/OUT: Error code */ |
| 137106 | 137621 | ){ |
| 137107 | 137622 | if( pExpr && SQLITE_OK==*pRc ){ |
| 137108 | 137623 | if( pExpr->eType==FTSQUERY_PHRASE ){ |
| 137109 | | - int i; |
| 137110 | 137624 | int nToken = pExpr->pPhrase->nToken; |
| 137111 | | - for(i=0; i<nToken; i++){ |
| 137112 | | - if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break; |
| 137625 | + if( nToken ){ |
| 137626 | + int i; |
| 137627 | + for(i=0; i<nToken; i++){ |
| 137628 | + if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break; |
| 137629 | + } |
| 137630 | + pExpr->bDeferred = (i==nToken); |
| 137113 | 137631 | } |
| 137114 | | - pExpr->bDeferred = (i==nToken); |
| 137115 | 137632 | *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase); |
| 137116 | 137633 | }else{ |
| 137117 | 137634 | fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc); |
| 137118 | 137635 | fts3EvalStartReaders(pCsr, pExpr->pRight, pRc); |
| 137119 | 137636 | pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred); |
| | @@ -138272,11 +138789,12 @@ |
| 138272 | 138789 | if( rc!=SQLITE_OK ) return rc; |
| 138273 | 138790 | |
| 138274 | 138791 | pIter = pPhrase->pOrPoslist; |
| 138275 | 138792 | iDocid = pPhrase->iOrDocid; |
| 138276 | 138793 | if( pCsr->bDesc==bDescDoclist ){ |
| 138277 | | - bEof = (pIter >= (pPhrase->doclist.aAll + pPhrase->doclist.nAll)); |
| 138794 | + bEof = !pPhrase->doclist.nAll || |
| 138795 | + (pIter >= (pPhrase->doclist.aAll + pPhrase->doclist.nAll)); |
| 138278 | 138796 | while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){ |
| 138279 | 138797 | sqlite3Fts3DoclistNext( |
| 138280 | 138798 | bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll, |
| 138281 | 138799 | &pIter, &iDocid, &bEof |
| 138282 | 138800 | ); |
| | @@ -138484,11 +139002,11 @@ |
| 138484 | 139002 | |
| 138485 | 139003 | *ppVtab = (sqlite3_vtab *)p; |
| 138486 | 139004 | return SQLITE_OK; |
| 138487 | 139005 | |
| 138488 | 139006 | bad_args: |
| 138489 | | - *pzErr = sqlite3_mprintf("invalid arguments to fts4aux constructor"); |
| 139007 | + sqlite3Fts3ErrMsg(pzErr, "invalid arguments to fts4aux constructor"); |
| 138490 | 139008 | return SQLITE_ERROR; |
| 138491 | 139009 | } |
| 138492 | 139010 | |
| 138493 | 139011 | /* |
| 138494 | 139012 | ** This function does the work for both the xDisconnect and xDestroy methods. |
| | @@ -139942,17 +140460,17 @@ |
| 139942 | 140460 | |
| 139943 | 140461 | if( rc!=SQLITE_OK ){ |
| 139944 | 140462 | sqlite3Fts3ExprFree(*ppExpr); |
| 139945 | 140463 | *ppExpr = 0; |
| 139946 | 140464 | if( rc==SQLITE_TOOBIG ){ |
| 139947 | | - *pzErr = sqlite3_mprintf( |
| 140465 | + sqlite3Fts3ErrMsg(pzErr, |
| 139948 | 140466 | "FTS expression tree is too large (maximum depth %d)", |
| 139949 | 140467 | SQLITE_FTS3_MAX_EXPR_DEPTH |
| 139950 | 140468 | ); |
| 139951 | 140469 | rc = SQLITE_ERROR; |
| 139952 | 140470 | }else if( rc==SQLITE_ERROR ){ |
| 139953 | | - *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z); |
| 140471 | + sqlite3Fts3ErrMsg(pzErr, "malformed MATCH expression: [%s]", z); |
| 139954 | 140472 | } |
| 139955 | 140473 | } |
| 139956 | 140474 | |
| 139957 | 140475 | return rc; |
| 139958 | 140476 | } |
| | @@ -141424,11 +141942,11 @@ |
| 141424 | 141942 | z[n] = '\0'; |
| 141425 | 141943 | sqlite3Fts3Dequote(z); |
| 141426 | 141944 | |
| 141427 | 141945 | m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1); |
| 141428 | 141946 | if( !m ){ |
| 141429 | | - *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z); |
| 141947 | + sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", z); |
| 141430 | 141948 | rc = SQLITE_ERROR; |
| 141431 | 141949 | }else{ |
| 141432 | 141950 | char const **aArg = 0; |
| 141433 | 141951 | int iArg = 0; |
| 141434 | 141952 | z = &z[n+1]; |
| | @@ -141447,11 +141965,11 @@ |
| 141447 | 141965 | z = &z[n+1]; |
| 141448 | 141966 | } |
| 141449 | 141967 | rc = m->xCreate(iArg, aArg, ppTok); |
| 141450 | 141968 | assert( rc!=SQLITE_OK || *ppTok ); |
| 141451 | 141969 | if( rc!=SQLITE_OK ){ |
| 141452 | | - *pzErr = sqlite3_mprintf("unknown tokenizer"); |
| 141970 | + sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer"); |
| 141453 | 141971 | }else{ |
| 141454 | 141972 | (*ppTok)->pModule = m; |
| 141455 | 141973 | } |
| 141456 | 141974 | sqlite3_free((void *)aArg); |
| 141457 | 141975 | } |
| | @@ -141531,13 +142049,13 @@ |
| 141531 | 142049 | |
| 141532 | 142050 | pHash = (Fts3Hash *)sqlite3_user_data(context); |
| 141533 | 142051 | p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); |
| 141534 | 142052 | |
| 141535 | 142053 | if( !p ){ |
| 141536 | | - char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); |
| 141537 | | - sqlite3_result_error(context, zErr, -1); |
| 141538 | | - sqlite3_free(zErr); |
| 142054 | + char *zErr2 = sqlite3_mprintf("unknown tokenizer: %s", zName); |
| 142055 | + sqlite3_result_error(context, zErr2, -1); |
| 142056 | + sqlite3_free(zErr2); |
| 141539 | 142057 | return; |
| 141540 | 142058 | } |
| 141541 | 142059 | |
| 141542 | 142060 | pRet = Tcl_NewObj(); |
| 141543 | 142061 | Tcl_IncrRefCount(pRet); |
| | @@ -142068,11 +142586,11 @@ |
| 142068 | 142586 | sqlite3_tokenizer_module *p; |
| 142069 | 142587 | int nName = (int)strlen(zName); |
| 142070 | 142588 | |
| 142071 | 142589 | p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); |
| 142072 | 142590 | if( !p ){ |
| 142073 | | - *pzErr = sqlite3_mprintf("unknown tokenizer: %s", zName); |
| 142591 | + sqlite3Fts3ErrMsg(pzErr, "unknown tokenizer: %s", zName); |
| 142074 | 142592 | return SQLITE_ERROR; |
| 142075 | 142593 | } |
| 142076 | 142594 | |
| 142077 | 142595 | *pp = p; |
| 142078 | 142596 | return SQLITE_OK; |
| | @@ -142765,11 +143283,11 @@ |
| 142765 | 143283 | /* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(?,?)", |
| 142766 | 143284 | /* 24 */ "", |
| 142767 | 143285 | /* 25 */ "", |
| 142768 | 143286 | |
| 142769 | 143287 | /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?", |
| 142770 | | -/* 27 */ "SELECT DISTINCT level / (1024 * ?) FROM %Q.'%q_segdir'", |
| 143288 | +/* 27 */ "SELECT ? UNION SELECT level / (1024 * ?) FROM %Q.'%q_segdir'", |
| 142771 | 143289 | |
| 142772 | 143290 | /* This statement is used to determine which level to read the input from |
| 142773 | 143291 | ** when performing an incremental merge. It returns the absolute level number |
| 142774 | 143292 | ** of the oldest level in the db that contains at least ? segments. Or, |
| 142775 | 143293 | ** if no level in the FTS index contains more than ? segments, the statement |
| | @@ -145883,11 +146401,12 @@ |
| 145883 | 146401 | sqlite3_stmt *pAllLangid = 0; |
| 145884 | 146402 | |
| 145885 | 146403 | rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0); |
| 145886 | 146404 | if( rc==SQLITE_OK ){ |
| 145887 | 146405 | int rc2; |
| 145888 | | - sqlite3_bind_int(pAllLangid, 1, p->nIndex); |
| 146406 | + sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid); |
| 146407 | + sqlite3_bind_int(pAllLangid, 2, p->nIndex); |
| 145889 | 146408 | while( sqlite3_step(pAllLangid)==SQLITE_ROW ){ |
| 145890 | 146409 | int i; |
| 145891 | 146410 | int iLangid = sqlite3_column_int(pAllLangid, 0); |
| 145892 | 146411 | for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){ |
| 145893 | 146412 | rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL); |
| | @@ -147215,11 +147734,11 @@ |
| 147215 | 147734 | while( i>0 && (pHint->a[i-1] & 0x80) ) i--; |
| 147216 | 147735 | |
| 147217 | 147736 | pHint->n = i; |
| 147218 | 147737 | i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel); |
| 147219 | 147738 | i += fts3GetVarint32(&pHint->a[i], pnInput); |
| 147220 | | - if( i!=nHint ) return SQLITE_CORRUPT_VTAB; |
| 147739 | + if( i!=nHint ) return FTS_CORRUPT_VTAB; |
| 147221 | 147740 | |
| 147222 | 147741 | return SQLITE_OK; |
| 147223 | 147742 | } |
| 147224 | 147743 | |
| 147225 | 147744 | |
| | @@ -147583,11 +148102,12 @@ |
| 147583 | 148102 | |
| 147584 | 148103 | /* This block calculates the checksum according to the FTS index. */ |
| 147585 | 148104 | rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0); |
| 147586 | 148105 | if( rc==SQLITE_OK ){ |
| 147587 | 148106 | int rc2; |
| 147588 | | - sqlite3_bind_int(pAllLangid, 1, p->nIndex); |
| 148107 | + sqlite3_bind_int(pAllLangid, 1, p->iPrevLangid); |
| 148108 | + sqlite3_bind_int(pAllLangid, 2, p->nIndex); |
| 147589 | 148109 | while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){ |
| 147590 | 148110 | int iLangid = sqlite3_column_int(pAllLangid, 0); |
| 147591 | 148111 | int i; |
| 147592 | 148112 | for(i=0; i<p->nIndex; i++){ |
| 147593 | 148113 | cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc); |
| | @@ -147596,11 +148116,10 @@ |
| 147596 | 148116 | rc2 = sqlite3_reset(pAllLangid); |
| 147597 | 148117 | if( rc==SQLITE_OK ) rc = rc2; |
| 147598 | 148118 | } |
| 147599 | 148119 | |
| 147600 | 148120 | /* This block calculates the checksum according to the %_content table */ |
| 147601 | | - rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0); |
| 147602 | 148121 | if( rc==SQLITE_OK ){ |
| 147603 | 148122 | sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule; |
| 147604 | 148123 | sqlite3_stmt *pStmt = 0; |
| 147605 | 148124 | char *zSql; |
| 147606 | 148125 | |
| | @@ -147693,11 +148212,11 @@ |
| 147693 | 148212 | Fts3Table *p /* FTS3 table handle */ |
| 147694 | 148213 | ){ |
| 147695 | 148214 | int rc; |
| 147696 | 148215 | int bOk = 0; |
| 147697 | 148216 | rc = fts3IntegrityCheck(p, &bOk); |
| 147698 | | - if( rc==SQLITE_OK && bOk==0 ) rc = SQLITE_CORRUPT_VTAB; |
| 148217 | + if( rc==SQLITE_OK && bOk==0 ) rc = FTS_CORRUPT_VTAB; |
| 147699 | 148218 | return rc; |
| 147700 | 148219 | } |
| 147701 | 148220 | |
| 147702 | 148221 | /* |
| 147703 | 148222 | ** Handle a 'special' INSERT of the form: |
| | @@ -148131,10 +148650,11 @@ |
| 148131 | 148650 | #define FTS3_MATCHINFO_NDOC 'n' /* 1 value */ |
| 148132 | 148651 | #define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */ |
| 148133 | 148652 | #define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */ |
| 148134 | 148653 | #define FTS3_MATCHINFO_LCS 's' /* nCol values */ |
| 148135 | 148654 | #define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */ |
| 148655 | +#define FTS3_MATCHINFO_LHITS 'y' /* nCol*nPhrase values */ |
| 148136 | 148656 | |
| 148137 | 148657 | /* |
| 148138 | 148658 | ** The default value for the second argument to matchinfo(). |
| 148139 | 148659 | */ |
| 148140 | 148660 | #define FTS3_MATCHINFO_DEFAULT "pcx" |
| | @@ -148912,10 +149432,55 @@ |
| 148912 | 149432 | } |
| 148913 | 149433 | } |
| 148914 | 149434 | |
| 148915 | 149435 | return rc; |
| 148916 | 149436 | } |
| 149437 | + |
| 149438 | +/* |
| 149439 | +** fts3ExprIterate() callback used to gather information for the matchinfo |
| 149440 | +** directive 'y'. |
| 149441 | +*/ |
| 149442 | +static int fts3ExprLHitsCb( |
| 149443 | + Fts3Expr *pExpr, /* Phrase expression node */ |
| 149444 | + int iPhrase, /* Phrase number */ |
| 149445 | + void *pCtx /* Pointer to MatchInfo structure */ |
| 149446 | +){ |
| 149447 | + MatchInfo *p = (MatchInfo *)pCtx; |
| 149448 | + Fts3Table *pTab = (Fts3Table *)p->pCursor->base.pVtab; |
| 149449 | + int rc = SQLITE_OK; |
| 149450 | + int iStart = iPhrase * p->nCol; |
| 149451 | + Fts3Expr *pEof; /* Ancestor node already at EOF */ |
| 149452 | + |
| 149453 | + /* This must be a phrase */ |
| 149454 | + assert( pExpr->pPhrase ); |
| 149455 | + |
| 149456 | + /* Initialize all output integers to zero. */ |
| 149457 | + memset(&p->aMatchinfo[iStart], 0, sizeof(u32) * p->nCol); |
| 149458 | + |
| 149459 | + /* Check if this or any parent node is at EOF. If so, then all output |
| 149460 | + ** values are zero. */ |
| 149461 | + for(pEof=pExpr; pEof && pEof->bEof==0; pEof=pEof->pParent); |
| 149462 | + |
| 149463 | + if( pEof==0 && pExpr->iDocid==p->pCursor->iPrevId ){ |
| 149464 | + Fts3Phrase *pPhrase = pExpr->pPhrase; |
| 149465 | + char *pIter = pPhrase->doclist.pList; |
| 149466 | + int iCol = 0; |
| 149467 | + |
| 149468 | + while( 1 ){ |
| 149469 | + int nHit = fts3ColumnlistCount(&pIter); |
| 149470 | + if( (pPhrase->iColumn>=pTab->nColumn || pPhrase->iColumn==iCol) ){ |
| 149471 | + p->aMatchinfo[iStart + iCol] = (u32)nHit; |
| 149472 | + } |
| 149473 | + assert( *pIter==0x00 || *pIter==0x01 ); |
| 149474 | + if( *pIter!=0x01 ) break; |
| 149475 | + pIter++; |
| 149476 | + pIter += fts3GetVarint32(pIter, &iCol); |
| 149477 | + } |
| 149478 | + } |
| 149479 | + |
| 149480 | + return rc; |
| 149481 | +} |
| 148917 | 149482 | |
| 148918 | 149483 | static int fts3MatchinfoCheck( |
| 148919 | 149484 | Fts3Table *pTab, |
| 148920 | 149485 | char cArg, |
| 148921 | 149486 | char **pzErr |
| | @@ -148925,14 +149490,15 @@ |
| 148925 | 149490 | || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4) |
| 148926 | 149491 | || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4) |
| 148927 | 149492 | || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize) |
| 148928 | 149493 | || (cArg==FTS3_MATCHINFO_LCS) |
| 148929 | 149494 | || (cArg==FTS3_MATCHINFO_HITS) |
| 149495 | + || (cArg==FTS3_MATCHINFO_LHITS) |
| 148930 | 149496 | ){ |
| 148931 | 149497 | return SQLITE_OK; |
| 148932 | 149498 | } |
| 148933 | | - *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg); |
| 149499 | + sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo request: %c", cArg); |
| 148934 | 149500 | return SQLITE_ERROR; |
| 148935 | 149501 | } |
| 148936 | 149502 | |
| 148937 | 149503 | static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){ |
| 148938 | 149504 | int nVal; /* Number of integers output by cArg */ |
| | @@ -148947,10 +149513,14 @@ |
| 148947 | 149513 | case FTS3_MATCHINFO_AVGLENGTH: |
| 148948 | 149514 | case FTS3_MATCHINFO_LENGTH: |
| 148949 | 149515 | case FTS3_MATCHINFO_LCS: |
| 148950 | 149516 | nVal = pInfo->nCol; |
| 148951 | 149517 | break; |
| 149518 | + |
| 149519 | + case FTS3_MATCHINFO_LHITS: |
| 149520 | + nVal = pInfo->nCol * pInfo->nPhrase; |
| 149521 | + break; |
| 148952 | 149522 | |
| 148953 | 149523 | default: |
| 148954 | 149524 | assert( cArg==FTS3_MATCHINFO_HITS ); |
| 148955 | 149525 | nVal = pInfo->nCol * pInfo->nPhrase * 3; |
| 148956 | 149526 | break; |
| | @@ -149201,10 +149771,14 @@ |
| 149201 | 149771 | rc = fts3ExprLoadDoclists(pCsr, 0, 0); |
| 149202 | 149772 | if( rc==SQLITE_OK ){ |
| 149203 | 149773 | rc = fts3MatchinfoLcs(pCsr, pInfo); |
| 149204 | 149774 | } |
| 149205 | 149775 | break; |
| 149776 | + |
| 149777 | + case FTS3_MATCHINFO_LHITS: |
| 149778 | + (void)fts3ExprIterate(pCsr->pExpr, fts3ExprLHitsCb, (void*)pInfo); |
| 149779 | + break; |
| 149206 | 149780 | |
| 149207 | 149781 | default: { |
| 149208 | 149782 | Fts3Expr *pExpr; |
| 149209 | 149783 | assert( zArg[i]==FTS3_MATCHINFO_HITS ); |
| 149210 | 149784 | pExpr = pCsr->pExpr; |
| | @@ -153214,15 +153788,23 @@ |
| 153214 | 153788 | ** conflict-handling mode specified by the user. |
| 153215 | 153789 | */ |
| 153216 | 153790 | if( nData>1 ){ |
| 153217 | 153791 | int ii; |
| 153218 | 153792 | |
| 153219 | | - /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */ |
| 153220 | | - assert( nData==(pRtree->nDim*2 + 3) ); |
| 153793 | + /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. |
| 153794 | + ** |
| 153795 | + ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared |
| 153796 | + ** with "column" that are interpreted as table constraints. |
| 153797 | + ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5)); |
| 153798 | + ** This problem was discovered after years of use, so we silently ignore |
| 153799 | + ** these kinds of misdeclared tables to avoid breaking any legacy. |
| 153800 | + */ |
| 153801 | + assert( nData<=(pRtree->nDim*2 + 3) ); |
| 153802 | + |
| 153221 | 153803 | #ifndef SQLITE_RTREE_INT_ONLY |
| 153222 | 153804 | if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ |
| 153223 | | - for(ii=0; ii<(pRtree->nDim*2); ii+=2){ |
| 153805 | + for(ii=0; ii<nData-4; ii+=2){ |
| 153224 | 153806 | cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]); |
| 153225 | 153807 | cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]); |
| 153226 | 153808 | if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){ |
| 153227 | 153809 | rc = SQLITE_CONSTRAINT; |
| 153228 | 153810 | goto constraint; |
| | @@ -153229,11 +153811,11 @@ |
| 153229 | 153811 | } |
| 153230 | 153812 | } |
| 153231 | 153813 | }else |
| 153232 | 153814 | #endif |
| 153233 | 153815 | { |
| 153234 | | - for(ii=0; ii<(pRtree->nDim*2); ii+=2){ |
| 153816 | + for(ii=0; ii<nData-4; ii+=2){ |
| 153235 | 153817 | cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]); |
| 153236 | 153818 | cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]); |
| 153237 | 153819 | if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){ |
| 153238 | 153820 | rc = SQLITE_CONSTRAINT; |
| 153239 | 153821 | goto constraint; |
| | @@ -154629,5 +155211,656 @@ |
| 154629 | 155211 | |
| 154630 | 155212 | #endif /* defined(SQLITE_ENABLE_ICU) */ |
| 154631 | 155213 | #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ |
| 154632 | 155214 | |
| 154633 | 155215 | /************** End of fts3_icu.c ********************************************/ |
| 155216 | +/************** Begin file dbstat.c ******************************************/ |
| 155217 | +/* |
| 155218 | +** 2010 July 12 |
| 155219 | +** |
| 155220 | +** The author disclaims copyright to this source code. In place of |
| 155221 | +** a legal notice, here is a blessing: |
| 155222 | +** |
| 155223 | +** May you do good and not evil. |
| 155224 | +** May you find forgiveness for yourself and forgive others. |
| 155225 | +** May you share freely, never taking more than you give. |
| 155226 | +** |
| 155227 | +****************************************************************************** |
| 155228 | +** |
| 155229 | +** This file contains an implementation of the "dbstat" virtual table. |
| 155230 | +** |
| 155231 | +** The dbstat virtual table is used to extract low-level formatting |
| 155232 | +** information from an SQLite database in order to implement the |
| 155233 | +** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script |
| 155234 | +** for an example implementation. |
| 155235 | +*/ |
| 155236 | + |
| 155237 | +#if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \ |
| 155238 | + && !defined(SQLITE_OMIT_VIRTUALTABLE) |
| 155239 | + |
| 155240 | +/* |
| 155241 | +** Page paths: |
| 155242 | +** |
| 155243 | +** The value of the 'path' column describes the path taken from the |
| 155244 | +** root-node of the b-tree structure to each page. The value of the |
| 155245 | +** root-node path is '/'. |
| 155246 | +** |
| 155247 | +** The value of the path for the left-most child page of the root of |
| 155248 | +** a b-tree is '/000/'. (Btrees store content ordered from left to right |
| 155249 | +** so the pages to the left have smaller keys than the pages to the right.) |
| 155250 | +** The next to left-most child of the root page is |
| 155251 | +** '/001', and so on, each sibling page identified by a 3-digit hex |
| 155252 | +** value. The children of the 451st left-most sibling have paths such |
| 155253 | +** as '/1c2/000/, '/1c2/001/' etc. |
| 155254 | +** |
| 155255 | +** Overflow pages are specified by appending a '+' character and a |
| 155256 | +** six-digit hexadecimal value to the path to the cell they are linked |
| 155257 | +** from. For example, the three overflow pages in a chain linked from |
| 155258 | +** the left-most cell of the 450th child of the root page are identified |
| 155259 | +** by the paths: |
| 155260 | +** |
| 155261 | +** '/1c2/000+000000' // First page in overflow chain |
| 155262 | +** '/1c2/000+000001' // Second page in overflow chain |
| 155263 | +** '/1c2/000+000002' // Third page in overflow chain |
| 155264 | +** |
| 155265 | +** If the paths are sorted using the BINARY collation sequence, then |
| 155266 | +** the overflow pages associated with a cell will appear earlier in the |
| 155267 | +** sort-order than its child page: |
| 155268 | +** |
| 155269 | +** '/1c2/000/' // Left-most child of 451st child of root |
| 155270 | +*/ |
| 155271 | +#define VTAB_SCHEMA \ |
| 155272 | + "CREATE TABLE xx( " \ |
| 155273 | + " name STRING, /* Name of table or index */" \ |
| 155274 | + " path INTEGER, /* Path to page from root */" \ |
| 155275 | + " pageno INTEGER, /* Page number */" \ |
| 155276 | + " pagetype STRING, /* 'internal', 'leaf' or 'overflow' */" \ |
| 155277 | + " ncell INTEGER, /* Cells on page (0 for overflow) */" \ |
| 155278 | + " payload INTEGER, /* Bytes of payload on this page */" \ |
| 155279 | + " unused INTEGER, /* Bytes of unused space on this page */" \ |
| 155280 | + " mx_payload INTEGER, /* Largest payload size of all cells */" \ |
| 155281 | + " pgoffset INTEGER, /* Offset of page in file */" \ |
| 155282 | + " pgsize INTEGER /* Size of the page */" \ |
| 155283 | + ");" |
| 155284 | + |
| 155285 | + |
| 155286 | +typedef struct StatTable StatTable; |
| 155287 | +typedef struct StatCursor StatCursor; |
| 155288 | +typedef struct StatPage StatPage; |
| 155289 | +typedef struct StatCell StatCell; |
| 155290 | + |
| 155291 | +struct StatCell { |
| 155292 | + int nLocal; /* Bytes of local payload */ |
| 155293 | + u32 iChildPg; /* Child node (or 0 if this is a leaf) */ |
| 155294 | + int nOvfl; /* Entries in aOvfl[] */ |
| 155295 | + u32 *aOvfl; /* Array of overflow page numbers */ |
| 155296 | + int nLastOvfl; /* Bytes of payload on final overflow page */ |
| 155297 | + int iOvfl; /* Iterates through aOvfl[] */ |
| 155298 | +}; |
| 155299 | + |
| 155300 | +struct StatPage { |
| 155301 | + u32 iPgno; |
| 155302 | + DbPage *pPg; |
| 155303 | + int iCell; |
| 155304 | + |
| 155305 | + char *zPath; /* Path to this page */ |
| 155306 | + |
| 155307 | + /* Variables populated by statDecodePage(): */ |
| 155308 | + u8 flags; /* Copy of flags byte */ |
| 155309 | + int nCell; /* Number of cells on page */ |
| 155310 | + int nUnused; /* Number of unused bytes on page */ |
| 155311 | + StatCell *aCell; /* Array of parsed cells */ |
| 155312 | + u32 iRightChildPg; /* Right-child page number (or 0) */ |
| 155313 | + int nMxPayload; /* Largest payload of any cell on this page */ |
| 155314 | +}; |
| 155315 | + |
| 155316 | +struct StatCursor { |
| 155317 | + sqlite3_vtab_cursor base; |
| 155318 | + sqlite3_stmt *pStmt; /* Iterates through set of root pages */ |
| 155319 | + int isEof; /* After pStmt has returned SQLITE_DONE */ |
| 155320 | + |
| 155321 | + StatPage aPage[32]; |
| 155322 | + int iPage; /* Current entry in aPage[] */ |
| 155323 | + |
| 155324 | + /* Values to return. */ |
| 155325 | + char *zName; /* Value of 'name' column */ |
| 155326 | + char *zPath; /* Value of 'path' column */ |
| 155327 | + u32 iPageno; /* Value of 'pageno' column */ |
| 155328 | + char *zPagetype; /* Value of 'pagetype' column */ |
| 155329 | + int nCell; /* Value of 'ncell' column */ |
| 155330 | + int nPayload; /* Value of 'payload' column */ |
| 155331 | + int nUnused; /* Value of 'unused' column */ |
| 155332 | + int nMxPayload; /* Value of 'mx_payload' column */ |
| 155333 | + i64 iOffset; /* Value of 'pgOffset' column */ |
| 155334 | + int szPage; /* Value of 'pgSize' column */ |
| 155335 | +}; |
| 155336 | + |
| 155337 | +struct StatTable { |
| 155338 | + sqlite3_vtab base; |
| 155339 | + sqlite3 *db; |
| 155340 | + int iDb; /* Index of database to analyze */ |
| 155341 | +}; |
| 155342 | + |
| 155343 | +#ifndef get2byte |
| 155344 | +# define get2byte(x) ((x)[0]<<8 | (x)[1]) |
| 155345 | +#endif |
| 155346 | + |
| 155347 | +/* |
| 155348 | +** Connect to or create a statvfs virtual table. |
| 155349 | +*/ |
| 155350 | +static int statConnect( |
| 155351 | + sqlite3 *db, |
| 155352 | + void *pAux, |
| 155353 | + int argc, const char *const*argv, |
| 155354 | + sqlite3_vtab **ppVtab, |
| 155355 | + char **pzErr |
| 155356 | +){ |
| 155357 | + StatTable *pTab = 0; |
| 155358 | + int rc = SQLITE_OK; |
| 155359 | + int iDb; |
| 155360 | + |
| 155361 | + if( argc>=4 ){ |
| 155362 | + iDb = sqlite3FindDbName(db, argv[3]); |
| 155363 | + if( iDb<0 ){ |
| 155364 | + *pzErr = sqlite3_mprintf("no such database: %s", argv[3]); |
| 155365 | + return SQLITE_ERROR; |
| 155366 | + } |
| 155367 | + }else{ |
| 155368 | + iDb = 0; |
| 155369 | + } |
| 155370 | + rc = sqlite3_declare_vtab(db, VTAB_SCHEMA); |
| 155371 | + if( rc==SQLITE_OK ){ |
| 155372 | + pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable)); |
| 155373 | + if( pTab==0 ) rc = SQLITE_NOMEM; |
| 155374 | + } |
| 155375 | + |
| 155376 | + assert( rc==SQLITE_OK || pTab==0 ); |
| 155377 | + if( rc==SQLITE_OK ){ |
| 155378 | + memset(pTab, 0, sizeof(StatTable)); |
| 155379 | + pTab->db = db; |
| 155380 | + pTab->iDb = iDb; |
| 155381 | + } |
| 155382 | + |
| 155383 | + *ppVtab = (sqlite3_vtab*)pTab; |
| 155384 | + return rc; |
| 155385 | +} |
| 155386 | + |
| 155387 | +/* |
| 155388 | +** Disconnect from or destroy a statvfs virtual table. |
| 155389 | +*/ |
| 155390 | +static int statDisconnect(sqlite3_vtab *pVtab){ |
| 155391 | + sqlite3_free(pVtab); |
| 155392 | + return SQLITE_OK; |
| 155393 | +} |
| 155394 | + |
| 155395 | +/* |
| 155396 | +** There is no "best-index". This virtual table always does a linear |
| 155397 | +** scan of the binary VFS log file. |
| 155398 | +*/ |
| 155399 | +static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 155400 | + |
| 155401 | + /* Records are always returned in ascending order of (name, path). |
| 155402 | + ** If this will satisfy the client, set the orderByConsumed flag so that |
| 155403 | + ** SQLite does not do an external sort. |
| 155404 | + */ |
| 155405 | + if( ( pIdxInfo->nOrderBy==1 |
| 155406 | + && pIdxInfo->aOrderBy[0].iColumn==0 |
| 155407 | + && pIdxInfo->aOrderBy[0].desc==0 |
| 155408 | + ) || |
| 155409 | + ( pIdxInfo->nOrderBy==2 |
| 155410 | + && pIdxInfo->aOrderBy[0].iColumn==0 |
| 155411 | + && pIdxInfo->aOrderBy[0].desc==0 |
| 155412 | + && pIdxInfo->aOrderBy[1].iColumn==1 |
| 155413 | + && pIdxInfo->aOrderBy[1].desc==0 |
| 155414 | + ) |
| 155415 | + ){ |
| 155416 | + pIdxInfo->orderByConsumed = 1; |
| 155417 | + } |
| 155418 | + |
| 155419 | + pIdxInfo->estimatedCost = 10.0; |
| 155420 | + return SQLITE_OK; |
| 155421 | +} |
| 155422 | + |
| 155423 | +/* |
| 155424 | +** Open a new statvfs cursor. |
| 155425 | +*/ |
| 155426 | +static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
| 155427 | + StatTable *pTab = (StatTable *)pVTab; |
| 155428 | + StatCursor *pCsr; |
| 155429 | + int rc; |
| 155430 | + |
| 155431 | + pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor)); |
| 155432 | + if( pCsr==0 ){ |
| 155433 | + rc = SQLITE_NOMEM; |
| 155434 | + }else{ |
| 155435 | + char *zSql; |
| 155436 | + memset(pCsr, 0, sizeof(StatCursor)); |
| 155437 | + pCsr->base.pVtab = pVTab; |
| 155438 | + |
| 155439 | + zSql = sqlite3_mprintf( |
| 155440 | + "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type" |
| 155441 | + " UNION ALL " |
| 155442 | + "SELECT name, rootpage, type" |
| 155443 | + " FROM \"%w\".sqlite_master WHERE rootpage!=0" |
| 155444 | + " ORDER BY name", pTab->db->aDb[pTab->iDb].zName); |
| 155445 | + if( zSql==0 ){ |
| 155446 | + rc = SQLITE_NOMEM; |
| 155447 | + }else{ |
| 155448 | + rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0); |
| 155449 | + sqlite3_free(zSql); |
| 155450 | + } |
| 155451 | + if( rc!=SQLITE_OK ){ |
| 155452 | + sqlite3_free(pCsr); |
| 155453 | + pCsr = 0; |
| 155454 | + } |
| 155455 | + } |
| 155456 | + |
| 155457 | + *ppCursor = (sqlite3_vtab_cursor *)pCsr; |
| 155458 | + return rc; |
| 155459 | +} |
| 155460 | + |
| 155461 | +static void statClearPage(StatPage *p){ |
| 155462 | + int i; |
| 155463 | + if( p->aCell ){ |
| 155464 | + for(i=0; i<p->nCell; i++){ |
| 155465 | + sqlite3_free(p->aCell[i].aOvfl); |
| 155466 | + } |
| 155467 | + sqlite3_free(p->aCell); |
| 155468 | + } |
| 155469 | + sqlite3PagerUnref(p->pPg); |
| 155470 | + sqlite3_free(p->zPath); |
| 155471 | + memset(p, 0, sizeof(StatPage)); |
| 155472 | +} |
| 155473 | + |
| 155474 | +static void statResetCsr(StatCursor *pCsr){ |
| 155475 | + int i; |
| 155476 | + sqlite3_reset(pCsr->pStmt); |
| 155477 | + for(i=0; i<ArraySize(pCsr->aPage); i++){ |
| 155478 | + statClearPage(&pCsr->aPage[i]); |
| 155479 | + } |
| 155480 | + pCsr->iPage = 0; |
| 155481 | + sqlite3_free(pCsr->zPath); |
| 155482 | + pCsr->zPath = 0; |
| 155483 | +} |
| 155484 | + |
| 155485 | +/* |
| 155486 | +** Close a statvfs cursor. |
| 155487 | +*/ |
| 155488 | +static int statClose(sqlite3_vtab_cursor *pCursor){ |
| 155489 | + StatCursor *pCsr = (StatCursor *)pCursor; |
| 155490 | + statResetCsr(pCsr); |
| 155491 | + sqlite3_finalize(pCsr->pStmt); |
| 155492 | + sqlite3_free(pCsr); |
| 155493 | + return SQLITE_OK; |
| 155494 | +} |
| 155495 | + |
| 155496 | +static void getLocalPayload( |
| 155497 | + int nUsable, /* Usable bytes per page */ |
| 155498 | + u8 flags, /* Page flags */ |
| 155499 | + int nTotal, /* Total record (payload) size */ |
| 155500 | + int *pnLocal /* OUT: Bytes stored locally */ |
| 155501 | +){ |
| 155502 | + int nLocal; |
| 155503 | + int nMinLocal; |
| 155504 | + int nMaxLocal; |
| 155505 | + |
| 155506 | + if( flags==0x0D ){ /* Table leaf node */ |
| 155507 | + nMinLocal = (nUsable - 12) * 32 / 255 - 23; |
| 155508 | + nMaxLocal = nUsable - 35; |
| 155509 | + }else{ /* Index interior and leaf nodes */ |
| 155510 | + nMinLocal = (nUsable - 12) * 32 / 255 - 23; |
| 155511 | + nMaxLocal = (nUsable - 12) * 64 / 255 - 23; |
| 155512 | + } |
| 155513 | + |
| 155514 | + nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4); |
| 155515 | + if( nLocal>nMaxLocal ) nLocal = nMinLocal; |
| 155516 | + *pnLocal = nLocal; |
| 155517 | +} |
| 155518 | + |
| 155519 | +static int statDecodePage(Btree *pBt, StatPage *p){ |
| 155520 | + int nUnused; |
| 155521 | + int iOff; |
| 155522 | + int nHdr; |
| 155523 | + int isLeaf; |
| 155524 | + int szPage; |
| 155525 | + |
| 155526 | + u8 *aData = sqlite3PagerGetData(p->pPg); |
| 155527 | + u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0]; |
| 155528 | + |
| 155529 | + p->flags = aHdr[0]; |
| 155530 | + p->nCell = get2byte(&aHdr[3]); |
| 155531 | + p->nMxPayload = 0; |
| 155532 | + |
| 155533 | + isLeaf = (p->flags==0x0A || p->flags==0x0D); |
| 155534 | + nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100; |
| 155535 | + |
| 155536 | + nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell; |
| 155537 | + nUnused += (int)aHdr[7]; |
| 155538 | + iOff = get2byte(&aHdr[1]); |
| 155539 | + while( iOff ){ |
| 155540 | + nUnused += get2byte(&aData[iOff+2]); |
| 155541 | + iOff = get2byte(&aData[iOff]); |
| 155542 | + } |
| 155543 | + p->nUnused = nUnused; |
| 155544 | + p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]); |
| 155545 | + szPage = sqlite3BtreeGetPageSize(pBt); |
| 155546 | + |
| 155547 | + if( p->nCell ){ |
| 155548 | + int i; /* Used to iterate through cells */ |
| 155549 | + int nUsable; /* Usable bytes per page */ |
| 155550 | + |
| 155551 | + sqlite3BtreeEnter(pBt); |
| 155552 | + nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt); |
| 155553 | + sqlite3BtreeLeave(pBt); |
| 155554 | + p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell)); |
| 155555 | + if( p->aCell==0 ) return SQLITE_NOMEM; |
| 155556 | + memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell)); |
| 155557 | + |
| 155558 | + for(i=0; i<p->nCell; i++){ |
| 155559 | + StatCell *pCell = &p->aCell[i]; |
| 155560 | + |
| 155561 | + iOff = get2byte(&aData[nHdr+i*2]); |
| 155562 | + if( !isLeaf ){ |
| 155563 | + pCell->iChildPg = sqlite3Get4byte(&aData[iOff]); |
| 155564 | + iOff += 4; |
| 155565 | + } |
| 155566 | + if( p->flags==0x05 ){ |
| 155567 | + /* A table interior node. nPayload==0. */ |
| 155568 | + }else{ |
| 155569 | + u32 nPayload; /* Bytes of payload total (local+overflow) */ |
| 155570 | + int nLocal; /* Bytes of payload stored locally */ |
| 155571 | + iOff += getVarint32(&aData[iOff], nPayload); |
| 155572 | + if( p->flags==0x0D ){ |
| 155573 | + u64 dummy; |
| 155574 | + iOff += sqlite3GetVarint(&aData[iOff], &dummy); |
| 155575 | + } |
| 155576 | + if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload; |
| 155577 | + getLocalPayload(nUsable, p->flags, nPayload, &nLocal); |
| 155578 | + pCell->nLocal = nLocal; |
| 155579 | + assert( nLocal>=0 ); |
| 155580 | + assert( nPayload>=(u32)nLocal ); |
| 155581 | + assert( nLocal<=(nUsable-35) ); |
| 155582 | + if( nPayload>(u32)nLocal ){ |
| 155583 | + int j; |
| 155584 | + int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4); |
| 155585 | + pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4); |
| 155586 | + pCell->nOvfl = nOvfl; |
| 155587 | + pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl); |
| 155588 | + if( pCell->aOvfl==0 ) return SQLITE_NOMEM; |
| 155589 | + pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]); |
| 155590 | + for(j=1; j<nOvfl; j++){ |
| 155591 | + int rc; |
| 155592 | + u32 iPrev = pCell->aOvfl[j-1]; |
| 155593 | + DbPage *pPg = 0; |
| 155594 | + rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg); |
| 155595 | + if( rc!=SQLITE_OK ){ |
| 155596 | + assert( pPg==0 ); |
| 155597 | + return rc; |
| 155598 | + } |
| 155599 | + pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg)); |
| 155600 | + sqlite3PagerUnref(pPg); |
| 155601 | + } |
| 155602 | + } |
| 155603 | + } |
| 155604 | + } |
| 155605 | + } |
| 155606 | + |
| 155607 | + return SQLITE_OK; |
| 155608 | +} |
| 155609 | + |
| 155610 | +/* |
| 155611 | +** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on |
| 155612 | +** the current value of pCsr->iPageno. |
| 155613 | +*/ |
| 155614 | +static void statSizeAndOffset(StatCursor *pCsr){ |
| 155615 | + StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab; |
| 155616 | + Btree *pBt = pTab->db->aDb[pTab->iDb].pBt; |
| 155617 | + Pager *pPager = sqlite3BtreePager(pBt); |
| 155618 | + sqlite3_file *fd; |
| 155619 | + sqlite3_int64 x[2]; |
| 155620 | + |
| 155621 | + /* The default page size and offset */ |
| 155622 | + pCsr->szPage = sqlite3BtreeGetPageSize(pBt); |
| 155623 | + pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1); |
| 155624 | + |
| 155625 | + /* If connected to a ZIPVFS backend, override the page size and |
| 155626 | + ** offset with actual values obtained from ZIPVFS. |
| 155627 | + */ |
| 155628 | + fd = sqlite3PagerFile(pPager); |
| 155629 | + x[0] = pCsr->iPageno; |
| 155630 | + if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){ |
| 155631 | + pCsr->iOffset = x[0]; |
| 155632 | + pCsr->szPage = (int)x[1]; |
| 155633 | + } |
| 155634 | +} |
| 155635 | + |
| 155636 | +/* |
| 155637 | +** Move a statvfs cursor to the next entry in the file. |
| 155638 | +*/ |
| 155639 | +static int statNext(sqlite3_vtab_cursor *pCursor){ |
| 155640 | + int rc; |
| 155641 | + int nPayload; |
| 155642 | + char *z; |
| 155643 | + StatCursor *pCsr = (StatCursor *)pCursor; |
| 155644 | + StatTable *pTab = (StatTable *)pCursor->pVtab; |
| 155645 | + Btree *pBt = pTab->db->aDb[pTab->iDb].pBt; |
| 155646 | + Pager *pPager = sqlite3BtreePager(pBt); |
| 155647 | + |
| 155648 | + sqlite3_free(pCsr->zPath); |
| 155649 | + pCsr->zPath = 0; |
| 155650 | + |
| 155651 | +statNextRestart: |
| 155652 | + if( pCsr->aPage[0].pPg==0 ){ |
| 155653 | + rc = sqlite3_step(pCsr->pStmt); |
| 155654 | + if( rc==SQLITE_ROW ){ |
| 155655 | + int nPage; |
| 155656 | + u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1); |
| 155657 | + sqlite3PagerPagecount(pPager, &nPage); |
| 155658 | + if( nPage==0 ){ |
| 155659 | + pCsr->isEof = 1; |
| 155660 | + return sqlite3_reset(pCsr->pStmt); |
| 155661 | + } |
| 155662 | + rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg); |
| 155663 | + pCsr->aPage[0].iPgno = iRoot; |
| 155664 | + pCsr->aPage[0].iCell = 0; |
| 155665 | + pCsr->aPage[0].zPath = z = sqlite3_mprintf("/"); |
| 155666 | + pCsr->iPage = 0; |
| 155667 | + if( z==0 ) rc = SQLITE_NOMEM; |
| 155668 | + }else{ |
| 155669 | + pCsr->isEof = 1; |
| 155670 | + return sqlite3_reset(pCsr->pStmt); |
| 155671 | + } |
| 155672 | + }else{ |
| 155673 | + |
| 155674 | + /* Page p itself has already been visited. */ |
| 155675 | + StatPage *p = &pCsr->aPage[pCsr->iPage]; |
| 155676 | + |
| 155677 | + while( p->iCell<p->nCell ){ |
| 155678 | + StatCell *pCell = &p->aCell[p->iCell]; |
| 155679 | + if( pCell->iOvfl<pCell->nOvfl ){ |
| 155680 | + int nUsable; |
| 155681 | + sqlite3BtreeEnter(pBt); |
| 155682 | + nUsable = sqlite3BtreeGetPageSize(pBt) - |
| 155683 | + sqlite3BtreeGetReserveNoMutex(pBt); |
| 155684 | + sqlite3BtreeLeave(pBt); |
| 155685 | + pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); |
| 155686 | + pCsr->iPageno = pCell->aOvfl[pCell->iOvfl]; |
| 155687 | + pCsr->zPagetype = "overflow"; |
| 155688 | + pCsr->nCell = 0; |
| 155689 | + pCsr->nMxPayload = 0; |
| 155690 | + pCsr->zPath = z = sqlite3_mprintf( |
| 155691 | + "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl |
| 155692 | + ); |
| 155693 | + if( pCell->iOvfl<pCell->nOvfl-1 ){ |
| 155694 | + pCsr->nUnused = 0; |
| 155695 | + pCsr->nPayload = nUsable - 4; |
| 155696 | + }else{ |
| 155697 | + pCsr->nPayload = pCell->nLastOvfl; |
| 155698 | + pCsr->nUnused = nUsable - 4 - pCsr->nPayload; |
| 155699 | + } |
| 155700 | + pCell->iOvfl++; |
| 155701 | + statSizeAndOffset(pCsr); |
| 155702 | + return z==0 ? SQLITE_NOMEM : SQLITE_OK; |
| 155703 | + } |
| 155704 | + if( p->iRightChildPg ) break; |
| 155705 | + p->iCell++; |
| 155706 | + } |
| 155707 | + |
| 155708 | + if( !p->iRightChildPg || p->iCell>p->nCell ){ |
| 155709 | + statClearPage(p); |
| 155710 | + if( pCsr->iPage==0 ) return statNext(pCursor); |
| 155711 | + pCsr->iPage--; |
| 155712 | + goto statNextRestart; /* Tail recursion */ |
| 155713 | + } |
| 155714 | + pCsr->iPage++; |
| 155715 | + assert( p==&pCsr->aPage[pCsr->iPage-1] ); |
| 155716 | + |
| 155717 | + if( p->iCell==p->nCell ){ |
| 155718 | + p[1].iPgno = p->iRightChildPg; |
| 155719 | + }else{ |
| 155720 | + p[1].iPgno = p->aCell[p->iCell].iChildPg; |
| 155721 | + } |
| 155722 | + rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg); |
| 155723 | + p[1].iCell = 0; |
| 155724 | + p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell); |
| 155725 | + p->iCell++; |
| 155726 | + if( z==0 ) rc = SQLITE_NOMEM; |
| 155727 | + } |
| 155728 | + |
| 155729 | + |
| 155730 | + /* Populate the StatCursor fields with the values to be returned |
| 155731 | + ** by the xColumn() and xRowid() methods. |
| 155732 | + */ |
| 155733 | + if( rc==SQLITE_OK ){ |
| 155734 | + int i; |
| 155735 | + StatPage *p = &pCsr->aPage[pCsr->iPage]; |
| 155736 | + pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); |
| 155737 | + pCsr->iPageno = p->iPgno; |
| 155738 | + |
| 155739 | + rc = statDecodePage(pBt, p); |
| 155740 | + if( rc==SQLITE_OK ){ |
| 155741 | + statSizeAndOffset(pCsr); |
| 155742 | + |
| 155743 | + switch( p->flags ){ |
| 155744 | + case 0x05: /* table internal */ |
| 155745 | + case 0x02: /* index internal */ |
| 155746 | + pCsr->zPagetype = "internal"; |
| 155747 | + break; |
| 155748 | + case 0x0D: /* table leaf */ |
| 155749 | + case 0x0A: /* index leaf */ |
| 155750 | + pCsr->zPagetype = "leaf"; |
| 155751 | + break; |
| 155752 | + default: |
| 155753 | + pCsr->zPagetype = "corrupted"; |
| 155754 | + break; |
| 155755 | + } |
| 155756 | + pCsr->nCell = p->nCell; |
| 155757 | + pCsr->nUnused = p->nUnused; |
| 155758 | + pCsr->nMxPayload = p->nMxPayload; |
| 155759 | + pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath); |
| 155760 | + if( z==0 ) rc = SQLITE_NOMEM; |
| 155761 | + nPayload = 0; |
| 155762 | + for(i=0; i<p->nCell; i++){ |
| 155763 | + nPayload += p->aCell[i].nLocal; |
| 155764 | + } |
| 155765 | + pCsr->nPayload = nPayload; |
| 155766 | + } |
| 155767 | + } |
| 155768 | + |
| 155769 | + return rc; |
| 155770 | +} |
| 155771 | + |
| 155772 | +static int statEof(sqlite3_vtab_cursor *pCursor){ |
| 155773 | + StatCursor *pCsr = (StatCursor *)pCursor; |
| 155774 | + return pCsr->isEof; |
| 155775 | +} |
| 155776 | + |
| 155777 | +static int statFilter( |
| 155778 | + sqlite3_vtab_cursor *pCursor, |
| 155779 | + int idxNum, const char *idxStr, |
| 155780 | + int argc, sqlite3_value **argv |
| 155781 | +){ |
| 155782 | + StatCursor *pCsr = (StatCursor *)pCursor; |
| 155783 | + |
| 155784 | + statResetCsr(pCsr); |
| 155785 | + return statNext(pCursor); |
| 155786 | +} |
| 155787 | + |
| 155788 | +static int statColumn( |
| 155789 | + sqlite3_vtab_cursor *pCursor, |
| 155790 | + sqlite3_context *ctx, |
| 155791 | + int i |
| 155792 | +){ |
| 155793 | + StatCursor *pCsr = (StatCursor *)pCursor; |
| 155794 | + switch( i ){ |
| 155795 | + case 0: /* name */ |
| 155796 | + sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT); |
| 155797 | + break; |
| 155798 | + case 1: /* path */ |
| 155799 | + sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT); |
| 155800 | + break; |
| 155801 | + case 2: /* pageno */ |
| 155802 | + sqlite3_result_int64(ctx, pCsr->iPageno); |
| 155803 | + break; |
| 155804 | + case 3: /* pagetype */ |
| 155805 | + sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC); |
| 155806 | + break; |
| 155807 | + case 4: /* ncell */ |
| 155808 | + sqlite3_result_int(ctx, pCsr->nCell); |
| 155809 | + break; |
| 155810 | + case 5: /* payload */ |
| 155811 | + sqlite3_result_int(ctx, pCsr->nPayload); |
| 155812 | + break; |
| 155813 | + case 6: /* unused */ |
| 155814 | + sqlite3_result_int(ctx, pCsr->nUnused); |
| 155815 | + break; |
| 155816 | + case 7: /* mx_payload */ |
| 155817 | + sqlite3_result_int(ctx, pCsr->nMxPayload); |
| 155818 | + break; |
| 155819 | + case 8: /* pgoffset */ |
| 155820 | + sqlite3_result_int64(ctx, pCsr->iOffset); |
| 155821 | + break; |
| 155822 | + default: /* pgsize */ |
| 155823 | + assert( i==9 ); |
| 155824 | + sqlite3_result_int(ctx, pCsr->szPage); |
| 155825 | + break; |
| 155826 | + } |
| 155827 | + return SQLITE_OK; |
| 155828 | +} |
| 155829 | + |
| 155830 | +static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ |
| 155831 | + StatCursor *pCsr = (StatCursor *)pCursor; |
| 155832 | + *pRowid = pCsr->iPageno; |
| 155833 | + return SQLITE_OK; |
| 155834 | +} |
| 155835 | + |
| 155836 | +/* |
| 155837 | +** Invoke this routine to register the "dbstat" virtual table module |
| 155838 | +*/ |
| 155839 | +SQLITE_API int SQLITE_STDCALL sqlite3_dbstat_register(sqlite3 *db){ |
| 155840 | + static sqlite3_module dbstat_module = { |
| 155841 | + 0, /* iVersion */ |
| 155842 | + statConnect, /* xCreate */ |
| 155843 | + statConnect, /* xConnect */ |
| 155844 | + statBestIndex, /* xBestIndex */ |
| 155845 | + statDisconnect, /* xDisconnect */ |
| 155846 | + statDisconnect, /* xDestroy */ |
| 155847 | + statOpen, /* xOpen - open a cursor */ |
| 155848 | + statClose, /* xClose - close a cursor */ |
| 155849 | + statFilter, /* xFilter - configure scan constraints */ |
| 155850 | + statNext, /* xNext - advance a cursor */ |
| 155851 | + statEof, /* xEof - check for end of scan */ |
| 155852 | + statColumn, /* xColumn - read data */ |
| 155853 | + statRowid, /* xRowid - read data */ |
| 155854 | + 0, /* xUpdate */ |
| 155855 | + 0, /* xBegin */ |
| 155856 | + 0, /* xSync */ |
| 155857 | + 0, /* xCommit */ |
| 155858 | + 0, /* xRollback */ |
| 155859 | + 0, /* xFindMethod */ |
| 155860 | + 0, /* xRename */ |
| 155861 | + }; |
| 155862 | + return sqlite3_create_module(db, "dbstat", &dbstat_module, 0); |
| 155863 | +} |
| 155864 | +#endif /* SQLITE_ENABLE_DBSTAT_VTAB */ |
| 155865 | + |
| 155866 | +/************** End of dbstat.c **********************************************/ |
| 154634 | 155867 | |