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 | ************************************************************************* |
| 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 | ** |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame^] | 17 | ** $Id: main.c,v 1.208 2004/06/09 09:55:18 danielk1977 Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 18 | */ |
| 19 | #include "sqliteInt.h" |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 20 | #include "os.h" |
drh | ce9079c | 2002-05-15 14:17:44 +0000 | [diff] [blame] | 21 | #include <ctype.h> |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 22 | |
| 23 | /* |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 24 | ** A pointer to this structure is used to communicate information |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 25 | ** from sqlite3Init into the sqlite3InitCallback. |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 26 | */ |
| 27 | typedef struct { |
| 28 | sqlite *db; /* The database being initialized */ |
| 29 | char **pzErrMsg; /* Error message stored here */ |
| 30 | } InitData; |
| 31 | |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 32 | /* |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 33 | ** The following constant value is used by the SQLITE_BIGENDIAN and |
| 34 | ** SQLITE_LITTLEENDIAN macros. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 35 | */ |
| 36 | const int sqlite3one = 1; |
| 37 | |
| 38 | /* |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 39 | ** Fill the InitData structure with an error message that indicates |
| 40 | ** that the database is corrupt. |
| 41 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 42 | static void corruptSchema(InitData *pData, const char *zExtra){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 43 | sqlite3SetString(pData->pzErrMsg, "malformed database schema", |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 44 | zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 45 | } |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 46 | |
| 47 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 48 | ** This is the callback routine for the code that initializes the |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 49 | ** database. See sqlite3Init() below for additional information. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 50 | ** |
| 51 | ** Each callback contains the following information: |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 52 | ** |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 53 | ** argv[0] = "file-format" or "schema-cookie" or "table" or "index" |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 54 | ** argv[1] = table or index name or meta statement type. |
| 55 | ** argv[2] = root page number for table or index. NULL for meta. |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 56 | ** argv[3] = SQL text for a CREATE TABLE or CREATE INDEX statement. |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 57 | ** argv[4] = "1" for temporary files, "0" for main database, "2" or more |
| 58 | ** for auxiliary database files. |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 59 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 60 | */ |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 61 | static |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 62 | int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){ |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 63 | InitData *pData = (InitData*)pInit; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 64 | int nErr = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 65 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 66 | assert( argc==5 ); |
drh | 98e3e60 | 2003-07-27 17:26:22 +0000 | [diff] [blame] | 67 | if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 68 | if( argv[0]==0 ){ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 69 | corruptSchema(pData, 0); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 70 | return 1; |
| 71 | } |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 72 | switch( argv[0][0] ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 73 | case 'v': |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 74 | case 'i': |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 75 | case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 76 | sqlite *db = pData->db; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 77 | if( argv[2]==0 || argv[4]==0 ){ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 78 | corruptSchema(pData, 0); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 79 | return 1; |
| 80 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 81 | if( argv[3] && argv[3][0] ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 82 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 83 | ** But because db->init.busy is set to 1, no VDBE code is generated |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 84 | ** or executed. All the parser does is build the internal data |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 85 | ** structures that describe the table, index, or view. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 86 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 87 | char *zErr; |
| 88 | assert( db->init.busy ); |
| 89 | db->init.iDb = atoi(argv[4]); |
| 90 | assert( db->init.iDb>=0 && db->init.iDb<db->nDb ); |
| 91 | db->init.newTnum = atoi(argv[2]); |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 92 | if( sqlite3_exec(db, argv[3], 0, 0, &zErr) ){ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 93 | corruptSchema(pData, zErr); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 94 | sqlite3_free(zErr); |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 95 | } |
| 96 | db->init.iDb = 0; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 97 | }else{ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 98 | /* If the SQL column is blank it means this is an index that |
| 99 | ** was created to be the PRIMARY KEY or to fulfill a UNIQUE |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 100 | ** constraint for a CREATE TABLE. The index should have already |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 101 | ** been created when we processed the CREATE TABLE. All we have |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 102 | ** to do here is record the root page number for that index. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 103 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 104 | int iDb; |
| 105 | Index *pIndex; |
| 106 | |
| 107 | iDb = atoi(argv[4]); |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 108 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 109 | pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zName); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 110 | if( pIndex==0 || pIndex->tnum!=0 ){ |
drh | da9e034 | 2002-01-10 14:31:48 +0000 | [diff] [blame] | 111 | /* This can occur if there exists an index on a TEMP table which |
| 112 | ** has the same name as another index on a permanent index. Since |
| 113 | ** the permanent table is hidden by the TEMP table, we can also |
| 114 | ** safely ignore the index on the permanent table. |
| 115 | */ |
| 116 | /* Do Nothing */; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 117 | }else{ |
| 118 | pIndex->tnum = atoi(argv[2]); |
| 119 | } |
| 120 | } |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | default: { |
| 124 | /* This can not happen! */ |
| 125 | nErr = 1; |
| 126 | assert( nErr==0 ); |
| 127 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 128 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 129 | return nErr; |
| 130 | } |
| 131 | |
| 132 | /* |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 133 | ** Attempt to read the database schema and initialize internal |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 134 | ** data structures for a single database file. The index of the |
| 135 | ** database file is given by iDb. iDb==0 is used for the main |
| 136 | ** database. iDb==1 should never be used. iDb>=2 is used for |
| 137 | ** auxiliary databases. Return one of the SQLITE_ error codes to |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 138 | ** indicate success or failure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 139 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 140 | static int sqlite3InitOne(sqlite *db, int iDb, char **pzErrMsg){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 141 | int rc; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 142 | BtCursor *curMain; |
| 143 | int size; |
| 144 | Table *pTab; |
| 145 | char *azArg[6]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 146 | char zDbNum[30]; |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 147 | int meta[10]; |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 148 | InitData initData; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | ** The master database table has a structure like this |
| 152 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 153 | static char master_schema[] = |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 154 | "CREATE TABLE sqlite_master(\n" |
| 155 | " type text,\n" |
| 156 | " name text,\n" |
| 157 | " tbl_name text,\n" |
| 158 | " rootpage integer,\n" |
| 159 | " sql text\n" |
| 160 | ")" |
| 161 | ; |
| 162 | static char temp_master_schema[] = |
| 163 | "CREATE TEMP TABLE sqlite_temp_master(\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 164 | " type text,\n" |
| 165 | " name text,\n" |
| 166 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 167 | " rootpage integer,\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 168 | " sql text\n" |
| 169 | ")" |
| 170 | ; |
| 171 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 172 | /* The following SQL will read the schema from the master tables. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 173 | */ |
danielk1977 | bf57cfe | 2004-05-11 09:50:02 +0000 | [diff] [blame] | 174 | static char init_script1[] = |
| 175 | "SELECT type, name, rootpage, sql, 1 FROM sqlite_temp_master"; |
| 176 | static char init_script2[] = |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 177 | "SELECT type, name, rootpage, sql, 0 FROM sqlite_master"; |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 178 | |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 179 | assert( iDb>=0 && iDb!=1 && iDb<db->nDb ); |
| 180 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 181 | /* Construct the schema tables: sqlite_master and sqlite_temp_master |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 182 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 183 | sqlite3SafetyOff(db); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 184 | azArg[0] = "table"; |
| 185 | azArg[1] = MASTER_NAME; |
danielk1977 | 8e15081 | 2004-05-10 01:17:37 +0000 | [diff] [blame] | 186 | azArg[2] = "1"; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 187 | azArg[3] = master_schema; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 188 | sprintf(zDbNum, "%d", iDb); |
| 189 | azArg[4] = zDbNum; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 190 | azArg[5] = 0; |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 191 | initData.db = db; |
| 192 | initData.pzErrMsg = pzErrMsg; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 193 | sqlite3InitCallback(&initData, 5, azArg, 0); |
| 194 | pTab = sqlite3FindTable(db, MASTER_NAME, "main"); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 195 | if( pTab ){ |
| 196 | pTab->readOnly = 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 197 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 198 | if( iDb==0 ){ |
| 199 | azArg[1] = TEMP_MASTER_NAME; |
| 200 | azArg[3] = temp_master_schema; |
| 201 | azArg[4] = "1"; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 202 | sqlite3InitCallback(&initData, 5, azArg, 0); |
| 203 | pTab = sqlite3FindTable(db, TEMP_MASTER_NAME, "temp"); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 204 | if( pTab ){ |
| 205 | pTab->readOnly = 1; |
| 206 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 207 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 208 | sqlite3SafetyOn(db); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 209 | |
| 210 | /* Create a cursor to hold the database open |
| 211 | */ |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 212 | if( db->aDb[iDb].pBt==0 ) return SQLITE_OK; |
danielk1977 | 8e15081 | 2004-05-10 01:17:37 +0000 | [diff] [blame] | 213 | rc = sqlite3BtreeCursor(db->aDb[iDb].pBt, MASTER_ROOT, 0, 0, 0, &curMain); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 214 | if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 215 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
drh | 92ed08a | 2002-07-30 18:43:40 +0000 | [diff] [blame] | 216 | return rc; |
| 217 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 218 | |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 219 | /* Get the database meta information. |
| 220 | ** |
| 221 | ** Meta values are as follows: |
| 222 | ** meta[0] Schema cookie. Changes with each schema change. |
| 223 | ** meta[1] File format of schema layer. |
| 224 | ** meta[2] Size of the page cache. |
| 225 | ** meta[3] Synchronous setting. 1:off, 2:normal, 3:full |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 226 | ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16 LE 3:UTF-16 BE |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 227 | ** meta[5] Pragma temp_store value. See comments on BtreeFactory |
| 228 | ** meta[6] |
| 229 | ** meta[7] |
| 230 | ** meta[8] |
| 231 | ** meta[9] |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 232 | ** |
| 233 | ** Note: The hash defined TEXT_Utf* symbols in sqliteInt.h correspond to |
| 234 | ** the possible values of meta[4]. |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 235 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 236 | if( rc==SQLITE_OK ){ |
| 237 | int i; |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 238 | for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){ |
| 239 | rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, i+1, &meta[i]); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 240 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 241 | if( rc ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 242 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 243 | sqlite3BtreeCloseCursor(curMain); |
| 244 | return rc; |
| 245 | } |
| 246 | }else{ |
| 247 | memset(meta, 0, sizeof(meta)); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 248 | } |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 249 | db->aDb[iDb].schema_cookie = meta[0]; |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 250 | |
| 251 | /* If opening a non-empty database, check the text encoding. For the |
| 252 | ** main database, set sqlite3.enc to the encoding of the main database. |
| 253 | ** For an attached db, it is an error if the encoding is not the same |
| 254 | ** as sqlite3.enc. |
| 255 | */ |
| 256 | if( meta[4] ){ /* text encoding */ |
| 257 | if( iDb==0 ){ |
| 258 | /* If opening the main database, set db->enc. */ |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 259 | db->enc = (u8)meta[4]; |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 260 | }else{ |
| 261 | /* If opening an attached database, the encoding much match db->enc */ |
| 262 | if( meta[4]!=db->enc ){ |
| 263 | sqlite3BtreeCloseCursor(curMain); |
| 264 | sqlite3SetString(pzErrMsg, "attached databases must use the same" |
| 265 | " text encoding as main database", (char*)0); |
| 266 | return SQLITE_ERROR; |
| 267 | } |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 268 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if( iDb==0 ){ |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 272 | size = meta[2]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 273 | if( size==0 ){ size = MAX_PAGES; } |
| 274 | db->cache_size = size; |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 275 | db->safety_level = meta[3]; |
| 276 | if( meta[5]>0 && meta[5]<=2 && db->temp_store==0 ){ |
| 277 | db->temp_store = meta[5]; |
drh | 1bdd9b5 | 2004-04-23 17:04:44 +0000 | [diff] [blame] | 278 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 279 | if( db->safety_level==0 ) db->safety_level = 2; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 280 | |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 281 | /* FIX ME: Every struct Db will need a next_cookie */ |
| 282 | db->next_cookie = meta[0]; |
| 283 | db->file_format = meta[1]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 284 | if( db->file_format==0 ){ |
| 285 | /* This happens if the database was initially empty */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 286 | db->file_format = 1; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 287 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 288 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | ** file_format==1 Version 3.0.0. |
| 292 | */ |
| 293 | if( meta[1]>1 ){ |
| 294 | sqlite3BtreeCloseCursor(curMain); |
| 295 | sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0); |
| 296 | return SQLITE_ERROR; |
| 297 | } |
| 298 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 299 | sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->cache_size); |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 300 | sqlite3BtreeSetSafetyLevel(db->aDb[iDb].pBt, meta[3]==0 ? 2 : meta[3]); |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 301 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 302 | /* Read the schema information out of the schema tables |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 303 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 304 | assert( db->init.busy ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 305 | if( rc==SQLITE_EMPTY ){ |
| 306 | /* For an empty database, there is nothing to read */ |
| 307 | rc = SQLITE_OK; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 308 | }else{ |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 309 | sqlite3SafetyOff(db); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 310 | if( iDb==0 ){ |
danielk1977 | bf57cfe | 2004-05-11 09:50:02 +0000 | [diff] [blame] | 311 | /* This SQL statement tries to read the temp.* schema from the |
danielk1977 | 8d05984 | 2004-05-12 11:24:02 +0000 | [diff] [blame] | 312 | ** sqlite_temp_master table. It might return SQLITE_EMPTY. |
danielk1977 | bf57cfe | 2004-05-11 09:50:02 +0000 | [diff] [blame] | 313 | */ |
danielk1977 | bf57cfe | 2004-05-11 09:50:02 +0000 | [diff] [blame] | 314 | rc = sqlite3_exec(db, init_script1, sqlite3InitCallback, &initData, 0); |
danielk1977 | bf57cfe | 2004-05-11 09:50:02 +0000 | [diff] [blame] | 315 | if( rc==SQLITE_OK || rc==SQLITE_EMPTY ){ |
| 316 | rc = sqlite3_exec(db, init_script2, sqlite3InitCallback, &initData, 0); |
| 317 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 318 | }else{ |
| 319 | char *zSql = 0; |
| 320 | sqlite3SetString(&zSql, |
| 321 | "SELECT type, name, rootpage, sql, ", zDbNum, " FROM \"", |
| 322 | db->aDb[iDb].zName, "\".sqlite_master", (char*)0); |
| 323 | rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); |
| 324 | sqliteFree(zSql); |
| 325 | } |
| 326 | sqlite3SafetyOn(db); |
| 327 | sqlite3BtreeCloseCursor(curMain); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 328 | } |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 329 | if( sqlite3_malloc_failed ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 330 | sqlite3SetString(pzErrMsg, "out of memory", (char*)0); |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 331 | rc = SQLITE_NOMEM; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 332 | sqlite3ResetInternalSchema(db, 0); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 333 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 334 | if( rc==SQLITE_OK ){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 335 | DbSetProperty(db, iDb, DB_SchemaLoaded); |
| 336 | if( iDb==0 ){ |
| 337 | DbSetProperty(db, 1, DB_SchemaLoaded); |
| 338 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 339 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 340 | sqlite3ResetInternalSchema(db, iDb); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 341 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 342 | return rc; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /* |
| 346 | ** Initialize all database files - the main database file, the file |
| 347 | ** used to store temporary tables, and any additional database files |
| 348 | ** created using ATTACH statements. Return a success code. If an |
| 349 | ** error occurs, write an error message into *pzErrMsg. |
| 350 | ** |
| 351 | ** After the database is initialized, the SQLITE_Initialized |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 352 | ** bit is set in the flags field of the sqlite structure. |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 353 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 354 | int sqlite3Init(sqlite *db, char **pzErrMsg){ |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 355 | int i, rc; |
| 356 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 357 | if( db->init.busy ) return SQLITE_OK; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 358 | assert( (db->flags & SQLITE_Initialized)==0 ); |
| 359 | rc = SQLITE_OK; |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 360 | db->init.busy = 1; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 361 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 362 | if( DbHasProperty(db, i, DB_SchemaLoaded) ) continue; |
| 363 | assert( i!=1 ); /* Should have been initialized together with 0 */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 364 | rc = sqlite3InitOne(db, i, pzErrMsg); |
drh | 8ef83ff | 2004-02-12 15:31:21 +0000 | [diff] [blame] | 365 | if( rc ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 366 | sqlite3ResetInternalSchema(db, i); |
drh | 8ef83ff | 2004-02-12 15:31:21 +0000 | [diff] [blame] | 367 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 368 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 369 | db->init.busy = 0; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 370 | if( rc==SQLITE_OK ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 371 | db->flags |= SQLITE_Initialized; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 372 | sqlite3CommitInternalChanges(db); |
drh | 2d71ca9 | 2004-02-10 02:27:04 +0000 | [diff] [blame] | 373 | } |
| 374 | |
drh | 2d71ca9 | 2004-02-10 02:27:04 +0000 | [diff] [blame] | 375 | if( rc!=SQLITE_OK ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 376 | db->flags &= ~SQLITE_Initialized; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 377 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 378 | return rc; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 382 | ** This routine is a no-op if the database schema is already initialised. |
| 383 | ** Otherwise, the schema is loaded. An error code is returned. |
| 384 | */ |
| 385 | int sqlite3ReadSchema(sqlite *db){ |
| 386 | int rc = SQLITE_OK; |
| 387 | char *zErrMsg = 0; |
| 388 | |
| 389 | if( !db->init.busy ){ |
| 390 | if( (db->flags & SQLITE_Initialized)==0 ){ |
| 391 | rc = sqlite3Init(db, &zErrMsg); |
| 392 | } |
| 393 | } |
| 394 | assert( (db->flags & SQLITE_Initialized)!=0 || db->init.busy ); |
| 395 | |
| 396 | sqlite3Error(db, rc, zErrMsg); |
| 397 | if( zErrMsg ){ |
| 398 | sqliteFree(zErrMsg); |
| 399 | } |
| 400 | return rc; |
| 401 | } |
| 402 | |
| 403 | /* |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 404 | ** The version of the library |
| 405 | */ |
drh | 096c497 | 2002-07-19 19:03:41 +0000 | [diff] [blame] | 406 | const char rcsid[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $"; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 407 | const char sqlite3_version[] = SQLITE_VERSION; |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 408 | |
| 409 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 410 | ** This is the default collating function named "BINARY" which is always |
| 411 | ** available. |
| 412 | */ |
| 413 | static int binaryCollatingFunc( |
| 414 | void *NotUsed, |
| 415 | int nKey1, const void *pKey1, |
| 416 | int nKey2, const void *pKey2 |
| 417 | ){ |
| 418 | int rc, n; |
| 419 | n = nKey1<nKey2 ? nKey1 : nKey2; |
| 420 | rc = memcmp(pKey1, pKey2, n); |
| 421 | if( rc==0 ){ |
| 422 | rc = nKey1 - nKey2; |
| 423 | } |
| 424 | return rc; |
| 425 | } |
| 426 | |
| 427 | /* |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame^] | 428 | ** Another built-in collating sequence: NOCASE. At the moment there is |
| 429 | ** only a UTF-8 implementation. |
| 430 | */ |
| 431 | static int nocaseCollatingFunc( |
| 432 | void *NotUsed, |
| 433 | int nKey1, const void *pKey1, |
| 434 | int nKey2, const void *pKey2 |
| 435 | ){ |
| 436 | int r = sqlite3StrNICmp( |
| 437 | (const char *)pKey1, (const char *)pKey2, (nKey1>nKey2)?nKey1:nKey2); |
| 438 | if( 0==r ){ |
| 439 | r = nKey1-nKey2; |
| 440 | } |
| 441 | return r; |
| 442 | } |
| 443 | |
| 444 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 445 | ** Return the ROWID of the most recent insert |
| 446 | */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 447 | long long int sqlite3_last_insert_rowid(sqlite *db){ |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 448 | return db->lastRowid; |
| 449 | } |
| 450 | |
| 451 | /* |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 452 | ** Return the number of changes in the most recent call to sqlite3_exec(). |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 453 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 454 | int sqlite3_changes(sqlite *db){ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 455 | return db->nChange; |
| 456 | } |
| 457 | |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 458 | /* |
| 459 | ** Return the number of changes produced by the last INSERT, UPDATE, or |
| 460 | ** DELETE statement to complete execution. The count does not include |
| 461 | ** changes due to SQL statements executed in trigger programs that were |
| 462 | ** triggered by that statement |
| 463 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 464 | int sqlite3_last_statement_changes(sqlite *db){ |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 465 | return db->lsChange; |
| 466 | } |
| 467 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 468 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 469 | ** Close an existing SQLite database |
| 470 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 471 | void sqlite3_close(sqlite *db){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 472 | HashElem *i; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 473 | int j; |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 474 | db->want_to_close = 1; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 475 | if( sqlite3SafetyCheck(db) || sqlite3SafetyOn(db) ){ |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 476 | /* printf("DID NOT CLOSE\n"); fflush(stdout); */ |
| 477 | return; |
| 478 | } |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 479 | db->magic = SQLITE_MAGIC_CLOSED; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 480 | for(j=0; j<db->nDb; j++){ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 481 | struct Db *pDb = &db->aDb[j]; |
| 482 | if( pDb->pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 483 | sqlite3BtreeClose(pDb->pBt); |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 484 | pDb->pBt = 0; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 485 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 486 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 487 | sqlite3ResetInternalSchema(db, 0); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 488 | assert( db->nDb<=2 ); |
| 489 | assert( db->aDb==db->aDbStatic ); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 490 | for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ |
| 491 | FuncDef *pFunc, *pNext; |
| 492 | for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 493 | pNext = pFunc->pNext; |
| 494 | sqliteFree(pFunc); |
| 495 | } |
| 496 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 497 | sqlite3HashClear(&db->aFunc); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 498 | sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 499 | sqliteFree(db); |
| 500 | } |
| 501 | |
| 502 | /* |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 503 | ** Rollback all database files. |
| 504 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 505 | void sqlite3RollbackAll(sqlite *db){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 506 | int i; |
| 507 | for(i=0; i<db->nDb; i++){ |
| 508 | if( db->aDb[i].pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 509 | sqlite3BtreeRollback(db->aDb[i].pBt); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 510 | db->aDb[i].inTrans = 0; |
| 511 | } |
| 512 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 513 | sqlite3ResetInternalSchema(db, 0); |
| 514 | /* sqlite3RollbackInternalChanges(db); */ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 518 | ** Return a static string that describes the kind of error specified in the |
| 519 | ** argument. |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 520 | */ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 521 | const char *sqlite3ErrStr(int rc){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 522 | const char *z; |
| 523 | switch( rc ){ |
| 524 | case SQLITE_OK: z = "not an error"; break; |
| 525 | case SQLITE_ERROR: z = "SQL logic error or missing database"; break; |
| 526 | case SQLITE_INTERNAL: z = "internal SQLite implementation flaw"; break; |
| 527 | case SQLITE_PERM: z = "access permission denied"; break; |
| 528 | case SQLITE_ABORT: z = "callback requested query abort"; break; |
| 529 | case SQLITE_BUSY: z = "database is locked"; break; |
| 530 | case SQLITE_LOCKED: z = "database table is locked"; break; |
| 531 | case SQLITE_NOMEM: z = "out of memory"; break; |
| 532 | case SQLITE_READONLY: z = "attempt to write a readonly database"; break; |
| 533 | case SQLITE_INTERRUPT: z = "interrupted"; break; |
| 534 | case SQLITE_IOERR: z = "disk I/O error"; break; |
| 535 | case SQLITE_CORRUPT: z = "database disk image is malformed"; break; |
| 536 | case SQLITE_NOTFOUND: z = "table or record not found"; break; |
| 537 | case SQLITE_FULL: z = "database is full"; break; |
| 538 | case SQLITE_CANTOPEN: z = "unable to open database file"; break; |
| 539 | case SQLITE_PROTOCOL: z = "database locking protocol failure"; break; |
| 540 | case SQLITE_EMPTY: z = "table contains no data"; break; |
| 541 | case SQLITE_SCHEMA: z = "database schema has changed"; break; |
| 542 | case SQLITE_TOOBIG: z = "too much data for one table row"; break; |
| 543 | case SQLITE_CONSTRAINT: z = "constraint failed"; break; |
| 544 | case SQLITE_MISMATCH: z = "datatype mismatch"; break; |
| 545 | case SQLITE_MISUSE: z = "library routine called out of sequence";break; |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 546 | case SQLITE_NOLFS: z = "kernel lacks large file support"; break; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 547 | case SQLITE_AUTH: z = "authorization denied"; break; |
jplyon | 892f671 | 2003-06-12 08:59:00 +0000 | [diff] [blame] | 548 | case SQLITE_FORMAT: z = "auxiliary database format error"; break; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 549 | case SQLITE_RANGE: z = "bind index out of range"; break; |
drh | c602f9a | 2004-02-12 19:01:04 +0000 | [diff] [blame] | 550 | case SQLITE_NOTADB: z = "file is encrypted or is not a database";break; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 551 | default: z = "unknown error"; break; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 552 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 553 | return z; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /* |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 557 | ** This routine implements a busy callback that sleeps and tries |
| 558 | ** again until a timeout value is reached. The timeout value is |
| 559 | ** an integer number of milliseconds passed in as the first |
| 560 | ** argument. |
| 561 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 562 | static int sqliteDefaultBusyCallback( |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 563 | void *Timeout, /* Maximum amount of time to wait */ |
| 564 | const char *NotUsed, /* The name of the table that is busy */ |
| 565 | int count /* Number of times table has been busy */ |
| 566 | ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 567 | #if SQLITE_MIN_SLEEP_MS==1 |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 568 | static const char delays[] = |
| 569 | { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 50, 100}; |
| 570 | static const short int totals[] = |
| 571 | { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228, 287}; |
| 572 | # define NDELAY (sizeof(delays)/sizeof(delays[0])) |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 573 | int timeout = (int)Timeout; |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 574 | int delay, prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 575 | |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 576 | if( count <= NDELAY ){ |
| 577 | delay = delays[count-1]; |
| 578 | prior = totals[count-1]; |
| 579 | }else{ |
| 580 | delay = delays[NDELAY-1]; |
| 581 | prior = totals[NDELAY-1] + delay*(count-NDELAY-1); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 582 | } |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 583 | if( prior + delay > timeout ){ |
| 584 | delay = timeout - prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 585 | if( delay<=0 ) return 0; |
| 586 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 587 | sqlite3OsSleep(delay); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 588 | return 1; |
| 589 | #else |
| 590 | int timeout = (int)Timeout; |
| 591 | if( (count+1)*1000 > timeout ){ |
| 592 | return 0; |
| 593 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 594 | sqlite3OsSleep(1000); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 595 | return 1; |
| 596 | #endif |
| 597 | } |
| 598 | |
| 599 | /* |
| 600 | ** This routine sets the busy callback for an Sqlite database to the |
| 601 | ** given callback function with the given argument. |
| 602 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 603 | void sqlite3_busy_handler( |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 604 | sqlite *db, |
| 605 | int (*xBusy)(void*,const char*,int), |
| 606 | void *pArg |
| 607 | ){ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 608 | db->busyHandler.xFunc = xBusy; |
| 609 | db->busyHandler.pArg = pArg; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 610 | } |
| 611 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 612 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 613 | /* |
| 614 | ** This routine sets the progress callback for an Sqlite database to the |
| 615 | ** given callback function with the given argument. The progress callback will |
| 616 | ** be invoked every nOps opcodes. |
| 617 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 618 | void sqlite3_progress_handler( |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 619 | sqlite *db, |
| 620 | int nOps, |
| 621 | int (*xProgress)(void*), |
| 622 | void *pArg |
| 623 | ){ |
| 624 | if( nOps>0 ){ |
| 625 | db->xProgress = xProgress; |
| 626 | db->nProgressOps = nOps; |
| 627 | db->pProgressArg = pArg; |
| 628 | }else{ |
| 629 | db->xProgress = 0; |
| 630 | db->nProgressOps = 0; |
| 631 | db->pProgressArg = 0; |
| 632 | } |
| 633 | } |
| 634 | #endif |
| 635 | |
| 636 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 637 | /* |
| 638 | ** This routine installs a default busy handler that waits for the |
| 639 | ** specified number of milliseconds before returning 0. |
| 640 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 641 | void sqlite3_busy_timeout(sqlite *db, int ms){ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 642 | if( ms>0 ){ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 643 | sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 644 | }else{ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 645 | sqlite3_busy_handler(db, 0, 0); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 648 | |
| 649 | /* |
| 650 | ** Cause any pending operation to stop at its earliest opportunity. |
| 651 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 652 | void sqlite3_interrupt(sqlite *db){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 653 | db->flags |= SQLITE_Interrupt; |
| 654 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | ** Windows systems should call this routine to free memory that |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 658 | ** is returned in the in the errmsg parameter of sqlite3_open() when |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 659 | ** SQLite is a DLL. For some reason, it does not work to call free() |
| 660 | ** directly. |
| 661 | ** |
| 662 | ** Note that we need to call free() not sqliteFree() here, since every |
| 663 | ** string that is exported from SQLite should have already passed through |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 664 | ** sqlite3StrRealloc(). |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 665 | */ |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 666 | void sqlite3_free(char *p){ free(p); } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 667 | |
| 668 | /* |
drh | df01489 | 2004-06-02 00:41:09 +0000 | [diff] [blame] | 669 | ** Create new user functions. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 670 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 671 | int sqlite3_create_function( |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 672 | sqlite3 *db, |
| 673 | const char *zFunctionName, |
| 674 | int nArg, |
| 675 | int eTextRep, |
| 676 | int iCollateArg, |
| 677 | void *pUserData, |
| 678 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 679 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 680 | void (*xFinal)(sqlite3_context*) |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 681 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 682 | FuncDef *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 683 | int nName; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 684 | |
danielk1977 | 398eae7 | 2004-05-26 06:58:43 +0000 | [diff] [blame] | 685 | if( (db==0 || zFunctionName==0 || sqlite3SafetyCheck(db)) || |
| 686 | (xFunc && (xFinal || xStep)) || |
| 687 | (!xFunc && (xFinal && !xStep)) || |
| 688 | (!xFunc && (!xFinal && xStep)) || |
| 689 | (nArg<-1 || nArg>127) || |
| 690 | (255<(nName = strlen(zFunctionName))) ){ |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 691 | return SQLITE_ERROR; |
| 692 | } |
| 693 | |
danielk1977 | d02eb1f | 2004-06-06 09:44:03 +0000 | [diff] [blame] | 694 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, eTextRep, 1); |
drh | 4e0f995 | 2002-02-27 01:53:13 +0000 | [diff] [blame] | 695 | if( p==0 ) return 1; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 696 | p->xFunc = xFunc; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 697 | p->xStep = xStep; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 698 | p->xFinalize = xFinal; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 699 | p->pUserData = pUserData; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 700 | return SQLITE_OK; |
| 701 | } |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 702 | int sqlite3_create_function16( |
| 703 | sqlite3 *db, |
| 704 | const void *zFunctionName, |
| 705 | int nArg, |
| 706 | int eTextRep, |
| 707 | int iCollateArg, |
| 708 | void *pUserData, |
| 709 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), |
| 710 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), |
| 711 | void (*xFinal)(sqlite3_context*) |
| 712 | ){ |
| 713 | int rc; |
| 714 | char *zFunctionName8; |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 715 | zFunctionName8 = sqlite3utf16to8(zFunctionName, -1, SQLITE_BIGENDIAN); |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 716 | if( !zFunctionName8 ){ |
| 717 | return SQLITE_NOMEM; |
| 718 | } |
| 719 | rc = sqlite3_create_function(db, zFunctionName8, nArg, eTextRep, |
| 720 | iCollateArg, pUserData, xFunc, xStep, xFinal); |
| 721 | sqliteFree(zFunctionName8); |
| 722 | return rc; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 723 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 724 | |
| 725 | /* |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 726 | ** Register a trace function. The pArg from the previously registered trace |
| 727 | ** is returned. |
| 728 | ** |
| 729 | ** A NULL trace function means that no tracing is executes. A non-NULL |
| 730 | ** trace is a pointer to a function that is invoked at the start of each |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 731 | ** sqlite3_exec(). |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 732 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 733 | void *sqlite3_trace(sqlite *db, void (*xTrace)(void*,const char*), void *pArg){ |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 734 | void *pOld = db->pTraceArg; |
| 735 | db->xTrace = xTrace; |
| 736 | db->pTraceArg = pArg; |
| 737 | return pOld; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 738 | } |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 739 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 740 | /*** EXPERIMENTAL *** |
| 741 | ** |
| 742 | ** Register a function to be invoked when a transaction comments. |
| 743 | ** If either function returns non-zero, then the commit becomes a |
| 744 | ** rollback. |
| 745 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 746 | void *sqlite3_commit_hook( |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 747 | sqlite *db, /* Attach the hook to this database */ |
| 748 | int (*xCallback)(void*), /* Function to invoke on each commit */ |
| 749 | void *pArg /* Argument to the function */ |
| 750 | ){ |
| 751 | void *pOld = db->pCommitArg; |
| 752 | db->xCommitCallback = xCallback; |
| 753 | db->pCommitArg = pArg; |
| 754 | return pOld; |
| 755 | } |
| 756 | |
| 757 | |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 758 | /* |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 759 | ** This routine is called to create a connection to a database BTree |
| 760 | ** driver. If zFilename is the name of a file, then that file is |
| 761 | ** opened and used. If zFilename is the magic name ":memory:" then |
| 762 | ** the database is stored in memory (and is thus forgotten as soon as |
| 763 | ** the connection is closed.) If zFilename is NULL then the database |
| 764 | ** is for temporary use only and is deleted as soon as the connection |
| 765 | ** is closed. |
| 766 | ** |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 767 | ** A temporary database can be either a disk file (that is automatically |
| 768 | ** deleted when the file is closed) or a set of red-black trees held in memory, |
| 769 | ** depending on the values of the TEMP_STORE compile-time macro and the |
| 770 | ** db->temp_store variable, according to the following chart: |
| 771 | ** |
| 772 | ** TEMP_STORE db->temp_store Location of temporary database |
| 773 | ** ---------- -------------- ------------------------------ |
| 774 | ** 0 any file |
| 775 | ** 1 1 file |
| 776 | ** 1 2 memory |
| 777 | ** 1 0 file |
| 778 | ** 2 1 file |
| 779 | ** 2 2 memory |
| 780 | ** 2 0 memory |
| 781 | ** 3 any memory |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 782 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 783 | int sqlite3BtreeFactory( |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 784 | const sqlite *db, /* Main database when opening aux otherwise 0 */ |
| 785 | const char *zFilename, /* Name of the file containing the BTree database */ |
| 786 | int omitJournal, /* if TRUE then do not journal this file */ |
| 787 | int nCache, /* How many pages in the page cache */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 788 | Btree **ppBtree /* Pointer to new Btree object written here */ |
| 789 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 790 | int btree_flags = 0; |
| 791 | |
drh | eec983e | 2004-05-08 10:11:36 +0000 | [diff] [blame] | 792 | assert( ppBtree != 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 793 | if( omitJournal ){ |
| 794 | btree_flags |= BTREE_OMIT_JOURNAL; |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 795 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 796 | if( !zFilename ){ |
| 797 | btree_flags |= BTREE_MEMORY; |
| 798 | } |
| 799 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 800 | return sqlite3BtreeOpen(zFilename, ppBtree, nCache, btree_flags, |
| 801 | &db->busyHandler); |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 802 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 803 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 804 | /* |
| 805 | ** Return UTF-8 encoded English language explanation of the most recent |
| 806 | ** error. |
| 807 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 808 | const char *sqlite3_errmsg(sqlite3 *db){ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 809 | if( !db ){ |
| 810 | /* If db is NULL, then assume that a malloc() failed during an |
| 811 | ** sqlite3_open() call. |
| 812 | */ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 813 | return sqlite3ErrStr(SQLITE_NOMEM); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 814 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 815 | if( db->zErrMsg ){ |
| 816 | return db->zErrMsg; |
| 817 | } |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 818 | return sqlite3ErrStr(db->errCode); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 819 | } |
| 820 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 821 | /* |
| 822 | ** Return UTF-16 encoded English language explanation of the most recent |
| 823 | ** error. |
| 824 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 825 | const void *sqlite3_errmsg16(sqlite3 *db){ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 826 | if( !db ){ |
| 827 | /* If db is NULL, then assume that a malloc() failed during an |
| 828 | ** sqlite3_open() call. We have a static version of the string |
| 829 | ** "out of memory" encoded using UTF-16 just for this purpose. |
| 830 | ** |
| 831 | ** Because all the characters in the string are in the unicode |
| 832 | ** range 0x00-0xFF, if we pad the big-endian string with a |
| 833 | ** zero byte, we can obtain the little-endian string with |
| 834 | ** &big_endian[1]. |
| 835 | */ |
| 836 | static char outOfMemBe[] = { |
| 837 | 0, 'o', 0, 'u', 0, 't', 0, ' ', |
| 838 | 0, 'o', 0, 'f', 0, ' ', |
| 839 | 0, 'm', 0, 'e', 0, 'm', 0, 'o', 0, 'r', 0, 'y', 0, 0, 0 |
| 840 | }; |
| 841 | static char *outOfMemLe = &outOfMemBe[1]; |
| 842 | |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 843 | if( SQLITE_BIGENDIAN ){ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 844 | return (void *)outOfMemBe; |
| 845 | }else{ |
| 846 | return (void *)outOfMemLe; |
| 847 | } |
| 848 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 849 | if( !db->zErrMsg16 ){ |
| 850 | char const *zErr8 = sqlite3_errmsg(db); |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 851 | if( SQLITE_BIGENDIAN ){ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 852 | db->zErrMsg16 = sqlite3utf8to16be(zErr8, -1); |
| 853 | }else{ |
| 854 | db->zErrMsg16 = sqlite3utf8to16le(zErr8, -1); |
| 855 | } |
| 856 | } |
| 857 | return db->zErrMsg16; |
| 858 | } |
| 859 | |
| 860 | int sqlite3_errcode(sqlite3 *db){ |
| 861 | return db->errCode; |
| 862 | } |
| 863 | |
| 864 | /* |
| 865 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 866 | */ |
| 867 | int sqlite3_prepare( |
| 868 | sqlite3 *db, /* Database handle. */ |
| 869 | const char *zSql, /* UTF-8 encoded SQL statement. */ |
| 870 | int nBytes, /* Length of zSql in bytes. */ |
| 871 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 872 | const char** pzTail /* OUT: End of parsed string */ |
| 873 | ){ |
| 874 | Parse sParse; |
| 875 | char *zErrMsg = 0; |
| 876 | int rc = SQLITE_OK; |
| 877 | |
| 878 | if( sqlite3SafetyOn(db) ){ |
| 879 | rc = SQLITE_MISUSE; |
| 880 | goto prepare_out; |
| 881 | } |
| 882 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 883 | if( db->pVdbe==0 ){ db->nChange = 0; } |
| 884 | memset(&sParse, 0, sizeof(sParse)); |
| 885 | sParse.db = db; |
| 886 | sqlite3RunParser(&sParse, zSql, &zErrMsg); |
| 887 | |
| 888 | if( db->xTrace && !db->init.busy ){ |
| 889 | /* Trace only the statment that was compiled. |
| 890 | ** Make a copy of that part of the SQL string since zSQL is const |
| 891 | ** and we must pass a zero terminated string to the trace function |
| 892 | ** The copy is unnecessary if the tail pointer is pointing at the |
| 893 | ** beginnig or end of the SQL string. |
| 894 | */ |
| 895 | if( sParse.zTail && sParse.zTail!=zSql && *sParse.zTail ){ |
| 896 | char *tmpSql = sqliteStrNDup(zSql, sParse.zTail - zSql); |
| 897 | if( tmpSql ){ |
| 898 | db->xTrace(db->pTraceArg, tmpSql); |
| 899 | free(tmpSql); |
| 900 | }else{ |
| 901 | /* If a memory error occurred during the copy, |
| 902 | ** trace entire SQL string and fall through to the |
| 903 | ** sqlite3_malloc_failed test to report the error. |
| 904 | */ |
| 905 | db->xTrace(db->pTraceArg, zSql); |
| 906 | } |
| 907 | }else{ |
| 908 | db->xTrace(db->pTraceArg, zSql); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | if( sqlite3_malloc_failed ){ |
| 913 | rc = SQLITE_NOMEM; |
| 914 | sqlite3RollbackAll(db); |
| 915 | sqlite3ResetInternalSchema(db, 0); |
| 916 | db->flags &= ~SQLITE_InTrans; |
| 917 | goto prepare_out; |
| 918 | } |
| 919 | if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK; |
| 920 | if( sParse.rc==SQLITE_SCHEMA ){ |
| 921 | sqlite3ResetInternalSchema(db, 0); |
| 922 | } |
| 923 | assert( ppStmt ); |
| 924 | *ppStmt = (sqlite3_stmt*)sParse.pVdbe; |
| 925 | if( pzTail ) *pzTail = sParse.zTail; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 926 | rc = sParse.rc; |
| 927 | |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 928 | if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){ |
| 929 | sqlite3VdbeSetNumCols(sParse.pVdbe, 5); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 930 | sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC); |
| 931 | sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC); |
| 932 | sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC); |
| 933 | sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC); |
| 934 | sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 935 | } |
| 936 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 937 | prepare_out: |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 938 | if( sqlite3SafetyOff(db) ){ |
| 939 | rc = SQLITE_MISUSE; |
| 940 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 941 | if( zErrMsg ){ |
| 942 | sqlite3Error(db, rc, "%s", zErrMsg); |
| 943 | }else{ |
| 944 | sqlite3Error(db, rc, 0); |
| 945 | } |
| 946 | return rc; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | ** Compile the UTF-16 encoded SQL statement zSql into a statement handle. |
| 951 | */ |
| 952 | int sqlite3_prepare16( |
| 953 | sqlite3 *db, /* Database handle. */ |
| 954 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 955 | int nBytes, /* Length of zSql in bytes. */ |
| 956 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 957 | const void **pzTail /* OUT: End of parsed string */ |
| 958 | ){ |
| 959 | /* This function currently works by first transforming the UTF-16 |
| 960 | ** encoded string to UTF-8, then invoking sqlite3_prepare(). The |
| 961 | ** tricky bit is figuring out the pointer to return in *pzTail. |
| 962 | */ |
| 963 | char *zSql8 = 0; |
| 964 | char const *zTail8 = 0; |
| 965 | int rc; |
| 966 | |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 967 | zSql8 = sqlite3utf16to8(zSql, nBytes, SQLITE_BIGENDIAN); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 968 | if( !zSql8 ){ |
| 969 | sqlite3Error(db, SQLITE_NOMEM, 0); |
| 970 | return SQLITE_NOMEM; |
| 971 | } |
| 972 | rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8); |
| 973 | |
| 974 | if( zTail8 && pzTail ){ |
| 975 | /* If sqlite3_prepare returns a tail pointer, we calculate the |
| 976 | ** equivalent pointer into the UTF-16 string by counting the unicode |
| 977 | ** characters between zSql8 and zTail8, and then returning a pointer |
| 978 | ** the same number of characters into the UTF-16 string. |
| 979 | */ |
| 980 | int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8); |
| 981 | *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed); |
| 982 | } |
| 983 | |
| 984 | return rc; |
| 985 | } |
| 986 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 987 | /* |
| 988 | ** This routine does the work of opening a database on behalf of |
| 989 | ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
| 990 | ** is UTF-8 encoded. The fourth argument, "def_enc" is one of the TEXT_* |
| 991 | ** macros from sqliteInt.h. If we end up creating a new database file |
| 992 | ** (not opening an existing one), the text encoding of the database |
| 993 | ** will be set to this value. |
| 994 | */ |
| 995 | static int openDatabase( |
| 996 | const char *zFilename, /* Database filename UTF-8 encoded */ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 997 | sqlite3 **ppDb /* OUT: Returned database handle */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 998 | ){ |
| 999 | sqlite3 *db; |
| 1000 | int rc, i; |
| 1001 | char *zErrMsg = 0; |
| 1002 | |
| 1003 | /* Allocate the sqlite data structure */ |
| 1004 | db = sqliteMalloc( sizeof(sqlite) ); |
| 1005 | if( db==0 ) goto opendb_out; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1006 | db->priorNewRowid = 0; |
| 1007 | db->magic = SQLITE_MAGIC_BUSY; |
| 1008 | db->nDb = 2; |
| 1009 | db->aDb = db->aDbStatic; |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1010 | db->enc = TEXT_Utf8; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1011 | db->autoCommit = 1; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1012 | /* db->flags |= SQLITE_ShortColNames; */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 1013 | sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1014 | sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0); |
| 1015 | for(i=0; i<db->nDb; i++){ |
| 1016 | sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0); |
| 1017 | sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0); |
| 1018 | sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0); |
| 1019 | sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1); |
| 1020 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1021 | |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame^] | 1022 | /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
| 1023 | ** and UTF-16, so add a version for each to avoid any unnecessary |
| 1024 | ** conversions. The only error that can occur here is a malloc() failure. |
| 1025 | */ |
| 1026 | sqlite3_create_collation(db, "BINARY", 0, 0, binaryCollatingFunc); |
| 1027 | sqlite3_create_collation(db, "BINARY", 1, 0, binaryCollatingFunc); |
| 1028 | db->pDfltColl = sqlite3FindCollSeq(db, "BINARY", 6, 0); |
| 1029 | if( !db->pDfltColl ){ |
| 1030 | rc = db->errCode; |
| 1031 | assert( rc!=SQLITE_OK ); |
| 1032 | db->magic = SQLITE_MAGIC_CLOSED; |
| 1033 | goto opendb_out; |
| 1034 | } |
| 1035 | |
| 1036 | /* Also add a UTF-8 case-insensitive collation sequence. */ |
| 1037 | sqlite3_create_collation(db, "NOCASE", 0, 0, nocaseCollatingFunc); |
| 1038 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1039 | /* Open the backend database driver */ |
| 1040 | if( zFilename[0]==':' && strcmp(zFilename,":memory:")==0 ){ |
| 1041 | db->temp_store = 2; |
| 1042 | } |
| 1043 | rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt); |
| 1044 | if( rc!=SQLITE_OK ){ |
| 1045 | /* FIX ME: sqlite3BtreeFactory() should call sqlite3Error(). */ |
| 1046 | sqlite3Error(db, rc, 0); |
| 1047 | db->magic = SQLITE_MAGIC_CLOSED; |
| 1048 | goto opendb_out; |
| 1049 | } |
| 1050 | db->aDb[0].zName = "main"; |
| 1051 | db->aDb[1].zName = "temp"; |
| 1052 | |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1053 | /* Register all built-in functions, but do not attempt to read the |
| 1054 | ** database schema yet. This is delayed until the first time the database |
| 1055 | ** is accessed. |
| 1056 | */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1057 | sqlite3RegisterBuiltinFunctions(db); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1058 | if( rc==SQLITE_OK ){ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1059 | db->magic = SQLITE_MAGIC_OPEN; |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1060 | }else{ |
| 1061 | sqlite3Error(db, rc, "%s", zErrMsg, 0); |
| 1062 | if( zErrMsg ) sqliteFree(zErrMsg); |
| 1063 | db->magic = SQLITE_MAGIC_CLOSED; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1064 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1065 | |
| 1066 | opendb_out: |
| 1067 | *ppDb = db; |
| 1068 | return sqlite3_errcode(db); |
| 1069 | } |
| 1070 | |
| 1071 | /* |
| 1072 | ** Open a new database handle. |
| 1073 | */ |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 1074 | int sqlite3_open( |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1075 | const char *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 1076 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1077 | ){ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1078 | return openDatabase(zFilename, ppDb); |
danielk1977 | 83ab5a8 | 2004-05-21 11:39:05 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1081 | /* |
| 1082 | ** Open a new database handle. |
| 1083 | */ |
| 1084 | int sqlite3_open16( |
| 1085 | const void *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 1086 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1087 | ){ |
| 1088 | char *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
| 1089 | int rc; |
| 1090 | |
| 1091 | assert( ppDb ); |
| 1092 | |
drh | 9c05483 | 2004-05-31 18:51:57 +0000 | [diff] [blame] | 1093 | zFilename8 = sqlite3utf16to8(zFilename, -1, SQLITE_BIGENDIAN); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1094 | if( !zFilename8 ){ |
| 1095 | *ppDb = 0; |
| 1096 | return SQLITE_NOMEM; |
| 1097 | } |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1098 | rc = openDatabase(zFilename8, ppDb); |
| 1099 | if( rc==SQLITE_OK && *ppDb ){ |
| 1100 | sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1101 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1102 | sqliteFree(zFilename8); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1103 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1104 | return rc; |
| 1105 | } |
| 1106 | |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1107 | /* |
| 1108 | ** The following routine destroys a virtual machine that is created by |
| 1109 | ** the sqlite3_compile() routine. The integer returned is an SQLITE_ |
| 1110 | ** success/failure code that describes the result of executing the virtual |
| 1111 | ** machine. |
| 1112 | ** |
| 1113 | ** This routine sets the error code and string returned by |
| 1114 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 1115 | */ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 1116 | int sqlite3_finalize(sqlite3_stmt *pStmt){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1117 | return sqlite3VdbeFinalize((Vdbe*)pStmt, 0); |
| 1118 | } |
| 1119 | |
| 1120 | /* |
| 1121 | ** Terminate the current execution of an SQL statement and reset it |
| 1122 | ** back to its starting state so that it can be reused. A success code from |
| 1123 | ** the prior execution is returned. |
| 1124 | ** |
| 1125 | ** This routine sets the error code and string returned by |
| 1126 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 1127 | */ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 1128 | int sqlite3_reset(sqlite3_stmt *pStmt){ |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1129 | int rc = sqlite3VdbeReset((Vdbe*)pStmt, 0); |
| 1130 | sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0); |
| 1131 | return rc; |
| 1132 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame^] | 1133 | |
| 1134 | int sqlite3_create_collation( |
| 1135 | sqlite3* db, |
| 1136 | const char *zName, |
| 1137 | int pref16, |
| 1138 | void* pCtx, |
| 1139 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 1140 | ){ |
| 1141 | CollSeq *pColl; |
| 1142 | int rc = SQLITE_OK; |
| 1143 | pColl = sqlite3FindCollSeq(db, zName, strlen(zName), 1); |
| 1144 | if( 0==pColl ){ |
| 1145 | rc = SQLITE_NOMEM; |
| 1146 | }else if( pref16 ){ |
| 1147 | pColl->xCmp16 = xCompare; |
| 1148 | pColl->pUser16 = pCtx; |
| 1149 | }else{ |
| 1150 | pColl->xCmp = xCompare; |
| 1151 | pColl->pUser = pCtx; |
| 1152 | } |
| 1153 | sqlite3Error(db, rc, 0); |
| 1154 | return SQLITE_OK; |
| 1155 | } |
| 1156 | |
| 1157 | int sqlite3_create_collation16( |
| 1158 | sqlite3* db, |
| 1159 | const char *zName, |
| 1160 | int pref16, |
| 1161 | void* pCtx, |
| 1162 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 1163 | ){ |
| 1164 | int rc; |
| 1165 | char *zName8 = sqlite3utf16to8(zName, -1, SQLITE_BIGENDIAN); |
| 1166 | rc = sqlite3_create_collation(db, zName8, pref16, pCtx, xCompare); |
| 1167 | sqliteFree(zName8); |
| 1168 | return rc; |
| 1169 | } |
| 1170 | |