blob: 6fae4df94669c4b640c685425bb232458f8ff0f4 [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001/*
2** Copyright (c) 1999, 2000 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** Main file for the SQLite library. The routines in this file
25** implement the programmer interface to the library. Routines in
26** other files are for internal use by SQLite and should not be
27** accessed by users of the library.
28**
drhdf16aed2000-06-02 14:27:22 +000029** $Id: main.c,v 1.7 2000/06/02 14:27:23 drh Exp $
drh75897232000-05-29 14:26:00 +000030*/
31#include "sqliteInt.h"
32
33/*
34** This is the callback routine for the code that initializes the
35** database. Each callback contains text of a CREATE TABLE or
36** CREATE INDEX statement that must be parsed to yield the internal
37** structures that describe the tables.
38*/
39static int sqliteOpenCb(void *pDb, int argc, char **argv, char **azColName){
40 sqlite *db = (sqlite*)pDb;
41 Parse sParse;
42 int nErr;
43 char *zErrMsg = 0;
44
45 if( argc!=1 ) return 0;
46 memset(&sParse, 0, sizeof(sParse));
47 sParse.db = db;
48 sParse.initFlag = 1;
49 nErr = sqliteRunParser(&sParse, argv[0], &zErrMsg);
50 return nErr;
51}
52
53/*
drh58b95762000-06-02 01:17:37 +000054** Attempt to read the database schema and initialize internal
55** data structures. Return one of the SQLITE_ error codes to
56** indicate success or failure.
drhbed86902000-06-02 13:27:59 +000057**
58** After the database is initialized, the SQLITE_Initialized
59** bit is set in the flags field of the sqlite structure. An
60** attempt is made to initialize the database as soon as it
61** is opened. If that fails (perhaps because another process
62** has the sqlite_master table locked) than another attempt
63** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +000064*/
drh58b95762000-06-02 01:17:37 +000065static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +000066 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +000067 int rc;
68
69 /*
70 ** The master database table has a structure like this
71 */
drh75897232000-05-29 14:26:00 +000072 static char master_schema[] =
73 "CREATE TABLE " MASTER_NAME " (\n"
74 " type text,\n"
75 " name text,\n"
76 " tbl_name text,\n"
77 " sql text\n"
78 ")"
79 ;
80
81 /* The following program is used to initialize the internal
82 ** structure holding the tables and indexes of the database.
83 ** The database contains a special table named "sqlite_master"
84 ** defined as follows:
85 **
86 ** CREATE TABLE sqlite_master (
87 ** type text, -- Either "table" or "index"
88 ** name text, -- Name of table or index
89 ** tbl_name text, -- Associated table
90 ** sql text -- The CREATE statement for this object
91 ** );
92 **
93 ** The sqlite_master table contains a single entry for each table
94 ** and each index. The "type" field tells whether the entry is
95 ** a table or index. The "name" field is the name of the object.
96 ** The "tbl_name" is the name of the associated table. For tables,
97 ** the tbl_name field is always the same as name. For indices, the
98 ** tbl_name field contains the name of the table that the index
99 ** indexes. Finally, the sql field contains the complete text of
100 ** the CREATE TABLE or CREATE INDEX statement that originally created
101 ** the table or index.
102 **
103 ** The following program invokes its callback on the SQL for each
104 ** table then goes back and invokes the callback on the
105 ** SQL for each index. The callback will invoke the
106 ** parser to build the internal representation of the
107 ** database scheme.
108 */
109 static VdbeOp initProg[] = {
drheec553b2000-06-02 01:51:20 +0000110 { OP_Open, 0, 0, MASTER_NAME},
drh75897232000-05-29 14:26:00 +0000111 { OP_Next, 0, 8, 0}, /* 1 */
112 { OP_Field, 0, 0, 0},
113 { OP_String, 0, 0, "table"},
114 { OP_Ne, 0, 1, 0},
115 { OP_Field, 0, 3, 0},
116 { OP_Callback, 1, 0, 0},
117 { OP_Goto, 0, 1, 0},
118 { OP_Rewind, 0, 0, 0}, /* 8 */
119 { OP_Next, 0, 16, 0}, /* 9 */
120 { OP_Field, 0, 0, 0},
121 { OP_String, 0, 0, "index"},
122 { OP_Ne, 0, 9, 0},
123 { OP_Field, 0, 3, 0},
124 { OP_Callback, 1, 0, 0},
125 { OP_Goto, 0, 9, 0},
126 { OP_Halt, 0, 0, 0}, /* 16 */
127 };
128
drh58b95762000-06-02 01:17:37 +0000129 /* Create a virtual machine to run the initialization program. Run
130 ** the program. The delete the virtual machine.
131 */
132 vdbe = sqliteVdbeCreate(db->pBe);
133 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
134 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg);
135 sqliteVdbeDelete(vdbe);
136 if( rc==SQLITE_OK ){
137 Table *pTab;
138 char *azArg[2];
139 azArg[0] = master_schema;
140 azArg[1] = 0;
141 sqliteOpenCb(db, 1, azArg, 0);
142 pTab = sqliteFindTable(db, MASTER_NAME);
143 if( pTab ){
144 pTab->readOnly = 1;
145 }
146 db->flags |= SQLITE_Initialized;
147 }else{
148 sqliteStrRealloc(pzErrMsg);
149 }
150 return rc;
151}
152
153/*
154** Open a new SQLite database. Construct an "sqlite" structure to define
155** the state of this database and return a pointer to that structure.
156**
157** An attempt is made to initialize the in-memory data structures that
158** hold the database schema. But if this fails (because the schema file
159** is locked) then that step is deferred until the first call to
160** sqlite_exec().
161*/
162sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
163 sqlite *db;
164 int rc;
165
166 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000167 db = sqliteMalloc( sizeof(sqlite) );
168 if( pzErrMsg ) *pzErrMsg = 0;
169 if( db==0 ){
170 sqliteSetString(pzErrMsg, "out of memory", 0);
drhc3c2fc92000-05-31 22:58:39 +0000171 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000172 return 0;
173 }
174
175 /* Open the backend database driver */
176 db->pBe = sqliteDbbeOpen(zFilename, (mode&0222)!=0, mode!=0, pzErrMsg);
177 if( db->pBe==0 ){
178 sqliteFree(db);
179 return 0;
180 }
181
drh58b95762000-06-02 01:17:37 +0000182 /* Attempt to read the schema */
183 rc = sqliteInit(db, pzErrMsg);
184 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
185 sqlite_close(db);
186 return 0;
drhbed86902000-06-02 13:27:59 +0000187 }else{
drhdf16aed2000-06-02 14:27:22 +0000188 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000189 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000190 }
drh75897232000-05-29 14:26:00 +0000191 return db;
192}
193
194/*
195** Close an existing SQLite database
196*/
197void sqlite_close(sqlite *db){
198 int i;
199 sqliteDbbeClose(db->pBe);
200 for(i=0; i<N_HASH; i++){
201 Table *pNext, *pList = db->apTblHash[i];
202 db->apTblHash[i] = 0;
203 while( pList ){
204 pNext = pList->pHash;
205 pList->pHash = 0;
206 sqliteDeleteTable(db, pList);
207 pList = pNext;
208 }
209 }
210 sqliteFree(db);
211}
212
213/*
214** Return TRUE if the given SQL string ends in a semicolon.
215*/
216int sqlite_complete(const char *zSql){
217 int i;
218 int lastWasSemi = 0;
219
220 i = 0;
221 while( i>=0 && zSql[i]!=0 ){
222 int tokenType;
223 int n;
224
225 n = sqliteGetToken(&zSql[i], &tokenType);
226 switch( tokenType ){
227 case TK_SPACE:
228 case TK_COMMENT:
229 break;
230 case TK_SEMI:
231 lastWasSemi = 1;
232 break;
233 default:
234 lastWasSemi = 0;
235 break;
236 }
237 i += n;
238 }
239 return lastWasSemi;
240}
241
242/*
drhbed86902000-06-02 13:27:59 +0000243** Execute SQL code. Return one of the SQLITE_ success/failure
244** codes. Also write an error message into memory obtained from
245** malloc() and make *pzErrMsg point to that message.
246**
247** If the SQL is a query, then for each row in the query result
248** the xCallback() function is called. pArg becomes the first
249** argument to xCallback(). If xCallback=NULL then no callback
250** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000251*/
252int sqlite_exec(
253 sqlite *db, /* The database on which the SQL executes */
254 char *zSql, /* The SQL to be executed */
255 sqlite_callback xCallback, /* Invoke this callback routine */
256 void *pArg, /* First argument to xCallback() */
257 char **pzErrMsg /* Write error messages here */
258){
259 Parse sParse;
drh58b95762000-06-02 01:17:37 +0000260 int rc;
drh75897232000-05-29 14:26:00 +0000261
262 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000263 if( (db->flags & SQLITE_Initialized)==0 ){
264 int rc = sqliteInit(db, pzErrMsg);
265 if( rc!=SQLITE_OK ) return rc;
266 }
drh75897232000-05-29 14:26:00 +0000267 memset(&sParse, 0, sizeof(sParse));
268 sParse.db = db;
269 sParse.xCallback = xCallback;
270 sParse.pArg = pArg;
drh58b95762000-06-02 01:17:37 +0000271 rc = sqliteRunParser(&sParse, zSql, pzErrMsg);
drhc3c2fc92000-05-31 22:58:39 +0000272 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000273 return rc;
drh75897232000-05-29 14:26:00 +0000274}