blob: 4deac6de0dc3f2261aaf9d3a82f56fdb1dd3c799 [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**
drhc3c2fc92000-05-31 22:58:39 +000029** $Id: main.c,v 1.2 2000/05/31 22:58: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;
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/*
54** Open a new SQLite database. Construct an "sqlite" structure to define
55** the state of this database and return a pointer to that structure.
56*/
57sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
58 sqlite *db;
59 Vdbe *vdbe;
60 Table *pTab;
61 char *azArg[2];
62 static char master_schema[] =
63 "CREATE TABLE " MASTER_NAME " (\n"
64 " type text,\n"
65 " name text,\n"
66 " tbl_name text,\n"
67 " sql text\n"
68 ")"
69 ;
70
71 /* The following program is used to initialize the internal
72 ** structure holding the tables and indexes of the database.
73 ** The database contains a special table named "sqlite_master"
74 ** defined as follows:
75 **
76 ** CREATE TABLE sqlite_master (
77 ** type text, -- Either "table" or "index"
78 ** name text, -- Name of table or index
79 ** tbl_name text, -- Associated table
80 ** sql text -- The CREATE statement for this object
81 ** );
82 **
83 ** The sqlite_master table contains a single entry for each table
84 ** and each index. The "type" field tells whether the entry is
85 ** a table or index. The "name" field is the name of the object.
86 ** The "tbl_name" is the name of the associated table. For tables,
87 ** the tbl_name field is always the same as name. For indices, the
88 ** tbl_name field contains the name of the table that the index
89 ** indexes. Finally, the sql field contains the complete text of
90 ** the CREATE TABLE or CREATE INDEX statement that originally created
91 ** the table or index.
92 **
93 ** The following program invokes its callback on the SQL for each
94 ** table then goes back and invokes the callback on the
95 ** SQL for each index. The callback will invoke the
96 ** parser to build the internal representation of the
97 ** database scheme.
98 */
99 static VdbeOp initProg[] = {
100 { OP_Open, 0, 0, MASTER_NAME},
101 { OP_Next, 0, 8, 0}, /* 1 */
102 { OP_Field, 0, 0, 0},
103 { OP_String, 0, 0, "table"},
104 { OP_Ne, 0, 1, 0},
105 { OP_Field, 0, 3, 0},
106 { OP_Callback, 1, 0, 0},
107 { OP_Goto, 0, 1, 0},
108 { OP_Rewind, 0, 0, 0}, /* 8 */
109 { OP_Next, 0, 16, 0}, /* 9 */
110 { OP_Field, 0, 0, 0},
111 { OP_String, 0, 0, "index"},
112 { OP_Ne, 0, 9, 0},
113 { OP_Field, 0, 3, 0},
114 { OP_Callback, 1, 0, 0},
115 { OP_Goto, 0, 9, 0},
116 { OP_Halt, 0, 0, 0}, /* 16 */
117 };
118
119 /* Allocate space to hold the main database structure */
120 db = sqliteMalloc( sizeof(sqlite) );
121 if( pzErrMsg ) *pzErrMsg = 0;
122 if( db==0 ){
123 sqliteSetString(pzErrMsg, "out of memory", 0);
drhc3c2fc92000-05-31 22:58:39 +0000124 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000125 return 0;
126 }
127
128 /* Open the backend database driver */
129 db->pBe = sqliteDbbeOpen(zFilename, (mode&0222)!=0, mode!=0, pzErrMsg);
130 if( db->pBe==0 ){
131 sqliteFree(db);
132 return 0;
133 }
134
135 /* Create a virtual machine to run the initialization program. Run
136 ** the program. The delete the virtual machine.
137 */
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 vdbe = sqliteVdbeCreate(db->pBe);
146 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
147 sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg);
148 sqliteVdbeDelete(vdbe);
149 return db;
150}
151
152/*
153** Close an existing SQLite database
154*/
155void sqlite_close(sqlite *db){
156 int i;
157 sqliteDbbeClose(db->pBe);
158 for(i=0; i<N_HASH; i++){
159 Table *pNext, *pList = db->apTblHash[i];
160 db->apTblHash[i] = 0;
161 while( pList ){
162 pNext = pList->pHash;
163 pList->pHash = 0;
164 sqliteDeleteTable(db, pList);
165 pList = pNext;
166 }
167 }
168 sqliteFree(db);
169}
170
171/*
172** Return TRUE if the given SQL string ends in a semicolon.
173*/
174int sqlite_complete(const char *zSql){
175 int i;
176 int lastWasSemi = 0;
177
178 i = 0;
179 while( i>=0 && zSql[i]!=0 ){
180 int tokenType;
181 int n;
182
183 n = sqliteGetToken(&zSql[i], &tokenType);
184 switch( tokenType ){
185 case TK_SPACE:
186 case TK_COMMENT:
187 break;
188 case TK_SEMI:
189 lastWasSemi = 1;
190 break;
191 default:
192 lastWasSemi = 0;
193 break;
194 }
195 i += n;
196 }
197 return lastWasSemi;
198}
199
200/*
201** Execute SQL code
202*/
203int sqlite_exec(
204 sqlite *db, /* The database on which the SQL executes */
205 char *zSql, /* The SQL to be executed */
206 sqlite_callback xCallback, /* Invoke this callback routine */
207 void *pArg, /* First argument to xCallback() */
208 char **pzErrMsg /* Write error messages here */
209){
210 Parse sParse;
211 int nErr;
212
213 if( pzErrMsg ) *pzErrMsg = 0;
214 memset(&sParse, 0, sizeof(sParse));
215 sParse.db = db;
216 sParse.xCallback = xCallback;
217 sParse.pArg = pArg;
218 nErr = sqliteRunParser(&sParse, zSql, pzErrMsg);
drhc3c2fc92000-05-31 22:58:39 +0000219 sqliteStrRealloc(pzErrMsg);
drh75897232000-05-29 14:26:00 +0000220 return nErr;
221}