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**
111366076Scy** 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
118342292Scy** or SHA3-256 hash of the entire source tree.  If the source code has
119342292Scy** been edited in any way since it was last checked in, then the last
120342292Scy** four hexadecimal digits of the hash may be modified.
121251883Speter**
122251883Speter** See also: [sqlite3_libversion()],
123251883Speter** [sqlite3_libversion_number()], [sqlite3_sourceid()],
124251883Speter** [sqlite_version()] and [sqlite_source_id()].
125251883Speter*/
126369951Scy#define SQLITE_VERSION        "3.35.5"
127369951Scy#define SQLITE_VERSION_NUMBER 3035005
128369951Scy#define SQLITE_SOURCE_ID      "2021-04-19 18:32:05 1b256d97b553a9611efca188a3d995a2fff712759044ba480f9a0c9e98fae886"
129251883Speter
130251883Speter/*
131251883Speter** CAPI3REF: Run-Time Library Version Numbers
132322444Speter** KEYWORDS: sqlite3_version sqlite3_sourceid
133251883Speter**
134251883Speter** These interfaces provide the same information as the [SQLITE_VERSION],
135251883Speter** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
136251883Speter** but are associated with the library instead of the header file.  ^(Cautious
137251883Speter** programmers might include assert() statements in their application to
138251883Speter** verify that values returned by these interfaces match the macros in
139298161Sbapt** the header, and thus ensure that the application is
140251883Speter** compiled with matching library and header files.
141251883Speter**
142251883Speter** <blockquote><pre>
143251883Speter** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
144342292Scy** assert( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,80)==0 );
145251883Speter** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
146251883Speter** </pre></blockquote>)^
147251883Speter**
148251883Speter** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
149251883Speter** macro.  ^The sqlite3_libversion() function returns a pointer to the
150251883Speter** to the sqlite3_version[] string constant.  The sqlite3_libversion()
151251883Speter** function is provided for use in DLLs since DLL users usually do not have
152251883Speter** direct access to string constants within the DLL.  ^The
153251883Speter** sqlite3_libversion_number() function returns an integer equal to
154366076Scy** [SQLITE_VERSION_NUMBER].  ^(The sqlite3_sourceid() function returns
155366076Scy** a pointer to a string constant whose value is the same as the
156342292Scy** [SQLITE_SOURCE_ID] C preprocessor macro.  Except if SQLite is built
157342292Scy** using an edited copy of [the amalgamation], then the last four characters
158342292Scy** of the hash might be different from [SQLITE_SOURCE_ID].)^
159251883Speter**
160251883Speter** See also: [sqlite_version()] and [sqlite_source_id()].
161251883Speter*/
162251883SpeterSQLITE_API SQLITE_EXTERN const char sqlite3_version[];
163322444SpeterSQLITE_API const char *sqlite3_libversion(void);
164322444SpeterSQLITE_API const char *sqlite3_sourceid(void);
165322444SpeterSQLITE_API int sqlite3_libversion_number(void);
166251883Speter
167251883Speter/*
168251883Speter** CAPI3REF: Run-Time Library Compilation Options Diagnostics
169251883Speter**
170366076Scy** ^The sqlite3_compileoption_used() function returns 0 or 1
171366076Scy** indicating whether the specified option was defined at
172366076Scy** compile time.  ^The SQLITE_ prefix may be omitted from the
173366076Scy** option name passed to sqlite3_compileoption_used().
174251883Speter**
175251883Speter** ^The sqlite3_compileoption_get() function allows iterating
176251883Speter** over the list of options that were defined at compile time by
177251883Speter** returning the N-th compile time option string.  ^If N is out of range,
178366076Scy** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_
179366076Scy** prefix is omitted from any strings returned by
180251883Speter** sqlite3_compileoption_get().
181251883Speter**
182251883Speter** ^Support for the diagnostic functions sqlite3_compileoption_used()
183366076Scy** and sqlite3_compileoption_get() may be omitted by specifying the
184251883Speter** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
185251883Speter**
186251883Speter** See also: SQL functions [sqlite_compileoption_used()] and
187251883Speter** [sqlite_compileoption_get()] and the [compile_options pragma].
188251883Speter*/
189251883Speter#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
190322444SpeterSQLITE_API int sqlite3_compileoption_used(const char *zOptName);
191322444SpeterSQLITE_API const char *sqlite3_compileoption_get(int N);
192347347Scy#else
193347347Scy# define sqlite3_compileoption_used(X) 0
194347347Scy# define sqlite3_compileoption_get(X)  ((void*)0)
195251883Speter#endif
196251883Speter
197251883Speter/*
198251883Speter** CAPI3REF: Test To See If The Library Is Threadsafe
199251883Speter**
200251883Speter** ^The sqlite3_threadsafe() function returns zero if and only if
201251883Speter** SQLite was compiled with mutexing code omitted due to the
202251883Speter** [SQLITE_THREADSAFE] compile-time option being set to 0.
203251883Speter**
204251883Speter** SQLite can be compiled with or without mutexes.  When
205251883Speter** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
206251883Speter** are enabled and SQLite is threadsafe.  When the
207366076Scy** [SQLITE_THREADSAFE] macro is 0,
208251883Speter** the mutexes are omitted.  Without the mutexes, it is not safe
209251883Speter** to use SQLite concurrently from more than one thread.
210251883Speter**
211251883Speter** Enabling mutexes incurs a measurable performance penalty.
212251883Speter** So if speed is of utmost importance, it makes sense to disable
213251883Speter** the mutexes.  But for maximum safety, mutexes should be enabled.
214251883Speter** ^The default behavior is for mutexes to be enabled.
215251883Speter**
216251883Speter** This interface can be used by an application to make sure that the
217251883Speter** version of SQLite that it is linking against was compiled with
218251883Speter** the desired setting of the [SQLITE_THREADSAFE] macro.
219251883Speter**
220251883Speter** This interface only reports on the compile-time mutex setting
221251883Speter** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
222251883Speter** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
223251883Speter** can be fully or partially disabled using a call to [sqlite3_config()]
224251883Speter** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
225282328Sbapt** or [SQLITE_CONFIG_SERIALIZED].  ^(The return value of the
226251883Speter** sqlite3_threadsafe() function shows only the compile-time setting of
227251883Speter** thread safety, not any run-time changes to that setting made by
228251883Speter** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
229251883Speter** is unchanged by calls to sqlite3_config().)^
230251883Speter**
231251883Speter** See the [threading mode] documentation for additional information.
232251883Speter*/
233322444SpeterSQLITE_API int sqlite3_threadsafe(void);
234251883Speter
235251883Speter/*
236251883Speter** CAPI3REF: Database Connection Handle
237251883Speter** KEYWORDS: {database connection} {database connections}
238251883Speter**
239251883Speter** Each open SQLite database is represented by a pointer to an instance of
240251883Speter** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
241251883Speter** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
242251883Speter** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
243251883Speter** and [sqlite3_close_v2()] are its destructors.  There are many other
244251883Speter** interfaces (such as
245251883Speter** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
246251883Speter** [sqlite3_busy_timeout()] to name but three) that are methods on an
247251883Speter** sqlite3 object.
248251883Speter*/
249251883Spetertypedef struct sqlite3 sqlite3;
250251883Speter
251251883Speter/*
252251883Speter** CAPI3REF: 64-Bit Integer Types
253251883Speter** KEYWORDS: sqlite_int64 sqlite_uint64
254251883Speter**
255251883Speter** Because there is no cross-platform way to specify 64-bit integer types
256251883Speter** SQLite includes typedefs for 64-bit signed and unsigned integers.
257251883Speter**
258251883Speter** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
259251883Speter** The sqlite_int64 and sqlite_uint64 types are supported for backwards
260251883Speter** compatibility only.
261251883Speter**
262251883Speter** ^The sqlite3_int64 and sqlite_int64 types can store integer values
263251883Speter** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
264366076Scy** sqlite3_uint64 and sqlite_uint64 types can store integer values
265251883Speter** between 0 and +18446744073709551615 inclusive.
266251883Speter*/
267251883Speter#ifdef SQLITE_INT64_TYPE
268251883Speter  typedef SQLITE_INT64_TYPE sqlite_int64;
269322444Speter# ifdef SQLITE_UINT64_TYPE
270322444Speter    typedef SQLITE_UINT64_TYPE sqlite_uint64;
271366076Scy# else
272322444Speter    typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
273322444Speter# endif
274251883Speter#elif defined(_MSC_VER) || defined(__BORLANDC__)
275251883Speter  typedef __int64 sqlite_int64;
276251883Speter  typedef unsigned __int64 sqlite_uint64;
277251883Speter#else
278251883Speter  typedef long long int sqlite_int64;
279251883Speter  typedef unsigned long long int sqlite_uint64;
280251883Speter#endif
281251883Spetertypedef sqlite_int64 sqlite3_int64;
282251883Spetertypedef sqlite_uint64 sqlite3_uint64;
283251883Speter
284251883Speter/*
285251883Speter** If compiling for a processor that lacks floating point support,
286251883Speter** substitute integer for floating-point.
287251883Speter*/
288251883Speter#ifdef SQLITE_OMIT_FLOATING_POINT
289251883Speter# define double sqlite3_int64
290251883Speter#endif
291251883Speter
292251883Speter/*
293251883Speter** CAPI3REF: Closing A Database Connection
294286510Speter** DESTRUCTOR: sqlite3
295251883Speter**
296251883Speter** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
297251883Speter** for the [sqlite3] object.
298274884Sbapt** ^Calls to sqlite3_close() and sqlite3_close_v2() return [SQLITE_OK] if
299251883Speter** the [sqlite3] object is successfully destroyed and all associated
300251883Speter** resources are deallocated.
301251883Speter**
302362190Scy** Ideally, applications should [sqlite3_finalize | finalize] all
303366076Scy** [prepared statements], [sqlite3_blob_close | close] all [BLOB handles], and
304362190Scy** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
305362190Scy** with the [sqlite3] object prior to attempting to close the object.
306251883Speter** ^If the database connection is associated with unfinalized prepared
307362190Scy** statements, BLOB handlers, and/or unfinished sqlite3_backup objects then
308362190Scy** sqlite3_close() will leave the database connection open and return
309362190Scy** [SQLITE_BUSY]. ^If sqlite3_close_v2() is called with unfinalized prepared
310362190Scy** statements, unclosed BLOB handlers, and/or unfinished sqlite3_backups,
311362190Scy** it returns [SQLITE_OK] regardless, but instead of deallocating the database
312362190Scy** connection immediately, it marks the database connection as an unusable
313362190Scy** "zombie" and makes arrangements to automatically deallocate the database
314362190Scy** connection after all prepared statements are finalized, all BLOB handles
315362190Scy** are closed, and all backups have finished. The sqlite3_close_v2() interface
316362190Scy** is intended for use with host languages that are garbage collected, and
317362190Scy** where the order in which destructors are called is arbitrary.
318251883Speter**
319251883Speter** ^If an [sqlite3] object is destroyed while a transaction is open,
320251883Speter** the transaction is automatically rolled back.
321251883Speter**
322251883Speter** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
323251883Speter** must be either a NULL
324251883Speter** pointer or an [sqlite3] object pointer obtained
325251883Speter** from [sqlite3_open()], [sqlite3_open16()], or
326251883Speter** [sqlite3_open_v2()], and not previously closed.
327251883Speter** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
328251883Speter** argument is a harmless no-op.
329251883Speter*/
330322444SpeterSQLITE_API int sqlite3_close(sqlite3*);
331322444SpeterSQLITE_API int sqlite3_close_v2(sqlite3*);
332251883Speter
333251883Speter/*
334251883Speter** The type for a callback function.
335251883Speter** This is legacy and deprecated.  It is included for historical
336251883Speter** compatibility and is not documented.
337251883Speter*/
338251883Spetertypedef int (*sqlite3_callback)(void*,int,char**, char**);
339251883Speter
340251883Speter/*
341251883Speter** CAPI3REF: One-Step Query Execution Interface
342286510Speter** METHOD: sqlite3
343251883Speter**
344251883Speter** The sqlite3_exec() interface is a convenience wrapper around
345251883Speter** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
346251883Speter** that allows an application to run multiple statements of SQL
347366076Scy** without having to use a lot of C code.
348251883Speter**
349251883Speter** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
350251883Speter** semicolon-separate SQL statements passed into its 2nd argument,
351251883Speter** in the context of the [database connection] passed in as its 1st
352251883Speter** argument.  ^If the callback function of the 3rd argument to
353251883Speter** sqlite3_exec() is not NULL, then it is invoked for each result row
354251883Speter** coming out of the evaluated SQL statements.  ^The 4th argument to
355251883Speter** sqlite3_exec() is relayed through to the 1st argument of each
356251883Speter** callback invocation.  ^If the callback pointer to sqlite3_exec()
357251883Speter** is NULL, then no callback is ever invoked and result rows are
358251883Speter** ignored.
359251883Speter**
360251883Speter** ^If an error occurs while evaluating the SQL statements passed into
361251883Speter** sqlite3_exec(), then execution of the current statement stops and
362251883Speter** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
363251883Speter** is not NULL then any error message is written into memory obtained
364251883Speter** from [sqlite3_malloc()] and passed back through the 5th parameter.
365251883Speter** To avoid memory leaks, the application should invoke [sqlite3_free()]
366251883Speter** on error message strings returned through the 5th parameter of
367298161Sbapt** sqlite3_exec() after the error message string is no longer needed.
368251883Speter** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
369251883Speter** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
370251883Speter** NULL before returning.
371251883Speter**
372251883Speter** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
373251883Speter** routine returns SQLITE_ABORT without invoking the callback again and
374251883Speter** without running any subsequent SQL statements.
375251883Speter**
376251883Speter** ^The 2nd argument to the sqlite3_exec() callback function is the
377251883Speter** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
378251883Speter** callback is an array of pointers to strings obtained as if from
379251883Speter** [sqlite3_column_text()], one for each column.  ^If an element of a
380251883Speter** result row is NULL then the corresponding string pointer for the
381251883Speter** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
382251883Speter** sqlite3_exec() callback is an array of pointers to strings where each
383251883Speter** entry represents the name of corresponding result column as obtained
384251883Speter** from [sqlite3_column_name()].
385251883Speter**
386251883Speter** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
387366076Scy** to an empty string, or a pointer that contains only whitespace and/or
388251883Speter** SQL comments, then no SQL statements are evaluated and the database
389251883Speter** is not changed.
390251883Speter**
391251883Speter** Restrictions:
392251883Speter**
393251883Speter** <ul>
394298161Sbapt** <li> The application must ensure that the 1st parameter to sqlite3_exec()
395251883Speter**      is a valid and open [database connection].
396269851Speter** <li> The application must not close the [database connection] specified by
397251883Speter**      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
398251883Speter** <li> The application must not modify the SQL statement text passed into
399251883Speter**      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
400251883Speter** </ul>
401251883Speter*/
402322444SpeterSQLITE_API int sqlite3_exec(
403251883Speter  sqlite3*,                                  /* An open database */
404251883Speter  const char *sql,                           /* SQL to be evaluated */
405251883Speter  int (*callback)(void*,int,char**,char**),  /* Callback function */
406251883Speter  void *,                                    /* 1st argument to callback */
407251883Speter  char **errmsg                              /* Error msg written here */
408251883Speter);
409251883Speter
410251883Speter/*
411251883Speter** CAPI3REF: Result Codes
412274884Sbapt** KEYWORDS: {result code definitions}
413251883Speter**
414251883Speter** Many SQLite functions return an integer result code from the set shown
415251883Speter** here in order to indicate success or failure.
416251883Speter**
417251883Speter** New error codes may be added in future versions of SQLite.
418251883Speter**
419274884Sbapt** See also: [extended result code definitions]
420251883Speter*/
421251883Speter#define SQLITE_OK           0   /* Successful result */
422251883Speter/* beginning-of-error-codes */
423322444Speter#define SQLITE_ERROR        1   /* Generic error */
424251883Speter#define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
425251883Speter#define SQLITE_PERM         3   /* Access permission denied */
426251883Speter#define SQLITE_ABORT        4   /* Callback routine requested an abort */
427251883Speter#define SQLITE_BUSY         5   /* The database file is locked */
428251883Speter#define SQLITE_LOCKED       6   /* A table in the database is locked */
429251883Speter#define SQLITE_NOMEM        7   /* A malloc() failed */
430251883Speter#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
431251883Speter#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
432251883Speter#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
433251883Speter#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
434251883Speter#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
435251883Speter#define SQLITE_FULL        13   /* Insertion failed because database is full */
436251883Speter#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
437251883Speter#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
438342292Scy#define SQLITE_EMPTY       16   /* Internal use only */
439251883Speter#define SQLITE_SCHEMA      17   /* The database schema changed */
440251883Speter#define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
441251883Speter#define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
442251883Speter#define SQLITE_MISMATCH    20   /* Data type mismatch */
443251883Speter#define SQLITE_MISUSE      21   /* Library used incorrectly */
444251883Speter#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
445251883Speter#define SQLITE_AUTH        23   /* Authorization denied */
446322444Speter#define SQLITE_FORMAT      24   /* Not used */
447251883Speter#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
448251883Speter#define SQLITE_NOTADB      26   /* File opened that is not a database file */
449251883Speter#define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
450251883Speter#define SQLITE_WARNING     28   /* Warnings from sqlite3_log() */
451251883Speter#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
452251883Speter#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
453251883Speter/* end-of-error-codes */
454251883Speter
455251883Speter/*
456251883Speter** CAPI3REF: Extended Result Codes
457274884Sbapt** KEYWORDS: {extended result code definitions}
458251883Speter**
459274884Sbapt** In its default configuration, SQLite API routines return one of 30 integer
460274884Sbapt** [result codes].  However, experience has shown that many of
461251883Speter** these result codes are too coarse-grained.  They do not provide as
462251883Speter** much information about problems as programmers might like.  In an effort to
463322444Speter** address this, newer versions of SQLite (version 3.3.8 [dateof:3.3.8]
464322444Speter** and later) include
465251883Speter** support for additional result codes that provide more detailed information
466274884Sbapt** about errors. These [extended result codes] are enabled or disabled
467251883Speter** on a per database connection basis using the
468274884Sbapt** [sqlite3_extended_result_codes()] API.  Or, the extended code for
469274884Sbapt** the most recent error can be obtained using
470274884Sbapt** [sqlite3_extended_errcode()].
471251883Speter*/
472342292Scy#define SQLITE_ERROR_MISSING_COLLSEQ   (SQLITE_ERROR | (1<<8))
473342292Scy#define SQLITE_ERROR_RETRY             (SQLITE_ERROR | (2<<8))
474342292Scy#define SQLITE_ERROR_SNAPSHOT          (SQLITE_ERROR | (3<<8))
475251883Speter#define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
476251883Speter#define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
477251883Speter#define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
478251883Speter#define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
479251883Speter#define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
480251883Speter#define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
481251883Speter#define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
482251883Speter#define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
483251883Speter#define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
484251883Speter#define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
485251883Speter#define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
486251883Speter#define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
487251883Speter#define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
488251883Speter#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
489251883Speter#define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
490251883Speter#define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
491251883Speter#define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
492251883Speter#define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
493251883Speter#define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
494251883Speter#define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
495251883Speter#define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
496251883Speter#define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
497251883Speter#define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
498251883Speter#define SQLITE_IOERR_MMAP              (SQLITE_IOERR | (24<<8))
499269851Speter#define SQLITE_IOERR_GETTEMPPATH       (SQLITE_IOERR | (25<<8))
500269851Speter#define SQLITE_IOERR_CONVPATH          (SQLITE_IOERR | (26<<8))
501298161Sbapt#define SQLITE_IOERR_VNODE             (SQLITE_IOERR | (27<<8))
502298161Sbapt#define SQLITE_IOERR_AUTH              (SQLITE_IOERR | (28<<8))
503342292Scy#define SQLITE_IOERR_BEGIN_ATOMIC      (SQLITE_IOERR | (29<<8))
504342292Scy#define SQLITE_IOERR_COMMIT_ATOMIC     (SQLITE_IOERR | (30<<8))
505342292Scy#define SQLITE_IOERR_ROLLBACK_ATOMIC   (SQLITE_IOERR | (31<<8))
506362190Scy#define SQLITE_IOERR_DATA              (SQLITE_IOERR | (32<<8))
507369950Scy#define SQLITE_IOERR_CORRUPTFS         (SQLITE_IOERR | (33<<8))
508251883Speter#define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
509342292Scy#define SQLITE_LOCKED_VTAB             (SQLITE_LOCKED |  (2<<8))
510251883Speter#define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
511269851Speter#define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
512362190Scy#define SQLITE_BUSY_TIMEOUT            (SQLITE_BUSY   |  (3<<8))
513251883Speter#define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
514251883Speter#define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
515251883Speter#define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
516269851Speter#define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
517342292Scy#define SQLITE_CANTOPEN_DIRTYWAL       (SQLITE_CANTOPEN | (5<<8)) /* Not Used */
518361456Scy#define SQLITE_CANTOPEN_SYMLINK        (SQLITE_CANTOPEN | (6<<8))
519251883Speter#define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
520342292Scy#define SQLITE_CORRUPT_SEQUENCE        (SQLITE_CORRUPT | (2<<8))
521362190Scy#define SQLITE_CORRUPT_INDEX           (SQLITE_CORRUPT | (3<<8))
522251883Speter#define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
523251883Speter#define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
524251883Speter#define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
525269851Speter#define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
526342292Scy#define SQLITE_READONLY_CANTINIT       (SQLITE_READONLY | (5<<8))
527342292Scy#define SQLITE_READONLY_DIRECTORY      (SQLITE_READONLY | (6<<8))
528251883Speter#define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
529251883Speter#define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
530251883Speter#define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
531251883Speter#define SQLITE_CONSTRAINT_FOREIGNKEY   (SQLITE_CONSTRAINT | (3<<8))
532251883Speter#define SQLITE_CONSTRAINT_FUNCTION     (SQLITE_CONSTRAINT | (4<<8))
533251883Speter#define SQLITE_CONSTRAINT_NOTNULL      (SQLITE_CONSTRAINT | (5<<8))
534251883Speter#define SQLITE_CONSTRAINT_PRIMARYKEY   (SQLITE_CONSTRAINT | (6<<8))
535251883Speter#define SQLITE_CONSTRAINT_TRIGGER      (SQLITE_CONSTRAINT | (7<<8))
536251883Speter#define SQLITE_CONSTRAINT_UNIQUE       (SQLITE_CONSTRAINT | (8<<8))
537251883Speter#define SQLITE_CONSTRAINT_VTAB         (SQLITE_CONSTRAINT | (9<<8))
538269851Speter#define SQLITE_CONSTRAINT_ROWID        (SQLITE_CONSTRAINT |(10<<8))
539361456Scy#define SQLITE_CONSTRAINT_PINNED       (SQLITE_CONSTRAINT |(11<<8))
540251883Speter#define SQLITE_NOTICE_RECOVER_WAL      (SQLITE_NOTICE | (1<<8))
541251883Speter#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
542269851Speter#define SQLITE_WARNING_AUTOINDEX       (SQLITE_WARNING | (1<<8))
543274884Sbapt#define SQLITE_AUTH_USER               (SQLITE_AUTH | (1<<8))
544305002Scy#define SQLITE_OK_LOAD_PERMANENTLY     (SQLITE_OK | (1<<8))
545361456Scy#define SQLITE_OK_SYMLINK              (SQLITE_OK | (2<<8))
546251883Speter
547251883Speter/*
548251883Speter** CAPI3REF: Flags For File Open Operations
549251883Speter**
550251883Speter** These bit values are intended for use in the
551251883Speter** 3rd parameter to the [sqlite3_open_v2()] interface and
552251883Speter** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
553251883Speter*/
554251883Speter#define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
555251883Speter#define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
556251883Speter#define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
557251883Speter#define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
558251883Speter#define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
559251883Speter#define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
560251883Speter#define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
561251883Speter#define SQLITE_OPEN_MEMORY           0x00000080  /* Ok for sqlite3_open_v2() */
562251883Speter#define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
563251883Speter#define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
564251883Speter#define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
565251883Speter#define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
566251883Speter#define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
567251883Speter#define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
568366076Scy#define SQLITE_OPEN_SUPER_JOURNAL    0x00004000  /* VFS only */
569251883Speter#define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
570251883Speter#define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
571251883Speter#define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
572251883Speter#define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
573251883Speter#define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
574361456Scy#define SQLITE_OPEN_NOFOLLOW         0x01000000  /* Ok for sqlite3_open_v2() */
575251883Speter
576251883Speter/* Reserved:                         0x00F00000 */
577366076Scy/* Legacy compatibility: */
578366076Scy#define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
579251883Speter
580366076Scy
581251883Speter/*
582251883Speter** CAPI3REF: Device Characteristics
583251883Speter**
584251883Speter** The xDeviceCharacteristics method of the [sqlite3_io_methods]
585251883Speter** object returns an integer which is a vector of these
586251883Speter** bit values expressing I/O characteristics of the mass storage
587251883Speter** device that holds the file that the [sqlite3_io_methods]
588251883Speter** refers to.
589251883Speter**
590251883Speter** The SQLITE_IOCAP_ATOMIC property means that all writes of
591251883Speter** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
592251883Speter** mean that writes of blocks that are nnn bytes in size and
593251883Speter** are aligned to an address which is an integer multiple of
594251883Speter** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
595251883Speter** that when data is appended to a file, the data is appended
596251883Speter** first then the size of the file is extended, never the other
597251883Speter** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
598251883Speter** information is written to disk in the same order as calls
599251883Speter** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
600251883Speter** after reboot following a crash or power loss, the only bytes in a
601251883Speter** file that were written at the application level might have changed
602251883Speter** and that adjacent bytes, even bytes within the same sector are
603269851Speter** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
604322444Speter** flag indicates that a file cannot be deleted when open.  The
605269851Speter** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
606269851Speter** read-only media and cannot be changed even by processes with
607269851Speter** elevated privileges.
608342292Scy**
609342292Scy** The SQLITE_IOCAP_BATCH_ATOMIC property means that the underlying
610342292Scy** filesystem supports doing multiple write operations atomically when those
611342292Scy** write operations are bracketed by [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] and
612342292Scy** [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].
613251883Speter*/
614251883Speter#define SQLITE_IOCAP_ATOMIC                 0x00000001
615251883Speter#define SQLITE_IOCAP_ATOMIC512              0x00000002
616251883Speter#define SQLITE_IOCAP_ATOMIC1K               0x00000004
617251883Speter#define SQLITE_IOCAP_ATOMIC2K               0x00000008
618251883Speter#define SQLITE_IOCAP_ATOMIC4K               0x00000010
619251883Speter#define SQLITE_IOCAP_ATOMIC8K               0x00000020
620251883Speter#define SQLITE_IOCAP_ATOMIC16K              0x00000040
621251883Speter#define SQLITE_IOCAP_ATOMIC32K              0x00000080
622251883Speter#define SQLITE_IOCAP_ATOMIC64K              0x00000100
623251883Speter#define SQLITE_IOCAP_SAFE_APPEND            0x00000200
624251883Speter#define SQLITE_IOCAP_SEQUENTIAL             0x00000400
625251883Speter#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
626251883Speter#define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
627269851Speter#define SQLITE_IOCAP_IMMUTABLE              0x00002000
628342292Scy#define SQLITE_IOCAP_BATCH_ATOMIC           0x00004000
629251883Speter
630251883Speter/*
631251883Speter** CAPI3REF: File Locking Levels
632251883Speter**
633251883Speter** SQLite uses one of these integer values as the second
634251883Speter** argument to calls it makes to the xLock() and xUnlock() methods
635251883Speter** of an [sqlite3_io_methods] object.
636251883Speter*/
637251883Speter#define SQLITE_LOCK_NONE          0
638251883Speter#define SQLITE_LOCK_SHARED        1
639251883Speter#define SQLITE_LOCK_RESERVED      2
640251883Speter#define SQLITE_LOCK_PENDING       3
641251883Speter#define SQLITE_LOCK_EXCLUSIVE     4
642251883Speter
643251883Speter/*
644251883Speter** CAPI3REF: Synchronization Type Flags
645251883Speter**
646251883Speter** When SQLite invokes the xSync() method of an
647251883Speter** [sqlite3_io_methods] object it uses a combination of
648251883Speter** these integer values as the second argument.
649251883Speter**
650251883Speter** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
651251883Speter** sync operation only needs to flush data to mass storage.  Inode
652251883Speter** information need not be flushed. If the lower four bits of the flag
653251883Speter** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
654251883Speter** If the lower four bits equal SQLITE_SYNC_FULL, that means
655251883Speter** to use Mac OS X style fullsync instead of fsync().
656251883Speter**
657251883Speter** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
658251883Speter** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
659251883Speter** settings.  The [synchronous pragma] determines when calls to the
660251883Speter** xSync VFS method occur and applies uniformly across all platforms.
661251883Speter** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
662251883Speter** energetic or rigorous or forceful the sync operations are and
663251883Speter** only make a difference on Mac OSX for the default SQLite code.
664251883Speter** (Third-party VFS implementations might also make the distinction
665251883Speter** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
666251883Speter** operating systems natively supported by SQLite, only Mac OSX
667251883Speter** cares about the difference.)
668251883Speter*/
669251883Speter#define SQLITE_SYNC_NORMAL        0x00002
670251883Speter#define SQLITE_SYNC_FULL          0x00003
671251883Speter#define SQLITE_SYNC_DATAONLY      0x00010
672251883Speter
673251883Speter/*
674251883Speter** CAPI3REF: OS Interface Open File Handle
675251883Speter**
676366076Scy** An [sqlite3_file] object represents an open file in the
677251883Speter** [sqlite3_vfs | OS interface layer].  Individual OS interface
678251883Speter** implementations will
679251883Speter** want to subclass this object by appending additional fields
680251883Speter** for their own use.  The pMethods entry is a pointer to an
681251883Speter** [sqlite3_io_methods] object that defines methods for performing
682251883Speter** I/O operations on the open file.
683251883Speter*/
684251883Spetertypedef struct sqlite3_file sqlite3_file;
685251883Speterstruct sqlite3_file {
686251883Speter  const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
687251883Speter};
688251883Speter
689251883Speter/*
690251883Speter** CAPI3REF: OS Interface File Virtual Methods Object
691251883Speter**
692251883Speter** Every file opened by the [sqlite3_vfs.xOpen] method populates an
693251883Speter** [sqlite3_file] object (or, more commonly, a subclass of the
694251883Speter** [sqlite3_file] object) with a pointer to an instance of this object.
695251883Speter** This object defines the methods used to perform various operations
696251883Speter** against the open file represented by the [sqlite3_file] object.
697251883Speter**
698366076Scy** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
699251883Speter** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
700251883Speter** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed.  The
701251883Speter** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
702251883Speter** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
703251883Speter** to NULL.
704251883Speter**
705251883Speter** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
706251883Speter** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
707251883Speter** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
708251883Speter** flag may be ORed in to indicate that only the data of the file
709251883Speter** and not its inode needs to be synced.
710251883Speter**
711251883Speter** The integer values to xLock() and xUnlock() are one of
712251883Speter** <ul>
713251883Speter** <li> [SQLITE_LOCK_NONE],
714251883Speter** <li> [SQLITE_LOCK_SHARED],
715251883Speter** <li> [SQLITE_LOCK_RESERVED],
716251883Speter** <li> [SQLITE_LOCK_PENDING], or
717251883Speter** <li> [SQLITE_LOCK_EXCLUSIVE].
718251883Speter** </ul>
719251883Speter** xLock() increases the lock. xUnlock() decreases the lock.
720251883Speter** The xCheckReservedLock() method checks whether any database connection,
721251883Speter** either in this process or in some other process, is holding a RESERVED,
722251883Speter** PENDING, or EXCLUSIVE lock on the file.  It returns true
723251883Speter** if such a lock exists and false otherwise.
724251883Speter**
725251883Speter** The xFileControl() method is a generic interface that allows custom
726251883Speter** VFS implementations to directly control an open file using the
727251883Speter** [sqlite3_file_control()] interface.  The second "op" argument is an
728251883Speter** integer opcode.  The third argument is a generic pointer intended to
729251883Speter** point to a structure that may contain arguments or space in which to
730251883Speter** write return values.  Potential uses for xFileControl() might be
731251883Speter** functions to enable blocking locks with timeouts, to change the
732251883Speter** locking strategy (for example to use dot-file locks), to inquire
733251883Speter** about the status of a lock, or to break stale locks.  The SQLite
734251883Speter** core reserves all opcodes less than 100 for its own use.
735274884Sbapt** A [file control opcodes | list of opcodes] less than 100 is available.
736251883Speter** Applications that define a custom xFileControl method should use opcodes
737251883Speter** greater than 100 to avoid conflicts.  VFS implementations should
738251883Speter** return [SQLITE_NOTFOUND] for file control opcodes that they do not
739251883Speter** recognize.
740251883Speter**
741251883Speter** The xSectorSize() method returns the sector size of the
742251883Speter** device that underlies the file.  The sector size is the
743251883Speter** minimum write that can be performed without disturbing
744251883Speter** other bytes in the file.  The xDeviceCharacteristics()
745251883Speter** method returns a bit vector describing behaviors of the
746251883Speter** underlying device:
747251883Speter**
748251883Speter** <ul>
749251883Speter** <li> [SQLITE_IOCAP_ATOMIC]
750251883Speter** <li> [SQLITE_IOCAP_ATOMIC512]
751251883Speter** <li> [SQLITE_IOCAP_ATOMIC1K]
752251883Speter** <li> [SQLITE_IOCAP_ATOMIC2K]
753251883Speter** <li> [SQLITE_IOCAP_ATOMIC4K]
754251883Speter** <li> [SQLITE_IOCAP_ATOMIC8K]
755251883Speter** <li> [SQLITE_IOCAP_ATOMIC16K]
756251883Speter** <li> [SQLITE_IOCAP_ATOMIC32K]
757251883Speter** <li> [SQLITE_IOCAP_ATOMIC64K]
758251883Speter** <li> [SQLITE_IOCAP_SAFE_APPEND]
759251883Speter** <li> [SQLITE_IOCAP_SEQUENTIAL]
760322444Speter** <li> [SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN]
761322444Speter** <li> [SQLITE_IOCAP_POWERSAFE_OVERWRITE]
762322444Speter** <li> [SQLITE_IOCAP_IMMUTABLE]
763342292Scy** <li> [SQLITE_IOCAP_BATCH_ATOMIC]
764251883Speter** </ul>
765251883Speter**
766251883Speter** The SQLITE_IOCAP_ATOMIC property means that all writes of
767251883Speter** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
768251883Speter** mean that writes of blocks that are nnn bytes in size and
769251883Speter** are aligned to an address which is an integer multiple of
770251883Speter** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
771251883Speter** that when data is appended to a file, the data is appended
772251883Speter** first then the size of the file is extended, never the other
773251883Speter** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
774251883Speter** information is written to disk in the same order as calls
775251883Speter** to xWrite().
776251883Speter**
777251883Speter** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
778251883Speter** in the unread portions of the buffer with zeros.  A VFS that
779251883Speter** fails to zero-fill short reads might seem to work.  However,
780251883Speter** failure to zero-fill short reads will eventually lead to
781251883Speter** database corruption.
782251883Speter*/
783251883Spetertypedef struct sqlite3_io_methods sqlite3_io_methods;
784251883Speterstruct sqlite3_io_methods {
785251883Speter  int iVersion;
786251883Speter  int (*xClose)(sqlite3_file*);
787251883Speter  int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
788251883Speter  int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
789251883Speter  int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
790251883Speter  int (*xSync)(sqlite3_file*, int flags);
791251883Speter  int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
792251883Speter  int (*xLock)(sqlite3_file*, int);
793251883Speter  int (*xUnlock)(sqlite3_file*, int);
794251883Speter  int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
795251883Speter  int (*xFileControl)(sqlite3_file*, int op, void *pArg);
796251883Speter  int (*xSectorSize)(sqlite3_file*);
797251883Speter  int (*xDeviceCharacteristics)(sqlite3_file*);
798251883Speter  /* Methods above are valid for version 1 */
799251883Speter  int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
800251883Speter  int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
801251883Speter  void (*xShmBarrier)(sqlite3_file*);
802251883Speter  int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
803251883Speter  /* Methods above are valid for version 2 */
804251883Speter  int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
805251883Speter  int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
806251883Speter  /* Methods above are valid for version 3 */
807251883Speter  /* Additional methods may be added in future releases */
808251883Speter};
809251883Speter
810251883Speter/*
811251883Speter** CAPI3REF: Standard File Control Opcodes
812274884Sbapt** KEYWORDS: {file control opcodes} {file control opcode}
813251883Speter**
814251883Speter** These integer constants are opcodes for the xFileControl method
815251883Speter** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
816251883Speter** interface.
817251883Speter**
818282328Sbapt** <ul>
819282328Sbapt** <li>[[SQLITE_FCNTL_LOCKSTATE]]
820251883Speter** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
821251883Speter** opcode causes the xFileControl method to write the current state of
822251883Speter** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
823251883Speter** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
824251883Speter** into an integer that the pArg argument points to. This capability
825282328Sbapt** is used during testing and is only available when the SQLITE_TEST
826282328Sbapt** compile-time option is used.
827282328Sbapt**
828251883Speter** <li>[[SQLITE_FCNTL_SIZE_HINT]]
829251883Speter** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
830251883Speter** layer a hint of how large the database file will grow to be during the
831251883Speter** current transaction.  This hint is not guaranteed to be accurate but it
832251883Speter** is often close.  The underlying VFS might choose to preallocate database
833251883Speter** file space based on this hint in order to help writes to the database
834251883Speter** file run faster.
835251883Speter**
836346442Scy** <li>[[SQLITE_FCNTL_SIZE_LIMIT]]
837346442Scy** The [SQLITE_FCNTL_SIZE_LIMIT] opcode is used by in-memory VFS that
838346442Scy** implements [sqlite3_deserialize()] to set an upper bound on the size
839346442Scy** of the in-memory database.  The argument is a pointer to a [sqlite3_int64].
840346442Scy** If the integer pointed to is negative, then it is filled in with the
841346442Scy** current limit.  Otherwise the limit is set to the larger of the value
842346442Scy** of the integer pointed to and the current database size.  The integer
843346442Scy** pointed to is set to the new limit.
844346442Scy**
845251883Speter** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
846251883Speter** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
847251883Speter** extends and truncates the database file in chunks of a size specified
848366076Scy** by the user. The fourth argument to [sqlite3_file_control()] should
849251883Speter** point to an integer (type int) containing the new chunk-size to use
850251883Speter** for the nominated database. Allocating database file space in large
851251883Speter** chunks (say 1MB at a time), may reduce file-system fragmentation and
852251883Speter** improve performance on some systems.
853251883Speter**
854251883Speter** <li>[[SQLITE_FCNTL_FILE_POINTER]]
855251883Speter** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
856251883Speter** to the [sqlite3_file] object associated with a particular database
857298161Sbapt** connection.  See also [SQLITE_FCNTL_JOURNAL_POINTER].
858251883Speter**
859298161Sbapt** <li>[[SQLITE_FCNTL_JOURNAL_POINTER]]
860298161Sbapt** The [SQLITE_FCNTL_JOURNAL_POINTER] opcode is used to obtain a pointer
861298161Sbapt** to the [sqlite3_file] object associated with the journal file (either
862298161Sbapt** the [rollback journal] or the [write-ahead log]) for a particular database
863298161Sbapt** connection.  See also [SQLITE_FCNTL_FILE_POINTER].
864298161Sbapt**
865251883Speter** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
866269851Speter** No longer in use.
867251883Speter**
868269851Speter** <li>[[SQLITE_FCNTL_SYNC]]
869269851Speter** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
870269851Speter** sent to the VFS immediately before the xSync method is invoked on a
871366076Scy** database file descriptor. Or, if the xSync method is not invoked
872366076Scy** because the user has configured SQLite with
873366076Scy** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
874269851Speter** of the xSync method. In most cases, the pointer argument passed with
875269851Speter** this file-control is NULL. However, if the database file is being synced
876269851Speter** as part of a multi-database commit, the argument points to a nul-terminated
877366076Scy** string containing the transactions super-journal file name. VFSes that
878366076Scy** do not need this signal should silently ignore this opcode. Applications
879366076Scy** should not call [sqlite3_file_control()] with this opcode as doing so may
880366076Scy** disrupt the operation of the specialized VFSes that do require it.
881269851Speter**
882269851Speter** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
883269851Speter** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
884269851Speter** and sent to the VFS after a transaction has been committed immediately
885269851Speter** but before the database is unlocked. VFSes that do not need this signal
886269851Speter** should silently ignore this opcode. Applications should not call
887366076Scy** [sqlite3_file_control()] with this opcode as doing so may disrupt the
888366076Scy** operation of the specialized VFSes that do require it.
889269851Speter**
890251883Speter** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
891251883Speter** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
892251883Speter** retry counts and intervals for certain disk I/O operations for the
893251883Speter** windows [VFS] in order to provide robustness in the presence of
894251883Speter** anti-virus programs.  By default, the windows VFS will retry file read,
895251883Speter** file write, and file delete operations up to 10 times, with a delay
896251883Speter** of 25 milliseconds before the first retry and with the delay increasing
897251883Speter** by an additional 25 milliseconds with each subsequent retry.  This
898251883Speter** opcode allows these two values (10 retries and 25 milliseconds of delay)
899251883Speter** to be adjusted.  The values are changed for all database connections
900251883Speter** within the same process.  The argument is a pointer to an array of two
901322444Speter** integers where the first integer is the new retry count and the second
902251883Speter** integer is the delay.  If either integer is negative, then the setting
903251883Speter** is not changed but instead the prior value of that setting is written
904251883Speter** into the array entry, allowing the current retry settings to be
905251883Speter** interrogated.  The zDbName parameter is ignored.
906251883Speter**
907251883Speter** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
908251883Speter** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
909251883Speter** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
910342292Scy** write ahead log ([WAL file]) and shared memory
911342292Scy** files used for transaction control
912251883Speter** are automatically deleted when the latest connection to the database
913251883Speter** closes.  Setting persistent WAL mode causes those files to persist after
914251883Speter** close.  Persisting the files is useful when other processes that do not
915251883Speter** have write permission on the directory containing the database file want
916251883Speter** to read the database file, as the WAL and shared memory files must exist
917251883Speter** in order for the database to be readable.  The fourth parameter to
918251883Speter** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
919251883Speter** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
920251883Speter** WAL mode.  If the integer is -1, then it is overwritten with the current
921251883Speter** WAL persistence setting.
922251883Speter**
923251883Speter** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
924251883Speter** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
925251883Speter** persistent "powersafe-overwrite" or "PSOW" setting.  The PSOW setting
926251883Speter** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
927251883Speter** xDeviceCharacteristics methods. The fourth parameter to
928251883Speter** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
929251883Speter** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
930251883Speter** mode.  If the integer is -1, then it is overwritten with the current
931251883Speter** zero-damage mode setting.
932251883Speter**
933251883Speter** <li>[[SQLITE_FCNTL_OVERWRITE]]
934251883Speter** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
935251883Speter** a write transaction to indicate that, unless it is rolled back for some
936366076Scy** reason, the entire database file will be overwritten by the current
937251883Speter** transaction. This is used by VACUUM operations.
938251883Speter**
939251883Speter** <li>[[SQLITE_FCNTL_VFSNAME]]
940251883Speter** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
941251883Speter** all [VFSes] in the VFS stack.  The names are of all VFS shims and the
942366076Scy** final bottom-level VFS are written into memory obtained from
943251883Speter** [sqlite3_malloc()] and the result is stored in the char* variable
944251883Speter** that the fourth parameter of [sqlite3_file_control()] points to.
945251883Speter** The caller is responsible for freeing the memory when done.  As with
946251883Speter** all file-control actions, there is no guarantee that this will actually
947251883Speter** do anything.  Callers should initialize the char* variable to a NULL
948251883Speter** pointer in case this file-control is not implemented.  This file-control
949251883Speter** is intended for diagnostic use only.
950251883Speter**
951298161Sbapt** <li>[[SQLITE_FCNTL_VFS_POINTER]]
952298161Sbapt** ^The [SQLITE_FCNTL_VFS_POINTER] opcode finds a pointer to the top-level
953298161Sbapt** [VFSes] currently in use.  ^(The argument X in
954298161Sbapt** sqlite3_file_control(db,SQLITE_FCNTL_VFS_POINTER,X) must be
955298161Sbapt** of type "[sqlite3_vfs] **".  This opcodes will set *X
956298161Sbapt** to a pointer to the top-level VFS.)^
957298161Sbapt** ^When there are multiple VFS shims in the stack, this opcode finds the
958298161Sbapt** upper-most shim only.
959298161Sbapt**
960251883Speter** <li>[[SQLITE_FCNTL_PRAGMA]]
961366076Scy** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
962251883Speter** file control is sent to the open [sqlite3_file] object corresponding
963251883Speter** to the database file to which the pragma statement refers. ^The argument
964251883Speter** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
965251883Speter** pointers to strings (char**) in which the second element of the array
966251883Speter** is the name of the pragma and the third element is the argument to the
967251883Speter** pragma or NULL if the pragma has no argument.  ^The handler for an
968251883Speter** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
969251883Speter** of the char** argument point to a string obtained from [sqlite3_mprintf()]
970251883Speter** or the equivalent and that string will become the result of the pragma or
971251883Speter** the error message if the pragma fails. ^If the
972366076Scy** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
973251883Speter** [PRAGMA] processing continues.  ^If the [SQLITE_FCNTL_PRAGMA]
974251883Speter** file control returns [SQLITE_OK], then the parser assumes that the
975251883Speter** VFS has handled the PRAGMA itself and the parser generates a no-op
976282328Sbapt** prepared statement if result string is NULL, or that returns a copy
977282328Sbapt** of the result string if the string is non-NULL.
978282328Sbapt** ^If the [SQLITE_FCNTL_PRAGMA] file control returns
979251883Speter** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
980251883Speter** that the VFS encountered an error while handling the [PRAGMA] and the
981251883Speter** compilation of the PRAGMA fails with an error.  ^The [SQLITE_FCNTL_PRAGMA]
982251883Speter** file control occurs at the beginning of pragma statement analysis and so
983251883Speter** it is able to override built-in [PRAGMA] statements.
984251883Speter**
985251883Speter** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
986251883Speter** ^The [SQLITE_FCNTL_BUSYHANDLER]
987251883Speter** file-control may be invoked by SQLite on the database file handle
988251883Speter** shortly after it is opened in order to provide a custom VFS with access
989361456Scy** to the connection's busy-handler callback. The argument is of type (void**)
990251883Speter** - an array of two (void *) values. The first (void *) actually points
991361456Scy** to a function of type (int (*)(void *)). In order to invoke the connection's
992251883Speter** busy-handler, this function should be invoked with the second (void *) in
993251883Speter** the array as the only argument. If it returns non-zero, then the operation
994251883Speter** should be retried. If it returns zero, the custom VFS should abandon the
995251883Speter** current operation.
996251883Speter**
997251883Speter** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
998361456Scy** ^Applications can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
999251883Speter** to have SQLite generate a
1000251883Speter** temporary filename using the same algorithm that is followed to generate
1001251883Speter** temporary filenames for TEMP tables and other internal uses.  The
1002251883Speter** argument should be a char** which will be filled with the filename
1003251883Speter** written into memory obtained from [sqlite3_malloc()].  The caller should
1004251883Speter** invoke [sqlite3_free()] on the result to avoid a memory leak.
1005251883Speter**
1006251883Speter** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
1007251883Speter** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
1008251883Speter** maximum number of bytes that will be used for memory-mapped I/O.
1009251883Speter** The argument is a pointer to a value of type sqlite3_int64 that
1010251883Speter** is an advisory maximum number of bytes in the file to memory map.  The
1011251883Speter** pointer is overwritten with the old value.  The limit is not changed if
1012366076Scy** the value originally pointed to is negative, and so the current limit
1013251883Speter** can be queried by passing in a pointer to a negative number.  This
1014251883Speter** file-control is used internally to implement [PRAGMA mmap_size].
1015251883Speter**
1016269851Speter** <li>[[SQLITE_FCNTL_TRACE]]
1017269851Speter** The [SQLITE_FCNTL_TRACE] file control provides advisory information
1018269851Speter** to the VFS about what the higher layers of the SQLite stack are doing.
1019269851Speter** This file control is used by some VFS activity tracing [shims].
1020269851Speter** The argument is a zero-terminated string.  Higher layers in the
1021269851Speter** SQLite stack may generate instances of this file control if
1022269851Speter** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1023269851Speter**
1024269851Speter** <li>[[SQLITE_FCNTL_HAS_MOVED]]
1025269851Speter** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
1026269851Speter** pointer to an integer and it writes a boolean into that integer depending
1027269851Speter** on whether or not the file has been renamed, moved, or deleted since it
1028269851Speter** was first opened.
1029269851Speter**
1030322444Speter** <li>[[SQLITE_FCNTL_WIN32_GET_HANDLE]]
1031322444Speter** The [SQLITE_FCNTL_WIN32_GET_HANDLE] opcode can be used to obtain the
1032322444Speter** underlying native file handle associated with a file handle.  This file
1033322444Speter** control interprets its argument as a pointer to a native file handle and
1034322444Speter** writes the resulting value there.
1035322444Speter**
1036269851Speter** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
1037269851Speter** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging.  This
1038269851Speter** opcode causes the xFileControl method to swap the file handle with the one
1039269851Speter** pointed to by the pArg argument.  This capability is used during testing
1040269851Speter** and only needs to be supported when SQLITE_TEST is defined.
1041269851Speter**
1042282328Sbapt** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
1043282328Sbapt** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
1044282328Sbapt** be advantageous to block on the next WAL lock if the lock is not immediately
1045282328Sbapt** available.  The WAL subsystem issues this signal during rare
1046282328Sbapt** circumstances in order to fix a problem with priority inversion.
1047282328Sbapt** Applications should <em>not</em> use this file-control.
1048282328Sbapt**
1049286510Speter** <li>[[SQLITE_FCNTL_ZIPVFS]]
1050286510Speter** The [SQLITE_FCNTL_ZIPVFS] opcode is implemented by zipvfs only. All other
1051286510Speter** VFS should return SQLITE_NOTFOUND for this opcode.
1052286510Speter**
1053286510Speter** <li>[[SQLITE_FCNTL_RBU]]
1054286510Speter** The [SQLITE_FCNTL_RBU] opcode is implemented by the special VFS used by
1055286510Speter** the RBU extension only.  All other VFS should return SQLITE_NOTFOUND for
1056366076Scy** this opcode.
1057342292Scy**
1058342292Scy** <li>[[SQLITE_FCNTL_BEGIN_ATOMIC_WRITE]]
1059342292Scy** If the [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] opcode returns SQLITE_OK, then
1060342292Scy** the file descriptor is placed in "batch write mode", which
1061342292Scy** means all subsequent write operations will be deferred and done
1062342292Scy** atomically at the next [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE].  Systems
1063342292Scy** that do not support batch atomic writes will return SQLITE_NOTFOUND.
1064342292Scy** ^Following a successful SQLITE_FCNTL_BEGIN_ATOMIC_WRITE and prior to
1065342292Scy** the closing [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] or
1066342292Scy** [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE], SQLite will make
1067342292Scy** no VFS interface calls on the same [sqlite3_file] file descriptor
1068342292Scy** except for calls to the xWrite method and the xFileControl method
1069342292Scy** with [SQLITE_FCNTL_SIZE_HINT].
1070342292Scy**
1071342292Scy** <li>[[SQLITE_FCNTL_COMMIT_ATOMIC_WRITE]]
1072342292Scy** The [SQLITE_FCNTL_COMMIT_ATOMIC_WRITE] opcode causes all write
1073366076Scy** operations since the previous successful call to
1074342292Scy** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be performed atomically.
1075342292Scy** This file control returns [SQLITE_OK] if and only if the writes were
1076342292Scy** all performed successfully and have been committed to persistent storage.
1077342292Scy** ^Regardless of whether or not it is successful, this file control takes
1078342292Scy** the file descriptor out of batch write mode so that all subsequent
1079342292Scy** write operations are independent.
1080342292Scy** ^SQLite will never invoke SQLITE_FCNTL_COMMIT_ATOMIC_WRITE without
1081342292Scy** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1082342292Scy**
1083342292Scy** <li>[[SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE]]
1084342292Scy** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
1085366076Scy** operations since the previous successful call to
1086342292Scy** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
1087342292Scy** ^This file control takes the file descriptor out of batch write mode
1088342292Scy** so that all subsequent write operations are independent.
1089342292Scy** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
1090342292Scy** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
1091342292Scy**
1092342292Scy** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
1093362190Scy** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode is used to configure a VFS
1094366076Scy** to block for up to M milliseconds before failing when attempting to
1095366076Scy** obtain a file lock using the xLock or xShmLock methods of the VFS.
1096362190Scy** The parameter is a pointer to a 32-bit signed integer that contains
1097362190Scy** the value that M is to be set to. Before returning, the 32-bit signed
1098362190Scy** integer is overwritten with the previous value of M.
1099342292Scy**
1100342292Scy** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1101342292Scy** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1102342292Scy** a database file.  The argument is a pointer to a 32-bit unsigned integer.
1103342292Scy** The "data version" for the pager is written into the pointer.  The
1104342292Scy** "data version" changes whenever any change occurs to the corresponding
1105342292Scy** database file, either through SQL statements on the same database
1106342292Scy** connection or through transactions committed by separate database
1107342292Scy** connections possibly in other processes. The [sqlite3_total_changes()]
1108342292Scy** interface can be used to find if any database on the connection has changed,
1109342292Scy** but that interface responds to changes on TEMP as well as MAIN and does
1110342292Scy** not provide a mechanism to detect changes to MAIN only.  Also, the
1111342292Scy** [sqlite3_total_changes()] interface responds to internal changes only and
1112342292Scy** omits changes made by other database connections.  The
1113361456Scy** [PRAGMA data_version] command provides a mechanism to detect changes to
1114342292Scy** a single attached database that occur due to other database connections,
1115342292Scy** but omits changes implemented by the database connection on which it is
1116342292Scy** called.  This file control is the only mechanism to detect changes that
1117342292Scy** happen either internally or externally and that are associated with
1118342292Scy** a particular attached database.
1119361456Scy**
1120362190Scy** <li>[[SQLITE_FCNTL_CKPT_START]]
1121362190Scy** The [SQLITE_FCNTL_CKPT_START] opcode is invoked from within a checkpoint
1122362190Scy** in wal mode before the client starts to copy pages from the wal
1123362190Scy** file to the database file.
1124362190Scy**
1125361456Scy** <li>[[SQLITE_FCNTL_CKPT_DONE]]
1126361456Scy** The [SQLITE_FCNTL_CKPT_DONE] opcode is invoked from within a checkpoint
1127361456Scy** in wal mode after the client has finished copying pages from the wal
1128361456Scy** file to the database file, but before the *-shm file is updated to
1129361456Scy** record the fact that the pages have been checkpointed.
1130251883Speter** </ul>
1131251883Speter*/
1132251883Speter#define SQLITE_FCNTL_LOCKSTATE               1
1133282328Sbapt#define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
1134282328Sbapt#define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
1135282328Sbapt#define SQLITE_FCNTL_LAST_ERRNO              4
1136251883Speter#define SQLITE_FCNTL_SIZE_HINT               5
1137251883Speter#define SQLITE_FCNTL_CHUNK_SIZE              6
1138251883Speter#define SQLITE_FCNTL_FILE_POINTER            7
1139251883Speter#define SQLITE_FCNTL_SYNC_OMITTED            8
1140251883Speter#define SQLITE_FCNTL_WIN32_AV_RETRY          9
1141251883Speter#define SQLITE_FCNTL_PERSIST_WAL            10
1142251883Speter#define SQLITE_FCNTL_OVERWRITE              11
1143251883Speter#define SQLITE_FCNTL_VFSNAME                12
1144251883Speter#define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
1145251883Speter#define SQLITE_FCNTL_PRAGMA                 14
1146251883Speter#define SQLITE_FCNTL_BUSYHANDLER            15
1147251883Speter#define SQLITE_FCNTL_TEMPFILENAME           16
1148251883Speter#define SQLITE_FCNTL_MMAP_SIZE              18
1149269851Speter#define SQLITE_FCNTL_TRACE                  19
1150269851Speter#define SQLITE_FCNTL_HAS_MOVED              20
1151269851Speter#define SQLITE_FCNTL_SYNC                   21
1152269851Speter#define SQLITE_FCNTL_COMMIT_PHASETWO        22
1153269851Speter#define SQLITE_FCNTL_WIN32_SET_HANDLE       23
1154282328Sbapt#define SQLITE_FCNTL_WAL_BLOCK              24
1155286510Speter#define SQLITE_FCNTL_ZIPVFS                 25
1156286510Speter#define SQLITE_FCNTL_RBU                    26
1157298161Sbapt#define SQLITE_FCNTL_VFS_POINTER            27
1158298161Sbapt#define SQLITE_FCNTL_JOURNAL_POINTER        28
1159322444Speter#define SQLITE_FCNTL_WIN32_GET_HANDLE       29
1160322444Speter#define SQLITE_FCNTL_PDB                    30
1161342292Scy#define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
1162342292Scy#define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
1163342292Scy#define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
1164342292Scy#define SQLITE_FCNTL_LOCK_TIMEOUT           34
1165342292Scy#define SQLITE_FCNTL_DATA_VERSION           35
1166346442Scy#define SQLITE_FCNTL_SIZE_LIMIT             36
1167361456Scy#define SQLITE_FCNTL_CKPT_DONE              37
1168362190Scy#define SQLITE_FCNTL_RESERVE_BYTES          38
1169362190Scy#define SQLITE_FCNTL_CKPT_START             39
1170251883Speter
1171282328Sbapt/* deprecated names */
1172282328Sbapt#define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
1173282328Sbapt#define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
1174282328Sbapt#define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
1175282328Sbapt
1176282328Sbapt
1177251883Speter/*
1178251883Speter** CAPI3REF: Mutex Handle
1179251883Speter**
1180251883Speter** The mutex module within SQLite defines [sqlite3_mutex] to be an
1181251883Speter** abstract type for a mutex object.  The SQLite core never looks
1182251883Speter** at the internal representation of an [sqlite3_mutex].  It only
1183251883Speter** deals with pointers to the [sqlite3_mutex] object.
1184251883Speter**
1185251883Speter** Mutexes are created using [sqlite3_mutex_alloc()].
1186251883Speter*/
1187251883Spetertypedef struct sqlite3_mutex sqlite3_mutex;
1188251883Speter
1189251883Speter/*
1190305002Scy** CAPI3REF: Loadable Extension Thunk
1191305002Scy**
1192305002Scy** A pointer to the opaque sqlite3_api_routines structure is passed as
1193305002Scy** the third parameter to entry points of [loadable extensions].  This
1194305002Scy** structure must be typedefed in order to work around compiler warnings
1195305002Scy** on some platforms.
1196305002Scy*/
1197305002Scytypedef struct sqlite3_api_routines sqlite3_api_routines;
1198305002Scy
1199305002Scy/*
1200251883Speter** CAPI3REF: OS Interface Object
1201251883Speter**
1202251883Speter** An instance of the sqlite3_vfs object defines the interface between
1203251883Speter** the SQLite core and the underlying operating system.  The "vfs"
1204251883Speter** in the name of the object stands for "virtual file system".  See
1205251883Speter** the [VFS | VFS documentation] for further information.
1206251883Speter**
1207342292Scy** The VFS interface is sometimes extended by adding new methods onto
1208342292Scy** the end.  Each time such an extension occurs, the iVersion field
1209342292Scy** is incremented.  The iVersion value started out as 1 in
1210342292Scy** SQLite [version 3.5.0] on [dateof:3.5.0], then increased to 2
1211342292Scy** with SQLite [version 3.7.0] on [dateof:3.7.0], and then increased
1212342292Scy** to 3 with SQLite [version 3.7.6] on [dateof:3.7.6].  Additional fields
1213342292Scy** may be appended to the sqlite3_vfs object and the iVersion value
1214342292Scy** may increase again in future versions of SQLite.
1215361456Scy** Note that due to an oversight, the structure
1216361456Scy** of the sqlite3_vfs object changed in the transition from
1217342292Scy** SQLite [version 3.5.9] to [version 3.6.0] on [dateof:3.6.0]
1218361456Scy** and yet the iVersion field was not increased.
1219251883Speter**
1220251883Speter** The szOsFile field is the size of the subclassed [sqlite3_file]
1221251883Speter** structure used by this VFS.  mxPathname is the maximum length of
1222251883Speter** a pathname in this VFS.
1223251883Speter**
1224251883Speter** Registered sqlite3_vfs objects are kept on a linked list formed by
1225251883Speter** the pNext pointer.  The [sqlite3_vfs_register()]
1226251883Speter** and [sqlite3_vfs_unregister()] interfaces manage this list
1227251883Speter** in a thread-safe way.  The [sqlite3_vfs_find()] interface
1228251883Speter** searches the list.  Neither the application code nor the VFS
1229251883Speter** implementation should use the pNext pointer.
1230251883Speter**
1231251883Speter** The pNext field is the only field in the sqlite3_vfs
1232251883Speter** structure that SQLite will ever modify.  SQLite will only access
1233251883Speter** or modify this field while holding a particular static mutex.
1234251883Speter** The application should never modify anything within the sqlite3_vfs
1235251883Speter** object once the object has been registered.
1236251883Speter**
1237251883Speter** The zName field holds the name of the VFS module.  The name must
1238251883Speter** be unique across all VFS modules.
1239251883Speter**
1240251883Speter** [[sqlite3_vfs.xOpen]]
1241251883Speter** ^SQLite guarantees that the zFilename parameter to xOpen
1242251883Speter** is either a NULL pointer or string obtained
1243251883Speter** from xFullPathname() with an optional suffix added.
1244251883Speter** ^If a suffix is added to the zFilename parameter, it will
1245251883Speter** consist of a single "-" character followed by no more than
1246251883Speter** 11 alphanumeric and/or "-" characters.
1247251883Speter** ^SQLite further guarantees that
1248251883Speter** the string will be valid and unchanged until xClose() is
1249251883Speter** called. Because of the previous sentence,
1250251883Speter** the [sqlite3_file] can safely store a pointer to the
1251251883Speter** filename if it needs to remember the filename for some reason.
1252251883Speter** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1253366076Scy** must invent its own temporary name for the file.  ^Whenever the
1254251883Speter** xFilename parameter is NULL it will also be the case that the
1255251883Speter** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1256251883Speter**
1257251883Speter** The flags argument to xOpen() includes all bits set in
1258251883Speter** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
1259251883Speter** or [sqlite3_open16()] is used, then flags includes at least
1260366076Scy** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
1261251883Speter** If xOpen() opens a file read-only then it sets *pOutFlags to
1262251883Speter** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
1263251883Speter**
1264251883Speter** ^(SQLite will also add one of the following flags to the xOpen()
1265251883Speter** call, depending on the object being opened:
1266251883Speter**
1267251883Speter** <ul>
1268251883Speter** <li>  [SQLITE_OPEN_MAIN_DB]
1269251883Speter** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
1270251883Speter** <li>  [SQLITE_OPEN_TEMP_DB]
1271251883Speter** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
1272251883Speter** <li>  [SQLITE_OPEN_TRANSIENT_DB]
1273251883Speter** <li>  [SQLITE_OPEN_SUBJOURNAL]
1274366076Scy** <li>  [SQLITE_OPEN_SUPER_JOURNAL]
1275251883Speter** <li>  [SQLITE_OPEN_WAL]
1276251883Speter** </ul>)^
1277251883Speter**
1278251883Speter** The file I/O implementation can use the object type flags to
1279251883Speter** change the way it deals with files.  For example, an application
1280251883Speter** that does not care about crash recovery or rollback might make
1281251883Speter** the open of a journal file a no-op.  Writes to this journal would
1282251883Speter** also be no-ops, and any attempt to read the journal would return
1283251883Speter** SQLITE_IOERR.  Or the implementation might recognize that a database
1284251883Speter** file will be doing page-aligned sector reads and writes in a random
1285251883Speter** order and set up its I/O subsystem accordingly.
1286251883Speter**
1287251883Speter** SQLite might also add one of the following flags to the xOpen method:
1288251883Speter**
1289251883Speter** <ul>
1290251883Speter** <li> [SQLITE_OPEN_DELETEONCLOSE]
1291251883Speter** <li> [SQLITE_OPEN_EXCLUSIVE]
1292251883Speter** </ul>
1293251883Speter**
1294251883Speter** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1295251883Speter** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
1296251883Speter** will be set for TEMP databases and their journals, transient
1297251883Speter** databases, and subjournals.
1298251883Speter**
1299251883Speter** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1300251883Speter** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1301251883Speter** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1302366076Scy** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
1303251883Speter** SQLITE_OPEN_CREATE, is used to indicate that file should always
1304251883Speter** be created, and that it is an error if it already exists.
1305366076Scy** It is <i>not</i> used to indicate the file should be opened
1306251883Speter** for exclusive access.
1307251883Speter**
1308251883Speter** ^At least szOsFile bytes of memory are allocated by SQLite
1309361456Scy** to hold the [sqlite3_file] structure passed as the third
1310251883Speter** argument to xOpen.  The xOpen method does not have to
1311251883Speter** allocate the structure; it should just fill it in.  Note that
1312251883Speter** the xOpen method must set the sqlite3_file.pMethods to either
1313251883Speter** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
1314251883Speter** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
1315251883Speter** element will be valid after xOpen returns regardless of the success
1316251883Speter** or failure of the xOpen call.
1317251883Speter**
1318251883Speter** [[sqlite3_vfs.xAccess]]
1319251883Speter** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1320251883Speter** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1321251883Speter** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1322351633Scy** to test whether a file is at least readable.  The SQLITE_ACCESS_READ
1323351633Scy** flag is never actually used and is not implemented in the built-in
1324351633Scy** VFSes of SQLite.  The file is named by the second argument and can be a
1325351633Scy** directory. The xAccess method returns [SQLITE_OK] on success or some
1326351633Scy** non-zero error code if there is an I/O error or if the name of
1327351633Scy** the file given in the second argument is illegal.  If SQLITE_OK
1328351633Scy** is returned, then non-zero or zero is written into *pResOut to indicate
1329366076Scy** whether or not the file is accessible.
1330251883Speter**
1331251883Speter** ^SQLite will always allocate at least mxPathname+1 bytes for the
1332251883Speter** output buffer xFullPathname.  The exact size of the output buffer
1333251883Speter** is also passed as a parameter to both  methods. If the output buffer
1334251883Speter** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1335251883Speter** handled as a fatal error by SQLite, vfs implementations should endeavor
1336251883Speter** to prevent this by setting mxPathname to a sufficiently large value.
1337251883Speter**
1338251883Speter** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1339251883Speter** interfaces are not strictly a part of the filesystem, but they are
1340251883Speter** included in the VFS structure for completeness.
1341251883Speter** The xRandomness() function attempts to return nBytes bytes
1342251883Speter** of good-quality randomness into zOut.  The return value is
1343251883Speter** the actual number of bytes of randomness obtained.
1344251883Speter** The xSleep() method causes the calling thread to sleep for at
1345251883Speter** least the number of microseconds given.  ^The xCurrentTime()
1346251883Speter** method returns a Julian Day Number for the current date and time as
1347251883Speter** a floating point value.
1348251883Speter** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1349366076Scy** Day Number multiplied by 86400000 (the number of milliseconds in
1350366076Scy** a 24-hour day).
1351251883Speter** ^SQLite will use the xCurrentTimeInt64() method to get the current
1352366076Scy** date and time if that method is available (if iVersion is 2 or
1353251883Speter** greater and the function pointer is not NULL) and will fall back
1354251883Speter** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1355251883Speter**
1356251883Speter** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1357251883Speter** are not used by the SQLite core.  These optional interfaces are provided
1358366076Scy** by some VFSes to facilitate testing of the VFS code. By overriding
1359251883Speter** system calls with functions under its control, a test program can
1360251883Speter** simulate faults and error conditions that would otherwise be difficult
1361251883Speter** or impossible to induce.  The set of system calls that can be overridden
1362251883Speter** varies from one VFS to another, and from one version of the same VFS to the
1363251883Speter** next.  Applications that use these interfaces must be prepared for any
1364251883Speter** or all of these interfaces to be NULL or for their behavior to change
1365251883Speter** from one release to the next.  Applications must not attempt to access
1366251883Speter** any of these methods if the iVersion of the VFS is less than 3.
1367251883Speter*/
1368251883Spetertypedef struct sqlite3_vfs sqlite3_vfs;
1369251883Spetertypedef void (*sqlite3_syscall_ptr)(void);
1370251883Speterstruct sqlite3_vfs {
1371251883Speter  int iVersion;            /* Structure version number (currently 3) */
1372251883Speter  int szOsFile;            /* Size of subclassed sqlite3_file */
1373251883Speter  int mxPathname;          /* Maximum file pathname length */
1374251883Speter  sqlite3_vfs *pNext;      /* Next registered VFS */
1375251883Speter  const char *zName;       /* Name of this virtual file system */
1376251883Speter  void *pAppData;          /* Pointer to application-specific data */
1377251883Speter  int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1378251883Speter               int flags, int *pOutFlags);
1379251883Speter  int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1380251883Speter  int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1381251883Speter  int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1382251883Speter  void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1383251883Speter  void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1384251883Speter  void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1385251883Speter  void (*xDlClose)(sqlite3_vfs*, void*);
1386251883Speter  int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1387251883Speter  int (*xSleep)(sqlite3_vfs*, int microseconds);
1388251883Speter  int (*xCurrentTime)(sqlite3_vfs*, double*);
1389251883Speter  int (*xGetLastError)(sqlite3_vfs*, int, char *);
1390251883Speter  /*
1391251883Speter  ** The methods above are in version 1 of the sqlite_vfs object
1392251883Speter  ** definition.  Those that follow are added in version 2 or later
1393251883Speter  */
1394251883Speter  int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1395251883Speter  /*
1396251883Speter  ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1397251883Speter  ** Those below are for version 3 and greater.
1398251883Speter  */
1399251883Speter  int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1400251883Speter  sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1401251883Speter  const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1402251883Speter  /*
1403251883Speter  ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1404298161Sbapt  ** New fields may be appended in future versions.  The iVersion
1405366076Scy  ** value will increment whenever this happens.
1406251883Speter  */
1407251883Speter};
1408251883Speter
1409251883Speter/*
1410251883Speter** CAPI3REF: Flags for the xAccess VFS method
1411251883Speter**
1412251883Speter** These integer constants can be used as the third parameter to
1413251883Speter** the xAccess method of an [sqlite3_vfs] object.  They determine
1414251883Speter** what kind of permissions the xAccess method is looking for.
1415251883Speter** With SQLITE_ACCESS_EXISTS, the xAccess method
1416251883Speter** simply checks whether the file exists.
1417251883Speter** With SQLITE_ACCESS_READWRITE, the xAccess method
1418251883Speter** checks whether the named directory is both readable and writable
1419251883Speter** (in other words, if files can be added, removed, and renamed within
1420251883Speter** the directory).
1421251883Speter** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1422251883Speter** [temp_store_directory pragma], though this could change in a future
1423251883Speter** release of SQLite.
1424251883Speter** With SQLITE_ACCESS_READ, the xAccess method
1425251883Speter** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
1426251883Speter** currently unused, though it might be used in a future release of
1427251883Speter** SQLite.
1428251883Speter*/
1429251883Speter#define SQLITE_ACCESS_EXISTS    0
1430251883Speter#define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
1431251883Speter#define SQLITE_ACCESS_READ      2   /* Unused */
1432251883Speter
1433251883Speter/*
1434251883Speter** CAPI3REF: Flags for the xShmLock VFS method
1435251883Speter**
1436251883Speter** These integer constants define the various locking operations
1437251883Speter** allowed by the xShmLock method of [sqlite3_io_methods].  The
1438251883Speter** following are the only legal combinations of flags to the
1439251883Speter** xShmLock method:
1440251883Speter**
1441251883Speter** <ul>
1442251883Speter** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1443251883Speter** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1444251883Speter** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1445251883Speter** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1446251883Speter** </ul>
1447251883Speter**
1448251883Speter** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1449366076Scy** was given on the corresponding lock.
1450251883Speter**
1451251883Speter** The xShmLock method can transition between unlocked and SHARED or
1452251883Speter** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
1453251883Speter** and EXCLUSIVE.
1454251883Speter*/
1455251883Speter#define SQLITE_SHM_UNLOCK       1
1456251883Speter#define SQLITE_SHM_LOCK         2
1457251883Speter#define SQLITE_SHM_SHARED       4
1458251883Speter#define SQLITE_SHM_EXCLUSIVE    8
1459251883Speter
1460251883Speter/*
1461251883Speter** CAPI3REF: Maximum xShmLock index
1462251883Speter**
1463251883Speter** The xShmLock method on [sqlite3_io_methods] may use values
1464251883Speter** between 0 and this upper bound as its "offset" argument.
1465251883Speter** The SQLite core will never attempt to acquire or release a
1466251883Speter** lock outside of this range
1467251883Speter*/
1468251883Speter#define SQLITE_SHM_NLOCK        8
1469251883Speter
1470251883Speter
1471251883Speter/*
1472251883Speter** CAPI3REF: Initialize The SQLite Library
1473251883Speter**
1474251883Speter** ^The sqlite3_initialize() routine initializes the
1475251883Speter** SQLite library.  ^The sqlite3_shutdown() routine
1476251883Speter** deallocates any resources that were allocated by sqlite3_initialize().
1477251883Speter** These routines are designed to aid in process initialization and
1478251883Speter** shutdown on embedded systems.  Workstation applications using
1479251883Speter** SQLite normally do not need to invoke either of these routines.
1480251883Speter**
1481251883Speter** A call to sqlite3_initialize() is an "effective" call if it is
1482251883Speter** the first time sqlite3_initialize() is invoked during the lifetime of
1483251883Speter** the process, or if it is the first time sqlite3_initialize() is invoked
1484251883Speter** following a call to sqlite3_shutdown().  ^(Only an effective call
1485251883Speter** of sqlite3_initialize() does any initialization.  All other calls
1486251883Speter** are harmless no-ops.)^
1487251883Speter**
1488251883Speter** A call to sqlite3_shutdown() is an "effective" call if it is the first
1489251883Speter** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
1490251883Speter** an effective call to sqlite3_shutdown() does any deinitialization.
1491251883Speter** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1492251883Speter**
1493251883Speter** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1494251883Speter** is not.  The sqlite3_shutdown() interface must only be called from a
1495251883Speter** single thread.  All open [database connections] must be closed and all
1496251883Speter** other SQLite resources must be deallocated prior to invoking
1497251883Speter** sqlite3_shutdown().
1498251883Speter**
1499251883Speter** Among other things, ^sqlite3_initialize() will invoke
1500251883Speter** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
1501251883Speter** will invoke sqlite3_os_end().
1502251883Speter**
1503251883Speter** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1504251883Speter** ^If for some reason, sqlite3_initialize() is unable to initialize
1505251883Speter** the library (perhaps it is unable to allocate a needed resource such
1506251883Speter** as a mutex) it returns an [error code] other than [SQLITE_OK].
1507251883Speter**
1508251883Speter** ^The sqlite3_initialize() routine is called internally by many other
1509251883Speter** SQLite interfaces so that an application usually does not need to
1510251883Speter** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
1511251883Speter** calls sqlite3_initialize() so the SQLite library will be automatically
1512251883Speter** initialized when [sqlite3_open()] is called if it has not be initialized
1513251883Speter** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1514251883Speter** compile-time option, then the automatic calls to sqlite3_initialize()
1515251883Speter** are omitted and the application must call sqlite3_initialize() directly
1516251883Speter** prior to using any other SQLite interface.  For maximum portability,
1517251883Speter** it is recommended that applications always invoke sqlite3_initialize()
1518251883Speter** directly prior to using any other SQLite interface.  Future releases
1519251883Speter** of SQLite may require this.  In other words, the behavior exhibited
1520251883Speter** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1521251883Speter** default behavior in some future release of SQLite.
1522251883Speter**
1523251883Speter** The sqlite3_os_init() routine does operating-system specific
1524251883Speter** initialization of the SQLite library.  The sqlite3_os_end()
1525251883Speter** routine undoes the effect of sqlite3_os_init().  Typical tasks
1526251883Speter** performed by these routines include allocation or deallocation
1527251883Speter** of static resources, initialization of global variables,
1528251883Speter** setting up a default [sqlite3_vfs] module, or setting up
1529251883Speter** a default configuration using [sqlite3_config()].
1530251883Speter**
1531251883Speter** The application should never invoke either sqlite3_os_init()
1532251883Speter** or sqlite3_os_end() directly.  The application should only invoke
1533251883Speter** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
1534251883Speter** interface is called automatically by sqlite3_initialize() and
1535251883Speter** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
1536251883Speter** implementations for sqlite3_os_init() and sqlite3_os_end()
1537251883Speter** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1538251883Speter** When [custom builds | built for other platforms]
1539251883Speter** (using the [SQLITE_OS_OTHER=1] compile-time
1540251883Speter** option) the application must supply a suitable implementation for
1541251883Speter** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
1542251883Speter** implementation of sqlite3_os_init() or sqlite3_os_end()
1543251883Speter** must return [SQLITE_OK] on success and some other [error code] upon
1544251883Speter** failure.
1545251883Speter*/
1546322444SpeterSQLITE_API int sqlite3_initialize(void);
1547322444SpeterSQLITE_API int sqlite3_shutdown(void);
1548322444SpeterSQLITE_API int sqlite3_os_init(void);
1549322444SpeterSQLITE_API int sqlite3_os_end(void);
1550251883Speter
1551251883Speter/*
1552251883Speter** CAPI3REF: Configuring The SQLite Library
1553251883Speter**
1554251883Speter** The sqlite3_config() interface is used to make global configuration
1555251883Speter** changes to SQLite in order to tune SQLite to the specific needs of
1556251883Speter** the application.  The default configuration is recommended for most
1557251883Speter** applications and so this routine is usually not necessary.  It is
1558251883Speter** provided to support rare applications with unusual needs.
1559251883Speter**
1560298161Sbapt** <b>The sqlite3_config() interface is not threadsafe. The application
1561298161Sbapt** must ensure that no other SQLite interfaces are invoked by other
1562298161Sbapt** threads while sqlite3_config() is running.</b>
1563298161Sbapt**
1564298161Sbapt** The sqlite3_config() interface
1565251883Speter** may only be invoked prior to library initialization using
1566251883Speter** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
1567251883Speter** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
1568251883Speter** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
1569251883Speter** Note, however, that ^sqlite3_config() can be called as part of the
1570251883Speter** implementation of an application-defined [sqlite3_os_init()].
1571251883Speter**
1572251883Speter** The first argument to sqlite3_config() is an integer
1573251883Speter** [configuration option] that determines
1574251883Speter** what property of SQLite is to be configured.  Subsequent arguments
1575251883Speter** vary depending on the [configuration option]
1576251883Speter** in the first argument.
1577251883Speter**
1578251883Speter** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
1579251883Speter** ^If the option is unknown or SQLite is unable to set the option
1580251883Speter** then this routine returns a non-zero [error code].
1581251883Speter*/
1582322444SpeterSQLITE_API int sqlite3_config(int, ...);
1583251883Speter
1584251883Speter/*
1585251883Speter** CAPI3REF: Configure database connections
1586286510Speter** METHOD: sqlite3
1587251883Speter**
1588251883Speter** The sqlite3_db_config() interface is used to make configuration
1589251883Speter** changes to a [database connection].  The interface is similar to
1590251883Speter** [sqlite3_config()] except that the changes apply to a single
1591251883Speter** [database connection] (specified in the first argument).
1592251883Speter**
1593251883Speter** The second argument to sqlite3_db_config(D,V,...)  is the
1594366076Scy** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
1595251883Speter** that indicates what aspect of the [database connection] is being configured.
1596251883Speter** Subsequent arguments vary depending on the configuration verb.
1597251883Speter**
1598251883Speter** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
1599251883Speter** the call is considered successful.
1600251883Speter*/
1601322444SpeterSQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
1602251883Speter
1603251883Speter/*
1604251883Speter** CAPI3REF: Memory Allocation Routines
1605251883Speter**
1606251883Speter** An instance of this object defines the interface between SQLite
1607251883Speter** and low-level memory allocation routines.
1608251883Speter**
1609251883Speter** This object is used in only one place in the SQLite interface.
1610251883Speter** A pointer to an instance of this object is the argument to
1611251883Speter** [sqlite3_config()] when the configuration option is
1612366076Scy** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
1613251883Speter** By creating an instance of this object
1614251883Speter** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
1615251883Speter** during configuration, an application can specify an alternative
1616251883Speter** memory allocation subsystem for SQLite to use for all of its
1617251883Speter** dynamic memory needs.
1618251883Speter**
1619251883Speter** Note that SQLite comes with several [built-in memory allocators]
1620251883Speter** that are perfectly adequate for the overwhelming majority of applications
1621251883Speter** and that this object is only useful to a tiny minority of applications
1622251883Speter** with specialized memory allocation requirements.  This object is
1623251883Speter** also used during testing of SQLite in order to specify an alternative
1624251883Speter** memory allocator that simulates memory out-of-memory conditions in
1625251883Speter** order to verify that SQLite recovers gracefully from such
1626251883Speter** conditions.
1627251883Speter**
1628251883Speter** The xMalloc, xRealloc, and xFree methods must work like the
1629251883Speter** malloc(), realloc() and free() functions from the standard C library.
1630251883Speter** ^SQLite guarantees that the second argument to
1631251883Speter** xRealloc is always a value returned by a prior call to xRoundup.
1632251883Speter**
1633251883Speter** xSize should return the allocated size of a memory allocation
1634251883Speter** previously obtained from xMalloc or xRealloc.  The allocated size
1635251883Speter** is always at least as big as the requested size but may be larger.
1636251883Speter**
1637251883Speter** The xRoundup method returns what would be the allocated size of
1638251883Speter** a memory allocation given a particular requested size.  Most memory
1639251883Speter** allocators round up memory allocations at least to the next multiple
1640251883Speter** of 8.  Some allocators round up to a larger multiple or to a power of 2.
1641251883Speter** Every memory allocation request coming in through [sqlite3_malloc()]
1642366076Scy** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0,
1643251883Speter** that causes the corresponding memory allocation to fail.
1644251883Speter**
1645269851Speter** The xInit method initializes the memory allocator.  For example,
1646361456Scy** it might allocate any required mutexes or initialize internal data
1647251883Speter** structures.  The xShutdown method is invoked (indirectly) by
1648251883Speter** [sqlite3_shutdown()] and should deallocate any resources acquired
1649251883Speter** by xInit.  The pAppData pointer is used as the only parameter to
1650251883Speter** xInit and xShutdown.
1651251883Speter**
1652366076Scy** SQLite holds the [SQLITE_MUTEX_STATIC_MAIN] mutex when it invokes
1653251883Speter** the xInit method, so the xInit method need not be threadsafe.  The
1654251883Speter** xShutdown method is only called from [sqlite3_shutdown()] so it does
1655251883Speter** not need to be threadsafe either.  For all other methods, SQLite
1656251883Speter** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
1657251883Speter** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
1658251883Speter** it is by default) and so the methods are automatically serialized.
1659251883Speter** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
1660251883Speter** methods must be threadsafe or else make their own arrangements for
1661251883Speter** serialization.
1662251883Speter**
1663251883Speter** SQLite will never invoke xInit() more than once without an intervening
1664251883Speter** call to xShutdown().
1665251883Speter*/
1666251883Spetertypedef struct sqlite3_mem_methods sqlite3_mem_methods;
1667251883Speterstruct sqlite3_mem_methods {
1668251883Speter  void *(*xMalloc)(int);         /* Memory allocation function */
1669251883Speter  void (*xFree)(void*);          /* Free a prior allocation */
1670251883Speter  void *(*xRealloc)(void*,int);  /* Resize an allocation */
1671251883Speter  int (*xSize)(void*);           /* Return the size of an allocation */
1672251883Speter  int (*xRoundup)(int);          /* Round up request size to allocation size */
1673251883Speter  int (*xInit)(void*);           /* Initialize the memory allocator */
1674251883Speter  void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
1675251883Speter  void *pAppData;                /* Argument to xInit() and xShutdown() */
1676251883Speter};
1677251883Speter
1678251883Speter/*
1679251883Speter** CAPI3REF: Configuration Options
1680251883Speter** KEYWORDS: {configuration option}
1681251883Speter**
1682251883Speter** These constants are the available integer configuration options that
1683251883Speter** can be passed as the first argument to the [sqlite3_config()] interface.
1684251883Speter**
1685251883Speter** New configuration options may be added in future releases of SQLite.
1686251883Speter** Existing configuration options might be discontinued.  Applications
1687251883Speter** should check the return code from [sqlite3_config()] to make sure that
1688251883Speter** the call worked.  The [sqlite3_config()] interface will return a
1689251883Speter** non-zero [error code] if a discontinued or unsupported configuration option
1690251883Speter** is invoked.
1691251883Speter**
1692251883Speter** <dl>
1693251883Speter** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
1694251883Speter** <dd>There are no arguments to this option.  ^This option sets the
1695251883Speter** [threading mode] to Single-thread.  In other words, it disables
1696251883Speter** all mutexing and puts SQLite into a mode where it can only be used
1697251883Speter** by a single thread.   ^If SQLite is compiled with
1698251883Speter** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1699251883Speter** it is not possible to change the [threading mode] from its default
1700366076Scy** value of Single-thread and so [sqlite3_config()] will return
1701251883Speter** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
1702251883Speter** configuration option.</dd>
1703251883Speter**
1704251883Speter** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
1705251883Speter** <dd>There are no arguments to this option.  ^This option sets the
1706251883Speter** [threading mode] to Multi-thread.  In other words, it disables
1707251883Speter** mutexing on [database connection] and [prepared statement] objects.
1708251883Speter** The application is responsible for serializing access to
1709251883Speter** [database connections] and [prepared statements].  But other mutexes
1710251883Speter** are enabled so that SQLite will be safe to use in a multi-threaded
1711251883Speter** environment as long as no two threads attempt to use the same
1712251883Speter** [database connection] at the same time.  ^If SQLite is compiled with
1713251883Speter** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1714251883Speter** it is not possible to set the Multi-thread [threading mode] and
1715251883Speter** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1716251883Speter** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
1717251883Speter**
1718251883Speter** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
1719251883Speter** <dd>There are no arguments to this option.  ^This option sets the
1720251883Speter** [threading mode] to Serialized. In other words, this option enables
1721251883Speter** all mutexes including the recursive
1722251883Speter** mutexes on [database connection] and [prepared statement] objects.
1723251883Speter** In this mode (which is the default when SQLite is compiled with
1724251883Speter** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
1725251883Speter** to [database connections] and [prepared statements] so that the
1726251883Speter** application is free to use the same [database connection] or the
1727251883Speter** same [prepared statement] in different threads at the same time.
1728251883Speter** ^If SQLite is compiled with
1729251883Speter** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1730251883Speter** it is not possible to set the Serialized [threading mode] and
1731251883Speter** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1732251883Speter** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
1733251883Speter**
1734251883Speter** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1735366076Scy** <dd> ^(The SQLITE_CONFIG_MALLOC option takes a single argument which is
1736282328Sbapt** a pointer to an instance of the [sqlite3_mem_methods] structure.
1737282328Sbapt** The argument specifies
1738251883Speter** alternative low-level memory allocation routines to be used in place of
1739251883Speter** the memory allocation routines built into SQLite.)^ ^SQLite makes
1740251883Speter** its own private copy of the content of the [sqlite3_mem_methods] structure
1741251883Speter** before the [sqlite3_config()] call returns.</dd>
1742251883Speter**
1743251883Speter** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1744282328Sbapt** <dd> ^(The SQLITE_CONFIG_GETMALLOC option takes a single argument which
1745282328Sbapt** is a pointer to an instance of the [sqlite3_mem_methods] structure.
1746282328Sbapt** The [sqlite3_mem_methods]
1747251883Speter** structure is filled with the currently defined memory allocation routines.)^
1748251883Speter** This option can be used to overload the default memory allocation
1749251883Speter** routines with a wrapper that simulations memory allocation failure or
1750251883Speter** tracks memory usage, for example. </dd>
1751251883Speter**
1752342292Scy** [[SQLITE_CONFIG_SMALL_MALLOC]] <dt>SQLITE_CONFIG_SMALL_MALLOC</dt>
1753342292Scy** <dd> ^The SQLITE_CONFIG_SMALL_MALLOC option takes single argument of
1754342292Scy** type int, interpreted as a boolean, which if true provides a hint to
1755342292Scy** SQLite that it should avoid large memory allocations if possible.
1756342292Scy** SQLite will run faster if it is free to make large memory allocations,
1757342292Scy** but some application might prefer to run slower in exchange for
1758342292Scy** guarantees about memory fragmentation that are possible if large
1759342292Scy** allocations are avoided.  This hint is normally off.
1760342292Scy** </dd>
1761342292Scy**
1762251883Speter** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1763282328Sbapt** <dd> ^The SQLITE_CONFIG_MEMSTATUS option takes single argument of type int,
1764282328Sbapt** interpreted as a boolean, which enables or disables the collection of
1765282328Sbapt** memory allocation statistics. ^(When memory allocation statistics are
1766282328Sbapt** disabled, the following SQLite interfaces become non-operational:
1767251883Speter**   <ul>
1768361456Scy**   <li> [sqlite3_hard_heap_limit64()]
1769251883Speter**   <li> [sqlite3_memory_used()]
1770251883Speter**   <li> [sqlite3_memory_highwater()]
1771251883Speter**   <li> [sqlite3_soft_heap_limit64()]
1772282328Sbapt**   <li> [sqlite3_status64()]
1773251883Speter**   </ul>)^
1774251883Speter** ^Memory allocation statistics are enabled by default unless SQLite is
1775251883Speter** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1776251883Speter** allocation statistics are disabled by default.
1777251883Speter** </dd>
1778251883Speter**
1779251883Speter** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1780342292Scy** <dd> The SQLITE_CONFIG_SCRATCH option is no longer used.
1781282328Sbapt** </dd>
1782251883Speter**
1783251883Speter** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1784298161Sbapt** <dd> ^The SQLITE_CONFIG_PAGECACHE option specifies a memory pool
1785282328Sbapt** that SQLite can use for the database page cache with the default page
1786366076Scy** cache implementation.
1787361456Scy** This configuration option is a no-op if an application-defined page
1788298161Sbapt** cache implementation is loaded using the [SQLITE_CONFIG_PCACHE2].
1789282328Sbapt** ^There are three arguments to SQLITE_CONFIG_PAGECACHE: A pointer to
1790298161Sbapt** 8-byte aligned memory (pMem), the size of each page cache line (sz),
1791298161Sbapt** and the number of cache lines (N).
1792251883Speter** The sz argument should be the size of the largest database page
1793282328Sbapt** (a power of two between 512 and 65536) plus some extra bytes for each
1794282328Sbapt** page header.  ^The number of extra bytes needed by the page header
1795298161Sbapt** can be determined using [SQLITE_CONFIG_PCACHE_HDRSZ].
1796282328Sbapt** ^It is harmless, apart from the wasted memory,
1797298161Sbapt** for the sz parameter to be larger than necessary.  The pMem
1798298161Sbapt** argument must be either a NULL pointer or a pointer to an 8-byte
1799298161Sbapt** aligned block of memory of at least sz*N bytes, otherwise
1800298161Sbapt** subsequent behavior is undefined.
1801298161Sbapt** ^When pMem is not NULL, SQLite will strive to use the memory provided
1802298161Sbapt** to satisfy page cache needs, falling back to [sqlite3_malloc()] if
1803298161Sbapt** a page cache line is larger than sz bytes or if all of the pMem buffer
1804298161Sbapt** is exhausted.
1805298161Sbapt** ^If pMem is NULL and N is non-zero, then each database connection
1806298161Sbapt** does an initial bulk allocation for page cache memory
1807298161Sbapt** from [sqlite3_malloc()] sufficient for N cache lines if N is positive or
1808298161Sbapt** of -1024*N bytes if N is negative, . ^If additional
1809298161Sbapt** page cache memory is needed beyond what is provided by the initial
1810298161Sbapt** allocation, then SQLite goes to [sqlite3_malloc()] separately for each
1811298161Sbapt** additional cache line. </dd>
1812251883Speter**
1813251883Speter** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1814366076Scy** <dd> ^The SQLITE_CONFIG_HEAP option specifies a static memory buffer
1815282328Sbapt** that SQLite will use for all of its dynamic memory allocation needs
1816342292Scy** beyond those provided for by [SQLITE_CONFIG_PAGECACHE].
1817282328Sbapt** ^The SQLITE_CONFIG_HEAP option is only available if SQLite is compiled
1818282328Sbapt** with either [SQLITE_ENABLE_MEMSYS3] or [SQLITE_ENABLE_MEMSYS5] and returns
1819282328Sbapt** [SQLITE_ERROR] if invoked otherwise.
1820282328Sbapt** ^There are three arguments to SQLITE_CONFIG_HEAP:
1821282328Sbapt** An 8-byte aligned pointer to the memory,
1822251883Speter** the number of bytes in the memory buffer, and the minimum allocation size.
1823251883Speter** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
1824251883Speter** to using its default memory allocator (the system malloc() implementation),
1825251883Speter** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
1826282328Sbapt** memory pointer is not NULL then the alternative memory
1827251883Speter** allocator is engaged to handle all of SQLites memory allocation needs.
1828251883Speter** The first pointer (the memory pointer) must be aligned to an 8-byte
1829251883Speter** boundary or subsequent behavior of SQLite will be undefined.
1830251883Speter** The minimum allocation size is capped at 2**12. Reasonable values
1831251883Speter** for the minimum allocation size are 2**5 through 2**8.</dd>
1832251883Speter**
1833251883Speter** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1834282328Sbapt** <dd> ^(The SQLITE_CONFIG_MUTEX option takes a single argument which is a
1835282328Sbapt** pointer to an instance of the [sqlite3_mutex_methods] structure.
1836282328Sbapt** The argument specifies alternative low-level mutex routines to be used
1837282328Sbapt** in place the mutex routines built into SQLite.)^  ^SQLite makes a copy of
1838282328Sbapt** the content of the [sqlite3_mutex_methods] structure before the call to
1839251883Speter** [sqlite3_config()] returns. ^If SQLite is compiled with
1840251883Speter** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1841251883Speter** the entire mutexing subsystem is omitted from the build and hence calls to
1842251883Speter** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
1843251883Speter** return [SQLITE_ERROR].</dd>
1844251883Speter**
1845251883Speter** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1846282328Sbapt** <dd> ^(The SQLITE_CONFIG_GETMUTEX option takes a single argument which
1847282328Sbapt** is a pointer to an instance of the [sqlite3_mutex_methods] structure.  The
1848251883Speter** [sqlite3_mutex_methods]
1849251883Speter** structure is filled with the currently defined mutex routines.)^
1850251883Speter** This option can be used to overload the default mutex allocation
1851251883Speter** routines with a wrapper used to track mutex usage for performance
1852251883Speter** profiling or testing, for example.   ^If SQLite is compiled with
1853251883Speter** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1854251883Speter** the entire mutexing subsystem is omitted from the build and hence calls to
1855251883Speter** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
1856251883Speter** return [SQLITE_ERROR].</dd>
1857251883Speter**
1858251883Speter** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1859282328Sbapt** <dd> ^(The SQLITE_CONFIG_LOOKASIDE option takes two arguments that determine
1860282328Sbapt** the default size of lookaside memory on each [database connection].
1861282328Sbapt** The first argument is the
1862251883Speter** size of each lookaside buffer slot and the second is the number of
1863282328Sbapt** slots allocated to each database connection.)^  ^(SQLITE_CONFIG_LOOKASIDE
1864282328Sbapt** sets the <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1865282328Sbapt** option to [sqlite3_db_config()] can be used to change the lookaside
1866251883Speter** configuration on individual connections.)^ </dd>
1867251883Speter**
1868251883Speter** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
1869366076Scy** <dd> ^(The SQLITE_CONFIG_PCACHE2 option takes a single argument which is
1870282328Sbapt** a pointer to an [sqlite3_pcache_methods2] object.  This object specifies
1871282328Sbapt** the interface to a custom page cache implementation.)^
1872282328Sbapt** ^SQLite makes a copy of the [sqlite3_pcache_methods2] object.</dd>
1873251883Speter**
1874251883Speter** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
1875282328Sbapt** <dd> ^(The SQLITE_CONFIG_GETPCACHE2 option takes a single argument which
1876282328Sbapt** is a pointer to an [sqlite3_pcache_methods2] object.  SQLite copies of
1877282328Sbapt** the current page cache implementation into that object.)^ </dd>
1878251883Speter**
1879251883Speter** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1880251883Speter** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
1881251883Speter** global [error log].
1882251883Speter** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
1883366076Scy** function with a call signature of void(*)(void*,int,const char*),
1884251883Speter** and a pointer to void. ^If the function pointer is not NULL, it is
1885251883Speter** invoked by [sqlite3_log()] to process each logging event.  ^If the
1886251883Speter** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
1887251883Speter** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
1888251883Speter** passed through as the first parameter to the application-defined logger
1889251883Speter** function whenever that function is invoked.  ^The second parameter to
1890251883Speter** the logger function is a copy of the first parameter to the corresponding
1891251883Speter** [sqlite3_log()] call and is intended to be a [result code] or an
1892251883Speter** [extended result code].  ^The third parameter passed to the logger is
1893251883Speter** log message after formatting via [sqlite3_snprintf()].
1894251883Speter** The SQLite logging interface is not reentrant; the logger function
1895251883Speter** supplied by the application must not invoke any SQLite interface.
1896251883Speter** In a multi-threaded application, the application-defined logger
1897251883Speter** function must be threadsafe. </dd>
1898251883Speter**
1899251883Speter** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
1900282328Sbapt** <dd>^(The SQLITE_CONFIG_URI option takes a single argument of type int.
1901282328Sbapt** If non-zero, then URI handling is globally enabled. If the parameter is zero,
1902282328Sbapt** then URI handling is globally disabled.)^ ^If URI handling is globally
1903282328Sbapt** enabled, all filenames passed to [sqlite3_open()], [sqlite3_open_v2()],
1904282328Sbapt** [sqlite3_open16()] or
1905251883Speter** specified as part of [ATTACH] commands are interpreted as URIs, regardless
1906251883Speter** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
1907269851Speter** connection is opened. ^If it is globally disabled, filenames are
1908251883Speter** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
1909269851Speter** database connection is opened. ^(By default, URI handling is globally
1910251883Speter** disabled. The default value may be changed by compiling with the
1911269851Speter** [SQLITE_USE_URI] symbol defined.)^
1912251883Speter**
1913251883Speter** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
1914282328Sbapt** <dd>^The SQLITE_CONFIG_COVERING_INDEX_SCAN option takes a single integer
1915282328Sbapt** argument which is interpreted as a boolean in order to enable or disable
1916282328Sbapt** the use of covering indices for full table scans in the query optimizer.
1917282328Sbapt** ^The default setting is determined
1918251883Speter** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
1919251883Speter** if that compile-time option is omitted.
1920251883Speter** The ability to disable the use of covering indices for full table scans
1921251883Speter** is because some incorrectly coded legacy applications might malfunction
1922269851Speter** when the optimization is enabled.  Providing the ability to
1923251883Speter** disable the optimization allows the older, buggy application code to work
1924251883Speter** without change even with newer versions of SQLite.
1925251883Speter**
1926251883Speter** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
1927251883Speter** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
1928251883Speter** <dd> These options are obsolete and should not be used by new code.
1929251883Speter** They are retained for backwards compatibility but are now no-ops.
1930251883Speter** </dd>
1931251883Speter**
1932251883Speter** [[SQLITE_CONFIG_SQLLOG]]
1933251883Speter** <dt>SQLITE_CONFIG_SQLLOG
1934251883Speter** <dd>This option is only available if sqlite is compiled with the
1935251883Speter** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
1936251883Speter** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
1937251883Speter** The second should be of type (void*). The callback is invoked by the library
1938251883Speter** in three separate circumstances, identified by the value passed as the
1939251883Speter** fourth parameter. If the fourth parameter is 0, then the database connection
1940251883Speter** passed as the second argument has just been opened. The third argument
1941251883Speter** points to a buffer containing the name of the main database file. If the
1942251883Speter** fourth parameter is 1, then the SQL statement that the third parameter
1943251883Speter** points to has just been executed. Or, if the fourth parameter is 2, then
1944251883Speter** the connection being passed as the second parameter is being closed. The
1945251883Speter** third parameter is passed NULL In this case.  An example of using this
1946251883Speter** configuration option can be seen in the "test_sqllog.c" source file in
1947251883Speter** the canonical SQLite source tree.</dd>
1948251883Speter**
1949251883Speter** [[SQLITE_CONFIG_MMAP_SIZE]]
1950251883Speter** <dt>SQLITE_CONFIG_MMAP_SIZE
1951269851Speter** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
1952251883Speter** that are the default mmap size limit (the default setting for
1953251883Speter** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
1954269851Speter** ^The default setting can be overridden by each database connection using
1955251883Speter** either the [PRAGMA mmap_size] command, or by using the
1956269851Speter** [SQLITE_FCNTL_MMAP_SIZE] file control.  ^(The maximum allowed mmap size
1957282328Sbapt** will be silently truncated if necessary so that it does not exceed the
1958282328Sbapt** compile-time maximum mmap size set by the
1959269851Speter** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
1960269851Speter** ^If either argument to this option is negative, then that argument is
1961251883Speter** changed to its compile-time default.
1962269851Speter**
1963269851Speter** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
1964269851Speter** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
1965282328Sbapt** <dd>^The SQLITE_CONFIG_WIN32_HEAPSIZE option is only available if SQLite is
1966282328Sbapt** compiled for Windows with the [SQLITE_WIN32_MALLOC] pre-processor macro
1967282328Sbapt** defined. ^SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
1968269851Speter** that specifies the maximum size of the created heap.
1969282328Sbapt**
1970282328Sbapt** [[SQLITE_CONFIG_PCACHE_HDRSZ]]
1971282328Sbapt** <dt>SQLITE_CONFIG_PCACHE_HDRSZ
1972282328Sbapt** <dd>^The SQLITE_CONFIG_PCACHE_HDRSZ option takes a single parameter which
1973282328Sbapt** is a pointer to an integer and writes into that integer the number of extra
1974282328Sbapt** bytes per page required for each page in [SQLITE_CONFIG_PAGECACHE].
1975282328Sbapt** The amount of extra space required can change depending on the compiler,
1976282328Sbapt** target platform, and SQLite version.
1977282328Sbapt**
1978282328Sbapt** [[SQLITE_CONFIG_PMASZ]]
1979282328Sbapt** <dt>SQLITE_CONFIG_PMASZ
1980282328Sbapt** <dd>^The SQLITE_CONFIG_PMASZ option takes a single parameter which
1981282328Sbapt** is an unsigned integer and sets the "Minimum PMA Size" for the multithreaded
1982282328Sbapt** sorter to that integer.  The default minimum PMA Size is set by the
1983282328Sbapt** [SQLITE_SORTER_PMASZ] compile-time option.  New threads are launched
1984282328Sbapt** to help with sort operations when multithreaded sorting
1985282328Sbapt** is enabled (using the [PRAGMA threads] command) and the amount of content
1986282328Sbapt** to be sorted exceeds the page size times the minimum of the
1987282328Sbapt** [PRAGMA cache_size] setting and this value.
1988298161Sbapt**
1989298161Sbapt** [[SQLITE_CONFIG_STMTJRNL_SPILL]]
1990298161Sbapt** <dt>SQLITE_CONFIG_STMTJRNL_SPILL
1991298161Sbapt** <dd>^The SQLITE_CONFIG_STMTJRNL_SPILL option takes a single parameter which
1992366076Scy** becomes the [statement journal] spill-to-disk threshold.
1993298161Sbapt** [Statement journals] are held in memory until their size (in bytes)
1994298161Sbapt** exceeds this threshold, at which point they are written to disk.
1995298161Sbapt** Or if the threshold is -1, statement journals are always held
1996298161Sbapt** exclusively in memory.
1997298161Sbapt** Since many statement journals never become large, setting the spill
1998298161Sbapt** threshold to a value such as 64KiB can greatly reduce the amount of
1999298161Sbapt** I/O required to support statement rollback.
2000298161Sbapt** The default value for this setting is controlled by the
2001298161Sbapt** [SQLITE_STMTJRNL_SPILL] compile-time option.
2002342292Scy**
2003342292Scy** [[SQLITE_CONFIG_SORTERREF_SIZE]]
2004342292Scy** <dt>SQLITE_CONFIG_SORTERREF_SIZE
2005342292Scy** <dd>The SQLITE_CONFIG_SORTERREF_SIZE option accepts a single parameter
2006342292Scy** of type (int) - the new value of the sorter-reference size threshold.
2007342292Scy** Usually, when SQLite uses an external sort to order records according
2008342292Scy** to an ORDER BY clause, all fields required by the caller are present in the
2009342292Scy** sorted records. However, if SQLite determines based on the declared type
2010342292Scy** of a table column that its values are likely to be very large - larger
2011342292Scy** than the configured sorter-reference size threshold - then a reference
2012342292Scy** is stored in each sorted record and the required column values loaded
2013342292Scy** from the database as records are returned in sorted order. The default
2014366076Scy** value for this option is to never use this optimization. Specifying a
2015342292Scy** negative value for this option restores the default behaviour.
2016342292Scy** This option is only available if SQLite is compiled with the
2017342292Scy** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2018346442Scy**
2019346442Scy** [[SQLITE_CONFIG_MEMDB_MAXSIZE]]
2020346442Scy** <dt>SQLITE_CONFIG_MEMDB_MAXSIZE
2021346442Scy** <dd>The SQLITE_CONFIG_MEMDB_MAXSIZE option accepts a single parameter
2022346442Scy** [sqlite3_int64] parameter which is the default maximum size for an in-memory
2023346442Scy** database created using [sqlite3_deserialize()].  This default maximum
2024346442Scy** size can be adjusted up or down for individual databases using the
2025346442Scy** [SQLITE_FCNTL_SIZE_LIMIT] [sqlite3_file_control|file-control].  If this
2026346442Scy** configuration setting is never used, then the default maximum is determined
2027346442Scy** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option.  If that
2028346442Scy** compile-time option is not set, then the default maximum is 1073741824.
2029251883Speter** </dl>
2030251883Speter*/
2031251883Speter#define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
2032251883Speter#define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
2033251883Speter#define SQLITE_CONFIG_SERIALIZED    3  /* nil */
2034251883Speter#define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
2035251883Speter#define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
2036342292Scy#define SQLITE_CONFIG_SCRATCH       6  /* No longer used */
2037251883Speter#define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
2038251883Speter#define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
2039251883Speter#define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
2040251883Speter#define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
2041251883Speter#define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
2042366076Scy/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
2043251883Speter#define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
2044251883Speter#define SQLITE_CONFIG_PCACHE       14  /* no-op */
2045251883Speter#define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
2046251883Speter#define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
2047251883Speter#define SQLITE_CONFIG_URI          17  /* int */
2048251883Speter#define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
2049251883Speter#define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
2050251883Speter#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
2051251883Speter#define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
2052251883Speter#define SQLITE_CONFIG_MMAP_SIZE    22  /* sqlite3_int64, sqlite3_int64 */
2053269851Speter#define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
2054282328Sbapt#define SQLITE_CONFIG_PCACHE_HDRSZ        24  /* int *psz */
2055282328Sbapt#define SQLITE_CONFIG_PMASZ               25  /* unsigned int szPma */
2056298161Sbapt#define SQLITE_CONFIG_STMTJRNL_SPILL      26  /* int nByte */
2057342292Scy#define SQLITE_CONFIG_SMALL_MALLOC        27  /* boolean */
2058342292Scy#define SQLITE_CONFIG_SORTERREF_SIZE      28  /* int nByte */
2059346442Scy#define SQLITE_CONFIG_MEMDB_MAXSIZE       29  /* sqlite3_int64 */
2060251883Speter
2061251883Speter/*
2062251883Speter** CAPI3REF: Database Connection Configuration Options
2063251883Speter**
2064251883Speter** These constants are the available integer configuration options that
2065251883Speter** can be passed as the second argument to the [sqlite3_db_config()] interface.
2066251883Speter**
2067251883Speter** New configuration options may be added in future releases of SQLite.
2068251883Speter** Existing configuration options might be discontinued.  Applications
2069251883Speter** should check the return code from [sqlite3_db_config()] to make sure that
2070251883Speter** the call worked.  ^The [sqlite3_db_config()] interface will return a
2071251883Speter** non-zero [error code] if a discontinued or unsupported configuration option
2072251883Speter** is invoked.
2073251883Speter**
2074251883Speter** <dl>
2075342292Scy** [[SQLITE_DBCONFIG_LOOKASIDE]]
2076251883Speter** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
2077366076Scy** <dd> ^This option takes three additional arguments that determine the
2078251883Speter** [lookaside memory allocator] configuration for the [database connection].
2079251883Speter** ^The first argument (the third parameter to [sqlite3_db_config()] is a
2080251883Speter** pointer to a memory buffer to use for lookaside memory.
2081251883Speter** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
2082251883Speter** may be NULL in which case SQLite will allocate the
2083251883Speter** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
2084251883Speter** size of each lookaside buffer slot.  ^The third argument is the number of
2085251883Speter** slots.  The size of the buffer in the first argument must be greater than
2086251883Speter** or equal to the product of the second and third arguments.  The buffer
2087251883Speter** must be aligned to an 8-byte boundary.  ^If the second argument to
2088251883Speter** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
2089251883Speter** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
2090251883Speter** configuration for a database connection can only be changed when that
2091251883Speter** connection is not currently using lookaside memory, or in other words
2092251883Speter** when the "current value" returned by
2093251883Speter** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
2094251883Speter** Any attempt to change the lookaside memory configuration when lookaside
2095366076Scy** memory is in use leaves the configuration unchanged and returns
2096251883Speter** [SQLITE_BUSY].)^</dd>
2097251883Speter**
2098342292Scy** [[SQLITE_DBCONFIG_ENABLE_FKEY]]
2099251883Speter** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
2100251883Speter** <dd> ^This option is used to enable or disable the enforcement of
2101251883Speter** [foreign key constraints].  There should be two additional arguments.
2102251883Speter** The first argument is an integer which is 0 to disable FK enforcement,
2103251883Speter** positive to enable FK enforcement or negative to leave FK enforcement
2104251883Speter** unchanged.  The second parameter is a pointer to an integer into which
2105251883Speter** is written 0 or 1 to indicate whether FK enforcement is off or on
2106251883Speter** following this call.  The second parameter may be a NULL pointer, in
2107251883Speter** which case the FK enforcement setting is not reported back. </dd>
2108251883Speter**
2109342292Scy** [[SQLITE_DBCONFIG_ENABLE_TRIGGER]]
2110251883Speter** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
2111251883Speter** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
2112251883Speter** There should be two additional arguments.
2113251883Speter** The first argument is an integer which is 0 to disable triggers,
2114251883Speter** positive to enable triggers or negative to leave the setting unchanged.
2115251883Speter** The second parameter is a pointer to an integer into which
2116251883Speter** is written 0 or 1 to indicate whether triggers are disabled or enabled
2117251883Speter** following this call.  The second parameter may be a NULL pointer, in
2118369951Scy** which case the trigger setting is not reported back.
2119251883Speter**
2120369951Scy** <p>Originally this option disabled all triggers.  ^(However, since
2121369951Scy** SQLite version 3.35.0, TEMP triggers are still allowed even if
2122369951Scy** this option is off.  So, in other words, this option now only disables
2123369951Scy** triggers in the main database schema or in the schemas of ATTACH-ed
2124369951Scy** databases.)^ </dd>
2125369951Scy**
2126355326Scy** [[SQLITE_DBCONFIG_ENABLE_VIEW]]
2127355326Scy** <dt>SQLITE_DBCONFIG_ENABLE_VIEW</dt>
2128355326Scy** <dd> ^This option is used to enable or disable [CREATE VIEW | views].
2129355326Scy** There should be two additional arguments.
2130355326Scy** The first argument is an integer which is 0 to disable views,
2131355326Scy** positive to enable views or negative to leave the setting unchanged.
2132355326Scy** The second parameter is a pointer to an integer into which
2133355326Scy** is written 0 or 1 to indicate whether views are disabled or enabled
2134355326Scy** following this call.  The second parameter may be a NULL pointer, in
2135369951Scy** which case the view setting is not reported back.
2136355326Scy**
2137369951Scy** <p>Originally this option disabled all views.  ^(However, since
2138369951Scy** SQLite version 3.35.0, TEMP views are still allowed even if
2139369951Scy** this option is off.  So, in other words, this option now only disables
2140369951Scy** views in the main database schema or in the schemas of ATTACH-ed
2141369951Scy** databases.)^ </dd>
2142369951Scy**
2143342292Scy** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
2144298161Sbapt** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
2145347347Scy** <dd> ^This option is used to enable or disable the
2146347347Scy** [fts3_tokenizer()] function which is part of the
2147298161Sbapt** [FTS3] full-text search engine extension.
2148298161Sbapt** There should be two additional arguments.
2149298161Sbapt** The first argument is an integer which is 0 to disable fts3_tokenizer() or
2150298161Sbapt** positive to enable fts3_tokenizer() or negative to leave the setting
2151298161Sbapt** unchanged.
2152298161Sbapt** The second parameter is a pointer to an integer into which
2153298161Sbapt** is written 0 or 1 to indicate whether fts3_tokenizer is disabled or enabled
2154298161Sbapt** following this call.  The second parameter may be a NULL pointer, in
2155298161Sbapt** which case the new setting is not reported back. </dd>
2156298161Sbapt**
2157342292Scy** [[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION]]
2158305002Scy** <dt>SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION</dt>
2159305002Scy** <dd> ^This option is used to enable or disable the [sqlite3_load_extension()]
2160305002Scy** interface independently of the [load_extension()] SQL function.
2161305002Scy** The [sqlite3_enable_load_extension()] API enables or disables both the
2162305002Scy** C-API [sqlite3_load_extension()] and the SQL function [load_extension()].
2163305002Scy** There should be two additional arguments.
2164305002Scy** When the first argument to this interface is 1, then only the C-API is
2165305002Scy** enabled and the SQL function remains disabled.  If the first argument to
2166305002Scy** this interface is 0, then both the C-API and the SQL function are disabled.
2167305002Scy** If the first argument is -1, then no changes are made to state of either the
2168305002Scy** C-API or the SQL function.
2169305002Scy** The second parameter is a pointer to an integer into which
2170305002Scy** is written 0 or 1 to indicate whether [sqlite3_load_extension()] interface
2171305002Scy** is disabled or enabled following this call.  The second parameter may
2172305002Scy** be a NULL pointer, in which case the new setting is not reported back.
2173305002Scy** </dd>
2174305002Scy**
2175342292Scy** [[SQLITE_DBCONFIG_MAINDBNAME]] <dt>SQLITE_DBCONFIG_MAINDBNAME</dt>
2176322444Speter** <dd> ^This option is used to change the name of the "main" database
2177322444Speter** schema.  ^The sole argument is a pointer to a constant UTF8 string
2178322444Speter** which will become the new schema name in place of "main".  ^SQLite
2179322444Speter** does not make a copy of the new main schema name string, so the application
2180322444Speter** must ensure that the argument passed into this DBCONFIG option is unchanged
2181322444Speter** until after the database connection closes.
2182322444Speter** </dd>
2183322444Speter**
2184366076Scy** [[SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE]]
2185322444Speter** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
2186366076Scy** <dd> Usually, when a database in wal mode is closed or detached from a
2187366076Scy** database handle, SQLite checks if this will mean that there are now no
2188366076Scy** connections at all to the database. If so, it performs a checkpoint
2189322444Speter** operation before closing the connection. This option may be used to
2190322444Speter** override this behaviour. The first parameter passed to this operation
2191342292Scy** is an integer - positive to disable checkpoints-on-close, or zero (the
2192342292Scy** default) to enable them, and negative to leave the setting unchanged.
2193342292Scy** The second parameter is a pointer to an integer
2194322444Speter** into which is written 0 or 1 to indicate whether checkpoints-on-close
2195322444Speter** have been disabled - 0 if they are not disabled, 1 if they are.
2196322444Speter** </dd>
2197322444Speter**
2198342292Scy** [[SQLITE_DBCONFIG_ENABLE_QPSG]] <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
2199322444Speter** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
2200322444Speter** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
2201322444Speter** a single SQL query statement will always use the same algorithm regardless
2202322444Speter** of values of [bound parameters].)^ The QPSG disables some query optimizations
2203322444Speter** that look at the values of bound parameters, which can make some queries
2204322444Speter** slower.  But the QPSG has the advantage of more predictable behavior.  With
2205322444Speter** the QPSG active, SQLite will always use the same query plan in the field as
2206322444Speter** was used during testing in the lab.
2207366076Scy** The first argument to this setting is an integer which is 0 to disable
2208342292Scy** the QPSG, positive to enable QPSG, or negative to leave the setting
2209342292Scy** unchanged. The second parameter is a pointer to an integer into which
2210342292Scy** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
2211342292Scy** following this call.
2212322444Speter** </dd>
2213322444Speter**
2214342292Scy** [[SQLITE_DBCONFIG_TRIGGER_EQP]] <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
2215366076Scy** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not
2216342292Scy** include output for any operations performed by trigger programs. This
2217342292Scy** option is used to set or clear (the default) a flag that governs this
2218342292Scy** behavior. The first parameter passed to this operation is an integer -
2219342292Scy** positive to enable output for trigger programs, or zero to disable it,
2220342292Scy** or negative to leave the setting unchanged.
2221366076Scy** The second parameter is a pointer to an integer into which is written
2222366076Scy** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if
2223366076Scy** it is not disabled, 1 if it is.
2224342292Scy** </dd>
2225342292Scy**
2226342292Scy** [[SQLITE_DBCONFIG_RESET_DATABASE]] <dt>SQLITE_DBCONFIG_RESET_DATABASE</dt>
2227342292Scy** <dd> Set the SQLITE_DBCONFIG_RESET_DATABASE flag and then run
2228342292Scy** [VACUUM] in order to reset a database back to an empty database
2229342292Scy** with no schema and no content. The following process works even for
2230342292Scy** a badly corrupted database file:
2231342292Scy** <ol>
2232342292Scy** <li> If the database connection is newly opened, make sure it has read the
2233342292Scy**      database schema by preparing then discarding some query against the
2234342292Scy**      database, or calling sqlite3_table_column_metadata(), ignoring any
2235342292Scy**      errors.  This step is only necessary if the application desires to keep
2236342292Scy**      the database in WAL mode after the reset if it was in WAL mode before
2237366076Scy**      the reset.
2238342292Scy** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
2239342292Scy** <li> [sqlite3_exec](db, "[VACUUM]", 0, 0, 0);
2240342292Scy** <li> sqlite3_db_config(db, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
2241342292Scy** </ol>
2242342292Scy** Because resetting a database is destructive and irreversible, the
2243342292Scy** process requires the use of this obscure API and multiple steps to help
2244342292Scy** ensure that it does not happen by accident.
2245342292Scy**
2246342292Scy** [[SQLITE_DBCONFIG_DEFENSIVE]] <dt>SQLITE_DBCONFIG_DEFENSIVE</dt>
2247342292Scy** <dd>The SQLITE_DBCONFIG_DEFENSIVE option activates or deactivates the
2248342292Scy** "defensive" flag for a database connection.  When the defensive
2249366076Scy** flag is enabled, language features that allow ordinary SQL to
2250342292Scy** deliberately corrupt the database file are disabled.  The disabled
2251342292Scy** features include but are not limited to the following:
2252342292Scy** <ul>
2253342292Scy** <li> The [PRAGMA writable_schema=ON] statement.
2254351633Scy** <li> The [PRAGMA journal_mode=OFF] statement.
2255342292Scy** <li> Writes to the [sqlite_dbpage] virtual table.
2256342292Scy** <li> Direct writes to [shadow tables].
2257342292Scy** </ul>
2258342292Scy** </dd>
2259347347Scy**
2260347347Scy** [[SQLITE_DBCONFIG_WRITABLE_SCHEMA]] <dt>SQLITE_DBCONFIG_WRITABLE_SCHEMA</dt>
2261347347Scy** <dd>The SQLITE_DBCONFIG_WRITABLE_SCHEMA option activates or deactivates the
2262347347Scy** "writable_schema" flag. This has the same effect and is logically equivalent
2263347347Scy** to setting [PRAGMA writable_schema=ON] or [PRAGMA writable_schema=OFF].
2264366076Scy** The first argument to this setting is an integer which is 0 to disable
2265347347Scy** the writable_schema, positive to enable writable_schema, or negative to
2266347347Scy** leave the setting unchanged. The second parameter is a pointer to an
2267347347Scy** integer into which is written 0 or 1 to indicate whether the writable_schema
2268347347Scy** is enabled or disabled following this call.
2269347347Scy** </dd>
2270351633Scy**
2271351633Scy** [[SQLITE_DBCONFIG_LEGACY_ALTER_TABLE]]
2272351633Scy** <dt>SQLITE_DBCONFIG_LEGACY_ALTER_TABLE</dt>
2273351633Scy** <dd>The SQLITE_DBCONFIG_LEGACY_ALTER_TABLE option activates or deactivates
2274351633Scy** the legacy behavior of the [ALTER TABLE RENAME] command such it
2275351633Scy** behaves as it did prior to [version 3.24.0] (2018-06-04).  See the
2276351633Scy** "Compatibility Notice" on the [ALTER TABLE RENAME documentation] for
2277351633Scy** additional information. This feature can also be turned on and off
2278351633Scy** using the [PRAGMA legacy_alter_table] statement.
2279351633Scy** </dd>
2280351633Scy**
2281351633Scy** [[SQLITE_DBCONFIG_DQS_DML]]
2282351633Scy** <dt>SQLITE_DBCONFIG_DQS_DML</td>
2283351633Scy** <dd>The SQLITE_DBCONFIG_DQS_DML option activates or deactivates
2284361456Scy** the legacy [double-quoted string literal] misfeature for DML statements
2285351633Scy** only, that is DELETE, INSERT, SELECT, and UPDATE statements. The
2286351633Scy** default value of this setting is determined by the [-DSQLITE_DQS]
2287351633Scy** compile-time option.
2288351633Scy** </dd>
2289351633Scy**
2290351633Scy** [[SQLITE_DBCONFIG_DQS_DDL]]
2291351633Scy** <dt>SQLITE_DBCONFIG_DQS_DDL</td>
2292351633Scy** <dd>The SQLITE_DBCONFIG_DQS option activates or deactivates
2293351633Scy** the legacy [double-quoted string literal] misfeature for DDL statements,
2294351633Scy** such as CREATE TABLE and CREATE INDEX. The
2295351633Scy** default value of this setting is determined by the [-DSQLITE_DQS]
2296351633Scy** compile-time option.
2297351633Scy** </dd>
2298361456Scy**
2299361456Scy** [[SQLITE_DBCONFIG_TRUSTED_SCHEMA]]
2300361456Scy** <dt>SQLITE_DBCONFIG_TRUSTED_SCHEMA</td>
2301361456Scy** <dd>The SQLITE_DBCONFIG_TRUSTED_SCHEMA option tells SQLite to
2302366076Scy** assume that database schemas are untainted by malicious content.
2303361456Scy** When the SQLITE_DBCONFIG_TRUSTED_SCHEMA option is disabled, SQLite
2304361456Scy** takes additional defensive steps to protect the application from harm
2305361456Scy** including:
2306361456Scy** <ul>
2307361456Scy** <li> Prohibit the use of SQL functions inside triggers, views,
2308366076Scy** CHECK constraints, DEFAULT clauses, expression indexes,
2309361456Scy** partial indexes, or generated columns
2310361456Scy** unless those functions are tagged with [SQLITE_INNOCUOUS].
2311361456Scy** <li> Prohibit the use of virtual tables inside of triggers or views
2312361456Scy** unless those virtual tables are tagged with [SQLITE_VTAB_INNOCUOUS].
2313361456Scy** </ul>
2314361456Scy** This setting defaults to "on" for legacy compatibility, however
2315361456Scy** all applications are advised to turn it off if possible. This setting
2316361456Scy** can also be controlled using the [PRAGMA trusted_schema] statement.
2317361456Scy** </dd>
2318361456Scy**
2319361456Scy** [[SQLITE_DBCONFIG_LEGACY_FILE_FORMAT]]
2320361456Scy** <dt>SQLITE_DBCONFIG_LEGACY_FILE_FORMAT</td>
2321361456Scy** <dd>The SQLITE_DBCONFIG_LEGACY_FILE_FORMAT option activates or deactivates
2322361456Scy** the legacy file format flag.  When activated, this flag causes all newly
2323361456Scy** created database file to have a schema format version number (the 4-byte
2324361456Scy** integer found at offset 44 into the database header) of 1.  This in turn
2325361456Scy** means that the resulting database file will be readable and writable by
2326361456Scy** any SQLite version back to 3.0.0 ([dateof:3.0.0]).  Without this setting,
2327361456Scy** newly created databases are generally not understandable by SQLite versions
2328361456Scy** prior to 3.3.0 ([dateof:3.3.0]).  As these words are written, there
2329366076Scy** is now scarcely any need to generated database files that are compatible
2330361456Scy** all the way back to version 3.0.0, and so this setting is of little
2331361456Scy** practical use, but is provided so that SQLite can continue to claim the
2332361456Scy** ability to generate new database files that are compatible with  version
2333361456Scy** 3.0.0.
2334361456Scy** <p>Note that when the SQLITE_DBCONFIG_LEGACY_FILE_FORMAT setting is on,
2335361456Scy** the [VACUUM] command will fail with an obscure error when attempting to
2336361456Scy** process a table with generated columns and a descending index.  This is
2337361456Scy** not considered a bug since SQLite versions 3.3.0 and earlier do not support
2338361456Scy** either generated columns or decending indexes.
2339361456Scy** </dd>
2340251883Speter** </dl>
2341251883Speter*/
2342322444Speter#define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
2343298161Sbapt#define SQLITE_DBCONFIG_LOOKASIDE             1001 /* void* int int */
2344298161Sbapt#define SQLITE_DBCONFIG_ENABLE_FKEY           1002 /* int int* */
2345298161Sbapt#define SQLITE_DBCONFIG_ENABLE_TRIGGER        1003 /* int int* */
2346298161Sbapt#define SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1004 /* int int* */
2347305002Scy#define SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION 1005 /* int int* */
2348322444Speter#define SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      1006 /* int int* */
2349322444Speter#define SQLITE_DBCONFIG_ENABLE_QPSG           1007 /* int int* */
2350342292Scy#define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
2351342292Scy#define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
2352342292Scy#define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
2353347347Scy#define SQLITE_DBCONFIG_WRITABLE_SCHEMA       1011 /* int int* */
2354351633Scy#define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    1012 /* int int* */
2355351633Scy#define SQLITE_DBCONFIG_DQS_DML               1013 /* int int* */
2356351633Scy#define SQLITE_DBCONFIG_DQS_DDL               1014 /* int int* */
2357355326Scy#define SQLITE_DBCONFIG_ENABLE_VIEW           1015 /* int int* */
2358361456Scy#define SQLITE_DBCONFIG_LEGACY_FILE_FORMAT    1016 /* int int* */
2359361456Scy#define SQLITE_DBCONFIG_TRUSTED_SCHEMA        1017 /* int int* */
2360361456Scy#define SQLITE_DBCONFIG_MAX                   1017 /* Largest DBCONFIG */
2361251883Speter
2362251883Speter/*
2363251883Speter** CAPI3REF: Enable Or Disable Extended Result Codes
2364286510Speter** METHOD: sqlite3
2365251883Speter**
2366251883Speter** ^The sqlite3_extended_result_codes() routine enables or disables the
2367251883Speter** [extended result codes] feature of SQLite. ^The extended result
2368251883Speter** codes are disabled by default for historical compatibility.
2369251883Speter*/
2370322444SpeterSQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
2371251883Speter
2372251883Speter/*
2373251883Speter** CAPI3REF: Last Insert Rowid
2374286510Speter** METHOD: sqlite3
2375251883Speter**
2376269851Speter** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
2377269851Speter** has a unique 64-bit signed
2378251883Speter** integer key called the [ROWID | "rowid"]. ^The rowid is always available
2379251883Speter** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
2380251883Speter** names are not also used by explicitly declared columns. ^If
2381251883Speter** the table has a column of type [INTEGER PRIMARY KEY] then that column
2382251883Speter** is another alias for the rowid.
2383251883Speter**
2384322444Speter** ^The sqlite3_last_insert_rowid(D) interface usually returns the [rowid] of
2385322444Speter** the most recent successful [INSERT] into a rowid table or [virtual table]
2386322444Speter** on database connection D. ^Inserts into [WITHOUT ROWID] tables are not
2387366076Scy** recorded. ^If no successful [INSERT]s into rowid tables have ever occurred
2388366076Scy** on the database connection D, then sqlite3_last_insert_rowid(D) returns
2389322444Speter** zero.
2390251883Speter**
2391322444Speter** As well as being set automatically as rows are inserted into database
2392322444Speter** tables, the value returned by this function may be set explicitly by
2393322444Speter** [sqlite3_set_last_insert_rowid()]
2394251883Speter**
2395322444Speter** Some virtual table implementations may INSERT rows into rowid tables as
2396322444Speter** part of committing a transaction (e.g. to flush data accumulated in memory
2397322444Speter** to disk). In this case subsequent calls to this function return the rowid
2398366076Scy** associated with these internal INSERT operations, which leads to
2399322444Speter** unintuitive results. Virtual table implementations that do write to rowid
2400366076Scy** tables in this way can avoid this problem by restoring the original
2401366076Scy** rowid value using [sqlite3_set_last_insert_rowid()] before returning
2402322444Speter** control to the user.
2403322444Speter**
2404366076Scy** ^(If an [INSERT] occurs within a trigger then this routine will
2405366076Scy** return the [rowid] of the inserted row as long as the trigger is
2406366076Scy** running. Once the trigger program ends, the value returned
2407322444Speter** by this routine reverts to what it was before the trigger was fired.)^
2408322444Speter**
2409251883Speter** ^An [INSERT] that fails due to a constraint violation is not a
2410251883Speter** successful [INSERT] and does not change the value returned by this
2411251883Speter** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
2412251883Speter** and INSERT OR ABORT make no changes to the return value of this
2413251883Speter** routine when their insertion fails.  ^(When INSERT OR REPLACE
2414251883Speter** encounters a constraint violation, it does not fail.  The
2415251883Speter** INSERT continues to completion after deleting rows that caused
2416251883Speter** the constraint problem so INSERT OR REPLACE will always change
2417251883Speter** the return value of this interface.)^
2418251883Speter**
2419251883Speter** ^For the purposes of this routine, an [INSERT] is considered to
2420251883Speter** be successful even if it is subsequently rolled back.
2421251883Speter**
2422251883Speter** This function is accessible to SQL statements via the
2423251883Speter** [last_insert_rowid() SQL function].
2424251883Speter**
2425251883Speter** If a separate thread performs a new [INSERT] on the same
2426251883Speter** database connection while the [sqlite3_last_insert_rowid()]
2427251883Speter** function is running and thus changes the last insert [rowid],
2428251883Speter** then the value returned by [sqlite3_last_insert_rowid()] is
2429251883Speter** unpredictable and might not equal either the old or the new
2430251883Speter** last insert [rowid].
2431251883Speter*/
2432322444SpeterSQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
2433251883Speter
2434251883Speter/*
2435322444Speter** CAPI3REF: Set the Last Insert Rowid value.
2436322444Speter** METHOD: sqlite3
2437322444Speter**
2438322444Speter** The sqlite3_set_last_insert_rowid(D, R) method allows the application to
2439366076Scy** set the value returned by calling sqlite3_last_insert_rowid(D) to R
2440322444Speter** without inserting a row into the database.
2441322444Speter*/
2442322444SpeterSQLITE_API void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
2443322444Speter
2444322444Speter/*
2445251883Speter** CAPI3REF: Count The Number Of Rows Modified
2446286510Speter** METHOD: sqlite3
2447251883Speter**
2448282328Sbapt** ^This function returns the number of rows modified, inserted or
2449282328Sbapt** deleted by the most recently completed INSERT, UPDATE or DELETE
2450282328Sbapt** statement on the database connection specified by the only parameter.
2451282328Sbapt** ^Executing any other type of SQL statement does not modify the value
2452282328Sbapt** returned by this function.
2453251883Speter**
2454282328Sbapt** ^Only changes made directly by the INSERT, UPDATE or DELETE statement are
2455366076Scy** considered - auxiliary changes caused by [CREATE TRIGGER | triggers],
2456282328Sbapt** [foreign key actions] or [REPLACE] constraint resolution are not counted.
2457366076Scy**
2458366076Scy** Changes to a view that are intercepted by
2459366076Scy** [INSTEAD OF trigger | INSTEAD OF triggers] are not counted. ^The value
2460366076Scy** returned by sqlite3_changes() immediately after an INSERT, UPDATE or
2461366076Scy** DELETE statement run on a view is always zero. Only changes made to real
2462282328Sbapt** tables are counted.
2463251883Speter**
2464282328Sbapt** Things are more complicated if the sqlite3_changes() function is
2465282328Sbapt** executed while a trigger program is running. This may happen if the
2466282328Sbapt** program uses the [changes() SQL function], or if some other callback
2467282328Sbapt** function invokes sqlite3_changes() directly. Essentially:
2468366076Scy**
2469282328Sbapt** <ul>
2470282328Sbapt**   <li> ^(Before entering a trigger program the value returned by
2471366076Scy**        sqlite3_changes() function is saved. After the trigger program
2472282328Sbapt**        has finished, the original value is restored.)^
2473366076Scy**
2474366076Scy**   <li> ^(Within a trigger program each INSERT, UPDATE and DELETE
2475366076Scy**        statement sets the value returned by sqlite3_changes()
2476366076Scy**        upon completion as normal. Of course, this value will not include
2477366076Scy**        any changes performed by sub-triggers, as the sqlite3_changes()
2478282328Sbapt**        value will be saved and restored after each sub-trigger has run.)^
2479282328Sbapt** </ul>
2480366076Scy**
2481282328Sbapt** ^This means that if the changes() SQL function (or similar) is used
2482366076Scy** by the first INSERT, UPDATE or DELETE statement within a trigger, it
2483282328Sbapt** returns the value as set when the calling statement began executing.
2484366076Scy** ^If it is used by the second or subsequent such statement within a trigger
2485366076Scy** program, the value returned reflects the number of rows modified by the
2486282328Sbapt** previous INSERT, UPDATE or DELETE statement within the same trigger.
2487251883Speter**
2488251883Speter** If a separate thread makes changes on the same database connection
2489251883Speter** while [sqlite3_changes()] is running then the value returned
2490251883Speter** is unpredictable and not meaningful.
2491342292Scy**
2492342292Scy** See also:
2493342292Scy** <ul>
2494342292Scy** <li> the [sqlite3_total_changes()] interface
2495342292Scy** <li> the [count_changes pragma]
2496342292Scy** <li> the [changes() SQL function]
2497342292Scy** <li> the [data_version pragma]
2498342292Scy** </ul>
2499251883Speter*/
2500322444SpeterSQLITE_API int sqlite3_changes(sqlite3*);
2501251883Speter
2502251883Speter/*
2503251883Speter** CAPI3REF: Total Number Of Rows Modified
2504286510Speter** METHOD: sqlite3
2505251883Speter**
2506282328Sbapt** ^This function returns the total number of rows inserted, modified or
2507282328Sbapt** deleted by all [INSERT], [UPDATE] or [DELETE] statements completed
2508282328Sbapt** since the database connection was opened, including those executed as
2509282328Sbapt** part of trigger programs. ^Executing any other type of SQL statement
2510282328Sbapt** does not affect the value returned by sqlite3_total_changes().
2511366076Scy**
2512282328Sbapt** ^Changes made as part of [foreign key actions] are included in the
2513282328Sbapt** count, but those made as part of REPLACE constraint resolution are
2514366076Scy** not. ^Changes to a view that are intercepted by INSTEAD OF triggers
2515282328Sbapt** are not counted.
2516342292Scy**
2517346983Scy** The [sqlite3_total_changes(D)] interface only reports the number
2518342292Scy** of rows that changed due to SQL statement run against database
2519342292Scy** connection D.  Any changes by other database connections are ignored.
2520342292Scy** To detect changes against a database file from other database
2521342292Scy** connections use the [PRAGMA data_version] command or the
2522342292Scy** [SQLITE_FCNTL_DATA_VERSION] [file control].
2523366076Scy**
2524251883Speter** If a separate thread makes changes on the same database connection
2525251883Speter** while [sqlite3_total_changes()] is running then the value
2526251883Speter** returned is unpredictable and not meaningful.
2527342292Scy**
2528342292Scy** See also:
2529342292Scy** <ul>
2530342292Scy** <li> the [sqlite3_changes()] interface
2531342292Scy** <li> the [count_changes pragma]
2532342292Scy** <li> the [changes() SQL function]
2533342292Scy** <li> the [data_version pragma]
2534342292Scy** <li> the [SQLITE_FCNTL_DATA_VERSION] [file control]
2535342292Scy** </ul>
2536251883Speter*/
2537322444SpeterSQLITE_API int sqlite3_total_changes(sqlite3*);
2538251883Speter
2539251883Speter/*
2540251883Speter** CAPI3REF: Interrupt A Long-Running Query
2541286510Speter** METHOD: sqlite3
2542251883Speter**
2543251883Speter** ^This function causes any pending database operation to abort and
2544251883Speter** return at its earliest opportunity. This routine is typically
2545251883Speter** called in response to a user action such as pressing "Cancel"
2546251883Speter** or Ctrl-C where the user wants a long query operation to halt
2547251883Speter** immediately.
2548251883Speter**
2549251883Speter** ^It is safe to call this routine from a thread different from the
2550251883Speter** thread that is currently running the database operation.  But it
2551251883Speter** is not safe to call this routine with a [database connection] that
2552251883Speter** is closed or might close before sqlite3_interrupt() returns.
2553251883Speter**
2554251883Speter** ^If an SQL operation is very nearly finished at the time when
2555251883Speter** sqlite3_interrupt() is called, then it might not have an opportunity
2556251883Speter** to be interrupted and might continue to completion.
2557251883Speter**
2558251883Speter** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
2559251883Speter** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
2560251883Speter** that is inside an explicit transaction, then the entire transaction
2561251883Speter** will be rolled back automatically.
2562251883Speter**
2563251883Speter** ^The sqlite3_interrupt(D) call is in effect until all currently running
2564251883Speter** SQL statements on [database connection] D complete.  ^Any new SQL statements
2565366076Scy** that are started after the sqlite3_interrupt() call and before the
2566361456Scy** running statement count reaches zero are interrupted as if they had been
2567251883Speter** running prior to the sqlite3_interrupt() call.  ^New SQL statements
2568251883Speter** that are started after the running statement count reaches zero are
2569251883Speter** not effected by the sqlite3_interrupt().
2570251883Speter** ^A call to sqlite3_interrupt(D) that occurs when there are no running
2571251883Speter** SQL statements is a no-op and has no effect on SQL statements
2572251883Speter** that are started after the sqlite3_interrupt() call returns.
2573251883Speter*/
2574322444SpeterSQLITE_API void sqlite3_interrupt(sqlite3*);
2575251883Speter
2576251883Speter/*
2577251883Speter** CAPI3REF: Determine If An SQL Statement Is Complete
2578251883Speter**
2579251883Speter** These routines are useful during command-line input to determine if the
2580251883Speter** currently entered text seems to form a complete SQL statement or
2581251883Speter** if additional input is needed before sending the text into
2582251883Speter** SQLite for parsing.  ^These routines return 1 if the input string
2583251883Speter** appears to be a complete SQL statement.  ^A statement is judged to be
2584251883Speter** complete if it ends with a semicolon token and is not a prefix of a
2585251883Speter** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
2586251883Speter** string literals or quoted identifier names or comments are not
2587251883Speter** independent tokens (they are part of the token in which they are
2588251883Speter** embedded) and thus do not count as a statement terminator.  ^Whitespace
2589251883Speter** and comments that follow the final semicolon are ignored.
2590251883Speter**
2591251883Speter** ^These routines return 0 if the statement is incomplete.  ^If a
2592251883Speter** memory allocation fails, then SQLITE_NOMEM is returned.
2593251883Speter**
2594251883Speter** ^These routines do not parse the SQL statements thus
2595251883Speter** will not detect syntactically incorrect SQL.
2596251883Speter**
2597366076Scy** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
2598251883Speter** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
2599251883Speter** automatically by sqlite3_complete16().  If that initialization fails,
2600251883Speter** then the return value from sqlite3_complete16() will be non-zero
2601251883Speter** regardless of whether or not the input SQL is complete.)^
2602251883Speter**
2603251883Speter** The input to [sqlite3_complete()] must be a zero-terminated
2604251883Speter** UTF-8 string.
2605251883Speter**
2606251883Speter** The input to [sqlite3_complete16()] must be a zero-terminated
2607251883Speter** UTF-16 string in native byte order.
2608251883Speter*/
2609322444SpeterSQLITE_API int sqlite3_complete(const char *sql);
2610322444SpeterSQLITE_API int sqlite3_complete16(const void *sql);
2611251883Speter
2612251883Speter/*
2613251883Speter** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2614282328Sbapt** KEYWORDS: {busy-handler callback} {busy handler}
2615286510Speter** METHOD: sqlite3
2616251883Speter**
2617274884Sbapt** ^The sqlite3_busy_handler(D,X,P) routine sets a callback function X
2618274884Sbapt** that might be invoked with argument P whenever
2619274884Sbapt** an attempt is made to access a database table associated with
2620274884Sbapt** [database connection] D when another thread
2621274884Sbapt** or process has the table locked.
2622274884Sbapt** The sqlite3_busy_handler() interface is used to implement
2623274884Sbapt** [sqlite3_busy_timeout()] and [PRAGMA busy_timeout].
2624251883Speter**
2625274884Sbapt** ^If the busy callback is NULL, then [SQLITE_BUSY]
2626251883Speter** is returned immediately upon encountering the lock.  ^If the busy callback
2627251883Speter** is not NULL, then the callback might be invoked with two arguments.
2628251883Speter**
2629251883Speter** ^The first argument to the busy handler is a copy of the void* pointer which
2630251883Speter** is the third argument to sqlite3_busy_handler().  ^The second argument to
2631251883Speter** the busy handler callback is the number of times that the busy handler has
2632282328Sbapt** been invoked previously for the same locking event.  ^If the
2633251883Speter** busy callback returns 0, then no additional attempts are made to
2634274884Sbapt** access the database and [SQLITE_BUSY] is returned
2635274884Sbapt** to the application.
2636251883Speter** ^If the callback returns non-zero, then another attempt
2637274884Sbapt** is made to access the database and the cycle repeats.
2638251883Speter**
2639251883Speter** The presence of a busy handler does not guarantee that it will be invoked
2640251883Speter** when there is lock contention. ^If SQLite determines that invoking the busy
2641251883Speter** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2642366076Scy** to the application instead of invoking the
2643274884Sbapt** busy handler.
2644251883Speter** Consider a scenario where one process is holding a read lock that
2645251883Speter** it is trying to promote to a reserved lock and
2646251883Speter** a second process is holding a reserved lock that it is trying
2647251883Speter** to promote to an exclusive lock.  The first process cannot proceed
2648251883Speter** because it is blocked by the second and the second process cannot
2649251883Speter** proceed because it is blocked by the first.  If both processes
2650251883Speter** invoke the busy handlers, neither will make any progress.  Therefore,
2651251883Speter** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
2652251883Speter** will induce the first process to release its read lock and allow
2653251883Speter** the second process to proceed.
2654251883Speter**
2655251883Speter** ^The default busy callback is NULL.
2656251883Speter**
2657251883Speter** ^(There can only be a single busy handler defined for each
2658251883Speter** [database connection].  Setting a new busy handler clears any
2659251883Speter** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
2660274884Sbapt** or evaluating [PRAGMA busy_timeout=N] will change the
2661274884Sbapt** busy handler and thus clear any previously set busy handler.
2662251883Speter**
2663251883Speter** The busy callback should not take any actions which modify the
2664274884Sbapt** database connection that invoked the busy handler.  In other words,
2665274884Sbapt** the busy handler is not reentrant.  Any such actions
2666251883Speter** result in undefined behavior.
2667366076Scy**
2668251883Speter** A busy handler must not close the database connection
2669251883Speter** or [prepared statement] that invoked the busy handler.
2670251883Speter*/
2671322444SpeterSQLITE_API int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
2672251883Speter
2673251883Speter/*
2674251883Speter** CAPI3REF: Set A Busy Timeout
2675286510Speter** METHOD: sqlite3
2676251883Speter**
2677251883Speter** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
2678251883Speter** for a specified amount of time when a table is locked.  ^The handler
2679251883Speter** will sleep multiple times until at least "ms" milliseconds of sleeping
2680251883Speter** have accumulated.  ^After at least "ms" milliseconds of sleeping,
2681251883Speter** the handler returns 0 which causes [sqlite3_step()] to return
2682274884Sbapt** [SQLITE_BUSY].
2683251883Speter**
2684251883Speter** ^Calling this routine with an argument less than or equal to zero
2685251883Speter** turns off all busy handlers.
2686251883Speter**
2687251883Speter** ^(There can only be a single busy handler for a particular
2688274884Sbapt** [database connection] at any given moment.  If another busy handler
2689251883Speter** was defined  (using [sqlite3_busy_handler()]) prior to calling
2690251883Speter** this routine, that other busy handler is cleared.)^
2691274884Sbapt**
2692274884Sbapt** See also:  [PRAGMA busy_timeout]
2693251883Speter*/
2694322444SpeterSQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
2695251883Speter
2696251883Speter/*
2697251883Speter** CAPI3REF: Convenience Routines For Running Queries
2698286510Speter** METHOD: sqlite3
2699251883Speter**
2700251883Speter** This is a legacy interface that is preserved for backwards compatibility.
2701251883Speter** Use of this interface is not recommended.
2702251883Speter**
2703251883Speter** Definition: A <b>result table</b> is memory data structure created by the
2704251883Speter** [sqlite3_get_table()] interface.  A result table records the
2705251883Speter** complete query results from one or more queries.
2706251883Speter**
2707251883Speter** The table conceptually has a number of rows and columns.  But
2708251883Speter** these numbers are not part of the result table itself.  These
2709251883Speter** numbers are obtained separately.  Let N be the number of rows
2710251883Speter** and M be the number of columns.
2711251883Speter**
2712251883Speter** A result table is an array of pointers to zero-terminated UTF-8 strings.
2713251883Speter** There are (N+1)*M elements in the array.  The first M pointers point
2714251883Speter** to zero-terminated strings that  contain the names of the columns.
2715251883Speter** The remaining entries all point to query results.  NULL values result
2716251883Speter** in NULL pointers.  All other values are in their UTF-8 zero-terminated
2717251883Speter** string representation as returned by [sqlite3_column_text()].
2718251883Speter**
2719251883Speter** A result table might consist of one or more memory allocations.
2720251883Speter** It is not safe to pass a result table directly to [sqlite3_free()].
2721251883Speter** A result table should be deallocated using [sqlite3_free_table()].
2722251883Speter**
2723251883Speter** ^(As an example of the result table format, suppose a query result
2724251883Speter** is as follows:
2725251883Speter**
2726251883Speter** <blockquote><pre>
2727251883Speter**        Name        | Age
2728251883Speter**        -----------------------
2729251883Speter**        Alice       | 43
2730251883Speter**        Bob         | 28
2731251883Speter**        Cindy       | 21
2732251883Speter** </pre></blockquote>
2733251883Speter**
2734361456Scy** There are two columns (M==2) and three rows (N==3).  Thus the
2735251883Speter** result table has 8 entries.  Suppose the result table is stored
2736361456Scy** in an array named azResult.  Then azResult holds this content:
2737251883Speter**
2738251883Speter** <blockquote><pre>
2739251883Speter**        azResult&#91;0] = "Name";
2740251883Speter**        azResult&#91;1] = "Age";
2741251883Speter**        azResult&#91;2] = "Alice";
2742251883Speter**        azResult&#91;3] = "43";
2743251883Speter**        azResult&#91;4] = "Bob";
2744251883Speter**        azResult&#91;5] = "28";
2745251883Speter**        azResult&#91;6] = "Cindy";
2746251883Speter**        azResult&#91;7] = "21";
2747251883Speter** </pre></blockquote>)^
2748251883Speter**
2749251883Speter** ^The sqlite3_get_table() function evaluates one or more
2750251883Speter** semicolon-separated SQL statements in the zero-terminated UTF-8
2751251883Speter** string of its 2nd parameter and returns a result table to the
2752251883Speter** pointer given in its 3rd parameter.
2753251883Speter**
2754251883Speter** After the application has finished with the result from sqlite3_get_table(),
2755251883Speter** it must pass the result table pointer to sqlite3_free_table() in order to
2756251883Speter** release the memory that was malloced.  Because of the way the
2757251883Speter** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
2758251883Speter** function must not try to call [sqlite3_free()] directly.  Only
2759251883Speter** [sqlite3_free_table()] is able to release the memory properly and safely.
2760251883Speter**
2761251883Speter** The sqlite3_get_table() interface is implemented as a wrapper around
2762251883Speter** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
2763251883Speter** to any internal data structures of SQLite.  It uses only the public
2764251883Speter** interface defined here.  As a consequence, errors that occur in the
2765251883Speter** wrapper layer outside of the internal [sqlite3_exec()] call are not
2766251883Speter** reflected in subsequent calls to [sqlite3_errcode()] or
2767251883Speter** [sqlite3_errmsg()].
2768251883Speter*/
2769322444SpeterSQLITE_API int sqlite3_get_table(
2770251883Speter  sqlite3 *db,          /* An open database */
2771251883Speter  const char *zSql,     /* SQL to be evaluated */
2772251883Speter  char ***pazResult,    /* Results of the query */
2773251883Speter  int *pnRow,           /* Number of result rows written here */
2774251883Speter  int *pnColumn,        /* Number of result columns written here */
2775251883Speter  char **pzErrmsg       /* Error msg written here */
2776251883Speter);
2777322444SpeterSQLITE_API void sqlite3_free_table(char **result);
2778251883Speter
2779251883Speter/*
2780251883Speter** CAPI3REF: Formatted String Printing Functions
2781251883Speter**
2782251883Speter** These routines are work-alikes of the "printf()" family of functions
2783251883Speter** from the standard C library.
2784342292Scy** These routines understand most of the common formatting options from
2785366076Scy** the standard library printf()
2786342292Scy** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
2787342292Scy** See the [built-in printf()] documentation for details.
2788251883Speter**
2789251883Speter** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
2790342292Scy** results into memory obtained from [sqlite3_malloc64()].
2791251883Speter** The strings returned by these two routines should be
2792251883Speter** released by [sqlite3_free()].  ^Both routines return a
2793342292Scy** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
2794251883Speter** memory to hold the resulting string.
2795251883Speter**
2796251883Speter** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
2797251883Speter** the standard C library.  The result is written into the
2798251883Speter** buffer supplied as the second parameter whose size is given by
2799251883Speter** the first parameter. Note that the order of the
2800251883Speter** first two parameters is reversed from snprintf().)^  This is an
2801251883Speter** historical accident that cannot be fixed without breaking
2802251883Speter** backwards compatibility.  ^(Note also that sqlite3_snprintf()
2803251883Speter** returns a pointer to its buffer instead of the number of
2804251883Speter** characters actually written into the buffer.)^  We admit that
2805251883Speter** the number of characters written would be a more useful return
2806251883Speter** value but we cannot change the implementation of sqlite3_snprintf()
2807251883Speter** now without breaking compatibility.
2808251883Speter**
2809251883Speter** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
2810251883Speter** guarantees that the buffer is always zero-terminated.  ^The first
2811251883Speter** parameter "n" is the total size of the buffer, including space for
2812251883Speter** the zero terminator.  So the longest string that can be completely
2813251883Speter** written will be n-1 characters.
2814251883Speter**
2815251883Speter** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
2816251883Speter**
2817342292Scy** See also:  [built-in printf()], [printf() SQL function]
2818251883Speter*/
2819322444SpeterSQLITE_API char *sqlite3_mprintf(const char*,...);
2820322444SpeterSQLITE_API char *sqlite3_vmprintf(const char*, va_list);
2821322444SpeterSQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
2822322444SpeterSQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
2823251883Speter
2824251883Speter/*
2825251883Speter** CAPI3REF: Memory Allocation Subsystem
2826251883Speter**
2827251883Speter** The SQLite core uses these three routines for all of its own
2828251883Speter** internal memory allocation needs. "Core" in the previous sentence
2829361456Scy** does not include operating-system specific [VFS] implementation.  The
2830251883Speter** Windows VFS uses native malloc() and free() for some operations.
2831251883Speter**
2832251883Speter** ^The sqlite3_malloc() routine returns a pointer to a block
2833251883Speter** of memory at least N bytes in length, where N is the parameter.
2834251883Speter** ^If sqlite3_malloc() is unable to obtain sufficient free
2835251883Speter** memory, it returns a NULL pointer.  ^If the parameter N to
2836251883Speter** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
2837251883Speter** a NULL pointer.
2838251883Speter**
2839274884Sbapt** ^The sqlite3_malloc64(N) routine works just like
2840274884Sbapt** sqlite3_malloc(N) except that N is an unsigned 64-bit integer instead
2841274884Sbapt** of a signed 32-bit integer.
2842274884Sbapt**
2843251883Speter** ^Calling sqlite3_free() with a pointer previously returned
2844251883Speter** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
2845251883Speter** that it might be reused.  ^The sqlite3_free() routine is
2846251883Speter** a no-op if is called with a NULL pointer.  Passing a NULL pointer
2847251883Speter** to sqlite3_free() is harmless.  After being freed, memory
2848251883Speter** should neither be read nor written.  Even reading previously freed
2849251883Speter** memory might result in a segmentation fault or other severe error.
2850251883Speter** Memory corruption, a segmentation fault, or other severe error
2851251883Speter** might result if sqlite3_free() is called with a non-NULL pointer that
2852251883Speter** was not obtained from sqlite3_malloc() or sqlite3_realloc().
2853251883Speter**
2854274884Sbapt** ^The sqlite3_realloc(X,N) interface attempts to resize a
2855274884Sbapt** prior memory allocation X to be at least N bytes.
2856274884Sbapt** ^If the X parameter to sqlite3_realloc(X,N)
2857251883Speter** is a NULL pointer then its behavior is identical to calling
2858274884Sbapt** sqlite3_malloc(N).
2859274884Sbapt** ^If the N parameter to sqlite3_realloc(X,N) is zero or
2860251883Speter** negative then the behavior is exactly the same as calling
2861274884Sbapt** sqlite3_free(X).
2862274884Sbapt** ^sqlite3_realloc(X,N) returns a pointer to a memory allocation
2863274884Sbapt** of at least N bytes in size or NULL if insufficient memory is available.
2864251883Speter** ^If M is the size of the prior allocation, then min(N,M) bytes
2865251883Speter** of the prior allocation are copied into the beginning of buffer returned
2866274884Sbapt** by sqlite3_realloc(X,N) and the prior allocation is freed.
2867274884Sbapt** ^If sqlite3_realloc(X,N) returns NULL and N is positive, then the
2868274884Sbapt** prior allocation is not freed.
2869251883Speter**
2870274884Sbapt** ^The sqlite3_realloc64(X,N) interfaces works the same as
2871274884Sbapt** sqlite3_realloc(X,N) except that N is a 64-bit unsigned integer instead
2872274884Sbapt** of a 32-bit signed integer.
2873274884Sbapt**
2874274884Sbapt** ^If X is a memory allocation previously obtained from sqlite3_malloc(),
2875274884Sbapt** sqlite3_malloc64(), sqlite3_realloc(), or sqlite3_realloc64(), then
2876274884Sbapt** sqlite3_msize(X) returns the size of that memory allocation in bytes.
2877274884Sbapt** ^The value returned by sqlite3_msize(X) might be larger than the number
2878274884Sbapt** of bytes requested when X was allocated.  ^If X is a NULL pointer then
2879274884Sbapt** sqlite3_msize(X) returns zero.  If X points to something that is not
2880274884Sbapt** the beginning of memory allocation, or if it points to a formerly
2881274884Sbapt** valid memory allocation that has now been freed, then the behavior
2882274884Sbapt** of sqlite3_msize(X) is undefined and possibly harmful.
2883274884Sbapt**
2884274884Sbapt** ^The memory returned by sqlite3_malloc(), sqlite3_realloc(),
2885274884Sbapt** sqlite3_malloc64(), and sqlite3_realloc64()
2886251883Speter** is always aligned to at least an 8 byte boundary, or to a
2887251883Speter** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
2888251883Speter** option is used.
2889251883Speter**
2890251883Speter** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
2891251883Speter** must be either NULL or else pointers obtained from a prior
2892251883Speter** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
2893251883Speter** not yet been released.
2894251883Speter**
2895251883Speter** The application must not read or write any part of
2896251883Speter** a block of memory after it has been released using
2897251883Speter** [sqlite3_free()] or [sqlite3_realloc()].
2898251883Speter*/
2899322444SpeterSQLITE_API void *sqlite3_malloc(int);
2900322444SpeterSQLITE_API void *sqlite3_malloc64(sqlite3_uint64);
2901322444SpeterSQLITE_API void *sqlite3_realloc(void*, int);
2902322444SpeterSQLITE_API void *sqlite3_realloc64(void*, sqlite3_uint64);
2903322444SpeterSQLITE_API void sqlite3_free(void*);
2904322444SpeterSQLITE_API sqlite3_uint64 sqlite3_msize(void*);
2905251883Speter
2906251883Speter/*
2907251883Speter** CAPI3REF: Memory Allocator Statistics
2908251883Speter**
2909251883Speter** SQLite provides these two interfaces for reporting on the status
2910251883Speter** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
2911251883Speter** routines, which form the built-in memory allocation subsystem.
2912251883Speter**
2913251883Speter** ^The [sqlite3_memory_used()] routine returns the number of bytes
2914251883Speter** of memory currently outstanding (malloced but not freed).
2915251883Speter** ^The [sqlite3_memory_highwater()] routine returns the maximum
2916251883Speter** value of [sqlite3_memory_used()] since the high-water mark
2917251883Speter** was last reset.  ^The values returned by [sqlite3_memory_used()] and
2918251883Speter** [sqlite3_memory_highwater()] include any overhead
2919251883Speter** added by SQLite in its implementation of [sqlite3_malloc()],
2920251883Speter** but not overhead added by the any underlying system library
2921251883Speter** routines that [sqlite3_malloc()] may call.
2922251883Speter**
2923251883Speter** ^The memory high-water mark is reset to the current value of
2924251883Speter** [sqlite3_memory_used()] if and only if the parameter to
2925251883Speter** [sqlite3_memory_highwater()] is true.  ^The value returned
2926251883Speter** by [sqlite3_memory_highwater(1)] is the high-water mark
2927251883Speter** prior to the reset.
2928251883Speter*/
2929322444SpeterSQLITE_API sqlite3_int64 sqlite3_memory_used(void);
2930322444SpeterSQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
2931251883Speter
2932251883Speter/*
2933251883Speter** CAPI3REF: Pseudo-Random Number Generator
2934251883Speter**
2935251883Speter** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
2936251883Speter** select random [ROWID | ROWIDs] when inserting new records into a table that
2937251883Speter** already uses the largest possible [ROWID].  The PRNG is also used for
2938361456Scy** the built-in random() and randomblob() SQL functions.  This interface allows
2939251883Speter** applications to access the same PRNG for other purposes.
2940251883Speter**
2941251883Speter** ^A call to this routine stores N bytes of randomness into buffer P.
2942282328Sbapt** ^The P parameter can be a NULL pointer.
2943251883Speter**
2944269851Speter** ^If this routine has not been previously called or if the previous
2945282328Sbapt** call had N less than one or a NULL pointer for P, then the PRNG is
2946282328Sbapt** seeded using randomness obtained from the xRandomness method of
2947282328Sbapt** the default [sqlite3_vfs] object.
2948282328Sbapt** ^If the previous call to this routine had an N of 1 or more and a
2949282328Sbapt** non-NULL P then the pseudo-randomness is generated
2950251883Speter** internally and without recourse to the [sqlite3_vfs] xRandomness
2951251883Speter** method.
2952251883Speter*/
2953322444SpeterSQLITE_API void sqlite3_randomness(int N, void *P);
2954251883Speter
2955251883Speter/*
2956251883Speter** CAPI3REF: Compile-Time Authorization Callbacks
2957286510Speter** METHOD: sqlite3
2958322444Speter** KEYWORDS: {authorizer callback}
2959251883Speter**
2960251883Speter** ^This routine registers an authorizer callback with a particular
2961251883Speter** [database connection], supplied in the first argument.
2962251883Speter** ^The authorizer callback is invoked as SQL statements are being compiled
2963251883Speter** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
2964322444Speter** [sqlite3_prepare_v3()], [sqlite3_prepare16()], [sqlite3_prepare16_v2()],
2965322444Speter** and [sqlite3_prepare16_v3()].  ^At various
2966251883Speter** points during the compilation process, as logic is being created
2967251883Speter** to perform various actions, the authorizer callback is invoked to
2968251883Speter** see if those actions are allowed.  ^The authorizer callback should
2969251883Speter** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
2970251883Speter** specific action but allow the SQL statement to continue to be
2971251883Speter** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
2972251883Speter** rejected with an error.  ^If the authorizer callback returns
2973251883Speter** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
2974251883Speter** then the [sqlite3_prepare_v2()] or equivalent call that triggered
2975251883Speter** the authorizer will fail with an error message.
2976251883Speter**
2977251883Speter** When the callback returns [SQLITE_OK], that means the operation
2978251883Speter** requested is ok.  ^When the callback returns [SQLITE_DENY], the
2979251883Speter** [sqlite3_prepare_v2()] or equivalent call that triggered the
2980251883Speter** authorizer will fail with an error message explaining that
2981366076Scy** access is denied.
2982251883Speter**
2983251883Speter** ^The first parameter to the authorizer callback is a copy of the third
2984251883Speter** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
2985251883Speter** to the callback is an integer [SQLITE_COPY | action code] that specifies
2986251883Speter** the particular action to be authorized. ^The third through sixth parameters
2987322444Speter** to the callback are either NULL pointers or zero-terminated strings
2988322444Speter** that contain additional details about the action to be authorized.
2989322444Speter** Applications must always be prepared to encounter a NULL pointer in any
2990322444Speter** of the third through the sixth parameters of the authorization callback.
2991251883Speter**
2992251883Speter** ^If the action code is [SQLITE_READ]
2993251883Speter** and the callback returns [SQLITE_IGNORE] then the
2994251883Speter** [prepared statement] statement is constructed to substitute
2995251883Speter** a NULL value in place of the table column that would have
2996251883Speter** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
2997251883Speter** return can be used to deny an untrusted user access to individual
2998251883Speter** columns of a table.
2999322444Speter** ^When a table is referenced by a [SELECT] but no column values are
3000322444Speter** extracted from that table (for example in a query like
3001322444Speter** "SELECT count(*) FROM tab") then the [SQLITE_READ] authorizer callback
3002322444Speter** is invoked once for that table with a column name that is an empty string.
3003251883Speter** ^If the action code is [SQLITE_DELETE] and the callback returns
3004251883Speter** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
3005251883Speter** [truncate optimization] is disabled and all rows are deleted individually.
3006251883Speter**
3007251883Speter** An authorizer is used when [sqlite3_prepare | preparing]
3008251883Speter** SQL statements from an untrusted source, to ensure that the SQL statements
3009251883Speter** do not try to access data they are not allowed to see, or that they do not
3010251883Speter** try to execute malicious statements that damage the database.  For
3011251883Speter** example, an application may allow a user to enter arbitrary
3012251883Speter** SQL queries for evaluation by a database.  But the application does
3013251883Speter** not want the user to be able to make arbitrary changes to the
3014251883Speter** database.  An authorizer could then be put in place while the
3015251883Speter** user-entered SQL is being [sqlite3_prepare | prepared] that
3016251883Speter** disallows everything except [SELECT] statements.
3017251883Speter**
3018251883Speter** Applications that need to process SQL from untrusted sources
3019251883Speter** might also consider lowering resource limits using [sqlite3_limit()]
3020251883Speter** and limiting database size using the [max_page_count] [PRAGMA]
3021251883Speter** in addition to using an authorizer.
3022251883Speter**
3023251883Speter** ^(Only a single authorizer can be in place on a database connection
3024251883Speter** at a time.  Each call to sqlite3_set_authorizer overrides the
3025251883Speter** previous call.)^  ^Disable the authorizer by installing a NULL callback.
3026251883Speter** The authorizer is disabled by default.
3027251883Speter**
3028251883Speter** The authorizer callback must not do anything that will modify
3029251883Speter** the database connection that invoked the authorizer callback.
3030251883Speter** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3031251883Speter** database connections for the meaning of "modify" in this paragraph.
3032251883Speter**
3033251883Speter** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
3034366076Scy** statement might be re-prepared during [sqlite3_step()] due to a
3035251883Speter** schema change.  Hence, the application should ensure that the
3036251883Speter** correct authorizer callback remains in place during the [sqlite3_step()].
3037251883Speter**
3038251883Speter** ^Note that the authorizer callback is invoked only during
3039251883Speter** [sqlite3_prepare()] or its variants.  Authorization is not
3040251883Speter** performed during statement evaluation in [sqlite3_step()], unless
3041251883Speter** as stated in the previous paragraph, sqlite3_step() invokes
3042251883Speter** sqlite3_prepare_v2() to reprepare a statement after a schema change.
3043251883Speter*/
3044322444SpeterSQLITE_API int sqlite3_set_authorizer(
3045251883Speter  sqlite3*,
3046251883Speter  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
3047251883Speter  void *pUserData
3048251883Speter);
3049251883Speter
3050251883Speter/*
3051251883Speter** CAPI3REF: Authorizer Return Codes
3052251883Speter**
3053251883Speter** The [sqlite3_set_authorizer | authorizer callback function] must
3054251883Speter** return either [SQLITE_OK] or one of these two constants in order
3055251883Speter** to signal SQLite whether or not the action is permitted.  See the
3056251883Speter** [sqlite3_set_authorizer | authorizer documentation] for additional
3057251883Speter** information.
3058251883Speter**
3059274884Sbapt** Note that SQLITE_IGNORE is also used as a [conflict resolution mode]
3060274884Sbapt** returned from the [sqlite3_vtab_on_conflict()] interface.
3061251883Speter*/
3062251883Speter#define SQLITE_DENY   1   /* Abort the SQL statement with an error */
3063251883Speter#define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
3064251883Speter
3065251883Speter/*
3066251883Speter** CAPI3REF: Authorizer Action Codes
3067251883Speter**
3068251883Speter** The [sqlite3_set_authorizer()] interface registers a callback function
3069251883Speter** that is invoked to authorize certain SQL statement actions.  The
3070251883Speter** second parameter to the callback is an integer code that specifies
3071251883Speter** what action is being authorized.  These are the integer action codes that
3072251883Speter** the authorizer callback may be passed.
3073251883Speter**
3074251883Speter** These action code values signify what kind of operation is to be
3075251883Speter** authorized.  The 3rd and 4th parameters to the authorization
3076251883Speter** callback function will be parameters or NULL depending on which of these
3077251883Speter** codes is used as the second parameter.  ^(The 5th parameter to the
3078251883Speter** authorizer callback is the name of the database ("main", "temp",
3079251883Speter** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
3080251883Speter** is the name of the inner-most trigger or view that is responsible for
3081251883Speter** the access attempt or NULL if this access attempt is directly from
3082251883Speter** top-level SQL code.
3083251883Speter*/
3084251883Speter/******************************************* 3rd ************ 4th ***********/
3085251883Speter#define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
3086251883Speter#define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
3087251883Speter#define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
3088251883Speter#define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
3089251883Speter#define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
3090251883Speter#define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
3091251883Speter#define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
3092251883Speter#define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
3093251883Speter#define SQLITE_DELETE                9   /* Table Name      NULL            */
3094251883Speter#define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
3095251883Speter#define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
3096251883Speter#define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
3097251883Speter#define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
3098251883Speter#define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
3099251883Speter#define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
3100251883Speter#define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
3101251883Speter#define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
3102251883Speter#define SQLITE_INSERT               18   /* Table Name      NULL            */
3103251883Speter#define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
3104251883Speter#define SQLITE_READ                 20   /* Table Name      Column Name     */
3105251883Speter#define SQLITE_SELECT               21   /* NULL            NULL            */
3106251883Speter#define SQLITE_TRANSACTION          22   /* Operation       NULL            */
3107251883Speter#define SQLITE_UPDATE               23   /* Table Name      Column Name     */
3108251883Speter#define SQLITE_ATTACH               24   /* Filename        NULL            */
3109251883Speter#define SQLITE_DETACH               25   /* Database Name   NULL            */
3110251883Speter#define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
3111251883Speter#define SQLITE_REINDEX              27   /* Index Name      NULL            */
3112251883Speter#define SQLITE_ANALYZE              28   /* Table Name      NULL            */
3113251883Speter#define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
3114251883Speter#define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
3115251883Speter#define SQLITE_FUNCTION             31   /* NULL            Function Name   */
3116251883Speter#define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
3117251883Speter#define SQLITE_COPY                  0   /* No longer used */
3118269851Speter#define SQLITE_RECURSIVE            33   /* NULL            NULL            */
3119251883Speter
3120251883Speter/*
3121251883Speter** CAPI3REF: Tracing And Profiling Functions
3122286510Speter** METHOD: sqlite3
3123251883Speter**
3124305002Scy** These routines are deprecated. Use the [sqlite3_trace_v2()] interface
3125305002Scy** instead of the routines described here.
3126305002Scy**
3127251883Speter** These routines register callback functions that can be used for
3128251883Speter** tracing and profiling the execution of SQL statements.
3129251883Speter**
3130251883Speter** ^The callback function registered by sqlite3_trace() is invoked at
3131251883Speter** various times when an SQL statement is being run by [sqlite3_step()].
3132251883Speter** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
3133251883Speter** SQL statement text as the statement first begins executing.
3134251883Speter** ^(Additional sqlite3_trace() callbacks might occur
3135251883Speter** as each triggered subprogram is entered.  The callbacks for triggers
3136251883Speter** contain a UTF-8 SQL comment that identifies the trigger.)^
3137251883Speter**
3138251883Speter** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
3139251883Speter** the length of [bound parameter] expansion in the output of sqlite3_trace().
3140251883Speter**
3141251883Speter** ^The callback function registered by sqlite3_profile() is invoked
3142251883Speter** as each SQL statement finishes.  ^The profile callback contains
3143251883Speter** the original statement text and an estimate of wall-clock time
3144251883Speter** of how long that statement took to run.  ^The profile callback
3145251883Speter** time is in units of nanoseconds, however the current implementation
3146251883Speter** is only capable of millisecond resolution so the six least significant
3147251883Speter** digits in the time are meaningless.  Future versions of SQLite
3148346442Scy** might provide greater resolution on the profiler callback.  Invoking
3149346442Scy** either [sqlite3_trace()] or [sqlite3_trace_v2()] will cancel the
3150346442Scy** profile callback.
3151251883Speter*/
3152322444SpeterSQLITE_API SQLITE_DEPRECATED void *sqlite3_trace(sqlite3*,
3153305002Scy   void(*xTrace)(void*,const char*), void*);
3154322444SpeterSQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
3155251883Speter   void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
3156251883Speter
3157251883Speter/*
3158305002Scy** CAPI3REF: SQL Trace Event Codes
3159305002Scy** KEYWORDS: SQLITE_TRACE
3160305002Scy**
3161305002Scy** These constants identify classes of events that can be monitored
3162342292Scy** using the [sqlite3_trace_v2()] tracing logic.  The M argument
3163342292Scy** to [sqlite3_trace_v2(D,M,X,P)] is an OR-ed combination of one or more of
3164305002Scy** the following constants.  ^The first argument to the trace callback
3165305002Scy** is one of the following constants.
3166305002Scy**
3167305002Scy** New tracing constants may be added in future releases.
3168305002Scy**
3169305002Scy** ^A trace callback has four arguments: xCallback(T,C,P,X).
3170305002Scy** ^The T argument is one of the integer type codes above.
3171305002Scy** ^The C argument is a copy of the context pointer passed in as the
3172305002Scy** fourth argument to [sqlite3_trace_v2()].
3173305002Scy** The P and X arguments are pointers whose meanings depend on T.
3174305002Scy**
3175305002Scy** <dl>
3176305002Scy** [[SQLITE_TRACE_STMT]] <dt>SQLITE_TRACE_STMT</dt>
3177305002Scy** <dd>^An SQLITE_TRACE_STMT callback is invoked when a prepared statement
3178305002Scy** first begins running and possibly at other times during the
3179305002Scy** execution of the prepared statement, such as at the start of each
3180305002Scy** trigger subprogram. ^The P argument is a pointer to the
3181305002Scy** [prepared statement]. ^The X argument is a pointer to a string which
3182366076Scy** is the unexpanded SQL text of the prepared statement or an SQL comment
3183305002Scy** that indicates the invocation of a trigger.  ^The callback can compute
3184305002Scy** the same text that would have been returned by the legacy [sqlite3_trace()]
3185305002Scy** interface by using the X argument when X begins with "--" and invoking
3186305002Scy** [sqlite3_expanded_sql(P)] otherwise.
3187305002Scy**
3188305002Scy** [[SQLITE_TRACE_PROFILE]] <dt>SQLITE_TRACE_PROFILE</dt>
3189305002Scy** <dd>^An SQLITE_TRACE_PROFILE callback provides approximately the same
3190305002Scy** information as is provided by the [sqlite3_profile()] callback.
3191305002Scy** ^The P argument is a pointer to the [prepared statement] and the
3192305002Scy** X argument points to a 64-bit integer which is the estimated of
3193305002Scy** the number of nanosecond that the prepared statement took to run.
3194305002Scy** ^The SQLITE_TRACE_PROFILE callback is invoked when the statement finishes.
3195305002Scy**
3196305002Scy** [[SQLITE_TRACE_ROW]] <dt>SQLITE_TRACE_ROW</dt>
3197305002Scy** <dd>^An SQLITE_TRACE_ROW callback is invoked whenever a prepared
3198366076Scy** statement generates a single row of result.
3199305002Scy** ^The P argument is a pointer to the [prepared statement] and the
3200305002Scy** X argument is unused.
3201305002Scy**
3202305002Scy** [[SQLITE_TRACE_CLOSE]] <dt>SQLITE_TRACE_CLOSE</dt>
3203305002Scy** <dd>^An SQLITE_TRACE_CLOSE callback is invoked when a database
3204305002Scy** connection closes.
3205305002Scy** ^The P argument is a pointer to the [database connection] object
3206305002Scy** and the X argument is unused.
3207305002Scy** </dl>
3208305002Scy*/
3209305002Scy#define SQLITE_TRACE_STMT       0x01
3210305002Scy#define SQLITE_TRACE_PROFILE    0x02
3211305002Scy#define SQLITE_TRACE_ROW        0x04
3212305002Scy#define SQLITE_TRACE_CLOSE      0x08
3213305002Scy
3214305002Scy/*
3215305002Scy** CAPI3REF: SQL Trace Hook
3216305002Scy** METHOD: sqlite3
3217305002Scy**
3218305002Scy** ^The sqlite3_trace_v2(D,M,X,P) interface registers a trace callback
3219305002Scy** function X against [database connection] D, using property mask M
3220305002Scy** and context pointer P.  ^If the X callback is
3221305002Scy** NULL or if the M mask is zero, then tracing is disabled.  The
3222305002Scy** M argument should be the bitwise OR-ed combination of
3223305002Scy** zero or more [SQLITE_TRACE] constants.
3224305002Scy**
3225366076Scy** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides
3226305002Scy** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
3227305002Scy**
3228366076Scy** ^The X callback is invoked whenever any of the events identified by
3229305002Scy** mask M occur.  ^The integer return value from the callback is currently
3230305002Scy** ignored, though this may change in future releases.  Callback
3231305002Scy** implementations should return zero to ensure future compatibility.
3232305002Scy**
3233305002Scy** ^A trace callback is invoked with four arguments: callback(T,C,P,X).
3234305002Scy** ^The T argument is one of the [SQLITE_TRACE]
3235305002Scy** constants to indicate why the callback was invoked.
3236305002Scy** ^The C argument is a copy of the context pointer.
3237305002Scy** The P and X arguments are pointers whose meanings depend on T.
3238305002Scy**
3239305002Scy** The sqlite3_trace_v2() interface is intended to replace the legacy
3240305002Scy** interfaces [sqlite3_trace()] and [sqlite3_profile()], both of which
3241305002Scy** are deprecated.
3242305002Scy*/
3243322444SpeterSQLITE_API int sqlite3_trace_v2(
3244305002Scy  sqlite3*,
3245305002Scy  unsigned uMask,
3246305002Scy  int(*xCallback)(unsigned,void*,void*,void*),
3247305002Scy  void *pCtx
3248305002Scy);
3249305002Scy
3250305002Scy/*
3251251883Speter** CAPI3REF: Query Progress Callbacks
3252286510Speter** METHOD: sqlite3
3253251883Speter**
3254251883Speter** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
3255251883Speter** function X to be invoked periodically during long running calls to
3256251883Speter** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
3257251883Speter** database connection D.  An example use for this
3258251883Speter** interface is to keep a GUI updated during a large query.
3259251883Speter**
3260366076Scy** ^The parameter P is passed through as the only parameter to the
3261366076Scy** callback function X.  ^The parameter N is the approximate number of
3262251883Speter** [virtual machine instructions] that are evaluated between successive
3263269851Speter** invocations of the callback X.  ^If N is less than one then the progress
3264269851Speter** handler is disabled.
3265251883Speter**
3266251883Speter** ^Only a single progress handler may be defined at one time per
3267251883Speter** [database connection]; setting a new progress handler cancels the
3268251883Speter** old one.  ^Setting parameter X to NULL disables the progress handler.
3269251883Speter** ^The progress handler is also disabled by setting N to a value less
3270251883Speter** than 1.
3271251883Speter**
3272251883Speter** ^If the progress callback returns non-zero, the operation is
3273251883Speter** interrupted.  This feature can be used to implement a
3274251883Speter** "Cancel" button on a GUI progress dialog box.
3275251883Speter**
3276251883Speter** The progress handler callback must not do anything that will modify
3277251883Speter** the database connection that invoked the progress handler.
3278251883Speter** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
3279251883Speter** database connections for the meaning of "modify" in this paragraph.
3280251883Speter**
3281251883Speter*/
3282322444SpeterSQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
3283251883Speter
3284251883Speter/*
3285251883Speter** CAPI3REF: Opening A New Database Connection
3286286510Speter** CONSTRUCTOR: sqlite3
3287251883Speter**
3288366076Scy** ^These routines open an SQLite database file as specified by the
3289251883Speter** filename argument. ^The filename argument is interpreted as UTF-8 for
3290251883Speter** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
3291251883Speter** order for sqlite3_open16(). ^(A [database connection] handle is usually
3292251883Speter** returned in *ppDb, even if an error occurs.  The only exception is that
3293251883Speter** if SQLite is unable to allocate memory to hold the [sqlite3] object,
3294251883Speter** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
3295251883Speter** object.)^ ^(If the database is opened (and/or created) successfully, then
3296251883Speter** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
3297251883Speter** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
3298251883Speter** an English language description of the error following a failure of any
3299251883Speter** of the sqlite3_open() routines.
3300251883Speter**
3301274884Sbapt** ^The default encoding will be UTF-8 for databases created using
3302274884Sbapt** sqlite3_open() or sqlite3_open_v2().  ^The default encoding for databases
3303274884Sbapt** created using sqlite3_open16() will be UTF-16 in the native byte order.
3304251883Speter**
3305251883Speter** Whether or not an error occurs when it is opened, resources
3306251883Speter** associated with the [database connection] handle should be released by
3307251883Speter** passing it to [sqlite3_close()] when it is no longer required.
3308251883Speter**
3309251883Speter** The sqlite3_open_v2() interface works like sqlite3_open()
3310251883Speter** except that it accepts two additional parameters for additional control
3311251883Speter** over the new database connection.  ^(The flags parameter to
3312361456Scy** sqlite3_open_v2() must include, at a minimum, one of the following
3313361456Scy** three flag combinations:)^
3314251883Speter**
3315251883Speter** <dl>
3316251883Speter** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
3317251883Speter** <dd>The database is opened in read-only mode.  If the database does not
3318251883Speter** already exist, an error is returned.</dd>)^
3319251883Speter**
3320251883Speter** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
3321251883Speter** <dd>The database is opened for reading and writing if possible, or reading
3322251883Speter** only if the file is write protected by the operating system.  In either
3323251883Speter** case the database must already exist, otherwise an error is returned.</dd>)^
3324251883Speter**
3325251883Speter** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
3326251883Speter** <dd>The database is opened for reading and writing, and is created if
3327251883Speter** it does not already exist. This is the behavior that is always used for
3328251883Speter** sqlite3_open() and sqlite3_open16().</dd>)^
3329251883Speter** </dl>
3330251883Speter**
3331361456Scy** In addition to the required flags, the following optional flags are
3332361456Scy** also supported:
3333361456Scy**
3334361456Scy** <dl>
3335361456Scy** ^(<dt>[SQLITE_OPEN_URI]</dt>
3336361456Scy** <dd>The filename can be interpreted as a URI if this flag is set.</dd>)^
3337361456Scy**
3338361456Scy** ^(<dt>[SQLITE_OPEN_MEMORY]</dt>
3339361456Scy** <dd>The database will be opened as an in-memory database.  The database
3340361456Scy** is named by the "filename" argument for the purposes of cache-sharing,
3341361456Scy** if shared cache mode is enabled, but the "filename" is otherwise ignored.
3342361456Scy** </dd>)^
3343361456Scy**
3344361456Scy** ^(<dt>[SQLITE_OPEN_NOMUTEX]</dt>
3345361456Scy** <dd>The new database connection will use the "multi-thread"
3346361456Scy** [threading mode].)^  This means that separate threads are allowed
3347361456Scy** to use SQLite at the same time, as long as each thread is using
3348361456Scy** a different [database connection].
3349361456Scy**
3350361456Scy** ^(<dt>[SQLITE_OPEN_FULLMUTEX]</dt>
3351361456Scy** <dd>The new database connection will use the "serialized"
3352361456Scy** [threading mode].)^  This means the multiple threads can safely
3353361456Scy** attempt to use the same database connection at the same time.
3354361456Scy** (Mutexes will block any actual concurrency, but in this mode
3355361456Scy** there is no harm in trying.)
3356361456Scy**
3357361456Scy** ^(<dt>[SQLITE_OPEN_SHAREDCACHE]</dt>
3358361456Scy** <dd>The database is opened [shared cache] enabled, overriding
3359361456Scy** the default shared cache setting provided by
3360361456Scy** [sqlite3_enable_shared_cache()].)^
3361361456Scy**
3362361456Scy** ^(<dt>[SQLITE_OPEN_PRIVATECACHE]</dt>
3363361456Scy** <dd>The database is opened [shared cache] disabled, overriding
3364361456Scy** the default shared cache setting provided by
3365361456Scy** [sqlite3_enable_shared_cache()].)^
3366361456Scy**
3367361456Scy** [[OPEN_NOFOLLOW]] ^(<dt>[SQLITE_OPEN_NOFOLLOW]</dt>
3368361456Scy** <dd>The database filename is not allowed to be a symbolic link</dd>
3369361456Scy** </dl>)^
3370361456Scy**
3371251883Speter** If the 3rd parameter to sqlite3_open_v2() is not one of the
3372361456Scy** required combinations shown above optionally combined with other
3373251883Speter** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
3374251883Speter** then the behavior is undefined.
3375251883Speter**
3376251883Speter** ^The fourth parameter to sqlite3_open_v2() is the name of the
3377251883Speter** [sqlite3_vfs] object that defines the operating system interface that
3378251883Speter** the new database connection should use.  ^If the fourth parameter is
3379251883Speter** a NULL pointer then the default [sqlite3_vfs] object is used.
3380251883Speter**
3381251883Speter** ^If the filename is ":memory:", then a private, temporary in-memory database
3382251883Speter** is created for the connection.  ^This in-memory database will vanish when
3383251883Speter** the database connection is closed.  Future versions of SQLite might
3384251883Speter** make use of additional special filenames that begin with the ":" character.
3385251883Speter** It is recommended that when a database filename actually does begin with
3386251883Speter** a ":" character you should prefix the filename with a pathname such as
3387251883Speter** "./" to avoid ambiguity.
3388251883Speter**
3389251883Speter** ^If the filename is an empty string, then a private, temporary
3390251883Speter** on-disk database will be created.  ^This private database will be
3391251883Speter** automatically deleted as soon as the database connection is closed.
3392251883Speter**
3393251883Speter** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
3394251883Speter**
3395251883Speter** ^If [URI filename] interpretation is enabled, and the filename argument
3396251883Speter** begins with "file:", then the filename is interpreted as a URI. ^URI
3397251883Speter** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
3398342292Scy** set in the third argument to sqlite3_open_v2(), or if it has
3399251883Speter** been enabled globally using the [SQLITE_CONFIG_URI] option with the
3400251883Speter** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
3401342292Scy** URI filename interpretation is turned off
3402251883Speter** by default, but future releases of SQLite might enable URI filename
3403251883Speter** interpretation by default.  See "[URI filenames]" for additional
3404251883Speter** information.
3405251883Speter**
3406251883Speter** URI filenames are parsed according to RFC 3986. ^If the URI contains an
3407366076Scy** authority, then it must be either an empty string or the string
3408366076Scy** "localhost". ^If the authority is not an empty string or "localhost", an
3409366076Scy** error is returned to the caller. ^The fragment component of a URI, if
3410251883Speter** present, is ignored.
3411251883Speter**
3412251883Speter** ^SQLite uses the path component of the URI as the name of the disk file
3413366076Scy** which contains the database. ^If the path begins with a '/' character,
3414366076Scy** then it is interpreted as an absolute path. ^If the path does not begin
3415251883Speter** with a '/' (meaning that the authority section is omitted from the URI)
3416366076Scy** then the path is interpreted as a relative path.
3417366076Scy** ^(On windows, the first component of an absolute path
3418274884Sbapt** is a drive specification (e.g. "C:").)^
3419251883Speter**
3420251883Speter** [[core URI query parameters]]
3421251883Speter** The query component of a URI may contain parameters that are interpreted
3422251883Speter** either by SQLite itself, or by a [VFS | custom VFS implementation].
3423274884Sbapt** SQLite and its built-in [VFSes] interpret the
3424274884Sbapt** following query parameters:
3425251883Speter**
3426251883Speter** <ul>
3427251883Speter**   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
3428251883Speter**     a VFS object that provides the operating system interface that should
3429251883Speter**     be used to access the database file on disk. ^If this option is set to
3430251883Speter**     an empty string the default VFS object is used. ^Specifying an unknown
3431251883Speter**     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
3432251883Speter**     present, then the VFS specified by the option takes precedence over
3433251883Speter**     the value passed as the fourth parameter to sqlite3_open_v2().
3434251883Speter**
3435251883Speter**   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
3436251883Speter**     "rwc", or "memory". Attempting to set it to any other value is
3437366076Scy**     an error)^.
3438366076Scy**     ^If "ro" is specified, then the database is opened for read-only
3439366076Scy**     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
3440366076Scy**     third argument to sqlite3_open_v2(). ^If the mode option is set to
3441366076Scy**     "rw", then the database is opened for read-write (but not create)
3442366076Scy**     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
3443366076Scy**     been set. ^Value "rwc" is equivalent to setting both
3444251883Speter**     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.  ^If the mode option is
3445251883Speter**     set to "memory" then a pure [in-memory database] that never reads
3446251883Speter**     or writes from disk is used. ^It is an error to specify a value for
3447251883Speter**     the mode parameter that is less restrictive than that specified by
3448251883Speter**     the flags passed in the third parameter to sqlite3_open_v2().
3449251883Speter**
3450251883Speter**   <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
3451251883Speter**     "private". ^Setting it to "shared" is equivalent to setting the
3452251883Speter**     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
3453366076Scy**     sqlite3_open_v2(). ^Setting the cache parameter to "private" is
3454251883Speter**     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
3455251883Speter**     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
3456251883Speter**     a URI filename, its value overrides any behavior requested by setting
3457251883Speter**     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
3458269851Speter**
3459274884Sbapt**  <li> <b>psow</b>: ^The psow parameter indicates whether or not the
3460269851Speter**     [powersafe overwrite] property does or does not apply to the
3461274884Sbapt**     storage media on which the database file resides.
3462269851Speter**
3463269851Speter**  <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
3464269851Speter**     which if set disables file locking in rollback journal modes.  This
3465269851Speter**     is useful for accessing a database on a filesystem that does not
3466269851Speter**     support locking.  Caution:  Database corruption might result if two
3467269851Speter**     or more processes write to the same database and any one of those
3468269851Speter**     processes uses nolock=1.
3469269851Speter**
3470269851Speter**  <li> <b>immutable</b>: ^The immutable parameter is a boolean query
3471269851Speter**     parameter that indicates that the database file is stored on
3472269851Speter**     read-only media.  ^When immutable is set, SQLite assumes that the
3473269851Speter**     database file cannot be changed, even by a process with higher
3474269851Speter**     privilege, and so the database is opened read-only and all locking
3475269851Speter**     and change detection is disabled.  Caution: Setting the immutable
3476269851Speter**     property on a database file that does in fact change can result
3477269851Speter**     in incorrect query results and/or [SQLITE_CORRUPT] errors.
3478269851Speter**     See also: [SQLITE_IOCAP_IMMUTABLE].
3479366076Scy**
3480251883Speter** </ul>
3481251883Speter**
3482251883Speter** ^Specifying an unknown parameter in the query component of a URI is not an
3483251883Speter** error.  Future versions of SQLite might understand additional query
3484251883Speter** parameters.  See "[query parameters with special meaning to SQLite]" for
3485251883Speter** additional information.
3486251883Speter**
3487251883Speter** [[URI filename examples]] <h3>URI filename examples</h3>
3488251883Speter**
3489251883Speter** <table border="1" align=center cellpadding=5>
3490251883Speter** <tr><th> URI filenames <th> Results
3491366076Scy** <tr><td> file:data.db <td>
3492251883Speter**          Open the file "data.db" in the current directory.
3493251883Speter** <tr><td> file:/home/fred/data.db<br>
3494366076Scy**          file:///home/fred/data.db <br>
3495366076Scy**          file://localhost/home/fred/data.db <br> <td>
3496251883Speter**          Open the database file "/home/fred/data.db".
3497366076Scy** <tr><td> file://darkstar/home/fred/data.db <td>
3498251883Speter**          An error. "darkstar" is not a recognized authority.
3499366076Scy** <tr><td style="white-space:nowrap">
3500251883Speter**          file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
3501251883Speter**     <td> Windows only: Open the file "data.db" on fred's desktop on drive
3502366076Scy**          C:. Note that the %20 escaping in this example is not strictly
3503251883Speter**          necessary - space characters can be used literally
3504251883Speter**          in URI filenames.
3505366076Scy** <tr><td> file:data.db?mode=ro&cache=private <td>
3506251883Speter**          Open file "data.db" in the current directory for read-only access.
3507251883Speter**          Regardless of whether or not shared-cache mode is enabled by
3508251883Speter**          default, use a private cache.
3509269851Speter** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
3510269851Speter**          Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
3511269851Speter**          that uses dot-files in place of posix advisory locking.
3512366076Scy** <tr><td> file:data.db?mode=readonly <td>
3513251883Speter**          An error. "readonly" is not a valid option for the "mode" parameter.
3514369951Scy**          Use "ro" instead:  "file:data.db?mode=ro".
3515251883Speter** </table>
3516251883Speter**
3517251883Speter** ^URI hexadecimal escape sequences (%HH) are supported within the path and
3518251883Speter** query components of a URI. A hexadecimal escape sequence consists of a
3519366076Scy** percent sign - "%" - followed by exactly two hexadecimal digits
3520251883Speter** specifying an octet value. ^Before the path or query components of a
3521366076Scy** URI filename are interpreted, they are encoded using UTF-8 and all
3522251883Speter** hexadecimal escape sequences replaced by a single byte containing the
3523251883Speter** corresponding octet. If this process generates an invalid UTF-8 encoding,
3524251883Speter** the results are undefined.
3525251883Speter**
3526251883Speter** <b>Note to Windows users:</b>  The encoding used for the filename argument
3527251883Speter** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
3528251883Speter** codepage is currently defined.  Filenames containing international
3529251883Speter** characters must be converted to UTF-8 prior to passing them into
3530251883Speter** sqlite3_open() or sqlite3_open_v2().
3531251883Speter**
3532251883Speter** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
3533251883Speter** prior to calling sqlite3_open() or sqlite3_open_v2().  Otherwise, various
3534251883Speter** features that require the use of temporary files may fail.
3535251883Speter**
3536251883Speter** See also: [sqlite3_temp_directory]
3537251883Speter*/
3538322444SpeterSQLITE_API int sqlite3_open(
3539251883Speter  const char *filename,   /* Database filename (UTF-8) */
3540251883Speter  sqlite3 **ppDb          /* OUT: SQLite db handle */
3541251883Speter);
3542322444SpeterSQLITE_API int sqlite3_open16(
3543251883Speter  const void *filename,   /* Database filename (UTF-16) */
3544251883Speter  sqlite3 **ppDb          /* OUT: SQLite db handle */
3545251883Speter);
3546322444SpeterSQLITE_API int sqlite3_open_v2(
3547251883Speter  const char *filename,   /* Database filename (UTF-8) */
3548251883Speter  sqlite3 **ppDb,         /* OUT: SQLite db handle */
3549251883Speter  int flags,              /* Flags */
3550251883Speter  const char *zVfs        /* Name of VFS module to use */
3551251883Speter);
3552251883Speter
3553251883Speter/*
3554251883Speter** CAPI3REF: Obtain Values For URI Parameters
3555251883Speter**
3556361456Scy** These are utility routines, useful to [VFS|custom VFS implementations],
3557366076Scy** that check if a database file was a URI that contained a specific query
3558251883Speter** parameter, and if so obtains the value of that query parameter.
3559251883Speter**
3560362190Scy** The first parameter to these interfaces (hereafter referred to
3561362190Scy** as F) must be one of:
3562362190Scy** <ul>
3563362190Scy** <li> A database filename pointer created by the SQLite core and
3564362190Scy** passed into the xOpen() method of a VFS implemention, or
3565362190Scy** <li> A filename obtained from [sqlite3_db_filename()], or
3566362190Scy** <li> A new filename constructed using [sqlite3_create_filename()].
3567362190Scy** </ul>
3568362190Scy** If the F parameter is not one of the above, then the behavior is
3569362190Scy** undefined and probably undesirable.  Older versions of SQLite were
3570362190Scy** more tolerant of invalid F parameters than newer versions.
3571362190Scy**
3572362190Scy** If F is a suitable filename (as described in the previous paragraph)
3573361456Scy** and if P is the name of the query parameter, then
3574251883Speter** sqlite3_uri_parameter(F,P) returns the value of the P
3575366076Scy** parameter if it exists or a NULL pointer if P does not appear as a
3576361456Scy** query parameter on F.  If P is a query parameter of F and it
3577251883Speter** has no explicit value, then sqlite3_uri_parameter(F,P) returns
3578251883Speter** a pointer to an empty string.
3579251883Speter**
3580251883Speter** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
3581251883Speter** parameter and returns true (1) or false (0) according to the value
3582251883Speter** of P.  The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
3583251883Speter** value of query parameter P is one of "yes", "true", or "on" in any
3584366076Scy** case or if the value begins with a non-zero number.  The
3585251883Speter** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
3586251883Speter** query parameter P is one of "no", "false", or "off" in any case or
3587251883Speter** if the value begins with a numeric zero.  If P is not a query
3588361456Scy** parameter on F or if the value of P does not match any of the
3589251883Speter** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
3590251883Speter**
3591251883Speter** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
3592251883Speter** 64-bit signed integer and returns that integer, or D if P does not
3593251883Speter** exist.  If the value of P is something other than an integer, then
3594251883Speter** zero is returned.
3595361456Scy**
3596361456Scy** The sqlite3_uri_key(F,N) returns a pointer to the name (not
3597361456Scy** the value) of the N-th query parameter for filename F, or a NULL
3598361456Scy** pointer if N is less than zero or greater than the number of query
3599361456Scy** parameters minus 1.  The N value is zero-based so N should be 0 to obtain
3600361456Scy** the name of the first query parameter, 1 for the second parameter, and
3601361456Scy** so forth.
3602366076Scy**
3603251883Speter** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
3604251883Speter** sqlite3_uri_boolean(F,P,B) returns B.  If F is not a NULL pointer and
3605361456Scy** is not a database file pathname pointer that the SQLite core passed
3606361456Scy** into the xOpen VFS method, then the behavior of this routine is undefined
3607361456Scy** and probably undesirable.
3608346442Scy**
3609361456Scy** Beginning with SQLite [version 3.31.0] ([dateof:3.31.0]) the input F
3610361456Scy** parameter can also be the name of a rollback journal file or WAL file
3611361456Scy** in addition to the main database file.  Prior to version 3.31.0, these
3612361456Scy** routines would only work if F was the name of the main database file.
3613361456Scy** When the F parameter is the name of the rollback journal or WAL file,
3614361456Scy** it has access to all the same query parameters as were found on the
3615361456Scy** main database file.
3616361456Scy**
3617346442Scy** See the [URI filename] documentation for additional information.
3618251883Speter*/
3619322444SpeterSQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
3620322444SpeterSQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
3621322444SpeterSQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
3622361456ScySQLITE_API const char *sqlite3_uri_key(const char *zFilename, int N);
3623251883Speter
3624361456Scy/*
3625361456Scy** CAPI3REF:  Translate filenames
3626361456Scy**
3627361456Scy** These routines are available to [VFS|custom VFS implementations] for
3628361456Scy** translating filenames between the main database file, the journal file,
3629361456Scy** and the WAL file.
3630361456Scy**
3631361456Scy** If F is the name of an sqlite database file, journal file, or WAL file
3632361456Scy** passed by the SQLite core into the VFS, then sqlite3_filename_database(F)
3633361456Scy** returns the name of the corresponding database file.
3634361456Scy**
3635361456Scy** If F is the name of an sqlite database file, journal file, or WAL file
3636361456Scy** passed by the SQLite core into the VFS, or if F is a database filename
3637361456Scy** obtained from [sqlite3_db_filename()], then sqlite3_filename_journal(F)
3638361456Scy** returns the name of the corresponding rollback journal file.
3639361456Scy**
3640361456Scy** If F is the name of an sqlite database file, journal file, or WAL file
3641361456Scy** that was passed by the SQLite core into the VFS, or if F is a database
3642361456Scy** filename obtained from [sqlite3_db_filename()], then
3643361456Scy** sqlite3_filename_wal(F) returns the name of the corresponding
3644361456Scy** WAL file.
3645361456Scy**
3646361456Scy** In all of the above, if F is not the name of a database, journal or WAL
3647361456Scy** filename passed into the VFS from the SQLite core and F is not the
3648361456Scy** return value from [sqlite3_db_filename()], then the result is
3649361456Scy** undefined and is likely a memory access violation.
3650361456Scy*/
3651361456ScySQLITE_API const char *sqlite3_filename_database(const char*);
3652361456ScySQLITE_API const char *sqlite3_filename_journal(const char*);
3653361456ScySQLITE_API const char *sqlite3_filename_wal(const char*);
3654251883Speter
3655362190Scy/*
3656362190Scy** CAPI3REF:  Database File Corresponding To A Journal
3657362190Scy**
3658362190Scy** ^If X is the name of a rollback or WAL-mode journal file that is
3659366076Scy** passed into the xOpen method of [sqlite3_vfs], then
3660362190Scy** sqlite3_database_file_object(X) returns a pointer to the [sqlite3_file]
3661362190Scy** object that represents the main database file.
3662362190Scy**
3663362190Scy** This routine is intended for use in custom [VFS] implementations
3664362190Scy** only.  It is not a general-purpose interface.
3665362190Scy** The argument sqlite3_file_object(X) must be a filename pointer that
3666366076Scy** has been passed into [sqlite3_vfs].xOpen method where the
3667362190Scy** flags parameter to xOpen contains one of the bits
3668362190Scy** [SQLITE_OPEN_MAIN_JOURNAL] or [SQLITE_OPEN_WAL].  Any other use
3669362190Scy** of this routine results in undefined and probably undesirable
3670362190Scy** behavior.
3671362190Scy*/
3672362190ScySQLITE_API sqlite3_file *sqlite3_database_file_object(const char*);
3673361456Scy
3674251883Speter/*
3675362190Scy** CAPI3REF: Create and Destroy VFS Filenames
3676362190Scy**
3677362190Scy** These interfces are provided for use by [VFS shim] implementations and
3678362190Scy** are not useful outside of that context.
3679362190Scy**
3680362190Scy** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of
3681362190Scy** database filename D with corresponding journal file J and WAL file W and
3682362190Scy** with N URI parameters key/values pairs in the array P.  The result from
3683362190Scy** sqlite3_create_filename(D,J,W,N,P) is a pointer to a database filename that
3684362190Scy** is safe to pass to routines like:
3685362190Scy** <ul>
3686362190Scy** <li> [sqlite3_uri_parameter()],
3687362190Scy** <li> [sqlite3_uri_boolean()],
3688362190Scy** <li> [sqlite3_uri_int64()],
3689366076Scy** <li> [sqlite3_uri_key()],
3690362190Scy** <li> [sqlite3_filename_database()],
3691362190Scy** <li> [sqlite3_filename_journal()], or
3692362190Scy** <li> [sqlite3_filename_wal()].
3693362190Scy** </ul>
3694362190Scy** If a memory allocation error occurs, sqlite3_create_filename() might
3695362190Scy** return a NULL pointer.  The memory obtained from sqlite3_create_filename(X)
3696362190Scy** must be released by a corresponding call to sqlite3_free_filename(Y).
3697362190Scy**
3698362190Scy** The P parameter in sqlite3_create_filename(D,J,W,N,P) should be an array
3699362190Scy** of 2*N pointers to strings.  Each pair of pointers in this array corresponds
3700362190Scy** to a key and value for a query parameter.  The P parameter may be a NULL
3701362190Scy** pointer if N is zero.  None of the 2*N pointers in the P array may be
3702362190Scy** NULL pointers and key pointers should not be empty strings.
3703362190Scy** None of the D, J, or W parameters to sqlite3_create_filename(D,J,W,N,P) may
3704362190Scy** be NULL pointers, though they can be empty strings.
3705362190Scy**
3706362190Scy** The sqlite3_free_filename(Y) routine releases a memory allocation
3707362190Scy** previously obtained from sqlite3_create_filename().  Invoking
3708362190Scy** sqlite3_free_filename(Y) where Y is a NULL pointer is a harmless no-op.
3709362190Scy**
3710362190Scy** If the Y parameter to sqlite3_free_filename(Y) is anything other
3711362190Scy** than a NULL pointer or a pointer previously acquired from
3712362190Scy** sqlite3_create_filename(), then bad things such as heap
3713369950Scy** corruption or segfaults may occur. The value Y should not be
3714362190Scy** used again after sqlite3_free_filename(Y) has been called.  This means
3715362190Scy** that if the [sqlite3_vfs.xOpen()] method of a VFS has been called using Y,
3716362190Scy** then the corresponding [sqlite3_module.xClose() method should also be
3717362190Scy** invoked prior to calling sqlite3_free_filename(Y).
3718362190Scy*/
3719362190ScySQLITE_API char *sqlite3_create_filename(
3720362190Scy  const char *zDatabase,
3721362190Scy  const char *zJournal,
3722362190Scy  const char *zWal,
3723362190Scy  int nParam,
3724362190Scy  const char **azParam
3725362190Scy);
3726362190ScySQLITE_API void sqlite3_free_filename(char*);
3727362190Scy
3728362190Scy/*
3729251883Speter** CAPI3REF: Error Codes And Messages
3730286510Speter** METHOD: sqlite3
3731251883Speter**
3732366076Scy** ^If the most recent sqlite3_* API call associated with
3733282328Sbapt** [database connection] D failed, then the sqlite3_errcode(D) interface
3734282328Sbapt** returns the numeric [result code] or [extended result code] for that
3735282328Sbapt** API call.
3736282328Sbapt** ^The sqlite3_extended_errcode()
3737366076Scy** interface is the same except that it always returns the
3738251883Speter** [extended result code] even when extended result codes are
3739251883Speter** disabled.
3740251883Speter**
3741342292Scy** The values returned by sqlite3_errcode() and/or
3742342292Scy** sqlite3_extended_errcode() might change with each API call.
3743342292Scy** Except, there are some interfaces that are guaranteed to never
3744342292Scy** change the value of the error code.  The error-code preserving
3745342292Scy** interfaces are:
3746342292Scy**
3747342292Scy** <ul>
3748342292Scy** <li> sqlite3_errcode()
3749342292Scy** <li> sqlite3_extended_errcode()
3750342292Scy** <li> sqlite3_errmsg()
3751342292Scy** <li> sqlite3_errmsg16()
3752342292Scy** </ul>
3753342292Scy**
3754251883Speter** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
3755251883Speter** text that describes the error, as either UTF-8 or UTF-16 respectively.
3756251883Speter** ^(Memory to hold the error message string is managed internally.
3757251883Speter** The application does not need to worry about freeing the result.
3758251883Speter** However, the error string might be overwritten or deallocated by
3759251883Speter** subsequent calls to other SQLite interface functions.)^
3760251883Speter**
3761251883Speter** ^The sqlite3_errstr() interface returns the English-language text
3762251883Speter** that describes the [result code], as UTF-8.
3763251883Speter** ^(Memory to hold the error message string is managed internally
3764251883Speter** and must not be freed by the application)^.
3765251883Speter**
3766251883Speter** When the serialized [threading mode] is in use, it might be the
3767251883Speter** case that a second error occurs on a separate thread in between
3768251883Speter** the time of the first error and the call to these interfaces.
3769251883Speter** When that happens, the second error will be reported since these
3770251883Speter** interfaces always report the most recent result.  To avoid
3771251883Speter** this, each thread can obtain exclusive use of the [database connection] D
3772251883Speter** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
3773251883Speter** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
3774251883Speter** all calls to the interfaces listed here are completed.
3775251883Speter**
3776251883Speter** If an interface fails with SQLITE_MISUSE, that means the interface
3777251883Speter** was invoked incorrectly by the application.  In that case, the
3778251883Speter** error code and message may or may not be set.
3779251883Speter*/
3780322444SpeterSQLITE_API int sqlite3_errcode(sqlite3 *db);
3781322444SpeterSQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
3782322444SpeterSQLITE_API const char *sqlite3_errmsg(sqlite3*);
3783322444SpeterSQLITE_API const void *sqlite3_errmsg16(sqlite3*);
3784322444SpeterSQLITE_API const char *sqlite3_errstr(int);
3785251883Speter
3786251883Speter/*
3787286510Speter** CAPI3REF: Prepared Statement Object
3788251883Speter** KEYWORDS: {prepared statement} {prepared statements}
3789251883Speter**
3790286510Speter** An instance of this object represents a single SQL statement that
3791286510Speter** has been compiled into binary form and is ready to be evaluated.
3792251883Speter**
3793286510Speter** Think of each SQL statement as a separate computer program.  The
3794366076Scy** original SQL text is source code.  A prepared statement object
3795286510Speter** is the compiled object code.  All SQL must be converted into a
3796286510Speter** prepared statement before it can be run.
3797251883Speter**
3798286510Speter** The life-cycle of a prepared statement object usually goes like this:
3799286510Speter**
3800251883Speter** <ol>
3801286510Speter** <li> Create the prepared statement object using [sqlite3_prepare_v2()].
3802286510Speter** <li> Bind values to [parameters] using the sqlite3_bind_*()
3803251883Speter**      interfaces.
3804251883Speter** <li> Run the SQL by calling [sqlite3_step()] one or more times.
3805286510Speter** <li> Reset the prepared statement using [sqlite3_reset()] then go back
3806251883Speter**      to step 2.  Do this zero or more times.
3807251883Speter** <li> Destroy the object using [sqlite3_finalize()].
3808251883Speter** </ol>
3809251883Speter*/
3810251883Spetertypedef struct sqlite3_stmt sqlite3_stmt;
3811251883Speter
3812251883Speter/*
3813251883Speter** CAPI3REF: Run-time Limits
3814286510Speter** METHOD: sqlite3
3815251883Speter**
3816251883Speter** ^(This interface allows the size of various constructs to be limited
3817251883Speter** on a connection by connection basis.  The first parameter is the
3818251883Speter** [database connection] whose limit is to be set or queried.  The
3819251883Speter** second parameter is one of the [limit categories] that define a
3820251883Speter** class of constructs to be size limited.  The third parameter is the
3821251883Speter** new limit for that construct.)^
3822251883Speter**
3823251883Speter** ^If the new limit is a negative number, the limit is unchanged.
3824366076Scy** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
3825251883Speter** [limits | hard upper bound]
3826251883Speter** set at compile-time by a C preprocessor macro called
3827251883Speter** [limits | SQLITE_MAX_<i>NAME</i>].
3828251883Speter** (The "_LIMIT_" in the name is changed to "_MAX_".))^
3829251883Speter** ^Attempts to increase a limit above its hard upper bound are
3830251883Speter** silently truncated to the hard upper bound.
3831251883Speter**
3832366076Scy** ^Regardless of whether or not the limit was changed, the
3833251883Speter** [sqlite3_limit()] interface returns the prior value of the limit.
3834251883Speter** ^Hence, to find the current value of a limit without changing it,
3835251883Speter** simply invoke this interface with the third parameter set to -1.
3836251883Speter**
3837251883Speter** Run-time limits are intended for use in applications that manage
3838251883Speter** both their own internal database and also databases that are controlled
3839251883Speter** by untrusted external sources.  An example application might be a
3840251883Speter** web browser that has its own databases for storing history and
3841251883Speter** separate databases controlled by JavaScript applications downloaded
3842251883Speter** off the Internet.  The internal databases can be given the
3843251883Speter** large, default limits.  Databases managed by external sources can
3844251883Speter** be given much smaller limits designed to prevent a denial of service
3845251883Speter** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
3846251883Speter** interface to further control untrusted SQL.  The size of the database
3847251883Speter** created by an untrusted script can be contained using the
3848251883Speter** [max_page_count] [PRAGMA].
3849251883Speter**
3850251883Speter** New run-time limit categories may be added in future releases.
3851251883Speter*/
3852322444SpeterSQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
3853251883Speter
3854251883Speter/*
3855251883Speter** CAPI3REF: Run-Time Limit Categories
3856251883Speter** KEYWORDS: {limit category} {*limit categories}
3857251883Speter**
3858251883Speter** These constants define various performance limits
3859251883Speter** that can be lowered at run-time using [sqlite3_limit()].
3860251883Speter** The synopsis of the meanings of the various limits is shown below.
3861251883Speter** Additional information is available at [limits | Limits in SQLite].
3862251883Speter**
3863251883Speter** <dl>
3864251883Speter** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
3865251883Speter** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
3866251883Speter**
3867251883Speter** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
3868251883Speter** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
3869251883Speter**
3870251883Speter** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
3871251883Speter** <dd>The maximum number of columns in a table definition or in the
3872251883Speter** result set of a [SELECT] or the maximum number of columns in an index
3873251883Speter** or in an ORDER BY or GROUP BY clause.</dd>)^
3874251883Speter**
3875251883Speter** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
3876251883Speter** <dd>The maximum depth of the parse tree on any expression.</dd>)^
3877251883Speter**
3878251883Speter** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
3879251883Speter** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
3880251883Speter**
3881251883Speter** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
3882251883Speter** <dd>The maximum number of instructions in a virtual machine program
3883322444Speter** used to implement an SQL statement.  If [sqlite3_prepare_v2()] or
3884322444Speter** the equivalent tries to allocate space for more than this many opcodes
3885322444Speter** in a single prepared statement, an SQLITE_NOMEM error is returned.</dd>)^
3886251883Speter**
3887251883Speter** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
3888251883Speter** <dd>The maximum number of arguments on a function.</dd>)^
3889251883Speter**
3890251883Speter** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
3891251883Speter** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
3892251883Speter**
3893251883Speter** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
3894251883Speter** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
3895251883Speter** <dd>The maximum length of the pattern argument to the [LIKE] or
3896251883Speter** [GLOB] operators.</dd>)^
3897251883Speter**
3898251883Speter** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
3899251883Speter** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
3900251883Speter** <dd>The maximum index number of any [parameter] in an SQL statement.)^
3901251883Speter**
3902251883Speter** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
3903251883Speter** <dd>The maximum depth of recursion for triggers.</dd>)^
3904274884Sbapt**
3905274884Sbapt** [[SQLITE_LIMIT_WORKER_THREADS]] ^(<dt>SQLITE_LIMIT_WORKER_THREADS</dt>
3906274884Sbapt** <dd>The maximum number of auxiliary worker threads that a single
3907274884Sbapt** [prepared statement] may start.</dd>)^
3908251883Speter** </dl>
3909251883Speter*/
3910251883Speter#define SQLITE_LIMIT_LENGTH                    0
3911251883Speter#define SQLITE_LIMIT_SQL_LENGTH                1
3912251883Speter#define SQLITE_LIMIT_COLUMN                    2
3913251883Speter#define SQLITE_LIMIT_EXPR_DEPTH                3
3914251883Speter#define SQLITE_LIMIT_COMPOUND_SELECT           4
3915251883Speter#define SQLITE_LIMIT_VDBE_OP                   5
3916251883Speter#define SQLITE_LIMIT_FUNCTION_ARG              6
3917251883Speter#define SQLITE_LIMIT_ATTACHED                  7
3918251883Speter#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
3919251883Speter#define SQLITE_LIMIT_VARIABLE_NUMBER           9
3920251883Speter#define SQLITE_LIMIT_TRIGGER_DEPTH            10
3921274884Sbapt#define SQLITE_LIMIT_WORKER_THREADS           11
3922251883Speter
3923251883Speter/*
3924322444Speter** CAPI3REF: Prepare Flags
3925322444Speter**
3926322444Speter** These constants define various flags that can be passed into
3927322444Speter** "prepFlags" parameter of the [sqlite3_prepare_v3()] and
3928322444Speter** [sqlite3_prepare16_v3()] interfaces.
3929322444Speter**
3930322444Speter** New flags may be added in future releases of SQLite.
3931322444Speter**
3932322444Speter** <dl>
3933322444Speter** [[SQLITE_PREPARE_PERSISTENT]] ^(<dt>SQLITE_PREPARE_PERSISTENT</dt>
3934322444Speter** <dd>The SQLITE_PREPARE_PERSISTENT flag is a hint to the query planner
3935322444Speter** that the prepared statement will be retained for a long time and
3936322444Speter** probably reused many times.)^ ^Without this flag, [sqlite3_prepare_v3()]
3937366076Scy** and [sqlite3_prepare16_v3()] assume that the prepared statement will
3938322444Speter** be used just once or at most a few times and then destroyed using
3939322444Speter** [sqlite3_finalize()] relatively soon. The current implementation acts
3940322444Speter** on this hint by avoiding the use of [lookaside memory] so as not to
3941322444Speter** deplete the limited store of lookaside memory. Future versions of
3942322444Speter** SQLite may act on this hint differently.
3943342292Scy**
3944346442Scy** [[SQLITE_PREPARE_NORMALIZE]] <dt>SQLITE_PREPARE_NORMALIZE</dt>
3945346442Scy** <dd>The SQLITE_PREPARE_NORMALIZE flag is a no-op. This flag used
3946346442Scy** to be required for any prepared statement that wanted to use the
3947346442Scy** [sqlite3_normalized_sql()] interface.  However, the
3948346442Scy** [sqlite3_normalized_sql()] interface is now available to all
3949346442Scy** prepared statements, regardless of whether or not they use this
3950346442Scy** flag.
3951346442Scy**
3952346442Scy** [[SQLITE_PREPARE_NO_VTAB]] <dt>SQLITE_PREPARE_NO_VTAB</dt>
3953346442Scy** <dd>The SQLITE_PREPARE_NO_VTAB flag causes the SQL compiler
3954346442Scy** to return an error (error code SQLITE_ERROR) if the statement uses
3955346442Scy** any virtual tables.
3956322444Speter** </dl>
3957322444Speter*/
3958322444Speter#define SQLITE_PREPARE_PERSISTENT              0x01
3959342292Scy#define SQLITE_PREPARE_NORMALIZE               0x02
3960346442Scy#define SQLITE_PREPARE_NO_VTAB                 0x04
3961322444Speter
3962322444Speter/*
3963251883Speter** CAPI3REF: Compiling An SQL Statement
3964251883Speter** KEYWORDS: {SQL statement compiler}
3965286510Speter** METHOD: sqlite3
3966286510Speter** CONSTRUCTOR: sqlite3_stmt
3967251883Speter**
3968322444Speter** To execute an SQL statement, it must first be compiled into a byte-code
3969322444Speter** program using one of these routines.  Or, in other words, these routines
3970322444Speter** are constructors for the [prepared statement] object.
3971251883Speter**
3972322444Speter** The preferred routine to use is [sqlite3_prepare_v2()].  The
3973322444Speter** [sqlite3_prepare()] interface is legacy and should be avoided.
3974322444Speter** [sqlite3_prepare_v3()] has an extra "prepFlags" option that is used
3975322444Speter** for special purposes.
3976322444Speter**
3977322444Speter** The use of the UTF-8 interfaces is preferred, as SQLite currently
3978322444Speter** does all parsing using UTF-8.  The UTF-16 interfaces are provided
3979322444Speter** as a convenience.  The UTF-16 interfaces work by converting the
3980322444Speter** input text into UTF-8, then invoking the corresponding UTF-8 interface.
3981322444Speter**
3982251883Speter** The first argument, "db", is a [database connection] obtained from a
3983251883Speter** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
3984251883Speter** [sqlite3_open16()].  The database connection must not have been closed.
3985251883Speter**
3986251883Speter** The second argument, "zSql", is the statement to be compiled, encoded
3987322444Speter** as either UTF-8 or UTF-16.  The sqlite3_prepare(), sqlite3_prepare_v2(),
3988322444Speter** and sqlite3_prepare_v3()
3989322444Speter** interfaces use UTF-8, and sqlite3_prepare16(), sqlite3_prepare16_v2(),
3990322444Speter** and sqlite3_prepare16_v3() use UTF-16.
3991251883Speter**
3992282328Sbapt** ^If the nByte argument is negative, then zSql is read up to the
3993282328Sbapt** first zero terminator. ^If nByte is positive, then it is the
3994282328Sbapt** number of bytes read from zSql.  ^If nByte is zero, then no prepared
3995282328Sbapt** statement is generated.
3996282328Sbapt** If the caller knows that the supplied string is nul-terminated, then
3997282328Sbapt** there is a small performance advantage to passing an nByte parameter that
3998282328Sbapt** is the number of bytes in the input string <i>including</i>
3999282328Sbapt** the nul-terminator.
4000251883Speter**
4001251883Speter** ^If pzTail is not NULL then *pzTail is made to point to the first byte
4002251883Speter** past the end of the first SQL statement in zSql.  These routines only
4003251883Speter** compile the first statement in zSql, so *pzTail is left pointing to
4004251883Speter** what remains uncompiled.
4005251883Speter**
4006251883Speter** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
4007251883Speter** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
4008251883Speter** to NULL.  ^If the input text contains no SQL (if the input is an empty
4009251883Speter** string or a comment) then *ppStmt is set to NULL.
4010251883Speter** The calling procedure is responsible for deleting the compiled
4011251883Speter** SQL statement using [sqlite3_finalize()] after it has finished with it.
4012251883Speter** ppStmt may not be NULL.
4013251883Speter**
4014251883Speter** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
4015251883Speter** otherwise an [error code] is returned.
4016251883Speter**
4017322444Speter** The sqlite3_prepare_v2(), sqlite3_prepare_v3(), sqlite3_prepare16_v2(),
4018322444Speter** and sqlite3_prepare16_v3() interfaces are recommended for all new programs.
4019322444Speter** The older interfaces (sqlite3_prepare() and sqlite3_prepare16())
4020322444Speter** are retained for backwards compatibility, but their use is discouraged.
4021322444Speter** ^In the "vX" interfaces, the prepared statement
4022251883Speter** that is returned (the [sqlite3_stmt] object) contains a copy of the
4023251883Speter** original SQL text. This causes the [sqlite3_step()] interface to
4024251883Speter** behave differently in three ways:
4025251883Speter**
4026251883Speter** <ol>
4027251883Speter** <li>
4028251883Speter** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
4029251883Speter** always used to do, [sqlite3_step()] will automatically recompile the SQL
4030251883Speter** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
4031251883Speter** retries will occur before sqlite3_step() gives up and returns an error.
4032251883Speter** </li>
4033251883Speter**
4034251883Speter** <li>
4035251883Speter** ^When an error occurs, [sqlite3_step()] will return one of the detailed
4036251883Speter** [error codes] or [extended error codes].  ^The legacy behavior was that
4037251883Speter** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
4038251883Speter** and the application would have to make a second call to [sqlite3_reset()]
4039251883Speter** in order to find the underlying cause of the problem. With the "v2" prepare
4040251883Speter** interfaces, the underlying reason for the error is returned immediately.
4041251883Speter** </li>
4042251883Speter**
4043251883Speter** <li>
4044366076Scy** ^If the specific value bound to a [parameter | host parameter] in the
4045251883Speter** WHERE clause might influence the choice of query plan for a statement,
4046366076Scy** then the statement will be automatically recompiled, as if there had been
4047361456Scy** a schema change, on the first [sqlite3_step()] call following any change
4048366076Scy** to the [sqlite3_bind_text | bindings] of that [parameter].
4049366076Scy** ^The specific value of a WHERE-clause [parameter] might influence the
4050251883Speter** choice of query plan if the parameter is the left-hand side of a [LIKE]
4051251883Speter** or [GLOB] operator or if the parameter is compared to an indexed column
4052355326Scy** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
4053251883Speter** </li>
4054342292Scy** </ol>
4055322444Speter**
4056322444Speter** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
4057322444Speter** the extra prepFlags parameter, which is a bit array consisting of zero or
4058322444Speter** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
4059322444Speter** sqlite3_prepare_v2() interface works exactly the same as
4060322444Speter** sqlite3_prepare_v3() with a zero prepFlags parameter.
4061251883Speter*/
4062322444SpeterSQLITE_API int sqlite3_prepare(
4063251883Speter  sqlite3 *db,            /* Database handle */
4064251883Speter  const char *zSql,       /* SQL statement, UTF-8 encoded */
4065251883Speter  int nByte,              /* Maximum length of zSql in bytes. */
4066251883Speter  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
4067251883Speter  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
4068251883Speter);
4069322444SpeterSQLITE_API int sqlite3_prepare_v2(
4070251883Speter  sqlite3 *db,            /* Database handle */
4071251883Speter  const char *zSql,       /* SQL statement, UTF-8 encoded */
4072251883Speter  int nByte,              /* Maximum length of zSql in bytes. */
4073251883Speter  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
4074251883Speter  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
4075251883Speter);
4076322444SpeterSQLITE_API int sqlite3_prepare_v3(
4077251883Speter  sqlite3 *db,            /* Database handle */
4078322444Speter  const char *zSql,       /* SQL statement, UTF-8 encoded */
4079322444Speter  int nByte,              /* Maximum length of zSql in bytes. */
4080322444Speter  unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4081322444Speter  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
4082322444Speter  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
4083322444Speter);
4084322444SpeterSQLITE_API int sqlite3_prepare16(
4085322444Speter  sqlite3 *db,            /* Database handle */
4086251883Speter  const void *zSql,       /* SQL statement, UTF-16 encoded */
4087251883Speter  int nByte,              /* Maximum length of zSql in bytes. */
4088251883Speter  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
4089251883Speter  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
4090251883Speter);
4091322444SpeterSQLITE_API int sqlite3_prepare16_v2(
4092251883Speter  sqlite3 *db,            /* Database handle */
4093251883Speter  const void *zSql,       /* SQL statement, UTF-16 encoded */
4094251883Speter  int nByte,              /* Maximum length of zSql in bytes. */
4095251883Speter  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
4096251883Speter  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
4097251883Speter);
4098322444SpeterSQLITE_API int sqlite3_prepare16_v3(
4099322444Speter  sqlite3 *db,            /* Database handle */
4100322444Speter  const void *zSql,       /* SQL statement, UTF-16 encoded */
4101322444Speter  int nByte,              /* Maximum length of zSql in bytes. */
4102322444Speter  unsigned int prepFlags, /* Zero or more SQLITE_PREPARE_ flags */
4103322444Speter  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
4104322444Speter  const void **pzTail     /* OUT: Pointer to unused portion of zSql */
4105322444Speter);
4106251883Speter
4107251883Speter/*
4108251883Speter** CAPI3REF: Retrieving Statement SQL
4109286510Speter** METHOD: sqlite3_stmt
4110251883Speter**
4111305002Scy** ^The sqlite3_sql(P) interface returns a pointer to a copy of the UTF-8
4112305002Scy** SQL text used to create [prepared statement] P if P was
4113322444Speter** created by [sqlite3_prepare_v2()], [sqlite3_prepare_v3()],
4114322444Speter** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4115305002Scy** ^The sqlite3_expanded_sql(P) interface returns a pointer to a UTF-8
4116305002Scy** string containing the SQL text of prepared statement P with
4117305002Scy** [bound parameters] expanded.
4118342292Scy** ^The sqlite3_normalized_sql(P) interface returns a pointer to a UTF-8
4119342292Scy** string containing the normalized SQL text of prepared statement P.  The
4120342292Scy** semantics used to normalize a SQL statement are unspecified and subject
4121342292Scy** to change.  At a minimum, literal values will be replaced with suitable
4122342292Scy** placeholders.
4123305002Scy**
4124305002Scy** ^(For example, if a prepared statement is created using the SQL
4125305002Scy** text "SELECT $abc,:xyz" and if parameter $abc is bound to integer 2345
4126305002Scy** and parameter :xyz is unbound, then sqlite3_sql() will return
4127305002Scy** the original string, "SELECT $abc,:xyz" but sqlite3_expanded_sql()
4128305002Scy** will return "SELECT 2345,NULL".)^
4129305002Scy**
4130305002Scy** ^The sqlite3_expanded_sql() interface returns NULL if insufficient memory
4131305002Scy** is available to hold the result, or if the result would exceed the
4132305002Scy** the maximum string length determined by the [SQLITE_LIMIT_LENGTH].
4133305002Scy**
4134305002Scy** ^The [SQLITE_TRACE_SIZE_LIMIT] compile-time option limits the size of
4135305002Scy** bound parameter expansions.  ^The [SQLITE_OMIT_TRACE] compile-time
4136305002Scy** option causes sqlite3_expanded_sql() to always return NULL.
4137305002Scy**
4138342292Scy** ^The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
4139342292Scy** are managed by SQLite and are automatically freed when the prepared
4140342292Scy** statement is finalized.
4141305002Scy** ^The string returned by sqlite3_expanded_sql(P), on the other hand,
4142305002Scy** is obtained from [sqlite3_malloc()] and must be free by the application
4143305002Scy** by passing it to [sqlite3_free()].
4144251883Speter*/
4145322444SpeterSQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
4146322444SpeterSQLITE_API char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
4147342292ScySQLITE_API const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
4148251883Speter
4149251883Speter/*
4150251883Speter** CAPI3REF: Determine If An SQL Statement Writes The Database
4151286510Speter** METHOD: sqlite3_stmt
4152251883Speter**
4153251883Speter** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
4154251883Speter** and only if the [prepared statement] X makes no direct changes to
4155251883Speter** the content of the database file.
4156251883Speter**
4157251883Speter** Note that [application-defined SQL functions] or
4158366076Scy** [virtual tables] might change the database indirectly as a side effect.
4159366076Scy** ^(For example, if an application defines a function "eval()" that
4160251883Speter** calls [sqlite3_exec()], then the following SQL statement would
4161251883Speter** change the database file through side-effects:
4162251883Speter**
4163251883Speter** <blockquote><pre>
4164251883Speter**    SELECT eval('DELETE FROM t1') FROM t2;
4165251883Speter** </pre></blockquote>
4166251883Speter**
4167251883Speter** But because the [SELECT] statement does not change the database file
4168251883Speter** directly, sqlite3_stmt_readonly() would still return true.)^
4169251883Speter**
4170251883Speter** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
4171251883Speter** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
4172251883Speter** since the statements themselves do not actually modify the database but
4173366076Scy** rather they control the timing of when other statements modify the
4174251883Speter** database.  ^The [ATTACH] and [DETACH] statements also cause
4175251883Speter** sqlite3_stmt_readonly() to return true since, while those statements
4176366076Scy** change the configuration of a database connection, they do not make
4177251883Speter** changes to the content of the database files on disk.
4178322444Speter** ^The sqlite3_stmt_readonly() interface returns true for [BEGIN] since
4179322444Speter** [BEGIN] merely sets internal flags, but the [BEGIN|BEGIN IMMEDIATE] and
4180322444Speter** [BEGIN|BEGIN EXCLUSIVE] commands do touch the database and so
4181322444Speter** sqlite3_stmt_readonly() returns false for those commands.
4182251883Speter*/
4183322444SpeterSQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4184251883Speter
4185251883Speter/*
4186347347Scy** CAPI3REF: Query The EXPLAIN Setting For A Prepared Statement
4187347347Scy** METHOD: sqlite3_stmt
4188347347Scy**
4189347347Scy** ^The sqlite3_stmt_isexplain(S) interface returns 1 if the
4190347347Scy** prepared statement S is an EXPLAIN statement, or 2 if the
4191347347Scy** statement S is an EXPLAIN QUERY PLAN.
4192347347Scy** ^The sqlite3_stmt_isexplain(S) interface returns 0 if S is
4193347347Scy** an ordinary statement or a NULL pointer.
4194347347Scy*/
4195347347ScySQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
4196347347Scy
4197347347Scy/*
4198251883Speter** CAPI3REF: Determine If A Prepared Statement Has Been Reset
4199286510Speter** METHOD: sqlite3_stmt
4200251883Speter**
4201251883Speter** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
4202366076Scy** [prepared statement] S has been stepped at least once using
4203298161Sbapt** [sqlite3_step(S)] but has neither run to completion (returned
4204298161Sbapt** [SQLITE_DONE] from [sqlite3_step(S)]) nor
4205251883Speter** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
4206366076Scy** interface returns false if S is a NULL pointer.  If S is not a
4207251883Speter** NULL pointer and is not a pointer to a valid [prepared statement]
4208251883Speter** object, then the behavior is undefined and probably undesirable.
4209251883Speter**
4210251883Speter** This interface can be used in combination [sqlite3_next_stmt()]
4211366076Scy** to locate all prepared statements associated with a database
4212251883Speter** connection that are in need of being reset.  This can be used,
4213366076Scy** for example, in diagnostic routines to search for prepared
4214251883Speter** statements that are holding a transaction open.
4215251883Speter*/
4216322444SpeterSQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
4217251883Speter
4218251883Speter/*
4219251883Speter** CAPI3REF: Dynamically Typed Value Object
4220251883Speter** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
4221251883Speter**
4222251883Speter** SQLite uses the sqlite3_value object to represent all values
4223251883Speter** that can be stored in a database table. SQLite uses dynamic typing
4224251883Speter** for the values it stores.  ^Values stored in sqlite3_value objects
4225251883Speter** can be integers, floating point values, strings, BLOBs, or NULL.
4226251883Speter**
4227251883Speter** An sqlite3_value object may be either "protected" or "unprotected".
4228251883Speter** Some interfaces require a protected sqlite3_value.  Other interfaces
4229251883Speter** will accept either a protected or an unprotected sqlite3_value.
4230251883Speter** Every interface that accepts sqlite3_value arguments specifies
4231286510Speter** whether or not it requires a protected sqlite3_value.  The
4232366076Scy** [sqlite3_value_dup()] interface can be used to construct a new
4233286510Speter** protected sqlite3_value from an unprotected sqlite3_value.
4234251883Speter**
4235251883Speter** The terms "protected" and "unprotected" refer to whether or not
4236251883Speter** a mutex is held.  An internal mutex is held for a protected
4237251883Speter** sqlite3_value object but no mutex is held for an unprotected
4238251883Speter** sqlite3_value object.  If SQLite is compiled to be single-threaded
4239251883Speter** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
4240366076Scy** or if SQLite is run in one of reduced mutex modes
4241251883Speter** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
4242251883Speter** then there is no distinction between protected and unprotected
4243251883Speter** sqlite3_value objects and they can be used interchangeably.  However,
4244251883Speter** for maximum code portability it is recommended that applications
4245251883Speter** still make the distinction between protected and unprotected
4246251883Speter** sqlite3_value objects even when not strictly required.
4247251883Speter**
4248251883Speter** ^The sqlite3_value objects that are passed as parameters into the
4249251883Speter** implementation of [application-defined SQL functions] are protected.
4250251883Speter** ^The sqlite3_value object returned by
4251251883Speter** [sqlite3_column_value()] is unprotected.
4252342292Scy** Unprotected sqlite3_value objects may only be used as arguments
4253342292Scy** to [sqlite3_result_value()], [sqlite3_bind_value()], and
4254342292Scy** [sqlite3_value_dup()].
4255251883Speter** The [sqlite3_value_blob | sqlite3_value_type()] family of
4256251883Speter** interfaces require protected sqlite3_value objects.
4257251883Speter*/
4258322444Spetertypedef struct sqlite3_value sqlite3_value;
4259251883Speter
4260251883Speter/*
4261251883Speter** CAPI3REF: SQL Function Context Object
4262251883Speter**
4263251883Speter** The context in which an SQL function executes is stored in an
4264251883Speter** sqlite3_context object.  ^A pointer to an sqlite3_context object
4265251883Speter** is always first parameter to [application-defined SQL functions].
4266251883Speter** The application-defined SQL function implementation will pass this
4267251883Speter** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
4268251883Speter** [sqlite3_aggregate_context()], [sqlite3_user_data()],
4269251883Speter** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
4270251883Speter** and/or [sqlite3_set_auxdata()].
4271251883Speter*/
4272251883Spetertypedef struct sqlite3_context sqlite3_context;
4273251883Speter
4274251883Speter/*
4275251883Speter** CAPI3REF: Binding Values To Prepared Statements
4276251883Speter** KEYWORDS: {host parameter} {host parameters} {host parameter name}
4277251883Speter** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
4278286510Speter** METHOD: sqlite3_stmt
4279251883Speter**
4280251883Speter** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
4281251883Speter** literals may be replaced by a [parameter] that matches one of following
4282251883Speter** templates:
4283251883Speter**
4284251883Speter** <ul>
4285251883Speter** <li>  ?
4286251883Speter** <li>  ?NNN
4287251883Speter** <li>  :VVV
4288251883Speter** <li>  @VVV
4289251883Speter** <li>  $VVV
4290251883Speter** </ul>
4291251883Speter**
4292251883Speter** In the templates above, NNN represents an integer literal,
4293251883Speter** and VVV represents an alphanumeric identifier.)^  ^The values of these
4294251883Speter** parameters (also called "host parameter names" or "SQL parameters")
4295251883Speter** can be set using the sqlite3_bind_*() routines defined here.
4296251883Speter**
4297251883Speter** ^The first argument to the sqlite3_bind_*() routines is always
4298251883Speter** a pointer to the [sqlite3_stmt] object returned from
4299251883Speter** [sqlite3_prepare_v2()] or its variants.
4300251883Speter**
4301251883Speter** ^The second argument is the index of the SQL parameter to be set.
4302251883Speter** ^The leftmost SQL parameter has an index of 1.  ^When the same named
4303251883Speter** SQL parameter is used more than once, second and subsequent
4304251883Speter** occurrences have the same index as the first occurrence.
4305251883Speter** ^The index for named parameters can be looked up using the
4306251883Speter** [sqlite3_bind_parameter_index()] API if desired.  ^The index
4307251883Speter** for "?NNN" parameters is the value of NNN.
4308251883Speter** ^The NNN value must be between 1 and the [sqlite3_limit()]
4309362190Scy** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 32766).
4310251883Speter**
4311251883Speter** ^The third argument is the value to bind to the parameter.
4312251883Speter** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4313251883Speter** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
4314251883Speter** is ignored and the end result is the same as sqlite3_bind_null().
4315362190Scy** ^If the third parameter to sqlite3_bind_text() is not NULL, then
4316362190Scy** it should be a pointer to well-formed UTF8 text.
4317362190Scy** ^If the third parameter to sqlite3_bind_text16() is not NULL, then
4318362190Scy** it should be a pointer to well-formed UTF16 text.
4319362190Scy** ^If the third parameter to sqlite3_bind_text64() is not NULL, then
4320362190Scy** it should be a pointer to a well-formed unicode string that is
4321362190Scy** either UTF8 if the sixth parameter is SQLITE_UTF8, or UTF16
4322362190Scy** otherwise.
4323251883Speter**
4324362190Scy** [[byte-order determination rules]] ^The byte-order of
4325362190Scy** UTF16 input text is determined by the byte-order mark (BOM, U+FEFF)
4326362190Scy** found in first character, which is removed, or in the absence of a BOM
4327362190Scy** the byte order is the native byte order of the host
4328362190Scy** machine for sqlite3_bind_text16() or the byte order specified in
4329366076Scy** the 6th parameter for sqlite3_bind_text64().)^
4330362190Scy** ^If UTF16 input text contains invalid unicode
4331362190Scy** characters, then SQLite might change those invalid characters
4332362190Scy** into the unicode replacement character: U+FFFD.
4333362190Scy**
4334251883Speter** ^(In those routines that have a fourth argument, its value is the
4335251883Speter** number of bytes in the parameter.  To be clear: the value is the
4336251883Speter** number of <u>bytes</u> in the value, not the number of characters.)^
4337251883Speter** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
4338251883Speter** is negative, then the length of the string is
4339251883Speter** the number of bytes up to the first zero terminator.
4340251883Speter** If the fourth parameter to sqlite3_bind_blob() is negative, then
4341251883Speter** the behavior is undefined.
4342251883Speter** If a non-negative fourth parameter is provided to sqlite3_bind_text()
4343274884Sbapt** or sqlite3_bind_text16() or sqlite3_bind_text64() then
4344274884Sbapt** that parameter must be the byte offset
4345251883Speter** where the NUL terminator would occur assuming the string were NUL
4346366076Scy** terminated.  If any NUL characters occurs at byte offsets less than
4347251883Speter** the value of the fourth parameter then the resulting string value will
4348251883Speter** contain embedded NULs.  The result of expressions involving strings
4349251883Speter** with embedded NULs is undefined.
4350251883Speter**
4351274884Sbapt** ^The fifth argument to the BLOB and string binding interfaces
4352274884Sbapt** is a destructor used to dispose of the BLOB or
4353251883Speter** string after SQLite has finished with it.  ^The destructor is called
4354347347Scy** to dispose of the BLOB or string even if the call to the bind API fails,
4355347347Scy** except the destructor is not called if the third parameter is a NULL
4356347347Scy** pointer or the fourth parameter is negative.
4357251883Speter** ^If the fifth argument is
4358251883Speter** the special value [SQLITE_STATIC], then SQLite assumes that the
4359251883Speter** information is in static, unmanaged space and does not need to be freed.
4360251883Speter** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
4361251883Speter** SQLite makes its own private copy of the data immediately, before
4362251883Speter** the sqlite3_bind_*() routine returns.
4363251883Speter**
4364274884Sbapt** ^The sixth argument to sqlite3_bind_text64() must be one of
4365274884Sbapt** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
4366274884Sbapt** to specify the encoding of the text in the third parameter.  If
4367274884Sbapt** the sixth argument to sqlite3_bind_text64() is not one of the
4368274884Sbapt** allowed values shown above, or if the text encoding is different
4369274884Sbapt** from the encoding specified by the sixth parameter, then the behavior
4370274884Sbapt** is undefined.
4371274884Sbapt**
4372251883Speter** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
4373251883Speter** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
4374251883Speter** (just an integer to hold its size) while it is being processed.
4375251883Speter** Zeroblobs are intended to serve as placeholders for BLOBs whose
4376251883Speter** content is later written using
4377251883Speter** [sqlite3_blob_open | incremental BLOB I/O] routines.
4378251883Speter** ^A negative value for the zeroblob results in a zero-length BLOB.
4379251883Speter**
4380322444Speter** ^The sqlite3_bind_pointer(S,I,P,T,D) routine causes the I-th parameter in
4381322444Speter** [prepared statement] S to have an SQL value of NULL, but to also be
4382322444Speter** associated with the pointer P of type T.  ^D is either a NULL pointer or
4383322444Speter** a pointer to a destructor function for P. ^SQLite will invoke the
4384322444Speter** destructor D with a single argument of P when it is finished using
4385322444Speter** P.  The T parameter should be a static string, preferably a string
4386322444Speter** literal. The sqlite3_bind_pointer() routine is part of the
4387322444Speter** [pointer passing interface] added for SQLite 3.20.0.
4388322444Speter**
4389251883Speter** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
4390251883Speter** for the [prepared statement] or with a prepared statement for which
4391251883Speter** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
4392251883Speter** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
4393251883Speter** routine is passed a [prepared statement] that has been finalized, the
4394251883Speter** result is undefined and probably harmful.
4395251883Speter**
4396251883Speter** ^Bindings are not cleared by the [sqlite3_reset()] routine.
4397251883Speter** ^Unbound parameters are interpreted as NULL.
4398251883Speter**
4399251883Speter** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
4400251883Speter** [error code] if anything goes wrong.
4401274884Sbapt** ^[SQLITE_TOOBIG] might be returned if the size of a string or BLOB
4402274884Sbapt** exceeds limits imposed by [sqlite3_limit]([SQLITE_LIMIT_LENGTH]) or
4403274884Sbapt** [SQLITE_MAX_LENGTH].
4404251883Speter** ^[SQLITE_RANGE] is returned if the parameter
4405251883Speter** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
4406251883Speter**
4407251883Speter** See also: [sqlite3_bind_parameter_count()],
4408251883Speter** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
4409251883Speter*/
4410322444SpeterSQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
4411322444SpeterSQLITE_API int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
4412274884Sbapt                        void(*)(void*));
4413322444SpeterSQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
4414322444SpeterSQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
4415322444SpeterSQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
4416322444SpeterSQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
4417322444SpeterSQLITE_API int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
4418322444SpeterSQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
4419322444SpeterSQLITE_API int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
4420274884Sbapt                         void(*)(void*), unsigned char encoding);
4421322444SpeterSQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
4422322444SpeterSQLITE_API int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
4423322444SpeterSQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
4424322444SpeterSQLITE_API int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
4425251883Speter
4426251883Speter/*
4427251883Speter** CAPI3REF: Number Of SQL Parameters
4428286510Speter** METHOD: sqlite3_stmt
4429251883Speter**
4430251883Speter** ^This routine can be used to find the number of [SQL parameters]
4431251883Speter** in a [prepared statement].  SQL parameters are tokens of the
4432251883Speter** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
4433251883Speter** placeholders for values that are [sqlite3_bind_blob | bound]
4434251883Speter** to the parameters at a later time.
4435251883Speter**
4436251883Speter** ^(This routine actually returns the index of the largest (rightmost)
4437251883Speter** parameter. For all forms except ?NNN, this will correspond to the
4438251883Speter** number of unique parameters.  If parameters of the ?NNN form are used,
4439251883Speter** there may be gaps in the list.)^
4440251883Speter**
4441251883Speter** See also: [sqlite3_bind_blob|sqlite3_bind()],
4442251883Speter** [sqlite3_bind_parameter_name()], and
4443251883Speter** [sqlite3_bind_parameter_index()].
4444251883Speter*/
4445322444SpeterSQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
4446251883Speter
4447251883Speter/*
4448251883Speter** CAPI3REF: Name Of A Host Parameter
4449286510Speter** METHOD: sqlite3_stmt
4450251883Speter**
4451251883Speter** ^The sqlite3_bind_parameter_name(P,N) interface returns
4452251883Speter** the name of the N-th [SQL parameter] in the [prepared statement] P.
4453251883Speter** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
4454251883Speter** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
4455251883Speter** respectively.
4456251883Speter** In other words, the initial ":" or "$" or "@" or "?"
4457251883Speter** is included as part of the name.)^
4458251883Speter** ^Parameters of the form "?" without a following integer have no name
4459251883Speter** and are referred to as "nameless" or "anonymous parameters".
4460251883Speter**
4461251883Speter** ^The first host parameter has an index of 1, not 0.
4462251883Speter**
4463251883Speter** ^If the value N is out of range or if the N-th parameter is
4464251883Speter** nameless, then NULL is returned.  ^The returned string is
4465251883Speter** always in UTF-8 encoding even if the named parameter was
4466322444Speter** originally specified as UTF-16 in [sqlite3_prepare16()],
4467322444Speter** [sqlite3_prepare16_v2()], or [sqlite3_prepare16_v3()].
4468251883Speter**
4469251883Speter** See also: [sqlite3_bind_blob|sqlite3_bind()],
4470251883Speter** [sqlite3_bind_parameter_count()], and
4471251883Speter** [sqlite3_bind_parameter_index()].
4472251883Speter*/
4473322444SpeterSQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
4474251883Speter
4475251883Speter/*
4476251883Speter** CAPI3REF: Index Of A Parameter With A Given Name
4477286510Speter** METHOD: sqlite3_stmt
4478251883Speter**
4479251883Speter** ^Return the index of an SQL parameter given its name.  ^The
4480251883Speter** index value returned is suitable for use as the second
4481251883Speter** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
4482251883Speter** is returned if no matching parameter is found.  ^The parameter
4483251883Speter** name must be given in UTF-8 even if the original statement
4484322444Speter** was prepared from UTF-16 text using [sqlite3_prepare16_v2()] or
4485322444Speter** [sqlite3_prepare16_v3()].
4486251883Speter**
4487251883Speter** See also: [sqlite3_bind_blob|sqlite3_bind()],
4488251883Speter** [sqlite3_bind_parameter_count()], and
4489298161Sbapt** [sqlite3_bind_parameter_name()].
4490251883Speter*/
4491322444SpeterSQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
4492251883Speter
4493251883Speter/*
4494251883Speter** CAPI3REF: Reset All Bindings On A Prepared Statement
4495286510Speter** METHOD: sqlite3_stmt
4496251883Speter**
4497251883Speter** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
4498251883Speter** the [sqlite3_bind_blob | bindings] on a [prepared statement].
4499251883Speter** ^Use this routine to reset all host parameters to NULL.
4500251883Speter*/
4501322444SpeterSQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
4502251883Speter
4503251883Speter/*
4504251883Speter** CAPI3REF: Number Of Columns In A Result Set
4505286510Speter** METHOD: sqlite3_stmt
4506251883Speter**
4507251883Speter** ^Return the number of columns in the result set returned by the
4508366076Scy** [prepared statement]. ^If this routine returns 0, that means the
4509322444Speter** [prepared statement] returns no data (for example an [UPDATE]).
4510322444Speter** ^However, just because this routine returns a positive number does not
4511322444Speter** mean that one or more rows of data will be returned.  ^A SELECT statement
4512322444Speter** will always have a positive sqlite3_column_count() but depending on the
4513322444Speter** WHERE clause constraints and the table content, it might return no rows.
4514251883Speter**
4515251883Speter** See also: [sqlite3_data_count()]
4516251883Speter*/
4517322444SpeterSQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
4518251883Speter
4519251883Speter/*
4520251883Speter** CAPI3REF: Column Names In A Result Set
4521286510Speter** METHOD: sqlite3_stmt
4522251883Speter**
4523251883Speter** ^These routines return the name assigned to a particular column
4524251883Speter** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
4525251883Speter** interface returns a pointer to a zero-terminated UTF-8 string
4526251883Speter** and sqlite3_column_name16() returns a pointer to a zero-terminated
4527251883Speter** UTF-16 string.  ^The first parameter is the [prepared statement]
4528251883Speter** that implements the [SELECT] statement. ^The second parameter is the
4529251883Speter** column number.  ^The leftmost column is number 0.
4530251883Speter**
4531251883Speter** ^The returned string pointer is valid until either the [prepared statement]
4532251883Speter** is destroyed by [sqlite3_finalize()] or until the statement is automatically
4533251883Speter** reprepared by the first call to [sqlite3_step()] for a particular run
4534251883Speter** or until the next call to
4535251883Speter** sqlite3_column_name() or sqlite3_column_name16() on the same column.
4536251883Speter**
4537251883Speter** ^If sqlite3_malloc() fails during the processing of either routine
4538251883Speter** (for example during a conversion from UTF-8 to UTF-16) then a
4539251883Speter** NULL pointer is returned.
4540251883Speter**
4541251883Speter** ^The name of a result column is the value of the "AS" clause for
4542251883Speter** that column, if there is an AS clause.  If there is no AS clause
4543251883Speter** then the name of the column is unspecified and may change from
4544251883Speter** one release of SQLite to the next.
4545251883Speter*/
4546322444SpeterSQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
4547322444SpeterSQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
4548251883Speter
4549251883Speter/*
4550251883Speter** CAPI3REF: Source Of Data In A Query Result
4551286510Speter** METHOD: sqlite3_stmt
4552251883Speter**
4553251883Speter** ^These routines provide a means to determine the database, table, and
4554251883Speter** table column that is the origin of a particular result column in
4555251883Speter** [SELECT] statement.
4556251883Speter** ^The name of the database or table or column can be returned as
4557251883Speter** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
4558251883Speter** the database name, the _table_ routines return the table name, and
4559251883Speter** the origin_ routines return the column name.
4560251883Speter** ^The returned string is valid until the [prepared statement] is destroyed
4561251883Speter** using [sqlite3_finalize()] or until the statement is automatically
4562251883Speter** reprepared by the first call to [sqlite3_step()] for a particular run
4563251883Speter** or until the same information is requested
4564251883Speter** again in a different encoding.
4565251883Speter**
4566251883Speter** ^The names returned are the original un-aliased names of the
4567251883Speter** database, table, and column.
4568251883Speter**
4569251883Speter** ^The first argument to these interfaces is a [prepared statement].
4570251883Speter** ^These functions return information about the Nth result column returned by
4571251883Speter** the statement, where N is the second function argument.
4572251883Speter** ^The left-most column is column 0 for these routines.
4573251883Speter**
4574251883Speter** ^If the Nth column returned by the statement is an expression or
4575251883Speter** subquery and is not a column value, then all of these functions return
4576361456Scy** NULL.  ^These routines might also return NULL if a memory allocation error
4577251883Speter** occurs.  ^Otherwise, they return the name of the attached database, table,
4578251883Speter** or column that query result column was extracted from.
4579251883Speter**
4580251883Speter** ^As with all other SQLite APIs, those whose names end with "16" return
4581251883Speter** UTF-16 encoded strings and the other functions return UTF-8.
4582251883Speter**
4583251883Speter** ^These APIs are only available if the library was compiled with the
4584251883Speter** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
4585251883Speter**
4586251883Speter** If two or more threads call one or more
4587251883Speter** [sqlite3_column_database_name | column metadata interfaces]
4588251883Speter** for the same [prepared statement] and result column
4589251883Speter** at the same time then the results are undefined.
4590251883Speter*/
4591322444SpeterSQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
4592322444SpeterSQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
4593322444SpeterSQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
4594322444SpeterSQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
4595322444SpeterSQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
4596322444SpeterSQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
4597251883Speter
4598251883Speter/*
4599251883Speter** CAPI3REF: Declared Datatype Of A Query Result
4600286510Speter** METHOD: sqlite3_stmt
4601251883Speter**
4602251883Speter** ^(The first parameter is a [prepared statement].
4603251883Speter** If this statement is a [SELECT] statement and the Nth column of the
4604251883Speter** returned result set of that [SELECT] is a table column (not an
4605251883Speter** expression or subquery) then the declared type of the table
4606251883Speter** column is returned.)^  ^If the Nth column of the result set is an
4607251883Speter** expression or subquery, then a NULL pointer is returned.
4608251883Speter** ^The returned string is always UTF-8 encoded.
4609251883Speter**
4610251883Speter** ^(For example, given the database schema:
4611251883Speter**
4612251883Speter** CREATE TABLE t1(c1 VARIANT);
4613251883Speter**
4614251883Speter** and the following statement to be compiled:
4615251883Speter**
4616251883Speter** SELECT c1 + 1, c1 FROM t1;
4617251883Speter**
4618251883Speter** this routine would return the string "VARIANT" for the second result
4619251883Speter** column (i==1), and a NULL pointer for the first result column (i==0).)^
4620251883Speter**
4621251883Speter** ^SQLite uses dynamic run-time typing.  ^So just because a column
4622251883Speter** is declared to contain a particular type does not mean that the
4623251883Speter** data stored in that column is of the declared type.  SQLite is
4624251883Speter** strongly typed, but the typing is dynamic not static.  ^Type
4625251883Speter** is associated with individual values, not with the containers
4626251883Speter** used to hold those values.
4627251883Speter*/
4628322444SpeterSQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
4629322444SpeterSQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
4630251883Speter
4631251883Speter/*
4632251883Speter** CAPI3REF: Evaluate An SQL Statement
4633286510Speter** METHOD: sqlite3_stmt
4634251883Speter**
4635322444Speter** After a [prepared statement] has been prepared using any of
4636322444Speter** [sqlite3_prepare_v2()], [sqlite3_prepare_v3()], [sqlite3_prepare16_v2()],
4637322444Speter** or [sqlite3_prepare16_v3()] or one of the legacy
4638251883Speter** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
4639251883Speter** must be called one or more times to evaluate the statement.
4640251883Speter**
4641251883Speter** The details of the behavior of the sqlite3_step() interface depend
4642322444Speter** on whether the statement was prepared using the newer "vX" interfaces
4643322444Speter** [sqlite3_prepare_v3()], [sqlite3_prepare_v2()], [sqlite3_prepare16_v3()],
4644322444Speter** [sqlite3_prepare16_v2()] or the older legacy
4645322444Speter** interfaces [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
4646322444Speter** new "vX" interface is recommended for new applications but the legacy
4647251883Speter** interface will continue to be supported.
4648251883Speter**
4649251883Speter** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
4650251883Speter** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
4651251883Speter** ^With the "v2" interface, any of the other [result codes] or
4652251883Speter** [extended result codes] might be returned as well.
4653251883Speter**
4654251883Speter** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
4655251883Speter** database locks it needs to do its job.  ^If the statement is a [COMMIT]
4656251883Speter** or occurs outside of an explicit transaction, then you can retry the
4657251883Speter** statement.  If the statement is not a [COMMIT] and occurs within an
4658251883Speter** explicit transaction then you should rollback the transaction before
4659251883Speter** continuing.
4660251883Speter**
4661251883Speter** ^[SQLITE_DONE] means that the statement has finished executing
4662251883Speter** successfully.  sqlite3_step() should not be called again on this virtual
4663251883Speter** machine without first calling [sqlite3_reset()] to reset the virtual
4664251883Speter** machine back to its initial state.
4665251883Speter**
4666251883Speter** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
4667251883Speter** is returned each time a new row of data is ready for processing by the
4668251883Speter** caller. The values may be accessed using the [column access functions].
4669251883Speter** sqlite3_step() is called again to retrieve the next row of data.
4670251883Speter**
4671251883Speter** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
4672251883Speter** violation) has occurred.  sqlite3_step() should not be called again on
4673251883Speter** the VM. More information may be found by calling [sqlite3_errmsg()].
4674251883Speter** ^With the legacy interface, a more specific error code (for example,
4675251883Speter** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
4676251883Speter** can be obtained by calling [sqlite3_reset()] on the
4677251883Speter** [prepared statement].  ^In the "v2" interface,
4678251883Speter** the more specific error code is returned directly by sqlite3_step().
4679251883Speter**
4680251883Speter** [SQLITE_MISUSE] means that the this routine was called inappropriately.
4681251883Speter** Perhaps it was called on a [prepared statement] that has
4682251883Speter** already been [sqlite3_finalize | finalized] or on one that had
4683251883Speter** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
4684251883Speter** be the case that the same database connection is being used by two or
4685251883Speter** more threads at the same moment in time.
4686251883Speter**
4687251883Speter** For all versions of SQLite up to and including 3.6.23.1, a call to
4688251883Speter** [sqlite3_reset()] was required after sqlite3_step() returned anything
4689251883Speter** other than [SQLITE_ROW] before any subsequent invocation of
4690366076Scy** sqlite3_step().  Failure to reset the prepared statement using
4691251883Speter** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
4692342292Scy** sqlite3_step().  But after [version 3.6.23.1] ([dateof:3.6.23.1],
4693322444Speter** sqlite3_step() began
4694251883Speter** calling [sqlite3_reset()] automatically in this circumstance rather
4695251883Speter** than returning [SQLITE_MISUSE].  This is not considered a compatibility
4696251883Speter** break because any application that ever receives an SQLITE_MISUSE error
4697251883Speter** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
4698251883Speter** can be used to restore the legacy behavior.
4699251883Speter**
4700251883Speter** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
4701251883Speter** API always returns a generic error code, [SQLITE_ERROR], following any
4702251883Speter** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
4703251883Speter** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
4704251883Speter** specific [error codes] that better describes the error.
4705251883Speter** We admit that this is a goofy design.  The problem has been fixed
4706251883Speter** with the "v2" interface.  If you prepare all of your SQL statements
4707322444Speter** using [sqlite3_prepare_v3()] or [sqlite3_prepare_v2()]
4708322444Speter** or [sqlite3_prepare16_v2()] or [sqlite3_prepare16_v3()] instead
4709251883Speter** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
4710251883Speter** then the more specific [error codes] are returned directly
4711322444Speter** by sqlite3_step().  The use of the "vX" interfaces is recommended.
4712251883Speter*/
4713322444SpeterSQLITE_API int sqlite3_step(sqlite3_stmt*);
4714251883Speter
4715251883Speter/*
4716251883Speter** CAPI3REF: Number of columns in a result set
4717286510Speter** METHOD: sqlite3_stmt
4718251883Speter**
4719251883Speter** ^The sqlite3_data_count(P) interface returns the number of columns in the
4720251883Speter** current row of the result set of [prepared statement] P.
4721251883Speter** ^If prepared statement P does not have results ready to return
4722361456Scy** (via calls to the [sqlite3_column_int | sqlite3_column()] family of
4723251883Speter** interfaces) then sqlite3_data_count(P) returns 0.
4724251883Speter** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
4725251883Speter** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
4726251883Speter** [sqlite3_step](P) returned [SQLITE_DONE].  ^The sqlite3_data_count(P)
4727251883Speter** will return non-zero if previous call to [sqlite3_step](P) returned
4728251883Speter** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
4729251883Speter** where it always returns zero since each step of that multi-step
4730251883Speter** pragma returns 0 columns of data.
4731251883Speter**
4732251883Speter** See also: [sqlite3_column_count()]
4733251883Speter*/
4734322444SpeterSQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
4735251883Speter
4736251883Speter/*
4737251883Speter** CAPI3REF: Fundamental Datatypes
4738251883Speter** KEYWORDS: SQLITE_TEXT
4739251883Speter**
4740251883Speter** ^(Every value in SQLite has one of five fundamental datatypes:
4741251883Speter**
4742251883Speter** <ul>
4743251883Speter** <li> 64-bit signed integer
4744251883Speter** <li> 64-bit IEEE floating point number
4745251883Speter** <li> string
4746251883Speter** <li> BLOB
4747251883Speter** <li> NULL
4748251883Speter** </ul>)^
4749251883Speter**
4750251883Speter** These constants are codes for each of those types.
4751251883Speter**
4752251883Speter** Note that the SQLITE_TEXT constant was also used in SQLite version 2
4753251883Speter** for a completely different meaning.  Software that links against both
4754251883Speter** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
4755251883Speter** SQLITE_TEXT.
4756251883Speter*/
4757251883Speter#define SQLITE_INTEGER  1
4758251883Speter#define SQLITE_FLOAT    2
4759251883Speter#define SQLITE_BLOB     4
4760251883Speter#define SQLITE_NULL     5
4761251883Speter#ifdef SQLITE_TEXT
4762251883Speter# undef SQLITE_TEXT
4763251883Speter#else
4764251883Speter# define SQLITE_TEXT     3
4765251883Speter#endif
4766251883Speter#define SQLITE3_TEXT     3
4767251883Speter
4768251883Speter/*
4769251883Speter** CAPI3REF: Result Values From A Query
4770251883Speter** KEYWORDS: {column access functions}
4771286510Speter** METHOD: sqlite3_stmt
4772251883Speter**
4773322444Speter** <b>Summary:</b>
4774322444Speter** <blockquote><table border=0 cellpadding=0 cellspacing=0>
4775322444Speter** <tr><td><b>sqlite3_column_blob</b><td>&rarr;<td>BLOB result
4776322444Speter** <tr><td><b>sqlite3_column_double</b><td>&rarr;<td>REAL result
4777322444Speter** <tr><td><b>sqlite3_column_int</b><td>&rarr;<td>32-bit INTEGER result
4778322444Speter** <tr><td><b>sqlite3_column_int64</b><td>&rarr;<td>64-bit INTEGER result
4779322444Speter** <tr><td><b>sqlite3_column_text</b><td>&rarr;<td>UTF-8 TEXT result
4780322444Speter** <tr><td><b>sqlite3_column_text16</b><td>&rarr;<td>UTF-16 TEXT result
4781366076Scy** <tr><td><b>sqlite3_column_value</b><td>&rarr;<td>The result as an
4782322444Speter** [sqlite3_value|unprotected sqlite3_value] object.
4783322444Speter** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
4784322444Speter** <tr><td><b>sqlite3_column_bytes</b><td>&rarr;<td>Size of a BLOB
4785322444Speter** or a UTF-8 TEXT result in bytes
4786322444Speter** <tr><td><b>sqlite3_column_bytes16&nbsp;&nbsp;</b>
4787322444Speter** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
4788322444Speter** TEXT in bytes
4789322444Speter** <tr><td><b>sqlite3_column_type</b><td>&rarr;<td>Default
4790322444Speter** datatype of the result
4791322444Speter** </table></blockquote>
4792322444Speter**
4793322444Speter** <b>Details:</b>
4794322444Speter**
4795251883Speter** ^These routines return information about a single column of the current
4796251883Speter** result row of a query.  ^In every case the first argument is a pointer
4797251883Speter** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
4798251883Speter** that was returned from [sqlite3_prepare_v2()] or one of its variants)
4799251883Speter** and the second argument is the index of the column for which information
4800251883Speter** should be returned. ^The leftmost column of the result set has the index 0.
4801251883Speter** ^The number of columns in the result can be determined using
4802251883Speter** [sqlite3_column_count()].
4803251883Speter**
4804251883Speter** If the SQL statement does not currently point to a valid row, or if the
4805251883Speter** column index is out of range, the result is undefined.
4806251883Speter** These routines may only be called when the most recent call to
4807251883Speter** [sqlite3_step()] has returned [SQLITE_ROW] and neither
4808251883Speter** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
4809251883Speter** If any of these routines are called after [sqlite3_reset()] or
4810251883Speter** [sqlite3_finalize()] or after [sqlite3_step()] has returned
4811251883Speter** something other than [SQLITE_ROW], the results are undefined.
4812251883Speter** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
4813251883Speter** are called from a different thread while any of these routines
4814251883Speter** are pending, then the results are undefined.
4815251883Speter**
4816322444Speter** The first six interfaces (_blob, _double, _int, _int64, _text, and _text16)
4817322444Speter** each return the value of a result column in a specific data format.  If
4818322444Speter** the result column is not initially in the requested format (for example,
4819322444Speter** if the query returns an integer but the sqlite3_column_text() interface
4820322444Speter** is used to extract the value) then an automatic type conversion is performed.
4821322444Speter**
4822251883Speter** ^The sqlite3_column_type() routine returns the
4823251883Speter** [SQLITE_INTEGER | datatype code] for the initial data type
4824251883Speter** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
4825322444Speter** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].
4826322444Speter** The return value of sqlite3_column_type() can be used to decide which
4827322444Speter** of the first six interface should be used to extract the column value.
4828322444Speter** The value returned by sqlite3_column_type() is only meaningful if no
4829366076Scy** automatic type conversions have occurred for the value in question.
4830322444Speter** After a type conversion, the result of calling sqlite3_column_type()
4831322444Speter** is undefined, though harmless.  Future
4832251883Speter** versions of SQLite may change the behavior of sqlite3_column_type()
4833251883Speter** following a type conversion.
4834251883Speter**
4835322444Speter** If the result is a BLOB or a TEXT string, then the sqlite3_column_bytes()
4836322444Speter** or sqlite3_column_bytes16() interfaces can be used to determine the size
4837322444Speter** of that BLOB or string.
4838322444Speter**
4839251883Speter** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
4840251883Speter** routine returns the number of bytes in that BLOB or string.
4841251883Speter** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
4842251883Speter** the string to UTF-8 and then returns the number of bytes.
4843251883Speter** ^If the result is a numeric value then sqlite3_column_bytes() uses
4844251883Speter** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
4845251883Speter** the number of bytes in that string.
4846251883Speter** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
4847251883Speter**
4848251883Speter** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
4849251883Speter** routine returns the number of bytes in that BLOB or string.
4850251883Speter** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
4851251883Speter** the string to UTF-16 and then returns the number of bytes.
4852251883Speter** ^If the result is a numeric value then sqlite3_column_bytes16() uses
4853251883Speter** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
4854251883Speter** the number of bytes in that string.
4855251883Speter** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
4856251883Speter**
4857366076Scy** ^The values returned by [sqlite3_column_bytes()] and
4858251883Speter** [sqlite3_column_bytes16()] do not include the zero terminators at the end
4859251883Speter** of the string.  ^For clarity: the values returned by
4860251883Speter** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
4861251883Speter** bytes in the string, not the number of characters.
4862251883Speter**
4863251883Speter** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
4864251883Speter** even empty strings, are always zero-terminated.  ^The return
4865251883Speter** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
4866251883Speter**
4867286510Speter** <b>Warning:</b> ^The object returned by [sqlite3_column_value()] is an
4868286510Speter** [unprotected sqlite3_value] object.  In a multithreaded environment,
4869286510Speter** an unprotected sqlite3_value object may only be used safely with
4870286510Speter** [sqlite3_bind_value()] and [sqlite3_result_value()].
4871251883Speter** If the [unprotected sqlite3_value] object returned by
4872251883Speter** [sqlite3_column_value()] is used in any other way, including calls
4873251883Speter** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
4874286510Speter** or [sqlite3_value_bytes()], the behavior is not threadsafe.
4875322444Speter** Hence, the sqlite3_column_value() interface
4876366076Scy** is normally only useful within the implementation of
4877322444Speter** [application-defined SQL functions] or [virtual tables], not within
4878322444Speter** top-level application code.
4879251883Speter**
4880322444Speter** The these routines may attempt to convert the datatype of the result.
4881322444Speter** ^For example, if the internal representation is FLOAT and a text result
4882251883Speter** is requested, [sqlite3_snprintf()] is used internally to perform the
4883251883Speter** conversion automatically.  ^(The following table details the conversions
4884251883Speter** that are applied:
4885251883Speter**
4886251883Speter** <blockquote>
4887251883Speter** <table border="1">
4888251883Speter** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
4889251883Speter**
4890251883Speter** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
4891251883Speter** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
4892269851Speter** <tr><td>  NULL    <td>   TEXT    <td> Result is a NULL pointer
4893269851Speter** <tr><td>  NULL    <td>   BLOB    <td> Result is a NULL pointer
4894251883Speter** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
4895251883Speter** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
4896251883Speter** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
4897269851Speter** <tr><td>  FLOAT   <td> INTEGER   <td> [CAST] to INTEGER
4898251883Speter** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
4899269851Speter** <tr><td>  FLOAT   <td>   BLOB    <td> [CAST] to BLOB
4900269851Speter** <tr><td>  TEXT    <td> INTEGER   <td> [CAST] to INTEGER
4901269851Speter** <tr><td>  TEXT    <td>  FLOAT    <td> [CAST] to REAL
4902251883Speter** <tr><td>  TEXT    <td>   BLOB    <td> No change
4903269851Speter** <tr><td>  BLOB    <td> INTEGER   <td> [CAST] to INTEGER
4904269851Speter** <tr><td>  BLOB    <td>  FLOAT    <td> [CAST] to REAL
4905251883Speter** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
4906251883Speter** </table>
4907251883Speter** </blockquote>)^
4908251883Speter**
4909251883Speter** Note that when type conversions occur, pointers returned by prior
4910251883Speter** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
4911251883Speter** sqlite3_column_text16() may be invalidated.
4912251883Speter** Type conversions and pointer invalidations might occur
4913251883Speter** in the following cases:
4914251883Speter**
4915251883Speter** <ul>
4916251883Speter** <li> The initial content is a BLOB and sqlite3_column_text() or
4917251883Speter**      sqlite3_column_text16() is called.  A zero-terminator might
4918251883Speter**      need to be added to the string.</li>
4919251883Speter** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
4920251883Speter**      sqlite3_column_text16() is called.  The content must be converted
4921251883Speter**      to UTF-16.</li>
4922251883Speter** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
4923251883Speter**      sqlite3_column_text() is called.  The content must be converted
4924251883Speter**      to UTF-8.</li>
4925251883Speter** </ul>
4926251883Speter**
4927251883Speter** ^Conversions between UTF-16be and UTF-16le are always done in place and do
4928251883Speter** not invalidate a prior pointer, though of course the content of the buffer
4929251883Speter** that the prior pointer references will have been modified.  Other kinds
4930251883Speter** of conversion are done in place when it is possible, but sometimes they
4931251883Speter** are not possible and in those cases prior pointers are invalidated.
4932251883Speter**
4933286510Speter** The safest policy is to invoke these routines
4934251883Speter** in one of the following ways:
4935251883Speter**
4936251883Speter** <ul>
4937251883Speter**  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
4938251883Speter**  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
4939251883Speter**  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
4940251883Speter** </ul>
4941251883Speter**
4942251883Speter** In other words, you should call sqlite3_column_text(),
4943251883Speter** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
4944251883Speter** into the desired format, then invoke sqlite3_column_bytes() or
4945251883Speter** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
4946251883Speter** to sqlite3_column_text() or sqlite3_column_blob() with calls to
4947251883Speter** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
4948251883Speter** with calls to sqlite3_column_bytes().
4949251883Speter**
4950251883Speter** ^The pointers returned are valid until a type conversion occurs as
4951251883Speter** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
4952251883Speter** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
4953322444Speter** and BLOBs is freed automatically.  Do not pass the pointers returned
4954269851Speter** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
4955251883Speter** [sqlite3_free()].
4956251883Speter**
4957342292Scy** As long as the input parameters are correct, these routines will only
4958342292Scy** fail if an out-of-memory error occurs during a format conversion.
4959342292Scy** Only the following subset of interfaces are subject to out-of-memory
4960342292Scy** errors:
4961342292Scy**
4962342292Scy** <ul>
4963342292Scy** <li> sqlite3_column_blob()
4964342292Scy** <li> sqlite3_column_text()
4965342292Scy** <li> sqlite3_column_text16()
4966342292Scy** <li> sqlite3_column_bytes()
4967342292Scy** <li> sqlite3_column_bytes16()
4968342292Scy** </ul>
4969342292Scy**
4970342292Scy** If an out-of-memory error occurs, then the return value from these
4971342292Scy** routines is the same as if the column had contained an SQL NULL value.
4972342292Scy** Valid SQL NULL returns can be distinguished from out-of-memory errors
4973342292Scy** by invoking the [sqlite3_errcode()] immediately after the suspect
4974342292Scy** return value is obtained and before any
4975342292Scy** other SQLite interface is called on the same [database connection].
4976251883Speter*/
4977322444SpeterSQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
4978322444SpeterSQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
4979322444SpeterSQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
4980322444SpeterSQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
4981322444SpeterSQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
4982322444SpeterSQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
4983322444SpeterSQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
4984322444SpeterSQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
4985322444SpeterSQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
4986322444SpeterSQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
4987251883Speter
4988251883Speter/*
4989251883Speter** CAPI3REF: Destroy A Prepared Statement Object
4990286510Speter** DESTRUCTOR: sqlite3_stmt
4991251883Speter**
4992251883Speter** ^The sqlite3_finalize() function is called to delete a [prepared statement].
4993251883Speter** ^If the most recent evaluation of the statement encountered no errors
4994251883Speter** or if the statement is never been evaluated, then sqlite3_finalize() returns
4995251883Speter** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
4996251883Speter** sqlite3_finalize(S) returns the appropriate [error code] or
4997251883Speter** [extended error code].
4998251883Speter**
4999251883Speter** ^The sqlite3_finalize(S) routine can be called at any point during
5000251883Speter** the life cycle of [prepared statement] S:
5001251883Speter** before statement S is ever evaluated, after
5002251883Speter** one or more calls to [sqlite3_reset()], or after any call
5003251883Speter** to [sqlite3_step()] regardless of whether or not the statement has
5004251883Speter** completed execution.
5005251883Speter**
5006251883Speter** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
5007251883Speter**
5008251883Speter** The application must finalize every [prepared statement] in order to avoid
5009251883Speter** resource leaks.  It is a grievous error for the application to try to use
5010251883Speter** a prepared statement after it has been finalized.  Any use of a prepared
5011251883Speter** statement after it has been finalized can result in undefined and
5012251883Speter** undesirable behavior such as segfaults and heap corruption.
5013251883Speter*/
5014322444SpeterSQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5015251883Speter
5016251883Speter/*
5017251883Speter** CAPI3REF: Reset A Prepared Statement Object
5018286510Speter** METHOD: sqlite3_stmt
5019251883Speter**
5020251883Speter** The sqlite3_reset() function is called to reset a [prepared statement]
5021251883Speter** object back to its initial state, ready to be re-executed.
5022251883Speter** ^Any SQL statement variables that had values bound to them using
5023251883Speter** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
5024251883Speter** Use [sqlite3_clear_bindings()] to reset the bindings.
5025251883Speter**
5026251883Speter** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
5027251883Speter** back to the beginning of its program.
5028251883Speter**
5029251883Speter** ^If the most recent call to [sqlite3_step(S)] for the
5030251883Speter** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
5031251883Speter** or if [sqlite3_step(S)] has never before been called on S,
5032251883Speter** then [sqlite3_reset(S)] returns [SQLITE_OK].
5033251883Speter**
5034251883Speter** ^If the most recent call to [sqlite3_step(S)] for the
5035251883Speter** [prepared statement] S indicated an error, then
5036251883Speter** [sqlite3_reset(S)] returns an appropriate [error code].
5037251883Speter**
5038251883Speter** ^The [sqlite3_reset(S)] interface does not change the values
5039251883Speter** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
5040251883Speter*/
5041322444SpeterSQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5042251883Speter
5043251883Speter/*
5044251883Speter** CAPI3REF: Create Or Redefine SQL Functions
5045251883Speter** KEYWORDS: {function creation routines}
5046286510Speter** METHOD: sqlite3
5047251883Speter**
5048251883Speter** ^These functions (collectively known as "function creation routines")
5049251883Speter** are used to add SQL functions or aggregates or to redefine the behavior
5050342292Scy** of existing SQL functions or aggregates. The only differences between
5051366076Scy** the three "sqlite3_create_function*" routines are the text encoding
5052366076Scy** expected for the second parameter (the name of the function being
5053342292Scy** created) and the presence or absence of a destructor callback for
5054342292Scy** the application data pointer. Function sqlite3_create_window_function()
5055342292Scy** is similar, but allows the user to supply the extra callback functions
5056342292Scy** needed by [aggregate window functions].
5057251883Speter**
5058251883Speter** ^The first parameter is the [database connection] to which the SQL
5059251883Speter** function is to be added.  ^If an application uses more than one database
5060251883Speter** connection then application-defined SQL functions must be added
5061251883Speter** to each database connection separately.
5062251883Speter**
5063251883Speter** ^The second parameter is the name of the SQL function to be created or
5064251883Speter** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
5065251883Speter** representation, exclusive of the zero-terminator.  ^Note that the name
5066366076Scy** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
5067251883Speter** ^Any attempt to create a function with a longer name
5068251883Speter** will result in [SQLITE_MISUSE] being returned.
5069251883Speter**
5070251883Speter** ^The third parameter (nArg)
5071251883Speter** is the number of arguments that the SQL function or
5072251883Speter** aggregate takes. ^If this parameter is -1, then the SQL function or
5073251883Speter** aggregate may take any number of arguments between 0 and the limit
5074251883Speter** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
5075251883Speter** parameter is less than -1 or greater than 127 then the behavior is
5076251883Speter** undefined.
5077251883Speter**
5078251883Speter** ^The fourth parameter, eTextRep, specifies what
5079251883Speter** [SQLITE_UTF8 | text encoding] this SQL function prefers for
5080269851Speter** its parameters.  The application should set this parameter to
5081366076Scy** [SQLITE_UTF16LE] if the function implementation invokes
5082269851Speter** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
5083269851Speter** implementation invokes [sqlite3_value_text16be()] on an input, or
5084269851Speter** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
5085269851Speter** otherwise.  ^The same SQL function may be registered multiple times using
5086269851Speter** different preferred text encodings, with different implementations for
5087269851Speter** each encoding.
5088251883Speter** ^When multiple implementations of the same function are available, SQLite
5089251883Speter** will pick the one that involves the least amount of data conversion.
5090251883Speter**
5091269851Speter** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
5092269851Speter** to signal that the function will always return the same result given
5093269851Speter** the same inputs within a single SQL statement.  Most SQL functions are
5094269851Speter** deterministic.  The built-in [random()] SQL function is an example of a
5095269851Speter** function that is not deterministic.  The SQLite query planner is able to
5096269851Speter** perform additional optimizations on deterministic functions, so use
5097269851Speter** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
5098269851Speter**
5099355326Scy** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
5100355326Scy** flag, which if present prevents the function from being invoked from
5101361456Scy** within VIEWs, TRIGGERs, CHECK constraints, generated column expressions,
5102361456Scy** index expressions, or the WHERE clause of partial indexes.
5103355326Scy**
5104361456Scy** <span style="background-color:#ffff90;">
5105361456Scy** For best security, the [SQLITE_DIRECTONLY] flag is recommended for
5106361456Scy** all application-defined SQL functions that do not need to be
5107361456Scy** used inside of triggers, view, CHECK constraints, or other elements of
5108366076Scy** the database schema.  This flags is especially recommended for SQL
5109361456Scy** functions that have side effects or reveal internal application state.
5110361456Scy** Without this flag, an attacker might be able to modify the schema of
5111361456Scy** a database file to include invocations of the function with parameters
5112361456Scy** chosen by the attacker, which the application will then execute when
5113361456Scy** the database file is opened and read.
5114361456Scy** </span>
5115361456Scy**
5116251883Speter** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
5117251883Speter** function can gain access to this pointer using [sqlite3_user_data()].)^
5118251883Speter**
5119342292Scy** ^The sixth, seventh and eighth parameters passed to the three
5120342292Scy** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
5121251883Speter** pointers to C-language functions that implement the SQL function or
5122251883Speter** aggregate. ^A scalar SQL function requires an implementation of the xFunc
5123251883Speter** callback only; NULL pointers must be passed as the xStep and xFinal
5124251883Speter** parameters. ^An aggregate SQL function requires an implementation of xStep
5125251883Speter** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
5126251883Speter** SQL function or aggregate, pass NULL pointers for all three function
5127251883Speter** callbacks.
5128251883Speter**
5129366076Scy** ^The sixth, seventh, eighth and ninth parameters (xStep, xFinal, xValue
5130342292Scy** and xInverse) passed to sqlite3_create_window_function are pointers to
5131342292Scy** C-language callbacks that implement the new function. xStep and xFinal
5132342292Scy** must both be non-NULL. xValue and xInverse may either both be NULL, in
5133366076Scy** which case a regular aggregate function is created, or must both be
5134342292Scy** non-NULL, in which case the new function may be used as either an aggregate
5135342292Scy** or aggregate window function. More details regarding the implementation
5136366076Scy** of aggregate window functions are
5137342292Scy** [user-defined window functions|available here].
5138251883Speter**
5139342292Scy** ^(If the final parameter to sqlite3_create_function_v2() or
5140342292Scy** sqlite3_create_window_function() is not NULL, then it is destructor for
5141366076Scy** the application data pointer. The destructor is invoked when the function
5142366076Scy** is deleted, either by being overloaded or when the database connection
5143366076Scy** closes.)^ ^The destructor is also invoked if the call to
5144342292Scy** sqlite3_create_function_v2() fails.  ^When the destructor callback is
5145342292Scy** invoked, it is passed a single argument which is a copy of the application
5146342292Scy** data pointer which was the fifth parameter to sqlite3_create_function_v2().
5147342292Scy**
5148251883Speter** ^It is permitted to register multiple implementations of the same
5149251883Speter** functions with the same name but with either differing numbers of
5150251883Speter** arguments or differing preferred text encodings.  ^SQLite will use
5151251883Speter** the implementation that most closely matches the way in which the
5152251883Speter** SQL function is used.  ^A function implementation with a non-negative
5153251883Speter** nArg parameter is a better match than a function implementation with
5154251883Speter** a negative nArg.  ^A function where the preferred text encoding
5155251883Speter** matches the database encoding is a better
5156366076Scy** match than a function where the encoding is different.
5157251883Speter** ^A function where the encoding difference is between UTF16le and UTF16be
5158251883Speter** is a closer match than a function where the encoding difference is
5159251883Speter** between UTF8 and UTF16.
5160251883Speter**
5161251883Speter** ^Built-in functions may be overloaded by new application-defined functions.
5162251883Speter**
5163251883Speter** ^An application-defined function is permitted to call other
5164251883Speter** SQLite interfaces.  However, such calls must not
5165251883Speter** close the database connection nor finalize or reset the prepared
5166251883Speter** statement in which the function is running.
5167251883Speter*/
5168322444SpeterSQLITE_API int sqlite3_create_function(
5169251883Speter  sqlite3 *db,
5170251883Speter  const char *zFunctionName,
5171251883Speter  int nArg,
5172251883Speter  int eTextRep,
5173251883Speter  void *pApp,
5174251883Speter  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5175251883Speter  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5176251883Speter  void (*xFinal)(sqlite3_context*)
5177251883Speter);
5178322444SpeterSQLITE_API int sqlite3_create_function16(
5179251883Speter  sqlite3 *db,
5180251883Speter  const void *zFunctionName,
5181251883Speter  int nArg,
5182251883Speter  int eTextRep,
5183251883Speter  void *pApp,
5184251883Speter  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5185251883Speter  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5186251883Speter  void (*xFinal)(sqlite3_context*)
5187251883Speter);
5188322444SpeterSQLITE_API int sqlite3_create_function_v2(
5189251883Speter  sqlite3 *db,
5190251883Speter  const char *zFunctionName,
5191251883Speter  int nArg,
5192251883Speter  int eTextRep,
5193251883Speter  void *pApp,
5194251883Speter  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
5195251883Speter  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5196251883Speter  void (*xFinal)(sqlite3_context*),
5197251883Speter  void(*xDestroy)(void*)
5198251883Speter);
5199342292ScySQLITE_API int sqlite3_create_window_function(
5200342292Scy  sqlite3 *db,
5201342292Scy  const char *zFunctionName,
5202342292Scy  int nArg,
5203342292Scy  int eTextRep,
5204342292Scy  void *pApp,
5205342292Scy  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
5206342292Scy  void (*xFinal)(sqlite3_context*),
5207342292Scy  void (*xValue)(sqlite3_context*),
5208342292Scy  void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
5209342292Scy  void(*xDestroy)(void*)
5210342292Scy);
5211251883Speter
5212251883Speter/*
5213251883Speter** CAPI3REF: Text Encodings
5214251883Speter**
5215251883Speter** These constant define integer codes that represent the various
5216251883Speter** text encodings supported by SQLite.
5217251883Speter*/
5218282328Sbapt#define SQLITE_UTF8           1    /* IMP: R-37514-35566 */
5219282328Sbapt#define SQLITE_UTF16LE        2    /* IMP: R-03371-37637 */
5220282328Sbapt#define SQLITE_UTF16BE        3    /* IMP: R-51971-34154 */
5221251883Speter#define SQLITE_UTF16          4    /* Use native byte order */
5222269851Speter#define SQLITE_ANY            5    /* Deprecated */
5223251883Speter#define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
5224251883Speter
5225251883Speter/*
5226269851Speter** CAPI3REF: Function Flags
5227269851Speter**
5228366076Scy** These constants may be ORed together with the
5229269851Speter** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
5230269851Speter** to [sqlite3_create_function()], [sqlite3_create_function16()], or
5231269851Speter** [sqlite3_create_function_v2()].
5232355326Scy**
5233361456Scy** <dl>
5234361456Scy** [[SQLITE_DETERMINISTIC]] <dt>SQLITE_DETERMINISTIC</dt><dd>
5235361456Scy** The SQLITE_DETERMINISTIC flag means that the new function always gives
5236361456Scy** the same output when the input parameters are the same.
5237361456Scy** The [abs|abs() function] is deterministic, for example, but
5238361456Scy** [randomblob|randomblob()] is not.  Functions must
5239361456Scy** be deterministic in order to be used in certain contexts such as
5240361456Scy** with the WHERE clause of [partial indexes] or in [generated columns].
5241361456Scy** SQLite might also optimize deterministic functions by factoring them
5242361456Scy** out of inner loops.
5243361456Scy** </dd>
5244366076Scy**
5245361456Scy** [[SQLITE_DIRECTONLY]] <dt>SQLITE_DIRECTONLY</dt><dd>
5246355326Scy** The SQLITE_DIRECTONLY flag means that the function may only be invoked
5247366076Scy** from top-level SQL, and cannot be used in VIEWs or TRIGGERs nor in
5248361456Scy** schema structures such as [CHECK constraints], [DEFAULT clauses],
5249361456Scy** [expression indexes], [partial indexes], or [generated columns].
5250361456Scy** The SQLITE_DIRECTONLY flags is a security feature which is recommended
5251361456Scy** for all [application-defined SQL functions], and especially for functions
5252361456Scy** that have side-effects or that could potentially leak sensitive
5253361456Scy** information.
5254361456Scy** </dd>
5255355326Scy**
5256361456Scy** [[SQLITE_INNOCUOUS]] <dt>SQLITE_INNOCUOUS</dt><dd>
5257361456Scy** The SQLITE_INNOCUOUS flag means that the function is unlikely
5258361456Scy** to cause problems even if misused.  An innocuous function should have
5259361456Scy** no side effects and should not depend on any values other than its
5260361456Scy** input parameters. The [abs|abs() function] is an example of an
5261361456Scy** innocuous function.
5262361456Scy** The [load_extension() SQL function] is not innocuous because of its
5263361456Scy** side effects.
5264361456Scy** <p> SQLITE_INNOCUOUS is similar to SQLITE_DETERMINISTIC, but is not
5265361456Scy** exactly the same.  The [random|random() function] is an example of a
5266361456Scy** function that is innocuous but not deterministic.
5267361456Scy** <p>Some heightened security settings
5268361456Scy** ([SQLITE_DBCONFIG_TRUSTED_SCHEMA] and [PRAGMA trusted_schema=OFF])
5269361456Scy** disable the use of SQL functions inside views and triggers and in
5270361456Scy** schema structures such as [CHECK constraints], [DEFAULT clauses],
5271361456Scy** [expression indexes], [partial indexes], and [generated columns] unless
5272361456Scy** the function is tagged with SQLITE_INNOCUOUS.  Most built-in functions
5273361456Scy** are innocuous.  Developers are advised to avoid using the
5274361456Scy** SQLITE_INNOCUOUS flag for application-defined functions unless the
5275361456Scy** function has been carefully audited and found to be free of potentially
5276361456Scy** security-adverse side-effects and information-leaks.
5277361456Scy** </dd>
5278361456Scy**
5279361456Scy** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5280355326Scy** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5281355326Scy** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5282355326Scy** Specifying this flag makes no difference for scalar or aggregate user
5283355326Scy** functions. However, if it is not specified for a user-defined window
5284355326Scy** function, then any sub-types belonging to arguments passed to the window
5285355326Scy** function may be discarded before the window function is called (i.e.
5286355326Scy** sqlite3_value_subtype() will always return 0).
5287361456Scy** </dd>
5288361456Scy** </dl>
5289269851Speter*/
5290355326Scy#define SQLITE_DETERMINISTIC    0x000000800
5291355326Scy#define SQLITE_DIRECTONLY       0x000080000
5292355326Scy#define SQLITE_SUBTYPE          0x000100000
5293361456Scy#define SQLITE_INNOCUOUS        0x000200000
5294269851Speter
5295269851Speter/*
5296251883Speter** CAPI3REF: Deprecated Functions
5297251883Speter** DEPRECATED
5298251883Speter**
5299251883Speter** These functions are [deprecated].  In order to maintain
5300366076Scy** backwards compatibility with older code, these functions continue
5301251883Speter** to be supported.  However, new applications should avoid
5302282328Sbapt** the use of these functions.  To encourage programmers to avoid
5303282328Sbapt** these functions, we will not explain what they do.
5304251883Speter*/
5305251883Speter#ifndef SQLITE_OMIT_DEPRECATED
5306322444SpeterSQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
5307322444SpeterSQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
5308322444SpeterSQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
5309322444SpeterSQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
5310322444SpeterSQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
5311322444SpeterSQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
5312251883Speter                      void*,sqlite3_int64);
5313251883Speter#endif
5314251883Speter
5315251883Speter/*
5316286510Speter** CAPI3REF: Obtaining SQL Values
5317286510Speter** METHOD: sqlite3_value
5318251883Speter**
5319322444Speter** <b>Summary:</b>
5320322444Speter** <blockquote><table border=0 cellpadding=0 cellspacing=0>
5321322444Speter** <tr><td><b>sqlite3_value_blob</b><td>&rarr;<td>BLOB value
5322322444Speter** <tr><td><b>sqlite3_value_double</b><td>&rarr;<td>REAL value
5323322444Speter** <tr><td><b>sqlite3_value_int</b><td>&rarr;<td>32-bit INTEGER value
5324322444Speter** <tr><td><b>sqlite3_value_int64</b><td>&rarr;<td>64-bit INTEGER value
5325322444Speter** <tr><td><b>sqlite3_value_pointer</b><td>&rarr;<td>Pointer value
5326322444Speter** <tr><td><b>sqlite3_value_text</b><td>&rarr;<td>UTF-8 TEXT value
5327322444Speter** <tr><td><b>sqlite3_value_text16</b><td>&rarr;<td>UTF-16 TEXT value in
5328322444Speter** the native byteorder
5329322444Speter** <tr><td><b>sqlite3_value_text16be</b><td>&rarr;<td>UTF-16be TEXT value
5330322444Speter** <tr><td><b>sqlite3_value_text16le</b><td>&rarr;<td>UTF-16le TEXT value
5331322444Speter** <tr><td>&nbsp;<td>&nbsp;<td>&nbsp;
5332322444Speter** <tr><td><b>sqlite3_value_bytes</b><td>&rarr;<td>Size of a BLOB
5333322444Speter** or a UTF-8 TEXT in bytes
5334322444Speter** <tr><td><b>sqlite3_value_bytes16&nbsp;&nbsp;</b>
5335322444Speter** <td>&rarr;&nbsp;&nbsp;<td>Size of UTF-16
5336322444Speter** TEXT in bytes
5337322444Speter** <tr><td><b>sqlite3_value_type</b><td>&rarr;<td>Default
5338322444Speter** datatype of the value
5339322444Speter** <tr><td><b>sqlite3_value_numeric_type&nbsp;&nbsp;</b>
5340322444Speter** <td>&rarr;&nbsp;&nbsp;<td>Best numeric datatype of the value
5341342292Scy** <tr><td><b>sqlite3_value_nochange&nbsp;&nbsp;</b>
5342342292Scy** <td>&rarr;&nbsp;&nbsp;<td>True if the column is unchanged in an UPDATE
5343342292Scy** against a virtual table.
5344347347Scy** <tr><td><b>sqlite3_value_frombind&nbsp;&nbsp;</b>
5345347347Scy** <td>&rarr;&nbsp;&nbsp;<td>True if value originated from a [bound parameter]
5346322444Speter** </table></blockquote>
5347251883Speter**
5348322444Speter** <b>Details:</b>
5349251883Speter**
5350322444Speter** These routines extract type, size, and content information from
5351322444Speter** [protected sqlite3_value] objects.  Protected sqlite3_value objects
5352361456Scy** are used to pass parameter information into the functions that
5353361456Scy** implement [application-defined SQL functions] and [virtual tables].
5354322444Speter**
5355251883Speter** These routines work only with [protected sqlite3_value] objects.
5356251883Speter** Any attempt to use these routines on an [unprotected sqlite3_value]
5357322444Speter** is not threadsafe.
5358251883Speter**
5359251883Speter** ^These routines work just like the corresponding [column access functions]
5360274884Sbapt** except that these routines take a single [protected sqlite3_value] object
5361251883Speter** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
5362251883Speter**
5363251883Speter** ^The sqlite3_value_text16() interface extracts a UTF-16 string
5364251883Speter** in the native byte-order of the host machine.  ^The
5365251883Speter** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
5366251883Speter** extract UTF-16 strings as big-endian and little-endian respectively.
5367251883Speter**
5368366076Scy** ^If [sqlite3_value] object V was initialized
5369322444Speter** using [sqlite3_bind_pointer(S,I,P,X,D)] or [sqlite3_result_pointer(C,P,X,D)]
5370322444Speter** and if X and Y are strings that compare equal according to strcmp(X,Y),
5371322444Speter** then sqlite3_value_pointer(V,Y) will return the pointer P.  ^Otherwise,
5372366076Scy** sqlite3_value_pointer(V,Y) returns a NULL. The sqlite3_bind_pointer()
5373322444Speter** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
5374322444Speter**
5375322444Speter** ^(The sqlite3_value_type(V) interface returns the
5376322444Speter** [SQLITE_INTEGER | datatype code] for the initial datatype of the
5377322444Speter** [sqlite3_value] object V. The returned value is one of [SQLITE_INTEGER],
5378322444Speter** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].)^
5379322444Speter** Other interfaces might change the datatype for an sqlite3_value object.
5380322444Speter** For example, if the datatype is initially SQLITE_INTEGER and
5381322444Speter** sqlite3_value_text(V) is called to extract a text value for that
5382322444Speter** integer, then subsequent calls to sqlite3_value_type(V) might return
5383322444Speter** SQLITE_TEXT.  Whether or not a persistent internal datatype conversion
5384322444Speter** occurs is undefined and may change from one release of SQLite to the next.
5385322444Speter**
5386251883Speter** ^(The sqlite3_value_numeric_type() interface attempts to apply
5387251883Speter** numeric affinity to the value.  This means that an attempt is
5388251883Speter** made to convert the value to an integer or floating point.  If
5389251883Speter** such a conversion is possible without loss of information (in other
5390251883Speter** words, if the value is a string that looks like a number)
5391251883Speter** then the conversion is performed.  Otherwise no conversion occurs.
5392251883Speter** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
5393251883Speter**
5394342292Scy** ^Within the [xUpdate] method of a [virtual table], the
5395342292Scy** sqlite3_value_nochange(X) interface returns true if and only if
5396342292Scy** the column corresponding to X is unchanged by the UPDATE operation
5397342292Scy** that the xUpdate method call was invoked to implement and if
5398342292Scy** and the prior [xColumn] method call that was invoked to extracted
5399342292Scy** the value for that column returned without setting a result (probably
5400342292Scy** because it queried [sqlite3_vtab_nochange()] and found that the column
5401342292Scy** was unchanging).  ^Within an [xUpdate] method, any value for which
5402342292Scy** sqlite3_value_nochange(X) is true will in all other respects appear
5403342292Scy** to be a NULL value.  If sqlite3_value_nochange(X) is invoked anywhere other
5404342292Scy** than within an [xUpdate] method call for an UPDATE statement, then
5405342292Scy** the return value is arbitrary and meaningless.
5406342292Scy**
5407347347Scy** ^The sqlite3_value_frombind(X) interface returns non-zero if the
5408347347Scy** value X originated from one of the [sqlite3_bind_int|sqlite3_bind()]
5409347347Scy** interfaces.  ^If X comes from an SQL literal value, or a table column,
5410361456Scy** or an expression, then sqlite3_value_frombind(X) returns zero.
5411347347Scy**
5412251883Speter** Please pay particular attention to the fact that the pointer returned
5413251883Speter** from [sqlite3_value_blob()], [sqlite3_value_text()], or
5414251883Speter** [sqlite3_value_text16()] can be invalidated by a subsequent call to
5415251883Speter** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
5416251883Speter** or [sqlite3_value_text16()].
5417251883Speter**
5418251883Speter** These routines must be called from the same thread as
5419251883Speter** the SQL function that supplied the [sqlite3_value*] parameters.
5420342292Scy**
5421342292Scy** As long as the input parameter is correct, these routines can only
5422342292Scy** fail if an out-of-memory error occurs during a format conversion.
5423342292Scy** Only the following subset of interfaces are subject to out-of-memory
5424342292Scy** errors:
5425342292Scy**
5426342292Scy** <ul>
5427342292Scy** <li> sqlite3_value_blob()
5428342292Scy** <li> sqlite3_value_text()
5429342292Scy** <li> sqlite3_value_text16()
5430342292Scy** <li> sqlite3_value_text16le()
5431342292Scy** <li> sqlite3_value_text16be()
5432342292Scy** <li> sqlite3_value_bytes()
5433342292Scy** <li> sqlite3_value_bytes16()
5434342292Scy** </ul>
5435342292Scy**
5436342292Scy** If an out-of-memory error occurs, then the return value from these
5437342292Scy** routines is the same as if the column had contained an SQL NULL value.
5438342292Scy** Valid SQL NULL returns can be distinguished from out-of-memory errors
5439342292Scy** by invoking the [sqlite3_errcode()] immediately after the suspect
5440342292Scy** return value is obtained and before any
5441342292Scy** other SQLite interface is called on the same [database connection].
5442251883Speter*/
5443322444SpeterSQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
5444322444SpeterSQLITE_API double sqlite3_value_double(sqlite3_value*);
5445322444SpeterSQLITE_API int sqlite3_value_int(sqlite3_value*);
5446322444SpeterSQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
5447322444SpeterSQLITE_API void *sqlite3_value_pointer(sqlite3_value*, const char*);
5448322444SpeterSQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
5449322444SpeterSQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
5450322444SpeterSQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
5451322444SpeterSQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
5452322444SpeterSQLITE_API int sqlite3_value_bytes(sqlite3_value*);
5453322444SpeterSQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
5454322444SpeterSQLITE_API int sqlite3_value_type(sqlite3_value*);
5455322444SpeterSQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
5456342292ScySQLITE_API int sqlite3_value_nochange(sqlite3_value*);
5457347347ScySQLITE_API int sqlite3_value_frombind(sqlite3_value*);
5458251883Speter
5459251883Speter/*
5460298161Sbapt** CAPI3REF: Finding The Subtype Of SQL Values
5461298161Sbapt** METHOD: sqlite3_value
5462298161Sbapt**
5463298161Sbapt** The sqlite3_value_subtype(V) function returns the subtype for
5464298161Sbapt** an [application-defined SQL function] argument V.  The subtype
5465298161Sbapt** information can be used to pass a limited amount of context from
5466298161Sbapt** one SQL function to another.  Use the [sqlite3_result_subtype()]
5467298161Sbapt** routine to set the subtype for the return value of an SQL function.
5468298161Sbapt*/
5469322444SpeterSQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
5470298161Sbapt
5471298161Sbapt/*
5472286510Speter** CAPI3REF: Copy And Free SQL Values
5473286510Speter** METHOD: sqlite3_value
5474286510Speter**
5475286510Speter** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
5476286510Speter** object D and returns a pointer to that copy.  ^The [sqlite3_value] returned
5477286510Speter** is a [protected sqlite3_value] object even if the input is not.
5478286510Speter** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
5479286510Speter** memory allocation fails.
5480286510Speter**
5481286510Speter** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
5482286510Speter** previously obtained from [sqlite3_value_dup()].  ^If V is a NULL pointer
5483286510Speter** then sqlite3_value_free(V) is a harmless no-op.
5484286510Speter*/
5485322444SpeterSQLITE_API sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
5486322444SpeterSQLITE_API void sqlite3_value_free(sqlite3_value*);
5487286510Speter
5488286510Speter/*
5489251883Speter** CAPI3REF: Obtain Aggregate Function Context
5490286510Speter** METHOD: sqlite3_context
5491251883Speter**
5492251883Speter** Implementations of aggregate SQL functions use this
5493251883Speter** routine to allocate memory for storing their state.
5494251883Speter**
5495366076Scy** ^The first time the sqlite3_aggregate_context(C,N) routine is called
5496361456Scy** for a particular aggregate function, SQLite allocates
5497361456Scy** N bytes of memory, zeroes out that memory, and returns a pointer
5498251883Speter** to the new memory. ^On second and subsequent calls to
5499251883Speter** sqlite3_aggregate_context() for the same aggregate function instance,
5500251883Speter** the same buffer is returned.  Sqlite3_aggregate_context() is normally
5501251883Speter** called once for each invocation of the xStep callback and then one
5502251883Speter** last time when the xFinal callback is invoked.  ^(When no rows match
5503251883Speter** an aggregate query, the xStep() callback of the aggregate function
5504251883Speter** implementation is never called and xFinal() is called exactly once.
5505251883Speter** In those cases, sqlite3_aggregate_context() might be called for the
5506251883Speter** first time from within xFinal().)^
5507251883Speter**
5508366076Scy** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
5509251883Speter** when first called if N is less than or equal to zero or if a memory
5510251883Speter** allocate error occurs.
5511251883Speter**
5512251883Speter** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
5513251883Speter** determined by the N parameter on first successful call.  Changing the
5514362190Scy** value of N in any subsequent call to sqlite3_aggregate_context() within
5515251883Speter** the same aggregate function instance will not resize the memory
5516251883Speter** allocation.)^  Within the xFinal callback, it is customary to set
5517366076Scy** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
5518251883Speter** pointless memory allocations occur.
5519251883Speter**
5520366076Scy** ^SQLite automatically frees the memory allocated by
5521251883Speter** sqlite3_aggregate_context() when the aggregate query concludes.
5522251883Speter**
5523251883Speter** The first parameter must be a copy of the
5524251883Speter** [sqlite3_context | SQL function context] that is the first parameter
5525251883Speter** to the xStep or xFinal callback routine that implements the aggregate
5526251883Speter** function.
5527251883Speter**
5528251883Speter** This routine must be called from the same thread in which
5529251883Speter** the aggregate SQL function is running.
5530251883Speter*/
5531322444SpeterSQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
5532251883Speter
5533251883Speter/*
5534251883Speter** CAPI3REF: User Data For Functions
5535286510Speter** METHOD: sqlite3_context
5536251883Speter**
5537251883Speter** ^The sqlite3_user_data() interface returns a copy of
5538251883Speter** the pointer that was the pUserData parameter (the 5th parameter)
5539251883Speter** of the [sqlite3_create_function()]
5540251883Speter** and [sqlite3_create_function16()] routines that originally
5541251883Speter** registered the application defined function.
5542251883Speter**
5543251883Speter** This routine must be called from the same thread in which
5544251883Speter** the application-defined function is running.
5545251883Speter*/
5546322444SpeterSQLITE_API void *sqlite3_user_data(sqlite3_context*);
5547251883Speter
5548251883Speter/*
5549251883Speter** CAPI3REF: Database Connection For Functions
5550286510Speter** METHOD: sqlite3_context
5551251883Speter**
5552251883Speter** ^The sqlite3_context_db_handle() interface returns a copy of
5553251883Speter** the pointer to the [database connection] (the 1st parameter)
5554251883Speter** of the [sqlite3_create_function()]
5555251883Speter** and [sqlite3_create_function16()] routines that originally
5556251883Speter** registered the application defined function.
5557251883Speter*/
5558322444SpeterSQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
5559251883Speter
5560251883Speter/*
5561251883Speter** CAPI3REF: Function Auxiliary Data
5562286510Speter** METHOD: sqlite3_context
5563251883Speter**
5564269851Speter** These functions may be used by (non-aggregate) SQL functions to
5565251883Speter** associate metadata with argument values. If the same value is passed to
5566251883Speter** multiple invocations of the same SQL function during query execution, under
5567269851Speter** some circumstances the associated metadata may be preserved.  An example
5568269851Speter** of where this might be useful is in a regular-expression matching
5569269851Speter** function. The compiled version of the regular expression can be stored as
5570366076Scy** metadata associated with the pattern string.
5571269851Speter** Then as long as the pattern string remains the same,
5572269851Speter** the compiled regular expression can be reused on multiple
5573269851Speter** invocations of the same function.
5574251883Speter**
5575322444Speter** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
5576322444Speter** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
5577322444Speter** value to the application-defined function.  ^N is zero for the left-most
5578322444Speter** function argument.  ^If there is no metadata
5579322444Speter** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
5580269851Speter** returns a NULL pointer.
5581251883Speter**
5582269851Speter** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
5583269851Speter** argument of the application-defined function.  ^Subsequent
5584269851Speter** calls to sqlite3_get_auxdata(C,N) return P from the most recent
5585269851Speter** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
5586269851Speter** NULL if the metadata has been discarded.
5587269851Speter** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
5588269851Speter** SQLite will invoke the destructor function X with parameter P exactly
5589269851Speter** once, when the metadata is discarded.
5590269851Speter** SQLite is free to discard the metadata at any time, including: <ul>
5591305002Scy** <li> ^(when the corresponding function parameter changes)^, or
5592305002Scy** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
5593305002Scy**      SQL statement)^, or
5594305002Scy** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
5595305002Scy**       parameter)^, or
5596366076Scy** <li> ^(during the original sqlite3_set_auxdata() call when a memory
5597305002Scy**      allocation error occurs.)^ </ul>
5598251883Speter**
5599366076Scy** Note the last bullet in particular.  The destructor X in
5600269851Speter** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
5601269851Speter** sqlite3_set_auxdata() interface even returns.  Hence sqlite3_set_auxdata()
5602269851Speter** should be called near the end of the function implementation and the
5603269851Speter** function implementation should not make any use of P after
5604269851Speter** sqlite3_set_auxdata() has been called.
5605251883Speter**
5606251883Speter** ^(In practice, metadata is preserved between function calls for
5607269851Speter** function parameters that are compile-time constants, including literal
5608269851Speter** values and [parameters] and expressions composed from the same.)^
5609251883Speter**
5610322444Speter** The value of the N parameter to these interfaces should be non-negative.
5611322444Speter** Future enhancements may make use of negative N values to define new
5612322444Speter** kinds of function caching behavior.
5613322444Speter**
5614251883Speter** These routines must be called from the same thread in which
5615251883Speter** the SQL function is running.
5616251883Speter*/
5617322444SpeterSQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
5618322444SpeterSQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
5619251883Speter
5620251883Speter
5621251883Speter/*
5622251883Speter** CAPI3REF: Constants Defining Special Destructor Behavior
5623251883Speter**
5624251883Speter** These are special values for the destructor that is passed in as the
5625251883Speter** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
5626251883Speter** argument is SQLITE_STATIC, it means that the content pointer is constant
5627251883Speter** and will never change.  It does not need to be destroyed.  ^The
5628251883Speter** SQLITE_TRANSIENT value means that the content will likely change in
5629251883Speter** the near future and that SQLite should make its own private copy of
5630251883Speter** the content before returning.
5631251883Speter**
5632251883Speter** The typedef is necessary to work around problems in certain
5633251883Speter** C++ compilers.
5634251883Speter*/
5635251883Spetertypedef void (*sqlite3_destructor_type)(void*);
5636251883Speter#define SQLITE_STATIC      ((sqlite3_destructor_type)0)
5637251883Speter#define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
5638251883Speter
5639251883Speter/*
5640251883Speter** CAPI3REF: Setting The Result Of An SQL Function
5641286510Speter** METHOD: sqlite3_context
5642251883Speter**
5643251883Speter** These routines are used by the xFunc or xFinal callbacks that
5644251883Speter** implement SQL functions and aggregates.  See
5645251883Speter** [sqlite3_create_function()] and [sqlite3_create_function16()]
5646251883Speter** for additional information.
5647251883Speter**
5648251883Speter** These functions work very much like the [parameter binding] family of
5649251883Speter** functions used to bind values to host parameters in prepared statements.
5650251883Speter** Refer to the [SQL parameter] documentation for additional information.
5651251883Speter**
5652251883Speter** ^The sqlite3_result_blob() interface sets the result from
5653251883Speter** an application-defined function to be the BLOB whose content is pointed
5654251883Speter** to by the second parameter and which is N bytes long where N is the
5655251883Speter** third parameter.
5656251883Speter**
5657286510Speter** ^The sqlite3_result_zeroblob(C,N) and sqlite3_result_zeroblob64(C,N)
5658286510Speter** interfaces set the result of the application-defined function to be
5659286510Speter** a BLOB containing all zero bytes and N bytes in size.
5660251883Speter**
5661251883Speter** ^The sqlite3_result_double() interface sets the result from
5662251883Speter** an application-defined function to be a floating point value specified
5663251883Speter** by its 2nd argument.
5664251883Speter**
5665251883Speter** ^The sqlite3_result_error() and sqlite3_result_error16() functions
5666251883Speter** cause the implemented SQL function to throw an exception.
5667251883Speter** ^SQLite uses the string pointed to by the
5668251883Speter** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
5669251883Speter** as the text of an error message.  ^SQLite interprets the error
5670251883Speter** message string from sqlite3_result_error() as UTF-8. ^SQLite
5671362190Scy** interprets the string from sqlite3_result_error16() as UTF-16 using
5672362190Scy** the same [byte-order determination rules] as [sqlite3_bind_text16()].
5673362190Scy** ^If the third parameter to sqlite3_result_error()
5674251883Speter** or sqlite3_result_error16() is negative then SQLite takes as the error
5675251883Speter** message all text up through the first zero character.
5676251883Speter** ^If the third parameter to sqlite3_result_error() or
5677251883Speter** sqlite3_result_error16() is non-negative then SQLite takes that many
5678251883Speter** bytes (not characters) from the 2nd parameter as the error message.
5679251883Speter** ^The sqlite3_result_error() and sqlite3_result_error16()
5680251883Speter** routines make a private copy of the error message text before
5681251883Speter** they return.  Hence, the calling function can deallocate or
5682251883Speter** modify the text after they return without harm.
5683251883Speter** ^The sqlite3_result_error_code() function changes the error code
5684251883Speter** returned by SQLite as a result of an error in a function.  ^By default,
5685251883Speter** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
5686251883Speter** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
5687251883Speter**
5688251883Speter** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
5689251883Speter** error indicating that a string or BLOB is too long to represent.
5690251883Speter**
5691251883Speter** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
5692251883Speter** error indicating that a memory allocation failed.
5693251883Speter**
5694251883Speter** ^The sqlite3_result_int() interface sets the return value
5695251883Speter** of the application-defined function to be the 32-bit signed integer
5696251883Speter** value given in the 2nd argument.
5697251883Speter** ^The sqlite3_result_int64() interface sets the return value
5698251883Speter** of the application-defined function to be the 64-bit signed integer
5699251883Speter** value given in the 2nd argument.
5700251883Speter**
5701251883Speter** ^The sqlite3_result_null() interface sets the return value
5702251883Speter** of the application-defined function to be NULL.
5703251883Speter**
5704251883Speter** ^The sqlite3_result_text(), sqlite3_result_text16(),
5705251883Speter** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
5706251883Speter** set the return value of the application-defined function to be
5707251883Speter** a text string which is represented as UTF-8, UTF-16 native byte order,
5708251883Speter** UTF-16 little endian, or UTF-16 big endian, respectively.
5709274884Sbapt** ^The sqlite3_result_text64() interface sets the return value of an
5710274884Sbapt** application-defined function to be a text string in an encoding
5711274884Sbapt** specified by the fifth (and last) parameter, which must be one
5712274884Sbapt** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].
5713251883Speter** ^SQLite takes the text result from the application from
5714251883Speter** the 2nd parameter of the sqlite3_result_text* interfaces.
5715251883Speter** ^If the 3rd parameter to the sqlite3_result_text* interfaces
5716251883Speter** is negative, then SQLite takes result text from the 2nd parameter
5717251883Speter** through the first zero character.
5718251883Speter** ^If the 3rd parameter to the sqlite3_result_text* interfaces
5719251883Speter** is non-negative, then as many bytes (not characters) of the text
5720251883Speter** pointed to by the 2nd parameter are taken as the application-defined
5721251883Speter** function result.  If the 3rd parameter is non-negative, then it
5722251883Speter** must be the byte offset into the string where the NUL terminator would
5723251883Speter** appear if the string where NUL terminated.  If any NUL characters occur
5724251883Speter** in the string at a byte offset that is less than the value of the 3rd
5725251883Speter** parameter, then the resulting string will contain embedded NULs and the
5726251883Speter** result of expressions operating on strings with embedded NULs is undefined.
5727251883Speter** ^If the 4th parameter to the sqlite3_result_text* interfaces
5728251883Speter** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
5729251883Speter** function as the destructor on the text or BLOB result when it has
5730251883Speter** finished using that result.
5731251883Speter** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
5732251883Speter** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
5733251883Speter** assumes that the text or BLOB result is in constant space and does not
5734251883Speter** copy the content of the parameter nor call a destructor on the content
5735251883Speter** when it has finished using that result.
5736251883Speter** ^If the 4th parameter to the sqlite3_result_text* interfaces
5737251883Speter** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
5738322444Speter** then SQLite makes a copy of the result into space obtained
5739251883Speter** from [sqlite3_malloc()] before it returns.
5740251883Speter**
5741362190Scy** ^For the sqlite3_result_text16(), sqlite3_result_text16le(), and
5742362190Scy** sqlite3_result_text16be() routines, and for sqlite3_result_text64()
5743362190Scy** when the encoding is not UTF8, if the input UTF16 begins with a
5744362190Scy** byte-order mark (BOM, U+FEFF) then the BOM is removed from the
5745362190Scy** string and the rest of the string is interpreted according to the
5746362190Scy** byte-order specified by the BOM.  ^The byte-order specified by
5747362190Scy** the BOM at the beginning of the text overrides the byte-order
5748362190Scy** specified by the interface procedure.  ^So, for example, if
5749362190Scy** sqlite3_result_text16le() is invoked with text that begins
5750362190Scy** with bytes 0xfe, 0xff (a big-endian byte-order mark) then the
5751362190Scy** first two bytes of input are skipped and the remaining input
5752362190Scy** is interpreted as UTF16BE text.
5753362190Scy**
5754362190Scy** ^For UTF16 input text to the sqlite3_result_text16(),
5755362190Scy** sqlite3_result_text16be(), sqlite3_result_text16le(), and
5756362190Scy** sqlite3_result_text64() routines, if the text contains invalid
5757362190Scy** UTF16 characters, the invalid characters might be converted
5758362190Scy** into the unicode replacement character, U+FFFD.
5759362190Scy**
5760251883Speter** ^The sqlite3_result_value() interface sets the result of
5761286510Speter** the application-defined function to be a copy of the
5762251883Speter** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
5763251883Speter** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
5764251883Speter** so that the [sqlite3_value] specified in the parameter may change or
5765251883Speter** be deallocated after sqlite3_result_value() returns without harm.
5766251883Speter** ^A [protected sqlite3_value] object may always be used where an
5767251883Speter** [unprotected sqlite3_value] object is required, so either
5768251883Speter** kind of [sqlite3_value] object can be used with this interface.
5769251883Speter**
5770322444Speter** ^The sqlite3_result_pointer(C,P,T,D) interface sets the result to an
5771322444Speter** SQL NULL value, just like [sqlite3_result_null(C)], except that it
5772366076Scy** also associates the host-language pointer P or type T with that
5773322444Speter** NULL value such that the pointer can be retrieved within an
5774322444Speter** [application-defined SQL function] using [sqlite3_value_pointer()].
5775322444Speter** ^If the D parameter is not NULL, then it is a pointer to a destructor
5776322444Speter** for the P parameter.  ^SQLite invokes D with P as its only argument
5777322444Speter** when SQLite is finished with P.  The T parameter should be a static
5778322444Speter** string and preferably a string literal. The sqlite3_result_pointer()
5779322444Speter** routine is part of the [pointer passing interface] added for SQLite 3.20.0.
5780322444Speter**
5781251883Speter** If these routines are called from within the different thread
5782251883Speter** than the one containing the application-defined function that received
5783251883Speter** the [sqlite3_context] pointer, the results are undefined.
5784251883Speter*/
5785322444SpeterSQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
5786322444SpeterSQLITE_API void sqlite3_result_blob64(sqlite3_context*,const void*,
5787282328Sbapt                           sqlite3_uint64,void(*)(void*));
5788322444SpeterSQLITE_API void sqlite3_result_double(sqlite3_context*, double);
5789322444SpeterSQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
5790322444SpeterSQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
5791322444SpeterSQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
5792322444SpeterSQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
5793322444SpeterSQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
5794322444SpeterSQLITE_API void sqlite3_result_int(sqlite3_context*, int);
5795322444SpeterSQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
5796322444SpeterSQLITE_API void sqlite3_result_null(sqlite3_context*);
5797322444SpeterSQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
5798322444SpeterSQLITE_API void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
5799274884Sbapt                           void(*)(void*), unsigned char encoding);
5800322444SpeterSQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
5801322444SpeterSQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
5802322444SpeterSQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
5803322444SpeterSQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
5804322444SpeterSQLITE_API void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
5805322444SpeterSQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
5806322444SpeterSQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
5807251883Speter
5808298161Sbapt
5809251883Speter/*
5810298161Sbapt** CAPI3REF: Setting The Subtype Of An SQL Function
5811298161Sbapt** METHOD: sqlite3_context
5812298161Sbapt**
5813298161Sbapt** The sqlite3_result_subtype(C,T) function causes the subtype of
5814366076Scy** the result from the [application-defined SQL function] with
5815366076Scy** [sqlite3_context] C to be the value T.  Only the lower 8 bits
5816298161Sbapt** of the subtype T are preserved in current versions of SQLite;
5817298161Sbapt** higher order bits are discarded.
5818298161Sbapt** The number of subtype bytes preserved by SQLite might increase
5819298161Sbapt** in future releases of SQLite.
5820298161Sbapt*/
5821322444SpeterSQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
5822298161Sbapt
5823298161Sbapt/*
5824251883Speter** CAPI3REF: Define New Collating Sequences
5825286510Speter** METHOD: sqlite3
5826251883Speter**
5827251883Speter** ^These functions add, remove, or modify a [collation] associated
5828251883Speter** with the [database connection] specified as the first argument.
5829251883Speter**
5830251883Speter** ^The name of the collation is a UTF-8 string
5831251883Speter** for sqlite3_create_collation() and sqlite3_create_collation_v2()
5832251883Speter** and a UTF-16 string in native byte order for sqlite3_create_collation16().
5833251883Speter** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
5834251883Speter** considered to be the same name.
5835251883Speter**
5836251883Speter** ^(The third argument (eTextRep) must be one of the constants:
5837251883Speter** <ul>
5838251883Speter** <li> [SQLITE_UTF8],
5839251883Speter** <li> [SQLITE_UTF16LE],
5840251883Speter** <li> [SQLITE_UTF16BE],
5841251883Speter** <li> [SQLITE_UTF16], or
5842251883Speter** <li> [SQLITE_UTF16_ALIGNED].
5843251883Speter** </ul>)^
5844251883Speter** ^The eTextRep argument determines the encoding of strings passed
5845361456Scy** to the collating function callback, xCompare.
5846251883Speter** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
5847251883Speter** force strings to be UTF16 with native byte order.
5848251883Speter** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
5849251883Speter** on an even byte address.
5850251883Speter**
5851251883Speter** ^The fourth argument, pArg, is an application data pointer that is passed
5852251883Speter** through as the first argument to the collating function callback.
5853251883Speter**
5854361456Scy** ^The fifth argument, xCompare, is a pointer to the collating function.
5855251883Speter** ^Multiple collating functions can be registered using the same name but
5856251883Speter** with different eTextRep parameters and SQLite will use whichever
5857251883Speter** function requires the least amount of data transformation.
5858361456Scy** ^If the xCompare argument is NULL then the collating function is
5859251883Speter** deleted.  ^When all collating functions having the same name are deleted,
5860251883Speter** that collation is no longer usable.
5861251883Speter**
5862366076Scy** ^The collating function callback is invoked with a copy of the pArg
5863251883Speter** application data pointer and with two strings in the encoding specified
5864361456Scy** by the eTextRep argument.  The two integer parameters to the collating
5865361456Scy** function callback are the length of the two strings, in bytes. The collating
5866361456Scy** function must return an integer that is negative, zero, or positive
5867251883Speter** if the first string is less than, equal to, or greater than the second,
5868251883Speter** respectively.  A collating function must always return the same answer
5869251883Speter** given the same inputs.  If two or more collating functions are registered
5870251883Speter** to the same collation name (using different eTextRep values) then all
5871251883Speter** must give an equivalent answer when invoked with equivalent strings.
5872251883Speter** The collating function must obey the following properties for all
5873251883Speter** strings A, B, and C:
5874251883Speter**
5875251883Speter** <ol>
5876251883Speter** <li> If A==B then B==A.
5877251883Speter** <li> If A==B and B==C then A==C.
5878251883Speter** <li> If A&lt;B THEN B&gt;A.
5879251883Speter** <li> If A&lt;B and B&lt;C then A&lt;C.
5880251883Speter** </ol>
5881251883Speter**
5882251883Speter** If a collating function fails any of the above constraints and that
5883361456Scy** collating function is registered and used, then the behavior of SQLite
5884251883Speter** is undefined.
5885251883Speter**
5886251883Speter** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
5887251883Speter** with the addition that the xDestroy callback is invoked on pArg when
5888251883Speter** the collating function is deleted.
5889251883Speter** ^Collating functions are deleted when they are overridden by later
5890251883Speter** calls to the collation creation functions or when the
5891251883Speter** [database connection] is closed using [sqlite3_close()].
5892251883Speter**
5893366076Scy** ^The xDestroy callback is <u>not</u> called if the
5894251883Speter** sqlite3_create_collation_v2() function fails.  Applications that invoke
5895366076Scy** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
5896251883Speter** check the return code and dispose of the application data pointer
5897251883Speter** themselves rather than expecting SQLite to deal with it for them.
5898366076Scy** This is different from every other SQLite interface.  The inconsistency
5899366076Scy** is unfortunate but cannot be changed without breaking backwards
5900251883Speter** compatibility.
5901251883Speter**
5902251883Speter** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
5903251883Speter*/
5904322444SpeterSQLITE_API int sqlite3_create_collation(
5905366076Scy  sqlite3*,
5906366076Scy  const char *zName,
5907366076Scy  int eTextRep,
5908251883Speter  void *pArg,
5909251883Speter  int(*xCompare)(void*,int,const void*,int,const void*)
5910251883Speter);
5911322444SpeterSQLITE_API int sqlite3_create_collation_v2(
5912366076Scy  sqlite3*,
5913366076Scy  const char *zName,
5914366076Scy  int eTextRep,
5915251883Speter  void *pArg,
5916251883Speter  int(*xCompare)(void*,int,const void*,int,const void*),
5917251883Speter  void(*xDestroy)(void*)
5918251883Speter);
5919322444SpeterSQLITE_API int sqlite3_create_collation16(
5920366076Scy  sqlite3*,
5921251883Speter  const void *zName,
5922366076Scy  int eTextRep,
5923251883Speter  void *pArg,
5924251883Speter  int(*xCompare)(void*,int,const void*,int,const void*)
5925251883Speter);
5926251883Speter
5927251883Speter/*
5928251883Speter** CAPI3REF: Collation Needed Callbacks
5929286510Speter** METHOD: sqlite3
5930251883Speter**
5931251883Speter** ^To avoid having to register all collation sequences before a database
5932251883Speter** can be used, a single callback function may be registered with the
5933251883Speter** [database connection] to be invoked whenever an undefined collation
5934251883Speter** sequence is required.
5935251883Speter**
5936251883Speter** ^If the function is registered using the sqlite3_collation_needed() API,
5937251883Speter** then it is passed the names of undefined collation sequences as strings
5938251883Speter** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
5939251883Speter** the names are passed as UTF-16 in machine native byte order.
5940251883Speter** ^A call to either function replaces the existing collation-needed callback.
5941251883Speter**
5942251883Speter** ^(When the callback is invoked, the first argument passed is a copy
5943251883Speter** of the second argument to sqlite3_collation_needed() or
5944251883Speter** sqlite3_collation_needed16().  The second argument is the database
5945251883Speter** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
5946251883Speter** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
5947251883Speter** sequence function required.  The fourth parameter is the name of the
5948251883Speter** required collation sequence.)^
5949251883Speter**
5950251883Speter** The callback function should register the desired collation using
5951251883Speter** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
5952251883Speter** [sqlite3_create_collation_v2()].
5953251883Speter*/
5954322444SpeterSQLITE_API int sqlite3_collation_needed(
5955366076Scy  sqlite3*,
5956366076Scy  void*,
5957251883Speter  void(*)(void*,sqlite3*,int eTextRep,const char*)
5958251883Speter);
5959322444SpeterSQLITE_API int sqlite3_collation_needed16(
5960366076Scy  sqlite3*,
5961251883Speter  void*,
5962251883Speter  void(*)(void*,sqlite3*,int eTextRep,const void*)
5963251883Speter);
5964251883Speter
5965251883Speter#ifdef SQLITE_ENABLE_CEROD
5966251883Speter/*
5967366076Scy** Specify the activation key for a CEROD database.  Unless
5968251883Speter** activated, none of the CEROD routines will work.
5969251883Speter*/
5970322444SpeterSQLITE_API void sqlite3_activate_cerod(
5971251883Speter  const char *zPassPhrase        /* Activation phrase */
5972251883Speter);
5973251883Speter#endif
5974251883Speter
5975251883Speter/*
5976251883Speter** CAPI3REF: Suspend Execution For A Short Time
5977251883Speter**
5978251883Speter** The sqlite3_sleep() function causes the current thread to suspend execution
5979251883Speter** for at least a number of milliseconds specified in its parameter.
5980251883Speter**
5981251883Speter** If the operating system does not support sleep requests with
5982251883Speter** millisecond time resolution, then the time will be rounded up to
5983251883Speter** the nearest second. The number of milliseconds of sleep actually
5984251883Speter** requested from the operating system is returned.
5985251883Speter**
5986251883Speter** ^SQLite implements this interface by calling the xSleep()
5987251883Speter** method of the default [sqlite3_vfs] object.  If the xSleep() method
5988251883Speter** of the default VFS is not implemented correctly, or not implemented at
5989251883Speter** all, then the behavior of sqlite3_sleep() may deviate from the description
5990251883Speter** in the previous paragraphs.
5991251883Speter*/
5992322444SpeterSQLITE_API int sqlite3_sleep(int);
5993251883Speter
5994251883Speter/*
5995251883Speter** CAPI3REF: Name Of The Folder Holding Temporary Files
5996251883Speter**
5997251883Speter** ^(If this global variable is made to point to a string which is
5998251883Speter** the name of a folder (a.k.a. directory), then all temporary files
5999251883Speter** created by SQLite when using a built-in [sqlite3_vfs | VFS]
6000251883Speter** will be placed in that directory.)^  ^If this variable
6001251883Speter** is a NULL pointer, then SQLite performs a search for an appropriate
6002251883Speter** temporary file directory.
6003251883Speter**
6004274884Sbapt** Applications are strongly discouraged from using this global variable.
6005274884Sbapt** It is required to set a temporary folder on Windows Runtime (WinRT).
6006274884Sbapt** But for all other platforms, it is highly recommended that applications
6007274884Sbapt** neither read nor write this variable.  This global variable is a relic
6008274884Sbapt** that exists for backwards compatibility of legacy applications and should
6009274884Sbapt** be avoided in new projects.
6010274884Sbapt**
6011251883Speter** It is not safe to read or modify this variable in more than one
6012251883Speter** thread at a time.  It is not safe to read or modify this variable
6013251883Speter** if a [database connection] is being used at the same time in a separate
6014251883Speter** thread.
6015251883Speter** It is intended that this variable be set once
6016251883Speter** as part of process initialization and before any SQLite interface
6017251883Speter** routines have been called and that this variable remain unchanged
6018251883Speter** thereafter.
6019251883Speter**
6020251883Speter** ^The [temp_store_directory pragma] may modify this variable and cause
6021251883Speter** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
6022251883Speter** the [temp_store_directory pragma] always assumes that any string
6023366076Scy** that this variable points to is held in memory obtained from
6024251883Speter** [sqlite3_malloc] and the pragma may attempt to free that memory
6025251883Speter** using [sqlite3_free].
6026251883Speter** Hence, if this variable is modified directly, either it should be
6027251883Speter** made NULL or made to point to memory obtained from [sqlite3_malloc]
6028251883Speter** or else the use of the [temp_store_directory pragma] should be avoided.
6029274884Sbapt** Except when requested by the [temp_store_directory pragma], SQLite
6030274884Sbapt** does not free the memory that sqlite3_temp_directory points to.  If
6031274884Sbapt** the application wants that memory to be freed, it must do
6032274884Sbapt** so itself, taking care to only do so after all [database connection]
6033274884Sbapt** objects have been destroyed.
6034251883Speter**
6035251883Speter** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
6036251883Speter** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
6037251883Speter** features that require the use of temporary files may fail.  Here is an
6038251883Speter** example of how to do this using C++ with the Windows Runtime:
6039251883Speter**
6040251883Speter** <blockquote><pre>
6041251883Speter** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
6042251883Speter** &nbsp;     TemporaryFolder->Path->Data();
6043251883Speter** char zPathBuf&#91;MAX_PATH + 1&#93;;
6044251883Speter** memset(zPathBuf, 0, sizeof(zPathBuf));
6045251883Speter** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
6046251883Speter** &nbsp;     NULL, NULL);
6047251883Speter** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
6048251883Speter** </pre></blockquote>
6049251883Speter*/
6050251883SpeterSQLITE_API SQLITE_EXTERN char *sqlite3_temp_directory;
6051251883Speter
6052251883Speter/*
6053251883Speter** CAPI3REF: Name Of The Folder Holding Database Files
6054251883Speter**
6055251883Speter** ^(If this global variable is made to point to a string which is
6056251883Speter** the name of a folder (a.k.a. directory), then all database files
6057251883Speter** specified with a relative pathname and created or accessed by
6058251883Speter** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
6059251883Speter** to be relative to that directory.)^ ^If this variable is a NULL
6060251883Speter** pointer, then SQLite assumes that all database files specified
6061251883Speter** with a relative pathname are relative to the current directory
6062251883Speter** for the process.  Only the windows VFS makes use of this global
6063251883Speter** variable; it is ignored by the unix VFS.
6064251883Speter**
6065251883Speter** Changing the value of this variable while a database connection is
6066251883Speter** open can result in a corrupt database.
6067251883Speter**
6068251883Speter** It is not safe to read or modify this variable in more than one
6069251883Speter** thread at a time.  It is not safe to read or modify this variable
6070251883Speter** if a [database connection] is being used at the same time in a separate
6071251883Speter** thread.
6072251883Speter** It is intended that this variable be set once
6073251883Speter** as part of process initialization and before any SQLite interface
6074251883Speter** routines have been called and that this variable remain unchanged
6075251883Speter** thereafter.
6076251883Speter**
6077251883Speter** ^The [data_store_directory pragma] may modify this variable and cause
6078251883Speter** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
6079251883Speter** the [data_store_directory pragma] always assumes that any string
6080366076Scy** that this variable points to is held in memory obtained from
6081251883Speter** [sqlite3_malloc] and the pragma may attempt to free that memory
6082251883Speter** using [sqlite3_free].
6083251883Speter** Hence, if this variable is modified directly, either it should be
6084251883Speter** made NULL or made to point to memory obtained from [sqlite3_malloc]
6085251883Speter** or else the use of the [data_store_directory pragma] should be avoided.
6086251883Speter*/
6087251883SpeterSQLITE_API SQLITE_EXTERN char *sqlite3_data_directory;
6088251883Speter
6089251883Speter/*
6090342292Scy** CAPI3REF: Win32 Specific Interface
6091342292Scy**
6092342292Scy** These interfaces are available only on Windows.  The
6093342292Scy** [sqlite3_win32_set_directory] interface is used to set the value associated
6094342292Scy** with the [sqlite3_temp_directory] or [sqlite3_data_directory] variable, to
6095342292Scy** zValue, depending on the value of the type parameter.  The zValue parameter
6096342292Scy** should be NULL to cause the previous value to be freed via [sqlite3_free];
6097342292Scy** a non-NULL value will be copied into memory obtained from [sqlite3_malloc]
6098342292Scy** prior to being used.  The [sqlite3_win32_set_directory] interface returns
6099342292Scy** [SQLITE_OK] to indicate success, [SQLITE_ERROR] if the type is unsupported,
6100342292Scy** or [SQLITE_NOMEM] if memory could not be allocated.  The value of the
6101342292Scy** [sqlite3_data_directory] variable is intended to act as a replacement for
6102342292Scy** the current directory on the sub-platforms of Win32 where that concept is
6103342292Scy** not present, e.g. WinRT and UWP.  The [sqlite3_win32_set_directory8] and
6104342292Scy** [sqlite3_win32_set_directory16] interfaces behave exactly the same as the
6105342292Scy** sqlite3_win32_set_directory interface except the string parameter must be
6106342292Scy** UTF-8 or UTF-16, respectively.
6107342292Scy*/
6108342292ScySQLITE_API int sqlite3_win32_set_directory(
6109342292Scy  unsigned long type, /* Identifier for directory being set or reset */
6110342292Scy  void *zValue        /* New value for directory being set or reset */
6111342292Scy);
6112342292ScySQLITE_API int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
6113342292ScySQLITE_API int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
6114342292Scy
6115342292Scy/*
6116342292Scy** CAPI3REF: Win32 Directory Types
6117342292Scy**
6118342292Scy** These macros are only available on Windows.  They define the allowed values
6119342292Scy** for the type argument to the [sqlite3_win32_set_directory] interface.
6120342292Scy*/
6121342292Scy#define SQLITE_WIN32_DATA_DIRECTORY_TYPE  1
6122342292Scy#define SQLITE_WIN32_TEMP_DIRECTORY_TYPE  2
6123342292Scy
6124342292Scy/*
6125251883Speter** CAPI3REF: Test For Auto-Commit Mode
6126251883Speter** KEYWORDS: {autocommit mode}
6127286510Speter** METHOD: sqlite3
6128251883Speter**
6129251883Speter** ^The sqlite3_get_autocommit() interface returns non-zero or
6130251883Speter** zero if the given database connection is or is not in autocommit mode,
6131251883Speter** respectively.  ^Autocommit mode is on by default.
6132251883Speter** ^Autocommit mode is disabled by a [BEGIN] statement.
6133251883Speter** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
6134251883Speter**
6135251883Speter** If certain kinds of errors occur on a statement within a multi-statement
6136251883Speter** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
6137251883Speter** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
6138251883Speter** transaction might be rolled back automatically.  The only way to
6139251883Speter** find out whether SQLite automatically rolled back the transaction after
6140251883Speter** an error is to use this function.
6141251883Speter**
6142251883Speter** If another thread changes the autocommit status of the database
6143251883Speter** connection while this routine is running, then the return value
6144251883Speter** is undefined.
6145251883Speter*/
6146322444SpeterSQLITE_API int sqlite3_get_autocommit(sqlite3*);
6147251883Speter
6148251883Speter/*
6149251883Speter** CAPI3REF: Find The Database Handle Of A Prepared Statement
6150286510Speter** METHOD: sqlite3_stmt
6151251883Speter**
6152251883Speter** ^The sqlite3_db_handle interface returns the [database connection] handle
6153251883Speter** to which a [prepared statement] belongs.  ^The [database connection]
6154251883Speter** returned by sqlite3_db_handle is the same [database connection]
6155251883Speter** that was the first argument
6156251883Speter** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
6157251883Speter** create the statement in the first place.
6158251883Speter*/
6159322444SpeterSQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
6160251883Speter
6161251883Speter/*
6162251883Speter** CAPI3REF: Return The Filename For A Database Connection
6163286510Speter** METHOD: sqlite3
6164251883Speter**
6165361456Scy** ^The sqlite3_db_filename(D,N) interface returns a pointer to the filename
6166361456Scy** associated with database N of connection D.
6167361456Scy** ^If there is no attached database N on the database
6168251883Speter** connection D, or if database N is a temporary or in-memory database, then
6169347347Scy** this function will return either a NULL pointer or an empty string.
6170251883Speter**
6171361456Scy** ^The string value returned by this routine is owned and managed by
6172361456Scy** the database connection.  ^The value will be valid until the database N
6173361456Scy** is [DETACH]-ed or until the database connection closes.
6174361456Scy**
6175251883Speter** ^The filename returned by this function is the output of the
6176251883Speter** xFullPathname method of the [VFS].  ^In other words, the filename
6177251883Speter** will be an absolute pathname, even if the filename used
6178251883Speter** to open the database originally was a URI or relative pathname.
6179361456Scy**
6180361456Scy** If the filename pointer returned by this routine is not NULL, then it
6181361456Scy** can be used as the filename input parameter to these routines:
6182361456Scy** <ul>
6183361456Scy** <li> [sqlite3_uri_parameter()]
6184361456Scy** <li> [sqlite3_uri_boolean()]
6185361456Scy** <li> [sqlite3_uri_int64()]
6186361456Scy** <li> [sqlite3_filename_database()]
6187361456Scy** <li> [sqlite3_filename_journal()]
6188361456Scy** <li> [sqlite3_filename_wal()]
6189361456Scy** </ul>
6190251883Speter*/
6191322444SpeterSQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
6192251883Speter
6193251883Speter/*
6194251883Speter** CAPI3REF: Determine if a database is read-only
6195286510Speter** METHOD: sqlite3
6196251883Speter**
6197251883Speter** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
6198251883Speter** of connection D is read-only, 0 if it is read/write, or -1 if N is not
6199251883Speter** the name of a database on connection D.
6200251883Speter*/
6201322444SpeterSQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
6202251883Speter
6203251883Speter/*
6204369950Scy** CAPI3REF: Determine the transaction state of a database
6205369950Scy** METHOD: sqlite3
6206369950Scy**
6207369950Scy** ^The sqlite3_txn_state(D,S) interface returns the current
6208369950Scy** [transaction state] of schema S in database connection D.  ^If S is NULL,
6209369950Scy** then the highest transaction state of any schema on database connection D
6210369950Scy** is returned.  Transaction states are (in order of lowest to highest):
6211369950Scy** <ol>
6212369950Scy** <li value="0"> SQLITE_TXN_NONE
6213369950Scy** <li value="1"> SQLITE_TXN_READ
6214369950Scy** <li value="2"> SQLITE_TXN_WRITE
6215369950Scy** </ol>
6216369950Scy** ^If the S argument to sqlite3_txn_state(D,S) is not the name of
6217369950Scy** a valid schema, then -1 is returned.
6218369950Scy*/
6219369950ScySQLITE_API int sqlite3_txn_state(sqlite3*,const char *zSchema);
6220369950Scy
6221369950Scy/*
6222369950Scy** CAPI3REF: Allowed return values from [sqlite3_txn_state()]
6223369950Scy** KEYWORDS: {transaction state}
6224369950Scy**
6225369950Scy** These constants define the current transaction state of a database file.
6226369950Scy** ^The [sqlite3_txn_state(D,S)] interface returns one of these
6227369950Scy** constants in order to describe the transaction state of schema S
6228369950Scy** in [database connection] D.
6229369950Scy**
6230369950Scy** <dl>
6231369950Scy** [[SQLITE_TXN_NONE]] <dt>SQLITE_TXN_NONE</dt>
6232369950Scy** <dd>The SQLITE_TXN_NONE state means that no transaction is currently
6233369950Scy** pending.</dd>
6234369950Scy**
6235369950Scy** [[SQLITE_TXN_READ]] <dt>SQLITE_TXN_READ</dt>
6236369950Scy** <dd>The SQLITE_TXN_READ state means that the database is currently
6237369950Scy** in a read transaction.  Content has been read from the database file
6238369950Scy** but nothing in the database file has changed.  The transaction state
6239369950Scy** will advanced to SQLITE_TXN_WRITE if any changes occur and there are
6240369950Scy** no other conflicting concurrent write transactions.  The transaction
6241369950Scy** state will revert to SQLITE_TXN_NONE following a [ROLLBACK] or
6242369950Scy** [COMMIT].</dd>
6243369950Scy**
6244369950Scy** [[SQLITE_TXN_WRITE]] <dt>SQLITE_TXN_WRITE</dt>
6245369950Scy** <dd>The SQLITE_TXN_WRITE state means that the database is currently
6246369950Scy** in a write transaction.  Content has been written to the database file
6247369950Scy** but has not yet committed.  The transaction state will change to
6248369950Scy** to SQLITE_TXN_NONE at the next [ROLLBACK] or [COMMIT].</dd>
6249369950Scy*/
6250369950Scy#define SQLITE_TXN_NONE  0
6251369950Scy#define SQLITE_TXN_READ  1
6252369950Scy#define SQLITE_TXN_WRITE 2
6253369950Scy
6254369950Scy/*
6255251883Speter** CAPI3REF: Find the next prepared statement
6256286510Speter** METHOD: sqlite3
6257251883Speter**
6258251883Speter** ^This interface returns a pointer to the next [prepared statement] after
6259251883Speter** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
6260251883Speter** then this interface returns a pointer to the first prepared statement
6261251883Speter** associated with the database connection pDb.  ^If no prepared statement
6262251883Speter** satisfies the conditions of this routine, it returns NULL.
6263251883Speter**
6264251883Speter** The [database connection] pointer D in a call to
6265251883Speter** [sqlite3_next_stmt(D,S)] must refer to an open database
6266251883Speter** connection and in particular must not be a NULL pointer.
6267251883Speter*/
6268322444SpeterSQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
6269251883Speter
6270251883Speter/*
6271251883Speter** CAPI3REF: Commit And Rollback Notification Callbacks
6272286510Speter** METHOD: sqlite3
6273251883Speter**
6274251883Speter** ^The sqlite3_commit_hook() interface registers a callback
6275251883Speter** function to be invoked whenever a transaction is [COMMIT | committed].
6276251883Speter** ^Any callback set by a previous call to sqlite3_commit_hook()
6277251883Speter** for the same database connection is overridden.
6278251883Speter** ^The sqlite3_rollback_hook() interface registers a callback
6279251883Speter** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
6280251883Speter** ^Any callback set by a previous call to sqlite3_rollback_hook()
6281251883Speter** for the same database connection is overridden.
6282251883Speter** ^The pArg argument is passed through to the callback.
6283251883Speter** ^If the callback on a commit hook function returns non-zero,
6284251883Speter** then the commit is converted into a rollback.
6285251883Speter**
6286251883Speter** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
6287251883Speter** return the P argument from the previous call of the same function
6288251883Speter** on the same [database connection] D, or NULL for
6289251883Speter** the first call for each function on D.
6290251883Speter**
6291251883Speter** The commit and rollback hook callbacks are not reentrant.
6292251883Speter** The callback implementation must not do anything that will modify
6293251883Speter** the database connection that invoked the callback.  Any actions
6294251883Speter** to modify the database connection must be deferred until after the
6295251883Speter** completion of the [sqlite3_step()] call that triggered the commit
6296251883Speter** or rollback hook in the first place.
6297251883Speter** Note that running any other SQL statements, including SELECT statements,
6298251883Speter** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
6299251883Speter** the database connections for the meaning of "modify" in this paragraph.
6300251883Speter**
6301251883Speter** ^Registering a NULL function disables the callback.
6302251883Speter**
6303251883Speter** ^When the commit hook callback routine returns zero, the [COMMIT]
6304251883Speter** operation is allowed to continue normally.  ^If the commit hook
6305251883Speter** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
6306251883Speter** ^The rollback hook is invoked on a rollback that results from a commit
6307251883Speter** hook returning non-zero, just as it would be with any other rollback.
6308251883Speter**
6309251883Speter** ^For the purposes of this API, a transaction is said to have been
6310251883Speter** rolled back if an explicit "ROLLBACK" statement is executed, or
6311251883Speter** an error or constraint causes an implicit rollback to occur.
6312251883Speter** ^The rollback callback is not invoked if a transaction is
6313251883Speter** automatically rolled back because the database connection is closed.
6314251883Speter**
6315251883Speter** See also the [sqlite3_update_hook()] interface.
6316251883Speter*/
6317322444SpeterSQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
6318322444SpeterSQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
6319251883Speter
6320251883Speter/*
6321251883Speter** CAPI3REF: Data Change Notification Callbacks
6322286510Speter** METHOD: sqlite3
6323251883Speter**
6324251883Speter** ^The sqlite3_update_hook() interface registers a callback function
6325251883Speter** with the [database connection] identified by the first argument
6326269851Speter** to be invoked whenever a row is updated, inserted or deleted in
6327305002Scy** a [rowid table].
6328251883Speter** ^Any callback set by a previous call to this function
6329251883Speter** for the same database connection is overridden.
6330251883Speter**
6331251883Speter** ^The second argument is a pointer to the function to invoke when a
6332269851Speter** row is updated, inserted or deleted in a rowid table.
6333251883Speter** ^The first argument to the callback is a copy of the third argument
6334251883Speter** to sqlite3_update_hook().
6335251883Speter** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
6336251883Speter** or [SQLITE_UPDATE], depending on the operation that caused the callback
6337251883Speter** to be invoked.
6338251883Speter** ^The third and fourth arguments to the callback contain pointers to the
6339251883Speter** database and table name containing the affected row.
6340251883Speter** ^The final callback parameter is the [rowid] of the row.
6341251883Speter** ^In the case of an update, this is the [rowid] after the update takes place.
6342251883Speter**
6343251883Speter** ^(The update hook is not invoked when internal system tables are
6344366076Scy** modified (i.e. sqlite_sequence).)^
6345269851Speter** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
6346251883Speter**
6347251883Speter** ^In the current implementation, the update hook
6348322444Speter** is not invoked when conflicting rows are deleted because of an
6349251883Speter** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
6350251883Speter** invoked when rows are deleted using the [truncate optimization].
6351251883Speter** The exceptions defined in this paragraph might change in a future
6352251883Speter** release of SQLite.
6353251883Speter**
6354251883Speter** The update hook implementation must not do anything that will modify
6355251883Speter** the database connection that invoked the update hook.  Any actions
6356251883Speter** to modify the database connection must be deferred until after the
6357251883Speter** completion of the [sqlite3_step()] call that triggered the update hook.
6358251883Speter** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
6359251883Speter** database connections for the meaning of "modify" in this paragraph.
6360251883Speter**
6361251883Speter** ^The sqlite3_update_hook(D,C,P) function
6362251883Speter** returns the P argument from the previous call
6363251883Speter** on the same [database connection] D, or NULL for
6364251883Speter** the first call on D.
6365251883Speter**
6366305002Scy** See also the [sqlite3_commit_hook()], [sqlite3_rollback_hook()],
6367305002Scy** and [sqlite3_preupdate_hook()] interfaces.
6368251883Speter*/
6369322444SpeterSQLITE_API void *sqlite3_update_hook(
6370366076Scy  sqlite3*,
6371251883Speter  void(*)(void *,int ,char const *,char const *,sqlite3_int64),
6372251883Speter  void*
6373251883Speter);
6374251883Speter
6375251883Speter/*
6376251883Speter** CAPI3REF: Enable Or Disable Shared Pager Cache
6377251883Speter**
6378251883Speter** ^(This routine enables or disables the sharing of the database cache
6379251883Speter** and schema data structures between [database connection | connections]
6380251883Speter** to the same database. Sharing is enabled if the argument is true
6381251883Speter** and disabled if the argument is false.)^
6382251883Speter**
6383251883Speter** ^Cache sharing is enabled and disabled for an entire process.
6384366076Scy** This is a change as of SQLite [version 3.5.0] ([dateof:3.5.0]).
6385322444Speter** In prior versions of SQLite,
6386251883Speter** sharing was enabled or disabled for each thread separately.
6387251883Speter**
6388251883Speter** ^(The cache sharing mode set by this interface effects all subsequent
6389251883Speter** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
6390361456Scy** Existing database connections continue to use the sharing mode
6391251883Speter** that was in effect at the time they were opened.)^
6392251883Speter**
6393251883Speter** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
6394251883Speter** successfully.  An [error code] is returned otherwise.)^
6395251883Speter**
6396361456Scy** ^Shared cache is disabled by default. It is recommended that it stay
6397361456Scy** that way.  In other words, do not use this routine.  This interface
6398361456Scy** continues to be provided for historical compatibility, but its use is
6399361456Scy** discouraged.  Any use of shared cache is discouraged.  If shared cache
6400361456Scy** must be used, it is recommended that shared cache only be enabled for
6401361456Scy** individual database connections using the [sqlite3_open_v2()] interface
6402361456Scy** with the [SQLITE_OPEN_SHAREDCACHE] flag.
6403251883Speter**
6404282328Sbapt** Note: This method is disabled on MacOS X 10.7 and iOS version 5.0
6405366076Scy** and will always return SQLITE_MISUSE. On those systems,
6406366076Scy** shared cache mode should be enabled per-database connection via
6407282328Sbapt** [sqlite3_open_v2()] with [SQLITE_OPEN_SHAREDCACHE].
6408282328Sbapt**
6409251883Speter** This interface is threadsafe on processors where writing a
6410251883Speter** 32-bit integer is atomic.
6411251883Speter**
6412251883Speter** See Also:  [SQLite Shared-Cache Mode]
6413251883Speter*/
6414322444SpeterSQLITE_API int sqlite3_enable_shared_cache(int);
6415251883Speter
6416251883Speter/*
6417251883Speter** CAPI3REF: Attempt To Free Heap Memory
6418251883Speter**
6419251883Speter** ^The sqlite3_release_memory() interface attempts to free N bytes
6420251883Speter** of heap memory by deallocating non-essential memory allocations
6421251883Speter** held by the database library.   Memory used to cache database
6422251883Speter** pages to improve performance is an example of non-essential memory.
6423251883Speter** ^sqlite3_release_memory() returns the number of bytes actually freed,
6424251883Speter** which might be more or less than the amount requested.
6425251883Speter** ^The sqlite3_release_memory() routine is a no-op returning zero
6426251883Speter** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
6427251883Speter**
6428251883Speter** See also: [sqlite3_db_release_memory()]
6429251883Speter*/
6430322444SpeterSQLITE_API int sqlite3_release_memory(int);
6431251883Speter
6432251883Speter/*
6433251883Speter** CAPI3REF: Free Memory Used By A Database Connection
6434286510Speter** METHOD: sqlite3
6435251883Speter**
6436251883Speter** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
6437251883Speter** memory as possible from database connection D. Unlike the
6438269851Speter** [sqlite3_release_memory()] interface, this interface is in effect even
6439269851Speter** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
6440251883Speter** omitted.
6441251883Speter**
6442251883Speter** See also: [sqlite3_release_memory()]
6443251883Speter*/
6444322444SpeterSQLITE_API int sqlite3_db_release_memory(sqlite3*);
6445251883Speter
6446251883Speter/*
6447251883Speter** CAPI3REF: Impose A Limit On Heap Size
6448251883Speter**
6449361456Scy** These interfaces impose limits on the amount of heap memory that will be
6450361456Scy** by all database connections within a single process.
6451361456Scy**
6452251883Speter** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
6453251883Speter** soft limit on the amount of heap memory that may be allocated by SQLite.
6454251883Speter** ^SQLite strives to keep heap memory utilization below the soft heap
6455251883Speter** limit by reducing the number of pages held in the page cache
6456251883Speter** as heap memory usages approaches the limit.
6457251883Speter** ^The soft heap limit is "soft" because even though SQLite strives to stay
6458251883Speter** below the limit, it will exceed the limit rather than generate
6459366076Scy** an [SQLITE_NOMEM] error.  In other words, the soft heap limit
6460251883Speter** is advisory only.
6461251883Speter**
6462361456Scy** ^The sqlite3_hard_heap_limit64(N) interface sets a hard upper bound of
6463361456Scy** N bytes on the amount of memory that will be allocated.  ^The
6464361456Scy** sqlite3_hard_heap_limit64(N) interface is similar to
6465361456Scy** sqlite3_soft_heap_limit64(N) except that memory allocations will fail
6466361456Scy** when the hard heap limit is reached.
6467361456Scy**
6468361456Scy** ^The return value from both sqlite3_soft_heap_limit64() and
6469361456Scy** sqlite3_hard_heap_limit64() is the size of
6470361456Scy** the heap limit prior to the call, or negative in the case of an
6471251883Speter** error.  ^If the argument N is negative
6472361456Scy** then no change is made to the heap limit.  Hence, the current
6473361456Scy** size of heap limits can be determined by invoking
6474361456Scy** sqlite3_soft_heap_limit64(-1) or sqlite3_hard_heap_limit(-1).
6475251883Speter**
6476361456Scy** ^Setting the heap limits to zero disables the heap limiter mechanism.
6477251883Speter**
6478361456Scy** ^The soft heap limit may not be greater than the hard heap limit.
6479361456Scy** ^If the hard heap limit is enabled and if sqlite3_soft_heap_limit(N)
6480361456Scy** is invoked with a value of N that is greater than the hard heap limit,
6481361456Scy** the the soft heap limit is set to the value of the hard heap limit.
6482361456Scy** ^The soft heap limit is automatically enabled whenever the hard heap
6483361456Scy** limit is enabled. ^When sqlite3_hard_heap_limit64(N) is invoked and
6484361456Scy** the soft heap limit is outside the range of 1..N, then the soft heap
6485361456Scy** limit is set to N.  ^Invoking sqlite3_soft_heap_limit64(0) when the
6486361456Scy** hard heap limit is enabled makes the soft heap limit equal to the
6487361456Scy** hard heap limit.
6488361456Scy**
6489361456Scy** The memory allocation limits can also be adjusted using
6490361456Scy** [PRAGMA soft_heap_limit] and [PRAGMA hard_heap_limit].
6491361456Scy**
6492361456Scy** ^(The heap limits are not enforced in the current implementation
6493251883Speter** if one or more of following conditions are true:
6494251883Speter**
6495251883Speter** <ul>
6496361456Scy** <li> The limit value is set to zero.
6497251883Speter** <li> Memory accounting is disabled using a combination of the
6498251883Speter**      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
6499251883Speter**      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
6500251883Speter** <li> An alternative page cache implementation is specified using
6501251883Speter**      [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
6502251883Speter** <li> The page cache allocates from its own memory pool supplied
6503251883Speter**      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
6504251883Speter**      from the heap.
6505251883Speter** </ul>)^
6506251883Speter**
6507361456Scy** The circumstances under which SQLite will enforce the heap limits may
6508251883Speter** changes in future releases of SQLite.
6509251883Speter*/
6510322444SpeterSQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
6511361456ScySQLITE_API sqlite3_int64 sqlite3_hard_heap_limit64(sqlite3_int64 N);
6512251883Speter
6513251883Speter/*
6514251883Speter** CAPI3REF: Deprecated Soft Heap Limit Interface
6515251883Speter** DEPRECATED
6516251883Speter**
6517251883Speter** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
6518251883Speter** interface.  This routine is provided for historical compatibility
6519251883Speter** only.  All new applications should use the
6520251883Speter** [sqlite3_soft_heap_limit64()] interface rather than this one.
6521251883Speter*/
6522322444SpeterSQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
6523251883Speter
6524251883Speter
6525251883Speter/*
6526251883Speter** CAPI3REF: Extract Metadata About A Column Of A Table
6527286510Speter** METHOD: sqlite3
6528251883Speter**
6529282328Sbapt** ^(The sqlite3_table_column_metadata(X,D,T,C,....) routine returns
6530282328Sbapt** information about column C of table T in database D
6531282328Sbapt** on [database connection] X.)^  ^The sqlite3_table_column_metadata()
6532282328Sbapt** interface returns SQLITE_OK and fills in the non-NULL pointers in
6533282328Sbapt** the final five arguments with appropriate values if the specified
6534282328Sbapt** column exists.  ^The sqlite3_table_column_metadata() interface returns
6535361456Scy** SQLITE_ERROR if the specified column does not exist.
6536282328Sbapt** ^If the column-name parameter to sqlite3_table_column_metadata() is a
6537305002Scy** NULL pointer, then this routine simply checks for the existence of the
6538282328Sbapt** table and returns SQLITE_OK if the table exists and SQLITE_ERROR if it
6539322444Speter** does not.  If the table name parameter T in a call to
6540322444Speter** sqlite3_table_column_metadata(X,D,T,C,...) is NULL then the result is
6541322444Speter** undefined behavior.
6542251883Speter**
6543251883Speter** ^The column is identified by the second, third and fourth parameters to
6544282328Sbapt** this function. ^(The second parameter is either the name of the database
6545251883Speter** (i.e. "main", "temp", or an attached database) containing the specified
6546282328Sbapt** table or NULL.)^ ^If it is NULL, then all attached databases are searched
6547251883Speter** for the table using the same algorithm used by the database engine to
6548251883Speter** resolve unqualified table references.
6549251883Speter**
6550251883Speter** ^The third and fourth parameters to this function are the table and column
6551282328Sbapt** name of the desired column, respectively.
6552251883Speter**
6553251883Speter** ^Metadata is returned by writing to the memory locations passed as the 5th
6554251883Speter** and subsequent parameters to this function. ^Any of these arguments may be
6555251883Speter** NULL, in which case the corresponding element of metadata is omitted.
6556251883Speter**
6557251883Speter** ^(<blockquote>
6558251883Speter** <table border="1">
6559251883Speter** <tr><th> Parameter <th> Output<br>Type <th>  Description
6560251883Speter**
6561251883Speter** <tr><td> 5th <td> const char* <td> Data type
6562251883Speter** <tr><td> 6th <td> const char* <td> Name of default collation sequence
6563251883Speter** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
6564251883Speter** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
6565251883Speter** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
6566251883Speter** </table>
6567251883Speter** </blockquote>)^
6568251883Speter**
6569251883Speter** ^The memory pointed to by the character pointers returned for the
6570282328Sbapt** declaration type and collation sequence is valid until the next
6571251883Speter** call to any SQLite API function.
6572251883Speter**
6573251883Speter** ^If the specified table is actually a view, an [error code] is returned.
6574251883Speter**
6575366076Scy** ^If the specified column is "rowid", "oid" or "_rowid_" and the table
6576282328Sbapt** is not a [WITHOUT ROWID] table and an
6577251883Speter** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
6578251883Speter** parameters are set for the explicitly declared column. ^(If there is no
6579282328Sbapt** [INTEGER PRIMARY KEY] column, then the outputs
6580282328Sbapt** for the [rowid] are set as follows:
6581251883Speter**
6582251883Speter** <pre>
6583251883Speter**     data type: "INTEGER"
6584251883Speter**     collation sequence: "BINARY"
6585251883Speter**     not null: 0
6586251883Speter**     primary key: 1
6587251883Speter**     auto increment: 0
6588251883Speter** </pre>)^
6589251883Speter**
6590282328Sbapt** ^This function causes all database schemas to be read from disk and
6591282328Sbapt** parsed, if that has not already been done, and returns an error if
6592282328Sbapt** any errors are encountered while loading the schema.
6593251883Speter*/
6594322444SpeterSQLITE_API int sqlite3_table_column_metadata(
6595251883Speter  sqlite3 *db,                /* Connection handle */
6596251883Speter  const char *zDbName,        /* Database name or NULL */
6597251883Speter  const char *zTableName,     /* Table name */
6598251883Speter  const char *zColumnName,    /* Column name */
6599251883Speter  char const **pzDataType,    /* OUTPUT: Declared data type */
6600251883Speter  char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
6601251883Speter  int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
6602251883Speter  int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
6603251883Speter  int *pAutoinc               /* OUTPUT: True if column is auto-increment */
6604251883Speter);
6605251883Speter
6606251883Speter/*
6607251883Speter** CAPI3REF: Load An Extension
6608286510Speter** METHOD: sqlite3
6609251883Speter**
6610251883Speter** ^This interface loads an SQLite extension library from the named file.
6611251883Speter**
6612251883Speter** ^The sqlite3_load_extension() interface attempts to load an
6613251883Speter** [SQLite extension] library contained in the file zFile.  If
6614251883Speter** the file cannot be loaded directly, attempts are made to load
6615251883Speter** with various operating-system specific extensions added.
6616251883Speter** So for example, if "samplelib" cannot be loaded, then names like
6617251883Speter** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
6618251883Speter** be tried also.
6619251883Speter**
6620251883Speter** ^The entry point is zProc.
6621251883Speter** ^(zProc may be 0, in which case SQLite will try to come up with an
6622251883Speter** entry point name on its own.  It first tries "sqlite3_extension_init".
6623251883Speter** If that does not work, it constructs a name "sqlite3_X_init" where the
6624251883Speter** X is consists of the lower-case equivalent of all ASCII alphabetic
6625251883Speter** characters in the filename from the last "/" to the first following
6626251883Speter** "." and omitting any initial "lib".)^
6627251883Speter** ^The sqlite3_load_extension() interface returns
6628251883Speter** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
6629251883Speter** ^If an error occurs and pzErrMsg is not 0, then the
6630251883Speter** [sqlite3_load_extension()] interface shall attempt to
6631251883Speter** fill *pzErrMsg with error message text stored in memory
6632251883Speter** obtained from [sqlite3_malloc()]. The calling function
6633251883Speter** should free this memory by calling [sqlite3_free()].
6634251883Speter**
6635251883Speter** ^Extension loading must be enabled using
6636305002Scy** [sqlite3_enable_load_extension()] or
6637305002Scy** [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],1,NULL)
6638305002Scy** prior to calling this API,
6639251883Speter** otherwise an error will be returned.
6640251883Speter**
6641366076Scy** <b>Security warning:</b> It is recommended that the
6642305002Scy** [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method be used to enable only this
6643305002Scy** interface.  The use of the [sqlite3_enable_load_extension()] interface
6644305002Scy** should be avoided.  This will keep the SQL function [load_extension()]
6645305002Scy** disabled and prevent SQL injections from giving attackers
6646305002Scy** access to extension loading capabilities.
6647305002Scy**
6648251883Speter** See also the [load_extension() SQL function].
6649251883Speter*/
6650322444SpeterSQLITE_API int sqlite3_load_extension(
6651251883Speter  sqlite3 *db,          /* Load the extension into this database connection */
6652251883Speter  const char *zFile,    /* Name of the shared library containing extension */
6653251883Speter  const char *zProc,    /* Entry point.  Derived from zFile if 0 */
6654251883Speter  char **pzErrMsg       /* Put error message here if not 0 */
6655251883Speter);
6656251883Speter
6657251883Speter/*
6658251883Speter** CAPI3REF: Enable Or Disable Extension Loading
6659286510Speter** METHOD: sqlite3
6660251883Speter**
6661251883Speter** ^So as not to open security holes in older applications that are
6662251883Speter** unprepared to deal with [extension loading], and as a means of disabling
6663251883Speter** [extension loading] while evaluating user-entered SQL, the following API
6664251883Speter** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
6665251883Speter**
6666251883Speter** ^Extension loading is off by default.
6667251883Speter** ^Call the sqlite3_enable_load_extension() routine with onoff==1
6668251883Speter** to turn extension loading on and call it with onoff==0 to turn
6669251883Speter** it back off again.
6670305002Scy**
6671305002Scy** ^This interface enables or disables both the C-API
6672305002Scy** [sqlite3_load_extension()] and the SQL function [load_extension()].
6673305002Scy** ^(Use [sqlite3_db_config](db,[SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION],..)
6674305002Scy** to enable or disable only the C-API.)^
6675305002Scy**
6676305002Scy** <b>Security warning:</b> It is recommended that extension loading
6677361456Scy** be enabled using the [SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION] method
6678305002Scy** rather than this interface, so the [load_extension()] SQL function
6679305002Scy** remains disabled. This will prevent SQL injections from giving attackers
6680305002Scy** access to extension loading capabilities.
6681251883Speter*/
6682322444SpeterSQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
6683251883Speter
6684251883Speter/*
6685251883Speter** CAPI3REF: Automatically Load Statically Linked Extensions
6686251883Speter**
6687251883Speter** ^This interface causes the xEntryPoint() function to be invoked for
6688251883Speter** each new [database connection] that is created.  The idea here is that
6689251883Speter** xEntryPoint() is the entry point for a statically linked [SQLite extension]
6690251883Speter** that is to be automatically loaded into all new database connections.
6691251883Speter**
6692251883Speter** ^(Even though the function prototype shows that xEntryPoint() takes
6693251883Speter** no arguments and returns void, SQLite invokes xEntryPoint() with three
6694305002Scy** arguments and expects an integer result as if the signature of the
6695251883Speter** entry point where as follows:
6696251883Speter**
6697251883Speter** <blockquote><pre>
6698251883Speter** &nbsp;  int xEntryPoint(
6699251883Speter** &nbsp;    sqlite3 *db,
6700251883Speter** &nbsp;    const char **pzErrMsg,
6701251883Speter** &nbsp;    const struct sqlite3_api_routines *pThunk
6702251883Speter** &nbsp;  );
6703251883Speter** </pre></blockquote>)^
6704251883Speter**
6705251883Speter** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
6706251883Speter** point to an appropriate error message (obtained from [sqlite3_mprintf()])
6707251883Speter** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
6708251883Speter** is NULL before calling the xEntryPoint().  ^SQLite will invoke
6709251883Speter** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
6710251883Speter** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
6711251883Speter** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
6712251883Speter**
6713251883Speter** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
6714251883Speter** on the list of automatic extensions is a harmless no-op. ^No entry point
6715251883Speter** will be called more than once for each database connection that is opened.
6716251883Speter**
6717269851Speter** See also: [sqlite3_reset_auto_extension()]
6718269851Speter** and [sqlite3_cancel_auto_extension()]
6719251883Speter*/
6720322444SpeterSQLITE_API int sqlite3_auto_extension(void(*xEntryPoint)(void));
6721251883Speter
6722251883Speter/*
6723269851Speter** CAPI3REF: Cancel Automatic Extension Loading
6724269851Speter**
6725269851Speter** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
6726269851Speter** initialization routine X that was registered using a prior call to
6727269851Speter** [sqlite3_auto_extension(X)].  ^The [sqlite3_cancel_auto_extension(X)]
6728366076Scy** routine returns 1 if initialization routine X was successfully
6729269851Speter** unregistered and it returns 0 if X was not on the list of initialization
6730269851Speter** routines.
6731269851Speter*/
6732322444SpeterSQLITE_API int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
6733269851Speter
6734269851Speter/*
6735251883Speter** CAPI3REF: Reset Automatic Extension Loading
6736251883Speter**
6737251883Speter** ^This interface disables all automatic extensions previously
6738251883Speter** registered using [sqlite3_auto_extension()].
6739251883Speter*/
6740322444SpeterSQLITE_API void sqlite3_reset_auto_extension(void);
6741251883Speter
6742251883Speter/*
6743251883Speter** The interface to the virtual-table mechanism is currently considered
6744251883Speter** to be experimental.  The interface might change in incompatible ways.
6745251883Speter** If this is a problem for you, do not use the interface at this time.
6746251883Speter**
6747251883Speter** When the virtual-table mechanism stabilizes, we will declare the
6748251883Speter** interface fixed, support it indefinitely, and remove this comment.
6749251883Speter*/
6750251883Speter
6751251883Speter/*
6752251883Speter** Structures used by the virtual table interface
6753251883Speter*/
6754251883Spetertypedef struct sqlite3_vtab sqlite3_vtab;
6755251883Spetertypedef struct sqlite3_index_info sqlite3_index_info;
6756251883Spetertypedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
6757251883Spetertypedef struct sqlite3_module sqlite3_module;
6758251883Speter
6759251883Speter/*
6760251883Speter** CAPI3REF: Virtual Table Object
6761251883Speter** KEYWORDS: sqlite3_module {virtual table module}
6762251883Speter**
6763366076Scy** This structure, sometimes called a "virtual table module",
6764366076Scy** defines the implementation of a [virtual table].
6765251883Speter** This structure consists mostly of methods for the module.
6766251883Speter**
6767251883Speter** ^A virtual table module is created by filling in a persistent
6768251883Speter** instance of this structure and passing a pointer to that instance
6769251883Speter** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
6770251883Speter** ^The registration remains valid until it is replaced by a different
6771251883Speter** module or until the [database connection] closes.  The content
6772251883Speter** of this structure must not change while it is registered with
6773251883Speter** any database connection.
6774251883Speter*/
6775251883Speterstruct sqlite3_module {
6776251883Speter  int iVersion;
6777251883Speter  int (*xCreate)(sqlite3*, void *pAux,
6778251883Speter               int argc, const char *const*argv,
6779251883Speter               sqlite3_vtab **ppVTab, char**);
6780251883Speter  int (*xConnect)(sqlite3*, void *pAux,
6781251883Speter               int argc, const char *const*argv,
6782251883Speter               sqlite3_vtab **ppVTab, char**);
6783251883Speter  int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
6784251883Speter  int (*xDisconnect)(sqlite3_vtab *pVTab);
6785251883Speter  int (*xDestroy)(sqlite3_vtab *pVTab);
6786251883Speter  int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
6787251883Speter  int (*xClose)(sqlite3_vtab_cursor*);
6788251883Speter  int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
6789251883Speter                int argc, sqlite3_value **argv);
6790251883Speter  int (*xNext)(sqlite3_vtab_cursor*);
6791251883Speter  int (*xEof)(sqlite3_vtab_cursor*);
6792251883Speter  int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
6793251883Speter  int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
6794251883Speter  int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
6795251883Speter  int (*xBegin)(sqlite3_vtab *pVTab);
6796251883Speter  int (*xSync)(sqlite3_vtab *pVTab);
6797251883Speter  int (*xCommit)(sqlite3_vtab *pVTab);
6798251883Speter  int (*xRollback)(sqlite3_vtab *pVTab);
6799251883Speter  int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
6800251883Speter                       void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
6801251883Speter                       void **ppArg);
6802251883Speter  int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
6803366076Scy  /* The methods above are in version 1 of the sqlite_module object. Those
6804251883Speter  ** below are for version 2 and greater. */
6805251883Speter  int (*xSavepoint)(sqlite3_vtab *pVTab, int);
6806251883Speter  int (*xRelease)(sqlite3_vtab *pVTab, int);
6807251883Speter  int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
6808342292Scy  /* The methods above are in versions 1 and 2 of the sqlite_module object.
6809342292Scy  ** Those below are for version 3 and greater. */
6810342292Scy  int (*xShadowName)(const char*);
6811251883Speter};
6812251883Speter
6813251883Speter/*
6814251883Speter** CAPI3REF: Virtual Table Indexing Information
6815251883Speter** KEYWORDS: sqlite3_index_info
6816251883Speter**
6817251883Speter** The sqlite3_index_info structure and its substructures is used as part
6818251883Speter** of the [virtual table] interface to
6819251883Speter** pass information into and receive the reply from the [xBestIndex]
6820251883Speter** method of a [virtual table module].  The fields under **Inputs** are the
6821251883Speter** inputs to xBestIndex and are read-only.  xBestIndex inserts its
6822251883Speter** results into the **Outputs** fields.
6823251883Speter**
6824251883Speter** ^(The aConstraint[] array records WHERE clause constraints of the form:
6825251883Speter**
6826251883Speter** <blockquote>column OP expr</blockquote>
6827251883Speter**
6828251883Speter** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
6829251883Speter** stored in aConstraint[].op using one of the
6830251883Speter** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
6831251883Speter** ^(The index of the column is stored in
6832251883Speter** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
6833251883Speter** expr on the right-hand side can be evaluated (and thus the constraint
6834251883Speter** is usable) and false if it cannot.)^
6835251883Speter**
6836251883Speter** ^The optimizer automatically inverts terms of the form "expr OP column"
6837251883Speter** and makes other simplifications to the WHERE clause in an attempt to
6838251883Speter** get as many WHERE clause terms into the form shown above as possible.
6839251883Speter** ^The aConstraint[] array only reports WHERE clause terms that are
6840251883Speter** relevant to the particular virtual table being queried.
6841251883Speter**
6842251883Speter** ^Information about the ORDER BY clause is stored in aOrderBy[].
6843251883Speter** ^Each term of aOrderBy records a column of the ORDER BY clause.
6844251883Speter**
6845298161Sbapt** The colUsed field indicates which columns of the virtual table may be
6846298161Sbapt** required by the current scan. Virtual table columns are numbered from
6847298161Sbapt** zero in the order in which they appear within the CREATE TABLE statement
6848298161Sbapt** passed to sqlite3_declare_vtab(). For the first 63 columns (columns 0-62),
6849298161Sbapt** the corresponding bit is set within the colUsed mask if the column may be
6850298161Sbapt** required by SQLite. If the table has at least 64 columns and any column
6851298161Sbapt** to the right of the first 63 is required, then bit 63 of colUsed is also
6852298161Sbapt** set. In other words, column iCol may be required if the expression
6853366076Scy** (colUsed & ((sqlite3_uint64)1 << (iCol>=63 ? 63 : iCol))) evaluates to
6854298161Sbapt** non-zero.
6855298161Sbapt**
6856251883Speter** The [xBestIndex] method must fill aConstraintUsage[] with information
6857251883Speter** about what parameters to pass to xFilter.  ^If argvIndex>0 then
6858251883Speter** the right-hand side of the corresponding aConstraint[] is evaluated
6859251883Speter** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
6860251883Speter** is true, then the constraint is assumed to be fully handled by the
6861361456Scy** virtual table and might not be checked again by the byte code.)^ ^(The
6862361456Scy** aConstraintUsage[].omit flag is an optimization hint. When the omit flag
6863361456Scy** is left in its default setting of false, the constraint will always be
6864361456Scy** checked separately in byte code.  If the omit flag is change to true, then
6865361456Scy** the constraint may or may not be checked in byte code.  In other words,
6866361456Scy** when the omit flag is true there is no guarantee that the constraint will
6867361456Scy** not be checked again using byte code.)^
6868251883Speter**
6869251883Speter** ^The idxNum and idxPtr values are recorded and passed into the
6870251883Speter** [xFilter] method.
6871251883Speter** ^[sqlite3_free()] is used to free idxPtr if and only if
6872251883Speter** needToFreeIdxPtr is true.
6873251883Speter**
6874251883Speter** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
6875251883Speter** the correct order to satisfy the ORDER BY clause so that no separate
6876251883Speter** sorting step is required.
6877251883Speter**
6878269851Speter** ^The estimatedCost value is an estimate of the cost of a particular
6879269851Speter** strategy. A cost of N indicates that the cost of the strategy is similar
6880366076Scy** to a linear scan of an SQLite table with N rows. A cost of log(N)
6881269851Speter** indicates that the expense of the operation is similar to that of a
6882269851Speter** binary search on a unique indexed field of an SQLite table with N rows.
6883269851Speter**
6884269851Speter** ^The estimatedRows value is an estimate of the number of rows that
6885269851Speter** will be returned by the strategy.
6886269851Speter**
6887366076Scy** The xBestIndex method may optionally populate the idxFlags field with a
6888298161Sbapt** mask of SQLITE_INDEX_SCAN_* flags. Currently there is only one such flag -
6889298161Sbapt** SQLITE_INDEX_SCAN_UNIQUE. If the xBestIndex method sets this flag, SQLite
6890366076Scy** assumes that the strategy may visit at most one row.
6891298161Sbapt**
6892298161Sbapt** Additionally, if xBestIndex sets the SQLITE_INDEX_SCAN_UNIQUE flag, then
6893298161Sbapt** SQLite also assumes that if a call to the xUpdate() method is made as
6894298161Sbapt** part of the same statement to delete or update a virtual table row and the
6895298161Sbapt** implementation returns SQLITE_CONSTRAINT, then there is no need to rollback
6896298161Sbapt** any database changes. In other words, if the xUpdate() returns
6897298161Sbapt** SQLITE_CONSTRAINT, the database contents must be exactly as they were
6898298161Sbapt** before xUpdate was called. By contrast, if SQLITE_INDEX_SCAN_UNIQUE is not
6899298161Sbapt** set and xUpdate returns SQLITE_CONSTRAINT, any database changes made by
6900298161Sbapt** the xUpdate method are automatically rolled back by SQLite.
6901298161Sbapt**
6902269851Speter** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
6903366076Scy** structure for SQLite [version 3.8.2] ([dateof:3.8.2]).
6904322444Speter** If a virtual table extension is
6905366076Scy** used with an SQLite version earlier than 3.8.2, the results of attempting
6906366076Scy** to read or write the estimatedRows field are undefined (but are likely
6907361456Scy** to include crashing the application). The estimatedRows field should
6908269851Speter** therefore only be used if [sqlite3_libversion_number()] returns a
6909298161Sbapt** value greater than or equal to 3008002. Similarly, the idxFlags field
6910366076Scy** was added for [version 3.9.0] ([dateof:3.9.0]).
6911322444Speter** It may therefore only be used if
6912298161Sbapt** sqlite3_libversion_number() returns a value greater than or equal to
6913298161Sbapt** 3009000.
6914251883Speter*/
6915251883Speterstruct sqlite3_index_info {
6916251883Speter  /* Inputs */
6917251883Speter  int nConstraint;           /* Number of entries in aConstraint */
6918251883Speter  struct sqlite3_index_constraint {
6919298161Sbapt     int iColumn;              /* Column constrained.  -1 for ROWID */
6920251883Speter     unsigned char op;         /* Constraint operator */
6921251883Speter     unsigned char usable;     /* True if this constraint is usable */
6922251883Speter     int iTermOffset;          /* Used internally - xBestIndex should ignore */
6923251883Speter  } *aConstraint;            /* Table of WHERE clause constraints */
6924251883Speter  int nOrderBy;              /* Number of terms in the ORDER BY clause */
6925251883Speter  struct sqlite3_index_orderby {
6926251883Speter     int iColumn;              /* Column number */
6927251883Speter     unsigned char desc;       /* True for DESC.  False for ASC. */
6928251883Speter  } *aOrderBy;               /* The ORDER BY clause */
6929251883Speter  /* Outputs */
6930251883Speter  struct sqlite3_index_constraint_usage {
6931251883Speter    int argvIndex;           /* if >0, constraint is part of argv to xFilter */
6932251883Speter    unsigned char omit;      /* Do not code a test for this constraint */
6933251883Speter  } *aConstraintUsage;
6934251883Speter  int idxNum;                /* Number used to identify the index */
6935251883Speter  char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
6936251883Speter  int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
6937251883Speter  int orderByConsumed;       /* True if output is already ordered */
6938269851Speter  double estimatedCost;           /* Estimated cost of using this index */
6939269851Speter  /* Fields below are only available in SQLite 3.8.2 and later */
6940269851Speter  sqlite3_int64 estimatedRows;    /* Estimated number of rows returned */
6941298161Sbapt  /* Fields below are only available in SQLite 3.9.0 and later */
6942298161Sbapt  int idxFlags;              /* Mask of SQLITE_INDEX_SCAN_* flags */
6943298161Sbapt  /* Fields below are only available in SQLite 3.10.0 and later */
6944298161Sbapt  sqlite3_uint64 colUsed;    /* Input: Mask of columns used by statement */
6945251883Speter};
6946251883Speter
6947251883Speter/*
6948298161Sbapt** CAPI3REF: Virtual Table Scan Flags
6949342292Scy**
6950366076Scy** Virtual table implementations are allowed to set the
6951342292Scy** [sqlite3_index_info].idxFlags field to some combination of
6952342292Scy** these bits.
6953298161Sbapt*/
6954298161Sbapt#define SQLITE_INDEX_SCAN_UNIQUE      1     /* Scan visits at most 1 row */
6955298161Sbapt
6956298161Sbapt/*
6957251883Speter** CAPI3REF: Virtual Table Constraint Operator Codes
6958251883Speter**
6959361456Scy** These macros define the allowed values for the
6960251883Speter** [sqlite3_index_info].aConstraint[].op field.  Each value represents
6961251883Speter** an operator that is part of a constraint term in the wHERE clause of
6962251883Speter** a query that uses a [virtual table].
6963251883Speter*/
6964342292Scy#define SQLITE_INDEX_CONSTRAINT_EQ         2
6965342292Scy#define SQLITE_INDEX_CONSTRAINT_GT         4
6966342292Scy#define SQLITE_INDEX_CONSTRAINT_LE         8
6967342292Scy#define SQLITE_INDEX_CONSTRAINT_LT        16
6968342292Scy#define SQLITE_INDEX_CONSTRAINT_GE        32
6969342292Scy#define SQLITE_INDEX_CONSTRAINT_MATCH     64
6970342292Scy#define SQLITE_INDEX_CONSTRAINT_LIKE      65
6971342292Scy#define SQLITE_INDEX_CONSTRAINT_GLOB      66
6972342292Scy#define SQLITE_INDEX_CONSTRAINT_REGEXP    67
6973342292Scy#define SQLITE_INDEX_CONSTRAINT_NE        68
6974342292Scy#define SQLITE_INDEX_CONSTRAINT_ISNOT     69
6975342292Scy#define SQLITE_INDEX_CONSTRAINT_ISNOTNULL 70
6976342292Scy#define SQLITE_INDEX_CONSTRAINT_ISNULL    71
6977342292Scy#define SQLITE_INDEX_CONSTRAINT_IS        72
6978342292Scy#define SQLITE_INDEX_CONSTRAINT_FUNCTION 150
6979251883Speter
6980251883Speter/*
6981251883Speter** CAPI3REF: Register A Virtual Table Implementation
6982286510Speter** METHOD: sqlite3
6983251883Speter**
6984251883Speter** ^These routines are used to register a new [virtual table module] name.
6985251883Speter** ^Module names must be registered before
6986251883Speter** creating a new [virtual table] using the module and before using a
6987251883Speter** preexisting [virtual table] for the module.
6988251883Speter**
6989251883Speter** ^The module name is registered on the [database connection] specified
6990366076Scy** by the first parameter.  ^The name of the module is given by the
6991251883Speter** second parameter.  ^The third parameter is a pointer to
6992251883Speter** the implementation of the [virtual table module].   ^The fourth
6993251883Speter** parameter is an arbitrary client data pointer that is passed through
6994251883Speter** into the [xCreate] and [xConnect] methods of the virtual table module
6995251883Speter** when a new virtual table is be being created or reinitialized.
6996251883Speter**
6997251883Speter** ^The sqlite3_create_module_v2() interface has a fifth parameter which
6998251883Speter** is a pointer to a destructor for the pClientData.  ^SQLite will
6999251883Speter** invoke the destructor function (if it is not NULL) when SQLite
7000251883Speter** no longer needs the pClientData pointer.  ^The destructor will also
7001251883Speter** be invoked if the call to sqlite3_create_module_v2() fails.
7002251883Speter** ^The sqlite3_create_module()
7003251883Speter** interface is equivalent to sqlite3_create_module_v2() with a NULL
7004251883Speter** destructor.
7005355326Scy**
7006355326Scy** ^If the third parameter (the pointer to the sqlite3_module object) is
7007355326Scy** NULL then no new module is create and any existing modules with the
7008355326Scy** same name are dropped.
7009355326Scy**
7010355326Scy** See also: [sqlite3_drop_modules()]
7011251883Speter*/
7012322444SpeterSQLITE_API int sqlite3_create_module(
7013251883Speter  sqlite3 *db,               /* SQLite connection to register module with */
7014251883Speter  const char *zName,         /* Name of the module */
7015251883Speter  const sqlite3_module *p,   /* Methods for the module */
7016251883Speter  void *pClientData          /* Client data for xCreate/xConnect */
7017251883Speter);
7018322444SpeterSQLITE_API int sqlite3_create_module_v2(
7019251883Speter  sqlite3 *db,               /* SQLite connection to register module with */
7020251883Speter  const char *zName,         /* Name of the module */
7021251883Speter  const sqlite3_module *p,   /* Methods for the module */
7022251883Speter  void *pClientData,         /* Client data for xCreate/xConnect */
7023251883Speter  void(*xDestroy)(void*)     /* Module destructor function */
7024251883Speter);
7025251883Speter
7026251883Speter/*
7027355326Scy** CAPI3REF: Remove Unnecessary Virtual Table Implementations
7028355326Scy** METHOD: sqlite3
7029355326Scy**
7030355326Scy** ^The sqlite3_drop_modules(D,L) interface removes all virtual
7031355326Scy** table modules from database connection D except those named on list L.
7032355326Scy** The L parameter must be either NULL or a pointer to an array of pointers
7033355326Scy** to strings where the array is terminated by a single NULL pointer.
7034355326Scy** ^If the L parameter is NULL, then all virtual table modules are removed.
7035355326Scy**
7036355326Scy** See also: [sqlite3_create_module()]
7037355326Scy*/
7038355326ScySQLITE_API int sqlite3_drop_modules(
7039355326Scy  sqlite3 *db,                /* Remove modules from this connection */
7040355326Scy  const char **azKeep         /* Except, do not remove the ones named here */
7041355326Scy);
7042355326Scy
7043355326Scy/*
7044251883Speter** CAPI3REF: Virtual Table Instance Object
7045251883Speter** KEYWORDS: sqlite3_vtab
7046251883Speter**
7047251883Speter** Every [virtual table module] implementation uses a subclass
7048251883Speter** of this object to describe a particular instance
7049251883Speter** of the [virtual table].  Each subclass will
7050251883Speter** be tailored to the specific needs of the module implementation.
7051251883Speter** The purpose of this superclass is to define certain fields that are
7052251883Speter** common to all module implementations.
7053251883Speter**
7054251883Speter** ^Virtual tables methods can set an error message by assigning a
7055251883Speter** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
7056251883Speter** take care that any prior string is freed by a call to [sqlite3_free()]
7057251883Speter** prior to assigning a new string to zErrMsg.  ^After the error message
7058251883Speter** is delivered up to the client application, the string will be automatically
7059251883Speter** freed by sqlite3_free() and the zErrMsg field will be zeroed.
7060251883Speter*/
7061251883Speterstruct sqlite3_vtab {
7062251883Speter  const sqlite3_module *pModule;  /* The module for this virtual table */
7063282328Sbapt  int nRef;                       /* Number of open cursors */
7064251883Speter  char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
7065251883Speter  /* Virtual table implementations will typically add additional fields */
7066251883Speter};
7067251883Speter
7068251883Speter/*
7069251883Speter** CAPI3REF: Virtual Table Cursor Object
7070251883Speter** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
7071251883Speter**
7072251883Speter** Every [virtual table module] implementation uses a subclass of the
7073251883Speter** following structure to describe cursors that point into the
7074251883Speter** [virtual table] and are used
7075251883Speter** to loop through the virtual table.  Cursors are created using the
7076251883Speter** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
7077251883Speter** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
7078251883Speter** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
7079251883Speter** of the module.  Each module implementation will define
7080251883Speter** the content of a cursor structure to suit its own needs.
7081251883Speter**
7082251883Speter** This superclass exists in order to define fields of the cursor that
7083251883Speter** are common to all implementations.
7084251883Speter*/
7085251883Speterstruct sqlite3_vtab_cursor {
7086251883Speter  sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
7087251883Speter  /* Virtual table implementations will typically add additional fields */
7088251883Speter};
7089251883Speter
7090251883Speter/*
7091251883Speter** CAPI3REF: Declare The Schema Of A Virtual Table
7092251883Speter**
7093251883Speter** ^The [xCreate] and [xConnect] methods of a
7094251883Speter** [virtual table module] call this interface
7095251883Speter** to declare the format (the names and datatypes of the columns) of
7096251883Speter** the virtual tables they implement.
7097251883Speter*/
7098322444SpeterSQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
7099251883Speter
7100251883Speter/*
7101251883Speter** CAPI3REF: Overload A Function For A Virtual Table
7102286510Speter** METHOD: sqlite3
7103251883Speter**
7104251883Speter** ^(Virtual tables can provide alternative implementations of functions
7105366076Scy** using the [xFindFunction] method of the [virtual table module].
7106251883Speter** But global versions of those functions
7107251883Speter** must exist in order to be overloaded.)^
7108251883Speter**
7109251883Speter** ^(This API makes sure a global version of a function with a particular
7110251883Speter** name and number of parameters exists.  If no such function exists
7111251883Speter** before this API is called, a new function is created.)^  ^The implementation
7112251883Speter** of the new function always causes an exception to be thrown.  So
7113251883Speter** the new function is not good for anything by itself.  Its only
7114251883Speter** purpose is to be a placeholder function that can be overloaded
7115251883Speter** by a [virtual table].
7116251883Speter*/
7117322444SpeterSQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
7118251883Speter
7119251883Speter/*
7120251883Speter** The interface to the virtual-table mechanism defined above (back up
7121251883Speter** to a comment remarkably similar to this one) is currently considered
7122251883Speter** to be experimental.  The interface might change in incompatible ways.
7123251883Speter** If this is a problem for you, do not use the interface at this time.
7124251883Speter**
7125251883Speter** When the virtual-table mechanism stabilizes, we will declare the
7126251883Speter** interface fixed, support it indefinitely, and remove this comment.
7127251883Speter*/
7128251883Speter
7129251883Speter/*
7130251883Speter** CAPI3REF: A Handle To An Open BLOB
7131251883Speter** KEYWORDS: {BLOB handle} {BLOB handles}
7132251883Speter**
7133251883Speter** An instance of this object represents an open BLOB on which
7134251883Speter** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
7135251883Speter** ^Objects of this type are created by [sqlite3_blob_open()]
7136251883Speter** and destroyed by [sqlite3_blob_close()].
7137251883Speter** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
7138251883Speter** can be used to read or write small subsections of the BLOB.
7139251883Speter** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
7140251883Speter*/
7141251883Spetertypedef struct sqlite3_blob sqlite3_blob;
7142251883Speter
7143251883Speter/*
7144251883Speter** CAPI3REF: Open A BLOB For Incremental I/O
7145286510Speter** METHOD: sqlite3
7146286510Speter** CONSTRUCTOR: sqlite3_blob
7147251883Speter**
7148251883Speter** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
7149251883Speter** in row iRow, column zColumn, table zTable in database zDb;
7150251883Speter** in other words, the same BLOB that would be selected by:
7151251883Speter**
7152251883Speter** <pre>
7153251883Speter**     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
7154251883Speter** </pre>)^
7155251883Speter**
7156366076Scy** ^(Parameter zDb is not the filename that contains the database, but
7157282328Sbapt** rather the symbolic name of the database. For attached databases, this is
7158282328Sbapt** the name that appears after the AS keyword in the [ATTACH] statement.
7159282328Sbapt** For the main database file, the database name is "main". For TEMP
7160282328Sbapt** tables, the database name is "temp".)^
7161282328Sbapt**
7162251883Speter** ^If the flags parameter is non-zero, then the BLOB is opened for read
7163282328Sbapt** and write access. ^If the flags parameter is zero, the BLOB is opened for
7164282328Sbapt** read-only access.
7165251883Speter**
7166282328Sbapt** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is stored
7167282328Sbapt** in *ppBlob. Otherwise an [error code] is returned and, unless the error
7168282328Sbapt** code is SQLITE_MISUSE, *ppBlob is set to NULL.)^ ^This means that, provided
7169366076Scy** the API is not misused, it is always safe to call [sqlite3_blob_close()]
7170282328Sbapt** on *ppBlob after this function it returns.
7171251883Speter**
7172282328Sbapt** This function fails with SQLITE_ERROR if any of the following are true:
7173282328Sbapt** <ul>
7174366076Scy**   <li> ^(Database zDb does not exist)^,
7175366076Scy**   <li> ^(Table zTable does not exist within database zDb)^,
7176366076Scy**   <li> ^(Table zTable is a WITHOUT ROWID table)^,
7177282328Sbapt**   <li> ^(Column zColumn does not exist)^,
7178282328Sbapt**   <li> ^(Row iRow is not present in the table)^,
7179282328Sbapt**   <li> ^(The specified column of row iRow contains a value that is not
7180282328Sbapt**         a TEXT or BLOB value)^,
7181366076Scy**   <li> ^(Column zColumn is part of an index, PRIMARY KEY or UNIQUE
7182282328Sbapt**         constraint and the blob is being opened for read/write access)^,
7183366076Scy**   <li> ^([foreign key constraints | Foreign key constraints] are enabled,
7184282328Sbapt**         column zColumn is part of a [child key] definition and the blob is
7185282328Sbapt**         being opened for read/write access)^.
7186282328Sbapt** </ul>
7187251883Speter**
7188366076Scy** ^Unless it returns SQLITE_MISUSE, this function sets the
7189366076Scy** [database connection] error code and message accessible via
7190366076Scy** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7191282328Sbapt**
7192322444Speter** A BLOB referenced by sqlite3_blob_open() may be read using the
7193322444Speter** [sqlite3_blob_read()] interface and modified by using
7194322444Speter** [sqlite3_blob_write()].  The [BLOB handle] can be moved to a
7195322444Speter** different row of the same table using the [sqlite3_blob_reopen()]
7196322444Speter** interface.  However, the column, table, or database of a [BLOB handle]
7197322444Speter** cannot be changed after the [BLOB handle] is opened.
7198282328Sbapt**
7199251883Speter** ^(If the row that a BLOB handle points to is modified by an
7200251883Speter** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
7201251883Speter** then the BLOB handle is marked as "expired".
7202251883Speter** This is true if any column of the row is changed, even a column
7203251883Speter** other than the one the BLOB handle is open on.)^
7204251883Speter** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
7205251883Speter** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
7206251883Speter** ^(Changes written into a BLOB prior to the BLOB expiring are not
7207251883Speter** rolled back by the expiration of the BLOB.  Such changes will eventually
7208251883Speter** commit if the transaction continues to completion.)^
7209251883Speter**
7210251883Speter** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
7211251883Speter** the opened blob.  ^The size of a blob may not be changed by this
7212251883Speter** interface.  Use the [UPDATE] SQL command to change the size of a
7213251883Speter** blob.
7214251883Speter**
7215251883Speter** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
7216366076Scy** and the built-in [zeroblob] SQL function may be used to create a
7217282328Sbapt** zero-filled blob to read or write using the incremental-blob interface.
7218251883Speter**
7219251883Speter** To avoid a resource leak, every open [BLOB handle] should eventually
7220251883Speter** be released by a call to [sqlite3_blob_close()].
7221322444Speter**
7222322444Speter** See also: [sqlite3_blob_close()],
7223322444Speter** [sqlite3_blob_reopen()], [sqlite3_blob_read()],
7224322444Speter** [sqlite3_blob_bytes()], [sqlite3_blob_write()].
7225251883Speter*/
7226322444SpeterSQLITE_API int sqlite3_blob_open(
7227251883Speter  sqlite3*,
7228251883Speter  const char *zDb,
7229251883Speter  const char *zTable,
7230251883Speter  const char *zColumn,
7231251883Speter  sqlite3_int64 iRow,
7232251883Speter  int flags,
7233251883Speter  sqlite3_blob **ppBlob
7234251883Speter);
7235251883Speter
7236251883Speter/*
7237251883Speter** CAPI3REF: Move a BLOB Handle to a New Row
7238286510Speter** METHOD: sqlite3_blob
7239251883Speter**
7240322444Speter** ^This function is used to move an existing [BLOB handle] so that it points
7241251883Speter** to a different row of the same database table. ^The new row is identified
7242251883Speter** by the rowid value passed as the second argument. Only the row can be
7243251883Speter** changed. ^The database, table and column on which the blob handle is open
7244322444Speter** remain the same. Moving an existing [BLOB handle] to a new row is
7245251883Speter** faster than closing the existing handle and opening a new one.
7246251883Speter**
7247251883Speter** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
7248251883Speter** it must exist and there must be either a blob or text value stored in
7249251883Speter** the nominated column.)^ ^If the new row is not present in the table, or if
7250251883Speter** it does not contain a blob or text value, or if another error occurs, an
7251251883Speter** SQLite error code is returned and the blob handle is considered aborted.
7252251883Speter** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
7253251883Speter** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
7254251883Speter** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
7255251883Speter** always returns zero.
7256251883Speter**
7257251883Speter** ^This function sets the database handle error code and message.
7258251883Speter*/
7259322444SpeterSQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
7260251883Speter
7261251883Speter/*
7262251883Speter** CAPI3REF: Close A BLOB Handle
7263286510Speter** DESTRUCTOR: sqlite3_blob
7264251883Speter**
7265282328Sbapt** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
7266366076Scy** unconditionally.  Even if this routine returns an error code, the
7267282328Sbapt** handle is still closed.)^
7268251883Speter**
7269282328Sbapt** ^If the blob handle being closed was opened for read-write access, and if
7270282328Sbapt** the database is in auto-commit mode and there are no other open read-write
7271282328Sbapt** blob handles or active write statements, the current transaction is
7272282328Sbapt** committed. ^If an error occurs while committing the transaction, an error
7273282328Sbapt** code is returned and the transaction rolled back.
7274251883Speter**
7275282328Sbapt** Calling this function with an argument that is not a NULL pointer or an
7276366076Scy** open blob handle results in undefined behaviour. ^Calling this routine
7277366076Scy** with a null pointer (such as would be returned by a failed call to
7278282328Sbapt** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7279366076Scy** is passed a valid open blob handle, the values returned by the
7280282328Sbapt** sqlite3_errcode() and sqlite3_errmsg() functions are set before returning.
7281251883Speter*/
7282322444SpeterSQLITE_API int sqlite3_blob_close(sqlite3_blob *);
7283251883Speter
7284251883Speter/*
7285251883Speter** CAPI3REF: Return The Size Of An Open BLOB
7286286510Speter** METHOD: sqlite3_blob
7287251883Speter**
7288366076Scy** ^Returns the size in bytes of the BLOB accessible via the
7289251883Speter** successfully opened [BLOB handle] in its only argument.  ^The
7290251883Speter** incremental blob I/O routines can only read or overwriting existing
7291251883Speter** blob content; they cannot change the size of a blob.
7292251883Speter**
7293251883Speter** This routine only works on a [BLOB handle] which has been created
7294251883Speter** by a prior successful call to [sqlite3_blob_open()] and which has not
7295251883Speter** been closed by [sqlite3_blob_close()].  Passing any other pointer in
7296251883Speter** to this routine results in undefined and probably undesirable behavior.
7297251883Speter*/
7298322444SpeterSQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
7299251883Speter
7300251883Speter/*
7301251883Speter** CAPI3REF: Read Data From A BLOB Incrementally
7302286510Speter** METHOD: sqlite3_blob
7303251883Speter**
7304251883Speter** ^(This function is used to read data from an open [BLOB handle] into a
7305251883Speter** caller-supplied buffer. N bytes of data are copied into buffer Z
7306251883Speter** from the open BLOB, starting at offset iOffset.)^
7307251883Speter**
7308251883Speter** ^If offset iOffset is less than N bytes from the end of the BLOB,
7309251883Speter** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
7310251883Speter** less than zero, [SQLITE_ERROR] is returned and no data is read.
7311251883Speter** ^The size of the blob (and hence the maximum value of N+iOffset)
7312251883Speter** can be determined using the [sqlite3_blob_bytes()] interface.
7313251883Speter**
7314251883Speter** ^An attempt to read from an expired [BLOB handle] fails with an
7315251883Speter** error code of [SQLITE_ABORT].
7316251883Speter**
7317251883Speter** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
7318251883Speter** Otherwise, an [error code] or an [extended error code] is returned.)^
7319251883Speter**
7320251883Speter** This routine only works on a [BLOB handle] which has been created
7321251883Speter** by a prior successful call to [sqlite3_blob_open()] and which has not
7322251883Speter** been closed by [sqlite3_blob_close()].  Passing any other pointer in
7323251883Speter** to this routine results in undefined and probably undesirable behavior.
7324251883Speter**
7325251883Speter** See also: [sqlite3_blob_write()].
7326251883Speter*/
7327322444SpeterSQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
7328251883Speter
7329251883Speter/*
7330251883Speter** CAPI3REF: Write Data Into A BLOB Incrementally
7331286510Speter** METHOD: sqlite3_blob
7332251883Speter**
7333282328Sbapt** ^(This function is used to write data into an open [BLOB handle] from a
7334282328Sbapt** caller-supplied buffer. N bytes of data are copied from the buffer Z
7335282328Sbapt** into the open BLOB, starting at offset iOffset.)^
7336251883Speter**
7337282328Sbapt** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
7338282328Sbapt** Otherwise, an  [error code] or an [extended error code] is returned.)^
7339366076Scy** ^Unless SQLITE_MISUSE is returned, this function sets the
7340366076Scy** [database connection] error code and message accessible via
7341366076Scy** [sqlite3_errcode()] and [sqlite3_errmsg()] and related functions.
7342282328Sbapt**
7343251883Speter** ^If the [BLOB handle] passed as the first argument was not opened for
7344251883Speter** writing (the flags parameter to [sqlite3_blob_open()] was zero),
7345251883Speter** this function returns [SQLITE_READONLY].
7346251883Speter**
7347282328Sbapt** This function may only modify the contents of the BLOB; it is
7348251883Speter** not possible to increase the size of a BLOB using this API.
7349251883Speter** ^If offset iOffset is less than N bytes from the end of the BLOB,
7350366076Scy** [SQLITE_ERROR] is returned and no data is written. The size of the
7351366076Scy** BLOB (and hence the maximum value of N+iOffset) can be determined
7352366076Scy** using the [sqlite3_blob_bytes()] interface. ^If N or iOffset are less
7353282328Sbapt** than zero [SQLITE_ERROR] is returned and no data is written.
7354251883Speter**
7355251883Speter** ^An attempt to write to an expired [BLOB handle] fails with an
7356251883Speter** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
7357251883Speter** before the [BLOB handle] expired are not rolled back by the
7358251883Speter** expiration of the handle, though of course those changes might
7359251883Speter** have been overwritten by the statement that expired the BLOB handle
7360251883Speter** or by other independent statements.
7361251883Speter**
7362251883Speter** This routine only works on a [BLOB handle] which has been created
7363251883Speter** by a prior successful call to [sqlite3_blob_open()] and which has not
7364251883Speter** been closed by [sqlite3_blob_close()].  Passing any other pointer in
7365251883Speter** to this routine results in undefined and probably undesirable behavior.
7366251883Speter**
7367251883Speter** See also: [sqlite3_blob_read()].
7368251883Speter*/
7369322444SpeterSQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
7370251883Speter
7371251883Speter/*
7372251883Speter** CAPI3REF: Virtual File System Objects
7373251883Speter**
7374251883Speter** A virtual filesystem (VFS) is an [sqlite3_vfs] object
7375251883Speter** that SQLite uses to interact
7376251883Speter** with the underlying operating system.  Most SQLite builds come with a
7377251883Speter** single default VFS that is appropriate for the host computer.
7378251883Speter** New VFSes can be registered and existing VFSes can be unregistered.
7379251883Speter** The following interfaces are provided.
7380251883Speter**
7381251883Speter** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
7382251883Speter** ^Names are case sensitive.
7383251883Speter** ^Names are zero-terminated UTF-8 strings.
7384251883Speter** ^If there is no match, a NULL pointer is returned.
7385251883Speter** ^If zVfsName is NULL then the default VFS is returned.
7386251883Speter**
7387251883Speter** ^New VFSes are registered with sqlite3_vfs_register().
7388251883Speter** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
7389251883Speter** ^The same VFS can be registered multiple times without injury.
7390251883Speter** ^To make an existing VFS into the default VFS, register it again
7391251883Speter** with the makeDflt flag set.  If two different VFSes with the
7392251883Speter** same name are registered, the behavior is undefined.  If a
7393251883Speter** VFS is registered with a name that is NULL or an empty string,
7394251883Speter** then the behavior is undefined.
7395251883Speter**
7396251883Speter** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
7397251883Speter** ^(If the default VFS is unregistered, another VFS is chosen as
7398251883Speter** the default.  The choice for the new VFS is arbitrary.)^
7399251883Speter*/
7400322444SpeterSQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
7401322444SpeterSQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
7402322444SpeterSQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
7403251883Speter
7404251883Speter/*
7405251883Speter** CAPI3REF: Mutexes
7406251883Speter**
7407251883Speter** The SQLite core uses these routines for thread
7408251883Speter** synchronization. Though they are intended for internal
7409251883Speter** use by SQLite, code that links against SQLite is
7410251883Speter** permitted to use any of these routines.
7411251883Speter**
7412251883Speter** The SQLite source code contains multiple implementations
7413251883Speter** of these mutex routines.  An appropriate implementation
7414282328Sbapt** is selected automatically at compile-time.  The following
7415251883Speter** implementations are available in the SQLite core:
7416251883Speter**
7417251883Speter** <ul>
7418251883Speter** <li>   SQLITE_MUTEX_PTHREADS
7419251883Speter** <li>   SQLITE_MUTEX_W32
7420251883Speter** <li>   SQLITE_MUTEX_NOOP
7421282328Sbapt** </ul>
7422251883Speter**
7423282328Sbapt** The SQLITE_MUTEX_NOOP implementation is a set of routines
7424251883Speter** that does no real locking and is appropriate for use in
7425282328Sbapt** a single-threaded application.  The SQLITE_MUTEX_PTHREADS and
7426251883Speter** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
7427251883Speter** and Windows.
7428251883Speter**
7429282328Sbapt** If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
7430251883Speter** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
7431251883Speter** implementation is included with the library. In this case the
7432251883Speter** application must supply a custom mutex implementation using the
7433251883Speter** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
7434251883Speter** before calling sqlite3_initialize() or any other public sqlite3_
7435282328Sbapt** function that calls sqlite3_initialize().
7436251883Speter**
7437251883Speter** ^The sqlite3_mutex_alloc() routine allocates a new
7438282328Sbapt** mutex and returns a pointer to it. ^The sqlite3_mutex_alloc()
7439282328Sbapt** routine returns NULL if it is unable to allocate the requested
7440282328Sbapt** mutex.  The argument to sqlite3_mutex_alloc() must one of these
7441282328Sbapt** integer constants:
7442251883Speter**
7443251883Speter** <ul>
7444251883Speter** <li>  SQLITE_MUTEX_FAST
7445251883Speter** <li>  SQLITE_MUTEX_RECURSIVE
7446366076Scy** <li>  SQLITE_MUTEX_STATIC_MAIN
7447251883Speter** <li>  SQLITE_MUTEX_STATIC_MEM
7448274884Sbapt** <li>  SQLITE_MUTEX_STATIC_OPEN
7449251883Speter** <li>  SQLITE_MUTEX_STATIC_PRNG
7450251883Speter** <li>  SQLITE_MUTEX_STATIC_LRU
7451274884Sbapt** <li>  SQLITE_MUTEX_STATIC_PMEM
7452274884Sbapt** <li>  SQLITE_MUTEX_STATIC_APP1
7453274884Sbapt** <li>  SQLITE_MUTEX_STATIC_APP2
7454282328Sbapt** <li>  SQLITE_MUTEX_STATIC_APP3
7455298161Sbapt** <li>  SQLITE_MUTEX_STATIC_VFS1
7456298161Sbapt** <li>  SQLITE_MUTEX_STATIC_VFS2
7457298161Sbapt** <li>  SQLITE_MUTEX_STATIC_VFS3
7458282328Sbapt** </ul>
7459251883Speter**
7460251883Speter** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
7461251883Speter** cause sqlite3_mutex_alloc() to create
7462251883Speter** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
7463251883Speter** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
7464251883Speter** The mutex implementation does not need to make a distinction
7465251883Speter** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
7466282328Sbapt** not want to.  SQLite will only request a recursive mutex in
7467282328Sbapt** cases where it really needs one.  If a faster non-recursive mutex
7468251883Speter** implementation is available on the host platform, the mutex subsystem
7469251883Speter** might return such a mutex in response to SQLITE_MUTEX_FAST.
7470251883Speter**
7471251883Speter** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
7472251883Speter** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
7473282328Sbapt** a pointer to a static preexisting mutex.  ^Nine static mutexes are
7474251883Speter** used by the current version of SQLite.  Future versions of SQLite
7475251883Speter** may add additional static mutexes.  Static mutexes are for internal
7476251883Speter** use by SQLite only.  Applications that use SQLite mutexes should
7477251883Speter** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
7478251883Speter** SQLITE_MUTEX_RECURSIVE.
7479251883Speter**
7480251883Speter** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
7481251883Speter** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
7482282328Sbapt** returns a different mutex on every call.  ^For the static
7483251883Speter** mutex types, the same mutex is returned on every call that has
7484251883Speter** the same type number.
7485251883Speter**
7486251883Speter** ^The sqlite3_mutex_free() routine deallocates a previously
7487282328Sbapt** allocated dynamic mutex.  Attempting to deallocate a static
7488282328Sbapt** mutex results in undefined behavior.
7489251883Speter**
7490251883Speter** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
7491251883Speter** to enter a mutex.  ^If another thread is already within the mutex,
7492251883Speter** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
7493251883Speter** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
7494251883Speter** upon successful entry.  ^(Mutexes created using
7495251883Speter** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
7496282328Sbapt** In such cases, the
7497251883Speter** mutex must be exited an equal number of times before another thread
7498282328Sbapt** can enter.)^  If the same thread tries to enter any mutex other
7499282328Sbapt** than an SQLITE_MUTEX_RECURSIVE more than once, the behavior is undefined.
7500251883Speter**
7501251883Speter** ^(Some systems (for example, Windows 95) do not support the operation
7502251883Speter** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
7503282328Sbapt** will always return SQLITE_BUSY. The SQLite core only ever uses
7504366076Scy** sqlite3_mutex_try() as an optimization so this is acceptable
7505282328Sbapt** behavior.)^
7506251883Speter**
7507251883Speter** ^The sqlite3_mutex_leave() routine exits a mutex that was
7508282328Sbapt** previously entered by the same thread.   The behavior
7509251883Speter** is undefined if the mutex is not currently entered by the
7510282328Sbapt** calling thread or is not currently allocated.
7511251883Speter**
7512251883Speter** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
7513251883Speter** sqlite3_mutex_leave() is a NULL pointer, then all three routines
7514251883Speter** behave as no-ops.
7515251883Speter**
7516251883Speter** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
7517251883Speter*/
7518322444SpeterSQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
7519322444SpeterSQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
7520322444SpeterSQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
7521322444SpeterSQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
7522322444SpeterSQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
7523251883Speter
7524251883Speter/*
7525251883Speter** CAPI3REF: Mutex Methods Object
7526251883Speter**
7527251883Speter** An instance of this structure defines the low-level routines
7528251883Speter** used to allocate and use mutexes.
7529251883Speter**
7530251883Speter** Usually, the default mutex implementations provided by SQLite are
7531282328Sbapt** sufficient, however the application has the option of substituting a custom
7532251883Speter** implementation for specialized deployments or systems for which SQLite
7533282328Sbapt** does not provide a suitable implementation. In this case, the application
7534251883Speter** creates and populates an instance of this structure to pass
7535251883Speter** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
7536251883Speter** Additionally, an instance of this structure can be used as an
7537251883Speter** output variable when querying the system for the current mutex
7538251883Speter** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
7539251883Speter**
7540251883Speter** ^The xMutexInit method defined by this structure is invoked as
7541251883Speter** part of system initialization by the sqlite3_initialize() function.
7542251883Speter** ^The xMutexInit routine is called by SQLite exactly once for each
7543251883Speter** effective call to [sqlite3_initialize()].
7544251883Speter**
7545251883Speter** ^The xMutexEnd method defined by this structure is invoked as
7546251883Speter** part of system shutdown by the sqlite3_shutdown() function. The
7547251883Speter** implementation of this method is expected to release all outstanding
7548251883Speter** resources obtained by the mutex methods implementation, especially
7549251883Speter** those obtained by the xMutexInit method.  ^The xMutexEnd()
7550251883Speter** interface is invoked exactly once for each call to [sqlite3_shutdown()].
7551251883Speter**
7552251883Speter** ^(The remaining seven methods defined by this structure (xMutexAlloc,
7553251883Speter** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
7554251883Speter** xMutexNotheld) implement the following interfaces (respectively):
7555251883Speter**
7556251883Speter** <ul>
7557251883Speter**   <li>  [sqlite3_mutex_alloc()] </li>
7558251883Speter**   <li>  [sqlite3_mutex_free()] </li>
7559251883Speter**   <li>  [sqlite3_mutex_enter()] </li>
7560251883Speter**   <li>  [sqlite3_mutex_try()] </li>
7561251883Speter**   <li>  [sqlite3_mutex_leave()] </li>
7562251883Speter**   <li>  [sqlite3_mutex_held()] </li>
7563251883Speter**   <li>  [sqlite3_mutex_notheld()] </li>
7564251883Speter** </ul>)^
7565251883Speter**
7566251883Speter** The only difference is that the public sqlite3_XXX functions enumerated
7567251883Speter** above silently ignore any invocations that pass a NULL pointer instead
7568251883Speter** of a valid mutex handle. The implementations of the methods defined
7569361456Scy** by this structure are not required to handle this case. The results
7570251883Speter** of passing a NULL pointer instead of a valid mutex handle are undefined
7571251883Speter** (i.e. it is acceptable to provide an implementation that segfaults if
7572251883Speter** it is passed a NULL pointer).
7573251883Speter**
7574282328Sbapt** The xMutexInit() method must be threadsafe.  It must be harmless to
7575251883Speter** invoke xMutexInit() multiple times within the same process and without
7576251883Speter** intervening calls to xMutexEnd().  Second and subsequent calls to
7577251883Speter** xMutexInit() must be no-ops.
7578251883Speter**
7579282328Sbapt** xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
7580282328Sbapt** and its associates).  Similarly, xMutexAlloc() must not use SQLite memory
7581251883Speter** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
7582251883Speter** memory allocation for a fast or recursive mutex.
7583251883Speter**
7584251883Speter** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
7585251883Speter** called, but only if the prior call to xMutexInit returned SQLITE_OK.
7586251883Speter** If xMutexInit fails in any way, it is expected to clean up after itself
7587251883Speter** prior to returning.
7588251883Speter*/
7589251883Spetertypedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
7590251883Speterstruct sqlite3_mutex_methods {
7591251883Speter  int (*xMutexInit)(void);
7592251883Speter  int (*xMutexEnd)(void);
7593251883Speter  sqlite3_mutex *(*xMutexAlloc)(int);
7594251883Speter  void (*xMutexFree)(sqlite3_mutex *);
7595251883Speter  void (*xMutexEnter)(sqlite3_mutex *);
7596251883Speter  int (*xMutexTry)(sqlite3_mutex *);
7597251883Speter  void (*xMutexLeave)(sqlite3_mutex *);
7598251883Speter  int (*xMutexHeld)(sqlite3_mutex *);
7599251883Speter  int (*xMutexNotheld)(sqlite3_mutex *);
7600251883Speter};
7601251883Speter
7602251883Speter/*
7603251883Speter** CAPI3REF: Mutex Verification Routines
7604251883Speter**
7605251883Speter** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
7606282328Sbapt** are intended for use inside assert() statements.  The SQLite core
7607251883Speter** never uses these routines except inside an assert() and applications
7608282328Sbapt** are advised to follow the lead of the core.  The SQLite core only
7609251883Speter** provides implementations for these routines when it is compiled
7610282328Sbapt** with the SQLITE_DEBUG flag.  External mutex implementations
7611251883Speter** are only required to provide these routines if SQLITE_DEBUG is
7612251883Speter** defined and if NDEBUG is not defined.
7613251883Speter**
7614282328Sbapt** These routines should return true if the mutex in their argument
7615251883Speter** is held or not held, respectively, by the calling thread.
7616251883Speter**
7617282328Sbapt** The implementation is not required to provide versions of these
7618251883Speter** routines that actually work. If the implementation does not provide working
7619251883Speter** versions of these routines, it should at least provide stubs that always
7620251883Speter** return true so that one does not get spurious assertion failures.
7621251883Speter**
7622282328Sbapt** If the argument to sqlite3_mutex_held() is a NULL pointer then
7623251883Speter** the routine should return 1.   This seems counter-intuitive since
7624251883Speter** clearly the mutex cannot be held if it does not exist.  But
7625251883Speter** the reason the mutex does not exist is because the build is not
7626251883Speter** using mutexes.  And we do not want the assert() containing the
7627251883Speter** call to sqlite3_mutex_held() to fail, so a non-zero return is
7628282328Sbapt** the appropriate thing to do.  The sqlite3_mutex_notheld()
7629251883Speter** interface should also return 1 when given a NULL pointer.
7630251883Speter*/
7631251883Speter#ifndef NDEBUG
7632322444SpeterSQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
7633322444SpeterSQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
7634251883Speter#endif
7635251883Speter
7636251883Speter/*
7637251883Speter** CAPI3REF: Mutex Types
7638251883Speter**
7639251883Speter** The [sqlite3_mutex_alloc()] interface takes a single argument
7640251883Speter** which is one of these integer constants.
7641251883Speter**
7642251883Speter** The set of static mutexes may change from one SQLite release to the
7643251883Speter** next.  Applications that override the built-in mutex logic must be
7644251883Speter** prepared to accommodate additional static mutexes.
7645251883Speter*/
7646251883Speter#define SQLITE_MUTEX_FAST             0
7647251883Speter#define SQLITE_MUTEX_RECURSIVE        1
7648366076Scy#define SQLITE_MUTEX_STATIC_MAIN      2
7649251883Speter#define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
7650251883Speter#define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
7651251883Speter#define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
7652322444Speter#define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_randomness() */
7653251883Speter#define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
7654251883Speter#define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
7655251883Speter#define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
7656274884Sbapt#define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
7657274884Sbapt#define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
7658274884Sbapt#define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
7659286510Speter#define SQLITE_MUTEX_STATIC_VFS1     11  /* For use by built-in VFS */
7660286510Speter#define SQLITE_MUTEX_STATIC_VFS2     12  /* For use by extension VFS */
7661286510Speter#define SQLITE_MUTEX_STATIC_VFS3     13  /* For use by application VFS */
7662251883Speter
7663366076Scy/* Legacy compatibility: */
7664366076Scy#define SQLITE_MUTEX_STATIC_MASTER    2
7665366076Scy
7666366076Scy
7667251883Speter/*
7668251883Speter** CAPI3REF: Retrieve the mutex for a database connection
7669286510Speter** METHOD: sqlite3
7670251883Speter**
7671366076Scy** ^This interface returns a pointer the [sqlite3_mutex] object that
7672251883Speter** serializes access to the [database connection] given in the argument
7673251883Speter** when the [threading mode] is Serialized.
7674251883Speter** ^If the [threading mode] is Single-thread or Multi-thread then this
7675251883Speter** routine returns a NULL pointer.
7676251883Speter*/
7677322444SpeterSQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
7678251883Speter
7679251883Speter/*
7680251883Speter** CAPI3REF: Low-Level Control Of Database Files
7681286510Speter** METHOD: sqlite3
7682342292Scy** KEYWORDS: {file control}
7683251883Speter**
7684251883Speter** ^The [sqlite3_file_control()] interface makes a direct call to the
7685251883Speter** xFileControl method for the [sqlite3_io_methods] object associated
7686251883Speter** with a particular database identified by the second argument. ^The
7687251883Speter** name of the database is "main" for the main database or "temp" for the
7688251883Speter** TEMP database, or the name that appears after the AS keyword for
7689251883Speter** databases that are added using the [ATTACH] SQL command.
7690251883Speter** ^A NULL pointer can be used in place of "main" to refer to the
7691251883Speter** main database file.
7692251883Speter** ^The third and fourth parameters to this routine
7693251883Speter** are passed directly through to the second and third parameters of
7694251883Speter** the xFileControl method.  ^The return value of the xFileControl
7695251883Speter** method becomes the return value of this routine.
7696251883Speter**
7697342292Scy** A few opcodes for [sqlite3_file_control()] are handled directly
7698366076Scy** by the SQLite core and never invoke the
7699342292Scy** sqlite3_io_methods.xFileControl method.
7700342292Scy** ^The [SQLITE_FCNTL_FILE_POINTER] value for the op parameter causes
7701251883Speter** a pointer to the underlying [sqlite3_file] object to be written into
7702342292Scy** the space pointed to by the 4th parameter.  The
7703342292Scy** [SQLITE_FCNTL_JOURNAL_POINTER] works similarly except that it returns
7704342292Scy** the [sqlite3_file] object associated with the journal file instead of
7705342292Scy** the main database.  The [SQLITE_FCNTL_VFS_POINTER] opcode returns
7706342292Scy** a pointer to the underlying [sqlite3_vfs] object for the file.
7707342292Scy** The [SQLITE_FCNTL_DATA_VERSION] returns the data version counter
7708342292Scy** from the pager.
7709251883Speter**
7710251883Speter** ^If the second parameter (zDbName) does not match the name of any
7711251883Speter** open database file, then SQLITE_ERROR is returned.  ^This error
7712251883Speter** code is not remembered and will not be recalled by [sqlite3_errcode()]
7713251883Speter** or [sqlite3_errmsg()].  The underlying xFileControl method might
7714251883Speter** also return SQLITE_ERROR.  There is no way to distinguish between
7715251883Speter** an incorrect zDbName and an SQLITE_ERROR return from the underlying
7716251883Speter** xFileControl method.
7717251883Speter**
7718342292Scy** See also: [file control opcodes]
7719251883Speter*/
7720322444SpeterSQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
7721251883Speter
7722251883Speter/*
7723251883Speter** CAPI3REF: Testing Interface
7724251883Speter**
7725251883Speter** ^The sqlite3_test_control() interface is used to read out internal
7726251883Speter** state of SQLite and to inject faults into SQLite for testing
7727251883Speter** purposes.  ^The first parameter is an operation code that determines
7728251883Speter** the number, meaning, and operation of all subsequent parameters.
7729251883Speter**
7730251883Speter** This interface is not for use by applications.  It exists solely
7731251883Speter** for verifying the correct operation of the SQLite library.  Depending
7732251883Speter** on how the SQLite library is compiled, this interface might not exist.
7733251883Speter**
7734251883Speter** The details of the operation codes, their meanings, the parameters
7735251883Speter** they take, and what they do are all subject to change without notice.
7736251883Speter** Unlike most of the SQLite API, this function is not guaranteed to
7737251883Speter** operate consistently from one release to the next.
7738251883Speter*/
7739322444SpeterSQLITE_API int sqlite3_test_control(int op, ...);
7740251883Speter
7741251883Speter/*
7742251883Speter** CAPI3REF: Testing Interface Operation Codes
7743251883Speter**
7744251883Speter** These constants are the valid operation code parameters used
7745251883Speter** as the first argument to [sqlite3_test_control()].
7746251883Speter**
7747251883Speter** These parameters and their meanings are subject to change
7748251883Speter** without notice.  These values are for testing purposes only.
7749251883Speter** Applications should not use any of these parameters or the
7750251883Speter** [sqlite3_test_control()] interface.
7751251883Speter*/
7752251883Speter#define SQLITE_TESTCTRL_FIRST                    5
7753251883Speter#define SQLITE_TESTCTRL_PRNG_SAVE                5
7754251883Speter#define SQLITE_TESTCTRL_PRNG_RESTORE             6
7755355326Scy#define SQLITE_TESTCTRL_PRNG_RESET               7  /* NOT USED */
7756251883Speter#define SQLITE_TESTCTRL_BITVEC_TEST              8
7757251883Speter#define SQLITE_TESTCTRL_FAULT_INSTALL            9
7758251883Speter#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
7759251883Speter#define SQLITE_TESTCTRL_PENDING_BYTE            11
7760251883Speter#define SQLITE_TESTCTRL_ASSERT                  12
7761251883Speter#define SQLITE_TESTCTRL_ALWAYS                  13
7762362190Scy#define SQLITE_TESTCTRL_RESERVE                 14  /* NOT USED */
7763251883Speter#define SQLITE_TESTCTRL_OPTIMIZATIONS           15
7764342292Scy#define SQLITE_TESTCTRL_ISKEYWORD               16  /* NOT USED */
7765342292Scy#define SQLITE_TESTCTRL_SCRATCHMALLOC           17  /* NOT USED */
7766342292Scy#define SQLITE_TESTCTRL_INTERNAL_FUNCTIONS      17
7767251883Speter#define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
7768274884Sbapt#define SQLITE_TESTCTRL_EXPLAIN_STMT            19  /* NOT USED */
7769322444Speter#define SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD    19
7770269851Speter#define SQLITE_TESTCTRL_NEVER_CORRUPT           20
7771269851Speter#define SQLITE_TESTCTRL_VDBE_COVERAGE           21
7772269851Speter#define SQLITE_TESTCTRL_BYTEORDER               22
7773274884Sbapt#define SQLITE_TESTCTRL_ISINIT                  23
7774274884Sbapt#define SQLITE_TESTCTRL_SORTER_MMAP             24
7775282328Sbapt#define SQLITE_TESTCTRL_IMPOSTER                25
7776342292Scy#define SQLITE_TESTCTRL_PARSER_COVERAGE         26
7777351633Scy#define SQLITE_TESTCTRL_RESULT_INTREAL          27
7778355326Scy#define SQLITE_TESTCTRL_PRNG_SEED               28
7779355326Scy#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS     29
7780369950Scy#define SQLITE_TESTCTRL_SEEK_COUNT              30
7781369951Scy#define SQLITE_TESTCTRL_TRACEFLAGS              31
7782369951Scy#define SQLITE_TESTCTRL_LAST                    31  /* Largest TESTCTRL */
7783251883Speter
7784251883Speter/*
7785342292Scy** CAPI3REF: SQL Keyword Checking
7786342292Scy**
7787366076Scy** These routines provide access to the set of SQL language keywords
7788342292Scy** recognized by SQLite.  Applications can uses these routines to determine
7789342292Scy** whether or not a specific identifier needs to be escaped (for example,
7790342292Scy** by enclosing in double-quotes) so as not to confuse the parser.
7791342292Scy**
7792342292Scy** The sqlite3_keyword_count() interface returns the number of distinct
7793342292Scy** keywords understood by SQLite.
7794342292Scy**
7795342292Scy** The sqlite3_keyword_name(N,Z,L) interface finds the N-th keyword and
7796342292Scy** makes *Z point to that keyword expressed as UTF8 and writes the number
7797342292Scy** of bytes in the keyword into *L.  The string that *Z points to is not
7798342292Scy** zero-terminated.  The sqlite3_keyword_name(N,Z,L) routine returns
7799342292Scy** SQLITE_OK if N is within bounds and SQLITE_ERROR if not. If either Z
7800342292Scy** or L are NULL or invalid pointers then calls to
7801342292Scy** sqlite3_keyword_name(N,Z,L) result in undefined behavior.
7802342292Scy**
7803342292Scy** The sqlite3_keyword_check(Z,L) interface checks to see whether or not
7804342292Scy** the L-byte UTF8 identifier that Z points to is a keyword, returning non-zero
7805342292Scy** if it is and zero if not.
7806342292Scy**
7807342292Scy** The parser used by SQLite is forgiving.  It is often possible to use
7808342292Scy** a keyword as an identifier as long as such use does not result in a
7809342292Scy** parsing ambiguity.  For example, the statement
7810342292Scy** "CREATE TABLE BEGIN(REPLACE,PRAGMA,END);" is accepted by SQLite, and
7811342292Scy** creates a new table named "BEGIN" with three columns named
7812342292Scy** "REPLACE", "PRAGMA", and "END".  Nevertheless, best practice is to avoid
7813342292Scy** using keywords as identifiers.  Common techniques used to avoid keyword
7814342292Scy** name collisions include:
7815342292Scy** <ul>
7816342292Scy** <li> Put all identifier names inside double-quotes.  This is the official
7817342292Scy**      SQL way to escape identifier names.
7818342292Scy** <li> Put identifier names inside &#91;...&#93;.  This is not standard SQL,
7819342292Scy**      but it is what SQL Server does and so lots of programmers use this
7820342292Scy**      technique.
7821342292Scy** <li> Begin every identifier with the letter "Z" as no SQL keywords start
7822342292Scy**      with "Z".
7823342292Scy** <li> Include a digit somewhere in every identifier name.
7824342292Scy** </ul>
7825342292Scy**
7826342292Scy** Note that the number of keywords understood by SQLite can depend on
7827342292Scy** compile-time options.  For example, "VACUUM" is not a keyword if
7828342292Scy** SQLite is compiled with the [-DSQLITE_OMIT_VACUUM] option.  Also,
7829342292Scy** new keywords may be added to future releases of SQLite.
7830342292Scy*/
7831342292ScySQLITE_API int sqlite3_keyword_count(void);
7832342292ScySQLITE_API int sqlite3_keyword_name(int,const char**,int*);
7833342292ScySQLITE_API int sqlite3_keyword_check(const char*,int);
7834342292Scy
7835342292Scy/*
7836342292Scy** CAPI3REF: Dynamic String Object
7837342292Scy** KEYWORDS: {dynamic string}
7838342292Scy**
7839342292Scy** An instance of the sqlite3_str object contains a dynamically-sized
7840342292Scy** string under construction.
7841342292Scy**
7842342292Scy** The lifecycle of an sqlite3_str object is as follows:
7843342292Scy** <ol>
7844342292Scy** <li> ^The sqlite3_str object is created using [sqlite3_str_new()].
7845342292Scy** <li> ^Text is appended to the sqlite3_str object using various
7846342292Scy** methods, such as [sqlite3_str_appendf()].
7847342292Scy** <li> ^The sqlite3_str object is destroyed and the string it created
7848342292Scy** is returned using the [sqlite3_str_finish()] interface.
7849342292Scy** </ol>
7850342292Scy*/
7851342292Scytypedef struct sqlite3_str sqlite3_str;
7852342292Scy
7853342292Scy/*
7854342292Scy** CAPI3REF: Create A New Dynamic String Object
7855342292Scy** CONSTRUCTOR: sqlite3_str
7856342292Scy**
7857342292Scy** ^The [sqlite3_str_new(D)] interface allocates and initializes
7858342292Scy** a new [sqlite3_str] object.  To avoid memory leaks, the object returned by
7859366076Scy** [sqlite3_str_new()] must be freed by a subsequent call to
7860342292Scy** [sqlite3_str_finish(X)].
7861342292Scy**
7862342292Scy** ^The [sqlite3_str_new(D)] interface always returns a pointer to a
7863342292Scy** valid [sqlite3_str] object, though in the event of an out-of-memory
7864342292Scy** error the returned object might be a special singleton that will
7865366076Scy** silently reject new text, always return SQLITE_NOMEM from
7866366076Scy** [sqlite3_str_errcode()], always return 0 for
7867342292Scy** [sqlite3_str_length()], and always return NULL from
7868342292Scy** [sqlite3_str_finish(X)].  It is always safe to use the value
7869342292Scy** returned by [sqlite3_str_new(D)] as the sqlite3_str parameter
7870342292Scy** to any of the other [sqlite3_str] methods.
7871342292Scy**
7872342292Scy** The D parameter to [sqlite3_str_new(D)] may be NULL.  If the
7873342292Scy** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum
7874342292Scy** length of the string contained in the [sqlite3_str] object will be
7875342292Scy** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead
7876342292Scy** of [SQLITE_MAX_LENGTH].
7877342292Scy*/
7878342292ScySQLITE_API sqlite3_str *sqlite3_str_new(sqlite3*);
7879342292Scy
7880342292Scy/*
7881342292Scy** CAPI3REF: Finalize A Dynamic String
7882342292Scy** DESTRUCTOR: sqlite3_str
7883342292Scy**
7884342292Scy** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X
7885342292Scy** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()]
7886342292Scy** that contains the constructed string.  The calling application should
7887342292Scy** pass the returned value to [sqlite3_free()] to avoid a memory leak.
7888342292Scy** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any
7889342292Scy** errors were encountered during construction of the string.  ^The
7890342292Scy** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the
7891342292Scy** string in [sqlite3_str] object X is zero bytes long.
7892342292Scy*/
7893342292ScySQLITE_API char *sqlite3_str_finish(sqlite3_str*);
7894342292Scy
7895342292Scy/*
7896342292Scy** CAPI3REF: Add Content To A Dynamic String
7897342292Scy** METHOD: sqlite3_str
7898342292Scy**
7899342292Scy** These interfaces add content to an sqlite3_str object previously obtained
7900342292Scy** from [sqlite3_str_new()].
7901342292Scy**
7902366076Scy** ^The [sqlite3_str_appendf(X,F,...)] and
7903342292Scy** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf]
7904366076Scy** functionality of SQLite to append formatted text onto the end of
7905342292Scy** [sqlite3_str] object X.
7906342292Scy**
7907342292Scy** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S
7908342292Scy** onto the end of the [sqlite3_str] object X.  N must be non-negative.
7909342292Scy** S must contain at least N non-zero bytes of content.  To append a
7910342292Scy** zero-terminated string in its entirety, use the [sqlite3_str_appendall()]
7911342292Scy** method instead.
7912342292Scy**
7913342292Scy** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of
7914342292Scy** zero-terminated string S onto the end of [sqlite3_str] object X.
7915342292Scy**
7916342292Scy** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the
7917342292Scy** single-byte character C onto the end of [sqlite3_str] object X.
7918342292Scy** ^This method can be used, for example, to add whitespace indentation.
7919342292Scy**
7920342292Scy** ^The [sqlite3_str_reset(X)] method resets the string under construction
7921366076Scy** inside [sqlite3_str] object X back to zero bytes in length.
7922342292Scy**
7923342292Scy** These methods do not return a result code.  ^If an error occurs, that fact
7924342292Scy** is recorded in the [sqlite3_str] object and can be recovered by a
7925342292Scy** subsequent call to [sqlite3_str_errcode(X)].
7926342292Scy*/
7927342292ScySQLITE_API void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
7928342292ScySQLITE_API void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
7929342292ScySQLITE_API void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
7930342292ScySQLITE_API void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
7931342292ScySQLITE_API void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
7932342292ScySQLITE_API void sqlite3_str_reset(sqlite3_str*);
7933342292Scy
7934342292Scy/*
7935342292Scy** CAPI3REF: Status Of A Dynamic String
7936342292Scy** METHOD: sqlite3_str
7937342292Scy**
7938342292Scy** These interfaces return the current status of an [sqlite3_str] object.
7939342292Scy**
7940342292Scy** ^If any prior errors have occurred while constructing the dynamic string
7941342292Scy** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return
7942342292Scy** an appropriate error code.  ^The [sqlite3_str_errcode(X)] method returns
7943342292Scy** [SQLITE_NOMEM] following any out-of-memory error, or
7944342292Scy** [SQLITE_TOOBIG] if the size of the dynamic string exceeds
7945342292Scy** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors.
7946342292Scy**
7947342292Scy** ^The [sqlite3_str_length(X)] method returns the current length, in bytes,
7948342292Scy** of the dynamic string under construction in [sqlite3_str] object X.
7949342292Scy** ^The length returned by [sqlite3_str_length(X)] does not include the
7950342292Scy** zero-termination byte.
7951342292Scy**
7952342292Scy** ^The [sqlite3_str_value(X)] method returns a pointer to the current
7953342292Scy** content of the dynamic string under construction in X.  The value
7954342292Scy** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X
7955342292Scy** and might be freed or altered by any subsequent method on the same
7956342292Scy** [sqlite3_str] object.  Applications must not used the pointer returned
7957342292Scy** [sqlite3_str_value(X)] after any subsequent method call on the same
7958342292Scy** object.  ^Applications may change the content of the string returned
7959342292Scy** by [sqlite3_str_value(X)] as long as they do not write into any bytes
7960342292Scy** outside the range of 0 to [sqlite3_str_length(X)] and do not read or
7961342292Scy** write any byte after any subsequent sqlite3_str method call.
7962342292Scy*/
7963342292ScySQLITE_API int sqlite3_str_errcode(sqlite3_str*);
7964342292ScySQLITE_API int sqlite3_str_length(sqlite3_str*);
7965342292ScySQLITE_API char *sqlite3_str_value(sqlite3_str*);
7966342292Scy
7967342292Scy/*
7968251883Speter** CAPI3REF: SQLite Runtime Status
7969251883Speter**
7970282328Sbapt** ^These interfaces are used to retrieve runtime status information
7971251883Speter** about the performance of SQLite, and optionally to reset various
7972251883Speter** highwater marks.  ^The first argument is an integer code for
7973251883Speter** the specific parameter to measure.  ^(Recognized integer codes
7974251883Speter** are of the form [status parameters | SQLITE_STATUS_...].)^
7975251883Speter** ^The current value of the parameter is returned into *pCurrent.
7976251883Speter** ^The highest recorded value is returned in *pHighwater.  ^If the
7977251883Speter** resetFlag is true, then the highest record value is reset after
7978251883Speter** *pHighwater is written.  ^(Some parameters do not record the highest
7979251883Speter** value.  For those parameters
7980251883Speter** nothing is written into *pHighwater and the resetFlag is ignored.)^
7981251883Speter** ^(Other parameters record only the highwater mark and not the current
7982251883Speter** value.  For these latter parameters nothing is written into *pCurrent.)^
7983251883Speter**
7984282328Sbapt** ^The sqlite3_status() and sqlite3_status64() routines return
7985282328Sbapt** SQLITE_OK on success and a non-zero [error code] on failure.
7986251883Speter**
7987282328Sbapt** If either the current value or the highwater mark is too large to
7988282328Sbapt** be represented by a 32-bit integer, then the values returned by
7989282328Sbapt** sqlite3_status() are undefined.
7990251883Speter**
7991251883Speter** See also: [sqlite3_db_status()]
7992251883Speter*/
7993322444SpeterSQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
7994322444SpeterSQLITE_API int sqlite3_status64(
7995282328Sbapt  int op,
7996282328Sbapt  sqlite3_int64 *pCurrent,
7997282328Sbapt  sqlite3_int64 *pHighwater,
7998282328Sbapt  int resetFlag
7999282328Sbapt);
8000251883Speter
8001251883Speter
8002251883Speter/*
8003251883Speter** CAPI3REF: Status Parameters
8004251883Speter** KEYWORDS: {status parameters}
8005251883Speter**
8006251883Speter** These integer constants designate various run-time status parameters
8007251883Speter** that can be returned by [sqlite3_status()].
8008251883Speter**
8009251883Speter** <dl>
8010251883Speter** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
8011251883Speter** <dd>This parameter is the current amount of memory checked out
8012251883Speter** using [sqlite3_malloc()], either directly or indirectly.  The
8013251883Speter** figure includes calls made to [sqlite3_malloc()] by the application
8014342292Scy** and internal memory usage by the SQLite library.  Auxiliary page-cache
8015251883Speter** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
8016251883Speter** this parameter.  The amount returned is the sum of the allocation
8017251883Speter** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
8018251883Speter**
8019251883Speter** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
8020251883Speter** <dd>This parameter records the largest memory allocation request
8021251883Speter** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
8022251883Speter** internal equivalents).  Only the value returned in the
8023366076Scy** *pHighwater parameter to [sqlite3_status()] is of interest.
8024251883Speter** The value written into the *pCurrent parameter is undefined.</dd>)^
8025251883Speter**
8026251883Speter** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
8027251883Speter** <dd>This parameter records the number of separate memory allocations
8028251883Speter** currently checked out.</dd>)^
8029251883Speter**
8030251883Speter** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
8031251883Speter** <dd>This parameter returns the number of pages used out of the
8032366076Scy** [pagecache memory allocator] that was configured using
8033251883Speter** [SQLITE_CONFIG_PAGECACHE].  The
8034251883Speter** value returned is in pages, not in bytes.</dd>)^
8035251883Speter**
8036366076Scy** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
8037251883Speter** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
8038251883Speter** <dd>This parameter returns the number of bytes of page cache
8039251883Speter** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
8040251883Speter** buffer and where forced to overflow to [sqlite3_malloc()].  The
8041251883Speter** returned value includes allocations that overflowed because they
8042251883Speter** where too large (they were larger than the "sz" parameter to
8043251883Speter** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
8044251883Speter** no space was left in the page cache.</dd>)^
8045251883Speter**
8046251883Speter** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
8047251883Speter** <dd>This parameter records the largest memory allocation request
8048361456Scy** handed to the [pagecache memory allocator].  Only the value returned in the
8049366076Scy** *pHighwater parameter to [sqlite3_status()] is of interest.
8050251883Speter** The value written into the *pCurrent parameter is undefined.</dd>)^
8051251883Speter**
8052342292Scy** [[SQLITE_STATUS_SCRATCH_USED]] <dt>SQLITE_STATUS_SCRATCH_USED</dt>
8053342292Scy** <dd>No longer used.</dd>
8054251883Speter**
8055251883Speter** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
8056342292Scy** <dd>No longer used.</dd>
8057251883Speter**
8058342292Scy** [[SQLITE_STATUS_SCRATCH_SIZE]] <dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
8059342292Scy** <dd>No longer used.</dd>
8060251883Speter**
8061251883Speter** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
8062366076Scy** <dd>The *pHighwater parameter records the deepest parser stack.
8063298161Sbapt** The *pCurrent value is undefined.  The *pHighwater value is only
8064251883Speter** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
8065251883Speter** </dl>
8066251883Speter**
8067251883Speter** New status parameters may be added from time to time.
8068251883Speter*/
8069251883Speter#define SQLITE_STATUS_MEMORY_USED          0
8070251883Speter#define SQLITE_STATUS_PAGECACHE_USED       1
8071251883Speter#define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
8072342292Scy#define SQLITE_STATUS_SCRATCH_USED         3  /* NOT USED */
8073342292Scy#define SQLITE_STATUS_SCRATCH_OVERFLOW     4  /* NOT USED */
8074251883Speter#define SQLITE_STATUS_MALLOC_SIZE          5
8075251883Speter#define SQLITE_STATUS_PARSER_STACK         6
8076251883Speter#define SQLITE_STATUS_PAGECACHE_SIZE       7
8077342292Scy#define SQLITE_STATUS_SCRATCH_SIZE         8  /* NOT USED */
8078251883Speter#define SQLITE_STATUS_MALLOC_COUNT         9
8079251883Speter
8080251883Speter/*
8081251883Speter** CAPI3REF: Database Connection Status
8082286510Speter** METHOD: sqlite3
8083251883Speter**
8084366076Scy** ^This interface is used to retrieve runtime status information
8085251883Speter** about a single [database connection].  ^The first argument is the
8086251883Speter** database connection object to be interrogated.  ^The second argument
8087251883Speter** is an integer constant, taken from the set of
8088251883Speter** [SQLITE_DBSTATUS options], that
8089366076Scy** determines the parameter to interrogate.  The set of
8090251883Speter** [SQLITE_DBSTATUS options] is likely
8091251883Speter** to grow in future releases of SQLite.
8092251883Speter**
8093251883Speter** ^The current value of the requested parameter is written into *pCur
8094251883Speter** and the highest instantaneous value is written into *pHiwtr.  ^If
8095251883Speter** the resetFlg is true, then the highest instantaneous value is
8096251883Speter** reset back down to the current value.
8097251883Speter**
8098251883Speter** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
8099251883Speter** non-zero [error code] on failure.
8100251883Speter**
8101251883Speter** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
8102251883Speter*/
8103322444SpeterSQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
8104251883Speter
8105251883Speter/*
8106251883Speter** CAPI3REF: Status Parameters for database connections
8107251883Speter** KEYWORDS: {SQLITE_DBSTATUS options}
8108251883Speter**
8109251883Speter** These constants are the available integer "verbs" that can be passed as
8110251883Speter** the second argument to the [sqlite3_db_status()] interface.
8111251883Speter**
8112251883Speter** New verbs may be added in future releases of SQLite. Existing verbs
8113251883Speter** might be discontinued. Applications should check the return code from
8114251883Speter** [sqlite3_db_status()] to make sure that the call worked.
8115251883Speter** The [sqlite3_db_status()] interface will return a non-zero error code
8116251883Speter** if a discontinued or unsupported verb is invoked.
8117251883Speter**
8118251883Speter** <dl>
8119251883Speter** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
8120251883Speter** <dd>This parameter returns the number of lookaside memory slots currently
8121251883Speter** checked out.</dd>)^
8122251883Speter**
8123251883Speter** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
8124366076Scy** <dd>This parameter returns the number of malloc attempts that were
8125251883Speter** satisfied using lookaside memory. Only the high-water value is meaningful;
8126251883Speter** the current value is always zero.)^
8127251883Speter**
8128251883Speter** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
8129251883Speter** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
8130251883Speter** <dd>This parameter returns the number malloc attempts that might have
8131251883Speter** been satisfied using lookaside memory but failed due to the amount of
8132251883Speter** memory requested being larger than the lookaside slot size.
8133251883Speter** Only the high-water value is meaningful;
8134251883Speter** the current value is always zero.)^
8135251883Speter**
8136251883Speter** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
8137251883Speter** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
8138251883Speter** <dd>This parameter returns the number malloc attempts that might have
8139251883Speter** been satisfied using lookaside memory but failed due to all lookaside
8140251883Speter** memory already being in use.
8141251883Speter** Only the high-water value is meaningful;
8142251883Speter** the current value is always zero.)^
8143251883Speter**
8144251883Speter** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
8145274884Sbapt** <dd>This parameter returns the approximate number of bytes of heap
8146251883Speter** memory used by all pager caches associated with the database connection.)^
8147251883Speter** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
8148251883Speter**
8149366076Scy** [[SQLITE_DBSTATUS_CACHE_USED_SHARED]]
8150305002Scy** ^(<dt>SQLITE_DBSTATUS_CACHE_USED_SHARED</dt>
8151305002Scy** <dd>This parameter is similar to DBSTATUS_CACHE_USED, except that if a
8152305002Scy** pager cache is shared between two or more connections the bytes of heap
8153305002Scy** memory used by that pager cache is divided evenly between the attached
8154305002Scy** connections.)^  In other words, if none of the pager caches associated
8155305002Scy** with the database connection are shared, this request returns the same
8156305002Scy** value as DBSTATUS_CACHE_USED. Or, if one or more or the pager caches are
8157305002Scy** shared, the value returned by this call will be smaller than that returned
8158305002Scy** by DBSTATUS_CACHE_USED. ^The highwater mark associated with
8159305002Scy** SQLITE_DBSTATUS_CACHE_USED_SHARED is always 0.
8160305002Scy**
8161251883Speter** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
8162274884Sbapt** <dd>This parameter returns the approximate number of bytes of heap
8163251883Speter** memory used to store the schema for all databases associated
8164366076Scy** with the connection - main, temp, and any [ATTACH]-ed databases.)^
8165251883Speter** ^The full amount of memory used by the schemas is reported, even if the
8166251883Speter** schema memory is shared with other database connections due to
8167251883Speter** [shared cache mode] being enabled.
8168251883Speter** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
8169251883Speter**
8170251883Speter** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
8171274884Sbapt** <dd>This parameter returns the approximate number of bytes of heap
8172251883Speter** and lookaside memory used by all prepared statements associated with
8173251883Speter** the database connection.)^
8174251883Speter** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
8175251883Speter** </dd>
8176251883Speter**
8177251883Speter** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
8178251883Speter** <dd>This parameter returns the number of pager cache hits that have
8179366076Scy** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
8180251883Speter** is always 0.
8181251883Speter** </dd>
8182251883Speter**
8183251883Speter** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
8184251883Speter** <dd>This parameter returns the number of pager cache misses that have
8185366076Scy** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
8186251883Speter** is always 0.
8187251883Speter** </dd>
8188251883Speter**
8189251883Speter** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
8190251883Speter** <dd>This parameter returns the number of dirty cache entries that have
8191251883Speter** been written to disk. Specifically, the number of pages written to the
8192251883Speter** wal file in wal mode databases, or the number of pages written to the
8193251883Speter** database file in rollback mode databases. Any pages written as part of
8194251883Speter** transaction rollback or database recovery operations are not included.
8195251883Speter** If an IO or other error occurs while writing a page to disk, the effect
8196251883Speter** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
8197251883Speter** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
8198251883Speter** </dd>
8199269851Speter**
8200342292Scy** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
8201342292Scy** <dd>This parameter returns the number of dirty cache entries that have
8202342292Scy** been written to disk in the middle of a transaction due to the page
8203342292Scy** cache overflowing. Transactions are more efficient if they are written
8204342292Scy** to disk all at once. When pages spill mid-transaction, that introduces
8205342292Scy** additional overhead. This parameter can be used help identify
8206361456Scy** inefficiencies that can be resolved by increasing the cache size.
8207342292Scy** </dd>
8208342292Scy**
8209269851Speter** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
8210269851Speter** <dd>This parameter returns zero for the current value if and only if
8211269851Speter** all foreign key constraints (deferred or immediate) have been
8212269851Speter** resolved.)^  ^The highwater mark is always 0.
8213269851Speter** </dd>
8214251883Speter** </dl>
8215251883Speter*/
8216251883Speter#define SQLITE_DBSTATUS_LOOKASIDE_USED       0
8217251883Speter#define SQLITE_DBSTATUS_CACHE_USED           1
8218251883Speter#define SQLITE_DBSTATUS_SCHEMA_USED          2
8219251883Speter#define SQLITE_DBSTATUS_STMT_USED            3
8220251883Speter#define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
8221251883Speter#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
8222251883Speter#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
8223251883Speter#define SQLITE_DBSTATUS_CACHE_HIT            7
8224251883Speter#define SQLITE_DBSTATUS_CACHE_MISS           8
8225251883Speter#define SQLITE_DBSTATUS_CACHE_WRITE          9
8226269851Speter#define SQLITE_DBSTATUS_DEFERRED_FKS        10
8227305002Scy#define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
8228342292Scy#define SQLITE_DBSTATUS_CACHE_SPILL         12
8229342292Scy#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
8230251883Speter
8231251883Speter
8232251883Speter/*
8233251883Speter** CAPI3REF: Prepared Statement Status
8234286510Speter** METHOD: sqlite3_stmt
8235251883Speter**
8236251883Speter** ^(Each prepared statement maintains various
8237251883Speter** [SQLITE_STMTSTATUS counters] that measure the number
8238251883Speter** of times it has performed specific operations.)^  These counters can
8239251883Speter** be used to monitor the performance characteristics of the prepared
8240251883Speter** statements.  For example, if the number of table steps greatly exceeds
8241251883Speter** the number of table searches or result rows, that would tend to indicate
8242251883Speter** that the prepared statement is using a full table scan rather than
8243366076Scy** an index.
8244251883Speter**
8245251883Speter** ^(This interface is used to retrieve and reset counter values from
8246251883Speter** a [prepared statement].  The first argument is the prepared statement
8247251883Speter** object to be interrogated.  The second argument
8248251883Speter** is an integer code for a specific [SQLITE_STMTSTATUS counter]
8249251883Speter** to be interrogated.)^
8250251883Speter** ^The current value of the requested counter is returned.
8251251883Speter** ^If the resetFlg is true, then the counter is reset to zero after this
8252251883Speter** interface call returns.
8253251883Speter**
8254251883Speter** See also: [sqlite3_status()] and [sqlite3_db_status()].
8255251883Speter*/
8256322444SpeterSQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
8257251883Speter
8258251883Speter/*
8259251883Speter** CAPI3REF: Status Parameters for prepared statements
8260251883Speter** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
8261251883Speter**
8262251883Speter** These preprocessor macros define integer codes that name counter
8263251883Speter** values associated with the [sqlite3_stmt_status()] interface.
8264251883Speter** The meanings of the various counters are as follows:
8265251883Speter**
8266251883Speter** <dl>
8267251883Speter** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
8268251883Speter** <dd>^This is the number of times that SQLite has stepped forward in
8269251883Speter** a table as part of a full table scan.  Large numbers for this counter
8270366076Scy** may indicate opportunities for performance improvement through
8271251883Speter** careful use of indices.</dd>
8272251883Speter**
8273251883Speter** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
8274251883Speter** <dd>^This is the number of sort operations that have occurred.
8275251883Speter** A non-zero value in this counter may indicate an opportunity to
8276251883Speter** improvement performance through careful use of indices.</dd>
8277251883Speter**
8278251883Speter** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
8279251883Speter** <dd>^This is the number of rows inserted into transient indices that
8280251883Speter** were created automatically in order to help joins run faster.
8281251883Speter** A non-zero value in this counter may indicate an opportunity to
8282251883Speter** improvement performance by adding permanent indices that do not
8283251883Speter** need to be reinitialized each time the statement is run.</dd>
8284269851Speter**
8285269851Speter** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
8286269851Speter** <dd>^This is the number of virtual machine operations executed
8287269851Speter** by the prepared statement if that number is less than or equal
8288366076Scy** to 2147483647.  The number of virtual machine operations can be
8289269851Speter** used as a proxy for the total work done by the prepared statement.
8290269851Speter** If the number of virtual machine operations exceeds 2147483647
8291269851Speter** then the value returned by this statement status code is undefined.
8292322444Speter**
8293322444Speter** [[SQLITE_STMTSTATUS_REPREPARE]] <dt>SQLITE_STMTSTATUS_REPREPARE</dt>
8294322444Speter** <dd>^This is the number of times that the prepare statement has been
8295366076Scy** automatically regenerated due to schema changes or changes to
8296322444Speter** [bound parameters] that might affect the query plan.
8297322444Speter**
8298322444Speter** [[SQLITE_STMTSTATUS_RUN]] <dt>SQLITE_STMTSTATUS_RUN</dt>
8299322444Speter** <dd>^This is the number of times that the prepared statement has
8300322444Speter** been run.  A single "run" for the purposes of this counter is one
8301322444Speter** or more calls to [sqlite3_step()] followed by a call to [sqlite3_reset()].
8302322444Speter** The counter is incremented on the first [sqlite3_step()] call of each
8303322444Speter** cycle.
8304322444Speter**
8305322444Speter** [[SQLITE_STMTSTATUS_MEMUSED]] <dt>SQLITE_STMTSTATUS_MEMUSED</dt>
8306322444Speter** <dd>^This is the approximate number of bytes of heap memory
8307322444Speter** used to store the prepared statement.  ^This value is not actually
8308322444Speter** a counter, and so the resetFlg parameter to sqlite3_stmt_status()
8309322444Speter** is ignored when the opcode is SQLITE_STMTSTATUS_MEMUSED.
8310269851Speter** </dd>
8311251883Speter** </dl>
8312251883Speter*/
8313251883Speter#define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
8314251883Speter#define SQLITE_STMTSTATUS_SORT              2
8315251883Speter#define SQLITE_STMTSTATUS_AUTOINDEX         3
8316269851Speter#define SQLITE_STMTSTATUS_VM_STEP           4
8317322444Speter#define SQLITE_STMTSTATUS_REPREPARE         5
8318322444Speter#define SQLITE_STMTSTATUS_RUN               6
8319322444Speter#define SQLITE_STMTSTATUS_MEMUSED           99
8320251883Speter
8321251883Speter/*
8322251883Speter** CAPI3REF: Custom Page Cache Object
8323251883Speter**
8324251883Speter** The sqlite3_pcache type is opaque.  It is implemented by
8325251883Speter** the pluggable module.  The SQLite core has no knowledge of
8326251883Speter** its size or internal structure and never deals with the
8327251883Speter** sqlite3_pcache object except by holding and passing pointers
8328251883Speter** to the object.
8329251883Speter**
8330251883Speter** See [sqlite3_pcache_methods2] for additional information.
8331251883Speter*/
8332251883Spetertypedef struct sqlite3_pcache sqlite3_pcache;
8333251883Speter
8334251883Speter/*
8335251883Speter** CAPI3REF: Custom Page Cache Object
8336251883Speter**
8337251883Speter** The sqlite3_pcache_page object represents a single page in the
8338251883Speter** page cache.  The page cache will allocate instances of this
8339251883Speter** object.  Various methods of the page cache use pointers to instances
8340251883Speter** of this object as parameters or as their return value.
8341251883Speter**
8342251883Speter** See [sqlite3_pcache_methods2] for additional information.
8343251883Speter*/
8344251883Spetertypedef struct sqlite3_pcache_page sqlite3_pcache_page;
8345251883Speterstruct sqlite3_pcache_page {
8346251883Speter  void *pBuf;        /* The content of the page */
8347251883Speter  void *pExtra;      /* Extra information associated with the page */
8348251883Speter};
8349251883Speter
8350251883Speter/*
8351251883Speter** CAPI3REF: Application Defined Page Cache.
8352251883Speter** KEYWORDS: {page cache}
8353251883Speter**
8354251883Speter** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
8355366076Scy** register an alternative page cache implementation by passing in an
8356251883Speter** instance of the sqlite3_pcache_methods2 structure.)^
8357366076Scy** In many applications, most of the heap memory allocated by
8358251883Speter** SQLite is used for the page cache.
8359366076Scy** By implementing a
8360251883Speter** custom page cache using this API, an application can better control
8361366076Scy** the amount of memory consumed by SQLite, the way in which
8362366076Scy** that memory is allocated and released, and the policies used to
8363366076Scy** determine exactly which parts of a database file are cached and for
8364251883Speter** how long.
8365251883Speter**
8366251883Speter** The alternative page cache mechanism is an
8367251883Speter** extreme measure that is only needed by the most demanding applications.
8368251883Speter** The built-in page cache is recommended for most uses.
8369251883Speter**
8370251883Speter** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
8371251883Speter** internal buffer by SQLite within the call to [sqlite3_config].  Hence
8372251883Speter** the application may discard the parameter after the call to
8373251883Speter** [sqlite3_config()] returns.)^
8374251883Speter**
8375251883Speter** [[the xInit() page cache method]]
8376366076Scy** ^(The xInit() method is called once for each effective
8377251883Speter** call to [sqlite3_initialize()])^
8378251883Speter** (usually only once during the lifetime of the process). ^(The xInit()
8379251883Speter** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
8380366076Scy** The intent of the xInit() method is to set up global data structures
8381366076Scy** required by the custom page cache implementation.
8382366076Scy** ^(If the xInit() method is NULL, then the
8383251883Speter** built-in default page cache is used instead of the application defined
8384251883Speter** page cache.)^
8385251883Speter**
8386251883Speter** [[the xShutdown() page cache method]]
8387251883Speter** ^The xShutdown() method is called by [sqlite3_shutdown()].
8388366076Scy** It can be used to clean up
8389251883Speter** any outstanding resources before process shutdown, if required.
8390251883Speter** ^The xShutdown() method may be NULL.
8391251883Speter**
8392251883Speter** ^SQLite automatically serializes calls to the xInit method,
8393251883Speter** so the xInit method need not be threadsafe.  ^The
8394251883Speter** xShutdown method is only called from [sqlite3_shutdown()] so it does
8395251883Speter** not need to be threadsafe either.  All other methods must be threadsafe
8396251883Speter** in multithreaded applications.
8397251883Speter**
8398251883Speter** ^SQLite will never invoke xInit() more than once without an intervening
8399251883Speter** call to xShutdown().
8400251883Speter**
8401251883Speter** [[the xCreate() page cache methods]]
8402251883Speter** ^SQLite invokes the xCreate() method to construct a new cache instance.
8403251883Speter** SQLite will typically create one cache instance for each open database file,
8404251883Speter** though this is not guaranteed. ^The
8405251883Speter** first parameter, szPage, is the size in bytes of the pages that must
8406251883Speter** be allocated by the cache.  ^szPage will always a power of two.  ^The
8407366076Scy** second parameter szExtra is a number of bytes of extra storage
8408251883Speter** associated with each page cache entry.  ^The szExtra parameter will
8409251883Speter** a number less than 250.  SQLite will use the
8410251883Speter** extra szExtra bytes on each page to store metadata about the underlying
8411251883Speter** database page on disk.  The value passed into szExtra depends
8412251883Speter** on the SQLite version, the target platform, and how SQLite was compiled.
8413251883Speter** ^The third argument to xCreate(), bPurgeable, is true if the cache being
8414251883Speter** created will be used to cache database pages of a file stored on disk, or
8415251883Speter** false if it is used for an in-memory database. The cache implementation
8416251883Speter** does not have to do anything special based with the value of bPurgeable;
8417251883Speter** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
8418251883Speter** never invoke xUnpin() except to deliberately delete a page.
8419251883Speter** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
8420366076Scy** false will always have the "discard" flag set to true.
8421251883Speter** ^Hence, a cache created with bPurgeable false will
8422251883Speter** never contain any unpinned pages.
8423251883Speter**
8424251883Speter** [[the xCachesize() page cache method]]
8425251883Speter** ^(The xCachesize() method may be called at any time by SQLite to set the
8426251883Speter** suggested maximum cache-size (number of pages stored by) the cache
8427251883Speter** instance passed as the first argument. This is the value configured using
8428251883Speter** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
8429251883Speter** parameter, the implementation is not required to do anything with this
8430251883Speter** value; it is advisory only.
8431251883Speter**
8432251883Speter** [[the xPagecount() page cache methods]]
8433251883Speter** The xPagecount() method must return the number of pages currently
8434251883Speter** stored in the cache, both pinned and unpinned.
8435366076Scy**
8436251883Speter** [[the xFetch() page cache methods]]
8437366076Scy** The xFetch() method locates a page in the cache and returns a pointer to
8438251883Speter** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
8439251883Speter** The pBuf element of the returned sqlite3_pcache_page object will be a
8440366076Scy** pointer to a buffer of szPage bytes used to store the content of a
8441251883Speter** single database page.  The pExtra element of sqlite3_pcache_page will be
8442251883Speter** a pointer to the szExtra bytes of extra storage that SQLite has requested
8443251883Speter** for each entry in the page cache.
8444251883Speter**
8445251883Speter** The page to be fetched is determined by the key. ^The minimum key value
8446251883Speter** is 1.  After it has been retrieved using xFetch, the page is considered
8447251883Speter** to be "pinned".
8448251883Speter**
8449251883Speter** If the requested page is already in the page cache, then the page cache
8450251883Speter** implementation must return a pointer to the page buffer with its content
8451251883Speter** intact.  If the requested page is not already in the cache, then the
8452251883Speter** cache implementation should use the value of the createFlag
8453251883Speter** parameter to help it determined what action to take:
8454251883Speter**
8455251883Speter** <table border=1 width=85% align=center>
8456251883Speter** <tr><th> createFlag <th> Behavior when page is not already in cache
8457251883Speter** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
8458251883Speter** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
8459251883Speter**                 Otherwise return NULL.
8460251883Speter** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
8461251883Speter**                 NULL if allocating a new page is effectively impossible.
8462251883Speter** </table>
8463251883Speter**
8464251883Speter** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
8465251883Speter** will only use a createFlag of 2 after a prior call with a createFlag of 1
8466361456Scy** failed.)^  In between the xFetch() calls, SQLite may
8467251883Speter** attempt to unpin one or more cache pages by spilling the content of
8468251883Speter** pinned pages to disk and synching the operating system disk cache.
8469251883Speter**
8470251883Speter** [[the xUnpin() page cache method]]
8471251883Speter** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
8472251883Speter** as its second argument.  If the third parameter, discard, is non-zero,
8473251883Speter** then the page must be evicted from the cache.
8474251883Speter** ^If the discard parameter is
8475251883Speter** zero, then the page may be discarded or retained at the discretion of
8476251883Speter** page cache implementation. ^The page cache implementation
8477251883Speter** may choose to evict unpinned pages at any time.
8478251883Speter**
8479366076Scy** The cache must not perform any reference counting. A single
8480366076Scy** call to xUnpin() unpins the page regardless of the number of prior calls
8481251883Speter** to xFetch().
8482251883Speter**
8483251883Speter** [[the xRekey() page cache methods]]
8484251883Speter** The xRekey() method is used to change the key value associated with the
8485251883Speter** page passed as the second argument. If the cache
8486251883Speter** previously contains an entry associated with newKey, it must be
8487251883Speter** discarded. ^Any prior cache entry associated with newKey is guaranteed not
8488251883Speter** to be pinned.
8489251883Speter**
8490251883Speter** When SQLite calls the xTruncate() method, the cache must discard all
8491251883Speter** existing cache entries with page numbers (keys) greater than or equal
8492251883Speter** to the value of the iLimit parameter passed to xTruncate(). If any
8493251883Speter** of these pages are pinned, they are implicitly unpinned, meaning that
8494251883Speter** they can be safely discarded.
8495251883Speter**
8496251883Speter** [[the xDestroy() page cache method]]
8497251883Speter** ^The xDestroy() method is used to delete a cache allocated by xCreate().
8498251883Speter** All resources associated with the specified cache should be freed. ^After
8499251883Speter** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
8500251883Speter** handle invalid, and will not use it with any other sqlite3_pcache_methods2
8501251883Speter** functions.
8502251883Speter**
8503251883Speter** [[the xShrink() page cache method]]
8504251883Speter** ^SQLite invokes the xShrink() method when it wants the page cache to
8505251883Speter** free up as much of heap memory as possible.  The page cache implementation
8506251883Speter** is not obligated to free any memory, but well-behaved implementations should
8507251883Speter** do their best.
8508251883Speter*/
8509251883Spetertypedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
8510251883Speterstruct sqlite3_pcache_methods2 {
8511251883Speter  int iVersion;
8512251883Speter  void *pArg;
8513251883Speter  int (*xInit)(void*);
8514251883Speter  void (*xShutdown)(void*);
8515251883Speter  sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
8516251883Speter  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
8517251883Speter  int (*xPagecount)(sqlite3_pcache*);
8518251883Speter  sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
8519251883Speter  void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
8520366076Scy  void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
8521251883Speter      unsigned oldKey, unsigned newKey);
8522251883Speter  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
8523251883Speter  void (*xDestroy)(sqlite3_pcache*);
8524251883Speter  void (*xShrink)(sqlite3_pcache*);
8525251883Speter};
8526251883Speter
8527251883Speter/*
8528251883Speter** This is the obsolete pcache_methods object that has now been replaced
8529251883Speter** by sqlite3_pcache_methods2.  This object is not used by SQLite.  It is
8530251883Speter** retained in the header file for backwards compatibility only.
8531251883Speter*/
8532251883Spetertypedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
8533251883Speterstruct sqlite3_pcache_methods {
8534251883Speter  void *pArg;
8535251883Speter  int (*xInit)(void*);
8536251883Speter  void (*xShutdown)(void*);
8537251883Speter  sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
8538251883Speter  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
8539251883Speter  int (*xPagecount)(sqlite3_pcache*);
8540251883Speter  void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
8541251883Speter  void (*xUnpin)(sqlite3_pcache*, void*, int discard);
8542251883Speter  void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
8543251883Speter  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
8544251883Speter  void (*xDestroy)(sqlite3_pcache*);
8545251883Speter};
8546251883Speter
8547251883Speter
8548251883Speter/*
8549251883Speter** CAPI3REF: Online Backup Object
8550251883Speter**
8551251883Speter** The sqlite3_backup object records state information about an ongoing
8552251883Speter** online backup operation.  ^The sqlite3_backup object is created by
8553251883Speter** a call to [sqlite3_backup_init()] and is destroyed by a call to
8554251883Speter** [sqlite3_backup_finish()].
8555251883Speter**
8556251883Speter** See Also: [Using the SQLite Online Backup API]
8557251883Speter*/
8558251883Spetertypedef struct sqlite3_backup sqlite3_backup;
8559251883Speter
8560251883Speter/*
8561251883Speter** CAPI3REF: Online Backup API.
8562251883Speter**
8563251883Speter** The backup API copies the content of one database into another.
8564251883Speter** It is useful either for creating backups of databases or
8565366076Scy** for copying in-memory databases to or from persistent files.
8566251883Speter**
8567251883Speter** See Also: [Using the SQLite Online Backup API]
8568251883Speter**
8569251883Speter** ^SQLite holds a write transaction open on the destination database file
8570251883Speter** for the duration of the backup operation.
8571251883Speter** ^The source database is read-locked only while it is being read;
8572251883Speter** it is not locked continuously for the entire backup operation.
8573251883Speter** ^Thus, the backup may be performed on a live source database without
8574251883Speter** preventing other database connections from
8575251883Speter** reading or writing to the source database while the backup is underway.
8576366076Scy**
8577366076Scy** ^(To perform a backup operation:
8578251883Speter**   <ol>
8579251883Speter**     <li><b>sqlite3_backup_init()</b> is called once to initialize the
8580366076Scy**         backup,
8581366076Scy**     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
8582251883Speter**         the data between the two databases, and finally
8583366076Scy**     <li><b>sqlite3_backup_finish()</b> is called to release all resources
8584366076Scy**         associated with the backup operation.
8585251883Speter**   </ol>)^
8586251883Speter** There should be exactly one call to sqlite3_backup_finish() for each
8587251883Speter** successful call to sqlite3_backup_init().
8588251883Speter**
8589251883Speter** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
8590251883Speter**
8591366076Scy** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
8592366076Scy** [database connection] associated with the destination database
8593251883Speter** and the database name, respectively.
8594251883Speter** ^The database name is "main" for the main database, "temp" for the
8595251883Speter** temporary database, or the name specified after the AS keyword in
8596251883Speter** an [ATTACH] statement for an attached database.
8597366076Scy** ^The S and M arguments passed to
8598251883Speter** sqlite3_backup_init(D,N,S,M) identify the [database connection]
8599251883Speter** and database name of the source database, respectively.
8600251883Speter** ^The source and destination [database connections] (parameters S and D)
8601251883Speter** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
8602251883Speter** an error.
8603251883Speter**
8604366076Scy** ^A call to sqlite3_backup_init() will fail, returning NULL, if
8605366076Scy** there is already a read or read-write transaction open on the
8606282328Sbapt** destination database.
8607282328Sbapt**
8608251883Speter** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
8609251883Speter** returned and an error code and error message are stored in the
8610251883Speter** destination [database connection] D.
8611251883Speter** ^The error code and message for the failed call to sqlite3_backup_init()
8612251883Speter** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
8613251883Speter** [sqlite3_errmsg16()] functions.
8614251883Speter** ^A successful call to sqlite3_backup_init() returns a pointer to an
8615251883Speter** [sqlite3_backup] object.
8616251883Speter** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
8617366076Scy** sqlite3_backup_finish() functions to perform the specified backup
8618251883Speter** operation.
8619251883Speter**
8620251883Speter** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
8621251883Speter**
8622366076Scy** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
8623251883Speter** the source and destination databases specified by [sqlite3_backup] object B.
8624366076Scy** ^If N is negative, all remaining source pages are copied.
8625251883Speter** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
8626251883Speter** are still more pages to be copied, then the function returns [SQLITE_OK].
8627251883Speter** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
8628251883Speter** from source to destination, then it returns [SQLITE_DONE].
8629251883Speter** ^If an error occurs while running sqlite3_backup_step(B,N),
8630251883Speter** then an [error code] is returned. ^As well as [SQLITE_OK] and
8631251883Speter** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
8632251883Speter** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
8633251883Speter** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
8634251883Speter**
8635251883Speter** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
8636251883Speter** <ol>
8637251883Speter** <li> the destination database was opened read-only, or
8638251883Speter** <li> the destination database is using write-ahead-log journaling
8639251883Speter** and the destination and source page sizes differ, or
8640251883Speter** <li> the destination database is an in-memory database and the
8641251883Speter** destination and source page sizes differ.
8642251883Speter** </ol>)^
8643251883Speter**
8644251883Speter** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
8645251883Speter** the [sqlite3_busy_handler | busy-handler function]
8646366076Scy** is invoked (if one is specified). ^If the
8647366076Scy** busy-handler returns non-zero before the lock is available, then
8648251883Speter** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
8649251883Speter** sqlite3_backup_step() can be retried later. ^If the source
8650251883Speter** [database connection]
8651251883Speter** is being used to write to the source database when sqlite3_backup_step()
8652251883Speter** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
8653251883Speter** case the call to sqlite3_backup_step() can be retried later on. ^(If
8654251883Speter** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
8655366076Scy** [SQLITE_READONLY] is returned, then
8656366076Scy** there is no point in retrying the call to sqlite3_backup_step(). These
8657366076Scy** errors are considered fatal.)^  The application must accept
8658366076Scy** that the backup operation has failed and pass the backup operation handle
8659251883Speter** to the sqlite3_backup_finish() to release associated resources.
8660251883Speter**
8661251883Speter** ^The first call to sqlite3_backup_step() obtains an exclusive lock
8662366076Scy** on the destination file. ^The exclusive lock is not released until either
8663366076Scy** sqlite3_backup_finish() is called or the backup operation is complete
8664251883Speter** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
8665251883Speter** sqlite3_backup_step() obtains a [shared lock] on the source database that
8666251883Speter** lasts for the duration of the sqlite3_backup_step() call.
8667251883Speter** ^Because the source database is not locked between calls to
8668251883Speter** sqlite3_backup_step(), the source database may be modified mid-way
8669251883Speter** through the backup process.  ^If the source database is modified by an
8670251883Speter** external process or via a database connection other than the one being
8671251883Speter** used by the backup operation, then the backup will be automatically
8672366076Scy** restarted by the next call to sqlite3_backup_step(). ^If the source
8673251883Speter** database is modified by the using the same database connection as is used
8674251883Speter** by the backup operation, then the backup database is automatically
8675251883Speter** updated at the same time.
8676251883Speter**
8677251883Speter** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
8678251883Speter**
8679366076Scy** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
8680251883Speter** application wishes to abandon the backup operation, the application
8681251883Speter** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
8682251883Speter** ^The sqlite3_backup_finish() interfaces releases all
8683366076Scy** resources associated with the [sqlite3_backup] object.
8684251883Speter** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
8685251883Speter** active write-transaction on the destination database is rolled back.
8686251883Speter** The [sqlite3_backup] object is invalid
8687251883Speter** and may not be used following a call to sqlite3_backup_finish().
8688251883Speter**
8689251883Speter** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
8690251883Speter** sqlite3_backup_step() errors occurred, regardless or whether or not
8691251883Speter** sqlite3_backup_step() completed.
8692251883Speter** ^If an out-of-memory condition or IO error occurred during any prior
8693251883Speter** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
8694251883Speter** sqlite3_backup_finish() returns the corresponding [error code].
8695251883Speter**
8696251883Speter** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
8697251883Speter** is not a permanent error and does not affect the return value of
8698251883Speter** sqlite3_backup_finish().
8699251883Speter**
8700282328Sbapt** [[sqlite3_backup_remaining()]] [[sqlite3_backup_pagecount()]]
8701251883Speter** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
8702251883Speter**
8703282328Sbapt** ^The sqlite3_backup_remaining() routine returns the number of pages still
8704282328Sbapt** to be backed up at the conclusion of the most recent sqlite3_backup_step().
8705282328Sbapt** ^The sqlite3_backup_pagecount() routine returns the total number of pages
8706282328Sbapt** in the source database at the conclusion of the most recent
8707282328Sbapt** sqlite3_backup_step().
8708282328Sbapt** ^(The values returned by these functions are only updated by
8709282328Sbapt** sqlite3_backup_step(). If the source database is modified in a way that
8710282328Sbapt** changes the size of the source database or the number of pages remaining,
8711282328Sbapt** those changes are not reflected in the output of sqlite3_backup_pagecount()
8712282328Sbapt** and sqlite3_backup_remaining() until after the next
8713282328Sbapt** sqlite3_backup_step().)^
8714251883Speter**
8715251883Speter** <b>Concurrent Usage of Database Handles</b>
8716251883Speter**
8717251883Speter** ^The source [database connection] may be used by the application for other
8718251883Speter** purposes while a backup operation is underway or being initialized.
8719251883Speter** ^If SQLite is compiled and configured to support threadsafe database
8720251883Speter** connections, then the source database connection may be used concurrently
8721251883Speter** from within other threads.
8722251883Speter**
8723366076Scy** However, the application must guarantee that the destination
8724366076Scy** [database connection] is not passed to any other API (by any thread) after
8725251883Speter** sqlite3_backup_init() is called and before the corresponding call to
8726251883Speter** sqlite3_backup_finish().  SQLite does not currently check to see
8727251883Speter** if the application incorrectly accesses the destination [database connection]
8728251883Speter** and so no error code is reported, but the operations may malfunction
8729251883Speter** nevertheless.  Use of the destination database connection while a
8730251883Speter** backup is in progress might also also cause a mutex deadlock.
8731251883Speter**
8732251883Speter** If running in [shared cache mode], the application must
8733251883Speter** guarantee that the shared cache used by the destination database
8734251883Speter** is not accessed while the backup is running. In practice this means
8735366076Scy** that the application must guarantee that the disk file being
8736251883Speter** backed up to is not accessed by any connection within the process,
8737251883Speter** not just the specific connection that was passed to sqlite3_backup_init().
8738251883Speter**
8739366076Scy** The [sqlite3_backup] object itself is partially threadsafe. Multiple
8740251883Speter** threads may safely make multiple concurrent calls to sqlite3_backup_step().
8741251883Speter** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
8742251883Speter** APIs are not strictly speaking threadsafe. If they are invoked at the
8743251883Speter** same time as another thread is invoking sqlite3_backup_step() it is
8744251883Speter** possible that they return invalid values.
8745251883Speter*/
8746322444SpeterSQLITE_API sqlite3_backup *sqlite3_backup_init(
8747251883Speter  sqlite3 *pDest,                        /* Destination database handle */
8748251883Speter  const char *zDestName,                 /* Destination database name */
8749251883Speter  sqlite3 *pSource,                      /* Source database handle */
8750251883Speter  const char *zSourceName                /* Source database name */
8751251883Speter);
8752322444SpeterSQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
8753322444SpeterSQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
8754322444SpeterSQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
8755322444SpeterSQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
8756251883Speter
8757251883Speter/*
8758251883Speter** CAPI3REF: Unlock Notification
8759286510Speter** METHOD: sqlite3
8760251883Speter**
8761251883Speter** ^When running in shared-cache mode, a database operation may fail with
8762251883Speter** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
8763251883Speter** individual tables within the shared-cache cannot be obtained. See
8764366076Scy** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
8765366076Scy** ^This API may be used to register a callback that SQLite will invoke
8766251883Speter** when the connection currently holding the required lock relinquishes it.
8767251883Speter** ^This API is only available if the library was compiled with the
8768251883Speter** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
8769251883Speter**
8770251883Speter** See Also: [Using the SQLite Unlock Notification Feature].
8771251883Speter**
8772251883Speter** ^Shared-cache locks are released when a database connection concludes
8773366076Scy** its current transaction, either by committing it or rolling it back.
8774251883Speter**
8775251883Speter** ^When a connection (known as the blocked connection) fails to obtain a
8776251883Speter** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
8777251883Speter** identity of the database connection (the blocking connection) that
8778366076Scy** has locked the required resource is stored internally. ^After an
8779251883Speter** application receives an SQLITE_LOCKED error, it may call the
8780366076Scy** sqlite3_unlock_notify() method with the blocked connection handle as
8781251883Speter** the first argument to register for a callback that will be invoked
8782251883Speter** when the blocking connections current transaction is concluded. ^The
8783251883Speter** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
8784361456Scy** call that concludes the blocking connection's transaction.
8785251883Speter**
8786251883Speter** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
8787251883Speter** there is a chance that the blocking connection will have already
8788251883Speter** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
8789251883Speter** If this happens, then the specified callback is invoked immediately,
8790251883Speter** from within the call to sqlite3_unlock_notify().)^
8791251883Speter**
8792251883Speter** ^If the blocked connection is attempting to obtain a write-lock on a
8793251883Speter** shared-cache table, and more than one other connection currently holds
8794366076Scy** a read-lock on the same table, then SQLite arbitrarily selects one of
8795251883Speter** the other connections to use as the blocking connection.
8796251883Speter**
8797366076Scy** ^(There may be at most one unlock-notify callback registered by a
8798251883Speter** blocked connection. If sqlite3_unlock_notify() is called when the
8799251883Speter** blocked connection already has a registered unlock-notify callback,
8800251883Speter** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
8801251883Speter** called with a NULL pointer as its second argument, then any existing
8802366076Scy** unlock-notify callback is canceled. ^The blocked connections
8803251883Speter** unlock-notify callback may also be canceled by closing the blocked
8804251883Speter** connection using [sqlite3_close()].
8805251883Speter**
8806251883Speter** The unlock-notify callback is not reentrant. If an application invokes
8807251883Speter** any sqlite3_xxx API functions from within an unlock-notify callback, a
8808251883Speter** crash or deadlock may be the result.
8809251883Speter**
8810251883Speter** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
8811251883Speter** returns SQLITE_OK.
8812251883Speter**
8813251883Speter** <b>Callback Invocation Details</b>
8814251883Speter**
8815366076Scy** When an unlock-notify callback is registered, the application provides a
8816251883Speter** single void* pointer that is passed to the callback when it is invoked.
8817251883Speter** However, the signature of the callback function allows SQLite to pass
8818251883Speter** it an array of void* context pointers. The first argument passed to
8819251883Speter** an unlock-notify callback is a pointer to an array of void* pointers,
8820251883Speter** and the second is the number of entries in the array.
8821251883Speter**
8822361456Scy** When a blocking connection's transaction is concluded, there may be
8823251883Speter** more than one blocked connection that has registered for an unlock-notify
8824251883Speter** callback. ^If two or more such blocked connections have specified the
8825251883Speter** same callback function, then instead of invoking the callback function
8826251883Speter** multiple times, it is invoked once with the set of void* context pointers
8827251883Speter** specified by the blocked connections bundled together into an array.
8828366076Scy** This gives the application an opportunity to prioritize any actions
8829251883Speter** related to the set of unblocked database connections.
8830251883Speter**
8831251883Speter** <b>Deadlock Detection</b>
8832251883Speter**
8833366076Scy** Assuming that after registering for an unlock-notify callback a
8834251883Speter** database waits for the callback to be issued before taking any further
8835251883Speter** action (a reasonable assumption), then using this API may cause the
8836251883Speter** application to deadlock. For example, if connection X is waiting for
8837251883Speter** connection Y's transaction to be concluded, and similarly connection
8838251883Speter** Y is waiting on connection X's transaction, then neither connection
8839251883Speter** will proceed and the system may remain deadlocked indefinitely.
8840251883Speter**
8841251883Speter** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
8842251883Speter** detection. ^If a given call to sqlite3_unlock_notify() would put the
8843251883Speter** system in a deadlocked state, then SQLITE_LOCKED is returned and no
8844251883Speter** unlock-notify callback is registered. The system is said to be in
8845251883Speter** a deadlocked state if connection A has registered for an unlock-notify
8846251883Speter** callback on the conclusion of connection B's transaction, and connection
8847251883Speter** B has itself registered for an unlock-notify callback when connection
8848251883Speter** A's transaction is concluded. ^Indirect deadlock is also detected, so
8849251883Speter** the system is also considered to be deadlocked if connection B has
8850251883Speter** registered for an unlock-notify callback on the conclusion of connection
8851251883Speter** C's transaction, where connection C is waiting on connection A. ^Any
8852251883Speter** number of levels of indirection are allowed.
8853251883Speter**
8854251883Speter** <b>The "DROP TABLE" Exception</b>
8855251883Speter**
8856366076Scy** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
8857251883Speter** always appropriate to call sqlite3_unlock_notify(). There is however,
8858251883Speter** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
8859251883Speter** SQLite checks if there are any currently executing SELECT statements
8860251883Speter** that belong to the same connection. If there are, SQLITE_LOCKED is
8861251883Speter** returned. In this case there is no "blocking connection", so invoking
8862251883Speter** sqlite3_unlock_notify() results in the unlock-notify callback being
8863251883Speter** invoked immediately. If the application then re-attempts the "DROP TABLE"
8864251883Speter** or "DROP INDEX" query, an infinite loop might be the result.
8865251883Speter**
8866251883Speter** One way around this problem is to check the extended error code returned
8867251883Speter** by an sqlite3_step() call. ^(If there is a blocking connection, then the
8868251883Speter** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
8869366076Scy** the special "DROP TABLE/INDEX" case, the extended error code is just
8870251883Speter** SQLITE_LOCKED.)^
8871251883Speter*/
8872322444SpeterSQLITE_API int sqlite3_unlock_notify(
8873251883Speter  sqlite3 *pBlocked,                          /* Waiting connection */
8874251883Speter  void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
8875251883Speter  void *pNotifyArg                            /* Argument to pass to xNotify */
8876251883Speter);
8877251883Speter
8878251883Speter
8879251883Speter/*
8880251883Speter** CAPI3REF: String Comparison
8881251883Speter**
8882251883Speter** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
8883251883Speter** and extensions to compare the contents of two buffers containing UTF-8
8884251883Speter** strings in a case-independent fashion, using the same definition of "case
8885251883Speter** independence" that SQLite uses internally when comparing identifiers.
8886251883Speter*/
8887322444SpeterSQLITE_API int sqlite3_stricmp(const char *, const char *);
8888322444SpeterSQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
8889251883Speter
8890251883Speter/*
8891251883Speter** CAPI3REF: String Globbing
8892251883Speter*
8893298161Sbapt** ^The [sqlite3_strglob(P,X)] interface returns zero if and only if
8894298161Sbapt** string X matches the [GLOB] pattern P.
8895298161Sbapt** ^The definition of [GLOB] pattern matching used in
8896251883Speter** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
8897298161Sbapt** SQL dialect understood by SQLite.  ^The [sqlite3_strglob(P,X)] function
8898298161Sbapt** is case sensitive.
8899251883Speter**
8900251883Speter** Note that this routine returns zero on a match and non-zero if the strings
8901251883Speter** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
8902298161Sbapt**
8903298161Sbapt** See also: [sqlite3_strlike()].
8904251883Speter*/
8905322444SpeterSQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
8906251883Speter
8907251883Speter/*
8908298161Sbapt** CAPI3REF: String LIKE Matching
8909298161Sbapt*
8910298161Sbapt** ^The [sqlite3_strlike(P,X,E)] interface returns zero if and only if
8911298161Sbapt** string X matches the [LIKE] pattern P with escape character E.
8912298161Sbapt** ^The definition of [LIKE] pattern matching used in
8913298161Sbapt** [sqlite3_strlike(P,X,E)] is the same as for the "X LIKE P ESCAPE E"
8914298161Sbapt** operator in the SQL dialect understood by SQLite.  ^For "X LIKE P" without
8915298161Sbapt** the ESCAPE clause, set the E parameter of [sqlite3_strlike(P,X,E)] to 0.
8916298161Sbapt** ^As with the LIKE operator, the [sqlite3_strlike(P,X,E)] function is case
8917298161Sbapt** insensitive - equivalent upper and lower case ASCII characters match
8918298161Sbapt** one another.
8919298161Sbapt**
8920298161Sbapt** ^The [sqlite3_strlike(P,X,E)] function matches Unicode characters, though
8921298161Sbapt** only ASCII characters are case folded.
8922298161Sbapt**
8923298161Sbapt** Note that this routine returns zero on a match and non-zero if the strings
8924298161Sbapt** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
8925298161Sbapt**
8926298161Sbapt** See also: [sqlite3_strglob()].
8927298161Sbapt*/
8928322444SpeterSQLITE_API int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
8929298161Sbapt
8930298161Sbapt/*
8931251883Speter** CAPI3REF: Error Logging Interface
8932251883Speter**
8933251883Speter** ^The [sqlite3_log()] interface writes a message into the [error log]
8934251883Speter** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
8935251883Speter** ^If logging is enabled, the zFormat string and subsequent arguments are
8936251883Speter** used with [sqlite3_snprintf()] to generate the final output string.
8937251883Speter**
8938251883Speter** The sqlite3_log() interface is intended for use by extensions such as
8939251883Speter** virtual tables, collating functions, and SQL functions.  While there is
8940251883Speter** nothing to prevent an application from calling sqlite3_log(), doing so
8941251883Speter** is considered bad form.
8942251883Speter**
8943251883Speter** The zFormat string must not be NULL.
8944251883Speter**
8945251883Speter** To avoid deadlocks and other threading problems, the sqlite3_log() routine
8946251883Speter** will not use dynamically allocated memory.  The log message is stored in
8947251883Speter** a fixed-length buffer on the stack.  If the log message is longer than
8948251883Speter** a few hundred characters, it will be truncated to the length of the
8949251883Speter** buffer.
8950251883Speter*/
8951322444SpeterSQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
8952251883Speter
8953251883Speter/*
8954251883Speter** CAPI3REF: Write-Ahead Log Commit Hook
8955286510Speter** METHOD: sqlite3
8956251883Speter**
8957251883Speter** ^The [sqlite3_wal_hook()] function is used to register a callback that
8958282328Sbapt** is invoked each time data is committed to a database in wal mode.
8959251883Speter**
8960366076Scy** ^(The callback is invoked by SQLite after the commit has taken place and
8961366076Scy** the associated write-lock on the database released)^, so the implementation
8962251883Speter** may read, write or [checkpoint] the database as required.
8963251883Speter**
8964251883Speter** ^The first parameter passed to the callback function when it is invoked
8965251883Speter** is a copy of the third parameter passed to sqlite3_wal_hook() when
8966251883Speter** registering the callback. ^The second is a copy of the database handle.
8967251883Speter** ^The third parameter is the name of the database that was written to -
8968251883Speter** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
8969251883Speter** is the number of pages currently in the write-ahead log file,
8970251883Speter** including those that were just committed.
8971251883Speter**
8972251883Speter** The callback function should normally return [SQLITE_OK].  ^If an error
8973251883Speter** code is returned, that error will propagate back up through the
8974251883Speter** SQLite code base to cause the statement that provoked the callback
8975251883Speter** to report an error, though the commit will have still occurred. If the
8976251883Speter** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
8977251883Speter** that does not correspond to any valid SQLite error code, the results
8978251883Speter** are undefined.
8979251883Speter**
8980366076Scy** A single database handle may have at most a single write-ahead log callback
8981251883Speter** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
8982251883Speter** previously registered write-ahead log callback. ^Note that the
8983251883Speter** [sqlite3_wal_autocheckpoint()] interface and the
8984251883Speter** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
8985298161Sbapt** overwrite any prior [sqlite3_wal_hook()] settings.
8986251883Speter*/
8987322444SpeterSQLITE_API void *sqlite3_wal_hook(
8988366076Scy  sqlite3*,
8989251883Speter  int(*)(void *,sqlite3*,const char*,int),
8990251883Speter  void*
8991251883Speter);
8992251883Speter
8993251883Speter/*
8994251883Speter** CAPI3REF: Configure an auto-checkpoint
8995286510Speter** METHOD: sqlite3
8996251883Speter**
8997251883Speter** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
8998251883Speter** [sqlite3_wal_hook()] that causes any database on [database connection] D
8999251883Speter** to automatically [checkpoint]
9000251883Speter** after committing a transaction if there are N or
9001366076Scy** more frames in the [write-ahead log] file.  ^Passing zero or
9002251883Speter** a negative value as the nFrame parameter disables automatic
9003251883Speter** checkpoints entirely.
9004251883Speter**
9005251883Speter** ^The callback registered by this function replaces any existing callback
9006251883Speter** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
9007251883Speter** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
9008251883Speter** configured by this function.
9009251883Speter**
9010251883Speter** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
9011251883Speter** from SQL.
9012251883Speter**
9013274884Sbapt** ^Checkpoints initiated by this mechanism are
9014274884Sbapt** [sqlite3_wal_checkpoint_v2|PASSIVE].
9015274884Sbapt**
9016251883Speter** ^Every new [database connection] defaults to having the auto-checkpoint
9017251883Speter** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
9018251883Speter** pages.  The use of this interface
9019251883Speter** is only necessary if the default setting is found to be suboptimal
9020251883Speter** for a particular application.
9021251883Speter*/
9022322444SpeterSQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
9023251883Speter
9024251883Speter/*
9025251883Speter** CAPI3REF: Checkpoint a database
9026286510Speter** METHOD: sqlite3
9027251883Speter**
9028282328Sbapt** ^(The sqlite3_wal_checkpoint(D,X) is equivalent to
9029282328Sbapt** [sqlite3_wal_checkpoint_v2](D,X,[SQLITE_CHECKPOINT_PASSIVE],0,0).)^
9030251883Speter**
9031366076Scy** In brief, sqlite3_wal_checkpoint(D,X) causes the content in the
9032282328Sbapt** [write-ahead log] for database X on [database connection] D to be
9033282328Sbapt** transferred into the database file and for the write-ahead log to
9034282328Sbapt** be reset.  See the [checkpointing] documentation for addition
9035282328Sbapt** information.
9036251883Speter**
9037282328Sbapt** This interface used to be the only way to cause a checkpoint to
9038282328Sbapt** occur.  But then the newer and more powerful [sqlite3_wal_checkpoint_v2()]
9039282328Sbapt** interface was added.  This interface is retained for backwards
9040282328Sbapt** compatibility and as a convenience for applications that need to manually
9041282328Sbapt** start a callback but which do not need the full power (and corresponding
9042282328Sbapt** complication) of [sqlite3_wal_checkpoint_v2()].
9043251883Speter*/
9044322444SpeterSQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
9045251883Speter
9046251883Speter/*
9047251883Speter** CAPI3REF: Checkpoint a database
9048286510Speter** METHOD: sqlite3
9049251883Speter**
9050282328Sbapt** ^(The sqlite3_wal_checkpoint_v2(D,X,M,L,C) interface runs a checkpoint
9051282328Sbapt** operation on database X of [database connection] D in mode M.  Status
9052282328Sbapt** information is written back into integers pointed to by L and C.)^
9053282328Sbapt** ^(The M parameter must be a valid [checkpoint mode]:)^
9054251883Speter**
9055251883Speter** <dl>
9056251883Speter** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
9057366076Scy**   ^Checkpoint as many frames as possible without waiting for any database
9058366076Scy**   readers or writers to finish, then sync the database file if all frames
9059282328Sbapt**   in the log were checkpointed. ^The [busy-handler callback]
9060366076Scy**   is never invoked in the SQLITE_CHECKPOINT_PASSIVE mode.
9061282328Sbapt**   ^On the other hand, passive mode might leave the checkpoint unfinished
9062282328Sbapt**   if there are concurrent readers or writers.
9063251883Speter**
9064251883Speter** <dt>SQLITE_CHECKPOINT_FULL<dd>
9065282328Sbapt**   ^This mode blocks (it invokes the
9066274884Sbapt**   [sqlite3_busy_handler|busy-handler callback]) until there is no
9067251883Speter**   database writer and all readers are reading from the most recent database
9068282328Sbapt**   snapshot. ^It then checkpoints all frames in the log file and syncs the
9069282328Sbapt**   database file. ^This mode blocks new database writers while it is pending,
9070282328Sbapt**   but new database readers are allowed to continue unimpeded.
9071251883Speter**
9072251883Speter** <dt>SQLITE_CHECKPOINT_RESTART<dd>
9073282328Sbapt**   ^This mode works the same way as SQLITE_CHECKPOINT_FULL with the addition
9074366076Scy**   that after checkpointing the log file it blocks (calls the
9075282328Sbapt**   [busy-handler callback])
9076366076Scy**   until all readers are reading from the database file only. ^This ensures
9077282328Sbapt**   that the next writer will restart the log file from the beginning.
9078282328Sbapt**   ^Like SQLITE_CHECKPOINT_FULL, this mode blocks new
9079282328Sbapt**   database writer attempts while it is pending, but does not impede readers.
9080282328Sbapt**
9081282328Sbapt** <dt>SQLITE_CHECKPOINT_TRUNCATE<dd>
9082282328Sbapt**   ^This mode works the same way as SQLITE_CHECKPOINT_RESTART with the
9083282328Sbapt**   addition that it also truncates the log file to zero bytes just prior
9084282328Sbapt**   to a successful return.
9085251883Speter** </dl>
9086251883Speter**
9087282328Sbapt** ^If pnLog is not NULL, then *pnLog is set to the total number of frames in
9088282328Sbapt** the log file or to -1 if the checkpoint could not run because
9089282328Sbapt** of an error or because the database is not in [WAL mode]. ^If pnCkpt is not
9090282328Sbapt** NULL,then *pnCkpt is set to the total number of checkpointed frames in the
9091282328Sbapt** log file (including any that were already checkpointed before the function
9092282328Sbapt** was called) or to -1 if the checkpoint could not run due to an error or
9093282328Sbapt** because the database is not in WAL mode. ^Note that upon successful
9094282328Sbapt** completion of an SQLITE_CHECKPOINT_TRUNCATE, the log file will have been
9095282328Sbapt** truncated to zero bytes and so both *pnLog and *pnCkpt will be set to zero.
9096251883Speter**
9097282328Sbapt** ^All calls obtain an exclusive "checkpoint" lock on the database file. ^If
9098366076Scy** any other process is running a checkpoint operation at the same time, the
9099366076Scy** lock cannot be obtained and SQLITE_BUSY is returned. ^Even if there is a
9100251883Speter** busy-handler configured, it will not be invoked in this case.
9101251883Speter**
9102366076Scy** ^The SQLITE_CHECKPOINT_FULL, RESTART and TRUNCATE modes also obtain the
9103282328Sbapt** exclusive "writer" lock on the database file. ^If the writer lock cannot be
9104282328Sbapt** obtained immediately, and a busy-handler is configured, it is invoked and
9105282328Sbapt** the writer lock retried until either the busy-handler returns 0 or the lock
9106282328Sbapt** is successfully obtained. ^The busy-handler is also invoked while waiting for
9107282328Sbapt** database readers as described above. ^If the busy-handler returns 0 before
9108251883Speter** the writer lock is obtained or while waiting for database readers, the
9109366076Scy** checkpoint operation proceeds from that point in the same way as
9110366076Scy** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
9111282328Sbapt** without blocking any further. ^SQLITE_BUSY is returned in this case.
9112251883Speter**
9113282328Sbapt** ^If parameter zDb is NULL or points to a zero length string, then the
9114366076Scy** specified operation is attempted on all WAL databases [attached] to
9115282328Sbapt** [database connection] db.  In this case the
9116366076Scy** values written to output parameters *pnLog and *pnCkpt are undefined. ^If
9117366076Scy** an SQLITE_BUSY error is encountered when processing one or more of the
9118366076Scy** attached WAL databases, the operation is still attempted on any remaining
9119366076Scy** attached databases and SQLITE_BUSY is returned at the end. ^If any other
9120366076Scy** error occurs while processing an attached database, processing is abandoned
9121366076Scy** and the error code is returned to the caller immediately. ^If no error
9122366076Scy** (SQLITE_BUSY or otherwise) is encountered while processing the attached
9123251883Speter** databases, SQLITE_OK is returned.
9124251883Speter**
9125282328Sbapt** ^If database zDb is the name of an attached database that is not in WAL
9126282328Sbapt** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. ^If
9127251883Speter** zDb is not NULL (or a zero length string) and is not the name of any
9128251883Speter** attached database, SQLITE_ERROR is returned to the caller.
9129282328Sbapt**
9130282328Sbapt** ^Unless it returns SQLITE_MISUSE,
9131282328Sbapt** the sqlite3_wal_checkpoint_v2() interface
9132282328Sbapt** sets the error information that is queried by
9133282328Sbapt** [sqlite3_errcode()] and [sqlite3_errmsg()].
9134282328Sbapt**
9135282328Sbapt** ^The [PRAGMA wal_checkpoint] command can be used to invoke this interface
9136282328Sbapt** from SQL.
9137251883Speter*/
9138322444SpeterSQLITE_API int sqlite3_wal_checkpoint_v2(
9139251883Speter  sqlite3 *db,                    /* Database handle */
9140251883Speter  const char *zDb,                /* Name of attached database (or NULL) */
9141251883Speter  int eMode,                      /* SQLITE_CHECKPOINT_* value */
9142251883Speter  int *pnLog,                     /* OUT: Size of WAL log in frames */
9143251883Speter  int *pnCkpt                     /* OUT: Total number of frames checkpointed */
9144251883Speter);
9145251883Speter
9146251883Speter/*
9147282328Sbapt** CAPI3REF: Checkpoint Mode Values
9148282328Sbapt** KEYWORDS: {checkpoint mode}
9149251883Speter**
9150282328Sbapt** These constants define all valid values for the "checkpoint mode" passed
9151282328Sbapt** as the third parameter to the [sqlite3_wal_checkpoint_v2()] interface.
9152282328Sbapt** See the [sqlite3_wal_checkpoint_v2()] documentation for details on the
9153282328Sbapt** meaning of each of these checkpoint modes.
9154251883Speter*/
9155282328Sbapt#define SQLITE_CHECKPOINT_PASSIVE  0  /* Do as much as possible w/o blocking */
9156282328Sbapt#define SQLITE_CHECKPOINT_FULL     1  /* Wait for writers, then checkpoint */
9157282328Sbapt#define SQLITE_CHECKPOINT_RESTART  2  /* Like FULL but wait for for readers */
9158282328Sbapt#define SQLITE_CHECKPOINT_TRUNCATE 3  /* Like RESTART but also truncate WAL */
9159251883Speter
9160251883Speter/*
9161251883Speter** CAPI3REF: Virtual Table Interface Configuration
9162251883Speter**
9163251883Speter** This function may be called by either the [xConnect] or [xCreate] method
9164251883Speter** of a [virtual table] implementation to configure
9165251883Speter** various facets of the virtual table interface.
9166251883Speter**
9167251883Speter** If this interface is invoked outside the context of an xConnect or
9168251883Speter** xCreate virtual table method then the behavior is undefined.
9169251883Speter**
9170361456Scy** In the call sqlite3_vtab_config(D,C,...) the D parameter is the
9171361456Scy** [database connection] in which the virtual table is being created and
9172361456Scy** which is passed in as the first argument to the [xConnect] or [xCreate]
9173361456Scy** method that is invoking sqlite3_vtab_config().  The C parameter is one
9174361456Scy** of the [virtual table configuration options].  The presence and meaning
9175361456Scy** of parameters after C depend on which [virtual table configuration option]
9176361456Scy** is used.
9177251883Speter*/
9178322444SpeterSQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
9179251883Speter
9180251883Speter/*
9181251883Speter** CAPI3REF: Virtual Table Configuration Options
9182366076Scy** KEYWORDS: {virtual table configuration options}
9183361456Scy** KEYWORDS: {virtual table configuration option}
9184251883Speter**
9185251883Speter** These macros define the various options to the
9186251883Speter** [sqlite3_vtab_config()] interface that [virtual table] implementations
9187251883Speter** can use to customize and optimize their behavior.
9188251883Speter**
9189251883Speter** <dl>
9190342292Scy** [[SQLITE_VTAB_CONSTRAINT_SUPPORT]]
9191361456Scy** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT</dt>
9192251883Speter** <dd>Calls of the form
9193251883Speter** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
9194251883Speter** where X is an integer.  If X is zero, then the [virtual table] whose
9195251883Speter** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
9196251883Speter** support constraints.  In this configuration (which is the default) if
9197251883Speter** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
9198251883Speter** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
9199251883Speter** specified as part of the users SQL statement, regardless of the actual
9200251883Speter** ON CONFLICT mode specified.
9201251883Speter**
9202251883Speter** If X is non-zero, then the virtual table implementation guarantees
9203251883Speter** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
9204251883Speter** any modifications to internal or persistent data structures have been made.
9205366076Scy** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
9206251883Speter** is able to roll back a statement or database transaction, and abandon
9207366076Scy** or continue processing the current SQL statement as appropriate.
9208251883Speter** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
9209251883Speter** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
9210251883Speter** had been ABORT.
9211251883Speter**
9212251883Speter** Virtual table implementations that are required to handle OR REPLACE
9213366076Scy** must do so within the [xUpdate] method. If a call to the
9214366076Scy** [sqlite3_vtab_on_conflict()] function indicates that the current ON
9215366076Scy** CONFLICT policy is REPLACE, the virtual table implementation should
9216251883Speter** silently replace the appropriate rows within the xUpdate callback and
9217251883Speter** return SQLITE_OK. Or, if this is not possible, it may return
9218366076Scy** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
9219251883Speter** constraint handling.
9220361456Scy** </dd>
9221361456Scy**
9222361456Scy** [[SQLITE_VTAB_DIRECTONLY]]<dt>SQLITE_VTAB_DIRECTONLY</dt>
9223361456Scy** <dd>Calls of the form
9224361456Scy** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the
9225361456Scy** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
9226361456Scy** prohibits that virtual table from being used from within triggers and
9227361456Scy** views.
9228361456Scy** </dd>
9229361456Scy**
9230361456Scy** [[SQLITE_VTAB_INNOCUOUS]]<dt>SQLITE_VTAB_INNOCUOUS</dt>
9231361456Scy** <dd>Calls of the form
9232361456Scy** [sqlite3_vtab_config](db,SQLITE_VTAB_INNOCUOUS) from within the
9233361456Scy** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
9234361456Scy** identify that virtual table as being safe to use from within triggers
9235361456Scy** and views.  Conceptually, the SQLITE_VTAB_INNOCUOUS tag means that the
9236361456Scy** virtual table can do no serious harm even if it is controlled by a
9237361456Scy** malicious hacker.  Developers should avoid setting the SQLITE_VTAB_INNOCUOUS
9238361456Scy** flag unless absolutely necessary.
9239361456Scy** </dd>
9240251883Speter** </dl>
9241251883Speter*/
9242251883Speter#define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
9243361456Scy#define SQLITE_VTAB_INNOCUOUS          2
9244361456Scy#define SQLITE_VTAB_DIRECTONLY         3
9245251883Speter
9246251883Speter/*
9247251883Speter** CAPI3REF: Determine The Virtual Table Conflict Policy
9248251883Speter**
9249251883Speter** This function may only be called from within a call to the [xUpdate] method
9250251883Speter** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
9251251883Speter** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
9252251883Speter** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
9253251883Speter** of the SQL statement that triggered the call to the [xUpdate] method of the
9254251883Speter** [virtual table].
9255251883Speter*/
9256322444SpeterSQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
9257251883Speter
9258251883Speter/*
9259342292Scy** CAPI3REF: Determine If Virtual Table Column Access Is For UPDATE
9260342292Scy**
9261342292Scy** If the sqlite3_vtab_nochange(X) routine is called within the [xColumn]
9262369950Scy** method of a [virtual table], then it might return true if the
9263342292Scy** column is being fetched as part of an UPDATE operation during which the
9264369950Scy** column value will not change.  The virtual table implementation can use
9265369950Scy** this hint as permission to substitute a return value that is less
9266369950Scy** expensive to compute and that the corresponding
9267342292Scy** [xUpdate] method understands as a "no-change" value.
9268342292Scy**
9269342292Scy** If the [xColumn] method calls sqlite3_vtab_nochange() and finds that
9270342292Scy** the column is not changed by the UPDATE statement, then the xColumn
9271342292Scy** method can optionally return without setting a result, without calling
9272342292Scy** any of the [sqlite3_result_int|sqlite3_result_xxxxx() interfaces].
9273342292Scy** In that case, [sqlite3_value_nochange(X)] will return true for the
9274342292Scy** same column in the [xUpdate] method.
9275369950Scy**
9276369950Scy** The sqlite3_vtab_nochange() routine is an optimization.  Virtual table
9277369950Scy** implementations should continue to give a correct answer even if the
9278369950Scy** sqlite3_vtab_nochange() interface were to always return false.  In the
9279369950Scy** current implementation, the sqlite3_vtab_nochange() interface does always
9280369950Scy** returns false for the enhanced [UPDATE FROM] statement.
9281342292Scy*/
9282342292ScySQLITE_API int sqlite3_vtab_nochange(sqlite3_context*);
9283342292Scy
9284342292Scy/*
9285342292Scy** CAPI3REF: Determine The Collation For a Virtual Table Constraint
9286342292Scy**
9287342292Scy** This function may only be called from within a call to the [xBestIndex]
9288366076Scy** method of a [virtual table].
9289342292Scy**
9290342292Scy** The first argument must be the sqlite3_index_info object that is the
9291342292Scy** first parameter to the xBestIndex() method. The second argument must be
9292342292Scy** an index into the aConstraint[] array belonging to the sqlite3_index_info
9293366076Scy** structure passed to xBestIndex. This function returns a pointer to a buffer
9294342292Scy** containing the name of the collation sequence for the corresponding
9295342292Scy** constraint.
9296342292Scy*/
9297342292ScySQLITE_API SQLITE_EXPERIMENTAL const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
9298342292Scy
9299342292Scy/*
9300251883Speter** CAPI3REF: Conflict resolution modes
9301274884Sbapt** KEYWORDS: {conflict resolution mode}
9302251883Speter**
9303251883Speter** These constants are returned by [sqlite3_vtab_on_conflict()] to
9304251883Speter** inform a [virtual table] implementation what the [ON CONFLICT] mode
9305251883Speter** is for the SQL statement being evaluated.
9306251883Speter**
9307251883Speter** Note that the [SQLITE_IGNORE] constant is also used as a potential
9308251883Speter** return value from the [sqlite3_set_authorizer()] callback and that
9309251883Speter** [SQLITE_ABORT] is also a [result code].
9310251883Speter*/
9311251883Speter#define SQLITE_ROLLBACK 1
9312251883Speter/* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
9313251883Speter#define SQLITE_FAIL     3
9314251883Speter/* #define SQLITE_ABORT 4  // Also an error code */
9315251883Speter#define SQLITE_REPLACE  5
9316251883Speter
9317282328Sbapt/*
9318282328Sbapt** CAPI3REF: Prepared Statement Scan Status Opcodes
9319282328Sbapt** KEYWORDS: {scanstatus options}
9320282328Sbapt**
9321282328Sbapt** The following constants can be used for the T parameter to the
9322282328Sbapt** [sqlite3_stmt_scanstatus(S,X,T,V)] interface.  Each constant designates a
9323282328Sbapt** different metric for sqlite3_stmt_scanstatus() to return.
9324282328Sbapt**
9325282328Sbapt** When the value returned to V is a string, space to hold that string is
9326282328Sbapt** managed by the prepared statement S and will be automatically freed when
9327282328Sbapt** S is finalized.
9328282328Sbapt**
9329282328Sbapt** <dl>
9330282328Sbapt** [[SQLITE_SCANSTAT_NLOOP]] <dt>SQLITE_SCANSTAT_NLOOP</dt>
9331361456Scy** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be
9332282328Sbapt** set to the total number of times that the X-th loop has run.</dd>
9333282328Sbapt**
9334282328Sbapt** [[SQLITE_SCANSTAT_NVISIT]] <dt>SQLITE_SCANSTAT_NVISIT</dt>
9335361456Scy** <dd>^The [sqlite3_int64] variable pointed to by the V parameter will be set
9336282328Sbapt** to the total number of rows examined by all iterations of the X-th loop.</dd>
9337282328Sbapt**
9338282328Sbapt** [[SQLITE_SCANSTAT_EST]] <dt>SQLITE_SCANSTAT_EST</dt>
9339361456Scy** <dd>^The "double" variable pointed to by the V parameter will be set to the
9340282328Sbapt** query planner's estimate for the average number of rows output from each
9341282328Sbapt** iteration of the X-th loop.  If the query planner's estimates was accurate,
9342282328Sbapt** then this value will approximate the quotient NVISIT/NLOOP and the
9343282328Sbapt** product of this value for all prior loops with the same SELECTID will
9344282328Sbapt** be the NLOOP value for the current loop.
9345282328Sbapt**
9346282328Sbapt** [[SQLITE_SCANSTAT_NAME]] <dt>SQLITE_SCANSTAT_NAME</dt>
9347361456Scy** <dd>^The "const char *" variable pointed to by the V parameter will be set
9348282328Sbapt** to a zero-terminated UTF-8 string containing the name of the index or table
9349282328Sbapt** used for the X-th loop.
9350282328Sbapt**
9351282328Sbapt** [[SQLITE_SCANSTAT_EXPLAIN]] <dt>SQLITE_SCANSTAT_EXPLAIN</dt>
9352361456Scy** <dd>^The "const char *" variable pointed to by the V parameter will be set
9353282328Sbapt** to a zero-terminated UTF-8 string containing the [EXPLAIN QUERY PLAN]
9354282328Sbapt** description for the X-th loop.
9355282328Sbapt**
9356282328Sbapt** [[SQLITE_SCANSTAT_SELECTID]] <dt>SQLITE_SCANSTAT_SELECT</dt>
9357361456Scy** <dd>^The "int" variable pointed to by the V parameter will be set to the
9358282328Sbapt** "select-id" for the X-th loop.  The select-id identifies which query or
9359282328Sbapt** subquery the loop is part of.  The main query has a select-id of zero.
9360282328Sbapt** The select-id is the same value as is output in the first column
9361282328Sbapt** of an [EXPLAIN QUERY PLAN] query.
9362282328Sbapt** </dl>
9363282328Sbapt*/
9364282328Sbapt#define SQLITE_SCANSTAT_NLOOP    0
9365282328Sbapt#define SQLITE_SCANSTAT_NVISIT   1
9366282328Sbapt#define SQLITE_SCANSTAT_EST      2
9367282328Sbapt#define SQLITE_SCANSTAT_NAME     3
9368282328Sbapt#define SQLITE_SCANSTAT_EXPLAIN  4
9369282328Sbapt#define SQLITE_SCANSTAT_SELECTID 5
9370251883Speter
9371282328Sbapt/*
9372282328Sbapt** CAPI3REF: Prepared Statement Scan Status
9373286510Speter** METHOD: sqlite3_stmt
9374282328Sbapt**
9375282328Sbapt** This interface returns information about the predicted and measured
9376282328Sbapt** performance for pStmt.  Advanced applications can use this
9377282328Sbapt** interface to compare the predicted and the measured performance and
9378282328Sbapt** issue warnings and/or rerun [ANALYZE] if discrepancies are found.
9379282328Sbapt**
9380282328Sbapt** Since this interface is expected to be rarely used, it is only
9381282328Sbapt** available if SQLite is compiled using the [SQLITE_ENABLE_STMT_SCANSTATUS]
9382282328Sbapt** compile-time option.
9383282328Sbapt**
9384282328Sbapt** The "iScanStatusOp" parameter determines which status information to return.
9385282328Sbapt** The "iScanStatusOp" must be one of the [scanstatus options] or the behavior
9386282328Sbapt** of this interface is undefined.
9387282328Sbapt** ^The requested measurement is written into a variable pointed to by
9388282328Sbapt** the "pOut" parameter.
9389282328Sbapt** Parameter "idx" identifies the specific loop to retrieve statistics for.
9390282328Sbapt** Loops are numbered starting from zero. ^If idx is out of range - less than
9391282328Sbapt** zero or greater than or equal to the total number of loops used to implement
9392282328Sbapt** the statement - a non-zero value is returned and the variable that pOut
9393282328Sbapt** points to is unchanged.
9394282328Sbapt**
9395282328Sbapt** ^Statistics might not be available for all loops in all statements. ^In cases
9396282328Sbapt** where there exist loops with no available statistics, this function behaves
9397282328Sbapt** as if the loop did not exist - it returns non-zero and leave the variable
9398282328Sbapt** that pOut points to unchanged.
9399282328Sbapt**
9400282328Sbapt** See also: [sqlite3_stmt_scanstatus_reset()]
9401282328Sbapt*/
9402322444SpeterSQLITE_API int sqlite3_stmt_scanstatus(
9403282328Sbapt  sqlite3_stmt *pStmt,      /* Prepared statement for which info desired */
9404282328Sbapt  int idx,                  /* Index of loop to report on */
9405282328Sbapt  int iScanStatusOp,        /* Information desired.  SQLITE_SCANSTAT_* */
9406282328Sbapt  void *pOut                /* Result written here */
9407366076Scy);
9408251883Speter
9409251883Speter/*
9410282328Sbapt** CAPI3REF: Zero Scan-Status Counters
9411286510Speter** METHOD: sqlite3_stmt
9412282328Sbapt**
9413282328Sbapt** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
9414282328Sbapt**
9415282328Sbapt** This API is only available if the library is built with pre-processor
9416282328Sbapt** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
9417282328Sbapt*/
9418322444SpeterSQLITE_API void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
9419282328Sbapt
9420298161Sbapt/*
9421298161Sbapt** CAPI3REF: Flush caches to disk mid-transaction
9422369950Scy** METHOD: sqlite3
9423298161Sbapt**
9424298161Sbapt** ^If a write-transaction is open on [database connection] D when the
9425298161Sbapt** [sqlite3_db_cacheflush(D)] interface invoked, any dirty
9426366076Scy** pages in the pager-cache that are not currently in use are written out
9427298161Sbapt** to disk. A dirty page may be in use if a database cursor created by an
9428298161Sbapt** active SQL statement is reading from it, or if it is page 1 of a database
9429298161Sbapt** file (page 1 is always "in use").  ^The [sqlite3_db_cacheflush(D)]
9430298161Sbapt** interface flushes caches for all schemas - "main", "temp", and
9431298161Sbapt** any [attached] databases.
9432298161Sbapt**
9433366076Scy** ^If this function needs to obtain extra database locks before dirty pages
9434366076Scy** can be flushed to disk, it does so. ^If those locks cannot be obtained
9435298161Sbapt** immediately and there is a busy-handler callback configured, it is invoked
9436298161Sbapt** in the usual manner. ^If the required lock still cannot be obtained, then
9437298161Sbapt** the database is skipped and an attempt made to flush any dirty pages
9438298161Sbapt** belonging to the next (if any) database. ^If any databases are skipped
9439298161Sbapt** because locks cannot be obtained, but no other error occurs, this
9440298161Sbapt** function returns SQLITE_BUSY.
9441298161Sbapt**
9442298161Sbapt** ^If any other error occurs while flushing dirty pages to disk (for
9443298161Sbapt** example an IO error or out-of-memory condition), then processing is
9444298161Sbapt** abandoned and an SQLite [error code] is returned to the caller immediately.
9445298161Sbapt**
9446298161Sbapt** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
9447298161Sbapt**
9448298161Sbapt** ^This function does not set the database handle error code or message
9449298161Sbapt** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
9450298161Sbapt*/
9451322444SpeterSQLITE_API int sqlite3_db_cacheflush(sqlite3*);
9452282328Sbapt
9453282328Sbapt/*
9454305002Scy** CAPI3REF: The pre-update hook.
9455369950Scy** METHOD: sqlite3
9456305002Scy**
9457305002Scy** ^These interfaces are only available if SQLite is compiled using the
9458305002Scy** [SQLITE_ENABLE_PREUPDATE_HOOK] compile-time option.
9459305002Scy**
9460305002Scy** ^The [sqlite3_preupdate_hook()] interface registers a callback function
9461305002Scy** that is invoked prior to each [INSERT], [UPDATE], and [DELETE] operation
9462322444Speter** on a database table.
9463305002Scy** ^At most one preupdate hook may be registered at a time on a single
9464305002Scy** [database connection]; each call to [sqlite3_preupdate_hook()] overrides
9465305002Scy** the previous setting.
9466305002Scy** ^The preupdate hook is disabled by invoking [sqlite3_preupdate_hook()]
9467305002Scy** with a NULL pointer as the second parameter.
9468305002Scy** ^The third parameter to [sqlite3_preupdate_hook()] is passed through as
9469305002Scy** the first parameter to callbacks.
9470305002Scy**
9471322444Speter** ^The preupdate hook only fires for changes to real database tables; the
9472322444Speter** preupdate hook is not invoked for changes to [virtual tables] or to
9473366076Scy** system tables like sqlite_sequence or sqlite_stat1.
9474305002Scy**
9475305002Scy** ^The second parameter to the preupdate callback is a pointer to
9476305002Scy** the [database connection] that registered the preupdate hook.
9477305002Scy** ^The third parameter to the preupdate callback is one of the constants
9478305002Scy** [SQLITE_INSERT], [SQLITE_DELETE], or [SQLITE_UPDATE] to identify the
9479305002Scy** kind of update operation that is about to occur.
9480305002Scy** ^(The fourth parameter to the preupdate callback is the name of the
9481305002Scy** database within the database connection that is being modified.  This
9482366076Scy** will be "main" for the main database or "temp" for TEMP tables or
9483305002Scy** the name given after the AS keyword in the [ATTACH] statement for attached
9484305002Scy** databases.)^
9485305002Scy** ^The fifth parameter to the preupdate callback is the name of the
9486305002Scy** table that is being modified.
9487305002Scy**
9488322444Speter** For an UPDATE or DELETE operation on a [rowid table], the sixth
9489366076Scy** parameter passed to the preupdate callback is the initial [rowid] of the
9490322444Speter** row being modified or deleted. For an INSERT operation on a rowid table,
9491366076Scy** or any operation on a WITHOUT ROWID table, the value of the sixth
9492322444Speter** parameter is undefined. For an INSERT or UPDATE on a rowid table the
9493322444Speter** seventh parameter is the final rowid value of the row being inserted
9494322444Speter** or updated. The value of the seventh parameter passed to the callback
9495322444Speter** function is not defined for operations on WITHOUT ROWID tables, or for
9496369950Scy** DELETE operations on rowid tables.
9497322444Speter**
9498305002Scy** The [sqlite3_preupdate_old()], [sqlite3_preupdate_new()],
9499305002Scy** [sqlite3_preupdate_count()], and [sqlite3_preupdate_depth()] interfaces
9500305002Scy** provide additional information about a preupdate event. These routines
9501305002Scy** may only be called from within a preupdate callback.  Invoking any of
9502305002Scy** these routines from outside of a preupdate callback or with a
9503305002Scy** [database connection] pointer that is different from the one supplied
9504305002Scy** to the preupdate callback results in undefined and probably undesirable
9505305002Scy** behavior.
9506305002Scy**
9507305002Scy** ^The [sqlite3_preupdate_count(D)] interface returns the number of columns
9508305002Scy** in the row that is being inserted, updated, or deleted.
9509305002Scy**
9510305002Scy** ^The [sqlite3_preupdate_old(D,N,P)] interface writes into P a pointer to
9511305002Scy** a [protected sqlite3_value] that contains the value of the Nth column of
9512305002Scy** the table row before it is updated.  The N parameter must be between 0
9513305002Scy** and one less than the number of columns or the behavior will be
9514305002Scy** undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE
9515305002Scy** preupdate callbacks; if it is used by an SQLITE_INSERT callback then the
9516305002Scy** behavior is undefined.  The [sqlite3_value] that P points to
9517305002Scy** will be destroyed when the preupdate callback returns.
9518305002Scy**
9519305002Scy** ^The [sqlite3_preupdate_new(D,N,P)] interface writes into P a pointer to
9520305002Scy** a [protected sqlite3_value] that contains the value of the Nth column of
9521305002Scy** the table row after it is updated.  The N parameter must be between 0
9522305002Scy** and one less than the number of columns or the behavior will be
9523305002Scy** undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE
9524305002Scy** preupdate callbacks; if it is used by an SQLITE_DELETE callback then the
9525305002Scy** behavior is undefined.  The [sqlite3_value] that P points to
9526305002Scy** will be destroyed when the preupdate callback returns.
9527305002Scy**
9528305002Scy** ^The [sqlite3_preupdate_depth(D)] interface returns 0 if the preupdate
9529305002Scy** callback was invoked as a result of a direct insert, update, or delete
9530366076Scy** operation; or 1 for inserts, updates, or deletes invoked by top-level
9531305002Scy** triggers; or 2 for changes resulting from triggers called by top-level
9532305002Scy** triggers; and so forth.
9533305002Scy**
9534305002Scy** See also:  [sqlite3_update_hook()]
9535305002Scy*/
9536322444Speter#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
9537322444SpeterSQLITE_API void *sqlite3_preupdate_hook(
9538305002Scy  sqlite3 *db,
9539305002Scy  void(*xPreUpdate)(
9540305002Scy    void *pCtx,                   /* Copy of third arg to preupdate_hook() */
9541305002Scy    sqlite3 *db,                  /* Database handle */
9542305002Scy    int op,                       /* SQLITE_UPDATE, DELETE or INSERT */
9543305002Scy    char const *zDb,              /* Database name */
9544305002Scy    char const *zName,            /* Table name */
9545305002Scy    sqlite3_int64 iKey1,          /* Rowid of row about to be deleted/updated */
9546305002Scy    sqlite3_int64 iKey2           /* New rowid value (for a rowid UPDATE) */
9547305002Scy  ),
9548305002Scy  void*
9549305002Scy);
9550322444SpeterSQLITE_API int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
9551322444SpeterSQLITE_API int sqlite3_preupdate_count(sqlite3 *);
9552322444SpeterSQLITE_API int sqlite3_preupdate_depth(sqlite3 *);
9553322444SpeterSQLITE_API int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
9554322444Speter#endif
9555305002Scy
9556305002Scy/*
9557298161Sbapt** CAPI3REF: Low-level system error code
9558369950Scy** METHOD: sqlite3
9559298161Sbapt**
9560298161Sbapt** ^Attempt to return the underlying operating system error code or error
9561305002Scy** number that caused the most recent I/O error or failure to open a file.
9562298161Sbapt** The return value is OS-dependent.  For example, on unix systems, after
9563298161Sbapt** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
9564298161Sbapt** called to get back the underlying "errno" that caused the problem, such
9565366076Scy** as ENOSPC, EAUTH, EISDIR, and so forth.
9566298161Sbapt*/
9567322444SpeterSQLITE_API int sqlite3_system_errno(sqlite3*);
9568298161Sbapt
9569298161Sbapt/*
9570298161Sbapt** CAPI3REF: Database Snapshot
9571322444Speter** KEYWORDS: {snapshot} {sqlite3_snapshot}
9572298161Sbapt**
9573298161Sbapt** An instance of the snapshot object records the state of a [WAL mode]
9574298161Sbapt** database for some specific point in history.
9575298161Sbapt**
9576298161Sbapt** In [WAL mode], multiple [database connections] that are open on the
9577298161Sbapt** same database file can each be reading a different historical version
9578298161Sbapt** of the database file.  When a [database connection] begins a read
9579298161Sbapt** transaction, that connection sees an unchanging copy of the database
9580298161Sbapt** as it existed for the point in time when the transaction first started.
9581298161Sbapt** Subsequent changes to the database from other connections are not seen
9582298161Sbapt** by the reader until a new read transaction is started.
9583298161Sbapt**
9584298161Sbapt** The sqlite3_snapshot object records state information about an historical
9585298161Sbapt** version of the database file so that it is possible to later open a new read
9586298161Sbapt** transaction that sees that historical version of the database rather than
9587298161Sbapt** the most recent version.
9588298161Sbapt*/
9589322444Spetertypedef struct sqlite3_snapshot {
9590322444Speter  unsigned char hidden[48];
9591322444Speter} sqlite3_snapshot;
9592298161Sbapt
9593298161Sbapt/*
9594298161Sbapt** CAPI3REF: Record A Database Snapshot
9595342292Scy** CONSTRUCTOR: sqlite3_snapshot
9596298161Sbapt**
9597298161Sbapt** ^The [sqlite3_snapshot_get(D,S,P)] interface attempts to make a
9598298161Sbapt** new [sqlite3_snapshot] object that records the current state of
9599298161Sbapt** schema S in database connection D.  ^On success, the
9600298161Sbapt** [sqlite3_snapshot_get(D,S,P)] interface writes a pointer to the newly
9601298161Sbapt** created [sqlite3_snapshot] object into *P and returns SQLITE_OK.
9602322444Speter** If there is not already a read-transaction open on schema S when
9603366076Scy** this function is called, one is opened automatically.
9604298161Sbapt**
9605322444Speter** The following must be true for this function to succeed. If any of
9606322444Speter** the following statements are false when sqlite3_snapshot_get() is
9607322444Speter** called, SQLITE_ERROR is returned. The final value of *P is undefined
9608366076Scy** in this case.
9609322444Speter**
9610322444Speter** <ul>
9611342292Scy**   <li> The database handle must not be in [autocommit mode].
9612322444Speter**
9613322444Speter**   <li> Schema S of [database connection] D must be a [WAL mode] database.
9614322444Speter**
9615322444Speter**   <li> There must not be a write transaction open on schema S of database
9616322444Speter**        connection D.
9617322444Speter**
9618322444Speter**   <li> One or more transactions must have been written to the current wal
9619322444Speter**        file since it was created on disk (by any connection). This means
9620366076Scy**        that a snapshot cannot be taken on a wal mode database with no wal
9621322444Speter**        file immediately after it is first opened. At least one transaction
9622322444Speter**        must be written to it first.
9623322444Speter** </ul>
9624322444Speter**
9625322444Speter** This function may also return SQLITE_NOMEM.  If it is called with the
9626366076Scy** database handle in autocommit mode but fails for some other reason,
9627322444Speter** whether or not a read transaction is opened on schema S is undefined.
9628322444Speter**
9629298161Sbapt** The [sqlite3_snapshot] object returned from a successful call to
9630298161Sbapt** [sqlite3_snapshot_get()] must be freed using [sqlite3_snapshot_free()]
9631298161Sbapt** to avoid a memory leak.
9632298161Sbapt**
9633298161Sbapt** The [sqlite3_snapshot_get()] interface is only available when the
9634342292Scy** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
9635298161Sbapt*/
9636322444SpeterSQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_get(
9637298161Sbapt  sqlite3 *db,
9638298161Sbapt  const char *zSchema,
9639298161Sbapt  sqlite3_snapshot **ppSnapshot
9640298161Sbapt);
9641298161Sbapt
9642298161Sbapt/*
9643298161Sbapt** CAPI3REF: Start a read transaction on an historical snapshot
9644342292Scy** METHOD: sqlite3_snapshot
9645298161Sbapt**
9646366076Scy** ^The [sqlite3_snapshot_open(D,S,P)] interface either starts a new read
9647366076Scy** transaction or upgrades an existing one for schema S of
9648366076Scy** [database connection] D such that the read transaction refers to
9649366076Scy** historical [snapshot] P, rather than the most recent change to the
9650366076Scy** database. ^The [sqlite3_snapshot_open()] interface returns SQLITE_OK
9651342292Scy** on success or an appropriate [error code] if it fails.
9652298161Sbapt**
9653366076Scy** ^In order to succeed, the database connection must not be in
9654342292Scy** [autocommit mode] when [sqlite3_snapshot_open(D,S,P)] is called. If there
9655342292Scy** is already a read transaction open on schema S, then the database handle
9656342292Scy** must have no active statements (SELECT statements that have been passed
9657366076Scy** to sqlite3_step() but not sqlite3_reset() or sqlite3_finalize()).
9658342292Scy** SQLITE_ERROR is returned if either of these conditions is violated, or
9659342292Scy** if schema S does not exist, or if the snapshot object is invalid.
9660342292Scy**
9661342292Scy** ^A call to sqlite3_snapshot_open() will fail to open if the specified
9662366076Scy** snapshot has been overwritten by a [checkpoint]. In this case
9663342292Scy** SQLITE_ERROR_SNAPSHOT is returned.
9664342292Scy**
9665366076Scy** If there is already a read transaction open when this function is
9666342292Scy** invoked, then the same read transaction remains open (on the same
9667342292Scy** database snapshot) if SQLITE_ERROR, SQLITE_BUSY or SQLITE_ERROR_SNAPSHOT
9668342292Scy** is returned. If another error code - for example SQLITE_PROTOCOL or an
9669342292Scy** SQLITE_IOERR error code - is returned, then the final state of the
9670366076Scy** read transaction is undefined. If SQLITE_OK is returned, then the
9671342292Scy** read transaction is now open on database snapshot P.
9672342292Scy**
9673305002Scy** ^(A call to [sqlite3_snapshot_open(D,S,P)] will fail if the
9674305002Scy** database connection D does not know that the database file for
9675305002Scy** schema S is in [WAL mode].  A database connection might not know
9676305002Scy** that the database file is in [WAL mode] if there has been no prior
9677366076Scy** I/O on that database connection, or if the database entered [WAL mode]
9678305002Scy** after the most recent I/O on the database connection.)^
9679305002Scy** (Hint: Run "[PRAGMA application_id]" against a newly opened
9680298161Sbapt** database connection in order to make it ready to use snapshots.)
9681298161Sbapt**
9682298161Sbapt** The [sqlite3_snapshot_open()] interface is only available when the
9683342292Scy** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
9684298161Sbapt*/
9685322444SpeterSQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_open(
9686298161Sbapt  sqlite3 *db,
9687298161Sbapt  const char *zSchema,
9688298161Sbapt  sqlite3_snapshot *pSnapshot
9689298161Sbapt);
9690298161Sbapt
9691298161Sbapt/*
9692298161Sbapt** CAPI3REF: Destroy a snapshot
9693342292Scy** DESTRUCTOR: sqlite3_snapshot
9694298161Sbapt**
9695298161Sbapt** ^The [sqlite3_snapshot_free(P)] interface destroys [sqlite3_snapshot] P.
9696298161Sbapt** The application must eventually free every [sqlite3_snapshot] object
9697298161Sbapt** using this routine to avoid a memory leak.
9698298161Sbapt**
9699298161Sbapt** The [sqlite3_snapshot_free()] interface is only available when the
9700342292Scy** [SQLITE_ENABLE_SNAPSHOT] compile-time option is used.
9701298161Sbapt*/
9702322444SpeterSQLITE_API SQLITE_EXPERIMENTAL void sqlite3_snapshot_free(sqlite3_snapshot*);
9703298161Sbapt
9704298161Sbapt/*
9705305002Scy** CAPI3REF: Compare the ages of two snapshot handles.
9706342292Scy** METHOD: sqlite3_snapshot
9707305002Scy**
9708305002Scy** The sqlite3_snapshot_cmp(P1, P2) interface is used to compare the ages
9709366076Scy** of two valid snapshot handles.
9710305002Scy**
9711366076Scy** If the two snapshot handles are not associated with the same database
9712366076Scy** file, the result of the comparison is undefined.
9713305002Scy**
9714305002Scy** Additionally, the result of the comparison is only valid if both of the
9715305002Scy** snapshot handles were obtained by calling sqlite3_snapshot_get() since the
9716305002Scy** last time the wal file was deleted. The wal file is deleted when the
9717305002Scy** database is changed back to rollback mode or when the number of database
9718366076Scy** clients drops to zero. If either snapshot handle was obtained before the
9719366076Scy** wal file was last deleted, the value returned by this function
9720305002Scy** is undefined.
9721305002Scy**
9722305002Scy** Otherwise, this API returns a negative value if P1 refers to an older
9723305002Scy** snapshot than P2, zero if the two handles refer to the same database
9724305002Scy** snapshot, and a positive value if P1 is a newer snapshot than P2.
9725342292Scy**
9726342292Scy** This interface is only available if SQLite is compiled with the
9727342292Scy** [SQLITE_ENABLE_SNAPSHOT] option.
9728305002Scy*/
9729322444SpeterSQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_cmp(
9730305002Scy  sqlite3_snapshot *p1,
9731305002Scy  sqlite3_snapshot *p2
9732305002Scy);
9733305002Scy
9734305002Scy/*
9735322444Speter** CAPI3REF: Recover snapshots from a wal file
9736342292Scy** METHOD: sqlite3_snapshot
9737322444Speter**
9738342292Scy** If a [WAL file] remains on disk after all database connections close
9739342292Scy** (either through the use of the [SQLITE_FCNTL_PERSIST_WAL] [file control]
9740342292Scy** or because the last process to have the database opened exited without
9741342292Scy** calling [sqlite3_close()]) and a new connection is subsequently opened
9742342292Scy** on that database and [WAL file], the [sqlite3_snapshot_open()] interface
9743342292Scy** will only be able to open the last transaction added to the WAL file
9744342292Scy** even though the WAL file contains other valid transactions.
9745322444Speter**
9746342292Scy** This function attempts to scan the WAL file associated with database zDb
9747322444Speter** of database handle db and make all valid snapshots available to
9748322444Speter** sqlite3_snapshot_open(). It is an error if there is already a read
9749342292Scy** transaction open on the database, or if the database is not a WAL mode
9750322444Speter** database.
9751322444Speter**
9752322444Speter** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
9753342292Scy**
9754342292Scy** This interface is only available if SQLite is compiled with the
9755342292Scy** [SQLITE_ENABLE_SNAPSHOT] option.
9756322444Speter*/
9757322444SpeterSQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
9758322444Speter
9759322444Speter/*
9760342292Scy** CAPI3REF: Serialize a database
9761342292Scy**
9762342292Scy** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
9763342292Scy** that is a serialization of the S database on [database connection] D.
9764342292Scy** If P is not a NULL pointer, then the size of the database in bytes
9765342292Scy** is written into *P.
9766342292Scy**
9767342292Scy** For an ordinary on-disk database file, the serialization is just a
9768342292Scy** copy of the disk file.  For an in-memory database or a "TEMP" database,
9769342292Scy** the serialization is the same sequence of bytes which would be written
9770342292Scy** to disk if that database where backed up to disk.
9771342292Scy**
9772342292Scy** The usual case is that sqlite3_serialize() copies the serialization of
9773342292Scy** the database into memory obtained from [sqlite3_malloc64()] and returns
9774342292Scy** a pointer to that memory.  The caller is responsible for freeing the
9775342292Scy** returned value to avoid a memory leak.  However, if the F argument
9776342292Scy** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
9777342292Scy** are made, and the sqlite3_serialize() function will return a pointer
9778342292Scy** to the contiguous memory representation of the database that SQLite
9779342292Scy** is currently using for that database, or NULL if the no such contiguous
9780342292Scy** memory representation of the database exists.  A contiguous memory
9781342292Scy** representation of the database will usually only exist if there has
9782342292Scy** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
9783342292Scy** values of D and S.
9784366076Scy** The size of the database is written into *P even if the
9785342292Scy** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
9786342292Scy** of the database exists.
9787342292Scy**
9788342292Scy** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
9789342292Scy** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
9790342292Scy** allocation error occurs.
9791342292Scy**
9792342292Scy** This interface is only available if SQLite is compiled with the
9793342292Scy** [SQLITE_ENABLE_DESERIALIZE] option.
9794342292Scy*/
9795342292ScySQLITE_API unsigned char *sqlite3_serialize(
9796342292Scy  sqlite3 *db,           /* The database connection */
9797342292Scy  const char *zSchema,   /* Which DB to serialize. ex: "main", "temp", ... */
9798342292Scy  sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
9799342292Scy  unsigned int mFlags    /* Zero or more SQLITE_SERIALIZE_* flags */
9800342292Scy);
9801342292Scy
9802342292Scy/*
9803342292Scy** CAPI3REF: Flags for sqlite3_serialize
9804342292Scy**
9805342292Scy** Zero or more of the following constants can be OR-ed together for
9806342292Scy** the F argument to [sqlite3_serialize(D,S,P,F)].
9807342292Scy**
9808342292Scy** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
9809342292Scy** a pointer to contiguous in-memory database that it is currently using,
9810342292Scy** without making a copy of the database.  If SQLite is not currently using
9811342292Scy** a contiguous in-memory database, then this option causes
9812342292Scy** [sqlite3_serialize()] to return a NULL pointer.  SQLite will only be
9813342292Scy** using a contiguous in-memory database if it has been initialized by a
9814342292Scy** prior call to [sqlite3_deserialize()].
9815342292Scy*/
9816342292Scy#define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
9817342292Scy
9818342292Scy/*
9819342292Scy** CAPI3REF: Deserialize a database
9820342292Scy**
9821366076Scy** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the
9822342292Scy** [database connection] D to disconnect from database S and then
9823342292Scy** reopen S as an in-memory database based on the serialization contained
9824342292Scy** in P.  The serialized database P is N bytes in size.  M is the size of
9825342292Scy** the buffer P, which might be larger than N.  If M is larger than N, and
9826342292Scy** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
9827342292Scy** permitted to add content to the in-memory database as long as the total
9828342292Scy** size does not exceed M bytes.
9829342292Scy**
9830342292Scy** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
9831342292Scy** invoke sqlite3_free() on the serialization buffer when the database
9832342292Scy** connection closes.  If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
9833342292Scy** SQLite will try to increase the buffer size using sqlite3_realloc64()
9834342292Scy** if writes on the database cause it to grow larger than M bytes.
9835342292Scy**
9836342292Scy** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
9837342292Scy** database is currently in a read transaction or is involved in a backup
9838342292Scy** operation.
9839342292Scy**
9840366076Scy** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
9841342292Scy** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
9842342292Scy** [sqlite3_free()] is invoked on argument P prior to returning.
9843342292Scy**
9844342292Scy** This interface is only available if SQLite is compiled with the
9845342292Scy** [SQLITE_ENABLE_DESERIALIZE] option.
9846342292Scy*/
9847342292ScySQLITE_API int sqlite3_deserialize(
9848342292Scy  sqlite3 *db,            /* The database connection */
9849342292Scy  const char *zSchema,    /* Which DB to reopen with the deserialization */
9850342292Scy  unsigned char *pData,   /* The serialized database content */
9851342292Scy  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
9852342292Scy  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
9853342292Scy  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
9854342292Scy);
9855342292Scy
9856342292Scy/*
9857342292Scy** CAPI3REF: Flags for sqlite3_deserialize()
9858342292Scy**
9859342292Scy** The following are allowed values for 6th argument (the F argument) to
9860342292Scy** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
9861342292Scy**
9862342292Scy** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
9863342292Scy** in the P argument is held in memory obtained from [sqlite3_malloc64()]
9864342292Scy** and that SQLite should take ownership of this memory and automatically
9865342292Scy** free it when it has finished using it.  Without this flag, the caller
9866342292Scy** is responsible for freeing any dynamically allocated memory.
9867342292Scy**
9868342292Scy** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
9869342292Scy** grow the size of the database using calls to [sqlite3_realloc64()].  This
9870342292Scy** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
9871342292Scy** Without this flag, the deserialized database cannot increase in size beyond
9872342292Scy** the number of bytes specified by the M parameter.
9873342292Scy**
9874342292Scy** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
9875342292Scy** should be treated as read-only.
9876342292Scy*/
9877342292Scy#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
9878342292Scy#define SQLITE_DESERIALIZE_RESIZEABLE  2 /* Resize using sqlite3_realloc64() */
9879342292Scy#define SQLITE_DESERIALIZE_READONLY    4 /* Database is read-only */
9880342292Scy
9881342292Scy/*
9882251883Speter** Undo the hack that converts floating point types to integer for
9883251883Speter** builds on processors without floating point support.
9884251883Speter*/
9885251883Speter#ifdef SQLITE_OMIT_FLOATING_POINT
9886251883Speter# undef double
9887251883Speter#endif
9888251883Speter
9889251883Speter#ifdef __cplusplus
9890251883Speter}  /* End of the 'extern "C"' block */
9891251883Speter#endif
9892305002Scy#endif /* SQLITE3_H */
9893251883Speter
9894305002Scy/******** Begin file sqlite3rtree.h *********/
9895251883Speter/*
9896251883Speter** 2010 August 30
9897251883Speter**
9898251883Speter** The author disclaims copyright to this source code.  In place of
9899251883Speter** a legal notice, here is a blessing:
9900251883Speter**
9901251883Speter**    May you do good and not evil.
9902251883Speter**    May you find forgiveness for yourself and forgive others.
9903251883Speter**    May you share freely, never taking more than you give.
9904251883Speter**
9905251883Speter*************************************************************************
9906251883Speter*/
9907251883Speter
9908251883Speter#ifndef _SQLITE3RTREE_H_
9909251883Speter#define _SQLITE3RTREE_H_
9910251883Speter
9911251883Speter
9912251883Speter#ifdef __cplusplus
9913251883Speterextern "C" {
9914251883Speter#endif
9915251883Speter
9916251883Spetertypedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
9917269851Spetertypedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
9918251883Speter
9919269851Speter/* The double-precision datatype used by RTree depends on the
9920269851Speter** SQLITE_RTREE_INT_ONLY compile-time option.
9921269851Speter*/
9922269851Speter#ifdef SQLITE_RTREE_INT_ONLY
9923269851Speter  typedef sqlite3_int64 sqlite3_rtree_dbl;
9924269851Speter#else
9925269851Speter  typedef double sqlite3_rtree_dbl;
9926269851Speter#endif
9927269851Speter
9928251883Speter/*
9929251883Speter** Register a geometry callback named zGeom that can be used as part of an
9930251883Speter** R-Tree geometry query as follows:
9931251883Speter**
9932251883Speter**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
9933251883Speter*/
9934322444SpeterSQLITE_API int sqlite3_rtree_geometry_callback(
9935251883Speter  sqlite3 *db,
9936251883Speter  const char *zGeom,
9937269851Speter  int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
9938251883Speter  void *pContext
9939251883Speter);
9940251883Speter
9941251883Speter
9942251883Speter/*
9943251883Speter** A pointer to a structure of the following type is passed as the first
9944251883Speter** argument to callbacks registered using rtree_geometry_callback().
9945251883Speter*/
9946251883Speterstruct sqlite3_rtree_geometry {
9947251883Speter  void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
9948251883Speter  int nParam;                     /* Size of array aParam[] */
9949269851Speter  sqlite3_rtree_dbl *aParam;      /* Parameters passed to SQL geom function */
9950251883Speter  void *pUser;                    /* Callback implementation user data */
9951251883Speter  void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
9952251883Speter};
9953251883Speter
9954269851Speter/*
9955366076Scy** Register a 2nd-generation geometry callback named zScore that can be
9956269851Speter** used as part of an R-Tree geometry query as follows:
9957269851Speter**
9958269851Speter**   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
9959269851Speter*/
9960322444SpeterSQLITE_API int sqlite3_rtree_query_callback(
9961269851Speter  sqlite3 *db,
9962269851Speter  const char *zQueryFunc,
9963269851Speter  int (*xQueryFunc)(sqlite3_rtree_query_info*),
9964269851Speter  void *pContext,
9965269851Speter  void (*xDestructor)(void*)
9966269851Speter);
9967251883Speter
9968269851Speter
9969269851Speter/*
9970366076Scy** A pointer to a structure of the following type is passed as the
9971269851Speter** argument to scored geometry callback registered using
9972269851Speter** sqlite3_rtree_query_callback().
9973269851Speter**
9974269851Speter** Note that the first 5 fields of this structure are identical to
9975269851Speter** sqlite3_rtree_geometry.  This structure is a subclass of
9976269851Speter** sqlite3_rtree_geometry.
9977269851Speter*/
9978269851Speterstruct sqlite3_rtree_query_info {
9979269851Speter  void *pContext;                   /* pContext from when function registered */
9980269851Speter  int nParam;                       /* Number of function parameters */
9981269851Speter  sqlite3_rtree_dbl *aParam;        /* value of function parameters */
9982269851Speter  void *pUser;                      /* callback can use this, if desired */
9983269851Speter  void (*xDelUser)(void*);          /* function to free pUser */
9984269851Speter  sqlite3_rtree_dbl *aCoord;        /* Coordinates of node or entry to check */
9985269851Speter  unsigned int *anQueue;            /* Number of pending entries in the queue */
9986269851Speter  int nCoord;                       /* Number of coordinates */
9987269851Speter  int iLevel;                       /* Level of current node or entry */
9988269851Speter  int mxLevel;                      /* The largest iLevel value in the tree */
9989269851Speter  sqlite3_int64 iRowid;             /* Rowid for current entry */
9990269851Speter  sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
9991269851Speter  int eParentWithin;                /* Visibility of parent node */
9992342292Scy  int eWithin;                      /* OUT: Visibility */
9993269851Speter  sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
9994286510Speter  /* The following fields are only available in 3.8.11 and later */
9995286510Speter  sqlite3_value **apSqlParam;       /* Original SQL values of parameters */
9996269851Speter};
9997269851Speter
9998269851Speter/*
9999269851Speter** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
10000269851Speter*/
10001269851Speter#define NOT_WITHIN       0   /* Object completely outside of query region */
10002269851Speter#define PARTLY_WITHIN    1   /* Object partially overlaps query region */
10003269851Speter#define FULLY_WITHIN     2   /* Object fully contained within query region */
10004269851Speter
10005269851Speter
10006251883Speter#ifdef __cplusplus
10007251883Speter}  /* end of the 'extern "C"' block */
10008251883Speter#endif
10009251883Speter
10010251883Speter#endif  /* ifndef _SQLITE3RTREE_H_ */
10011251883Speter
10012305002Scy/******** End of sqlite3rtree.h *********/
10013305002Scy/******** Begin file sqlite3session.h *********/
10014305002Scy
10015305002Scy#if !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION)
10016305002Scy#define __SQLITESESSION_H_ 1
10017305002Scy
10018298161Sbapt/*
10019305002Scy** Make sure we can call this stuff from C++.
10020305002Scy*/
10021305002Scy#ifdef __cplusplus
10022305002Scyextern "C" {
10023305002Scy#endif
10024305002Scy
10025305002Scy
10026305002Scy/*
10027305002Scy** CAPI3REF: Session Object Handle
10028342292Scy**
10029342292Scy** An instance of this object is a [session] that can be used to
10030342292Scy** record changes to a database.
10031305002Scy*/
10032305002Scytypedef struct sqlite3_session sqlite3_session;
10033305002Scy
10034305002Scy/*
10035305002Scy** CAPI3REF: Changeset Iterator Handle
10036342292Scy**
10037342292Scy** An instance of this object acts as a cursor for iterating
10038342292Scy** over the elements of a [changeset] or [patchset].
10039305002Scy*/
10040305002Scytypedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
10041305002Scy
10042305002Scy/*
10043305002Scy** CAPI3REF: Create A New Session Object
10044342292Scy** CONSTRUCTOR: sqlite3_session
10045305002Scy**
10046305002Scy** Create a new session object attached to database handle db. If successful,
10047305002Scy** a pointer to the new object is written to *ppSession and SQLITE_OK is
10048305002Scy** returned. If an error occurs, *ppSession is set to NULL and an SQLite
10049305002Scy** error code (e.g. SQLITE_NOMEM) is returned.
10050305002Scy**
10051305002Scy** It is possible to create multiple session objects attached to a single
10052305002Scy** database handle.
10053305002Scy**
10054305002Scy** Session objects created using this function should be deleted using the
10055305002Scy** [sqlite3session_delete()] function before the database handle that they
10056305002Scy** are attached to is itself closed. If the database handle is closed before
10057305002Scy** the session object is deleted, then the results of calling any session
10058305002Scy** module function, including [sqlite3session_delete()] on the session object
10059305002Scy** are undefined.
10060305002Scy**
10061305002Scy** Because the session module uses the [sqlite3_preupdate_hook()] API, it
10062305002Scy** is not possible for an application to register a pre-update hook on a
10063305002Scy** database handle that has one or more session objects attached. Nor is
10064305002Scy** it possible to create a session object attached to a database handle for
10065366076Scy** which a pre-update hook is already defined. The results of attempting
10066305002Scy** either of these things are undefined.
10067305002Scy**
10068305002Scy** The session object will be used to create changesets for tables in
10069305002Scy** database zDb, where zDb is either "main", or "temp", or the name of an
10070305002Scy** attached database. It is not an error if database zDb is not attached
10071305002Scy** to the database when the session object is created.
10072305002Scy*/
10073322444SpeterSQLITE_API int sqlite3session_create(
10074305002Scy  sqlite3 *db,                    /* Database handle */
10075305002Scy  const char *zDb,                /* Name of db (e.g. "main") */
10076305002Scy  sqlite3_session **ppSession     /* OUT: New session object */
10077305002Scy);
10078305002Scy
10079305002Scy/*
10080305002Scy** CAPI3REF: Delete A Session Object
10081342292Scy** DESTRUCTOR: sqlite3_session
10082305002Scy**
10083366076Scy** Delete a session object previously allocated using
10084305002Scy** [sqlite3session_create()]. Once a session object has been deleted, the
10085305002Scy** results of attempting to use pSession with any other session module
10086305002Scy** function are undefined.
10087305002Scy**
10088305002Scy** Session objects must be deleted before the database handle to which they
10089366076Scy** are attached is closed. Refer to the documentation for
10090305002Scy** [sqlite3session_create()] for details.
10091305002Scy*/
10092322444SpeterSQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
10093305002Scy
10094305002Scy
10095305002Scy/*
10096305002Scy** CAPI3REF: Enable Or Disable A Session Object
10097342292Scy** METHOD: sqlite3_session
10098305002Scy**
10099305002Scy** Enable or disable the recording of changes by a session object. When
10100305002Scy** enabled, a session object records changes made to the database. When
10101305002Scy** disabled - it does not. A newly created session object is enabled.
10102305002Scy** Refer to the documentation for [sqlite3session_changeset()] for further
10103305002Scy** details regarding how enabling and disabling a session object affects
10104305002Scy** the eventual changesets.
10105305002Scy**
10106305002Scy** Passing zero to this function disables the session. Passing a value
10107366076Scy** greater than zero enables it. Passing a value less than zero is a
10108305002Scy** no-op, and may be used to query the current state of the session.
10109305002Scy**
10110366076Scy** The return value indicates the final state of the session object: 0 if
10111305002Scy** the session is disabled, or 1 if it is enabled.
10112305002Scy*/
10113322444SpeterSQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
10114305002Scy
10115305002Scy/*
10116305002Scy** CAPI3REF: Set Or Clear the Indirect Change Flag
10117342292Scy** METHOD: sqlite3_session
10118305002Scy**
10119305002Scy** Each change recorded by a session object is marked as either direct or
10120305002Scy** indirect. A change is marked as indirect if either:
10121305002Scy**
10122305002Scy** <ul>
10123305002Scy**   <li> The session object "indirect" flag is set when the change is
10124305002Scy**        made, or
10125366076Scy**   <li> The change is made by an SQL trigger or foreign key action
10126305002Scy**        instead of directly as a result of a users SQL statement.
10127305002Scy** </ul>
10128305002Scy**
10129305002Scy** If a single row is affected by more than one operation within a session,
10130305002Scy** then the change is considered indirect if all operations meet the criteria
10131305002Scy** for an indirect change above, or direct otherwise.
10132305002Scy**
10133305002Scy** This function is used to set, clear or query the session object indirect
10134305002Scy** flag.  If the second argument passed to this function is zero, then the
10135305002Scy** indirect flag is cleared. If it is greater than zero, the indirect flag
10136305002Scy** is set. Passing a value less than zero does not modify the current value
10137366076Scy** of the indirect flag, and may be used to query the current state of the
10138305002Scy** indirect flag for the specified session object.
10139305002Scy**
10140366076Scy** The return value indicates the final state of the indirect flag: 0 if
10141305002Scy** it is clear, or 1 if it is set.
10142305002Scy*/
10143322444SpeterSQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
10144305002Scy
10145305002Scy/*
10146305002Scy** CAPI3REF: Attach A Table To A Session Object
10147342292Scy** METHOD: sqlite3_session
10148305002Scy**
10149305002Scy** If argument zTab is not NULL, then it is the name of a table to attach
10150366076Scy** to the session object passed as the first argument. All subsequent changes
10151366076Scy** made to the table while the session object is enabled will be recorded. See
10152305002Scy** documentation for [sqlite3session_changeset()] for further details.
10153305002Scy**
10154305002Scy** Or, if argument zTab is NULL, then changes are recorded for all tables
10155366076Scy** in the database. If additional tables are added to the database (by
10156366076Scy** executing "CREATE TABLE" statements) after this call is made, changes for
10157305002Scy** the new tables are also recorded.
10158305002Scy**
10159305002Scy** Changes can only be recorded for tables that have a PRIMARY KEY explicitly
10160366076Scy** defined as part of their CREATE TABLE statement. It does not matter if the
10161305002Scy** PRIMARY KEY is an "INTEGER PRIMARY KEY" (rowid alias) or not. The PRIMARY
10162305002Scy** KEY may consist of a single column, or may be a composite key.
10163366076Scy**
10164305002Scy** It is not an error if the named table does not exist in the database. Nor
10165305002Scy** is it an error if the named table does not have a PRIMARY KEY. However,
10166305002Scy** no changes will be recorded in either of these scenarios.
10167305002Scy**
10168305002Scy** Changes are not recorded for individual rows that have NULL values stored
10169305002Scy** in one or more of their PRIMARY KEY columns.
10170305002Scy**
10171366076Scy** SQLITE_OK is returned if the call completes without error. Or, if an error
10172305002Scy** occurs, an SQLite error code (e.g. SQLITE_NOMEM) is returned.
10173342292Scy**
10174342292Scy** <h3>Special sqlite_stat1 Handling</h3>
10175342292Scy**
10176366076Scy** As of SQLite version 3.22.0, the "sqlite_stat1" table is an exception to
10177342292Scy** some of the rules above. In SQLite, the schema of sqlite_stat1 is:
10178342292Scy**  <pre>
10179366076Scy**  &nbsp;     CREATE TABLE sqlite_stat1(tbl,idx,stat)
10180342292Scy**  </pre>
10181342292Scy**
10182366076Scy** Even though sqlite_stat1 does not have a PRIMARY KEY, changes are
10183366076Scy** recorded for it as if the PRIMARY KEY is (tbl,idx). Additionally, changes
10184342292Scy** are recorded for rows for which (idx IS NULL) is true. However, for such
10185342292Scy** rows a zero-length blob (SQL value X'') is stored in the changeset or
10186342292Scy** patchset instead of a NULL value. This allows such changesets to be
10187342292Scy** manipulated by legacy implementations of sqlite3changeset_invert(),
10188342292Scy** concat() and similar.
10189342292Scy**
10190366076Scy** The sqlite3changeset_apply() function automatically converts the
10191342292Scy** zero-length blob back to a NULL value when updating the sqlite_stat1
10192342292Scy** table. However, if the application calls sqlite3changeset_new(),
10193366076Scy** sqlite3changeset_old() or sqlite3changeset_conflict on a changeset
10194342292Scy** iterator directly (including on a changeset iterator passed to a
10195342292Scy** conflict-handler callback) then the X'' value is returned. The application
10196342292Scy** must translate X'' to NULL itself if required.
10197342292Scy**
10198342292Scy** Legacy (older than 3.22.0) versions of the sessions module cannot capture
10199342292Scy** changes made to the sqlite_stat1 table. Legacy versions of the
10200342292Scy** sqlite3changeset_apply() function silently ignore any modifications to the
10201342292Scy** sqlite_stat1 table that are part of a changeset or patchset.
10202305002Scy*/
10203322444SpeterSQLITE_API int sqlite3session_attach(
10204305002Scy  sqlite3_session *pSession,      /* Session object */
10205305002Scy  const char *zTab                /* Table name */
10206305002Scy);
10207305002Scy
10208305002Scy/*
10209305002Scy** CAPI3REF: Set a table filter on a Session Object.
10210342292Scy** METHOD: sqlite3_session
10211305002Scy**
10212366076Scy** The second argument (xFilter) is the "filter callback". For changes to rows
10213322444Speter** in tables that are not attached to the Session object, the filter is called
10214366076Scy** to determine whether changes to the table's rows should be tracked or not.
10215366076Scy** If xFilter returns 0, changes are not tracked. Note that once a table is
10216305002Scy** attached, xFilter will not be called again.
10217305002Scy*/
10218322444SpeterSQLITE_API void sqlite3session_table_filter(
10219305002Scy  sqlite3_session *pSession,      /* Session object */
10220305002Scy  int(*xFilter)(
10221305002Scy    void *pCtx,                   /* Copy of third arg to _filter_table() */
10222305002Scy    const char *zTab              /* Table name */
10223305002Scy  ),
10224305002Scy  void *pCtx                      /* First argument passed to xFilter */
10225305002Scy);
10226305002Scy
10227305002Scy/*
10228305002Scy** CAPI3REF: Generate A Changeset From A Session Object
10229342292Scy** METHOD: sqlite3_session
10230305002Scy**
10231366076Scy** Obtain a changeset containing changes to the tables attached to the
10232366076Scy** session object passed as the first argument. If successful,
10233366076Scy** set *ppChangeset to point to a buffer containing the changeset
10234305002Scy** and *pnChangeset to the size of the changeset in bytes before returning
10235305002Scy** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
10236305002Scy** zero and return an SQLite error code.
10237305002Scy**
10238305002Scy** A changeset consists of zero or more INSERT, UPDATE and/or DELETE changes,
10239305002Scy** each representing a change to a single row of an attached table. An INSERT
10240305002Scy** change contains the values of each field of a new database row. A DELETE
10241305002Scy** contains the original values of each field of a deleted database row. An
10242305002Scy** UPDATE change contains the original values of each field of an updated
10243305002Scy** database row along with the updated values for each updated non-primary-key
10244305002Scy** column. It is not possible for an UPDATE change to represent a change that
10245305002Scy** modifies the values of primary key columns. If such a change is made, it
10246305002Scy** is represented in a changeset as a DELETE followed by an INSERT.
10247305002Scy**
10248366076Scy** Changes are not recorded for rows that have NULL values stored in one or
10249305002Scy** more of their PRIMARY KEY columns. If such a row is inserted or deleted,
10250305002Scy** no corresponding change is present in the changesets returned by this
10251305002Scy** function. If an existing row with one or more NULL values stored in
10252305002Scy** PRIMARY KEY columns is updated so that all PRIMARY KEY columns are non-NULL,
10253305002Scy** only an INSERT is appears in the changeset. Similarly, if an existing row
10254305002Scy** with non-NULL PRIMARY KEY values is updated so that one or more of its
10255305002Scy** PRIMARY KEY columns are set to NULL, the resulting changeset contains a
10256305002Scy** DELETE change only.
10257305002Scy**
10258305002Scy** The contents of a changeset may be traversed using an iterator created
10259305002Scy** using the [sqlite3changeset_start()] API. A changeset may be applied to
10260305002Scy** a database with a compatible schema using the [sqlite3changeset_apply()]
10261305002Scy** API.
10262305002Scy**
10263305002Scy** Within a changeset generated by this function, all changes related to a
10264305002Scy** single table are grouped together. In other words, when iterating through
10265305002Scy** a changeset or when applying a changeset to a database, all changes related
10266305002Scy** to a single table are processed before moving on to the next table. Tables
10267305002Scy** are sorted in the same order in which they were attached (or auto-attached)
10268305002Scy** to the sqlite3_session object. The order in which the changes related to
10269305002Scy** a single table are stored is undefined.
10270305002Scy**
10271305002Scy** Following a successful call to this function, it is the responsibility of
10272305002Scy** the caller to eventually free the buffer that *ppChangeset points to using
10273305002Scy** [sqlite3_free()].
10274305002Scy**
10275305002Scy** <h3>Changeset Generation</h3>
10276305002Scy**
10277305002Scy** Once a table has been attached to a session object, the session object
10278305002Scy** records the primary key values of all new rows inserted into the table.
10279305002Scy** It also records the original primary key and other column values of any
10280305002Scy** deleted or updated rows. For each unique primary key value, data is only
10281305002Scy** recorded once - the first time a row with said primary key is inserted,
10282305002Scy** updated or deleted in the lifetime of the session.
10283305002Scy**
10284305002Scy** There is one exception to the previous paragraph: when a row is inserted,
10285305002Scy** updated or deleted, if one or more of its primary key columns contain a
10286305002Scy** NULL value, no record of the change is made.
10287305002Scy**
10288305002Scy** The session object therefore accumulates two types of records - those
10289305002Scy** that consist of primary key values only (created when the user inserts
10290305002Scy** a new record) and those that consist of the primary key values and the
10291305002Scy** original values of other table columns (created when the users deletes
10292305002Scy** or updates a record).
10293305002Scy**
10294305002Scy** When this function is called, the requested changeset is created using
10295305002Scy** both the accumulated records and the current contents of the database
10296305002Scy** file. Specifically:
10297305002Scy**
10298305002Scy** <ul>
10299305002Scy**   <li> For each record generated by an insert, the database is queried
10300305002Scy**        for a row with a matching primary key. If one is found, an INSERT
10301366076Scy**        change is added to the changeset. If no such row is found, no change
10302305002Scy**        is added to the changeset.
10303305002Scy**
10304366076Scy**   <li> For each record generated by an update or delete, the database is
10305305002Scy**        queried for a row with a matching primary key. If such a row is
10306305002Scy**        found and one or more of the non-primary key fields have been
10307366076Scy**        modified from their original values, an UPDATE change is added to
10308366076Scy**        the changeset. Or, if no such row is found in the table, a DELETE
10309305002Scy**        change is added to the changeset. If there is a row with a matching
10310305002Scy**        primary key in the database, but all fields contain their original
10311305002Scy**        values, no change is added to the changeset.
10312305002Scy** </ul>
10313305002Scy**
10314305002Scy** This means, amongst other things, that if a row is inserted and then later
10315305002Scy** deleted while a session object is active, neither the insert nor the delete
10316366076Scy** will be present in the changeset. Or if a row is deleted and then later a
10317305002Scy** row with the same primary key values inserted while a session object is
10318305002Scy** active, the resulting changeset will contain an UPDATE change instead of
10319305002Scy** a DELETE and an INSERT.
10320305002Scy**
10321305002Scy** When a session object is disabled (see the [sqlite3session_enable()] API),
10322305002Scy** it does not accumulate records when rows are inserted, updated or deleted.
10323305002Scy** This may appear to have some counter-intuitive effects if a single row
10324305002Scy** is written to more than once during a session. For example, if a row
10325366076Scy** is inserted while a session object is enabled, then later deleted while
10326305002Scy** the same session object is disabled, no INSERT record will appear in the
10327305002Scy** changeset, even though the delete took place while the session was disabled.
10328366076Scy** Or, if one field of a row is updated while a session is disabled, and
10329305002Scy** another field of the same row is updated while the session is enabled, the
10330305002Scy** resulting changeset will contain an UPDATE change that updates both fields.
10331305002Scy*/
10332322444SpeterSQLITE_API int sqlite3session_changeset(
10333305002Scy  sqlite3_session *pSession,      /* Session object */
10334305002Scy  int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
10335305002Scy  void **ppChangeset              /* OUT: Buffer containing changeset */
10336305002Scy);
10337305002Scy
10338305002Scy/*
10339342292Scy** CAPI3REF: Load The Difference Between Tables Into A Session
10340342292Scy** METHOD: sqlite3_session
10341305002Scy**
10342305002Scy** If it is not already attached to the session object passed as the first
10343305002Scy** argument, this function attaches table zTbl in the same manner as the
10344305002Scy** [sqlite3session_attach()] function. If zTbl does not exist, or if it
10345305002Scy** does not have a primary key, this function is a no-op (but does not return
10346305002Scy** an error).
10347305002Scy**
10348305002Scy** Argument zFromDb must be the name of a database ("main", "temp" etc.)
10349366076Scy** attached to the same database handle as the session object that contains
10350305002Scy** a table compatible with the table attached to the session by this function.
10351305002Scy** A table is considered compatible if it:
10352305002Scy**
10353305002Scy** <ul>
10354305002Scy**   <li> Has the same name,
10355305002Scy**   <li> Has the same set of columns declared in the same order, and
10356305002Scy**   <li> Has the same PRIMARY KEY definition.
10357305002Scy** </ul>
10358305002Scy**
10359305002Scy** If the tables are not compatible, SQLITE_SCHEMA is returned. If the tables
10360305002Scy** are compatible but do not have any PRIMARY KEY columns, it is not an error
10361305002Scy** but no changes are added to the session object. As with other session
10362305002Scy** APIs, tables without PRIMARY KEYs are simply ignored.
10363305002Scy**
10364305002Scy** This function adds a set of changes to the session object that could be
10365366076Scy** used to update the table in database zFrom (call this the "from-table")
10366366076Scy** so that its content is the same as the table attached to the session
10367305002Scy** object (call this the "to-table"). Specifically:
10368305002Scy**
10369305002Scy** <ul>
10370366076Scy**   <li> For each row (primary key) that exists in the to-table but not in
10371305002Scy**     the from-table, an INSERT record is added to the session object.
10372305002Scy**
10373366076Scy**   <li> For each row (primary key) that exists in the to-table but not in
10374305002Scy**     the from-table, a DELETE record is added to the session object.
10375305002Scy**
10376366076Scy**   <li> For each row (primary key) that exists in both tables, but features
10377322444Speter**     different non-PK values in each, an UPDATE record is added to the
10378366076Scy**     session.
10379305002Scy** </ul>
10380305002Scy**
10381305002Scy** To clarify, if this function is called and then a changeset constructed
10382366076Scy** using [sqlite3session_changeset()], then after applying that changeset to
10383366076Scy** database zFrom the contents of the two compatible tables would be
10384305002Scy** identical.
10385305002Scy**
10386305002Scy** It an error if database zFrom does not exist or does not contain the
10387305002Scy** required compatible table.
10388305002Scy**
10389361456Scy** If the operation is successful, SQLITE_OK is returned. Otherwise, an SQLite
10390305002Scy** error code. In this case, if argument pzErrMsg is not NULL, *pzErrMsg
10391366076Scy** may be set to point to a buffer containing an English language error
10392305002Scy** message. It is the responsibility of the caller to free this buffer using
10393305002Scy** sqlite3_free().
10394305002Scy*/
10395322444SpeterSQLITE_API int sqlite3session_diff(
10396305002Scy  sqlite3_session *pSession,
10397305002Scy  const char *zFromDb,
10398305002Scy  const char *zTbl,
10399305002Scy  char **pzErrMsg
10400305002Scy);
10401305002Scy
10402305002Scy
10403305002Scy/*
10404305002Scy** CAPI3REF: Generate A Patchset From A Session Object
10405342292Scy** METHOD: sqlite3_session
10406305002Scy**
10407305002Scy** The differences between a patchset and a changeset are that:
10408305002Scy**
10409305002Scy** <ul>
10410366076Scy**   <li> DELETE records consist of the primary key fields only. The
10411305002Scy**        original values of other fields are omitted.
10412366076Scy**   <li> The original values of any modified fields are omitted from
10413305002Scy**        UPDATE records.
10414305002Scy** </ul>
10415305002Scy**
10416366076Scy** A patchset blob may be used with up to date versions of all
10417366076Scy** sqlite3changeset_xxx API functions except for sqlite3changeset_invert(),
10418305002Scy** which returns SQLITE_CORRUPT if it is passed a patchset. Similarly,
10419305002Scy** attempting to use a patchset blob with old versions of the
10420366076Scy** sqlite3changeset_xxx APIs also provokes an SQLITE_CORRUPT error.
10421305002Scy**
10422366076Scy** Because the non-primary key "old.*" fields are omitted, no
10423305002Scy** SQLITE_CHANGESET_DATA conflicts can be detected or reported if a patchset
10424305002Scy** is passed to the sqlite3changeset_apply() API. Other conflict types work
10425305002Scy** in the same way as for changesets.
10426305002Scy**
10427305002Scy** Changes within a patchset are ordered in the same way as for changesets
10428305002Scy** generated by the sqlite3session_changeset() function (i.e. all changes for
10429305002Scy** a single table are grouped together, tables appear in the order in which
10430305002Scy** they were attached to the session object).
10431305002Scy*/
10432322444SpeterSQLITE_API int sqlite3session_patchset(
10433305002Scy  sqlite3_session *pSession,      /* Session object */
10434342292Scy  int *pnPatchset,                /* OUT: Size of buffer at *ppPatchset */
10435342292Scy  void **ppPatchset               /* OUT: Buffer containing patchset */
10436305002Scy);
10437305002Scy
10438305002Scy/*
10439305002Scy** CAPI3REF: Test if a changeset has recorded any changes.
10440305002Scy**
10441366076Scy** Return non-zero if no changes to attached tables have been recorded by
10442366076Scy** the session object passed as the first argument. Otherwise, if one or
10443305002Scy** more changes have been recorded, return zero.
10444305002Scy**
10445305002Scy** Even if this function returns zero, it is possible that calling
10446305002Scy** [sqlite3session_changeset()] on the session handle may still return a
10447366076Scy** changeset that contains no changes. This can happen when a row in
10448366076Scy** an attached table is modified and then later on the original values
10449305002Scy** are restored. However, if this function returns non-zero, then it is
10450366076Scy** guaranteed that a call to sqlite3session_changeset() will return a
10451305002Scy** changeset containing zero changes.
10452305002Scy*/
10453322444SpeterSQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
10454305002Scy
10455305002Scy/*
10456369951Scy** CAPI3REF: Query for the amount of heap memory used by a session object.
10457369951Scy**
10458369951Scy** This API returns the total amount of heap memory in bytes currently
10459369951Scy** used by the session object passed as the only argument.
10460369951Scy*/
10461369951ScySQLITE_API sqlite3_int64 sqlite3session_memory_used(sqlite3_session *pSession);
10462369951Scy
10463369951Scy/*
10464366076Scy** CAPI3REF: Create An Iterator To Traverse A Changeset
10465342292Scy** CONSTRUCTOR: sqlite3_changeset_iter
10466305002Scy**
10467305002Scy** Create an iterator used to iterate through the contents of a changeset.
10468305002Scy** If successful, *pp is set to point to the iterator handle and SQLITE_OK
10469305002Scy** is returned. Otherwise, if an error occurs, *pp is set to zero and an
10470305002Scy** SQLite error code is returned.
10471305002Scy**
10472366076Scy** The following functions can be used to advance and query a changeset
10473305002Scy** iterator created by this function:
10474305002Scy**
10475305002Scy** <ul>
10476305002Scy**   <li> [sqlite3changeset_next()]
10477305002Scy**   <li> [sqlite3changeset_op()]
10478305002Scy**   <li> [sqlite3changeset_new()]
10479305002Scy**   <li> [sqlite3changeset_old()]
10480305002Scy** </ul>
10481305002Scy**
10482305002Scy** It is the responsibility of the caller to eventually destroy the iterator
10483305002Scy** by passing it to [sqlite3changeset_finalize()]. The buffer containing the
10484305002Scy** changeset (pChangeset) must remain valid until after the iterator is
10485305002Scy** destroyed.
10486305002Scy**
10487305002Scy** Assuming the changeset blob was created by one of the
10488305002Scy** [sqlite3session_changeset()], [sqlite3changeset_concat()] or
10489366076Scy** [sqlite3changeset_invert()] functions, all changes within the changeset
10490366076Scy** that apply to a single table are grouped together. This means that when
10491366076Scy** an application iterates through a changeset using an iterator created by
10492366076Scy** this function, all changes that relate to a single table are visited
10493366076Scy** consecutively. There is no chance that the iterator will visit a change
10494366076Scy** the applies to table X, then one for table Y, and then later on visit
10495305002Scy** another change for table X.
10496342292Scy**
10497342292Scy** The behavior of sqlite3changeset_start_v2() and its streaming equivalent
10498342292Scy** may be modified by passing a combination of
10499342292Scy** [SQLITE_CHANGESETSTART_INVERT | supported flags] as the 4th parameter.
10500342292Scy**
10501342292Scy** Note that the sqlite3changeset_start_v2() API is still <b>experimental</b>
10502342292Scy** and therefore subject to change.
10503305002Scy*/
10504322444SpeterSQLITE_API int sqlite3changeset_start(
10505305002Scy  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
10506305002Scy  int nChangeset,                 /* Size of changeset blob in bytes */
10507305002Scy  void *pChangeset                /* Pointer to blob containing changeset */
10508305002Scy);
10509342292ScySQLITE_API int sqlite3changeset_start_v2(
10510342292Scy  sqlite3_changeset_iter **pp,    /* OUT: New changeset iterator handle */
10511342292Scy  int nChangeset,                 /* Size of changeset blob in bytes */
10512342292Scy  void *pChangeset,               /* Pointer to blob containing changeset */
10513342292Scy  int flags                       /* SESSION_CHANGESETSTART_* flags */
10514342292Scy);
10515305002Scy
10516342292Scy/*
10517342292Scy** CAPI3REF: Flags for sqlite3changeset_start_v2
10518342292Scy**
10519342292Scy** The following flags may passed via the 4th parameter to
10520342292Scy** [sqlite3changeset_start_v2] and [sqlite3changeset_start_v2_strm]:
10521342292Scy**
10522342292Scy** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
10523342292Scy**   Invert the changeset while iterating through it. This is equivalent to
10524342292Scy**   inverting a changeset using sqlite3changeset_invert() before applying it.
10525342292Scy**   It is an error to specify this flag with a patchset.
10526342292Scy*/
10527342292Scy#define SQLITE_CHANGESETSTART_INVERT        0x0002
10528305002Scy
10529342292Scy
10530305002Scy/*
10531305002Scy** CAPI3REF: Advance A Changeset Iterator
10532342292Scy** METHOD: sqlite3_changeset_iter
10533305002Scy**
10534361456Scy** This function may only be used with iterators created by the function
10535305002Scy** [sqlite3changeset_start()]. If it is called on an iterator passed to
10536305002Scy** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
10537305002Scy** is returned and the call has no effect.
10538305002Scy**
10539305002Scy** Immediately after an iterator is created by sqlite3changeset_start(), it
10540305002Scy** does not point to any change in the changeset. Assuming the changeset
10541305002Scy** is not empty, the first call to this function advances the iterator to
10542305002Scy** point to the first change in the changeset. Each subsequent call advances
10543305002Scy** the iterator to point to the next change in the changeset (if any). If
10544305002Scy** no error occurs and the iterator points to a valid change after a call
10545366076Scy** to sqlite3changeset_next() has advanced it, SQLITE_ROW is returned.
10546305002Scy** Otherwise, if all changes in the changeset have already been visited,
10547305002Scy** SQLITE_DONE is returned.
10548305002Scy**
10549366076Scy** If an error occurs, an SQLite error code is returned. Possible error
10550366076Scy** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or
10551305002Scy** SQLITE_NOMEM.
10552305002Scy*/
10553322444SpeterSQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
10554305002Scy
10555305002Scy/*
10556305002Scy** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
10557342292Scy** METHOD: sqlite3_changeset_iter
10558305002Scy**
10559305002Scy** The pIter argument passed to this function may either be an iterator
10560305002Scy** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10561305002Scy** created by [sqlite3changeset_start()]. In the latter case, the most recent
10562305002Scy** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
10563305002Scy** is not the case, this function returns [SQLITE_MISUSE].
10564305002Scy**
10565369951Scy** Arguments pOp, pnCol and pzTab may not be NULL. Upon return, three
10566369951Scy** outputs are set through these pointers:
10567369951Scy**
10568369951Scy** *pOp is set to one of [SQLITE_INSERT], [SQLITE_DELETE] or [SQLITE_UPDATE],
10569369951Scy** depending on the type of change that the iterator currently points to;
10570369951Scy**
10571369951Scy** *pnCol is set to the number of columns in the table affected by the change; and
10572369951Scy**
10573369951Scy** *pzTab is set to point to a nul-terminated utf-8 encoded string containing
10574369951Scy** the name of the table affected by the current change. The buffer remains
10575369951Scy** valid until either sqlite3changeset_next() is called on the iterator
10576369951Scy** or until the conflict-handler function returns.
10577369951Scy**
10578369951Scy** If pbIndirect is not NULL, then *pbIndirect is set to true (1) if the change
10579305002Scy** is an indirect change, or false (0) otherwise. See the documentation for
10580305002Scy** [sqlite3session_indirect()] for a description of direct and indirect
10581369951Scy** changes.
10582305002Scy**
10583305002Scy** If no error occurs, SQLITE_OK is returned. If an error does occur, an
10584305002Scy** SQLite error code is returned. The values of the output variables may not
10585305002Scy** be trusted in this case.
10586305002Scy*/
10587322444SpeterSQLITE_API int sqlite3changeset_op(
10588305002Scy  sqlite3_changeset_iter *pIter,  /* Iterator object */
10589305002Scy  const char **pzTab,             /* OUT: Pointer to table name */
10590305002Scy  int *pnCol,                     /* OUT: Number of columns in table */
10591305002Scy  int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
10592305002Scy  int *pbIndirect                 /* OUT: True for an 'indirect' change */
10593305002Scy);
10594305002Scy
10595305002Scy/*
10596305002Scy** CAPI3REF: Obtain The Primary Key Definition Of A Table
10597342292Scy** METHOD: sqlite3_changeset_iter
10598305002Scy**
10599305002Scy** For each modified table, a changeset includes the following:
10600305002Scy**
10601305002Scy** <ul>
10602305002Scy**   <li> The number of columns in the table, and
10603305002Scy**   <li> Which of those columns make up the tables PRIMARY KEY.
10604305002Scy** </ul>
10605305002Scy**
10606305002Scy** This function is used to find which columns comprise the PRIMARY KEY of
10607305002Scy** the table modified by the change that iterator pIter currently points to.
10608305002Scy** If successful, *pabPK is set to point to an array of nCol entries, where
10609305002Scy** nCol is the number of columns in the table. Elements of *pabPK are set to
10610305002Scy** 0x01 if the corresponding column is part of the tables primary key, or
10611305002Scy** 0x00 if it is not.
10612305002Scy**
10613322444Speter** If argument pnCol is not NULL, then *pnCol is set to the number of columns
10614305002Scy** in the table.
10615305002Scy**
10616305002Scy** If this function is called when the iterator does not point to a valid
10617305002Scy** entry, SQLITE_MISUSE is returned and the output variables zeroed. Otherwise,
10618305002Scy** SQLITE_OK is returned and the output variables populated as described
10619305002Scy** above.
10620305002Scy*/
10621322444SpeterSQLITE_API int sqlite3changeset_pk(
10622305002Scy  sqlite3_changeset_iter *pIter,  /* Iterator object */
10623305002Scy  unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
10624305002Scy  int *pnCol                      /* OUT: Number of entries in output array */
10625305002Scy);
10626305002Scy
10627305002Scy/*
10628305002Scy** CAPI3REF: Obtain old.* Values From A Changeset Iterator
10629342292Scy** METHOD: sqlite3_changeset_iter
10630305002Scy**
10631305002Scy** The pIter argument passed to this function may either be an iterator
10632305002Scy** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10633305002Scy** created by [sqlite3changeset_start()]. In the latter case, the most recent
10634366076Scy** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
10635305002Scy** Furthermore, it may only be called if the type of change that the iterator
10636305002Scy** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
10637305002Scy** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
10638305002Scy**
10639305002Scy** Argument iVal must be greater than or equal to 0, and less than the number
10640305002Scy** of columns in the table affected by the current change. Otherwise,
10641305002Scy** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
10642305002Scy**
10643305002Scy** If successful, this function sets *ppValue to point to a protected
10644366076Scy** sqlite3_value object containing the iVal'th value from the vector of
10645305002Scy** original row values stored as part of the UPDATE or DELETE change and
10646366076Scy** returns SQLITE_OK. The name of the function comes from the fact that this
10647305002Scy** is similar to the "old.*" columns available to update or delete triggers.
10648305002Scy**
10649305002Scy** If some other error occurs (e.g. an OOM condition), an SQLite error code
10650305002Scy** is returned and *ppValue is set to NULL.
10651305002Scy*/
10652322444SpeterSQLITE_API int sqlite3changeset_old(
10653305002Scy  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
10654305002Scy  int iVal,                       /* Column number */
10655305002Scy  sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
10656305002Scy);
10657305002Scy
10658305002Scy/*
10659305002Scy** CAPI3REF: Obtain new.* Values From A Changeset Iterator
10660342292Scy** METHOD: sqlite3_changeset_iter
10661305002Scy**
10662305002Scy** The pIter argument passed to this function may either be an iterator
10663305002Scy** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
10664305002Scy** created by [sqlite3changeset_start()]. In the latter case, the most recent
10665366076Scy** call to [sqlite3changeset_next()] must have returned SQLITE_ROW.
10666305002Scy** Furthermore, it may only be called if the type of change that the iterator
10667305002Scy** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
10668305002Scy** this function returns [SQLITE_MISUSE] and sets *ppValue to NULL.
10669305002Scy**
10670305002Scy** Argument iVal must be greater than or equal to 0, and less than the number
10671305002Scy** of columns in the table affected by the current change. Otherwise,
10672305002Scy** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
10673305002Scy**
10674305002Scy** If successful, this function sets *ppValue to point to a protected
10675366076Scy** sqlite3_value object containing the iVal'th value from the vector of
10676305002Scy** new row values stored as part of the UPDATE or INSERT change and
10677305002Scy** returns SQLITE_OK. If the change is an UPDATE and does not include
10678366076Scy** a new value for the requested column, *ppValue is set to NULL and
10679366076Scy** SQLITE_OK returned. The name of the function comes from the fact that
10680366076Scy** this is similar to the "new.*" columns available to update or delete
10681305002Scy** triggers.
10682305002Scy**
10683305002Scy** If some other error occurs (e.g. an OOM condition), an SQLite error code
10684305002Scy** is returned and *ppValue is set to NULL.
10685305002Scy*/
10686322444SpeterSQLITE_API int sqlite3changeset_new(
10687305002Scy  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
10688305002Scy  int iVal,                       /* Column number */
10689305002Scy  sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
10690305002Scy);
10691305002Scy
10692305002Scy/*
10693305002Scy** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
10694342292Scy** METHOD: sqlite3_changeset_iter
10695305002Scy**
10696305002Scy** This function should only be used with iterator objects passed to a
10697305002Scy** conflict-handler callback by [sqlite3changeset_apply()] with either
10698305002Scy** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
10699305002Scy** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
10700305002Scy** is set to NULL.
10701305002Scy**
10702305002Scy** Argument iVal must be greater than or equal to 0, and less than the number
10703305002Scy** of columns in the table affected by the current change. Otherwise,
10704305002Scy** [SQLITE_RANGE] is returned and *ppValue is set to NULL.
10705305002Scy**
10706305002Scy** If successful, this function sets *ppValue to point to a protected
10707366076Scy** sqlite3_value object containing the iVal'th value from the
10708305002Scy** "conflicting row" associated with the current conflict-handler callback
10709305002Scy** and returns SQLITE_OK.
10710305002Scy**
10711305002Scy** If some other error occurs (e.g. an OOM condition), an SQLite error code
10712305002Scy** is returned and *ppValue is set to NULL.
10713305002Scy*/
10714322444SpeterSQLITE_API int sqlite3changeset_conflict(
10715305002Scy  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
10716305002Scy  int iVal,                       /* Column number */
10717305002Scy  sqlite3_value **ppValue         /* OUT: Value from conflicting row */
10718305002Scy);
10719305002Scy
10720305002Scy/*
10721305002Scy** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
10722342292Scy** METHOD: sqlite3_changeset_iter
10723305002Scy**
10724305002Scy** This function may only be called with an iterator passed to an
10725305002Scy** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
10726305002Scy** it sets the output variable to the total number of known foreign key
10727305002Scy** violations in the destination database and returns SQLITE_OK.
10728305002Scy**
10729305002Scy** In all other cases this function returns SQLITE_MISUSE.
10730305002Scy*/
10731322444SpeterSQLITE_API int sqlite3changeset_fk_conflicts(
10732305002Scy  sqlite3_changeset_iter *pIter,  /* Changeset iterator */
10733305002Scy  int *pnOut                      /* OUT: Number of FK violations */
10734305002Scy);
10735305002Scy
10736305002Scy
10737305002Scy/*
10738305002Scy** CAPI3REF: Finalize A Changeset Iterator
10739342292Scy** METHOD: sqlite3_changeset_iter
10740305002Scy**
10741305002Scy** This function is used to finalize an iterator allocated with
10742305002Scy** [sqlite3changeset_start()].
10743305002Scy**
10744305002Scy** This function should only be called on iterators created using the
10745305002Scy** [sqlite3changeset_start()] function. If an application calls this
10746305002Scy** function with an iterator passed to a conflict-handler by
10747305002Scy** [sqlite3changeset_apply()], [SQLITE_MISUSE] is immediately returned and the
10748305002Scy** call has no effect.
10749305002Scy**
10750305002Scy** If an error was encountered within a call to an sqlite3changeset_xxx()
10751366076Scy** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an
10752305002Scy** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
10753305002Scy** to that error is returned by this function. Otherwise, SQLITE_OK is
10754305002Scy** returned. This is to allow the following pattern (pseudo-code):
10755305002Scy**
10756342292Scy** <pre>
10757305002Scy**   sqlite3changeset_start();
10758305002Scy**   while( SQLITE_ROW==sqlite3changeset_next() ){
10759305002Scy**     // Do something with change.
10760305002Scy**   }
10761305002Scy**   rc = sqlite3changeset_finalize();
10762305002Scy**   if( rc!=SQLITE_OK ){
10763366076Scy**     // An error has occurred
10764305002Scy**   }
10765342292Scy** </pre>
10766305002Scy*/
10767322444SpeterSQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
10768305002Scy
10769305002Scy/*
10770305002Scy** CAPI3REF: Invert A Changeset
10771305002Scy**
10772305002Scy** This function is used to "invert" a changeset object. Applying an inverted
10773305002Scy** changeset to a database reverses the effects of applying the uninverted
10774305002Scy** changeset. Specifically:
10775305002Scy**
10776305002Scy** <ul>
10777305002Scy**   <li> Each DELETE change is changed to an INSERT, and
10778305002Scy**   <li> Each INSERT change is changed to a DELETE, and
10779305002Scy**   <li> For each UPDATE change, the old.* and new.* values are exchanged.
10780305002Scy** </ul>
10781305002Scy**
10782305002Scy** This function does not change the order in which changes appear within
10783305002Scy** the changeset. It merely reverses the sense of each individual change.
10784305002Scy**
10785305002Scy** If successful, a pointer to a buffer containing the inverted changeset
10786305002Scy** is stored in *ppOut, the size of the same buffer is stored in *pnOut, and
10787305002Scy** SQLITE_OK is returned. If an error occurs, both *pnOut and *ppOut are
10788305002Scy** zeroed and an SQLite error code returned.
10789305002Scy**
10790305002Scy** It is the responsibility of the caller to eventually call sqlite3_free()
10791366076Scy** on the *ppOut pointer to free the buffer allocation following a successful
10792305002Scy** call to this function.
10793305002Scy**
10794305002Scy** WARNING/TODO: This function currently assumes that the input is a valid
10795305002Scy** changeset. If it is not, the results are undefined.
10796305002Scy*/
10797322444SpeterSQLITE_API int sqlite3changeset_invert(
10798305002Scy  int nIn, const void *pIn,       /* Input changeset */
10799305002Scy  int *pnOut, void **ppOut        /* OUT: Inverse of input */
10800305002Scy);
10801305002Scy
10802305002Scy/*
10803305002Scy** CAPI3REF: Concatenate Two Changeset Objects
10804305002Scy**
10805366076Scy** This function is used to concatenate two changesets, A and B, into a
10806305002Scy** single changeset. The result is a changeset equivalent to applying
10807366076Scy** changeset A followed by changeset B.
10808305002Scy**
10809366076Scy** This function combines the two input changesets using an
10810305002Scy** sqlite3_changegroup object. Calling it produces similar results as the
10811305002Scy** following code fragment:
10812305002Scy**
10813342292Scy** <pre>
10814305002Scy**   sqlite3_changegroup *pGrp;
10815305002Scy**   rc = sqlite3_changegroup_new(&pGrp);
10816305002Scy**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
10817305002Scy**   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
10818305002Scy**   if( rc==SQLITE_OK ){
10819305002Scy**     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
10820305002Scy**   }else{
10821305002Scy**     *ppOut = 0;
10822305002Scy**     *pnOut = 0;
10823305002Scy**   }
10824342292Scy** </pre>
10825305002Scy**
10826305002Scy** Refer to the sqlite3_changegroup documentation below for details.
10827305002Scy*/
10828322444SpeterSQLITE_API int sqlite3changeset_concat(
10829305002Scy  int nA,                         /* Number of bytes in buffer pA */
10830305002Scy  void *pA,                       /* Pointer to buffer containing changeset A */
10831305002Scy  int nB,                         /* Number of bytes in buffer pB */
10832305002Scy  void *pB,                       /* Pointer to buffer containing changeset B */
10833305002Scy  int *pnOut,                     /* OUT: Number of bytes in output changeset */
10834305002Scy  void **ppOut                    /* OUT: Buffer containing output changeset */
10835305002Scy);
10836305002Scy
10837305002Scy
10838305002Scy/*
10839322444Speter** CAPI3REF: Changegroup Handle
10840342292Scy**
10841366076Scy** A changegroup is an object used to combine two or more
10842342292Scy** [changesets] or [patchsets]
10843305002Scy*/
10844305002Scytypedef struct sqlite3_changegroup sqlite3_changegroup;
10845305002Scy
10846305002Scy/*
10847322444Speter** CAPI3REF: Create A New Changegroup Object
10848342292Scy** CONSTRUCTOR: sqlite3_changegroup
10849305002Scy**
10850305002Scy** An sqlite3_changegroup object is used to combine two or more changesets
10851305002Scy** (or patchsets) into a single changeset (or patchset). A single changegroup
10852305002Scy** object may combine changesets or patchsets, but not both. The output is
10853305002Scy** always in the same format as the input.
10854305002Scy**
10855305002Scy** If successful, this function returns SQLITE_OK and populates (*pp) with
10856305002Scy** a pointer to a new sqlite3_changegroup object before returning. The caller
10857366076Scy** should eventually free the returned object using a call to
10858305002Scy** sqlite3changegroup_delete(). If an error occurs, an SQLite error code
10859305002Scy** (i.e. SQLITE_NOMEM) is returned and *pp is set to NULL.
10860305002Scy**
10861305002Scy** The usual usage pattern for an sqlite3_changegroup object is as follows:
10862305002Scy**
10863305002Scy** <ul>
10864305002Scy**   <li> It is created using a call to sqlite3changegroup_new().
10865305002Scy**
10866305002Scy**   <li> Zero or more changesets (or patchsets) are added to the object
10867305002Scy**        by calling sqlite3changegroup_add().
10868305002Scy**
10869366076Scy**   <li> The result of combining all input changesets together is obtained
10870305002Scy**        by the application via a call to sqlite3changegroup_output().
10871305002Scy**
10872305002Scy**   <li> The object is deleted using a call to sqlite3changegroup_delete().
10873305002Scy** </ul>
10874305002Scy**
10875305002Scy** Any number of calls to add() and output() may be made between the calls to
10876305002Scy** new() and delete(), and in any order.
10877305002Scy**
10878366076Scy** As well as the regular sqlite3changegroup_add() and
10879305002Scy** sqlite3changegroup_output() functions, also available are the streaming
10880305002Scy** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
10881305002Scy*/
10882322444SpeterSQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
10883305002Scy
10884305002Scy/*
10885322444Speter** CAPI3REF: Add A Changeset To A Changegroup
10886342292Scy** METHOD: sqlite3_changegroup
10887322444Speter**
10888305002Scy** Add all changes within the changeset (or patchset) in buffer pData (size
10889366076Scy** nData bytes) to the changegroup.
10890305002Scy**
10891305002Scy** If the buffer contains a patchset, then all prior calls to this function
10892305002Scy** on the same changegroup object must also have specified patchsets. Or, if
10893305002Scy** the buffer contains a changeset, so must have the earlier calls to this
10894305002Scy** function. Otherwise, SQLITE_ERROR is returned and no changes are added
10895305002Scy** to the changegroup.
10896305002Scy**
10897305002Scy** Rows within the changeset and changegroup are identified by the values in
10898305002Scy** their PRIMARY KEY columns. A change in the changeset is considered to
10899305002Scy** apply to the same row as a change already present in the changegroup if
10900305002Scy** the two rows have the same primary key.
10901305002Scy**
10902322444Speter** Changes to rows that do not already appear in the changegroup are
10903305002Scy** simply copied into it. Or, if both the new changeset and the changegroup
10904305002Scy** contain changes that apply to a single row, the final contents of the
10905305002Scy** changegroup depends on the type of each change, as follows:
10906305002Scy**
10907305002Scy** <table border=1 style="margin-left:8ex;margin-right:8ex">
10908305002Scy**   <tr><th style="white-space:pre">Existing Change  </th>
10909305002Scy**       <th style="white-space:pre">New Change       </th>
10910305002Scy**       <th>Output Change
10911305002Scy**   <tr><td>INSERT <td>INSERT <td>
10912305002Scy**       The new change is ignored. This case does not occur if the new
10913305002Scy**       changeset was recorded immediately after the changesets already
10914305002Scy**       added to the changegroup.
10915305002Scy**   <tr><td>INSERT <td>UPDATE <td>
10916366076Scy**       The INSERT change remains in the changegroup. The values in the
10917305002Scy**       INSERT change are modified as if the row was inserted by the
10918305002Scy**       existing change and then updated according to the new change.
10919305002Scy**   <tr><td>INSERT <td>DELETE <td>
10920305002Scy**       The existing INSERT is removed from the changegroup. The DELETE is
10921305002Scy**       not added.
10922305002Scy**   <tr><td>UPDATE <td>INSERT <td>
10923305002Scy**       The new change is ignored. This case does not occur if the new
10924305002Scy**       changeset was recorded immediately after the changesets already
10925305002Scy**       added to the changegroup.
10926305002Scy**   <tr><td>UPDATE <td>UPDATE <td>
10927366076Scy**       The existing UPDATE remains within the changegroup. It is amended
10928366076Scy**       so that the accompanying values are as if the row was updated once
10929305002Scy**       by the existing change and then again by the new change.
10930305002Scy**   <tr><td>UPDATE <td>DELETE <td>
10931305002Scy**       The existing UPDATE is replaced by the new DELETE within the
10932305002Scy**       changegroup.
10933305002Scy**   <tr><td>DELETE <td>INSERT <td>
10934305002Scy**       If one or more of the column values in the row inserted by the
10935366076Scy**       new change differ from those in the row deleted by the existing
10936305002Scy**       change, the existing DELETE is replaced by an UPDATE within the
10937366076Scy**       changegroup. Otherwise, if the inserted row is exactly the same
10938305002Scy**       as the deleted row, the existing DELETE is simply discarded.
10939305002Scy**   <tr><td>DELETE <td>UPDATE <td>
10940305002Scy**       The new change is ignored. This case does not occur if the new
10941305002Scy**       changeset was recorded immediately after the changesets already
10942305002Scy**       added to the changegroup.
10943305002Scy**   <tr><td>DELETE <td>DELETE <td>
10944305002Scy**       The new change is ignored. This case does not occur if the new
10945305002Scy**       changeset was recorded immediately after the changesets already
10946305002Scy**       added to the changegroup.
10947305002Scy** </table>
10948305002Scy**
10949305002Scy** If the new changeset contains changes to a table that is already present
10950305002Scy** in the changegroup, then the number of columns and the position of the
10951305002Scy** primary key columns for the table must be consistent. If this is not the
10952305002Scy** case, this function fails with SQLITE_SCHEMA. If the input changeset
10953305002Scy** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
10954305002Scy** returned. Or, if an out-of-memory condition occurs during processing, this
10955361456Scy** function returns SQLITE_NOMEM. In all cases, if an error occurs the state
10956361456Scy** of the final contents of the changegroup is undefined.
10957305002Scy**
10958305002Scy** If no error occurs, SQLITE_OK is returned.
10959305002Scy*/
10960322444SpeterSQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
10961305002Scy
10962305002Scy/*
10963322444Speter** CAPI3REF: Obtain A Composite Changeset From A Changegroup
10964342292Scy** METHOD: sqlite3_changegroup
10965322444Speter**
10966305002Scy** Obtain a buffer containing a changeset (or patchset) representing the
10967305002Scy** current contents of the changegroup. If the inputs to the changegroup
10968305002Scy** were themselves changesets, the output is a changeset. Or, if the
10969305002Scy** inputs were patchsets, the output is also a patchset.
10970305002Scy**
10971305002Scy** As with the output of the sqlite3session_changeset() and
10972305002Scy** sqlite3session_patchset() functions, all changes related to a single
10973305002Scy** table are grouped together in the output of this function. Tables appear
10974305002Scy** in the same order as for the very first changeset added to the changegroup.
10975305002Scy** If the second or subsequent changesets added to the changegroup contain
10976305002Scy** changes for tables that do not appear in the first changeset, they are
10977305002Scy** appended onto the end of the output changeset, again in the order in
10978305002Scy** which they are first encountered.
10979305002Scy**
10980305002Scy** If an error occurs, an SQLite error code is returned and the output
10981305002Scy** variables (*pnData) and (*ppData) are set to 0. Otherwise, SQLITE_OK
10982366076Scy** is returned and the output variables are set to the size of and a
10983305002Scy** pointer to the output buffer, respectively. In this case it is the
10984305002Scy** responsibility of the caller to eventually free the buffer using a
10985305002Scy** call to sqlite3_free().
10986305002Scy*/
10987322444SpeterSQLITE_API int sqlite3changegroup_output(
10988305002Scy  sqlite3_changegroup*,
10989305002Scy  int *pnData,                    /* OUT: Size of output buffer in bytes */
10990305002Scy  void **ppData                   /* OUT: Pointer to output buffer */
10991305002Scy);
10992305002Scy
10993305002Scy/*
10994322444Speter** CAPI3REF: Delete A Changegroup Object
10995342292Scy** DESTRUCTOR: sqlite3_changegroup
10996305002Scy*/
10997322444SpeterSQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
10998305002Scy
10999305002Scy/*
11000305002Scy** CAPI3REF: Apply A Changeset To A Database
11001305002Scy**
11002342292Scy** Apply a changeset or patchset to a database. These functions attempt to
11003342292Scy** update the "main" database attached to handle db with the changes found in
11004366076Scy** the changeset passed via the second and third arguments.
11005305002Scy**
11006342292Scy** The fourth argument (xFilter) passed to these functions is the "filter
11007305002Scy** callback". If it is not NULL, then for each table affected by at least one
11008305002Scy** change in the changeset, the filter callback is invoked with
11009305002Scy** the table name as the second argument, and a copy of the context pointer
11010342292Scy** passed as the sixth argument as the first. If the "filter callback"
11011342292Scy** returns zero, then no attempt is made to apply any changes to the table.
11012342292Scy** Otherwise, if the return value is non-zero or the xFilter argument to
11013342292Scy** is NULL, all changes related to the table are attempted.
11014305002Scy**
11015366076Scy** For each table that is not excluded by the filter callback, this function
11016366076Scy** tests that the target database contains a compatible table. A table is
11017305002Scy** considered compatible if all of the following are true:
11018305002Scy**
11019305002Scy** <ul>
11020366076Scy**   <li> The table has the same name as the name recorded in the
11021305002Scy**        changeset, and
11022366076Scy**   <li> The table has at least as many columns as recorded in the
11023305002Scy**        changeset, and
11024366076Scy**   <li> The table has primary key columns in the same position as
11025305002Scy**        recorded in the changeset.
11026305002Scy** </ul>
11027305002Scy**
11028305002Scy** If there is no compatible table, it is not an error, but none of the
11029305002Scy** changes associated with the table are applied. A warning message is issued
11030305002Scy** via the sqlite3_log() mechanism with the error code SQLITE_SCHEMA. At most
11031305002Scy** one such warning is issued for each table in the changeset.
11032305002Scy**
11033366076Scy** For each change for which there is a compatible table, an attempt is made
11034366076Scy** to modify the table contents according to the UPDATE, INSERT or DELETE
11035366076Scy** change. If a change cannot be applied cleanly, the conflict handler
11036366076Scy** function passed as the fifth argument to sqlite3changeset_apply() may be
11037366076Scy** invoked. A description of exactly when the conflict handler is invoked for
11038305002Scy** each type of change is below.
11039305002Scy**
11040305002Scy** Unlike the xFilter argument, xConflict may not be passed NULL. The results
11041305002Scy** of passing anything other than a valid function pointer as the xConflict
11042305002Scy** argument are undefined.
11043305002Scy**
11044305002Scy** Each time the conflict handler function is invoked, it must return one
11045366076Scy** of [SQLITE_CHANGESET_OMIT], [SQLITE_CHANGESET_ABORT] or
11046305002Scy** [SQLITE_CHANGESET_REPLACE]. SQLITE_CHANGESET_REPLACE may only be returned
11047305002Scy** if the second argument passed to the conflict handler is either
11048305002Scy** SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If the conflict-handler
11049305002Scy** returns an illegal value, any changes already made are rolled back and
11050366076Scy** the call to sqlite3changeset_apply() returns SQLITE_MISUSE. Different
11051305002Scy** actions are taken by sqlite3changeset_apply() depending on the value
11052305002Scy** returned by each invocation of the conflict-handler function. Refer to
11053366076Scy** the documentation for the three
11054305002Scy** [SQLITE_CHANGESET_OMIT|available return values] for details.
11055305002Scy**
11056305002Scy** <dl>
11057305002Scy** <dt>DELETE Changes<dd>
11058366076Scy**   For each DELETE change, the function checks if the target database
11059366076Scy**   contains a row with the same primary key value (or values) as the
11060366076Scy**   original row values stored in the changeset. If it does, and the values
11061366076Scy**   stored in all non-primary key columns also match the values stored in
11062305002Scy**   the changeset the row is deleted from the target database.
11063305002Scy**
11064305002Scy**   If a row with matching primary key values is found, but one or more of
11065305002Scy**   the non-primary key fields contains a value different from the original
11066305002Scy**   row value stored in the changeset, the conflict-handler function is
11067322444Speter**   invoked with [SQLITE_CHANGESET_DATA] as the second argument. If the
11068322444Speter**   database table has more columns than are recorded in the changeset,
11069322444Speter**   only the values of those non-primary key fields are compared against
11070322444Speter**   the current database contents - any trailing database table columns
11071322444Speter**   are ignored.
11072305002Scy**
11073305002Scy**   If no row with matching primary key values is found in the database,
11074305002Scy**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11075305002Scy**   passed as the second argument.
11076305002Scy**
11077305002Scy**   If the DELETE operation is attempted, but SQLite returns SQLITE_CONSTRAINT
11078305002Scy**   (which can only happen if a foreign key constraint is violated), the
11079305002Scy**   conflict-handler function is invoked with [SQLITE_CHANGESET_CONSTRAINT]
11080305002Scy**   passed as the second argument. This includes the case where the DELETE
11081305002Scy**   operation is attempted because an earlier call to the conflict handler
11082305002Scy**   function returned [SQLITE_CHANGESET_REPLACE].
11083305002Scy**
11084305002Scy** <dt>INSERT Changes<dd>
11085305002Scy**   For each INSERT change, an attempt is made to insert the new row into
11086322444Speter**   the database. If the changeset row contains fewer fields than the
11087322444Speter**   database table, the trailing fields are populated with their default
11088322444Speter**   values.
11089305002Scy**
11090366076Scy**   If the attempt to insert the row fails because the database already
11091305002Scy**   contains a row with the same primary key values, the conflict handler
11092366076Scy**   function is invoked with the second argument set to
11093305002Scy**   [SQLITE_CHANGESET_CONFLICT].
11094305002Scy**
11095305002Scy**   If the attempt to insert the row fails because of some other constraint
11096366076Scy**   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is
11097305002Scy**   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
11098366076Scy**   This includes the case where the INSERT operation is re-attempted because
11099366076Scy**   an earlier call to the conflict handler function returned
11100305002Scy**   [SQLITE_CHANGESET_REPLACE].
11101305002Scy**
11102305002Scy** <dt>UPDATE Changes<dd>
11103366076Scy**   For each UPDATE change, the function checks if the target database
11104366076Scy**   contains a row with the same primary key value (or values) as the
11105366076Scy**   original row values stored in the changeset. If it does, and the values
11106322444Speter**   stored in all modified non-primary key columns also match the values
11107322444Speter**   stored in the changeset the row is updated within the target database.
11108305002Scy**
11109305002Scy**   If a row with matching primary key values is found, but one or more of
11110322444Speter**   the modified non-primary key fields contains a value different from an
11111322444Speter**   original row value stored in the changeset, the conflict-handler function
11112322444Speter**   is invoked with [SQLITE_CHANGESET_DATA] as the second argument. Since
11113305002Scy**   UPDATE changes only contain values for non-primary key fields that are
11114305002Scy**   to be modified, only those fields need to match the original values to
11115305002Scy**   avoid the SQLITE_CHANGESET_DATA conflict-handler callback.
11116305002Scy**
11117305002Scy**   If no row with matching primary key values is found in the database,
11118305002Scy**   the conflict-handler function is invoked with [SQLITE_CHANGESET_NOTFOUND]
11119305002Scy**   passed as the second argument.
11120305002Scy**
11121366076Scy**   If the UPDATE operation is attempted, but SQLite returns
11122366076Scy**   SQLITE_CONSTRAINT, the conflict-handler function is invoked with
11123305002Scy**   [SQLITE_CHANGESET_CONSTRAINT] passed as the second argument.
11124366076Scy**   This includes the case where the UPDATE operation is attempted after
11125305002Scy**   an earlier call to the conflict handler function returned
11126366076Scy**   [SQLITE_CHANGESET_REPLACE].
11127305002Scy** </dl>
11128305002Scy**
11129305002Scy** It is safe to execute SQL statements, including those that write to the
11130305002Scy** table that the callback related to, from within the xConflict callback.
11131361456Scy** This can be used to further customize the application's conflict
11132305002Scy** resolution strategy.
11133305002Scy**
11134342292Scy** All changes made by these functions are enclosed in a savepoint transaction.
11135305002Scy** If any other error (aside from a constraint failure when attempting to
11136305002Scy** write to the target database) occurs, then the savepoint transaction is
11137366076Scy** rolled back, restoring the target database to its original state, and an
11138305002Scy** SQLite error code returned.
11139342292Scy**
11140342292Scy** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
11141342292Scy** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
11142366076Scy** may set (*ppRebase) to point to a "rebase" that may be used with the
11143342292Scy** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
11144342292Scy** is set to the size of the buffer in bytes. It is the responsibility of the
11145342292Scy** caller to eventually free any such buffer using sqlite3_free(). The buffer
11146342292Scy** is only allocated and populated if one or more conflicts were encountered
11147342292Scy** while applying the patchset. See comments surrounding the sqlite3_rebaser
11148342292Scy** APIs for further details.
11149342292Scy**
11150342292Scy** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
11151342292Scy** may be modified by passing a combination of
11152342292Scy** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
11153342292Scy**
11154342292Scy** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
11155342292Scy** and therefore subject to change.
11156305002Scy*/
11157322444SpeterSQLITE_API int sqlite3changeset_apply(
11158305002Scy  sqlite3 *db,                    /* Apply change to "main" db of this handle */
11159305002Scy  int nChangeset,                 /* Size of changeset in bytes */
11160305002Scy  void *pChangeset,               /* Changeset blob */
11161305002Scy  int(*xFilter)(
11162305002Scy    void *pCtx,                   /* Copy of sixth arg to _apply() */
11163305002Scy    const char *zTab              /* Table name */
11164305002Scy  ),
11165305002Scy  int(*xConflict)(
11166305002Scy    void *pCtx,                   /* Copy of sixth arg to _apply() */
11167305002Scy    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
11168305002Scy    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
11169305002Scy  ),
11170305002Scy  void *pCtx                      /* First argument passed to xConflict */
11171305002Scy);
11172342292ScySQLITE_API int sqlite3changeset_apply_v2(
11173342292Scy  sqlite3 *db,                    /* Apply change to "main" db of this handle */
11174342292Scy  int nChangeset,                 /* Size of changeset in bytes */
11175342292Scy  void *pChangeset,               /* Changeset blob */
11176342292Scy  int(*xFilter)(
11177342292Scy    void *pCtx,                   /* Copy of sixth arg to _apply() */
11178342292Scy    const char *zTab              /* Table name */
11179342292Scy  ),
11180342292Scy  int(*xConflict)(
11181342292Scy    void *pCtx,                   /* Copy of sixth arg to _apply() */
11182342292Scy    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
11183342292Scy    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
11184342292Scy  ),
11185342292Scy  void *pCtx,                     /* First argument passed to xConflict */
11186342292Scy  void **ppRebase, int *pnRebase, /* OUT: Rebase data */
11187342292Scy  int flags                       /* SESSION_CHANGESETAPPLY_* flags */
11188342292Scy);
11189305002Scy
11190342292Scy/*
11191342292Scy** CAPI3REF: Flags for sqlite3changeset_apply_v2
11192342292Scy**
11193342292Scy** The following flags may passed via the 9th parameter to
11194342292Scy** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
11195342292Scy**
11196342292Scy** <dl>
11197342292Scy** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
11198342292Scy**   Usually, the sessions module encloses all operations performed by
11199342292Scy**   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
11200342292Scy**   SAVEPOINT is committed if the changeset or patchset is successfully
11201342292Scy**   applied, or rolled back if an error occurs. Specifying this flag
11202342292Scy**   causes the sessions module to omit this savepoint. In this case, if the
11203366076Scy**   caller has an open transaction or savepoint when apply_v2() is called,
11204342292Scy**   it may revert the partially applied changeset by rolling it back.
11205342292Scy**
11206342292Scy** <dt>SQLITE_CHANGESETAPPLY_INVERT <dd>
11207342292Scy**   Invert the changeset before applying it. This is equivalent to inverting
11208342292Scy**   a changeset using sqlite3changeset_invert() before applying it. It is
11209342292Scy**   an error to specify this flag with a patchset.
11210342292Scy*/
11211342292Scy#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
11212342292Scy#define SQLITE_CHANGESETAPPLY_INVERT        0x0002
11213342292Scy
11214366076Scy/*
11215305002Scy** CAPI3REF: Constants Passed To The Conflict Handler
11216305002Scy**
11217305002Scy** Values that may be passed as the second argument to a conflict-handler.
11218305002Scy**
11219305002Scy** <dl>
11220305002Scy** <dt>SQLITE_CHANGESET_DATA<dd>
11221305002Scy**   The conflict handler is invoked with CHANGESET_DATA as the second argument
11222305002Scy**   when processing a DELETE or UPDATE change if a row with the required
11223366076Scy**   PRIMARY KEY fields is present in the database, but one or more other
11224366076Scy**   (non primary-key) fields modified by the update do not contain the
11225305002Scy**   expected "before" values.
11226366076Scy**
11227305002Scy**   The conflicting row, in this case, is the database row with the matching
11228305002Scy**   primary key.
11229366076Scy**
11230305002Scy** <dt>SQLITE_CHANGESET_NOTFOUND<dd>
11231305002Scy**   The conflict handler is invoked with CHANGESET_NOTFOUND as the second
11232305002Scy**   argument when processing a DELETE or UPDATE change if a row with the
11233305002Scy**   required PRIMARY KEY fields is not present in the database.
11234366076Scy**
11235305002Scy**   There is no conflicting row in this case. The results of invoking the
11236305002Scy**   sqlite3changeset_conflict() API are undefined.
11237366076Scy**
11238305002Scy** <dt>SQLITE_CHANGESET_CONFLICT<dd>
11239305002Scy**   CHANGESET_CONFLICT is passed as the second argument to the conflict
11240366076Scy**   handler while processing an INSERT change if the operation would result
11241305002Scy**   in duplicate primary key values.
11242366076Scy**
11243305002Scy**   The conflicting row in this case is the database row with the matching
11244305002Scy**   primary key.
11245305002Scy**
11246305002Scy** <dt>SQLITE_CHANGESET_FOREIGN_KEY<dd>
11247305002Scy**   If foreign key handling is enabled, and applying a changeset leaves the
11248366076Scy**   database in a state containing foreign key violations, the conflict
11249305002Scy**   handler is invoked with CHANGESET_FOREIGN_KEY as the second argument
11250305002Scy**   exactly once before the changeset is committed. If the conflict handler
11251305002Scy**   returns CHANGESET_OMIT, the changes, including those that caused the
11252305002Scy**   foreign key constraint violation, are committed. Or, if it returns
11253305002Scy**   CHANGESET_ABORT, the changeset is rolled back.
11254305002Scy**
11255305002Scy**   No current or conflicting row information is provided. The only function
11256305002Scy**   it is possible to call on the supplied sqlite3_changeset_iter handle
11257305002Scy**   is sqlite3changeset_fk_conflicts().
11258366076Scy**
11259305002Scy** <dt>SQLITE_CHANGESET_CONSTRAINT<dd>
11260366076Scy**   If any other constraint violation occurs while applying a change (i.e.
11261366076Scy**   a UNIQUE, CHECK or NOT NULL constraint), the conflict handler is
11262305002Scy**   invoked with CHANGESET_CONSTRAINT as the second argument.
11263366076Scy**
11264305002Scy**   There is no conflicting row in this case. The results of invoking the
11265305002Scy**   sqlite3changeset_conflict() API are undefined.
11266305002Scy**
11267305002Scy** </dl>
11268305002Scy*/
11269305002Scy#define SQLITE_CHANGESET_DATA        1
11270305002Scy#define SQLITE_CHANGESET_NOTFOUND    2
11271305002Scy#define SQLITE_CHANGESET_CONFLICT    3
11272305002Scy#define SQLITE_CHANGESET_CONSTRAINT  4
11273305002Scy#define SQLITE_CHANGESET_FOREIGN_KEY 5
11274305002Scy
11275366076Scy/*
11276305002Scy** CAPI3REF: Constants Returned By The Conflict Handler
11277305002Scy**
11278305002Scy** A conflict handler callback must return one of the following three values.
11279305002Scy**
11280305002Scy** <dl>
11281305002Scy** <dt>SQLITE_CHANGESET_OMIT<dd>
11282305002Scy**   If a conflict handler returns this value no special action is taken. The
11283366076Scy**   change that caused the conflict is not applied. The session module
11284305002Scy**   continues to the next change in the changeset.
11285305002Scy**
11286305002Scy** <dt>SQLITE_CHANGESET_REPLACE<dd>
11287305002Scy**   This value may only be returned if the second argument to the conflict
11288305002Scy**   handler was SQLITE_CHANGESET_DATA or SQLITE_CHANGESET_CONFLICT. If this
11289366076Scy**   is not the case, any changes applied so far are rolled back and the
11290305002Scy**   call to sqlite3changeset_apply() returns SQLITE_MISUSE.
11291305002Scy**
11292305002Scy**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_DATA conflict
11293305002Scy**   handler, then the conflicting row is either updated or deleted, depending
11294305002Scy**   on the type of change.
11295305002Scy**
11296305002Scy**   If CHANGESET_REPLACE is returned by an SQLITE_CHANGESET_CONFLICT conflict
11297305002Scy**   handler, then the conflicting row is removed from the database and a
11298305002Scy**   second attempt to apply the change is made. If this second attempt fails,
11299305002Scy**   the original row is restored to the database before continuing.
11300305002Scy**
11301305002Scy** <dt>SQLITE_CHANGESET_ABORT<dd>
11302366076Scy**   If this value is returned, any changes applied so far are rolled back
11303305002Scy**   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
11304305002Scy** </dl>
11305305002Scy*/
11306305002Scy#define SQLITE_CHANGESET_OMIT       0
11307305002Scy#define SQLITE_CHANGESET_REPLACE    1
11308305002Scy#define SQLITE_CHANGESET_ABORT      2
11309305002Scy
11310366076Scy/*
11311342292Scy** CAPI3REF: Rebasing changesets
11312342292Scy** EXPERIMENTAL
11313342292Scy**
11314342292Scy** Suppose there is a site hosting a database in state S0. And that
11315342292Scy** modifications are made that move that database to state S1 and a
11316342292Scy** changeset recorded (the "local" changeset). Then, a changeset based
11317366076Scy** on S0 is received from another site (the "remote" changeset) and
11318366076Scy** applied to the database. The database is then in state
11319342292Scy** (S1+"remote"), where the exact state depends on any conflict
11320342292Scy** resolution decisions (OMIT or REPLACE) made while applying "remote".
11321366076Scy** Rebasing a changeset is to update it to take those conflict
11322342292Scy** resolution decisions into account, so that the same conflicts
11323366076Scy** do not have to be resolved elsewhere in the network.
11324342292Scy**
11325342292Scy** For example, if both the local and remote changesets contain an
11326342292Scy** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
11327342292Scy**
11328342292Scy**   local:  INSERT INTO t1 VALUES(1, 'v1');
11329342292Scy**   remote: INSERT INTO t1 VALUES(1, 'v2');
11330342292Scy**
11331342292Scy** and the conflict resolution is REPLACE, then the INSERT change is
11332342292Scy** removed from the local changeset (it was overridden). Or, if the
11333342292Scy** conflict resolution was "OMIT", then the local changeset is modified
11334342292Scy** to instead contain:
11335342292Scy**
11336342292Scy**           UPDATE t1 SET b = 'v2' WHERE a=1;
11337342292Scy**
11338342292Scy** Changes within the local changeset are rebased as follows:
11339342292Scy**
11340342292Scy** <dl>
11341342292Scy** <dt>Local INSERT<dd>
11342366076Scy**   This may only conflict with a remote INSERT. If the conflict
11343342292Scy**   resolution was OMIT, then add an UPDATE change to the rebased
11344342292Scy**   changeset. Or, if the conflict resolution was REPLACE, add
11345342292Scy**   nothing to the rebased changeset.
11346342292Scy**
11347342292Scy** <dt>Local DELETE<dd>
11348342292Scy**   This may conflict with a remote UPDATE or DELETE. In both cases the
11349342292Scy**   only possible resolution is OMIT. If the remote operation was a
11350342292Scy**   DELETE, then add no change to the rebased changeset. If the remote
11351342292Scy**   operation was an UPDATE, then the old.* fields of change are updated
11352342292Scy**   to reflect the new.* values in the UPDATE.
11353342292Scy**
11354342292Scy** <dt>Local UPDATE<dd>
11355342292Scy**   This may conflict with a remote UPDATE or DELETE. If it conflicts
11356342292Scy**   with a DELETE, and the conflict resolution was OMIT, then the update
11357342292Scy**   is changed into an INSERT. Any undefined values in the new.* record
11358342292Scy**   from the update change are filled in using the old.* values from
11359342292Scy**   the conflicting DELETE. Or, if the conflict resolution was REPLACE,
11360342292Scy**   the UPDATE change is simply omitted from the rebased changeset.
11361342292Scy**
11362342292Scy**   If conflict is with a remote UPDATE and the resolution is OMIT, then
11363342292Scy**   the old.* values are rebased using the new.* values in the remote
11364342292Scy**   change. Or, if the resolution is REPLACE, then the change is copied
11365342292Scy**   into the rebased changeset with updates to columns also updated by
11366366076Scy**   the conflicting remote UPDATE removed. If this means no columns would
11367342292Scy**   be updated, the change is omitted.
11368342292Scy** </dl>
11369342292Scy**
11370366076Scy** A local change may be rebased against multiple remote changes
11371366076Scy** simultaneously. If a single key is modified by multiple remote
11372342292Scy** changesets, they are combined as follows before the local changeset
11373342292Scy** is rebased:
11374342292Scy**
11375342292Scy** <ul>
11376342292Scy**    <li> If there has been one or more REPLACE resolutions on a
11377342292Scy**         key, it is rebased according to a REPLACE.
11378342292Scy**
11379342292Scy**    <li> If there have been no REPLACE resolutions on a key, then
11380342292Scy**         the local changeset is rebased according to the most recent
11381342292Scy**         of the OMIT resolutions.
11382342292Scy** </ul>
11383342292Scy**
11384366076Scy** Note that conflict resolutions from multiple remote changesets are
11385366076Scy** combined on a per-field basis, not per-row. This means that in the
11386366076Scy** case of multiple remote UPDATE operations, some fields of a single
11387366076Scy** local change may be rebased for REPLACE while others are rebased for
11388342292Scy** OMIT.
11389342292Scy**
11390342292Scy** In order to rebase a local changeset, the remote changeset must first
11391342292Scy** be applied to the local database using sqlite3changeset_apply_v2() and
11392342292Scy** the buffer of rebase information captured. Then:
11393342292Scy**
11394342292Scy** <ol>
11395366076Scy**   <li> An sqlite3_rebaser object is created by calling
11396342292Scy**        sqlite3rebaser_create().
11397342292Scy**   <li> The new object is configured with the rebase buffer obtained from
11398342292Scy**        sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
11399342292Scy**        If the local changeset is to be rebased against multiple remote
11400342292Scy**        changesets, then sqlite3rebaser_configure() should be called
11401342292Scy**        multiple times, in the same order that the multiple
11402342292Scy**        sqlite3changeset_apply_v2() calls were made.
11403342292Scy**   <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
11404342292Scy**   <li> The sqlite3_rebaser object is deleted by calling
11405342292Scy**        sqlite3rebaser_delete().
11406342292Scy** </ol>
11407342292Scy*/
11408342292Scytypedef struct sqlite3_rebaser sqlite3_rebaser;
11409342292Scy
11410305002Scy/*
11411342292Scy** CAPI3REF: Create a changeset rebaser object.
11412342292Scy** EXPERIMENTAL
11413342292Scy**
11414342292Scy** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
11415342292Scy** point to the new object and return SQLITE_OK. Otherwise, if an error
11416366076Scy** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew)
11417366076Scy** to NULL.
11418342292Scy*/
11419342292ScySQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
11420342292Scy
11421342292Scy/*
11422342292Scy** CAPI3REF: Configure a changeset rebaser object.
11423342292Scy** EXPERIMENTAL
11424342292Scy**
11425342292Scy** Configure the changeset rebaser object to rebase changesets according
11426342292Scy** to the conflict resolutions described by buffer pRebase (size nRebase
11427342292Scy** bytes), which must have been obtained from a previous call to
11428342292Scy** sqlite3changeset_apply_v2().
11429342292Scy*/
11430342292ScySQLITE_API int sqlite3rebaser_configure(
11431366076Scy  sqlite3_rebaser*,
11432342292Scy  int nRebase, const void *pRebase
11433366076Scy);
11434342292Scy
11435342292Scy/*
11436342292Scy** CAPI3REF: Rebase a changeset
11437342292Scy** EXPERIMENTAL
11438342292Scy**
11439342292Scy** Argument pIn must point to a buffer containing a changeset nIn bytes
11440342292Scy** in size. This function allocates and populates a buffer with a copy
11441361456Scy** of the changeset rebased according to the configuration of the
11442342292Scy** rebaser object passed as the first argument. If successful, (*ppOut)
11443366076Scy** is set to point to the new buffer containing the rebased changeset and
11444342292Scy** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
11445342292Scy** responsibility of the caller to eventually free the new buffer using
11446342292Scy** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
11447342292Scy** are set to zero and an SQLite error code returned.
11448342292Scy*/
11449342292ScySQLITE_API int sqlite3rebaser_rebase(
11450342292Scy  sqlite3_rebaser*,
11451366076Scy  int nIn, const void *pIn,
11452366076Scy  int *pnOut, void **ppOut
11453342292Scy);
11454342292Scy
11455342292Scy/*
11456342292Scy** CAPI3REF: Delete a changeset rebaser object.
11457342292Scy** EXPERIMENTAL
11458342292Scy**
11459342292Scy** Delete the changeset rebaser object and all associated resources. There
11460342292Scy** should be one call to this function for each successful invocation
11461342292Scy** of sqlite3rebaser_create().
11462342292Scy*/
11463366076ScySQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p);
11464342292Scy
11465342292Scy/*
11466305002Scy** CAPI3REF: Streaming Versions of API functions.
11467305002Scy**
11468366076Scy** The six streaming API xxx_strm() functions serve similar purposes to the
11469305002Scy** corresponding non-streaming API functions:
11470305002Scy**
11471305002Scy** <table border=1 style="margin-left:8ex;margin-right:8ex">
11472305002Scy**   <tr><th>Streaming function<th>Non-streaming equivalent</th>
11473366076Scy**   <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply]
11474366076Scy**   <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2]
11475366076Scy**   <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat]
11476366076Scy**   <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert]
11477366076Scy**   <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start]
11478366076Scy**   <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset]
11479366076Scy**   <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset]
11480305002Scy** </table>
11481305002Scy**
11482305002Scy** Non-streaming functions that accept changesets (or patchsets) as input
11483366076Scy** require that the entire changeset be stored in a single buffer in memory.
11484366076Scy** Similarly, those that return a changeset or patchset do so by returning
11485366076Scy** a pointer to a single large buffer allocated using sqlite3_malloc().
11486366076Scy** Normally this is convenient. However, if an application running in a
11487305002Scy** low-memory environment is required to handle very large changesets, the
11488305002Scy** large contiguous memory allocations required can become onerous.
11489305002Scy**
11490305002Scy** In order to avoid this problem, instead of a single large buffer, input
11491305002Scy** is passed to a streaming API functions by way of a callback function that
11492305002Scy** the sessions module invokes to incrementally request input data as it is
11493305002Scy** required. In all cases, a pair of API function parameters such as
11494305002Scy**
11495305002Scy**  <pre>
11496305002Scy**  &nbsp;     int nChangeset,
11497305002Scy**  &nbsp;     void *pChangeset,
11498305002Scy**  </pre>
11499305002Scy**
11500305002Scy** Is replaced by:
11501305002Scy**
11502305002Scy**  <pre>
11503305002Scy**  &nbsp;     int (*xInput)(void *pIn, void *pData, int *pnData),
11504305002Scy**  &nbsp;     void *pIn,
11505305002Scy**  </pre>
11506305002Scy**
11507305002Scy** Each time the xInput callback is invoked by the sessions module, the first
11508366076Scy** argument passed is a copy of the supplied pIn context pointer. The second
11509366076Scy** argument, pData, points to a buffer (*pnData) bytes in size. Assuming no
11510366076Scy** error occurs the xInput method should copy up to (*pnData) bytes of data
11511366076Scy** into the buffer and set (*pnData) to the actual number of bytes copied
11512366076Scy** before returning SQLITE_OK. If the input is completely exhausted, (*pnData)
11513366076Scy** should be set to zero to indicate this. Or, if an error occurs, an SQLite
11514305002Scy** error code should be returned. In all cases, if an xInput callback returns
11515305002Scy** an error, all processing is abandoned and the streaming API function
11516305002Scy** returns a copy of the error code to the caller.
11517305002Scy**
11518305002Scy** In the case of sqlite3changeset_start_strm(), the xInput callback may be
11519305002Scy** invoked by the sessions module at any point during the lifetime of the
11520305002Scy** iterator. If such an xInput callback returns an error, the iterator enters
11521366076Scy** an error state, whereby all subsequent calls to iterator functions
11522305002Scy** immediately fail with the same error code as returned by xInput.
11523305002Scy**
11524305002Scy** Similarly, streaming API functions that return changesets (or patchsets)
11525305002Scy** return them in chunks by way of a callback function instead of via a
11526305002Scy** pointer to a single large buffer. In this case, a pair of parameters such
11527305002Scy** as:
11528305002Scy**
11529305002Scy**  <pre>
11530305002Scy**  &nbsp;     int *pnChangeset,
11531305002Scy**  &nbsp;     void **ppChangeset,
11532305002Scy**  </pre>
11533305002Scy**
11534305002Scy** Is replaced by:
11535305002Scy**
11536305002Scy**  <pre>
11537305002Scy**  &nbsp;     int (*xOutput)(void *pOut, const void *pData, int nData),
11538305002Scy**  &nbsp;     void *pOut
11539305002Scy**  </pre>
11540305002Scy**
11541305002Scy** The xOutput callback is invoked zero or more times to return data to
11542305002Scy** the application. The first parameter passed to each call is a copy of the
11543305002Scy** pOut pointer supplied by the application. The second parameter, pData,
11544305002Scy** points to a buffer nData bytes in size containing the chunk of output
11545305002Scy** data being returned. If the xOutput callback successfully processes the
11546305002Scy** supplied data, it should return SQLITE_OK to indicate success. Otherwise,
11547305002Scy** it should return some other SQLite error code. In this case processing
11548305002Scy** is immediately abandoned and the streaming API function returns a copy
11549305002Scy** of the xOutput error code to the application.
11550305002Scy**
11551366076Scy** The sessions module never invokes an xOutput callback with the third
11552305002Scy** parameter set to a value less than or equal to zero. Other than this,
11553305002Scy** no guarantees are made as to the size of the chunks of data returned.
11554305002Scy*/
11555322444SpeterSQLITE_API int sqlite3changeset_apply_strm(
11556305002Scy  sqlite3 *db,                    /* Apply change to "main" db of this handle */
11557305002Scy  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
11558305002Scy  void *pIn,                                          /* First arg for xInput */
11559305002Scy  int(*xFilter)(
11560305002Scy    void *pCtx,                   /* Copy of sixth arg to _apply() */
11561305002Scy    const char *zTab              /* Table name */
11562305002Scy  ),
11563305002Scy  int(*xConflict)(
11564305002Scy    void *pCtx,                   /* Copy of sixth arg to _apply() */
11565305002Scy    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
11566305002Scy    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
11567305002Scy  ),
11568305002Scy  void *pCtx                      /* First argument passed to xConflict */
11569305002Scy);
11570342292ScySQLITE_API int sqlite3changeset_apply_v2_strm(
11571342292Scy  sqlite3 *db,                    /* Apply change to "main" db of this handle */
11572342292Scy  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
11573342292Scy  void *pIn,                                          /* First arg for xInput */
11574342292Scy  int(*xFilter)(
11575342292Scy    void *pCtx,                   /* Copy of sixth arg to _apply() */
11576342292Scy    const char *zTab              /* Table name */
11577342292Scy  ),
11578342292Scy  int(*xConflict)(
11579342292Scy    void *pCtx,                   /* Copy of sixth arg to _apply() */
11580342292Scy    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
11581342292Scy    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
11582342292Scy  ),
11583342292Scy  void *pCtx,                     /* First argument passed to xConflict */
11584342292Scy  void **ppRebase, int *pnRebase,
11585342292Scy  int flags
11586342292Scy);
11587322444SpeterSQLITE_API int sqlite3changeset_concat_strm(
11588305002Scy  int (*xInputA)(void *pIn, void *pData, int *pnData),
11589305002Scy  void *pInA,
11590305002Scy  int (*xInputB)(void *pIn, void *pData, int *pnData),
11591305002Scy  void *pInB,
11592305002Scy  int (*xOutput)(void *pOut, const void *pData, int nData),
11593305002Scy  void *pOut
11594305002Scy);
11595322444SpeterSQLITE_API int sqlite3changeset_invert_strm(
11596305002Scy  int (*xInput)(void *pIn, void *pData, int *pnData),
11597305002Scy  void *pIn,
11598305002Scy  int (*xOutput)(void *pOut, const void *pData, int nData),
11599305002Scy  void *pOut
11600305002Scy);
11601322444SpeterSQLITE_API int sqlite3changeset_start_strm(
11602305002Scy  sqlite3_changeset_iter **pp,
11603305002Scy  int (*xInput)(void *pIn, void *pData, int *pnData),
11604305002Scy  void *pIn
11605305002Scy);
11606342292ScySQLITE_API int sqlite3changeset_start_v2_strm(
11607342292Scy  sqlite3_changeset_iter **pp,
11608342292Scy  int (*xInput)(void *pIn, void *pData, int *pnData),
11609342292Scy  void *pIn,
11610342292Scy  int flags
11611342292Scy);
11612322444SpeterSQLITE_API int sqlite3session_changeset_strm(
11613305002Scy  sqlite3_session *pSession,
11614305002Scy  int (*xOutput)(void *pOut, const void *pData, int nData),
11615305002Scy  void *pOut
11616305002Scy);
11617322444SpeterSQLITE_API int sqlite3session_patchset_strm(
11618305002Scy  sqlite3_session *pSession,
11619305002Scy  int (*xOutput)(void *pOut, const void *pData, int nData),
11620305002Scy  void *pOut
11621305002Scy);
11622366076ScySQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*,
11623305002Scy    int (*xInput)(void *pIn, void *pData, int *pnData),
11624305002Scy    void *pIn
11625305002Scy);
11626322444SpeterSQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
11627366076Scy    int (*xOutput)(void *pOut, const void *pData, int nData),
11628305002Scy    void *pOut
11629305002Scy);
11630342292ScySQLITE_API int sqlite3rebaser_rebase_strm(
11631342292Scy  sqlite3_rebaser *pRebaser,
11632342292Scy  int (*xInput)(void *pIn, void *pData, int *pnData),
11633342292Scy  void *pIn,
11634342292Scy  int (*xOutput)(void *pOut, const void *pData, int nData),
11635342292Scy  void *pOut
11636342292Scy);
11637305002Scy
11638342292Scy/*
11639342292Scy** CAPI3REF: Configure global parameters
11640342292Scy**
11641342292Scy** The sqlite3session_config() interface is used to make global configuration
11642366076Scy** changes to the sessions module in order to tune it to the specific needs
11643342292Scy** of the application.
11644342292Scy**
11645342292Scy** The sqlite3session_config() interface is not threadsafe. If it is invoked
11646342292Scy** while any other thread is inside any other sessions method then the
11647342292Scy** results are undefined. Furthermore, if it is invoked after any sessions
11648366076Scy** related objects have been created, the results are also undefined.
11649342292Scy**
11650342292Scy** The first argument to the sqlite3session_config() function must be one
11651366076Scy** of the SQLITE_SESSION_CONFIG_XXX constants defined below. The
11652342292Scy** interpretation of the (void*) value passed as the second parameter and
11653342292Scy** the effect of calling this function depends on the value of the first
11654342292Scy** parameter.
11655342292Scy**
11656342292Scy** <dl>
11657342292Scy** <dt>SQLITE_SESSION_CONFIG_STRMSIZE<dd>
11658342292Scy**    By default, the sessions module streaming interfaces attempt to input
11659342292Scy**    and output data in approximately 1 KiB chunks. This operand may be used
11660342292Scy**    to set and query the value of this configuration setting. The pointer
11661342292Scy**    passed as the second argument must point to a value of type (int).
11662342292Scy**    If this value is greater than 0, it is used as the new streaming data
11663342292Scy**    chunk size for both input and output. Before returning, the (int) value
11664342292Scy**    pointed to by pArg is set to the final value of the streaming interface
11665342292Scy**    chunk size.
11666342292Scy** </dl>
11667342292Scy**
11668342292Scy** This function returns SQLITE_OK if successful, or an SQLite error code
11669342292Scy** otherwise.
11670342292Scy*/
11671342292ScySQLITE_API int sqlite3session_config(int op, void *pArg);
11672305002Scy
11673305002Scy/*
11674342292Scy** CAPI3REF: Values for sqlite3session_config().
11675342292Scy*/
11676342292Scy#define SQLITE_SESSION_CONFIG_STRMSIZE 1
11677342292Scy
11678342292Scy/*
11679305002Scy** Make sure we can call this stuff from C++.
11680305002Scy*/
11681305002Scy#ifdef __cplusplus
11682305002Scy}
11683305002Scy#endif
11684305002Scy
11685305002Scy#endif  /* !defined(__SQLITESESSION_H_) && defined(SQLITE_ENABLE_SESSION) */
11686305002Scy
11687305002Scy/******** End of sqlite3session.h *********/
11688305002Scy/******** Begin file fts5.h *********/
11689305002Scy/*
11690298161Sbapt** 2014 May 31
11691298161Sbapt**
11692298161Sbapt** The author disclaims copyright to this source code.  In place of
11693298161Sbapt** a legal notice, here is a blessing:
11694298161Sbapt**
11695298161Sbapt**    May you do good and not evil.
11696298161Sbapt**    May you find forgiveness for yourself and forgive others.
11697298161Sbapt**    May you share freely, never taking more than you give.
11698298161Sbapt**
11699298161Sbapt******************************************************************************
11700298161Sbapt**
11701366076Scy** Interfaces to extend FTS5. Using the interfaces defined in this file,
11702298161Sbapt** FTS5 may be extended with:
11703298161Sbapt**
11704298161Sbapt**     * custom tokenizers, and
11705298161Sbapt**     * custom auxiliary functions.
11706298161Sbapt*/
11707298161Sbapt
11708298161Sbapt
11709298161Sbapt#ifndef _FTS5_H
11710298161Sbapt#define _FTS5_H
11711298161Sbapt
11712298161Sbapt
11713298161Sbapt#ifdef __cplusplus
11714298161Sbaptextern "C" {
11715298161Sbapt#endif
11716298161Sbapt
11717298161Sbapt/*************************************************************************
11718298161Sbapt** CUSTOM AUXILIARY FUNCTIONS
11719298161Sbapt**
11720298161Sbapt** Virtual table implementations may overload SQL functions by implementing
11721298161Sbapt** the sqlite3_module.xFindFunction() method.
11722298161Sbapt*/
11723298161Sbapt
11724298161Sbapttypedef struct Fts5ExtensionApi Fts5ExtensionApi;
11725298161Sbapttypedef struct Fts5Context Fts5Context;
11726298161Sbapttypedef struct Fts5PhraseIter Fts5PhraseIter;
11727298161Sbapt
11728298161Sbapttypedef void (*fts5_extension_function)(
11729298161Sbapt  const Fts5ExtensionApi *pApi,   /* API offered by current FTS version */
11730298161Sbapt  Fts5Context *pFts,              /* First arg to pass to pApi functions */
11731298161Sbapt  sqlite3_context *pCtx,          /* Context for returning result/error */
11732298161Sbapt  int nVal,                       /* Number of values in apVal[] array */
11733298161Sbapt  sqlite3_value **apVal           /* Array of trailing arguments */
11734298161Sbapt);
11735298161Sbapt
11736298161Sbaptstruct Fts5PhraseIter {
11737298161Sbapt  const unsigned char *a;
11738298161Sbapt  const unsigned char *b;
11739298161Sbapt};
11740298161Sbapt
11741298161Sbapt/*
11742298161Sbapt** EXTENSION API FUNCTIONS
11743298161Sbapt**
11744298161Sbapt** xUserData(pFts):
11745366076Scy**   Return a copy of the context pointer the extension function was
11746298161Sbapt**   registered with.
11747298161Sbapt**
11748298161Sbapt** xColumnTotalSize(pFts, iCol, pnToken):
11749298161Sbapt**   If parameter iCol is less than zero, set output variable *pnToken
11750298161Sbapt**   to the total number of tokens in the FTS5 table. Or, if iCol is
11751298161Sbapt**   non-negative but less than the number of columns in the table, return
11752366076Scy**   the total number of tokens in column iCol, considering all rows in
11753298161Sbapt**   the FTS5 table.
11754298161Sbapt**
11755298161Sbapt**   If parameter iCol is greater than or equal to the number of columns
11756298161Sbapt**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
11757366076Scy**   an OOM condition or IO error), an appropriate SQLite error code is
11758298161Sbapt**   returned.
11759298161Sbapt**
11760298161Sbapt** xColumnCount(pFts):
11761298161Sbapt**   Return the number of columns in the table.
11762298161Sbapt**
11763298161Sbapt** xColumnSize(pFts, iCol, pnToken):
11764298161Sbapt**   If parameter iCol is less than zero, set output variable *pnToken
11765298161Sbapt**   to the total number of tokens in the current row. Or, if iCol is
11766298161Sbapt**   non-negative but less than the number of columns in the table, set
11767298161Sbapt**   *pnToken to the number of tokens in column iCol of the current row.
11768298161Sbapt**
11769298161Sbapt**   If parameter iCol is greater than or equal to the number of columns
11770298161Sbapt**   in the table, SQLITE_RANGE is returned. Or, if an error occurs (e.g.
11771366076Scy**   an OOM condition or IO error), an appropriate SQLite error code is
11772298161Sbapt**   returned.
11773298161Sbapt**
11774298161Sbapt**   This function may be quite inefficient if used with an FTS5 table
11775298161Sbapt**   created with the "columnsize=0" option.
11776298161Sbapt**
11777298161Sbapt** xColumnText:
11778298161Sbapt**   This function attempts to retrieve the text of column iCol of the
11779298161Sbapt**   current document. If successful, (*pz) is set to point to a buffer
11780298161Sbapt**   containing the text in utf-8 encoding, (*pn) is set to the size in bytes
11781298161Sbapt**   (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
11782298161Sbapt**   if an error occurs, an SQLite error code is returned and the final values
11783298161Sbapt**   of (*pz) and (*pn) are undefined.
11784298161Sbapt**
11785298161Sbapt** xPhraseCount:
11786298161Sbapt**   Returns the number of phrases in the current query expression.
11787298161Sbapt**
11788298161Sbapt** xPhraseSize:
11789298161Sbapt**   Returns the number of tokens in phrase iPhrase of the query. Phrases
11790298161Sbapt**   are numbered starting from zero.
11791298161Sbapt**
11792298161Sbapt** xInstCount:
11793298161Sbapt**   Set *pnInst to the total number of occurrences of all phrases within
11794298161Sbapt**   the query within the current row. Return SQLITE_OK if successful, or
11795298161Sbapt**   an error code (i.e. SQLITE_NOMEM) if an error occurs.
11796298161Sbapt**
11797298161Sbapt**   This API can be quite slow if used with an FTS5 table created with the
11798366076Scy**   "detail=none" or "detail=column" option. If the FTS5 table is created
11799366076Scy**   with either "detail=none" or "detail=column" and "content=" option
11800298161Sbapt**   (i.e. if it is a contentless table), then this API always returns 0.
11801298161Sbapt**
11802298161Sbapt** xInst:
11803298161Sbapt**   Query for the details of phrase match iIdx within the current row.
11804298161Sbapt**   Phrase matches are numbered starting from zero, so the iIdx argument
11805298161Sbapt**   should be greater than or equal to zero and smaller than the value
11806298161Sbapt**   output by xInstCount().
11807298161Sbapt**
11808298161Sbapt**   Usually, output parameter *piPhrase is set to the phrase number, *piCol
11809298161Sbapt**   to the column in which it occurs and *piOff the token offset of the
11810346442Scy**   first token of the phrase. Returns SQLITE_OK if successful, or an error
11811346442Scy**   code (i.e. SQLITE_NOMEM) if an error occurs.
11812298161Sbapt**
11813298161Sbapt**   This API can be quite slow if used with an FTS5 table created with the
11814366076Scy**   "detail=none" or "detail=column" option.
11815298161Sbapt**
11816298161Sbapt** xRowid:
11817298161Sbapt**   Returns the rowid of the current row.
11818298161Sbapt**
11819298161Sbapt** xTokenize:
11820298161Sbapt**   Tokenize text using the tokenizer belonging to the FTS5 table.
11821298161Sbapt**
11822298161Sbapt** xQueryPhrase(pFts5, iPhrase, pUserData, xCallback):
11823298161Sbapt**   This API function is used to query the FTS table for phrase iPhrase
11824298161Sbapt**   of the current query. Specifically, a query equivalent to:
11825298161Sbapt**
11826298161Sbapt**       ... FROM ftstable WHERE ftstable MATCH $p ORDER BY rowid
11827298161Sbapt**
11828298161Sbapt**   with $p set to a phrase equivalent to the phrase iPhrase of the
11829305002Scy**   current query is executed. Any column filter that applies to
11830366076Scy**   phrase iPhrase of the current query is included in $p. For each
11831366076Scy**   row visited, the callback function passed as the fourth argument
11832366076Scy**   is invoked. The context and API objects passed to the callback
11833305002Scy**   function may be used to access the properties of each matched row.
11834366076Scy**   Invoking Api.xUserData() returns a copy of the pointer passed as
11835305002Scy**   the third argument to pUserData.
11836298161Sbapt**
11837298161Sbapt**   If the callback function returns any value other than SQLITE_OK, the
11838298161Sbapt**   query is abandoned and the xQueryPhrase function returns immediately.
11839298161Sbapt**   If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
11840298161Sbapt**   Otherwise, the error code is propagated upwards.
11841298161Sbapt**
11842298161Sbapt**   If the query runs to completion without incident, SQLITE_OK is returned.
11843298161Sbapt**   Or, if some error occurs before the query completes or is aborted by
11844298161Sbapt**   the callback, an SQLite error code is returned.
11845298161Sbapt**
11846298161Sbapt**
11847298161Sbapt** xSetAuxdata(pFts5, pAux, xDelete)
11848298161Sbapt**
11849366076Scy**   Save the pointer passed as the second argument as the extension function's
11850298161Sbapt**   "auxiliary data". The pointer may then be retrieved by the current or any
11851298161Sbapt**   future invocation of the same fts5 extension function made as part of
11852347347Scy**   the same MATCH query using the xGetAuxdata() API.
11853298161Sbapt**
11854298161Sbapt**   Each extension function is allocated a single auxiliary data slot for
11855366076Scy**   each FTS query (MATCH expression). If the extension function is invoked
11856366076Scy**   more than once for a single FTS query, then all invocations share a
11857298161Sbapt**   single auxiliary data context.
11858298161Sbapt**
11859298161Sbapt**   If there is already an auxiliary data pointer when this function is
11860298161Sbapt**   invoked, then it is replaced by the new pointer. If an xDelete callback
11861298161Sbapt**   was specified along with the original pointer, it is invoked at this
11862298161Sbapt**   point.
11863298161Sbapt**
11864298161Sbapt**   The xDelete callback, if one is specified, is also invoked on the
11865298161Sbapt**   auxiliary data pointer after the FTS5 query has finished.
11866298161Sbapt**
11867347347Scy**   If an error (e.g. an OOM condition) occurs within this function,
11868298161Sbapt**   the auxiliary data is set to NULL and an error code returned. If the
11869298161Sbapt**   xDelete parameter was not NULL, it is invoked on the auxiliary data
11870298161Sbapt**   pointer before returning.
11871298161Sbapt**
11872298161Sbapt**
11873298161Sbapt** xGetAuxdata(pFts5, bClear)
11874298161Sbapt**
11875366076Scy**   Returns the current auxiliary data pointer for the fts5 extension
11876298161Sbapt**   function. See the xSetAuxdata() method for details.
11877298161Sbapt**
11878298161Sbapt**   If the bClear argument is non-zero, then the auxiliary data is cleared
11879298161Sbapt**   (set to NULL) before this function returns. In this case the xDelete,
11880298161Sbapt**   if any, is not invoked.
11881298161Sbapt**
11882298161Sbapt**
11883298161Sbapt** xRowCount(pFts5, pnRow)
11884298161Sbapt**
11885298161Sbapt**   This function is used to retrieve the total number of rows in the table.
11886298161Sbapt**   In other words, the same value that would be returned by:
11887298161Sbapt**
11888298161Sbapt**        SELECT count(*) FROM ftstable;
11889298161Sbapt**
11890298161Sbapt** xPhraseFirst()
11891298161Sbapt**   This function is used, along with type Fts5PhraseIter and the xPhraseNext
11892298161Sbapt**   method, to iterate through all instances of a single query phrase within
11893298161Sbapt**   the current row. This is the same information as is accessible via the
11894298161Sbapt**   xInstCount/xInst APIs. While the xInstCount/xInst APIs are more convenient
11895366076Scy**   to use, this API may be faster under some circumstances. To iterate
11896298161Sbapt**   through instances of phrase iPhrase, use the following code:
11897298161Sbapt**
11898298161Sbapt**       Fts5PhraseIter iter;
11899298161Sbapt**       int iCol, iOff;
11900298161Sbapt**       for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
11901298161Sbapt**           iCol>=0;
11902298161Sbapt**           pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
11903298161Sbapt**       ){
11904298161Sbapt**         // An instance of phrase iPhrase at offset iOff of column iCol
11905298161Sbapt**       }
11906298161Sbapt**
11907298161Sbapt**   The Fts5PhraseIter structure is defined above. Applications should not
11908298161Sbapt**   modify this structure directly - it should only be used as shown above
11909298161Sbapt**   with the xPhraseFirst() and xPhraseNext() API methods (and by
11910298161Sbapt**   xPhraseFirstColumn() and xPhraseNextColumn() as illustrated below).
11911298161Sbapt**
11912298161Sbapt**   This API can be quite slow if used with an FTS5 table created with the
11913366076Scy**   "detail=none" or "detail=column" option. If the FTS5 table is created
11914366076Scy**   with either "detail=none" or "detail=column" and "content=" option
11915298161Sbapt**   (i.e. if it is a contentless table), then this API always iterates
11916298161Sbapt**   through an empty set (all calls to xPhraseFirst() set iCol to -1).
11917298161Sbapt**
11918298161Sbapt** xPhraseNext()
11919298161Sbapt**   See xPhraseFirst above.
11920298161Sbapt**
11921298161Sbapt** xPhraseFirstColumn()
11922298161Sbapt**   This function and xPhraseNextColumn() are similar to the xPhraseFirst()
11923298161Sbapt**   and xPhraseNext() APIs described above. The difference is that instead
11924298161Sbapt**   of iterating through all instances of a phrase in the current row, these
11925298161Sbapt**   APIs are used to iterate through the set of columns in the current row
11926298161Sbapt**   that contain one or more instances of a specified phrase. For example:
11927298161Sbapt**
11928298161Sbapt**       Fts5PhraseIter iter;
11929298161Sbapt**       int iCol;
11930298161Sbapt**       for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
11931298161Sbapt**           iCol>=0;
11932298161Sbapt**           pApi->xPhraseNextColumn(pFts, &iter, &iCol)
11933298161Sbapt**       ){
11934298161Sbapt**         // Column iCol contains at least one instance of phrase iPhrase
11935298161Sbapt**       }
11936298161Sbapt**
11937298161Sbapt**   This API can be quite slow if used with an FTS5 table created with the
11938366076Scy**   "detail=none" option. If the FTS5 table is created with either
11939366076Scy**   "detail=none" "content=" option (i.e. if it is a contentless table),
11940366076Scy**   then this API always iterates through an empty set (all calls to
11941298161Sbapt**   xPhraseFirstColumn() set iCol to -1).
11942298161Sbapt**
11943298161Sbapt**   The information accessed using this API and its companion
11944298161Sbapt**   xPhraseFirstColumn() may also be obtained using xPhraseFirst/xPhraseNext
11945298161Sbapt**   (or xInst/xInstCount). The chief advantage of this API is that it is
11946298161Sbapt**   significantly more efficient than those alternatives when used with
11947366076Scy**   "detail=column" tables.
11948298161Sbapt**
11949298161Sbapt** xPhraseNextColumn()
11950298161Sbapt**   See xPhraseFirstColumn above.
11951298161Sbapt*/
11952298161Sbaptstruct Fts5ExtensionApi {
11953298161Sbapt  int iVersion;                   /* Currently always set to 3 */
11954298161Sbapt
11955298161Sbapt  void *(*xUserData)(Fts5Context*);
11956298161Sbapt
11957298161Sbapt  int (*xColumnCount)(Fts5Context*);
11958298161Sbapt  int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
11959298161Sbapt  int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
11960298161Sbapt
11961366076Scy  int (*xTokenize)(Fts5Context*,
11962298161Sbapt    const char *pText, int nText, /* Text to tokenize */
11963298161Sbapt    void *pCtx,                   /* Context passed to xToken() */
11964298161Sbapt    int (*xToken)(void*, int, const char*, int, int, int)       /* Callback */
11965298161Sbapt  );
11966298161Sbapt
11967298161Sbapt  int (*xPhraseCount)(Fts5Context*);
11968298161Sbapt  int (*xPhraseSize)(Fts5Context*, int iPhrase);
11969298161Sbapt
11970298161Sbapt  int (*xInstCount)(Fts5Context*, int *pnInst);
11971298161Sbapt  int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
11972298161Sbapt
11973298161Sbapt  sqlite3_int64 (*xRowid)(Fts5Context*);
11974298161Sbapt  int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
11975298161Sbapt  int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
11976298161Sbapt
11977298161Sbapt  int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
11978298161Sbapt    int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
11979298161Sbapt  );
11980298161Sbapt  int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
11981298161Sbapt  void *(*xGetAuxdata)(Fts5Context*, int bClear);
11982298161Sbapt
11983298161Sbapt  int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
11984298161Sbapt  void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
11985298161Sbapt
11986298161Sbapt  int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
11987298161Sbapt  void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
11988298161Sbapt};
11989298161Sbapt
11990366076Scy/*
11991298161Sbapt** CUSTOM AUXILIARY FUNCTIONS
11992298161Sbapt*************************************************************************/
11993298161Sbapt
11994298161Sbapt/*************************************************************************
11995298161Sbapt** CUSTOM TOKENIZERS
11996298161Sbapt**
11997366076Scy** Applications may also register custom tokenizer types. A tokenizer
11998366076Scy** is registered by providing fts5 with a populated instance of the
11999298161Sbapt** following structure. All structure methods must be defined, setting
12000298161Sbapt** any member of the fts5_tokenizer struct to NULL leads to undefined
12001298161Sbapt** behaviour. The structure methods are expected to function as follows:
12002298161Sbapt**
12003298161Sbapt** xCreate:
12004305002Scy**   This function is used to allocate and initialize a tokenizer instance.
12005298161Sbapt**   A tokenizer instance is required to actually tokenize text.
12006298161Sbapt**
12007298161Sbapt**   The first argument passed to this function is a copy of the (void*)
12008298161Sbapt**   pointer provided by the application when the fts5_tokenizer object
12009366076Scy**   was registered with FTS5 (the third argument to xCreateTokenizer()).
12010298161Sbapt**   The second and third arguments are an array of nul-terminated strings
12011298161Sbapt**   containing the tokenizer arguments, if any, specified following the
12012298161Sbapt**   tokenizer name as part of the CREATE VIRTUAL TABLE statement used
12013298161Sbapt**   to create the FTS5 table.
12014298161Sbapt**
12015366076Scy**   The final argument is an output variable. If successful, (*ppOut)
12016298161Sbapt**   should be set to point to the new tokenizer handle and SQLITE_OK
12017298161Sbapt**   returned. If an error occurs, some value other than SQLITE_OK should
12018366076Scy**   be returned. In this case, fts5 assumes that the final value of *ppOut
12019298161Sbapt**   is undefined.
12020298161Sbapt**
12021298161Sbapt** xDelete:
12022298161Sbapt**   This function is invoked to delete a tokenizer handle previously
12023298161Sbapt**   allocated using xCreate(). Fts5 guarantees that this function will
12024298161Sbapt**   be invoked exactly once for each successful call to xCreate().
12025298161Sbapt**
12026298161Sbapt** xTokenize:
12027366076Scy**   This function is expected to tokenize the nText byte string indicated
12028298161Sbapt**   by argument pText. pText may or may not be nul-terminated. The first
12029298161Sbapt**   argument passed to this function is a pointer to an Fts5Tokenizer object
12030298161Sbapt**   returned by an earlier call to xCreate().
12031298161Sbapt**
12032298161Sbapt**   The second argument indicates the reason that FTS5 is requesting
12033298161Sbapt**   tokenization of the supplied text. This is always one of the following
12034298161Sbapt**   four values:
12035298161Sbapt**
12036298161Sbapt**   <ul><li> <b>FTS5_TOKENIZE_DOCUMENT</b> - A document is being inserted into
12037298161Sbapt**            or removed from the FTS table. The tokenizer is being invoked to
12038298161Sbapt**            determine the set of tokens to add to (or delete from) the
12039298161Sbapt**            FTS index.
12040298161Sbapt**
12041366076Scy**       <li> <b>FTS5_TOKENIZE_QUERY</b> - A MATCH query is being executed
12042366076Scy**            against the FTS index. The tokenizer is being called to tokenize
12043298161Sbapt**            a bareword or quoted string specified as part of the query.
12044298161Sbapt**
12045298161Sbapt**       <li> <b>(FTS5_TOKENIZE_QUERY | FTS5_TOKENIZE_PREFIX)</b> - Same as
12046298161Sbapt**            FTS5_TOKENIZE_QUERY, except that the bareword or quoted string is
12047298161Sbapt**            followed by a "*" character, indicating that the last token
12048298161Sbapt**            returned by the tokenizer will be treated as a token prefix.
12049298161Sbapt**
12050366076Scy**       <li> <b>FTS5_TOKENIZE_AUX</b> - The tokenizer is being invoked to
12051298161Sbapt**            satisfy an fts5_api.xTokenize() request made by an auxiliary
12052298161Sbapt**            function. Or an fts5_api.xColumnSize() request made by the same
12053366076Scy**            on a columnsize=0 database.
12054298161Sbapt**   </ul>
12055298161Sbapt**
12056298161Sbapt**   For each token in the input string, the supplied callback xToken() must
12057298161Sbapt**   be invoked. The first argument to it should be a copy of the pointer
12058298161Sbapt**   passed as the second argument to xTokenize(). The third and fourth
12059298161Sbapt**   arguments are a pointer to a buffer containing the token text, and the
12060298161Sbapt**   size of the token in bytes. The 4th and 5th arguments are the byte offsets
12061298161Sbapt**   of the first byte of and first byte immediately following the text from
12062298161Sbapt**   which the token is derived within the input.
12063298161Sbapt**
12064298161Sbapt**   The second argument passed to the xToken() callback ("tflags") should
12065366076Scy**   normally be set to 0. The exception is if the tokenizer supports
12066298161Sbapt**   synonyms. In this case see the discussion below for details.
12067298161Sbapt**
12068366076Scy**   FTS5 assumes the xToken() callback is invoked for each token in the
12069298161Sbapt**   order that they occur within the input text.
12070298161Sbapt**
12071298161Sbapt**   If an xToken() callback returns any value other than SQLITE_OK, then
12072298161Sbapt**   the tokenization should be abandoned and the xTokenize() method should
12073298161Sbapt**   immediately return a copy of the xToken() return value. Or, if the
12074298161Sbapt**   input buffer is exhausted, xTokenize() should return SQLITE_OK. Finally,
12075298161Sbapt**   if an error occurs with the xTokenize() implementation itself, it
12076298161Sbapt**   may abandon the tokenization and return any error code other than
12077298161Sbapt**   SQLITE_OK or SQLITE_DONE.
12078298161Sbapt**
12079298161Sbapt** SYNONYM SUPPORT
12080298161Sbapt**
12081298161Sbapt**   Custom tokenizers may also support synonyms. Consider a case in which a
12082366076Scy**   user wishes to query for a phrase such as "first place". Using the
12083298161Sbapt**   built-in tokenizers, the FTS5 query 'first + place' will match instances
12084298161Sbapt**   of "first place" within the document set, but not alternative forms
12085298161Sbapt**   such as "1st place". In some applications, it would be better to match
12086298161Sbapt**   all instances of "first place" or "1st place" regardless of which form
12087298161Sbapt**   the user specified in the MATCH query text.
12088298161Sbapt**
12089298161Sbapt**   There are several ways to approach this in FTS5:
12090298161Sbapt**
12091361456Scy**   <ol><li> By mapping all synonyms to a single token. In this case, using
12092361456Scy**            the above example, this means that the tokenizer returns the
12093298161Sbapt**            same token for inputs "first" and "1st". Say that token is in
12094298161Sbapt**            fact "first", so that when the user inserts the document "I won
12095298161Sbapt**            1st place" entries are added to the index for tokens "i", "won",
12096298161Sbapt**            "first" and "place". If the user then queries for '1st + place',
12097298161Sbapt**            the tokenizer substitutes "first" for "1st" and the query works
12098298161Sbapt**            as expected.
12099298161Sbapt**
12100346442Scy**       <li> By querying the index for all synonyms of each query term
12101346442Scy**            separately. In this case, when tokenizing query text, the
12102366076Scy**            tokenizer may provide multiple synonyms for a single term
12103366076Scy**            within the document. FTS5 then queries the index for each
12104346442Scy**            synonym individually. For example, faced with the query:
12105298161Sbapt**
12106298161Sbapt**   <codeblock>
12107298161Sbapt**     ... MATCH 'first place'</codeblock>
12108298161Sbapt**
12109298161Sbapt**            the tokenizer offers both "1st" and "first" as synonyms for the
12110366076Scy**            first token in the MATCH query and FTS5 effectively runs a query
12111298161Sbapt**            similar to:
12112298161Sbapt**
12113298161Sbapt**   <codeblock>
12114298161Sbapt**     ... MATCH '(first OR 1st) place'</codeblock>
12115298161Sbapt**
12116298161Sbapt**            except that, for the purposes of auxiliary functions, the query
12117366076Scy**            still appears to contain just two phrases - "(first OR 1st)"
12118298161Sbapt**            being treated as a single phrase.
12119298161Sbapt**
12120298161Sbapt**       <li> By adding multiple synonyms for a single term to the FTS index.
12121298161Sbapt**            Using this method, when tokenizing document text, the tokenizer
12122366076Scy**            provides multiple synonyms for each token. So that when a
12123298161Sbapt**            document such as "I won first place" is tokenized, entries are
12124298161Sbapt**            added to the FTS index for "i", "won", "first", "1st" and
12125298161Sbapt**            "place".
12126298161Sbapt**
12127298161Sbapt**            This way, even if the tokenizer does not provide synonyms
12128346442Scy**            when tokenizing query text (it should not - to do so would be
12129366076Scy**            inefficient), it doesn't matter if the user queries for
12130342292Scy**            'first + place' or '1st + place', as there are entries in the
12131298161Sbapt**            FTS index corresponding to both forms of the first token.
12132298161Sbapt**   </ol>
12133298161Sbapt**
12134298161Sbapt**   Whether it is parsing document or query text, any call to xToken that
12135298161Sbapt**   specifies a <i>tflags</i> argument with the FTS5_TOKEN_COLOCATED bit
12136298161Sbapt**   is considered to supply a synonym for the previous token. For example,
12137298161Sbapt**   when parsing the document "I won first place", a tokenizer that supports
12138298161Sbapt**   synonyms would call xToken() 5 times, as follows:
12139298161Sbapt**
12140298161Sbapt**   <codeblock>
12141298161Sbapt**       xToken(pCtx, 0, "i",                      1,  0,  1);
12142298161Sbapt**       xToken(pCtx, 0, "won",                    3,  2,  5);
12143298161Sbapt**       xToken(pCtx, 0, "first",                  5,  6, 11);
12144298161Sbapt**       xToken(pCtx, FTS5_TOKEN_COLOCATED, "1st", 3,  6, 11);
12145298161Sbapt**       xToken(pCtx, 0, "place",                  5, 12, 17);
12146298161Sbapt**</codeblock>
12147298161Sbapt**
12148298161Sbapt**   It is an error to specify the FTS5_TOKEN_COLOCATED flag the first time
12149298161Sbapt**   xToken() is called. Multiple synonyms may be specified for a single token
12150366076Scy**   by making multiple calls to xToken(FTS5_TOKEN_COLOCATED) in sequence.
12151298161Sbapt**   There is no limit to the number of synonyms that may be provided for a
12152298161Sbapt**   single token.
12153298161Sbapt**
12154366076Scy**   In many cases, method (1) above is the best approach. It does not add
12155298161Sbapt**   extra data to the FTS index or require FTS5 to query for multiple terms,
12156298161Sbapt**   so it is efficient in terms of disk space and query speed. However, it
12157298161Sbapt**   does not support prefix queries very well. If, as suggested above, the
12158342292Scy**   token "first" is substituted for "1st" by the tokenizer, then the query:
12159298161Sbapt**
12160298161Sbapt**   <codeblock>
12161298161Sbapt**     ... MATCH '1s*'</codeblock>
12162298161Sbapt**
12163298161Sbapt**   will not match documents that contain the token "1st" (as the tokenizer
12164298161Sbapt**   will probably not map "1s" to any prefix of "first").
12165298161Sbapt**
12166366076Scy**   For full prefix support, method (3) may be preferred. In this case,
12167298161Sbapt**   because the index contains entries for both "first" and "1st", prefix
12168298161Sbapt**   queries such as 'fi*' or '1s*' will match correctly. However, because
12169298161Sbapt**   extra entries are added to the FTS index, this method uses more space
12170298161Sbapt**   within the database.
12171298161Sbapt**
12172298161Sbapt**   Method (2) offers a midpoint between (1) and (3). Using this method,
12173366076Scy**   a query such as '1s*' will match documents that contain the literal
12174298161Sbapt**   token "1st", but not "first" (assuming the tokenizer is not able to
12175298161Sbapt**   provide synonyms for prefixes). However, a non-prefix query like '1st'
12176298161Sbapt**   will match against "1st" and "first". This method does not require
12177366076Scy**   extra disk space, as no extra entries are added to the FTS index.
12178298161Sbapt**   On the other hand, it may require more CPU cycles to run MATCH queries,
12179298161Sbapt**   as separate queries of the FTS index are required for each synonym.
12180298161Sbapt**
12181298161Sbapt**   When using methods (2) or (3), it is important that the tokenizer only
12182298161Sbapt**   provide synonyms when tokenizing document text (method (2)) or query
12183298161Sbapt**   text (method (3)), not both. Doing so will not cause any errors, but is
12184298161Sbapt**   inefficient.
12185298161Sbapt*/
12186298161Sbapttypedef struct Fts5Tokenizer Fts5Tokenizer;
12187298161Sbapttypedef struct fts5_tokenizer fts5_tokenizer;
12188298161Sbaptstruct fts5_tokenizer {
12189298161Sbapt  int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
12190298161Sbapt  void (*xDelete)(Fts5Tokenizer*);
12191366076Scy  int (*xTokenize)(Fts5Tokenizer*,
12192298161Sbapt      void *pCtx,
12193298161Sbapt      int flags,            /* Mask of FTS5_TOKENIZE_* flags */
12194366076Scy      const char *pText, int nText,
12195298161Sbapt      int (*xToken)(
12196298161Sbapt        void *pCtx,         /* Copy of 2nd argument to xTokenize() */
12197298161Sbapt        int tflags,         /* Mask of FTS5_TOKEN_* flags */
12198298161Sbapt        const char *pToken, /* Pointer to buffer containing token */
12199298161Sbapt        int nToken,         /* Size of token in bytes */
12200298161Sbapt        int iStart,         /* Byte offset of token within input text */
12201298161Sbapt        int iEnd            /* Byte offset of end of token within input text */
12202298161Sbapt      )
12203298161Sbapt  );
12204298161Sbapt};
12205298161Sbapt
12206298161Sbapt/* Flags that may be passed as the third argument to xTokenize() */
12207298161Sbapt#define FTS5_TOKENIZE_QUERY     0x0001
12208298161Sbapt#define FTS5_TOKENIZE_PREFIX    0x0002
12209298161Sbapt#define FTS5_TOKENIZE_DOCUMENT  0x0004
12210298161Sbapt#define FTS5_TOKENIZE_AUX       0x0008
12211298161Sbapt
12212298161Sbapt/* Flags that may be passed by the tokenizer implementation back to FTS5
12213298161Sbapt** as the third argument to the supplied xToken callback. */
12214298161Sbapt#define FTS5_TOKEN_COLOCATED    0x0001      /* Same position as prev. token */
12215298161Sbapt
12216298161Sbapt/*
12217298161Sbapt** END OF CUSTOM TOKENIZERS
12218298161Sbapt*************************************************************************/
12219298161Sbapt
12220298161Sbapt/*************************************************************************
12221298161Sbapt** FTS5 EXTENSION REGISTRATION API
12222298161Sbapt*/
12223298161Sbapttypedef struct fts5_api fts5_api;
12224298161Sbaptstruct fts5_api {
12225298161Sbapt  int iVersion;                   /* Currently always set to 2 */
12226298161Sbapt
12227298161Sbapt  /* Create a new tokenizer */
12228298161Sbapt  int (*xCreateTokenizer)(
12229298161Sbapt    fts5_api *pApi,
12230298161Sbapt    const char *zName,
12231298161Sbapt    void *pContext,
12232298161Sbapt    fts5_tokenizer *pTokenizer,
12233298161Sbapt    void (*xDestroy)(void*)
12234298161Sbapt  );
12235298161Sbapt
12236298161Sbapt  /* Find an existing tokenizer */
12237298161Sbapt  int (*xFindTokenizer)(
12238298161Sbapt    fts5_api *pApi,
12239298161Sbapt    const char *zName,
12240298161Sbapt    void **ppContext,
12241298161Sbapt    fts5_tokenizer *pTokenizer
12242298161Sbapt  );
12243298161Sbapt
12244298161Sbapt  /* Create a new auxiliary function */
12245298161Sbapt  int (*xCreateFunction)(
12246298161Sbapt    fts5_api *pApi,
12247298161Sbapt    const char *zName,
12248298161Sbapt    void *pContext,
12249298161Sbapt    fts5_extension_function xFunction,
12250298161Sbapt    void (*xDestroy)(void*)
12251298161Sbapt  );
12252298161Sbapt};
12253298161Sbapt
12254298161Sbapt/*
12255298161Sbapt** END OF REGISTRATION API
12256298161Sbapt*************************************************************************/
12257298161Sbapt
12258298161Sbapt#ifdef __cplusplus
12259298161Sbapt}  /* end of the 'extern "C"' block */
12260298161Sbapt#endif
12261298161Sbapt
12262298161Sbapt#endif /* _FTS5_H */
12263298161Sbapt
12264305002Scy/******** End of fts5.h *********/
12265