1
2 #pragma ident "%Z%%M% %I% %E% SMI"
3
4 /*
5 ** 2003 April 6
6 **
7 ** The author disclaims copyright to this source code. In place of
8 ** a legal notice, here is a blessing:
9 **
10 ** May you do good and not evil.
11 ** May you find forgiveness for yourself and forgive others.
12 ** May you share freely, never taking more than you give.
13 **
14 *************************************************************************
15 ** This file contains code used to implement the VACUUM command.
16 **
17 ** Most of the code in this file may be omitted by defining the
18 ** SQLITE_OMIT_VACUUM macro.
19 **
20 ** $Id: vacuum.c,v 1.13.2.2 2004/06/04 19:07:54 drh Exp $
21 */
22 #include "sqliteInt.h"
23 #include "os.h"
24
25 /*
26 ** A structure for holding a dynamic string - a string that can grow
27 ** without bound.
28 */
29 typedef struct dynStr dynStr;
30 struct dynStr {
31 char *z; /* Text of the string in space obtained from sqliteMalloc() */
32 int nAlloc; /* Amount of space allocated to z[] */
33 int nUsed; /* Next unused slot in z[] */
34 };
35
36 /*
37 ** A structure that holds the vacuum context
38 */
39 typedef struct vacuumStruct vacuumStruct;
40 struct vacuumStruct {
41 sqlite *dbOld; /* Original database */
42 sqlite *dbNew; /* New database */
43 char **pzErrMsg; /* Write errors here */
44 int rc; /* Set to non-zero on an error */
45 const char *zTable; /* Name of a table being copied */
46 const char *zPragma; /* Pragma to execute with results */
47 dynStr s1, s2; /* Two dynamic strings */
48 };
49
50 #if !defined(SQLITE_OMIT_VACUUM) || SQLITE_OMIT_VACUUM
51 /*
52 ** Append text to a dynamic string
53 */
appendText(dynStr * p,const char * zText,int nText)54 static void appendText(dynStr *p, const char *zText, int nText){
55 if( nText<0 ) nText = strlen(zText);
56 if( p->z==0 || p->nUsed + nText + 1 >= p->nAlloc ){
57 char *zNew;
58 p->nAlloc = p->nUsed + nText + 1000;
59 zNew = sqliteRealloc(p->z, p->nAlloc);
60 if( zNew==0 ){
61 sqliteFree(p->z);
62 memset(p, 0, sizeof(*p));
63 return;
64 }
65 p->z = zNew;
66 }
67 memcpy(&p->z[p->nUsed], zText, nText+1);
68 p->nUsed += nText;
69 }
70
71 /*
72 ** Append text to a dynamic string, having first put the text in quotes.
73 */
appendQuoted(dynStr * p,const char * zText)74 static void appendQuoted(dynStr *p, const char *zText){
75 int i, j;
76 appendText(p, "'", 1);
77 for(i=j=0; zText[i]; i++){
78 if( zText[i]=='\'' ){
79 appendText(p, &zText[j], i-j+1);
80 j = i + 1;
81 appendText(p, "'", 1);
82 }
83 }
84 if( j<i ){
85 appendText(p, &zText[j], i-j);
86 }
87 appendText(p, "'", 1);
88 }
89
90 /*
91 ** Execute statements of SQL. If an error occurs, write the error
92 ** message into *pzErrMsg and return non-zero.
93 */
execsql(char ** pzErrMsg,sqlite * db,const char * zSql)94 static int execsql(char **pzErrMsg, sqlite *db, const char *zSql){
95 char *zErrMsg = 0;
96 int rc;
97
98 /* printf("***** executing *****\n%s\n", zSql); */
99 rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
100 if( zErrMsg ){
101 sqliteSetString(pzErrMsg, zErrMsg, (char*)0);
102 sqlite_freemem(zErrMsg);
103 }
104 return rc;
105 }
106
107 /*
108 ** This is the second stage callback. Each invocation contains all the
109 ** data for a single row of a single table in the original database. This
110 ** routine must write that information into the new database.
111 */
vacuumCallback2(void * pArg,int argc,char ** argv,char ** NotUsed)112 static int vacuumCallback2(void *pArg, int argc, char **argv, char **NotUsed){
113 vacuumStruct *p = (vacuumStruct*)pArg;
114 const char *zSep = "(";
115 int i;
116
117 if( argv==0 ) return 0;
118 p->s2.nUsed = 0;
119 appendText(&p->s2, "INSERT INTO ", -1);
120 appendQuoted(&p->s2, p->zTable);
121 appendText(&p->s2, " VALUES", -1);
122 for(i=0; i<argc; i++){
123 appendText(&p->s2, zSep, 1);
124 zSep = ",";
125 if( argv[i]==0 ){
126 appendText(&p->s2, "NULL", 4);
127 }else{
128 appendQuoted(&p->s2, argv[i]);
129 }
130 }
131 appendText(&p->s2,")", 1);
132 p->rc = execsql(p->pzErrMsg, p->dbNew, p->s2.z);
133 return p->rc;
134 }
135
136 /*
137 ** This is the first stage callback. Each invocation contains three
138 ** arguments where are taken from the SQLITE_MASTER table of the original
139 ** database: (1) the entry type, (2) the entry name, and (3) the SQL for
140 ** the entry. In all cases, execute the SQL of the third argument.
141 ** For tables, run a query to select all entries in that table and
142 ** transfer them to the second-stage callback.
143 */
vacuumCallback1(void * pArg,int argc,char ** argv,char ** NotUsed)144 static int vacuumCallback1(void *pArg, int argc, char **argv, char **NotUsed){
145 vacuumStruct *p = (vacuumStruct*)pArg;
146 int rc = 0;
147 assert( argc==3 );
148 if( argv==0 ) return 0;
149 assert( argv[0]!=0 );
150 assert( argv[1]!=0 );
151 assert( argv[2]!=0 );
152 rc = execsql(p->pzErrMsg, p->dbNew, argv[2]);
153 if( rc==SQLITE_OK && strcmp(argv[0],"table")==0 ){
154 char *zErrMsg = 0;
155 p->s1.nUsed = 0;
156 appendText(&p->s1, "SELECT * FROM ", -1);
157 appendQuoted(&p->s1, argv[1]);
158 p->zTable = argv[1];
159 rc = sqlite_exec(p->dbOld, p->s1.z, vacuumCallback2, p, &zErrMsg);
160 if( zErrMsg ){
161 sqliteSetString(p->pzErrMsg, zErrMsg, (char*)0);
162 sqlite_freemem(zErrMsg);
163 }
164 }
165 if( rc!=SQLITE_ABORT ) p->rc = rc;
166 return rc;
167 }
168
169 /*
170 ** This callback is used to transfer PRAGMA settings from one database
171 ** to the other. The value in argv[0] should be passed to a pragma
172 ** identified by ((vacuumStruct*)pArg)->zPragma.
173 */
vacuumCallback3(void * pArg,int argc,char ** argv,char ** NotUsed)174 static int vacuumCallback3(void *pArg, int argc, char **argv, char **NotUsed){
175 vacuumStruct *p = (vacuumStruct*)pArg;
176 char zBuf[200];
177 assert( argc==1 );
178 if( argv==0 ) return 0;
179 assert( argv[0]!=0 );
180 assert( strlen(p->zPragma)<100 );
181 assert( strlen(argv[0])<30 );
182 sprintf(zBuf,"PRAGMA %s=%s;", p->zPragma, argv[0]);
183 p->rc = execsql(p->pzErrMsg, p->dbNew, zBuf);
184 return p->rc;
185 }
186
187 /*
188 ** Generate a random name of 20 character in length.
189 */
randomName(unsigned char * zBuf)190 static void randomName(unsigned char *zBuf){
191 static const unsigned char zChars[] =
192 "abcdefghijklmnopqrstuvwxyz"
193 "0123456789";
194 int i;
195 sqliteRandomness(20, zBuf);
196 for(i=0; i<20; i++){
197 zBuf[i] = zChars[ zBuf[i]%(sizeof(zChars)-1) ];
198 }
199 }
200 #endif
201
202 /*
203 ** The non-standard VACUUM command is used to clean up the database,
204 ** collapse free space, etc. It is modelled after the VACUUM command
205 ** in PostgreSQL.
206 **
207 ** In version 1.0.x of SQLite, the VACUUM command would call
208 ** gdbm_reorganize() on all the database tables. But beginning
209 ** with 2.0.0, SQLite no longer uses GDBM so this command has
210 ** become a no-op.
211 */
sqliteVacuum(Parse * pParse,Token * pTableName)212 void sqliteVacuum(Parse *pParse, Token *pTableName){
213 Vdbe *v = sqliteGetVdbe(pParse);
214 sqliteVdbeAddOp(v, OP_Vacuum, 0, 0);
215 return;
216 }
217
218 /*
219 ** This routine implements the OP_Vacuum opcode of the VDBE.
220 */
sqliteRunVacuum(char ** pzErrMsg,sqlite * db)221 int sqliteRunVacuum(char **pzErrMsg, sqlite *db){
222 #if !defined(SQLITE_OMIT_VACUUM) || SQLITE_OMIT_VACUUM
223 const char *zFilename; /* full pathname of the database file */
224 int nFilename; /* number of characters in zFilename[] */
225 char *zTemp = 0; /* a temporary file in same directory as zFilename */
226 sqlite *dbNew = 0; /* The new vacuumed database */
227 int rc = SQLITE_OK; /* Return code from service routines */
228 int i; /* Loop counter */
229 char *zErrMsg; /* Error message */
230 vacuumStruct sVac; /* Information passed to callbacks */
231
232 /* These are all of the pragmas that need to be transferred over
233 ** to the new database */
234 static const char *zPragma[] = {
235 "default_synchronous",
236 "default_cache_size",
237 /* "default_temp_store", */
238 };
239
240 if( db->flags & SQLITE_InTrans ){
241 sqliteSetString(pzErrMsg, "cannot VACUUM from within a transaction",
242 (char*)0);
243 return SQLITE_ERROR;
244 }
245 if( db->flags & SQLITE_Interrupt ){
246 return SQLITE_INTERRUPT;
247 }
248 memset(&sVac, 0, sizeof(sVac));
249
250 /* Get the full pathname of the database file and create two
251 ** temporary filenames in the same directory as the original file.
252 */
253 zFilename = sqliteBtreeGetFilename(db->aDb[0].pBt);
254 if( zFilename==0 ){
255 /* This only happens with the in-memory database. VACUUM is a no-op
256 ** there, so just return */
257 return SQLITE_OK;
258 }
259 nFilename = strlen(zFilename);
260 zTemp = sqliteMalloc( nFilename+100 );
261 if( zTemp==0 ) return SQLITE_NOMEM;
262 strcpy(zTemp, zFilename);
263 for(i=0; i<10; i++){
264 zTemp[nFilename] = '-';
265 randomName((unsigned char*)&zTemp[nFilename+1]);
266 if( !sqliteOsFileExists(zTemp) ) break;
267 }
268 if( i>=10 ){
269 sqliteSetString(pzErrMsg, "unable to create a temporary database file "
270 "in the same directory as the original database", (char*)0);
271 goto end_of_vacuum;
272 }
273
274
275 dbNew = sqlite_open(zTemp, 0, &zErrMsg);
276 if( dbNew==0 ){
277 sqliteSetString(pzErrMsg, "unable to open a temporary database at ",
278 zTemp, " - ", zErrMsg, (char*)0);
279 goto end_of_vacuum;
280 }
281 if( (rc = execsql(pzErrMsg, db, "BEGIN"))!=0 ) goto end_of_vacuum;
282 if( (rc = execsql(pzErrMsg, dbNew, "PRAGMA synchronous=off; BEGIN"))!=0 ){
283 goto end_of_vacuum;
284 }
285
286 sVac.dbOld = db;
287 sVac.dbNew = dbNew;
288 sVac.pzErrMsg = pzErrMsg;
289 for(i=0; rc==SQLITE_OK && i<sizeof(zPragma)/sizeof(zPragma[0]); i++){
290 char zBuf[200];
291 assert( strlen(zPragma[i])<100 );
292 sprintf(zBuf, "PRAGMA %s;", zPragma[i]);
293 sVac.zPragma = zPragma[i];
294 rc = sqlite_exec(db, zBuf, vacuumCallback3, &sVac, &zErrMsg);
295 }
296 if( rc==SQLITE_OK ){
297 rc = sqlite_exec(db,
298 "SELECT type, name, sql FROM sqlite_master "
299 "WHERE sql NOT NULL AND type!='view' "
300 "UNION ALL "
301 "SELECT type, name, sql FROM sqlite_master "
302 "WHERE sql NOT NULL AND type=='view'",
303 vacuumCallback1, &sVac, &zErrMsg);
304 }
305 if( rc==SQLITE_OK ){
306 rc = sqliteBtreeCopyFile(db->aDb[0].pBt, dbNew->aDb[0].pBt);
307 sqlite_exec(db, "COMMIT", 0, 0, 0);
308 sqliteResetInternalSchema(db, 0);
309 }
310
311 end_of_vacuum:
312 if( rc && zErrMsg!=0 ){
313 sqliteSetString(pzErrMsg, "unable to vacuum database - ",
314 zErrMsg, (char*)0);
315 }
316 sqlite_exec(db, "ROLLBACK", 0, 0, 0);
317 if( (dbNew && (dbNew->flags & SQLITE_Interrupt))
318 || (db->flags & SQLITE_Interrupt) ){
319 rc = SQLITE_INTERRUPT;
320 }
321 if( dbNew ) sqlite_close(dbNew);
322 sqliteOsDelete(zTemp);
323 sqliteFree(zTemp);
324 sqliteFree(sVac.s1.z);
325 sqliteFree(sVac.s2.z);
326 if( zErrMsg ) sqlite_freemem(zErrMsg);
327 if( rc==SQLITE_ABORT && sVac.rc!=SQLITE_INTERRUPT ) sVac.rc = SQLITE_ERROR;
328 return sVac.rc;
329 #endif
330 }
331