blob: 317c08ce61d8a8c3f1266227898768bacfbc7d9b [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**
drh9bb61fe2000-06-05 16:01:39 +000029** $Id: main.c,v 1.9 2000/06/05 16:01:39 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;
drh75897232000-05-29 14:26:00 +000043
44 if( argc!=1 ) return 0;
45 memset(&sParse, 0, sizeof(sParse));
46 sParse.db = db;
47 sParse.initFlag = 1;
drhd1dedb82000-06-05 02:07:04 +000048 nErr = sqliteRunParser(&sParse, argv[0], 0);
drh75897232000-05-29 14:26:00 +000049 return nErr;
50}
51
52/*
drh58b95762000-06-02 01:17:37 +000053** Attempt to read the database schema and initialize internal
54** data structures. Return one of the SQLITE_ error codes to
55** indicate success or failure.
drhbed86902000-06-02 13:27:59 +000056**
57** After the database is initialized, the SQLITE_Initialized
58** bit is set in the flags field of the sqlite structure. An
59** attempt is made to initialize the database as soon as it
60** is opened. If that fails (perhaps because another process
61** has the sqlite_master table locked) than another attempt
62** is made the first time the database is accessed.
drh75897232000-05-29 14:26:00 +000063*/
drh58b95762000-06-02 01:17:37 +000064static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +000065 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +000066 int rc;
67
68 /*
69 ** The master database table has a structure like this
70 */
drh75897232000-05-29 14:26:00 +000071 static char master_schema[] =
72 "CREATE TABLE " MASTER_NAME " (\n"
73 " type text,\n"
74 " name text,\n"
75 " tbl_name text,\n"
76 " sql text\n"
77 ")"
78 ;
79
80 /* The following program is used to initialize the internal
81 ** structure holding the tables and indexes of the database.
82 ** The database contains a special table named "sqlite_master"
83 ** defined as follows:
84 **
85 ** CREATE TABLE sqlite_master (
86 ** type text, -- Either "table" or "index"
87 ** name text, -- Name of table or index
88 ** tbl_name text, -- Associated table
89 ** sql text -- The CREATE statement for this object
90 ** );
91 **
92 ** The sqlite_master table contains a single entry for each table
93 ** and each index. The "type" field tells whether the entry is
94 ** a table or index. The "name" field is the name of the object.
95 ** The "tbl_name" is the name of the associated table. For tables,
96 ** the tbl_name field is always the same as name. For indices, the
97 ** tbl_name field contains the name of the table that the index
98 ** indexes. Finally, the sql field contains the complete text of
99 ** the CREATE TABLE or CREATE INDEX statement that originally created
100 ** the table or index.
101 **
102 ** The following program invokes its callback on the SQL for each
103 ** table then goes back and invokes the callback on the
104 ** SQL for each index. The callback will invoke the
105 ** parser to build the internal representation of the
106 ** database scheme.
107 */
108 static VdbeOp initProg[] = {
drheec553b2000-06-02 01:51:20 +0000109 { OP_Open, 0, 0, MASTER_NAME},
drh75897232000-05-29 14:26:00 +0000110 { OP_Next, 0, 8, 0}, /* 1 */
111 { OP_Field, 0, 0, 0},
112 { OP_String, 0, 0, "table"},
113 { OP_Ne, 0, 1, 0},
114 { OP_Field, 0, 3, 0},
115 { OP_Callback, 1, 0, 0},
116 { OP_Goto, 0, 1, 0},
117 { OP_Rewind, 0, 0, 0}, /* 8 */
118 { OP_Next, 0, 16, 0}, /* 9 */
119 { OP_Field, 0, 0, 0},
120 { OP_String, 0, 0, "index"},
121 { OP_Ne, 0, 9, 0},
122 { OP_Field, 0, 3, 0},
123 { OP_Callback, 1, 0, 0},
124 { OP_Goto, 0, 9, 0},
125 { OP_Halt, 0, 0, 0}, /* 16 */
126 };
127
drh58b95762000-06-02 01:17:37 +0000128 /* Create a virtual machine to run the initialization program. Run
129 ** the program. The delete the virtual machine.
130 */
131 vdbe = sqliteVdbeCreate(db->pBe);
132 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
133 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg);
134 sqliteVdbeDelete(vdbe);
135 if( rc==SQLITE_OK ){
136 Table *pTab;
137 char *azArg[2];
138 azArg[0] = master_schema;
139 azArg[1] = 0;
140 sqliteOpenCb(db, 1, azArg, 0);
141 pTab = sqliteFindTable(db, MASTER_NAME);
142 if( pTab ){
143 pTab->readOnly = 1;
144 }
145 db->flags |= SQLITE_Initialized;
146 }else{
147 sqliteStrRealloc(pzErrMsg);
148 }
149 return rc;
150}
151
152/*
153** Open a new SQLite database. Construct an "sqlite" structure to define
154** the state of this database and return a pointer to that structure.
155**
156** An attempt is made to initialize the in-memory data structures that
157** hold the database schema. But if this fails (because the schema file
158** is locked) then that step is deferred until the first call to
159** sqlite_exec().
160*/
161sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
162 sqlite *db;
163 int rc;
164
165 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000166 db = sqliteMalloc( sizeof(sqlite) );
167 if( pzErrMsg ) *pzErrMsg = 0;
168 if( db==0 ){
169 sqliteSetString(pzErrMsg, "out of memory", 0);
drhc3c2fc92000-05-31 22:58:39 +0000170 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000171 return 0;
172 }
173
174 /* Open the backend database driver */
175 db->pBe = sqliteDbbeOpen(zFilename, (mode&0222)!=0, mode!=0, pzErrMsg);
176 if( db->pBe==0 ){
177 sqliteFree(db);
178 return 0;
179 }
180
drh58b95762000-06-02 01:17:37 +0000181 /* Attempt to read the schema */
182 rc = sqliteInit(db, pzErrMsg);
183 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
184 sqlite_close(db);
185 return 0;
drhbed86902000-06-02 13:27:59 +0000186 }else{
drhdf16aed2000-06-02 14:27:22 +0000187 sqliteFree(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000188 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000189 }
drh75897232000-05-29 14:26:00 +0000190 return db;
191}
192
193/*
194** Close an existing SQLite database
195*/
196void sqlite_close(sqlite *db){
197 int i;
198 sqliteDbbeClose(db->pBe);
199 for(i=0; i<N_HASH; i++){
200 Table *pNext, *pList = db->apTblHash[i];
201 db->apTblHash[i] = 0;
202 while( pList ){
203 pNext = pList->pHash;
204 pList->pHash = 0;
205 sqliteDeleteTable(db, pList);
206 pList = pNext;
207 }
208 }
209 sqliteFree(db);
210}
211
212/*
213** Return TRUE if the given SQL string ends in a semicolon.
214*/
215int sqlite_complete(const char *zSql){
216 int i;
217 int lastWasSemi = 0;
218
219 i = 0;
220 while( i>=0 && zSql[i]!=0 ){
221 int tokenType;
222 int n;
223
224 n = sqliteGetToken(&zSql[i], &tokenType);
225 switch( tokenType ){
226 case TK_SPACE:
227 case TK_COMMENT:
228 break;
229 case TK_SEMI:
230 lastWasSemi = 1;
231 break;
232 default:
233 lastWasSemi = 0;
234 break;
235 }
236 i += n;
237 }
238 return lastWasSemi;
239}
240
241/*
drhbed86902000-06-02 13:27:59 +0000242** Execute SQL code. Return one of the SQLITE_ success/failure
243** codes. Also write an error message into memory obtained from
244** malloc() and make *pzErrMsg point to that message.
245**
246** If the SQL is a query, then for each row in the query result
247** the xCallback() function is called. pArg becomes the first
248** argument to xCallback(). If xCallback=NULL then no callback
249** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000250*/
251int sqlite_exec(
252 sqlite *db, /* The database on which the SQL executes */
253 char *zSql, /* The SQL to be executed */
254 sqlite_callback xCallback, /* Invoke this callback routine */
255 void *pArg, /* First argument to xCallback() */
256 char **pzErrMsg /* Write error messages here */
257){
258 Parse sParse;
drh58b95762000-06-02 01:17:37 +0000259 int rc;
drh75897232000-05-29 14:26:00 +0000260
261 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000262 if( (db->flags & SQLITE_Initialized)==0 ){
263 int rc = sqliteInit(db, pzErrMsg);
264 if( rc!=SQLITE_OK ) return rc;
265 }
drh75897232000-05-29 14:26:00 +0000266 memset(&sParse, 0, sizeof(sParse));
267 sParse.db = db;
268 sParse.xCallback = xCallback;
269 sParse.pArg = pArg;
drh58b95762000-06-02 01:17:37 +0000270 rc = sqliteRunParser(&sParse, zSql, pzErrMsg);
drhc3c2fc92000-05-31 22:58:39 +0000271 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000272 return rc;
drh75897232000-05-29 14:26:00 +0000273}