File Coverage

vacuum.c
Criterion Covered Total %
statement 0 137 0.0
branch 0 82 0.0
condition n/a
subroutine n/a
pod n/a
total 0 219 0.0


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