blob: 20c8289041c08e56a39a549df1346c232dcc5512 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
drhb19a2bc2001-09-16 00:13:26 +000012** This header file defines the interface that the SQLite library
drh75897232000-05-29 14:26:00 +000013** presents to client programs.
14**
drh900dfba2004-07-21 15:21:36 +000015** @(#) $Id: sqlite.h.in,v 1.109 2004/07/21 15:21:36 drh Exp $
drh75897232000-05-29 14:26:00 +000016*/
17#ifndef _SQLITE_H_
18#define _SQLITE_H_
drha18c5682000-10-08 22:20:57 +000019#include <stdarg.h> /* Needed for the definition of va_list */
drh75897232000-05-29 14:26:00 +000020
21/*
drh382c0242001-10-06 16:33:02 +000022** Make sure we can call this stuff from C++.
23*/
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/*
drhb86ccfb2003-01-28 23:13:10 +000029** The version of the SQLite library.
30*/
31#define SQLITE_VERSION "--VERS--"
32
33/*
drhb217a572000-08-22 13:40:18 +000034** The version string is also compiled into the library so that a program
35** can check to make sure that the lib*.a file and the *.h file are from
36** the same version.
37*/
danielk19776f8a5032004-05-10 10:34:51 +000038extern const char sqlite3_version[];
drh303aaa72000-08-17 10:22:34 +000039
40/*
drh75897232000-05-29 14:26:00 +000041** Each open sqlite database is represented by an instance of the
42** following opaque structure.
43*/
danielk197765904932004-05-26 06:18:37 +000044typedef struct sqlite sqlite3;
45
drh75897232000-05-29 14:26:00 +000046
47/*
drhefad9992004-06-22 12:13:55 +000048** Some compilers do not support the "long long" datatype. So we have
49** to do a typedef that for 64-bit integers that depends on what compiler
50** is being used.
51*/
52#if defined(_MSC_VER) || defined(__BORLANDC__)
53 typedef __int64 sqlite_int64;
54#else
55 typedef long long int sqlite_int64;
56#endif
57
58
59/*
drh75897232000-05-29 14:26:00 +000060** A function to close the database.
61**
62** Call this function with a pointer to a structure that was previously
danielk19776f8a5032004-05-10 10:34:51 +000063** returned from sqlite3_open() and the corresponding database will by closed.
danielk197796d81f92004-06-19 03:33:57 +000064**
65** All SQL statements prepared using sqlite3_prepare() or
66** sqlite3_prepare16() must be deallocated using sqlite3_finalize() before
67** this routine is called. Otherwise, SQLITE_BUSY is returned and the
68** database connection remains open.
drh75897232000-05-29 14:26:00 +000069*/
danielk1977f9d64d22004-06-19 08:18:07 +000070int sqlite3_close(sqlite3 *);
drh75897232000-05-29 14:26:00 +000071
72/*
73** The type for a callback function.
74*/
75typedef int (*sqlite_callback)(void*,int,char**, char**);
76
77/*
78** A function to executes one or more statements of SQL.
79**
80** If one or more of the SQL statements are queries, then
81** the callback function specified by the 3rd parameter is
82** invoked once for each row of the query result. This callback
83** should normally return 0. If the callback returns a non-zero
84** value then the query is aborted, all subsequent SQL statements
danielk19776f8a5032004-05-10 10:34:51 +000085** are skipped and the sqlite3_exec() function returns the SQLITE_ABORT.
drh75897232000-05-29 14:26:00 +000086**
87** The 4th parameter is an arbitrary pointer that is passed
88** to the callback function as its first parameter.
89**
90** The 2nd parameter to the callback function is the number of
drhb19a2bc2001-09-16 00:13:26 +000091** columns in the query result. The 3rd parameter to the callback
92** is an array of strings holding the values for each column.
93** The 4th parameter to the callback is an array of strings holding
94** the names of each column.
drh75897232000-05-29 14:26:00 +000095**
96** The callback function may be NULL, even for queries. A NULL
97** callback is not an error. It just means that no callback
98** will be invoked.
99**
100** If an error occurs while parsing or evaluating the SQL (but
101** not while executing the callback) then an appropriate error
102** message is written into memory obtained from malloc() and
drhb19a2bc2001-09-16 00:13:26 +0000103** *errmsg is made to point to that message. The calling function
104** is responsible for freeing the memory that holds the error
drh3f4fedb2004-05-31 19:34:33 +0000105** message. Use sqlite3_free() for this. If errmsg==NULL,
drhb86ccfb2003-01-28 23:13:10 +0000106** then no error message is ever written.
drhb19a2bc2001-09-16 00:13:26 +0000107**
108** The return value is is SQLITE_OK if there are no errors and
109** some other return code if there is an error. The particular
110** return value depends on the type of error.
drh58b95762000-06-02 01:17:37 +0000111**
112** If the query could not be executed because a database file is
drh2dfbbca2000-07-28 14:32:48 +0000113** locked or busy, then this function returns SQLITE_BUSY. (This
danielk19776f8a5032004-05-10 10:34:51 +0000114** behavior can be modified somewhat using the sqlite3_busy_handler()
115** and sqlite3_busy_timeout() functions below.)
drh75897232000-05-29 14:26:00 +0000116*/
danielk19776f8a5032004-05-10 10:34:51 +0000117int sqlite3_exec(
danielk1977f9d64d22004-06-19 08:18:07 +0000118 sqlite3*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000119 const char *sql, /* SQL to be executed */
drh75897232000-05-29 14:26:00 +0000120 sqlite_callback, /* Callback function */
121 void *, /* 1st argument to callback function */
122 char **errmsg /* Error msg written here */
123);
124
drh58b95762000-06-02 01:17:37 +0000125/*
danielk19776f8a5032004-05-10 10:34:51 +0000126** Return values for sqlite3_exec() and sqlite3_step()
drh58b95762000-06-02 01:17:37 +0000127*/
drh717e6402001-09-27 03:22:32 +0000128#define SQLITE_OK 0 /* Successful result */
129#define SQLITE_ERROR 1 /* SQL error or missing database */
130#define SQLITE_INTERNAL 2 /* An internal logic error in SQLite */
131#define SQLITE_PERM 3 /* Access permission denied */
132#define SQLITE_ABORT 4 /* Callback routine requested an abort */
133#define SQLITE_BUSY 5 /* The database file is locked */
134#define SQLITE_LOCKED 6 /* A table in the database is locked */
135#define SQLITE_NOMEM 7 /* A malloc() failed */
136#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
drh24cd67e2004-05-10 16:18:47 +0000137#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
drh717e6402001-09-27 03:22:32 +0000138#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
139#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
140#define SQLITE_NOTFOUND 12 /* (Internal Only) Table or record not found */
141#define SQLITE_FULL 13 /* Insertion failed because database is full */
142#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
143#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
drh24cd67e2004-05-10 16:18:47 +0000144#define SQLITE_EMPTY 16 /* Database is empty */
drh717e6402001-09-27 03:22:32 +0000145#define SQLITE_SCHEMA 17 /* The database schema changed */
146#define SQLITE_TOOBIG 18 /* Too much data for one row of a table */
147#define SQLITE_CONSTRAINT 19 /* Abort due to contraint violation */
drh8aff1012001-12-22 14:49:24 +0000148#define SQLITE_MISMATCH 20 /* Data type mismatch */
drh247be432002-05-10 05:44:55 +0000149#define SQLITE_MISUSE 21 /* Library used incorrectly */
drh8766c342002-11-09 00:33:15 +0000150#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
drhed6c8672003-01-12 18:02:16 +0000151#define SQLITE_AUTH 23 /* Authorization denied */
drh1c2d8412003-03-31 00:30:47 +0000152#define SQLITE_FORMAT 24 /* Auxiliary database format error */
danielk19776f8a5032004-05-10 10:34:51 +0000153#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
drhc602f9a2004-02-12 19:01:04 +0000154#define SQLITE_NOTADB 26 /* File opened that is not a database file */
danielk19776f8a5032004-05-10 10:34:51 +0000155#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
156#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
drh717e6402001-09-27 03:22:32 +0000157
drhaf9ff332002-01-16 21:00:27 +0000158/*
159** Each entry in an SQLite table has a unique integer key. (The key is
160** the value of the INTEGER PRIMARY KEY column if there is such a column,
161** otherwise the key is generated at random. The unique key is always
162** available as the ROWID, OID, or _ROWID_ column.) The following routine
163** returns the integer key of the most recent insert in the database.
164**
165** This function is similar to the mysql_insert_id() function from MySQL.
166*/
drhefad9992004-06-22 12:13:55 +0000167sqlite_int64 sqlite3_last_insert_rowid(sqlite3*);
drhaf9ff332002-01-16 21:00:27 +0000168
drhc8d30ac2002-04-12 10:08:59 +0000169/*
170** This function returns the number of database rows that were changed
danielk19776f8a5032004-05-10 10:34:51 +0000171** (or inserted or deleted) by the most recent called sqlite3_exec().
drhc8d30ac2002-04-12 10:08:59 +0000172**
173** All changes are counted, even if they were later undone by a
174** ROLLBACK or ABORT. Except, changes associated with creating and
175** dropping tables are not counted.
176**
danielk19776f8a5032004-05-10 10:34:51 +0000177** If a callback invokes sqlite3_exec() recursively, then the changes
drhc8d30ac2002-04-12 10:08:59 +0000178** in the inner, recursive call are counted together with the changes
179** in the outer call.
180**
181** SQLite implements the command "DELETE FROM table" without a WHERE clause
182** by dropping and recreating the table. (This is much faster than going
183** through and deleting individual elements form the table.) Because of
184** this optimization, the change count for "DELETE FROM table" will be
185** zero regardless of the number of elements that were originally in the
186** table. To get an accurate count of the number of rows deleted, use
187** "DELETE FROM table WHERE 1" instead.
188*/
danielk1977f9d64d22004-06-19 08:18:07 +0000189int sqlite3_changes(sqlite3*);
drhc8d30ac2002-04-12 10:08:59 +0000190
rdcf146a772004-02-25 22:51:06 +0000191/*
danielk1977b28af712004-06-21 06:50:26 +0000192** This function returns the number of database rows that have been
193** modified by INSERT, UPDATE or DELETE statements since the database handle
194** was opened. This includes UPDATE, INSERT and DELETE statements executed
195** as part of trigger programs. All changes are counted as soon as the
196** statement that makes them is completed (when the statement handle is
197** passed to sqlite3_reset() or sqlite_finalise()).
rdcf146a772004-02-25 22:51:06 +0000198**
199** SQLite implements the command "DELETE FROM table" without a WHERE clause
200** by dropping and recreating the table. (This is much faster than going
201** through and deleting individual elements form the table.) Because of
202** this optimization, the change count for "DELETE FROM table" will be
203** zero regardless of the number of elements that were originally in the
204** table. To get an accurate count of the number of rows deleted, use
205** "DELETE FROM table WHERE 1" instead.
rdcf146a772004-02-25 22:51:06 +0000206*/
danielk1977b28af712004-06-21 06:50:26 +0000207int sqlite3_total_changes(sqlite3*);
208
drh4c504392000-10-16 22:06:40 +0000209/* This function causes any pending database operation to abort and
210** return at its earliest opportunity. This routine is typically
drh66b89c82000-11-28 20:47:17 +0000211** called in response to a user action such as pressing "Cancel"
drh4c504392000-10-16 22:06:40 +0000212** or Ctrl-C where the user wants a long query operation to halt
213** immediately.
214*/
danielk1977f9d64d22004-06-19 08:18:07 +0000215void sqlite3_interrupt(sqlite3*);
drh4c504392000-10-16 22:06:40 +0000216
drheec553b2000-06-02 01:51:20 +0000217
danielk197761de0d12004-05-27 23:56:16 +0000218/* These functions return true if the given input string comprises
219** one or more complete SQL statements. For the sqlite3_complete() call,
220** the parameter must be a nul-terminated UTF-8 string. For
221** sqlite3_complete16(), a nul-terminated machine byte order UTF-16 string
222** is required.
drh75897232000-05-29 14:26:00 +0000223**
224** The algorithm is simple. If the last token other than spaces
225** and comments is a semicolon, then return true. otherwise return
226** false.
227*/
danielk19776f8a5032004-05-10 10:34:51 +0000228int sqlite3_complete(const char *sql);
danielk197761de0d12004-05-27 23:56:16 +0000229int sqlite3_complete16(const void *sql);
drh75897232000-05-29 14:26:00 +0000230
drh2dfbbca2000-07-28 14:32:48 +0000231/*
232** This routine identifies a callback function that is invoked
233** whenever an attempt is made to open a database table that is
234** currently locked by another process or thread. If the busy callback
danielk19776f8a5032004-05-10 10:34:51 +0000235** is NULL, then sqlite3_exec() returns SQLITE_BUSY immediately if
drh2dfbbca2000-07-28 14:32:48 +0000236** it finds a locked table. If the busy callback is not NULL, then
danielk19776f8a5032004-05-10 10:34:51 +0000237** sqlite3_exec() invokes the callback with three arguments. The
drh2dfbbca2000-07-28 14:32:48 +0000238** second argument is the name of the locked table and the third
239** argument is the number of times the table has been busy. If the
danielk19776f8a5032004-05-10 10:34:51 +0000240** busy callback returns 0, then sqlite3_exec() immediately returns
241** SQLITE_BUSY. If the callback returns non-zero, then sqlite3_exec()
drh2dfbbca2000-07-28 14:32:48 +0000242** tries to open the table again and the cycle repeats.
243**
244** The default busy callback is NULL.
245**
246** Sqlite is re-entrant, so the busy handler may start a new query.
247** (It is not clear why anyone would every want to do this, but it
248** is allowed, in theory.) But the busy handler may not close the
249** database. Closing the database from a busy handler will delete
250** data structures out from under the executing query and will
251** probably result in a coredump.
252*/
danielk1977f9d64d22004-06-19 08:18:07 +0000253int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
drh2dfbbca2000-07-28 14:32:48 +0000254
255/*
256** This routine sets a busy handler that sleeps for a while when a
257** table is locked. The handler will sleep multiple times until
258** at least "ms" milleseconds of sleeping have been done. After
259** "ms" milleseconds of sleeping, the handler returns 0 which
danielk19776f8a5032004-05-10 10:34:51 +0000260** causes sqlite3_exec() to return SQLITE_BUSY.
drh2dfbbca2000-07-28 14:32:48 +0000261**
262** Calling this routine with an argument less than or equal to zero
263** turns off all busy handlers.
264*/
danielk1977f9d64d22004-06-19 08:18:07 +0000265int sqlite3_busy_timeout(sqlite3*, int ms);
drh2dfbbca2000-07-28 14:32:48 +0000266
drhe3710332000-09-29 13:30:53 +0000267/*
danielk19776f8a5032004-05-10 10:34:51 +0000268** This next routine is really just a wrapper around sqlite3_exec().
drhe3710332000-09-29 13:30:53 +0000269** Instead of invoking a user-supplied callback for each row of the
270** result, this routine remembers each row of the result in memory
271** obtained from malloc(), then returns all of the result after the
drha18c5682000-10-08 22:20:57 +0000272** query has finished.
273**
274** As an example, suppose the query result where this table:
275**
276** Name | Age
277** -----------------------
278** Alice | 43
279** Bob | 28
280** Cindy | 21
281**
282** If the 3rd argument were &azResult then after the function returns
drh98699b52000-10-09 12:57:00 +0000283** azResult will contain the following data:
drha18c5682000-10-08 22:20:57 +0000284**
285** azResult[0] = "Name";
286** azResult[1] = "Age";
287** azResult[2] = "Alice";
288** azResult[3] = "43";
289** azResult[4] = "Bob";
290** azResult[5] = "28";
291** azResult[6] = "Cindy";
292** azResult[7] = "21";
293**
294** Notice that there is an extra row of data containing the column
295** headers. But the *nrow return value is still 3. *ncolumn is
296** set to 2. In general, the number of values inserted into azResult
297** will be ((*nrow) + 1)*(*ncolumn).
298**
299** After the calling function has finished using the result, it should
danielk19776f8a5032004-05-10 10:34:51 +0000300** pass the result data pointer to sqlite3_free_table() in order to
drha18c5682000-10-08 22:20:57 +0000301** release the memory that was malloc-ed. Because of the way the
302** malloc() happens, the calling function must not try to call
danielk19776f8a5032004-05-10 10:34:51 +0000303** malloc() directly. Only sqlite3_free_table() is able to release
drha18c5682000-10-08 22:20:57 +0000304** the memory properly and safely.
drhe3710332000-09-29 13:30:53 +0000305**
danielk19776f8a5032004-05-10 10:34:51 +0000306** The return value of this routine is the same as from sqlite3_exec().
drhe3710332000-09-29 13:30:53 +0000307*/
danielk19776f8a5032004-05-10 10:34:51 +0000308int sqlite3_get_table(
danielk1977f9d64d22004-06-19 08:18:07 +0000309 sqlite3*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000310 const char *sql, /* SQL to be executed */
drhe3710332000-09-29 13:30:53 +0000311 char ***resultp, /* Result written to a char *[] that this points to */
312 int *nrow, /* Number of result rows written here */
313 int *ncolumn, /* Number of result columns written here */
314 char **errmsg /* Error msg written here */
315);
316
317/*
danielk19776f8a5032004-05-10 10:34:51 +0000318** Call this routine to free the memory that sqlite3_get_table() allocated.
drhe3710332000-09-29 13:30:53 +0000319*/
danielk19776f8a5032004-05-10 10:34:51 +0000320void sqlite3_free_table(char **result);
drhe3710332000-09-29 13:30:53 +0000321
drha18c5682000-10-08 22:20:57 +0000322/*
drh4f26d6c2004-05-26 23:25:30 +0000323** The following routines are variants of the "sprintf()" from the
324** standard C library. The resulting string is written into memory
325** obtained from malloc() so that there is never a possiblity of buffer
326** overflow. These routines also implement some additional formatting
327** options that are useful for constructing SQL statements.
328**
329** The strings returned by these routines should be freed by calling
330** sqlite3_free().
drha18c5682000-10-08 22:20:57 +0000331**
332** All of the usual printf formatting options apply. In addition, there
333** is a "%q" option. %q works like %s in that it substitutes a null-terminated
drh66b89c82000-11-28 20:47:17 +0000334** string from the argument list. But %q also doubles every '\'' character.
drha18c5682000-10-08 22:20:57 +0000335** %q is designed for use inside a string literal. By doubling each '\''
drh66b89c82000-11-28 20:47:17 +0000336** character it escapes that character and allows it to be inserted into
drha18c5682000-10-08 22:20:57 +0000337** the string.
338**
339** For example, so some string variable contains text as follows:
340**
341** char *zText = "It's a happy day!";
342**
343** We can use this text in an SQL statement as follows:
344**
danielk19776f8a5032004-05-10 10:34:51 +0000345** sqlite3_exec_printf(db, "INSERT INTO table VALUES('%q')",
drha18c5682000-10-08 22:20:57 +0000346** callback1, 0, 0, zText);
347**
348** Because the %q format string is used, the '\'' character in zText
349** is escaped and the SQL generated is as follows:
350**
351** INSERT INTO table1 VALUES('It''s a happy day!')
352**
353** This is correct. Had we used %s instead of %q, the generated SQL
354** would have looked like this:
355**
356** INSERT INTO table1 VALUES('It's a happy day!');
357**
358** This second example is an SQL syntax error. As a general rule you
359** should always use %q instead of %s when inserting text into a string
360** literal.
361*/
danielk19776f8a5032004-05-10 10:34:51 +0000362char *sqlite3_mprintf(const char*,...);
363char *sqlite3_vmprintf(const char*, va_list);
drh4f26d6c2004-05-26 23:25:30 +0000364void sqlite3_free(char *z);
drh5191b7e2002-03-08 02:12:00 +0000365
366/*
drhed6c8672003-01-12 18:02:16 +0000367** This routine registers a callback with the SQLite library. The
drhb86ccfb2003-01-28 23:13:10 +0000368** callback is invoked (at compile-time, not at run-time) for each
369** attempt to access a column of a table in the database. The callback
370** returns SQLITE_OK if access is allowed, SQLITE_DENY if the entire
371** SQL statement should be aborted with an error and SQLITE_IGNORE
372** if the column should be treated as a NULL value.
drhed6c8672003-01-12 18:02:16 +0000373*/
danielk19776f8a5032004-05-10 10:34:51 +0000374int sqlite3_set_authorizer(
danielk1977f9d64d22004-06-19 08:18:07 +0000375 sqlite3*,
drhe22a3342003-04-22 20:30:37 +0000376 int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
drhe5f9c642003-01-13 23:27:31 +0000377 void *pUserData
drhed6c8672003-01-12 18:02:16 +0000378);
379
380/*
381** The second parameter to the access authorization function above will
drhe5f9c642003-01-13 23:27:31 +0000382** be one of the values below. These values signify what kind of operation
383** is to be authorized. The 3rd and 4th parameters to the authorization
384** function will be parameters or NULL depending on which of the following
drhe22a3342003-04-22 20:30:37 +0000385** codes is used as the second parameter. The 5th parameter is the name
386** of the database ("main", "temp", etc.) if applicable. The 6th parameter
drh5cf590c2003-04-24 01:45:04 +0000387** is the name of the inner-most trigger or view that is responsible for
388** the access attempt or NULL if this access attempt is directly from
389** input SQL code.
drhe5f9c642003-01-13 23:27:31 +0000390**
391** Arg-3 Arg-4
drhed6c8672003-01-12 18:02:16 +0000392*/
drh77ad4e42003-01-14 02:49:27 +0000393#define SQLITE_COPY 0 /* Table Name File Name */
drhe5f9c642003-01-13 23:27:31 +0000394#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */
395#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */
396#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */
397#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */
drh77ad4e42003-01-14 02:49:27 +0000398#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */
drhe5f9c642003-01-13 23:27:31 +0000399#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */
drh77ad4e42003-01-14 02:49:27 +0000400#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */
drhe5f9c642003-01-13 23:27:31 +0000401#define SQLITE_CREATE_VIEW 8 /* View Name NULL */
402#define SQLITE_DELETE 9 /* Table Name NULL */
drh77ad4e42003-01-14 02:49:27 +0000403#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */
drhe5f9c642003-01-13 23:27:31 +0000404#define SQLITE_DROP_TABLE 11 /* Table Name NULL */
drh77ad4e42003-01-14 02:49:27 +0000405#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */
drhe5f9c642003-01-13 23:27:31 +0000406#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */
drh77ad4e42003-01-14 02:49:27 +0000407#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */
drhe5f9c642003-01-13 23:27:31 +0000408#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */
drh77ad4e42003-01-14 02:49:27 +0000409#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */
drhe5f9c642003-01-13 23:27:31 +0000410#define SQLITE_DROP_VIEW 17 /* View Name NULL */
411#define SQLITE_INSERT 18 /* Table Name NULL */
412#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */
413#define SQLITE_READ 20 /* Table Name Column Name */
414#define SQLITE_SELECT 21 /* NULL NULL */
415#define SQLITE_TRANSACTION 22 /* NULL NULL */
416#define SQLITE_UPDATE 23 /* Table Name Column Name */
drh81e293b2003-06-06 19:00:42 +0000417#define SQLITE_ATTACH 24 /* Filename NULL */
418#define SQLITE_DETACH 25 /* Database Name NULL */
419
drhed6c8672003-01-12 18:02:16 +0000420
421/*
422** The return value of the authorization function should be one of the
423** following constants:
424*/
425/* #define SQLITE_OK 0 // Allow access (This is actually defined above) */
426#define SQLITE_DENY 1 /* Abort the SQL statement with an error */
427#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */
428
429/*
danielk19776f8a5032004-05-10 10:34:51 +0000430** Register a function that is called at every invocation of sqlite3_exec()
danielk19774ad17132004-05-21 01:47:26 +0000431** or sqlite3_prepare(). This function can be used (for example) to generate
drhb86ccfb2003-01-28 23:13:10 +0000432** a log file of all SQL executed against a database.
drh18de4822003-01-16 16:28:53 +0000433*/
danielk1977f9d64d22004-06-19 08:18:07 +0000434void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
drh18de4822003-01-16 16:28:53 +0000435
danielk1977348bb5d2003-10-18 09:37:26 +0000436/*
437** This routine configures a callback function - the progress callback - that
danielk19776f8a5032004-05-10 10:34:51 +0000438** is invoked periodically during long running calls to sqlite3_exec(),
439** sqlite3_step() and sqlite3_get_table(). An example use for this API is to keep
danielk1977348bb5d2003-10-18 09:37:26 +0000440** a GUI updated during a large query.
441**
442** The progress callback is invoked once for every N virtual machine opcodes,
443** where N is the second argument to this function. The progress callback
444** itself is identified by the third argument to this function. The fourth
445** argument to this function is a void pointer passed to the progress callback
446** function each time it is invoked.
447**
danielk19776f8a5032004-05-10 10:34:51 +0000448** If a call to sqlite3_exec(), sqlite3_step() or sqlite3_get_table() results
danielk1977348bb5d2003-10-18 09:37:26 +0000449** in less than N opcodes being executed, then the progress callback is not
450** invoked.
451**
danielk1977348bb5d2003-10-18 09:37:26 +0000452** To remove the progress callback altogether, pass NULL as the third
453** argument to this function.
454**
455** If the progress callback returns a result other than 0, then the current
456** query is immediately terminated and any database changes rolled back. If the
457** query was part of a larger transaction, then the transaction is not rolled
danielk19776f8a5032004-05-10 10:34:51 +0000458** back and remains active. The sqlite3_exec() call returns SQLITE_ABORT.
drhaa940ea2004-01-15 02:44:03 +0000459**
460******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
danielk1977348bb5d2003-10-18 09:37:26 +0000461*/
danielk1977f9d64d22004-06-19 08:18:07 +0000462void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
danielk1977348bb5d2003-10-18 09:37:26 +0000463
drhaa940ea2004-01-15 02:44:03 +0000464/*
465** Register a callback function to be invoked whenever a new transaction
466** is committed. The pArg argument is passed through to the callback.
467** callback. If the callback function returns non-zero, then the commit
468** is converted into a rollback.
469**
470** If another function was previously registered, its pArg value is returned.
471** Otherwise NULL is returned.
472**
473** Registering a NULL function disables the callback.
474**
475******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ******
476*/
danielk1977f9d64d22004-06-19 08:18:07 +0000477void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
drhaa940ea2004-01-15 02:44:03 +0000478
drh22fbcb82004-02-01 01:22:50 +0000479/*
drh4f26d6c2004-05-26 23:25:30 +0000480** Open the sqlite database file "filename". The "filename" is UTF-8
481** encoded for sqlite3_open() and UTF-16 encoded in the native byte order
482** for sqlite3_open16(). An sqlite3* handle is returned in *ppDb, even
483** if an error occurs. If the database is opened (or created) successfully,
484** then SQLITE_OK is returned. Otherwise an error code is returned. The
485** sqlite3_errmsg() or sqlite3_errmsg16() routines can be used to obtain
486** an English language description of the error.
drh22fbcb82004-02-01 01:22:50 +0000487**
drh4f26d6c2004-05-26 23:25:30 +0000488** If the database file does not exist, then a new database is created.
489** The encoding for the database is UTF-8 if sqlite3_open() is called and
490** UTF-16 if sqlite3_open16 is used.
danielk197765904932004-05-26 06:18:37 +0000491**
492** Whether or not an error occurs when it is opened, resources associated
493** with the sqlite3* handle should be released by passing it to
494** sqlite3_close() when it is no longer required.
495*/
496int sqlite3_open(
497 const char *filename, /* Database filename (UTF-8) */
danielk19774f057f92004-06-08 00:02:33 +0000498 sqlite3 **ppDb /* OUT: SQLite db handle */
danielk197765904932004-05-26 06:18:37 +0000499);
danielk197765904932004-05-26 06:18:37 +0000500int sqlite3_open16(
501 const void *filename, /* Database filename (UTF-16) */
danielk19774f057f92004-06-08 00:02:33 +0000502 sqlite3 **ppDb /* OUT: SQLite db handle */
danielk197765904932004-05-26 06:18:37 +0000503);
danielk1977295ba552004-05-19 10:34:51 +0000504
danielk197765904932004-05-26 06:18:37 +0000505/*
506** Return the error code for the most recent sqlite3_* API call associated
507** with sqlite3 handle 'db'. SQLITE_OK is returned if the most recent
508** API call was successful.
509**
510** Calls to many sqlite3_* functions set the error code and string returned
511** by sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16()
512** (overwriting the previous values). Note that calls to sqlite3_errcode(),
513** sqlite3_errmsg() and sqlite3_errmsg16() themselves do not affect the
514** results of future invocations.
515**
516** Assuming no other intervening sqlite3_* API calls are made, the error
517** code returned by this function is associated with the same error as
518** the strings returned by sqlite3_errmsg() and sqlite3_errmsg16().
519*/
520int sqlite3_errcode(sqlite3 *db);
521
522/*
523** Return a pointer to a UTF-8 encoded string describing in english the
524** error condition for the most recent sqlite3_* API call. The returned
525** string is always terminated by an 0x00 byte.
526**
527** The string "not an error" is returned when the most recent API call was
528** successful.
529*/
530const char *sqlite3_errmsg(sqlite3*);
531
532/*
533** Return a pointer to a UTF-16 native byte order encoded string describing
534** in english the error condition for the most recent sqlite3_* API call.
535** The returned string is always terminated by a pair of 0x00 bytes.
536**
537** The string "not an error" is returned when the most recent API call was
538** successful.
539*/
540const void *sqlite3_errmsg16(sqlite3*);
541
542/*
543** An instance of the following opaque structure is used to represent
544** a compiled SQL statment.
545*/
danielk1977fc57d7b2004-05-26 02:04:57 +0000546typedef struct sqlite3_stmt sqlite3_stmt;
547
danielk1977e3209e42004-05-20 01:40:18 +0000548/*
danielk197765904932004-05-26 06:18:37 +0000549** To execute an SQL query, it must first be compiled into a byte-code
550** program using one of the following routines. The only difference between
551** them is that the second argument, specifying the SQL statement to
552** compile, is assumed to be encoded in UTF-8 for the sqlite3_prepare()
553** function and UTF-16 for sqlite3_prepare16().
554**
555** The first parameter "db" is an SQLite database handle. The second
556** parameter "zSql" is the statement to be compiled, encoded as either
557** UTF-8 or UTF-16 (see above). If the next parameter, "nBytes", is less
558** than zero, then zSql is read up to the first nul terminator. If
559** "nBytes" is not less than zero, then it is the length of the string zSql
560** in bytes (not characters).
561**
562** *pzTail is made to point to the first byte past the end of the first
563** SQL statement in zSql. This routine only compiles the first statement
564** in zSql, so *pzTail is left pointing to what remains uncompiled.
565**
566** *ppStmt is left pointing to a compiled SQL statement that can be
567** executed using sqlite3_step(). Or if there is an error, *ppStmt may be
568** set to NULL. If the input text contained no SQL (if the input is and
569** empty string or a comment) then *ppStmt is set to NULL.
570**
571** On success, SQLITE_OK is returned. Otherwise an error code is returned.
572*/
573int sqlite3_prepare(
574 sqlite3 *db, /* Database handle */
575 const char *zSql, /* SQL statement, UTF-8 encoded */
576 int nBytes, /* Length of zSql in bytes. */
577 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
578 const char **pzTail /* OUT: Pointer to unused portion of zSql */
579);
580int sqlite3_prepare16(
581 sqlite3 *db, /* Database handle */
582 const void *zSql, /* SQL statement, UTF-16 encoded */
583 int nBytes, /* Length of zSql in bytes. */
584 sqlite3_stmt **ppStmt, /* OUT: Statement handle */
585 const void **pzTail /* OUT: Pointer to unused portion of zSql */
586);
587
588/*
drhf4479502004-05-27 03:12:53 +0000589** Pointers to the following two opaque structures are used to communicate
590** with the implementations of user-defined functions.
591*/
592typedef struct sqlite3_context sqlite3_context;
593typedef struct Mem sqlite3_value;
594
595/*
drh4f26d6c2004-05-26 23:25:30 +0000596** In the SQL strings input to sqlite3_prepare() and sqlite3_prepare16(),
597** one or more literals can be replace by a wildcard "?" or ":N:" where
598** N is an integer. These value of these wildcard literals can be set
599** using the routines listed below.
600**
601** In every case, the first parameter is a pointer to the sqlite3_stmt
602** structure returned from sqlite3_prepare(). The second parameter is the
603** index of the wildcard. The first "?" has an index of 1. ":N:" wildcards
604** use the index N.
605**
drh900dfba2004-07-21 15:21:36 +0000606** The fifth parameter to sqlite3_bind_blob(), sqlite3_bind_text(), and
607** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
608** text after SQLite has finished with it. If the fifth argument is the
609** special value SQLITE_STATIC, then the library assumes that the information
610** is in static, unmanaged space and does not need to be freed. If the
611** fifth argument has the value SQLITE_TRANSIENT, then SQLite makes its
612** own private copy of the data.
drh4f26d6c2004-05-26 23:25:30 +0000613**
614** The sqlite3_bind_* routine must be called before sqlite3_step() after
615** an sqlite3_prepare() or sqlite3_reset(). Unbound wildcards are interpreted
616** as NULL.
617*/
danielk1977d8123362004-06-12 09:25:12 +0000618int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
drhf4479502004-05-27 03:12:53 +0000619int sqlite3_bind_double(sqlite3_stmt*, int, double);
620int sqlite3_bind_int(sqlite3_stmt*, int, int);
drhefad9992004-06-22 12:13:55 +0000621int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite_int64);
drhf4479502004-05-27 03:12:53 +0000622int sqlite3_bind_null(sqlite3_stmt*, int);
danielk1977d8123362004-06-12 09:25:12 +0000623int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
624int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
drhf4479502004-05-27 03:12:53 +0000625int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
drh4f26d6c2004-05-26 23:25:30 +0000626
627/*
drh75f6a032004-07-15 14:15:00 +0000628** Return the number of wildcards in a compiled SQL statement. This
629** routine was added to support DBD::SQLite.
630**
631**** EXPERIMENTAL *****
632*/
633int sqlite3_bind_parameter_count(sqlite3_stmt*);
634
635/*
danielk197765904932004-05-26 06:18:37 +0000636** Return the number of columns in the result set returned by the compiled
637** SQL statement. This routine returns 0 if pStmt is an SQL statement
638** that does not return data (for example an UPDATE).
639*/
640int sqlite3_column_count(sqlite3_stmt *pStmt);
641
642/*
643** The first parameter is a compiled SQL statement. This function returns
644** the column heading for the Nth column of that statement, where N is the
drh4f26d6c2004-05-26 23:25:30 +0000645** second function parameter. The string returned is UTF-8 for
646** sqlite3_column_name() and UTF-16 for sqlite3_column_name16().
danielk197765904932004-05-26 06:18:37 +0000647*/
648const char *sqlite3_column_name(sqlite3_stmt*,int);
danielk197765904932004-05-26 06:18:37 +0000649const void *sqlite3_column_name16(sqlite3_stmt*,int);
650
651/*
652** The first parameter is a compiled SQL statement. If this statement
653** is a SELECT statement, the Nth column of the returned result set
654** of the SELECT is a table column then the declared type of the table
655** column is returned. If the Nth column of the result set is not at table
656** column, then a NULL pointer is returned. The returned string is always
657** UTF-8 encoded. For example, in the database schema:
658**
659** CREATE TABLE t1(c1 VARIANT);
660**
661** And the following statement compiled:
662**
663** SELECT c1 + 1, 0 FROM t1;
664**
665** Then this routine would return the string "VARIANT" for the second
666** result column (i==1), and a NULL pointer for the first result column
667** (i==0).
668*/
669const char *sqlite3_column_decltype(sqlite3_stmt *, int i);
670
671/*
672** The first parameter is a compiled SQL statement. If this statement
673** is a SELECT statement, the Nth column of the returned result set
674** of the SELECT is a table column then the declared type of the table
675** column is returned. If the Nth column of the result set is not at table
676** column, then a NULL pointer is returned. The returned string is always
677** UTF-16 encoded. For example, in the database schema:
678**
drh4f26d6c2004-05-26 23:25:30 +0000679** CREATE TABLE t1(c1 INTEGER);
danielk197765904932004-05-26 06:18:37 +0000680**
681** And the following statement compiled:
682**
683** SELECT c1 + 1, 0 FROM t1;
684**
drh4f26d6c2004-05-26 23:25:30 +0000685** Then this routine would return the string "INTEGER" for the second
danielk197765904932004-05-26 06:18:37 +0000686** result column (i==1), and a NULL pointer for the first result column
687** (i==0).
688*/
689const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
690
danielk1977106bb232004-05-21 10:08:53 +0000691/*
692** After an SQL query has been compiled with a call to either
693** sqlite3_prepare() or sqlite3_prepare16(), then this function must be
694** called one or more times to execute the statement.
695**
696** The return value will be either SQLITE_BUSY, SQLITE_DONE,
697** SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE.
698**
699** SQLITE_BUSY means that the database engine attempted to open
700** a locked database and there is no busy callback registered.
701** Call sqlite3_step() again to retry the open.
702**
703** SQLITE_DONE means that the statement has finished executing
704** successfully. sqlite3_step() should not be called again on this virtual
705** machine.
706**
707** If the SQL statement being executed returns any data, then
708** SQLITE_ROW is returned each time a new row of data is ready
709** for processing by the caller. The values may be accessed using
710** the sqlite3_column_*() functions described below. sqlite3_step()
711** is called again to retrieve the next row of data.
712**
713** SQLITE_ERROR means that a run-time error (such as a constraint
714** violation) has occurred. sqlite3_step() should not be called again on
715** the VM. More information may be found by calling sqlite3_errmsg().
716**
717** SQLITE_MISUSE means that the this routine was called inappropriately.
718** Perhaps it was called on a virtual machine that had already been
719** finalized or on one that had previously returned SQLITE_ERROR or
720** SQLITE_DONE. Or it could be the case the the same database connection
721** is being used simulataneously by two or more threads.
722*/
danielk197717240fd2004-05-26 00:07:25 +0000723int sqlite3_step(sqlite3_stmt*);
danielk1977106bb232004-05-21 10:08:53 +0000724
danielk1977106bb232004-05-21 10:08:53 +0000725/*
726** Return the number of values in the current row of the result set.
727**
728** After a call to sqlite3_step() that returns SQLITE_ROW, this routine
729** will return the same value as the sqlite3_column_count() function.
730** After sqlite3_step() has returned an SQLITE_DONE, SQLITE_BUSY or
731** error code, or before sqlite3_step() has been called on a
732** compiled SQL statement, this routine returns zero.
733*/
danielk197793d46752004-05-23 13:30:58 +0000734int sqlite3_data_count(sqlite3_stmt *pStmt);
danielk19774adee202004-05-08 08:23:19 +0000735
drh4f26d6c2004-05-26 23:25:30 +0000736/*
737** Values are stored in the database in one of the following fundamental
738** types.
739*/
drh9c054832004-05-31 18:51:57 +0000740#define SQLITE_INTEGER 1
741#define SQLITE_FLOAT 2
742#define SQLITE_TEXT 3
743#define SQLITE_BLOB 4
744#define SQLITE_NULL 5
danielk19774adee202004-05-08 08:23:19 +0000745
danielk1977106bb232004-05-21 10:08:53 +0000746/*
drh4f26d6c2004-05-26 23:25:30 +0000747** The next group of routines returns information about the information
748** in a single column of the current result row of a query. In every
749** case the first parameter is a pointer to the SQL statement that is being
750** executed (the sqlite_stmt* that was returned from sqlite3_prepare()) and
751** the second argument is the index of the column for which information
752** should be returned. iCol is zero-indexed. The left-most column as an
753** index of 0.
danielk1977106bb232004-05-21 10:08:53 +0000754**
drh4f26d6c2004-05-26 23:25:30 +0000755** If the SQL statement is not currently point to a valid row, or if the
756** the colulmn index is out of range, the result is undefined.
757**
758** These routines attempt to convert the value where appropriate. For
759** example, if the internal representation is FLOAT and a text result
760** is requested, sprintf() is used internally to do the conversion
761** automatically. The following table details the conversions that
762** are applied:
763**
764** Internal Type Requested Type Conversion
765** ------------- -------------- --------------------------
766** NULL INTEGER Result is 0
767** NULL FLOAT Result is 0.0
768** NULL TEXT Result is an empty string
769** NULL BLOB Result is a zero-length BLOB
770** INTEGER FLOAT Convert from integer to float
771** INTEGER TEXT ASCII rendering of the integer
772** INTEGER BLOB Same as for INTEGER->TEXT
773** FLOAT INTEGER Convert from float to integer
774** FLOAT TEXT ASCII rendering of the float
775** FLOAT BLOB Same as FLOAT->TEXT
776** TEXT INTEGER Use atoi()
777** TEXT FLOAT Use atof()
778** TEXT BLOB No change
779** BLOB INTEGER Convert to TEXT then use atoi()
780** BLOB FLOAT Convert to TEXT then use atof()
781** BLOB TEXT Add a \000 terminator if needed
782**
783** The following access routines are provided:
784**
785** _type() Return the datatype of the result. This is one of
786** SQLITE_INTEGER, SQLITE_FLOAT, SQLITE_TEXT, SQLITE_BLOB,
787** or SQLITE_NULL.
788** _blob() Return the value of a BLOB.
789** _bytes() Return the number of bytes in a BLOB value or the number
790** of bytes in a TEXT value represented as UTF-8. The \000
791** terminator is included in the byte count for TEXT values.
792** _bytes16() Return the number of bytes in a BLOB value or the number
793** of bytes in a TEXT value represented as UTF-16. The \u0000
794** terminator is included in the byte count for TEXT values.
795** _double() Return a FLOAT value.
796** _int() Return an INTEGER value in the host computer's native
797** integer representation. This might be either a 32- or 64-bit
798** integer depending on the host.
799** _int64() Return an INTEGER value as a 64-bit signed integer.
800** _text() Return the value as UTF-8 text.
801** _text16() Return the value as UTF-16 text.
danielk1977106bb232004-05-21 10:08:53 +0000802*/
drhf4479502004-05-27 03:12:53 +0000803const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
804int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
805int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
806double sqlite3_column_double(sqlite3_stmt*, int iCol);
807int sqlite3_column_int(sqlite3_stmt*, int iCol);
drhefad9992004-06-22 12:13:55 +0000808sqlite_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
drhf4479502004-05-27 03:12:53 +0000809const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
810const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
drh4f26d6c2004-05-26 23:25:30 +0000811int sqlite3_column_type(sqlite3_stmt*, int iCol);
danielk19774adee202004-05-08 08:23:19 +0000812
danielk197765904932004-05-26 06:18:37 +0000813/*
814** The sqlite3_finalize() function is called to delete a compiled
815** SQL statement obtained by a previous call to sqlite3_prepare()
816** or sqlite3_prepare16(). If the statement was executed successfully, or
817** not executed at all, then SQLITE_OK is returned. If execution of the
818** statement failed then an error code is returned.
819**
820** This routine can be called at any point during the execution of the
821** virtual machine. If the virtual machine has not completed execution
822** when this routine is called, that is like encountering an error or
823** an interrupt. (See sqlite3_interrupt().) Incomplete updates may be
824** rolled back and transactions cancelled, depending on the circumstances,
825** and the result code returned will be SQLITE_ABORT.
826*/
827int sqlite3_finalize(sqlite3_stmt *pStmt);
828
829/*
830** The sqlite3_reset() function is called to reset a compiled SQL
831** statement obtained by a previous call to sqlite3_prepare() or
832** sqlite3_prepare16() back to it's initial state, ready to be re-executed.
833** Any SQL statement variables that had values bound to them using
834** the sqlite3_bind_*() API retain their values.
835*/
836int sqlite3_reset(sqlite3_stmt *pStmt);
837
838/*
danielk197765904932004-05-26 06:18:37 +0000839** The following two functions are used to add user functions or aggregates
840** implemented in C to the SQL langauge interpreted by SQLite. The
841** difference only between the two is that the second parameter, the
842** name of the (scalar) function or aggregate, is encoded in UTF-8 for
843** sqlite3_create_function() and UTF-16 for sqlite3_create_function16().
844**
845** The first argument is the database handle that the new function or
846** aggregate is to be added to. If a single program uses more than one
847** database handle internally, then user functions or aggregates must
848** be added individually to each database handle with which they will be
849** used.
850**
851** The third parameter is the number of arguments that the function or
852** aggregate takes. If this parameter is negative, then the function or
853** aggregate may take any number of arguments.
854**
danielk1977d8123362004-06-12 09:25:12 +0000855** The fourth parameter is one of SQLITE_UTF* values defined below,
856** indicating the encoding that the function is most likely to handle
857** values in. This does not change the behaviour of the programming
858** interface. However, if two versions of the same function are registered
859** with different encoding values, SQLite invokes the version likely to
860** minimize conversions between text encodings.
danielk1977d02eb1f2004-06-06 09:44:03 +0000861**
danielk197765904932004-05-26 06:18:37 +0000862** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are
863** pointers to user implemented C functions that implement the user
864** function or aggregate. A scalar function requires an implementation of
865** the xFunc callback only, NULL pointers should be passed as the xStep
866** and xFinal parameters. An aggregate function requires an implementation
867** of xStep and xFinal, but NULL should be passed for xFunc. To delete an
868** existing user function or aggregate, pass NULL for all three function
869** callback. Specifying an inconstent set of callback values, such as an
870** xFunc and an xFinal, or an xStep but no xFinal, SQLITE_ERROR is
871** returned.
872*/
873int sqlite3_create_function(
874 sqlite3 *,
875 const char *zFunctionName,
876 int nArg,
877 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000878 void*,
879 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
880 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
881 void (*xFinal)(sqlite3_context*)
882);
883int sqlite3_create_function16(
884 sqlite3*,
885 const void *zFunctionName,
886 int nArg,
887 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000888 void*,
889 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
890 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
891 void (*xFinal)(sqlite3_context*)
892);
893
894/*
danielk197765904932004-05-26 06:18:37 +0000895** The next routine returns the number of calls to xStep for a particular
896** aggregate function instance. The current call to xStep counts so this
897** routine always returns at least 1.
898*/
899int sqlite3_aggregate_count(sqlite3_context*);
900
danielk19770ffba6b2004-05-24 09:10:10 +0000901/*
drh4f26d6c2004-05-26 23:25:30 +0000902** The next group of routines returns information about parameters to
903** a user-defined function. Function implementations use these routines
904** to access their parameters. These routines are the same as the
905** sqlite3_column_* routines except that these routines take a single
906** sqlite3_value* pointer instead of an sqlite3_stmt* and an integer
907** column number.
danielk19770ffba6b2004-05-24 09:10:10 +0000908*/
drhf4479502004-05-27 03:12:53 +0000909const void *sqlite3_value_blob(sqlite3_value*);
910int sqlite3_value_bytes(sqlite3_value*);
911int sqlite3_value_bytes16(sqlite3_value*);
912double sqlite3_value_double(sqlite3_value*);
913int sqlite3_value_int(sqlite3_value*);
drhefad9992004-06-22 12:13:55 +0000914sqlite_int64 sqlite3_value_int64(sqlite3_value*);
drhf4479502004-05-27 03:12:53 +0000915const unsigned char *sqlite3_value_text(sqlite3_value*);
916const void *sqlite3_value_text16(sqlite3_value*);
danielk1977d8123362004-06-12 09:25:12 +0000917const void *sqlite3_value_text16le(sqlite3_value*);
918const void *sqlite3_value_text16be(sqlite3_value*);
danielk197793d46752004-05-23 13:30:58 +0000919int sqlite3_value_type(sqlite3_value*);
danielk19770ffba6b2004-05-24 09:10:10 +0000920
921/*
danielk19770ae8b832004-05-25 12:05:56 +0000922** Aggregate functions use the following routine to allocate
923** a structure for storing their state. The first time this routine
924** is called for a particular aggregate, a new structure of size nBytes
925** is allocated, zeroed, and returned. On subsequent calls (for the
926** same aggregate instance) the same buffer is returned. The implementation
927** of the aggregate can use the returned buffer to accumulate data.
928**
929** The buffer allocated is freed automatically by SQLite.
930*/
drh4f26d6c2004-05-26 23:25:30 +0000931void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
danielk19777e18c252004-05-25 11:47:24 +0000932
933/*
934** The pUserData parameter to the sqlite3_create_function() and
935** sqlite3_create_aggregate() routines used to register user functions
936** is available to the implementation of the function using this
937** call.
938*/
939void *sqlite3_user_data(sqlite3_context*);
940
941/*
danielk1977682f68b2004-06-05 10:22:17 +0000942** The following two functions may be used by scalar user functions to
943** associate meta-data with argument values. If the same value is passed to
944** multiple invocations of the user-function during query execution, under
945** some circumstances the associated meta-data may be preserved. This may
946** be used, for example, to add a regular-expression matching scalar
947** function. The compiled version of the regular expression is stored as
948** meta-data associated with the SQL value passed as the regular expression
949** pattern.
950**
951** Calling sqlite3_get_auxdata() returns a pointer to the meta data
952** associated with the Nth argument value to the current user function
953** call, where N is the second parameter. If no meta-data has been set for
954** that value, then a NULL pointer is returned.
955**
956** The sqlite3_set_auxdata() is used to associate meta data with a user
957** function argument. The third parameter is a pointer to the meta data
958** to be associated with the Nth user function argument value. The fourth
959** parameter specifies a 'delete function' that will be called on the meta
960** data pointer to release it when it is no longer required. If the delete
961** function pointer is NULL, it is not invoked.
962**
963** In practice, meta-data is preserved between function calls for
964** expressions that are constant at compile time. This includes literal
965** values and SQL variables.
966*/
967void *sqlite3_get_auxdata(sqlite3_context*, int);
968void sqlite3_set_auxdata(sqlite3_context*, int, void*, void (*)(void*));
969
drha2854222004-06-17 19:04:17 +0000970
971/*
972** These are special value for the destructor that is passed in as the
973** final argument to routines like sqlite3_result_blob(). If the destructor
974** argument is SQLITE_STATIC, it means that the content pointer is constant
975** and will never change. It does not need to be destroyed. The
976** SQLITE_TRANSIENT value means that the content will likely change in
977** the near future and that SQLite should make its own private copy of
978** the content before returning.
979*/
danielk1977d8123362004-06-12 09:25:12 +0000980#define SQLITE_STATIC ((void(*)(void *))0)
981#define SQLITE_TRANSIENT ((void(*)(void *))-1)
982
danielk1977682f68b2004-06-05 10:22:17 +0000983/*
drh4f26d6c2004-05-26 23:25:30 +0000984** User-defined functions invoke the following routines in order to
985** set their return value.
danielk19777e18c252004-05-25 11:47:24 +0000986*/
danielk1977d8123362004-06-12 09:25:12 +0000987void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
drh4f26d6c2004-05-26 23:25:30 +0000988void sqlite3_result_double(sqlite3_context*, double);
danielk19777e18c252004-05-25 11:47:24 +0000989void sqlite3_result_error(sqlite3_context*, const char*, int);
990void sqlite3_result_error16(sqlite3_context*, const void*, int);
drh4f26d6c2004-05-26 23:25:30 +0000991void sqlite3_result_int(sqlite3_context*, int);
drhefad9992004-06-22 12:13:55 +0000992void sqlite3_result_int64(sqlite3_context*, sqlite_int64);
drh4f26d6c2004-05-26 23:25:30 +0000993void sqlite3_result_null(sqlite3_context*);
danielk1977d8123362004-06-12 09:25:12 +0000994void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
995void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
996void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
997void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
drh4f26d6c2004-05-26 23:25:30 +0000998void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
drhf9b596e2004-05-26 16:54:42 +0000999
drh52619df2004-06-11 17:48:02 +00001000/*
1001** These are the allowed values for the eTextRep argument to
1002** sqlite3_create_collation and sqlite3_create_function.
1003*/
danielk1977466be562004-06-10 02:16:01 +00001004#define SQLITE_UTF8 1
danielk1977dc8453f2004-06-12 00:42:34 +00001005#define SQLITE_UTF16LE 2
1006#define SQLITE_UTF16BE 3
1007#define SQLITE_UTF16 4 /* Use native byte order */
drh52619df2004-06-11 17:48:02 +00001008#define SQLITE_ANY 5 /* sqlite3_create_function only */
danielk1977466be562004-06-10 02:16:01 +00001009
danielk19777cedc8d2004-06-10 10:50:08 +00001010/*
1011** These two functions are used to add new collation sequences to the
1012** sqlite3 handle specified as the first argument.
1013**
1014** The name of the new collation sequence is specified as a UTF-8 string
1015** for sqlite3_create_collation() and a UTF-16 string for
1016** sqlite3_create_collation16(). In both cases the name is passed as the
1017** second function argument.
1018**
1019** The third argument must be one of the constants SQLITE_UTF8,
1020** SQLITE_UTF16LE or SQLITE_UTF16BE, indicating that the user-supplied
1021** routine expects to be passed pointers to strings encoded using UTF-8,
1022** UTF-16 little-endian or UTF-16 big-endian respectively.
1023**
1024** A pointer to the user supplied routine must be passed as the fifth
1025** argument. If it is NULL, this is the same as deleting the collation
1026** sequence (so that SQLite cannot call it anymore). Each time the user
1027** supplied function is invoked, it is passed a copy of the void* passed as
1028** the fourth argument to sqlite3_create_collation() or
1029** sqlite3_create_collation16() as its first parameter.
1030**
1031** The remaining arguments to the user-supplied routine are two strings,
1032** each represented by a [length, data] pair and encoded in the encoding
1033** that was passed as the third argument when the collation sequence was
1034** registered. The user routine should return negative, zero or positive if
1035** the first string is less than, equal to, or greater than the second
1036** string. i.e. (STRING1 - STRING2).
1037*/
danielk19770202b292004-06-09 09:55:16 +00001038int sqlite3_create_collation(
1039 sqlite3*,
1040 const char *zName,
danielk19777cedc8d2004-06-10 10:50:08 +00001041 int eTextRep,
danielk19770202b292004-06-09 09:55:16 +00001042 void*,
1043 int(*xCompare)(void*,int,const void*,int,const void*)
1044);
1045int sqlite3_create_collation16(
1046 sqlite3*,
1047 const char *zName,
danielk19777cedc8d2004-06-10 10:50:08 +00001048 int eTextRep,
danielk19770202b292004-06-09 09:55:16 +00001049 void*,
1050 int(*xCompare)(void*,int,const void*,int,const void*)
1051);
1052
danielk19777cedc8d2004-06-10 10:50:08 +00001053/*
1054** To avoid having to register all collation sequences before a database
1055** can be used, a single callback function may be registered with the
1056** database handle to be called whenever an undefined collation sequence is
1057** required.
1058**
1059** If the function is registered using the sqlite3_collation_needed() API,
1060** then it is passed the names of undefined collation sequences as strings
1061** encoded in UTF-8. If sqlite3_collation_needed16() is used, the names
1062** are passed as UTF-16 in machine native byte order. A call to either
1063** function replaces any existing callback.
1064**
1065** When the user-function is invoked, the first argument passed is a copy
1066** of the second argument to sqlite3_collation_needed() or
1067** sqlite3_collation_needed16(). The second argument is the database
1068** handle. The third argument is one of SQLITE_UTF8, SQLITE_UTF16BE or
1069** SQLITE_UTF16LE, indicating the most desirable form of the collation
1070** sequence function required. The fourth parameter is the name of the
1071** required collation sequence.
1072**
1073** The collation sequence is returned to SQLite by a collation-needed
1074** callback using the sqlite3_create_collation() or
1075** sqlite3_create_collation16() APIs, described above.
1076*/
1077int sqlite3_collation_needed(
1078 sqlite3*,
1079 void*,
1080 void(*)(void*,sqlite3*,int eTextRep,const char*)
1081);
1082int sqlite3_collation_needed16(
1083 sqlite3*,
1084 void*,
1085 void(*)(void*,sqlite3*,int eTextRep,const void*)
1086);
1087
danielk19770202b292004-06-09 09:55:16 +00001088
drh382c0242001-10-06 16:33:02 +00001089#ifdef __cplusplus
1090} /* End of the 'extern "C"' block */
1091#endif
danielk19774adee202004-05-08 08:23:19 +00001092#endif