sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / expert / expert.c
| 1 | /* |
| 2 | ** 2017 April 07 |
| 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 | */ |
| 13 | |
| 14 | |
| 15 | #include <sqlite3.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include "sqlite3expert.h" |
| 20 | |
| 21 | |
| 22 | static void option_requires_argument(const char *zOpt){ |
| 23 | fprintf(stderr, "Option requires an argument: %s\n", zOpt); |
| 24 | exit(-3); |
| 25 | } |
| 26 | |
| 27 | static int option_integer_arg(const char *zVal){ |
| 28 | return atoi(zVal); |
| 29 | } |
| 30 | |
| 31 | static void usage(char **argv){ |
| 32 | fprintf(stderr, "\n"); |
| 33 | fprintf(stderr, "Usage %s ?OPTIONS? DATABASE\n", argv[0]); |
| 34 | fprintf(stderr, "\n"); |
| 35 | fprintf(stderr, "Options are:\n"); |
| 36 | fprintf(stderr, " -sql SQL (analyze SQL statements passed as argument)\n"); |
| 37 | fprintf(stderr, " -file FILE (read SQL statements from file FILE)\n"); |
| 38 | fprintf(stderr, " -verbose LEVEL (integer verbosity level. default 1)\n"); |
| 39 | fprintf(stderr, " -sample PERCENT (percent of db to sample. default 100)\n"); |
| 40 | exit(-1); |
| 41 | } |
| 42 | |
| 43 | static int readSqlFromFile(sqlite3expert *p, const char *zFile, char **pzErr){ |
| 44 | FILE *in = fopen(zFile, "rb"); |
| 45 | long nIn; |
| 46 | size_t nRead; |
| 47 | char *pBuf; |
| 48 | int rc; |
| 49 | if( in==0 ){ |
| 50 | *pzErr = sqlite3_mprintf("failed to open file %s\n", zFile); |
| 51 | return SQLITE_ERROR; |
| 52 | } |
| 53 | fseek(in, 0, SEEK_END); |
| 54 | nIn = ftell(in); |
| 55 | rewind(in); |
| 56 | pBuf = sqlite3_malloc64( nIn+1 ); |
| 57 | if( pBuf==0 ){ |
| 58 | *pzErr = sqlite3_mprintf("out of memory\n"); |
| 59 | return SQLITE_NOMEM; |
| 60 | } |
| 61 | nRead = fread(pBuf, nIn, 1, in); |
| 62 | fclose(in); |
| 63 | if( nRead!=1 ){ |
| 64 | sqlite3_free(pBuf); |
| 65 | *pzErr = sqlite3_mprintf("failed to read file %s\n", zFile); |
| 66 | return SQLITE_ERROR; |
| 67 | } |
| 68 | pBuf[nIn] = 0; |
| 69 | rc = sqlite3_expert_sql(p, pBuf, pzErr); |
| 70 | sqlite3_free(pBuf); |
| 71 | return rc; |
| 72 | } |
| 73 | |
| 74 | int main(int argc, char **argv){ |
| 75 | const char *zDb; |
| 76 | int rc = 0; |
| 77 | char *zErr = 0; |
| 78 | int i; |
| 79 | int iVerbose = 1; /* -verbose option */ |
| 80 | |
| 81 | sqlite3 *db = 0; |
| 82 | sqlite3expert *p = 0; |
| 83 | |
| 84 | if( argc<2 ) usage(argv); |
| 85 | zDb = argv[argc-1]; |
| 86 | if( zDb[0]=='-' ) usage(argv); |
| 87 | rc = sqlite3_open(zDb, &db); |
| 88 | if( rc!=SQLITE_OK ){ |
| 89 | fprintf(stderr, "Cannot open db file: %s - %s\n", zDb, sqlite3_errmsg(db)); |
| 90 | exit(-2); |
| 91 | } |
| 92 | |
| 93 | p = sqlite3_expert_new(db, &zErr); |
| 94 | if( p==0 ){ |
| 95 | fprintf(stderr, "Cannot run analysis: %s\n", zErr); |
| 96 | rc = 1; |
| 97 | }else{ |
| 98 | for(i=1; i<(argc-1); i++){ |
| 99 | char *zArg = argv[i]; |
| 100 | int nArg; |
| 101 | if( zArg[0]=='-' && zArg[1]=='-' && zArg[2]!=0 ) zArg++; |
| 102 | nArg = (int)strlen(zArg); |
| 103 | if( nArg>=2 && 0==sqlite3_strnicmp(zArg, "-file", nArg) ){ |
| 104 | if( ++i==(argc-1) ) option_requires_argument("-file"); |
| 105 | rc = readSqlFromFile(p, argv[i], &zErr); |
| 106 | } |
| 107 | |
| 108 | else if( nArg>=3 && 0==sqlite3_strnicmp(zArg, "-sql", nArg) ){ |
| 109 | if( ++i==(argc-1) ) option_requires_argument("-sql"); |
| 110 | rc = sqlite3_expert_sql(p, argv[i], &zErr); |
| 111 | } |
| 112 | |
| 113 | else if( nArg>=3 && 0==sqlite3_strnicmp(zArg, "-sample", nArg) ){ |
| 114 | int iSample; |
| 115 | if( ++i==(argc-1) ) option_requires_argument("-sample"); |
| 116 | iSample = option_integer_arg(argv[i]); |
| 117 | sqlite3_expert_config(p, EXPERT_CONFIG_SAMPLE, iSample); |
| 118 | } |
| 119 | |
| 120 | else if( nArg>=2 && 0==sqlite3_strnicmp(zArg, "-verbose", nArg) ){ |
| 121 | if( ++i==(argc-1) ) option_requires_argument("-verbose"); |
| 122 | iVerbose = option_integer_arg(argv[i]); |
| 123 | } |
| 124 | |
| 125 | else{ |
| 126 | usage(argv); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if( rc==SQLITE_OK ){ |
| 132 | rc = sqlite3_expert_analyze(p, &zErr); |
| 133 | } |
| 134 | |
| 135 | if( rc==SQLITE_OK ){ |
| 136 | int nQuery = sqlite3_expert_count(p); |
| 137 | if( iVerbose>0 ){ |
| 138 | const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES); |
| 139 | fprintf(stdout, "-- Candidates -------------------------------\n"); |
| 140 | fprintf(stdout, "%s\n", zCand); |
| 141 | } |
| 142 | for(i=0; i<nQuery; i++){ |
| 143 | const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL); |
| 144 | const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES); |
| 145 | const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN); |
| 146 | if( zIdx==0 ) zIdx = "(no new indexes)\n"; |
| 147 | if( iVerbose>0 ){ |
| 148 | fprintf(stdout, "-- Query %d ----------------------------------\n",i+1); |
| 149 | fprintf(stdout, "%s\n\n", zSql); |
| 150 | } |
| 151 | fprintf(stdout, "%s\n%s\n", zIdx, zEQP); |
| 152 | } |
| 153 | }else{ |
| 154 | fprintf(stderr, "Error: %s\n", zErr ? zErr : "?"); |
| 155 | } |
| 156 | |
| 157 | sqlite3_expert_destroy(p); |
| 158 | sqlite3_free(zErr); |
| 159 | return rc; |
| 160 | } |