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 | ** |
drh | ef0715c | 2004-06-29 10:53:54 +0000 | [diff] [blame] | 17 | ** $Id: main.c,v 1.240 2004/06/29 10:53:55 drh 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){ |
drh | ef0715c | 2004-06-29 10:53:54 +0000 | [diff] [blame] | 43 | if( !sqlite3_malloc_failed ){ |
| 44 | sqlite3SetString(pData->pzErrMsg, "malformed database schema", |
| 45 | zExtra!=0 && zExtra[0]!=0 ? " - " : (char*)0, zExtra, (char*)0); |
| 46 | } |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 47 | } |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 50 | ** This is the callback routine for the code that initializes the |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 51 | ** database. See sqlite3Init() below for additional information. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 52 | ** |
| 53 | ** Each callback contains the following information: |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 54 | ** |
drh | 4a32431 | 2001-12-21 14:30:42 +0000 | [diff] [blame] | 55 | ** argv[0] = "file-format" or "schema-cookie" or "table" or "index" |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 56 | ** argv[1] = table or index name or meta statement type. |
| 57 | ** argv[2] = root page number for table or index. NULL for meta. |
drh | e78e828 | 2003-01-19 03:59:45 +0000 | [diff] [blame] | 58 | ** argv[3] = SQL text for a CREATE TABLE or CREATE INDEX statement. |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 59 | ** argv[4] = "1" for temporary files, "0" for main database, "2" or more |
| 60 | ** for auxiliary database files. |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 61 | ** |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 62 | */ |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 63 | static |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 64 | int sqlite3InitCallback(void *pInit, int argc, char **argv, char **azColName){ |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 65 | InitData *pData = (InitData*)pInit; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 66 | int nErr = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 67 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 68 | assert( argc==5 ); |
drh | 98e3e60 | 2003-07-27 17:26:22 +0000 | [diff] [blame] | 69 | if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 70 | if( argv[0]==0 ){ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 71 | corruptSchema(pData, 0); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 72 | return 1; |
| 73 | } |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 74 | switch( argv[0][0] ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 75 | case 'v': |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 76 | case 'i': |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 77 | case 't': { /* CREATE TABLE, CREATE INDEX, or CREATE VIEW statements */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 78 | sqlite *db = pData->db; |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 79 | if( argv[2]==0 || argv[4]==0 ){ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 80 | corruptSchema(pData, 0); |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 81 | return 1; |
| 82 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 83 | if( argv[3] && argv[3][0] ){ |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 84 | /* Call the parser to process a CREATE TABLE, INDEX or VIEW. |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 85 | ** 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] | 86 | ** or executed. All the parser does is build the internal data |
drh | 17f7193 | 2002-02-21 12:01:27 +0000 | [diff] [blame] | 87 | ** structures that describe the table, index, or view. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 88 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 89 | char *zErr; |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 90 | int rc; |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 91 | 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]); |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 95 | rc = sqlite3_exec(db, argv[3], 0, 0, &zErr); |
| 96 | db->init.iDb = 0; |
| 97 | if( SQLITE_OK!=rc ){ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 98 | corruptSchema(pData, zErr); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 99 | sqlite3_free(zErr); |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 100 | return rc; |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 101 | } |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 102 | }else{ |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 103 | /* 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 |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 105 | ** constraint for a CREATE TABLE. The index should have already |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 106 | ** been created when we processed the CREATE TABLE. All we have |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 107 | ** to do here is record the root page number for that index. |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 108 | */ |
drh | d24cc42 | 2003-03-27 12:51:24 +0000 | [diff] [blame] | 109 | int iDb; |
| 110 | Index *pIndex; |
| 111 | |
| 112 | iDb = atoi(argv[4]); |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 113 | assert( iDb>=0 && iDb<db->nDb ); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 114 | pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zName); |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 115 | if( pIndex==0 || pIndex->tnum!=0 ){ |
drh | da9e034 | 2002-01-10 14:31:48 +0000 | [diff] [blame] | 116 | /* 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 */; |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 122 | }else{ |
| 123 | pIndex->tnum = atoi(argv[2]); |
| 124 | } |
| 125 | } |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 126 | } |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 127 | break; |
drh | d78eeee | 2001-09-13 16:18:53 +0000 | [diff] [blame] | 128 | default: { |
| 129 | /* This can not happen! */ |
| 130 | nErr = 1; |
| 131 | assert( nErr==0 ); |
| 132 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 133 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 134 | return nErr; |
| 135 | } |
| 136 | |
| 137 | /* |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 138 | ** Attempt to read the database schema and initialize internal |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 139 | ** 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 |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 143 | ** indicate success or failure. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 144 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 145 | static int sqlite3InitOne(sqlite *db, int iDb, char **pzErrMsg){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 146 | int rc; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 147 | BtCursor *curMain; |
| 148 | int size; |
| 149 | Table *pTab; |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 150 | char const *azArg[6]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 151 | char zDbNum[30]; |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 152 | int meta[10]; |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 153 | InitData initData; |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 154 | char const *zMasterSchema; |
| 155 | char const *zMasterName; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 156 | |
| 157 | /* |
| 158 | ** The master database table has a structure like this |
| 159 | */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 160 | static char master_schema[] = |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 161 | "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" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 171 | " type text,\n" |
| 172 | " name text,\n" |
| 173 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 174 | " rootpage integer,\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 175 | " sql text\n" |
| 176 | ")" |
| 177 | ; |
| 178 | |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 179 | assert( iDb>=0 && iDb<db->nDb ); |
drh | 603240c | 2002-03-05 01:11:12 +0000 | [diff] [blame] | 180 | |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 181 | /* 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. |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 184 | */ |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 185 | 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. */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 194 | sqlite3SafetyOff(db); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 195 | azArg[0] = "table"; |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 196 | azArg[1] = zMasterName; |
danielk1977 | 8e15081 | 2004-05-10 01:17:37 +0000 | [diff] [blame] | 197 | azArg[2] = "1"; |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 198 | azArg[3] = zMasterSchema; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 199 | sprintf(zDbNum, "%d", iDb); |
| 200 | azArg[4] = zDbNum; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 201 | azArg[5] = 0; |
drh | c231172 | 2002-07-19 17:46:38 +0000 | [diff] [blame] | 202 | initData.db = db; |
| 203 | initData.pzErrMsg = pzErrMsg; |
danielk1977 | 2b44485 | 2004-06-29 07:45:33 +0000 | [diff] [blame] | 204 | rc = sqlite3InitCallback(&initData, 5, (char **)azArg, 0); |
| 205 | if( rc!=SQLITE_OK ){ |
| 206 | sqlite3SafetyOn(db); |
| 207 | return rc; |
| 208 | } |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 209 | pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 210 | if( pTab ){ |
| 211 | pTab->readOnly = 1; |
drh | d8bc708 | 2000-06-07 23:51:50 +0000 | [diff] [blame] | 212 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 213 | sqlite3SafetyOn(db); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 214 | |
| 215 | /* Create a cursor to hold the database open |
| 216 | */ |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 217 | if( db->aDb[iDb].pBt==0 ) return SQLITE_OK; |
danielk1977 | 8e15081 | 2004-05-10 01:17:37 +0000 | [diff] [blame] | 218 | rc = sqlite3BtreeCursor(db->aDb[iDb].pBt, MASTER_ROOT, 0, 0, 0, &curMain); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 219 | if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 220 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
drh | 92ed08a | 2002-07-30 18:43:40 +0000 | [diff] [blame] | 221 | return rc; |
| 222 | } |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 223 | |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 224 | /* 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. |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 230 | ** meta[3] |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 231 | ** meta[4] Db text encoding. 1:UTF-8 3:UTF-16 LE 4:UTF-16 BE |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 232 | ** meta[5] |
danielk1977 | 962398d | 2004-06-14 09:35:16 +0000 | [diff] [blame] | 233 | ** meta[6] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 234 | ** meta[7] |
| 235 | ** meta[8] |
| 236 | ** meta[9] |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 237 | ** |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 238 | ** Note: The hash defined SQLITE_UTF* symbols in sqliteInt.h correspond to |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 239 | ** the possible values of meta[4]. |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 240 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 241 | if( rc==SQLITE_OK ){ |
| 242 | int i; |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 243 | for(i=0; rc==SQLITE_OK && i<sizeof(meta)/sizeof(meta[0]); i++){ |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 244 | rc = sqlite3BtreeGetMeta(db->aDb[iDb].pBt, i+1, (u32 *)&meta[i]); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 245 | } |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 246 | if( rc ){ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 247 | sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 248 | sqlite3BtreeCloseCursor(curMain); |
| 249 | return rc; |
| 250 | } |
| 251 | }else{ |
| 252 | memset(meta, 0, sizeof(meta)); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 253 | } |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 254 | db->aDb[iDb].schema_cookie = meta[0]; |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 255 | |
| 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. */ |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 264 | db->enc = (u8)meta[4]; |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 265 | db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0); |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 266 | }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 | } |
danielk1977 | 172bc39 | 2004-05-22 08:09:11 +0000 | [diff] [blame] | 274 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 275 | } |
| 276 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 277 | size = meta[2]; |
| 278 | if( size==0 ){ size = MAX_PAGES; } |
| 279 | db->aDb[iDb].cache_size = size; |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 280 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 281 | if( iDb==0 ){ |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 282 | db->file_format = meta[1]; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 283 | if( db->file_format==0 ){ |
| 284 | /* This happens if the database was initially empty */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 285 | db->file_format = 1; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 286 | } |
drh | 2803757 | 2000-08-02 13:47:41 +0000 | [diff] [blame] | 287 | } |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 288 | |
| 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 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 298 | sqlite3BtreeSetCacheSize(db->aDb[iDb].pBt, db->aDb[iDb].cache_size); |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 299 | |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 300 | /* Read the schema information out of the schema tables |
drh | aacc543 | 2002-01-06 17:07:40 +0000 | [diff] [blame] | 301 | */ |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 302 | assert( db->init.busy ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 303 | if( rc==SQLITE_EMPTY ){ |
| 304 | /* For an empty database, there is nothing to read */ |
| 305 | rc = SQLITE_OK; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 306 | }else{ |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 307 | char *zSql = 0; |
danielk1977 | 3df6b25 | 2004-05-29 10:23:19 +0000 | [diff] [blame] | 308 | sqlite3SafetyOff(db); |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 309 | 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); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 314 | sqlite3SafetyOn(db); |
| 315 | sqlite3BtreeCloseCursor(curMain); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 316 | } |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 317 | if( sqlite3_malloc_failed ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 318 | sqlite3SetString(pzErrMsg, "out of memory", (char*)0); |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 319 | rc = SQLITE_NOMEM; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 320 | sqlite3ResetInternalSchema(db, 0); |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 321 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 322 | if( rc==SQLITE_OK ){ |
drh | 8bf8dc9 | 2003-05-17 17:35:10 +0000 | [diff] [blame] | 323 | DbSetProperty(db, iDb, DB_SchemaLoaded); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 324 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 325 | sqlite3ResetInternalSchema(db, iDb); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 326 | } |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 327 | return rc; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 328 | } |
| 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 |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 337 | ** bit is set in the flags field of the sqlite structure. |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 338 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 339 | int sqlite3Init(sqlite *db, char **pzErrMsg){ |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 340 | int i, rc; |
| 341 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 342 | if( db->init.busy ) return SQLITE_OK; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 343 | assert( (db->flags & SQLITE_Initialized)==0 ); |
| 344 | rc = SQLITE_OK; |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 345 | db->init.busy = 1; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 346 | for(i=0; rc==SQLITE_OK && i<db->nDb; i++){ |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 347 | if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 348 | rc = sqlite3InitOne(db, i, pzErrMsg); |
drh | 8ef83ff | 2004-02-12 15:31:21 +0000 | [diff] [blame] | 349 | if( rc ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 350 | sqlite3ResetInternalSchema(db, i); |
drh | 8ef83ff | 2004-02-12 15:31:21 +0000 | [diff] [blame] | 351 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 352 | } |
danielk1977 | d008cfe | 2004-06-19 02:22:10 +0000 | [diff] [blame] | 353 | |
| 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 | |
drh | 1d85d93 | 2004-02-14 23:05:52 +0000 | [diff] [blame] | 365 | db->init.busy = 0; |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 366 | if( rc==SQLITE_OK ){ |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 367 | db->flags |= SQLITE_Initialized; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 368 | sqlite3CommitInternalChanges(db); |
drh | 2d71ca9 | 2004-02-10 02:27:04 +0000 | [diff] [blame] | 369 | } |
| 370 | |
drh | 2d71ca9 | 2004-02-10 02:27:04 +0000 | [diff] [blame] | 371 | if( rc!=SQLITE_OK ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 372 | db->flags &= ~SQLITE_Initialized; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 373 | } |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 374 | return rc; |
drh | 58b9576 | 2000-06-02 01:17:37 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /* |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 378 | ** 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 | */ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 381 | int sqlite3ReadSchema(Parse *pParse){ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 382 | int rc = SQLITE_OK; |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 383 | sqlite3 *db = pParse->db; |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 384 | if( !db->init.busy ){ |
| 385 | if( (db->flags & SQLITE_Initialized)==0 ){ |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 386 | rc = sqlite3Init(db, &pParse->zErrMsg); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
danielk1977 | c039139 | 2004-06-09 12:30:04 +0000 | [diff] [blame] | 389 | assert( rc!=SQLITE_OK || (db->flags & SQLITE_Initialized)||db->init.busy ); |
danielk1977 | 8a41449 | 2004-06-29 08:59:35 +0000 | [diff] [blame] | 390 | if( rc!=SQLITE_OK ){ |
| 391 | pParse->rc = rc; |
| 392 | pParse->nErr++; |
| 393 | } |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 394 | return rc; |
| 395 | } |
| 396 | |
| 397 | /* |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 398 | ** The version of the library |
| 399 | */ |
drh | 38f8271 | 2004-06-18 17:10:16 +0000 | [diff] [blame] | 400 | const char rcsid3[] = "@(#) \044Id: SQLite version " SQLITE_VERSION " $"; |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 401 | const char sqlite3_version[] = SQLITE_VERSION; |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 402 | |
| 403 | /* |
drh | d3d39e9 | 2004-05-20 22:16:29 +0000 | [diff] [blame] | 404 | ** This is the default collating function named "BINARY" which is always |
| 405 | ** available. |
| 406 | */ |
| 407 | static 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 | /* |
danielk1977 | dc1bdc4 | 2004-06-11 10:51:27 +0000 | [diff] [blame] | 422 | ** 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. |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 429 | */ |
| 430 | static 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 | /* |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 444 | ** Return the ROWID of the most recent insert |
| 445 | */ |
drh | efad999 | 2004-06-22 12:13:55 +0000 | [diff] [blame] | 446 | sqlite_int64 sqlite3_last_insert_rowid(sqlite *db){ |
drh | af9ff33 | 2002-01-16 21:00:27 +0000 | [diff] [blame] | 447 | return db->lastRowid; |
| 448 | } |
| 449 | |
| 450 | /* |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 451 | ** Return the number of changes in the most recent call to sqlite3_exec(). |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 452 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 453 | int sqlite3_changes(sqlite *db){ |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 454 | return db->nChange; |
| 455 | } |
| 456 | |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 457 | /* |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 458 | ** Return the number of changes since the database handle was opened. |
rdc | f146a77 | 2004-02-25 22:51:06 +0000 | [diff] [blame] | 459 | */ |
danielk1977 | b28af71 | 2004-06-21 06:50:26 +0000 | [diff] [blame] | 460 | int sqlite3_total_changes(sqlite3 *db){ |
| 461 | return db->nTotalChange; |
rdc | b0c374f | 2004-02-20 22:53:38 +0000 | [diff] [blame] | 462 | } |
| 463 | |
drh | c8d30ac | 2002-04-12 10:08:59 +0000 | [diff] [blame] | 464 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame] | 465 | ** Close an existing SQLite database |
| 466 | */ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 467 | int sqlite3_close(sqlite *db){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 468 | HashElem *i; |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 469 | int j; |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 470 | |
| 471 | if( !db ){ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 472 | return SQLITE_OK; |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 473 | } |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 474 | |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 475 | 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 | |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 482 | /* 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) ); |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 489 | |
| 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 | */ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 495 | if( db->magic!=SQLITE_MAGIC_CLOSED && sqlite3SafetyOn(db) ){ |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 496 | /* printf("DID NOT CLOSE\n"); fflush(stdout); */ |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 497 | return SQLITE_ERROR; |
drh | 94e9203 | 2003-02-16 22:21:32 +0000 | [diff] [blame] | 498 | } |
danielk1977 | e004840 | 2004-06-15 16:51:01 +0000 | [diff] [blame] | 499 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 500 | for(j=0; j<db->nDb; j++){ |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 501 | struct Db *pDb = &db->aDb[j]; |
| 502 | if( pDb->pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 503 | sqlite3BtreeClose(pDb->pBt); |
drh | 4d189ca | 2004-02-12 18:46:38 +0000 | [diff] [blame] | 504 | pDb->pBt = 0; |
drh | 113088e | 2003-03-20 01:16:58 +0000 | [diff] [blame] | 505 | } |
drh | f57b339 | 2001-10-08 13:22:32 +0000 | [diff] [blame] | 506 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 507 | sqlite3ResetInternalSchema(db, 0); |
drh | 1c2d841 | 2003-03-31 00:30:47 +0000 | [diff] [blame] | 508 | assert( db->nDb<=2 ); |
| 509 | assert( db->aDb==db->aDbStatic ); |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 510 | for(i=sqliteHashFirst(&db->aFunc); i; i=sqliteHashNext(i)){ |
| 511 | FuncDef *pFunc, *pNext; |
| 512 | for(pFunc = (FuncDef*)sqliteHashData(i); pFunc; pFunc=pNext){ |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 513 | pNext = pFunc->pNext; |
| 514 | sqliteFree(pFunc); |
| 515 | } |
| 516 | } |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 517 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 518 | for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 519 | CollSeq *pColl = (CollSeq *)sqliteHashData(i); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 520 | sqliteFree(pColl); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 521 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 522 | sqlite3HashClear(&db->aCollSeq); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 523 | |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 524 | sqlite3HashClear(&db->aFunc); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 525 | sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 526 | if( db->pValue ){ |
| 527 | sqlite3ValueFree(db->pValue); |
| 528 | } |
| 529 | if( db->pErr ){ |
| 530 | sqlite3ValueFree(db->pErr); |
| 531 | } |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 532 | |
| 533 | db->magic = SQLITE_MAGIC_ERROR; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 534 | sqliteFree(db); |
danielk1977 | 96d81f9 | 2004-06-19 03:33:57 +0000 | [diff] [blame] | 535 | return SQLITE_OK; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 539 | ** Rollback all database files. |
| 540 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 541 | void sqlite3RollbackAll(sqlite *db){ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 542 | int i; |
| 543 | for(i=0; i<db->nDb; i++){ |
| 544 | if( db->aDb[i].pBt ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 545 | sqlite3BtreeRollback(db->aDb[i].pBt); |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 546 | db->aDb[i].inTrans = 0; |
| 547 | } |
| 548 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 549 | sqlite3ResetInternalSchema(db, 0); |
| 550 | /* sqlite3RollbackInternalChanges(db); */ |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /* |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 554 | ** Return a static string that describes the kind of error specified in the |
| 555 | ** argument. |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 556 | */ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 557 | const char *sqlite3ErrStr(int rc){ |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 558 | 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; |
drh | 8766c34 | 2002-11-09 00:33:15 +0000 | [diff] [blame] | 582 | case SQLITE_NOLFS: z = "kernel lacks large file support"; break; |
drh | ed6c867 | 2003-01-12 18:02:16 +0000 | [diff] [blame] | 583 | case SQLITE_AUTH: z = "authorization denied"; break; |
jplyon | 892f671 | 2003-06-12 08:59:00 +0000 | [diff] [blame] | 584 | case SQLITE_FORMAT: z = "auxiliary database format error"; break; |
drh | 7c972de | 2003-09-06 22:18:07 +0000 | [diff] [blame] | 585 | case SQLITE_RANGE: z = "bind index out of range"; break; |
drh | c602f9a | 2004-02-12 19:01:04 +0000 | [diff] [blame] | 586 | case SQLITE_NOTADB: z = "file is encrypted or is not a database";break; |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 587 | default: z = "unknown error"; break; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 588 | } |
drh | c22bd47 | 2002-05-10 13:14:07 +0000 | [diff] [blame] | 589 | return z; |
drh | 247be43 | 2002-05-10 05:44:55 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /* |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 593 | ** 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 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 598 | static int sqliteDefaultBusyCallback( |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 599 | void *Timeout, /* Maximum amount of time to wait */ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 600 | int count /* Number of times table has been busy */ |
| 601 | ){ |
drh | 8cfbf08 | 2001-09-19 13:22:39 +0000 | [diff] [blame] | 602 | #if SQLITE_MIN_SLEEP_MS==1 |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 603 | 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])) |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 608 | int timeout = (int)Timeout; |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 609 | int delay, prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 610 | |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 611 | 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); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 617 | } |
drh | d1bec47 | 2004-01-15 13:29:31 +0000 | [diff] [blame] | 618 | if( prior + delay > timeout ){ |
| 619 | delay = timeout - prior; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 620 | if( delay<=0 ) return 0; |
| 621 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 622 | sqlite3OsSleep(delay); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 623 | return 1; |
| 624 | #else |
| 625 | int timeout = (int)Timeout; |
| 626 | if( (count+1)*1000 > timeout ){ |
| 627 | return 0; |
| 628 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 629 | sqlite3OsSleep(1000); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 630 | 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 | */ |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 638 | int sqlite3_busy_handler( |
| 639 | sqlite3 *db, |
danielk1977 | 2a764eb | 2004-06-12 01:43:26 +0000 | [diff] [blame] | 640 | int (*xBusy)(void*,int), |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 641 | void *pArg |
| 642 | ){ |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 643 | db->busyHandler.xFunc = xBusy; |
| 644 | db->busyHandler.pArg = pArg; |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 645 | return SQLITE_OK; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 646 | } |
| 647 | |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 648 | #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 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 654 | void sqlite3_progress_handler( |
danielk1977 | 348bb5d | 2003-10-18 09:37:26 +0000 | [diff] [blame] | 655 | 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 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 673 | /* |
| 674 | ** This routine installs a default busy handler that waits for the |
| 675 | ** specified number of milliseconds before returning 0. |
| 676 | */ |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 677 | int sqlite3_busy_timeout(sqlite3 *db, int ms){ |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 678 | if( ms>0 ){ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 679 | sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)ms); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 680 | }else{ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 681 | sqlite3_busy_handler(db, 0, 0); |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 682 | } |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 683 | return SQLITE_OK; |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 684 | } |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 685 | |
| 686 | /* |
| 687 | ** Cause any pending operation to stop at its earliest opportunity. |
| 688 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 689 | void sqlite3_interrupt(sqlite *db){ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 690 | db->flags |= SQLITE_Interrupt; |
| 691 | } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 692 | |
| 693 | /* |
| 694 | ** Windows systems should call this routine to free memory that |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 695 | ** is returned in the in the errmsg parameter of sqlite3_open() when |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 696 | ** 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 |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 701 | ** sqlite3StrRealloc(). |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 702 | */ |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 703 | void sqlite3_free(char *p){ free(p); } |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 704 | |
| 705 | /* |
drh | df01489 | 2004-06-02 00:41:09 +0000 | [diff] [blame] | 706 | ** Create new user functions. |
drh | fa86c41 | 2002-02-02 15:01:15 +0000 | [diff] [blame] | 707 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 708 | int sqlite3_create_function( |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 709 | sqlite3 *db, |
| 710 | const char *zFunctionName, |
| 711 | int nArg, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 712 | int enc, |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 713 | void *pUserData, |
| 714 | void (*xFunc)(sqlite3_context*,int,sqlite3_value **), |
| 715 | void (*xStep)(sqlite3_context*,int,sqlite3_value **), |
| 716 | void (*xFinal)(sqlite3_context*) |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 717 | ){ |
drh | 0bce835 | 2002-02-28 00:41:10 +0000 | [diff] [blame] | 718 | FuncDef *p; |
drh | 4b59ab5 | 2002-08-24 18:24:51 +0000 | [diff] [blame] | 719 | int nName; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 720 | |
danielk1977 | 398eae7 | 2004-05-26 06:58:43 +0000 | [diff] [blame] | 721 | 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))) ){ |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 727 | return SQLITE_ERROR; |
| 728 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 729 | |
| 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, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 742 | pUserData, xFunc, xStep, xFinal); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 743 | if( rc!=SQLITE_OK ) return rc; |
| 744 | rc = sqlite3_create_function(db, zFunctionName, nArg, SQLITE_UTF16LE, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 745 | pUserData, xFunc, xStep, xFinal); |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 746 | if( rc!=SQLITE_OK ) return rc; |
| 747 | enc = SQLITE_UTF16BE; |
| 748 | } |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 749 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 750 | p = sqlite3FindFunction(db, zFunctionName, nName, nArg, enc, 1); |
drh | 4e0f995 | 2002-02-27 01:53:13 +0000 | [diff] [blame] | 751 | if( p==0 ) return 1; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 752 | p->xFunc = xFunc; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 753 | p->xStep = xStep; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 754 | p->xFinalize = xFinal; |
drh | 1350b03 | 2002-02-27 19:00:20 +0000 | [diff] [blame] | 755 | p->pUserData = pUserData; |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 756 | return SQLITE_OK; |
| 757 | } |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 758 | int sqlite3_create_function16( |
| 759 | sqlite3 *db, |
| 760 | const void *zFunctionName, |
| 761 | int nArg, |
| 762 | int eTextRep, |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 763 | 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; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 769 | 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 ){ |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 776 | return SQLITE_NOMEM; |
| 777 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 778 | rc = sqlite3_create_function(db, zFunc8, nArg, eTextRep, |
danielk1977 | f9d64d2 | 2004-06-19 08:18:07 +0000 | [diff] [blame] | 779 | pUserData, xFunc, xStep, xFinal); |
danielk1977 | 6590493 | 2004-05-26 06:18:37 +0000 | [diff] [blame] | 780 | return rc; |
drh | 8e0a2f9 | 2002-02-23 23:45:45 +0000 | [diff] [blame] | 781 | } |
drh | c9b84a1 | 2002-06-20 11:36:48 +0000 | [diff] [blame] | 782 | |
| 783 | /* |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 784 | ** 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 |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 789 | ** sqlite3_exec(). |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 790 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 791 | void *sqlite3_trace(sqlite *db, void (*xTrace)(void*,const char*), void *pArg){ |
drh | 18de482 | 2003-01-16 16:28:53 +0000 | [diff] [blame] | 792 | void *pOld = db->pTraceArg; |
| 793 | db->xTrace = xTrace; |
| 794 | db->pTraceArg = pArg; |
| 795 | return pOld; |
drh | 0d1a643 | 2003-04-03 15:46:04 +0000 | [diff] [blame] | 796 | } |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 797 | |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 798 | /*** 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 | */ |
danielk1977 | 24b03fd | 2004-05-10 10:34:34 +0000 | [diff] [blame] | 804 | void *sqlite3_commit_hook( |
drh | aa940ea | 2004-01-15 02:44:03 +0000 | [diff] [blame] | 805 | 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 | |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 816 | /* |
drh | 13bff81 | 2003-04-15 01:19:47 +0000 | [diff] [blame] | 817 | ** 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. |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 824 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 825 | int sqlite3BtreeFactory( |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 826 | 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 */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 830 | Btree **ppBtree /* Pointer to new Btree object written here */ |
| 831 | ){ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 832 | int btree_flags = 0; |
| 833 | |
drh | eec983e | 2004-05-08 10:11:36 +0000 | [diff] [blame] | 834 | assert( ppBtree != 0); |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 835 | if( omitJournal ){ |
| 836 | btree_flags |= BTREE_OMIT_JOURNAL; |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 837 | } |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 838 | 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 | */ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 842 | btree_flags |= BTREE_MEMORY; |
| 843 | } |
| 844 | |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 845 | return sqlite3BtreeOpen(zFilename, ppBtree, nCache, btree_flags, |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 846 | (void *)&db->busyHandler); |
paul | b0208cc | 2003-04-13 18:26:49 +0000 | [diff] [blame] | 847 | } |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 848 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 849 | /* |
| 850 | ** Return UTF-8 encoded English language explanation of the most recent |
| 851 | ** error. |
| 852 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 853 | const char *sqlite3_errmsg(sqlite3 *db){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 854 | if( !db || !db->pErr ){ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 855 | /* If db is NULL, then assume that a malloc() failed during an |
| 856 | ** sqlite3_open() call. |
| 857 | */ |
danielk1977 | f20b21c | 2004-05-31 23:56:42 +0000 | [diff] [blame] | 858 | return sqlite3ErrStr(SQLITE_NOMEM); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 859 | } |
danielk1977 | b2c7f5b | 2004-06-26 10:02:16 +0000 | [diff] [blame] | 860 | if( db->magic!=SQLITE_MAGIC_OPEN && |
| 861 | db->magic!=SQLITE_MAGIC_BUSY && |
| 862 | db->magic!=SQLITE_MAGIC_CLOSED |
| 863 | ){ |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 864 | return sqlite3ErrStr(SQLITE_MISUSE); |
| 865 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 866 | if( !sqlite3_value_text(db->pErr) ){ |
| 867 | return sqlite3ErrStr(db->errCode); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 868 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 869 | return sqlite3_value_text(db->pErr); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 870 | } |
| 871 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 872 | /* |
| 873 | ** Return UTF-16 encoded English language explanation of the most recent |
| 874 | ** error. |
| 875 | */ |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 876 | const void *sqlite3_errmsg16(sqlite3 *db){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 877 | /* 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 | }; |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 887 | 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 | }; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 895 | |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 896 | if( db && db->pErr ){ |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 897 | if( db->magic==SQLITE_MAGIC_ERROR ){ |
| 898 | return (void *)(&misuseBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); |
| 899 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 900 | if( !sqlite3_value_text16(db->pErr) ){ |
| 901 | sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), |
| 902 | SQLITE_UTF8, SQLITE_STATIC); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 903 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 904 | if( sqlite3_value_text16(db->pErr) ){ |
| 905 | return sqlite3_value_text16(db->pErr); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 906 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | /* If db is NULL, then assume that a malloc() failed during an |
| 910 | ** sqlite3_open() call. We have a static version of the string |
| 911 | ** "out of memory" encoded using UTF-16 just for this purpose. |
| 912 | */ |
| 913 | return (void *)(&outOfMemBe[SQLITE_UTF16NATIVE==SQLITE_UTF16LE?1:0]); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | int sqlite3_errcode(sqlite3 *db){ |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 917 | if( !db ) return SQLITE_NOMEM; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 918 | return db->errCode; |
| 919 | } |
| 920 | |
| 921 | /* |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 922 | ** Check schema cookies in all databases except TEMP. If any cookie is out |
| 923 | ** of date, return 0. If all schema cookies are current, return 1. |
| 924 | */ |
| 925 | static int schemaIsValid(sqlite *db){ |
| 926 | int iDb; |
| 927 | int rc; |
| 928 | BtCursor *curTemp; |
| 929 | int cookie; |
| 930 | int allOk = 1; |
| 931 | |
| 932 | for(iDb=0; allOk && iDb<db->nDb; iDb++){ |
| 933 | Btree *pBt; |
| 934 | if( iDb==1 ) continue; |
| 935 | pBt = db->aDb[iDb].pBt; |
| 936 | if( pBt==0 ) continue; |
| 937 | rc = sqlite3BtreeCursor(pBt, MASTER_ROOT, 0, 0, 0, &curTemp); |
| 938 | if( rc==SQLITE_OK ){ |
danielk1977 | e0d4b06 | 2004-06-28 01:11:46 +0000 | [diff] [blame] | 939 | rc = sqlite3BtreeGetMeta(pBt, 1, (u32 *)&cookie); |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 940 | if( rc==SQLITE_OK && cookie!=db->aDb[iDb].schema_cookie ){ |
| 941 | allOk = 0; |
| 942 | } |
| 943 | sqlite3BtreeCloseCursor(curTemp); |
| 944 | } |
| 945 | } |
| 946 | return allOk; |
| 947 | } |
| 948 | |
| 949 | /* |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 950 | ** Compile the UTF-8 encoded SQL statement zSql into a statement handle. |
| 951 | */ |
| 952 | int sqlite3_prepare( |
| 953 | sqlite3 *db, /* Database handle. */ |
| 954 | const char *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 char** pzTail /* OUT: End of parsed string */ |
| 958 | ){ |
| 959 | Parse sParse; |
| 960 | char *zErrMsg = 0; |
| 961 | int rc = SQLITE_OK; |
| 962 | |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 963 | assert( ppStmt ); |
| 964 | *ppStmt = 0; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 965 | if( sqlite3SafetyOn(db) ){ |
danielk1977 | e35ee19 | 2004-06-26 09:50:11 +0000 | [diff] [blame] | 966 | return SQLITE_MISUSE; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 967 | } |
| 968 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 969 | memset(&sParse, 0, sizeof(sParse)); |
| 970 | sParse.db = db; |
| 971 | sqlite3RunParser(&sParse, zSql, &zErrMsg); |
| 972 | |
| 973 | if( db->xTrace && !db->init.busy ){ |
| 974 | /* Trace only the statment that was compiled. |
| 975 | ** Make a copy of that part of the SQL string since zSQL is const |
| 976 | ** and we must pass a zero terminated string to the trace function |
| 977 | ** The copy is unnecessary if the tail pointer is pointing at the |
| 978 | ** beginnig or end of the SQL string. |
| 979 | */ |
| 980 | if( sParse.zTail && sParse.zTail!=zSql && *sParse.zTail ){ |
| 981 | char *tmpSql = sqliteStrNDup(zSql, sParse.zTail - zSql); |
| 982 | if( tmpSql ){ |
| 983 | db->xTrace(db->pTraceArg, tmpSql); |
| 984 | free(tmpSql); |
| 985 | }else{ |
| 986 | /* If a memory error occurred during the copy, |
| 987 | ** trace entire SQL string and fall through to the |
| 988 | ** sqlite3_malloc_failed test to report the error. |
| 989 | */ |
| 990 | db->xTrace(db->pTraceArg, zSql); |
| 991 | } |
| 992 | }else{ |
| 993 | db->xTrace(db->pTraceArg, zSql); |
| 994 | } |
| 995 | } |
| 996 | |
drh | 35d4c2f | 2004-06-10 01:30:59 +0000 | [diff] [blame] | 997 | /* Print a copy of SQL as it is executed if the SQL_TRACE pragma is turned |
| 998 | ** on in debugging mode. |
| 999 | */ |
| 1000 | #ifdef SQLITE_DEBUG |
| 1001 | if( (db->flags & SQLITE_SqlTrace)!=0 && sParse.zTail && sParse.zTail!=zSql ){ |
| 1002 | sqlite3DebugPrintf("SQL-trace: %.*s\n", sParse.zTail - zSql, zSql); |
| 1003 | } |
| 1004 | #endif /* SQLITE_DEBUG */ |
| 1005 | |
| 1006 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1007 | if( sqlite3_malloc_failed ){ |
| 1008 | rc = SQLITE_NOMEM; |
| 1009 | sqlite3RollbackAll(db); |
| 1010 | sqlite3ResetInternalSchema(db, 0); |
| 1011 | db->flags &= ~SQLITE_InTrans; |
| 1012 | goto prepare_out; |
| 1013 | } |
| 1014 | if( sParse.rc==SQLITE_DONE ) sParse.rc = SQLITE_OK; |
drh | a6ecd33 | 2004-06-10 00:29:09 +0000 | [diff] [blame] | 1015 | if( sParse.checkSchema && !schemaIsValid(db) ){ |
| 1016 | sParse.rc = SQLITE_SCHEMA; |
| 1017 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1018 | if( sParse.rc==SQLITE_SCHEMA ){ |
| 1019 | sqlite3ResetInternalSchema(db, 0); |
| 1020 | } |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1021 | if( pzTail ) *pzTail = sParse.zTail; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1022 | rc = sParse.rc; |
| 1023 | |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1024 | if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){ |
| 1025 | sqlite3VdbeSetNumCols(sParse.pVdbe, 5); |
danielk1977 | 3cf8606 | 2004-05-26 10:11:05 +0000 | [diff] [blame] | 1026 | sqlite3VdbeSetColName(sParse.pVdbe, 0, "addr", P3_STATIC); |
| 1027 | sqlite3VdbeSetColName(sParse.pVdbe, 1, "opcode", P3_STATIC); |
| 1028 | sqlite3VdbeSetColName(sParse.pVdbe, 2, "p1", P3_STATIC); |
| 1029 | sqlite3VdbeSetColName(sParse.pVdbe, 3, "p2", P3_STATIC); |
| 1030 | sqlite3VdbeSetColName(sParse.pVdbe, 4, "p3", P3_STATIC); |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1033 | prepare_out: |
danielk1977 | 22322fd | 2004-05-25 23:35:17 +0000 | [diff] [blame] | 1034 | if( sqlite3SafetyOff(db) ){ |
| 1035 | rc = SQLITE_MISUSE; |
| 1036 | } |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 1037 | if( rc==SQLITE_OK ){ |
| 1038 | *ppStmt = (sqlite3_stmt*)sParse.pVdbe; |
| 1039 | }else if( sParse.pVdbe ){ |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 1040 | sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); |
danielk1977 | 5c4c778 | 2004-06-16 10:39:23 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1043 | if( zErrMsg ){ |
| 1044 | sqlite3Error(db, rc, "%s", zErrMsg); |
danielk1977 | b20e56b | 2004-06-15 13:36:30 +0000 | [diff] [blame] | 1045 | sqliteFree(zErrMsg); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1046 | }else{ |
| 1047 | sqlite3Error(db, rc, 0); |
| 1048 | } |
| 1049 | return rc; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | ** Compile the UTF-16 encoded SQL statement zSql into a statement handle. |
| 1054 | */ |
| 1055 | int sqlite3_prepare16( |
| 1056 | sqlite3 *db, /* Database handle. */ |
| 1057 | const void *zSql, /* UTF-8 encoded SQL statement. */ |
| 1058 | int nBytes, /* Length of zSql in bytes. */ |
| 1059 | sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ |
| 1060 | const void **pzTail /* OUT: End of parsed string */ |
| 1061 | ){ |
| 1062 | /* This function currently works by first transforming the UTF-16 |
| 1063 | ** encoded string to UTF-8, then invoking sqlite3_prepare(). The |
| 1064 | ** tricky bit is figuring out the pointer to return in *pzTail. |
| 1065 | */ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1066 | char const *zSql8 = 0; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1067 | char const *zTail8 = 0; |
| 1068 | int rc; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1069 | sqlite3_value *pTmp; |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1070 | |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1071 | pTmp = sqlite3GetTransientValue(db); |
| 1072 | sqlite3ValueSetStr(pTmp, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 1073 | zSql8 = sqlite3ValueText(pTmp, SQLITE_UTF8); |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 1074 | if( !zSql8 ){ |
| 1075 | sqlite3Error(db, SQLITE_NOMEM, 0); |
| 1076 | return SQLITE_NOMEM; |
| 1077 | } |
| 1078 | rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8); |
| 1079 | |
| 1080 | if( zTail8 && pzTail ){ |
| 1081 | /* If sqlite3_prepare returns a tail pointer, we calculate the |
| 1082 | ** equivalent pointer into the UTF-16 string by counting the unicode |
| 1083 | ** characters between zSql8 and zTail8, and then returning a pointer |
| 1084 | ** the same number of characters into the UTF-16 string. |
| 1085 | */ |
| 1086 | int chars_parsed = sqlite3utf8CharLen(zSql8, zTail8-zSql8); |
| 1087 | *pzTail = (u8 *)zSql + sqlite3utf16ByteLen(zSql, chars_parsed); |
| 1088 | } |
| 1089 | |
| 1090 | return rc; |
| 1091 | } |
| 1092 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1093 | /* |
| 1094 | ** This routine does the work of opening a database on behalf of |
| 1095 | ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" |
| 1096 | ** is UTF-8 encoded. The fourth argument, "def_enc" is one of the TEXT_* |
| 1097 | ** macros from sqliteInt.h. If we end up creating a new database file |
| 1098 | ** (not opening an existing one), the text encoding of the database |
| 1099 | ** will be set to this value. |
| 1100 | */ |
| 1101 | static int openDatabase( |
| 1102 | const char *zFilename, /* Database filename UTF-8 encoded */ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1103 | sqlite3 **ppDb /* OUT: Returned database handle */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1104 | ){ |
| 1105 | sqlite3 *db; |
| 1106 | int rc, i; |
| 1107 | char *zErrMsg = 0; |
| 1108 | |
| 1109 | /* Allocate the sqlite data structure */ |
| 1110 | db = sqliteMalloc( sizeof(sqlite) ); |
| 1111 | if( db==0 ) goto opendb_out; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1112 | db->priorNewRowid = 0; |
| 1113 | db->magic = SQLITE_MAGIC_BUSY; |
| 1114 | db->nDb = 2; |
| 1115 | db->aDb = db->aDbStatic; |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 1116 | db->enc = SQLITE_UTF8; |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 1117 | db->autoCommit = 1; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1118 | /* db->flags |= SQLITE_ShortColNames; */ |
drh | f9b596e | 2004-05-26 16:54:42 +0000 | [diff] [blame] | 1119 | sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1120 | sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0); |
| 1121 | for(i=0; i<db->nDb; i++){ |
| 1122 | sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0); |
| 1123 | sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0); |
| 1124 | sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0); |
| 1125 | sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1); |
| 1126 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1127 | |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1128 | /* Add the default collation sequence BINARY. BINARY works for both UTF-8 |
| 1129 | ** and UTF-16, so add a version for each to avoid any unnecessary |
| 1130 | ** conversions. The only error that can occur here is a malloc() failure. |
| 1131 | */ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1132 | sqlite3_create_collation(db, "BINARY", SQLITE_UTF8, 0,binaryCollatingFunc); |
| 1133 | sqlite3_create_collation(db, "BINARY", SQLITE_UTF16LE, 0,binaryCollatingFunc); |
| 1134 | sqlite3_create_collation(db, "BINARY", SQLITE_UTF16BE, 0,binaryCollatingFunc); |
| 1135 | db->pDfltColl = sqlite3FindCollSeq(db, db->enc, "BINARY", 6, 0); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1136 | if( !db->pDfltColl ){ |
| 1137 | rc = db->errCode; |
| 1138 | assert( rc!=SQLITE_OK ); |
| 1139 | db->magic = SQLITE_MAGIC_CLOSED; |
| 1140 | goto opendb_out; |
| 1141 | } |
| 1142 | |
| 1143 | /* Also add a UTF-8 case-insensitive collation sequence. */ |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1144 | sqlite3_create_collation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1145 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1146 | /* Open the backend database driver */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1147 | rc = sqlite3BtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt); |
| 1148 | if( rc!=SQLITE_OK ){ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1149 | sqlite3Error(db, rc, 0); |
| 1150 | db->magic = SQLITE_MAGIC_CLOSED; |
| 1151 | goto opendb_out; |
| 1152 | } |
| 1153 | db->aDb[0].zName = "main"; |
| 1154 | db->aDb[1].zName = "temp"; |
| 1155 | |
danielk1977 | 91cf71b | 2004-06-26 06:37:06 +0000 | [diff] [blame] | 1156 | /* The default safety_level for the main database is 'full' for the temp |
| 1157 | ** database it is 'NONE'. This matches the pager layer defaults. */ |
| 1158 | db->aDb[0].safety_level = 3; |
| 1159 | db->aDb[1].safety_level = 1; |
| 1160 | |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1161 | /* Register all built-in functions, but do not attempt to read the |
| 1162 | ** database schema yet. This is delayed until the first time the database |
| 1163 | ** is accessed. |
| 1164 | */ |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1165 | sqlite3RegisterBuiltinFunctions(db); |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1166 | if( rc==SQLITE_OK ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1167 | sqlite3Error(db, SQLITE_OK, 0); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1168 | db->magic = SQLITE_MAGIC_OPEN; |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1169 | }else{ |
| 1170 | sqlite3Error(db, rc, "%s", zErrMsg, 0); |
| 1171 | if( zErrMsg ) sqliteFree(zErrMsg); |
| 1172 | db->magic = SQLITE_MAGIC_CLOSED; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1173 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1174 | |
| 1175 | opendb_out: |
| 1176 | *ppDb = db; |
| 1177 | return sqlite3_errcode(db); |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | ** Open a new database handle. |
| 1182 | */ |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 1183 | int sqlite3_open( |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1184 | const char *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 1185 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1186 | ){ |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1187 | return openDatabase(zFilename, ppDb); |
danielk1977 | 83ab5a8 | 2004-05-21 11:39:05 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1190 | /* |
| 1191 | ** Open a new database handle. |
| 1192 | */ |
| 1193 | int sqlite3_open16( |
| 1194 | const void *zFilename, |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 1195 | sqlite3 **ppDb |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1196 | ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1197 | char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ |
| 1198 | int rc = SQLITE_NOMEM; |
| 1199 | sqlite3_value *pVal; |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1200 | |
| 1201 | assert( ppDb ); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1202 | *ppDb = 0; |
| 1203 | pVal = sqlite3ValueNew(); |
| 1204 | sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 1205 | zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); |
| 1206 | if( zFilename8 ){ |
| 1207 | rc = openDatabase(zFilename8, ppDb); |
| 1208 | if( rc==SQLITE_OK && *ppDb ){ |
| 1209 | sqlite3_exec(*ppDb, "PRAGMA encoding = 'UTF-16'", 0, 0, 0); |
| 1210 | } |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1211 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1212 | if( pVal ){ |
| 1213 | sqlite3ValueFree(pVal); |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1214 | } |
danielk1977 | 8e22787 | 2004-06-07 07:52:17 +0000 | [diff] [blame] | 1215 | |
danielk1977 | 4ad1713 | 2004-05-21 01:47:26 +0000 | [diff] [blame] | 1216 | return rc; |
| 1217 | } |
| 1218 | |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1219 | /* |
| 1220 | ** The following routine destroys a virtual machine that is created by |
| 1221 | ** the sqlite3_compile() routine. The integer returned is an SQLITE_ |
| 1222 | ** success/failure code that describes the result of executing the virtual |
| 1223 | ** machine. |
| 1224 | ** |
| 1225 | ** This routine sets the error code and string returned by |
| 1226 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 1227 | */ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 1228 | int sqlite3_finalize(sqlite3_stmt *pStmt){ |
danielk1977 | 9e6db7d | 2004-06-21 08:18:51 +0000 | [diff] [blame] | 1229 | return sqlite3VdbeFinalize((Vdbe*)pStmt); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | ** Terminate the current execution of an SQL statement and reset it |
| 1234 | ** back to its starting state so that it can be reused. A success code from |
| 1235 | ** the prior execution is returned. |
| 1236 | ** |
| 1237 | ** This routine sets the error code and string returned by |
| 1238 | ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16(). |
| 1239 | */ |
danielk1977 | fc57d7b | 2004-05-26 02:04:57 +0000 | [diff] [blame] | 1240 | int sqlite3_reset(sqlite3_stmt *pStmt){ |
danielk1977 | 9e6db7d | 2004-06-21 08:18:51 +0000 | [diff] [blame] | 1241 | int rc = sqlite3VdbeReset((Vdbe*)pStmt); |
danielk1977 | 106bb23 | 2004-05-21 10:08:53 +0000 | [diff] [blame] | 1242 | sqlite3VdbeMakeReady((Vdbe*)pStmt, -1, 0); |
| 1243 | return rc; |
| 1244 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1245 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1246 | /* |
| 1247 | ** Register a new collation sequence with the database handle db. |
| 1248 | */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1249 | int sqlite3_create_collation( |
| 1250 | sqlite3* db, |
| 1251 | const char *zName, |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1252 | int enc, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1253 | void* pCtx, |
| 1254 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 1255 | ){ |
| 1256 | CollSeq *pColl; |
| 1257 | int rc = SQLITE_OK; |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1258 | |
| 1259 | /* If SQLITE_UTF16 is specified as the encoding type, transform this |
| 1260 | ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the |
| 1261 | ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. |
| 1262 | */ |
| 1263 | if( enc==SQLITE_UTF16 ){ |
| 1264 | enc = SQLITE_UTF16NATIVE; |
| 1265 | } |
| 1266 | |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1267 | if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16LE && enc!=SQLITE_UTF16BE ){ |
| 1268 | sqlite3Error(db, SQLITE_ERROR, |
| 1269 | "Param 3 to sqlite3_create_collation() must be one of " |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1270 | "SQLITE_UTF8, SQLITE_UTF16, SQLITE_UTF16LE or SQLITE_UTF16BE" |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1271 | ); |
| 1272 | return SQLITE_ERROR; |
| 1273 | } |
| 1274 | pColl = sqlite3FindCollSeq(db, (u8)enc, zName, strlen(zName), 1); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1275 | if( 0==pColl ){ |
| 1276 | rc = SQLITE_NOMEM; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1277 | }else{ |
| 1278 | pColl->xCmp = xCompare; |
| 1279 | pColl->pUser = pCtx; |
| 1280 | } |
| 1281 | sqlite3Error(db, rc, 0); |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1282 | return rc; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1285 | /* |
| 1286 | ** Register a new collation sequence with the database handle db. |
| 1287 | */ |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1288 | int sqlite3_create_collation16( |
| 1289 | sqlite3* db, |
| 1290 | const char *zName, |
danielk1977 | 466be56 | 2004-06-10 02:16:01 +0000 | [diff] [blame] | 1291 | int enc, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1292 | void* pCtx, |
| 1293 | int(*xCompare)(void*,int,const void*,int,const void*) |
| 1294 | ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 1295 | char const *zName8; |
| 1296 | sqlite3_value *pTmp = sqlite3GetTransientValue(db); |
| 1297 | sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF16NATIVE, SQLITE_STATIC); |
| 1298 | zName8 = sqlite3ValueText(pTmp, SQLITE_UTF8); |
| 1299 | return sqlite3_create_collation(db, zName8, enc, pCtx, xCompare); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 1300 | } |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1301 | |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1302 | /* |
| 1303 | ** Register a collation sequence factory callback with the database handle |
| 1304 | ** db. Replace any previously installed collation sequence factory. |
| 1305 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1306 | int sqlite3_collation_needed( |
| 1307 | sqlite3 *db, |
| 1308 | void *pCollNeededArg, |
| 1309 | void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) |
| 1310 | ){ |
| 1311 | db->xCollNeeded = xCollNeeded; |
| 1312 | db->xCollNeeded16 = 0; |
| 1313 | db->pCollNeededArg = pCollNeededArg; |
| 1314 | return SQLITE_OK; |
| 1315 | } |
danielk1977 | d812336 | 2004-06-12 09:25:12 +0000 | [diff] [blame] | 1316 | |
| 1317 | /* |
| 1318 | ** Register a collation sequence factory callback with the database handle |
| 1319 | ** db. Replace any previously installed collation sequence factory. |
| 1320 | */ |
danielk1977 | 7cedc8d | 2004-06-10 10:50:08 +0000 | [diff] [blame] | 1321 | int sqlite3_collation_needed16( |
| 1322 | sqlite3 *db, |
| 1323 | void *pCollNeededArg, |
| 1324 | void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) |
| 1325 | ){ |
| 1326 | db->xCollNeeded = 0; |
| 1327 | db->xCollNeeded16 = xCollNeeded16; |
| 1328 | db->pCollNeededArg = pCollNeededArg; |
| 1329 | return SQLITE_OK; |
| 1330 | } |