Index: test/wordcount.c ================================================================== --- test/wordcount.c +++ test/wordcount.c @@ -16,14 +16,20 @@ ** --without-rowid Use a WITHOUT ROWID table to store the words. ** --insert Use INSERT mode (the default) ** --replace Use REPLACE mode ** --select Use SELECT mode ** --update Use UPDATE mode +** --delete Use DELETE mode ** --nocase Add the NOCASE collating sequence to the words. ** --trace Enable sqlite3_trace() output. ** --summary Show summary information on the collected data. ** --stats Show sqlite3_status() results at the end. +** --pagesize NNN Use a page size of NNN +** --cachesize NNN Use a cache size of NNN +** --commit NNN Commit after every NNN operations +** --nosync Use PRAGMA synchronous=OFF +** --journal MMMM Use PRAGMA journal_mode=MMMM ** ** Modes: ** ** Insert mode means: ** (1) INSERT OR IGNORE INTO wordcount VALUES($new,1) @@ -35,15 +41,24 @@ ** ** Replace mode means: ** (1) REPLACE INTO wordcount ** VALUES($new,ifnull((SELECT cnt FROM wordcount WHERE word=$new),0)+1); ** -** Select mode modes: -** (1) SELECT 1 FROM wordcount WHERE word=$newword +** Select mode means: +** (1) SELECT 1 FROM wordcount WHERE word=$new ** (2) INSERT INTO wordcount VALUES($new,1) -- if (1) returns nothing ** (3) UPDATE wordcount SET cnt=cnt+1 WHERE word=$new --if (1) return TRUE ** +** Delete mode means: +** (1) DELETE FROM wordcount WHERE word=$new +** +** Note that delete mode is only useful for preexisting databases. The +** wordcount table is created using IF NOT EXISTS so this utility can be +** run multiple times on the same database file. The --without-rowid, +** --nocase, and --pagesize parameters are only effective when creating +** a new database and are harmless no-ops on preexisting databases. +** ****************************************************************************** ** ** Compile as follows: ** ** gcc -I. wordcount.c sqlite3.c -ldl -lpthreads @@ -91,10 +106,11 @@ /* Define operating modes */ #define MODE_INSERT 0 #define MODE_REPLACE 1 #define MODE_SELECT 2 #define MODE_UPDATE 3 +#define MODE_DELETE 4 int main(int argc, char **argv){ const char *zFileToRead = 0; /* Input file. NULL for stdin */ const char *zDbName = 0; /* Name of the database file to create */ int useWithoutRowid = 0; /* True for --without-rowid */ @@ -101,16 +117,23 @@ int iMode = MODE_INSERT; /* One of MODE_xxxxx */ int useNocase = 0; /* True for --nocase */ int doTrace = 0; /* True for --trace */ int showStats = 0; /* True for --stats */ int showSummary = 0; /* True for --summary */ + int cacheSize = 0; /* Desired cache size. 0 means default */ + int pageSize = 0; /* Desired page size. 0 means default */ + int commitInterval = 0; /* How often to commit. 0 means never */ + int noSync = 0; /* True for --nosync */ + const char *zJMode = 0; /* Journal mode */ + int nOp = 0; /* Operation counter */ int i, j; /* Loop counters */ sqlite3 *db; /* The SQLite database connection */ char *zSql; /* Constructed SQL statement */ sqlite3_stmt *pInsert = 0; /* The INSERT statement */ sqlite3_stmt *pUpdate = 0; /* The UPDATE statement */ sqlite3_stmt *pSelect = 0; /* The SELECT statement */ + sqlite3_stmt *pDelete = 0; /* The DELETE statement */ FILE *in; /* The open input file */ int rc; /* Return code from an SQLite interface */ int iCur, iHiwtr; /* Statistics values, current and "highwater" */ char zInput[2000]; /* A single line of input */ @@ -127,18 +150,33 @@ iMode = MODE_SELECT; }else if( strcmp(z,"insert")==0 ){ iMode = MODE_INSERT; }else if( strcmp(z,"update")==0 ){ iMode = MODE_UPDATE; + }else if( strcmp(z,"delete")==0 ){ + iMode = MODE_DELETE; }else if( strcmp(z,"nocase")==0 ){ useNocase = 1; }else if( strcmp(z,"trace")==0 ){ doTrace = 1; + }else if( strcmp(z,"nosync")==0 ){ + noSync = 1; }else if( strcmp(z,"stats")==0 ){ showStats = 1; }else if( strcmp(z,"summary")==0 ){ showSummary = 1; + }else if( strcmp(z,"cachesize")==0 && i0 && (nOp%commitInterval)==0 ){ + sqlite3_exec(db, "COMMIT; BEGIN IMMEDIATE", 0, 0, 0); + } } } sqlite3_exec(db, "COMMIT", 0, 0, 0); if( zFileToRead ) fclose(in); sqlite3_finalize(pInsert); sqlite3_finalize(pUpdate); sqlite3_finalize(pSelect); + sqlite3_finalize(pDelete); if( showSummary ){ sqlite3_exec(db, "SELECT '-- count(*): ', count(*) FROM wordcount;\n" "SELECT '-- sum(cnt): ', sum(cnt) FROM wordcount;\n"