blob: c035302b2956c710fa8699a71db9197cd54bc59b [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**
drheec553b2000-06-02 01:51:20 +000029** $Id: main.c,v 1.5 2000/06/02 01:51:20 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.
drh75897232000-05-29 14:26:00 +000057*/
drh58b95762000-06-02 01:17:37 +000058static int sqliteInit(sqlite *db, char **pzErrMsg){
drh75897232000-05-29 14:26:00 +000059 Vdbe *vdbe;
drh58b95762000-06-02 01:17:37 +000060 int rc;
61
62 /*
63 ** The master database table has a structure like this
64 */
drh75897232000-05-29 14:26:00 +000065 static char master_schema[] =
66 "CREATE TABLE " MASTER_NAME " (\n"
67 " type text,\n"
68 " name text,\n"
69 " tbl_name text,\n"
70 " sql text\n"
71 ")"
72 ;
73
74 /* The following program is used to initialize the internal
75 ** structure holding the tables and indexes of the database.
76 ** The database contains a special table named "sqlite_master"
77 ** defined as follows:
78 **
79 ** CREATE TABLE sqlite_master (
80 ** type text, -- Either "table" or "index"
81 ** name text, -- Name of table or index
82 ** tbl_name text, -- Associated table
83 ** sql text -- The CREATE statement for this object
84 ** );
85 **
86 ** The sqlite_master table contains a single entry for each table
87 ** and each index. The "type" field tells whether the entry is
88 ** a table or index. The "name" field is the name of the object.
89 ** The "tbl_name" is the name of the associated table. For tables,
90 ** the tbl_name field is always the same as name. For indices, the
91 ** tbl_name field contains the name of the table that the index
92 ** indexes. Finally, the sql field contains the complete text of
93 ** the CREATE TABLE or CREATE INDEX statement that originally created
94 ** the table or index.
95 **
96 ** The following program invokes its callback on the SQL for each
97 ** table then goes back and invokes the callback on the
98 ** SQL for each index. The callback will invoke the
99 ** parser to build the internal representation of the
100 ** database scheme.
101 */
102 static VdbeOp initProg[] = {
drheec553b2000-06-02 01:51:20 +0000103 { OP_Open, 0, 0, MASTER_NAME},
drh75897232000-05-29 14:26:00 +0000104 { OP_Next, 0, 8, 0}, /* 1 */
105 { OP_Field, 0, 0, 0},
106 { OP_String, 0, 0, "table"},
107 { OP_Ne, 0, 1, 0},
108 { OP_Field, 0, 3, 0},
109 { OP_Callback, 1, 0, 0},
110 { OP_Goto, 0, 1, 0},
111 { OP_Rewind, 0, 0, 0}, /* 8 */
112 { OP_Next, 0, 16, 0}, /* 9 */
113 { OP_Field, 0, 0, 0},
114 { OP_String, 0, 0, "index"},
115 { OP_Ne, 0, 9, 0},
116 { OP_Field, 0, 3, 0},
117 { OP_Callback, 1, 0, 0},
118 { OP_Goto, 0, 9, 0},
119 { OP_Halt, 0, 0, 0}, /* 16 */
120 };
121
drh58b95762000-06-02 01:17:37 +0000122 /* Create a virtual machine to run the initialization program. Run
123 ** the program. The delete the virtual machine.
124 */
125 vdbe = sqliteVdbeCreate(db->pBe);
126 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
127 rc = sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg);
128 sqliteVdbeDelete(vdbe);
129 if( rc==SQLITE_OK ){
130 Table *pTab;
131 char *azArg[2];
132 azArg[0] = master_schema;
133 azArg[1] = 0;
134 sqliteOpenCb(db, 1, azArg, 0);
135 pTab = sqliteFindTable(db, MASTER_NAME);
136 if( pTab ){
137 pTab->readOnly = 1;
138 }
139 db->flags |= SQLITE_Initialized;
140 }else{
141 sqliteStrRealloc(pzErrMsg);
142 }
143 return rc;
144}
145
146/*
147** Open a new SQLite database. Construct an "sqlite" structure to define
148** the state of this database and return a pointer to that structure.
149**
150** An attempt is made to initialize the in-memory data structures that
151** hold the database schema. But if this fails (because the schema file
152** is locked) then that step is deferred until the first call to
153** sqlite_exec().
154*/
155sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
156 sqlite *db;
157 int rc;
158
159 /* Allocate the sqlite data structure */
drh75897232000-05-29 14:26:00 +0000160 db = sqliteMalloc( sizeof(sqlite) );
161 if( pzErrMsg ) *pzErrMsg = 0;
162 if( db==0 ){
163 sqliteSetString(pzErrMsg, "out of memory", 0);
drhc3c2fc92000-05-31 22:58:39 +0000164 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000165 return 0;
166 }
167
168 /* Open the backend database driver */
169 db->pBe = sqliteDbbeOpen(zFilename, (mode&0222)!=0, mode!=0, pzErrMsg);
170 if( db->pBe==0 ){
171 sqliteFree(db);
172 return 0;
173 }
174
drh58b95762000-06-02 01:17:37 +0000175 /* Attempt to read the schema */
176 rc = sqliteInit(db, pzErrMsg);
177 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
178 sqlite_close(db);
179 return 0;
drh75897232000-05-29 14:26:00 +0000180 }
drh75897232000-05-29 14:26:00 +0000181 return db;
182}
183
184/*
185** Close an existing SQLite database
186*/
187void sqlite_close(sqlite *db){
188 int i;
189 sqliteDbbeClose(db->pBe);
190 for(i=0; i<N_HASH; i++){
191 Table *pNext, *pList = db->apTblHash[i];
192 db->apTblHash[i] = 0;
193 while( pList ){
194 pNext = pList->pHash;
195 pList->pHash = 0;
196 sqliteDeleteTable(db, pList);
197 pList = pNext;
198 }
199 }
200 sqliteFree(db);
201}
202
203/*
204** Return TRUE if the given SQL string ends in a semicolon.
205*/
206int sqlite_complete(const char *zSql){
207 int i;
208 int lastWasSemi = 0;
209
210 i = 0;
211 while( i>=0 && zSql[i]!=0 ){
212 int tokenType;
213 int n;
214
215 n = sqliteGetToken(&zSql[i], &tokenType);
216 switch( tokenType ){
217 case TK_SPACE:
218 case TK_COMMENT:
219 break;
220 case TK_SEMI:
221 lastWasSemi = 1;
222 break;
223 default:
224 lastWasSemi = 0;
225 break;
226 }
227 i += n;
228 }
229 return lastWasSemi;
230}
231
232/*
233** Execute SQL code
234*/
235int sqlite_exec(
236 sqlite *db, /* The database on which the SQL executes */
237 char *zSql, /* The SQL to be executed */
238 sqlite_callback xCallback, /* Invoke this callback routine */
239 void *pArg, /* First argument to xCallback() */
240 char **pzErrMsg /* Write error messages here */
241){
242 Parse sParse;
drh58b95762000-06-02 01:17:37 +0000243 int rc;
drh75897232000-05-29 14:26:00 +0000244
245 if( pzErrMsg ) *pzErrMsg = 0;
drh58b95762000-06-02 01:17:37 +0000246 if( (db->flags & SQLITE_Initialized)==0 ){
247 int rc = sqliteInit(db, pzErrMsg);
248 if( rc!=SQLITE_OK ) return rc;
249 }
drh75897232000-05-29 14:26:00 +0000250 memset(&sParse, 0, sizeof(sParse));
251 sParse.db = db;
252 sParse.xCallback = xCallback;
253 sParse.pArg = pArg;
drh58b95762000-06-02 01:17:37 +0000254 rc = sqliteRunParser(&sParse, zSql, pzErrMsg);
drhc3c2fc92000-05-31 22:58:39 +0000255 sqliteStrRealloc(pzErrMsg);
drh58b95762000-06-02 01:17:37 +0000256 return rc;
drh75897232000-05-29 14:26:00 +0000257}