blob: 3c81beca33052932a72d5ee3c4f0d3e82295a473 [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**
drhd8bc7082000-06-07 23:51:50 +000029** $Id: main.c,v 1.12 2000/06/07 23:51:50 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);
drhd8bc7082000-06-07 23:51:50 +0000132 if( vdbe==0 ){
133 sqliteSetString(pzErrMsg, "out of memory",0);
134 return 1;
135 }
drh58b95762000-06-02 01:17:37 +0000136 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
137 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg);
138 sqliteVdbeDelete(vdbe);
139 if( rc==SQLITE_OK ){
140 Table *pTab;
141 char *azArg[2];
142 azArg[0] = master_schema;
143 azArg[1] = 0;
144 sqliteOpenCb(db, 1, azArg, 0);
145 pTab = sqliteFindTable(db, MASTER_NAME);
146 if( pTab ){
147 pTab->readOnly = 1;
148 }
149 db->flags |= SQLITE_Initialized;
150 }else{
151 sqliteStrRealloc(pzErrMsg);
152 }
153 return rc;
154}
155
156/*
157** Open a new SQLite database. Construct an "sqlite" structure to define
158** the state of this database and return a pointer to that structure.
159**
160** An attempt is made to initialize the in-memory data structures that
161** hold the database schema. But if this fails (because the schema file
162** is locked) then that step is deferred until the first call to
163** sqlite_exec().
164*/
165sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
166 sqlite *db;
167 int rc;
168
169 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000170 db = sqliteMalloc( sizeof(sqlite) );
171 if( pzErrMsg ) *pzErrMsg = 0;
172 if( db==0 ){
173 sqliteSetString(pzErrMsg, "out of memory", 0);
drhc3c2fc92000-05-31 22:58:39 +0000174 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000175 return 0;
176 }
177
178 /* Open the backend database driver */
179 db->pBe = sqliteDbbeOpen(zFilename, (mode&0222)!=0, mode!=0, pzErrMsg);
180 if( db->pBe==0 ){
drh5974a302000-06-07 14:42:26 +0000181 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000182 sqliteFree(db);
183 return 0;
184 }
185
drh58b95762000-06-02 01:17:37 +0000186 /* Attempt to read the schema */
187 rc = sqliteInit(db, pzErrMsg);
188 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
189 sqlite_close(db);
190 return 0;
drhbed86902000-06-02 13:27:59 +0000191 }else{
drhcc85b412000-06-07 15:11:27 +0000192 free(*pzErrMsg);
drhbed86902000-06-02 13:27:59 +0000193 *pzErrMsg = 0;
drh75897232000-05-29 14:26:00 +0000194 }
drh75897232000-05-29 14:26:00 +0000195 return db;
196}
197
198/*
199** Close an existing SQLite database
200*/
201void sqlite_close(sqlite *db){
202 int i;
203 sqliteDbbeClose(db->pBe);
204 for(i=0; i<N_HASH; i++){
205 Table *pNext, *pList = db->apTblHash[i];
206 db->apTblHash[i] = 0;
207 while( pList ){
208 pNext = pList->pHash;
209 pList->pHash = 0;
210 sqliteDeleteTable(db, pList);
211 pList = pNext;
212 }
213 }
214 sqliteFree(db);
215}
216
217/*
218** Return TRUE if the given SQL string ends in a semicolon.
219*/
220int sqlite_complete(const char *zSql){
221 int i;
222 int lastWasSemi = 0;
223
224 i = 0;
225 while( i>=0 && zSql[i]!=0 ){
226 int tokenType;
227 int n;
228
229 n = sqliteGetToken(&zSql[i], &tokenType);
230 switch( tokenType ){
231 case TK_SPACE:
232 case TK_COMMENT:
233 break;
234 case TK_SEMI:
235 lastWasSemi = 1;
236 break;
237 default:
238 lastWasSemi = 0;
239 break;
240 }
241 i += n;
242 }
243 return lastWasSemi;
244}
245
246/*
drhbed86902000-06-02 13:27:59 +0000247** Execute SQL code. Return one of the SQLITE_ success/failure
248** codes. Also write an error message into memory obtained from
249** malloc() and make *pzErrMsg point to that message.
250**
251** If the SQL is a query, then for each row in the query result
252** the xCallback() function is called. pArg becomes the first
253** argument to xCallback(). If xCallback=NULL then no callback
254** is invoked, even for queries.
drh75897232000-05-29 14:26:00 +0000255*/
256int sqlite_exec(
257 sqlite *db, /* The database on which the SQL executes */
258 char *zSql, /* The SQL to be executed */
259 sqlite_callback xCallback, /* Invoke this callback routine */
260 void *pArg, /* First argument to xCallback() */
261 char **pzErrMsg /* Write error messages here */
262){
263 Parse sParse;
drh58b95762000-06-02 01:17:37 +0000264 int rc;
drh75897232000-05-29 14:26:00 +0000265
266 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000267 if( (db->flags & SQLITE_Initialized)==0 ){
268 int rc = sqliteInit(db, pzErrMsg);
269 if( rc!=SQLITE_OK ) return rc;
270 }
drh75897232000-05-29 14:26:00 +0000271 memset(&sParse, 0, sizeof(sParse));
272 sParse.db = db;
273 sParse.xCallback = xCallback;
274 sParse.pArg = pArg;
drh58b95762000-06-02 01:17:37 +0000275 rc = sqliteRunParser(&sParse, zSql, pzErrMsg);
drhc3c2fc92000-05-31 22:58:39 +0000276 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000277 return rc;
drh75897232000-05-29 14:26:00 +0000278}