blob: 0056c0d2fabfc8bad49e3f1389d941eb517ae48d [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**
drh303aaa72000-08-17 10:22:34 +000027** @(#) $Id: sqlite.h.in,v 1.1 2000/08/17 10:22:34 drh Exp $
drh75897232000-05-29 14:26:00 +000028*/
29#ifndef _SQLITE_H_
30#define _SQLITE_H_
31
32/*
drh303aaa72000-08-17 10:22:34 +000033** The version of the SQLite library
34*/
35#define SQLITE_VERSION --VERS--
36#define SQLITE_VERSION_STRING "--VERS--"
37
38/*
drh75897232000-05-29 14:26:00 +000039** Each open sqlite database is represented by an instance of the
40** following opaque structure.
41*/
42typedef struct sqlite sqlite;
43
44/*
45** A function to open a new sqlite database.
46**
47** If the database does not exist and mode indicates write
48** permission, then a new database is created. If the database
49** does not exist and mode does not indicate write permission,
50** then the open fails, an error message generated (if errmsg!=0)
51** and the function returns 0.
52**
53** If mode does not indicates user write permission, then the
54** database is opened read-only.
55**
56** The Truth: As currently implemented, all databases are opened
57** for writing all the time. Maybe someday we will provide the
58** ability to open a database readonly. The mode parameters is
59** provide in anticipation of that enhancement.
60*/
61sqlite *sqlite_open(const char *filename, int mode, char **errmsg);
62
63/*
64** A function to close the database.
65**
66** Call this function with a pointer to a structure that was previously
67** returned from sqlite_open() and the corresponding database will by closed.
68*/
69void sqlite_close(sqlite *);
70
71/*
72** The type for a callback function.
73*/
74typedef int (*sqlite_callback)(void*,int,char**, char**);
75
76/*
77** A function to executes one or more statements of SQL.
78**
79** If one or more of the SQL statements are queries, then
80** the callback function specified by the 3rd parameter is
81** invoked once for each row of the query result. This callback
82** should normally return 0. If the callback returns a non-zero
83** value then the query is aborted, all subsequent SQL statements
drh58b95762000-06-02 01:17:37 +000084** are skipped and the sqlite_exec() function returns the SQLITE_ABORT.
drh75897232000-05-29 14:26:00 +000085**
86** The 4th parameter is an arbitrary pointer that is passed
87** to the callback function as its first parameter.
88**
89** The 2nd parameter to the callback function is the number of
90** columns in the query result. The 3rd parameter is an array
91** of string holding the values for each column. The 4th parameter
92** is an array of strings holding the names of each column.
93**
94** The callback function may be NULL, even for queries. A NULL
95** callback is not an error. It just means that no callback
96** will be invoked.
97**
98** If an error occurs while parsing or evaluating the SQL (but
99** not while executing the callback) then an appropriate error
100** message is written into memory obtained from malloc() and
101** *errmsg is made to point to that message. If errmsg==NULL,
102** then no error message is ever written. The return value is
drh2dfbbca2000-07-28 14:32:48 +0000103** SQLITE_ERROR if an error occurs. The calling function is
104** responsible for freeing the memory that holds the error
105** message.
drh58b95762000-06-02 01:17:37 +0000106**
107** If the query could not be executed because a database file is
drh2dfbbca2000-07-28 14:32:48 +0000108** locked or busy, then this function returns SQLITE_BUSY. (This
109** behavior can be modified somewhat using the sqlite_busy_handler()
110** and sqlite_busy_timeout() functions below.) If the query could
111** not be executed because a file is missing or has incorrect
112** permissions, this function returns SQLITE_ERROR.
drh75897232000-05-29 14:26:00 +0000113*/
114int sqlite_exec(
115 sqlite*, /* An open database */
116 char *sql, /* SQL to be executed */
117 sqlite_callback, /* Callback function */
118 void *, /* 1st argument to callback function */
119 char **errmsg /* Error msg written here */
120);
121
drh58b95762000-06-02 01:17:37 +0000122/*
123** Return values fro sqlite_exec()
124*/
125#define SQLITE_OK 0 /* Successful result */
126#define SQLITE_INTERNAL 1 /* An internal logic error in SQLite */
127#define SQLITE_ERROR 2 /* SQL error or missing database */
drheec553b2000-06-02 01:51:20 +0000128#define SQLITE_PERM 3 /* Access permission denied */
129#define SQLITE_ABORT 4 /* Callback routine requested an abort */
130#define SQLITE_BUSY 5 /* One or more database files are locked */
131#define SQLITE_NOMEM 6 /* A malloc() failed */
132#define SQLITE_READONLY 7 /* Attempt to write a readonly database */
133
drh75897232000-05-29 14:26:00 +0000134/* This function returns true if the given input string comprises
135** one or more complete SQL statements.
136**
137** The algorithm is simple. If the last token other than spaces
138** and comments is a semicolon, then return true. otherwise return
139** false.
140*/
141int sqlite_complete(const char *sql);
142
drh2dfbbca2000-07-28 14:32:48 +0000143/*
144** This routine identifies a callback function that is invoked
145** whenever an attempt is made to open a database table that is
146** currently locked by another process or thread. If the busy callback
147** is NULL, then sqlite_exec() returns SQLITE_BUSY immediately if
148** it finds a locked table. If the busy callback is not NULL, then
149** sqlite_exec() invokes the callback with three arguments. The
150** second argument is the name of the locked table and the third
151** argument is the number of times the table has been busy. If the
152** busy callback returns 0, then sqlite_exec() immediately returns
153** SQLITE_BUSY. If the callback returns non-zero, then sqlite_exec()
154** tries to open the table again and the cycle repeats.
155**
156** The default busy callback is NULL.
157**
158** Sqlite is re-entrant, so the busy handler may start a new query.
159** (It is not clear why anyone would every want to do this, but it
160** is allowed, in theory.) But the busy handler may not close the
161** database. Closing the database from a busy handler will delete
162** data structures out from under the executing query and will
163** probably result in a coredump.
164*/
165void sqlite_busy_handler(sqlite*, int(*)(void*,const char*,int), void*);
166
167/*
168** This routine sets a busy handler that sleeps for a while when a
169** table is locked. The handler will sleep multiple times until
170** at least "ms" milleseconds of sleeping have been done. After
171** "ms" milleseconds of sleeping, the handler returns 0 which
172** causes sqlite_exec() to return SQLITE_BUSY.
173**
174** Calling this routine with an argument less than or equal to zero
175** turns off all busy handlers.
176*/
177void sqlite_busy_timeout(sqlite*, int ms);
178
drh75897232000-05-29 14:26:00 +0000179#endif /* _SQLITE_H_ */