drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 12 | ** This header file defines the interface that the SQLite library |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 13 | ** presents to client programs. |
| 14 | ** |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 15 | ** @(#) $Id: sqlite.h.in,v 1.32 2002/06/20 11:36:50 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 16 | */ |
| 17 | #ifndef _SQLITE_H_ |
| 18 | #define _SQLITE_H_ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 19 | #include <stdarg.h> /* Needed for the definition of va_list */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 20 | |
| 21 | /* |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 22 | ** The version of the SQLite library. |
drh | 303aaa7 | 2000-08-17 10:22:34 +0000 | [diff] [blame] | 23 | */ |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 24 | #define SQLITE_VERSION "--VERS--" |
| 25 | |
| 26 | /* |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 27 | ** Make sure we can call this stuff from C++. |
| 28 | */ |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | |
| 33 | /* |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 34 | ** 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 | */ |
| 38 | extern const char sqlite_version[]; |
drh | 303aaa7 | 2000-08-17 10:22:34 +0000 | [diff] [blame] | 39 | |
| 40 | /* |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 41 | ** 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 | */ |
drh | fbc3eab | 2001-04-06 16:13:42 +0000 | [diff] [blame] | 53 | extern const char sqlite_encoding[]; |
drh | 297ecf1 | 2001-04-05 15:57:13 +0000 | [diff] [blame] | 54 | |
| 55 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 56 | ** Each open sqlite database is represented by an instance of the |
| 57 | ** following opaque structure. |
| 58 | */ |
| 59 | typedef 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 | */ |
| 78 | sqlite *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 | */ |
| 86 | void sqlite_close(sqlite *); |
| 87 | |
| 88 | /* |
| 89 | ** The type for a callback function. |
| 90 | */ |
| 91 | typedef 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 |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 101 | ** are skipped and the sqlite_exec() function returns the SQLITE_ABORT. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 102 | ** |
| 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 |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 107 | ** 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 111 | ** |
| 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 |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 119 | ** *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. |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 126 | ** |
| 127 | ** If the query could not be executed because a database file is |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 128 | ** locked or busy, then this function returns SQLITE_BUSY. (This |
| 129 | ** behavior can be modified somewhat using the sqlite_busy_handler() |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 130 | ** and sqlite_busy_timeout() functions below.) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 131 | */ |
| 132 | int sqlite_exec( |
| 133 | sqlite*, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 134 | const char *sql, /* SQL to be executed */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 135 | sqlite_callback, /* Callback function */ |
| 136 | void *, /* 1st argument to callback function */ |
| 137 | char **errmsg /* Error msg written here */ |
| 138 | ); |
| 139 | |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 140 | /* |
drh | 98699b5 | 2000-10-09 12:57:00 +0000 | [diff] [blame] | 141 | ** Return values for sqlite_exec() |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 142 | */ |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 143 | #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 */ |
drh | 8aff101 | 2001-12-22 14:49:24 +0000 | [diff] [blame] | 163 | #define SQLITE_MISMATCH 20 /* Data type mismatch */ |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 164 | #define SQLITE_MISUSE 21 /* Library used incorrectly */ |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 165 | |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 166 | /* |
| 167 | ** Each entry in an SQLite table has a unique integer key. (The key is |
| 168 | ** the value of the INTEGER PRIMARY KEY column if there is such a column, |
| 169 | ** otherwise the key is generated at random. The unique key is always |
| 170 | ** available as the ROWID, OID, or _ROWID_ column.) The following routine |
| 171 | ** returns the integer key of the most recent insert in the database. |
| 172 | ** |
| 173 | ** This function is similar to the mysql_insert_id() function from MySQL. |
| 174 | */ |
| 175 | int sqlite_last_insert_rowid(sqlite*); |
| 176 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 177 | /* |
| 178 | ** This function returns the number of database rows that were changed |
| 179 | ** (or inserted or deleted) by the most recent called sqlite_exec(). |
| 180 | ** |
| 181 | ** All changes are counted, even if they were later undone by a |
| 182 | ** ROLLBACK or ABORT. Except, changes associated with creating and |
| 183 | ** dropping tables are not counted. |
| 184 | ** |
| 185 | ** If a callback invokes sqlite_exec() recursively, then the changes |
| 186 | ** in the inner, recursive call are counted together with the changes |
| 187 | ** in the outer call. |
| 188 | ** |
| 189 | ** SQLite implements the command "DELETE FROM table" without a WHERE clause |
| 190 | ** by dropping and recreating the table. (This is much faster than going |
| 191 | ** through and deleting individual elements form the table.) Because of |
| 192 | ** this optimization, the change count for "DELETE FROM table" will be |
| 193 | ** zero regardless of the number of elements that were originally in the |
| 194 | ** table. To get an accurate count of the number of rows deleted, use |
| 195 | ** "DELETE FROM table WHERE 1" instead. |
| 196 | */ |
| 197 | int sqlite_changes(sqlite*); |
| 198 | |
drh | 717e640 | 2001-09-27 03:22:32 +0000 | [diff] [blame] | 199 | /* If the parameter to this routine is one of the return value constants |
| 200 | ** defined above, then this routine returns a constant text string which |
| 201 | ** descripts (in English) the meaning of the return value. |
| 202 | */ |
drh | 6d4abfb | 2001-10-22 02:58:08 +0000 | [diff] [blame] | 203 | const char *sqlite_error_string(int); |
| 204 | #define sqliteErrStr sqlite_error_string /* Legacy. Do not use in new code. */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 205 | |
| 206 | /* This function causes any pending database operation to abort and |
| 207 | ** return at its earliest opportunity. This routine is typically |
drh | 66b89c8 | 2000-11-28 20:47:17 +0000 | [diff] [blame] | 208 | ** called in response to a user action such as pressing "Cancel" |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 209 | ** or Ctrl-C where the user wants a long query operation to halt |
| 210 | ** immediately. |
| 211 | */ |
| 212 | void sqlite_interrupt(sqlite*); |
| 213 | |
drh | eec553b | 2000-06-02 01:51:20 +0000 | [diff] [blame] | 214 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 215 | /* This function returns true if the given input string comprises |
| 216 | ** one or more complete SQL statements. |
| 217 | ** |
| 218 | ** The algorithm is simple. If the last token other than spaces |
| 219 | ** and comments is a semicolon, then return true. otherwise return |
| 220 | ** false. |
| 221 | */ |
| 222 | int sqlite_complete(const char *sql); |
| 223 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 224 | /* |
| 225 | ** This routine identifies a callback function that is invoked |
| 226 | ** whenever an attempt is made to open a database table that is |
| 227 | ** currently locked by another process or thread. If the busy callback |
| 228 | ** is NULL, then sqlite_exec() returns SQLITE_BUSY immediately if |
| 229 | ** it finds a locked table. If the busy callback is not NULL, then |
| 230 | ** sqlite_exec() invokes the callback with three arguments. The |
| 231 | ** second argument is the name of the locked table and the third |
| 232 | ** argument is the number of times the table has been busy. If the |
| 233 | ** busy callback returns 0, then sqlite_exec() immediately returns |
| 234 | ** SQLITE_BUSY. If the callback returns non-zero, then sqlite_exec() |
| 235 | ** tries to open the table again and the cycle repeats. |
| 236 | ** |
| 237 | ** The default busy callback is NULL. |
| 238 | ** |
| 239 | ** Sqlite is re-entrant, so the busy handler may start a new query. |
| 240 | ** (It is not clear why anyone would every want to do this, but it |
| 241 | ** is allowed, in theory.) But the busy handler may not close the |
| 242 | ** database. Closing the database from a busy handler will delete |
| 243 | ** data structures out from under the executing query and will |
| 244 | ** probably result in a coredump. |
| 245 | */ |
| 246 | void sqlite_busy_handler(sqlite*, int(*)(void*,const char*,int), void*); |
| 247 | |
| 248 | /* |
| 249 | ** This routine sets a busy handler that sleeps for a while when a |
| 250 | ** table is locked. The handler will sleep multiple times until |
| 251 | ** at least "ms" milleseconds of sleeping have been done. After |
| 252 | ** "ms" milleseconds of sleeping, the handler returns 0 which |
| 253 | ** causes sqlite_exec() to return SQLITE_BUSY. |
| 254 | ** |
| 255 | ** Calling this routine with an argument less than or equal to zero |
| 256 | ** turns off all busy handlers. |
| 257 | */ |
| 258 | void sqlite_busy_timeout(sqlite*, int ms); |
| 259 | |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 260 | /* |
| 261 | ** This next routine is really just a wrapper around sqlite_exec(). |
| 262 | ** Instead of invoking a user-supplied callback for each row of the |
| 263 | ** result, this routine remembers each row of the result in memory |
| 264 | ** obtained from malloc(), then returns all of the result after the |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 265 | ** query has finished. |
| 266 | ** |
| 267 | ** As an example, suppose the query result where this table: |
| 268 | ** |
| 269 | ** Name | Age |
| 270 | ** ----------------------- |
| 271 | ** Alice | 43 |
| 272 | ** Bob | 28 |
| 273 | ** Cindy | 21 |
| 274 | ** |
| 275 | ** If the 3rd argument were &azResult then after the function returns |
drh | 98699b5 | 2000-10-09 12:57:00 +0000 | [diff] [blame] | 276 | ** azResult will contain the following data: |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 277 | ** |
| 278 | ** azResult[0] = "Name"; |
| 279 | ** azResult[1] = "Age"; |
| 280 | ** azResult[2] = "Alice"; |
| 281 | ** azResult[3] = "43"; |
| 282 | ** azResult[4] = "Bob"; |
| 283 | ** azResult[5] = "28"; |
| 284 | ** azResult[6] = "Cindy"; |
| 285 | ** azResult[7] = "21"; |
| 286 | ** |
| 287 | ** Notice that there is an extra row of data containing the column |
| 288 | ** headers. But the *nrow return value is still 3. *ncolumn is |
| 289 | ** set to 2. In general, the number of values inserted into azResult |
| 290 | ** will be ((*nrow) + 1)*(*ncolumn). |
| 291 | ** |
| 292 | ** After the calling function has finished using the result, it should |
| 293 | ** pass the result data pointer to sqlite_free_table() in order to |
| 294 | ** release the memory that was malloc-ed. Because of the way the |
| 295 | ** malloc() happens, the calling function must not try to call |
| 296 | ** malloc() directly. Only sqlite_free_table() is able to release |
| 297 | ** the memory properly and safely. |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 298 | ** |
| 299 | ** The return value of this routine is the same as from sqlite_exec(). |
| 300 | */ |
| 301 | int sqlite_get_table( |
| 302 | sqlite*, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 303 | const char *sql, /* SQL to be executed */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 304 | char ***resultp, /* Result written to a char *[] that this points to */ |
| 305 | int *nrow, /* Number of result rows written here */ |
| 306 | int *ncolumn, /* Number of result columns written here */ |
| 307 | char **errmsg /* Error msg written here */ |
| 308 | ); |
| 309 | |
| 310 | /* |
| 311 | ** Call this routine to free the memory that sqlite_get_table() allocated. |
| 312 | */ |
| 313 | void sqlite_free_table(char **result); |
| 314 | |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 315 | /* |
| 316 | ** The following routines are wrappers around sqlite_exec() and |
drh | 98699b5 | 2000-10-09 12:57:00 +0000 | [diff] [blame] | 317 | ** sqlite_get_table(). The only difference between the routines that |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 318 | ** follow and the originals is that the second argument to the |
| 319 | ** routines that follow is really a printf()-style format |
| 320 | ** string describing the SQL to be executed. Arguments to the format |
| 321 | ** string appear at the end of the argument list. |
| 322 | ** |
| 323 | ** All of the usual printf formatting options apply. In addition, there |
| 324 | ** is a "%q" option. %q works like %s in that it substitutes a null-terminated |
drh | 66b89c8 | 2000-11-28 20:47:17 +0000 | [diff] [blame] | 325 | ** string from the argument list. But %q also doubles every '\'' character. |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 326 | ** %q is designed for use inside a string literal. By doubling each '\'' |
drh | 66b89c8 | 2000-11-28 20:47:17 +0000 | [diff] [blame] | 327 | ** character it escapes that character and allows it to be inserted into |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 328 | ** the string. |
| 329 | ** |
| 330 | ** For example, so some string variable contains text as follows: |
| 331 | ** |
| 332 | ** char *zText = "It's a happy day!"; |
| 333 | ** |
| 334 | ** We can use this text in an SQL statement as follows: |
| 335 | ** |
| 336 | ** sqlite_exec_printf(db, "INSERT INTO table VALUES('%q')", |
| 337 | ** callback1, 0, 0, zText); |
| 338 | ** |
| 339 | ** Because the %q format string is used, the '\'' character in zText |
| 340 | ** is escaped and the SQL generated is as follows: |
| 341 | ** |
| 342 | ** INSERT INTO table1 VALUES('It''s a happy day!') |
| 343 | ** |
| 344 | ** This is correct. Had we used %s instead of %q, the generated SQL |
| 345 | ** would have looked like this: |
| 346 | ** |
| 347 | ** INSERT INTO table1 VALUES('It's a happy day!'); |
| 348 | ** |
| 349 | ** This second example is an SQL syntax error. As a general rule you |
| 350 | ** should always use %q instead of %s when inserting text into a string |
| 351 | ** literal. |
| 352 | */ |
| 353 | int sqlite_exec_printf( |
| 354 | sqlite*, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 355 | const char *sqlFormat, /* printf-style format string for the SQL */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 356 | sqlite_callback, /* Callback function */ |
| 357 | void *, /* 1st argument to callback function */ |
| 358 | char **errmsg, /* Error msg written here */ |
| 359 | ... /* Arguments to the format string. */ |
| 360 | ); |
| 361 | int sqlite_exec_vprintf( |
| 362 | sqlite*, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 363 | const char *sqlFormat, /* printf-style format string for the SQL */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 364 | sqlite_callback, /* Callback function */ |
| 365 | void *, /* 1st argument to callback function */ |
| 366 | char **errmsg, /* Error msg written here */ |
| 367 | va_list ap /* Arguments to the format string. */ |
| 368 | ); |
| 369 | int sqlite_get_table_printf( |
| 370 | sqlite*, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 371 | const char *sqlFormat, /* printf-style format string for the SQL */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 372 | char ***resultp, /* Result written to a char *[] that this points to */ |
| 373 | int *nrow, /* Number of result rows written here */ |
| 374 | int *ncolumn, /* Number of result columns written here */ |
| 375 | char **errmsg, /* Error msg written here */ |
| 376 | ... /* Arguments to the format string */ |
| 377 | ); |
| 378 | int sqlite_get_table_vprintf( |
| 379 | sqlite*, /* An open database */ |
drh | 9f71c2e | 2001-11-03 23:57:09 +0000 | [diff] [blame] | 380 | const char *sqlFormat, /* printf-style format string for the SQL */ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 381 | char ***resultp, /* Result written to a char *[] that this points to */ |
| 382 | int *nrow, /* Number of result rows written here */ |
| 383 | int *ncolumn, /* Number of result columns written here */ |
| 384 | char **errmsg, /* Error msg written here */ |
| 385 | va_list ap /* Arguments to the format string */ |
| 386 | ); |
| 387 | |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 388 | /* |
drh | 5191b7e | 2002-03-08 02:12:00 +0000 | [diff] [blame] | 389 | ** Windows systems should call this routine to free memory that |
| 390 | ** is returned in the in the errmsg parameter of sqlite_open() when |
| 391 | ** SQLite is a DLL. For some reason, it does not work to call free() |
| 392 | ** directly. |
| 393 | */ |
| 394 | void sqlite_freemem(void *p); |
| 395 | |
| 396 | /* |
| 397 | ** Windows systems need functions to call to return the sqlite_version |
| 398 | ** and sqlite_encoding strings. |
| 399 | */ |
| 400 | const char *sqlite_libversion(void); |
| 401 | const char *sqlite_libencoding(void); |
| 402 | |
| 403 | /* |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 404 | ** A pointer to the following structure is used to communicate with |
| 405 | ** the implementations of user-defined functions. |
| 406 | */ |
| 407 | typedef struct sqlite_func sqlite_func; |
| 408 | |
| 409 | /* |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 410 | ** Use the following routines to create new user-defined functions. See |
| 411 | ** the documentation for details. |
| 412 | */ |
| 413 | int sqlite_create_function( |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 414 | sqlite*, /* Database where the new function is registered */ |
| 415 | const char *zName, /* Name of the new function */ |
| 416 | int nArg, /* Number of arguments. -1 means any number */ |
| 417 | void (*xFunc)(sqlite_func*,int,const char**), /* C code to implement */ |
| 418 | void *pUserData /* Available via the sqlite_user_data() call */ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 419 | ); |
| 420 | int sqlite_create_aggregate( |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 421 | sqlite*, /* Database where the new function is registered */ |
| 422 | const char *zName, /* Name of the function */ |
| 423 | int nArg, /* Number of arguments */ |
| 424 | void (*xStep)(sqlite_func*,int,const char**), /* Called for each row */ |
| 425 | void (*xFinalize)(sqlite_func*), /* Called once to get final result */ |
| 426 | void *pUserData /* Available via the sqlite_user_data() call */ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 427 | ); |
| 428 | |
| 429 | /* |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 430 | ** Use the following routine to define the datatype returned by a |
| 431 | ** user-defined function. The second argument can be one of the |
| 432 | ** constants SQLITE_NUMERIC, SQLITE_TEXT, or SQLITE_ARGS or it |
| 433 | ** can be an integer greater than or equal to zero. The datatype |
| 434 | ** will be numeric or text (the only two types supported) if the |
| 435 | ** argument is SQLITE_NUMERIC or SQLITE_TEXT. If the argument is |
| 436 | ** SQLITE_ARGS, then the datatype is numeric if any argument to the |
| 437 | ** function is numeric and is text otherwise. If the second argument |
| 438 | ** is an integer, then the datatype of the result is the same as the |
| 439 | ** parameter to the function that corresponds to that integer. |
| 440 | */ |
| 441 | int sqlite_function_type( |
| 442 | sqlite *db, /* The database there the function is registered */ |
| 443 | const char *zName, /* Name of the function */ |
| 444 | int datatype /* The datatype for this function */ |
| 445 | ); |
| 446 | #define SQLITE_NUMERIC (-1) |
| 447 | #define SQLITE_TEXT (-2) |
| 448 | #define SQLITE_ARGS (-3) |
| 449 | |
| 450 | /* |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 451 | ** The user function implementations call one of the following four routines |
| 452 | ** in order to return their results. The first parameter to each of these |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 453 | ** routines is a copy of the first argument to xFunc() or xFinialize(). |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 454 | ** The second parameter to these routines is the result to be returned. |
| 455 | ** A NULL can be passed as the second parameter to sqlite_set_result_string() |
| 456 | ** in order to return a NULL result. |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 457 | ** |
| 458 | ** The 3rd argument to _string and _error is the number of characters to |
| 459 | ** take from the string. If this argument is negative, then all characters |
| 460 | ** up to and including the first '\000' are used. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 461 | ** |
| 462 | ** The sqlite_set_result_string() function allocates a buffer to hold the |
| 463 | ** result and returns a pointer to this buffer. The calling routine |
| 464 | ** (that is, the implmentation of a user function) can alter the content |
| 465 | ** of this buffer if desired. |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 466 | */ |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 467 | char *sqlite_set_result_string(sqlite_func*,const char*,int); |
| 468 | void sqlite_set_result_int(sqlite_func*,int); |
| 469 | void sqlite_set_result_double(sqlite_func*,double); |
| 470 | void sqlite_set_result_error(sqlite_func*,const char*,int); |
| 471 | |
| 472 | /* |
| 473 | ** The pUserData parameter to the sqlite_create_function() and |
| 474 | ** sqlite_create_aggregate() routines used to register user functions |
| 475 | ** is available to the implementation of the function using this |
| 476 | ** call. |
| 477 | */ |
| 478 | void *sqlite_user_data(sqlite_func*); |
| 479 | |
| 480 | /* |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 481 | ** Aggregate functions use the following routine to allocate |
| 482 | ** a structure for storing their state. The first time this routine |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 483 | ** is called for a particular aggregate, a new structure of size nBytes |
| 484 | ** is allocated, zeroed, and returned. On subsequent calls (for the |
| 485 | ** same aggregate instance) the same buffer is returned. The implementation |
| 486 | ** of the aggregate can use the returned buffer to accumulate data. |
| 487 | ** |
| 488 | ** The buffer allocated is freed automatically be SQLite. |
| 489 | */ |
| 490 | void *sqlite_aggregate_context(sqlite_func*, int nBytes); |
| 491 | |
| 492 | /* |
drh | dd5baa9 | 2002-02-27 19:50:59 +0000 | [diff] [blame] | 493 | ** The next routine returns the number of calls to xStep for a particular |
| 494 | ** aggregate function instance. The current call to xStep counts so this |
| 495 | ** routine always returns at least 1. |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 496 | */ |
| 497 | int sqlite_aggregate_count(sqlite_func*); |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 498 | |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 499 | #ifdef __cplusplus |
| 500 | } /* End of the 'extern "C"' block */ |
| 501 | #endif |
| 502 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 503 | #endif /* _SQLITE_H_ */ |