blob: 4bbc75cedda9b9ae0656ba54630b184ab079eda4 [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**
29** $Id: main.c,v 1.1 2000/05/29 14:26:01 drh Exp $
30*/
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);
124 return 0;
125 }
126
127 /* Open the backend database driver */
128 db->pBe = sqliteDbbeOpen(zFilename, (mode&0222)!=0, mode!=0, pzErrMsg);
129 if( db->pBe==0 ){
130 sqliteFree(db);
131 return 0;
132 }
133
134 /* Create a virtual machine to run the initialization program. Run
135 ** the program. The delete the virtual machine.
136 */
137 azArg[0] = master_schema;
138 azArg[1] = 0;
139 sqliteOpenCb(db, 1, azArg, 0);
140 pTab = sqliteFindTable(db, MASTER_NAME);
141 if( pTab ){
142 pTab->readOnly = 1;
143 }
144 vdbe = sqliteVdbeCreate(db->pBe);
145 sqliteVdbeAddOpList(vdbe, sizeof(initProg)/sizeof(initProg[0]), initProg);
146 sqliteVdbeExec(vdbe, sqliteOpenCb, db, pzErrMsg);
147 sqliteVdbeDelete(vdbe);
148 return db;
149}
150
151/*
152** Close an existing SQLite database
153*/
154void sqlite_close(sqlite *db){
155 int i;
156 sqliteDbbeClose(db->pBe);
157 for(i=0; i<N_HASH; i++){
158 Table *pNext, *pList = db->apTblHash[i];
159 db->apTblHash[i] = 0;
160 while( pList ){
161 pNext = pList->pHash;
162 pList->pHash = 0;
163 sqliteDeleteTable(db, pList);
164 pList = pNext;
165 }
166 }
167 sqliteFree(db);
168}
169
170/*
171** Return TRUE if the given SQL string ends in a semicolon.
172*/
173int sqlite_complete(const char *zSql){
174 int i;
175 int lastWasSemi = 0;
176
177 i = 0;
178 while( i>=0 && zSql[i]!=0 ){
179 int tokenType;
180 int n;
181
182 n = sqliteGetToken(&zSql[i], &tokenType);
183 switch( tokenType ){
184 case TK_SPACE:
185 case TK_COMMENT:
186 break;
187 case TK_SEMI:
188 lastWasSemi = 1;
189 break;
190 default:
191 lastWasSemi = 0;
192 break;
193 }
194 i += n;
195 }
196 return lastWasSemi;
197}
198
199/*
200** Execute SQL code
201*/
202int sqlite_exec(
203 sqlite *db, /* The database on which the SQL executes */
204 char *zSql, /* The SQL to be executed */
205 sqlite_callback xCallback, /* Invoke this callback routine */
206 void *pArg, /* First argument to xCallback() */
207 char **pzErrMsg /* Write error messages here */
208){
209 Parse sParse;
210 int nErr;
211
212 if( pzErrMsg ) *pzErrMsg = 0;
213 memset(&sParse, 0, sizeof(sParse));
214 sParse.db = db;
215 sParse.xCallback = xCallback;
216 sParse.pArg = pArg;
217 nErr = sqliteRunParser(&sParse, zSql, pzErrMsg);
218 return nErr;
219}