blob: 1ba84badbe632a4bd1556e7f4e492f6e826bb544 [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**
drhc8d30ac2002-04-12 10:08:59 +000015** @(#) $Id: sqlite.h.in,v 1.30 2002/04/12 10:08:59 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/*
drhb217a572000-08-22 13:40:18 +000022** The version of the SQLite library.
drh303aaa72000-08-17 10:22:34 +000023*/
drhb217a572000-08-22 13:40:18 +000024#define SQLITE_VERSION "--VERS--"
25
26/*
drh382c0242001-10-06 16:33:02 +000027** Make sure we can call this stuff from C++.
28*/
29#ifdef __cplusplus
30extern "C" {
31#endif
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*/
38extern const char sqlite_version[];
drh303aaa72000-08-17 10:22:34 +000039
40/*
drh297ecf12001-04-05 15:57:13 +000041** The SQLITE_UTF8 macro is defined if the library expects to see
42** UTF-8 encoded data. The SQLITE_ISO8859 macro is defined if the
43** iso8859 encoded should be used.
44*/
45#define SQLITE_--ENCODING-- 1
46
47/*
48** The following constant holds one of two strings, "UTF-8" or "iso8859",
49** depending on which character encoding the SQLite library expects to
50** see. The character encoding makes a difference for the LIKE and GLOB
51** operators and for the LENGTH() and SUBSTR() functions.
52*/
drhfbc3eab2001-04-06 16:13:42 +000053extern const char sqlite_encoding[];
drh297ecf12001-04-05 15:57:13 +000054
55/*
drh75897232000-05-29 14:26:00 +000056** Each open sqlite database is represented by an instance of the
57** following opaque structure.
58*/
59typedef struct sqlite sqlite;
60
61/*
62** A function to open a new sqlite database.
63**
64** If the database does not exist and mode indicates write
65** permission, then a new database is created. If the database
66** does not exist and mode does not indicate write permission,
67** then the open fails, an error message generated (if errmsg!=0)
68** and the function returns 0.
69**
70** If mode does not indicates user write permission, then the
71** database is opened read-only.
72**
73** The Truth: As currently implemented, all databases are opened
74** for writing all the time. Maybe someday we will provide the
75** ability to open a database readonly. The mode parameters is
76** provide in anticipation of that enhancement.
77*/
78sqlite *sqlite_open(const char *filename, int mode, char **errmsg);
79
80/*
81** A function to close the database.
82**
83** Call this function with a pointer to a structure that was previously
84** returned from sqlite_open() and the corresponding database will by closed.
85*/
86void sqlite_close(sqlite *);
87
88/*
89** The type for a callback function.
90*/
91typedef int (*sqlite_callback)(void*,int,char**, char**);
92
93/*
94** A function to executes one or more statements of SQL.
95**
96** If one or more of the SQL statements are queries, then
97** the callback function specified by the 3rd parameter is
98** invoked once for each row of the query result. This callback
99** should normally return 0. If the callback returns a non-zero
100** value then the query is aborted, all subsequent SQL statements
drh58b95762000-06-02 01:17:37 +0000101** are skipped and the sqlite_exec() function returns the SQLITE_ABORT.
drh75897232000-05-29 14:26:00 +0000102**
103** The 4th parameter is an arbitrary pointer that is passed
104** to the callback function as its first parameter.
105**
106** The 2nd parameter to the callback function is the number of
drhb19a2bc2001-09-16 00:13:26 +0000107** columns in the query result. The 3rd parameter to the callback
108** is an array of strings holding the values for each column.
109** The 4th parameter to the callback is an array of strings holding
110** the names of each column.
drh75897232000-05-29 14:26:00 +0000111**
112** The callback function may be NULL, even for queries. A NULL
113** callback is not an error. It just means that no callback
114** will be invoked.
115**
116** If an error occurs while parsing or evaluating the SQL (but
117** not while executing the callback) then an appropriate error
118** message is written into memory obtained from malloc() and
drhb19a2bc2001-09-16 00:13:26 +0000119** *errmsg is made to point to that message. The calling function
120** is responsible for freeing the memory that holds the error
121** message. If errmsg==NULL, then no error message is ever written.
122**
123** The return value is is SQLITE_OK if there are no errors and
124** some other return code if there is an error. The particular
125** return value depends on the type of error.
drh58b95762000-06-02 01:17:37 +0000126**
127** If the query could not be executed because a database file is
drh2dfbbca2000-07-28 14:32:48 +0000128** locked or busy, then this function returns SQLITE_BUSY. (This
129** behavior can be modified somewhat using the sqlite_busy_handler()
drhb19a2bc2001-09-16 00:13:26 +0000130** and sqlite_busy_timeout() functions below.)
drh75897232000-05-29 14:26:00 +0000131*/
132int sqlite_exec(
133 sqlite*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000134 const char *sql, /* SQL to be executed */
drh75897232000-05-29 14:26:00 +0000135 sqlite_callback, /* Callback function */
136 void *, /* 1st argument to callback function */
137 char **errmsg /* Error msg written here */
138);
139
drh58b95762000-06-02 01:17:37 +0000140/*
drh98699b52000-10-09 12:57:00 +0000141** Return values for sqlite_exec()
drh58b95762000-06-02 01:17:37 +0000142*/
drh717e6402001-09-27 03:22:32 +0000143#define SQLITE_OK 0 /* Successful result */
144#define SQLITE_ERROR 1 /* SQL error or missing database */
145#define SQLITE_INTERNAL 2 /* An internal logic error in SQLite */
146#define SQLITE_PERM 3 /* Access permission denied */
147#define SQLITE_ABORT 4 /* Callback routine requested an abort */
148#define SQLITE_BUSY 5 /* The database file is locked */
149#define SQLITE_LOCKED 6 /* A table in the database is locked */
150#define SQLITE_NOMEM 7 /* A malloc() failed */
151#define SQLITE_READONLY 8 /* Attempt to write a readonly database */
152#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite_interrupt() */
153#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
154#define SQLITE_CORRUPT 11 /* The database disk image is malformed */
155#define SQLITE_NOTFOUND 12 /* (Internal Only) Table or record not found */
156#define SQLITE_FULL 13 /* Insertion failed because database is full */
157#define SQLITE_CANTOPEN 14 /* Unable to open the database file */
158#define SQLITE_PROTOCOL 15 /* Database lock protocol error */
159#define SQLITE_EMPTY 16 /* (Internal Only) Database table is empty */
160#define SQLITE_SCHEMA 17 /* The database schema changed */
161#define SQLITE_TOOBIG 18 /* Too much data for one row of a table */
162#define SQLITE_CONSTRAINT 19 /* Abort due to contraint violation */
drh8aff1012001-12-22 14:49:24 +0000163#define SQLITE_MISMATCH 20 /* Data type mismatch */
drh717e6402001-09-27 03:22:32 +0000164
drhaf9ff332002-01-16 21:00:27 +0000165/*
166** Each entry in an SQLite table has a unique integer key. (The key is
167** the value of the INTEGER PRIMARY KEY column if there is such a column,
168** otherwise the key is generated at random. The unique key is always
169** available as the ROWID, OID, or _ROWID_ column.) The following routine
170** returns the integer key of the most recent insert in the database.
171**
172** This function is similar to the mysql_insert_id() function from MySQL.
173*/
174int sqlite_last_insert_rowid(sqlite*);
175
drhc8d30ac2002-04-12 10:08:59 +0000176/*
177** This function returns the number of database rows that were changed
178** (or inserted or deleted) by the most recent called sqlite_exec().
179**
180** All changes are counted, even if they were later undone by a
181** ROLLBACK or ABORT. Except, changes associated with creating and
182** dropping tables are not counted.
183**
184** If a callback invokes sqlite_exec() recursively, then the changes
185** in the inner, recursive call are counted together with the changes
186** in the outer call.
187**
188** SQLite implements the command "DELETE FROM table" without a WHERE clause
189** by dropping and recreating the table. (This is much faster than going
190** through and deleting individual elements form the table.) Because of
191** this optimization, the change count for "DELETE FROM table" will be
192** zero regardless of the number of elements that were originally in the
193** table. To get an accurate count of the number of rows deleted, use
194** "DELETE FROM table WHERE 1" instead.
195*/
196int sqlite_changes(sqlite*);
197
drh717e6402001-09-27 03:22:32 +0000198/* If the parameter to this routine is one of the return value constants
199** defined above, then this routine returns a constant text string which
200** descripts (in English) the meaning of the return value.
201*/
drh6d4abfb2001-10-22 02:58:08 +0000202const char *sqlite_error_string(int);
203#define sqliteErrStr sqlite_error_string /* Legacy. Do not use in new code. */
drh4c504392000-10-16 22:06:40 +0000204
205/* This function causes any pending database operation to abort and
206** return at its earliest opportunity. This routine is typically
drh66b89c82000-11-28 20:47:17 +0000207** called in response to a user action such as pressing "Cancel"
drh4c504392000-10-16 22:06:40 +0000208** or Ctrl-C where the user wants a long query operation to halt
209** immediately.
210*/
211void sqlite_interrupt(sqlite*);
212
drheec553b2000-06-02 01:51:20 +0000213
drh75897232000-05-29 14:26:00 +0000214/* This function returns true if the given input string comprises
215** one or more complete SQL statements.
216**
217** The algorithm is simple. If the last token other than spaces
218** and comments is a semicolon, then return true. otherwise return
219** false.
220*/
221int sqlite_complete(const char *sql);
222
drh2dfbbca2000-07-28 14:32:48 +0000223/*
224** This routine identifies a callback function that is invoked
225** whenever an attempt is made to open a database table that is
226** currently locked by another process or thread. If the busy callback
227** is NULL, then sqlite_exec() returns SQLITE_BUSY immediately if
228** it finds a locked table. If the busy callback is not NULL, then
229** sqlite_exec() invokes the callback with three arguments. The
230** second argument is the name of the locked table and the third
231** argument is the number of times the table has been busy. If the
232** busy callback returns 0, then sqlite_exec() immediately returns
233** SQLITE_BUSY. If the callback returns non-zero, then sqlite_exec()
234** tries to open the table again and the cycle repeats.
235**
236** The default busy callback is NULL.
237**
238** Sqlite is re-entrant, so the busy handler may start a new query.
239** (It is not clear why anyone would every want to do this, but it
240** is allowed, in theory.) But the busy handler may not close the
241** database. Closing the database from a busy handler will delete
242** data structures out from under the executing query and will
243** probably result in a coredump.
244*/
245void sqlite_busy_handler(sqlite*, int(*)(void*,const char*,int), void*);
246
247/*
248** This routine sets a busy handler that sleeps for a while when a
249** table is locked. The handler will sleep multiple times until
250** at least "ms" milleseconds of sleeping have been done. After
251** "ms" milleseconds of sleeping, the handler returns 0 which
252** causes sqlite_exec() to return SQLITE_BUSY.
253**
254** Calling this routine with an argument less than or equal to zero
255** turns off all busy handlers.
256*/
257void sqlite_busy_timeout(sqlite*, int ms);
258
drhe3710332000-09-29 13:30:53 +0000259/*
260** This next routine is really just a wrapper around sqlite_exec().
261** Instead of invoking a user-supplied callback for each row of the
262** result, this routine remembers each row of the result in memory
263** obtained from malloc(), then returns all of the result after the
drha18c5682000-10-08 22:20:57 +0000264** query has finished.
265**
266** As an example, suppose the query result where this table:
267**
268** Name | Age
269** -----------------------
270** Alice | 43
271** Bob | 28
272** Cindy | 21
273**
274** If the 3rd argument were &azResult then after the function returns
drh98699b52000-10-09 12:57:00 +0000275** azResult will contain the following data:
drha18c5682000-10-08 22:20:57 +0000276**
277** azResult[0] = "Name";
278** azResult[1] = "Age";
279** azResult[2] = "Alice";
280** azResult[3] = "43";
281** azResult[4] = "Bob";
282** azResult[5] = "28";
283** azResult[6] = "Cindy";
284** azResult[7] = "21";
285**
286** Notice that there is an extra row of data containing the column
287** headers. But the *nrow return value is still 3. *ncolumn is
288** set to 2. In general, the number of values inserted into azResult
289** will be ((*nrow) + 1)*(*ncolumn).
290**
291** After the calling function has finished using the result, it should
292** pass the result data pointer to sqlite_free_table() in order to
293** release the memory that was malloc-ed. Because of the way the
294** malloc() happens, the calling function must not try to call
295** malloc() directly. Only sqlite_free_table() is able to release
296** the memory properly and safely.
drhe3710332000-09-29 13:30:53 +0000297**
298** The return value of this routine is the same as from sqlite_exec().
299*/
300int sqlite_get_table(
301 sqlite*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000302 const char *sql, /* SQL to be executed */
drhe3710332000-09-29 13:30:53 +0000303 char ***resultp, /* Result written to a char *[] that this points to */
304 int *nrow, /* Number of result rows written here */
305 int *ncolumn, /* Number of result columns written here */
306 char **errmsg /* Error msg written here */
307);
308
309/*
310** Call this routine to free the memory that sqlite_get_table() allocated.
311*/
312void sqlite_free_table(char **result);
313
drha18c5682000-10-08 22:20:57 +0000314/*
315** The following routines are wrappers around sqlite_exec() and
drh98699b52000-10-09 12:57:00 +0000316** sqlite_get_table(). The only difference between the routines that
drha18c5682000-10-08 22:20:57 +0000317** follow and the originals is that the second argument to the
318** routines that follow is really a printf()-style format
319** string describing the SQL to be executed. Arguments to the format
320** string appear at the end of the argument list.
321**
322** All of the usual printf formatting options apply. In addition, there
323** is a "%q" option. %q works like %s in that it substitutes a null-terminated
drh66b89c82000-11-28 20:47:17 +0000324** string from the argument list. But %q also doubles every '\'' character.
drha18c5682000-10-08 22:20:57 +0000325** %q is designed for use inside a string literal. By doubling each '\''
drh66b89c82000-11-28 20:47:17 +0000326** character it escapes that character and allows it to be inserted into
drha18c5682000-10-08 22:20:57 +0000327** the string.
328**
329** For example, so some string variable contains text as follows:
330**
331** char *zText = "It's a happy day!";
332**
333** We can use this text in an SQL statement as follows:
334**
335** sqlite_exec_printf(db, "INSERT INTO table VALUES('%q')",
336** callback1, 0, 0, zText);
337**
338** Because the %q format string is used, the '\'' character in zText
339** is escaped and the SQL generated is as follows:
340**
341** INSERT INTO table1 VALUES('It''s a happy day!')
342**
343** This is correct. Had we used %s instead of %q, the generated SQL
344** would have looked like this:
345**
346** INSERT INTO table1 VALUES('It's a happy day!');
347**
348** This second example is an SQL syntax error. As a general rule you
349** should always use %q instead of %s when inserting text into a string
350** literal.
351*/
352int sqlite_exec_printf(
353 sqlite*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000354 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000355 sqlite_callback, /* Callback function */
356 void *, /* 1st argument to callback function */
357 char **errmsg, /* Error msg written here */
358 ... /* Arguments to the format string. */
359);
360int sqlite_exec_vprintf(
361 sqlite*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000362 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000363 sqlite_callback, /* Callback function */
364 void *, /* 1st argument to callback function */
365 char **errmsg, /* Error msg written here */
366 va_list ap /* Arguments to the format string. */
367);
368int sqlite_get_table_printf(
369 sqlite*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000370 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000371 char ***resultp, /* Result written to a char *[] that this points to */
372 int *nrow, /* Number of result rows written here */
373 int *ncolumn, /* Number of result columns written here */
374 char **errmsg, /* Error msg written here */
375 ... /* Arguments to the format string */
376);
377int sqlite_get_table_vprintf(
378 sqlite*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000379 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000380 char ***resultp, /* Result written to a char *[] that this points to */
381 int *nrow, /* Number of result rows written here */
382 int *ncolumn, /* Number of result columns written here */
383 char **errmsg, /* Error msg written here */
384 va_list ap /* Arguments to the format string */
385);
386
drh8e0a2f92002-02-23 23:45:45 +0000387/*
drh5191b7e2002-03-08 02:12:00 +0000388** Windows systems should call this routine to free memory that
389** is returned in the in the errmsg parameter of sqlite_open() when
390** SQLite is a DLL. For some reason, it does not work to call free()
391** directly.
392*/
393void sqlite_freemem(void *p);
394
395/*
396** Windows systems need functions to call to return the sqlite_version
397** and sqlite_encoding strings.
398*/
399const char *sqlite_libversion(void);
400const char *sqlite_libencoding(void);
401
402/*
drh1350b032002-02-27 19:00:20 +0000403** A pointer to the following structure is used to communicate with
404** the implementations of user-defined functions.
405*/
406typedef struct sqlite_func sqlite_func;
407
408/*
drh8e0a2f92002-02-23 23:45:45 +0000409** Use the following routines to create new user-defined functions. See
410** the documentation for details.
411*/
412int sqlite_create_function(
drh1350b032002-02-27 19:00:20 +0000413 sqlite*, /* Database where the new function is registered */
414 const char *zName, /* Name of the new function */
415 int nArg, /* Number of arguments. -1 means any number */
416 void (*xFunc)(sqlite_func*,int,const char**), /* C code to implement */
417 void *pUserData /* Available via the sqlite_user_data() call */
drh8e0a2f92002-02-23 23:45:45 +0000418);
419int sqlite_create_aggregate(
drh1350b032002-02-27 19:00:20 +0000420 sqlite*, /* Database where the new function is registered */
421 const char *zName, /* Name of the function */
422 int nArg, /* Number of arguments */
423 void (*xStep)(sqlite_func*,int,const char**), /* Called for each row */
424 void (*xFinalize)(sqlite_func*), /* Called once to get final result */
425 void *pUserData /* Available via the sqlite_user_data() call */
drh8e0a2f92002-02-23 23:45:45 +0000426);
427
428/*
429** The user function implementations call one of the following four routines
430** in order to return their results. The first parameter to each of these
drhdd5baa92002-02-27 19:50:59 +0000431** routines is a copy of the first argument to xFunc() or xFinialize().
drh1350b032002-02-27 19:00:20 +0000432** The second parameter to these routines is the result to be returned.
433** A NULL can be passed as the second parameter to sqlite_set_result_string()
434** in order to return a NULL result.
drh8e0a2f92002-02-23 23:45:45 +0000435**
436** The 3rd argument to _string and _error is the number of characters to
437** take from the string. If this argument is negative, then all characters
438** up to and including the first '\000' are used.
drh1350b032002-02-27 19:00:20 +0000439**
440** The sqlite_set_result_string() function allocates a buffer to hold the
441** result and returns a pointer to this buffer. The calling routine
442** (that is, the implmentation of a user function) can alter the content
443** of this buffer if desired.
drh8e0a2f92002-02-23 23:45:45 +0000444*/
drh1350b032002-02-27 19:00:20 +0000445char *sqlite_set_result_string(sqlite_func*,const char*,int);
446void sqlite_set_result_int(sqlite_func*,int);
447void sqlite_set_result_double(sqlite_func*,double);
448void sqlite_set_result_error(sqlite_func*,const char*,int);
449
450/*
451** The pUserData parameter to the sqlite_create_function() and
452** sqlite_create_aggregate() routines used to register user functions
453** is available to the implementation of the function using this
454** call.
455*/
456void *sqlite_user_data(sqlite_func*);
457
458/*
drhdd5baa92002-02-27 19:50:59 +0000459** Aggregate functions use the following routine to allocate
460** a structure for storing their state. The first time this routine
drh1350b032002-02-27 19:00:20 +0000461** is called for a particular aggregate, a new structure of size nBytes
462** is allocated, zeroed, and returned. On subsequent calls (for the
463** same aggregate instance) the same buffer is returned. The implementation
464** of the aggregate can use the returned buffer to accumulate data.
465**
466** The buffer allocated is freed automatically be SQLite.
467*/
468void *sqlite_aggregate_context(sqlite_func*, int nBytes);
469
470/*
drhdd5baa92002-02-27 19:50:59 +0000471** The next routine returns the number of calls to xStep for a particular
472** aggregate function instance. The current call to xStep counts so this
473** routine always returns at least 1.
drh1350b032002-02-27 19:00:20 +0000474*/
475int sqlite_aggregate_count(sqlite_func*);
drh8e0a2f92002-02-23 23:45:45 +0000476
drh382c0242001-10-06 16:33:02 +0000477#ifdef __cplusplus
478} /* End of the 'extern "C"' block */
479#endif
480
drh75897232000-05-29 14:26:00 +0000481#endif /* _SQLITE_H_ */