blob: 9b06b21525b339bd932c8d1c27a7e669760597e4 [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**
drhe3710332000-09-29 13:30:53 +000027** @(#) $Id: sqlite.h.in,v 1.3 2000/09/29 13:30:55 drh Exp $
drh75897232000-05-29 14:26:00 +000028*/
29#ifndef _SQLITE_H_
30#define _SQLITE_H_
31
32/*
drhb217a572000-08-22 13:40:18 +000033** The version of the SQLite library.
drh303aaa72000-08-17 10:22:34 +000034*/
drhb217a572000-08-22 13:40:18 +000035#define SQLITE_VERSION "--VERS--"
36
37/*
38** The version string is also compiled into the library so that a program
39** can check to make sure that the lib*.a file and the *.h file are from
40** the same version.
41*/
42extern const char sqlite_version[];
drh303aaa72000-08-17 10:22:34 +000043
44/*
drh75897232000-05-29 14:26:00 +000045** Each open sqlite database is represented by an instance of the
46** following opaque structure.
47*/
48typedef struct sqlite sqlite;
49
50/*
51** A function to open a new sqlite database.
52**
53** If the database does not exist and mode indicates write
54** permission, then a new database is created. If the database
55** does not exist and mode does not indicate write permission,
56** then the open fails, an error message generated (if errmsg!=0)
57** and the function returns 0.
58**
59** If mode does not indicates user write permission, then the
60** database is opened read-only.
61**
62** The Truth: As currently implemented, all databases are opened
63** for writing all the time. Maybe someday we will provide the
64** ability to open a database readonly. The mode parameters is
65** provide in anticipation of that enhancement.
66*/
67sqlite *sqlite_open(const char *filename, int mode, char **errmsg);
68
69/*
70** A function to close the database.
71**
72** Call this function with a pointer to a structure that was previously
73** returned from sqlite_open() and the corresponding database will by closed.
74*/
75void sqlite_close(sqlite *);
76
77/*
78** The type for a callback function.
79*/
80typedef int (*sqlite_callback)(void*,int,char**, char**);
81
82/*
83** A function to executes one or more statements of SQL.
84**
85** If one or more of the SQL statements are queries, then
86** the callback function specified by the 3rd parameter is
87** invoked once for each row of the query result. This callback
88** should normally return 0. If the callback returns a non-zero
89** value then the query is aborted, all subsequent SQL statements
drh58b95762000-06-02 01:17:37 +000090** are skipped and the sqlite_exec() function returns the SQLITE_ABORT.
drh75897232000-05-29 14:26:00 +000091**
92** The 4th parameter is an arbitrary pointer that is passed
93** to the callback function as its first parameter.
94**
95** The 2nd parameter to the callback function is the number of
96** columns in the query result. The 3rd parameter is an array
97** of string holding the values for each column. The 4th parameter
98** is an array of strings holding the names of each column.
99**
100** The callback function may be NULL, even for queries. A NULL
101** callback is not an error. It just means that no callback
102** will be invoked.
103**
104** If an error occurs while parsing or evaluating the SQL (but
105** not while executing the callback) then an appropriate error
106** message is written into memory obtained from malloc() and
107** *errmsg is made to point to that message. If errmsg==NULL,
108** then no error message is ever written. The return value is
drh2dfbbca2000-07-28 14:32:48 +0000109** SQLITE_ERROR if an error occurs. The calling function is
110** responsible for freeing the memory that holds the error
111** message.
drh58b95762000-06-02 01:17:37 +0000112**
113** If the query could not be executed because a database file is
drh2dfbbca2000-07-28 14:32:48 +0000114** locked or busy, then this function returns SQLITE_BUSY. (This
115** behavior can be modified somewhat using the sqlite_busy_handler()
116** and sqlite_busy_timeout() functions below.) If the query could
117** not be executed because a file is missing or has incorrect
118** permissions, this function returns SQLITE_ERROR.
drh75897232000-05-29 14:26:00 +0000119*/
120int sqlite_exec(
121 sqlite*, /* An open database */
122 char *sql, /* SQL to be executed */
123 sqlite_callback, /* Callback function */
124 void *, /* 1st argument to callback function */
125 char **errmsg /* Error msg written here */
126);
127
drh58b95762000-06-02 01:17:37 +0000128/*
129** Return values fro sqlite_exec()
130*/
131#define SQLITE_OK 0 /* Successful result */
132#define SQLITE_INTERNAL 1 /* An internal logic error in SQLite */
133#define SQLITE_ERROR 2 /* SQL error or missing database */
drheec553b2000-06-02 01:51:20 +0000134#define SQLITE_PERM 3 /* Access permission denied */
135#define SQLITE_ABORT 4 /* Callback routine requested an abort */
136#define SQLITE_BUSY 5 /* One or more database files are locked */
137#define SQLITE_NOMEM 6 /* A malloc() failed */
138#define SQLITE_READONLY 7 /* Attempt to write a readonly database */
139
drh75897232000-05-29 14:26:00 +0000140/* This function returns true if the given input string comprises
141** one or more complete SQL statements.
142**
143** The algorithm is simple. If the last token other than spaces
144** and comments is a semicolon, then return true. otherwise return
145** false.
146*/
147int sqlite_complete(const char *sql);
148
drh2dfbbca2000-07-28 14:32:48 +0000149/*
150** This routine identifies a callback function that is invoked
151** whenever an attempt is made to open a database table that is
152** currently locked by another process or thread. If the busy callback
153** is NULL, then sqlite_exec() returns SQLITE_BUSY immediately if
154** it finds a locked table. If the busy callback is not NULL, then
155** sqlite_exec() invokes the callback with three arguments. The
156** second argument is the name of the locked table and the third
157** argument is the number of times the table has been busy. If the
158** busy callback returns 0, then sqlite_exec() immediately returns
159** SQLITE_BUSY. If the callback returns non-zero, then sqlite_exec()
160** tries to open the table again and the cycle repeats.
161**
162** The default busy callback is NULL.
163**
164** Sqlite is re-entrant, so the busy handler may start a new query.
165** (It is not clear why anyone would every want to do this, but it
166** is allowed, in theory.) But the busy handler may not close the
167** database. Closing the database from a busy handler will delete
168** data structures out from under the executing query and will
169** probably result in a coredump.
170*/
171void sqlite_busy_handler(sqlite*, int(*)(void*,const char*,int), void*);
172
173/*
174** This routine sets a busy handler that sleeps for a while when a
175** table is locked. The handler will sleep multiple times until
176** at least "ms" milleseconds of sleeping have been done. After
177** "ms" milleseconds of sleeping, the handler returns 0 which
178** causes sqlite_exec() to return SQLITE_BUSY.
179**
180** Calling this routine with an argument less than or equal to zero
181** turns off all busy handlers.
182*/
183void sqlite_busy_timeout(sqlite*, int ms);
184
drhe3710332000-09-29 13:30:53 +0000185/*
186** This next routine is really just a wrapper around sqlite_exec().
187** Instead of invoking a user-supplied callback for each row of the
188** result, this routine remembers each row of the result in memory
189** obtained from malloc(), then returns all of the result after the
190** query has finished. After the calling function has finished using
191** the result, it should pass the result data pointer to
192** sqlite_free_table() in order to release the memory that was malloc-ed.
193** Because of the way the malloc() happens, the calling function must
194** not try to call malloc() directly. Only sqlite_free_table() is able
195** to release the memory properly and safely.
196**
197** The return value of this routine is the same as from sqlite_exec().
198*/
199int sqlite_get_table(
200 sqlite*, /* An open database */
201 char *sql, /* SQL to be executed */
202 char ***resultp, /* Result written to a char *[] that this points to */
203 int *nrow, /* Number of result rows written here */
204 int *ncolumn, /* Number of result columns written here */
205 char **errmsg /* Error msg written here */
206);
207
208/*
209** Call this routine to free the memory that sqlite_get_table() allocated.
210*/
211void sqlite_free_table(char **result);
212
213
drh75897232000-05-29 14:26:00 +0000214#endif /* _SQLITE_H_ */