blob: 251bd35d8f08bb73941fe17367cb124461ec0546 [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**
drh1350b032002-02-27 19:00:20 +000015** @(#) $Id: sqlite.h.in,v 1.27 2002/02/27 19:00:22 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
drh717e6402001-09-27 03:22:32 +0000176/* If the parameter to this routine is one of the return value constants
177** defined above, then this routine returns a constant text string which
178** descripts (in English) the meaning of the return value.
179*/
drh6d4abfb2001-10-22 02:58:08 +0000180const char *sqlite_error_string(int);
181#define sqliteErrStr sqlite_error_string /* Legacy. Do not use in new code. */
drh4c504392000-10-16 22:06:40 +0000182
183/* This function causes any pending database operation to abort and
184** return at its earliest opportunity. This routine is typically
drh66b89c82000-11-28 20:47:17 +0000185** called in response to a user action such as pressing "Cancel"
drh4c504392000-10-16 22:06:40 +0000186** or Ctrl-C where the user wants a long query operation to halt
187** immediately.
188*/
189void sqlite_interrupt(sqlite*);
190
drheec553b2000-06-02 01:51:20 +0000191
drh75897232000-05-29 14:26:00 +0000192/* This function returns true if the given input string comprises
193** one or more complete SQL statements.
194**
195** The algorithm is simple. If the last token other than spaces
196** and comments is a semicolon, then return true. otherwise return
197** false.
198*/
199int sqlite_complete(const char *sql);
200
drh2dfbbca2000-07-28 14:32:48 +0000201/*
202** This routine identifies a callback function that is invoked
203** whenever an attempt is made to open a database table that is
204** currently locked by another process or thread. If the busy callback
205** is NULL, then sqlite_exec() returns SQLITE_BUSY immediately if
206** it finds a locked table. If the busy callback is not NULL, then
207** sqlite_exec() invokes the callback with three arguments. The
208** second argument is the name of the locked table and the third
209** argument is the number of times the table has been busy. If the
210** busy callback returns 0, then sqlite_exec() immediately returns
211** SQLITE_BUSY. If the callback returns non-zero, then sqlite_exec()
212** tries to open the table again and the cycle repeats.
213**
214** The default busy callback is NULL.
215**
216** Sqlite is re-entrant, so the busy handler may start a new query.
217** (It is not clear why anyone would every want to do this, but it
218** is allowed, in theory.) But the busy handler may not close the
219** database. Closing the database from a busy handler will delete
220** data structures out from under the executing query and will
221** probably result in a coredump.
222*/
223void sqlite_busy_handler(sqlite*, int(*)(void*,const char*,int), void*);
224
225/*
226** This routine sets a busy handler that sleeps for a while when a
227** table is locked. The handler will sleep multiple times until
228** at least "ms" milleseconds of sleeping have been done. After
229** "ms" milleseconds of sleeping, the handler returns 0 which
230** causes sqlite_exec() to return SQLITE_BUSY.
231**
232** Calling this routine with an argument less than or equal to zero
233** turns off all busy handlers.
234*/
235void sqlite_busy_timeout(sqlite*, int ms);
236
drhe3710332000-09-29 13:30:53 +0000237/*
238** This next routine is really just a wrapper around sqlite_exec().
239** Instead of invoking a user-supplied callback for each row of the
240** result, this routine remembers each row of the result in memory
241** obtained from malloc(), then returns all of the result after the
drha18c5682000-10-08 22:20:57 +0000242** query has finished.
243**
244** As an example, suppose the query result where this table:
245**
246** Name | Age
247** -----------------------
248** Alice | 43
249** Bob | 28
250** Cindy | 21
251**
252** If the 3rd argument were &azResult then after the function returns
drh98699b52000-10-09 12:57:00 +0000253** azResult will contain the following data:
drha18c5682000-10-08 22:20:57 +0000254**
255** azResult[0] = "Name";
256** azResult[1] = "Age";
257** azResult[2] = "Alice";
258** azResult[3] = "43";
259** azResult[4] = "Bob";
260** azResult[5] = "28";
261** azResult[6] = "Cindy";
262** azResult[7] = "21";
263**
264** Notice that there is an extra row of data containing the column
265** headers. But the *nrow return value is still 3. *ncolumn is
266** set to 2. In general, the number of values inserted into azResult
267** will be ((*nrow) + 1)*(*ncolumn).
268**
269** After the calling function has finished using the result, it should
270** pass the result data pointer to sqlite_free_table() in order to
271** release the memory that was malloc-ed. Because of the way the
272** malloc() happens, the calling function must not try to call
273** malloc() directly. Only sqlite_free_table() is able to release
274** the memory properly and safely.
drhe3710332000-09-29 13:30:53 +0000275**
276** The return value of this routine is the same as from sqlite_exec().
277*/
278int sqlite_get_table(
279 sqlite*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000280 const char *sql, /* SQL to be executed */
drhe3710332000-09-29 13:30:53 +0000281 char ***resultp, /* Result written to a char *[] that this points to */
282 int *nrow, /* Number of result rows written here */
283 int *ncolumn, /* Number of result columns written here */
284 char **errmsg /* Error msg written here */
285);
286
287/*
288** Call this routine to free the memory that sqlite_get_table() allocated.
289*/
290void sqlite_free_table(char **result);
291
drha18c5682000-10-08 22:20:57 +0000292/*
293** The following routines are wrappers around sqlite_exec() and
drh98699b52000-10-09 12:57:00 +0000294** sqlite_get_table(). The only difference between the routines that
drha18c5682000-10-08 22:20:57 +0000295** follow and the originals is that the second argument to the
296** routines that follow is really a printf()-style format
297** string describing the SQL to be executed. Arguments to the format
298** string appear at the end of the argument list.
299**
300** All of the usual printf formatting options apply. In addition, there
301** is a "%q" option. %q works like %s in that it substitutes a null-terminated
drh66b89c82000-11-28 20:47:17 +0000302** string from the argument list. But %q also doubles every '\'' character.
drha18c5682000-10-08 22:20:57 +0000303** %q is designed for use inside a string literal. By doubling each '\''
drh66b89c82000-11-28 20:47:17 +0000304** character it escapes that character and allows it to be inserted into
drha18c5682000-10-08 22:20:57 +0000305** the string.
306**
307** For example, so some string variable contains text as follows:
308**
309** char *zText = "It's a happy day!";
310**
311** We can use this text in an SQL statement as follows:
312**
313** sqlite_exec_printf(db, "INSERT INTO table VALUES('%q')",
314** callback1, 0, 0, zText);
315**
316** Because the %q format string is used, the '\'' character in zText
317** is escaped and the SQL generated is as follows:
318**
319** INSERT INTO table1 VALUES('It''s a happy day!')
320**
321** This is correct. Had we used %s instead of %q, the generated SQL
322** would have looked like this:
323**
324** INSERT INTO table1 VALUES('It's a happy day!');
325**
326** This second example is an SQL syntax error. As a general rule you
327** should always use %q instead of %s when inserting text into a string
328** literal.
329*/
330int sqlite_exec_printf(
331 sqlite*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000332 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000333 sqlite_callback, /* Callback function */
334 void *, /* 1st argument to callback function */
335 char **errmsg, /* Error msg written here */
336 ... /* Arguments to the format string. */
337);
338int sqlite_exec_vprintf(
339 sqlite*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000340 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000341 sqlite_callback, /* Callback function */
342 void *, /* 1st argument to callback function */
343 char **errmsg, /* Error msg written here */
344 va_list ap /* Arguments to the format string. */
345);
346int sqlite_get_table_printf(
347 sqlite*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000348 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000349 char ***resultp, /* Result written to a char *[] that this points to */
350 int *nrow, /* Number of result rows written here */
351 int *ncolumn, /* Number of result columns written here */
352 char **errmsg, /* Error msg written here */
353 ... /* Arguments to the format string */
354);
355int sqlite_get_table_vprintf(
356 sqlite*, /* An open database */
drh9f71c2e2001-11-03 23:57:09 +0000357 const char *sqlFormat, /* printf-style format string for the SQL */
drha18c5682000-10-08 22:20:57 +0000358 char ***resultp, /* Result written to a char *[] that this points to */
359 int *nrow, /* Number of result rows written here */
360 int *ncolumn, /* Number of result columns written here */
361 char **errmsg, /* Error msg written here */
362 va_list ap /* Arguments to the format string */
363);
364
drh8e0a2f92002-02-23 23:45:45 +0000365/*
drh1350b032002-02-27 19:00:20 +0000366** A pointer to the following structure is used to communicate with
367** the implementations of user-defined functions.
368*/
369typedef struct sqlite_func sqlite_func;
370
371/*
drh8e0a2f92002-02-23 23:45:45 +0000372** Use the following routines to create new user-defined functions. See
373** the documentation for details.
374*/
375int sqlite_create_function(
drh1350b032002-02-27 19:00:20 +0000376 sqlite*, /* Database where the new function is registered */
377 const char *zName, /* Name of the new function */
378 int nArg, /* Number of arguments. -1 means any number */
379 void (*xFunc)(sqlite_func*,int,const char**), /* C code to implement */
380 void *pUserData /* Available via the sqlite_user_data() call */
drh8e0a2f92002-02-23 23:45:45 +0000381);
382int sqlite_create_aggregate(
drh1350b032002-02-27 19:00:20 +0000383 sqlite*, /* Database where the new function is registered */
384 const char *zName, /* Name of the function */
385 int nArg, /* Number of arguments */
386 void (*xStep)(sqlite_func*,int,const char**), /* Called for each row */
387 void (*xFinalize)(sqlite_func*), /* Called once to get final result */
388 void *pUserData /* Available via the sqlite_user_data() call */
drh8e0a2f92002-02-23 23:45:45 +0000389);
390
391/*
392** The user function implementations call one of the following four routines
393** in order to return their results. The first parameter to each of these
drh1350b032002-02-27 19:00:20 +0000394** routines is a copy of the first argument to xFunc() or xFinialize()
395** The second parameter to these routines is the result to be returned.
396** A NULL can be passed as the second parameter to sqlite_set_result_string()
397** in order to return a NULL result.
drh8e0a2f92002-02-23 23:45:45 +0000398**
399** The 3rd argument to _string and _error is the number of characters to
400** take from the string. If this argument is negative, then all characters
401** up to and including the first '\000' are used.
drh1350b032002-02-27 19:00:20 +0000402**
403** The sqlite_set_result_string() function allocates a buffer to hold the
404** result and returns a pointer to this buffer. The calling routine
405** (that is, the implmentation of a user function) can alter the content
406** of this buffer if desired.
drh8e0a2f92002-02-23 23:45:45 +0000407*/
drh1350b032002-02-27 19:00:20 +0000408char *sqlite_set_result_string(sqlite_func*,const char*,int);
409void sqlite_set_result_int(sqlite_func*,int);
410void sqlite_set_result_double(sqlite_func*,double);
411void sqlite_set_result_error(sqlite_func*,const char*,int);
412
413/*
414** The pUserData parameter to the sqlite_create_function() and
415** sqlite_create_aggregate() routines used to register user functions
416** is available to the implementation of the function using this
417** call.
418*/
419void *sqlite_user_data(sqlite_func*);
420
421/*
422** User aggregate functions use the following routine to allocate
423** a structure for storing their context. The first time this routine
424** is called for a particular aggregate, a new structure of size nBytes
425** is allocated, zeroed, and returned. On subsequent calls (for the
426** same aggregate instance) the same buffer is returned. The implementation
427** of the aggregate can use the returned buffer to accumulate data.
428**
429** The buffer allocated is freed automatically be SQLite.
430*/
431void *sqlite_aggregate_context(sqlite_func*, int nBytes);
432
433/*
434** The next return returns the number of calls to xStep for a particular
435** aggregate function instance. The current call to xStep counts so the
436** function always returns at least 1.
437*/
438int sqlite_aggregate_count(sqlite_func*);
drh8e0a2f92002-02-23 23:45:45 +0000439
drh382c0242001-10-06 16:33:02 +0000440#ifdef __cplusplus
441} /* End of the 'extern "C"' block */
442#endif
443
drh75897232000-05-29 14:26:00 +0000444#endif /* _SQLITE_H_ */