drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1 | /* |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 2 | ** 2001 September 15 |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** This file contains code to implement the "sqlite" command line |
| 13 | ** utility for accessing SQLite databases. |
| 14 | ** |
drh | de7446b | 2009-05-31 17:16:09 +0000 | [diff] [blame] | 15 | ** $Id: shell.c,v 1.210 2009/05/31 17:16:10 drh Exp $ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 16 | */ |
shane | 18e526c | 2008-12-10 22:30:24 +0000 | [diff] [blame] | 17 | #if defined(_WIN32) || defined(WIN32) |
| 18 | /* This needs to come before any includes for MSVC compiler */ |
| 19 | #define _CRT_SECURE_NO_WARNINGS |
| 20 | #endif |
| 21 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 25 | #include <assert.h> |
drh | 1d482dd | 2004-05-31 18:23:07 +0000 | [diff] [blame] | 26 | #include "sqlite3.h" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 27 | #include <ctype.h> |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 28 | #include <stdarg.h> |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 29 | |
drh | 454ad58 | 2007-11-26 22:54:27 +0000 | [diff] [blame] | 30 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 31 | # include <signal.h> |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 32 | # if !defined(__RTP__) && !defined(_WRS_KERNEL) |
| 33 | # include <pwd.h> |
| 34 | # endif |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 35 | # include <unistd.h> |
| 36 | # include <sys/types.h> |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 37 | #endif |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 38 | |
drh | cdb36b7 | 2006-06-12 12:57:45 +0000 | [diff] [blame] | 39 | #ifdef __OS2__ |
| 40 | # include <unistd.h> |
| 41 | #endif |
| 42 | |
drh | 16e5955 | 2000-07-31 11:57:37 +0000 | [diff] [blame] | 43 | #if defined(HAVE_READLINE) && HAVE_READLINE==1 |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 44 | # include <readline/readline.h> |
| 45 | # include <readline/history.h> |
| 46 | #else |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 47 | # define readline(p) local_getline(p,stdin) |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 48 | # define add_history(X) |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 49 | # define read_history(X) |
| 50 | # define write_history(X) |
| 51 | # define stifle_history(X) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 54 | #if defined(_WIN32) || defined(WIN32) |
| 55 | # include <io.h> |
shane | 18e526c | 2008-12-10 22:30:24 +0000 | [diff] [blame] | 56 | #define isatty(h) _isatty(h) |
| 57 | #define access(f,m) _access((f),(m)) |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 58 | #else |
drh | 4328c8b | 2003-04-26 02:50:11 +0000 | [diff] [blame] | 59 | /* Make sure isatty() has a prototype. |
| 60 | */ |
| 61 | extern int isatty(); |
adamd | 2e8464a | 2006-09-06 21:39:40 +0000 | [diff] [blame] | 62 | #endif |
drh | 4328c8b | 2003-04-26 02:50:11 +0000 | [diff] [blame] | 63 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 64 | #if defined(_WIN32_WCE) |
| 65 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() |
| 66 | * thus we always assume that we have a console. That can be |
| 67 | * overridden with the -batch command line option. |
| 68 | */ |
| 69 | #define isatty(x) 1 |
| 70 | #endif |
| 71 | |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 72 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL) |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 73 | #include <sys/time.h> |
| 74 | #include <sys/resource.h> |
| 75 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 76 | /* Saved resource information for the beginning of an operation */ |
| 77 | static struct rusage sBegin; |
| 78 | |
| 79 | /* True if the timer is enabled */ |
| 80 | static int enableTimer = 0; |
| 81 | |
| 82 | /* |
| 83 | ** Begin timing an operation |
| 84 | */ |
| 85 | static void beginTimer(void){ |
| 86 | if( enableTimer ){ |
| 87 | getrusage(RUSAGE_SELF, &sBegin); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* Return the difference of two time_structs in seconds */ |
| 92 | static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ |
| 93 | return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + |
| 94 | (double)(pEnd->tv_sec - pStart->tv_sec); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | ** Print the timing results. |
| 99 | */ |
| 100 | static void endTimer(void){ |
| 101 | if( enableTimer ){ |
| 102 | struct rusage sEnd; |
| 103 | getrusage(RUSAGE_SELF, &sEnd); |
| 104 | printf("CPU Time: user %f sys %f\n", |
| 105 | timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), |
| 106 | timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); |
| 107 | } |
| 108 | } |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame^] | 109 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 110 | #define BEGIN_TIMER beginTimer() |
| 111 | #define END_TIMER endTimer() |
| 112 | #define HAS_TIMER 1 |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame^] | 113 | |
| 114 | #elif (defined(_WIN32) || defined(WIN32)) |
| 115 | |
| 116 | #include <windows.h> |
| 117 | |
| 118 | /* Saved resource information for the beginning of an operation */ |
| 119 | static HANDLE hProcess; |
| 120 | static FILETIME ftKernelBegin; |
| 121 | static FILETIME ftUserBegin; |
| 122 | typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME); |
| 123 | static GETPROCTIMES getProcessTimesAddr = NULL; |
| 124 | |
| 125 | /* True if the timer is enabled */ |
| 126 | static int enableTimer = 0; |
| 127 | |
| 128 | /* |
| 129 | ** Check to see if we have timer support. Return 1 if necessary |
| 130 | ** support found (or found previously). |
| 131 | */ |
| 132 | static int hasTimer(void){ |
| 133 | if( getProcessTimesAddr ){ |
| 134 | return 1; |
| 135 | } else { |
| 136 | /* GetProcessTimes() isn't supported in WIN95 and some other Windows versions. |
| 137 | ** See if the version we are running on has it, and if it does, save off |
| 138 | ** a pointer to it and the current process handle. |
| 139 | */ |
| 140 | hProcess = GetCurrentProcess(); |
| 141 | if( hProcess ){ |
| 142 | HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); |
| 143 | if( NULL != hinstLib ){ |
| 144 | getProcessTimesAddr = (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); |
| 145 | if( NULL != getProcessTimesAddr ){ |
| 146 | return 1; |
| 147 | } |
| 148 | FreeLibrary(hinstLib); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | ** Begin timing an operation |
| 157 | */ |
| 158 | static void beginTimer(void){ |
| 159 | if( enableTimer && getProcessTimesAddr ){ |
| 160 | FILETIME ftCreation, ftExit; |
| 161 | getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /* Return the difference of two FILETIME structs in seconds */ |
| 166 | static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ |
| 167 | sqlite_int64 i64Start = *((sqlite_int64 *) pStart); |
| 168 | sqlite_int64 i64End = *((sqlite_int64 *) pEnd); |
| 169 | return (double) ((i64End - i64Start) / 10000000.0); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | ** Print the timing results. |
| 174 | */ |
| 175 | static void endTimer(void){ |
| 176 | if( enableTimer && getProcessTimesAddr){ |
| 177 | FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; |
| 178 | getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd); |
| 179 | printf("CPU Time: user %f sys %f\n", |
| 180 | timeDiff(&ftUserBegin, &ftUserEnd), |
| 181 | timeDiff(&ftKernelBegin, &ftKernelEnd)); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | #define BEGIN_TIMER beginTimer() |
| 186 | #define END_TIMER endTimer() |
| 187 | #define HAS_TIMER hasTimer() |
| 188 | |
drh | da10822 | 2009-02-25 19:07:24 +0000 | [diff] [blame] | 189 | #else |
| 190 | #define BEGIN_TIMER |
| 191 | #define END_TIMER |
| 192 | #define HAS_TIMER 0 |
| 193 | #endif |
| 194 | |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 195 | /* |
| 196 | ** Used to prevent warnings about unused parameters |
| 197 | */ |
| 198 | #define UNUSED_PARAMETER(x) (void)(x) |
| 199 | |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 200 | |
| 201 | /************************************************************************** |
| 202 | *************************************************************************** |
| 203 | ** Begin genfkey logic. |
| 204 | */ |
| 205 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined SQLITE_OMIT_SUBQUERY |
| 206 | |
| 207 | #define GENFKEY_ERROR 1 |
| 208 | #define GENFKEY_DROPTRIGGER 2 |
| 209 | #define GENFKEY_CREATETRIGGER 3 |
| 210 | static int genfkey_create_triggers(sqlite3 *, const char *, void *, |
| 211 | int (*)(void *, int, const char *) |
| 212 | ); |
| 213 | |
| 214 | struct GenfkeyCb { |
| 215 | void *pCtx; |
| 216 | int eType; |
| 217 | int (*xData)(void *, int, const char *); |
| 218 | }; |
| 219 | typedef struct GenfkeyCb GenfkeyCb; |
| 220 | |
| 221 | /* The code in this file defines a sqlite3 virtual-table module that |
| 222 | ** provides a read-only view of the current database schema. There is one |
| 223 | ** row in the schema table for each column in the database schema. |
| 224 | */ |
| 225 | #define SCHEMA \ |
| 226 | "CREATE TABLE x(" \ |
| 227 | "database," /* Name of database (i.e. main, temp etc.) */ \ |
| 228 | "tablename," /* Name of table */ \ |
| 229 | "cid," /* Column number (from left-to-right, 0 upward) */ \ |
| 230 | "name," /* Column name */ \ |
| 231 | "type," /* Specified type (i.e. VARCHAR(32)) */ \ |
| 232 | "not_null," /* Boolean. True if NOT NULL was specified */ \ |
| 233 | "dflt_value," /* Default value for this column */ \ |
| 234 | "pk" /* True if this column is part of the primary key */ \ |
| 235 | ")" |
| 236 | |
| 237 | #define SCHEMA2 \ |
| 238 | "CREATE TABLE x(" \ |
| 239 | "database," /* Name of database (i.e. main, temp etc.) */ \ |
| 240 | "from_tbl," /* Name of table */ \ |
| 241 | "fkid," \ |
| 242 | "seq," \ |
| 243 | "to_tbl," \ |
| 244 | "from_col," \ |
| 245 | "to_col," \ |
| 246 | "on_update," \ |
| 247 | "on_delete," \ |
| 248 | "match" \ |
| 249 | ")" |
| 250 | |
| 251 | #define SCHEMA3 \ |
| 252 | "CREATE TABLE x(" \ |
| 253 | "database," /* Name of database (i.e. main, temp etc.) */ \ |
| 254 | "tablename," /* Name of table */ \ |
| 255 | "seq," \ |
| 256 | "name," \ |
| 257 | "isunique" \ |
| 258 | ")" |
| 259 | |
| 260 | #define SCHEMA4 \ |
| 261 | "CREATE TABLE x(" \ |
| 262 | "database," /* Name of database (i.e. main, temp etc.) */ \ |
| 263 | "indexname," /* Name of table */ \ |
| 264 | "seqno," \ |
| 265 | "cid," \ |
| 266 | "name" \ |
| 267 | ")" |
| 268 | |
| 269 | #define SCHEMA5 \ |
| 270 | "CREATE TABLE x(" \ |
| 271 | "database," /* Name of database (i.e. main, temp etc.) */ \ |
| 272 | "triggername," /* Name of trigger */ \ |
| 273 | "dummy" /* Unused */ \ |
| 274 | ")" |
| 275 | |
| 276 | typedef struct SchemaTable SchemaTable; |
| 277 | struct SchemaTable { |
| 278 | const char *zName; |
| 279 | const char *zObject; |
| 280 | const char *zPragma; |
| 281 | const char *zSchema; |
| 282 | } aSchemaTable[] = { |
| 283 | { "table_info", "table", "PRAGMA %Q.table_info(%Q)", SCHEMA }, |
| 284 | { "foreign_key_list", "table", "PRAGMA %Q.foreign_key_list(%Q)", SCHEMA2 }, |
| 285 | { "index_list", "table", "PRAGMA %Q.index_list(%Q)", SCHEMA3 }, |
| 286 | { "index_info", "index", "PRAGMA %Q.index_info(%Q)", SCHEMA4 }, |
| 287 | { "trigger_list", "trigger", "SELECT 1", SCHEMA5 }, |
| 288 | { 0, 0, 0, 0 } |
| 289 | }; |
| 290 | |
| 291 | typedef struct schema_vtab schema_vtab; |
| 292 | typedef struct schema_cursor schema_cursor; |
| 293 | |
| 294 | /* A schema table object */ |
| 295 | struct schema_vtab { |
| 296 | sqlite3_vtab base; |
| 297 | sqlite3 *db; |
| 298 | SchemaTable *pType; |
| 299 | }; |
| 300 | |
| 301 | /* A schema table cursor object */ |
| 302 | struct schema_cursor { |
| 303 | sqlite3_vtab_cursor base; |
| 304 | sqlite3_stmt *pDbList; |
| 305 | sqlite3_stmt *pTableList; |
| 306 | sqlite3_stmt *pColumnList; |
| 307 | int rowid; |
| 308 | }; |
| 309 | |
| 310 | /* |
| 311 | ** Table destructor for the schema module. |
| 312 | */ |
| 313 | static int schemaDestroy(sqlite3_vtab *pVtab){ |
| 314 | sqlite3_free(pVtab); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | ** Table constructor for the schema module. |
| 320 | */ |
| 321 | static int schemaCreate( |
| 322 | sqlite3 *db, |
| 323 | void *pAux, |
| 324 | int argc, const char *const*argv, |
| 325 | sqlite3_vtab **ppVtab, |
| 326 | char **pzErr |
| 327 | ){ |
| 328 | int rc = SQLITE_NOMEM; |
| 329 | schema_vtab *pVtab; |
| 330 | SchemaTable *pType = &aSchemaTable[0]; |
| 331 | |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 332 | UNUSED_PARAMETER(pzErr); |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 333 | if( argc>3 ){ |
| 334 | int i; |
| 335 | pType = 0; |
| 336 | for(i=0; aSchemaTable[i].zName; i++){ |
| 337 | if( 0==strcmp(argv[3], aSchemaTable[i].zName) ){ |
| 338 | pType = &aSchemaTable[i]; |
| 339 | } |
| 340 | } |
| 341 | if( !pType ){ |
| 342 | return SQLITE_ERROR; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | pVtab = sqlite3_malloc(sizeof(schema_vtab)); |
| 347 | if( pVtab ){ |
| 348 | memset(pVtab, 0, sizeof(schema_vtab)); |
| 349 | pVtab->db = (sqlite3 *)pAux; |
| 350 | pVtab->pType = pType; |
| 351 | rc = sqlite3_declare_vtab(db, pType->zSchema); |
| 352 | } |
| 353 | *ppVtab = (sqlite3_vtab *)pVtab; |
| 354 | return rc; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | ** Open a new cursor on the schema table. |
| 359 | */ |
| 360 | static int schemaOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
| 361 | int rc = SQLITE_NOMEM; |
| 362 | schema_cursor *pCur; |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 363 | UNUSED_PARAMETER(pVTab); |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 364 | pCur = sqlite3_malloc(sizeof(schema_cursor)); |
| 365 | if( pCur ){ |
| 366 | memset(pCur, 0, sizeof(schema_cursor)); |
| 367 | *ppCursor = (sqlite3_vtab_cursor *)pCur; |
| 368 | rc = SQLITE_OK; |
| 369 | } |
| 370 | return rc; |
| 371 | } |
| 372 | |
| 373 | /* |
| 374 | ** Close a schema table cursor. |
| 375 | */ |
| 376 | static int schemaClose(sqlite3_vtab_cursor *cur){ |
| 377 | schema_cursor *pCur = (schema_cursor *)cur; |
| 378 | sqlite3_finalize(pCur->pDbList); |
| 379 | sqlite3_finalize(pCur->pTableList); |
| 380 | sqlite3_finalize(pCur->pColumnList); |
| 381 | sqlite3_free(pCur); |
| 382 | return SQLITE_OK; |
| 383 | } |
| 384 | |
| 385 | static void columnToResult(sqlite3_context *ctx, sqlite3_stmt *pStmt, int iCol){ |
| 386 | switch( sqlite3_column_type(pStmt, iCol) ){ |
| 387 | case SQLITE_NULL: |
| 388 | sqlite3_result_null(ctx); |
| 389 | break; |
| 390 | case SQLITE_INTEGER: |
| 391 | sqlite3_result_int64(ctx, sqlite3_column_int64(pStmt, iCol)); |
| 392 | break; |
| 393 | case SQLITE_FLOAT: |
| 394 | sqlite3_result_double(ctx, sqlite3_column_double(pStmt, iCol)); |
| 395 | break; |
| 396 | case SQLITE_TEXT: { |
| 397 | const char *z = (const char *)sqlite3_column_text(pStmt, iCol); |
| 398 | sqlite3_result_text(ctx, z, -1, SQLITE_TRANSIENT); |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | ** Retrieve a column of data. |
| 406 | */ |
| 407 | static int schemaColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ |
| 408 | schema_cursor *pCur = (schema_cursor *)cur; |
| 409 | switch( i ){ |
| 410 | case 0: |
| 411 | columnToResult(ctx, pCur->pDbList, 1); |
| 412 | break; |
| 413 | case 1: |
| 414 | columnToResult(ctx, pCur->pTableList, 0); |
| 415 | break; |
| 416 | default: |
| 417 | columnToResult(ctx, pCur->pColumnList, i-2); |
| 418 | break; |
| 419 | } |
| 420 | return SQLITE_OK; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | ** Retrieve the current rowid. |
| 425 | */ |
| 426 | static int schemaRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
| 427 | schema_cursor *pCur = (schema_cursor *)cur; |
| 428 | *pRowid = pCur->rowid; |
| 429 | return SQLITE_OK; |
| 430 | } |
| 431 | |
| 432 | static int finalize(sqlite3_stmt **ppStmt){ |
| 433 | int rc = sqlite3_finalize(*ppStmt); |
| 434 | *ppStmt = 0; |
| 435 | return rc; |
| 436 | } |
| 437 | |
| 438 | static int schemaEof(sqlite3_vtab_cursor *cur){ |
| 439 | schema_cursor *pCur = (schema_cursor *)cur; |
| 440 | return (pCur->pDbList ? 0 : 1); |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | ** Advance the cursor to the next row. |
| 445 | */ |
| 446 | static int schemaNext(sqlite3_vtab_cursor *cur){ |
| 447 | int rc = SQLITE_OK; |
| 448 | schema_cursor *pCur = (schema_cursor *)cur; |
| 449 | schema_vtab *pVtab = (schema_vtab *)(cur->pVtab); |
| 450 | char *zSql = 0; |
| 451 | |
| 452 | while( !pCur->pColumnList || SQLITE_ROW!=sqlite3_step(pCur->pColumnList) ){ |
| 453 | if( SQLITE_OK!=(rc = finalize(&pCur->pColumnList)) ) goto next_exit; |
| 454 | |
| 455 | while( !pCur->pTableList || SQLITE_ROW!=sqlite3_step(pCur->pTableList) ){ |
| 456 | if( SQLITE_OK!=(rc = finalize(&pCur->pTableList)) ) goto next_exit; |
| 457 | |
| 458 | assert(pCur->pDbList); |
| 459 | while( SQLITE_ROW!=sqlite3_step(pCur->pDbList) ){ |
| 460 | rc = finalize(&pCur->pDbList); |
| 461 | goto next_exit; |
| 462 | } |
| 463 | |
| 464 | /* Set zSql to the SQL to pull the list of tables from the |
| 465 | ** sqlite_master (or sqlite_temp_master) table of the database |
| 466 | ** identfied by the row pointed to by the SQL statement pCur->pDbList |
| 467 | ** (iterating through a "PRAGMA database_list;" statement). |
| 468 | */ |
| 469 | if( sqlite3_column_int(pCur->pDbList, 0)==1 ){ |
| 470 | zSql = sqlite3_mprintf( |
| 471 | "SELECT name FROM sqlite_temp_master WHERE type=%Q", |
| 472 | pVtab->pType->zObject |
| 473 | ); |
| 474 | }else{ |
| 475 | sqlite3_stmt *pDbList = pCur->pDbList; |
| 476 | zSql = sqlite3_mprintf( |
| 477 | "SELECT name FROM %Q.sqlite_master WHERE type=%Q", |
| 478 | sqlite3_column_text(pDbList, 1), pVtab->pType->zObject |
| 479 | ); |
| 480 | } |
| 481 | if( !zSql ){ |
| 482 | rc = SQLITE_NOMEM; |
| 483 | goto next_exit; |
| 484 | } |
| 485 | |
| 486 | rc = sqlite3_prepare(pVtab->db, zSql, -1, &pCur->pTableList, 0); |
| 487 | sqlite3_free(zSql); |
| 488 | if( rc!=SQLITE_OK ) goto next_exit; |
| 489 | } |
| 490 | |
| 491 | /* Set zSql to the SQL to the table_info pragma for the table currently |
| 492 | ** identified by the rows pointed to by statements pCur->pDbList and |
| 493 | ** pCur->pTableList. |
| 494 | */ |
| 495 | zSql = sqlite3_mprintf(pVtab->pType->zPragma, |
| 496 | sqlite3_column_text(pCur->pDbList, 1), |
| 497 | sqlite3_column_text(pCur->pTableList, 0) |
| 498 | ); |
| 499 | |
| 500 | if( !zSql ){ |
| 501 | rc = SQLITE_NOMEM; |
| 502 | goto next_exit; |
| 503 | } |
| 504 | rc = sqlite3_prepare(pVtab->db, zSql, -1, &pCur->pColumnList, 0); |
| 505 | sqlite3_free(zSql); |
| 506 | if( rc!=SQLITE_OK ) goto next_exit; |
| 507 | } |
| 508 | pCur->rowid++; |
| 509 | |
| 510 | next_exit: |
| 511 | /* TODO: Handle rc */ |
| 512 | return rc; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | ** Reset a schema table cursor. |
| 517 | */ |
| 518 | static int schemaFilter( |
| 519 | sqlite3_vtab_cursor *pVtabCursor, |
| 520 | int idxNum, const char *idxStr, |
| 521 | int argc, sqlite3_value **argv |
| 522 | ){ |
| 523 | int rc; |
| 524 | schema_vtab *pVtab = (schema_vtab *)(pVtabCursor->pVtab); |
| 525 | schema_cursor *pCur = (schema_cursor *)pVtabCursor; |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 526 | UNUSED_PARAMETER(idxNum); |
| 527 | UNUSED_PARAMETER(idxStr); |
| 528 | UNUSED_PARAMETER(argc); |
| 529 | UNUSED_PARAMETER(argv); |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 530 | pCur->rowid = 0; |
| 531 | finalize(&pCur->pTableList); |
| 532 | finalize(&pCur->pColumnList); |
| 533 | finalize(&pCur->pDbList); |
| 534 | rc = sqlite3_prepare(pVtab->db,"SELECT 0, 'main'", -1, &pCur->pDbList, 0); |
| 535 | return (rc==SQLITE_OK ? schemaNext(pVtabCursor) : rc); |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | ** Analyse the WHERE condition. |
| 540 | */ |
| 541 | static int schemaBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 542 | UNUSED_PARAMETER(tab); |
| 543 | UNUSED_PARAMETER(pIdxInfo); |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 544 | return SQLITE_OK; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | ** A virtual table module that merely echos method calls into TCL |
| 549 | ** variables. |
| 550 | */ |
| 551 | static sqlite3_module schemaModule = { |
| 552 | 0, /* iVersion */ |
| 553 | schemaCreate, |
| 554 | schemaCreate, |
| 555 | schemaBestIndex, |
| 556 | schemaDestroy, |
| 557 | schemaDestroy, |
| 558 | schemaOpen, /* xOpen - open a cursor */ |
| 559 | schemaClose, /* xClose - close a cursor */ |
| 560 | schemaFilter, /* xFilter - configure scan constraints */ |
| 561 | schemaNext, /* xNext - advance a cursor */ |
| 562 | schemaEof, /* xEof */ |
| 563 | schemaColumn, /* xColumn - read data */ |
| 564 | schemaRowid, /* xRowid - read data */ |
| 565 | 0, /* xUpdate */ |
| 566 | 0, /* xBegin */ |
| 567 | 0, /* xSync */ |
| 568 | 0, /* xCommit */ |
| 569 | 0, /* xRollback */ |
| 570 | 0, /* xFindMethod */ |
| 571 | 0, /* xRename */ |
| 572 | }; |
| 573 | |
| 574 | /* |
| 575 | ** Extension load function. |
| 576 | */ |
| 577 | static int installSchemaModule(sqlite3 *db, sqlite3 *sdb){ |
| 578 | sqlite3_create_module(db, "schema", &schemaModule, (void *)sdb); |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | ** sj(zValue, zJoin) |
| 584 | ** |
| 585 | ** The following block contains the implementation of an aggregate |
| 586 | ** function that returns a string. Each time the function is stepped, |
| 587 | ** it appends data to an internal buffer. When the aggregate is finalized, |
| 588 | ** the contents of the buffer are returned. |
| 589 | ** |
| 590 | ** The first time the aggregate is stepped the buffer is set to a copy |
| 591 | ** of the first argument. The second time and subsequent times it is |
| 592 | ** stepped a copy of the second argument is appended to the buffer, then |
| 593 | ** a copy of the first. |
| 594 | ** |
| 595 | ** Example: |
| 596 | ** |
| 597 | ** INSERT INTO t1(a) VALUES('1'); |
| 598 | ** INSERT INTO t1(a) VALUES('2'); |
| 599 | ** INSERT INTO t1(a) VALUES('3'); |
| 600 | ** SELECT sj(a, ', ') FROM t1; |
| 601 | ** |
| 602 | ** => "1, 2, 3" |
| 603 | ** |
| 604 | */ |
| 605 | struct StrBuffer { |
| 606 | char *zBuf; |
| 607 | }; |
| 608 | typedef struct StrBuffer StrBuffer; |
| 609 | static void joinFinalize(sqlite3_context *context){ |
| 610 | StrBuffer *p; |
| 611 | p = (StrBuffer *)sqlite3_aggregate_context(context, sizeof(StrBuffer)); |
| 612 | sqlite3_result_text(context, p->zBuf, -1, SQLITE_TRANSIENT); |
| 613 | sqlite3_free(p->zBuf); |
| 614 | } |
| 615 | static void joinStep( |
| 616 | sqlite3_context *context, |
| 617 | int argc, |
| 618 | sqlite3_value **argv |
| 619 | ){ |
| 620 | StrBuffer *p; |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 621 | UNUSED_PARAMETER(argc); |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 622 | p = (StrBuffer *)sqlite3_aggregate_context(context, sizeof(StrBuffer)); |
| 623 | if( p->zBuf==0 ){ |
| 624 | p->zBuf = sqlite3_mprintf("%s", sqlite3_value_text(argv[0])); |
| 625 | }else{ |
| 626 | char *zTmp = p->zBuf; |
| 627 | p->zBuf = sqlite3_mprintf("%s%s%s", |
| 628 | zTmp, sqlite3_value_text(argv[1]), sqlite3_value_text(argv[0]) |
| 629 | ); |
| 630 | sqlite3_free(zTmp); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /* |
| 635 | ** dq(zString) |
| 636 | ** |
| 637 | ** This scalar function accepts a single argument and interprets it as |
| 638 | ** a text value. The return value is the argument enclosed in double |
| 639 | ** quotes. If any double quote characters are present in the argument, |
| 640 | ** these are escaped. |
| 641 | ** |
| 642 | ** dq('the raven "Nevermore."') == '"the raven ""Nevermore."""' |
| 643 | */ |
| 644 | static void doublequote( |
| 645 | sqlite3_context *context, |
| 646 | int argc, |
| 647 | sqlite3_value **argv |
| 648 | ){ |
| 649 | int ii; |
| 650 | char *zOut; |
| 651 | char *zCsr; |
| 652 | const char *zIn = (const char *)sqlite3_value_text(argv[0]); |
| 653 | int nIn = sqlite3_value_bytes(argv[0]); |
| 654 | |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 655 | UNUSED_PARAMETER(argc); |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 656 | zOut = sqlite3_malloc(nIn*2+3); |
| 657 | zCsr = zOut; |
| 658 | *zCsr++ = '"'; |
| 659 | for(ii=0; ii<nIn; ii++){ |
| 660 | *zCsr++ = zIn[ii]; |
| 661 | if( zIn[ii]=='"' ){ |
| 662 | *zCsr++ = '"'; |
| 663 | } |
| 664 | } |
| 665 | *zCsr++ = '"'; |
| 666 | *zCsr++ = '\0'; |
| 667 | |
| 668 | sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT); |
| 669 | sqlite3_free(zOut); |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | ** multireplace(zString, zSearch1, zReplace1, ...) |
| 674 | */ |
| 675 | static void multireplace( |
| 676 | sqlite3_context *context, |
| 677 | int argc, |
| 678 | sqlite3_value **argv |
| 679 | ){ |
| 680 | int i = 0; |
| 681 | char *zOut = 0; |
| 682 | int nOut = 0; |
| 683 | int nMalloc = 0; |
| 684 | const char *zIn = (const char *)sqlite3_value_text(argv[0]); |
| 685 | int nIn = sqlite3_value_bytes(argv[0]); |
| 686 | |
| 687 | while( i<nIn ){ |
| 688 | const char *zCopy = &zIn[i]; |
| 689 | int nCopy = 1; |
| 690 | int nReplace = 1; |
| 691 | int j; |
| 692 | for(j=1; j<(argc-1); j+=2){ |
| 693 | const char *z = (const char *)sqlite3_value_text(argv[j]); |
| 694 | int n = sqlite3_value_bytes(argv[j]); |
| 695 | if( n<=(nIn-i) && 0==strncmp(z, zCopy, n) ){ |
| 696 | zCopy = (const char *)sqlite3_value_text(argv[j+1]); |
| 697 | nCopy = sqlite3_value_bytes(argv[j+1]); |
| 698 | nReplace = n; |
| 699 | break; |
| 700 | } |
| 701 | } |
| 702 | if( (nOut+nCopy)>nMalloc ){ |
drh | de7446b | 2009-05-31 17:16:09 +0000 | [diff] [blame] | 703 | char *zNew; |
danielk1977 | 9365c67 | 2009-03-13 15:32:53 +0000 | [diff] [blame] | 704 | nMalloc = 16 + (nOut+nCopy)*2; |
drh | de7446b | 2009-05-31 17:16:09 +0000 | [diff] [blame] | 705 | zNew = (char*)sqlite3_realloc(zOut, nMalloc); |
| 706 | if( zNew==0 ){ |
| 707 | sqlite3_result_error_nomem(context); |
| 708 | return; |
| 709 | }else{ |
| 710 | zOut = zNew; |
| 711 | } |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 712 | } |
danielk1977 | 9365c67 | 2009-03-13 15:32:53 +0000 | [diff] [blame] | 713 | assert( nMalloc>=(nOut+nCopy) ); |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 714 | memcpy(&zOut[nOut], zCopy, nCopy); |
| 715 | i += nReplace; |
| 716 | nOut += nCopy; |
| 717 | } |
| 718 | |
| 719 | sqlite3_result_text(context, zOut, nOut, SQLITE_TRANSIENT); |
| 720 | sqlite3_free(zOut); |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | ** A callback for sqlite3_exec() invokes the callback specified by the |
| 725 | ** GenfkeyCb structure pointed to by the void* passed as the first argument. |
| 726 | */ |
| 727 | static int invokeCallback(void *p, int nArg, char **azArg, char **azCol){ |
| 728 | GenfkeyCb *pCb = (GenfkeyCb *)p; |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 729 | UNUSED_PARAMETER(nArg); |
| 730 | UNUSED_PARAMETER(azCol); |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 731 | return pCb->xData(pCb->pCtx, pCb->eType, azArg[0]); |
| 732 | } |
| 733 | |
| 734 | int detectSchemaProblem( |
| 735 | sqlite3 *db, /* Database connection */ |
| 736 | const char *zMessage, /* English language error message */ |
| 737 | const char *zSql, /* SQL statement to run */ |
| 738 | GenfkeyCb *pCb |
| 739 | ){ |
| 740 | sqlite3_stmt *pStmt; |
| 741 | int rc; |
| 742 | rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); |
| 743 | if( rc!=SQLITE_OK ){ |
| 744 | return rc; |
| 745 | } |
| 746 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 747 | char *zDel; |
| 748 | int iFk = sqlite3_column_int(pStmt, 0); |
| 749 | const char *zTab = (const char *)sqlite3_column_text(pStmt, 1); |
| 750 | zDel = sqlite3_mprintf("Error in table %s: %s", zTab, zMessage); |
| 751 | rc = pCb->xData(pCb->pCtx, pCb->eType, zDel); |
| 752 | sqlite3_free(zDel); |
| 753 | if( rc!=SQLITE_OK ) return rc; |
| 754 | zDel = sqlite3_mprintf( |
| 755 | "DELETE FROM temp.fkey WHERE from_tbl = %Q AND fkid = %d" |
| 756 | , zTab, iFk |
| 757 | ); |
| 758 | sqlite3_exec(db, zDel, 0, 0, 0); |
| 759 | sqlite3_free(zDel); |
| 760 | } |
| 761 | sqlite3_finalize(pStmt); |
| 762 | return SQLITE_OK; |
| 763 | } |
| 764 | |
| 765 | /* |
| 766 | ** Create and populate temporary table "fkey". |
| 767 | */ |
| 768 | static int populateTempTable(sqlite3 *db, GenfkeyCb *pCallback){ |
| 769 | int rc; |
| 770 | |
| 771 | rc = sqlite3_exec(db, |
| 772 | "CREATE VIRTUAL TABLE temp.v_fkey USING schema(foreign_key_list);" |
| 773 | "CREATE VIRTUAL TABLE temp.v_col USING schema(table_info);" |
| 774 | "CREATE VIRTUAL TABLE temp.v_idxlist USING schema(index_list);" |
| 775 | "CREATE VIRTUAL TABLE temp.v_idxinfo USING schema(index_info);" |
| 776 | "CREATE VIRTUAL TABLE temp.v_triggers USING schema(trigger_list);" |
| 777 | "CREATE TABLE temp.fkey AS " |
| 778 | "SELECT from_tbl, to_tbl, fkid, from_col, to_col, on_update, on_delete " |
| 779 | "FROM temp.v_fkey WHERE database = 'main';" |
| 780 | , 0, 0, 0 |
| 781 | ); |
| 782 | if( rc!=SQLITE_OK ) return rc; |
| 783 | |
| 784 | rc = detectSchemaProblem(db, "foreign key columns do not exist", |
| 785 | "SELECT fkid, from_tbl " |
| 786 | "FROM temp.fkey " |
| 787 | "WHERE to_col IS NOT NULL AND NOT EXISTS (SELECT 1 " |
| 788 | "FROM temp.v_col WHERE tablename=to_tbl AND name==to_col" |
| 789 | ")", pCallback |
| 790 | ); |
| 791 | if( rc!=SQLITE_OK ) return rc; |
| 792 | |
| 793 | /* At this point the temp.fkey table is mostly populated. If any foreign |
| 794 | ** keys were specified so that they implicitly refer to they primary |
| 795 | ** key of the parent table, the "to_col" values of the temp.fkey rows |
| 796 | ** are still set to NULL. |
| 797 | ** |
| 798 | ** This is easily fixed for single column primary keys, but not for |
| 799 | ** composites. With a composite primary key, there is no way to reliably |
| 800 | ** query sqlite for the order in which the columns that make up the |
| 801 | ** composite key were declared i.e. there is no way to tell if the |
| 802 | ** schema actually contains "PRIMARY KEY(a, b)" or "PRIMARY KEY(b, a)". |
| 803 | ** Therefore, this case is not handled. The following function call |
| 804 | ** detects instances of this case. |
| 805 | */ |
| 806 | rc = detectSchemaProblem(db, "implicit mapping to composite primary key", |
| 807 | "SELECT fkid, from_tbl " |
| 808 | "FROM temp.fkey " |
| 809 | "WHERE to_col IS NULL " |
| 810 | "GROUP BY fkid, from_tbl HAVING count(*) > 1", pCallback |
| 811 | ); |
| 812 | if( rc!=SQLITE_OK ) return rc; |
| 813 | |
| 814 | /* Detect attempts to implicitly map to the primary key of a table |
| 815 | ** that has no primary key column. |
| 816 | */ |
| 817 | rc = detectSchemaProblem(db, "implicit mapping to non-existant primary key", |
| 818 | "SELECT fkid, from_tbl " |
| 819 | "FROM temp.fkey " |
| 820 | "WHERE to_col IS NULL AND NOT EXISTS " |
| 821 | "(SELECT 1 FROM temp.v_col WHERE pk AND tablename = temp.fkey.to_tbl)" |
| 822 | , pCallback |
| 823 | ); |
| 824 | if( rc!=SQLITE_OK ) return rc; |
| 825 | |
| 826 | /* Fix all the implicit primary key mappings in the temp.fkey table. */ |
| 827 | rc = sqlite3_exec(db, |
| 828 | "UPDATE temp.fkey SET to_col = " |
| 829 | "(SELECT name FROM temp.v_col WHERE pk AND tablename=temp.fkey.to_tbl)" |
| 830 | " WHERE to_col IS NULL;" |
| 831 | , 0, 0, 0 |
| 832 | ); |
| 833 | if( rc!=SQLITE_OK ) return rc; |
| 834 | |
| 835 | /* Now check that all all parent keys are either primary keys or |
| 836 | ** subject to a unique constraint. |
| 837 | */ |
| 838 | rc = sqlite3_exec(db, |
| 839 | "CREATE TABLE temp.idx2 AS SELECT " |
| 840 | "il.tablename AS tablename," |
| 841 | "ii.indexname AS indexname," |
| 842 | "ii.name AS col " |
| 843 | "FROM temp.v_idxlist AS il, temp.v_idxinfo AS ii " |
| 844 | "WHERE il.isunique AND il.database='main' AND ii.indexname = il.name;" |
| 845 | "INSERT INTO temp.idx2 " |
| 846 | "SELECT tablename, 'pk', name FROM temp.v_col WHERE pk;" |
| 847 | |
| 848 | "CREATE TABLE temp.idx AS SELECT " |
| 849 | "tablename, indexname, sj(dq(col),',') AS cols " |
| 850 | "FROM (SELECT * FROM temp.idx2 ORDER BY col) " |
| 851 | "GROUP BY tablename, indexname;" |
| 852 | |
| 853 | "CREATE TABLE temp.fkey2 AS SELECT " |
| 854 | "fkid, from_tbl, to_tbl, sj(dq(to_col),',') AS cols " |
| 855 | "FROM (SELECT * FROM temp.fkey ORDER BY to_col) " |
| 856 | "GROUP BY fkid, from_tbl;" |
| 857 | |
| 858 | "CREATE TABLE temp.triggers AS SELECT " |
| 859 | "triggername FROM temp.v_triggers WHERE database='main' AND " |
| 860 | "triggername LIKE 'genfkey%';" |
| 861 | , 0, 0, 0 |
| 862 | ); |
| 863 | if( rc!=SQLITE_OK ) return rc; |
| 864 | rc = detectSchemaProblem(db, "foreign key is not unique", |
| 865 | "SELECT fkid, from_tbl " |
| 866 | "FROM temp.fkey2 " |
| 867 | "WHERE NOT EXISTS (SELECT 1 " |
| 868 | "FROM temp.idx WHERE tablename=to_tbl AND fkey2.cols==idx.cols" |
| 869 | ")", pCallback |
| 870 | ); |
| 871 | if( rc!=SQLITE_OK ) return rc; |
| 872 | |
| 873 | return rc; |
| 874 | } |
| 875 | |
| 876 | #define GENFKEY_ERROR 1 |
| 877 | #define GENFKEY_DROPTRIGGER 2 |
| 878 | #define GENFKEY_CREATETRIGGER 3 |
| 879 | static int genfkey_create_triggers( |
| 880 | sqlite3 *sdb, /* Connection to read schema from */ |
| 881 | const char *zDb, /* Name of db to read ("main", "temp") */ |
| 882 | void *pCtx, /* Context pointer to pass to xData */ |
| 883 | int (*xData)(void *, int, const char *) |
| 884 | ){ |
| 885 | const char *zSql = |
| 886 | "SELECT multireplace('" |
| 887 | |
| 888 | "-- Triggers for foreign key mapping:\n" |
| 889 | "--\n" |
| 890 | "-- /from_readable/ REFERENCES /to_readable/\n" |
| 891 | "-- on delete /on_delete/\n" |
| 892 | "-- on update /on_update/\n" |
| 893 | "--\n" |
| 894 | |
| 895 | /* The "BEFORE INSERT ON <referencing>" trigger. This trigger's job is to |
| 896 | ** throw an exception if the user tries to insert a row into the |
| 897 | ** referencing table for which there is no corresponding row in |
| 898 | ** the referenced table. |
| 899 | */ |
| 900 | "CREATE TRIGGER /name/_insert_referencing BEFORE INSERT ON /tbl/ WHEN \n" |
| 901 | " /key_notnull/ AND NOT EXISTS (SELECT 1 FROM /ref/ WHERE /cond1/)\n" |
| 902 | "BEGIN\n" |
| 903 | " SELECT RAISE(ABORT, ''constraint failed'');\n" |
| 904 | "END;\n" |
| 905 | |
| 906 | /* The "BEFORE UPDATE ON <referencing>" trigger. This trigger's job |
| 907 | ** is to throw an exception if the user tries to update a row in the |
| 908 | ** referencing table causing it to correspond to no row in the |
| 909 | ** referenced table. |
| 910 | */ |
| 911 | "CREATE TRIGGER /name/_update_referencing BEFORE\n" |
| 912 | " UPDATE OF /rkey_list/ ON /tbl/ WHEN \n" |
| 913 | " /key_notnull/ AND \n" |
| 914 | " NOT EXISTS (SELECT 1 FROM /ref/ WHERE /cond1/)\n" |
| 915 | "BEGIN\n" |
| 916 | " SELECT RAISE(ABORT, ''constraint failed'');\n" |
| 917 | "END;\n" |
| 918 | |
| 919 | |
| 920 | /* The "BEFORE DELETE ON <referenced>" trigger. This trigger's job |
| 921 | ** is to detect when a row is deleted from the referenced table to |
| 922 | ** which rows in the referencing table correspond. The action taken |
| 923 | ** depends on the value of the 'ON DELETE' clause. |
| 924 | */ |
| 925 | "CREATE TRIGGER /name/_delete_referenced BEFORE DELETE ON /ref/ WHEN\n" |
| 926 | " EXISTS (SELECT 1 FROM /tbl/ WHERE /cond2/)\n" |
| 927 | "BEGIN\n" |
| 928 | " /delete_action/\n" |
| 929 | "END;\n" |
| 930 | |
dan | 1da40a3 | 2009-09-19 17:00:31 +0000 | [diff] [blame] | 931 | /* The "AFTER UPDATE ON <referenced>" trigger. This trigger's job |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 932 | ** is to detect when the key columns of a row in the referenced table |
| 933 | ** to which one or more rows in the referencing table correspond are |
| 934 | ** updated. The action taken depends on the value of the 'ON UPDATE' |
| 935 | ** clause. |
| 936 | */ |
| 937 | "CREATE TRIGGER /name/_update_referenced AFTER\n" |
| 938 | " UPDATE OF /fkey_list/ ON /ref/ WHEN \n" |
| 939 | " EXISTS (SELECT 1 FROM /tbl/ WHERE /cond2/)\n" |
| 940 | "BEGIN\n" |
| 941 | " /update_action/\n" |
| 942 | "END;\n" |
| 943 | "'" |
| 944 | |
| 945 | /* These are used in the SQL comment written above each set of triggers */ |
| 946 | ", '/from_readable/', from_tbl || '(' || sj(from_col, ', ') || ')'" |
| 947 | ", '/to_readable/', to_tbl || '(' || sj(to_col, ', ') || ')'" |
| 948 | ", '/on_delete/', on_delete" |
| 949 | ", '/on_update/', on_update" |
| 950 | |
| 951 | ", '/name/', 'genfkey' || min(rowid)" |
| 952 | ", '/tbl/', dq(from_tbl)" |
| 953 | ", '/ref/', dq(to_tbl)" |
| 954 | ", '/key_notnull/', sj('new.' || dq(from_col) || ' IS NOT NULL', ' AND ')" |
| 955 | |
dan | 8b6d37d | 2009-10-08 13:42:28 +0000 | [diff] [blame] | 956 | ", '/fkey_list/', sj(dq(to_col), ', ')" |
| 957 | ", '/rkey_list/', sj(dq(from_col), ', ')" |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 958 | |
| 959 | ", '/cond1/', sj(multireplace('new./from/ == /to/'" |
| 960 | ", '/from/', dq(from_col)" |
| 961 | ", '/to/', dq(to_col)" |
| 962 | "), ' AND ')" |
| 963 | ", '/cond2/', sj(multireplace('old./to/ == /from/'" |
| 964 | ", '/from/', dq(from_col)" |
| 965 | ", '/to/', dq(to_col)" |
| 966 | "), ' AND ')" |
| 967 | |
| 968 | ", '/update_action/', CASE on_update " |
| 969 | "WHEN 'SET NULL' THEN " |
| 970 | "multireplace('UPDATE /tbl/ SET /setlist/ WHERE /where/;' " |
dan | 8b6d37d | 2009-10-08 13:42:28 +0000 | [diff] [blame] | 971 | ", '/setlist/', sj(dq(from_col)||' = NULL',', ')" |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 972 | ", '/tbl/', dq(from_tbl)" |
dan | 8b6d37d | 2009-10-08 13:42:28 +0000 | [diff] [blame] | 973 | ", '/where/', sj(dq(from_col)||' = old.'||dq(to_col),' AND ')" |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 974 | ")" |
| 975 | "WHEN 'CASCADE' THEN " |
| 976 | "multireplace('UPDATE /tbl/ SET /setlist/ WHERE /where/;' " |
| 977 | ", '/setlist/', sj(dq(from_col)||' = new.'||dq(to_col),', ')" |
| 978 | ", '/tbl/', dq(from_tbl)" |
| 979 | ", '/where/', sj(dq(from_col)||' = old.'||dq(to_col),' AND ')" |
| 980 | ")" |
| 981 | "ELSE " |
| 982 | " 'SELECT RAISE(ABORT, ''constraint failed'');'" |
| 983 | "END " |
| 984 | |
| 985 | ", '/delete_action/', CASE on_delete " |
| 986 | "WHEN 'SET NULL' THEN " |
| 987 | "multireplace('UPDATE /tbl/ SET /setlist/ WHERE /where/;' " |
dan | 8b6d37d | 2009-10-08 13:42:28 +0000 | [diff] [blame] | 988 | ", '/setlist/', sj(dq(from_col)||' = NULL',', ')" |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 989 | ", '/tbl/', dq(from_tbl)" |
dan | 8b6d37d | 2009-10-08 13:42:28 +0000 | [diff] [blame] | 990 | ", '/where/', sj(dq(from_col)||' = old.'||dq(to_col),' AND ')" |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 991 | ")" |
| 992 | "WHEN 'CASCADE' THEN " |
| 993 | "multireplace('DELETE FROM /tbl/ WHERE /where/;' " |
| 994 | ", '/tbl/', dq(from_tbl)" |
| 995 | ", '/where/', sj(dq(from_col)||' = old.'||dq(to_col),' AND ')" |
| 996 | ")" |
| 997 | "ELSE " |
| 998 | " 'SELECT RAISE(ABORT, ''constraint failed'');'" |
| 999 | "END " |
| 1000 | |
| 1001 | ") FROM temp.fkey " |
| 1002 | "GROUP BY from_tbl, fkid" |
| 1003 | ; |
| 1004 | |
| 1005 | int rc; |
| 1006 | const int enc = SQLITE_UTF8; |
| 1007 | sqlite3 *db = 0; |
| 1008 | |
| 1009 | GenfkeyCb cb; |
| 1010 | cb.xData = xData; |
| 1011 | cb.pCtx = pCtx; |
| 1012 | |
shane | c0688ea | 2009-03-05 03:48:06 +0000 | [diff] [blame] | 1013 | UNUSED_PARAMETER(zDb); |
| 1014 | |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 1015 | /* Open the working database handle. */ |
| 1016 | rc = sqlite3_open(":memory:", &db); |
| 1017 | if( rc!=SQLITE_OK ) goto genfkey_exit; |
| 1018 | |
| 1019 | /* Create the special scalar and aggregate functions used by this program. */ |
| 1020 | sqlite3_create_function(db, "dq", 1, enc, 0, doublequote, 0, 0); |
| 1021 | sqlite3_create_function(db, "multireplace", -1, enc, db, multireplace, 0, 0); |
| 1022 | sqlite3_create_function(db, "sj", 2, enc, 0, 0, joinStep, joinFinalize); |
| 1023 | |
| 1024 | /* Install the "schema" virtual table module */ |
| 1025 | installSchemaModule(db, sdb); |
| 1026 | |
| 1027 | /* Create and populate a temp table with the information required to |
| 1028 | ** build the foreign key triggers. See function populateTempTable() |
| 1029 | ** for details. |
| 1030 | */ |
| 1031 | cb.eType = GENFKEY_ERROR; |
| 1032 | rc = populateTempTable(db, &cb); |
| 1033 | if( rc!=SQLITE_OK ) goto genfkey_exit; |
| 1034 | |
| 1035 | /* Unless the --no-drop option was specified, generate DROP TRIGGER |
| 1036 | ** statements to drop any triggers in the database generated by a |
| 1037 | ** previous run of this program. |
| 1038 | */ |
| 1039 | cb.eType = GENFKEY_DROPTRIGGER; |
| 1040 | rc = sqlite3_exec(db, |
| 1041 | "SELECT 'DROP TRIGGER main.' || dq(triggername) || ';' FROM triggers" |
| 1042 | ,invokeCallback, (void *)&cb, 0 |
| 1043 | ); |
| 1044 | if( rc!=SQLITE_OK ) goto genfkey_exit; |
| 1045 | |
| 1046 | /* Run the main query to create the trigger definitions. */ |
| 1047 | cb.eType = GENFKEY_CREATETRIGGER; |
| 1048 | rc = sqlite3_exec(db, zSql, invokeCallback, (void *)&cb, 0); |
| 1049 | if( rc!=SQLITE_OK ) goto genfkey_exit; |
| 1050 | |
| 1051 | genfkey_exit: |
| 1052 | sqlite3_close(db); |
| 1053 | return rc; |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | #endif |
| 1058 | /* End genfkey logic. */ |
| 1059 | /*************************************************************************/ |
| 1060 | /*************************************************************************/ |
| 1061 | |
drh | e91d16b | 2008-12-08 18:27:31 +0000 | [diff] [blame] | 1062 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1063 | ** If the following flag is set, then command execution stops |
| 1064 | ** at an error if we are not interactive. |
| 1065 | */ |
| 1066 | static int bail_on_error = 0; |
| 1067 | |
| 1068 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1069 | ** Threat stdin as an interactive input if the following variable |
| 1070 | ** is true. Otherwise, assume stdin is connected to a file or pipe. |
| 1071 | */ |
| 1072 | static int stdin_is_interactive = 1; |
| 1073 | |
| 1074 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1075 | ** The following is the open SQLite database. We make a pointer |
| 1076 | ** to this database a static variable so that it can be accessed |
| 1077 | ** by the SIGINT handler to interrupt database processing. |
| 1078 | */ |
danielk1977 | 92f9a1b | 2004-06-19 09:08:16 +0000 | [diff] [blame] | 1079 | static sqlite3 *db = 0; |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1080 | |
| 1081 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1082 | ** True if an interrupt (Control-C) has been received. |
| 1083 | */ |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 1084 | static volatile int seenInterrupt = 0; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1085 | |
| 1086 | /* |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1087 | ** This is the name of our program. It is set in main(), used |
| 1088 | ** in a number of other places, mostly for error messages. |
| 1089 | */ |
| 1090 | static char *Argv0; |
| 1091 | |
| 1092 | /* |
| 1093 | ** Prompt strings. Initialized in main. Settable with |
| 1094 | ** .prompt main continue |
| 1095 | */ |
| 1096 | static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ |
| 1097 | static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ |
| 1098 | |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1099 | /* |
| 1100 | ** Write I/O traces to the following stream. |
| 1101 | */ |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 1102 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1103 | static FILE *iotrace = 0; |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 1104 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1105 | |
| 1106 | /* |
| 1107 | ** This routine works like printf in that its first argument is a |
| 1108 | ** format string and subsequent arguments are values to be substituted |
| 1109 | ** in place of % fields. The result of formatting this string |
| 1110 | ** is written to iotrace. |
| 1111 | */ |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 1112 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1113 | static void iotracePrintf(const char *zFormat, ...){ |
| 1114 | va_list ap; |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 1115 | char *z; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1116 | if( iotrace==0 ) return; |
| 1117 | va_start(ap, zFormat); |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 1118 | z = sqlite3_vmprintf(zFormat, ap); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1119 | va_end(ap); |
drh | f075cd0 | 2007-02-28 06:14:25 +0000 | [diff] [blame] | 1120 | fprintf(iotrace, "%s", z); |
| 1121 | sqlite3_free(z); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1122 | } |
rse | be0a909 | 2007-07-30 18:24:38 +0000 | [diff] [blame] | 1123 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 1124 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1125 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1126 | /* |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 1127 | ** Determines if a string is a number of not. |
| 1128 | */ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1129 | static int isNumber(const char *z, int *realnum){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 1130 | if( *z=='-' || *z=='+' ) z++; |
| 1131 | if( !isdigit(*z) ){ |
| 1132 | return 0; |
| 1133 | } |
| 1134 | z++; |
| 1135 | if( realnum ) *realnum = 0; |
| 1136 | while( isdigit(*z) ){ z++; } |
| 1137 | if( *z=='.' ){ |
| 1138 | z++; |
| 1139 | if( !isdigit(*z) ) return 0; |
| 1140 | while( isdigit(*z) ){ z++; } |
| 1141 | if( realnum ) *realnum = 1; |
| 1142 | } |
| 1143 | if( *z=='e' || *z=='E' ){ |
| 1144 | z++; |
| 1145 | if( *z=='+' || *z=='-' ) z++; |
| 1146 | if( !isdigit(*z) ) return 0; |
| 1147 | while( isdigit(*z) ){ z++; } |
| 1148 | if( realnum ) *realnum = 1; |
| 1149 | } |
| 1150 | return *z==0; |
| 1151 | } |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 1152 | |
| 1153 | /* |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1154 | ** A global char* and an SQL function to access its current value |
| 1155 | ** from within an SQL statement. This program used to use the |
| 1156 | ** sqlite_exec_printf() API to substitue a string into an SQL statement. |
| 1157 | ** The correct way to do this with sqlite3 is to use the bind API, but |
| 1158 | ** since the shell is built around the callback paradigm it would be a lot |
| 1159 | ** of work. Instead just use this hack, which is quite harmless. |
| 1160 | */ |
| 1161 | static const char *zShellStatic = 0; |
| 1162 | static void shellstaticFunc( |
| 1163 | sqlite3_context *context, |
| 1164 | int argc, |
| 1165 | sqlite3_value **argv |
| 1166 | ){ |
| 1167 | assert( 0==argc ); |
| 1168 | assert( zShellStatic ); |
shane | d87897d | 2009-01-30 05:40:27 +0000 | [diff] [blame] | 1169 | UNUSED_PARAMETER(argc); |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1170 | UNUSED_PARAMETER(argv); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 1171 | sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC); |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1176 | ** This routine reads a line of text from FILE in, stores |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1177 | ** the text in memory obtained from malloc() and returns a pointer |
| 1178 | ** to the text. NULL is returned at end of file, or if malloc() |
| 1179 | ** fails. |
| 1180 | ** |
| 1181 | ** The interface is like "readline" but no command-line editing |
| 1182 | ** is done. |
| 1183 | */ |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 1184 | static char *local_getline(char *zPrompt, FILE *in){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1185 | char *zLine; |
| 1186 | int nLine; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1187 | int n; |
| 1188 | int eol; |
| 1189 | |
| 1190 | if( zPrompt && *zPrompt ){ |
| 1191 | printf("%s",zPrompt); |
| 1192 | fflush(stdout); |
| 1193 | } |
| 1194 | nLine = 100; |
| 1195 | zLine = malloc( nLine ); |
| 1196 | if( zLine==0 ) return 0; |
| 1197 | n = 0; |
| 1198 | eol = 0; |
| 1199 | while( !eol ){ |
| 1200 | if( n+100>nLine ){ |
| 1201 | nLine = nLine*2 + 100; |
| 1202 | zLine = realloc(zLine, nLine); |
| 1203 | if( zLine==0 ) return 0; |
| 1204 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1205 | if( fgets(&zLine[n], nLine - n, in)==0 ){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1206 | if( n==0 ){ |
| 1207 | free(zLine); |
| 1208 | return 0; |
| 1209 | } |
| 1210 | zLine[n] = 0; |
| 1211 | eol = 1; |
| 1212 | break; |
| 1213 | } |
| 1214 | while( zLine[n] ){ n++; } |
| 1215 | if( n>0 && zLine[n-1]=='\n' ){ |
| 1216 | n--; |
| 1217 | zLine[n] = 0; |
| 1218 | eol = 1; |
| 1219 | } |
| 1220 | } |
| 1221 | zLine = realloc( zLine, n+1 ); |
| 1222 | return zLine; |
| 1223 | } |
| 1224 | |
| 1225 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1226 | ** Retrieve a single line of input text. |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1227 | ** |
| 1228 | ** zPrior is a string of prior text retrieved. If not the empty |
| 1229 | ** string, then issue a continuation prompt. |
| 1230 | */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1231 | static char *one_input_line(const char *zPrior, FILE *in){ |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1232 | char *zPrompt; |
| 1233 | char *zResult; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1234 | if( in!=0 ){ |
drh | 9347b20 | 2003-07-18 01:30:59 +0000 | [diff] [blame] | 1235 | return local_getline(0, in); |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1236 | } |
| 1237 | if( zPrior && zPrior[0] ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1238 | zPrompt = continuePrompt; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1239 | }else{ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1240 | zPrompt = mainPrompt; |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1241 | } |
| 1242 | zResult = readline(zPrompt); |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1243 | #if defined(HAVE_READLINE) && HAVE_READLINE==1 |
drh | eb741d5 | 2006-06-03 17:37:25 +0000 | [diff] [blame] | 1244 | if( zResult && *zResult ) add_history(zResult); |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1245 | #endif |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1246 | return zResult; |
| 1247 | } |
| 1248 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1249 | struct previous_mode_data { |
| 1250 | int valid; /* Is there legit data in here? */ |
| 1251 | int mode; |
| 1252 | int showHeader; |
| 1253 | int colWidth[100]; |
| 1254 | }; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1255 | |
drh | 8e7e7a2 | 2000-05-30 18:45:23 +0000 | [diff] [blame] | 1256 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1257 | ** An pointer to an instance of this structure is passed from |
| 1258 | ** the main program to the callback. This is used to communicate |
| 1259 | ** state and mode information. |
| 1260 | */ |
| 1261 | struct callback_data { |
danielk1977 | 92f9a1b | 2004-06-19 09:08:16 +0000 | [diff] [blame] | 1262 | sqlite3 *db; /* The database */ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1263 | int echoOn; /* True to echo input commands */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1264 | int cnt; /* Number of records displayed so far */ |
| 1265 | FILE *out; /* Write results here */ |
| 1266 | int mode; /* An output mode setting */ |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1267 | int writableSchema; /* True if PRAGMA writable_schema=ON */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1268 | int showHeader; /* True to show column names in List or Column mode */ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1269 | char *zDestTable; /* Name of destination table when MODE_Insert */ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1270 | char separator[20]; /* Separator character for MODE_List */ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1271 | int colWidth[100]; /* Requested width of each column when in column mode*/ |
| 1272 | int actualWidth[100]; /* Actual width of each column */ |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 1273 | char nullvalue[20]; /* The text to print when a NULL comes back from |
| 1274 | ** the database */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1275 | struct previous_mode_data explainPrev; |
drh | 8396566 | 2003-04-17 02:54:13 +0000 | [diff] [blame] | 1276 | /* Holds the mode information just before |
| 1277 | ** .explain ON */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1278 | char outfile[FILENAME_MAX]; /* Filename for *out */ |
| 1279 | const char *zDbFilename; /* name of the database file */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1280 | }; |
| 1281 | |
| 1282 | /* |
| 1283 | ** These are the allowed modes. |
| 1284 | */ |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 1285 | #define MODE_Line 0 /* One column per line. Blank line between records */ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1286 | #define MODE_Column 1 /* One record per line in neat columns */ |
| 1287 | #define MODE_List 2 /* One record per line with a separator */ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1288 | #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ |
| 1289 | #define MODE_Html 4 /* Generate an XHTML table */ |
| 1290 | #define MODE_Insert 5 /* Generate SQL "insert" statements */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1291 | #define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */ |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1292 | #define MODE_Csv 7 /* Quote strings, numbers are plain */ |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1293 | #define MODE_Explain 8 /* Like MODE_Column, but do not truncate data */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1294 | |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1295 | static const char *modeDescr[] = { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1296 | "line", |
| 1297 | "column", |
| 1298 | "list", |
| 1299 | "semi", |
| 1300 | "html", |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1301 | "insert", |
| 1302 | "tcl", |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1303 | "csv", |
drh | 66ce4d0 | 2008-02-15 17:38:06 +0000 | [diff] [blame] | 1304 | "explain", |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1305 | }; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1306 | |
| 1307 | /* |
| 1308 | ** Number of elements in an array |
| 1309 | */ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1310 | #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1311 | |
| 1312 | /* |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1313 | ** Compute a string length that is limited to what can be stored in |
| 1314 | ** lower 30 bits of a 32-bit signed integer. |
| 1315 | */ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1316 | static int strlen30(const char *z){ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1317 | const char *z2 = z; |
| 1318 | while( *z2 ){ z2++; } |
| 1319 | return 0x3fffffff & (int)(z2 - z); |
| 1320 | } |
| 1321 | |
| 1322 | /* |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1323 | ** Output the given string as a quoted string using SQL quoting conventions. |
| 1324 | */ |
| 1325 | static void output_quoted_string(FILE *out, const char *z){ |
| 1326 | int i; |
| 1327 | int nSingle = 0; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1328 | for(i=0; z[i]; i++){ |
| 1329 | if( z[i]=='\'' ) nSingle++; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1330 | } |
| 1331 | if( nSingle==0 ){ |
| 1332 | fprintf(out,"'%s'",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1333 | }else{ |
| 1334 | fprintf(out,"'"); |
| 1335 | while( *z ){ |
| 1336 | for(i=0; z[i] && z[i]!='\''; i++){} |
| 1337 | if( i==0 ){ |
| 1338 | fprintf(out,"''"); |
| 1339 | z++; |
| 1340 | }else if( z[i]=='\'' ){ |
| 1341 | fprintf(out,"%.*s''",i,z); |
| 1342 | z += i+1; |
| 1343 | }else{ |
drh | cd7d273 | 2002-02-26 23:24:26 +0000 | [diff] [blame] | 1344 | fprintf(out,"%s",z); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1345 | break; |
| 1346 | } |
| 1347 | } |
drh | cd7d273 | 2002-02-26 23:24:26 +0000 | [diff] [blame] | 1348 | fprintf(out,"'"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1353 | ** Output the given string as a quoted according to C or TCL quoting rules. |
| 1354 | */ |
| 1355 | static void output_c_string(FILE *out, const char *z){ |
| 1356 | unsigned int c; |
| 1357 | fputc('"', out); |
| 1358 | while( (c = *(z++))!=0 ){ |
| 1359 | if( c=='\\' ){ |
| 1360 | fputc(c, out); |
| 1361 | fputc(c, out); |
| 1362 | }else if( c=='\t' ){ |
| 1363 | fputc('\\', out); |
| 1364 | fputc('t', out); |
| 1365 | }else if( c=='\n' ){ |
| 1366 | fputc('\\', out); |
| 1367 | fputc('n', out); |
| 1368 | }else if( c=='\r' ){ |
| 1369 | fputc('\\', out); |
| 1370 | fputc('r', out); |
| 1371 | }else if( !isprint(c) ){ |
drh | 0a8640d | 2005-08-30 20:12:02 +0000 | [diff] [blame] | 1372 | fprintf(out, "\\%03o", c&0xff); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1373 | }else{ |
| 1374 | fputc(c, out); |
| 1375 | } |
| 1376 | } |
| 1377 | fputc('"', out); |
| 1378 | } |
| 1379 | |
| 1380 | /* |
drh | c08a4f1 | 2000-06-15 16:49:48 +0000 | [diff] [blame] | 1381 | ** Output the given string with characters that are special to |
| 1382 | ** HTML escaped. |
| 1383 | */ |
| 1384 | static void output_html_string(FILE *out, const char *z){ |
| 1385 | int i; |
| 1386 | while( *z ){ |
| 1387 | for(i=0; z[i] && z[i]!='<' && z[i]!='&'; i++){} |
| 1388 | if( i>0 ){ |
| 1389 | fprintf(out,"%.*s",i,z); |
| 1390 | } |
| 1391 | if( z[i]=='<' ){ |
| 1392 | fprintf(out,"<"); |
| 1393 | }else if( z[i]=='&' ){ |
| 1394 | fprintf(out,"&"); |
| 1395 | }else{ |
| 1396 | break; |
| 1397 | } |
| 1398 | z += i + 1; |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | /* |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1403 | ** If a field contains any character identified by a 1 in the following |
| 1404 | ** array, then the string must be quoted for CSV. |
| 1405 | */ |
| 1406 | static const char needCsvQuote[] = { |
| 1407 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1408 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1409 | 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1410 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1411 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1412 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1413 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1414 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 1415 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1416 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1417 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1418 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1419 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1420 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1421 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1422 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 1423 | }; |
| 1424 | |
| 1425 | /* |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1426 | ** Output a single term of CSV. Actually, p->separator is used for |
| 1427 | ** the separator, which may or may not be a comma. p->nullvalue is |
| 1428 | ** the null value. Strings are quoted using ANSI-C rules. Numbers |
| 1429 | ** appear outside of quotes. |
| 1430 | */ |
| 1431 | static void output_csv(struct callback_data *p, const char *z, int bSep){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1432 | FILE *out = p->out; |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1433 | if( z==0 ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1434 | fprintf(out,"%s",p->nullvalue); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1435 | }else{ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1436 | int i; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1437 | int nSep = strlen30(p->separator); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1438 | for(i=0; z[i]; i++){ |
drh | c85375d | 2007-12-18 15:41:44 +0000 | [diff] [blame] | 1439 | if( needCsvQuote[((unsigned char*)z)[i]] |
| 1440 | || (z[i]==p->separator[0] && |
| 1441 | (nSep==1 || memcmp(z, p->separator, nSep)==0)) ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 1442 | i = 0; |
| 1443 | break; |
| 1444 | } |
| 1445 | } |
| 1446 | if( i==0 ){ |
| 1447 | putc('"', out); |
| 1448 | for(i=0; z[i]; i++){ |
| 1449 | if( z[i]=='"' ) putc('"', out); |
| 1450 | putc(z[i], out); |
| 1451 | } |
| 1452 | putc('"', out); |
| 1453 | }else{ |
| 1454 | fprintf(out, "%s", z); |
| 1455 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1456 | } |
| 1457 | if( bSep ){ |
drh | d0e7788 | 2008-01-14 15:20:08 +0000 | [diff] [blame] | 1458 | fprintf(p->out, "%s", p->separator); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1462 | #ifdef SIGINT |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1463 | /* |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1464 | ** This routine runs when the user presses Ctrl-C |
| 1465 | */ |
| 1466 | static void interrupt_handler(int NotUsed){ |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1467 | UNUSED_PARAMETER(NotUsed); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 1468 | seenInterrupt = 1; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 1469 | if( db ) sqlite3_interrupt(db); |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1470 | } |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 1471 | #endif |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 1472 | |
| 1473 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1474 | ** This is the callback routine that the SQLite library |
| 1475 | ** invokes for each row of a query result. |
| 1476 | */ |
| 1477 | static int callback(void *pArg, int nArg, char **azArg, char **azCol){ |
| 1478 | int i; |
| 1479 | struct callback_data *p = (struct callback_data*)pArg; |
| 1480 | switch( p->mode ){ |
| 1481 | case MODE_Line: { |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1482 | int w = 5; |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1483 | if( azArg==0 ) break; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1484 | for(i=0; i<nArg; i++){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1485 | int len = strlen30(azCol[i] ? azCol[i] : ""); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1486 | if( len>w ) w = len; |
| 1487 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1488 | if( p->cnt++>0 ) fprintf(p->out,"\n"); |
| 1489 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 1490 | fprintf(p->out,"%*s = %s\n", w, azCol[i], |
drh | a69d916 | 2003-04-17 22:57:53 +0000 | [diff] [blame] | 1491 | azArg[i] ? azArg[i] : p->nullvalue); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1492 | } |
| 1493 | break; |
| 1494 | } |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1495 | case MODE_Explain: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1496 | case MODE_Column: { |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1497 | if( p->cnt++==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1498 | for(i=0; i<nArg; i++){ |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1499 | int w, n; |
| 1500 | if( i<ArraySize(p->colWidth) ){ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1501 | w = p->colWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1502 | }else{ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1503 | w = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1504 | } |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1505 | if( w<=0 ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1506 | w = strlen30(azCol[i] ? azCol[i] : ""); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1507 | if( w<10 ) w = 10; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1508 | n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue); |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1509 | if( w<n ) w = n; |
| 1510 | } |
| 1511 | if( i<ArraySize(p->actualWidth) ){ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 1512 | p->actualWidth[i] = w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1513 | } |
| 1514 | if( p->showHeader ){ |
| 1515 | fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " "); |
| 1516 | } |
| 1517 | } |
| 1518 | if( p->showHeader ){ |
| 1519 | for(i=0; i<nArg; i++){ |
| 1520 | int w; |
| 1521 | if( i<ArraySize(p->actualWidth) ){ |
| 1522 | w = p->actualWidth[i]; |
| 1523 | }else{ |
| 1524 | w = 10; |
| 1525 | } |
| 1526 | fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------" |
| 1527 | "----------------------------------------------------------", |
| 1528 | i==nArg-1 ? "\n": " "); |
| 1529 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1530 | } |
| 1531 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1532 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1533 | for(i=0; i<nArg; i++){ |
| 1534 | int w; |
drh | a0c66f5 | 2000-07-29 13:20:21 +0000 | [diff] [blame] | 1535 | if( i<ArraySize(p->actualWidth) ){ |
| 1536 | w = p->actualWidth[i]; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1537 | }else{ |
| 1538 | w = 10; |
| 1539 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 1540 | if( p->mode==MODE_Explain && azArg[i] && |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1541 | strlen30(azArg[i])>w ){ |
| 1542 | w = strlen30(azArg[i]); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 1543 | } |
drh | c61053b | 2000-06-04 12:58:36 +0000 | [diff] [blame] | 1544 | fprintf(p->out,"%-*.*s%s",w,w, |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1545 | azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " "); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1546 | } |
| 1547 | break; |
| 1548 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1549 | case MODE_Semi: |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1550 | case MODE_List: { |
| 1551 | if( p->cnt++==0 && p->showHeader ){ |
| 1552 | for(i=0; i<nArg; i++){ |
| 1553 | fprintf(p->out,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator); |
| 1554 | } |
| 1555 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1556 | if( azArg==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1557 | for(i=0; i<nArg; i++){ |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1558 | char *z = azArg[i]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1559 | if( z==0 ) z = p->nullvalue; |
drh | 71172c5 | 2002-01-24 00:00:21 +0000 | [diff] [blame] | 1560 | fprintf(p->out, "%s", z); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 1561 | if( i<nArg-1 ){ |
| 1562 | fprintf(p->out, "%s", p->separator); |
| 1563 | }else if( p->mode==MODE_Semi ){ |
| 1564 | fprintf(p->out, ";\n"); |
| 1565 | }else{ |
| 1566 | fprintf(p->out, "\n"); |
| 1567 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1568 | } |
| 1569 | break; |
| 1570 | } |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1571 | case MODE_Html: { |
| 1572 | if( p->cnt++==0 && p->showHeader ){ |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 1573 | fprintf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1574 | for(i=0; i<nArg; i++){ |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 1575 | fprintf(p->out,"<TH>%s</TH>",azCol[i]); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1576 | } |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 1577 | fprintf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1578 | } |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1579 | if( azArg==0 ) break; |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 1580 | fprintf(p->out,"<TR>"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1581 | for(i=0; i<nArg; i++){ |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 1582 | fprintf(p->out,"<TD>"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1583 | output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 1584 | fprintf(p->out,"</TD>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1585 | } |
mihailim | 57c591a | 2008-06-23 21:26:05 +0000 | [diff] [blame] | 1586 | fprintf(p->out,"</TR>\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 1587 | break; |
| 1588 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1589 | case MODE_Tcl: { |
| 1590 | if( p->cnt++==0 && p->showHeader ){ |
| 1591 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 1592 | output_c_string(p->out,azCol[i] ? azCol[i] : ""); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1593 | fprintf(p->out, "%s", p->separator); |
| 1594 | } |
| 1595 | fprintf(p->out,"\n"); |
| 1596 | } |
| 1597 | if( azArg==0 ) break; |
| 1598 | for(i=0; i<nArg; i++){ |
| 1599 | output_c_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); |
| 1600 | fprintf(p->out, "%s", p->separator); |
| 1601 | } |
| 1602 | fprintf(p->out,"\n"); |
| 1603 | break; |
| 1604 | } |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1605 | case MODE_Csv: { |
| 1606 | if( p->cnt++==0 && p->showHeader ){ |
| 1607 | for(i=0; i<nArg; i++){ |
drh | 2cc5569 | 2006-06-27 20:39:04 +0000 | [diff] [blame] | 1608 | output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1); |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 1609 | } |
| 1610 | fprintf(p->out,"\n"); |
| 1611 | } |
| 1612 | if( azArg==0 ) break; |
| 1613 | for(i=0; i<nArg; i++){ |
| 1614 | output_csv(p, azArg[i], i<nArg-1); |
| 1615 | } |
| 1616 | fprintf(p->out,"\n"); |
| 1617 | break; |
| 1618 | } |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1619 | case MODE_Insert: { |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1620 | if( azArg==0 ) break; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1621 | fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1622 | for(i=0; i<nArg; i++){ |
| 1623 | char *zSep = i>0 ? ",": ""; |
| 1624 | if( azArg[i]==0 ){ |
| 1625 | fprintf(p->out,"%sNULL",zSep); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 1626 | }else if( isNumber(azArg[i], 0) ){ |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1627 | fprintf(p->out,"%s%s",zSep, azArg[i]); |
| 1628 | }else{ |
| 1629 | if( zSep[0] ) fprintf(p->out,"%s",zSep); |
| 1630 | output_quoted_string(p->out, azArg[i]); |
| 1631 | } |
| 1632 | } |
| 1633 | fprintf(p->out,");\n"); |
drh | 6a53534 | 2001-10-19 16:44:56 +0000 | [diff] [blame] | 1634 | break; |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 1635 | } |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 1636 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1637 | return 0; |
| 1638 | } |
| 1639 | |
| 1640 | /* |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1641 | ** Set the destination table field of the callback_data structure to |
| 1642 | ** the name of the table given. Escape any quote characters in the |
| 1643 | ** table name. |
| 1644 | */ |
| 1645 | static void set_table_name(struct callback_data *p, const char *zName){ |
| 1646 | int i, n; |
| 1647 | int needQuote; |
| 1648 | char *z; |
| 1649 | |
| 1650 | if( p->zDestTable ){ |
| 1651 | free(p->zDestTable); |
| 1652 | p->zDestTable = 0; |
| 1653 | } |
| 1654 | if( zName==0 ) return; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1655 | needQuote = !isalpha((unsigned char)*zName) && *zName!='_'; |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1656 | for(i=n=0; zName[i]; i++, n++){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 1657 | if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1658 | needQuote = 1; |
| 1659 | if( zName[i]=='\'' ) n++; |
| 1660 | } |
| 1661 | } |
| 1662 | if( needQuote ) n += 2; |
| 1663 | z = p->zDestTable = malloc( n+1 ); |
| 1664 | if( z==0 ){ |
| 1665 | fprintf(stderr,"Out of memory!\n"); |
| 1666 | exit(1); |
| 1667 | } |
| 1668 | n = 0; |
| 1669 | if( needQuote ) z[n++] = '\''; |
| 1670 | for(i=0; zName[i]; i++){ |
| 1671 | z[n++] = zName[i]; |
| 1672 | if( zName[i]=='\'' ) z[n++] = '\''; |
| 1673 | } |
| 1674 | if( needQuote ) z[n++] = '\''; |
| 1675 | z[n] = 0; |
| 1676 | } |
| 1677 | |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1678 | /* zIn is either a pointer to a NULL-terminated string in memory obtained |
| 1679 | ** from malloc(), or a NULL pointer. The string pointed to by zAppend is |
| 1680 | ** added to zIn, and the result returned in memory obtained from malloc(). |
| 1681 | ** zIn, if it was not NULL, is freed. |
| 1682 | ** |
| 1683 | ** If the third argument, quote, is not '\0', then it is used as a |
| 1684 | ** quote character for zAppend. |
| 1685 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1686 | static char *appendText(char *zIn, char const *zAppend, char quote){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1687 | int len; |
| 1688 | int i; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1689 | int nAppend = strlen30(zAppend); |
| 1690 | int nIn = (zIn?strlen30(zIn):0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1691 | |
| 1692 | len = nAppend+nIn+1; |
| 1693 | if( quote ){ |
| 1694 | len += 2; |
| 1695 | for(i=0; i<nAppend; i++){ |
| 1696 | if( zAppend[i]==quote ) len++; |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | zIn = (char *)realloc(zIn, len); |
| 1701 | if( !zIn ){ |
| 1702 | return 0; |
| 1703 | } |
| 1704 | |
| 1705 | if( quote ){ |
| 1706 | char *zCsr = &zIn[nIn]; |
| 1707 | *zCsr++ = quote; |
| 1708 | for(i=0; i<nAppend; i++){ |
| 1709 | *zCsr++ = zAppend[i]; |
| 1710 | if( zAppend[i]==quote ) *zCsr++ = quote; |
| 1711 | } |
| 1712 | *zCsr++ = quote; |
| 1713 | *zCsr++ = '\0'; |
| 1714 | assert( (zCsr-zIn)==len ); |
| 1715 | }else{ |
| 1716 | memcpy(&zIn[nIn], zAppend, nAppend); |
| 1717 | zIn[len-1] = '\0'; |
| 1718 | } |
| 1719 | |
| 1720 | return zIn; |
| 1721 | } |
| 1722 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1723 | |
| 1724 | /* |
| 1725 | ** Execute a query statement that has a single result column. Print |
| 1726 | ** that result column on a line by itself with a semicolon terminator. |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1727 | ** |
| 1728 | ** This is used, for example, to show the schema of the database by |
| 1729 | ** querying the SQLITE_MASTER table. |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1730 | */ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1731 | static int run_table_dump_query( |
| 1732 | FILE *out, /* Send output here */ |
| 1733 | sqlite3 *db, /* Database to query */ |
| 1734 | const char *zSelect, /* SELECT statement to extract content */ |
| 1735 | const char *zFirstRow /* Print before first row, if not NULL */ |
| 1736 | ){ |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1737 | sqlite3_stmt *pSelect; |
| 1738 | int rc; |
| 1739 | rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0); |
| 1740 | if( rc!=SQLITE_OK || !pSelect ){ |
| 1741 | return rc; |
| 1742 | } |
| 1743 | rc = sqlite3_step(pSelect); |
| 1744 | while( rc==SQLITE_ROW ){ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1745 | if( zFirstRow ){ |
| 1746 | fprintf(out, "%s", zFirstRow); |
| 1747 | zFirstRow = 0; |
| 1748 | } |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1749 | fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0)); |
| 1750 | rc = sqlite3_step(pSelect); |
| 1751 | } |
| 1752 | return sqlite3_finalize(pSelect); |
| 1753 | } |
| 1754 | |
| 1755 | |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 1756 | /* |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1757 | ** This is a different callback routine used for dumping the database. |
| 1758 | ** Each row received by this callback consists of a table name, |
| 1759 | ** the table type ("index" or "table") and SQL to create the table. |
| 1760 | ** This routine should print text sufficient to recreate the table. |
| 1761 | */ |
| 1762 | static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1763 | int rc; |
| 1764 | const char *zTable; |
| 1765 | const char *zType; |
| 1766 | const char *zSql; |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1767 | const char *zPrepStmt = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1768 | struct callback_data *p = (struct callback_data *)pArg; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1769 | |
drh | 902b9ee | 2008-12-05 17:17:07 +0000 | [diff] [blame] | 1770 | UNUSED_PARAMETER(azCol); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1771 | if( nArg!=3 ) return 1; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1772 | zTable = azArg[0]; |
| 1773 | zType = azArg[1]; |
| 1774 | zSql = azArg[2]; |
| 1775 | |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 1776 | if( strcmp(zTable, "sqlite_sequence")==0 ){ |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1777 | zPrepStmt = "DELETE FROM sqlite_sequence;\n"; |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 1778 | }else if( strcmp(zTable, "sqlite_stat1")==0 ){ |
| 1779 | fprintf(p->out, "ANALYZE sqlite_master;\n"); |
| 1780 | }else if( strncmp(zTable, "sqlite_", 7)==0 ){ |
| 1781 | return 0; |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1782 | }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ |
| 1783 | char *zIns; |
| 1784 | if( !p->writableSchema ){ |
| 1785 | fprintf(p->out, "PRAGMA writable_schema=ON;\n"); |
| 1786 | p->writableSchema = 1; |
| 1787 | } |
| 1788 | zIns = sqlite3_mprintf( |
| 1789 | "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" |
| 1790 | "VALUES('table','%q','%q',0,'%q');", |
| 1791 | zTable, zTable, zSql); |
| 1792 | fprintf(p->out, "%s\n", zIns); |
| 1793 | sqlite3_free(zIns); |
| 1794 | return 0; |
drh | 00b950d | 2005-09-11 02:03:03 +0000 | [diff] [blame] | 1795 | }else{ |
| 1796 | fprintf(p->out, "%s;\n", zSql); |
drh | f8eb96a | 2005-02-03 00:42:34 +0000 | [diff] [blame] | 1797 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1798 | |
| 1799 | if( strcmp(zType, "table")==0 ){ |
| 1800 | sqlite3_stmt *pTableInfo = 0; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1801 | char *zSelect = 0; |
| 1802 | char *zTableInfo = 0; |
| 1803 | char *zTmp = 0; |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1804 | int nRow = 0; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1805 | |
| 1806 | zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0); |
| 1807 | zTableInfo = appendText(zTableInfo, zTable, '"'); |
| 1808 | zTableInfo = appendText(zTableInfo, ");", 0); |
| 1809 | |
| 1810 | rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1811 | free(zTableInfo); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1812 | if( rc!=SQLITE_OK || !pTableInfo ){ |
| 1813 | return 1; |
| 1814 | } |
| 1815 | |
| 1816 | zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0); |
| 1817 | zTmp = appendText(zTmp, zTable, '"'); |
| 1818 | if( zTmp ){ |
| 1819 | zSelect = appendText(zSelect, zTmp, '\''); |
| 1820 | } |
| 1821 | zSelect = appendText(zSelect, " || ' VALUES(' || ", 0); |
| 1822 | rc = sqlite3_step(pTableInfo); |
| 1823 | while( rc==SQLITE_ROW ){ |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1824 | const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1); |
danielk1977 | 3f41e97 | 2004-06-08 00:39:01 +0000 | [diff] [blame] | 1825 | zSelect = appendText(zSelect, "quote(", 0); |
danielk1977 | 2e588c7 | 2005-12-09 14:25:08 +0000 | [diff] [blame] | 1826 | zSelect = appendText(zSelect, zText, '"'); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1827 | rc = sqlite3_step(pTableInfo); |
| 1828 | if( rc==SQLITE_ROW ){ |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1829 | zSelect = appendText(zSelect, ") || ',' || ", 0); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1830 | }else{ |
| 1831 | zSelect = appendText(zSelect, ") ", 0); |
| 1832 | } |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1833 | nRow++; |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1834 | } |
| 1835 | rc = sqlite3_finalize(pTableInfo); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1836 | if( rc!=SQLITE_OK || nRow==0 ){ |
| 1837 | free(zSelect); |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1838 | return 1; |
| 1839 | } |
| 1840 | zSelect = appendText(zSelect, "|| ')' FROM ", 0); |
| 1841 | zSelect = appendText(zSelect, zTable, '"'); |
| 1842 | |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1843 | rc = run_table_dump_query(p->out, p->db, zSelect, zPrepStmt); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1844 | if( rc==SQLITE_CORRUPT ){ |
| 1845 | zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0); |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 1846 | rc = run_table_dump_query(p->out, p->db, zSelect, 0); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1847 | } |
danielk1977 | 2a02e33 | 2004-06-05 08:04:36 +0000 | [diff] [blame] | 1848 | if( zSelect ) free(zSelect); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1849 | } |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 1850 | return 0; |
| 1851 | } |
| 1852 | |
| 1853 | /* |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 1854 | ** Run zQuery. Use dump_callback() as the callback routine so that |
| 1855 | ** the contents of the query are output as SQL statements. |
| 1856 | ** |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1857 | ** If we get a SQLITE_CORRUPT error, rerun the query after appending |
| 1858 | ** "ORDER BY rowid DESC" to the end. |
| 1859 | */ |
| 1860 | static int run_schema_dump_query( |
| 1861 | struct callback_data *p, |
| 1862 | const char *zQuery, |
| 1863 | char **pzErrMsg |
| 1864 | ){ |
| 1865 | int rc; |
| 1866 | rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg); |
| 1867 | if( rc==SQLITE_CORRUPT ){ |
| 1868 | char *zQ2; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 1869 | int len = strlen30(zQuery); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1870 | if( pzErrMsg ) sqlite3_free(*pzErrMsg); |
| 1871 | zQ2 = malloc( len+100 ); |
| 1872 | if( zQ2==0 ) return rc; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 1873 | sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery); |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1874 | rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg); |
| 1875 | free(zQ2); |
| 1876 | } |
| 1877 | return rc; |
| 1878 | } |
| 1879 | |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 1880 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_SUBQUERY) |
| 1881 | struct GenfkeyCmd { |
| 1882 | sqlite3 *db; /* Database handle */ |
| 1883 | struct callback_data *pCb; /* Callback data */ |
| 1884 | int isIgnoreErrors; /* True for --ignore-errors */ |
| 1885 | int isExec; /* True for --exec */ |
| 1886 | int isNoDrop; /* True for --no-drop */ |
| 1887 | int nErr; /* Number of errors seen so far */ |
| 1888 | }; |
| 1889 | typedef struct GenfkeyCmd GenfkeyCmd; |
| 1890 | |
| 1891 | static int genfkeyParseArgs(GenfkeyCmd *p, char **azArg, int nArg){ |
| 1892 | int ii; |
| 1893 | memset(p, 0, sizeof(GenfkeyCmd)); |
| 1894 | |
| 1895 | for(ii=0; ii<nArg; ii++){ |
drh | 93a989c | 2009-03-16 10:59:44 +0000 | [diff] [blame] | 1896 | int n = strlen30(azArg[ii]); |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 1897 | |
| 1898 | if( n>2 && n<10 && 0==strncmp(azArg[ii], "--no-drop", n) ){ |
| 1899 | p->isNoDrop = 1; |
| 1900 | }else if( n>2 && n<16 && 0==strncmp(azArg[ii], "--ignore-errors", n) ){ |
| 1901 | p->isIgnoreErrors = 1; |
| 1902 | }else if( n>2 && n<7 && 0==strncmp(azArg[ii], "--exec", n) ){ |
| 1903 | p->isExec = 1; |
| 1904 | }else{ |
| 1905 | fprintf(stderr, "unknown option: %s\n", azArg[ii]); |
| 1906 | return -1; |
| 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | return SQLITE_OK; |
| 1911 | } |
| 1912 | |
| 1913 | static int genfkeyCmdCb(void *pCtx, int eType, const char *z){ |
| 1914 | GenfkeyCmd *p = (GenfkeyCmd *)pCtx; |
| 1915 | if( eType==GENFKEY_ERROR && !p->isIgnoreErrors ){ |
| 1916 | p->nErr++; |
| 1917 | fprintf(stderr, "%s\n", z); |
| 1918 | } |
| 1919 | |
| 1920 | if( p->nErr==0 && ( |
| 1921 | (eType==GENFKEY_CREATETRIGGER) |
| 1922 | || (eType==GENFKEY_DROPTRIGGER && !p->isNoDrop) |
| 1923 | )){ |
| 1924 | if( p->isExec ){ |
| 1925 | sqlite3_exec(p->db, z, 0, 0, 0); |
| 1926 | }else{ |
| 1927 | char *zCol = "sql"; |
| 1928 | callback((void *)p->pCb, 1, (char **)&z, (char **)&zCol); |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | return SQLITE_OK; |
| 1933 | } |
| 1934 | #endif |
| 1935 | |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 1936 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1937 | ** Text of a help message |
| 1938 | */ |
persicom | 1d0b872 | 2002-04-18 02:53:04 +0000 | [diff] [blame] | 1939 | static char zHelp[] = |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 1940 | ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" |
drh | 20f99c4 | 2007-01-08 14:31:35 +0000 | [diff] [blame] | 1941 | ".bail ON|OFF Stop after hitting an error. Default OFF\n" |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 1942 | ".databases List names and files of attached databases\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 1943 | ".dump ?TABLE? ... Dump the database in an SQL text format\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1944 | ".echo ON|OFF Turn command echo on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1945 | ".exit Exit this program\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1946 | ".explain ON|OFF Turn output mode suitable for EXPLAIN on or off.\n" |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 1947 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_SUBQUERY) |
| 1948 | ".genfkey ?OPTIONS? Options are:\n" |
| 1949 | " --no-drop: Do not drop old fkey triggers.\n" |
| 1950 | " --ignore-errors: Ignore tables with fkey errors\n" |
| 1951 | " --exec: Execute generated SQL immediately\n" |
danielk1977 | e632004 | 2009-02-25 15:43:57 +0000 | [diff] [blame] | 1952 | " See file tool/genfkey.README in the source \n" |
| 1953 | " distribution for further information.\n" |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 1954 | #endif |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1955 | ".header(s) ON|OFF Turn display of headers on or off\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1956 | ".help Show this message\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 1957 | ".import FILE TABLE Import data from FILE into TABLE\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1958 | ".indices TABLE Show names of all indices on TABLE\n" |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 1959 | #ifdef SQLITE_ENABLE_IOTRACE |
| 1960 | ".iotrace FILE Enable I/O diagnostic logging to FILE\n" |
| 1961 | #endif |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 1962 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 1963 | ".load FILE ?ENTRY? Load an extension library\n" |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 1964 | #endif |
danielk1977 | 6b77a36 | 2005-01-13 11:10:25 +0000 | [diff] [blame] | 1965 | ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" |
drh | 3b584fa | 2004-09-24 12:50:03 +0000 | [diff] [blame] | 1966 | " csv Comma-separated values\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 1967 | " column Left-aligned columns. (See .width)\n" |
| 1968 | " html HTML <table> code\n" |
| 1969 | " insert SQL insert statements for TABLE\n" |
| 1970 | " line One value per line\n" |
| 1971 | " list Values delimited by .separator string\n" |
| 1972 | " tabs Tab-separated values\n" |
| 1973 | " tcl TCL list elements\n" |
| 1974 | ".nullvalue STRING Print STRING in place of NULL values\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1975 | ".output FILENAME Send output to FILENAME\n" |
| 1976 | ".output stdout Send output to the screen\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1977 | ".prompt MAIN CONTINUE Replace the standard prompts\n" |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 1978 | ".quit Exit this program\n" |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1979 | ".read FILENAME Execute SQL in FILENAME\n" |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 1980 | ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1981 | ".schema ?TABLE? Show the CREATE statements\n" |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 1982 | ".separator STRING Change separator used by output mode and .import\n" |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 1983 | ".show Show the current values for various settings\n" |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 1984 | ".tables ?PATTERN? List names of tables matching a LIKE pattern\n" |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 1985 | ".timeout MS Try opening locked tables for MS milliseconds\n" |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1986 | ".width NUM NUM ... Set column widths for \"column\" mode\n" |
| 1987 | ; |
| 1988 | |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame^] | 1989 | static char zTimerHelp[] = |
| 1990 | ".timer ON|OFF Turn the CPU timer measurement on or off\n" |
| 1991 | ; |
| 1992 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1993 | /* Forward reference */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 1994 | static int process_input(struct callback_data *p, FILE *in); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 1995 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 1996 | /* |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 1997 | ** Make sure the database is open. If it is not, then open it. If |
| 1998 | ** the database fails to open, print an error message and exit. |
| 1999 | */ |
| 2000 | static void open_db(struct callback_data *p){ |
| 2001 | if( p->db==0 ){ |
danielk1977 | 4f057f9 | 2004-06-08 00:02:33 +0000 | [diff] [blame] | 2002 | sqlite3_open(p->zDbFilename, &p->db); |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 2003 | db = p->db; |
drh | 4cea5ba | 2008-05-05 16:27:24 +0000 | [diff] [blame] | 2004 | if( db && sqlite3_errcode(db)==SQLITE_OK ){ |
| 2005 | sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0, |
| 2006 | shellstaticFunc, 0, 0); |
| 2007 | } |
| 2008 | if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){ |
danielk1977 | 8029086 | 2004-05-22 09:21:21 +0000 | [diff] [blame] | 2009 | fprintf(stderr,"Unable to open database \"%s\": %s\n", |
| 2010 | p->zDbFilename, sqlite3_errmsg(db)); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2011 | exit(1); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2012 | } |
drh | c2e87a3 | 2006-06-27 15:16:14 +0000 | [diff] [blame] | 2013 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 2014 | sqlite3_enable_load_extension(p->db, 1); |
| 2015 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | /* |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2020 | ** Do C-language style dequoting. |
| 2021 | ** |
| 2022 | ** \t -> tab |
| 2023 | ** \n -> newline |
| 2024 | ** \r -> carriage return |
| 2025 | ** \NNN -> ascii character NNN in octal |
| 2026 | ** \\ -> backslash |
| 2027 | */ |
| 2028 | static void resolve_backslashes(char *z){ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 2029 | int i, j; |
| 2030 | char c; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2031 | for(i=j=0; (c = z[i])!=0; i++, j++){ |
| 2032 | if( c=='\\' ){ |
| 2033 | c = z[++i]; |
| 2034 | if( c=='n' ){ |
| 2035 | c = '\n'; |
| 2036 | }else if( c=='t' ){ |
| 2037 | c = '\t'; |
| 2038 | }else if( c=='r' ){ |
| 2039 | c = '\r'; |
| 2040 | }else if( c>='0' && c<='7' ){ |
drh | aa81608 | 2005-12-29 12:53:09 +0000 | [diff] [blame] | 2041 | c -= '0'; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2042 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 2043 | i++; |
| 2044 | c = (c<<3) + z[i] - '0'; |
| 2045 | if( z[i+1]>='0' && z[i+1]<='7' ){ |
| 2046 | i++; |
| 2047 | c = (c<<3) + z[i] - '0'; |
| 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | } |
| 2052 | z[j] = c; |
| 2053 | } |
| 2054 | z[j] = 0; |
| 2055 | } |
| 2056 | |
| 2057 | /* |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2058 | ** Interpret zArg as a boolean value. Return either 0 or 1. |
| 2059 | */ |
| 2060 | static int booleanValue(char *zArg){ |
| 2061 | int val = atoi(zArg); |
| 2062 | int j; |
| 2063 | for(j=0; zArg[j]; j++){ |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 2064 | zArg[j] = (char)tolower(zArg[j]); |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2065 | } |
| 2066 | if( strcmp(zArg,"on")==0 ){ |
| 2067 | val = 1; |
| 2068 | }else if( strcmp(zArg,"yes")==0 ){ |
| 2069 | val = 1; |
| 2070 | } |
| 2071 | return val; |
| 2072 | } |
| 2073 | |
| 2074 | /* |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2075 | ** If an input line begins with "." then invoke this routine to |
| 2076 | ** process that line. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2077 | ** |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 2078 | ** Return 1 on error, 2 to exit, and 0 otherwise. |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2079 | */ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2080 | static int do_meta_command(char *zLine, struct callback_data *p){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2081 | int i = 1; |
| 2082 | int nArg = 0; |
| 2083 | int n, c; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2084 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2085 | char *azArg[50]; |
| 2086 | |
| 2087 | /* Parse the input line into tokens. |
| 2088 | */ |
| 2089 | while( zLine[i] && nArg<ArraySize(azArg) ){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 2090 | while( isspace((unsigned char)zLine[i]) ){ i++; } |
drh | 0633368 | 2004-03-09 13:37:45 +0000 | [diff] [blame] | 2091 | if( zLine[i]==0 ) break; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2092 | if( zLine[i]=='\'' || zLine[i]=='"' ){ |
| 2093 | int delim = zLine[i++]; |
| 2094 | azArg[nArg++] = &zLine[i]; |
| 2095 | while( zLine[i] && zLine[i]!=delim ){ i++; } |
| 2096 | if( zLine[i]==delim ){ |
| 2097 | zLine[i++] = 0; |
| 2098 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2099 | if( delim=='"' ) resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2100 | }else{ |
| 2101 | azArg[nArg++] = &zLine[i]; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 2102 | while( zLine[i] && !isspace((unsigned char)zLine[i]) ){ i++; } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2103 | if( zLine[i] ) zLine[i++] = 0; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2104 | resolve_backslashes(azArg[nArg-1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | /* Process the input line. |
| 2109 | */ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2110 | if( nArg==0 ) return rc; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2111 | n = strlen30(azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2112 | c = azArg[0][0]; |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 2113 | if( c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0 && nArg>1 ){ |
| 2114 | const char *zDestFile; |
| 2115 | const char *zDb; |
| 2116 | sqlite3 *pDest; |
| 2117 | sqlite3_backup *pBackup; |
| 2118 | int rc; |
| 2119 | if( nArg==2 ){ |
| 2120 | zDestFile = azArg[1]; |
| 2121 | zDb = "main"; |
| 2122 | }else{ |
| 2123 | zDestFile = azArg[2]; |
| 2124 | zDb = azArg[1]; |
| 2125 | } |
| 2126 | rc = sqlite3_open(zDestFile, &pDest); |
| 2127 | if( rc!=SQLITE_OK ){ |
| 2128 | fprintf(stderr, "Error: cannot open %s\n", zDestFile); |
| 2129 | sqlite3_close(pDest); |
| 2130 | return 1; |
| 2131 | } |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 2132 | open_db(p); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 2133 | pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); |
| 2134 | if( pBackup==0 ){ |
| 2135 | fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
| 2136 | sqlite3_close(pDest); |
| 2137 | return 1; |
| 2138 | } |
| 2139 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} |
| 2140 | sqlite3_backup_finish(pBackup); |
| 2141 | if( rc==SQLITE_DONE ){ |
| 2142 | rc = SQLITE_OK; |
| 2143 | }else{ |
| 2144 | fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); |
| 2145 | } |
| 2146 | sqlite3_close(pDest); |
| 2147 | }else |
| 2148 | |
| 2149 | if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2150 | bail_on_error = booleanValue(azArg[1]); |
| 2151 | }else |
| 2152 | |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 2153 | if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){ |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 2154 | struct callback_data data; |
| 2155 | char *zErrMsg = 0; |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 2156 | open_db(p); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 2157 | memcpy(&data, p, sizeof(data)); |
drh | d888544 | 2004-03-17 23:42:12 +0000 | [diff] [blame] | 2158 | data.showHeader = 1; |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 2159 | data.mode = MODE_Column; |
drh | d888544 | 2004-03-17 23:42:12 +0000 | [diff] [blame] | 2160 | data.colWidth[0] = 3; |
| 2161 | data.colWidth[1] = 15; |
| 2162 | data.colWidth[2] = 58; |
drh | 0b2110c | 2004-10-26 00:08:10 +0000 | [diff] [blame] | 2163 | data.cnt = 0; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2164 | sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg); |
jplyon | 672a1ed | 2003-05-11 20:07:05 +0000 | [diff] [blame] | 2165 | if( zErrMsg ){ |
| 2166 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 2167 | sqlite3_free(zErrMsg); |
jplyon | 6a65bb3 | 2003-05-04 07:25:57 +0000 | [diff] [blame] | 2168 | } |
| 2169 | }else |
| 2170 | |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2171 | if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){ |
| 2172 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2173 | open_db(p); |
drh | f1dfc4f | 2009-09-23 15:51:35 +0000 | [diff] [blame] | 2174 | /* When playing back a "dump", the content might appear in an order |
| 2175 | ** which causes immediate foreign key constraints to be violated. |
| 2176 | ** So disable foreign-key constraint enforcement to prevent problems. */ |
| 2177 | fprintf(p->out, "PRAGMA foreign_keys=OFF;\n"); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2178 | fprintf(p->out, "BEGIN TRANSACTION;\n"); |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2179 | p->writableSchema = 0; |
drh | 93f41e5 | 2008-08-11 19:12:34 +0000 | [diff] [blame] | 2180 | sqlite3_exec(p->db, "PRAGMA writable_schema=ON", 0, 0, 0); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2181 | if( nArg==1 ){ |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2182 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2183 | "SELECT name, type, sql FROM sqlite_master " |
drh | 4f32476 | 2009-05-21 14:51:03 +0000 | [diff] [blame] | 2184 | "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'", 0 |
| 2185 | ); |
| 2186 | run_schema_dump_query(p, |
| 2187 | "SELECT name, type, sql FROM sqlite_master " |
| 2188 | "WHERE name=='sqlite_sequence'", 0 |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 2189 | ); |
| 2190 | run_table_dump_query(p->out, p->db, |
| 2191 | "SELECT sql FROM sqlite_master " |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2192 | "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2193 | ); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2194 | }else{ |
| 2195 | int i; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2196 | for(i=1; i<nArg; i++){ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2197 | zShellStatic = azArg[i]; |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2198 | run_schema_dump_query(p, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2199 | "SELECT name, type, sql FROM sqlite_master " |
drh | dd3d459 | 2004-08-30 01:54:05 +0000 | [diff] [blame] | 2200 | "WHERE tbl_name LIKE shellstatic() AND type=='table'" |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2201 | " AND sql NOT NULL", 0); |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 2202 | run_table_dump_query(p->out, p->db, |
| 2203 | "SELECT sql FROM sqlite_master " |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2204 | "WHERE sql NOT NULL" |
| 2205 | " AND type IN ('index','trigger','view')" |
drh | 157e29a | 2009-05-21 15:15:00 +0000 | [diff] [blame] | 2206 | " AND tbl_name LIKE shellstatic()", 0 |
drh | 0b9a594 | 2006-09-13 20:22:02 +0000 | [diff] [blame] | 2207 | ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2208 | zShellStatic = 0; |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2209 | } |
| 2210 | } |
drh | 45e29d8 | 2006-11-20 16:21:10 +0000 | [diff] [blame] | 2211 | if( p->writableSchema ){ |
| 2212 | fprintf(p->out, "PRAGMA writable_schema=OFF;\n"); |
| 2213 | p->writableSchema = 0; |
| 2214 | } |
drh | 93f41e5 | 2008-08-11 19:12:34 +0000 | [diff] [blame] | 2215 | sqlite3_exec(p->db, "PRAGMA writable_schema=OFF", 0, 0, 0); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2216 | if( zErrMsg ){ |
| 2217 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 2218 | sqlite3_free(zErrMsg); |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2219 | }else{ |
| 2220 | fprintf(p->out, "COMMIT;\n"); |
drh | 4c653a0 | 2000-06-07 01:27:47 +0000 | [diff] [blame] | 2221 | } |
| 2222 | }else |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2223 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2224 | if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2225 | p->echoOn = booleanValue(azArg[1]); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2226 | }else |
| 2227 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2228 | if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 2229 | rc = 2; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2230 | }else |
| 2231 | |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 2232 | if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2233 | int val = nArg>=2 ? booleanValue(azArg[1]) : 1; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2234 | if(val == 1) { |
| 2235 | if(!p->explainPrev.valid) { |
| 2236 | p->explainPrev.valid = 1; |
| 2237 | p->explainPrev.mode = p->mode; |
| 2238 | p->explainPrev.showHeader = p->showHeader; |
| 2239 | memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth)); |
| 2240 | } |
| 2241 | /* We could put this code under the !p->explainValid |
| 2242 | ** condition so that it does not execute if we are already in |
| 2243 | ** explain mode. However, always executing it allows us an easy |
| 2244 | ** was to reset to explain mode in case the user previously |
| 2245 | ** did an .explain followed by a .width, .mode or .header |
| 2246 | ** command. |
| 2247 | */ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 2248 | p->mode = MODE_Explain; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2249 | p->showHeader = 1; |
| 2250 | memset(p->colWidth,0,ArraySize(p->colWidth)); |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 2251 | p->colWidth[0] = 4; /* addr */ |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2252 | p->colWidth[1] = 13; /* opcode */ |
| 2253 | p->colWidth[2] = 4; /* P1 */ |
| 2254 | p->colWidth[3] = 4; /* P2 */ |
| 2255 | p->colWidth[4] = 4; /* P3 */ |
| 2256 | p->colWidth[5] = 13; /* P4 */ |
danielk1977 | 0d78bae | 2008-01-03 07:09:48 +0000 | [diff] [blame] | 2257 | p->colWidth[6] = 2; /* P5 */ |
drh | 60a713c | 2008-01-21 16:22:45 +0000 | [diff] [blame] | 2258 | p->colWidth[7] = 13; /* Comment */ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2259 | }else if (p->explainPrev.valid) { |
| 2260 | p->explainPrev.valid = 0; |
| 2261 | p->mode = p->explainPrev.mode; |
| 2262 | p->showHeader = p->explainPrev.showHeader; |
| 2263 | memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth)); |
| 2264 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2265 | }else |
| 2266 | |
danielk1977 | c8c7069 | 2009-02-25 15:22:02 +0000 | [diff] [blame] | 2267 | #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_SUBQUERY) |
| 2268 | if( c=='g' && strncmp(azArg[0], "genfkey", n)==0 ){ |
| 2269 | GenfkeyCmd cmd; |
| 2270 | if( 0==genfkeyParseArgs(&cmd, &azArg[1], nArg-1) ){ |
| 2271 | cmd.db = p->db; |
| 2272 | cmd.pCb = p; |
| 2273 | genfkey_create_triggers(p->db, "main", (void *)&cmd, genfkeyCmdCb); |
| 2274 | } |
| 2275 | }else |
| 2276 | #endif |
| 2277 | |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2278 | if( c=='h' && (strncmp(azArg[0], "header", n)==0 || |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2279 | strncmp(azArg[0], "headers", n)==0 )&& nArg>1 ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2280 | p->showHeader = booleanValue(azArg[1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2281 | }else |
| 2282 | |
| 2283 | if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ |
drh | a81c64a | 2009-01-14 23:38:02 +0000 | [diff] [blame] | 2284 | fprintf(stderr,"%s",zHelp); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame^] | 2285 | if( HAS_TIMER ){ |
| 2286 | fprintf(stderr,"%s",zTimerHelp); |
| 2287 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2288 | }else |
| 2289 | |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2290 | if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg>=3 ){ |
| 2291 | char *zTable = azArg[2]; /* Insert data into this table */ |
| 2292 | char *zFile = azArg[1]; /* The file from which to extract data */ |
| 2293 | sqlite3_stmt *pStmt; /* A statement */ |
| 2294 | int rc; /* Result code */ |
| 2295 | int nCol; /* Number of columns in the table */ |
| 2296 | int nByte; /* Number of bytes in an SQL string */ |
| 2297 | int i, j; /* Loop counters */ |
| 2298 | int nSep; /* Number of bytes in p->separator[] */ |
| 2299 | char *zSql; /* An SQL statement */ |
| 2300 | char *zLine; /* A single line of input from the file */ |
| 2301 | char **azCol; /* zLine[] broken up into columns */ |
| 2302 | char *zCommit; /* How to commit changes */ |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 2303 | FILE *in; /* The input file */ |
| 2304 | int lineno = 0; /* Line number of input file */ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2305 | |
drh | a543c82 | 2006-06-08 16:10:14 +0000 | [diff] [blame] | 2306 | open_db(p); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2307 | nSep = strlen30(p->separator); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2308 | if( nSep==0 ){ |
| 2309 | fprintf(stderr, "non-null separator required for import\n"); |
| 2310 | return 0; |
| 2311 | } |
| 2312 | zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); |
| 2313 | if( zSql==0 ) return 0; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2314 | nByte = strlen30(zSql); |
drh | 5e6078b | 2006-01-31 19:07:22 +0000 | [diff] [blame] | 2315 | rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2316 | sqlite3_free(zSql); |
| 2317 | if( rc ){ |
| 2318 | fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db)); |
| 2319 | nCol = 0; |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 2320 | rc = 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2321 | }else{ |
| 2322 | nCol = sqlite3_column_count(pStmt); |
| 2323 | } |
| 2324 | sqlite3_finalize(pStmt); |
| 2325 | if( nCol==0 ) return 0; |
| 2326 | zSql = malloc( nByte + 20 + nCol*2 ); |
| 2327 | if( zSql==0 ) return 0; |
| 2328 | sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable); |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2329 | j = strlen30(zSql); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2330 | for(i=1; i<nCol; i++){ |
| 2331 | zSql[j++] = ','; |
| 2332 | zSql[j++] = '?'; |
| 2333 | } |
| 2334 | zSql[j++] = ')'; |
| 2335 | zSql[j] = 0; |
drh | 5e6078b | 2006-01-31 19:07:22 +0000 | [diff] [blame] | 2336 | rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2337 | free(zSql); |
| 2338 | if( rc ){ |
| 2339 | fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db)); |
| 2340 | sqlite3_finalize(pStmt); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 2341 | return 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2342 | } |
| 2343 | in = fopen(zFile, "rb"); |
| 2344 | if( in==0 ){ |
| 2345 | fprintf(stderr, "cannot open file: %s\n", zFile); |
| 2346 | sqlite3_finalize(pStmt); |
| 2347 | return 0; |
| 2348 | } |
| 2349 | azCol = malloc( sizeof(azCol[0])*(nCol+1) ); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 2350 | if( azCol==0 ){ |
| 2351 | fclose(in); |
| 2352 | return 0; |
| 2353 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2354 | sqlite3_exec(p->db, "BEGIN", 0, 0, 0); |
| 2355 | zCommit = "COMMIT"; |
| 2356 | while( (zLine = local_getline(0, in))!=0 ){ |
| 2357 | char *z; |
| 2358 | i = 0; |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 2359 | lineno++; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2360 | azCol[0] = zLine; |
drh | 36d4e97 | 2004-10-06 14:39:06 +0000 | [diff] [blame] | 2361 | for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){ |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2362 | if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){ |
| 2363 | *z = 0; |
| 2364 | i++; |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 2365 | if( i<nCol ){ |
| 2366 | azCol[i] = &z[nSep]; |
| 2367 | z += nSep-1; |
| 2368 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2369 | } |
| 2370 | } |
drh | 1cd7f83 | 2005-08-05 18:50:51 +0000 | [diff] [blame] | 2371 | *z = 0; |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 2372 | if( i+1!=nCol ){ |
| 2373 | fprintf(stderr,"%s line %d: expected %d columns of data but found %d\n", |
| 2374 | zFile, lineno, nCol, i+1); |
| 2375 | zCommit = "ROLLBACK"; |
drh | 1822eee | 2008-12-04 12:26:00 +0000 | [diff] [blame] | 2376 | free(zLine); |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 2377 | break; |
| 2378 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2379 | for(i=0; i<nCol; i++){ |
| 2380 | sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC); |
| 2381 | } |
| 2382 | sqlite3_step(pStmt); |
| 2383 | rc = sqlite3_reset(pStmt); |
| 2384 | free(zLine); |
| 2385 | if( rc!=SQLITE_OK ){ |
| 2386 | fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db)); |
| 2387 | zCommit = "ROLLBACK"; |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 2388 | rc = 1; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2389 | break; |
| 2390 | } |
| 2391 | } |
| 2392 | free(azCol); |
| 2393 | fclose(in); |
| 2394 | sqlite3_finalize(pStmt); |
drh | b860bc9 | 2004-08-04 15:16:55 +0000 | [diff] [blame] | 2395 | sqlite3_exec(p->db, zCommit, 0, 0, 0); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2396 | }else |
| 2397 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2398 | if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg>1 ){ |
| 2399 | struct callback_data data; |
| 2400 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2401 | open_db(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2402 | memcpy(&data, p, sizeof(data)); |
| 2403 | data.showHeader = 0; |
| 2404 | data.mode = MODE_List; |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2405 | zShellStatic = azArg[1]; |
| 2406 | sqlite3_exec(p->db, |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2407 | "SELECT name FROM sqlite_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2408 | "WHERE type='index' AND tbl_name LIKE shellstatic() " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2409 | "UNION ALL " |
| 2410 | "SELECT name FROM sqlite_temp_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2411 | "WHERE type='index' AND tbl_name LIKE shellstatic() " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2412 | "ORDER BY 1", |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2413 | callback, &data, &zErrMsg |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2414 | ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2415 | zShellStatic = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2416 | if( zErrMsg ){ |
| 2417 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 2418 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2419 | } |
| 2420 | }else |
| 2421 | |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 2422 | #ifdef SQLITE_ENABLE_IOTRACE |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2423 | if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 2424 | extern void (*sqlite3IoTrace)(const char*, ...); |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2425 | if( iotrace && iotrace!=stdout ) fclose(iotrace); |
| 2426 | iotrace = 0; |
| 2427 | if( nArg<2 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 2428 | sqlite3IoTrace = 0; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2429 | }else if( strcmp(azArg[1], "-")==0 ){ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 2430 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2431 | iotrace = stdout; |
| 2432 | }else{ |
| 2433 | iotrace = fopen(azArg[1], "w"); |
| 2434 | if( iotrace==0 ){ |
| 2435 | fprintf(stderr, "cannot open \"%s\"\n", azArg[1]); |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 2436 | sqlite3IoTrace = 0; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2437 | }else{ |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 2438 | sqlite3IoTrace = iotracePrintf; |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2439 | } |
| 2440 | } |
| 2441 | }else |
drh | ae5e445 | 2007-05-03 17:18:36 +0000 | [diff] [blame] | 2442 | #endif |
drh | b060341 | 2007-02-28 04:47:26 +0000 | [diff] [blame] | 2443 | |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 2444 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 2445 | if( c=='l' && strncmp(azArg[0], "load", n)==0 && nArg>=2 ){ |
| 2446 | const char *zFile, *zProc; |
| 2447 | char *zErrMsg = 0; |
| 2448 | int rc; |
| 2449 | zFile = azArg[1]; |
| 2450 | zProc = nArg>=3 ? azArg[2] : 0; |
| 2451 | open_db(p); |
| 2452 | rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); |
| 2453 | if( rc!=SQLITE_OK ){ |
| 2454 | fprintf(stderr, "%s\n", zErrMsg); |
| 2455 | sqlite3_free(zErrMsg); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 2456 | rc = 1; |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 2457 | } |
| 2458 | }else |
drh | 70df4fe | 2006-06-13 15:12:21 +0000 | [diff] [blame] | 2459 | #endif |
drh | 1e397f8 | 2006-06-08 15:28:43 +0000 | [diff] [blame] | 2460 | |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2461 | if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2462 | int n2 = strlen30(azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2463 | if( strncmp(azArg[1],"line",n2)==0 |
| 2464 | || |
| 2465 | strncmp(azArg[1],"lines",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2466 | p->mode = MODE_Line; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2467 | }else if( strncmp(azArg[1],"column",n2)==0 |
| 2468 | || |
| 2469 | strncmp(azArg[1],"columns",n2)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2470 | p->mode = MODE_Column; |
| 2471 | }else if( strncmp(azArg[1],"list",n2)==0 ){ |
| 2472 | p->mode = MODE_List; |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 2473 | }else if( strncmp(azArg[1],"html",n2)==0 ){ |
| 2474 | p->mode = MODE_Html; |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2475 | }else if( strncmp(azArg[1],"tcl",n2)==0 ){ |
| 2476 | p->mode = MODE_Tcl; |
| 2477 | }else if( strncmp(azArg[1],"csv",n2)==0 ){ |
drh | 8e64d1c | 2004-10-07 00:32:39 +0000 | [diff] [blame] | 2478 | p->mode = MODE_Csv; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2479 | sqlite3_snprintf(sizeof(p->separator), p->separator, ","); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2480 | }else if( strncmp(azArg[1],"tabs",n2)==0 ){ |
| 2481 | p->mode = MODE_List; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2482 | sqlite3_snprintf(sizeof(p->separator), p->separator, "\t"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2483 | }else if( strncmp(azArg[1],"insert",n2)==0 ){ |
| 2484 | p->mode = MODE_Insert; |
| 2485 | if( nArg>=3 ){ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2486 | set_table_name(p, azArg[2]); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2487 | }else{ |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 2488 | set_table_name(p, "table"); |
drh | 28bd4bc | 2000-06-15 15:57:22 +0000 | [diff] [blame] | 2489 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2490 | }else { |
drh | cf68ae9 | 2006-12-19 18:47:41 +0000 | [diff] [blame] | 2491 | fprintf(stderr,"mode should be one of: " |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2492 | "column csv html insert line list tabs tcl\n"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2493 | } |
| 2494 | }else |
| 2495 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2496 | if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) { |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2497 | sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue, |
| 2498 | "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2499 | }else |
| 2500 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2501 | if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){ |
| 2502 | if( p->out!=stdout ){ |
| 2503 | fclose(p->out); |
| 2504 | } |
| 2505 | if( strcmp(azArg[1],"stdout")==0 ){ |
| 2506 | p->out = stdout; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2507 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "stdout"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2508 | }else{ |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 2509 | p->out = fopen(azArg[1], "wb"); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2510 | if( p->out==0 ){ |
| 2511 | fprintf(stderr,"can't write to \"%s\"\n", azArg[1]); |
| 2512 | p->out = stdout; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2513 | } else { |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2514 | sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2515 | } |
| 2516 | } |
| 2517 | }else |
| 2518 | |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 2519 | if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2520 | if( nArg >= 2) { |
| 2521 | strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); |
| 2522 | } |
| 2523 | if( nArg >= 3) { |
| 2524 | strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); |
| 2525 | } |
| 2526 | }else |
| 2527 | |
| 2528 | if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){ |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 2529 | rc = 2; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2530 | }else |
| 2531 | |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 2532 | if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 && nArg==2 ){ |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 2533 | FILE *alt = fopen(azArg[1], "rb"); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2534 | if( alt==0 ){ |
| 2535 | fprintf(stderr,"can't open \"%s\"\n", azArg[1]); |
| 2536 | }else{ |
| 2537 | process_input(p, alt); |
| 2538 | fclose(alt); |
| 2539 | } |
| 2540 | }else |
| 2541 | |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 2542 | if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 && nArg>1 ){ |
| 2543 | const char *zSrcFile; |
| 2544 | const char *zDb; |
| 2545 | sqlite3 *pSrc; |
| 2546 | sqlite3_backup *pBackup; |
| 2547 | int rc; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 2548 | int nTimeout = 0; |
| 2549 | |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 2550 | if( nArg==2 ){ |
| 2551 | zSrcFile = azArg[1]; |
| 2552 | zDb = "main"; |
| 2553 | }else{ |
| 2554 | zSrcFile = azArg[2]; |
| 2555 | zDb = azArg[1]; |
| 2556 | } |
| 2557 | rc = sqlite3_open(zSrcFile, &pSrc); |
| 2558 | if( rc!=SQLITE_OK ){ |
| 2559 | fprintf(stderr, "Error: cannot open %s\n", zSrcFile); |
| 2560 | sqlite3_close(pSrc); |
| 2561 | return 1; |
| 2562 | } |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 2563 | open_db(p); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 2564 | pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); |
| 2565 | if( pBackup==0 ){ |
| 2566 | fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 2567 | sqlite3_close(pSrc); |
| 2568 | return 1; |
| 2569 | } |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 2570 | while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK |
| 2571 | || rc==SQLITE_BUSY ){ |
| 2572 | if( rc==SQLITE_BUSY ){ |
| 2573 | if( nTimeout++ >= 3 ) break; |
| 2574 | sqlite3_sleep(100); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 2575 | } |
| 2576 | } |
| 2577 | sqlite3_backup_finish(pBackup); |
| 2578 | if( rc==SQLITE_DONE ){ |
| 2579 | rc = SQLITE_OK; |
drh | dc2c491 | 2009-02-04 22:46:47 +0000 | [diff] [blame] | 2580 | }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ |
| 2581 | fprintf(stderr, "source database is busy\n"); |
drh | 9ff849f | 2009-02-04 20:55:57 +0000 | [diff] [blame] | 2582 | }else{ |
| 2583 | fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); |
| 2584 | } |
| 2585 | sqlite3_close(pSrc); |
| 2586 | }else |
| 2587 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2588 | if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){ |
| 2589 | struct callback_data data; |
| 2590 | char *zErrMsg = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2591 | open_db(p); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2592 | memcpy(&data, p, sizeof(data)); |
| 2593 | data.showHeader = 0; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2594 | data.mode = MODE_Semi; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2595 | if( nArg>1 ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2596 | int i; |
shane | 7d3846a | 2008-12-11 02:58:26 +0000 | [diff] [blame] | 2597 | for(i=0; azArg[1][i]; i++) azArg[1][i] = (char)tolower(azArg[1][i]); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2598 | if( strcmp(azArg[1],"sqlite_master")==0 ){ |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2599 | char *new_argv[2], *new_colv[2]; |
| 2600 | new_argv[0] = "CREATE TABLE sqlite_master (\n" |
| 2601 | " type text,\n" |
| 2602 | " name text,\n" |
| 2603 | " tbl_name text,\n" |
drh | adbca9c | 2001-09-27 15:11:53 +0000 | [diff] [blame] | 2604 | " rootpage integer,\n" |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2605 | " sql text\n" |
| 2606 | ")"; |
| 2607 | new_argv[1] = 0; |
| 2608 | new_colv[0] = "sql"; |
| 2609 | new_colv[1] = 0; |
| 2610 | callback(&data, 1, new_argv, new_colv); |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2611 | }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2612 | char *new_argv[2], *new_colv[2]; |
| 2613 | new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" |
| 2614 | " type text,\n" |
| 2615 | " name text,\n" |
| 2616 | " tbl_name text,\n" |
| 2617 | " rootpage integer,\n" |
| 2618 | " sql text\n" |
| 2619 | ")"; |
| 2620 | new_argv[1] = 0; |
| 2621 | new_colv[0] = "sql"; |
| 2622 | new_colv[1] = 0; |
| 2623 | callback(&data, 1, new_argv, new_colv); |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2624 | }else{ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2625 | zShellStatic = azArg[1]; |
| 2626 | sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2627 | "SELECT sql FROM " |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 2628 | " (SELECT sql sql, type type, tbl_name tbl_name, name name" |
| 2629 | " FROM sqlite_master UNION ALL" |
| 2630 | " SELECT sql, type, tbl_name, name FROM sqlite_temp_master) " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2631 | "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2632 | "ORDER BY substr(type,2,1), name", |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2633 | callback, &data, &zErrMsg); |
| 2634 | zShellStatic = 0; |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2635 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2636 | }else{ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2637 | sqlite3_exec(p->db, |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2638 | "SELECT sql FROM " |
drh | 8f800a7 | 2009-01-14 23:17:55 +0000 | [diff] [blame] | 2639 | " (SELECT sql sql, type type, tbl_name tbl_name, name name" |
| 2640 | " FROM sqlite_master UNION ALL" |
| 2641 | " SELECT sql, type, tbl_name, name FROM sqlite_temp_master) " |
drh | 0c35667 | 2005-09-10 22:40:53 +0000 | [diff] [blame] | 2642 | "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'" |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2643 | "ORDER BY substr(type,2,1), name", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2644 | callback, &data, &zErrMsg |
| 2645 | ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2646 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2647 | if( zErrMsg ){ |
| 2648 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 2649 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2650 | } |
| 2651 | }else |
| 2652 | |
| 2653 | if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2654 | sqlite3_snprintf(sizeof(p->separator), p->separator, |
| 2655 | "%.*s", (int)sizeof(p->separator)-1, azArg[1]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2656 | }else |
| 2657 | |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2658 | if( c=='s' && strncmp(azArg[0], "show", n)==0){ |
| 2659 | int i; |
| 2660 | fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2661 | fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off"); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 2662 | fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2663 | fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2664 | fprintf(p->out,"%9.9s: ", "nullvalue"); |
| 2665 | output_c_string(p->out, p->nullvalue); |
| 2666 | fprintf(p->out, "\n"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2667 | fprintf(p->out,"%9.9s: %s\n","output", |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2668 | strlen30(p->outfile) ? p->outfile : "stdout"); |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2669 | fprintf(p->out,"%9.9s: ", "separator"); |
| 2670 | output_c_string(p->out, p->separator); |
| 2671 | fprintf(p->out, "\n"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2672 | fprintf(p->out,"%9.9s: ","width"); |
| 2673 | for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2674 | fprintf(p->out,"%d ",p->colWidth[i]); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2675 | } |
drh | feac5f8 | 2004-08-01 00:10:45 +0000 | [diff] [blame] | 2676 | fprintf(p->out,"\n"); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2677 | }else |
| 2678 | |
drh | 2dfbbca | 2000-07-28 14:32:48 +0000 | [diff] [blame] | 2679 | if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 ){ |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2680 | char **azResult; |
| 2681 | int nRow, rc; |
| 2682 | char *zErrMsg; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2683 | open_db(p); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 2684 | if( nArg==1 ){ |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2685 | rc = sqlite3_get_table(p->db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 2686 | "SELECT name FROM sqlite_master " |
drh | 0c35667 | 2005-09-10 22:40:53 +0000 | [diff] [blame] | 2687 | "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'" |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2688 | "UNION ALL " |
| 2689 | "SELECT name FROM sqlite_temp_master " |
| 2690 | "WHERE type IN ('table','view') " |
| 2691 | "ORDER BY 1", |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2692 | &azResult, &nRow, 0, &zErrMsg |
| 2693 | ); |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 2694 | }else{ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2695 | zShellStatic = azArg[1]; |
| 2696 | rc = sqlite3_get_table(p->db, |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 2697 | "SELECT name FROM sqlite_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2698 | "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2699 | "UNION ALL " |
| 2700 | "SELECT name FROM sqlite_temp_master " |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2701 | "WHERE type IN ('table','view') AND name LIKE '%'||shellstatic()||'%' " |
drh | e0bc404 | 2002-06-25 01:09:11 +0000 | [diff] [blame] | 2702 | "ORDER BY 1", |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2703 | &azResult, &nRow, 0, &zErrMsg |
drh | a18c568 | 2000-10-08 22:20:57 +0000 | [diff] [blame] | 2704 | ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 2705 | zShellStatic = 0; |
drh | a50da10 | 2000-08-08 20:19:09 +0000 | [diff] [blame] | 2706 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2707 | if( zErrMsg ){ |
| 2708 | fprintf(stderr,"Error: %s\n", zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 2709 | sqlite3_free(zErrMsg); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2710 | } |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2711 | if( rc==SQLITE_OK ){ |
| 2712 | int len, maxlen = 0; |
| 2713 | int i, j; |
| 2714 | int nPrintCol, nPrintRow; |
| 2715 | for(i=1; i<=nRow; i++){ |
| 2716 | if( azResult[i]==0 ) continue; |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2717 | len = strlen30(azResult[i]); |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2718 | if( len>maxlen ) maxlen = len; |
| 2719 | } |
| 2720 | nPrintCol = 80/(maxlen+2); |
| 2721 | if( nPrintCol<1 ) nPrintCol = 1; |
| 2722 | nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; |
| 2723 | for(i=0; i<nPrintRow; i++){ |
| 2724 | for(j=i+1; j<=nRow; j+=nPrintRow){ |
| 2725 | char *zSp = j<=nPrintRow ? "" : " "; |
| 2726 | printf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j] : ""); |
| 2727 | } |
| 2728 | printf("\n"); |
| 2729 | } |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 2730 | }else{ |
| 2731 | rc = 1; |
drh | e371033 | 2000-09-29 13:30:53 +0000 | [diff] [blame] | 2732 | } |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2733 | sqlite3_free_table(azResult); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2734 | }else |
| 2735 | |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 2736 | if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2737 | open_db(p); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2738 | sqlite3_busy_timeout(p->db, atoi(azArg[1])); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame^] | 2739 | }else if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 && nArg>1 ){ |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 2740 | enableTimer = booleanValue(azArg[1]); |
shane | b320ccd | 2009-10-21 03:42:58 +0000 | [diff] [blame^] | 2741 | }else if( c=='w' && strncmp(azArg[0], "width", n)==0 ){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2742 | int j; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 2743 | assert( nArg<=ArraySize(azArg) ); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2744 | for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){ |
| 2745 | p->colWidth[j-1] = atoi(azArg[j]); |
| 2746 | } |
| 2747 | }else |
| 2748 | |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 2749 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2750 | { |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2751 | fprintf(stderr, "unknown command or invalid arguments: " |
| 2752 | " \"%s\". Enter \".help\" for help\n", azArg[0]); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2753 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2754 | |
| 2755 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2758 | /* |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 2759 | ** Return TRUE if a semicolon occurs anywhere in the first N characters |
| 2760 | ** of string z[]. |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 2761 | */ |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 2762 | static int _contains_semicolon(const char *z, int N){ |
| 2763 | int i; |
| 2764 | for(i=0; i<N; i++){ if( z[i]==';' ) return 1; } |
| 2765 | return 0; |
drh | 324ccef | 2003-02-05 14:06:20 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | /* |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 2769 | ** Test to see if a line consists entirely of whitespace. |
| 2770 | */ |
| 2771 | static int _all_whitespace(const char *z){ |
| 2772 | for(; *z; z++){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 2773 | if( isspace(*(unsigned char*)z) ) continue; |
drh | 70c7a4b | 2003-04-26 03:03:06 +0000 | [diff] [blame] | 2774 | if( *z=='/' && z[1]=='*' ){ |
| 2775 | z += 2; |
| 2776 | while( *z && (*z!='*' || z[1]!='/') ){ z++; } |
| 2777 | if( *z==0 ) return 0; |
| 2778 | z++; |
| 2779 | continue; |
| 2780 | } |
| 2781 | if( *z=='-' && z[1]=='-' ){ |
| 2782 | z += 2; |
| 2783 | while( *z && *z!='\n' ){ z++; } |
| 2784 | if( *z==0 ) return 1; |
| 2785 | continue; |
| 2786 | } |
| 2787 | return 0; |
| 2788 | } |
| 2789 | return 1; |
| 2790 | } |
| 2791 | |
| 2792 | /* |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 2793 | ** Return TRUE if the line typed in is an SQL command terminator other |
| 2794 | ** than a semi-colon. The SQL Server style "go" command is understood |
| 2795 | ** as is the Oracle "/". |
| 2796 | */ |
| 2797 | static int _is_command_terminator(const char *zLine){ |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 2798 | while( isspace(*(unsigned char*)zLine) ){ zLine++; }; |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 2799 | if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){ |
| 2800 | return 1; /* Oracle */ |
| 2801 | } |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 2802 | if( tolower(zLine[0])=='g' && tolower(zLine[1])=='o' |
| 2803 | && _all_whitespace(&zLine[2]) ){ |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 2804 | return 1; /* SQL Server */ |
| 2805 | } |
| 2806 | return 0; |
| 2807 | } |
| 2808 | |
| 2809 | /* |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 2810 | ** Return true if zSql is a complete SQL statement. Return false if it |
| 2811 | ** ends in the middle of a string literal or C-style comment. |
| 2812 | */ |
| 2813 | static int _is_complete(char *zSql, int nSql){ |
| 2814 | int rc; |
| 2815 | if( zSql==0 ) return 1; |
| 2816 | zSql[nSql] = ';'; |
| 2817 | zSql[nSql+1] = 0; |
| 2818 | rc = sqlite3_complete(zSql); |
| 2819 | zSql[nSql] = 0; |
| 2820 | return rc; |
| 2821 | } |
| 2822 | |
| 2823 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2824 | ** Read input from *in and process it. If *in==0 then input |
| 2825 | ** is interactive - the user is typing it it. Otherwise, input |
| 2826 | ** is coming from a file or device. A prompt is issued and history |
| 2827 | ** is saved only if input is interactive. An interrupt signal will |
| 2828 | ** cause this routine to exit immediately, unless input is interactive. |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2829 | ** |
| 2830 | ** Return the number of errors. |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2831 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2832 | static int process_input(struct callback_data *p, FILE *in){ |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 2833 | char *zLine = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2834 | char *zSql = 0; |
| 2835 | int nSql = 0; |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 2836 | int nSqlPrior = 0; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2837 | char *zErrMsg; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2838 | int rc; |
| 2839 | int errCnt = 0; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2840 | int lineno = 0; |
| 2841 | int startline = 0; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2842 | |
| 2843 | while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){ |
| 2844 | fflush(p->out); |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 2845 | free(zLine); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2846 | zLine = one_input_line(zSql, in); |
| 2847 | if( zLine==0 ){ |
| 2848 | break; /* We have reached EOF */ |
| 2849 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2850 | if( seenInterrupt ){ |
| 2851 | if( in!=0 ) break; |
| 2852 | seenInterrupt = 0; |
| 2853 | } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2854 | lineno++; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2855 | if( p->echoOn ) printf("%s\n", zLine); |
drh | f817b6b | 2003-06-16 00:16:41 +0000 | [diff] [blame] | 2856 | if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue; |
drh | 2af0b2d | 2002-02-21 02:25:02 +0000 | [diff] [blame] | 2857 | if( zLine && zLine[0]=='.' && nSql==0 ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2858 | rc = do_meta_command(zLine, p); |
drh | 47ad684 | 2006-11-08 12:25:42 +0000 | [diff] [blame] | 2859 | if( rc==2 ){ |
| 2860 | break; |
| 2861 | }else if( rc ){ |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2862 | errCnt++; |
| 2863 | } |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2864 | continue; |
| 2865 | } |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 2866 | if( _is_command_terminator(zLine) && _is_complete(zSql, nSql) ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2867 | memcpy(zLine,";",2); |
drh | a9b1716 | 2003-04-29 18:01:28 +0000 | [diff] [blame] | 2868 | } |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 2869 | nSqlPrior = nSql; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2870 | if( zSql==0 ){ |
| 2871 | int i; |
drh | 4c755c0 | 2004-08-08 20:22:17 +0000 | [diff] [blame] | 2872 | for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){} |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2873 | if( zLine[i]!=0 ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2874 | nSql = strlen30(zLine); |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 2875 | zSql = malloc( nSql+3 ); |
drh | c1f4494 | 2006-05-10 14:39:13 +0000 | [diff] [blame] | 2876 | if( zSql==0 ){ |
| 2877 | fprintf(stderr, "out of memory\n"); |
| 2878 | exit(1); |
| 2879 | } |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2880 | memcpy(zSql, zLine, nSql+1); |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2881 | startline = lineno; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2882 | } |
| 2883 | }else{ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2884 | int len = strlen30(zLine); |
drh | 233a531 | 2008-12-18 22:25:13 +0000 | [diff] [blame] | 2885 | zSql = realloc( zSql, nSql + len + 4 ); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2886 | if( zSql==0 ){ |
| 2887 | fprintf(stderr,"%s: out of memory!\n", Argv0); |
| 2888 | exit(1); |
| 2889 | } |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2890 | zSql[nSql++] = '\n'; |
| 2891 | memcpy(&zSql[nSql], zLine, len+1); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2892 | nSql += len; |
| 2893 | } |
drh | 91a6639 | 2007-09-07 01:12:32 +0000 | [diff] [blame] | 2894 | if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) |
| 2895 | && sqlite3_complete(zSql) ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2896 | p->cnt = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 2897 | open_db(p); |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 2898 | BEGIN_TIMER; |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 2899 | rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg); |
drh | 3b1a988 | 2007-11-02 12:53:03 +0000 | [diff] [blame] | 2900 | END_TIMER; |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 2901 | if( rc || zErrMsg ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2902 | char zPrefix[100]; |
| 2903 | if( in!=0 || !stdin_is_interactive ){ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2904 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, |
| 2905 | "SQL error near line %d:", startline); |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2906 | }else{ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2907 | sqlite3_snprintf(sizeof(zPrefix), zPrefix, "SQL error:"); |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2908 | } |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 2909 | if( zErrMsg!=0 ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2910 | printf("%s %s\n", zPrefix, zErrMsg); |
drh | 3f4fedb | 2004-05-31 19:34:33 +0000 | [diff] [blame] | 2911 | sqlite3_free(zErrMsg); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 2912 | zErrMsg = 0; |
| 2913 | }else{ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 2914 | printf("%s %s\n", zPrefix, sqlite3_errmsg(p->db)); |
drh | 7f953e2 | 2002-07-13 17:33:45 +0000 | [diff] [blame] | 2915 | } |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2916 | errCnt++; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2917 | } |
| 2918 | free(zSql); |
| 2919 | zSql = 0; |
| 2920 | nSql = 0; |
| 2921 | } |
| 2922 | } |
| 2923 | if( zSql ){ |
drh | dfef499 | 2008-11-11 18:55:03 +0000 | [diff] [blame] | 2924 | if( !_all_whitespace(zSql) ) fprintf(stderr, "Incomplete SQL: %s\n", zSql); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2925 | free(zSql); |
| 2926 | } |
danielk1977 | 2ac2762 | 2007-07-03 05:31:16 +0000 | [diff] [blame] | 2927 | free(zLine); |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 2928 | return errCnt; |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2931 | /* |
| 2932 | ** Return a pathname which is the user's home directory. A |
| 2933 | ** 0 return indicates an error of some kind. Space to hold the |
| 2934 | ** resulting string is obtained from malloc(). The calling |
| 2935 | ** function should free the result. |
| 2936 | */ |
| 2937 | static char *find_home_dir(void){ |
| 2938 | char *home_dir = NULL; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 2939 | |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 2940 | #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL) |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2941 | struct passwd *pwent; |
| 2942 | uid_t uid = getuid(); |
drh | bd842ba | 2002-08-21 11:26:41 +0000 | [diff] [blame] | 2943 | if( (pwent=getpwuid(uid)) != NULL) { |
| 2944 | home_dir = pwent->pw_dir; |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2945 | } |
| 2946 | #endif |
| 2947 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 2948 | #if defined(_WIN32_WCE) |
| 2949 | /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() |
| 2950 | */ |
| 2951 | home_dir = strdup("/"); |
| 2952 | #else |
| 2953 | |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 2954 | #if defined(_WIN32) || defined(WIN32) || defined(__OS2__) |
| 2955 | if (!home_dir) { |
| 2956 | home_dir = getenv("USERPROFILE"); |
| 2957 | } |
| 2958 | #endif |
| 2959 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2960 | if (!home_dir) { |
| 2961 | home_dir = getenv("HOME"); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
drh | cdb36b7 | 2006-06-12 12:57:45 +0000 | [diff] [blame] | 2964 | #if defined(_WIN32) || defined(WIN32) || defined(__OS2__) |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 2965 | if (!home_dir) { |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 2966 | char *zDrive, *zPath; |
| 2967 | int n; |
| 2968 | zDrive = getenv("HOMEDRIVE"); |
| 2969 | zPath = getenv("HOMEPATH"); |
| 2970 | if( zDrive && zPath ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2971 | n = strlen30(zDrive) + strlen30(zPath) + 1; |
drh | 164a1b6 | 2006-08-19 11:15:20 +0000 | [diff] [blame] | 2972 | home_dir = malloc( n ); |
| 2973 | if( home_dir==0 ) return 0; |
| 2974 | sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); |
| 2975 | return home_dir; |
| 2976 | } |
| 2977 | home_dir = "c:\\"; |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 2978 | } |
| 2979 | #endif |
| 2980 | |
chw | 65d3c13 | 2007-11-12 21:09:10 +0000 | [diff] [blame] | 2981 | #endif /* !_WIN32_WCE */ |
| 2982 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2983 | if( home_dir ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 2984 | int n = strlen30(home_dir) + 1; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 2985 | char *z = malloc( n ); |
| 2986 | if( z ) memcpy(z, home_dir, n); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2987 | home_dir = z; |
| 2988 | } |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 2989 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 2990 | return home_dir; |
| 2991 | } |
| 2992 | |
| 2993 | /* |
| 2994 | ** Read input from the file given by sqliterc_override. Or if that |
| 2995 | ** parameter is NULL, take input from ~/.sqliterc |
| 2996 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 2997 | static void process_sqliterc( |
| 2998 | struct callback_data *p, /* Configuration data */ |
| 2999 | const char *sqliterc_override /* Name of config file. NULL to use default */ |
| 3000 | ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3001 | char *home_dir = NULL; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3002 | const char *sqliterc = sqliterc_override; |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 3003 | char *zBuf = 0; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3004 | FILE *in = NULL; |
drh | a959ac4 | 2007-06-20 13:10:00 +0000 | [diff] [blame] | 3005 | int nBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3006 | |
| 3007 | if (sqliterc == NULL) { |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3008 | home_dir = find_home_dir(); |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 3009 | if( home_dir==0 ){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3010 | #if !defined(__RTP__) && !defined(_WRS_KERNEL) |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 3011 | fprintf(stderr,"%s: cannot locate your home directory!\n", Argv0); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 3012 | #endif |
drh | e98d4fa | 2002-04-21 19:06:22 +0000 | [diff] [blame] | 3013 | return; |
| 3014 | } |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 3015 | nBuf = strlen30(home_dir) + 16; |
drh | a959ac4 | 2007-06-20 13:10:00 +0000 | [diff] [blame] | 3016 | zBuf = malloc( nBuf ); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3017 | if( zBuf==0 ){ |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3018 | fprintf(stderr,"%s: out of memory!\n", Argv0); |
| 3019 | exit(1); |
| 3020 | } |
drh | a959ac4 | 2007-06-20 13:10:00 +0000 | [diff] [blame] | 3021 | sqlite3_snprintf(nBuf, zBuf,"%s/.sqliterc",home_dir); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3022 | free(home_dir); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3023 | sqliterc = (const char*)zBuf; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3024 | } |
drh | a1f9b5e | 2004-02-14 16:31:02 +0000 | [diff] [blame] | 3025 | in = fopen(sqliterc,"rb"); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3026 | if( in ){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3027 | if( stdin_is_interactive ){ |
drh | b695aca | 2007-07-30 20:41:52 +0000 | [diff] [blame] | 3028 | printf("-- Loading resources from %s\n",sqliterc); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3029 | } |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3030 | process_input(p,in); |
drh | dd45df8 | 2002-04-18 12:39:03 +0000 | [diff] [blame] | 3031 | fclose(in); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3032 | } |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 3033 | free(zBuf); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3034 | return; |
| 3035 | } |
| 3036 | |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3037 | /* |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 3038 | ** Show available command line options |
| 3039 | */ |
| 3040 | static const char zOptions[] = |
| 3041 | " -init filename read/process named file\n" |
| 3042 | " -echo print commands before execution\n" |
| 3043 | " -[no]header turn headers on or off\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 3044 | " -bail stop after hitting an error\n" |
| 3045 | " -interactive force interactive I/O\n" |
| 3046 | " -batch force batch I/O\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 3047 | " -column set output mode to 'column'\n" |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 3048 | " -csv set output mode to 'csv'\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 3049 | " -html set output mode to HTML\n" |
| 3050 | " -line set output mode to 'line'\n" |
| 3051 | " -list set output mode to 'list'\n" |
| 3052 | " -separator 'x' set output field separator (|)\n" |
| 3053 | " -nullvalue 'text' set text string for NULL values\n" |
| 3054 | " -version show SQLite version\n" |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 3055 | ; |
| 3056 | static void usage(int showDetail){ |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 3057 | fprintf(stderr, |
| 3058 | "Usage: %s [OPTIONS] FILENAME [SQL]\n" |
| 3059 | "FILENAME is the name of an SQLite database. A new database is created\n" |
| 3060 | "if the file does not previously exist.\n", Argv0); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 3061 | if( showDetail ){ |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 3062 | fprintf(stderr, "OPTIONS include:\n%s", zOptions); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 3063 | }else{ |
| 3064 | fprintf(stderr, "Use the -help option for additional information\n"); |
| 3065 | } |
| 3066 | exit(1); |
| 3067 | } |
| 3068 | |
| 3069 | /* |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3070 | ** Initialize the state information in data |
| 3071 | */ |
drh | 0850b53 | 2006-01-31 19:31:43 +0000 | [diff] [blame] | 3072 | static void main_init(struct callback_data *data) { |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3073 | memset(data, 0, sizeof(*data)); |
| 3074 | data->mode = MODE_List; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 3075 | memcpy(data->separator,"|", 2); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3076 | data->showHeader = 0; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 3077 | sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); |
| 3078 | sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3081 | int main(int argc, char **argv){ |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3082 | char *zErrMsg = 0; |
| 3083 | struct callback_data data; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3084 | const char *zInitFile = 0; |
| 3085 | char *zFirstCmd = 0; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3086 | int i; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3087 | int rc = 0; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3088 | |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3089 | Argv0 = argv[0]; |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3090 | main_init(&data); |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3091 | stdin_is_interactive = isatty(0); |
persicom | 7e2dfdd | 2002-04-18 02:46:52 +0000 | [diff] [blame] | 3092 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3093 | /* Make sure we have a valid signal handler early, before anything |
| 3094 | ** else is done. |
| 3095 | */ |
drh | 4c50439 | 2000-10-16 22:06:40 +0000 | [diff] [blame] | 3096 | #ifdef SIGINT |
| 3097 | signal(SIGINT, interrupt_handler); |
| 3098 | #endif |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3099 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3100 | /* Do an initial pass through the command-line argument to locate |
| 3101 | ** the name of the database file, the name of the initialization file, |
| 3102 | ** and the first command to execute. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3103 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3104 | for(i=1; i<argc-1; i++){ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3105 | char *z; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3106 | if( argv[i][0]!='-' ) break; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3107 | z = argv[i]; |
| 3108 | if( z[0]=='-' && z[1]=='-' ) z++; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3109 | if( strcmp(argv[i],"-separator")==0 || strcmp(argv[i],"-nullvalue")==0 ){ |
| 3110 | i++; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3111 | }else if( strcmp(argv[i],"-init")==0 ){ |
| 3112 | i++; |
| 3113 | zInitFile = argv[i]; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3114 | } |
| 3115 | } |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3116 | if( i<argc ){ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 3117 | #if defined(SQLITE_OS_OS2) && SQLITE_OS_OS2 |
pweilbacher | d190be8 | 2008-04-15 18:50:02 +0000 | [diff] [blame] | 3118 | data.zDbFilename = (const char *)convertCpPathToUtf8( argv[i++] ); |
| 3119 | #else |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3120 | data.zDbFilename = argv[i++]; |
pweilbacher | d190be8 | 2008-04-15 18:50:02 +0000 | [diff] [blame] | 3121 | #endif |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3122 | }else{ |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 3123 | #ifndef SQLITE_OMIT_MEMORYDB |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3124 | data.zDbFilename = ":memory:"; |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 3125 | #else |
| 3126 | data.zDbFilename = 0; |
| 3127 | #endif |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3128 | } |
| 3129 | if( i<argc ){ |
| 3130 | zFirstCmd = argv[i++]; |
| 3131 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3132 | data.out = stdout; |
| 3133 | |
drh | 01b4171 | 2005-08-29 23:06:23 +0000 | [diff] [blame] | 3134 | #ifdef SQLITE_OMIT_MEMORYDB |
| 3135 | if( data.zDbFilename==0 ){ |
| 3136 | fprintf(stderr,"%s: no database filename specified\n", argv[0]); |
| 3137 | exit(1); |
| 3138 | } |
| 3139 | #endif |
| 3140 | |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3141 | /* Go ahead and open the database file if it already exists. If the |
| 3142 | ** file does not exist, delay opening it. This prevents empty database |
| 3143 | ** files from being created if a user mistypes the database name argument |
| 3144 | ** to the sqlite command-line tool. |
| 3145 | */ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 3146 | if( access(data.zDbFilename, 0)==0 ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3147 | open_db(&data); |
| 3148 | } |
| 3149 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3150 | /* Process the initialization file if there is one. If no -init option |
| 3151 | ** is given on the command line, look for a file named ~/.sqliterc and |
| 3152 | ** try to process it. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3153 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3154 | process_sqliterc(&data,zInitFile); |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3155 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3156 | /* Make a second pass through the command-line argument and set |
| 3157 | ** options. This second pass is delayed until after the initialization |
| 3158 | ** file is processed so that the command-line arguments will override |
| 3159 | ** settings in the initialization file. |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3160 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3161 | for(i=1; i<argc && argv[i][0]=='-'; i++){ |
| 3162 | char *z = argv[i]; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3163 | if( z[1]=='-' ){ z++; } |
drh | 2e584cd | 2006-09-25 13:09:22 +0000 | [diff] [blame] | 3164 | if( strcmp(z,"-init")==0 ){ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3165 | i++; |
| 3166 | }else if( strcmp(z,"-html")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 3167 | data.mode = MODE_Html; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3168 | }else if( strcmp(z,"-list")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 3169 | data.mode = MODE_List; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3170 | }else if( strcmp(z,"-line")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 3171 | data.mode = MODE_Line; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3172 | }else if( strcmp(z,"-column")==0 ){ |
drh | 8b32e17 | 2002-04-08 02:42:57 +0000 | [diff] [blame] | 3173 | data.mode = MODE_Column; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 3174 | }else if( strcmp(z,"-csv")==0 ){ |
| 3175 | data.mode = MODE_Csv; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 3176 | memcpy(data.separator,",",2); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3177 | }else if( strcmp(z,"-separator")==0 ){ |
| 3178 | i++; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 3179 | sqlite3_snprintf(sizeof(data.separator), data.separator, |
| 3180 | "%.*s",(int)sizeof(data.separator)-1,argv[i]); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3181 | }else if( strcmp(z,"-nullvalue")==0 ){ |
| 3182 | i++; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 3183 | sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue, |
| 3184 | "%.*s",(int)sizeof(data.nullvalue)-1,argv[i]); |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3185 | }else if( strcmp(z,"-header")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 3186 | data.showHeader = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3187 | }else if( strcmp(z,"-noheader")==0 ){ |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 3188 | data.showHeader = 0; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3189 | }else if( strcmp(z,"-echo")==0 ){ |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3190 | data.echoOn = 1; |
drh | c49f44e | 2006-10-26 18:15:42 +0000 | [diff] [blame] | 3191 | }else if( strcmp(z,"-bail")==0 ){ |
| 3192 | bail_on_error = 1; |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3193 | }else if( strcmp(z,"-version")==0 ){ |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 3194 | printf("%s\n", sqlite3_libversion()); |
drh | 151e3e1 | 2006-06-06 12:32:21 +0000 | [diff] [blame] | 3195 | return 0; |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3196 | }else if( strcmp(z,"-interactive")==0 ){ |
| 3197 | stdin_is_interactive = 1; |
| 3198 | }else if( strcmp(z,"-batch")==0 ){ |
| 3199 | stdin_is_interactive = 0; |
drh | 80e8be9 | 2006-08-29 12:04:19 +0000 | [diff] [blame] | 3200 | }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){ |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 3201 | usage(1); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 3202 | }else{ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3203 | fprintf(stderr,"%s: unknown option: %s\n", Argv0, z); |
drh | e1e38c4 | 2003-05-04 18:30:59 +0000 | [diff] [blame] | 3204 | fprintf(stderr,"Use -help for a list of options.\n"); |
drh | 1e5d0e9 | 2000-05-31 23:33:17 +0000 | [diff] [blame] | 3205 | return 1; |
| 3206 | } |
| 3207 | } |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3208 | |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3209 | if( zFirstCmd ){ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3210 | /* Run just the command that follows the database name |
| 3211 | */ |
drh | 22fbcb8 | 2004-02-01 01:22:50 +0000 | [diff] [blame] | 3212 | if( zFirstCmd[0]=='.' ){ |
| 3213 | do_meta_command(zFirstCmd, &data); |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 3214 | exit(0); |
| 3215 | }else{ |
| 3216 | int rc; |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3217 | open_db(&data); |
danielk1977 | 6f8a503 | 2004-05-10 10:34:51 +0000 | [diff] [blame] | 3218 | rc = sqlite3_exec(data.db, zFirstCmd, callback, &data, &zErrMsg); |
drh | 6ff1385 | 2001-11-25 13:18:23 +0000 | [diff] [blame] | 3219 | if( rc!=0 && zErrMsg!=0 ){ |
| 3220 | fprintf(stderr,"SQL error: %s\n", zErrMsg); |
| 3221 | exit(1); |
| 3222 | } |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3223 | } |
| 3224 | }else{ |
drh | 44c2eb1 | 2003-04-30 11:38:26 +0000 | [diff] [blame] | 3225 | /* Run commands received from standard input |
| 3226 | */ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3227 | if( stdin_is_interactive ){ |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3228 | char *zHome; |
| 3229 | char *zHistory = 0; |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 3230 | int nHistory; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3231 | printf( |
drh | b217a57 | 2000-08-22 13:40:18 +0000 | [diff] [blame] | 3232 | "SQLite version %s\n" |
mihailim | 65df9db | 2008-06-28 11:29:22 +0000 | [diff] [blame] | 3233 | "Enter \".help\" for instructions\n" |
| 3234 | "Enter SQL statements terminated with a \";\"\n", |
drh | c8d7441 | 2004-08-31 23:41:26 +0000 | [diff] [blame] | 3235 | sqlite3_libversion() |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3236 | ); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3237 | zHome = find_home_dir(); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 3238 | if( zHome ){ |
drh | 4f21c4a | 2008-12-10 22:15:00 +0000 | [diff] [blame] | 3239 | nHistory = strlen30(zHome) + 20; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 3240 | if( (zHistory = malloc(nHistory))!=0 ){ |
| 3241 | sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); |
| 3242 | } |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3243 | } |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 3244 | #if defined(HAVE_READLINE) && HAVE_READLINE==1 |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3245 | if( zHistory ) read_history(zHistory); |
danielk1977 | 4af00c6 | 2005-01-23 23:43:21 +0000 | [diff] [blame] | 3246 | #endif |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3247 | rc = process_input(&data, 0); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3248 | if( zHistory ){ |
| 3249 | stifle_history(100); |
| 3250 | write_history(zHistory); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 3251 | free(zHistory); |
drh | 67505e7 | 2002-04-19 12:34:06 +0000 | [diff] [blame] | 3252 | } |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 3253 | free(zHome); |
drh | daffd0e | 2001-04-11 14:28:42 +0000 | [diff] [blame] | 3254 | }else{ |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3255 | rc = process_input(&data, stdin); |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3256 | } |
| 3257 | } |
drh | 33048c0 | 2001-10-01 14:29:22 +0000 | [diff] [blame] | 3258 | set_table_name(&data, 0); |
adamd | 0a3daa3 | 2006-07-28 20:16:14 +0000 | [diff] [blame] | 3259 | if( db ){ |
| 3260 | if( sqlite3_close(db)!=SQLITE_OK ){ |
| 3261 | fprintf(stderr,"error closing database: %s\n", sqlite3_errmsg(db)); |
| 3262 | } |
| 3263 | } |
drh | c28490c | 2006-10-26 14:25:58 +0000 | [diff] [blame] | 3264 | return rc; |
drh | 7589723 | 2000-05-29 14:26:00 +0000 | [diff] [blame] | 3265 | } |