Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/wordcount.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / wordcount.c` 659 lines ยท 23690 bytes Raw Blame History 1 /* 2 ** This C program extracts all "words" from an input document and adds them 3 ** to an SQLite database. A "word" is any contiguous sequence of alphabetic 4 ** characters. All digits, punctuation, and whitespace characters are 5 ** word separators. The database stores a single entry for each distinct 6 ** word together with a count of the number of occurrences of that word. 7 ** A fresh database is created automatically on each run. 8 ** 9 ** wordcount DATABASE INPUTFILE 10 ** 11 ** The INPUTFILE name can be omitted, in which case input it taken from 12 ** standard input. 13 ** 14 ** Option: 15 ** 16 ** 17 ** Modes: 18 ** 19 ** Insert mode means: 20 ** (1) INSERT OR IGNORE INTO wordcount VALUES($new,1) 21 ** (2) UPDATE wordcount SET cnt=cnt+1 WHERE word=$new -- if (1) is a noop 22 ** 23 ** Update mode means: 24 ** (1) INSERT OR IGNORE INTO wordcount VALUES($new,0) 25 ** (2) UPDATE wordcount SET cnt=cnt+1 WHERE word=$new 26 ** 27 ** Replace mode means: 28 ** (1) REPLACE INTO wordcount 29 ** VALUES($new,ifnull((SELECT cnt FROM wordcount WHERE word=$new),0)+1); 30 ** 31 ** Upsert mode means: 32 ** (1) INSERT INTO wordcount VALUES($new,1) 33 ** ON CONFLICT(word) DO UPDATE SET cnt=cnt+1 34 ** 35 ** Select mode means: 36 ** (1) SELECT 1 FROM wordcount WHERE word=$new 37 ** (2) INSERT INTO wordcount VALUES($new,1) -- if (1) returns nothing 38 ** (3) UPDATE wordcount SET cnt=cnt+1 WHERE word=$new --if (1) return TRUE 39 ** 40 ** Delete mode means: 41 ** (1) DELETE FROM wordcount WHERE word=$new 42 ** 43 ** Query mode means: 44 ** (1) SELECT cnt FROM wordcount WHERE word=$new 45 ** 46 ** Note that delete mode and query mode are only useful for preexisting 47 ** databases. The wordcount table is created using IF NOT EXISTS so this 48 ** utility can be run multiple times on the same database file. The 49 ** --without-rowid, --nocase, and --pagesize parameters are only effective 50 ** when creating a new database and are harmless no-ops on preexisting 51 ** databases. 52 ** 53 ****************************************************************************** 54 ** 55 ** Compile as follows: 56 ** 57 ** gcc -I. wordcount.c sqlite3.c -ldl -lpthreads 58 ** 59 ** Or: 60 ** 61 ** gcc -I. -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \ 62 ** wordcount.c sqlite3.c 63 */ 64 #include 65 #include 66 #include 67 #include 68 #include 69 #include "sqlite3.h" 70 #ifndef _WIN32 71 # include 72 #else 73 # include 74 #endif 75 #define ISALPHA(X) isalpha((unsigned char)(X)) 76 77 const char zHelp[] = 78 "Usage: wordcount [OPTIONS] DATABASE [INPUT]\n" 79 " --all Repeat the test for all test modes\n" 80 " --cachesize NNN Use a cache size of NNN\n" 81 " --commit NNN Commit after every NNN operations\n" 82 " --delete Use DELETE mode\n" 83 " --insert Use INSERT mode (the default)\n" 84 " --journal MMMM Use PRAGMA journal_mode=MMMM\n" 85 " --nocase Add the NOCASE collating sequence to the words.\n" 86 " --nosync Use PRAGMA synchronous=OFF\n" 87 " --pagesize NNN Use a page size of NNN\n" 88 " --query Use QUERY mode\n" 89 " --replace Use REPLACE mode\n" 90 " --select Use SELECT mode\n" 91 " --stats Show sqlite3_status() results at the end.\n" 92 " --summary Show summary information on the collected data.\n" 93 " --tag NAME Tag all output using NAME. Use only stdout.\n" 94 " --timer Time the operation of this program\n" 95 " --trace Enable sqlite3_trace() output.\n" 96 " --update Use UPDATE mode\n" 97 " --upsert Use UPSERT mode\n" 98 " --without-rowid Use a WITHOUT ROWID table to store the words.\n" 99 ; 100 101 /* Output tag */ 102 char *zTag = "--"; 103 104 /* Return the current wall-clock time */ 105 static sqlite3_int64 realTime(void){ 106 static sqlite3_vfs *clockVfs = 0; 107 sqlite3_int64 t; 108 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); 109 if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){ 110 clockVfs->xCurrentTimeInt64(clockVfs, &t); 111 }else{ 112 double r; 113 clockVfs->xCurrentTime(clockVfs, &r); 114 t = (sqlite3_int64)(r*`86400000`.0); 115 } 116 return t; 117 } 118 119 /* Print an error message and exit */ 120 static void fatal_error(const char *zMsg, ...){ 121 va_list ap; 122 va_start(ap, zMsg); 123 vfprintf(stderr, zMsg, ap); 124 va_end(ap); 125 exit(1); 126 } 127 128 /* Print a usage message and quit */ 129 static void usage(void){ 130 printf("%s",zHelp); 131 exit(0); 132 } 133 134 /* The sqlite3_trace() callback function */ 135 static void traceCallback(void *NotUsed, const char *zSql){ 136 printf("%s;\n", zSql); 137 } 138 139 /* An sqlite3_exec() callback that prints results on standard output, 140 ** each column separated by a single space. */ 141 static int printResult(void *NotUsed, int nArg, char **azArg, char **azNm){ 142 int i; 143 printf("%s", zTag); 144 for(i=0; i=MODE_COUNT*2 ) return 0; 253 i = (*piLoopCnt)++; 254 *pUseWithoutRowid = i&1; 255 *piMode2 = i>>1; 256 return 1; 257 } 258 259 int main(int argc, char **argv){ 260 const char *zFileToRead = 0; /* Input file. NULL for stdin */ 261 const char *zDbName = 0; /* Name of the database file to create */ 262 int useWithoutRowid = 0; /* True for --without-rowid */ 263 int iMode = MODE_INSERT; /* One of MODE_xxxxx */ 264 int iMode2; /* Mode to use for current --all iteration */ 265 int iLoopCnt = 0; /* Which iteration when running --all */ 266 int useNocase = 0; /* True for --nocase */ 267 int doTrace = 0; /* True for --trace */ 268 int showStats = 0; /* True for --stats */ 269 int showSummary = 0; /* True for --summary */ 270 int showTimer = 0; /* True for --timer */ 271 int cacheSize = 0; /* Desired cache size. 0 means default */ 272 int pageSize = 0; /* Desired page size. 0 means default */ 273 int commitInterval = 0; /* How often to commit. 0 means never */ 274 int noSync = 0; /* True for --nosync */ 275 const char *zJMode = 0; /* Journal mode */ 276 int nOp = 0; /* Operation counter */ 277 int i, j; /* Loop counters */ 278 sqlite3 *db; /* The SQLite database connection */ 279 char *zSql; /* Constructed SQL statement */ 280 sqlite3_stmt *pInsert = 0; /* The INSERT statement */ 281 sqlite3_stmt *pUpdate = 0; /* The UPDATE statement */ 282 sqlite3_stmt *pSelect = 0; /* The SELECT statement */ 283 sqlite3_stmt *pDelete = 0; /* The DELETE statement */ 284 FILE *in; /* The open input file */ 285 int rc; /* Return code from an SQLite interface */ 286 int iCur, iHiwtr; /* Statistics values, current and "highwater" */ 287 FILE *pTimer = stderr; /* Output channel for the timer */ 288 sqlite3_int64 sumCnt = 0; /* Sum in QUERY mode */ 289 sqlite3_int64 startTime; /* Time of start */ 290 sqlite3_int64 totalTime = 0; /* Total time */ 291 char zInput[2000]; /* A single line of input */ 292 293 /* Process command-line arguments */ 294 for(i=1; i0 && (nOp%commitInterval)==0 ){ 555 sqlite3_exec(db, "COMMIT; BEGIN IMMEDIATE", 0, 0, 0); 556 } 557 } 558 } 559 sqlite3_exec(db, "COMMIT", 0, 0, 0); 560 sqlite3_finalize(pInsert); pInsert = 0; 561 sqlite3_finalize(pUpdate); pUpdate = 0; 562 sqlite3_finalize(pSelect); pSelect = 0; 563 sqlite3_finalize(pDelete); pDelete = 0; 564 565 if( iMode2==MODE_QUERY && iMode!=MODE_ALL ){ 566 printf("%s sum of cnt: %lld\n", zTag, sumCnt); 567 rc = sqlite3_prepare_v2(db,"SELECT sum(cnt*cnt) FROM wordcount", -1, 568 &pSelect, 0); 569 if( rc==SQLITE_OK && sqlite3_step(pSelect)==SQLITE_ROW ){ 570 printf("%s double-check: %lld\n", zTag,sqlite3_column_int64(pSelect,0)); 571 } 572 sqlite3_finalize(pSelect); 573 } 574 575 576 if( showTimer ){ 577 sqlite3_int64 elapseTime = realTime() - startTime; 578 totalTime += elapseTime; 579 fprintf(pTimer, "%3d.%03d wordcount", (int)(elapseTime/1000), 580 (int)(elapseTime%1000)); 581 if( iMode==MODE_ALL ){ 582 fprintf(pTimer, " %s%s\n", azMode[iMode2], 583 useWithoutRowid? " --without-rowid" : ""); 584 }else{ 585 for(i=1; i