blob: 4909688da7b8e6766ad29d79babbed917ee98cd7 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
drhb19a2bc2001-09-16 00:13:26 +00002** 2001 September 15
drh75897232000-05-29 14:26:00 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drh75897232000-05-29 14:26:00 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** 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.
drh75897232000-05-29 14:26:00 +000010**
11*************************************************************************
12** Main file for the SQLite library. The routines in this file
13** implement the programmer interface to the library. Routines in
14** other files are for internal use by SQLite and should not be
15** accessed by users of the library.
16**
danielk197796fb0dd2004-06-30 09:49:22 +000017** $Id: main.c,v 1.243 2004/06/30 09:49:24 danielk1977 Exp $
drh75897232000-05-29 14:26:00 +000018*/
19#include "sqliteInt.h"
drh8cfbf082001-09-19 13:22:39 +000020#include "os.h"
drhce9079c2002-05-15 14:17:44 +000021#include <ctype.h>
drh75897232000-05-29 14:26:00 +000022
23/*
drhc2311722002-07-19 17:46:38 +000024** A pointer to this structure is used to communicate information
danielk19774adee202004-05-08 08:23:19 +000025** from sqlite3Init into the sqlite3InitCallback.
drhc2311722002-07-19 17:46:38 +000026*/
27typedef struct {
28 sqlite *db; /* The database being initialized */
29 char **pzErrMsg; /* Error message stored here */
30} InitData;
31
drh8bf8dc92003-05-17 17:35:10 +000032/*
drh9c054832004-05-31 18:51:57 +000033** The following constant value is used by the SQLITE_BIGENDIAN and
34** SQLITE_LITTLEENDIAN macros.
drhbbd42a62004-05-22 17:41:58 +000035*/
36const int sqlite3one = 1;
37
38/*
drh8bf8dc92003-05-17 17:35:10 +000039** Fill the InitData structure with an error message that indicates
40** that the database is corrupt.
41*/
drh1d85d932004-02-14 23:05:52 +000042static void corruptSchema(InitData *pData, const char *zExtra){
drhef0715c2004-06-29 10:53:54 +000043 if( !sqlite3_malloc_failed ){
44 sqlite3SetString(pData->pzErrMsg, "malformed database schema",
45 zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0);
46 }
drh8bf8dc92003-05-17 17:35:10 +000047}
drhc2311722002-07-19 17:46:38 +000048
49/*
drh75897232000-05-29 14:26:00 +000050** This is the callback routine for the code that initializes the
danielk19774adee202004-05-08 08:23:19 +000051** database. See sqlite3Init() below for additional information.
drh382c0242001-10-06 16:33:02 +000052**
53** Each callback contains the following information:
drh28037572000-08-02 13:47:41 +000054**
drh4a324312001-12-21 14:30:42 +000055** argv[0] = "file-format" or "schema-cookie" or "table" or "index"
drhe3c41372001-09-17 20:25:58 +000056** argv[1] = table or index name or meta statement type.
57** argv[2] = root page number for table or index. NULL for meta.
drhe78e8282003-01-19 03:59:45 +000058** argv[3] = SQL text for a CREATE TABLE or CREATE INDEX statement.
drh1c2d8412003-03-31 00:30:47 +000059** argv[4] = "1" for temporary files, "0" for main database, "2" or more
60** for auxiliary database files.
drhd78eeee2001-09-13 16:18:53 +000061**
drh75897232000-05-29 14:26:00 +000062*/
drhc2311722002-07-19 17:46:38 +000063static
danielk19774adee202004-05-08 08:23:19 +000064int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){
drhc2311722002-07-19 17:46:38 +000065 InitData *pData = (InitData*)pInit;
drhd78eeee2001-09-13 16:18:53 +000066 int nErr = 0;
drh75897232000-05-29 14:26:00 +000067
drhe0bc4042002-06-25 01:09:11 +000068 assert( argc==5 );
drh98e3e602003-07-27 17:26:22 +000069 if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */
drh8bf8dc92003-05-17 17:35:10 +000070 if( argv[0]==0 ){
drh1d85d932004-02-14 23:05:52 +000071 corruptSchema(pData, 0);
drh8bf8dc92003-05-17 17:35:10 +000072 return 1;
73 }
drhd78eeee2001-09-13 16:18:53 +000074 switch( argv[0][0] ){
drh17f71932002-02-21 12:01:27 +000075 case 'v':
drhd78eeee2001-09-13 16:18:53 +000076 case 'i':
drh17f71932002-02-21 12:01:27 +000077 case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */
drh1d85d932004-02-14 23:05:52 +000078 sqlite *db = pData->db;
drh8bf8dc92003-05-17 17:35:10 +000079 if( argv[2]==0 || argv[4]==0 ){
drh1d85d932004-02-14 23:05:52 +000080 corruptSchema(pData, 0);
drh8bf8dc92003-05-17 17:35:10 +000081 return 1;
82 }
drhadbca9c2001-09-27 15:11:53 +000083 if( argv[3] && argv[3][0] ){
drh17f71932002-02-21 12:01:27 +000084 /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
drh1d85d932004-02-14 23:05:52 +000085 ** But because db->init.busy is set to 1, no VDBE code is generated
drh382c0242001-10-06 16:33:02 +000086 ** or executed. All the parser does is build the internal data
drh17f71932002-02-21 12:01:27 +000087 ** structures that describe the table, index, or view.
drh382c0242001-10-06 16:33:02 +000088 */
drh1d85d932004-02-14 23:05:52 +000089 char *zErr;
danielk19772b444852004-06-29 07:45:33 +000090 int rc;
drh1d85d932004-02-14 23:05:52 +000091 assert( db->init.busy );
92 db->init.iDb = atoi(argv[4]);
93 assert( db->init.iDb>=0 && db->init.iDb<db->nDb );
94 db->init.newTnum = atoi(argv[2]);
danielk19772b444852004-06-29 07:45:33 +000095 rc = sqlite3_exec(db, argv[3], 0, 0, &zErr);
96 db->init.iDb = 0;
97 if( SQLITE_OK!=rc ){
drh1d85d932004-02-14 23:05:52 +000098 corruptSchema(pData, zErr);
drh3f4fedb2004-05-31 19:34:33 +000099 sqlite3_free(zErr);
danielk19772b444852004-06-29 07:45:33 +0000100 return rc;
drh1d85d932004-02-14 23:05:52 +0000101 }
drhadbca9c2001-09-27 15:11:53 +0000102 }else{
drh382c0242001-10-06 16:33:02 +0000103 /* If the SQL column is blank it means this is an index that
104 ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
drhaacc5432002-01-06 17:07:40 +0000105 ** constraint for a CREATE TABLE. The index should have already
drh382c0242001-10-06 16:33:02 +0000106 ** been created when we processed the CREATE TABLE. All we have
drhaacc5432002-01-06 17:07:40 +0000107 ** to do here is record the root page number for that index.
drh382c0242001-10-06 16:33:02 +0000108 */
drhd24cc422003-03-27 12:51:24 +0000109 int iDb;
110 Index *pIndex;
111
112 iDb = atoi(argv[4]);
drh1d85d932004-02-14 23:05:52 +0000113 assert( iDb>=0 && iDb<db->nDb );
danielk19774adee202004-05-08 08:23:19 +0000114 pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zName);
drhadbca9c2001-09-27 15:11:53 +0000115 if( pIndex==0 || pIndex->tnum!=0 ){
drhda9e0342002-01-10 14:31:48 +0000116 /* This can occur if there exists an index on a TEMP table which
117 ** has the same name as another index on a permanent index. Since
118 ** the permanent table is hidden by the TEMP table, we can also
119 ** safely ignore the index on the permanent table.
120 */
121 /* Do Nothing */;
drhadbca9c2001-09-27 15:11:53 +0000122 }else{
123 pIndex->tnum = atoi(argv[2]);
124 }
125 }
drhd78eeee2001-09-13 16:18:53 +0000126 }
danielk19772b444852004-06-29 07:45:33 +0000127 break;
drhd78eeee2001-09-13 16:18:53 +0000128 default: {
129 /* This can not happen! */
130 nErr = 1;
131 assert( nErr==0 );
132 }
drh28037572000-08-02 13:47:41 +0000133 }
drh75897232000-05-29 14:26:00 +0000134 return nErr;
135}
136
137/*
drh58b95762000-06-02 01:17:37 +0000138** Attempt to read the database schema and initialize internal
drh1c2d8412003-03-31 00:30:47 +0000139** data structures for a single database file. The index of the
140** database file is given by iDb. iDb==0 is used for the main
141** database. iDb==1 should never be used. iDb>=2 is used for
142** auxiliary databases. Return one of the SQLITE_ error codes to
drh58b95762000-06-02 01:17:37 +0000143** indicate success or failure.
drh75897232000-05-29 14:26:00 +0000144*/
danielk19774adee202004-05-08 08:23:19 +0000145static int sqlite3InitOne(sqlite *db, int iDb, char **pzErrMsg){
drh58b95762000-06-02 01:17:37 +0000146 int rc;
drhe0bc4042002-06-25 01:09:11 +0000147 BtCursor *curMain;
148 int size;
149 Table *pTab;
danielk1977d008cfe2004-06-19 02:22:10 +0000150 char const *azArg[6];
drh1c2d8412003-03-31 00:30:47 +0000151 char zDbNum[30];
drha3b321d2004-05-11 09:31:31 +0000152 int meta[10];
drhc2311722002-07-19 17:46:38 +0000153 InitData initData;
danielk1977d008cfe2004-06-19 02:22:10 +0000154 char const *zMasterSchema;
155 char const *zMasterName;
drh58b95762000-06-02 01:17:37 +0000156
157 /*
158 ** The master database table has a structure like this
159 */
drh75897232000-05-29 14:26:00 +0000160 static char master_schema[] =
drhe0bc4042002-06-25 01:09:11 +0000161 "CREATE TABLE sqlite_master(\n"
162 " type text,\n"
163 " name text,\n"
164 " tbl_name text,\n"
165 " rootpage integer,\n"
166 " sql text\n"
167 ")"
168 ;
169 static char temp_master_schema[] =
170 "CREATE TEMP TABLE sqlite_temp_master(\n"
drh75897232000-05-29 14:26:00 +0000171 " type text,\n"
172 " name text,\n"
173 " tbl_name text,\n"
drhadbca9c2001-09-27 15:11:53 +0000174 " rootpage integer,\n"
drh75897232000-05-29 14:26:00 +0000175 " sql text\n"
176 ")"
177 ;
178
danielk1977d008cfe2004-06-19 02:22:10 +0000179 assert( iDb>=0 && iDb<db->nDb );
drh603240c2002-03-05 01:11:12 +0000180
danielk1977d008cfe2004-06-19 02:22:10 +0000181 /* zMasterSchema and zInitScript are set to point at the master schema
182 ** and initialisation script appropriate for the database being
183 ** initialised. zMasterName is the name of the master table.
drh58b95762000-06-02 01:17:37 +0000184 */
danielk1977d008cfe2004-06-19 02:22:10 +0000185 if( iDb==1 ){
186 zMasterSchema = temp_master_schema;
187 zMasterName = TEMP_MASTER_NAME;
188 }else{
189 zMasterSchema = master_schema;
190 zMasterName = MASTER_NAME;
191 }
192
193 /* Construct the schema tables. */
danielk19774adee202004-05-08 08:23:19 +0000194 sqlite3SafetyOff(db);
drhe0bc4042002-06-25 01:09:11 +0000195 azArg[0] = "table";
danielk1977d008cfe2004-06-19 02:22:10 +0000196 azArg[1] = zMasterName;
danielk19778e150812004-05-10 01:17:37 +0000197 azArg[2] = "1";
danielk1977d008cfe2004-06-19 02:22:10 +0000198 azArg[3] = zMasterSchema;
drh1c2d8412003-03-31 00:30:47 +0000199 sprintf(zDbNum, "%d", iDb);
200 azArg[4] = zDbNum;
drhe0bc4042002-06-25 01:09:11 +0000201 azArg[5] = 0;
drhc2311722002-07-19 17:46:38 +0000202 initData.db = db;
203 initData.pzErrMsg = pzErrMsg;
danielk19772b444852004-06-29 07:45:33 +0000204 rc = sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
205 if( rc!=SQLITE_OK ){
206 sqlite3SafetyOn(db);
207 return rc;
208 }
danielk1977d008cfe2004-06-19 02:22:10 +0000209 pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
drhe0bc4042002-06-25 01:09:11 +0000210 if( pTab ){
211 pTab->readOnly = 1;
drhd8bc7082000-06-07 23:51:50 +0000212 }
danielk19774adee202004-05-08 08:23:19 +0000213 sqlite3SafetyOn(db);
drhe0bc4042002-06-25 01:09:11 +0000214
215 /* Create a cursor to hold the database open
216 */
drh1c2d8412003-03-31 00:30:47 +0000217 if( db->aDb[iDb].pBt==0 ) return SQLITE_OK;
danielk19778e150812004-05-10 01:17:37 +0000218 rc = sqlite3BtreeCursor(db->aDb[iDb].pBt, MASTER_ROOT, 0, 0, 0, &curMain);
drhf328bc82004-05-10 23:29:49 +0000219 if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){
danielk1977f20b21c2004-05-31 23:56:42 +0000220 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drh92ed08a2002-07-30 18:43:40 +0000221 return rc;
222 }
drhe0bc4042002-06-25 01:09:11 +0000223
drha3b321d2004-05-11 09:31:31 +0000224 /* Get the database meta information.
225 **
226 ** Meta values are as follows:
227 ** meta[0] Schema cookie. Changes with each schema change.
228 ** meta[1] File format of schema layer.
229 ** meta[2] Size of the page cache.
danielk197791cf71b2004-06-26 06:37:06 +0000230 ** meta[3]
danielk1977dc8453f2004-06-12 00:42:34 +0000231 ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE
danielk197791cf71b2004-06-26 06:37:06 +0000232 ** meta[5]
danielk1977962398d2004-06-14 09:35:16 +0000233 ** meta[6]
drha3b321d2004-05-11 09:31:31 +0000234 ** meta[7]
235 ** meta[8]
236 ** meta[9]
danielk1977172bc392004-05-22 08:09:11 +0000237 **
danielk1977dc8453f2004-06-12 00:42:34 +0000238 ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to
danielk1977172bc392004-05-22 08:09:11 +0000239 ** the possible values of meta[4].
drhe0bc4042002-06-25 01:09:11 +0000240 */
drhf328bc82004-05-10 23:29:49 +0000241 if( rc==SQLITE_OK ){
242 int i;
drha3b321d2004-05-11 09:31:31 +0000243 for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){
danielk1977e0d4b062004-06-28 01:11:46 +0000244 rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, i+1, (u32 *)&meta[i]);
danielk19774adee202004-05-08 08:23:19 +0000245 }
drhf328bc82004-05-10 23:29:49 +0000246 if( rc ){
danielk1977f20b21c2004-05-31 23:56:42 +0000247 sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0);
drhf328bc82004-05-10 23:29:49 +0000248 sqlite3BtreeCloseCursor(curMain);
249 return rc;
250 }
251 }else{
252 memset(meta, 0, sizeof(meta));
drhe0bc4042002-06-25 01:09:11 +0000253 }
drha3b321d2004-05-11 09:31:31 +0000254 db->aDb[iDb].schema_cookie = meta[0];
danielk19773df6b252004-05-29 10:23:19 +0000255
256 /* If opening a non-empty database, check the text encoding. For the
257 ** main database, set sqlite3.enc to the encoding of the main database.
258 ** For an attached db, it is an error if the encoding is not the same
259 ** as sqlite3.enc.
260 */
261 if( meta[4] ){ /* text encoding */
262 if( iDb==0 ){
263 /* If opening the main database, set db->enc. */
danielk1977172bc392004-05-22 08:09:11 +0000264 db->enc = (u8)meta[4];
danielk1977466be562004-06-10 02:16:01 +0000265 db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0);
danielk19773df6b252004-05-29 10:23:19 +0000266 }else{
267 /* If opening an attached database, the encoding much match db->enc */
268 if( meta[4]!=db->enc ){
269 sqlite3BtreeCloseCursor(curMain);
270 sqlite3SetString(pzErrMsg, "attached databases must use the same"
271 " text encoding as main database", (char*)0);
272 return SQLITE_ERROR;
273 }
danielk1977172bc392004-05-22 08:09:11 +0000274 }
danielk19773df6b252004-05-29 10:23:19 +0000275 }
276
danielk197791cf71b2004-06-26 06:37:06 +0000277 size = meta[2];
278 if( size==0 ){ size = MAX_PAGES; }
279 db->aDb[iDb].cache_size = size;
drhe0bc4042002-06-25 01:09:11 +0000280
danielk197791cf71b2004-06-26 06:37:06 +0000281 if( iDb==0 ){
danielk19773df6b252004-05-29 10:23:19 +0000282 db->file_format = meta[1];
drh1c2d8412003-03-31 00:30:47 +0000283 if( db->file_format==0 ){
284 /* This happens if the database was initially empty */
drhf328bc82004-05-10 23:29:49 +0000285 db->file_format = 1;
drh1c2d8412003-03-31 00:30:47 +0000286 }
drh28037572000-08-02 13:47:41 +0000287 }
danielk19773df6b252004-05-29 10:23:19 +0000288
289 /*
290 ** file_format==1 Version 3.0.0.
291 */
292 if( meta[1]>1 ){
293 sqlite3BtreeCloseCursor(curMain);
294 sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
295 return SQLITE_ERROR;
296 }
297
danielk197791cf71b2004-06-26 06:37:06 +0000298 sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size);
drhaacc5432002-01-06 17:07:40 +0000299
drhe0bc4042002-06-25 01:09:11 +0000300 /* Read the schema information out of the schema tables
drhaacc5432002-01-06 17:07:40 +0000301 */
drh1d85d932004-02-14 23:05:52 +0000302 assert( db->init.busy );
drhf328bc82004-05-10 23:29:49 +0000303 if( rc==SQLITE_EMPTY ){
304 /* For an empty database, there is nothing to read */
305 rc = SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000306 }else{
danielk1977d008cfe2004-06-19 02:22:10 +0000307 char *zSql = 0;
danielk19773df6b252004-05-29 10:23:19 +0000308 sqlite3SafetyOff(db);
danielk1977d008cfe2004-06-19 02:22:10 +0000309 sqlite3SetString(&zSql,
310 "SELECT type, name, rootpage, sql, ", zDbNum, " FROM \"",
311 db->aDb[iDb].zName, "\".", zMasterName, (char*)0);
312 rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
313 sqliteFree(zSql);
drhf328bc82004-05-10 23:29:49 +0000314 sqlite3SafetyOn(db);
315 sqlite3BtreeCloseCursor(curMain);
drh1c2d8412003-03-31 00:30:47 +0000316 }
danielk197724b03fd2004-05-10 10:34:34 +0000317 if( sqlite3_malloc_failed ){
danielk19774adee202004-05-08 08:23:19 +0000318 sqlite3SetString(pzErrMsg, "out of memory", (char*)0);
drh1d85d932004-02-14 23:05:52 +0000319 rc = SQLITE_NOMEM;
danielk19774adee202004-05-08 08:23:19 +0000320 sqlite3ResetInternalSchema(db, 0);
drhe0bc4042002-06-25 01:09:11 +0000321 }
drh1d85d932004-02-14 23:05:52 +0000322 if( rc==SQLITE_OK ){
drh8bf8dc92003-05-17 17:35:10 +0000323 DbSetProperty(db, iDb, DB_SchemaLoaded);
drh1c2d8412003-03-31 00:30:47 +0000324 }else{
danielk19774adee202004-05-08 08:23:19 +0000325 sqlite3ResetInternalSchema(db, iDb);
drh1c2d8412003-03-31 00:30:47 +0000326 }
drh1d85d932004-02-14 23:05:52 +0000327 return rc;
drh1c2d8412003-03-31 00:30:47 +0000328}
329
330/*
331** Initialize all database files - the main database file, the file
332** used to store temporary tables, and any additional database files
333** created using ATTACH statements. Return a success code. If an
334** error occurs, write an error message into *pzErrMsg.
335**
336** After the database is initialized, the SQLITE_Initialized
danielk19778e227872004-06-07 07:52:17 +0000337** bit is set in the flags field of the sqlite structure.
drh1c2d8412003-03-31 00:30:47 +0000338*/
danielk19774adee202004-05-08 08:23:19 +0000339int sqlite3Init(sqlite *db, char **pzErrMsg){
drh1c2d8412003-03-31 00:30:47 +0000340 int i, rc;
341
drh1d85d932004-02-14 23:05:52 +0000342 if( db->init.busy ) return SQLITE_OK;
drh1c2d8412003-03-31 00:30:47 +0000343 assert( (db->flags & SQLITE_Initialized)==0 );
344 rc = SQLITE_OK;
drh1d85d932004-02-14 23:05:52 +0000345 db->init.busy = 1;
drh1c2d8412003-03-31 00:30:47 +0000346 for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
danielk1977d008cfe2004-06-19 02:22:10 +0000347 if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
danielk19774adee202004-05-08 08:23:19 +0000348 rc = sqlite3InitOne(db, i, pzErrMsg);
drh8ef83ff2004-02-12 15:31:21 +0000349 if( rc ){
danielk19774adee202004-05-08 08:23:19 +0000350 sqlite3ResetInternalSchema(db, i);
drh8ef83ff2004-02-12 15:31:21 +0000351 }
drh1c2d8412003-03-31 00:30:47 +0000352 }
danielk1977d008cfe2004-06-19 02:22:10 +0000353
354 /* Once all the other databases have been initialised, load the schema
355 ** for the TEMP database. This is loaded last, as the TEMP database
356 ** schema may contain references to objects in other databases.
357 */
358 if( rc==SQLITE_OK && db->nDb>1 && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
359 rc = sqlite3InitOne(db, 1, pzErrMsg);
360 if( rc ){
361 sqlite3ResetInternalSchema(db, 1);
362 }
363 }
364
drh1d85d932004-02-14 23:05:52 +0000365 db->init.busy = 0;
drh1c2d8412003-03-31 00:30:47 +0000366 if( rc==SQLITE_OK ){
drh58b95762000-06-02 01:17:37 +0000367 db->flags |= SQLITE_Initialized;
danielk19774adee202004-05-08 08:23:19 +0000368 sqlite3CommitInternalChanges(db);
drh2d71ca92004-02-10 02:27:04 +0000369 }
370
drh2d71ca92004-02-10 02:27:04 +0000371 if( rc!=SQLITE_OK ){
drhe0bc4042002-06-25 01:09:11 +0000372 db->flags &= ~SQLITE_Initialized;
drh58b95762000-06-02 01:17:37 +0000373 }
drh1c2d8412003-03-31 00:30:47 +0000374 return rc;
drh58b95762000-06-02 01:17:37 +0000375}
376
377/*
danielk19778e227872004-06-07 07:52:17 +0000378** This routine is a no-op if the database schema is already initialised.
379** Otherwise, the schema is loaded. An error code is returned.
380*/
danielk19778a414492004-06-29 08:59:35 +0000381int sqlite3ReadSchema(Parse *pParse){
danielk19778e227872004-06-07 07:52:17 +0000382 int rc = SQLITE_OK;
danielk19778a414492004-06-29 08:59:35 +0000383 sqlite3 *db = pParse->db;
danielk19778e227872004-06-07 07:52:17 +0000384 if( !db->init.busy ){
385 if( (db->flags & SQLITE_Initialized)==0 ){
danielk19778a414492004-06-29 08:59:35 +0000386 rc = sqlite3Init(db, &pParse->zErrMsg);
danielk19778e227872004-06-07 07:52:17 +0000387 }
388 }
danielk1977c0391392004-06-09 12:30:04 +0000389 assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized)||db->init.busy );
danielk19778a414492004-06-29 08:59:35 +0000390 if( rc!=SQLITE_OK ){
391 pParse->rc = rc;
392 pParse->nErr++;
393 }
danielk19778e227872004-06-07 07:52:17 +0000394 return rc;
395}
396
397/*
drhb217a572000-08-22 13:40:18 +0000398** The version of the library
399*/
drh38f82712004-06-18 17:10:16 +0000400const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $";
danielk197724b03fd2004-05-10 10:34:34 +0000401const char sqlite3_version[] = SQLITE_VERSION;
drhb217a572000-08-22 13:40:18 +0000402
403/*
drhd3d39e92004-05-20 22:16:29 +0000404** This is the default collating function named "BINARY" which is always
405** available.
406*/
407static int binaryCollatingFunc(
408 void *NotUsed,
409 int nKey1, const void *pKey1,
410 int nKey2, const void *pKey2
411){
412 int rc, n;
413 n = nKey1<nKey2 ? nKey1 : nKey2;
414 rc = memcmp(pKey1, pKey2, n);
415 if( rc==0 ){
416 rc = nKey1 - nKey2;
417 }
418 return rc;
419}
420
421/*
danielk1977dc1bdc42004-06-11 10:51:27 +0000422** Another built-in collating sequence: NOCASE.
423**
424** This collating sequence is intended to be used for "case independant
425** comparison". SQLite's knowledge of upper and lower case equivalents
426** extends only to the 26 characters used in the English language.
427**
428** At the moment there is only a UTF-8 implementation.
danielk19770202b292004-06-09 09:55:16 +0000429*/
430static int nocaseCollatingFunc(
431 void *NotUsed,
432 int nKey1, const void *pKey1,
433 int nKey2, const void *pKey2
434){
435 int r = sqlite3StrNICmp(
436 (const char *)pKey1, (const char *)pKey2, (nKey1>nKey2)?nKey1:nKey2);
437 if( 0==r ){
438 r = nKey1-nKey2;
439 }
440 return r;
441}
442
443/*
drhaf9ff332002-01-16 21:00:27 +0000444** Return the ROWID of the most recent insert
445*/
drhefad9992004-06-22 12:13:55 +0000446sqlite_int64 sqlite3_last_insert_rowid(sqlite *db){
drhaf9ff332002-01-16 21:00:27 +0000447 return db->lastRowid;
448}
449
450/*
danielk197724b03fd2004-05-10 10:34:34 +0000451** Return the number of changes in the most recent call to sqlite3_exec().
drhc8d30ac2002-04-12 10:08:59 +0000452*/
danielk197724b03fd2004-05-10 10:34:34 +0000453int sqlite3_changes(sqlite *db){
drhc8d30ac2002-04-12 10:08:59 +0000454 return db->nChange;
455}
456
rdcf146a772004-02-25 22:51:06 +0000457/*
danielk1977b28af712004-06-21 06:50:26 +0000458** Return the number of changes since the database handle was opened.
rdcf146a772004-02-25 22:51:06 +0000459*/
danielk1977b28af712004-06-21 06:50:26 +0000460int sqlite3_total_changes(sqlite3 *db){
461 return db->nTotalChange;
rdcb0c374f2004-02-20 22:53:38 +0000462}
463
drhc8d30ac2002-04-12 10:08:59 +0000464/*
drh50e5dad2001-09-15 00:57:28 +0000465** Close an existing SQLite database
466*/
danielk197796d81f92004-06-19 03:33:57 +0000467int sqlite3_close(sqlite *db){
drh8e0a2f92002-02-23 23:45:45 +0000468 HashElem *i;
drh001bbcb2003-03-19 03:14:00 +0000469 int j;
danielk19775c4c7782004-06-16 10:39:23 +0000470
471 if( !db ){
danielk197796d81f92004-06-19 03:33:57 +0000472 return SQLITE_OK;
danielk19775c4c7782004-06-16 10:39:23 +0000473 }
danielk197796d81f92004-06-19 03:33:57 +0000474
danielk1977e35ee192004-06-26 09:50:11 +0000475 if( db->magic!=SQLITE_MAGIC_CLOSED &&
476 db->magic!=SQLITE_MAGIC_OPEN &&
477 db->magic!=SQLITE_MAGIC_BUSY
478 ){
479 return SQLITE_MISUSE;
480 }
481
danielk197796d81f92004-06-19 03:33:57 +0000482 /* If there are any outstanding VMs, return SQLITE_BUSY. */
483 if( db->pVdbe ){
484 sqlite3Error(db, SQLITE_BUSY,
485 "Unable to close due to unfinalised statements");
486 return SQLITE_BUSY;
487 }
488 assert( !sqlite3SafetyCheck(db) );
danielk1977e0048402004-06-15 16:51:01 +0000489
490 /* FIX ME: db->magic may be set to SQLITE_MAGIC_CLOSED if the database
491 ** cannot be opened for some reason. So this routine needs to run in
492 ** that case. But maybe there should be an extra magic value for the
493 ** "failed to open" state.
494 */
danielk197796d81f92004-06-19 03:33:57 +0000495 if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){
drh94e92032003-02-16 22:21:32 +0000496 /* printf("DID NOT CLOSE\n"); fflush(stdout); */
danielk197796d81f92004-06-19 03:33:57 +0000497 return SQLITE_ERROR;
drh94e92032003-02-16 22:21:32 +0000498 }
danielk1977e0048402004-06-15 16:51:01 +0000499
drh001bbcb2003-03-19 03:14:00 +0000500 for(j=0; j<db->nDb; j++){
drh4d189ca2004-02-12 18:46:38 +0000501 struct Db *pDb = &db->aDb[j];
502 if( pDb->pBt ){
danielk19774adee202004-05-08 08:23:19 +0000503 sqlite3BtreeClose(pDb->pBt);
drh4d189ca2004-02-12 18:46:38 +0000504 pDb->pBt = 0;
drh113088e2003-03-20 01:16:58 +0000505 }
drhf57b3392001-10-08 13:22:32 +0000506 }
danielk19774adee202004-05-08 08:23:19 +0000507 sqlite3ResetInternalSchema(db, 0);
drh1c2d8412003-03-31 00:30:47 +0000508 assert( db->nDb<=2 );
509 assert( db->aDb==db->aDbStatic );
drh0bce8352002-02-28 00:41:10 +0000510 for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){
511 FuncDef *pFunc, *pNext;
512 for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){
drh8e0a2f92002-02-23 23:45:45 +0000513 pNext = pFunc->pNext;
514 sqliteFree(pFunc);
515 }
516 }
danielk1977466be562004-06-10 02:16:01 +0000517
danielk1977d8123362004-06-12 09:25:12 +0000518 for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
danielk1977466be562004-06-10 02:16:01 +0000519 CollSeq *pColl = (CollSeq *)sqliteHashData(i);
danielk1977d8123362004-06-12 09:25:12 +0000520 sqliteFree(pColl);
danielk1977466be562004-06-10 02:16:01 +0000521 }
danielk1977d8123362004-06-12 09:25:12 +0000522 sqlite3HashClear(&db->aCollSeq);
danielk1977466be562004-06-10 02:16:01 +0000523
danielk19774adee202004-05-08 08:23:19 +0000524 sqlite3HashClear(&db->aFunc);
danielk19776622cce2004-05-20 11:00:52 +0000525 sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
danielk1977bfd6cce2004-06-18 04:24:54 +0000526 if( db->pValue ){
527 sqlite3ValueFree(db->pValue);
528 }
529 if( db->pErr ){
530 sqlite3ValueFree(db->pErr);
531 }
danielk197796d81f92004-06-19 03:33:57 +0000532
533 db->magic = SQLITE_MAGIC_ERROR;
drh75897232000-05-29 14:26:00 +0000534 sqliteFree(db);
danielk197796d81f92004-06-19 03:33:57 +0000535 return SQLITE_OK;
drh75897232000-05-29 14:26:00 +0000536}
537
538/*
drh001bbcb2003-03-19 03:14:00 +0000539** Rollback all database files.
540*/
danielk19774adee202004-05-08 08:23:19 +0000541void sqlite3RollbackAll(sqlite *db){
drh001bbcb2003-03-19 03:14:00 +0000542 int i;
543 for(i=0; i<db->nDb; i++){
544 if( db->aDb[i].pBt ){
danielk19774adee202004-05-08 08:23:19 +0000545 sqlite3BtreeRollback(db->aDb[i].pBt);
drh001bbcb2003-03-19 03:14:00 +0000546 db->aDb[i].inTrans = 0;
547 }
548 }
danielk19774adee202004-05-08 08:23:19 +0000549 sqlite3ResetInternalSchema(db, 0);
550 /* sqlite3RollbackInternalChanges(db); */
drh001bbcb2003-03-19 03:14:00 +0000551}
552
553/*
drhc22bd472002-05-10 13:14:07 +0000554** Return a static string that describes the kind of error specified in the
555** argument.
drh247be432002-05-10 05:44:55 +0000556*/
danielk1977f20b21c2004-05-31 23:56:42 +0000557const char *sqlite3ErrStr(int rc){
drhc22bd472002-05-10 13:14:07 +0000558 const char *z;
559 switch( rc ){
560 case SQLITE_OK: z = "not an error"; break;
561 case SQLITE_ERROR: z = "SQL logic error or missing database"; break;
562 case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break;
563 case SQLITE_PERM: z = "access permission denied"; break;
564 case SQLITE_ABORT: z = "callback requested query abort"; break;
565 case SQLITE_BUSY: z = "database is locked"; break;
566 case SQLITE_LOCKED: z = "database table is locked"; break;
567 case SQLITE_NOMEM: z = "out of memory"; break;
568 case SQLITE_READONLY: z = "attempt to write a readonly database"; break;
569 case SQLITE_INTERRUPT: z = "interrupted"; break;
570 case SQLITE_IOERR: z = "disk I/O error"; break;
571 case SQLITE_CORRUPT: z = "database disk image is malformed"; break;
572 case SQLITE_NOTFOUND: z = "table or record not found"; break;
573 case SQLITE_FULL: z = "database is full"; break;
574 case SQLITE_CANTOPEN: z = "unable to open database file"; break;
575 case SQLITE_PROTOCOL: z = "database locking protocol failure"; break;
576 case SQLITE_EMPTY: z = "table contains no data"; break;
577 case SQLITE_SCHEMA: z = "database schema has changed"; break;
578 case SQLITE_TOOBIG: z = "too much data for one table row"; break;
579 case SQLITE_CONSTRAINT: z = "constraint failed"; break;
580 case SQLITE_MISMATCH: z = "datatype mismatch"; break;
581 case SQLITE_MISUSE: z = "library routine called out of sequence";break;
drh8766c342002-11-09 00:33:15 +0000582 case SQLITE_NOLFS: z = "kernel lacks large file support"; break;
drhed6c8672003-01-12 18:02:16 +0000583 case SQLITE_AUTH: z = "authorization denied"; break;
jplyon892f6712003-06-12 08:59:00 +0000584 case SQLITE_FORMAT: z = "auxiliary database format error"; break;
drh7c972de2003-09-06 22:18:07 +0000585 case SQLITE_RANGE: z = "bind index out of range"; break;
drhc602f9a2004-02-12 19:01:04 +0000586 case SQLITE_NOTADB: z = "file is encrypted or is not a database";break;
drhc22bd472002-05-10 13:14:07 +0000587 default: z = "unknown error"; break;
drh247be432002-05-10 05:44:55 +0000588 }
drhc22bd472002-05-10 13:14:07 +0000589 return z;
drh247be432002-05-10 05:44:55 +0000590}
591
592/*
drh2dfbbca2000-07-28 14:32:48 +0000593** This routine implements a busy callback that sleeps and tries
594** again until a timeout value is reached. The timeout value is
595** an integer number of milliseconds passed in as the first
596** argument.
597*/
drhdaffd0e2001-04-11 14:28:42 +0000598static int sqliteDefaultBusyCallback(
drh2dfbbca2000-07-28 14:32:48 +0000599 void *Timeout, /* Maximum amount of time to wait */
drh2dfbbca2000-07-28 14:32:48 +0000600 int count /* Number of times table has been busy */
601){
drh8cfbf082001-09-19 13:22:39 +0000602#if SQLITE_MIN_SLEEP_MS==1
drhd1bec472004-01-15 13:29:31 +0000603 static const char delays[] =
604 { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 50, 100};
605 static const short int totals[] =
606 { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228, 287};
607# define NDELAY (sizeof(delays)/sizeof(delays[0]))
drh2dfbbca2000-07-28 14:32:48 +0000608 int timeout = (int)Timeout;
drhd1bec472004-01-15 13:29:31 +0000609 int delay, prior;
drh2dfbbca2000-07-28 14:32:48 +0000610
drhd1bec472004-01-15 13:29:31 +0000611 if( count <= NDELAY ){
612 delay = delays[count-1];
613 prior = totals[count-1];
614 }else{
615 delay = delays[NDELAY-1];
616 prior = totals[NDELAY-1] + delay*(count-NDELAY-1);
drh2dfbbca2000-07-28 14:32:48 +0000617 }
drhd1bec472004-01-15 13:29:31 +0000618 if( prior + delay > timeout ){
619 delay = timeout - prior;
drh2dfbbca2000-07-28 14:32:48 +0000620 if( delay<=0 ) return 0;
621 }
danielk19774adee202004-05-08 08:23:19 +0000622 sqlite3OsSleep(delay);
drh2dfbbca2000-07-28 14:32:48 +0000623 return 1;
624#else
625 int timeout = (int)Timeout;
626 if( (count+1)*1000 > timeout ){
627 return 0;
628 }
danielk19774adee202004-05-08 08:23:19 +0000629 sqlite3OsSleep(1000);
drh2dfbbca2000-07-28 14:32:48 +0000630 return 1;
631#endif
632}
633
634/*
635** This routine sets the busy callback for an Sqlite database to the
636** given callback function with the given argument.
637*/
danielk1977f9d64d22004-06-19 08:18:07 +0000638int sqlite3_busy_handler(
639 sqlite3 *db,
danielk19772a764eb2004-06-12 01:43:26 +0000640 int (*xBusy)(void*,int),
drh2dfbbca2000-07-28 14:32:48 +0000641 void *pArg
642){
danielk197724162fe2004-06-04 06:22:00 +0000643 db->busyHandler.xFunc = xBusy;
644 db->busyHandler.pArg = pArg;
danielk1977f9d64d22004-06-19 08:18:07 +0000645 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000646}
647
danielk1977348bb5d2003-10-18 09:37:26 +0000648#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
649/*
650** This routine sets the progress callback for an Sqlite database to the
651** given callback function with the given argument. The progress callback will
652** be invoked every nOps opcodes.
653*/
danielk197724b03fd2004-05-10 10:34:34 +0000654void sqlite3_progress_handler(
danielk1977348bb5d2003-10-18 09:37:26 +0000655 sqlite *db,
656 int nOps,
657 int (*xProgress)(void*),
658 void *pArg
659){
660 if( nOps>0 ){
661 db->xProgress = xProgress;
662 db->nProgressOps = nOps;
663 db->pProgressArg = pArg;
664 }else{
665 db->xProgress = 0;
666 db->nProgressOps = 0;
667 db->pProgressArg = 0;
668 }
669}
670#endif
671
672
drh2dfbbca2000-07-28 14:32:48 +0000673/*
674** This routine installs a default busy handler that waits for the
675** specified number of milliseconds before returning 0.
676*/
danielk1977f9d64d22004-06-19 08:18:07 +0000677int sqlite3_busy_timeout(sqlite3 *db, int ms){
drh2dfbbca2000-07-28 14:32:48 +0000678 if( ms>0 ){
danielk197724b03fd2004-05-10 10:34:34 +0000679 sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms);
drh2dfbbca2000-07-28 14:32:48 +0000680 }else{
danielk197724b03fd2004-05-10 10:34:34 +0000681 sqlite3_busy_handler(db, 0, 0);
drh2dfbbca2000-07-28 14:32:48 +0000682 }
danielk1977f9d64d22004-06-19 08:18:07 +0000683 return SQLITE_OK;
drh2dfbbca2000-07-28 14:32:48 +0000684}
drh4c504392000-10-16 22:06:40 +0000685
686/*
687** Cause any pending operation to stop at its earliest opportunity.
688*/
danielk197724b03fd2004-05-10 10:34:34 +0000689void sqlite3_interrupt(sqlite *db){
drh4c504392000-10-16 22:06:40 +0000690 db->flags |= SQLITE_Interrupt;
691}
drhfa86c412002-02-02 15:01:15 +0000692
693/*
694** Windows systems should call this routine to free memory that
danielk197724b03fd2004-05-10 10:34:34 +0000695** is returned in the in the errmsg parameter of sqlite3_open() when
drhfa86c412002-02-02 15:01:15 +0000696** SQLite is a DLL. For some reason, it does not work to call free()
697** directly.
698**
699** Note that we need to call free() not sqliteFree() here, since every
700** string that is exported from SQLite should have already passed through
danielk19774adee202004-05-08 08:23:19 +0000701** sqlite3StrRealloc().
drhfa86c412002-02-02 15:01:15 +0000702*/
drh3f4fedb2004-05-31 19:34:33 +0000703void sqlite3_free(char *p){ free(p); }
drhfa86c412002-02-02 15:01:15 +0000704
705/*
drhdf014892004-06-02 00:41:09 +0000706** Create new user functions.
drhfa86c412002-02-02 15:01:15 +0000707*/
danielk197724b03fd2004-05-10 10:34:34 +0000708int sqlite3_create_function(
danielk197765904932004-05-26 06:18:37 +0000709 sqlite3 *db,
710 const char *zFunctionName,
711 int nArg,
danielk1977d8123362004-06-12 09:25:12 +0000712 int enc,
danielk197765904932004-05-26 06:18:37 +0000713 void *pUserData,
714 void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
715 void (*xStep)(sqlite3_context*,int,sqlite3_value **),
716 void (*xFinal)(sqlite3_context*)
drh8e0a2f92002-02-23 23:45:45 +0000717){
drh0bce8352002-02-28 00:41:10 +0000718 FuncDef *p;
drh4b59ab52002-08-24 18:24:51 +0000719 int nName;
danielk197765904932004-05-26 06:18:37 +0000720
danielk1977398eae72004-05-26 06:58:43 +0000721 if( (db==0 || zFunctionName==0 || sqlite3SafetyCheck(db)) ||
722 (xFunc && (xFinal || xStep)) ||
723 (!xFunc && (xFinal && !xStep)) ||
724 (!xFunc && (!xFinal && xStep)) ||
725 (nArg<-1 || nArg>127) ||
726 (255<(nName = strlen(zFunctionName))) ){
danielk197765904932004-05-26 06:18:37 +0000727 return SQLITE_ERROR;
728 }
danielk1977d8123362004-06-12 09:25:12 +0000729
730 /* If SQLITE_UTF16 is specified as the encoding type, transform this
731 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
732 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
733 **
734 ** If SQLITE_ANY is specified, add three versions of the function
735 ** to the hash table.
736 */
737 if( enc==SQLITE_UTF16 ){
738 enc = SQLITE_UTF16NATIVE;
739 }else if( enc==SQLITE_ANY ){
740 int rc;
741 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF8,
danielk1977f9d64d22004-06-19 08:18:07 +0000742 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000743 if( rc!=SQLITE_OK ) return rc;
744 rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE,
danielk1977f9d64d22004-06-19 08:18:07 +0000745 pUserData, xFunc, xStep, xFinal);
danielk1977d8123362004-06-12 09:25:12 +0000746 if( rc!=SQLITE_OK ) return rc;
747 enc = SQLITE_UTF16BE;
748 }
danielk197765904932004-05-26 06:18:37 +0000749
danielk1977d8123362004-06-12 09:25:12 +0000750 p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1);
drh4e0f9952002-02-27 01:53:13 +0000751 if( p==0 ) return 1;
drh8e0a2f92002-02-23 23:45:45 +0000752 p->xFunc = xFunc;
drh8e0a2f92002-02-23 23:45:45 +0000753 p->xStep = xStep;
danielk197765904932004-05-26 06:18:37 +0000754 p->xFinalize = xFinal;
drh1350b032002-02-27 19:00:20 +0000755 p->pUserData = pUserData;
danielk197765904932004-05-26 06:18:37 +0000756 return SQLITE_OK;
757}
danielk197765904932004-05-26 06:18:37 +0000758int sqlite3_create_function16(
759 sqlite3 *db,
760 const void *zFunctionName,
761 int nArg,
762 int eTextRep,
danielk197765904932004-05-26 06:18:37 +0000763 void *pUserData,
764 void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
765 void (*xStep)(sqlite3_context*,int,sqlite3_value**),
766 void (*xFinal)(sqlite3_context*)
767){
768 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +0000769 char const *zFunc8;
770
771 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
772 sqlite3ValueSetStr(pTmp, -1, zFunctionName, SQLITE_UTF16NATIVE,SQLITE_STATIC);
773 zFunc8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
774
775 if( !zFunc8 ){
danielk197765904932004-05-26 06:18:37 +0000776 return SQLITE_NOMEM;
777 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000778 rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep,
danielk1977f9d64d22004-06-19 08:18:07 +0000779 pUserData, xFunc, xStep, xFinal);
danielk197765904932004-05-26 06:18:37 +0000780 return rc;
drh8e0a2f92002-02-23 23:45:45 +0000781}
drhc9b84a12002-06-20 11:36:48 +0000782
783/*
drh18de4822003-01-16 16:28:53 +0000784** Register a trace function. The pArg from the previously registered trace
785** is returned.
786**
787** A NULL trace function means that no tracing is executes. A non-NULL
788** trace is a pointer to a function that is invoked at the start of each
danielk197724b03fd2004-05-10 10:34:34 +0000789** sqlite3_exec().
drh18de4822003-01-16 16:28:53 +0000790*/
danielk197724b03fd2004-05-10 10:34:34 +0000791void *sqlite3_trace(sqlite *db, void (*xTrace)(void*,const char*), void *pArg){
drh18de4822003-01-16 16:28:53 +0000792 void *pOld = db->pTraceArg;
793 db->xTrace = xTrace;
794 db->pTraceArg = pArg;
795 return pOld;
drh0d1a6432003-04-03 15:46:04 +0000796}
paulb0208cc2003-04-13 18:26:49 +0000797
drhaa940ea2004-01-15 02:44:03 +0000798/*** EXPERIMENTAL ***
799**
800** Register a function to be invoked when a transaction comments.
801** If either function returns non-zero, then the commit becomes a
802** rollback.
803*/
danielk197724b03fd2004-05-10 10:34:34 +0000804void *sqlite3_commit_hook(
drhaa940ea2004-01-15 02:44:03 +0000805 sqlite *db, /* Attach the hook to this database */
806 int (*xCallback)(void*), /* Function to invoke on each commit */
807 void *pArg /* Argument to the function */
808){
809 void *pOld = db->pCommitArg;
810 db->xCommitCallback = xCallback;
811 db->pCommitArg = pArg;
812 return pOld;
813}
814
815
paulb0208cc2003-04-13 18:26:49 +0000816/*
drh13bff812003-04-15 01:19:47 +0000817** This routine is called to create a connection to a database BTree
818** driver. If zFilename is the name of a file, then that file is
819** opened and used. If zFilename is the magic name ":memory:" then
820** the database is stored in memory (and is thus forgotten as soon as
821** the connection is closed.) If zFilename is NULL then the database
822** is for temporary use only and is deleted as soon as the connection
823** is closed.
paulb0208cc2003-04-13 18:26:49 +0000824*/
danielk19774adee202004-05-08 08:23:19 +0000825int sqlite3BtreeFactory(
paulb0208cc2003-04-13 18:26:49 +0000826 const sqlite *db, /* Main database when opening aux otherwise 0 */
827 const char *zFilename, /* Name of the file containing the BTree database */
828 int omitJournal, /* if TRUE then do not journal this file */
829 int nCache, /* How many pages in the page cache */
danielk19774adee202004-05-08 08:23:19 +0000830 Btree **ppBtree /* Pointer to new Btree object written here */
831){
danielk19774adee202004-05-08 08:23:19 +0000832 int btree_flags = 0;
833
drheec983e2004-05-08 10:11:36 +0000834 assert( ppBtree != 0);
danielk19774adee202004-05-08 08:23:19 +0000835 if( omitJournal ){
836 btree_flags |= BTREE_OMIT_JOURNAL;
paulb0208cc2003-04-13 18:26:49 +0000837 }
danielk197791cf71b2004-06-26 06:37:06 +0000838 if( !zFilename || !strcmp(zFilename, ":memory:") ){
839 /* If zFilename is NULL or the magic string ":memory:" then the
840 ** new btree storest data in main memory, not a file.
841 */
danielk19774adee202004-05-08 08:23:19 +0000842 btree_flags |= BTREE_MEMORY;
843 }
844
danielk197724162fe2004-06-04 06:22:00 +0000845 return sqlite3BtreeOpen(zFilename, ppBtree, nCache, btree_flags,
danielk1977d8123362004-06-12 09:25:12 +0000846 (void *)&db->busyHandler);
paulb0208cc2003-04-13 18:26:49 +0000847}
danielk19774adee202004-05-08 08:23:19 +0000848
danielk19774ad17132004-05-21 01:47:26 +0000849/*
850** Return UTF-8 encoded English language explanation of the most recent
851** error.
852*/
danielk19776622cce2004-05-20 11:00:52 +0000853const char *sqlite3_errmsg(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000854 if( !db || !db->pErr ){
danielk19774ad17132004-05-21 01:47:26 +0000855 /* If db is NULL, then assume that a malloc() failed during an
856 ** sqlite3_open() call.
857 */
danielk1977f20b21c2004-05-31 23:56:42 +0000858 return sqlite3ErrStr(SQLITE_NOMEM);
danielk19774ad17132004-05-21 01:47:26 +0000859 }
danielk1977b2c7f5b2004-06-26 10:02:16 +0000860 if( db->magic!=SQLITE_MAGIC_OPEN &&
861 db->magic!=SQLITE_MAGIC_BUSY &&
862 db->magic!=SQLITE_MAGIC_CLOSED
863 ){
danielk1977e35ee192004-06-26 09:50:11 +0000864 return sqlite3ErrStr(SQLITE_MISUSE);
865 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000866 if( !sqlite3_value_text(db->pErr) ){
867 return sqlite3ErrStr(db->errCode);
danielk19776622cce2004-05-20 11:00:52 +0000868 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000869 return sqlite3_value_text(db->pErr);
danielk19776622cce2004-05-20 11:00:52 +0000870}
871
danielk19774ad17132004-05-21 01:47:26 +0000872/*
873** Return UTF-16 encoded English language explanation of the most recent
874** error.
875*/
danielk19776622cce2004-05-20 11:00:52 +0000876const void *sqlite3_errmsg16(sqlite3 *db){
danielk1977bfd6cce2004-06-18 04:24:54 +0000877 /* Because all the characters in the string are in the unicode
878 ** range 0x00-0xFF, if we pad the big-endian string with a
879 ** zero byte, we can obtain the little-endian string with
880 ** &big_endian[1].
881 */
882 static char outOfMemBe[] = {
883 0, 'o', 0, 'u', 0, 't', 0, ' ',
884 0, 'o', 0, 'f', 0, ' ',
885 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0
886 };
danielk1977e35ee192004-06-26 09:50:11 +0000887 static char misuseBe [] = {
888 0, 'l', 0, 'i', 0, 'b', 0, 'r', 0, 'a', 0, 'r', 0, 'y', 0, ' ',
889 0, 'r', 0, 'o', 0, 'u', 0, 't', 0, 'i', 0, 'n', 0, 'e', 0, ' ',
890 0, 'c', 0, 'a', 0, 'l', 0, 'l', 0, 'e', 0, 'd', 0, ' ',
891 0, 'o', 0, 'u', 0, 't', 0, ' ',
892 0, 'o', 0, 'f', 0, ' ',
893 0, 's', 0, 'e', 0, 'q', 0, 'u', 0, 'e', 0, 'n', 0, 'c', 0, 'e', 0, 0, 0
894 };
danielk19774ad17132004-05-21 01:47:26 +0000895
danielk1977bfd6cce2004-06-18 04:24:54 +0000896 if( db && db->pErr ){
danielk1977312d6b32004-06-29 13:18:23 +0000897 if( db->magic!=SQLITE_MAGIC_OPEN &&
898 db->magic!=SQLITE_MAGIC_BUSY &&
899 db->magic!=SQLITE_MAGIC_CLOSED
900 ){
danielk1977e35ee192004-06-26 09:50:11 +0000901 return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
902 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000903 if( !sqlite3_value_text16(db->pErr) ){
904 sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode),
905 SQLITE_UTF8, SQLITE_STATIC);
danielk19774ad17132004-05-21 01:47:26 +0000906 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000907 if( sqlite3_value_text16(db->pErr) ){
908 return sqlite3_value_text16(db->pErr);
danielk19776622cce2004-05-20 11:00:52 +0000909 }
danielk1977bfd6cce2004-06-18 04:24:54 +0000910 }
911
912 /* If db is NULL, then assume that a malloc() failed during an
913 ** sqlite3_open() call. We have a static version of the string
914 ** "out of memory" encoded using UTF-16 just for this purpose.
915 */
916 return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]);
danielk19776622cce2004-05-20 11:00:52 +0000917}
918
919int sqlite3_errcode(sqlite3 *db){
danielk19775c4c7782004-06-16 10:39:23 +0000920 if( !db ) return SQLITE_NOMEM;
danielk19776622cce2004-05-20 11:00:52 +0000921 return db->errCode;
922}
923
924/*
drha6ecd332004-06-10 00:29:09 +0000925** Check schema cookies in all databases except TEMP. If any cookie is out
926** of date, return 0. If all schema cookies are current, return 1.
927*/
928static int schemaIsValid(sqlite *db){
929 int iDb;
930 int rc;
931 BtCursor *curTemp;
932 int cookie;
933 int allOk = 1;
934
935 for(iDb=0; allOk && iDb<db->nDb; iDb++){
936 Btree *pBt;
937 if( iDb==1 ) continue;
938 pBt = db->aDb[iDb].pBt;
939 if( pBt==0 ) continue;
940 rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp);
941 if( rc==SQLITE_OK ){
danielk1977e0d4b062004-06-28 01:11:46 +0000942 rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie);
drha6ecd332004-06-10 00:29:09 +0000943 if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){
944 allOk = 0;
945 }
946 sqlite3BtreeCloseCursor(curTemp);
947 }
948 }
949 return allOk;
950}
951
952/*
danielk19776622cce2004-05-20 11:00:52 +0000953** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
954*/
955int sqlite3_prepare(
956 sqlite3 *db, /* Database handle. */
957 const char *zSql, /* UTF-8 encoded SQL statement. */
958 int nBytes, /* Length of zSql in bytes. */
959 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
960 const char** pzTail /* OUT: End of parsed string */
961){
962 Parse sParse;
963 char *zErrMsg = 0;
964 int rc = SQLITE_OK;
965
danielk197796fb0dd2004-06-30 09:49:22 +0000966 if( sqlite3_malloc_failed ){
967 return SQLITE_NOMEM;
968 }
969
danielk19775c4c7782004-06-16 10:39:23 +0000970 assert( ppStmt );
971 *ppStmt = 0;
danielk19776622cce2004-05-20 11:00:52 +0000972 if( sqlite3SafetyOn(db) ){
danielk1977e35ee192004-06-26 09:50:11 +0000973 return SQLITE_MISUSE;
danielk19776622cce2004-05-20 11:00:52 +0000974 }
975
danielk19776622cce2004-05-20 11:00:52 +0000976 memset(&sParse, 0, sizeof(sParse));
977 sParse.db = db;
978 sqlite3RunParser(&sParse, zSql, &zErrMsg);
979
980 if( db->xTrace && !db->init.busy ){
981 /* Trace only the statment that was compiled.
982 ** Make a copy of that part of the SQL string since zSQL is const
983 ** and we must pass a zero terminated string to the trace function
984 ** The copy is unnecessary if the tail pointer is pointing at the
985 ** beginnig or end of the SQL string.
986 */
987 if( sParse.zTail && sParse.zTail!=zSql && *sParse.zTail ){
988 char *tmpSql = sqliteStrNDup(zSql, sParse.zTail - zSql);
989 if( tmpSql ){
990 db->xTrace(db->pTraceArg, tmpSql);
drhb97759e2004-06-29 11:26:59 +0000991 sqliteFree(tmpSql);
danielk19776622cce2004-05-20 11:00:52 +0000992 }else{
993 /* If a memory error occurred during the copy,
994 ** trace entire SQL string and fall through to the
995 ** sqlite3_malloc_failed test to report the error.
996 */
997 db->xTrace(db->pTraceArg, zSql);
998 }
999 }else{
1000 db->xTrace(db->pTraceArg, zSql);
1001 }
1002 }
1003
drh35d4c2f2004-06-10 01:30:59 +00001004 /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned
1005 ** on in debugging mode.
1006 */
1007#ifdef SQLITE_DEBUG
1008 if( (db->flags & SQLITE_SqlTrace)!=0 && sParse.zTail && sParse.zTail!=zSql ){
1009 sqlite3DebugPrintf("SQL-trace: %.*s\n", sParse.zTail - zSql, zSql);
1010 }
1011#endif /* SQLITE_DEBUG */
1012
1013
danielk19776622cce2004-05-20 11:00:52 +00001014 if( sqlite3_malloc_failed ){
1015 rc = SQLITE_NOMEM;
1016 sqlite3RollbackAll(db);
1017 sqlite3ResetInternalSchema(db, 0);
1018 db->flags &= ~SQLITE_InTrans;
1019 goto prepare_out;
1020 }
1021 if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK;
drha6ecd332004-06-10 00:29:09 +00001022 if( sParse.checkSchema && !schemaIsValid(db) ){
1023 sParse.rc = SQLITE_SCHEMA;
1024 }
danielk19776622cce2004-05-20 11:00:52 +00001025 if( sParse.rc==SQLITE_SCHEMA ){
1026 sqlite3ResetInternalSchema(db, 0);
1027 }
danielk19776622cce2004-05-20 11:00:52 +00001028 if( pzTail ) *pzTail = sParse.zTail;
danielk19776622cce2004-05-20 11:00:52 +00001029 rc = sParse.rc;
1030
danielk197722322fd2004-05-25 23:35:17 +00001031 if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
1032 sqlite3VdbeSetNumCols(sParse.pVdbe, 5);
danielk19773cf86062004-05-26 10:11:05 +00001033 sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC);
1034 sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC);
1035 sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC);
1036 sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC);
1037 sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC);
danielk197722322fd2004-05-25 23:35:17 +00001038 }
1039
danielk19776622cce2004-05-20 11:00:52 +00001040prepare_out:
danielk197722322fd2004-05-25 23:35:17 +00001041 if( sqlite3SafetyOff(db) ){
1042 rc = SQLITE_MISUSE;
1043 }
danielk19775c4c7782004-06-16 10:39:23 +00001044 if( rc==SQLITE_OK ){
1045 *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
1046 }else if( sParse.pVdbe ){
danielk1977cfe9a692004-06-16 12:00:29 +00001047 sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe);
danielk19775c4c7782004-06-16 10:39:23 +00001048 }
1049
danielk19776622cce2004-05-20 11:00:52 +00001050 if( zErrMsg ){
1051 sqlite3Error(db, rc, "%s", zErrMsg);
danielk1977b20e56b2004-06-15 13:36:30 +00001052 sqliteFree(zErrMsg);
danielk19776622cce2004-05-20 11:00:52 +00001053 }else{
1054 sqlite3Error(db, rc, 0);
1055 }
1056 return rc;
1057}
1058
1059/*
1060** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
1061*/
1062int sqlite3_prepare16(
1063 sqlite3 *db, /* Database handle. */
1064 const void *zSql, /* UTF-8 encoded SQL statement. */
1065 int nBytes, /* Length of zSql in bytes. */
1066 sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
1067 const void **pzTail /* OUT: End of parsed string */
1068){
1069 /* This function currently works by first transforming the UTF-16
1070 ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
1071 ** tricky bit is figuring out the pointer to return in *pzTail.
1072 */
danielk1977bfd6cce2004-06-18 04:24:54 +00001073 char const *zSql8 = 0;
danielk19776622cce2004-05-20 11:00:52 +00001074 char const *zTail8 = 0;
1075 int rc;
danielk1977bfd6cce2004-06-18 04:24:54 +00001076 sqlite3_value *pTmp;
danielk19776622cce2004-05-20 11:00:52 +00001077
danielk1977bfd6cce2004-06-18 04:24:54 +00001078 pTmp = sqlite3GetTransientValue(db);
1079 sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1080 zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
danielk19776622cce2004-05-20 11:00:52 +00001081 if( !zSql8 ){
1082 sqlite3Error(db, SQLITE_NOMEM, 0);
1083 return SQLITE_NOMEM;
1084 }
1085 rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);
1086
1087 if( zTail8 && pzTail ){
1088 /* If sqlite3_prepare returns a tail pointer, we calculate the
1089 ** equivalent pointer into the UTF-16 string by counting the unicode
1090 ** characters between zSql8 and zTail8, and then returning a pointer
1091 ** the same number of characters into the UTF-16 string.
1092 */
1093 int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8);
1094 *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed);
1095 }
1096
1097 return rc;
1098}
1099
danielk19774ad17132004-05-21 01:47:26 +00001100/*
1101** This routine does the work of opening a database on behalf of
1102** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
1103** is UTF-8 encoded. The fourth argument, "def_enc" is one of the TEXT_*
1104** macros from sqliteInt.h. If we end up creating a new database file
1105** (not opening an existing one), the text encoding of the database
1106** will be set to this value.
1107*/
1108static int openDatabase(
1109 const char *zFilename, /* Database filename UTF-8 encoded */
danielk19778e227872004-06-07 07:52:17 +00001110 sqlite3 **ppDb /* OUT: Returned database handle */
danielk19774ad17132004-05-21 01:47:26 +00001111){
1112 sqlite3 *db;
1113 int rc, i;
1114 char *zErrMsg = 0;
1115
1116 /* Allocate the sqlite data structure */
1117 db = sqliteMalloc( sizeof(sqlite) );
1118 if( db==0 ) goto opendb_out;
danielk19774ad17132004-05-21 01:47:26 +00001119 db->priorNewRowid = 0;
1120 db->magic = SQLITE_MAGIC_BUSY;
1121 db->nDb = 2;
1122 db->aDb = db->aDbStatic;
danielk1977dc8453f2004-06-12 00:42:34 +00001123 db->enc = SQLITE_UTF8;
danielk19771d850a72004-05-31 08:26:49 +00001124 db->autoCommit = 1;
danielk19774ad17132004-05-21 01:47:26 +00001125 /* db->flags |= SQLITE_ShortColNames; */
drhf9b596e2004-05-26 16:54:42 +00001126 sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
danielk19774ad17132004-05-21 01:47:26 +00001127 sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
1128 for(i=0; i<db->nDb; i++){
1129 sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
1130 sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
1131 sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
1132 sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
1133 }
danielk19774ad17132004-05-21 01:47:26 +00001134
danielk19770202b292004-06-09 09:55:16 +00001135 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
1136 ** and UTF-16, so add a version for each to avoid any unnecessary
1137 ** conversions. The only error that can occur here is a malloc() failure.
1138 */
danielk1977466be562004-06-10 02:16:01 +00001139 sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binaryCollatingFunc);
1140 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16LE, 0,binaryCollatingFunc);
1141 sqlite3_create_collation(db, "BINARY", SQLITE_UTF16BE, 0,binaryCollatingFunc);
1142 db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0);
danielk19770202b292004-06-09 09:55:16 +00001143 if( !db->pDfltColl ){
1144 rc = db->errCode;
1145 assert( rc!=SQLITE_OK );
1146 db->magic = SQLITE_MAGIC_CLOSED;
1147 goto opendb_out;
1148 }
1149
1150 /* Also add a UTF-8 case-insensitive collation sequence. */
danielk1977466be562004-06-10 02:16:01 +00001151 sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc);
danielk19770202b292004-06-09 09:55:16 +00001152
danielk19774ad17132004-05-21 01:47:26 +00001153 /* Open the backend database driver */
danielk19774ad17132004-05-21 01:47:26 +00001154 rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
1155 if( rc!=SQLITE_OK ){
danielk19774ad17132004-05-21 01:47:26 +00001156 sqlite3Error(db, rc, 0);
1157 db->magic = SQLITE_MAGIC_CLOSED;
1158 goto opendb_out;
1159 }
1160 db->aDb[0].zName = "main";
1161 db->aDb[1].zName = "temp";
1162
danielk197791cf71b2004-06-26 06:37:06 +00001163 /* The default safety_level for the main database is 'full' for the temp
1164 ** database it is 'NONE'. This matches the pager layer defaults. */
1165 db->aDb[0].safety_level = 3;
1166 db->aDb[1].safety_level = 1;
1167
danielk19778e227872004-06-07 07:52:17 +00001168 /* Register all built-in functions, but do not attempt to read the
1169 ** database schema yet. This is delayed until the first time the database
1170 ** is accessed.
1171 */
danielk19774ad17132004-05-21 01:47:26 +00001172 sqlite3RegisterBuiltinFunctions(db);
danielk19778e227872004-06-07 07:52:17 +00001173 if( rc==SQLITE_OK ){
danielk1977bfd6cce2004-06-18 04:24:54 +00001174 sqlite3Error(db, SQLITE_OK, 0);
danielk19774ad17132004-05-21 01:47:26 +00001175 db->magic = SQLITE_MAGIC_OPEN;
danielk19778e227872004-06-07 07:52:17 +00001176 }else{
1177 sqlite3Error(db, rc, "%s", zErrMsg, 0);
1178 if( zErrMsg ) sqliteFree(zErrMsg);
1179 db->magic = SQLITE_MAGIC_CLOSED;
danielk19774ad17132004-05-21 01:47:26 +00001180 }
danielk19774ad17132004-05-21 01:47:26 +00001181
1182opendb_out:
1183 *ppDb = db;
1184 return sqlite3_errcode(db);
1185}
1186
1187/*
1188** Open a new database handle.
1189*/
danielk197780290862004-05-22 09:21:21 +00001190int sqlite3_open(
danielk19774ad17132004-05-21 01:47:26 +00001191 const char *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001192 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001193){
danielk19778e227872004-06-07 07:52:17 +00001194 return openDatabase(zFilename, ppDb);
danielk197783ab5a82004-05-21 11:39:05 +00001195}
1196
danielk19774ad17132004-05-21 01:47:26 +00001197/*
1198** Open a new database handle.
1199*/
1200int sqlite3_open16(
1201 const void *zFilename,
danielk19774f057f92004-06-08 00:02:33 +00001202 sqlite3 **ppDb
danielk19774ad17132004-05-21 01:47:26 +00001203){
danielk1977bfd6cce2004-06-18 04:24:54 +00001204 char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */
1205 int rc = SQLITE_NOMEM;
1206 sqlite3_value *pVal;
danielk19774ad17132004-05-21 01:47:26 +00001207
1208 assert( ppDb );
danielk1977bfd6cce2004-06-18 04:24:54 +00001209 *ppDb = 0;
1210 pVal = sqlite3ValueNew();
1211 sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1212 zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
1213 if( zFilename8 ){
1214 rc = openDatabase(zFilename8, ppDb);
1215 if( rc==SQLITE_OK && *ppDb ){
1216 sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
1217 }
danielk19774ad17132004-05-21 01:47:26 +00001218 }
danielk1977bfd6cce2004-06-18 04:24:54 +00001219 if( pVal ){
1220 sqlite3ValueFree(pVal);
danielk19774ad17132004-05-21 01:47:26 +00001221 }
danielk19778e227872004-06-07 07:52:17 +00001222
danielk19774ad17132004-05-21 01:47:26 +00001223 return rc;
1224}
1225
danielk1977106bb232004-05-21 10:08:53 +00001226/*
1227** The following routine destroys a virtual machine that is created by
1228** the sqlite3_compile() routine. The integer returned is an SQLITE_
1229** success/failure code that describes the result of executing the virtual
1230** machine.
1231**
1232** This routine sets the error code and string returned by
1233** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1234*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001235int sqlite3_finalize(sqlite3_stmt *pStmt){
danielk19779e6db7d2004-06-21 08:18:51 +00001236 return sqlite3VdbeFinalize((Vdbe*)pStmt);
danielk1977106bb232004-05-21 10:08:53 +00001237}
1238
1239/*
1240** Terminate the current execution of an SQL statement and reset it
1241** back to its starting state so that it can be reused. A success code from
1242** the prior execution is returned.
1243**
1244** This routine sets the error code and string returned by
1245** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
1246*/
danielk1977fc57d7b2004-05-26 02:04:57 +00001247int sqlite3_reset(sqlite3_stmt *pStmt){
danielk19779e6db7d2004-06-21 08:18:51 +00001248 int rc = sqlite3VdbeReset((Vdbe*)pStmt);
danielk1977106bb232004-05-21 10:08:53 +00001249 sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0);
1250 return rc;
1251}
danielk19770202b292004-06-09 09:55:16 +00001252
danielk1977d8123362004-06-12 09:25:12 +00001253/*
1254** Register a new collation sequence with the database handle db.
1255*/
danielk19770202b292004-06-09 09:55:16 +00001256int sqlite3_create_collation(
1257 sqlite3* db,
1258 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001259 int enc,
danielk19770202b292004-06-09 09:55:16 +00001260 void* pCtx,
1261 int(*xCompare)(void*,int,const void*,int,const void*)
1262){
1263 CollSeq *pColl;
1264 int rc = SQLITE_OK;
danielk1977d8123362004-06-12 09:25:12 +00001265
1266 /* If SQLITE_UTF16 is specified as the encoding type, transform this
1267 ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
1268 ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
1269 */
1270 if( enc==SQLITE_UTF16 ){
1271 enc = SQLITE_UTF16NATIVE;
1272 }
1273
danielk1977466be562004-06-10 02:16:01 +00001274 if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){
1275 sqlite3Error(db, SQLITE_ERROR,
1276 "Param 3 to sqlite3_create_collation() must be one of "
danielk1977d8123362004-06-12 09:25:12 +00001277 "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE"
danielk1977466be562004-06-10 02:16:01 +00001278 );
1279 return SQLITE_ERROR;
1280 }
1281 pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1);
danielk19770202b292004-06-09 09:55:16 +00001282 if( 0==pColl ){
1283 rc = SQLITE_NOMEM;
danielk19770202b292004-06-09 09:55:16 +00001284 }else{
1285 pColl->xCmp = xCompare;
1286 pColl->pUser = pCtx;
danielk1977312d6b32004-06-29 13:18:23 +00001287 pColl->enc = enc;
danielk19770202b292004-06-09 09:55:16 +00001288 }
1289 sqlite3Error(db, rc, 0);
danielk1977466be562004-06-10 02:16:01 +00001290 return rc;
danielk19770202b292004-06-09 09:55:16 +00001291}
1292
danielk1977d8123362004-06-12 09:25:12 +00001293/*
1294** Register a new collation sequence with the database handle db.
1295*/
danielk19770202b292004-06-09 09:55:16 +00001296int sqlite3_create_collation16(
1297 sqlite3* db,
1298 const char *zName,
danielk1977466be562004-06-10 02:16:01 +00001299 int enc,
danielk19770202b292004-06-09 09:55:16 +00001300 void* pCtx,
1301 int(*xCompare)(void*,int,const void*,int,const void*)
1302){
danielk1977bfd6cce2004-06-18 04:24:54 +00001303 char const *zName8;
1304 sqlite3_value *pTmp = sqlite3GetTransientValue(db);
1305 sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC);
1306 zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8);
1307 return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare);
danielk19770202b292004-06-09 09:55:16 +00001308}
danielk19777cedc8d2004-06-10 10:50:08 +00001309
danielk1977d8123362004-06-12 09:25:12 +00001310/*
1311** Register a collation sequence factory callback with the database handle
1312** db. Replace any previously installed collation sequence factory.
1313*/
danielk19777cedc8d2004-06-10 10:50:08 +00001314int sqlite3_collation_needed(
1315 sqlite3 *db,
1316 void *pCollNeededArg,
1317 void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
1318){
1319 db->xCollNeeded = xCollNeeded;
1320 db->xCollNeeded16 = 0;
1321 db->pCollNeededArg = pCollNeededArg;
1322 return SQLITE_OK;
1323}
danielk1977d8123362004-06-12 09:25:12 +00001324
1325/*
1326** Register a collation sequence factory callback with the database handle
1327** db. Replace any previously installed collation sequence factory.
1328*/
danielk19777cedc8d2004-06-10 10:50:08 +00001329int sqlite3_collation_needed16(
1330 sqlite3 *db,
1331 void *pCollNeededArg,
1332 void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
1333){
1334 db->xCollNeeded = 0;
1335 db->xCollNeeded16 = xCollNeeded16;
1336 db->pCollNeededArg = pCollNeededArg;
1337 return SQLITE_OK;
1338}