sqlite3.h revision 322444
1251883Speter/* 2322444Speter** 2001-09-15 3251883Speter** 4251883Speter** The author disclaims copyright to this source code. In place of 5251883Speter** a legal notice, here is a blessing: 6251883Speter** 7251883Speter** May you do good and not evil. 8251883Speter** May you find forgiveness for yourself and forgive others. 9251883Speter** May you share freely, never taking more than you give. 10251883Speter** 11251883Speter************************************************************************* 12251883Speter** This header file defines the interface that the SQLite library 13251883Speter** presents to client programs. If a C-function, structure, datatype, 14251883Speter** or constant definition does not appear in this file, then it is 15251883Speter** not a published API of SQLite, is subject to change without 16251883Speter** notice, and should not be referenced by programs that use SQLite. 17251883Speter** 18251883Speter** Some of the definitions that are in this file are marked as 19251883Speter** "experimental". Experimental interfaces are normally new 20251883Speter** features recently added to SQLite. We do not anticipate changes 21251883Speter** to experimental interfaces but reserve the right to make minor changes 22251883Speter** if experience from use "in the wild" suggest such changes are prudent. 23251883Speter** 24251883Speter** The official C-language API documentation for SQLite is derived 25251883Speter** from comments in this file. This file is the authoritative source 26286510Speter** on how SQLite interfaces are supposed to operate. 27251883Speter** 28251883Speter** The name of this file under configuration management is "sqlite.h.in". 29251883Speter** The makefile makes some minor changes to this file (such as inserting 30251883Speter** the version number) and changes its name to "sqlite3.h" as 31251883Speter** part of the build process. 32251883Speter*/ 33305002Scy#ifndef SQLITE3_H 34305002Scy#define SQLITE3_H 35251883Speter#include <stdarg.h> /* Needed for the definition of va_list */ 36251883Speter 37251883Speter/* 38251883Speter** Make sure we can call this stuff from C++. 39251883Speter*/ 40251883Speter#ifdef __cplusplus 41251883Speterextern "C" { 42251883Speter#endif 43251883Speter 44251883Speter 45251883Speter/* 46282328Sbapt** Provide the ability to override linkage features of the interface. 47251883Speter*/ 48251883Speter#ifndef SQLITE_EXTERN 49251883Speter# define SQLITE_EXTERN extern 50251883Speter#endif 51251883Speter#ifndef SQLITE_API 52251883Speter# define SQLITE_API 53251883Speter#endif 54282328Sbapt#ifndef SQLITE_CDECL 55282328Sbapt# define SQLITE_CDECL 56282328Sbapt#endif 57305002Scy#ifndef SQLITE_APICALL 58305002Scy# define SQLITE_APICALL 59305002Scy#endif 60282328Sbapt#ifndef SQLITE_STDCALL 61305002Scy# define SQLITE_STDCALL SQLITE_APICALL 62282328Sbapt#endif 63305002Scy#ifndef SQLITE_CALLBACK 64305002Scy# define SQLITE_CALLBACK 65305002Scy#endif 66305002Scy#ifndef SQLITE_SYSAPI 67305002Scy# define SQLITE_SYSAPI 68305002Scy#endif 69251883Speter 70251883Speter/* 71251883Speter** These no-op macros are used in front of interfaces to mark those 72251883Speter** interfaces as either deprecated or experimental. New applications 73282328Sbapt** should not use deprecated interfaces - they are supported for backwards 74251883Speter** compatibility only. Application writers should be aware that 75251883Speter** experimental interfaces are subject to change in point releases. 76251883Speter** 77251883Speter** These macros used to resolve to various kinds of compiler magic that 78251883Speter** would generate warning messages when they were used. But that 79251883Speter** compiler magic ended up generating such a flurry of bug reports 80251883Speter** that we have taken it all out and gone back to using simple 81251883Speter** noop macros. 82251883Speter*/ 83251883Speter#define SQLITE_DEPRECATED 84251883Speter#define SQLITE_EXPERIMENTAL 85251883Speter 86251883Speter/* 87251883Speter** Ensure these symbols were not defined by some previous header file. 88251883Speter*/ 89251883Speter#ifdef SQLITE_VERSION 90251883Speter# undef SQLITE_VERSION 91251883Speter#endif 92251883Speter#ifdef SQLITE_VERSION_NUMBER 93251883Speter# undef SQLITE_VERSION_NUMBER 94251883Speter#endif 95251883Speter 96251883Speter/* 97251883Speter** CAPI3REF: Compile-Time Library Version Numbers 98251883Speter** 99251883Speter** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header 100251883Speter** evaluates to a string literal that is the SQLite version in the 101251883Speter** format "X.Y.Z" where X is the major version number (always 3 for 102251883Speter** SQLite3) and Y is the minor version number and Z is the release number.)^ 103251883Speter** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer 104251883Speter** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same 105251883Speter** numbers used in [SQLITE_VERSION].)^ 106251883Speter** The SQLITE_VERSION_NUMBER for any given release of SQLite will also 107251883Speter** be larger than the release from which it is derived. Either Y will 108251883Speter** be held constant and Z will be incremented or else Y will be incremented 109251883Speter** and Z will be reset to zero. 110251883Speter** 111322444Speter** Since [version 3.6.18] ([dateof:3.6.18]), 112322444Speter** SQLite source code has been stored in the 113251883Speter** <a href="http://www.fossil-scm.org/">Fossil configuration management 114251883Speter** system</a>. ^The SQLITE_SOURCE_ID macro evaluates to 115251883Speter** a string which identifies a particular check-in of SQLite 116251883Speter** within its configuration management system. ^The SQLITE_SOURCE_ID 117322444Speter** string contains the date and time of the check-in (UTC) and a SHA1 118322444Speter** or SHA3-256 hash of the entire source tree. 119251883Speter** 120251883Speter** See also: [sqlite3_libversion()], 121251883Speter** [sqlite3_libversion_number()], [sqlite3_sourceid()], 122251883Speter** [sqlite_version()] and [sqlite_source_id()]. 123251883Speter*/ 124322444Speter#define SQLITE_VERSION "3.20.0" 125322444Speter#define SQLITE_VERSION_NUMBER 3020000 126322444Speter#define SQLITE_SOURCE_ID "2017-08-01 13:24:15 9501e22dfeebdcefa783575e47c60b514d7c2e0cad73b2a496c0bc4b680900a8" 127251883Speter 128251883Speter/* 129251883Speter** CAPI3REF: Run-Time Library Version Numbers 130322444Speter** KEYWORDS: sqlite3_version sqlite3_sourceid 131251883Speter** 132251883Speter** These interfaces provide the same information as the [SQLITE_VERSION], 133251883Speter** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros 134251883Speter** but are associated with the library instead of the header file. ^(Cautious 135251883Speter** programmers might include assert() statements in their application to 136251883Speter** verify that values returned by these interfaces match the macros in 137298161Sbapt** the header, and thus ensure that the application is 138251883Speter** compiled with matching library and header files. 139251883Speter** 140251883Speter** <blockquote><pre> 141251883Speter** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER ); 142251883Speter** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 ); 143251883Speter** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 ); 144251883Speter** </pre></blockquote>)^ 145251883Speter** 146251883Speter** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION] 147251883Speter** macro. ^The sqlite3_libversion() function returns a pointer to the 148251883Speter** to the sqlite3_version[] string constant. The sqlite3_libversion() 149251883Speter** function is provided for use in DLLs since DLL users usually do not have 150251883Speter** direct access to string constants within the DLL. ^The 151251883Speter** sqlite3_libversion_number() function returns an integer equal to 152251883Speter** [SQLITE_VERSION_NUMBER]. ^The sqlite3_sourceid() function returns 153251883Speter** a pointer to a string constant whose value is the same as the 154251883Speter** [SQLITE_SOURCE_ID] C preprocessor macro. 155251883Speter** 156251883Speter** See also: [sqlite_version()] and [sqlite_source_id()]. 157251883Speter*/ 158251883SpeterSQLITE_API SQLITE_EXTERN const char sqlite3_version[]; 159322444SpeterSQLITE_API const char *sqlite3_libversion(void); 160322444SpeterSQLITE_API const char *sqlite3_sourceid(void); 161322444SpeterSQLITE_API int sqlite3_libversion_number(void); 162251883Speter 163251883Speter/* 164251883Speter** CAPI3REF: Run-Time Library Compilation Options Diagnostics 165251883Speter** 166251883Speter** ^The sqlite3_compileoption_used() function returns 0 or 1 167251883Speter** indicating whether the specified option was defined at 168251883Speter** compile time. ^The SQLITE_ prefix may be omitted from the 169251883Speter** option name passed to sqlite3_compileoption_used(). 170251883Speter** 171251883Speter** ^The sqlite3_compileoption_get() function allows iterating 172251883Speter** over the list of options that were defined at compile time by 173251883Speter** returning the N-th compile time option string. ^If N is out of range, 174251883Speter** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_ 175251883Speter** prefix is omitted from any strings returned by 176251883Speter** sqlite3_compileoption_get(). 177251883Speter** 178251883Speter** ^Support for the diagnostic functions sqlite3_compileoption_used() 179251883Speter** and sqlite3_compileoption_get() may be omitted by specifying the 180251883Speter** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time. 181251883Speter** 182251883Speter** See also: SQL functions [sqlite_compileoption_used()] and 183251883Speter** [sqlite_compileoption_get()] and the [compile_options pragma]. 184251883Speter*/ 185251883Speter#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 186322444SpeterSQLITE_API int sqlite3_compileoption_used(const char *zOptName); 187322444SpeterSQLITE_API const char *sqlite3_compileoption_get(int N); 188251883Speter#endif 189251883Speter 190251883Speter/* 191251883Speter** CAPI3REF: Test To See If The Library Is Threadsafe 192251883Speter** 193251883Speter** ^The sqlite3_threadsafe() function returns zero if and only if 194251883Speter** SQLite was compiled with mutexing code omitted due to the 195251883Speter** [SQLITE_THREADSAFE] compile-time option being set to 0. 196251883Speter** 197251883Speter** SQLite can be compiled with or without mutexes. When 198251883Speter** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes 199251883Speter** are enabled and SQLite is threadsafe. When the 200251883Speter** [SQLITE_THREADSAFE] macro is 0, 201251883Speter** the mutexes are omitted. Without the mutexes, it is not safe 202251883Speter** to use SQLite concurrently from more than one thread. 203251883Speter** 204251883Speter** Enabling mutexes incurs a measurable performance penalty. 205251883Speter** So if speed is of utmost importance, it makes sense to disable 206251883Speter** the mutexes. But for maximum safety, mutexes should be enabled. 207251883Speter** ^The default behavior is for mutexes to be enabled. 208251883Speter** 209251883Speter** This interface can be used by an application to make sure that the 210251883Speter** version of SQLite that it is linking against was compiled with 211251883Speter** the desired setting of the [SQLITE_THREADSAFE] macro. 212251883Speter** 213251883Speter** This interface only reports on the compile-time mutex setting 214251883Speter** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with 215251883Speter** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but 216251883Speter** can be fully or partially disabled using a call to [sqlite3_config()] 217251883Speter** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD], 218282328Sbapt** or [SQLITE_CONFIG_SERIALIZED]. ^(The return value of the 219251883Speter** sqlite3_threadsafe() function shows only the compile-time setting of 220251883Speter** thread safety, not any run-time changes to that setting made by 221251883Speter** sqlite3_config(). In other words, the return value from sqlite3_threadsafe() 222251883Speter** is unchanged by calls to sqlite3_config().)^ 223251883Speter** 224251883Speter** See the [threading mode] documentation for additional information. 225251883Speter*/ 226322444SpeterSQLITE_API int sqlite3_threadsafe(void); 227251883Speter 228251883Speter/* 229251883Speter** CAPI3REF: Database Connection Handle 230251883Speter** KEYWORDS: {database connection} {database connections} 231251883Speter** 232251883Speter** Each open SQLite database is represented by a pointer to an instance of 233251883Speter** the opaque structure named "sqlite3". It is useful to think of an sqlite3 234251883Speter** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and 235251883Speter** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()] 236251883Speter** and [sqlite3_close_v2()] are its destructors. There are many other 237251883Speter** interfaces (such as 238251883Speter** [sqlite3_prepare_v2()], [sqlite3_create_function()], and 239251883Speter** [sqlite3_busy_timeout()] to name but three) that are methods on an 240251883Speter** sqlite3 object. 241251883Speter*/ 242251883Spetertypedef struct sqlite3 sqlite3; 243251883Speter 244251883Speter/* 245251883Speter** CAPI3REF: 64-Bit Integer Types 246251883Speter** KEYWORDS: sqlite_int64 sqlite_uint64 247251883Speter** 248251883Speter** Because there is no cross-platform way to specify 64-bit integer types 249251883Speter** SQLite includes typedefs for 64-bit signed and unsigned integers. 250251883Speter** 251251883Speter** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions. 252251883Speter** The sqlite_int64 and sqlite_uint64 types are supported for backwards 253251883Speter** compatibility only. 254251883Speter** 255251883Speter** ^The sqlite3_int64 and sqlite_int64 types can store integer values 256251883Speter** between -9223372036854775808 and +9223372036854775807 inclusive. ^The 257251883Speter** sqlite3_uint64 and sqlite_uint64 types can store integer values 258251883Speter** between 0 and +18446744073709551615 inclusive. 259251883Speter*/ 260251883Speter#ifdef SQLITE_INT64_TYPE 261251883Speter typedef SQLITE_INT64_TYPE sqlite_int64; 262322444Speter# ifdef SQLITE_UINT64_TYPE 263322444Speter typedef SQLITE_UINT64_TYPE sqlite_uint64; 264322444Speter# else 265322444Speter typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; 266322444Speter# endif 267251883Speter#elif defined(_MSC_VER) || defined(__BORLANDC__) 268251883Speter typedef __int64 sqlite_int64; 269251883Speter typedef unsigned __int64 sqlite_uint64; 270251883Speter#else 271251883Speter typedef long long int sqlite_int64; 272251883Speter typedef unsigned long long int sqlite_uint64; 273251883Speter#endif 274251883Spetertypedef sqlite_int64 sqlite3_int64; 275251883Spetertypedef sqlite_uint64 sqlite3_uint64; 276251883Speter 277251883Speter/* 278251883Speter** If compiling for a processor that lacks floating point support, 279251883Speter** substitute integer for floating-point. 280251883Speter*/ 281251883Speter#ifdef SQLITE_OMIT_FLOATING_POINT 282251883Speter# define double sqlite3_int64 283251883Speter#endif 284251883Speter 285251883Speter/* 286251883Speter** CAPI3REF: Closing A Database Connection 287286510Speter** DESTRUCTOR: sqlite3 288251883Speter** 289251883Speter** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors 290251883Speter** for the [sqlite3] object. 291274884Sbapt** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if 292251883Speter** the [sqlite3] object is successfully destroyed and all associated 293251883Speter** resources are deallocated. 294251883Speter** 295251883Speter** ^If the database connection is associated with unfinalized prepared 296251883Speter** statements or unfinished sqlite3_backup objects then sqlite3_close() 297251883Speter** will leave the database connection open and return [SQLITE_BUSY]. 298251883Speter** ^If sqlite3_close_v2() is called with unfinalized prepared statements 299274884Sbapt** and/or unfinished sqlite3_backups, then the database connection becomes 300251883Speter** an unusable "zombie" which will automatically be deallocated when the 301251883Speter** last prepared statement is finalized or the last sqlite3_backup is 302251883Speter** finished. The sqlite3_close_v2() interface is intended for use with 303251883Speter** host languages that are garbage collected, and where the order in which 304251883Speter** destructors are called is arbitrary. 305251883Speter** 306251883Speter** Applications should [sqlite3_finalize | finalize] all [prepared statements], 307251883Speter** [sqlite3_blob_close | close] all [BLOB handles], and 308251883Speter** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated 309251883Speter** with the [sqlite3] object prior to attempting to close the object. ^If 310251883Speter** sqlite3_close_v2() is called on a [database connection] that still has 311251883Speter** outstanding [prepared statements], [BLOB handles], and/or 312274884Sbapt** [sqlite3_backup] objects then it returns [SQLITE_OK] and the deallocation 313251883Speter** of resources is deferred until all [prepared statements], [BLOB handles], 314251883Speter** and [sqlite3_backup] objects are also destroyed. 315251883Speter** 316251883Speter** ^If an [sqlite3] object is destroyed while a transaction is open, 317251883Speter** the transaction is automatically rolled back. 318251883Speter** 319251883Speter** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)] 320251883Speter** must be either a NULL 321251883Speter** pointer or an [sqlite3] object pointer obtained 322251883Speter** from [sqlite3_open()], [sqlite3_open16()], or 323251883Speter** [sqlite3_open_v2()], and not previously closed. 324251883Speter** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer 325251883Speter** argument is a harmless no-op. 326251883Speter*/ 327322444SpeterSQLITE_API int sqlite3_close(sqlite3*); 328322444SpeterSQLITE_API int sqlite3_close_v2(sqlite3*); 329251883Speter 330251883Speter/* 331251883Speter** The type for a callback function. 332251883Speter** This is legacy and deprecated. It is included for historical 333251883Speter** compatibility and is not documented. 334251883Speter*/ 335251883Spetertypedef int (*sqlite3_callback)(void*,int,char**, char**); 336251883Speter 337251883Speter/* 338251883Speter** CAPI3REF: One-Step Query Execution Interface 339286510Speter** METHOD: sqlite3 340251883Speter** 341251883Speter** The sqlite3_exec() interface is a convenience wrapper around 342251883Speter** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()], 343251883Speter** that allows an application to run multiple statements of SQL 344251883Speter** without having to use a lot of C code. 345251883Speter** 346251883Speter** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded, 347251883Speter** semicolon-separate SQL statements passed into its 2nd argument, 348251883Speter** in the context of the [database connection] passed in as its 1st 349251883Speter** argument. ^If the callback function of the 3rd argument to 350251883Speter** sqlite3_exec() is not NULL, then it is invoked for each result row 351251883Speter** coming out of the evaluated SQL statements. ^The 4th argument to 352251883Speter** sqlite3_exec() is relayed through to the 1st argument of each 353251883Speter** callback invocation. ^If the callback pointer to sqlite3_exec() 354251883Speter** is NULL, then no callback is ever invoked and result rows are 355251883Speter** ignored. 356251883Speter** 357251883Speter** ^If an error occurs while evaluating the SQL statements passed into 358251883Speter** sqlite3_exec(), then execution of the current statement stops and 359251883Speter** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec() 360251883Speter** is not NULL then any error message is written into memory obtained 361251883Speter** from [sqlite3_malloc()] and passed back through the 5th parameter. 362251883Speter** To avoid memory leaks, the application should invoke [sqlite3_free()] 363251883Speter** on error message strings returned through the 5th parameter of 364298161Sbapt** sqlite3_exec() after the error message string is no longer needed. 365251883Speter** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors 366251883Speter** occur, then sqlite3_exec() sets the pointer in its 5th parameter to 367251883Speter** NULL before returning. 368251883Speter** 369251883Speter** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec() 370251883Speter** routine returns SQLITE_ABORT without invoking the callback again and 371251883Speter** without running any subsequent SQL statements. 372251883Speter** 373251883Speter** ^The 2nd argument to the sqlite3_exec() callback function is the 374251883Speter** number of columns in the result. ^The 3rd argument to the sqlite3_exec() 375251883Speter** callback is an array of pointers to strings obtained as if from 376251883Speter** [sqlite3_column_text()], one for each column. ^If an element of a 377251883Speter** result row is NULL then the corresponding string pointer for the 378251883Speter** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the 379251883Speter** sqlite3_exec() callback is an array of pointers to strings where each 380251883Speter** entry represents the name of corresponding result column as obtained 381251883Speter** from [sqlite3_column_name()]. 382251883Speter** 383251883Speter** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer 384251883Speter** to an empty string, or a pointer that contains only whitespace and/or 385251883Speter** SQL comments, then no SQL statements are evaluated and the database 386251883Speter** is not changed. 387251883Speter** 388251883Speter** Restrictions: 389251883Speter** 390251883Speter** <ul> 391298161Sbapt** <li> The application must ensure that the 1st parameter to sqlite3_exec() 392251883Speter** is a valid and open [database connection]. 393269851Speter** <li> The application must not close the [database connection] specified by 394251883Speter** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running. 395251883Speter** <li> The application must not modify the SQL statement text passed into 396251883Speter** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running. 397251883Speter** </ul> 398251883Speter*/ 399322444SpeterSQLITE_API int sqlite3_exec( 400251883Speter sqlite3*, /* An open database */ 401251883Speter const char *sql, /* SQL to be evaluated */ 402251883Speter int (*callback)(void*,int,char**,char**), /* Callback function */ 403251883Speter void *, /* 1st argument to callback */ 404251883Speter char **errmsg /* Error msg written here */ 405251883Speter); 406251883Speter 407251883Speter/* 408251883Speter** CAPI3REF: Result Codes 409274884Sbapt** KEYWORDS: {result code definitions} 410251883Speter** 411251883Speter** Many SQLite functions return an integer result code from the set shown 412251883Speter** here in order to indicate success or failure. 413251883Speter** 414251883Speter** New error codes may be added in future versions of SQLite. 415251883Speter** 416274884Sbapt** See also: [extended result code definitions] 417251883Speter*/ 418251883Speter#define SQLITE_OK 0 /* Successful result */ 419251883Speter/* beginning-of-error-codes */ 420322444Speter#define SQLITE_ERROR 1 /* Generic error */ 421251883Speter#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ 422251883Speter#define SQLITE_PERM 3 /* Access permission denied */ 423251883Speter#define SQLITE_ABORT 4 /* Callback routine requested an abort */ 424251883Speter#define SQLITE_BUSY 5 /* The database file is locked */ 425251883Speter#define SQLITE_LOCKED 6 /* A table in the database is locked */ 426251883Speter#define SQLITE_NOMEM 7 /* A malloc() failed */ 427251883Speter#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ 428251883Speter#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ 429251883Speter#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ 430251883Speter#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ 431251883Speter#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */ 432251883Speter#define SQLITE_FULL 13 /* Insertion failed because database is full */ 433251883Speter#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ 434251883Speter#define SQLITE_PROTOCOL 15 /* Database lock protocol error */ 435322444Speter#define SQLITE_EMPTY 16 /* Not used */ 436251883Speter#define SQLITE_SCHEMA 17 /* The database schema changed */ 437251883Speter#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ 438251883Speter#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ 439251883Speter#define SQLITE_MISMATCH 20 /* Data type mismatch */ 440251883Speter#define SQLITE_MISUSE 21 /* Library used incorrectly */ 441251883Speter#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ 442251883Speter#define SQLITE_AUTH 23 /* Authorization denied */ 443322444Speter#define SQLITE_FORMAT 24 /* Not used */ 444251883Speter#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ 445251883Speter#define SQLITE_NOTADB 26 /* File opened that is not a database file */ 446251883Speter#define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */ 447251883Speter#define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */ 448251883Speter#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ 449251883Speter#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ 450251883Speter/* end-of-error-codes */ 451251883Speter 452251883Speter/* 453251883Speter** CAPI3REF: Extended Result Codes 454274884Sbapt** KEYWORDS: {extended result code definitions} 455251883Speter** 456274884Sbapt** In its default configuration, SQLite API routines return one of 30 integer 457274884Sbapt** [result codes]. However, experience has shown that many of 458251883Speter** these result codes are too coarse-grained. They do not provide as 459251883Speter** much information about problems as programmers might like. In an effort to 460322444Speter** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8] 461322444Speter** and later) include 462251883Speter** support for additional result codes that provide more detailed information 463274884Sbapt** about errors. These [extended result codes] are enabled or disabled 464251883Speter** on a per database connection basis using the 465274884Sbapt** [sqlite3_extended_result_codes()] API. Or, the extended code for 466274884Sbapt** the most recent error can be obtained using 467274884Sbapt** [sqlite3_extended_errcode()]. 468251883Speter*/ 469251883Speter#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) 470251883Speter#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) 471251883Speter#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) 472251883Speter#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) 473251883Speter#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) 474251883Speter#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) 475251883Speter#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) 476251883Speter#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) 477251883Speter#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) 478251883Speter#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) 479251883Speter#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) 480251883Speter#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) 481251883Speter#define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8)) 482251883Speter#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8)) 483251883Speter#define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8)) 484251883Speter#define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8)) 485251883Speter#define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8)) 486251883Speter#define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8)) 487251883Speter#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8)) 488251883Speter#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8)) 489251883Speter#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8)) 490251883Speter#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8)) 491251883Speter#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8)) 492251883Speter#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8)) 493269851Speter#define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8)) 494269851Speter#define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8)) 495298161Sbapt#define SQLITE_IOERR_VNODE (SQLITE_IOERR | (27<<8)) 496298161Sbapt#define SQLITE_IOERR_AUTH (SQLITE_IOERR | (28<<8)) 497251883Speter#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8)) 498251883Speter#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8)) 499269851Speter#define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8)) 500251883Speter#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8)) 501251883Speter#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8)) 502251883Speter#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8)) 503269851Speter#define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8)) 504251883Speter#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8)) 505251883Speter#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8)) 506251883Speter#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8)) 507251883Speter#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8)) 508269851Speter#define SQLITE_READONLY_DBMOVED (SQLITE_READONLY | (4<<8)) 509251883Speter#define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8)) 510251883Speter#define SQLITE_CONSTRAINT_CHECK (SQLITE_CONSTRAINT | (1<<8)) 511251883Speter#define SQLITE_CONSTRAINT_COMMITHOOK (SQLITE_CONSTRAINT | (2<<8)) 512251883Speter#define SQLITE_CONSTRAINT_FOREIGNKEY (SQLITE_CONSTRAINT | (3<<8)) 513251883Speter#define SQLITE_CONSTRAINT_FUNCTION (SQLITE_CONSTRAINT | (4<<8)) 514251883Speter#define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8)) 515251883Speter#define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8)) 516251883Speter#define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8)) 517251883Speter#define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8)) 518251883Speter#define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8)) 519269851Speter#define SQLITE_CONSTRAINT_ROWID (SQLITE_CONSTRAINT |(10<<8)) 520251883Speter#define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8)) 521251883Speter#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8)) 522269851Speter#define SQLITE_WARNING_AUTOINDEX (SQLITE_WARNING | (1<<8)) 523274884Sbapt#define SQLITE_AUTH_USER (SQLITE_AUTH | (1<<8)) 524305002Scy#define SQLITE_OK_LOAD_PERMANENTLY (SQLITE_OK | (1<<8)) 525251883Speter 526251883Speter/* 527251883Speter** CAPI3REF: Flags For File Open Operations 528251883Speter** 529251883Speter** These bit values are intended for use in the 530251883Speter** 3rd parameter to the [sqlite3_open_v2()] interface and 531251883Speter** in the 4th parameter to the [sqlite3_vfs.xOpen] method. 532251883Speter*/ 533251883Speter#define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */ 534251883Speter#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */ 535251883Speter#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */ 536251883Speter#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */ 537251883Speter#define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */ 538251883Speter#define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */ 539251883Speter#define SQLITE_OPEN_URI 0x00000040 /* Ok for sqlite3_open_v2() */ 540251883Speter#define SQLITE_OPEN_MEMORY 0x00000080 /* Ok for sqlite3_open_v2() */ 541251883Speter#define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */ 542251883Speter#define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */ 543251883Speter#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */ 544251883Speter#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */ 545251883Speter#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */ 546251883Speter#define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */ 547251883Speter#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */ 548251883Speter#define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */ 549251883Speter#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */ 550251883Speter#define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */ 551251883Speter#define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */ 552251883Speter#define SQLITE_OPEN_WAL 0x00080000 /* VFS only */ 553251883Speter 554251883Speter/* Reserved: 0x00F00000 */ 555251883Speter 556251883Speter/* 557251883Speter** CAPI3REF: Device Characteristics 558251883Speter** 559251883Speter** The xDeviceCharacteristics method of the [sqlite3_io_methods] 560251883Speter** object returns an integer which is a vector of these 561251883Speter** bit values expressing I/O characteristics of the mass storage 562251883Speter** device that holds the file that the [sqlite3_io_methods] 563251883Speter** refers to. 564251883Speter** 565251883Speter** The SQLITE_IOCAP_ATOMIC property means that all writes of 566251883Speter** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values 567251883Speter** mean that writes of blocks that are nnn bytes in size and 568251883Speter** are aligned to an address which is an integer multiple of 569251883Speter** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means 570251883Speter** that when data is appended to a file, the data is appended 571251883Speter** first then the size of the file is extended, never the other 572251883Speter** way around. The SQLITE_IOCAP_SEQUENTIAL property means that 573251883Speter** information is written to disk in the same order as calls 574251883Speter** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that 575251883Speter** after reboot following a crash or power loss, the only bytes in a 576251883Speter** file that were written at the application level might have changed 577251883Speter** and that adjacent bytes, even bytes within the same sector are 578269851Speter** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 579322444Speter** flag indicates that a file cannot be deleted when open. The 580269851Speter** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on 581269851Speter** read-only media and cannot be changed even by processes with 582269851Speter** elevated privileges. 583251883Speter*/ 584251883Speter#define SQLITE_IOCAP_ATOMIC 0x00000001 585251883Speter#define SQLITE_IOCAP_ATOMIC512 0x00000002 586251883Speter#define SQLITE_IOCAP_ATOMIC1K 0x00000004 587251883Speter#define SQLITE_IOCAP_ATOMIC2K 0x00000008 588251883Speter#define SQLITE_IOCAP_ATOMIC4K 0x00000010 589251883Speter#define SQLITE_IOCAP_ATOMIC8K 0x00000020 590251883Speter#define SQLITE_IOCAP_ATOMIC16K 0x00000040 591251883Speter#define SQLITE_IOCAP_ATOMIC32K 0x00000080 592251883Speter#define SQLITE_IOCAP_ATOMIC64K 0x00000100 593251883Speter#define SQLITE_IOCAP_SAFE_APPEND 0x00000200 594251883Speter#define SQLITE_IOCAP_SEQUENTIAL 0x00000400 595251883Speter#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800 596251883Speter#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000 597269851Speter#define SQLITE_IOCAP_IMMUTABLE 0x00002000 598251883Speter 599251883Speter/* 600251883Speter** CAPI3REF: File Locking Levels 601251883Speter** 602251883Speter** SQLite uses one of these integer values as the second 603251883Speter** argument to calls it makes to the xLock() and xUnlock() methods 604251883Speter** of an [sqlite3_io_methods] object. 605251883Speter*/ 606251883Speter#define SQLITE_LOCK_NONE 0 607251883Speter#define SQLITE_LOCK_SHARED 1 608251883Speter#define SQLITE_LOCK_RESERVED 2 609251883Speter#define SQLITE_LOCK_PENDING 3 610251883Speter#define SQLITE_LOCK_EXCLUSIVE 4 611251883Speter 612251883Speter/* 613251883Speter** CAPI3REF: Synchronization Type Flags 614251883Speter** 615251883Speter** When SQLite invokes the xSync() method of an 616251883Speter** [sqlite3_io_methods] object it uses a combination of 617251883Speter** these integer values as the second argument. 618251883Speter** 619251883Speter** When the SQLITE_SYNC_DATAONLY flag is used, it means that the 620251883Speter** sync operation only needs to flush data to mass storage. Inode 621251883Speter** information need not be flushed. If the lower four bits of the flag 622251883Speter** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics. 623251883Speter** If the lower four bits equal SQLITE_SYNC_FULL, that means 624251883Speter** to use Mac OS X style fullsync instead of fsync(). 625251883Speter** 626251883Speter** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags 627251883Speter** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL 628251883Speter** settings. The [synchronous pragma] determines when calls to the 629251883Speter** xSync VFS method occur and applies uniformly across all platforms. 630251883Speter** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how 631251883Speter** energetic or rigorous or forceful the sync operations are and 632251883Speter** only make a difference on Mac OSX for the default SQLite code. 633251883Speter** (Third-party VFS implementations might also make the distinction 634251883Speter** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the 635251883Speter** operating systems natively supported by SQLite, only Mac OSX 636251883Speter** cares about the difference.) 637251883Speter*/ 638251883Speter#define SQLITE_SYNC_NORMAL 0x00002 639251883Speter#define SQLITE_SYNC_FULL 0x00003 640251883Speter#define SQLITE_SYNC_DATAONLY 0x00010 641251883Speter 642251883Speter/* 643251883Speter** CAPI3REF: OS Interface Open File Handle 644251883Speter** 645251883Speter** An [sqlite3_file] object represents an open file in the 646251883Speter** [sqlite3_vfs | OS interface layer]. Individual OS interface 647251883Speter** implementations will 648251883Speter** want to subclass this object by appending additional fields 649251883Speter** for their own use. The pMethods entry is a pointer to an 650251883Speter** [sqlite3_io_methods] object that defines methods for performing 651251883Speter** I/O operations on the open file. 652251883Speter*/ 653251883Spetertypedef struct sqlite3_file sqlite3_file; 654251883Speterstruct sqlite3_file { 655251883Speter const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ 656251883Speter}; 657251883Speter 658251883Speter/* 659251883Speter** CAPI3REF: OS Interface File Virtual Methods Object 660251883Speter** 661251883Speter** Every file opened by the [sqlite3_vfs.xOpen] method populates an 662251883Speter** [sqlite3_file] object (or, more commonly, a subclass of the 663251883Speter** [sqlite3_file] object) with a pointer to an instance of this object. 664251883Speter** This object defines the methods used to perform various operations 665251883Speter** against the open file represented by the [sqlite3_file] object. 666251883Speter** 667251883Speter** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element 668251883Speter** to a non-NULL pointer, then the sqlite3_io_methods.xClose method 669251883Speter** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed. The 670251883Speter** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen] 671251883Speter** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element 672251883Speter** to NULL. 673251883Speter** 674251883Speter** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or 675251883Speter** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). 676251883Speter** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY] 677251883Speter** flag may be ORed in to indicate that only the data of the file 678251883Speter** and not its inode needs to be synced. 679251883Speter** 680251883Speter** The integer values to xLock() and xUnlock() are one of 681251883Speter** <ul> 682251883Speter** <li> [SQLITE_LOCK_NONE], 683251883Speter** <li> [SQLITE_LOCK_SHARED], 684251883Speter** <li> [SQLITE_LOCK_RESERVED], 685251883Speter** <li> [SQLITE_LOCK_PENDING], or 686251883Speter** <li> [SQLITE_LOCK_EXCLUSIVE]. 687251883Speter** </ul> 688251883Speter** xLock() increases the lock. xUnlock() decreases the lock. 689251883Speter** The xCheckReservedLock() method checks whether any database connection, 690251883Speter** either in this process or in some other process, is holding a RESERVED, 691251883Speter** PENDING, or EXCLUSIVE lock on the file. It returns true 692251883Speter** if such a lock exists and false otherwise. 693251883Speter** 694251883Speter** The xFileControl() method is a generic interface that allows custom 695251883Speter** VFS implementations to directly control an open file using the 696251883Speter** [sqlite3_file_control()] interface. The second "op" argument is an 697251883Speter** integer opcode. The third argument is a generic pointer intended to 698251883Speter** point to a structure that may contain arguments or space in which to 699251883Speter** write return values. Potential uses for xFileControl() might be 700251883Speter** functions to enable blocking locks with timeouts, to change the 701251883Speter** locking strategy (for example to use dot-file locks), to inquire 702251883Speter** about the status of a lock, or to break stale locks. The SQLite 703251883Speter** core reserves all opcodes less than 100 for its own use. 704274884Sbapt** A [file control opcodes | list of opcodes] less than 100 is available. 705251883Speter** Applications that define a custom xFileControl method should use opcodes 706251883Speter** greater than 100 to avoid conflicts. VFS implementations should 707251883Speter** return [SQLITE_NOTFOUND] for file control opcodes that they do not 708251883Speter** recognize. 709251883Speter** 710251883Speter** The xSectorSize() method returns the sector size of the 711251883Speter** device that underlies the file. The sector size is the 712251883Speter** minimum write that can be performed without disturbing 713251883Speter** other bytes in the file. The xDeviceCharacteristics() 714251883Speter** method returns a bit vector describing behaviors of the 715251883Speter** underlying device: 716251883Speter** 717251883Speter** <ul> 718251883Speter** <li> [SQLITE_IOCAP_ATOMIC] 719251883Speter** <li> [SQLITE_IOCAP_ATOMIC512] 720251883Speter** <li> [SQLITE_IOCAP_ATOMIC1K] 721251883Speter** <li> [SQLITE_IOCAP_ATOMIC2K] 722251883Speter** <li> [SQLITE_IOCAP_ATOMIC4K] 723251883Speter** <li> [SQLITE_IOCAP_ATOMIC8K] 724251883Speter** <li> [SQLITE_IOCAP_ATOMIC16K] 725251883Speter** <li> [SQLITE_IOCAP_ATOMIC32K] 726251883Speter** <li> [SQLITE_IOCAP_ATOMIC64K] 727251883Speter** <li> [SQLITE_IOCAP_SAFE_APPEND] 728251883Speter** <li> [SQLITE_IOCAP_SEQUENTIAL] 729322444Speter** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN] 730322444Speter** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE] 731322444Speter** <li> [SQLITE_IOCAP_IMMUTABLE] 732251883Speter** </ul> 733251883Speter** 734251883Speter** The SQLITE_IOCAP_ATOMIC property means that all writes of 735251883Speter** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values 736251883Speter** mean that writes of blocks that are nnn bytes in size and 737251883Speter** are aligned to an address which is an integer multiple of 738251883Speter** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means 739251883Speter** that when data is appended to a file, the data is appended 740251883Speter** first then the size of the file is extended, never the other 741251883Speter** way around. The SQLITE_IOCAP_SEQUENTIAL property means that 742251883Speter** information is written to disk in the same order as calls 743251883Speter** to xWrite(). 744251883Speter** 745251883Speter** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill 746251883Speter** in the unread portions of the buffer with zeros. A VFS that 747251883Speter** fails to zero-fill short reads might seem to work. However, 748251883Speter** failure to zero-fill short reads will eventually lead to 749251883Speter** database corruption. 750251883Speter*/ 751251883Spetertypedef struct sqlite3_io_methods sqlite3_io_methods; 752251883Speterstruct sqlite3_io_methods { 753251883Speter int iVersion; 754251883Speter int (*xClose)(sqlite3_file*); 755251883Speter int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); 756251883Speter int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); 757251883Speter int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); 758251883Speter int (*xSync)(sqlite3_file*, int flags); 759251883Speter int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); 760251883Speter int (*xLock)(sqlite3_file*, int); 761251883Speter int (*xUnlock)(sqlite3_file*, int); 762251883Speter int (*xCheckReservedLock)(sqlite3_file*, int *pResOut); 763251883Speter int (*xFileControl)(sqlite3_file*, int op, void *pArg); 764251883Speter int (*xSectorSize)(sqlite3_file*); 765251883Speter int (*xDeviceCharacteristics)(sqlite3_file*); 766251883Speter /* Methods above are valid for version 1 */ 767251883Speter int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**); 768251883Speter int (*xShmLock)(sqlite3_file*, int offset, int n, int flags); 769251883Speter void (*xShmBarrier)(sqlite3_file*); 770251883Speter int (*xShmUnmap)(sqlite3_file*, int deleteFlag); 771251883Speter /* Methods above are valid for version 2 */ 772251883Speter int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); 773251883Speter int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p); 774251883Speter /* Methods above are valid for version 3 */ 775251883Speter /* Additional methods may be added in future releases */ 776251883Speter}; 777251883Speter 778251883Speter/* 779251883Speter** CAPI3REF: Standard File Control Opcodes 780274884Sbapt** KEYWORDS: {file control opcodes} {file control opcode} 781251883Speter** 782251883Speter** These integer constants are opcodes for the xFileControl method 783251883Speter** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()] 784251883Speter** interface. 785251883Speter** 786282328Sbapt** <ul> 787282328Sbapt** <li>[[SQLITE_FCNTL_LOCKSTATE]] 788251883Speter** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This 789251883Speter** opcode causes the xFileControl method to write the current state of 790251883Speter** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], 791251883Speter** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) 792251883Speter** into an integer that the pArg argument points to. This capability 793282328Sbapt** is used during testing and is only available when the SQLITE_TEST 794282328Sbapt** compile-time option is used. 795282328Sbapt** 796251883Speter** <li>[[SQLITE_FCNTL_SIZE_HINT]] 797251883Speter** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS 798251883Speter** layer a hint of how large the database file will grow to be during the 799251883Speter** current transaction. This hint is not guaranteed to be accurate but it 800251883Speter** is often close. The underlying VFS might choose to preallocate database 801251883Speter** file space based on this hint in order to help writes to the database 802251883Speter** file run faster. 803251883Speter** 804251883Speter** <li>[[SQLITE_FCNTL_CHUNK_SIZE]] 805251883Speter** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS 806251883Speter** extends and truncates the database file in chunks of a size specified 807251883Speter** by the user. The fourth argument to [sqlite3_file_control()] should 808251883Speter** point to an integer (type int) containing the new chunk-size to use 809251883Speter** for the nominated database. Allocating database file space in large 810251883Speter** chunks (say 1MB at a time), may reduce file-system fragmentation and 811251883Speter** improve performance on some systems. 812251883Speter** 813251883Speter** <li>[[SQLITE_FCNTL_FILE_POINTER]] 814251883Speter** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer 815251883Speter** to the [sqlite3_file] object associated with a particular database 816298161Sbapt** connection. See also [SQLITE_FCNTL_JOURNAL_POINTER]. 817251883Speter** 818298161Sbapt** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]] 819298161Sbapt** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer 820298161Sbapt** to the [sqlite3_file] object associated with the journal file (either 821298161Sbapt** the [rollback journal] or the [write-ahead log]) for a particular database 822298161Sbapt** connection. See also [SQLITE_FCNTL_FILE_POINTER]. 823298161Sbapt** 824251883Speter** <li>[[SQLITE_FCNTL_SYNC_OMITTED]] 825269851Speter** No longer in use. 826251883Speter** 827269851Speter** <li>[[SQLITE_FCNTL_SYNC]] 828269851Speter** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and 829269851Speter** sent to the VFS immediately before the xSync method is invoked on a 830269851Speter** database file descriptor. Or, if the xSync method is not invoked 831269851Speter** because the user has configured SQLite with 832269851Speter** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place 833269851Speter** of the xSync method. In most cases, the pointer argument passed with 834269851Speter** this file-control is NULL. However, if the database file is being synced 835269851Speter** as part of a multi-database commit, the argument points to a nul-terminated 836269851Speter** string containing the transactions master-journal file name. VFSes that 837269851Speter** do not need this signal should silently ignore this opcode. Applications 838269851Speter** should not call [sqlite3_file_control()] with this opcode as doing so may 839269851Speter** disrupt the operation of the specialized VFSes that do require it. 840269851Speter** 841269851Speter** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]] 842269851Speter** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite 843269851Speter** and sent to the VFS after a transaction has been committed immediately 844269851Speter** but before the database is unlocked. VFSes that do not need this signal 845269851Speter** should silently ignore this opcode. Applications should not call 846269851Speter** [sqlite3_file_control()] with this opcode as doing so may disrupt the 847269851Speter** operation of the specialized VFSes that do require it. 848269851Speter** 849251883Speter** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]] 850251883Speter** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic 851251883Speter** retry counts and intervals for certain disk I/O operations for the 852251883Speter** windows [VFS] in order to provide robustness in the presence of 853251883Speter** anti-virus programs. By default, the windows VFS will retry file read, 854251883Speter** file write, and file delete operations up to 10 times, with a delay 855251883Speter** of 25 milliseconds before the first retry and with the delay increasing 856251883Speter** by an additional 25 milliseconds with each subsequent retry. This 857251883Speter** opcode allows these two values (10 retries and 25 milliseconds of delay) 858251883Speter** to be adjusted. The values are changed for all database connections 859251883Speter** within the same process. The argument is a pointer to an array of two 860322444Speter** integers where the first integer is the new retry count and the second 861251883Speter** integer is the delay. If either integer is negative, then the setting 862251883Speter** is not changed but instead the prior value of that setting is written 863251883Speter** into the array entry, allowing the current retry settings to be 864251883Speter** interrogated. The zDbName parameter is ignored. 865251883Speter** 866251883Speter** <li>[[SQLITE_FCNTL_PERSIST_WAL]] 867251883Speter** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the 868251883Speter** persistent [WAL | Write Ahead Log] setting. By default, the auxiliary 869251883Speter** write ahead log and shared memory files used for transaction control 870251883Speter** are automatically deleted when the latest connection to the database 871251883Speter** closes. Setting persistent WAL mode causes those files to persist after 872251883Speter** close. Persisting the files is useful when other processes that do not 873251883Speter** have write permission on the directory containing the database file want 874251883Speter** to read the database file, as the WAL and shared memory files must exist 875251883Speter** in order for the database to be readable. The fourth parameter to 876251883Speter** [sqlite3_file_control()] for this opcode should be a pointer to an integer. 877251883Speter** That integer is 0 to disable persistent WAL mode or 1 to enable persistent 878251883Speter** WAL mode. If the integer is -1, then it is overwritten with the current 879251883Speter** WAL persistence setting. 880251883Speter** 881251883Speter** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]] 882251883Speter** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the 883251883Speter** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting 884251883Speter** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the 885251883Speter** xDeviceCharacteristics methods. The fourth parameter to 886251883Speter** [sqlite3_file_control()] for this opcode should be a pointer to an integer. 887251883Speter** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage 888251883Speter** mode. If the integer is -1, then it is overwritten with the current 889251883Speter** zero-damage mode setting. 890251883Speter** 891251883Speter** <li>[[SQLITE_FCNTL_OVERWRITE]] 892251883Speter** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening 893251883Speter** a write transaction to indicate that, unless it is rolled back for some 894251883Speter** reason, the entire database file will be overwritten by the current 895251883Speter** transaction. This is used by VACUUM operations. 896251883Speter** 897251883Speter** <li>[[SQLITE_FCNTL_VFSNAME]] 898251883Speter** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of 899251883Speter** all [VFSes] in the VFS stack. The names are of all VFS shims and the 900251883Speter** final bottom-level VFS are written into memory obtained from 901251883Speter** [sqlite3_malloc()] and the result is stored in the char* variable 902251883Speter** that the fourth parameter of [sqlite3_file_control()] points to. 903251883Speter** The caller is responsible for freeing the memory when done. As with 904251883Speter** all file-control actions, there is no guarantee that this will actually 905251883Speter** do anything. Callers should initialize the char* variable to a NULL 906251883Speter** pointer in case this file-control is not implemented. This file-control 907251883Speter** is intended for diagnostic use only. 908251883Speter** 909298161Sbapt** <li>[[SQLITE_FCNTL_VFS_POINTER]] 910298161Sbapt** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level 911298161Sbapt** [VFSes] currently in use. ^(The argument X in 912298161Sbapt** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be 913298161Sbapt** of type "[sqlite3_vfs] **". This opcodes will set *X 914298161Sbapt** to a pointer to the top-level VFS.)^ 915298161Sbapt** ^When there are multiple VFS shims in the stack, this opcode finds the 916298161Sbapt** upper-most shim only. 917298161Sbapt** 918251883Speter** <li>[[SQLITE_FCNTL_PRAGMA]] 919251883Speter** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA] 920251883Speter** file control is sent to the open [sqlite3_file] object corresponding 921251883Speter** to the database file to which the pragma statement refers. ^The argument 922251883Speter** to the [SQLITE_FCNTL_PRAGMA] file control is an array of 923251883Speter** pointers to strings (char**) in which the second element of the array 924251883Speter** is the name of the pragma and the third element is the argument to the 925251883Speter** pragma or NULL if the pragma has no argument. ^The handler for an 926251883Speter** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element 927251883Speter** of the char** argument point to a string obtained from [sqlite3_mprintf()] 928251883Speter** or the equivalent and that string will become the result of the pragma or 929251883Speter** the error message if the pragma fails. ^If the 930251883Speter** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal 931251883Speter** [PRAGMA] processing continues. ^If the [SQLITE_FCNTL_PRAGMA] 932251883Speter** file control returns [SQLITE_OK], then the parser assumes that the 933251883Speter** VFS has handled the PRAGMA itself and the parser generates a no-op 934282328Sbapt** prepared statement if result string is NULL, or that returns a copy 935282328Sbapt** of the result string if the string is non-NULL. 936282328Sbapt** ^If the [SQLITE_FCNTL_PRAGMA] file control returns 937251883Speter** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means 938251883Speter** that the VFS encountered an error while handling the [PRAGMA] and the 939251883Speter** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA] 940251883Speter** file control occurs at the beginning of pragma statement analysis and so 941251883Speter** it is able to override built-in [PRAGMA] statements. 942251883Speter** 943251883Speter** <li>[[SQLITE_FCNTL_BUSYHANDLER]] 944251883Speter** ^The [SQLITE_FCNTL_BUSYHANDLER] 945251883Speter** file-control may be invoked by SQLite on the database file handle 946251883Speter** shortly after it is opened in order to provide a custom VFS with access 947251883Speter** to the connections busy-handler callback. The argument is of type (void **) 948251883Speter** - an array of two (void *) values. The first (void *) actually points 949251883Speter** to a function of type (int (*)(void *)). In order to invoke the connections 950251883Speter** busy-handler, this function should be invoked with the second (void *) in 951251883Speter** the array as the only argument. If it returns non-zero, then the operation 952251883Speter** should be retried. If it returns zero, the custom VFS should abandon the 953251883Speter** current operation. 954251883Speter** 955251883Speter** <li>[[SQLITE_FCNTL_TEMPFILENAME]] 956251883Speter** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control 957251883Speter** to have SQLite generate a 958251883Speter** temporary filename using the same algorithm that is followed to generate 959251883Speter** temporary filenames for TEMP tables and other internal uses. The 960251883Speter** argument should be a char** which will be filled with the filename 961251883Speter** written into memory obtained from [sqlite3_malloc()]. The caller should 962251883Speter** invoke [sqlite3_free()] on the result to avoid a memory leak. 963251883Speter** 964251883Speter** <li>[[SQLITE_FCNTL_MMAP_SIZE]] 965251883Speter** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the 966251883Speter** maximum number of bytes that will be used for memory-mapped I/O. 967251883Speter** The argument is a pointer to a value of type sqlite3_int64 that 968251883Speter** is an advisory maximum number of bytes in the file to memory map. The 969251883Speter** pointer is overwritten with the old value. The limit is not changed if 970251883Speter** the value originally pointed to is negative, and so the current limit 971251883Speter** can be queried by passing in a pointer to a negative number. This 972251883Speter** file-control is used internally to implement [PRAGMA mmap_size]. 973251883Speter** 974269851Speter** <li>[[SQLITE_FCNTL_TRACE]] 975269851Speter** The [SQLITE_FCNTL_TRACE] file control provides advisory information 976269851Speter** to the VFS about what the higher layers of the SQLite stack are doing. 977269851Speter** This file control is used by some VFS activity tracing [shims]. 978269851Speter** The argument is a zero-terminated string. Higher layers in the 979269851Speter** SQLite stack may generate instances of this file control if 980269851Speter** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled. 981269851Speter** 982269851Speter** <li>[[SQLITE_FCNTL_HAS_MOVED]] 983269851Speter** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a 984269851Speter** pointer to an integer and it writes a boolean into that integer depending 985269851Speter** on whether or not the file has been renamed, moved, or deleted since it 986269851Speter** was first opened. 987269851Speter** 988322444Speter** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]] 989322444Speter** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the 990322444Speter** underlying native file handle associated with a file handle. This file 991322444Speter** control interprets its argument as a pointer to a native file handle and 992322444Speter** writes the resulting value there. 993322444Speter** 994269851Speter** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]] 995269851Speter** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging. This 996269851Speter** opcode causes the xFileControl method to swap the file handle with the one 997269851Speter** pointed to by the pArg argument. This capability is used during testing 998269851Speter** and only needs to be supported when SQLITE_TEST is defined. 999269851Speter** 1000282328Sbapt** <li>[[SQLITE_FCNTL_WAL_BLOCK]] 1001282328Sbapt** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might 1002282328Sbapt** be advantageous to block on the next WAL lock if the lock is not immediately 1003282328Sbapt** available. The WAL subsystem issues this signal during rare 1004282328Sbapt** circumstances in order to fix a problem with priority inversion. 1005282328Sbapt** Applications should <em>not</em> use this file-control. 1006282328Sbapt** 1007286510Speter** <li>[[SQLITE_FCNTL_ZIPVFS]] 1008286510Speter** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other 1009286510Speter** VFS should return SQLITE_NOTFOUND for this opcode. 1010286510Speter** 1011286510Speter** <li>[[SQLITE_FCNTL_RBU]] 1012286510Speter** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by 1013286510Speter** the RBU extension only. All other VFS should return SQLITE_NOTFOUND for 1014286510Speter** this opcode. 1015251883Speter** </ul> 1016251883Speter*/ 1017251883Speter#define SQLITE_FCNTL_LOCKSTATE 1 1018282328Sbapt#define SQLITE_FCNTL_GET_LOCKPROXYFILE 2 1019282328Sbapt#define SQLITE_FCNTL_SET_LOCKPROXYFILE 3 1020282328Sbapt#define SQLITE_FCNTL_LAST_ERRNO 4 1021251883Speter#define SQLITE_FCNTL_SIZE_HINT 5 1022251883Speter#define SQLITE_FCNTL_CHUNK_SIZE 6 1023251883Speter#define SQLITE_FCNTL_FILE_POINTER 7 1024251883Speter#define SQLITE_FCNTL_SYNC_OMITTED 8 1025251883Speter#define SQLITE_FCNTL_WIN32_AV_RETRY 9 1026251883Speter#define SQLITE_FCNTL_PERSIST_WAL 10 1027251883Speter#define SQLITE_FCNTL_OVERWRITE 11 1028251883Speter#define SQLITE_FCNTL_VFSNAME 12 1029251883Speter#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13 1030251883Speter#define SQLITE_FCNTL_PRAGMA 14 1031251883Speter#define SQLITE_FCNTL_BUSYHANDLER 15 1032251883Speter#define SQLITE_FCNTL_TEMPFILENAME 16 1033251883Speter#define SQLITE_FCNTL_MMAP_SIZE 18 1034269851Speter#define SQLITE_FCNTL_TRACE 19 1035269851Speter#define SQLITE_FCNTL_HAS_MOVED 20 1036269851Speter#define SQLITE_FCNTL_SYNC 21 1037269851Speter#define SQLITE_FCNTL_COMMIT_PHASETWO 22 1038269851Speter#define SQLITE_FCNTL_WIN32_SET_HANDLE 23 1039282328Sbapt#define SQLITE_FCNTL_WAL_BLOCK 24 1040286510Speter#define SQLITE_FCNTL_ZIPVFS 25 1041286510Speter#define SQLITE_FCNTL_RBU 26 1042298161Sbapt#define SQLITE_FCNTL_VFS_POINTER 27 1043298161Sbapt#define SQLITE_FCNTL_JOURNAL_POINTER 28 1044322444Speter#define SQLITE_FCNTL_WIN32_GET_HANDLE 29 1045322444Speter#define SQLITE_FCNTL_PDB 30 1046251883Speter 1047282328Sbapt/* deprecated names */ 1048282328Sbapt#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE 1049282328Sbapt#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE 1050282328Sbapt#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO 1051282328Sbapt 1052282328Sbapt 1053251883Speter/* 1054251883Speter** CAPI3REF: Mutex Handle 1055251883Speter** 1056251883Speter** The mutex module within SQLite defines [sqlite3_mutex] to be an 1057251883Speter** abstract type for a mutex object. The SQLite core never looks 1058251883Speter** at the internal representation of an [sqlite3_mutex]. It only 1059251883Speter** deals with pointers to the [sqlite3_mutex] object. 1060251883Speter** 1061251883Speter** Mutexes are created using [sqlite3_mutex_alloc()]. 1062251883Speter*/ 1063251883Spetertypedef struct sqlite3_mutex sqlite3_mutex; 1064251883Speter 1065251883Speter/* 1066305002Scy** CAPI3REF: Loadable Extension Thunk 1067305002Scy** 1068305002Scy** A pointer to the opaque sqlite3_api_routines structure is passed as 1069305002Scy** the third parameter to entry points of [loadable extensions]. This 1070305002Scy** structure must be typedefed in order to work around compiler warnings 1071305002Scy** on some platforms. 1072305002Scy*/ 1073305002Scytypedef struct sqlite3_api_routines sqlite3_api_routines; 1074305002Scy 1075305002Scy/* 1076251883Speter** CAPI3REF: OS Interface Object 1077251883Speter** 1078251883Speter** An instance of the sqlite3_vfs object defines the interface between 1079251883Speter** the SQLite core and the underlying operating system. The "vfs" 1080251883Speter** in the name of the object stands for "virtual file system". See 1081251883Speter** the [VFS | VFS documentation] for further information. 1082251883Speter** 1083251883Speter** The value of the iVersion field is initially 1 but may be larger in 1084251883Speter** future versions of SQLite. Additional fields may be appended to this 1085251883Speter** object when the iVersion value is increased. Note that the structure 1086251883Speter** of the sqlite3_vfs object changes in the transaction between 1087251883Speter** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not 1088251883Speter** modified. 1089251883Speter** 1090251883Speter** The szOsFile field is the size of the subclassed [sqlite3_file] 1091251883Speter** structure used by this VFS. mxPathname is the maximum length of 1092251883Speter** a pathname in this VFS. 1093251883Speter** 1094251883Speter** Registered sqlite3_vfs objects are kept on a linked list formed by 1095251883Speter** the pNext pointer. The [sqlite3_vfs_register()] 1096251883Speter** and [sqlite3_vfs_unregister()] interfaces manage this list 1097251883Speter** in a thread-safe way. The [sqlite3_vfs_find()] interface 1098251883Speter** searches the list. Neither the application code nor the VFS 1099251883Speter** implementation should use the pNext pointer. 1100251883Speter** 1101251883Speter** The pNext field is the only field in the sqlite3_vfs 1102251883Speter** structure that SQLite will ever modify. SQLite will only access 1103251883Speter** or modify this field while holding a particular static mutex. 1104251883Speter** The application should never modify anything within the sqlite3_vfs 1105251883Speter** object once the object has been registered. 1106251883Speter** 1107251883Speter** The zName field holds the name of the VFS module. The name must 1108251883Speter** be unique across all VFS modules. 1109251883Speter** 1110251883Speter** [[sqlite3_vfs.xOpen]] 1111251883Speter** ^SQLite guarantees that the zFilename parameter to xOpen 1112251883Speter** is either a NULL pointer or string obtained 1113251883Speter** from xFullPathname() with an optional suffix added. 1114251883Speter** ^If a suffix is added to the zFilename parameter, it will 1115251883Speter** consist of a single "-" character followed by no more than 1116251883Speter** 11 alphanumeric and/or "-" characters. 1117251883Speter** ^SQLite further guarantees that 1118251883Speter** the string will be valid and unchanged until xClose() is 1119251883Speter** called. Because of the previous sentence, 1120251883Speter** the [sqlite3_file] can safely store a pointer to the 1121251883Speter** filename if it needs to remember the filename for some reason. 1122251883Speter** If the zFilename parameter to xOpen is a NULL pointer then xOpen 1123251883Speter** must invent its own temporary name for the file. ^Whenever the 1124251883Speter** xFilename parameter is NULL it will also be the case that the 1125251883Speter** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. 1126251883Speter** 1127251883Speter** The flags argument to xOpen() includes all bits set in 1128251883Speter** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] 1129251883Speter** or [sqlite3_open16()] is used, then flags includes at least 1130251883Speter** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. 1131251883Speter** If xOpen() opens a file read-only then it sets *pOutFlags to 1132251883Speter** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. 1133251883Speter** 1134251883Speter** ^(SQLite will also add one of the following flags to the xOpen() 1135251883Speter** call, depending on the object being opened: 1136251883Speter** 1137251883Speter** <ul> 1138251883Speter** <li> [SQLITE_OPEN_MAIN_DB] 1139251883Speter** <li> [SQLITE_OPEN_MAIN_JOURNAL] 1140251883Speter** <li> [SQLITE_OPEN_TEMP_DB] 1141251883Speter** <li> [SQLITE_OPEN_TEMP_JOURNAL] 1142251883Speter** <li> [SQLITE_OPEN_TRANSIENT_DB] 1143251883Speter** <li> [SQLITE_OPEN_SUBJOURNAL] 1144251883Speter** <li> [SQLITE_OPEN_MASTER_JOURNAL] 1145251883Speter** <li> [SQLITE_OPEN_WAL] 1146251883Speter** </ul>)^ 1147251883Speter** 1148251883Speter** The file I/O implementation can use the object type flags to 1149251883Speter** change the way it deals with files. For example, an application 1150251883Speter** that does not care about crash recovery or rollback might make 1151251883Speter** the open of a journal file a no-op. Writes to this journal would 1152251883Speter** also be no-ops, and any attempt to read the journal would return 1153251883Speter** SQLITE_IOERR. Or the implementation might recognize that a database 1154251883Speter** file will be doing page-aligned sector reads and writes in a random 1155251883Speter** order and set up its I/O subsystem accordingly. 1156251883Speter** 1157251883Speter** SQLite might also add one of the following flags to the xOpen method: 1158251883Speter** 1159251883Speter** <ul> 1160251883Speter** <li> [SQLITE_OPEN_DELETEONCLOSE] 1161251883Speter** <li> [SQLITE_OPEN_EXCLUSIVE] 1162251883Speter** </ul> 1163251883Speter** 1164251883Speter** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be 1165251883Speter** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE] 1166251883Speter** will be set for TEMP databases and their journals, transient 1167251883Speter** databases, and subjournals. 1168251883Speter** 1169251883Speter** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction 1170251883Speter** with the [SQLITE_OPEN_CREATE] flag, which are both directly 1171251883Speter** analogous to the O_EXCL and O_CREAT flags of the POSIX open() 1172251883Speter** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the 1173251883Speter** SQLITE_OPEN_CREATE, is used to indicate that file should always 1174251883Speter** be created, and that it is an error if it already exists. 1175251883Speter** It is <i>not</i> used to indicate the file should be opened 1176251883Speter** for exclusive access. 1177251883Speter** 1178251883Speter** ^At least szOsFile bytes of memory are allocated by SQLite 1179251883Speter** to hold the [sqlite3_file] structure passed as the third 1180251883Speter** argument to xOpen. The xOpen method does not have to 1181251883Speter** allocate the structure; it should just fill it in. Note that 1182251883Speter** the xOpen method must set the sqlite3_file.pMethods to either 1183251883Speter** a valid [sqlite3_io_methods] object or to NULL. xOpen must do 1184251883Speter** this even if the open fails. SQLite expects that the sqlite3_file.pMethods 1185251883Speter** element will be valid after xOpen returns regardless of the success 1186251883Speter** or failure of the xOpen call. 1187251883Speter** 1188251883Speter** [[sqlite3_vfs.xAccess]] 1189251883Speter** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] 1190251883Speter** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to 1191251883Speter** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] 1192251883Speter** to test whether a file is at least readable. The file can be a 1193251883Speter** directory. 1194251883Speter** 1195251883Speter** ^SQLite will always allocate at least mxPathname+1 bytes for the 1196251883Speter** output buffer xFullPathname. The exact size of the output buffer 1197251883Speter** is also passed as a parameter to both methods. If the output buffer 1198251883Speter** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is 1199251883Speter** handled as a fatal error by SQLite, vfs implementations should endeavor 1200251883Speter** to prevent this by setting mxPathname to a sufficiently large value. 1201251883Speter** 1202251883Speter** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64() 1203251883Speter** interfaces are not strictly a part of the filesystem, but they are 1204251883Speter** included in the VFS structure for completeness. 1205251883Speter** The xRandomness() function attempts to return nBytes bytes 1206251883Speter** of good-quality randomness into zOut. The return value is 1207251883Speter** the actual number of bytes of randomness obtained. 1208251883Speter** The xSleep() method causes the calling thread to sleep for at 1209251883Speter** least the number of microseconds given. ^The xCurrentTime() 1210251883Speter** method returns a Julian Day Number for the current date and time as 1211251883Speter** a floating point value. 1212251883Speter** ^The xCurrentTimeInt64() method returns, as an integer, the Julian 1213251883Speter** Day Number multiplied by 86400000 (the number of milliseconds in 1214251883Speter** a 24-hour day). 1215251883Speter** ^SQLite will use the xCurrentTimeInt64() method to get the current 1216251883Speter** date and time if that method is available (if iVersion is 2 or 1217251883Speter** greater and the function pointer is not NULL) and will fall back 1218251883Speter** to xCurrentTime() if xCurrentTimeInt64() is unavailable. 1219251883Speter** 1220251883Speter** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces 1221251883Speter** are not used by the SQLite core. These optional interfaces are provided 1222251883Speter** by some VFSes to facilitate testing of the VFS code. By overriding 1223251883Speter** system calls with functions under its control, a test program can 1224251883Speter** simulate faults and error conditions that would otherwise be difficult 1225251883Speter** or impossible to induce. The set of system calls that can be overridden 1226251883Speter** varies from one VFS to another, and from one version of the same VFS to the 1227251883Speter** next. Applications that use these interfaces must be prepared for any 1228251883Speter** or all of these interfaces to be NULL or for their behavior to change 1229251883Speter** from one release to the next. Applications must not attempt to access 1230251883Speter** any of these methods if the iVersion of the VFS is less than 3. 1231251883Speter*/ 1232251883Spetertypedef struct sqlite3_vfs sqlite3_vfs; 1233251883Spetertypedef void (*sqlite3_syscall_ptr)(void); 1234251883Speterstruct sqlite3_vfs { 1235251883Speter int iVersion; /* Structure version number (currently 3) */ 1236251883Speter int szOsFile; /* Size of subclassed sqlite3_file */ 1237251883Speter int mxPathname; /* Maximum file pathname length */ 1238251883Speter sqlite3_vfs *pNext; /* Next registered VFS */ 1239251883Speter const char *zName; /* Name of this virtual file system */ 1240251883Speter void *pAppData; /* Pointer to application-specific data */ 1241251883Speter int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, 1242251883Speter int flags, int *pOutFlags); 1243251883Speter int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); 1244251883Speter int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut); 1245251883Speter int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); 1246251883Speter void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); 1247251883Speter void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); 1248251883Speter void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); 1249251883Speter void (*xDlClose)(sqlite3_vfs*, void*); 1250251883Speter int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); 1251251883Speter int (*xSleep)(sqlite3_vfs*, int microseconds); 1252251883Speter int (*xCurrentTime)(sqlite3_vfs*, double*); 1253251883Speter int (*xGetLastError)(sqlite3_vfs*, int, char *); 1254251883Speter /* 1255251883Speter ** The methods above are in version 1 of the sqlite_vfs object 1256251883Speter ** definition. Those that follow are added in version 2 or later 1257251883Speter */ 1258251883Speter int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*); 1259251883Speter /* 1260251883Speter ** The methods above are in versions 1 and 2 of the sqlite_vfs object. 1261251883Speter ** Those below are for version 3 and greater. 1262251883Speter */ 1263251883Speter int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr); 1264251883Speter sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName); 1265251883Speter const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName); 1266251883Speter /* 1267251883Speter ** The methods above are in versions 1 through 3 of the sqlite_vfs object. 1268298161Sbapt ** New fields may be appended in future versions. The iVersion 1269251883Speter ** value will increment whenever this happens. 1270251883Speter */ 1271251883Speter}; 1272251883Speter 1273251883Speter/* 1274251883Speter** CAPI3REF: Flags for the xAccess VFS method 1275251883Speter** 1276251883Speter** These integer constants can be used as the third parameter to 1277251883Speter** the xAccess method of an [sqlite3_vfs] object. They determine 1278251883Speter** what kind of permissions the xAccess method is looking for. 1279251883Speter** With SQLITE_ACCESS_EXISTS, the xAccess method 1280251883Speter** simply checks whether the file exists. 1281251883Speter** With SQLITE_ACCESS_READWRITE, the xAccess method 1282251883Speter** checks whether the named directory is both readable and writable 1283251883Speter** (in other words, if files can be added, removed, and renamed within 1284251883Speter** the directory). 1285251883Speter** The SQLITE_ACCESS_READWRITE constant is currently used only by the 1286251883Speter** [temp_store_directory pragma], though this could change in a future 1287251883Speter** release of SQLite. 1288251883Speter** With SQLITE_ACCESS_READ, the xAccess method 1289251883Speter** checks whether the file is readable. The SQLITE_ACCESS_READ constant is 1290251883Speter** currently unused, though it might be used in a future release of 1291251883Speter** SQLite. 1292251883Speter*/ 1293251883Speter#define SQLITE_ACCESS_EXISTS 0 1294251883Speter#define SQLITE_ACCESS_READWRITE 1 /* Used by PRAGMA temp_store_directory */ 1295251883Speter#define SQLITE_ACCESS_READ 2 /* Unused */ 1296251883Speter 1297251883Speter/* 1298251883Speter** CAPI3REF: Flags for the xShmLock VFS method 1299251883Speter** 1300251883Speter** These integer constants define the various locking operations 1301251883Speter** allowed by the xShmLock method of [sqlite3_io_methods]. The 1302251883Speter** following are the only legal combinations of flags to the 1303251883Speter** xShmLock method: 1304251883Speter** 1305251883Speter** <ul> 1306251883Speter** <li> SQLITE_SHM_LOCK | SQLITE_SHM_SHARED 1307251883Speter** <li> SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE 1308251883Speter** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED 1309251883Speter** <li> SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE 1310251883Speter** </ul> 1311251883Speter** 1312251883Speter** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as 1313282328Sbapt** was given on the corresponding lock. 1314251883Speter** 1315251883Speter** The xShmLock method can transition between unlocked and SHARED or 1316251883Speter** between unlocked and EXCLUSIVE. It cannot transition between SHARED 1317251883Speter** and EXCLUSIVE. 1318251883Speter*/ 1319251883Speter#define SQLITE_SHM_UNLOCK 1 1320251883Speter#define SQLITE_SHM_LOCK 2 1321251883Speter#define SQLITE_SHM_SHARED 4 1322251883Speter#define SQLITE_SHM_EXCLUSIVE 8 1323251883Speter 1324251883Speter/* 1325251883Speter** CAPI3REF: Maximum xShmLock index 1326251883Speter** 1327251883Speter** The xShmLock method on [sqlite3_io_methods] may use values 1328251883Speter** between 0 and this upper bound as its "offset" argument. 1329251883Speter** The SQLite core will never attempt to acquire or release a 1330251883Speter** lock outside of this range 1331251883Speter*/ 1332251883Speter#define SQLITE_SHM_NLOCK 8 1333251883Speter 1334251883Speter 1335251883Speter/* 1336251883Speter** CAPI3REF: Initialize The SQLite Library 1337251883Speter** 1338251883Speter** ^The sqlite3_initialize() routine initializes the 1339251883Speter** SQLite library. ^The sqlite3_shutdown() routine 1340251883Speter** deallocates any resources that were allocated by sqlite3_initialize(). 1341251883Speter** These routines are designed to aid in process initialization and 1342251883Speter** shutdown on embedded systems. Workstation applications using 1343251883Speter** SQLite normally do not need to invoke either of these routines. 1344251883Speter** 1345251883Speter** A call to sqlite3_initialize() is an "effective" call if it is 1346251883Speter** the first time sqlite3_initialize() is invoked during the lifetime of 1347251883Speter** the process, or if it is the first time sqlite3_initialize() is invoked 1348251883Speter** following a call to sqlite3_shutdown(). ^(Only an effective call 1349251883Speter** of sqlite3_initialize() does any initialization. All other calls 1350251883Speter** are harmless no-ops.)^ 1351251883Speter** 1352251883Speter** A call to sqlite3_shutdown() is an "effective" call if it is the first 1353251883Speter** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only 1354251883Speter** an effective call to sqlite3_shutdown() does any deinitialization. 1355251883Speter** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^ 1356251883Speter** 1357251883Speter** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown() 1358251883Speter** is not. The sqlite3_shutdown() interface must only be called from a 1359251883Speter** single thread. All open [database connections] must be closed and all 1360251883Speter** other SQLite resources must be deallocated prior to invoking 1361251883Speter** sqlite3_shutdown(). 1362251883Speter** 1363251883Speter** Among other things, ^sqlite3_initialize() will invoke 1364251883Speter** sqlite3_os_init(). Similarly, ^sqlite3_shutdown() 1365251883Speter** will invoke sqlite3_os_end(). 1366251883Speter** 1367251883Speter** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success. 1368251883Speter** ^If for some reason, sqlite3_initialize() is unable to initialize 1369251883Speter** the library (perhaps it is unable to allocate a needed resource such 1370251883Speter** as a mutex) it returns an [error code] other than [SQLITE_OK]. 1371251883Speter** 1372251883Speter** ^The sqlite3_initialize() routine is called internally by many other 1373251883Speter** SQLite interfaces so that an application usually does not need to 1374251883Speter** invoke sqlite3_initialize() directly. For example, [sqlite3_open()] 1375251883Speter** calls sqlite3_initialize() so the SQLite library will be automatically 1376251883Speter** initialized when [sqlite3_open()] is called if it has not be initialized 1377251883Speter** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT] 1378251883Speter** compile-time option, then the automatic calls to sqlite3_initialize() 1379251883Speter** are omitted and the application must call sqlite3_initialize() directly 1380251883Speter** prior to using any other SQLite interface. For maximum portability, 1381251883Speter** it is recommended that applications always invoke sqlite3_initialize() 1382251883Speter** directly prior to using any other SQLite interface. Future releases 1383251883Speter** of SQLite may require this. In other words, the behavior exhibited 1384251883Speter** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the 1385251883Speter** default behavior in some future release of SQLite. 1386251883Speter** 1387251883Speter** The sqlite3_os_init() routine does operating-system specific 1388251883Speter** initialization of the SQLite library. The sqlite3_os_end() 1389251883Speter** routine undoes the effect of sqlite3_os_init(). Typical tasks 1390251883Speter** performed by these routines include allocation or deallocation 1391251883Speter** of static resources, initialization of global variables, 1392251883Speter** setting up a default [sqlite3_vfs] module, or setting up 1393251883Speter** a default configuration using [sqlite3_config()]. 1394251883Speter** 1395251883Speter** The application should never invoke either sqlite3_os_init() 1396251883Speter** or sqlite3_os_end() directly. The application should only invoke 1397251883Speter** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init() 1398251883Speter** interface is called automatically by sqlite3_initialize() and 1399251883Speter** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate 1400251883Speter** implementations for sqlite3_os_init() and sqlite3_os_end() 1401251883Speter** are built into SQLite when it is compiled for Unix, Windows, or OS/2. 1402251883Speter** When [custom builds | built for other platforms] 1403251883Speter** (using the [SQLITE_OS_OTHER=1] compile-time 1404251883Speter** option) the application must supply a suitable implementation for 1405251883Speter** sqlite3_os_init() and sqlite3_os_end(). An application-supplied 1406251883Speter** implementation of sqlite3_os_init() or sqlite3_os_end() 1407251883Speter** must return [SQLITE_OK] on success and some other [error code] upon 1408251883Speter** failure. 1409251883Speter*/ 1410322444SpeterSQLITE_API int sqlite3_initialize(void); 1411322444SpeterSQLITE_API int sqlite3_shutdown(void); 1412322444SpeterSQLITE_API int sqlite3_os_init(void); 1413322444SpeterSQLITE_API int sqlite3_os_end(void); 1414251883Speter 1415251883Speter/* 1416251883Speter** CAPI3REF: Configuring The SQLite Library 1417251883Speter** 1418251883Speter** The sqlite3_config() interface is used to make global configuration 1419251883Speter** changes to SQLite in order to tune SQLite to the specific needs of 1420251883Speter** the application. The default configuration is recommended for most 1421251883Speter** applications and so this routine is usually not necessary. It is 1422251883Speter** provided to support rare applications with unusual needs. 1423251883Speter** 1424298161Sbapt** <b>The sqlite3_config() interface is not threadsafe. The application 1425298161Sbapt** must ensure that no other SQLite interfaces are invoked by other 1426298161Sbapt** threads while sqlite3_config() is running.</b> 1427298161Sbapt** 1428298161Sbapt** The sqlite3_config() interface 1429251883Speter** may only be invoked prior to library initialization using 1430251883Speter** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()]. 1431251883Speter** ^If sqlite3_config() is called after [sqlite3_initialize()] and before 1432251883Speter** [sqlite3_shutdown()] then it will return SQLITE_MISUSE. 1433251883Speter** Note, however, that ^sqlite3_config() can be called as part of the 1434251883Speter** implementation of an application-defined [sqlite3_os_init()]. 1435251883Speter** 1436251883Speter** The first argument to sqlite3_config() is an integer 1437251883Speter** [configuration option] that determines 1438251883Speter** what property of SQLite is to be configured. Subsequent arguments 1439251883Speter** vary depending on the [configuration option] 1440251883Speter** in the first argument. 1441251883Speter** 1442251883Speter** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK]. 1443251883Speter** ^If the option is unknown or SQLite is unable to set the option 1444251883Speter** then this routine returns a non-zero [error code]. 1445251883Speter*/ 1446322444SpeterSQLITE_API int sqlite3_config(int, ...); 1447251883Speter 1448251883Speter/* 1449251883Speter** CAPI3REF: Configure database connections 1450286510Speter** METHOD: sqlite3 1451251883Speter** 1452251883Speter** The sqlite3_db_config() interface is used to make configuration 1453251883Speter** changes to a [database connection]. The interface is similar to 1454251883Speter** [sqlite3_config()] except that the changes apply to a single 1455251883Speter** [database connection] (specified in the first argument). 1456251883Speter** 1457251883Speter** The second argument to sqlite3_db_config(D,V,...) is the 1458251883Speter** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code 1459251883Speter** that indicates what aspect of the [database connection] is being configured. 1460251883Speter** Subsequent arguments vary depending on the configuration verb. 1461251883Speter** 1462251883Speter** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if 1463251883Speter** the call is considered successful. 1464251883Speter*/ 1465322444SpeterSQLITE_API int sqlite3_db_config(sqlite3*, int op, ...); 1466251883Speter 1467251883Speter/* 1468251883Speter** CAPI3REF: Memory Allocation Routines 1469251883Speter** 1470251883Speter** An instance of this object defines the interface between SQLite 1471251883Speter** and low-level memory allocation routines. 1472251883Speter** 1473251883Speter** This object is used in only one place in the SQLite interface. 1474251883Speter** A pointer to an instance of this object is the argument to 1475251883Speter** [sqlite3_config()] when the configuration option is 1476251883Speter** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC]. 1477251883Speter** By creating an instance of this object 1478251883Speter** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC]) 1479251883Speter** during configuration, an application can specify an alternative 1480251883Speter** memory allocation subsystem for SQLite to use for all of its 1481251883Speter** dynamic memory needs. 1482251883Speter** 1483251883Speter** Note that SQLite comes with several [built-in memory allocators] 1484251883Speter** that are perfectly adequate for the overwhelming majority of applications 1485251883Speter** and that this object is only useful to a tiny minority of applications 1486251883Speter** with specialized memory allocation requirements. This object is 1487251883Speter** also used during testing of SQLite in order to specify an alternative 1488251883Speter** memory allocator that simulates memory out-of-memory conditions in 1489251883Speter** order to verify that SQLite recovers gracefully from such 1490251883Speter** conditions. 1491251883Speter** 1492251883Speter** The xMalloc, xRealloc, and xFree methods must work like the 1493251883Speter** malloc(), realloc() and free() functions from the standard C library. 1494251883Speter** ^SQLite guarantees that the second argument to 1495251883Speter** xRealloc is always a value returned by a prior call to xRoundup. 1496251883Speter** 1497251883Speter** xSize should return the allocated size of a memory allocation 1498251883Speter** previously obtained from xMalloc or xRealloc. The allocated size 1499251883Speter** is always at least as big as the requested size but may be larger. 1500251883Speter** 1501251883Speter** The xRoundup method returns what would be the allocated size of 1502251883Speter** a memory allocation given a particular requested size. Most memory 1503251883Speter** allocators round up memory allocations at least to the next multiple 1504251883Speter** of 8. Some allocators round up to a larger multiple or to a power of 2. 1505251883Speter** Every memory allocation request coming in through [sqlite3_malloc()] 1506251883Speter** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0, 1507251883Speter** that causes the corresponding memory allocation to fail. 1508251883Speter** 1509269851Speter** The xInit method initializes the memory allocator. For example, 1510251883Speter** it might allocate any require mutexes or initialize internal data 1511251883Speter** structures. The xShutdown method is invoked (indirectly) by 1512251883Speter** [sqlite3_shutdown()] and should deallocate any resources acquired 1513251883Speter** by xInit. The pAppData pointer is used as the only parameter to 1514251883Speter** xInit and xShutdown. 1515251883Speter** 1516251883Speter** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes 1517251883Speter** the xInit method, so the xInit method need not be threadsafe. The 1518251883Speter** xShutdown method is only called from [sqlite3_shutdown()] so it does 1519251883Speter** not need to be threadsafe either. For all other methods, SQLite 1520251883Speter** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the 1521251883Speter** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which 1522251883Speter** it is by default) and so the methods are automatically serialized. 1523251883Speter** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other 1524251883Speter** methods must be threadsafe or else make their own arrangements for 1525251883Speter** serialization. 1526251883Speter** 1527251883Speter** SQLite will never invoke xInit() more than once without an intervening 1528251883Speter** call to xShutdown(). 1529251883Speter*/ 1530251883Spetertypedef struct sqlite3_mem_methods sqlite3_mem_methods; 1531251883Speterstruct sqlite3_mem_methods { 1532251883Speter void *(*xMalloc)(int); /* Memory allocation function */ 1533251883Speter void (*xFree)(void*); /* Free a prior allocation */ 1534251883Speter void *(*xRealloc)(void*,int); /* Resize an allocation */ 1535251883Speter int (*xSize)(void*); /* Return the size of an allocation */ 1536251883Speter int (*xRoundup)(int); /* Round up request size to allocation size */ 1537251883Speter int (*xInit)(void*); /* Initialize the memory allocator */ 1538251883Speter void (*xShutdown)(void*); /* Deinitialize the memory allocator */ 1539251883Speter void *pAppData; /* Argument to xInit() and xShutdown() */ 1540251883Speter}; 1541251883Speter 1542251883Speter/* 1543251883Speter** CAPI3REF: Configuration Options 1544251883Speter** KEYWORDS: {configuration option} 1545251883Speter** 1546251883Speter** These constants are the available integer configuration options that 1547251883Speter** can be passed as the first argument to the [sqlite3_config()] interface. 1548251883Speter** 1549251883Speter** New configuration options may be added in future releases of SQLite. 1550251883Speter** Existing configuration options might be discontinued. Applications 1551251883Speter** should check the return code from [sqlite3_config()] to make sure that 1552251883Speter** the call worked. The [sqlite3_config()] interface will return a 1553251883Speter** non-zero [error code] if a discontinued or unsupported configuration option 1554251883Speter** is invoked. 1555251883Speter** 1556251883Speter** <dl> 1557251883Speter** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt> 1558251883Speter** <dd>There are no arguments to this option. ^This option sets the 1559251883Speter** [threading mode] to Single-thread. In other words, it disables 1560251883Speter** all mutexing and puts SQLite into a mode where it can only be used 1561251883Speter** by a single thread. ^If SQLite is compiled with 1562251883Speter** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then 1563251883Speter** it is not possible to change the [threading mode] from its default 1564251883Speter** value of Single-thread and so [sqlite3_config()] will return 1565251883Speter** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD 1566251883Speter** configuration option.</dd> 1567251883Speter** 1568251883Speter** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt> 1569251883Speter** <dd>There are no arguments to this option. ^This option sets the 1570251883Speter** [threading mode] to Multi-thread. In other words, it disables 1571251883Speter** mutexing on [database connection] and [prepared statement] objects. 1572251883Speter** The application is responsible for serializing access to 1573251883Speter** [database connections] and [prepared statements]. But other mutexes 1574251883Speter** are enabled so that SQLite will be safe to use in a multi-threaded 1575251883Speter** environment as long as no two threads attempt to use the same 1576251883Speter** [database connection] at the same time. ^If SQLite is compiled with 1577251883Speter** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then 1578251883Speter** it is not possible to set the Multi-thread [threading mode] and 1579251883Speter** [sqlite3_config()] will return [SQLITE_ERROR] if called with the 1580251883Speter** SQLITE_CONFIG_MULTITHREAD configuration option.</dd> 1581251883Speter** 1582251883Speter** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt> 1583251883Speter** <dd>There are no arguments to this option. ^This option sets the 1584251883Speter** [threading mode] to Serialized. In other words, this option enables 1585251883Speter** all mutexes including the recursive 1586251883Speter** mutexes on [database connection] and [prepared statement] objects. 1587251883Speter** In this mode (which is the default when SQLite is compiled with 1588251883Speter** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access 1589251883Speter** to [database connections] and [prepared statements] so that the 1590251883Speter** application is free to use the same [database connection] or the 1591251883Speter** same [prepared statement] in different threads at the same time. 1592251883Speter** ^If SQLite is compiled with 1593251883Speter** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then 1594251883Speter** it is not possible to set the Serialized [threading mode] and 1595251883Speter** [sqlite3_config()] will return [SQLITE_ERROR] if called with the 1596251883Speter** SQLITE_CONFIG_SERIALIZED configuration option.</dd> 1597251883Speter** 1598251883Speter** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt> 1599282328Sbapt** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is 1600282328Sbapt** a pointer to an instance of the [sqlite3_mem_methods] structure. 1601282328Sbapt** The argument specifies 1602251883Speter** alternative low-level memory allocation routines to be used in place of 1603251883Speter** the memory allocation routines built into SQLite.)^ ^SQLite makes 1604251883Speter** its own private copy of the content of the [sqlite3_mem_methods] structure 1605251883Speter** before the [sqlite3_config()] call returns.</dd> 1606251883Speter** 1607251883Speter** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt> 1608282328Sbapt** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which 1609282328Sbapt** is a pointer to an instance of the [sqlite3_mem_methods] structure. 1610282328Sbapt** The [sqlite3_mem_methods] 1611251883Speter** structure is filled with the currently defined memory allocation routines.)^ 1612251883Speter** This option can be used to overload the default memory allocation 1613251883Speter** routines with a wrapper that simulations memory allocation failure or 1614251883Speter** tracks memory usage, for example. </dd> 1615251883Speter** 1616251883Speter** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt> 1617282328Sbapt** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int, 1618282328Sbapt** interpreted as a boolean, which enables or disables the collection of 1619282328Sbapt** memory allocation statistics. ^(When memory allocation statistics are 1620282328Sbapt** disabled, the following SQLite interfaces become non-operational: 1621251883Speter** <ul> 1622251883Speter** <li> [sqlite3_memory_used()] 1623251883Speter** <li> [sqlite3_memory_highwater()] 1624251883Speter** <li> [sqlite3_soft_heap_limit64()] 1625282328Sbapt** <li> [sqlite3_status64()] 1626251883Speter** </ul>)^ 1627251883Speter** ^Memory allocation statistics are enabled by default unless SQLite is 1628251883Speter** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory 1629251883Speter** allocation statistics are disabled by default. 1630251883Speter** </dd> 1631251883Speter** 1632251883Speter** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt> 1633282328Sbapt** <dd> ^The SQLITE_CONFIG_SCRATCH option specifies a static memory buffer 1634282328Sbapt** that SQLite can use for scratch memory. ^(There are three arguments 1635282328Sbapt** to SQLITE_CONFIG_SCRATCH: A pointer an 8-byte 1636251883Speter** aligned memory buffer from which the scratch allocations will be 1637251883Speter** drawn, the size of each scratch allocation (sz), 1638282328Sbapt** and the maximum number of scratch allocations (N).)^ 1639251883Speter** The first argument must be a pointer to an 8-byte aligned buffer 1640251883Speter** of at least sz*N bytes of memory. 1641282328Sbapt** ^SQLite will not use more than one scratch buffers per thread. 1642282328Sbapt** ^SQLite will never request a scratch buffer that is more than 6 1643282328Sbapt** times the database page size. 1644282328Sbapt** ^If SQLite needs needs additional 1645251883Speter** scratch memory beyond what is provided by this configuration option, then 1646282328Sbapt** [sqlite3_malloc()] will be used to obtain the memory needed.<p> 1647282328Sbapt** ^When the application provides any amount of scratch memory using 1648282328Sbapt** SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary large 1649282328Sbapt** [sqlite3_malloc|heap allocations]. 1650282328Sbapt** This can help [Robson proof|prevent memory allocation failures] due to heap 1651282328Sbapt** fragmentation in low-memory embedded systems. 1652282328Sbapt** </dd> 1653251883Speter** 1654251883Speter** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt> 1655298161Sbapt** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool 1656282328Sbapt** that SQLite can use for the database page cache with the default page 1657282328Sbapt** cache implementation. 1658298161Sbapt** This configuration option is a no-op if an application-define page 1659298161Sbapt** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2]. 1660282328Sbapt** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to 1661298161Sbapt** 8-byte aligned memory (pMem), the size of each page cache line (sz), 1662298161Sbapt** and the number of cache lines (N). 1663251883Speter** The sz argument should be the size of the largest database page 1664282328Sbapt** (a power of two between 512 and 65536) plus some extra bytes for each 1665282328Sbapt** page header. ^The number of extra bytes needed by the page header 1666298161Sbapt** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ]. 1667282328Sbapt** ^It is harmless, apart from the wasted memory, 1668298161Sbapt** for the sz parameter to be larger than necessary. The pMem 1669298161Sbapt** argument must be either a NULL pointer or a pointer to an 8-byte 1670298161Sbapt** aligned block of memory of at least sz*N bytes, otherwise 1671298161Sbapt** subsequent behavior is undefined. 1672298161Sbapt** ^When pMem is not NULL, SQLite will strive to use the memory provided 1673298161Sbapt** to satisfy page cache needs, falling back to [sqlite3_malloc()] if 1674298161Sbapt** a page cache line is larger than sz bytes or if all of the pMem buffer 1675298161Sbapt** is exhausted. 1676298161Sbapt** ^If pMem is NULL and N is non-zero, then each database connection 1677298161Sbapt** does an initial bulk allocation for page cache memory 1678298161Sbapt** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or 1679298161Sbapt** of -1024*N bytes if N is negative, . ^If additional 1680298161Sbapt** page cache memory is needed beyond what is provided by the initial 1681298161Sbapt** allocation, then SQLite goes to [sqlite3_malloc()] separately for each 1682298161Sbapt** additional cache line. </dd> 1683251883Speter** 1684251883Speter** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt> 1685282328Sbapt** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer 1686282328Sbapt** that SQLite will use for all of its dynamic memory allocation needs 1687282328Sbapt** beyond those provided for by [SQLITE_CONFIG_SCRATCH] and 1688282328Sbapt** [SQLITE_CONFIG_PAGECACHE]. 1689282328Sbapt** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled 1690282328Sbapt** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns 1691282328Sbapt** [SQLITE_ERROR] if invoked otherwise. 1692282328Sbapt** ^There are three arguments to SQLITE_CONFIG_HEAP: 1693282328Sbapt** An 8-byte aligned pointer to the memory, 1694251883Speter** the number of bytes in the memory buffer, and the minimum allocation size. 1695251883Speter** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts 1696251883Speter** to using its default memory allocator (the system malloc() implementation), 1697251883Speter** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the 1698282328Sbapt** memory pointer is not NULL then the alternative memory 1699251883Speter** allocator is engaged to handle all of SQLites memory allocation needs. 1700251883Speter** The first pointer (the memory pointer) must be aligned to an 8-byte 1701251883Speter** boundary or subsequent behavior of SQLite will be undefined. 1702251883Speter** The minimum allocation size is capped at 2**12. Reasonable values 1703251883Speter** for the minimum allocation size are 2**5 through 2**8.</dd> 1704251883Speter** 1705251883Speter** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt> 1706282328Sbapt** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a 1707282328Sbapt** pointer to an instance of the [sqlite3_mutex_methods] structure. 1708282328Sbapt** The argument specifies alternative low-level mutex routines to be used 1709282328Sbapt** in place the mutex routines built into SQLite.)^ ^SQLite makes a copy of 1710282328Sbapt** the content of the [sqlite3_mutex_methods] structure before the call to 1711251883Speter** [sqlite3_config()] returns. ^If SQLite is compiled with 1712251883Speter** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then 1713251883Speter** the entire mutexing subsystem is omitted from the build and hence calls to 1714251883Speter** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will 1715251883Speter** return [SQLITE_ERROR].</dd> 1716251883Speter** 1717251883Speter** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt> 1718282328Sbapt** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which 1719282328Sbapt** is a pointer to an instance of the [sqlite3_mutex_methods] structure. The 1720251883Speter** [sqlite3_mutex_methods] 1721251883Speter** structure is filled with the currently defined mutex routines.)^ 1722251883Speter** This option can be used to overload the default mutex allocation 1723251883Speter** routines with a wrapper used to track mutex usage for performance 1724251883Speter** profiling or testing, for example. ^If SQLite is compiled with 1725251883Speter** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then 1726251883Speter** the entire mutexing subsystem is omitted from the build and hence calls to 1727251883Speter** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will 1728251883Speter** return [SQLITE_ERROR].</dd> 1729251883Speter** 1730251883Speter** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt> 1731282328Sbapt** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine 1732282328Sbapt** the default size of lookaside memory on each [database connection]. 1733282328Sbapt** The first argument is the 1734251883Speter** size of each lookaside buffer slot and the second is the number of 1735282328Sbapt** slots allocated to each database connection.)^ ^(SQLITE_CONFIG_LOOKASIDE 1736282328Sbapt** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] 1737282328Sbapt** option to [sqlite3_db_config()] can be used to change the lookaside 1738251883Speter** configuration on individual connections.)^ </dd> 1739251883Speter** 1740251883Speter** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt> 1741282328Sbapt** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is 1742282328Sbapt** a pointer to an [sqlite3_pcache_methods2] object. This object specifies 1743282328Sbapt** the interface to a custom page cache implementation.)^ 1744282328Sbapt** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd> 1745251883Speter** 1746251883Speter** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt> 1747282328Sbapt** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which 1748282328Sbapt** is a pointer to an [sqlite3_pcache_methods2] object. SQLite copies of 1749282328Sbapt** the current page cache implementation into that object.)^ </dd> 1750251883Speter** 1751251883Speter** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt> 1752251883Speter** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite 1753251883Speter** global [error log]. 1754251883Speter** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a 1755251883Speter** function with a call signature of void(*)(void*,int,const char*), 1756251883Speter** and a pointer to void. ^If the function pointer is not NULL, it is 1757251883Speter** invoked by [sqlite3_log()] to process each logging event. ^If the 1758251883Speter** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op. 1759251883Speter** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is 1760251883Speter** passed through as the first parameter to the application-defined logger 1761251883Speter** function whenever that function is invoked. ^The second parameter to 1762251883Speter** the logger function is a copy of the first parameter to the corresponding 1763251883Speter** [sqlite3_log()] call and is intended to be a [result code] or an 1764251883Speter** [extended result code]. ^The third parameter passed to the logger is 1765251883Speter** log message after formatting via [sqlite3_snprintf()]. 1766251883Speter** The SQLite logging interface is not reentrant; the logger function 1767251883Speter** supplied by the application must not invoke any SQLite interface. 1768251883Speter** In a multi-threaded application, the application-defined logger 1769251883Speter** function must be threadsafe. </dd> 1770251883Speter** 1771251883Speter** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI 1772282328Sbapt** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int. 1773282328Sbapt** If non-zero, then URI handling is globally enabled. If the parameter is zero, 1774282328Sbapt** then URI handling is globally disabled.)^ ^If URI handling is globally 1775282328Sbapt** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()], 1776282328Sbapt** [sqlite3_open16()] or 1777251883Speter** specified as part of [ATTACH] commands are interpreted as URIs, regardless 1778251883Speter** of whether or not the [SQLITE_OPEN_URI] flag is set when the database 1779269851Speter** connection is opened. ^If it is globally disabled, filenames are 1780251883Speter** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the 1781269851Speter** database connection is opened. ^(By default, URI handling is globally 1782251883Speter** disabled. The default value may be changed by compiling with the 1783269851Speter** [SQLITE_USE_URI] symbol defined.)^ 1784251883Speter** 1785251883Speter** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN 1786282328Sbapt** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer 1787282328Sbapt** argument which is interpreted as a boolean in order to enable or disable 1788282328Sbapt** the use of covering indices for full table scans in the query optimizer. 1789282328Sbapt** ^The default setting is determined 1790251883Speter** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on" 1791251883Speter** if that compile-time option is omitted. 1792251883Speter** The ability to disable the use of covering indices for full table scans 1793251883Speter** is because some incorrectly coded legacy applications might malfunction 1794269851Speter** when the optimization is enabled. Providing the ability to 1795251883Speter** disable the optimization allows the older, buggy application code to work 1796251883Speter** without change even with newer versions of SQLite. 1797251883Speter** 1798251883Speter** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]] 1799251883Speter** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE 1800251883Speter** <dd> These options are obsolete and should not be used by new code. 1801251883Speter** They are retained for backwards compatibility but are now no-ops. 1802251883Speter** </dd> 1803251883Speter** 1804251883Speter** [[SQLITE_CONFIG_SQLLOG]] 1805251883Speter** <dt>SQLITE_CONFIG_SQLLOG 1806251883Speter** <dd>This option is only available if sqlite is compiled with the 1807251883Speter** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should 1808251883Speter** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int). 1809251883Speter** The second should be of type (void*). The callback is invoked by the library 1810251883Speter** in three separate circumstances, identified by the value passed as the 1811251883Speter** fourth parameter. If the fourth parameter is 0, then the database connection 1812251883Speter** passed as the second argument has just been opened. The third argument 1813251883Speter** points to a buffer containing the name of the main database file. If the 1814251883Speter** fourth parameter is 1, then the SQL statement that the third parameter 1815251883Speter** points to has just been executed. Or, if the fourth parameter is 2, then 1816251883Speter** the connection being passed as the second parameter is being closed. The 1817251883Speter** third parameter is passed NULL In this case. An example of using this 1818251883Speter** configuration option can be seen in the "test_sqllog.c" source file in 1819251883Speter** the canonical SQLite source tree.</dd> 1820251883Speter** 1821251883Speter** [[SQLITE_CONFIG_MMAP_SIZE]] 1822251883Speter** <dt>SQLITE_CONFIG_MMAP_SIZE 1823269851Speter** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values 1824251883Speter** that are the default mmap size limit (the default setting for 1825251883Speter** [PRAGMA mmap_size]) and the maximum allowed mmap size limit. 1826269851Speter** ^The default setting can be overridden by each database connection using 1827251883Speter** either the [PRAGMA mmap_size] command, or by using the 1828269851Speter** [SQLITE_FCNTL_MMAP_SIZE] file control. ^(The maximum allowed mmap size 1829282328Sbapt** will be silently truncated if necessary so that it does not exceed the 1830282328Sbapt** compile-time maximum mmap size set by the 1831269851Speter** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^ 1832269851Speter** ^If either argument to this option is negative, then that argument is 1833251883Speter** changed to its compile-time default. 1834269851Speter** 1835269851Speter** [[SQLITE_CONFIG_WIN32_HEAPSIZE]] 1836269851Speter** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE 1837282328Sbapt** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is 1838282328Sbapt** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro 1839282328Sbapt** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value 1840269851Speter** that specifies the maximum size of the created heap. 1841282328Sbapt** 1842282328Sbapt** [[SQLITE_CONFIG_PCACHE_HDRSZ]] 1843282328Sbapt** <dt>SQLITE_CONFIG_PCACHE_HDRSZ 1844282328Sbapt** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which 1845282328Sbapt** is a pointer to an integer and writes into that integer the number of extra 1846282328Sbapt** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE]. 1847282328Sbapt** The amount of extra space required can change depending on the compiler, 1848282328Sbapt** target platform, and SQLite version. 1849282328Sbapt** 1850282328Sbapt** [[SQLITE_CONFIG_PMASZ]] 1851282328Sbapt** <dt>SQLITE_CONFIG_PMASZ 1852282328Sbapt** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which 1853282328Sbapt** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded 1854282328Sbapt** sorter to that integer. The default minimum PMA Size is set by the 1855282328Sbapt** [SQLITE_SORTER_PMASZ] compile-time option. New threads are launched 1856282328Sbapt** to help with sort operations when multithreaded sorting 1857282328Sbapt** is enabled (using the [PRAGMA threads] command) and the amount of content 1858282328Sbapt** to be sorted exceeds the page size times the minimum of the 1859282328Sbapt** [PRAGMA cache_size] setting and this value. 1860298161Sbapt** 1861298161Sbapt** [[SQLITE_CONFIG_STMTJRNL_SPILL]] 1862298161Sbapt** <dt>SQLITE_CONFIG_STMTJRNL_SPILL 1863298161Sbapt** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which 1864298161Sbapt** becomes the [statement journal] spill-to-disk threshold. 1865298161Sbapt** [Statement journals] are held in memory until their size (in bytes) 1866298161Sbapt** exceeds this threshold, at which point they are written to disk. 1867298161Sbapt** Or if the threshold is -1, statement journals are always held 1868298161Sbapt** exclusively in memory. 1869298161Sbapt** Since many statement journals never become large, setting the spill 1870298161Sbapt** threshold to a value such as 64KiB can greatly reduce the amount of 1871298161Sbapt** I/O required to support statement rollback. 1872298161Sbapt** The default value for this setting is controlled by the 1873298161Sbapt** [SQLITE_STMTJRNL_SPILL] compile-time option. 1874251883Speter** </dl> 1875251883Speter*/ 1876251883Speter#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ 1877251883Speter#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ 1878251883Speter#define SQLITE_CONFIG_SERIALIZED 3 /* nil */ 1879251883Speter#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ 1880251883Speter#define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */ 1881251883Speter#define SQLITE_CONFIG_SCRATCH 6 /* void*, int sz, int N */ 1882251883Speter#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */ 1883251883Speter#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */ 1884251883Speter#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */ 1885251883Speter#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */ 1886251883Speter#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */ 1887251883Speter/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ 1888251883Speter#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */ 1889251883Speter#define SQLITE_CONFIG_PCACHE 14 /* no-op */ 1890251883Speter#define SQLITE_CONFIG_GETPCACHE 15 /* no-op */ 1891251883Speter#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */ 1892251883Speter#define SQLITE_CONFIG_URI 17 /* int */ 1893251883Speter#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */ 1894251883Speter#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */ 1895251883Speter#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */ 1896251883Speter#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */ 1897251883Speter#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */ 1898269851Speter#define SQLITE_CONFIG_WIN32_HEAPSIZE 23 /* int nByte */ 1899282328Sbapt#define SQLITE_CONFIG_PCACHE_HDRSZ 24 /* int *psz */ 1900282328Sbapt#define SQLITE_CONFIG_PMASZ 25 /* unsigned int szPma */ 1901298161Sbapt#define SQLITE_CONFIG_STMTJRNL_SPILL 26 /* int nByte */ 1902251883Speter 1903251883Speter/* 1904251883Speter** CAPI3REF: Database Connection Configuration Options 1905251883Speter** 1906251883Speter** These constants are the available integer configuration options that 1907251883Speter** can be passed as the second argument to the [sqlite3_db_config()] interface. 1908251883Speter** 1909251883Speter** New configuration options may be added in future releases of SQLite. 1910251883Speter** Existing configuration options might be discontinued. Applications 1911251883Speter** should check the return code from [sqlite3_db_config()] to make sure that 1912251883Speter** the call worked. ^The [sqlite3_db_config()] interface will return a 1913251883Speter** non-zero [error code] if a discontinued or unsupported configuration option 1914251883Speter** is invoked. 1915251883Speter** 1916251883Speter** <dl> 1917251883Speter** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt> 1918251883Speter** <dd> ^This option takes three additional arguments that determine the 1919251883Speter** [lookaside memory allocator] configuration for the [database connection]. 1920251883Speter** ^The first argument (the third parameter to [sqlite3_db_config()] is a 1921251883Speter** pointer to a memory buffer to use for lookaside memory. 1922251883Speter** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb 1923251883Speter** may be NULL in which case SQLite will allocate the 1924251883Speter** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the 1925251883Speter** size of each lookaside buffer slot. ^The third argument is the number of 1926251883Speter** slots. The size of the buffer in the first argument must be greater than 1927251883Speter** or equal to the product of the second and third arguments. The buffer 1928251883Speter** must be aligned to an 8-byte boundary. ^If the second argument to 1929251883Speter** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally 1930251883Speter** rounded down to the next smaller multiple of 8. ^(The lookaside memory 1931251883Speter** configuration for a database connection can only be changed when that 1932251883Speter** connection is not currently using lookaside memory, or in other words 1933251883Speter** when the "current value" returned by 1934251883Speter** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero. 1935251883Speter** Any attempt to change the lookaside memory configuration when lookaside 1936251883Speter** memory is in use leaves the configuration unchanged and returns 1937251883Speter** [SQLITE_BUSY].)^</dd> 1938251883Speter** 1939251883Speter** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt> 1940251883Speter** <dd> ^This option is used to enable or disable the enforcement of 1941251883Speter** [foreign key constraints]. There should be two additional arguments. 1942251883Speter** The first argument is an integer which is 0 to disable FK enforcement, 1943251883Speter** positive to enable FK enforcement or negative to leave FK enforcement 1944251883Speter** unchanged. The second parameter is a pointer to an integer into which 1945251883Speter** is written 0 or 1 to indicate whether FK enforcement is off or on 1946251883Speter** following this call. The second parameter may be a NULL pointer, in 1947251883Speter** which case the FK enforcement setting is not reported back. </dd> 1948251883Speter** 1949251883Speter** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt> 1950251883Speter** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers]. 1951251883Speter** There should be two additional arguments. 1952251883Speter** The first argument is an integer which is 0 to disable triggers, 1953251883Speter** positive to enable triggers or negative to leave the setting unchanged. 1954251883Speter** The second parameter is a pointer to an integer into which 1955251883Speter** is written 0 or 1 to indicate whether triggers are disabled or enabled 1956251883Speter** following this call. The second parameter may be a NULL pointer, in 1957251883Speter** which case the trigger setting is not reported back. </dd> 1958251883Speter** 1959298161Sbapt** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt> 1960298161Sbapt** <dd> ^This option is used to enable or disable the two-argument 1961298161Sbapt** version of the [fts3_tokenizer()] function which is part of the 1962298161Sbapt** [FTS3] full-text search engine extension. 1963298161Sbapt** There should be two additional arguments. 1964298161Sbapt** The first argument is an integer which is 0 to disable fts3_tokenizer() or 1965298161Sbapt** positive to enable fts3_tokenizer() or negative to leave the setting 1966298161Sbapt** unchanged. 1967298161Sbapt** The second parameter is a pointer to an integer into which 1968298161Sbapt** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled 1969298161Sbapt** following this call. The second parameter may be a NULL pointer, in 1970298161Sbapt** which case the new setting is not reported back. </dd> 1971298161Sbapt** 1972305002Scy** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt> 1973305002Scy** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()] 1974305002Scy** interface independently of the [load_extension()] SQL function. 1975305002Scy** The [sqlite3_enable_load_extension()] API enables or disables both the 1976305002Scy** C-API [sqlite3_load_extension()] and the SQL function [load_extension()]. 1977305002Scy** There should be two additional arguments. 1978305002Scy** When the first argument to this interface is 1, then only the C-API is 1979305002Scy** enabled and the SQL function remains disabled. If the first argument to 1980305002Scy** this interface is 0, then both the C-API and the SQL function are disabled. 1981305002Scy** If the first argument is -1, then no changes are made to state of either the 1982305002Scy** C-API or the SQL function. 1983305002Scy** The second parameter is a pointer to an integer into which 1984305002Scy** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface 1985305002Scy** is disabled or enabled following this call. The second parameter may 1986305002Scy** be a NULL pointer, in which case the new setting is not reported back. 1987305002Scy** </dd> 1988305002Scy** 1989322444Speter** <dt>SQLITE_DBCONFIG_MAINDBNAME</dt> 1990322444Speter** <dd> ^This option is used to change the name of the "main" database 1991322444Speter** schema. ^The sole argument is a pointer to a constant UTF8 string 1992322444Speter** which will become the new schema name in place of "main". ^SQLite 1993322444Speter** does not make a copy of the new main schema name string, so the application 1994322444Speter** must ensure that the argument passed into this DBCONFIG option is unchanged 1995322444Speter** until after the database connection closes. 1996322444Speter** </dd> 1997322444Speter** 1998322444Speter** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt> 1999322444Speter** <dd> Usually, when a database in wal mode is closed or detached from a 2000322444Speter** database handle, SQLite checks if this will mean that there are now no 2001322444Speter** connections at all to the database. If so, it performs a checkpoint 2002322444Speter** operation before closing the connection. This option may be used to 2003322444Speter** override this behaviour. The first parameter passed to this operation 2004322444Speter** is an integer - non-zero to disable checkpoints-on-close, or zero (the 2005322444Speter** default) to enable them. The second parameter is a pointer to an integer 2006322444Speter** into which is written 0 or 1 to indicate whether checkpoints-on-close 2007322444Speter** have been disabled - 0 if they are not disabled, 1 if they are. 2008322444Speter** </dd> 2009322444Speter** 2010322444Speter** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt> 2011322444Speter** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates 2012322444Speter** the [query planner stability guarantee] (QPSG). When the QPSG is active, 2013322444Speter** a single SQL query statement will always use the same algorithm regardless 2014322444Speter** of values of [bound parameters].)^ The QPSG disables some query optimizations 2015322444Speter** that look at the values of bound parameters, which can make some queries 2016322444Speter** slower. But the QPSG has the advantage of more predictable behavior. With 2017322444Speter** the QPSG active, SQLite will always use the same query plan in the field as 2018322444Speter** was used during testing in the lab. 2019322444Speter** </dd> 2020322444Speter** 2021251883Speter** </dl> 2022251883Speter*/ 2023322444Speter#define SQLITE_DBCONFIG_MAINDBNAME 1000 /* const char* */ 2024298161Sbapt#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ 2025298161Sbapt#define SQLITE_DBCONFIG_ENABLE_FKEY 1002 /* int int* */ 2026298161Sbapt#define SQLITE_DBCONFIG_ENABLE_TRIGGER 1003 /* int int* */ 2027298161Sbapt#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */ 2028305002Scy#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */ 2029322444Speter#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE 1006 /* int int* */ 2030322444Speter#define SQLITE_DBCONFIG_ENABLE_QPSG 1007 /* int int* */ 2031251883Speter 2032251883Speter 2033251883Speter/* 2034251883Speter** CAPI3REF: Enable Or Disable Extended Result Codes 2035286510Speter** METHOD: sqlite3 2036251883Speter** 2037251883Speter** ^The sqlite3_extended_result_codes() routine enables or disables the 2038251883Speter** [extended result codes] feature of SQLite. ^The extended result 2039251883Speter** codes are disabled by default for historical compatibility. 2040251883Speter*/ 2041322444SpeterSQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff); 2042251883Speter 2043251883Speter/* 2044251883Speter** CAPI3REF: Last Insert Rowid 2045286510Speter** METHOD: sqlite3 2046251883Speter** 2047269851Speter** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables) 2048269851Speter** has a unique 64-bit signed 2049251883Speter** integer key called the [ROWID | "rowid"]. ^The rowid is always available 2050251883Speter** as an undeclared column named ROWID, OID, or _ROWID_ as long as those 2051251883Speter** names are not also used by explicitly declared columns. ^If 2052251883Speter** the table has a column of type [INTEGER PRIMARY KEY] then that column 2053251883Speter** is another alias for the rowid. 2054251883Speter** 2055322444Speter** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of 2056322444Speter** the most recent successful [INSERT] into a rowid table or [virtual table] 2057322444Speter** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not 2058322444Speter** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred 2059322444Speter** on the database connection D, then sqlite3_last_insert_rowid(D) returns 2060322444Speter** zero. 2061251883Speter** 2062322444Speter** As well as being set automatically as rows are inserted into database 2063322444Speter** tables, the value returned by this function may be set explicitly by 2064322444Speter** [sqlite3_set_last_insert_rowid()] 2065251883Speter** 2066322444Speter** Some virtual table implementations may INSERT rows into rowid tables as 2067322444Speter** part of committing a transaction (e.g. to flush data accumulated in memory 2068322444Speter** to disk). In this case subsequent calls to this function return the rowid 2069322444Speter** associated with these internal INSERT operations, which leads to 2070322444Speter** unintuitive results. Virtual table implementations that do write to rowid 2071322444Speter** tables in this way can avoid this problem by restoring the original 2072322444Speter** rowid value using [sqlite3_set_last_insert_rowid()] before returning 2073322444Speter** control to the user. 2074322444Speter** 2075322444Speter** ^(If an [INSERT] occurs within a trigger then this routine will 2076322444Speter** return the [rowid] of the inserted row as long as the trigger is 2077322444Speter** running. Once the trigger program ends, the value returned 2078322444Speter** by this routine reverts to what it was before the trigger was fired.)^ 2079322444Speter** 2080251883Speter** ^An [INSERT] that fails due to a constraint violation is not a 2081251883Speter** successful [INSERT] and does not change the value returned by this 2082251883Speter** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, 2083251883Speter** and INSERT OR ABORT make no changes to the return value of this 2084251883Speter** routine when their insertion fails. ^(When INSERT OR REPLACE 2085251883Speter** encounters a constraint violation, it does not fail. The 2086251883Speter** INSERT continues to completion after deleting rows that caused 2087251883Speter** the constraint problem so INSERT OR REPLACE will always change 2088251883Speter** the return value of this interface.)^ 2089251883Speter** 2090251883Speter** ^For the purposes of this routine, an [INSERT] is considered to 2091251883Speter** be successful even if it is subsequently rolled back. 2092251883Speter** 2093251883Speter** This function is accessible to SQL statements via the 2094251883Speter** [last_insert_rowid() SQL function]. 2095251883Speter** 2096251883Speter** If a separate thread performs a new [INSERT] on the same 2097251883Speter** database connection while the [sqlite3_last_insert_rowid()] 2098251883Speter** function is running and thus changes the last insert [rowid], 2099251883Speter** then the value returned by [sqlite3_last_insert_rowid()] is 2100251883Speter** unpredictable and might not equal either the old or the new 2101251883Speter** last insert [rowid]. 2102251883Speter*/ 2103322444SpeterSQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); 2104251883Speter 2105251883Speter/* 2106322444Speter** CAPI3REF: Set the Last Insert Rowid value. 2107322444Speter** METHOD: sqlite3 2108322444Speter** 2109322444Speter** The sqlite3_set_last_insert_rowid(D, R) method allows the application to 2110322444Speter** set the value returned by calling sqlite3_last_insert_rowid(D) to R 2111322444Speter** without inserting a row into the database. 2112322444Speter*/ 2113322444SpeterSQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64); 2114322444Speter 2115322444Speter/* 2116251883Speter** CAPI3REF: Count The Number Of Rows Modified 2117286510Speter** METHOD: sqlite3 2118251883Speter** 2119282328Sbapt** ^This function returns the number of rows modified, inserted or 2120282328Sbapt** deleted by the most recently completed INSERT, UPDATE or DELETE 2121282328Sbapt** statement on the database connection specified by the only parameter. 2122282328Sbapt** ^Executing any other type of SQL statement does not modify the value 2123282328Sbapt** returned by this function. 2124251883Speter** 2125282328Sbapt** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are 2126282328Sbapt** considered - auxiliary changes caused by [CREATE TRIGGER | triggers], 2127282328Sbapt** [foreign key actions] or [REPLACE] constraint resolution are not counted. 2128282328Sbapt** 2129282328Sbapt** Changes to a view that are intercepted by 2130282328Sbapt** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value 2131282328Sbapt** returned by sqlite3_changes() immediately after an INSERT, UPDATE or 2132282328Sbapt** DELETE statement run on a view is always zero. Only changes made to real 2133282328Sbapt** tables are counted. 2134251883Speter** 2135282328Sbapt** Things are more complicated if the sqlite3_changes() function is 2136282328Sbapt** executed while a trigger program is running. This may happen if the 2137282328Sbapt** program uses the [changes() SQL function], or if some other callback 2138282328Sbapt** function invokes sqlite3_changes() directly. Essentially: 2139282328Sbapt** 2140282328Sbapt** <ul> 2141282328Sbapt** <li> ^(Before entering a trigger program the value returned by 2142282328Sbapt** sqlite3_changes() function is saved. After the trigger program 2143282328Sbapt** has finished, the original value is restored.)^ 2144282328Sbapt** 2145282328Sbapt** <li> ^(Within a trigger program each INSERT, UPDATE and DELETE 2146282328Sbapt** statement sets the value returned by sqlite3_changes() 2147282328Sbapt** upon completion as normal. Of course, this value will not include 2148282328Sbapt** any changes performed by sub-triggers, as the sqlite3_changes() 2149282328Sbapt** value will be saved and restored after each sub-trigger has run.)^ 2150282328Sbapt** </ul> 2151282328Sbapt** 2152282328Sbapt** ^This means that if the changes() SQL function (or similar) is used 2153282328Sbapt** by the first INSERT, UPDATE or DELETE statement within a trigger, it 2154282328Sbapt** returns the value as set when the calling statement began executing. 2155282328Sbapt** ^If it is used by the second or subsequent such statement within a trigger 2156282328Sbapt** program, the value returned reflects the number of rows modified by the 2157282328Sbapt** previous INSERT, UPDATE or DELETE statement within the same trigger. 2158251883Speter** 2159251883Speter** See also the [sqlite3_total_changes()] interface, the 2160251883Speter** [count_changes pragma], and the [changes() SQL function]. 2161251883Speter** 2162251883Speter** If a separate thread makes changes on the same database connection 2163251883Speter** while [sqlite3_changes()] is running then the value returned 2164251883Speter** is unpredictable and not meaningful. 2165251883Speter*/ 2166322444SpeterSQLITE_API int sqlite3_changes(sqlite3*); 2167251883Speter 2168251883Speter/* 2169251883Speter** CAPI3REF: Total Number Of Rows Modified 2170286510Speter** METHOD: sqlite3 2171251883Speter** 2172282328Sbapt** ^This function returns the total number of rows inserted, modified or 2173282328Sbapt** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed 2174282328Sbapt** since the database connection was opened, including those executed as 2175282328Sbapt** part of trigger programs. ^Executing any other type of SQL statement 2176282328Sbapt** does not affect the value returned by sqlite3_total_changes(). 2177282328Sbapt** 2178282328Sbapt** ^Changes made as part of [foreign key actions] are included in the 2179282328Sbapt** count, but those made as part of REPLACE constraint resolution are 2180282328Sbapt** not. ^Changes to a view that are intercepted by INSTEAD OF triggers 2181282328Sbapt** are not counted. 2182282328Sbapt** 2183251883Speter** See also the [sqlite3_changes()] interface, the 2184251883Speter** [count_changes pragma], and the [total_changes() SQL function]. 2185251883Speter** 2186251883Speter** If a separate thread makes changes on the same database connection 2187251883Speter** while [sqlite3_total_changes()] is running then the value 2188251883Speter** returned is unpredictable and not meaningful. 2189251883Speter*/ 2190322444SpeterSQLITE_API int sqlite3_total_changes(sqlite3*); 2191251883Speter 2192251883Speter/* 2193251883Speter** CAPI3REF: Interrupt A Long-Running Query 2194286510Speter** METHOD: sqlite3 2195251883Speter** 2196251883Speter** ^This function causes any pending database operation to abort and 2197251883Speter** return at its earliest opportunity. This routine is typically 2198251883Speter** called in response to a user action such as pressing "Cancel" 2199251883Speter** or Ctrl-C where the user wants a long query operation to halt 2200251883Speter** immediately. 2201251883Speter** 2202251883Speter** ^It is safe to call this routine from a thread different from the 2203251883Speter** thread that is currently running the database operation. But it 2204251883Speter** is not safe to call this routine with a [database connection] that 2205251883Speter** is closed or might close before sqlite3_interrupt() returns. 2206251883Speter** 2207251883Speter** ^If an SQL operation is very nearly finished at the time when 2208251883Speter** sqlite3_interrupt() is called, then it might not have an opportunity 2209251883Speter** to be interrupted and might continue to completion. 2210251883Speter** 2211251883Speter** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT]. 2212251883Speter** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE 2213251883Speter** that is inside an explicit transaction, then the entire transaction 2214251883Speter** will be rolled back automatically. 2215251883Speter** 2216251883Speter** ^The sqlite3_interrupt(D) call is in effect until all currently running 2217251883Speter** SQL statements on [database connection] D complete. ^Any new SQL statements 2218251883Speter** that are started after the sqlite3_interrupt() call and before the 2219251883Speter** running statements reaches zero are interrupted as if they had been 2220251883Speter** running prior to the sqlite3_interrupt() call. ^New SQL statements 2221251883Speter** that are started after the running statement count reaches zero are 2222251883Speter** not effected by the sqlite3_interrupt(). 2223251883Speter** ^A call to sqlite3_interrupt(D) that occurs when there are no running 2224251883Speter** SQL statements is a no-op and has no effect on SQL statements 2225251883Speter** that are started after the sqlite3_interrupt() call returns. 2226251883Speter*/ 2227322444SpeterSQLITE_API void sqlite3_interrupt(sqlite3*); 2228251883Speter 2229251883Speter/* 2230251883Speter** CAPI3REF: Determine If An SQL Statement Is Complete 2231251883Speter** 2232251883Speter** These routines are useful during command-line input to determine if the 2233251883Speter** currently entered text seems to form a complete SQL statement or 2234251883Speter** if additional input is needed before sending the text into 2235251883Speter** SQLite for parsing. ^These routines return 1 if the input string 2236251883Speter** appears to be a complete SQL statement. ^A statement is judged to be 2237251883Speter** complete if it ends with a semicolon token and is not a prefix of a 2238251883Speter** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within 2239251883Speter** string literals or quoted identifier names or comments are not 2240251883Speter** independent tokens (they are part of the token in which they are 2241251883Speter** embedded) and thus do not count as a statement terminator. ^Whitespace 2242251883Speter** and comments that follow the final semicolon are ignored. 2243251883Speter** 2244251883Speter** ^These routines return 0 if the statement is incomplete. ^If a 2245251883Speter** memory allocation fails, then SQLITE_NOMEM is returned. 2246251883Speter** 2247251883Speter** ^These routines do not parse the SQL statements thus 2248251883Speter** will not detect syntactically incorrect SQL. 2249251883Speter** 2250251883Speter** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior 2251251883Speter** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked 2252251883Speter** automatically by sqlite3_complete16(). If that initialization fails, 2253251883Speter** then the return value from sqlite3_complete16() will be non-zero 2254251883Speter** regardless of whether or not the input SQL is complete.)^ 2255251883Speter** 2256251883Speter** The input to [sqlite3_complete()] must be a zero-terminated 2257251883Speter** UTF-8 string. 2258251883Speter** 2259251883Speter** The input to [sqlite3_complete16()] must be a zero-terminated 2260251883Speter** UTF-16 string in native byte order. 2261251883Speter*/ 2262322444SpeterSQLITE_API int sqlite3_complete(const char *sql); 2263322444SpeterSQLITE_API int sqlite3_complete16(const void *sql); 2264251883Speter 2265251883Speter/* 2266251883Speter** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors 2267282328Sbapt** KEYWORDS: {busy-handler callback} {busy handler} 2268286510Speter** METHOD: sqlite3 2269251883Speter** 2270274884Sbapt** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X 2271274884Sbapt** that might be invoked with argument P whenever 2272274884Sbapt** an attempt is made to access a database table associated with 2273274884Sbapt** [database connection] D when another thread 2274274884Sbapt** or process has the table locked. 2275274884Sbapt** The sqlite3_busy_handler() interface is used to implement 2276274884Sbapt** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout]. 2277251883Speter** 2278274884Sbapt** ^If the busy callback is NULL, then [SQLITE_BUSY] 2279251883Speter** is returned immediately upon encountering the lock. ^If the busy callback 2280251883Speter** is not NULL, then the callback might be invoked with two arguments. 2281251883Speter** 2282251883Speter** ^The first argument to the busy handler is a copy of the void* pointer which 2283251883Speter** is the third argument to sqlite3_busy_handler(). ^The second argument to 2284251883Speter** the busy handler callback is the number of times that the busy handler has 2285282328Sbapt** been invoked previously for the same locking event. ^If the 2286251883Speter** busy callback returns 0, then no additional attempts are made to 2287274884Sbapt** access the database and [SQLITE_BUSY] is returned 2288274884Sbapt** to the application. 2289251883Speter** ^If the callback returns non-zero, then another attempt 2290274884Sbapt** is made to access the database and the cycle repeats. 2291251883Speter** 2292251883Speter** The presence of a busy handler does not guarantee that it will be invoked 2293251883Speter** when there is lock contention. ^If SQLite determines that invoking the busy 2294251883Speter** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY] 2295274884Sbapt** to the application instead of invoking the 2296274884Sbapt** busy handler. 2297251883Speter** Consider a scenario where one process is holding a read lock that 2298251883Speter** it is trying to promote to a reserved lock and 2299251883Speter** a second process is holding a reserved lock that it is trying 2300251883Speter** to promote to an exclusive lock. The first process cannot proceed 2301251883Speter** because it is blocked by the second and the second process cannot 2302251883Speter** proceed because it is blocked by the first. If both processes 2303251883Speter** invoke the busy handlers, neither will make any progress. Therefore, 2304251883Speter** SQLite returns [SQLITE_BUSY] for the first process, hoping that this 2305251883Speter** will induce the first process to release its read lock and allow 2306251883Speter** the second process to proceed. 2307251883Speter** 2308251883Speter** ^The default busy callback is NULL. 2309251883Speter** 2310251883Speter** ^(There can only be a single busy handler defined for each 2311251883Speter** [database connection]. Setting a new busy handler clears any 2312251883Speter** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()] 2313274884Sbapt** or evaluating [PRAGMA busy_timeout=N] will change the 2314274884Sbapt** busy handler and thus clear any previously set busy handler. 2315251883Speter** 2316251883Speter** The busy callback should not take any actions which modify the 2317274884Sbapt** database connection that invoked the busy handler. In other words, 2318274884Sbapt** the busy handler is not reentrant. Any such actions 2319251883Speter** result in undefined behavior. 2320251883Speter** 2321251883Speter** A busy handler must not close the database connection 2322251883Speter** or [prepared statement] that invoked the busy handler. 2323251883Speter*/ 2324322444SpeterSQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*); 2325251883Speter 2326251883Speter/* 2327251883Speter** CAPI3REF: Set A Busy Timeout 2328286510Speter** METHOD: sqlite3 2329251883Speter** 2330251883Speter** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps 2331251883Speter** for a specified amount of time when a table is locked. ^The handler 2332251883Speter** will sleep multiple times until at least "ms" milliseconds of sleeping 2333251883Speter** have accumulated. ^After at least "ms" milliseconds of sleeping, 2334251883Speter** the handler returns 0 which causes [sqlite3_step()] to return 2335274884Sbapt** [SQLITE_BUSY]. 2336251883Speter** 2337251883Speter** ^Calling this routine with an argument less than or equal to zero 2338251883Speter** turns off all busy handlers. 2339251883Speter** 2340251883Speter** ^(There can only be a single busy handler for a particular 2341274884Sbapt** [database connection] at any given moment. If another busy handler 2342251883Speter** was defined (using [sqlite3_busy_handler()]) prior to calling 2343251883Speter** this routine, that other busy handler is cleared.)^ 2344274884Sbapt** 2345274884Sbapt** See also: [PRAGMA busy_timeout] 2346251883Speter*/ 2347322444SpeterSQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms); 2348251883Speter 2349251883Speter/* 2350251883Speter** CAPI3REF: Convenience Routines For Running Queries 2351286510Speter** METHOD: sqlite3 2352251883Speter** 2353251883Speter** This is a legacy interface that is preserved for backwards compatibility. 2354251883Speter** Use of this interface is not recommended. 2355251883Speter** 2356251883Speter** Definition: A <b>result table</b> is memory data structure created by the 2357251883Speter** [sqlite3_get_table()] interface. A result table records the 2358251883Speter** complete query results from one or more queries. 2359251883Speter** 2360251883Speter** The table conceptually has a number of rows and columns. But 2361251883Speter** these numbers are not part of the result table itself. These 2362251883Speter** numbers are obtained separately. Let N be the number of rows 2363251883Speter** and M be the number of columns. 2364251883Speter** 2365251883Speter** A result table is an array of pointers to zero-terminated UTF-8 strings. 2366251883Speter** There are (N+1)*M elements in the array. The first M pointers point 2367251883Speter** to zero-terminated strings that contain the names of the columns. 2368251883Speter** The remaining entries all point to query results. NULL values result 2369251883Speter** in NULL pointers. All other values are in their UTF-8 zero-terminated 2370251883Speter** string representation as returned by [sqlite3_column_text()]. 2371251883Speter** 2372251883Speter** A result table might consist of one or more memory allocations. 2373251883Speter** It is not safe to pass a result table directly to [sqlite3_free()]. 2374251883Speter** A result table should be deallocated using [sqlite3_free_table()]. 2375251883Speter** 2376251883Speter** ^(As an example of the result table format, suppose a query result 2377251883Speter** is as follows: 2378251883Speter** 2379251883Speter** <blockquote><pre> 2380251883Speter** Name | Age 2381251883Speter** ----------------------- 2382251883Speter** Alice | 43 2383251883Speter** Bob | 28 2384251883Speter** Cindy | 21 2385251883Speter** </pre></blockquote> 2386251883Speter** 2387251883Speter** There are two column (M==2) and three rows (N==3). Thus the 2388251883Speter** result table has 8 entries. Suppose the result table is stored 2389251883Speter** in an array names azResult. Then azResult holds this content: 2390251883Speter** 2391251883Speter** <blockquote><pre> 2392251883Speter** azResult[0] = "Name"; 2393251883Speter** azResult[1] = "Age"; 2394251883Speter** azResult[2] = "Alice"; 2395251883Speter** azResult[3] = "43"; 2396251883Speter** azResult[4] = "Bob"; 2397251883Speter** azResult[5] = "28"; 2398251883Speter** azResult[6] = "Cindy"; 2399251883Speter** azResult[7] = "21"; 2400251883Speter** </pre></blockquote>)^ 2401251883Speter** 2402251883Speter** ^The sqlite3_get_table() function evaluates one or more 2403251883Speter** semicolon-separated SQL statements in the zero-terminated UTF-8 2404251883Speter** string of its 2nd parameter and returns a result table to the 2405251883Speter** pointer given in its 3rd parameter. 2406251883Speter** 2407251883Speter** After the application has finished with the result from sqlite3_get_table(), 2408251883Speter** it must pass the result table pointer to sqlite3_free_table() in order to 2409251883Speter** release the memory that was malloced. Because of the way the 2410251883Speter** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling 2411251883Speter** function must not try to call [sqlite3_free()] directly. Only 2412251883Speter** [sqlite3_free_table()] is able to release the memory properly and safely. 2413251883Speter** 2414251883Speter** The sqlite3_get_table() interface is implemented as a wrapper around 2415251883Speter** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access 2416251883Speter** to any internal data structures of SQLite. It uses only the public 2417251883Speter** interface defined here. As a consequence, errors that occur in the 2418251883Speter** wrapper layer outside of the internal [sqlite3_exec()] call are not 2419251883Speter** reflected in subsequent calls to [sqlite3_errcode()] or 2420251883Speter** [sqlite3_errmsg()]. 2421251883Speter*/ 2422322444SpeterSQLITE_API int sqlite3_get_table( 2423251883Speter sqlite3 *db, /* An open database */ 2424251883Speter const char *zSql, /* SQL to be evaluated */ 2425251883Speter char ***pazResult, /* Results of the query */ 2426251883Speter int *pnRow, /* Number of result rows written here */ 2427251883Speter int *pnColumn, /* Number of result columns written here */ 2428251883Speter char **pzErrmsg /* Error msg written here */ 2429251883Speter); 2430322444SpeterSQLITE_API void sqlite3_free_table(char **result); 2431251883Speter 2432251883Speter/* 2433251883Speter** CAPI3REF: Formatted String Printing Functions 2434251883Speter** 2435251883Speter** These routines are work-alikes of the "printf()" family of functions 2436251883Speter** from the standard C library. 2437282328Sbapt** These routines understand most of the common K&R formatting options, 2438282328Sbapt** plus some additional non-standard formats, detailed below. 2439282328Sbapt** Note that some of the more obscure formatting options from recent 2440282328Sbapt** C-library standards are omitted from this implementation. 2441251883Speter** 2442251883Speter** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their 2443251883Speter** results into memory obtained from [sqlite3_malloc()]. 2444251883Speter** The strings returned by these two routines should be 2445251883Speter** released by [sqlite3_free()]. ^Both routines return a 2446251883Speter** NULL pointer if [sqlite3_malloc()] is unable to allocate enough 2447251883Speter** memory to hold the resulting string. 2448251883Speter** 2449251883Speter** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from 2450251883Speter** the standard C library. The result is written into the 2451251883Speter** buffer supplied as the second parameter whose size is given by 2452251883Speter** the first parameter. Note that the order of the 2453251883Speter** first two parameters is reversed from snprintf().)^ This is an 2454251883Speter** historical accident that cannot be fixed without breaking 2455251883Speter** backwards compatibility. ^(Note also that sqlite3_snprintf() 2456251883Speter** returns a pointer to its buffer instead of the number of 2457251883Speter** characters actually written into the buffer.)^ We admit that 2458251883Speter** the number of characters written would be a more useful return 2459251883Speter** value but we cannot change the implementation of sqlite3_snprintf() 2460251883Speter** now without breaking compatibility. 2461251883Speter** 2462251883Speter** ^As long as the buffer size is greater than zero, sqlite3_snprintf() 2463251883Speter** guarantees that the buffer is always zero-terminated. ^The first 2464251883Speter** parameter "n" is the total size of the buffer, including space for 2465251883Speter** the zero terminator. So the longest string that can be completely 2466251883Speter** written will be n-1 characters. 2467251883Speter** 2468251883Speter** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf(). 2469251883Speter** 2470251883Speter** These routines all implement some additional formatting 2471251883Speter** options that are useful for constructing SQL statements. 2472251883Speter** All of the usual printf() formatting options apply. In addition, there 2473282328Sbapt** is are "%q", "%Q", "%w" and "%z" options. 2474251883Speter** 2475251883Speter** ^(The %q option works like %s in that it substitutes a nul-terminated 2476251883Speter** string from the argument list. But %q also doubles every '\'' character. 2477251883Speter** %q is designed for use inside a string literal.)^ By doubling each '\'' 2478251883Speter** character it escapes that character and allows it to be inserted into 2479251883Speter** the string. 2480251883Speter** 2481251883Speter** For example, assume the string variable zText contains text as follows: 2482251883Speter** 2483251883Speter** <blockquote><pre> 2484251883Speter** char *zText = "It's a happy day!"; 2485251883Speter** </pre></blockquote> 2486251883Speter** 2487251883Speter** One can use this text in an SQL statement as follows: 2488251883Speter** 2489251883Speter** <blockquote><pre> 2490251883Speter** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText); 2491251883Speter** sqlite3_exec(db, zSQL, 0, 0, 0); 2492251883Speter** sqlite3_free(zSQL); 2493251883Speter** </pre></blockquote> 2494251883Speter** 2495251883Speter** Because the %q format string is used, the '\'' character in zText 2496251883Speter** is escaped and the SQL generated is as follows: 2497251883Speter** 2498251883Speter** <blockquote><pre> 2499251883Speter** INSERT INTO table1 VALUES('It''s a happy day!') 2500251883Speter** </pre></blockquote> 2501251883Speter** 2502251883Speter** This is correct. Had we used %s instead of %q, the generated SQL 2503251883Speter** would have looked like this: 2504251883Speter** 2505251883Speter** <blockquote><pre> 2506251883Speter** INSERT INTO table1 VALUES('It's a happy day!'); 2507251883Speter** </pre></blockquote> 2508251883Speter** 2509251883Speter** This second example is an SQL syntax error. As a general rule you should 2510251883Speter** always use %q instead of %s when inserting text into a string literal. 2511251883Speter** 2512251883Speter** ^(The %Q option works like %q except it also adds single quotes around 2513251883Speter** the outside of the total string. Additionally, if the parameter in the 2514251883Speter** argument list is a NULL pointer, %Q substitutes the text "NULL" (without 2515251883Speter** single quotes).)^ So, for example, one could say: 2516251883Speter** 2517251883Speter** <blockquote><pre> 2518251883Speter** char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText); 2519251883Speter** sqlite3_exec(db, zSQL, 0, 0, 0); 2520251883Speter** sqlite3_free(zSQL); 2521251883Speter** </pre></blockquote> 2522251883Speter** 2523251883Speter** The code above will render a correct SQL statement in the zSQL 2524251883Speter** variable even if the zText variable is a NULL pointer. 2525251883Speter** 2526282328Sbapt** ^(The "%w" formatting option is like "%q" except that it expects to 2527282328Sbapt** be contained within double-quotes instead of single quotes, and it 2528282328Sbapt** escapes the double-quote character instead of the single-quote 2529282328Sbapt** character.)^ The "%w" formatting option is intended for safely inserting 2530282328Sbapt** table and column names into a constructed SQL statement. 2531282328Sbapt** 2532251883Speter** ^(The "%z" formatting option works like "%s" but with the 2533251883Speter** addition that after the string has been read and copied into 2534251883Speter** the result, [sqlite3_free()] is called on the input string.)^ 2535251883Speter*/ 2536322444SpeterSQLITE_API char *sqlite3_mprintf(const char*,...); 2537322444SpeterSQLITE_API char *sqlite3_vmprintf(const char*, va_list); 2538322444SpeterSQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...); 2539322444SpeterSQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list); 2540251883Speter 2541251883Speter/* 2542251883Speter** CAPI3REF: Memory Allocation Subsystem 2543251883Speter** 2544251883Speter** The SQLite core uses these three routines for all of its own 2545251883Speter** internal memory allocation needs. "Core" in the previous sentence 2546251883Speter** does not include operating-system specific VFS implementation. The 2547251883Speter** Windows VFS uses native malloc() and free() for some operations. 2548251883Speter** 2549251883Speter** ^The sqlite3_malloc() routine returns a pointer to a block 2550251883Speter** of memory at least N bytes in length, where N is the parameter. 2551251883Speter** ^If sqlite3_malloc() is unable to obtain sufficient free 2552251883Speter** memory, it returns a NULL pointer. ^If the parameter N to 2553251883Speter** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns 2554251883Speter** a NULL pointer. 2555251883Speter** 2556274884Sbapt** ^The sqlite3_malloc64(N) routine works just like 2557274884Sbapt** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead 2558274884Sbapt** of a signed 32-bit integer. 2559274884Sbapt** 2560251883Speter** ^Calling sqlite3_free() with a pointer previously returned 2561251883Speter** by sqlite3_malloc() or sqlite3_realloc() releases that memory so 2562251883Speter** that it might be reused. ^The sqlite3_free() routine is 2563251883Speter** a no-op if is called with a NULL pointer. Passing a NULL pointer 2564251883Speter** to sqlite3_free() is harmless. After being freed, memory 2565251883Speter** should neither be read nor written. Even reading previously freed 2566251883Speter** memory might result in a segmentation fault or other severe error. 2567251883Speter** Memory corruption, a segmentation fault, or other severe error 2568251883Speter** might result if sqlite3_free() is called with a non-NULL pointer that 2569251883Speter** was not obtained from sqlite3_malloc() or sqlite3_realloc(). 2570251883Speter** 2571274884Sbapt** ^The sqlite3_realloc(X,N) interface attempts to resize a 2572274884Sbapt** prior memory allocation X to be at least N bytes. 2573274884Sbapt** ^If the X parameter to sqlite3_realloc(X,N) 2574251883Speter** is a NULL pointer then its behavior is identical to calling 2575274884Sbapt** sqlite3_malloc(N). 2576274884Sbapt** ^If the N parameter to sqlite3_realloc(X,N) is zero or 2577251883Speter** negative then the behavior is exactly the same as calling 2578274884Sbapt** sqlite3_free(X). 2579274884Sbapt** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation 2580274884Sbapt** of at least N bytes in size or NULL if insufficient memory is available. 2581251883Speter** ^If M is the size of the prior allocation, then min(N,M) bytes 2582251883Speter** of the prior allocation are copied into the beginning of buffer returned 2583274884Sbapt** by sqlite3_realloc(X,N) and the prior allocation is freed. 2584274884Sbapt** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the 2585274884Sbapt** prior allocation is not freed. 2586251883Speter** 2587274884Sbapt** ^The sqlite3_realloc64(X,N) interfaces works the same as 2588274884Sbapt** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead 2589274884Sbapt** of a 32-bit signed integer. 2590274884Sbapt** 2591274884Sbapt** ^If X is a memory allocation previously obtained from sqlite3_malloc(), 2592274884Sbapt** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then 2593274884Sbapt** sqlite3_msize(X) returns the size of that memory allocation in bytes. 2594274884Sbapt** ^The value returned by sqlite3_msize(X) might be larger than the number 2595274884Sbapt** of bytes requested when X was allocated. ^If X is a NULL pointer then 2596274884Sbapt** sqlite3_msize(X) returns zero. If X points to something that is not 2597274884Sbapt** the beginning of memory allocation, or if it points to a formerly 2598274884Sbapt** valid memory allocation that has now been freed, then the behavior 2599274884Sbapt** of sqlite3_msize(X) is undefined and possibly harmful. 2600274884Sbapt** 2601274884Sbapt** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(), 2602274884Sbapt** sqlite3_malloc64(), and sqlite3_realloc64() 2603251883Speter** is always aligned to at least an 8 byte boundary, or to a 2604251883Speter** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time 2605251883Speter** option is used. 2606251883Speter** 2607251883Speter** In SQLite version 3.5.0 and 3.5.1, it was possible to define 2608251883Speter** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in 2609251883Speter** implementation of these routines to be omitted. That capability 2610251883Speter** is no longer provided. Only built-in memory allocators can be used. 2611251883Speter** 2612251883Speter** Prior to SQLite version 3.7.10, the Windows OS interface layer called 2613251883Speter** the system malloc() and free() directly when converting 2614251883Speter** filenames between the UTF-8 encoding used by SQLite 2615251883Speter** and whatever filename encoding is used by the particular Windows 2616251883Speter** installation. Memory allocation errors were detected, but 2617251883Speter** they were reported back as [SQLITE_CANTOPEN] or 2618251883Speter** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. 2619251883Speter** 2620251883Speter** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()] 2621251883Speter** must be either NULL or else pointers obtained from a prior 2622251883Speter** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have 2623251883Speter** not yet been released. 2624251883Speter** 2625251883Speter** The application must not read or write any part of 2626251883Speter** a block of memory after it has been released using 2627251883Speter** [sqlite3_free()] or [sqlite3_realloc()]. 2628251883Speter*/ 2629322444SpeterSQLITE_API void *sqlite3_malloc(int); 2630322444SpeterSQLITE_API void *sqlite3_malloc64(sqlite3_uint64); 2631322444SpeterSQLITE_API void *sqlite3_realloc(void*, int); 2632322444SpeterSQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64); 2633322444SpeterSQLITE_API void sqlite3_free(void*); 2634322444SpeterSQLITE_API sqlite3_uint64 sqlite3_msize(void*); 2635251883Speter 2636251883Speter/* 2637251883Speter** CAPI3REF: Memory Allocator Statistics 2638251883Speter** 2639251883Speter** SQLite provides these two interfaces for reporting on the status 2640251883Speter** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()] 2641251883Speter** routines, which form the built-in memory allocation subsystem. 2642251883Speter** 2643251883Speter** ^The [sqlite3_memory_used()] routine returns the number of bytes 2644251883Speter** of memory currently outstanding (malloced but not freed). 2645251883Speter** ^The [sqlite3_memory_highwater()] routine returns the maximum 2646251883Speter** value of [sqlite3_memory_used()] since the high-water mark 2647251883Speter** was last reset. ^The values returned by [sqlite3_memory_used()] and 2648251883Speter** [sqlite3_memory_highwater()] include any overhead 2649251883Speter** added by SQLite in its implementation of [sqlite3_malloc()], 2650251883Speter** but not overhead added by the any underlying system library 2651251883Speter** routines that [sqlite3_malloc()] may call. 2652251883Speter** 2653251883Speter** ^The memory high-water mark is reset to the current value of 2654251883Speter** [sqlite3_memory_used()] if and only if the parameter to 2655251883Speter** [sqlite3_memory_highwater()] is true. ^The value returned 2656251883Speter** by [sqlite3_memory_highwater(1)] is the high-water mark 2657251883Speter** prior to the reset. 2658251883Speter*/ 2659322444SpeterSQLITE_API sqlite3_int64 sqlite3_memory_used(void); 2660322444SpeterSQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag); 2661251883Speter 2662251883Speter/* 2663251883Speter** CAPI3REF: Pseudo-Random Number Generator 2664251883Speter** 2665251883Speter** SQLite contains a high-quality pseudo-random number generator (PRNG) used to 2666251883Speter** select random [ROWID | ROWIDs] when inserting new records into a table that 2667251883Speter** already uses the largest possible [ROWID]. The PRNG is also used for 2668251883Speter** the build-in random() and randomblob() SQL functions. This interface allows 2669251883Speter** applications to access the same PRNG for other purposes. 2670251883Speter** 2671251883Speter** ^A call to this routine stores N bytes of randomness into buffer P. 2672282328Sbapt** ^The P parameter can be a NULL pointer. 2673251883Speter** 2674269851Speter** ^If this routine has not been previously called or if the previous 2675282328Sbapt** call had N less than one or a NULL pointer for P, then the PRNG is 2676282328Sbapt** seeded using randomness obtained from the xRandomness method of 2677282328Sbapt** the default [sqlite3_vfs] object. 2678282328Sbapt** ^If the previous call to this routine had an N of 1 or more and a 2679282328Sbapt** non-NULL P then the pseudo-randomness is generated 2680251883Speter** internally and without recourse to the [sqlite3_vfs] xRandomness 2681251883Speter** method. 2682251883Speter*/ 2683322444SpeterSQLITE_API void sqlite3_randomness(int N, void *P); 2684251883Speter 2685251883Speter/* 2686251883Speter** CAPI3REF: Compile-Time Authorization Callbacks 2687286510Speter** METHOD: sqlite3 2688322444Speter** KEYWORDS: {authorizer callback} 2689251883Speter** 2690251883Speter** ^This routine registers an authorizer callback with a particular 2691251883Speter** [database connection], supplied in the first argument. 2692251883Speter** ^The authorizer callback is invoked as SQL statements are being compiled 2693251883Speter** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], 2694322444Speter** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()], 2695322444Speter** and [sqlite3_prepare16_v3()]. ^At various 2696251883Speter** points during the compilation process, as logic is being created 2697251883Speter** to perform various actions, the authorizer callback is invoked to 2698251883Speter** see if those actions are allowed. ^The authorizer callback should 2699251883Speter** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the 2700251883Speter** specific action but allow the SQL statement to continue to be 2701251883Speter** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be 2702251883Speter** rejected with an error. ^If the authorizer callback returns 2703251883Speter** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] 2704251883Speter** then the [sqlite3_prepare_v2()] or equivalent call that triggered 2705251883Speter** the authorizer will fail with an error message. 2706251883Speter** 2707251883Speter** When the callback returns [SQLITE_OK], that means the operation 2708251883Speter** requested is ok. ^When the callback returns [SQLITE_DENY], the 2709251883Speter** [sqlite3_prepare_v2()] or equivalent call that triggered the 2710251883Speter** authorizer will fail with an error message explaining that 2711251883Speter** access is denied. 2712251883Speter** 2713251883Speter** ^The first parameter to the authorizer callback is a copy of the third 2714251883Speter** parameter to the sqlite3_set_authorizer() interface. ^The second parameter 2715251883Speter** to the callback is an integer [SQLITE_COPY | action code] that specifies 2716251883Speter** the particular action to be authorized. ^The third through sixth parameters 2717322444Speter** to the callback are either NULL pointers or zero-terminated strings 2718322444Speter** that contain additional details about the action to be authorized. 2719322444Speter** Applications must always be prepared to encounter a NULL pointer in any 2720322444Speter** of the third through the sixth parameters of the authorization callback. 2721251883Speter** 2722251883Speter** ^If the action code is [SQLITE_READ] 2723251883Speter** and the callback returns [SQLITE_IGNORE] then the 2724251883Speter** [prepared statement] statement is constructed to substitute 2725251883Speter** a NULL value in place of the table column that would have 2726251883Speter** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE] 2727251883Speter** return can be used to deny an untrusted user access to individual 2728251883Speter** columns of a table. 2729322444Speter** ^When a table is referenced by a [SELECT] but no column values are 2730322444Speter** extracted from that table (for example in a query like 2731322444Speter** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback 2732322444Speter** is invoked once for that table with a column name that is an empty string. 2733251883Speter** ^If the action code is [SQLITE_DELETE] and the callback returns 2734251883Speter** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the 2735251883Speter** [truncate optimization] is disabled and all rows are deleted individually. 2736251883Speter** 2737251883Speter** An authorizer is used when [sqlite3_prepare | preparing] 2738251883Speter** SQL statements from an untrusted source, to ensure that the SQL statements 2739251883Speter** do not try to access data they are not allowed to see, or that they do not 2740251883Speter** try to execute malicious statements that damage the database. For 2741251883Speter** example, an application may allow a user to enter arbitrary 2742251883Speter** SQL queries for evaluation by a database. But the application does 2743251883Speter** not want the user to be able to make arbitrary changes to the 2744251883Speter** database. An authorizer could then be put in place while the 2745251883Speter** user-entered SQL is being [sqlite3_prepare | prepared] that 2746251883Speter** disallows everything except [SELECT] statements. 2747251883Speter** 2748251883Speter** Applications that need to process SQL from untrusted sources 2749251883Speter** might also consider lowering resource limits using [sqlite3_limit()] 2750251883Speter** and limiting database size using the [max_page_count] [PRAGMA] 2751251883Speter** in addition to using an authorizer. 2752251883Speter** 2753251883Speter** ^(Only a single authorizer can be in place on a database connection 2754251883Speter** at a time. Each call to sqlite3_set_authorizer overrides the 2755251883Speter** previous call.)^ ^Disable the authorizer by installing a NULL callback. 2756251883Speter** The authorizer is disabled by default. 2757251883Speter** 2758251883Speter** The authorizer callback must not do anything that will modify 2759251883Speter** the database connection that invoked the authorizer callback. 2760251883Speter** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their 2761251883Speter** database connections for the meaning of "modify" in this paragraph. 2762251883Speter** 2763251883Speter** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the 2764251883Speter** statement might be re-prepared during [sqlite3_step()] due to a 2765251883Speter** schema change. Hence, the application should ensure that the 2766251883Speter** correct authorizer callback remains in place during the [sqlite3_step()]. 2767251883Speter** 2768251883Speter** ^Note that the authorizer callback is invoked only during 2769251883Speter** [sqlite3_prepare()] or its variants. Authorization is not 2770251883Speter** performed during statement evaluation in [sqlite3_step()], unless 2771251883Speter** as stated in the previous paragraph, sqlite3_step() invokes 2772251883Speter** sqlite3_prepare_v2() to reprepare a statement after a schema change. 2773251883Speter*/ 2774322444SpeterSQLITE_API int sqlite3_set_authorizer( 2775251883Speter sqlite3*, 2776251883Speter int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), 2777251883Speter void *pUserData 2778251883Speter); 2779251883Speter 2780251883Speter/* 2781251883Speter** CAPI3REF: Authorizer Return Codes 2782251883Speter** 2783251883Speter** The [sqlite3_set_authorizer | authorizer callback function] must 2784251883Speter** return either [SQLITE_OK] or one of these two constants in order 2785251883Speter** to signal SQLite whether or not the action is permitted. See the 2786251883Speter** [sqlite3_set_authorizer | authorizer documentation] for additional 2787251883Speter** information. 2788251883Speter** 2789274884Sbapt** Note that SQLITE_IGNORE is also used as a [conflict resolution mode] 2790274884Sbapt** returned from the [sqlite3_vtab_on_conflict()] interface. 2791251883Speter*/ 2792251883Speter#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ 2793251883Speter#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ 2794251883Speter 2795251883Speter/* 2796251883Speter** CAPI3REF: Authorizer Action Codes 2797251883Speter** 2798251883Speter** The [sqlite3_set_authorizer()] interface registers a callback function 2799251883Speter** that is invoked to authorize certain SQL statement actions. The 2800251883Speter** second parameter to the callback is an integer code that specifies 2801251883Speter** what action is being authorized. These are the integer action codes that 2802251883Speter** the authorizer callback may be passed. 2803251883Speter** 2804251883Speter** These action code values signify what kind of operation is to be 2805251883Speter** authorized. The 3rd and 4th parameters to the authorization 2806251883Speter** callback function will be parameters or NULL depending on which of these 2807251883Speter** codes is used as the second parameter. ^(The 5th parameter to the 2808251883Speter** authorizer callback is the name of the database ("main", "temp", 2809251883Speter** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback 2810251883Speter** is the name of the inner-most trigger or view that is responsible for 2811251883Speter** the access attempt or NULL if this access attempt is directly from 2812251883Speter** top-level SQL code. 2813251883Speter*/ 2814251883Speter/******************************************* 3rd ************ 4th ***********/ 2815251883Speter#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ 2816251883Speter#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ 2817251883Speter#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ 2818251883Speter#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ 2819251883Speter#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ 2820251883Speter#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ 2821251883Speter#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ 2822251883Speter#define SQLITE_CREATE_VIEW 8 /* View Name NULL */ 2823251883Speter#define SQLITE_DELETE 9 /* Table Name NULL */ 2824251883Speter#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ 2825251883Speter#define SQLITE_DROP_TABLE 11 /* Table Name NULL */ 2826251883Speter#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ 2827251883Speter#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ 2828251883Speter#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ 2829251883Speter#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ 2830251883Speter#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ 2831251883Speter#define SQLITE_DROP_VIEW 17 /* View Name NULL */ 2832251883Speter#define SQLITE_INSERT 18 /* Table Name NULL */ 2833251883Speter#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ 2834251883Speter#define SQLITE_READ 20 /* Table Name Column Name */ 2835251883Speter#define SQLITE_SELECT 21 /* NULL NULL */ 2836251883Speter#define SQLITE_TRANSACTION 22 /* Operation NULL */ 2837251883Speter#define SQLITE_UPDATE 23 /* Table Name Column Name */ 2838251883Speter#define SQLITE_ATTACH 24 /* Filename NULL */ 2839251883Speter#define SQLITE_DETACH 25 /* Database Name NULL */ 2840251883Speter#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ 2841251883Speter#define SQLITE_REINDEX 27 /* Index Name NULL */ 2842251883Speter#define SQLITE_ANALYZE 28 /* Table Name NULL */ 2843251883Speter#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ 2844251883Speter#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ 2845251883Speter#define SQLITE_FUNCTION 31 /* NULL Function Name */ 2846251883Speter#define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */ 2847251883Speter#define SQLITE_COPY 0 /* No longer used */ 2848269851Speter#define SQLITE_RECURSIVE 33 /* NULL NULL */ 2849251883Speter 2850251883Speter/* 2851251883Speter** CAPI3REF: Tracing And Profiling Functions 2852286510Speter** METHOD: sqlite3 2853251883Speter** 2854305002Scy** These routines are deprecated. Use the [sqlite3_trace_v2()] interface 2855305002Scy** instead of the routines described here. 2856305002Scy** 2857251883Speter** These routines register callback functions that can be used for 2858251883Speter** tracing and profiling the execution of SQL statements. 2859251883Speter** 2860251883Speter** ^The callback function registered by sqlite3_trace() is invoked at 2861251883Speter** various times when an SQL statement is being run by [sqlite3_step()]. 2862251883Speter** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the 2863251883Speter** SQL statement text as the statement first begins executing. 2864251883Speter** ^(Additional sqlite3_trace() callbacks might occur 2865251883Speter** as each triggered subprogram is entered. The callbacks for triggers 2866251883Speter** contain a UTF-8 SQL comment that identifies the trigger.)^ 2867251883Speter** 2868251883Speter** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit 2869251883Speter** the length of [bound parameter] expansion in the output of sqlite3_trace(). 2870251883Speter** 2871251883Speter** ^The callback function registered by sqlite3_profile() is invoked 2872251883Speter** as each SQL statement finishes. ^The profile callback contains 2873251883Speter** the original statement text and an estimate of wall-clock time 2874251883Speter** of how long that statement took to run. ^The profile callback 2875251883Speter** time is in units of nanoseconds, however the current implementation 2876251883Speter** is only capable of millisecond resolution so the six least significant 2877251883Speter** digits in the time are meaningless. Future versions of SQLite 2878251883Speter** might provide greater resolution on the profiler callback. The 2879251883Speter** sqlite3_profile() function is considered experimental and is 2880251883Speter** subject to change in future versions of SQLite. 2881251883Speter*/ 2882322444SpeterSQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*, 2883305002Scy void(*xTrace)(void*,const char*), void*); 2884322444SpeterSQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*, 2885251883Speter void(*xProfile)(void*,const char*,sqlite3_uint64), void*); 2886251883Speter 2887251883Speter/* 2888305002Scy** CAPI3REF: SQL Trace Event Codes 2889305002Scy** KEYWORDS: SQLITE_TRACE 2890305002Scy** 2891305002Scy** These constants identify classes of events that can be monitored 2892305002Scy** using the [sqlite3_trace_v2()] tracing logic. The third argument 2893305002Scy** to [sqlite3_trace_v2()] is an OR-ed combination of one or more of 2894305002Scy** the following constants. ^The first argument to the trace callback 2895305002Scy** is one of the following constants. 2896305002Scy** 2897305002Scy** New tracing constants may be added in future releases. 2898305002Scy** 2899305002Scy** ^A trace callback has four arguments: xCallback(T,C,P,X). 2900305002Scy** ^The T argument is one of the integer type codes above. 2901305002Scy** ^The C argument is a copy of the context pointer passed in as the 2902305002Scy** fourth argument to [sqlite3_trace_v2()]. 2903305002Scy** The P and X arguments are pointers whose meanings depend on T. 2904305002Scy** 2905305002Scy** <dl> 2906305002Scy** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt> 2907305002Scy** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement 2908305002Scy** first begins running and possibly at other times during the 2909305002Scy** execution of the prepared statement, such as at the start of each 2910305002Scy** trigger subprogram. ^The P argument is a pointer to the 2911305002Scy** [prepared statement]. ^The X argument is a pointer to a string which 2912305002Scy** is the unexpanded SQL text of the prepared statement or an SQL comment 2913305002Scy** that indicates the invocation of a trigger. ^The callback can compute 2914305002Scy** the same text that would have been returned by the legacy [sqlite3_trace()] 2915305002Scy** interface by using the X argument when X begins with "--" and invoking 2916305002Scy** [sqlite3_expanded_sql(P)] otherwise. 2917305002Scy** 2918305002Scy** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt> 2919305002Scy** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same 2920305002Scy** information as is provided by the [sqlite3_profile()] callback. 2921305002Scy** ^The P argument is a pointer to the [prepared statement] and the 2922305002Scy** X argument points to a 64-bit integer which is the estimated of 2923305002Scy** the number of nanosecond that the prepared statement took to run. 2924305002Scy** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes. 2925305002Scy** 2926305002Scy** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt> 2927305002Scy** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared 2928305002Scy** statement generates a single row of result. 2929305002Scy** ^The P argument is a pointer to the [prepared statement] and the 2930305002Scy** X argument is unused. 2931305002Scy** 2932305002Scy** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt> 2933305002Scy** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database 2934305002Scy** connection closes. 2935305002Scy** ^The P argument is a pointer to the [database connection] object 2936305002Scy** and the X argument is unused. 2937305002Scy** </dl> 2938305002Scy*/ 2939305002Scy#define SQLITE_TRACE_STMT 0x01 2940305002Scy#define SQLITE_TRACE_PROFILE 0x02 2941305002Scy#define SQLITE_TRACE_ROW 0x04 2942305002Scy#define SQLITE_TRACE_CLOSE 0x08 2943305002Scy 2944305002Scy/* 2945305002Scy** CAPI3REF: SQL Trace Hook 2946305002Scy** METHOD: sqlite3 2947305002Scy** 2948305002Scy** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback 2949305002Scy** function X against [database connection] D, using property mask M 2950305002Scy** and context pointer P. ^If the X callback is 2951305002Scy** NULL or if the M mask is zero, then tracing is disabled. The 2952305002Scy** M argument should be the bitwise OR-ed combination of 2953305002Scy** zero or more [SQLITE_TRACE] constants. 2954305002Scy** 2955305002Scy** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides 2956305002Scy** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2(). 2957305002Scy** 2958305002Scy** ^The X callback is invoked whenever any of the events identified by 2959305002Scy** mask M occur. ^The integer return value from the callback is currently 2960305002Scy** ignored, though this may change in future releases. Callback 2961305002Scy** implementations should return zero to ensure future compatibility. 2962305002Scy** 2963305002Scy** ^A trace callback is invoked with four arguments: callback(T,C,P,X). 2964305002Scy** ^The T argument is one of the [SQLITE_TRACE] 2965305002Scy** constants to indicate why the callback was invoked. 2966305002Scy** ^The C argument is a copy of the context pointer. 2967305002Scy** The P and X arguments are pointers whose meanings depend on T. 2968305002Scy** 2969305002Scy** The sqlite3_trace_v2() interface is intended to replace the legacy 2970305002Scy** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which 2971305002Scy** are deprecated. 2972305002Scy*/ 2973322444SpeterSQLITE_API int sqlite3_trace_v2( 2974305002Scy sqlite3*, 2975305002Scy unsigned uMask, 2976305002Scy int(*xCallback)(unsigned,void*,void*,void*), 2977305002Scy void *pCtx 2978305002Scy); 2979305002Scy 2980305002Scy/* 2981251883Speter** CAPI3REF: Query Progress Callbacks 2982286510Speter** METHOD: sqlite3 2983251883Speter** 2984251883Speter** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback 2985251883Speter** function X to be invoked periodically during long running calls to 2986251883Speter** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for 2987251883Speter** database connection D. An example use for this 2988251883Speter** interface is to keep a GUI updated during a large query. 2989251883Speter** 2990251883Speter** ^The parameter P is passed through as the only parameter to the 2991269851Speter** callback function X. ^The parameter N is the approximate number of 2992251883Speter** [virtual machine instructions] that are evaluated between successive 2993269851Speter** invocations of the callback X. ^If N is less than one then the progress 2994269851Speter** handler is disabled. 2995251883Speter** 2996251883Speter** ^Only a single progress handler may be defined at one time per 2997251883Speter** [database connection]; setting a new progress handler cancels the 2998251883Speter** old one. ^Setting parameter X to NULL disables the progress handler. 2999251883Speter** ^The progress handler is also disabled by setting N to a value less 3000251883Speter** than 1. 3001251883Speter** 3002251883Speter** ^If the progress callback returns non-zero, the operation is 3003251883Speter** interrupted. This feature can be used to implement a 3004251883Speter** "Cancel" button on a GUI progress dialog box. 3005251883Speter** 3006251883Speter** The progress handler callback must not do anything that will modify 3007251883Speter** the database connection that invoked the progress handler. 3008251883Speter** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their 3009251883Speter** database connections for the meaning of "modify" in this paragraph. 3010251883Speter** 3011251883Speter*/ 3012322444SpeterSQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); 3013251883Speter 3014251883Speter/* 3015251883Speter** CAPI3REF: Opening A New Database Connection 3016286510Speter** CONSTRUCTOR: sqlite3 3017251883Speter** 3018251883Speter** ^These routines open an SQLite database file as specified by the 3019251883Speter** filename argument. ^The filename argument is interpreted as UTF-8 for 3020251883Speter** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte 3021251883Speter** order for sqlite3_open16(). ^(A [database connection] handle is usually 3022251883Speter** returned in *ppDb, even if an error occurs. The only exception is that 3023251883Speter** if SQLite is unable to allocate memory to hold the [sqlite3] object, 3024251883Speter** a NULL will be written into *ppDb instead of a pointer to the [sqlite3] 3025251883Speter** object.)^ ^(If the database is opened (and/or created) successfully, then 3026251883Speter** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The 3027251883Speter** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain 3028251883Speter** an English language description of the error following a failure of any 3029251883Speter** of the sqlite3_open() routines. 3030251883Speter** 3031274884Sbapt** ^The default encoding will be UTF-8 for databases created using 3032274884Sbapt** sqlite3_open() or sqlite3_open_v2(). ^The default encoding for databases 3033274884Sbapt** created using sqlite3_open16() will be UTF-16 in the native byte order. 3034251883Speter** 3035251883Speter** Whether or not an error occurs when it is opened, resources 3036251883Speter** associated with the [database connection] handle should be released by 3037251883Speter** passing it to [sqlite3_close()] when it is no longer required. 3038251883Speter** 3039251883Speter** The sqlite3_open_v2() interface works like sqlite3_open() 3040251883Speter** except that it accepts two additional parameters for additional control 3041251883Speter** over the new database connection. ^(The flags parameter to 3042251883Speter** sqlite3_open_v2() can take one of 3043251883Speter** the following three values, optionally combined with the 3044251883Speter** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE], 3045251883Speter** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^ 3046251883Speter** 3047251883Speter** <dl> 3048251883Speter** ^(<dt>[SQLITE_OPEN_READONLY]</dt> 3049251883Speter** <dd>The database is opened in read-only mode. If the database does not 3050251883Speter** already exist, an error is returned.</dd>)^ 3051251883Speter** 3052251883Speter** ^(<dt>[SQLITE_OPEN_READWRITE]</dt> 3053251883Speter** <dd>The database is opened for reading and writing if possible, or reading 3054251883Speter** only if the file is write protected by the operating system. In either 3055251883Speter** case the database must already exist, otherwise an error is returned.</dd>)^ 3056251883Speter** 3057251883Speter** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt> 3058251883Speter** <dd>The database is opened for reading and writing, and is created if 3059251883Speter** it does not already exist. This is the behavior that is always used for 3060251883Speter** sqlite3_open() and sqlite3_open16().</dd>)^ 3061251883Speter** </dl> 3062251883Speter** 3063251883Speter** If the 3rd parameter to sqlite3_open_v2() is not one of the 3064251883Speter** combinations shown above optionally combined with other 3065251883Speter** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits] 3066251883Speter** then the behavior is undefined. 3067251883Speter** 3068251883Speter** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection 3069251883Speter** opens in the multi-thread [threading mode] as long as the single-thread 3070251883Speter** mode has not been set at compile-time or start-time. ^If the 3071251883Speter** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens 3072251883Speter** in the serialized [threading mode] unless single-thread was 3073251883Speter** previously selected at compile-time or start-time. 3074251883Speter** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be 3075251883Speter** eligible to use [shared cache mode], regardless of whether or not shared 3076251883Speter** cache is enabled using [sqlite3_enable_shared_cache()]. ^The 3077251883Speter** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not 3078251883Speter** participate in [shared cache mode] even if it is enabled. 3079251883Speter** 3080251883Speter** ^The fourth parameter to sqlite3_open_v2() is the name of the 3081251883Speter** [sqlite3_vfs] object that defines the operating system interface that 3082251883Speter** the new database connection should use. ^If the fourth parameter is 3083251883Speter** a NULL pointer then the default [sqlite3_vfs] object is used. 3084251883Speter** 3085251883Speter** ^If the filename is ":memory:", then a private, temporary in-memory database 3086251883Speter** is created for the connection. ^This in-memory database will vanish when 3087251883Speter** the database connection is closed. Future versions of SQLite might 3088251883Speter** make use of additional special filenames that begin with the ":" character. 3089251883Speter** It is recommended that when a database filename actually does begin with 3090251883Speter** a ":" character you should prefix the filename with a pathname such as 3091251883Speter** "./" to avoid ambiguity. 3092251883Speter** 3093251883Speter** ^If the filename is an empty string, then a private, temporary 3094251883Speter** on-disk database will be created. ^This private database will be 3095251883Speter** automatically deleted as soon as the database connection is closed. 3096251883Speter** 3097251883Speter** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3> 3098251883Speter** 3099251883Speter** ^If [URI filename] interpretation is enabled, and the filename argument 3100251883Speter** begins with "file:", then the filename is interpreted as a URI. ^URI 3101251883Speter** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is 3102251883Speter** set in the fourth argument to sqlite3_open_v2(), or if it has 3103251883Speter** been enabled globally using the [SQLITE_CONFIG_URI] option with the 3104251883Speter** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option. 3105251883Speter** As of SQLite version 3.7.7, URI filename interpretation is turned off 3106251883Speter** by default, but future releases of SQLite might enable URI filename 3107251883Speter** interpretation by default. See "[URI filenames]" for additional 3108251883Speter** information. 3109251883Speter** 3110251883Speter** URI filenames are parsed according to RFC 3986. ^If the URI contains an 3111251883Speter** authority, then it must be either an empty string or the string 3112251883Speter** "localhost". ^If the authority is not an empty string or "localhost", an 3113251883Speter** error is returned to the caller. ^The fragment component of a URI, if 3114251883Speter** present, is ignored. 3115251883Speter** 3116251883Speter** ^SQLite uses the path component of the URI as the name of the disk file 3117251883Speter** which contains the database. ^If the path begins with a '/' character, 3118251883Speter** then it is interpreted as an absolute path. ^If the path does not begin 3119251883Speter** with a '/' (meaning that the authority section is omitted from the URI) 3120251883Speter** then the path is interpreted as a relative path. 3121274884Sbapt** ^(On windows, the first component of an absolute path 3122274884Sbapt** is a drive specification (e.g. "C:").)^ 3123251883Speter** 3124251883Speter** [[core URI query parameters]] 3125251883Speter** The query component of a URI may contain parameters that are interpreted 3126251883Speter** either by SQLite itself, or by a [VFS | custom VFS implementation]. 3127274884Sbapt** SQLite and its built-in [VFSes] interpret the 3128274884Sbapt** following query parameters: 3129251883Speter** 3130251883Speter** <ul> 3131251883Speter** <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of 3132251883Speter** a VFS object that provides the operating system interface that should 3133251883Speter** be used to access the database file on disk. ^If this option is set to 3134251883Speter** an empty string the default VFS object is used. ^Specifying an unknown 3135251883Speter** VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is 3136251883Speter** present, then the VFS specified by the option takes precedence over 3137251883Speter** the value passed as the fourth parameter to sqlite3_open_v2(). 3138251883Speter** 3139251883Speter** <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw", 3140251883Speter** "rwc", or "memory". Attempting to set it to any other value is 3141251883Speter** an error)^. 3142251883Speter** ^If "ro" is specified, then the database is opened for read-only 3143251883Speter** access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the 3144251883Speter** third argument to sqlite3_open_v2(). ^If the mode option is set to 3145251883Speter** "rw", then the database is opened for read-write (but not create) 3146251883Speter** access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had 3147251883Speter** been set. ^Value "rwc" is equivalent to setting both 3148251883Speter** SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE. ^If the mode option is 3149251883Speter** set to "memory" then a pure [in-memory database] that never reads 3150251883Speter** or writes from disk is used. ^It is an error to specify a value for 3151251883Speter** the mode parameter that is less restrictive than that specified by 3152251883Speter** the flags passed in the third parameter to sqlite3_open_v2(). 3153251883Speter** 3154251883Speter** <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or 3155251883Speter** "private". ^Setting it to "shared" is equivalent to setting the 3156251883Speter** SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to 3157251883Speter** sqlite3_open_v2(). ^Setting the cache parameter to "private" is 3158251883Speter** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit. 3159251883Speter** ^If sqlite3_open_v2() is used and the "cache" parameter is present in 3160251883Speter** a URI filename, its value overrides any behavior requested by setting 3161251883Speter** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag. 3162269851Speter** 3163274884Sbapt** <li> <b>psow</b>: ^The psow parameter indicates whether or not the 3164269851Speter** [powersafe overwrite] property does or does not apply to the 3165274884Sbapt** storage media on which the database file resides. 3166269851Speter** 3167269851Speter** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter 3168269851Speter** which if set disables file locking in rollback journal modes. This 3169269851Speter** is useful for accessing a database on a filesystem that does not 3170269851Speter** support locking. Caution: Database corruption might result if two 3171269851Speter** or more processes write to the same database and any one of those 3172269851Speter** processes uses nolock=1. 3173269851Speter** 3174269851Speter** <li> <b>immutable</b>: ^The immutable parameter is a boolean query 3175269851Speter** parameter that indicates that the database file is stored on 3176269851Speter** read-only media. ^When immutable is set, SQLite assumes that the 3177269851Speter** database file cannot be changed, even by a process with higher 3178269851Speter** privilege, and so the database is opened read-only and all locking 3179269851Speter** and change detection is disabled. Caution: Setting the immutable 3180269851Speter** property on a database file that does in fact change can result 3181269851Speter** in incorrect query results and/or [SQLITE_CORRUPT] errors. 3182269851Speter** See also: [SQLITE_IOCAP_IMMUTABLE]. 3183269851Speter** 3184251883Speter** </ul> 3185251883Speter** 3186251883Speter** ^Specifying an unknown parameter in the query component of a URI is not an 3187251883Speter** error. Future versions of SQLite might understand additional query 3188251883Speter** parameters. See "[query parameters with special meaning to SQLite]" for 3189251883Speter** additional information. 3190251883Speter** 3191251883Speter** [[URI filename examples]] <h3>URI filename examples</h3> 3192251883Speter** 3193251883Speter** <table border="1" align=center cellpadding=5> 3194251883Speter** <tr><th> URI filenames <th> Results 3195251883Speter** <tr><td> file:data.db <td> 3196251883Speter** Open the file "data.db" in the current directory. 3197251883Speter** <tr><td> file:/home/fred/data.db<br> 3198251883Speter** file:///home/fred/data.db <br> 3199251883Speter** file://localhost/home/fred/data.db <br> <td> 3200251883Speter** Open the database file "/home/fred/data.db". 3201251883Speter** <tr><td> file://darkstar/home/fred/data.db <td> 3202251883Speter** An error. "darkstar" is not a recognized authority. 3203251883Speter** <tr><td style="white-space:nowrap"> 3204251883Speter** file:///C:/Documents%20and%20Settings/fred/Desktop/data.db 3205251883Speter** <td> Windows only: Open the file "data.db" on fred's desktop on drive 3206251883Speter** C:. Note that the %20 escaping in this example is not strictly 3207251883Speter** necessary - space characters can be used literally 3208251883Speter** in URI filenames. 3209251883Speter** <tr><td> file:data.db?mode=ro&cache=private <td> 3210251883Speter** Open file "data.db" in the current directory for read-only access. 3211251883Speter** Regardless of whether or not shared-cache mode is enabled by 3212251883Speter** default, use a private cache. 3213269851Speter** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td> 3214269851Speter** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile" 3215269851Speter** that uses dot-files in place of posix advisory locking. 3216251883Speter** <tr><td> file:data.db?mode=readonly <td> 3217251883Speter** An error. "readonly" is not a valid option for the "mode" parameter. 3218251883Speter** </table> 3219251883Speter** 3220251883Speter** ^URI hexadecimal escape sequences (%HH) are supported within the path and 3221251883Speter** query components of a URI. A hexadecimal escape sequence consists of a 3222251883Speter** percent sign - "%" - followed by exactly two hexadecimal digits 3223251883Speter** specifying an octet value. ^Before the path or query components of a 3224251883Speter** URI filename are interpreted, they are encoded using UTF-8 and all 3225251883Speter** hexadecimal escape sequences replaced by a single byte containing the 3226251883Speter** corresponding octet. If this process generates an invalid UTF-8 encoding, 3227251883Speter** the results are undefined. 3228251883Speter** 3229251883Speter** <b>Note to Windows users:</b> The encoding used for the filename argument 3230251883Speter** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever 3231251883Speter** codepage is currently defined. Filenames containing international 3232251883Speter** characters must be converted to UTF-8 prior to passing them into 3233251883Speter** sqlite3_open() or sqlite3_open_v2(). 3234251883Speter** 3235251883Speter** <b>Note to Windows Runtime users:</b> The temporary directory must be set 3236251883Speter** prior to calling sqlite3_open() or sqlite3_open_v2(). Otherwise, various 3237251883Speter** features that require the use of temporary files may fail. 3238251883Speter** 3239251883Speter** See also: [sqlite3_temp_directory] 3240251883Speter*/ 3241322444SpeterSQLITE_API int sqlite3_open( 3242251883Speter const char *filename, /* Database filename (UTF-8) */ 3243251883Speter sqlite3 **ppDb /* OUT: SQLite db handle */ 3244251883Speter); 3245322444SpeterSQLITE_API int sqlite3_open16( 3246251883Speter const void *filename, /* Database filename (UTF-16) */ 3247251883Speter sqlite3 **ppDb /* OUT: SQLite db handle */ 3248251883Speter); 3249322444SpeterSQLITE_API int sqlite3_open_v2( 3250251883Speter const char *filename, /* Database filename (UTF-8) */ 3251251883Speter sqlite3 **ppDb, /* OUT: SQLite db handle */ 3252251883Speter int flags, /* Flags */ 3253251883Speter const char *zVfs /* Name of VFS module to use */ 3254251883Speter); 3255251883Speter 3256251883Speter/* 3257251883Speter** CAPI3REF: Obtain Values For URI Parameters 3258251883Speter** 3259251883Speter** These are utility routines, useful to VFS implementations, that check 3260251883Speter** to see if a database file was a URI that contained a specific query 3261251883Speter** parameter, and if so obtains the value of that query parameter. 3262251883Speter** 3263251883Speter** If F is the database filename pointer passed into the xOpen() method of 3264251883Speter** a VFS implementation when the flags parameter to xOpen() has one or 3265251883Speter** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and 3266251883Speter** P is the name of the query parameter, then 3267251883Speter** sqlite3_uri_parameter(F,P) returns the value of the P 3268251883Speter** parameter if it exists or a NULL pointer if P does not appear as a 3269251883Speter** query parameter on F. If P is a query parameter of F 3270251883Speter** has no explicit value, then sqlite3_uri_parameter(F,P) returns 3271251883Speter** a pointer to an empty string. 3272251883Speter** 3273251883Speter** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean 3274251883Speter** parameter and returns true (1) or false (0) according to the value 3275251883Speter** of P. The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the 3276251883Speter** value of query parameter P is one of "yes", "true", or "on" in any 3277251883Speter** case or if the value begins with a non-zero number. The 3278251883Speter** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of 3279251883Speter** query parameter P is one of "no", "false", or "off" in any case or 3280251883Speter** if the value begins with a numeric zero. If P is not a query 3281251883Speter** parameter on F or if the value of P is does not match any of the 3282251883Speter** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0). 3283251883Speter** 3284251883Speter** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a 3285251883Speter** 64-bit signed integer and returns that integer, or D if P does not 3286251883Speter** exist. If the value of P is something other than an integer, then 3287251883Speter** zero is returned. 3288251883Speter** 3289251883Speter** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and 3290251883Speter** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and 3291251883Speter** is not a database file pathname pointer that SQLite passed into the xOpen 3292251883Speter** VFS method, then the behavior of this routine is undefined and probably 3293251883Speter** undesirable. 3294251883Speter*/ 3295322444SpeterSQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam); 3296322444SpeterSQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault); 3297322444SpeterSQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64); 3298251883Speter 3299251883Speter 3300251883Speter/* 3301251883Speter** CAPI3REF: Error Codes And Messages 3302286510Speter** METHOD: sqlite3 3303251883Speter** 3304282328Sbapt** ^If the most recent sqlite3_* API call associated with 3305282328Sbapt** [database connection] D failed, then the sqlite3_errcode(D) interface 3306282328Sbapt** returns the numeric [result code] or [extended result code] for that 3307282328Sbapt** API call. 3308282328Sbapt** If the most recent API call was successful, 3309282328Sbapt** then the return value from sqlite3_errcode() is undefined. 3310282328Sbapt** ^The sqlite3_extended_errcode() 3311251883Speter** interface is the same except that it always returns the 3312251883Speter** [extended result code] even when extended result codes are 3313251883Speter** disabled. 3314251883Speter** 3315251883Speter** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language 3316251883Speter** text that describes the error, as either UTF-8 or UTF-16 respectively. 3317251883Speter** ^(Memory to hold the error message string is managed internally. 3318251883Speter** The application does not need to worry about freeing the result. 3319251883Speter** However, the error string might be overwritten or deallocated by 3320251883Speter** subsequent calls to other SQLite interface functions.)^ 3321251883Speter** 3322251883Speter** ^The sqlite3_errstr() interface returns the English-language text 3323251883Speter** that describes the [result code], as UTF-8. 3324251883Speter** ^(Memory to hold the error message string is managed internally 3325251883Speter** and must not be freed by the application)^. 3326251883Speter** 3327251883Speter** When the serialized [threading mode] is in use, it might be the 3328251883Speter** case that a second error occurs on a separate thread in between 3329251883Speter** the time of the first error and the call to these interfaces. 3330251883Speter** When that happens, the second error will be reported since these 3331251883Speter** interfaces always report the most recent result. To avoid 3332251883Speter** this, each thread can obtain exclusive use of the [database connection] D 3333251883Speter** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning 3334251883Speter** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after 3335251883Speter** all calls to the interfaces listed here are completed. 3336251883Speter** 3337251883Speter** If an interface fails with SQLITE_MISUSE, that means the interface 3338251883Speter** was invoked incorrectly by the application. In that case, the 3339251883Speter** error code and message may or may not be set. 3340251883Speter*/ 3341322444SpeterSQLITE_API int sqlite3_errcode(sqlite3 *db); 3342322444SpeterSQLITE_API int sqlite3_extended_errcode(sqlite3 *db); 3343322444SpeterSQLITE_API const char *sqlite3_errmsg(sqlite3*); 3344322444SpeterSQLITE_API const void *sqlite3_errmsg16(sqlite3*); 3345322444SpeterSQLITE_API const char *sqlite3_errstr(int); 3346251883Speter 3347251883Speter/* 3348286510Speter** CAPI3REF: Prepared Statement Object 3349251883Speter** KEYWORDS: {prepared statement} {prepared statements} 3350251883Speter** 3351286510Speter** An instance of this object represents a single SQL statement that 3352286510Speter** has been compiled into binary form and is ready to be evaluated. 3353251883Speter** 3354286510Speter** Think of each SQL statement as a separate computer program. The 3355286510Speter** original SQL text is source code. A prepared statement object 3356286510Speter** is the compiled object code. All SQL must be converted into a 3357286510Speter** prepared statement before it can be run. 3358251883Speter** 3359286510Speter** The life-cycle of a prepared statement object usually goes like this: 3360286510Speter** 3361251883Speter** <ol> 3362286510Speter** <li> Create the prepared statement object using [sqlite3_prepare_v2()]. 3363286510Speter** <li> Bind values to [parameters] using the sqlite3_bind_*() 3364251883Speter** interfaces. 3365251883Speter** <li> Run the SQL by calling [sqlite3_step()] one or more times. 3366286510Speter** <li> Reset the prepared statement using [sqlite3_reset()] then go back 3367251883Speter** to step 2. Do this zero or more times. 3368251883Speter** <li> Destroy the object using [sqlite3_finalize()]. 3369251883Speter** </ol> 3370251883Speter*/ 3371251883Spetertypedef struct sqlite3_stmt sqlite3_stmt; 3372251883Speter 3373251883Speter/* 3374251883Speter** CAPI3REF: Run-time Limits 3375286510Speter** METHOD: sqlite3 3376251883Speter** 3377251883Speter** ^(This interface allows the size of various constructs to be limited 3378251883Speter** on a connection by connection basis. The first parameter is the 3379251883Speter** [database connection] whose limit is to be set or queried. The 3380251883Speter** second parameter is one of the [limit categories] that define a 3381251883Speter** class of constructs to be size limited. The third parameter is the 3382251883Speter** new limit for that construct.)^ 3383251883Speter** 3384251883Speter** ^If the new limit is a negative number, the limit is unchanged. 3385251883Speter** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a 3386251883Speter** [limits | hard upper bound] 3387251883Speter** set at compile-time by a C preprocessor macro called 3388251883Speter** [limits | SQLITE_MAX_<i>NAME</i>]. 3389251883Speter** (The "_LIMIT_" in the name is changed to "_MAX_".))^ 3390251883Speter** ^Attempts to increase a limit above its hard upper bound are 3391251883Speter** silently truncated to the hard upper bound. 3392251883Speter** 3393251883Speter** ^Regardless of whether or not the limit was changed, the 3394251883Speter** [sqlite3_limit()] interface returns the prior value of the limit. 3395251883Speter** ^Hence, to find the current value of a limit without changing it, 3396251883Speter** simply invoke this interface with the third parameter set to -1. 3397251883Speter** 3398251883Speter** Run-time limits are intended for use in applications that manage 3399251883Speter** both their own internal database and also databases that are controlled 3400251883Speter** by untrusted external sources. An example application might be a 3401251883Speter** web browser that has its own databases for storing history and 3402251883Speter** separate databases controlled by JavaScript applications downloaded 3403251883Speter** off the Internet. The internal databases can be given the 3404251883Speter** large, default limits. Databases managed by external sources can 3405251883Speter** be given much smaller limits designed to prevent a denial of service 3406251883Speter** attack. Developers might also want to use the [sqlite3_set_authorizer()] 3407251883Speter** interface to further control untrusted SQL. The size of the database 3408251883Speter** created by an untrusted script can be contained using the 3409251883Speter** [max_page_count] [PRAGMA]. 3410251883Speter** 3411251883Speter** New run-time limit categories may be added in future releases. 3412251883Speter*/ 3413322444SpeterSQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal); 3414251883Speter 3415251883Speter/* 3416251883Speter** CAPI3REF: Run-Time Limit Categories 3417251883Speter** KEYWORDS: {limit category} {*limit categories} 3418251883Speter** 3419251883Speter** These constants define various performance limits 3420251883Speter** that can be lowered at run-time using [sqlite3_limit()]. 3421251883Speter** The synopsis of the meanings of the various limits is shown below. 3422251883Speter** Additional information is available at [limits | Limits in SQLite]. 3423251883Speter** 3424251883Speter** <dl> 3425251883Speter** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt> 3426251883Speter** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^ 3427251883Speter** 3428251883Speter** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt> 3429251883Speter** <dd>The maximum length of an SQL statement, in bytes.</dd>)^ 3430251883Speter** 3431251883Speter** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt> 3432251883Speter** <dd>The maximum number of columns in a table definition or in the 3433251883Speter** result set of a [SELECT] or the maximum number of columns in an index 3434251883Speter** or in an ORDER BY or GROUP BY clause.</dd>)^ 3435251883Speter** 3436251883Speter** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt> 3437251883Speter** <dd>The maximum depth of the parse tree on any expression.</dd>)^ 3438251883Speter** 3439251883Speter** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt> 3440251883Speter** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^ 3441251883Speter** 3442251883Speter** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt> 3443251883Speter** <dd>The maximum number of instructions in a virtual machine program 3444322444Speter** used to implement an SQL statement. If [sqlite3_prepare_v2()] or 3445322444Speter** the equivalent tries to allocate space for more than this many opcodes 3446322444Speter** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^ 3447251883Speter** 3448251883Speter** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt> 3449251883Speter** <dd>The maximum number of arguments on a function.</dd>)^ 3450251883Speter** 3451251883Speter** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt> 3452251883Speter** <dd>The maximum number of [ATTACH | attached databases].)^</dd> 3453251883Speter** 3454251883Speter** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]] 3455251883Speter** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt> 3456251883Speter** <dd>The maximum length of the pattern argument to the [LIKE] or 3457251883Speter** [GLOB] operators.</dd>)^ 3458251883Speter** 3459251883Speter** [[SQLITE_LIMIT_VARIABLE_NUMBER]] 3460251883Speter** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt> 3461251883Speter** <dd>The maximum index number of any [parameter] in an SQL statement.)^ 3462251883Speter** 3463251883Speter** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt> 3464251883Speter** <dd>The maximum depth of recursion for triggers.</dd>)^ 3465274884Sbapt** 3466274884Sbapt** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt> 3467274884Sbapt** <dd>The maximum number of auxiliary worker threads that a single 3468274884Sbapt** [prepared statement] may start.</dd>)^ 3469251883Speter** </dl> 3470251883Speter*/ 3471251883Speter#define SQLITE_LIMIT_LENGTH 0 3472251883Speter#define SQLITE_LIMIT_SQL_LENGTH 1 3473251883Speter#define SQLITE_LIMIT_COLUMN 2 3474251883Speter#define SQLITE_LIMIT_EXPR_DEPTH 3 3475251883Speter#define SQLITE_LIMIT_COMPOUND_SELECT 4 3476251883Speter#define SQLITE_LIMIT_VDBE_OP 5 3477251883Speter#define SQLITE_LIMIT_FUNCTION_ARG 6 3478251883Speter#define SQLITE_LIMIT_ATTACHED 7 3479251883Speter#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8 3480251883Speter#define SQLITE_LIMIT_VARIABLE_NUMBER 9 3481251883Speter#define SQLITE_LIMIT_TRIGGER_DEPTH 10 3482274884Sbapt#define SQLITE_LIMIT_WORKER_THREADS 11 3483251883Speter 3484251883Speter/* 3485322444Speter** CAPI3REF: Prepare Flags 3486322444Speter** 3487322444Speter** These constants define various flags that can be passed into 3488322444Speter** "prepFlags" parameter of the [sqlite3_prepare_v3()] and 3489322444Speter** [sqlite3_prepare16_v3()] interfaces. 3490322444Speter** 3491322444Speter** New flags may be added in future releases of SQLite. 3492322444Speter** 3493322444Speter** <dl> 3494322444Speter** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt> 3495322444Speter** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner 3496322444Speter** that the prepared statement will be retained for a long time and 3497322444Speter** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()] 3498322444Speter** and [sqlite3_prepare16_v3()] assume that the prepared statement will 3499322444Speter** be used just once or at most a few times and then destroyed using 3500322444Speter** [sqlite3_finalize()] relatively soon. The current implementation acts 3501322444Speter** on this hint by avoiding the use of [lookaside memory] so as not to 3502322444Speter** deplete the limited store of lookaside memory. Future versions of 3503322444Speter** SQLite may act on this hint differently. 3504322444Speter** </dl> 3505322444Speter*/ 3506322444Speter#define SQLITE_PREPARE_PERSISTENT 0x01 3507322444Speter 3508322444Speter/* 3509251883Speter** CAPI3REF: Compiling An SQL Statement 3510251883Speter** KEYWORDS: {SQL statement compiler} 3511286510Speter** METHOD: sqlite3 3512286510Speter** CONSTRUCTOR: sqlite3_stmt 3513251883Speter** 3514322444Speter** To execute an SQL statement, it must first be compiled into a byte-code 3515322444Speter** program using one of these routines. Or, in other words, these routines 3516322444Speter** are constructors for the [prepared statement] object. 3517251883Speter** 3518322444Speter** The preferred routine to use is [sqlite3_prepare_v2()]. The 3519322444Speter** [sqlite3_prepare()] interface is legacy and should be avoided. 3520322444Speter** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used 3521322444Speter** for special purposes. 3522322444Speter** 3523322444Speter** The use of the UTF-8 interfaces is preferred, as SQLite currently 3524322444Speter** does all parsing using UTF-8. The UTF-16 interfaces are provided 3525322444Speter** as a convenience. The UTF-16 interfaces work by converting the 3526322444Speter** input text into UTF-8, then invoking the corresponding UTF-8 interface. 3527322444Speter** 3528251883Speter** The first argument, "db", is a [database connection] obtained from a 3529251883Speter** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or 3530251883Speter** [sqlite3_open16()]. The database connection must not have been closed. 3531251883Speter** 3532251883Speter** The second argument, "zSql", is the statement to be compiled, encoded 3533322444Speter** as either UTF-8 or UTF-16. The sqlite3_prepare(), sqlite3_prepare_v2(), 3534322444Speter** and sqlite3_prepare_v3() 3535322444Speter** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(), 3536322444Speter** and sqlite3_prepare16_v3() use UTF-16. 3537251883Speter** 3538282328Sbapt** ^If the nByte argument is negative, then zSql is read up to the 3539282328Sbapt** first zero terminator. ^If nByte is positive, then it is the 3540282328Sbapt** number of bytes read from zSql. ^If nByte is zero, then no prepared 3541282328Sbapt** statement is generated. 3542282328Sbapt** If the caller knows that the supplied string is nul-terminated, then 3543282328Sbapt** there is a small performance advantage to passing an nByte parameter that 3544282328Sbapt** is the number of bytes in the input string <i>including</i> 3545282328Sbapt** the nul-terminator. 3546251883Speter** 3547251883Speter** ^If pzTail is not NULL then *pzTail is made to point to the first byte 3548251883Speter** past the end of the first SQL statement in zSql. These routines only 3549251883Speter** compile the first statement in zSql, so *pzTail is left pointing to 3550251883Speter** what remains uncompiled. 3551251883Speter** 3552251883Speter** ^*ppStmt is left pointing to a compiled [prepared statement] that can be 3553251883Speter** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set 3554251883Speter** to NULL. ^If the input text contains no SQL (if the input is an empty 3555251883Speter** string or a comment) then *ppStmt is set to NULL. 3556251883Speter** The calling procedure is responsible for deleting the compiled 3557251883Speter** SQL statement using [sqlite3_finalize()] after it has finished with it. 3558251883Speter** ppStmt may not be NULL. 3559251883Speter** 3560251883Speter** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK]; 3561251883Speter** otherwise an [error code] is returned. 3562251883Speter** 3563322444Speter** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(), 3564322444Speter** and sqlite3_prepare16_v3() interfaces are recommended for all new programs. 3565322444Speter** The older interfaces (sqlite3_prepare() and sqlite3_prepare16()) 3566322444Speter** are retained for backwards compatibility, but their use is discouraged. 3567322444Speter** ^In the "vX" interfaces, the prepared statement 3568251883Speter** that is returned (the [sqlite3_stmt] object) contains a copy of the 3569251883Speter** original SQL text. This causes the [sqlite3_step()] interface to 3570251883Speter** behave differently in three ways: 3571251883Speter** 3572251883Speter** <ol> 3573251883Speter** <li> 3574251883Speter** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it 3575251883Speter** always used to do, [sqlite3_step()] will automatically recompile the SQL 3576251883Speter** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY] 3577251883Speter** retries will occur before sqlite3_step() gives up and returns an error. 3578251883Speter** </li> 3579251883Speter** 3580251883Speter** <li> 3581251883Speter** ^When an error occurs, [sqlite3_step()] will return one of the detailed 3582251883Speter** [error codes] or [extended error codes]. ^The legacy behavior was that 3583251883Speter** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code 3584251883Speter** and the application would have to make a second call to [sqlite3_reset()] 3585251883Speter** in order to find the underlying cause of the problem. With the "v2" prepare 3586251883Speter** interfaces, the underlying reason for the error is returned immediately. 3587251883Speter** </li> 3588251883Speter** 3589251883Speter** <li> 3590251883Speter** ^If the specific value bound to [parameter | host parameter] in the 3591251883Speter** WHERE clause might influence the choice of query plan for a statement, 3592251883Speter** then the statement will be automatically recompiled, as if there had been 3593251883Speter** a schema change, on the first [sqlite3_step()] call following any change 3594251883Speter** to the [sqlite3_bind_text | bindings] of that [parameter]. 3595251883Speter** ^The specific value of WHERE-clause [parameter] might influence the 3596251883Speter** choice of query plan if the parameter is the left-hand side of a [LIKE] 3597251883Speter** or [GLOB] operator or if the parameter is compared to an indexed column 3598251883Speter** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled. 3599251883Speter** </li> 3600322444Speter** 3601322444Speter** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having 3602322444Speter** the extra prepFlags parameter, which is a bit array consisting of zero or 3603322444Speter** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags. ^The 3604322444Speter** sqlite3_prepare_v2() interface works exactly the same as 3605322444Speter** sqlite3_prepare_v3() with a zero prepFlags parameter. 3606251883Speter** </ol> 3607251883Speter*/ 3608322444SpeterSQLITE_API int sqlite3_prepare( 3609251883Speter sqlite3 *db, /* Database handle */ 3610251883Speter const char *zSql, /* SQL statement, UTF-8 encoded */ 3611251883Speter int nByte, /* Maximum length of zSql in bytes. */ 3612251883Speter sqlite3_stmt **ppStmt, /* OUT: Statement handle */ 3613251883Speter const char **pzTail /* OUT: Pointer to unused portion of zSql */ 3614251883Speter); 3615322444SpeterSQLITE_API int sqlite3_prepare_v2( 3616251883Speter sqlite3 *db, /* Database handle */ 3617251883Speter const char *zSql, /* SQL statement, UTF-8 encoded */ 3618251883Speter int nByte, /* Maximum length of zSql in bytes. */ 3619251883Speter sqlite3_stmt **ppStmt, /* OUT: Statement handle */ 3620251883Speter const char **pzTail /* OUT: Pointer to unused portion of zSql */ 3621251883Speter); 3622322444SpeterSQLITE_API int sqlite3_prepare_v3( 3623251883Speter sqlite3 *db, /* Database handle */ 3624322444Speter const char *zSql, /* SQL statement, UTF-8 encoded */ 3625322444Speter int nByte, /* Maximum length of zSql in bytes. */ 3626322444Speter unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */ 3627322444Speter sqlite3_stmt **ppStmt, /* OUT: Statement handle */ 3628322444Speter const char **pzTail /* OUT: Pointer to unused portion of zSql */ 3629322444Speter); 3630322444SpeterSQLITE_API int sqlite3_prepare16( 3631322444Speter sqlite3 *db, /* Database handle */ 3632251883Speter const void *zSql, /* SQL statement, UTF-16 encoded */ 3633251883Speter int nByte, /* Maximum length of zSql in bytes. */ 3634251883Speter sqlite3_stmt **ppStmt, /* OUT: Statement handle */ 3635251883Speter const void **pzTail /* OUT: Pointer to unused portion of zSql */ 3636251883Speter); 3637322444SpeterSQLITE_API int sqlite3_prepare16_v2( 3638251883Speter sqlite3 *db, /* Database handle */ 3639251883Speter const void *zSql, /* SQL statement, UTF-16 encoded */ 3640251883Speter int nByte, /* Maximum length of zSql in bytes. */ 3641251883Speter sqlite3_stmt **ppStmt, /* OUT: Statement handle */ 3642251883Speter const void **pzTail /* OUT: Pointer to unused portion of zSql */ 3643251883Speter); 3644322444SpeterSQLITE_API int sqlite3_prepare16_v3( 3645322444Speter sqlite3 *db, /* Database handle */ 3646322444Speter const void *zSql, /* SQL statement, UTF-16 encoded */ 3647322444Speter int nByte, /* Maximum length of zSql in bytes. */ 3648322444Speter unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */ 3649322444Speter sqlite3_stmt **ppStmt, /* OUT: Statement handle */ 3650322444Speter const void **pzTail /* OUT: Pointer to unused portion of zSql */ 3651322444Speter); 3652251883Speter 3653251883Speter/* 3654251883Speter** CAPI3REF: Retrieving Statement SQL 3655286510Speter** METHOD: sqlite3_stmt 3656251883Speter** 3657305002Scy** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8 3658305002Scy** SQL text used to create [prepared statement] P if P was 3659322444Speter** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], 3660322444Speter** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()]. 3661305002Scy** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8 3662305002Scy** string containing the SQL text of prepared statement P with 3663305002Scy** [bound parameters] expanded. 3664305002Scy** 3665305002Scy** ^(For example, if a prepared statement is created using the SQL 3666305002Scy** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345 3667305002Scy** and parameter :xyz is unbound, then sqlite3_sql() will return 3668305002Scy** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql() 3669305002Scy** will return "SELECT 2345,NULL".)^ 3670305002Scy** 3671305002Scy** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory 3672305002Scy** is available to hold the result, or if the result would exceed the 3673305002Scy** the maximum string length determined by the [SQLITE_LIMIT_LENGTH]. 3674305002Scy** 3675305002Scy** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of 3676305002Scy** bound parameter expansions. ^The [SQLITE_OMIT_TRACE] compile-time 3677305002Scy** option causes sqlite3_expanded_sql() to always return NULL. 3678305002Scy** 3679305002Scy** ^The string returned by sqlite3_sql(P) is managed by SQLite and is 3680305002Scy** automatically freed when the prepared statement is finalized. 3681305002Scy** ^The string returned by sqlite3_expanded_sql(P), on the other hand, 3682305002Scy** is obtained from [sqlite3_malloc()] and must be free by the application 3683305002Scy** by passing it to [sqlite3_free()]. 3684251883Speter*/ 3685322444SpeterSQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt); 3686322444SpeterSQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt); 3687251883Speter 3688251883Speter/* 3689251883Speter** CAPI3REF: Determine If An SQL Statement Writes The Database 3690286510Speter** METHOD: sqlite3_stmt 3691251883Speter** 3692251883Speter** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if 3693251883Speter** and only if the [prepared statement] X makes no direct changes to 3694251883Speter** the content of the database file. 3695251883Speter** 3696251883Speter** Note that [application-defined SQL functions] or 3697251883Speter** [virtual tables] might change the database indirectly as a side effect. 3698251883Speter** ^(For example, if an application defines a function "eval()" that 3699251883Speter** calls [sqlite3_exec()], then the following SQL statement would 3700251883Speter** change the database file through side-effects: 3701251883Speter** 3702251883Speter** <blockquote><pre> 3703251883Speter** SELECT eval('DELETE FROM t1') FROM t2; 3704251883Speter** </pre></blockquote> 3705251883Speter** 3706251883Speter** But because the [SELECT] statement does not change the database file 3707251883Speter** directly, sqlite3_stmt_readonly() would still return true.)^ 3708251883Speter** 3709251883Speter** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK], 3710251883Speter** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true, 3711251883Speter** since the statements themselves do not actually modify the database but 3712251883Speter** rather they control the timing of when other statements modify the 3713251883Speter** database. ^The [ATTACH] and [DETACH] statements also cause 3714251883Speter** sqlite3_stmt_readonly() to return true since, while those statements 3715251883Speter** change the configuration of a database connection, they do not make 3716251883Speter** changes to the content of the database files on disk. 3717322444Speter** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since 3718322444Speter** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and 3719322444Speter** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so 3720322444Speter** sqlite3_stmt_readonly() returns false for those commands. 3721251883Speter*/ 3722322444SpeterSQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt); 3723251883Speter 3724251883Speter/* 3725251883Speter** CAPI3REF: Determine If A Prepared Statement Has Been Reset 3726286510Speter** METHOD: sqlite3_stmt 3727251883Speter** 3728251883Speter** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the 3729251883Speter** [prepared statement] S has been stepped at least once using 3730298161Sbapt** [sqlite3_step(S)] but has neither run to completion (returned 3731298161Sbapt** [SQLITE_DONE] from [sqlite3_step(S)]) nor 3732251883Speter** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S) 3733251883Speter** interface returns false if S is a NULL pointer. If S is not a 3734251883Speter** NULL pointer and is not a pointer to a valid [prepared statement] 3735251883Speter** object, then the behavior is undefined and probably undesirable. 3736251883Speter** 3737251883Speter** This interface can be used in combination [sqlite3_next_stmt()] 3738251883Speter** to locate all prepared statements associated with a database 3739251883Speter** connection that are in need of being reset. This can be used, 3740251883Speter** for example, in diagnostic routines to search for prepared 3741251883Speter** statements that are holding a transaction open. 3742251883Speter*/ 3743322444SpeterSQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*); 3744251883Speter 3745251883Speter/* 3746251883Speter** CAPI3REF: Dynamically Typed Value Object 3747251883Speter** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value} 3748251883Speter** 3749251883Speter** SQLite uses the sqlite3_value object to represent all values 3750251883Speter** that can be stored in a database table. SQLite uses dynamic typing 3751251883Speter** for the values it stores. ^Values stored in sqlite3_value objects 3752251883Speter** can be integers, floating point values, strings, BLOBs, or NULL. 3753251883Speter** 3754251883Speter** An sqlite3_value object may be either "protected" or "unprotected". 3755251883Speter** Some interfaces require a protected sqlite3_value. Other interfaces 3756251883Speter** will accept either a protected or an unprotected sqlite3_value. 3757251883Speter** Every interface that accepts sqlite3_value arguments specifies 3758286510Speter** whether or not it requires a protected sqlite3_value. The 3759286510Speter** [sqlite3_value_dup()] interface can be used to construct a new 3760286510Speter** protected sqlite3_value from an unprotected sqlite3_value. 3761251883Speter** 3762251883Speter** The terms "protected" and "unprotected" refer to whether or not 3763251883Speter** a mutex is held. An internal mutex is held for a protected 3764251883Speter** sqlite3_value object but no mutex is held for an unprotected 3765251883Speter** sqlite3_value object. If SQLite is compiled to be single-threaded 3766251883Speter** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0) 3767251883Speter** or if SQLite is run in one of reduced mutex modes 3768251883Speter** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] 3769251883Speter** then there is no distinction between protected and unprotected 3770251883Speter** sqlite3_value objects and they can be used interchangeably. However, 3771251883Speter** for maximum code portability it is recommended that applications 3772251883Speter** still make the distinction between protected and unprotected 3773251883Speter** sqlite3_value objects even when not strictly required. 3774251883Speter** 3775251883Speter** ^The sqlite3_value objects that are passed as parameters into the 3776251883Speter** implementation of [application-defined SQL functions] are protected. 3777251883Speter** ^The sqlite3_value object returned by 3778251883Speter** [sqlite3_column_value()] is unprotected. 3779251883Speter** Unprotected sqlite3_value objects may only be used with 3780251883Speter** [sqlite3_result_value()] and [sqlite3_bind_value()]. 3781251883Speter** The [sqlite3_value_blob | sqlite3_value_type()] family of 3782251883Speter** interfaces require protected sqlite3_value objects. 3783251883Speter*/ 3784322444Spetertypedef struct sqlite3_value sqlite3_value; 3785251883Speter 3786251883Speter/* 3787251883Speter** CAPI3REF: SQL Function Context Object 3788251883Speter** 3789251883Speter** The context in which an SQL function executes is stored in an 3790251883Speter** sqlite3_context object. ^A pointer to an sqlite3_context object 3791251883Speter** is always first parameter to [application-defined SQL functions]. 3792251883Speter** The application-defined SQL function implementation will pass this 3793251883Speter** pointer through into calls to [sqlite3_result_int | sqlite3_result()], 3794251883Speter** [sqlite3_aggregate_context()], [sqlite3_user_data()], 3795251883Speter** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()], 3796251883Speter** and/or [sqlite3_set_auxdata()]. 3797251883Speter*/ 3798251883Spetertypedef struct sqlite3_context sqlite3_context; 3799251883Speter 3800251883Speter/* 3801251883Speter** CAPI3REF: Binding Values To Prepared Statements 3802251883Speter** KEYWORDS: {host parameter} {host parameters} {host parameter name} 3803251883Speter** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding} 3804286510Speter** METHOD: sqlite3_stmt 3805251883Speter** 3806251883Speter** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants, 3807251883Speter** literals may be replaced by a [parameter] that matches one of following 3808251883Speter** templates: 3809251883Speter** 3810251883Speter** <ul> 3811251883Speter** <li> ? 3812251883Speter** <li> ?NNN 3813251883Speter** <li> :VVV 3814251883Speter** <li> @VVV 3815251883Speter** <li> $VVV 3816251883Speter** </ul> 3817251883Speter** 3818251883Speter** In the templates above, NNN represents an integer literal, 3819251883Speter** and VVV represents an alphanumeric identifier.)^ ^The values of these 3820251883Speter** parameters (also called "host parameter names" or "SQL parameters") 3821251883Speter** can be set using the sqlite3_bind_*() routines defined here. 3822251883Speter** 3823251883Speter** ^The first argument to the sqlite3_bind_*() routines is always 3824251883Speter** a pointer to the [sqlite3_stmt] object returned from 3825251883Speter** [sqlite3_prepare_v2()] or its variants. 3826251883Speter** 3827251883Speter** ^The second argument is the index of the SQL parameter to be set. 3828251883Speter** ^The leftmost SQL parameter has an index of 1. ^When the same named 3829251883Speter** SQL parameter is used more than once, second and subsequent 3830251883Speter** occurrences have the same index as the first occurrence. 3831251883Speter** ^The index for named parameters can be looked up using the 3832251883Speter** [sqlite3_bind_parameter_index()] API if desired. ^The index 3833251883Speter** for "?NNN" parameters is the value of NNN. 3834251883Speter** ^The NNN value must be between 1 and the [sqlite3_limit()] 3835251883Speter** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999). 3836251883Speter** 3837251883Speter** ^The third argument is the value to bind to the parameter. 3838251883Speter** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16() 3839251883Speter** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter 3840251883Speter** is ignored and the end result is the same as sqlite3_bind_null(). 3841251883Speter** 3842251883Speter** ^(In those routines that have a fourth argument, its value is the 3843251883Speter** number of bytes in the parameter. To be clear: the value is the 3844251883Speter** number of <u>bytes</u> in the value, not the number of characters.)^ 3845251883Speter** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16() 3846251883Speter** is negative, then the length of the string is 3847251883Speter** the number of bytes up to the first zero terminator. 3848251883Speter** If the fourth parameter to sqlite3_bind_blob() is negative, then 3849251883Speter** the behavior is undefined. 3850251883Speter** If a non-negative fourth parameter is provided to sqlite3_bind_text() 3851274884Sbapt** or sqlite3_bind_text16() or sqlite3_bind_text64() then 3852274884Sbapt** that parameter must be the byte offset 3853251883Speter** where the NUL terminator would occur assuming the string were NUL 3854251883Speter** terminated. If any NUL characters occur at byte offsets less than 3855251883Speter** the value of the fourth parameter then the resulting string value will 3856251883Speter** contain embedded NULs. The result of expressions involving strings 3857251883Speter** with embedded NULs is undefined. 3858251883Speter** 3859274884Sbapt** ^The fifth argument to the BLOB and string binding interfaces 3860274884Sbapt** is a destructor used to dispose of the BLOB or 3861251883Speter** string after SQLite has finished with it. ^The destructor is called 3862274884Sbapt** to dispose of the BLOB or string even if the call to bind API fails. 3863251883Speter** ^If the fifth argument is 3864251883Speter** the special value [SQLITE_STATIC], then SQLite assumes that the 3865251883Speter** information is in static, unmanaged space and does not need to be freed. 3866251883Speter** ^If the fifth argument has the value [SQLITE_TRANSIENT], then 3867251883Speter** SQLite makes its own private copy of the data immediately, before 3868251883Speter** the sqlite3_bind_*() routine returns. 3869251883Speter** 3870274884Sbapt** ^The sixth argument to sqlite3_bind_text64() must be one of 3871274884Sbapt** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE] 3872274884Sbapt** to specify the encoding of the text in the third parameter. If 3873274884Sbapt** the sixth argument to sqlite3_bind_text64() is not one of the 3874274884Sbapt** allowed values shown above, or if the text encoding is different 3875274884Sbapt** from the encoding specified by the sixth parameter, then the behavior 3876274884Sbapt** is undefined. 3877274884Sbapt** 3878251883Speter** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that 3879251883Speter** is filled with zeroes. ^A zeroblob uses a fixed amount of memory 3880251883Speter** (just an integer to hold its size) while it is being processed. 3881251883Speter** Zeroblobs are intended to serve as placeholders for BLOBs whose 3882251883Speter** content is later written using 3883251883Speter** [sqlite3_blob_open | incremental BLOB I/O] routines. 3884251883Speter** ^A negative value for the zeroblob results in a zero-length BLOB. 3885251883Speter** 3886322444Speter** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in 3887322444Speter** [prepared statement] S to have an SQL value of NULL, but to also be 3888322444Speter** associated with the pointer P of type T. ^D is either a NULL pointer or 3889322444Speter** a pointer to a destructor function for P. ^SQLite will invoke the 3890322444Speter** destructor D with a single argument of P when it is finished using 3891322444Speter** P. The T parameter should be a static string, preferably a string 3892322444Speter** literal. The sqlite3_bind_pointer() routine is part of the 3893322444Speter** [pointer passing interface] added for SQLite 3.20.0. 3894322444Speter** 3895251883Speter** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer 3896251883Speter** for the [prepared statement] or with a prepared statement for which 3897251883Speter** [sqlite3_step()] has been called more recently than [sqlite3_reset()], 3898251883Speter** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_() 3899251883Speter** routine is passed a [prepared statement] that has been finalized, the 3900251883Speter** result is undefined and probably harmful. 3901251883Speter** 3902251883Speter** ^Bindings are not cleared by the [sqlite3_reset()] routine. 3903251883Speter** ^Unbound parameters are interpreted as NULL. 3904251883Speter** 3905251883Speter** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an 3906251883Speter** [error code] if anything goes wrong. 3907274884Sbapt** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB 3908274884Sbapt** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or 3909274884Sbapt** [SQLITE_MAX_LENGTH]. 3910251883Speter** ^[SQLITE_RANGE] is returned if the parameter 3911251883Speter** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails. 3912251883Speter** 3913251883Speter** See also: [sqlite3_bind_parameter_count()], 3914251883Speter** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()]. 3915251883Speter*/ 3916322444SpeterSQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); 3917322444SpeterSQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64, 3918274884Sbapt void(*)(void*)); 3919322444SpeterSQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double); 3920322444SpeterSQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int); 3921322444SpeterSQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); 3922322444SpeterSQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int); 3923322444SpeterSQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*)); 3924322444SpeterSQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); 3925322444SpeterSQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64, 3926274884Sbapt void(*)(void*), unsigned char encoding); 3927322444SpeterSQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); 3928322444SpeterSQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*)); 3929322444SpeterSQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); 3930322444SpeterSQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64); 3931251883Speter 3932251883Speter/* 3933251883Speter** CAPI3REF: Number Of SQL Parameters 3934286510Speter** METHOD: sqlite3_stmt 3935251883Speter** 3936251883Speter** ^This routine can be used to find the number of [SQL parameters] 3937251883Speter** in a [prepared statement]. SQL parameters are tokens of the 3938251883Speter** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as 3939251883Speter** placeholders for values that are [sqlite3_bind_blob | bound] 3940251883Speter** to the parameters at a later time. 3941251883Speter** 3942251883Speter** ^(This routine actually returns the index of the largest (rightmost) 3943251883Speter** parameter. For all forms except ?NNN, this will correspond to the 3944251883Speter** number of unique parameters. If parameters of the ?NNN form are used, 3945251883Speter** there may be gaps in the list.)^ 3946251883Speter** 3947251883Speter** See also: [sqlite3_bind_blob|sqlite3_bind()], 3948251883Speter** [sqlite3_bind_parameter_name()], and 3949251883Speter** [sqlite3_bind_parameter_index()]. 3950251883Speter*/ 3951322444SpeterSQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*); 3952251883Speter 3953251883Speter/* 3954251883Speter** CAPI3REF: Name Of A Host Parameter 3955286510Speter** METHOD: sqlite3_stmt 3956251883Speter** 3957251883Speter** ^The sqlite3_bind_parameter_name(P,N) interface returns 3958251883Speter** the name of the N-th [SQL parameter] in the [prepared statement] P. 3959251883Speter** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA" 3960251883Speter** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA" 3961251883Speter** respectively. 3962251883Speter** In other words, the initial ":" or "$" or "@" or "?" 3963251883Speter** is included as part of the name.)^ 3964251883Speter** ^Parameters of the form "?" without a following integer have no name 3965251883Speter** and are referred to as "nameless" or "anonymous parameters". 3966251883Speter** 3967251883Speter** ^The first host parameter has an index of 1, not 0. 3968251883Speter** 3969251883Speter** ^If the value N is out of range or if the N-th parameter is 3970251883Speter** nameless, then NULL is returned. ^The returned string is 3971251883Speter** always in UTF-8 encoding even if the named parameter was 3972322444Speter** originally specified as UTF-16 in [sqlite3_prepare16()], 3973322444Speter** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()]. 3974251883Speter** 3975251883Speter** See also: [sqlite3_bind_blob|sqlite3_bind()], 3976251883Speter** [sqlite3_bind_parameter_count()], and 3977251883Speter** [sqlite3_bind_parameter_index()]. 3978251883Speter*/ 3979322444SpeterSQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); 3980251883Speter 3981251883Speter/* 3982251883Speter** CAPI3REF: Index Of A Parameter With A Given Name 3983286510Speter** METHOD: sqlite3_stmt 3984251883Speter** 3985251883Speter** ^Return the index of an SQL parameter given its name. ^The 3986251883Speter** index value returned is suitable for use as the second 3987251883Speter** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero 3988251883Speter** is returned if no matching parameter is found. ^The parameter 3989251883Speter** name must be given in UTF-8 even if the original statement 3990322444Speter** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or 3991322444Speter** [sqlite3_prepare16_v3()]. 3992251883Speter** 3993251883Speter** See also: [sqlite3_bind_blob|sqlite3_bind()], 3994251883Speter** [sqlite3_bind_parameter_count()], and 3995298161Sbapt** [sqlite3_bind_parameter_name()]. 3996251883Speter*/ 3997322444SpeterSQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); 3998251883Speter 3999251883Speter/* 4000251883Speter** CAPI3REF: Reset All Bindings On A Prepared Statement 4001286510Speter** METHOD: sqlite3_stmt 4002251883Speter** 4003251883Speter** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset 4004251883Speter** the [sqlite3_bind_blob | bindings] on a [prepared statement]. 4005251883Speter** ^Use this routine to reset all host parameters to NULL. 4006251883Speter*/ 4007322444SpeterSQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*); 4008251883Speter 4009251883Speter/* 4010251883Speter** CAPI3REF: Number Of Columns In A Result Set 4011286510Speter** METHOD: sqlite3_stmt 4012251883Speter** 4013251883Speter** ^Return the number of columns in the result set returned by the 4014322444Speter** [prepared statement]. ^If this routine returns 0, that means the 4015322444Speter** [prepared statement] returns no data (for example an [UPDATE]). 4016322444Speter** ^However, just because this routine returns a positive number does not 4017322444Speter** mean that one or more rows of data will be returned. ^A SELECT statement 4018322444Speter** will always have a positive sqlite3_column_count() but depending on the 4019322444Speter** WHERE clause constraints and the table content, it might return no rows. 4020251883Speter** 4021251883Speter** See also: [sqlite3_data_count()] 4022251883Speter*/ 4023322444SpeterSQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt); 4024251883Speter 4025251883Speter/* 4026251883Speter** CAPI3REF: Column Names In A Result Set 4027286510Speter** METHOD: sqlite3_stmt 4028251883Speter** 4029251883Speter** ^These routines return the name assigned to a particular column 4030251883Speter** in the result set of a [SELECT] statement. ^The sqlite3_column_name() 4031251883Speter** interface returns a pointer to a zero-terminated UTF-8 string 4032251883Speter** and sqlite3_column_name16() returns a pointer to a zero-terminated 4033251883Speter** UTF-16 string. ^The first parameter is the [prepared statement] 4034251883Speter** that implements the [SELECT] statement. ^The second parameter is the 4035251883Speter** column number. ^The leftmost column is number 0. 4036251883Speter** 4037251883Speter** ^The returned string pointer is valid until either the [prepared statement] 4038251883Speter** is destroyed by [sqlite3_finalize()] or until the statement is automatically 4039251883Speter** reprepared by the first call to [sqlite3_step()] for a particular run 4040251883Speter** or until the next call to 4041251883Speter** sqlite3_column_name() or sqlite3_column_name16() on the same column. 4042251883Speter** 4043251883Speter** ^If sqlite3_malloc() fails during the processing of either routine 4044251883Speter** (for example during a conversion from UTF-8 to UTF-16) then a 4045251883Speter** NULL pointer is returned. 4046251883Speter** 4047251883Speter** ^The name of a result column is the value of the "AS" clause for 4048251883Speter** that column, if there is an AS clause. If there is no AS clause 4049251883Speter** then the name of the column is unspecified and may change from 4050251883Speter** one release of SQLite to the next. 4051251883Speter*/ 4052322444SpeterSQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N); 4053322444SpeterSQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N); 4054251883Speter 4055251883Speter/* 4056251883Speter** CAPI3REF: Source Of Data In A Query Result 4057286510Speter** METHOD: sqlite3_stmt 4058251883Speter** 4059251883Speter** ^These routines provide a means to determine the database, table, and 4060251883Speter** table column that is the origin of a particular result column in 4061251883Speter** [SELECT] statement. 4062251883Speter** ^The name of the database or table or column can be returned as 4063251883Speter** either a UTF-8 or UTF-16 string. ^The _database_ routines return 4064251883Speter** the database name, the _table_ routines return the table name, and 4065251883Speter** the origin_ routines return the column name. 4066251883Speter** ^The returned string is valid until the [prepared statement] is destroyed 4067251883Speter** using [sqlite3_finalize()] or until the statement is automatically 4068251883Speter** reprepared by the first call to [sqlite3_step()] for a particular run 4069251883Speter** or until the same information is requested 4070251883Speter** again in a different encoding. 4071251883Speter** 4072251883Speter** ^The names returned are the original un-aliased names of the 4073251883Speter** database, table, and column. 4074251883Speter** 4075251883Speter** ^The first argument to these interfaces is a [prepared statement]. 4076251883Speter** ^These functions return information about the Nth result column returned by 4077251883Speter** the statement, where N is the second function argument. 4078251883Speter** ^The left-most column is column 0 for these routines. 4079251883Speter** 4080251883Speter** ^If the Nth column returned by the statement is an expression or 4081251883Speter** subquery and is not a column value, then all of these functions return 4082251883Speter** NULL. ^These routine might also return NULL if a memory allocation error 4083251883Speter** occurs. ^Otherwise, they return the name of the attached database, table, 4084251883Speter** or column that query result column was extracted from. 4085251883Speter** 4086251883Speter** ^As with all other SQLite APIs, those whose names end with "16" return 4087251883Speter** UTF-16 encoded strings and the other functions return UTF-8. 4088251883Speter** 4089251883Speter** ^These APIs are only available if the library was compiled with the 4090251883Speter** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol. 4091251883Speter** 4092251883Speter** If two or more threads call one or more of these routines against the same 4093251883Speter** prepared statement and column at the same time then the results are 4094251883Speter** undefined. 4095251883Speter** 4096251883Speter** If two or more threads call one or more 4097251883Speter** [sqlite3_column_database_name | column metadata interfaces] 4098251883Speter** for the same [prepared statement] and result column 4099251883Speter** at the same time then the results are undefined. 4100251883Speter*/ 4101322444SpeterSQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int); 4102322444SpeterSQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int); 4103322444SpeterSQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int); 4104322444SpeterSQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int); 4105322444SpeterSQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int); 4106322444SpeterSQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); 4107251883Speter 4108251883Speter/* 4109251883Speter** CAPI3REF: Declared Datatype Of A Query Result 4110286510Speter** METHOD: sqlite3_stmt 4111251883Speter** 4112251883Speter** ^(The first parameter is a [prepared statement]. 4113251883Speter** If this statement is a [SELECT] statement and the Nth column of the 4114251883Speter** returned result set of that [SELECT] is a table column (not an 4115251883Speter** expression or subquery) then the declared type of the table 4116251883Speter** column is returned.)^ ^If the Nth column of the result set is an 4117251883Speter** expression or subquery, then a NULL pointer is returned. 4118251883Speter** ^The returned string is always UTF-8 encoded. 4119251883Speter** 4120251883Speter** ^(For example, given the database schema: 4121251883Speter** 4122251883Speter** CREATE TABLE t1(c1 VARIANT); 4123251883Speter** 4124251883Speter** and the following statement to be compiled: 4125251883Speter** 4126251883Speter** SELECT c1 + 1, c1 FROM t1; 4127251883Speter** 4128251883Speter** this routine would return the string "VARIANT" for the second result 4129251883Speter** column (i==1), and a NULL pointer for the first result column (i==0).)^ 4130251883Speter** 4131251883Speter** ^SQLite uses dynamic run-time typing. ^So just because a column 4132251883Speter** is declared to contain a particular type does not mean that the 4133251883Speter** data stored in that column is of the declared type. SQLite is 4134251883Speter** strongly typed, but the typing is dynamic not static. ^Type 4135251883Speter** is associated with individual values, not with the containers 4136251883Speter** used to hold those values. 4137251883Speter*/ 4138322444SpeterSQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int); 4139322444SpeterSQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int); 4140251883Speter 4141251883Speter/* 4142251883Speter** CAPI3REF: Evaluate An SQL Statement 4143286510Speter** METHOD: sqlite3_stmt 4144251883Speter** 4145322444Speter** After a [prepared statement] has been prepared using any of 4146322444Speter** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()], 4147322444Speter** or [sqlite3_prepare16_v3()] or one of the legacy 4148251883Speter** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function 4149251883Speter** must be called one or more times to evaluate the statement. 4150251883Speter** 4151251883Speter** The details of the behavior of the sqlite3_step() interface depend 4152322444Speter** on whether the statement was prepared using the newer "vX" interfaces 4153322444Speter** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()], 4154322444Speter** [sqlite3_prepare16_v2()] or the older legacy 4155322444Speter** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the 4156322444Speter** new "vX" interface is recommended for new applications but the legacy 4157251883Speter** interface will continue to be supported. 4158251883Speter** 4159251883Speter** ^In the legacy interface, the return value will be either [SQLITE_BUSY], 4160251883Speter** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. 4161251883Speter** ^With the "v2" interface, any of the other [result codes] or 4162251883Speter** [extended result codes] might be returned as well. 4163251883Speter** 4164251883Speter** ^[SQLITE_BUSY] means that the database engine was unable to acquire the 4165251883Speter** database locks it needs to do its job. ^If the statement is a [COMMIT] 4166251883Speter** or occurs outside of an explicit transaction, then you can retry the 4167251883Speter** statement. If the statement is not a [COMMIT] and occurs within an 4168251883Speter** explicit transaction then you should rollback the transaction before 4169251883Speter** continuing. 4170251883Speter** 4171251883Speter** ^[SQLITE_DONE] means that the statement has finished executing 4172251883Speter** successfully. sqlite3_step() should not be called again on this virtual 4173251883Speter** machine without first calling [sqlite3_reset()] to reset the virtual 4174251883Speter** machine back to its initial state. 4175251883Speter** 4176251883Speter** ^If the SQL statement being executed returns any data, then [SQLITE_ROW] 4177251883Speter** is returned each time a new row of data is ready for processing by the 4178251883Speter** caller. The values may be accessed using the [column access functions]. 4179251883Speter** sqlite3_step() is called again to retrieve the next row of data. 4180251883Speter** 4181251883Speter** ^[SQLITE_ERROR] means that a run-time error (such as a constraint 4182251883Speter** violation) has occurred. sqlite3_step() should not be called again on 4183251883Speter** the VM. More information may be found by calling [sqlite3_errmsg()]. 4184251883Speter** ^With the legacy interface, a more specific error code (for example, 4185251883Speter** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) 4186251883Speter** can be obtained by calling [sqlite3_reset()] on the 4187251883Speter** [prepared statement]. ^In the "v2" interface, 4188251883Speter** the more specific error code is returned directly by sqlite3_step(). 4189251883Speter** 4190251883Speter** [SQLITE_MISUSE] means that the this routine was called inappropriately. 4191251883Speter** Perhaps it was called on a [prepared statement] that has 4192251883Speter** already been [sqlite3_finalize | finalized] or on one that had 4193251883Speter** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could 4194251883Speter** be the case that the same database connection is being used by two or 4195251883Speter** more threads at the same moment in time. 4196251883Speter** 4197251883Speter** For all versions of SQLite up to and including 3.6.23.1, a call to 4198251883Speter** [sqlite3_reset()] was required after sqlite3_step() returned anything 4199251883Speter** other than [SQLITE_ROW] before any subsequent invocation of 4200251883Speter** sqlite3_step(). Failure to reset the prepared statement using 4201251883Speter** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from 4202322444Speter** sqlite3_step(). But after [version 3.6.23.1] ([dateof:3.6.23.1]), 4203322444Speter** sqlite3_step() began 4204251883Speter** calling [sqlite3_reset()] automatically in this circumstance rather 4205251883Speter** than returning [SQLITE_MISUSE]. This is not considered a compatibility 4206251883Speter** break because any application that ever receives an SQLITE_MISUSE error 4207251883Speter** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option 4208251883Speter** can be used to restore the legacy behavior. 4209251883Speter** 4210251883Speter** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step() 4211251883Speter** API always returns a generic error code, [SQLITE_ERROR], following any 4212251883Speter** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call 4213251883Speter** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the 4214251883Speter** specific [error codes] that better describes the error. 4215251883Speter** We admit that this is a goofy design. The problem has been fixed 4216251883Speter** with the "v2" interface. If you prepare all of your SQL statements 4217322444Speter** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()] 4218322444Speter** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead 4219251883Speter** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces, 4220251883Speter** then the more specific [error codes] are returned directly 4221322444Speter** by sqlite3_step(). The use of the "vX" interfaces is recommended. 4222251883Speter*/ 4223322444SpeterSQLITE_API int sqlite3_step(sqlite3_stmt*); 4224251883Speter 4225251883Speter/* 4226251883Speter** CAPI3REF: Number of columns in a result set 4227286510Speter** METHOD: sqlite3_stmt 4228251883Speter** 4229251883Speter** ^The sqlite3_data_count(P) interface returns the number of columns in the 4230251883Speter** current row of the result set of [prepared statement] P. 4231251883Speter** ^If prepared statement P does not have results ready to return 4232251883Speter** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of 4233251883Speter** interfaces) then sqlite3_data_count(P) returns 0. 4234251883Speter** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer. 4235251883Speter** ^The sqlite3_data_count(P) routine returns 0 if the previous call to 4236251883Speter** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P) 4237251883Speter** will return non-zero if previous call to [sqlite3_step](P) returned 4238251883Speter** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum] 4239251883Speter** where it always returns zero since each step of that multi-step 4240251883Speter** pragma returns 0 columns of data. 4241251883Speter** 4242251883Speter** See also: [sqlite3_column_count()] 4243251883Speter*/ 4244322444SpeterSQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); 4245251883Speter 4246251883Speter/* 4247251883Speter** CAPI3REF: Fundamental Datatypes 4248251883Speter** KEYWORDS: SQLITE_TEXT 4249251883Speter** 4250251883Speter** ^(Every value in SQLite has one of five fundamental datatypes: 4251251883Speter** 4252251883Speter** <ul> 4253251883Speter** <li> 64-bit signed integer 4254251883Speter** <li> 64-bit IEEE floating point number 4255251883Speter** <li> string 4256251883Speter** <li> BLOB 4257251883Speter** <li> NULL 4258251883Speter** </ul>)^ 4259251883Speter** 4260251883Speter** These constants are codes for each of those types. 4261251883Speter** 4262251883Speter** Note that the SQLITE_TEXT constant was also used in SQLite version 2 4263251883Speter** for a completely different meaning. Software that links against both 4264251883Speter** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not 4265251883Speter** SQLITE_TEXT. 4266251883Speter*/ 4267251883Speter#define SQLITE_INTEGER 1 4268251883Speter#define SQLITE_FLOAT 2 4269251883Speter#define SQLITE_BLOB 4 4270251883Speter#define SQLITE_NULL 5 4271251883Speter#ifdef SQLITE_TEXT 4272251883Speter# undef SQLITE_TEXT 4273251883Speter#else 4274251883Speter# define SQLITE_TEXT 3 4275251883Speter#endif 4276251883Speter#define SQLITE3_TEXT 3 4277251883Speter 4278251883Speter/* 4279251883Speter** CAPI3REF: Result Values From A Query 4280251883Speter** KEYWORDS: {column access functions} 4281286510Speter** METHOD: sqlite3_stmt 4282251883Speter** 4283322444Speter** <b>Summary:</b> 4284322444Speter** <blockquote><table border=0 cellpadding=0 cellspacing=0> 4285322444Speter** <tr><td><b>sqlite3_column_blob</b><td>→<td>BLOB result 4286322444Speter** <tr><td><b>sqlite3_column_double</b><td>→<td>REAL result 4287322444Speter** <tr><td><b>sqlite3_column_int</b><td>→<td>32-bit INTEGER result 4288322444Speter** <tr><td><b>sqlite3_column_int64</b><td>→<td>64-bit INTEGER result 4289322444Speter** <tr><td><b>sqlite3_column_text</b><td>→<td>UTF-8 TEXT result 4290322444Speter** <tr><td><b>sqlite3_column_text16</b><td>→<td>UTF-16 TEXT result 4291322444Speter** <tr><td><b>sqlite3_column_value</b><td>→<td>The result as an 4292322444Speter** [sqlite3_value|unprotected sqlite3_value] object. 4293322444Speter** <tr><td> <td> <td> 4294322444Speter** <tr><td><b>sqlite3_column_bytes</b><td>→<td>Size of a BLOB 4295322444Speter** or a UTF-8 TEXT result in bytes 4296322444Speter** <tr><td><b>sqlite3_column_bytes16 </b> 4297322444Speter** <td>→ <td>Size of UTF-16 4298322444Speter** TEXT in bytes 4299322444Speter** <tr><td><b>sqlite3_column_type</b><td>→<td>Default 4300322444Speter** datatype of the result 4301322444Speter** </table></blockquote> 4302322444Speter** 4303322444Speter** <b>Details:</b> 4304322444Speter** 4305251883Speter** ^These routines return information about a single column of the current 4306251883Speter** result row of a query. ^In every case the first argument is a pointer 4307251883Speter** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*] 4308251883Speter** that was returned from [sqlite3_prepare_v2()] or one of its variants) 4309251883Speter** and the second argument is the index of the column for which information 4310251883Speter** should be returned. ^The leftmost column of the result set has the index 0. 4311251883Speter** ^The number of columns in the result can be determined using 4312251883Speter** [sqlite3_column_count()]. 4313251883Speter** 4314251883Speter** If the SQL statement does not currently point to a valid row, or if the 4315251883Speter** column index is out of range, the result is undefined. 4316251883Speter** These routines may only be called when the most recent call to 4317251883Speter** [sqlite3_step()] has returned [SQLITE_ROW] and neither 4318251883Speter** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently. 4319251883Speter** If any of these routines are called after [sqlite3_reset()] or 4320251883Speter** [sqlite3_finalize()] or after [sqlite3_step()] has returned 4321251883Speter** something other than [SQLITE_ROW], the results are undefined. 4322251883Speter** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()] 4323251883Speter** are called from a different thread while any of these routines 4324251883Speter** are pending, then the results are undefined. 4325251883Speter** 4326322444Speter** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16) 4327322444Speter** each return the value of a result column in a specific data format. If 4328322444Speter** the result column is not initially in the requested format (for example, 4329322444Speter** if the query returns an integer but the sqlite3_column_text() interface 4330322444Speter** is used to extract the value) then an automatic type conversion is performed. 4331322444Speter** 4332251883Speter** ^The sqlite3_column_type() routine returns the 4333251883Speter** [SQLITE_INTEGER | datatype code] for the initial data type 4334251883Speter** of the result column. ^The returned value is one of [SQLITE_INTEGER], 4335322444Speter** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. 4336322444Speter** The return value of sqlite3_column_type() can be used to decide which 4337322444Speter** of the first six interface should be used to extract the column value. 4338322444Speter** The value returned by sqlite3_column_type() is only meaningful if no 4339322444Speter** automatic type conversions have occurred for the value in question. 4340322444Speter** After a type conversion, the result of calling sqlite3_column_type() 4341322444Speter** is undefined, though harmless. Future 4342251883Speter** versions of SQLite may change the behavior of sqlite3_column_type() 4343251883Speter** following a type conversion. 4344251883Speter** 4345322444Speter** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes() 4346322444Speter** or sqlite3_column_bytes16() interfaces can be used to determine the size 4347322444Speter** of that BLOB or string. 4348322444Speter** 4349251883Speter** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() 4350251883Speter** routine returns the number of bytes in that BLOB or string. 4351251883Speter** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts 4352251883Speter** the string to UTF-8 and then returns the number of bytes. 4353251883Speter** ^If the result is a numeric value then sqlite3_column_bytes() uses 4354251883Speter** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns 4355251883Speter** the number of bytes in that string. 4356251883Speter** ^If the result is NULL, then sqlite3_column_bytes() returns zero. 4357251883Speter** 4358251883Speter** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16() 4359251883Speter** routine returns the number of bytes in that BLOB or string. 4360251883Speter** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts 4361251883Speter** the string to UTF-16 and then returns the number of bytes. 4362251883Speter** ^If the result is a numeric value then sqlite3_column_bytes16() uses 4363251883Speter** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns 4364251883Speter** the number of bytes in that string. 4365251883Speter** ^If the result is NULL, then sqlite3_column_bytes16() returns zero. 4366251883Speter** 4367251883Speter** ^The values returned by [sqlite3_column_bytes()] and 4368251883Speter** [sqlite3_column_bytes16()] do not include the zero terminators at the end 4369251883Speter** of the string. ^For clarity: the values returned by 4370251883Speter** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of 4371251883Speter** bytes in the string, not the number of characters. 4372251883Speter** 4373251883Speter** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), 4374251883Speter** even empty strings, are always zero-terminated. ^The return 4375251883Speter** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. 4376251883Speter** 4377286510Speter** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an 4378286510Speter** [unprotected sqlite3_value] object. In a multithreaded environment, 4379286510Speter** an unprotected sqlite3_value object may only be used safely with 4380286510Speter** [sqlite3_bind_value()] and [sqlite3_result_value()]. 4381251883Speter** If the [unprotected sqlite3_value] object returned by 4382251883Speter** [sqlite3_column_value()] is used in any other way, including calls 4383251883Speter** to routines like [sqlite3_value_int()], [sqlite3_value_text()], 4384286510Speter** or [sqlite3_value_bytes()], the behavior is not threadsafe. 4385322444Speter** Hence, the sqlite3_column_value() interface 4386322444Speter** is normally only useful within the implementation of 4387322444Speter** [application-defined SQL functions] or [virtual tables], not within 4388322444Speter** top-level application code. 4389251883Speter** 4390322444Speter** The these routines may attempt to convert the datatype of the result. 4391322444Speter** ^For example, if the internal representation is FLOAT and a text result 4392251883Speter** is requested, [sqlite3_snprintf()] is used internally to perform the 4393251883Speter** conversion automatically. ^(The following table details the conversions 4394251883Speter** that are applied: 4395251883Speter** 4396251883Speter** <blockquote> 4397251883Speter** <table border="1"> 4398251883Speter** <tr><th> Internal<br>Type <th> Requested<br>Type <th> Conversion 4399251883Speter** 4400251883Speter** <tr><td> NULL <td> INTEGER <td> Result is 0 4401251883Speter** <tr><td> NULL <td> FLOAT <td> Result is 0.0 4402269851Speter** <tr><td> NULL <td> TEXT <td> Result is a NULL pointer 4403269851Speter** <tr><td> NULL <td> BLOB <td> Result is a NULL pointer 4404251883Speter** <tr><td> INTEGER <td> FLOAT <td> Convert from integer to float 4405251883Speter** <tr><td> INTEGER <td> TEXT <td> ASCII rendering of the integer 4406251883Speter** <tr><td> INTEGER <td> BLOB <td> Same as INTEGER->TEXT 4407269851Speter** <tr><td> FLOAT <td> INTEGER <td> [CAST] to INTEGER 4408251883Speter** <tr><td> FLOAT <td> TEXT <td> ASCII rendering of the float 4409269851Speter** <tr><td> FLOAT <td> BLOB <td> [CAST] to BLOB 4410269851Speter** <tr><td> TEXT <td> INTEGER <td> [CAST] to INTEGER 4411269851Speter** <tr><td> TEXT <td> FLOAT <td> [CAST] to REAL 4412251883Speter** <tr><td> TEXT <td> BLOB <td> No change 4413269851Speter** <tr><td> BLOB <td> INTEGER <td> [CAST] to INTEGER 4414269851Speter** <tr><td> BLOB <td> FLOAT <td> [CAST] to REAL 4415251883Speter** <tr><td> BLOB <td> TEXT <td> Add a zero terminator if needed 4416251883Speter** </table> 4417251883Speter** </blockquote>)^ 4418251883Speter** 4419251883Speter** Note that when type conversions occur, pointers returned by prior 4420251883Speter** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or 4421251883Speter** sqlite3_column_text16() may be invalidated. 4422251883Speter** Type conversions and pointer invalidations might occur 4423251883Speter** in the following cases: 4424251883Speter** 4425251883Speter** <ul> 4426251883Speter** <li> The initial content is a BLOB and sqlite3_column_text() or 4427251883Speter** sqlite3_column_text16() is called. A zero-terminator might 4428251883Speter** need to be added to the string.</li> 4429251883Speter** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or 4430251883Speter** sqlite3_column_text16() is called. The content must be converted 4431251883Speter** to UTF-16.</li> 4432251883Speter** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or 4433251883Speter** sqlite3_column_text() is called. The content must be converted 4434251883Speter** to UTF-8.</li> 4435251883Speter** </ul> 4436251883Speter** 4437251883Speter** ^Conversions between UTF-16be and UTF-16le are always done in place and do 4438251883Speter** not invalidate a prior pointer, though of course the content of the buffer 4439251883Speter** that the prior pointer references will have been modified. Other kinds 4440251883Speter** of conversion are done in place when it is possible, but sometimes they 4441251883Speter** are not possible and in those cases prior pointers are invalidated. 4442251883Speter** 4443286510Speter** The safest policy is to invoke these routines 4444251883Speter** in one of the following ways: 4445251883Speter** 4446251883Speter** <ul> 4447251883Speter** <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li> 4448251883Speter** <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li> 4449251883Speter** <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li> 4450251883Speter** </ul> 4451251883Speter** 4452251883Speter** In other words, you should call sqlite3_column_text(), 4453251883Speter** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result 4454251883Speter** into the desired format, then invoke sqlite3_column_bytes() or 4455251883Speter** sqlite3_column_bytes16() to find the size of the result. Do not mix calls 4456251883Speter** to sqlite3_column_text() or sqlite3_column_blob() with calls to 4457251883Speter** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16() 4458251883Speter** with calls to sqlite3_column_bytes(). 4459251883Speter** 4460251883Speter** ^The pointers returned are valid until a type conversion occurs as 4461251883Speter** described above, or until [sqlite3_step()] or [sqlite3_reset()] or 4462251883Speter** [sqlite3_finalize()] is called. ^The memory space used to hold strings 4463322444Speter** and BLOBs is freed automatically. Do not pass the pointers returned 4464269851Speter** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into 4465251883Speter** [sqlite3_free()]. 4466251883Speter** 4467251883Speter** ^(If a memory allocation error occurs during the evaluation of any 4468251883Speter** of these routines, a default value is returned. The default value 4469251883Speter** is either the integer 0, the floating point number 0.0, or a NULL 4470251883Speter** pointer. Subsequent calls to [sqlite3_errcode()] will return 4471251883Speter** [SQLITE_NOMEM].)^ 4472251883Speter*/ 4473322444SpeterSQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); 4474322444SpeterSQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol); 4475322444SpeterSQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol); 4476322444SpeterSQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); 4477322444SpeterSQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); 4478322444SpeterSQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); 4479322444SpeterSQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); 4480322444SpeterSQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol); 4481322444SpeterSQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); 4482322444SpeterSQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol); 4483251883Speter 4484251883Speter/* 4485251883Speter** CAPI3REF: Destroy A Prepared Statement Object 4486286510Speter** DESTRUCTOR: sqlite3_stmt 4487251883Speter** 4488251883Speter** ^The sqlite3_finalize() function is called to delete a [prepared statement]. 4489251883Speter** ^If the most recent evaluation of the statement encountered no errors 4490251883Speter** or if the statement is never been evaluated, then sqlite3_finalize() returns 4491251883Speter** SQLITE_OK. ^If the most recent evaluation of statement S failed, then 4492251883Speter** sqlite3_finalize(S) returns the appropriate [error code] or 4493251883Speter** [extended error code]. 4494251883Speter** 4495251883Speter** ^The sqlite3_finalize(S) routine can be called at any point during 4496251883Speter** the life cycle of [prepared statement] S: 4497251883Speter** before statement S is ever evaluated, after 4498251883Speter** one or more calls to [sqlite3_reset()], or after any call 4499251883Speter** to [sqlite3_step()] regardless of whether or not the statement has 4500251883Speter** completed execution. 4501251883Speter** 4502251883Speter** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op. 4503251883Speter** 4504251883Speter** The application must finalize every [prepared statement] in order to avoid 4505251883Speter** resource leaks. It is a grievous error for the application to try to use 4506251883Speter** a prepared statement after it has been finalized. Any use of a prepared 4507251883Speter** statement after it has been finalized can result in undefined and 4508251883Speter** undesirable behavior such as segfaults and heap corruption. 4509251883Speter*/ 4510322444SpeterSQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt); 4511251883Speter 4512251883Speter/* 4513251883Speter** CAPI3REF: Reset A Prepared Statement Object 4514286510Speter** METHOD: sqlite3_stmt 4515251883Speter** 4516251883Speter** The sqlite3_reset() function is called to reset a [prepared statement] 4517251883Speter** object back to its initial state, ready to be re-executed. 4518251883Speter** ^Any SQL statement variables that had values bound to them using 4519251883Speter** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. 4520251883Speter** Use [sqlite3_clear_bindings()] to reset the bindings. 4521251883Speter** 4522251883Speter** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S 4523251883Speter** back to the beginning of its program. 4524251883Speter** 4525251883Speter** ^If the most recent call to [sqlite3_step(S)] for the 4526251883Speter** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE], 4527251883Speter** or if [sqlite3_step(S)] has never before been called on S, 4528251883Speter** then [sqlite3_reset(S)] returns [SQLITE_OK]. 4529251883Speter** 4530251883Speter** ^If the most recent call to [sqlite3_step(S)] for the 4531251883Speter** [prepared statement] S indicated an error, then 4532251883Speter** [sqlite3_reset(S)] returns an appropriate [error code]. 4533251883Speter** 4534251883Speter** ^The [sqlite3_reset(S)] interface does not change the values 4535251883Speter** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S. 4536251883Speter*/ 4537322444SpeterSQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt); 4538251883Speter 4539251883Speter/* 4540251883Speter** CAPI3REF: Create Or Redefine SQL Functions 4541251883Speter** KEYWORDS: {function creation routines} 4542251883Speter** KEYWORDS: {application-defined SQL function} 4543251883Speter** KEYWORDS: {application-defined SQL functions} 4544286510Speter** METHOD: sqlite3 4545251883Speter** 4546251883Speter** ^These functions (collectively known as "function creation routines") 4547251883Speter** are used to add SQL functions or aggregates or to redefine the behavior 4548251883Speter** of existing SQL functions or aggregates. The only differences between 4549251883Speter** these routines are the text encoding expected for 4550251883Speter** the second parameter (the name of the function being created) 4551251883Speter** and the presence or absence of a destructor callback for 4552251883Speter** the application data pointer. 4553251883Speter** 4554251883Speter** ^The first parameter is the [database connection] to which the SQL 4555251883Speter** function is to be added. ^If an application uses more than one database 4556251883Speter** connection then application-defined SQL functions must be added 4557251883Speter** to each database connection separately. 4558251883Speter** 4559251883Speter** ^The second parameter is the name of the SQL function to be created or 4560251883Speter** redefined. ^The length of the name is limited to 255 bytes in a UTF-8 4561251883Speter** representation, exclusive of the zero-terminator. ^Note that the name 4562251883Speter** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes. 4563251883Speter** ^Any attempt to create a function with a longer name 4564251883Speter** will result in [SQLITE_MISUSE] being returned. 4565251883Speter** 4566251883Speter** ^The third parameter (nArg) 4567251883Speter** is the number of arguments that the SQL function or 4568251883Speter** aggregate takes. ^If this parameter is -1, then the SQL function or 4569251883Speter** aggregate may take any number of arguments between 0 and the limit 4570251883Speter** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third 4571251883Speter** parameter is less than -1 or greater than 127 then the behavior is 4572251883Speter** undefined. 4573251883Speter** 4574251883Speter** ^The fourth parameter, eTextRep, specifies what 4575251883Speter** [SQLITE_UTF8 | text encoding] this SQL function prefers for 4576269851Speter** its parameters. The application should set this parameter to 4577269851Speter** [SQLITE_UTF16LE] if the function implementation invokes 4578269851Speter** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the 4579269851Speter** implementation invokes [sqlite3_value_text16be()] on an input, or 4580269851Speter** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8] 4581269851Speter** otherwise. ^The same SQL function may be registered multiple times using 4582269851Speter** different preferred text encodings, with different implementations for 4583269851Speter** each encoding. 4584251883Speter** ^When multiple implementations of the same function are available, SQLite 4585251883Speter** will pick the one that involves the least amount of data conversion. 4586251883Speter** 4587269851Speter** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC] 4588269851Speter** to signal that the function will always return the same result given 4589269851Speter** the same inputs within a single SQL statement. Most SQL functions are 4590269851Speter** deterministic. The built-in [random()] SQL function is an example of a 4591269851Speter** function that is not deterministic. The SQLite query planner is able to 4592269851Speter** perform additional optimizations on deterministic functions, so use 4593269851Speter** of the [SQLITE_DETERMINISTIC] flag is recommended where possible. 4594269851Speter** 4595251883Speter** ^(The fifth parameter is an arbitrary pointer. The implementation of the 4596251883Speter** function can gain access to this pointer using [sqlite3_user_data()].)^ 4597251883Speter** 4598251883Speter** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are 4599251883Speter** pointers to C-language functions that implement the SQL function or 4600251883Speter** aggregate. ^A scalar SQL function requires an implementation of the xFunc 4601251883Speter** callback only; NULL pointers must be passed as the xStep and xFinal 4602251883Speter** parameters. ^An aggregate SQL function requires an implementation of xStep 4603251883Speter** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing 4604251883Speter** SQL function or aggregate, pass NULL pointers for all three function 4605251883Speter** callbacks. 4606251883Speter** 4607251883Speter** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL, 4608251883Speter** then it is destructor for the application data pointer. 4609251883Speter** The destructor is invoked when the function is deleted, either by being 4610251883Speter** overloaded or when the database connection closes.)^ 4611251883Speter** ^The destructor is also invoked if the call to 4612251883Speter** sqlite3_create_function_v2() fails. 4613251883Speter** ^When the destructor callback of the tenth parameter is invoked, it 4614251883Speter** is passed a single argument which is a copy of the application data 4615251883Speter** pointer which was the fifth parameter to sqlite3_create_function_v2(). 4616251883Speter** 4617251883Speter** ^It is permitted to register multiple implementations of the same 4618251883Speter** functions with the same name but with either differing numbers of 4619251883Speter** arguments or differing preferred text encodings. ^SQLite will use 4620251883Speter** the implementation that most closely matches the way in which the 4621251883Speter** SQL function is used. ^A function implementation with a non-negative 4622251883Speter** nArg parameter is a better match than a function implementation with 4623251883Speter** a negative nArg. ^A function where the preferred text encoding 4624251883Speter** matches the database encoding is a better 4625251883Speter** match than a function where the encoding is different. 4626251883Speter** ^A function where the encoding difference is between UTF16le and UTF16be 4627251883Speter** is a closer match than a function where the encoding difference is 4628251883Speter** between UTF8 and UTF16. 4629251883Speter** 4630251883Speter** ^Built-in functions may be overloaded by new application-defined functions. 4631251883Speter** 4632251883Speter** ^An application-defined function is permitted to call other 4633251883Speter** SQLite interfaces. However, such calls must not 4634251883Speter** close the database connection nor finalize or reset the prepared 4635251883Speter** statement in which the function is running. 4636251883Speter*/ 4637322444SpeterSQLITE_API int sqlite3_create_function( 4638251883Speter sqlite3 *db, 4639251883Speter const char *zFunctionName, 4640251883Speter int nArg, 4641251883Speter int eTextRep, 4642251883Speter void *pApp, 4643251883Speter void (*xFunc)(sqlite3_context*,int,sqlite3_value**), 4644251883Speter void (*xStep)(sqlite3_context*,int,sqlite3_value**), 4645251883Speter void (*xFinal)(sqlite3_context*) 4646251883Speter); 4647322444SpeterSQLITE_API int sqlite3_create_function16( 4648251883Speter sqlite3 *db, 4649251883Speter const void *zFunctionName, 4650251883Speter int nArg, 4651251883Speter int eTextRep, 4652251883Speter void *pApp, 4653251883Speter void (*xFunc)(sqlite3_context*,int,sqlite3_value**), 4654251883Speter void (*xStep)(sqlite3_context*,int,sqlite3_value**), 4655251883Speter void (*xFinal)(sqlite3_context*) 4656251883Speter); 4657322444SpeterSQLITE_API int sqlite3_create_function_v2( 4658251883Speter sqlite3 *db, 4659251883Speter const char *zFunctionName, 4660251883Speter int nArg, 4661251883Speter int eTextRep, 4662251883Speter void *pApp, 4663251883Speter void (*xFunc)(sqlite3_context*,int,sqlite3_value**), 4664251883Speter void (*xStep)(sqlite3_context*,int,sqlite3_value**), 4665251883Speter void (*xFinal)(sqlite3_context*), 4666251883Speter void(*xDestroy)(void*) 4667251883Speter); 4668251883Speter 4669251883Speter/* 4670251883Speter** CAPI3REF: Text Encodings 4671251883Speter** 4672251883Speter** These constant define integer codes that represent the various 4673251883Speter** text encodings supported by SQLite. 4674251883Speter*/ 4675282328Sbapt#define SQLITE_UTF8 1 /* IMP: R-37514-35566 */ 4676282328Sbapt#define SQLITE_UTF16LE 2 /* IMP: R-03371-37637 */ 4677282328Sbapt#define SQLITE_UTF16BE 3 /* IMP: R-51971-34154 */ 4678251883Speter#define SQLITE_UTF16 4 /* Use native byte order */ 4679269851Speter#define SQLITE_ANY 5 /* Deprecated */ 4680251883Speter#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ 4681251883Speter 4682251883Speter/* 4683269851Speter** CAPI3REF: Function Flags 4684269851Speter** 4685269851Speter** These constants may be ORed together with the 4686269851Speter** [SQLITE_UTF8 | preferred text encoding] as the fourth argument 4687269851Speter** to [sqlite3_create_function()], [sqlite3_create_function16()], or 4688269851Speter** [sqlite3_create_function_v2()]. 4689269851Speter*/ 4690269851Speter#define SQLITE_DETERMINISTIC 0x800 4691269851Speter 4692269851Speter/* 4693251883Speter** CAPI3REF: Deprecated Functions 4694251883Speter** DEPRECATED 4695251883Speter** 4696251883Speter** These functions are [deprecated]. In order to maintain 4697251883Speter** backwards compatibility with older code, these functions continue 4698251883Speter** to be supported. However, new applications should avoid 4699282328Sbapt** the use of these functions. To encourage programmers to avoid 4700282328Sbapt** these functions, we will not explain what they do. 4701251883Speter*/ 4702251883Speter#ifndef SQLITE_OMIT_DEPRECATED 4703322444SpeterSQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*); 4704322444SpeterSQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*); 4705322444SpeterSQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); 4706322444SpeterSQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void); 4707322444SpeterSQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void); 4708322444SpeterSQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int), 4709251883Speter void*,sqlite3_int64); 4710251883Speter#endif 4711251883Speter 4712251883Speter/* 4713286510Speter** CAPI3REF: Obtaining SQL Values 4714286510Speter** METHOD: sqlite3_value 4715251883Speter** 4716322444Speter** <b>Summary:</b> 4717322444Speter** <blockquote><table border=0 cellpadding=0 cellspacing=0> 4718322444Speter** <tr><td><b>sqlite3_value_blob</b><td>→<td>BLOB value 4719322444Speter** <tr><td><b>sqlite3_value_double</b><td>→<td>REAL value 4720322444Speter** <tr><td><b>sqlite3_value_int</b><td>→<td>32-bit INTEGER value 4721322444Speter** <tr><td><b>sqlite3_value_int64</b><td>→<td>64-bit INTEGER value 4722322444Speter** <tr><td><b>sqlite3_value_pointer</b><td>→<td>Pointer value 4723322444Speter** <tr><td><b>sqlite3_value_text</b><td>→<td>UTF-8 TEXT value 4724322444Speter** <tr><td><b>sqlite3_value_text16</b><td>→<td>UTF-16 TEXT value in 4725322444Speter** the native byteorder 4726322444Speter** <tr><td><b>sqlite3_value_text16be</b><td>→<td>UTF-16be TEXT value 4727322444Speter** <tr><td><b>sqlite3_value_text16le</b><td>→<td>UTF-16le TEXT value 4728322444Speter** <tr><td> <td> <td> 4729322444Speter** <tr><td><b>sqlite3_value_bytes</b><td>→<td>Size of a BLOB 4730322444Speter** or a UTF-8 TEXT in bytes 4731322444Speter** <tr><td><b>sqlite3_value_bytes16 </b> 4732322444Speter** <td>→ <td>Size of UTF-16 4733322444Speter** TEXT in bytes 4734322444Speter** <tr><td><b>sqlite3_value_type</b><td>→<td>Default 4735322444Speter** datatype of the value 4736322444Speter** <tr><td><b>sqlite3_value_numeric_type </b> 4737322444Speter** <td>→ <td>Best numeric datatype of the value 4738322444Speter** </table></blockquote> 4739251883Speter** 4740322444Speter** <b>Details:</b> 4741251883Speter** 4742322444Speter** These routines extract type, size, and content information from 4743322444Speter** [protected sqlite3_value] objects. Protected sqlite3_value objects 4744322444Speter** are used to pass parameter information into implementation of 4745322444Speter** [application-defined SQL functions] and [virtual tables]. 4746322444Speter** 4747251883Speter** These routines work only with [protected sqlite3_value] objects. 4748251883Speter** Any attempt to use these routines on an [unprotected sqlite3_value] 4749322444Speter** is not threadsafe. 4750251883Speter** 4751251883Speter** ^These routines work just like the corresponding [column access functions] 4752274884Sbapt** except that these routines take a single [protected sqlite3_value] object 4753251883Speter** pointer instead of a [sqlite3_stmt*] pointer and an integer column number. 4754251883Speter** 4755251883Speter** ^The sqlite3_value_text16() interface extracts a UTF-16 string 4756251883Speter** in the native byte-order of the host machine. ^The 4757251883Speter** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces 4758251883Speter** extract UTF-16 strings as big-endian and little-endian respectively. 4759251883Speter** 4760322444Speter** ^If [sqlite3_value] object V was initialized 4761322444Speter** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)] 4762322444Speter** and if X and Y are strings that compare equal according to strcmp(X,Y), 4763322444Speter** then sqlite3_value_pointer(V,Y) will return the pointer P. ^Otherwise, 4764322444Speter** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer() 4765322444Speter** routine is part of the [pointer passing interface] added for SQLite 3.20.0. 4766322444Speter** 4767322444Speter** ^(The sqlite3_value_type(V) interface returns the 4768322444Speter** [SQLITE_INTEGER | datatype code] for the initial datatype of the 4769322444Speter** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER], 4770322444Speter** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^ 4771322444Speter** Other interfaces might change the datatype for an sqlite3_value object. 4772322444Speter** For example, if the datatype is initially SQLITE_INTEGER and 4773322444Speter** sqlite3_value_text(V) is called to extract a text value for that 4774322444Speter** integer, then subsequent calls to sqlite3_value_type(V) might return 4775322444Speter** SQLITE_TEXT. Whether or not a persistent internal datatype conversion 4776322444Speter** occurs is undefined and may change from one release of SQLite to the next. 4777322444Speter** 4778251883Speter** ^(The sqlite3_value_numeric_type() interface attempts to apply 4779251883Speter** numeric affinity to the value. This means that an attempt is 4780251883Speter** made to convert the value to an integer or floating point. If 4781251883Speter** such a conversion is possible without loss of information (in other 4782251883Speter** words, if the value is a string that looks like a number) 4783251883Speter** then the conversion is performed. Otherwise no conversion occurs. 4784251883Speter** The [SQLITE_INTEGER | datatype] after conversion is returned.)^ 4785251883Speter** 4786251883Speter** Please pay particular attention to the fact that the pointer returned 4787251883Speter** from [sqlite3_value_blob()], [sqlite3_value_text()], or 4788251883Speter** [sqlite3_value_text16()] can be invalidated by a subsequent call to 4789251883Speter** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], 4790251883Speter** or [sqlite3_value_text16()]. 4791251883Speter** 4792251883Speter** These routines must be called from the same thread as 4793251883Speter** the SQL function that supplied the [sqlite3_value*] parameters. 4794251883Speter*/ 4795322444SpeterSQLITE_API const void *sqlite3_value_blob(sqlite3_value*); 4796322444SpeterSQLITE_API double sqlite3_value_double(sqlite3_value*); 4797322444SpeterSQLITE_API int sqlite3_value_int(sqlite3_value*); 4798322444SpeterSQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*); 4799322444SpeterSQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*); 4800322444SpeterSQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*); 4801322444SpeterSQLITE_API const void *sqlite3_value_text16(sqlite3_value*); 4802322444SpeterSQLITE_API const void *sqlite3_value_text16le(sqlite3_value*); 4803322444SpeterSQLITE_API const void *sqlite3_value_text16be(sqlite3_value*); 4804322444SpeterSQLITE_API int sqlite3_value_bytes(sqlite3_value*); 4805322444SpeterSQLITE_API int sqlite3_value_bytes16(sqlite3_value*); 4806322444SpeterSQLITE_API int sqlite3_value_type(sqlite3_value*); 4807322444SpeterSQLITE_API int sqlite3_value_numeric_type(sqlite3_value*); 4808251883Speter 4809251883Speter/* 4810298161Sbapt** CAPI3REF: Finding The Subtype Of SQL Values 4811298161Sbapt** METHOD: sqlite3_value 4812298161Sbapt** 4813298161Sbapt** The sqlite3_value_subtype(V) function returns the subtype for 4814298161Sbapt** an [application-defined SQL function] argument V. The subtype 4815298161Sbapt** information can be used to pass a limited amount of context from 4816298161Sbapt** one SQL function to another. Use the [sqlite3_result_subtype()] 4817298161Sbapt** routine to set the subtype for the return value of an SQL function. 4818298161Sbapt*/ 4819322444SpeterSQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*); 4820298161Sbapt 4821298161Sbapt/* 4822286510Speter** CAPI3REF: Copy And Free SQL Values 4823286510Speter** METHOD: sqlite3_value 4824286510Speter** 4825286510Speter** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value] 4826286510Speter** object D and returns a pointer to that copy. ^The [sqlite3_value] returned 4827286510Speter** is a [protected sqlite3_value] object even if the input is not. 4828286510Speter** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a 4829286510Speter** memory allocation fails. 4830286510Speter** 4831286510Speter** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object 4832286510Speter** previously obtained from [sqlite3_value_dup()]. ^If V is a NULL pointer 4833286510Speter** then sqlite3_value_free(V) is a harmless no-op. 4834286510Speter*/ 4835322444SpeterSQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*); 4836322444SpeterSQLITE_API void sqlite3_value_free(sqlite3_value*); 4837286510Speter 4838286510Speter/* 4839251883Speter** CAPI3REF: Obtain Aggregate Function Context 4840286510Speter** METHOD: sqlite3_context 4841251883Speter** 4842251883Speter** Implementations of aggregate SQL functions use this 4843251883Speter** routine to allocate memory for storing their state. 4844251883Speter** 4845251883Speter** ^The first time the sqlite3_aggregate_context(C,N) routine is called 4846251883Speter** for a particular aggregate function, SQLite 4847251883Speter** allocates N of memory, zeroes out that memory, and returns a pointer 4848251883Speter** to the new memory. ^On second and subsequent calls to 4849251883Speter** sqlite3_aggregate_context() for the same aggregate function instance, 4850251883Speter** the same buffer is returned. Sqlite3_aggregate_context() is normally 4851251883Speter** called once for each invocation of the xStep callback and then one 4852251883Speter** last time when the xFinal callback is invoked. ^(When no rows match 4853251883Speter** an aggregate query, the xStep() callback of the aggregate function 4854251883Speter** implementation is never called and xFinal() is called exactly once. 4855251883Speter** In those cases, sqlite3_aggregate_context() might be called for the 4856251883Speter** first time from within xFinal().)^ 4857251883Speter** 4858251883Speter** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer 4859251883Speter** when first called if N is less than or equal to zero or if a memory 4860251883Speter** allocate error occurs. 4861251883Speter** 4862251883Speter** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is 4863251883Speter** determined by the N parameter on first successful call. Changing the 4864251883Speter** value of N in subsequent call to sqlite3_aggregate_context() within 4865251883Speter** the same aggregate function instance will not resize the memory 4866251883Speter** allocation.)^ Within the xFinal callback, it is customary to set 4867251883Speter** N=0 in calls to sqlite3_aggregate_context(C,N) so that no 4868251883Speter** pointless memory allocations occur. 4869251883Speter** 4870251883Speter** ^SQLite automatically frees the memory allocated by 4871251883Speter** sqlite3_aggregate_context() when the aggregate query concludes. 4872251883Speter** 4873251883Speter** The first parameter must be a copy of the 4874251883Speter** [sqlite3_context | SQL function context] that is the first parameter 4875251883Speter** to the xStep or xFinal callback routine that implements the aggregate 4876251883Speter** function. 4877251883Speter** 4878251883Speter** This routine must be called from the same thread in which 4879251883Speter** the aggregate SQL function is running. 4880251883Speter*/ 4881322444SpeterSQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); 4882251883Speter 4883251883Speter/* 4884251883Speter** CAPI3REF: User Data For Functions 4885286510Speter** METHOD: sqlite3_context 4886251883Speter** 4887251883Speter** ^The sqlite3_user_data() interface returns a copy of 4888251883Speter** the pointer that was the pUserData parameter (the 5th parameter) 4889251883Speter** of the [sqlite3_create_function()] 4890251883Speter** and [sqlite3_create_function16()] routines that originally 4891251883Speter** registered the application defined function. 4892251883Speter** 4893251883Speter** This routine must be called from the same thread in which 4894251883Speter** the application-defined function is running. 4895251883Speter*/ 4896322444SpeterSQLITE_API void *sqlite3_user_data(sqlite3_context*); 4897251883Speter 4898251883Speter/* 4899251883Speter** CAPI3REF: Database Connection For Functions 4900286510Speter** METHOD: sqlite3_context 4901251883Speter** 4902251883Speter** ^The sqlite3_context_db_handle() interface returns a copy of 4903251883Speter** the pointer to the [database connection] (the 1st parameter) 4904251883Speter** of the [sqlite3_create_function()] 4905251883Speter** and [sqlite3_create_function16()] routines that originally 4906251883Speter** registered the application defined function. 4907251883Speter*/ 4908322444SpeterSQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*); 4909251883Speter 4910251883Speter/* 4911251883Speter** CAPI3REF: Function Auxiliary Data 4912286510Speter** METHOD: sqlite3_context 4913251883Speter** 4914269851Speter** These functions may be used by (non-aggregate) SQL functions to 4915251883Speter** associate metadata with argument values. If the same value is passed to 4916251883Speter** multiple invocations of the same SQL function during query execution, under 4917269851Speter** some circumstances the associated metadata may be preserved. An example 4918269851Speter** of where this might be useful is in a regular-expression matching 4919269851Speter** function. The compiled version of the regular expression can be stored as 4920269851Speter** metadata associated with the pattern string. 4921269851Speter** Then as long as the pattern string remains the same, 4922269851Speter** the compiled regular expression can be reused on multiple 4923269851Speter** invocations of the same function. 4924251883Speter** 4925322444Speter** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata 4926322444Speter** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument 4927322444Speter** value to the application-defined function. ^N is zero for the left-most 4928322444Speter** function argument. ^If there is no metadata 4929322444Speter** associated with the function argument, the sqlite3_get_auxdata(C,N) interface 4930269851Speter** returns a NULL pointer. 4931251883Speter** 4932269851Speter** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th 4933269851Speter** argument of the application-defined function. ^Subsequent 4934269851Speter** calls to sqlite3_get_auxdata(C,N) return P from the most recent 4935269851Speter** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or 4936269851Speter** NULL if the metadata has been discarded. 4937269851Speter** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL, 4938269851Speter** SQLite will invoke the destructor function X with parameter P exactly 4939269851Speter** once, when the metadata is discarded. 4940269851Speter** SQLite is free to discard the metadata at any time, including: <ul> 4941305002Scy** <li> ^(when the corresponding function parameter changes)^, or 4942305002Scy** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the 4943305002Scy** SQL statement)^, or 4944305002Scy** <li> ^(when sqlite3_set_auxdata() is invoked again on the same 4945305002Scy** parameter)^, or 4946305002Scy** <li> ^(during the original sqlite3_set_auxdata() call when a memory 4947305002Scy** allocation error occurs.)^ </ul> 4948251883Speter** 4949269851Speter** Note the last bullet in particular. The destructor X in 4950269851Speter** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the 4951269851Speter** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata() 4952269851Speter** should be called near the end of the function implementation and the 4953269851Speter** function implementation should not make any use of P after 4954269851Speter** sqlite3_set_auxdata() has been called. 4955251883Speter** 4956251883Speter** ^(In practice, metadata is preserved between function calls for 4957269851Speter** function parameters that are compile-time constants, including literal 4958269851Speter** values and [parameters] and expressions composed from the same.)^ 4959251883Speter** 4960322444Speter** The value of the N parameter to these interfaces should be non-negative. 4961322444Speter** Future enhancements may make use of negative N values to define new 4962322444Speter** kinds of function caching behavior. 4963322444Speter** 4964251883Speter** These routines must be called from the same thread in which 4965251883Speter** the SQL function is running. 4966251883Speter*/ 4967322444SpeterSQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N); 4968322444SpeterSQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); 4969251883Speter 4970251883Speter 4971251883Speter/* 4972251883Speter** CAPI3REF: Constants Defining Special Destructor Behavior 4973251883Speter** 4974251883Speter** These are special values for the destructor that is passed in as the 4975251883Speter** final argument to routines like [sqlite3_result_blob()]. ^If the destructor 4976251883Speter** argument is SQLITE_STATIC, it means that the content pointer is constant 4977251883Speter** and will never change. It does not need to be destroyed. ^The 4978251883Speter** SQLITE_TRANSIENT value means that the content will likely change in 4979251883Speter** the near future and that SQLite should make its own private copy of 4980251883Speter** the content before returning. 4981251883Speter** 4982251883Speter** The typedef is necessary to work around problems in certain 4983251883Speter** C++ compilers. 4984251883Speter*/ 4985251883Spetertypedef void (*sqlite3_destructor_type)(void*); 4986251883Speter#define SQLITE_STATIC ((sqlite3_destructor_type)0) 4987251883Speter#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) 4988251883Speter 4989251883Speter/* 4990251883Speter** CAPI3REF: Setting The Result Of An SQL Function 4991286510Speter** METHOD: sqlite3_context 4992251883Speter** 4993251883Speter** These routines are used by the xFunc or xFinal callbacks that 4994251883Speter** implement SQL functions and aggregates. See 4995251883Speter** [sqlite3_create_function()] and [sqlite3_create_function16()] 4996251883Speter** for additional information. 4997251883Speter** 4998251883Speter** These functions work very much like the [parameter binding] family of 4999251883Speter** functions used to bind values to host parameters in prepared statements. 5000251883Speter** Refer to the [SQL parameter] documentation for additional information. 5001251883Speter** 5002251883Speter** ^The sqlite3_result_blob() interface sets the result from 5003251883Speter** an application-defined function to be the BLOB whose content is pointed 5004251883Speter** to by the second parameter and which is N bytes long where N is the 5005251883Speter** third parameter. 5006251883Speter** 5007286510Speter** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N) 5008286510Speter** interfaces set the result of the application-defined function to be 5009286510Speter** a BLOB containing all zero bytes and N bytes in size. 5010251883Speter** 5011251883Speter** ^The sqlite3_result_double() interface sets the result from 5012251883Speter** an application-defined function to be a floating point value specified 5013251883Speter** by its 2nd argument. 5014251883Speter** 5015251883Speter** ^The sqlite3_result_error() and sqlite3_result_error16() functions 5016251883Speter** cause the implemented SQL function to throw an exception. 5017251883Speter** ^SQLite uses the string pointed to by the 5018251883Speter** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() 5019251883Speter** as the text of an error message. ^SQLite interprets the error 5020251883Speter** message string from sqlite3_result_error() as UTF-8. ^SQLite 5021251883Speter** interprets the string from sqlite3_result_error16() as UTF-16 in native 5022251883Speter** byte order. ^If the third parameter to sqlite3_result_error() 5023251883Speter** or sqlite3_result_error16() is negative then SQLite takes as the error 5024251883Speter** message all text up through the first zero character. 5025251883Speter** ^If the third parameter to sqlite3_result_error() or 5026251883Speter** sqlite3_result_error16() is non-negative then SQLite takes that many 5027251883Speter** bytes (not characters) from the 2nd parameter as the error message. 5028251883Speter** ^The sqlite3_result_error() and sqlite3_result_error16() 5029251883Speter** routines make a private copy of the error message text before 5030251883Speter** they return. Hence, the calling function can deallocate or 5031251883Speter** modify the text after they return without harm. 5032251883Speter** ^The sqlite3_result_error_code() function changes the error code 5033251883Speter** returned by SQLite as a result of an error in a function. ^By default, 5034251883Speter** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error() 5035251883Speter** or sqlite3_result_error16() resets the error code to SQLITE_ERROR. 5036251883Speter** 5037251883Speter** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an 5038251883Speter** error indicating that a string or BLOB is too long to represent. 5039251883Speter** 5040251883Speter** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an 5041251883Speter** error indicating that a memory allocation failed. 5042251883Speter** 5043251883Speter** ^The sqlite3_result_int() interface sets the return value 5044251883Speter** of the application-defined function to be the 32-bit signed integer 5045251883Speter** value given in the 2nd argument. 5046251883Speter** ^The sqlite3_result_int64() interface sets the return value 5047251883Speter** of the application-defined function to be the 64-bit signed integer 5048251883Speter** value given in the 2nd argument. 5049251883Speter** 5050251883Speter** ^The sqlite3_result_null() interface sets the return value 5051251883Speter** of the application-defined function to be NULL. 5052251883Speter** 5053251883Speter** ^The sqlite3_result_text(), sqlite3_result_text16(), 5054251883Speter** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces 5055251883Speter** set the return value of the application-defined function to be 5056251883Speter** a text string which is represented as UTF-8, UTF-16 native byte order, 5057251883Speter** UTF-16 little endian, or UTF-16 big endian, respectively. 5058274884Sbapt** ^The sqlite3_result_text64() interface sets the return value of an 5059274884Sbapt** application-defined function to be a text string in an encoding 5060274884Sbapt** specified by the fifth (and last) parameter, which must be one 5061274884Sbapt** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]. 5062251883Speter** ^SQLite takes the text result from the application from 5063251883Speter** the 2nd parameter of the sqlite3_result_text* interfaces. 5064251883Speter** ^If the 3rd parameter to the sqlite3_result_text* interfaces 5065251883Speter** is negative, then SQLite takes result text from the 2nd parameter 5066251883Speter** through the first zero character. 5067251883Speter** ^If the 3rd parameter to the sqlite3_result_text* interfaces 5068251883Speter** is non-negative, then as many bytes (not characters) of the text 5069251883Speter** pointed to by the 2nd parameter are taken as the application-defined 5070251883Speter** function result. If the 3rd parameter is non-negative, then it 5071251883Speter** must be the byte offset into the string where the NUL terminator would 5072251883Speter** appear if the string where NUL terminated. If any NUL characters occur 5073251883Speter** in the string at a byte offset that is less than the value of the 3rd 5074251883Speter** parameter, then the resulting string will contain embedded NULs and the 5075251883Speter** result of expressions operating on strings with embedded NULs is undefined. 5076251883Speter** ^If the 4th parameter to the sqlite3_result_text* interfaces 5077251883Speter** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that 5078251883Speter** function as the destructor on the text or BLOB result when it has 5079251883Speter** finished using that result. 5080251883Speter** ^If the 4th parameter to the sqlite3_result_text* interfaces or to 5081251883Speter** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite 5082251883Speter** assumes that the text or BLOB result is in constant space and does not 5083251883Speter** copy the content of the parameter nor call a destructor on the content 5084251883Speter** when it has finished using that result. 5085251883Speter** ^If the 4th parameter to the sqlite3_result_text* interfaces 5086251883Speter** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT 5087322444Speter** then SQLite makes a copy of the result into space obtained 5088251883Speter** from [sqlite3_malloc()] before it returns. 5089251883Speter** 5090251883Speter** ^The sqlite3_result_value() interface sets the result of 5091286510Speter** the application-defined function to be a copy of the 5092251883Speter** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The 5093251883Speter** sqlite3_result_value() interface makes a copy of the [sqlite3_value] 5094251883Speter** so that the [sqlite3_value] specified in the parameter may change or 5095251883Speter** be deallocated after sqlite3_result_value() returns without harm. 5096251883Speter** ^A [protected sqlite3_value] object may always be used where an 5097251883Speter** [unprotected sqlite3_value] object is required, so either 5098251883Speter** kind of [sqlite3_value] object can be used with this interface. 5099251883Speter** 5100322444Speter** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an 5101322444Speter** SQL NULL value, just like [sqlite3_result_null(C)], except that it 5102322444Speter** also associates the host-language pointer P or type T with that 5103322444Speter** NULL value such that the pointer can be retrieved within an 5104322444Speter** [application-defined SQL function] using [sqlite3_value_pointer()]. 5105322444Speter** ^If the D parameter is not NULL, then it is a pointer to a destructor 5106322444Speter** for the P parameter. ^SQLite invokes D with P as its only argument 5107322444Speter** when SQLite is finished with P. The T parameter should be a static 5108322444Speter** string and preferably a string literal. The sqlite3_result_pointer() 5109322444Speter** routine is part of the [pointer passing interface] added for SQLite 3.20.0. 5110322444Speter** 5111251883Speter** If these routines are called from within the different thread 5112251883Speter** than the one containing the application-defined function that received 5113251883Speter** the [sqlite3_context] pointer, the results are undefined. 5114251883Speter*/ 5115322444SpeterSQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); 5116322444SpeterSQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*, 5117282328Sbapt sqlite3_uint64,void(*)(void*)); 5118322444SpeterSQLITE_API void sqlite3_result_double(sqlite3_context*, double); 5119322444SpeterSQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int); 5120322444SpeterSQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int); 5121322444SpeterSQLITE_API void sqlite3_result_error_toobig(sqlite3_context*); 5122322444SpeterSQLITE_API void sqlite3_result_error_nomem(sqlite3_context*); 5123322444SpeterSQLITE_API void sqlite3_result_error_code(sqlite3_context*, int); 5124322444SpeterSQLITE_API void sqlite3_result_int(sqlite3_context*, int); 5125322444SpeterSQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); 5126322444SpeterSQLITE_API void sqlite3_result_null(sqlite3_context*); 5127322444SpeterSQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); 5128322444SpeterSQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64, 5129274884Sbapt void(*)(void*), unsigned char encoding); 5130322444SpeterSQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); 5131322444SpeterSQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); 5132322444SpeterSQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); 5133322444SpeterSQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*); 5134322444SpeterSQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*)); 5135322444SpeterSQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n); 5136322444SpeterSQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n); 5137251883Speter 5138298161Sbapt 5139251883Speter/* 5140298161Sbapt** CAPI3REF: Setting The Subtype Of An SQL Function 5141298161Sbapt** METHOD: sqlite3_context 5142298161Sbapt** 5143298161Sbapt** The sqlite3_result_subtype(C,T) function causes the subtype of 5144298161Sbapt** the result from the [application-defined SQL function] with 5145298161Sbapt** [sqlite3_context] C to be the value T. Only the lower 8 bits 5146298161Sbapt** of the subtype T are preserved in current versions of SQLite; 5147298161Sbapt** higher order bits are discarded. 5148298161Sbapt** The number of subtype bytes preserved by SQLite might increase 5149298161Sbapt** in future releases of SQLite. 5150298161Sbapt*/ 5151322444SpeterSQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int); 5152298161Sbapt 5153298161Sbapt/* 5154251883Speter** CAPI3REF: Define New Collating Sequences 5155286510Speter** METHOD: sqlite3 5156251883Speter** 5157251883Speter** ^These functions add, remove, or modify a [collation] associated 5158251883Speter** with the [database connection] specified as the first argument. 5159251883Speter** 5160251883Speter** ^The name of the collation is a UTF-8 string 5161251883Speter** for sqlite3_create_collation() and sqlite3_create_collation_v2() 5162251883Speter** and a UTF-16 string in native byte order for sqlite3_create_collation16(). 5163251883Speter** ^Collation names that compare equal according to [sqlite3_strnicmp()] are 5164251883Speter** considered to be the same name. 5165251883Speter** 5166251883Speter** ^(The third argument (eTextRep) must be one of the constants: 5167251883Speter** <ul> 5168251883Speter** <li> [SQLITE_UTF8], 5169251883Speter** <li> [SQLITE_UTF16LE], 5170251883Speter** <li> [SQLITE_UTF16BE], 5171251883Speter** <li> [SQLITE_UTF16], or 5172251883Speter** <li> [SQLITE_UTF16_ALIGNED]. 5173251883Speter** </ul>)^ 5174251883Speter** ^The eTextRep argument determines the encoding of strings passed 5175251883Speter** to the collating function callback, xCallback. 5176251883Speter** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep 5177251883Speter** force strings to be UTF16 with native byte order. 5178251883Speter** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin 5179251883Speter** on an even byte address. 5180251883Speter** 5181251883Speter** ^The fourth argument, pArg, is an application data pointer that is passed 5182251883Speter** through as the first argument to the collating function callback. 5183251883Speter** 5184251883Speter** ^The fifth argument, xCallback, is a pointer to the collating function. 5185251883Speter** ^Multiple collating functions can be registered using the same name but 5186251883Speter** with different eTextRep parameters and SQLite will use whichever 5187251883Speter** function requires the least amount of data transformation. 5188251883Speter** ^If the xCallback argument is NULL then the collating function is 5189251883Speter** deleted. ^When all collating functions having the same name are deleted, 5190251883Speter** that collation is no longer usable. 5191251883Speter** 5192251883Speter** ^The collating function callback is invoked with a copy of the pArg 5193251883Speter** application data pointer and with two strings in the encoding specified 5194251883Speter** by the eTextRep argument. The collating function must return an 5195251883Speter** integer that is negative, zero, or positive 5196251883Speter** if the first string is less than, equal to, or greater than the second, 5197251883Speter** respectively. A collating function must always return the same answer 5198251883Speter** given the same inputs. If two or more collating functions are registered 5199251883Speter** to the same collation name (using different eTextRep values) then all 5200251883Speter** must give an equivalent answer when invoked with equivalent strings. 5201251883Speter** The collating function must obey the following properties for all 5202251883Speter** strings A, B, and C: 5203251883Speter** 5204251883Speter** <ol> 5205251883Speter** <li> If A==B then B==A. 5206251883Speter** <li> If A==B and B==C then A==C. 5207251883Speter** <li> If A<B THEN B>A. 5208251883Speter** <li> If A<B and B<C then A<C. 5209251883Speter** </ol> 5210251883Speter** 5211251883Speter** If a collating function fails any of the above constraints and that 5212251883Speter** collating function is registered and used, then the behavior of SQLite 5213251883Speter** is undefined. 5214251883Speter** 5215251883Speter** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation() 5216251883Speter** with the addition that the xDestroy callback is invoked on pArg when 5217251883Speter** the collating function is deleted. 5218251883Speter** ^Collating functions are deleted when they are overridden by later 5219251883Speter** calls to the collation creation functions or when the 5220251883Speter** [database connection] is closed using [sqlite3_close()]. 5221251883Speter** 5222251883Speter** ^The xDestroy callback is <u>not</u> called if the 5223251883Speter** sqlite3_create_collation_v2() function fails. Applications that invoke 5224251883Speter** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should 5225251883Speter** check the return code and dispose of the application data pointer 5226251883Speter** themselves rather than expecting SQLite to deal with it for them. 5227251883Speter** This is different from every other SQLite interface. The inconsistency 5228251883Speter** is unfortunate but cannot be changed without breaking backwards 5229251883Speter** compatibility. 5230251883Speter** 5231251883Speter** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()]. 5232251883Speter*/ 5233322444SpeterSQLITE_API int sqlite3_create_collation( 5234251883Speter sqlite3*, 5235251883Speter const char *zName, 5236251883Speter int eTextRep, 5237251883Speter void *pArg, 5238251883Speter int(*xCompare)(void*,int,const void*,int,const void*) 5239251883Speter); 5240322444SpeterSQLITE_API int sqlite3_create_collation_v2( 5241251883Speter sqlite3*, 5242251883Speter const char *zName, 5243251883Speter int eTextRep, 5244251883Speter void *pArg, 5245251883Speter int(*xCompare)(void*,int,const void*,int,const void*), 5246251883Speter void(*xDestroy)(void*) 5247251883Speter); 5248322444SpeterSQLITE_API int sqlite3_create_collation16( 5249251883Speter sqlite3*, 5250251883Speter const void *zName, 5251251883Speter int eTextRep, 5252251883Speter void *pArg, 5253251883Speter int(*xCompare)(void*,int,const void*,int,const void*) 5254251883Speter); 5255251883Speter 5256251883Speter/* 5257251883Speter** CAPI3REF: Collation Needed Callbacks 5258286510Speter** METHOD: sqlite3 5259251883Speter** 5260251883Speter** ^To avoid having to register all collation sequences before a database 5261251883Speter** can be used, a single callback function may be registered with the 5262251883Speter** [database connection] to be invoked whenever an undefined collation 5263251883Speter** sequence is required. 5264251883Speter** 5265251883Speter** ^If the function is registered using the sqlite3_collation_needed() API, 5266251883Speter** then it is passed the names of undefined collation sequences as strings 5267251883Speter** encoded in UTF-8. ^If sqlite3_collation_needed16() is used, 5268251883Speter** the names are passed as UTF-16 in machine native byte order. 5269251883Speter** ^A call to either function replaces the existing collation-needed callback. 5270251883Speter** 5271251883Speter** ^(When the callback is invoked, the first argument passed is a copy 5272251883Speter** of the second argument to sqlite3_collation_needed() or 5273251883Speter** sqlite3_collation_needed16(). The second argument is the database 5274251883Speter** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE], 5275251883Speter** or [SQLITE_UTF16LE], indicating the most desirable form of the collation 5276251883Speter** sequence function required. The fourth parameter is the name of the 5277251883Speter** required collation sequence.)^ 5278251883Speter** 5279251883Speter** The callback function should register the desired collation using 5280251883Speter** [sqlite3_create_collation()], [sqlite3_create_collation16()], or 5281251883Speter** [sqlite3_create_collation_v2()]. 5282251883Speter*/ 5283322444SpeterSQLITE_API int sqlite3_collation_needed( 5284251883Speter sqlite3*, 5285251883Speter void*, 5286251883Speter void(*)(void*,sqlite3*,int eTextRep,const char*) 5287251883Speter); 5288322444SpeterSQLITE_API int sqlite3_collation_needed16( 5289251883Speter sqlite3*, 5290251883Speter void*, 5291251883Speter void(*)(void*,sqlite3*,int eTextRep,const void*) 5292251883Speter); 5293251883Speter 5294251883Speter#ifdef SQLITE_HAS_CODEC 5295251883Speter/* 5296251883Speter** Specify the key for an encrypted database. This routine should be 5297251883Speter** called right after sqlite3_open(). 5298251883Speter** 5299251883Speter** The code to implement this API is not available in the public release 5300251883Speter** of SQLite. 5301251883Speter*/ 5302322444SpeterSQLITE_API int sqlite3_key( 5303251883Speter sqlite3 *db, /* Database to be rekeyed */ 5304251883Speter const void *pKey, int nKey /* The key */ 5305251883Speter); 5306322444SpeterSQLITE_API int sqlite3_key_v2( 5307269851Speter sqlite3 *db, /* Database to be rekeyed */ 5308269851Speter const char *zDbName, /* Name of the database */ 5309269851Speter const void *pKey, int nKey /* The key */ 5310269851Speter); 5311251883Speter 5312251883Speter/* 5313251883Speter** Change the key on an open database. If the current database is not 5314251883Speter** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the 5315251883Speter** database is decrypted. 5316251883Speter** 5317251883Speter** The code to implement this API is not available in the public release 5318251883Speter** of SQLite. 5319251883Speter*/ 5320322444SpeterSQLITE_API int sqlite3_rekey( 5321251883Speter sqlite3 *db, /* Database to be rekeyed */ 5322251883Speter const void *pKey, int nKey /* The new key */ 5323251883Speter); 5324322444SpeterSQLITE_API int sqlite3_rekey_v2( 5325269851Speter sqlite3 *db, /* Database to be rekeyed */ 5326269851Speter const char *zDbName, /* Name of the database */ 5327269851Speter const void *pKey, int nKey /* The new key */ 5328269851Speter); 5329251883Speter 5330251883Speter/* 5331251883Speter** Specify the activation key for a SEE database. Unless 5332251883Speter** activated, none of the SEE routines will work. 5333251883Speter*/ 5334322444SpeterSQLITE_API void sqlite3_activate_see( 5335251883Speter const char *zPassPhrase /* Activation phrase */ 5336251883Speter); 5337251883Speter#endif 5338251883Speter 5339251883Speter#ifdef SQLITE_ENABLE_CEROD 5340251883Speter/* 5341251883Speter** Specify the activation key for a CEROD database. Unless 5342251883Speter** activated, none of the CEROD routines will work. 5343251883Speter*/ 5344322444SpeterSQLITE_API void sqlite3_activate_cerod( 5345251883Speter const char *zPassPhrase /* Activation phrase */ 5346251883Speter); 5347251883Speter#endif 5348251883Speter 5349251883Speter/* 5350251883Speter** CAPI3REF: Suspend Execution For A Short Time 5351251883Speter** 5352251883Speter** The sqlite3_sleep() function causes the current thread to suspend execution 5353251883Speter** for at least a number of milliseconds specified in its parameter. 5354251883Speter** 5355251883Speter** If the operating system does not support sleep requests with 5356251883Speter** millisecond time resolution, then the time will be rounded up to 5357251883Speter** the nearest second. The number of milliseconds of sleep actually 5358251883Speter** requested from the operating system is returned. 5359251883Speter** 5360251883Speter** ^SQLite implements this interface by calling the xSleep() 5361251883Speter** method of the default [sqlite3_vfs] object. If the xSleep() method 5362251883Speter** of the default VFS is not implemented correctly, or not implemented at 5363251883Speter** all, then the behavior of sqlite3_sleep() may deviate from the description 5364251883Speter** in the previous paragraphs. 5365251883Speter*/ 5366322444SpeterSQLITE_API int sqlite3_sleep(int); 5367251883Speter 5368251883Speter/* 5369251883Speter** CAPI3REF: Name Of The Folder Holding Temporary Files 5370251883Speter** 5371251883Speter** ^(If this global variable is made to point to a string which is 5372251883Speter** the name of a folder (a.k.a. directory), then all temporary files 5373251883Speter** created by SQLite when using a built-in [sqlite3_vfs | VFS] 5374251883Speter** will be placed in that directory.)^ ^If this variable 5375251883Speter** is a NULL pointer, then SQLite performs a search for an appropriate 5376251883Speter** temporary file directory. 5377251883Speter** 5378274884Sbapt** Applications are strongly discouraged from using this global variable. 5379274884Sbapt** It is required to set a temporary folder on Windows Runtime (WinRT). 5380274884Sbapt** But for all other platforms, it is highly recommended that applications 5381274884Sbapt** neither read nor write this variable. This global variable is a relic 5382274884Sbapt** that exists for backwards compatibility of legacy applications and should 5383274884Sbapt** be avoided in new projects. 5384274884Sbapt** 5385251883Speter** It is not safe to read or modify this variable in more than one 5386251883Speter** thread at a time. It is not safe to read or modify this variable 5387251883Speter** if a [database connection] is being used at the same time in a separate 5388251883Speter** thread. 5389251883Speter** It is intended that this variable be set once 5390251883Speter** as part of process initialization and before any SQLite interface 5391251883Speter** routines have been called and that this variable remain unchanged 5392251883Speter** thereafter. 5393251883Speter** 5394251883Speter** ^The [temp_store_directory pragma] may modify this variable and cause 5395251883Speter** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore, 5396251883Speter** the [temp_store_directory pragma] always assumes that any string 5397251883Speter** that this variable points to is held in memory obtained from 5398251883Speter** [sqlite3_malloc] and the pragma may attempt to free that memory 5399251883Speter** using [sqlite3_free]. 5400251883Speter** Hence, if this variable is modified directly, either it should be 5401251883Speter** made NULL or made to point to memory obtained from [sqlite3_malloc] 5402251883Speter** or else the use of the [temp_store_directory pragma] should be avoided. 5403274884Sbapt** Except when requested by the [temp_store_directory pragma], SQLite 5404274884Sbapt** does not free the memory that sqlite3_temp_directory points to. If 5405274884Sbapt** the application wants that memory to be freed, it must do 5406274884Sbapt** so itself, taking care to only do so after all [database connection] 5407274884Sbapt** objects have been destroyed. 5408251883Speter** 5409251883Speter** <b>Note to Windows Runtime users:</b> The temporary directory must be set 5410251883Speter** prior to calling [sqlite3_open] or [sqlite3_open_v2]. Otherwise, various 5411251883Speter** features that require the use of temporary files may fail. Here is an 5412251883Speter** example of how to do this using C++ with the Windows Runtime: 5413251883Speter** 5414251883Speter** <blockquote><pre> 5415251883Speter** LPCWSTR zPath = Windows::Storage::ApplicationData::Current-> 5416251883Speter** TemporaryFolder->Path->Data(); 5417251883Speter** char zPathBuf[MAX_PATH + 1]; 5418251883Speter** memset(zPathBuf, 0, sizeof(zPathBuf)); 5419251883Speter** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf), 5420251883Speter** NULL, NULL); 5421251883Speter** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf); 5422251883Speter** </pre></blockquote> 5423251883Speter*/ 5424251883SpeterSQLITE_API SQLITE_EXTERN char *sqlite3_temp_directory; 5425251883Speter 5426251883Speter/* 5427251883Speter** CAPI3REF: Name Of The Folder Holding Database Files 5428251883Speter** 5429251883Speter** ^(If this global variable is made to point to a string which is 5430251883Speter** the name of a folder (a.k.a. directory), then all database files 5431251883Speter** specified with a relative pathname and created or accessed by 5432251883Speter** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed 5433251883Speter** to be relative to that directory.)^ ^If this variable is a NULL 5434251883Speter** pointer, then SQLite assumes that all database files specified 5435251883Speter** with a relative pathname are relative to the current directory 5436251883Speter** for the process. Only the windows VFS makes use of this global 5437251883Speter** variable; it is ignored by the unix VFS. 5438251883Speter** 5439251883Speter** Changing the value of this variable while a database connection is 5440251883Speter** open can result in a corrupt database. 5441251883Speter** 5442251883Speter** It is not safe to read or modify this variable in more than one 5443251883Speter** thread at a time. It is not safe to read or modify this variable 5444251883Speter** if a [database connection] is being used at the same time in a separate 5445251883Speter** thread. 5446251883Speter** It is intended that this variable be set once 5447251883Speter** as part of process initialization and before any SQLite interface 5448251883Speter** routines have been called and that this variable remain unchanged 5449251883Speter** thereafter. 5450251883Speter** 5451251883Speter** ^The [data_store_directory pragma] may modify this variable and cause 5452251883Speter** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore, 5453251883Speter** the [data_store_directory pragma] always assumes that any string 5454251883Speter** that this variable points to is held in memory obtained from 5455251883Speter** [sqlite3_malloc] and the pragma may attempt to free that memory 5456251883Speter** using [sqlite3_free]. 5457251883Speter** Hence, if this variable is modified directly, either it should be 5458251883Speter** made NULL or made to point to memory obtained from [sqlite3_malloc] 5459251883Speter** or else the use of the [data_store_directory pragma] should be avoided. 5460251883Speter*/ 5461251883SpeterSQLITE_API SQLITE_EXTERN char *sqlite3_data_directory; 5462251883Speter 5463251883Speter/* 5464251883Speter** CAPI3REF: Test For Auto-Commit Mode 5465251883Speter** KEYWORDS: {autocommit mode} 5466286510Speter** METHOD: sqlite3 5467251883Speter** 5468251883Speter** ^The sqlite3_get_autocommit() interface returns non-zero or 5469251883Speter** zero if the given database connection is or is not in autocommit mode, 5470251883Speter** respectively. ^Autocommit mode is on by default. 5471251883Speter** ^Autocommit mode is disabled by a [BEGIN] statement. 5472251883Speter** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK]. 5473251883Speter** 5474251883Speter** If certain kinds of errors occur on a statement within a multi-statement 5475251883Speter** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR], 5476251883Speter** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the 5477251883Speter** transaction might be rolled back automatically. The only way to 5478251883Speter** find out whether SQLite automatically rolled back the transaction after 5479251883Speter** an error is to use this function. 5480251883Speter** 5481251883Speter** If another thread changes the autocommit status of the database 5482251883Speter** connection while this routine is running, then the return value 5483251883Speter** is undefined. 5484251883Speter*/ 5485322444SpeterSQLITE_API int sqlite3_get_autocommit(sqlite3*); 5486251883Speter 5487251883Speter/* 5488251883Speter** CAPI3REF: Find The Database Handle Of A Prepared Statement 5489286510Speter** METHOD: sqlite3_stmt 5490251883Speter** 5491251883Speter** ^The sqlite3_db_handle interface returns the [database connection] handle 5492251883Speter** to which a [prepared statement] belongs. ^The [database connection] 5493251883Speter** returned by sqlite3_db_handle is the same [database connection] 5494251883Speter** that was the first argument 5495251883Speter** to the [sqlite3_prepare_v2()] call (or its variants) that was used to 5496251883Speter** create the statement in the first place. 5497251883Speter*/ 5498322444SpeterSQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*); 5499251883Speter 5500251883Speter/* 5501251883Speter** CAPI3REF: Return The Filename For A Database Connection 5502286510Speter** METHOD: sqlite3 5503251883Speter** 5504251883Speter** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename 5505251883Speter** associated with database N of connection D. ^The main database file 5506251883Speter** has the name "main". If there is no attached database N on the database 5507251883Speter** connection D, or if database N is a temporary or in-memory database, then 5508251883Speter** a NULL pointer is returned. 5509251883Speter** 5510251883Speter** ^The filename returned by this function is the output of the 5511251883Speter** xFullPathname method of the [VFS]. ^In other words, the filename 5512251883Speter** will be an absolute pathname, even if the filename used 5513251883Speter** to open the database originally was a URI or relative pathname. 5514251883Speter*/ 5515322444SpeterSQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName); 5516251883Speter 5517251883Speter/* 5518251883Speter** CAPI3REF: Determine if a database is read-only 5519286510Speter** METHOD: sqlite3 5520251883Speter** 5521251883Speter** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N 5522251883Speter** of connection D is read-only, 0 if it is read/write, or -1 if N is not 5523251883Speter** the name of a database on connection D. 5524251883Speter*/ 5525322444SpeterSQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName); 5526251883Speter 5527251883Speter/* 5528251883Speter** CAPI3REF: Find the next prepared statement 5529286510Speter** METHOD: sqlite3 5530251883Speter** 5531251883Speter** ^This interface returns a pointer to the next [prepared statement] after 5532251883Speter** pStmt associated with the [database connection] pDb. ^If pStmt is NULL 5533251883Speter** then this interface returns a pointer to the first prepared statement 5534251883Speter** associated with the database connection pDb. ^If no prepared statement 5535251883Speter** satisfies the conditions of this routine, it returns NULL. 5536251883Speter** 5537251883Speter** The [database connection] pointer D in a call to 5538251883Speter** [sqlite3_next_stmt(D,S)] must refer to an open database 5539251883Speter** connection and in particular must not be a NULL pointer. 5540251883Speter*/ 5541322444SpeterSQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt); 5542251883Speter 5543251883Speter/* 5544251883Speter** CAPI3REF: Commit And Rollback Notification Callbacks 5545286510Speter** METHOD: sqlite3 5546251883Speter** 5547251883Speter** ^The sqlite3_commit_hook() interface registers a callback 5548251883Speter** function to be invoked whenever a transaction is [COMMIT | committed]. 5549251883Speter** ^Any callback set by a previous call to sqlite3_commit_hook() 5550251883Speter** for the same database connection is overridden. 5551251883Speter** ^The sqlite3_rollback_hook() interface registers a callback 5552251883Speter** function to be invoked whenever a transaction is [ROLLBACK | rolled back]. 5553251883Speter** ^Any callback set by a previous call to sqlite3_rollback_hook() 5554251883Speter** for the same database connection is overridden. 5555251883Speter** ^The pArg argument is passed through to the callback. 5556251883Speter** ^If the callback on a commit hook function returns non-zero, 5557251883Speter** then the commit is converted into a rollback. 5558251883Speter** 5559251883Speter** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions 5560251883Speter** return the P argument from the previous call of the same function 5561251883Speter** on the same [database connection] D, or NULL for 5562251883Speter** the first call for each function on D. 5563251883Speter** 5564251883Speter** The commit and rollback hook callbacks are not reentrant. 5565251883Speter** The callback implementation must not do anything that will modify 5566251883Speter** the database connection that invoked the callback. Any actions 5567251883Speter** to modify the database connection must be deferred until after the 5568251883Speter** completion of the [sqlite3_step()] call that triggered the commit 5569251883Speter** or rollback hook in the first place. 5570251883Speter** Note that running any other SQL statements, including SELECT statements, 5571251883Speter** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify 5572251883Speter** the database connections for the meaning of "modify" in this paragraph. 5573251883Speter** 5574251883Speter** ^Registering a NULL function disables the callback. 5575251883Speter** 5576251883Speter** ^When the commit hook callback routine returns zero, the [COMMIT] 5577251883Speter** operation is allowed to continue normally. ^If the commit hook 5578251883Speter** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK]. 5579251883Speter** ^The rollback hook is invoked on a rollback that results from a commit 5580251883Speter** hook returning non-zero, just as it would be with any other rollback. 5581251883Speter** 5582251883Speter** ^For the purposes of this API, a transaction is said to have been 5583251883Speter** rolled back if an explicit "ROLLBACK" statement is executed, or 5584251883Speter** an error or constraint causes an implicit rollback to occur. 5585251883Speter** ^The rollback callback is not invoked if a transaction is 5586251883Speter** automatically rolled back because the database connection is closed. 5587251883Speter** 5588251883Speter** See also the [sqlite3_update_hook()] interface. 5589251883Speter*/ 5590322444SpeterSQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); 5591322444SpeterSQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); 5592251883Speter 5593251883Speter/* 5594251883Speter** CAPI3REF: Data Change Notification Callbacks 5595286510Speter** METHOD: sqlite3 5596251883Speter** 5597251883Speter** ^The sqlite3_update_hook() interface registers a callback function 5598251883Speter** with the [database connection] identified by the first argument 5599269851Speter** to be invoked whenever a row is updated, inserted or deleted in 5600305002Scy** a [rowid table]. 5601251883Speter** ^Any callback set by a previous call to this function 5602251883Speter** for the same database connection is overridden. 5603251883Speter** 5604251883Speter** ^The second argument is a pointer to the function to invoke when a 5605269851Speter** row is updated, inserted or deleted in a rowid table. 5606251883Speter** ^The first argument to the callback is a copy of the third argument 5607251883Speter** to sqlite3_update_hook(). 5608251883Speter** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE], 5609251883Speter** or [SQLITE_UPDATE], depending on the operation that caused the callback 5610251883Speter** to be invoked. 5611251883Speter** ^The third and fourth arguments to the callback contain pointers to the 5612251883Speter** database and table name containing the affected row. 5613251883Speter** ^The final callback parameter is the [rowid] of the row. 5614251883Speter** ^In the case of an update, this is the [rowid] after the update takes place. 5615251883Speter** 5616251883Speter** ^(The update hook is not invoked when internal system tables are 5617251883Speter** modified (i.e. sqlite_master and sqlite_sequence).)^ 5618269851Speter** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified. 5619251883Speter** 5620251883Speter** ^In the current implementation, the update hook 5621322444Speter** is not invoked when conflicting rows are deleted because of an 5622251883Speter** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook 5623251883Speter** invoked when rows are deleted using the [truncate optimization]. 5624251883Speter** The exceptions defined in this paragraph might change in a future 5625251883Speter** release of SQLite. 5626251883Speter** 5627251883Speter** The update hook implementation must not do anything that will modify 5628251883Speter** the database connection that invoked the update hook. Any actions 5629251883Speter** to modify the database connection must be deferred until after the 5630251883Speter** completion of the [sqlite3_step()] call that triggered the update hook. 5631251883Speter** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their 5632251883Speter** database connections for the meaning of "modify" in this paragraph. 5633251883Speter** 5634251883Speter** ^The sqlite3_update_hook(D,C,P) function 5635251883Speter** returns the P argument from the previous call 5636251883Speter** on the same [database connection] D, or NULL for 5637251883Speter** the first call on D. 5638251883Speter** 5639305002Scy** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()], 5640305002Scy** and [sqlite3_preupdate_hook()] interfaces. 5641251883Speter*/ 5642322444SpeterSQLITE_API void *sqlite3_update_hook( 5643251883Speter sqlite3*, 5644251883Speter void(*)(void *,int ,char const *,char const *,sqlite3_int64), 5645251883Speter void* 5646251883Speter); 5647251883Speter 5648251883Speter/* 5649251883Speter** CAPI3REF: Enable Or Disable Shared Pager Cache 5650251883Speter** 5651251883Speter** ^(This routine enables or disables the sharing of the database cache 5652251883Speter** and schema data structures between [database connection | connections] 5653251883Speter** to the same database. Sharing is enabled if the argument is true 5654251883Speter** and disabled if the argument is false.)^ 5655251883Speter** 5656251883Speter** ^Cache sharing is enabled and disabled for an entire process. 5657322444Speter** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]). 5658322444Speter** In prior versions of SQLite, 5659251883Speter** sharing was enabled or disabled for each thread separately. 5660251883Speter** 5661251883Speter** ^(The cache sharing mode set by this interface effects all subsequent 5662251883Speter** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. 5663251883Speter** Existing database connections continue use the sharing mode 5664251883Speter** that was in effect at the time they were opened.)^ 5665251883Speter** 5666251883Speter** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled 5667251883Speter** successfully. An [error code] is returned otherwise.)^ 5668251883Speter** 5669251883Speter** ^Shared cache is disabled by default. But this might change in 5670251883Speter** future releases of SQLite. Applications that care about shared 5671251883Speter** cache setting should set it explicitly. 5672251883Speter** 5673282328Sbapt** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0 5674282328Sbapt** and will always return SQLITE_MISUSE. On those systems, 5675282328Sbapt** shared cache mode should be enabled per-database connection via 5676282328Sbapt** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE]. 5677282328Sbapt** 5678251883Speter** This interface is threadsafe on processors where writing a 5679251883Speter** 32-bit integer is atomic. 5680251883Speter** 5681251883Speter** See Also: [SQLite Shared-Cache Mode] 5682251883Speter*/ 5683322444SpeterSQLITE_API int sqlite3_enable_shared_cache(int); 5684251883Speter 5685251883Speter/* 5686251883Speter** CAPI3REF: Attempt To Free Heap Memory 5687251883Speter** 5688251883Speter** ^The sqlite3_release_memory() interface attempts to free N bytes 5689251883Speter** of heap memory by deallocating non-essential memory allocations 5690251883Speter** held by the database library. Memory used to cache database 5691251883Speter** pages to improve performance is an example of non-essential memory. 5692251883Speter** ^sqlite3_release_memory() returns the number of bytes actually freed, 5693251883Speter** which might be more or less than the amount requested. 5694251883Speter** ^The sqlite3_release_memory() routine is a no-op returning zero 5695251883Speter** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT]. 5696251883Speter** 5697251883Speter** See also: [sqlite3_db_release_memory()] 5698251883Speter*/ 5699322444SpeterSQLITE_API int sqlite3_release_memory(int); 5700251883Speter 5701251883Speter/* 5702251883Speter** CAPI3REF: Free Memory Used By A Database Connection 5703286510Speter** METHOD: sqlite3 5704251883Speter** 5705251883Speter** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap 5706251883Speter** memory as possible from database connection D. Unlike the 5707269851Speter** [sqlite3_release_memory()] interface, this interface is in effect even 5708269851Speter** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is 5709251883Speter** omitted. 5710251883Speter** 5711251883Speter** See also: [sqlite3_release_memory()] 5712251883Speter*/ 5713322444SpeterSQLITE_API int sqlite3_db_release_memory(sqlite3*); 5714251883Speter 5715251883Speter/* 5716251883Speter** CAPI3REF: Impose A Limit On Heap Size 5717251883Speter** 5718251883Speter** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the 5719251883Speter** soft limit on the amount of heap memory that may be allocated by SQLite. 5720251883Speter** ^SQLite strives to keep heap memory utilization below the soft heap 5721251883Speter** limit by reducing the number of pages held in the page cache 5722251883Speter** as heap memory usages approaches the limit. 5723251883Speter** ^The soft heap limit is "soft" because even though SQLite strives to stay 5724251883Speter** below the limit, it will exceed the limit rather than generate 5725251883Speter** an [SQLITE_NOMEM] error. In other words, the soft heap limit 5726251883Speter** is advisory only. 5727251883Speter** 5728251883Speter** ^The return value from sqlite3_soft_heap_limit64() is the size of 5729251883Speter** the soft heap limit prior to the call, or negative in the case of an 5730251883Speter** error. ^If the argument N is negative 5731251883Speter** then no change is made to the soft heap limit. Hence, the current 5732251883Speter** size of the soft heap limit can be determined by invoking 5733251883Speter** sqlite3_soft_heap_limit64() with a negative argument. 5734251883Speter** 5735251883Speter** ^If the argument N is zero then the soft heap limit is disabled. 5736251883Speter** 5737251883Speter** ^(The soft heap limit is not enforced in the current implementation 5738251883Speter** if one or more of following conditions are true: 5739251883Speter** 5740251883Speter** <ul> 5741251883Speter** <li> The soft heap limit is set to zero. 5742251883Speter** <li> Memory accounting is disabled using a combination of the 5743251883Speter** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and 5744251883Speter** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option. 5745251883Speter** <li> An alternative page cache implementation is specified using 5746251883Speter** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...). 5747251883Speter** <li> The page cache allocates from its own memory pool supplied 5748251883Speter** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than 5749251883Speter** from the heap. 5750251883Speter** </ul>)^ 5751251883Speter** 5752322444Speter** Beginning with SQLite [version 3.7.3] ([dateof:3.7.3]), 5753322444Speter** the soft heap limit is enforced 5754251883Speter** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT] 5755251883Speter** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT], 5756251883Speter** the soft heap limit is enforced on every memory allocation. Without 5757251883Speter** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced 5758251883Speter** when memory is allocated by the page cache. Testing suggests that because 5759251883Speter** the page cache is the predominate memory user in SQLite, most 5760251883Speter** applications will achieve adequate soft heap limit enforcement without 5761251883Speter** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT]. 5762251883Speter** 5763251883Speter** The circumstances under which SQLite will enforce the soft heap limit may 5764251883Speter** changes in future releases of SQLite. 5765251883Speter*/ 5766322444SpeterSQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N); 5767251883Speter 5768251883Speter/* 5769251883Speter** CAPI3REF: Deprecated Soft Heap Limit Interface 5770251883Speter** DEPRECATED 5771251883Speter** 5772251883Speter** This is a deprecated version of the [sqlite3_soft_heap_limit64()] 5773251883Speter** interface. This routine is provided for historical compatibility 5774251883Speter** only. All new applications should use the 5775251883Speter** [sqlite3_soft_heap_limit64()] interface rather than this one. 5776251883Speter*/ 5777322444SpeterSQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N); 5778251883Speter 5779251883Speter 5780251883Speter/* 5781251883Speter** CAPI3REF: Extract Metadata About A Column Of A Table 5782286510Speter** METHOD: sqlite3 5783251883Speter** 5784282328Sbapt** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns 5785282328Sbapt** information about column C of table T in database D 5786282328Sbapt** on [database connection] X.)^ ^The sqlite3_table_column_metadata() 5787282328Sbapt** interface returns SQLITE_OK and fills in the non-NULL pointers in 5788282328Sbapt** the final five arguments with appropriate values if the specified 5789282328Sbapt** column exists. ^The sqlite3_table_column_metadata() interface returns 5790282328Sbapt** SQLITE_ERROR and if the specified column does not exist. 5791282328Sbapt** ^If the column-name parameter to sqlite3_table_column_metadata() is a 5792305002Scy** NULL pointer, then this routine simply checks for the existence of the 5793282328Sbapt** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it 5794322444Speter** does not. If the table name parameter T in a call to 5795322444Speter** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is 5796322444Speter** undefined behavior. 5797251883Speter** 5798251883Speter** ^The column is identified by the second, third and fourth parameters to 5799282328Sbapt** this function. ^(The second parameter is either the name of the database 5800251883Speter** (i.e. "main", "temp", or an attached database) containing the specified 5801282328Sbapt** table or NULL.)^ ^If it is NULL, then all attached databases are searched 5802251883Speter** for the table using the same algorithm used by the database engine to 5803251883Speter** resolve unqualified table references. 5804251883Speter** 5805251883Speter** ^The third and fourth parameters to this function are the table and column 5806282328Sbapt** name of the desired column, respectively. 5807251883Speter** 5808251883Speter** ^Metadata is returned by writing to the memory locations passed as the 5th 5809251883Speter** and subsequent parameters to this function. ^Any of these arguments may be 5810251883Speter** NULL, in which case the corresponding element of metadata is omitted. 5811251883Speter** 5812251883Speter** ^(<blockquote> 5813251883Speter** <table border="1"> 5814251883Speter** <tr><th> Parameter <th> Output<br>Type <th> Description 5815251883Speter** 5816251883Speter** <tr><td> 5th <td> const char* <td> Data type 5817251883Speter** <tr><td> 6th <td> const char* <td> Name of default collation sequence 5818251883Speter** <tr><td> 7th <td> int <td> True if column has a NOT NULL constraint 5819251883Speter** <tr><td> 8th <td> int <td> True if column is part of the PRIMARY KEY 5820251883Speter** <tr><td> 9th <td> int <td> True if column is [AUTOINCREMENT] 5821251883Speter** </table> 5822251883Speter** </blockquote>)^ 5823251883Speter** 5824251883Speter** ^The memory pointed to by the character pointers returned for the 5825282328Sbapt** declaration type and collation sequence is valid until the next 5826251883Speter** call to any SQLite API function. 5827251883Speter** 5828251883Speter** ^If the specified table is actually a view, an [error code] is returned. 5829251883Speter** 5830282328Sbapt** ^If the specified column is "rowid", "oid" or "_rowid_" and the table 5831282328Sbapt** is not a [WITHOUT ROWID] table and an 5832251883Speter** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output 5833251883Speter** parameters are set for the explicitly declared column. ^(If there is no 5834282328Sbapt** [INTEGER PRIMARY KEY] column, then the outputs 5835282328Sbapt** for the [rowid] are set as follows: 5836251883Speter** 5837251883Speter** <pre> 5838251883Speter** data type: "INTEGER" 5839251883Speter** collation sequence: "BINARY" 5840251883Speter** not null: 0 5841251883Speter** primary key: 1 5842251883Speter** auto increment: 0 5843251883Speter** </pre>)^ 5844251883Speter** 5845282328Sbapt** ^This function causes all database schemas to be read from disk and 5846282328Sbapt** parsed, if that has not already been done, and returns an error if 5847282328Sbapt** any errors are encountered while loading the schema. 5848251883Speter*/ 5849322444SpeterSQLITE_API int sqlite3_table_column_metadata( 5850251883Speter sqlite3 *db, /* Connection handle */ 5851251883Speter const char *zDbName, /* Database name or NULL */ 5852251883Speter const char *zTableName, /* Table name */ 5853251883Speter const char *zColumnName, /* Column name */ 5854251883Speter char const **pzDataType, /* OUTPUT: Declared data type */ 5855251883Speter char const **pzCollSeq, /* OUTPUT: Collation sequence name */ 5856251883Speter int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ 5857251883Speter int *pPrimaryKey, /* OUTPUT: True if column part of PK */ 5858251883Speter int *pAutoinc /* OUTPUT: True if column is auto-increment */ 5859251883Speter); 5860251883Speter 5861251883Speter/* 5862251883Speter** CAPI3REF: Load An Extension 5863286510Speter** METHOD: sqlite3 5864251883Speter** 5865251883Speter** ^This interface loads an SQLite extension library from the named file. 5866251883Speter** 5867251883Speter** ^The sqlite3_load_extension() interface attempts to load an 5868251883Speter** [SQLite extension] library contained in the file zFile. If 5869251883Speter** the file cannot be loaded directly, attempts are made to load 5870251883Speter** with various operating-system specific extensions added. 5871251883Speter** So for example, if "samplelib" cannot be loaded, then names like 5872251883Speter** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might 5873251883Speter** be tried also. 5874251883Speter** 5875251883Speter** ^The entry point is zProc. 5876251883Speter** ^(zProc may be 0, in which case SQLite will try to come up with an 5877251883Speter** entry point name on its own. It first tries "sqlite3_extension_init". 5878251883Speter** If that does not work, it constructs a name "sqlite3_X_init" where the 5879251883Speter** X is consists of the lower-case equivalent of all ASCII alphabetic 5880251883Speter** characters in the filename from the last "/" to the first following 5881251883Speter** "." and omitting any initial "lib".)^ 5882251883Speter** ^The sqlite3_load_extension() interface returns 5883251883Speter** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. 5884251883Speter** ^If an error occurs and pzErrMsg is not 0, then the 5885251883Speter** [sqlite3_load_extension()] interface shall attempt to 5886251883Speter** fill *pzErrMsg with error message text stored in memory 5887251883Speter** obtained from [sqlite3_malloc()]. The calling function 5888251883Speter** should free this memory by calling [sqlite3_free()]. 5889251883Speter** 5890251883Speter** ^Extension loading must be enabled using 5891305002Scy** [sqlite3_enable_load_extension()] or 5892305002Scy** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL) 5893305002Scy** prior to calling this API, 5894251883Speter** otherwise an error will be returned. 5895251883Speter** 5896305002Scy** <b>Security warning:</b> It is recommended that the 5897305002Scy** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this 5898305002Scy** interface. The use of the [sqlite3_enable_load_extension()] interface 5899305002Scy** should be avoided. This will keep the SQL function [load_extension()] 5900305002Scy** disabled and prevent SQL injections from giving attackers 5901305002Scy** access to extension loading capabilities. 5902305002Scy** 5903251883Speter** See also the [load_extension() SQL function]. 5904251883Speter*/ 5905322444SpeterSQLITE_API int sqlite3_load_extension( 5906251883Speter sqlite3 *db, /* Load the extension into this database connection */ 5907251883Speter const char *zFile, /* Name of the shared library containing extension */ 5908251883Speter const char *zProc, /* Entry point. Derived from zFile if 0 */ 5909251883Speter char **pzErrMsg /* Put error message here if not 0 */ 5910251883Speter); 5911251883Speter 5912251883Speter/* 5913251883Speter** CAPI3REF: Enable Or Disable Extension Loading 5914286510Speter** METHOD: sqlite3 5915251883Speter** 5916251883Speter** ^So as not to open security holes in older applications that are 5917251883Speter** unprepared to deal with [extension loading], and as a means of disabling 5918251883Speter** [extension loading] while evaluating user-entered SQL, the following API 5919251883Speter** is provided to turn the [sqlite3_load_extension()] mechanism on and off. 5920251883Speter** 5921251883Speter** ^Extension loading is off by default. 5922251883Speter** ^Call the sqlite3_enable_load_extension() routine with onoff==1 5923251883Speter** to turn extension loading on and call it with onoff==0 to turn 5924251883Speter** it back off again. 5925305002Scy** 5926305002Scy** ^This interface enables or disables both the C-API 5927305002Scy** [sqlite3_load_extension()] and the SQL function [load_extension()]. 5928305002Scy** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..) 5929305002Scy** to enable or disable only the C-API.)^ 5930305002Scy** 5931305002Scy** <b>Security warning:</b> It is recommended that extension loading 5932305002Scy** be disabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method 5933305002Scy** rather than this interface, so the [load_extension()] SQL function 5934305002Scy** remains disabled. This will prevent SQL injections from giving attackers 5935305002Scy** access to extension loading capabilities. 5936251883Speter*/ 5937322444SpeterSQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff); 5938251883Speter 5939251883Speter/* 5940251883Speter** CAPI3REF: Automatically Load Statically Linked Extensions 5941251883Speter** 5942251883Speter** ^This interface causes the xEntryPoint() function to be invoked for 5943251883Speter** each new [database connection] that is created. The idea here is that 5944251883Speter** xEntryPoint() is the entry point for a statically linked [SQLite extension] 5945251883Speter** that is to be automatically loaded into all new database connections. 5946251883Speter** 5947251883Speter** ^(Even though the function prototype shows that xEntryPoint() takes 5948251883Speter** no arguments and returns void, SQLite invokes xEntryPoint() with three 5949305002Scy** arguments and expects an integer result as if the signature of the 5950251883Speter** entry point where as follows: 5951251883Speter** 5952251883Speter** <blockquote><pre> 5953251883Speter** int xEntryPoint( 5954251883Speter** sqlite3 *db, 5955251883Speter** const char **pzErrMsg, 5956251883Speter** const struct sqlite3_api_routines *pThunk 5957251883Speter** ); 5958251883Speter** </pre></blockquote>)^ 5959251883Speter** 5960251883Speter** If the xEntryPoint routine encounters an error, it should make *pzErrMsg 5961251883Speter** point to an appropriate error message (obtained from [sqlite3_mprintf()]) 5962251883Speter** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg 5963251883Speter** is NULL before calling the xEntryPoint(). ^SQLite will invoke 5964251883Speter** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any 5965251883Speter** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()], 5966251883Speter** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail. 5967251883Speter** 5968251883Speter** ^Calling sqlite3_auto_extension(X) with an entry point X that is already 5969251883Speter** on the list of automatic extensions is a harmless no-op. ^No entry point 5970251883Speter** will be called more than once for each database connection that is opened. 5971251883Speter** 5972269851Speter** See also: [sqlite3_reset_auto_extension()] 5973269851Speter** and [sqlite3_cancel_auto_extension()] 5974251883Speter*/ 5975322444SpeterSQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void)); 5976251883Speter 5977251883Speter/* 5978269851Speter** CAPI3REF: Cancel Automatic Extension Loading 5979269851Speter** 5980269851Speter** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the 5981269851Speter** initialization routine X that was registered using a prior call to 5982269851Speter** [sqlite3_auto_extension(X)]. ^The [sqlite3_cancel_auto_extension(X)] 5983269851Speter** routine returns 1 if initialization routine X was successfully 5984269851Speter** unregistered and it returns 0 if X was not on the list of initialization 5985269851Speter** routines. 5986269851Speter*/ 5987322444SpeterSQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void)); 5988269851Speter 5989269851Speter/* 5990251883Speter** CAPI3REF: Reset Automatic Extension Loading 5991251883Speter** 5992251883Speter** ^This interface disables all automatic extensions previously 5993251883Speter** registered using [sqlite3_auto_extension()]. 5994251883Speter*/ 5995322444SpeterSQLITE_API void sqlite3_reset_auto_extension(void); 5996251883Speter 5997251883Speter/* 5998251883Speter** The interface to the virtual-table mechanism is currently considered 5999251883Speter** to be experimental. The interface might change in incompatible ways. 6000251883Speter** If this is a problem for you, do not use the interface at this time. 6001251883Speter** 6002251883Speter** When the virtual-table mechanism stabilizes, we will declare the 6003251883Speter** interface fixed, support it indefinitely, and remove this comment. 6004251883Speter*/ 6005251883Speter 6006251883Speter/* 6007251883Speter** Structures used by the virtual table interface 6008251883Speter*/ 6009251883Spetertypedef struct sqlite3_vtab sqlite3_vtab; 6010251883Spetertypedef struct sqlite3_index_info sqlite3_index_info; 6011251883Spetertypedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; 6012251883Spetertypedef struct sqlite3_module sqlite3_module; 6013251883Speter 6014251883Speter/* 6015251883Speter** CAPI3REF: Virtual Table Object 6016251883Speter** KEYWORDS: sqlite3_module {virtual table module} 6017251883Speter** 6018251883Speter** This structure, sometimes called a "virtual table module", 6019251883Speter** defines the implementation of a [virtual tables]. 6020251883Speter** This structure consists mostly of methods for the module. 6021251883Speter** 6022251883Speter** ^A virtual table module is created by filling in a persistent 6023251883Speter** instance of this structure and passing a pointer to that instance 6024251883Speter** to [sqlite3_create_module()] or [sqlite3_create_module_v2()]. 6025251883Speter** ^The registration remains valid until it is replaced by a different 6026251883Speter** module or until the [database connection] closes. The content 6027251883Speter** of this structure must not change while it is registered with 6028251883Speter** any database connection. 6029251883Speter*/ 6030251883Speterstruct sqlite3_module { 6031251883Speter int iVersion; 6032251883Speter int (*xCreate)(sqlite3*, void *pAux, 6033251883Speter int argc, const char *const*argv, 6034251883Speter sqlite3_vtab **ppVTab, char**); 6035251883Speter int (*xConnect)(sqlite3*, void *pAux, 6036251883Speter int argc, const char *const*argv, 6037251883Speter sqlite3_vtab **ppVTab, char**); 6038251883Speter int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); 6039251883Speter int (*xDisconnect)(sqlite3_vtab *pVTab); 6040251883Speter int (*xDestroy)(sqlite3_vtab *pVTab); 6041251883Speter int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); 6042251883Speter int (*xClose)(sqlite3_vtab_cursor*); 6043251883Speter int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, 6044251883Speter int argc, sqlite3_value **argv); 6045251883Speter int (*xNext)(sqlite3_vtab_cursor*); 6046251883Speter int (*xEof)(sqlite3_vtab_cursor*); 6047251883Speter int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); 6048251883Speter int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); 6049251883Speter int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); 6050251883Speter int (*xBegin)(sqlite3_vtab *pVTab); 6051251883Speter int (*xSync)(sqlite3_vtab *pVTab); 6052251883Speter int (*xCommit)(sqlite3_vtab *pVTab); 6053251883Speter int (*xRollback)(sqlite3_vtab *pVTab); 6054251883Speter int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, 6055251883Speter void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), 6056251883Speter void **ppArg); 6057251883Speter int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); 6058251883Speter /* The methods above are in version 1 of the sqlite_module object. Those 6059251883Speter ** below are for version 2 and greater. */ 6060251883Speter int (*xSavepoint)(sqlite3_vtab *pVTab, int); 6061251883Speter int (*xRelease)(sqlite3_vtab *pVTab, int); 6062251883Speter int (*xRollbackTo)(sqlite3_vtab *pVTab, int); 6063251883Speter}; 6064251883Speter 6065251883Speter/* 6066251883Speter** CAPI3REF: Virtual Table Indexing Information 6067251883Speter** KEYWORDS: sqlite3_index_info 6068251883Speter** 6069251883Speter** The sqlite3_index_info structure and its substructures is used as part 6070251883Speter** of the [virtual table] interface to 6071251883Speter** pass information into and receive the reply from the [xBestIndex] 6072251883Speter** method of a [virtual table module]. The fields under **Inputs** are the 6073251883Speter** inputs to xBestIndex and are read-only. xBestIndex inserts its 6074251883Speter** results into the **Outputs** fields. 6075251883Speter** 6076251883Speter** ^(The aConstraint[] array records WHERE clause constraints of the form: 6077251883Speter** 6078251883Speter** <blockquote>column OP expr</blockquote> 6079251883Speter** 6080251883Speter** where OP is =, <, <=, >, or >=.)^ ^(The particular operator is 6081251883Speter** stored in aConstraint[].op using one of the 6082251883Speter** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^ 6083251883Speter** ^(The index of the column is stored in 6084251883Speter** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the 6085251883Speter** expr on the right-hand side can be evaluated (and thus the constraint 6086251883Speter** is usable) and false if it cannot.)^ 6087251883Speter** 6088251883Speter** ^The optimizer automatically inverts terms of the form "expr OP column" 6089251883Speter** and makes other simplifications to the WHERE clause in an attempt to 6090251883Speter** get as many WHERE clause terms into the form shown above as possible. 6091251883Speter** ^The aConstraint[] array only reports WHERE clause terms that are 6092251883Speter** relevant to the particular virtual table being queried. 6093251883Speter** 6094251883Speter** ^Information about the ORDER BY clause is stored in aOrderBy[]. 6095251883Speter** ^Each term of aOrderBy records a column of the ORDER BY clause. 6096251883Speter** 6097298161Sbapt** The colUsed field indicates which columns of the virtual table may be 6098298161Sbapt** required by the current scan. Virtual table columns are numbered from 6099298161Sbapt** zero in the order in which they appear within the CREATE TABLE statement 6100298161Sbapt** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62), 6101298161Sbapt** the corresponding bit is set within the colUsed mask if the column may be 6102298161Sbapt** required by SQLite. If the table has at least 64 columns and any column 6103298161Sbapt** to the right of the first 63 is required, then bit 63 of colUsed is also 6104298161Sbapt** set. In other words, column iCol may be required if the expression 6105298161Sbapt** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to 6106298161Sbapt** non-zero. 6107298161Sbapt** 6108251883Speter** The [xBestIndex] method must fill aConstraintUsage[] with information 6109251883Speter** about what parameters to pass to xFilter. ^If argvIndex>0 then 6110251883Speter** the right-hand side of the corresponding aConstraint[] is evaluated 6111251883Speter** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit 6112251883Speter** is true, then the constraint is assumed to be fully handled by the 6113251883Speter** virtual table and is not checked again by SQLite.)^ 6114251883Speter** 6115251883Speter** ^The idxNum and idxPtr values are recorded and passed into the 6116251883Speter** [xFilter] method. 6117251883Speter** ^[sqlite3_free()] is used to free idxPtr if and only if 6118251883Speter** needToFreeIdxPtr is true. 6119251883Speter** 6120251883Speter** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in 6121251883Speter** the correct order to satisfy the ORDER BY clause so that no separate 6122251883Speter** sorting step is required. 6123251883Speter** 6124269851Speter** ^The estimatedCost value is an estimate of the cost of a particular 6125269851Speter** strategy. A cost of N indicates that the cost of the strategy is similar 6126269851Speter** to a linear scan of an SQLite table with N rows. A cost of log(N) 6127269851Speter** indicates that the expense of the operation is similar to that of a 6128269851Speter** binary search on a unique indexed field of an SQLite table with N rows. 6129269851Speter** 6130269851Speter** ^The estimatedRows value is an estimate of the number of rows that 6131269851Speter** will be returned by the strategy. 6132269851Speter** 6133298161Sbapt** The xBestIndex method may optionally populate the idxFlags field with a 6134298161Sbapt** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag - 6135298161Sbapt** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite 6136298161Sbapt** assumes that the strategy may visit at most one row. 6137298161Sbapt** 6138298161Sbapt** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then 6139298161Sbapt** SQLite also assumes that if a call to the xUpdate() method is made as 6140298161Sbapt** part of the same statement to delete or update a virtual table row and the 6141298161Sbapt** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback 6142298161Sbapt** any database changes. In other words, if the xUpdate() returns 6143298161Sbapt** SQLITE_CONSTRAINT, the database contents must be exactly as they were 6144298161Sbapt** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not 6145298161Sbapt** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by 6146298161Sbapt** the xUpdate method are automatically rolled back by SQLite. 6147298161Sbapt** 6148269851Speter** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info 6149322444Speter** structure for SQLite [version 3.8.2] ([dateof:3.8.2]). 6150322444Speter** If a virtual table extension is 6151269851Speter** used with an SQLite version earlier than 3.8.2, the results of attempting 6152269851Speter** to read or write the estimatedRows field are undefined (but are likely 6153269851Speter** to included crashing the application). The estimatedRows field should 6154269851Speter** therefore only be used if [sqlite3_libversion_number()] returns a 6155298161Sbapt** value greater than or equal to 3008002. Similarly, the idxFlags field 6156322444Speter** was added for [version 3.9.0] ([dateof:3.9.0]). 6157322444Speter** It may therefore only be used if 6158298161Sbapt** sqlite3_libversion_number() returns a value greater than or equal to 6159298161Sbapt** 3009000. 6160251883Speter*/ 6161251883Speterstruct sqlite3_index_info { 6162251883Speter /* Inputs */ 6163251883Speter int nConstraint; /* Number of entries in aConstraint */ 6164251883Speter struct sqlite3_index_constraint { 6165298161Sbapt int iColumn; /* Column constrained. -1 for ROWID */ 6166251883Speter unsigned char op; /* Constraint operator */ 6167251883Speter unsigned char usable; /* True if this constraint is usable */ 6168251883Speter int iTermOffset; /* Used internally - xBestIndex should ignore */ 6169251883Speter } *aConstraint; /* Table of WHERE clause constraints */ 6170251883Speter int nOrderBy; /* Number of terms in the ORDER BY clause */ 6171251883Speter struct sqlite3_index_orderby { 6172251883Speter int iColumn; /* Column number */ 6173251883Speter unsigned char desc; /* True for DESC. False for ASC. */ 6174251883Speter } *aOrderBy; /* The ORDER BY clause */ 6175251883Speter /* Outputs */ 6176251883Speter struct sqlite3_index_constraint_usage { 6177251883Speter int argvIndex; /* if >0, constraint is part of argv to xFilter */ 6178251883Speter unsigned char omit; /* Do not code a test for this constraint */ 6179251883Speter } *aConstraintUsage; 6180251883Speter int idxNum; /* Number used to identify the index */ 6181251883Speter char *idxStr; /* String, possibly obtained from sqlite3_malloc */ 6182251883Speter int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ 6183251883Speter int orderByConsumed; /* True if output is already ordered */ 6184269851Speter double estimatedCost; /* Estimated cost of using this index */ 6185269851Speter /* Fields below are only available in SQLite 3.8.2 and later */ 6186269851Speter sqlite3_int64 estimatedRows; /* Estimated number of rows returned */ 6187298161Sbapt /* Fields below are only available in SQLite 3.9.0 and later */ 6188298161Sbapt int idxFlags; /* Mask of SQLITE_INDEX_SCAN_* flags */ 6189298161Sbapt /* Fields below are only available in SQLite 3.10.0 and later */ 6190298161Sbapt sqlite3_uint64 colUsed; /* Input: Mask of columns used by statement */ 6191251883Speter}; 6192251883Speter 6193251883Speter/* 6194298161Sbapt** CAPI3REF: Virtual Table Scan Flags 6195298161Sbapt*/ 6196298161Sbapt#define SQLITE_INDEX_SCAN_UNIQUE 1 /* Scan visits at most 1 row */ 6197298161Sbapt 6198298161Sbapt/* 6199251883Speter** CAPI3REF: Virtual Table Constraint Operator Codes 6200251883Speter** 6201251883Speter** These macros defined the allowed values for the 6202251883Speter** [sqlite3_index_info].aConstraint[].op field. Each value represents 6203251883Speter** an operator that is part of a constraint term in the wHERE clause of 6204251883Speter** a query that uses a [virtual table]. 6205251883Speter*/ 6206298161Sbapt#define SQLITE_INDEX_CONSTRAINT_EQ 2 6207298161Sbapt#define SQLITE_INDEX_CONSTRAINT_GT 4 6208298161Sbapt#define SQLITE_INDEX_CONSTRAINT_LE 8 6209298161Sbapt#define SQLITE_INDEX_CONSTRAINT_LT 16 6210298161Sbapt#define SQLITE_INDEX_CONSTRAINT_GE 32 6211298161Sbapt#define SQLITE_INDEX_CONSTRAINT_MATCH 64 6212298161Sbapt#define SQLITE_INDEX_CONSTRAINT_LIKE 65 6213298161Sbapt#define SQLITE_INDEX_CONSTRAINT_GLOB 66 6214298161Sbapt#define SQLITE_INDEX_CONSTRAINT_REGEXP 67 6215251883Speter 6216251883Speter/* 6217251883Speter** CAPI3REF: Register A Virtual Table Implementation 6218286510Speter** METHOD: sqlite3 6219251883Speter** 6220251883Speter** ^These routines are used to register a new [virtual table module] name. 6221251883Speter** ^Module names must be registered before 6222251883Speter** creating a new [virtual table] using the module and before using a 6223251883Speter** preexisting [virtual table] for the module. 6224251883Speter** 6225251883Speter** ^The module name is registered on the [database connection] specified 6226251883Speter** by the first parameter. ^The name of the module is given by the 6227251883Speter** second parameter. ^The third parameter is a pointer to 6228251883Speter** the implementation of the [virtual table module]. ^The fourth 6229251883Speter** parameter is an arbitrary client data pointer that is passed through 6230251883Speter** into the [xCreate] and [xConnect] methods of the virtual table module 6231251883Speter** when a new virtual table is be being created or reinitialized. 6232251883Speter** 6233251883Speter** ^The sqlite3_create_module_v2() interface has a fifth parameter which 6234251883Speter** is a pointer to a destructor for the pClientData. ^SQLite will 6235251883Speter** invoke the destructor function (if it is not NULL) when SQLite 6236251883Speter** no longer needs the pClientData pointer. ^The destructor will also 6237251883Speter** be invoked if the call to sqlite3_create_module_v2() fails. 6238251883Speter** ^The sqlite3_create_module() 6239251883Speter** interface is equivalent to sqlite3_create_module_v2() with a NULL 6240251883Speter** destructor. 6241251883Speter*/ 6242322444SpeterSQLITE_API int sqlite3_create_module( 6243251883Speter sqlite3 *db, /* SQLite connection to register module with */ 6244251883Speter const char *zName, /* Name of the module */ 6245251883Speter const sqlite3_module *p, /* Methods for the module */ 6246251883Speter void *pClientData /* Client data for xCreate/xConnect */ 6247251883Speter); 6248322444SpeterSQLITE_API int sqlite3_create_module_v2( 6249251883Speter sqlite3 *db, /* SQLite connection to register module with */ 6250251883Speter const char *zName, /* Name of the module */ 6251251883Speter const sqlite3_module *p, /* Methods for the module */ 6252251883Speter void *pClientData, /* Client data for xCreate/xConnect */ 6253251883Speter void(*xDestroy)(void*) /* Module destructor function */ 6254251883Speter); 6255251883Speter 6256251883Speter/* 6257251883Speter** CAPI3REF: Virtual Table Instance Object 6258251883Speter** KEYWORDS: sqlite3_vtab 6259251883Speter** 6260251883Speter** Every [virtual table module] implementation uses a subclass 6261251883Speter** of this object to describe a particular instance 6262251883Speter** of the [virtual table]. Each subclass will 6263251883Speter** be tailored to the specific needs of the module implementation. 6264251883Speter** The purpose of this superclass is to define certain fields that are 6265251883Speter** common to all module implementations. 6266251883Speter** 6267251883Speter** ^Virtual tables methods can set an error message by assigning a 6268251883Speter** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should 6269251883Speter** take care that any prior string is freed by a call to [sqlite3_free()] 6270251883Speter** prior to assigning a new string to zErrMsg. ^After the error message 6271251883Speter** is delivered up to the client application, the string will be automatically 6272251883Speter** freed by sqlite3_free() and the zErrMsg field will be zeroed. 6273251883Speter*/ 6274251883Speterstruct sqlite3_vtab { 6275251883Speter const sqlite3_module *pModule; /* The module for this virtual table */ 6276282328Sbapt int nRef; /* Number of open cursors */ 6277251883Speter char *zErrMsg; /* Error message from sqlite3_mprintf() */ 6278251883Speter /* Virtual table implementations will typically add additional fields */ 6279251883Speter}; 6280251883Speter 6281251883Speter/* 6282251883Speter** CAPI3REF: Virtual Table Cursor Object 6283251883Speter** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor} 6284251883Speter** 6285251883Speter** Every [virtual table module] implementation uses a subclass of the 6286251883Speter** following structure to describe cursors that point into the 6287251883Speter** [virtual table] and are used 6288251883Speter** to loop through the virtual table. Cursors are created using the 6289251883Speter** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed 6290251883Speter** by the [sqlite3_module.xClose | xClose] method. Cursors are used 6291251883Speter** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods 6292251883Speter** of the module. Each module implementation will define 6293251883Speter** the content of a cursor structure to suit its own needs. 6294251883Speter** 6295251883Speter** This superclass exists in order to define fields of the cursor that 6296251883Speter** are common to all implementations. 6297251883Speter*/ 6298251883Speterstruct sqlite3_vtab_cursor { 6299251883Speter sqlite3_vtab *pVtab; /* Virtual table of this cursor */ 6300251883Speter /* Virtual table implementations will typically add additional fields */ 6301251883Speter}; 6302251883Speter 6303251883Speter/* 6304251883Speter** CAPI3REF: Declare The Schema Of A Virtual Table 6305251883Speter** 6306251883Speter** ^The [xCreate] and [xConnect] methods of a 6307251883Speter** [virtual table module] call this interface 6308251883Speter** to declare the format (the names and datatypes of the columns) of 6309251883Speter** the virtual tables they implement. 6310251883Speter*/ 6311322444SpeterSQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL); 6312251883Speter 6313251883Speter/* 6314251883Speter** CAPI3REF: Overload A Function For A Virtual Table 6315286510Speter** METHOD: sqlite3 6316251883Speter** 6317251883Speter** ^(Virtual tables can provide alternative implementations of functions 6318251883Speter** using the [xFindFunction] method of the [virtual table module]. 6319251883Speter** But global versions of those functions 6320251883Speter** must exist in order to be overloaded.)^ 6321251883Speter** 6322251883Speter** ^(This API makes sure a global version of a function with a particular 6323251883Speter** name and number of parameters exists. If no such function exists 6324251883Speter** before this API is called, a new function is created.)^ ^The implementation 6325251883Speter** of the new function always causes an exception to be thrown. So 6326251883Speter** the new function is not good for anything by itself. Its only 6327251883Speter** purpose is to be a placeholder function that can be overloaded 6328251883Speter** by a [virtual table]. 6329251883Speter*/ 6330322444SpeterSQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); 6331251883Speter 6332251883Speter/* 6333251883Speter** The interface to the virtual-table mechanism defined above (back up 6334251883Speter** to a comment remarkably similar to this one) is currently considered 6335251883Speter** to be experimental. The interface might change in incompatible ways. 6336251883Speter** If this is a problem for you, do not use the interface at this time. 6337251883Speter** 6338251883Speter** When the virtual-table mechanism stabilizes, we will declare the 6339251883Speter** interface fixed, support it indefinitely, and remove this comment. 6340251883Speter*/ 6341251883Speter 6342251883Speter/* 6343251883Speter** CAPI3REF: A Handle To An Open BLOB 6344251883Speter** KEYWORDS: {BLOB handle} {BLOB handles} 6345251883Speter** 6346251883Speter** An instance of this object represents an open BLOB on which 6347251883Speter** [sqlite3_blob_open | incremental BLOB I/O] can be performed. 6348251883Speter** ^Objects of this type are created by [sqlite3_blob_open()] 6349251883Speter** and destroyed by [sqlite3_blob_close()]. 6350251883Speter** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces 6351251883Speter** can be used to read or write small subsections of the BLOB. 6352251883Speter** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes. 6353251883Speter*/ 6354251883Spetertypedef struct sqlite3_blob sqlite3_blob; 6355251883Speter 6356251883Speter/* 6357251883Speter** CAPI3REF: Open A BLOB For Incremental I/O 6358286510Speter** METHOD: sqlite3 6359286510Speter** CONSTRUCTOR: sqlite3_blob 6360251883Speter** 6361251883Speter** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located 6362251883Speter** in row iRow, column zColumn, table zTable in database zDb; 6363251883Speter** in other words, the same BLOB that would be selected by: 6364251883Speter** 6365251883Speter** <pre> 6366251883Speter** SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow; 6367251883Speter** </pre>)^ 6368251883Speter** 6369282328Sbapt** ^(Parameter zDb is not the filename that contains the database, but 6370282328Sbapt** rather the symbolic name of the database. For attached databases, this is 6371282328Sbapt** the name that appears after the AS keyword in the [ATTACH] statement. 6372282328Sbapt** For the main database file, the database name is "main". For TEMP 6373282328Sbapt** tables, the database name is "temp".)^ 6374282328Sbapt** 6375251883Speter** ^If the flags parameter is non-zero, then the BLOB is opened for read 6376282328Sbapt** and write access. ^If the flags parameter is zero, the BLOB is opened for 6377282328Sbapt** read-only access. 6378251883Speter** 6379282328Sbapt** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored 6380282328Sbapt** in *ppBlob. Otherwise an [error code] is returned and, unless the error 6381282328Sbapt** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided 6382282328Sbapt** the API is not misused, it is always safe to call [sqlite3_blob_close()] 6383282328Sbapt** on *ppBlob after this function it returns. 6384251883Speter** 6385282328Sbapt** This function fails with SQLITE_ERROR if any of the following are true: 6386282328Sbapt** <ul> 6387282328Sbapt** <li> ^(Database zDb does not exist)^, 6388282328Sbapt** <li> ^(Table zTable does not exist within database zDb)^, 6389282328Sbapt** <li> ^(Table zTable is a WITHOUT ROWID table)^, 6390282328Sbapt** <li> ^(Column zColumn does not exist)^, 6391282328Sbapt** <li> ^(Row iRow is not present in the table)^, 6392282328Sbapt** <li> ^(The specified column of row iRow contains a value that is not 6393282328Sbapt** a TEXT or BLOB value)^, 6394282328Sbapt** <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE 6395282328Sbapt** constraint and the blob is being opened for read/write access)^, 6396282328Sbapt** <li> ^([foreign key constraints | Foreign key constraints] are enabled, 6397282328Sbapt** column zColumn is part of a [child key] definition and the blob is 6398282328Sbapt** being opened for read/write access)^. 6399282328Sbapt** </ul> 6400251883Speter** 6401282328Sbapt** ^Unless it returns SQLITE_MISUSE, this function sets the 6402282328Sbapt** [database connection] error code and message accessible via 6403282328Sbapt** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. 6404282328Sbapt** 6405322444Speter** A BLOB referenced by sqlite3_blob_open() may be read using the 6406322444Speter** [sqlite3_blob_read()] interface and modified by using 6407322444Speter** [sqlite3_blob_write()]. The [BLOB handle] can be moved to a 6408322444Speter** different row of the same table using the [sqlite3_blob_reopen()] 6409322444Speter** interface. However, the column, table, or database of a [BLOB handle] 6410322444Speter** cannot be changed after the [BLOB handle] is opened. 6411282328Sbapt** 6412251883Speter** ^(If the row that a BLOB handle points to is modified by an 6413251883Speter** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects 6414251883Speter** then the BLOB handle is marked as "expired". 6415251883Speter** This is true if any column of the row is changed, even a column 6416251883Speter** other than the one the BLOB handle is open on.)^ 6417251883Speter** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for 6418251883Speter** an expired BLOB handle fail with a return code of [SQLITE_ABORT]. 6419251883Speter** ^(Changes written into a BLOB prior to the BLOB expiring are not 6420251883Speter** rolled back by the expiration of the BLOB. Such changes will eventually 6421251883Speter** commit if the transaction continues to completion.)^ 6422251883Speter** 6423251883Speter** ^Use the [sqlite3_blob_bytes()] interface to determine the size of 6424251883Speter** the opened blob. ^The size of a blob may not be changed by this 6425251883Speter** interface. Use the [UPDATE] SQL command to change the size of a 6426251883Speter** blob. 6427251883Speter** 6428251883Speter** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces 6429282328Sbapt** and the built-in [zeroblob] SQL function may be used to create a 6430282328Sbapt** zero-filled blob to read or write using the incremental-blob interface. 6431251883Speter** 6432251883Speter** To avoid a resource leak, every open [BLOB handle] should eventually 6433251883Speter** be released by a call to [sqlite3_blob_close()]. 6434322444Speter** 6435322444Speter** See also: [sqlite3_blob_close()], 6436322444Speter** [sqlite3_blob_reopen()], [sqlite3_blob_read()], 6437322444Speter** [sqlite3_blob_bytes()], [sqlite3_blob_write()]. 6438251883Speter*/ 6439322444SpeterSQLITE_API int sqlite3_blob_open( 6440251883Speter sqlite3*, 6441251883Speter const char *zDb, 6442251883Speter const char *zTable, 6443251883Speter const char *zColumn, 6444251883Speter sqlite3_int64 iRow, 6445251883Speter int flags, 6446251883Speter sqlite3_blob **ppBlob 6447251883Speter); 6448251883Speter 6449251883Speter/* 6450251883Speter** CAPI3REF: Move a BLOB Handle to a New Row 6451286510Speter** METHOD: sqlite3_blob 6452251883Speter** 6453322444Speter** ^This function is used to move an existing [BLOB handle] so that it points 6454251883Speter** to a different row of the same database table. ^The new row is identified 6455251883Speter** by the rowid value passed as the second argument. Only the row can be 6456251883Speter** changed. ^The database, table and column on which the blob handle is open 6457322444Speter** remain the same. Moving an existing [BLOB handle] to a new row is 6458251883Speter** faster than closing the existing handle and opening a new one. 6459251883Speter** 6460251883Speter** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] - 6461251883Speter** it must exist and there must be either a blob or text value stored in 6462251883Speter** the nominated column.)^ ^If the new row is not present in the table, or if 6463251883Speter** it does not contain a blob or text value, or if another error occurs, an 6464251883Speter** SQLite error code is returned and the blob handle is considered aborted. 6465251883Speter** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or 6466251883Speter** [sqlite3_blob_reopen()] on an aborted blob handle immediately return 6467251883Speter** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle 6468251883Speter** always returns zero. 6469251883Speter** 6470251883Speter** ^This function sets the database handle error code and message. 6471251883Speter*/ 6472322444SpeterSQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); 6473251883Speter 6474251883Speter/* 6475251883Speter** CAPI3REF: Close A BLOB Handle 6476286510Speter** DESTRUCTOR: sqlite3_blob 6477251883Speter** 6478282328Sbapt** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed 6479282328Sbapt** unconditionally. Even if this routine returns an error code, the 6480282328Sbapt** handle is still closed.)^ 6481251883Speter** 6482282328Sbapt** ^If the blob handle being closed was opened for read-write access, and if 6483282328Sbapt** the database is in auto-commit mode and there are no other open read-write 6484282328Sbapt** blob handles or active write statements, the current transaction is 6485282328Sbapt** committed. ^If an error occurs while committing the transaction, an error 6486282328Sbapt** code is returned and the transaction rolled back. 6487251883Speter** 6488282328Sbapt** Calling this function with an argument that is not a NULL pointer or an 6489282328Sbapt** open blob handle results in undefined behaviour. ^Calling this routine 6490282328Sbapt** with a null pointer (such as would be returned by a failed call to 6491282328Sbapt** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function 6492282328Sbapt** is passed a valid open blob handle, the values returned by the 6493282328Sbapt** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning. 6494251883Speter*/ 6495322444SpeterSQLITE_API int sqlite3_blob_close(sqlite3_blob *); 6496251883Speter 6497251883Speter/* 6498251883Speter** CAPI3REF: Return The Size Of An Open BLOB 6499286510Speter** METHOD: sqlite3_blob 6500251883Speter** 6501251883Speter** ^Returns the size in bytes of the BLOB accessible via the 6502251883Speter** successfully opened [BLOB handle] in its only argument. ^The 6503251883Speter** incremental blob I/O routines can only read or overwriting existing 6504251883Speter** blob content; they cannot change the size of a blob. 6505251883Speter** 6506251883Speter** This routine only works on a [BLOB handle] which has been created 6507251883Speter** by a prior successful call to [sqlite3_blob_open()] and which has not 6508251883Speter** been closed by [sqlite3_blob_close()]. Passing any other pointer in 6509251883Speter** to this routine results in undefined and probably undesirable behavior. 6510251883Speter*/ 6511322444SpeterSQLITE_API int sqlite3_blob_bytes(sqlite3_blob *); 6512251883Speter 6513251883Speter/* 6514251883Speter** CAPI3REF: Read Data From A BLOB Incrementally 6515286510Speter** METHOD: sqlite3_blob 6516251883Speter** 6517251883Speter** ^(This function is used to read data from an open [BLOB handle] into a 6518251883Speter** caller-supplied buffer. N bytes of data are copied into buffer Z 6519251883Speter** from the open BLOB, starting at offset iOffset.)^ 6520251883Speter** 6521251883Speter** ^If offset iOffset is less than N bytes from the end of the BLOB, 6522251883Speter** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is 6523251883Speter** less than zero, [SQLITE_ERROR] is returned and no data is read. 6524251883Speter** ^The size of the blob (and hence the maximum value of N+iOffset) 6525251883Speter** can be determined using the [sqlite3_blob_bytes()] interface. 6526251883Speter** 6527251883Speter** ^An attempt to read from an expired [BLOB handle] fails with an 6528251883Speter** error code of [SQLITE_ABORT]. 6529251883Speter** 6530251883Speter** ^(On success, sqlite3_blob_read() returns SQLITE_OK. 6531251883Speter** Otherwise, an [error code] or an [extended error code] is returned.)^ 6532251883Speter** 6533251883Speter** This routine only works on a [BLOB handle] which has been created 6534251883Speter** by a prior successful call to [sqlite3_blob_open()] and which has not 6535251883Speter** been closed by [sqlite3_blob_close()]. Passing any other pointer in 6536251883Speter** to this routine results in undefined and probably undesirable behavior. 6537251883Speter** 6538251883Speter** See also: [sqlite3_blob_write()]. 6539251883Speter*/ 6540322444SpeterSQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); 6541251883Speter 6542251883Speter/* 6543251883Speter** CAPI3REF: Write Data Into A BLOB Incrementally 6544286510Speter** METHOD: sqlite3_blob 6545251883Speter** 6546282328Sbapt** ^(This function is used to write data into an open [BLOB handle] from a 6547282328Sbapt** caller-supplied buffer. N bytes of data are copied from the buffer Z 6548282328Sbapt** into the open BLOB, starting at offset iOffset.)^ 6549251883Speter** 6550282328Sbapt** ^(On success, sqlite3_blob_write() returns SQLITE_OK. 6551282328Sbapt** Otherwise, an [error code] or an [extended error code] is returned.)^ 6552282328Sbapt** ^Unless SQLITE_MISUSE is returned, this function sets the 6553282328Sbapt** [database connection] error code and message accessible via 6554282328Sbapt** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions. 6555282328Sbapt** 6556251883Speter** ^If the [BLOB handle] passed as the first argument was not opened for 6557251883Speter** writing (the flags parameter to [sqlite3_blob_open()] was zero), 6558251883Speter** this function returns [SQLITE_READONLY]. 6559251883Speter** 6560282328Sbapt** This function may only modify the contents of the BLOB; it is 6561251883Speter** not possible to increase the size of a BLOB using this API. 6562251883Speter** ^If offset iOffset is less than N bytes from the end of the BLOB, 6563282328Sbapt** [SQLITE_ERROR] is returned and no data is written. The size of the 6564282328Sbapt** BLOB (and hence the maximum value of N+iOffset) can be determined 6565282328Sbapt** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less 6566282328Sbapt** than zero [SQLITE_ERROR] is returned and no data is written. 6567251883Speter** 6568251883Speter** ^An attempt to write to an expired [BLOB handle] fails with an 6569251883Speter** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred 6570251883Speter** before the [BLOB handle] expired are not rolled back by the 6571251883Speter** expiration of the handle, though of course those changes might 6572251883Speter** have been overwritten by the statement that expired the BLOB handle 6573251883Speter** or by other independent statements. 6574251883Speter** 6575251883Speter** This routine only works on a [BLOB handle] which has been created 6576251883Speter** by a prior successful call to [sqlite3_blob_open()] and which has not 6577251883Speter** been closed by [sqlite3_blob_close()]. Passing any other pointer in 6578251883Speter** to this routine results in undefined and probably undesirable behavior. 6579251883Speter** 6580251883Speter** See also: [sqlite3_blob_read()]. 6581251883Speter*/ 6582322444SpeterSQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); 6583251883Speter 6584251883Speter/* 6585251883Speter** CAPI3REF: Virtual File System Objects 6586251883Speter** 6587251883Speter** A virtual filesystem (VFS) is an [sqlite3_vfs] object 6588251883Speter** that SQLite uses to interact 6589251883Speter** with the underlying operating system. Most SQLite builds come with a 6590251883Speter** single default VFS that is appropriate for the host computer. 6591251883Speter** New VFSes can be registered and existing VFSes can be unregistered. 6592251883Speter** The following interfaces are provided. 6593251883Speter** 6594251883Speter** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name. 6595251883Speter** ^Names are case sensitive. 6596251883Speter** ^Names are zero-terminated UTF-8 strings. 6597251883Speter** ^If there is no match, a NULL pointer is returned. 6598251883Speter** ^If zVfsName is NULL then the default VFS is returned. 6599251883Speter** 6600251883Speter** ^New VFSes are registered with sqlite3_vfs_register(). 6601251883Speter** ^Each new VFS becomes the default VFS if the makeDflt flag is set. 6602251883Speter** ^The same VFS can be registered multiple times without injury. 6603251883Speter** ^To make an existing VFS into the default VFS, register it again 6604251883Speter** with the makeDflt flag set. If two different VFSes with the 6605251883Speter** same name are registered, the behavior is undefined. If a 6606251883Speter** VFS is registered with a name that is NULL or an empty string, 6607251883Speter** then the behavior is undefined. 6608251883Speter** 6609251883Speter** ^Unregister a VFS with the sqlite3_vfs_unregister() interface. 6610251883Speter** ^(If the default VFS is unregistered, another VFS is chosen as 6611251883Speter** the default. The choice for the new VFS is arbitrary.)^ 6612251883Speter*/ 6613322444SpeterSQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); 6614322444SpeterSQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); 6615322444SpeterSQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*); 6616251883Speter 6617251883Speter/* 6618251883Speter** CAPI3REF: Mutexes 6619251883Speter** 6620251883Speter** The SQLite core uses these routines for thread 6621251883Speter** synchronization. Though they are intended for internal 6622251883Speter** use by SQLite, code that links against SQLite is 6623251883Speter** permitted to use any of these routines. 6624251883Speter** 6625251883Speter** The SQLite source code contains multiple implementations 6626251883Speter** of these mutex routines. An appropriate implementation 6627282328Sbapt** is selected automatically at compile-time. The following 6628251883Speter** implementations are available in the SQLite core: 6629251883Speter** 6630251883Speter** <ul> 6631251883Speter** <li> SQLITE_MUTEX_PTHREADS 6632251883Speter** <li> SQLITE_MUTEX_W32 6633251883Speter** <li> SQLITE_MUTEX_NOOP 6634282328Sbapt** </ul> 6635251883Speter** 6636282328Sbapt** The SQLITE_MUTEX_NOOP implementation is a set of routines 6637251883Speter** that does no real locking and is appropriate for use in 6638282328Sbapt** a single-threaded application. The SQLITE_MUTEX_PTHREADS and 6639251883Speter** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix 6640251883Speter** and Windows. 6641251883Speter** 6642282328Sbapt** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor 6643251883Speter** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex 6644251883Speter** implementation is included with the library. In this case the 6645251883Speter** application must supply a custom mutex implementation using the 6646251883Speter** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function 6647251883Speter** before calling sqlite3_initialize() or any other public sqlite3_ 6648282328Sbapt** function that calls sqlite3_initialize(). 6649251883Speter** 6650251883Speter** ^The sqlite3_mutex_alloc() routine allocates a new 6651282328Sbapt** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc() 6652282328Sbapt** routine returns NULL if it is unable to allocate the requested 6653282328Sbapt** mutex. The argument to sqlite3_mutex_alloc() must one of these 6654282328Sbapt** integer constants: 6655251883Speter** 6656251883Speter** <ul> 6657251883Speter** <li> SQLITE_MUTEX_FAST 6658251883Speter** <li> SQLITE_MUTEX_RECURSIVE 6659251883Speter** <li> SQLITE_MUTEX_STATIC_MASTER 6660251883Speter** <li> SQLITE_MUTEX_STATIC_MEM 6661274884Sbapt** <li> SQLITE_MUTEX_STATIC_OPEN 6662251883Speter** <li> SQLITE_MUTEX_STATIC_PRNG 6663251883Speter** <li> SQLITE_MUTEX_STATIC_LRU 6664274884Sbapt** <li> SQLITE_MUTEX_STATIC_PMEM 6665274884Sbapt** <li> SQLITE_MUTEX_STATIC_APP1 6666274884Sbapt** <li> SQLITE_MUTEX_STATIC_APP2 6667282328Sbapt** <li> SQLITE_MUTEX_STATIC_APP3 6668298161Sbapt** <li> SQLITE_MUTEX_STATIC_VFS1 6669298161Sbapt** <li> SQLITE_MUTEX_STATIC_VFS2 6670298161Sbapt** <li> SQLITE_MUTEX_STATIC_VFS3 6671282328Sbapt** </ul> 6672251883Speter** 6673251883Speter** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) 6674251883Speter** cause sqlite3_mutex_alloc() to create 6675251883Speter** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE 6676251883Speter** is used but not necessarily so when SQLITE_MUTEX_FAST is used. 6677251883Speter** The mutex implementation does not need to make a distinction 6678251883Speter** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does 6679282328Sbapt** not want to. SQLite will only request a recursive mutex in 6680282328Sbapt** cases where it really needs one. If a faster non-recursive mutex 6681251883Speter** implementation is available on the host platform, the mutex subsystem 6682251883Speter** might return such a mutex in response to SQLITE_MUTEX_FAST. 6683251883Speter** 6684251883Speter** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other 6685251883Speter** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return 6686282328Sbapt** a pointer to a static preexisting mutex. ^Nine static mutexes are 6687251883Speter** used by the current version of SQLite. Future versions of SQLite 6688251883Speter** may add additional static mutexes. Static mutexes are for internal 6689251883Speter** use by SQLite only. Applications that use SQLite mutexes should 6690251883Speter** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or 6691251883Speter** SQLITE_MUTEX_RECURSIVE. 6692251883Speter** 6693251883Speter** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST 6694251883Speter** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() 6695282328Sbapt** returns a different mutex on every call. ^For the static 6696251883Speter** mutex types, the same mutex is returned on every call that has 6697251883Speter** the same type number. 6698251883Speter** 6699251883Speter** ^The sqlite3_mutex_free() routine deallocates a previously 6700282328Sbapt** allocated dynamic mutex. Attempting to deallocate a static 6701282328Sbapt** mutex results in undefined behavior. 6702251883Speter** 6703251883Speter** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt 6704251883Speter** to enter a mutex. ^If another thread is already within the mutex, 6705251883Speter** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return 6706251883Speter** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK] 6707251883Speter** upon successful entry. ^(Mutexes created using 6708251883Speter** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. 6709282328Sbapt** In such cases, the 6710251883Speter** mutex must be exited an equal number of times before another thread 6711282328Sbapt** can enter.)^ If the same thread tries to enter any mutex other 6712282328Sbapt** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined. 6713251883Speter** 6714251883Speter** ^(Some systems (for example, Windows 95) do not support the operation 6715251883Speter** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() 6716282328Sbapt** will always return SQLITE_BUSY. The SQLite core only ever uses 6717282328Sbapt** sqlite3_mutex_try() as an optimization so this is acceptable 6718282328Sbapt** behavior.)^ 6719251883Speter** 6720251883Speter** ^The sqlite3_mutex_leave() routine exits a mutex that was 6721282328Sbapt** previously entered by the same thread. The behavior 6722251883Speter** is undefined if the mutex is not currently entered by the 6723282328Sbapt** calling thread or is not currently allocated. 6724251883Speter** 6725251883Speter** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or 6726251883Speter** sqlite3_mutex_leave() is a NULL pointer, then all three routines 6727251883Speter** behave as no-ops. 6728251883Speter** 6729251883Speter** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. 6730251883Speter*/ 6731322444SpeterSQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int); 6732322444SpeterSQLITE_API void sqlite3_mutex_free(sqlite3_mutex*); 6733322444SpeterSQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*); 6734322444SpeterSQLITE_API int sqlite3_mutex_try(sqlite3_mutex*); 6735322444SpeterSQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*); 6736251883Speter 6737251883Speter/* 6738251883Speter** CAPI3REF: Mutex Methods Object 6739251883Speter** 6740251883Speter** An instance of this structure defines the low-level routines 6741251883Speter** used to allocate and use mutexes. 6742251883Speter** 6743251883Speter** Usually, the default mutex implementations provided by SQLite are 6744282328Sbapt** sufficient, however the application has the option of substituting a custom 6745251883Speter** implementation for specialized deployments or systems for which SQLite 6746282328Sbapt** does not provide a suitable implementation. In this case, the application 6747251883Speter** creates and populates an instance of this structure to pass 6748251883Speter** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. 6749251883Speter** Additionally, an instance of this structure can be used as an 6750251883Speter** output variable when querying the system for the current mutex 6751251883Speter** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. 6752251883Speter** 6753251883Speter** ^The xMutexInit method defined by this structure is invoked as 6754251883Speter** part of system initialization by the sqlite3_initialize() function. 6755251883Speter** ^The xMutexInit routine is called by SQLite exactly once for each 6756251883Speter** effective call to [sqlite3_initialize()]. 6757251883Speter** 6758251883Speter** ^The xMutexEnd method defined by this structure is invoked as 6759251883Speter** part of system shutdown by the sqlite3_shutdown() function. The 6760251883Speter** implementation of this method is expected to release all outstanding 6761251883Speter** resources obtained by the mutex methods implementation, especially 6762251883Speter** those obtained by the xMutexInit method. ^The xMutexEnd() 6763251883Speter** interface is invoked exactly once for each call to [sqlite3_shutdown()]. 6764251883Speter** 6765251883Speter** ^(The remaining seven methods defined by this structure (xMutexAlloc, 6766251883Speter** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and 6767251883Speter** xMutexNotheld) implement the following interfaces (respectively): 6768251883Speter** 6769251883Speter** <ul> 6770251883Speter** <li> [sqlite3_mutex_alloc()] </li> 6771251883Speter** <li> [sqlite3_mutex_free()] </li> 6772251883Speter** <li> [sqlite3_mutex_enter()] </li> 6773251883Speter** <li> [sqlite3_mutex_try()] </li> 6774251883Speter** <li> [sqlite3_mutex_leave()] </li> 6775251883Speter** <li> [sqlite3_mutex_held()] </li> 6776251883Speter** <li> [sqlite3_mutex_notheld()] </li> 6777251883Speter** </ul>)^ 6778251883Speter** 6779251883Speter** The only difference is that the public sqlite3_XXX functions enumerated 6780251883Speter** above silently ignore any invocations that pass a NULL pointer instead 6781251883Speter** of a valid mutex handle. The implementations of the methods defined 6782251883Speter** by this structure are not required to handle this case, the results 6783251883Speter** of passing a NULL pointer instead of a valid mutex handle are undefined 6784251883Speter** (i.e. it is acceptable to provide an implementation that segfaults if 6785251883Speter** it is passed a NULL pointer). 6786251883Speter** 6787282328Sbapt** The xMutexInit() method must be threadsafe. It must be harmless to 6788251883Speter** invoke xMutexInit() multiple times within the same process and without 6789251883Speter** intervening calls to xMutexEnd(). Second and subsequent calls to 6790251883Speter** xMutexInit() must be no-ops. 6791251883Speter** 6792282328Sbapt** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] 6793282328Sbapt** and its associates). Similarly, xMutexAlloc() must not use SQLite memory 6794251883Speter** allocation for a static mutex. ^However xMutexAlloc() may use SQLite 6795251883Speter** memory allocation for a fast or recursive mutex. 6796251883Speter** 6797251883Speter** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is 6798251883Speter** called, but only if the prior call to xMutexInit returned SQLITE_OK. 6799251883Speter** If xMutexInit fails in any way, it is expected to clean up after itself 6800251883Speter** prior to returning. 6801251883Speter*/ 6802251883Spetertypedef struct sqlite3_mutex_methods sqlite3_mutex_methods; 6803251883Speterstruct sqlite3_mutex_methods { 6804251883Speter int (*xMutexInit)(void); 6805251883Speter int (*xMutexEnd)(void); 6806251883Speter sqlite3_mutex *(*xMutexAlloc)(int); 6807251883Speter void (*xMutexFree)(sqlite3_mutex *); 6808251883Speter void (*xMutexEnter)(sqlite3_mutex *); 6809251883Speter int (*xMutexTry)(sqlite3_mutex *); 6810251883Speter void (*xMutexLeave)(sqlite3_mutex *); 6811251883Speter int (*xMutexHeld)(sqlite3_mutex *); 6812251883Speter int (*xMutexNotheld)(sqlite3_mutex *); 6813251883Speter}; 6814251883Speter 6815251883Speter/* 6816251883Speter** CAPI3REF: Mutex Verification Routines 6817251883Speter** 6818251883Speter** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines 6819282328Sbapt** are intended for use inside assert() statements. The SQLite core 6820251883Speter** never uses these routines except inside an assert() and applications 6821282328Sbapt** are advised to follow the lead of the core. The SQLite core only 6822251883Speter** provides implementations for these routines when it is compiled 6823282328Sbapt** with the SQLITE_DEBUG flag. External mutex implementations 6824251883Speter** are only required to provide these routines if SQLITE_DEBUG is 6825251883Speter** defined and if NDEBUG is not defined. 6826251883Speter** 6827282328Sbapt** These routines should return true if the mutex in their argument 6828251883Speter** is held or not held, respectively, by the calling thread. 6829251883Speter** 6830282328Sbapt** The implementation is not required to provide versions of these 6831251883Speter** routines that actually work. If the implementation does not provide working 6832251883Speter** versions of these routines, it should at least provide stubs that always 6833251883Speter** return true so that one does not get spurious assertion failures. 6834251883Speter** 6835282328Sbapt** If the argument to sqlite3_mutex_held() is a NULL pointer then 6836251883Speter** the routine should return 1. This seems counter-intuitive since 6837251883Speter** clearly the mutex cannot be held if it does not exist. But 6838251883Speter** the reason the mutex does not exist is because the build is not 6839251883Speter** using mutexes. And we do not want the assert() containing the 6840251883Speter** call to sqlite3_mutex_held() to fail, so a non-zero return is 6841282328Sbapt** the appropriate thing to do. The sqlite3_mutex_notheld() 6842251883Speter** interface should also return 1 when given a NULL pointer. 6843251883Speter*/ 6844251883Speter#ifndef NDEBUG 6845322444SpeterSQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); 6846322444SpeterSQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); 6847251883Speter#endif 6848251883Speter 6849251883Speter/* 6850251883Speter** CAPI3REF: Mutex Types 6851251883Speter** 6852251883Speter** The [sqlite3_mutex_alloc()] interface takes a single argument 6853251883Speter** which is one of these integer constants. 6854251883Speter** 6855251883Speter** The set of static mutexes may change from one SQLite release to the 6856251883Speter** next. Applications that override the built-in mutex logic must be 6857251883Speter** prepared to accommodate additional static mutexes. 6858251883Speter*/ 6859251883Speter#define SQLITE_MUTEX_FAST 0 6860251883Speter#define SQLITE_MUTEX_RECURSIVE 1 6861251883Speter#define SQLITE_MUTEX_STATIC_MASTER 2 6862251883Speter#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ 6863251883Speter#define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */ 6864251883Speter#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */ 6865322444Speter#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_randomness() */ 6866251883Speter#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ 6867251883Speter#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */ 6868251883Speter#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */ 6869274884Sbapt#define SQLITE_MUTEX_STATIC_APP1 8 /* For use by application */ 6870274884Sbapt#define SQLITE_MUTEX_STATIC_APP2 9 /* For use by application */ 6871274884Sbapt#define SQLITE_MUTEX_STATIC_APP3 10 /* For use by application */ 6872286510Speter#define SQLITE_MUTEX_STATIC_VFS1 11 /* For use by built-in VFS */ 6873286510Speter#define SQLITE_MUTEX_STATIC_VFS2 12 /* For use by extension VFS */ 6874286510Speter#define SQLITE_MUTEX_STATIC_VFS3 13 /* For use by application VFS */ 6875251883Speter 6876251883Speter/* 6877251883Speter** CAPI3REF: Retrieve the mutex for a database connection 6878286510Speter** METHOD: sqlite3 6879251883Speter** 6880251883Speter** ^This interface returns a pointer the [sqlite3_mutex] object that 6881251883Speter** serializes access to the [database connection] given in the argument 6882251883Speter** when the [threading mode] is Serialized. 6883251883Speter** ^If the [threading mode] is Single-thread or Multi-thread then this 6884251883Speter** routine returns a NULL pointer. 6885251883Speter*/ 6886322444SpeterSQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*); 6887251883Speter 6888251883Speter/* 6889251883Speter** CAPI3REF: Low-Level Control Of Database Files 6890286510Speter** METHOD: sqlite3 6891251883Speter** 6892251883Speter** ^The [sqlite3_file_control()] interface makes a direct call to the 6893251883Speter** xFileControl method for the [sqlite3_io_methods] object associated 6894251883Speter** with a particular database identified by the second argument. ^The 6895251883Speter** name of the database is "main" for the main database or "temp" for the 6896251883Speter** TEMP database, or the name that appears after the AS keyword for 6897251883Speter** databases that are added using the [ATTACH] SQL command. 6898251883Speter** ^A NULL pointer can be used in place of "main" to refer to the 6899251883Speter** main database file. 6900251883Speter** ^The third and fourth parameters to this routine 6901251883Speter** are passed directly through to the second and third parameters of 6902251883Speter** the xFileControl method. ^The return value of the xFileControl 6903251883Speter** method becomes the return value of this routine. 6904251883Speter** 6905251883Speter** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes 6906251883Speter** a pointer to the underlying [sqlite3_file] object to be written into 6907251883Speter** the space pointed to by the 4th parameter. ^The SQLITE_FCNTL_FILE_POINTER 6908251883Speter** case is a short-circuit path which does not actually invoke the 6909251883Speter** underlying sqlite3_io_methods.xFileControl method. 6910251883Speter** 6911251883Speter** ^If the second parameter (zDbName) does not match the name of any 6912251883Speter** open database file, then SQLITE_ERROR is returned. ^This error 6913251883Speter** code is not remembered and will not be recalled by [sqlite3_errcode()] 6914251883Speter** or [sqlite3_errmsg()]. The underlying xFileControl method might 6915251883Speter** also return SQLITE_ERROR. There is no way to distinguish between 6916251883Speter** an incorrect zDbName and an SQLITE_ERROR return from the underlying 6917251883Speter** xFileControl method. 6918251883Speter** 6919251883Speter** See also: [SQLITE_FCNTL_LOCKSTATE] 6920251883Speter*/ 6921322444SpeterSQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); 6922251883Speter 6923251883Speter/* 6924251883Speter** CAPI3REF: Testing Interface 6925251883Speter** 6926251883Speter** ^The sqlite3_test_control() interface is used to read out internal 6927251883Speter** state of SQLite and to inject faults into SQLite for testing 6928251883Speter** purposes. ^The first parameter is an operation code that determines 6929251883Speter** the number, meaning, and operation of all subsequent parameters. 6930251883Speter** 6931251883Speter** This interface is not for use by applications. It exists solely 6932251883Speter** for verifying the correct operation of the SQLite library. Depending 6933251883Speter** on how the SQLite library is compiled, this interface might not exist. 6934251883Speter** 6935251883Speter** The details of the operation codes, their meanings, the parameters 6936251883Speter** they take, and what they do are all subject to change without notice. 6937251883Speter** Unlike most of the SQLite API, this function is not guaranteed to 6938251883Speter** operate consistently from one release to the next. 6939251883Speter*/ 6940322444SpeterSQLITE_API int sqlite3_test_control(int op, ...); 6941251883Speter 6942251883Speter/* 6943251883Speter** CAPI3REF: Testing Interface Operation Codes 6944251883Speter** 6945251883Speter** These constants are the valid operation code parameters used 6946251883Speter** as the first argument to [sqlite3_test_control()]. 6947251883Speter** 6948251883Speter** These parameters and their meanings are subject to change 6949251883Speter** without notice. These values are for testing purposes only. 6950251883Speter** Applications should not use any of these parameters or the 6951251883Speter** [sqlite3_test_control()] interface. 6952251883Speter*/ 6953251883Speter#define SQLITE_TESTCTRL_FIRST 5 6954251883Speter#define SQLITE_TESTCTRL_PRNG_SAVE 5 6955251883Speter#define SQLITE_TESTCTRL_PRNG_RESTORE 6 6956251883Speter#define SQLITE_TESTCTRL_PRNG_RESET 7 6957251883Speter#define SQLITE_TESTCTRL_BITVEC_TEST 8 6958251883Speter#define SQLITE_TESTCTRL_FAULT_INSTALL 9 6959251883Speter#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10 6960251883Speter#define SQLITE_TESTCTRL_PENDING_BYTE 11 6961251883Speter#define SQLITE_TESTCTRL_ASSERT 12 6962251883Speter#define SQLITE_TESTCTRL_ALWAYS 13 6963251883Speter#define SQLITE_TESTCTRL_RESERVE 14 6964251883Speter#define SQLITE_TESTCTRL_OPTIMIZATIONS 15 6965251883Speter#define SQLITE_TESTCTRL_ISKEYWORD 16 6966251883Speter#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 6967251883Speter#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18 6968274884Sbapt#define SQLITE_TESTCTRL_EXPLAIN_STMT 19 /* NOT USED */ 6969322444Speter#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD 19 6970269851Speter#define SQLITE_TESTCTRL_NEVER_CORRUPT 20 6971269851Speter#define SQLITE_TESTCTRL_VDBE_COVERAGE 21 6972269851Speter#define SQLITE_TESTCTRL_BYTEORDER 22 6973274884Sbapt#define SQLITE_TESTCTRL_ISINIT 23 6974274884Sbapt#define SQLITE_TESTCTRL_SORTER_MMAP 24 6975282328Sbapt#define SQLITE_TESTCTRL_IMPOSTER 25 6976282328Sbapt#define SQLITE_TESTCTRL_LAST 25 6977251883Speter 6978251883Speter/* 6979251883Speter** CAPI3REF: SQLite Runtime Status 6980251883Speter** 6981282328Sbapt** ^These interfaces are used to retrieve runtime status information 6982251883Speter** about the performance of SQLite, and optionally to reset various 6983251883Speter** highwater marks. ^The first argument is an integer code for 6984251883Speter** the specific parameter to measure. ^(Recognized integer codes 6985251883Speter** are of the form [status parameters | SQLITE_STATUS_...].)^ 6986251883Speter** ^The current value of the parameter is returned into *pCurrent. 6987251883Speter** ^The highest recorded value is returned in *pHighwater. ^If the 6988251883Speter** resetFlag is true, then the highest record value is reset after 6989251883Speter** *pHighwater is written. ^(Some parameters do not record the highest 6990251883Speter** value. For those parameters 6991251883Speter** nothing is written into *pHighwater and the resetFlag is ignored.)^ 6992251883Speter** ^(Other parameters record only the highwater mark and not the current 6993251883Speter** value. For these latter parameters nothing is written into *pCurrent.)^ 6994251883Speter** 6995282328Sbapt** ^The sqlite3_status() and sqlite3_status64() routines return 6996282328Sbapt** SQLITE_OK on success and a non-zero [error code] on failure. 6997251883Speter** 6998282328Sbapt** If either the current value or the highwater mark is too large to 6999282328Sbapt** be represented by a 32-bit integer, then the values returned by 7000282328Sbapt** sqlite3_status() are undefined. 7001251883Speter** 7002251883Speter** See also: [sqlite3_db_status()] 7003251883Speter*/ 7004322444SpeterSQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); 7005322444SpeterSQLITE_API int sqlite3_status64( 7006282328Sbapt int op, 7007282328Sbapt sqlite3_int64 *pCurrent, 7008282328Sbapt sqlite3_int64 *pHighwater, 7009282328Sbapt int resetFlag 7010282328Sbapt); 7011251883Speter 7012251883Speter 7013251883Speter/* 7014251883Speter** CAPI3REF: Status Parameters 7015251883Speter** KEYWORDS: {status parameters} 7016251883Speter** 7017251883Speter** These integer constants designate various run-time status parameters 7018251883Speter** that can be returned by [sqlite3_status()]. 7019251883Speter** 7020251883Speter** <dl> 7021251883Speter** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt> 7022251883Speter** <dd>This parameter is the current amount of memory checked out 7023251883Speter** using [sqlite3_malloc()], either directly or indirectly. The 7024251883Speter** figure includes calls made to [sqlite3_malloc()] by the application 7025251883Speter** and internal memory usage by the SQLite library. Scratch memory 7026251883Speter** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache 7027251883Speter** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in 7028251883Speter** this parameter. The amount returned is the sum of the allocation 7029251883Speter** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^ 7030251883Speter** 7031251883Speter** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt> 7032251883Speter** <dd>This parameter records the largest memory allocation request 7033251883Speter** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their 7034251883Speter** internal equivalents). Only the value returned in the 7035251883Speter** *pHighwater parameter to [sqlite3_status()] is of interest. 7036251883Speter** The value written into the *pCurrent parameter is undefined.</dd>)^ 7037251883Speter** 7038251883Speter** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt> 7039251883Speter** <dd>This parameter records the number of separate memory allocations 7040251883Speter** currently checked out.</dd>)^ 7041251883Speter** 7042251883Speter** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt> 7043251883Speter** <dd>This parameter returns the number of pages used out of the 7044251883Speter** [pagecache memory allocator] that was configured using 7045251883Speter** [SQLITE_CONFIG_PAGECACHE]. The 7046251883Speter** value returned is in pages, not in bytes.</dd>)^ 7047251883Speter** 7048251883Speter** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]] 7049251883Speter** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt> 7050251883Speter** <dd>This parameter returns the number of bytes of page cache 7051251883Speter** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE] 7052251883Speter** buffer and where forced to overflow to [sqlite3_malloc()]. The 7053251883Speter** returned value includes allocations that overflowed because they 7054251883Speter** where too large (they were larger than the "sz" parameter to 7055251883Speter** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because 7056251883Speter** no space was left in the page cache.</dd>)^ 7057251883Speter** 7058251883Speter** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt> 7059251883Speter** <dd>This parameter records the largest memory allocation request 7060251883Speter** handed to [pagecache memory allocator]. Only the value returned in the 7061251883Speter** *pHighwater parameter to [sqlite3_status()] is of interest. 7062251883Speter** The value written into the *pCurrent parameter is undefined.</dd>)^ 7063251883Speter** 7064251883Speter** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt> 7065251883Speter** <dd>This parameter returns the number of allocations used out of the 7066251883Speter** [scratch memory allocator] configured using 7067251883Speter** [SQLITE_CONFIG_SCRATCH]. The value returned is in allocations, not 7068251883Speter** in bytes. Since a single thread may only have one scratch allocation 7069251883Speter** outstanding at time, this parameter also reports the number of threads 7070251883Speter** using scratch memory at the same time.</dd>)^ 7071251883Speter** 7072251883Speter** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt> 7073251883Speter** <dd>This parameter returns the number of bytes of scratch memory 7074251883Speter** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH] 7075251883Speter** buffer and where forced to overflow to [sqlite3_malloc()]. The values 7076251883Speter** returned include overflows because the requested allocation was too 7077251883Speter** larger (that is, because the requested allocation was larger than the 7078251883Speter** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer 7079251883Speter** slots were available. 7080251883Speter** </dd>)^ 7081251883Speter** 7082251883Speter** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt> 7083251883Speter** <dd>This parameter records the largest memory allocation request 7084251883Speter** handed to [scratch memory allocator]. Only the value returned in the 7085251883Speter** *pHighwater parameter to [sqlite3_status()] is of interest. 7086251883Speter** The value written into the *pCurrent parameter is undefined.</dd>)^ 7087251883Speter** 7088251883Speter** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt> 7089298161Sbapt** <dd>The *pHighwater parameter records the deepest parser stack. 7090298161Sbapt** The *pCurrent value is undefined. The *pHighwater value is only 7091251883Speter** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^ 7092251883Speter** </dl> 7093251883Speter** 7094251883Speter** New status parameters may be added from time to time. 7095251883Speter*/ 7096251883Speter#define SQLITE_STATUS_MEMORY_USED 0 7097251883Speter#define SQLITE_STATUS_PAGECACHE_USED 1 7098251883Speter#define SQLITE_STATUS_PAGECACHE_OVERFLOW 2 7099251883Speter#define SQLITE_STATUS_SCRATCH_USED 3 7100251883Speter#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 7101251883Speter#define SQLITE_STATUS_MALLOC_SIZE 5 7102251883Speter#define SQLITE_STATUS_PARSER_STACK 6 7103251883Speter#define SQLITE_STATUS_PAGECACHE_SIZE 7 7104251883Speter#define SQLITE_STATUS_SCRATCH_SIZE 8 7105251883Speter#define SQLITE_STATUS_MALLOC_COUNT 9 7106251883Speter 7107251883Speter/* 7108251883Speter** CAPI3REF: Database Connection Status 7109286510Speter** METHOD: sqlite3 7110251883Speter** 7111251883Speter** ^This interface is used to retrieve runtime status information 7112251883Speter** about a single [database connection]. ^The first argument is the 7113251883Speter** database connection object to be interrogated. ^The second argument 7114251883Speter** is an integer constant, taken from the set of 7115251883Speter** [SQLITE_DBSTATUS options], that 7116251883Speter** determines the parameter to interrogate. The set of 7117251883Speter** [SQLITE_DBSTATUS options] is likely 7118251883Speter** to grow in future releases of SQLite. 7119251883Speter** 7120251883Speter** ^The current value of the requested parameter is written into *pCur 7121251883Speter** and the highest instantaneous value is written into *pHiwtr. ^If 7122251883Speter** the resetFlg is true, then the highest instantaneous value is 7123251883Speter** reset back down to the current value. 7124251883Speter** 7125251883Speter** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a 7126251883Speter** non-zero [error code] on failure. 7127251883Speter** 7128251883Speter** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. 7129251883Speter*/ 7130322444SpeterSQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); 7131251883Speter 7132251883Speter/* 7133251883Speter** CAPI3REF: Status Parameters for database connections 7134251883Speter** KEYWORDS: {SQLITE_DBSTATUS options} 7135251883Speter** 7136251883Speter** These constants are the available integer "verbs" that can be passed as 7137251883Speter** the second argument to the [sqlite3_db_status()] interface. 7138251883Speter** 7139251883Speter** New verbs may be added in future releases of SQLite. Existing verbs 7140251883Speter** might be discontinued. Applications should check the return code from 7141251883Speter** [sqlite3_db_status()] to make sure that the call worked. 7142251883Speter** The [sqlite3_db_status()] interface will return a non-zero error code 7143251883Speter** if a discontinued or unsupported verb is invoked. 7144251883Speter** 7145251883Speter** <dl> 7146251883Speter** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt> 7147251883Speter** <dd>This parameter returns the number of lookaside memory slots currently 7148251883Speter** checked out.</dd>)^ 7149251883Speter** 7150251883Speter** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt> 7151251883Speter** <dd>This parameter returns the number malloc attempts that were 7152251883Speter** satisfied using lookaside memory. Only the high-water value is meaningful; 7153251883Speter** the current value is always zero.)^ 7154251883Speter** 7155251883Speter** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]] 7156251883Speter** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt> 7157251883Speter** <dd>This parameter returns the number malloc attempts that might have 7158251883Speter** been satisfied using lookaside memory but failed due to the amount of 7159251883Speter** memory requested being larger than the lookaside slot size. 7160251883Speter** Only the high-water value is meaningful; 7161251883Speter** the current value is always zero.)^ 7162251883Speter** 7163251883Speter** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]] 7164251883Speter** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt> 7165251883Speter** <dd>This parameter returns the number malloc attempts that might have 7166251883Speter** been satisfied using lookaside memory but failed due to all lookaside 7167251883Speter** memory already being in use. 7168251883Speter** Only the high-water value is meaningful; 7169251883Speter** the current value is always zero.)^ 7170251883Speter** 7171251883Speter** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt> 7172274884Sbapt** <dd>This parameter returns the approximate number of bytes of heap 7173251883Speter** memory used by all pager caches associated with the database connection.)^ 7174251883Speter** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0. 7175251883Speter** 7176305002Scy** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]] 7177305002Scy** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt> 7178305002Scy** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a 7179305002Scy** pager cache is shared between two or more connections the bytes of heap 7180305002Scy** memory used by that pager cache is divided evenly between the attached 7181305002Scy** connections.)^ In other words, if none of the pager caches associated 7182305002Scy** with the database connection are shared, this request returns the same 7183305002Scy** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are 7184305002Scy** shared, the value returned by this call will be smaller than that returned 7185305002Scy** by DBSTATUS_CACHE_USED. ^The highwater mark associated with 7186305002Scy** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0. 7187305002Scy** 7188251883Speter** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt> 7189274884Sbapt** <dd>This parameter returns the approximate number of bytes of heap 7190251883Speter** memory used to store the schema for all databases associated 7191251883Speter** with the connection - main, temp, and any [ATTACH]-ed databases.)^ 7192251883Speter** ^The full amount of memory used by the schemas is reported, even if the 7193251883Speter** schema memory is shared with other database connections due to 7194251883Speter** [shared cache mode] being enabled. 7195251883Speter** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0. 7196251883Speter** 7197251883Speter** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt> 7198274884Sbapt** <dd>This parameter returns the approximate number of bytes of heap 7199251883Speter** and lookaside memory used by all prepared statements associated with 7200251883Speter** the database connection.)^ 7201251883Speter** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0. 7202251883Speter** </dd> 7203251883Speter** 7204251883Speter** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt> 7205251883Speter** <dd>This parameter returns the number of pager cache hits that have 7206251883Speter** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT 7207251883Speter** is always 0. 7208251883Speter** </dd> 7209251883Speter** 7210251883Speter** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt> 7211251883Speter** <dd>This parameter returns the number of pager cache misses that have 7212251883Speter** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS 7213251883Speter** is always 0. 7214251883Speter** </dd> 7215251883Speter** 7216251883Speter** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt> 7217251883Speter** <dd>This parameter returns the number of dirty cache entries that have 7218251883Speter** been written to disk. Specifically, the number of pages written to the 7219251883Speter** wal file in wal mode databases, or the number of pages written to the 7220251883Speter** database file in rollback mode databases. Any pages written as part of 7221251883Speter** transaction rollback or database recovery operations are not included. 7222251883Speter** If an IO or other error occurs while writing a page to disk, the effect 7223251883Speter** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The 7224251883Speter** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0. 7225251883Speter** </dd> 7226269851Speter** 7227269851Speter** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt> 7228269851Speter** <dd>This parameter returns zero for the current value if and only if 7229269851Speter** all foreign key constraints (deferred or immediate) have been 7230269851Speter** resolved.)^ ^The highwater mark is always 0. 7231269851Speter** </dd> 7232251883Speter** </dl> 7233251883Speter*/ 7234251883Speter#define SQLITE_DBSTATUS_LOOKASIDE_USED 0 7235251883Speter#define SQLITE_DBSTATUS_CACHE_USED 1 7236251883Speter#define SQLITE_DBSTATUS_SCHEMA_USED 2 7237251883Speter#define SQLITE_DBSTATUS_STMT_USED 3 7238251883Speter#define SQLITE_DBSTATUS_LOOKASIDE_HIT 4 7239251883Speter#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5 7240251883Speter#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6 7241251883Speter#define SQLITE_DBSTATUS_CACHE_HIT 7 7242251883Speter#define SQLITE_DBSTATUS_CACHE_MISS 8 7243251883Speter#define SQLITE_DBSTATUS_CACHE_WRITE 9 7244269851Speter#define SQLITE_DBSTATUS_DEFERRED_FKS 10 7245305002Scy#define SQLITE_DBSTATUS_CACHE_USED_SHARED 11 7246305002Scy#define SQLITE_DBSTATUS_MAX 11 /* Largest defined DBSTATUS */ 7247251883Speter 7248251883Speter 7249251883Speter/* 7250251883Speter** CAPI3REF: Prepared Statement Status 7251286510Speter** METHOD: sqlite3_stmt 7252251883Speter** 7253251883Speter** ^(Each prepared statement maintains various 7254251883Speter** [SQLITE_STMTSTATUS counters] that measure the number 7255251883Speter** of times it has performed specific operations.)^ These counters can 7256251883Speter** be used to monitor the performance characteristics of the prepared 7257251883Speter** statements. For example, if the number of table steps greatly exceeds 7258251883Speter** the number of table searches or result rows, that would tend to indicate 7259251883Speter** that the prepared statement is using a full table scan rather than 7260251883Speter** an index. 7261251883Speter** 7262251883Speter** ^(This interface is used to retrieve and reset counter values from 7263251883Speter** a [prepared statement]. The first argument is the prepared statement 7264251883Speter** object to be interrogated. The second argument 7265251883Speter** is an integer code for a specific [SQLITE_STMTSTATUS counter] 7266251883Speter** to be interrogated.)^ 7267251883Speter** ^The current value of the requested counter is returned. 7268251883Speter** ^If the resetFlg is true, then the counter is reset to zero after this 7269251883Speter** interface call returns. 7270251883Speter** 7271251883Speter** See also: [sqlite3_status()] and [sqlite3_db_status()]. 7272251883Speter*/ 7273322444SpeterSQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); 7274251883Speter 7275251883Speter/* 7276251883Speter** CAPI3REF: Status Parameters for prepared statements 7277251883Speter** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters} 7278251883Speter** 7279251883Speter** These preprocessor macros define integer codes that name counter 7280251883Speter** values associated with the [sqlite3_stmt_status()] interface. 7281251883Speter** The meanings of the various counters are as follows: 7282251883Speter** 7283251883Speter** <dl> 7284251883Speter** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt> 7285251883Speter** <dd>^This is the number of times that SQLite has stepped forward in 7286251883Speter** a table as part of a full table scan. Large numbers for this counter 7287251883Speter** may indicate opportunities for performance improvement through 7288251883Speter** careful use of indices.</dd> 7289251883Speter** 7290251883Speter** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt> 7291251883Speter** <dd>^This is the number of sort operations that have occurred. 7292251883Speter** A non-zero value in this counter may indicate an opportunity to 7293251883Speter** improvement performance through careful use of indices.</dd> 7294251883Speter** 7295251883Speter** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt> 7296251883Speter** <dd>^This is the number of rows inserted into transient indices that 7297251883Speter** were created automatically in order to help joins run faster. 7298251883Speter** A non-zero value in this counter may indicate an opportunity to 7299251883Speter** improvement performance by adding permanent indices that do not 7300251883Speter** need to be reinitialized each time the statement is run.</dd> 7301269851Speter** 7302269851Speter** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt> 7303269851Speter** <dd>^This is the number of virtual machine operations executed 7304269851Speter** by the prepared statement if that number is less than or equal 7305269851Speter** to 2147483647. The number of virtual machine operations can be 7306269851Speter** used as a proxy for the total work done by the prepared statement. 7307269851Speter** If the number of virtual machine operations exceeds 2147483647 7308269851Speter** then the value returned by this statement status code is undefined. 7309322444Speter** 7310322444Speter** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt> 7311322444Speter** <dd>^This is the number of times that the prepare statement has been 7312322444Speter** automatically regenerated due to schema changes or change to 7313322444Speter** [bound parameters] that might affect the query plan. 7314322444Speter** 7315322444Speter** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt> 7316322444Speter** <dd>^This is the number of times that the prepared statement has 7317322444Speter** been run. A single "run" for the purposes of this counter is one 7318322444Speter** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()]. 7319322444Speter** The counter is incremented on the first [sqlite3_step()] call of each 7320322444Speter** cycle. 7321322444Speter** 7322322444Speter** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt> 7323322444Speter** <dd>^This is the approximate number of bytes of heap memory 7324322444Speter** used to store the prepared statement. ^This value is not actually 7325322444Speter** a counter, and so the resetFlg parameter to sqlite3_stmt_status() 7326322444Speter** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED. 7327269851Speter** </dd> 7328251883Speter** </dl> 7329251883Speter*/ 7330251883Speter#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1 7331251883Speter#define SQLITE_STMTSTATUS_SORT 2 7332251883Speter#define SQLITE_STMTSTATUS_AUTOINDEX 3 7333269851Speter#define SQLITE_STMTSTATUS_VM_STEP 4 7334322444Speter#define SQLITE_STMTSTATUS_REPREPARE 5 7335322444Speter#define SQLITE_STMTSTATUS_RUN 6 7336322444Speter#define SQLITE_STMTSTATUS_MEMUSED 99 7337251883Speter 7338251883Speter/* 7339251883Speter** CAPI3REF: Custom Page Cache Object 7340251883Speter** 7341251883Speter** The sqlite3_pcache type is opaque. It is implemented by 7342251883Speter** the pluggable module. The SQLite core has no knowledge of 7343251883Speter** its size or internal structure and never deals with the 7344251883Speter** sqlite3_pcache object except by holding and passing pointers 7345251883Speter** to the object. 7346251883Speter** 7347251883Speter** See [sqlite3_pcache_methods2] for additional information. 7348251883Speter*/ 7349251883Spetertypedef struct sqlite3_pcache sqlite3_pcache; 7350251883Speter 7351251883Speter/* 7352251883Speter** CAPI3REF: Custom Page Cache Object 7353251883Speter** 7354251883Speter** The sqlite3_pcache_page object represents a single page in the 7355251883Speter** page cache. The page cache will allocate instances of this 7356251883Speter** object. Various methods of the page cache use pointers to instances 7357251883Speter** of this object as parameters or as their return value. 7358251883Speter** 7359251883Speter** See [sqlite3_pcache_methods2] for additional information. 7360251883Speter*/ 7361251883Spetertypedef struct sqlite3_pcache_page sqlite3_pcache_page; 7362251883Speterstruct sqlite3_pcache_page { 7363251883Speter void *pBuf; /* The content of the page */ 7364251883Speter void *pExtra; /* Extra information associated with the page */ 7365251883Speter}; 7366251883Speter 7367251883Speter/* 7368251883Speter** CAPI3REF: Application Defined Page Cache. 7369251883Speter** KEYWORDS: {page cache} 7370251883Speter** 7371251883Speter** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can 7372251883Speter** register an alternative page cache implementation by passing in an 7373251883Speter** instance of the sqlite3_pcache_methods2 structure.)^ 7374251883Speter** In many applications, most of the heap memory allocated by 7375251883Speter** SQLite is used for the page cache. 7376251883Speter** By implementing a 7377251883Speter** custom page cache using this API, an application can better control 7378251883Speter** the amount of memory consumed by SQLite, the way in which 7379251883Speter** that memory is allocated and released, and the policies used to 7380251883Speter** determine exactly which parts of a database file are cached and for 7381251883Speter** how long. 7382251883Speter** 7383251883Speter** The alternative page cache mechanism is an 7384251883Speter** extreme measure that is only needed by the most demanding applications. 7385251883Speter** The built-in page cache is recommended for most uses. 7386251883Speter** 7387251883Speter** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an 7388251883Speter** internal buffer by SQLite within the call to [sqlite3_config]. Hence 7389251883Speter** the application may discard the parameter after the call to 7390251883Speter** [sqlite3_config()] returns.)^ 7391251883Speter** 7392251883Speter** [[the xInit() page cache method]] 7393251883Speter** ^(The xInit() method is called once for each effective 7394251883Speter** call to [sqlite3_initialize()])^ 7395251883Speter** (usually only once during the lifetime of the process). ^(The xInit() 7396251883Speter** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^ 7397251883Speter** The intent of the xInit() method is to set up global data structures 7398251883Speter** required by the custom page cache implementation. 7399251883Speter** ^(If the xInit() method is NULL, then the 7400251883Speter** built-in default page cache is used instead of the application defined 7401251883Speter** page cache.)^ 7402251883Speter** 7403251883Speter** [[the xShutdown() page cache method]] 7404251883Speter** ^The xShutdown() method is called by [sqlite3_shutdown()]. 7405251883Speter** It can be used to clean up 7406251883Speter** any outstanding resources before process shutdown, if required. 7407251883Speter** ^The xShutdown() method may be NULL. 7408251883Speter** 7409251883Speter** ^SQLite automatically serializes calls to the xInit method, 7410251883Speter** so the xInit method need not be threadsafe. ^The 7411251883Speter** xShutdown method is only called from [sqlite3_shutdown()] so it does 7412251883Speter** not need to be threadsafe either. All other methods must be threadsafe 7413251883Speter** in multithreaded applications. 7414251883Speter** 7415251883Speter** ^SQLite will never invoke xInit() more than once without an intervening 7416251883Speter** call to xShutdown(). 7417251883Speter** 7418251883Speter** [[the xCreate() page cache methods]] 7419251883Speter** ^SQLite invokes the xCreate() method to construct a new cache instance. 7420251883Speter** SQLite will typically create one cache instance for each open database file, 7421251883Speter** though this is not guaranteed. ^The 7422251883Speter** first parameter, szPage, is the size in bytes of the pages that must 7423251883Speter** be allocated by the cache. ^szPage will always a power of two. ^The 7424251883Speter** second parameter szExtra is a number of bytes of extra storage 7425251883Speter** associated with each page cache entry. ^The szExtra parameter will 7426251883Speter** a number less than 250. SQLite will use the 7427251883Speter** extra szExtra bytes on each page to store metadata about the underlying 7428251883Speter** database page on disk. The value passed into szExtra depends 7429251883Speter** on the SQLite version, the target platform, and how SQLite was compiled. 7430251883Speter** ^The third argument to xCreate(), bPurgeable, is true if the cache being 7431251883Speter** created will be used to cache database pages of a file stored on disk, or 7432251883Speter** false if it is used for an in-memory database. The cache implementation 7433251883Speter** does not have to do anything special based with the value of bPurgeable; 7434251883Speter** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will 7435251883Speter** never invoke xUnpin() except to deliberately delete a page. 7436251883Speter** ^In other words, calls to xUnpin() on a cache with bPurgeable set to 7437251883Speter** false will always have the "discard" flag set to true. 7438251883Speter** ^Hence, a cache created with bPurgeable false will 7439251883Speter** never contain any unpinned pages. 7440251883Speter** 7441251883Speter** [[the xCachesize() page cache method]] 7442251883Speter** ^(The xCachesize() method may be called at any time by SQLite to set the 7443251883Speter** suggested maximum cache-size (number of pages stored by) the cache 7444251883Speter** instance passed as the first argument. This is the value configured using 7445251883Speter** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable 7446251883Speter** parameter, the implementation is not required to do anything with this 7447251883Speter** value; it is advisory only. 7448251883Speter** 7449251883Speter** [[the xPagecount() page cache methods]] 7450251883Speter** The xPagecount() method must return the number of pages currently 7451251883Speter** stored in the cache, both pinned and unpinned. 7452251883Speter** 7453251883Speter** [[the xFetch() page cache methods]] 7454251883Speter** The xFetch() method locates a page in the cache and returns a pointer to 7455251883Speter** an sqlite3_pcache_page object associated with that page, or a NULL pointer. 7456251883Speter** The pBuf element of the returned sqlite3_pcache_page object will be a 7457251883Speter** pointer to a buffer of szPage bytes used to store the content of a 7458251883Speter** single database page. The pExtra element of sqlite3_pcache_page will be 7459251883Speter** a pointer to the szExtra bytes of extra storage that SQLite has requested 7460251883Speter** for each entry in the page cache. 7461251883Speter** 7462251883Speter** The page to be fetched is determined by the key. ^The minimum key value 7463251883Speter** is 1. After it has been retrieved using xFetch, the page is considered 7464251883Speter** to be "pinned". 7465251883Speter** 7466251883Speter** If the requested page is already in the page cache, then the page cache 7467251883Speter** implementation must return a pointer to the page buffer with its content 7468251883Speter** intact. If the requested page is not already in the cache, then the 7469251883Speter** cache implementation should use the value of the createFlag 7470251883Speter** parameter to help it determined what action to take: 7471251883Speter** 7472251883Speter** <table border=1 width=85% align=center> 7473251883Speter** <tr><th> createFlag <th> Behavior when page is not already in cache 7474251883Speter** <tr><td> 0 <td> Do not allocate a new page. Return NULL. 7475251883Speter** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so. 7476251883Speter** Otherwise return NULL. 7477251883Speter** <tr><td> 2 <td> Make every effort to allocate a new page. Only return 7478251883Speter** NULL if allocating a new page is effectively impossible. 7479251883Speter** </table> 7480251883Speter** 7481251883Speter** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite 7482251883Speter** will only use a createFlag of 2 after a prior call with a createFlag of 1 7483251883Speter** failed.)^ In between the to xFetch() calls, SQLite may 7484251883Speter** attempt to unpin one or more cache pages by spilling the content of 7485251883Speter** pinned pages to disk and synching the operating system disk cache. 7486251883Speter** 7487251883Speter** [[the xUnpin() page cache method]] 7488251883Speter** ^xUnpin() is called by SQLite with a pointer to a currently pinned page 7489251883Speter** as its second argument. If the third parameter, discard, is non-zero, 7490251883Speter** then the page must be evicted from the cache. 7491251883Speter** ^If the discard parameter is 7492251883Speter** zero, then the page may be discarded or retained at the discretion of 7493251883Speter** page cache implementation. ^The page cache implementation 7494251883Speter** may choose to evict unpinned pages at any time. 7495251883Speter** 7496251883Speter** The cache must not perform any reference counting. A single 7497251883Speter** call to xUnpin() unpins the page regardless of the number of prior calls 7498251883Speter** to xFetch(). 7499251883Speter** 7500251883Speter** [[the xRekey() page cache methods]] 7501251883Speter** The xRekey() method is used to change the key value associated with the 7502251883Speter** page passed as the second argument. If the cache 7503251883Speter** previously contains an entry associated with newKey, it must be 7504251883Speter** discarded. ^Any prior cache entry associated with newKey is guaranteed not 7505251883Speter** to be pinned. 7506251883Speter** 7507251883Speter** When SQLite calls the xTruncate() method, the cache must discard all 7508251883Speter** existing cache entries with page numbers (keys) greater than or equal 7509251883Speter** to the value of the iLimit parameter passed to xTruncate(). If any 7510251883Speter** of these pages are pinned, they are implicitly unpinned, meaning that 7511251883Speter** they can be safely discarded. 7512251883Speter** 7513251883Speter** [[the xDestroy() page cache method]] 7514251883Speter** ^The xDestroy() method is used to delete a cache allocated by xCreate(). 7515251883Speter** All resources associated with the specified cache should be freed. ^After 7516251883Speter** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*] 7517251883Speter** handle invalid, and will not use it with any other sqlite3_pcache_methods2 7518251883Speter** functions. 7519251883Speter** 7520251883Speter** [[the xShrink() page cache method]] 7521251883Speter** ^SQLite invokes the xShrink() method when it wants the page cache to 7522251883Speter** free up as much of heap memory as possible. The page cache implementation 7523251883Speter** is not obligated to free any memory, but well-behaved implementations should 7524251883Speter** do their best. 7525251883Speter*/ 7526251883Spetertypedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2; 7527251883Speterstruct sqlite3_pcache_methods2 { 7528251883Speter int iVersion; 7529251883Speter void *pArg; 7530251883Speter int (*xInit)(void*); 7531251883Speter void (*xShutdown)(void*); 7532251883Speter sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable); 7533251883Speter void (*xCachesize)(sqlite3_pcache*, int nCachesize); 7534251883Speter int (*xPagecount)(sqlite3_pcache*); 7535251883Speter sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag); 7536251883Speter void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard); 7537251883Speter void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*, 7538251883Speter unsigned oldKey, unsigned newKey); 7539251883Speter void (*xTruncate)(sqlite3_pcache*, unsigned iLimit); 7540251883Speter void (*xDestroy)(sqlite3_pcache*); 7541251883Speter void (*xShrink)(sqlite3_pcache*); 7542251883Speter}; 7543251883Speter 7544251883Speter/* 7545251883Speter** This is the obsolete pcache_methods object that has now been replaced 7546251883Speter** by sqlite3_pcache_methods2. This object is not used by SQLite. It is 7547251883Speter** retained in the header file for backwards compatibility only. 7548251883Speter*/ 7549251883Spetertypedef struct sqlite3_pcache_methods sqlite3_pcache_methods; 7550251883Speterstruct sqlite3_pcache_methods { 7551251883Speter void *pArg; 7552251883Speter int (*xInit)(void*); 7553251883Speter void (*xShutdown)(void*); 7554251883Speter sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable); 7555251883Speter void (*xCachesize)(sqlite3_pcache*, int nCachesize); 7556251883Speter int (*xPagecount)(sqlite3_pcache*); 7557251883Speter void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag); 7558251883Speter void (*xUnpin)(sqlite3_pcache*, void*, int discard); 7559251883Speter void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey); 7560251883Speter void (*xTruncate)(sqlite3_pcache*, unsigned iLimit); 7561251883Speter void (*xDestroy)(sqlite3_pcache*); 7562251883Speter}; 7563251883Speter 7564251883Speter 7565251883Speter/* 7566251883Speter** CAPI3REF: Online Backup Object 7567251883Speter** 7568251883Speter** The sqlite3_backup object records state information about an ongoing 7569251883Speter** online backup operation. ^The sqlite3_backup object is created by 7570251883Speter** a call to [sqlite3_backup_init()] and is destroyed by a call to 7571251883Speter** [sqlite3_backup_finish()]. 7572251883Speter** 7573251883Speter** See Also: [Using the SQLite Online Backup API] 7574251883Speter*/ 7575251883Spetertypedef struct sqlite3_backup sqlite3_backup; 7576251883Speter 7577251883Speter/* 7578251883Speter** CAPI3REF: Online Backup API. 7579251883Speter** 7580251883Speter** The backup API copies the content of one database into another. 7581251883Speter** It is useful either for creating backups of databases or 7582251883Speter** for copying in-memory databases to or from persistent files. 7583251883Speter** 7584251883Speter** See Also: [Using the SQLite Online Backup API] 7585251883Speter** 7586251883Speter** ^SQLite holds a write transaction open on the destination database file 7587251883Speter** for the duration of the backup operation. 7588251883Speter** ^The source database is read-locked only while it is being read; 7589251883Speter** it is not locked continuously for the entire backup operation. 7590251883Speter** ^Thus, the backup may be performed on a live source database without 7591251883Speter** preventing other database connections from 7592251883Speter** reading or writing to the source database while the backup is underway. 7593251883Speter** 7594251883Speter** ^(To perform a backup operation: 7595251883Speter** <ol> 7596251883Speter** <li><b>sqlite3_backup_init()</b> is called once to initialize the 7597251883Speter** backup, 7598251883Speter** <li><b>sqlite3_backup_step()</b> is called one or more times to transfer 7599251883Speter** the data between the two databases, and finally 7600251883Speter** <li><b>sqlite3_backup_finish()</b> is called to release all resources 7601251883Speter** associated with the backup operation. 7602251883Speter** </ol>)^ 7603251883Speter** There should be exactly one call to sqlite3_backup_finish() for each 7604251883Speter** successful call to sqlite3_backup_init(). 7605251883Speter** 7606251883Speter** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b> 7607251883Speter** 7608251883Speter** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the 7609251883Speter** [database connection] associated with the destination database 7610251883Speter** and the database name, respectively. 7611251883Speter** ^The database name is "main" for the main database, "temp" for the 7612251883Speter** temporary database, or the name specified after the AS keyword in 7613251883Speter** an [ATTACH] statement for an attached database. 7614251883Speter** ^The S and M arguments passed to 7615251883Speter** sqlite3_backup_init(D,N,S,M) identify the [database connection] 7616251883Speter** and database name of the source database, respectively. 7617251883Speter** ^The source and destination [database connections] (parameters S and D) 7618251883Speter** must be different or else sqlite3_backup_init(D,N,S,M) will fail with 7619251883Speter** an error. 7620251883Speter** 7621305002Scy** ^A call to sqlite3_backup_init() will fail, returning NULL, if 7622282328Sbapt** there is already a read or read-write transaction open on the 7623282328Sbapt** destination database. 7624282328Sbapt** 7625251883Speter** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is 7626251883Speter** returned and an error code and error message are stored in the 7627251883Speter** destination [database connection] D. 7628251883Speter** ^The error code and message for the failed call to sqlite3_backup_init() 7629251883Speter** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or 7630251883Speter** [sqlite3_errmsg16()] functions. 7631251883Speter** ^A successful call to sqlite3_backup_init() returns a pointer to an 7632251883Speter** [sqlite3_backup] object. 7633251883Speter** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and 7634251883Speter** sqlite3_backup_finish() functions to perform the specified backup 7635251883Speter** operation. 7636251883Speter** 7637251883Speter** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b> 7638251883Speter** 7639251883Speter** ^Function sqlite3_backup_step(B,N) will copy up to N pages between 7640251883Speter** the source and destination databases specified by [sqlite3_backup] object B. 7641251883Speter** ^If N is negative, all remaining source pages are copied. 7642251883Speter** ^If sqlite3_backup_step(B,N) successfully copies N pages and there 7643251883Speter** are still more pages to be copied, then the function returns [SQLITE_OK]. 7644251883Speter** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages 7645251883Speter** from source to destination, then it returns [SQLITE_DONE]. 7646251883Speter** ^If an error occurs while running sqlite3_backup_step(B,N), 7647251883Speter** then an [error code] is returned. ^As well as [SQLITE_OK] and 7648251883Speter** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY], 7649251883Speter** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an 7650251883Speter** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code. 7651251883Speter** 7652251883Speter** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if 7653251883Speter** <ol> 7654251883Speter** <li> the destination database was opened read-only, or 7655251883Speter** <li> the destination database is using write-ahead-log journaling 7656251883Speter** and the destination and source page sizes differ, or 7657251883Speter** <li> the destination database is an in-memory database and the 7658251883Speter** destination and source page sizes differ. 7659251883Speter** </ol>)^ 7660251883Speter** 7661251883Speter** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then 7662251883Speter** the [sqlite3_busy_handler | busy-handler function] 7663251883Speter** is invoked (if one is specified). ^If the 7664251883Speter** busy-handler returns non-zero before the lock is available, then 7665251883Speter** [SQLITE_BUSY] is returned to the caller. ^In this case the call to 7666251883Speter** sqlite3_backup_step() can be retried later. ^If the source 7667251883Speter** [database connection] 7668251883Speter** is being used to write to the source database when sqlite3_backup_step() 7669251883Speter** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this 7670251883Speter** case the call to sqlite3_backup_step() can be retried later on. ^(If 7671251883Speter** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or 7672251883Speter** [SQLITE_READONLY] is returned, then 7673251883Speter** there is no point in retrying the call to sqlite3_backup_step(). These 7674251883Speter** errors are considered fatal.)^ The application must accept 7675251883Speter** that the backup operation has failed and pass the backup operation handle 7676251883Speter** to the sqlite3_backup_finish() to release associated resources. 7677251883Speter** 7678251883Speter** ^The first call to sqlite3_backup_step() obtains an exclusive lock 7679251883Speter** on the destination file. ^The exclusive lock is not released until either 7680251883Speter** sqlite3_backup_finish() is called or the backup operation is complete 7681251883Speter** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to 7682251883Speter** sqlite3_backup_step() obtains a [shared lock] on the source database that 7683251883Speter** lasts for the duration of the sqlite3_backup_step() call. 7684251883Speter** ^Because the source database is not locked between calls to 7685251883Speter** sqlite3_backup_step(), the source database may be modified mid-way 7686251883Speter** through the backup process. ^If the source database is modified by an 7687251883Speter** external process or via a database connection other than the one being 7688251883Speter** used by the backup operation, then the backup will be automatically 7689251883Speter** restarted by the next call to sqlite3_backup_step(). ^If the source 7690251883Speter** database is modified by the using the same database connection as is used 7691251883Speter** by the backup operation, then the backup database is automatically 7692251883Speter** updated at the same time. 7693251883Speter** 7694251883Speter** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b> 7695251883Speter** 7696251883Speter** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the 7697251883Speter** application wishes to abandon the backup operation, the application 7698251883Speter** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish(). 7699251883Speter** ^The sqlite3_backup_finish() interfaces releases all 7700251883Speter** resources associated with the [sqlite3_backup] object. 7701251883Speter** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any 7702251883Speter** active write-transaction on the destination database is rolled back. 7703251883Speter** The [sqlite3_backup] object is invalid 7704251883Speter** and may not be used following a call to sqlite3_backup_finish(). 7705251883Speter** 7706251883Speter** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no 7707251883Speter** sqlite3_backup_step() errors occurred, regardless or whether or not 7708251883Speter** sqlite3_backup_step() completed. 7709251883Speter** ^If an out-of-memory condition or IO error occurred during any prior 7710251883Speter** sqlite3_backup_step() call on the same [sqlite3_backup] object, then 7711251883Speter** sqlite3_backup_finish() returns the corresponding [error code]. 7712251883Speter** 7713251883Speter** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step() 7714251883Speter** is not a permanent error and does not affect the return value of 7715251883Speter** sqlite3_backup_finish(). 7716251883Speter** 7717282328Sbapt** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]] 7718251883Speter** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b> 7719251883Speter** 7720282328Sbapt** ^The sqlite3_backup_remaining() routine returns the number of pages still 7721282328Sbapt** to be backed up at the conclusion of the most recent sqlite3_backup_step(). 7722282328Sbapt** ^The sqlite3_backup_pagecount() routine returns the total number of pages 7723282328Sbapt** in the source database at the conclusion of the most recent 7724282328Sbapt** sqlite3_backup_step(). 7725282328Sbapt** ^(The values returned by these functions are only updated by 7726282328Sbapt** sqlite3_backup_step(). If the source database is modified in a way that 7727282328Sbapt** changes the size of the source database or the number of pages remaining, 7728282328Sbapt** those changes are not reflected in the output of sqlite3_backup_pagecount() 7729282328Sbapt** and sqlite3_backup_remaining() until after the next 7730282328Sbapt** sqlite3_backup_step().)^ 7731251883Speter** 7732251883Speter** <b>Concurrent Usage of Database Handles</b> 7733251883Speter** 7734251883Speter** ^The source [database connection] may be used by the application for other 7735251883Speter** purposes while a backup operation is underway or being initialized. 7736251883Speter** ^If SQLite is compiled and configured to support threadsafe database 7737251883Speter** connections, then the source database connection may be used concurrently 7738251883Speter** from within other threads. 7739251883Speter** 7740251883Speter** However, the application must guarantee that the destination 7741251883Speter** [database connection] is not passed to any other API (by any thread) after 7742251883Speter** sqlite3_backup_init() is called and before the corresponding call to 7743251883Speter** sqlite3_backup_finish(). SQLite does not currently check to see 7744251883Speter** if the application incorrectly accesses the destination [database connection] 7745251883Speter** and so no error code is reported, but the operations may malfunction 7746251883Speter** nevertheless. Use of the destination database connection while a 7747251883Speter** backup is in progress might also also cause a mutex deadlock. 7748251883Speter** 7749251883Speter** If running in [shared cache mode], the application must 7750251883Speter** guarantee that the shared cache used by the destination database 7751251883Speter** is not accessed while the backup is running. In practice this means 7752251883Speter** that the application must guarantee that the disk file being 7753251883Speter** backed up to is not accessed by any connection within the process, 7754251883Speter** not just the specific connection that was passed to sqlite3_backup_init(). 7755251883Speter** 7756251883Speter** The [sqlite3_backup] object itself is partially threadsafe. Multiple 7757251883Speter** threads may safely make multiple concurrent calls to sqlite3_backup_step(). 7758251883Speter** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount() 7759251883Speter** APIs are not strictly speaking threadsafe. If they are invoked at the 7760251883Speter** same time as another thread is invoking sqlite3_backup_step() it is 7761251883Speter** possible that they return invalid values. 7762251883Speter*/ 7763322444SpeterSQLITE_API sqlite3_backup *sqlite3_backup_init( 7764251883Speter sqlite3 *pDest, /* Destination database handle */ 7765251883Speter const char *zDestName, /* Destination database name */ 7766251883Speter sqlite3 *pSource, /* Source database handle */ 7767251883Speter const char *zSourceName /* Source database name */ 7768251883Speter); 7769322444SpeterSQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage); 7770322444SpeterSQLITE_API int sqlite3_backup_finish(sqlite3_backup *p); 7771322444SpeterSQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p); 7772322444SpeterSQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p); 7773251883Speter 7774251883Speter/* 7775251883Speter** CAPI3REF: Unlock Notification 7776286510Speter** METHOD: sqlite3 7777251883Speter** 7778251883Speter** ^When running in shared-cache mode, a database operation may fail with 7779251883Speter** an [SQLITE_LOCKED] error if the required locks on the shared-cache or 7780251883Speter** individual tables within the shared-cache cannot be obtained. See 7781251883Speter** [SQLite Shared-Cache Mode] for a description of shared-cache locking. 7782251883Speter** ^This API may be used to register a callback that SQLite will invoke 7783251883Speter** when the connection currently holding the required lock relinquishes it. 7784251883Speter** ^This API is only available if the library was compiled with the 7785251883Speter** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined. 7786251883Speter** 7787251883Speter** See Also: [Using the SQLite Unlock Notification Feature]. 7788251883Speter** 7789251883Speter** ^Shared-cache locks are released when a database connection concludes 7790251883Speter** its current transaction, either by committing it or rolling it back. 7791251883Speter** 7792251883Speter** ^When a connection (known as the blocked connection) fails to obtain a 7793251883Speter** shared-cache lock and SQLITE_LOCKED is returned to the caller, the 7794251883Speter** identity of the database connection (the blocking connection) that 7795251883Speter** has locked the required resource is stored internally. ^After an 7796251883Speter** application receives an SQLITE_LOCKED error, it may call the 7797251883Speter** sqlite3_unlock_notify() method with the blocked connection handle as 7798251883Speter** the first argument to register for a callback that will be invoked 7799251883Speter** when the blocking connections current transaction is concluded. ^The 7800251883Speter** callback is invoked from within the [sqlite3_step] or [sqlite3_close] 7801251883Speter** call that concludes the blocking connections transaction. 7802251883Speter** 7803251883Speter** ^(If sqlite3_unlock_notify() is called in a multi-threaded application, 7804251883Speter** there is a chance that the blocking connection will have already 7805251883Speter** concluded its transaction by the time sqlite3_unlock_notify() is invoked. 7806251883Speter** If this happens, then the specified callback is invoked immediately, 7807251883Speter** from within the call to sqlite3_unlock_notify().)^ 7808251883Speter** 7809251883Speter** ^If the blocked connection is attempting to obtain a write-lock on a 7810251883Speter** shared-cache table, and more than one other connection currently holds 7811251883Speter** a read-lock on the same table, then SQLite arbitrarily selects one of 7812251883Speter** the other connections to use as the blocking connection. 7813251883Speter** 7814251883Speter** ^(There may be at most one unlock-notify callback registered by a 7815251883Speter** blocked connection. If sqlite3_unlock_notify() is called when the 7816251883Speter** blocked connection already has a registered unlock-notify callback, 7817251883Speter** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is 7818251883Speter** called with a NULL pointer as its second argument, then any existing 7819251883Speter** unlock-notify callback is canceled. ^The blocked connections 7820251883Speter** unlock-notify callback may also be canceled by closing the blocked 7821251883Speter** connection using [sqlite3_close()]. 7822251883Speter** 7823251883Speter** The unlock-notify callback is not reentrant. If an application invokes 7824251883Speter** any sqlite3_xxx API functions from within an unlock-notify callback, a 7825251883Speter** crash or deadlock may be the result. 7826251883Speter** 7827251883Speter** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always 7828251883Speter** returns SQLITE_OK. 7829251883Speter** 7830251883Speter** <b>Callback Invocation Details</b> 7831251883Speter** 7832251883Speter** When an unlock-notify callback is registered, the application provides a 7833251883Speter** single void* pointer that is passed to the callback when it is invoked. 7834251883Speter** However, the signature of the callback function allows SQLite to pass 7835251883Speter** it an array of void* context pointers. The first argument passed to 7836251883Speter** an unlock-notify callback is a pointer to an array of void* pointers, 7837251883Speter** and the second is the number of entries in the array. 7838251883Speter** 7839251883Speter** When a blocking connections transaction is concluded, there may be 7840251883Speter** more than one blocked connection that has registered for an unlock-notify 7841251883Speter** callback. ^If two or more such blocked connections have specified the 7842251883Speter** same callback function, then instead of invoking the callback function 7843251883Speter** multiple times, it is invoked once with the set of void* context pointers 7844251883Speter** specified by the blocked connections bundled together into an array. 7845251883Speter** This gives the application an opportunity to prioritize any actions 7846251883Speter** related to the set of unblocked database connections. 7847251883Speter** 7848251883Speter** <b>Deadlock Detection</b> 7849251883Speter** 7850251883Speter** Assuming that after registering for an unlock-notify callback a 7851251883Speter** database waits for the callback to be issued before taking any further 7852251883Speter** action (a reasonable assumption), then using this API may cause the 7853251883Speter** application to deadlock. For example, if connection X is waiting for 7854251883Speter** connection Y's transaction to be concluded, and similarly connection 7855251883Speter** Y is waiting on connection X's transaction, then neither connection 7856251883Speter** will proceed and the system may remain deadlocked indefinitely. 7857251883Speter** 7858251883Speter** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock 7859251883Speter** detection. ^If a given call to sqlite3_unlock_notify() would put the 7860251883Speter** system in a deadlocked state, then SQLITE_LOCKED is returned and no 7861251883Speter** unlock-notify callback is registered. The system is said to be in 7862251883Speter** a deadlocked state if connection A has registered for an unlock-notify 7863251883Speter** callback on the conclusion of connection B's transaction, and connection 7864251883Speter** B has itself registered for an unlock-notify callback when connection 7865251883Speter** A's transaction is concluded. ^Indirect deadlock is also detected, so 7866251883Speter** the system is also considered to be deadlocked if connection B has 7867251883Speter** registered for an unlock-notify callback on the conclusion of connection 7868251883Speter** C's transaction, where connection C is waiting on connection A. ^Any 7869251883Speter** number of levels of indirection are allowed. 7870251883Speter** 7871251883Speter** <b>The "DROP TABLE" Exception</b> 7872251883Speter** 7873251883Speter** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost 7874251883Speter** always appropriate to call sqlite3_unlock_notify(). There is however, 7875251883Speter** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement, 7876251883Speter** SQLite checks if there are any currently executing SELECT statements 7877251883Speter** that belong to the same connection. If there are, SQLITE_LOCKED is 7878251883Speter** returned. In this case there is no "blocking connection", so invoking 7879251883Speter** sqlite3_unlock_notify() results in the unlock-notify callback being 7880251883Speter** invoked immediately. If the application then re-attempts the "DROP TABLE" 7881251883Speter** or "DROP INDEX" query, an infinite loop might be the result. 7882251883Speter** 7883251883Speter** One way around this problem is to check the extended error code returned 7884251883Speter** by an sqlite3_step() call. ^(If there is a blocking connection, then the 7885251883Speter** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in 7886251883Speter** the special "DROP TABLE/INDEX" case, the extended error code is just 7887251883Speter** SQLITE_LOCKED.)^ 7888251883Speter*/ 7889322444SpeterSQLITE_API int sqlite3_unlock_notify( 7890251883Speter sqlite3 *pBlocked, /* Waiting connection */ 7891251883Speter void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */ 7892251883Speter void *pNotifyArg /* Argument to pass to xNotify */ 7893251883Speter); 7894251883Speter 7895251883Speter 7896251883Speter/* 7897251883Speter** CAPI3REF: String Comparison 7898251883Speter** 7899251883Speter** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications 7900251883Speter** and extensions to compare the contents of two buffers containing UTF-8 7901251883Speter** strings in a case-independent fashion, using the same definition of "case 7902251883Speter** independence" that SQLite uses internally when comparing identifiers. 7903251883Speter*/ 7904322444SpeterSQLITE_API int sqlite3_stricmp(const char *, const char *); 7905322444SpeterSQLITE_API int sqlite3_strnicmp(const char *, const char *, int); 7906251883Speter 7907251883Speter/* 7908251883Speter** CAPI3REF: String Globbing 7909251883Speter* 7910298161Sbapt** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if 7911298161Sbapt** string X matches the [GLOB] pattern P. 7912298161Sbapt** ^The definition of [GLOB] pattern matching used in 7913251883Speter** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the 7914298161Sbapt** SQL dialect understood by SQLite. ^The [sqlite3_strglob(P,X)] function 7915298161Sbapt** is case sensitive. 7916251883Speter** 7917251883Speter** Note that this routine returns zero on a match and non-zero if the strings 7918251883Speter** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()]. 7919298161Sbapt** 7920298161Sbapt** See also: [sqlite3_strlike()]. 7921251883Speter*/ 7922322444SpeterSQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr); 7923251883Speter 7924251883Speter/* 7925298161Sbapt** CAPI3REF: String LIKE Matching 7926298161Sbapt* 7927298161Sbapt** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if 7928298161Sbapt** string X matches the [LIKE] pattern P with escape character E. 7929298161Sbapt** ^The definition of [LIKE] pattern matching used in 7930298161Sbapt** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E" 7931298161Sbapt** operator in the SQL dialect understood by SQLite. ^For "X LIKE P" without 7932298161Sbapt** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0. 7933298161Sbapt** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case 7934298161Sbapt** insensitive - equivalent upper and lower case ASCII characters match 7935298161Sbapt** one another. 7936298161Sbapt** 7937298161Sbapt** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though 7938298161Sbapt** only ASCII characters are case folded. 7939298161Sbapt** 7940298161Sbapt** Note that this routine returns zero on a match and non-zero if the strings 7941298161Sbapt** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()]. 7942298161Sbapt** 7943298161Sbapt** See also: [sqlite3_strglob()]. 7944298161Sbapt*/ 7945322444SpeterSQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc); 7946298161Sbapt 7947298161Sbapt/* 7948251883Speter** CAPI3REF: Error Logging Interface 7949251883Speter** 7950251883Speter** ^The [sqlite3_log()] interface writes a message into the [error log] 7951251883Speter** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()]. 7952251883Speter** ^If logging is enabled, the zFormat string and subsequent arguments are 7953251883Speter** used with [sqlite3_snprintf()] to generate the final output string. 7954251883Speter** 7955251883Speter** The sqlite3_log() interface is intended for use by extensions such as 7956251883Speter** virtual tables, collating functions, and SQL functions. While there is 7957251883Speter** nothing to prevent an application from calling sqlite3_log(), doing so 7958251883Speter** is considered bad form. 7959251883Speter** 7960251883Speter** The zFormat string must not be NULL. 7961251883Speter** 7962251883Speter** To avoid deadlocks and other threading problems, the sqlite3_log() routine 7963251883Speter** will not use dynamically allocated memory. The log message is stored in 7964251883Speter** a fixed-length buffer on the stack. If the log message is longer than 7965251883Speter** a few hundred characters, it will be truncated to the length of the 7966251883Speter** buffer. 7967251883Speter*/ 7968322444SpeterSQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...); 7969251883Speter 7970251883Speter/* 7971251883Speter** CAPI3REF: Write-Ahead Log Commit Hook 7972286510Speter** METHOD: sqlite3 7973251883Speter** 7974251883Speter** ^The [sqlite3_wal_hook()] function is used to register a callback that 7975282328Sbapt** is invoked each time data is committed to a database in wal mode. 7976251883Speter** 7977282328Sbapt** ^(The callback is invoked by SQLite after the commit has taken place and 7978282328Sbapt** the associated write-lock on the database released)^, so the implementation 7979251883Speter** may read, write or [checkpoint] the database as required. 7980251883Speter** 7981251883Speter** ^The first parameter passed to the callback function when it is invoked 7982251883Speter** is a copy of the third parameter passed to sqlite3_wal_hook() when 7983251883Speter** registering the callback. ^The second is a copy of the database handle. 7984251883Speter** ^The third parameter is the name of the database that was written to - 7985251883Speter** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter 7986251883Speter** is the number of pages currently in the write-ahead log file, 7987251883Speter** including those that were just committed. 7988251883Speter** 7989251883Speter** The callback function should normally return [SQLITE_OK]. ^If an error 7990251883Speter** code is returned, that error will propagate back up through the 7991251883Speter** SQLite code base to cause the statement that provoked the callback 7992251883Speter** to report an error, though the commit will have still occurred. If the 7993251883Speter** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value 7994251883Speter** that does not correspond to any valid SQLite error code, the results 7995251883Speter** are undefined. 7996251883Speter** 7997251883Speter** A single database handle may have at most a single write-ahead log callback 7998251883Speter** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any 7999251883Speter** previously registered write-ahead log callback. ^Note that the 8000251883Speter** [sqlite3_wal_autocheckpoint()] interface and the 8001251883Speter** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will 8002298161Sbapt** overwrite any prior [sqlite3_wal_hook()] settings. 8003251883Speter*/ 8004322444SpeterSQLITE_API void *sqlite3_wal_hook( 8005251883Speter sqlite3*, 8006251883Speter int(*)(void *,sqlite3*,const char*,int), 8007251883Speter void* 8008251883Speter); 8009251883Speter 8010251883Speter/* 8011251883Speter** CAPI3REF: Configure an auto-checkpoint 8012286510Speter** METHOD: sqlite3 8013251883Speter** 8014251883Speter** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around 8015251883Speter** [sqlite3_wal_hook()] that causes any database on [database connection] D 8016251883Speter** to automatically [checkpoint] 8017251883Speter** after committing a transaction if there are N or 8018251883Speter** more frames in the [write-ahead log] file. ^Passing zero or 8019251883Speter** a negative value as the nFrame parameter disables automatic 8020251883Speter** checkpoints entirely. 8021251883Speter** 8022251883Speter** ^The callback registered by this function replaces any existing callback 8023251883Speter** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback 8024251883Speter** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism 8025251883Speter** configured by this function. 8026251883Speter** 8027251883Speter** ^The [wal_autocheckpoint pragma] can be used to invoke this interface 8028251883Speter** from SQL. 8029251883Speter** 8030274884Sbapt** ^Checkpoints initiated by this mechanism are 8031274884Sbapt** [sqlite3_wal_checkpoint_v2|PASSIVE]. 8032274884Sbapt** 8033251883Speter** ^Every new [database connection] defaults to having the auto-checkpoint 8034251883Speter** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT] 8035251883Speter** pages. The use of this interface 8036251883Speter** is only necessary if the default setting is found to be suboptimal 8037251883Speter** for a particular application. 8038251883Speter*/ 8039322444SpeterSQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N); 8040251883Speter 8041251883Speter/* 8042251883Speter** CAPI3REF: Checkpoint a database 8043286510Speter** METHOD: sqlite3 8044251883Speter** 8045282328Sbapt** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to 8046282328Sbapt** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^ 8047251883Speter** 8048282328Sbapt** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the 8049282328Sbapt** [write-ahead log] for database X on [database connection] D to be 8050282328Sbapt** transferred into the database file and for the write-ahead log to 8051282328Sbapt** be reset. See the [checkpointing] documentation for addition 8052282328Sbapt** information. 8053251883Speter** 8054282328Sbapt** This interface used to be the only way to cause a checkpoint to 8055282328Sbapt** occur. But then the newer and more powerful [sqlite3_wal_checkpoint_v2()] 8056282328Sbapt** interface was added. This interface is retained for backwards 8057282328Sbapt** compatibility and as a convenience for applications that need to manually 8058282328Sbapt** start a callback but which do not need the full power (and corresponding 8059282328Sbapt** complication) of [sqlite3_wal_checkpoint_v2()]. 8060251883Speter*/ 8061322444SpeterSQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); 8062251883Speter 8063251883Speter/* 8064251883Speter** CAPI3REF: Checkpoint a database 8065286510Speter** METHOD: sqlite3 8066251883Speter** 8067282328Sbapt** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint 8068282328Sbapt** operation on database X of [database connection] D in mode M. Status 8069282328Sbapt** information is written back into integers pointed to by L and C.)^ 8070282328Sbapt** ^(The M parameter must be a valid [checkpoint mode]:)^ 8071251883Speter** 8072251883Speter** <dl> 8073251883Speter** <dt>SQLITE_CHECKPOINT_PASSIVE<dd> 8074282328Sbapt** ^Checkpoint as many frames as possible without waiting for any database 8075282328Sbapt** readers or writers to finish, then sync the database file if all frames 8076282328Sbapt** in the log were checkpointed. ^The [busy-handler callback] 8077282328Sbapt** is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode. 8078282328Sbapt** ^On the other hand, passive mode might leave the checkpoint unfinished 8079282328Sbapt** if there are concurrent readers or writers. 8080251883Speter** 8081251883Speter** <dt>SQLITE_CHECKPOINT_FULL<dd> 8082282328Sbapt** ^This mode blocks (it invokes the 8083274884Sbapt** [sqlite3_busy_handler|busy-handler callback]) until there is no 8084251883Speter** database writer and all readers are reading from the most recent database 8085282328Sbapt** snapshot. ^It then checkpoints all frames in the log file and syncs the 8086282328Sbapt** database file. ^This mode blocks new database writers while it is pending, 8087282328Sbapt** but new database readers are allowed to continue unimpeded. 8088251883Speter** 8089251883Speter** <dt>SQLITE_CHECKPOINT_RESTART<dd> 8090282328Sbapt** ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition 8091282328Sbapt** that after checkpointing the log file it blocks (calls the 8092282328Sbapt** [busy-handler callback]) 8093282328Sbapt** until all readers are reading from the database file only. ^This ensures 8094282328Sbapt** that the next writer will restart the log file from the beginning. 8095282328Sbapt** ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new 8096282328Sbapt** database writer attempts while it is pending, but does not impede readers. 8097282328Sbapt** 8098282328Sbapt** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd> 8099282328Sbapt** ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the 8100282328Sbapt** addition that it also truncates the log file to zero bytes just prior 8101282328Sbapt** to a successful return. 8102251883Speter** </dl> 8103251883Speter** 8104282328Sbapt** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in 8105282328Sbapt** the log file or to -1 if the checkpoint could not run because 8106282328Sbapt** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not 8107282328Sbapt** NULL,then *pnCkpt is set to the total number of checkpointed frames in the 8108282328Sbapt** log file (including any that were already checkpointed before the function 8109282328Sbapt** was called) or to -1 if the checkpoint could not run due to an error or 8110282328Sbapt** because the database is not in WAL mode. ^Note that upon successful 8111282328Sbapt** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been 8112282328Sbapt** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero. 8113251883Speter** 8114282328Sbapt** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If 8115251883Speter** any other process is running a checkpoint operation at the same time, the 8116282328Sbapt** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a 8117251883Speter** busy-handler configured, it will not be invoked in this case. 8118251883Speter** 8119282328Sbapt** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the 8120282328Sbapt** exclusive "writer" lock on the database file. ^If the writer lock cannot be 8121282328Sbapt** obtained immediately, and a busy-handler is configured, it is invoked and 8122282328Sbapt** the writer lock retried until either the busy-handler returns 0 or the lock 8123282328Sbapt** is successfully obtained. ^The busy-handler is also invoked while waiting for 8124282328Sbapt** database readers as described above. ^If the busy-handler returns 0 before 8125251883Speter** the writer lock is obtained or while waiting for database readers, the 8126251883Speter** checkpoint operation proceeds from that point in the same way as 8127251883Speter** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible 8128282328Sbapt** without blocking any further. ^SQLITE_BUSY is returned in this case. 8129251883Speter** 8130282328Sbapt** ^If parameter zDb is NULL or points to a zero length string, then the 8131282328Sbapt** specified operation is attempted on all WAL databases [attached] to 8132282328Sbapt** [database connection] db. In this case the 8133282328Sbapt** values written to output parameters *pnLog and *pnCkpt are undefined. ^If 8134251883Speter** an SQLITE_BUSY error is encountered when processing one or more of the 8135251883Speter** attached WAL databases, the operation is still attempted on any remaining 8136282328Sbapt** attached databases and SQLITE_BUSY is returned at the end. ^If any other 8137251883Speter** error occurs while processing an attached database, processing is abandoned 8138282328Sbapt** and the error code is returned to the caller immediately. ^If no error 8139251883Speter** (SQLITE_BUSY or otherwise) is encountered while processing the attached 8140251883Speter** databases, SQLITE_OK is returned. 8141251883Speter** 8142282328Sbapt** ^If database zDb is the name of an attached database that is not in WAL 8143282328Sbapt** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If 8144251883Speter** zDb is not NULL (or a zero length string) and is not the name of any 8145251883Speter** attached database, SQLITE_ERROR is returned to the caller. 8146282328Sbapt** 8147282328Sbapt** ^Unless it returns SQLITE_MISUSE, 8148282328Sbapt** the sqlite3_wal_checkpoint_v2() interface 8149282328Sbapt** sets the error information that is queried by 8150282328Sbapt** [sqlite3_errcode()] and [sqlite3_errmsg()]. 8151282328Sbapt** 8152282328Sbapt** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface 8153282328Sbapt** from SQL. 8154251883Speter*/ 8155322444SpeterSQLITE_API int sqlite3_wal_checkpoint_v2( 8156251883Speter sqlite3 *db, /* Database handle */ 8157251883Speter const char *zDb, /* Name of attached database (or NULL) */ 8158251883Speter int eMode, /* SQLITE_CHECKPOINT_* value */ 8159251883Speter int *pnLog, /* OUT: Size of WAL log in frames */ 8160251883Speter int *pnCkpt /* OUT: Total number of frames checkpointed */ 8161251883Speter); 8162251883Speter 8163251883Speter/* 8164282328Sbapt** CAPI3REF: Checkpoint Mode Values 8165282328Sbapt** KEYWORDS: {checkpoint mode} 8166251883Speter** 8167282328Sbapt** These constants define all valid values for the "checkpoint mode" passed 8168282328Sbapt** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface. 8169282328Sbapt** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the 8170282328Sbapt** meaning of each of these checkpoint modes. 8171251883Speter*/ 8172282328Sbapt#define SQLITE_CHECKPOINT_PASSIVE 0 /* Do as much as possible w/o blocking */ 8173282328Sbapt#define SQLITE_CHECKPOINT_FULL 1 /* Wait for writers, then checkpoint */ 8174282328Sbapt#define SQLITE_CHECKPOINT_RESTART 2 /* Like FULL but wait for for readers */ 8175282328Sbapt#define SQLITE_CHECKPOINT_TRUNCATE 3 /* Like RESTART but also truncate WAL */ 8176251883Speter 8177251883Speter/* 8178251883Speter** CAPI3REF: Virtual Table Interface Configuration 8179251883Speter** 8180251883Speter** This function may be called by either the [xConnect] or [xCreate] method 8181251883Speter** of a [virtual table] implementation to configure 8182251883Speter** various facets of the virtual table interface. 8183251883Speter** 8184251883Speter** If this interface is invoked outside the context of an xConnect or 8185251883Speter** xCreate virtual table method then the behavior is undefined. 8186251883Speter** 8187251883Speter** At present, there is only one option that may be configured using 8188251883Speter** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].) Further options 8189251883Speter** may be added in the future. 8190251883Speter*/ 8191322444SpeterSQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...); 8192251883Speter 8193251883Speter/* 8194251883Speter** CAPI3REF: Virtual Table Configuration Options 8195251883Speter** 8196251883Speter** These macros define the various options to the 8197251883Speter** [sqlite3_vtab_config()] interface that [virtual table] implementations 8198251883Speter** can use to customize and optimize their behavior. 8199251883Speter** 8200251883Speter** <dl> 8201251883Speter** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT 8202251883Speter** <dd>Calls of the form 8203251883Speter** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported, 8204251883Speter** where X is an integer. If X is zero, then the [virtual table] whose 8205251883Speter** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not 8206251883Speter** support constraints. In this configuration (which is the default) if 8207251883Speter** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire 8208251883Speter** statement is rolled back as if [ON CONFLICT | OR ABORT] had been 8209251883Speter** specified as part of the users SQL statement, regardless of the actual 8210251883Speter** ON CONFLICT mode specified. 8211251883Speter** 8212251883Speter** If X is non-zero, then the virtual table implementation guarantees 8213251883Speter** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before 8214251883Speter** any modifications to internal or persistent data structures have been made. 8215251883Speter** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite 8216251883Speter** is able to roll back a statement or database transaction, and abandon 8217251883Speter** or continue processing the current SQL statement as appropriate. 8218251883Speter** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns 8219251883Speter** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode 8220251883Speter** had been ABORT. 8221251883Speter** 8222251883Speter** Virtual table implementations that are required to handle OR REPLACE 8223251883Speter** must do so within the [xUpdate] method. If a call to the 8224251883Speter** [sqlite3_vtab_on_conflict()] function indicates that the current ON 8225251883Speter** CONFLICT policy is REPLACE, the virtual table implementation should 8226251883Speter** silently replace the appropriate rows within the xUpdate callback and 8227251883Speter** return SQLITE_OK. Or, if this is not possible, it may return 8228251883Speter** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT 8229251883Speter** constraint handling. 8230251883Speter** </dl> 8231251883Speter*/ 8232251883Speter#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1 8233251883Speter 8234251883Speter/* 8235251883Speter** CAPI3REF: Determine The Virtual Table Conflict Policy 8236251883Speter** 8237251883Speter** This function may only be called from within a call to the [xUpdate] method 8238251883Speter** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The 8239251883Speter** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL], 8240251883Speter** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode 8241251883Speter** of the SQL statement that triggered the call to the [xUpdate] method of the 8242251883Speter** [virtual table]. 8243251883Speter*/ 8244322444SpeterSQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *); 8245251883Speter 8246251883Speter/* 8247251883Speter** CAPI3REF: Conflict resolution modes 8248274884Sbapt** KEYWORDS: {conflict resolution mode} 8249251883Speter** 8250251883Speter** These constants are returned by [sqlite3_vtab_on_conflict()] to 8251251883Speter** inform a [virtual table] implementation what the [ON CONFLICT] mode 8252251883Speter** is for the SQL statement being evaluated. 8253251883Speter** 8254251883Speter** Note that the [SQLITE_IGNORE] constant is also used as a potential 8255251883Speter** return value from the [sqlite3_set_authorizer()] callback and that 8256251883Speter** [SQLITE_ABORT] is also a [result code]. 8257251883Speter*/ 8258251883Speter#define SQLITE_ROLLBACK 1 8259251883Speter/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */ 8260251883Speter#define SQLITE_FAIL 3 8261251883Speter/* #define SQLITE_ABORT 4 // Also an error code */ 8262251883Speter#define SQLITE_REPLACE 5 8263251883Speter 8264282328Sbapt/* 8265282328Sbapt** CAPI3REF: Prepared Statement Scan Status Opcodes 8266282328Sbapt** KEYWORDS: {scanstatus options} 8267282328Sbapt** 8268282328Sbapt** The following constants can be used for the T parameter to the 8269282328Sbapt** [sqlite3_stmt_scanstatus(S,X,T,V)] interface. Each constant designates a 8270282328Sbapt** different metric for sqlite3_stmt_scanstatus() to return. 8271282328Sbapt** 8272282328Sbapt** When the value returned to V is a string, space to hold that string is 8273282328Sbapt** managed by the prepared statement S and will be automatically freed when 8274282328Sbapt** S is finalized. 8275282328Sbapt** 8276282328Sbapt** <dl> 8277282328Sbapt** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt> 8278282328Sbapt** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be 8279282328Sbapt** set to the total number of times that the X-th loop has run.</dd> 8280282328Sbapt** 8281282328Sbapt** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt> 8282282328Sbapt** <dd>^The [sqlite3_int64] variable pointed to by the T parameter will be set 8283282328Sbapt** to the total number of rows examined by all iterations of the X-th loop.</dd> 8284282328Sbapt** 8285282328Sbapt** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt> 8286282328Sbapt** <dd>^The "double" variable pointed to by the T parameter will be set to the 8287282328Sbapt** query planner's estimate for the average number of rows output from each 8288282328Sbapt** iteration of the X-th loop. If the query planner's estimates was accurate, 8289282328Sbapt** then this value will approximate the quotient NVISIT/NLOOP and the 8290282328Sbapt** product of this value for all prior loops with the same SELECTID will 8291282328Sbapt** be the NLOOP value for the current loop. 8292282328Sbapt** 8293282328Sbapt** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt> 8294282328Sbapt** <dd>^The "const char *" variable pointed to by the T parameter will be set 8295282328Sbapt** to a zero-terminated UTF-8 string containing the name of the index or table 8296282328Sbapt** used for the X-th loop. 8297282328Sbapt** 8298282328Sbapt** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt> 8299282328Sbapt** <dd>^The "const char *" variable pointed to by the T parameter will be set 8300282328Sbapt** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN] 8301282328Sbapt** description for the X-th loop. 8302282328Sbapt** 8303282328Sbapt** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt> 8304282328Sbapt** <dd>^The "int" variable pointed to by the T parameter will be set to the 8305282328Sbapt** "select-id" for the X-th loop. The select-id identifies which query or 8306282328Sbapt** subquery the loop is part of. The main query has a select-id of zero. 8307282328Sbapt** The select-id is the same value as is output in the first column 8308282328Sbapt** of an [EXPLAIN QUERY PLAN] query. 8309282328Sbapt** </dl> 8310282328Sbapt*/ 8311282328Sbapt#define SQLITE_SCANSTAT_NLOOP 0 8312282328Sbapt#define SQLITE_SCANSTAT_NVISIT 1 8313282328Sbapt#define SQLITE_SCANSTAT_EST 2 8314282328Sbapt#define SQLITE_SCANSTAT_NAME 3 8315282328Sbapt#define SQLITE_SCANSTAT_EXPLAIN 4 8316282328Sbapt#define SQLITE_SCANSTAT_SELECTID 5 8317251883Speter 8318282328Sbapt/* 8319282328Sbapt** CAPI3REF: Prepared Statement Scan Status 8320286510Speter** METHOD: sqlite3_stmt 8321282328Sbapt** 8322282328Sbapt** This interface returns information about the predicted and measured 8323282328Sbapt** performance for pStmt. Advanced applications can use this 8324282328Sbapt** interface to compare the predicted and the measured performance and 8325282328Sbapt** issue warnings and/or rerun [ANALYZE] if discrepancies are found. 8326282328Sbapt** 8327282328Sbapt** Since this interface is expected to be rarely used, it is only 8328282328Sbapt** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS] 8329282328Sbapt** compile-time option. 8330282328Sbapt** 8331282328Sbapt** The "iScanStatusOp" parameter determines which status information to return. 8332282328Sbapt** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior 8333282328Sbapt** of this interface is undefined. 8334282328Sbapt** ^The requested measurement is written into a variable pointed to by 8335282328Sbapt** the "pOut" parameter. 8336282328Sbapt** Parameter "idx" identifies the specific loop to retrieve statistics for. 8337282328Sbapt** Loops are numbered starting from zero. ^If idx is out of range - less than 8338282328Sbapt** zero or greater than or equal to the total number of loops used to implement 8339282328Sbapt** the statement - a non-zero value is returned and the variable that pOut 8340282328Sbapt** points to is unchanged. 8341282328Sbapt** 8342282328Sbapt** ^Statistics might not be available for all loops in all statements. ^In cases 8343282328Sbapt** where there exist loops with no available statistics, this function behaves 8344282328Sbapt** as if the loop did not exist - it returns non-zero and leave the variable 8345282328Sbapt** that pOut points to unchanged. 8346282328Sbapt** 8347282328Sbapt** See also: [sqlite3_stmt_scanstatus_reset()] 8348282328Sbapt*/ 8349322444SpeterSQLITE_API int sqlite3_stmt_scanstatus( 8350282328Sbapt sqlite3_stmt *pStmt, /* Prepared statement for which info desired */ 8351282328Sbapt int idx, /* Index of loop to report on */ 8352282328Sbapt int iScanStatusOp, /* Information desired. SQLITE_SCANSTAT_* */ 8353282328Sbapt void *pOut /* Result written here */ 8354282328Sbapt); 8355251883Speter 8356251883Speter/* 8357282328Sbapt** CAPI3REF: Zero Scan-Status Counters 8358286510Speter** METHOD: sqlite3_stmt 8359282328Sbapt** 8360282328Sbapt** ^Zero all [sqlite3_stmt_scanstatus()] related event counters. 8361282328Sbapt** 8362282328Sbapt** This API is only available if the library is built with pre-processor 8363282328Sbapt** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined. 8364282328Sbapt*/ 8365322444SpeterSQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*); 8366282328Sbapt 8367298161Sbapt/* 8368298161Sbapt** CAPI3REF: Flush caches to disk mid-transaction 8369298161Sbapt** 8370298161Sbapt** ^If a write-transaction is open on [database connection] D when the 8371298161Sbapt** [sqlite3_db_cacheflush(D)] interface invoked, any dirty 8372298161Sbapt** pages in the pager-cache that are not currently in use are written out 8373298161Sbapt** to disk. A dirty page may be in use if a database cursor created by an 8374298161Sbapt** active SQL statement is reading from it, or if it is page 1 of a database 8375298161Sbapt** file (page 1 is always "in use"). ^The [sqlite3_db_cacheflush(D)] 8376298161Sbapt** interface flushes caches for all schemas - "main", "temp", and 8377298161Sbapt** any [attached] databases. 8378298161Sbapt** 8379298161Sbapt** ^If this function needs to obtain extra database locks before dirty pages 8380298161Sbapt** can be flushed to disk, it does so. ^If those locks cannot be obtained 8381298161Sbapt** immediately and there is a busy-handler callback configured, it is invoked 8382298161Sbapt** in the usual manner. ^If the required lock still cannot be obtained, then 8383298161Sbapt** the database is skipped and an attempt made to flush any dirty pages 8384298161Sbapt** belonging to the next (if any) database. ^If any databases are skipped 8385298161Sbapt** because locks cannot be obtained, but no other error occurs, this 8386298161Sbapt** function returns SQLITE_BUSY. 8387298161Sbapt** 8388298161Sbapt** ^If any other error occurs while flushing dirty pages to disk (for 8389298161Sbapt** example an IO error or out-of-memory condition), then processing is 8390298161Sbapt** abandoned and an SQLite [error code] is returned to the caller immediately. 8391298161Sbapt** 8392298161Sbapt** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK. 8393298161Sbapt** 8394298161Sbapt** ^This function does not set the database handle error code or message 8395298161Sbapt** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions. 8396298161Sbapt*/ 8397322444SpeterSQLITE_API int sqlite3_db_cacheflush(sqlite3*); 8398282328Sbapt 8399282328Sbapt/* 8400305002Scy** CAPI3REF: The pre-update hook. 8401305002Scy** 8402305002Scy** ^These interfaces are only available if SQLite is compiled using the 8403305002Scy** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option. 8404305002Scy** 8405305002Scy** ^The [sqlite3_preupdate_hook()] interface registers a callback function 8406305002Scy** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation 8407322444Speter** on a database table. 8408305002Scy** ^At most one preupdate hook may be registered at a time on a single 8409305002Scy** [database connection]; each call to [sqlite3_preupdate_hook()] overrides 8410305002Scy** the previous setting. 8411305002Scy** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()] 8412305002Scy** with a NULL pointer as the second parameter. 8413305002Scy** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as 8414305002Scy** the first parameter to callbacks. 8415305002Scy** 8416322444Speter** ^The preupdate hook only fires for changes to real database tables; the 8417322444Speter** preupdate hook is not invoked for changes to [virtual tables] or to 8418322444Speter** system tables like sqlite_master or sqlite_stat1. 8419305002Scy** 8420305002Scy** ^The second parameter to the preupdate callback is a pointer to 8421305002Scy** the [database connection] that registered the preupdate hook. 8422305002Scy** ^The third parameter to the preupdate callback is one of the constants 8423305002Scy** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the 8424305002Scy** kind of update operation that is about to occur. 8425305002Scy** ^(The fourth parameter to the preupdate callback is the name of the 8426305002Scy** database within the database connection that is being modified. This 8427305002Scy** will be "main" for the main database or "temp" for TEMP tables or 8428305002Scy** the name given after the AS keyword in the [ATTACH] statement for attached 8429305002Scy** databases.)^ 8430305002Scy** ^The fifth parameter to the preupdate callback is the name of the 8431305002Scy** table that is being modified. 8432305002Scy** 8433322444Speter** For an UPDATE or DELETE operation on a [rowid table], the sixth 8434322444Speter** parameter passed to the preupdate callback is the initial [rowid] of the 8435322444Speter** row being modified or deleted. For an INSERT operation on a rowid table, 8436322444Speter** or any operation on a WITHOUT ROWID table, the value of the sixth 8437322444Speter** parameter is undefined. For an INSERT or UPDATE on a rowid table the 8438322444Speter** seventh parameter is the final rowid value of the row being inserted 8439322444Speter** or updated. The value of the seventh parameter passed to the callback 8440322444Speter** function is not defined for operations on WITHOUT ROWID tables, or for 8441322444Speter** INSERT operations on rowid tables. 8442322444Speter** 8443305002Scy** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()], 8444305002Scy** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces 8445305002Scy** provide additional information about a preupdate event. These routines 8446305002Scy** may only be called from within a preupdate callback. Invoking any of 8447305002Scy** these routines from outside of a preupdate callback or with a 8448305002Scy** [database connection] pointer that is different from the one supplied 8449305002Scy** to the preupdate callback results in undefined and probably undesirable 8450305002Scy** behavior. 8451305002Scy** 8452305002Scy** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns 8453305002Scy** in the row that is being inserted, updated, or deleted. 8454305002Scy** 8455305002Scy** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to 8456305002Scy** a [protected sqlite3_value] that contains the value of the Nth column of 8457305002Scy** the table row before it is updated. The N parameter must be between 0 8458305002Scy** and one less than the number of columns or the behavior will be 8459305002Scy** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE 8460305002Scy** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the 8461305002Scy** behavior is undefined. The [sqlite3_value] that P points to 8462305002Scy** will be destroyed when the preupdate callback returns. 8463305002Scy** 8464305002Scy** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to 8465305002Scy** a [protected sqlite3_value] that contains the value of the Nth column of 8466305002Scy** the table row after it is updated. The N parameter must be between 0 8467305002Scy** and one less than the number of columns or the behavior will be 8468305002Scy** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE 8469305002Scy** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the 8470305002Scy** behavior is undefined. The [sqlite3_value] that P points to 8471305002Scy** will be destroyed when the preupdate callback returns. 8472305002Scy** 8473305002Scy** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate 8474305002Scy** callback was invoked as a result of a direct insert, update, or delete 8475305002Scy** operation; or 1 for inserts, updates, or deletes invoked by top-level 8476305002Scy** triggers; or 2 for changes resulting from triggers called by top-level 8477305002Scy** triggers; and so forth. 8478305002Scy** 8479305002Scy** See also: [sqlite3_update_hook()] 8480305002Scy*/ 8481322444Speter#if defined(SQLITE_ENABLE_PREUPDATE_HOOK) 8482322444SpeterSQLITE_API void *sqlite3_preupdate_hook( 8483305002Scy sqlite3 *db, 8484305002Scy void(*xPreUpdate)( 8485305002Scy void *pCtx, /* Copy of third arg to preupdate_hook() */ 8486305002Scy sqlite3 *db, /* Database handle */ 8487305002Scy int op, /* SQLITE_UPDATE, DELETE or INSERT */ 8488305002Scy char const *zDb, /* Database name */ 8489305002Scy char const *zName, /* Table name */ 8490305002Scy sqlite3_int64 iKey1, /* Rowid of row about to be deleted/updated */ 8491305002Scy sqlite3_int64 iKey2 /* New rowid value (for a rowid UPDATE) */ 8492305002Scy ), 8493305002Scy void* 8494305002Scy); 8495322444SpeterSQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **); 8496322444SpeterSQLITE_API int sqlite3_preupdate_count(sqlite3 *); 8497322444SpeterSQLITE_API int sqlite3_preupdate_depth(sqlite3 *); 8498322444SpeterSQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **); 8499322444Speter#endif 8500305002Scy 8501305002Scy/* 8502298161Sbapt** CAPI3REF: Low-level system error code 8503298161Sbapt** 8504298161Sbapt** ^Attempt to return the underlying operating system error code or error 8505305002Scy** number that caused the most recent I/O error or failure to open a file. 8506298161Sbapt** The return value is OS-dependent. For example, on unix systems, after 8507298161Sbapt** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be 8508298161Sbapt** called to get back the underlying "errno" that caused the problem, such 8509298161Sbapt** as ENOSPC, EAUTH, EISDIR, and so forth. 8510298161Sbapt*/ 8511322444SpeterSQLITE_API int sqlite3_system_errno(sqlite3*); 8512298161Sbapt 8513298161Sbapt/* 8514298161Sbapt** CAPI3REF: Database Snapshot 8515322444Speter** KEYWORDS: {snapshot} {sqlite3_snapshot} 8516298161Sbapt** EXPERIMENTAL 8517298161Sbapt** 8518298161Sbapt** An instance of the snapshot object records the state of a [WAL mode] 8519298161Sbapt** database for some specific point in history. 8520298161Sbapt** 8521298161Sbapt** In [WAL mode], multiple [database connections] that are open on the 8522298161Sbapt** same database file can each be reading a different historical version 8523298161Sbapt** of the database file. When a [database connection] begins a read 8524298161Sbapt** transaction, that connection sees an unchanging copy of the database 8525298161Sbapt** as it existed for the point in time when the transaction first started. 8526298161Sbapt** Subsequent changes to the database from other connections are not seen 8527298161Sbapt** by the reader until a new read transaction is started. 8528298161Sbapt** 8529298161Sbapt** The sqlite3_snapshot object records state information about an historical 8530298161Sbapt** version of the database file so that it is possible to later open a new read 8531298161Sbapt** transaction that sees that historical version of the database rather than 8532298161Sbapt** the most recent version. 8533298161Sbapt** 8534298161Sbapt** The constructor for this object is [sqlite3_snapshot_get()]. The 8535298161Sbapt** [sqlite3_snapshot_open()] method causes a fresh read transaction to refer 8536298161Sbapt** to an historical snapshot (if possible). The destructor for 8537298161Sbapt** sqlite3_snapshot objects is [sqlite3_snapshot_free()]. 8538298161Sbapt*/ 8539322444Spetertypedef struct sqlite3_snapshot { 8540322444Speter unsigned char hidden[48]; 8541322444Speter} sqlite3_snapshot; 8542298161Sbapt 8543298161Sbapt/* 8544298161Sbapt** CAPI3REF: Record A Database Snapshot 8545298161Sbapt** EXPERIMENTAL 8546298161Sbapt** 8547298161Sbapt** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a 8548298161Sbapt** new [sqlite3_snapshot] object that records the current state of 8549298161Sbapt** schema S in database connection D. ^On success, the 8550298161Sbapt** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly 8551298161Sbapt** created [sqlite3_snapshot] object into *P and returns SQLITE_OK. 8552322444Speter** If there is not already a read-transaction open on schema S when 8553322444Speter** this function is called, one is opened automatically. 8554298161Sbapt** 8555322444Speter** The following must be true for this function to succeed. If any of 8556322444Speter** the following statements are false when sqlite3_snapshot_get() is 8557322444Speter** called, SQLITE_ERROR is returned. The final value of *P is undefined 8558322444Speter** in this case. 8559322444Speter** 8560322444Speter** <ul> 8561322444Speter** <li> The database handle must be in [autocommit mode]. 8562322444Speter** 8563322444Speter** <li> Schema S of [database connection] D must be a [WAL mode] database. 8564322444Speter** 8565322444Speter** <li> There must not be a write transaction open on schema S of database 8566322444Speter** connection D. 8567322444Speter** 8568322444Speter** <li> One or more transactions must have been written to the current wal 8569322444Speter** file since it was created on disk (by any connection). This means 8570322444Speter** that a snapshot cannot be taken on a wal mode database with no wal 8571322444Speter** file immediately after it is first opened. At least one transaction 8572322444Speter** must be written to it first. 8573322444Speter** </ul> 8574322444Speter** 8575322444Speter** This function may also return SQLITE_NOMEM. If it is called with the 8576322444Speter** database handle in autocommit mode but fails for some other reason, 8577322444Speter** whether or not a read transaction is opened on schema S is undefined. 8578322444Speter** 8579298161Sbapt** The [sqlite3_snapshot] object returned from a successful call to 8580298161Sbapt** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()] 8581298161Sbapt** to avoid a memory leak. 8582298161Sbapt** 8583298161Sbapt** The [sqlite3_snapshot_get()] interface is only available when the 8584298161Sbapt** SQLITE_ENABLE_SNAPSHOT compile-time option is used. 8585298161Sbapt*/ 8586322444SpeterSQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get( 8587298161Sbapt sqlite3 *db, 8588298161Sbapt const char *zSchema, 8589298161Sbapt sqlite3_snapshot **ppSnapshot 8590298161Sbapt); 8591298161Sbapt 8592298161Sbapt/* 8593298161Sbapt** CAPI3REF: Start a read transaction on an historical snapshot 8594298161Sbapt** EXPERIMENTAL 8595298161Sbapt** 8596305002Scy** ^The [sqlite3_snapshot_open(D,S,P)] interface starts a 8597305002Scy** read transaction for schema S of 8598305002Scy** [database connection] D such that the read transaction 8599305002Scy** refers to historical [snapshot] P, rather than the most 8600305002Scy** recent change to the database. 8601298161Sbapt** ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK on success 8602298161Sbapt** or an appropriate [error code] if it fails. 8603298161Sbapt** 8604298161Sbapt** ^In order to succeed, a call to [sqlite3_snapshot_open(D,S,P)] must be 8605305002Scy** the first operation following the [BEGIN] that takes the schema S 8606305002Scy** out of [autocommit mode]. 8607305002Scy** ^In other words, schema S must not currently be in 8608305002Scy** a transaction for [sqlite3_snapshot_open(D,S,P)] to work, but the 8609305002Scy** database connection D must be out of [autocommit mode]. 8610305002Scy** ^A [snapshot] will fail to open if it has been overwritten by a 8611298161Sbapt** [checkpoint]. 8612305002Scy** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the 8613305002Scy** database connection D does not know that the database file for 8614305002Scy** schema S is in [WAL mode]. A database connection might not know 8615305002Scy** that the database file is in [WAL mode] if there has been no prior 8616305002Scy** I/O on that database connection, or if the database entered [WAL mode] 8617305002Scy** after the most recent I/O on the database connection.)^ 8618305002Scy** (Hint: Run "[PRAGMA application_id]" against a newly opened 8619298161Sbapt** database connection in order to make it ready to use snapshots.) 8620298161Sbapt** 8621298161Sbapt** The [sqlite3_snapshot_open()] interface is only available when the 8622298161Sbapt** SQLITE_ENABLE_SNAPSHOT compile-time option is used. 8623298161Sbapt*/ 8624322444SpeterSQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open( 8625298161Sbapt sqlite3 *db, 8626298161Sbapt const char *zSchema, 8627298161Sbapt sqlite3_snapshot *pSnapshot 8628298161Sbapt); 8629298161Sbapt 8630298161Sbapt/* 8631298161Sbapt** CAPI3REF: Destroy a snapshot 8632298161Sbapt** EXPERIMENTAL 8633298161Sbapt** 8634298161Sbapt** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P. 8635298161Sbapt** The application must eventually free every [sqlite3_snapshot] object 8636298161Sbapt** using this routine to avoid a memory leak. 8637298161Sbapt** 8638298161Sbapt** The [sqlite3_snapshot_free()] interface is only available when the 8639298161Sbapt** SQLITE_ENABLE_SNAPSHOT compile-time option is used. 8640298161Sbapt*/ 8641322444SpeterSQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*); 8642298161Sbapt 8643298161Sbapt/* 8644305002Scy** CAPI3REF: Compare the ages of two snapshot handles. 8645305002Scy** EXPERIMENTAL 8646305002Scy** 8647305002Scy** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages 8648305002Scy** of two valid snapshot handles. 8649305002Scy** 8650305002Scy** If the two snapshot handles are not associated with the same database 8651305002Scy** file, the result of the comparison is undefined. 8652305002Scy** 8653305002Scy** Additionally, the result of the comparison is only valid if both of the 8654305002Scy** snapshot handles were obtained by calling sqlite3_snapshot_get() since the 8655305002Scy** last time the wal file was deleted. The wal file is deleted when the 8656305002Scy** database is changed back to rollback mode or when the number of database 8657305002Scy** clients drops to zero. If either snapshot handle was obtained before the 8658305002Scy** wal file was last deleted, the value returned by this function 8659305002Scy** is undefined. 8660305002Scy** 8661305002Scy** Otherwise, this API returns a negative value if P1 refers to an older 8662305002Scy** snapshot than P2, zero if the two handles refer to the same database 8663305002Scy** snapshot, and a positive value if P1 is a newer snapshot than P2. 8664305002Scy*/ 8665322444SpeterSQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp( 8666305002Scy sqlite3_snapshot *p1, 8667305002Scy sqlite3_snapshot *p2 8668305002Scy); 8669305002Scy 8670305002Scy/* 8671322444Speter** CAPI3REF: Recover snapshots from a wal file 8672322444Speter** EXPERIMENTAL 8673322444Speter** 8674322444Speter** If all connections disconnect from a database file but do not perform 8675322444Speter** a checkpoint, the existing wal file is opened along with the database 8676322444Speter** file the next time the database is opened. At this point it is only 8677322444Speter** possible to successfully call sqlite3_snapshot_open() to open the most 8678322444Speter** recent snapshot of the database (the one at the head of the wal file), 8679322444Speter** even though the wal file may contain other valid snapshots for which 8680322444Speter** clients have sqlite3_snapshot handles. 8681322444Speter** 8682322444Speter** This function attempts to scan the wal file associated with database zDb 8683322444Speter** of database handle db and make all valid snapshots available to 8684322444Speter** sqlite3_snapshot_open(). It is an error if there is already a read 8685322444Speter** transaction open on the database, or if the database is not a wal mode 8686322444Speter** database. 8687322444Speter** 8688322444Speter** SQLITE_OK is returned if successful, or an SQLite error code otherwise. 8689322444Speter*/ 8690322444SpeterSQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb); 8691322444Speter 8692322444Speter/* 8693251883Speter** Undo the hack that converts floating point types to integer for 8694251883Speter** builds on processors without floating point support. 8695251883Speter*/ 8696251883Speter#ifdef SQLITE_OMIT_FLOATING_POINT 8697251883Speter# undef double 8698251883Speter#endif 8699251883Speter 8700251883Speter#ifdef __cplusplus 8701251883Speter} /* End of the 'extern "C"' block */ 8702251883Speter#endif 8703305002Scy#endif /* SQLITE3_H */ 8704251883Speter 8705305002Scy/******** Begin file sqlite3rtree.h *********/ 8706251883Speter/* 8707251883Speter** 2010 August 30 8708251883Speter** 8709251883Speter** The author disclaims copyright to this source code. In place of 8710251883Speter** a legal notice, here is a blessing: 8711251883Speter** 8712251883Speter** May you do good and not evil. 8713251883Speter** May you find forgiveness for yourself and forgive others. 8714251883Speter** May you share freely, never taking more than you give. 8715251883Speter** 8716251883Speter************************************************************************* 8717251883Speter*/ 8718251883Speter 8719251883Speter#ifndef _SQLITE3RTREE_H_ 8720251883Speter#define _SQLITE3RTREE_H_ 8721251883Speter 8722251883Speter 8723251883Speter#ifdef __cplusplus 8724251883Speterextern "C" { 8725251883Speter#endif 8726251883Speter 8727251883Spetertypedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry; 8728269851Spetertypedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info; 8729251883Speter 8730269851Speter/* The double-precision datatype used by RTree depends on the 8731269851Speter** SQLITE_RTREE_INT_ONLY compile-time option. 8732269851Speter*/ 8733269851Speter#ifdef SQLITE_RTREE_INT_ONLY 8734269851Speter typedef sqlite3_int64 sqlite3_rtree_dbl; 8735269851Speter#else 8736269851Speter typedef double sqlite3_rtree_dbl; 8737269851Speter#endif 8738269851Speter 8739251883Speter/* 8740251883Speter** Register a geometry callback named zGeom that can be used as part of an 8741251883Speter** R-Tree geometry query as follows: 8742251883Speter** 8743251883Speter** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...) 8744251883Speter*/ 8745322444SpeterSQLITE_API int sqlite3_rtree_geometry_callback( 8746251883Speter sqlite3 *db, 8747251883Speter const char *zGeom, 8748269851Speter int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*), 8749251883Speter void *pContext 8750251883Speter); 8751251883Speter 8752251883Speter 8753251883Speter/* 8754251883Speter** A pointer to a structure of the following type is passed as the first 8755251883Speter** argument to callbacks registered using rtree_geometry_callback(). 8756251883Speter*/ 8757251883Speterstruct sqlite3_rtree_geometry { 8758251883Speter void *pContext; /* Copy of pContext passed to s_r_g_c() */ 8759251883Speter int nParam; /* Size of array aParam[] */ 8760269851Speter sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */ 8761251883Speter void *pUser; /* Callback implementation user data */ 8762251883Speter void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */ 8763251883Speter}; 8764251883Speter 8765269851Speter/* 8766269851Speter** Register a 2nd-generation geometry callback named zScore that can be 8767269851Speter** used as part of an R-Tree geometry query as follows: 8768269851Speter** 8769269851Speter** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...) 8770269851Speter*/ 8771322444SpeterSQLITE_API int sqlite3_rtree_query_callback( 8772269851Speter sqlite3 *db, 8773269851Speter const char *zQueryFunc, 8774269851Speter int (*xQueryFunc)(sqlite3_rtree_query_info*), 8775269851Speter void *pContext, 8776269851Speter void (*xDestructor)(void*) 8777269851Speter); 8778251883Speter 8779269851Speter 8780269851Speter/* 8781269851Speter** A pointer to a structure of the following type is passed as the 8782269851Speter** argument to scored geometry callback registered using 8783269851Speter** sqlite3_rtree_query_callback(). 8784269851Speter** 8785269851Speter** Note that the first 5 fields of this structure are identical to 8786269851Speter** sqlite3_rtree_geometry. This structure is a subclass of 8787269851Speter** sqlite3_rtree_geometry. 8788269851Speter*/ 8789269851Speterstruct sqlite3_rtree_query_info { 8790269851Speter void *pContext; /* pContext from when function registered */ 8791269851Speter int nParam; /* Number of function parameters */ 8792269851Speter sqlite3_rtree_dbl *aParam; /* value of function parameters */ 8793269851Speter void *pUser; /* callback can use this, if desired */ 8794269851Speter void (*xDelUser)(void*); /* function to free pUser */ 8795269851Speter sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */ 8796269851Speter unsigned int *anQueue; /* Number of pending entries in the queue */ 8797269851Speter int nCoord; /* Number of coordinates */ 8798269851Speter int iLevel; /* Level of current node or entry */ 8799269851Speter int mxLevel; /* The largest iLevel value in the tree */ 8800269851Speter sqlite3_int64 iRowid; /* Rowid for current entry */ 8801269851Speter sqlite3_rtree_dbl rParentScore; /* Score of parent node */ 8802269851Speter int eParentWithin; /* Visibility of parent node */ 8803269851Speter int eWithin; /* OUT: Visiblity */ 8804269851Speter sqlite3_rtree_dbl rScore; /* OUT: Write the score here */ 8805286510Speter /* The following fields are only available in 3.8.11 and later */ 8806286510Speter sqlite3_value **apSqlParam; /* Original SQL values of parameters */ 8807269851Speter}; 8808269851Speter 8809269851Speter/* 8810269851Speter** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin. 8811269851Speter*/ 8812269851Speter#define NOT_WITHIN 0 /* Object completely outside of query region */ 8813269851Speter#define PARTLY_WITHIN 1 /* Object partially overlaps query region */ 8814269851Speter#define FULLY_WITHIN 2 /* Object fully contained within query region */ 8815269851Speter 8816269851Speter 8817251883Speter#ifdef __cplusplus 8818251883Speter} /* end of the 'extern "C"' block */ 8819251883Speter#endif 8820251883Speter 8821251883Speter#endif /* ifndef _SQLITE3RTREE_H_ */ 8822251883Speter 8823305002Scy/******** End of sqlite3rtree.h *********/ 8824305002Scy/******** Begin file sqlite3session.h *********/ 8825305002Scy 8826305002Scy#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) 8827305002Scy#define __SQLITESESSION_H_ 1 8828305002Scy 8829298161Sbapt/* 8830305002Scy** Make sure we can call this stuff from C++. 8831305002Scy*/ 8832305002Scy#ifdef __cplusplus 8833305002Scyextern "C" { 8834305002Scy#endif 8835305002Scy 8836305002Scy 8837305002Scy/* 8838305002Scy** CAPI3REF: Session Object Handle 8839305002Scy*/ 8840305002Scytypedef struct sqlite3_session sqlite3_session; 8841305002Scy 8842305002Scy/* 8843305002Scy** CAPI3REF: Changeset Iterator Handle 8844305002Scy*/ 8845305002Scytypedef struct sqlite3_changeset_iter sqlite3_changeset_iter; 8846305002Scy 8847305002Scy/* 8848305002Scy** CAPI3REF: Create A New Session Object 8849305002Scy** 8850305002Scy** Create a new session object attached to database handle db. If successful, 8851305002Scy** a pointer to the new object is written to *ppSession and SQLITE_OK is 8852305002Scy** returned. If an error occurs, *ppSession is set to NULL and an SQLite 8853305002Scy** error code (e.g. SQLITE_NOMEM) is returned. 8854305002Scy** 8855305002Scy** It is possible to create multiple session objects attached to a single 8856305002Scy** database handle. 8857305002Scy** 8858305002Scy** Session objects created using this function should be deleted using the 8859305002Scy** [sqlite3session_delete()] function before the database handle that they 8860305002Scy** are attached to is itself closed. If the database handle is closed before 8861305002Scy** the session object is deleted, then the results of calling any session 8862305002Scy** module function, including [sqlite3session_delete()] on the session object 8863305002Scy** are undefined. 8864305002Scy** 8865305002Scy** Because the session module uses the [sqlite3_preupdate_hook()] API, it 8866305002Scy** is not possible for an application to register a pre-update hook on a 8867305002Scy** database handle that has one or more session objects attached. Nor is 8868305002Scy** it possible to create a session object attached to a database handle for 8869305002Scy** which a pre-update hook is already defined. The results of attempting 8870305002Scy** either of these things are undefined. 8871305002Scy** 8872305002Scy** The session object will be used to create changesets for tables in 8873305002Scy** database zDb, where zDb is either "main", or "temp", or the name of an 8874305002Scy** attached database. It is not an error if database zDb is not attached 8875305002Scy** to the database when the session object is created. 8876305002Scy*/ 8877322444SpeterSQLITE_API int sqlite3session_create( 8878305002Scy sqlite3 *db, /* Database handle */ 8879305002Scy const char *zDb, /* Name of db (e.g. "main") */ 8880305002Scy sqlite3_session **ppSession /* OUT: New session object */ 8881305002Scy); 8882305002Scy 8883305002Scy/* 8884305002Scy** CAPI3REF: Delete A Session Object 8885305002Scy** 8886305002Scy** Delete a session object previously allocated using 8887305002Scy** [sqlite3session_create()]. Once a session object has been deleted, the 8888305002Scy** results of attempting to use pSession with any other session module 8889305002Scy** function are undefined. 8890305002Scy** 8891305002Scy** Session objects must be deleted before the database handle to which they 8892305002Scy** are attached is closed. Refer to the documentation for 8893305002Scy** [sqlite3session_create()] for details. 8894305002Scy*/ 8895322444SpeterSQLITE_API void sqlite3session_delete(sqlite3_session *pSession); 8896305002Scy 8897305002Scy 8898305002Scy/* 8899305002Scy** CAPI3REF: Enable Or Disable A Session Object 8900305002Scy** 8901305002Scy** Enable or disable the recording of changes by a session object. When 8902305002Scy** enabled, a session object records changes made to the database. When 8903305002Scy** disabled - it does not. A newly created session object is enabled. 8904305002Scy** Refer to the documentation for [sqlite3session_changeset()] for further 8905305002Scy** details regarding how enabling and disabling a session object affects 8906305002Scy** the eventual changesets. 8907305002Scy** 8908305002Scy** Passing zero to this function disables the session. Passing a value 8909305002Scy** greater than zero enables it. Passing a value less than zero is a 8910305002Scy** no-op, and may be used to query the current state of the session. 8911305002Scy** 8912305002Scy** The return value indicates the final state of the session object: 0 if 8913305002Scy** the session is disabled, or 1 if it is enabled. 8914305002Scy*/ 8915322444SpeterSQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable); 8916305002Scy 8917305002Scy/* 8918305002Scy** CAPI3REF: Set Or Clear the Indirect Change Flag 8919305002Scy** 8920305002Scy** Each change recorded by a session object is marked as either direct or 8921305002Scy** indirect. A change is marked as indirect if either: 8922305002Scy** 8923305002Scy** <ul> 8924305002Scy** <li> The session object "indirect" flag is set when the change is 8925305002Scy** made, or 8926305002Scy** <li> The change is made by an SQL trigger or foreign key action 8927305002Scy** instead of directly as a result of a users SQL statement. 8928305002Scy** </ul> 8929305002Scy** 8930305002Scy** If a single row is affected by more than one operation within a session, 8931305002Scy** then the change is considered indirect if all operations meet the criteria 8932305002Scy** for an indirect change above, or direct otherwise. 8933305002Scy** 8934305002Scy** This function is used to set, clear or query the session object indirect 8935305002Scy** flag. If the second argument passed to this function is zero, then the 8936305002Scy** indirect flag is cleared. If it is greater than zero, the indirect flag 8937305002Scy** is set. Passing a value less than zero does not modify the current value 8938305002Scy** of the indirect flag, and may be used to query the current state of the 8939305002Scy** indirect flag for the specified session object. 8940305002Scy** 8941305002Scy** The return value indicates the final state of the indirect flag: 0 if 8942305002Scy** it is clear, or 1 if it is set. 8943305002Scy*/ 8944322444SpeterSQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect); 8945305002Scy 8946305002Scy/* 8947305002Scy** CAPI3REF: Attach A Table To A Session Object 8948305002Scy** 8949305002Scy** If argument zTab is not NULL, then it is the name of a table to attach 8950305002Scy** to the session object passed as the first argument. All subsequent changes 8951305002Scy** made to the table while the session object is enabled will be recorded. See 8952305002Scy** documentation for [sqlite3session_changeset()] for further details. 8953305002Scy** 8954305002Scy** Or, if argument zTab is NULL, then changes are recorded for all tables 8955305002Scy** in the database. If additional tables are added to the database (by 8956305002Scy** executing "CREATE TABLE" statements) after this call is made, changes for 8957305002Scy** the new tables are also recorded. 8958305002Scy** 8959305002Scy** Changes can only be recorded for tables that have a PRIMARY KEY explicitly 8960305002Scy** defined as part of their CREATE TABLE statement. It does not matter if the 8961305002Scy** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY 8962305002Scy** KEY may consist of a single column, or may be a composite key. 8963305002Scy** 8964305002Scy** It is not an error if the named table does not exist in the database. Nor 8965305002Scy** is it an error if the named table does not have a PRIMARY KEY. However, 8966305002Scy** no changes will be recorded in either of these scenarios. 8967305002Scy** 8968305002Scy** Changes are not recorded for individual rows that have NULL values stored 8969305002Scy** in one or more of their PRIMARY KEY columns. 8970305002Scy** 8971305002Scy** SQLITE_OK is returned if the call completes without error. Or, if an error 8972305002Scy** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned. 8973305002Scy*/ 8974322444SpeterSQLITE_API int sqlite3session_attach( 8975305002Scy sqlite3_session *pSession, /* Session object */ 8976305002Scy const char *zTab /* Table name */ 8977305002Scy); 8978305002Scy 8979305002Scy/* 8980305002Scy** CAPI3REF: Set a table filter on a Session Object. 8981305002Scy** 8982305002Scy** The second argument (xFilter) is the "filter callback". For changes to rows 8983322444Speter** in tables that are not attached to the Session object, the filter is called 8984305002Scy** to determine whether changes to the table's rows should be tracked or not. 8985305002Scy** If xFilter returns 0, changes is not tracked. Note that once a table is 8986305002Scy** attached, xFilter will not be called again. 8987305002Scy*/ 8988322444SpeterSQLITE_API void sqlite3session_table_filter( 8989305002Scy sqlite3_session *pSession, /* Session object */ 8990305002Scy int(*xFilter)( 8991305002Scy void *pCtx, /* Copy of third arg to _filter_table() */ 8992305002Scy const char *zTab /* Table name */ 8993305002Scy ), 8994305002Scy void *pCtx /* First argument passed to xFilter */ 8995305002Scy); 8996305002Scy 8997305002Scy/* 8998305002Scy** CAPI3REF: Generate A Changeset From A Session Object 8999305002Scy** 9000305002Scy** Obtain a changeset containing changes to the tables attached to the 9001305002Scy** session object passed as the first argument. If successful, 9002305002Scy** set *ppChangeset to point to a buffer containing the changeset 9003305002Scy** and *pnChangeset to the size of the changeset in bytes before returning 9004305002Scy** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to 9005305002Scy** zero and return an SQLite error code. 9006305002Scy** 9007305002Scy** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes, 9008305002Scy** each representing a change to a single row of an attached table. An INSERT 9009305002Scy** change contains the values of each field of a new database row. A DELETE 9010305002Scy** contains the original values of each field of a deleted database row. An 9011305002Scy** UPDATE change contains the original values of each field of an updated 9012305002Scy** database row along with the updated values for each updated non-primary-key 9013305002Scy** column. It is not possible for an UPDATE change to represent a change that 9014305002Scy** modifies the values of primary key columns. If such a change is made, it 9015305002Scy** is represented in a changeset as a DELETE followed by an INSERT. 9016305002Scy** 9017305002Scy** Changes are not recorded for rows that have NULL values stored in one or 9018305002Scy** more of their PRIMARY KEY columns. If such a row is inserted or deleted, 9019305002Scy** no corresponding change is present in the changesets returned by this 9020305002Scy** function. If an existing row with one or more NULL values stored in 9021305002Scy** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL, 9022305002Scy** only an INSERT is appears in the changeset. Similarly, if an existing row 9023305002Scy** with non-NULL PRIMARY KEY values is updated so that one or more of its 9024305002Scy** PRIMARY KEY columns are set to NULL, the resulting changeset contains a 9025305002Scy** DELETE change only. 9026305002Scy** 9027305002Scy** The contents of a changeset may be traversed using an iterator created 9028305002Scy** using the [sqlite3changeset_start()] API. A changeset may be applied to 9029305002Scy** a database with a compatible schema using the [sqlite3changeset_apply()] 9030305002Scy** API. 9031305002Scy** 9032305002Scy** Within a changeset generated by this function, all changes related to a 9033305002Scy** single table are grouped together. In other words, when iterating through 9034305002Scy** a changeset or when applying a changeset to a database, all changes related 9035305002Scy** to a single table are processed before moving on to the next table. Tables 9036305002Scy** are sorted in the same order in which they were attached (or auto-attached) 9037305002Scy** to the sqlite3_session object. The order in which the changes related to 9038305002Scy** a single table are stored is undefined. 9039305002Scy** 9040305002Scy** Following a successful call to this function, it is the responsibility of 9041305002Scy** the caller to eventually free the buffer that *ppChangeset points to using 9042305002Scy** [sqlite3_free()]. 9043305002Scy** 9044305002Scy** <h3>Changeset Generation</h3> 9045305002Scy** 9046305002Scy** Once a table has been attached to a session object, the session object 9047305002Scy** records the primary key values of all new rows inserted into the table. 9048305002Scy** It also records the original primary key and other column values of any 9049305002Scy** deleted or updated rows. For each unique primary key value, data is only 9050305002Scy** recorded once - the first time a row with said primary key is inserted, 9051305002Scy** updated or deleted in the lifetime of the session. 9052305002Scy** 9053305002Scy** There is one exception to the previous paragraph: when a row is inserted, 9054305002Scy** updated or deleted, if one or more of its primary key columns contain a 9055305002Scy** NULL value, no record of the change is made. 9056305002Scy** 9057305002Scy** The session object therefore accumulates two types of records - those 9058305002Scy** that consist of primary key values only (created when the user inserts 9059305002Scy** a new record) and those that consist of the primary key values and the 9060305002Scy** original values of other table columns (created when the users deletes 9061305002Scy** or updates a record). 9062305002Scy** 9063305002Scy** When this function is called, the requested changeset is created using 9064305002Scy** both the accumulated records and the current contents of the database 9065305002Scy** file. Specifically: 9066305002Scy** 9067305002Scy** <ul> 9068305002Scy** <li> For each record generated by an insert, the database is queried 9069305002Scy** for a row with a matching primary key. If one is found, an INSERT 9070305002Scy** change is added to the changeset. If no such row is found, no change 9071305002Scy** is added to the changeset. 9072305002Scy** 9073305002Scy** <li> For each record generated by an update or delete, the database is 9074305002Scy** queried for a row with a matching primary key. If such a row is 9075305002Scy** found and one or more of the non-primary key fields have been 9076305002Scy** modified from their original values, an UPDATE change is added to 9077305002Scy** the changeset. Or, if no such row is found in the table, a DELETE 9078305002Scy** change is added to the changeset. If there is a row with a matching 9079305002Scy** primary key in the database, but all fields contain their original 9080305002Scy** values, no change is added to the changeset. 9081305002Scy** </ul> 9082305002Scy** 9083305002Scy** This means, amongst other things, that if a row is inserted and then later 9084305002Scy** deleted while a session object is active, neither the insert nor the delete 9085305002Scy** will be present in the changeset. Or if a row is deleted and then later a 9086305002Scy** row with the same primary key values inserted while a session object is 9087305002Scy** active, the resulting changeset will contain an UPDATE change instead of 9088305002Scy** a DELETE and an INSERT. 9089305002Scy** 9090305002Scy** When a session object is disabled (see the [sqlite3session_enable()] API), 9091305002Scy** it does not accumulate records when rows are inserted, updated or deleted. 9092305002Scy** This may appear to have some counter-intuitive effects if a single row 9093305002Scy** is written to more than once during a session. For example, if a row 9094305002Scy** is inserted while a session object is enabled, then later deleted while 9095305002Scy** the same session object is disabled, no INSERT record will appear in the 9096305002Scy** changeset, even though the delete took place while the session was disabled. 9097305002Scy** Or, if one field of a row is updated while a session is disabled, and 9098305002Scy** another field of the same row is updated while the session is enabled, the 9099305002Scy** resulting changeset will contain an UPDATE change that updates both fields. 9100305002Scy*/ 9101322444SpeterSQLITE_API int sqlite3session_changeset( 9102305002Scy sqlite3_session *pSession, /* Session object */ 9103305002Scy int *pnChangeset, /* OUT: Size of buffer at *ppChangeset */ 9104305002Scy void **ppChangeset /* OUT: Buffer containing changeset */ 9105305002Scy); 9106305002Scy 9107305002Scy/* 9108305002Scy** CAPI3REF: Load The Difference Between Tables Into A Session 9109305002Scy** 9110305002Scy** If it is not already attached to the session object passed as the first 9111305002Scy** argument, this function attaches table zTbl in the same manner as the 9112305002Scy** [sqlite3session_attach()] function. If zTbl does not exist, or if it 9113305002Scy** does not have a primary key, this function is a no-op (but does not return 9114305002Scy** an error). 9115305002Scy** 9116305002Scy** Argument zFromDb must be the name of a database ("main", "temp" etc.) 9117305002Scy** attached to the same database handle as the session object that contains 9118305002Scy** a table compatible with the table attached to the session by this function. 9119305002Scy** A table is considered compatible if it: 9120305002Scy** 9121305002Scy** <ul> 9122305002Scy** <li> Has the same name, 9123305002Scy** <li> Has the same set of columns declared in the same order, and 9124305002Scy** <li> Has the same PRIMARY KEY definition. 9125305002Scy** </ul> 9126305002Scy** 9127305002Scy** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables 9128305002Scy** are compatible but do not have any PRIMARY KEY columns, it is not an error 9129305002Scy** but no changes are added to the session object. As with other session 9130305002Scy** APIs, tables without PRIMARY KEYs are simply ignored. 9131305002Scy** 9132305002Scy** This function adds a set of changes to the session object that could be 9133305002Scy** used to update the table in database zFrom (call this the "from-table") 9134305002Scy** so that its content is the same as the table attached to the session 9135305002Scy** object (call this the "to-table"). Specifically: 9136305002Scy** 9137305002Scy** <ul> 9138305002Scy** <li> For each row (primary key) that exists in the to-table but not in 9139305002Scy** the from-table, an INSERT record is added to the session object. 9140305002Scy** 9141305002Scy** <li> For each row (primary key) that exists in the to-table but not in 9142305002Scy** the from-table, a DELETE record is added to the session object. 9143305002Scy** 9144305002Scy** <li> For each row (primary key) that exists in both tables, but features 9145322444Speter** different non-PK values in each, an UPDATE record is added to the 9146322444Speter** session. 9147305002Scy** </ul> 9148305002Scy** 9149305002Scy** To clarify, if this function is called and then a changeset constructed 9150305002Scy** using [sqlite3session_changeset()], then after applying that changeset to 9151305002Scy** database zFrom the contents of the two compatible tables would be 9152305002Scy** identical. 9153305002Scy** 9154305002Scy** It an error if database zFrom does not exist or does not contain the 9155305002Scy** required compatible table. 9156305002Scy** 9157305002Scy** If the operation successful, SQLITE_OK is returned. Otherwise, an SQLite 9158305002Scy** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg 9159305002Scy** may be set to point to a buffer containing an English language error 9160305002Scy** message. It is the responsibility of the caller to free this buffer using 9161305002Scy** sqlite3_free(). 9162305002Scy*/ 9163322444SpeterSQLITE_API int sqlite3session_diff( 9164305002Scy sqlite3_session *pSession, 9165305002Scy const char *zFromDb, 9166305002Scy const char *zTbl, 9167305002Scy char **pzErrMsg 9168305002Scy); 9169305002Scy 9170305002Scy 9171305002Scy/* 9172305002Scy** CAPI3REF: Generate A Patchset From A Session Object 9173305002Scy** 9174305002Scy** The differences between a patchset and a changeset are that: 9175305002Scy** 9176305002Scy** <ul> 9177305002Scy** <li> DELETE records consist of the primary key fields only. The 9178305002Scy** original values of other fields are omitted. 9179305002Scy** <li> The original values of any modified fields are omitted from 9180305002Scy** UPDATE records. 9181305002Scy** </ul> 9182305002Scy** 9183305002Scy** A patchset blob may be used with up to date versions of all 9184305002Scy** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(), 9185305002Scy** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly, 9186305002Scy** attempting to use a patchset blob with old versions of the 9187305002Scy** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error. 9188305002Scy** 9189305002Scy** Because the non-primary key "old.*" fields are omitted, no 9190305002Scy** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset 9191305002Scy** is passed to the sqlite3changeset_apply() API. Other conflict types work 9192305002Scy** in the same way as for changesets. 9193305002Scy** 9194305002Scy** Changes within a patchset are ordered in the same way as for changesets 9195305002Scy** generated by the sqlite3session_changeset() function (i.e. all changes for 9196305002Scy** a single table are grouped together, tables appear in the order in which 9197305002Scy** they were attached to the session object). 9198305002Scy*/ 9199322444SpeterSQLITE_API int sqlite3session_patchset( 9200305002Scy sqlite3_session *pSession, /* Session object */ 9201305002Scy int *pnPatchset, /* OUT: Size of buffer at *ppChangeset */ 9202305002Scy void **ppPatchset /* OUT: Buffer containing changeset */ 9203305002Scy); 9204305002Scy 9205305002Scy/* 9206305002Scy** CAPI3REF: Test if a changeset has recorded any changes. 9207305002Scy** 9208305002Scy** Return non-zero if no changes to attached tables have been recorded by 9209305002Scy** the session object passed as the first argument. Otherwise, if one or 9210305002Scy** more changes have been recorded, return zero. 9211305002Scy** 9212305002Scy** Even if this function returns zero, it is possible that calling 9213305002Scy** [sqlite3session_changeset()] on the session handle may still return a 9214305002Scy** changeset that contains no changes. This can happen when a row in 9215305002Scy** an attached table is modified and then later on the original values 9216305002Scy** are restored. However, if this function returns non-zero, then it is 9217305002Scy** guaranteed that a call to sqlite3session_changeset() will return a 9218305002Scy** changeset containing zero changes. 9219305002Scy*/ 9220322444SpeterSQLITE_API int sqlite3session_isempty(sqlite3_session *pSession); 9221305002Scy 9222305002Scy/* 9223305002Scy** CAPI3REF: Create An Iterator To Traverse A Changeset 9224305002Scy** 9225305002Scy** Create an iterator used to iterate through the contents of a changeset. 9226305002Scy** If successful, *pp is set to point to the iterator handle and SQLITE_OK 9227305002Scy** is returned. Otherwise, if an error occurs, *pp is set to zero and an 9228305002Scy** SQLite error code is returned. 9229305002Scy** 9230305002Scy** The following functions can be used to advance and query a changeset 9231305002Scy** iterator created by this function: 9232305002Scy** 9233305002Scy** <ul> 9234305002Scy** <li> [sqlite3changeset_next()] 9235305002Scy** <li> [sqlite3changeset_op()] 9236305002Scy** <li> [sqlite3changeset_new()] 9237305002Scy** <li> [sqlite3changeset_old()] 9238305002Scy** </ul> 9239305002Scy** 9240305002Scy** It is the responsibility of the caller to eventually destroy the iterator 9241305002Scy** by passing it to [sqlite3changeset_finalize()]. The buffer containing the 9242305002Scy** changeset (pChangeset) must remain valid until after the iterator is 9243305002Scy** destroyed. 9244305002Scy** 9245305002Scy** Assuming the changeset blob was created by one of the 9246305002Scy** [sqlite3session_changeset()], [sqlite3changeset_concat()] or 9247305002Scy** [sqlite3changeset_invert()] functions, all changes within the changeset 9248305002Scy** that apply to a single table are grouped together. This means that when 9249305002Scy** an application iterates through a changeset using an iterator created by 9250322444Speter** this function, all changes that relate to a single table are visited 9251305002Scy** consecutively. There is no chance that the iterator will visit a change 9252305002Scy** the applies to table X, then one for table Y, and then later on visit 9253305002Scy** another change for table X. 9254305002Scy*/ 9255322444SpeterSQLITE_API int sqlite3changeset_start( 9256305002Scy sqlite3_changeset_iter **pp, /* OUT: New changeset iterator handle */ 9257305002Scy int nChangeset, /* Size of changeset blob in bytes */ 9258305002Scy void *pChangeset /* Pointer to blob containing changeset */ 9259305002Scy); 9260305002Scy 9261305002Scy 9262305002Scy/* 9263305002Scy** CAPI3REF: Advance A Changeset Iterator 9264305002Scy** 9265305002Scy** This function may only be used with iterators created by function 9266305002Scy** [sqlite3changeset_start()]. If it is called on an iterator passed to 9267305002Scy** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE 9268305002Scy** is returned and the call has no effect. 9269305002Scy** 9270305002Scy** Immediately after an iterator is created by sqlite3changeset_start(), it 9271305002Scy** does not point to any change in the changeset. Assuming the changeset 9272305002Scy** is not empty, the first call to this function advances the iterator to 9273305002Scy** point to the first change in the changeset. Each subsequent call advances 9274305002Scy** the iterator to point to the next change in the changeset (if any). If 9275305002Scy** no error occurs and the iterator points to a valid change after a call 9276305002Scy** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned. 9277305002Scy** Otherwise, if all changes in the changeset have already been visited, 9278305002Scy** SQLITE_DONE is returned. 9279305002Scy** 9280305002Scy** If an error occurs, an SQLite error code is returned. Possible error 9281305002Scy** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 9282305002Scy** SQLITE_NOMEM. 9283305002Scy*/ 9284322444SpeterSQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter); 9285305002Scy 9286305002Scy/* 9287305002Scy** CAPI3REF: Obtain The Current Operation From A Changeset Iterator 9288305002Scy** 9289305002Scy** The pIter argument passed to this function may either be an iterator 9290305002Scy** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator 9291305002Scy** created by [sqlite3changeset_start()]. In the latter case, the most recent 9292305002Scy** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this 9293305002Scy** is not the case, this function returns [SQLITE_MISUSE]. 9294305002Scy** 9295305002Scy** If argument pzTab is not NULL, then *pzTab is set to point to a 9296305002Scy** nul-terminated utf-8 encoded string containing the name of the table 9297305002Scy** affected by the current change. The buffer remains valid until either 9298305002Scy** sqlite3changeset_next() is called on the iterator or until the 9299305002Scy** conflict-handler function returns. If pnCol is not NULL, then *pnCol is 9300305002Scy** set to the number of columns in the table affected by the change. If 9301305002Scy** pbIncorrect is not NULL, then *pbIndirect is set to true (1) if the change 9302305002Scy** is an indirect change, or false (0) otherwise. See the documentation for 9303305002Scy** [sqlite3session_indirect()] for a description of direct and indirect 9304305002Scy** changes. Finally, if pOp is not NULL, then *pOp is set to one of 9305305002Scy** [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE], depending on the 9306305002Scy** type of change that the iterator currently points to. 9307305002Scy** 9308305002Scy** If no error occurs, SQLITE_OK is returned. If an error does occur, an 9309305002Scy** SQLite error code is returned. The values of the output variables may not 9310305002Scy** be trusted in this case. 9311305002Scy*/ 9312322444SpeterSQLITE_API int sqlite3changeset_op( 9313305002Scy sqlite3_changeset_iter *pIter, /* Iterator object */ 9314305002Scy const char **pzTab, /* OUT: Pointer to table name */ 9315305002Scy int *pnCol, /* OUT: Number of columns in table */ 9316305002Scy int *pOp, /* OUT: SQLITE_INSERT, DELETE or UPDATE */ 9317305002Scy int *pbIndirect /* OUT: True for an 'indirect' change */ 9318305002Scy); 9319305002Scy 9320305002Scy/* 9321305002Scy** CAPI3REF: Obtain The Primary Key Definition Of A Table 9322305002Scy** 9323305002Scy** For each modified table, a changeset includes the following: 9324305002Scy** 9325305002Scy** <ul> 9326305002Scy** <li> The number of columns in the table, and 9327305002Scy** <li> Which of those columns make up the tables PRIMARY KEY. 9328305002Scy** </ul> 9329305002Scy** 9330305002Scy** This function is used to find which columns comprise the PRIMARY KEY of 9331305002Scy** the table modified by the change that iterator pIter currently points to. 9332305002Scy** If successful, *pabPK is set to point to an array of nCol entries, where 9333305002Scy** nCol is the number of columns in the table. Elements of *pabPK are set to 9334305002Scy** 0x01 if the corresponding column is part of the tables primary key, or 9335305002Scy** 0x00 if it is not. 9336305002Scy** 9337322444Speter** If argument pnCol is not NULL, then *pnCol is set to the number of columns 9338305002Scy** in the table. 9339305002Scy** 9340305002Scy** If this function is called when the iterator does not point to a valid 9341305002Scy** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise, 9342305002Scy** SQLITE_OK is returned and the output variables populated as described 9343305002Scy** above. 9344305002Scy*/ 9345322444SpeterSQLITE_API int sqlite3changeset_pk( 9346305002Scy sqlite3_changeset_iter *pIter, /* Iterator object */ 9347305002Scy unsigned char **pabPK, /* OUT: Array of boolean - true for PK cols */ 9348305002Scy int *pnCol /* OUT: Number of entries in output array */ 9349305002Scy); 9350305002Scy 9351305002Scy/* 9352305002Scy** CAPI3REF: Obtain old.* Values From A Changeset Iterator 9353305002Scy** 9354305002Scy** The pIter argument passed to this function may either be an iterator 9355305002Scy** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator 9356305002Scy** created by [sqlite3changeset_start()]. In the latter case, the most recent 9357305002Scy** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 9358305002Scy** Furthermore, it may only be called if the type of change that the iterator 9359305002Scy** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise, 9360305002Scy** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. 9361305002Scy** 9362305002Scy** Argument iVal must be greater than or equal to 0, and less than the number 9363305002Scy** of columns in the table affected by the current change. Otherwise, 9364305002Scy** [SQLITE_RANGE] is returned and *ppValue is set to NULL. 9365305002Scy** 9366305002Scy** If successful, this function sets *ppValue to point to a protected 9367305002Scy** sqlite3_value object containing the iVal'th value from the vector of 9368305002Scy** original row values stored as part of the UPDATE or DELETE change and 9369305002Scy** returns SQLITE_OK. The name of the function comes from the fact that this 9370305002Scy** is similar to the "old.*" columns available to update or delete triggers. 9371305002Scy** 9372305002Scy** If some other error occurs (e.g. an OOM condition), an SQLite error code 9373305002Scy** is returned and *ppValue is set to NULL. 9374305002Scy*/ 9375322444SpeterSQLITE_API int sqlite3changeset_old( 9376305002Scy sqlite3_changeset_iter *pIter, /* Changeset iterator */ 9377305002Scy int iVal, /* Column number */ 9378305002Scy sqlite3_value **ppValue /* OUT: Old value (or NULL pointer) */ 9379305002Scy); 9380305002Scy 9381305002Scy/* 9382305002Scy** CAPI3REF: Obtain new.* Values From A Changeset Iterator 9383305002Scy** 9384305002Scy** The pIter argument passed to this function may either be an iterator 9385305002Scy** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator 9386305002Scy** created by [sqlite3changeset_start()]. In the latter case, the most recent 9387305002Scy** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 9388305002Scy** Furthermore, it may only be called if the type of change that the iterator 9389305002Scy** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise, 9390305002Scy** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL. 9391305002Scy** 9392305002Scy** Argument iVal must be greater than or equal to 0, and less than the number 9393305002Scy** of columns in the table affected by the current change. Otherwise, 9394305002Scy** [SQLITE_RANGE] is returned and *ppValue is set to NULL. 9395305002Scy** 9396305002Scy** If successful, this function sets *ppValue to point to a protected 9397305002Scy** sqlite3_value object containing the iVal'th value from the vector of 9398305002Scy** new row values stored as part of the UPDATE or INSERT change and 9399305002Scy** returns SQLITE_OK. If the change is an UPDATE and does not include 9400305002Scy** a new value for the requested column, *ppValue is set to NULL and 9401305002Scy** SQLITE_OK returned. The name of the function comes from the fact that 9402305002Scy** this is similar to the "new.*" columns available to update or delete 9403305002Scy** triggers. 9404305002Scy** 9405305002Scy** If some other error occurs (e.g. an OOM condition), an SQLite error code 9406305002Scy** is returned and *ppValue is set to NULL. 9407305002Scy*/ 9408322444SpeterSQLITE_API int sqlite3changeset_new( 9409305002Scy sqlite3_changeset_iter *pIter, /* Changeset iterator */ 9410305002Scy int iVal, /* Column number */ 9411305002Scy sqlite3_value **ppValue /* OUT: New value (or NULL pointer) */ 9412305002Scy); 9413305002Scy 9414305002Scy/* 9415305002Scy** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator 9416305002Scy** 9417305002Scy** This function should only be used with iterator objects passed to a 9418305002Scy** conflict-handler callback by [sqlite3changeset_apply()] with either 9419305002Scy** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function 9420305002Scy** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue 9421305002Scy** is set to NULL. 9422305002Scy** 9423305002Scy** Argument iVal must be greater than or equal to 0, and less than the number 9424305002Scy** of columns in the table affected by the current change. Otherwise, 9425305002Scy** [SQLITE_RANGE] is returned and *ppValue is set to NULL. 9426305002Scy** 9427305002Scy** If successful, this function sets *ppValue to point to a protected 9428305002Scy** sqlite3_value object containing the iVal'th value from the 9429305002Scy** "conflicting row" associated with the current conflict-handler callback 9430305002Scy** and returns SQLITE_OK. 9431305002Scy** 9432305002Scy** If some other error occurs (e.g. an OOM condition), an SQLite error code 9433305002Scy** is returned and *ppValue is set to NULL. 9434305002Scy*/ 9435322444SpeterSQLITE_API int sqlite3changeset_conflict( 9436305002Scy sqlite3_changeset_iter *pIter, /* Changeset iterator */ 9437305002Scy int iVal, /* Column number */ 9438305002Scy sqlite3_value **ppValue /* OUT: Value from conflicting row */ 9439305002Scy); 9440305002Scy 9441305002Scy/* 9442305002Scy** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations 9443305002Scy** 9444305002Scy** This function may only be called with an iterator passed to an 9445305002Scy** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case 9446305002Scy** it sets the output variable to the total number of known foreign key 9447305002Scy** violations in the destination database and returns SQLITE_OK. 9448305002Scy** 9449305002Scy** In all other cases this function returns SQLITE_MISUSE. 9450305002Scy*/ 9451322444SpeterSQLITE_API int sqlite3changeset_fk_conflicts( 9452305002Scy sqlite3_changeset_iter *pIter, /* Changeset iterator */ 9453305002Scy int *pnOut /* OUT: Number of FK violations */ 9454305002Scy); 9455305002Scy 9456305002Scy 9457305002Scy/* 9458305002Scy** CAPI3REF: Finalize A Changeset Iterator 9459305002Scy** 9460305002Scy** This function is used to finalize an iterator allocated with 9461305002Scy** [sqlite3changeset_start()]. 9462305002Scy** 9463305002Scy** This function should only be called on iterators created using the 9464305002Scy** [sqlite3changeset_start()] function. If an application calls this 9465305002Scy** function with an iterator passed to a conflict-handler by 9466305002Scy** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the 9467305002Scy** call has no effect. 9468305002Scy** 9469305002Scy** If an error was encountered within a call to an sqlite3changeset_xxx() 9470305002Scy** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 9471305002Scy** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding 9472305002Scy** to that error is returned by this function. Otherwise, SQLITE_OK is 9473305002Scy** returned. This is to allow the following pattern (pseudo-code): 9474305002Scy** 9475305002Scy** sqlite3changeset_start(); 9476305002Scy** while( SQLITE_ROW==sqlite3changeset_next() ){ 9477305002Scy** // Do something with change. 9478305002Scy** } 9479305002Scy** rc = sqlite3changeset_finalize(); 9480305002Scy** if( rc!=SQLITE_OK ){ 9481305002Scy** // An error has occurred 9482305002Scy** } 9483305002Scy*/ 9484322444SpeterSQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter); 9485305002Scy 9486305002Scy/* 9487305002Scy** CAPI3REF: Invert A Changeset 9488305002Scy** 9489305002Scy** This function is used to "invert" a changeset object. Applying an inverted 9490305002Scy** changeset to a database reverses the effects of applying the uninverted 9491305002Scy** changeset. Specifically: 9492305002Scy** 9493305002Scy** <ul> 9494305002Scy** <li> Each DELETE change is changed to an INSERT, and 9495305002Scy** <li> Each INSERT change is changed to a DELETE, and 9496305002Scy** <li> For each UPDATE change, the old.* and new.* values are exchanged. 9497305002Scy** </ul> 9498305002Scy** 9499305002Scy** This function does not change the order in which changes appear within 9500305002Scy** the changeset. It merely reverses the sense of each individual change. 9501305002Scy** 9502305002Scy** If successful, a pointer to a buffer containing the inverted changeset 9503305002Scy** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and 9504305002Scy** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are 9505305002Scy** zeroed and an SQLite error code returned. 9506305002Scy** 9507305002Scy** It is the responsibility of the caller to eventually call sqlite3_free() 9508305002Scy** on the *ppOut pointer to free the buffer allocation following a successful 9509305002Scy** call to this function. 9510305002Scy** 9511305002Scy** WARNING/TODO: This function currently assumes that the input is a valid 9512305002Scy** changeset. If it is not, the results are undefined. 9513305002Scy*/ 9514322444SpeterSQLITE_API int sqlite3changeset_invert( 9515305002Scy int nIn, const void *pIn, /* Input changeset */ 9516305002Scy int *pnOut, void **ppOut /* OUT: Inverse of input */ 9517305002Scy); 9518305002Scy 9519305002Scy/* 9520305002Scy** CAPI3REF: Concatenate Two Changeset Objects 9521305002Scy** 9522305002Scy** This function is used to concatenate two changesets, A and B, into a 9523305002Scy** single changeset. The result is a changeset equivalent to applying 9524305002Scy** changeset A followed by changeset B. 9525305002Scy** 9526305002Scy** This function combines the two input changesets using an 9527305002Scy** sqlite3_changegroup object. Calling it produces similar results as the 9528305002Scy** following code fragment: 9529305002Scy** 9530305002Scy** sqlite3_changegroup *pGrp; 9531305002Scy** rc = sqlite3_changegroup_new(&pGrp); 9532305002Scy** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA); 9533305002Scy** if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB); 9534305002Scy** if( rc==SQLITE_OK ){ 9535305002Scy** rc = sqlite3changegroup_output(pGrp, pnOut, ppOut); 9536305002Scy** }else{ 9537305002Scy** *ppOut = 0; 9538305002Scy** *pnOut = 0; 9539305002Scy** } 9540305002Scy** 9541305002Scy** Refer to the sqlite3_changegroup documentation below for details. 9542305002Scy*/ 9543322444SpeterSQLITE_API int sqlite3changeset_concat( 9544305002Scy int nA, /* Number of bytes in buffer pA */ 9545305002Scy void *pA, /* Pointer to buffer containing changeset A */ 9546305002Scy int nB, /* Number of bytes in buffer pB */ 9547305002Scy void *pB, /* Pointer to buffer containing changeset B */ 9548305002Scy int *pnOut, /* OUT: Number of bytes in output changeset */ 9549305002Scy void **ppOut /* OUT: Buffer containing output changeset */ 9550305002Scy); 9551305002Scy 9552305002Scy 9553305002Scy/* 9554322444Speter** CAPI3REF: Changegroup Handle 9555305002Scy*/ 9556305002Scytypedef struct sqlite3_changegroup sqlite3_changegroup; 9557305002Scy 9558305002Scy/* 9559322444Speter** CAPI3REF: Create A New Changegroup Object 9560305002Scy** 9561305002Scy** An sqlite3_changegroup object is used to combine two or more changesets 9562305002Scy** (or patchsets) into a single changeset (or patchset). A single changegroup 9563305002Scy** object may combine changesets or patchsets, but not both. The output is 9564305002Scy** always in the same format as the input. 9565305002Scy** 9566305002Scy** If successful, this function returns SQLITE_OK and populates (*pp) with 9567305002Scy** a pointer to a new sqlite3_changegroup object before returning. The caller 9568305002Scy** should eventually free the returned object using a call to 9569305002Scy** sqlite3changegroup_delete(). If an error occurs, an SQLite error code 9570305002Scy** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL. 9571305002Scy** 9572305002Scy** The usual usage pattern for an sqlite3_changegroup object is as follows: 9573305002Scy** 9574305002Scy** <ul> 9575305002Scy** <li> It is created using a call to sqlite3changegroup_new(). 9576305002Scy** 9577305002Scy** <li> Zero or more changesets (or patchsets) are added to the object 9578305002Scy** by calling sqlite3changegroup_add(). 9579305002Scy** 9580305002Scy** <li> The result of combining all input changesets together is obtained 9581305002Scy** by the application via a call to sqlite3changegroup_output(). 9582305002Scy** 9583305002Scy** <li> The object is deleted using a call to sqlite3changegroup_delete(). 9584305002Scy** </ul> 9585305002Scy** 9586305002Scy** Any number of calls to add() and output() may be made between the calls to 9587305002Scy** new() and delete(), and in any order. 9588305002Scy** 9589305002Scy** As well as the regular sqlite3changegroup_add() and 9590305002Scy** sqlite3changegroup_output() functions, also available are the streaming 9591305002Scy** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm(). 9592305002Scy*/ 9593322444SpeterSQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp); 9594305002Scy 9595305002Scy/* 9596322444Speter** CAPI3REF: Add A Changeset To A Changegroup 9597322444Speter** 9598305002Scy** Add all changes within the changeset (or patchset) in buffer pData (size 9599305002Scy** nData bytes) to the changegroup. 9600305002Scy** 9601305002Scy** If the buffer contains a patchset, then all prior calls to this function 9602305002Scy** on the same changegroup object must also have specified patchsets. Or, if 9603305002Scy** the buffer contains a changeset, so must have the earlier calls to this 9604305002Scy** function. Otherwise, SQLITE_ERROR is returned and no changes are added 9605305002Scy** to the changegroup. 9606305002Scy** 9607305002Scy** Rows within the changeset and changegroup are identified by the values in 9608305002Scy** their PRIMARY KEY columns. A change in the changeset is considered to 9609305002Scy** apply to the same row as a change already present in the changegroup if 9610305002Scy** the two rows have the same primary key. 9611305002Scy** 9612322444Speter** Changes to rows that do not already appear in the changegroup are 9613305002Scy** simply copied into it. Or, if both the new changeset and the changegroup 9614305002Scy** contain changes that apply to a single row, the final contents of the 9615305002Scy** changegroup depends on the type of each change, as follows: 9616305002Scy** 9617305002Scy** <table border=1 style="margin-left:8ex;margin-right:8ex"> 9618305002Scy** <tr><th style="white-space:pre">Existing Change </th> 9619305002Scy** <th style="white-space:pre">New Change </th> 9620305002Scy** <th>Output Change 9621305002Scy** <tr><td>INSERT <td>INSERT <td> 9622305002Scy** The new change is ignored. This case does not occur if the new 9623305002Scy** changeset was recorded immediately after the changesets already 9624305002Scy** added to the changegroup. 9625305002Scy** <tr><td>INSERT <td>UPDATE <td> 9626305002Scy** The INSERT change remains in the changegroup. The values in the 9627305002Scy** INSERT change are modified as if the row was inserted by the 9628305002Scy** existing change and then updated according to the new change. 9629305002Scy** <tr><td>INSERT <td>DELETE <td> 9630305002Scy** The existing INSERT is removed from the changegroup. The DELETE is 9631305002Scy** not added. 9632305002Scy** <tr><td>UPDATE <td>INSERT <td> 9633305002Scy** The new change is ignored. This case does not occur if the new 9634305002Scy** changeset was recorded immediately after the changesets already 9635305002Scy** added to the changegroup. 9636305002Scy** <tr><td>UPDATE <td>UPDATE <td> 9637305002Scy** The existing UPDATE remains within the changegroup. It is amended 9638305002Scy** so that the accompanying values are as if the row was updated once 9639305002Scy** by the existing change and then again by the new change. 9640305002Scy** <tr><td>UPDATE <td>DELETE <td> 9641305002Scy** The existing UPDATE is replaced by the new DELETE within the 9642305002Scy** changegroup. 9643305002Scy** <tr><td>DELETE <td>INSERT <td> 9644305002Scy** If one or more of the column values in the row inserted by the 9645305002Scy** new change differ from those in the row deleted by the existing 9646305002Scy** change, the existing DELETE is replaced by an UPDATE within the 9647305002Scy** changegroup. Otherwise, if the inserted row is exactly the same 9648305002Scy** as the deleted row, the existing DELETE is simply discarded. 9649305002Scy** <tr><td>DELETE <td>UPDATE <td> 9650305002Scy** The new change is ignored. This case does not occur if the new 9651305002Scy** changeset was recorded immediately after the changesets already 9652305002Scy** added to the changegroup. 9653305002Scy** <tr><td>DELETE <td>DELETE <td> 9654305002Scy** The new change is ignored. This case does not occur if the new 9655305002Scy** changeset was recorded immediately after the changesets already 9656305002Scy** added to the changegroup. 9657305002Scy** </table> 9658305002Scy** 9659305002Scy** If the new changeset contains changes to a table that is already present 9660305002Scy** in the changegroup, then the number of columns and the position of the 9661305002Scy** primary key columns for the table must be consistent. If this is not the 9662305002Scy** case, this function fails with SQLITE_SCHEMA. If the input changeset 9663305002Scy** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is 9664305002Scy** returned. Or, if an out-of-memory condition occurs during processing, this 9665305002Scy** function returns SQLITE_NOMEM. In all cases, if an error occurs the 9666305002Scy** final contents of the changegroup is undefined. 9667305002Scy** 9668305002Scy** If no error occurs, SQLITE_OK is returned. 9669305002Scy*/ 9670322444SpeterSQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData); 9671305002Scy 9672305002Scy/* 9673322444Speter** CAPI3REF: Obtain A Composite Changeset From A Changegroup 9674322444Speter** 9675305002Scy** Obtain a buffer containing a changeset (or patchset) representing the 9676305002Scy** current contents of the changegroup. If the inputs to the changegroup 9677305002Scy** were themselves changesets, the output is a changeset. Or, if the 9678305002Scy** inputs were patchsets, the output is also a patchset. 9679305002Scy** 9680305002Scy** As with the output of the sqlite3session_changeset() and 9681305002Scy** sqlite3session_patchset() functions, all changes related to a single 9682305002Scy** table are grouped together in the output of this function. Tables appear 9683305002Scy** in the same order as for the very first changeset added to the changegroup. 9684305002Scy** If the second or subsequent changesets added to the changegroup contain 9685305002Scy** changes for tables that do not appear in the first changeset, they are 9686305002Scy** appended onto the end of the output changeset, again in the order in 9687305002Scy** which they are first encountered. 9688305002Scy** 9689305002Scy** If an error occurs, an SQLite error code is returned and the output 9690305002Scy** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK 9691305002Scy** is returned and the output variables are set to the size of and a 9692305002Scy** pointer to the output buffer, respectively. In this case it is the 9693305002Scy** responsibility of the caller to eventually free the buffer using a 9694305002Scy** call to sqlite3_free(). 9695305002Scy*/ 9696322444SpeterSQLITE_API int sqlite3changegroup_output( 9697305002Scy sqlite3_changegroup*, 9698305002Scy int *pnData, /* OUT: Size of output buffer in bytes */ 9699305002Scy void **ppData /* OUT: Pointer to output buffer */ 9700305002Scy); 9701305002Scy 9702305002Scy/* 9703322444Speter** CAPI3REF: Delete A Changegroup Object 9704305002Scy*/ 9705322444SpeterSQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*); 9706305002Scy 9707305002Scy/* 9708305002Scy** CAPI3REF: Apply A Changeset To A Database 9709305002Scy** 9710305002Scy** Apply a changeset to a database. This function attempts to update the 9711305002Scy** "main" database attached to handle db with the changes found in the 9712305002Scy** changeset passed via the second and third arguments. 9713305002Scy** 9714305002Scy** The fourth argument (xFilter) passed to this function is the "filter 9715305002Scy** callback". If it is not NULL, then for each table affected by at least one 9716305002Scy** change in the changeset, the filter callback is invoked with 9717305002Scy** the table name as the second argument, and a copy of the context pointer 9718305002Scy** passed as the sixth argument to this function as the first. If the "filter 9719305002Scy** callback" returns zero, then no attempt is made to apply any changes to 9720305002Scy** the table. Otherwise, if the return value is non-zero or the xFilter 9721305002Scy** argument to this function is NULL, all changes related to the table are 9722305002Scy** attempted. 9723305002Scy** 9724305002Scy** For each table that is not excluded by the filter callback, this function 9725305002Scy** tests that the target database contains a compatible table. A table is 9726305002Scy** considered compatible if all of the following are true: 9727305002Scy** 9728305002Scy** <ul> 9729305002Scy** <li> The table has the same name as the name recorded in the 9730305002Scy** changeset, and 9731322444Speter** <li> The table has at least as many columns as recorded in the 9732305002Scy** changeset, and 9733305002Scy** <li> The table has primary key columns in the same position as 9734305002Scy** recorded in the changeset. 9735305002Scy** </ul> 9736305002Scy** 9737305002Scy** If there is no compatible table, it is not an error, but none of the 9738305002Scy** changes associated with the table are applied. A warning message is issued 9739305002Scy** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most 9740305002Scy** one such warning is issued for each table in the changeset. 9741305002Scy** 9742305002Scy** For each change for which there is a compatible table, an attempt is made 9743305002Scy** to modify the table contents according to the UPDATE, INSERT or DELETE 9744305002Scy** change. If a change cannot be applied cleanly, the conflict handler 9745305002Scy** function passed as the fifth argument to sqlite3changeset_apply() may be 9746305002Scy** invoked. A description of exactly when the conflict handler is invoked for 9747305002Scy** each type of change is below. 9748305002Scy** 9749305002Scy** Unlike the xFilter argument, xConflict may not be passed NULL. The results 9750305002Scy** of passing anything other than a valid function pointer as the xConflict 9751305002Scy** argument are undefined. 9752305002Scy** 9753305002Scy** Each time the conflict handler function is invoked, it must return one 9754305002Scy** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or 9755305002Scy** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned 9756305002Scy** if the second argument passed to the conflict handler is either 9757305002Scy** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler 9758305002Scy** returns an illegal value, any changes already made are rolled back and 9759305002Scy** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different 9760305002Scy** actions are taken by sqlite3changeset_apply() depending on the value 9761305002Scy** returned by each invocation of the conflict-handler function. Refer to 9762305002Scy** the documentation for the three 9763305002Scy** [SQLITE_CHANGESET_OMIT|available return values] for details. 9764305002Scy** 9765305002Scy** <dl> 9766305002Scy** <dt>DELETE Changes<dd> 9767305002Scy** For each DELETE change, this function checks if the target database 9768305002Scy** contains a row with the same primary key value (or values) as the 9769305002Scy** original row values stored in the changeset. If it does, and the values 9770305002Scy** stored in all non-primary key columns also match the values stored in 9771305002Scy** the changeset the row is deleted from the target database. 9772305002Scy** 9773305002Scy** If a row with matching primary key values is found, but one or more of 9774305002Scy** the non-primary key fields contains a value different from the original 9775305002Scy** row value stored in the changeset, the conflict-handler function is 9776322444Speter** invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the 9777322444Speter** database table has more columns than are recorded in the changeset, 9778322444Speter** only the values of those non-primary key fields are compared against 9779322444Speter** the current database contents - any trailing database table columns 9780322444Speter** are ignored. 9781305002Scy** 9782305002Scy** If no row with matching primary key values is found in the database, 9783305002Scy** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] 9784305002Scy** passed as the second argument. 9785305002Scy** 9786305002Scy** If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT 9787305002Scy** (which can only happen if a foreign key constraint is violated), the 9788305002Scy** conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT] 9789305002Scy** passed as the second argument. This includes the case where the DELETE 9790305002Scy** operation is attempted because an earlier call to the conflict handler 9791305002Scy** function returned [SQLITE_CHANGESET_REPLACE]. 9792305002Scy** 9793305002Scy** <dt>INSERT Changes<dd> 9794305002Scy** For each INSERT change, an attempt is made to insert the new row into 9795322444Speter** the database. If the changeset row contains fewer fields than the 9796322444Speter** database table, the trailing fields are populated with their default 9797322444Speter** values. 9798305002Scy** 9799305002Scy** If the attempt to insert the row fails because the database already 9800305002Scy** contains a row with the same primary key values, the conflict handler 9801305002Scy** function is invoked with the second argument set to 9802305002Scy** [SQLITE_CHANGESET_CONFLICT]. 9803305002Scy** 9804305002Scy** If the attempt to insert the row fails because of some other constraint 9805305002Scy** violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 9806305002Scy** invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT]. 9807305002Scy** This includes the case where the INSERT operation is re-attempted because 9808305002Scy** an earlier call to the conflict handler function returned 9809305002Scy** [SQLITE_CHANGESET_REPLACE]. 9810305002Scy** 9811305002Scy** <dt>UPDATE Changes<dd> 9812305002Scy** For each UPDATE change, this function checks if the target database 9813305002Scy** contains a row with the same primary key value (or values) as the 9814305002Scy** original row values stored in the changeset. If it does, and the values 9815322444Speter** stored in all modified non-primary key columns also match the values 9816322444Speter** stored in the changeset the row is updated within the target database. 9817305002Scy** 9818305002Scy** If a row with matching primary key values is found, but one or more of 9819322444Speter** the modified non-primary key fields contains a value different from an 9820322444Speter** original row value stored in the changeset, the conflict-handler function 9821322444Speter** is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since 9822305002Scy** UPDATE changes only contain values for non-primary key fields that are 9823305002Scy** to be modified, only those fields need to match the original values to 9824305002Scy** avoid the SQLITE_CHANGESET_DATA conflict-handler callback. 9825305002Scy** 9826305002Scy** If no row with matching primary key values is found in the database, 9827305002Scy** the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND] 9828305002Scy** passed as the second argument. 9829305002Scy** 9830305002Scy** If the UPDATE operation is attempted, but SQLite returns 9831305002Scy** SQLITE_CONSTRAINT, the conflict-handler function is invoked with 9832305002Scy** [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument. 9833305002Scy** This includes the case where the UPDATE operation is attempted after 9834305002Scy** an earlier call to the conflict handler function returned 9835305002Scy** [SQLITE_CHANGESET_REPLACE]. 9836305002Scy** </dl> 9837305002Scy** 9838305002Scy** It is safe to execute SQL statements, including those that write to the 9839305002Scy** table that the callback related to, from within the xConflict callback. 9840305002Scy** This can be used to further customize the applications conflict 9841305002Scy** resolution strategy. 9842305002Scy** 9843305002Scy** All changes made by this function are enclosed in a savepoint transaction. 9844305002Scy** If any other error (aside from a constraint failure when attempting to 9845305002Scy** write to the target database) occurs, then the savepoint transaction is 9846305002Scy** rolled back, restoring the target database to its original state, and an 9847305002Scy** SQLite error code returned. 9848305002Scy*/ 9849322444SpeterSQLITE_API int sqlite3changeset_apply( 9850305002Scy sqlite3 *db, /* Apply change to "main" db of this handle */ 9851305002Scy int nChangeset, /* Size of changeset in bytes */ 9852305002Scy void *pChangeset, /* Changeset blob */ 9853305002Scy int(*xFilter)( 9854305002Scy void *pCtx, /* Copy of sixth arg to _apply() */ 9855305002Scy const char *zTab /* Table name */ 9856305002Scy ), 9857305002Scy int(*xConflict)( 9858305002Scy void *pCtx, /* Copy of sixth arg to _apply() */ 9859305002Scy int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ 9860305002Scy sqlite3_changeset_iter *p /* Handle describing change and conflict */ 9861305002Scy ), 9862305002Scy void *pCtx /* First argument passed to xConflict */ 9863305002Scy); 9864305002Scy 9865305002Scy/* 9866305002Scy** CAPI3REF: Constants Passed To The Conflict Handler 9867305002Scy** 9868305002Scy** Values that may be passed as the second argument to a conflict-handler. 9869305002Scy** 9870305002Scy** <dl> 9871305002Scy** <dt>SQLITE_CHANGESET_DATA<dd> 9872305002Scy** The conflict handler is invoked with CHANGESET_DATA as the second argument 9873305002Scy** when processing a DELETE or UPDATE change if a row with the required 9874305002Scy** PRIMARY KEY fields is present in the database, but one or more other 9875305002Scy** (non primary-key) fields modified by the update do not contain the 9876305002Scy** expected "before" values. 9877305002Scy** 9878305002Scy** The conflicting row, in this case, is the database row with the matching 9879305002Scy** primary key. 9880305002Scy** 9881305002Scy** <dt>SQLITE_CHANGESET_NOTFOUND<dd> 9882305002Scy** The conflict handler is invoked with CHANGESET_NOTFOUND as the second 9883305002Scy** argument when processing a DELETE or UPDATE change if a row with the 9884305002Scy** required PRIMARY KEY fields is not present in the database. 9885305002Scy** 9886305002Scy** There is no conflicting row in this case. The results of invoking the 9887305002Scy** sqlite3changeset_conflict() API are undefined. 9888305002Scy** 9889305002Scy** <dt>SQLITE_CHANGESET_CONFLICT<dd> 9890305002Scy** CHANGESET_CONFLICT is passed as the second argument to the conflict 9891305002Scy** handler while processing an INSERT change if the operation would result 9892305002Scy** in duplicate primary key values. 9893305002Scy** 9894305002Scy** The conflicting row in this case is the database row with the matching 9895305002Scy** primary key. 9896305002Scy** 9897305002Scy** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd> 9898305002Scy** If foreign key handling is enabled, and applying a changeset leaves the 9899305002Scy** database in a state containing foreign key violations, the conflict 9900305002Scy** handler is invoked with CHANGESET_FOREIGN_KEY as the second argument 9901305002Scy** exactly once before the changeset is committed. If the conflict handler 9902305002Scy** returns CHANGESET_OMIT, the changes, including those that caused the 9903305002Scy** foreign key constraint violation, are committed. Or, if it returns 9904305002Scy** CHANGESET_ABORT, the changeset is rolled back. 9905305002Scy** 9906305002Scy** No current or conflicting row information is provided. The only function 9907305002Scy** it is possible to call on the supplied sqlite3_changeset_iter handle 9908305002Scy** is sqlite3changeset_fk_conflicts(). 9909305002Scy** 9910305002Scy** <dt>SQLITE_CHANGESET_CONSTRAINT<dd> 9911305002Scy** If any other constraint violation occurs while applying a change (i.e. 9912305002Scy** a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is 9913305002Scy** invoked with CHANGESET_CONSTRAINT as the second argument. 9914305002Scy** 9915305002Scy** There is no conflicting row in this case. The results of invoking the 9916305002Scy** sqlite3changeset_conflict() API are undefined. 9917305002Scy** 9918305002Scy** </dl> 9919305002Scy*/ 9920305002Scy#define SQLITE_CHANGESET_DATA 1 9921305002Scy#define SQLITE_CHANGESET_NOTFOUND 2 9922305002Scy#define SQLITE_CHANGESET_CONFLICT 3 9923305002Scy#define SQLITE_CHANGESET_CONSTRAINT 4 9924305002Scy#define SQLITE_CHANGESET_FOREIGN_KEY 5 9925305002Scy 9926305002Scy/* 9927305002Scy** CAPI3REF: Constants Returned By The Conflict Handler 9928305002Scy** 9929305002Scy** A conflict handler callback must return one of the following three values. 9930305002Scy** 9931305002Scy** <dl> 9932305002Scy** <dt>SQLITE_CHANGESET_OMIT<dd> 9933305002Scy** If a conflict handler returns this value no special action is taken. The 9934305002Scy** change that caused the conflict is not applied. The session module 9935305002Scy** continues to the next change in the changeset. 9936305002Scy** 9937305002Scy** <dt>SQLITE_CHANGESET_REPLACE<dd> 9938305002Scy** This value may only be returned if the second argument to the conflict 9939305002Scy** handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this 9940305002Scy** is not the case, any changes applied so far are rolled back and the 9941305002Scy** call to sqlite3changeset_apply() returns SQLITE_MISUSE. 9942305002Scy** 9943305002Scy** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict 9944305002Scy** handler, then the conflicting row is either updated or deleted, depending 9945305002Scy** on the type of change. 9946305002Scy** 9947305002Scy** If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict 9948305002Scy** handler, then the conflicting row is removed from the database and a 9949305002Scy** second attempt to apply the change is made. If this second attempt fails, 9950305002Scy** the original row is restored to the database before continuing. 9951305002Scy** 9952305002Scy** <dt>SQLITE_CHANGESET_ABORT<dd> 9953305002Scy** If this value is returned, any changes applied so far are rolled back 9954305002Scy** and the call to sqlite3changeset_apply() returns SQLITE_ABORT. 9955305002Scy** </dl> 9956305002Scy*/ 9957305002Scy#define SQLITE_CHANGESET_OMIT 0 9958305002Scy#define SQLITE_CHANGESET_REPLACE 1 9959305002Scy#define SQLITE_CHANGESET_ABORT 2 9960305002Scy 9961305002Scy/* 9962305002Scy** CAPI3REF: Streaming Versions of API functions. 9963305002Scy** 9964305002Scy** The six streaming API xxx_strm() functions serve similar purposes to the 9965305002Scy** corresponding non-streaming API functions: 9966305002Scy** 9967305002Scy** <table border=1 style="margin-left:8ex;margin-right:8ex"> 9968305002Scy** <tr><th>Streaming function<th>Non-streaming equivalent</th> 9969305002Scy** <tr><td>sqlite3changeset_apply_str<td>[sqlite3changeset_apply] 9970305002Scy** <tr><td>sqlite3changeset_concat_str<td>[sqlite3changeset_concat] 9971305002Scy** <tr><td>sqlite3changeset_invert_str<td>[sqlite3changeset_invert] 9972305002Scy** <tr><td>sqlite3changeset_start_str<td>[sqlite3changeset_start] 9973305002Scy** <tr><td>sqlite3session_changeset_str<td>[sqlite3session_changeset] 9974305002Scy** <tr><td>sqlite3session_patchset_str<td>[sqlite3session_patchset] 9975305002Scy** </table> 9976305002Scy** 9977305002Scy** Non-streaming functions that accept changesets (or patchsets) as input 9978305002Scy** require that the entire changeset be stored in a single buffer in memory. 9979305002Scy** Similarly, those that return a changeset or patchset do so by returning 9980305002Scy** a pointer to a single large buffer allocated using sqlite3_malloc(). 9981305002Scy** Normally this is convenient. However, if an application running in a 9982305002Scy** low-memory environment is required to handle very large changesets, the 9983305002Scy** large contiguous memory allocations required can become onerous. 9984305002Scy** 9985305002Scy** In order to avoid this problem, instead of a single large buffer, input 9986305002Scy** is passed to a streaming API functions by way of a callback function that 9987305002Scy** the sessions module invokes to incrementally request input data as it is 9988305002Scy** required. In all cases, a pair of API function parameters such as 9989305002Scy** 9990305002Scy** <pre> 9991305002Scy** int nChangeset, 9992305002Scy** void *pChangeset, 9993305002Scy** </pre> 9994305002Scy** 9995305002Scy** Is replaced by: 9996305002Scy** 9997305002Scy** <pre> 9998305002Scy** int (*xInput)(void *pIn, void *pData, int *pnData), 9999305002Scy** void *pIn, 10000305002Scy** </pre> 10001305002Scy** 10002305002Scy** Each time the xInput callback is invoked by the sessions module, the first 10003305002Scy** argument passed is a copy of the supplied pIn context pointer. The second 10004305002Scy** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no 10005305002Scy** error occurs the xInput method should copy up to (*pnData) bytes of data 10006305002Scy** into the buffer and set (*pnData) to the actual number of bytes copied 10007305002Scy** before returning SQLITE_OK. If the input is completely exhausted, (*pnData) 10008305002Scy** should be set to zero to indicate this. Or, if an error occurs, an SQLite 10009305002Scy** error code should be returned. In all cases, if an xInput callback returns 10010305002Scy** an error, all processing is abandoned and the streaming API function 10011305002Scy** returns a copy of the error code to the caller. 10012305002Scy** 10013305002Scy** In the case of sqlite3changeset_start_strm(), the xInput callback may be 10014305002Scy** invoked by the sessions module at any point during the lifetime of the 10015305002Scy** iterator. If such an xInput callback returns an error, the iterator enters 10016305002Scy** an error state, whereby all subsequent calls to iterator functions 10017305002Scy** immediately fail with the same error code as returned by xInput. 10018305002Scy** 10019305002Scy** Similarly, streaming API functions that return changesets (or patchsets) 10020305002Scy** return them in chunks by way of a callback function instead of via a 10021305002Scy** pointer to a single large buffer. In this case, a pair of parameters such 10022305002Scy** as: 10023305002Scy** 10024305002Scy** <pre> 10025305002Scy** int *pnChangeset, 10026305002Scy** void **ppChangeset, 10027305002Scy** </pre> 10028305002Scy** 10029305002Scy** Is replaced by: 10030305002Scy** 10031305002Scy** <pre> 10032305002Scy** int (*xOutput)(void *pOut, const void *pData, int nData), 10033305002Scy** void *pOut 10034305002Scy** </pre> 10035305002Scy** 10036305002Scy** The xOutput callback is invoked zero or more times to return data to 10037305002Scy** the application. The first parameter passed to each call is a copy of the 10038305002Scy** pOut pointer supplied by the application. The second parameter, pData, 10039305002Scy** points to a buffer nData bytes in size containing the chunk of output 10040305002Scy** data being returned. If the xOutput callback successfully processes the 10041305002Scy** supplied data, it should return SQLITE_OK to indicate success. Otherwise, 10042305002Scy** it should return some other SQLite error code. In this case processing 10043305002Scy** is immediately abandoned and the streaming API function returns a copy 10044305002Scy** of the xOutput error code to the application. 10045305002Scy** 10046305002Scy** The sessions module never invokes an xOutput callback with the third 10047305002Scy** parameter set to a value less than or equal to zero. Other than this, 10048305002Scy** no guarantees are made as to the size of the chunks of data returned. 10049305002Scy*/ 10050322444SpeterSQLITE_API int sqlite3changeset_apply_strm( 10051305002Scy sqlite3 *db, /* Apply change to "main" db of this handle */ 10052305002Scy int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */ 10053305002Scy void *pIn, /* First arg for xInput */ 10054305002Scy int(*xFilter)( 10055305002Scy void *pCtx, /* Copy of sixth arg to _apply() */ 10056305002Scy const char *zTab /* Table name */ 10057305002Scy ), 10058305002Scy int(*xConflict)( 10059305002Scy void *pCtx, /* Copy of sixth arg to _apply() */ 10060305002Scy int eConflict, /* DATA, MISSING, CONFLICT, CONSTRAINT */ 10061305002Scy sqlite3_changeset_iter *p /* Handle describing change and conflict */ 10062305002Scy ), 10063305002Scy void *pCtx /* First argument passed to xConflict */ 10064305002Scy); 10065322444SpeterSQLITE_API int sqlite3changeset_concat_strm( 10066305002Scy int (*xInputA)(void *pIn, void *pData, int *pnData), 10067305002Scy void *pInA, 10068305002Scy int (*xInputB)(void *pIn, void *pData, int *pnData), 10069305002Scy void *pInB, 10070305002Scy int (*xOutput)(void *pOut, const void *pData, int nData), 10071305002Scy void *pOut 10072305002Scy); 10073322444SpeterSQLITE_API int sqlite3changeset_invert_strm( 10074305002Scy int (*xInput)(void *pIn, void *pData, int *pnData), 10075305002Scy void *pIn, 10076305002Scy int (*xOutput)(void *pOut, const void *pData, int nData), 10077305002Scy void *pOut 10078305002Scy); 10079322444SpeterSQLITE_API int sqlite3changeset_start_strm( 10080305002Scy sqlite3_changeset_iter **pp, 10081305002Scy int (*xInput)(void *pIn, void *pData, int *pnData), 10082305002Scy void *pIn 10083305002Scy); 10084322444SpeterSQLITE_API int sqlite3session_changeset_strm( 10085305002Scy sqlite3_session *pSession, 10086305002Scy int (*xOutput)(void *pOut, const void *pData, int nData), 10087305002Scy void *pOut 10088305002Scy); 10089322444SpeterSQLITE_API int sqlite3session_patchset_strm( 10090305002Scy sqlite3_session *pSession, 10091305002Scy int (*xOutput)(void *pOut, const void *pData, int nData), 10092305002Scy void *pOut 10093305002Scy); 10094322444SpeterSQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, 10095305002Scy int (*xInput)(void *pIn, void *pData, int *pnData), 10096305002Scy void *pIn 10097305002Scy); 10098322444SpeterSQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*, 10099305002Scy int (*xOutput)(void *pOut, const void *pData, int nData), 10100305002Scy void *pOut 10101305002Scy); 10102305002Scy 10103305002Scy 10104305002Scy/* 10105305002Scy** Make sure we can call this stuff from C++. 10106305002Scy*/ 10107305002Scy#ifdef __cplusplus 10108305002Scy} 10109305002Scy#endif 10110305002Scy 10111305002Scy#endif /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */ 10112305002Scy 10113305002Scy/******** End of sqlite3session.h *********/ 10114305002Scy/******** Begin file fts5.h *********/ 10115305002Scy/* 10116298161Sbapt** 2014 May 31 10117298161Sbapt** 10118298161Sbapt** The author disclaims copyright to this source code. In place of 10119298161Sbapt** a legal notice, here is a blessing: 10120298161Sbapt** 10121298161Sbapt** May you do good and not evil. 10122298161Sbapt** May you find forgiveness for yourself and forgive others. 10123298161Sbapt** May you share freely, never taking more than you give. 10124298161Sbapt** 10125298161Sbapt****************************************************************************** 10126298161Sbapt** 10127298161Sbapt** Interfaces to extend FTS5. Using the interfaces defined in this file, 10128298161Sbapt** FTS5 may be extended with: 10129298161Sbapt** 10130298161Sbapt** * custom tokenizers, and 10131298161Sbapt** * custom auxiliary functions. 10132298161Sbapt*/ 10133298161Sbapt 10134298161Sbapt 10135298161Sbapt#ifndef _FTS5_H 10136298161Sbapt#define _FTS5_H 10137298161Sbapt 10138298161Sbapt 10139298161Sbapt#ifdef __cplusplus 10140298161Sbaptextern "C" { 10141298161Sbapt#endif 10142298161Sbapt 10143298161Sbapt/************************************************************************* 10144298161Sbapt** CUSTOM AUXILIARY FUNCTIONS 10145298161Sbapt** 10146298161Sbapt** Virtual table implementations may overload SQL functions by implementing 10147298161Sbapt** the sqlite3_module.xFindFunction() method. 10148298161Sbapt*/ 10149298161Sbapt 10150298161Sbapttypedef struct Fts5ExtensionApi Fts5ExtensionApi; 10151298161Sbapttypedef struct Fts5Context Fts5Context; 10152298161Sbapttypedef struct Fts5PhraseIter Fts5PhraseIter; 10153298161Sbapt 10154298161Sbapttypedef void (*fts5_extension_function)( 10155298161Sbapt const Fts5ExtensionApi *pApi, /* API offered by current FTS version */ 10156298161Sbapt Fts5Context *pFts, /* First arg to pass to pApi functions */ 10157298161Sbapt sqlite3_context *pCtx, /* Context for returning result/error */ 10158298161Sbapt int nVal, /* Number of values in apVal[] array */ 10159298161Sbapt sqlite3_value **apVal /* Array of trailing arguments */ 10160298161Sbapt); 10161298161Sbapt 10162298161Sbaptstruct Fts5PhraseIter { 10163298161Sbapt const unsigned char *a; 10164298161Sbapt const unsigned char *b; 10165298161Sbapt}; 10166298161Sbapt 10167298161Sbapt/* 10168298161Sbapt** EXTENSION API FUNCTIONS 10169298161Sbapt** 10170298161Sbapt** xUserData(pFts): 10171298161Sbapt** Return a copy of the context pointer the extension function was 10172298161Sbapt** registered with. 10173298161Sbapt** 10174298161Sbapt** xColumnTotalSize(pFts, iCol, pnToken): 10175298161Sbapt** If parameter iCol is less than zero, set output variable *pnToken 10176298161Sbapt** to the total number of tokens in the FTS5 table. Or, if iCol is 10177298161Sbapt** non-negative but less than the number of columns in the table, return 10178298161Sbapt** the total number of tokens in column iCol, considering all rows in 10179298161Sbapt** the FTS5 table. 10180298161Sbapt** 10181298161Sbapt** If parameter iCol is greater than or equal to the number of columns 10182298161Sbapt** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g. 10183298161Sbapt** an OOM condition or IO error), an appropriate SQLite error code is 10184298161Sbapt** returned. 10185298161Sbapt** 10186298161Sbapt** xColumnCount(pFts): 10187298161Sbapt** Return the number of columns in the table. 10188298161Sbapt** 10189298161Sbapt** xColumnSize(pFts, iCol, pnToken): 10190298161Sbapt** If parameter iCol is less than zero, set output variable *pnToken 10191298161Sbapt** to the total number of tokens in the current row. Or, if iCol is 10192298161Sbapt** non-negative but less than the number of columns in the table, set 10193298161Sbapt** *pnToken to the number of tokens in column iCol of the current row. 10194298161Sbapt** 10195298161Sbapt** If parameter iCol is greater than or equal to the number of columns 10196298161Sbapt** in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g. 10197298161Sbapt** an OOM condition or IO error), an appropriate SQLite error code is 10198298161Sbapt** returned. 10199298161Sbapt** 10200298161Sbapt** This function may be quite inefficient if used with an FTS5 table 10201298161Sbapt** created with the "columnsize=0" option. 10202298161Sbapt** 10203298161Sbapt** xColumnText: 10204298161Sbapt** This function attempts to retrieve the text of column iCol of the 10205298161Sbapt** current document. If successful, (*pz) is set to point to a buffer 10206298161Sbapt** containing the text in utf-8 encoding, (*pn) is set to the size in bytes 10207298161Sbapt** (not characters) of the buffer and SQLITE_OK is returned. Otherwise, 10208298161Sbapt** if an error occurs, an SQLite error code is returned and the final values 10209298161Sbapt** of (*pz) and (*pn) are undefined. 10210298161Sbapt** 10211298161Sbapt** xPhraseCount: 10212298161Sbapt** Returns the number of phrases in the current query expression. 10213298161Sbapt** 10214298161Sbapt** xPhraseSize: 10215298161Sbapt** Returns the number of tokens in phrase iPhrase of the query. Phrases 10216298161Sbapt** are numbered starting from zero. 10217298161Sbapt** 10218298161Sbapt** xInstCount: 10219298161Sbapt** Set *pnInst to the total number of occurrences of all phrases within 10220298161Sbapt** the query within the current row. Return SQLITE_OK if successful, or 10221298161Sbapt** an error code (i.e. SQLITE_NOMEM) if an error occurs. 10222298161Sbapt** 10223298161Sbapt** This API can be quite slow if used with an FTS5 table created with the 10224298161Sbapt** "detail=none" or "detail=column" option. If the FTS5 table is created 10225298161Sbapt** with either "detail=none" or "detail=column" and "content=" option 10226298161Sbapt** (i.e. if it is a contentless table), then this API always returns 0. 10227298161Sbapt** 10228298161Sbapt** xInst: 10229298161Sbapt** Query for the details of phrase match iIdx within the current row. 10230298161Sbapt** Phrase matches are numbered starting from zero, so the iIdx argument 10231298161Sbapt** should be greater than or equal to zero and smaller than the value 10232298161Sbapt** output by xInstCount(). 10233298161Sbapt** 10234298161Sbapt** Usually, output parameter *piPhrase is set to the phrase number, *piCol 10235298161Sbapt** to the column in which it occurs and *piOff the token offset of the 10236298161Sbapt** first token of the phrase. The exception is if the table was created 10237298161Sbapt** with the offsets=0 option specified. In this case *piOff is always 10238298161Sbapt** set to -1. 10239298161Sbapt** 10240298161Sbapt** Returns SQLITE_OK if successful, or an error code (i.e. SQLITE_NOMEM) 10241298161Sbapt** if an error occurs. 10242298161Sbapt** 10243298161Sbapt** This API can be quite slow if used with an FTS5 table created with the 10244298161Sbapt** "detail=none" or "detail=column" option. 10245298161Sbapt** 10246298161Sbapt** xRowid: 10247298161Sbapt** Returns the rowid of the current row. 10248298161Sbapt** 10249298161Sbapt** xTokenize: 10250298161Sbapt** Tokenize text using the tokenizer belonging to the FTS5 table. 10251298161Sbapt** 10252298161Sbapt** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback): 10253298161Sbapt** This API function is used to query the FTS table for phrase iPhrase 10254298161Sbapt** of the current query. Specifically, a query equivalent to: 10255298161Sbapt** 10256298161Sbapt** ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid 10257298161Sbapt** 10258298161Sbapt** with $p set to a phrase equivalent to the phrase iPhrase of the 10259305002Scy** current query is executed. Any column filter that applies to 10260305002Scy** phrase iPhrase of the current query is included in $p. For each 10261305002Scy** row visited, the callback function passed as the fourth argument 10262305002Scy** is invoked. The context and API objects passed to the callback 10263305002Scy** function may be used to access the properties of each matched row. 10264305002Scy** Invoking Api.xUserData() returns a copy of the pointer passed as 10265305002Scy** the third argument to pUserData. 10266298161Sbapt** 10267298161Sbapt** If the callback function returns any value other than SQLITE_OK, the 10268298161Sbapt** query is abandoned and the xQueryPhrase function returns immediately. 10269298161Sbapt** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK. 10270298161Sbapt** Otherwise, the error code is propagated upwards. 10271298161Sbapt** 10272298161Sbapt** If the query runs to completion without incident, SQLITE_OK is returned. 10273298161Sbapt** Or, if some error occurs before the query completes or is aborted by 10274298161Sbapt** the callback, an SQLite error code is returned. 10275298161Sbapt** 10276298161Sbapt** 10277298161Sbapt** xSetAuxdata(pFts5, pAux, xDelete) 10278298161Sbapt** 10279298161Sbapt** Save the pointer passed as the second argument as the extension functions 10280298161Sbapt** "auxiliary data". The pointer may then be retrieved by the current or any 10281298161Sbapt** future invocation of the same fts5 extension function made as part of 10282298161Sbapt** of the same MATCH query using the xGetAuxdata() API. 10283298161Sbapt** 10284298161Sbapt** Each extension function is allocated a single auxiliary data slot for 10285298161Sbapt** each FTS query (MATCH expression). If the extension function is invoked 10286298161Sbapt** more than once for a single FTS query, then all invocations share a 10287298161Sbapt** single auxiliary data context. 10288298161Sbapt** 10289298161Sbapt** If there is already an auxiliary data pointer when this function is 10290298161Sbapt** invoked, then it is replaced by the new pointer. If an xDelete callback 10291298161Sbapt** was specified along with the original pointer, it is invoked at this 10292298161Sbapt** point. 10293298161Sbapt** 10294298161Sbapt** The xDelete callback, if one is specified, is also invoked on the 10295298161Sbapt** auxiliary data pointer after the FTS5 query has finished. 10296298161Sbapt** 10297298161Sbapt** If an error (e.g. an OOM condition) occurs within this function, an 10298298161Sbapt** the auxiliary data is set to NULL and an error code returned. If the 10299298161Sbapt** xDelete parameter was not NULL, it is invoked on the auxiliary data 10300298161Sbapt** pointer before returning. 10301298161Sbapt** 10302298161Sbapt** 10303298161Sbapt** xGetAuxdata(pFts5, bClear) 10304298161Sbapt** 10305298161Sbapt** Returns the current auxiliary data pointer for the fts5 extension 10306298161Sbapt** function. See the xSetAuxdata() method for details. 10307298161Sbapt** 10308298161Sbapt** If the bClear argument is non-zero, then the auxiliary data is cleared 10309298161Sbapt** (set to NULL) before this function returns. In this case the xDelete, 10310298161Sbapt** if any, is not invoked. 10311298161Sbapt** 10312298161Sbapt** 10313298161Sbapt** xRowCount(pFts5, pnRow) 10314298161Sbapt** 10315298161Sbapt** This function is used to retrieve the total number of rows in the table. 10316298161Sbapt** In other words, the same value that would be returned by: 10317298161Sbapt** 10318298161Sbapt** SELECT count(*) FROM ftstable; 10319298161Sbapt** 10320298161Sbapt** xPhraseFirst() 10321298161Sbapt** This function is used, along with type Fts5PhraseIter and the xPhraseNext 10322298161Sbapt** method, to iterate through all instances of a single query phrase within 10323298161Sbapt** the current row. This is the same information as is accessible via the 10324298161Sbapt** xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient 10325298161Sbapt** to use, this API may be faster under some circumstances. To iterate 10326298161Sbapt** through instances of phrase iPhrase, use the following code: 10327298161Sbapt** 10328298161Sbapt** Fts5PhraseIter iter; 10329298161Sbapt** int iCol, iOff; 10330298161Sbapt** for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff); 10331298161Sbapt** iCol>=0; 10332298161Sbapt** pApi->xPhraseNext(pFts, &iter, &iCol, &iOff) 10333298161Sbapt** ){ 10334298161Sbapt** // An instance of phrase iPhrase at offset iOff of column iCol 10335298161Sbapt** } 10336298161Sbapt** 10337298161Sbapt** The Fts5PhraseIter structure is defined above. Applications should not 10338298161Sbapt** modify this structure directly - it should only be used as shown above 10339298161Sbapt** with the xPhraseFirst() and xPhraseNext() API methods (and by 10340298161Sbapt** xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below). 10341298161Sbapt** 10342298161Sbapt** This API can be quite slow if used with an FTS5 table created with the 10343298161Sbapt** "detail=none" or "detail=column" option. If the FTS5 table is created 10344298161Sbapt** with either "detail=none" or "detail=column" and "content=" option 10345298161Sbapt** (i.e. if it is a contentless table), then this API always iterates 10346298161Sbapt** through an empty set (all calls to xPhraseFirst() set iCol to -1). 10347298161Sbapt** 10348298161Sbapt** xPhraseNext() 10349298161Sbapt** See xPhraseFirst above. 10350298161Sbapt** 10351298161Sbapt** xPhraseFirstColumn() 10352298161Sbapt** This function and xPhraseNextColumn() are similar to the xPhraseFirst() 10353298161Sbapt** and xPhraseNext() APIs described above. The difference is that instead 10354298161Sbapt** of iterating through all instances of a phrase in the current row, these 10355298161Sbapt** APIs are used to iterate through the set of columns in the current row 10356298161Sbapt** that contain one or more instances of a specified phrase. For example: 10357298161Sbapt** 10358298161Sbapt** Fts5PhraseIter iter; 10359298161Sbapt** int iCol; 10360298161Sbapt** for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol); 10361298161Sbapt** iCol>=0; 10362298161Sbapt** pApi->xPhraseNextColumn(pFts, &iter, &iCol) 10363298161Sbapt** ){ 10364298161Sbapt** // Column iCol contains at least one instance of phrase iPhrase 10365298161Sbapt** } 10366298161Sbapt** 10367298161Sbapt** This API can be quite slow if used with an FTS5 table created with the 10368298161Sbapt** "detail=none" option. If the FTS5 table is created with either 10369298161Sbapt** "detail=none" "content=" option (i.e. if it is a contentless table), 10370298161Sbapt** then this API always iterates through an empty set (all calls to 10371298161Sbapt** xPhraseFirstColumn() set iCol to -1). 10372298161Sbapt** 10373298161Sbapt** The information accessed using this API and its companion 10374298161Sbapt** xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext 10375298161Sbapt** (or xInst/xInstCount). The chief advantage of this API is that it is 10376298161Sbapt** significantly more efficient than those alternatives when used with 10377298161Sbapt** "detail=column" tables. 10378298161Sbapt** 10379298161Sbapt** xPhraseNextColumn() 10380298161Sbapt** See xPhraseFirstColumn above. 10381298161Sbapt*/ 10382298161Sbaptstruct Fts5ExtensionApi { 10383298161Sbapt int iVersion; /* Currently always set to 3 */ 10384298161Sbapt 10385298161Sbapt void *(*xUserData)(Fts5Context*); 10386298161Sbapt 10387298161Sbapt int (*xColumnCount)(Fts5Context*); 10388298161Sbapt int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow); 10389298161Sbapt int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken); 10390298161Sbapt 10391298161Sbapt int (*xTokenize)(Fts5Context*, 10392298161Sbapt const char *pText, int nText, /* Text to tokenize */ 10393298161Sbapt void *pCtx, /* Context passed to xToken() */ 10394298161Sbapt int (*xToken)(void*, int, const char*, int, int, int) /* Callback */ 10395298161Sbapt ); 10396298161Sbapt 10397298161Sbapt int (*xPhraseCount)(Fts5Context*); 10398298161Sbapt int (*xPhraseSize)(Fts5Context*, int iPhrase); 10399298161Sbapt 10400298161Sbapt int (*xInstCount)(Fts5Context*, int *pnInst); 10401298161Sbapt int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff); 10402298161Sbapt 10403298161Sbapt sqlite3_int64 (*xRowid)(Fts5Context*); 10404298161Sbapt int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn); 10405298161Sbapt int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken); 10406298161Sbapt 10407298161Sbapt int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData, 10408298161Sbapt int(*)(const Fts5ExtensionApi*,Fts5Context*,void*) 10409298161Sbapt ); 10410298161Sbapt int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*)); 10411298161Sbapt void *(*xGetAuxdata)(Fts5Context*, int bClear); 10412298161Sbapt 10413298161Sbapt int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*); 10414298161Sbapt void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff); 10415298161Sbapt 10416298161Sbapt int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*); 10417298161Sbapt void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol); 10418298161Sbapt}; 10419298161Sbapt 10420298161Sbapt/* 10421298161Sbapt** CUSTOM AUXILIARY FUNCTIONS 10422298161Sbapt*************************************************************************/ 10423298161Sbapt 10424298161Sbapt/************************************************************************* 10425298161Sbapt** CUSTOM TOKENIZERS 10426298161Sbapt** 10427298161Sbapt** Applications may also register custom tokenizer types. A tokenizer 10428298161Sbapt** is registered by providing fts5 with a populated instance of the 10429298161Sbapt** following structure. All structure methods must be defined, setting 10430298161Sbapt** any member of the fts5_tokenizer struct to NULL leads to undefined 10431298161Sbapt** behaviour. The structure methods are expected to function as follows: 10432298161Sbapt** 10433298161Sbapt** xCreate: 10434305002Scy** This function is used to allocate and initialize a tokenizer instance. 10435298161Sbapt** A tokenizer instance is required to actually tokenize text. 10436298161Sbapt** 10437298161Sbapt** The first argument passed to this function is a copy of the (void*) 10438298161Sbapt** pointer provided by the application when the fts5_tokenizer object 10439298161Sbapt** was registered with FTS5 (the third argument to xCreateTokenizer()). 10440298161Sbapt** The second and third arguments are an array of nul-terminated strings 10441298161Sbapt** containing the tokenizer arguments, if any, specified following the 10442298161Sbapt** tokenizer name as part of the CREATE VIRTUAL TABLE statement used 10443298161Sbapt** to create the FTS5 table. 10444298161Sbapt** 10445298161Sbapt** The final argument is an output variable. If successful, (*ppOut) 10446298161Sbapt** should be set to point to the new tokenizer handle and SQLITE_OK 10447298161Sbapt** returned. If an error occurs, some value other than SQLITE_OK should 10448298161Sbapt** be returned. In this case, fts5 assumes that the final value of *ppOut 10449298161Sbapt** is undefined. 10450298161Sbapt** 10451298161Sbapt** xDelete: 10452298161Sbapt** This function is invoked to delete a tokenizer handle previously 10453298161Sbapt** allocated using xCreate(). Fts5 guarantees that this function will 10454298161Sbapt** be invoked exactly once for each successful call to xCreate(). 10455298161Sbapt** 10456298161Sbapt** xTokenize: 10457298161Sbapt** This function is expected to tokenize the nText byte string indicated 10458298161Sbapt** by argument pText. pText may or may not be nul-terminated. The first 10459298161Sbapt** argument passed to this function is a pointer to an Fts5Tokenizer object 10460298161Sbapt** returned by an earlier call to xCreate(). 10461298161Sbapt** 10462298161Sbapt** The second argument indicates the reason that FTS5 is requesting 10463298161Sbapt** tokenization of the supplied text. This is always one of the following 10464298161Sbapt** four values: 10465298161Sbapt** 10466298161Sbapt** <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into 10467298161Sbapt** or removed from the FTS table. The tokenizer is being invoked to 10468298161Sbapt** determine the set of tokens to add to (or delete from) the 10469298161Sbapt** FTS index. 10470298161Sbapt** 10471298161Sbapt** <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed 10472298161Sbapt** against the FTS index. The tokenizer is being called to tokenize 10473298161Sbapt** a bareword or quoted string specified as part of the query. 10474298161Sbapt** 10475298161Sbapt** <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as 10476298161Sbapt** FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is 10477298161Sbapt** followed by a "*" character, indicating that the last token 10478298161Sbapt** returned by the tokenizer will be treated as a token prefix. 10479298161Sbapt** 10480298161Sbapt** <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to 10481298161Sbapt** satisfy an fts5_api.xTokenize() request made by an auxiliary 10482298161Sbapt** function. Or an fts5_api.xColumnSize() request made by the same 10483298161Sbapt** on a columnsize=0 database. 10484298161Sbapt** </ul> 10485298161Sbapt** 10486298161Sbapt** For each token in the input string, the supplied callback xToken() must 10487298161Sbapt** be invoked. The first argument to it should be a copy of the pointer 10488298161Sbapt** passed as the second argument to xTokenize(). The third and fourth 10489298161Sbapt** arguments are a pointer to a buffer containing the token text, and the 10490298161Sbapt** size of the token in bytes. The 4th and 5th arguments are the byte offsets 10491298161Sbapt** of the first byte of and first byte immediately following the text from 10492298161Sbapt** which the token is derived within the input. 10493298161Sbapt** 10494298161Sbapt** The second argument passed to the xToken() callback ("tflags") should 10495298161Sbapt** normally be set to 0. The exception is if the tokenizer supports 10496298161Sbapt** synonyms. In this case see the discussion below for details. 10497298161Sbapt** 10498298161Sbapt** FTS5 assumes the xToken() callback is invoked for each token in the 10499298161Sbapt** order that they occur within the input text. 10500298161Sbapt** 10501298161Sbapt** If an xToken() callback returns any value other than SQLITE_OK, then 10502298161Sbapt** the tokenization should be abandoned and the xTokenize() method should 10503298161Sbapt** immediately return a copy of the xToken() return value. Or, if the 10504298161Sbapt** input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally, 10505298161Sbapt** if an error occurs with the xTokenize() implementation itself, it 10506298161Sbapt** may abandon the tokenization and return any error code other than 10507298161Sbapt** SQLITE_OK or SQLITE_DONE. 10508298161Sbapt** 10509298161Sbapt** SYNONYM SUPPORT 10510298161Sbapt** 10511298161Sbapt** Custom tokenizers may also support synonyms. Consider a case in which a 10512298161Sbapt** user wishes to query for a phrase such as "first place". Using the 10513298161Sbapt** built-in tokenizers, the FTS5 query 'first + place' will match instances 10514298161Sbapt** of "first place" within the document set, but not alternative forms 10515298161Sbapt** such as "1st place". In some applications, it would be better to match 10516298161Sbapt** all instances of "first place" or "1st place" regardless of which form 10517298161Sbapt** the user specified in the MATCH query text. 10518298161Sbapt** 10519298161Sbapt** There are several ways to approach this in FTS5: 10520298161Sbapt** 10521298161Sbapt** <ol><li> By mapping all synonyms to a single token. In this case, the 10522298161Sbapt** In the above example, this means that the tokenizer returns the 10523298161Sbapt** same token for inputs "first" and "1st". Say that token is in 10524298161Sbapt** fact "first", so that when the user inserts the document "I won 10525298161Sbapt** 1st place" entries are added to the index for tokens "i", "won", 10526298161Sbapt** "first" and "place". If the user then queries for '1st + place', 10527298161Sbapt** the tokenizer substitutes "first" for "1st" and the query works 10528298161Sbapt** as expected. 10529298161Sbapt** 10530298161Sbapt** <li> By adding multiple synonyms for a single term to the FTS index. 10531298161Sbapt** In this case, when tokenizing query text, the tokenizer may 10532298161Sbapt** provide multiple synonyms for a single term within the document. 10533298161Sbapt** FTS5 then queries the index for each synonym individually. For 10534298161Sbapt** example, faced with the query: 10535298161Sbapt** 10536298161Sbapt** <codeblock> 10537298161Sbapt** ... MATCH 'first place'</codeblock> 10538298161Sbapt** 10539298161Sbapt** the tokenizer offers both "1st" and "first" as synonyms for the 10540298161Sbapt** first token in the MATCH query and FTS5 effectively runs a query 10541298161Sbapt** similar to: 10542298161Sbapt** 10543298161Sbapt** <codeblock> 10544298161Sbapt** ... MATCH '(first OR 1st) place'</codeblock> 10545298161Sbapt** 10546298161Sbapt** except that, for the purposes of auxiliary functions, the query 10547298161Sbapt** still appears to contain just two phrases - "(first OR 1st)" 10548298161Sbapt** being treated as a single phrase. 10549298161Sbapt** 10550298161Sbapt** <li> By adding multiple synonyms for a single term to the FTS index. 10551298161Sbapt** Using this method, when tokenizing document text, the tokenizer 10552298161Sbapt** provides multiple synonyms for each token. So that when a 10553298161Sbapt** document such as "I won first place" is tokenized, entries are 10554298161Sbapt** added to the FTS index for "i", "won", "first", "1st" and 10555298161Sbapt** "place". 10556298161Sbapt** 10557298161Sbapt** This way, even if the tokenizer does not provide synonyms 10558298161Sbapt** when tokenizing query text (it should not - to do would be 10559298161Sbapt** inefficient), it doesn't matter if the user queries for 10560298161Sbapt** 'first + place' or '1st + place', as there are entires in the 10561298161Sbapt** FTS index corresponding to both forms of the first token. 10562298161Sbapt** </ol> 10563298161Sbapt** 10564298161Sbapt** Whether it is parsing document or query text, any call to xToken that 10565298161Sbapt** specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit 10566298161Sbapt** is considered to supply a synonym for the previous token. For example, 10567298161Sbapt** when parsing the document "I won first place", a tokenizer that supports 10568298161Sbapt** synonyms would call xToken() 5 times, as follows: 10569298161Sbapt** 10570298161Sbapt** <codeblock> 10571298161Sbapt** xToken(pCtx, 0, "i", 1, 0, 1); 10572298161Sbapt** xToken(pCtx, 0, "won", 3, 2, 5); 10573298161Sbapt** xToken(pCtx, 0, "first", 5, 6, 11); 10574298161Sbapt** xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3, 6, 11); 10575298161Sbapt** xToken(pCtx, 0, "place", 5, 12, 17); 10576298161Sbapt**</codeblock> 10577298161Sbapt** 10578298161Sbapt** It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time 10579298161Sbapt** xToken() is called. Multiple synonyms may be specified for a single token 10580298161Sbapt** by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence. 10581298161Sbapt** There is no limit to the number of synonyms that may be provided for a 10582298161Sbapt** single token. 10583298161Sbapt** 10584298161Sbapt** In many cases, method (1) above is the best approach. It does not add 10585298161Sbapt** extra data to the FTS index or require FTS5 to query for multiple terms, 10586298161Sbapt** so it is efficient in terms of disk space and query speed. However, it 10587298161Sbapt** does not support prefix queries very well. If, as suggested above, the 10588298161Sbapt** token "first" is subsituted for "1st" by the tokenizer, then the query: 10589298161Sbapt** 10590298161Sbapt** <codeblock> 10591298161Sbapt** ... MATCH '1s*'</codeblock> 10592298161Sbapt** 10593298161Sbapt** will not match documents that contain the token "1st" (as the tokenizer 10594298161Sbapt** will probably not map "1s" to any prefix of "first"). 10595298161Sbapt** 10596298161Sbapt** For full prefix support, method (3) may be preferred. In this case, 10597298161Sbapt** because the index contains entries for both "first" and "1st", prefix 10598298161Sbapt** queries such as 'fi*' or '1s*' will match correctly. However, because 10599298161Sbapt** extra entries are added to the FTS index, this method uses more space 10600298161Sbapt** within the database. 10601298161Sbapt** 10602298161Sbapt** Method (2) offers a midpoint between (1) and (3). Using this method, 10603298161Sbapt** a query such as '1s*' will match documents that contain the literal 10604298161Sbapt** token "1st", but not "first" (assuming the tokenizer is not able to 10605298161Sbapt** provide synonyms for prefixes). However, a non-prefix query like '1st' 10606298161Sbapt** will match against "1st" and "first". This method does not require 10607298161Sbapt** extra disk space, as no extra entries are added to the FTS index. 10608298161Sbapt** On the other hand, it may require more CPU cycles to run MATCH queries, 10609298161Sbapt** as separate queries of the FTS index are required for each synonym. 10610298161Sbapt** 10611298161Sbapt** When using methods (2) or (3), it is important that the tokenizer only 10612298161Sbapt** provide synonyms when tokenizing document text (method (2)) or query 10613298161Sbapt** text (method (3)), not both. Doing so will not cause any errors, but is 10614298161Sbapt** inefficient. 10615298161Sbapt*/ 10616298161Sbapttypedef struct Fts5Tokenizer Fts5Tokenizer; 10617298161Sbapttypedef struct fts5_tokenizer fts5_tokenizer; 10618298161Sbaptstruct fts5_tokenizer { 10619298161Sbapt int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut); 10620298161Sbapt void (*xDelete)(Fts5Tokenizer*); 10621298161Sbapt int (*xTokenize)(Fts5Tokenizer*, 10622298161Sbapt void *pCtx, 10623298161Sbapt int flags, /* Mask of FTS5_TOKENIZE_* flags */ 10624298161Sbapt const char *pText, int nText, 10625298161Sbapt int (*xToken)( 10626298161Sbapt void *pCtx, /* Copy of 2nd argument to xTokenize() */ 10627298161Sbapt int tflags, /* Mask of FTS5_TOKEN_* flags */ 10628298161Sbapt const char *pToken, /* Pointer to buffer containing token */ 10629298161Sbapt int nToken, /* Size of token in bytes */ 10630298161Sbapt int iStart, /* Byte offset of token within input text */ 10631298161Sbapt int iEnd /* Byte offset of end of token within input text */ 10632298161Sbapt ) 10633298161Sbapt ); 10634298161Sbapt}; 10635298161Sbapt 10636298161Sbapt/* Flags that may be passed as the third argument to xTokenize() */ 10637298161Sbapt#define FTS5_TOKENIZE_QUERY 0x0001 10638298161Sbapt#define FTS5_TOKENIZE_PREFIX 0x0002 10639298161Sbapt#define FTS5_TOKENIZE_DOCUMENT 0x0004 10640298161Sbapt#define FTS5_TOKENIZE_AUX 0x0008 10641298161Sbapt 10642298161Sbapt/* Flags that may be passed by the tokenizer implementation back to FTS5 10643298161Sbapt** as the third argument to the supplied xToken callback. */ 10644298161Sbapt#define FTS5_TOKEN_COLOCATED 0x0001 /* Same position as prev. token */ 10645298161Sbapt 10646298161Sbapt/* 10647298161Sbapt** END OF CUSTOM TOKENIZERS 10648298161Sbapt*************************************************************************/ 10649298161Sbapt 10650298161Sbapt/************************************************************************* 10651298161Sbapt** FTS5 EXTENSION REGISTRATION API 10652298161Sbapt*/ 10653298161Sbapttypedef struct fts5_api fts5_api; 10654298161Sbaptstruct fts5_api { 10655298161Sbapt int iVersion; /* Currently always set to 2 */ 10656298161Sbapt 10657298161Sbapt /* Create a new tokenizer */ 10658298161Sbapt int (*xCreateTokenizer)( 10659298161Sbapt fts5_api *pApi, 10660298161Sbapt const char *zName, 10661298161Sbapt void *pContext, 10662298161Sbapt fts5_tokenizer *pTokenizer, 10663298161Sbapt void (*xDestroy)(void*) 10664298161Sbapt ); 10665298161Sbapt 10666298161Sbapt /* Find an existing tokenizer */ 10667298161Sbapt int (*xFindTokenizer)( 10668298161Sbapt fts5_api *pApi, 10669298161Sbapt const char *zName, 10670298161Sbapt void **ppContext, 10671298161Sbapt fts5_tokenizer *pTokenizer 10672298161Sbapt ); 10673298161Sbapt 10674298161Sbapt /* Create a new auxiliary function */ 10675298161Sbapt int (*xCreateFunction)( 10676298161Sbapt fts5_api *pApi, 10677298161Sbapt const char *zName, 10678298161Sbapt void *pContext, 10679298161Sbapt fts5_extension_function xFunction, 10680298161Sbapt void (*xDestroy)(void*) 10681298161Sbapt ); 10682298161Sbapt}; 10683298161Sbapt 10684298161Sbapt/* 10685298161Sbapt** END OF REGISTRATION API 10686298161Sbapt*************************************************************************/ 10687298161Sbapt 10688298161Sbapt#ifdef __cplusplus 10689298161Sbapt} /* end of the 'extern "C"' block */ 10690298161Sbapt#endif 10691298161Sbapt 10692298161Sbapt#endif /* _FTS5_H */ 10693298161Sbapt 10694305002Scy/******** End of fts5.h *********/ 10695