File indexing completed on 2024-05-12 16:13:59

0001 /*
0002 ** 2001 September 15
0003 **
0004 ** The author disclaims copyright to this source code.  In place of
0005 ** a legal notice, here is a blessing:
0006 **
0007 **    May you do good and not evil.
0008 **    May you find forgiveness for yourself and forgive others.
0009 **    May you share freely, never taking more than you give.
0010 **
0011 *************************************************************************
0012 ** This file contains code to implement the "sqlite" command line
0013 ** utility for accessing SQLite databases.
0014 */
0015 #if defined(_WIN32) || defined(WIN32)
0016 # if !defined(_CRT_SECURE_NO_WARNINGS)
0017 /* This needs to come before any includes for MSVC compiler */
0018 #  define _CRT_SECURE_NO_WARNINGS
0019 # endif
0020 # ifndef access
0021 # include <io.h>
0022 #  define access(f, m) _access_s(f, m)
0023 # endif
0024 #endif
0025 
0026 /*
0027 ** Enable large-file support for fopen() and friends on unix.
0028 */
0029 #ifndef SQLITE_DISABLE_LFS
0030 # define _LARGE_FILE       1
0031 # ifndef _FILE_OFFSET_BITS
0032 #   define _FILE_OFFSET_BITS 64
0033 # endif
0034 # define _LARGEFILE_SOURCE 1
0035 #endif
0036 
0037 #include <stdlib.h>
0038 #include <string.h>
0039 #include <stdio.h>
0040 #include <assert.h>
0041 #include "sqlite3.h"
0042 #include <ctype.h>
0043 #include <stdarg.h>
0044 #include <fcntl.h>
0045 
0046 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__)
0047 # include <signal.h>
0048 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
0049 #  include <pwd.h>
0050 # endif
0051 # include <unistd.h>
0052 # include <sys/types.h>
0053 #endif
0054 
0055 #ifdef __OS2__
0056 # include <unistd.h>
0057 #endif
0058 
0059 /*
0060 ** Used to prevent warnings about unused parameters
0061 */
0062 #define UNUSED_PARAMETER(x) (void)(x)
0063 
0064 /*
0065 ** The following is the open SQLite database.  We make a pointer
0066 ** to this database a static variable so that it can be accessed
0067 ** by the SIGINT handler to interrupt database processing.
0068 */
0069 static sqlite3 *db = 0;
0070 
0071 /*
0072 ** A global char* and an SQL function to access its current value
0073 ** from within an SQL statement. This program used to use the
0074 ** sqlite_exec_printf() API to substitue a string into an SQL statement.
0075 ** The correct way to do this with sqlite3 is to use the bind API, but
0076 ** since the shell is built around the callback paradigm it would be a lot
0077 ** of work. Instead just use this hack, which is quite harmless.
0078 */
0079 static const char *zShellStatic = 0;
0080 static void shellstaticFunc(
0081   sqlite3_context *context,
0082   int argc,
0083   sqlite3_value **argv
0084 ){
0085   assert( 0==argc );
0086   assert( zShellStatic );
0087   UNUSED_PARAMETER(argc);
0088   UNUSED_PARAMETER(argv);
0089   sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC);
0090 }
0091 
0092 /*
0093 ** An pointer to an instance of this structure is passed from
0094 ** the main program to the callback.  This is used to communicate
0095 ** state and mode information.
0096 */
0097 struct callback_data {
0098   sqlite3 *db;           /* The database */
0099   int echoOn;            /* True to echo input commands */
0100   int statsOn;           /* True to display memory stats before each finalize */
0101   int cnt;               /* Number of records displayed so far */
0102   FILE *out;             /* Write results here */
0103   int nErr;              /* Number of errors seen */
0104   int mode;              /* An output mode setting */
0105   int writableSchema;    /* True if PRAGMA writable_schema=ON */
0106   int showHeader;        /* True to show column names in List or Column mode */
0107   char *zDestTable;      /* Name of destination table when MODE_Insert */
0108   char separator[20];    /* Separator character for MODE_List */
0109   int colWidth[100];     /* Requested width of each column when in column mode*/
0110   int actualWidth[100];  /* Actual width of each column */
0111   char nullvalue[20];    /* The text to print when a NULL comes back from
0112                          ** the database */
0113   char outfile[FILENAME_MAX]; /* Filename for *out */
0114   const char *zDbFilename;    /* name of the database file */
0115   const char *zVfs;           /* Name of VFS to use */
0116   sqlite3_stmt *pStmt;   /* Current statement if any. */
0117   FILE *pLog;            /* Write log output here */
0118 };
0119 
0120 /*
0121 ** These are the allowed modes.
0122 */
0123 #define MODE_Line     0  /* One column per line.  Blank line between records */
0124 #define MODE_Column   1  /* One record per line in neat columns */
0125 #define MODE_List     2  /* One record per line with a separator */
0126 #define MODE_Semi     3  /* Same as MODE_List but append ";" to each line */
0127 #define MODE_Html     4  /* Generate an XHTML table */
0128 #define MODE_Insert   5  /* Generate SQL "insert" statements */
0129 #define MODE_Tcl      6  /* Generate ANSI-C or TCL quoted elements */
0130 #define MODE_Csv      7  /* Quote strings, numbers are plain */
0131 #define MODE_Explain  8  /* Like MODE_Column, but do not truncate data */
0132 
0133 /*
0134 ** Number of elements in an array
0135 */
0136 #define ArraySize(X)  (int)(sizeof(X)/sizeof(X[0]))
0137 
0138 /*
0139 ** Compute a string length that is limited to what can be stored in
0140 ** lower 30 bits of a 32-bit signed integer.
0141 */
0142 static int strlen30(const char *z){
0143   const char *z2 = z;
0144   while( *z2 ){ z2++; }
0145   return 0x3fffffff & (int)(z2 - z);
0146 }
0147 
0148 /*
0149 ** A callback for the sqlite3_log() interface.
0150 */
0151 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
0152   struct callback_data *p = (struct callback_data*)pArg;
0153   if( p->pLog==0 ) return;
0154   fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
0155   fflush(p->pLog);
0156 }
0157 
0158 /* zIn is either a pointer to a NULL-terminated string in memory obtained
0159 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
0160 ** added to zIn, and the result returned in memory obtained from malloc().
0161 ** zIn, if it was not NULL, is freed.
0162 **
0163 ** If the third argument, quote, is not '\0', then it is used as a
0164 ** quote character for zAppend.
0165 */
0166 static char *appendText(char *zIn, char const *zAppend, char quote){
0167   int len;
0168   int i;
0169   int nAppend = strlen30(zAppend);
0170   int nIn = (zIn?strlen30(zIn):0);
0171 
0172   len = nAppend+nIn+1;
0173   if( quote ){
0174     len += 2;
0175     for(i=0; i<nAppend; i++){
0176       if( zAppend[i]==quote ) len++;
0177     }
0178   }
0179 
0180   zIn = (char *)realloc(zIn, len);
0181   if( !zIn ){
0182     return 0;
0183   }
0184 
0185   if( quote ){
0186     char *zCsr = &zIn[nIn];
0187     *zCsr++ = quote;
0188     for(i=0; i<nAppend; i++){
0189       *zCsr++ = zAppend[i];
0190       if( zAppend[i]==quote ) *zCsr++ = quote;
0191     }
0192     *zCsr++ = quote;
0193     *zCsr++ = '\0';
0194     assert( (zCsr-zIn)==len );
0195   }else{
0196     memcpy(&zIn[nIn], zAppend, nAppend);
0197     zIn[len-1] = '\0';
0198   }
0199 
0200   return zIn;
0201 }
0202 
0203 /*
0204 ** Execute a query statement that has a single result column.  Print
0205 ** that result column on a line by itself with a semicolon terminator.
0206 **
0207 ** This is used, for example, to show the schema of the database by
0208 ** querying the SQLITE_MASTER table.
0209 */
0210 static int run_table_dump_query(
0211   struct callback_data *p, /* Query context */
0212   const char *zSelect,     /* SELECT statement to extract content */
0213   const char *zFirstRow    /* Print before first row, if not NULL */
0214 ){
0215   sqlite3_stmt *pSelect;
0216   int rc;
0217   rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0);
0218   if( rc!=SQLITE_OK || !pSelect ){
0219     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
0220     p->nErr++;
0221     return rc;
0222   }
0223   rc = sqlite3_step(pSelect);
0224   while( rc==SQLITE_ROW ){
0225     if( zFirstRow ){
0226       fprintf(p->out, "%s", zFirstRow);
0227       zFirstRow = 0;
0228     }
0229     fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0));
0230     rc = sqlite3_step(pSelect);
0231   }
0232   rc = sqlite3_finalize(pSelect);
0233   if( rc!=SQLITE_OK ){
0234     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
0235     p->nErr++;
0236   }
0237   return rc;
0238 }
0239 
0240 /*
0241 ** This is a different callback routine used for dumping the database.
0242 ** Each row received by this callback consists of a table name,
0243 ** the table type ("index" or "table") and SQL to create the table.
0244 ** This routine should print text sufficient to recreate the table.
0245 */
0246 static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
0247   int rc;
0248   const char *zTable;
0249   const char *zType;
0250   const char *zSql;
0251   const char *zPrepStmt = 0;
0252   struct callback_data *p = (struct callback_data *)pArg;
0253 
0254   UNUSED_PARAMETER(azCol);
0255   if( nArg!=3 ) return 1;
0256   zTable = azArg[0];
0257   zType = azArg[1];
0258   zSql = azArg[2];
0259 
0260   if( strcmp(zTable, "sqlite_sequence")==0 ){
0261     zPrepStmt = "DELETE FROM sqlite_sequence;\n";
0262   }else if( strcmp(zTable, "sqlite_stat1")==0 ){
0263     fprintf(p->out, "ANALYZE sqlite_master;\n");
0264   }else if( strncmp(zTable, "sqlite_", 7)==0 ){
0265     return 0;
0266   }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
0267     char *zIns;
0268     if( !p->writableSchema ){
0269       fprintf(p->out, "PRAGMA writable_schema=ON;\n");
0270       p->writableSchema = 1;
0271     }
0272     zIns = sqlite3_mprintf(
0273        "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
0274        "VALUES('table','%q','%q',0,'%q');",
0275        zTable, zTable, zSql);
0276     fprintf(p->out, "%s\n", zIns);
0277     sqlite3_free(zIns);
0278     return 0;
0279   }else{
0280     fprintf(p->out, "%s;\n", zSql);
0281   }
0282 
0283   if( strcmp(zType, "table")==0 ){
0284     sqlite3_stmt *pTableInfo = 0;
0285     char *zSelect = 0;
0286     char *zTableInfo = 0;
0287     char *zTmp = 0;
0288     int nRow = 0;
0289 
0290     zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
0291     zTableInfo = appendText(zTableInfo, zTable, '"');
0292     zTableInfo = appendText(zTableInfo, ");", 0);
0293 
0294     rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0);
0295     free(zTableInfo);
0296     if( rc!=SQLITE_OK || !pTableInfo ){
0297       return 1;
0298     }
0299 
0300     zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0);
0301     zTmp = appendText(zTmp, zTable, '"');
0302     if( zTmp ){
0303       zSelect = appendText(zSelect, zTmp, '\'');
0304       free(zTmp);
0305     }
0306     zSelect = appendText(zSelect, " || ' VALUES(' || ", 0);
0307     rc = sqlite3_step(pTableInfo);
0308     while( rc==SQLITE_ROW ){
0309       const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1);
0310       zSelect = appendText(zSelect, "quote(", 0);
0311       zSelect = appendText(zSelect, zText, '"');
0312       rc = sqlite3_step(pTableInfo);
0313       if( rc==SQLITE_ROW ){
0314         zSelect = appendText(zSelect, ") || ',' || ", 0);
0315       }else{
0316         zSelect = appendText(zSelect, ") ", 0);
0317       }
0318       nRow++;
0319     }
0320     rc = sqlite3_finalize(pTableInfo);
0321     if( rc!=SQLITE_OK || nRow==0 ){
0322       free(zSelect);
0323       return 1;
0324     }
0325     zSelect = appendText(zSelect, "|| ')' FROM  ", 0);
0326     zSelect = appendText(zSelect, zTable, '"');
0327 
0328     rc = run_table_dump_query(p, zSelect, zPrepStmt);
0329     if( rc==SQLITE_CORRUPT ){
0330       zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
0331       run_table_dump_query(p, zSelect, 0);
0332     }
0333     free(zSelect);
0334   }
0335   return 0;
0336 }
0337 
0338 /*
0339 ** Run zQuery.  Use dump_callback() as the callback routine so that
0340 ** the contents of the query are output as SQL statements.
0341 **
0342 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
0343 ** "ORDER BY rowid DESC" to the end.
0344 */
0345 static int run_schema_dump_query(
0346   struct callback_data *p,
0347   const char *zQuery
0348 ){
0349   int rc;
0350   char *zErr = 0;
0351   rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
0352   if( rc==SQLITE_CORRUPT ){
0353     char *zQ2;
0354     int len = strlen30(zQuery);
0355     fprintf(p->out, "/****** CORRUPTION ERROR *******/\n");
0356     if( zErr ){
0357       fprintf(p->out, "/****** %s ******/\n", zErr);
0358       sqlite3_free(zErr);
0359       zErr = 0;
0360     }
0361     zQ2 = malloc( len+100 );
0362     if( zQ2==0 ) return rc;
0363     sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
0364     rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
0365     if( rc ){
0366       fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
0367     }else{
0368       rc = SQLITE_CORRUPT;
0369     }
0370     sqlite3_free(zErr);
0371     free(zQ2);
0372   }
0373   return rc;
0374 }
0375 
0376 /*
0377 ** Make sure the database is open.  If it is not, then open it.  If
0378 ** the database fails to open, print an error message and exit.
0379 */
0380 static void open_db(struct callback_data *p){
0381   if( p->db==0 ){
0382     sqlite3_open(p->zDbFilename, &p->db);
0383     db = p->db;
0384     if( db && sqlite3_errcode(db)==SQLITE_OK ){
0385       sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0,
0386           shellstaticFunc, 0, 0);
0387     }
0388     if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){
0389       fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
0390           p->zDbFilename, sqlite3_errmsg(db));
0391       exit(1);
0392     }
0393 #ifndef SQLITE_OMIT_LOAD_EXTENSION
0394     sqlite3_enable_load_extension(p->db, 1);
0395 #endif
0396   }
0397 }
0398 
0399 static int table_size(struct callback_data *p, const unsigned char* table)
0400 {
0401   int rc;
0402   sqlite3_stmt *pSelect;
0403   const char *sqlPref = "SELECT COUNT() FROM ";
0404   char *sql = malloc(strlen(sqlPref) + 1 + strlen((const char*)table));
0405   strcpy(sql, sqlPref);
0406   strcat(sql, (const char*)table);
0407   /*fprintf(stderr, "%s", sql);*/
0408   rc = sqlite3_prepare(p->db, sql, -1, &pSelect, 0);
0409   free(sql);
0410   if( rc!=SQLITE_OK || !pSelect ){
0411     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
0412     return -1;
0413   }
0414   rc = sqlite3_step(pSelect);
0415   if( rc==SQLITE_ROW ){
0416     const unsigned char *countStr = sqlite3_column_text(pSelect, 0);
0417     int res = atoi((const char*)countStr);
0418     rc = sqlite3_finalize(pSelect);
0419     if( rc!=SQLITE_OK ){
0420         fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
0421         return -1;
0422     }
0423     /*fprintf(stderr, "\n%d?\n", res);*/
0424     return res;
0425   }
0426   rc = sqlite3_finalize(pSelect);
0427   if( rc!=SQLITE_OK ){
0428     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
0429   }
0430   return -1;
0431 }
0432 
0433 /*
0434 ** If an input line begins with "." then invoke this routine to
0435 ** process that line.
0436 **
0437 ** Return 1 on error, 2 to exit, and 0 otherwise.
0438 */
0439 static int do_meta_command(char *zLine, struct callback_data *p){
0440   int i = 1;
0441   sqlite3_stmt *pSelect;
0442   int totalRecords;
0443   int prevPercent;
0444   int size;
0445   int percent = 100;
0446   int rc = 0;
0447   UNUSED_PARAMETER(zLine);
0448 
0449     open_db(p);
0450     /* When playing back a "dump", the content might appear in an order
0451     ** which causes immediate foreign key constraints to be violated.
0452     ** So disable foreign-key constraint enforcement to prevent problems. */
0453     fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
0454     fprintf(p->out, "BEGIN TRANSACTION;\n");
0455     p->writableSchema = 0;
0456     sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
0457     p->nErr = 0;
0458 
0459   /* get table sizes */
0460   rc = sqlite3_prepare(p->db,
0461         "SELECT name FROM sqlite_master "
0462         "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'", -1, &pSelect, 0);
0463   if( rc!=SQLITE_OK || !pSelect ){
0464     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
0465     return rc;
0466   }
0467   rc = sqlite3_step(pSelect);
0468   totalRecords = 0;
0469   for(i=0; rc==SQLITE_ROW; i++){
0470     int size = table_size(p, sqlite3_column_text(pSelect, 0));
0471     if (size < 0) {
0472       sqlite3_finalize(pSelect);
0473       return 1;
0474     }
0475     totalRecords += size;
0476     rc = sqlite3_step(pSelect);
0477   }
0478   /*fprintf(stderr, "totalRecords=%d\n", totalRecords);*/
0479   rc = sqlite3_finalize(pSelect);
0480   if( rc!=SQLITE_OK ){
0481     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
0482     return 1;
0483   }
0484 
0485   /* dump tables */
0486   rc = sqlite3_prepare(p->db,
0487         "SELECT name FROM sqlite_master "
0488         "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'", -1, &pSelect, 0);
0489   if( rc!=SQLITE_OK || !pSelect ){
0490     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
0491     return rc;
0492   }
0493   rc = sqlite3_step(pSelect);
0494   prevPercent = -1;
0495   for(i=0; rc==SQLITE_ROW;){
0496     zShellStatic = (const char*)sqlite3_column_text(pSelect, 0);
0497     run_schema_dump_query(p,
0498       "SELECT name, type, sql FROM sqlite_master "
0499       "WHERE tbl_name LIKE shellstatic() AND type=='table'"
0500       "  AND sql NOT NULL");
0501     run_table_dump_query(p,
0502       "SELECT sql FROM sqlite_master "
0503       "WHERE sql NOT NULL"
0504       "  AND type IN ('index','trigger','view')"
0505       "  AND tbl_name LIKE shellstatic()", 0
0506     );
0507 
0508     size = table_size(p, sqlite3_column_text(pSelect, 0));
0509     if (size < 0) {
0510       sqlite3_finalize(pSelect);
0511       return 1;
0512     }
0513     if (totalRecords > 0) {
0514         percent = 100 * i / totalRecords;
0515     }
0516     if(prevPercent < percent){
0517       fprintf(stderr, "DUMP: %d%%\n", percent);
0518       prevPercent = percent;
0519     }
0520     i += size;
0521     rc = sqlite3_step(pSelect);
0522   }
0523   zShellStatic = 0;
0524   rc = sqlite3_finalize(pSelect);
0525   if( rc!=SQLITE_OK ){
0526     fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
0527     return 1;
0528   }
0529 
0530   run_schema_dump_query(p,
0531     "SELECT name, type, sql FROM sqlite_master "
0532     "WHERE name=='sqlite_sequence'"
0533   );
0534   run_table_dump_query(p,
0535     "SELECT sql FROM sqlite_master "
0536     "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
0537   );
0538   if( p->writableSchema ){
0539     fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
0540     p->writableSchema = 0;
0541   }
0542   sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
0543   sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
0544   fprintf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
0545 
0546   return rc;
0547 }
0548 
0549 /*
0550 ** Initialize the state information in data
0551 */
0552 static void main_init(struct callback_data *data) {
0553   memset(data, 0, sizeof(*data));
0554   data->mode = MODE_List;
0555   memcpy(data->separator,"|", 2);
0556   data->showHeader = 0;
0557 #ifdef SQLITE_CONFIG_URI
0558   sqlite3_config(SQLITE_CONFIG_URI, 1);
0559 #endif
0560 #ifdef SQLITE_CONFIG_LOG
0561   sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
0562 #endif
0563   sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
0564 }
0565 
0566 int shell_main(const char *inFilename) {
0567   struct callback_data data;
0568   int rc = 0;
0569 
0570   main_init(&data);
0571   data.zDbFilename = inFilename;
0572 
0573   /* Go ahead and open the database file if it already exists.  If the
0574   ** file does not exist, delay opening it.  This prevents empty database
0575   ** files from being created if a user mistypes the database name argument
0576   ** to the sqlite command-line tool.
0577   */
0578   if( access(data.zDbFilename, 0)==0 ){
0579     open_db(&data);
0580   }
0581 
0582   data.out = stdout;
0583   rc = do_meta_command(0, &data);
0584 
0585   if( data.db ){
0586     sqlite3_close(data.db);
0587   }
0588   return rc;
0589 }