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