blob: deadbb0655a5870afd10564c8d927ddf62ed1935 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** This header file defines the interface that the sqlite library
25** presents to client programs.
26**
drh66b89c82000-11-28 20:47:17 +000027** @(#) $Id: sqlite.h.in,v 1.7 2000/11/28 20:47:20 drh Exp $
drh75897232000-05-29 14:26:00 +000028*/
29#ifndef _SQLITE_H_
30#define _SQLITE_H_
drha18c5682000-10-08 22:20:57 +000031#include <stdarg.h> /* Needed for the definition of va_list */
drh75897232000-05-29 14:26:00 +000032
33/*
drhb217a572000-08-22 13:40:18 +000034** The version of the SQLite library.
drh303aaa72000-08-17 10:22:34 +000035*/
drhb217a572000-08-22 13:40:18 +000036#define SQLITE_VERSION "--VERS--"
37
38/*
39** The version string is also compiled into the library so that a program
40** can check to make sure that the lib*.a file and the *.h file are from
41** the same version.
42*/
43extern const char sqlite_version[];
drh303aaa72000-08-17 10:22:34 +000044
45/*
drh75897232000-05-29 14:26:00 +000046** Each open sqlite database is represented by an instance of the
47** following opaque structure.
48*/
49typedef struct sqlite sqlite;
50
51/*
52** A function to open a new sqlite database.
53**
54** If the database does not exist and mode indicates write
55** permission, then a new database is created. If the database
56** does not exist and mode does not indicate write permission,
57** then the open fails, an error message generated (if errmsg!=0)
58** and the function returns 0.
59**
60** If mode does not indicates user write permission, then the
61** database is opened read-only.
62**
63** The Truth: As currently implemented, all databases are opened
64** for writing all the time. Maybe someday we will provide the
65** ability to open a database readonly. The mode parameters is
66** provide in anticipation of that enhancement.
67*/
68sqlite *sqlite_open(const char *filename, int mode, char **errmsg);
69
70/*
71** A function to close the database.
72**
73** Call this function with a pointer to a structure that was previously
74** returned from sqlite_open() and the corresponding database will by closed.
75*/
76void sqlite_close(sqlite *);
77
78/*
79** The type for a callback function.
80*/
81typedef int (*sqlite_callback)(void*,int,char**, char**);
82
83/*
84** A function to executes one or more statements of SQL.
85**
86** If one or more of the SQL statements are queries, then
87** the callback function specified by the 3rd parameter is
88** invoked once for each row of the query result. This callback
89** should normally return 0. If the callback returns a non-zero
90** value then the query is aborted, all subsequent SQL statements
drh58b95762000-06-02 01:17:37 +000091** are skipped and the sqlite_exec() function returns the SQLITE_ABORT.
drh75897232000-05-29 14:26:00 +000092**
93** The 4th parameter is an arbitrary pointer that is passed
94** to the callback function as its first parameter.
95**
96** The 2nd parameter to the callback function is the number of
97** columns in the query result. The 3rd parameter is an array
98** of string holding the values for each column. The 4th parameter
99** is an array of strings holding the names of each column.
100**
101** The callback function may be NULL, even for queries. A NULL
102** callback is not an error. It just means that no callback
103** will be invoked.
104**
105** If an error occurs while parsing or evaluating the SQL (but
106** not while executing the callback) then an appropriate error
107** message is written into memory obtained from malloc() and
108** *errmsg is made to point to that message. If errmsg==NULL,
109** then no error message is ever written. The return value is
drh2dfbbca2000-07-28 14:32:48 +0000110** SQLITE_ERROR if an error occurs. The calling function is
111** responsible for freeing the memory that holds the error
112** message.
drh58b95762000-06-02 01:17:37 +0000113**
114** If the query could not be executed because a database file is
drh2dfbbca2000-07-28 14:32:48 +0000115** locked or busy, then this function returns SQLITE_BUSY. (This
116** behavior can be modified somewhat using the sqlite_busy_handler()
117** and sqlite_busy_timeout() functions below.) If the query could
118** not be executed because a file is missing or has incorrect
119** permissions, this function returns SQLITE_ERROR.
drh75897232000-05-29 14:26:00 +0000120*/
121int sqlite_exec(
122 sqlite*, /* An open database */
123 char *sql, /* SQL to be executed */
124 sqlite_callback, /* Callback function */
125 void *, /* 1st argument to callback function */
126 char **errmsg /* Error msg written here */
127);
128
drh58b95762000-06-02 01:17:37 +0000129/*
drh98699b52000-10-09 12:57:00 +0000130** Return values for sqlite_exec()
drh58b95762000-06-02 01:17:37 +0000131*/
132#define SQLITE_OK 0 /* Successful result */
drh4c504392000-10-16 22:06:40 +0000133#define SQLITE_ERROR 1 /* SQL error or missing database */
134#define SQLITE_INTERNAL 2 /* An internal logic error in SQLite */
drheec553b2000-06-02 01:51:20 +0000135#define SQLITE_PERM 3 /* Access permission denied */
136#define SQLITE_ABORT 4 /* Callback routine requested an abort */
137#define SQLITE_BUSY 5 /* One or more database files are locked */
138#define SQLITE_NOMEM 6 /* A malloc() failed */
139#define SQLITE_READONLY 7 /* Attempt to write a readonly database */
drh4c504392000-10-16 22:06:40 +0000140#define SQLITE_INTERRUPT 8 /* Operation terminated by sqlite_interrupt() */
141
142/* This function causes any pending database operation to abort and
143** return at its earliest opportunity. This routine is typically
drh66b89c82000-11-28 20:47:17 +0000144** called in response to a user action such as pressing "Cancel"
drh4c504392000-10-16 22:06:40 +0000145** or Ctrl-C where the user wants a long query operation to halt
146** immediately.
147*/
148void sqlite_interrupt(sqlite*);
149
drheec553b2000-06-02 01:51:20 +0000150
drh75897232000-05-29 14:26:00 +0000151/* This function returns true if the given input string comprises
152** one or more complete SQL statements.
153**
154** The algorithm is simple. If the last token other than spaces
155** and comments is a semicolon, then return true. otherwise return
156** false.
157*/
158int sqlite_complete(const char *sql);
159
drh2dfbbca2000-07-28 14:32:48 +0000160/*
161** This routine identifies a callback function that is invoked
162** whenever an attempt is made to open a database table that is
163** currently locked by another process or thread. If the busy callback
164** is NULL, then sqlite_exec() returns SQLITE_BUSY immediately if
165** it finds a locked table. If the busy callback is not NULL, then
166** sqlite_exec() invokes the callback with three arguments. The
167** second argument is the name of the locked table and the third
168** argument is the number of times the table has been busy. If the
169** busy callback returns 0, then sqlite_exec() immediately returns
170** SQLITE_BUSY. If the callback returns non-zero, then sqlite_exec()
171** tries to open the table again and the cycle repeats.
172**
173** The default busy callback is NULL.
174**
175** Sqlite is re-entrant, so the busy handler may start a new query.
176** (It is not clear why anyone would every want to do this, but it
177** is allowed, in theory.) But the busy handler may not close the
178** database. Closing the database from a busy handler will delete
179** data structures out from under the executing query and will
180** probably result in a coredump.
181*/
182void sqlite_busy_handler(sqlite*, int(*)(void*,const char*,int), void*);
183
184/*
185** This routine sets a busy handler that sleeps for a while when a
186** table is locked. The handler will sleep multiple times until
187** at least "ms" milleseconds of sleeping have been done. After
188** "ms" milleseconds of sleeping, the handler returns 0 which
189** causes sqlite_exec() to return SQLITE_BUSY.
190**
191** Calling this routine with an argument less than or equal to zero
192** turns off all busy handlers.
193*/
194void sqlite_busy_timeout(sqlite*, int ms);
195
drhe3710332000-09-29 13:30:53 +0000196/*
197** This next routine is really just a wrapper around sqlite_exec().
198** Instead of invoking a user-supplied callback for each row of the
199** result, this routine remembers each row of the result in memory
200** obtained from malloc(), then returns all of the result after the
drha18c5682000-10-08 22:20:57 +0000201** query has finished.
202**
203** As an example, suppose the query result where this table:
204**
205** Name | Age
206** -----------------------
207** Alice | 43
208** Bob | 28
209** Cindy | 21
210**
211** If the 3rd argument were &azResult then after the function returns
drh98699b52000-10-09 12:57:00 +0000212** azResult will contain the following data:
drha18c5682000-10-08 22:20:57 +0000213**
214** azResult[0] = "Name";
215** azResult[1] = "Age";
216** azResult[2] = "Alice";
217** azResult[3] = "43";
218** azResult[4] = "Bob";
219** azResult[5] = "28";
220** azResult[6] = "Cindy";
221** azResult[7] = "21";
222**
223** Notice that there is an extra row of data containing the column
224** headers. But the *nrow return value is still 3. *ncolumn is
225** set to 2. In general, the number of values inserted into azResult
226** will be ((*nrow) + 1)*(*ncolumn).
227**
228** After the calling function has finished using the result, it should
229** pass the result data pointer to sqlite_free_table() in order to
230** release the memory that was malloc-ed. Because of the way the
231** malloc() happens, the calling function must not try to call
232** malloc() directly. Only sqlite_free_table() is able to release
233** the memory properly and safely.
drhe3710332000-09-29 13:30:53 +0000234**
235** The return value of this routine is the same as from sqlite_exec().
236*/
237int sqlite_get_table(
238 sqlite*, /* An open database */
239 char *sql, /* SQL to be executed */
240 char ***resultp, /* Result written to a char *[] that this points to */
241 int *nrow, /* Number of result rows written here */
242 int *ncolumn, /* Number of result columns written here */
243 char **errmsg /* Error msg written here */
244);
245
246/*
247** Call this routine to free the memory that sqlite_get_table() allocated.
248*/
249void sqlite_free_table(char **result);
250
drha18c5682000-10-08 22:20:57 +0000251/*
252** The following routines are wrappers around sqlite_exec() and
drh98699b52000-10-09 12:57:00 +0000253** sqlite_get_table(). The only difference between the routines that
drha18c5682000-10-08 22:20:57 +0000254** follow and the originals is that the second argument to the
255** routines that follow is really a printf()-style format
256** string describing the SQL to be executed. Arguments to the format
257** string appear at the end of the argument list.
258**
259** All of the usual printf formatting options apply. In addition, there
260** is a "%q" option. %q works like %s in that it substitutes a null-terminated
drh66b89c82000-11-28 20:47:17 +0000261** string from the argument list. But %q also doubles every '\'' character.
drha18c5682000-10-08 22:20:57 +0000262** %q is designed for use inside a string literal. By doubling each '\''
drh66b89c82000-11-28 20:47:17 +0000263** character it escapes that character and allows it to be inserted into
drha18c5682000-10-08 22:20:57 +0000264** the string.
265**
266** For example, so some string variable contains text as follows:
267**
268** char *zText = "It's a happy day!";
269**
270** We can use this text in an SQL statement as follows:
271**
272** sqlite_exec_printf(db, "INSERT INTO table VALUES('%q')",
273** callback1, 0, 0, zText);
274**
275** Because the %q format string is used, the '\'' character in zText
276** is escaped and the SQL generated is as follows:
277**
278** INSERT INTO table1 VALUES('It''s a happy day!')
279**
280** This is correct. Had we used %s instead of %q, the generated SQL
281** would have looked like this:
282**
283** INSERT INTO table1 VALUES('It's a happy day!');
284**
285** This second example is an SQL syntax error. As a general rule you
286** should always use %q instead of %s when inserting text into a string
287** literal.
288*/
289int sqlite_exec_printf(
290 sqlite*, /* An open database */
291 char *sqlFormat, /* printf-style format string for the SQL */
292 sqlite_callback, /* Callback function */
293 void *, /* 1st argument to callback function */
294 char **errmsg, /* Error msg written here */
295 ... /* Arguments to the format string. */
296);
297int sqlite_exec_vprintf(
298 sqlite*, /* An open database */
299 char *sqlFormat, /* printf-style format string for the SQL */
300 sqlite_callback, /* Callback function */
301 void *, /* 1st argument to callback function */
302 char **errmsg, /* Error msg written here */
303 va_list ap /* Arguments to the format string. */
304);
305int sqlite_get_table_printf(
306 sqlite*, /* An open database */
307 char *sqlFormat, /* printf-style format string for the SQL */
308 char ***resultp, /* Result written to a char *[] that this points to */
309 int *nrow, /* Number of result rows written here */
310 int *ncolumn, /* Number of result columns written here */
311 char **errmsg, /* Error msg written here */
312 ... /* Arguments to the format string */
313);
314int sqlite_get_table_vprintf(
315 sqlite*, /* An open database */
316 char *sqlFormat, /* printf-style format string for the SQL */
317 char ***resultp, /* Result written to a char *[] that this points to */
318 int *nrow, /* Number of result rows written here */
319 int *ncolumn, /* Number of result columns written here */
320 char **errmsg, /* Error msg written here */
321 va_list ap /* Arguments to the format string */
322);
323
drhe3710332000-09-29 13:30:53 +0000324
drh75897232000-05-29 14:26:00 +0000325#endif /* _SQLITE_H_ */