2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A** 2003 April 6
2N/A**
2N/A** The author disclaims copyright to this source code. In place of
2N/A** a legal notice, here is a blessing:
2N/A**
2N/A** May you do good and not evil.
2N/A** May you find forgiveness for yourself and forgive others.
2N/A** May you share freely, never taking more than you give.
2N/A**
2N/A*************************************************************************
2N/A** This file contains code used to implement the VACUUM command.
2N/A**
2N/A** Most of the code in this file may be omitted by defining the
2N/A** SQLITE_OMIT_VACUUM macro.
2N/A**
2N/A** $Id: vacuum.c,v 1.13.2.2 2004/06/04 19:07:54 drh Exp $
2N/A*/
2N/A#include "sqliteInt.h"
2N/A#include "os.h"
2N/A
2N/A/*
2N/A** A structure for holding a dynamic string - a string that can grow
2N/A** without bound.
2N/A*/
2N/Atypedef struct dynStr dynStr;
2N/Astruct dynStr {
2N/A char *z; /* Text of the string in space obtained from sqliteMalloc() */
2N/A int nAlloc; /* Amount of space allocated to z[] */
2N/A int nUsed; /* Next unused slot in z[] */
2N/A};
2N/A
2N/A/*
2N/A** A structure that holds the vacuum context
2N/A*/
2N/Atypedef struct vacuumStruct vacuumStruct;
2N/Astruct vacuumStruct {
2N/A sqlite *dbOld; /* Original database */
2N/A sqlite *dbNew; /* New database */
2N/A char **pzErrMsg; /* Write errors here */
2N/A int rc; /* Set to non-zero on an error */
2N/A const char *zTable; /* Name of a table being copied */
2N/A const char *zPragma; /* Pragma to execute with results */
2N/A dynStr s1, s2; /* Two dynamic strings */
2N/A};
2N/A
2N/A#if !defined(SQLITE_OMIT_VACUUM) || SQLITE_OMIT_VACUUM
2N/A/*
2N/A** Append text to a dynamic string
2N/A*/
2N/Astatic void appendText(dynStr *p, const char *zText, int nText){
2N/A if( nText<0 ) nText = strlen(zText);
2N/A if( p->z==0 || p->nUsed + nText + 1 >= p->nAlloc ){
2N/A char *zNew;
2N/A p->nAlloc = p->nUsed + nText + 1000;
2N/A zNew = sqliteRealloc(p->z, p->nAlloc);
2N/A if( zNew==0 ){
2N/A sqliteFree(p->z);
2N/A memset(p, 0, sizeof(*p));
2N/A return;
2N/A }
2N/A p->z = zNew;
2N/A }
2N/A memcpy(&p->z[p->nUsed], zText, nText+1);
2N/A p->nUsed += nText;
2N/A}
2N/A
2N/A/*
2N/A** Append text to a dynamic string, having first put the text in quotes.
2N/A*/
2N/Astatic void appendQuoted(dynStr *p, const char *zText){
2N/A int i, j;
2N/A appendText(p, "'", 1);
2N/A for(i=j=0; zText[i]; i++){
2N/A if( zText[i]=='\'' ){
2N/A appendText(p, &zText[j], i-j+1);
2N/A j = i + 1;
2N/A appendText(p, "'", 1);
2N/A }
2N/A }
2N/A if( j<i ){
2N/A appendText(p, &zText[j], i-j);
2N/A }
2N/A appendText(p, "'", 1);
2N/A}
2N/A
2N/A/*
2N/A** Execute statements of SQL. If an error occurs, write the error
2N/A** message into *pzErrMsg and return non-zero.
2N/A*/
2N/Astatic int execsql(char **pzErrMsg, sqlite *db, const char *zSql){
2N/A char *zErrMsg = 0;
2N/A int rc;
2N/A
2N/A /* printf("***** executing *****\n%s\n", zSql); */
2N/A rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
2N/A if( zErrMsg ){
2N/A sqliteSetString(pzErrMsg, zErrMsg, (char*)0);
2N/A sqlite_freemem(zErrMsg);
2N/A }
2N/A return rc;
2N/A}
2N/A
2N/A/*
2N/A** This is the second stage callback. Each invocation contains all the
2N/A** data for a single row of a single table in the original database. This
2N/A** routine must write that information into the new database.
2N/A*/
2N/Astatic int vacuumCallback2(void *pArg, int argc, char **argv, char **NotUsed){
2N/A vacuumStruct *p = (vacuumStruct*)pArg;
2N/A const char *zSep = "(";
2N/A int i;
2N/A
2N/A if( argv==0 ) return 0;
2N/A p->s2.nUsed = 0;
2N/A appendText(&p->s2, "INSERT INTO ", -1);
2N/A appendQuoted(&p->s2, p->zTable);
2N/A appendText(&p->s2, " VALUES", -1);
2N/A for(i=0; i<argc; i++){
2N/A appendText(&p->s2, zSep, 1);
2N/A zSep = ",";
2N/A if( argv[i]==0 ){
2N/A appendText(&p->s2, "NULL", 4);
2N/A }else{
2N/A appendQuoted(&p->s2, argv[i]);
2N/A }
2N/A }
2N/A appendText(&p->s2,")", 1);
2N/A p->rc = execsql(p->pzErrMsg, p->dbNew, p->s2.z);
2N/A return p->rc;
2N/A}
2N/A
2N/A/*
2N/A** This is the first stage callback. Each invocation contains three
2N/A** arguments where are taken from the SQLITE_MASTER table of the original
2N/A** database: (1) the entry type, (2) the entry name, and (3) the SQL for
2N/A** the entry. In all cases, execute the SQL of the third argument.
2N/A** For tables, run a query to select all entries in that table and
2N/A** transfer them to the second-stage callback.
2N/A*/
2N/Astatic int vacuumCallback1(void *pArg, int argc, char **argv, char **NotUsed){
2N/A vacuumStruct *p = (vacuumStruct*)pArg;
2N/A int rc = 0;
2N/A assert( argc==3 );
2N/A if( argv==0 ) return 0;
2N/A assert( argv[0]!=0 );
2N/A assert( argv[1]!=0 );
2N/A assert( argv[2]!=0 );
2N/A rc = execsql(p->pzErrMsg, p->dbNew, argv[2]);
2N/A if( rc==SQLITE_OK && strcmp(argv[0],"table")==0 ){
2N/A char *zErrMsg = 0;
2N/A p->s1.nUsed = 0;
2N/A appendText(&p->s1, "SELECT * FROM ", -1);
2N/A appendQuoted(&p->s1, argv[1]);
2N/A p->zTable = argv[1];
2N/A rc = sqlite_exec(p->dbOld, p->s1.z, vacuumCallback2, p, &zErrMsg);
2N/A if( zErrMsg ){
2N/A sqliteSetString(p->pzErrMsg, zErrMsg, (char*)0);
2N/A sqlite_freemem(zErrMsg);
2N/A }
2N/A }
2N/A if( rc!=SQLITE_ABORT ) p->rc = rc;
2N/A return rc;
2N/A}
2N/A
2N/A/*
2N/A** This callback is used to transfer PRAGMA settings from one database
2N/A** to the other. The value in argv[0] should be passed to a pragma
2N/A** identified by ((vacuumStruct*)pArg)->zPragma.
2N/A*/
2N/Astatic int vacuumCallback3(void *pArg, int argc, char **argv, char **NotUsed){
2N/A vacuumStruct *p = (vacuumStruct*)pArg;
2N/A char zBuf[200];
2N/A assert( argc==1 );
2N/A if( argv==0 ) return 0;
2N/A assert( argv[0]!=0 );
2N/A assert( strlen(p->zPragma)<100 );
2N/A assert( strlen(argv[0])<30 );
2N/A sprintf(zBuf,"PRAGMA %s=%s;", p->zPragma, argv[0]);
2N/A p->rc = execsql(p->pzErrMsg, p->dbNew, zBuf);
2N/A return p->rc;
2N/A}
2N/A
2N/A/*
2N/A** Generate a random name of 20 character in length.
2N/A*/
2N/Astatic void randomName(unsigned char *zBuf){
2N/A static const unsigned char zChars[] =
2N/A "abcdefghijklmnopqrstuvwxyz"
2N/A "0123456789";
2N/A int i;
2N/A sqliteRandomness(20, zBuf);
2N/A for(i=0; i<20; i++){
2N/A zBuf[i] = zChars[ zBuf[i]%(sizeof(zChars)-1) ];
2N/A }
2N/A}
2N/A#endif
2N/A
2N/A/*
2N/A** The non-standard VACUUM command is used to clean up the database,
2N/A** collapse free space, etc. It is modelled after the VACUUM command
2N/A** in PostgreSQL.
2N/A**
2N/A** In version 1.0.x of SQLite, the VACUUM command would call
2N/A** gdbm_reorganize() on all the database tables. But beginning
2N/A** with 2.0.0, SQLite no longer uses GDBM so this command has
2N/A** become a no-op.
2N/A*/
2N/Avoid sqliteVacuum(Parse *pParse, Token *pTableName){
2N/A Vdbe *v = sqliteGetVdbe(pParse);
2N/A sqliteVdbeAddOp(v, OP_Vacuum, 0, 0);
2N/A return;
2N/A}
2N/A
2N/A/*
2N/A** This routine implements the OP_Vacuum opcode of the VDBE.
2N/A*/
2N/Aint sqliteRunVacuum(char **pzErrMsg, sqlite *db){
2N/A#if !defined(SQLITE_OMIT_VACUUM) || SQLITE_OMIT_VACUUM
2N/A const char *zFilename; /* full pathname of the database file */
2N/A int nFilename; /* number of characters in zFilename[] */
2N/A char *zTemp = 0; /* a temporary file in same directory as zFilename */
2N/A sqlite *dbNew = 0; /* The new vacuumed database */
2N/A int rc = SQLITE_OK; /* Return code from service routines */
2N/A int i; /* Loop counter */
2N/A char *zErrMsg; /* Error message */
2N/A vacuumStruct sVac; /* Information passed to callbacks */
2N/A
2N/A /* These are all of the pragmas that need to be transferred over
2N/A ** to the new database */
2N/A static const char *zPragma[] = {
2N/A "default_synchronous",
2N/A "default_cache_size",
2N/A /* "default_temp_store", */
2N/A };
2N/A
2N/A if( db->flags & SQLITE_InTrans ){
2N/A sqliteSetString(pzErrMsg, "cannot VACUUM from within a transaction",
2N/A (char*)0);
2N/A return SQLITE_ERROR;
2N/A }
2N/A if( db->flags & SQLITE_Interrupt ){
2N/A return SQLITE_INTERRUPT;
2N/A }
2N/A memset(&sVac, 0, sizeof(sVac));
2N/A
2N/A /* Get the full pathname of the database file and create two
2N/A ** temporary filenames in the same directory as the original file.
2N/A */
2N/A zFilename = sqliteBtreeGetFilename(db->aDb[0].pBt);
2N/A if( zFilename==0 ){
2N/A /* This only happens with the in-memory database. VACUUM is a no-op
2N/A ** there, so just return */
2N/A return SQLITE_OK;
2N/A }
2N/A nFilename = strlen(zFilename);
2N/A zTemp = sqliteMalloc( nFilename+100 );
2N/A if( zTemp==0 ) return SQLITE_NOMEM;
2N/A strcpy(zTemp, zFilename);
2N/A for(i=0; i<10; i++){
2N/A zTemp[nFilename] = '-';
2N/A randomName((unsigned char*)&zTemp[nFilename+1]);
2N/A if( !sqliteOsFileExists(zTemp) ) break;
2N/A }
2N/A if( i>=10 ){
2N/A sqliteSetString(pzErrMsg, "unable to create a temporary database file "
2N/A "in the same directory as the original database", (char*)0);
2N/A goto end_of_vacuum;
2N/A }
2N/A
2N/A
2N/A dbNew = sqlite_open(zTemp, 0, &zErrMsg);
2N/A if( dbNew==0 ){
2N/A sqliteSetString(pzErrMsg, "unable to open a temporary database at ",
2N/A zTemp, " - ", zErrMsg, (char*)0);
2N/A goto end_of_vacuum;
2N/A }
2N/A if( (rc = execsql(pzErrMsg, db, "BEGIN"))!=0 ) goto end_of_vacuum;
2N/A if( (rc = execsql(pzErrMsg, dbNew, "PRAGMA synchronous=off; BEGIN"))!=0 ){
2N/A goto end_of_vacuum;
2N/A }
2N/A
2N/A sVac.dbOld = db;
2N/A sVac.dbNew = dbNew;
2N/A sVac.pzErrMsg = pzErrMsg;
2N/A for(i=0; rc==SQLITE_OK && i<sizeof(zPragma)/sizeof(zPragma[0]); i++){
2N/A char zBuf[200];
2N/A assert( strlen(zPragma[i])<100 );
2N/A sprintf(zBuf, "PRAGMA %s;", zPragma[i]);
2N/A sVac.zPragma = zPragma[i];
2N/A rc = sqlite_exec(db, zBuf, vacuumCallback3, &sVac, &zErrMsg);
2N/A }
2N/A if( rc==SQLITE_OK ){
2N/A rc = sqlite_exec(db,
2N/A "SELECT type, name, sql FROM sqlite_master "
2N/A "WHERE sql NOT NULL AND type!='view' "
2N/A "UNION ALL "
2N/A "SELECT type, name, sql FROM sqlite_master "
2N/A "WHERE sql NOT NULL AND type=='view'",
2N/A vacuumCallback1, &sVac, &zErrMsg);
2N/A }
2N/A if( rc==SQLITE_OK ){
2N/A rc = sqliteBtreeCopyFile(db->aDb[0].pBt, dbNew->aDb[0].pBt);
2N/A sqlite_exec(db, "COMMIT", 0, 0, 0);
2N/A sqliteResetInternalSchema(db, 0);
2N/A }
2N/A
2N/Aend_of_vacuum:
2N/A if( rc && zErrMsg!=0 ){
2N/A sqliteSetString(pzErrMsg, "unable to vacuum database - ",
2N/A zErrMsg, (char*)0);
2N/A }
2N/A sqlite_exec(db, "ROLLBACK", 0, 0, 0);
2N/A if( (dbNew && (dbNew->flags & SQLITE_Interrupt))
2N/A || (db->flags & SQLITE_Interrupt) ){
2N/A rc = SQLITE_INTERRUPT;
2N/A }
2N/A if( dbNew ) sqlite_close(dbNew);
2N/A sqliteOsDelete(zTemp);
2N/A sqliteFree(zTemp);
2N/A sqliteFree(sVac.s1.z);
2N/A sqliteFree(sVac.s2.z);
2N/A if( zErrMsg ) sqlite_freemem(zErrMsg);
2N/A if( rc==SQLITE_ABORT && sVac.rc!=SQLITE_INTERRUPT ) sVac.rc = SQLITE_ERROR;
2N/A return sVac.rc;
2N/A#endif
2N/A}